nicolasbeauvais /
flysystem-onedrive
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 NicolasBeauvais\FlysystemOneDrive; |
||
| 4 | |||
| 5 | use ArrayObject; |
||
| 6 | use Microsoft\Graph\Graph; |
||
| 7 | use League\Flysystem\Config; |
||
| 8 | use League\Flysystem\Adapter\AbstractAdapter; |
||
| 9 | use League\Flysystem\Adapter\Polyfill\NotSupportingVisibilityTrait; |
||
| 10 | |||
| 11 | class OneDriveAdapter extends AbstractAdapter |
||
| 12 | { |
||
| 13 | use NotSupportingVisibilityTrait; |
||
| 14 | |||
| 15 | /** @var \Microsoft\Graph\Graph */ |
||
| 16 | protected $graph; |
||
| 17 | |||
| 18 | private $usePath; |
||
| 19 | |||
| 20 | public function __construct(Graph $graph, string $prefix = 'root', bool $usePath = true) |
||
| 21 | { |
||
| 22 | $this->graph = $graph; |
||
| 23 | $this->usePath = $usePath; |
||
| 24 | |||
| 25 | $this->setPathPrefix('/drive/'.$prefix.($this->usePath ? ':' : '')); |
||
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * {@inheritdoc} |
||
| 30 | */ |
||
| 31 | public function write($path, $contents, Config $config) |
||
| 32 | { |
||
| 33 | return $this->upload($path, $contents); |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * {@inheritdoc} |
||
| 38 | */ |
||
| 39 | public function writeStream($path, $resource, Config $config) |
||
| 40 | { |
||
| 41 | return $this->upload($path, $resource); |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * {@inheritdoc} |
||
| 46 | */ |
||
| 47 | public function update($path, $contents, Config $config) |
||
| 48 | { |
||
| 49 | return $this->upload($path, $contents); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * {@inheritdoc} |
||
| 54 | */ |
||
| 55 | public function updateStream($path, $resource, Config $config) |
||
| 56 | { |
||
| 57 | return $this->upload($path, $resource); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * {@inheritdoc} |
||
| 62 | */ |
||
| 63 | public function rename($path, $newPath): bool |
||
| 64 | { |
||
| 65 | $endpoint = $this->applyPathPrefix($path); |
||
| 66 | |||
| 67 | $patch = explode('/', $newPath); |
||
| 68 | $sliced = implode('/', array_slice($patch, 0, -1)); |
||
| 69 | |||
| 70 | try { |
||
| 71 | $this->graph->createRequest('PATCH', $endpoint) |
||
| 72 | ->attachBody([ |
||
| 73 | 'name' => end($patch), |
||
| 74 | 'parentReference' => [ |
||
| 75 | 'path' => $this->getPathPrefix().(empty($sliced) ? '' : rtrim($sliced, '/').'/'), |
||
| 76 | ], |
||
| 77 | ]) |
||
| 78 | ->execute(); |
||
| 79 | } catch (\Exception $e) { |
||
| 80 | return false; |
||
| 81 | } |
||
| 82 | |||
| 83 | return true; |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * {@inheritdoc} |
||
| 88 | */ |
||
| 89 | public function copy($path, $newPath): bool |
||
| 90 | { |
||
| 91 | $endpoint = $this->applyPathPrefix($path); |
||
| 92 | |||
| 93 | $patch = explode('/', $newPath); |
||
| 94 | $sliced = implode('/', array_slice($patch, 0, -1)); |
||
| 95 | |||
| 96 | try { |
||
| 97 | $promise = $this->graph->createRequest('POST', $endpoint.($this->usePath ? ':' : '').'/copy') |
||
| 98 | ->attachBody([ |
||
| 99 | 'name' => end($patch), |
||
| 100 | 'parentReference' => [ |
||
| 101 | 'path' => $this->getPathPrefix().(empty($sliced) ? '' : rtrim($sliced, '/').'/'), |
||
| 102 | ], |
||
| 103 | ]) |
||
| 104 | ->executeAsync(); |
||
| 105 | $promise->wait(); |
||
| 106 | } catch (\Exception $e) { |
||
| 107 | return false; |
||
| 108 | } |
||
| 109 | |||
| 110 | return true; |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * {@inheritdoc} |
||
| 115 | */ |
||
| 116 | public function delete($path): bool |
||
| 117 | { |
||
| 118 | $endpoint = $this->applyPathPrefix($path); |
||
| 119 | |||
| 120 | try { |
||
| 121 | $this->graph->createRequest('DELETE', $endpoint)->execute(); |
||
| 122 | } catch (\Exception $e) { |
||
| 123 | return false; |
||
| 124 | } |
||
| 125 | |||
| 126 | return true; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * {@inheritdoc} |
||
| 131 | */ |
||
| 132 | public function deleteDir($dirname): bool |
||
| 133 | { |
||
| 134 | return $this->delete($dirname); |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * {@inheritdoc} |
||
| 139 | */ |
||
| 140 | public function createDir($dirname, Config $config) |
||
| 141 | { |
||
| 142 | $patch = explode('/', $dirname); |
||
| 143 | $sliced = implode('/', array_slice($patch, 0, -1)); |
||
| 144 | |||
| 145 | View Code Duplication | if (empty($sliced) && $this->usePath) { |
|
| 146 | $endpoint = str_replace(':/', '', $this->getPathPrefix()).'/children'; |
||
| 147 | } else { |
||
| 148 | $endpoint = $this->applyPathPrefix($sliced).($this->usePath ? ':' : '').'/children'; |
||
| 149 | } |
||
| 150 | |||
| 151 | try { |
||
| 152 | $response = $this->graph->createRequest('POST', $endpoint) |
||
| 153 | ->attachBody([ |
||
| 154 | 'name' => end($patch), |
||
| 155 | 'folder' => new ArrayObject(), |
||
| 156 | ])->execute(); |
||
| 157 | } catch (\Exception $e) { |
||
| 158 | return false; |
||
| 159 | } |
||
| 160 | |||
| 161 | return $this->normalizeResponse($response->getBody(), $dirname); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * {@inheritdoc} |
||
| 166 | */ |
||
| 167 | public function has($path) |
||
| 168 | { |
||
| 169 | return $this->getMetadata($path); |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * {@inheritdoc} |
||
| 174 | */ |
||
| 175 | public function read($path) |
||
| 176 | { |
||
| 177 | if (! $object = $this->readStream($path)) { |
||
| 178 | return false; |
||
| 179 | } |
||
| 180 | |||
| 181 | $object['contents'] = stream_get_contents($object['stream']); |
||
| 182 | fclose($object['stream']); |
||
| 183 | unset($object['stream']); |
||
| 184 | |||
| 185 | return $object; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * {@inheritdoc} |
||
| 190 | */ |
||
| 191 | public function readStream($path) |
||
| 192 | { |
||
| 193 | $path = $this->applyPathPrefix($path); |
||
| 194 | |||
| 195 | try { |
||
| 196 | $file = tempnam(sys_get_temp_dir(), 'onedrive'); |
||
| 197 | |||
| 198 | $this->graph->createRequest('GET', $path.($this->usePath ? ':' : '').'/content') |
||
| 199 | ->download($file); |
||
| 200 | |||
| 201 | $stream = fopen($file, 'r'); |
||
| 202 | } catch (\Exception $e) { |
||
| 203 | return false; |
||
| 204 | } |
||
| 205 | |||
| 206 | return compact('stream'); |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * {@inheritdoc} |
||
| 211 | */ |
||
| 212 | public function listContents($directory = '', $recursive = false): array |
||
| 213 | { |
||
| 214 | View Code Duplication | if ($directory === '' && $this->usePath) { |
|
| 215 | $endpoint = str_replace(':/', '', $this->getPathPrefix()).'/children'; |
||
| 216 | } else { |
||
| 217 | $endpoint = $this->applyPathPrefix($directory).($this->usePath ? ':' : '').'/children'; |
||
| 218 | } |
||
| 219 | |||
| 220 | try { |
||
| 221 | $results = []; |
||
| 222 | $response = $this->graph->createRequest('GET', $endpoint)->execute(); |
||
| 223 | $items = $response->getBody()['value']; |
||
| 224 | |||
| 225 | if (! count($items)) { |
||
| 226 | return []; |
||
| 227 | } |
||
| 228 | |||
| 229 | foreach ($items as &$item) { |
||
| 230 | $results[] = $this->normalizeResponse($item, $this->applyPathPrefix($directory)); |
||
| 231 | |||
| 232 | if ($recursive && isset($item['folder'])) { |
||
| 233 | $results = array_merge($results, $this->listContents($directory.'/'.$item['name'], true)); |
||
| 234 | } |
||
| 235 | } |
||
| 236 | } catch (\Exception $e) { |
||
| 237 | return false; |
||
| 238 | } |
||
| 239 | |||
| 240 | return $results; |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * {@inheritdoc} |
||
| 245 | */ |
||
| 246 | public function getMetadata($path) |
||
| 247 | { |
||
| 248 | $path = $this->applyPathPrefix($path); |
||
| 249 | |||
| 250 | try { |
||
| 251 | $response = $this->graph->createRequest('GET', $path)->execute(); |
||
| 252 | } catch (\Exception $e) { |
||
| 253 | return false; |
||
| 254 | } |
||
| 255 | |||
| 256 | return $this->normalizeResponse($response->getBody(), $path); |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * {@inheritdoc} |
||
| 261 | */ |
||
| 262 | public function getSize($path) |
||
| 263 | { |
||
| 264 | return $this->getMetadata($path); |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * {@inheritdoc} |
||
| 269 | */ |
||
| 270 | public function getMimetype($path) |
||
| 271 | { |
||
| 272 | return $this->getMetadata($path); |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * {@inheritdoc} |
||
| 277 | */ |
||
| 278 | public function getTimestamp($path) |
||
| 279 | { |
||
| 280 | return $this->getMetadata($path); |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * {@inheritdoc} |
||
| 285 | */ |
||
| 286 | public function applyPathPrefix($path): string |
||
| 287 | { |
||
| 288 | $path = parent::applyPathPrefix($path); |
||
| 289 | |||
| 290 | return '/'.trim($path, '/'); |
||
| 291 | } |
||
| 292 | |||
| 293 | public function getGraph(): Graph |
||
| 294 | { |
||
| 295 | return $this->graph; |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param string $path |
||
| 300 | * @param resource|string $contents |
||
| 301 | * |
||
| 302 | * @return array|false file metadata |
||
| 303 | */ |
||
| 304 | protected function upload(string $path, $contents) |
||
| 305 | { |
||
| 306 | $path = $this->applyPathPrefix($path); |
||
| 307 | |||
| 308 | try { |
||
| 309 | $contents = $stream = \GuzzleHttp\Psr7\stream_for($contents); |
||
|
0 ignored issues
–
show
|
|||
| 310 | |||
| 311 | $response = $this->graph->createRequest('PUT', $path.($this->usePath ? ':' : '').'/content') |
||
| 312 | ->attachBody($contents) |
||
| 313 | ->execute(); |
||
| 314 | } catch (\Exception $e) { |
||
| 315 | return false; |
||
| 316 | } |
||
| 317 | |||
| 318 | return $this->normalizeResponse($response->getBody(), $path); |
||
| 319 | } |
||
| 320 | |||
| 321 | protected function normalizeResponse(array $response, string $path): array |
||
| 322 | { |
||
| 323 | $path = trim($this->removePathPrefix($path), '/'); |
||
| 324 | |||
| 325 | return [ |
||
| 326 | 'path' => empty($path) ? $response['name'] : $path.'/'.$response['name'], |
||
| 327 | 'timestamp' => strtotime($response['lastModifiedDateTime']), |
||
| 328 | 'size' => $response['size'], |
||
| 329 | 'bytes' => $response['size'], |
||
| 330 | 'type' => isset($response['file']) ? 'file' : 'dir', |
||
| 331 | 'mimetype' => isset($response['file']) ? $response['file']['mimeType'] : null, |
||
| 332 | 'link' => isset($response['webUrl']) ? $response['webUrl'] : null, |
||
| 333 | ]; |
||
| 334 | } |
||
| 335 | } |
||
| 336 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.