elFinderVolumeOneDrive.class.php 60 KB

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