Edit File by line

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