MediaWiki  master
WebRequestUpload.php
Go to the documentation of this file.
1 <?php
24 
31  protected $request;
32  protected $doesExist;
33  protected $fileInfo;
34 
41  public function __construct( $request, $key ) {
42  $this->request = $request;
43  $this->doesExist = isset( $_FILES[$key] );
44  if ( $this->doesExist ) {
45  $this->fileInfo = $_FILES[$key];
46  }
47  }
48 
54  public function exists() {
55  return $this->doesExist;
56  }
57 
63  public function getName() {
64  if ( !$this->exists() ) {
65  return null;
66  }
67 
68  $name = $this->fileInfo['name'];
69 
70  # Safari sends filenames in HTML-encoded Unicode form D...
71  # Horrid and evil! Let's try to make some kind of sense of it.
72  $name = Sanitizer::decodeCharReferences( $name );
73  $name = MediaWikiServices::getInstance()->getContentLanguage()->normalize( $name );
74  wfDebug( __METHOD__ . ": {$this->fileInfo['name']} normalized to '$name'" );
75  return $name;
76  }
77 
83  public function getSize() {
84  if ( !$this->exists() ) {
85  return 0;
86  }
87 
88  return $this->fileInfo['size'];
89  }
90 
96  public function getTempName() {
97  if ( !$this->exists() ) {
98  return null;
99  }
100 
101  return $this->fileInfo['tmp_name'];
102  }
103 
110  public function getType() {
111  if ( !$this->exists() ) {
112  return null;
113  }
114 
115  return $this->fileInfo['type'];
116  }
117 
124  public function getError() {
125  if ( !$this->exists() ) {
126  return 0; # UPLOAD_ERR_OK
127  }
128 
129  return $this->fileInfo['error'];
130  }
131 
138  public function isIniSizeOverflow() {
139  if ( $this->getError() == UPLOAD_ERR_INI_SIZE ) {
140  # PHP indicated that upload_max_filesize is exceeded
141  return true;
142  }
143 
144  $contentLength = $this->request->getHeader( 'Content-Length' );
145  $maxPostSize = wfShorthandToInteger( ini_get( 'post_max_size' ), 0 );
146 
147  if ( $maxPostSize && $contentLength > $maxPostSize ) {
148  # post_max_size is exceeded
149  return true;
150  }
151 
152  return false;
153  }
154 }
WebRequestUpload\getError
getError()
Return the upload error.
Definition: WebRequestUpload.php:124
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:154
WebRequestUpload\$doesExist
$doesExist
Definition: WebRequestUpload.php:32
WebRequestUpload\__construct
__construct( $request, $key)
Constructor.
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:138
WebRequestUpload\exists
exists()
Return whether a file with this name was uploaded.
Definition: WebRequestUpload.php:54
WebRequestUpload\$fileInfo
$fileInfo
Definition: WebRequestUpload.php:33
WebRequestUpload\getName
getName()
Return the original filename of the uploaded file.
Definition: WebRequestUpload.php:63
WebRequestUpload\$request
$request
Definition: WebRequestUpload.php:31
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:909
WebRequestUpload\getTempName
getTempName()
Return the path to the temporary file.
Definition: WebRequestUpload.php:96
WebRequestUpload\getType
getType()
Return the client specified content type.
Definition: WebRequestUpload.php:110
wfShorthandToInteger
wfShorthandToInteger( $string='', $default=-1)
Converts shorthand byte notation to integer form.
Definition: GlobalFunctions.php:2663
WebRequestUpload\getSize
getSize()
Return the file size of the uploaded file.
Definition: WebRequestUpload.php:83
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:1467