patoui /
router
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Patoui\Router; |
||
| 6 | |||
| 7 | use InvalidArgumentException; |
||
| 8 | use Psr\Http\Message\StreamInterface; |
||
| 9 | use Psr\Http\Message\UploadedFileInterface; |
||
| 10 | use RuntimeException; |
||
| 11 | |||
| 12 | final class UploadedFile implements UploadedFileInterface |
||
| 13 | { |
||
| 14 | private string $file; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 15 | |||
| 16 | private StreamInterface $stream; |
||
| 17 | |||
| 18 | private bool $hasMoved = false; |
||
| 19 | |||
| 20 | private ?int $size; |
||
| 21 | |||
| 22 | private int $error; |
||
| 23 | |||
| 24 | private ?string $name; |
||
| 25 | |||
| 26 | private ?string $type; |
||
| 27 | |||
| 28 | private bool $isSapi; |
||
| 29 | |||
| 30 | /** @var array<int> */ |
||
| 31 | private static array $validUploadErrorCodes = [ |
||
| 32 | UPLOAD_ERR_OK, |
||
| 33 | UPLOAD_ERR_INI_SIZE, |
||
| 34 | UPLOAD_ERR_FORM_SIZE, |
||
| 35 | UPLOAD_ERR_PARTIAL, |
||
| 36 | UPLOAD_ERR_NO_FILE, |
||
| 37 | UPLOAD_ERR_NO_TMP_DIR, |
||
| 38 | UPLOAD_ERR_CANT_WRITE, |
||
| 39 | UPLOAD_ERR_EXTENSION, |
||
| 40 | ]; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * UploadedFile constructor. |
||
| 44 | * @param string|StreamInterface $file |
||
| 45 | * @param string|null $name |
||
| 46 | * @param string|null $type |
||
| 47 | * @param int|null $size |
||
| 48 | * @param int $error |
||
| 49 | * @psalm-suppress RedundantConditionGivenDocblockType Used for is_string check. |
||
| 50 | */ |
||
| 51 | public function __construct( |
||
| 52 | $file, |
||
| 53 | ?string $name = null, |
||
| 54 | ?string $type = null, |
||
| 55 | ?int $size = null, |
||
| 56 | int $error = UPLOAD_ERR_OK |
||
| 57 | ) { |
||
| 58 | if (! in_array($error, self::$validUploadErrorCodes, true)) { |
||
| 59 | throw new InvalidArgumentException('Invalid upload error code.'); |
||
| 60 | } |
||
| 61 | |||
| 62 | if ($file instanceof StreamInterface) { |
||
| 63 | /** @psalm-suppress MixedAssignment */ |
||
| 64 | $fileUri = $file->getMetadata('uri'); |
||
| 65 | if (! is_string($fileUri)) { |
||
| 66 | throw new InvalidArgumentException('URI not available for given stream'); |
||
| 67 | } |
||
| 68 | $this->file = $fileUri; |
||
| 69 | $this->stream = $file; |
||
| 70 | } elseif (is_string($file)) { |
||
| 71 | $this->file = $file; |
||
| 72 | $this->stream = (new StreamFactory())->createStreamFromFile($file); |
||
| 73 | } else { |
||
| 74 | throw new InvalidArgumentException('Invalid type for file, must be string or implement StreamInterface.'); |
||
| 75 | } |
||
| 76 | $this->size = $size; |
||
| 77 | $this->error = $error; |
||
| 78 | $this->name = $name; |
||
| 79 | $this->type = $type; |
||
| 80 | $this->isSapi = ! empty($_FILES); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @return array<UploadedFile> |
||
| 85 | * @psalm-suppress MixedAssignment |
||
| 86 | * @psalm-suppress MixedArrayAccess |
||
| 87 | * @psalm-suppress MixedArgument |
||
| 88 | */ |
||
| 89 | public static function makeWithGlobals(): array |
||
| 90 | { |
||
| 91 | $uploadedFiles = []; |
||
| 92 | |||
| 93 | foreach ($_FILES as $field => $file) { |
||
| 94 | if (is_string($file['tmp_name']) || (is_object($file['tmp_name']) && $file['tmp_name'] instanceof StreamInterface)) { |
||
| 95 | $uploadedFiles[] = new static( |
||
| 96 | $file['tmp_name'], |
||
| 97 | isset($file['name']) ? (string) $file['name'] : null, |
||
| 98 | isset($file['type']) ? (string) $file['type'] : null, |
||
| 99 | isset($file['size']) ? (int) $file['size'] : null, |
||
| 100 | (int) ($file['error'] ?? UPLOAD_ERR_OK), |
||
| 101 | ); |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | return $uploadedFiles; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * {@inheritdoc} |
||
| 110 | */ |
||
| 111 | public function getStream(): StreamInterface |
||
| 112 | { |
||
| 113 | return $this->stream; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * {@inheritdoc} |
||
| 118 | */ |
||
| 119 | public function moveTo($targetPath): void |
||
| 120 | { |
||
| 121 | if ($this->hasMoved) { |
||
| 122 | throw new RuntimeException('Uploaded file has already been moved'); |
||
| 123 | } |
||
| 124 | |||
| 125 | if ($this->isSapi) { |
||
| 126 | if (! is_uploaded_file($this->file)) { |
||
| 127 | throw new RuntimeException('Invalid uploaded file'); |
||
| 128 | } |
||
| 129 | if (! move_uploaded_file($this->file, $targetPath)) { |
||
| 130 | throw new RuntimeException('Error occurred while moving file'); |
||
| 131 | } |
||
| 132 | } elseif (! rename($this->file, $targetPath)) { |
||
| 133 | throw new RuntimeException('Error occurred while moving file'); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * {@inheritdoc} |
||
| 139 | */ |
||
| 140 | public function getSize(): ?int |
||
| 141 | { |
||
| 142 | return $this->size; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * {@inheritdoc} |
||
| 147 | */ |
||
| 148 | public function getError(): int |
||
| 149 | { |
||
| 150 | return $this->error; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * {@inheritdoc} |
||
| 155 | */ |
||
| 156 | public function getClientFilename(): ?string |
||
| 157 | { |
||
| 158 | return $this->name; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * {@inheritdoc} |
||
| 163 | */ |
||
| 164 | public function getClientMediaType(): ?string |
||
| 165 | { |
||
| 166 | return $this->type; |
||
| 167 | } |
||
| 168 | } |
||
| 169 |