MediaWiki REL1_39
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}
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.
Object to access the $_FILES array.
getType()
Return the client specified content type.
getError()
Return the upload error.
isIniSizeOverflow()
Returns whether this upload failed because of overflow of a maximum set in php.ini.
const REQUIRED_FILEINFO_KEYS
All keys a fileinfo has to specific to work with this class.
exists()
Return whether a file with this name was uploaded.
__construct( $request, $key)
Constructor.
getSize()
Return the file size of the uploaded file.
getTempName()
Return the path to the temporary file.
getName()
Return the original filename of the uploaded file.
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...