MediaWiki REL1_40
WebRequestUpload.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Request;
24
26use Sanitizer;
27use WebRequest;
28
29// The point of this class is to be a wrapper around super globals
30// phpcs:disable MediaWiki.Usage.SuperGlobalsUsage.SuperGlobals
31
39 public const REQUIRED_FILEINFO_KEYS = [ 'name', 'size', 'tmp_name', 'type', 'error', ];
41 protected $request;
43 protected $doesExist;
45 protected $fileInfo;
46
53 public function __construct( $request, $key ) {
54 $this->request = $request;
55 $this->doesExist = isset( $_FILES[$key] );
56 if ( $this->doesExist ) {
57 $this->fileInfo = $_FILES[$key];
58 }
59 }
60
66 public function exists() {
67 return $this->doesExist;
68 }
69
75 public function getName() {
76 if ( !$this->exists() ) {
77 return null;
78 }
79
80 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
81 $name = $this->fileInfo['name'];
82
83 # Safari sends filenames in HTML-encoded Unicode form D...
84 # Horrid and evil! Let's try to make some kind of sense of it.
85 $name = Sanitizer::decodeCharReferences( $name );
86 $name = MediaWikiServices::getInstance()->getContentLanguage()->normalize( $name );
87 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
88 wfDebug( __METHOD__ . ": {$this->fileInfo['name']} normalized to '$name'" );
89 return $name;
90 }
91
97 public function getSize() {
98 if ( !$this->exists() ) {
99 return 0;
100 }
101
102 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
103 return $this->fileInfo['size'];
104 }
105
111 public function getTempName() {
112 if ( !$this->exists() ) {
113 return null;
114 }
115
116 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
117 return $this->fileInfo['tmp_name'];
118 }
119
126 public function getType() {
127 if ( !$this->exists() ) {
128 return null;
129 }
130
131 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
132 return $this->fileInfo['type'];
133 }
134
141 public function getError() {
142 if ( !$this->exists() ) {
143 return 0; # UPLOAD_ERR_OK
144 }
145
146 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
147 return $this->fileInfo['error'];
148 }
149
156 public function isIniSizeOverflow() {
157 if ( $this->getError() == UPLOAD_ERR_INI_SIZE ) {
158 # PHP indicated that upload_max_filesize is exceeded
159 return true;
160 }
161
162 $contentLength = $this->request->getHeader( 'Content-Length' );
163 $maxPostSize = wfShorthandToInteger( ini_get( 'post_max_size' ), 0 );
164
165 if ( $maxPostSize && $contentLength > $maxPostSize ) {
166 # post_max_size is exceeded
167 return true;
168 }
169
170 return false;
171 }
172}
173
174class_alias( WebRequestUpload::class, 'WebRequestUpload' );
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfShorthandToInteger(?string $string='', int $default=-1)
Converts shorthand byte notation to integer form.
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Object to access the $_FILES array.
isIniSizeOverflow()
Returns whether this upload failed because of overflow of a maximum set in php.ini.
getType()
Return the client specified content type.
getTempName()
Return the path to the temporary file.
getError()
Return the upload error.
getName()
Return the original filename of the uploaded file.
exists()
Return whether a file with this name was uploaded.
getSize()
Return the file size of the uploaded file.
const REQUIRED_FILEINFO_KEYS
All keys a fileinfo has to specific to work with this class.
__construct( $request, $key)
Constructor.
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:41
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...