| 3 |
liveuser |
1 |
<?php
|
|
|
2 |
|
|
|
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/>.
|
|
|
22 |
|
|
|
23 |
*/
|
|
|
24 |
namespace qbpwcf;
|
|
|
25 |
|
|
|
26 |
/*
|
|
|
27 |
類別說明:
|
|
|
28 |
執行外部程式的類別.
|
|
|
29 |
備註:
|
|
|
30 |
無.
|
|
|
31 |
*/
|
|
|
32 |
class external{
|
|
|
33 |
|
|
|
34 |
/*
|
|
|
35 |
#函式說明:
|
|
|
36 |
#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
|
|
|
37 |
#回傳結果:
|
|
|
38 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
39 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
40 |
#$result["function"],當前執行的函式名稱.
|
|
|
41 |
#必填參數:
|
|
|
42 |
#$method,物件,為物件實體或類別名稱,會自動置入該參數.
|
|
|
43 |
#$arguments,陣列,為呼叫方法時所用的參數.
|
|
|
44 |
#可省略參數:
|
|
|
45 |
#無.
|
|
|
46 |
#參考資料:
|
|
|
47 |
#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
|
|
|
48 |
*/
|
|
|
49 |
public function __call($method,$arguments){
|
| 226 |
liveuser |
50 |
|
| 3 |
liveuser |
51 |
#取得當前執行的函式
|
|
|
52 |
$result["function"]=__FUNCTION__;
|
| 226 |
liveuser |
53 |
|
| 3 |
liveuser |
54 |
#設置執行不正常
|
|
|
55 |
$result["status"]="false";
|
| 226 |
liveuser |
56 |
|
| 3 |
liveuser |
57 |
#設置執行錯誤
|
|
|
58 |
$result["error"][]=__NAMESPACE__ ."/".$method."() 不存在!";
|
| 226 |
liveuser |
59 |
|
| 3 |
liveuser |
60 |
#設置所丟入的參數
|
|
|
61 |
$result["error"][]=$arguments;
|
| 226 |
liveuser |
62 |
|
| 3 |
liveuser |
63 |
#回傳結果
|
|
|
64 |
return $result;
|
| 226 |
liveuser |
65 |
|
| 3 |
liveuser |
66 |
}#function __call end
|
| 226 |
liveuser |
67 |
|
| 3 |
liveuser |
68 |
/*
|
|
|
69 |
#函式說明:
|
|
|
70 |
#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
|
|
|
71 |
#回傳結果:
|
|
|
72 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
73 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
74 |
#$result["function"],當前執行的函式名稱.
|
|
|
75 |
#必填參數:
|
|
|
76 |
#$method,物件,為物件實體或類別名稱,會自動置入該參數.
|
|
|
77 |
#$arguments,陣列,為呼叫方法時所用的參數.
|
|
|
78 |
#可省略參數:
|
|
|
79 |
#無.
|
|
|
80 |
#參考資料:
|
|
|
81 |
#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
|
|
|
82 |
*/
|
|
|
83 |
public static function __callStatic($method,$arguments){
|
| 226 |
liveuser |
84 |
|
| 3 |
liveuser |
85 |
#取得當前執行的函式
|
|
|
86 |
$result["function"]=__FUNCTION__;
|
| 226 |
liveuser |
87 |
|
| 3 |
liveuser |
88 |
#設置執行不正常
|
|
|
89 |
$result["status"]="false";
|
| 226 |
liveuser |
90 |
|
| 3 |
liveuser |
91 |
#設置執行錯誤
|
|
|
92 |
$result["error"][]="欲呼叫的". __NAMESPACE__ ."/".$method."() 不存在!";
|
| 226 |
liveuser |
93 |
|
| 3 |
liveuser |
94 |
#設置所丟入的參數
|
|
|
95 |
$result["error"][]=$arguments;
|
| 226 |
liveuser |
96 |
|
| 3 |
liveuser |
97 |
#回傳結果
|
|
|
98 |
return $result;
|
| 226 |
liveuser |
99 |
|
| 3 |
liveuser |
100 |
}#function __callStatic end
|
| 226 |
liveuser |
101 |
|
| 3 |
liveuser |
102 |
/*
|
|
|
103 |
#函式說明:
|
| 226 |
liveuser |
104 |
#測試php呼叫java後回傳的結果
|
| 3 |
liveuser |
105 |
#回傳結果:
|
|
|
106 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
107 |
#$result["function"],當前執行的函式名稱.
|
|
|
108 |
#$result["error"],錯誤訊息陣列
|
|
|
109 |
#$result["cmd"],執行的指令內容.
|
|
|
110 |
#$result["output"],爲執行完java class檔案後的輸出陣列
|
|
|
111 |
#$result["outputFromFile"]["fileContent"],取得輸出的內容陣列.
|
|
|
112 |
#$result["outputFromFile"]["lineCount"],取得輸出內容總共的行數.
|
|
|
113 |
#$result["outputFromFile"]["fullContent"],取得輸出的內容陣列.
|
|
|
114 |
#必填參數:
|
|
|
115 |
$conf["javaClassFileAddress"]="";#java class檔案的位置與名稱,附檔名(.class)不用附上。
|
|
|
116 |
#可省略參數:
|
|
|
117 |
#$conf["arguments"]="";#要給java class檔案的參數
|
|
|
118 |
#參考資料:
|
|
|
119 |
#http://php.net/manual/en/function.exec.php
|
|
|
120 |
#備註:
|
|
|
121 |
#目前無法透過apache的身份執行java程式.
|
|
|
122 |
*/
|
|
|
123 |
public static function callJava($conf){
|
|
|
124 |
|
|
|
125 |
#初始化要回傳的內容
|
|
|
126 |
$result=array();
|
|
|
127 |
|
|
|
128 |
#取得當前執行的函式
|
|
|
129 |
$result["function"]=__FUNCTION__;
|
|
|
130 |
|
|
|
131 |
#如果 $conf 不為陣列
|
|
|
132 |
if(gettype($conf)!="array"){
|
| 226 |
liveuser |
133 |
|
| 3 |
liveuser |
134 |
#設置執行失敗
|
|
|
135 |
$result["status"]="false";
|
| 226 |
liveuser |
136 |
|
| 3 |
liveuser |
137 |
#設置執行錯誤訊息
|
|
|
138 |
$result["error"][]="\$conf變數須為陣列形態";
|
|
|
139 |
|
|
|
140 |
#回傳結果
|
|
|
141 |
return $result;
|
| 226 |
liveuser |
142 |
|
| 3 |
liveuser |
143 |
}#if end
|
|
|
144 |
|
|
|
145 |
#如果$conf["javaClassFileAddress"]沒有設定
|
|
|
146 |
if(!isset($conf["javaClassFileAddress"])){
|
|
|
147 |
|
|
|
148 |
#設置執行錯誤
|
|
|
149 |
$result["status"]="false";
|
|
|
150 |
|
|
|
151 |
#設置錯誤訊息
|
|
|
152 |
$result["error"][]="\$conf[\"javaClassFileAddress\"]參數不存在!";
|
|
|
153 |
|
|
|
154 |
#回傳結果
|
|
|
155 |
return $result;
|
|
|
156 |
|
|
|
157 |
}#if end
|
|
|
158 |
|
|
|
159 |
#如果$conf["arguments"]沒有設置
|
|
|
160 |
if(!isset($conf["arguments"])){
|
| 226 |
liveuser |
161 |
|
| 3 |
liveuser |
162 |
#設爲""
|
|
|
163 |
$conf["arguments"]="";
|
|
|
164 |
|
|
|
165 |
}#if end
|
|
|
166 |
|
|
|
167 |
#執行java程式
|
|
|
168 |
#函式說明:
|
|
|
169 |
#呼叫shell執行系統命令,並取得回傳的內容.
|
|
|
170 |
#回傳結果:
|
|
|
171 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
172 |
#$result["error"],錯誤訊息陣列
|
|
|
173 |
#$result["function"],當前執行的函數名稱
|
|
|
174 |
#$result["cmd"],執行的指令內容.
|
|
|
175 |
#$result["outputFromFile"]["fileContent"],取得輸出的內容陣列.
|
|
|
176 |
#$result["outputFromFile"]["lineCount"],取得輸出內容總共的行數.
|
|
|
177 |
#$result["outputFromFile"]["fullContent"],取得輸出的內容陣列.
|
|
|
178 |
#必填參數:
|
|
|
179 |
#$conf["external::callShell"]["command"],字串.
|
|
|
180 |
$conf["external::callShell"]["command"]="java";#要執行的指令與參數.
|
|
|
181 |
#可省略參數:
|
|
|
182 |
#$conf["external::callShell"]["argu"],陣列字串,指令搭配的參數,預設為空陣列.
|
|
|
183 |
$conf["external::callShell"]["argu"]=array($conf["javaClassFileAddress"],$conf["arguments"]);
|
|
|
184 |
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
|
|
|
185 |
#$conf["enablePrintDescription"]="true";
|
|
|
186 |
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容.
|
|
|
187 |
#$conf["printDescription"]="";
|
|
|
188 |
$conf["external::callShell"]["escapeshellarg"]="true";
|
|
|
189 |
#參考資料:
|
|
|
190 |
#http://php.net/manual/en/function.exec.php
|
|
|
191 |
$callShell=external::callShell($conf["external::callShell"]);
|
|
|
192 |
unset($conf["external::callShell"]);
|
|
|
193 |
|
|
|
194 |
#debug
|
|
|
195 |
#var_dump($callShell);
|
|
|
196 |
#exit;
|
|
|
197 |
|
|
|
198 |
#若執行外部 java class 程式回傳的狀態為 "false"
|
|
|
199 |
if($callShell["status"]=="false"){
|
| 226 |
liveuser |
200 |
|
| 3 |
liveuser |
201 |
#代表執行外部程式不正常.
|
| 226 |
liveuser |
202 |
|
| 3 |
liveuser |
203 |
#設置錯誤識別
|
|
|
204 |
$result["status"]="false";
|
| 226 |
liveuser |
205 |
|
| 3 |
liveuser |
206 |
#設置錯誤訊息
|
|
|
207 |
$result["error"]=$callShell;
|
| 226 |
liveuser |
208 |
|
| 3 |
liveuser |
209 |
#回傳結果
|
|
|
210 |
return $result;
|
| 226 |
liveuser |
211 |
|
| 3 |
liveuser |
212 |
}#if end
|
| 226 |
liveuser |
213 |
|
| 3 |
liveuser |
214 |
#取得輸出的內容陣列.
|
|
|
215 |
$result["outputFromFile"]["fileContent"]=$callShell["outputFromFile"]["fileContent"];
|
| 226 |
liveuser |
216 |
|
| 3 |
liveuser |
217 |
#取得輸出內容總共的行數.
|
|
|
218 |
$result["outputFromFile"]["lineCount"]=$callShell["outputFromFile"]["lineCount"];
|
| 226 |
liveuser |
219 |
|
| 3 |
liveuser |
220 |
#取得輸出的內容陣列.
|
|
|
221 |
$result["outputFromFile"]["fullContent"]=$callShell["outputFromFile"]["fullContent"];
|
| 226 |
liveuser |
222 |
|
| 3 |
liveuser |
223 |
#取得執行的指令內容
|
| 226 |
liveuser |
224 |
$result["cmd"]=$callShell["cmd"];
|
|
|
225 |
|
| 3 |
liveuser |
226 |
#執行到這邊代表執行正常
|
|
|
227 |
$result["status"]="true";
|
|
|
228 |
|
|
|
229 |
#取得輸出的內容
|
|
|
230 |
$result["output"]=$callShell["output"];
|
|
|
231 |
|
|
|
232 |
#回傳結果
|
|
|
233 |
return $result;
|
|
|
234 |
|
|
|
235 |
}#function testPheCallJava end
|
|
|
236 |
|
|
|
237 |
/*
|
|
|
238 |
#函式說明:
|
|
|
239 |
#呼叫shell執行編譯過的2元碼程式或具備執行權限的檔案後回傳的結果
|
|
|
240 |
#回傳結果:
|
|
|
241 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
242 |
#$result["function"],正在執行的函式
|
|
|
243 |
#$result["error"],錯誤訊息陣列.
|
|
|
244 |
#$result["error"]["returnCode"],錯誤代碼.
|
|
|
245 |
#$result["output"],爲執行完二元碼後的輸出陣列.
|
|
|
246 |
#$result["escapedCmd"],實際執行的指令.
|
|
|
247 |
#必填參數:
|
|
|
248 |
$conf["exeFileAddress"]="";#可執行檔的位置與名稱
|
|
|
249 |
#可省略參數:
|
|
|
250 |
#$conf["arguments"]="";#要給執行檔的參數
|
|
|
251 |
#參考資料:
|
|
|
252 |
#http://php.net/manual/en/function.exec.php
|
|
|
253 |
#備註:
|
|
|
254 |
#無.
|
| 226 |
liveuser |
255 |
*/
|
| 3 |
liveuser |
256 |
public static function execByteCode($conf){
|
|
|
257 |
|
|
|
258 |
#初始化要回傳的結果
|
|
|
259 |
$result=array();
|
| 226 |
liveuser |
260 |
|
| 3 |
liveuser |
261 |
#取得當前執行的函式
|
|
|
262 |
$result["function"]=__FUNCTION__;
|
| 226 |
liveuser |
263 |
|
| 3 |
liveuser |
264 |
#如果 $conf 不為陣列
|
|
|
265 |
if(gettype($conf)!="array"){
|
| 226 |
liveuser |
266 |
|
| 3 |
liveuser |
267 |
#設置執行失敗
|
|
|
268 |
$result["status"]="false";
|
| 226 |
liveuser |
269 |
|
| 3 |
liveuser |
270 |
#設置執行錯誤訊息
|
|
|
271 |
$result["error"][]="\$conf變數須為陣列形態";
|
|
|
272 |
|
|
|
273 |
#如果傳入的參數為 null
|
|
|
274 |
if($conf==null){
|
| 226 |
liveuser |
275 |
|
| 3 |
liveuser |
276 |
#設置執行錯誤訊息
|
|
|
277 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
| 226 |
liveuser |
278 |
|
| 3 |
liveuser |
279 |
}#if end
|
|
|
280 |
|
|
|
281 |
#回傳結果
|
|
|
282 |
return $result;
|
| 226 |
liveuser |
283 |
|
| 3 |
liveuser |
284 |
}#if end
|
| 226 |
liveuser |
285 |
|
| 3 |
liveuser |
286 |
#如果$conf["exeFileAddress"]沒有設定
|
|
|
287 |
if(!isset($conf["exeFileAddress"])){
|
|
|
288 |
|
|
|
289 |
#設置執行錯誤識別
|
|
|
290 |
$result["status"];
|
|
|
291 |
|
|
|
292 |
#設置錯誤訊息
|
|
|
293 |
$result["error"][]="\$conf[\"exeFileAddress\"]參數不存在!";
|
|
|
294 |
|
|
|
295 |
#回傳結果
|
|
|
296 |
return $result;
|
|
|
297 |
|
|
|
298 |
}#if end
|
| 226 |
liveuser |
299 |
|
| 3 |
liveuser |
300 |
#初始化要使用的參數
|
|
|
301 |
$escapedShellArg="";
|
|
|
302 |
|
|
|
303 |
#如果$conf["arguments"]沒有設置
|
|
|
304 |
if(isset($conf["arguments"])){
|
| 226 |
liveuser |
305 |
|
| 3 |
liveuser |
306 |
#取得已經處理好的參數
|
|
|
307 |
$escapedShellArg=" ".escapeshellarg($conf["arguments"]);
|
|
|
308 |
|
|
|
309 |
}#if end
|
|
|
310 |
|
|
|
311 |
#初始化執行的語法
|
|
|
312 |
$runStr=$conf["exeFileAddress"];
|
|
|
313 |
|
|
|
314 |
#如果沒有路徑的符號
|
|
|
315 |
if(strpos($runStr,"/")===false){
|
| 226 |
liveuser |
316 |
|
| 3 |
liveuser |
317 |
#代表要執行的程式在當前路徑
|
|
|
318 |
$runStr="./".$runStr;
|
| 226 |
liveuser |
319 |
|
| 3 |
liveuser |
320 |
}#if end
|
| 226 |
liveuser |
321 |
|
| 3 |
liveuser |
322 |
#處理執行的語法
|
|
|
323 |
$runStr=escapeshellarg($runStr);
|
|
|
324 |
|
|
|
325 |
#取得處理好的指令
|
|
|
326 |
$escapeShellCmd=$runStr.$escapedShellArg;
|
|
|
327 |
|
|
|
328 |
#儲存實際執行的指令
|
|
|
329 |
$result["escapedCmd"]=$escapeShellCmd;
|
|
|
330 |
|
|
|
331 |
#執行腳本程式,將輸出結果儲存在 $resultArray 陣列裏面
|
|
|
332 |
exec($escapeShellCmd,$resultArray,$execStatus);
|
|
|
333 |
|
|
|
334 |
#取得執行外部程式的輸出
|
|
|
335 |
$result["output"]=$resultArray;
|
|
|
336 |
|
|
|
337 |
#若執行外部程式回傳的狀態不為 0
|
|
|
338 |
if($execStatus!=0){
|
| 226 |
liveuser |
339 |
|
| 3 |
liveuser |
340 |
#代表執行外部程式不正常.
|
| 226 |
liveuser |
341 |
|
| 3 |
liveuser |
342 |
#設置錯誤識別
|
|
|
343 |
$result["status"]="false";
|
| 226 |
liveuser |
344 |
|
| 3 |
liveuser |
345 |
#設置錯誤代碼
|
|
|
346 |
$result["error"]["returnCode"]=$execStatus;
|
| 226 |
liveuser |
347 |
|
| 3 |
liveuser |
348 |
#設置錯誤訊息
|
|
|
349 |
$result["error"][]="執行".$conf["exeFileAddress"]."意外結束!";
|
| 226 |
liveuser |
350 |
|
| 3 |
liveuser |
351 |
#回傳結果
|
|
|
352 |
return $result;
|
| 226 |
liveuser |
353 |
|
| 3 |
liveuser |
354 |
}#if end
|
| 226 |
liveuser |
355 |
|
| 3 |
liveuser |
356 |
#執行到這邊代表執行正常
|
|
|
357 |
$result["status"]="true";
|
|
|
358 |
|
|
|
359 |
#回傳結果
|
|
|
360 |
return $result;
|
|
|
361 |
|
|
|
362 |
}#function execByteCode end
|
| 226 |
liveuser |
363 |
|
| 3 |
liveuser |
364 |
/*
|
|
|
365 |
#函式說明:
|
|
|
366 |
#解析指令與參數,回傳指令與參數給 callShell 函式使用.
|
|
|
367 |
#回傳結果:
|
|
|
368 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
369 |
#$result["error"],錯誤訊息陣列.
|
|
|
370 |
#$result["function"],當前執行的函數名稱.
|
|
|
371 |
#$result["argu"],使用的參數.
|
|
|
372 |
#$result["cmd"],執行的指令名稱.
|
|
|
373 |
#$result["params"],執行指令伴隨的參數.
|
|
|
374 |
#必填參數:
|
|
|
375 |
#$conf["cmdArray"],字串陣列,要執行的指令字串.
|
|
|
376 |
$conf["cmdArray"]=array();
|
|
|
377 |
#可省略參數:
|
|
|
378 |
#無.
|
|
|
379 |
#參考資料:
|
|
|
380 |
#array_shift=>https://www.php.net/manual/en/function.array-shift.php
|
|
|
381 |
#備註:
|
|
|
382 |
#無.
|
|
|
383 |
*/
|
|
|
384 |
public static function callShellHelper(&$conf){
|
| 226 |
liveuser |
385 |
|
| 3 |
liveuser |
386 |
#初始化要回傳的結果
|
|
|
387 |
$result=array();
|
| 226 |
liveuser |
388 |
|
| 3 |
liveuser |
389 |
#取得當前執行的函數名稱
|
|
|
390 |
$result["function"]=__FUNCTION__;
|
| 226 |
liveuser |
391 |
|
| 3 |
liveuser |
392 |
#如果 $conf 不為陣列
|
|
|
393 |
if(gettype($conf)!="array"){
|
| 226 |
liveuser |
394 |
|
| 3 |
liveuser |
395 |
#設置執行失敗
|
|
|
396 |
$result["status"]="false";
|
| 226 |
liveuser |
397 |
|
| 3 |
liveuser |
398 |
#設置執行錯誤訊息
|
|
|
399 |
$result["error"][]="\$conf變數須為陣列形態";
|
|
|
400 |
|
|
|
401 |
#如果傳入的參數為 null
|
|
|
402 |
if($conf==null){
|
| 226 |
liveuser |
403 |
|
| 3 |
liveuser |
404 |
#設置執行錯誤訊息
|
|
|
405 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
| 226 |
liveuser |
406 |
|
| 3 |
liveuser |
407 |
}#if end
|
|
|
408 |
|
|
|
409 |
#回傳結果
|
|
|
410 |
return $result;
|
| 226 |
liveuser |
411 |
|
| 3 |
liveuser |
412 |
}#if end
|
| 226 |
liveuser |
413 |
|
| 3 |
liveuser |
414 |
#取得參數
|
|
|
415 |
$result["argu"]=$conf;
|
| 226 |
liveuser |
416 |
|
| 3 |
liveuser |
417 |
#參數檢查
|
|
|
418 |
#函式說明:
|
|
|
419 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
420 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
421 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
422 |
#$result["function"],當前執行的函式名稱.
|
|
|
423 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
424 |
#$result[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
425 |
#$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
426 |
#$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
427 |
#必填寫的參數:
|
|
|
428 |
#$conf["variableCheck::checkArguments"]["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
429 |
$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
|
|
|
430 |
#$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
431 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("cmdArray");
|
| 226 |
liveuser |
432 |
#$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
433 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("array");
|
|
|
434 |
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
435 |
$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
436 |
#可以省略的參數:
|
|
|
437 |
#$conf["variableCheck::checkArguments"]["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true"。
|
|
|
438 |
$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
|
|
|
439 |
#$conf["variableCheck::checkArguments"]["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
|
|
440 |
#$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("pre","enablePrintDescription","printDescription","argu","escapeshellarg","username","password","useScript","logFilePath","inBackGround","arguIsAddr","getErr","plainArgu","useApostrophe","doNotRun","thereIsShellVar");
|
|
|
441 |
#$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("pre","enablePrintDescription","printDescription","argu","escapeshellarg","username","password","useScript","logFilePath","inBackGround","arguIsAddr","getErr","doNotRun","thereIsShellVar");
|
| 226 |
liveuser |
442 |
#$conf["variableCheck::checkArguments"]["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
443 |
#$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("array","string","string","array","string","string","string","string","string","string","array","string","array","array","string","array");
|
|
|
444 |
#$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("array","string","string","array","string","string","string","string","string","string","array","string","string","array");
|
|
|
445 |
#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,"null"代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
|
|
446 |
#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(null,"false",null,null,"false",null,null,"false",".qbpwcf_tmp/external/callShell/","false",null,"false",null,null,"false",null);
|
|
|
447 |
#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(null,"false",null,null,"false",null,null,"false",".qbpwcf_tmp/external/callShell/","false",null,"false","false",null);
|
|
|
448 |
#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
449 |
#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"][]=array("argu","arguIsAddr");
|
|
|
450 |
$checkResult=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
|
|
|
451 |
unset($conf["variableCheck::checkArguments"]);
|
| 226 |
liveuser |
452 |
|
| 3 |
liveuser |
453 |
#如果檢查參數失敗
|
|
|
454 |
if($checkResult["status"]=="false"){
|
| 226 |
liveuser |
455 |
|
| 3 |
liveuser |
456 |
#設置執行失敗
|
|
|
457 |
$result["status"]="false";
|
| 226 |
liveuser |
458 |
|
| 3 |
liveuser |
459 |
#設置執行錯誤
|
|
|
460 |
$result["error"]=$checkResult;
|
| 226 |
liveuser |
461 |
|
| 3 |
liveuser |
462 |
#回傳結果
|
|
|
463 |
return $result;
|
| 226 |
liveuser |
464 |
|
| 3 |
liveuser |
465 |
}#if end
|
| 226 |
liveuser |
466 |
|
| 3 |
liveuser |
467 |
#如果檢查參數不通過
|
|
|
468 |
if($checkResult["passed"]=="false"){
|
| 226 |
liveuser |
469 |
|
| 3 |
liveuser |
470 |
#設置執行失敗
|
|
|
471 |
$result["status"]="false";
|
| 226 |
liveuser |
472 |
|
| 3 |
liveuser |
473 |
#設置執行錯誤
|
|
|
474 |
$result["error"]=$checkResult;
|
| 226 |
liveuser |
475 |
|
| 3 |
liveuser |
476 |
#回傳結果
|
|
|
477 |
return $result;
|
| 226 |
liveuser |
478 |
|
| 3 |
liveuser |
479 |
}#if end
|
| 226 |
liveuser |
480 |
|
| 3 |
liveuser |
481 |
#取得使用的指令
|
|
|
482 |
$result["cmd"]=array_shift($conf["cmdArray"]);
|
| 226 |
liveuser |
483 |
|
| 3 |
liveuser |
484 |
#取得使用的參數
|
|
|
485 |
$result["params"]=$conf["cmdArray"];
|
| 226 |
liveuser |
486 |
|
| 3 |
liveuser |
487 |
#設置執行正常
|
|
|
488 |
$result["status"]="true";
|
| 226 |
liveuser |
489 |
|
| 3 |
liveuser |
490 |
#回傳結果
|
|
|
491 |
return $result;
|
| 226 |
liveuser |
492 |
|
| 3 |
liveuser |
493 |
}#function callShellHelper end
|
| 226 |
liveuser |
494 |
|
| 3 |
liveuser |
495 |
/*
|
|
|
496 |
#函式說明:
|
|
|
497 |
#呼叫shell執行系統命令,並取得回傳的內容.
|
|
|
498 |
#回傳結果:
|
|
|
499 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
500 |
#$result["error"],錯誤訊息陣列.
|
|
|
501 |
#$result["function"],當前執行的函數名稱.
|
|
|
502 |
#$result["argu"],使用的參數.
|
|
|
503 |
#$result["cmd"],執行的指令內容.
|
|
|
504 |
#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
| 60 |
liveuser |
505 |
#$result["output"],爲執行完後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
|
|
|
506 |
#$result["content"],為執行完後的輸出字串.
|
| 3 |
liveuser |
507 |
#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
508 |
#$result["running"],是否還在執行.
|
|
|
509 |
#$result["pid"],pid.
|
|
|
510 |
#$result["statusCode"],執行結束後的代碼.
|
|
|
511 |
#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
|
| 255 |
liveuser |
512 |
#$result["noEscaped"],陣列,儲存重新排序過未經過escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
|
| 3 |
liveuser |
513 |
#必填參數:
|
|
|
514 |
#$conf["command"],字串,要執行的指令.
|
|
|
515 |
$conf["command"]="";
|
|
|
516 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
|
|
517 |
$conf["fileArgu"]=__FILE__;
|
|
|
518 |
#可省略參數:
|
|
|
519 |
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
|
|
|
520 |
#$conf["argu"]=array("");
|
|
|
521 |
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
|
|
|
522 |
#$conf["arguIsAddr"]=array();
|
|
|
523 |
#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
|
|
|
524 |
#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
|
|
|
525 |
#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
|
|
|
526 |
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
|
|
|
527 |
#$conf["enablePrintDescription"]="true";
|
|
|
528 |
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
|
|
|
529 |
#$conf["printDescription"]="";
|
|
|
530 |
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
|
|
|
531 |
#$conf["escapeshellarg"]="false";
|
|
|
532 |
#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
|
|
|
533 |
#$conf["thereIsShellVar"]=array();
|
|
|
534 |
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
|
|
|
535 |
#$conf["username"]="";
|
|
|
536 |
#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
| 226 |
liveuser |
537 |
#$conf["password"]="";
|
| 3 |
liveuser |
538 |
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
|
|
|
539 |
#$conf["useScript"]="";
|
|
|
540 |
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
|
|
|
541 |
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
|
|
|
542 |
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
|
|
|
543 |
#$conf["inBackGround"]="";
|
|
|
544 |
#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
|
|
|
545 |
#$conf["getErr"]="false";
|
|
|
546 |
#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
|
|
|
547 |
#$conf["doNotRun"]="false";
|
| 255 |
liveuser |
548 |
#$conf["remoteIp"],字串,遠端ip或domainName,預設不使用,若存在則會透過ssh指令進行連線,然後執行指令.
|
|
|
549 |
#$conf["remoteIp"]="";
|
|
|
550 |
#$conf["remoteUser"],字串,遠端的使用者賬戶,預設不指定,亦即跟為運行該php程式的使用者.
|
|
|
551 |
#$conf["remoteUser"]="";
|
|
|
552 |
#參考資料:
|
| 3 |
liveuser |
553 |
#exec=>http://php.net/manual/en/function.exec.php
|
|
|
554 |
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
|
|
|
555 |
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
|
|
|
556 |
#備註:
|
|
|
557 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
|
|
|
558 |
#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
|
| 183 |
liveuser |
559 |
*/
|
| 3 |
liveuser |
560 |
public static function callShell(&$conf){
|
| 226 |
liveuser |
561 |
|
| 3 |
liveuser |
562 |
#初始化要回傳的結果
|
|
|
563 |
$result=array();
|
| 226 |
liveuser |
564 |
|
| 3 |
liveuser |
565 |
#取得當前執行的函數名稱
|
|
|
566 |
$result["function"]=__FUNCTION__;
|
| 226 |
liveuser |
567 |
|
| 3 |
liveuser |
568 |
#如果 $conf 不為陣列
|
|
|
569 |
if(gettype($conf)!="array"){
|
| 226 |
liveuser |
570 |
|
| 3 |
liveuser |
571 |
#設置執行失敗
|
|
|
572 |
$result["status"]="false";
|
| 226 |
liveuser |
573 |
|
| 3 |
liveuser |
574 |
#設置執行錯誤訊息
|
|
|
575 |
$result["error"][]="\$conf變數須為陣列形態";
|
|
|
576 |
|
|
|
577 |
#如果傳入的參數為 null
|
|
|
578 |
if($conf==null){
|
| 226 |
liveuser |
579 |
|
| 3 |
liveuser |
580 |
#設置執行錯誤訊息
|
|
|
581 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
| 226 |
liveuser |
582 |
|
| 3 |
liveuser |
583 |
}#if end
|
|
|
584 |
|
|
|
585 |
#回傳結果
|
|
|
586 |
return $result;
|
| 226 |
liveuser |
587 |
|
| 3 |
liveuser |
588 |
}#if end
|
| 226 |
liveuser |
589 |
|
| 3 |
liveuser |
590 |
#取得參數
|
|
|
591 |
$result["argu"]=$conf;
|
| 226 |
liveuser |
592 |
|
| 3 |
liveuser |
593 |
#參數檢查
|
|
|
594 |
#函式說明:
|
|
|
595 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
596 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
597 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
598 |
#$result["function"],當前執行的函式名稱.
|
|
|
599 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
600 |
#$result[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
601 |
#$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
602 |
#$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
603 |
#必填寫的參數:
|
|
|
604 |
#$conf["variableCheck::checkArguments"]["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
605 |
$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
|
|
|
606 |
#$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
607 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("command","fileArgu");
|
| 226 |
liveuser |
608 |
#$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
609 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string");
|
|
|
610 |
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
611 |
$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
612 |
#可以省略的參數:
|
|
|
613 |
#$conf["variableCheck::checkArguments"]["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true"。
|
|
|
614 |
#$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
|
|
|
615 |
#$conf["variableCheck::checkArguments"]["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
| 255 |
liveuser |
616 |
$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("pre","enablePrintDescription","printDescription","argu","escapeshellarg","username","password","useScript","logFilePath","inBackGround","arguIsAddr","getErr","doNotRun","thereIsShellVar","remoteIp","remoteUser");
|
| 226 |
liveuser |
617 |
#$conf["variableCheck::checkArguments"]["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
| 255 |
liveuser |
618 |
$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("array","string","string","array","string","string","string","string","string","string","array","string","string","array","string","string");
|
| 3 |
liveuser |
619 |
#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,"null"代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
| 255 |
liveuser |
620 |
$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(null,"false",null,null,"false",null,null,"false",".qbpwcf_tmp/external/callShell/","false",null,"false","false",null,null,null);
|
| 3 |
liveuser |
621 |
#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
622 |
$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"][]=array("argu","arguIsAddr");
|
|
|
623 |
$checkResult=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
|
|
|
624 |
unset($conf["variableCheck::checkArguments"]);
|
| 226 |
liveuser |
625 |
|
| 3 |
liveuser |
626 |
#如果檢查參數失敗
|
|
|
627 |
if($checkResult["status"]=="false"){
|
| 226 |
liveuser |
628 |
|
| 3 |
liveuser |
629 |
#設置執行失敗
|
|
|
630 |
$result["status"]="false";
|
| 226 |
liveuser |
631 |
|
| 3 |
liveuser |
632 |
#設置執行錯誤
|
|
|
633 |
$result["error"]=$checkResult;
|
| 226 |
liveuser |
634 |
|
| 3 |
liveuser |
635 |
#回傳結果
|
|
|
636 |
return $result;
|
| 226 |
liveuser |
637 |
|
| 3 |
liveuser |
638 |
}#if end
|
| 226 |
liveuser |
639 |
|
| 3 |
liveuser |
640 |
#如果檢查參數不通過
|
|
|
641 |
if($checkResult["passed"]=="false"){
|
| 226 |
liveuser |
642 |
|
| 3 |
liveuser |
643 |
#設置執行失敗
|
|
|
644 |
$result["status"]="false";
|
| 226 |
liveuser |
645 |
|
| 3 |
liveuser |
646 |
#設置執行錯誤
|
|
|
647 |
$result["error"]=$checkResult;
|
| 226 |
liveuser |
648 |
|
| 3 |
liveuser |
649 |
#回傳結果
|
|
|
650 |
return $result;
|
| 226 |
liveuser |
651 |
|
| 3 |
liveuser |
652 |
}#if end
|
| 226 |
liveuser |
653 |
|
| 3 |
liveuser |
654 |
#如果是web環境,則不能執行script指令
|
|
|
655 |
#涵式說明:
|
|
|
656 |
#判斷當前環境為web還是cmd
|
|
|
657 |
#回傳結果:
|
|
|
658 |
#$result,"web"或"cmd"
|
|
|
659 |
if(csInformation::getEnv()==="web" && $conf["useScript"]==="true"){
|
| 226 |
liveuser |
660 |
|
| 3 |
liveuser |
661 |
#設置執行失敗
|
|
|
662 |
$result["status"]="false";
|
| 226 |
liveuser |
663 |
|
| 3 |
liveuser |
664 |
#設置執行錯誤訊息
|
|
|
665 |
$result["error"][]="函數 ".$result["function"]." 使用參數 useScript 為 ".$conf["useScript"]." 時僅能在命令列環境下運行!";
|
| 226 |
liveuser |
666 |
|
| 3 |
liveuser |
667 |
#回傳結果
|
|
|
668 |
return $result;
|
| 226 |
liveuser |
669 |
|
| 3 |
liveuser |
670 |
}#if end
|
| 226 |
liveuser |
671 |
|
| 3 |
liveuser |
672 |
#如果有設置 $conf["pre"]
|
|
|
673 |
if(isset($conf["pre"])){
|
| 226 |
liveuser |
674 |
|
| 3 |
liveuser |
675 |
#初始化存放新的參數
|
|
|
676 |
$newArgu=array();
|
| 226 |
liveuser |
677 |
|
| 3 |
liveuser |
678 |
#初始化那些參數含有 shell 變數
|
|
|
679 |
$thereIsShellVar=array();
|
| 226 |
liveuser |
680 |
|
| 3 |
liveuser |
681 |
#記錄原先的指令
|
|
|
682 |
$oriCmd=$conf["command"];
|
| 226 |
liveuser |
683 |
|
| 3 |
liveuser |
684 |
#針對每個前置指令
|
|
|
685 |
foreach($conf["pre"] as $index=>$info){
|
| 226 |
liveuser |
686 |
|
| 3 |
liveuser |
687 |
#如果是第一個要執行的 pre 指令
|
|
|
688 |
if($index===0){
|
| 226 |
liveuser |
689 |
|
| 3 |
liveuser |
690 |
#更新 指令的名稱
|
|
|
691 |
$conf["command"]=$info["cmd"];
|
| 226 |
liveuser |
692 |
|
| 3 |
liveuser |
693 |
}#if end
|
| 226 |
liveuser |
694 |
|
| 3 |
liveuser |
695 |
#反之為後面的 pre 指令
|
|
|
696 |
else{
|
| 226 |
liveuser |
697 |
|
| 3 |
liveuser |
698 |
#增加參數
|
|
|
699 |
$newArgu[]=$info["cmd"];
|
| 226 |
liveuser |
700 |
|
|
|
701 |
#設置不含有 shell 變數
|
| 3 |
liveuser |
702 |
$thereIsShellVar[]="false";
|
| 226 |
liveuser |
703 |
|
| 3 |
liveuser |
704 |
}#else end
|
| 226 |
liveuser |
705 |
|
| 3 |
liveuser |
706 |
#針對每個參數
|
|
|
707 |
foreach($info["param"] as $param){
|
| 226 |
liveuser |
708 |
|
| 3 |
liveuser |
709 |
#增加參數
|
|
|
710 |
$newArgu[]=$param;
|
| 226 |
liveuser |
711 |
|
|
|
712 |
#設置不含有 shell 變數
|
| 3 |
liveuser |
713 |
$thereIsShellVar[]="false";
|
| 226 |
liveuser |
714 |
|
|
|
715 |
}#foreach end
|
|
|
716 |
|
|
|
717 |
#增加分號
|
| 3 |
liveuser |
718 |
$newArgu[]=";";
|
| 226 |
liveuser |
719 |
|
|
|
720 |
#設置不含有 shell 變數
|
| 3 |
liveuser |
721 |
$thereIsShellVar[]="false";
|
| 226 |
liveuser |
722 |
|
| 3 |
liveuser |
723 |
}#foreach end
|
| 226 |
liveuser |
724 |
|
| 3 |
liveuser |
725 |
#原本的指令變成參數
|
|
|
726 |
$newArgu[]=$oriCmd;
|
| 226 |
liveuser |
727 |
|
|
|
728 |
#設置不含有 shell 變數
|
| 3 |
liveuser |
729 |
$thereIsShellVar[]="false";
|
| 226 |
liveuser |
730 |
|
| 3 |
liveuser |
731 |
#針對本來要執行的參數
|
|
|
732 |
foreach($conf["argu"] as $index => $param){
|
| 226 |
liveuser |
733 |
|
| 3 |
liveuser |
734 |
#增加參數
|
|
|
735 |
$newArgu[]=$param;
|
| 226 |
liveuser |
736 |
|
| 3 |
liveuser |
737 |
#如果有設置是否含有 shell 變數
|
|
|
738 |
if(isset($conf["thereIsShellVar"])){
|
| 226 |
liveuser |
739 |
|
|
|
740 |
#設置是否含有 shell 變數
|
| 3 |
liveuser |
741 |
$thereIsShellVar[]=$conf["thereIsShellVar"][$index];
|
| 226 |
liveuser |
742 |
|
| 3 |
liveuser |
743 |
}#if end
|
| 226 |
liveuser |
744 |
|
| 3 |
liveuser |
745 |
#反之
|
|
|
746 |
else{
|
| 226 |
liveuser |
747 |
|
|
|
748 |
#設置不含有 shell 變數
|
| 3 |
liveuser |
749 |
$thereIsShellVar[]="false";
|
| 226 |
liveuser |
750 |
|
| 3 |
liveuser |
751 |
}#else end
|
| 226 |
liveuser |
752 |
|
| 3 |
liveuser |
753 |
}#foreach end
|
| 255 |
liveuser |
754 |
|
| 3 |
liveuser |
755 |
#更新 argu 參數
|
|
|
756 |
$conf["argu"]=$newArgu;
|
| 226 |
liveuser |
757 |
|
| 3 |
liveuser |
758 |
#更新 thereIsShellVar 參數
|
|
|
759 |
$conf["thereIsShellVar"]=$thereIsShellVar;
|
| 226 |
liveuser |
760 |
|
| 3 |
liveuser |
761 |
#移除處理掉的 pre 參數
|
|
|
762 |
unset($conf["pre"]);
|
| 226 |
liveuser |
763 |
|
| 3 |
liveuser |
764 |
#遞迴呼叫
|
|
|
765 |
return self::callShell($conf);
|
| 226 |
liveuser |
766 |
|
| 3 |
liveuser |
767 |
}#if end
|
| 226 |
liveuser |
768 |
|
| 3 |
liveuser |
769 |
#如果沒有設置要印出來的描述內容
|
|
|
770 |
if(!isset($conf["printDescription"])){
|
| 226 |
liveuser |
771 |
|
| 3 |
liveuser |
772 |
#預設為要執行的指令
|
|
|
773 |
$conf["printDescription"]=$conf["command"];
|
| 226 |
liveuser |
774 |
|
| 3 |
liveuser |
775 |
#如果有設置參數
|
|
|
776 |
if(isset($conf["argu"])){
|
| 226 |
liveuser |
777 |
|
| 3 |
liveuser |
778 |
#針對每個參數
|
|
|
779 |
foreach($conf["argu"] as $argu){
|
| 226 |
liveuser |
780 |
|
| 3 |
liveuser |
781 |
#若有參數則加在後面
|
|
|
782 |
$conf["printDescription"]=$conf["printDescription"]." ".$argu;
|
| 226 |
liveuser |
783 |
|
| 3 |
liveuser |
784 |
}#foreach end
|
| 226 |
liveuser |
785 |
|
| 3 |
liveuser |
786 |
}#if end
|
| 226 |
liveuser |
787 |
|
| 3 |
liveuser |
788 |
}#if end
|
| 226 |
liveuser |
789 |
|
| 3 |
liveuser |
790 |
#如果要印出 $conf["printDescription"] 的內容
|
|
|
791 |
if($conf["enablePrintDescription"]=="true"){
|
| 226 |
liveuser |
792 |
|
| 3 |
liveuser |
793 |
#輸出文字
|
|
|
794 |
echo "執行「".$conf["printDescription"]."」命令\n";
|
| 226 |
liveuser |
795 |
|
| 3 |
liveuser |
796 |
}#if end
|
| 226 |
liveuser |
797 |
|
| 3 |
liveuser |
798 |
#初始化執行的指令
|
|
|
799 |
$result["cmd"]=$conf["command"];
|
| 226 |
liveuser |
800 |
|
| 3 |
liveuser |
801 |
#初始化輸出
|
|
|
802 |
$output=array();
|
| 226 |
liveuser |
803 |
|
| 3 |
liveuser |
804 |
#取得 PATH
|
|
|
805 |
exec("echo \$PATH",$output,$status);
|
| 226 |
liveuser |
806 |
|
| 3 |
liveuser |
807 |
#如果執行失敗
|
|
|
808 |
if($status!==0){
|
| 226 |
liveuser |
809 |
|
| 3 |
liveuser |
810 |
#設置執行失敗
|
|
|
811 |
$result["status"]="false";
|
| 226 |
liveuser |
812 |
|
| 3 |
liveuser |
813 |
#設置執行錯誤訊息
|
|
|
814 |
$result["error"]="取得系統環境變數 PATH 失敗";
|
| 226 |
liveuser |
815 |
|
| 3 |
liveuser |
816 |
#回傳結果
|
|
|
817 |
return $result;
|
| 226 |
liveuser |
818 |
|
| 3 |
liveuser |
819 |
}#if end
|
| 226 |
liveuser |
820 |
|
|
|
821 |
#取得 PATH 設定
|
| 3 |
liveuser |
822 |
$PATHS=$output[0];
|
| 226 |
liveuser |
823 |
|
| 3 |
liveuser |
824 |
#函式說明:
|
|
|
825 |
#將固定格式的字串分開,並回傳分開的結果.
|
|
|
826 |
#回傳結果:
|
|
|
827 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
828 |
#$result["error"],錯誤訊息陣列
|
|
|
829 |
#$result["function"],當前執行的函數名稱.
|
|
|
830 |
#$result["argu"],使用的參數.
|
|
|
831 |
#$result["oriStr"],要分割的原始字串內容
|
|
|
832 |
#$result["dataArray"],爲分割好字串的陣列內容,$result["dataArray"][$i]爲第($i+1)段的內容。
|
|
|
833 |
#$result["dataCounts"],爲總共分成幾段
|
|
|
834 |
#$result["found"],是否有在$conf["stringIn"]找到$conf["spiltSymbol"],"true"代表有找到,"false"代表沒有找到.
|
|
|
835 |
#必填參數:
|
|
|
836 |
#$conf["stringIn"],字串,要處理的字串.
|
|
|
837 |
$conf["stringProcess::spiltString"]["stringIn"]=$PATHS;
|
|
|
838 |
#$conf["spiltSymbol"],字串,爲以哪個符號作爲分割.
|
|
|
839 |
$conf["stringProcess::spiltString"]["spiltSymbol"]=":";
|
|
|
840 |
#可省略參數:
|
|
|
841 |
#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
|
|
|
842 |
$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
|
|
|
843 |
#參考資料:
|
|
|
844 |
#無.
|
|
|
845 |
#備註:
|
|
|
846 |
#無.
|
|
|
847 |
$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
|
|
|
848 |
unset($conf["stringProcess::spiltString"]);
|
| 226 |
liveuser |
849 |
|
| 3 |
liveuser |
850 |
#如果分割字串失敗
|
|
|
851 |
if($spiltString["status"]==="false"){
|
| 226 |
liveuser |
852 |
|
| 3 |
liveuser |
853 |
#設置執行失敗
|
|
|
854 |
$result["status"]="false";
|
| 226 |
liveuser |
855 |
|
| 3 |
liveuser |
856 |
#設置執行錯誤訊息
|
|
|
857 |
$result["error"]="取得系統環境變數 PATH 失敗";
|
| 226 |
liveuser |
858 |
|
| 3 |
liveuser |
859 |
#回傳結果
|
|
|
860 |
return $result;
|
| 226 |
liveuser |
861 |
|
| 3 |
liveuser |
862 |
}#if end
|
| 226 |
liveuser |
863 |
|
| 3 |
liveuser |
864 |
#取得PATH字串陣列
|
|
|
865 |
$PATHS=$spiltString["dataArray"];
|
| 226 |
liveuser |
866 |
|
| 3 |
liveuser |
867 |
#預設沒有找到程式
|
|
|
868 |
$cmdFound=false;
|
| 226 |
liveuser |
869 |
|
| 218 |
liveuser |
870 |
#如果指令為 "if"
|
|
|
871 |
if($result["cmd"]==="if"){
|
| 226 |
liveuser |
872 |
|
| 218 |
liveuser |
873 |
#則其為 bash 提供的控制敘述,一定存在,不用檢查.
|
|
|
874 |
$cmdFound=true;
|
| 226 |
liveuser |
875 |
|
| 218 |
liveuser |
876 |
}#if end
|
| 226 |
liveuser |
877 |
|
| 3 |
liveuser |
878 |
#如果要執行的程式有指定位置
|
| 218 |
liveuser |
879 |
else if(strpos($result["cmd"],"/")!==false){
|
| 226 |
liveuser |
880 |
|
| 3 |
liveuser |
881 |
#要運行的指令
|
| 218 |
liveuser |
882 |
$cmd="if [ -f ".escapeshellarg($result["cmd"])." ]; then".PHP_EOL."exit;".PHP_EOL."else".PHP_EOL."exit 1;".PHP_EOL."fi";
|
| 226 |
liveuser |
883 |
|
| 3 |
liveuser |
884 |
#初始化輸出
|
|
|
885 |
$output=array();
|
| 226 |
liveuser |
886 |
|
| 3 |
liveuser |
887 |
#執行指令
|
|
|
888 |
exec($cmd,$output,$status);
|
| 255 |
liveuser |
889 |
|
| 3 |
liveuser |
890 |
#如果有找到
|
|
|
891 |
if($status===0){
|
| 226 |
liveuser |
892 |
|
| 3 |
liveuser |
893 |
#設置有找到
|
|
|
894 |
$cmdFound=true;
|
| 226 |
liveuser |
895 |
|
| 3 |
liveuser |
896 |
}#if end
|
| 226 |
liveuser |
897 |
|
| 3 |
liveuser |
898 |
}#if end
|
| 226 |
liveuser |
899 |
|
| 3 |
liveuser |
900 |
#反之
|
|
|
901 |
else{
|
| 226 |
liveuser |
902 |
|
| 3 |
liveuser |
903 |
#針對每個 $PATHS
|
|
|
904 |
foreach($PATHS as $path){
|
| 226 |
liveuser |
905 |
|
| 3 |
liveuser |
906 |
#要運行的指令
|
| 218 |
liveuser |
907 |
$cmd="if [ -f ".escapeshellarg($path."/".$result["cmd"])." ]; then".PHP_EOL."exit;".PHP_EOL."else".PHP_EOL."exit 1;".PHP_EOL."fi";
|
| 226 |
liveuser |
908 |
|
| 3 |
liveuser |
909 |
#初始化輸出
|
|
|
910 |
$output=array();
|
| 226 |
liveuser |
911 |
|
| 3 |
liveuser |
912 |
#執行指令
|
|
|
913 |
exec($cmd,$output,$status);
|
| 226 |
liveuser |
914 |
|
| 3 |
liveuser |
915 |
#如果執行正常
|
|
|
916 |
if($status===0){
|
| 226 |
liveuser |
917 |
|
| 3 |
liveuser |
918 |
#代表有找到程式
|
|
|
919 |
$cmdFound=true;
|
| 226 |
liveuser |
920 |
|
| 3 |
liveuser |
921 |
#跳離 foreach
|
|
|
922 |
break;
|
| 226 |
liveuser |
923 |
|
| 3 |
liveuser |
924 |
}#if end
|
| 226 |
liveuser |
925 |
|
| 3 |
liveuser |
926 |
}#foreach end
|
| 226 |
liveuser |
927 |
|
| 3 |
liveuser |
928 |
}#else end
|
| 226 |
liveuser |
929 |
|
| 3 |
liveuser |
930 |
#如果找不到要執行的程式
|
|
|
931 |
if($cmdFound===false){
|
| 226 |
liveuser |
932 |
|
| 255 |
liveuser |
933 |
#取得搜尋 "=" 的結果
|
| 3 |
liveuser |
934 |
$equalPosition=strpos($result["cmd"],"=");
|
| 226 |
liveuser |
935 |
|
| 3 |
liveuser |
936 |
#若指令不為 shell 變數指派
|
|
|
937 |
if( $equalPosition===false ){
|
| 226 |
liveuser |
938 |
|
| 3 |
liveuser |
939 |
#設置執行失敗
|
|
|
940 |
$result["status"]="false";
|
| 226 |
liveuser |
941 |
|
| 3 |
liveuser |
942 |
#設置執行錯誤訊息
|
|
|
943 |
$result["error"]="要執行的程式「".$result["cmd"]."」不存在!";
|
| 226 |
liveuser |
944 |
|
| 3 |
liveuser |
945 |
#回傳結果
|
|
|
946 |
return $result;
|
| 226 |
liveuser |
947 |
|
| 3 |
liveuser |
948 |
}#if end
|
| 226 |
liveuser |
949 |
|
| 3 |
liveuser |
950 |
#代表有找到程式
|
|
|
951 |
$cmdFound=true;
|
| 226 |
liveuser |
952 |
|
| 3 |
liveuser |
953 |
}#if end
|
| 226 |
liveuser |
954 |
|
| 255 |
liveuser |
955 |
#初始化儲存未escaped後的指令與參數資訊
|
|
|
956 |
$result["noEscaped"]=array();
|
| 226 |
liveuser |
957 |
|
| 255 |
liveuser |
958 |
#儲存執行的指令
|
|
|
959 |
$result["noEscaped"]["cmd"]=$conf["command"];
|
| 226 |
liveuser |
960 |
|
| 255 |
liveuser |
961 |
#初始化儲存執行的參數
|
|
|
962 |
$result["noEscaped"]["argu"]=array();
|
|
|
963 |
|
|
|
964 |
#初始化儲存escaped後的指令與參數資訊
|
|
|
965 |
$result["escaped"]=array();
|
| 226 |
liveuser |
966 |
|
| 255 |
liveuser |
967 |
#儲存執行的指令
|
|
|
968 |
$result["escaped"]["cmd"]=escapeshellarg($conf["command"]);
|
| 226 |
liveuser |
969 |
|
| 255 |
liveuser |
970 |
#初始化儲存執行的參數
|
|
|
971 |
$result["escaped"]["argu"]=array();
|
|
|
972 |
|
|
|
973 |
#如果有參數存在
|
|
|
974 |
if(isset($conf["argu"])){
|
| 226 |
liveuser |
975 |
|
| 255 |
liveuser |
976 |
#有幾個參數就執行幾次
|
|
|
977 |
for($i=0;$i<count($conf["argu"]);$i++){
|
| 226 |
liveuser |
978 |
|
| 255 |
liveuser |
979 |
#如果有指定位置參數
|
|
|
980 |
if(isset($conf["arguIsAddr"])){
|
| 226 |
liveuser |
981 |
|
| 255 |
liveuser |
982 |
#如果是位置參數
|
|
|
983 |
if($conf["arguIsAddr"][$i]==="true"){
|
| 226 |
liveuser |
984 |
|
| 255 |
liveuser |
985 |
#如果相對路徑就要轉換路徑
|
|
|
986 |
if(strpos($conf["argu"][$i],"/")!==0){
|
| 226 |
liveuser |
987 |
|
| 255 |
liveuser |
988 |
#函數說明:
|
|
|
989 |
#將多個路徑字串變成相對於當前路徑的相對路徑字串
|
|
|
990 |
#回傳結果:
|
|
|
991 |
#$result["status"],"true"爲建立成功,"false"爲建立失敗.
|
|
|
992 |
#$result["error"],錯誤訊息陣列.
|
|
|
993 |
#$result["function"],函數名稱.
|
|
|
994 |
#$result["content"],字串陣列,多個轉換好的相對路徑字串.
|
|
|
995 |
#必填參數:
|
|
|
996 |
#$conf["path"],陣列字串,要轉換成相對路徑的字串.;
|
|
|
997 |
$conf["fileAccess::getRelativePath"]["path"]=array($conf["argu"][$i]);
|
|
|
998 |
#$conf["fileArgu"],字串,當前路徑.
|
|
|
999 |
$conf["fileAccess::getRelativePath"]["fileArgu"]=$conf["fileArgu"];
|
|
|
1000 |
$getRelativePath=fileAccess::getRelativePath($conf["fileAccess::getRelativePath"]);
|
|
|
1001 |
unset($conf["fileAccess::getRelativePath"]);
|
| 226 |
liveuser |
1002 |
|
| 255 |
liveuser |
1003 |
#var_dump($getRelativePath);
|
| 226 |
liveuser |
1004 |
|
| 255 |
liveuser |
1005 |
#如果轉換路徑失敗
|
|
|
1006 |
if($getRelativePath["status"]==="false"){
|
| 226 |
liveuser |
1007 |
|
| 255 |
liveuser |
1008 |
#設置執行失敗的識別
|
|
|
1009 |
$result["status"]="false";
|
|
|
1010 |
|
|
|
1011 |
#設置執行失敗的訊息
|
|
|
1012 |
$result["error"]=$getRelativePath;
|
| 226 |
liveuser |
1013 |
|
| 255 |
liveuser |
1014 |
#回傳結果
|
|
|
1015 |
return $result;
|
| 226 |
liveuser |
1016 |
|
| 255 |
liveuser |
1017 |
}#if end
|
| 226 |
liveuser |
1018 |
|
| 255 |
liveuser |
1019 |
#取得相對路徑
|
|
|
1020 |
$conf["argu"][$i]=$getRelativePath["content"][0];
|
| 226 |
liveuser |
1021 |
|
| 255 |
liveuser |
1022 |
#取得相對路徑
|
|
|
1023 |
$relativePath=$getRelativePath["content"][0];
|
| 226 |
liveuser |
1024 |
|
| 255 |
liveuser |
1025 |
#保存其相對路徑
|
|
|
1026 |
$conf["argu"][$i]=$relativePath;
|
| 226 |
liveuser |
1027 |
|
| 3 |
liveuser |
1028 |
}#if end
|
| 226 |
liveuser |
1029 |
|
| 3 |
liveuser |
1030 |
}#if end
|
| 255 |
liveuser |
1031 |
|
|
|
1032 |
}#if end
|
|
|
1033 |
|
|
|
1034 |
#如果參數長度大於3
|
|
|
1035 |
if(strlen($conf["argu"][$i])>3){
|
| 226 |
liveuser |
1036 |
|
| 255 |
liveuser |
1037 |
#如果參數是 ${xxx}
|
|
|
1038 |
if( strpos($conf["argu"][$i],"\${")===0 && strpos($conf["argu"][$i],"}")===(strlen($conf["argu"][$i])-1) ){
|
| 226 |
liveuser |
1039 |
|
| 255 |
liveuser |
1040 |
#預設要空格
|
|
|
1041 |
$space=" ";
|
| 226 |
liveuser |
1042 |
|
| 255 |
liveuser |
1043 |
#如果前一個參數為 "=" 結尾
|
|
|
1044 |
if($result["cmd"][strlen($result["cmd"])-1]==="="){
|
| 226 |
liveuser |
1045 |
|
| 255 |
liveuser |
1046 |
#不放空格
|
|
|
1047 |
$space="";
|
| 226 |
liveuser |
1048 |
|
| 255 |
liveuser |
1049 |
#如果是第一個參數
|
|
|
1050 |
if($i===0){
|
| 226 |
liveuser |
1051 |
|
| 255 |
liveuser |
1052 |
#更新真正執行的指令
|
|
|
1053 |
$result["cmd"]=$result["cmd"].$space.$conf["argu"][$i];
|
|
|
1054 |
|
|
|
1055 |
#儲存escape的cmd
|
|
|
1056 |
$result["escaped"]["cmd"]=escapeshellarg($result["cmd"]);
|
|
|
1057 |
|
|
|
1058 |
#儲存未escape的cmd
|
|
|
1059 |
$result["noEscaped"]["cmd"]=$result["cmd"];
|
| 226 |
liveuser |
1060 |
|
| 255 |
liveuser |
1061 |
}#if end
|
| 226 |
liveuser |
1062 |
|
| 255 |
liveuser |
1063 |
#反之真的為參數
|
|
|
1064 |
else{
|
| 226 |
liveuser |
1065 |
|
| 255 |
liveuser |
1066 |
#取得參數的索引
|
|
|
1067 |
$arguIndex=count($conf["argu"])-1;
|
| 226 |
liveuser |
1068 |
|
| 255 |
liveuser |
1069 |
#儲存真正的參數
|
|
|
1070 |
$result["escaped"]["argu"][$arguIndex]=$conf["argu"][$arguIndex].$space.$conf["argu"][$i];
|
| 226 |
liveuser |
1071 |
|
| 255 |
liveuser |
1072 |
#儲存未escape的參數
|
|
|
1073 |
$result["unescape"]["argu"][$arguIndex]=$conf["argu"][$arguIndex].$space.$conf["argu"][$i];
|
| 226 |
liveuser |
1074 |
|
| 255 |
liveuser |
1075 |
}#else end
|
| 226 |
liveuser |
1076 |
|
| 255 |
liveuser |
1077 |
}#if end
|
| 226 |
liveuser |
1078 |
|
| 255 |
liveuser |
1079 |
#反之
|
|
|
1080 |
else{
|
| 226 |
liveuser |
1081 |
|
| 255 |
liveuser |
1082 |
#儲存未escape的參數
|
|
|
1083 |
$result["noescaped"]["argu"][]=$conf["argu"][$i];
|
|
|
1084 |
|
|
|
1085 |
#儲存escape的參數
|
|
|
1086 |
$result["escaped"]["argu"][]=escapeshellarg($conf["argu"][$i]);
|
| 226 |
liveuser |
1087 |
|
| 255 |
liveuser |
1088 |
}#else
|
| 226 |
liveuser |
1089 |
|
| 255 |
liveuser |
1090 |
#組合執行的指令
|
|
|
1091 |
$result["cmd"]=$result["cmd"].$space.$conf["argu"][$i];
|
| 226 |
liveuser |
1092 |
|
| 255 |
liveuser |
1093 |
#下一個參數
|
|
|
1094 |
continue;
|
| 226 |
liveuser |
1095 |
|
| 3 |
liveuser |
1096 |
}#if end
|
| 226 |
liveuser |
1097 |
|
| 255 |
liveuser |
1098 |
}#if end
|
| 226 |
liveuser |
1099 |
|
| 255 |
liveuser |
1100 |
#如果參數是 < 、<< 、> 、>> 、| 、2>&1、;、`、~、&、$!
|
|
|
1101 |
if($conf["argu"][$i]===">" || $conf["argu"][$i]==="<" || $conf["argu"][$i]===">>" || $conf["argu"][$i]==="<<" || $conf["argu"][$i]==="|" || $conf["argu"][$i]==="2>&1" || $conf["argu"][$i]===";" || $conf["argu"][$i]==="`" || $conf["argu"][$i]==="~" || $conf["argu"][$i]==="&" || $conf["argu"][$i]==="$!"){
|
| 226 |
liveuser |
1102 |
|
| 255 |
liveuser |
1103 |
#預設要空格
|
|
|
1104 |
$space=" ";
|
| 226 |
liveuser |
1105 |
|
| 255 |
liveuser |
1106 |
#組合執行的指令
|
|
|
1107 |
$result["cmd"]=$result["cmd"].$space.$conf["argu"][$i];
|
| 226 |
liveuser |
1108 |
|
| 255 |
liveuser |
1109 |
#儲存真正的參數
|
|
|
1110 |
$result["escaped"]["argu"][]=$conf["argu"][$i];
|
|
|
1111 |
|
|
|
1112 |
}#if end
|
|
|
1113 |
|
|
|
1114 |
#反之
|
|
|
1115 |
else{
|
|
|
1116 |
|
|
|
1117 |
#如果是第一個參數,且指令結尾為「=」結束
|
|
|
1118 |
if($i===0 && $result["cmd"][strlen($result["cmd"])-1]==="="){
|
|
|
1119 |
|
|
|
1120 |
#處理參數後再串接指令
|
|
|
1121 |
$result["cmd"]=$result["cmd"].escapeshellarg($conf["argu"][$i]);
|
|
|
1122 |
|
|
|
1123 |
#更新真正執行的指令
|
|
|
1124 |
$result["escaped"]["cmd"]=$result["cmd"];
|
|
|
1125 |
|
|
|
1126 |
#儲存unescape的指令
|
|
|
1127 |
$result["noescaped"]["cmd"]=$result["cmd"];
|
|
|
1128 |
|
| 3 |
liveuser |
1129 |
}#if end
|
| 226 |
liveuser |
1130 |
|
| 3 |
liveuser |
1131 |
#反之
|
|
|
1132 |
else{
|
| 226 |
liveuser |
1133 |
|
| 255 |
liveuser |
1134 |
#儲存unescape的參數
|
|
|
1135 |
$unescapeArgu=$conf["argu"][$i];
|
| 226 |
liveuser |
1136 |
|
| 255 |
liveuser |
1137 |
#儲存escape好的參數
|
|
|
1138 |
$escapeArgu=escapeshellarg($conf["argu"][$i]);
|
| 226 |
liveuser |
1139 |
|
| 255 |
liveuser |
1140 |
#如果有設置 $conf["thereIsShellVar"]
|
|
|
1141 |
if(isset($conf["thereIsShellVar"])){
|
| 226 |
liveuser |
1142 |
|
| 255 |
liveuser |
1143 |
#如果有設置 $conf["thereIsShellVar"][$i]
|
|
|
1144 |
if(isset($conf["thereIsShellVar"][$i])){
|
| 226 |
liveuser |
1145 |
|
| 255 |
liveuser |
1146 |
#如果含有 shell 變數
|
|
|
1147 |
if($conf["thereIsShellVar"][$i]==="true"){
|
| 226 |
liveuser |
1148 |
|
| 255 |
liveuser |
1149 |
#檢查是否使用 shell 變數
|
|
|
1150 |
#函式說明:
|
|
|
1151 |
#尋找陣列中有無符合的key
|
|
|
1152 |
#回傳結果:
|
|
|
1153 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
1154 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
1155 |
#$result["function"],當前執行的函式名稱.
|
|
|
1156 |
#$result["argu"],所使用的參數.
|
|
|
1157 |
#$result["founded"],是否有找到shell變數,"true"代表有找到,"false"代表沒有找到..
|
|
|
1158 |
#必填參數:
|
|
|
1159 |
#$conf["input"],字串,要檢查的字串..
|
|
|
1160 |
$conf["search::findShellVar"]["input"]=$escapeArgu;
|
|
|
1161 |
#可省略參數:
|
|
|
1162 |
#無.
|
|
|
1163 |
#參考資料:
|
|
|
1164 |
#無.
|
|
|
1165 |
#備註:
|
|
|
1166 |
#無.
|
|
|
1167 |
$findShellVar=search::findShellVar($conf["search::findShellVar"]);
|
|
|
1168 |
unset($conf["search::findShellVar"]);
|
| 226 |
liveuser |
1169 |
|
| 255 |
liveuser |
1170 |
#如果檢查失敗
|
|
|
1171 |
if($findShellVar["status"]=="false"){
|
| 226 |
liveuser |
1172 |
|
| 255 |
liveuser |
1173 |
#設置錯誤識別
|
|
|
1174 |
$result["status"]="false";
|
| 226 |
liveuser |
1175 |
|
| 255 |
liveuser |
1176 |
#設置錯誤訊息
|
|
|
1177 |
$result["error"]=$findShellVar;
|
| 226 |
liveuser |
1178 |
|
| 255 |
liveuser |
1179 |
#回傳解果
|
|
|
1180 |
return $result;
|
|
|
1181 |
|
|
|
1182 |
}#if end
|
|
|
1183 |
|
|
|
1184 |
#含有 shell 變數
|
|
|
1185 |
if($findShellVar["founded"]=="true"){
|
|
|
1186 |
|
|
|
1187 |
#確認該字串是否 開頭跟結尾為 「'」
|
|
|
1188 |
#函式說明:
|
|
|
1189 |
#取得符合特定字首與字尾的字串
|
| 3 |
liveuser |
1190 |
#回傳結果:
|
| 255 |
liveuser |
1191 |
#$result["status"],若爲"true"則代表執行正常;若爲"false"則代表執行失敗。
|
|
|
1192 |
#$result["function"],當前執行的函數名稱.
|
|
|
1193 |
#$result["error"],錯誤訊息陣列.
|
|
|
1194 |
#$result["founded"],若為"true"則代表有找到符合字首條件的結果;若爲"false"則代表沒有找到。
|
|
|
1195 |
#$result["returnString"],爲符合字首條件的字串內容。
|
|
|
1196 |
#$result["argu"],使用的參數.
|
| 3 |
liveuser |
1197 |
#必填參數:
|
| 255 |
liveuser |
1198 |
#$conf["checkString"],字串,要檢查的字串.
|
|
|
1199 |
$conf["search::getMeetConditionsString"]["checkString"]=$escapeArgu;
|
| 3 |
liveuser |
1200 |
#可省略參數:
|
| 255 |
liveuser |
1201 |
#$conf["frontWord"],字串,用來檢查字首應該要有什麼字串,預設不指定.
|
|
|
1202 |
$conf["search::getMeetConditionsString"]["frontWord"]="'";
|
|
|
1203 |
#$conf["tailWord"],字串,用來檢查字尾應該要有什麼字串,預設不指定.
|
|
|
1204 |
$conf["search::getMeetConditionsString"]["tailWord"]="'";
|
| 3 |
liveuser |
1205 |
#參考資料:
|
| 255 |
liveuser |
1206 |
#str_spilt(),可以將字串依照字母分割成一個個陣列字串。
|
| 3 |
liveuser |
1207 |
#備註:
|
|
|
1208 |
#無.
|
| 255 |
liveuser |
1209 |
$getMeetConditionsString=search::getMeetConditionsString($conf["search::getMeetConditionsString"]);
|
|
|
1210 |
unset($conf["search::getMeetConditionsString"]);
|
| 226 |
liveuser |
1211 |
|
| 3 |
liveuser |
1212 |
#如果檢查失敗
|
| 255 |
liveuser |
1213 |
if($getMeetConditionsString["status"]=="false"){
|
| 226 |
liveuser |
1214 |
|
| 3 |
liveuser |
1215 |
#設置錯誤識別
|
|
|
1216 |
$result["status"]="false";
|
| 226 |
liveuser |
1217 |
|
| 3 |
liveuser |
1218 |
#設置錯誤訊息
|
| 255 |
liveuser |
1219 |
$result["error"]=$getMeetConditionsString;
|
| 226 |
liveuser |
1220 |
|
| 3 |
liveuser |
1221 |
#回傳解果
|
|
|
1222 |
return $result;
|
| 226 |
liveuser |
1223 |
|
| 3 |
liveuser |
1224 |
}#if end
|
| 226 |
liveuser |
1225 |
|
| 3 |
liveuser |
1226 |
#含有 shell 變數
|
| 255 |
liveuser |
1227 |
if($getMeetConditionsString["founded"]==="true"){
|
| 226 |
liveuser |
1228 |
|
| 255 |
liveuser |
1229 |
#將含有 shell 變數的參數變成 '....'${hellVar}'....'
|
| 3 |
liveuser |
1230 |
#函式說明:
|
| 255 |
liveuser |
1231 |
#處理字串避免網頁出錯
|
| 3 |
liveuser |
1232 |
#回傳結果:
|
| 255 |
liveuser |
1233 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
1234 |
#$result["function"],當前執行的函數.
|
|
|
1235 |
#$result["content"],爲處理好的字串.
|
| 3 |
liveuser |
1236 |
#$result["error"],錯誤訊息陣列.
|
|
|
1237 |
#$result["argu"],使用的參數.
|
|
|
1238 |
#必填參數:
|
| 255 |
liveuser |
1239 |
#$conf["stringIn"],字串,爲要處理的字串
|
|
|
1240 |
$conf["stringProcess::correctCharacter"]["stringIn"]=$escapeArgu;
|
| 3 |
liveuser |
1241 |
#可省略參數:
|
| 255 |
liveuser |
1242 |
#$conf["selectedCharacter"],字串陣列,爲被選擇要處理的字串/字元,須爲陣列值。若不設定則預設爲要將這些字串作替換 ("<",">","=","//","'","$","%","&","|","/*","*","#","\"").特殊字元,「\n」代表換行,「\t」代表tab鍵的間隔
|
|
|
1243 |
$conf["stringProcess::correctCharacter"]["selectedCharacter"]=array("\${","}");
|
|
|
1244 |
#$conf["changeTo"],字串陣列,爲被選擇的字元要換成什麼字串/字元,須爲陣列值。若不設定,則預設爲更換成""(空字串).
|
|
|
1245 |
$conf["stringProcess::correctCharacter"]["changeTo"]=array("'\${","}'");
|
| 3 |
liveuser |
1246 |
#參考資料:
|
| 255 |
liveuser |
1247 |
#無.
|
| 3 |
liveuser |
1248 |
#備註:
|
|
|
1249 |
#無.
|
| 255 |
liveuser |
1250 |
$correctCharacter=stringProcess::correctCharacter($conf["stringProcess::correctCharacter"]);
|
|
|
1251 |
unset($conf["stringProcess::correctCharacter"]);
|
| 226 |
liveuser |
1252 |
|
| 255 |
liveuser |
1253 |
#如果執行失敗
|
|
|
1254 |
if($correctCharacter["status"]=="false"){
|
| 226 |
liveuser |
1255 |
|
| 3 |
liveuser |
1256 |
#設置錯誤識別
|
|
|
1257 |
$result["status"]="false";
|
| 226 |
liveuser |
1258 |
|
| 3 |
liveuser |
1259 |
#設置錯誤訊息
|
| 255 |
liveuser |
1260 |
$result["error"]=$correctCharacter;
|
| 226 |
liveuser |
1261 |
|
| 3 |
liveuser |
1262 |
#回傳解果
|
|
|
1263 |
return $result;
|
| 226 |
liveuser |
1264 |
|
| 3 |
liveuser |
1265 |
}#if end
|
| 226 |
liveuser |
1266 |
|
| 255 |
liveuser |
1267 |
#更新成可以呈現 shell 變數的字串
|
|
|
1268 |
$escapeArgu=$correctCharacter["content"];
|
| 226 |
liveuser |
1269 |
|
| 3 |
liveuser |
1270 |
}#if end
|
| 226 |
liveuser |
1271 |
|
| 3 |
liveuser |
1272 |
}#if end
|
| 226 |
liveuser |
1273 |
|
| 3 |
liveuser |
1274 |
}#if end
|
| 226 |
liveuser |
1275 |
|
| 3 |
liveuser |
1276 |
}#if end
|
| 226 |
liveuser |
1277 |
|
| 255 |
liveuser |
1278 |
}#if end
|
|
|
1279 |
|
|
|
1280 |
#如果是要執行 escpaed 後的指令
|
|
|
1281 |
if($conf["escapeshellarg"]==="true"){
|
|
|
1282 |
|
| 3 |
liveuser |
1283 |
#串接指令
|
|
|
1284 |
$result["cmd"]=$result["cmd"]." ".$escapeArgu;
|
| 255 |
liveuser |
1285 |
|
|
|
1286 |
}#if end
|
|
|
1287 |
|
|
|
1288 |
#反之
|
|
|
1289 |
else{
|
|
|
1290 |
|
|
|
1291 |
#串接指令
|
|
|
1292 |
$result["cmd"]=$result["cmd"]." ".$unescapeArgu;
|
|
|
1293 |
|
|
|
1294 |
}#else end
|
| 226 |
liveuser |
1295 |
|
| 255 |
liveuser |
1296 |
#儲存真正的參數
|
|
|
1297 |
$result["escaped"]["argu"][]=$escapeArgu;
|
|
|
1298 |
|
|
|
1299 |
#儲存未escape的參數
|
|
|
1300 |
$result["noEscaped"]["argu"][]=$unescapeArgu;
|
| 226 |
liveuser |
1301 |
|
| 3 |
liveuser |
1302 |
}#else end
|
| 226 |
liveuser |
1303 |
|
| 3 |
liveuser |
1304 |
}#for end
|
| 226 |
liveuser |
1305 |
|
| 3 |
liveuser |
1306 |
}#if end
|
| 226 |
liveuser |
1307 |
|
| 3 |
liveuser |
1308 |
}#if end
|
| 226 |
liveuser |
1309 |
|
| 3 |
liveuser |
1310 |
#如果有設置使用者名稱
|
|
|
1311 |
if(isset($conf["username"])){
|
| 226 |
liveuser |
1312 |
|
| 3 |
liveuser |
1313 |
#檢查環境為apache還是cmd
|
|
|
1314 |
#涵式說明:
|
|
|
1315 |
#判斷當前環境為web還是cmd
|
|
|
1316 |
#回傳結果:
|
|
|
1317 |
#$result,"web"或"cmd"
|
|
|
1318 |
if(csInformation::getEnv()=="web"){
|
| 226 |
liveuser |
1319 |
|
| 3 |
liveuser |
1320 |
#設置執行失敗
|
|
|
1321 |
$result["status"]="false";
|
| 226 |
liveuser |
1322 |
|
| 3 |
liveuser |
1323 |
#設置執行錯誤訊息
|
|
|
1324 |
$result["error"][]="函數 ".$result["function"]." 的 username 參數僅能在命令列環境下運行!";
|
| 226 |
liveuser |
1325 |
|
| 3 |
liveuser |
1326 |
#回傳結果
|
|
|
1327 |
return $result;
|
| 226 |
liveuser |
1328 |
|
| 3 |
liveuser |
1329 |
}#if end
|
| 226 |
liveuser |
1330 |
|
| 3 |
liveuser |
1331 |
#函式說明:
|
|
|
1332 |
#取得用戶端的資訊,並依據需要寫入到資料表裡面
|
|
|
1333 |
#回傳的結果:
|
|
|
1334 |
#$result["status"],執行是否正常,"true"代表執行成功,"false"代表執行失敗.
|
|
|
1335 |
#$result["error"],錯誤訊息.
|
|
|
1336 |
#$result["function"],檔前執行的函數名稱.
|
|
|
1337 |
#$result["mode"],當前的模式是"cmd"還是"web".
|
|
|
1338 |
#$result["userBrowserType"],爲使用者的瀏覽器資訊
|
|
|
1339 |
#$result["userIp"],爲使用者的IP
|
|
|
1340 |
#$result["serverIp"],為伺服器的IP
|
|
|
1341 |
#$result["server_name"],伺服器的 domain name
|
|
|
1342 |
#$result["scheme"],通訊協定
|
|
|
1343 |
#$result["serverPort"],伺服器給對外下載網頁的port
|
|
|
1344 |
#$result["requestUri"],爲使用者要求的網址/php檔案.
|
|
|
1345 |
#$result["requestUriFull"],為使用者要求的完整網址/php檔案路徑.
|
|
|
1346 |
#$result["clientRequestIP"],用戶端要求的ip與port
|
|
|
1347 |
#$result["username"],爲使用者目前的帳戶,若爲""則表示尚未登入成功
|
|
|
1348 |
#$result["phpUser"],運行該php的使用者帳戶.若為空字串則代表非使用者直接觸發.
|
|
|
1349 |
#$result["phpUserType"],運行該php的使用者帳戶類型,可能有"regular(no wheel member)","wheel(can use sudo)","intrinsic(root)","system(qemu,apache,...)".
|
|
|
1350 |
#$result["header"],接收到的 header 陣列.
|
|
|
1351 |
#$result["body"],接收到的 body 字串.
|
|
|
1352 |
#必填參數:
|
|
|
1353 |
#$conf["getAccount"],字串,是否要取得帳號,"true"代表要;"false"代表不要.
|
|
|
1354 |
$conf["csInformation::getConnectionInfo"]["getAccount"]="true";
|
|
|
1355 |
#可省略參數:
|
|
|
1356 |
#$conf["accountVar"],字串,帳號儲存在哪個變數裏面,預設爲$_SESSION["username"].
|
|
|
1357 |
#$conf["accountVar"]=$_SESSION["username"];
|
|
|
1358 |
#$conf["saveToDb"],字串,是否要除儲存到資料庫,"true"為要儲存",預設為"false"不儲存.
|
|
|
1359 |
#$conf["saveToDb"]="true";
|
|
|
1360 |
#$conf["dbAddress"],字串,爲mysql/mariadb server的位置,若saveToDb設為"true",則該參數為必填.
|
|
|
1361 |
#$conf["dbAddress"]=$dbAddress;
|
|
|
1362 |
#$conf["dbAccount"],字串,爲用於連入mysql/mariadb server時要使用的帳號,若saveToDb設為"true",則該參數為必填.
|
|
|
1363 |
#$conf["dbAccount"]=$dbAccount;
|
|
|
1364 |
#$conf["dbName"],字串,要選取的資料庫名稱,若saveToDb設為"true",則該參數為必填.
|
|
|
1365 |
#$conf["dbName"]=$dbName;
|
|
|
1366 |
#$conf["tableName"],字串,爲要插入資料的資料表名稱,若saveToDb設為"true",則該參數為必填.
|
|
|
1367 |
#$conf["tableName"]="visitorInfo";
|
|
|
1368 |
#$conf["columnName"],字串陣列,爲資料表的項目名稱,例如:$conf["columnName"]=array("columnName1","columnName2","columnName3",...);寫入的資料依序為,使用者帳戶、瀏覽器資訊、使用者IP、觀看的網址、當時的時間.若saveToDb設為"true",則該參數為必填.
|
|
|
1369 |
#$conf["columnName"]=array("username","userWebBrowser","userIp","requestUri","systemDateAndTime");
|
|
|
1370 |
#$conf["dbPassword"],字串,爲連線到mysql/mariadb server時要使用的密碼,可省略,若省略則代表不使用密碼.
|
|
|
1371 |
#$conf["dbPassword"]=$dbPassword;
|
|
|
1372 |
#參考資料:
|
|
|
1373 |
#$_SERVER=>http://php.net/manual/zh/reserved.variables.server.php
|
|
|
1374 |
#取得伺服器名稱與IP=>http://php.net/manual/en/function.gethostname.php
|
|
|
1375 |
#備註:
|
|
|
1376 |
#無.
|
|
|
1377 |
$getConnectionInfo=csInformation::getConnectionInfo($conf["csInformation::getConnectionInfo"]);
|
|
|
1378 |
unset($conf["csInformation::getConnectionInfo"]);
|
| 226 |
liveuser |
1379 |
|
| 3 |
liveuser |
1380 |
#如果執行失敗
|
|
|
1381 |
if($getConnectionInfo["status"]==="false"){
|
| 226 |
liveuser |
1382 |
|
| 3 |
liveuser |
1383 |
#設置執行失敗的識別
|
|
|
1384 |
$result["status"]="false";
|
| 226 |
liveuser |
1385 |
|
| 3 |
liveuser |
1386 |
#設置執行失敗的訊息
|
|
|
1387 |
$result["error"]=$getConnectionInfo;
|
| 226 |
liveuser |
1388 |
|
| 3 |
liveuser |
1389 |
#回傳結果
|
|
|
1390 |
return $result;
|
| 226 |
liveuser |
1391 |
|
| 3 |
liveuser |
1392 |
}#if end
|
| 226 |
liveuser |
1393 |
|
| 3 |
liveuser |
1394 |
#如果不是以運行php的使用者角色來執行shell
|
|
|
1395 |
if($conf["username"]!==$getConnectionInfo["phpUser"]){
|
| 226 |
liveuser |
1396 |
|
| 3 |
liveuser |
1397 |
#如果有設置使用者密碼
|
|
|
1398 |
if(isset($conf["password"])){
|
| 226 |
liveuser |
1399 |
|
| 3 |
liveuser |
1400 |
#透過使用者與密碼來執行指令
|
|
|
1401 |
$result["cmd"]="echo ".$conf["password"]." | su ".$conf["username"]." -c '".$result["cmd"]."'";
|
| 226 |
liveuser |
1402 |
|
| 3 |
liveuser |
1403 |
}#if end
|
| 226 |
liveuser |
1404 |
|
| 3 |
liveuser |
1405 |
#反之沒有設置密碼
|
|
|
1406 |
else{
|
| 226 |
liveuser |
1407 |
|
| 3 |
liveuser |
1408 |
#透過指定的使用者來執行指令
|
|
|
1409 |
$result["cmd"]="su ".$conf["username"]." -c '".$result["cmd"]."'";
|
| 226 |
liveuser |
1410 |
|
| 3 |
liveuser |
1411 |
}#else end
|
| 226 |
liveuser |
1412 |
|
| 60 |
liveuser |
1413 |
}#if end
|
| 226 |
liveuser |
1414 |
|
| 3 |
liveuser |
1415 |
}#if end
|
| 226 |
liveuser |
1416 |
|
| 3 |
liveuser |
1417 |
#如果 $conf["useScript"] 為 "true"
|
|
|
1418 |
if($conf["useScript"]==="true"){
|
| 226 |
liveuser |
1419 |
|
| 3 |
liveuser |
1420 |
#轉換 $conf["logFile"] 為絕對路徑
|
|
|
1421 |
#函數說明:
|
|
|
1422 |
#將檔案的位置名稱變成網址,也可以取得檔案位於伺服器上檔案系統的絕對位置.
|
|
|
1423 |
#回傳結果:
|
|
|
1424 |
#$result["status"],"true"爲建立成功,"false"爲建立失敗.
|
|
|
1425 |
#$result["error"],錯誤訊息陣列.
|
| 226 |
liveuser |
1426 |
#$result["function"],函數名稱.
|
| 3 |
liveuser |
1427 |
#$result["content"],網址,若是在命令列執行,則為"null".
|
|
|
1428 |
#$result["webPathFromRoot"],相對於網頁根目錄的路徑.
|
|
|
1429 |
#$result["fileSystemAbsoulutePosition"],針對伺服器端的絕對位置,亦即從網頁「/」目錄開始的路徑.
|
|
|
1430 |
#$result["fileSystemRelativePosition"],針對伺服器檔案系統的相對位置.
|
|
|
1431 |
#必填參數:
|
|
|
1432 |
#$conf["address"],字串,檔案的相對位置,若為絕對位置則會自動轉換成相對位置.
|
|
|
1433 |
$conf["fileAccess::getInternetAddress"]["address"]=$conf["logFilePath"];
|
|
|
1434 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.
|
|
|
1435 |
$conf["fileAccess::getInternetAddress"]["fileArgu"]=$conf["fileArgu"];
|
|
|
1436 |
#可省略參數:
|
|
|
1437 |
#$conf["userDir"],字串,網頁是否置放於家目錄底下,"true"為是,"false"為不是,預設為"true".
|
|
|
1438 |
#$conf["fileAccess::getInternetAddress"]["userDir"]="true";
|
|
|
1439 |
#備註:
|
|
|
1440 |
#在命令列執行,所得的路徑是錯誤的。
|
|
|
1441 |
$getInternetAddress=fileAccess::getInternetAddress($conf["fileAccess::getInternetAddress"]);
|
|
|
1442 |
unset($conf["fileAccess::getInternetAddress"]);
|
| 226 |
liveuser |
1443 |
|
| 3 |
liveuser |
1444 |
#如果轉換路徑失敗
|
| 255 |
liveuser |
1445 |
if($getInternetAddress["status"]==="false"){
|
| 226 |
liveuser |
1446 |
|
| 3 |
liveuser |
1447 |
#設置執行失敗
|
|
|
1448 |
$result["status"]="false";
|
| 226 |
liveuser |
1449 |
|
| 3 |
liveuser |
1450 |
#設置執行錯誤
|
|
|
1451 |
$result["error"]=$getInternetAddress;
|
| 226 |
liveuser |
1452 |
|
| 3 |
liveuser |
1453 |
#回傳結果
|
|
|
1454 |
return $result;
|
| 226 |
liveuser |
1455 |
|
| 3 |
liveuser |
1456 |
}#if end
|
| 226 |
liveuser |
1457 |
|
| 3 |
liveuser |
1458 |
#取得轉換後的絕對路徑
|
|
|
1459 |
$conf["logFilePath"]=$getInternetAddress["fileSystemAbsoulutePosition"];
|
| 226 |
liveuser |
1460 |
|
| 3 |
liveuser |
1461 |
#確保logFile的路徑
|
|
|
1462 |
#涵式說明:
|
|
|
1463 |
#確保路徑存在.
|
|
|
1464 |
#回傳的結果:
|
|
|
1465 |
#$result["status"],執行正常與否,"true"代表正常,"false"代表不正常.
|
|
|
1466 |
#$result["error"],錯誤訊息陣列.
|
|
|
1467 |
#$resutl["function"],當前執行的涵式名稱.
|
|
|
1468 |
#$result["path"],建立好的路徑字串.
|
|
|
1469 |
#$result["fileName"],檔案名稱,若 $conf["haveFileName"] 為 "true" 則會回傳.
|
|
|
1470 |
#必填參數::
|
|
|
1471 |
#$conf["path"],要檢查的路徑
|
| 226 |
liveuser |
1472 |
$conf["fileAccess::validatePath"]["path"]=$conf["logFilePath"];
|
| 3 |
liveuser |
1473 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
|
|
|
1474 |
$conf["fileAccess::validatePath"]["fileArgu"]=$conf["fileArgu"];
|
|
|
1475 |
#可省略參數:
|
|
|
1476 |
#$conf["haveFileName"],字串,"true"代表有$conf["path"]檔案名稱,"false"代表$conf["path"]為純路徑,預設為"false".
|
|
|
1477 |
#$conf["fileAccess::validatePath"]["haveFileName"]="true";
|
|
|
1478 |
#$conf["dirPermission"],字串,新建資料夾的權限設定,預設爲0770,亦即擁有者,同群組者可以讀,寫,存取,其他人僅能存取.
|
|
|
1479 |
#$conf["dirPermission"]="";
|
|
|
1480 |
$validatePath=fileAccess::validatePath($conf["fileAccess::validatePath"]);
|
|
|
1481 |
unset($conf["fileAccess::validatePath"]);
|
| 226 |
liveuser |
1482 |
|
| 3 |
liveuser |
1483 |
#如果確保設定檔路徑失敗
|
|
|
1484 |
if($validatePath["status"]=="false"){
|
| 226 |
liveuser |
1485 |
|
| 3 |
liveuser |
1486 |
#設置執行不正常
|
|
|
1487 |
$result["status"]="false";
|
| 226 |
liveuser |
1488 |
|
| 3 |
liveuser |
1489 |
#設置執行錯誤
|
|
|
1490 |
$result["error"]=$validatePath;
|
| 226 |
liveuser |
1491 |
|
| 3 |
liveuser |
1492 |
#回傳結果
|
|
|
1493 |
return $result;
|
| 226 |
liveuser |
1494 |
|
| 3 |
liveuser |
1495 |
}#if end
|
| 226 |
liveuser |
1496 |
|
| 3 |
liveuser |
1497 |
#初始化 $conf["logFileName"]
|
|
|
1498 |
$conf["logFileName"]="";
|
| 226 |
liveuser |
1499 |
|
| 3 |
liveuser |
1500 |
#無窮迴圈
|
|
|
1501 |
for($i=0;$i<1;){
|
| 226 |
liveuser |
1502 |
|
| 3 |
liveuser |
1503 |
#函式說明:
|
|
|
1504 |
#php內建的date()函數
|
|
|
1505 |
#回傳結果:
|
|
|
1506 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
1507 |
#$result["error"],錯誤訊息陣列.
|
|
|
1508 |
#$result["content"],自釘格式的時間字串.
|
|
|
1509 |
#必填參數:
|
|
|
1510 |
#$conf["timeZone"],字串,時區代號,可以設定的時區列表=> http://www.php.net/manual/en/timezones.php 台灣則為"Asia/Taipei".
|
|
|
1511 |
$conf["time::buildInDate"]["timeZone"]="Asia/Taipei";
|
|
|
1512 |
#$conf["format"],字串,要顯示哪些時間單位,格式與順序為何?西元年為'Y',月份為'm',日為'd',星期為D,以24計的時為'G'以12計的時為'h',分為'i',秒為's',當年的第幾個禮拜為'W',時區為'e',當年第幾天為'z',若想顯示英文字請輸入"\字母".
|
|
|
1513 |
$conf["time::buildInDate"]["format"]="Y-m-d(星期D) G:i:s 第W周";
|
|
|
1514 |
#參考資料來源:
|
|
|
1515 |
#http://php.net/manual/en/function.date.php
|
|
|
1516 |
$buildInDate=time::buildInDate($conf["time::buildInDate"]);
|
|
|
1517 |
unset($conf["time::buildInDate"]);
|
| 226 |
liveuser |
1518 |
|
| 3 |
liveuser |
1519 |
#如果取得時間失敗
|
|
|
1520 |
if($buildInDate["status"]=="false"){
|
| 226 |
liveuser |
1521 |
|
| 3 |
liveuser |
1522 |
#設置執行不正常
|
|
|
1523 |
$result["status"]="false";
|
| 226 |
liveuser |
1524 |
|
| 3 |
liveuser |
1525 |
#設置執行錯誤
|
|
|
1526 |
$result["error"]=$buildInDate;
|
| 226 |
liveuser |
1527 |
|
| 3 |
liveuser |
1528 |
#回傳結果
|
|
|
1529 |
return $result;
|
| 226 |
liveuser |
1530 |
|
| 3 |
liveuser |
1531 |
}#if end
|
| 226 |
liveuser |
1532 |
|
| 3 |
liveuser |
1533 |
#取得logFileName
|
|
|
1534 |
$conf["logFileName"]="output-".$buildInDate["content"].".log";
|
| 226 |
liveuser |
1535 |
|
| 3 |
liveuser |
1536 |
#確認檔案名稱是否有重複
|
|
|
1537 |
#涵式說明:檢查多個檔案與資料夾是否存在.
|
|
|
1538 |
#回傳結果:
|
|
|
1539 |
#$result["status"],執行正常與否,"true"代表正常,"false"代表不正常.
|
|
|
1540 |
#$result["error"],錯誤訊息陣列.
|
|
|
1541 |
#$resutl["function"],當前執行的涵式名稱.
|
|
|
1542 |
#$result["allExist"],所有檔案皆存在的識別,"true"代表皆存在,"false"代表沒有全部都存在.
|
|
|
1543 |
#$result["varName"][$i],爲第$i個資料夾或檔案的路徑與名稱。
|
|
|
1544 |
#$result["varNameFullPath"][$i],爲第$i個資料夾或檔案的完整檔案系統路徑與名稱。
|
|
|
1545 |
#$result["varNameWebPath"][$i],為第$i個資料夾或檔案的網址
|
|
|
1546 |
#$result["varExist"][$i],爲第$i個資料夾或檔案是否存在,"true"代表存在,"false"代表不存在。
|
|
|
1547 |
#必填參數:
|
|
|
1548 |
#$conf["fileArray"],陣列字串,要檢查是否存在的檔案有哪些,須爲一維陣列數值。
|
|
|
1549 |
$conf["fileAccess::checkMultiFileExist"]["fileArray"]=array($conf["logFilePath"].$conf["logFileName"]);
|
|
|
1550 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
|
|
|
1551 |
$conf["fileAccess::checkMultiFileExist"]["fileArgu"]=$conf["fileArgu"];
|
|
|
1552 |
#可省略參數
|
|
|
1553 |
#$conf["disableWebSearch"],"字串",是否取消「當檔案找不到時,改用catchWebContent類別的wget函數來檢查檔案是否存在於網路上」的功能,"false"不取消,若要取消該功能請設為"true",若抓到的內容為空字串則會視為檔案不存在,預設為"true".
|
|
|
1554 |
#$conf["disableWebSearch"]="false";
|
|
|
1555 |
#$conf["userDir"],字串,網頁是否置放於家目錄底下,"true"為是,"false"為不是,預設為"true".
|
|
|
1556 |
#$conf["userDir"]="true";
|
|
|
1557 |
#參考資料來源:
|
|
|
1558 |
#http://php.net/manual/en/function.file-exists.php
|
|
|
1559 |
#http://php.net/manual/en/control-structures.foreach.php
|
|
|
1560 |
#備註:
|
|
|
1561 |
#函數file_exists檢查的路徑為檔案系統的路徑
|
|
|
1562 |
$checkMultiFileExist=fileAccess::checkMultiFileExist($conf["fileAccess::checkMultiFileExist"]);
|
|
|
1563 |
unset($conf["fileAccess::checkMultiFileExist"]);
|
| 226 |
liveuser |
1564 |
|
| 3 |
liveuser |
1565 |
#如果檢查檔案存在失敗
|
|
|
1566 |
if($checkMultiFileExist["status"]==="false"){
|
| 226 |
liveuser |
1567 |
|
| 3 |
liveuser |
1568 |
#設置執行不正常
|
|
|
1569 |
$result["status"]="false";
|
| 226 |
liveuser |
1570 |
|
| 3 |
liveuser |
1571 |
#設置執行錯誤
|
|
|
1572 |
$result["error"]=$checkMultiFileExist;
|
| 226 |
liveuser |
1573 |
|
| 3 |
liveuser |
1574 |
#回傳結果
|
|
|
1575 |
return $result;
|
| 226 |
liveuser |
1576 |
|
| 3 |
liveuser |
1577 |
}#if end
|
| 226 |
liveuser |
1578 |
|
| 3 |
liveuser |
1579 |
#如果檔案不存在
|
|
|
1580 |
if($checkMultiFileExist["allExist"]==="false"){
|
| 226 |
liveuser |
1581 |
|
| 3 |
liveuser |
1582 |
#跳出迴圈
|
|
|
1583 |
break;
|
| 226 |
liveuser |
1584 |
|
| 3 |
liveuser |
1585 |
}#else end
|
| 226 |
liveuser |
1586 |
|
| 3 |
liveuser |
1587 |
}#for end
|
| 226 |
liveuser |
1588 |
|
| 3 |
liveuser |
1589 |
#用script指令抓取輸出,並存到 $conf["logFilePath"]."/".$conf["logFileName"].
|
|
|
1590 |
$result["cmd"]="script -q -c \"".$result["cmd"]."\" \"".$conf["logFilePath"]."/".$conf["logFileName"]."\"";
|
| 226 |
liveuser |
1591 |
|
| 3 |
liveuser |
1592 |
}#if end
|
| 226 |
liveuser |
1593 |
|
| 255 |
liveuser |
1594 |
#如果有指定遠端ip
|
|
|
1595 |
if(isset($conf["remoteIp"])){
|
|
|
1596 |
|
|
|
1597 |
#設定ssh指令
|
|
|
1598 |
$ssh="ssh ".$conf["remoteIp"];
|
|
|
1599 |
|
|
|
1600 |
#如果有指定遠端使用者
|
|
|
1601 |
if(isset($conf["remoteUser"])){
|
|
|
1602 |
|
|
|
1603 |
#設定ssh指令
|
|
|
1604 |
$ssh="ssh ".$conf["remoteUser"]."@".$conf["remoteIp"];
|
|
|
1605 |
|
|
|
1606 |
}#if end
|
|
|
1607 |
|
|
|
1608 |
#檢測能否成功遠端的指令
|
|
|
1609 |
$testRemote="echo 'env | grep SSH_CONNECTION' | ".$ssh;
|
|
|
1610 |
|
|
|
1611 |
#debug
|
|
|
1612 |
#var_dump(__LINE__,$testRemote);
|
|
|
1613 |
|
|
|
1614 |
#初始化輸出
|
|
|
1615 |
$output=array();
|
|
|
1616 |
|
|
|
1617 |
#執行檢測遠端是否成功
|
|
|
1618 |
exec($testRemote,$output,$status);
|
|
|
1619 |
|
|
|
1620 |
#如果異常
|
|
|
1621 |
if($status!==0){
|
|
|
1622 |
|
|
|
1623 |
#設置執行不正常
|
|
|
1624 |
$result["status"]="false";
|
|
|
1625 |
|
|
|
1626 |
#設置執行錯誤
|
|
|
1627 |
$result["error"]="can not run cmd on remote shell(".$testRemote.")";
|
|
|
1628 |
|
|
|
1629 |
#回傳結果
|
|
|
1630 |
return $result;
|
|
|
1631 |
|
|
|
1632 |
}#if end
|
|
|
1633 |
|
|
|
1634 |
#如果異常
|
|
|
1635 |
if(count($output)===0){
|
|
|
1636 |
|
|
|
1637 |
#設置執行不正常
|
|
|
1638 |
$result["status"]="false";
|
|
|
1639 |
|
|
|
1640 |
#設置執行錯誤
|
|
|
1641 |
$result["error"]="can not run cmd on remote shell(".$testRemote.")";
|
|
|
1642 |
|
|
|
1643 |
#回傳結果
|
|
|
1644 |
return $result;
|
|
|
1645 |
|
|
|
1646 |
}#if end
|
|
|
1647 |
|
|
|
1648 |
#繼承既有參數
|
|
|
1649 |
$conf["external::callShell"]=$conf;
|
|
|
1650 |
|
|
|
1651 |
#連線到遠端的ssh參數
|
|
|
1652 |
$remote="";
|
|
|
1653 |
|
|
|
1654 |
#如果有 remoteIp 參數
|
|
|
1655 |
if(isset($conf["external::callShell"]["remoteIp"])){
|
|
|
1656 |
|
|
|
1657 |
#設置 remote ip
|
|
|
1658 |
$remote=$conf["external::callShell"]["remoteIp"];
|
|
|
1659 |
|
|
|
1660 |
#移除之
|
|
|
1661 |
unset($conf["external::callShell"]["remoteIp"]);
|
|
|
1662 |
|
|
|
1663 |
#如果有 remoteUser 參數
|
|
|
1664 |
if(isset($conf["external::callShell"]["remoteUser"])){
|
|
|
1665 |
|
|
|
1666 |
#設置 remote user
|
|
|
1667 |
$remote=$conf["external::callShell"]["remoteUser"]."@".$remote;
|
|
|
1668 |
|
|
|
1669 |
#移除之
|
|
|
1670 |
unset($conf["external::callShell"]["remoteUser"]);
|
|
|
1671 |
|
|
|
1672 |
}#if end
|
|
|
1673 |
|
|
|
1674 |
}#if end
|
|
|
1675 |
|
|
|
1676 |
#設置不執行,只要印出指令內容
|
|
|
1677 |
$conf["external::callShell"]["doNotRun"]="true";
|
|
|
1678 |
|
|
|
1679 |
#設置不escape
|
|
|
1680 |
#$conf["external::callShell"]["escapeshellarg"]="false";
|
|
|
1681 |
|
|
|
1682 |
#函式說明:
|
|
|
1683 |
#呼叫shell執行系統命令,並取得回傳的內容.
|
|
|
1684 |
#回傳結果:
|
|
|
1685 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
1686 |
#$result["error"],錯誤訊息陣列.
|
|
|
1687 |
#$result["function"],當前執行的函數名稱.
|
|
|
1688 |
#$result["argu"],使用的參數.
|
|
|
1689 |
#$result["cmd"],執行的指令內容.
|
|
|
1690 |
#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
1691 |
#$result["output"],爲執行完後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
|
|
|
1692 |
#$result["content"],為執行完後的輸出字串.
|
|
|
1693 |
#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
1694 |
#$result["running"],是否還在執行.
|
|
|
1695 |
#$result["pid"],pid.
|
|
|
1696 |
#$result["statusCode"],執行結束後的代碼.
|
|
|
1697 |
#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
|
|
|
1698 |
#$result["noEcaped"],陣列,儲存重新排序過未經過escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
|
|
|
1699 |
#必填參數:
|
|
|
1700 |
#$conf["command"],字串,要執行的指令.
|
|
|
1701 |
#$conf["external::callShell"]["command"]="";
|
|
|
1702 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
|
|
1703 |
#$conf["external::callShell"]["fileArgu"]=__FILE__;
|
|
|
1704 |
#可省略參數:
|
|
|
1705 |
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
|
|
|
1706 |
#$conf["argu"]=array("");
|
|
|
1707 |
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
|
|
|
1708 |
#$conf["arguIsAddr"]=array();
|
|
|
1709 |
#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
|
|
|
1710 |
#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
|
|
|
1711 |
#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
|
|
|
1712 |
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
|
|
|
1713 |
#$conf["enablePrintDescription"]="true";
|
|
|
1714 |
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
|
|
|
1715 |
#$conf["printDescription"]="";
|
|
|
1716 |
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
|
|
|
1717 |
#$conf["escapeshellarg"]="false";
|
|
|
1718 |
#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
|
|
|
1719 |
#$conf["thereIsShellVar"]=array();
|
|
|
1720 |
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
|
|
|
1721 |
#$conf["username"]="";
|
|
|
1722 |
#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
|
|
1723 |
#$conf["password"]="";
|
|
|
1724 |
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
|
|
|
1725 |
#$conf["useScript"]="";
|
|
|
1726 |
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
|
|
|
1727 |
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
|
|
|
1728 |
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
|
|
|
1729 |
#$conf["inBackGround"]="";
|
|
|
1730 |
#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
|
|
|
1731 |
#$conf["getErr"]="false";
|
|
|
1732 |
#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
|
|
|
1733 |
#$conf["doNotRun"]="false";
|
|
|
1734 |
#$conf["remoteIp"],字串,遠端ip或domainName,預設不使用,若存在則會透過ssh指令進行連線,然後執行指令.
|
|
|
1735 |
#$conf["remoteIp"]="";
|
|
|
1736 |
#$conf["remoteUser"],字串,遠端的使用者賬戶,預設不指定,亦即跟為運行該php程式的使用者.
|
|
|
1737 |
#$conf["remoteUser"]="";
|
|
|
1738 |
#參考資料:
|
|
|
1739 |
#exec=>http://php.net/manual/en/function.exec.php
|
|
|
1740 |
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
|
|
|
1741 |
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
|
|
|
1742 |
#備註:
|
|
|
1743 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
|
|
|
1744 |
#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
|
|
|
1745 |
$callShell=external::callShell($conf["external::callShell"]);
|
|
|
1746 |
unset($conf["external::callShell"]);
|
|
|
1747 |
|
|
|
1748 |
#如果執行異常
|
|
|
1749 |
if($callShell["status"]==="false"){
|
|
|
1750 |
|
|
|
1751 |
#設置執行不正常
|
|
|
1752 |
$result["status"]="false";
|
|
|
1753 |
|
|
|
1754 |
#設置執行錯誤
|
|
|
1755 |
$result["error"]=$callShell;
|
|
|
1756 |
|
|
|
1757 |
#回傳結果
|
|
|
1758 |
return $result;
|
|
|
1759 |
|
|
|
1760 |
}#if end
|
|
|
1761 |
|
|
|
1762 |
#指令與參數組成的陣列
|
|
|
1763 |
#var_dump(__LINE__,$callShell);exit;
|
|
|
1764 |
|
|
|
1765 |
#函式說明:
|
|
|
1766 |
#將一維陣列轉換為用特定符號間隔的字串,ex:array("1","2","3") to "a;b;c;".
|
|
|
1767 |
#回傳的結果:
|
|
|
1768 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
1769 |
#$result["function"],當前執行的function名稱
|
|
|
1770 |
#$result["error"],錯誤訊息陣列.
|
|
|
1771 |
#$result["content"],處理好的字串.
|
|
|
1772 |
#$result["argu"],使用的參數.
|
|
|
1773 |
#必填參數:
|
|
|
1774 |
#$conf["inputArray"],字串陣列,要轉成字串的一維陣列.
|
|
|
1775 |
$conf["arrays::arrayToString"]["inputArray"]=$callShell["noEscaped"]["array"];
|
|
|
1776 |
#可省略參數:
|
|
|
1777 |
#$conf["spiltSymbol"],字串,用來區隔字串的符號,預設為;
|
|
|
1778 |
$conf["arrays::arrayToString"]["spiltSymbol"]=" ";
|
|
|
1779 |
#$conf["skipEnd"],字串,結尾是否不要加上符號,預設為"false",要加上符號,"true"代表不要加上符號。
|
|
|
1780 |
$conf["arrays::arrayToString"]["skipEnd"]="true";
|
|
|
1781 |
#$conf["spiltSymbolAtStart"],字串,是否要在開頭加上spiltSymbol,預設為"false",代表不要;反之為“true”.
|
|
|
1782 |
#$conf["spiltSymbolAtStart"]="";
|
|
|
1783 |
#參考資料:
|
|
|
1784 |
#無.
|
|
|
1785 |
#備註:
|
|
|
1786 |
#無.
|
|
|
1787 |
$arrayToString=arrays::arrayToString($conf["arrays::arrayToString"]);
|
|
|
1788 |
unset($conf["arrays::arrayToString"]);
|
|
|
1789 |
|
|
|
1790 |
#如果執行異常
|
|
|
1791 |
if($arrayToString["status"]==="false"){
|
|
|
1792 |
|
|
|
1793 |
#設置執行不正常
|
|
|
1794 |
$result["status"]="false";
|
|
|
1795 |
|
|
|
1796 |
#設置執行錯誤
|
|
|
1797 |
$result["error"]=$arrayToString;
|
|
|
1798 |
|
|
|
1799 |
#回傳結果
|
|
|
1800 |
return $result;
|
|
|
1801 |
|
|
|
1802 |
}#if end
|
|
|
1803 |
|
|
|
1804 |
#初始化要執行的程式
|
|
|
1805 |
$cmd="echo";
|
|
|
1806 |
|
|
|
1807 |
#初始化要在遠端執行的參數
|
|
|
1808 |
$argu=array($arrayToString["content"],"|","ssh",$remote);
|
|
|
1809 |
|
|
|
1810 |
#函式說明:
|
|
|
1811 |
#呼叫shell執行系統命令,並取得回傳的內容.
|
|
|
1812 |
#回傳結果:
|
|
|
1813 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
1814 |
#$result["error"],錯誤訊息陣列.
|
|
|
1815 |
#$result["function"],當前執行的函數名稱.
|
|
|
1816 |
#$result["argu"],使用的參數.
|
|
|
1817 |
#$result["cmd"],執行的指令內容.
|
|
|
1818 |
#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
1819 |
#$result["output"],爲執行完後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
|
|
|
1820 |
#$result["content"],為執行完後的輸出字串.
|
|
|
1821 |
#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
1822 |
#$result["running"],是否還在執行.
|
|
|
1823 |
#$result["pid"],pid.
|
|
|
1824 |
#$result["statusCode"],執行結束後的代碼.
|
|
|
1825 |
#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
|
|
|
1826 |
#$result["noEcaped"],陣列,儲存重新排序過未經過escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
|
|
|
1827 |
#必填參數:
|
|
|
1828 |
#$conf["command"],字串,要執行的指令.
|
|
|
1829 |
$conf["external::callShell"]["command"]=$cmd;
|
|
|
1830 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
|
|
1831 |
$conf["external::callShell"]["fileArgu"]=__FILE__;
|
|
|
1832 |
#可省略參數:
|
|
|
1833 |
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
|
|
|
1834 |
$conf["external::callShell"]["argu"]=$argu;
|
|
|
1835 |
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
|
|
|
1836 |
#$conf["arguIsAddr"]=array();
|
|
|
1837 |
#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
|
|
|
1838 |
#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
|
|
|
1839 |
#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
|
|
|
1840 |
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
|
|
|
1841 |
#$conf["enablePrintDescription"]="true";
|
|
|
1842 |
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
|
|
|
1843 |
#$conf["printDescription"]="";
|
|
|
1844 |
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
|
|
|
1845 |
$conf["external::callShell"]["escapeshellarg"]="true";
|
|
|
1846 |
#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
|
|
|
1847 |
#$conf["thereIsShellVar"]=array();
|
|
|
1848 |
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
|
|
|
1849 |
#$conf["username"]="";
|
|
|
1850 |
#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
|
|
1851 |
#$conf["password"]="";
|
|
|
1852 |
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
|
|
|
1853 |
#$conf["useScript"]="";
|
|
|
1854 |
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
|
|
|
1855 |
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
|
|
|
1856 |
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
|
|
|
1857 |
#$conf["inBackGround"]="";
|
|
|
1858 |
#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
|
|
|
1859 |
#$conf["getErr"]="false";
|
|
|
1860 |
#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
|
|
|
1861 |
#$conf["doNotRun"]="false";
|
|
|
1862 |
#$conf["remoteIp"],字串,遠端ip或domainName,預設不使用,若存在則會透過ssh指令進行連線,然後執行指令.
|
|
|
1863 |
#$conf["remoteIp"]="";
|
|
|
1864 |
#$conf["remoteUser"],字串,遠端的使用者賬戶,預設不指定,亦即跟為運行該php程式的使用者.
|
|
|
1865 |
#$conf["remoteUser"]="";
|
|
|
1866 |
#參考資料:
|
|
|
1867 |
#exec=>http://php.net/manual/en/function.exec.php
|
|
|
1868 |
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
|
|
|
1869 |
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
|
|
|
1870 |
#備註:
|
|
|
1871 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
|
|
|
1872 |
#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
|
|
|
1873 |
return external::callShell($conf["external::callShell"]);
|
|
|
1874 |
unset($conf["external::callShell"]);
|
|
|
1875 |
|
|
|
1876 |
}#if end
|
|
|
1877 |
|
| 3 |
liveuser |
1878 |
#如果 $conf["inBackGround"] 為 "true"
|
|
|
1879 |
if($conf["inBackGround"]==="true"){
|
| 226 |
liveuser |
1880 |
|
| 3 |
liveuser |
1881 |
#函式說明:
|
|
|
1882 |
#呼叫shell執行系統命令,並取得回傳的內容與pid.
|
|
|
1883 |
#回傳結果:
|
|
|
1884 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
1885 |
#$result["error"],錯誤訊息陣列.
|
|
|
1886 |
#$result["function"],當前執行的函數名稱.
|
|
|
1887 |
#$result["cmd"],執行的指令內容.
|
|
|
1888 |
#$result["fullCmd"],執行的完整指令
|
|
|
1889 |
#$result["content"],爲執行完二元碼後的輸出陣列.
|
|
|
1890 |
#$result["tmpFileOutput"],儲存輸出的暫村檔案名稱.
|
|
|
1891 |
#$result["running"],程式是否還在執行.
|
|
|
1892 |
#$result["pid"],pid.
|
|
|
1893 |
#$result["statusCode"],執行結果狀態碼.
|
|
|
1894 |
#必填參數:
|
|
|
1895 |
#$conf["command"],字串,要執行的指令.
|
|
|
1896 |
$conf["external::callShellV2"]["command"]=$result["cmd"];
|
|
|
1897 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
|
|
1898 |
$conf["external::callShellV2"]["fileArgu"]=$conf["fileArgu"];
|
|
|
1899 |
#可省略參數:
|
|
|
1900 |
#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
|
|
|
1901 |
$conf["external::callShellV2"]["doNotRun"]=$conf["doNotRun"];
|
|
|
1902 |
#備註:
|
|
|
1903 |
#不是所有指令都能用apache的身份執行,目前已知java,javac,ping指令無法執行,透過su使用root身份可能會被selinux阻擋.
|
|
|
1904 |
#參考資料:
|
|
|
1905 |
#http://php.net/manual/en/function.exec.php
|
|
|
1906 |
$callShellV2=external::callShellInBg($conf["external::callShellV2"]);
|
|
|
1907 |
unset($conf["external::callShellV2"]);
|
| 226 |
liveuser |
1908 |
|
| 3 |
liveuser |
1909 |
#如果執行失敗
|
|
|
1910 |
if($callShellV2["status"]==="false"){
|
| 226 |
liveuser |
1911 |
|
| 3 |
liveuser |
1912 |
#設置執行不正常
|
|
|
1913 |
$result["status"]="false";
|
| 226 |
liveuser |
1914 |
|
| 3 |
liveuser |
1915 |
#設置執行錯誤
|
|
|
1916 |
$result["error"]=$callShellV2;
|
| 226 |
liveuser |
1917 |
|
| 3 |
liveuser |
1918 |
#回傳結果
|
|
|
1919 |
return $result;
|
| 226 |
liveuser |
1920 |
|
| 3 |
liveuser |
1921 |
}#if end
|
| 226 |
liveuser |
1922 |
|
| 3 |
liveuser |
1923 |
#執行到這邊代表執行正常
|
|
|
1924 |
$result["status"]="true";
|
|
|
1925 |
|
|
|
1926 |
#取得當下的輸出
|
|
|
1927 |
$result["content"]=$callShellV2["content"];
|
| 226 |
liveuser |
1928 |
|
| 3 |
liveuser |
1929 |
#取得集記錄輸出的暫存檔案位置
|
|
|
1930 |
$result["tmpFileOutput"]=$callShellV2["tmpFileOutput"];
|
| 226 |
liveuser |
1931 |
|
| 3 |
liveuser |
1932 |
#取得執行的完整指令
|
|
|
1933 |
$result["fullCmd"]=$callShellV2["fullCmd"];
|
|
|
1934 |
|
|
|
1935 |
#取得執行的指令
|
|
|
1936 |
$result["cmd"]=$callShellV2["cmd"];
|
|
|
1937 |
|
|
|
1938 |
#取得程序的pid
|
|
|
1939 |
$result["pid"]=$callShellV2["pid"];
|
|
|
1940 |
|
|
|
1941 |
#取得是否還在執行
|
|
|
1942 |
$result["running"]=$callShellV2["running"];
|
| 226 |
liveuser |
1943 |
|
| 3 |
liveuser |
1944 |
#取得執行狀態代碼
|
|
|
1945 |
$result["statusCode"]=$callShellV2["statusCode"];
|
|
|
1946 |
|
|
|
1947 |
#回傳結果
|
|
|
1948 |
return $result;
|
| 226 |
liveuser |
1949 |
|
| 3 |
liveuser |
1950 |
}#if end
|
| 226 |
liveuser |
1951 |
|
| 3 |
liveuser |
1952 |
#反之沒有要在背景中執行
|
|
|
1953 |
else{
|
| 226 |
liveuser |
1954 |
|
| 3 |
liveuser |
1955 |
#如果要將錯誤輸出導到標準輸出
|
|
|
1956 |
if($conf["getErr"]==="true"){
|
| 226 |
liveuser |
1957 |
|
| 3 |
liveuser |
1958 |
#指令加上 "2>&1"
|
|
|
1959 |
$result["cmd"]=$result["cmd"]." 2>&1";
|
| 226 |
liveuser |
1960 |
|
| 3 |
liveuser |
1961 |
}#if end
|
| 226 |
liveuser |
1962 |
|
| 3 |
liveuser |
1963 |
#如果沒有要執行指令
|
|
|
1964 |
if($conf["doNotRun"]==="true"){
|
| 226 |
liveuser |
1965 |
|
| 3 |
liveuser |
1966 |
#初始化執行結果狀態碼為0
|
|
|
1967 |
$result["statusCode"]=0;
|
| 226 |
liveuser |
1968 |
|
| 3 |
liveuser |
1969 |
#初始化輸出為空
|
|
|
1970 |
$resultArray=array();
|
| 226 |
liveuser |
1971 |
|
| 3 |
liveuser |
1972 |
#初始化執行結果狀態碼為0
|
|
|
1973 |
$execStatus=0;
|
| 226 |
liveuser |
1974 |
|
| 3 |
liveuser |
1975 |
}#if end
|
| 226 |
liveuser |
1976 |
|
| 3 |
liveuser |
1977 |
#反之要執行指令
|
|
|
1978 |
else{
|
| 226 |
liveuser |
1979 |
|
| 255 |
liveuser |
1980 |
#debug
|
|
|
1981 |
#var_dump(__LINE__,$result["cmd"]);
|
| 226 |
liveuser |
1982 |
|
| 3 |
liveuser |
1983 |
#執行腳本程式,將輸出結果儲存在 $resultArray 陣列裏面
|
| 60 |
liveuser |
1984 |
exec($result["cmd"],$resultArray,$execStatus);
|
| 226 |
liveuser |
1985 |
|
| 3 |
liveuser |
1986 |
#取得執行結果狀態碼
|
|
|
1987 |
$result["statusCode"]=$execStatus;
|
| 226 |
liveuser |
1988 |
|
| 3 |
liveuser |
1989 |
}#else end
|
| 226 |
liveuser |
1990 |
|
| 3 |
liveuser |
1991 |
}#else end
|
| 226 |
liveuser |
1992 |
|
| 3 |
liveuser |
1993 |
#如果 $conf["useScript"] 為 "true"
|
| 60 |
liveuser |
1994 |
if($conf["useScript"]=="true"){
|
| 226 |
liveuser |
1995 |
|
| 3 |
liveuser |
1996 |
#讀取暫存檔案的內容
|
|
|
1997 |
#函式說明:
|
|
|
1998 |
#依據行號分隔抓取檔案的內容,結果會回傳一個陣列
|
|
|
1999 |
#回傳的變數說明:
|
|
|
2000 |
#$result["status"],執行是否成功,"true"代表成功;"fasle"代表失敗.
|
|
|
2001 |
#$result["error"],錯誤訊息提示.
|
|
|
2002 |
#$result["warning"],警告訊息.
|
|
|
2003 |
#$result["function"],當前執行的函數名稱.
|
|
|
2004 |
#$result["fileContent"],爲檔案的內容陣列.
|
|
|
2005 |
#$result["lineCount"],爲檔案內容總共的行數.
|
|
|
2006 |
#$result["fullContent"],為檔案的完整內容.
|
|
|
2007 |
#必填參數::
|
|
|
2008 |
#$conf["filePositionAndName"],字串,爲檔案的位置以及名稱.
|
|
|
2009 |
$conf["fileAccess::getFileContent"]["filePositionAndName"]=$conf["logFilePath"]."/".$conf["logFileName"];
|
|
|
2010 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
|
|
|
2011 |
$conf["fileAccess::getFileContent"]["fileArgu"]=$conf["fileArgu"];
|
|
|
2012 |
#參考資料:
|
|
|
2013 |
#file(),取得檔案內容的行數.
|
|
|
2014 |
#file=>http:#php.net/manual/en/function.file.php
|
|
|
2015 |
#rtrim(),剔除透過file()取得每行內容結尾的換行符號.
|
|
|
2016 |
#filesize=>http://php.net/manual/en/function.filesize.php
|
|
|
2017 |
$getFileContent=fileAccess::getFileContent($conf["fileAccess::getFileContent"]);
|
|
|
2018 |
unset($conf["fileAccess::getFileContent"]);
|
| 226 |
liveuser |
2019 |
|
| 3 |
liveuser |
2020 |
#如果取得檔案內容失敗
|
|
|
2021 |
if($getFileContent["status"]=="false"){
|
| 226 |
liveuser |
2022 |
|
| 3 |
liveuser |
2023 |
#設置執行不正常
|
|
|
2024 |
$result["status"]="false";
|
| 226 |
liveuser |
2025 |
|
| 3 |
liveuser |
2026 |
#設置執行錯誤
|
|
|
2027 |
$result["error"]=$getFileContent;
|
| 226 |
liveuser |
2028 |
|
| 3 |
liveuser |
2029 |
#回傳結果
|
|
|
2030 |
return $result;
|
| 226 |
liveuser |
2031 |
|
| 3 |
liveuser |
2032 |
}#if end
|
| 226 |
liveuser |
2033 |
|
| 3 |
liveuser |
2034 |
#移除暫存檔案
|
|
|
2035 |
#涵式說明:
|
|
|
2036 |
#移除檔案
|
|
|
2037 |
#回傳的結果:
|
|
|
2038 |
#$result["status"],"true"代表移除成功,"false"代表移除失敗.
|
|
|
2039 |
#$result["error"],錯誤訊息陣列
|
|
|
2040 |
#$result["warning"],警告訊息陣列
|
|
|
2041 |
#$result["function"],當前執行的函數名稱
|
|
|
2042 |
#必填參數::
|
|
|
2043 |
#$conf["fileAddress"],字串,要移除檔案的位置.
|
|
|
2044 |
$conf["fileAccess::delFile"]["fileAddress"]=$conf["logFilePath"]."/".$conf["logFileName"];
|
|
|
2045 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.
|
|
|
2046 |
$conf["fileAccess::delFile"]["fileArgu"]=$conf["fileArgu"];
|
|
|
2047 |
#可省略參數:
|
|
|
2048 |
#$conf["commentsArray"],字串陣列,提示的文字描述,$conf["commentsArray"][$i]代表第($+1)行的描述.
|
|
|
2049 |
#$conf["commentsArray"]=array("");
|
|
|
2050 |
$delFile=fileAccess::delFile($conf["fileAccess::delFile"]);
|
|
|
2051 |
unset($conf["fileAccess::delFile"]);
|
| 226 |
liveuser |
2052 |
|
| 3 |
liveuser |
2053 |
#如果移除檔案失敗
|
|
|
2054 |
if($delFile["status"]=="false"){
|
| 226 |
liveuser |
2055 |
|
| 3 |
liveuser |
2056 |
#設置執行不正常
|
|
|
2057 |
$result["status"]="false";
|
| 226 |
liveuser |
2058 |
|
| 3 |
liveuser |
2059 |
#設置執行錯誤
|
|
|
2060 |
$result["error"]=$getFileContent;
|
| 226 |
liveuser |
2061 |
|
| 3 |
liveuser |
2062 |
#回傳結果
|
|
|
2063 |
return $result;
|
| 226 |
liveuser |
2064 |
|
| 3 |
liveuser |
2065 |
}#if end
|
| 226 |
liveuser |
2066 |
|
| 3 |
liveuser |
2067 |
#取得執行外部程式的輸出
|
|
|
2068 |
$result["output"]=$getFileContent["fileContent"];
|
| 226 |
liveuser |
2069 |
|
| 3 |
liveuser |
2070 |
}#if end
|
| 226 |
liveuser |
2071 |
|
| 3 |
liveuser |
2072 |
#反之
|
|
|
2073 |
else{
|
| 226 |
liveuser |
2074 |
|
| 3 |
liveuser |
2075 |
#取得執行外部程式的輸出
|
|
|
2076 |
$result["output"]=$resultArray;
|
| 226 |
liveuser |
2077 |
|
| 3 |
liveuser |
2078 |
}#else end
|
|
|
2079 |
|
| 255 |
liveuser |
2080 |
#初始化暫存 指令跟參數的陣列
|
|
|
2081 |
$cmdAndArguArray=array();
|
| 226 |
liveuser |
2082 |
|
| 255 |
liveuser |
2083 |
#取得第一個元素
|
|
|
2084 |
$cmdAndArguArray[]=&$result["escaped"]["cmd"];
|
| 226 |
liveuser |
2085 |
|
| 255 |
liveuser |
2086 |
#如果有 argu
|
|
|
2087 |
if(isset($result["escaped"]["argu"])){
|
| 226 |
liveuser |
2088 |
|
| 255 |
liveuser |
2089 |
#針對每個參數
|
|
|
2090 |
foreach($result["escaped"]["argu"] as $arguKey=>$arguVal){
|
| 226 |
liveuser |
2091 |
|
| 255 |
liveuser |
2092 |
#取得該參數
|
|
|
2093 |
$cmdAndArguArray[]=&$result["escaped"]["argu"][$arguKey];
|
| 226 |
liveuser |
2094 |
|
| 255 |
liveuser |
2095 |
}#foreach end
|
| 226 |
liveuser |
2096 |
|
| 3 |
liveuser |
2097 |
}#if end
|
| 226 |
liveuser |
2098 |
|
| 255 |
liveuser |
2099 |
#設置 cmd & argu 合併為一維陣列後的結果
|
|
|
2100 |
$result["escaped"]["array"]=&$cmdAndArguArray;
|
| 226 |
liveuser |
2101 |
|
| 255 |
liveuser |
2102 |
#從 $result["noEscaped"]["cmd"] 跟 $result["noEscaped"]["argu"] 產生 $result["noEscaped"]["array"]
|
|
|
2103 |
#函式說明:
|
|
|
2104 |
#將多個一維陣列串聯起來,key從0開始排序.
|
|
|
2105 |
#回傳的結果:
|
|
|
2106 |
#$result["status"],"true"表執行正常,"false"代表執行不正常.
|
|
|
2107 |
#$result["error"],錯誤訊息陣列.
|
|
|
2108 |
#$result["function"],當前執行的函數.
|
|
|
2109 |
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
|
|
|
2110 |
#$result["content"],合併好的一維陣列.
|
|
|
2111 |
#必填參數
|
|
|
2112 |
#$conf["inputArray"],陣列,要合併的一維陣列變數,例如:=array($array1,$array2);
|
|
|
2113 |
$conf["arrays::mergeArray"]["inputArray"]=array(array($result["noEscaped"]["cmd"]),$result["noEscaped"]["argu"]);
|
|
|
2114 |
#可省略參數:
|
|
|
2115 |
#$conf["allowRepeat"],字串,預設為"true",允許重複的結果;若為"false"則不會出現重複的元素內容.
|
|
|
2116 |
#$conf["allowRepeat"]="true";
|
|
|
2117 |
#$conf["looseDiff"],字串,預設為"false",代表要嚴謹判斷為有相異,例如陣列中元素的key順序不同(整數)就代表有相異;反之為"true",例如陣列中元素的key順序不同(非整數),但value有相同,則視為無相異.
|
|
|
2118 |
#$conf["looseDiff"]="false";
|
|
|
2119 |
#參考資料:
|
|
|
2120 |
#無.
|
|
|
2121 |
#備註:
|
|
|
2122 |
#無.
|
|
|
2123 |
$mergeArray=arrays::mergeArray($conf["arrays::mergeArray"]);
|
|
|
2124 |
unset($conf["arrays::mergeArray"]);
|
| 3 |
liveuser |
2125 |
|
| 255 |
liveuser |
2126 |
#如果異常
|
|
|
2127 |
if($mergeArray["status"]=="false"){
|
| 226 |
liveuser |
2128 |
|
| 255 |
liveuser |
2129 |
#設置執行不正常
|
|
|
2130 |
$result["status"]="false";
|
| 226 |
liveuser |
2131 |
|
| 255 |
liveuser |
2132 |
#設置執行錯誤
|
|
|
2133 |
$result["error"]=$mergeArray;
|
| 226 |
liveuser |
2134 |
|
| 255 |
liveuser |
2135 |
#回傳結果
|
|
|
2136 |
return $result;
|
| 226 |
liveuser |
2137 |
|
| 255 |
liveuser |
2138 |
}#if end
|
| 226 |
liveuser |
2139 |
|
| 255 |
liveuser |
2140 |
#取得未escape的指令與參數陣列
|
|
|
2141 |
$result["noEscaped"]["array"]=$mergeArray["content"];
|
| 226 |
liveuser |
2142 |
|
| 3 |
liveuser |
2143 |
#若執行外部式回傳的狀態不為0
|
|
|
2144 |
if($execStatus!==0){
|
| 226 |
liveuser |
2145 |
|
| 3 |
liveuser |
2146 |
#代表執行外部程式不正常.
|
| 226 |
liveuser |
2147 |
|
| 3 |
liveuser |
2148 |
#設置錯誤識別
|
|
|
2149 |
$result["status"]="false";
|
| 226 |
liveuser |
2150 |
|
| 3 |
liveuser |
2151 |
#設錯誤訊息
|
|
|
2152 |
$result["error"][]=$execStatus;
|
|
|
2153 |
$result["error"][]=$resultArray;
|
| 226 |
liveuser |
2154 |
|
| 3 |
liveuser |
2155 |
#設置錯誤訊息
|
|
|
2156 |
$result["error"][]="執行 「".$result["cmd"]."」 命令意外結束!";
|
|
|
2157 |
$result["error"][]="有時可能是因為沒有權限導致的錯誤";
|
|
|
2158 |
$result["error"][]="有時可能是因為指令語法不正確導致的錯誤";
|
| 226 |
liveuser |
2159 |
|
| 3 |
liveuser |
2160 |
#回傳結果
|
|
|
2161 |
return $result;
|
| 226 |
liveuser |
2162 |
|
| 3 |
liveuser |
2163 |
}#if end
|
| 226 |
liveuser |
2164 |
|
| 60 |
liveuser |
2165 |
#初始化完整標準輸出
|
|
|
2166 |
$result["content"]="";
|
| 226 |
liveuser |
2167 |
|
| 60 |
liveuser |
2168 |
#如果有 output
|
|
|
2169 |
if(!empty($result["output"])){
|
| 226 |
liveuser |
2170 |
|
| 60 |
liveuser |
2171 |
#函式說明:
|
|
|
2172 |
#將一維陣列轉換為用特定符號間隔的字串,ex:array("1","2","3") to "a;b;c;".
|
|
|
2173 |
#回傳的結果:
|
|
|
2174 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
2175 |
#$result["function"],當前執行的function名稱
|
|
|
2176 |
#$result["error"],錯誤訊息陣列.
|
|
|
2177 |
#$result["content"],處理好的字串.
|
|
|
2178 |
#$result["argu"],使用的參數.
|
|
|
2179 |
#必填參數:
|
|
|
2180 |
#$conf["inputArray"],字串陣列,要轉成字串的一維陣列.
|
|
|
2181 |
$conf["arrays::arrayToString"]["inputArray"]=$result["output"];
|
|
|
2182 |
#可省略參數:
|
|
|
2183 |
#$conf["spiltSymbol"],字串,用來區隔字串的符號,預設為;
|
|
|
2184 |
$conf["arrays::arrayToString"]["spiltSymbol"]=PHP_EOL;
|
|
|
2185 |
#$conf["skipEnd"],字串,結尾是否不要加上符號,預設為"false",要加上符號,"true"代表不要加上符號。
|
|
|
2186 |
$conf["arrays::arrayToString"]["skipEnd"]="true";
|
|
|
2187 |
#$conf["spiltSymbolAtStart"],字串,是否要在開頭加上spiltSymbol,預設為"false",代表不要;反之為“true”.
|
|
|
2188 |
#$conf["spiltSymbolAtStart"]="";
|
|
|
2189 |
#參考資料:
|
|
|
2190 |
#無.
|
|
|
2191 |
#備註:
|
|
|
2192 |
#無.
|
|
|
2193 |
$arrayToString=arrays::arrayToString($conf["arrays::arrayToString"]);
|
|
|
2194 |
unset($conf["arrays::arrayToString"]);
|
| 226 |
liveuser |
2195 |
|
| 60 |
liveuser |
2196 |
#如果執行失敗
|
|
|
2197 |
if($arrayToString["status"]=="false"){
|
| 226 |
liveuser |
2198 |
|
| 60 |
liveuser |
2199 |
#設置執行不正常
|
|
|
2200 |
$result["status"]="false";
|
| 226 |
liveuser |
2201 |
|
| 60 |
liveuser |
2202 |
#設置執行錯誤
|
|
|
2203 |
$result["error"]=$arrayToString;
|
| 226 |
liveuser |
2204 |
|
| 60 |
liveuser |
2205 |
#回傳結果
|
|
|
2206 |
return $result;
|
| 226 |
liveuser |
2207 |
|
| 60 |
liveuser |
2208 |
}#if end
|
| 226 |
liveuser |
2209 |
|
| 60 |
liveuser |
2210 |
#儲存完整標準輸出的內容
|
|
|
2211 |
$result["content"]=$arrayToString["content"];
|
| 226 |
liveuser |
2212 |
|
| 60 |
liveuser |
2213 |
}#if end
|
| 226 |
liveuser |
2214 |
|
| 3 |
liveuser |
2215 |
#執行到這邊代表執行正常
|
|
|
2216 |
$result["status"]="true";
|
| 226 |
liveuser |
2217 |
|
| 3 |
liveuser |
2218 |
#回傳結果
|
|
|
2219 |
return $result;
|
| 226 |
liveuser |
2220 |
|
| 3 |
liveuser |
2221 |
}#function callShell end
|
| 226 |
liveuser |
2222 |
|
| 3 |
liveuser |
2223 |
/*
|
|
|
2224 |
#函式說明:
|
|
|
2225 |
#呼叫shell執行系統命令,並取得回傳儲存輸出內容的檔案與pid.
|
|
|
2226 |
#回傳結果:
|
|
|
2227 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
2228 |
#$result["error"],錯誤訊息陣列.
|
|
|
2229 |
#$result["function"],當前執行的函數名稱.
|
|
|
2230 |
#$result["cmd"],執行的指令內容.
|
|
|
2231 |
#$result["fullCmd"],執行的完整指令
|
|
|
2232 |
#$result["content"],爲執行完二元碼後的輸出陣列.
|
|
|
2233 |
#$result["tmpFileOutput"],儲存輸出的暫村檔案名稱.
|
|
|
2234 |
#$result["runing"],程式是否還在執行.
|
|
|
2235 |
#$result["pid"],pid.
|
|
|
2236 |
#$result["statusCode"],執行結果狀態碼.
|
|
|
2237 |
#必填參數:
|
|
|
2238 |
#$conf["command"],字串,要執行的指令.
|
|
|
2239 |
$conf["command"]="";
|
|
|
2240 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
| 226 |
liveuser |
2241 |
$conf["fileArgu"]=__FILE__;
|
| 3 |
liveuser |
2242 |
#可省略參數:
|
|
|
2243 |
#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
|
|
|
2244 |
#$conf["doNotRun"]="false";
|
|
|
2245 |
#$conf["leftLogFile"],字串,預設為"false"代表要將 log 檔案移除;"true"代表要保留log檔案.
|
|
|
2246 |
#$conf["leftLogFile"]="true";
|
|
|
2247 |
#備註:
|
|
|
2248 |
#不是所有指令都能用apache的身份執行,目前已知java,javac,ping指令無法執行,透過su使用root身份可能會被selinux阻擋.
|
|
|
2249 |
#如果輸出的內容包含換行符號,將會被過濾掉.
|
|
|
2250 |
#nohup $cmd > $logFilePath 2>&1 & echo $! 會將指令的標準輸入寫入檔案 $logFilePath 並忽略錯誤輸出,將指令在背景執行,僅印出執行緒id.
|
|
|
2251 |
#$cmd > /dev/null 2>&1 &; 會將$cmd放在背景執行,且不會卡住.
|
|
|
2252 |
#參考資料:
|
|
|
2253 |
#http://php.net/manual/en/function.exec.php
|
| 226 |
liveuser |
2254 |
*/
|
| 3 |
liveuser |
2255 |
public static function callShellInBg(&$conf){
|
| 226 |
liveuser |
2256 |
|
| 3 |
liveuser |
2257 |
#初始化要回傳的結果
|
|
|
2258 |
$result=array();
|
|
|
2259 |
|
|
|
2260 |
#取得當前執行的函數名稱
|
|
|
2261 |
$result["function"]=__FUNCTION__;
|
|
|
2262 |
|
|
|
2263 |
#如果沒有參數
|
|
|
2264 |
if(func_num_args()==0){
|
| 226 |
liveuser |
2265 |
|
| 3 |
liveuser |
2266 |
#設置執行失敗
|
|
|
2267 |
$result["status"]="false";
|
| 226 |
liveuser |
2268 |
|
| 3 |
liveuser |
2269 |
#設置執行錯誤訊息
|
|
|
2270 |
$result["error"]="函數".$result["function"]."需要參數";
|
| 226 |
liveuser |
2271 |
|
| 3 |
liveuser |
2272 |
#回傳結果
|
|
|
2273 |
return $result;
|
| 226 |
liveuser |
2274 |
|
| 3 |
liveuser |
2275 |
}#if end
|
| 226 |
liveuser |
2276 |
|
| 3 |
liveuser |
2277 |
#取得參數
|
|
|
2278 |
$result["argu"]=$conf;
|
|
|
2279 |
|
|
|
2280 |
#如果 $conf 不為陣列
|
|
|
2281 |
if(gettype($conf)!=="array"){
|
| 226 |
liveuser |
2282 |
|
| 3 |
liveuser |
2283 |
#設置執行失敗
|
|
|
2284 |
$result["status"]="false";
|
| 226 |
liveuser |
2285 |
|
| 3 |
liveuser |
2286 |
#設置執行錯誤訊息
|
|
|
2287 |
$result["error"][]="\$conf變數須為陣列形態";
|
| 226 |
liveuser |
2288 |
|
| 3 |
liveuser |
2289 |
#如果傳入的參數為 null
|
|
|
2290 |
if($conf===null){
|
| 226 |
liveuser |
2291 |
|
| 3 |
liveuser |
2292 |
#設置執行錯誤訊息
|
|
|
2293 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
| 226 |
liveuser |
2294 |
|
| 3 |
liveuser |
2295 |
}#if end
|
|
|
2296 |
|
|
|
2297 |
#回傳結果
|
|
|
2298 |
return $result;
|
| 226 |
liveuser |
2299 |
|
| 3 |
liveuser |
2300 |
}#if end
|
| 226 |
liveuser |
2301 |
|
| 3 |
liveuser |
2302 |
#檢查參數
|
|
|
2303 |
#函式說明:
|
|
|
2304 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
2305 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
2306 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
2307 |
#$result["function"],當前執行的函式名稱.
|
|
|
2308 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
2309 |
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
2310 |
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
2311 |
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
2312 |
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
|
|
|
2313 |
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
|
|
|
2314 |
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
|
|
|
2315 |
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
|
|
|
2316 |
#必填寫的參數:
|
|
|
2317 |
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
2318 |
$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
|
|
|
2319 |
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
2320 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("command","fileArgu");
|
|
|
2321 |
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
|
|
|
2322 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string");
|
|
|
2323 |
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
2324 |
$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
2325 |
#可以省略的參數:
|
|
|
2326 |
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
|
|
|
2327 |
#$conf["canBeEmptyString"]="false";
|
|
|
2328 |
#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
|
|
|
2329 |
#$conf["canNotBeEmpty"]=array();
|
|
|
2330 |
#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
|
|
|
2331 |
#$conf["canBeEmpty"]=array();
|
|
|
2332 |
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
|
|
|
2333 |
#$conf["skipableVariableCanNotBeEmpty"]=array();
|
|
|
2334 |
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
|
|
2335 |
$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("doNotRun","leftLogFile");
|
| 226 |
liveuser |
2336 |
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
2337 |
$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string");
|
|
|
2338 |
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
|
|
2339 |
$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("false","false");
|
|
|
2340 |
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
2341 |
#$conf["arrayCountEqualCheck"][]=array();
|
|
|
2342 |
#參考資料來源:
|
|
|
2343 |
#array_keys=>http://php.net/manual/en/function.array-keys.php
|
|
|
2344 |
#建議:
|
|
|
2345 |
#增加可省略參數全部不能為空字串或空陣列的參數功能.
|
|
|
2346 |
$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
|
|
|
2347 |
unset($conf["variableCheck::checkArguments"]);
|
| 226 |
liveuser |
2348 |
|
| 3 |
liveuser |
2349 |
#如果檢查參數失敗
|
|
|
2350 |
if($checkArguments["status"]==="false"){
|
| 226 |
liveuser |
2351 |
|
| 3 |
liveuser |
2352 |
#設置執行失敗
|
|
|
2353 |
$result["status"]="false";
|
| 226 |
liveuser |
2354 |
|
| 3 |
liveuser |
2355 |
#設置執行錯誤訊息
|
|
|
2356 |
$result["error"]=$checkArguments;
|
| 226 |
liveuser |
2357 |
|
| 3 |
liveuser |
2358 |
#回傳結果
|
|
|
2359 |
return $result;
|
| 226 |
liveuser |
2360 |
|
| 3 |
liveuser |
2361 |
}#if end
|
| 226 |
liveuser |
2362 |
|
| 3 |
liveuser |
2363 |
#如果參數檢查不過
|
|
|
2364 |
if($checkArguments["passed"]==="false"){
|
| 226 |
liveuser |
2365 |
|
| 3 |
liveuser |
2366 |
#設置執行失敗
|
|
|
2367 |
$result["status"]="false";
|
| 226 |
liveuser |
2368 |
|
| 3 |
liveuser |
2369 |
#設置執行錯誤訊息
|
|
|
2370 |
$result["error"]=$checkArguments;
|
| 226 |
liveuser |
2371 |
|
| 3 |
liveuser |
2372 |
#回傳結果
|
|
|
2373 |
return $result;
|
| 226 |
liveuser |
2374 |
|
| 3 |
liveuser |
2375 |
}#if end
|
| 226 |
liveuser |
2376 |
|
| 3 |
liveuser |
2377 |
#產生暫存檔案
|
|
|
2378 |
#函數說明:
|
| 226 |
liveuser |
2379 |
#建立暫存目錄與回傳暫存檔案名稱路徑
|
| 3 |
liveuser |
2380 |
#回傳結果:
|
|
|
2381 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
2382 |
#$result["error"],錯誤訊息.
|
|
|
2383 |
#$result["function"],當前執行的函數名稱.
|
| 226 |
liveuser |
2384 |
#$result["content"],暫存檔案的路徑與名稱.
|
| 3 |
liveuser |
2385 |
#必填參數:
|
|
|
2386 |
#無
|
|
|
2387 |
#可省略參數:
|
|
|
2388 |
#$conf["tempDir"],字串,暫存目錄的名稱,預設為.fileAccess/createTempFile
|
|
|
2389 |
#$conf["tempDIr"]="";
|
|
|
2390 |
#$conf["fileArgu"],字串,__FILE__的內容,預設為當前檔案的位置.
|
|
|
2391 |
$conf["fileAccess::createTempFile"]["fileArgu"]=$conf["fileArgu"];
|
|
|
2392 |
#$conf["createPath"],字串,是否僅要建立目錄,"true"代表要,"false"代表不要,預設為"true".
|
|
|
2393 |
#$conf["createPath"]="";
|
|
|
2394 |
#$conf["createFile"],字串,是否要在暫存路徑建立好後建立暫存檔案,"true"代表要,"false"代表不用,預設為"true".
|
|
|
2395 |
#$conf["createFile"]="";
|
|
|
2396 |
$createTempFile=fileAccess::createTempFile($conf["fileAccess::createTempFile"]);
|
|
|
2397 |
unset($conf["fileAccess::createTempFile"]);
|
| 226 |
liveuser |
2398 |
|
| 3 |
liveuser |
2399 |
#如果建立暫存檔案失敗
|
|
|
2400 |
if($createTempFile["status"]==="false"){
|
| 226 |
liveuser |
2401 |
|
| 3 |
liveuser |
2402 |
#設置執行失敗
|
|
|
2403 |
$result["status"]="false";
|
| 226 |
liveuser |
2404 |
|
| 3 |
liveuser |
2405 |
#設置執行錯誤訊息
|
|
|
2406 |
$result["error"]=$createTempFile;
|
| 226 |
liveuser |
2407 |
|
| 3 |
liveuser |
2408 |
#回傳結果
|
|
|
2409 |
return $result;
|
| 226 |
liveuser |
2410 |
|
| 3 |
liveuser |
2411 |
}#if end
|
| 226 |
liveuser |
2412 |
|
| 3 |
liveuser |
2413 |
#取得暫存檔案的路徑與名稱
|
|
|
2414 |
$logFilePath=$createTempFile["content"];
|
| 226 |
liveuser |
2415 |
|
| 3 |
liveuser |
2416 |
#執行 commad 並輸出結果到 $logFilePath, 忽略錯誤輸出, 印出pid
|
|
|
2417 |
$command = 'nohup '.$conf["command"].' > '.$logFilePath.' 2>&1 & echo $!';
|
| 226 |
liveuser |
2418 |
|
|
|
2419 |
#$cmd="nohup";
|
| 3 |
liveuser |
2420 |
#$paramsArray=array($conf["command"],">",$logFilePath,"2>&1","&","echo","$!");
|
| 226 |
liveuser |
2421 |
|
| 3 |
liveuser |
2422 |
#如果不用執行指令
|
|
|
2423 |
if($conf["doNotRun"]==="true"){
|
| 226 |
liveuser |
2424 |
|
| 3 |
liveuser |
2425 |
#預設執行結果代碼為0
|
|
|
2426 |
$status=0;
|
| 226 |
liveuser |
2427 |
|
| 3 |
liveuser |
2428 |
#預設pid為0
|
|
|
2429 |
$op=array("0");
|
| 226 |
liveuser |
2430 |
|
| 3 |
liveuser |
2431 |
}#if end
|
| 226 |
liveuser |
2432 |
|
| 3 |
liveuser |
2433 |
#反之
|
|
|
2434 |
else{
|
| 226 |
liveuser |
2435 |
|
| 3 |
liveuser |
2436 |
#執行command, 輸出放到$op
|
|
|
2437 |
exec($command ,$op ,$status);
|
| 226 |
liveuser |
2438 |
|
| 3 |
liveuser |
2439 |
}#else end
|
| 226 |
liveuser |
2440 |
|
| 3 |
liveuser |
2441 |
#取得執行結果狀態碼
|
|
|
2442 |
$result["statusCode"]=$status;
|
| 226 |
liveuser |
2443 |
|
| 3 |
liveuser |
2444 |
#設置執行的完整指令
|
|
|
2445 |
$result["fullCmd"]=$command;
|
| 226 |
liveuser |
2446 |
|
| 3 |
liveuser |
2447 |
#設置執行的指令
|
|
|
2448 |
$result["cmd"]=$conf["command"];
|
| 226 |
liveuser |
2449 |
|
| 3 |
liveuser |
2450 |
#取得pid
|
|
|
2451 |
$result["pid"]=$op[0];
|
| 226 |
liveuser |
2452 |
|
| 3 |
liveuser |
2453 |
#函式說明:
|
|
|
2454 |
#依據行號分隔抓取檔案的內容,結果會回傳一個陣列
|
|
|
2455 |
#回傳的變數說明:
|
|
|
2456 |
#$result["status"],執行是否成功,"true"代表成功;"fasle"代表失敗.
|
|
|
2457 |
#$result["error"],錯誤訊息提示.
|
|
|
2458 |
#$result["warning"],警告訊息.
|
|
|
2459 |
#$result["function"],當前執行的函數名稱.
|
|
|
2460 |
#$result["fileContent"],爲檔案的內容陣列.
|
|
|
2461 |
#$result["lineCount"],爲檔案內容總共的行數.
|
|
|
2462 |
#$result["fullContent"],為檔案的完整內容.
|
|
|
2463 |
#必填參數::
|
|
|
2464 |
#$conf["filePositionAndName"],字串,爲檔案的位置以及名稱.
|
|
|
2465 |
$conf["fileAccess::getFileContent"]["filePositionAndName"]=$logFilePath;
|
|
|
2466 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
|
|
|
2467 |
$conf["fileAccess::getFileContent"]["fileArgu"]=$conf["fileArgu"];
|
|
|
2468 |
#可省略參數:
|
|
|
2469 |
#$conf["web"],是要取得網路上的檔案則為"true";反之則為"false".
|
|
|
2470 |
$conf["fileAccess::getFileContent"]["web"]="false";
|
|
|
2471 |
#參考資料:
|
|
|
2472 |
#file(),取得檔案內容的行數.
|
|
|
2473 |
#file=>http:#php.net/manual/en/function.file.php
|
|
|
2474 |
#rtrim(),剔除透過file()取得每行內容結尾的換行符號.
|
|
|
2475 |
#filesize=>http://php.net/manual/en/function.filesize.php
|
|
|
2476 |
$getFileContent=fileAccess::getFileContent($conf["fileAccess::getFileContent"]);
|
| 226 |
liveuser |
2477 |
unset($conf["fileAccess::getFileContent"]);
|
|
|
2478 |
|
| 3 |
liveuser |
2479 |
#如果讀取檔案失敗
|
|
|
2480 |
if($getFileContent["status"]==="false"){
|
| 226 |
liveuser |
2481 |
|
| 3 |
liveuser |
2482 |
#設置執行失敗
|
|
|
2483 |
$result["status"]="false";
|
| 226 |
liveuser |
2484 |
|
| 3 |
liveuser |
2485 |
#設置執行錯誤訊息
|
|
|
2486 |
$result["error"]=$getFileContent;
|
| 226 |
liveuser |
2487 |
|
| 3 |
liveuser |
2488 |
#回傳結果
|
|
|
2489 |
return $result;
|
| 226 |
liveuser |
2490 |
|
| 3 |
liveuser |
2491 |
}#if end
|
| 226 |
liveuser |
2492 |
|
| 3 |
liveuser |
2493 |
#取得輸出內容
|
|
|
2494 |
$result["content"]=$getFileContent["fileContent"];
|
| 226 |
liveuser |
2495 |
|
| 3 |
liveuser |
2496 |
#取得暫存檔案路徑與名稱
|
|
|
2497 |
$result["tmpFileOutput"]=$logFilePath;
|
| 226 |
liveuser |
2498 |
|
| 3 |
liveuser |
2499 |
#如果沒有要保留 log 檔案
|
|
|
2500 |
if($conf["leftLogFile"]==="false"){
|
| 226 |
liveuser |
2501 |
|
| 3 |
liveuser |
2502 |
#函式說明:
|
|
|
2503 |
#移除檔案
|
|
|
2504 |
#回傳結果:
|
|
|
2505 |
#$result["status"],"true"代表移除成功,"false"代表移除失敗.
|
|
|
2506 |
#$result["error"],錯誤訊息陣列.
|
|
|
2507 |
#$result["warning"],警告訊息陣列.
|
|
|
2508 |
#$result["function"],當前執行的函數名稱.
|
|
|
2509 |
#$result["argu"],當前函式使用的參數.
|
|
|
2510 |
#必填參數:
|
|
|
2511 |
#$conf["fileAddress"],字串,要移除檔案的位置.
|
|
|
2512 |
$conf["fileAccess::delFile"]["fileAddress"]=$logFilePath;
|
|
|
2513 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.
|
|
|
2514 |
$conf["fileAccess::delFile"]["fileArgu"]=$conf["fileArgu"];
|
|
|
2515 |
#可省略參數:
|
|
|
2516 |
#$conf["commentsArray"],字串陣列,提示的文字描述,$conf["commentsArray"][$i]代表第($+1)行的描述.
|
|
|
2517 |
#$conf["commentsArray"]=array("");
|
|
|
2518 |
#$conf["allowDelSymlink"],字串,預設為"false",不移除軟連結;"true"代表要移除軟連結.
|
|
|
2519 |
#$conf["allowDelSymlink"]="true";
|
|
|
2520 |
#$conf["allowDelFolder"],字串,預設為"false",不移除目錄;"true"代表要移除目錄.
|
|
|
2521 |
#$conf["allowDelFolder"]="true";
|
|
|
2522 |
#參考資料:
|
|
|
2523 |
#無.
|
|
|
2524 |
#備註:
|
|
|
2525 |
#無.
|
|
|
2526 |
$delFile=fileAccess::delFile($conf["fileAccess::delFile"]);
|
|
|
2527 |
unset($conf["fileAccess::delFile"]);
|
| 226 |
liveuser |
2528 |
|
| 3 |
liveuser |
2529 |
#如果執行失敗
|
|
|
2530 |
if($delFile["status"]==="false"){
|
| 226 |
liveuser |
2531 |
|
| 3 |
liveuser |
2532 |
#設置執行失敗
|
|
|
2533 |
$result["status"]="false";
|
| 226 |
liveuser |
2534 |
|
| 3 |
liveuser |
2535 |
#設置執行錯誤訊息
|
|
|
2536 |
$result["error"]=$delFile;
|
| 226 |
liveuser |
2537 |
|
| 3 |
liveuser |
2538 |
#回傳結果
|
|
|
2539 |
return $result;
|
| 226 |
liveuser |
2540 |
|
| 3 |
liveuser |
2541 |
}#if end
|
| 226 |
liveuser |
2542 |
|
| 3 |
liveuser |
2543 |
}#if end
|
| 226 |
liveuser |
2544 |
|
| 3 |
liveuser |
2545 |
#函式說明:
|
| 226 |
liveuser |
2546 |
#檢查程序是否還在執行
|
| 3 |
liveuser |
2547 |
#回傳結果
|
|
|
2548 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
2549 |
#$result["error"],錯誤訊息陣列.
|
| 226 |
liveuser |
2550 |
#$result["function"],當前執行的函數名稱.
|
| 3 |
liveuser |
2551 |
#$result["exist"],程序是否存在,"true"代表存在,"false"代表不存在.
|
|
|
2552 |
#必填參數:
|
|
|
2553 |
#$conf["pid"],字串,程序的id.
|
|
|
2554 |
$conf["external::processStatus"]["pid"]=$result["pid"];
|
|
|
2555 |
#$conf["cmd"],字串,程序的指令.
|
|
|
2556 |
$conf["external::processStatus"]["cmd"]=$result["cmd"];
|
|
|
2557 |
#$conf["fileArgu"],字串,__FILE__的內容,__FILE__的內容.
|
|
|
2558 |
$conf["external::processStatus"]["fileArgu"]=$conf["fileArgu"];
|
|
|
2559 |
$processStatus=external::processStatus($conf["external::processStatus"]);
|
|
|
2560 |
unset($conf["external::processStatus"]);
|
| 226 |
liveuser |
2561 |
|
| 3 |
liveuser |
2562 |
#如果查詢pid是否存在失敗
|
|
|
2563 |
if($processStatus["status"]==="false"){
|
| 226 |
liveuser |
2564 |
|
| 3 |
liveuser |
2565 |
#設置執行失敗
|
|
|
2566 |
$result["status"]="false";
|
| 226 |
liveuser |
2567 |
|
| 3 |
liveuser |
2568 |
#設置執行錯誤訊息
|
|
|
2569 |
$result["error"]=$processStatus;
|
| 226 |
liveuser |
2570 |
|
| 3 |
liveuser |
2571 |
#回傳結果
|
|
|
2572 |
return $result;
|
| 226 |
liveuser |
2573 |
|
| 3 |
liveuser |
2574 |
}#if end
|
| 226 |
liveuser |
2575 |
|
| 3 |
liveuser |
2576 |
#取得程序是否還在執行
|
|
|
2577 |
$result["running"]=$processStatus["exist"];
|
| 226 |
liveuser |
2578 |
|
| 3 |
liveuser |
2579 |
#設置執行正常
|
|
|
2580 |
$result["status"]="true";
|
| 226 |
liveuser |
2581 |
|
| 3 |
liveuser |
2582 |
#回傳結果
|
|
|
2583 |
return $result;
|
| 226 |
liveuser |
2584 |
|
| 3 |
liveuser |
2585 |
}#function callShellInBg end
|
| 226 |
liveuser |
2586 |
|
| 3 |
liveuser |
2587 |
/*
|
|
|
2588 |
#函式說明:
|
|
|
2589 |
#呼叫shell_exec執行系統命令,並取得回傳儲存輸出內容的檔案與pid.
|
|
|
2590 |
#回傳結果:
|
|
|
2591 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
2592 |
#$result["error"],錯誤訊息陣列.
|
|
|
2593 |
#$result["function"],當前執行的函數名稱.
|
|
|
2594 |
#$result["cmd"],執行的指令內容.
|
|
|
2595 |
#$result["fullCmd"],執行的完整指令
|
|
|
2596 |
#$result["content"],爲執行完二元碼後的輸出結果,會儲存成字串,包含換行符號.
|
|
|
2597 |
#$result["runing"],程式是否還在執行.
|
|
|
2598 |
#$result["pid"],pid.
|
|
|
2599 |
#必填參數:
|
|
|
2600 |
#$conf["command"],字串,要執行的指令.
|
|
|
2601 |
$conf["command"]="";
|
|
|
2602 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
| 226 |
liveuser |
2603 |
$conf["fileArgu"]=__FILE__;
|
| 3 |
liveuser |
2604 |
#可省略參數:
|
|
|
2605 |
#無.
|
|
|
2606 |
#參考資料:
|
|
|
2607 |
#http://php.net/manual/en/function.exec.php
|
|
|
2608 |
#備註:
|
|
|
2609 |
#不是所有指令都能用apache的身份執行,目前已知java,javac,ping指令無法執行,透過su使用root身份可能會被selinux阻擋.
|
| 226 |
liveuser |
2610 |
*/
|
| 3 |
liveuser |
2611 |
public static function callShellExecInBg(&$conf){
|
| 226 |
liveuser |
2612 |
|
| 3 |
liveuser |
2613 |
#初始化要回傳的結果
|
|
|
2614 |
$result=array();
|
|
|
2615 |
|
|
|
2616 |
#取得當前執行的函數名稱
|
|
|
2617 |
$result["function"]=__FUNCTION__;
|
|
|
2618 |
|
|
|
2619 |
#如果沒有參數
|
|
|
2620 |
if(func_num_args()==0){
|
| 226 |
liveuser |
2621 |
|
| 3 |
liveuser |
2622 |
#設置執行失敗
|
|
|
2623 |
$result["status"]="false";
|
| 226 |
liveuser |
2624 |
|
| 3 |
liveuser |
2625 |
#設置執行錯誤訊息
|
|
|
2626 |
$result["error"]="函數".$result["function"]."需要參數";
|
| 226 |
liveuser |
2627 |
|
| 3 |
liveuser |
2628 |
#回傳結果
|
|
|
2629 |
return $result;
|
| 226 |
liveuser |
2630 |
|
| 3 |
liveuser |
2631 |
}#if end
|
| 226 |
liveuser |
2632 |
|
| 3 |
liveuser |
2633 |
#取得參數
|
|
|
2634 |
$result["argu"]=$conf;
|
|
|
2635 |
|
|
|
2636 |
#如果 $conf 不為陣列
|
|
|
2637 |
if(gettype($conf)!=="array"){
|
| 226 |
liveuser |
2638 |
|
| 3 |
liveuser |
2639 |
#設置執行失敗
|
|
|
2640 |
$result["status"]="false";
|
| 226 |
liveuser |
2641 |
|
| 3 |
liveuser |
2642 |
#設置執行錯誤訊息
|
|
|
2643 |
$result["error"][]="\$conf變數須為陣列形態";
|
| 226 |
liveuser |
2644 |
|
| 3 |
liveuser |
2645 |
#如果傳入的參數為 null
|
|
|
2646 |
if($conf===null){
|
| 226 |
liveuser |
2647 |
|
| 3 |
liveuser |
2648 |
#設置執行錯誤訊息
|
|
|
2649 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
| 226 |
liveuser |
2650 |
|
| 3 |
liveuser |
2651 |
}#if end
|
|
|
2652 |
|
|
|
2653 |
#回傳結果
|
|
|
2654 |
return $result;
|
| 226 |
liveuser |
2655 |
|
| 3 |
liveuser |
2656 |
}#if end
|
| 226 |
liveuser |
2657 |
|
| 3 |
liveuser |
2658 |
#檢查參數
|
|
|
2659 |
#函式說明:
|
|
|
2660 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
2661 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
2662 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
2663 |
#$result["function"],當前執行的函式名稱.
|
|
|
2664 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
2665 |
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
2666 |
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
2667 |
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
2668 |
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
|
|
|
2669 |
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
|
|
|
2670 |
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
|
|
|
2671 |
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
|
|
|
2672 |
#必填寫的參數:
|
|
|
2673 |
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
2674 |
$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
|
|
|
2675 |
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
2676 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("command","fileArgu");
|
|
|
2677 |
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
|
|
|
2678 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string");
|
|
|
2679 |
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
2680 |
$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
2681 |
#可以省略的參數:
|
|
|
2682 |
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
|
|
|
2683 |
#$conf["canBeEmptyString"]="false";
|
|
|
2684 |
#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
|
|
|
2685 |
#$conf["canNotBeEmpty"]=array();
|
|
|
2686 |
#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
|
|
|
2687 |
#$conf["canBeEmpty"]=array();
|
|
|
2688 |
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
|
|
|
2689 |
#$conf["skipableVariableCanNotBeEmpty"]=array();
|
|
|
2690 |
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
|
|
2691 |
#$conf["skipableVariableName"]=array();
|
| 226 |
liveuser |
2692 |
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
2693 |
#$conf["skipableVariableType"]=array();
|
|
|
2694 |
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
|
|
2695 |
#$conf["skipableVarDefaultValue"]=array("");
|
|
|
2696 |
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
2697 |
#$conf["arrayCountEqualCheck"][]=array();
|
|
|
2698 |
#參考資料來源:
|
|
|
2699 |
#array_keys=>http://php.net/manual/en/function.array-keys.php
|
|
|
2700 |
#建議:
|
|
|
2701 |
#增加可省略參數全部不能為空字串或空陣列的參數功能.
|
|
|
2702 |
$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
|
|
|
2703 |
unset($conf["variableCheck::checkArguments"]);
|
| 226 |
liveuser |
2704 |
|
| 3 |
liveuser |
2705 |
#如果檢查參數失敗
|
|
|
2706 |
if($checkArguments["status"]==="false"){
|
| 226 |
liveuser |
2707 |
|
| 3 |
liveuser |
2708 |
#設置執行失敗
|
|
|
2709 |
$result["status"]="false";
|
| 226 |
liveuser |
2710 |
|
| 3 |
liveuser |
2711 |
#設置執行錯誤訊息
|
|
|
2712 |
$result["error"]=$checkArguments;
|
| 226 |
liveuser |
2713 |
|
| 3 |
liveuser |
2714 |
#回傳結果
|
|
|
2715 |
return $result;
|
| 226 |
liveuser |
2716 |
|
| 3 |
liveuser |
2717 |
}#if end
|
| 226 |
liveuser |
2718 |
|
| 3 |
liveuser |
2719 |
#如果參數檢查不過
|
|
|
2720 |
if($checkArguments["passed"]==="false"){
|
| 226 |
liveuser |
2721 |
|
| 3 |
liveuser |
2722 |
#設置執行失敗
|
|
|
2723 |
$result["status"]="false";
|
| 226 |
liveuser |
2724 |
|
| 3 |
liveuser |
2725 |
#設置執行錯誤訊息
|
|
|
2726 |
$result["error"]=$checkArguments;
|
| 226 |
liveuser |
2727 |
|
| 3 |
liveuser |
2728 |
#回傳結果
|
|
|
2729 |
return $result;
|
| 226 |
liveuser |
2730 |
|
| 3 |
liveuser |
2731 |
}#if end
|
| 226 |
liveuser |
2732 |
|
| 3 |
liveuser |
2733 |
#產生暫存檔案
|
|
|
2734 |
#函數說明:
|
| 226 |
liveuser |
2735 |
#建立暫存目錄與回傳暫存檔案名稱路徑
|
| 3 |
liveuser |
2736 |
#回傳結果:
|
|
|
2737 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
2738 |
#$result["error"],錯誤訊息.
|
|
|
2739 |
#$result["function"],當前執行的函數名稱.
|
| 226 |
liveuser |
2740 |
#$result["content"],暫存檔案的路徑與名稱.
|
| 3 |
liveuser |
2741 |
#必填參數:
|
|
|
2742 |
#無
|
|
|
2743 |
#可省略參數:
|
|
|
2744 |
#$conf["tempDir"],字串,暫存目錄的名稱,預設為.fileAccess/createTempFile
|
|
|
2745 |
#$conf["tempDIr"]="";
|
|
|
2746 |
#$conf["fileArgu"],字串,__FILE__的內容,預設為當前檔案的位置.
|
|
|
2747 |
$conf["fileAccess::createTempFile"]["fileArgu"]=$conf["fileArgu"];
|
|
|
2748 |
#$conf["createPath"],字串,是否僅要建立目錄,"true"代表要,"false"代表不要,預設為"true".
|
|
|
2749 |
#$conf["createPath"]="";
|
|
|
2750 |
#$conf["createFile"],字串,是否要在暫存路徑建立好後建立暫存檔案,"true"代表要,"false"代表不用,預設為"true".
|
|
|
2751 |
#$conf["createFile"]="";
|
|
|
2752 |
$createTempFile=fileAccess::createTempFile($conf["fileAccess::createTempFile"]);
|
|
|
2753 |
unset($conf["fileAccess::createTempFile"]);
|
| 226 |
liveuser |
2754 |
|
| 3 |
liveuser |
2755 |
#如果建立暫存檔案失敗
|
|
|
2756 |
if($createTempFile["status"]==="false"){
|
| 226 |
liveuser |
2757 |
|
| 3 |
liveuser |
2758 |
#設置執行失敗
|
|
|
2759 |
$result["status"]="false";
|
| 226 |
liveuser |
2760 |
|
| 3 |
liveuser |
2761 |
#設置執行錯誤訊息
|
|
|
2762 |
$result["error"]=$createTempFile;
|
| 226 |
liveuser |
2763 |
|
| 3 |
liveuser |
2764 |
#回傳結果
|
|
|
2765 |
return $result;
|
| 226 |
liveuser |
2766 |
|
| 3 |
liveuser |
2767 |
}#if end
|
| 226 |
liveuser |
2768 |
|
| 3 |
liveuser |
2769 |
#取得暫存檔案的路徑與名稱
|
|
|
2770 |
$logFilePath=$createTempFile["content"];
|
| 226 |
liveuser |
2771 |
|
| 3 |
liveuser |
2772 |
#執行 commad 並輸出結果到 "/tmp/externel.Process", 忽略錯誤輸出, 印出pid
|
|
|
2773 |
$command = 'nohup '.$conf["command"].' > '.$logFilePath.' 2>&1 & echo $!';
|
| 226 |
liveuser |
2774 |
|
| 3 |
liveuser |
2775 |
#執行command, 輸出放到$op
|
|
|
2776 |
$op=shell_exec($command);
|
| 226 |
liveuser |
2777 |
|
| 3 |
liveuser |
2778 |
#設置執行的完整指令
|
|
|
2779 |
$result["fullCmd"]=$command;
|
| 226 |
liveuser |
2780 |
|
| 3 |
liveuser |
2781 |
#設置執行的指令
|
|
|
2782 |
$result["cmd"]=$conf["command"];
|
| 226 |
liveuser |
2783 |
|
| 3 |
liveuser |
2784 |
#取得pid
|
|
|
2785 |
$result["pid"]=$op;
|
| 226 |
liveuser |
2786 |
|
| 3 |
liveuser |
2787 |
#函式說明:
|
|
|
2788 |
#依據行號分隔抓取檔案的內容,結果會回傳一個陣列
|
|
|
2789 |
#回傳的變數說明:
|
|
|
2790 |
#$result["status"],執行是否成功,"true"代表成功;"fasle"代表失敗.
|
|
|
2791 |
#$result["error"],錯誤訊息提示.
|
|
|
2792 |
#$result["warning"],警告訊息.
|
|
|
2793 |
#$result["function"],當前執行的函數名稱.
|
|
|
2794 |
#$result["fileContent"],爲檔案的內容陣列.
|
|
|
2795 |
#$result["lineCount"],爲檔案內容總共的行數.
|
|
|
2796 |
#$result["fullContent"],為檔案的完整內容.
|
|
|
2797 |
#必填參數::
|
|
|
2798 |
#$conf["filePositionAndName"],字串,爲檔案的位置以及名稱.
|
|
|
2799 |
$conf["fileAccess::getFileContent"]["filePositionAndName"]=$logFilePath;
|
|
|
2800 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
|
|
|
2801 |
$conf["fileAccess::getFileContent"]["fileArgu"]=$conf["fileArgu"];
|
|
|
2802 |
#參考資料:
|
|
|
2803 |
#file(),取得檔案內容的行數.
|
|
|
2804 |
#file=>http:#php.net/manual/en/function.file.php
|
|
|
2805 |
#rtrim(),剔除透過file()取得每行內容結尾的換行符號.
|
|
|
2806 |
#filesize=>http://php.net/manual/en/function.filesize.php
|
|
|
2807 |
$getFileContent=fileAccess::getFileContent($conf["fileAccess::getFileContent"]);
|
| 226 |
liveuser |
2808 |
unset($conf["fileAccess::getFileContent"]);
|
|
|
2809 |
|
| 3 |
liveuser |
2810 |
#如果讀取檔案失敗
|
|
|
2811 |
if($getFileContent["status"]==="false"){
|
| 226 |
liveuser |
2812 |
|
| 3 |
liveuser |
2813 |
#設置執行失敗
|
|
|
2814 |
$result["status"]="false";
|
| 226 |
liveuser |
2815 |
|
| 3 |
liveuser |
2816 |
#設置執行錯誤訊息
|
|
|
2817 |
$result["error"]=$getFileContent;
|
| 226 |
liveuser |
2818 |
|
| 3 |
liveuser |
2819 |
#回傳結果
|
|
|
2820 |
return $result;
|
| 226 |
liveuser |
2821 |
|
| 3 |
liveuser |
2822 |
}#if end
|
| 226 |
liveuser |
2823 |
|
| 3 |
liveuser |
2824 |
#取得輸出內容
|
|
|
2825 |
$result["content"]=$getFileContent["fileContent"];
|
| 226 |
liveuser |
2826 |
|
| 3 |
liveuser |
2827 |
#取得暫存檔案路徑與名稱
|
|
|
2828 |
$result["tmpFileOutput"]=$logFilePath;
|
| 226 |
liveuser |
2829 |
|
| 3 |
liveuser |
2830 |
#函式說明:
|
| 226 |
liveuser |
2831 |
#檢查程序是否還在執行
|
| 3 |
liveuser |
2832 |
#回傳結果
|
|
|
2833 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
2834 |
#$result["error"],錯誤訊息陣列.
|
| 226 |
liveuser |
2835 |
#$result["function"],當前執行的函數名稱.
|
| 3 |
liveuser |
2836 |
#$result["exist"],程序是否存在,"true"代表存在,"false"代表不存在.
|
|
|
2837 |
#必填參數:
|
|
|
2838 |
#$conf["pid"],字串,程序的id.
|
|
|
2839 |
$conf["external::processStatus"]["pid"]=$result["pid"];
|
|
|
2840 |
#$conf["cmd"],字串,程序的指令.
|
|
|
2841 |
$conf["external::processStatus"]["cmd"]=$result["cmd"];
|
|
|
2842 |
#$conf["fileArgu"],字串,__FILE__的內容,__FILE__的內容.
|
|
|
2843 |
$conf["external::processStatus"]["fileArgu"]=$conf["fileArgu"];
|
|
|
2844 |
$processStatus=external::processStatus($conf["external::processStatus"]);
|
|
|
2845 |
unset($conf["external::processStatus"]);
|
| 226 |
liveuser |
2846 |
|
| 3 |
liveuser |
2847 |
#如果查詢pid是否存在失敗
|
|
|
2848 |
if($processStatus["status"]==="false"){
|
| 226 |
liveuser |
2849 |
|
| 3 |
liveuser |
2850 |
#設置執行失敗
|
|
|
2851 |
$result["status"]="false";
|
| 226 |
liveuser |
2852 |
|
| 3 |
liveuser |
2853 |
#設置執行錯誤訊息
|
|
|
2854 |
$result["error"]=$processStatus;
|
| 226 |
liveuser |
2855 |
|
| 3 |
liveuser |
2856 |
#回傳結果
|
|
|
2857 |
return $result;
|
| 226 |
liveuser |
2858 |
|
| 3 |
liveuser |
2859 |
}#if end
|
| 226 |
liveuser |
2860 |
|
| 3 |
liveuser |
2861 |
#取得程序是否還在執行
|
|
|
2862 |
$result["runing"]=$processStatus["exist"];
|
| 226 |
liveuser |
2863 |
|
| 3 |
liveuser |
2864 |
#設置執行正常
|
|
|
2865 |
$result["status"]="true";
|
| 226 |
liveuser |
2866 |
|
| 3 |
liveuser |
2867 |
#回傳結果
|
|
|
2868 |
return $result;
|
| 226 |
liveuser |
2869 |
|
| 66 |
liveuser |
2870 |
}#function callShellExecInBg end
|
| 226 |
liveuser |
2871 |
|
| 3 |
liveuser |
2872 |
/*
|
|
|
2873 |
#函式說明:
|
| 226 |
liveuser |
2874 |
#檢查程序是否還在執行
|
| 3 |
liveuser |
2875 |
#回傳結果:
|
|
|
2876 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
2877 |
#$result["error"],錯誤訊息陣列.
|
| 226 |
liveuser |
2878 |
#$result["function"],當前執行的函數名稱.
|
| 3 |
liveuser |
2879 |
#$result["exist"],程序是否存在,"true"代表存在,"false"代表不存在.
|
|
|
2880 |
#必填參數:
|
|
|
2881 |
#$conf["pid"],字串,程序的id.
|
|
|
2882 |
$conf["pid"]="";
|
|
|
2883 |
#$conf["cmd"],字串,程序的指令.
|
|
|
2884 |
$conf["cmd"]=""
|
|
|
2885 |
#$conf["fileArgu"],字串,__FILE__的內容,__FILE__的內容.
|
|
|
2886 |
$conf["fileArgu"]=__FILE__;
|
|
|
2887 |
#可省略參數:
|
|
|
2888 |
#無.
|
|
|
2889 |
#參考資料:
|
|
|
2890 |
#無.
|
|
|
2891 |
#備註:
|
|
|
2892 |
#無.
|
| 226 |
liveuser |
2893 |
*/
|
| 3 |
liveuser |
2894 |
public static function processStatus(&$conf){
|
| 226 |
liveuser |
2895 |
|
| 3 |
liveuser |
2896 |
#初始化要回傳的結果
|
|
|
2897 |
$result=array();
|
|
|
2898 |
|
|
|
2899 |
#取得當前執行的函數名稱
|
|
|
2900 |
$result["function"]=__FUNCTION__;
|
|
|
2901 |
|
|
|
2902 |
#如果沒有參數
|
|
|
2903 |
if(func_num_args()==0){
|
| 226 |
liveuser |
2904 |
|
| 3 |
liveuser |
2905 |
#設置執行失敗
|
|
|
2906 |
$result["status"]="false";
|
| 226 |
liveuser |
2907 |
|
| 3 |
liveuser |
2908 |
#設置執行錯誤訊息
|
|
|
2909 |
$result["error"]="函數".$result["function"]."需要參數";
|
| 226 |
liveuser |
2910 |
|
| 3 |
liveuser |
2911 |
#回傳結果
|
|
|
2912 |
return $result;
|
| 226 |
liveuser |
2913 |
|
| 3 |
liveuser |
2914 |
}#if end
|
| 226 |
liveuser |
2915 |
|
| 3 |
liveuser |
2916 |
#取得參數
|
|
|
2917 |
$result["argu"]=$conf;
|
|
|
2918 |
|
|
|
2919 |
#如果 $conf 不為陣列
|
|
|
2920 |
if(gettype($conf)!=="array"){
|
| 226 |
liveuser |
2921 |
|
| 3 |
liveuser |
2922 |
#設置執行失敗
|
|
|
2923 |
$result["status"]="false";
|
| 226 |
liveuser |
2924 |
|
| 3 |
liveuser |
2925 |
#設置執行錯誤訊息
|
|
|
2926 |
$result["error"][]="\$conf變數須為陣列形態";
|
| 226 |
liveuser |
2927 |
|
| 3 |
liveuser |
2928 |
#如果傳入的參數為 null
|
|
|
2929 |
if($conf===null){
|
| 226 |
liveuser |
2930 |
|
| 3 |
liveuser |
2931 |
#設置執行錯誤訊息
|
|
|
2932 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
| 226 |
liveuser |
2933 |
|
| 3 |
liveuser |
2934 |
}#if end
|
|
|
2935 |
|
|
|
2936 |
#回傳結果
|
|
|
2937 |
return $result;
|
| 226 |
liveuser |
2938 |
|
| 3 |
liveuser |
2939 |
}#if end
|
| 226 |
liveuser |
2940 |
|
| 3 |
liveuser |
2941 |
#檢查參數
|
|
|
2942 |
#函式說明:
|
|
|
2943 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
2944 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
2945 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
2946 |
#$result["function"],當前執行的函式名稱.
|
|
|
2947 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
2948 |
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
2949 |
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
2950 |
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
2951 |
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
|
|
|
2952 |
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
|
|
|
2953 |
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
|
|
|
2954 |
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
|
|
|
2955 |
#必填寫的參數:
|
|
|
2956 |
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
2957 |
$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
|
|
|
2958 |
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
2959 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("pid","cmd","fileArgu");
|
|
|
2960 |
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
|
|
|
2961 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string","string");
|
|
|
2962 |
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
2963 |
$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
2964 |
#可以省略的參數:
|
|
|
2965 |
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
|
|
|
2966 |
#$conf["canBeEmptyString"]="false";
|
|
|
2967 |
#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
|
|
|
2968 |
#$conf["canNotBeEmpty"]=array();
|
|
|
2969 |
#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
|
|
|
2970 |
#$conf["canBeEmpty"]=array();
|
|
|
2971 |
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
|
|
|
2972 |
#$conf["skipableVariableCanNotBeEmpty"]=array();
|
|
|
2973 |
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
|
|
2974 |
#$conf["skipableVariableName"]=array();
|
| 226 |
liveuser |
2975 |
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
2976 |
#$conf["skipableVariableType"]=array();
|
|
|
2977 |
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
|
|
2978 |
#$conf["skipableVarDefaultValue"]=array("");
|
|
|
2979 |
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
2980 |
#$conf["arrayCountEqualCheck"][]=array();
|
|
|
2981 |
#參考資料來源:
|
|
|
2982 |
#array_keys=>http://php.net/manual/en/function.array-keys.php
|
|
|
2983 |
#建議:
|
|
|
2984 |
#增加可省略參數全部不能為空字串或空陣列的參數功能.
|
|
|
2985 |
$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
|
|
|
2986 |
unset($conf["variableCheck::checkArguments"]);
|
| 226 |
liveuser |
2987 |
|
| 3 |
liveuser |
2988 |
#如果檢查參數失敗
|
|
|
2989 |
if($checkArguments["status"]==="false"){
|
| 226 |
liveuser |
2990 |
|
| 3 |
liveuser |
2991 |
#設置執行失敗
|
|
|
2992 |
$result["status"]="false";
|
| 226 |
liveuser |
2993 |
|
| 3 |
liveuser |
2994 |
#設置執行錯誤訊息
|
|
|
2995 |
$result["error"]=$checkArguments;
|
| 226 |
liveuser |
2996 |
|
| 3 |
liveuser |
2997 |
#回傳結果
|
|
|
2998 |
return $result;
|
| 226 |
liveuser |
2999 |
|
| 3 |
liveuser |
3000 |
}#if end
|
| 226 |
liveuser |
3001 |
|
| 3 |
liveuser |
3002 |
#如果參數檢查不過
|
|
|
3003 |
if($checkArguments["passed"]==="false"){
|
| 226 |
liveuser |
3004 |
|
| 3 |
liveuser |
3005 |
#設置執行失敗
|
|
|
3006 |
$result["status"]="false";
|
| 226 |
liveuser |
3007 |
|
| 3 |
liveuser |
3008 |
#設置執行錯誤訊息
|
|
|
3009 |
$result["error"]=$checkArguments;
|
| 226 |
liveuser |
3010 |
|
| 3 |
liveuser |
3011 |
#回傳結果
|
|
|
3012 |
return $result;
|
| 226 |
liveuser |
3013 |
|
| 3 |
liveuser |
3014 |
}#if end
|
| 226 |
liveuser |
3015 |
|
| 3 |
liveuser |
3016 |
#產生搜尋process的指令
|
| 226 |
liveuser |
3017 |
#執行檢查程序是否依然存在
|
| 3 |
liveuser |
3018 |
#函式說明:
|
|
|
3019 |
#呼叫shell執行系統命令,並取得回傳的內容.
|
|
|
3020 |
#回傳結果:
|
|
|
3021 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
3022 |
#$result["error"],錯誤訊息陣列.
|
|
|
3023 |
#$result["function"],當前執行的函數名稱.
|
|
|
3024 |
#$result["argu"],使用的參數.
|
|
|
3025 |
#$result["cmd"],執行的指令內容.
|
|
|
3026 |
#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
3027 |
#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
|
|
|
3028 |
#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
3029 |
#$result["running"],是否還在執行.
|
|
|
3030 |
#$result["pid"],pid.
|
|
|
3031 |
#$result["statusCode"],執行結束後的代碼.
|
|
|
3032 |
#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu").
|
|
|
3033 |
#必填參數:
|
|
|
3034 |
#$conf["command"],字串,要執行的指令與.
|
|
|
3035 |
$conf["external::callShell"]["command"]="ps";
|
|
|
3036 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
| 226 |
liveuser |
3037 |
$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];
|
| 3 |
liveuser |
3038 |
#可省略參數:
|
|
|
3039 |
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
|
|
|
3040 |
$conf["external::callShell"]["argu"]=array("-aux","|","cat","|","grep",$conf["cmd"],"|","grep",$conf["pid"]);
|
|
|
3041 |
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
|
|
|
3042 |
#$conf["arguIsAddr"]=array();
|
|
|
3043 |
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
|
|
|
3044 |
#$conf["enablePrintDescription"]="true";
|
|
|
3045 |
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容.
|
|
|
3046 |
#$conf["printDescription"]="";
|
|
|
3047 |
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".
|
|
|
3048 |
$conf["external::callShell"]["escapeshellarg"]="true";
|
|
|
3049 |
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
|
|
|
3050 |
#$conf["username"]="";
|
|
|
3051 |
#$conf["password"],字串,與$conf["username"]搭配的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
|
|
3052 |
#$conf["password"]="";
|
|
|
3053 |
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
|
|
|
3054 |
#$conf["useScript"]="";
|
|
|
3055 |
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
|
|
|
3056 |
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
|
|
|
3057 |
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
|
|
|
3058 |
#$conf["inBackGround"]="";
|
|
|
3059 |
#備註:
|
|
|
3060 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
|
|
|
3061 |
#參考資料:
|
|
|
3062 |
#exec=>http://php.net/manual/en/function.exec.php
|
|
|
3063 |
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
|
|
|
3064 |
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
|
|
|
3065 |
$callShell=external::callShell($conf["external::callShell"]);
|
|
|
3066 |
unset($conf["external::callShell"]);
|
| 226 |
liveuser |
3067 |
|
| 3 |
liveuser |
3068 |
#如果執行程式失敗,且不是因為沒有抓取到內容的錯誤.
|
|
|
3069 |
if($callShell["status"]==="false" && $callShell["statusCode"]!==1 && $callShell["output"]!==array() ){
|
| 226 |
liveuser |
3070 |
|
| 3 |
liveuser |
3071 |
#設置執行失敗
|
|
|
3072 |
$result["status"]="false";
|
| 226 |
liveuser |
3073 |
|
| 3 |
liveuser |
3074 |
#設置執行錯誤訊息
|
|
|
3075 |
$result["error"]=$callShell;
|
| 226 |
liveuser |
3076 |
|
| 3 |
liveuser |
3077 |
#回傳結果
|
|
|
3078 |
return $result;
|
| 226 |
liveuser |
3079 |
|
| 3 |
liveuser |
3080 |
}#if end
|
| 226 |
liveuser |
3081 |
|
| 3 |
liveuser |
3082 |
#預設假設程序已經執行完畢
|
|
|
3083 |
$result["exist"]="false";
|
|
|
3084 |
|
|
|
3085 |
#如果程序還在執行
|
|
|
3086 |
if(count($callShell["output"])>0){
|
| 226 |
liveuser |
3087 |
|
| 3 |
liveuser |
3088 |
#設置程序還在執行
|
|
|
3089 |
$result["exist"]="true";
|
| 226 |
liveuser |
3090 |
|
| 3 |
liveuser |
3091 |
}#if end
|
| 226 |
liveuser |
3092 |
|
| 3 |
liveuser |
3093 |
#設置執行正常
|
| 226 |
liveuser |
3094 |
$result["status"]="true";
|
|
|
3095 |
|
| 3 |
liveuser |
3096 |
#回傳結果
|
|
|
3097 |
return $result;
|
| 226 |
liveuser |
3098 |
|
|
|
3099 |
}#function processStatus end
|
|
|
3100 |
|
| 3 |
liveuser |
3101 |
/*
|
|
|
3102 |
#函式說明:
|
|
|
3103 |
#呼叫shell依序執行系統命令,並取得回傳的內容.
|
|
|
3104 |
#回傳結果:
|
|
|
3105 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
3106 |
#$result["error"],錯誤訊息陣列.
|
| 226 |
liveuser |
3107 |
#$result["function"],當前執行的函數名稱.
|
| 3 |
liveuser |
3108 |
#$result["content"],爲執行完每個指令後的回傳結果.
|
|
|
3109 |
#$result["cmd"],執行的指令內容,若"sameShell"參數為"true",則會有該數值.
|
|
|
3110 |
#$result["output"],執行後得到的輸出,若"sameShell"參數為"true",則會有該數值.
|
|
|
3111 |
#必填參數:
|
|
|
3112 |
#$conf["command"],字串陣列,要執行的指令.
|
|
|
3113 |
$conf["command"]=array("");
|
|
|
3114 |
#$conf["fileArgu"],字串,變數__FILE__的內容,預設為當前檔案的路徑與名稱.
|
| 226 |
liveuser |
3115 |
#$conf["fileArgu"]="";
|
| 3 |
liveuser |
3116 |
#可省略參數:
|
|
|
3117 |
#$conf["argu"],陣字串列,執行各個$conf["command"]時指令搭配的參數,預設為空陣列.
|
|
|
3118 |
#$conf["argu"]=array(array(""));
|
|
|
3119 |
#$conf["enablePrintDescription"],字串陣列,執行各個$conf["command"]時是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,其數量須與$conf["command"]的元素數量相同,若只有一個元素,則代表是每個$conf["command"]執行時都用此參數.
|
|
|
3120 |
#$conf["enablePrintDescription"]=array("false");
|
|
|
3121 |
#$conf["printDescription"],字串陣列,執行各個$conf["command"]前要印出來的的文字,預設為$conf["command"]的內容,其數量須與$conf["command"]的元素數量相同,若只有一個元素,則代表是每個$conf["command"]執行時都用此參數.
|
|
|
3122 |
#$conf["printDescription"]=array("");
|
|
|
3123 |
#$conf["escapeshellarg"],字串陣列,執行各個$conf["command"]時是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false",其數量須與$conf["command"]的元素數量相同,若只有一個元素,則代表是每個$conf["command"]執行時都用此參數.
|
|
|
3124 |
#$conf["escapeshellarg"]=array("false");
|
|
|
3125 |
#$conf["username"],陣列字串,每個指令要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
|
|
|
3126 |
#$conf["username"]=array("");
|
|
|
3127 |
#$conf["password"],陣列字串,每個指令與$conf["username"]搭配的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
|
|
3128 |
#$conf["password"]=array("");
|
|
|
3129 |
#$conf["useScript"],字串,每個指令的執行是否要啟用Linux的script指令來記錄輸出,"true"代表要;"false"代表不要,預設為"false".
|
|
|
3130 |
#$conf["useScript"]="";
|
|
|
3131 |
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 ".qbpwcf_tmp/external/callShell/".
|
|
|
3132 |
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
|
|
|
3133 |
#$conf["inBackGround"],字串,每個指令的執行是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用";"區隔的多個指令將會出錯.
|
|
|
3134 |
#$conf["inBackGround"]="";
|
|
|
3135 |
#$conf["sameShell"],字串,"true"代表每個指令都在同空shell環境下執行,亦即會繼承變數、位置等資訊;預設為"false"代表要在個別獨立的shell環境下執行.
|
|
|
3136 |
#$conf["sameShell"]="true";
|
|
|
3137 |
#參考資料:
|
|
|
3138 |
#exec=>http://php.net/manual/en/function.exec.php
|
|
|
3139 |
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
|
|
|
3140 |
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
|
|
|
3141 |
#備註:
|
|
|
3142 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行.
|
| 226 |
liveuser |
3143 |
*/
|
| 3 |
liveuser |
3144 |
public static function callShellMulti(&$conf){
|
| 226 |
liveuser |
3145 |
|
| 3 |
liveuser |
3146 |
#初始化要回傳的結果
|
|
|
3147 |
$result=array();
|
| 226 |
liveuser |
3148 |
|
| 3 |
liveuser |
3149 |
#取得當前執行的函數名稱
|
|
|
3150 |
$result["function"]=__FUNCTION__;
|
| 226 |
liveuser |
3151 |
|
| 3 |
liveuser |
3152 |
#如果 $conf 不為陣列
|
|
|
3153 |
if(gettype($conf)!="array"){
|
| 226 |
liveuser |
3154 |
|
| 3 |
liveuser |
3155 |
#設置執行失敗
|
|
|
3156 |
$result["status"]="false";
|
| 226 |
liveuser |
3157 |
|
| 3 |
liveuser |
3158 |
#設置執行錯誤訊息
|
|
|
3159 |
$result["error"][]="\$conf變數須為陣列形態";
|
|
|
3160 |
|
|
|
3161 |
#如果傳入的參數為 null
|
|
|
3162 |
if($conf==null){
|
| 226 |
liveuser |
3163 |
|
| 3 |
liveuser |
3164 |
#設置執行錯誤訊息
|
|
|
3165 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
| 226 |
liveuser |
3166 |
|
| 3 |
liveuser |
3167 |
}#if end
|
|
|
3168 |
|
|
|
3169 |
#回傳結果
|
|
|
3170 |
return $result;
|
| 226 |
liveuser |
3171 |
|
| 3 |
liveuser |
3172 |
}#if end
|
| 226 |
liveuser |
3173 |
|
| 3 |
liveuser |
3174 |
#參數檢查
|
|
|
3175 |
#函式說明:
|
|
|
3176 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
3177 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
3178 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
3179 |
#$result["function"],當前執行的函式名稱.
|
|
|
3180 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
3181 |
#$result[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
3182 |
#$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
3183 |
#$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
3184 |
#必填寫的參數:
|
|
|
3185 |
#$conf["variableCheck::checkArguments"]["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
3186 |
$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
|
|
|
3187 |
#$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
3188 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("command","fileArgu");
|
| 226 |
liveuser |
3189 |
#$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
3190 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("array","string");
|
|
|
3191 |
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
3192 |
$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
3193 |
#可以省略的參數:
|
|
|
3194 |
#$conf["variableCheck::checkArguments"]["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true"。
|
|
|
3195 |
#$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
|
|
|
3196 |
#$conf["variableCheck::checkArguments"]["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
|
|
3197 |
$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("enablePrintDescription","printDescription","argu","escapeshellarg","username","password","useScript","logFilePath","inBackGround","sameShell");
|
| 226 |
liveuser |
3198 |
#$conf["variableCheck::checkArguments"]["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
3199 |
$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("array","array","array","array","array","array","string","string","string","string");
|
|
|
3200 |
#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,"null"代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
| 218 |
liveuser |
3201 |
$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(null,null,null,array(),null,null,null,null,null,"false");
|
| 3 |
liveuser |
3202 |
#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
3203 |
#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"][]=array();
|
|
|
3204 |
$checkResult=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
|
|
|
3205 |
unset($conf["variableCheck::checkArguments"]);
|
| 226 |
liveuser |
3206 |
|
| 3 |
liveuser |
3207 |
#如果檢查參數失敗
|
|
|
3208 |
if($checkResult["status"]=="false"){
|
| 226 |
liveuser |
3209 |
|
| 3 |
liveuser |
3210 |
#設置執行失敗
|
|
|
3211 |
$result["status"]="false";
|
| 226 |
liveuser |
3212 |
|
| 3 |
liveuser |
3213 |
#設置執行錯誤
|
|
|
3214 |
$result["error"]=$checkResult;
|
| 226 |
liveuser |
3215 |
|
| 3 |
liveuser |
3216 |
#回傳結果
|
|
|
3217 |
return $result;
|
| 226 |
liveuser |
3218 |
|
| 3 |
liveuser |
3219 |
}#if end
|
| 226 |
liveuser |
3220 |
|
| 3 |
liveuser |
3221 |
#如果檢查參數不通過
|
|
|
3222 |
if($checkResult["passed"]=="false"){
|
| 226 |
liveuser |
3223 |
|
| 3 |
liveuser |
3224 |
#設置執行失敗
|
|
|
3225 |
$result["status"]="false";
|
| 226 |
liveuser |
3226 |
|
| 3 |
liveuser |
3227 |
#設置執行錯誤
|
|
|
3228 |
$result["error"]=$checkResult;
|
| 226 |
liveuser |
3229 |
|
| 3 |
liveuser |
3230 |
#回傳結果
|
|
|
3231 |
return $result;
|
| 226 |
liveuser |
3232 |
|
| 218 |
liveuser |
3233 |
}#if end
|
| 226 |
liveuser |
3234 |
|
| 3 |
liveuser |
3235 |
#有幾個 $conf["command"] 就執行幾次
|
|
|
3236 |
for($i=0;$i<count($conf["command"]);$i++){
|
| 226 |
liveuser |
3237 |
|
| 3 |
liveuser |
3238 |
#如果 $conf["argu"] 存在
|
|
|
3239 |
if(isset($conf["argu"])){
|
| 226 |
liveuser |
3240 |
|
| 3 |
liveuser |
3241 |
#如果 $conf["argu"] 為 array 型態,且其元素數量等於 $conf["command"] 的元素數量
|
|
|
3242 |
if(gettype($conf["argu"])=="array" && count($conf["argu"])==count($conf["command"])){
|
| 226 |
liveuser |
3243 |
|
| 3 |
liveuser |
3244 |
#設置 $conf["argu"] 參數
|
|
|
3245 |
$conf["external::callShell"]["argu"]=$conf["argu"][$i];
|
| 226 |
liveuser |
3246 |
|
| 3 |
liveuser |
3247 |
}#if end
|
| 226 |
liveuser |
3248 |
|
| 3 |
liveuser |
3249 |
}#if end
|
| 226 |
liveuser |
3250 |
|
| 3 |
liveuser |
3251 |
#如果 $conf["enablePrintDescription"] 存在
|
|
|
3252 |
if(isset($conf["enablePrintDescription"])){
|
| 226 |
liveuser |
3253 |
|
| 3 |
liveuser |
3254 |
#如果 $conf["enablePrintDescription"] 為 array 型態,且其元素數量等於 $conf["command"] 的元素數量
|
|
|
3255 |
if(gettype($conf["enablePrintDescription"])=="array" && count($conf["enablePrintDescription"])==count($conf["command"])){
|
| 226 |
liveuser |
3256 |
|
| 3 |
liveuser |
3257 |
#設置 $conf["enablePrintDescription"] 參數
|
|
|
3258 |
$conf["external::callShell"]["enablePrintDescription"]=$conf["enablePrintDescription"][$i];
|
| 226 |
liveuser |
3259 |
|
| 3 |
liveuser |
3260 |
}#if end
|
| 226 |
liveuser |
3261 |
|
| 3 |
liveuser |
3262 |
#反之如果 $conf["enablePrintDescription"] 為 array 型態,且只有一個
|
|
|
3263 |
else if(gettype($conf["enablePrintDescription"])=="array" && count($conf["enablePrintDescription"])==1){
|
| 226 |
liveuser |
3264 |
|
| 3 |
liveuser |
3265 |
#設置 $conf["external::callShell"]["enablePrintDescription"] 參數為 $conf["enablePrintDescription"] 的第一個元素
|
|
|
3266 |
$conf["external::callShell"]["enablePrintDescription"]=$conf["enablePrintDescription"][0];
|
| 226 |
liveuser |
3267 |
|
| 3 |
liveuser |
3268 |
}#if end
|
| 226 |
liveuser |
3269 |
|
| 3 |
liveuser |
3270 |
}#if end
|
| 226 |
liveuser |
3271 |
|
| 3 |
liveuser |
3272 |
#如果 $conf["printDescription"] 存在
|
|
|
3273 |
if(isset($conf["printDescription"])){
|
| 226 |
liveuser |
3274 |
|
| 3 |
liveuser |
3275 |
#如果 $conf["printDescription"] 為 array 型態,且其元素數量等於 $conf["command"] 的元素數量
|
|
|
3276 |
if(gettype($conf["printDescription"])=="array" && count($conf["printDescription"])==count($conf["command"])){
|
| 226 |
liveuser |
3277 |
|
| 3 |
liveuser |
3278 |
#設置 $conf["enablePrintDescription"] 參數
|
|
|
3279 |
$conf["external::callShell"]["printDescription"]=$conf["printDescription"][$i];
|
| 226 |
liveuser |
3280 |
|
| 3 |
liveuser |
3281 |
}#if end
|
| 226 |
liveuser |
3282 |
|
| 3 |
liveuser |
3283 |
#反之如果 $conf["printDescription"] 為 array 型態,且只有一個
|
|
|
3284 |
else if(gettype($conf["printDescription"])=="array" && count($conf["printDescription"])==1){
|
| 226 |
liveuser |
3285 |
|
| 3 |
liveuser |
3286 |
#設置 $conf["external::callShell"]["printDescription"] 參數為 $conf["printDescription"] 的第一個元素
|
|
|
3287 |
$conf["external::callShell"]["printDescription"]=$conf["printDescription"][0];
|
| 226 |
liveuser |
3288 |
|
| 3 |
liveuser |
3289 |
}#if end
|
| 226 |
liveuser |
3290 |
|
| 3 |
liveuser |
3291 |
}#if end
|
| 226 |
liveuser |
3292 |
|
| 3 |
liveuser |
3293 |
#如果 $conf["escapeshellarg"] 存在
|
|
|
3294 |
if(isset($conf["escapeshellarg"])){
|
| 226 |
liveuser |
3295 |
|
| 3 |
liveuser |
3296 |
#如果 $conf["escapeshellarg"] 為 array 型態,且其元素數量等於 $conf["command"] 的元素數量
|
|
|
3297 |
if(gettype($conf["escapeshellarg"])=="array" && count($conf["escapeshellarg"])==count($conf["command"])){
|
| 226 |
liveuser |
3298 |
|
| 3 |
liveuser |
3299 |
#設置 $conf["escapeshellarg"] 參數
|
|
|
3300 |
$conf["external::callShell"]["escapeshellarg"]=$conf["escapeshellarg"][$i];
|
| 226 |
liveuser |
3301 |
|
| 3 |
liveuser |
3302 |
}#if end
|
| 226 |
liveuser |
3303 |
|
| 3 |
liveuser |
3304 |
#反之如果 $conf["escapeshellarg"] 為 array 型態,且只有一個
|
|
|
3305 |
else if(gettype($conf["escapeshellarg"])=="array" && count($conf["escapeshellarg"])==1){
|
| 226 |
liveuser |
3306 |
|
| 3 |
liveuser |
3307 |
#設置 $conf["external::callShell"]["escapeshellarg"] 參數為 $conf["escapeshellarg"] 的第一個元素
|
|
|
3308 |
$conf["external::callShell"]["escapeshellarg"]=$conf["escapeshellarg"][0];
|
| 226 |
liveuser |
3309 |
|
| 3 |
liveuser |
3310 |
}#if end
|
| 226 |
liveuser |
3311 |
|
| 3 |
liveuser |
3312 |
}#if end
|
| 226 |
liveuser |
3313 |
|
| 3 |
liveuser |
3314 |
#如果 $conf["username"] 存在
|
|
|
3315 |
if(isset($conf["username"])){
|
| 226 |
liveuser |
3316 |
|
| 3 |
liveuser |
3317 |
#如果 $conf["username"] 為 array 型態,且其元素數量等於 $conf["command"] 的元素數量
|
|
|
3318 |
if(gettype($conf["username"])=="array" && count($conf["username"])==count($conf["command"])){
|
| 226 |
liveuser |
3319 |
|
| 3 |
liveuser |
3320 |
#設置 $conf["username"] 參數
|
|
|
3321 |
$conf["external::callShell"]["username"]=$conf["username"][$i];
|
| 226 |
liveuser |
3322 |
|
| 3 |
liveuser |
3323 |
}#if end
|
| 226 |
liveuser |
3324 |
|
| 3 |
liveuser |
3325 |
#反之如果 $conf["escapeshellarg"] 為 array 型態,且只有一個
|
|
|
3326 |
else if(gettype($conf["username"])=="array" && count($conf["username"])==1){
|
| 226 |
liveuser |
3327 |
|
| 3 |
liveuser |
3328 |
#設置 $conf["external::callShell"]["username"] 參數為 $conf["username"] 的第一個元素
|
|
|
3329 |
$conf["external::callShell"]["username"]=$conf["username"][0];
|
| 226 |
liveuser |
3330 |
|
| 3 |
liveuser |
3331 |
}#if end
|
| 226 |
liveuser |
3332 |
|
| 218 |
liveuser |
3333 |
}#if end
|
| 226 |
liveuser |
3334 |
|
| 3 |
liveuser |
3335 |
#如果 $conf["password"] 存在
|
|
|
3336 |
if(isset($conf["password"])){
|
| 226 |
liveuser |
3337 |
|
| 3 |
liveuser |
3338 |
#如果 $conf["password"] 為 array 型態,且其元素數量等於 $conf["command"] 的元素數量
|
|
|
3339 |
if(gettype($conf["password"])=="array" && count($conf["password"])==count($conf["command"])){
|
| 226 |
liveuser |
3340 |
|
| 3 |
liveuser |
3341 |
#設置 $conf["password"] 參數
|
|
|
3342 |
$conf["external::callShell"]["password"]=$conf["password"][$i];
|
| 226 |
liveuser |
3343 |
|
| 3 |
liveuser |
3344 |
}#if end
|
| 226 |
liveuser |
3345 |
|
| 3 |
liveuser |
3346 |
#反之如果 $conf["password"] 為 array 型態,且只有一個
|
|
|
3347 |
else if(gettype($conf["password"])=="array" && count($conf["password"])==1){
|
| 226 |
liveuser |
3348 |
|
| 3 |
liveuser |
3349 |
#設置 $conf["external::callShell"]["password"] 參數為 $conf["password"] 的第一個元素
|
|
|
3350 |
$conf["external::callShell"]["password"]=$conf["password"][0];
|
| 226 |
liveuser |
3351 |
|
| 3 |
liveuser |
3352 |
}#if end
|
| 226 |
liveuser |
3353 |
|
| 218 |
liveuser |
3354 |
}#if end
|
| 226 |
liveuser |
3355 |
|
| 3 |
liveuser |
3356 |
#如果有設置 $conf["logFilePath"]
|
|
|
3357 |
if(isset($conf["logFilePath"])){
|
| 226 |
liveuser |
3358 |
|
| 3 |
liveuser |
3359 |
#設置 $conf["logFilePath"] 參數
|
|
|
3360 |
$conf["external::callShell"]["logFilePath"]=$conf["logFilePath"];
|
| 226 |
liveuser |
3361 |
|
| 218 |
liveuser |
3362 |
}#if end
|
| 226 |
liveuser |
3363 |
|
| 3 |
liveuser |
3364 |
#如果有設置 $conf["fileArgu"]
|
|
|
3365 |
if(isset($conf["fileArgu"])){
|
| 226 |
liveuser |
3366 |
|
| 3 |
liveuser |
3367 |
#設置 $conf["fileArgu"] 參數
|
|
|
3368 |
$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];
|
| 226 |
liveuser |
3369 |
|
| 3 |
liveuser |
3370 |
}#if end
|
| 226 |
liveuser |
3371 |
|
| 3 |
liveuser |
3372 |
#如果有設置 $conf["inBackGround"]
|
|
|
3373 |
if(isset($conf["inBackGround"])){
|
| 226 |
liveuser |
3374 |
|
| 3 |
liveuser |
3375 |
#設置 $conf["fileArgu"] 參數
|
|
|
3376 |
$conf["external::callShell"]["inBackGround"]=$conf["inBackGround"];
|
| 226 |
liveuser |
3377 |
|
| 3 |
liveuser |
3378 |
}#if end
|
| 226 |
liveuser |
3379 |
|
| 3 |
liveuser |
3380 |
#如果有指定 sameShell
|
|
|
3381 |
if($conf["sameShell"]==="true"){
|
| 226 |
liveuser |
3382 |
|
| 3 |
liveuser |
3383 |
#設置不執行指令
|
|
|
3384 |
$conf["external::callShell"]["doNotRun"]="true";
|
| 226 |
liveuser |
3385 |
|
| 3 |
liveuser |
3386 |
}#if end
|
| 226 |
liveuser |
3387 |
|
| 218 |
liveuser |
3388 |
#如果沒有指定是否要 escapeshellarg
|
|
|
3389 |
if(!isset($conf["escapeshellarg"][$i])){
|
| 226 |
liveuser |
3390 |
|
| 218 |
liveuser |
3391 |
#設置為 "false"
|
|
|
3392 |
$conf["escapeshellarg"][$i]="false";
|
| 226 |
liveuser |
3393 |
|
| 218 |
liveuser |
3394 |
}#if end
|
| 226 |
liveuser |
3395 |
|
| 3 |
liveuser |
3396 |
#函式說明:
|
|
|
3397 |
#呼叫shell執行系統命令,並取得回傳的內容.
|
|
|
3398 |
#回傳結果:
|
|
|
3399 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
3400 |
#$result["error"],錯誤訊息陣列.
|
|
|
3401 |
#$result["function"],當前執行的函數名稱.
|
|
|
3402 |
#$result["cmd"],執行的指令內容.
|
|
|
3403 |
#$result["output"],爲執行完二元碼後的輸出陣列.
|
|
|
3404 |
#必填參數:
|
|
|
3405 |
#$conf["command"],字串,要執行的指令與.
|
|
|
3406 |
$conf["external::callShell"]["command"]=$conf["command"][$i];
|
|
|
3407 |
#可省略參數:
|
|
|
3408 |
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
|
|
|
3409 |
#$conf["argu"]=array("");
|
|
|
3410 |
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
|
|
|
3411 |
#$conf["enablePrintDescription"]="true";
|
|
|
3412 |
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容.
|
|
|
3413 |
#$conf["printDescription"]="";
|
|
|
3414 |
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".
|
| 218 |
liveuser |
3415 |
$conf["external::callShell"]["escapeshellarg"]=$conf["escapeshellarg"][$i];
|
| 3 |
liveuser |
3416 |
#$conf["username"],字串,要用什麼使用者來執行,預設為執行apache的使用者,該參數不適用於apache環境.
|
|
|
3417 |
#$conf["username"]="";
|
|
|
3418 |
#$conf["password"],字串,與$conf["username"]搭配的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
|
|
3419 |
#$conf["password"]="";
|
|
|
3420 |
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要;"false"代表不要,預設為"false".
|
|
|
3421 |
#$conf["useScript"]="";
|
|
|
3422 |
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 ".qbpwcf_tmp/external/callShell/".
|
|
|
3423 |
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
|
|
|
3424 |
#$conf["fileArgu"],字串,變數__FILE__的內容,預設為當前檔案的路徑與名稱.
|
| 226 |
liveuser |
3425 |
#$conf["fileArgu"]="";
|
| 3 |
liveuser |
3426 |
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false".
|
|
|
3427 |
#$conf["inBackGround"]="";
|
|
|
3428 |
#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
|
|
|
3429 |
#$conf["external::callShell"]["doNotRun"]="false";
|
|
|
3430 |
#備註:
|
|
|
3431 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行.
|
|
|
3432 |
#參考資料:
|
|
|
3433 |
#exec=>http://php.net/manual/en/function.exec.php
|
|
|
3434 |
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
|
|
|
3435 |
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
|
|
|
3436 |
$callShell=external::callShell($conf["external::callShell"]);
|
|
|
3437 |
unset($conf["external::callShell"]);
|
| 226 |
liveuser |
3438 |
|
| 3 |
liveuser |
3439 |
#如果執行shell失敗
|
|
|
3440 |
if($callShell["status"]=="false"){
|
| 226 |
liveuser |
3441 |
|
| 3 |
liveuser |
3442 |
#設置執行失敗
|
|
|
3443 |
$result["status"]="false";
|
| 226 |
liveuser |
3444 |
|
| 3 |
liveuser |
3445 |
#設置執行錯誤
|
|
|
3446 |
$result["error"]=$callShell;
|
| 226 |
liveuser |
3447 |
|
| 3 |
liveuser |
3448 |
#回傳結果
|
|
|
3449 |
return $result;
|
| 226 |
liveuser |
3450 |
|
| 3 |
liveuser |
3451 |
}#if end
|
| 226 |
liveuser |
3452 |
|
| 3 |
liveuser |
3453 |
#如果有指定 sameShell
|
|
|
3454 |
if($conf["sameShell"]==="true"){
|
| 226 |
liveuser |
3455 |
|
| 3 |
liveuser |
3456 |
#如果沒有指定 $rawCmd
|
|
|
3457 |
if(!isset($rawCmd)){
|
| 226 |
liveuser |
3458 |
|
| 3 |
liveuser |
3459 |
#初始化要執行的指令內容
|
|
|
3460 |
$rawCmd=$callShell["cmd"];
|
| 226 |
liveuser |
3461 |
|
| 3 |
liveuser |
3462 |
}#if end
|
| 226 |
liveuser |
3463 |
|
| 3 |
liveuser |
3464 |
#反之
|
|
|
3465 |
else{
|
| 226 |
liveuser |
3466 |
|
| 3 |
liveuser |
3467 |
#用";"串接指令
|
|
|
3468 |
$rawCmd=$rawCmd.";".$callShell["cmd"];
|
| 226 |
liveuser |
3469 |
|
| 3 |
liveuser |
3470 |
}#else end
|
| 226 |
liveuser |
3471 |
|
| 3 |
liveuser |
3472 |
}#if end
|
| 226 |
liveuser |
3473 |
|
| 3 |
liveuser |
3474 |
#反之在個別shell環境下執行
|
|
|
3475 |
else{
|
| 226 |
liveuser |
3476 |
|
| 3 |
liveuser |
3477 |
#紀錄每個指令執行的結果
|
|
|
3478 |
$result["content"][]=$callShell;
|
| 226 |
liveuser |
3479 |
|
| 3 |
liveuser |
3480 |
}#else end
|
| 226 |
liveuser |
3481 |
|
| 3 |
liveuser |
3482 |
}#for end
|
| 226 |
liveuser |
3483 |
|
| 3 |
liveuser |
3484 |
#如果有 $rawCmd 存在
|
|
|
3485 |
if(isset($rawCmd)){
|
| 226 |
liveuser |
3486 |
|
| 3 |
liveuser |
3487 |
#運行指令
|
|
|
3488 |
exec($rawCmd,$output,$status);
|
| 226 |
liveuser |
3489 |
|
| 3 |
liveuser |
3490 |
#設置執行的指令內容.
|
|
|
3491 |
$result["cmd"]=$rawCmd;
|
| 226 |
liveuser |
3492 |
|
| 3 |
liveuser |
3493 |
#執行後得到的輸出.
|
|
|
3494 |
$result["output"]=$output;
|
| 226 |
liveuser |
3495 |
|
| 3 |
liveuser |
3496 |
}#if end
|
| 226 |
liveuser |
3497 |
|
| 3 |
liveuser |
3498 |
#執行到這邊代表執行正常
|
|
|
3499 |
$result["status"]="true";
|
| 226 |
liveuser |
3500 |
|
| 3 |
liveuser |
3501 |
#回傳結果
|
|
|
3502 |
return $result;
|
| 226 |
liveuser |
3503 |
|
| 3 |
liveuser |
3504 |
}#function callShellMulti end
|
| 226 |
liveuser |
3505 |
|
| 3 |
liveuser |
3506 |
/*
|
|
|
3507 |
#函式說明:
|
|
|
3508 |
#編譯C++檔案,下載輸出的2元碼.
|
|
|
3509 |
#回傳結果:
|
|
|
3510 |
#當 $conf["noDownload"] 設為 "true" 或執行出錯時 才會有回傳的結果.
|
|
|
3511 |
#$result["status"],"true"代表執行正常,"false"代表執行失敗.
|
|
|
3512 |
#$result["error"],錯誤訊息陣列.
|
|
|
3513 |
#$result["function"],正在執行的函式
|
|
|
3514 |
#$result["content"],編譯好的檔案位置.
|
|
|
3515 |
#必填參數:
|
|
|
3516 |
#$conf["cppAddress"],字串,要編譯的cpp檔位置,請加上cpp副檔名.
|
|
|
3517 |
$conf["cppAddress"]="";
|
|
|
3518 |
#可省略參數:
|
|
|
3519 |
#$conf["outputAddress"],字串,輸出的檔案位置與名稱,請加上附檔名,預設為".bin/cppOriFileName.bin".
|
|
|
3520 |
#$conf["outputAddress"]="";
|
|
|
3521 |
#$conf["noDownload"],字串,"true"為不要下載,改成回傳編譯好的檔案位置.
|
|
|
3522 |
#$conf["noDownload"]="true";
|
|
|
3523 |
#參考資料:
|
|
|
3524 |
#無.
|
|
|
3525 |
#備註:
|
|
|
3526 |
#測試中...
|
|
|
3527 |
#為了抓取編譯cpp產生的錯誤,要改用script指令來抓取輸出的結果.
|
| 226 |
liveuser |
3528 |
*/
|
| 3 |
liveuser |
3529 |
public static function compileCppThenDownload(&$conf){
|
| 226 |
liveuser |
3530 |
|
| 3 |
liveuser |
3531 |
#初始化要回傳的變數
|
|
|
3532 |
$result=array();
|
| 226 |
liveuser |
3533 |
|
| 3 |
liveuser |
3534 |
#取得當前執行的函式
|
|
|
3535 |
$result["function"]=__FUNCTION__;
|
| 226 |
liveuser |
3536 |
|
| 3 |
liveuser |
3537 |
#如果 $conf 不為陣列
|
|
|
3538 |
if(gettype($conf)!="array"){
|
| 226 |
liveuser |
3539 |
|
| 3 |
liveuser |
3540 |
#設置執行失敗
|
|
|
3541 |
$result["status"]="false";
|
| 226 |
liveuser |
3542 |
|
| 3 |
liveuser |
3543 |
#設置執行錯誤訊息
|
|
|
3544 |
$result["error"][]="\$conf變數須為陣列形態";
|
|
|
3545 |
|
|
|
3546 |
#如果傳入的參數為 null
|
|
|
3547 |
if($conf==null){
|
| 226 |
liveuser |
3548 |
|
| 3 |
liveuser |
3549 |
#設置執行錯誤訊息
|
|
|
3550 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
| 226 |
liveuser |
3551 |
|
| 3 |
liveuser |
3552 |
}#if end
|
|
|
3553 |
|
|
|
3554 |
#回傳結果
|
|
|
3555 |
return $result;
|
| 226 |
liveuser |
3556 |
|
| 3 |
liveuser |
3557 |
}#if end
|
| 226 |
liveuser |
3558 |
|
| 3 |
liveuser |
3559 |
#檢查參數
|
|
|
3560 |
#函式說明:
|
|
|
3561 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
3562 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
3563 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
3564 |
#$result["function"],當前執行的函式名稱.
|
|
|
3565 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
3566 |
#$result[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
3567 |
#$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
3568 |
#$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
3569 |
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
|
|
|
3570 |
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
|
|
|
3571 |
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
|
|
|
3572 |
#必填寫的參數:
|
|
|
3573 |
#$conf["variableCheck::checkArguments"]["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
3574 |
$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
|
|
|
3575 |
#$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
3576 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("cppAddress");
|
| 226 |
liveuser |
3577 |
#$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
3578 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string");
|
|
|
3579 |
#$conf["variableCheck::checkArguments"]["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
3580 |
$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
3581 |
#可以省略的參數:
|
|
|
3582 |
#$conf["variableCheck::checkArguments"]["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true"。
|
|
|
3583 |
$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
|
|
|
3584 |
#$conf["variableCheck::checkArguments"]["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
|
|
3585 |
$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("outputAddress","noDownload");
|
| 226 |
liveuser |
3586 |
#$conf["variableCheck::checkArguments"]["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
3587 |
$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string");
|
|
|
3588 |
#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
|
|
3589 |
$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(".bin/".$conf["cppAddress"].".bin","true");
|
|
|
3590 |
#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
3591 |
#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"][]=array();
|
|
|
3592 |
#參考資料來源:
|
|
|
3593 |
#array_keys=>http://php.net/manual/en/function.array-keys.php
|
|
|
3594 |
$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
|
|
|
3595 |
unset($conf["variableCheck::checkArguments"]);
|
| 226 |
liveuser |
3596 |
|
| 3 |
liveuser |
3597 |
#如果 $checkArguments["status"] 等於 "false"
|
|
|
3598 |
if($checkArguments["status"]=="false"){
|
| 226 |
liveuser |
3599 |
|
| 3 |
liveuser |
3600 |
#設置 $result["status"] 為 "false"
|
|
|
3601 |
$result["status"]="false";
|
| 226 |
liveuser |
3602 |
|
| 3 |
liveuser |
3603 |
#設置 $result["error"]
|
|
|
3604 |
$result["error"]=$checkArguments;
|
| 226 |
liveuser |
3605 |
|
| 3 |
liveuser |
3606 |
#回傳結果
|
|
|
3607 |
return $result;
|
| 226 |
liveuser |
3608 |
|
| 3 |
liveuser |
3609 |
}#if end
|
| 226 |
liveuser |
3610 |
|
| 3 |
liveuser |
3611 |
#如果 $checkArguments["passed"] 等於 "false"
|
|
|
3612 |
if($checkArguments["passed"]=="false"){
|
| 226 |
liveuser |
3613 |
|
| 3 |
liveuser |
3614 |
#設置 $result["status"] 為 "false"
|
|
|
3615 |
$result["status"]="false";
|
| 226 |
liveuser |
3616 |
|
| 3 |
liveuser |
3617 |
#設置 $result["error"]
|
|
|
3618 |
$result["error"]=$checkArguments;
|
| 226 |
liveuser |
3619 |
|
| 3 |
liveuser |
3620 |
#回傳結果
|
|
|
3621 |
return $result;
|
| 226 |
liveuser |
3622 |
|
| 3 |
liveuser |
3623 |
}#if end
|
|
|
3624 |
|
|
|
3625 |
#涵是說明:
|
|
|
3626 |
#取得檔案路徑字串的路徑與檔案的名稱與檔案副檔名
|
|
|
3627 |
#回傳結果:
|
|
|
3628 |
#$result["status"],執行是否正常,"true"正常,"false"代表不正常.
|
|
|
3629 |
#$result["error"],錯誤訊息.
|
|
|
3630 |
#$result["filePath"],路徑字串.
|
|
|
3631 |
#$result["fileName"],檔案名稱字串.
|
|
|
3632 |
#$result["fileExtention"],檔案的副檔名.
|
|
|
3633 |
#$result["fullFileName"],含副檔名的檔案名稱.
|
|
|
3634 |
#$result["fullFilePathAndName"],完整的檔案路徑(含副檔名).
|
|
|
3635 |
#必填參數:
|
|
|
3636 |
#$conf["fileAccess"]["getFileAddressAndNameAndFileExtention"]["fileAddressAndName"],字串,檔案名稱與其路徑.
|
|
|
3637 |
$conf["fileAccess"]["getFileAddressAndNameAndFileExtention"]["fileAddressAndName"]=$conf["outputAddress"];
|
|
|
3638 |
#可省略參數:
|
|
|
3639 |
#無.
|
|
|
3640 |
$filePathInfo=fileAccess::getFileAddressAndNameAndFileExtention($conf["fileAccess"]["getFileAddressAndNameAndFileExtention"]);
|
| 226 |
liveuser |
3641 |
unset($conf["fileAccess"]["getFileAddressAndNameAndFileExtention"]);
|
|
|
3642 |
|
| 3 |
liveuser |
3643 |
#若取得檔案路徑字串的路徑與檔案的名稱與檔案副檔名失敗
|
|
|
3644 |
if($filePathInfo["status"]=="false"){
|
| 226 |
liveuser |
3645 |
|
| 3 |
liveuser |
3646 |
#設置 $result["status"] 為 "false"
|
|
|
3647 |
$result["status"]="false";
|
| 226 |
liveuser |
3648 |
|
| 3 |
liveuser |
3649 |
#設置 $result["error"]
|
|
|
3650 |
$result["error"]=$filePathInfo;
|
| 226 |
liveuser |
3651 |
|
| 3 |
liveuser |
3652 |
#回傳結果
|
|
|
3653 |
return $result;
|
| 226 |
liveuser |
3654 |
|
| 3 |
liveuser |
3655 |
}#if end
|
| 226 |
liveuser |
3656 |
|
| 3 |
liveuser |
3657 |
#如果 $filePathInfo["filePath"] 不等於 ""
|
|
|
3658 |
if($filePathInfo["filePath"]!=""){
|
| 226 |
liveuser |
3659 |
|
| 3 |
liveuser |
3660 |
#涵式說明:
|
|
|
3661 |
#建立資料夾,若要建立的資料夾所屬路徑不存在,則會自動嘗試建立,建立後的資料夾也可指定權限.
|
|
|
3662 |
#回傳的結果:
|
|
|
3663 |
#$result["status"],"true"爲建立成功,"false"爲建立失敗.
|
|
|
3664 |
#$result["error"],錯誤訊息陣列
|
|
|
3665 |
#必填參數::
|
|
|
3666 |
$conf["fileAccess"]["createFolderAfterCheck"]["dirPositionAndName"]=$filePathInfo["filePath"];#新建的位置與名稱
|
|
|
3667 |
#可省略參數:
|
|
|
3668 |
#$conf["fileAccess"]["createFolderAfterCheck"]["dirPermission"]="";#新建資料夾的權限設定,預設爲0770,亦即擁有者,同群組者可以讀,寫,存取,其他人僅能存取.
|
|
|
3669 |
$checkResult=fileAccess::createFolderAfterCheck($conf["fileAccess"]["createFolderAfterCheck"]);
|
|
|
3670 |
unset($conf["fileAccess"]["createFolderAfterCheck"]);
|
| 226 |
liveuser |
3671 |
|
| 3 |
liveuser |
3672 |
#如果 $checkResult["status"] 等於 "false"
|
|
|
3673 |
if($checkResult["status"]=="false"){
|
| 226 |
liveuser |
3674 |
|
| 3 |
liveuser |
3675 |
#設置錯誤識別
|
|
|
3676 |
$result["status"]="false";
|
| 226 |
liveuser |
3677 |
|
| 3 |
liveuser |
3678 |
#設置錯誤訊息
|
|
|
3679 |
$result["error"]=$checkResult;
|
| 226 |
liveuser |
3680 |
|
| 3 |
liveuser |
3681 |
#回傳結果
|
|
|
3682 |
return $result;
|
| 226 |
liveuser |
3683 |
|
| 3 |
liveuser |
3684 |
}#if end
|
| 226 |
liveuser |
3685 |
|
| 3 |
liveuser |
3686 |
}#if end
|
| 226 |
liveuser |
3687 |
|
| 3 |
liveuser |
3688 |
#檢查目標 cpp 檔案在不在
|
|
|
3689 |
#涵式說明:檢查多個檔案與資料夾是否存在.
|
|
|
3690 |
#回傳結果:
|
|
|
3691 |
#$result["varName"][$i],爲第$i個資料夾或檔案的名稱。
|
|
|
3692 |
#$result["varExist"][$i],爲第$i個資料夾或檔案是否存在,"true"代表存在,"false"代表不存在。
|
|
|
3693 |
#必填參數:
|
|
|
3694 |
$conf["fileAccess"]["checkMultiFileExist"]["fileArray"]=array($conf["cppAddress"]);#要檢查書否存在的檔案有哪些,須爲一維陣列數值。
|
|
|
3695 |
#參考資料來源:
|
|
|
3696 |
#http://php.net/manual/en/function.file-exists.php
|
|
|
3697 |
#http://php.net/manual/en/control-structures.foreach.php
|
|
|
3698 |
$checkResult=fileAccess::checkMultiFileExist($conf["fileAccess"]["checkMultiFileExist"]);
|
|
|
3699 |
unset($conf["fileAccess"]["checkMultiFileExist"]);
|
| 226 |
liveuser |
3700 |
|
| 3 |
liveuser |
3701 |
#如果 $checkResult["status"]為"false"
|
|
|
3702 |
if($checkResult["status"]=="false"){
|
| 226 |
liveuser |
3703 |
|
| 3 |
liveuser |
3704 |
#設置錯誤識別
|
|
|
3705 |
$result["status"]="false";
|
| 226 |
liveuser |
3706 |
|
| 3 |
liveuser |
3707 |
#設置錯誤訊息
|
|
|
3708 |
$result["error"]=$checkResult;
|
| 226 |
liveuser |
3709 |
|
| 3 |
liveuser |
3710 |
#回傳結果
|
|
|
3711 |
return $result;
|
| 226 |
liveuser |
3712 |
|
| 3 |
liveuser |
3713 |
}#if end
|
| 226 |
liveuser |
3714 |
|
| 3 |
liveuser |
3715 |
#如果 $checkResult["varExist"][0] 等於 "false"
|
|
|
3716 |
if($checkResult["varExist"][0]=="false"){
|
| 226 |
liveuser |
3717 |
|
| 3 |
liveuser |
3718 |
#設置錯誤識別
|
|
|
3719 |
$result["status"]="false";
|
| 226 |
liveuser |
3720 |
|
| 3 |
liveuser |
3721 |
#設置錯誤訊息
|
|
|
3722 |
$result["error"][]="要編譯的檔案 ".$conf["cppAddress"]." 不存在";
|
| 226 |
liveuser |
3723 |
|
| 3 |
liveuser |
3724 |
#回傳結果
|
|
|
3725 |
return $result;
|
| 226 |
liveuser |
3726 |
|
| 3 |
liveuser |
3727 |
}#if end
|
| 226 |
liveuser |
3728 |
|
| 3 |
liveuser |
3729 |
#執行系統命令 編譯 $conf["cppAddress"]
|
|
|
3730 |
#函式說明:
|
|
|
3731 |
#呼叫shell執行系統命令,並取得回傳的內容.
|
|
|
3732 |
#回傳結果:
|
|
|
3733 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
3734 |
#$result["error"],錯誤訊息陣列.
|
|
|
3735 |
#$result["function"],當前執行的函數名稱.
|
|
|
3736 |
#$result["argu"],使用的參數.
|
|
|
3737 |
#$result["cmd"],執行的指令內容.
|
|
|
3738 |
#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
3739 |
#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
|
|
|
3740 |
#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
3741 |
#$result["running"],是否還在執行.
|
|
|
3742 |
#$result["pid"],pid.
|
|
|
3743 |
#$result["statusCode"],執行結束後的代碼.
|
|
|
3744 |
#$result["escape"],陣列,儲存出新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu").
|
|
|
3745 |
#必填參數:
|
|
|
3746 |
#$conf["command"],字串,要執行的指令.
|
|
|
3747 |
$conf["external::callShell"]["command"]="g++";
|
|
|
3748 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
|
|
3749 |
$conf["external::callShell"]["fileArgu"]=__FILE__;
|
|
|
3750 |
#可省略參數:
|
|
|
3751 |
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
|
|
|
3752 |
$conf["external::callShell"]["argu"]=array($conf["cppAddress"],"-o",$conf["outputAddress"]);
|
|
|
3753 |
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
|
|
|
3754 |
#$conf["arguIsAddr"]=array();
|
|
|
3755 |
#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
|
|
|
3756 |
#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
|
|
|
3757 |
#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
|
|
|
3758 |
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
|
|
|
3759 |
#$conf["enablePrintDescription"]="true";
|
|
|
3760 |
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
|
|
|
3761 |
#$conf["printDescription"]="";
|
|
|
3762 |
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
|
|
|
3763 |
$conf["external::callShell"]["escapeshellarg"]="true";
|
|
|
3764 |
#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
|
|
|
3765 |
#$conf["thereIsShellVar"]=array();
|
|
|
3766 |
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
|
|
|
3767 |
#$conf["username"]="";
|
|
|
3768 |
#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
|
|
3769 |
#$conf["password"]="";
|
|
|
3770 |
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
|
|
|
3771 |
#$conf["useScript"]="";
|
|
|
3772 |
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
|
|
|
3773 |
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
|
|
|
3774 |
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
|
|
|
3775 |
#$conf["inBackGround"]="";
|
|
|
3776 |
#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
|
|
|
3777 |
#$conf["getErr"]="false";
|
|
|
3778 |
#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
|
|
|
3779 |
#$conf["doNotRun"]="false";
|
|
|
3780 |
#參考資料:
|
|
|
3781 |
#exec=>http://php.net/manual/en/function.exec.php
|
|
|
3782 |
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
|
|
|
3783 |
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
|
|
|
3784 |
#備註:
|
|
|
3785 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
|
|
|
3786 |
#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
|
|
|
3787 |
$execResult=external::callShell($conf["external::callShell"]);
|
|
|
3788 |
unset($conf["external::callShell"]);
|
| 226 |
liveuser |
3789 |
|
| 3 |
liveuser |
3790 |
#如果 $execResult["status"] 等於 "false"
|
|
|
3791 |
if($execResult["status"]=="false"){
|
| 226 |
liveuser |
3792 |
|
| 3 |
liveuser |
3793 |
#設置錯誤識別
|
|
|
3794 |
$result["status"]="false";
|
| 226 |
liveuser |
3795 |
|
| 3 |
liveuser |
3796 |
#設置錯誤訊息
|
|
|
3797 |
$result["error"]=$execResult;
|
| 226 |
liveuser |
3798 |
|
| 3 |
liveuser |
3799 |
#回傳結果
|
|
|
3800 |
return $result;
|
| 226 |
liveuser |
3801 |
|
| 3 |
liveuser |
3802 |
}#if end
|
| 226 |
liveuser |
3803 |
|
| 3 |
liveuser |
3804 |
#如果 $conf["noDownload"] 有設置了
|
|
|
3805 |
if(isset($conf["noDownload"])){
|
| 226 |
liveuser |
3806 |
|
| 3 |
liveuser |
3807 |
#如果$conf["noDownload"] 等於 "true"
|
|
|
3808 |
if($conf["noDownload"]=="true"){
|
| 226 |
liveuser |
3809 |
|
| 3 |
liveuser |
3810 |
#設置執行正常的識別
|
|
|
3811 |
$result["status"]="true";
|
| 226 |
liveuser |
3812 |
|
| 3 |
liveuser |
3813 |
#設置編譯好的檔案位置
|
|
|
3814 |
$result["content"]=$conf["outputAddress"];
|
| 226 |
liveuser |
3815 |
|
| 3 |
liveuser |
3816 |
#回傳結果
|
|
|
3817 |
return $result;
|
| 226 |
liveuser |
3818 |
|
| 3 |
liveuser |
3819 |
}#if end
|
| 226 |
liveuser |
3820 |
|
| 3 |
liveuser |
3821 |
}#if end
|
| 226 |
liveuser |
3822 |
|
| 3 |
liveuser |
3823 |
#說明:
|
|
|
3824 |
#要求使用者Download某檔案
|
|
|
3825 |
#回傳結果:
|
|
|
3826 |
#true爲成功,反之爲錯誤訊息
|
|
|
3827 |
#必填參數::
|
|
|
3828 |
$conf["header"]["askUserDownloadFile"]["filePositionAndName"]=$conf["outputAddress"];#檔案的位置與名稱
|
|
|
3829 |
#可省略參數:
|
|
|
3830 |
$conf["header"]["askUserDownloadFile"]["fileDisplayName"]=$filePathInfo["fullFileName"];#要顯示的檔案名稱,若要放中文請將其文字放在()裏面,這樣種文字才會顯現。預設為 basename($conf["filePositionAndName"])." date:".$系統時間;
|
|
|
3831 |
#參考資料來源:
|
|
|
3832 |
#http:#php.net/manual/en/function.readfile.php
|
|
|
3833 |
return header::askUserDownloadFile($conf["header"]["askUserDownloadFile"]);
|
|
|
3834 |
unset($conf["header"]["askUserDownloadFile"]);
|
| 226 |
liveuser |
3835 |
|
| 3 |
liveuser |
3836 |
}#function compileCppThenDownload end
|
|
|
3837 |
|
|
|
3838 |
/*
|
|
|
3839 |
#函式說明:
|
|
|
3840 |
#運用「nohup」與「&」來達到雖依序執行但不等待前一個指令執行完再執行下一個指令,形成在背景假多工的執行每個指令.
|
| 226 |
liveuser |
3841 |
#回傳結果:
|
| 3 |
liveuser |
3842 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
3843 |
#$result["error"],錯誤訊息陣列.
|
|
|
3844 |
#$result["function"],當前執行的函數名稱.
|
|
|
3845 |
#$result["cmd"],執行的指令內容.
|
|
|
3846 |
#$result["output"],爲執行完二元碼後的輸出陣列.
|
|
|
3847 |
#必填參數:
|
|
|
3848 |
#$conf["command"],字串陣列,要執行的指令
|
|
|
3849 |
$conf["command"]=array("");
|
|
|
3850 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
| 226 |
liveuser |
3851 |
$conf["fileArgu"]=__FILE__;
|
| 3 |
liveuser |
3852 |
#可省略參數:
|
|
|
3853 |
#$conf["argu"],二維陣列字串,每個指令搭配的參數,預設為空陣列.
|
|
|
3854 |
#$conf["argu"]=array(array());
|
|
|
3855 |
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
|
|
|
3856 |
#$conf["enablePrintDescription"]="true";
|
|
|
3857 |
#$conf["printDescription"],字串陣列,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容.
|
|
|
3858 |
#$conf["printDescription"]=array("");
|
|
|
3859 |
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".
|
|
|
3860 |
#$conf["escapeshellarg"]="false";
|
|
|
3861 |
#$conf["username"],字串陣列,每個指令要用什麼使用者來執行,預設為執行php使用者,該參數不適用於apache環境.
|
|
|
3862 |
#$conf["username"]=array("");
|
|
|
3863 |
#$conf["password"],字串陣列,與$conf["username"]搭配的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
|
|
3864 |
#$conf["password"]=array("");
|
|
|
3865 |
#$conf["web"],字串,腳本檔案是存放在web則為"true";若為檔案系統則為"false",預設為"false".
|
|
|
3866 |
#$conf["web"]=""
|
|
|
3867 |
#$conf["wait"],字串,執行時是否要等待前一個程序執行完,預設為"false"不等待,反之為"true"要等待.
|
|
|
3868 |
#$conf["wait"]="true";
|
|
|
3869 |
#參考資料:
|
|
|
3870 |
#無.
|
|
|
3871 |
#備註:
|
|
|
3872 |
#無.
|
|
|
3873 |
*/
|
|
|
3874 |
public static function multiThreadsShell(&$conf){
|
| 226 |
liveuser |
3875 |
|
| 3 |
liveuser |
3876 |
#初始化要回傳的結果
|
|
|
3877 |
$result=array();
|
| 226 |
liveuser |
3878 |
|
| 3 |
liveuser |
3879 |
#取得當前執行的函數名稱
|
|
|
3880 |
$result["function"]=__FUNCTION__;
|
| 226 |
liveuser |
3881 |
|
| 3 |
liveuser |
3882 |
#如果 $conf 不為陣列
|
|
|
3883 |
if(gettype($conf)!="array"){
|
| 226 |
liveuser |
3884 |
|
| 3 |
liveuser |
3885 |
#設置執行失敗
|
|
|
3886 |
$result["status"]="false";
|
| 226 |
liveuser |
3887 |
|
| 3 |
liveuser |
3888 |
#設置執行錯誤訊息
|
|
|
3889 |
$result["error"][]="\$conf變數須為陣列形態";
|
|
|
3890 |
|
|
|
3891 |
#如果傳入的參數為 null
|
|
|
3892 |
if($conf==null){
|
| 226 |
liveuser |
3893 |
|
| 3 |
liveuser |
3894 |
#設置執行錯誤訊息
|
|
|
3895 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
| 226 |
liveuser |
3896 |
|
| 3 |
liveuser |
3897 |
}#if end
|
|
|
3898 |
|
|
|
3899 |
#回傳結果
|
|
|
3900 |
return $result;
|
| 226 |
liveuser |
3901 |
|
| 3 |
liveuser |
3902 |
}#if end
|
| 226 |
liveuser |
3903 |
|
| 3 |
liveuser |
3904 |
#參數檢查
|
|
|
3905 |
#函式說明:
|
|
|
3906 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
3907 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
3908 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
3909 |
#$result["function"],當前執行的函式名稱.
|
|
|
3910 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
3911 |
#$result[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
3912 |
#$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
3913 |
#$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
3914 |
#必填寫的參數:
|
|
|
3915 |
#$conf["variableCheck::checkArguments"]["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
3916 |
$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
|
|
|
3917 |
#$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
3918 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("command","fileArgu");
|
| 226 |
liveuser |
3919 |
#$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
3920 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("array","string");
|
|
|
3921 |
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
3922 |
$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
3923 |
#可以省略的參數:
|
|
|
3924 |
#$conf["variableCheck::checkArguments"]["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true"。
|
|
|
3925 |
#$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
|
|
|
3926 |
#$conf["variableCheck::checkArguments"]["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
|
|
3927 |
$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("enablePrintDescription","printDescription","argu","escapeshellarg","username","password","web","wait");
|
| 226 |
liveuser |
3928 |
#$conf["variableCheck::checkArguments"]["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
3929 |
$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","array","array","string","array","array","string","string");
|
|
|
3930 |
#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,"null"代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
|
|
3931 |
$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("false",$conf["command"],null,"false",null,null,"false","false");
|
|
|
3932 |
#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
3933 |
#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"][]=array();
|
|
|
3934 |
$checkResult=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
|
|
|
3935 |
unset($conf["variableCheck::checkArguments"]);
|
| 226 |
liveuser |
3936 |
|
| 3 |
liveuser |
3937 |
#如果檢查參數失敗
|
|
|
3938 |
if($checkResult["status"]=="false"){
|
| 226 |
liveuser |
3939 |
|
| 3 |
liveuser |
3940 |
#設置執行失敗
|
|
|
3941 |
$result["status"]="false";
|
| 226 |
liveuser |
3942 |
|
| 3 |
liveuser |
3943 |
#設置執行錯誤
|
|
|
3944 |
$result["error"]=$checkResult;
|
| 226 |
liveuser |
3945 |
|
| 3 |
liveuser |
3946 |
#回傳結果
|
|
|
3947 |
return $result;
|
| 226 |
liveuser |
3948 |
|
| 3 |
liveuser |
3949 |
}#if end
|
| 226 |
liveuser |
3950 |
|
| 3 |
liveuser |
3951 |
#如果檢查參數不通過
|
|
|
3952 |
if($checkResult["passed"]=="false"){
|
| 226 |
liveuser |
3953 |
|
| 3 |
liveuser |
3954 |
#設置執行失敗
|
|
|
3955 |
$result["status"]="false";
|
| 226 |
liveuser |
3956 |
|
| 3 |
liveuser |
3957 |
#設置執行錯誤
|
|
|
3958 |
$result["error"]=$checkResult;
|
| 226 |
liveuser |
3959 |
|
| 3 |
liveuser |
3960 |
#回傳結果
|
|
|
3961 |
return $result;
|
| 226 |
liveuser |
3962 |
|
| 3 |
liveuser |
3963 |
}#if end
|
| 226 |
liveuser |
3964 |
|
| 3 |
liveuser |
3965 |
#初始化儲存要寫入到腳本檔案裡面的內容
|
|
|
3966 |
$cmdStr="";
|
| 226 |
liveuser |
3967 |
|
| 3 |
liveuser |
3968 |
#初始化儲存暫存輸出結果的檔案
|
| 226 |
liveuser |
3969 |
$scriptOutFileArray=array();
|
|
|
3970 |
|
| 3 |
liveuser |
3971 |
#針對每個 $conf["command"]
|
|
|
3972 |
for($i=0;$i<count($conf["command"]);$i++){
|
| 226 |
liveuser |
3973 |
|
| 3 |
liveuser |
3974 |
#如果有設定 $conf["enablePrintDescription"] 為 "true"
|
|
|
3975 |
if($conf["enablePrintDescription"]=="true"){
|
| 226 |
liveuser |
3976 |
|
| 3 |
liveuser |
3977 |
#如果存在 $conf["printDescription"][$i]
|
|
|
3978 |
if(isset($conf["printDescription"][$i])){
|
| 226 |
liveuser |
3979 |
|
| 3 |
liveuser |
3980 |
#儲存 印出描述與換行 的指令
|
|
|
3981 |
$cmdArray[]="echo ".$conf["printDescription"][$i]." \r\n";
|
| 226 |
liveuser |
3982 |
|
| 3 |
liveuser |
3983 |
}#if end
|
| 226 |
liveuser |
3984 |
|
| 3 |
liveuser |
3985 |
#如果存在 $conf["argu"]
|
|
|
3986 |
if(isset($conf["argu"])){
|
| 226 |
liveuser |
3987 |
|
| 3 |
liveuser |
3988 |
#如果存在 $conf["argu"][$i]
|
|
|
3989 |
if(isset($conf["argu"][$i])){
|
| 226 |
liveuser |
3990 |
|
| 3 |
liveuser |
3991 |
#如果 $conf["argu"][$i] 形態為 "array"
|
|
|
3992 |
if(gettype($conf["argu"][$i])=="array"){
|
| 226 |
liveuser |
3993 |
|
|
|
3994 |
#參數 $conf["argu"][$i] 有幾個元素就執行幾次
|
| 3 |
liveuser |
3995 |
for($j=0;$j<count($conf["argu"][$i]);$j++){
|
| 226 |
liveuser |
3996 |
|
| 3 |
liveuser |
3997 |
#如果 $conf["escapeshellarg"] 為 "true"
|
|
|
3998 |
if($conf["escapeshellarg"]=="true"){
|
| 226 |
liveuser |
3999 |
|
| 3 |
liveuser |
4000 |
#過濾參數
|
|
|
4001 |
$conf["argu"][$i]=escapeshellarg($conf["argu"][$i]);
|
| 226 |
liveuser |
4002 |
|
| 3 |
liveuser |
4003 |
}#if end
|
| 226 |
liveuser |
4004 |
|
| 3 |
liveuser |
4005 |
#附加參數
|
|
|
4006 |
$conf["command"][$i]=$conf["command"][$i]." ".$conf["argu"][$i];
|
| 226 |
liveuser |
4007 |
|
| 3 |
liveuser |
4008 |
}#for end
|
| 226 |
liveuser |
4009 |
|
| 3 |
liveuser |
4010 |
}#if end
|
| 226 |
liveuser |
4011 |
|
| 3 |
liveuser |
4012 |
}#if end
|
| 226 |
liveuser |
4013 |
|
| 3 |
liveuser |
4014 |
}#if end
|
| 226 |
liveuser |
4015 |
|
| 3 |
liveuser |
4016 |
}#if end
|
| 226 |
liveuser |
4017 |
|
| 3 |
liveuser |
4018 |
#如果 $conf["username"] 存在
|
|
|
4019 |
if(isset($conf["username"])){
|
| 226 |
liveuser |
4020 |
|
| 3 |
liveuser |
4021 |
#如果 $conf["username"][$i] 存在
|
|
|
4022 |
if(isset($conf["username"][$i])){
|
| 226 |
liveuser |
4023 |
|
| 3 |
liveuser |
4024 |
#如果 $conf["password"] 存在
|
|
|
4025 |
if(isset($conf["password"])){
|
| 226 |
liveuser |
4026 |
|
| 3 |
liveuser |
4027 |
#如果 $conf["password"][$i] 存在
|
|
|
4028 |
if(isset($conf["password"][$i])){
|
| 226 |
liveuser |
4029 |
|
| 3 |
liveuser |
4030 |
#加上使用特定使用者身份的語法與驗證的密碼
|
|
|
4031 |
$conf["command"][$i]="echo ".$conf["password"][$i]." | su ".$conf["username"][$i]." -c'".$conf["command"][$i]."'";
|
| 226 |
liveuser |
4032 |
|
| 3 |
liveuser |
4033 |
}#if end
|
| 226 |
liveuser |
4034 |
|
| 3 |
liveuser |
4035 |
}#if end
|
| 226 |
liveuser |
4036 |
|
| 3 |
liveuser |
4037 |
#反之
|
|
|
4038 |
else{
|
| 226 |
liveuser |
4039 |
|
| 3 |
liveuser |
4040 |
#加上使用特定使用者身份的語法
|
|
|
4041 |
$conf["command"][$i]="su ".$conf["username"][$i]." -c'".$conf["command"][$i]."'";
|
| 226 |
liveuser |
4042 |
|
| 3 |
liveuser |
4043 |
}#else end
|
| 226 |
liveuser |
4044 |
|
| 3 |
liveuser |
4045 |
}#if end
|
| 226 |
liveuser |
4046 |
|
| 3 |
liveuser |
4047 |
}#if end
|
| 226 |
liveuser |
4048 |
|
| 3 |
liveuser |
4049 |
#如果不等待前一個指令
|
|
|
4050 |
if($conf["wait"]==="false"){
|
| 226 |
liveuser |
4051 |
|
| 3 |
liveuser |
4052 |
#加上 & 到 $conf["command"][$i]
|
|
|
4053 |
$conf["command"][$i]=$conf["command"][$i]." &".PHP_EOL;
|
| 226 |
liveuser |
4054 |
|
| 3 |
liveuser |
4055 |
}#if end
|
| 226 |
liveuser |
4056 |
|
| 3 |
liveuser |
4057 |
#反之要等待前一個指令
|
|
|
4058 |
else{
|
| 226 |
liveuser |
4059 |
|
| 3 |
liveuser |
4060 |
#僅加上換行
|
|
|
4061 |
$conf["command"][$i]=$conf["command"][$i]." ".PHP_EOL;
|
| 226 |
liveuser |
4062 |
|
| 3 |
liveuser |
4063 |
}#else end
|
| 226 |
liveuser |
4064 |
|
| 3 |
liveuser |
4065 |
#儲存要執行的指令
|
|
|
4066 |
$cmdStr=$cmdStr.$conf["command"][$i];
|
| 226 |
liveuser |
4067 |
|
| 3 |
liveuser |
4068 |
}#for end
|
| 226 |
liveuser |
4069 |
|
| 3 |
liveuser |
4070 |
#debug
|
| 226 |
liveuser |
4071 |
#var_dump($cmdStr);
|
|
|
4072 |
|
| 3 |
liveuser |
4073 |
#建立用來儲存指令的腳本檔案
|
|
|
4074 |
#涵式說明:
|
|
|
4075 |
#將字串寫入到檔案
|
|
|
4076 |
#回傳的結果:
|
|
|
4077 |
#$result["status"],"true"表示檔案寫入成功,"false"表示檔案寫入失敗.
|
|
|
4078 |
#$result["error"],錯誤訊息陣列.
|
|
|
4079 |
#$result["function"],當前執行的函數名稱.
|
|
|
4080 |
#$result["fileInfo"],實際上寫入的檔案資訊陣列.
|
|
|
4081 |
#$result["fileInfo"]["createdFileName"],建立好的檔案名稱.
|
|
|
4082 |
#$result["fileInfo"]["createdFilePath"],檔案建立的路徑.
|
|
|
4083 |
#$result["fileInfo"]["createdFilePathAndName"].建立好的檔案名稱與路徑.
|
|
|
4084 |
#必填參數::
|
|
|
4085 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
|
|
|
4086 |
$conf["fileAccess::writeTextIntoFile"]["fileArgu"]=$conf["fileArgu"];
|
|
|
4087 |
#可省略參數:
|
|
|
4088 |
#$conf["fileName"],字串,爲要編輯的檔案名稱,預設為隨機產生的檔案名稱.
|
|
|
4089 |
#$conf["fileName"]="";
|
|
|
4090 |
#$conf["inputString"],字串,爲要寫入到裏面的內容,若要每筆資料寫入後換行,則可以在字串內容後面加上 \r\n 即可,預設為"".
|
|
|
4091 |
$conf["fileAccess::writeTextIntoFile"]["inputString"]=$cmdStr;#爲要寫入到裏面的內容,若要每筆資料寫入後換行,則可以在字串內容後面加上 \r\n 即可.
|
|
|
4092 |
#$conf["writeMethod"],字串,爲檔案撰寫的方式,可省略,是複寫'a'還是,重新寫入'w',預設爲'w',重新寫入.
|
|
|
4093 |
#$conf["writeMethod"]="a";
|
|
|
4094 |
#$conf["checkRepeat"],字串,"true"代表建立檔案之前要先檢查檔案是否存在,若存在則在原名稱後面加上從(1)開始的編號.
|
|
|
4095 |
#$conf["checkRepeat"]="";
|
|
|
4096 |
#$conf["filenameExtensionStartPoint"],字串,檔案的副檔名是從倒數第幾個小數點(dot)開始,預設為"1",最後一個小數點,必須與$conf["checkRepeat"]搭配才會生效.
|
|
|
4097 |
#$conf["filenameExtensionStartPoint"]="";
|
|
|
4098 |
#$conf["repeatNameRule"],字串,遇到相同名稱的檔案要如何加上識別的編號,編號用「\$i」表示,預設為"(\$i)",必須與$conf["checkRepeat"]搭配才會生效.
|
|
|
4099 |
#$conf["repeatNameRule"]="";
|
|
|
4100 |
#$conf["web"],檔案是否位於網站上"true",若是在檔案系統則為"false",預設為"true".
|
|
|
4101 |
$conf["fileAccess::writeTextIntoFile"]["web"]="false";
|
|
|
4102 |
$createShellScriptFile=fileAccess::writeTextIntoFile($conf["fileAccess::writeTextIntoFile"]);
|
|
|
4103 |
unset($conf["fileAccess::writeTextIntoFile"]);
|
| 226 |
liveuser |
4104 |
|
| 3 |
liveuser |
4105 |
#debug
|
|
|
4106 |
#var_dump($createShellScriptFile);
|
|
|
4107 |
#exit;
|
| 226 |
liveuser |
4108 |
|
| 3 |
liveuser |
4109 |
#如果建立腳本檔案失敗
|
|
|
4110 |
if($createShellScriptFile["status"]=="false"){
|
| 226 |
liveuser |
4111 |
|
| 3 |
liveuser |
4112 |
#設置執行不正常
|
|
|
4113 |
$result["status"]="false";
|
| 226 |
liveuser |
4114 |
|
| 3 |
liveuser |
4115 |
#設置執行錯誤
|
|
|
4116 |
$result["error"]=$createShellScriptFile;
|
| 226 |
liveuser |
4117 |
|
| 3 |
liveuser |
4118 |
#回傳結果
|
|
|
4119 |
return $result;
|
| 226 |
liveuser |
4120 |
|
| 3 |
liveuser |
4121 |
}#if end
|
| 226 |
liveuser |
4122 |
|
| 3 |
liveuser |
4123 |
#移除腳本的指令
|
|
|
4124 |
$delScript="rm ".$createShellScriptFile["fileInfo"]["createdFilePathAndName"].";";
|
| 226 |
liveuser |
4125 |
|
| 3 |
liveuser |
4126 |
#附加寫入移除腳本的指令
|
|
|
4127 |
#回傳的結果:
|
|
|
4128 |
#$result["status"],"true"表示檔案寫入成功,"false"表示檔案寫入失敗.
|
|
|
4129 |
#$result["error"],錯誤訊息陣列.
|
|
|
4130 |
#$result["function"],當前執行的函數名稱.
|
|
|
4131 |
#$result["fileInfo"],實際上寫入的檔案資訊陣列.
|
|
|
4132 |
#$result["fileInfo"]["createdFileName"],建立好的檔案名稱.
|
|
|
4133 |
#$result["fileInfo"]["createdFilePath"],檔案建立的路徑.
|
|
|
4134 |
#$result["fileInfo"]["createdFilePathAndName"].建立好的檔案名稱與路徑.
|
|
|
4135 |
#必填參數::
|
|
|
4136 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
|
|
|
4137 |
$conf["fileAccess::writeTextIntoFile"]["fileArgu"]=$conf["fileArgu"];
|
|
|
4138 |
#可省略參數:
|
|
|
4139 |
$conf["fileAccess::writeTextIntoFile"]["fileName"]=$createShellScriptFile["fileInfo"]["createdFilePathAndName"];#爲要編輯的檔案名稱
|
|
|
4140 |
$conf["fileAccess::writeTextIntoFile"]["inputString"]=$delScript;#爲要寫入到裏面的內容,若要每筆資料寫入後換行,則可以在字串內容後面加上 \r\n 即可.
|
|
|
4141 |
#$conf["writeMethod"],字串,爲檔案撰寫的方式,可省略,是複寫'a'還是,重新寫入'w',預設爲'w',重新寫入.
|
|
|
4142 |
$conf["fileAccess::writeTextIntoFile"]["writeMethod"]="a";
|
|
|
4143 |
#$conf["checkRepeat"],字串,"true"代表建立檔案之前要先檢查檔案是否存在,若存在則在原名稱後面加上從(1)開始的編號.
|
|
|
4144 |
#$conf["checkRepeat"]="";
|
|
|
4145 |
#$conf["filenameExtensionStartPoint"],字串,檔案的副檔名是從倒數第幾個小數點(dot)開始,預設為"1",最後一個小數點,必須與$conf["checkRepeat"]搭配才會生效.
|
|
|
4146 |
#$conf["filenameExtensionStartPoint"]="";
|
|
|
4147 |
#$conf["repeatNameRule"],字串,遇到相同名稱的檔案要如何加上識別的編號,編號用「\$i」表示,預設為"(\$i)",必須與$conf["checkRepeat"]搭配才會生效.
|
|
|
4148 |
#$conf["repeatNameRule"]="";
|
|
|
4149 |
#$conf["web"],檔案是否位於網站上"true",若是在檔案系統則為"false",預設為"true".
|
|
|
4150 |
$conf["fileAccess::writeTextIntoFile"]["web"]="false";
|
|
|
4151 |
$createShellScriptFile=fileAccess::writeTextIntoFile($conf["fileAccess::writeTextIntoFile"]);
|
|
|
4152 |
unset($conf["fileAccess::writeTextIntoFile"]);
|
| 226 |
liveuser |
4153 |
|
| 3 |
liveuser |
4154 |
#如果建立腳本檔案失敗
|
|
|
4155 |
if($createShellScriptFile["status"]=="false"){
|
| 226 |
liveuser |
4156 |
|
| 3 |
liveuser |
4157 |
#設置執行不正常
|
|
|
4158 |
$result["status"]="false";
|
| 226 |
liveuser |
4159 |
|
| 3 |
liveuser |
4160 |
#設置執行錯誤
|
|
|
4161 |
$result["error"]=$createShellScriptFile;
|
| 226 |
liveuser |
4162 |
|
| 3 |
liveuser |
4163 |
#回傳結果
|
|
|
4164 |
return $result;
|
| 226 |
liveuser |
4165 |
|
| 3 |
liveuser |
4166 |
}#if end
|
| 226 |
liveuser |
4167 |
|
| 3 |
liveuser |
4168 |
#debug
|
|
|
4169 |
#exec("echo '".$createShellScriptFile["fileInfo"]["createdFilePathAndName"]."' > /var/www/html/latest/tmp/debug1.log",$resultArray,$execStatus);
|
|
|
4170 |
#exec("cat '".$createShellScriptFile["fileInfo"]["createdFilePathAndName"]."' > /var/www/html/latest/tmp/debug2.log",$resultArray,$execStatus);
|
| 226 |
liveuser |
4171 |
|
| 3 |
liveuser |
4172 |
#執行腳本程式,將輸出結果儲存在 $resultArray 陣列裏面
|
|
|
4173 |
#exec("sh ".$createShellScriptFile["fileInfo"]["createdFilePathAndName"]." > /dev/null 2>&1 &",$resultArray,$execStatus);
|
| 226 |
liveuser |
4174 |
|
| 3 |
liveuser |
4175 |
#函式說明:
|
|
|
4176 |
#呼叫shell執行系統命令,並取得回傳的內容.
|
|
|
4177 |
#回傳結果:
|
|
|
4178 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
4179 |
#$result["error"],錯誤訊息陣列.
|
|
|
4180 |
#$result["function"],當前執行的函數名稱.
|
|
|
4181 |
#$result["argu"],使用的參數.
|
|
|
4182 |
#$result["cmd"],執行的指令內容.
|
|
|
4183 |
#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
4184 |
#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
|
|
|
4185 |
#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
4186 |
#$result["running"],是否還在執行.
|
|
|
4187 |
#$result["pid"],pid.
|
|
|
4188 |
#$result["statusCode"],執行結束後的代碼.
|
|
|
4189 |
#必填參數:
|
|
|
4190 |
#$conf["command"],字串,要執行的指令與.
|
|
|
4191 |
$conf["external::callShell"]["command"]="sh";
|
|
|
4192 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
|
|
4193 |
$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];
|
|
|
4194 |
#可省略參數:
|
|
|
4195 |
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
|
|
|
4196 |
$conf["external::callShell"]["argu"]=array($createShellScriptFile["fileInfo"]["createdFilePathAndName"],">","/dev/null","2>&1","&");
|
|
|
4197 |
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
|
|
|
4198 |
#$conf["arguIsAddr"]=array();
|
|
|
4199 |
#$conf["plainArgu"],字串陣列,哪幾個參數不要加上"",元算若為"true"則代表不用包;反之"false"則代表要包.
|
|
|
4200 |
#$conf["external::callShell"]["plainArgu"]=array("false","true","true","true","true");
|
|
|
4201 |
#$conf["useApostrophe"],字串陣列,如果有需要包住,則用「'」,而非「"」處理.前者為"true";後者為"false".
|
|
|
4202 |
#$conf["external::callShell"]["useApostrophe"]=array("true","false","false","false","false");
|
|
|
4203 |
#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
|
|
|
4204 |
#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
|
|
|
4205 |
#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
|
|
|
4206 |
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
|
|
|
4207 |
#$conf["enablePrintDescription"]="true";
|
|
|
4208 |
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
|
|
|
4209 |
#$conf["printDescription"]="";
|
|
|
4210 |
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".
|
|
|
4211 |
$conf["external::callShell"]["escapeshellarg"]="true";
|
|
|
4212 |
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
|
|
|
4213 |
#$conf["username"]="";
|
|
|
4214 |
#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
|
|
4215 |
#$conf["password"]="";
|
|
|
4216 |
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
|
|
|
4217 |
#$conf["useScript"]="";
|
|
|
4218 |
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
|
|
|
4219 |
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
|
|
|
4220 |
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
|
|
|
4221 |
#$conf["inBackGround"]="";
|
|
|
4222 |
#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
|
|
|
4223 |
#$conf["getErr"]="false";
|
|
|
4224 |
#參考資料:
|
|
|
4225 |
#exec=>http://php.net/manual/en/function.exec.php
|
|
|
4226 |
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
|
|
|
4227 |
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
|
|
|
4228 |
#備註:
|
|
|
4229 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
|
|
|
4230 |
$callShell=external::callShell($conf["external::callShell"]);
|
|
|
4231 |
unset($conf["external::callShell"]);
|
| 226 |
liveuser |
4232 |
|
| 3 |
liveuser |
4233 |
$fsock=fopen("/var/www/html/latest/tmp/debug","w");
|
|
|
4234 |
fwrite($fsock,print_r($callShell,true));
|
|
|
4235 |
fclose($fsock);
|
| 226 |
liveuser |
4236 |
|
| 3 |
liveuser |
4237 |
#如果執行失敗
|
|
|
4238 |
if($callShell["status"]==="false"){
|
| 226 |
liveuser |
4239 |
|
| 3 |
liveuser |
4240 |
#設置執行不正常
|
|
|
4241 |
$result["status"]="false";
|
| 226 |
liveuser |
4242 |
|
| 3 |
liveuser |
4243 |
#設置執行錯誤
|
|
|
4244 |
$result["error"]=$callShell;
|
| 226 |
liveuser |
4245 |
|
| 3 |
liveuser |
4246 |
#回傳結果
|
|
|
4247 |
return $result;
|
| 226 |
liveuser |
4248 |
|
| 3 |
liveuser |
4249 |
}#if end
|
| 226 |
liveuser |
4250 |
|
| 3 |
liveuser |
4251 |
#設置執行正常
|
|
|
4252 |
$result["status"]="true";
|
| 226 |
liveuser |
4253 |
|
| 3 |
liveuser |
4254 |
#回傳結果
|
|
|
4255 |
return $result;
|
| 226 |
liveuser |
4256 |
|
| 3 |
liveuser |
4257 |
}#function multiThreadsShell end
|
| 226 |
liveuser |
4258 |
|
| 3 |
liveuser |
4259 |
/*
|
|
|
4260 |
#函式說明:
|
|
|
4261 |
#呼叫shell執行系統命令,中止執行中的pid.
|
|
|
4262 |
#回傳結果:
|
|
|
4263 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
4264 |
#$result["error"],錯誤訊息陣列.
|
|
|
4265 |
#$result["function"],當前執行的函數名稱.
|
|
|
4266 |
#$result["warning"],警告訊息陣列.
|
|
|
4267 |
#$result["cmd"],執行的指令內容.
|
|
|
4268 |
#$result["output"],爲執行完二元碼後的輸出陣列.
|
|
|
4269 |
#$result["runing"],程式是否還在執行.
|
|
|
4270 |
#必填參數:
|
|
|
4271 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
| 226 |
liveuser |
4272 |
$conf["fileArgu"]=__FILE__;
|
| 3 |
liveuser |
4273 |
#$conf["pid"],字串,執行序的pid.
|
|
|
4274 |
$conf["pid"]="";
|
|
|
4275 |
#$conf["cmd"],字串,要中止的指令關鍵字.
|
| 226 |
liveuser |
4276 |
$conf["cmd"]="";
|
| 3 |
liveuser |
4277 |
#可省略參數:
|
|
|
4278 |
#無.
|
|
|
4279 |
#參考資料:
|
|
|
4280 |
#http://php.net/manual/en/function.exec.php
|
|
|
4281 |
#備註:
|
|
|
4282 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,透過su使用root身份可能會被selinux阻擋.
|
| 130 |
liveuser |
4283 |
*/
|
| 3 |
liveuser |
4284 |
public static function killProcess(&$conf){
|
| 226 |
liveuser |
4285 |
|
| 3 |
liveuser |
4286 |
#初始化要回傳的結果
|
|
|
4287 |
$result=array();
|
|
|
4288 |
|
|
|
4289 |
#取得當前執行的函數名稱
|
|
|
4290 |
$result["function"]=__FUNCTION__;
|
|
|
4291 |
|
|
|
4292 |
#如果沒有參數
|
|
|
4293 |
if(func_num_args()==0){
|
| 226 |
liveuser |
4294 |
|
| 3 |
liveuser |
4295 |
#設置執行失敗
|
|
|
4296 |
$result["status"]="false";
|
| 226 |
liveuser |
4297 |
|
| 3 |
liveuser |
4298 |
#設置執行錯誤訊息
|
|
|
4299 |
$result["error"]="函數".$result["function"]."需要參數";
|
| 226 |
liveuser |
4300 |
|
| 3 |
liveuser |
4301 |
#回傳結果
|
|
|
4302 |
return $result;
|
| 226 |
liveuser |
4303 |
|
| 3 |
liveuser |
4304 |
}#if end
|
| 226 |
liveuser |
4305 |
|
| 3 |
liveuser |
4306 |
#取得參數
|
|
|
4307 |
$result["argu"]=$conf;
|
|
|
4308 |
|
|
|
4309 |
#如果 $conf 不為陣列
|
|
|
4310 |
if(gettype($conf)!=="array"){
|
| 226 |
liveuser |
4311 |
|
| 3 |
liveuser |
4312 |
#設置執行失敗
|
|
|
4313 |
$result["status"]="false";
|
| 226 |
liveuser |
4314 |
|
| 3 |
liveuser |
4315 |
#設置執行錯誤訊息
|
|
|
4316 |
$result["error"][]="\$conf變數須為陣列形態";
|
| 226 |
liveuser |
4317 |
|
| 3 |
liveuser |
4318 |
#如果傳入的參數為 null
|
|
|
4319 |
if($conf===null){
|
| 226 |
liveuser |
4320 |
|
| 3 |
liveuser |
4321 |
#設置執行錯誤訊息
|
|
|
4322 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
| 226 |
liveuser |
4323 |
|
| 3 |
liveuser |
4324 |
}#if end
|
|
|
4325 |
|
|
|
4326 |
#回傳結果
|
|
|
4327 |
return $result;
|
| 226 |
liveuser |
4328 |
|
| 3 |
liveuser |
4329 |
}#if end
|
| 226 |
liveuser |
4330 |
|
| 3 |
liveuser |
4331 |
#檢查參數
|
|
|
4332 |
#函式說明:
|
|
|
4333 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
4334 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
4335 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
4336 |
#$result["function"],當前執行的函式名稱.
|
|
|
4337 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
4338 |
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
4339 |
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
4340 |
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
4341 |
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
|
|
|
4342 |
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
|
|
|
4343 |
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
|
|
|
4344 |
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
|
|
|
4345 |
#必填寫的參數:
|
|
|
4346 |
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
4347 |
$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
|
|
|
4348 |
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
4349 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("pid","cmd","fileArgu");
|
|
|
4350 |
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
|
|
|
4351 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string","string");
|
|
|
4352 |
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
4353 |
$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
4354 |
#可以省略的參數:
|
|
|
4355 |
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
|
|
|
4356 |
#$conf["canBeEmptyString"]="false";
|
|
|
4357 |
#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
|
|
|
4358 |
#$conf["canNotBeEmpty"]=array();
|
|
|
4359 |
#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
|
|
|
4360 |
#$conf["canBeEmpty"]=array();
|
|
|
4361 |
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
|
|
|
4362 |
#$conf["skipableVariableCanNotBeEmpty"]=array();
|
|
|
4363 |
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
|
|
4364 |
#$conf["skipableVariableName"]=array();
|
| 226 |
liveuser |
4365 |
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
4366 |
#$conf["skipableVariableType"]=array();
|
|
|
4367 |
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
|
|
4368 |
#$conf["skipableVarDefaultValue"]=array("");
|
|
|
4369 |
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
4370 |
#$conf["arrayCountEqualCheck"][]=array();
|
|
|
4371 |
#參考資料來源:
|
|
|
4372 |
#array_keys=>http://php.net/manual/en/function.array-keys.php
|
|
|
4373 |
#建議:
|
|
|
4374 |
#增加可省略參數全部不能為空字串或空陣列的參數功能.
|
|
|
4375 |
$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
|
|
|
4376 |
unset($conf["variableCheck::checkArguments"]);
|
| 226 |
liveuser |
4377 |
|
| 3 |
liveuser |
4378 |
#如果檢查參數失敗
|
|
|
4379 |
if($checkArguments["status"]==="false"){
|
| 226 |
liveuser |
4380 |
|
| 3 |
liveuser |
4381 |
#設置執行失敗
|
|
|
4382 |
$result["status"]="false";
|
| 226 |
liveuser |
4383 |
|
| 3 |
liveuser |
4384 |
#設置執行錯誤訊息
|
|
|
4385 |
$result["error"]=$checkArguments;
|
| 226 |
liveuser |
4386 |
|
| 3 |
liveuser |
4387 |
#回傳結果
|
|
|
4388 |
return $result;
|
| 226 |
liveuser |
4389 |
|
| 3 |
liveuser |
4390 |
}#if end
|
| 226 |
liveuser |
4391 |
|
| 3 |
liveuser |
4392 |
#如果參數檢查不過
|
|
|
4393 |
if($checkArguments["passed"]==="false"){
|
| 226 |
liveuser |
4394 |
|
| 3 |
liveuser |
4395 |
#設置執行失敗
|
|
|
4396 |
$result["status"]="false";
|
| 226 |
liveuser |
4397 |
|
| 3 |
liveuser |
4398 |
#設置執行錯誤訊息
|
|
|
4399 |
$result["error"]=$checkArguments;
|
| 226 |
liveuser |
4400 |
|
| 3 |
liveuser |
4401 |
#回傳結果
|
|
|
4402 |
return $result;
|
| 226 |
liveuser |
4403 |
|
| 3 |
liveuser |
4404 |
}#if end
|
| 226 |
liveuser |
4405 |
|
| 3 |
liveuser |
4406 |
#函式說明:
|
| 226 |
liveuser |
4407 |
#檢查程序是否還在執行
|
| 3 |
liveuser |
4408 |
#回傳結果
|
|
|
4409 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
4410 |
#$result["error"],錯誤訊息陣列.
|
| 226 |
liveuser |
4411 |
#$result["function"],當前執行的函數名稱.
|
| 3 |
liveuser |
4412 |
#$result["exist"],程序是否存在,"true"代表存在,"false"代表不存在.
|
|
|
4413 |
#必填參數:
|
|
|
4414 |
#$conf["pid"],字串,程序的id.
|
|
|
4415 |
$conf["external::processStatus"]["pid"]=$conf["pid"];
|
|
|
4416 |
#$conf["cmd"],字串,程序的指令.
|
|
|
4417 |
$conf["external::processStatus"]["cmd"]=$conf["cmd"];
|
|
|
4418 |
#$conf["fileArgu"],字串,__FILE__的內容,__FILE__的內容.
|
|
|
4419 |
$conf["external::processStatus"]["fileArgu"]=$conf["fileArgu"];
|
|
|
4420 |
$processStatus=external::processStatus($conf["external::processStatus"]);
|
|
|
4421 |
unset($conf["external::processStatus"]);
|
| 226 |
liveuser |
4422 |
|
| 3 |
liveuser |
4423 |
#如果檢查程序是否還在執行失敗
|
|
|
4424 |
if($processStatus["status"]==="false"){
|
| 226 |
liveuser |
4425 |
|
| 3 |
liveuser |
4426 |
#設置執行失敗
|
|
|
4427 |
$result["status"]="false";
|
| 226 |
liveuser |
4428 |
|
| 3 |
liveuser |
4429 |
#設置執行錯誤訊息
|
|
|
4430 |
$result["error"]=$processStatus;
|
| 226 |
liveuser |
4431 |
|
| 3 |
liveuser |
4432 |
#回傳結果
|
|
|
4433 |
return $result;
|
| 226 |
liveuser |
4434 |
|
| 3 |
liveuser |
4435 |
}#if end
|
| 226 |
liveuser |
4436 |
|
| 3 |
liveuser |
4437 |
#如果程序沒有在執行
|
|
|
4438 |
if($processStatus["exist"]==="false"){
|
| 226 |
liveuser |
4439 |
|
| 3 |
liveuser |
4440 |
#設置執行成功
|
|
|
4441 |
$result["status"]="true";
|
| 226 |
liveuser |
4442 |
|
| 3 |
liveuser |
4443 |
#設置警告訊息
|
|
|
4444 |
$result["warning"][]="要中止的程序不存在";
|
| 226 |
liveuser |
4445 |
|
| 3 |
liveuser |
4446 |
#回傳結果
|
|
|
4447 |
return $result;
|
| 226 |
liveuser |
4448 |
|
| 3 |
liveuser |
4449 |
}#if end
|
| 226 |
liveuser |
4450 |
|
|
|
4451 |
#中止程式
|
| 3 |
liveuser |
4452 |
#函式說明:
|
|
|
4453 |
#呼叫shell執行系統命令,並取得回傳的內容.
|
|
|
4454 |
#回傳結果:
|
|
|
4455 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
4456 |
#$result["error"],錯誤訊息陣列.
|
|
|
4457 |
#$result["function"],當前執行的函數名稱.
|
|
|
4458 |
#$result["cmd"],執行的指令內容.
|
|
|
4459 |
#$result["output"],爲執行完二元碼後的輸出陣列.
|
|
|
4460 |
#必填參數:
|
|
|
4461 |
#$conf["command"],字串,要執行的指令與.
|
|
|
4462 |
$conf["external::callShell"]["command"]="kill";
|
|
|
4463 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
| 226 |
liveuser |
4464 |
$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];
|
| 3 |
liveuser |
4465 |
#可省略參數:
|
|
|
4466 |
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
|
|
|
4467 |
$conf["external::callShell"]["argu"]=array($conf["pid"]);
|
|
|
4468 |
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
|
|
|
4469 |
#$conf["arguIsAddr"]=array();
|
|
|
4470 |
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
|
|
|
4471 |
#$conf["enablePrintDescription"]="true";
|
|
|
4472 |
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容.
|
|
|
4473 |
#$conf["printDescription"]="";
|
|
|
4474 |
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".
|
|
|
4475 |
$conf["external::callShell"]["escapeshellarg"]="true";
|
|
|
4476 |
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
|
|
|
4477 |
#$conf["username"]="";
|
|
|
4478 |
#$conf["password"],字串,與$conf["username"]搭配的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
|
|
4479 |
#$conf["password"]="";
|
|
|
4480 |
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
|
|
|
4481 |
#$conf["useScript"]="";
|
|
|
4482 |
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
|
|
|
4483 |
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
|
|
|
4484 |
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
|
|
|
4485 |
#$conf["inBackGround"]="";
|
|
|
4486 |
#備註:
|
|
|
4487 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
|
|
|
4488 |
#參考資料:
|
|
|
4489 |
#exec=>http://php.net/manual/en/function.exec.php
|
|
|
4490 |
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
|
|
|
4491 |
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
|
|
|
4492 |
$callShell=external::callShell($conf["external::callShell"]);
|
|
|
4493 |
unset($conf["external::callShell"]);
|
| 226 |
liveuser |
4494 |
|
| 3 |
liveuser |
4495 |
#如果執行程式失敗
|
|
|
4496 |
if($callShell["status"]==="false"){
|
| 226 |
liveuser |
4497 |
|
| 3 |
liveuser |
4498 |
#設置執行失敗
|
|
|
4499 |
$result["status"]="false";
|
| 226 |
liveuser |
4500 |
|
| 3 |
liveuser |
4501 |
#設置執行錯誤訊息
|
|
|
4502 |
$result["error"]=$callShell;
|
| 226 |
liveuser |
4503 |
|
| 3 |
liveuser |
4504 |
#回傳結果
|
|
|
4505 |
return $result;
|
| 226 |
liveuser |
4506 |
|
| 3 |
liveuser |
4507 |
}#if end
|
| 226 |
liveuser |
4508 |
|
| 3 |
liveuser |
4509 |
#檢查程序是否已經中止
|
|
|
4510 |
#函式說明:
|
| 226 |
liveuser |
4511 |
#檢查程序是否還在執行
|
| 3 |
liveuser |
4512 |
#回傳結果
|
|
|
4513 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
4514 |
#$result["error"],錯誤訊息陣列.
|
| 226 |
liveuser |
4515 |
#$result["function"],當前執行的函數名稱.
|
| 3 |
liveuser |
4516 |
#$result["exist"],程序是否存在,"true"代表存在,"false"代表不存在.
|
|
|
4517 |
#必填參數:
|
|
|
4518 |
#$conf["pid"],字串,程序的id.
|
|
|
4519 |
$conf["external::processStatus"]["pid"]=$conf["pid"];
|
|
|
4520 |
#$conf["cmd"],字串,程序的指令.
|
|
|
4521 |
$conf["external::processStatus"]["cmd"]=$conf["cmd"];
|
|
|
4522 |
#$conf["fileArgu"],字串,__FILE__的內容,__FILE__的內容.
|
|
|
4523 |
$conf["external::processStatus"]["fileArgu"]=$conf["fileArgu"];
|
|
|
4524 |
$processStatus=external::processStatus($conf["external::processStatus"]);
|
|
|
4525 |
unset($conf["external::processStatus"]);
|
| 226 |
liveuser |
4526 |
|
| 3 |
liveuser |
4527 |
#如果檢查程序是否還在執行失敗
|
|
|
4528 |
if($processStatus["status"]==="false"){
|
| 226 |
liveuser |
4529 |
|
| 3 |
liveuser |
4530 |
#設置執行失敗
|
|
|
4531 |
$result["status"]="false";
|
| 226 |
liveuser |
4532 |
|
| 3 |
liveuser |
4533 |
#設置執行錯誤訊息
|
|
|
4534 |
$result["error"]=$processStatus;
|
| 226 |
liveuser |
4535 |
|
| 3 |
liveuser |
4536 |
#回傳結果
|
|
|
4537 |
return $result;
|
| 226 |
liveuser |
4538 |
|
| 3 |
liveuser |
4539 |
}#if end
|
| 226 |
liveuser |
4540 |
|
| 3 |
liveuser |
4541 |
#如果程序沒有在執行
|
|
|
4542 |
if($processStatus["exist"]==="false"){
|
| 226 |
liveuser |
4543 |
|
| 3 |
liveuser |
4544 |
#設置執行成功
|
| 226 |
liveuser |
4545 |
$result["status"]="true";
|
|
|
4546 |
|
| 3 |
liveuser |
4547 |
#回傳結果
|
|
|
4548 |
return $result;
|
| 226 |
liveuser |
4549 |
|
| 3 |
liveuser |
4550 |
}#if end
|
| 226 |
liveuser |
4551 |
|
| 3 |
liveuser |
4552 |
#反之
|
|
|
4553 |
else{
|
| 226 |
liveuser |
4554 |
|
| 3 |
liveuser |
4555 |
#再度嘗試中止程式
|
|
|
4556 |
return external::killProcess($conf);
|
|
|
4557 |
unset($conf);
|
| 226 |
liveuser |
4558 |
|
|
|
4559 |
}#else end
|
|
|
4560 |
|
| 3 |
liveuser |
4561 |
}#function killProcess end
|
| 226 |
liveuser |
4562 |
|
| 3 |
liveuser |
4563 |
/*
|
|
|
4564 |
#函式說明:
|
| 226 |
liveuser |
4565 |
#檢查目標程式是否執行超過時間,將之中止.
|
| 3 |
liveuser |
4566 |
#回傳結果
|
|
|
4567 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
4568 |
#$result["error"],錯誤訊息陣列.
|
| 226 |
liveuser |
4569 |
#$result["function"],當前執行的函數名稱.
|
| 3 |
liveuser |
4570 |
#$result["timeout"],程序是否timeout,"true"代表是,"false"代表不是.
|
|
|
4571 |
#必填參數:
|
|
|
4572 |
#$conf["pid"],字串,程序的id.
|
|
|
4573 |
$conf["pid"]=$conf["pid"];
|
|
|
4574 |
#$conf["cmd"],字串,程序的指令.
|
|
|
4575 |
$conf["cmd"]=$conf["cmd"];
|
|
|
4576 |
#$conf["fileArgu"],字串,__FILE__的內容,__FILE__的內容.
|
|
|
4577 |
$conf["fileArgu"]=$conf["fileArgu"];
|
|
|
4578 |
#$conf["timeout"],整數,多少時間過後要timeout,單位為秒.
|
|
|
4579 |
$conf["timeout"]=60;
|
|
|
4580 |
#可省略參數:
|
|
|
4581 |
#無.
|
|
|
4582 |
#參考資料:
|
|
|
4583 |
#無.
|
|
|
4584 |
#備註:
|
|
|
4585 |
#無.
|
|
|
4586 |
*/
|
|
|
4587 |
public static function timeout(&$conf=array()){
|
| 226 |
liveuser |
4588 |
|
| 3 |
liveuser |
4589 |
#初始化要回傳的結果
|
|
|
4590 |
$result=array();
|
|
|
4591 |
|
|
|
4592 |
#取得當前執行的函數名稱
|
|
|
4593 |
$result["function"]=__FUNCTION__;
|
|
|
4594 |
|
|
|
4595 |
#如果沒有參數
|
|
|
4596 |
if(func_num_args()==0){
|
| 226 |
liveuser |
4597 |
|
| 3 |
liveuser |
4598 |
#設置執行失敗
|
|
|
4599 |
$result["status"]="false";
|
| 226 |
liveuser |
4600 |
|
| 3 |
liveuser |
4601 |
#設置執行錯誤訊息
|
|
|
4602 |
$result["error"]="函數".$result["function"]."需要參數";
|
| 226 |
liveuser |
4603 |
|
| 3 |
liveuser |
4604 |
#回傳結果
|
|
|
4605 |
return $result;
|
| 226 |
liveuser |
4606 |
|
| 3 |
liveuser |
4607 |
}#if end
|
|
|
4608 |
|
|
|
4609 |
#取得參數
|
|
|
4610 |
$result["argu"]=$conf;
|
|
|
4611 |
|
|
|
4612 |
#如果 $conf 不為陣列
|
|
|
4613 |
if(gettype($conf)!=="array"){
|
| 226 |
liveuser |
4614 |
|
| 3 |
liveuser |
4615 |
#設置執行失敗
|
|
|
4616 |
$result["status"]="false";
|
| 226 |
liveuser |
4617 |
|
| 3 |
liveuser |
4618 |
#設置執行錯誤訊息
|
|
|
4619 |
$result["error"][]="\$conf變數須為陣列形態";
|
| 226 |
liveuser |
4620 |
|
| 3 |
liveuser |
4621 |
#如果傳入的參數為 null
|
|
|
4622 |
if($conf===null){
|
| 226 |
liveuser |
4623 |
|
| 3 |
liveuser |
4624 |
#設置執行錯誤訊息
|
|
|
4625 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
| 226 |
liveuser |
4626 |
|
| 3 |
liveuser |
4627 |
}#if end
|
|
|
4628 |
|
|
|
4629 |
#回傳結果
|
|
|
4630 |
return $result;
|
| 226 |
liveuser |
4631 |
|
| 3 |
liveuser |
4632 |
}#if end
|
| 226 |
liveuser |
4633 |
|
| 3 |
liveuser |
4634 |
#檢查參數
|
|
|
4635 |
#函式說明:
|
|
|
4636 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
4637 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
4638 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
4639 |
#$result["function"],當前執行的函式名稱.
|
|
|
4640 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
4641 |
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
4642 |
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
4643 |
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
4644 |
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
|
|
|
4645 |
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
|
|
|
4646 |
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
|
|
|
4647 |
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
|
|
|
4648 |
#必填寫的參數:
|
|
|
4649 |
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
4650 |
$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
|
|
|
4651 |
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
4652 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("cmd","fileArgu","pid","timeout");
|
|
|
4653 |
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
|
|
|
4654 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string","string","integer");
|
|
|
4655 |
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
4656 |
$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
4657 |
#可以省略的參數:
|
|
|
4658 |
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
|
|
|
4659 |
#$conf["canBeEmptyString"]="false";
|
|
|
4660 |
#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
|
|
|
4661 |
#$conf["canNotBeEmpty"]=array();
|
|
|
4662 |
#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
|
|
|
4663 |
#$conf["canBeEmpty"]=array();
|
|
|
4664 |
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
|
|
|
4665 |
#$conf["skipableVariableCanNotBeEmpty"]=array();
|
|
|
4666 |
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
|
|
4667 |
#$conf["skipableVariableName"]=array();
|
| 226 |
liveuser |
4668 |
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
4669 |
#$conf["skipableVariableType"]=array();
|
|
|
4670 |
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
|
|
4671 |
#$conf["skipableVarDefaultValue"]=array("");
|
|
|
4672 |
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
4673 |
#$conf["arrayCountEqualCheck"][]=array();
|
|
|
4674 |
#參考資料來源:
|
|
|
4675 |
#array_keys=>http://php.net/manual/en/function.array-keys.php
|
|
|
4676 |
#建議:
|
|
|
4677 |
#增加可省略參數全部不能為空字串或空陣列的參數功能.
|
|
|
4678 |
$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
|
|
|
4679 |
unset($conf["variableCheck::checkArguments"]);
|
| 226 |
liveuser |
4680 |
|
| 3 |
liveuser |
4681 |
#如果檢查參數失敗
|
|
|
4682 |
if($checkArguments["status"]==="false"){
|
| 226 |
liveuser |
4683 |
|
| 3 |
liveuser |
4684 |
#設置執行失敗
|
|
|
4685 |
$result["status"]="false";
|
| 226 |
liveuser |
4686 |
|
| 3 |
liveuser |
4687 |
#設置執行錯誤訊息
|
|
|
4688 |
$result["error"]=$checkArguments;
|
| 226 |
liveuser |
4689 |
|
| 3 |
liveuser |
4690 |
#回傳結果
|
|
|
4691 |
return $result;
|
| 226 |
liveuser |
4692 |
|
| 3 |
liveuser |
4693 |
}#if end
|
| 226 |
liveuser |
4694 |
|
| 3 |
liveuser |
4695 |
#如果參數檢查不過
|
|
|
4696 |
if($checkArguments["passed"]==="false"){
|
| 226 |
liveuser |
4697 |
|
| 3 |
liveuser |
4698 |
#設置執行失敗
|
|
|
4699 |
$result["status"]="false";
|
| 226 |
liveuser |
4700 |
|
| 3 |
liveuser |
4701 |
#設置執行錯誤訊息
|
|
|
4702 |
$result["error"]=$checkArguments;
|
| 226 |
liveuser |
4703 |
|
| 3 |
liveuser |
4704 |
#回傳結果
|
|
|
4705 |
return $result;
|
| 226 |
liveuser |
4706 |
|
| 3 |
liveuser |
4707 |
}#if end
|
| 226 |
liveuser |
4708 |
|
| 3 |
liveuser |
4709 |
#無窮迴圈
|
|
|
4710 |
while(true){
|
| 226 |
liveuser |
4711 |
|
| 3 |
liveuser |
4712 |
#函式說明:
|
| 226 |
liveuser |
4713 |
#檢查程序是否還在執行
|
| 3 |
liveuser |
4714 |
#回傳結果
|
|
|
4715 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
4716 |
#$result["error"],錯誤訊息陣列.
|
| 226 |
liveuser |
4717 |
#$result["function"],當前執行的函數名稱.
|
| 3 |
liveuser |
4718 |
#$result["exist"],程序是否存在,"true"代表存在,"false"代表不存在.
|
|
|
4719 |
#必填參數:
|
|
|
4720 |
#$conf["pid"],字串,程序的id.
|
|
|
4721 |
$conf["external::processStatus"]["pid"]=$conf["pid"];
|
|
|
4722 |
#$conf["cmd"],字串,程序的指令.
|
|
|
4723 |
$conf["external::processStatus"]["cmd"]=$conf["cmd"];
|
|
|
4724 |
#$conf["fileArgu"],字串,__FILE__的內容,__FILE__的內容.
|
|
|
4725 |
$conf["external::processStatus"]["fileArgu"]=$conf["fileArgu"];
|
|
|
4726 |
$processStatus=external::processStatus($conf["external::processStatus"]);
|
|
|
4727 |
unset($conf["external::processStatus"]);
|
| 226 |
liveuser |
4728 |
|
| 3 |
liveuser |
4729 |
#如果執行失敗
|
|
|
4730 |
if($processStatus["status"]==="false"){
|
| 226 |
liveuser |
4731 |
|
| 3 |
liveuser |
4732 |
#設置執行失敗
|
|
|
4733 |
$result["status"]="false";
|
| 226 |
liveuser |
4734 |
|
| 3 |
liveuser |
4735 |
#設置執行錯誤訊息
|
|
|
4736 |
$result["error"]=$processStatus;
|
| 226 |
liveuser |
4737 |
|
| 3 |
liveuser |
4738 |
#回傳結果
|
|
|
4739 |
return $result;
|
| 226 |
liveuser |
4740 |
|
| 3 |
liveuser |
4741 |
}#if end
|
| 226 |
liveuser |
4742 |
|
| 3 |
liveuser |
4743 |
#如果程序已經不在
|
|
|
4744 |
if($processStatus["exist"]==="false"){
|
| 226 |
liveuser |
4745 |
|
| 3 |
liveuser |
4746 |
#設置執行正常
|
|
|
4747 |
$result["status"]="true";
|
| 226 |
liveuser |
4748 |
|
| 3 |
liveuser |
4749 |
#設置沒有timeout
|
|
|
4750 |
$result["timeout"]="false";
|
| 226 |
liveuser |
4751 |
|
| 3 |
liveuser |
4752 |
#回傳結果
|
|
|
4753 |
return $result;
|
| 226 |
liveuser |
4754 |
|
| 3 |
liveuser |
4755 |
}#if end
|
| 226 |
liveuser |
4756 |
|
| 3 |
liveuser |
4757 |
#如果已經timeout
|
|
|
4758 |
if($conf["timeout"]===0){
|
| 226 |
liveuser |
4759 |
|
| 3 |
liveuser |
4760 |
#設置執行正常
|
|
|
4761 |
$result["status"]="true";
|
| 226 |
liveuser |
4762 |
|
| 3 |
liveuser |
4763 |
#設置有timeout
|
|
|
4764 |
$result["timeout"]="true";
|
| 226 |
liveuser |
4765 |
|
| 3 |
liveuser |
4766 |
#中止該程序
|
|
|
4767 |
#函式說明:
|
|
|
4768 |
#呼叫shell執行系統命令,中止執行中的pid.
|
|
|
4769 |
#回傳結果:
|
|
|
4770 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
4771 |
#$result["error"],錯誤訊息陣列.
|
|
|
4772 |
#$result["function"],當前執行的函數名稱.
|
|
|
4773 |
#$result["warning"],警告訊息陣列.
|
|
|
4774 |
#$result["cmd"],執行的指令內容.
|
|
|
4775 |
#$result["output"],爲執行完二元碼後的輸出陣列.
|
|
|
4776 |
#$result["runing"],程式是否還在執行.
|
|
|
4777 |
#必填的參數
|
|
|
4778 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
| 226 |
liveuser |
4779 |
$conf["external::killProcess"]["fileArgu"]=$conf["fileArgu"];
|
| 3 |
liveuser |
4780 |
#$conf["pid"],字串,執行序的pid.
|
|
|
4781 |
$conf["external::killProcess"]["pid"]=$conf["pid"];
|
|
|
4782 |
#$conf["cmd"],字串,要中止的指令關鍵字.
|
| 226 |
liveuser |
4783 |
$conf["external::killProcess"]["cmd"]=$conf["cmd"];
|
| 3 |
liveuser |
4784 |
#備註:
|
|
|
4785 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,透過su使用root身份可能會被selinux阻擋.
|
|
|
4786 |
#參考資料:
|
|
|
4787 |
#http://php.net/manual/en/function.exec.php
|
|
|
4788 |
$killProcess=external::killProcess($conf["external::killProcess"]);
|
|
|
4789 |
unset($conf["external::killProcess"]);
|
| 226 |
liveuser |
4790 |
|
| 3 |
liveuser |
4791 |
#如果中止程序失敗
|
|
|
4792 |
if($killProcess["status"]==="false"){
|
| 226 |
liveuser |
4793 |
|
| 3 |
liveuser |
4794 |
#設置執行失敗
|
|
|
4795 |
$result["status"]="false";
|
| 226 |
liveuser |
4796 |
|
| 3 |
liveuser |
4797 |
#設置執行錯誤訊息
|
|
|
4798 |
$result["error"]=$killProcess;
|
| 226 |
liveuser |
4799 |
|
| 3 |
liveuser |
4800 |
#回傳結果
|
|
|
4801 |
return $result;
|
| 226 |
liveuser |
4802 |
|
| 3 |
liveuser |
4803 |
}#if end
|
| 226 |
liveuser |
4804 |
|
| 3 |
liveuser |
4805 |
#回傳結果
|
|
|
4806 |
return $result;
|
| 226 |
liveuser |
4807 |
|
| 3 |
liveuser |
4808 |
}#if end
|
| 226 |
liveuser |
4809 |
|
|
|
4810 |
#休息一秒
|
| 3 |
liveuser |
4811 |
sleep(1);
|
| 226 |
liveuser |
4812 |
|
| 3 |
liveuser |
4813 |
#剩餘timeout時間減一
|
| 226 |
liveuser |
4814 |
$conf["timeout"]--;
|
| 3 |
liveuser |
4815 |
|
| 226 |
liveuser |
4816 |
}#while end
|
|
|
4817 |
|
|
|
4818 |
}#function timeout end
|
|
|
4819 |
|
| 3 |
liveuser |
4820 |
}#class external end
|
|
|
4821 |
|
|
|
4822 |
?>
|