MediaWiki  master
WebRequestUpload.php
Go to the documentation of this file.
1 <?php
24 
25 // The point of this class is to be a wrapper around super globals
26 // phpcs:disable MediaWiki.Usage.SuperGlobalsUsage.SuperGlobals
27 
35  public const REQUIRED_FILEINFO_KEYS = [ 'name', 'size', 'tmp_name', 'type', 'error', ];
37  protected $request;
39  protected $doesExist;
41  protected $fileInfo;
42 
49  public function __construct( $request, $key ) {
50  $this->request = $request;
51  $this->doesExist = isset( $_FILES[$key] );
52  if ( $this->doesExist ) {
53  $this->fileInfo = $_FILES[$key];
54  }
55  }
56 
62  public function exists() {
63  return $this->doesExist;
64  }
65 
71  public function getName() {
72  if ( !$this->exists() ) {
73  return null;
74  }
75 
76  // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
77  $name = $this->fileInfo['name'];
78 
79  # Safari sends filenames in HTML-encoded Unicode form D...
80  # Horrid and evil! Let's try to make some kind of sense of it.
81  $name = Sanitizer::decodeCharReferences( $name );
82  $name = MediaWikiServices::getInstance()->getContentLanguage()->normalize( $name );
83  // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
84  wfDebug( __METHOD__ . ": {$this->fileInfo['name']} normalized to '$name'" );
85  return $name;
86  }
87 
93  public function getSize() {
94  if ( !$this->exists() ) {
95  return 0;
96  }
97 
98  // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
99  return $this->fileInfo['size'];
100  }
101 
107  public function getTempName() {
108  if ( !$this->exists() ) {
109  return null;
110  }
111 
112  // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
113  return $this->fileInfo['tmp_name'];
114  }
115 
122  public function getType() {
123  if ( !$this->exists() ) {
124  return null;
125  }
126 
127  // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
128  return $this->fileInfo['type'];
129  }
130 
137  public function getError() {
138  if ( !$this->exists() ) {
139  return 0; # UPLOAD_ERR_OK
140  }
141 
142  // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
143  return $this->fileInfo['error'];
144  }
145 
152  public function isIniSizeOverflow() {
153  if ( $this->getError() == UPLOAD_ERR_INI_SIZE ) {
154  # PHP indicated that upload_max_filesize is exceeded
155  return true;
156  }
157 
158  $contentLength = $this->request->getHeader( 'Content-Length' );
159  $maxPostSize = wfShorthandToInteger( ini_get( 'post_max_size' ), 0 );
160 
161  if ( $maxPostSize && $contentLength > $maxPostSize ) {
162  # post_max_size is exceeded
163  return true;
164  }
165 
166  return false;
167  }
168 }
WebRequestUpload\getError
getError()
Return the upload error.
Definition: WebRequestUpload.php:137
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:200
WebRequestUpload\__construct
__construct( $request, $key)
Constructor.
Definition: WebRequestUpload.php:49
WebRequestUpload\$fileInfo
array null $fileInfo
Definition: WebRequestUpload.php:41
WebRequestUpload\isIniSizeOverflow
isIniSizeOverflow()
Returns whether this upload failed because of overflow of a maximum set in php.ini.
Definition: WebRequestUpload.php:152
WebRequestUpload\$request
WebRequest $request
Definition: WebRequestUpload.php:37
WebRequestUpload\exists
exists()
Return whether a file with this name was uploaded.
Definition: WebRequestUpload.php:62
WebRequestUpload\getName
getName()
Return the original filename of the uploaded file.
Definition: WebRequestUpload.php:71
WebRequestUpload
Object to access the $_FILES array.
Definition: WebRequestUpload.php:33
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:886
WebRequestUpload\getTempName
getTempName()
Return the path to the temporary file.
Definition: WebRequestUpload.php:107
WebRequestUpload\getType
getType()
Return the client specified content type.
Definition: WebRequestUpload.php:122
WebRequest
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
Definition: WebRequest.php:43
wfShorthandToInteger
wfShorthandToInteger( $string='', $default=-1)
Converts shorthand byte notation to integer form.
Definition: GlobalFunctions.php:2412
WebRequestUpload\getSize
getSize()
Return the file size of the uploaded file.
Definition: WebRequestUpload.php:93
Sanitizer\decodeCharReferences
static decodeCharReferences( $text)
Decode any character references, numeric or named entities, in the text and return a UTF-8 string.
Definition: Sanitizer.php:1229
WebRequestUpload\$doesExist
bool $doesExist
Definition: WebRequestUpload.php:39
WebRequestUpload\REQUIRED_FILEINFO_KEYS
const REQUIRED_FILEINFO_KEYS
All keys a fileinfo has to specific to work with this class.
Definition: WebRequestUpload.php:35