Subversion Repositories qbpwcf-lib(archive)

Rev

Rev 619 | Rev 906 | 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,Chen

    This file is part of QBPWCF.

    QBPWCF is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the 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 of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with QBPWCF.  If not, see <http://www.gnu.org/licenses/>.
    
*/

/*

說明:
終止指定的一個或多個程序,然後再啟動其程式.

*/

#指派命名空間
namespace qbpwcf;

#以該檔案的實際位置的 lib path 為 include path 首位
exec("cd ".pathinfo(__FILE__)["dirname"]."/../../;pwd;",$output,$status);
set_include_path($output[0].PATH_SEPARATOR.get_include_path());

#匯入外部套件
include("allInOne.php");

#函式說明:
#抓取命令列的參數.
#回傳結果:
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
#$reuslt["error"],執行不正常結束的錯訊息陣列.
#$result["function"],當前執行的函式名稱.
#$result["argu"],使用的參數陣列.
#$result["content"],要回傳的參數陣列.
#$result["count"],參數的數量.
#必填參數:
#無
#可省略參數:
#$conf["echo"],"true"代表要將抓到的參數一個個印出來,"false"代表用回傳的方式,預設為"false".
#$conf["echo"]="false";
$conf=array();
$getArgu=cmd::getArgu($conf);
unset($conf);

#如果執行失敗
if($getArgu["status"]==="false"){

        #設置執行失敗
        $result["status"]="false";

        #設置執行錯誤訊息
        $result["error"]=$getArgu;

        #印出訊息
        var_dump($result);

        #結束執行
        exit;

        }#if end

#如果沒參數
if($getArgu["count"]===1){

        #印出用法
        #函式說明:
        #印出多行文字,結尾自動換行.
        #回傳的結果:
        #$result["status"],執行是否成功,"true"代表成功,"false"代表失敗.
        #$result["function"],當前執行的函數名稱.
        #$result["error"],錯誤訊息陣列.
        #必填的參數:
        #$conf["outputStringArray"],字串陣列,每行要印出的文字內容.
        $conf["outputStringArray"]=array();
        $conf["outputStringArray"][]="-h/--help 顯示本說明";
        $conf["outputStringArray"][]="--program2kill 要結束的特定程式關鍵字,若有多個則代表有多個程序要結束";
        $conf["outputStringArray"][]="--program2run 要執行的程式位置與名稱";
        $echoMultiLine=cmd::echoMultiLine($conf);
        unset($conf);
        
        #結束執行
        exit;
        
        }

#第一個參數
$firsArgu=$getArgu["content"][1];

#如果程式後面的參數爲 -h 或 --help 參
if($firsArgu==="-h" || $firsArgu==="--help"){

        #印出用法
        #函式說明:
        #印出多行文字,結尾自動換行.
        #回傳的結果:
        #$result["status"],執行是否成功,"true"代表成功,"false"代表失敗.
        #$result["function"],當前執行的函數名稱.
        #$result["error"],錯誤訊息陣列.
        #必填的參數:
        #$conf["outputStringArray"],字串陣列,每行要印出的文字內容.
        $conf["outputStringArray"]=array();
        $conf["outputStringArray"][]="-h/--help 顯示本說明";
        $conf["outputStringArray"][]="--program2kill 要結束的程式關鍵字";
        $conf["outputStringArray"][]="--program2run 要執行的程式位置與名稱";
        $echoMultiLine=cmd::echoMultiLine($conf);
        unset($conf);
        
        #結束執行
        exit;
        
        }

#函式說明:
#解析參數.
#回傳結果:
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
#$reuslt["error"],執行不正常結束的錯訊息陣列.
#$result["function"],當前執行的函式名稱.
#$result["content"],解析好的參數陣列.
#$result["content"][$key][$i],參數 $key 的 $i+1 個參數數值內容.
#$result["program"],字串,執行的程式名稱.
#必填參數:
#無
#可省略參數:
#無
#備註:
#僅能在命令列底下執行.
#建議:
#以後可將參數 --a--b 的名稱與後面的數值 $value 存成 $result["a"]["b"][$i]=$value .
$conf=array();
$parseArgu=cmd::parseArgu($conf);
unset($conf);

#要終止的程式關鍵字
$programs2kill=$parseArgu["content"]["program2kill"];

#要執行的程式位置與名稱
$program2run=$parseArgu["content"]["program2run"][0];

#針對每個要中止程序的關鍵字
foreach($programs2kill as $program2kill){

        #透過 ps auxwf 檢查執行中的程式 pid
        #涵式說明:
        #呼叫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["command"]="ps";
        #$conf["fileArgu"],字串,變數__FILE__的內容.
        $conf["fileArgu"]=__FILE__;             
        #可省略參數:
        #$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
        $conf["argu"]=array("auxwf","|","grep",$program2kill);
        #$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".
        $conf["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";
        #備註:
        #不是所有指令都能用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);
        unset($conf);

        #如果執行失敗
        if($callShell["status"]==="false"){

                #設置執行失敗
                $result["status"]="false";

                #設置執行錯誤訊息
                $result["error"]=$getArgu;

                #印出訊息
                var_dump($result);

                #結束執行
                exit;
                
                }

        #取得輸出
        $output=$callShell["output"];

        #剔除有grep關鍵字的程式        
        #函式說明:
        #檢查多個字串中的每個字串是否有多個關鍵字
        #回傳的結果:
        #$result["status"],整體來說,執行是否成功,"true"代表執行成功,"false"代表執行失敗。
        #$result["function"],當前執行的函數名稱.
        #$result["error"],錯誤訊息.
        #$result["argu"],使用的參數.
        #$result["foundedTrueKey"],結果為"true"的被搜尋元素key陣列.
        #$result["foundedKeyWords"].找到的關鍵字.
        #$result["foundedFalseKey"],結果為"false"的被搜尋元素key陣列.
        #$result["foundedTrueKeyWords"],二維陣列,各個字串有找到的關鍵字陣列.
        #$result["foundedAll"],是否每個關鍵字都有找到,"true"代表每個都有找到,"false"代表沒有每個都找到.
        #必填的參數:
        $conf["keyWords"]=array("grep","rerunProgram.php");#想要搜尋的關鍵字
        $conf["stringArray"]=$output;#要被搜尋的字串內容陣列
        #可省略的參數:
        #$conf["completeEqual"]="true";#是否內容要完全符合,不能多出任何不符合的內容,預設為"false"不需要完全符合.
        $findManyKeyWordsFromManyString=search::findManyKeyWordsFromManyString($conf);
        unset($conf);

        #如果執行失敗
        if($findManyKeyWordsFromManyString["status"]==="false"){

                #設置執行失敗
                $result["status"]="false";

                #設置執行錯誤訊息
                $result["error"]=$findManyKeyWordsFromManyString;

                #印出訊息
                var_dump($result);

                #結束執行
                exit;

                }

        #如果沒有找到要移除的程序
        if(!isset($findManyKeyWordsFromManyString["foundedFalseKey"]))
        {
                #跳下一個
                continue;
        }

        #取得程序的輸出
        $pidsInfo=$findManyKeyWordsFromManyString["foundedFalseKey"];

        #有幾個程序就執行幾次
        foreach($pidsInfo as $pidInfo){

                #取得 pid
                #涵式說明:
                #將固定格式的字串分開,並回傳分開的結果。
                #回傳結果:
                #$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"]=$pidInfo;#要處理的字串。
                $conf["spiltSymbol"]=" ";#爲以哪個符號作爲分割
                #可省略參數:
                #$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
                $conf["allowEmptyStr"]="false";
                $spiltString=stringProcess::spiltString($conf);
                unset($conf);

                #如果執行失敗
                if($spiltString["status"]==="false"){
                
                        #設置執行失敗
                        $result["status"]="false";

                        #設置執行錯誤訊息
                        $result["error"]=$spiltString;

                        #印出訊息
                        var_dump($result);

                        #結束執行
                        exit;
                
                        }
                
                #取得程序的pid
                $pid=$spiltString["dataArray"][1];
                
                #涵式說明:
                #呼叫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["command"]="kill";
                #$conf["fileArgu"],字串,變數__FILE__的內容.
                $conf["fileArgu"]=__FILE__;             
                #可省略參數:
                #$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
                $conf["argu"]=array($pid);
                #$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".
                $conf["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";
                #備註:
                #不是所有指令都能用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);
                unset($conf);

                #如果執行失敗
                if($callShell["status"]==="false"){
                
                        #設置執行失敗
                        $result["status"]="false";

                        #設置執行錯誤訊息
                        $result["error"]=$getArgu;

                        #印出訊息
                        var_dump($result);

                        #結束執行
                        exit;
                        
                        }
                
                #印出執行的結果
                var_dump($callShell);

                }

        }#foreach end

#執行程式 $program2run
#涵式說明:
#呼叫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["command"]=$program2run;
#$conf["fileArgu"],字串,變數__FILE__的內容.
$conf["fileArgu"]=__FILE__;             
#可省略參數:
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
#$conf["argu"]=array($pid);
#$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".
$conf["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"]="true";
#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
#$conf["getErr"]="false";
#備註:
#不是所有指令都能用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);
unset($conf);

#如果執行失敗
if($callShell["status"]==="false"){

        #設置執行失敗
        $result["status"]="false";

        #設置執行錯誤訊息
        $result["error"]=$getArgu;

        #印出訊息
        var_dump($result);

        #結束執行
        exit;

        }#if end

#印出執行的結果
var_dump($callShell);

?>