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 Twistor\Flysystem; |
||
4 | |||
5 | use GuzzleHttp\Client; |
||
6 | use GuzzleHttp\ClientInterface; |
||
7 | use GuzzleHttp\Exception\BadResponseException; |
||
8 | use GuzzleHttp\Exception\ClientException; |
||
9 | use GuzzleHttp\Psr7\Uri; |
||
10 | use League\Flysystem\AdapterInterface; |
||
11 | use League\Flysystem\Config; |
||
12 | use League\Flysystem\Util\MimeType; |
||
13 | use Psr\Http\Message\ResponseInterface; |
||
14 | |||
15 | /** |
||
16 | * Uses Guzzle as a backend for HTTP URLs. |
||
17 | */ |
||
18 | class GuzzleAdapter implements AdapterInterface |
||
19 | { |
||
20 | /** |
||
21 | * Whether this endpoint supports head requests. |
||
22 | * |
||
23 | * @var bool |
||
24 | */ |
||
25 | protected $supportsHead = true; |
||
26 | |||
27 | /** |
||
28 | * The base URL. |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $base; |
||
33 | |||
34 | /** |
||
35 | * The Guzzle HTTP client. |
||
36 | * |
||
37 | * @var \GuzzleHttp\ClientInterface |
||
38 | */ |
||
39 | protected $client; |
||
40 | |||
41 | /** |
||
42 | * The visibility of this adapter. |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $visibility = AdapterInterface::VISIBILITY_PUBLIC; |
||
47 | |||
48 | /** |
||
49 | * Constructs a GuzzleAdapter object. |
||
50 | * |
||
51 | * @param string $base The base URL. |
||
52 | * @param \GuzzleHttp\ClientInterface $client An optional Guzzle client. |
||
53 | * @param bool $supportsHead Whether the client supports HEAD requests. |
||
54 | */ |
||
55 | 51 | public function __construct($base, ClientInterface $client = null, $supportsHead = true) |
|
56 | { |
||
57 | 51 | $this->base = rtrim($base, '/') . '/'; |
|
58 | 51 | $this->client = $client ?: new Client(); |
|
59 | 51 | $this->supportsHead = $supportsHead; |
|
60 | |||
61 | 51 | if (isset(parse_url($base)['user'])) { |
|
62 | 6 | $this->visibility = AdapterInterface::VISIBILITY_PRIVATE; |
|
63 | 2 | }; |
|
64 | 51 | } |
|
65 | |||
66 | /** |
||
67 | * Returns the base URL. |
||
68 | * |
||
69 | * @return string The base URL. |
||
70 | */ |
||
71 | 3 | public function getBaseUrl() |
|
72 | { |
||
73 | 3 | return $this->base; |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * @inheritdoc |
||
78 | */ |
||
79 | 3 | public function copy($path, $newpath) |
|
80 | { |
||
81 | 3 | return false; |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * @inheritdoc |
||
86 | */ |
||
87 | 3 | public function createDir($path, Config $config) |
|
88 | { |
||
89 | 3 | return false; |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * @inheritdoc |
||
94 | */ |
||
95 | 3 | public function delete($path) |
|
96 | { |
||
97 | 3 | return false; |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * @inheritdoc |
||
102 | */ |
||
103 | 3 | public function deleteDir($path) |
|
104 | { |
||
105 | 3 | return false; |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * @inheritdoc |
||
110 | */ |
||
111 | 3 | public function getMetadata($path) |
|
112 | { |
||
113 | 3 | if ( ! $response = $this->head($path)) { |
|
114 | 3 | return false; |
|
115 | } |
||
116 | |||
117 | return [ |
||
118 | 3 | 'type' => 'file', |
|
119 | 3 | 'path' => $path, |
|
120 | 3 | ] + $this->getResponseMetadata($path, $response); |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * @inheritdoc |
||
125 | */ |
||
126 | 3 | public function getMimetype($path) |
|
127 | { |
||
128 | 3 | return $this->getMetadata($path); |
|
129 | } |
||
130 | |||
131 | /** |
||
132 | * @inheritdoc |
||
133 | */ |
||
134 | 3 | public function getSize($path) |
|
135 | { |
||
136 | 3 | return $this->getMetadata($path); |
|
137 | } |
||
138 | |||
139 | /** |
||
140 | * @inheritdoc |
||
141 | */ |
||
142 | 3 | public function getTimestamp($path) |
|
143 | { |
||
144 | 3 | return $this->getMetadata($path); |
|
145 | } |
||
146 | |||
147 | /** |
||
148 | * @inheritdoc |
||
149 | */ |
||
150 | 3 | public function getVisibility($path) |
|
151 | { |
||
152 | return [ |
||
153 | 3 | 'path' => $path, |
|
154 | 3 | 'visibility' => $this->visibility, |
|
155 | 1 | ]; |
|
156 | } |
||
157 | |||
158 | /** |
||
159 | * @inheritdoc |
||
160 | */ |
||
161 | 3 | public function has($path) |
|
162 | { |
||
163 | 3 | return (bool) $this->head($path); |
|
164 | } |
||
165 | |||
166 | /** |
||
167 | * @inheritdoc |
||
168 | */ |
||
169 | 3 | public function listContents($directory = '', $recursive = false) |
|
170 | { |
||
171 | 3 | return []; |
|
172 | } |
||
173 | |||
174 | /** |
||
175 | * @inheritdoc |
||
176 | */ |
||
177 | 3 | View Code Duplication | public function read($path) |
0 ignored issues
–
show
|
|||
178 | { |
||
179 | 3 | if ( ! $response = $this->get($path)) { |
|
180 | 3 | return false; |
|
181 | } |
||
182 | |||
183 | return [ |
||
184 | 3 | 'path' => $path, |
|
185 | 3 | 'contents' => (string) $response->getBody(), |
|
186 | 3 | ] + $this->getResponseMetadata($path, $response); |
|
187 | } |
||
188 | |||
189 | /** |
||
190 | * @inheritdoc |
||
191 | */ |
||
192 | 3 | View Code Duplication | public function readStream($path) |
0 ignored issues
–
show
This method seems to be duplicated in 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. ![]() |
|||
193 | { |
||
194 | 3 | if ( ! $response = $this->get($path)) { |
|
195 | 3 | return false; |
|
196 | } |
||
197 | |||
198 | return [ |
||
199 | 3 | 'path' => $path, |
|
200 | 3 | 'stream' => $response->getBody()->detach(), |
|
201 | 3 | ] + $this->getResponseMetadata($path, $response); |
|
202 | } |
||
203 | |||
204 | /** |
||
205 | * @inheritdoc |
||
206 | */ |
||
207 | 3 | public function rename($path, $newpath) |
|
208 | { |
||
209 | 3 | return false; |
|
210 | } |
||
211 | |||
212 | /** |
||
213 | * @inheritdoc |
||
214 | */ |
||
215 | 3 | public function setVisibility($path, $visibility) |
|
216 | { |
||
217 | 3 | throw new \LogicException('GuzzleAdapter does not support visibility. Path: ' . $path . ', visibility: ' . $visibility); |
|
218 | } |
||
219 | |||
220 | /** |
||
221 | * @inheritdoc |
||
222 | */ |
||
223 | 3 | public function update($path, $contents, Config $conf) |
|
224 | { |
||
225 | 3 | return false; |
|
226 | } |
||
227 | |||
228 | /** |
||
229 | * @inheritdoc |
||
230 | */ |
||
231 | 3 | public function updateStream($path, $resource, Config $config) |
|
232 | { |
||
233 | 3 | return false; |
|
234 | } |
||
235 | |||
236 | /** |
||
237 | * @inheritdoc |
||
238 | */ |
||
239 | 3 | public function write($path, $contents, Config $config) |
|
240 | { |
||
241 | 3 | return false; |
|
242 | } |
||
243 | |||
244 | /** |
||
245 | * @inheritdoc |
||
246 | */ |
||
247 | 3 | public function writeStream($path, $resource, Config $config) |
|
248 | { |
||
249 | 3 | return false; |
|
250 | } |
||
251 | |||
252 | /** |
||
253 | * Performs a GET request. |
||
254 | * |
||
255 | * @param string $path The path to GET. |
||
256 | * |
||
257 | * @return \GuzzleHttp\Psr7\Response|false The response or false if failed. |
||
258 | */ |
||
259 | 9 | protected function get($path) |
|
260 | { |
||
261 | try { |
||
262 | 9 | $response = $this->client->get($this->base . $path); |
|
263 | 7 | } catch (BadResponseException $e) { |
|
264 | 6 | return false; |
|
265 | } |
||
266 | |||
267 | 9 | if ($response->getStatusCode() !== 200) { |
|
268 | 3 | return false; |
|
269 | } |
||
270 | |||
271 | 9 | return $response; |
|
272 | } |
||
273 | |||
274 | /** |
||
275 | * Returns the mimetype of a response. |
||
276 | * |
||
277 | * @param string $path |
||
278 | * @param \Psr\Http\Message\ResponseInterface $response |
||
279 | * |
||
280 | * @return string |
||
281 | */ |
||
282 | 9 | protected function getMimetypeFromResponse($path, ResponseInterface $response) { |
|
283 | 9 | if ($mimetype = $response->getHeader('Content-Type')) { |
|
284 | |||
285 | 3 | list($mimetype) = explode(';', reset($mimetype), 2); |
|
286 | |||
287 | 3 | return trim($mimetype); |
|
288 | } |
||
289 | |||
290 | // Try to guess from file extension. |
||
291 | 9 | $uri = new Uri($path); |
|
292 | |||
293 | 9 | return MimeType::detectByFilename($uri->getPath()); |
|
294 | } |
||
295 | |||
296 | /** |
||
297 | * Returns the metadata array for a response. |
||
298 | * |
||
299 | * @param string $path |
||
300 | * @param \Psr\Http\Message\ResponseInterface $response |
||
301 | * |
||
302 | * @return array |
||
303 | */ |
||
304 | 9 | protected function getResponseMetadata($path, ResponseInterface $response) { |
|
305 | $metadata = [ |
||
306 | 9 | 'visibility' => $this->visibility, |
|
307 | 9 | 'mimetype' => $this->getMimetypeFromResponse($path, $response), |
|
308 | 3 | ]; |
|
309 | |||
310 | 9 | if ($last_modified = $response->getHeader('Last-Modified')) { |
|
311 | 3 | $last_modified = strtotime(reset($last_modified)); |
|
312 | |||
313 | 3 | if ($last_modified !== false) { |
|
314 | 3 | $metadata['timestamp'] = $last_modified; |
|
315 | 1 | } |
|
316 | 1 | } |
|
317 | |||
318 | 9 | if ($length = $response->getHeader('Content-Length')) { |
|
319 | 3 | $length = reset($length); |
|
320 | |||
321 | 3 | if (is_numeric($length)) { |
|
322 | 3 | $metadata['size'] = (int) $length; |
|
323 | 1 | } |
|
324 | 1 | } |
|
325 | |||
326 | 9 | return $metadata; |
|
327 | } |
||
328 | |||
329 | /** |
||
330 | * Performs a HEAD request. |
||
331 | * |
||
332 | * @param string $path The path to HEAD. |
||
333 | * |
||
334 | * @return \GuzzleHttp\Psr7\Response|false The response or false if failed. |
||
335 | */ |
||
336 | 6 | protected function head($path) |
|
337 | { |
||
338 | 6 | if ( ! $this->supportsHead) { |
|
339 | 3 | return $this->get($path); |
|
340 | } |
||
341 | |||
342 | try { |
||
343 | 6 | $response = $this->client->head($this->base . $path); |
|
344 | 6 | } catch (ClientException $e) { |
|
345 | 6 | if ($e->getResponse()->getStatusCode() === 405) { |
|
346 | 3 | $this->supportsHead = false; |
|
347 | |||
348 | 3 | return $this->get($path); |
|
349 | } |
||
350 | |||
351 | 3 | return false; |
|
352 | 3 | } catch (BadResponseException $e) { |
|
353 | 3 | return false; |
|
354 | } |
||
355 | |||
356 | 6 | if ($response->getStatusCode() !== 200) { |
|
357 | 3 | return false; |
|
358 | } |
||
359 | |||
360 | 6 | return $response; |
|
361 | } |
||
362 | } |
||
363 |
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.