Remove HWLDFWordAccumulator, deprecated in 1.28
[lhc/web/wiklou.git] / includes / WebRequestUpload.php
1 <?php
2 /**
3 * Object to access the $_FILES array
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 */
22
23 use MediaWiki\MediaWikiServices;
24
25 /**
26 * Object to access the $_FILES array
27 *
28 * @ingroup HTTP
29 */
30 class WebRequestUpload {
31 protected $request;
32 protected $doesExist;
33 protected $fileInfo;
34
35 /**
36 * Constructor. Should only be called by WebRequest
37 *
38 * @param WebRequest $request The associated request
39 * @param string $key Key in $_FILES array (name of form field)
40 */
41 public function __construct( $request, $key ) {
42 $this->request = $request;
43 $this->doesExist = isset( $_FILES[$key] );
44 if ( $this->doesExist ) {
45 $this->fileInfo = $_FILES[$key];
46 }
47 }
48
49 /**
50 * Return whether a file with this name was uploaded.
51 *
52 * @return bool
53 */
54 public function exists() {
55 return $this->doesExist;
56 }
57
58 /**
59 * Return the original filename of the uploaded file
60 *
61 * @return string|null Filename or null if non-existent
62 */
63 public function getName() {
64 if ( !$this->exists() ) {
65 return null;
66 }
67
68 $name = $this->fileInfo['name'];
69
70 # Safari sends filenames in HTML-encoded Unicode form D...
71 # Horrid and evil! Let's try to make some kind of sense of it.
72 $name = Sanitizer::decodeCharReferences( $name );
73 $name = MediaWikiServices::getInstance()->getContentLanguage()->normalize( $name );
74 wfDebug( __METHOD__ . ": {$this->fileInfo['name']} normalized to '$name'\n" );
75 return $name;
76 }
77
78 /**
79 * Return the file size of the uploaded file
80 *
81 * @return int File size or zero if non-existent
82 */
83 public function getSize() {
84 if ( !$this->exists() ) {
85 return 0;
86 }
87
88 return $this->fileInfo['size'];
89 }
90
91 /**
92 * Return the path to the temporary file
93 *
94 * @return string|null Path or null if non-existent
95 */
96 public function getTempName() {
97 if ( !$this->exists() ) {
98 return null;
99 }
100
101 return $this->fileInfo['tmp_name'];
102 }
103
104 /**
105 * Return the upload error. See link for explanation
106 * https://www.php.net/manual/en/features.file-upload.errors.php
107 *
108 * @return int One of the UPLOAD_ constants, 0 if non-existent
109 */
110 public function getError() {
111 if ( !$this->exists() ) {
112 return 0; # UPLOAD_ERR_OK
113 }
114
115 return $this->fileInfo['error'];
116 }
117
118 /**
119 * Returns whether this upload failed because of overflow of a maximum set
120 * in php.ini
121 *
122 * @return bool
123 */
124 public function isIniSizeOverflow() {
125 if ( $this->getError() == UPLOAD_ERR_INI_SIZE ) {
126 # PHP indicated that upload_max_filesize is exceeded
127 return true;
128 }
129
130 $contentLength = $this->request->getHeader( 'Content-Length' );
131 $maxPostSize = wfShorthandToInteger(
132 ini_get( 'post_max_size' ) ?: ini_get( 'hhvm.server.max_post_size' ),
133 0
134 );
135
136 if ( $maxPostSize && $contentLength > $maxPostSize ) {
137 # post_max_size is exceeded
138 return true;
139 }
140
141 return false;
142 }
143 }