PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 3.3.1
GiveWP – Donation Plugin and Fundraising Platform v3.3.1
4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / vendor / vendor-prefixed / symfony / http-foundation / File / UploadedFile.php
give / vendor / vendor-prefixed / symfony / http-foundation / File Last commit date
Exception 2 years ago MimeType 2 years ago File.php 2 years ago Stream.php 2 years ago UploadedFile.php 2 years ago
UploadedFile.php
285 lines
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 *
11 * Modified by impress-org on 23-January-2024 using Strauss.
12 * @see https://github.com/BrianHenryIE/strauss
13 */
14
15 namespace Give\Vendors\Symfony\Component\HttpFoundation\File;
16
17 use Give\Vendors\Symfony\Component\HttpFoundation\File\Exception\FileException;
18 use Give\Vendors\Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
19 use Give\Vendors\Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;
20
21 /**
22 * A file uploaded through a form.
23 *
24 * @author Bernhard Schussek <bschussek@gmail.com>
25 * @author Florian Eckerstorfer <florian@eckerstorfer.org>
26 * @author Fabien Potencier <fabien@symfony.com>
27 */
28 class UploadedFile extends File
29 {
30 private $test;
31 private $originalName;
32 private $mimeType;
33 private $size;
34 private $error;
35
36 /**
37 * Accepts the information of the uploaded file as provided by the PHP global $_FILES.
38 *
39 * The file object is only created when the uploaded file is valid (i.e. when the
40 * isValid() method returns true). Otherwise the only methods that could be called
41 * on an UploadedFile instance are:
42 *
43 * * getClientOriginalName,
44 * * getClientMimeType,
45 * * isValid,
46 * * getError.
47 *
48 * Calling any other method on an non-valid instance will cause an unpredictable result.
49 *
50 * @param string $path The full temporary path to the file
51 * @param string $originalName The original file name of the uploaded file
52 * @param string|null $mimeType The type of the file as provided by PHP; null defaults to application/octet-stream
53 * @param int|null $size The file size provided by the uploader
54 * @param int|null $error The error constant of the upload (one of PHP's UPLOAD_ERR_XXX constants); null defaults to UPLOAD_ERR_OK
55 * @param bool $test Whether the test mode is active
56 * Local files are used in test mode hence the code should not enforce HTTP uploads
57 *
58 * @throws FileException If file_uploads is disabled
59 * @throws FileNotFoundException If the file does not exist
60 */
61 public function __construct($path, $originalName, $mimeType = null, $size = null, $error = null, $test = false)
62 {
63 $this->originalName = $this->getName($originalName);
64 $this->mimeType = $mimeType ?: 'application/octet-stream';
65 $this->size = $size;
66 $this->error = $error ?: \UPLOAD_ERR_OK;
67 $this->test = (bool) $test;
68
69 parent::__construct($path, \UPLOAD_ERR_OK === $this->error);
70 }
71
72 /**
73 * Returns the original file name.
74 *
75 * It is extracted from the request from which the file has been uploaded.
76 * Then it should not be considered as a safe value.
77 *
78 * @return string The original name
79 */
80 public function getClientOriginalName()
81 {
82 return $this->originalName;
83 }
84
85 /**
86 * Returns the original file extension.
87 *
88 * It is extracted from the original file name that was uploaded.
89 * Then it should not be considered as a safe value.
90 *
91 * @return string The extension
92 */
93 public function getClientOriginalExtension()
94 {
95 return pathinfo($this->originalName, \PATHINFO_EXTENSION);
96 }
97
98 /**
99 * Returns the file mime type.
100 *
101 * The client mime type is extracted from the request from which the file
102 * was uploaded, so it should not be considered as a safe value.
103 *
104 * For a trusted mime type, use getMimeType() instead (which guesses the mime
105 * type based on the file content).
106 *
107 * @return string The mime type
108 *
109 * @see getMimeType()
110 */
111 public function getClientMimeType()
112 {
113 return $this->mimeType;
114 }
115
116 /**
117 * Returns the extension based on the client mime type.
118 *
119 * If the mime type is unknown, returns null.
120 *
121 * This method uses the mime type as guessed by getClientMimeType()
122 * to guess the file extension. As such, the extension returned
123 * by this method cannot be trusted.
124 *
125 * For a trusted extension, use guessExtension() instead (which guesses
126 * the extension based on the guessed mime type for the file).
127 *
128 * @return string|null The guessed extension or null if it cannot be guessed
129 *
130 * @see guessExtension()
131 * @see getClientMimeType()
132 */
133 public function guessClientExtension()
134 {
135 $type = $this->getClientMimeType();
136 $guesser = ExtensionGuesser::getInstance();
137
138 return $guesser->guess($type);
139 }
140
141 /**
142 * Returns the file size.
143 *
144 * It is extracted from the request from which the file has been uploaded.
145 * Then it should not be considered as a safe value.
146 *
147 * @return int|null The file size
148 */
149 public function getClientSize()
150 {
151 return $this->size;
152 }
153
154 /**
155 * Returns the upload error.
156 *
157 * If the upload was successful, the constant UPLOAD_ERR_OK is returned.
158 * Otherwise one of the other UPLOAD_ERR_XXX constants is returned.
159 *
160 * @return int The upload error
161 */
162 public function getError()
163 {
164 return $this->error;
165 }
166
167 /**
168 * Returns whether the file was uploaded successfully.
169 *
170 * @return bool True if the file has been uploaded with HTTP and no error occurred
171 */
172 public function isValid()
173 {
174 $isOk = \UPLOAD_ERR_OK === $this->error;
175
176 return $this->test ? $isOk : $isOk && is_uploaded_file($this->getPathname());
177 }
178
179 /**
180 * Moves the file to a new location.
181 *
182 * @param string $directory The destination folder
183 * @param string $name The new file name
184 *
185 * @return File A File object representing the new file
186 *
187 * @throws FileException if, for any reason, the file could not have been moved
188 */
189 public function move($directory, $name = null)
190 {
191 if ($this->isValid()) {
192 if ($this->test) {
193 return parent::move($directory, $name);
194 }
195
196 $target = $this->getTargetFile($directory, $name);
197
198 set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; });
199 $moved = move_uploaded_file($this->getPathname(), $target);
200 restore_error_handler();
201 if (!$moved) {
202 throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s).', $this->getPathname(), $target, strip_tags($error)));
203 }
204
205 @chmod($target, 0666 & ~umask());
206
207 return $target;
208 }
209
210 throw new FileException($this->getErrorMessage());
211 }
212
213 /**
214 * Returns the maximum size of an uploaded file as configured in php.ini.
215 *
216 * @return int The maximum size of an uploaded file in bytes
217 */
218 public static function getMaxFilesize()
219 {
220 $sizePostMax = self::parseFilesize(ini_get('post_max_size'));
221 $sizeUploadMax = self::parseFilesize(ini_get('upload_max_filesize'));
222
223 return min($sizePostMax ?: \PHP_INT_MAX, $sizeUploadMax ?: \PHP_INT_MAX);
224 }
225
226 /**
227 * Returns the given size from an ini value in bytes.
228 *
229 * @return int The given size in bytes
230 */
231 private static function parseFilesize($size)
232 {
233 if ('' === $size) {
234 return 0;
235 }
236
237 $size = strtolower($size);
238
239 $max = ltrim($size, '+');
240 if (0 === strpos($max, '0x')) {
241 $max = \intval($max, 16);
242 } elseif (0 === strpos($max, '0')) {
243 $max = \intval($max, 8);
244 } else {
245 $max = (int) $max;
246 }
247
248 switch (substr($size, -1)) {
249 case 't': $max *= 1024;
250 // no break
251 case 'g': $max *= 1024;
252 // no break
253 case 'm': $max *= 1024;
254 // no break
255 case 'k': $max *= 1024;
256 }
257
258 return $max;
259 }
260
261 /**
262 * Returns an informative upload error message.
263 *
264 * @return string The error message regarding the specified error code
265 */
266 public function getErrorMessage()
267 {
268 static $errors = [
269 \UPLOAD_ERR_INI_SIZE => 'The file "%s" exceeds your upload_max_filesize ini directive (limit is %d KiB).',
270 \UPLOAD_ERR_FORM_SIZE => 'The file "%s" exceeds the upload limit defined in your form.',
271 \UPLOAD_ERR_PARTIAL => 'The file "%s" was only partially uploaded.',
272 \UPLOAD_ERR_NO_FILE => 'No file was uploaded.',
273 \UPLOAD_ERR_CANT_WRITE => 'The file "%s" could not be written on disk.',
274 \UPLOAD_ERR_NO_TMP_DIR => 'File could not be uploaded: missing temporary directory.',
275 \UPLOAD_ERR_EXTENSION => 'File upload was stopped by a PHP extension.',
276 ];
277
278 $errorCode = $this->error;
279 $maxFilesize = \UPLOAD_ERR_INI_SIZE === $errorCode ? self::getMaxFilesize() / 1024 : 0;
280 $message = isset($errors[$errorCode]) ? $errors[$errorCode] : 'The file "%s" was not uploaded due to an unknown error.';
281
282 return sprintf($message, $this->getClientOriginalName(), $maxFilesize);
283 }
284 }
285