MediaWiki  master
WebRequestUpload.php
Go to the documentation of this file.
1 <?php
24 
32  protected $request;
34  protected $doesExist;
36  protected $fileInfo;
37 
44  public function __construct( $request, $key ) {
45  $this->request = $request;
46  $this->doesExist = isset( $_FILES[$key] );
47  if ( $this->doesExist ) {
48  $this->fileInfo = $_FILES[$key];
49  }
50  }
51 
57  public function exists() {
58  return $this->doesExist;
59  }
60 
66  public function getName() {
67  if ( !$this->exists() ) {
68  return null;
69  }
70 
71  // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
72  $name = $this->fileInfo['name'];
73 
74  # Safari sends filenames in HTML-encoded Unicode form D...
75  # Horrid and evil! Let's try to make some kind of sense of it.
76  $name = Sanitizer::decodeCharReferences( $name );
77  $name = MediaWikiServices::getInstance()->getContentLanguage()->normalize( $name );
78  // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
79  wfDebug( __METHOD__ . ": {$this->fileInfo['name']} normalized to '$name'" );
80  return $name;
81  }
82 
88  public function getSize() {
89  if ( !$this->exists() ) {
90  return 0;
91  }
92 
93  // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
94  return $this->fileInfo['size'];
95  }
96 
102  public function getTempName() {
103  if ( !$this->exists() ) {
104  return null;
105  }
106 
107  // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
108  return $this->fileInfo['tmp_name'];
109  }
110 
117  public function getType() {
118  if ( !$this->exists() ) {
119  return null;
120  }
121 
122  // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
123  return $this->fileInfo['type'];
124  }
125 
132  public function getError() {
133  if ( !$this->exists() ) {
134  return 0; # UPLOAD_ERR_OK
135  }
136 
137  // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
138  return $this->fileInfo['error'];
139  }
140 
147  public function isIniSizeOverflow() {
148  if ( $this->getError() == UPLOAD_ERR_INI_SIZE ) {
149  # PHP indicated that upload_max_filesize is exceeded
150  return true;
151  }
152 
153  $contentLength = $this->request->getHeader( 'Content-Length' );
154  $maxPostSize = wfShorthandToInteger( ini_get( 'post_max_size' ), 0 );
155 
156  if ( $maxPostSize && $contentLength > $maxPostSize ) {
157  # post_max_size is exceeded
158  return true;
159  }
160 
161  return false;
162  }
163 }
WebRequestUpload\getError
getError()
Return the upload error.
Definition: WebRequestUpload.php:132
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:166
WebRequestUpload\__construct
__construct( $request, $key)
Constructor.
Definition: WebRequestUpload.php:44
WebRequestUpload\$fileInfo
array null $fileInfo
Definition: WebRequestUpload.php:36
WebRequestUpload\isIniSizeOverflow
isIniSizeOverflow()
Returns whether this upload failed because of overflow of a maximum set in php.ini.
Definition: WebRequestUpload.php:147
WebRequestUpload\$request
WebRequest $request
Definition: WebRequestUpload.php:32
WebRequestUpload\exists
exists()
Return whether a file with this name was uploaded.
Definition: WebRequestUpload.php:57
WebRequestUpload\getName
getName()
Return the original filename of the uploaded file.
Definition: WebRequestUpload.php:66
WebRequestUpload
Object to access the $_FILES array.
Definition: WebRequestUpload.php:30
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:914
WebRequestUpload\getTempName
getTempName()
Return the path to the temporary file.
Definition: WebRequestUpload.php:102
WebRequestUpload\getType
getType()
Return the client specified content type.
Definition: WebRequestUpload.php:117
WebRequest
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
Definition: WebRequest.php:42
wfShorthandToInteger
wfShorthandToInteger( $string='', $default=-1)
Converts shorthand byte notation to integer form.
Definition: GlobalFunctions.php:2662
WebRequestUpload\getSize
getSize()
Return the file size of the uploaded file.
Definition: WebRequestUpload.php:88
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:1232
WebRequestUpload\$doesExist
bool $doesExist
Definition: WebRequestUpload.php:34