elFinderVolumeGoogleDrive.class.php 67 KB

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