MediaWiki master
WebRequestUpload.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Request;
24
27
28// The point of this class is to be a wrapper around super globals
29// phpcs:disable MediaWiki.Usage.SuperGlobalsUsage.SuperGlobals
30
38 public const REQUIRED_FILEINFO_KEYS = [ 'name', 'size', 'tmp_name', 'type', 'error', ];
40 protected $request;
42 protected $doesExist;
44 protected $fileInfo;
45
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];
57 }
58 }
59
65 public function exists() {
66 return $this->doesExist;
67 }
68
74 public function getName() {
75 if ( !$this->exists() ) {
76 return null;
77 }
78
79 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
80 $name = $this->fileInfo['name'];
81
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;
89 }
90
96 public function getSize() {
97 if ( !$this->exists() ) {
98 return 0;
99 }
100
101 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
102 return $this->fileInfo['size'];
103 }
104
110 public function getTempName() {
111 if ( !$this->exists() ) {
112 return null;
113 }
114
115 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
116 return $this->fileInfo['tmp_name'];
117 }
118
125 public function getType() {
126 if ( !$this->exists() ) {
127 return null;
128 }
129
130 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
131 return $this->fileInfo['type'];
132 }
133
140 public function getError() {
141 if ( !$this->exists() ) {
142 return 0; # UPLOAD_ERR_OK
143 }
144
145 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
146 return $this->fileInfo['error'];
147 }
148
155 public function isIniSizeOverflow() {
156 if ( $this->getError() == UPLOAD_ERR_INI_SIZE ) {
157 # PHP indicated that upload_max_filesize is exceeded
158 return true;
159 }
160
161 $contentLength = $this->request->getHeader( 'Content-Length' );
162 $maxPostSize = wfShorthandToInteger( ini_get( 'post_max_size' ), 0 );
163
164 if ( $maxPostSize && $contentLength > $maxPostSize ) {
165 # post_max_size is exceeded
166 return true;
167 }
168
169 return false;
170 }
171}
172
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.
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:46
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.
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form,...