| 1 | <?php |
| 2 | /** |
| 3 | * Backend for regular file upload. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | * http://www.gnu.org/copyleft/gpl.html |
| 19 | * |
| 20 | * @file |
| 21 | * @ingroup Upload |
| 22 | */ |
| 23 | |
| 24 | use MediaWiki\Request\WebRequest; |
| 25 | use MediaWiki\Request\WebRequestUpload; |
| 26 | |
| 27 | /** |
| 28 | * Implements regular file uploads |
| 29 | * |
| 30 | * @ingroup Upload |
| 31 | * @author Bryan Tong Minh |
| 32 | */ |
| 33 | class UploadFromFile extends UploadBase { |
| 34 | /** |
| 35 | * @var WebRequestUpload |
| 36 | */ |
| 37 | protected $mUpload = null; |
| 38 | |
| 39 | /** |
| 40 | * @param WebRequest &$request |
| 41 | */ |
| 42 | public function initializeFromRequest( &$request ) { |
| 43 | $upload = $request->getUpload( 'wpUploadFile' ); |
| 44 | $desiredDestName = $request->getText( 'wpDestFile' ); |
| 45 | if ( !$desiredDestName ) { |
| 46 | $desiredDestName = $upload->getName(); |
| 47 | } |
| 48 | |
| 49 | // @phan-suppress-next-line PhanTypeMismatchArgumentNullable getName only null on failure |
| 50 | $this->initialize( $desiredDestName, $upload ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Initialize from a filename and a MediaWiki\Request\WebRequestUpload |
| 55 | * @param string $name |
| 56 | * @param WebRequestUpload $webRequestUpload |
| 57 | */ |
| 58 | public function initialize( $name, $webRequestUpload ) { |
| 59 | $this->mUpload = $webRequestUpload; |
| 60 | $this->initializePathInfo( $name, |
| 61 | $this->mUpload->getTempName(), $this->mUpload->getSize() ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @param WebRequest $request |
| 66 | * @return bool |
| 67 | */ |
| 68 | public static function isValidRequest( $request ) { |
| 69 | # Allow all requests, even if no file is present, so that an error |
| 70 | # because a post_max_size or upload_max_filesize overflow |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @return string |
| 76 | */ |
| 77 | public function getSourceType() { |
| 78 | return 'file'; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @return array |
| 83 | */ |
| 84 | public function verifyUpload() { |
| 85 | # Check for a post_max_size or upload_max_size overflow, so that a |
| 86 | # proper error can be shown to the user |
| 87 | if ( $this->mTempPath === null || $this->isEmptyFile() ) { |
| 88 | if ( $this->mUpload->isIniSizeOverflow() ) { |
| 89 | return [ |
| 90 | 'status' => UploadBase::FILE_TOO_LARGE, |
| 91 | 'max' => min( |
| 92 | self::getMaxUploadSize( $this->getSourceType() ), |
| 93 | self::getMaxPhpUploadSize() |
| 94 | ), |
| 95 | ]; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return parent::verifyUpload(); |
| 100 | } |
| 101 | } |