elFinderFlysystemGoogleDriveNetmount.php 13 KB

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