elFinderVolumeGoogleDrive.class.php 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163
  1. <?php
  2. /**
  3. * Simple elFinder driver for GoogleDrive
  4. * google-api-php-client-2.x or above.
  5. *
  6. * @author Dmitry (dio) Levashov
  7. * @author Cem (discofever)
  8. **/
  9. class elFinderVolumeGoogleDrive 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 = 'gd';
  19. /**
  20. * Google API client object.
  21. *
  22. * @var object
  23. **/
  24. protected $client = null;
  25. /**
  26. * GoogleDrive service object.
  27. *
  28. * @var object
  29. **/
  30. protected $service = null;
  31. /**
  32. * Cache of parents of each directories.
  33. *
  34. * @var array
  35. */
  36. protected $parents = [];
  37. /**
  38. * Cache of chiled directories of each directories.
  39. *
  40. * @var array
  41. */
  42. protected $directories = null;
  43. /**
  44. * Cache of itemID => name of each items.
  45. *
  46. * @var array
  47. */
  48. protected $names = [];
  49. /**
  50. * MIME tyoe of directory.
  51. *
  52. * @var string
  53. */
  54. const DIRMIME = 'application/vnd.google-apps.folder';
  55. /**
  56. * Fetch fields for list.
  57. *
  58. * @var string
  59. */
  60. const FETCHFIELDS_LIST = 'files(id,name,mimeType,modifiedTime,parents,permissions,size,imageMediaMetadata(height,width),thumbnailLink,webContentLink,webViewLink),nextPageToken';
  61. /**
  62. * Fetch fields for get.
  63. *
  64. * @var string
  65. */
  66. const FETCHFIELDS_GET = 'id,name,mimeType,modifiedTime,parents,permissions,size,imageMediaMetadata(height,width),thumbnailLink,webContentLink,webViewLink';
  67. /**
  68. * Directory for tmp files
  69. * If not set driver will try to use tmbDir as tmpDir.
  70. *
  71. * @var string
  72. **/
  73. protected $tmp = '';
  74. /**
  75. * Net mount key.
  76. *
  77. * @var string
  78. **/
  79. public $netMountKey = '';
  80. /**
  81. * Current token expires
  82. *
  83. * @var integer
  84. **/
  85. private $expires;
  86. /**
  87. * Constructor
  88. * Extend options with required fields.
  89. *
  90. * @author Dmitry (dio) Levashov
  91. * @author Cem (DiscoFever)
  92. **/
  93. public function __construct()
  94. {
  95. $opts = [
  96. 'client_id' => '',
  97. 'client_secret' => '',
  98. 'access_token' => [],
  99. 'refresh_token' => '',
  100. 'serviceAccountConfigFile' => '',
  101. 'root' => 'My Drive',
  102. 'gdAlias' => '%s@GDrive',
  103. 'googleApiClient' => '',
  104. 'path' => '/',
  105. 'tmbPath' => '',
  106. 'separator' => '/',
  107. 'useGoogleTmb' => true,
  108. 'acceptedName' => '#.#',
  109. 'rootCssClass' => 'elfinder-navbar-root-googledrive',
  110. 'publishPermission' => [
  111. 'type' => 'anyone',
  112. 'role' => 'reader',
  113. 'withLink' => true,
  114. ],
  115. 'appsExportMap' => [
  116. 'application/vnd.google-apps.document' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  117. 'application/vnd.google-apps.spreadsheet' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  118. 'application/vnd.google-apps.drawing' => 'application/pdf',
  119. 'application/vnd.google-apps.presentation' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  120. 'application/vnd.google-apps.script' => 'application/vnd.google-apps.script+json',
  121. 'default' => 'application/pdf',
  122. ],
  123. ];
  124. $this->options = array_merge($this->options, $opts);
  125. $this->options['mimeDetect'] = 'internal';
  126. }
  127. /*********************************************************************/
  128. /* ORIGINAL FUNCTIONS */
  129. /*********************************************************************/
  130. /**
  131. * Get Parent ID, Item ID, Parent Path as an array from path.
  132. *
  133. * @param string $path
  134. *
  135. * @return array
  136. */
  137. protected function _gd_splitPath($path)
  138. {
  139. $path = trim($path, '/');
  140. $pid = '';
  141. if ($path === '') {
  142. $id = 'root';
  143. $parent = '';
  144. } else {
  145. $path = str_replace('\\/', chr(0), $path);
  146. $paths = explode('/', $path);
  147. $id = array_pop($paths);
  148. $id = str_replace(chr(0), '/', $id);
  149. if ($paths) {
  150. $parent = '/' . implode('/', $paths);
  151. $pid = array_pop($paths);
  152. } else {
  153. $rootid = ($this->root === '/') ? 'root' : trim($this->root, '/');
  154. if ($id === $rootid) {
  155. $parent = '';
  156. } else {
  157. $parent = $this->root;
  158. $pid = $rootid;
  159. }
  160. }
  161. }
  162. return array($pid, $id, $parent);
  163. }
  164. /**
  165. * Drive query and fetchAll.
  166. *
  167. * @param string $sql
  168. *
  169. * @return bool|array
  170. */
  171. private function _gd_query($opts)
  172. {
  173. $result = [];
  174. $pageToken = null;
  175. $parameters = [
  176. 'fields' => self::FETCHFIELDS_LIST,
  177. 'pageSize' => 1000,
  178. 'spaces' => 'drive',
  179. ];
  180. if (is_array($opts)) {
  181. $parameters = array_merge($parameters, $opts);
  182. }
  183. do {
  184. try {
  185. if ($pageToken) {
  186. $parameters['pageToken'] = $pageToken;
  187. }
  188. $files = $this->service->files->listFiles($parameters);
  189. $result = array_merge($result, $files->getFiles());
  190. $pageToken = $files->getNextPageToken();
  191. } catch (Exception $e) {
  192. $pageToken = null;
  193. }
  194. } while ($pageToken);
  195. return $result;
  196. }
  197. /**
  198. * Get dat(googledrive metadata) from GoogleDrive.
  199. *
  200. * @param string $path
  201. *
  202. * @return array googledrive metadata
  203. */
  204. private function _gd_getFile($path, $fields = '')
  205. {
  206. list(, $itemId) = $this->_gd_splitPath($path);
  207. if (!$fields) {
  208. $fields = self::FETCHFIELDS_GET;
  209. }
  210. try {
  211. $file = $this->service->files->get($itemId, ['fields' => $fields]);
  212. if ($file instanceof Google_Service_Drive_DriveFile) {
  213. return $file;
  214. } else {
  215. return [];
  216. }
  217. } catch (Exception $e) {
  218. return [];
  219. }
  220. }
  221. /**
  222. * Parse line from googledrive metadata output and return file stat (array).
  223. *
  224. * @param array $raw line from ftp_rawlist() output
  225. *
  226. * @return array
  227. * @author Dmitry Levashov
  228. **/
  229. protected function _gd_parseRaw($raw)
  230. {
  231. $stat = [];
  232. $stat['iid'] = isset($raw['id']) ? $raw['id'] : 'root';
  233. $stat['name'] = isset($raw['name']) ? $raw['name'] : '';
  234. if (isset($raw['modifiedTime'])) {
  235. $stat['ts'] = strtotime($raw['modifiedTime']);
  236. }
  237. if ($raw['mimeType'] === self::DIRMIME) {
  238. $stat['mime'] = 'directory';
  239. $stat['size'] = 0;
  240. } else {
  241. $stat['mime'] = $raw['mimeType'] == 'image/bmp' ? 'image/x-ms-bmp' : $raw['mimeType'];
  242. $stat['size'] = (int)$raw['size'];
  243. if ($size = $raw->getImageMediaMetadata()) {
  244. $stat['width'] = $size['width'];
  245. $stat['height'] = $size['height'];
  246. }
  247. $published = $this->_gd_isPublished($raw);
  248. if ($this->options['useGoogleTmb']) {
  249. if (isset($raw['thumbnailLink'])) {
  250. if ($published) {
  251. $stat['tmb'] = 'drive.google.com/thumbnail?authuser=0&sz=s' . $this->options['tmbSize'] . '&id=' . $raw['id'];
  252. } else {
  253. $stat['tmb'] = substr($raw['thumbnailLink'], 8); // remove "https://"
  254. }
  255. } else {
  256. $stat['tmb'] = '';
  257. }
  258. }
  259. if ($published) {
  260. $stat['url'] = $this->_gd_getLink($raw);
  261. } elseif (!$this->disabledGetUrl) {
  262. $stat['url'] = '1';
  263. }
  264. }
  265. return $stat;
  266. }
  267. /**
  268. * Get dat(googledrive metadata) from GoogleDrive.
  269. *
  270. * @param string $path
  271. *
  272. * @return array googledrive metadata
  273. */
  274. private function _gd_getNameByPath($path)
  275. {
  276. list(, $itemId) = $this->_gd_splitPath($path);
  277. if (!$this->names) {
  278. $this->_gd_getDirectoryData();
  279. }
  280. return isset($this->names[$itemId]) ? $this->names[$itemId] : '';
  281. }
  282. /**
  283. * Make cache of $parents, $names and $directories.
  284. *
  285. * @param bool $usecache
  286. */
  287. protected function _gd_getDirectoryData($usecache = true)
  288. {
  289. if ($usecache) {
  290. $cache = $this->session->get($this->id . $this->netMountKey, []);
  291. if ($cache) {
  292. $this->parents = $cache['parents'];
  293. $this->names = $cache['names'];
  294. $this->directories = $cache['directories'];
  295. return;
  296. }
  297. }
  298. $root = '';
  299. if ($this->root === '/') {
  300. // get root id
  301. if ($res = $this->_gd_getFile('/', 'id')) {
  302. $root = $res->getId();
  303. }
  304. }
  305. $data = [];
  306. $opts = [
  307. 'fields' => 'files(id, name, parents)',
  308. 'q' => sprintf('trashed=false and mimeType="%s"', self::DIRMIME),
  309. ];
  310. $res = $this->_gd_query($opts);
  311. foreach ($res as $raw) {
  312. if ($parents = $raw->getParents()) {
  313. $id = $raw->getId();
  314. $this->parents[$id] = $parents;
  315. $this->names[$id] = $raw->getName();
  316. foreach ($parents as $p) {
  317. if (isset($data[$p])) {
  318. $data[$p][] = $id;
  319. } else {
  320. $data[$p] = [$id];
  321. }
  322. }
  323. }
  324. }
  325. if ($root && isset($data[$root])) {
  326. $data['root'] = $data[$root];
  327. }
  328. $this->directories = $data;
  329. $this->session->set($this->id . $this->netMountKey, [
  330. 'parents' => $this->parents,
  331. 'names' => $this->names,
  332. 'directories' => $this->directories,
  333. ]);
  334. }
  335. /**
  336. * Get descendants directories.
  337. *
  338. * @param string $itemId
  339. *
  340. * @return array
  341. */
  342. protected function _gd_getDirectories($itemId)
  343. {
  344. $ret = [];
  345. if ($this->directories === null) {
  346. $this->_gd_getDirectoryData();
  347. }
  348. $data = $this->directories;
  349. if (isset($data[$itemId])) {
  350. $ret = $data[$itemId];
  351. foreach ($data[$itemId] as $cid) {
  352. $ret = array_merge($ret, $this->_gd_getDirectories($cid));
  353. }
  354. }
  355. return $ret;
  356. }
  357. /**
  358. * Get ID based path from item ID.
  359. *
  360. * @param string $id
  361. *
  362. * @return array
  363. */
  364. protected function _gd_getMountPaths($id)
  365. {
  366. $root = false;
  367. if ($this->directories === null) {
  368. $this->_gd_getDirectoryData();
  369. }
  370. list($pid) = explode('/', $id, 2);
  371. $path = $id;
  372. if ('/' . $pid === $this->root) {
  373. $root = true;
  374. } elseif (!isset($this->parents[$pid])) {
  375. $root = true;
  376. $path = ltrim(substr($path, strlen($pid)), '/');
  377. }
  378. $res = [];
  379. if ($root) {
  380. if ($this->root === '/' || strpos('/' . $path, $this->root) === 0) {
  381. $res = [(strpos($path, '/') === false) ? '/' : ('/' . $path)];
  382. }
  383. } else {
  384. foreach ($this->parents[$pid] as $p) {
  385. $_p = $p . '/' . $path;
  386. $res = array_merge($res, $this->_gd_getMountPaths($_p));
  387. }
  388. }
  389. return $res;
  390. }
  391. /**
  392. * Return is published.
  393. *
  394. * @param object $file
  395. *
  396. * @return bool
  397. */
  398. protected function _gd_isPublished($file)
  399. {
  400. $res = false;
  401. $pType = $this->options['publishPermission']['type'];
  402. $pRole = $this->options['publishPermission']['role'];
  403. if ($permissions = $file->getPermissions()) {
  404. foreach ($permissions as $permission) {
  405. if ($permission->type === $pType && $permission->role === $pRole) {
  406. $res = true;
  407. break;
  408. }
  409. }
  410. }
  411. return $res;
  412. }
  413. /**
  414. * return item URL link.
  415. *
  416. * @param object $file
  417. *
  418. * @return string
  419. */
  420. protected function _gd_getLink($file)
  421. {
  422. if (strpos($file->mimeType, 'application/vnd.google-apps.') !== 0) {
  423. if ($url = $file->getWebContentLink()) {
  424. return str_replace('export=download', 'export=media', $url);
  425. }
  426. }
  427. if ($url = $file->getWebViewLink()) {
  428. return $url;
  429. }
  430. return '';
  431. }
  432. /**
  433. * Get download url.
  434. *
  435. * @param Google_Service_Drive_DriveFile $file
  436. *
  437. * @return string|false
  438. */
  439. protected function _gd_getDownloadUrl($file)
  440. {
  441. if (strpos($file->mimeType, 'application/vnd.google-apps.') !== 0) {
  442. return 'https://www.googleapis.com/drive/v3/files/' . $file->getId() . '?alt=media';
  443. } else {
  444. $mimeMap = $this->options['appsExportMap'];
  445. if (isset($mimeMap[$file->getMimeType()])) {
  446. $mime = $mimeMap[$file->getMimeType()];
  447. } else {
  448. $mime = $mimeMap['default'];
  449. }
  450. $mime = rawurlencode($mime);
  451. return 'https://www.googleapis.com/drive/v3/files/' . $file->getId() . '/export?mimeType=' . $mime;
  452. }
  453. return false;
  454. }
  455. /**
  456. * Get thumbnail from GoogleDrive.com.
  457. *
  458. * @param string $path
  459. *
  460. * @return string | boolean
  461. */
  462. protected function _gd_getThumbnail($path)
  463. {
  464. list(, $itemId) = $this->_gd_splitPath($path);
  465. try {
  466. $contents = $this->service->files->get($itemId, [
  467. 'alt' => 'media',
  468. ]);
  469. $contents = $contents->getBody()->detach();
  470. rewind($contents);
  471. return $contents;
  472. } catch (Exception $e) {
  473. return false;
  474. }
  475. }
  476. /**
  477. * Publish permissions specified path item.
  478. *
  479. * @param string $path
  480. *
  481. * @return bool
  482. */
  483. protected function _gd_publish($path)
  484. {
  485. if ($file = $this->_gd_getFile($path)) {
  486. if ($this->_gd_isPublished($file)) {
  487. return true;
  488. }
  489. try {
  490. if ($this->service->permissions->create($file->getId(), new \Google_Service_Drive_Permission($this->options['publishPermission']))) {
  491. return true;
  492. }
  493. } catch (Exception $e) {
  494. return false;
  495. }
  496. }
  497. return false;
  498. }
  499. /**
  500. * unPublish permissions specified path.
  501. *
  502. * @param string $path
  503. *
  504. * @return bool
  505. */
  506. protected function _gd_unPublish($path)
  507. {
  508. if ($file = $this->_gd_getFile($path)) {
  509. if (!$this->_gd_isPublished($file)) {
  510. return true;
  511. }
  512. $permissions = $file->getPermissions();
  513. $pType = $this->options['publishPermission']['type'];
  514. $pRole = $this->options['publishPermission']['role'];
  515. try {
  516. foreach ($permissions as $permission) {
  517. if ($permission->type === $pType && $permission->role === $pRole) {
  518. $this->service->permissions->delete($file->getId(), $permission->getId());
  519. return true;
  520. break;
  521. }
  522. }
  523. } catch (Exception $e) {
  524. return false;
  525. }
  526. }
  527. return false;
  528. }
  529. /**
  530. * Read file chunk.
  531. *
  532. * @param resource $handle
  533. * @param int $chunkSize
  534. *
  535. * @return string
  536. */
  537. protected function _gd_readFileChunk($handle, $chunkSize)
  538. {
  539. $byteCount = 0;
  540. $giantChunk = '';
  541. while (!feof($handle)) {
  542. // fread will never return more than 8192 bytes if the stream is read buffered and it does not represent a plain file
  543. $chunk = fread($handle, 8192);
  544. $byteCount += strlen($chunk);
  545. $giantChunk .= $chunk;
  546. if ($byteCount >= $chunkSize) {
  547. return $giantChunk;
  548. }
  549. }
  550. return $giantChunk;
  551. }
  552. /*********************************************************************/
  553. /* EXTENDED FUNCTIONS */
  554. /*********************************************************************/
  555. /**
  556. * Prepare
  557. * Call from elFinder::netmout() before volume->mount().
  558. *
  559. * @return array
  560. * @author Naoki Sawada
  561. * @author Raja Sharma updating for GoogleDrive
  562. **/
  563. public function netmountPrepare($options)
  564. {
  565. if (empty($options['client_id']) && defined('ELFINDER_GOOGLEDRIVE_CLIENTID')) {
  566. $options['client_id'] = ELFINDER_GOOGLEDRIVE_CLIENTID;
  567. }
  568. if (empty($options['client_secret']) && defined('ELFINDER_GOOGLEDRIVE_CLIENTSECRET')) {
  569. $options['client_secret'] = ELFINDER_GOOGLEDRIVE_CLIENTSECRET;
  570. }
  571. if (empty($options['googleApiClient']) && defined('ELFINDER_GOOGLEDRIVE_GOOGLEAPICLIENT')) {
  572. $options['googleApiClient'] = ELFINDER_GOOGLEDRIVE_GOOGLEAPICLIENT;
  573. include_once $options['googleApiClient'];
  574. }
  575. if (!isset($options['pass'])) {
  576. $options['pass'] = '';
  577. }
  578. try {
  579. $client = new \Google_Client();
  580. $client->setClientId($options['client_id']);
  581. $client->setClientSecret($options['client_secret']);
  582. if ($options['pass'] === 'reauth') {
  583. $options['pass'] = '';
  584. $this->session->set('GoogleDriveAuthParams', [])->set('GoogleDriveTokens', []);
  585. } elseif ($options['pass'] === 'googledrive') {
  586. $options['pass'] = '';
  587. }
  588. $options = array_merge($this->session->get('GoogleDriveAuthParams', []), $options);
  589. if (!isset($options['access_token'])) {
  590. $options['access_token'] = $this->session->get('GoogleDriveTokens', []);
  591. $this->session->remove('GoogleDriveTokens');
  592. }
  593. $aToken = $options['access_token'];
  594. $rootObj = $service = null;
  595. if ($aToken) {
  596. try {
  597. $client->setAccessToken($aToken);
  598. if ($client->isAccessTokenExpired()) {
  599. $aToken = array_merge($aToken, $client->fetchAccessTokenWithRefreshToken());
  600. $client->setAccessToken($aToken);
  601. }
  602. $service = new \Google_Service_Drive($client);
  603. $rootObj = $service->files->get('root');
  604. $options['access_token'] = $aToken;
  605. $this->session->set('GoogleDriveAuthParams', $options);
  606. } catch (Exception $e) {
  607. $aToken = [];
  608. $options['access_token'] = [];
  609. if ($options['user'] !== 'init') {
  610. $this->session->set('GoogleDriveAuthParams', $options);
  611. return ['exit' => true, 'error' => elFinder::ERROR_REAUTH_REQUIRE];
  612. }
  613. }
  614. }
  615. $itpCare = isset($options['code']);
  616. $code = $itpCare? $options['code'] : (isset($_GET['code'])? $_GET['code'] : '');
  617. if ($code || (isset($options['user']) && $options['user'] === 'init')) {
  618. if (empty($options['url'])) {
  619. $options['url'] = elFinder::getConnectorUrl();
  620. }
  621. if (isset($options['id'])) {
  622. $callback = $options['url']
  623. . (strpos($options['url'], '?') !== false? '&' : '?') . 'cmd=netmount&protocol=googledrive&host=' . ($options['id'] === 'elfinder'? '1' : $options['id']);
  624. $client->setRedirectUri($callback);
  625. }
  626. if (!$aToken && empty($code)) {
  627. $client->setScopes([Google_Service_Drive::DRIVE]);
  628. if (!empty($options['offline'])) {
  629. $client->setApprovalPrompt('force');
  630. $client->setAccessType('offline');
  631. }
  632. $url = $client->createAuthUrl();
  633. $html = '<input id="elf-volumedriver-googledrive-host-btn" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" value="{msg:btnApprove}" type="button">';
  634. $html .= '<script>
  635. $("#' . $options['id'] . '").elfinder("instance").trigger("netmount", {protocol: "googledrive", mode: "makebtn", url: "' . $url . '"});
  636. </script>';
  637. if (empty($options['pass']) && $options['host'] !== '1') {
  638. $options['pass'] = 'return';
  639. $this->session->set('GoogleDriveAuthParams', $options);
  640. return ['exit' => true, 'body' => $html];
  641. } else {
  642. $out = [
  643. 'node' => $options['id'],
  644. 'json' => '{"protocol": "googledrive", "mode": "makebtn", "body" : "' . str_replace($html, '"', '\\"') . '", "error" : "' . elFinder::ERROR_ACCESS_DENIED . '"}',
  645. 'bind' => 'netmount',
  646. ];
  647. return ['exit' => 'callback', 'out' => $out];
  648. }
  649. } else {
  650. if ($code) {
  651. if (!empty($options['id'])) {
  652. $aToken = $client->fetchAccessTokenWithAuthCode($code);
  653. $options['access_token'] = $aToken;
  654. unset($options['code']);
  655. $this->session->set('GoogleDriveTokens', $aToken)->set('GoogleDriveAuthParams', $options);
  656. $out = [
  657. 'node' => $options['id'],
  658. 'json' => '{"protocol": "googledrive", "mode": "done", "reset": 1}',
  659. 'bind' => 'netmount',
  660. ];
  661. } else {
  662. $nodeid = ($_GET['host'] === '1')? 'elfinder' : $_GET['host'];
  663. $out = array(
  664. 'node' => $nodeid,
  665. 'json' => json_encode(array(
  666. 'protocol' => 'googledrive',
  667. 'host' => $nodeid,
  668. 'mode' => 'redirect',
  669. 'options' => array(
  670. 'id' => $nodeid,
  671. 'code'=> $code
  672. )
  673. )),
  674. 'bind' => 'netmount'
  675. );
  676. }
  677. if (!$itpCare) {
  678. return array('exit' => 'callback', 'out' => $out);
  679. } else {
  680. return array('exit' => true, 'body' => $out['json']);
  681. }
  682. }
  683. $path = $options['path'];
  684. if ($path === '/') {
  685. $path = 'root';
  686. }
  687. $folders = [];
  688. foreach ($service->files->listFiles([
  689. 'pageSize' => 1000,
  690. 'q' => sprintf('trashed = false and "%s" in parents and mimeType = "application/vnd.google-apps.folder"', $path),
  691. ]) as $f) {
  692. $folders[$f->getId()] = $f->getName();
  693. }
  694. natcasesort($folders);
  695. if ($options['pass'] === 'folders') {
  696. return ['exit' => true, 'folders' => $folders];
  697. }
  698. $folders = ['root' => $rootObj->getName()] + $folders;
  699. $folders = json_encode($folders);
  700. $expires = empty($aToken['refresh_token']) ? $aToken['created'] + $aToken['expires_in'] - 30 : 0;
  701. $mnt2res = empty($aToken['refresh_token']) ? '' : ', "mnt2res": 1';
  702. $json = '{"protocol": "googledrive", "mode": "done", "folders": ' . $folders . ', "expires": ' . $expires . $mnt2res . '}';
  703. $options['pass'] = 'return';
  704. $html = 'Google.com';
  705. $html .= '<script>
  706. $("#' . $options['id'] . '").elfinder("instance").trigger("netmount", ' . $json . ');
  707. </script>';
  708. $this->session->set('GoogleDriveAuthParams', $options);
  709. return ['exit' => true, 'body' => $html];
  710. }
  711. }
  712. } catch (Exception $e) {
  713. $this->session->remove('GoogleDriveAuthParams')->remove('GoogleDriveTokens');
  714. if (empty($options['pass'])) {
  715. return ['exit' => true, 'body' => '{msg:' . elFinder::ERROR_ACCESS_DENIED . '}' . ' ' . $e->getMessage()];
  716. } else {
  717. return ['exit' => true, 'error' => [elFinder::ERROR_ACCESS_DENIED, $e->getMessage()]];
  718. }
  719. }
  720. if (!$aToken) {
  721. return ['exit' => true, 'error' => elFinder::ERROR_REAUTH_REQUIRE];
  722. }
  723. if ($options['path'] === '/') {
  724. $options['path'] = 'root';
  725. }
  726. try {
  727. $file = $service->files->get($options['path']);
  728. $options['alias'] = sprintf($this->options['gdAlias'], $file->getName());
  729. } catch (Google_Service_Exception $e) {
  730. $err = json_decode($e->getMessage(), true);
  731. if (isset($err['error']) && $err['error']['code'] == 404) {
  732. return ['exit' => true, 'error' => [elFinder::ERROR_TRGDIR_NOT_FOUND, $options['path']]];
  733. } else {
  734. return ['exit' => true, 'error' => $e->getMessage()];
  735. }
  736. } catch (Exception $e) {
  737. return ['exit' => true, 'error' => $e->getMessage()];
  738. }
  739. foreach (['host', 'user', 'pass', 'id', 'offline'] as $key) {
  740. unset($options[$key]);
  741. }
  742. return $options;
  743. }
  744. /**
  745. * process of on netunmount
  746. * Drop `googledrive` & rm thumbs.
  747. *
  748. * @param $netVolumes
  749. * @param $key
  750. *
  751. * @return bool
  752. */
  753. public function netunmount($netVolumes, $key)
  754. {
  755. if (!$this->options['useGoogleTmb']) {
  756. if ($tmbs = glob(rtrim($this->options['tmbPath'], '\\/') . DIRECTORY_SEPARATOR . $this->netMountKey . '*.png')) {
  757. foreach ($tmbs as $file) {
  758. unlink($file);
  759. }
  760. }
  761. }
  762. $this->session->remove($this->id . $this->netMountKey);
  763. return true;
  764. }
  765. /**
  766. * Return fileinfo based on filename
  767. * For item ID based path file system
  768. * Please override if needed on each drivers.
  769. *
  770. * @param string $path file cache
  771. *
  772. * @return array
  773. */
  774. protected function isNameExists($path)
  775. {
  776. list($parentId, $name) = $this->_gd_splitPath($path);
  777. $opts = [
  778. 'q' => sprintf('trashed=false and "%s" in parents and name="%s"', $parentId, $name),
  779. 'fields' => self::FETCHFIELDS_LIST,
  780. ];
  781. $srcFile = $this->_gd_query($opts);
  782. return empty($srcFile) ? false : $this->_gd_parseRaw($srcFile[0]);
  783. }
  784. /*********************************************************************/
  785. /* INIT AND CONFIGURE */
  786. /*********************************************************************/
  787. /**
  788. * Prepare FTP connection
  789. * Connect to remote server and check if credentials are correct, if so, store the connection id in $ftp_conn.
  790. *
  791. * @return bool
  792. * @author Dmitry (dio) Levashov
  793. * @author Cem (DiscoFever)
  794. **/
  795. protected function init()
  796. {
  797. $serviceAccountConfig = '';
  798. if (empty($this->options['serviceAccountConfigFile'])) {
  799. if (empty($options['client_id'])) {
  800. if (defined('ELFINDER_GOOGLEDRIVE_CLIENTID') && ELFINDER_GOOGLEDRIVE_CLIENTID) {
  801. $this->options['client_id'] = ELFINDER_GOOGLEDRIVE_CLIENTID;
  802. } else {
  803. return $this->setError('Required option "client_id" is undefined.');
  804. }
  805. }
  806. if (empty($options['client_secret'])) {
  807. if (defined('ELFINDER_GOOGLEDRIVE_CLIENTSECRET') && ELFINDER_GOOGLEDRIVE_CLIENTSECRET) {
  808. $this->options['client_secret'] = ELFINDER_GOOGLEDRIVE_CLIENTSECRET;
  809. } else {
  810. return $this->setError('Required option "client_secret" is undefined.');
  811. }
  812. }
  813. if (!$this->options['access_token'] && !$this->options['refresh_token']) {
  814. return $this->setError('Required option "access_token" or "refresh_token" is undefined.');
  815. }
  816. } else {
  817. if (!is_readable($this->options['serviceAccountConfigFile'])) {
  818. return $this->setError('Option "serviceAccountConfigFile" file is not readable.');
  819. }
  820. $serviceAccountConfig = $this->options['serviceAccountConfigFile'];
  821. }
  822. try {
  823. if (!$serviceAccountConfig) {
  824. $aTokenFile = '';
  825. if ($this->options['refresh_token']) {
  826. // permanent mount
  827. $aToken = $this->options['refresh_token'];
  828. $this->options['access_token'] = '';
  829. $tmp = elFinder::getStaticVar('commonTempPath');
  830. if (!$tmp) {
  831. $tmp = $this->getTempPath();
  832. }
  833. if ($tmp) {
  834. $aTokenFile = $tmp . DIRECTORY_SEPARATOR . md5($this->options['client_id'] . $this->options['refresh_token']) . '.gtoken';
  835. if (is_file($aTokenFile)) {
  836. $this->options['access_token'] = json_decode(file_get_contents($aTokenFile), true);
  837. }
  838. }
  839. } else {
  840. // make net mount key for network mount
  841. if (is_array($this->options['access_token'])) {
  842. $aToken = !empty($this->options['access_token']['refresh_token'])
  843. ? $this->options['access_token']['refresh_token']
  844. : $this->options['access_token']['access_token'];
  845. } else {
  846. return $this->setError('Required option "access_token" is not Array or empty.');
  847. }
  848. }
  849. }
  850. $errors = [];
  851. if ($this->needOnline && !$this->service) {
  852. if (($this->options['googleApiClient'] || defined('ELFINDER_GOOGLEDRIVE_GOOGLEAPICLIENT')) && !class_exists('Google_Client')) {
  853. include_once $this->options['googleApiClient'] ? $this->options['googleApiClient'] : ELFINDER_GOOGLEDRIVE_GOOGLEAPICLIENT;
  854. }
  855. if (!class_exists('Google_Client')) {
  856. return $this->setError('Class Google_Client not found.');
  857. }
  858. $this->client = new \Google_Client();
  859. $client = $this->client;
  860. if (!$serviceAccountConfig) {
  861. if ($this->options['access_token']) {
  862. $client->setAccessToken($this->options['access_token']);
  863. $access_token = $this->options['access_token'];
  864. }
  865. if ($client->isAccessTokenExpired()) {
  866. $client->setClientId($this->options['client_id']);
  867. $client->setClientSecret($this->options['client_secret']);
  868. $access_token = $client->fetchAccessTokenWithRefreshToken($this->options['refresh_token'] ?: null);
  869. $client->setAccessToken($access_token);
  870. if ($aTokenFile) {
  871. file_put_contents($aTokenFile, json_encode($access_token));
  872. } else {
  873. $access_token['refresh_token'] = $this->options['access_token']['refresh_token'];
  874. }
  875. if (!empty($this->options['netkey'])) {
  876. elFinder::$instance->updateNetVolumeOption($this->options['netkey'], 'access_token', $access_token);
  877. }
  878. $this->options['access_token'] = $access_token;
  879. }
  880. $this->expires = empty($access_token['refresh_token']) ? $access_token['created'] + $access_token['expires_in'] - 30 : 0;
  881. } else {
  882. $client->setAuthConfigFile($serviceAccountConfig);
  883. $client->setScopes([Google_Service_Drive::DRIVE]);
  884. $aToken = $client->getClientId();
  885. }
  886. $this->service = new \Google_Service_Drive($client);
  887. }
  888. if ($this->needOnline) {
  889. $this->netMountKey = md5($aToken . '-' . $this->options['path']);
  890. }
  891. } catch (InvalidArgumentException $e) {
  892. $errors[] = $e->getMessage();
  893. } catch (Google_Service_Exception $e) {
  894. $errors[] = $e->getMessage();
  895. }
  896. if ($this->needOnline && !$this->service) {
  897. $this->session->remove($this->id . $this->netMountKey);
  898. if ($aTokenFile) {
  899. if (is_file($aTokenFile)) {
  900. unlink($aTokenFile);
  901. }
  902. }
  903. $errors[] = 'Google Drive Service could not be loaded.';
  904. return $this->setError($errors);
  905. }
  906. // normalize root path
  907. if ($this->options['path'] == 'root') {
  908. $this->options['path'] = '/';
  909. }
  910. $this->root = $this->options['path'] = $this->_normpath($this->options['path']);
  911. if (empty($this->options['alias'])) {
  912. if ($this->needOnline) {
  913. $this->options['root'] = ($this->options['root'] === '')? $this->_gd_getNameByPath('root') : $this->options['root'];
  914. $this->options['alias'] = ($this->options['path'] === '/') ? $this->options['root'] : sprintf($this->options['gdAlias'], $this->_gd_getNameByPath($this->options['path']));
  915. if (!empty($this->options['netkey'])) {
  916. elFinder::$instance->updateNetVolumeOption($this->options['netkey'], 'alias', $this->options['alias']);
  917. }
  918. } else {
  919. $this->options['root'] = ($this->options['root'] === '')? 'GoogleDrive' : $this->options['root'];
  920. $this->options['alias'] = $this->options['root'];
  921. }
  922. }
  923. $this->rootName = isset($this->options['alias'])? $this->options['alias'] : 'GoogleDrive';
  924. if (!empty($this->options['tmpPath'])) {
  925. if ((is_dir($this->options['tmpPath']) || mkdir($this->options['tmpPath'])) && is_writable($this->options['tmpPath'])) {
  926. $this->tmp = $this->options['tmpPath'];
  927. }
  928. }
  929. if (!$this->tmp && ($tmp = elFinder::getStaticVar('commonTempPath'))) {
  930. $this->tmp = $tmp;
  931. }
  932. // This driver dose not support `syncChkAsTs`
  933. $this->options['syncChkAsTs'] = false;
  934. // 'lsPlSleep' minmum 10 sec
  935. $this->options['lsPlSleep'] = max(10, $this->options['lsPlSleep']);
  936. if ($this->options['useGoogleTmb']) {
  937. $this->options['tmbURL'] = 'https://';
  938. $this->options['tmbPath'] = '';
  939. }
  940. // enable command archive
  941. $this->options['useRemoteArchive'] = true;
  942. return true;
  943. }
  944. /**
  945. * Configure after successfull mount.
  946. *
  947. * @author Dmitry (dio) Levashov
  948. **/
  949. protected function configure()
  950. {
  951. parent::configure();
  952. // fallback of $this->tmp
  953. if (!$this->tmp && $this->tmbPathWritable) {
  954. $this->tmp = $this->tmbPath;
  955. }
  956. if ($this->needOnline && $this->isMyReload()) {
  957. $this->_gd_getDirectoryData(false);
  958. }
  959. }
  960. /*********************************************************************/
  961. /* FS API */
  962. /*********************************************************************/
  963. /**
  964. * Close opened connection.
  965. *
  966. * @author Dmitry (dio) Levashov
  967. **/
  968. public function umount()
  969. {
  970. }
  971. /**
  972. * Cache dir contents.
  973. *
  974. * @param string $path dir path
  975. *
  976. * @return array
  977. * @author Dmitry Levashov
  978. */
  979. protected function cacheDir($path)
  980. {
  981. $this->dirsCache[$path] = [];
  982. $hasDir = false;
  983. list(, $pid) = $this->_gd_splitPath($path);
  984. $opts = [
  985. 'fields' => self::FETCHFIELDS_LIST,
  986. 'q' => sprintf('trashed=false and "%s" in parents', $pid),
  987. ];
  988. $res = $this->_gd_query($opts);
  989. $mountPath = $this->_normpath($path . '/');
  990. if ($res) {
  991. foreach ($res as $raw) {
  992. if ($stat = $this->_gd_parseRaw($raw)) {
  993. $stat = $this->updateCache($mountPath . $raw->id, $stat);
  994. if (empty($stat['hidden']) && $path !== $mountPath . $raw->id) {
  995. if (!$hasDir && $stat['mime'] === 'directory') {
  996. $hasDir = true;
  997. }
  998. $this->dirsCache[$path][] = $mountPath . $raw->id;
  999. }
  1000. }
  1001. }
  1002. }
  1003. if (isset($this->sessionCache['subdirs'])) {
  1004. $this->sessionCache['subdirs'][$path] = $hasDir;
  1005. }
  1006. return $this->dirsCache[$path];
  1007. }
  1008. /**
  1009. * Recursive files search.
  1010. *
  1011. * @param string $path dir path
  1012. * @param string $q search string
  1013. * @param array $mimes
  1014. *
  1015. * @return array
  1016. * @throws elFinderAbortException
  1017. * @author Naoki Sawada
  1018. */
  1019. protected function doSearch($path, $q, $mimes)
  1020. {
  1021. if (!empty($this->doSearchCurrentQuery['matchMethod'])) {
  1022. // has custom match method use elFinderVolumeDriver::doSearch()
  1023. return parent::doSearch($path, $q, $mimes);
  1024. }
  1025. list(, $itemId) = $this->_gd_splitPath($path);
  1026. $path = $this->_normpath($path . '/');
  1027. $result = [];
  1028. $query = '';
  1029. if ($itemId !== 'root') {
  1030. $dirs = array_merge([$itemId], $this->_gd_getDirectories($itemId));
  1031. $query = '(\'' . implode('\' in parents or \'', $dirs) . '\' in parents)';
  1032. }
  1033. $tmp = [];
  1034. if (!$mimes) {
  1035. foreach (explode(' ', $q) as $_v) {
  1036. $tmp[] = 'fullText contains \'' . str_replace('\'', '\\\'', $_v) . '\'';
  1037. }
  1038. $query .= ($query ? ' and ' : '') . implode(' and ', $tmp);
  1039. } else {
  1040. foreach ($mimes as $_v) {
  1041. $tmp[] = 'mimeType contains \'' . str_replace('\'', '\\\'', $_v) . '\'';
  1042. }
  1043. $query .= ($query ? ' and ' : '') . '(' . implode(' or ', $tmp) . ')';
  1044. }
  1045. $opts = [
  1046. 'q' => sprintf('trashed=false and (%s)', $query),
  1047. ];
  1048. $res = $this->_gd_query($opts);
  1049. $timeout = $this->options['searchTimeout'] ? $this->searchStart + $this->options['searchTimeout'] : 0;
  1050. foreach ($res as $raw) {
  1051. if ($timeout && $timeout < time()) {
  1052. $this->setError(elFinder::ERROR_SEARCH_TIMEOUT, $this->_path($path));
  1053. break;
  1054. }
  1055. if ($stat = $this->_gd_parseRaw($raw)) {
  1056. if ($parents = $raw->getParents()) {
  1057. foreach ($parents as $parent) {
  1058. $paths = $this->_gd_getMountPaths($parent);
  1059. foreach ($paths as $path) {
  1060. $path = ($path === '') ? '/' : (rtrim($path, '/') . '/');
  1061. if (!isset($this->cache[$path . $raw->id])) {
  1062. $stat = $this->updateCache($path . $raw->id, $stat);
  1063. } else {
  1064. $stat = $this->cache[$path . $raw->id];
  1065. }
  1066. if (empty($stat['hidden'])) {
  1067. $stat['path'] = $this->_path($path) . $stat['name'];
  1068. $result[] = $stat;
  1069. }
  1070. }
  1071. }
  1072. }
  1073. }
  1074. }
  1075. return $result;
  1076. }
  1077. /**
  1078. * Copy file/recursive copy dir only in current volume.
  1079. * Return new file path or false.
  1080. *
  1081. * @param string $src source path
  1082. * @param string $dst destination dir path
  1083. * @param string $name new file name (optionaly)
  1084. *
  1085. * @return string|false
  1086. * @author Dmitry (dio) Levashov
  1087. * @author Naoki Sawada
  1088. **/
  1089. protected function copy($src, $dst, $name)
  1090. {
  1091. $this->clearcache();
  1092. $res = $this->_gd_getFile($src);
  1093. if ($res['mimeType'] == self::DIRMIME) {
  1094. $newDir = $this->_mkdir($dst, $name);
  1095. if ($newDir) {
  1096. list(, $itemId) = $this->_gd_splitPath($newDir);
  1097. list(, $srcId) = $this->_gd_splitPath($src);
  1098. $path = $this->_joinPath($dst, $itemId);
  1099. $opts = [
  1100. 'q' => sprintf('trashed=false and "%s" in parents', $srcId),
  1101. ];
  1102. $res = $this->_gd_query($opts);
  1103. foreach ($res as $raw) {
  1104. $raw['mimeType'] == self::DIRMIME ? $this->copy($src . '/' . $raw['id'], $path, $raw['name']) : $this->_copy($src . '/' . $raw['id'], $path, $raw['name']);
  1105. }
  1106. $ret = $this->_joinPath($dst, $itemId);
  1107. $this->added[] = $this->stat($ret);
  1108. } else {
  1109. $ret = $this->setError(elFinder::ERROR_COPY, $this->_path($src));
  1110. }
  1111. } else {
  1112. if ($itemId = $this->_copy($src, $dst, $name)) {
  1113. $ret = $this->_joinPath($dst, $itemId);
  1114. $this->added[] = $this->stat($ret);
  1115. } else {
  1116. $ret = $this->setError(elFinder::ERROR_COPY, $this->_path($src));
  1117. }
  1118. }
  1119. return $ret;
  1120. }
  1121. /**
  1122. * Remove file/ recursive remove dir.
  1123. *
  1124. * @param string $path file path
  1125. * @param bool $force try to remove even if file locked
  1126. * @param bool $recursive
  1127. *
  1128. * @return bool
  1129. * @throws elFinderAbortException
  1130. * @author Dmitry (dio) Levashov
  1131. * @author Naoki Sawada
  1132. */
  1133. protected function remove($path, $force = false, $recursive = false)
  1134. {
  1135. $stat = $this->stat($path);
  1136. $stat['realpath'] = $path;
  1137. $this->rmTmb($stat);
  1138. $this->clearcache();
  1139. if (empty($stat)) {
  1140. return $this->setError(elFinder::ERROR_RM, $this->_path($path), elFinder::ERROR_FILE_NOT_FOUND);
  1141. }
  1142. if (!$force && !empty($stat['locked'])) {
  1143. return $this->setError(elFinder::ERROR_LOCKED, $this->_path($path));
  1144. }
  1145. if ($stat['mime'] == 'directory') {
  1146. if (!$recursive && !$this->_rmdir($path)) {
  1147. return $this->setError(elFinder::ERROR_RM, $this->_path($path));
  1148. }
  1149. } else {
  1150. if (!$recursive && !$this->_unlink($path)) {
  1151. return $this->setError(elFinder::ERROR_RM, $this->_path($path));
  1152. }
  1153. }
  1154. $this->removed[] = $stat;
  1155. return true;
  1156. }
  1157. /**
  1158. * Create thumnbnail and return it's URL on success.
  1159. *
  1160. * @param string $path file path
  1161. * @param $stat
  1162. *
  1163. * @return string|false
  1164. * @throws ImagickException
  1165. * @throws elFinderAbortException
  1166. * @author Dmitry (dio) Levashov
  1167. * @author Naoki Sawada
  1168. */
  1169. protected function createTmb($path, $stat)
  1170. {
  1171. if (!$stat || !$this->canCreateTmb($path, $stat)) {
  1172. return false;
  1173. }
  1174. $name = $this->tmbname($stat);
  1175. $tmb = $this->tmbPath . DIRECTORY_SEPARATOR . $name;
  1176. // copy image into tmbPath so some drivers does not store files on local fs
  1177. if (!$data = $this->_gd_getThumbnail($path)) {
  1178. return false;
  1179. }
  1180. if (!file_put_contents($tmb, $data)) {
  1181. return false;
  1182. }
  1183. $result = false;
  1184. $tmbSize = $this->tmbSize;
  1185. if (($s = getimagesize($tmb)) == false) {
  1186. return false;
  1187. }
  1188. /* If image smaller or equal thumbnail size - just fitting to thumbnail square */
  1189. if ($s[0] <= $tmbSize && $s[1] <= $tmbSize) {
  1190. $result = $this->imgSquareFit($tmb, $tmbSize, $tmbSize, 'center', 'middle', $this->options['tmbBgColor'], 'png');
  1191. } else {
  1192. if ($this->options['tmbCrop']) {
  1193. /* Resize and crop if image bigger than thumbnail */
  1194. if (!(($s[0] > $tmbSize && $s[1] <= $tmbSize) || ($s[0] <= $tmbSize && $s[1] > $tmbSize)) || ($s[0] > $tmbSize && $s[1] > $tmbSize)) {
  1195. $result = $this->imgResize($tmb, $tmbSize, $tmbSize, true, false, 'png');
  1196. }
  1197. if (($s = getimagesize($tmb)) != false) {
  1198. $x = $s[0] > $tmbSize ? intval(($s[0] - $tmbSize) / 2) : 0;
  1199. $y = $s[1] > $tmbSize ? intval(($s[1] - $tmbSize) / 2) : 0;
  1200. $result = $this->imgCrop($tmb, $tmbSize, $tmbSize, $x, $y, 'png');
  1201. }
  1202. } else {
  1203. $result = $this->imgResize($tmb, $tmbSize, $tmbSize, true, true, 'png');
  1204. }
  1205. $result = $this->imgSquareFit($tmb, $tmbSize, $tmbSize, 'center', 'middle', $this->options['tmbBgColor'], 'png');
  1206. }
  1207. if (!$result) {
  1208. unlink($tmb);
  1209. return false;
  1210. }
  1211. return $name;
  1212. }
  1213. /**
  1214. * Return thumbnail file name for required file.
  1215. *
  1216. * @param array $stat file stat
  1217. *
  1218. * @return string
  1219. * @author Dmitry (dio) Levashov
  1220. **/
  1221. protected function tmbname($stat)
  1222. {
  1223. return $this->netMountKey . $stat['iid'] . $stat['ts'] . '.png';
  1224. }
  1225. /**
  1226. * Return content URL (for netmout volume driver)
  1227. * If file.url == 1 requests from JavaScript client with XHR.
  1228. *
  1229. * @param string $hash file hash
  1230. * @param array $options options array
  1231. *
  1232. * @return bool|string
  1233. * @author Naoki Sawada
  1234. */
  1235. public function getContentUrl($hash, $options = [])
  1236. {
  1237. if (!empty($options['onetime']) && $this->options['onetimeUrl']) {
  1238. return parent::getContentUrl($hash, $options);
  1239. }
  1240. if (!empty($options['temporary'])) {
  1241. // try make temporary file
  1242. $url = parent::getContentUrl($hash, $options);
  1243. if ($url) {
  1244. return $url;
  1245. }
  1246. }
  1247. if (($file = $this->file($hash)) == false || !$file['url'] || $file['url'] == 1) {
  1248. $path = $this->decode($hash);
  1249. if ($this->_gd_publish($path)) {
  1250. if ($raw = $this->_gd_getFile($path)) {
  1251. return $this->_gd_getLink($raw);
  1252. }
  1253. }
  1254. }
  1255. return false;
  1256. }
  1257. /**
  1258. * Return debug info for client.
  1259. *
  1260. * @return array
  1261. **/
  1262. public function debug()
  1263. {
  1264. $res = parent::debug();
  1265. if (!empty($this->options['netkey']) && empty($this->options['refresh_token']) && $this->options['access_token'] && isset($this->options['access_token']['refresh_token'])) {
  1266. $res['refresh_token'] = $this->options['access_token']['refresh_token'];
  1267. }
  1268. return $res;
  1269. }
  1270. /*********************** paths/urls *************************/
  1271. /**
  1272. * Return parent directory path.
  1273. *
  1274. * @param string $path file path
  1275. *
  1276. * @return string
  1277. * @author Dmitry (dio) Levashov
  1278. **/
  1279. protected function _dirname($path)
  1280. {
  1281. list(, , $parent) = $this->_gd_splitPath($path);
  1282. return $this->_normpath($parent);
  1283. }
  1284. /**
  1285. * Return file name.
  1286. *
  1287. * @param string $path file path
  1288. *
  1289. * @return string
  1290. * @author Dmitry (dio) Levashov
  1291. **/
  1292. protected function _basename($path)
  1293. {
  1294. list(, $basename) = $this->_gd_splitPath($path);
  1295. return $basename;
  1296. }
  1297. /**
  1298. * Join dir name and file name and retur full path.
  1299. *
  1300. * @param string $dir
  1301. * @param string $name
  1302. *
  1303. * @return string
  1304. * @author Dmitry (dio) Levashov
  1305. **/
  1306. protected function _joinPath($dir, $name)
  1307. {
  1308. return $this->_normpath($dir . '/' . str_replace('/', '\\/', $name));
  1309. }
  1310. /**
  1311. * Return normalized path, this works the same as os.path.normpath() in Python.
  1312. *
  1313. * @param string $path path
  1314. *
  1315. * @return string
  1316. * @author Troex Nevelin
  1317. **/
  1318. protected function _normpath($path)
  1319. {
  1320. if (DIRECTORY_SEPARATOR !== '/') {
  1321. $path = str_replace(DIRECTORY_SEPARATOR, '/', $path);
  1322. }
  1323. $path = '/' . ltrim($path, '/');
  1324. return $path;
  1325. }
  1326. /**
  1327. * Return file path related to root dir.
  1328. *
  1329. * @param string $path file path
  1330. *
  1331. * @return string
  1332. * @author Dmitry (dio) Levashov
  1333. **/
  1334. protected function _relpath($path)
  1335. {
  1336. return $path;
  1337. }
  1338. /**
  1339. * Convert path related to root dir into real path.
  1340. *
  1341. * @param string $path file path
  1342. *
  1343. * @return string
  1344. * @author Dmitry (dio) Levashov
  1345. **/
  1346. protected function _abspath($path)
  1347. {
  1348. return $path;
  1349. }
  1350. /**
  1351. * Return fake path started from root dir.
  1352. *
  1353. * @param string $path file path
  1354. *
  1355. * @return string
  1356. * @author Dmitry (dio) Levashov
  1357. **/
  1358. protected function _path($path)
  1359. {
  1360. if (!$this->names) {
  1361. $this->_gd_getDirectoryData();
  1362. }
  1363. $path = $this->_normpath(substr($path, strlen($this->root)));
  1364. $names = [];
  1365. $paths = explode('/', $path);
  1366. foreach ($paths as $_p) {
  1367. $names[] = isset($this->names[$_p]) ? $this->names[$_p] : $_p;
  1368. }
  1369. return $this->rootName . implode('/', $names);
  1370. }
  1371. /**
  1372. * Return true if $path is children of $parent.
  1373. *
  1374. * @param string $path path to check
  1375. * @param string $parent parent path
  1376. *
  1377. * @return bool
  1378. * @author Dmitry (dio) Levashov
  1379. **/
  1380. protected function _inpath($path, $parent)
  1381. {
  1382. return $path == $parent || strpos($path, $parent . '/') === 0;
  1383. }
  1384. /***************** file stat ********************/
  1385. /**
  1386. * Return stat for given path.
  1387. * Stat contains following fields:
  1388. * - (int) size file size in b. required
  1389. * - (int) ts file modification time in unix time. required
  1390. * - (string) mime mimetype. required for folders, others - optionally
  1391. * - (bool) read read permissions. required
  1392. * - (bool) write write permissions. required
  1393. * - (bool) locked is object locked. optionally
  1394. * - (bool) hidden is object hidden. optionally
  1395. * - (string) alias for symlinks - link target path relative to root path. optionally
  1396. * - (string) target for symlinks - link target path. optionally.
  1397. * If file does not exists - returns empty array or false.
  1398. *
  1399. * @param string $path file path
  1400. *
  1401. * @return array|false
  1402. * @author Dmitry (dio) Levashov
  1403. **/
  1404. protected function _stat($path)
  1405. {
  1406. if ($raw = $this->_gd_getFile($path)) {
  1407. $stat = $this->_gd_parseRaw($raw);
  1408. if ($path === $this->root) {
  1409. $stat['expires'] = $this->expires;
  1410. }
  1411. return $stat;
  1412. }
  1413. return false;
  1414. }
  1415. /**
  1416. * Return true if path is dir and has at least one childs directory.
  1417. *
  1418. * @param string $path dir path
  1419. *
  1420. * @return bool
  1421. * @author Dmitry (dio) Levashov
  1422. **/
  1423. protected function _subdirs($path)
  1424. {
  1425. if ($this->directories === null) {
  1426. $this->_gd_getDirectoryData();
  1427. }
  1428. list(, $itemId) = $this->_gd_splitPath($path);
  1429. return isset($this->directories[$itemId]);
  1430. }
  1431. /**
  1432. * Return object width and height
  1433. * Ususaly used for images, but can be realize for video etc...
  1434. *
  1435. * @param string $path file path
  1436. * @param string $mime file mime type
  1437. *
  1438. * @return string
  1439. * @throws ImagickException
  1440. * @throws elFinderAbortException
  1441. * @author Dmitry (dio) Levashov
  1442. */
  1443. protected function _dimensions($path, $mime)
  1444. {
  1445. if (strpos($mime, 'image') !== 0) {
  1446. return '';
  1447. }
  1448. $ret = '';
  1449. if ($file = $this->_gd_getFile($path)) {
  1450. if (isset($file['imageMediaMetadata'])) {
  1451. $ret = array('dim' => $file['imageMediaMetadata']['width'] . 'x' . $file['imageMediaMetadata']['height']);
  1452. if (func_num_args() > 2) {
  1453. $args = func_get_arg(2);
  1454. } else {
  1455. $args = array();
  1456. }
  1457. if (!empty($args['substitute'])) {
  1458. $tmbSize = intval($args['substitute']);
  1459. $srcSize = explode('x', $ret['dim']);
  1460. if ($srcSize[0] && $srcSize[1]) {
  1461. if (min(($tmbSize / $srcSize[0]), ($tmbSize / $srcSize[1])) < 1) {
  1462. if ($this->_gd_isPublished($file)) {
  1463. $tmbSize = strval($tmbSize);
  1464. $ret['url'] = 'https://drive.google.com/thumbnail?authuser=0&sz=s' . $tmbSize . '&id=' . $file['id'];
  1465. } elseif ($subImgLink = $this->getSubstituteImgLink(elFinder::$currentArgs['target'], $srcSize)) {
  1466. $ret['url'] = $subImgLink;
  1467. }
  1468. }
  1469. }
  1470. }
  1471. }
  1472. }
  1473. return $ret;
  1474. }
  1475. /******************** file/dir content *********************/
  1476. /**
  1477. * Return files list in directory.
  1478. *
  1479. * @param string $path dir path
  1480. *
  1481. * @return array
  1482. * @author Dmitry (dio) Levashov
  1483. * @author Cem (DiscoFever)
  1484. **/
  1485. protected function _scandir($path)
  1486. {
  1487. return isset($this->dirsCache[$path])
  1488. ? $this->dirsCache[$path]
  1489. : $this->cacheDir($path);
  1490. }
  1491. /**
  1492. * Open file and return file pointer.
  1493. *
  1494. * @param string $path file path
  1495. * @param bool $write open file for writing
  1496. *
  1497. * @return resource|false
  1498. * @author Dmitry (dio) Levashov
  1499. **/
  1500. protected function _fopen($path, $mode = 'rb')
  1501. {
  1502. if ($mode === 'rb' || $mode === 'r') {
  1503. if ($file = $this->_gd_getFile($path)) {
  1504. if ($dlurl = $this->_gd_getDownloadUrl($file)) {
  1505. $token = $this->client->getAccessToken();
  1506. if (!$token && $this->client->isUsingApplicationDefaultCredentials()) {
  1507. $this->client->fetchAccessTokenWithAssertion();
  1508. $token = $this->client->getAccessToken();
  1509. }
  1510. $access_token = '';
  1511. if (is_array($token)) {
  1512. $access_token = $token['access_token'];
  1513. } else {
  1514. if ($token = json_decode($this->client->getAccessToken())) {
  1515. $access_token = $token->access_token;
  1516. }
  1517. }
  1518. if ($access_token) {
  1519. $data = array(
  1520. 'target' => $dlurl,
  1521. 'headers' => array('Authorization: Bearer ' . $access_token),
  1522. );
  1523. // to support range request
  1524. if (func_num_args() > 2) {
  1525. $opts = func_get_arg(2);
  1526. } else {
  1527. $opts = array();
  1528. }
  1529. if (!empty($opts['httpheaders'])) {
  1530. $data['headers'] = array_merge($opts['httpheaders'], $data['headers']);
  1531. }
  1532. return elFinder::getStreamByUrl($data);
  1533. }
  1534. }
  1535. }
  1536. }
  1537. return false;
  1538. }
  1539. /**
  1540. * Close opened file.
  1541. *
  1542. * @param resource $fp file pointer
  1543. *
  1544. * @return bool
  1545. * @author Dmitry (dio) Levashov
  1546. **/
  1547. protected function _fclose($fp, $path = '')
  1548. {
  1549. is_resource($fp) && fclose($fp);
  1550. if ($path) {
  1551. unlink($this->getTempFile($path));
  1552. }
  1553. }
  1554. /******************** file/dir manipulations *************************/
  1555. /**
  1556. * Create dir and return created dir path or false on failed.
  1557. *
  1558. * @param string $path parent dir path
  1559. * @param string $name new directory name
  1560. *
  1561. * @return string|bool
  1562. * @author Dmitry (dio) Levashov
  1563. **/
  1564. protected function _mkdir($path, $name)
  1565. {
  1566. $path = $this->_joinPath($path, $name);
  1567. list($parentId, , $parent) = $this->_gd_splitPath($path);
  1568. try {
  1569. $file = new \Google_Service_Drive_DriveFile();
  1570. $file->setName($name);
  1571. $file->setMimeType(self::DIRMIME);
  1572. $file->setParents([$parentId]);
  1573. //create the Folder in the Parent
  1574. $obj = $this->service->files->create($file);
  1575. if ($obj instanceof Google_Service_Drive_DriveFile) {
  1576. $path = $this->_joinPath($parent, $obj['id']);
  1577. $this->_gd_getDirectoryData(false);
  1578. return $path;
  1579. } else {
  1580. return false;
  1581. }
  1582. } catch (Exception $e) {
  1583. return $this->setError('GoogleDrive error: ' . $e->getMessage());
  1584. }
  1585. }
  1586. /**
  1587. * Create file and return it's path or false on failed.
  1588. *
  1589. * @param string $path parent dir path
  1590. * @param string $name new file name
  1591. *
  1592. * @return string|bool
  1593. * @author Dmitry (dio) Levashov
  1594. **/
  1595. protected function _mkfile($path, $name)
  1596. {
  1597. return $this->_save($this->tmpfile(), $path, $name, []);
  1598. }
  1599. /**
  1600. * Create symlink. FTP driver does not support symlinks.
  1601. *
  1602. * @param string $target link target
  1603. * @param string $path symlink path
  1604. *
  1605. * @return bool
  1606. * @author Dmitry (dio) Levashov
  1607. **/
  1608. protected function _symlink($target, $path, $name)
  1609. {
  1610. return false;
  1611. }
  1612. /**
  1613. * Copy file into another file.
  1614. *
  1615. * @param string $source source file path
  1616. * @param string $targetDir target directory path
  1617. * @param string $name new file name
  1618. *
  1619. * @return bool
  1620. * @author Dmitry (dio) Levashov
  1621. **/
  1622. protected function _copy($source, $targetDir, $name)
  1623. {
  1624. $source = $this->_normpath($source);
  1625. $targetDir = $this->_normpath($targetDir);
  1626. try {
  1627. $file = new \Google_Service_Drive_DriveFile();
  1628. $file->setName($name);
  1629. //Set the Parent id
  1630. list(, $parentId) = $this->_gd_splitPath($targetDir);
  1631. $file->setParents([$parentId]);
  1632. list(, $srcId) = $this->_gd_splitPath($source);
  1633. $file = $this->service->files->copy($srcId, $file, ['fields' => self::FETCHFIELDS_GET]);
  1634. $itemId = $file->id;
  1635. return $itemId;
  1636. } catch (Exception $e) {
  1637. return $this->setError('GoogleDrive error: ' . $e->getMessage());
  1638. }
  1639. return true;
  1640. }
  1641. /**
  1642. * Move file into another parent dir.
  1643. * Return new file path or false.
  1644. *
  1645. * @param string $source source file path
  1646. * @param string $target target dir path
  1647. * @param string $name file name
  1648. *
  1649. * @return string|bool
  1650. * @author Dmitry (dio) Levashov
  1651. **/
  1652. protected function _move($source, $targetDir, $name)
  1653. {
  1654. list($removeParents, $itemId) = $this->_gd_splitPath($source);
  1655. $target = $this->_normpath($targetDir . '/' . $itemId);
  1656. try {
  1657. //moving and renaming a file or directory
  1658. $files = new \Google_Service_Drive_DriveFile();
  1659. $files->setName($name);
  1660. //Set new Parent and remove old parent
  1661. list(, $addParents) = $this->_gd_splitPath($targetDir);
  1662. $opts = ['addParents' => $addParents, 'removeParents' => $removeParents];
  1663. $file = $this->service->files->update($itemId, $files, $opts);
  1664. if ($file->getMimeType() === self::DIRMIME) {
  1665. $this->_gd_getDirectoryData(false);
  1666. }
  1667. } catch (Exception $e) {
  1668. return $this->setError('GoogleDrive error: ' . $e->getMessage());
  1669. }
  1670. return $target;
  1671. }
  1672. /**
  1673. * Remove file.
  1674. *
  1675. * @param string $path file path
  1676. *
  1677. * @return bool
  1678. * @author Dmitry (dio) Levashov
  1679. **/
  1680. protected function _unlink($path)
  1681. {
  1682. try {
  1683. $files = new \Google_Service_Drive_DriveFile();
  1684. $files->setTrashed(true);
  1685. list($pid, $itemId) = $this->_gd_splitPath($path);
  1686. $opts = ['removeParents' => $pid];
  1687. $this->service->files->update($itemId, $files, $opts);
  1688. } catch (Exception $e) {
  1689. return $this->setError('GoogleDrive error: ' . $e->getMessage());
  1690. }
  1691. return true;
  1692. }
  1693. /**
  1694. * Remove dir.
  1695. *
  1696. * @param string $path dir path
  1697. *
  1698. * @return bool
  1699. * @author Dmitry (dio) Levashov
  1700. **/
  1701. protected function _rmdir($path)
  1702. {
  1703. $res = $this->_unlink($path);
  1704. $res && $this->_gd_getDirectoryData(false);
  1705. return $res;
  1706. }
  1707. /**
  1708. * Create new file and write into it from file pointer.
  1709. * Return new file path or false on error.
  1710. *
  1711. * @param resource $fp file pointer
  1712. * @param $path
  1713. * @param string $name file name
  1714. * @param array $stat file stat (required by some virtual fs)
  1715. *
  1716. * @return bool|string
  1717. * @author Dmitry (dio) Levashov
  1718. */
  1719. protected function _save($fp, $path, $name, $stat)
  1720. {
  1721. if ($name !== '') {
  1722. $path .= '/' . str_replace('/', '\\/', $name);
  1723. }
  1724. list($parentId, $itemId, $parent) = $this->_gd_splitPath($path);
  1725. if ($name === '') {
  1726. $stat['iid'] = $itemId;
  1727. }
  1728. if (!$stat || empty($stat['iid'])) {
  1729. $opts = [
  1730. 'q' => sprintf('trashed=false and "%s" in parents and name="%s"', $parentId, $name),
  1731. 'fields' => self::FETCHFIELDS_LIST,
  1732. ];
  1733. $srcFile = $this->_gd_query($opts);
  1734. $srcFile = empty($srcFile) ? null : $srcFile[0];
  1735. } else {
  1736. $srcFile = $this->_gd_getFile($path);
  1737. }
  1738. try {
  1739. $mode = 'update';
  1740. $mime = isset($stat['mime']) ? $stat['mime'] : '';
  1741. $file = new Google_Service_Drive_DriveFile();
  1742. if ($srcFile) {
  1743. $mime = $srcFile->getMimeType();
  1744. } else {
  1745. $mode = 'insert';
  1746. $file->setName($name);
  1747. $file->setParents([
  1748. $parentId,
  1749. ]);
  1750. }
  1751. if (!$mime) {
  1752. $mime = self::mimetypeInternalDetect($name);
  1753. }
  1754. if ($mime === 'unknown') {
  1755. $mime = 'application/octet-stream';
  1756. }
  1757. $file->setMimeType($mime);
  1758. $size = 0;
  1759. if (isset($stat['size'])) {
  1760. $size = $stat['size'];
  1761. } else {
  1762. $fstat = fstat($fp);
  1763. if (!empty($fstat['size'])) {
  1764. $size = $fstat['size'];
  1765. }
  1766. }
  1767. // set chunk size (max: 100MB)
  1768. $chunkSizeBytes = 100 * 1024 * 1024;
  1769. if ($size > 0) {
  1770. $memory = elFinder::getIniBytes('memory_limit');
  1771. if ($memory > 0) {
  1772. $chunkSizeBytes = max(262144, min([$chunkSizeBytes, (intval($memory / 4 / 256) * 256)]));
  1773. }
  1774. }
  1775. if ($size > $chunkSizeBytes) {
  1776. $client = $this->client;
  1777. // Call the API with the media upload, defer so it doesn't immediately return.
  1778. $client->setDefer(true);
  1779. if ($mode === 'insert') {
  1780. $request = $this->service->files->create($file, [
  1781. 'fields' => self::FETCHFIELDS_GET,
  1782. ]);
  1783. } else {
  1784. $request = $this->service->files->update($srcFile->getId(), $file, [
  1785. 'fields' => self::FETCHFIELDS_GET,
  1786. ]);
  1787. }
  1788. // Create a media file upload to represent our upload process.
  1789. $media = new Google_Http_MediaFileUpload($client, $request, $mime, null, true, $chunkSizeBytes);
  1790. $media->setFileSize($size);
  1791. // Upload the various chunks. $status will be false until the process is
  1792. // complete.
  1793. $status = false;
  1794. while (!$status && !feof($fp)) {
  1795. elFinder::checkAborted();
  1796. // read until you get $chunkSizeBytes from TESTFILE
  1797. // fread will never return more than 8192 bytes if the stream is read buffered and it does not represent a plain file
  1798. // An example of a read buffered file is when reading from a URL
  1799. $chunk = $this->_gd_readFileChunk($fp, $chunkSizeBytes);
  1800. $status = $media->nextChunk($chunk);
  1801. }
  1802. // The final value of $status will be the data from the API for the object
  1803. // that has been uploaded.
  1804. if ($status !== false) {
  1805. $obj = $status;
  1806. }
  1807. $client->setDefer(false);
  1808. } else {
  1809. $params = [
  1810. 'data' => stream_get_contents($fp),
  1811. 'uploadType' => 'media',
  1812. 'fields' => self::FETCHFIELDS_GET,
  1813. ];
  1814. if ($mode === 'insert') {
  1815. $obj = $this->service->files->create($file, $params);
  1816. } else {
  1817. $obj = $this->service->files->update($srcFile->getId(), $file, $params);
  1818. }
  1819. }
  1820. if ($obj instanceof Google_Service_Drive_DriveFile) {
  1821. return $this->_joinPath($parent, $obj->getId());
  1822. } else {
  1823. return false;
  1824. }
  1825. } catch (Exception $e) {
  1826. return $this->setError('GoogleDrive error: ' . $e->getMessage());
  1827. }
  1828. }
  1829. /**
  1830. * Get file contents.
  1831. *
  1832. * @param string $path file path
  1833. *
  1834. * @return string|false
  1835. * @author Dmitry (dio) Levashov
  1836. **/
  1837. protected function _getContents($path)
  1838. {
  1839. $contents = '';
  1840. try {
  1841. list(, $itemId) = $this->_gd_splitPath($path);
  1842. $contents = $this->service->files->get($itemId, [
  1843. 'alt' => 'media',
  1844. ]);
  1845. $contents = (string)$contents->getBody();
  1846. } catch (Exception $e) {
  1847. return $this->setError('GoogleDrive error: ' . $e->getMessage());
  1848. }
  1849. return $contents;
  1850. }
  1851. /**
  1852. * Write a string to a file.
  1853. *
  1854. * @param string $path file path
  1855. * @param string $content new file content
  1856. *
  1857. * @return bool
  1858. * @author Dmitry (dio) Levashov
  1859. **/
  1860. protected function _filePutContents($path, $content)
  1861. {
  1862. $res = false;
  1863. if ($local = $this->getTempFile($path)) {
  1864. if (file_put_contents($local, $content, LOCK_EX) !== false
  1865. && ($fp = fopen($local, 'rb'))) {
  1866. clearstatcache();
  1867. $res = $this->_save($fp, $path, '', []);
  1868. fclose($fp);
  1869. }
  1870. file_exists($local) && unlink($local);
  1871. }
  1872. return $res;
  1873. }
  1874. /**
  1875. * Detect available archivers.
  1876. **/
  1877. protected function _checkArchivers()
  1878. {
  1879. // die('Not yet implemented. (_checkArchivers)');
  1880. return [];
  1881. }
  1882. /**
  1883. * chmod implementation.
  1884. *
  1885. * @return bool
  1886. **/
  1887. protected function _chmod($path, $mode)
  1888. {
  1889. return false;
  1890. }
  1891. /**
  1892. * Unpack archive.
  1893. *
  1894. * @param string $path archive path
  1895. * @param array $arc archiver command and arguments (same as in $this->archivers)
  1896. *
  1897. * @return void
  1898. * @author Dmitry (dio) Levashov
  1899. * @author Alexey Sukhotin
  1900. */
  1901. protected function _unpack($path, $arc)
  1902. {
  1903. die('Not yet implemented. (_unpack)');
  1904. //return false;
  1905. }
  1906. /**
  1907. * Extract files from archive.
  1908. *
  1909. * @param string $path archive path
  1910. * @param array $arc archiver command and arguments (same as in $this->archivers)
  1911. *
  1912. * @return void
  1913. * @author Dmitry (dio) Levashov,
  1914. * @author Alexey Sukhotin
  1915. */
  1916. protected function _extract($path, $arc)
  1917. {
  1918. die('Not yet implemented. (_extract)');
  1919. }
  1920. /**
  1921. * Create archive and return its path.
  1922. *
  1923. * @param string $dir target dir
  1924. * @param array $files files names list
  1925. * @param string $name archive name
  1926. * @param array $arc archiver options
  1927. *
  1928. * @return string|bool
  1929. * @author Dmitry (dio) Levashov,
  1930. * @author Alexey Sukhotin
  1931. **/
  1932. protected function _archive($dir, $files, $name, $arc)
  1933. {
  1934. die('Not yet implemented. (_archive)');
  1935. }
  1936. } // END class