From 53f88aa506f9beec678ba05ddd2e378121bfbe95 Mon Sep 17 00:00:00 2001 From: qkqpttgf <45693631+qkqpttgf@users.noreply.github.com> Date: Sun, 7 Jun 2020 18:43:47 +0800 Subject: [PATCH] Alifc (#73) * Update index.php * Update common.php * Update and rename scf.php to tencentscf.php * Create alifc.php * Update common.php * Update alifc.php * Update alifc.php * Update alifc.php * Update normal.php * Update common.php * Update common.php * Update conststr.php * Update normal.php * Update normal.php * Update conststr.php * Update index.php * Update common.php * Rename alifc.php to AliyunFC.php * Rename heroku.php to Heroku.php * Rename normal.php to Normal.php * Rename tencentscf.php to TencentSCF.php * Update common.php * Update conststr.php * Update common.php * Update common.php * Update common.php --- common.php | 136 +++++++-- conststr.php | 10 +- index.php | 65 ++++- platform/AliyunFC.php | 397 +++++++++++++++++++++++++++ platform/{heroku.php => Heroku.php} | 0 platform/{normal.php => Normal.php} | 68 ++++- platform/{scf.php => TencentSCF.php} | 2 + 7 files changed, 645 insertions(+), 33 deletions(-) create mode 100644 platform/AliyunFC.php rename platform/{heroku.php => Heroku.php} (100%) rename platform/{normal.php => Normal.php} (79%) rename platform/{scf.php => TencentSCF.php} (96%) diff --git a/common.php b/common.php index 61838da..de54a53 100644 --- a/common.php +++ b/common.php @@ -5,6 +5,8 @@ $Base64Env = [ //'Region', // used in SCF. //'SecretId', // used in SCF. //'SecretKey', // used in SCF. + //'AccessKeyID', // used in FC. + //'AccessKeySecret', // used in FC. //'admin', //'adminloginpage', 'background', @@ -33,6 +35,7 @@ $Base64Env = [ //'sharecookie', 'shareapiurl', //'siteid', + 'domainforproxy', 'public_path', //'refresh_token', //'token_expires', @@ -43,6 +46,8 @@ $CommonEnv = [ 'Region', // used in SCF. 'SecretId', // used in SCF. 'SecretKey', // used in SCF. + 'AccessKeyID', // used in FC. + 'AccessKeySecret', // used in FC. 'admin', 'adminloginpage', 'background', @@ -64,6 +69,8 @@ $ShowedCommonEnv = [ //'Region', // used in SCF. //'SecretId', // used in SCF. //'SecretKey', // used in SCF. + //'AccessKeyID', // used in FC. + //'AccessKeySecret', // used in FC. //'admin', 'adminloginpage', 'background', @@ -95,6 +102,7 @@ $InnerEnv = [ 'shareurl', //'sharecookie', 'shareapiurl', + 'domainforproxy', 'public_path', 'refresh_token', 'token_expires', @@ -115,6 +123,7 @@ $ShowedInnerEnv = [ //'shareurl', //'sharecookie', //'shareapiurl', + 'domainforproxy', 'public_path', //'refresh_token', //'token_expires', @@ -317,6 +326,10 @@ function main($path) $files = list_files($path); //echo json_encode(array_keys($files['children']), JSON_PRETTY_PRINT); + if ($_GET['json']) { + // return a json + return files_json($files); + } if (isset($_GET['random'])&&$_GET['random']!=='') { if ($_SERVER['ishidden']<4) { $tmp = []; @@ -325,14 +338,29 @@ function main($path) } $tmp = array_values($tmp); if (count($tmp)>0) { - if (isset($_GET['url'])) return output($tmp[rand(0,count($tmp)-1)], 200); - return output('', 302, [ 'Location' => $tmp[rand(0,count($tmp)-1)] ]); + $url = $tmp[rand(0,count($tmp)-1)]; + if (isset($_GET['url'])) return output($url, 200); + $domainforproxy = ''; + $domainforproxy = getConfig('domainforproxy'); + if ($domainforproxy!='') { + $url = proxy_replace_domain($url, $domainforproxy); + } + return output('', 302, [ 'Location' => $url ]); } else return output('',404); } else return output('',401); } if (isset($files['file']) && !isset($_GET['preview'])) { // is file && not preview mode - if ( $_SERVER['ishidden']<4 || (!!getConfig('downloadencrypt')&&$files['name']!=getConfig('passfile')) ) return output('', 302, [ 'Location' => $files[$_SERVER['DownurlStrName']] ]); + if ( $_SERVER['ishidden']<4 || (!!getConfig('downloadencrypt')&&$files['name']!=getConfig('passfile')) ) { + $url = $files[$_SERVER['DownurlStrName']]; + $domainforproxy = ''; + $domainforproxy = getConfig('domainforproxy'); + if ($domainforproxy!='') { + $url = proxy_replace_domain($url, $domainforproxy); + } + if ( strtolower(splitlast($files['name'],'.')[1])=='html' ) return output($files['content']['body'], $files['content']['stat']); + else return output('', 302, [ 'Location' => $url ]); + } } if ( isset($files['folder']) || isset($files['file']) ) { return render_list($path, $files); @@ -347,6 +375,54 @@ function main($path) } } +function proxy_replace_domain($url, $domainforproxy) +{ + $tmp = splitfirst($url, '//'); + $http = $tmp[0]; + $tmp = splitfirst($tmp[1], '/'); + $domain = $tmp[0]; + $uri = $tmp[1]; + if (substr($domainforproxy, 0, 7)=='http://' || substr($domainforproxy, 0, 8)=='https://') $aim = $domainforproxy; + else $aim = $http . '//' . $domainforproxy; + if (substr($aim, -1)=='/') $aim = substr($aim, 0, -1); + return $aim . '/' . $uri . '&Origindomain=' . $domain; + //$url = str_replace($tmp, $domainforproxy, $url).'&Origindomain='.$tmp; +} + +function files_json($files) +{ + //$tmp = ''; + if (isset($files['file'])) { + $tmp['file']['type'] = 0; + $tmp['file']['id'] = $files['id']; + $tmp['file']['name'] = $files['name']; + $tmp['file']['time'] = $files['lastModifiedDateTime']; + $tmp['file']['size'] = $files['size']; + $tmp['file']['mime'] = $files['file']['mimeType']; + $tmp['file']['url'] = $files[$_SERVER['DownurlStrName']]; + $tmp['url'] = $files[$_SERVER['DownurlStrName']]; + } elseif (isset($files['folder'])) { + $tmp['list'] = []; + foreach ($files['children'] as $file) { + $tmp1 = null; + $tmp1 = []; + if (isset($file['file'])) { + $tmp1['type'] = 0; + $tmp1['url'] = $file[$_SERVER['DownurlStrName']]; + } elseif (isset($file['folder'])) { + $tmp1['type'] = 1; + } + $tmp1['id'] = $file['id']; + $tmp1['name'] = $file['name']; + $tmp1['time'] = $file['lastModifiedDateTime']; + $tmp1['size'] = $file['size']; + $tmp1['mime'] = $file['file']['mimeType']; + array_push($tmp['list'], $tmp1); + } + } else return output('', 404); + return output(json_encode($tmp)); +} + function get_access_token($refresh_token) { if (getConfig('Drive_ver')=='shareurl') { @@ -802,8 +878,15 @@ function get_thumbnails_url($path = '/', $location = 0) } } if ($thumb_url!='') { - if ($location) return output('', 302, [ 'Location' => $thumb_url ]); - else return output($thumb_url); + if ($location) { + $url = $thumb_url; + $domainforproxy = ''; + $domainforproxy = getConfig('domainforproxy'); + if ($domainforproxy!='') { + $url = proxy_replace_domain($url, $domainforproxy); + } + return output('', 302, [ 'Location' => $url ]); + } else return output($thumb_url); } return output('', 404); } @@ -1541,11 +1624,10 @@ function EnvOpt($needUpdate = 0) }*/ $response = setConfigResponse( setConfig($tmp, $_SERVER['disk_oprating']) ); if (api_error($response)) { - $html = api_error_msg($response); - $title = 'Error'; - } else { + $html = api_error_msg($response); + $title = 'Error'; + } else { //WaitSCFStat(); - //sleep(3); $html .= getconstStr('Success') . '!
'; $title = getconstStr('Setup'); @@ -1655,16 +1737,30 @@ function EnvOpt($needUpdate = 0) } $html .= ' '.getconstStr('AddDisk').'

'; - if (!((isset($_SERVER['USER'])&&$_SERVER['USER']==='qcloud')||(isset($_SERVER['HEROKU_APP_DIR'])&&$_SERVER['HEROKU_APP_DIR']==='/app'))) { + + $canOneKeyUpate = 0; + if (isset($_SERVER['USER'])&&$_SERVER['USER']==='qcloud') { + $canOneKeyUpate = 1; + } elseif (isset($_SERVER['HEROKU_APP_DIR'])&&$_SERVER['HEROKU_APP_DIR']==='/app') { + $canOneKeyUpate = 1; + } elseif (isset($_SERVER['FC_SERVER_PATH'])&&$_SERVER['FC_SERVER_PATH']==='/var/fc/runtime/php7.2') { + $canOneKeyUpate = 1; + } else { + $tmp = time(); + if ( mkdir(''.$tmp, 0777) ) { + rmdir(''.$tmp); + $canOneKeyUpate = 1; + } + } + if (!$canOneKeyUpate) { $html .= ' -'.getconstStr('VPSnotupdate').'
'; +'.getconstStr('CannotOneKeyUpate').'
'; } else { $html .= '
- - - - + + + @@ -1673,7 +1769,6 @@ function EnvOpt($needUpdate = 0) ', 'Program updating', 201); + } + return output('Jump', 302); + } + if ($_GET['install1']) { + //if ($_POST['admin']!='') { + $tmp['timezone'] = $_COOKIE['timezone']; + $AccessKeyID = getConfig('AccessKeyID'); + if ($AccessKeyID=='') { + $AccessKeyID = $_POST['AccessKeyID']; + $tmp['AccessKeyID'] = $AccessKeyID; + } + $AccessKeySecret = getConfig('AccessKeySecret'); + if ($AccessKeySecret=='') { + $AccessKeySecret = $_POST['AccessKeySecret']; + $tmp['AccessKeySecret'] = $AccessKeySecret; + } + $response = json_decode(SetbaseConfig($tmp, $_SERVER['accountId'], $_SERVER['region'], $_SERVER['service_name'], $_SERVER['function_name'], $AccessKeyID, $AccessKeySecret), true); + if (api_error($response)) { + $html = api_error_msg($response); + $title = 'Error'; + return message($html, $title, 201); + } else { + $html .= ' + +
+ +
+ '; + $title = getconstStr('SetAdminPassword'); + return message($html, $title, 201); + } + //} + } + if ($_GET['install0']) { + $html .= ' +
+language:
'; + foreach ($constStr['languages'] as $key1 => $value1) { + $html .= ' +
'; + } + if (getConfig('AccessKeyID')==''||getConfig('AccessKeySecret')=='') $html .= ' + '.getconstStr('Create').' AccessKeyID & AccessKeySecret
+
+
'; + $html .= ' + +
+ '; + $title = getconstStr('SelectLanguage'); + return message($html, $title, 201); + } + $html .= ''.getconstStr('ClickInstall').', '.getconstStr('LogintoBind'); + $title = 'Error'; + return message($html, $title, 201); +} + +function getfunctioninfo($accountId, $region, $service_name, $function_name, $AccessKeyID, $AccessKeySecret) +{ + $fcClient = new Client([ + "endpoint" => 'https://'.$accountId.'.'.$region.'.fc.aliyuncs.com', + "accessKeyID" => $AccessKeyID, + "accessKeySecret" => $AccessKeySecret + ]); + return $fcClient->getFunction($service_name, $function_name); +} + +function updateEnvironment($Envs, $accountId, $region, $service_name, $function_name, $AccessKeyID, $AccessKeySecret) +{ + //print_r($Envs); + $fcClient = new Client([ + "endpoint" => 'https://'.$accountId.'.'.$region.'.fc.aliyuncs.com', + "accessKeyID" => $AccessKeyID, + "accessKeySecret" => $AccessKeySecret + ]); + $tmp = $fcClient->getFunction($service_name, $function_name)['data']; + //$tmp = getfunctioninfo($accountId, $region, $service_name, $function_name, $AccessKeyID, $AccessKeySecret)['data']; + foreach ($tmp['environmentVariables'] as $key => $value ) { + $tmp_env[$key] = $value; + } + foreach ($Envs as $key1 => $value1) { + $tmp_env[$key1] = $value1; + } + $tmp_env = array_filter($tmp_env, 'array_value_isnot_null'); // remove null. 清除空值 + ksort($tmp_env); + + $tmpdata['functionName'] = $tmp['functionName']; + $tmpdata['description'] = $tmp['description']; + $tmpdata['memorySize'] = $tmp['memorySize']; + $tmpdata['timeout'] = $tmp['timeout']; + $tmpdata['runtime'] = $tmp['runtime']; + $tmpdata['handler'] = $tmp['handler']; + $tmpdata['environmentVariables'] = $tmp_env; + $tmpdata['code']['zipFile'] = base64_encode( file_get_contents($fcClient->getFunctionCode($service_name, $function_name)['data']['url']) ); + return $fcClient->updateFunction($service_name, $function_name, $tmpdata); +} + +function SetbaseConfig($Envs, $accountId, $region, $service_name, $function_name, $AccessKeyID, $AccessKeySecret) +{ + //echo json_encode($Envs,JSON_PRETTY_PRINT); + $fcClient = new Client([ + "endpoint" => 'https://'.$accountId.'.'.$region.'.fc.aliyuncs.com', + "accessKeyID" => $AccessKeyID, + "accessKeySecret" => $AccessKeySecret + ]); + $tmp = $fcClient->getFunction($service_name, $function_name)['data']; + // $tmp = getfunctioninfo($accountId, $region, $service_name, $function_name, $AccessKeyID, $AccessKeySecret)['data']; + foreach ($tmp['environmentVariables'] as $key => $value ) { + $tmp_env[$key] = $value; + } + foreach ($Envs as $key1 => $value1) { + $tmp_env[$key1] = $value1; + } + $tmp_env = array_filter($tmp_env, 'array_value_isnot_null'); // remove null. 清除空值 + ksort($tmp_env); + + $tmpdata['functionName'] = $function_name; + $tmpdata['description'] = 'Onedrive index and manager in Ali FC.'; + $tmpdata['memorySize'] = 128; + $tmpdata['timeout'] = 30; + $tmpdata['runtime'] = 'php7.2'; + $tmpdata['handler'] = 'index.handler'; + $tmpdata['environmentVariables'] = $tmp_env; + $tmpdata['code']['zipFile'] = base64_encode( file_get_contents($fcClient->getFunctionCode($service_name, $function_name)['data']['url']) ); + return $fcClient->updateFunction($service_name, $function_name, $tmpdata); +} + +function updateProgram($accountId, $region, $service_name, $function_name, $AccessKeyID, $AccessKeySecret, $source) +{ + //WaitSCFStat(); + $fcClient = new Client([ + "endpoint" => 'https://'.$accountId.'.'.$region.'.fc.aliyuncs.com', + "accessKeyID" => $AccessKeyID, + "accessKeySecret" => $AccessKeySecret + ]); + $tmp = $fcClient->getFunction($service_name, $function_name)['data']; + //$tmp = getfunctioninfo($accountId, $region, $service_name, $function_name, $AccessKeyID, $AccessKeySecret)['data']; + + $tmpdata['functionName'] = $tmp['functionName']; + $tmpdata['description'] = $tmp['description']; + $tmpdata['memorySize'] = $tmp['memorySize']; + $tmpdata['timeout'] = $tmp['timeout']; + $tmpdata['runtime'] = $tmp['runtime']; + $tmpdata['handler'] = $tmp['handler']; + $tmpdata['environmentVariables'] = $tmp['environmentVariables']; + $tmpdata['code']['zipFile'] = base64_encode( file_get_contents($source) ); + + return $fcClient->updateFunction($service_name, $function_name, $tmpdata); +} + +function api_error($response) +{ + return !isset($response['data']); +} + +function api_error_msg($response) +{ + return $response; + return $response['Error']['Code'] . '
+' . $response['Error']['Message'] . '

+function_name:' . $_SERVER['function_name'] . '
+Region:' . $_SERVER['Region'] . '
+namespace:' . $_SERVER['namespace'] . '
+'; +} + +function setConfigResponse($response) +{ + return $response; +} + +function OnekeyUpate($auth = 'qkqpttgf', $project = 'OneManager-php', $branch = 'master') +{ + $source = '/tmp/code.zip'; + $outPath = '/tmp/'; + + // 从github下载对应tar.gz,并解压 + $url = 'https://github.com/' . $auth . '/' . $project . '/tarball/' . $branch . '/'; + $tarfile = '/tmp/github.tar.gz'; + file_put_contents($tarfile, file_get_contents($url)); + $phar = new PharData($tarfile); + $html = $phar->extractTo($outPath, null, true);//路径 要解压的文件 是否覆盖 + + // 获取解压出的目录名 +/* + @ob_start(); + passthru('ls /tmp | grep '.$auth.'-'.$project.'',$stat); + $html.='状态:' . $stat . ' + 结果: + '; + $archivefolder = ob_get_clean(); + if (substr($archivefolder,-1)==PHP_EOL) $archivefolder = substr($archivefolder, 0, -1); + $outPath .= $archivefolder; + $html.=htmlspecialchars($archivefolder); + //return $html; +*/ + $tmp = scandir($outPath); + $name = $auth.'-'.$project; + foreach ($tmp as $f) { + if ( substr($f, 0, strlen($name)) == $name) { + $outPath .= $f; + break; + } + } + + // 将目录中文件打包成zip + $zip=new ZipArchive(); + if($zip->open($source, ZipArchive::CREATE)){ + addFileToZip($zip, $outPath); //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法 + $zip->close(); //关闭处理的zip文件 + } + + return updateProgram($_SERVER['accountId'], $_SERVER['region'], $_SERVER['service_name'], $_SERVER['function_name'], getConfig('AccessKeyID'), getConfig('AccessKeySecret'), $source); +} + +function addFileToZip($zip, $rootpath, $path = '') +{ + if (substr($rootpath,-1)=='/') $rootpath = substr($rootpath, 0, -1); + if (substr($path,0,1)=='/') $path = substr($path, 1); + $handler=opendir(path_format($rootpath.'/'.$path)); //打开当前文件夹由$path指定。 + while($filename=readdir($handler)){ + if($filename != "." && $filename != ".."){//文件夹文件名字为'.'和‘..’,不要对他们进行操作 + $nowname = path_format($rootpath.'/'.$path."/".$filename); + if(is_dir($nowname)){// 如果读取的某个对象是文件夹,则递归 + addFileToZip($zip, $rootpath, $path."/".$filename); + }else{ //将文件加入zip对象 + $zip->addFile($nowname); + $newname = $path."/".$filename; + if (substr($newname,0,1)=='/') $newname = substr($newname, 1); + $zip->renameName($nowname, $newname); + } + } + } + @closedir($path); +} diff --git a/platform/heroku.php b/platform/Heroku.php similarity index 100% rename from platform/heroku.php rename to platform/Heroku.php diff --git a/platform/normal.php b/platform/Normal.php similarity index 79% rename from platform/normal.php rename to platform/Normal.php index c56577b..0149a95 100644 --- a/platform/normal.php +++ b/platform/Normal.php @@ -252,12 +252,70 @@ Can not write config to file.
'; } -function OnekeyUpate() -{ - return json_decode(updateHerokuapp(getConfig('function_name'), getConfig('APIKey'))['body'], true); -} - function setConfigResponse($response) { return $response; } + +function OnekeyUpate($auth = 'qkqpttgf', $project = 'OneManager-php', $branch = 'master') +{ + // __DIR__ is xxx/platform + $projectPath = splitlast(__DIR__, '/')[0]; + + // 从github下载对应tar.gz,并解压 + $url = 'https://github.com/' . $auth . '/' . $project . '/tarball/' . $branch . '/'; + $tarfile = $projectPath.'/github.tar.gz'; + $githubfile = file_get_contents($url); + if (!$githubfile) return 0; + file_put_contents($tarfile, $githubfile); + if (splitfirst(PHP_VERSION, '.')[0] == '7') { + $phar = new PharData($tarfile); // need php7 + $phar->extractTo($projectPath, null, true);//路径 要解压的文件 是否覆盖 + } else { + ob_start(); + passthru('tar -xzvf '.$tarfile,$stat); + ob_get_clean(); + } + unlink($tarfile); + + $outPath = ''; + $tmp = scandir($projectPath); + $name = $auth.'-'.$project; + foreach ($tmp as $f) { + if ( substr($f, 0, strlen($name)) == $name) { + $outPath = $projectPath . '/' . $f; + break; + } + } + //error_log($outPath); + if ($outPath=='') return 0; + + //unlink($outPath.'/config.php'); + rename($projectPath.'/config.php', $outPath.'/config.php'); + + return moveFolder($outPath, $projectPath); +} + +function moveFolder($from, $to) +{ + if (substr($from, -1)=='/') $from = substr($from, 0, -1); + if (substr($to, -1)=='/') $to = substr($to, 0, -1); + if (!file_exists($to)) mkdir($to, 0777); + $handler=opendir($from); + while($filename=readdir($handler)) { + if($filename != '.' && $filename != '..'){ + $fromfile = $from.'/'.$filename; + $tofile = $to.'/'.$filename; + if(is_dir($fromfile)){// 如果读取的某个对象是文件夹,则递归 + moveFolder($fromfile, $tofile); + }else{ + //if (file_exists($tofile)) unlink($tofile); + rename($fromfile, $tofile); + if (file_exists($fromfile)) unlink($fromfile); + } + } + } + closedir($handler); + rmdir($from); + return 1; +} diff --git a/platform/scf.php b/platform/TencentSCF.php similarity index 96% rename from platform/scf.php rename to platform/TencentSCF.php index f37a687..497d110 100644 --- a/platform/scf.php +++ b/platform/TencentSCF.php @@ -23,6 +23,8 @@ function GetGlobalVariable($event) $pos = strpos($cookievalues,"="); $_COOKIE[urldecode(substr($cookievalues,0,$pos))]=urldecode(substr($cookievalues,$pos+1)); } + $_SERVER['HTTP_USER_AGENT'] = $event['headers']['user-agent']; + $_SERVER['HTTP_TRANSLATE']==$event['headers']['translate'];//'f' $_SERVER['USER'] = 'qcloud'; }