| 3 |
liveuser |
1 |
#!/usr/bin/php
|
|
|
2 |
<?php
|
|
|
3 |
|
|
|
4 |
/*
|
|
|
5 |
QBPWCF, Quick Build PHP website Component base on Fedora Linux.
|
| 239 |
liveuser |
6 |
Copyright (C) 2014~2026 MIN ZHI, CHEN
|
| 3 |
liveuser |
7 |
|
|
|
8 |
This file is part of QBPWCF.
|
|
|
9 |
|
|
|
10 |
QBPWCF is free software: you can redistribute it and/or modify
|
|
|
11 |
it under the terms of the GNU General Public License as published by
|
|
|
12 |
the Free Software Foundation, either version 3 of the License, or
|
|
|
13 |
(at your option) any later version.
|
|
|
14 |
|
|
|
15 |
QBPWCF is distributed in the hope that it will be useful,
|
|
|
16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
18 |
GNU General Public License for more details.
|
|
|
19 |
|
|
|
20 |
You should have received a copy of the GNU General Public License
|
|
|
21 |
along with QBPWCF. If not, see <http://www.gnu.org/licenses/>.
|
| 226 |
liveuser |
22 |
|
| 3 |
liveuser |
23 |
*/
|
|
|
24 |
|
|
|
25 |
#300986
|
|
|
26 |
#321099
|
|
|
27 |
#sed -n '3000,3999p' filename
|
|
|
28 |
|
|
|
29 |
#使用命名空間qbpwcf
|
|
|
30 |
namespace qbpwcf;
|
|
|
31 |
|
|
|
32 |
#取得 lib path
|
|
|
33 |
exec("php -f ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/libexec/folderOfUsrLib.php"),$output,$status);
|
|
|
34 |
|
|
|
35 |
#如果執行失敗
|
|
|
36 |
if($status!==0){
|
|
|
37 |
|
|
|
38 |
#debug
|
|
|
39 |
var_dump(__LINE__,$output);
|
|
|
40 |
|
|
|
41 |
#結束執行,回傳shell 1.
|
|
|
42 |
exit(1);
|
|
|
43 |
|
|
|
44 |
}#if end
|
|
|
45 |
|
|
|
46 |
#儲存lib path
|
|
|
47 |
$folderOfUsrLib=$output[0];
|
|
|
48 |
|
|
|
49 |
#以該檔案的實際位置的 lib path 為 include path 首位
|
|
|
50 |
$output=array();
|
|
|
51 |
exec("cd ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/../".$folderOfUsrLib."/qbpwcf").";pwd;",$output,$status);
|
|
|
52 |
|
|
|
53 |
#如果執行失敗
|
|
|
54 |
if($status!==0){
|
|
|
55 |
|
|
|
56 |
#debug
|
|
|
57 |
var_dump(__LINE__,$output);
|
|
|
58 |
|
|
|
59 |
#結束執行,回傳shell 1.
|
|
|
60 |
exit(1);
|
|
|
61 |
|
|
|
62 |
}#if end
|
|
|
63 |
|
| 226 |
liveuser |
64 |
#設置 include path
|
| 3 |
liveuser |
65 |
set_include_path($output[0].PATH_SEPARATOR.get_include_path());
|
|
|
66 |
|
|
|
67 |
#匯入外部套件
|
|
|
68 |
include("allInOne.php");
|
|
|
69 |
|
|
|
70 |
#如果不存在要轉換成16進位的數值
|
| 168 |
liveuser |
71 |
if( !isset($_SERVER['argv'][1]) ){
|
|
|
72 |
|
| 3 |
liveuser |
73 |
#show help
|
|
|
74 |
help();
|
|
|
75 |
|
|
|
76 |
#停止執行
|
|
|
77 |
exit;
|
|
|
78 |
|
| 168 |
liveuser |
79 |
}#if end
|
|
|
80 |
|
| 3 |
liveuser |
81 |
#如果第參數為 "help" 或 "-h"
|
| 168 |
liveuser |
82 |
if( $_SERVER['argv'][1]==="help" || $_SERVER['argv'][1]==="-h" ){
|
|
|
83 |
|
| 3 |
liveuser |
84 |
#show help
|
|
|
85 |
help();
|
|
|
86 |
|
|
|
87 |
#停止執行
|
|
|
88 |
exit;
|
|
|
89 |
|
| 168 |
liveuser |
90 |
}#if end
|
| 3 |
liveuser |
91 |
|
|
|
92 |
#如果參數不齊全
|
| 168 |
liveuser |
93 |
if(!isset($_SERVER['argv'][3])){
|
| 3 |
liveuser |
94 |
|
|
|
95 |
#show help
|
|
|
96 |
help();
|
|
|
97 |
|
|
|
98 |
#停止執行
|
|
|
99 |
exit;
|
|
|
100 |
|
| 168 |
liveuser |
101 |
}#if end
|
| 3 |
liveuser |
102 |
|
|
|
103 |
#涵式說明:
|
|
|
104 |
#呼叫shell執行系統命令,並取得回傳的內容.
|
|
|
105 |
#回傳的結果:
|
|
|
106 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
107 |
#$result["error"],錯誤訊息陣列.
|
|
|
108 |
#$result["function"],當前執行的函數名稱.
|
|
|
109 |
#$result["argu"],使用的參數.
|
|
|
110 |
#$result["cmd"],執行的指令內容.
|
|
|
111 |
#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
112 |
#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
|
|
|
113 |
#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
114 |
#$result["running"],是否還在執行.
|
|
|
115 |
#$result["pid"],pid.
|
|
|
116 |
#$result["statusCode"],執行結束後的代碼.
|
|
|
117 |
#必填的參數
|
|
|
118 |
#$conf["command"],字串,要執行的指令與.
|
|
|
119 |
$conf["command"]="sed";
|
|
|
120 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
|
|
121 |
$conf["fileArgu"]=__FILE__;
|
|
|
122 |
#可省略參數:
|
|
|
123 |
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
|
|
|
124 |
$conf["argu"]=array("-n",$_SERVER['argv'][1].",".$_SERVER['argv'][2]."p",$_SERVER['argv'][3]);
|
|
|
125 |
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
|
|
|
126 |
#$conf["arguIsAddr"]=array();
|
|
|
127 |
#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
|
|
|
128 |
#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
|
|
|
129 |
#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
|
|
|
130 |
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
|
|
|
131 |
#$conf["enablePrintDescription"]="true";
|
|
|
132 |
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
|
|
|
133 |
#$conf["printDescription"]="";
|
|
|
134 |
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".
|
|
|
135 |
$conf["escapeshellarg"]="true";
|
|
|
136 |
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
|
|
|
137 |
#$conf["username"]="";
|
|
|
138 |
#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
|
|
139 |
#$conf["password"]="";
|
|
|
140 |
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
|
|
|
141 |
#$conf["useScript"]="";
|
|
|
142 |
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
|
|
|
143 |
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
|
|
|
144 |
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
|
|
|
145 |
#$conf["inBackGround"]="";
|
|
|
146 |
#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
|
|
|
147 |
#$conf["getErr"]="false";
|
|
|
148 |
#備註:
|
|
|
149 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
|
|
|
150 |
#參考資料:
|
|
|
151 |
#exec=>http://php.net/manual/en/function.exec.php
|
|
|
152 |
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
|
|
|
153 |
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
|
|
|
154 |
$callShell=external::callShell($conf);
|
|
|
155 |
unset($conf);
|
|
|
156 |
|
|
|
157 |
#如果執行失敗
|
| 168 |
liveuser |
158 |
if($callShell["status"]==="false"){
|
|
|
159 |
|
| 3 |
liveuser |
160 |
#印出內容
|
|
|
161 |
var_dump($callShell);
|
| 226 |
liveuser |
162 |
|
| 3 |
liveuser |
163 |
#結束執行
|
|
|
164 |
exit;
|
|
|
165 |
|
| 168 |
liveuser |
166 |
}#if end
|
|
|
167 |
|
| 3 |
liveuser |
168 |
#針對每行輸出
|
| 168 |
liveuser |
169 |
foreach($callShell["output"] as $line){
|
| 3 |
liveuser |
170 |
|
|
|
171 |
#印出內容
|
|
|
172 |
echo $line.PHP_EOL;
|
|
|
173 |
|
| 168 |
liveuser |
174 |
}#foreach end
|
| 3 |
liveuser |
175 |
|
|
|
176 |
#說明
|
| 168 |
liveuser |
177 |
function help(){
|
|
|
178 |
|
| 3 |
liveuser |
179 |
#說明
|
|
|
180 |
echo $_SERVER['argv'][0]." lineStartPoint lineEndPoint file".PHP_EOL;
|
|
|
181 |
|
| 168 |
liveuser |
182 |
}#function end
|
|
|
183 |
|
| 3 |
liveuser |
184 |
?>
|