| 5 |
liveuser |
1 |
#!/usr/bin/php
|
|
|
2 |
<?php
|
|
|
3 |
|
|
|
4 |
/*
|
|
|
5 |
QBPWCF, Quick Build PHP website Component base on Fedora Linux.
|
|
|
6 |
Copyright (C) 2015~2024 Min-Jhin,Chen
|
|
|
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/>.
|
|
|
22 |
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
/*
|
|
|
26 |
|
|
|
27 |
測試 usr/bin/ 底下的所有程式
|
|
|
28 |
|
|
|
29 |
*/
|
|
|
30 |
|
|
|
31 |
#使用 qbpwcf 命名空間
|
|
|
32 |
namespace qbpwcf;
|
|
|
33 |
|
|
|
34 |
#初始化輸出
|
|
|
35 |
$output=array();
|
|
|
36 |
|
|
|
37 |
#取得 lib path
|
| 97 |
liveuser |
38 |
exec("php -f ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/../../../../usr/bin/libexec/folderOfUsrLib.php"),$output,$status);
|
| 5 |
liveuser |
39 |
|
|
|
40 |
#如果執行失敗
|
|
|
41 |
if($status!==0){
|
|
|
42 |
|
|
|
43 |
#debug
|
|
|
44 |
var_dump(__LINE__,$output);
|
|
|
45 |
|
|
|
46 |
#結束執行,回傳shell 1.
|
|
|
47 |
exit(1);
|
|
|
48 |
|
|
|
49 |
}#if end
|
|
|
50 |
|
|
|
51 |
#儲存lib path
|
|
|
52 |
$folderOfUsrLib=$output[0];
|
|
|
53 |
|
|
|
54 |
#初始化輸出
|
|
|
55 |
$output=array();
|
|
|
56 |
|
|
|
57 |
#以該檔案的實際位置的 lib path 為 include path 首位
|
| 97 |
liveuser |
58 |
exec("cd ".pathinfo(__FILE__)["dirname"]."/../../../../usr/".$folderOfUsrLib."/qbpwcf;pwd;",$output,$status);
|
| 5 |
liveuser |
59 |
set_include_path($output[0].PATH_SEPARATOR.get_include_path());
|
|
|
60 |
|
|
|
61 |
#匯入套件
|
|
|
62 |
require_once("allInOne.php");
|
|
|
63 |
|
|
|
64 |
#說明函式
|
|
|
65 |
function help(){
|
|
|
66 |
|
|
|
67 |
#印出指令說明
|
|
|
68 |
echo "用於測試 usr/bin/ 底下程式的測試程式".PHP_EOL;
|
|
|
69 |
echo "Usage of ".basename(__FILE__).":".PHP_EOL;
|
|
|
70 |
echo "--list all/name of program under 'usr/bin/' 代表顯示所有可以執行的程式或顯示特定程式的用法.".PHP_EOL;
|
|
|
71 |
echo "--cmd name of program under 'usr/bin/' 代表要執行的程式名稱.".PHP_EOL;
|
|
|
72 |
echo "--paramName a param name of program under 'usr/bin/' 代表執行程式要用的參數名稱.".PHP_EOL;
|
|
|
73 |
echo "--paramValue a param value of program under 'usr/bin/' 代表執行程式要用的參數數值.".PHP_EOL;
|
|
|
74 |
echo PHP_EOL;
|
|
|
75 |
echo "使用 backupDb.php 的範例:".PHP_EOL;
|
|
|
76 |
echo "usr_bin_test.php --cmd backupDb.php --paramName --config --paramValue sample/assets\ of\ config/database.php --paramName --acctVarName --paramValue \"db['default']['username']\" --paramName --passVarName --paramValue \"db['default']['password']\" --paramName --dbVarName --paramValue \"db['default']['database']\" --paramName --dbAddrVarName --paramValue \"db['default']['hostname']\" --paramName --dbPortVarName --paramValue \"db['default']['port']\" --paramName --backupAddr --paramValue \"./\"".PHP_EOL;
|
|
|
77 |
echo PHP_EOL;
|
|
|
78 |
echo "使用 registerService.php 的範例:".PHP_EOL;
|
|
|
79 |
echo "./usr_bin_test.php --cmd registerService.php --paramName '--name' --paramValue ps --paramName '--cmd' --paramValue '#!/bin/bash' --paramName '--cmd' --paramValue 'while [ true ]' --paramName '--cmd' --paramValue 'do' --paramName '--cmd' --paramValue 'clear' --paramName '--cmd' --paramValue 'ps axuwf | cat' --paramName '--cmd' --paramValue 'sleep 1' --paramName '--cmd' --paramValue 'done'".PHP_EOL;
|
|
|
80 |
|
|
|
81 |
#一般錯誤結束執行
|
|
|
82 |
exit(1);
|
|
|
83 |
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
#函式說明:
|
|
|
87 |
#解析參數.
|
|
|
88 |
#回傳結果:
|
|
|
89 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
90 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
91 |
#$result["function"],當前執行的函式名稱.
|
|
|
92 |
#$result["content"],解析好的參數陣列.
|
|
|
93 |
#$result["content"][$key][$i],參數 $key 的 $i+1 個參數數值內容.
|
|
|
94 |
#$result["program"],字串,執行的程式名稱.
|
|
|
95 |
#必填參數:
|
|
|
96 |
#無
|
|
|
97 |
#可省略參數:
|
|
|
98 |
#$conf["helpFunc"],如果解析的參數不成對,則要執行的函式名稱.
|
|
|
99 |
$conf["helpFunc"]="help";
|
|
|
100 |
#備註:
|
|
|
101 |
#僅能在命令列底下執行.
|
|
|
102 |
#建議:
|
|
|
103 |
#以後可將參數 --a--b 的名稱與後面的數值 $value 存成 $result["a"]["b"][$i]=$value .
|
|
|
104 |
$parseArgu=cmd::parseArgu($conf);
|
|
|
105 |
unset($conf);
|
|
|
106 |
|
|
|
107 |
#如果解析參數失敗
|
|
|
108 |
if($parseArgu["status"]==="false"){
|
|
|
109 |
|
|
|
110 |
#印出結果
|
|
|
111 |
var_dump($parseArgu);
|
|
|
112 |
|
|
|
113 |
#一般錯誤結束執行
|
|
|
114 |
exit(1);
|
|
|
115 |
|
|
|
116 |
}#if end
|
|
|
117 |
|
|
|
118 |
#如果得到的參數為0
|
|
|
119 |
if(count($parseArgu["content"])===0){
|
|
|
120 |
|
|
|
121 |
#提示說明
|
|
|
122 |
help();
|
|
|
123 |
|
|
|
124 |
#正常結束執行
|
|
|
125 |
exit(0);
|
|
|
126 |
|
|
|
127 |
}#if end
|
|
|
128 |
|
|
|
129 |
#檢查參數
|
|
|
130 |
#函式說明:
|
|
|
131 |
#檢查必填與可省略參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
132 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
133 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
134 |
#$result["function"],當前執行的函式名稱.
|
|
|
135 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
136 |
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
137 |
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
138 |
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
139 |
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
|
|
|
140 |
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
|
|
|
141 |
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
|
|
|
142 |
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
|
|
|
143 |
#必填寫的參數:
|
|
|
144 |
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
145 |
$conf["varInput"]=&$parseArgu["content"];
|
|
|
146 |
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
147 |
$conf["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
148 |
#可以省略的參數:
|
|
|
149 |
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
150 |
#$conf["mustBeFilledVariableName"]=array("config","acctVarName","passVarName","dbVarName","dbAddrVarName","dbPortVarName","backupAddr");
|
|
|
151 |
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
|
|
|
152 |
#$conf["mustBeFilledVariableType"]=array("array","array","array","array","array","array","array");
|
|
|
153 |
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
|
|
|
154 |
#$conf["canBeEmptyString"]="false";
|
|
|
155 |
#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
|
|
|
156 |
#$conf["canNotBeEmpty"]=array();
|
|
|
157 |
#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
|
|
|
158 |
#$conf["canBeEmpty"]=array();
|
|
|
159 |
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
|
|
|
160 |
#$conf["skipableVariableCanNotBeEmpty"]=array("backTime");
|
|
|
161 |
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
|
|
162 |
$conf["skipableVariableName"]=array("list","cmd","paramName","paramValue");
|
|
|
163 |
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
|
|
164 |
$conf["skipableVariableType"]=array("array","array","array","array");
|
|
|
165 |
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
|
|
166 |
$conf["skipableVarDefaultValue"]=array(null,null,null,null);
|
|
|
167 |
#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
|
|
|
168 |
#$conf["variableCheck::checkArguments"]["disallowAllSkipableVarIsEmpty"]="false";
|
|
|
169 |
#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
|
|
|
170 |
#$conf["disallowAllSkipableVarIsEmptyArray"]="";
|
|
|
171 |
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
172 |
$conf["arrayCountEqualCheck"][]=array("paramName","paramValue");
|
|
|
173 |
#參考資料來源:
|
|
|
174 |
#array_keys=>http://php.net/manual/en/function.array-keys.php
|
|
|
175 |
$checkArguments=variableCheck::checkArguments($conf);
|
|
|
176 |
unset($conf);
|
|
|
177 |
|
|
|
178 |
#若執行失敗
|
|
|
179 |
if($checkArguments["status"]==="false"){
|
|
|
180 |
|
|
|
181 |
#印出結果
|
|
|
182 |
var_dump($checkArguments);
|
|
|
183 |
|
|
|
184 |
#一般錯誤結束執行
|
|
|
185 |
exit(1);
|
|
|
186 |
|
|
|
187 |
}#if end
|
|
|
188 |
|
|
|
189 |
#若檢查不通過
|
|
|
190 |
if($checkArguments["passed"]==="false"){
|
|
|
191 |
|
|
|
192 |
#印出結果
|
|
|
193 |
var_dump($checkArguments);
|
|
|
194 |
|
|
|
195 |
#一般錯誤結束執行
|
|
|
196 |
exit(1);
|
|
|
197 |
|
|
|
198 |
}#if end
|
|
|
199 |
|
|
|
200 |
#如果有 list 參數
|
|
|
201 |
if(isset($parseArgu["content"]["list"])){
|
|
|
202 |
|
|
|
203 |
#如果第一個 list 參數為 all
|
|
|
204 |
if($parseArgu["content"]["list"][0]==="all"){
|
|
|
205 |
|
|
|
206 |
#取得 usr/bin 底下的程式清單
|
|
|
207 |
#函式說明:
|
|
|
208 |
#開啟特定目錄,取得底下的檔案路徑清單.
|
|
|
209 |
#回傳結果:
|
|
|
210 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
211 |
#$result["error"],錯誤訊息.
|
|
|
212 |
#$result["function"],當前執行的函數名稱.
|
|
|
213 |
#$result["argu"],所使用的參數.
|
|
|
214 |
#$result["content"],讀取到的內容陣列.
|
|
|
215 |
#$result["content"][$i],第$i+1個結果.
|
|
|
216 |
#$result["content"][$i]["name"],第$i+1個名稱.
|
|
|
217 |
#$result["content"][$i]["dir"],第$i+1個檔案是否為資料夾.
|
|
|
218 |
#$result["content"][$i]["info"],第$i+1個檔案的額外資訊.
|
|
|
219 |
#$result["content"][$i]["size"],第$i+1個檔案的大小(bytes).
|
|
|
220 |
#必填參數:
|
|
|
221 |
#$conf["path"],字串,要取得檔案資訊的所屬路徑.
|
|
|
222 |
#$conf["path"]="/usr/lib/qbpwcf/usr/bin/";
|
|
|
223 |
$conf["path"]=$output[0]."/usr/bin/";
|
|
|
224 |
#可省略參數:
|
|
|
225 |
#無.
|
|
|
226 |
#參考資料
|
|
|
227 |
#無.
|
|
|
228 |
#備註:
|
|
|
229 |
#無.
|
|
|
230 |
$listInfo=fileAccess::listInfo($conf);
|
|
|
231 |
unset($conf);
|
|
|
232 |
|
|
|
233 |
#如果執行失敗
|
|
|
234 |
if($listInfo["status"]==="false"){
|
|
|
235 |
|
|
|
236 |
#印出結果
|
|
|
237 |
var_dump($listInfo);
|
|
|
238 |
|
|
|
239 |
#一般錯誤結束執行
|
|
|
240 |
exit(1);
|
|
|
241 |
|
|
|
242 |
}#if end
|
|
|
243 |
|
|
|
244 |
#針對每行輸出
|
|
|
245 |
foreach($listInfo["content"] as $line){
|
|
|
246 |
|
|
|
247 |
#如果是當前目錄或上層目錄
|
|
|
248 |
if($line["name"]==="." || $line["name"]===".."){
|
|
|
249 |
|
|
|
250 |
#換下一個
|
|
|
251 |
continue;
|
|
|
252 |
|
|
|
253 |
}#if end
|
|
|
254 |
|
|
|
255 |
#如果是資料夾
|
|
|
256 |
if($line["dir"]==="true"){
|
|
|
257 |
|
|
|
258 |
#換下一個
|
|
|
259 |
continue;
|
|
|
260 |
|
|
|
261 |
}#if end
|
|
|
262 |
|
|
|
263 |
#輸出結果
|
|
|
264 |
echo $line["name"].PHP_EOL;
|
|
|
265 |
|
|
|
266 |
}#foreach end
|
|
|
267 |
|
|
|
268 |
#正常結束執行
|
|
|
269 |
exit(0);
|
|
|
270 |
|
|
|
271 |
}#if end
|
|
|
272 |
|
|
|
273 |
#反之要 指定程式 的用法
|
|
|
274 |
else{
|
|
|
275 |
|
|
|
276 |
#函式說明:
|
|
|
277 |
#呼叫shell執行系統命令,並取得回傳的內容.
|
|
|
278 |
#回傳結果:
|
|
|
279 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
280 |
#$result["error"],錯誤訊息陣列.
|
|
|
281 |
#$result["function"],當前執行的函數名稱.
|
|
|
282 |
#$result["argu"],使用的參數.
|
|
|
283 |
#$result["cmd"],執行的指令內容.
|
|
|
284 |
#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
285 |
#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
|
|
|
286 |
#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
287 |
#$result["running"],是否還在執行.
|
|
|
288 |
#$result["pid"],pid.
|
|
|
289 |
#$result["statusCode"],執行結束後的代碼.
|
|
|
290 |
#必填參數:
|
|
|
291 |
#$conf["command"],字串,要執行的指令與.
|
|
|
292 |
$conf["command"]=$output[0]."/usr/bin/".$parseArgu["content"]["list"][0];
|
|
|
293 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
|
|
294 |
$conf["fileArgu"]=__FILE__;
|
|
|
295 |
#可省略參數:
|
|
|
296 |
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
|
|
|
297 |
$conf["argu"]=array("-h");
|
|
|
298 |
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
|
|
|
299 |
#$conf["arguIsAddr"]=array();
|
|
|
300 |
#$conf["plainArgu"],字串陣列,哪幾個參數不要加上"",若為"true"則代表不用包;反之"false"則代表要包.
|
|
|
301 |
#$conf["plainArgu"]=array();
|
|
|
302 |
#$conf["useApostrophe"],字串陣列,如果有需要包住,則用「'」,而非「"」處理.前者為"true";後者為"false".
|
|
|
303 |
#$conf["useApostrophe"]=array();
|
|
|
304 |
#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
|
|
|
305 |
#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
|
|
|
306 |
#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
|
|
|
307 |
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
|
|
|
308 |
#$conf["enablePrintDescription"]="true";
|
|
|
309 |
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
|
|
|
310 |
#$conf["printDescription"]="";
|
|
|
311 |
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
|
|
|
312 |
$conf["escapeshellarg"]="true";
|
|
|
313 |
#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
|
|
|
314 |
#$conf["thereIsShellVar"]=array();
|
|
|
315 |
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
|
|
|
316 |
#$conf["username"]="";
|
|
|
317 |
#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
|
|
318 |
#$conf["password"]="";
|
|
|
319 |
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
|
|
|
320 |
#$conf["useScript"]="";
|
|
|
321 |
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
|
|
|
322 |
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
|
|
|
323 |
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
|
|
|
324 |
#$conf["inBackGround"]="";
|
|
|
325 |
#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
|
|
|
326 |
#$conf["getErr"]="false";
|
|
|
327 |
#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
|
|
|
328 |
#$conf["doNotRun"]="false";
|
|
|
329 |
#參考資料:
|
|
|
330 |
#exec=>http://php.net/manual/en/function.exec.php
|
|
|
331 |
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
|
|
|
332 |
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
|
|
|
333 |
#備註:
|
|
|
334 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
|
|
|
335 |
$callShell=external::callShell($conf);
|
|
|
336 |
unset($conf);
|
|
|
337 |
|
|
|
338 |
#如果執行失敗
|
|
|
339 |
if($callShell["status"]==="false"){
|
|
|
340 |
|
|
|
341 |
#印出結果
|
|
|
342 |
var_dump($callShell);
|
|
|
343 |
|
|
|
344 |
#一般錯誤結束執行
|
|
|
345 |
exit(1);
|
|
|
346 |
|
|
|
347 |
}#if end
|
|
|
348 |
|
|
|
349 |
#針對每行輸出
|
|
|
350 |
foreach($callShell["output"] as $line){
|
|
|
351 |
|
|
|
352 |
#輸出結果
|
|
|
353 |
echo $line.PHP_EOL;
|
|
|
354 |
|
|
|
355 |
}#foreach end
|
|
|
356 |
|
|
|
357 |
#正常結束執行
|
|
|
358 |
exit(0);
|
|
|
359 |
|
|
|
360 |
}#else end
|
|
|
361 |
|
|
|
362 |
}#if end
|
|
|
363 |
|
|
|
364 |
#如果有 cmd 參數
|
|
|
365 |
if(isset($parseArgu["content"]["cmd"])){
|
|
|
366 |
|
|
|
367 |
#取得第一個 cmd 參數
|
|
|
368 |
$cmd=$parseArgu["content"]["cmd"][0];
|
|
|
369 |
|
|
|
370 |
#初始化參數
|
|
|
371 |
$params=array();
|
|
|
372 |
|
|
|
373 |
#如果有 paramName 參數
|
|
|
374 |
if(isset($parseArgu["content"]["paramName"])){
|
|
|
375 |
|
|
|
376 |
#針對每個參數名稱
|
|
|
377 |
for($i=0;$i<count($parseArgu["content"]["paramName"]);$i++){
|
|
|
378 |
|
|
|
379 |
#取得第 params 名稱
|
|
|
380 |
$params[]=$parseArgu["content"]["paramName"][$i];
|
|
|
381 |
|
|
|
382 |
#取得第 params 數值
|
|
|
383 |
$params[]=$parseArgu["content"]["paramValue"][$i];
|
|
|
384 |
|
|
|
385 |
}#if end
|
|
|
386 |
|
|
|
387 |
}#if end
|
|
|
388 |
|
|
|
389 |
#函式說明:
|
|
|
390 |
#呼叫shell執行系統命令,並取得回傳的內容.
|
|
|
391 |
#回傳結果:
|
|
|
392 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
393 |
#$result["error"],錯誤訊息陣列.
|
|
|
394 |
#$result["function"],當前執行的函數名稱.
|
|
|
395 |
#$result["argu"],使用的參數.
|
|
|
396 |
#$result["cmd"],執行的指令內容.
|
|
|
397 |
#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
398 |
#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
|
|
|
399 |
#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
400 |
#$result["running"],是否還在執行.
|
|
|
401 |
#$result["pid"],pid.
|
|
|
402 |
#$result["statusCode"],執行結束後的代碼.
|
|
|
403 |
#必填參數:
|
|
|
404 |
#$conf["command"],字串,要執行的指令與.
|
|
|
405 |
$conf["command"]=$output[0]."/usr/bin/".$cmd;
|
|
|
406 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
|
|
407 |
$conf["fileArgu"]=__FILE__;
|
|
|
408 |
#可省略參數:
|
|
|
409 |
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
|
|
|
410 |
$conf["argu"]=$params;
|
|
|
411 |
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
|
|
|
412 |
#$conf["arguIsAddr"]=array();
|
|
|
413 |
#$conf["plainArgu"],字串陣列,哪幾個參數不要加上"",若為"true"則代表不用包;反之"false"則代表要包.
|
|
|
414 |
#$conf["plainArgu"]=array();
|
|
|
415 |
#$conf["useApostrophe"],字串陣列,如果有需要包住,則用「'」,而非「"」處理.前者為"true";後者為"false".
|
|
|
416 |
#$conf["useApostrophe"]=array();
|
|
|
417 |
#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
|
|
|
418 |
#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
|
|
|
419 |
#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
|
|
|
420 |
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
|
|
|
421 |
#$conf["enablePrintDescription"]="true";
|
|
|
422 |
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
|
|
|
423 |
#$conf["printDescription"]="";
|
|
|
424 |
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
|
|
|
425 |
$conf["escapeshellarg"]="true";
|
|
|
426 |
#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
|
|
|
427 |
#$conf["thereIsShellVar"]=array();
|
|
|
428 |
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
|
|
|
429 |
#$conf["username"]="";
|
|
|
430 |
#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
|
|
431 |
#$conf["password"]="";
|
|
|
432 |
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
|
|
|
433 |
#$conf["useScript"]="";
|
|
|
434 |
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
|
|
|
435 |
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
|
|
|
436 |
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
|
|
|
437 |
#$conf["inBackGround"]="";
|
|
|
438 |
#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
|
|
|
439 |
#$conf["getErr"]="false";
|
|
|
440 |
#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
|
|
|
441 |
#$conf["doNotRun"]="false";
|
|
|
442 |
#參考資料:
|
|
|
443 |
#exec=>http://php.net/manual/en/function.exec.php
|
|
|
444 |
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
|
|
|
445 |
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
|
|
|
446 |
#備註:
|
|
|
447 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
|
|
|
448 |
$callShell=external::callShell($conf);
|
|
|
449 |
unset($conf);
|
|
|
450 |
|
|
|
451 |
#如果執行失敗
|
|
|
452 |
if($callShell["status"]==="false"){
|
|
|
453 |
|
|
|
454 |
#印出結果
|
|
|
455 |
var_dump($callShell);
|
|
|
456 |
|
|
|
457 |
#一般錯誤結束執行
|
|
|
458 |
exit(1);
|
|
|
459 |
|
|
|
460 |
}#if end
|
|
|
461 |
|
|
|
462 |
#針對每行輸出
|
|
|
463 |
foreach($callShell["output"] as $line){
|
|
|
464 |
|
|
|
465 |
#輸出結果
|
|
|
466 |
echo $line.PHP_EOL;
|
|
|
467 |
|
|
|
468 |
}#foreach end
|
|
|
469 |
|
|
|
470 |
#正常結束執行
|
|
|
471 |
exit(0);
|
|
|
472 |
|
|
|
473 |
}#if end
|
|
|
474 |
|
|
|
475 |
?>
|