PluginProbe ʕ •ᴥ•ʔ
File Manager Pro – Filester / 1.8
File Manager Pro – Filester v1.8
2.1.1 trunk 1.6.1 1.7.6 1.8 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9 2.0 2.0.1 2.0.2 2.1.0
filester / includes / File_manager / lib / php / elFinderVolumeOneDrive.class.php
filester / includes / File_manager / lib / php Last commit date
.tmp 2 years ago editors 2 years ago libs 2 years ago plugins 2 years ago resources 2 years ago MySQLStorage.sql 2 years ago autoload.php 2 years ago elFinder.class.php 2 years ago elFinderConnector.class.php 2 years ago elFinderFlysystemGoogleDriveNetmount.php 2 years ago elFinderPlugin.php 2 years ago elFinderSession.php 2 years ago elFinderSessionInterface.php 2 years ago elFinderVolumeBox.class.php 2 years ago elFinderVolumeDriver.class.php 2 years ago elFinderVolumeDropbox.class.php 2 years ago elFinderVolumeDropbox2.class.php 2 years ago elFinderVolumeFTP.class.php 2 years ago elFinderVolumeGoogleDrive.class.php 2 years ago elFinderVolumeGroup.class.php 2 years ago elFinderVolumeLocalFileSystem.class.php 2 years ago elFinderVolumeMySQL.class.php 2 years ago elFinderVolumeOneDrive.class.php 2 years ago elFinderVolumeSFTPphpseclib.class.php 2 years ago elFinderVolumeTrash.class.php 2 years ago elFinderVolumeTrashMySQL.class.php 2 years ago index.php 2 years ago mime.types 2 years ago
elFinderVolumeOneDrive.class.php
2124 lines
1 <?php
2
3 /**
4 * Simple elFinder driver for OneDrive
5 * onedrive api v5.0.
6 *
7 * @author Dmitry (dio) Levashov
8 * @author Cem (discofever)
9 **/
10 class elFinderVolumeOneDrive extends elFinderVolumeDriver
11 {
12 /**
13 * Driver id
14 * Must be started from letter and contains [a-z0-9]
15 * Used as part of volume id.
16 *
17 * @var string
18 **/
19 protected $driverId = 'od';
20
21 /**
22 * @var string The base URL for API requests
23 **/
24 const API_URL = 'https://graph.microsoft.com/v1.0/me/drive/items/';
25
26 /**
27 * @var string The base URL for authorization requests
28 */
29 const AUTH_URL = 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize';
30
31 /**
32 * @var string The base URL for token requests
33 */
34 const TOKEN_URL = 'https://login.microsoftonline.com/common/oauth2/v2.0/token';
35
36 /**
37 * OneDrive token object.
38 *
39 * @var object
40 **/
41 protected $token = null;
42
43 /**
44 * Directory for tmp files
45 * If not set driver will try to use tmbDir as tmpDir.
46 *
47 * @var string
48 **/
49 protected $tmp = '';
50
51 /**
52 * Net mount key.
53 *
54 * @var string
55 **/
56 public $netMountKey = '';
57
58 /**
59 * Thumbnail prefix.
60 *
61 * @var string
62 **/
63 protected $tmbPrefix = '';
64
65 /**
66 * hasCache by folders.
67 *
68 * @var array
69 **/
70 protected $HasdirsCache = array();
71
72 /**
73 * Query options of API call.
74 *
75 * @var array
76 */
77 protected $queryOptions = array();
78
79 /**
80 * Current token expires
81 *
82 * @var integer
83 **/
84 private $expires;
85
86 /**
87 * Path to access token file for permanent mount
88 *
89 * @var string
90 */
91 private $aTokenFile = '';
92
93 /**
94 * Constructor
95 * Extend options with required fields.
96 *
97 * @author Dmitry (dio) Levashov
98 * @author Cem (DiscoFever)
99 **/
100 public function __construct()
101 {
102 $opts = array(
103 'client_id' => '',
104 'client_secret' => '',
105 'accessToken' => '',
106 'root' => 'OneDrive.com',
107 'OneDriveApiClient' => '',
108 'path' => '/',
109 'separator' => '/',
110 'tmbPath' => '',
111 'tmbURL' => '',
112 'tmpPath' => '',
113 'acceptedName' => '#^[^/\\?*:|"<>]*[^./\\?*:|"<>]$#',
114 'rootCssClass' => 'elfinder-navbar-root-onedrive',
115 'useApiThumbnail' => true,
116 );
117 $this->options = array_merge($this->options, $opts);
118 $this->options['mimeDetect'] = 'internal';
119 }
120
121 /*********************************************************************/
122 /* ORIGINAL FUNCTIONS */
123 /*********************************************************************/
124
125 /**
126 * Obtains a new access token from OAuth. This token is valid for one hour.
127 *
128 * @param $client_id
129 * @param $client_secret
130 * @param string $code The code returned by OneDrive after
131 * successful log in
132 *
133 * @return object|string
134 * @throws Exception Thrown if the redirect URI of this Client instance's
135 * state is not set
136 */
137 protected function _od_obtainAccessToken($client_id, $client_secret, $code, $nodeid)
138 {
139 if (null === $client_id) {
140 return 'The client ID must be set to call obtainAccessToken()';
141 }
142
143 if (null === $client_secret) {
144 return 'The client Secret must be set to call obtainAccessToken()';
145 }
146
147 $redirect = elFinder::getConnectorUrl();
148 if (strpos($redirect, '/netmount/onedrive/') === false) {
149 $redirect .= '/netmount/onedrive/' . ($nodeid === 'elfinder'? '1' : $nodeid);
150 }
151
152 $url = self::TOKEN_URL;
153
154 $curl = curl_init();
155
156 $fields = http_build_query(
157 array(
158 'client_id' => $client_id,
159 'redirect_uri' => $redirect,
160 'client_secret' => $client_secret,
161 'code' => $code,
162 'grant_type' => 'authorization_code',
163 )
164 );
165
166 curl_setopt_array($curl, array(
167 // General options.
168 CURLOPT_RETURNTRANSFER => true,
169 CURLOPT_POST => true,
170 CURLOPT_POSTFIELDS => $fields,
171
172 CURLOPT_HTTPHEADER => array(
173 'Content-Length: ' . strlen($fields),
174 ),
175
176 CURLOPT_URL => $url,
177 ));
178
179 $result = elFinder::curlExec($curl);
180
181 $decoded = json_decode($result);
182
183 if (null === $decoded) {
184 throw new \Exception('json_decode() failed');
185 }
186
187 $res = (object)array(
188 'expires' => time() + $decoded->expires_in - 30,
189 'initialToken' => '',
190 'data' => $decoded
191 );
192 if (!empty($decoded->refresh_token)) {
193 $res->initialToken = md5($client_id . $decoded->refresh_token);
194 }
195 return $res;
196 }
197
198 /**
199 * Get token and auto refresh.
200 *
201 * @return true
202 * @throws Exception
203 */
204 protected function _od_refreshToken()
205 {
206 if (!property_exists($this->token, 'expires') || $this->token->expires < time()) {
207 if (!$this->options['client_id']) {
208 $this->options['client_id'] = ELFINDER_ONEDRIVE_CLIENTID;
209 }
210
211 if (!$this->options['client_secret']) {
212 $this->options['client_secret'] = ELFINDER_ONEDRIVE_CLIENTSECRET;
213 }
214
215 if (empty($this->token->data->refresh_token)) {
216 throw new \Exception(elFinder::ERROR_REAUTH_REQUIRE);
217 } else {
218 $refresh_token = $this->token->data->refresh_token;
219 $initialToken = $this->_od_getInitialToken();
220 }
221
222 $url = self::TOKEN_URL;
223
224 $curl = curl_init();
225
226 curl_setopt_array($curl, array(
227 // General options.
228 CURLOPT_RETURNTRANSFER => true,
229 CURLOPT_POST => true, // i am sending post data
230 CURLOPT_POSTFIELDS => 'client_id=' . urlencode($this->options['client_id'])
231 . '&client_secret=' . urlencode($this->options['client_secret'])
232 . '&grant_type=refresh_token'
233 . '&refresh_token=' . urlencode($this->token->data->refresh_token),
234
235 CURLOPT_URL => $url,
236 ));
237
238 $result = elFinder::curlExec($curl);
239
240 $decoded = json_decode($result);
241
242 if (!$decoded) {
243 throw new \Exception('json_decode() failed');
244 }
245
246 if (empty($decoded->access_token)) {
247 if ($this->aTokenFile) {
248 if (is_file($this->aTokenFile)) {
249 unlink($this->aTokenFile);
250 }
251 }
252 $err = property_exists($decoded, 'error')? ' ' . $decoded->error : '';
253 $err .= property_exists($decoded, 'error_description')? ' ' . $decoded->error_description : '';
254 throw new \Exception($err? $err : elFinder::ERROR_REAUTH_REQUIRE);
255 }
256
257 $token = (object)array(
258 'expires' => time() + $decoded->expires_in - 30,
259 'initialToken' => $initialToken,
260 'data' => $decoded,
261 );
262
263 $this->token = $token;
264 $json = json_encode($token);
265
266 if (!empty($decoded->refresh_token)) {
267 if (empty($this->options['netkey']) && $this->aTokenFile) {
268 file_put_contents($this->aTokenFile, json_encode($token));
269 $this->options['accessToken'] = $json;
270 } else if (!empty($this->options['netkey'])) {
271 // OAuth2 refresh token can be used only once,
272 // so update it if it is the same as the token file
273 $aTokenFile = $this->_od_getATokenFile();
274 if ($aTokenFile && is_file($aTokenFile)) {
275 if ($_token = json_decode(file_get_contents($aTokenFile))) {
276 if ($_token->data->refresh_token === $refresh_token) {
277 file_put_contents($aTokenFile, $json);
278 }
279 }
280 }
281 $this->options['accessToken'] = $json;
282 // update session value
283 elFinder::$instance->updateNetVolumeOption($this->options['netkey'], 'accessToken', $this->options['accessToken']);
284 $this->session->set('OneDriveTokens', $token);
285 } else {
286 throw new \Exception(elFinder::ERROR_CREATING_TEMP_DIR);
287 }
288 }
289 }
290
291 return true;
292 }
293
294 /**
295 * Get Parent ID, Item ID, Parent Path as an array from path.
296 *
297 * @param string $path
298 *
299 * @return array
300 */
301 protected function _od_splitPath($path)
302 {
303 $path = trim($path, '/');
304 $pid = '';
305 if ($path === '') {
306 $id = 'root';
307 $parent = '';
308 } else {
309 $paths = explode('/', trim($path, '/'));
310 $id = array_pop($paths);
311 if ($paths) {
312 $parent = '/' . implode('/', $paths);
313 $pid = array_pop($paths);
314 } else {
315 $pid = 'root';
316 $parent = '/';
317 }
318 }
319
320 return array($pid, $id, $parent);
321 }
322
323 /**
324 * Creates a base cURL object which is compatible with the OneDrive API.
325 *
326 * @return resource A compatible cURL object
327 */
328 protected function _od_prepareCurl($url = null)
329 {
330 $curl = curl_init($url);
331
332 $defaultOptions = array(
333 // General options.
334 CURLOPT_RETURNTRANSFER => true,
335 CURLOPT_HTTPHEADER => array(
336 'Content-Type: application/json',
337 'Authorization: Bearer ' . $this->token->data->access_token,
338 ),
339 );
340
341 curl_setopt_array($curl, $defaultOptions);
342
343 return $curl;
344 }
345
346 /**
347 * Creates a base cURL object which is compatible with the OneDrive API.
348 *
349 * @param string $path The path of the API call (eg. me/skydrive)
350 * @param bool $contents
351 *
352 * @return resource A compatible cURL object
353 * @throws elFinderAbortException
354 */
355 protected function _od_createCurl($path, $contents = false)
356 {
357 elFinder::checkAborted();
358 $curl = $this->_od_prepareCurl($path);
359
360 if ($contents) {
361 $res = elFinder::curlExec($curl);
362 } else {
363 $result = json_decode(elFinder::curlExec($curl));
364 if (isset($result->value)) {
365 $res = $result->value;
366 unset($result->value);
367 $result = (array)$result;
368 if (!empty($result['@odata.nextLink'])) {
369 $nextRes = $this->_od_createCurl($result['@odata.nextLink'], false);
370 if (is_array($nextRes)) {
371 $res = array_merge($res, $nextRes);
372 }
373 }
374 } else {
375 $res = $result;
376 }
377 }
378
379 return $res;
380 }
381
382 /**
383 * Drive query and fetchAll.
384 *
385 * @param $itemId
386 * @param bool $fetch_self
387 * @param bool $recursive
388 * @param array $options
389 *
390 * @return object|array
391 * @throws elFinderAbortException
392 */
393 protected function _od_query($itemId, $fetch_self = false, $recursive = false, $options = array())
394 {
395 $result = array();
396
397 if (null === $itemId) {
398 $itemId = 'root';
399 }
400
401 if ($fetch_self == true) {
402 $path = $itemId;
403 } else {
404 $path = $itemId . '/children';
405 }
406
407 if (isset($options['query'])) {
408 $path .= '?' . http_build_query($options['query']);
409 }
410
411 $url = self::API_URL . $path;
412
413 $res = $this->_od_createCurl($url);
414 if (!$fetch_self && $recursive && is_array($res)) {
415 foreach ($res as $file) {
416 $result[] = $file;
417 if (!empty($file->folder)) {
418 $result = array_merge($result, $this->_od_query($file->id, false, true, $options));
419 }
420 }
421 } else {
422 $result = $res;
423 }
424
425 return isset($result->error) ? array() : $result;
426 }
427
428 /**
429 * Parse line from onedrive metadata output and return file stat (array).
430 *
431 * @param object $raw line from ftp_rawlist() output
432 *
433 * @return array
434 * @author Dmitry Levashov
435 **/
436 protected function _od_parseRaw($raw)
437 {
438 $stat = array();
439
440 $folder = isset($raw->folder) ? $raw->folder : null;
441
442 $stat['rev'] = isset($raw->id) ? $raw->id : 'root';
443 $stat['name'] = $raw->name;
444 if (isset($raw->lastModifiedDateTime)) {
445 $stat['ts'] = strtotime($raw->lastModifiedDateTime);
446 }
447
448 if ($folder) {
449 $stat['mime'] = 'directory';
450 $stat['size'] = 0;
451 if (empty($folder->childCount)) {
452 $stat['dirs'] = 0;
453 } else {
454 $stat['dirs'] = -1;
455 }
456 } else {
457 if (isset($raw->file->mimeType)) {
458 $stat['mime'] = $raw->file->mimeType;
459 }
460 $stat['size'] = (int)$raw->size;
461 if (!$this->disabledGetUrl) {
462 $stat['url'] = '1';
463 }
464 if (isset($raw->image) && $img = $raw->image) {
465 isset($img->width) ? $stat['width'] = $img->width : $stat['width'] = 0;
466 isset($img->height) ? $stat['height'] = $img->height : $stat['height'] = 0;
467 }
468 if (!empty($raw->thumbnails)) {
469 if ($raw->thumbnails[0]->small->url) {
470 $stat['tmb'] = substr($raw->thumbnails[0]->small->url, 8); // remove "https://"
471 }
472 } elseif (!empty($raw->file->processingMetadata)) {
473 $stat['tmb'] = '1';
474 }
475 }
476
477 return $stat;
478 }
479
480 /**
481 * Get raw data(onedrive metadata) from OneDrive.
482 *
483 * @param string $path
484 *
485 * @return array|object onedrive metadata
486 */
487 protected function _od_getFileRaw($path)
488 {
489 list(, $itemId) = $this->_od_splitPath($path);
490 try {
491 $res = $this->_od_query($itemId, true, false, $this->queryOptions);
492
493 return $res;
494 } catch (Exception $e) {
495 return array();
496 }
497 }
498
499 /**
500 * Get thumbnail from OneDrive.com.
501 *
502 * @param string $path
503 *
504 * @return string | boolean
505 */
506 protected function _od_getThumbnail($path)
507 {
508 list(, $itemId) = $this->_od_splitPath($path);
509
510 try {
511 $url = self::API_URL . $itemId . '/thumbnails/0/medium/content';
512
513 return $this->_od_createCurl($url, $contents = true);
514 } catch (Exception $e) {
515 return false;
516 }
517 }
518
519 /**
520 * Upload large files with an upload session.
521 *
522 * @param resource $fp source file pointer
523 * @param number $size total size
524 * @param string $name item name
525 * @param string $itemId item identifier
526 * @param string $parent parent
527 * @param string $parentId parent identifier
528 *
529 * @return string The item path
530 */
531 protected function _od_uploadSession($fp, $size, $name, $itemId, $parent, $parentId)
532 {
533 try {
534 $send = $this->_od_getChunkData($fp);
535 if ($send === false) {
536 throw new Exception('Data can not be acquired from the source.');
537 }
538
539 // create upload session
540 if ($itemId) {
541 $url = self::API_URL . $itemId . '/createUploadSession';
542 } else {
543 $url = self::API_URL . $parentId . ':/' . rawurlencode($name) . ':/createUploadSession';
544 }
545 $curl = $this->_od_prepareCurl($url);
546 curl_setopt_array($curl, array(
547 CURLOPT_POST => true,
548 CURLOPT_POSTFIELDS => '{}',
549 ));
550 $sess = json_decode(elFinder::curlExec($curl));
551
552 if ($sess) {
553 if (isset($sess->error)) {
554 throw new Exception($sess->error->message);
555 }
556 $next = strlen($send);
557 $range = '0-' . ($next - 1) . '/' . $size;
558 } else {
559 throw new Exception('API response can not be obtained.');
560 }
561
562 $id = null;
563 $retry = 0;
564 while ($sess) {
565 elFinder::extendTimeLimit();
566 $putFp = tmpfile();
567 fwrite($putFp, $send);
568 rewind($putFp);
569 $_size = strlen($send);
570 $url = $sess->uploadUrl;
571 $curl = curl_init();
572 $options = array(
573 CURLOPT_URL => $url,
574 CURLOPT_PUT => true,
575 CURLOPT_RETURNTRANSFER => true,
576 CURLOPT_INFILE => $putFp,
577 CURLOPT_INFILESIZE => $_size,
578 CURLOPT_HTTPHEADER => array(
579 'Content-Length: ' . $_size,
580 'Content-Range: bytes ' . $range,
581 ),
582 );
583 curl_setopt_array($curl, $options);
584 $sess = json_decode(elFinder::curlExec($curl));
585 if ($sess) {
586 if (isset($sess->error)) {
587 throw new Exception($sess->error->message);
588 }
589 if (isset($sess->id)) {
590 $id = $sess->id;
591 break;
592 }
593 if (isset($sess->nextExpectedRanges)) {
594 list($_next) = explode('-', $sess->nextExpectedRanges[0]);
595 if ($next == $_next) {
596 $send = $this->_od_getChunkData($fp);
597 if ($send === false) {
598 throw new Exception('Data can not be acquired from the source.');
599 }
600 $next += strlen($send);
601 $range = $_next . '-' . ($next - 1) . '/' . $size;
602 $retry = 0;
603 } else {
604 if (++$retry > 3) {
605 throw new Exception('Retry limit exceeded with uploadSession API call.');
606 }
607 }
608 $sess->uploadUrl = $url;
609 }
610 } else {
611 throw new Exception('API response can not be obtained.');
612 }
613 }
614
615 if ($id) {
616 return $this->_joinPath($parent, $id);
617 } else {
618 throw new Exception('An error occurred in the uploadSession API call.');
619 }
620 } catch (Exception $e) {
621 return $this->setError('OneDrive error: ' . $e->getMessage());
622 }
623 }
624
625 /**
626 * Get chunk data by file pointer to upload session.
627 *
628 * @param resource $fp source file pointer
629 *
630 * @return bool|string chunked data
631 */
632 protected function _od_getChunkData($fp)
633 {
634 static $chunkSize = null;
635 if ($chunkSize === null) {
636 $mem = elFinder::getIniBytes('memory_limit');
637 if ($mem < 1) {
638 $mem = 10485760; // 10 MiB
639 } else {
640 $mem -= memory_get_usage() - 1061548;
641 $mem = min($mem, 10485760);
642 }
643 if ($mem > 327680) {
644 $chunkSize = floor($mem / 327680) * 327680;
645 } else {
646 $chunkSize = $mem;
647 }
648 }
649 if ($chunkSize < 8192) {
650 return false;
651 }
652
653 $contents = '';
654 while (!feof($fp) && strlen($contents) < $chunkSize) {
655 $contents .= fread($fp, 8192);
656 }
657
658 return $contents;
659 }
660
661 /**
662 * Get AccessToken file path
663 *
664 * @return string ( description_of_the_return_value )
665 */
666 protected function _od_getATokenFile()
667 {
668 $tmp = $aTokenFile = '';
669 if (!empty($this->token->data->refresh_token)) {
670 if (!$this->tmp) {
671 $tmp = elFinder::getStaticVar('commonTempPath');
672 if (!$tmp) {
673 $tmp = $this->getTempPath();
674 }
675 $this->tmp = $tmp;
676 }
677 if ($tmp) {
678 $aTokenFile = $tmp . DIRECTORY_SEPARATOR . $this->_od_getInitialToken() . '.otoken';
679 }
680 }
681 return $aTokenFile;
682 }
683
684 /**
685 * Get Initial Token (MD5 hash)
686 *
687 * @return string
688 */
689 protected function _od_getInitialToken()
690 {
691 return (empty($this->token->initialToken)? md5($this->options['client_id'] . (!empty($this->token->data->refresh_token)? $this->token->data->refresh_token : $this->token->data->access_token)) : $this->token->initialToken);
692 }
693
694 /*********************************************************************/
695 /* OVERRIDE FUNCTIONS */
696 /*********************************************************************/
697
698 /**
699 * Prepare
700 * Call from elFinder::netmout() before volume->mount().
701 *
702 * @return array
703 * @author Naoki Sawada
704 * @author Raja Sharma updating for OneDrive
705 **/
706 public function netmountPrepare($options)
707 {
708 if (empty($options['client_id']) && defined('ELFINDER_ONEDRIVE_CLIENTID')) {
709 $options['client_id'] = ELFINDER_ONEDRIVE_CLIENTID;
710 }
711 if (empty($options['client_secret']) && defined('ELFINDER_ONEDRIVE_CLIENTSECRET')) {
712 $options['client_secret'] = ELFINDER_ONEDRIVE_CLIENTSECRET;
713 }
714
715 if (isset($options['pass']) && $options['pass'] === 'reauth') {
716 $options['user'] = 'init';
717 $options['pass'] = '';
718 $this->session->remove('OneDriveTokens');
719 }
720
721 if (isset($options['id'])) {
722 $this->session->set('nodeId', $options['id']);
723 } elseif ($_id = $this->session->get('nodeId')) {
724 $options['id'] = $_id;
725 $this->session->set('nodeId', $_id);
726 }
727
728 if (!empty($options['tmpPath'])) {
729 if ((is_dir($options['tmpPath']) || mkdir($this->options['tmpPath'])) && is_writable($options['tmpPath'])) {
730 $this->tmp = $options['tmpPath'];
731 }
732 }
733
734 try {
735 if (empty($options['client_id']) || empty($options['client_secret'])) {
736 return array('exit' => true, 'body' => '{msg:errNetMountNoDriver}');
737 }
738
739 $itpCare = isset($options['code']);
740 $code = $itpCare? $options['code'] : (isset($_GET['code'])? $_GET['code'] : '');
741 if ($code) {
742 try {
743 if (!empty($options['id'])) {
744 // Obtain the token using the code received by the OneDrive API
745 $this->session->set('OneDriveTokens',
746 $this->_od_obtainAccessToken($options['client_id'], $options['client_secret'], $code, $options['id']));
747
748 $out = array(
749 'node' => $options['id'],
750 'json' => '{"protocol": "onedrive", "mode": "done", "reset": 1}',
751 'bind' => 'netmount',
752 );
753 } else {
754 $nodeid = ($_GET['host'] === '1')? 'elfinder' : $_GET['host'];
755 $out = array(
756 'node' => $nodeid,
757 'json' => json_encode(array(
758 'protocol' => 'onedrive',
759 'host' => $nodeid,
760 'mode' => 'redirect',
761 'options' => array(
762 'id' => $nodeid,
763 'code'=> $code
764 )
765 )),
766 'bind' => 'netmount'
767 );
768 }
769 if (!$itpCare) {
770 return array('exit' => 'callback', 'out' => $out);
771 } else {
772 return array('exit' => true, 'body' => $out['json']);
773 }
774 } catch (Exception $e) {
775 $out = array(
776 'node' => $options['id'],
777 'json' => json_encode(array('error' => elFinder::ERROR_ACCESS_DENIED . ' ' . $e->getMessage())),
778 );
779
780 return array('exit' => 'callback', 'out' => $out);
781 }
782 } elseif (!empty($_GET['error'])) {
783 $out = array(
784 'node' => $options['id'],
785 'json' => json_encode(array('error' => elFinder::ERROR_ACCESS_DENIED)),
786 );
787
788 return array('exit' => 'callback', 'out' => $out);
789 }
790
791 if ($options['user'] === 'init') {
792 $this->token = $this->session->get('OneDriveTokens');
793
794 if ($this->token) {
795 try {
796 $this->_od_refreshToken();
797 } catch (Exception $e) {
798 $this->setError($e->getMessage());
799 $this->token = null;
800 $this->session->remove('OneDriveTokens');
801 }
802 }
803
804 if (empty($this->token)) {
805 $result = false;
806 } else {
807 $path = $options['path'];
808 if ($path === '/') {
809 $path = 'root';
810 }
811 $result = $this->_od_query($path, false, false, array(
812 'query' => array(
813 'select' => 'id,name',
814 'filter' => 'folder ne null',
815 ),
816 ));
817 }
818
819 if ($result === false) {
820 try {
821 $this->session->set('OneDriveTokens', (object)array('token' => null));
822
823 $offline = '';
824 // Gets a log in URL with sufficient privileges from the OneDrive API
825 if (!empty($options['offline'])) {
826 $offline = ' offline_access';
827 }
828
829 $redirect_uri = elFinder::getConnectorUrl() . '/netmount/onedrive/' . ($options['id'] === 'elfinder'? '1' : $options['id']);
830 $url = self::AUTH_URL
831 . '?client_id=' . urlencode($options['client_id'])
832 . '&scope=' . urlencode('files.readwrite.all' . $offline)
833 . '&response_type=code'
834 . '&redirect_uri=' . urlencode($redirect_uri);
835
836 } catch (Exception $e) {
837 return array('exit' => true, 'body' => '{msg:errAccess}');
838 }
839
840 $html = '<input id="elf-volumedriver-onedrive-host-btn" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" value="{msg:btnApprove}" type="button">';
841 $html .= '<script>
842 $("#' . $options['id'] . '").elfinder("instance").trigger("netmount", {protocol: "onedrive", mode: "makebtn", url: "' . $url . '"});
843 </script>';
844
845 return array('exit' => true, 'body' => $html);
846 } else {
847 $folders = [];
848
849 if ($result) {
850 foreach ($result as $res) {
851 $folders[$res->id] = $res->name;
852 }
853 natcasesort($folders);
854 }
855
856 if ($options['pass'] === 'folders') {
857 return ['exit' => true, 'folders' => $folders];
858 }
859
860 $folders = ['root' => 'My OneDrive'] + $folders;
861 $folders = json_encode($folders);
862
863 $expires = empty($this->token->data->refresh_token) ? (int)$this->token->expires : 0;
864 $mnt2res = empty($this->token->data->refresh_token) ? '' : ', "mnt2res": 1';
865 $json = '{"protocol": "onedrive", "mode": "done", "folders": ' . $folders . ', "expires": ' . $expires . $mnt2res .'}';
866 $html = 'OneDrive.com';
867 $html .= '<script>
868 $("#' . $options['id'] . '").elfinder("instance").trigger("netmount", ' . $json . ');
869 </script>';
870
871 return array('exit' => true, 'body' => $html);
872 }
873 }
874 } catch (Exception $e) {
875 return array('exit' => true, 'body' => '{msg:errNetMountNoDriver}');
876 }
877
878 if ($_aToken = $this->session->get('OneDriveTokens')) {
879 $options['accessToken'] = json_encode($_aToken);
880 if ($this->options['path'] === 'root' || !$this->options['path']) {
881 $this->options['path'] = '/';
882 }
883 } else {
884 $this->session->remove('OneDriveTokens');
885 $this->setError(elFinder::ERROR_NETMOUNT, $options['host'], implode(' ', $this->error()));
886
887 return array('exit' => true, 'error' => $this->error());
888 }
889
890 $this->session->remove('nodeId');
891 unset($options['user'], $options['pass'], $options['id']);
892
893 return $options;
894 }
895
896 /**
897 * process of on netunmount
898 * Drop `onedrive` & rm thumbs.
899 *
900 * @param array $options
901 *
902 * @return bool
903 */
904 public function netunmount($netVolumes, $key)
905 {
906 if (!$this->options['useApiThumbnail'] && ($tmbs = glob(rtrim($this->options['tmbPath'], '\\/') . DIRECTORY_SEPARATOR . $this->tmbPrefix . '*.png'))) {
907 foreach ($tmbs as $file) {
908 unlink($file);
909 }
910 }
911
912 return true;
913 }
914
915 /**
916 * Return debug info for client.
917 *
918 * @return array
919 **/
920 public function debug()
921 {
922 $res = parent::debug();
923 if (!empty($this->options['netkey']) && !empty($this->options['accessToken'])) {
924 $res['accessToken'] = $this->options['accessToken'];
925 }
926
927 return $res;
928 }
929
930 /*********************************************************************/
931 /* INIT AND CONFIGURE */
932 /*********************************************************************/
933
934 /**
935 * Prepare FTP connection
936 * Connect to remote server and check if credentials are correct, if so, store the connection id in $ftp_conn.
937 *
938 * @return bool
939 * @throws elFinderAbortException
940 * @author Dmitry (dio) Levashov
941 * @author Cem (DiscoFever)
942 */
943 protected function init()
944 {
945 if (!$this->options['accessToken']) {
946 return $this->setError('Required option `accessToken` is undefined.');
947 }
948
949 if (!empty($this->options['tmpPath'])) {
950 if ((is_dir($this->options['tmpPath']) || mkdir($this->options['tmpPath'])) && is_writable($this->options['tmpPath'])) {
951 $this->tmp = $this->options['tmpPath'];
952 }
953 }
954
955 $error = false;
956 try {
957 $this->token = json_decode($this->options['accessToken']);
958 if (!is_object($this->token)) {
959 throw new Exception('Required option `accessToken` is invalid JSON.');
960 }
961
962 // make net mount key
963 if (empty($this->options['netkey'])) {
964 $this->netMountKey = $this->_od_getInitialToken();
965 } else {
966 $this->netMountKey = $this->options['netkey'];
967 }
968
969 if ($this->aTokenFile = $this->_od_getATokenFile()) {
970 if (empty($this->options['netkey'])) {
971 if ($this->aTokenFile) {
972 if (is_file($this->aTokenFile)) {
973 $this->token = json_decode(file_get_contents($this->aTokenFile));
974 if (!is_object($this->token)) {
975 unlink($this->aTokenFile);
976 throw new Exception('Required option `accessToken` is invalid JSON.');
977 }
978 } else {
979 file_put_contents($this->aTokenFile, $this->token);
980 }
981 }
982 } else if (is_file($this->aTokenFile)) {
983 // If the refresh token is the same as the permanent volume
984 $this->token = json_decode(file_get_contents($this->aTokenFile));
985 }
986 }
987
988 if ($this->needOnline) {
989 $this->_od_refreshToken();
990
991 $this->expires = empty($this->token->data->refresh_token) ? (int)$this->token->expires : 0;
992 }
993 } catch (Exception $e) {
994 $this->token = null;
995 $error = true;
996 $this->setError($e->getMessage());
997 }
998
999 if ($this->netMountKey) {
1000 $this->tmbPrefix = 'onedrive' . base_convert($this->netMountKey, 16, 32);
1001 }
1002
1003 if ($error) {
1004 if (empty($this->options['netkey']) && $this->tmbPrefix) {
1005 // for delete thumbnail
1006 $this->netunmount(null, null);
1007 }
1008 return false;
1009 }
1010
1011 // normalize root path
1012 if ($this->options['path'] == 'root') {
1013 $this->options['path'] = '/';
1014 }
1015
1016 $this->root = $this->options['path'] = $this->_normpath($this->options['path']);
1017
1018 $this->options['root'] = ($this->options['root'] == '')? 'OneDrive.com' : $this->options['root'];
1019
1020 if (empty($this->options['alias'])) {
1021 if ($this->needOnline) {
1022 $this->options['alias'] = ($this->options['path'] === '/') ? $this->options['root'] :
1023 $this->_od_query(basename($this->options['path']), $fetch_self = true)->name . '@OneDrive';
1024 if (!empty($this->options['netkey'])) {
1025 elFinder::$instance->updateNetVolumeOption($this->options['netkey'], 'alias', $this->options['alias']);
1026 }
1027 } else {
1028 $this->options['alias'] = $this->options['root'];
1029 }
1030 }
1031
1032 $this->rootName = $this->options['alias'];
1033
1034 // This driver dose not support `syncChkAsTs`
1035 $this->options['syncChkAsTs'] = false;
1036
1037 // 'lsPlSleep' minmum 10 sec
1038 $this->options['lsPlSleep'] = max(10, $this->options['lsPlSleep']);
1039
1040 $this->queryOptions = array(
1041 'query' => array(
1042 'select' => 'id,name,lastModifiedDateTime,file,folder,size,image',
1043 ),
1044 );
1045
1046 if ($this->options['useApiThumbnail']) {
1047 $this->options['tmbURL'] = 'https://';
1048 $this->options['tmbPath'] = '';
1049 $this->queryOptions['query']['expand'] = 'thumbnails(select=small)';
1050 }
1051
1052 // enable command archive
1053 $this->options['useRemoteArchive'] = true;
1054
1055 return true;
1056 }
1057
1058 /**
1059 * Configure after successfull mount.
1060 *
1061 * @author Dmitry (dio) Levashov
1062 **/
1063 protected function configure()
1064 {
1065 parent::configure();
1066
1067 // fallback of $this->tmp
1068 if (!$this->tmp && $this->tmbPathWritable) {
1069 $this->tmp = $this->tmbPath;
1070 }
1071 }
1072
1073 /*********************************************************************/
1074 /* FS API */
1075 /*********************************************************************/
1076
1077 /**
1078 * Close opened connection.
1079 *
1080 * @author Dmitry (dio) Levashov
1081 **/
1082 public function umount()
1083 {
1084 }
1085
1086 protected function isNameExists($path)
1087 {
1088 list($pid, $name) = $this->_od_splitPath($path);
1089
1090 $raw = $this->_od_query($pid . '/children/' . rawurlencode($name), true);
1091 return $raw ? $this->_od_parseRaw($raw) : false;
1092 }
1093
1094 /**
1095 * Cache dir contents.
1096 *
1097 * @param string $path dir path
1098 *
1099 * @return array
1100 * @throws elFinderAbortException
1101 * @author Dmitry Levashov
1102 */
1103 protected function cacheDir($path)
1104 {
1105 $this->dirsCache[$path] = array();
1106 $hasDir = false;
1107
1108 list(, $itemId) = $this->_od_splitPath($path);
1109
1110 $res = $this->_od_query($itemId, false, false, $this->queryOptions);
1111
1112 if ($res) {
1113 foreach ($res as $raw) {
1114 if ($stat = $this->_od_parseRaw($raw)) {
1115 $itemPath = $this->_joinPath($path, $raw->id);
1116 $stat = $this->updateCache($itemPath, $stat);
1117 if (empty($stat['hidden'])) {
1118 if (!$hasDir && $stat['mime'] === 'directory') {
1119 $hasDir = true;
1120 }
1121 $this->dirsCache[$path][] = $itemPath;
1122 }
1123 }
1124 }
1125 }
1126
1127 if (isset($this->sessionCache['subdirs'])) {
1128 $this->sessionCache['subdirs'][$path] = $hasDir;
1129 }
1130
1131 return $this->dirsCache[$path];
1132 }
1133
1134 /**
1135 * Copy file/recursive copy dir only in current volume.
1136 * Return new file path or false.
1137 *
1138 * @param string $src source path
1139 * @param string $dst destination dir path
1140 * @param string $name new file name (optionaly)
1141 *
1142 * @return string|false
1143 * @throws elFinderAbortException
1144 * @author Dmitry (dio) Levashov
1145 * @author Naoki Sawada
1146 */
1147 protected function copy($src, $dst, $name)
1148 {
1149 $itemId = '';
1150 if ($this->options['copyJoin']) {
1151 $test = $this->joinPathCE($dst, $name);
1152 if ($testStat = $this->isNameExists($test)) {
1153 $this->remove($test);
1154 }
1155 }
1156
1157 if ($path = $this->_copy($src, $dst, $name)) {
1158 $this->added[] = $this->stat($path);
1159 } else {
1160 $this->setError(elFinder::ERROR_COPY, $this->_path($src));
1161 }
1162
1163 return $path;
1164 }
1165
1166 /**
1167 * Remove file/ recursive remove dir.
1168 *
1169 * @param string $path file path
1170 * @param bool $force try to remove even if file locked
1171 *
1172 * @return bool
1173 * @throws elFinderAbortException
1174 * @author Dmitry (dio) Levashov
1175 * @author Naoki Sawada
1176 */
1177 protected function remove($path, $force = false)
1178 {
1179 $stat = $this->stat($path);
1180 $stat['realpath'] = $path;
1181 $this->rmTmb($stat);
1182 $this->clearcache();
1183
1184 if (empty($stat)) {
1185 return $this->setError(elFinder::ERROR_RM, $this->_path($path), elFinder::ERROR_FILE_NOT_FOUND);
1186 }
1187
1188 if (!$force && !empty($stat['locked'])) {
1189 return $this->setError(elFinder::ERROR_LOCKED, $this->_path($path));
1190 }
1191
1192 if ($stat['mime'] == 'directory') {
1193 if (!$this->_rmdir($path)) {
1194 return $this->setError(elFinder::ERROR_RM, $this->_path($path));
1195 }
1196 } else {
1197 if (!$this->_unlink($path)) {
1198 return $this->setError(elFinder::ERROR_RM, $this->_path($path));
1199 }
1200 }
1201
1202 $this->removed[] = $stat;
1203
1204 return true;
1205 }
1206
1207 /**
1208 * Create thumnbnail and return it's URL on success.
1209 *
1210 * @param string $path file path
1211 * @param $stat
1212 *
1213 * @return string|false
1214 * @throws ImagickException
1215 * @throws elFinderAbortException
1216 * @author Dmitry (dio) Levashov
1217 * @author Naoki Sawada
1218 */
1219 protected function createTmb($path, $stat)
1220 {
1221 if ($this->options['useApiThumbnail']) {
1222 if (func_num_args() > 2) {
1223 list(, , $count) = func_get_args();
1224 } else {
1225 $count = 0;
1226 }
1227 if ($count < 10) {
1228 if (isset($stat['tmb']) && $stat['tmb'] != '1') {
1229 return $stat['tmb'];
1230 } else {
1231 sleep(2);
1232 elFinder::extendTimeLimit();
1233 $this->clearcache();
1234 $stat = $this->stat($path);
1235
1236 return $this->createTmb($path, $stat, ++$count);
1237 }
1238 }
1239
1240 return false;
1241 }
1242 if (!$stat || !$this->canCreateTmb($path, $stat)) {
1243 return false;
1244 }
1245
1246 $name = $this->tmbname($stat);
1247 $tmb = $this->tmbPath . DIRECTORY_SEPARATOR . $name;
1248
1249 // copy image into tmbPath so some drivers does not store files on local fs
1250 if (!$data = $this->_od_getThumbnail($path)) {
1251 return false;
1252 }
1253 if (!file_put_contents($tmb, $data)) {
1254 return false;
1255 }
1256
1257 $result = false;
1258
1259 $tmbSize = $this->tmbSize;
1260
1261 if (($s = getimagesize($tmb)) == false) {
1262 return false;
1263 }
1264
1265 /* If image smaller or equal thumbnail size - just fitting to thumbnail square */
1266 if ($s[0] <= $tmbSize && $s[1] <= $tmbSize) {
1267 $result = $this->imgSquareFit($tmb, $tmbSize, $tmbSize, 'center', 'middle', $this->options['tmbBgColor'], 'png');
1268 } else {
1269 if ($this->options['tmbCrop']) {
1270
1271 /* Resize and crop if image bigger than thumbnail */
1272 if (!(($s[0] > $tmbSize && $s[1] <= $tmbSize) || ($s[0] <= $tmbSize && $s[1] > $tmbSize)) || ($s[0] > $tmbSize && $s[1] > $tmbSize)) {
1273 $result = $this->imgResize($tmb, $tmbSize, $tmbSize, true, false, 'png');
1274 }
1275
1276 if (($s = getimagesize($tmb)) != false) {
1277 $x = $s[0] > $tmbSize ? intval(($s[0] - $tmbSize) / 2) : 0;
1278 $y = $s[1] > $tmbSize ? intval(($s[1] - $tmbSize) / 2) : 0;
1279 $result = $this->imgCrop($tmb, $tmbSize, $tmbSize, $x, $y, 'png');
1280 }
1281 } else {
1282 $result = $this->imgResize($tmb, $tmbSize, $tmbSize, true, true, 'png');
1283 }
1284
1285 $result = $this->imgSquareFit($tmb, $tmbSize, $tmbSize, 'center', 'middle', $this->options['tmbBgColor'], 'png');
1286 }
1287
1288 if (!$result) {
1289 unlink($tmb);
1290
1291 return false;
1292 }
1293
1294 return $name;
1295 }
1296
1297 /**
1298 * Return thumbnail file name for required file.
1299 *
1300 * @param array $stat file stat
1301 *
1302 * @return string
1303 * @author Dmitry (dio) Levashov
1304 **/
1305 protected function tmbname($stat)
1306 {
1307 return $this->tmbPrefix . $stat['rev'] . $stat['ts'] . '.png';
1308 }
1309
1310 /**
1311 * Return content URL.
1312 *
1313 * @param string $hash file hash
1314 * @param array $options options
1315 *
1316 * @return string
1317 * @author Naoki Sawada
1318 **/
1319 public function getContentUrl($hash, $options = array())
1320 {
1321 if (!empty($options['onetime']) && $this->options['onetimeUrl']) {
1322 return parent::getContentUrl($hash, $options);
1323 }
1324 if (!empty($options['temporary'])) {
1325 // try make temporary file
1326 $url = parent::getContentUrl($hash, $options);
1327 if ($url) {
1328 return $url;
1329 }
1330 }
1331 $res = '';
1332 if (($file = $this->file($hash)) == false || !$file['url'] || $file['url'] == 1) {
1333 $path = $this->decode($hash);
1334
1335 list(, $itemId) = $this->_od_splitPath($path);
1336 try {
1337 $url = self::API_URL . $itemId . '/createLink';
1338 $data = (object)array(
1339 'type' => 'embed',
1340 'scope' => 'anonymous',
1341 );
1342 $curl = $this->_od_prepareCurl($url);
1343 curl_setopt_array($curl, array(
1344 CURLOPT_POST => true,
1345 CURLOPT_POSTFIELDS => json_encode($data),
1346 ));
1347
1348 $result = elFinder::curlExec($curl);
1349 if ($result) {
1350 $result = json_decode($result);
1351 if (isset($result->link)) {
1352 list(, $res) = explode('?', $result->link->webUrl);
1353 $res = 'https://onedrive.live.com/download.aspx?' . $res;
1354 }
1355 }
1356 } catch (Exception $e) {
1357 $res = '';
1358 }
1359 }
1360
1361 return $res;
1362 }
1363
1364 /*********************** paths/urls *************************/
1365
1366 /**
1367 * Return parent directory path.
1368 *
1369 * @param string $path file path
1370 *
1371 * @return string
1372 * @author Dmitry (dio) Levashov
1373 **/
1374 protected function _dirname($path)
1375 {
1376 list(, , $dirname) = $this->_od_splitPath($path);
1377
1378 return $dirname;
1379 }
1380
1381 /**
1382 * Return file name.
1383 *
1384 * @param string $path file path
1385 *
1386 * @return string
1387 * @author Dmitry (dio) Levashov
1388 **/
1389 protected function _basename($path)
1390 {
1391 list(, $basename) = $this->_od_splitPath($path);
1392
1393 return $basename;
1394 }
1395
1396 /**
1397 * Join dir name and file name and retur full path.
1398 *
1399 * @param string $dir
1400 * @param string $name
1401 *
1402 * @return string
1403 * @author Dmitry (dio) Levashov
1404 **/
1405 protected function _joinPath($dir, $name)
1406 {
1407 if ($dir === 'root') {
1408 $dir = '';
1409 }
1410
1411 return $this->_normpath($dir . '/' . $name);
1412 }
1413
1414 /**
1415 * Return normalized path, this works the same as os.path.normpath() in Python.
1416 *
1417 * @param string $path path
1418 *
1419 * @return string
1420 * @author Troex Nevelin
1421 **/
1422 protected function _normpath($path)
1423 {
1424 if (DIRECTORY_SEPARATOR !== '/') {
1425 $path = str_replace(DIRECTORY_SEPARATOR, '/', $path);
1426 }
1427 $path = '/' . ltrim($path, '/');
1428
1429 return $path;
1430 }
1431
1432 /**
1433 * Return file path related to root dir.
1434 *
1435 * @param string $path file path
1436 *
1437 * @return string
1438 * @author Dmitry (dio) Levashov
1439 **/
1440 protected function _relpath($path)
1441 {
1442 return $path;
1443 }
1444
1445 /**
1446 * Convert path related to root dir into real path.
1447 *
1448 * @param string $path file path
1449 *
1450 * @return string
1451 * @author Dmitry (dio) Levashov
1452 **/
1453 protected function _abspath($path)
1454 {
1455 return $path;
1456 }
1457
1458 /**
1459 * Return fake path started from root dir.
1460 *
1461 * @param string $path file path
1462 *
1463 * @return string
1464 * @author Dmitry (dio) Levashov
1465 **/
1466 protected function _path($path)
1467 {
1468 return $this->rootName . $this->_normpath(substr($path, strlen($this->root)));
1469 }
1470
1471 /**
1472 * Return true if $path is children of $parent.
1473 *
1474 * @param string $path path to check
1475 * @param string $parent parent path
1476 *
1477 * @return bool
1478 * @author Dmitry (dio) Levashov
1479 **/
1480 protected function _inpath($path, $parent)
1481 {
1482 return $path == $parent || strpos($path, $parent . '/') === 0;
1483 }
1484
1485 /***************** file stat ********************/
1486 /**
1487 * Return stat for given path.
1488 * Stat contains following fields:
1489 * - (int) size file size in b. required
1490 * - (int) ts file modification time in unix time. required
1491 * - (string) mime mimetype. required for folders, others - optionally
1492 * - (bool) read read permissions. required
1493 * - (bool) write write permissions. required
1494 * - (bool) locked is object locked. optionally
1495 * - (bool) hidden is object hidden. optionally
1496 * - (string) alias for symlinks - link target path relative to root path. optionally
1497 * - (string) target for symlinks - link target path. optionally.
1498 * If file does not exists - returns empty array or false.
1499 *
1500 * @param string $path file path
1501 *
1502 * @return array|false
1503 * @author Dmitry (dio) Levashov
1504 **/
1505 protected function _stat($path)
1506 {
1507 if ($raw = $this->_od_getFileRaw($path)) {
1508 $stat = $this->_od_parseRaw($raw);
1509 if ($path === $this->root) {
1510 $stat['expires'] = $this->expires;
1511 }
1512 return $stat;
1513 }
1514
1515 return false;
1516 }
1517
1518 /**
1519 * Return true if path is dir and has at least one childs directory.
1520 *
1521 * @param string $path dir path
1522 *
1523 * @return bool
1524 * @throws elFinderAbortException
1525 * @author Dmitry (dio) Levashov
1526 */
1527 protected function _subdirs($path)
1528 {
1529 list(, $itemId) = $this->_od_splitPath($path);
1530
1531 return (bool)$this->_od_query($itemId, false, false, array(
1532 'query' => array(
1533 'top' => 1,
1534 'select' => 'id',
1535 'filter' => 'folder ne null',
1536 ),
1537 ));
1538 }
1539
1540 /**
1541 * Return object width and height
1542 * Ususaly used for images, but can be realize for video etc...
1543 *
1544 * @param string $path file path
1545 * @param string $mime file mime type
1546 *
1547 * @return string
1548 * @throws elFinderAbortException
1549 * @author Dmitry (dio) Levashov
1550 */
1551 protected function _dimensions($path, $mime)
1552 {
1553 if (strpos($mime, 'image') !== 0) {
1554 return '';
1555 }
1556
1557 //$cache = $this->_od_getFileRaw($path);
1558 if (func_num_args() > 2) {
1559 $args = func_get_arg(2);
1560 } else {
1561 $args = array();
1562 }
1563 if (!empty($args['substitute'])) {
1564 $tmbSize = intval($args['substitute']);
1565 } else {
1566 $tmbSize = null;
1567 }
1568 list(, $itemId) = $this->_od_splitPath($path);
1569 $options = array(
1570 'query' => array(
1571 'select' => 'id,image',
1572 ),
1573 );
1574 if ($tmbSize) {
1575 $tmb = 'c' . $tmbSize . 'x' . $tmbSize;
1576 $options['query']['expand'] = 'thumbnails(select=' . $tmb . ')';
1577 }
1578 $raw = $this->_od_query($itemId, true, false, $options);
1579
1580 if ($raw && property_exists($raw, 'image') && $img = $raw->image) {
1581 if (isset($img->width) && isset($img->height)) {
1582 $ret = array('dim' => $img->width . 'x' . $img->height);
1583 if ($tmbSize) {
1584 $srcSize = explode('x', $ret['dim']);
1585 if (min(($tmbSize / $srcSize[0]), ($tmbSize / $srcSize[1])) < 1) {
1586 if (!empty($raw->thumbnails)) {
1587 $tmbArr = (array)$raw->thumbnails[0];
1588 if (!empty($tmbArr[$tmb]->url)) {
1589 $ret['url'] = $tmbArr[$tmb]->url;
1590 }
1591 }
1592 }
1593 }
1594
1595 return $ret;
1596 }
1597 }
1598
1599 $ret = '';
1600 if ($work = $this->getWorkFile($path)) {
1601 if ($size = @getimagesize($work)) {
1602 $cache['width'] = $size[0];
1603 $cache['height'] = $size[1];
1604 $ret = $size[0] . 'x' . $size[1];
1605 }
1606 }
1607 is_file($work) && @unlink($work);
1608
1609 return $ret;
1610 }
1611
1612 /******************** file/dir content *********************/
1613
1614 /**
1615 * Return files list in directory.
1616 *
1617 * @param string $path dir path
1618 *
1619 * @return array
1620 * @throws elFinderAbortException
1621 * @author Dmitry (dio) Levashov
1622 * @author Cem (DiscoFever)
1623 */
1624 protected function _scandir($path)
1625 {
1626 return isset($this->dirsCache[$path])
1627 ? $this->dirsCache[$path]
1628 : $this->cacheDir($path);
1629 }
1630
1631 /**
1632 * Open file and return file pointer.
1633 *
1634 * @param string $path file path
1635 * @param bool $write open file for writing
1636 *
1637 * @return resource|false
1638 * @author Dmitry (dio) Levashov
1639 **/
1640 protected function _fopen($path, $mode = 'rb')
1641 {
1642 if ($mode === 'rb' || $mode === 'r') {
1643 list(, $itemId) = $this->_od_splitPath($path);
1644 $data = array(
1645 'target' => self::API_URL . $itemId . '/content',
1646 'headers' => array('Authorization: Bearer ' . $this->token->data->access_token),
1647 );
1648
1649 // to support range request
1650 if (func_num_args() > 2) {
1651 $opts = func_get_arg(2);
1652 } else {
1653 $opts = array();
1654 }
1655 if (!empty($opts['httpheaders'])) {
1656 $data['headers'] = array_merge($opts['httpheaders'], $data['headers']);
1657 }
1658
1659 return elFinder::getStreamByUrl($data);
1660 }
1661
1662 return false;
1663 }
1664
1665 /**
1666 * Close opened file.
1667 *
1668 * @param resource $fp file pointer
1669 *
1670 * @return bool
1671 * @author Dmitry (dio) Levashov
1672 **/
1673 protected function _fclose($fp, $path = '')
1674 {
1675 is_resource($fp) && fclose($fp);
1676 if ($path) {
1677 unlink($this->getTempFile($path));
1678 }
1679 }
1680
1681 /******************** file/dir manipulations *************************/
1682
1683 /**
1684 * Create dir and return created dir path or false on failed.
1685 *
1686 * @param string $path parent dir path
1687 * @param string $name new directory name
1688 *
1689 * @return string|bool
1690 * @author Dmitry (dio) Levashov
1691 **/
1692 protected function _mkdir($path, $name)
1693 {
1694 $namePath = $this->_joinPath($path, $name);
1695 list($parentId) = $this->_od_splitPath($namePath);
1696
1697 try {
1698 $properties = array(
1699 'name' => (string)$name,
1700 'folder' => (object)array(),
1701 );
1702
1703 $data = (object)$properties;
1704
1705 $url = self::API_URL . $parentId . '/children';
1706
1707 $curl = $this->_od_prepareCurl($url);
1708
1709 curl_setopt_array($curl, array(
1710 CURLOPT_POST => true,
1711 CURLOPT_POSTFIELDS => json_encode($data),
1712 ));
1713
1714 //create the Folder in the Parent
1715 $result = elFinder::curlExec($curl);
1716 $folder = json_decode($result);
1717
1718 return $this->_joinPath($path, $folder->id);
1719 } catch (Exception $e) {
1720 return $this->setError('OneDrive error: ' . $e->getMessage());
1721 }
1722 }
1723
1724 /**
1725 * Create file and return it's path or false on failed.
1726 *
1727 * @param string $path parent dir path
1728 * @param string $name new file name
1729 *
1730 * @return string|bool
1731 * @author Dmitry (dio) Levashov
1732 **/
1733 protected function _mkfile($path, $name)
1734 {
1735 return $this->_save($this->tmpfile(), $path, $name, array());
1736 }
1737
1738 /**
1739 * Create symlink. FTP driver does not support symlinks.
1740 *
1741 * @param string $target link target
1742 * @param string $path symlink path
1743 *
1744 * @return bool
1745 * @author Dmitry (dio) Levashov
1746 **/
1747 protected function _symlink($target, $path, $name)
1748 {
1749 return false;
1750 }
1751
1752 /**
1753 * Copy file into another file.
1754 *
1755 * @param string $source source file path
1756 * @param string $targetDir target directory path
1757 * @param string $name new file name
1758 *
1759 * @return bool
1760 * @author Dmitry (dio) Levashov
1761 **/
1762 protected function _copy($source, $targetDir, $name)
1763 {
1764 $path = $this->_joinPath($targetDir, $name);
1765
1766 try {
1767 //Set the Parent id
1768 list(, $parentId) = $this->_od_splitPath($targetDir);
1769 list(, $itemId) = $this->_od_splitPath($source);
1770
1771 $url = self::API_URL . $itemId . '/copy';
1772
1773 $properties = array(
1774 'name' => (string)$name,
1775 );
1776 if ($parentId === 'root') {
1777 $properties['parentReference'] = (object)array('path' => '/drive/root:');
1778 } else {
1779 $properties['parentReference'] = (object)array('id' => (string)$parentId);
1780 }
1781 $data = (object)$properties;
1782 $curl = $this->_od_prepareCurl($url);
1783 curl_setopt_array($curl, array(
1784 CURLOPT_POST => true,
1785 CURLOPT_HEADER => true,
1786 CURLOPT_HTTPHEADER => array(
1787 'Content-Type: application/json',
1788 'Authorization: Bearer ' . $this->token->data->access_token,
1789 'Prefer: respond-async',
1790 ),
1791 CURLOPT_POSTFIELDS => json_encode($data),
1792 ));
1793 $result = elFinder::curlExec($curl);
1794
1795 $res = new stdClass();
1796 if (preg_match('/Location: (.+)/', $result, $m)) {
1797 $monUrl = trim($m[1]);
1798 while ($res) {
1799 usleep(200000);
1800 $curl = curl_init($monUrl);
1801 curl_setopt_array($curl, array(
1802 CURLOPT_RETURNTRANSFER => true,
1803 CURLOPT_HTTPHEADER => array(
1804 'Content-Type: application/json',
1805 ),
1806 ));
1807 $res = json_decode(elFinder::curlExec($curl));
1808 if (isset($res->status)) {
1809 if ($res->status === 'completed' || $res->status === 'failed') {
1810 break;
1811 }
1812 } elseif (isset($res->error)) {
1813 return $this->setError('OneDrive error: ' . $res->error->message);
1814 }
1815 }
1816 }
1817
1818 if ($res && isset($res->resourceId)) {
1819 if (isset($res->folder) && isset($this->sessionCache['subdirs'])) {
1820 $this->sessionCache['subdirs'][$targetDir] = true;
1821 }
1822
1823 return $this->_joinPath($targetDir, $res->resourceId);
1824 }
1825
1826 return false;
1827 } catch (Exception $e) {
1828 return $this->setError('OneDrive error: ' . $e->getMessage());
1829 }
1830
1831 return true;
1832 }
1833
1834 /**
1835 * Move file into another parent dir.
1836 * Return new file path or false.
1837 *
1838 * @param string $source source file path
1839 * @param $targetDir
1840 * @param string $name file name
1841 *
1842 * @return string|bool
1843 * @author Dmitry (dio) Levashov
1844 */
1845 protected function _move($source, $targetDir, $name)
1846 {
1847 try {
1848 list(, $targetParentId) = $this->_od_splitPath($targetDir);
1849 list($sourceParentId, $itemId, $srcParent) = $this->_od_splitPath($source);
1850
1851 $properties = array(
1852 'name' => (string)$name,
1853 );
1854 if ($targetParentId !== $sourceParentId) {
1855 $properties['parentReference'] = (object)array('id' => (string)$targetParentId);
1856 }
1857
1858 $url = self::API_URL . $itemId;
1859 $data = (object)$properties;
1860
1861 $curl = $this->_od_prepareCurl($url);
1862
1863 curl_setopt_array($curl, array(
1864 CURLOPT_CUSTOMREQUEST => 'PATCH',
1865 CURLOPT_POSTFIELDS => json_encode($data),
1866 ));
1867
1868 $result = json_decode(elFinder::curlExec($curl));
1869 if ($result && isset($result->id)) {
1870 return $targetDir . '/' . $result->id;
1871 } else {
1872 return false;
1873 }
1874 } catch (Exception $e) {
1875 return $this->setError('OneDrive error: ' . $e->getMessage());
1876 }
1877
1878 return false;
1879 }
1880
1881 /**
1882 * Remove file.
1883 *
1884 * @param string $path file path
1885 *
1886 * @return bool
1887 * @author Dmitry (dio) Levashov
1888 **/
1889 protected function _unlink($path)
1890 {
1891 $stat = $this->stat($path);
1892 try {
1893 list(, $itemId) = $this->_od_splitPath($path);
1894
1895 $url = self::API_URL . $itemId;
1896
1897 $curl = $this->_od_prepareCurl($url);
1898 curl_setopt_array($curl, array(
1899 CURLOPT_CUSTOMREQUEST => 'DELETE',
1900 ));
1901
1902 //unlink or delete File or Folder in the Parent
1903 $result = elFinder::curlExec($curl);
1904 } catch (Exception $e) {
1905 return $this->setError('OneDrive error: ' . $e->getMessage());
1906 }
1907
1908 return true;
1909 }
1910
1911 /**
1912 * Remove dir.
1913 *
1914 * @param string $path dir path
1915 *
1916 * @return bool
1917 * @author Dmitry (dio) Levashov
1918 **/
1919 protected function _rmdir($path)
1920 {
1921 return $this->_unlink($path);
1922 }
1923
1924 /**
1925 * Create new file and write into it from file pointer.
1926 * Return new file path or false on error.
1927 *
1928 * @param resource $fp file pointer
1929 * @param $path
1930 * @param string $name file name
1931 * @param array $stat file stat (required by some virtual fs)
1932 *
1933 * @return bool|string
1934 * @author Dmitry (dio) Levashov
1935 */
1936 protected function _save($fp, $path, $name, $stat)
1937 {
1938 $itemId = '';
1939 $size = null;
1940 if ($name === '') {
1941 list($parentId, $itemId, $parent) = $this->_od_splitPath($path);
1942 } else {
1943 if ($stat) {
1944 if (isset($stat['name'])) {
1945 $name = $stat['name'];
1946 }
1947 if (isset($stat['rev']) && strpos($stat['hash'], $this->id) === 0) {
1948 $itemId = $stat['rev'];
1949 }
1950 }
1951 list(, $parentId) = $this->_od_splitPath($path);
1952 $parent = $path;
1953 }
1954
1955 if ($stat && isset($stat['size'])) {
1956 $size = $stat['size'];
1957 } else {
1958 $stats = fstat($fp);
1959 if (isset($stats[7])) {
1960 $size = $stats[7];
1961 }
1962 }
1963
1964 if ($size > 4194304) {
1965 return $this->_od_uploadSession($fp, $size, $name, $itemId, $parent, $parentId);
1966 }
1967
1968 try {
1969 // for unseekable file pointer
1970 if (!elFinder::isSeekableStream($fp)) {
1971 if ($tfp = tmpfile()) {
1972 if (stream_copy_to_stream($fp, $tfp, $size? $size : -1) !== false) {
1973 rewind($tfp);
1974 $fp = $tfp;
1975 }
1976 }
1977 }
1978
1979 //Create or Update a file
1980 if ($itemId === '') {
1981 $url = self::API_URL . $parentId . ':/' . rawurlencode($name) . ':/content';
1982 } else {
1983 $url = self::API_URL . $itemId . '/content';
1984 }
1985 $curl = $this->_od_prepareCurl();
1986
1987 $options = array(
1988 CURLOPT_URL => $url,
1989 CURLOPT_PUT => true,
1990 CURLOPT_INFILE => $fp,
1991 );
1992 // Size
1993 if ($size !== null) {
1994 $options[CURLOPT_INFILESIZE] = $size;
1995 }
1996
1997 curl_setopt_array($curl, $options);
1998
1999 //create or update File in the Target
2000 $file = json_decode(elFinder::curlExec($curl));
2001
2002 return $this->_joinPath($parent, $file->id);
2003 } catch (Exception $e) {
2004 return $this->setError('OneDrive error: ' . $e->getMessage());
2005 }
2006 }
2007
2008 /**
2009 * Get file contents.
2010 *
2011 * @param string $path file path
2012 *
2013 * @return string|false
2014 * @author Dmitry (dio) Levashov
2015 **/
2016 protected function _getContents($path)
2017 {
2018 $contents = '';
2019
2020 try {
2021 list(, $itemId) = $this->_od_splitPath($path);
2022 $url = self::API_URL . $itemId . '/content';
2023 $contents = $this->_od_createCurl($url, $contents = true);
2024 } catch (Exception $e) {
2025 return $this->setError('OneDrive error: ' . $e->getMessage());
2026 }
2027
2028 return $contents;
2029 }
2030
2031 /**
2032 * Write a string to a file.
2033 *
2034 * @param string $path file path
2035 * @param string $content new file content
2036 *
2037 * @return bool
2038 * @author Dmitry (dio) Levashov
2039 **/
2040 protected function _filePutContents($path, $content)
2041 {
2042 $res = false;
2043
2044 if ($local = $this->getTempFile($path)) {
2045 if (file_put_contents($local, $content, LOCK_EX) !== false
2046 && ($fp = fopen($local, 'rb'))) {
2047 clearstatcache();
2048 $res = $this->_save($fp, $path, '', array());
2049 fclose($fp);
2050 }
2051 file_exists($local) && unlink($local);
2052 }
2053
2054 return $res;
2055 }
2056
2057 /**
2058 * Detect available archivers.
2059 **/
2060 protected function _checkArchivers()
2061 {
2062 // die('Not yet implemented. (_checkArchivers)');
2063 return array();
2064 }
2065
2066 /**
2067 * chmod implementation.
2068 *
2069 * @return bool
2070 **/
2071 protected function _chmod($path, $mode)
2072 {
2073 return false;
2074 }
2075
2076 /**
2077 * Unpack archive.
2078 *
2079 * @param string $path archive path
2080 * @param array $arc archiver command and arguments (same as in $this->archivers)
2081 *
2082 * @return void
2083 * @author Dmitry (dio) Levashov
2084 * @author Alexey Sukhotin
2085 */
2086 protected function _unpack($path, $arc)
2087 {
2088 die('Not yet implemented. (_unpack)');
2089 //return false;
2090 }
2091
2092 /**
2093 * Extract files from archive.
2094 *
2095 * @param string $path archive path
2096 * @param array $arc archiver command and arguments (same as in $this->archivers)
2097 *
2098 * @return void
2099 * @author Dmitry (dio) Levashov,
2100 * @author Alexey Sukhotin
2101 */
2102 protected function _extract($path, $arc)
2103 {
2104 die('Not yet implemented. (_extract)');
2105 }
2106
2107 /**
2108 * Create archive and return its path.
2109 *
2110 * @param string $dir target dir
2111 * @param array $files files names list
2112 * @param string $name archive name
2113 * @param array $arc archiver options
2114 *
2115 * @return string|bool
2116 * @author Dmitry (dio) Levashov,
2117 * @author Alexey Sukhotin
2118 **/
2119 protected function _archive($dir, $files, $name, $arc)
2120 {
2121 die('Not yet implemented. (_archive)');
2122 }
2123 } // END class
2124