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