elFinderVolumeOneDrive.class.php 66 KB

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