From b4310925bae4cda715f9c8e81c2c20011c403dba Mon Sep 17 00:00:00 2001 From: Bryan Tong Minh Date: Tue, 27 Jul 2010 21:53:52 +0000 Subject: [PATCH] Follow-up r70037: Fix ApiUpload by passing a WebRequestUpload to the the initializer Follow-up r64403 and r69911: Fix broken upload from stash in Api. Please tests your commits, even if the change seems totally harmless. You can use to automatically test uploading via the Api. --- includes/api/ApiUpload.php | 99 ++++++++++++++---------------- includes/upload/UploadFromFile.php | 25 ++++---- 2 files changed, 58 insertions(+), 66 deletions(-) diff --git a/includes/api/ApiUpload.php b/includes/api/ApiUpload.php index 16e6420e0f..fd8c493d06 100644 --- a/includes/api/ApiUpload.php +++ b/includes/api/ApiUpload.php @@ -54,72 +54,65 @@ class ApiUpload extends ApiBase { // One and only one of the following parameters is needed $this->requireOnlyOneParameter( $this->mParams, 'sessionkey', 'file', 'url' ); + // And this one is needed + if ( !isset( $this->mParams['filename'] ) ) { + $this->dieUsageMsg( array( 'missingparam', 'filename' ) ); + } + if ( $this->mParams['sessionkey'] ) { - /** - * Upload stashed in a previous request - */ - // Check the session key - if ( !isset( $_SESSION[$this->mUpload->getSessionKey()][$this->mParams['sessionkey']] ) ) { + // Upload stashed in a previous request + $sessionData = $request->getSessionData( UploadBase::SESSION_KEYNAME ); + if ( !UploadFromStash::isValidSessionKey( $this->mParams['sessionkey'], $sessionData ) ) { $this->dieUsageMsg( array( 'invalid-session-key' ) ); } $this->mUpload = new UploadFromStash(); $this->mUpload->initialize( $this->mParams['filename'], $this->mParams['sessionkey'], - $_SESSION[$this->mUpload->getSessionKey()][$this->mParams['sessionkey']] ); - } elseif ( isset( $this->mParams['filename'] ) ) { - /** - * Upload from URL, etc. - * Parameter filename is required - */ - if ( isset( $this->mParams['file'] ) ) { - $this->mUpload = new UploadFromFile(); - $this->mUpload->initialize( - $this->mParams['filename'], - $request->getFileTempName( 'file' ), - $request->getFileSize( 'file' ) - ); - } elseif ( isset( $this->mParams['url'] ) ) { - // make sure upload by URL is enabled: - if ( !$wgAllowCopyUploads ) { - $this->dieUsageMsg( array( 'copyuploaddisabled' ) ); - } + $sessionData[$this->mParams['sessionkey']] ); + + + } elseif ( isset( $this->mParams['file'] ) ) { + $this->mUpload = new UploadFromFile(); + $this->mUpload->initialize( + $this->mParams['filename'], + $request->getUpload( 'file' ) + ); + } elseif ( isset( $this->mParams['url'] ) ) { + // Make sure upload by URL is enabled: + if ( !$wgAllowCopyUploads ) { + $this->dieUsageMsg( array( 'copyuploaddisabled' ) ); + } - // make sure the current user can upload - if ( !$wgUser->isAllowed( 'upload_by_url' ) ) { - $this->dieUsageMsg( array( 'badaccess-groups' ) ); - } + $this->mUpload = new UploadFromUrl; + $async = $this->mParams['asyncdownload'] ? 'async' : null; + $this->checkPermissions( $wgUser ); - $this->mUpload = new UploadFromUrl; - $async = $this->mParams['asyncdownload'] ? 'async' : null; - - $result = $this->mUpload->initialize( $this->mParams['filename'], - $this->mParams['url'], - $this->mParams['comment'], - $this->mParams['watchlist'], - $this->mParams['ignorewarnings'], - $async ); - - $this->checkPermissions( $wgUser ); - if ( $async ) { - $this->getResult()->addValue( null, - $this->getModuleName(), - array( 'queued' => $result ) ); - return; - } + $result = $this->mUpload->initialize( + $this->mParams['filename'], + $this->mParams['url'], + $this->mParams['comment'], + $this->mParams['watchlist'], + $this->mParams['ignorewarnings'], + $async ); + + if ( $async ) { + $this->getResult()->addValue( null, + $this->getModuleName(), + array( 'queued' => $result ) ); + return; + } - $status = $this->mUpload->retrieveFileFromUrl(); - if ( !$status->isGood() ) { - $this->getResult()->addValue( null, - $this->getModuleName(), - array( 'error' => $status ) ); - return; - } + $status = $this->mUpload->retrieveFileFromUrl(); + if ( !$status->isGood() ) { + $this->getResult()->addValue( null, + $this->getModuleName(), + array( 'error' => $status ) ); + return; } - } else { - $this->dieUsageMsg( array( 'missingparam', 'filename' ) ); } + $this->checkPermissions( $wgUser ); diff --git a/includes/upload/UploadFromFile.php b/includes/upload/UploadFromFile.php index b653a47eb7..706da4dd5d 100644 --- a/includes/upload/UploadFromFile.php +++ b/includes/upload/UploadFromFile.php @@ -8,25 +8,24 @@ * Implements regular file uploads */ class UploadFromFile extends UploadBase { - protected $mWebUpload = null; + protected $mUpload = null; function initializeFromRequest( &$request ) { - $this->mWebUpload = $request->getUpload( 'wpUploadFile' ); - + $upload = $request->getUpload( 'wpUploadFile' ); $desiredDestName = $request->getText( 'wpDestFile' ); if( !$desiredDestName ) - $desiredDestName = $this->mWebUpload->getName(); - return $this->initializePathInfo( - $desiredDestName, - $this->mWebUpload->getTempName(), - $this->mWebUpload->getSize() - ); + $desiredDestName = $upload->getName(); + + return $this->initialize( $desiredDestName, $upload ); } + /** - * Entry point for upload from file. + * Initialize from a filename and a WebRequestUpload */ - function initialize( $name, $tempPath, $fileSize ) { - return $this->initializePathInfo( $name, $tempPath, $fileSize ); + function initialize( $name, $webRequestUpload ) { + $this->mUpload = $webRequestUpload; + return $this->initializePathInfo( $name, + $this->mUpload->getTempName(), $this->mUpload->getSize() ); } static function isValidRequest( $request ) { # Allow all requests, even if no file is present, so that an error @@ -38,7 +37,7 @@ class UploadFromFile extends UploadBase { # Check for a post_max_size or upload_max_size overflow, so that a # proper error can be shown to the user if ( is_null( $this->mTempPath ) || $this->isEmptyFile() ) { - if ( $this->mWebUpload->isIniSizeOverflow() ) { + if ( $this->mUpload->isIniSizeOverflow() ) { global $wgMaxUploadSize; return array( 'status' => self::FILE_TOO_LARGE, -- 2.20.1