summaryrefslogtreecommitdiff
path: root/includes/upload/UploadFromFile.php
blob: aa0cc77bbf9fa118faff1d66e6fbf4380e18791a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/**
 * Backend for regular file upload.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup Upload
 */

/**
 * Implements regular file uploads
 *
 * @ingroup Upload
 * @author Bryan Tong Minh
 */
class UploadFromFile extends UploadBase {

	/**
	 * @var WebRequestUpload
	 */
	protected $mUpload = null;

	/**
	 * @param $request WebRequest
	 */
	function initializeFromRequest( &$request ) {
		$upload = $request->getUpload( 'wpUploadFile' );
		$desiredDestName = $request->getText( 'wpDestFile' );
		if( !$desiredDestName ) {
			$desiredDestName = $upload->getName();
		}

		$this->initialize( $desiredDestName, $upload );
	}

	/**
	 * Initialize from a filename and a WebRequestUpload
	 * @param $name
	 * @param $webRequestUpload
	 */
	function initialize( $name, $webRequestUpload ) {
		$this->mUpload = $webRequestUpload;
		$this->initializePathInfo( $name,
			$this->mUpload->getTempName(), $this->mUpload->getSize() );
	}

	/**
	 * @param $request
	 * @return bool
	 */
	static function isValidRequest( $request ) {
		# Allow all requests, even if no file is present, so that an error
		# because a post_max_size or upload_max_filesize overflow
		return true;
	}

	/**
	 * @return string
	 */
	public function getSourceType() {
		return 'file';
	}

	/**
	 * @return array
	 */
	public function verifyUpload() {
		# 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->mUpload->isIniSizeOverflow() ) {
				return array( 
					'status' => UploadBase::FILE_TOO_LARGE,
					'max' => min( 
						self::getMaxUploadSize( $this->getSourceType() ), 
						wfShorthandToInteger( ini_get( 'upload_max_filesize' ) ), 
						wfShorthandToInteger( ini_get( 'post_max_size' ) )
					),
				);
			}
		}
		
		return parent::verifyUpload();
	}
}