From dee127ef64bb81500c7c4e7b16521aabdad8eb9a Mon Sep 17 00:00:00 2001 From: qkqpttgf <45693631+qkqpttgf@users.noreply.github.com> Date: Mon, 13 Jul 2020 17:43:22 +0800 Subject: [PATCH] Create HuaweiFG.php --- platform/HuaweiFG.php | 743 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 743 insertions(+) create mode 100644 platform/HuaweiFG.php diff --git a/platform/HuaweiFG.php b/platform/HuaweiFG.php new file mode 100644 index 0000000..a2d198e --- /dev/null +++ b/platform/HuaweiFG.php @@ -0,0 +1,743 @@ +geteventID(); + $tmp['RemainingTimeInMilliSeconds'] = $context->getRemainingTimeInMilliSeconds(); + $tmp['AccessKey'] = $context->getAccessKey(); + $tmp['SecretKey'] = $context->getSecretKey(); + $tmp['UserData']['HW_urn'] = $context->getUserData('HW_urn'); + $tmp['FunctionName'] = $context->getFunctionName(); + $tmp['RunningTimeInSeconds'] = $context->getRunningTimeInSeconds(); + $tmp['Version'] = $context->getVersion(); + $tmp['MemorySize'] = $context->getMemorySize(); + $tmp['CPUNumber'] = $context->getCPUNumber(); + $tmp['ProjectID'] = $context->getProjectID(); + $tmp['Package'] = $context->Package(); + $tmp['Token'] = $context->getToken(); + $tmp['Logger'] = $context->getLogger(); + + if (strlen(json_encode($event['body']))>500) $event['body']=substr($event['body'],0,strpos($event['body'],'base64')+30) . '...Too Long!...' . substr($event['body'],-50); + echo urldecode(json_encode($event, JSON_PRETTY_PRINT)) . ' + +' . urldecode(json_encode($tmp, JSON_PRETTY_PRINT)) . ' + +'; +} + +function GetGlobalVariable($event) +{ + $_GET = $event['queryStringParameters']; + $postbody = explode("&",$event['body']); + foreach ($postbody as $postvalues) { + $pos = strpos($postvalues,"="); + $_POST[urldecode(substr($postvalues,0,$pos))]=urldecode(substr($postvalues,$pos+1)); + } + $cookiebody = explode("; ",$event['headers']['cookie']); + foreach ($cookiebody as $cookievalues) { + $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['_APP_SHARE_DIR'] = '/var/share/CFF/processrouter'; +} + +function GetPathSetting($event, $context) +{ + $_SERVER['firstacceptlanguage'] = strtolower(splitfirst(splitfirst($event['headers']['accept-language'],';')[0],',')[0]); + $_SERVER['function_name'] = $context->getFunctionName(); + $_SERVER['ProjectID'] = $context->getProjectID(); + $host_name = $event['headers']['host']; + $_SERVER['HTTP_HOST'] = $host_name; + $path = path_format($event['pathParameters'][''].'/'); + $_SERVER['base_path'] = path_format($event['path'].'/'); + if ( $_SERVER['base_path'] == $path ) { + $_SERVER['base_path'] = '/'; + } else { + $_SERVER['base_path'] = substr($_SERVER['base_path'], 0, -strlen($path)); + if ($_SERVER['base_path']=='') $_SERVER['base_path'] = '/'; + } + if (substr($path,-1)=='/') $path=substr($path,0,-1); + $_SERVER['is_guestup_path'] = is_guestup_path($path); + $_SERVER['PHP_SELF'] = path_format($_SERVER['base_path'] . $path); + $_SERVER['REMOTE_ADDR'] = $event['headers']['x-real-ip']; + $_SERVER['HTTP_X_REQUESTED_WITH'] = $event['headers']['x-requested-with']; + return $path; +} + +function getConfig($str, $disktag = '') +{ + global $InnerEnv; + global $Base64Env; + global $contextUserData; + if (in_array($str, $InnerEnv)) { + if ($disktag=='') $disktag = $_SERVER['disktag']; + $env = json_decode($contextUserData->getUserData($disktag), true); + if (isset($env[$str])) { + if (in_array($str, $Base64Env)) return equal_replace($env[$str],1); + else return $env[$str]; + } + } else { + if (in_array($str, $Base64Env)) return equal_replace($contextUserData->getUserData($str),1); + else return $contextUserData->getUserData($str); + } + return ''; +} + +function setConfig($arr, $disktag = '') +{ + global $InnerEnv; + global $Base64Env; + global $contextUserData; + if ($disktag=='') $disktag = $_SERVER['disktag']; + $disktags = explode("|",getConfig('disktag')); + $diskconfig = json_decode($contextUserData->getUserData($disktag), true); + $tmp = []; + $indisk = 0; + $oparetdisk = 0; + foreach ($arr as $k => $v) { + if (in_array($k, $InnerEnv)) { + if (in_array($k, $Base64Env)) $diskconfig[$k] = equal_replace($v); + else $diskconfig[$k] = $v; + $indisk = 1; + } elseif ($k=='disktag_add') { + array_push($disktags, $v); + $oparetdisk = 1; + } elseif ($k=='disktag_del') { + $disktags = array_diff($disktags, [ $v ]); + $tmp[$v] = ''; + $oparetdisk = 1; + } else { + if (in_array($k, $Base64Env)) $tmp[$k] = equal_replace($v); + else $tmp[$k] = $v; + } + } + if ($indisk) { + $diskconfig = array_filter($diskconfig, 'array_value_isnot_null'); + ksort($diskconfig); + $tmp[$disktag] = json_encode($diskconfig); + } + if ($oparetdisk) { + $disktags = array_unique($disktags); + foreach ($disktags as $disktag) if ($disktag!='') $disktag_s .= $disktag . '|'; + if ($disktag_s!='') $tmp['disktag'] = substr($disktag_s, 0, -1); + else $tmp['disktag'] = ''; + } +// echo '正式设置:'.json_encode($tmp,JSON_PRETTY_PRINT).' +//'; + $response = updateEnvironment($tmp, getConfig('HW_urn'), getConfig('HW_key'), getConfig('HW_secret')); + // WaitSCFStat(); + return $response; +} + +function WaitSCFStat() +{ + $trynum = 0; + while( json_decode(getfunctioninfo($_SERVER['function_name'], $_SERVER['Region'], $_SERVER['namespace'], getConfig('SecretId'), getConfig('SecretKey')),true)['Response']['Status']!='Active' ) echo ' +'.++$trynum; +} + +function install() +{ + global $constStr; + if ($_GET['install2']) { + $tmp['admin'] = $_POST['admin']; + setConfig($tmp); + if (needUpdate()) { + OnekeyUpate(); + return message('update to github version, reinstall. + + ', 'Program updating', 201); + } + return output('Jump + + ', 302); + } + if ($_GET['install1']) { + //if ($_POST['admin']!='') { + $tmp['timezone'] = $_COOKIE['timezone']; + $tmp['HW_urn'] = getConfig('HW_urn'); + if ($tmp['HW_urn']=='') { + $tmp['HW_urn'] = $_POST['HW_urn']; + } + $tmp['HW_key'] = getConfig('HW_key'); + if ($tmp['HW_key']=='') { + $tmp['HW_key'] = $_POST['HW_key']; + } + $tmp['HW_secret'] = getConfig('HW_secret'); + if ($tmp['HW_secret']=='') { + $tmp['HW_secret'] = $_POST['HW_secret']; + } + //$response = json_decode(SetbaseConfig($tmp, $HW_urn, $HW_name, $HW_pwd), true)['Response']; + $response = setConfigResponse( SetbaseConfig($tmp, $tmp['HW_urn'], $tmp['HW_key'], $tmp['HW_secret']) ); + 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 .= ' + + '; + $title = getconstStr('SelectLanguage'); + return message($html, $title, 201); + } + $html .= ''.getconstStr('ClickInstall').', '.getconstStr('LogintoBind'); + $title = 'Error'; + return message($html, $title, 201); +} + +function getfunctioninfo($HW_urn, $HW_key, $HW_secret) +{ + $URN = explode(':', $HW_urn); + $Region = $URN[2]; + $project_id = $URN[3]; + $url = 'https://functiongraph.' . $Region . '.myhuaweicloud.com/v2/' . $project_id . '/fgs/functions/' . $HW_urn . '/config'; + $signer = new Signer(); + $signer->Key = $HW_key; + $signer->Secret = $HW_secret; + $req = new Request('GET', $url); + $req->headers = array( + 'content-type' => 'application/json;charset=utf8', + ); + $req->body = ''; + $curl = $signer->Sign($req); + $response = curl_exec($curl); + $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); + curl_close($curl); + return $response; +} + + +function updateEnvironment($Envs, $HW_urn, $HW_key, $HW_secret) +{ + //echo json_encode($Envs,JSON_PRETTY_PRINT); + global $contextUserData; + $tmp_env = json_decode(json_decode(getfunctioninfo($HW_urn, $HW_key, $HW_secret),true)['user_data'],true); + foreach ($Envs as $key1 => $value1) { + $tmp_env[$key1] = $value1; + } + $tmp_env = array_filter($tmp_env, 'array_value_isnot_null'); // remove null. 清除空值 + ksort($tmp_env); + + $URN = explode(':', $HW_urn); + $Region = $URN[2]; + $project_id = $URN[3]; + $url = 'https://functiongraph.' . $Region . '.myhuaweicloud.com/v2/' . $project_id . '/fgs/functions/' . $HW_urn . '/config'; + $signer = new Signer(); + $signer->Key = $HW_key; + $signer->Secret = $HW_secret; + $req = new Request('PUT', $url); + $req->headers = array( + 'content-type' => 'application/json;charset=utf8', + ); + $tmpdata['handler'] = 'index.handler'; + $tmpdata['memory_size'] = $contextUserData->getMemorySize()+1-1; + $tmpdata['runtime'] = 'PHP7.3'; + $tmpdata['timeout'] = $contextUserData->getRunningTimeInSeconds()+1-1; + $tmpdata['user_data'] = json_encode($tmp_env); + $req->body = json_encode($tmpdata); + $curl = $signer->Sign($req); + $response = curl_exec($curl); + $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); + curl_close($curl); + return $response; +} + +function SetbaseConfig($Envs, $HW_urn, $HW_key, $HW_secret) +{ + //echo json_encode($Envs,JSON_PRETTY_PRINT); + $tmp_env = json_decode(json_decode(getfunctioninfo($HW_urn, $HW_key, $HW_secret),true)['user_data'],true); + foreach ($Envs as $key1 => $value1) { + $tmp_env[$key1] = $value1; + } + $tmp_env = array_filter($tmp_env, 'array_value_isnot_null'); // remove null. 清除空值 + ksort($tmp_env); + + // https://functiongraph.cn-north-4.myhuaweicloud.com/v2/{project_id}/fgs/functions/{function_urn}/config + $URN = explode(':', $HW_urn); + $Region = $URN[2]; + $project_id = $URN[3]; + $url = 'https://functiongraph.' . $Region . '.myhuaweicloud.com/v2/' . $project_id . '/fgs/functions/' . $HW_urn . '/config'; + $signer = new Signer(); + $signer->Key = $HW_key; + $signer->Secret = $HW_secret; + $req = new Request('PUT', $url); + $req->headers = array( + 'content-type' => 'application/json;charset=utf8', + ); + $tmpdata['handler'] = 'index.handler'; + $tmpdata['memory_size'] = 128; + $tmpdata['runtime'] = 'PHP7.3'; + $tmpdata['timeout'] = 30; + $tmpdata['user_data'] = json_encode($tmp_env); + $req->body = json_encode($tmpdata); + $curl = $signer->Sign($req); + $response = curl_exec($curl); + $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); + curl_close($curl); + return $response; +} + +function updateProgram($HW_urn, $HW_key, $HW_secret, $source) +{ + $URN = explode(':', $HW_urn); + $Region = $URN[2]; + $project_id = $URN[3]; + $url = 'https://functiongraph.' . $Region . '.myhuaweicloud.com/v2/' . $project_id . '/fgs/functions/' . $HW_urn . '/code'; + $signer = new Signer(); + $signer->Key = $HW_key; + $signer->Secret = $HW_secret; + $req = new Request('PUT', $url); + $req->headers = array( + 'content-type' => 'application/json;charset=utf8', + ); + $tmpdata['code_type'] = 'zip'; + $tmpdata['func_code']['file'] = base64_encode( file_get_contents($source) ); + $req->body = json_encode($tmpdata); + $curl = $signer->Sign($req); + $response = curl_exec($curl); + $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); + curl_close($curl); + return $response; +} + +function api_error($response) +{ + return isset($response['error_code']); +} + +function api_error_msg($response) +{ + return $response['error_code'] . '