Merge "mediawiki.router: Move hashchange handler to a real method"
[mediawiki.git] / includes / Request / WebRequestUpload.php
blobbb800e0e62794bb25a247a4fc829260d995eb408
1 <?php
2 /**
3 * Object to access the $_FILES array
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.
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.
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
20 * @file
23 namespace MediaWiki\Request;
25 use MediaWiki\MediaWikiServices;
26 use MediaWiki\Parser\Sanitizer;
28 // The point of this class is to be a wrapper around super globals
29 // phpcs:disable MediaWiki.Usage.SuperGlobalsUsage.SuperGlobals
31 /**
32 * Object to access the $_FILES array
34 * @ingroup HTTP
36 class WebRequestUpload {
37 /** All keys a fileinfo has to specific to work with this class */
38 public const REQUIRED_FILEINFO_KEYS = [ 'name', 'size', 'tmp_name', 'type', 'error', ];
39 /** @var WebRequest */
40 protected $request;
41 /** @var bool */
42 protected $doesExist;
43 /** @var array|null */
44 protected $fileInfo;
46 /**
47 * Constructor. Should only be called by WebRequest
49 * @param WebRequest $request The associated request
50 * @param string $key Key in $_FILES array (name of form field)
52 public function __construct( $request, $key ) {
53 $this->request = $request;
54 $this->doesExist = isset( $_FILES[$key] );
55 if ( $this->doesExist ) {
56 $this->fileInfo = $_FILES[$key];
60 /**
61 * Return whether a file with this name was uploaded.
63 * @return bool
65 public function exists() {
66 return $this->doesExist;
69 /**
70 * Return the original filename of the uploaded file
72 * @return string|null Filename or null if non-existent
74 public function getName() {
75 if ( !$this->exists() ) {
76 return null;
79 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
80 $name = $this->fileInfo['name'];
82 # Safari sends filenames in HTML-encoded Unicode form D...
83 # Horrid and evil! Let's try to make some kind of sense of it.
84 $name = Sanitizer::decodeCharReferences( $name );
85 $name = MediaWikiServices::getInstance()->getContentLanguage()->normalize( $name );
86 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
87 wfDebug( __METHOD__ . ": {$this->fileInfo['name']} normalized to '$name'" );
88 return $name;
91 /**
92 * Return the file size of the uploaded file
94 * @return int File size or zero if non-existent
96 public function getSize() {
97 if ( !$this->exists() ) {
98 return 0;
101 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
102 return $this->fileInfo['size'];
106 * Return the path to the temporary file
108 * @return string|null Path or null if non-existent
110 public function getTempName() {
111 if ( !$this->exists() ) {
112 return null;
115 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
116 return $this->fileInfo['tmp_name'];
120 * Return the client specified content type
122 * @return string|null Type or null if non-existent
123 * @since 1.35
125 public function getType() {
126 if ( !$this->exists() ) {
127 return null;
130 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
131 return $this->fileInfo['type'];
135 * Return the upload error. See link for explanation
136 * https://www.php.net/manual/en/features.file-upload.errors.php
138 * @return int One of the UPLOAD_ constants, 0 if non-existent
140 public function getError() {
141 if ( !$this->exists() ) {
142 return 0; # UPLOAD_ERR_OK
145 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
146 return $this->fileInfo['error'];
150 * Returns whether this upload failed because of overflow of a maximum set
151 * in php.ini
153 * @return bool
155 public function isIniSizeOverflow() {
156 if ( $this->getError() == UPLOAD_ERR_INI_SIZE ) {
157 # PHP indicated that upload_max_filesize is exceeded
158 return true;
161 $contentLength = $this->request->getHeader( 'Content-Length' );
162 $maxPostSize = wfShorthandToInteger( ini_get( 'post_max_size' ), 0 );
164 if ( $maxPostSize && $contentLength > $maxPostSize ) {
165 # post_max_size is exceeded
166 return true;
169 return false;
173 /** @deprecated class alias since 1.40 */
174 class_alias( WebRequestUpload::class, 'WebRequestUpload' );