Rev 130 | Rev 218 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
<?php/*QBPWCF, Quick Build PHP website Component base on Fedora Linux.Copyright (C) 2015~2025 Min-Jhin,ChenThis file is part of QBPWCF.QBPWCF is free software: you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation, either version 3 of the License, or(at your option) any later version.QBPWCF is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with QBPWCF. If not, see <http://www.gnu.org/licenses/>.*/namespace qbpwcf;/*類別說明:執行外部程式的類別.備註:無.*/class external{/*#函式說明:#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$reuslt["error"],執行不正常結束的錯訊息陣列.#$result["function"],當前執行的函式名稱.#必填參數:#$method,物件,為物件實體或類別名稱,會自動置入該參數.#$arguments,陣列,為呼叫方法時所用的參數.#可省略參數:#無.#參考資料:#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic*/public function __call($method,$arguments){#取得當前執行的函式$result["function"]=__FUNCTION__;#設置執行不正常$result["status"]="false";#設置執行錯誤$result["error"][]=__NAMESPACE__ ."/".$method."() 不存在!";#設置所丟入的參數$result["error"][]=$arguments;#回傳結果return $result;}#function __call end/*#函式說明:#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$reuslt["error"],執行不正常結束的錯訊息陣列.#$result["function"],當前執行的函式名稱.#必填參數:#$method,物件,為物件實體或類別名稱,會自動置入該參數.#$arguments,陣列,為呼叫方法時所用的參數.#可省略參數:#無.#參考資料:#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic*/public static function __callStatic($method,$arguments){#取得當前執行的函式$result["function"]=__FUNCTION__;#設置執行不正常$result["status"]="false";#設置執行錯誤$result["error"][]="欲呼叫的". __NAMESPACE__ ."/".$method."() 不存在!";#設置所丟入的參數$result["error"][]=$arguments;#回傳結果return $result;}#function __callStatic end/*#函式說明:#測試php呼叫java後回傳的結果#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["function"],當前執行的函式名稱.#$result["error"],錯誤訊息陣列#$result["cmd"],執行的指令內容.#$result["output"],爲執行完java class檔案後的輸出陣列#$result["outputFromFile"]["fileContent"],取得輸出的內容陣列.#$result["outputFromFile"]["lineCount"],取得輸出內容總共的行數.#$result["outputFromFile"]["fullContent"],取得輸出的內容陣列.#必填參數:$conf["javaClassFileAddress"]="";#java class檔案的位置與名稱,附檔名(.class)不用附上。#可省略參數:#$conf["arguments"]="";#要給java class檔案的參數#參考資料:#http://php.net/manual/en/function.exec.php#備註:#目前無法透過apache的身份執行java程式.*/public static function callJava($conf){#初始化要回傳的內容$result=array();#取得當前執行的函式$result["function"]=__FUNCTION__;#如果 $conf 不為陣列if(gettype($conf)!="array"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"][]="\$conf變數須為陣列形態";#回傳結果return $result;}#if end#如果$conf["javaClassFileAddress"]沒有設定if(!isset($conf["javaClassFileAddress"])){#設置執行錯誤$result["status"]="false";#設置錯誤訊息$result["error"][]="\$conf[\"javaClassFileAddress\"]參數不存在!";#回傳結果return $result;}#if end#如果$conf["arguments"]沒有設置if(!isset($conf["arguments"])){#設爲""$conf["arguments"]="";}#if end#執行java程式#函式說明:#呼叫shell執行系統命令,並取得回傳的內容.#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息陣列#$result["function"],當前執行的函數名稱#$result["cmd"],執行的指令內容.#$result["outputFromFile"]["fileContent"],取得輸出的內容陣列.#$result["outputFromFile"]["lineCount"],取得輸出內容總共的行數.#$result["outputFromFile"]["fullContent"],取得輸出的內容陣列.#必填參數:#$conf["external::callShell"]["command"],字串.$conf["external::callShell"]["command"]="java";#要執行的指令與參數.#可省略參數:#$conf["external::callShell"]["argu"],陣列字串,指令搭配的參數,預設為空陣列.$conf["external::callShell"]["argu"]=array($conf["javaClassFileAddress"],$conf["arguments"]);#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".#$conf["enablePrintDescription"]="true";#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容.#$conf["printDescription"]="";$conf["external::callShell"]["escapeshellarg"]="true";#參考資料:#http://php.net/manual/en/function.exec.php$callShell=external::callShell($conf["external::callShell"]);unset($conf["external::callShell"]);#debug#var_dump($callShell);#exit;#若執行外部 java class 程式回傳的狀態為 "false"if($callShell["status"]=="false"){#代表執行外部程式不正常.#設置錯誤識別$result["status"]="false";#設置錯誤訊息$result["error"]=$callShell;#回傳結果return $result;}#if end#取得輸出的內容陣列.$result["outputFromFile"]["fileContent"]=$callShell["outputFromFile"]["fileContent"];#取得輸出內容總共的行數.$result["outputFromFile"]["lineCount"]=$callShell["outputFromFile"]["lineCount"];#取得輸出的內容陣列.$result["outputFromFile"]["fullContent"]=$callShell["outputFromFile"]["fullContent"];#取得執行的指令內容$result["cmd"]=$callShell["cmd"];#執行到這邊代表執行正常$result["status"]="true";#取得輸出的內容$result["output"]=$callShell["output"];#回傳結果return $result;}#function testPheCallJava end/*#函式說明:#呼叫shell執行編譯過的2元碼程式或具備執行權限的檔案後回傳的結果#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["function"],正在執行的函式#$result["error"],錯誤訊息陣列.#$result["error"]["returnCode"],錯誤代碼.#$result["output"],爲執行完二元碼後的輸出陣列.#$result["escapedCmd"],實際執行的指令.#必填參數:$conf["exeFileAddress"]="";#可執行檔的位置與名稱#可省略參數:#$conf["arguments"]="";#要給執行檔的參數#參考資料:#http://php.net/manual/en/function.exec.php#備註:#無.*/public static function execByteCode($conf){#初始化要回傳的結果$result=array();#取得當前執行的函式$result["function"]=__FUNCTION__;#如果 $conf 不為陣列if(gettype($conf)!="array"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"][]="\$conf變數須為陣列形態";#如果傳入的參數為 nullif($conf==null){#設置執行錯誤訊息$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";}#if end#回傳結果return $result;}#if end#如果$conf["exeFileAddress"]沒有設定if(!isset($conf["exeFileAddress"])){#設置執行錯誤識別$result["status"];#設置錯誤訊息$result["error"][]="\$conf[\"exeFileAddress\"]參數不存在!";#回傳結果return $result;}#if end#初始化要使用的參數$escapedShellArg="";#如果$conf["arguments"]沒有設置if(isset($conf["arguments"])){#取得已經處理好的參數$escapedShellArg=" ".escapeshellarg($conf["arguments"]);}#if end#初始化執行的語法$runStr=$conf["exeFileAddress"];#如果沒有路徑的符號if(strpos($runStr,"/")===false){#代表要執行的程式在當前路徑$runStr="./".$runStr;}#if end#處理執行的語法$runStr=escapeshellarg($runStr);#取得處理好的指令$escapeShellCmd=$runStr.$escapedShellArg;#儲存實際執行的指令$result["escapedCmd"]=$escapeShellCmd;#執行腳本程式,將輸出結果儲存在 $resultArray 陣列裏面exec($escapeShellCmd,$resultArray,$execStatus);#取得執行外部程式的輸出$result["output"]=$resultArray;#若執行外部程式回傳的狀態不為 0if($execStatus!=0){#代表執行外部程式不正常.#設置錯誤識別$result["status"]="false";#設置錯誤代碼$result["error"]["returnCode"]=$execStatus;#設置錯誤訊息$result["error"][]="執行".$conf["exeFileAddress"]."意外結束!";#回傳結果return $result;}#if end#執行到這邊代表執行正常$result["status"]="true";#回傳結果return $result;}#function execByteCode end/*#函式說明:#解析指令與參數,回傳指令與參數給 callShell 函式使用.#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息陣列.#$result["function"],當前執行的函數名稱.#$result["argu"],使用的參數.#$result["cmd"],執行的指令名稱.#$result["params"],執行指令伴隨的參數.#必填參數:#$conf["cmdArray"],字串陣列,要執行的指令字串.$conf["cmdArray"]=array();#可省略參數:#無.#參考資料:#array_shift=>https://www.php.net/manual/en/function.array-shift.php#備註:#無.*/public static function callShellHelper(&$conf){#初始化要回傳的結果$result=array();#取得當前執行的函數名稱$result["function"]=__FUNCTION__;#如果 $conf 不為陣列if(gettype($conf)!="array"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"][]="\$conf變數須為陣列形態";#如果傳入的參數為 nullif($conf==null){#設置執行錯誤訊息$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";}#if end#回傳結果return $result;}#if end#取得參數$result["argu"]=$conf;#參數檢查#函式說明:#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$reuslt["error"],執行不正常結束的錯訊息陣列.#$result["function"],當前執行的函式名稱.#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過#$result[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在#$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確#$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息#必填寫的參數:#$conf["variableCheck::checkArguments"]["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。$conf["variableCheck::checkArguments"]["varInput"]=&$conf;#$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("cmdArray");#$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double");$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("array");#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";#可以省略的參數:#$conf["variableCheck::checkArguments"]["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true"。$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";#$conf["variableCheck::checkArguments"]["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");#$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("pre","enablePrintDescription","printDescription","argu","escapeshellarg","username","password","useScript","logFilePath","inBackGround","arguIsAddr","getErr","plainArgu","useApostrophe","doNotRun","thereIsShellVar");#$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("pre","enablePrintDescription","printDescription","argu","escapeshellarg","username","password","useScript","logFilePath","inBackGround","arguIsAddr","getErr","doNotRun","thereIsShellVar");#$conf["variableCheck::checkArguments"]["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");#$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("array","string","string","array","string","string","string","string","string","string","array","string","array","array","string","array");#$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("array","string","string","array","string","string","string","string","string","string","array","string","string","array");#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,"null"代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(null,"false",null,null,"false",null,null,"false",".qbpwcf_tmp/external/callShell/","false",null,"false",null,null,"false",null);#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(null,"false",null,null,"false",null,null,"false",".qbpwcf_tmp/external/callShell/","false",null,"false","false",null);#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"][]=array("argu","arguIsAddr");$checkResult=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);unset($conf["variableCheck::checkArguments"]);#如果檢查參數失敗if($checkResult["status"]=="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤$result["error"]=$checkResult;#回傳結果return $result;}#if end#如果檢查參數不通過if($checkResult["passed"]=="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤$result["error"]=$checkResult;#回傳結果return $result;}#if end#取得使用的指令$result["cmd"]=array_shift($conf["cmdArray"]);#取得使用的參數$result["params"]=$conf["cmdArray"];#設置執行正常$result["status"]="true";#回傳結果return $result;}#function callShellHelper end/*#函式說明:#呼叫shell執行系統命令,並取得回傳的內容.#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息陣列.#$result["function"],當前執行的函數名稱.#$result["argu"],使用的參數.#$result["cmd"],執行的指令內容.#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.#$result["output"],爲執行完後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.#$result["content"],為執行完後的輸出字串.#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.#$result["running"],是否還在執行.#$result["pid"],pid.#$result["statusCode"],執行結束後的代碼.#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").#$result["noEcaped"],陣列,儲存重新排序過未經過escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").#必填參數:#$conf["command"],字串,要執行的指令.$conf["command"]="";#$conf["fileArgu"],字串,變數__FILE__的內容.$conf["fileArgu"]=__FILE__;#可省略參數:#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.#$conf["argu"]=array("");#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").#$conf["arguIsAddr"]=array();#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".#$conf["enablePrintDescription"]="true";#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.#$conf["printDescription"]="";#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.#$conf["escapeshellarg"]="false";#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.#$conf["thereIsShellVar"]=array();#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.#$conf["username"]="";#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.#$conf["password"]="";#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".#$conf["useScript"]="";#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.#$conf["inBackGround"]="";#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.#$conf["getErr"]="false";#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.#$conf["doNotRun"]="false";#參考資料:#exec=>http://php.net/manual/en/function.exec.php#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php#備註:#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.#疑似檢查要執行的指令是否存在的shell script會有異常報錯,目前抓不到原因,但不影響執行結果.*/public static function callShell(&$conf){#初始化要回傳的結果$result=array();#取得當前執行的函數名稱$result["function"]=__FUNCTION__;#如果 $conf 不為陣列if(gettype($conf)!="array"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"][]="\$conf變數須為陣列形態";#如果傳入的參數為 nullif($conf==null){#設置執行錯誤訊息$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";}#if end#回傳結果return $result;}#if end#取得參數$result["argu"]=$conf;#參數檢查#函式說明:#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$reuslt["error"],執行不正常結束的錯訊息陣列.#$result["function"],當前執行的函式名稱.#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過#$result[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在#$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確#$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息#必填寫的參數:#$conf["variableCheck::checkArguments"]["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。$conf["variableCheck::checkArguments"]["varInput"]=&$conf;#$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("command","fileArgu");#$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double");$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string");#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";#可以省略的參數:#$conf["variableCheck::checkArguments"]["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true"。#$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";#$conf["variableCheck::checkArguments"]["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("pre","enablePrintDescription","printDescription","argu","escapeshellarg","username","password","useScript","logFilePath","inBackGround","arguIsAddr","getErr","doNotRun","thereIsShellVar");#$conf["variableCheck::checkArguments"]["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("array","string","string","array","string","string","string","string","string","string","array","string","string","array");#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,"null"代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(null,"false",null,null,"false",null,null,"false",".qbpwcf_tmp/external/callShell/","false",null,"false","false",null);#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"][]=array("argu","arguIsAddr");$checkResult=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);unset($conf["variableCheck::checkArguments"]);#如果檢查參數失敗if($checkResult["status"]=="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤$result["error"]=$checkResult;#回傳結果return $result;}#if end#如果檢查參數不通過if($checkResult["passed"]=="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤$result["error"]=$checkResult;#回傳結果return $result;}#if end#如果是web環境,則不能執行script指令#涵式說明:#判斷當前環境為web還是cmd#回傳結果:#$result,"web"或"cmd"if(csInformation::getEnv()==="web" && $conf["useScript"]==="true"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"][]="函數 ".$result["function"]." 使用參數 useScript 為 ".$conf["useScript"]." 時僅能在命令列環境下運行!";#回傳結果return $result;}#if end#如果有設置 $conf["pre"]if(isset($conf["pre"])){#初始化存放新的參數$newArgu=array();#初始化那些參數含有 shell 變數$thereIsShellVar=array();#debug#var_dump($conf["pre"]);#記錄原先的指令$oriCmd=$conf["command"];#針對每個前置指令foreach($conf["pre"] as $index=>$info){#debug#var_dump($index);#如果是第一個要執行的 pre 指令if($index===0){#更新 指令的名稱$conf["command"]=$info["cmd"];}#if end#反之為後面的 pre 指令else{#增加參數$newArgu[]=$info["cmd"];#設置不含有 shell 變數$thereIsShellVar[]="false";}#else end#針對每個參數foreach($info["param"] as $param){#增加參數$newArgu[]=$param;#設置不含有 shell 變數$thereIsShellVar[]="false";}#foreach end#增加分號$newArgu[]=";";#設置不含有 shell 變數$thereIsShellVar[]="false";}#foreach end#原本的指令變成參數$newArgu[]=$oriCmd;#設置不含有 shell 變數$thereIsShellVar[]="false";#debug#var_dump($conf["argu"]);#針對本來要執行的參數foreach($conf["argu"] as $index => $param){#增加參數$newArgu[]=$param;#如果有設置是否含有 shell 變數if(isset($conf["thereIsShellVar"])){#設置是否含有 shell 變數$thereIsShellVar[]=$conf["thereIsShellVar"][$index];}#if end#反之else{#設置不含有 shell 變數$thereIsShellVar[]="false";}#else end}#foreach end#debug#var_dump($newArgu);#更新 argu 參數$conf["argu"]=$newArgu;#更新 thereIsShellVar 參數$conf["thereIsShellVar"]=$thereIsShellVar;#移除處理掉的 pre 參數unset($conf["pre"]);#debug#var_dump($conf);#遞迴呼叫return self::callShell($conf);}#if end#如果沒有設置要印出來的描述內容if(!isset($conf["printDescription"])){#預設為要執行的指令$conf["printDescription"]=$conf["command"];#如果有設置參數if(isset($conf["argu"])){#針對每個參數foreach($conf["argu"] as $argu){#若有參數則加在後面$conf["printDescription"]=$conf["printDescription"]." ".$argu;}#foreach end}#if end}#if end#如果要印出 $conf["printDescription"] 的內容if($conf["enablePrintDescription"]=="true"){#輸出文字echo "執行「".$conf["printDescription"]."」命令\n";}#if end#初始化執行的指令$result["cmd"]=$conf["command"];#初始化輸出$output=array();#取得 PATHexec("echo \$PATH",$output,$status);#如果執行失敗if($status!==0){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"]="取得系統環境變數 PATH 失敗";#回傳結果return $result;}#if end#取得 PATH 設定$PATHS=$output[0];#函式說明:#將固定格式的字串分開,並回傳分開的結果.#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息陣列#$result["function"],當前執行的函數名稱.#$result["argu"],使用的參數.#$result["oriStr"],要分割的原始字串內容#$result["dataArray"],爲分割好字串的陣列內容,$result["dataArray"][$i]爲第($i+1)段的內容。#$result["dataCounts"],爲總共分成幾段#$result["found"],是否有在$conf["stringIn"]找到$conf["spiltSymbol"],"true"代表有找到,"false"代表沒有找到.#必填參數:#$conf["stringIn"],字串,要處理的字串.$conf["stringProcess::spiltString"]["stringIn"]=$PATHS;#$conf["spiltSymbol"],字串,爲以哪個符號作爲分割.$conf["stringProcess::spiltString"]["spiltSymbol"]=":";#可省略參數:#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";#參考資料:#無.#備註:#無.$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);unset($conf["stringProcess::spiltString"]);#如果分割字串失敗if($spiltString["status"]==="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"]="取得系統環境變數 PATH 失敗";#回傳結果return $result;}#if end#取得PATH字串陣列$PATHS=$spiltString["dataArray"];#預設沒有找到程式$cmdFound=false;#如果要執行的程式有指定位置if(strpos($result["cmd"],"/")!==false){#要運行的指令$cmd="targetToCheck='".$result["cmd"]."';".PHP_EOL."if [ -f \${targetToCheck} ]; then".PHP_EOL."exit;".PHP_EOL."else".PHP_EOL."exit 1;".PHP_EOL."fi";#初始化輸出$output=array();#執行指令exec($cmd,$output,$status);/*#debug$fh=fopen("/tmp/external::callShell.log","a");fwrite($fh,PHP_EOL."start".PHP_EOL);fwrite($fh,$result["cmd"].PHP_EOL);fwrite($fh,$cmd.PHP_EOL);fwrite($fh,print_r($ouput,true).PHP_EOL);fwrite($fh,$status.PHP_EOL);fwrite($fh,PHP_EOL."end".PHP_EOL);fclose($fh);*/#如果有找到if($status===0){#設置有找到$cmdFound=true;}#if end}#if end#反之else{#針對每個 $PATHSforeach($PATHS as $path){#要運行的指令$cmd="targetToCheck='".$path."/".$result["cmd"]."';".PHP_EOL."if [ -f \${targetToCheck} ]; then".PHP_EOL."exit;".PHP_EOL."else".PHP_EOL."exit 1;".PHP_EOL."fi";#初始化輸出$output=array();#執行指令exec($cmd,$output,$status);/*#debug$fh=fopen("/tmp/external::callShell.log","a");fwrite($fh,PHP_EOL."start".PHP_EOL);fwrite($fh,$result["cmd"].PHP_EOL);fwrite($fh,$cmd.PHP_EOL);fwrite($fh,print_r($ouput,true).PHP_EOL);fwrite($fh,$status.PHP_EOL);fwrite($fh,PHP_EOL."end".PHP_EOL);fclose($fh);*/#如果執行正常if($status===0){#代表有找到程式$cmdFound=true;#跳離 foreachbreak;}#if end}#foreach end}#else end#如果找不到要執行的程式if($cmdFound===false){$equalPosition=strpos($result["cmd"],"=");#若指令不為 shell 變數指派if( $equalPosition===false ){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"]="要執行的程式「".$result["cmd"]."」不存在!";#回傳結果return $result;}#if end#代表有找到程式$cmdFound=true;}#if end#如果 $conf["escapeshellarg"] 為 "true"if($conf["escapeshellarg"]==="true"){#取得真正執行的指令$result["escape"]["cmd"]=$result["cmd"];#如果有參數存在if(isset($conf["argu"])){#有幾個參數就執行幾次for($i=0;$i<count($conf["argu"]);$i++){#如果有指定位置參數if(isset($conf["arguIsAddr"])){#如果是位置參數if($conf["arguIsAddr"][$i]==="true"){#如果相對路徑就要轉換路徑if(strpos($conf["argu"][$i],"/")!==0){#函數說明:#將多個路徑字串變成相對於當前路徑的相對路徑字串#回傳結果:#$result["status"],"true"爲建立成功,"false"爲建立失敗.#$result["error"],錯誤訊息陣列.#$result["function"],函數名稱.#$result["content"],字串陣列,多個轉換好的相對路徑字串.#必填參數:#$conf["path"],陣列字串,要轉換成相對路徑的字串.;$conf["fileAccess::getRelativePath"]["path"]=array($conf["argu"][$i]);#$conf["fileArgu"],字串,當前路徑.$conf["fileAccess::getRelativePath"]["fileArgu"]=$conf["fileArgu"];$getRelativePath=fileAccess::getRelativePath($conf["fileAccess::getRelativePath"]);unset($conf["fileAccess::getRelativePath"]);#var_dump($getRelativePath);#如果轉換路徑失敗if($getRelativePath["status"]==="false"){#設置執行失敗的識別$result["status"]="false";#設置執行失敗的訊息$result["error"]=$getRelativePath;#回傳結果return $result;}#if end#取得相對路徑$conf["argu"][$i]=$getRelativePath["content"][0];#取得相對路徑$relativePath=$getRelativePath["content"][0];#保存其相對路徑$conf["argu"][$i]=$relativePath;}#if end}#if end}#if end#如果參數長度大於3if(strlen($conf["argu"][$i])>3){#如果參數是 ${xxx}if( strpos($conf["argu"][$i],"\${")===0 && strpos($conf["argu"][$i],"}")===(strlen($conf["argu"][$i])-1) ){#預設要空格$space=" ";#如果前一個參數為 "=" 結尾if($result["cmd"][strlen($result["cmd"])-1]==="="){#不放空格$space="";#如果是第一個參數if($i===0){#更新真正執行的指令$result["escape"]["cmd"]=$result["cmd"].$space.$conf["argu"][$i];}#if end#反之真的為參數else{#取得參數的索引$arguIndex=count($conf["argu"])-1;#儲存真正的參數$result["escape"]["argu"][$arguIndex]=$conf["argu"][$arguIndex].$space.$conf["argu"][$i];}#else end}#if end#反之else{#儲存真正的參數$result["escape"]["argu"][]=$conf["argu"][$i];}#else#組合執行的指令$result["cmd"]=$result["cmd"].$space.$conf["argu"][$i];#下一個參數continue;}#if end}#if end#如果參數是 < 、<< 、> 、>> 、| 、2>&1、;、`、~、&、$!if($conf["argu"][$i]===">" || $conf["argu"][$i]==="<" || $conf["argu"][$i]===">>" || $conf["argu"][$i]==="<<" || $conf["argu"][$i]==="|" || $conf["argu"][$i]==="2>&1" || $conf["argu"][$i]===";" || $conf["argu"][$i]==="`" || $conf["argu"][$i]==="~" || $conf["argu"][$i]==="&" || $conf["argu"][$i]==="$!"){#預設要空格$space=" ";#組合執行的指令$result["cmd"]=$result["cmd"].$space.$conf["argu"][$i];#儲存真正的參數$result["escape"]["argu"][]=$conf["argu"][$i];}#if end#反之else{#如果是第一個參數,且指令結尾為「=」結束if($i===0 && $result["cmd"][strlen($result["cmd"])-1]==="="){#處理參數後再串接指令$result["cmd"]=$result["cmd"].escapeshellarg($conf["argu"][$i]);#更新真正執行的指令$result["escape"]["cmd"]=$result["cmd"];}#if end#反之else{#儲存escape好的參數$escapeArgu=escapeshellarg($conf["argu"][$i]);#如果有設置 $conf["thereIsShellVar"]if(isset($conf["thereIsShellVar"])){#如果有設置 $conf["thereIsShellVar"][$i]if(isset($conf["thereIsShellVar"][$i])){#如果含有 shell 變數if($conf["thereIsShellVar"][$i]==="true"){#檢查是否使用 shell 變數#函式說明:#尋找陣列中有無符合的key#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$reuslt["error"],執行不正常結束的錯訊息陣列.#$result["function"],當前執行的函式名稱.#$result["argu"],所使用的參數.#$result["founded"],是否有找到shell變數,"true"代表有找到,"false"代表沒有找到..#必填參數:#$conf["input"],字串,要檢查的字串..$conf["search::findShellVar"]["input"]=$escapeArgu;#可省略參數:#無.#參考資料:#無.#備註:#無.$findShellVar=search::findShellVar($conf["search::findShellVar"]);unset($conf["search::findShellVar"]);#如果檢查失敗if($findShellVar["status"]=="false"){#設置錯誤識別$result["status"]="false";#設置錯誤訊息$result["error"]=$findShellVar;#回傳解果return $result;}#if end#含有 shell 變數if($findShellVar["founded"]=="true"){#確認該字串是否 開頭跟結尾為 「'」#函式說明:#取得符合特定字首與字尾的字串#回傳結果:#$result["status"],若爲"true"則代表執行正常;若爲"false"則代表執行失敗。#$result["function"],當前執行的函數名稱.#$result["error"],錯誤訊息陣列.#$result["founded"],若為"true"則代表有找到符合字首條件的結果;若爲"false"則代表沒有找到。#$result["returnString"],爲符合字首條件的字串內容。#$result["argu"],使用的參數.#必填參數:#$conf["checkString"],字串,要檢查的字串.$conf["search::getMeetConditionsString"]["checkString"]=$escapeArgu;#可省略參數:#$conf["frontWord"],字串,用來檢查字首應該要有什麼字串,預設不指定.$conf["search::getMeetConditionsString"]["frontWord"]="'";#$conf["tailWord"],字串,用來檢查字尾應該要有什麼字串,預設不指定.$conf["search::getMeetConditionsString"]["tailWord"]="'";#參考資料:#str_spilt(),可以將字串依照字母分割成一個個陣列字串。#備註:#無.$getMeetConditionsString=search::getMeetConditionsString($conf["search::getMeetConditionsString"]);unset($conf["search::getMeetConditionsString"]);#如果檢查失敗if($getMeetConditionsString["status"]=="false"){#設置錯誤識別$result["status"]="false";#設置錯誤訊息$result["error"]=$getMeetConditionsString;#回傳解果return $result;}#if end#含有 shell 變數if($getMeetConditionsString["founded"]==="true"){#將含有 shell 變數的參數變成 '....'${hellVar}'....'#函式說明:#處理字串避免網頁出錯#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["function"],當前執行的函數.#$result["content"],爲處理好的字串.#$result["error"],錯誤訊息陣列.#$result["argu"],使用的參數.#必填參數:#$conf["stringIn"],字串,爲要處理的字串$conf["stringProcess::correctCharacter"]["stringIn"]=$escapeArgu;#可省略參數:#$conf["selectedCharacter"],字串陣列,爲被選擇要處理的字串/字元,須爲陣列值。若不設定則預設爲要將這些字串作替換 ("<",">","=","//","'","$","%","&","|","/*","*","#","\"").特殊字元,「\n」代表換行,「\t」代表tab鍵的間隔$conf["stringProcess::correctCharacter"]["selectedCharacter"]=array("\${","}");#$conf["changeTo"],字串陣列,爲被選擇的字元要換成什麼字串/字元,須爲陣列值。若不設定,則預設爲更換成""(空字串).$conf["stringProcess::correctCharacter"]["changeTo"]=array("'\${","}'");#參考資料:#無.#備註:#無.$correctCharacter=stringProcess::correctCharacter($conf["stringProcess::correctCharacter"]);unset($conf["stringProcess::correctCharacter"]);#如果執行失敗if($correctCharacter["status"]=="false"){#設置錯誤識別$result["status"]="false";#設置錯誤訊息$result["error"]=$correctCharacter;#回傳解果return $result;}#if end#更新成可以呈現 shell 變數的字串$escapeArgu=$correctCharacter["content"];}#if end}#if end}#if end}#if end}#if end#串接指令$result["cmd"]=$result["cmd"]." ".$escapeArgu;#儲存真正的參數$result["escape"]["argu"][]=$escapeArgu;}#else}#else end}#for end}#if end}#if end#反之 $conf["escapeshellarg"] 為 "false"else{#初始化儲存未escaped後的指令與參數資訊$result["noEscaped"]=array();#儲存執行的指令$result["noEscaped"]["cmd"]=$conf["command"];#初始化儲存執行的參數$result["noEscaped"]["argu"]=array();#如果有參數存在if(isset($conf["argu"])){#儲存執行的參數$result["noEscaped"]["argu"]=$conf["argu"];#有幾個參數就執行幾次for($i=0;$i<count($conf["argu"]);$i++){#如果有指定位置參數if(isset($conf["arguIsAddr"])){#如果是位置參數if($conf["arguIsAddr"][$i]==="true"){#如果是相對路徑就要轉換路徑if(strpos($conf["argu"][$i],"/")!==0){#函數說明:#將多個路徑字串變成相對於當前路徑的相對路徑字串#回傳結果:#$result["status"],"true"爲建立成功,"false"爲建立失敗.#$result["error"],錯誤訊息陣列.#$result["function"],函數名稱.#$result["content"],字串陣列,多個轉換好的相對路徑字串.#必填參數:#$conf["path"],陣列字串,要轉換成相對路徑的字串.;$conf["fileAccess::getRelativePath"]["path"]=array($conf["argu"][$i]);#$conf["fileArgu"],字串,當前路徑.$conf["fileAccess::getRelativePath"]["fileArgu"]=$conf["fileArgu"];$getRelativePath=fileAccess::getRelativePath($conf["fileAccess::getRelativePath"]);unset($conf["fileAccess::getRelativePath"]);#如果轉換路徑失敗if($getRelativePath["status"]==="false"){#設置執行失敗的識別$result["status"]="false";#設置執行失敗的訊息$result["error"]=$getRelativePath;#回傳結果return $result;}#if end#取得相對路徑$relativePath=$getRelativePath["content"][0];#保存其相對路徑$conf["argu"][$i]=$relativePath;}#if end}#if end}#if end#組合執行的指令$result["cmd"]=$result["cmd"]." ".$conf["argu"][$i];}#for end}#if end}#else end#如果有設置使用者名稱if(isset($conf["username"])){#檢查環境為apache還是cmd#涵式說明:#判斷當前環境為web還是cmd#回傳結果:#$result,"web"或"cmd"if(csInformation::getEnv()=="web"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"][]="函數 ".$result["function"]." 的 username 參數僅能在命令列環境下運行!";#回傳結果return $result;}#if end#函式說明:#取得用戶端的資訊,並依據需要寫入到資料表裡面#回傳的結果:#$result["status"],執行是否正常,"true"代表執行成功,"false"代表執行失敗.#$result["error"],錯誤訊息.#$result["function"],檔前執行的函數名稱.#$result["mode"],當前的模式是"cmd"還是"web".#$result["userBrowserType"],爲使用者的瀏覽器資訊#$result["userIp"],爲使用者的IP#$result["serverIp"],為伺服器的IP#$result["server_name"],伺服器的 domain name#$result["scheme"],通訊協定#$result["serverPort"],伺服器給對外下載網頁的port#$result["requestUri"],爲使用者要求的網址/php檔案.#$result["requestUriFull"],為使用者要求的完整網址/php檔案路徑.#$result["clientRequestIP"],用戶端要求的ip與port#$result["username"],爲使用者目前的帳戶,若爲""則表示尚未登入成功#$result["phpUser"],運行該php的使用者帳戶.若為空字串則代表非使用者直接觸發.#$result["phpUserType"],運行該php的使用者帳戶類型,可能有"regular(no wheel member)","wheel(can use sudo)","intrinsic(root)","system(qemu,apache,...)".#$result["header"],接收到的 header 陣列.#$result["body"],接收到的 body 字串.#必填參數:#$conf["getAccount"],字串,是否要取得帳號,"true"代表要;"false"代表不要.$conf["csInformation::getConnectionInfo"]["getAccount"]="true";#可省略參數:#$conf["accountVar"],字串,帳號儲存在哪個變數裏面,預設爲$_SESSION["username"].#$conf["accountVar"]=$_SESSION["username"];#$conf["saveToDb"],字串,是否要除儲存到資料庫,"true"為要儲存",預設為"false"不儲存.#$conf["saveToDb"]="true";#$conf["dbAddress"],字串,爲mysql/mariadb server的位置,若saveToDb設為"true",則該參數為必填.#$conf["dbAddress"]=$dbAddress;#$conf["dbAccount"],字串,爲用於連入mysql/mariadb server時要使用的帳號,若saveToDb設為"true",則該參數為必填.#$conf["dbAccount"]=$dbAccount;#$conf["dbName"],字串,要選取的資料庫名稱,若saveToDb設為"true",則該參數為必填.#$conf["dbName"]=$dbName;#$conf["tableName"],字串,爲要插入資料的資料表名稱,若saveToDb設為"true",則該參數為必填.#$conf["tableName"]="visitorInfo";#$conf["columnName"],字串陣列,爲資料表的項目名稱,例如:$conf["columnName"]=array("columnName1","columnName2","columnName3",...);寫入的資料依序為,使用者帳戶、瀏覽器資訊、使用者IP、觀看的網址、當時的時間.若saveToDb設為"true",則該參數為必填.#$conf["columnName"]=array("username","userWebBrowser","userIp","requestUri","systemDateAndTime");#$conf["dbPassword"],字串,爲連線到mysql/mariadb server時要使用的密碼,可省略,若省略則代表不使用密碼.#$conf["dbPassword"]=$dbPassword;#參考資料:#$_SERVER=>http://php.net/manual/zh/reserved.variables.server.php#取得伺服器名稱與IP=>http://php.net/manual/en/function.gethostname.php#備註:#無.$getConnectionInfo=csInformation::getConnectionInfo($conf["csInformation::getConnectionInfo"]);unset($conf["csInformation::getConnectionInfo"]);#如果執行失敗if($getConnectionInfo["status"]==="false"){#設置執行失敗的識別$result["status"]="false";#設置執行失敗的訊息$result["error"]=$getConnectionInfo;#回傳結果return $result;}#if end#如果不是以運行php的使用者角色來執行shellif($conf["username"]!==$getConnectionInfo["phpUser"]){#如果有設置使用者密碼if(isset($conf["password"])){#透過使用者與密碼來執行指令$result["cmd"]="echo ".$conf["password"]." | su ".$conf["username"]." -c '".$result["cmd"]."'";}#if end#反之沒有設置密碼else{#透過指定的使用者來執行指令$result["cmd"]="su ".$conf["username"]." -c '".$result["cmd"]."'";}#else end}#if end}#if end#如果 $conf["useScript"] 為 "true"if($conf["useScript"]==="true"){#轉換 $conf["logFile"] 為絕對路徑#函數說明:#將檔案的位置名稱變成網址,也可以取得檔案位於伺服器上檔案系統的絕對位置.#回傳結果:#$result["status"],"true"爲建立成功,"false"爲建立失敗.#$result["error"],錯誤訊息陣列.#$result["function"],函數名稱.#$result["content"],網址,若是在命令列執行,則為"null".#$result["webPathFromRoot"],相對於網頁根目錄的路徑.#$result["fileSystemAbsoulutePosition"],針對伺服器端的絕對位置,亦即從網頁「/」目錄開始的路徑.#$result["fileSystemRelativePosition"],針對伺服器檔案系統的相對位置.#必填參數:#$conf["address"],字串,檔案的相對位置,若為絕對位置則會自動轉換成相對位置.$conf["fileAccess::getInternetAddress"]["address"]=$conf["logFilePath"];#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.$conf["fileAccess::getInternetAddress"]["fileArgu"]=$conf["fileArgu"];#可省略參數:#$conf["userDir"],字串,網頁是否置放於家目錄底下,"true"為是,"false"為不是,預設為"true".#$conf["fileAccess::getInternetAddress"]["userDir"]="true";#備註:#在命令列執行,所得的路徑是錯誤的。$getInternetAddress=fileAccess::getInternetAddress($conf["fileAccess::getInternetAddress"]);unset($conf["fileAccess::getInternetAddress"]);#如果轉換路徑失敗if($getInternetAddress["status"]=="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤$result["error"]=$getInternetAddress;#回傳結果return $result;}#if end#取得轉換後的絕對路徑$conf["logFilePath"]=$getInternetAddress["fileSystemAbsoulutePosition"];#確保logFile的路徑#涵式說明:#確保路徑存在.#回傳的結果:#$result["status"],執行正常與否,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息陣列.#$resutl["function"],當前執行的涵式名稱.#$result["path"],建立好的路徑字串.#$result["fileName"],檔案名稱,若 $conf["haveFileName"] 為 "true" 則會回傳.#必填參數::#$conf["path"],要檢查的路徑$conf["fileAccess::validatePath"]["path"]=$conf["logFilePath"];#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑$conf["fileAccess::validatePath"]["fileArgu"]=$conf["fileArgu"];#可省略參數:#$conf["haveFileName"],字串,"true"代表有$conf["path"]檔案名稱,"false"代表$conf["path"]為純路徑,預設為"false".#$conf["fileAccess::validatePath"]["haveFileName"]="true";#$conf["dirPermission"],字串,新建資料夾的權限設定,預設爲0770,亦即擁有者,同群組者可以讀,寫,存取,其他人僅能存取.#$conf["dirPermission"]="";$validatePath=fileAccess::validatePath($conf["fileAccess::validatePath"]);unset($conf["fileAccess::validatePath"]);#如果確保設定檔路徑失敗if($validatePath["status"]=="false"){#設置執行不正常$result["status"]="false";#設置執行錯誤$result["error"]=$validatePath;#回傳結果return $result;}#if end#初始化 $conf["logFileName"]$conf["logFileName"]="";#無窮迴圈for($i=0;$i<1;){#函式說明:#php內建的date()函數#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息陣列.#$result["content"],自釘格式的時間字串.#必填參數:#$conf["timeZone"],字串,時區代號,可以設定的時區列表=> http://www.php.net/manual/en/timezones.php 台灣則為"Asia/Taipei".$conf["time::buildInDate"]["timeZone"]="Asia/Taipei";#$conf["format"],字串,要顯示哪些時間單位,格式與順序為何?西元年為'Y',月份為'm',日為'd',星期為D,以24計的時為'G'以12計的時為'h',分為'i',秒為's',當年的第幾個禮拜為'W',時區為'e',當年第幾天為'z',若想顯示英文字請輸入"\字母".$conf["time::buildInDate"]["format"]="Y-m-d(星期D) G:i:s 第W周";#參考資料來源:#http://php.net/manual/en/function.date.php$buildInDate=time::buildInDate($conf["time::buildInDate"]);unset($conf["time::buildInDate"]);#如果取得時間失敗if($buildInDate["status"]=="false"){#設置執行不正常$result["status"]="false";#設置執行錯誤$result["error"]=$buildInDate;#回傳結果return $result;}#if end#取得logFileName$conf["logFileName"]="output-".$buildInDate["content"].".log";#確認檔案名稱是否有重複#涵式說明:檢查多個檔案與資料夾是否存在.#回傳結果:#$result["status"],執行正常與否,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息陣列.#$resutl["function"],當前執行的涵式名稱.#$result["allExist"],所有檔案皆存在的識別,"true"代表皆存在,"false"代表沒有全部都存在.#$result["varName"][$i],爲第$i個資料夾或檔案的路徑與名稱。#$result["varNameFullPath"][$i],爲第$i個資料夾或檔案的完整檔案系統路徑與名稱。#$result["varNameWebPath"][$i],為第$i個資料夾或檔案的網址#$result["varExist"][$i],爲第$i個資料夾或檔案是否存在,"true"代表存在,"false"代表不存在。#必填參數:#$conf["fileArray"],陣列字串,要檢查是否存在的檔案有哪些,須爲一維陣列數值。$conf["fileAccess::checkMultiFileExist"]["fileArray"]=array($conf["logFilePath"].$conf["logFileName"]);#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑$conf["fileAccess::checkMultiFileExist"]["fileArgu"]=$conf["fileArgu"];#可省略參數#$conf["disableWebSearch"],"字串",是否取消「當檔案找不到時,改用catchWebContent類別的wget函數來檢查檔案是否存在於網路上」的功能,"false"不取消,若要取消該功能請設為"true",若抓到的內容為空字串則會視為檔案不存在,預設為"true".#$conf["disableWebSearch"]="false";#$conf["userDir"],字串,網頁是否置放於家目錄底下,"true"為是,"false"為不是,預設為"true".#$conf["userDir"]="true";#參考資料來源:#http://php.net/manual/en/function.file-exists.php#http://php.net/manual/en/control-structures.foreach.php#備註:#函數file_exists檢查的路徑為檔案系統的路徑$checkMultiFileExist=fileAccess::checkMultiFileExist($conf["fileAccess::checkMultiFileExist"]);unset($conf["fileAccess::checkMultiFileExist"]);#如果檢查檔案存在失敗if($checkMultiFileExist["status"]==="false"){#設置執行不正常$result["status"]="false";#設置執行錯誤$result["error"]=$checkMultiFileExist;#回傳結果return $result;}#if end#如果檔案不存在if($checkMultiFileExist["allExist"]==="false"){#跳出迴圈break;}#else end}#for end#用script指令抓取輸出,並存到 $conf["logFilePath"]."/".$conf["logFileName"].$result["cmd"]="script -q -c \"".$result["cmd"]."\" \"".$conf["logFilePath"]."/".$conf["logFileName"]."\"";}#if end#如果 $conf["inBackGround"] 為 "true"if($conf["inBackGround"]==="true"){#函式說明:#呼叫shell執行系統命令,並取得回傳的內容與pid.#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息陣列.#$result["function"],當前執行的函數名稱.#$result["cmd"],執行的指令內容.#$result["fullCmd"],執行的完整指令#$result["content"],爲執行完二元碼後的輸出陣列.#$result["tmpFileOutput"],儲存輸出的暫村檔案名稱.#$result["running"],程式是否還在執行.#$result["pid"],pid.#$result["statusCode"],執行結果狀態碼.#必填參數:#$conf["command"],字串,要執行的指令.$conf["external::callShellV2"]["command"]=$result["cmd"];#$conf["fileArgu"],字串,變數__FILE__的內容.$conf["external::callShellV2"]["fileArgu"]=$conf["fileArgu"];#可省略參數:#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.$conf["external::callShellV2"]["doNotRun"]=$conf["doNotRun"];#備註:#不是所有指令都能用apache的身份執行,目前已知java,javac,ping指令無法執行,透過su使用root身份可能會被selinux阻擋.#參考資料:#http://php.net/manual/en/function.exec.php$callShellV2=external::callShellInBg($conf["external::callShellV2"]);unset($conf["external::callShellV2"]);#如果執行失敗if($callShellV2["status"]==="false"){#設置執行不正常$result["status"]="false";#設置執行錯誤$result["error"]=$callShellV2;#回傳結果return $result;}#if end#執行到這邊代表執行正常$result["status"]="true";#取得當下的輸出$result["content"]=$callShellV2["content"];#取得集記錄輸出的暫存檔案位置$result["tmpFileOutput"]=$callShellV2["tmpFileOutput"];#取得執行的完整指令$result["fullCmd"]=$callShellV2["fullCmd"];#取得執行的指令$result["cmd"]=$callShellV2["cmd"];#取得程序的pid$result["pid"]=$callShellV2["pid"];#取得是否還在執行$result["running"]=$callShellV2["running"];#取得執行狀態代碼$result["statusCode"]=$callShellV2["statusCode"];#回傳結果return $result;}#if end#反之沒有要在背景中執行else{#如果要將錯誤輸出導到標準輸出if($conf["getErr"]==="true"){#指令加上 "2>&1"$result["cmd"]=$result["cmd"]." 2>&1";}#if end#如果沒有要執行指令if($conf["doNotRun"]==="true"){#初始化執行結果狀態碼為0$result["statusCode"]=0;#初始化輸出為空$resultArray=array();#初始化執行結果狀態碼為0$execStatus=0;}#if end#反之要執行指令else{#var_dump($result["cmd"]);#執行腳本程式,將輸出結果儲存在 $resultArray 陣列裏面exec($result["cmd"],$resultArray,$execStatus);#取得執行結果狀態碼$result["statusCode"]=$execStatus;}#else end}#else end#如果 $conf["useScript"] 為 "true"if($conf["useScript"]=="true"){#讀取暫存檔案的內容#函式說明:#依據行號分隔抓取檔案的內容,結果會回傳一個陣列#回傳的變數說明:#$result["status"],執行是否成功,"true"代表成功;"fasle"代表失敗.#$result["error"],錯誤訊息提示.#$result["warning"],警告訊息.#$result["function"],當前執行的函數名稱.#$result["fileContent"],爲檔案的內容陣列.#$result["lineCount"],爲檔案內容總共的行數.#$result["fullContent"],為檔案的完整內容.#必填參數::#$conf["filePositionAndName"],字串,爲檔案的位置以及名稱.$conf["fileAccess::getFileContent"]["filePositionAndName"]=$conf["logFilePath"]."/".$conf["logFileName"];#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑$conf["fileAccess::getFileContent"]["fileArgu"]=$conf["fileArgu"];#參考資料:#file(),取得檔案內容的行數.#file=>http:#php.net/manual/en/function.file.php#rtrim(),剔除透過file()取得每行內容結尾的換行符號.#filesize=>http://php.net/manual/en/function.filesize.php$getFileContent=fileAccess::getFileContent($conf["fileAccess::getFileContent"]);unset($conf["fileAccess::getFileContent"]);#如果取得檔案內容失敗if($getFileContent["status"]=="false"){#設置執行不正常$result["status"]="false";#設置執行錯誤$result["error"]=$getFileContent;#回傳結果return $result;}#if end#移除暫存檔案#涵式說明:#移除檔案#回傳的結果:#$result["status"],"true"代表移除成功,"false"代表移除失敗.#$result["error"],錯誤訊息陣列#$result["warning"],警告訊息陣列#$result["function"],當前執行的函數名稱#必填參數::#$conf["fileAddress"],字串,要移除檔案的位置.$conf["fileAccess::delFile"]["fileAddress"]=$conf["logFilePath"]."/".$conf["logFileName"];#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.$conf["fileAccess::delFile"]["fileArgu"]=$conf["fileArgu"];#可省略參數:#$conf["commentsArray"],字串陣列,提示的文字描述,$conf["commentsArray"][$i]代表第($+1)行的描述.#$conf["commentsArray"]=array("");$delFile=fileAccess::delFile($conf["fileAccess::delFile"]);unset($conf["fileAccess::delFile"]);#如果移除檔案失敗if($delFile["status"]=="false"){#設置執行不正常$result["status"]="false";#設置執行錯誤$result["error"]=$getFileContent;#回傳結果return $result;}#if end#取得執行外部程式的輸出$result["output"]=$getFileContent["fileContent"];}#if end#反之else{#取得執行外部程式的輸出$result["output"]=$resultArray;}#else end#如果有escape後的結果if(isset($result["escape"])){#初始化暫存 指令跟參數的陣列$cmdAndArguArray=array();#取得第一個元素$cmdAndArguArray[]=&$result["escape"]["cmd"];#如果有 arguif(isset($result["escape"]["argu"])){#針對每個參數foreach($result["escape"]["argu"] as $arguKey=>$arguVal){#取得該參數$cmdAndArguArray[]=&$result["escape"]["argu"][$arguKey];}#foreach end}#if end#設置 cmd & argu 合併為一維陣列後的結果$result["escape"]["array"]=&$cmdAndArguArray;}#if end#如果有不 Escaped 的指令資訊結果if(isset($result["noEscaped"])){#從 $result["noEscaped"]["cmd"] 跟 $result["noEscaped"]["argu"] 產生 $result["noEscaped"]["array"]#函式說明:#將多個一維陣列串聯起來,key從0開始排序.#回傳的結果:#$result["status"],"true"表執行正常,"false"代表執行不正常.#$result["error"],錯誤訊息陣列.#$result["function"],當前執行的函數.#$result["argu"],字串陣列,目前輸入的參數名稱陣列.#$result["content"],合併好的一維陣列.#必填參數#$conf["inputArray"],陣列,要合併的一維陣列變數,例如:=array($array1,$array2);$conf["arrays::mergeArray"]["inputArray"]=array(array($result["noEscaped"]["cmd"]),$result["noEscaped"]["argu"]);#可省略參數:#$conf["allowRepeat"],字串,預設為"true",允許重複的結果;若為"false"則不會出現重複的元素內容.#$conf["allowRepeat"]="true";#$conf["looseDiff"],字串,預設為"false",代表要嚴謹判斷為有相異,例如陣列中元素的key順序不同(整數)就代表有相異;反之為"true",例如陣列中元素的key順序不同(非整數),但value有相同,則視為無相異.#$conf["looseDiff"]="false";#參考資料:#無.#備註:#無.$mergeArray=arrays::mergeArray($conf["arrays::mergeArray"]);unset($conf["arrays::mergeArray"]);#如果異常if($mergeArray["status"]=="false"){#設置執行不正常$result["status"]="false";#設置執行錯誤$result["error"]=$mergeArray;#回傳結果return $result;}#if end#取得未escape的指令與參數陣列$result["noEscaped"]["array"]=$mergeArray["content"];}#if end#若執行外部式回傳的狀態不為0if($execStatus!==0){#代表執行外部程式不正常.#設置錯誤識別$result["status"]="false";#設錯誤訊息$result["error"][]=$execStatus;$result["error"][]=$resultArray;#設置錯誤訊息$result["error"][]="執行 「".$result["cmd"]."」 命令意外結束!";$result["error"][]="有時可能是因為沒有權限導致的錯誤";$result["error"][]="有時可能是因為指令語法不正確導致的錯誤";#回傳結果return $result;}#if end#初始化完整標準輸出$result["content"]="";#如果有 outputif(!empty($result["output"])){#函式說明:#將一維陣列轉換為用特定符號間隔的字串,ex:array("1","2","3") to "a;b;c;".#回傳的結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["function"],當前執行的function名稱#$result["error"],錯誤訊息陣列.#$result["content"],處理好的字串.#$result["argu"],使用的參數.#必填參數:#$conf["inputArray"],字串陣列,要轉成字串的一維陣列.$conf["arrays::arrayToString"]["inputArray"]=$result["output"];#可省略參數:#$conf["spiltSymbol"],字串,用來區隔字串的符號,預設為;$conf["arrays::arrayToString"]["spiltSymbol"]=PHP_EOL;#$conf["skipEnd"],字串,結尾是否不要加上符號,預設為"false",要加上符號,"true"代表不要加上符號。$conf["arrays::arrayToString"]["skipEnd"]="true";#$conf["spiltSymbolAtStart"],字串,是否要在開頭加上spiltSymbol,預設為"false",代表不要;反之為“true”.#$conf["spiltSymbolAtStart"]="";#參考資料:#無.#備註:#無.$arrayToString=arrays::arrayToString($conf["arrays::arrayToString"]);unset($conf["arrays::arrayToString"]);#如果執行失敗if($arrayToString["status"]=="false"){#設置執行不正常$result["status"]="false";#設置執行錯誤$result["error"]=$arrayToString;#回傳結果return $result;}#if end#儲存完整標準輸出的內容$result["content"]=$arrayToString["content"];}#if end#執行到這邊代表執行正常$result["status"]="true";#回傳結果return $result;}#function callShell end/*#函式說明:#呼叫shell執行系統命令,並取得回傳儲存輸出內容的檔案與pid.#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息陣列.#$result["function"],當前執行的函數名稱.#$result["cmd"],執行的指令內容.#$result["fullCmd"],執行的完整指令#$result["content"],爲執行完二元碼後的輸出陣列.#$result["tmpFileOutput"],儲存輸出的暫村檔案名稱.#$result["runing"],程式是否還在執行.#$result["pid"],pid.#$result["statusCode"],執行結果狀態碼.#必填參數:#$conf["command"],字串,要執行的指令.$conf["command"]="";#$conf["fileArgu"],字串,變數__FILE__的內容.$conf["fileArgu"]=__FILE__;#可省略參數:#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.#$conf["doNotRun"]="false";#$conf["leftLogFile"],字串,預設為"false"代表要將 log 檔案移除;"true"代表要保留log檔案.#$conf["leftLogFile"]="true";#備註:#不是所有指令都能用apache的身份執行,目前已知java,javac,ping指令無法執行,透過su使用root身份可能會被selinux阻擋.#如果輸出的內容包含換行符號,將會被過濾掉.#nohup $cmd > $logFilePath 2>&1 & echo $! 會將指令的標準輸入寫入檔案 $logFilePath 並忽略錯誤輸出,將指令在背景執行,僅印出執行緒id.#$cmd > /dev/null 2>&1 &; 會將$cmd放在背景執行,且不會卡住.#參考資料:#http://php.net/manual/en/function.exec.php*/public static function callShellInBg(&$conf){#初始化要回傳的結果$result=array();#取得當前執行的函數名稱$result["function"]=__FUNCTION__;#如果沒有參數if(func_num_args()==0){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"]="函數".$result["function"]."需要參數";#回傳結果return $result;}#if end#取得參數$result["argu"]=$conf;#如果 $conf 不為陣列if(gettype($conf)!=="array"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"][]="\$conf變數須為陣列形態";#如果傳入的參數為 nullif($conf===null){#設置執行錯誤訊息$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";}#if end#回傳結果return $result;}#if end#檢查參數#函式說明:#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$reuslt["error"],執行不正常結束的錯訊息陣列.#$result["function"],當前執行的函式名稱.#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.#$result["argu"],字串陣列,目前輸入的參數名稱陣列.#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.#$result["notNeedVar"],字串陣列,多餘的參數名稱.#必填寫的參數:#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。$conf["variableCheck::checkArguments"]["varInput"]=&$conf;#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("command","fileArgu");#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string");#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";#可以省略的參數:#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.#$conf["canBeEmptyString"]="false";#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.#$conf["canNotBeEmpty"]=array();#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.#$conf["canBeEmpty"]=array();#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.#$conf["skipableVariableCanNotBeEmpty"]=array();#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("doNotRun","leftLogFile");#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string");#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("false","false");#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.#$conf["arrayCountEqualCheck"][]=array();#參考資料來源:#array_keys=>http://php.net/manual/en/function.array-keys.php#建議:#增加可省略參數全部不能為空字串或空陣列的參數功能.$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);unset($conf["variableCheck::checkArguments"]);#如果檢查參數失敗if($checkArguments["status"]==="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"]=$checkArguments;#回傳結果return $result;}#if end#如果參數檢查不過if($checkArguments["passed"]==="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"]=$checkArguments;#回傳結果return $result;}#if end#產生暫存檔案#函數說明:#建立暫存目錄與回傳暫存檔案名稱路徑#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息.#$result["function"],當前執行的函數名稱.#$result["content"],暫存檔案的路徑與名稱.#必填參數:#無#可省略參數:#$conf["tempDir"],字串,暫存目錄的名稱,預設為.fileAccess/createTempFile#$conf["tempDIr"]="";#$conf["fileArgu"],字串,__FILE__的內容,預設為當前檔案的位置.$conf["fileAccess::createTempFile"]["fileArgu"]=$conf["fileArgu"];#$conf["createPath"],字串,是否僅要建立目錄,"true"代表要,"false"代表不要,預設為"true".#$conf["createPath"]="";#$conf["createFile"],字串,是否要在暫存路徑建立好後建立暫存檔案,"true"代表要,"false"代表不用,預設為"true".#$conf["createFile"]="";$createTempFile=fileAccess::createTempFile($conf["fileAccess::createTempFile"]);unset($conf["fileAccess::createTempFile"]);#如果建立暫存檔案失敗if($createTempFile["status"]==="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"]=$createTempFile;#回傳結果return $result;}#if end#取得暫存檔案的路徑與名稱$logFilePath=$createTempFile["content"];#執行 commad 並輸出結果到 $logFilePath, 忽略錯誤輸出, 印出pid$command = 'nohup '.$conf["command"].' > '.$logFilePath.' 2>&1 & echo $!';#$cmd="nohup";#$paramsArray=array($conf["command"],">",$logFilePath,"2>&1","&","echo","$!");#如果不用執行指令if($conf["doNotRun"]==="true"){#預設執行結果代碼為0$status=0;#預設pid為0$op=array("0");}#if end#反之else{#執行command, 輸出放到$opexec($command ,$op ,$status);}#else end#取得執行結果狀態碼$result["statusCode"]=$status;#設置執行的完整指令$result["fullCmd"]=$command;#設置執行的指令$result["cmd"]=$conf["command"];#取得pid$result["pid"]=$op[0];#函式說明:#依據行號分隔抓取檔案的內容,結果會回傳一個陣列#回傳的變數說明:#$result["status"],執行是否成功,"true"代表成功;"fasle"代表失敗.#$result["error"],錯誤訊息提示.#$result["warning"],警告訊息.#$result["function"],當前執行的函數名稱.#$result["fileContent"],爲檔案的內容陣列.#$result["lineCount"],爲檔案內容總共的行數.#$result["fullContent"],為檔案的完整內容.#必填參數::#$conf["filePositionAndName"],字串,爲檔案的位置以及名稱.$conf["fileAccess::getFileContent"]["filePositionAndName"]=$logFilePath;#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑$conf["fileAccess::getFileContent"]["fileArgu"]=$conf["fileArgu"];#可省略參數:#$conf["web"],是要取得網路上的檔案則為"true";反之則為"false".$conf["fileAccess::getFileContent"]["web"]="false";#參考資料:#file(),取得檔案內容的行數.#file=>http:#php.net/manual/en/function.file.php#rtrim(),剔除透過file()取得每行內容結尾的換行符號.#filesize=>http://php.net/manual/en/function.filesize.php$getFileContent=fileAccess::getFileContent($conf["fileAccess::getFileContent"]);unset($conf["fileAccess::getFileContent"]);#如果讀取檔案失敗if($getFileContent["status"]==="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"]=$getFileContent;#回傳結果return $result;}#if end#取得輸出內容$result["content"]=$getFileContent["fileContent"];#取得暫存檔案路徑與名稱$result["tmpFileOutput"]=$logFilePath;#如果沒有要保留 log 檔案if($conf["leftLogFile"]==="false"){#函式說明:#移除檔案#回傳結果:#$result["status"],"true"代表移除成功,"false"代表移除失敗.#$result["error"],錯誤訊息陣列.#$result["warning"],警告訊息陣列.#$result["function"],當前執行的函數名稱.#$result["argu"],當前函式使用的參數.#必填參數:#$conf["fileAddress"],字串,要移除檔案的位置.$conf["fileAccess::delFile"]["fileAddress"]=$logFilePath;#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.$conf["fileAccess::delFile"]["fileArgu"]=$conf["fileArgu"];#可省略參數:#$conf["commentsArray"],字串陣列,提示的文字描述,$conf["commentsArray"][$i]代表第($+1)行的描述.#$conf["commentsArray"]=array("");#$conf["allowDelSymlink"],字串,預設為"false",不移除軟連結;"true"代表要移除軟連結.#$conf["allowDelSymlink"]="true";#$conf["allowDelFolder"],字串,預設為"false",不移除目錄;"true"代表要移除目錄.#$conf["allowDelFolder"]="true";#參考資料:#無.#備註:#無.$delFile=fileAccess::delFile($conf["fileAccess::delFile"]);unset($conf["fileAccess::delFile"]);#如果執行失敗if($delFile["status"]==="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"]=$delFile;#回傳結果return $result;}#if end}#if end#函式說明:#檢查程序是否還在執行#回傳結果#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息陣列.#$result["function"],當前執行的函數名稱.#$result["exist"],程序是否存在,"true"代表存在,"false"代表不存在.#必填參數:#$conf["pid"],字串,程序的id.$conf["external::processStatus"]["pid"]=$result["pid"];#$conf["cmd"],字串,程序的指令.$conf["external::processStatus"]["cmd"]=$result["cmd"];#$conf["fileArgu"],字串,__FILE__的內容,__FILE__的內容.$conf["external::processStatus"]["fileArgu"]=$conf["fileArgu"];$processStatus=external::processStatus($conf["external::processStatus"]);unset($conf["external::processStatus"]);#如果查詢pid是否存在失敗if($processStatus["status"]==="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"]=$processStatus;#回傳結果return $result;}#if end#取得程序是否還在執行$result["running"]=$processStatus["exist"];#設置執行正常$result["status"]="true";#回傳結果return $result;}#function callShellInBg end/*#函式說明:#呼叫shell_exec執行系統命令,並取得回傳儲存輸出內容的檔案與pid.#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息陣列.#$result["function"],當前執行的函數名稱.#$result["cmd"],執行的指令內容.#$result["fullCmd"],執行的完整指令#$result["content"],爲執行完二元碼後的輸出結果,會儲存成字串,包含換行符號.#$result["runing"],程式是否還在執行.#$result["pid"],pid.#必填參數:#$conf["command"],字串,要執行的指令.$conf["command"]="";#$conf["fileArgu"],字串,變數__FILE__的內容.$conf["fileArgu"]=__FILE__;#可省略參數:#無.#參考資料:#http://php.net/manual/en/function.exec.php#備註:#不是所有指令都能用apache的身份執行,目前已知java,javac,ping指令無法執行,透過su使用root身份可能會被selinux阻擋.*/public static function callShellExecInBg(&$conf){#初始化要回傳的結果$result=array();#取得當前執行的函數名稱$result["function"]=__FUNCTION__;#如果沒有參數if(func_num_args()==0){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"]="函數".$result["function"]."需要參數";#回傳結果return $result;}#if end#取得參數$result["argu"]=$conf;#如果 $conf 不為陣列if(gettype($conf)!=="array"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"][]="\$conf變數須為陣列形態";#如果傳入的參數為 nullif($conf===null){#設置執行錯誤訊息$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";}#if end#回傳結果return $result;}#if end#檢查參數#函式說明:#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$reuslt["error"],執行不正常結束的錯訊息陣列.#$result["function"],當前執行的函式名稱.#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.#$result["argu"],字串陣列,目前輸入的參數名稱陣列.#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.#$result["notNeedVar"],字串陣列,多餘的參數名稱.#必填寫的參數:#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。$conf["variableCheck::checkArguments"]["varInput"]=&$conf;#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("command","fileArgu");#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string");#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";#可以省略的參數:#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.#$conf["canBeEmptyString"]="false";#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.#$conf["canNotBeEmpty"]=array();#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.#$conf["canBeEmpty"]=array();#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.#$conf["skipableVariableCanNotBeEmpty"]=array();#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");#$conf["skipableVariableName"]=array();#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");#$conf["skipableVariableType"]=array();#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".#$conf["skipableVarDefaultValue"]=array("");#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.#$conf["arrayCountEqualCheck"][]=array();#參考資料來源:#array_keys=>http://php.net/manual/en/function.array-keys.php#建議:#增加可省略參數全部不能為空字串或空陣列的參數功能.$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);unset($conf["variableCheck::checkArguments"]);#如果檢查參數失敗if($checkArguments["status"]==="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"]=$checkArguments;#回傳結果return $result;}#if end#如果參數檢查不過if($checkArguments["passed"]==="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"]=$checkArguments;#回傳結果return $result;}#if end#產生暫存檔案#函數說明:#建立暫存目錄與回傳暫存檔案名稱路徑#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息.#$result["function"],當前執行的函數名稱.#$result["content"],暫存檔案的路徑與名稱.#必填參數:#無#可省略參數:#$conf["tempDir"],字串,暫存目錄的名稱,預設為.fileAccess/createTempFile#$conf["tempDIr"]="";#$conf["fileArgu"],字串,__FILE__的內容,預設為當前檔案的位置.$conf["fileAccess::createTempFile"]["fileArgu"]=$conf["fileArgu"];#$conf["createPath"],字串,是否僅要建立目錄,"true"代表要,"false"代表不要,預設為"true".#$conf["createPath"]="";#$conf["createFile"],字串,是否要在暫存路徑建立好後建立暫存檔案,"true"代表要,"false"代表不用,預設為"true".#$conf["createFile"]="";$createTempFile=fileAccess::createTempFile($conf["fileAccess::createTempFile"]);unset($conf["fileAccess::createTempFile"]);#如果建立暫存檔案失敗if($createTempFile["status"]==="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"]=$createTempFile;#回傳結果return $result;}#if end#取得暫存檔案的路徑與名稱$logFilePath=$createTempFile["content"];#執行 commad 並輸出結果到 "/tmp/externel.Process", 忽略錯誤輸出, 印出pid$command = 'nohup '.$conf["command"].' > '.$logFilePath.' 2>&1 & echo $!';#執行command, 輸出放到$op$op=shell_exec($command);#設置執行的完整指令$result["fullCmd"]=$command;#設置執行的指令$result["cmd"]=$conf["command"];#取得pid$result["pid"]=$op;#函式說明:#依據行號分隔抓取檔案的內容,結果會回傳一個陣列#回傳的變數說明:#$result["status"],執行是否成功,"true"代表成功;"fasle"代表失敗.#$result["error"],錯誤訊息提示.#$result["warning"],警告訊息.#$result["function"],當前執行的函數名稱.#$result["fileContent"],爲檔案的內容陣列.#$result["lineCount"],爲檔案內容總共的行數.#$result["fullContent"],為檔案的完整內容.#必填參數::#$conf["filePositionAndName"],字串,爲檔案的位置以及名稱.$conf["fileAccess::getFileContent"]["filePositionAndName"]=$logFilePath;#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑$conf["fileAccess::getFileContent"]["fileArgu"]=$conf["fileArgu"];#參考資料:#file(),取得檔案內容的行數.#file=>http:#php.net/manual/en/function.file.php#rtrim(),剔除透過file()取得每行內容結尾的換行符號.#filesize=>http://php.net/manual/en/function.filesize.php$getFileContent=fileAccess::getFileContent($conf["fileAccess::getFileContent"]);unset($conf["fileAccess::getFileContent"]);#如果讀取檔案失敗if($getFileContent["status"]==="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"]=$getFileContent;#回傳結果return $result;}#if end#取得輸出內容$result["content"]=$getFileContent["fileContent"];#取得暫存檔案路徑與名稱$result["tmpFileOutput"]=$logFilePath;#函式說明:#檢查程序是否還在執行#回傳結果#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息陣列.#$result["function"],當前執行的函數名稱.#$result["exist"],程序是否存在,"true"代表存在,"false"代表不存在.#必填參數:#$conf["pid"],字串,程序的id.$conf["external::processStatus"]["pid"]=$result["pid"];#$conf["cmd"],字串,程序的指令.$conf["external::processStatus"]["cmd"]=$result["cmd"];#$conf["fileArgu"],字串,__FILE__的內容,__FILE__的內容.$conf["external::processStatus"]["fileArgu"]=$conf["fileArgu"];$processStatus=external::processStatus($conf["external::processStatus"]);unset($conf["external::processStatus"]);#如果查詢pid是否存在失敗if($processStatus["status"]==="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"]=$processStatus;#回傳結果return $result;}#if end#取得程序是否還在執行$result["runing"]=$processStatus["exist"];#設置執行正常$result["status"]="true";#回傳結果return $result;}#function callShellExecInBg end/*#函式說明:#檢查程序是否還在執行#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息陣列.#$result["function"],當前執行的函數名稱.#$result["exist"],程序是否存在,"true"代表存在,"false"代表不存在.#必填參數:#$conf["pid"],字串,程序的id.$conf["pid"]="";#$conf["cmd"],字串,程序的指令.$conf["cmd"]=""#$conf["fileArgu"],字串,__FILE__的內容,__FILE__的內容.$conf["fileArgu"]=__FILE__;#可省略參數:#無.#參考資料:#無.#備註:#無.*/public static function processStatus(&$conf){#初始化要回傳的結果$result=array();#取得當前執行的函數名稱$result["function"]=__FUNCTION__;#如果沒有參數if(func_num_args()==0){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"]="函數".$result["function"]."需要參數";#回傳結果return $result;}#if end#取得參數$result["argu"]=$conf;#如果 $conf 不為陣列if(gettype($conf)!=="array"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"][]="\$conf變數須為陣列形態";#如果傳入的參數為 nullif($conf===null){#設置執行錯誤訊息$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";}#if end#回傳結果return $result;}#if end#檢查參數#函式說明:#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$reuslt["error"],執行不正常結束的錯訊息陣列.#$result["function"],當前執行的函式名稱.#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.#$result["argu"],字串陣列,目前輸入的參數名稱陣列.#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.#$result["notNeedVar"],字串陣列,多餘的參數名稱.#必填寫的參數:#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。$conf["variableCheck::checkArguments"]["varInput"]=&$conf;#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("pid","cmd","fileArgu");#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string","string");#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";#可以省略的參數:#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.#$conf["canBeEmptyString"]="false";#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.#$conf["canNotBeEmpty"]=array();#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.#$conf["canBeEmpty"]=array();#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.#$conf["skipableVariableCanNotBeEmpty"]=array();#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");#$conf["skipableVariableName"]=array();#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");#$conf["skipableVariableType"]=array();#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".#$conf["skipableVarDefaultValue"]=array("");#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.#$conf["arrayCountEqualCheck"][]=array();#參考資料來源:#array_keys=>http://php.net/manual/en/function.array-keys.php#建議:#增加可省略參數全部不能為空字串或空陣列的參數功能.$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);unset($conf["variableCheck::checkArguments"]);#如果檢查參數失敗if($checkArguments["status"]==="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"]=$checkArguments;#回傳結果return $result;}#if end#如果參數檢查不過if($checkArguments["passed"]==="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"]=$checkArguments;#回傳結果return $result;}#if end#產生搜尋process的指令#執行檢查程序是否依然存在#函式說明:#呼叫shell執行系統命令,並取得回傳的內容.#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息陣列.#$result["function"],當前執行的函數名稱.#$result["argu"],使用的參數.#$result["cmd"],執行的指令內容.#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.#$result["running"],是否還在執行.#$result["pid"],pid.#$result["statusCode"],執行結束後的代碼.#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu").#必填參數:#$conf["command"],字串,要執行的指令與.$conf["external::callShell"]["command"]="ps";#$conf["fileArgu"],字串,變數__FILE__的內容.$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];#可省略參數:#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.$conf["external::callShell"]["argu"]=array("-aux","|","cat","|","grep",$conf["cmd"],"|","grep",$conf["pid"]);#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").#$conf["arguIsAddr"]=array();#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".#$conf["enablePrintDescription"]="true";#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容.#$conf["printDescription"]="";#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".$conf["external::callShell"]["escapeshellarg"]="true";#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.#$conf["username"]="";#$conf["password"],字串,與$conf["username"]搭配的使用者密碼,預設不使用密碼,該參數不適用於apache環境.#$conf["password"]="";#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".#$conf["useScript"]="";#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.#$conf["inBackGround"]="";#備註:#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.#參考資料:#exec=>http://php.net/manual/en/function.exec.php#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php$callShell=external::callShell($conf["external::callShell"]);unset($conf["external::callShell"]);#如果執行程式失敗,且不是因為沒有抓取到內容的錯誤.if($callShell["status"]==="false" && $callShell["statusCode"]!==1 && $callShell["output"]!==array() ){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"]=$callShell;#回傳結果return $result;}#if end#預設假設程序已經執行完畢$result["exist"]="false";#如果程序還在執行if(count($callShell["output"])>0){#設置程序還在執行$result["exist"]="true";}#if end#設置執行正常$result["status"]="true";#回傳結果return $result;}#function processStatus end/*#函式說明:#呼叫shell依序執行系統命令,並取得回傳的內容.#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息陣列.#$result["function"],當前執行的函數名稱.#$result["content"],爲執行完每個指令後的回傳結果.#$result["cmd"],執行的指令內容,若"sameShell"參數為"true",則會有該數值.#$result["output"],執行後得到的輸出,若"sameShell"參數為"true",則會有該數值.#必填參數:#$conf["command"],字串陣列,要執行的指令.$conf["command"]=array("");#$conf["fileArgu"],字串,變數__FILE__的內容,預設為當前檔案的路徑與名稱.#$conf["fileArgu"]="";#可省略參數:#$conf["argu"],陣字串列,執行各個$conf["command"]時指令搭配的參數,預設為空陣列.#$conf["argu"]=array(array(""));#$conf["enablePrintDescription"],字串陣列,執行各個$conf["command"]時是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,其數量須與$conf["command"]的元素數量相同,若只有一個元素,則代表是每個$conf["command"]執行時都用此參數.#$conf["enablePrintDescription"]=array("false");#$conf["printDescription"],字串陣列,執行各個$conf["command"]前要印出來的的文字,預設為$conf["command"]的內容,其數量須與$conf["command"]的元素數量相同,若只有一個元素,則代表是每個$conf["command"]執行時都用此參數.#$conf["printDescription"]=array("");#$conf["escapeshellarg"],字串陣列,執行各個$conf["command"]時是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false",其數量須與$conf["command"]的元素數量相同,若只有一個元素,則代表是每個$conf["command"]執行時都用此參數.#$conf["escapeshellarg"]=array("false");#$conf["username"],陣列字串,每個指令要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.#$conf["username"]=array("");#$conf["password"],陣列字串,每個指令與$conf["username"]搭配的使用者密碼,預設不使用密碼,該參數不適用於apache環境.#$conf["password"]=array("");#$conf["useScript"],字串,每個指令的執行是否要啟用Linux的script指令來記錄輸出,"true"代表要;"false"代表不要,預設為"false".#$conf["useScript"]="";#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 ".qbpwcf_tmp/external/callShell/".#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";#$conf["inBackGround"],字串,每個指令的執行是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用";"區隔的多個指令將會出錯.#$conf["inBackGround"]="";#$conf["sameShell"],字串,"true"代表每個指令都在同空shell環境下執行,亦即會繼承變數、位置等資訊;預設為"false"代表要在個別獨立的shell環境下執行.#$conf["sameShell"]="true";#參考資料:#exec=>http://php.net/manual/en/function.exec.php#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php#備註:#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行.*/public static function callShellMulti(&$conf){#初始化要回傳的結果$result=array();#取得當前執行的函數名稱$result["function"]=__FUNCTION__;#如果 $conf 不為陣列if(gettype($conf)!="array"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"][]="\$conf變數須為陣列形態";#如果傳入的參數為 nullif($conf==null){#設置執行錯誤訊息$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";}#if end#回傳結果return $result;}#if end#參數檢查#函式說明:#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$reuslt["error"],執行不正常結束的錯訊息陣列.#$result["function"],當前執行的函式名稱.#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過#$result[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在#$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確#$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息#必填寫的參數:#$conf["variableCheck::checkArguments"]["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。$conf["variableCheck::checkArguments"]["varInput"]=&$conf;#$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("command","fileArgu");#$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double");$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("array","string");#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";#可以省略的參數:#$conf["variableCheck::checkArguments"]["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true"。#$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";#$conf["variableCheck::checkArguments"]["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("enablePrintDescription","printDescription","argu","escapeshellarg","username","password","useScript","logFilePath","inBackGround","sameShell");#$conf["variableCheck::checkArguments"]["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("array","array","array","array","array","array","string","string","string","string");#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,"null"代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(null,null,null,null,null,null,null,null,null,"false");#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"][]=array();$checkResult=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);unset($conf["variableCheck::checkArguments"]);#如果檢查參數失敗if($checkResult["status"]=="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤$result["error"]=$checkResult;#回傳結果return $result;}#if end#如果檢查參數不通過if($checkResult["passed"]=="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤$result["error"]=$checkResult;#回傳結果return $result;}#if end#有幾個 $conf["command"] 就執行幾次for($i=0;$i<count($conf["command"]);$i++){#如果 $conf["argu"] 存在if(isset($conf["argu"])){#如果 $conf["argu"] 為 array 型態,且其元素數量等於 $conf["command"] 的元素數量if(gettype($conf["argu"])=="array" && count($conf["argu"])==count($conf["command"])){#設置 $conf["argu"] 參數$conf["external::callShell"]["argu"]=$conf["argu"][$i];}#if end}#if end#如果 $conf["enablePrintDescription"] 存在if(isset($conf["enablePrintDescription"])){#如果 $conf["enablePrintDescription"] 為 array 型態,且其元素數量等於 $conf["command"] 的元素數量if(gettype($conf["enablePrintDescription"])=="array" && count($conf["enablePrintDescription"])==count($conf["command"])){#設置 $conf["enablePrintDescription"] 參數$conf["external::callShell"]["enablePrintDescription"]=$conf["enablePrintDescription"][$i];}#if end#反之如果 $conf["enablePrintDescription"] 為 array 型態,且只有一個else if(gettype($conf["enablePrintDescription"])=="array" && count($conf["enablePrintDescription"])==1){#設置 $conf["external::callShell"]["enablePrintDescription"] 參數為 $conf["enablePrintDescription"] 的第一個元素$conf["external::callShell"]["enablePrintDescription"]=$conf["enablePrintDescription"][0];}#if end}#if end#如果 $conf["printDescription"] 存在if(isset($conf["printDescription"])){#如果 $conf["printDescription"] 為 array 型態,且其元素數量等於 $conf["command"] 的元素數量if(gettype($conf["printDescription"])=="array" && count($conf["printDescription"])==count($conf["command"])){#設置 $conf["enablePrintDescription"] 參數$conf["external::callShell"]["printDescription"]=$conf["printDescription"][$i];}#if end#反之如果 $conf["printDescription"] 為 array 型態,且只有一個else if(gettype($conf["printDescription"])=="array" && count($conf["printDescription"])==1){#設置 $conf["external::callShell"]["printDescription"] 參數為 $conf["printDescription"] 的第一個元素$conf["external::callShell"]["printDescription"]=$conf["printDescription"][0];}#if end}#if end#如果 $conf["escapeshellarg"] 存在if(isset($conf["escapeshellarg"])){#如果 $conf["escapeshellarg"] 為 array 型態,且其元素數量等於 $conf["command"] 的元素數量if(gettype($conf["escapeshellarg"])=="array" && count($conf["escapeshellarg"])==count($conf["command"])){#設置 $conf["escapeshellarg"] 參數$conf["external::callShell"]["escapeshellarg"]=$conf["escapeshellarg"][$i];}#if end#反之如果 $conf["escapeshellarg"] 為 array 型態,且只有一個else if(gettype($conf["escapeshellarg"])=="array" && count($conf["escapeshellarg"])==1){#設置 $conf["external::callShell"]["escapeshellarg"] 參數為 $conf["escapeshellarg"] 的第一個元素$conf["external::callShell"]["escapeshellarg"]=$conf["escapeshellarg"][0];}#if end}#if end#如果 $conf["username"] 存在if(isset($conf["username"])){#如果 $conf["username"] 為 array 型態,且其元素數量等於 $conf["command"] 的元素數量if(gettype($conf["username"])=="array" && count($conf["username"])==count($conf["command"])){#設置 $conf["username"] 參數$conf["external::callShell"]["username"]=$conf["username"][$i];}#if end#反之如果 $conf["escapeshellarg"] 為 array 型態,且只有一個else if(gettype($conf["username"])=="array" && count($conf["username"])==1){#設置 $conf["external::callShell"]["username"] 參數為 $conf["username"] 的第一個元素$conf["external::callShell"]["username"]=$conf["username"][0];}#if end}#if end#如果 $conf["password"] 存在if(isset($conf["password"])){#如果 $conf["password"] 為 array 型態,且其元素數量等於 $conf["command"] 的元素數量if(gettype($conf["password"])=="array" && count($conf["password"])==count($conf["command"])){#設置 $conf["password"] 參數$conf["external::callShell"]["password"]=$conf["password"][$i];}#if end#反之如果 $conf["password"] 為 array 型態,且只有一個else if(gettype($conf["password"])=="array" && count($conf["password"])==1){#設置 $conf["external::callShell"]["password"] 參數為 $conf["password"] 的第一個元素$conf["external::callShell"]["password"]=$conf["password"][0];}#if end}#if end#如果有設置 $conf["logFilePath"]if(isset($conf["logFilePath"])){#設置 $conf["logFilePath"] 參數$conf["external::callShell"]["logFilePath"]=$conf["logFilePath"];}#if end#如果有設置 $conf["fileArgu"]if(isset($conf["fileArgu"])){#設置 $conf["fileArgu"] 參數$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];}#if end#如果有設置 $conf["inBackGround"]if(isset($conf["inBackGround"])){#設置 $conf["fileArgu"] 參數$conf["external::callShell"]["inBackGround"]=$conf["inBackGround"];}#if end#如果有指定 sameShellif($conf["sameShell"]==="true"){#設置不執行指令$conf["external::callShell"]["doNotRun"]="true";}#if end#函式說明:#呼叫shell執行系統命令,並取得回傳的內容.#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息陣列.#$result["function"],當前執行的函數名稱.#$result["cmd"],執行的指令內容.#$result["output"],爲執行完二元碼後的輸出陣列.#必填參數:#$conf["command"],字串,要執行的指令與.$conf["external::callShell"]["command"]=$conf["command"][$i];#可省略參數:#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.#$conf["argu"]=array("");#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".#$conf["enablePrintDescription"]="true";#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容.#$conf["printDescription"]="";#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".#$conf["escapeshellarg"]="false";#$conf["username"],字串,要用什麼使用者來執行,預設為執行apache的使用者,該參數不適用於apache環境.#$conf["username"]="";#$conf["password"],字串,與$conf["username"]搭配的使用者密碼,預設不使用密碼,該參數不適用於apache環境.#$conf["password"]="";#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要;"false"代表不要,預設為"false".#$conf["useScript"]="";#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 ".qbpwcf_tmp/external/callShell/".#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";#$conf["fileArgu"],字串,變數__FILE__的內容,預設為當前檔案的路徑與名稱.#$conf["fileArgu"]="";#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false".#$conf["inBackGround"]="";#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.#$conf["external::callShell"]["doNotRun"]="false";#備註:#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行.#參考資料:#exec=>http://php.net/manual/en/function.exec.php#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php$callShell=external::callShell($conf["external::callShell"]);unset($conf["external::callShell"]);#如果執行shell失敗if($callShell["status"]=="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤$result["error"]=$callShell;#回傳結果return $result;}#if end#如果有指定 sameShellif($conf["sameShell"]==="true"){#如果沒有指定 $rawCmdif(!isset($rawCmd)){#初始化要執行的指令內容$rawCmd=$callShell["cmd"];}#if end#反之else{#用";"串接指令$rawCmd=$rawCmd.";".$callShell["cmd"];}#else end}#if end#反之在個別shell環境下執行else{#紀錄每個指令執行的結果$result["content"][]=$callShell;}#else end}#for end#如果有 $rawCmd 存在if(isset($rawCmd)){#運行指令exec($rawCmd,$output,$status);#設置執行的指令內容.$result["cmd"]=$rawCmd;#執行後得到的輸出.$result["output"]=$output;}#if end#執行到這邊代表執行正常$result["status"]="true";#回傳結果return $result;}#function callShellMulti end/*#函式說明:#編譯C++檔案,下載輸出的2元碼.#回傳結果:#當 $conf["noDownload"] 設為 "true" 或執行出錯時 才會有回傳的結果.#$result["status"],"true"代表執行正常,"false"代表執行失敗.#$result["error"],錯誤訊息陣列.#$result["function"],正在執行的函式#$result["content"],編譯好的檔案位置.#必填參數:#$conf["cppAddress"],字串,要編譯的cpp檔位置,請加上cpp副檔名.$conf["cppAddress"]="";#可省略參數:#$conf["outputAddress"],字串,輸出的檔案位置與名稱,請加上附檔名,預設為".bin/cppOriFileName.bin".#$conf["outputAddress"]="";#$conf["noDownload"],字串,"true"為不要下載,改成回傳編譯好的檔案位置.#$conf["noDownload"]="true";#參考資料:#無.#備註:#測試中...#為了抓取編譯cpp產生的錯誤,要改用script指令來抓取輸出的結果.*/public static function compileCppThenDownload(&$conf){#初始化要回傳的變數$result=array();#取得當前執行的函式$result["function"]=__FUNCTION__;#如果 $conf 不為陣列if(gettype($conf)!="array"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"][]="\$conf變數須為陣列形態";#如果傳入的參數為 nullif($conf==null){#設置執行錯誤訊息$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";}#if end#回傳結果return $result;}#if end#檢查參數#函式說明:#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$reuslt["error"],執行不正常結束的錯訊息陣列.#$result["function"],當前執行的函式名稱.#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過#$result[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在#$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確#$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息#$result["argu"],字串陣列,目前輸入的參數名稱陣列.#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.#$result["notNeedVar"],字串陣列,多餘的參數名稱.#必填寫的參數:#$conf["variableCheck::checkArguments"]["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。$conf["variableCheck::checkArguments"]["varInput"]=&$conf;#$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("cppAddress");#$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double");$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string");#$conf["variableCheck::checkArguments"]["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";#可以省略的參數:#$conf["variableCheck::checkArguments"]["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true"。$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";#$conf["variableCheck::checkArguments"]["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("outputAddress","noDownload");#$conf["variableCheck::checkArguments"]["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string");#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(".bin/".$conf["cppAddress"].".bin","true");#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"][]=array();#參考資料來源:#array_keys=>http://php.net/manual/en/function.array-keys.php$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);unset($conf["variableCheck::checkArguments"]);#如果 $checkArguments["status"] 等於 "false"if($checkArguments["status"]=="false"){#設置 $result["status"] 為 "false"$result["status"]="false";#設置 $result["error"]$result["error"]=$checkArguments;#回傳結果return $result;}#if end#如果 $checkArguments["passed"] 等於 "false"if($checkArguments["passed"]=="false"){#設置 $result["status"] 為 "false"$result["status"]="false";#設置 $result["error"]$result["error"]=$checkArguments;#回傳結果return $result;}#if end#涵是說明:#取得檔案路徑字串的路徑與檔案的名稱與檔案副檔名#回傳結果:#$result["status"],執行是否正常,"true"正常,"false"代表不正常.#$result["error"],錯誤訊息.#$result["filePath"],路徑字串.#$result["fileName"],檔案名稱字串.#$result["fileExtention"],檔案的副檔名.#$result["fullFileName"],含副檔名的檔案名稱.#$result["fullFilePathAndName"],完整的檔案路徑(含副檔名).#必填參數:#$conf["fileAccess"]["getFileAddressAndNameAndFileExtention"]["fileAddressAndName"],字串,檔案名稱與其路徑.$conf["fileAccess"]["getFileAddressAndNameAndFileExtention"]["fileAddressAndName"]=$conf["outputAddress"];#可省略參數:#無.$filePathInfo=fileAccess::getFileAddressAndNameAndFileExtention($conf["fileAccess"]["getFileAddressAndNameAndFileExtention"]);unset($conf["fileAccess"]["getFileAddressAndNameAndFileExtention"]);#若取得檔案路徑字串的路徑與檔案的名稱與檔案副檔名失敗if($filePathInfo["status"]=="false"){#設置 $result["status"] 為 "false"$result["status"]="false";#設置 $result["error"]$result["error"]=$filePathInfo;#回傳結果return $result;}#if end#如果 $filePathInfo["filePath"] 不等於 ""if($filePathInfo["filePath"]!=""){#涵式說明:#建立資料夾,若要建立的資料夾所屬路徑不存在,則會自動嘗試建立,建立後的資料夾也可指定權限.#回傳的結果:#$result["status"],"true"爲建立成功,"false"爲建立失敗.#$result["error"],錯誤訊息陣列#必填參數::$conf["fileAccess"]["createFolderAfterCheck"]["dirPositionAndName"]=$filePathInfo["filePath"];#新建的位置與名稱#可省略參數:#$conf["fileAccess"]["createFolderAfterCheck"]["dirPermission"]="";#新建資料夾的權限設定,預設爲0770,亦即擁有者,同群組者可以讀,寫,存取,其他人僅能存取.$checkResult=fileAccess::createFolderAfterCheck($conf["fileAccess"]["createFolderAfterCheck"]);unset($conf["fileAccess"]["createFolderAfterCheck"]);#如果 $checkResult["status"] 等於 "false"if($checkResult["status"]=="false"){#設置錯誤識別$result["status"]="false";#設置錯誤訊息$result["error"]=$checkResult;#回傳結果return $result;}#if end}#if end#檢查目標 cpp 檔案在不在#涵式說明:檢查多個檔案與資料夾是否存在.#回傳結果:#$result["varName"][$i],爲第$i個資料夾或檔案的名稱。#$result["varExist"][$i],爲第$i個資料夾或檔案是否存在,"true"代表存在,"false"代表不存在。#必填參數:$conf["fileAccess"]["checkMultiFileExist"]["fileArray"]=array($conf["cppAddress"]);#要檢查書否存在的檔案有哪些,須爲一維陣列數值。#參考資料來源:#http://php.net/manual/en/function.file-exists.php#http://php.net/manual/en/control-structures.foreach.php$checkResult=fileAccess::checkMultiFileExist($conf["fileAccess"]["checkMultiFileExist"]);unset($conf["fileAccess"]["checkMultiFileExist"]);#如果 $checkResult["status"]為"false"if($checkResult["status"]=="false"){#設置錯誤識別$result["status"]="false";#設置錯誤訊息$result["error"]=$checkResult;#回傳結果return $result;}#if end#如果 $checkResult["varExist"][0] 等於 "false"if($checkResult["varExist"][0]=="false"){#設置錯誤識別$result["status"]="false";#設置錯誤訊息$result["error"][]="要編譯的檔案 ".$conf["cppAddress"]." 不存在";#回傳結果return $result;}#if end#執行系統命令 編譯 $conf["cppAddress"]#函式說明:#呼叫shell執行系統命令,並取得回傳的內容.#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息陣列.#$result["function"],當前執行的函數名稱.#$result["argu"],使用的參數.#$result["cmd"],執行的指令內容.#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.#$result["running"],是否還在執行.#$result["pid"],pid.#$result["statusCode"],執行結束後的代碼.#$result["escape"],陣列,儲存出新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu").#必填參數:#$conf["command"],字串,要執行的指令.$conf["external::callShell"]["command"]="g++";#$conf["fileArgu"],字串,變數__FILE__的內容.$conf["external::callShell"]["fileArgu"]=__FILE__;#可省略參數:#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.$conf["external::callShell"]["argu"]=array($conf["cppAddress"],"-o",$conf["outputAddress"]);#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").#$conf["arguIsAddr"]=array();#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".#$conf["enablePrintDescription"]="true";#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.#$conf["printDescription"]="";#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.$conf["external::callShell"]["escapeshellarg"]="true";#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.#$conf["thereIsShellVar"]=array();#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.#$conf["username"]="";#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.#$conf["password"]="";#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".#$conf["useScript"]="";#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.#$conf["inBackGround"]="";#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.#$conf["getErr"]="false";#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.#$conf["doNotRun"]="false";#參考資料:#exec=>http://php.net/manual/en/function.exec.php#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php#備註:#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.$execResult=external::callShell($conf["external::callShell"]);unset($conf["external::callShell"]);#如果 $execResult["status"] 等於 "false"if($execResult["status"]=="false"){#設置錯誤識別$result["status"]="false";#設置錯誤訊息$result["error"]=$execResult;#回傳結果return $result;}#if end#如果 $conf["noDownload"] 有設置了if(isset($conf["noDownload"])){#如果$conf["noDownload"] 等於 "true"if($conf["noDownload"]=="true"){#設置執行正常的識別$result["status"]="true";#設置編譯好的檔案位置$result["content"]=$conf["outputAddress"];#回傳結果return $result;}#if end}#if end#說明:#要求使用者Download某檔案#回傳結果:#true爲成功,反之爲錯誤訊息#必填參數::$conf["header"]["askUserDownloadFile"]["filePositionAndName"]=$conf["outputAddress"];#檔案的位置與名稱#可省略參數:$conf["header"]["askUserDownloadFile"]["fileDisplayName"]=$filePathInfo["fullFileName"];#要顯示的檔案名稱,若要放中文請將其文字放在()裏面,這樣種文字才會顯現。預設為 basename($conf["filePositionAndName"])." date:".$系統時間;#參考資料來源:#http:#php.net/manual/en/function.readfile.phpreturn header::askUserDownloadFile($conf["header"]["askUserDownloadFile"]);unset($conf["header"]["askUserDownloadFile"]);}#function compileCppThenDownload end/*#函式說明:#運用「nohup」與「&」來達到雖依序執行但不等待前一個指令執行完再執行下一個指令,形成在背景假多工的執行每個指令.#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息陣列.#$result["function"],當前執行的函數名稱.#$result["cmd"],執行的指令內容.#$result["output"],爲執行完二元碼後的輸出陣列.#必填參數:#$conf["command"],字串陣列,要執行的指令$conf["command"]=array("");#$conf["fileArgu"],字串,變數__FILE__的內容.$conf["fileArgu"]=__FILE__;#可省略參數:#$conf["argu"],二維陣列字串,每個指令搭配的參數,預設為空陣列.#$conf["argu"]=array(array());#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".#$conf["enablePrintDescription"]="true";#$conf["printDescription"],字串陣列,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容.#$conf["printDescription"]=array("");#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".#$conf["escapeshellarg"]="false";#$conf["username"],字串陣列,每個指令要用什麼使用者來執行,預設為執行php使用者,該參數不適用於apache環境.#$conf["username"]=array("");#$conf["password"],字串陣列,與$conf["username"]搭配的使用者密碼,預設不使用密碼,該參數不適用於apache環境.#$conf["password"]=array("");#$conf["web"],字串,腳本檔案是存放在web則為"true";若為檔案系統則為"false",預設為"false".#$conf["web"]=""#$conf["wait"],字串,執行時是否要等待前一個程序執行完,預設為"false"不等待,反之為"true"要等待.#$conf["wait"]="true";#參考資料:#無.#備註:#無.*/public static function multiThreadsShell(&$conf){#初始化要回傳的結果$result=array();#取得當前執行的函數名稱$result["function"]=__FUNCTION__;#如果 $conf 不為陣列if(gettype($conf)!="array"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"][]="\$conf變數須為陣列形態";#如果傳入的參數為 nullif($conf==null){#設置執行錯誤訊息$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";}#if end#回傳結果return $result;}#if end#參數檢查#函式說明:#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$reuslt["error"],執行不正常結束的錯訊息陣列.#$result["function"],當前執行的函式名稱.#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過#$result[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在#$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確#$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息#必填寫的參數:#$conf["variableCheck::checkArguments"]["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。$conf["variableCheck::checkArguments"]["varInput"]=&$conf;#$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("command","fileArgu");#$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double");$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("array","string");#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";#可以省略的參數:#$conf["variableCheck::checkArguments"]["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true"。#$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";#$conf["variableCheck::checkArguments"]["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("enablePrintDescription","printDescription","argu","escapeshellarg","username","password","web","wait");#$conf["variableCheck::checkArguments"]["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","array","array","string","array","array","string","string");#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,"null"代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("false",$conf["command"],null,"false",null,null,"false","false");#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"][]=array();$checkResult=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);unset($conf["variableCheck::checkArguments"]);#如果檢查參數失敗if($checkResult["status"]=="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤$result["error"]=$checkResult;#回傳結果return $result;}#if end#如果檢查參數不通過if($checkResult["passed"]=="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤$result["error"]=$checkResult;#回傳結果return $result;}#if end#初始化儲存要寫入到腳本檔案裡面的內容$cmdStr="";#初始化儲存暫存輸出結果的檔案$scriptOutFileArray=array();#針對每個 $conf["command"]for($i=0;$i<count($conf["command"]);$i++){#如果有設定 $conf["enablePrintDescription"] 為 "true"if($conf["enablePrintDescription"]=="true"){#如果存在 $conf["printDescription"][$i]if(isset($conf["printDescription"][$i])){#儲存 印出描述與換行 的指令$cmdArray[]="echo ".$conf["printDescription"][$i]." \r\n";}#if end#如果存在 $conf["argu"]if(isset($conf["argu"])){#如果存在 $conf["argu"][$i]if(isset($conf["argu"][$i])){#如果 $conf["argu"][$i] 形態為 "array"if(gettype($conf["argu"][$i])=="array"){#參數 $conf["argu"][$i] 有幾個元素就執行幾次for($j=0;$j<count($conf["argu"][$i]);$j++){#如果 $conf["escapeshellarg"] 為 "true"if($conf["escapeshellarg"]=="true"){#過濾參數$conf["argu"][$i]=escapeshellarg($conf["argu"][$i]);}#if end#附加參數$conf["command"][$i]=$conf["command"][$i]." ".$conf["argu"][$i];}#for end}#if end}#if end}#if end}#if end#如果 $conf["username"] 存在if(isset($conf["username"])){#如果 $conf["username"][$i] 存在if(isset($conf["username"][$i])){#如果 $conf["password"] 存在if(isset($conf["password"])){#如果 $conf["password"][$i] 存在if(isset($conf["password"][$i])){#加上使用特定使用者身份的語法與驗證的密碼$conf["command"][$i]="echo ".$conf["password"][$i]." | su ".$conf["username"][$i]." -c'".$conf["command"][$i]."'";}#if end}#if end#反之else{#加上使用特定使用者身份的語法$conf["command"][$i]="su ".$conf["username"][$i]." -c'".$conf["command"][$i]."'";}#else end}#if end}#if end#如果不等待前一個指令if($conf["wait"]==="false"){#加上 & 到 $conf["command"][$i]$conf["command"][$i]=$conf["command"][$i]." &".PHP_EOL;}#if end#反之要等待前一個指令else{#僅加上換行$conf["command"][$i]=$conf["command"][$i]." ".PHP_EOL;}#else end#儲存要執行的指令$cmdStr=$cmdStr.$conf["command"][$i];}#for end#debug#var_dump($cmdStr);#建立用來儲存指令的腳本檔案#涵式說明:#將字串寫入到檔案#回傳的結果:#$result["status"],"true"表示檔案寫入成功,"false"表示檔案寫入失敗.#$result["error"],錯誤訊息陣列.#$result["function"],當前執行的函數名稱.#$result["fileInfo"],實際上寫入的檔案資訊陣列.#$result["fileInfo"]["createdFileName"],建立好的檔案名稱.#$result["fileInfo"]["createdFilePath"],檔案建立的路徑.#$result["fileInfo"]["createdFilePathAndName"].建立好的檔案名稱與路徑.#必填參數::#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑$conf["fileAccess::writeTextIntoFile"]["fileArgu"]=$conf["fileArgu"];#可省略參數:#$conf["fileName"],字串,爲要編輯的檔案名稱,預設為隨機產生的檔案名稱.#$conf["fileName"]="";#$conf["inputString"],字串,爲要寫入到裏面的內容,若要每筆資料寫入後換行,則可以在字串內容後面加上 \r\n 即可,預設為"".$conf["fileAccess::writeTextIntoFile"]["inputString"]=$cmdStr;#爲要寫入到裏面的內容,若要每筆資料寫入後換行,則可以在字串內容後面加上 \r\n 即可.#$conf["writeMethod"],字串,爲檔案撰寫的方式,可省略,是複寫'a'還是,重新寫入'w',預設爲'w',重新寫入.#$conf["writeMethod"]="a";#$conf["checkRepeat"],字串,"true"代表建立檔案之前要先檢查檔案是否存在,若存在則在原名稱後面加上從(1)開始的編號.#$conf["checkRepeat"]="";#$conf["filenameExtensionStartPoint"],字串,檔案的副檔名是從倒數第幾個小數點(dot)開始,預設為"1",最後一個小數點,必須與$conf["checkRepeat"]搭配才會生效.#$conf["filenameExtensionStartPoint"]="";#$conf["repeatNameRule"],字串,遇到相同名稱的檔案要如何加上識別的編號,編號用「\$i」表示,預設為"(\$i)",必須與$conf["checkRepeat"]搭配才會生效.#$conf["repeatNameRule"]="";#$conf["web"],檔案是否位於網站上"true",若是在檔案系統則為"false",預設為"true".$conf["fileAccess::writeTextIntoFile"]["web"]="false";$createShellScriptFile=fileAccess::writeTextIntoFile($conf["fileAccess::writeTextIntoFile"]);unset($conf["fileAccess::writeTextIntoFile"]);#debug#var_dump($createShellScriptFile);#exit;#如果建立腳本檔案失敗if($createShellScriptFile["status"]=="false"){#設置執行不正常$result["status"]="false";#設置執行錯誤$result["error"]=$createShellScriptFile;#回傳結果return $result;}#if end#移除腳本的指令$delScript="rm ".$createShellScriptFile["fileInfo"]["createdFilePathAndName"].";";#附加寫入移除腳本的指令#回傳的結果:#$result["status"],"true"表示檔案寫入成功,"false"表示檔案寫入失敗.#$result["error"],錯誤訊息陣列.#$result["function"],當前執行的函數名稱.#$result["fileInfo"],實際上寫入的檔案資訊陣列.#$result["fileInfo"]["createdFileName"],建立好的檔案名稱.#$result["fileInfo"]["createdFilePath"],檔案建立的路徑.#$result["fileInfo"]["createdFilePathAndName"].建立好的檔案名稱與路徑.#必填參數::#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑$conf["fileAccess::writeTextIntoFile"]["fileArgu"]=$conf["fileArgu"];#可省略參數:$conf["fileAccess::writeTextIntoFile"]["fileName"]=$createShellScriptFile["fileInfo"]["createdFilePathAndName"];#爲要編輯的檔案名稱$conf["fileAccess::writeTextIntoFile"]["inputString"]=$delScript;#爲要寫入到裏面的內容,若要每筆資料寫入後換行,則可以在字串內容後面加上 \r\n 即可.#$conf["writeMethod"],字串,爲檔案撰寫的方式,可省略,是複寫'a'還是,重新寫入'w',預設爲'w',重新寫入.$conf["fileAccess::writeTextIntoFile"]["writeMethod"]="a";#$conf["checkRepeat"],字串,"true"代表建立檔案之前要先檢查檔案是否存在,若存在則在原名稱後面加上從(1)開始的編號.#$conf["checkRepeat"]="";#$conf["filenameExtensionStartPoint"],字串,檔案的副檔名是從倒數第幾個小數點(dot)開始,預設為"1",最後一個小數點,必須與$conf["checkRepeat"]搭配才會生效.#$conf["filenameExtensionStartPoint"]="";#$conf["repeatNameRule"],字串,遇到相同名稱的檔案要如何加上識別的編號,編號用「\$i」表示,預設為"(\$i)",必須與$conf["checkRepeat"]搭配才會生效.#$conf["repeatNameRule"]="";#$conf["web"],檔案是否位於網站上"true",若是在檔案系統則為"false",預設為"true".$conf["fileAccess::writeTextIntoFile"]["web"]="false";$createShellScriptFile=fileAccess::writeTextIntoFile($conf["fileAccess::writeTextIntoFile"]);unset($conf["fileAccess::writeTextIntoFile"]);#如果建立腳本檔案失敗if($createShellScriptFile["status"]=="false"){#設置執行不正常$result["status"]="false";#設置執行錯誤$result["error"]=$createShellScriptFile;#回傳結果return $result;}#if end#debug#exec("echo '".$createShellScriptFile["fileInfo"]["createdFilePathAndName"]."' > /var/www/html/latest/tmp/debug1.log",$resultArray,$execStatus);#exec("cat '".$createShellScriptFile["fileInfo"]["createdFilePathAndName"]."' > /var/www/html/latest/tmp/debug2.log",$resultArray,$execStatus);#執行腳本程式,將輸出結果儲存在 $resultArray 陣列裏面#exec("sh ".$createShellScriptFile["fileInfo"]["createdFilePathAndName"]." > /dev/null 2>&1 &",$resultArray,$execStatus);#函式說明:#呼叫shell執行系統命令,並取得回傳的內容.#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息陣列.#$result["function"],當前執行的函數名稱.#$result["argu"],使用的參數.#$result["cmd"],執行的指令內容.#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.#$result["running"],是否還在執行.#$result["pid"],pid.#$result["statusCode"],執行結束後的代碼.#必填參數:#$conf["command"],字串,要執行的指令與.$conf["external::callShell"]["command"]="sh";#$conf["fileArgu"],字串,變數__FILE__的內容.$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];#可省略參數:#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.$conf["external::callShell"]["argu"]=array($createShellScriptFile["fileInfo"]["createdFilePathAndName"],">","/dev/null","2>&1","&");#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").#$conf["arguIsAddr"]=array();#$conf["plainArgu"],字串陣列,哪幾個參數不要加上"",元算若為"true"則代表不用包;反之"false"則代表要包.#$conf["external::callShell"]["plainArgu"]=array("false","true","true","true","true");#$conf["useApostrophe"],字串陣列,如果有需要包住,則用「'」,而非「"」處理.前者為"true";後者為"false".#$conf["external::callShell"]["useApostrophe"]=array("true","false","false","false","false");#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".#$conf["enablePrintDescription"]="true";#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.#$conf["printDescription"]="";#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".$conf["external::callShell"]["escapeshellarg"]="true";#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.#$conf["username"]="";#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.#$conf["password"]="";#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".#$conf["useScript"]="";#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.#$conf["inBackGround"]="";#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.#$conf["getErr"]="false";#參考資料:#exec=>http://php.net/manual/en/function.exec.php#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php#備註:#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.$callShell=external::callShell($conf["external::callShell"]);unset($conf["external::callShell"]);$fsock=fopen("/var/www/html/latest/tmp/debug","w");fwrite($fsock,print_r($callShell,true));fclose($fsock);#如果執行失敗if($callShell["status"]==="false"){#設置執行不正常$result["status"]="false";#設置執行錯誤$result["error"]=$callShell;#回傳結果return $result;}#if end#設置執行正常$result["status"]="true";#回傳結果return $result;}#function multiThreadsShell end/*#函式說明:#呼叫shell執行系統命令,中止執行中的pid.#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息陣列.#$result["function"],當前執行的函數名稱.#$result["warning"],警告訊息陣列.#$result["cmd"],執行的指令內容.#$result["output"],爲執行完二元碼後的輸出陣列.#$result["runing"],程式是否還在執行.#必填參數:#$conf["fileArgu"],字串,變數__FILE__的內容.$conf["fileArgu"]=__FILE__;#$conf["pid"],字串,執行序的pid.$conf["pid"]="";#$conf["cmd"],字串,要中止的指令關鍵字.$conf["cmd"]="";#可省略參數:#無.#參考資料:#http://php.net/manual/en/function.exec.php#備註:#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,透過su使用root身份可能會被selinux阻擋.*/public static function killProcess(&$conf){#初始化要回傳的結果$result=array();#取得當前執行的函數名稱$result["function"]=__FUNCTION__;#如果沒有參數if(func_num_args()==0){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"]="函數".$result["function"]."需要參數";#回傳結果return $result;}#if end#取得參數$result["argu"]=$conf;#如果 $conf 不為陣列if(gettype($conf)!=="array"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"][]="\$conf變數須為陣列形態";#如果傳入的參數為 nullif($conf===null){#設置執行錯誤訊息$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";}#if end#回傳結果return $result;}#if end#檢查參數#函式說明:#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$reuslt["error"],執行不正常結束的錯訊息陣列.#$result["function"],當前執行的函式名稱.#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.#$result["argu"],字串陣列,目前輸入的參數名稱陣列.#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.#$result["notNeedVar"],字串陣列,多餘的參數名稱.#必填寫的參數:#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。$conf["variableCheck::checkArguments"]["varInput"]=&$conf;#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("pid","cmd","fileArgu");#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string","string");#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";#可以省略的參數:#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.#$conf["canBeEmptyString"]="false";#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.#$conf["canNotBeEmpty"]=array();#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.#$conf["canBeEmpty"]=array();#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.#$conf["skipableVariableCanNotBeEmpty"]=array();#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");#$conf["skipableVariableName"]=array();#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");#$conf["skipableVariableType"]=array();#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".#$conf["skipableVarDefaultValue"]=array("");#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.#$conf["arrayCountEqualCheck"][]=array();#參考資料來源:#array_keys=>http://php.net/manual/en/function.array-keys.php#建議:#增加可省略參數全部不能為空字串或空陣列的參數功能.$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);unset($conf["variableCheck::checkArguments"]);#如果檢查參數失敗if($checkArguments["status"]==="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"]=$checkArguments;#回傳結果return $result;}#if end#如果參數檢查不過if($checkArguments["passed"]==="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"]=$checkArguments;#回傳結果return $result;}#if end#函式說明:#檢查程序是否還在執行#回傳結果#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息陣列.#$result["function"],當前執行的函數名稱.#$result["exist"],程序是否存在,"true"代表存在,"false"代表不存在.#必填參數:#$conf["pid"],字串,程序的id.$conf["external::processStatus"]["pid"]=$conf["pid"];#$conf["cmd"],字串,程序的指令.$conf["external::processStatus"]["cmd"]=$conf["cmd"];#$conf["fileArgu"],字串,__FILE__的內容,__FILE__的內容.$conf["external::processStatus"]["fileArgu"]=$conf["fileArgu"];$processStatus=external::processStatus($conf["external::processStatus"]);unset($conf["external::processStatus"]);#如果檢查程序是否還在執行失敗if($processStatus["status"]==="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"]=$processStatus;#回傳結果return $result;}#if end#如果程序沒有在執行if($processStatus["exist"]==="false"){#設置執行成功$result["status"]="true";#設置警告訊息$result["warning"][]="要中止的程序不存在";#回傳結果return $result;}#if end#中止程式#函式說明:#呼叫shell執行系統命令,並取得回傳的內容.#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息陣列.#$result["function"],當前執行的函數名稱.#$result["cmd"],執行的指令內容.#$result["output"],爲執行完二元碼後的輸出陣列.#必填參數:#$conf["command"],字串,要執行的指令與.$conf["external::callShell"]["command"]="kill";#$conf["fileArgu"],字串,變數__FILE__的內容.$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];#可省略參數:#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.$conf["external::callShell"]["argu"]=array($conf["pid"]);#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").#$conf["arguIsAddr"]=array();#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".#$conf["enablePrintDescription"]="true";#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容.#$conf["printDescription"]="";#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".$conf["external::callShell"]["escapeshellarg"]="true";#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.#$conf["username"]="";#$conf["password"],字串,與$conf["username"]搭配的使用者密碼,預設不使用密碼,該參數不適用於apache環境.#$conf["password"]="";#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".#$conf["useScript"]="";#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.#$conf["inBackGround"]="";#備註:#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.#參考資料:#exec=>http://php.net/manual/en/function.exec.php#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php$callShell=external::callShell($conf["external::callShell"]);unset($conf["external::callShell"]);#如果執行程式失敗if($callShell["status"]==="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"]=$callShell;#回傳結果return $result;}#if end#檢查程序是否已經中止#函式說明:#檢查程序是否還在執行#回傳結果#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息陣列.#$result["function"],當前執行的函數名稱.#$result["exist"],程序是否存在,"true"代表存在,"false"代表不存在.#必填參數:#$conf["pid"],字串,程序的id.$conf["external::processStatus"]["pid"]=$conf["pid"];#$conf["cmd"],字串,程序的指令.$conf["external::processStatus"]["cmd"]=$conf["cmd"];#$conf["fileArgu"],字串,__FILE__的內容,__FILE__的內容.$conf["external::processStatus"]["fileArgu"]=$conf["fileArgu"];$processStatus=external::processStatus($conf["external::processStatus"]);unset($conf["external::processStatus"]);#如果檢查程序是否還在執行失敗if($processStatus["status"]==="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"]=$processStatus;#回傳結果return $result;}#if end#如果程序沒有在執行if($processStatus["exist"]==="false"){#設置執行成功$result["status"]="true";#回傳結果return $result;}#if end#反之else{#再度嘗試中止程式return external::killProcess($conf);unset($conf);}#else end}#function killProcess end/*#函式說明:#檢查目標程式是否執行超過時間,將之中止.#回傳結果#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息陣列.#$result["function"],當前執行的函數名稱.#$result["timeout"],程序是否timeout,"true"代表是,"false"代表不是.#必填參數:#$conf["pid"],字串,程序的id.$conf["pid"]=$conf["pid"];#$conf["cmd"],字串,程序的指令.$conf["cmd"]=$conf["cmd"];#$conf["fileArgu"],字串,__FILE__的內容,__FILE__的內容.$conf["fileArgu"]=$conf["fileArgu"];#$conf["timeout"],整數,多少時間過後要timeout,單位為秒.$conf["timeout"]=60;#可省略參數:#無.#參考資料:#無.#備註:#無.*/public static function timeout(&$conf=array()){#初始化要回傳的結果$result=array();#取得當前執行的函數名稱$result["function"]=__FUNCTION__;#如果沒有參數if(func_num_args()==0){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"]="函數".$result["function"]."需要參數";#回傳結果return $result;}#if end#取得參數$result["argu"]=$conf;#如果 $conf 不為陣列if(gettype($conf)!=="array"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"][]="\$conf變數須為陣列形態";#如果傳入的參數為 nullif($conf===null){#設置執行錯誤訊息$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";}#if end#回傳結果return $result;}#if end#檢查參數#函式說明:#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$reuslt["error"],執行不正常結束的錯訊息陣列.#$result["function"],當前執行的函式名稱.#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.#$result["argu"],字串陣列,目前輸入的參數名稱陣列.#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.#$result["notNeedVar"],字串陣列,多餘的參數名稱.#必填寫的參數:#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。$conf["variableCheck::checkArguments"]["varInput"]=&$conf;#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("cmd","fileArgu","pid","timeout");#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string","string","integer");#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";#可以省略的參數:#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.#$conf["canBeEmptyString"]="false";#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.#$conf["canNotBeEmpty"]=array();#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.#$conf["canBeEmpty"]=array();#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.#$conf["skipableVariableCanNotBeEmpty"]=array();#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");#$conf["skipableVariableName"]=array();#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");#$conf["skipableVariableType"]=array();#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".#$conf["skipableVarDefaultValue"]=array("");#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.#$conf["arrayCountEqualCheck"][]=array();#參考資料來源:#array_keys=>http://php.net/manual/en/function.array-keys.php#建議:#增加可省略參數全部不能為空字串或空陣列的參數功能.$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);unset($conf["variableCheck::checkArguments"]);#如果檢查參數失敗if($checkArguments["status"]==="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"]=$checkArguments;#回傳結果return $result;}#if end#如果參數檢查不過if($checkArguments["passed"]==="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"]=$checkArguments;#回傳結果return $result;}#if end#無窮迴圈while(true){#函式說明:#檢查程序是否還在執行#回傳結果#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息陣列.#$result["function"],當前執行的函數名稱.#$result["exist"],程序是否存在,"true"代表存在,"false"代表不存在.#必填參數:#$conf["pid"],字串,程序的id.$conf["external::processStatus"]["pid"]=$conf["pid"];#$conf["cmd"],字串,程序的指令.$conf["external::processStatus"]["cmd"]=$conf["cmd"];#$conf["fileArgu"],字串,__FILE__的內容,__FILE__的內容.$conf["external::processStatus"]["fileArgu"]=$conf["fileArgu"];$processStatus=external::processStatus($conf["external::processStatus"]);unset($conf["external::processStatus"]);#如果執行失敗if($processStatus["status"]==="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"]=$processStatus;#回傳結果return $result;}#if end#如果程序已經不在if($processStatus["exist"]==="false"){#設置執行正常$result["status"]="true";#設置沒有timeout$result["timeout"]="false";#回傳結果return $result;}#if end#如果已經timeoutif($conf["timeout"]===0){#設置執行正常$result["status"]="true";#設置有timeout$result["timeout"]="true";#中止該程序#函式說明:#呼叫shell執行系統命令,中止執行中的pid.#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息陣列.#$result["function"],當前執行的函數名稱.#$result["warning"],警告訊息陣列.#$result["cmd"],執行的指令內容.#$result["output"],爲執行完二元碼後的輸出陣列.#$result["runing"],程式是否還在執行.#必填的參數#$conf["fileArgu"],字串,變數__FILE__的內容.$conf["external::killProcess"]["fileArgu"]=$conf["fileArgu"];#$conf["pid"],字串,執行序的pid.$conf["external::killProcess"]["pid"]=$conf["pid"];#$conf["cmd"],字串,要中止的指令關鍵字.$conf["external::killProcess"]["cmd"]=$conf["cmd"];#備註:#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,透過su使用root身份可能會被selinux阻擋.#參考資料:#http://php.net/manual/en/function.exec.php$killProcess=external::killProcess($conf["external::killProcess"]);unset($conf["external::killProcess"]);#如果中止程序失敗if($killProcess["status"]==="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"]=$killProcess;#回傳結果return $result;}#if end#回傳結果return $result;}#if end#休息一秒sleep(1);#剩餘timeout時間減一$conf["timeout"]--;}#while end}#function timeout end}#class external end?>