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 | namespace Thruster\Component\HttpMessage; |
||
4 | |||
5 | use Psr\Http\Message\MessageInterface; |
||
6 | use Psr\Http\Message\RequestInterface; |
||
7 | use Psr\Http\Message\ResponseInterface; |
||
8 | use Psr\Http\Message\StreamInterface; |
||
9 | use Psr\Http\Message\UriInterface; |
||
10 | |||
11 | /** |
||
12 | * Returns a UriInterface for the given value. |
||
13 | * |
||
14 | * This function accepts a string or {@see Psr\Http\Message\UriInterface} and |
||
15 | * returns a UriInterface for the given value. If the value is already a |
||
16 | * `UriInterface`, it is returned as-is. |
||
17 | * |
||
18 | * @param string|UriInterface $uri |
||
19 | * |
||
20 | * @return UriInterface |
||
21 | * @throws \InvalidArgumentException |
||
22 | */ |
||
23 | function uri_for($uri) |
||
24 | { |
||
25 | if ($uri instanceof UriInterface) { |
||
26 | return $uri; |
||
27 | } elseif (is_string($uri)) { |
||
28 | return new Uri($uri); |
||
29 | } |
||
30 | |||
31 | throw new \InvalidArgumentException('URI must be a string or UriInterface'); |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * Create a new stream based on the input type. |
||
36 | * |
||
37 | * Options is an associative array that can contain the following keys: |
||
38 | * - metadata: Array of custom metadata. |
||
39 | * - size: Size of the stream. |
||
40 | * |
||
41 | * @param resource|string|StreamInterface $resource Entity body data |
||
42 | * @param array $options Additional options |
||
43 | * |
||
44 | * @return Stream |
||
45 | * @throws \InvalidArgumentException if the $resource arg is not valid. |
||
46 | */ |
||
47 | function stream_for($resource = '', array $options = []) |
||
48 | { |
||
49 | 83 | switch (gettype($resource)) { |
|
50 | case 'string': |
||
51 | 56 | $stream = fopen('php://temp', 'r+'); |
|
52 | 56 | if ($resource !== '') { |
|
53 | 54 | fwrite($stream, $resource); |
|
54 | 54 | fseek($stream, 0); |
|
55 | } |
||
56 | 56 | return new Stream($stream, $options); |
|
57 | case 'resource': |
||
58 | 36 | return new Stream($resource, $options); |
|
59 | case 'object': |
||
60 | 8 | if ($resource instanceof StreamInterface) { |
|
61 | 4 | return $resource; |
|
62 | } elseif ($resource instanceof \Iterator) { |
||
63 | return new PumpStream(function () use ($resource) { |
||
64 | if (!$resource->valid()) { |
||
65 | return false; |
||
66 | } |
||
67 | $result = $resource->current(); |
||
68 | $resource->next(); |
||
69 | return $result; |
||
70 | }, $options); |
||
71 | 4 | } elseif (method_exists($resource, '__toString')) { |
|
72 | return stream_for((string) $resource, $options); |
||
73 | } |
||
74 | 4 | break; |
|
75 | case 'NULL': |
||
76 | return new Stream(fopen('php://temp', 'r+'), $options); |
||
77 | } |
||
78 | |||
79 | 4 | if (is_callable($resource)) { |
|
80 | 4 | return new PumpStream($resource, $options); |
|
81 | } |
||
82 | |||
83 | throw new \InvalidArgumentException('Invalid resource type: ' . gettype($resource)); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Copy the contents of a stream into a string until the given number of |
||
88 | * bytes have been read. |
||
89 | * |
||
90 | * @param StreamInterface $stream Stream to read |
||
91 | * @param int $maxLen Maximum number of bytes to read. Pass -1 |
||
92 | * to read the entire stream. |
||
93 | * @return string |
||
94 | * @throws \RuntimeException on error. |
||
95 | */ |
||
96 | function copy_to_string(StreamInterface $stream, int $maxLen = -1) : string |
||
97 | { |
||
98 | 29 | $buffer = ''; |
|
99 | |||
100 | 29 | if (-1 === $maxLen) { |
|
101 | 29 | while (false === $stream->eof()) { |
|
102 | 25 | $buf = $stream->read(1048576); |
|
103 | |||
104 | // Using a loose equality here to match on '' and false. |
||
105 | 23 | if ($buf == null) { |
|
106 | 8 | break; |
|
107 | } |
||
108 | |||
109 | 22 | $buffer .= $buf; |
|
110 | } |
||
111 | 27 | return $buffer; |
|
112 | } |
||
113 | |||
114 | $len = 0; |
||
115 | View Code Duplication | while (false === $stream->eof() && $len < $maxLen) { |
|
0 ignored issues
–
show
|
|||
116 | $buf = $stream->read($maxLen - $len); |
||
117 | // Using a loose equality here to match on '' and false. |
||
118 | if (null === $buf) { |
||
119 | break; |
||
120 | } |
||
121 | |||
122 | $buffer .= $buf; |
||
123 | $len = strlen($buffer); |
||
124 | } |
||
125 | |||
126 | return $buffer; |
||
127 | } |
||
128 | |||
129 | |||
130 | |||
131 | /** |
||
132 | * Safely opens a PHP stream resource using a filename. |
||
133 | * |
||
134 | * When fopen fails, PHP normally raises a warning. This function adds an |
||
135 | * error handler that checks for errors and throws an exception instead. |
||
136 | * |
||
137 | * @param string $filename File to open |
||
138 | * @param string $mode Mode used to open the file |
||
139 | * |
||
140 | * @return resource |
||
141 | * @throws \RuntimeException if the file cannot be opened |
||
142 | */ |
||
143 | function try_fopen($filename, $mode) |
||
144 | { |
||
145 | 7 | $ex = null; |
|
146 | set_error_handler(function () use ($filename, $mode, &$ex) { |
||
147 | $ex = new \RuntimeException(sprintf( |
||
148 | 'Unable to open %s using mode %s: %s', |
||
149 | $filename, |
||
150 | $mode, |
||
151 | func_get_args()[1] |
||
152 | )); |
||
153 | 7 | }); |
|
154 | |||
155 | 7 | $handle = fopen($filename, $mode); |
|
156 | 7 | restore_error_handler(); |
|
157 | |||
158 | 7 | if ($ex) { |
|
159 | /** @var $ex \RuntimeException */ |
||
160 | throw $ex; |
||
161 | } |
||
162 | |||
163 | 7 | return $handle; |
|
164 | } |
||
165 | |||
166 | /** |
||
167 | * Copy the contents of a stream into another stream until the given number |
||
168 | * of bytes have been read. |
||
169 | * |
||
170 | * @param StreamInterface $source Stream to read from |
||
171 | * @param StreamInterface $dest Stream to write to |
||
172 | * @param int $maxLen Maximum number of bytes to read. Pass -1 |
||
173 | * to read the entire stream. |
||
174 | * |
||
175 | * @throws \RuntimeException on error. |
||
176 | */ |
||
177 | function copy_to_stream(StreamInterface $source, StreamInterface $dest, $maxLen = -1) { |
||
178 | 4 | if (-1 === $maxLen) { |
|
179 | 4 | while (!$source->eof()) { |
|
180 | 4 | if (!$dest->write($source->read(1048576))) { |
|
181 | 4 | break; |
|
182 | } |
||
183 | } |
||
184 | 4 | return; |
|
185 | } |
||
186 | |||
187 | $bytes = 0; |
||
188 | View Code Duplication | while (false === $source->eof()) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
189 | $buf = $source->read($maxLen - $bytes); |
||
190 | if (false === ($len = strlen($buf))) { |
||
191 | break; |
||
192 | } |
||
193 | |||
194 | $bytes += $len; |
||
195 | $dest->write($buf); |
||
196 | |||
197 | if ($bytes == $maxLen) { |
||
198 | break; |
||
199 | } |
||
200 | } |
||
201 | } |
||
202 | |||
203 | |||
204 | /** |
||
205 | * Read a line from the stream up to the maximum allowed buffer length |
||
206 | * |
||
207 | * @param StreamInterface $stream Stream to read from |
||
208 | * @param int $maxLength Maximum buffer length |
||
209 | * |
||
210 | * @return string|bool |
||
211 | */ |
||
212 | function readline(StreamInterface $stream, $maxLength = null) |
||
213 | { |
||
214 | 1 | $buffer = ''; |
|
215 | 1 | $size = 0; |
|
216 | |||
217 | 1 | while (false === $stream->eof()) { |
|
218 | // Using a loose equality here to match on '' and false. |
||
219 | 1 | if (null === ($byte = $stream->read(1))) { |
|
220 | return $buffer; |
||
221 | } |
||
222 | |||
223 | 1 | $buffer .= $byte; |
|
224 | // Break when a new line is found or the max length - 1 is reached |
||
225 | 1 | if ($byte === PHP_EOL || ++$size === $maxLength - 1) { |
|
226 | 1 | break; |
|
227 | } |
||
228 | } |
||
229 | |||
230 | 1 | return $buffer; |
|
231 | } |
||
232 | |||
233 | /** |
||
234 | * Determines the mimetype of a file by looking at its extension. |
||
235 | * |
||
236 | * @param $filename |
||
237 | * |
||
238 | * @return null|string |
||
239 | */ |
||
240 | function mimetype_from_filename(string $filename) |
||
241 | { |
||
242 | 3 | return mimetype_from_extension(pathinfo($filename, PATHINFO_EXTENSION)); |
|
243 | } |
||
244 | |||
245 | /** |
||
246 | * Maps a file extensions to a mimetype. |
||
247 | * |
||
248 | * @param $extension string The file extension. |
||
249 | * |
||
250 | * @return string|null |
||
251 | * @link http://svn.apache.org/repos/asf/httpd/httpd/branches/1.3.x/conf/mime.types |
||
252 | */ |
||
253 | function mimetype_from_extension(string $extension) |
||
254 | { |
||
255 | 3 | static $mimetypes = [ |
|
256 | '7z' => 'application/x-7z-compressed', |
||
257 | 'aac' => 'audio/x-aac', |
||
258 | 'ai' => 'application/postscript', |
||
259 | 'aif' => 'audio/x-aiff', |
||
260 | 'asc' => 'text/plain', |
||
261 | 'asf' => 'video/x-ms-asf', |
||
262 | 'atom' => 'application/atom+xml', |
||
263 | 'avi' => 'video/x-msvideo', |
||
264 | 'bmp' => 'image/bmp', |
||
265 | 'bz2' => 'application/x-bzip2', |
||
266 | 'cer' => 'application/pkix-cert', |
||
267 | 'crl' => 'application/pkix-crl', |
||
268 | 'crt' => 'application/x-x509-ca-cert', |
||
269 | 'css' => 'text/css', |
||
270 | 'csv' => 'text/csv', |
||
271 | 'cu' => 'application/cu-seeme', |
||
272 | 'deb' => 'application/x-debian-package', |
||
273 | 'doc' => 'application/msword', |
||
274 | 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
||
275 | 'dvi' => 'application/x-dvi', |
||
276 | 'eot' => 'application/vnd.ms-fontobject', |
||
277 | 'eps' => 'application/postscript', |
||
278 | 'epub' => 'application/epub+zip', |
||
279 | 'etx' => 'text/x-setext', |
||
280 | 'flac' => 'audio/flac', |
||
281 | 'flv' => 'video/x-flv', |
||
282 | 'gif' => 'image/gif', |
||
283 | 'gz' => 'application/gzip', |
||
284 | 'htm' => 'text/html', |
||
285 | 'html' => 'text/html', |
||
286 | 'ico' => 'image/x-icon', |
||
287 | 'ics' => 'text/calendar', |
||
288 | 'ini' => 'text/plain', |
||
289 | 'iso' => 'application/x-iso9660-image', |
||
290 | 'jar' => 'application/java-archive', |
||
291 | 'jpe' => 'image/jpeg', |
||
292 | 'jpeg' => 'image/jpeg', |
||
293 | 'jpg' => 'image/jpeg', |
||
294 | 'js' => 'text/javascript', |
||
295 | 'json' => 'application/json', |
||
296 | 'latex' => 'application/x-latex', |
||
297 | 'log' => 'text/plain', |
||
298 | 'm4a' => 'audio/mp4', |
||
299 | 'm4v' => 'video/mp4', |
||
300 | 'mid' => 'audio/midi', |
||
301 | 'midi' => 'audio/midi', |
||
302 | 'mov' => 'video/quicktime', |
||
303 | 'mp3' => 'audio/mpeg', |
||
304 | 'mp4' => 'video/mp4', |
||
305 | 'mp4a' => 'audio/mp4', |
||
306 | 'mp4v' => 'video/mp4', |
||
307 | 'mpe' => 'video/mpeg', |
||
308 | 'mpeg' => 'video/mpeg', |
||
309 | 'mpg' => 'video/mpeg', |
||
310 | 'mpg4' => 'video/mp4', |
||
311 | 'oga' => 'audio/ogg', |
||
312 | 'ogg' => 'audio/ogg', |
||
313 | 'ogv' => 'video/ogg', |
||
314 | 'ogx' => 'application/ogg', |
||
315 | 'pbm' => 'image/x-portable-bitmap', |
||
316 | 'pdf' => 'application/pdf', |
||
317 | 'pgm' => 'image/x-portable-graymap', |
||
318 | 'png' => 'image/png', |
||
319 | 'pnm' => 'image/x-portable-anymap', |
||
320 | 'ppm' => 'image/x-portable-pixmap', |
||
321 | 'ppt' => 'application/vnd.ms-powerpoint', |
||
322 | 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', |
||
323 | 'ps' => 'application/postscript', |
||
324 | 'qt' => 'video/quicktime', |
||
325 | 'rar' => 'application/x-rar-compressed', |
||
326 | 'ras' => 'image/x-cmu-raster', |
||
327 | 'rss' => 'application/rss+xml', |
||
328 | 'rtf' => 'application/rtf', |
||
329 | 'sgm' => 'text/sgml', |
||
330 | 'sgml' => 'text/sgml', |
||
331 | 'svg' => 'image/svg+xml', |
||
332 | 'swf' => 'application/x-shockwave-flash', |
||
333 | 'tar' => 'application/x-tar', |
||
334 | 'tif' => 'image/tiff', |
||
335 | 'tiff' => 'image/tiff', |
||
336 | 'torrent' => 'application/x-bittorrent', |
||
337 | 'ttf' => 'application/x-font-ttf', |
||
338 | 'txt' => 'text/plain', |
||
339 | 'wav' => 'audio/x-wav', |
||
340 | 'webm' => 'video/webm', |
||
341 | 'wma' => 'audio/x-ms-wma', |
||
342 | 'wmv' => 'video/x-ms-wmv', |
||
343 | 'woff' => 'application/x-font-woff', |
||
344 | 'wsdl' => 'application/wsdl+xml', |
||
345 | 'xbm' => 'image/x-xbitmap', |
||
346 | 'xls' => 'application/vnd.ms-excel', |
||
347 | 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', |
||
348 | 'xml' => 'application/xml', |
||
349 | 'xpm' => 'image/x-xpixmap', |
||
350 | 'xwd' => 'image/x-xwindowdump', |
||
351 | 'yaml' => 'text/yaml', |
||
352 | 'yml' => 'text/yaml', |
||
353 | 'zip' => 'application/zip', |
||
354 | ]; |
||
355 | |||
356 | 3 | $extension = strtolower($extension); |
|
357 | |||
358 | 3 | return $mimetypes[$extension] ?? null; |
|
359 | } |
||
360 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.