Big oops - merged to wrong branch.
[lhc/web/wiklou.git] / includes / upload / UploadFromFile.php
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 /**
25 * Implements regular file uploads
26 *
27 * @ingroup Upload
28 * @author Bryan Tong Minh
29 */
30 class UploadFromFile extends UploadBase {
31
32 /**
33 * @var WebRequestUpload
34 */
35 protected $mUpload = null;
36
37 /**
38 * @param $request WebRequest
39 * @return null
40 */
41 function initializeFromRequest( &$request ) {
42 $upload = $request->getUpload( 'wpUploadFile' );
43 $desiredDestName = $request->getText( 'wpDestFile' );
44 if( !$desiredDestName )
45 $desiredDestName = $upload->getName();
46
47 return $this->initialize( $desiredDestName, $upload );
48 }
49
50 /**
51 * Initialize from a filename and a WebRequestUpload
52 * @param $name
53 * @param $webRequestUpload
54 * @return null
55 */
56 function initialize( $name, $webRequestUpload ) {
57 $this->mUpload = $webRequestUpload;
58 return $this->initializePathInfo( $name,
59 $this->mUpload->getTempName(), $this->mUpload->getSize() );
60 }
61
62 /**
63 * @param $request
64 * @return bool
65 */
66 static function isValidRequest( $request ) {
67 # Allow all requests, even if no file is present, so that an error
68 # because a post_max_size or upload_max_filesize overflow
69 return true;
70 }
71
72 /**
73 * @return string
74 */
75 public function getSourceType() {
76 return 'file';
77 }
78
79 /**
80 * @return array
81 */
82 public function verifyUpload() {
83 # Check for a post_max_size or upload_max_size overflow, so that a
84 # proper error can be shown to the user
85 if ( is_null( $this->mTempPath ) || $this->isEmptyFile() ) {
86 if ( $this->mUpload->isIniSizeOverflow() ) {
87 return array(
88 'status' => UploadBase::FILE_TOO_LARGE,
89 'max' => min(
90 self::getMaxUploadSize( $this->getSourceType() ),
91 wfShorthandToInteger( ini_get( 'upload_max_filesize' ) ),
92 wfShorthandToInteger( ini_get( 'post_max_size' ) )
93 ),
94 );
95 }
96 }
97
98 return parent::verifyUpload();
99 }
100 }