MediaWiki master
WebRequestUpload.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Request;
10
13
14// The point of this class is to be a wrapper around super globals
15// phpcs:disable MediaWiki.Usage.SuperGlobalsUsage.SuperGlobals
16
24 public const REQUIRED_FILEINFO_KEYS = [ 'name', 'size', 'tmp_name', 'type', 'error', ];
26 protected $request;
28 protected $doesExist;
30 protected $fileInfo;
31
38 public function __construct( $request, $key ) {
39 $this->request = $request;
40 $this->doesExist = isset( $_FILES[$key] );
41 if ( $this->doesExist ) {
42 $this->fileInfo = $_FILES[$key];
43 }
44 }
45
51 public function exists() {
52 return $this->doesExist;
53 }
54
60 public function getName() {
61 if ( !$this->exists() ) {
62 return null;
63 }
64
65 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
66 $name = $this->fileInfo['name'];
67
68 # Safari sends filenames in HTML-encoded Unicode form D...
69 # Horrid and evil! Let's try to make some kind of sense of it.
70 $name = Sanitizer::decodeCharReferences( $name );
71 $name = MediaWikiServices::getInstance()->getContentLanguage()->normalize( $name );
72 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
73 wfDebug( __METHOD__ . ": {$this->fileInfo['name']} normalized to '$name'" );
74 return $name;
75 }
76
82 public function getSize() {
83 if ( !$this->exists() ) {
84 return 0;
85 }
86
87 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
88 return $this->fileInfo['size'];
89 }
90
96 public function getTempName() {
97 if ( !$this->exists() ) {
98 return null;
99 }
100
101 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
102 return $this->fileInfo['tmp_name'];
103 }
104
111 public function getType() {
112 if ( !$this->exists() ) {
113 return null;
114 }
115
116 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
117 return $this->fileInfo['type'];
118 }
119
126 public function getError() {
127 if ( !$this->exists() ) {
128 return 0; # UPLOAD_ERR_OK
129 }
130
131 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Okay after exists check
132 return $this->fileInfo['error'];
133 }
134
141 public function isIniSizeOverflow() {
142 if ( $this->getError() == UPLOAD_ERR_INI_SIZE ) {
143 # PHP indicated that upload_max_filesize is exceeded
144 return true;
145 }
146
147 $contentLength = $this->request->getHeader( 'Content-Length' );
148 $maxPostSize = wfShorthandToInteger( ini_get( 'post_max_size' ), 0 );
149
150 if ( $maxPostSize && $contentLength > $maxPostSize ) {
151 # post_max_size is exceeded
152 return true;
153 }
154
155 return false;
156 }
157}
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:32
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,...