elFinderFlysystemGoogleDriveNetmount.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <?php
  2. use League\Flysystem\Filesystem;
  3. use League\Flysystem\Adapter\Local;
  4. use League\Flysystem\Cached\CachedAdapter;
  5. use League\Flysystem\Cached\Storage\Adapter as ACache;
  6. use Hypweb\Flysystem\GoogleDrive\GoogleDriveAdapter;
  7. use Hypweb\Flysystem\Cached\Extra\Hasdir;
  8. use Hypweb\Flysystem\Cached\Extra\DisableEnsureParentDirectories;
  9. use Hypweb\elFinderFlysystemDriverExt\Driver as ExtDriver;
  10. elFinder::$netDrivers['googledrive'] = 'FlysystemGoogleDriveNetmount';
  11. if (!class_exists('elFinderVolumeFlysystemGoogleDriveCache', false)) {
  12. class elFinderVolumeFlysystemGoogleDriveCache extends ACache
  13. {
  14. use Hasdir;
  15. use DisableEnsureParentDirectories;
  16. }
  17. }
  18. class elFinderVolumeFlysystemGoogleDriveNetmount extends ExtDriver
  19. {
  20. public function __construct()
  21. {
  22. parent::__construct();
  23. $opts = array(
  24. 'acceptedName' => '#^[^/\\?*:|"<>]*[^./\\?*:|"<>]$#',
  25. 'rootCssClass' => 'elfinder-navbar-root-googledrive',
  26. 'gdAlias' => '%s@GDrive',
  27. 'gdCacheDir' => __DIR__ . '/.tmp',
  28. 'gdCachePrefix' => 'gd-',
  29. 'gdCacheExpire' => 600
  30. );
  31. $this->options = array_merge($this->options, $opts);
  32. }
  33. /**
  34. * Prepare driver before mount volume.
  35. * Return true if volume is ready.
  36. *
  37. * @return bool
  38. **/
  39. protected function init()
  40. {
  41. if (empty($this->options['icon'])) {
  42. $this->options['icon'] = true;
  43. }
  44. if ($res = parent::init()) {
  45. if ($this->options['icon'] === true) {
  46. unset($this->options['icon']);
  47. }
  48. // enable command archive
  49. $this->options['useRemoteArchive'] = true;
  50. }
  51. return $res;
  52. }
  53. /**
  54. * Prepare
  55. * Call from elFinder::netmout() before volume->mount()
  56. *
  57. * @param $options
  58. *
  59. * @return Array
  60. * @author Naoki Sawada
  61. */
  62. public function netmountPrepare($options)
  63. {
  64. if (empty($options['client_id']) && defined('ELFINDER_GOOGLEDRIVE_CLIENTID')) {
  65. $options['client_id'] = ELFINDER_GOOGLEDRIVE_CLIENTID;
  66. }
  67. if (empty($options['client_secret']) && defined('ELFINDER_GOOGLEDRIVE_CLIENTSECRET')) {
  68. $options['client_secret'] = ELFINDER_GOOGLEDRIVE_CLIENTSECRET;
  69. }
  70. if (!isset($options['pass'])) {
  71. $options['pass'] = '';
  72. }
  73. try {
  74. $client = new \Google_Client();
  75. $client->setClientId($options['client_id']);
  76. $client->setClientSecret($options['client_secret']);
  77. if ($options['pass'] === 'reauth') {
  78. $options['pass'] = '';
  79. $this->session->set('GoogleDriveAuthParams', [])->set('GoogleDriveTokens', []);
  80. } else if ($options['pass'] === 'googledrive') {
  81. $options['pass'] = '';
  82. }
  83. $options = array_merge($this->session->get('GoogleDriveAuthParams', []), $options);
  84. if (!isset($options['access_token'])) {
  85. $options['access_token'] = $this->session->get('GoogleDriveTokens', []);
  86. $this->session->remove('GoogleDriveTokens');
  87. }
  88. $aToken = $options['access_token'];
  89. $rootObj = $service = null;
  90. if ($aToken) {
  91. try {
  92. $client->setAccessToken($aToken);
  93. if ($client->isAccessTokenExpired()) {
  94. $aToken = array_merge($aToken, $client->fetchAccessTokenWithRefreshToken());
  95. $client->setAccessToken($aToken);
  96. }
  97. $service = new \Google_Service_Drive($client);
  98. $rootObj = $service->files->get('root');
  99. $options['access_token'] = $aToken;
  100. $this->session->set('GoogleDriveAuthParams', $options);
  101. } catch (Exception $e) {
  102. $aToken = [];
  103. $options['access_token'] = [];
  104. if ($options['user'] !== 'init') {
  105. $this->session->set('GoogleDriveAuthParams', $options);
  106. return array('exit' => true, 'error' => elFinder::ERROR_REAUTH_REQUIRE);
  107. }
  108. }
  109. }
  110. $itpCare = isset($options['code']);
  111. $code = $itpCare? $options['code'] : (isset($_GET['code'])? $_GET['code'] : '');
  112. if ($code || $options['user'] === 'init') {
  113. if (empty($options['url'])) {
  114. $options['url'] = elFinder::getConnectorUrl();
  115. }
  116. if (isset($options['id'])) {
  117. $callback = $options['url'] . (strpos($options['url'], '?') !== false? '&' : '?') . 'cmd=netmount&protocol=googledrive&host=' . ($options['id'] === 'elfinder'? '1' : $options['id']);
  118. $client->setRedirectUri($callback);
  119. }
  120. if (!$aToken && empty($code)) {
  121. $client->setScopes([Google_Service_Drive::DRIVE]);
  122. if (!empty($options['offline'])) {
  123. $client->setApprovalPrompt('force');
  124. $client->setAccessType('offline');
  125. }
  126. $url = $client->createAuthUrl();
  127. $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">';
  128. $html .= '<script>
  129. $("#' . $options['id'] . '").elfinder("instance").trigger("netmount", {protocol: "googledrive", mode: "makebtn", url: "' . $url . '"});
  130. </script>';
  131. if (empty($options['pass']) && $options['host'] !== '1') {
  132. $options['pass'] = 'return';
  133. $this->session->set('GoogleDriveAuthParams', $options);
  134. return array('exit' => true, 'body' => $html);
  135. } else {
  136. $out = array(
  137. 'node' => $options['id'],
  138. 'json' => '{"protocol": "googledrive", "mode": "makebtn", "body" : "' . str_replace($html, '"', '\\"') . '", "error" : "' . elFinder::ERROR_ACCESS_DENIED . '"}',
  139. 'bind' => 'netmount'
  140. );
  141. return array('exit' => 'callback', 'out' => $out);
  142. }
  143. } else {
  144. if ($code) {
  145. if (!empty($options['id'])) {
  146. $aToken = $client->fetchAccessTokenWithAuthCode($code);
  147. $options['access_token'] = $aToken;
  148. unset($options['code']);
  149. $this->session->set('GoogleDriveTokens', $aToken)->set('GoogleDriveAuthParams', $options);
  150. $out = array(
  151. 'node' => $options['id'],
  152. 'json' => '{"protocol": "googledrive", "mode": "done", "reset": 1}',
  153. 'bind' => 'netmount'
  154. );
  155. } else {
  156. $nodeid = ($_GET['host'] === '1')? 'elfinder' : $_GET['host'];
  157. $out = array(
  158. 'node' => $nodeid,
  159. 'json' => json_encode(array(
  160. 'protocol' => 'googledrive',
  161. 'host' => $nodeid,
  162. 'mode' => 'redirect',
  163. 'options' => array(
  164. 'id' => $nodeid,
  165. 'code'=> $code
  166. )
  167. )),
  168. 'bind' => 'netmount'
  169. );
  170. }
  171. if (!$itpCare) {
  172. return array('exit' => 'callback', 'out' => $out);
  173. } else {
  174. return array('exit' => true, 'body' => $out['json']);
  175. }
  176. }
  177. $folders = [];
  178. foreach ($service->files->listFiles([
  179. 'pageSize' => 1000,
  180. 'q' => 'trashed = false and mimeType = "application/vnd.google-apps.folder"'
  181. ]) as $f) {
  182. $folders[$f->getId()] = $f->getName();
  183. }
  184. natcasesort($folders);
  185. $folders = ['root' => $rootObj->getName()] + $folders;
  186. $folders = json_encode($folders);
  187. $json = '{"protocol": "googledrive", "mode": "done", "folders": ' . $folders . '}';
  188. $options['pass'] = 'return';
  189. $html = 'Google.com';
  190. $html .= '<script>
  191. $("#' . $options['id'] . '").elfinder("instance").trigger("netmount", ' . $json . ');
  192. </script>';
  193. $this->session->set('GoogleDriveAuthParams', $options);
  194. return array('exit' => true, 'body' => $html);
  195. }
  196. }
  197. } catch (Exception $e) {
  198. $this->session->remove('GoogleDriveAuthParams')->remove('GoogleDriveTokens');
  199. if (empty($options['pass'])) {
  200. return array('exit' => true, 'body' => '{msg:' . elFinder::ERROR_ACCESS_DENIED . '}' . ' ' . $e->getMessage());
  201. } else {
  202. return array('exit' => true, 'error' => [elFinder::ERROR_ACCESS_DENIED, $e->getMessage()]);
  203. }
  204. }
  205. if (!$aToken) {
  206. return array('exit' => true, 'error' => elFinder::ERROR_REAUTH_REQUIRE);
  207. }
  208. if ($options['path'] === '/') {
  209. $options['path'] = 'root';
  210. }
  211. try {
  212. $file = $service->files->get($options['path']);
  213. $options['alias'] = sprintf($this->options['gdAlias'], $file->getName());
  214. if (!empty($this->options['netkey'])) {
  215. elFinder::$instance->updateNetVolumeOption($this->options['netkey'], 'alias', $this->options['alias']);
  216. }
  217. } catch (Google_Service_Exception $e) {
  218. $err = json_decode($e->getMessage(), true);
  219. if (isset($err['error']) && $err['error']['code'] == 404) {
  220. return array('exit' => true, 'error' => [elFinder::ERROR_TRGDIR_NOT_FOUND, $options['path']]);
  221. } else {
  222. return array('exit' => true, 'error' => $e->getMessage());
  223. }
  224. } catch (Exception $e) {
  225. return array('exit' => true, 'error' => $e->getMessage());
  226. }
  227. foreach (['host', 'user', 'pass', 'id', 'offline'] as $key) {
  228. unset($options[$key]);
  229. }
  230. return $options;
  231. }
  232. /**
  233. * process of on netunmount
  234. * Drop table `dropbox` & rm thumbs
  235. *
  236. * @param $netVolumes
  237. * @param $key
  238. *
  239. * @return bool
  240. * @internal param array $options
  241. */
  242. public function netunmount($netVolumes, $key)
  243. {
  244. $cache = $this->options['gdCacheDir'] . DIRECTORY_SEPARATOR . $this->options['gdCachePrefix'] . $this->netMountKey;
  245. if (file_exists($cache) && is_writeable($cache)) {
  246. unlink($cache);
  247. }
  248. if ($tmbs = glob($this->tmbPath . DIRECTORY_SEPARATOR . $this->netMountKey . '*')) {
  249. foreach ($tmbs as $file) {
  250. unlink($file);
  251. }
  252. }
  253. return true;
  254. }
  255. /**
  256. * "Mount" volume.
  257. * Return true if volume available for read or write,
  258. * false - otherwise
  259. *
  260. * @param array $opts
  261. *
  262. * @return bool
  263. * @author Naoki Sawada
  264. */
  265. public function mount(array $opts)
  266. {
  267. $creds = null;
  268. if (isset($opts['access_token'])) {
  269. $this->netMountKey = md5(join('-', array('googledrive', $opts['path'], (isset($opts['access_token']['refresh_token']) ? $opts['access_token']['refresh_token'] : $opts['access_token']['access_token']))));
  270. }
  271. $client = new \Google_Client();
  272. $client->setClientId($opts['client_id']);
  273. $client->setClientSecret($opts['client_secret']);
  274. if (!empty($opts['access_token'])) {
  275. $client->setAccessToken($opts['access_token']);
  276. }
  277. if ($this->needOnline && $client->isAccessTokenExpired()) {
  278. try {
  279. $creds = $client->fetchAccessTokenWithRefreshToken();
  280. } catch (LogicException $e) {
  281. $this->session->remove('GoogleDriveAuthParams');
  282. throw $e;
  283. }
  284. }
  285. $service = new \Google_Service_Drive($client);
  286. // If path is not set, use the root
  287. if (!isset($opts['path']) || $opts['path'] === '') {
  288. $opts['path'] = 'root';
  289. }
  290. $googleDrive = new GoogleDriveAdapter($service, $opts['path'], ['useHasDir' => true]);
  291. $opts['fscache'] = null;
  292. if ($this->options['gdCacheDir'] && is_writeable($this->options['gdCacheDir'])) {
  293. if ($this->options['gdCacheExpire']) {
  294. $opts['fscache'] = new elFinderVolumeFlysystemGoogleDriveCache(new Local($this->options['gdCacheDir']), $this->options['gdCachePrefix'] . $this->netMountKey, $this->options['gdCacheExpire']);
  295. }
  296. }
  297. if ($opts['fscache']) {
  298. $filesystem = new Filesystem(new CachedAdapter($googleDrive, $opts['fscache']));
  299. } else {
  300. $filesystem = new Filesystem($googleDrive);
  301. }
  302. $opts['driver'] = 'FlysystemExt';
  303. $opts['filesystem'] = $filesystem;
  304. $opts['separator'] = '/';
  305. $opts['checkSubfolders'] = true;
  306. if (!isset($opts['alias'])) {
  307. $opts['alias'] = 'GoogleDrive';
  308. }
  309. if ($res = parent::mount($opts)) {
  310. // update access_token of session data
  311. if ($creds) {
  312. $netVolumes = $this->session->get('netvolume');
  313. $netVolumes[$this->netMountKey]['access_token'] = array_merge($netVolumes[$this->netMountKey]['access_token'], $creds);
  314. $this->session->set('netvolume', $netVolumes);
  315. }
  316. }
  317. return $res;
  318. }
  319. /**
  320. * @inheritdoc
  321. */
  322. protected function tmbname($stat)
  323. {
  324. return $this->netMountKey . substr(substr($stat['hash'], strlen($this->id)), -38) . $stat['ts'] . '.png';
  325. }
  326. /**
  327. * Return debug info for client.
  328. *
  329. * @return array
  330. **/
  331. public function debug()
  332. {
  333. $res = parent::debug();
  334. if (!empty($this->options['netkey']) && empty($this->options['refresh_token']) && $this->options['access_token'] && isset($this->options['access_token']['refresh_token'])) {
  335. $res['refresh_token'] = $this->options['access_token']['refresh_token'];
  336. }
  337. return $res;
  338. }
  339. }