UploadedFile.php
260 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 | namespace Matomo\Dependencies\Symfony\Component\HttpFoundation\File; |
| 12 | |
| 13 | use Matomo\Dependencies\Symfony\Component\HttpFoundation\File\Exception\CannotWriteFileException; |
| 14 | use Matomo\Dependencies\Symfony\Component\HttpFoundation\File\Exception\ExtensionFileException; |
| 15 | use Matomo\Dependencies\Symfony\Component\HttpFoundation\File\Exception\FileException; |
| 16 | use Matomo\Dependencies\Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException; |
| 17 | use Matomo\Dependencies\Symfony\Component\HttpFoundation\File\Exception\FormSizeFileException; |
| 18 | use Matomo\Dependencies\Symfony\Component\HttpFoundation\File\Exception\IniSizeFileException; |
| 19 | use Matomo\Dependencies\Symfony\Component\HttpFoundation\File\Exception\NoFileException; |
| 20 | use Matomo\Dependencies\Symfony\Component\HttpFoundation\File\Exception\NoTmpDirFileException; |
| 21 | use Matomo\Dependencies\Symfony\Component\HttpFoundation\File\Exception\PartialFileException; |
| 22 | use Symfony\Component\Mime\MimeTypes; |
| 23 | /** |
| 24 | * A file uploaded through a form. |
| 25 | * |
| 26 | * @author Bernhard Schussek <bschussek@gmail.com> |
| 27 | * @author Florian Eckerstorfer <florian@eckerstorfer.org> |
| 28 | * @author Fabien Potencier <fabien@symfony.com> |
| 29 | */ |
| 30 | class UploadedFile extends File |
| 31 | { |
| 32 | private $test; |
| 33 | private $originalName; |
| 34 | private $mimeType; |
| 35 | private $error; |
| 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 $error The error constant of the upload (one of PHP's UPLOAD_ERR_XXX constants); null defaults to UPLOAD_ERR_OK |
| 54 | * @param bool $test Whether the test mode is active |
| 55 | * Local files are used in test mode hence the code should not enforce HTTP uploads |
| 56 | * |
| 57 | * @throws FileException If file_uploads is disabled |
| 58 | * @throws FileNotFoundException If the file does not exist |
| 59 | */ |
| 60 | public function __construct(string $path, string $originalName, ?string $mimeType = null, ?int $error = null, bool $test = \false) |
| 61 | { |
| 62 | $this->originalName = $this->getName($originalName); |
| 63 | $this->mimeType = $mimeType ?: 'application/octet-stream'; |
| 64 | $this->error = $error ?: \UPLOAD_ERR_OK; |
| 65 | $this->test = $test; |
| 66 | parent::__construct($path, \UPLOAD_ERR_OK === $this->error); |
| 67 | } |
| 68 | /** |
| 69 | * Returns the original file name. |
| 70 | * |
| 71 | * It is extracted from the request from which the file has been uploaded. |
| 72 | * This should not be considered as a safe value to use for a file name on your servers. |
| 73 | * |
| 74 | * @return string |
| 75 | */ |
| 76 | public function getClientOriginalName() |
| 77 | { |
| 78 | return $this->originalName; |
| 79 | } |
| 80 | /** |
| 81 | * Returns the original file extension. |
| 82 | * |
| 83 | * It is extracted from the original file name that was uploaded. |
| 84 | * This should not be considered as a safe value to use for a file name on your servers. |
| 85 | * |
| 86 | * @return string |
| 87 | */ |
| 88 | public function getClientOriginalExtension() |
| 89 | { |
| 90 | return pathinfo($this->originalName, \PATHINFO_EXTENSION); |
| 91 | } |
| 92 | /** |
| 93 | * Returns the file mime type. |
| 94 | * |
| 95 | * The client mime type is extracted from the request from which the file |
| 96 | * was uploaded, so it should not be considered as a safe value. |
| 97 | * |
| 98 | * For a trusted mime type, use getMimeType() instead (which guesses the mime |
| 99 | * type based on the file content). |
| 100 | * |
| 101 | * @return string |
| 102 | * |
| 103 | * @see getMimeType() |
| 104 | */ |
| 105 | public function getClientMimeType() |
| 106 | { |
| 107 | return $this->mimeType; |
| 108 | } |
| 109 | /** |
| 110 | * Returns the extension based on the client mime type. |
| 111 | * |
| 112 | * If the mime type is unknown, returns null. |
| 113 | * |
| 114 | * This method uses the mime type as guessed by getClientMimeType() |
| 115 | * to guess the file extension. As such, the extension returned |
| 116 | * by this method cannot be trusted. |
| 117 | * |
| 118 | * For a trusted extension, use guessExtension() instead (which guesses |
| 119 | * the extension based on the guessed mime type for the file). |
| 120 | * |
| 121 | * @return string|null |
| 122 | * |
| 123 | * @see guessExtension() |
| 124 | * @see getClientMimeType() |
| 125 | */ |
| 126 | public function guessClientExtension() |
| 127 | { |
| 128 | if (!class_exists(MimeTypes::class)) { |
| 129 | throw new \LogicException('You cannot guess the extension as the Mime component is not installed. Try running "composer require symfony/mime".'); |
| 130 | } |
| 131 | return MimeTypes::getDefault()->getExtensions($this->getClientMimeType())[0] ?? null; |
| 132 | } |
| 133 | /** |
| 134 | * Returns the upload error. |
| 135 | * |
| 136 | * If the upload was successful, the constant UPLOAD_ERR_OK is returned. |
| 137 | * Otherwise one of the other UPLOAD_ERR_XXX constants is returned. |
| 138 | * |
| 139 | * @return int |
| 140 | */ |
| 141 | public function getError() |
| 142 | { |
| 143 | return $this->error; |
| 144 | } |
| 145 | /** |
| 146 | * Returns whether the file has been uploaded with HTTP and no error occurred. |
| 147 | * |
| 148 | * @return bool |
| 149 | */ |
| 150 | public function isValid() |
| 151 | { |
| 152 | $isOk = \UPLOAD_ERR_OK === $this->error; |
| 153 | return $this->test ? $isOk : $isOk && is_uploaded_file($this->getPathname()); |
| 154 | } |
| 155 | /** |
| 156 | * Moves the file to a new location. |
| 157 | * |
| 158 | * @return File |
| 159 | * |
| 160 | * @throws FileException if, for any reason, the file could not have been moved |
| 161 | */ |
| 162 | public function move(string $directory, ?string $name = null) |
| 163 | { |
| 164 | if ($this->isValid()) { |
| 165 | if ($this->test) { |
| 166 | return parent::move($directory, $name); |
| 167 | } |
| 168 | $target = $this->getTargetFile($directory, $name); |
| 169 | set_error_handler(function ($type, $msg) use(&$error) { |
| 170 | $error = $msg; |
| 171 | }); |
| 172 | try { |
| 173 | $moved = move_uploaded_file($this->getPathname(), $target); |
| 174 | } finally { |
| 175 | restore_error_handler(); |
| 176 | } |
| 177 | if (!$moved) { |
| 178 | throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s).', $this->getPathname(), $target, strip_tags($error))); |
| 179 | } |
| 180 | @chmod($target, 0666 & ~umask()); |
| 181 | return $target; |
| 182 | } |
| 183 | switch ($this->error) { |
| 184 | case \UPLOAD_ERR_INI_SIZE: |
| 185 | throw new IniSizeFileException($this->getErrorMessage()); |
| 186 | case \UPLOAD_ERR_FORM_SIZE: |
| 187 | throw new FormSizeFileException($this->getErrorMessage()); |
| 188 | case \UPLOAD_ERR_PARTIAL: |
| 189 | throw new PartialFileException($this->getErrorMessage()); |
| 190 | case \UPLOAD_ERR_NO_FILE: |
| 191 | throw new NoFileException($this->getErrorMessage()); |
| 192 | case \UPLOAD_ERR_CANT_WRITE: |
| 193 | throw new CannotWriteFileException($this->getErrorMessage()); |
| 194 | case \UPLOAD_ERR_NO_TMP_DIR: |
| 195 | throw new NoTmpDirFileException($this->getErrorMessage()); |
| 196 | case \UPLOAD_ERR_EXTENSION: |
| 197 | throw new ExtensionFileException($this->getErrorMessage()); |
| 198 | } |
| 199 | throw new FileException($this->getErrorMessage()); |
| 200 | } |
| 201 | /** |
| 202 | * Returns the maximum size of an uploaded file as configured in php.ini. |
| 203 | * |
| 204 | * @return int|float The maximum size of an uploaded file in bytes (returns float if size > PHP_INT_MAX) |
| 205 | */ |
| 206 | public static function getMaxFilesize() |
| 207 | { |
| 208 | $sizePostMax = self::parseFilesize(\ini_get('post_max_size')); |
| 209 | $sizeUploadMax = self::parseFilesize(\ini_get('upload_max_filesize')); |
| 210 | return min($sizePostMax ?: \PHP_INT_MAX, $sizeUploadMax ?: \PHP_INT_MAX); |
| 211 | } |
| 212 | /** |
| 213 | * Returns the given size from an ini value in bytes. |
| 214 | * |
| 215 | * @return int|float Returns float if size > PHP_INT_MAX |
| 216 | */ |
| 217 | private static function parseFilesize(string $size) |
| 218 | { |
| 219 | if ('' === $size) { |
| 220 | return 0; |
| 221 | } |
| 222 | $size = strtolower($size); |
| 223 | $max = ltrim($size, '+'); |
| 224 | if (str_starts_with($max, '0x')) { |
| 225 | $max = \intval($max, 16); |
| 226 | } elseif (str_starts_with($max, '0')) { |
| 227 | $max = \intval($max, 8); |
| 228 | } else { |
| 229 | $max = (int) $max; |
| 230 | } |
| 231 | switch (substr($size, -1)) { |
| 232 | case 't': |
| 233 | $max *= 1024; |
| 234 | // no break |
| 235 | case 'g': |
| 236 | $max *= 1024; |
| 237 | // no break |
| 238 | case 'm': |
| 239 | $max *= 1024; |
| 240 | // no break |
| 241 | case 'k': |
| 242 | $max *= 1024; |
| 243 | } |
| 244 | return $max; |
| 245 | } |
| 246 | /** |
| 247 | * Returns an informative upload error message. |
| 248 | * |
| 249 | * @return string |
| 250 | */ |
| 251 | public function getErrorMessage() |
| 252 | { |
| 253 | static $errors = [\UPLOAD_ERR_INI_SIZE => 'The file "%s" exceeds your upload_max_filesize ini directive (limit is %d KiB).', \UPLOAD_ERR_FORM_SIZE => 'The file "%s" exceeds the upload limit defined in your form.', \UPLOAD_ERR_PARTIAL => 'The file "%s" was only partially uploaded.', \UPLOAD_ERR_NO_FILE => 'No file was uploaded.', \UPLOAD_ERR_CANT_WRITE => 'The file "%s" could not be written on disk.', \UPLOAD_ERR_NO_TMP_DIR => 'File could not be uploaded: missing temporary directory.', \UPLOAD_ERR_EXTENSION => 'File upload was stopped by a PHP extension.']; |
| 254 | $errorCode = $this->error; |
| 255 | $maxFilesize = \UPLOAD_ERR_INI_SIZE === $errorCode ? self::getMaxFilesize() / 1024 : 0; |
| 256 | $message = $errors[$errorCode] ?? 'The file "%s" was not uploaded due to an unknown error.'; |
| 257 | return sprintf($message, $this->getClientOriginalName(), $maxFilesize); |
| 258 | } |
| 259 | } |
| 260 |