wp/wp-admin/includes/class-file-upload-upgrader.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    50 	 * @param string $form      The name of the form the file was uploaded from.
    50 	 * @param string $form      The name of the form the file was uploaded from.
    51 	 * @param string $urlholder The name of the `GET` parameter that holds the filename.
    51 	 * @param string $urlholder The name of the `GET` parameter that holds the filename.
    52 	 */
    52 	 */
    53 	public function __construct( $form, $urlholder ) {
    53 	public function __construct( $form, $urlholder ) {
    54 
    54 
    55 		if ( empty($_FILES[$form]['name']) && empty($_GET[$urlholder]) )
    55 		if ( empty( $_FILES[ $form ]['name'] ) && empty( $_GET[ $urlholder ] ) ) {
    56 			wp_die(__('Please select a file'));
    56 			wp_die( __( 'Please select a file' ) );
       
    57 		}
    57 
    58 
    58 		//Handle a newly uploaded file, Else assume it's already been uploaded
    59 		//Handle a newly uploaded file, Else assume it's already been uploaded
    59 		if ( ! empty($_FILES) ) {
    60 		if ( ! empty( $_FILES ) ) {
    60 			$overrides = array( 'test_form' => false, 'test_type' => false );
    61 			$overrides = array(
    61 			$file = wp_handle_upload( $_FILES[$form], $overrides );
    62 				'test_form' => false,
       
    63 				'test_type' => false,
       
    64 			);
       
    65 			$file      = wp_handle_upload( $_FILES[ $form ], $overrides );
    62 
    66 
    63 			if ( isset( $file['error'] ) )
    67 			if ( isset( $file['error'] ) ) {
    64 				wp_die( $file['error'] );
    68 				wp_die( $file['error'] );
       
    69 			}
    65 
    70 
    66 			$this->filename = $_FILES[$form]['name'];
    71 			$this->filename = $_FILES[ $form ]['name'];
    67 			$this->package = $file['file'];
    72 			$this->package  = $file['file'];
    68 
    73 
    69 			// Construct the object array
    74 			// Construct the object array
    70 			$object = array(
    75 			$object = array(
    71 				'post_title' => $this->filename,
    76 				'post_title'     => $this->filename,
    72 				'post_content' => $file['url'],
    77 				'post_content'   => $file['url'],
    73 				'post_mime_type' => $file['type'],
    78 				'post_mime_type' => $file['type'],
    74 				'guid' => $file['url'],
    79 				'guid'           => $file['url'],
    75 				'context' => 'upgrader',
    80 				'context'        => 'upgrader',
    76 				'post_status' => 'private'
    81 				'post_status'    => 'private',
    77 			);
    82 			);
    78 
    83 
    79 			// Save the data.
    84 			// Save the data.
    80 			$this->id = wp_insert_attachment( $object, $file['file'] );
    85 			$this->id = wp_insert_attachment( $object, $file['file'] );
    81 
    86 
    82 			// Schedule a cleanup for 2 hours from now in case of failed installation.
    87 			// Schedule a cleanup for 2 hours from now in case of failed installation.
    83 			wp_schedule_single_event( time() + 2 * HOUR_IN_SECONDS, 'upgrader_scheduled_cleanup', array( $this->id ) );
    88 			wp_schedule_single_event( time() + 2 * HOUR_IN_SECONDS, 'upgrader_scheduled_cleanup', array( $this->id ) );
    84 
    89 
    85 		} elseif ( is_numeric( $_GET[$urlholder] ) ) {
    90 		} elseif ( is_numeric( $_GET[ $urlholder ] ) ) {
    86 			// Numeric Package = previously uploaded file, see above.
    91 			// Numeric Package = previously uploaded file, see above.
    87 			$this->id = (int) $_GET[$urlholder];
    92 			$this->id   = (int) $_GET[ $urlholder ];
    88 			$attachment = get_post( $this->id );
    93 			$attachment = get_post( $this->id );
    89 			if ( empty($attachment) )
    94 			if ( empty( $attachment ) ) {
    90 				wp_die(__('Please select a file'));
    95 				wp_die( __( 'Please select a file' ) );
       
    96 			}
    91 
    97 
    92 			$this->filename = $attachment->post_title;
    98 			$this->filename = $attachment->post_title;
    93 			$this->package = get_attached_file( $attachment->ID );
    99 			$this->package  = get_attached_file( $attachment->ID );
    94 		} else {
   100 		} else {
    95 			// Else, It's set to something, Back compat for plugins using the old (pre-3.3) File_Uploader handler.
   101 			// Else, It's set to something, Back compat for plugins using the old (pre-3.3) File_Uploader handler.
    96 			if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) )
   102 			if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) ) {
    97 				wp_die( $uploads['error'] );
   103 				wp_die( $uploads['error'] );
       
   104 			}
    98 
   105 
    99 			$this->filename = sanitize_file_name( $_GET[ $urlholder ] );
   106 			$this->filename = sanitize_file_name( $_GET[ $urlholder ] );
   100 			$this->package = $uploads['basedir'] . '/' . $this->filename;
   107 			$this->package  = $uploads['basedir'] . '/' . $this->filename;
   101 
   108 
   102 			if ( 0 !== strpos( realpath( $this->package ), realpath( $uploads['basedir'] ) ) ) {
   109 			if ( 0 !== strpos( realpath( $this->package ), realpath( $uploads['basedir'] ) ) ) {
   103 				wp_die( __( 'Please select a file' ) );
   110 				wp_die( __( 'Please select a file' ) );
   104 			}
   111 			}
   105 		}
   112 		}
   111 	 * @since 3.2.2
   118 	 * @since 3.2.2
   112 	 *
   119 	 *
   113 	 * @return bool Whether the cleanup was successful.
   120 	 * @return bool Whether the cleanup was successful.
   114 	 */
   121 	 */
   115 	public function cleanup() {
   122 	public function cleanup() {
   116 		if ( $this->id )
   123 		if ( $this->id ) {
   117 			wp_delete_attachment( $this->id );
   124 			wp_delete_attachment( $this->id );
   118 
   125 
   119 		elseif ( file_exists( $this->package ) )
   126 		} elseif ( file_exists( $this->package ) ) {
   120 			return @unlink( $this->package );
   127 			return @unlink( $this->package );
       
   128 		}
   121 
   129 
   122 		return true;
   130 		return true;
   123 	}
   131 	}
   124 }
   132 }