class-file-upload-upgrader.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Upgrade API: File_Upload_Upgrader class
  4. *
  5. * @package WordPress
  6. * @subpackage Upgrader
  7. * @since 4.6.0
  8. */
  9. /**
  10. * Core class used for handling file uploads.
  11. *
  12. * This class handles the upload process and passes it as if it's a local file
  13. * to the Upgrade/Installer functions.
  14. *
  15. * @since 2.8.0
  16. * @since 4.6.0 Moved to its own file from wp-admin/includes/class-wp-upgrader.php.
  17. */
  18. class File_Upload_Upgrader {
  19. /**
  20. * The full path to the file package.
  21. *
  22. * @since 2.8.0
  23. * @var string $package
  24. */
  25. public $package;
  26. /**
  27. * The name of the file.
  28. *
  29. * @since 2.8.0
  30. * @var string $filename
  31. */
  32. public $filename;
  33. /**
  34. * The ID of the attachment post for this file.
  35. *
  36. * @since 3.3.0
  37. * @var int $id
  38. */
  39. public $id = 0;
  40. /**
  41. * Construct the upgrader for a form.
  42. *
  43. * @since 2.8.0
  44. *
  45. * @param string $form The name of the form the file was uploaded from.
  46. * @param string $urlholder The name of the `GET` parameter that holds the filename.
  47. */
  48. public function __construct( $form, $urlholder ) {
  49. if ( empty($_FILES[$form]['name']) && empty($_GET[$urlholder]) )
  50. wp_die(__('Please select a file'));
  51. //Handle a newly uploaded file, Else assume it's already been uploaded
  52. if ( ! empty($_FILES) ) {
  53. $overrides = array( 'test_form' => false, 'test_type' => false );
  54. $file = wp_handle_upload( $_FILES[$form], $overrides );
  55. if ( isset( $file['error'] ) )
  56. wp_die( $file['error'] );
  57. $this->filename = $_FILES[$form]['name'];
  58. $this->package = $file['file'];
  59. // Construct the object array
  60. $object = array(
  61. 'post_title' => $this->filename,
  62. 'post_content' => $file['url'],
  63. 'post_mime_type' => $file['type'],
  64. 'guid' => $file['url'],
  65. 'context' => 'upgrader',
  66. 'post_status' => 'private'
  67. );
  68. // Save the data.
  69. $this->id = wp_insert_attachment( $object, $file['file'] );
  70. // Schedule a cleanup for 2 hours from now in case of failed installation.
  71. wp_schedule_single_event( time() + 2 * HOUR_IN_SECONDS, 'upgrader_scheduled_cleanup', array( $this->id ) );
  72. } elseif ( is_numeric( $_GET[$urlholder] ) ) {
  73. // Numeric Package = previously uploaded file, see above.
  74. $this->id = (int) $_GET[$urlholder];
  75. $attachment = get_post( $this->id );
  76. if ( empty($attachment) )
  77. wp_die(__('Please select a file'));
  78. $this->filename = $attachment->post_title;
  79. $this->package = get_attached_file( $attachment->ID );
  80. } else {
  81. // Else, It's set to something, Back compat for plugins using the old (pre-3.3) File_Uploader handler.
  82. if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) )
  83. wp_die( $uploads['error'] );
  84. $this->filename = sanitize_file_name( $_GET[ $urlholder ] );
  85. $this->package = $uploads['basedir'] . '/' . $this->filename;
  86. if ( 0 !== strpos( realpath( $this->package ), realpath( $uploads['basedir'] ) ) ) {
  87. wp_die( __( 'Please select a file' ) );
  88. }
  89. }
  90. }
  91. /**
  92. * Delete the attachment/uploaded file.
  93. *
  94. * @since 3.2.2
  95. *
  96. * @return bool Whether the cleanup was successful.
  97. */
  98. public function cleanup() {
  99. if ( $this->id )
  100. wp_delete_attachment( $this->id );
  101. elseif ( file_exists( $this->package ) )
  102. return @unlink( $this->package );
  103. return true;
  104. }
  105. }