Special case opus mime detction
[mediawiki.git] / includes / WebRequestUpload.php
blob916a10c9f0777e71f9b34f583da1a833bae944e2
1 <?php
2 /**
3 * Object to access the $_FILES array
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 /**
24 * Object to access the $_FILES array
26 * @ingroup HTTP
28 class WebRequestUpload {
29 protected $request;
30 protected $doesExist;
31 protected $fileInfo;
33 /**
34 * Constructor. Should only be called by WebRequest
36 * @param WebRequest $request The associated request
37 * @param string $key Key in $_FILES array (name of form field)
39 public function __construct( $request, $key ) {
40 $this->request = $request;
41 $this->doesExist = isset( $_FILES[$key] );
42 if ( $this->doesExist ) {
43 $this->fileInfo = $_FILES[$key];
47 /**
48 * Return whether a file with this name was uploaded.
50 * @return bool
52 public function exists() {
53 return $this->doesExist;
56 /**
57 * Return the original filename of the uploaded file
59 * @return string|null Filename or null if non-existent
61 public function getName() {
62 if ( !$this->exists() ) {
63 return null;
66 global $wgContLang;
67 $name = $this->fileInfo['name'];
69 # Safari sends filenames in HTML-encoded Unicode form D...
70 # Horrid and evil! Let's try to make some kind of sense of it.
71 $name = Sanitizer::decodeCharReferences( $name );
72 $name = $wgContLang->normalize( $name );
73 wfDebug( __METHOD__ . ": {$this->fileInfo['name']} normalized to '$name'\n" );
74 return $name;
77 /**
78 * Return the file size of the uploaded file
80 * @return int File size or zero if non-existent
82 public function getSize() {
83 if ( !$this->exists() ) {
84 return 0;
87 return $this->fileInfo['size'];
90 /**
91 * Return the path to the temporary file
93 * @return string|null Path or null if non-existent
95 public function getTempName() {
96 if ( !$this->exists() ) {
97 return null;
100 return $this->fileInfo['tmp_name'];
104 * Return the upload error. See link for explanation
105 * https://secure.php.net/manual/en/features.file-upload.errors.php
107 * @return int One of the UPLOAD_ constants, 0 if non-existent
109 public function getError() {
110 if ( !$this->exists() ) {
111 return 0; # UPLOAD_ERR_OK
114 return $this->fileInfo['error'];
118 * Returns whether this upload failed because of overflow of a maximum set
119 * in php.ini
121 * @return bool
123 public function isIniSizeOverflow() {
124 if ( $this->getError() == UPLOAD_ERR_INI_SIZE ) {
125 # PHP indicated that upload_max_filesize is exceeded
126 return true;
129 $contentLength = $this->request->getHeader( 'Content-Length' );
130 $maxPostSize = wfShorthandToInteger(
131 ini_get( 'post_max_size' ) ?: ini_get( 'hhvm.server.max_post_size' ),
135 if ( $maxPostSize && $contentLength > $maxPostSize ) {
136 # post_max_size is exceeded
137 return true;
140 return false;