PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.4
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.4
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / vendor_prefixed / guzzlehttp / psr7 / src / UploadedFile.php

UploadedFile.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.4, at vendor_prefixed/guzzlehttp/psr7/src/UploadedFile.php

256 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace YoastSEO_Vendor\GuzzleHttp\Psr7;
4
5 use InvalidArgumentException;
6 use YoastSEO_Vendor\Psr\Http\Message\StreamInterface;
7 use YoastSEO_Vendor\Psr\Http\Message\UploadedFileInterface;
8 use RuntimeException;
9 class UploadedFile implements \YoastSEO_Vendor\Psr\Http\Message\UploadedFileInterface
10 {
11 /**
12 * @var int[]
13 */
14 private static $errors = [\UPLOAD_ERR_OK, \UPLOAD_ERR_INI_SIZE, \UPLOAD_ERR_FORM_SIZE, \UPLOAD_ERR_PARTIAL, \UPLOAD_ERR_NO_FILE, \UPLOAD_ERR_NO_TMP_DIR, \UPLOAD_ERR_CANT_WRITE, \UPLOAD_ERR_EXTENSION];
15 /**
16 * @var string
17 */
18 private $clientFilename;
19 /**
20 * @var string
21 */
22 private $clientMediaType;
23 /**
24 * @var int
25 */
26 private $error;
27 /**
28 * @var string|null
29 */
30 private $file;
31 /**
32 * @var bool
33 */
34 private $moved = \false;
35 /**
36 * @var int
37 */
38 private $size;
39 /**
40 * @var StreamInterface|null
41 */
42 private $stream;
43 /**
44 * @param StreamInterface|string|resource $streamOrFile
45 * @param int $size
46 * @param int $errorStatus
47 * @param string|null $clientFilename
48 * @param string|null $clientMediaType
49 */
50 public function __construct($streamOrFile, $size, $errorStatus, $clientFilename = null, $clientMediaType = null)
51 {
52 $this->setError($errorStatus);
53 $this->setSize($size);
54 $this->setClientFilename($clientFilename);
55 $this->setClientMediaType($clientMediaType);
56 if ($this->isOk()) {
57 $this->setStreamOrFile($streamOrFile);
58 }
59 }
60 /**
61 * Depending on the value set file or stream variable
62 *
63 * @param mixed $streamOrFile
64 *
65 * @throws InvalidArgumentException
66 */
67 private function setStreamOrFile($streamOrFile)
68 {
69 if (\is_string($streamOrFile)) {
70 $this->file = $streamOrFile;
71 } elseif (\is_resource($streamOrFile)) {
72 $this->stream = new \YoastSEO_Vendor\GuzzleHttp\Psr7\Stream($streamOrFile);
73 } elseif ($streamOrFile instanceof \YoastSEO_Vendor\Psr\Http\Message\StreamInterface) {
74 $this->stream = $streamOrFile;
75 } else {
76 throw new \InvalidArgumentException('Invalid stream or file provided for UploadedFile');
77 }
78 }
79 /**
80 * @param int $error
81 *
82 * @throws InvalidArgumentException
83 */
84 private function setError($error)
85 {
86 if (\false === \is_int($error)) {
87 throw new \InvalidArgumentException('Upload file error status must be an integer');
88 }
89 if (\false === \in_array($error, \YoastSEO_Vendor\GuzzleHttp\Psr7\UploadedFile::$errors)) {
90 throw new \InvalidArgumentException('Invalid error status for UploadedFile');
91 }
92 $this->error = $error;
93 }
94 /**
95 * @param int $size
96 *
97 * @throws InvalidArgumentException
98 */
99 private function setSize($size)
100 {
101 if (\false === \is_int($size)) {
102 throw new \InvalidArgumentException('Upload file size must be an integer');
103 }
104 $this->size = $size;
105 }
106 /**
107 * @param mixed $param
108 *
109 * @return bool
110 */
111 private function isStringOrNull($param)
112 {
113 return \in_array(\gettype($param), ['string', 'NULL']);
114 }
115 /**
116 * @param mixed $param
117 *
118 * @return bool
119 */
120 private function isStringNotEmpty($param)
121 {
122 return \is_string($param) && \false === empty($param);
123 }
124 /**
125 * @param string|null $clientFilename
126 *
127 * @throws InvalidArgumentException
128 */
129 private function setClientFilename($clientFilename)
130 {
131 if (\false === $this->isStringOrNull($clientFilename)) {
132 throw new \InvalidArgumentException('Upload file client filename must be a string or null');
133 }
134 $this->clientFilename = $clientFilename;
135 }
136 /**
137 * @param string|null $clientMediaType
138 *
139 * @throws InvalidArgumentException
140 */
141 private function setClientMediaType($clientMediaType)
142 {
143 if (\false === $this->isStringOrNull($clientMediaType)) {
144 throw new \InvalidArgumentException('Upload file client media type must be a string or null');
145 }
146 $this->clientMediaType = $clientMediaType;
147 }
148 /**
149 * Return true if there is no upload error
150 *
151 * @return bool
152 */
153 private function isOk()
154 {
155 return $this->error === \UPLOAD_ERR_OK;
156 }
157 /**
158 * @return bool
159 */
160 public function isMoved()
161 {
162 return $this->moved;
163 }
164 /**
165 * @throws RuntimeException if is moved or not ok
166 */
167 private function validateActive()
168 {
169 if (\false === $this->isOk()) {
170 throw new \RuntimeException('Cannot retrieve stream due to upload error');
171 }
172 if ($this->isMoved()) {
173 throw new \RuntimeException('Cannot retrieve stream after it has already been moved');
174 }
175 }
176 /**
177 * {@inheritdoc}
178 *
179 * @throws RuntimeException if the upload was not successful.
180 */
181 public function getStream()
182 {
183 $this->validateActive();
184 if ($this->stream instanceof \YoastSEO_Vendor\Psr\Http\Message\StreamInterface) {
185 return $this->stream;
186 }
187 return new \YoastSEO_Vendor\GuzzleHttp\Psr7\LazyOpenStream($this->file, 'r+');
188 }
189 /**
190 * {@inheritdoc}
191 *
192 * @see http://php.net/is_uploaded_file
193 * @see http://php.net/move_uploaded_file
194 *
195 * @param string $targetPath Path to which to move the uploaded file.
196 *
197 * @throws RuntimeException if the upload was not successful.
198 * @throws InvalidArgumentException if the $path specified is invalid.
199 * @throws RuntimeException on any error during the move operation, or on
200 * the second or subsequent call to the method.
201 */
202 public function moveTo($targetPath)
203 {
204 $this->validateActive();
205 if (\false === $this->isStringNotEmpty($targetPath)) {
206 throw new \InvalidArgumentException('Invalid path provided for move operation; must be a non-empty string');
207 }
208 if ($this->file) {
209 $this->moved = \php_sapi_name() == 'cli' ? \rename($this->file, $targetPath) : \move_uploaded_file($this->file, $targetPath);
210 } else {
211 \YoastSEO_Vendor\GuzzleHttp\Psr7\Utils::copyToStream($this->getStream(), new \YoastSEO_Vendor\GuzzleHttp\Psr7\LazyOpenStream($targetPath, 'w'));
212 $this->moved = \true;
213 }
214 if (\false === $this->moved) {
215 throw new \RuntimeException(\sprintf('Uploaded file could not be moved to %s', $targetPath));
216 }
217 }
218 /**
219 * {@inheritdoc}
220 *
221 * @return int|null The file size in bytes or null if unknown.
222 */
223 public function getSize()
224 {
225 return $this->size;
226 }
227 /**
228 * {@inheritdoc}
229 *
230 * @see http://php.net/manual/en/features.file-upload.errors.php
231 *
232 * @return int One of PHP's UPLOAD_ERR_XXX constants.
233 */
234 public function getError()
235 {
236 return $this->error;
237 }
238 /**
239 * {@inheritdoc}
240 *
241 * @return string|null The filename sent by the client or null if none
242 * was provided.
243 */
244 public function getClientFilename()
245 {
246 return $this->clientFilename;
247 }
248 /**
249 * {@inheritdoc}
250 */
251 public function getClientMediaType()
252 {
253 return $this->clientMediaType;
254 }
255 }
256