elFinderVolumeGoogleDrive.class.php 66 KB

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