elFinderFlysystemGoogleDriveNetmount.php 13 KB

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