Apply PSR-12 code style (#3886)
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / upload / UploadFile.class.php
1 <?php
2
3 namespace wcf\system\upload;
4
5 /**
6 * Represents a file upload.
7 *
8 * @author Marcel Werk
9 * @copyright 2001-2019 WoltLab GmbH
10 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
11 * @package WoltLabSuite\Core\System\Upload
12 */
13 class UploadFile
14 {
15 /**
16 * original file name
17 * @var string
18 */
19 protected $filename = '';
20
21 /**
22 * internal file id
23 * @var int
24 */
25 protected $internalFileID = 0;
26
27 /**
28 * location of the uploaded file
29 * @var string
30 */
31 protected $location = '';
32
33 /**
34 * file size
35 * @var int
36 */
37 protected $filesize = 0;
38
39 /**
40 * file upload error code
41 * @var int
42 */
43 protected $errorCode = 0;
44
45 /**
46 * MIME type
47 * @var string
48 */
49 protected $mimeType = '';
50
51 /**
52 * validation error type
53 * @var string
54 */
55 protected $validationErrorType = '';
56
57 /**
58 * additional data for validation errors
59 * @var array
60 */
61 protected $validationErrorAdditionalData = [];
62
63 /**
64 * Creates a new UploadFile object.
65 *
66 * @param string $filename
67 * @param string $location
68 * @param int $filesize
69 * @param int $errorCode
70 * @param string $mimeType
71 *
72 * @throws \Exception if an error occurred during upload and debug mode is enabled
73 */
74 public function __construct($filename, $location, $filesize, $errorCode = 0, $mimeType = '')
75 {
76 if (\preg_match('~^__wcf_([0-9]+)_(.*)~', $filename, $matches)) {
77 $this->internalFileID = $matches[1];
78 $filename = $matches[2];
79 }
80
81 $this->filename = $filename;
82 $this->location = $location;
83 $this->filesize = $filesize;
84 $this->errorCode = $errorCode;
85 $this->mimeType = $mimeType;
86
87 if (ENABLE_DEBUG_MODE) {
88 switch ($errorCode) {
89 case \UPLOAD_ERR_INI_SIZE:
90 throw new \Exception("The uploaded file is larger than PHP's `upload_max_filesize`.");
91 case \UPLOAD_ERR_FORM_SIZE:
92 throw new \Exception("The uploaded file is larger than the form's `MAX_FILE_SIZE`.");
93 case \UPLOAD_ERR_PARTIAL:
94 throw new \Exception("The uploaded file was only partially uploaded.");
95 case \UPLOAD_ERR_NO_FILE:
96 throw new \Exception("No file was uploaded.");
97 case \UPLOAD_ERR_NO_TMP_DIR:
98 throw new \Exception("There is no temporary folder where PHP can save the file.");
99 case \UPLOAD_ERR_CANT_WRITE:
100 throw new \Exception("The uploaded file could not be written to disk.");
101 case \UPLOAD_ERR_EXTENSION:
102 throw new \Exception("A PHP extension stopped the file upload.");
103 }
104 }
105 }
106
107 /**
108 * Returns the original file name.
109 *
110 * @return string
111 */
112 public function getFilename()
113 {
114 return $this->filename;
115 }
116
117 /**
118 * Returns internal file id.
119 *
120 * @return int
121 */
122 public function getInternalFileID()
123 {
124 return $this->internalFileID;
125 }
126
127 /**
128 * Returns the extension of the original file name.
129 *
130 * @return string
131 */
132 public function getFileExtension()
133 {
134 if (($position = \mb_strrpos($this->getFilename(), '.')) !== false) {
135 return \mb_strtolower(\mb_substr($this->getFilename(), $position + 1));
136 }
137
138 return '';
139 }
140
141 /**
142 * Returns the file location.
143 *
144 * @return string
145 */
146 public function getLocation()
147 {
148 return $this->location;
149 }
150
151 /**
152 * Returns the file size.
153 *
154 * @return int
155 */
156 public function getFilesize()
157 {
158 return $this->filesize;
159 }
160
161 /**
162 * Returns the MIME type.
163 *
164 * @return string
165 */
166 public function getMimeType()
167 {
168 return $this->mimeType;
169 }
170
171 /**
172 * Returns the error code.
173 *
174 * @return int
175 */
176 public function getErrorCode()
177 {
178 return $this->errorCode;
179 }
180
181 /**
182 * Sets the validation error type.
183 *
184 * @param string $validationErrorType
185 * @param array $additionalData
186 */
187 public function setValidationErrorType($validationErrorType, array $additionalData = [])
188 {
189 $this->validationErrorType = $validationErrorType;
190 $this->validationErrorAdditionalData = $additionalData;
191 }
192
193 /**
194 * Returns the validation error type.
195 *
196 * @return string
197 */
198 public function getValidationErrorType()
199 {
200 return $this->validationErrorType;
201 }
202
203 /**
204 * Returns the validation error additional data array.
205 *
206 * @return array
207 */
208 public function getValidationErrorAdditionalData()
209 {
210 return $this->validationErrorAdditionalData;
211 }
212
213 /**
214 * Returns the image data of the file or `null` if the file is no image.
215 *
216 * @return array|null
217 */
218 public function getImageData()
219 {
220 if (($imageData = @\getimagesize($this->getLocation())) !== false) {
221 return [
222 'width' => $imageData[0],
223 'height' => $imageData[1],
224 'mimeType' => $imageData['mime'],
225 ];
226 }
227 }
228
229 /**
230 * Moves the uploaded file to the given location and updates the internal location value to the new location
231 * and the internal filename value to the new filename derived from the given location.
232 *
233 * @param string $newLocation new file location
234 */
235 public function moveUploadedFile($newLocation)
236 {
237 \move_uploaded_file($this->location, $newLocation);
238
239 $this->location = $newLocation;
240 $this->filename = \basename($this->location);
241 }
242 }