: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Cached\CachedAdapter;
use League\Flysystem\Cached\Storage\Adapter as ACache;
use Hypweb\Flysystem\GoogleDrive\GoogleDriveAdapter;
use Hypweb\Flysystem\Cached\Extra\Hasdir;
use Hypweb\Flysystem\Cached\Extra\DisableEnsureParentDirectories;
use Hypweb\elFinderFlysystemDriverExt\Driver as ExtDriver;
elFinder::$netDrivers['googledrive'] = 'FlysystemGoogleDriveNetmount';
if (!class_exists('elFinderVolumeFlysystemGoogleDriveCache', false)) {
class elFinderVolumeFlysystemGoogleDriveCache extends ACache
use DisableEnsureParentDirectories;
class elFinderVolumeFlysystemGoogleDriveNetmount extends ExtDriver
public function __construct()
'acceptedName' => '#^[^/\\?*:|"<>]*[^./\\?*:|"<>]$#',
'rootCssClass' => 'elfinder-navbar-root-googledrive',
'gdAlias' => '%s@GDrive',
'gdCacheDir' => __DIR__ . '/.tmp',
'gdCachePrefix' => 'gd-',
$this->options = array_merge($this->options, $opts);
* Prepare driver before mount volume.
* Return true if volume is ready.
protected function init()
if (empty($this->options['icon'])) {
$this->options['icon'] = true;
if ($res = parent::init()) {
if ($this->options['icon'] === true) {
unset($this->options['icon']);
// enable command archive
$this->options['useRemoteArchive'] = true;
* Call from elFinder::netmout() before volume->mount()
public function netmountPrepare($options)
if (empty($options['client_id']) && defined('ELFINDER_GOOGLEDRIVE_CLIENTID')) {
$options['client_id'] = ELFINDER_GOOGLEDRIVE_CLIENTID;
if (empty($options['client_secret']) && defined('ELFINDER_GOOGLEDRIVE_CLIENTSECRET')) {
$options['client_secret'] = ELFINDER_GOOGLEDRIVE_CLIENTSECRET;
if (!isset($options['pass'])) {
$client = new \Google_Client();
$client->setClientId($options['client_id']);
$client->setClientSecret($options['client_secret']);
if ($options['pass'] === 'reauth') {
$this->session->set('GoogleDriveAuthParams', [])->set('GoogleDriveTokens', []);
} else if ($options['pass'] === 'googledrive') {
$options = array_merge($this->session->get('GoogleDriveAuthParams', []), $options);
if (!isset($options['access_token'])) {
$options['access_token'] = $this->session->get('GoogleDriveTokens', []);
$this->session->remove('GoogleDriveTokens');
$aToken = $options['access_token'];
$rootObj = $service = null;
$client->setAccessToken($aToken);
if ($client->isAccessTokenExpired()) {
$aToken = array_merge($aToken, $client->fetchAccessTokenWithRefreshToken());
$client->setAccessToken($aToken);
$service = new \Google_Service_Drive($client);
$rootObj = $service->files->get('root');
$options['access_token'] = $aToken;
$this->session->set('GoogleDriveAuthParams', $options);
$options['access_token'] = [];
if ($options['user'] !== 'init') {
$this->session->set('GoogleDriveAuthParams', $options);
return array('exit' => true, 'error' => elFinder::ERROR_REAUTH_REQUIRE);
$itpCare = isset($options['code']);
$code = $itpCare? $options['code'] : (isset($_GET['code'])? $_GET['code'] : '');
if ($code || $options['user'] === 'init') {
if (empty($options['url'])) {
$options['url'] = elFinder::getConnectorUrl();
if (isset($options['id'])) {
$callback = $options['url'] . (strpos($options['url'], '?') !== false? '&' : '?') . 'cmd=netmount&protocol=googledrive&host=' . ($options['id'] === 'elfinder'? '1' : $options['id']);
$client->setRedirectUri($callback);
if (!$aToken && empty($code)) {
$client->setScopes([Google_Service_Drive::DRIVE]);
if (!empty($options['offline'])) {
$client->setApprovalPrompt('force');
$client->setAccessType('offline');
$url = $client->createAuthUrl();
$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">';
jQuery("#' . $options['id'] . '").elfinder("instance").trigger("netmount", {protocol: "googledrive", mode: "makebtn", url: "' . $url . '"});
if (empty($options['pass']) && $options['host'] !== '1') {
$options['pass'] = 'return';
$this->session->set('GoogleDriveAuthParams', $options);
return array('exit' => true, 'body' => $html);
'node' => $options['id'],
'json' => '{"protocol": "googledrive", "mode": "makebtn", "body" : "' . str_replace($html, '"', '\\"') . '", "error" : "' . elFinder::ERROR_ACCESS_DENIED . '"}',
return array('exit' => 'callback', 'out' => $out);
if (!empty($options['id'])) {
$aToken = $client->fetchAccessTokenWithAuthCode($code);
$options['access_token'] = $aToken;
$this->session->set('GoogleDriveTokens', $aToken)->set('GoogleDriveAuthParams', $options);
'node' => $options['id'],
'json' => '{"protocol": "googledrive", "mode": "done", "reset": 1}',
$nodeid = ($_GET['host'] === '1')? 'elfinder' : $_GET['host'];
'json' => json_encode(array(
'protocol' => 'googledrive',
return array('exit' => 'callback', 'out' => $out);
return array('exit' => true, 'body' => $out['json']);
foreach ($service->files->listFiles([
'q' => 'trashed = false and mimeType = "application/vnd.google-apps.folder"'
$folders[$f->getId()] = $f->getName();
$folders = ['root' => $rootObj->getName()] + $folders;
$folders = json_encode($folders);
$json = '{"protocol": "googledrive", "mode": "done", "folders": ' . $folders . '}';
$options['pass'] = 'return';
jQuery("#' . $options['id'] . '").elfinder("instance").trigger("netmount", ' . $json . ');
$this->session->set('GoogleDriveAuthParams', $options);
return array('exit' => true, 'body' => $html);
$this->session->remove('GoogleDriveAuthParams')->remove('GoogleDriveTokens');
if (empty($options['pass'])) {
return array('exit' => true, 'body' => '{msg:' . elFinder::ERROR_ACCESS_DENIED . '}' . ' ' . $e->getMessage());
return array('exit' => true, 'error' => [elFinder::ERROR_ACCESS_DENIED, $e->getMessage()]);
return array('exit' => true, 'error' => elFinder::ERROR_REAUTH_REQUIRE);
if ($options['path'] === '/') {
$options['path'] = 'root';
$file = $service->files->get($options['path']);
$options['alias'] = sprintf($this->options['gdAlias'], $file->getName());
if (!empty($this->options['netkey'])) {
elFinder::$instance->updateNetVolumeOption($this->options['netkey'], 'alias', $this->options['alias']);
} catch (Google_Service_Exception $e) {
$err = json_decode($e->getMessage(), true);
if (isset($err['error']) && $err['error']['code'] == 404) {
return array('exit' => true, 'error' => [elFinder::ERROR_TRGDIR_NOT_FOUND, $options['path']]);
return array('exit' => true, 'error' => $e->getMessage());
return array('exit' => true, 'error' => $e->getMessage());
foreach (['host', 'user', 'pass', 'id', 'offline'] as $key) {
* process of on netunmount
* Drop table `dropbox` & rm thumbs
* @internal param array $options
public function netunmount($netVolumes, $key)
$cache = $this->options['gdCacheDir'] . DIRECTORY_SEPARATOR . $this->options['gdCachePrefix'] . $this->netMountKey;
if (file_exists($cache) && is_writeable($cache)) {
if ($tmbs = glob($this->tmbPath . DIRECTORY_SEPARATOR . $this->netMountKey . '*')) {
foreach ($tmbs as $file) {
* Return true if volume available for read or write,
public function mount(array $opts)
if (isset($opts['access_token'])) {
$this->netMountKey = md5(join('-', array('googledrive', $opts['path'], (isset($opts['access_token']['refresh_token']) ? $opts['access_token']['refresh_token'] : $opts['access_token']['access_token']))));
$client = new \Google_Client();
$client->setClientId($opts['client_id']);
$client->setClientSecret($opts['client_secret']);
if (!empty($opts['access_token'])) {
$client->setAccessToken($opts['access_token']);
if ($this->needOnline && $client->isAccessTokenExpired()) {
$creds = $client->fetchAccessTokenWithRefreshToken();
} catch (LogicException $e) {
$this->session->remove('GoogleDriveAuthParams');
$service = new \Google_Service_Drive($client);
// If path is not set, use the root
if (!isset($opts['path']) || $opts['path'] === '') {
$googleDrive = new GoogleDriveAdapter($service, $opts['path'], ['useHasDir' => true]);
if ($this->options['gdCacheDir'] && is_writeable($this->options['gdCacheDir'])) {
if ($this->options['gdCacheExpire']) {
$opts['fscache'] = new elFinderVolumeFlysystemGoogleDriveCache(new Local($this->options['gdCacheDir']), $this->options['gdCachePrefix'] . $this->netMountKey, $this->options['gdCacheExpire']);
$filesystem = new Filesystem(new CachedAdapter($googleDrive, $opts['fscache']));
$filesystem = new Filesystem($googleDrive);
$opts['driver'] = 'FlysystemExt';
$opts['filesystem'] = $filesystem;
$opts['separator'] = '/';
$opts['checkSubfolders'] = true;
if (!isset($opts['alias'])) {
$opts['alias'] = 'GoogleDrive';
if ($res = parent::mount($opts)) {
// update access_token of session data
$netVolumes = $this->session->get('netvolume');
$netVolumes[$this->netMountKey]['access_token'] = array_merge($netVolumes[$this->netMountKey]['access_token'], $creds);
$this->session->set('netvolume', $netVolumes);
protected function tmbname($stat)
return $this->netMountKey . substr(substr($stat['hash'], strlen($this->id)), -38) . $stat['ts'] . '.png';
* Return debug info for client.
if (!empty($this->options['netkey']) && empty($this->options['refresh_token']) && $this->options['access_token'] && isset($this->options['access_token']['refresh_token'])) {
$res['refresh_token'] = $this->options['access_token']['refresh_token'];