Rev 3 | Rev 130 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
#!/usr/bin/php<?php/*QBPWCF, Quick Build PHP website Component base on Fedora Linux.Copyright (C) 2015~2024 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/>.*//*說明:註冊服務.範例:#每次執行 ps auxwf | cat 後,下一秒就刷新畫面重新執行.registerService.php --cmds clear --cmds 'ps auxwf | cat' --cmds 'sleep 1'備註:目前不支援在 ostree 環境下運行.*/#指派命名空間namespace qbpwcf;#取得 lib pathexec("php -f ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/libexec/folderOfUsrLib.php"),$output,$status);#如果執行失敗if($status!==0){#debugvar_dump(__LINE__,$output);#結束執行,回傳shell 1.exit(1);}#if end#儲存lib path$folderOfUsrLib=$output[0];#以該檔案的實際位置的 lib path 為 include path 首位$output=array();exec("cd ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/../".$folderOfUsrLib."/qbpwcf").";pwd;",$output,$status);#如果執行失敗if($status!==0){#debugvar_dump(__LINE__,$output);#結束執行,回傳shell 1.exit(1);}#if end#設置 include pathset_include_path($output[0].PATH_SEPARATOR.get_include_path());#匯入外部套件include("allInOne.php");#說明函式function help(){#印出指令說明echo "Usage of ".basename(__FILE__).":".PHP_EOL;echo "--cmd [command to run] 代表要執行的指令,若有多個則會按照順序執行.".PHP_EOL;echo "--name [service name] 服務的名稱".PHP_EOL;echo "--runOnBoot [yes/no] 開機好就要啟動該服務,預設為yes".PHP_EOL;echo "--runNow [yes/no] 馬上運行服務,預設為yes".PHP_EOL;#結束執行exit;}#函式說明:#解析參數.#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$reuslt["error"],執行不正常結束的錯訊息陣列.#$result["function"],當前執行的函式名稱.#$result["content"],解析好的參數陣列.#$result["content"][$key][$i],參數 $key 的 $i+1 個參數數值內容.#$result["program"],字串,執行的程式名稱.#必填參數:#無.#可省略參數:#$conf["helpFunc"],如果解析的參數不成對,則要執行的函式名稱.$conf["helpFunc"]="help";#$conf["parseFormVar"],解析表單變數(get\post),預設為"false"不使用;反之設置為"true".$conf["parseFormVar"]="false";#參考資料:#無.#備註:#僅能在命令列底下執行.#以後可將參數 --a--b 的名稱與後面的數值 $value 存成 $result["a"]["b"][$i]=$value .$parseArgu=cmd::parseArgu($conf);unset($conf);#如果取得參數失敗if($parseArgu["status"]==="false"){#設置執行失敗$result["status"]="false";#設置執行錯誤訊息$result["error"]=$parseArgu;#印出訊息var_dump($result);#結束執行exit;}#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["varInput"]=&$parseArgu["content"];#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.$conf["referenceVarKey"]="variableCheck::checkArguments";#可以省略的參數:#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");$conf["mustBeFilledVariableName"]=array("cmd","name");#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.$conf["mustBeFilledVariableType"]=array("array","array");#$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("backTime");#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");$conf["skipableVariableName"]=array("runOnBoot","runNow");#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");$conf["skipableVariableType"]=array("array","array");#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".$conf["skipableVarDefaultValue"]=array(array("yes"),array("yes"));#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".#$conf["variableCheck::checkArguments"]["disallowAllSkipableVarIsEmpty"]="false";#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".#$conf["disallowAllSkipableVarIsEmptyArray"]="";#$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);unset($conf);#若執行失敗if($checkArguments["status"]==="false"){#印出結果var_dump($checkArguments);#結束執行exit;}#if end#若檢查不通過if($checkArguments["passed"]==="false"){#debugvar_dump($checkArguments);#提示用法help();#結束執行exit;}#if end#取得指令陣列$cmds=$parseArgu["content"]["cmd"];#取得目標參數$serviceName=$parseArgu["content"]["name"][0];#取得runOnBoot參數$runOnBoot=$parseArgu["content"]["runOnBoot"][0];#預設為 false$runOnBoot="false";#如果是 yesif($runOnBoot==="yes"){#轉換為 "true"$runOnBoot="true";}#if end#取得runNow參數$runNow=$parseArgu["content"]["runNow"][0];#預設為 false$runNow="false";#如果是 yesif($runNow==="yes"){#轉換為 "true"$runNow="true";}#if end#建立暫存檔案#函式說明:#於本套件位置底下的tmp資料夾下建立與回傳暫存檔案名稱路徑#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息.#$result["function"],當前執行的函數名稱.#$result["content"],暫存檔案的路徑與名稱.#$result["fileName"],暫存檔案的名稱.#$result["path"],暫存檔案的路徑.#必填參數:#無.#可省略參數:#無.#參考資料:#無.#備註:#無.$conf=array();$createTempFile=fileAccess::createTempFile($conf);unset($conf);#如果建立暫存檔案失敗if($createTempFile["status"]==="false"){#debugvar_dump($createTempFile);#結束執行,回傳 shell 1exit(1);}#if end#撰寫要執行的程式內容#函式說明:#將多行字串寫入到檔案#回傳結果:#$result["status"],"true"表示檔案寫入成功,"false"表示檔案寫入失敗.#$result["error"],錯誤訊息陣列.#$result["function"],當前執行函數的名稱.#必填參數:#$conf["fileName"],字串,爲要編輯的檔案名稱$conf["fileName"]=$createTempFile["content"];#$conf["inputString"],字串陣列,爲要寫入到 $conf["fileName"] 裏面的內容. $conf["inputString"][$i] 代表第 $i+1 行。$conf["inputString"]=$cmds;#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑$conf["fileArgu"]=__FILE__;#可省略參數:#$conf["writeMethod"]="a";#爲檔案撰寫的方式,可省略,是複寫'a'還是,重新寫入'w',預設爲'w',重新寫入。#參考資料:#無.#備註:#無.$writeMultiLine=fileAccess::writeMultiLine($conf);unset($conf);#如果建立檔案失敗if($writeMultiLine["status"]==="false"){#debugvar_dump($writeMultiLine);#結束執行,回傳 shell 1exit(1);}#if end#移動程式到 /usr/bin/ 底下#函式說明:#移動檔案#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["function"],當前執行的函數名稱#$result["error"],錯誤訊息.#$result["content"],檔案輸出後的位置與名稱.#必填參數:#$conf["from"],字串,要移動的檔案名稱與位置.$conf["from"]=$createTempFile["content"];#$conf["to"],字串,要移動到的位置與名稱$conf["to"]="/usr/bin/".$serviceName.".sh";#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑$conf["fileArgu"]=__FILE__;#可省略參數:#$conf["commentsArray"],字串陣列,提示的文字描述,$conf["commentsArray"][$i]代表第($+1)行的描述.#$conf["commentsArray"]=array("");#參考資料:#無.#備註:#無.$mv=fileAccess::mv($conf);unset($conf);#如果移動檔案失敗if($mv["status"]==="false"){#debugvar_dump($mv);#結束執行,回傳 shell 1exit(1);}#if end#函式說明:#將要執行的程式變成透過 systemd 來運行.#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息.#$result["function"],當前執行的函式名稱.#$result["argu"],所使用的參數.#$result["content"],執行的結果.#必填參數:#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.$conf["fileArgu"]=__FILE__;#$conf["name"],字串,服務名稱,實際產生的system名稱會加上".service".$conf["name"]=$serviceName;#$conf["cmd"],字串,要執行的指令.$conf["cmd"]=$serviceName.".sh";#可省略參數:#$conf["params"],字串陣列,指令要使用的參數.#$conf["params"]=array("");#$conf["enable"],字串,預設為"true",代表該服務為enable.$conf["enable"]=$runOnBoot;#$conf["startNow"],字串,預設為"true",代表該服務要立刻執行.$conf["startNow"]=$runNow;#參考資料:#無.#備註:#無.$registerService=cmd::registerService($conf);unset($conf);#如果執行失敗if($registerService["status"]==="false"){#debugvar_dump($registerService);#結束執行,回傳 shell 1exit(1);}#if end