Subversion Repositories qbpwcf-lib(archive)

Rev

Rev 924 | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/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/>.
    
*/

#300986
#321099
#sed -n '3000,3999p' filename

#使用命名空間qbpwcf
namespace qbpwcf;

#取得 lib path
exec("php -f ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/libexec/folderOfUsrLib.php"),$output,$status);

#如果執行失敗
if($status!==0){

        #debug
        var_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){

        #debug
        var_dump(__LINE__,$output);

        #結束執行,回傳shell 1.
        exit(1);

        }#if end

#設置 include path 
set_include_path($output[0].PATH_SEPARATOR.get_include_path());

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

#如果不存在要轉換成16進位的數值
if( !isset($_SERVER['argv'][1]) )
{
        #show help
        help();

        #停止執行
        exit;
}

#如果第參數為 "help" 或 "-h"
if( $_SERVER['argv'][1]==="help" || $_SERVER['argv'][1]==="-h" )
{
        #show help
        help();

        #停止執行
        exit;

}#if end

#如果參數不齊全
if(!isset($_SERVER['argv'][3]))
{

        #show help
        help();

        #停止執行
        exit;

}#if 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["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
#$result["running"],是否還在執行.
#$result["pid"],pid.
#$result["statusCode"],執行結束後的代碼.
#必填的參數
#$conf["command"],字串,要執行的指令與.
$conf["command"]="sed";
#$conf["fileArgu"],字串,變數__FILE__的內容.
$conf["fileArgu"]=__FILE__;
#可省略參數:
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
$conf["argu"]=array("-n",$_SERVER['argv'][1].",".$_SERVER['argv'][2]."p",$_SERVER['argv'][3]);
#$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")
{
        #印出內容
        var_dump($callShell);
        
        #結束執行
        exit;
}

#針對每行輸出
foreach($callShell["output"] as $line)
{

        #印出內容
        echo $line.PHP_EOL;

}#foreach end

#說明
function help()
{
        #說明
        echo $_SERVER['argv'][0]." lineStartPoint lineEndPoint file".PHP_EOL;
}

?>