| 3 |
liveuser |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
|
|
|
5 |
QBPWCF, Quick Build PHP website Component base on Fedora Linux.
|
| 239 |
liveuser |
6 |
Copyright (C) 2014~2026 MIN ZHI, CHEN
|
| 3 |
liveuser |
7 |
|
|
|
8 |
This file is part of QBPWCF.
|
|
|
9 |
|
|
|
10 |
QBPWCF is free software: you can redistribute it and/or modify
|
|
|
11 |
it under the terms of the GNU General Public License as published by
|
|
|
12 |
the Free Software Foundation, either version 3 of the License, or
|
|
|
13 |
(at your option) any later version.
|
|
|
14 |
|
|
|
15 |
QBPWCF is distributed in the hope that it will be useful,
|
|
|
16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
18 |
GNU General Public License for more details.
|
|
|
19 |
|
|
|
20 |
You should have received a copy of the GNU General Public License
|
|
|
21 |
along with QBPWCF. If not, see <http://www.gnu.org/licenses/>.
|
|
|
22 |
|
|
|
23 |
*/
|
|
|
24 |
namespace qbpwcf;
|
|
|
25 |
|
|
|
26 |
/*
|
|
|
27 |
類別說明:
|
|
|
28 |
執行外部程式的類別.
|
|
|
29 |
備註:
|
|
|
30 |
無.
|
|
|
31 |
*/
|
|
|
32 |
class external{
|
|
|
33 |
|
|
|
34 |
/*
|
|
|
35 |
#函式說明:
|
|
|
36 |
#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
|
|
|
37 |
#回傳結果:
|
|
|
38 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
39 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
40 |
#$result["function"],當前執行的函式名稱.
|
|
|
41 |
#必填參數:
|
|
|
42 |
#$method,物件,為物件實體或類別名稱,會自動置入該參數.
|
|
|
43 |
#$arguments,陣列,為呼叫方法時所用的參數.
|
|
|
44 |
#可省略參數:
|
|
|
45 |
#無.
|
|
|
46 |
#參考資料:
|
|
|
47 |
#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
|
|
|
48 |
*/
|
|
|
49 |
public function __call($method,$arguments){
|
| 226 |
liveuser |
50 |
|
| 3 |
liveuser |
51 |
#取得當前執行的函式
|
|
|
52 |
$result["function"]=__FUNCTION__;
|
| 226 |
liveuser |
53 |
|
| 3 |
liveuser |
54 |
#設置執行不正常
|
|
|
55 |
$result["status"]="false";
|
| 226 |
liveuser |
56 |
|
| 3 |
liveuser |
57 |
#設置執行錯誤
|
|
|
58 |
$result["error"][]=__NAMESPACE__ ."/".$method."() 不存在!";
|
| 226 |
liveuser |
59 |
|
| 3 |
liveuser |
60 |
#設置所丟入的參數
|
|
|
61 |
$result["error"][]=$arguments;
|
| 226 |
liveuser |
62 |
|
| 3 |
liveuser |
63 |
#回傳結果
|
|
|
64 |
return $result;
|
| 226 |
liveuser |
65 |
|
| 3 |
liveuser |
66 |
}#function __call end
|
| 226 |
liveuser |
67 |
|
| 3 |
liveuser |
68 |
/*
|
|
|
69 |
#函式說明:
|
|
|
70 |
#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
|
|
|
71 |
#回傳結果:
|
|
|
72 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
73 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
74 |
#$result["function"],當前執行的函式名稱.
|
|
|
75 |
#必填參數:
|
|
|
76 |
#$method,物件,為物件實體或類別名稱,會自動置入該參數.
|
|
|
77 |
#$arguments,陣列,為呼叫方法時所用的參數.
|
|
|
78 |
#可省略參數:
|
|
|
79 |
#無.
|
|
|
80 |
#參考資料:
|
|
|
81 |
#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
|
|
|
82 |
*/
|
|
|
83 |
public static function __callStatic($method,$arguments){
|
| 226 |
liveuser |
84 |
|
| 3 |
liveuser |
85 |
#取得當前執行的函式
|
|
|
86 |
$result["function"]=__FUNCTION__;
|
| 226 |
liveuser |
87 |
|
| 3 |
liveuser |
88 |
#設置執行不正常
|
|
|
89 |
$result["status"]="false";
|
| 226 |
liveuser |
90 |
|
| 3 |
liveuser |
91 |
#設置執行錯誤
|
|
|
92 |
$result["error"][]="欲呼叫的". __NAMESPACE__ ."/".$method."() 不存在!";
|
| 226 |
liveuser |
93 |
|
| 3 |
liveuser |
94 |
#設置所丟入的參數
|
|
|
95 |
$result["error"][]=$arguments;
|
| 226 |
liveuser |
96 |
|
| 3 |
liveuser |
97 |
#回傳結果
|
|
|
98 |
return $result;
|
| 226 |
liveuser |
99 |
|
| 3 |
liveuser |
100 |
}#function __callStatic end
|
| 226 |
liveuser |
101 |
|
| 3 |
liveuser |
102 |
/*
|
|
|
103 |
#函式說明:
|
| 226 |
liveuser |
104 |
#測試php呼叫java後回傳的結果
|
| 3 |
liveuser |
105 |
#回傳結果:
|
|
|
106 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
107 |
#$result["function"],當前執行的函式名稱.
|
|
|
108 |
#$result["error"],錯誤訊息陣列
|
|
|
109 |
#$result["cmd"],執行的指令內容.
|
|
|
110 |
#$result["output"],爲執行完java class檔案後的輸出陣列
|
|
|
111 |
#$result["outputFromFile"]["fileContent"],取得輸出的內容陣列.
|
|
|
112 |
#$result["outputFromFile"]["lineCount"],取得輸出內容總共的行數.
|
|
|
113 |
#$result["outputFromFile"]["fullContent"],取得輸出的內容陣列.
|
|
|
114 |
#必填參數:
|
|
|
115 |
$conf["javaClassFileAddress"]="";#java class檔案的位置與名稱,附檔名(.class)不用附上。
|
|
|
116 |
#可省略參數:
|
|
|
117 |
#$conf["arguments"]="";#要給java class檔案的參數
|
|
|
118 |
#參考資料:
|
|
|
119 |
#http://php.net/manual/en/function.exec.php
|
|
|
120 |
#備註:
|
|
|
121 |
#目前無法透過apache的身份執行java程式.
|
|
|
122 |
*/
|
|
|
123 |
public static function callJava($conf){
|
|
|
124 |
|
|
|
125 |
#初始化要回傳的內容
|
|
|
126 |
$result=array();
|
|
|
127 |
|
|
|
128 |
#取得當前執行的函式
|
|
|
129 |
$result["function"]=__FUNCTION__;
|
|
|
130 |
|
|
|
131 |
#如果 $conf 不為陣列
|
|
|
132 |
if(gettype($conf)!="array"){
|
| 226 |
liveuser |
133 |
|
| 3 |
liveuser |
134 |
#設置執行失敗
|
|
|
135 |
$result["status"]="false";
|
| 226 |
liveuser |
136 |
|
| 3 |
liveuser |
137 |
#設置執行錯誤訊息
|
|
|
138 |
$result["error"][]="\$conf變數須為陣列形態";
|
|
|
139 |
|
|
|
140 |
#回傳結果
|
|
|
141 |
return $result;
|
| 226 |
liveuser |
142 |
|
| 3 |
liveuser |
143 |
}#if end
|
|
|
144 |
|
|
|
145 |
#如果$conf["javaClassFileAddress"]沒有設定
|
|
|
146 |
if(!isset($conf["javaClassFileAddress"])){
|
|
|
147 |
|
|
|
148 |
#設置執行錯誤
|
|
|
149 |
$result["status"]="false";
|
|
|
150 |
|
|
|
151 |
#設置錯誤訊息
|
|
|
152 |
$result["error"][]="\$conf[\"javaClassFileAddress\"]參數不存在!";
|
|
|
153 |
|
|
|
154 |
#回傳結果
|
|
|
155 |
return $result;
|
|
|
156 |
|
|
|
157 |
}#if end
|
|
|
158 |
|
|
|
159 |
#如果$conf["arguments"]沒有設置
|
|
|
160 |
if(!isset($conf["arguments"])){
|
| 226 |
liveuser |
161 |
|
| 3 |
liveuser |
162 |
#設爲""
|
|
|
163 |
$conf["arguments"]="";
|
|
|
164 |
|
|
|
165 |
}#if end
|
|
|
166 |
|
|
|
167 |
#執行java程式
|
|
|
168 |
#函式說明:
|
|
|
169 |
#呼叫shell執行系統命令,並取得回傳的內容.
|
|
|
170 |
#回傳結果:
|
|
|
171 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
172 |
#$result["error"],錯誤訊息陣列
|
|
|
173 |
#$result["function"],當前執行的函數名稱
|
|
|
174 |
#$result["cmd"],執行的指令內容.
|
|
|
175 |
#$result["outputFromFile"]["fileContent"],取得輸出的內容陣列.
|
|
|
176 |
#$result["outputFromFile"]["lineCount"],取得輸出內容總共的行數.
|
|
|
177 |
#$result["outputFromFile"]["fullContent"],取得輸出的內容陣列.
|
|
|
178 |
#必填參數:
|
|
|
179 |
#$conf["external::callShell"]["command"],字串.
|
|
|
180 |
$conf["external::callShell"]["command"]="java";#要執行的指令與參數.
|
|
|
181 |
#可省略參數:
|
|
|
182 |
#$conf["external::callShell"]["argu"],陣列字串,指令搭配的參數,預設為空陣列.
|
|
|
183 |
$conf["external::callShell"]["argu"]=array($conf["javaClassFileAddress"],$conf["arguments"]);
|
|
|
184 |
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
|
|
|
185 |
#$conf["enablePrintDescription"]="true";
|
|
|
186 |
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容.
|
|
|
187 |
#$conf["printDescription"]="";
|
|
|
188 |
$conf["external::callShell"]["escapeshellarg"]="true";
|
|
|
189 |
#參考資料:
|
|
|
190 |
#http://php.net/manual/en/function.exec.php
|
|
|
191 |
$callShell=external::callShell($conf["external::callShell"]);
|
|
|
192 |
unset($conf["external::callShell"]);
|
|
|
193 |
|
|
|
194 |
#debug
|
|
|
195 |
#var_dump($callShell);
|
|
|
196 |
#exit;
|
|
|
197 |
|
|
|
198 |
#若執行外部 java class 程式回傳的狀態為 "false"
|
|
|
199 |
if($callShell["status"]=="false"){
|
| 226 |
liveuser |
200 |
|
| 3 |
liveuser |
201 |
#代表執行外部程式不正常.
|
| 226 |
liveuser |
202 |
|
| 3 |
liveuser |
203 |
#設置錯誤識別
|
|
|
204 |
$result["status"]="false";
|
| 226 |
liveuser |
205 |
|
| 3 |
liveuser |
206 |
#設置錯誤訊息
|
|
|
207 |
$result["error"]=$callShell;
|
| 226 |
liveuser |
208 |
|
| 3 |
liveuser |
209 |
#回傳結果
|
|
|
210 |
return $result;
|
| 226 |
liveuser |
211 |
|
| 3 |
liveuser |
212 |
}#if end
|
| 226 |
liveuser |
213 |
|
| 3 |
liveuser |
214 |
#取得輸出的內容陣列.
|
|
|
215 |
$result["outputFromFile"]["fileContent"]=$callShell["outputFromFile"]["fileContent"];
|
| 226 |
liveuser |
216 |
|
| 3 |
liveuser |
217 |
#取得輸出內容總共的行數.
|
|
|
218 |
$result["outputFromFile"]["lineCount"]=$callShell["outputFromFile"]["lineCount"];
|
| 226 |
liveuser |
219 |
|
| 3 |
liveuser |
220 |
#取得輸出的內容陣列.
|
|
|
221 |
$result["outputFromFile"]["fullContent"]=$callShell["outputFromFile"]["fullContent"];
|
| 226 |
liveuser |
222 |
|
| 3 |
liveuser |
223 |
#取得執行的指令內容
|
| 226 |
liveuser |
224 |
$result["cmd"]=$callShell["cmd"];
|
|
|
225 |
|
| 3 |
liveuser |
226 |
#執行到這邊代表執行正常
|
|
|
227 |
$result["status"]="true";
|
|
|
228 |
|
|
|
229 |
#取得輸出的內容
|
|
|
230 |
$result["output"]=$callShell["output"];
|
|
|
231 |
|
|
|
232 |
#回傳結果
|
|
|
233 |
return $result;
|
|
|
234 |
|
|
|
235 |
}#function testPheCallJava end
|
|
|
236 |
|
|
|
237 |
/*
|
|
|
238 |
#函式說明:
|
|
|
239 |
#呼叫shell執行編譯過的2元碼程式或具備執行權限的檔案後回傳的結果
|
|
|
240 |
#回傳結果:
|
|
|
241 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
242 |
#$result["function"],正在執行的函式
|
|
|
243 |
#$result["error"],錯誤訊息陣列.
|
|
|
244 |
#$result["error"]["returnCode"],錯誤代碼.
|
|
|
245 |
#$result["output"],爲執行完二元碼後的輸出陣列.
|
|
|
246 |
#$result["escapedCmd"],實際執行的指令.
|
|
|
247 |
#必填參數:
|
|
|
248 |
$conf["exeFileAddress"]="";#可執行檔的位置與名稱
|
|
|
249 |
#可省略參數:
|
|
|
250 |
#$conf["arguments"]="";#要給執行檔的參數
|
|
|
251 |
#參考資料:
|
|
|
252 |
#http://php.net/manual/en/function.exec.php
|
|
|
253 |
#備註:
|
|
|
254 |
#無.
|
| 226 |
liveuser |
255 |
*/
|
| 3 |
liveuser |
256 |
public static function execByteCode($conf){
|
|
|
257 |
|
|
|
258 |
#初始化要回傳的結果
|
|
|
259 |
$result=array();
|
| 226 |
liveuser |
260 |
|
| 3 |
liveuser |
261 |
#取得當前執行的函式
|
|
|
262 |
$result["function"]=__FUNCTION__;
|
| 226 |
liveuser |
263 |
|
| 3 |
liveuser |
264 |
#如果 $conf 不為陣列
|
|
|
265 |
if(gettype($conf)!="array"){
|
| 226 |
liveuser |
266 |
|
| 3 |
liveuser |
267 |
#設置執行失敗
|
|
|
268 |
$result["status"]="false";
|
| 226 |
liveuser |
269 |
|
| 3 |
liveuser |
270 |
#設置執行錯誤訊息
|
|
|
271 |
$result["error"][]="\$conf變數須為陣列形態";
|
|
|
272 |
|
|
|
273 |
#如果傳入的參數為 null
|
|
|
274 |
if($conf==null){
|
| 226 |
liveuser |
275 |
|
| 3 |
liveuser |
276 |
#設置執行錯誤訊息
|
|
|
277 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
| 226 |
liveuser |
278 |
|
| 3 |
liveuser |
279 |
}#if end
|
|
|
280 |
|
|
|
281 |
#回傳結果
|
|
|
282 |
return $result;
|
| 226 |
liveuser |
283 |
|
| 3 |
liveuser |
284 |
}#if end
|
| 226 |
liveuser |
285 |
|
| 3 |
liveuser |
286 |
#如果$conf["exeFileAddress"]沒有設定
|
|
|
287 |
if(!isset($conf["exeFileAddress"])){
|
|
|
288 |
|
|
|
289 |
#設置執行錯誤識別
|
|
|
290 |
$result["status"];
|
|
|
291 |
|
|
|
292 |
#設置錯誤訊息
|
|
|
293 |
$result["error"][]="\$conf[\"exeFileAddress\"]參數不存在!";
|
|
|
294 |
|
|
|
295 |
#回傳結果
|
|
|
296 |
return $result;
|
|
|
297 |
|
|
|
298 |
}#if end
|
| 226 |
liveuser |
299 |
|
| 3 |
liveuser |
300 |
#初始化要使用的參數
|
|
|
301 |
$escapedShellArg="";
|
|
|
302 |
|
|
|
303 |
#如果$conf["arguments"]沒有設置
|
|
|
304 |
if(isset($conf["arguments"])){
|
| 226 |
liveuser |
305 |
|
| 3 |
liveuser |
306 |
#取得已經處理好的參數
|
|
|
307 |
$escapedShellArg=" ".escapeshellarg($conf["arguments"]);
|
|
|
308 |
|
|
|
309 |
}#if end
|
|
|
310 |
|
|
|
311 |
#初始化執行的語法
|
|
|
312 |
$runStr=$conf["exeFileAddress"];
|
|
|
313 |
|
|
|
314 |
#如果沒有路徑的符號
|
|
|
315 |
if(strpos($runStr,"/")===false){
|
| 226 |
liveuser |
316 |
|
| 3 |
liveuser |
317 |
#代表要執行的程式在當前路徑
|
|
|
318 |
$runStr="./".$runStr;
|
| 226 |
liveuser |
319 |
|
| 3 |
liveuser |
320 |
}#if end
|
| 226 |
liveuser |
321 |
|
| 3 |
liveuser |
322 |
#處理執行的語法
|
|
|
323 |
$runStr=escapeshellarg($runStr);
|
|
|
324 |
|
|
|
325 |
#取得處理好的指令
|
|
|
326 |
$escapeShellCmd=$runStr.$escapedShellArg;
|
|
|
327 |
|
|
|
328 |
#儲存實際執行的指令
|
|
|
329 |
$result["escapedCmd"]=$escapeShellCmd;
|
|
|
330 |
|
|
|
331 |
#執行腳本程式,將輸出結果儲存在 $resultArray 陣列裏面
|
|
|
332 |
exec($escapeShellCmd,$resultArray,$execStatus);
|
|
|
333 |
|
|
|
334 |
#取得執行外部程式的輸出
|
|
|
335 |
$result["output"]=$resultArray;
|
|
|
336 |
|
|
|
337 |
#若執行外部程式回傳的狀態不為 0
|
|
|
338 |
if($execStatus!=0){
|
| 226 |
liveuser |
339 |
|
| 3 |
liveuser |
340 |
#代表執行外部程式不正常.
|
| 226 |
liveuser |
341 |
|
| 3 |
liveuser |
342 |
#設置錯誤識別
|
|
|
343 |
$result["status"]="false";
|
| 226 |
liveuser |
344 |
|
| 3 |
liveuser |
345 |
#設置錯誤代碼
|
|
|
346 |
$result["error"]["returnCode"]=$execStatus;
|
| 226 |
liveuser |
347 |
|
| 3 |
liveuser |
348 |
#設置錯誤訊息
|
|
|
349 |
$result["error"][]="執行".$conf["exeFileAddress"]."意外結束!";
|
| 226 |
liveuser |
350 |
|
| 3 |
liveuser |
351 |
#回傳結果
|
|
|
352 |
return $result;
|
| 226 |
liveuser |
353 |
|
| 3 |
liveuser |
354 |
}#if end
|
| 226 |
liveuser |
355 |
|
| 3 |
liveuser |
356 |
#執行到這邊代表執行正常
|
|
|
357 |
$result["status"]="true";
|
|
|
358 |
|
|
|
359 |
#回傳結果
|
|
|
360 |
return $result;
|
|
|
361 |
|
|
|
362 |
}#function execByteCode end
|
| 226 |
liveuser |
363 |
|
| 3 |
liveuser |
364 |
/*
|
|
|
365 |
#函式說明:
|
| 294 |
liveuser |
366 |
#解析含有指令與參數的陣列,回傳指令與參數給 callShell 函式使用.
|
| 3 |
liveuser |
367 |
#回傳結果:
|
|
|
368 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
369 |
#$result["error"],錯誤訊息陣列.
|
|
|
370 |
#$result["function"],當前執行的函數名稱.
|
|
|
371 |
#$result["argu"],使用的參數.
|
|
|
372 |
#$result["cmd"],執行的指令名稱.
|
|
|
373 |
#$result["params"],執行指令伴隨的參數.
|
|
|
374 |
#必填參數:
|
|
|
375 |
#$conf["cmdArray"],字串陣列,要執行的指令字串.
|
|
|
376 |
$conf["cmdArray"]=array();
|
|
|
377 |
#可省略參數:
|
|
|
378 |
#無.
|
|
|
379 |
#參考資料:
|
|
|
380 |
#array_shift=>https://www.php.net/manual/en/function.array-shift.php
|
|
|
381 |
#備註:
|
|
|
382 |
#無.
|
|
|
383 |
*/
|
|
|
384 |
public static function callShellHelper(&$conf){
|
| 226 |
liveuser |
385 |
|
| 3 |
liveuser |
386 |
#初始化要回傳的結果
|
|
|
387 |
$result=array();
|
| 226 |
liveuser |
388 |
|
| 3 |
liveuser |
389 |
#取得當前執行的函數名稱
|
|
|
390 |
$result["function"]=__FUNCTION__;
|
| 226 |
liveuser |
391 |
|
| 3 |
liveuser |
392 |
#如果 $conf 不為陣列
|
|
|
393 |
if(gettype($conf)!="array"){
|
| 226 |
liveuser |
394 |
|
| 3 |
liveuser |
395 |
#設置執行失敗
|
|
|
396 |
$result["status"]="false";
|
| 226 |
liveuser |
397 |
|
| 3 |
liveuser |
398 |
#設置執行錯誤訊息
|
|
|
399 |
$result["error"][]="\$conf變數須為陣列形態";
|
|
|
400 |
|
|
|
401 |
#如果傳入的參數為 null
|
|
|
402 |
if($conf==null){
|
| 226 |
liveuser |
403 |
|
| 3 |
liveuser |
404 |
#設置執行錯誤訊息
|
|
|
405 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
| 226 |
liveuser |
406 |
|
| 3 |
liveuser |
407 |
}#if end
|
|
|
408 |
|
|
|
409 |
#回傳結果
|
|
|
410 |
return $result;
|
| 226 |
liveuser |
411 |
|
| 3 |
liveuser |
412 |
}#if end
|
| 226 |
liveuser |
413 |
|
| 3 |
liveuser |
414 |
#取得參數
|
|
|
415 |
$result["argu"]=$conf;
|
| 226 |
liveuser |
416 |
|
| 3 |
liveuser |
417 |
#參數檢查
|
|
|
418 |
#函式說明:
|
|
|
419 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
420 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
421 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
422 |
#$result["function"],當前執行的函式名稱.
|
|
|
423 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
424 |
#$result[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
425 |
#$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
426 |
#$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
427 |
#必填寫的參數:
|
|
|
428 |
#$conf["variableCheck::checkArguments"]["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
429 |
$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
|
|
|
430 |
#$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
431 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("cmdArray");
|
| 226 |
liveuser |
432 |
#$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
433 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("array");
|
|
|
434 |
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
435 |
$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
436 |
#可以省略的參數:
|
|
|
437 |
#$conf["variableCheck::checkArguments"]["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true"。
|
|
|
438 |
$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
|
|
|
439 |
#$conf["variableCheck::checkArguments"]["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
|
|
440 |
#$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("pre","enablePrintDescription","printDescription","argu","escapeshellarg","username","password","useScript","logFilePath","inBackGround","arguIsAddr","getErr","plainArgu","useApostrophe","doNotRun","thereIsShellVar");
|
|
|
441 |
#$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("pre","enablePrintDescription","printDescription","argu","escapeshellarg","username","password","useScript","logFilePath","inBackGround","arguIsAddr","getErr","doNotRun","thereIsShellVar");
|
| 226 |
liveuser |
442 |
#$conf["variableCheck::checkArguments"]["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
443 |
#$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("array","string","string","array","string","string","string","string","string","string","array","string","array","array","string","array");
|
|
|
444 |
#$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("array","string","string","array","string","string","string","string","string","string","array","string","string","array");
|
|
|
445 |
#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,"null"代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
|
|
446 |
#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(null,"false",null,null,"false",null,null,"false",".qbpwcf_tmp/external/callShell/","false",null,"false",null,null,"false",null);
|
|
|
447 |
#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(null,"false",null,null,"false",null,null,"false",".qbpwcf_tmp/external/callShell/","false",null,"false","false",null);
|
|
|
448 |
#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
449 |
#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"][]=array("argu","arguIsAddr");
|
|
|
450 |
$checkResult=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
|
|
|
451 |
unset($conf["variableCheck::checkArguments"]);
|
| 226 |
liveuser |
452 |
|
| 3 |
liveuser |
453 |
#如果檢查參數失敗
|
|
|
454 |
if($checkResult["status"]=="false"){
|
| 226 |
liveuser |
455 |
|
| 3 |
liveuser |
456 |
#設置執行失敗
|
|
|
457 |
$result["status"]="false";
|
| 226 |
liveuser |
458 |
|
| 3 |
liveuser |
459 |
#設置執行錯誤
|
|
|
460 |
$result["error"]=$checkResult;
|
| 226 |
liveuser |
461 |
|
| 3 |
liveuser |
462 |
#回傳結果
|
|
|
463 |
return $result;
|
| 226 |
liveuser |
464 |
|
| 3 |
liveuser |
465 |
}#if end
|
| 226 |
liveuser |
466 |
|
| 3 |
liveuser |
467 |
#如果檢查參數不通過
|
|
|
468 |
if($checkResult["passed"]=="false"){
|
| 226 |
liveuser |
469 |
|
| 3 |
liveuser |
470 |
#設置執行失敗
|
|
|
471 |
$result["status"]="false";
|
| 226 |
liveuser |
472 |
|
| 3 |
liveuser |
473 |
#設置執行錯誤
|
|
|
474 |
$result["error"]=$checkResult;
|
| 226 |
liveuser |
475 |
|
| 3 |
liveuser |
476 |
#回傳結果
|
|
|
477 |
return $result;
|
| 226 |
liveuser |
478 |
|
| 3 |
liveuser |
479 |
}#if end
|
| 226 |
liveuser |
480 |
|
| 3 |
liveuser |
481 |
#取得使用的指令
|
|
|
482 |
$result["cmd"]=array_shift($conf["cmdArray"]);
|
| 226 |
liveuser |
483 |
|
| 3 |
liveuser |
484 |
#取得使用的參數
|
|
|
485 |
$result["params"]=$conf["cmdArray"];
|
| 226 |
liveuser |
486 |
|
| 3 |
liveuser |
487 |
#設置執行正常
|
|
|
488 |
$result["status"]="true";
|
| 226 |
liveuser |
489 |
|
| 3 |
liveuser |
490 |
#回傳結果
|
|
|
491 |
return $result;
|
| 226 |
liveuser |
492 |
|
| 3 |
liveuser |
493 |
}#function callShellHelper end
|
| 226 |
liveuser |
494 |
|
| 3 |
liveuser |
495 |
/*
|
|
|
496 |
#函式說明:
|
|
|
497 |
#呼叫shell執行系統命令,並取得回傳的內容.
|
|
|
498 |
#回傳結果:
|
|
|
499 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
500 |
#$result["error"],錯誤訊息陣列.
|
|
|
501 |
#$result["function"],當前執行的函數名稱.
|
|
|
502 |
#$result["argu"],使用的參數.
|
|
|
503 |
#$result["cmd"],執行的指令內容.
|
|
|
504 |
#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
| 60 |
liveuser |
505 |
#$result["output"],爲執行完後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
|
|
|
506 |
#$result["content"],為執行完後的輸出字串.
|
| 3 |
liveuser |
507 |
#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
508 |
#$result["running"],是否還在執行.
|
|
|
509 |
#$result["pid"],pid.
|
|
|
510 |
#$result["statusCode"],執行結束後的代碼.
|
|
|
511 |
#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
|
| 255 |
liveuser |
512 |
#$result["noEscaped"],陣列,儲存重新排序過未經過escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
|
| 3 |
liveuser |
513 |
#必填參數:
|
|
|
514 |
#$conf["command"],字串,要執行的指令.
|
|
|
515 |
$conf["command"]="";
|
|
|
516 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
|
|
517 |
$conf["fileArgu"]=__FILE__;
|
|
|
518 |
#可省略參數:
|
|
|
519 |
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
|
|
|
520 |
#$conf["argu"]=array("");
|
|
|
521 |
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
|
|
|
522 |
#$conf["arguIsAddr"]=array();
|
|
|
523 |
#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
|
|
|
524 |
#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
|
|
|
525 |
#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
|
|
|
526 |
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
|
|
|
527 |
#$conf["enablePrintDescription"]="true";
|
|
|
528 |
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
|
|
|
529 |
#$conf["printDescription"]="";
|
|
|
530 |
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
|
|
|
531 |
#$conf["escapeshellarg"]="false";
|
|
|
532 |
#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
|
|
|
533 |
#$conf["thereIsShellVar"]=array();
|
|
|
534 |
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
|
|
|
535 |
#$conf["username"]="";
|
|
|
536 |
#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
| 226 |
liveuser |
537 |
#$conf["password"]="";
|
| 3 |
liveuser |
538 |
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
|
|
|
539 |
#$conf["useScript"]="";
|
|
|
540 |
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
|
|
|
541 |
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
|
|
|
542 |
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
|
|
|
543 |
#$conf["inBackGround"]="";
|
|
|
544 |
#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
|
|
|
545 |
#$conf["getErr"]="false";
|
|
|
546 |
#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
|
|
|
547 |
#$conf["doNotRun"]="false";
|
| 294 |
liveuser |
548 |
#$conf["cmdContainArgu"],字串,cmd參數是否已經含有參數,預設為"false"代表沒有;反之為"true".
|
|
|
549 |
#$conf["cmdContainArgu"]="false";
|
| 255 |
liveuser |
550 |
#$conf["remoteIp"],字串,遠端ip或domainName,預設不使用,若存在則會透過ssh指令進行連線,然後執行指令.
|
|
|
551 |
#$conf["remoteIp"]="";
|
|
|
552 |
#$conf["remoteUser"],字串,遠端的使用者賬戶,預設不指定,亦即跟為運行該php程式的使用者.
|
|
|
553 |
#$conf["remoteUser"]="";
|
|
|
554 |
#參考資料:
|
| 3 |
liveuser |
555 |
#exec=>http://php.net/manual/en/function.exec.php
|
|
|
556 |
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
|
|
|
557 |
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
|
|
|
558 |
#備註:
|
|
|
559 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
|
|
|
560 |
#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
|
| 183 |
liveuser |
561 |
*/
|
| 3 |
liveuser |
562 |
public static function callShell(&$conf){
|
| 226 |
liveuser |
563 |
|
| 3 |
liveuser |
564 |
#初始化要回傳的結果
|
|
|
565 |
$result=array();
|
| 226 |
liveuser |
566 |
|
| 3 |
liveuser |
567 |
#取得當前執行的函數名稱
|
|
|
568 |
$result["function"]=__FUNCTION__;
|
| 226 |
liveuser |
569 |
|
| 3 |
liveuser |
570 |
#如果 $conf 不為陣列
|
|
|
571 |
if(gettype($conf)!="array"){
|
| 226 |
liveuser |
572 |
|
| 3 |
liveuser |
573 |
#設置執行失敗
|
|
|
574 |
$result["status"]="false";
|
| 226 |
liveuser |
575 |
|
| 3 |
liveuser |
576 |
#設置執行錯誤訊息
|
|
|
577 |
$result["error"][]="\$conf變數須為陣列形態";
|
|
|
578 |
|
|
|
579 |
#如果傳入的參數為 null
|
|
|
580 |
if($conf==null){
|
| 226 |
liveuser |
581 |
|
| 3 |
liveuser |
582 |
#設置執行錯誤訊息
|
|
|
583 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
| 226 |
liveuser |
584 |
|
| 3 |
liveuser |
585 |
}#if end
|
|
|
586 |
|
|
|
587 |
#回傳結果
|
|
|
588 |
return $result;
|
| 226 |
liveuser |
589 |
|
| 3 |
liveuser |
590 |
}#if end
|
| 226 |
liveuser |
591 |
|
| 3 |
liveuser |
592 |
#取得參數
|
|
|
593 |
$result["argu"]=$conf;
|
| 226 |
liveuser |
594 |
|
| 3 |
liveuser |
595 |
#參數檢查
|
|
|
596 |
#函式說明:
|
|
|
597 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
598 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
599 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
600 |
#$result["function"],當前執行的函式名稱.
|
|
|
601 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
602 |
#$result[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
603 |
#$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
604 |
#$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
605 |
#必填寫的參數:
|
|
|
606 |
#$conf["variableCheck::checkArguments"]["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
607 |
$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
|
|
|
608 |
#$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
609 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("command","fileArgu");
|
| 226 |
liveuser |
610 |
#$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
611 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string");
|
|
|
612 |
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
613 |
$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
614 |
#可以省略的參數:
|
|
|
615 |
#$conf["variableCheck::checkArguments"]["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true"。
|
|
|
616 |
#$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
|
|
|
617 |
#$conf["variableCheck::checkArguments"]["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
| 294 |
liveuser |
618 |
$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("pre","enablePrintDescription","printDescription","argu","escapeshellarg","username","password","useScript","logFilePath","inBackGround","arguIsAddr","getErr","doNotRun","thereIsShellVar","remoteIp","remoteUser","cmdContainArgu");
|
| 226 |
liveuser |
619 |
#$conf["variableCheck::checkArguments"]["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
| 294 |
liveuser |
620 |
$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("array","string","string","array","string","string","string","string","string","string","array","string","string","array","string","string","string");
|
| 3 |
liveuser |
621 |
#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,"null"代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
| 294 |
liveuser |
622 |
$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(null,"false",null,null,"false",null,null,"false",".qbpwcf_tmp/external/callShell/","false",null,"false","false",null,null,null,"false");
|
| 3 |
liveuser |
623 |
#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
624 |
$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"][]=array("argu","arguIsAddr");
|
|
|
625 |
$checkResult=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
|
|
|
626 |
unset($conf["variableCheck::checkArguments"]);
|
| 226 |
liveuser |
627 |
|
| 3 |
liveuser |
628 |
#如果檢查參數失敗
|
|
|
629 |
if($checkResult["status"]=="false"){
|
| 226 |
liveuser |
630 |
|
| 3 |
liveuser |
631 |
#設置執行失敗
|
|
|
632 |
$result["status"]="false";
|
| 226 |
liveuser |
633 |
|
| 3 |
liveuser |
634 |
#設置執行錯誤
|
|
|
635 |
$result["error"]=$checkResult;
|
| 226 |
liveuser |
636 |
|
| 3 |
liveuser |
637 |
#回傳結果
|
|
|
638 |
return $result;
|
| 226 |
liveuser |
639 |
|
| 3 |
liveuser |
640 |
}#if end
|
| 226 |
liveuser |
641 |
|
| 3 |
liveuser |
642 |
#如果檢查參數不通過
|
|
|
643 |
if($checkResult["passed"]=="false"){
|
| 226 |
liveuser |
644 |
|
| 3 |
liveuser |
645 |
#設置執行失敗
|
|
|
646 |
$result["status"]="false";
|
| 226 |
liveuser |
647 |
|
| 3 |
liveuser |
648 |
#設置執行錯誤
|
|
|
649 |
$result["error"]=$checkResult;
|
| 226 |
liveuser |
650 |
|
| 3 |
liveuser |
651 |
#回傳結果
|
|
|
652 |
return $result;
|
| 226 |
liveuser |
653 |
|
| 3 |
liveuser |
654 |
}#if end
|
| 226 |
liveuser |
655 |
|
| 3 |
liveuser |
656 |
#如果是web環境,則不能執行script指令
|
|
|
657 |
#涵式說明:
|
|
|
658 |
#判斷當前環境為web還是cmd
|
|
|
659 |
#回傳結果:
|
|
|
660 |
#$result,"web"或"cmd"
|
|
|
661 |
if(csInformation::getEnv()==="web" && $conf["useScript"]==="true"){
|
| 226 |
liveuser |
662 |
|
| 3 |
liveuser |
663 |
#設置執行失敗
|
|
|
664 |
$result["status"]="false";
|
| 226 |
liveuser |
665 |
|
| 3 |
liveuser |
666 |
#設置執行錯誤訊息
|
|
|
667 |
$result["error"][]="函數 ".$result["function"]." 使用參數 useScript 為 ".$conf["useScript"]." 時僅能在命令列環境下運行!";
|
| 226 |
liveuser |
668 |
|
| 3 |
liveuser |
669 |
#回傳結果
|
|
|
670 |
return $result;
|
| 226 |
liveuser |
671 |
|
| 3 |
liveuser |
672 |
}#if end
|
| 226 |
liveuser |
673 |
|
| 3 |
liveuser |
674 |
#如果有設置 $conf["pre"]
|
|
|
675 |
if(isset($conf["pre"])){
|
| 226 |
liveuser |
676 |
|
| 3 |
liveuser |
677 |
#初始化存放新的參數
|
|
|
678 |
$newArgu=array();
|
| 226 |
liveuser |
679 |
|
| 3 |
liveuser |
680 |
#初始化那些參數含有 shell 變數
|
|
|
681 |
$thereIsShellVar=array();
|
| 226 |
liveuser |
682 |
|
| 3 |
liveuser |
683 |
#記錄原先的指令
|
|
|
684 |
$oriCmd=$conf["command"];
|
| 226 |
liveuser |
685 |
|
| 3 |
liveuser |
686 |
#針對每個前置指令
|
|
|
687 |
foreach($conf["pre"] as $index=>$info){
|
| 226 |
liveuser |
688 |
|
| 3 |
liveuser |
689 |
#如果是第一個要執行的 pre 指令
|
|
|
690 |
if($index===0){
|
| 226 |
liveuser |
691 |
|
| 3 |
liveuser |
692 |
#更新 指令的名稱
|
|
|
693 |
$conf["command"]=$info["cmd"];
|
| 226 |
liveuser |
694 |
|
| 3 |
liveuser |
695 |
}#if end
|
| 226 |
liveuser |
696 |
|
| 3 |
liveuser |
697 |
#反之為後面的 pre 指令
|
|
|
698 |
else{
|
| 226 |
liveuser |
699 |
|
| 3 |
liveuser |
700 |
#增加參數
|
|
|
701 |
$newArgu[]=$info["cmd"];
|
| 226 |
liveuser |
702 |
|
|
|
703 |
#設置不含有 shell 變數
|
| 3 |
liveuser |
704 |
$thereIsShellVar[]="false";
|
| 226 |
liveuser |
705 |
|
| 3 |
liveuser |
706 |
}#else end
|
| 226 |
liveuser |
707 |
|
| 3 |
liveuser |
708 |
#針對每個參數
|
|
|
709 |
foreach($info["param"] as $param){
|
| 226 |
liveuser |
710 |
|
| 3 |
liveuser |
711 |
#增加參數
|
|
|
712 |
$newArgu[]=$param;
|
| 226 |
liveuser |
713 |
|
|
|
714 |
#設置不含有 shell 變數
|
| 3 |
liveuser |
715 |
$thereIsShellVar[]="false";
|
| 226 |
liveuser |
716 |
|
|
|
717 |
}#foreach end
|
|
|
718 |
|
|
|
719 |
#增加分號
|
| 3 |
liveuser |
720 |
$newArgu[]=";";
|
| 226 |
liveuser |
721 |
|
|
|
722 |
#設置不含有 shell 變數
|
| 3 |
liveuser |
723 |
$thereIsShellVar[]="false";
|
| 226 |
liveuser |
724 |
|
| 3 |
liveuser |
725 |
}#foreach end
|
| 226 |
liveuser |
726 |
|
| 3 |
liveuser |
727 |
#原本的指令變成參數
|
|
|
728 |
$newArgu[]=$oriCmd;
|
| 226 |
liveuser |
729 |
|
|
|
730 |
#設置不含有 shell 變數
|
| 3 |
liveuser |
731 |
$thereIsShellVar[]="false";
|
| 226 |
liveuser |
732 |
|
| 3 |
liveuser |
733 |
#針對本來要執行的參數
|
|
|
734 |
foreach($conf["argu"] as $index => $param){
|
| 226 |
liveuser |
735 |
|
| 3 |
liveuser |
736 |
#增加參數
|
|
|
737 |
$newArgu[]=$param;
|
| 226 |
liveuser |
738 |
|
| 3 |
liveuser |
739 |
#如果有設置是否含有 shell 變數
|
|
|
740 |
if(isset($conf["thereIsShellVar"])){
|
| 226 |
liveuser |
741 |
|
|
|
742 |
#設置是否含有 shell 變數
|
| 3 |
liveuser |
743 |
$thereIsShellVar[]=$conf["thereIsShellVar"][$index];
|
| 226 |
liveuser |
744 |
|
| 3 |
liveuser |
745 |
}#if end
|
| 226 |
liveuser |
746 |
|
| 3 |
liveuser |
747 |
#反之
|
|
|
748 |
else{
|
| 226 |
liveuser |
749 |
|
|
|
750 |
#設置不含有 shell 變數
|
| 3 |
liveuser |
751 |
$thereIsShellVar[]="false";
|
| 226 |
liveuser |
752 |
|
| 3 |
liveuser |
753 |
}#else end
|
| 226 |
liveuser |
754 |
|
| 3 |
liveuser |
755 |
}#foreach end
|
| 255 |
liveuser |
756 |
|
| 3 |
liveuser |
757 |
#更新 argu 參數
|
|
|
758 |
$conf["argu"]=$newArgu;
|
| 226 |
liveuser |
759 |
|
| 3 |
liveuser |
760 |
#更新 thereIsShellVar 參數
|
|
|
761 |
$conf["thereIsShellVar"]=$thereIsShellVar;
|
| 226 |
liveuser |
762 |
|
| 3 |
liveuser |
763 |
#移除處理掉的 pre 參數
|
|
|
764 |
unset($conf["pre"]);
|
| 226 |
liveuser |
765 |
|
| 3 |
liveuser |
766 |
#遞迴呼叫
|
|
|
767 |
return self::callShell($conf);
|
| 226 |
liveuser |
768 |
|
| 3 |
liveuser |
769 |
}#if end
|
| 294 |
liveuser |
770 |
|
|
|
771 |
#如果 cmd 參數含有 arug
|
|
|
772 |
if($conf["cmdContainArgu"]==="true"){
|
|
|
773 |
|
|
|
774 |
#如果 cmd 中間含有 " | " 跟 "ssh"
|
|
|
775 |
#函式說明:
|
|
|
776 |
#尋找字串中是否含有符合格式的內容,且回傳解析好的變數數值.
|
|
|
777 |
#回傳結果:
|
|
|
778 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
779 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
780 |
#$result["function"],當前執行的函式名稱.
|
|
|
781 |
#$result["argu"],所使用的參數.
|
|
|
782 |
#$result["found"],是否有找到符合格式的字串內容,"true"代表有找到,"false"代表沒有找到.
|
|
|
783 |
#$result["content"],陣列,若為n個${*},則當found為"true"時,就會回傳n個元素.
|
|
|
784 |
#$result["parsedVar"][varName],陣列,解析好的變數陣列,varName為${}中的內容,變數的key從0開始.
|
|
|
785 |
#必填參數:
|
|
|
786 |
#$conf["input"],字串,要檢查的字串.
|
|
|
787 |
$conf["search::findSpecifyStrFormat"]["input"]=$conf["command"];
|
|
|
788 |
#$conf["format"],格式字串,要尋找的格式字串.格式為固定的字串("fixedStr format")與變數("${keyWordVarName}")組成.
|
|
|
789 |
$conf["search::findSpecifyStrFormat"]["format"]="${cmdAndArgu} | ssh ${remoteIp}";
|
|
|
790 |
#可省略參數:
|
|
|
791 |
#$conf["varEqual"],陣列,變數對應的數值,null代表不指定,其他內容代表該變數解析出來必須要為該內容.
|
|
|
792 |
$conf["search::findSpecifyStrFormat"]["varEqual"]=array(null,null);
|
|
|
793 |
#$conf["varCon"],陣列,每個varEqual為null者,其是否有其他條件,預設為null代表無其他條件,條件的表示是用陣列的key與value來表達,例如:array("no_tail"=>" not"),就代表變數的結尾不能為" not",可以用的key有"head",代表開頭要有什麼;"no_head",代表不能為什麼開頭;"tail",代表要什麼結尾;"no_tail",代表不能什麼結尾.
|
|
|
794 |
#$conf["varCon"]=array("no_tail"=>" not");
|
|
|
795 |
#參考資料:
|
|
|
796 |
#無.
|
|
|
797 |
#備註:
|
|
|
798 |
#無.
|
|
|
799 |
$findSpecifyStrFormat=search::findSpecifyStrFormat($conf["search::findSpecifyStrFormat"]);
|
|
|
800 |
unset($conf["search::findSpecifyStrFormat"]);
|
|
|
801 |
|
|
|
802 |
#若執行異常
|
|
|
803 |
if($findSpecifyStrFormat["status"]==="false"){
|
|
|
804 |
|
|
|
805 |
#設置執行錯誤
|
|
|
806 |
$result["error"]=$findSpecifyStrFormat;
|
|
|
807 |
|
|
|
808 |
#設置執行異常識別
|
|
|
809 |
$result["status"]="false";
|
|
|
810 |
|
|
|
811 |
return $result;
|
|
|
812 |
|
|
|
813 |
}#if end
|
|
|
814 |
|
|
|
815 |
#如果有找到符合的格式
|
|
|
816 |
if($findSpecifyStrFormat["found"]==="true"){
|
|
|
817 |
|
|
|
818 |
#取得尚未解析的指令與參數
|
|
|
819 |
$cmdAndArgu=$findSpecifyStrFormat["parsedVar"]["cmdAndArgu"][0];
|
|
|
820 |
|
|
|
821 |
#函式說明:
|
|
|
822 |
#將指令字串解析成陣列,方便給予 external::callShell 使用
|
|
|
823 |
#回傳結果:
|
|
|
824 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
825 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
826 |
#$result["function"],當前執行的函式名稱.
|
|
|
827 |
#$result["content"],解析好的指令陣列.
|
|
|
828 |
#$result["cmd"],解析好的指令名稱.
|
|
|
829 |
#$result["argus"],解析好的參數陣列.
|
|
|
830 |
#$result["argu"],所使用的參數.
|
|
|
831 |
#必填參數
|
|
|
832 |
#$conf["cmdStr"],字串,要解析的指令字串
|
|
|
833 |
$conf["cmd::parseCmdString"]["cmdStr"]=$cmdAndArgu;
|
|
|
834 |
#可省略參數:
|
|
|
835 |
#無.
|
|
|
836 |
#參考資料:
|
|
|
837 |
#無.
|
|
|
838 |
#備註:
|
|
|
839 |
#無.
|
|
|
840 |
$parseCmdString=cmd::parseCmdString($conf["cmd::parseCmdString"]);
|
|
|
841 |
unset($conf["cmd::parseCmdString"]);
|
|
|
842 |
|
|
|
843 |
#若執行異常
|
|
|
844 |
if($parseCmdString["status"]==="false"){
|
|
|
845 |
|
|
|
846 |
#設置執行錯誤
|
|
|
847 |
$result["error"]=$parseCmdString;
|
|
|
848 |
|
|
|
849 |
#設置執行異常識別
|
|
|
850 |
$result["status"]="false";
|
|
|
851 |
|
|
|
852 |
return $result;
|
|
|
853 |
|
|
|
854 |
}#if end
|
|
|
855 |
|
|
|
856 |
#取得新的 cmd
|
|
|
857 |
$conf["command"]=$parseCmdString["cmd"];
|
|
|
858 |
|
|
|
859 |
#取得新的優先參數
|
|
|
860 |
$highPriorityArgu=$parseCmdString["argus"];
|
|
|
861 |
|
|
|
862 |
#合併參數
|
|
|
863 |
#函式說明:
|
|
|
864 |
#將多個一維陣列串聯起來,key從0開始排序.
|
|
|
865 |
#回傳的結果:
|
|
|
866 |
#$result["status"],"true"表執行正常,"false"代表執行不正常.
|
|
|
867 |
#$result["error"],錯誤訊息陣列.
|
|
|
868 |
#$result["function"],當前執行的函數.
|
|
|
869 |
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
|
|
|
870 |
#$result["content"],合併好的一維陣列.
|
|
|
871 |
#必填參數
|
|
|
872 |
#$conf["inputArray"],陣列,要合併的一維陣列變數,例如:=array($array1,$array2);
|
|
|
873 |
$conf["arrays::mergeArray"]["inputArray"]=array($highPriorityArgu,$conf["argu"]);
|
|
|
874 |
#可省略參數:
|
|
|
875 |
#$conf["allowRepeat"],字串,預設為"true",允許重複的結果;若為"false"則不會出現重複的元素內容.
|
|
|
876 |
#$conf["allowRepeat"]="true";
|
|
|
877 |
#$conf["looseDiff"],字串,預設為"false",代表要嚴謹判斷為有相異,例如陣列中元素的key順序不同(整數)就代表有相異;反之為"true",例如陣列中元素的key順序不同(非整數),但value有相同,則視為無相異.
|
|
|
878 |
#$conf["looseDiff"]="false";
|
|
|
879 |
#參考資料:
|
|
|
880 |
#無.
|
|
|
881 |
#備註:
|
|
|
882 |
#無.
|
|
|
883 |
$mergeArray=arrays::mergeArray($conf["arrays::mergeArray"]);
|
|
|
884 |
unset($conf["arrays::mergeArray"]);
|
|
|
885 |
|
|
|
886 |
#若執行異常
|
|
|
887 |
if($mergeArray["status"]==="false"){
|
|
|
888 |
|
|
|
889 |
#設置執行錯誤
|
|
|
890 |
$result["error"]=$mergeArray;
|
|
|
891 |
|
|
|
892 |
#設置執行異常識別
|
|
|
893 |
$result["status"]="false";
|
|
|
894 |
|
|
|
895 |
return $result;
|
|
|
896 |
|
|
|
897 |
}#if end
|
|
|
898 |
|
|
|
899 |
#設置新的參數
|
|
|
900 |
$conf["argu"]=$mergeArray["content"];
|
|
|
901 |
|
|
|
902 |
#取得遠端的ip
|
|
|
903 |
$conf["remoteIp"]=$findSpecifyStrFormat["parsedVar"]["remoteIp"][0];
|
|
|
904 |
|
|
|
905 |
#設置cmd參數已經不含argu了
|
|
|
906 |
$conf["cmdContainArgu"]="false";
|
|
|
907 |
|
|
|
908 |
#遞迴呼叫
|
|
|
909 |
return self::callShell($conf);
|
|
|
910 |
|
|
|
911 |
}#if end
|
|
|
912 |
|
|
|
913 |
#反之
|
|
|
914 |
else{
|
|
|
915 |
|
|
|
916 |
#函式說明:
|
|
|
917 |
#將指令字串解析成陣列,方便給予 external::callShell 使用
|
|
|
918 |
#回傳結果:
|
|
|
919 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
920 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
921 |
#$result["function"],當前執行的函式名稱.
|
|
|
922 |
#$result["content"],解析好的指令陣列.
|
|
|
923 |
#$result["cmd"],解析好的指令名稱.
|
|
|
924 |
#$result["argus"],解析好的參數陣列.
|
|
|
925 |
#$result["argu"],所使用的參數.
|
|
|
926 |
#必填參數
|
|
|
927 |
#$conf["cmdStr"],字串,要解析的指令字串
|
|
|
928 |
$conf["cmd::parseCmdString"]["cmdStr"]=$conf["command"];
|
|
|
929 |
#可省略參數:
|
|
|
930 |
#無.
|
|
|
931 |
#參考資料:
|
|
|
932 |
#無.
|
|
|
933 |
#備註:
|
|
|
934 |
#無.
|
|
|
935 |
$parseCmdString=cmd::parseCmdString($conf["cmd::parseCmdString"]);
|
|
|
936 |
unset($conf["cmd::parseCmdString"]);
|
|
|
937 |
|
|
|
938 |
#若執行異常
|
|
|
939 |
if($parseCmdString["status"]==="false"){
|
|
|
940 |
|
|
|
941 |
#設置執行錯誤
|
|
|
942 |
$result["error"]=$parseCmdString;
|
|
|
943 |
|
|
|
944 |
#設置執行異常識別
|
|
|
945 |
$result["status"]="false";
|
|
|
946 |
|
|
|
947 |
return $result;
|
|
|
948 |
|
|
|
949 |
}#if end
|
|
|
950 |
|
|
|
951 |
#取得新的 cmd
|
|
|
952 |
$conf["command"]=$parseCmdString["cmd"];
|
|
|
953 |
|
|
|
954 |
#取得新的優先參數
|
|
|
955 |
$highPriorityArgu=$parseCmdString["argus"];
|
|
|
956 |
|
|
|
957 |
#合併參數
|
|
|
958 |
#函式說明:
|
|
|
959 |
#將多個一維陣列串聯起來,key從0開始排序.
|
|
|
960 |
#回傳的結果:
|
|
|
961 |
#$result["status"],"true"表執行正常,"false"代表執行不正常.
|
|
|
962 |
#$result["error"],錯誤訊息陣列.
|
|
|
963 |
#$result["function"],當前執行的函數.
|
|
|
964 |
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
|
|
|
965 |
#$result["content"],合併好的一維陣列.
|
|
|
966 |
#必填參數
|
|
|
967 |
#$conf["inputArray"],陣列,要合併的一維陣列變數,例如:=array($array1,$array2);
|
|
|
968 |
$conf["arrays::mergeArray"]["inputArray"]=array($highPriorityArgu,$conf["argu"]);
|
|
|
969 |
#可省略參數:
|
|
|
970 |
#$conf["allowRepeat"],字串,預設為"true",允許重複的結果;若為"false"則不會出現重複的元素內容.
|
|
|
971 |
#$conf["allowRepeat"]="true";
|
|
|
972 |
#$conf["looseDiff"],字串,預設為"false",代表要嚴謹判斷為有相異,例如陣列中元素的key順序不同(整數)就代表有相異;反之為"true",例如陣列中元素的key順序不同(非整數),但value有相同,則視為無相異.
|
|
|
973 |
#$conf["looseDiff"]="false";
|
|
|
974 |
#參考資料:
|
|
|
975 |
#無.
|
|
|
976 |
#備註:
|
|
|
977 |
#無.
|
|
|
978 |
$mergeArray=arrays::mergeArray($conf["arrays::mergeArray"]);
|
|
|
979 |
unset($conf["arrays::mergeArray"]);
|
|
|
980 |
|
|
|
981 |
#若執行異常
|
|
|
982 |
if($mergeArray["status"]==="false"){
|
|
|
983 |
|
|
|
984 |
#設置執行錯誤
|
|
|
985 |
$result["error"]=$mergeArray;
|
|
|
986 |
|
|
|
987 |
#設置執行異常識別
|
|
|
988 |
$result["status"]="false";
|
|
|
989 |
|
|
|
990 |
return $result;
|
|
|
991 |
|
|
|
992 |
}#if end
|
|
|
993 |
|
|
|
994 |
#設置新的參數
|
|
|
995 |
$conf["argu"]=$mergeArray["content"];
|
|
|
996 |
|
|
|
997 |
#設置cmd參數已經不含argu了
|
|
|
998 |
$conf["cmdContainArgu"]="false";
|
|
|
999 |
|
|
|
1000 |
#遞迴呼叫
|
|
|
1001 |
return self::callShell($conf);
|
|
|
1002 |
|
|
|
1003 |
}#else end
|
|
|
1004 |
|
|
|
1005 |
}#if end
|
| 226 |
liveuser |
1006 |
|
| 3 |
liveuser |
1007 |
#如果沒有設置要印出來的描述內容
|
|
|
1008 |
if(!isset($conf["printDescription"])){
|
| 226 |
liveuser |
1009 |
|
| 3 |
liveuser |
1010 |
#預設為要執行的指令
|
|
|
1011 |
$conf["printDescription"]=$conf["command"];
|
| 226 |
liveuser |
1012 |
|
| 3 |
liveuser |
1013 |
#如果有設置參數
|
|
|
1014 |
if(isset($conf["argu"])){
|
| 226 |
liveuser |
1015 |
|
| 3 |
liveuser |
1016 |
#針對每個參數
|
|
|
1017 |
foreach($conf["argu"] as $argu){
|
| 226 |
liveuser |
1018 |
|
| 3 |
liveuser |
1019 |
#若有參數則加在後面
|
|
|
1020 |
$conf["printDescription"]=$conf["printDescription"]." ".$argu;
|
| 226 |
liveuser |
1021 |
|
| 3 |
liveuser |
1022 |
}#foreach end
|
| 226 |
liveuser |
1023 |
|
| 3 |
liveuser |
1024 |
}#if end
|
| 226 |
liveuser |
1025 |
|
| 3 |
liveuser |
1026 |
}#if end
|
| 226 |
liveuser |
1027 |
|
| 3 |
liveuser |
1028 |
#如果要印出 $conf["printDescription"] 的內容
|
|
|
1029 |
if($conf["enablePrintDescription"]=="true"){
|
| 226 |
liveuser |
1030 |
|
| 3 |
liveuser |
1031 |
#輸出文字
|
|
|
1032 |
echo "執行「".$conf["printDescription"]."」命令\n";
|
| 226 |
liveuser |
1033 |
|
| 3 |
liveuser |
1034 |
}#if end
|
| 226 |
liveuser |
1035 |
|
| 3 |
liveuser |
1036 |
#初始化執行的指令
|
|
|
1037 |
$result["cmd"]=$conf["command"];
|
| 226 |
liveuser |
1038 |
|
| 3 |
liveuser |
1039 |
#初始化輸出
|
|
|
1040 |
$output=array();
|
| 226 |
liveuser |
1041 |
|
| 3 |
liveuser |
1042 |
#取得 PATH
|
|
|
1043 |
exec("echo \$PATH",$output,$status);
|
| 226 |
liveuser |
1044 |
|
| 3 |
liveuser |
1045 |
#如果執行失敗
|
|
|
1046 |
if($status!==0){
|
| 226 |
liveuser |
1047 |
|
| 3 |
liveuser |
1048 |
#設置執行失敗
|
|
|
1049 |
$result["status"]="false";
|
| 226 |
liveuser |
1050 |
|
| 3 |
liveuser |
1051 |
#設置執行錯誤訊息
|
|
|
1052 |
$result["error"]="取得系統環境變數 PATH 失敗";
|
| 226 |
liveuser |
1053 |
|
| 3 |
liveuser |
1054 |
#回傳結果
|
|
|
1055 |
return $result;
|
| 226 |
liveuser |
1056 |
|
| 3 |
liveuser |
1057 |
}#if end
|
| 226 |
liveuser |
1058 |
|
|
|
1059 |
#取得 PATH 設定
|
| 3 |
liveuser |
1060 |
$PATHS=$output[0];
|
| 226 |
liveuser |
1061 |
|
| 3 |
liveuser |
1062 |
#函式說明:
|
|
|
1063 |
#將固定格式的字串分開,並回傳分開的結果.
|
|
|
1064 |
#回傳結果:
|
|
|
1065 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
1066 |
#$result["error"],錯誤訊息陣列
|
|
|
1067 |
#$result["function"],當前執行的函數名稱.
|
|
|
1068 |
#$result["argu"],使用的參數.
|
|
|
1069 |
#$result["oriStr"],要分割的原始字串內容
|
|
|
1070 |
#$result["dataArray"],爲分割好字串的陣列內容,$result["dataArray"][$i]爲第($i+1)段的內容。
|
|
|
1071 |
#$result["dataCounts"],爲總共分成幾段
|
|
|
1072 |
#$result["found"],是否有在$conf["stringIn"]找到$conf["spiltSymbol"],"true"代表有找到,"false"代表沒有找到.
|
|
|
1073 |
#必填參數:
|
|
|
1074 |
#$conf["stringIn"],字串,要處理的字串.
|
|
|
1075 |
$conf["stringProcess::spiltString"]["stringIn"]=$PATHS;
|
|
|
1076 |
#$conf["spiltSymbol"],字串,爲以哪個符號作爲分割.
|
|
|
1077 |
$conf["stringProcess::spiltString"]["spiltSymbol"]=":";
|
|
|
1078 |
#可省略參數:
|
|
|
1079 |
#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
|
|
|
1080 |
$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
|
|
|
1081 |
#參考資料:
|
|
|
1082 |
#無.
|
|
|
1083 |
#備註:
|
|
|
1084 |
#無.
|
|
|
1085 |
$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
|
|
|
1086 |
unset($conf["stringProcess::spiltString"]);
|
| 226 |
liveuser |
1087 |
|
| 3 |
liveuser |
1088 |
#如果分割字串失敗
|
|
|
1089 |
if($spiltString["status"]==="false"){
|
| 226 |
liveuser |
1090 |
|
| 3 |
liveuser |
1091 |
#設置執行失敗
|
|
|
1092 |
$result["status"]="false";
|
| 226 |
liveuser |
1093 |
|
| 3 |
liveuser |
1094 |
#設置執行錯誤訊息
|
|
|
1095 |
$result["error"]="取得系統環境變數 PATH 失敗";
|
| 226 |
liveuser |
1096 |
|
| 3 |
liveuser |
1097 |
#回傳結果
|
|
|
1098 |
return $result;
|
| 226 |
liveuser |
1099 |
|
| 3 |
liveuser |
1100 |
}#if end
|
| 226 |
liveuser |
1101 |
|
| 3 |
liveuser |
1102 |
#取得PATH字串陣列
|
|
|
1103 |
$PATHS=$spiltString["dataArray"];
|
| 226 |
liveuser |
1104 |
|
| 3 |
liveuser |
1105 |
#預設沒有找到程式
|
|
|
1106 |
$cmdFound=false;
|
| 226 |
liveuser |
1107 |
|
| 218 |
liveuser |
1108 |
#如果指令為 "if"
|
|
|
1109 |
if($result["cmd"]==="if"){
|
| 226 |
liveuser |
1110 |
|
| 218 |
liveuser |
1111 |
#則其為 bash 提供的控制敘述,一定存在,不用檢查.
|
|
|
1112 |
$cmdFound=true;
|
| 226 |
liveuser |
1113 |
|
| 218 |
liveuser |
1114 |
}#if end
|
| 226 |
liveuser |
1115 |
|
| 3 |
liveuser |
1116 |
#如果要執行的程式有指定位置
|
| 218 |
liveuser |
1117 |
else if(strpos($result["cmd"],"/")!==false){
|
| 226 |
liveuser |
1118 |
|
| 3 |
liveuser |
1119 |
#要運行的指令
|
| 218 |
liveuser |
1120 |
$cmd="if [ -f ".escapeshellarg($result["cmd"])." ]; then".PHP_EOL."exit;".PHP_EOL."else".PHP_EOL."exit 1;".PHP_EOL."fi";
|
| 226 |
liveuser |
1121 |
|
| 3 |
liveuser |
1122 |
#初始化輸出
|
|
|
1123 |
$output=array();
|
| 226 |
liveuser |
1124 |
|
| 3 |
liveuser |
1125 |
#執行指令
|
|
|
1126 |
exec($cmd,$output,$status);
|
| 255 |
liveuser |
1127 |
|
| 3 |
liveuser |
1128 |
#如果有找到
|
|
|
1129 |
if($status===0){
|
| 226 |
liveuser |
1130 |
|
| 3 |
liveuser |
1131 |
#設置有找到
|
|
|
1132 |
$cmdFound=true;
|
| 226 |
liveuser |
1133 |
|
| 3 |
liveuser |
1134 |
}#if end
|
| 226 |
liveuser |
1135 |
|
| 3 |
liveuser |
1136 |
}#if end
|
| 226 |
liveuser |
1137 |
|
| 3 |
liveuser |
1138 |
#反之
|
|
|
1139 |
else{
|
| 226 |
liveuser |
1140 |
|
| 3 |
liveuser |
1141 |
#針對每個 $PATHS
|
|
|
1142 |
foreach($PATHS as $path){
|
| 226 |
liveuser |
1143 |
|
| 3 |
liveuser |
1144 |
#要運行的指令
|
| 218 |
liveuser |
1145 |
$cmd="if [ -f ".escapeshellarg($path."/".$result["cmd"])." ]; then".PHP_EOL."exit;".PHP_EOL."else".PHP_EOL."exit 1;".PHP_EOL."fi";
|
| 226 |
liveuser |
1146 |
|
| 3 |
liveuser |
1147 |
#初始化輸出
|
|
|
1148 |
$output=array();
|
| 226 |
liveuser |
1149 |
|
| 3 |
liveuser |
1150 |
#執行指令
|
|
|
1151 |
exec($cmd,$output,$status);
|
| 226 |
liveuser |
1152 |
|
| 3 |
liveuser |
1153 |
#如果執行正常
|
|
|
1154 |
if($status===0){
|
| 226 |
liveuser |
1155 |
|
| 3 |
liveuser |
1156 |
#代表有找到程式
|
|
|
1157 |
$cmdFound=true;
|
| 226 |
liveuser |
1158 |
|
| 3 |
liveuser |
1159 |
#跳離 foreach
|
|
|
1160 |
break;
|
| 226 |
liveuser |
1161 |
|
| 3 |
liveuser |
1162 |
}#if end
|
| 226 |
liveuser |
1163 |
|
| 3 |
liveuser |
1164 |
}#foreach end
|
| 226 |
liveuser |
1165 |
|
| 3 |
liveuser |
1166 |
}#else end
|
| 226 |
liveuser |
1167 |
|
| 3 |
liveuser |
1168 |
#如果找不到要執行的程式
|
|
|
1169 |
if($cmdFound===false){
|
| 226 |
liveuser |
1170 |
|
| 255 |
liveuser |
1171 |
#取得搜尋 "=" 的結果
|
| 3 |
liveuser |
1172 |
$equalPosition=strpos($result["cmd"],"=");
|
| 226 |
liveuser |
1173 |
|
| 3 |
liveuser |
1174 |
#若指令不為 shell 變數指派
|
|
|
1175 |
if( $equalPosition===false ){
|
| 226 |
liveuser |
1176 |
|
| 3 |
liveuser |
1177 |
#設置執行失敗
|
|
|
1178 |
$result["status"]="false";
|
| 226 |
liveuser |
1179 |
|
| 3 |
liveuser |
1180 |
#設置執行錯誤訊息
|
|
|
1181 |
$result["error"]="要執行的程式「".$result["cmd"]."」不存在!";
|
| 226 |
liveuser |
1182 |
|
| 3 |
liveuser |
1183 |
#回傳結果
|
|
|
1184 |
return $result;
|
| 226 |
liveuser |
1185 |
|
| 3 |
liveuser |
1186 |
}#if end
|
| 226 |
liveuser |
1187 |
|
| 3 |
liveuser |
1188 |
#代表有找到程式
|
|
|
1189 |
$cmdFound=true;
|
| 226 |
liveuser |
1190 |
|
| 3 |
liveuser |
1191 |
}#if end
|
| 226 |
liveuser |
1192 |
|
| 255 |
liveuser |
1193 |
#初始化儲存未escaped後的指令與參數資訊
|
|
|
1194 |
$result["noEscaped"]=array();
|
| 226 |
liveuser |
1195 |
|
| 255 |
liveuser |
1196 |
#儲存執行的指令
|
|
|
1197 |
$result["noEscaped"]["cmd"]=$conf["command"];
|
| 226 |
liveuser |
1198 |
|
| 255 |
liveuser |
1199 |
#初始化儲存執行的參數
|
|
|
1200 |
$result["noEscaped"]["argu"]=array();
|
|
|
1201 |
|
|
|
1202 |
#初始化儲存escaped後的指令與參數資訊
|
|
|
1203 |
$result["escaped"]=array();
|
| 226 |
liveuser |
1204 |
|
| 255 |
liveuser |
1205 |
#儲存執行的指令
|
|
|
1206 |
$result["escaped"]["cmd"]=escapeshellarg($conf["command"]);
|
| 226 |
liveuser |
1207 |
|
| 255 |
liveuser |
1208 |
#初始化儲存執行的參數
|
|
|
1209 |
$result["escaped"]["argu"]=array();
|
|
|
1210 |
|
|
|
1211 |
#如果有參數存在
|
|
|
1212 |
if(isset($conf["argu"])){
|
| 226 |
liveuser |
1213 |
|
| 255 |
liveuser |
1214 |
#有幾個參數就執行幾次
|
|
|
1215 |
for($i=0;$i<count($conf["argu"]);$i++){
|
| 226 |
liveuser |
1216 |
|
| 255 |
liveuser |
1217 |
#如果有指定位置參數
|
|
|
1218 |
if(isset($conf["arguIsAddr"])){
|
| 226 |
liveuser |
1219 |
|
| 255 |
liveuser |
1220 |
#如果是位置參數
|
|
|
1221 |
if($conf["arguIsAddr"][$i]==="true"){
|
| 226 |
liveuser |
1222 |
|
| 255 |
liveuser |
1223 |
#如果相對路徑就要轉換路徑
|
|
|
1224 |
if(strpos($conf["argu"][$i],"/")!==0){
|
| 226 |
liveuser |
1225 |
|
| 255 |
liveuser |
1226 |
#函數說明:
|
|
|
1227 |
#將多個路徑字串變成相對於當前路徑的相對路徑字串
|
|
|
1228 |
#回傳結果:
|
|
|
1229 |
#$result["status"],"true"爲建立成功,"false"爲建立失敗.
|
|
|
1230 |
#$result["error"],錯誤訊息陣列.
|
|
|
1231 |
#$result["function"],函數名稱.
|
|
|
1232 |
#$result["content"],字串陣列,多個轉換好的相對路徑字串.
|
|
|
1233 |
#必填參數:
|
|
|
1234 |
#$conf["path"],陣列字串,要轉換成相對路徑的字串.;
|
|
|
1235 |
$conf["fileAccess::getRelativePath"]["path"]=array($conf["argu"][$i]);
|
|
|
1236 |
#$conf["fileArgu"],字串,當前路徑.
|
|
|
1237 |
$conf["fileAccess::getRelativePath"]["fileArgu"]=$conf["fileArgu"];
|
|
|
1238 |
$getRelativePath=fileAccess::getRelativePath($conf["fileAccess::getRelativePath"]);
|
|
|
1239 |
unset($conf["fileAccess::getRelativePath"]);
|
| 226 |
liveuser |
1240 |
|
| 255 |
liveuser |
1241 |
#var_dump($getRelativePath);
|
| 226 |
liveuser |
1242 |
|
| 255 |
liveuser |
1243 |
#如果轉換路徑失敗
|
|
|
1244 |
if($getRelativePath["status"]==="false"){
|
| 226 |
liveuser |
1245 |
|
| 255 |
liveuser |
1246 |
#設置執行失敗的識別
|
|
|
1247 |
$result["status"]="false";
|
|
|
1248 |
|
|
|
1249 |
#設置執行失敗的訊息
|
|
|
1250 |
$result["error"]=$getRelativePath;
|
| 226 |
liveuser |
1251 |
|
| 255 |
liveuser |
1252 |
#回傳結果
|
|
|
1253 |
return $result;
|
| 226 |
liveuser |
1254 |
|
| 255 |
liveuser |
1255 |
}#if end
|
| 226 |
liveuser |
1256 |
|
| 255 |
liveuser |
1257 |
#取得相對路徑
|
|
|
1258 |
$conf["argu"][$i]=$getRelativePath["content"][0];
|
| 226 |
liveuser |
1259 |
|
| 255 |
liveuser |
1260 |
#取得相對路徑
|
|
|
1261 |
$relativePath=$getRelativePath["content"][0];
|
| 226 |
liveuser |
1262 |
|
| 255 |
liveuser |
1263 |
#保存其相對路徑
|
|
|
1264 |
$conf["argu"][$i]=$relativePath;
|
| 226 |
liveuser |
1265 |
|
| 3 |
liveuser |
1266 |
}#if end
|
| 226 |
liveuser |
1267 |
|
| 3 |
liveuser |
1268 |
}#if end
|
| 255 |
liveuser |
1269 |
|
|
|
1270 |
}#if end
|
|
|
1271 |
|
|
|
1272 |
#如果參數長度大於3
|
|
|
1273 |
if(strlen($conf["argu"][$i])>3){
|
| 226 |
liveuser |
1274 |
|
| 255 |
liveuser |
1275 |
#如果參數是 ${xxx}
|
|
|
1276 |
if( strpos($conf["argu"][$i],"\${")===0 && strpos($conf["argu"][$i],"}")===(strlen($conf["argu"][$i])-1) ){
|
| 226 |
liveuser |
1277 |
|
| 255 |
liveuser |
1278 |
#預設要空格
|
|
|
1279 |
$space=" ";
|
| 226 |
liveuser |
1280 |
|
| 255 |
liveuser |
1281 |
#如果前一個參數為 "=" 結尾
|
|
|
1282 |
if($result["cmd"][strlen($result["cmd"])-1]==="="){
|
| 226 |
liveuser |
1283 |
|
| 255 |
liveuser |
1284 |
#不放空格
|
|
|
1285 |
$space="";
|
| 226 |
liveuser |
1286 |
|
| 255 |
liveuser |
1287 |
#如果是第一個參數
|
|
|
1288 |
if($i===0){
|
| 226 |
liveuser |
1289 |
|
| 255 |
liveuser |
1290 |
#更新真正執行的指令
|
|
|
1291 |
$result["cmd"]=$result["cmd"].$space.$conf["argu"][$i];
|
|
|
1292 |
|
|
|
1293 |
#儲存escape的cmd
|
|
|
1294 |
$result["escaped"]["cmd"]=escapeshellarg($result["cmd"]);
|
|
|
1295 |
|
|
|
1296 |
#儲存未escape的cmd
|
|
|
1297 |
$result["noEscaped"]["cmd"]=$result["cmd"];
|
| 226 |
liveuser |
1298 |
|
| 255 |
liveuser |
1299 |
}#if end
|
| 226 |
liveuser |
1300 |
|
| 255 |
liveuser |
1301 |
#反之真的為參數
|
|
|
1302 |
else{
|
| 226 |
liveuser |
1303 |
|
| 255 |
liveuser |
1304 |
#取得參數的索引
|
|
|
1305 |
$arguIndex=count($conf["argu"])-1;
|
| 226 |
liveuser |
1306 |
|
| 255 |
liveuser |
1307 |
#儲存真正的參數
|
|
|
1308 |
$result["escaped"]["argu"][$arguIndex]=$conf["argu"][$arguIndex].$space.$conf["argu"][$i];
|
| 226 |
liveuser |
1309 |
|
| 255 |
liveuser |
1310 |
#儲存未escape的參數
|
|
|
1311 |
$result["unescape"]["argu"][$arguIndex]=$conf["argu"][$arguIndex].$space.$conf["argu"][$i];
|
| 226 |
liveuser |
1312 |
|
| 255 |
liveuser |
1313 |
}#else end
|
| 226 |
liveuser |
1314 |
|
| 255 |
liveuser |
1315 |
}#if end
|
| 226 |
liveuser |
1316 |
|
| 255 |
liveuser |
1317 |
#反之
|
|
|
1318 |
else{
|
| 226 |
liveuser |
1319 |
|
| 255 |
liveuser |
1320 |
#儲存未escape的參數
|
|
|
1321 |
$result["noescaped"]["argu"][]=$conf["argu"][$i];
|
|
|
1322 |
|
|
|
1323 |
#儲存escape的參數
|
|
|
1324 |
$result["escaped"]["argu"][]=escapeshellarg($conf["argu"][$i]);
|
| 226 |
liveuser |
1325 |
|
| 255 |
liveuser |
1326 |
}#else
|
| 226 |
liveuser |
1327 |
|
| 255 |
liveuser |
1328 |
#組合執行的指令
|
|
|
1329 |
$result["cmd"]=$result["cmd"].$space.$conf["argu"][$i];
|
| 226 |
liveuser |
1330 |
|
| 255 |
liveuser |
1331 |
#下一個參數
|
|
|
1332 |
continue;
|
| 226 |
liveuser |
1333 |
|
| 3 |
liveuser |
1334 |
}#if end
|
| 226 |
liveuser |
1335 |
|
| 255 |
liveuser |
1336 |
}#if end
|
| 226 |
liveuser |
1337 |
|
| 255 |
liveuser |
1338 |
#如果參數是 < 、<< 、> 、>> 、| 、2>&1、;、`、~、&、$!
|
|
|
1339 |
if($conf["argu"][$i]===">" || $conf["argu"][$i]==="<" || $conf["argu"][$i]===">>" || $conf["argu"][$i]==="<<" || $conf["argu"][$i]==="|" || $conf["argu"][$i]==="2>&1" || $conf["argu"][$i]===";" || $conf["argu"][$i]==="`" || $conf["argu"][$i]==="~" || $conf["argu"][$i]==="&" || $conf["argu"][$i]==="$!"){
|
| 226 |
liveuser |
1340 |
|
| 255 |
liveuser |
1341 |
#預設要空格
|
|
|
1342 |
$space=" ";
|
| 226 |
liveuser |
1343 |
|
| 255 |
liveuser |
1344 |
#組合執行的指令
|
|
|
1345 |
$result["cmd"]=$result["cmd"].$space.$conf["argu"][$i];
|
| 226 |
liveuser |
1346 |
|
| 255 |
liveuser |
1347 |
#儲存真正的參數
|
|
|
1348 |
$result["escaped"]["argu"][]=$conf["argu"][$i];
|
|
|
1349 |
|
|
|
1350 |
}#if end
|
|
|
1351 |
|
|
|
1352 |
#反之
|
|
|
1353 |
else{
|
|
|
1354 |
|
|
|
1355 |
#如果是第一個參數,且指令結尾為「=」結束
|
|
|
1356 |
if($i===0 && $result["cmd"][strlen($result["cmd"])-1]==="="){
|
|
|
1357 |
|
|
|
1358 |
#處理參數後再串接指令
|
|
|
1359 |
$result["cmd"]=$result["cmd"].escapeshellarg($conf["argu"][$i]);
|
|
|
1360 |
|
|
|
1361 |
#更新真正執行的指令
|
|
|
1362 |
$result["escaped"]["cmd"]=$result["cmd"];
|
|
|
1363 |
|
|
|
1364 |
#儲存unescape的指令
|
|
|
1365 |
$result["noescaped"]["cmd"]=$result["cmd"];
|
|
|
1366 |
|
| 3 |
liveuser |
1367 |
}#if end
|
| 226 |
liveuser |
1368 |
|
| 3 |
liveuser |
1369 |
#反之
|
|
|
1370 |
else{
|
| 226 |
liveuser |
1371 |
|
| 255 |
liveuser |
1372 |
#儲存unescape的參數
|
|
|
1373 |
$unescapeArgu=$conf["argu"][$i];
|
| 226 |
liveuser |
1374 |
|
| 255 |
liveuser |
1375 |
#儲存escape好的參數
|
|
|
1376 |
$escapeArgu=escapeshellarg($conf["argu"][$i]);
|
| 226 |
liveuser |
1377 |
|
| 255 |
liveuser |
1378 |
#如果有設置 $conf["thereIsShellVar"]
|
|
|
1379 |
if(isset($conf["thereIsShellVar"])){
|
| 226 |
liveuser |
1380 |
|
| 255 |
liveuser |
1381 |
#如果有設置 $conf["thereIsShellVar"][$i]
|
|
|
1382 |
if(isset($conf["thereIsShellVar"][$i])){
|
| 226 |
liveuser |
1383 |
|
| 255 |
liveuser |
1384 |
#如果含有 shell 變數
|
|
|
1385 |
if($conf["thereIsShellVar"][$i]==="true"){
|
| 226 |
liveuser |
1386 |
|
| 255 |
liveuser |
1387 |
#檢查是否使用 shell 變數
|
|
|
1388 |
#函式說明:
|
|
|
1389 |
#尋找陣列中有無符合的key
|
|
|
1390 |
#回傳結果:
|
|
|
1391 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
1392 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
1393 |
#$result["function"],當前執行的函式名稱.
|
|
|
1394 |
#$result["argu"],所使用的參數.
|
|
|
1395 |
#$result["founded"],是否有找到shell變數,"true"代表有找到,"false"代表沒有找到..
|
|
|
1396 |
#必填參數:
|
|
|
1397 |
#$conf["input"],字串,要檢查的字串..
|
|
|
1398 |
$conf["search::findShellVar"]["input"]=$escapeArgu;
|
|
|
1399 |
#可省略參數:
|
|
|
1400 |
#無.
|
|
|
1401 |
#參考資料:
|
|
|
1402 |
#無.
|
|
|
1403 |
#備註:
|
|
|
1404 |
#無.
|
|
|
1405 |
$findShellVar=search::findShellVar($conf["search::findShellVar"]);
|
|
|
1406 |
unset($conf["search::findShellVar"]);
|
| 226 |
liveuser |
1407 |
|
| 255 |
liveuser |
1408 |
#如果檢查失敗
|
|
|
1409 |
if($findShellVar["status"]=="false"){
|
| 226 |
liveuser |
1410 |
|
| 255 |
liveuser |
1411 |
#設置錯誤識別
|
|
|
1412 |
$result["status"]="false";
|
| 226 |
liveuser |
1413 |
|
| 255 |
liveuser |
1414 |
#設置錯誤訊息
|
|
|
1415 |
$result["error"]=$findShellVar;
|
| 226 |
liveuser |
1416 |
|
| 255 |
liveuser |
1417 |
#回傳解果
|
|
|
1418 |
return $result;
|
|
|
1419 |
|
|
|
1420 |
}#if end
|
|
|
1421 |
|
|
|
1422 |
#含有 shell 變數
|
|
|
1423 |
if($findShellVar["founded"]=="true"){
|
|
|
1424 |
|
|
|
1425 |
#確認該字串是否 開頭跟結尾為 「'」
|
|
|
1426 |
#函式說明:
|
|
|
1427 |
#取得符合特定字首與字尾的字串
|
| 3 |
liveuser |
1428 |
#回傳結果:
|
| 255 |
liveuser |
1429 |
#$result["status"],若爲"true"則代表執行正常;若爲"false"則代表執行失敗。
|
|
|
1430 |
#$result["function"],當前執行的函數名稱.
|
|
|
1431 |
#$result["error"],錯誤訊息陣列.
|
|
|
1432 |
#$result["founded"],若為"true"則代表有找到符合字首條件的結果;若爲"false"則代表沒有找到。
|
|
|
1433 |
#$result["returnString"],爲符合字首條件的字串內容。
|
|
|
1434 |
#$result["argu"],使用的參數.
|
| 3 |
liveuser |
1435 |
#必填參數:
|
| 255 |
liveuser |
1436 |
#$conf["checkString"],字串,要檢查的字串.
|
|
|
1437 |
$conf["search::getMeetConditionsString"]["checkString"]=$escapeArgu;
|
| 3 |
liveuser |
1438 |
#可省略參數:
|
| 255 |
liveuser |
1439 |
#$conf["frontWord"],字串,用來檢查字首應該要有什麼字串,預設不指定.
|
|
|
1440 |
$conf["search::getMeetConditionsString"]["frontWord"]="'";
|
|
|
1441 |
#$conf["tailWord"],字串,用來檢查字尾應該要有什麼字串,預設不指定.
|
|
|
1442 |
$conf["search::getMeetConditionsString"]["tailWord"]="'";
|
| 3 |
liveuser |
1443 |
#參考資料:
|
| 255 |
liveuser |
1444 |
#str_spilt(),可以將字串依照字母分割成一個個陣列字串。
|
| 3 |
liveuser |
1445 |
#備註:
|
|
|
1446 |
#無.
|
| 255 |
liveuser |
1447 |
$getMeetConditionsString=search::getMeetConditionsString($conf["search::getMeetConditionsString"]);
|
|
|
1448 |
unset($conf["search::getMeetConditionsString"]);
|
| 226 |
liveuser |
1449 |
|
| 3 |
liveuser |
1450 |
#如果檢查失敗
|
| 255 |
liveuser |
1451 |
if($getMeetConditionsString["status"]=="false"){
|
| 226 |
liveuser |
1452 |
|
| 3 |
liveuser |
1453 |
#設置錯誤識別
|
|
|
1454 |
$result["status"]="false";
|
| 226 |
liveuser |
1455 |
|
| 3 |
liveuser |
1456 |
#設置錯誤訊息
|
| 255 |
liveuser |
1457 |
$result["error"]=$getMeetConditionsString;
|
| 226 |
liveuser |
1458 |
|
| 3 |
liveuser |
1459 |
#回傳解果
|
|
|
1460 |
return $result;
|
| 226 |
liveuser |
1461 |
|
| 3 |
liveuser |
1462 |
}#if end
|
| 226 |
liveuser |
1463 |
|
| 3 |
liveuser |
1464 |
#含有 shell 變數
|
| 255 |
liveuser |
1465 |
if($getMeetConditionsString["founded"]==="true"){
|
| 226 |
liveuser |
1466 |
|
| 255 |
liveuser |
1467 |
#將含有 shell 變數的參數變成 '....'${hellVar}'....'
|
| 3 |
liveuser |
1468 |
#函式說明:
|
| 255 |
liveuser |
1469 |
#處理字串避免網頁出錯
|
| 3 |
liveuser |
1470 |
#回傳結果:
|
| 255 |
liveuser |
1471 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
1472 |
#$result["function"],當前執行的函數.
|
|
|
1473 |
#$result["content"],爲處理好的字串.
|
| 3 |
liveuser |
1474 |
#$result["error"],錯誤訊息陣列.
|
|
|
1475 |
#$result["argu"],使用的參數.
|
|
|
1476 |
#必填參數:
|
| 255 |
liveuser |
1477 |
#$conf["stringIn"],字串,爲要處理的字串
|
|
|
1478 |
$conf["stringProcess::correctCharacter"]["stringIn"]=$escapeArgu;
|
| 3 |
liveuser |
1479 |
#可省略參數:
|
| 255 |
liveuser |
1480 |
#$conf["selectedCharacter"],字串陣列,爲被選擇要處理的字串/字元,須爲陣列值。若不設定則預設爲要將這些字串作替換 ("<",">","=","//","'","$","%","&","|","/*","*","#","\"").特殊字元,「\n」代表換行,「\t」代表tab鍵的間隔
|
|
|
1481 |
$conf["stringProcess::correctCharacter"]["selectedCharacter"]=array("\${","}");
|
|
|
1482 |
#$conf["changeTo"],字串陣列,爲被選擇的字元要換成什麼字串/字元,須爲陣列值。若不設定,則預設爲更換成""(空字串).
|
|
|
1483 |
$conf["stringProcess::correctCharacter"]["changeTo"]=array("'\${","}'");
|
| 3 |
liveuser |
1484 |
#參考資料:
|
| 255 |
liveuser |
1485 |
#無.
|
| 3 |
liveuser |
1486 |
#備註:
|
|
|
1487 |
#無.
|
| 255 |
liveuser |
1488 |
$correctCharacter=stringProcess::correctCharacter($conf["stringProcess::correctCharacter"]);
|
|
|
1489 |
unset($conf["stringProcess::correctCharacter"]);
|
| 226 |
liveuser |
1490 |
|
| 255 |
liveuser |
1491 |
#如果執行失敗
|
|
|
1492 |
if($correctCharacter["status"]=="false"){
|
| 226 |
liveuser |
1493 |
|
| 3 |
liveuser |
1494 |
#設置錯誤識別
|
|
|
1495 |
$result["status"]="false";
|
| 226 |
liveuser |
1496 |
|
| 3 |
liveuser |
1497 |
#設置錯誤訊息
|
| 255 |
liveuser |
1498 |
$result["error"]=$correctCharacter;
|
| 226 |
liveuser |
1499 |
|
| 3 |
liveuser |
1500 |
#回傳解果
|
|
|
1501 |
return $result;
|
| 226 |
liveuser |
1502 |
|
| 3 |
liveuser |
1503 |
}#if end
|
| 226 |
liveuser |
1504 |
|
| 255 |
liveuser |
1505 |
#更新成可以呈現 shell 變數的字串
|
|
|
1506 |
$escapeArgu=$correctCharacter["content"];
|
| 226 |
liveuser |
1507 |
|
| 3 |
liveuser |
1508 |
}#if end
|
| 226 |
liveuser |
1509 |
|
| 3 |
liveuser |
1510 |
}#if end
|
| 226 |
liveuser |
1511 |
|
| 3 |
liveuser |
1512 |
}#if end
|
| 226 |
liveuser |
1513 |
|
| 3 |
liveuser |
1514 |
}#if end
|
| 226 |
liveuser |
1515 |
|
| 255 |
liveuser |
1516 |
}#if end
|
|
|
1517 |
|
|
|
1518 |
#如果是要執行 escpaed 後的指令
|
|
|
1519 |
if($conf["escapeshellarg"]==="true"){
|
|
|
1520 |
|
| 3 |
liveuser |
1521 |
#串接指令
|
|
|
1522 |
$result["cmd"]=$result["cmd"]." ".$escapeArgu;
|
| 255 |
liveuser |
1523 |
|
|
|
1524 |
}#if end
|
|
|
1525 |
|
|
|
1526 |
#反之
|
|
|
1527 |
else{
|
|
|
1528 |
|
|
|
1529 |
#串接指令
|
|
|
1530 |
$result["cmd"]=$result["cmd"]." ".$unescapeArgu;
|
|
|
1531 |
|
|
|
1532 |
}#else end
|
| 226 |
liveuser |
1533 |
|
| 255 |
liveuser |
1534 |
#儲存真正的參數
|
|
|
1535 |
$result["escaped"]["argu"][]=$escapeArgu;
|
|
|
1536 |
|
|
|
1537 |
#儲存未escape的參數
|
|
|
1538 |
$result["noEscaped"]["argu"][]=$unescapeArgu;
|
| 226 |
liveuser |
1539 |
|
| 3 |
liveuser |
1540 |
}#else end
|
| 226 |
liveuser |
1541 |
|
| 3 |
liveuser |
1542 |
}#for end
|
| 226 |
liveuser |
1543 |
|
| 3 |
liveuser |
1544 |
}#if end
|
| 226 |
liveuser |
1545 |
|
| 3 |
liveuser |
1546 |
}#if end
|
| 226 |
liveuser |
1547 |
|
| 3 |
liveuser |
1548 |
#如果有設置使用者名稱
|
|
|
1549 |
if(isset($conf["username"])){
|
| 226 |
liveuser |
1550 |
|
| 3 |
liveuser |
1551 |
#檢查環境為apache還是cmd
|
|
|
1552 |
#涵式說明:
|
|
|
1553 |
#判斷當前環境為web還是cmd
|
|
|
1554 |
#回傳結果:
|
|
|
1555 |
#$result,"web"或"cmd"
|
|
|
1556 |
if(csInformation::getEnv()=="web"){
|
| 226 |
liveuser |
1557 |
|
| 3 |
liveuser |
1558 |
#設置執行失敗
|
|
|
1559 |
$result["status"]="false";
|
| 226 |
liveuser |
1560 |
|
| 3 |
liveuser |
1561 |
#設置執行錯誤訊息
|
|
|
1562 |
$result["error"][]="函數 ".$result["function"]." 的 username 參數僅能在命令列環境下運行!";
|
| 226 |
liveuser |
1563 |
|
| 3 |
liveuser |
1564 |
#回傳結果
|
|
|
1565 |
return $result;
|
| 226 |
liveuser |
1566 |
|
| 3 |
liveuser |
1567 |
}#if end
|
| 226 |
liveuser |
1568 |
|
| 3 |
liveuser |
1569 |
#函式說明:
|
|
|
1570 |
#取得用戶端的資訊,並依據需要寫入到資料表裡面
|
|
|
1571 |
#回傳的結果:
|
|
|
1572 |
#$result["status"],執行是否正常,"true"代表執行成功,"false"代表執行失敗.
|
|
|
1573 |
#$result["error"],錯誤訊息.
|
|
|
1574 |
#$result["function"],檔前執行的函數名稱.
|
|
|
1575 |
#$result["mode"],當前的模式是"cmd"還是"web".
|
|
|
1576 |
#$result["userBrowserType"],爲使用者的瀏覽器資訊
|
|
|
1577 |
#$result["userIp"],爲使用者的IP
|
|
|
1578 |
#$result["serverIp"],為伺服器的IP
|
|
|
1579 |
#$result["server_name"],伺服器的 domain name
|
|
|
1580 |
#$result["scheme"],通訊協定
|
|
|
1581 |
#$result["serverPort"],伺服器給對外下載網頁的port
|
|
|
1582 |
#$result["requestUri"],爲使用者要求的網址/php檔案.
|
|
|
1583 |
#$result["requestUriFull"],為使用者要求的完整網址/php檔案路徑.
|
|
|
1584 |
#$result["clientRequestIP"],用戶端要求的ip與port
|
|
|
1585 |
#$result["username"],爲使用者目前的帳戶,若爲""則表示尚未登入成功
|
|
|
1586 |
#$result["phpUser"],運行該php的使用者帳戶.若為空字串則代表非使用者直接觸發.
|
|
|
1587 |
#$result["phpUserType"],運行該php的使用者帳戶類型,可能有"regular(no wheel member)","wheel(can use sudo)","intrinsic(root)","system(qemu,apache,...)".
|
|
|
1588 |
#$result["header"],接收到的 header 陣列.
|
|
|
1589 |
#$result["body"],接收到的 body 字串.
|
|
|
1590 |
#必填參數:
|
|
|
1591 |
#$conf["getAccount"],字串,是否要取得帳號,"true"代表要;"false"代表不要.
|
|
|
1592 |
$conf["csInformation::getConnectionInfo"]["getAccount"]="true";
|
|
|
1593 |
#可省略參數:
|
|
|
1594 |
#$conf["accountVar"],字串,帳號儲存在哪個變數裏面,預設爲$_SESSION["username"].
|
|
|
1595 |
#$conf["accountVar"]=$_SESSION["username"];
|
|
|
1596 |
#$conf["saveToDb"],字串,是否要除儲存到資料庫,"true"為要儲存",預設為"false"不儲存.
|
|
|
1597 |
#$conf["saveToDb"]="true";
|
|
|
1598 |
#$conf["dbAddress"],字串,爲mysql/mariadb server的位置,若saveToDb設為"true",則該參數為必填.
|
|
|
1599 |
#$conf["dbAddress"]=$dbAddress;
|
|
|
1600 |
#$conf["dbAccount"],字串,爲用於連入mysql/mariadb server時要使用的帳號,若saveToDb設為"true",則該參數為必填.
|
|
|
1601 |
#$conf["dbAccount"]=$dbAccount;
|
|
|
1602 |
#$conf["dbName"],字串,要選取的資料庫名稱,若saveToDb設為"true",則該參數為必填.
|
|
|
1603 |
#$conf["dbName"]=$dbName;
|
|
|
1604 |
#$conf["tableName"],字串,爲要插入資料的資料表名稱,若saveToDb設為"true",則該參數為必填.
|
|
|
1605 |
#$conf["tableName"]="visitorInfo";
|
|
|
1606 |
#$conf["columnName"],字串陣列,爲資料表的項目名稱,例如:$conf["columnName"]=array("columnName1","columnName2","columnName3",...);寫入的資料依序為,使用者帳戶、瀏覽器資訊、使用者IP、觀看的網址、當時的時間.若saveToDb設為"true",則該參數為必填.
|
|
|
1607 |
#$conf["columnName"]=array("username","userWebBrowser","userIp","requestUri","systemDateAndTime");
|
|
|
1608 |
#$conf["dbPassword"],字串,爲連線到mysql/mariadb server時要使用的密碼,可省略,若省略則代表不使用密碼.
|
|
|
1609 |
#$conf["dbPassword"]=$dbPassword;
|
|
|
1610 |
#參考資料:
|
|
|
1611 |
#$_SERVER=>http://php.net/manual/zh/reserved.variables.server.php
|
|
|
1612 |
#取得伺服器名稱與IP=>http://php.net/manual/en/function.gethostname.php
|
|
|
1613 |
#備註:
|
|
|
1614 |
#無.
|
|
|
1615 |
$getConnectionInfo=csInformation::getConnectionInfo($conf["csInformation::getConnectionInfo"]);
|
|
|
1616 |
unset($conf["csInformation::getConnectionInfo"]);
|
| 226 |
liveuser |
1617 |
|
| 3 |
liveuser |
1618 |
#如果執行失敗
|
|
|
1619 |
if($getConnectionInfo["status"]==="false"){
|
| 226 |
liveuser |
1620 |
|
| 3 |
liveuser |
1621 |
#設置執行失敗的識別
|
|
|
1622 |
$result["status"]="false";
|
| 226 |
liveuser |
1623 |
|
| 3 |
liveuser |
1624 |
#設置執行失敗的訊息
|
|
|
1625 |
$result["error"]=$getConnectionInfo;
|
| 226 |
liveuser |
1626 |
|
| 3 |
liveuser |
1627 |
#回傳結果
|
|
|
1628 |
return $result;
|
| 226 |
liveuser |
1629 |
|
| 3 |
liveuser |
1630 |
}#if end
|
| 226 |
liveuser |
1631 |
|
| 3 |
liveuser |
1632 |
#如果不是以運行php的使用者角色來執行shell
|
|
|
1633 |
if($conf["username"]!==$getConnectionInfo["phpUser"]){
|
| 226 |
liveuser |
1634 |
|
| 3 |
liveuser |
1635 |
#如果有設置使用者密碼
|
|
|
1636 |
if(isset($conf["password"])){
|
| 226 |
liveuser |
1637 |
|
| 3 |
liveuser |
1638 |
#透過使用者與密碼來執行指令
|
|
|
1639 |
$result["cmd"]="echo ".$conf["password"]." | su ".$conf["username"]." -c '".$result["cmd"]."'";
|
| 226 |
liveuser |
1640 |
|
| 3 |
liveuser |
1641 |
}#if end
|
| 226 |
liveuser |
1642 |
|
| 3 |
liveuser |
1643 |
#反之沒有設置密碼
|
|
|
1644 |
else{
|
| 226 |
liveuser |
1645 |
|
| 3 |
liveuser |
1646 |
#透過指定的使用者來執行指令
|
|
|
1647 |
$result["cmd"]="su ".$conf["username"]." -c '".$result["cmd"]."'";
|
| 226 |
liveuser |
1648 |
|
| 3 |
liveuser |
1649 |
}#else end
|
| 226 |
liveuser |
1650 |
|
| 60 |
liveuser |
1651 |
}#if end
|
| 226 |
liveuser |
1652 |
|
| 3 |
liveuser |
1653 |
}#if end
|
| 226 |
liveuser |
1654 |
|
| 3 |
liveuser |
1655 |
#如果 $conf["useScript"] 為 "true"
|
|
|
1656 |
if($conf["useScript"]==="true"){
|
| 226 |
liveuser |
1657 |
|
| 3 |
liveuser |
1658 |
#轉換 $conf["logFile"] 為絕對路徑
|
|
|
1659 |
#函數說明:
|
|
|
1660 |
#將檔案的位置名稱變成網址,也可以取得檔案位於伺服器上檔案系統的絕對位置.
|
|
|
1661 |
#回傳結果:
|
|
|
1662 |
#$result["status"],"true"爲建立成功,"false"爲建立失敗.
|
|
|
1663 |
#$result["error"],錯誤訊息陣列.
|
| 226 |
liveuser |
1664 |
#$result["function"],函數名稱.
|
| 3 |
liveuser |
1665 |
#$result["content"],網址,若是在命令列執行,則為"null".
|
|
|
1666 |
#$result["webPathFromRoot"],相對於網頁根目錄的路徑.
|
|
|
1667 |
#$result["fileSystemAbsoulutePosition"],針對伺服器端的絕對位置,亦即從網頁「/」目錄開始的路徑.
|
|
|
1668 |
#$result["fileSystemRelativePosition"],針對伺服器檔案系統的相對位置.
|
|
|
1669 |
#必填參數:
|
|
|
1670 |
#$conf["address"],字串,檔案的相對位置,若為絕對位置則會自動轉換成相對位置.
|
|
|
1671 |
$conf["fileAccess::getInternetAddress"]["address"]=$conf["logFilePath"];
|
|
|
1672 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.
|
|
|
1673 |
$conf["fileAccess::getInternetAddress"]["fileArgu"]=$conf["fileArgu"];
|
|
|
1674 |
#可省略參數:
|
|
|
1675 |
#$conf["userDir"],字串,網頁是否置放於家目錄底下,"true"為是,"false"為不是,預設為"true".
|
|
|
1676 |
#$conf["fileAccess::getInternetAddress"]["userDir"]="true";
|
|
|
1677 |
#備註:
|
|
|
1678 |
#在命令列執行,所得的路徑是錯誤的。
|
|
|
1679 |
$getInternetAddress=fileAccess::getInternetAddress($conf["fileAccess::getInternetAddress"]);
|
|
|
1680 |
unset($conf["fileAccess::getInternetAddress"]);
|
| 226 |
liveuser |
1681 |
|
| 3 |
liveuser |
1682 |
#如果轉換路徑失敗
|
| 255 |
liveuser |
1683 |
if($getInternetAddress["status"]==="false"){
|
| 226 |
liveuser |
1684 |
|
| 3 |
liveuser |
1685 |
#設置執行失敗
|
|
|
1686 |
$result["status"]="false";
|
| 226 |
liveuser |
1687 |
|
| 3 |
liveuser |
1688 |
#設置執行錯誤
|
|
|
1689 |
$result["error"]=$getInternetAddress;
|
| 226 |
liveuser |
1690 |
|
| 3 |
liveuser |
1691 |
#回傳結果
|
|
|
1692 |
return $result;
|
| 226 |
liveuser |
1693 |
|
| 3 |
liveuser |
1694 |
}#if end
|
| 226 |
liveuser |
1695 |
|
| 3 |
liveuser |
1696 |
#取得轉換後的絕對路徑
|
|
|
1697 |
$conf["logFilePath"]=$getInternetAddress["fileSystemAbsoulutePosition"];
|
| 226 |
liveuser |
1698 |
|
| 3 |
liveuser |
1699 |
#確保logFile的路徑
|
|
|
1700 |
#涵式說明:
|
|
|
1701 |
#確保路徑存在.
|
|
|
1702 |
#回傳的結果:
|
|
|
1703 |
#$result["status"],執行正常與否,"true"代表正常,"false"代表不正常.
|
|
|
1704 |
#$result["error"],錯誤訊息陣列.
|
|
|
1705 |
#$resutl["function"],當前執行的涵式名稱.
|
|
|
1706 |
#$result["path"],建立好的路徑字串.
|
|
|
1707 |
#$result["fileName"],檔案名稱,若 $conf["haveFileName"] 為 "true" 則會回傳.
|
|
|
1708 |
#必填參數::
|
|
|
1709 |
#$conf["path"],要檢查的路徑
|
| 226 |
liveuser |
1710 |
$conf["fileAccess::validatePath"]["path"]=$conf["logFilePath"];
|
| 3 |
liveuser |
1711 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
|
|
|
1712 |
$conf["fileAccess::validatePath"]["fileArgu"]=$conf["fileArgu"];
|
|
|
1713 |
#可省略參數:
|
|
|
1714 |
#$conf["haveFileName"],字串,"true"代表有$conf["path"]檔案名稱,"false"代表$conf["path"]為純路徑,預設為"false".
|
|
|
1715 |
#$conf["fileAccess::validatePath"]["haveFileName"]="true";
|
|
|
1716 |
#$conf["dirPermission"],字串,新建資料夾的權限設定,預設爲0770,亦即擁有者,同群組者可以讀,寫,存取,其他人僅能存取.
|
|
|
1717 |
#$conf["dirPermission"]="";
|
|
|
1718 |
$validatePath=fileAccess::validatePath($conf["fileAccess::validatePath"]);
|
|
|
1719 |
unset($conf["fileAccess::validatePath"]);
|
| 226 |
liveuser |
1720 |
|
| 3 |
liveuser |
1721 |
#如果確保設定檔路徑失敗
|
|
|
1722 |
if($validatePath["status"]=="false"){
|
| 226 |
liveuser |
1723 |
|
| 3 |
liveuser |
1724 |
#設置執行不正常
|
|
|
1725 |
$result["status"]="false";
|
| 226 |
liveuser |
1726 |
|
| 3 |
liveuser |
1727 |
#設置執行錯誤
|
|
|
1728 |
$result["error"]=$validatePath;
|
| 226 |
liveuser |
1729 |
|
| 3 |
liveuser |
1730 |
#回傳結果
|
|
|
1731 |
return $result;
|
| 226 |
liveuser |
1732 |
|
| 3 |
liveuser |
1733 |
}#if end
|
| 226 |
liveuser |
1734 |
|
| 3 |
liveuser |
1735 |
#初始化 $conf["logFileName"]
|
|
|
1736 |
$conf["logFileName"]="";
|
| 226 |
liveuser |
1737 |
|
| 3 |
liveuser |
1738 |
#無窮迴圈
|
|
|
1739 |
for($i=0;$i<1;){
|
| 226 |
liveuser |
1740 |
|
| 3 |
liveuser |
1741 |
#函式說明:
|
|
|
1742 |
#php內建的date()函數
|
|
|
1743 |
#回傳結果:
|
|
|
1744 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
1745 |
#$result["error"],錯誤訊息陣列.
|
|
|
1746 |
#$result["content"],自釘格式的時間字串.
|
|
|
1747 |
#必填參數:
|
|
|
1748 |
#$conf["timeZone"],字串,時區代號,可以設定的時區列表=> http://www.php.net/manual/en/timezones.php 台灣則為"Asia/Taipei".
|
|
|
1749 |
$conf["time::buildInDate"]["timeZone"]="Asia/Taipei";
|
|
|
1750 |
#$conf["format"],字串,要顯示哪些時間單位,格式與順序為何?西元年為'Y',月份為'm',日為'd',星期為D,以24計的時為'G'以12計的時為'h',分為'i',秒為's',當年的第幾個禮拜為'W',時區為'e',當年第幾天為'z',若想顯示英文字請輸入"\字母".
|
|
|
1751 |
$conf["time::buildInDate"]["format"]="Y-m-d(星期D) G:i:s 第W周";
|
|
|
1752 |
#參考資料來源:
|
|
|
1753 |
#http://php.net/manual/en/function.date.php
|
|
|
1754 |
$buildInDate=time::buildInDate($conf["time::buildInDate"]);
|
|
|
1755 |
unset($conf["time::buildInDate"]);
|
| 226 |
liveuser |
1756 |
|
| 3 |
liveuser |
1757 |
#如果取得時間失敗
|
|
|
1758 |
if($buildInDate["status"]=="false"){
|
| 226 |
liveuser |
1759 |
|
| 3 |
liveuser |
1760 |
#設置執行不正常
|
|
|
1761 |
$result["status"]="false";
|
| 226 |
liveuser |
1762 |
|
| 3 |
liveuser |
1763 |
#設置執行錯誤
|
|
|
1764 |
$result["error"]=$buildInDate;
|
| 226 |
liveuser |
1765 |
|
| 3 |
liveuser |
1766 |
#回傳結果
|
|
|
1767 |
return $result;
|
| 226 |
liveuser |
1768 |
|
| 3 |
liveuser |
1769 |
}#if end
|
| 226 |
liveuser |
1770 |
|
| 3 |
liveuser |
1771 |
#取得logFileName
|
|
|
1772 |
$conf["logFileName"]="output-".$buildInDate["content"].".log";
|
| 226 |
liveuser |
1773 |
|
| 3 |
liveuser |
1774 |
#確認檔案名稱是否有重複
|
|
|
1775 |
#涵式說明:檢查多個檔案與資料夾是否存在.
|
|
|
1776 |
#回傳結果:
|
|
|
1777 |
#$result["status"],執行正常與否,"true"代表正常,"false"代表不正常.
|
|
|
1778 |
#$result["error"],錯誤訊息陣列.
|
|
|
1779 |
#$resutl["function"],當前執行的涵式名稱.
|
|
|
1780 |
#$result["allExist"],所有檔案皆存在的識別,"true"代表皆存在,"false"代表沒有全部都存在.
|
|
|
1781 |
#$result["varName"][$i],爲第$i個資料夾或檔案的路徑與名稱。
|
|
|
1782 |
#$result["varNameFullPath"][$i],爲第$i個資料夾或檔案的完整檔案系統路徑與名稱。
|
|
|
1783 |
#$result["varNameWebPath"][$i],為第$i個資料夾或檔案的網址
|
|
|
1784 |
#$result["varExist"][$i],爲第$i個資料夾或檔案是否存在,"true"代表存在,"false"代表不存在。
|
|
|
1785 |
#必填參數:
|
|
|
1786 |
#$conf["fileArray"],陣列字串,要檢查是否存在的檔案有哪些,須爲一維陣列數值。
|
|
|
1787 |
$conf["fileAccess::checkMultiFileExist"]["fileArray"]=array($conf["logFilePath"].$conf["logFileName"]);
|
|
|
1788 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
|
|
|
1789 |
$conf["fileAccess::checkMultiFileExist"]["fileArgu"]=$conf["fileArgu"];
|
|
|
1790 |
#可省略參數
|
|
|
1791 |
#$conf["disableWebSearch"],"字串",是否取消「當檔案找不到時,改用catchWebContent類別的wget函數來檢查檔案是否存在於網路上」的功能,"false"不取消,若要取消該功能請設為"true",若抓到的內容為空字串則會視為檔案不存在,預設為"true".
|
|
|
1792 |
#$conf["disableWebSearch"]="false";
|
|
|
1793 |
#$conf["userDir"],字串,網頁是否置放於家目錄底下,"true"為是,"false"為不是,預設為"true".
|
|
|
1794 |
#$conf["userDir"]="true";
|
|
|
1795 |
#參考資料來源:
|
|
|
1796 |
#http://php.net/manual/en/function.file-exists.php
|
|
|
1797 |
#http://php.net/manual/en/control-structures.foreach.php
|
|
|
1798 |
#備註:
|
|
|
1799 |
#函數file_exists檢查的路徑為檔案系統的路徑
|
|
|
1800 |
$checkMultiFileExist=fileAccess::checkMultiFileExist($conf["fileAccess::checkMultiFileExist"]);
|
|
|
1801 |
unset($conf["fileAccess::checkMultiFileExist"]);
|
| 226 |
liveuser |
1802 |
|
| 3 |
liveuser |
1803 |
#如果檢查檔案存在失敗
|
|
|
1804 |
if($checkMultiFileExist["status"]==="false"){
|
| 226 |
liveuser |
1805 |
|
| 3 |
liveuser |
1806 |
#設置執行不正常
|
|
|
1807 |
$result["status"]="false";
|
| 226 |
liveuser |
1808 |
|
| 3 |
liveuser |
1809 |
#設置執行錯誤
|
|
|
1810 |
$result["error"]=$checkMultiFileExist;
|
| 226 |
liveuser |
1811 |
|
| 3 |
liveuser |
1812 |
#回傳結果
|
|
|
1813 |
return $result;
|
| 226 |
liveuser |
1814 |
|
| 3 |
liveuser |
1815 |
}#if end
|
| 226 |
liveuser |
1816 |
|
| 3 |
liveuser |
1817 |
#如果檔案不存在
|
|
|
1818 |
if($checkMultiFileExist["allExist"]==="false"){
|
| 226 |
liveuser |
1819 |
|
| 3 |
liveuser |
1820 |
#跳出迴圈
|
|
|
1821 |
break;
|
| 226 |
liveuser |
1822 |
|
| 3 |
liveuser |
1823 |
}#else end
|
| 226 |
liveuser |
1824 |
|
| 3 |
liveuser |
1825 |
}#for end
|
| 226 |
liveuser |
1826 |
|
| 3 |
liveuser |
1827 |
#用script指令抓取輸出,並存到 $conf["logFilePath"]."/".$conf["logFileName"].
|
|
|
1828 |
$result["cmd"]="script -q -c \"".$result["cmd"]."\" \"".$conf["logFilePath"]."/".$conf["logFileName"]."\"";
|
| 226 |
liveuser |
1829 |
|
| 3 |
liveuser |
1830 |
}#if end
|
| 226 |
liveuser |
1831 |
|
| 255 |
liveuser |
1832 |
#如果有指定遠端ip
|
|
|
1833 |
if(isset($conf["remoteIp"])){
|
|
|
1834 |
|
|
|
1835 |
#設定ssh指令
|
|
|
1836 |
$ssh="ssh ".$conf["remoteIp"];
|
|
|
1837 |
|
|
|
1838 |
#如果有指定遠端使用者
|
|
|
1839 |
if(isset($conf["remoteUser"])){
|
|
|
1840 |
|
|
|
1841 |
#設定ssh指令
|
|
|
1842 |
$ssh="ssh ".$conf["remoteUser"]."@".$conf["remoteIp"];
|
|
|
1843 |
|
|
|
1844 |
}#if end
|
|
|
1845 |
|
|
|
1846 |
#檢測能否成功遠端的指令
|
|
|
1847 |
$testRemote="echo 'env | grep SSH_CONNECTION' | ".$ssh;
|
|
|
1848 |
|
|
|
1849 |
#debug
|
|
|
1850 |
#var_dump(__LINE__,$testRemote);
|
|
|
1851 |
|
|
|
1852 |
#初始化輸出
|
|
|
1853 |
$output=array();
|
|
|
1854 |
|
|
|
1855 |
#執行檢測遠端是否成功
|
|
|
1856 |
exec($testRemote,$output,$status);
|
|
|
1857 |
|
|
|
1858 |
#如果異常
|
|
|
1859 |
if($status!==0){
|
|
|
1860 |
|
|
|
1861 |
#設置執行不正常
|
|
|
1862 |
$result["status"]="false";
|
|
|
1863 |
|
|
|
1864 |
#設置執行錯誤
|
|
|
1865 |
$result["error"]="can not run cmd on remote shell(".$testRemote.")";
|
|
|
1866 |
|
|
|
1867 |
#回傳結果
|
|
|
1868 |
return $result;
|
|
|
1869 |
|
|
|
1870 |
}#if end
|
|
|
1871 |
|
|
|
1872 |
#如果異常
|
|
|
1873 |
if(count($output)===0){
|
|
|
1874 |
|
|
|
1875 |
#設置執行不正常
|
|
|
1876 |
$result["status"]="false";
|
|
|
1877 |
|
|
|
1878 |
#設置執行錯誤
|
|
|
1879 |
$result["error"]="can not run cmd on remote shell(".$testRemote.")";
|
|
|
1880 |
|
|
|
1881 |
#回傳結果
|
|
|
1882 |
return $result;
|
|
|
1883 |
|
|
|
1884 |
}#if end
|
|
|
1885 |
|
|
|
1886 |
#繼承既有參數
|
|
|
1887 |
$conf["external::callShell"]=$conf;
|
|
|
1888 |
|
|
|
1889 |
#連線到遠端的ssh參數
|
|
|
1890 |
$remote="";
|
|
|
1891 |
|
|
|
1892 |
#如果有 remoteIp 參數
|
|
|
1893 |
if(isset($conf["external::callShell"]["remoteIp"])){
|
|
|
1894 |
|
|
|
1895 |
#設置 remote ip
|
|
|
1896 |
$remote=$conf["external::callShell"]["remoteIp"];
|
|
|
1897 |
|
|
|
1898 |
#移除之
|
|
|
1899 |
unset($conf["external::callShell"]["remoteIp"]);
|
|
|
1900 |
|
|
|
1901 |
#如果有 remoteUser 參數
|
|
|
1902 |
if(isset($conf["external::callShell"]["remoteUser"])){
|
|
|
1903 |
|
|
|
1904 |
#設置 remote user
|
|
|
1905 |
$remote=$conf["external::callShell"]["remoteUser"]."@".$remote;
|
|
|
1906 |
|
|
|
1907 |
#移除之
|
|
|
1908 |
unset($conf["external::callShell"]["remoteUser"]);
|
|
|
1909 |
|
|
|
1910 |
}#if end
|
|
|
1911 |
|
|
|
1912 |
}#if end
|
|
|
1913 |
|
|
|
1914 |
#設置不執行,只要印出指令內容
|
|
|
1915 |
$conf["external::callShell"]["doNotRun"]="true";
|
|
|
1916 |
|
|
|
1917 |
#設置不escape
|
|
|
1918 |
#$conf["external::callShell"]["escapeshellarg"]="false";
|
|
|
1919 |
|
|
|
1920 |
#函式說明:
|
|
|
1921 |
#呼叫shell執行系統命令,並取得回傳的內容.
|
|
|
1922 |
#回傳結果:
|
|
|
1923 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
1924 |
#$result["error"],錯誤訊息陣列.
|
|
|
1925 |
#$result["function"],當前執行的函數名稱.
|
|
|
1926 |
#$result["argu"],使用的參數.
|
|
|
1927 |
#$result["cmd"],執行的指令內容.
|
|
|
1928 |
#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
1929 |
#$result["output"],爲執行完後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
|
|
|
1930 |
#$result["content"],為執行完後的輸出字串.
|
|
|
1931 |
#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
1932 |
#$result["running"],是否還在執行.
|
|
|
1933 |
#$result["pid"],pid.
|
|
|
1934 |
#$result["statusCode"],執行結束後的代碼.
|
|
|
1935 |
#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
|
|
|
1936 |
#$result["noEcaped"],陣列,儲存重新排序過未經過escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
|
|
|
1937 |
#必填參數:
|
|
|
1938 |
#$conf["command"],字串,要執行的指令.
|
|
|
1939 |
#$conf["external::callShell"]["command"]="";
|
|
|
1940 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
|
|
1941 |
#$conf["external::callShell"]["fileArgu"]=__FILE__;
|
|
|
1942 |
#可省略參數:
|
|
|
1943 |
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
|
|
|
1944 |
#$conf["argu"]=array("");
|
|
|
1945 |
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
|
|
|
1946 |
#$conf["arguIsAddr"]=array();
|
|
|
1947 |
#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
|
|
|
1948 |
#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
|
|
|
1949 |
#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
|
|
|
1950 |
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
|
|
|
1951 |
#$conf["enablePrintDescription"]="true";
|
|
|
1952 |
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
|
|
|
1953 |
#$conf["printDescription"]="";
|
|
|
1954 |
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
|
|
|
1955 |
#$conf["escapeshellarg"]="false";
|
|
|
1956 |
#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
|
|
|
1957 |
#$conf["thereIsShellVar"]=array();
|
|
|
1958 |
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
|
|
|
1959 |
#$conf["username"]="";
|
|
|
1960 |
#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
|
|
1961 |
#$conf["password"]="";
|
|
|
1962 |
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
|
|
|
1963 |
#$conf["useScript"]="";
|
|
|
1964 |
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
|
|
|
1965 |
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
|
|
|
1966 |
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
|
|
|
1967 |
#$conf["inBackGround"]="";
|
|
|
1968 |
#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
|
|
|
1969 |
#$conf["getErr"]="false";
|
|
|
1970 |
#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
|
|
|
1971 |
#$conf["doNotRun"]="false";
|
|
|
1972 |
#$conf["remoteIp"],字串,遠端ip或domainName,預設不使用,若存在則會透過ssh指令進行連線,然後執行指令.
|
|
|
1973 |
#$conf["remoteIp"]="";
|
|
|
1974 |
#$conf["remoteUser"],字串,遠端的使用者賬戶,預設不指定,亦即跟為運行該php程式的使用者.
|
|
|
1975 |
#$conf["remoteUser"]="";
|
|
|
1976 |
#參考資料:
|
|
|
1977 |
#exec=>http://php.net/manual/en/function.exec.php
|
|
|
1978 |
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
|
|
|
1979 |
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
|
|
|
1980 |
#備註:
|
|
|
1981 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
|
|
|
1982 |
#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
|
|
|
1983 |
$callShell=external::callShell($conf["external::callShell"]);
|
|
|
1984 |
unset($conf["external::callShell"]);
|
|
|
1985 |
|
|
|
1986 |
#如果執行異常
|
|
|
1987 |
if($callShell["status"]==="false"){
|
|
|
1988 |
|
|
|
1989 |
#設置執行不正常
|
|
|
1990 |
$result["status"]="false";
|
|
|
1991 |
|
|
|
1992 |
#設置執行錯誤
|
|
|
1993 |
$result["error"]=$callShell;
|
|
|
1994 |
|
|
|
1995 |
#回傳結果
|
|
|
1996 |
return $result;
|
|
|
1997 |
|
|
|
1998 |
}#if end
|
|
|
1999 |
|
|
|
2000 |
#指令與參數組成的陣列
|
|
|
2001 |
#var_dump(__LINE__,$callShell);exit;
|
|
|
2002 |
|
|
|
2003 |
#函式說明:
|
|
|
2004 |
#將一維陣列轉換為用特定符號間隔的字串,ex:array("1","2","3") to "a;b;c;".
|
|
|
2005 |
#回傳的結果:
|
|
|
2006 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
2007 |
#$result["function"],當前執行的function名稱
|
|
|
2008 |
#$result["error"],錯誤訊息陣列.
|
|
|
2009 |
#$result["content"],處理好的字串.
|
|
|
2010 |
#$result["argu"],使用的參數.
|
|
|
2011 |
#必填參數:
|
|
|
2012 |
#$conf["inputArray"],字串陣列,要轉成字串的一維陣列.
|
|
|
2013 |
$conf["arrays::arrayToString"]["inputArray"]=$callShell["noEscaped"]["array"];
|
|
|
2014 |
#可省略參數:
|
|
|
2015 |
#$conf["spiltSymbol"],字串,用來區隔字串的符號,預設為;
|
|
|
2016 |
$conf["arrays::arrayToString"]["spiltSymbol"]=" ";
|
|
|
2017 |
#$conf["skipEnd"],字串,結尾是否不要加上符號,預設為"false",要加上符號,"true"代表不要加上符號。
|
|
|
2018 |
$conf["arrays::arrayToString"]["skipEnd"]="true";
|
|
|
2019 |
#$conf["spiltSymbolAtStart"],字串,是否要在開頭加上spiltSymbol,預設為"false",代表不要;反之為“true”.
|
|
|
2020 |
#$conf["spiltSymbolAtStart"]="";
|
|
|
2021 |
#參考資料:
|
|
|
2022 |
#無.
|
|
|
2023 |
#備註:
|
|
|
2024 |
#無.
|
|
|
2025 |
$arrayToString=arrays::arrayToString($conf["arrays::arrayToString"]);
|
|
|
2026 |
unset($conf["arrays::arrayToString"]);
|
|
|
2027 |
|
|
|
2028 |
#如果執行異常
|
|
|
2029 |
if($arrayToString["status"]==="false"){
|
|
|
2030 |
|
|
|
2031 |
#設置執行不正常
|
|
|
2032 |
$result["status"]="false";
|
|
|
2033 |
|
|
|
2034 |
#設置執行錯誤
|
|
|
2035 |
$result["error"]=$arrayToString;
|
|
|
2036 |
|
|
|
2037 |
#回傳結果
|
|
|
2038 |
return $result;
|
|
|
2039 |
|
|
|
2040 |
}#if end
|
|
|
2041 |
|
|
|
2042 |
#初始化要執行的程式
|
|
|
2043 |
$cmd="echo";
|
|
|
2044 |
|
|
|
2045 |
#初始化要在遠端執行的參數
|
|
|
2046 |
$argu=array($arrayToString["content"],"|","ssh",$remote);
|
|
|
2047 |
|
|
|
2048 |
#函式說明:
|
|
|
2049 |
#呼叫shell執行系統命令,並取得回傳的內容.
|
|
|
2050 |
#回傳結果:
|
|
|
2051 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
2052 |
#$result["error"],錯誤訊息陣列.
|
|
|
2053 |
#$result["function"],當前執行的函數名稱.
|
|
|
2054 |
#$result["argu"],使用的參數.
|
|
|
2055 |
#$result["cmd"],執行的指令內容.
|
|
|
2056 |
#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
2057 |
#$result["output"],爲執行完後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
|
|
|
2058 |
#$result["content"],為執行完後的輸出字串.
|
|
|
2059 |
#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
2060 |
#$result["running"],是否還在執行.
|
|
|
2061 |
#$result["pid"],pid.
|
|
|
2062 |
#$result["statusCode"],執行結束後的代碼.
|
|
|
2063 |
#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
|
|
|
2064 |
#$result["noEcaped"],陣列,儲存重新排序過未經過escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
|
|
|
2065 |
#必填參數:
|
|
|
2066 |
#$conf["command"],字串,要執行的指令.
|
|
|
2067 |
$conf["external::callShell"]["command"]=$cmd;
|
|
|
2068 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
|
|
2069 |
$conf["external::callShell"]["fileArgu"]=__FILE__;
|
|
|
2070 |
#可省略參數:
|
|
|
2071 |
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
|
|
|
2072 |
$conf["external::callShell"]["argu"]=$argu;
|
|
|
2073 |
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
|
|
|
2074 |
#$conf["arguIsAddr"]=array();
|
|
|
2075 |
#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
|
|
|
2076 |
#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
|
|
|
2077 |
#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
|
|
|
2078 |
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
|
|
|
2079 |
#$conf["enablePrintDescription"]="true";
|
|
|
2080 |
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
|
|
|
2081 |
#$conf["printDescription"]="";
|
|
|
2082 |
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
|
|
|
2083 |
$conf["external::callShell"]["escapeshellarg"]="true";
|
|
|
2084 |
#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
|
|
|
2085 |
#$conf["thereIsShellVar"]=array();
|
|
|
2086 |
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
|
|
|
2087 |
#$conf["username"]="";
|
|
|
2088 |
#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
|
|
2089 |
#$conf["password"]="";
|
|
|
2090 |
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
|
|
|
2091 |
#$conf["useScript"]="";
|
|
|
2092 |
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
|
|
|
2093 |
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
|
|
|
2094 |
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
|
|
|
2095 |
#$conf["inBackGround"]="";
|
|
|
2096 |
#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
|
|
|
2097 |
#$conf["getErr"]="false";
|
|
|
2098 |
#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
|
|
|
2099 |
#$conf["doNotRun"]="false";
|
|
|
2100 |
#$conf["remoteIp"],字串,遠端ip或domainName,預設不使用,若存在則會透過ssh指令進行連線,然後執行指令.
|
|
|
2101 |
#$conf["remoteIp"]="";
|
|
|
2102 |
#$conf["remoteUser"],字串,遠端的使用者賬戶,預設不指定,亦即跟為運行該php程式的使用者.
|
|
|
2103 |
#$conf["remoteUser"]="";
|
|
|
2104 |
#參考資料:
|
|
|
2105 |
#exec=>http://php.net/manual/en/function.exec.php
|
|
|
2106 |
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
|
|
|
2107 |
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
|
|
|
2108 |
#備註:
|
|
|
2109 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
|
|
|
2110 |
#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
|
|
|
2111 |
return external::callShell($conf["external::callShell"]);
|
|
|
2112 |
unset($conf["external::callShell"]);
|
|
|
2113 |
|
|
|
2114 |
}#if end
|
|
|
2115 |
|
| 3 |
liveuser |
2116 |
#如果 $conf["inBackGround"] 為 "true"
|
|
|
2117 |
if($conf["inBackGround"]==="true"){
|
| 226 |
liveuser |
2118 |
|
| 3 |
liveuser |
2119 |
#函式說明:
|
|
|
2120 |
#呼叫shell執行系統命令,並取得回傳的內容與pid.
|
|
|
2121 |
#回傳結果:
|
|
|
2122 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
2123 |
#$result["error"],錯誤訊息陣列.
|
|
|
2124 |
#$result["function"],當前執行的函數名稱.
|
|
|
2125 |
#$result["cmd"],執行的指令內容.
|
|
|
2126 |
#$result["fullCmd"],執行的完整指令
|
|
|
2127 |
#$result["content"],爲執行完二元碼後的輸出陣列.
|
|
|
2128 |
#$result["tmpFileOutput"],儲存輸出的暫村檔案名稱.
|
|
|
2129 |
#$result["running"],程式是否還在執行.
|
|
|
2130 |
#$result["pid"],pid.
|
|
|
2131 |
#$result["statusCode"],執行結果狀態碼.
|
|
|
2132 |
#必填參數:
|
|
|
2133 |
#$conf["command"],字串,要執行的指令.
|
|
|
2134 |
$conf["external::callShellV2"]["command"]=$result["cmd"];
|
|
|
2135 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
|
|
2136 |
$conf["external::callShellV2"]["fileArgu"]=$conf["fileArgu"];
|
|
|
2137 |
#可省略參數:
|
|
|
2138 |
#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
|
|
|
2139 |
$conf["external::callShellV2"]["doNotRun"]=$conf["doNotRun"];
|
|
|
2140 |
#備註:
|
|
|
2141 |
#不是所有指令都能用apache的身份執行,目前已知java,javac,ping指令無法執行,透過su使用root身份可能會被selinux阻擋.
|
|
|
2142 |
#參考資料:
|
|
|
2143 |
#http://php.net/manual/en/function.exec.php
|
|
|
2144 |
$callShellV2=external::callShellInBg($conf["external::callShellV2"]);
|
|
|
2145 |
unset($conf["external::callShellV2"]);
|
| 226 |
liveuser |
2146 |
|
| 3 |
liveuser |
2147 |
#如果執行失敗
|
|
|
2148 |
if($callShellV2["status"]==="false"){
|
| 226 |
liveuser |
2149 |
|
| 3 |
liveuser |
2150 |
#設置執行不正常
|
|
|
2151 |
$result["status"]="false";
|
| 226 |
liveuser |
2152 |
|
| 3 |
liveuser |
2153 |
#設置執行錯誤
|
|
|
2154 |
$result["error"]=$callShellV2;
|
| 226 |
liveuser |
2155 |
|
| 3 |
liveuser |
2156 |
#回傳結果
|
|
|
2157 |
return $result;
|
| 226 |
liveuser |
2158 |
|
| 3 |
liveuser |
2159 |
}#if end
|
| 226 |
liveuser |
2160 |
|
| 3 |
liveuser |
2161 |
#執行到這邊代表執行正常
|
|
|
2162 |
$result["status"]="true";
|
|
|
2163 |
|
|
|
2164 |
#取得當下的輸出
|
|
|
2165 |
$result["content"]=$callShellV2["content"];
|
| 226 |
liveuser |
2166 |
|
| 3 |
liveuser |
2167 |
#取得集記錄輸出的暫存檔案位置
|
|
|
2168 |
$result["tmpFileOutput"]=$callShellV2["tmpFileOutput"];
|
| 226 |
liveuser |
2169 |
|
| 3 |
liveuser |
2170 |
#取得執行的完整指令
|
|
|
2171 |
$result["fullCmd"]=$callShellV2["fullCmd"];
|
|
|
2172 |
|
|
|
2173 |
#取得執行的指令
|
|
|
2174 |
$result["cmd"]=$callShellV2["cmd"];
|
|
|
2175 |
|
|
|
2176 |
#取得程序的pid
|
|
|
2177 |
$result["pid"]=$callShellV2["pid"];
|
|
|
2178 |
|
|
|
2179 |
#取得是否還在執行
|
|
|
2180 |
$result["running"]=$callShellV2["running"];
|
| 226 |
liveuser |
2181 |
|
| 3 |
liveuser |
2182 |
#取得執行狀態代碼
|
|
|
2183 |
$result["statusCode"]=$callShellV2["statusCode"];
|
|
|
2184 |
|
|
|
2185 |
#回傳結果
|
|
|
2186 |
return $result;
|
| 226 |
liveuser |
2187 |
|
| 3 |
liveuser |
2188 |
}#if end
|
| 226 |
liveuser |
2189 |
|
| 3 |
liveuser |
2190 |
#反之沒有要在背景中執行
|
|
|
2191 |
else{
|
| 226 |
liveuser |
2192 |
|
| 3 |
liveuser |
2193 |
#如果要將錯誤輸出導到標準輸出
|
|
|
2194 |
if($conf["getErr"]==="true"){
|
| 226 |
liveuser |
2195 |
|
| 3 |
liveuser |
2196 |
#指令加上 "2>&1"
|
|
|
2197 |
$result["cmd"]=$result["cmd"]." 2>&1";
|
| 226 |
liveuser |
2198 |
|
| 3 |
liveuser |
2199 |
}#if end
|
| 226 |
liveuser |
2200 |
|
| 3 |
liveuser |
2201 |
#如果沒有要執行指令
|
|
|
2202 |
if($conf["doNotRun"]==="true"){
|
| 226 |
liveuser |
2203 |
|
| 3 |
liveuser |
2204 |
#初始化執行結果狀態碼為0
|
|
|
2205 |
$result["statusCode"]=0;
|
| 226 |
liveuser |
2206 |
|
| 3 |
liveuser |
2207 |
#初始化輸出為空
|
|
|
2208 |
$resultArray=array();
|
| 226 |
liveuser |
2209 |
|
| 3 |
liveuser |
2210 |
#初始化執行結果狀態碼為0
|
|
|
2211 |
$execStatus=0;
|
| 226 |
liveuser |
2212 |
|
| 3 |
liveuser |
2213 |
}#if end
|
| 226 |
liveuser |
2214 |
|
| 3 |
liveuser |
2215 |
#反之要執行指令
|
|
|
2216 |
else{
|
| 226 |
liveuser |
2217 |
|
| 255 |
liveuser |
2218 |
#debug
|
|
|
2219 |
#var_dump(__LINE__,$result["cmd"]);
|
| 226 |
liveuser |
2220 |
|
| 3 |
liveuser |
2221 |
#執行腳本程式,將輸出結果儲存在 $resultArray 陣列裏面
|
| 60 |
liveuser |
2222 |
exec($result["cmd"],$resultArray,$execStatus);
|
| 226 |
liveuser |
2223 |
|
| 3 |
liveuser |
2224 |
#取得執行結果狀態碼
|
|
|
2225 |
$result["statusCode"]=$execStatus;
|
| 226 |
liveuser |
2226 |
|
| 3 |
liveuser |
2227 |
}#else end
|
| 226 |
liveuser |
2228 |
|
| 3 |
liveuser |
2229 |
}#else end
|
| 226 |
liveuser |
2230 |
|
| 3 |
liveuser |
2231 |
#如果 $conf["useScript"] 為 "true"
|
| 60 |
liveuser |
2232 |
if($conf["useScript"]=="true"){
|
| 226 |
liveuser |
2233 |
|
| 3 |
liveuser |
2234 |
#讀取暫存檔案的內容
|
|
|
2235 |
#函式說明:
|
|
|
2236 |
#依據行號分隔抓取檔案的內容,結果會回傳一個陣列
|
|
|
2237 |
#回傳的變數說明:
|
|
|
2238 |
#$result["status"],執行是否成功,"true"代表成功;"fasle"代表失敗.
|
|
|
2239 |
#$result["error"],錯誤訊息提示.
|
|
|
2240 |
#$result["warning"],警告訊息.
|
|
|
2241 |
#$result["function"],當前執行的函數名稱.
|
|
|
2242 |
#$result["fileContent"],爲檔案的內容陣列.
|
|
|
2243 |
#$result["lineCount"],爲檔案內容總共的行數.
|
|
|
2244 |
#$result["fullContent"],為檔案的完整內容.
|
|
|
2245 |
#必填參數::
|
|
|
2246 |
#$conf["filePositionAndName"],字串,爲檔案的位置以及名稱.
|
|
|
2247 |
$conf["fileAccess::getFileContent"]["filePositionAndName"]=$conf["logFilePath"]."/".$conf["logFileName"];
|
|
|
2248 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
|
|
|
2249 |
$conf["fileAccess::getFileContent"]["fileArgu"]=$conf["fileArgu"];
|
|
|
2250 |
#參考資料:
|
|
|
2251 |
#file(),取得檔案內容的行數.
|
|
|
2252 |
#file=>http:#php.net/manual/en/function.file.php
|
|
|
2253 |
#rtrim(),剔除透過file()取得每行內容結尾的換行符號.
|
|
|
2254 |
#filesize=>http://php.net/manual/en/function.filesize.php
|
|
|
2255 |
$getFileContent=fileAccess::getFileContent($conf["fileAccess::getFileContent"]);
|
|
|
2256 |
unset($conf["fileAccess::getFileContent"]);
|
| 226 |
liveuser |
2257 |
|
| 3 |
liveuser |
2258 |
#如果取得檔案內容失敗
|
|
|
2259 |
if($getFileContent["status"]=="false"){
|
| 226 |
liveuser |
2260 |
|
| 3 |
liveuser |
2261 |
#設置執行不正常
|
|
|
2262 |
$result["status"]="false";
|
| 226 |
liveuser |
2263 |
|
| 3 |
liveuser |
2264 |
#設置執行錯誤
|
|
|
2265 |
$result["error"]=$getFileContent;
|
| 226 |
liveuser |
2266 |
|
| 3 |
liveuser |
2267 |
#回傳結果
|
|
|
2268 |
return $result;
|
| 226 |
liveuser |
2269 |
|
| 3 |
liveuser |
2270 |
}#if end
|
| 226 |
liveuser |
2271 |
|
| 3 |
liveuser |
2272 |
#移除暫存檔案
|
|
|
2273 |
#涵式說明:
|
|
|
2274 |
#移除檔案
|
|
|
2275 |
#回傳的結果:
|
|
|
2276 |
#$result["status"],"true"代表移除成功,"false"代表移除失敗.
|
|
|
2277 |
#$result["error"],錯誤訊息陣列
|
|
|
2278 |
#$result["warning"],警告訊息陣列
|
|
|
2279 |
#$result["function"],當前執行的函數名稱
|
|
|
2280 |
#必填參數::
|
|
|
2281 |
#$conf["fileAddress"],字串,要移除檔案的位置.
|
|
|
2282 |
$conf["fileAccess::delFile"]["fileAddress"]=$conf["logFilePath"]."/".$conf["logFileName"];
|
|
|
2283 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.
|
|
|
2284 |
$conf["fileAccess::delFile"]["fileArgu"]=$conf["fileArgu"];
|
|
|
2285 |
#可省略參數:
|
|
|
2286 |
#$conf["commentsArray"],字串陣列,提示的文字描述,$conf["commentsArray"][$i]代表第($+1)行的描述.
|
|
|
2287 |
#$conf["commentsArray"]=array("");
|
|
|
2288 |
$delFile=fileAccess::delFile($conf["fileAccess::delFile"]);
|
|
|
2289 |
unset($conf["fileAccess::delFile"]);
|
| 226 |
liveuser |
2290 |
|
| 3 |
liveuser |
2291 |
#如果移除檔案失敗
|
|
|
2292 |
if($delFile["status"]=="false"){
|
| 226 |
liveuser |
2293 |
|
| 3 |
liveuser |
2294 |
#設置執行不正常
|
|
|
2295 |
$result["status"]="false";
|
| 226 |
liveuser |
2296 |
|
| 3 |
liveuser |
2297 |
#設置執行錯誤
|
|
|
2298 |
$result["error"]=$getFileContent;
|
| 226 |
liveuser |
2299 |
|
| 3 |
liveuser |
2300 |
#回傳結果
|
|
|
2301 |
return $result;
|
| 226 |
liveuser |
2302 |
|
| 3 |
liveuser |
2303 |
}#if end
|
| 226 |
liveuser |
2304 |
|
| 3 |
liveuser |
2305 |
#取得執行外部程式的輸出
|
|
|
2306 |
$result["output"]=$getFileContent["fileContent"];
|
| 226 |
liveuser |
2307 |
|
| 3 |
liveuser |
2308 |
}#if end
|
| 226 |
liveuser |
2309 |
|
| 3 |
liveuser |
2310 |
#反之
|
|
|
2311 |
else{
|
| 226 |
liveuser |
2312 |
|
| 3 |
liveuser |
2313 |
#取得執行外部程式的輸出
|
|
|
2314 |
$result["output"]=$resultArray;
|
| 226 |
liveuser |
2315 |
|
| 3 |
liveuser |
2316 |
}#else end
|
|
|
2317 |
|
| 255 |
liveuser |
2318 |
#初始化暫存 指令跟參數的陣列
|
|
|
2319 |
$cmdAndArguArray=array();
|
| 226 |
liveuser |
2320 |
|
| 255 |
liveuser |
2321 |
#取得第一個元素
|
|
|
2322 |
$cmdAndArguArray[]=&$result["escaped"]["cmd"];
|
| 226 |
liveuser |
2323 |
|
| 255 |
liveuser |
2324 |
#如果有 argu
|
|
|
2325 |
if(isset($result["escaped"]["argu"])){
|
| 226 |
liveuser |
2326 |
|
| 255 |
liveuser |
2327 |
#針對每個參數
|
|
|
2328 |
foreach($result["escaped"]["argu"] as $arguKey=>$arguVal){
|
| 226 |
liveuser |
2329 |
|
| 255 |
liveuser |
2330 |
#取得該參數
|
|
|
2331 |
$cmdAndArguArray[]=&$result["escaped"]["argu"][$arguKey];
|
| 226 |
liveuser |
2332 |
|
| 255 |
liveuser |
2333 |
}#foreach end
|
| 226 |
liveuser |
2334 |
|
| 3 |
liveuser |
2335 |
}#if end
|
| 226 |
liveuser |
2336 |
|
| 255 |
liveuser |
2337 |
#設置 cmd & argu 合併為一維陣列後的結果
|
|
|
2338 |
$result["escaped"]["array"]=&$cmdAndArguArray;
|
| 226 |
liveuser |
2339 |
|
| 255 |
liveuser |
2340 |
#從 $result["noEscaped"]["cmd"] 跟 $result["noEscaped"]["argu"] 產生 $result["noEscaped"]["array"]
|
|
|
2341 |
#函式說明:
|
|
|
2342 |
#將多個一維陣列串聯起來,key從0開始排序.
|
|
|
2343 |
#回傳的結果:
|
|
|
2344 |
#$result["status"],"true"表執行正常,"false"代表執行不正常.
|
|
|
2345 |
#$result["error"],錯誤訊息陣列.
|
|
|
2346 |
#$result["function"],當前執行的函數.
|
|
|
2347 |
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
|
|
|
2348 |
#$result["content"],合併好的一維陣列.
|
|
|
2349 |
#必填參數
|
|
|
2350 |
#$conf["inputArray"],陣列,要合併的一維陣列變數,例如:=array($array1,$array2);
|
|
|
2351 |
$conf["arrays::mergeArray"]["inputArray"]=array(array($result["noEscaped"]["cmd"]),$result["noEscaped"]["argu"]);
|
|
|
2352 |
#可省略參數:
|
|
|
2353 |
#$conf["allowRepeat"],字串,預設為"true",允許重複的結果;若為"false"則不會出現重複的元素內容.
|
|
|
2354 |
#$conf["allowRepeat"]="true";
|
|
|
2355 |
#$conf["looseDiff"],字串,預設為"false",代表要嚴謹判斷為有相異,例如陣列中元素的key順序不同(整數)就代表有相異;反之為"true",例如陣列中元素的key順序不同(非整數),但value有相同,則視為無相異.
|
|
|
2356 |
#$conf["looseDiff"]="false";
|
|
|
2357 |
#參考資料:
|
|
|
2358 |
#無.
|
|
|
2359 |
#備註:
|
|
|
2360 |
#無.
|
|
|
2361 |
$mergeArray=arrays::mergeArray($conf["arrays::mergeArray"]);
|
|
|
2362 |
unset($conf["arrays::mergeArray"]);
|
| 3 |
liveuser |
2363 |
|
| 255 |
liveuser |
2364 |
#如果異常
|
|
|
2365 |
if($mergeArray["status"]=="false"){
|
| 226 |
liveuser |
2366 |
|
| 255 |
liveuser |
2367 |
#設置執行不正常
|
|
|
2368 |
$result["status"]="false";
|
| 226 |
liveuser |
2369 |
|
| 255 |
liveuser |
2370 |
#設置執行錯誤
|
|
|
2371 |
$result["error"]=$mergeArray;
|
| 226 |
liveuser |
2372 |
|
| 255 |
liveuser |
2373 |
#回傳結果
|
|
|
2374 |
return $result;
|
| 226 |
liveuser |
2375 |
|
| 255 |
liveuser |
2376 |
}#if end
|
| 226 |
liveuser |
2377 |
|
| 255 |
liveuser |
2378 |
#取得未escape的指令與參數陣列
|
|
|
2379 |
$result["noEscaped"]["array"]=$mergeArray["content"];
|
| 226 |
liveuser |
2380 |
|
| 3 |
liveuser |
2381 |
#若執行外部式回傳的狀態不為0
|
|
|
2382 |
if($execStatus!==0){
|
| 226 |
liveuser |
2383 |
|
| 3 |
liveuser |
2384 |
#代表執行外部程式不正常.
|
| 226 |
liveuser |
2385 |
|
| 3 |
liveuser |
2386 |
#設置錯誤識別
|
|
|
2387 |
$result["status"]="false";
|
| 226 |
liveuser |
2388 |
|
| 3 |
liveuser |
2389 |
#設錯誤訊息
|
|
|
2390 |
$result["error"][]=$execStatus;
|
|
|
2391 |
$result["error"][]=$resultArray;
|
| 226 |
liveuser |
2392 |
|
| 3 |
liveuser |
2393 |
#設置錯誤訊息
|
|
|
2394 |
$result["error"][]="執行 「".$result["cmd"]."」 命令意外結束!";
|
|
|
2395 |
$result["error"][]="有時可能是因為沒有權限導致的錯誤";
|
|
|
2396 |
$result["error"][]="有時可能是因為指令語法不正確導致的錯誤";
|
| 226 |
liveuser |
2397 |
|
| 3 |
liveuser |
2398 |
#回傳結果
|
|
|
2399 |
return $result;
|
| 226 |
liveuser |
2400 |
|
| 3 |
liveuser |
2401 |
}#if end
|
| 226 |
liveuser |
2402 |
|
| 60 |
liveuser |
2403 |
#初始化完整標準輸出
|
|
|
2404 |
$result["content"]="";
|
| 226 |
liveuser |
2405 |
|
| 60 |
liveuser |
2406 |
#如果有 output
|
|
|
2407 |
if(!empty($result["output"])){
|
| 226 |
liveuser |
2408 |
|
| 60 |
liveuser |
2409 |
#函式說明:
|
|
|
2410 |
#將一維陣列轉換為用特定符號間隔的字串,ex:array("1","2","3") to "a;b;c;".
|
|
|
2411 |
#回傳的結果:
|
|
|
2412 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
2413 |
#$result["function"],當前執行的function名稱
|
|
|
2414 |
#$result["error"],錯誤訊息陣列.
|
|
|
2415 |
#$result["content"],處理好的字串.
|
|
|
2416 |
#$result["argu"],使用的參數.
|
|
|
2417 |
#必填參數:
|
|
|
2418 |
#$conf["inputArray"],字串陣列,要轉成字串的一維陣列.
|
|
|
2419 |
$conf["arrays::arrayToString"]["inputArray"]=$result["output"];
|
|
|
2420 |
#可省略參數:
|
|
|
2421 |
#$conf["spiltSymbol"],字串,用來區隔字串的符號,預設為;
|
|
|
2422 |
$conf["arrays::arrayToString"]["spiltSymbol"]=PHP_EOL;
|
|
|
2423 |
#$conf["skipEnd"],字串,結尾是否不要加上符號,預設為"false",要加上符號,"true"代表不要加上符號。
|
|
|
2424 |
$conf["arrays::arrayToString"]["skipEnd"]="true";
|
|
|
2425 |
#$conf["spiltSymbolAtStart"],字串,是否要在開頭加上spiltSymbol,預設為"false",代表不要;反之為“true”.
|
|
|
2426 |
#$conf["spiltSymbolAtStart"]="";
|
|
|
2427 |
#參考資料:
|
|
|
2428 |
#無.
|
|
|
2429 |
#備註:
|
|
|
2430 |
#無.
|
|
|
2431 |
$arrayToString=arrays::arrayToString($conf["arrays::arrayToString"]);
|
|
|
2432 |
unset($conf["arrays::arrayToString"]);
|
| 226 |
liveuser |
2433 |
|
| 60 |
liveuser |
2434 |
#如果執行失敗
|
|
|
2435 |
if($arrayToString["status"]=="false"){
|
| 226 |
liveuser |
2436 |
|
| 60 |
liveuser |
2437 |
#設置執行不正常
|
|
|
2438 |
$result["status"]="false";
|
| 226 |
liveuser |
2439 |
|
| 60 |
liveuser |
2440 |
#設置執行錯誤
|
|
|
2441 |
$result["error"]=$arrayToString;
|
| 226 |
liveuser |
2442 |
|
| 60 |
liveuser |
2443 |
#回傳結果
|
|
|
2444 |
return $result;
|
| 226 |
liveuser |
2445 |
|
| 60 |
liveuser |
2446 |
}#if end
|
| 226 |
liveuser |
2447 |
|
| 60 |
liveuser |
2448 |
#儲存完整標準輸出的內容
|
|
|
2449 |
$result["content"]=$arrayToString["content"];
|
| 226 |
liveuser |
2450 |
|
| 60 |
liveuser |
2451 |
}#if end
|
| 226 |
liveuser |
2452 |
|
| 3 |
liveuser |
2453 |
#執行到這邊代表執行正常
|
|
|
2454 |
$result["status"]="true";
|
| 226 |
liveuser |
2455 |
|
| 3 |
liveuser |
2456 |
#回傳結果
|
|
|
2457 |
return $result;
|
| 226 |
liveuser |
2458 |
|
| 3 |
liveuser |
2459 |
}#function callShell end
|
| 226 |
liveuser |
2460 |
|
| 3 |
liveuser |
2461 |
/*
|
|
|
2462 |
#函式說明:
|
|
|
2463 |
#呼叫shell執行系統命令,並取得回傳儲存輸出內容的檔案與pid.
|
|
|
2464 |
#回傳結果:
|
|
|
2465 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
2466 |
#$result["error"],錯誤訊息陣列.
|
|
|
2467 |
#$result["function"],當前執行的函數名稱.
|
|
|
2468 |
#$result["cmd"],執行的指令內容.
|
|
|
2469 |
#$result["fullCmd"],執行的完整指令
|
|
|
2470 |
#$result["content"],爲執行完二元碼後的輸出陣列.
|
|
|
2471 |
#$result["tmpFileOutput"],儲存輸出的暫村檔案名稱.
|
|
|
2472 |
#$result["runing"],程式是否還在執行.
|
|
|
2473 |
#$result["pid"],pid.
|
|
|
2474 |
#$result["statusCode"],執行結果狀態碼.
|
|
|
2475 |
#必填參數:
|
|
|
2476 |
#$conf["command"],字串,要執行的指令.
|
|
|
2477 |
$conf["command"]="";
|
|
|
2478 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
| 226 |
liveuser |
2479 |
$conf["fileArgu"]=__FILE__;
|
| 3 |
liveuser |
2480 |
#可省略參數:
|
|
|
2481 |
#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
|
|
|
2482 |
#$conf["doNotRun"]="false";
|
|
|
2483 |
#$conf["leftLogFile"],字串,預設為"false"代表要將 log 檔案移除;"true"代表要保留log檔案.
|
|
|
2484 |
#$conf["leftLogFile"]="true";
|
|
|
2485 |
#備註:
|
|
|
2486 |
#不是所有指令都能用apache的身份執行,目前已知java,javac,ping指令無法執行,透過su使用root身份可能會被selinux阻擋.
|
|
|
2487 |
#如果輸出的內容包含換行符號,將會被過濾掉.
|
|
|
2488 |
#nohup $cmd > $logFilePath 2>&1 & echo $! 會將指令的標準輸入寫入檔案 $logFilePath 並忽略錯誤輸出,將指令在背景執行,僅印出執行緒id.
|
|
|
2489 |
#$cmd > /dev/null 2>&1 &; 會將$cmd放在背景執行,且不會卡住.
|
|
|
2490 |
#參考資料:
|
|
|
2491 |
#http://php.net/manual/en/function.exec.php
|
| 226 |
liveuser |
2492 |
*/
|
| 3 |
liveuser |
2493 |
public static function callShellInBg(&$conf){
|
| 226 |
liveuser |
2494 |
|
| 3 |
liveuser |
2495 |
#初始化要回傳的結果
|
|
|
2496 |
$result=array();
|
|
|
2497 |
|
|
|
2498 |
#取得當前執行的函數名稱
|
|
|
2499 |
$result["function"]=__FUNCTION__;
|
|
|
2500 |
|
|
|
2501 |
#如果沒有參數
|
|
|
2502 |
if(func_num_args()==0){
|
| 226 |
liveuser |
2503 |
|
| 3 |
liveuser |
2504 |
#設置執行失敗
|
|
|
2505 |
$result["status"]="false";
|
| 226 |
liveuser |
2506 |
|
| 3 |
liveuser |
2507 |
#設置執行錯誤訊息
|
|
|
2508 |
$result["error"]="函數".$result["function"]."需要參數";
|
| 226 |
liveuser |
2509 |
|
| 3 |
liveuser |
2510 |
#回傳結果
|
|
|
2511 |
return $result;
|
| 226 |
liveuser |
2512 |
|
| 3 |
liveuser |
2513 |
}#if end
|
| 226 |
liveuser |
2514 |
|
| 3 |
liveuser |
2515 |
#取得參數
|
|
|
2516 |
$result["argu"]=$conf;
|
|
|
2517 |
|
|
|
2518 |
#如果 $conf 不為陣列
|
|
|
2519 |
if(gettype($conf)!=="array"){
|
| 226 |
liveuser |
2520 |
|
| 3 |
liveuser |
2521 |
#設置執行失敗
|
|
|
2522 |
$result["status"]="false";
|
| 226 |
liveuser |
2523 |
|
| 3 |
liveuser |
2524 |
#設置執行錯誤訊息
|
|
|
2525 |
$result["error"][]="\$conf變數須為陣列形態";
|
| 226 |
liveuser |
2526 |
|
| 3 |
liveuser |
2527 |
#如果傳入的參數為 null
|
|
|
2528 |
if($conf===null){
|
| 226 |
liveuser |
2529 |
|
| 3 |
liveuser |
2530 |
#設置執行錯誤訊息
|
|
|
2531 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
| 226 |
liveuser |
2532 |
|
| 3 |
liveuser |
2533 |
}#if end
|
|
|
2534 |
|
|
|
2535 |
#回傳結果
|
|
|
2536 |
return $result;
|
| 226 |
liveuser |
2537 |
|
| 3 |
liveuser |
2538 |
}#if end
|
| 226 |
liveuser |
2539 |
|
| 3 |
liveuser |
2540 |
#檢查參數
|
|
|
2541 |
#函式說明:
|
|
|
2542 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
2543 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
2544 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
2545 |
#$result["function"],當前執行的函式名稱.
|
|
|
2546 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
2547 |
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
2548 |
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
2549 |
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
2550 |
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
|
|
|
2551 |
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
|
|
|
2552 |
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
|
|
|
2553 |
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
|
|
|
2554 |
#必填寫的參數:
|
|
|
2555 |
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
2556 |
$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
|
|
|
2557 |
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
2558 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("command","fileArgu");
|
|
|
2559 |
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
|
|
|
2560 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string");
|
|
|
2561 |
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
2562 |
$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
2563 |
#可以省略的參數:
|
|
|
2564 |
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
|
|
|
2565 |
#$conf["canBeEmptyString"]="false";
|
|
|
2566 |
#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
|
|
|
2567 |
#$conf["canNotBeEmpty"]=array();
|
|
|
2568 |
#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
|
|
|
2569 |
#$conf["canBeEmpty"]=array();
|
|
|
2570 |
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
|
|
|
2571 |
#$conf["skipableVariableCanNotBeEmpty"]=array();
|
|
|
2572 |
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
|
|
2573 |
$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("doNotRun","leftLogFile");
|
| 226 |
liveuser |
2574 |
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
2575 |
$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string");
|
|
|
2576 |
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
|
|
2577 |
$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("false","false");
|
|
|
2578 |
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
2579 |
#$conf["arrayCountEqualCheck"][]=array();
|
|
|
2580 |
#參考資料來源:
|
|
|
2581 |
#array_keys=>http://php.net/manual/en/function.array-keys.php
|
|
|
2582 |
#建議:
|
|
|
2583 |
#增加可省略參數全部不能為空字串或空陣列的參數功能.
|
|
|
2584 |
$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
|
|
|
2585 |
unset($conf["variableCheck::checkArguments"]);
|
| 226 |
liveuser |
2586 |
|
| 3 |
liveuser |
2587 |
#如果檢查參數失敗
|
|
|
2588 |
if($checkArguments["status"]==="false"){
|
| 226 |
liveuser |
2589 |
|
| 3 |
liveuser |
2590 |
#設置執行失敗
|
|
|
2591 |
$result["status"]="false";
|
| 226 |
liveuser |
2592 |
|
| 3 |
liveuser |
2593 |
#設置執行錯誤訊息
|
|
|
2594 |
$result["error"]=$checkArguments;
|
| 226 |
liveuser |
2595 |
|
| 3 |
liveuser |
2596 |
#回傳結果
|
|
|
2597 |
return $result;
|
| 226 |
liveuser |
2598 |
|
| 3 |
liveuser |
2599 |
}#if end
|
| 226 |
liveuser |
2600 |
|
| 3 |
liveuser |
2601 |
#如果參數檢查不過
|
|
|
2602 |
if($checkArguments["passed"]==="false"){
|
| 226 |
liveuser |
2603 |
|
| 3 |
liveuser |
2604 |
#設置執行失敗
|
|
|
2605 |
$result["status"]="false";
|
| 226 |
liveuser |
2606 |
|
| 3 |
liveuser |
2607 |
#設置執行錯誤訊息
|
|
|
2608 |
$result["error"]=$checkArguments;
|
| 226 |
liveuser |
2609 |
|
| 3 |
liveuser |
2610 |
#回傳結果
|
|
|
2611 |
return $result;
|
| 226 |
liveuser |
2612 |
|
| 3 |
liveuser |
2613 |
}#if end
|
| 226 |
liveuser |
2614 |
|
| 3 |
liveuser |
2615 |
#產生暫存檔案
|
|
|
2616 |
#函數說明:
|
| 226 |
liveuser |
2617 |
#建立暫存目錄與回傳暫存檔案名稱路徑
|
| 3 |
liveuser |
2618 |
#回傳結果:
|
|
|
2619 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
2620 |
#$result["error"],錯誤訊息.
|
|
|
2621 |
#$result["function"],當前執行的函數名稱.
|
| 226 |
liveuser |
2622 |
#$result["content"],暫存檔案的路徑與名稱.
|
| 3 |
liveuser |
2623 |
#必填參數:
|
|
|
2624 |
#無
|
|
|
2625 |
#可省略參數:
|
|
|
2626 |
#$conf["tempDir"],字串,暫存目錄的名稱,預設為.fileAccess/createTempFile
|
|
|
2627 |
#$conf["tempDIr"]="";
|
|
|
2628 |
#$conf["fileArgu"],字串,__FILE__的內容,預設為當前檔案的位置.
|
|
|
2629 |
$conf["fileAccess::createTempFile"]["fileArgu"]=$conf["fileArgu"];
|
|
|
2630 |
#$conf["createPath"],字串,是否僅要建立目錄,"true"代表要,"false"代表不要,預設為"true".
|
|
|
2631 |
#$conf["createPath"]="";
|
|
|
2632 |
#$conf["createFile"],字串,是否要在暫存路徑建立好後建立暫存檔案,"true"代表要,"false"代表不用,預設為"true".
|
|
|
2633 |
#$conf["createFile"]="";
|
|
|
2634 |
$createTempFile=fileAccess::createTempFile($conf["fileAccess::createTempFile"]);
|
|
|
2635 |
unset($conf["fileAccess::createTempFile"]);
|
| 226 |
liveuser |
2636 |
|
| 3 |
liveuser |
2637 |
#如果建立暫存檔案失敗
|
|
|
2638 |
if($createTempFile["status"]==="false"){
|
| 226 |
liveuser |
2639 |
|
| 3 |
liveuser |
2640 |
#設置執行失敗
|
|
|
2641 |
$result["status"]="false";
|
| 226 |
liveuser |
2642 |
|
| 3 |
liveuser |
2643 |
#設置執行錯誤訊息
|
|
|
2644 |
$result["error"]=$createTempFile;
|
| 226 |
liveuser |
2645 |
|
| 3 |
liveuser |
2646 |
#回傳結果
|
|
|
2647 |
return $result;
|
| 226 |
liveuser |
2648 |
|
| 3 |
liveuser |
2649 |
}#if end
|
| 226 |
liveuser |
2650 |
|
| 3 |
liveuser |
2651 |
#取得暫存檔案的路徑與名稱
|
|
|
2652 |
$logFilePath=$createTempFile["content"];
|
| 226 |
liveuser |
2653 |
|
| 3 |
liveuser |
2654 |
#執行 commad 並輸出結果到 $logFilePath, 忽略錯誤輸出, 印出pid
|
|
|
2655 |
$command = 'nohup '.$conf["command"].' > '.$logFilePath.' 2>&1 & echo $!';
|
| 226 |
liveuser |
2656 |
|
|
|
2657 |
#$cmd="nohup";
|
| 3 |
liveuser |
2658 |
#$paramsArray=array($conf["command"],">",$logFilePath,"2>&1","&","echo","$!");
|
| 226 |
liveuser |
2659 |
|
| 3 |
liveuser |
2660 |
#如果不用執行指令
|
|
|
2661 |
if($conf["doNotRun"]==="true"){
|
| 226 |
liveuser |
2662 |
|
| 3 |
liveuser |
2663 |
#預設執行結果代碼為0
|
|
|
2664 |
$status=0;
|
| 226 |
liveuser |
2665 |
|
| 3 |
liveuser |
2666 |
#預設pid為0
|
|
|
2667 |
$op=array("0");
|
| 226 |
liveuser |
2668 |
|
| 3 |
liveuser |
2669 |
}#if end
|
| 226 |
liveuser |
2670 |
|
| 3 |
liveuser |
2671 |
#反之
|
|
|
2672 |
else{
|
| 226 |
liveuser |
2673 |
|
| 3 |
liveuser |
2674 |
#執行command, 輸出放到$op
|
|
|
2675 |
exec($command ,$op ,$status);
|
| 226 |
liveuser |
2676 |
|
| 3 |
liveuser |
2677 |
}#else end
|
| 226 |
liveuser |
2678 |
|
| 3 |
liveuser |
2679 |
#取得執行結果狀態碼
|
|
|
2680 |
$result["statusCode"]=$status;
|
| 226 |
liveuser |
2681 |
|
| 3 |
liveuser |
2682 |
#設置執行的完整指令
|
|
|
2683 |
$result["fullCmd"]=$command;
|
| 226 |
liveuser |
2684 |
|
| 3 |
liveuser |
2685 |
#設置執行的指令
|
|
|
2686 |
$result["cmd"]=$conf["command"];
|
| 226 |
liveuser |
2687 |
|
| 3 |
liveuser |
2688 |
#取得pid
|
|
|
2689 |
$result["pid"]=$op[0];
|
| 226 |
liveuser |
2690 |
|
| 3 |
liveuser |
2691 |
#函式說明:
|
|
|
2692 |
#依據行號分隔抓取檔案的內容,結果會回傳一個陣列
|
|
|
2693 |
#回傳的變數說明:
|
|
|
2694 |
#$result["status"],執行是否成功,"true"代表成功;"fasle"代表失敗.
|
|
|
2695 |
#$result["error"],錯誤訊息提示.
|
|
|
2696 |
#$result["warning"],警告訊息.
|
|
|
2697 |
#$result["function"],當前執行的函數名稱.
|
|
|
2698 |
#$result["fileContent"],爲檔案的內容陣列.
|
|
|
2699 |
#$result["lineCount"],爲檔案內容總共的行數.
|
|
|
2700 |
#$result["fullContent"],為檔案的完整內容.
|
|
|
2701 |
#必填參數::
|
|
|
2702 |
#$conf["filePositionAndName"],字串,爲檔案的位置以及名稱.
|
|
|
2703 |
$conf["fileAccess::getFileContent"]["filePositionAndName"]=$logFilePath;
|
|
|
2704 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
|
|
|
2705 |
$conf["fileAccess::getFileContent"]["fileArgu"]=$conf["fileArgu"];
|
|
|
2706 |
#可省略參數:
|
|
|
2707 |
#$conf["web"],是要取得網路上的檔案則為"true";反之則為"false".
|
|
|
2708 |
$conf["fileAccess::getFileContent"]["web"]="false";
|
|
|
2709 |
#參考資料:
|
|
|
2710 |
#file(),取得檔案內容的行數.
|
|
|
2711 |
#file=>http:#php.net/manual/en/function.file.php
|
|
|
2712 |
#rtrim(),剔除透過file()取得每行內容結尾的換行符號.
|
|
|
2713 |
#filesize=>http://php.net/manual/en/function.filesize.php
|
|
|
2714 |
$getFileContent=fileAccess::getFileContent($conf["fileAccess::getFileContent"]);
|
| 226 |
liveuser |
2715 |
unset($conf["fileAccess::getFileContent"]);
|
|
|
2716 |
|
| 3 |
liveuser |
2717 |
#如果讀取檔案失敗
|
|
|
2718 |
if($getFileContent["status"]==="false"){
|
| 226 |
liveuser |
2719 |
|
| 3 |
liveuser |
2720 |
#設置執行失敗
|
|
|
2721 |
$result["status"]="false";
|
| 226 |
liveuser |
2722 |
|
| 3 |
liveuser |
2723 |
#設置執行錯誤訊息
|
|
|
2724 |
$result["error"]=$getFileContent;
|
| 226 |
liveuser |
2725 |
|
| 3 |
liveuser |
2726 |
#回傳結果
|
|
|
2727 |
return $result;
|
| 226 |
liveuser |
2728 |
|
| 3 |
liveuser |
2729 |
}#if end
|
| 226 |
liveuser |
2730 |
|
| 3 |
liveuser |
2731 |
#取得輸出內容
|
|
|
2732 |
$result["content"]=$getFileContent["fileContent"];
|
| 226 |
liveuser |
2733 |
|
| 3 |
liveuser |
2734 |
#取得暫存檔案路徑與名稱
|
|
|
2735 |
$result["tmpFileOutput"]=$logFilePath;
|
| 226 |
liveuser |
2736 |
|
| 3 |
liveuser |
2737 |
#如果沒有要保留 log 檔案
|
|
|
2738 |
if($conf["leftLogFile"]==="false"){
|
| 226 |
liveuser |
2739 |
|
| 3 |
liveuser |
2740 |
#函式說明:
|
|
|
2741 |
#移除檔案
|
|
|
2742 |
#回傳結果:
|
|
|
2743 |
#$result["status"],"true"代表移除成功,"false"代表移除失敗.
|
|
|
2744 |
#$result["error"],錯誤訊息陣列.
|
|
|
2745 |
#$result["warning"],警告訊息陣列.
|
|
|
2746 |
#$result["function"],當前執行的函數名稱.
|
|
|
2747 |
#$result["argu"],當前函式使用的參數.
|
|
|
2748 |
#必填參數:
|
|
|
2749 |
#$conf["fileAddress"],字串,要移除檔案的位置.
|
|
|
2750 |
$conf["fileAccess::delFile"]["fileAddress"]=$logFilePath;
|
|
|
2751 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.
|
|
|
2752 |
$conf["fileAccess::delFile"]["fileArgu"]=$conf["fileArgu"];
|
|
|
2753 |
#可省略參數:
|
|
|
2754 |
#$conf["commentsArray"],字串陣列,提示的文字描述,$conf["commentsArray"][$i]代表第($+1)行的描述.
|
|
|
2755 |
#$conf["commentsArray"]=array("");
|
|
|
2756 |
#$conf["allowDelSymlink"],字串,預設為"false",不移除軟連結;"true"代表要移除軟連結.
|
|
|
2757 |
#$conf["allowDelSymlink"]="true";
|
|
|
2758 |
#$conf["allowDelFolder"],字串,預設為"false",不移除目錄;"true"代表要移除目錄.
|
|
|
2759 |
#$conf["allowDelFolder"]="true";
|
|
|
2760 |
#參考資料:
|
|
|
2761 |
#無.
|
|
|
2762 |
#備註:
|
|
|
2763 |
#無.
|
|
|
2764 |
$delFile=fileAccess::delFile($conf["fileAccess::delFile"]);
|
|
|
2765 |
unset($conf["fileAccess::delFile"]);
|
| 226 |
liveuser |
2766 |
|
| 3 |
liveuser |
2767 |
#如果執行失敗
|
|
|
2768 |
if($delFile["status"]==="false"){
|
| 226 |
liveuser |
2769 |
|
| 3 |
liveuser |
2770 |
#設置執行失敗
|
|
|
2771 |
$result["status"]="false";
|
| 226 |
liveuser |
2772 |
|
| 3 |
liveuser |
2773 |
#設置執行錯誤訊息
|
|
|
2774 |
$result["error"]=$delFile;
|
| 226 |
liveuser |
2775 |
|
| 3 |
liveuser |
2776 |
#回傳結果
|
|
|
2777 |
return $result;
|
| 226 |
liveuser |
2778 |
|
| 3 |
liveuser |
2779 |
}#if end
|
| 226 |
liveuser |
2780 |
|
| 3 |
liveuser |
2781 |
}#if end
|
| 226 |
liveuser |
2782 |
|
| 3 |
liveuser |
2783 |
#函式說明:
|
| 226 |
liveuser |
2784 |
#檢查程序是否還在執行
|
| 3 |
liveuser |
2785 |
#回傳結果
|
|
|
2786 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
2787 |
#$result["error"],錯誤訊息陣列.
|
| 226 |
liveuser |
2788 |
#$result["function"],當前執行的函數名稱.
|
| 3 |
liveuser |
2789 |
#$result["exist"],程序是否存在,"true"代表存在,"false"代表不存在.
|
|
|
2790 |
#必填參數:
|
|
|
2791 |
#$conf["pid"],字串,程序的id.
|
|
|
2792 |
$conf["external::processStatus"]["pid"]=$result["pid"];
|
|
|
2793 |
#$conf["cmd"],字串,程序的指令.
|
|
|
2794 |
$conf["external::processStatus"]["cmd"]=$result["cmd"];
|
|
|
2795 |
#$conf["fileArgu"],字串,__FILE__的內容,__FILE__的內容.
|
|
|
2796 |
$conf["external::processStatus"]["fileArgu"]=$conf["fileArgu"];
|
|
|
2797 |
$processStatus=external::processStatus($conf["external::processStatus"]);
|
|
|
2798 |
unset($conf["external::processStatus"]);
|
| 226 |
liveuser |
2799 |
|
| 3 |
liveuser |
2800 |
#如果查詢pid是否存在失敗
|
|
|
2801 |
if($processStatus["status"]==="false"){
|
| 226 |
liveuser |
2802 |
|
| 3 |
liveuser |
2803 |
#設置執行失敗
|
|
|
2804 |
$result["status"]="false";
|
| 226 |
liveuser |
2805 |
|
| 3 |
liveuser |
2806 |
#設置執行錯誤訊息
|
|
|
2807 |
$result["error"]=$processStatus;
|
| 226 |
liveuser |
2808 |
|
| 3 |
liveuser |
2809 |
#回傳結果
|
|
|
2810 |
return $result;
|
| 226 |
liveuser |
2811 |
|
| 3 |
liveuser |
2812 |
}#if end
|
| 226 |
liveuser |
2813 |
|
| 3 |
liveuser |
2814 |
#取得程序是否還在執行
|
|
|
2815 |
$result["running"]=$processStatus["exist"];
|
| 226 |
liveuser |
2816 |
|
| 3 |
liveuser |
2817 |
#設置執行正常
|
|
|
2818 |
$result["status"]="true";
|
| 226 |
liveuser |
2819 |
|
| 3 |
liveuser |
2820 |
#回傳結果
|
|
|
2821 |
return $result;
|
| 226 |
liveuser |
2822 |
|
| 3 |
liveuser |
2823 |
}#function callShellInBg end
|
| 226 |
liveuser |
2824 |
|
| 3 |
liveuser |
2825 |
/*
|
|
|
2826 |
#函式說明:
|
|
|
2827 |
#呼叫shell_exec執行系統命令,並取得回傳儲存輸出內容的檔案與pid.
|
|
|
2828 |
#回傳結果:
|
|
|
2829 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
2830 |
#$result["error"],錯誤訊息陣列.
|
|
|
2831 |
#$result["function"],當前執行的函數名稱.
|
|
|
2832 |
#$result["cmd"],執行的指令內容.
|
|
|
2833 |
#$result["fullCmd"],執行的完整指令
|
|
|
2834 |
#$result["content"],爲執行完二元碼後的輸出結果,會儲存成字串,包含換行符號.
|
|
|
2835 |
#$result["runing"],程式是否還在執行.
|
|
|
2836 |
#$result["pid"],pid.
|
|
|
2837 |
#必填參數:
|
|
|
2838 |
#$conf["command"],字串,要執行的指令.
|
|
|
2839 |
$conf["command"]="";
|
|
|
2840 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
| 226 |
liveuser |
2841 |
$conf["fileArgu"]=__FILE__;
|
| 3 |
liveuser |
2842 |
#可省略參數:
|
|
|
2843 |
#無.
|
|
|
2844 |
#參考資料:
|
|
|
2845 |
#http://php.net/manual/en/function.exec.php
|
|
|
2846 |
#備註:
|
|
|
2847 |
#不是所有指令都能用apache的身份執行,目前已知java,javac,ping指令無法執行,透過su使用root身份可能會被selinux阻擋.
|
| 226 |
liveuser |
2848 |
*/
|
| 3 |
liveuser |
2849 |
public static function callShellExecInBg(&$conf){
|
| 226 |
liveuser |
2850 |
|
| 3 |
liveuser |
2851 |
#初始化要回傳的結果
|
|
|
2852 |
$result=array();
|
|
|
2853 |
|
|
|
2854 |
#取得當前執行的函數名稱
|
|
|
2855 |
$result["function"]=__FUNCTION__;
|
|
|
2856 |
|
|
|
2857 |
#如果沒有參數
|
|
|
2858 |
if(func_num_args()==0){
|
| 226 |
liveuser |
2859 |
|
| 3 |
liveuser |
2860 |
#設置執行失敗
|
|
|
2861 |
$result["status"]="false";
|
| 226 |
liveuser |
2862 |
|
| 3 |
liveuser |
2863 |
#設置執行錯誤訊息
|
|
|
2864 |
$result["error"]="函數".$result["function"]."需要參數";
|
| 226 |
liveuser |
2865 |
|
| 3 |
liveuser |
2866 |
#回傳結果
|
|
|
2867 |
return $result;
|
| 226 |
liveuser |
2868 |
|
| 3 |
liveuser |
2869 |
}#if end
|
| 226 |
liveuser |
2870 |
|
| 3 |
liveuser |
2871 |
#取得參數
|
|
|
2872 |
$result["argu"]=$conf;
|
|
|
2873 |
|
|
|
2874 |
#如果 $conf 不為陣列
|
|
|
2875 |
if(gettype($conf)!=="array"){
|
| 226 |
liveuser |
2876 |
|
| 3 |
liveuser |
2877 |
#設置執行失敗
|
|
|
2878 |
$result["status"]="false";
|
| 226 |
liveuser |
2879 |
|
| 3 |
liveuser |
2880 |
#設置執行錯誤訊息
|
|
|
2881 |
$result["error"][]="\$conf變數須為陣列形態";
|
| 226 |
liveuser |
2882 |
|
| 3 |
liveuser |
2883 |
#如果傳入的參數為 null
|
|
|
2884 |
if($conf===null){
|
| 226 |
liveuser |
2885 |
|
| 3 |
liveuser |
2886 |
#設置執行錯誤訊息
|
|
|
2887 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
| 226 |
liveuser |
2888 |
|
| 3 |
liveuser |
2889 |
}#if end
|
|
|
2890 |
|
|
|
2891 |
#回傳結果
|
|
|
2892 |
return $result;
|
| 226 |
liveuser |
2893 |
|
| 3 |
liveuser |
2894 |
}#if end
|
| 226 |
liveuser |
2895 |
|
| 3 |
liveuser |
2896 |
#檢查參數
|
|
|
2897 |
#函式說明:
|
|
|
2898 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
2899 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
2900 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
2901 |
#$result["function"],當前執行的函式名稱.
|
|
|
2902 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
2903 |
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
2904 |
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
2905 |
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
2906 |
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
|
|
|
2907 |
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
|
|
|
2908 |
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
|
|
|
2909 |
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
|
|
|
2910 |
#必填寫的參數:
|
|
|
2911 |
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
2912 |
$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
|
|
|
2913 |
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
2914 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("command","fileArgu");
|
|
|
2915 |
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
|
|
|
2916 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string");
|
|
|
2917 |
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
2918 |
$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
2919 |
#可以省略的參數:
|
|
|
2920 |
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
|
|
|
2921 |
#$conf["canBeEmptyString"]="false";
|
|
|
2922 |
#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
|
|
|
2923 |
#$conf["canNotBeEmpty"]=array();
|
|
|
2924 |
#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
|
|
|
2925 |
#$conf["canBeEmpty"]=array();
|
|
|
2926 |
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
|
|
|
2927 |
#$conf["skipableVariableCanNotBeEmpty"]=array();
|
|
|
2928 |
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
|
|
2929 |
#$conf["skipableVariableName"]=array();
|
| 226 |
liveuser |
2930 |
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
2931 |
#$conf["skipableVariableType"]=array();
|
|
|
2932 |
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
|
|
2933 |
#$conf["skipableVarDefaultValue"]=array("");
|
|
|
2934 |
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
2935 |
#$conf["arrayCountEqualCheck"][]=array();
|
|
|
2936 |
#參考資料來源:
|
|
|
2937 |
#array_keys=>http://php.net/manual/en/function.array-keys.php
|
|
|
2938 |
#建議:
|
|
|
2939 |
#增加可省略參數全部不能為空字串或空陣列的參數功能.
|
|
|
2940 |
$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
|
|
|
2941 |
unset($conf["variableCheck::checkArguments"]);
|
| 226 |
liveuser |
2942 |
|
| 3 |
liveuser |
2943 |
#如果檢查參數失敗
|
|
|
2944 |
if($checkArguments["status"]==="false"){
|
| 226 |
liveuser |
2945 |
|
| 3 |
liveuser |
2946 |
#設置執行失敗
|
|
|
2947 |
$result["status"]="false";
|
| 226 |
liveuser |
2948 |
|
| 3 |
liveuser |
2949 |
#設置執行錯誤訊息
|
|
|
2950 |
$result["error"]=$checkArguments;
|
| 226 |
liveuser |
2951 |
|
| 3 |
liveuser |
2952 |
#回傳結果
|
|
|
2953 |
return $result;
|
| 226 |
liveuser |
2954 |
|
| 3 |
liveuser |
2955 |
}#if end
|
| 226 |
liveuser |
2956 |
|
| 3 |
liveuser |
2957 |
#如果參數檢查不過
|
|
|
2958 |
if($checkArguments["passed"]==="false"){
|
| 226 |
liveuser |
2959 |
|
| 3 |
liveuser |
2960 |
#設置執行失敗
|
|
|
2961 |
$result["status"]="false";
|
| 226 |
liveuser |
2962 |
|
| 3 |
liveuser |
2963 |
#設置執行錯誤訊息
|
|
|
2964 |
$result["error"]=$checkArguments;
|
| 226 |
liveuser |
2965 |
|
| 3 |
liveuser |
2966 |
#回傳結果
|
|
|
2967 |
return $result;
|
| 226 |
liveuser |
2968 |
|
| 3 |
liveuser |
2969 |
}#if end
|
| 226 |
liveuser |
2970 |
|
| 3 |
liveuser |
2971 |
#產生暫存檔案
|
|
|
2972 |
#函數說明:
|
| 226 |
liveuser |
2973 |
#建立暫存目錄與回傳暫存檔案名稱路徑
|
| 3 |
liveuser |
2974 |
#回傳結果:
|
|
|
2975 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
2976 |
#$result["error"],錯誤訊息.
|
|
|
2977 |
#$result["function"],當前執行的函數名稱.
|
| 226 |
liveuser |
2978 |
#$result["content"],暫存檔案的路徑與名稱.
|
| 3 |
liveuser |
2979 |
#必填參數:
|
|
|
2980 |
#無
|
|
|
2981 |
#可省略參數:
|
|
|
2982 |
#$conf["tempDir"],字串,暫存目錄的名稱,預設為.fileAccess/createTempFile
|
|
|
2983 |
#$conf["tempDIr"]="";
|
|
|
2984 |
#$conf["fileArgu"],字串,__FILE__的內容,預設為當前檔案的位置.
|
|
|
2985 |
$conf["fileAccess::createTempFile"]["fileArgu"]=$conf["fileArgu"];
|
|
|
2986 |
#$conf["createPath"],字串,是否僅要建立目錄,"true"代表要,"false"代表不要,預設為"true".
|
|
|
2987 |
#$conf["createPath"]="";
|
|
|
2988 |
#$conf["createFile"],字串,是否要在暫存路徑建立好後建立暫存檔案,"true"代表要,"false"代表不用,預設為"true".
|
|
|
2989 |
#$conf["createFile"]="";
|
|
|
2990 |
$createTempFile=fileAccess::createTempFile($conf["fileAccess::createTempFile"]);
|
|
|
2991 |
unset($conf["fileAccess::createTempFile"]);
|
| 226 |
liveuser |
2992 |
|
| 3 |
liveuser |
2993 |
#如果建立暫存檔案失敗
|
|
|
2994 |
if($createTempFile["status"]==="false"){
|
| 226 |
liveuser |
2995 |
|
| 3 |
liveuser |
2996 |
#設置執行失敗
|
|
|
2997 |
$result["status"]="false";
|
| 226 |
liveuser |
2998 |
|
| 3 |
liveuser |
2999 |
#設置執行錯誤訊息
|
|
|
3000 |
$result["error"]=$createTempFile;
|
| 226 |
liveuser |
3001 |
|
| 3 |
liveuser |
3002 |
#回傳結果
|
|
|
3003 |
return $result;
|
| 226 |
liveuser |
3004 |
|
| 3 |
liveuser |
3005 |
}#if end
|
| 226 |
liveuser |
3006 |
|
| 3 |
liveuser |
3007 |
#取得暫存檔案的路徑與名稱
|
|
|
3008 |
$logFilePath=$createTempFile["content"];
|
| 226 |
liveuser |
3009 |
|
| 3 |
liveuser |
3010 |
#執行 commad 並輸出結果到 "/tmp/externel.Process", 忽略錯誤輸出, 印出pid
|
|
|
3011 |
$command = 'nohup '.$conf["command"].' > '.$logFilePath.' 2>&1 & echo $!';
|
| 226 |
liveuser |
3012 |
|
| 3 |
liveuser |
3013 |
#執行command, 輸出放到$op
|
|
|
3014 |
$op=shell_exec($command);
|
| 226 |
liveuser |
3015 |
|
| 3 |
liveuser |
3016 |
#設置執行的完整指令
|
|
|
3017 |
$result["fullCmd"]=$command;
|
| 226 |
liveuser |
3018 |
|
| 3 |
liveuser |
3019 |
#設置執行的指令
|
|
|
3020 |
$result["cmd"]=$conf["command"];
|
| 226 |
liveuser |
3021 |
|
| 3 |
liveuser |
3022 |
#取得pid
|
|
|
3023 |
$result["pid"]=$op;
|
| 226 |
liveuser |
3024 |
|
| 3 |
liveuser |
3025 |
#函式說明:
|
|
|
3026 |
#依據行號分隔抓取檔案的內容,結果會回傳一個陣列
|
|
|
3027 |
#回傳的變數說明:
|
|
|
3028 |
#$result["status"],執行是否成功,"true"代表成功;"fasle"代表失敗.
|
|
|
3029 |
#$result["error"],錯誤訊息提示.
|
|
|
3030 |
#$result["warning"],警告訊息.
|
|
|
3031 |
#$result["function"],當前執行的函數名稱.
|
|
|
3032 |
#$result["fileContent"],爲檔案的內容陣列.
|
|
|
3033 |
#$result["lineCount"],爲檔案內容總共的行數.
|
|
|
3034 |
#$result["fullContent"],為檔案的完整內容.
|
|
|
3035 |
#必填參數::
|
|
|
3036 |
#$conf["filePositionAndName"],字串,爲檔案的位置以及名稱.
|
|
|
3037 |
$conf["fileAccess::getFileContent"]["filePositionAndName"]=$logFilePath;
|
|
|
3038 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
|
|
|
3039 |
$conf["fileAccess::getFileContent"]["fileArgu"]=$conf["fileArgu"];
|
|
|
3040 |
#參考資料:
|
|
|
3041 |
#file(),取得檔案內容的行數.
|
|
|
3042 |
#file=>http:#php.net/manual/en/function.file.php
|
|
|
3043 |
#rtrim(),剔除透過file()取得每行內容結尾的換行符號.
|
|
|
3044 |
#filesize=>http://php.net/manual/en/function.filesize.php
|
|
|
3045 |
$getFileContent=fileAccess::getFileContent($conf["fileAccess::getFileContent"]);
|
| 226 |
liveuser |
3046 |
unset($conf["fileAccess::getFileContent"]);
|
|
|
3047 |
|
| 3 |
liveuser |
3048 |
#如果讀取檔案失敗
|
|
|
3049 |
if($getFileContent["status"]==="false"){
|
| 226 |
liveuser |
3050 |
|
| 3 |
liveuser |
3051 |
#設置執行失敗
|
|
|
3052 |
$result["status"]="false";
|
| 226 |
liveuser |
3053 |
|
| 3 |
liveuser |
3054 |
#設置執行錯誤訊息
|
|
|
3055 |
$result["error"]=$getFileContent;
|
| 226 |
liveuser |
3056 |
|
| 3 |
liveuser |
3057 |
#回傳結果
|
|
|
3058 |
return $result;
|
| 226 |
liveuser |
3059 |
|
| 3 |
liveuser |
3060 |
}#if end
|
| 226 |
liveuser |
3061 |
|
| 3 |
liveuser |
3062 |
#取得輸出內容
|
|
|
3063 |
$result["content"]=$getFileContent["fileContent"];
|
| 226 |
liveuser |
3064 |
|
| 3 |
liveuser |
3065 |
#取得暫存檔案路徑與名稱
|
|
|
3066 |
$result["tmpFileOutput"]=$logFilePath;
|
| 226 |
liveuser |
3067 |
|
| 3 |
liveuser |
3068 |
#函式說明:
|
| 226 |
liveuser |
3069 |
#檢查程序是否還在執行
|
| 3 |
liveuser |
3070 |
#回傳結果
|
|
|
3071 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
3072 |
#$result["error"],錯誤訊息陣列.
|
| 226 |
liveuser |
3073 |
#$result["function"],當前執行的函數名稱.
|
| 3 |
liveuser |
3074 |
#$result["exist"],程序是否存在,"true"代表存在,"false"代表不存在.
|
|
|
3075 |
#必填參數:
|
|
|
3076 |
#$conf["pid"],字串,程序的id.
|
|
|
3077 |
$conf["external::processStatus"]["pid"]=$result["pid"];
|
|
|
3078 |
#$conf["cmd"],字串,程序的指令.
|
|
|
3079 |
$conf["external::processStatus"]["cmd"]=$result["cmd"];
|
|
|
3080 |
#$conf["fileArgu"],字串,__FILE__的內容,__FILE__的內容.
|
|
|
3081 |
$conf["external::processStatus"]["fileArgu"]=$conf["fileArgu"];
|
|
|
3082 |
$processStatus=external::processStatus($conf["external::processStatus"]);
|
|
|
3083 |
unset($conf["external::processStatus"]);
|
| 226 |
liveuser |
3084 |
|
| 3 |
liveuser |
3085 |
#如果查詢pid是否存在失敗
|
|
|
3086 |
if($processStatus["status"]==="false"){
|
| 226 |
liveuser |
3087 |
|
| 3 |
liveuser |
3088 |
#設置執行失敗
|
|
|
3089 |
$result["status"]="false";
|
| 226 |
liveuser |
3090 |
|
| 3 |
liveuser |
3091 |
#設置執行錯誤訊息
|
|
|
3092 |
$result["error"]=$processStatus;
|
| 226 |
liveuser |
3093 |
|
| 3 |
liveuser |
3094 |
#回傳結果
|
|
|
3095 |
return $result;
|
| 226 |
liveuser |
3096 |
|
| 3 |
liveuser |
3097 |
}#if end
|
| 226 |
liveuser |
3098 |
|
| 3 |
liveuser |
3099 |
#取得程序是否還在執行
|
|
|
3100 |
$result["runing"]=$processStatus["exist"];
|
| 226 |
liveuser |
3101 |
|
| 3 |
liveuser |
3102 |
#設置執行正常
|
|
|
3103 |
$result["status"]="true";
|
| 226 |
liveuser |
3104 |
|
| 3 |
liveuser |
3105 |
#回傳結果
|
|
|
3106 |
return $result;
|
| 226 |
liveuser |
3107 |
|
| 66 |
liveuser |
3108 |
}#function callShellExecInBg end
|
| 226 |
liveuser |
3109 |
|
| 3 |
liveuser |
3110 |
/*
|
|
|
3111 |
#函式說明:
|
| 226 |
liveuser |
3112 |
#檢查程序是否還在執行
|
| 3 |
liveuser |
3113 |
#回傳結果:
|
|
|
3114 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
3115 |
#$result["error"],錯誤訊息陣列.
|
| 226 |
liveuser |
3116 |
#$result["function"],當前執行的函數名稱.
|
| 3 |
liveuser |
3117 |
#$result["exist"],程序是否存在,"true"代表存在,"false"代表不存在.
|
|
|
3118 |
#必填參數:
|
|
|
3119 |
#$conf["pid"],字串,程序的id.
|
|
|
3120 |
$conf["pid"]="";
|
|
|
3121 |
#$conf["cmd"],字串,程序的指令.
|
|
|
3122 |
$conf["cmd"]=""
|
|
|
3123 |
#$conf["fileArgu"],字串,__FILE__的內容,__FILE__的內容.
|
|
|
3124 |
$conf["fileArgu"]=__FILE__;
|
|
|
3125 |
#可省略參數:
|
|
|
3126 |
#無.
|
|
|
3127 |
#參考資料:
|
|
|
3128 |
#無.
|
|
|
3129 |
#備註:
|
|
|
3130 |
#無.
|
| 226 |
liveuser |
3131 |
*/
|
| 3 |
liveuser |
3132 |
public static function processStatus(&$conf){
|
| 226 |
liveuser |
3133 |
|
| 3 |
liveuser |
3134 |
#初始化要回傳的結果
|
|
|
3135 |
$result=array();
|
|
|
3136 |
|
|
|
3137 |
#取得當前執行的函數名稱
|
|
|
3138 |
$result["function"]=__FUNCTION__;
|
|
|
3139 |
|
|
|
3140 |
#如果沒有參數
|
|
|
3141 |
if(func_num_args()==0){
|
| 226 |
liveuser |
3142 |
|
| 3 |
liveuser |
3143 |
#設置執行失敗
|
|
|
3144 |
$result["status"]="false";
|
| 226 |
liveuser |
3145 |
|
| 3 |
liveuser |
3146 |
#設置執行錯誤訊息
|
|
|
3147 |
$result["error"]="函數".$result["function"]."需要參數";
|
| 226 |
liveuser |
3148 |
|
| 3 |
liveuser |
3149 |
#回傳結果
|
|
|
3150 |
return $result;
|
| 226 |
liveuser |
3151 |
|
| 3 |
liveuser |
3152 |
}#if end
|
| 226 |
liveuser |
3153 |
|
| 3 |
liveuser |
3154 |
#取得參數
|
|
|
3155 |
$result["argu"]=$conf;
|
|
|
3156 |
|
|
|
3157 |
#如果 $conf 不為陣列
|
|
|
3158 |
if(gettype($conf)!=="array"){
|
| 226 |
liveuser |
3159 |
|
| 3 |
liveuser |
3160 |
#設置執行失敗
|
|
|
3161 |
$result["status"]="false";
|
| 226 |
liveuser |
3162 |
|
| 3 |
liveuser |
3163 |
#設置執行錯誤訊息
|
|
|
3164 |
$result["error"][]="\$conf變數須為陣列形態";
|
| 226 |
liveuser |
3165 |
|
| 3 |
liveuser |
3166 |
#如果傳入的參數為 null
|
|
|
3167 |
if($conf===null){
|
| 226 |
liveuser |
3168 |
|
| 3 |
liveuser |
3169 |
#設置執行錯誤訊息
|
|
|
3170 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
| 226 |
liveuser |
3171 |
|
| 3 |
liveuser |
3172 |
}#if end
|
|
|
3173 |
|
|
|
3174 |
#回傳結果
|
|
|
3175 |
return $result;
|
| 226 |
liveuser |
3176 |
|
| 3 |
liveuser |
3177 |
}#if end
|
| 226 |
liveuser |
3178 |
|
| 3 |
liveuser |
3179 |
#檢查參數
|
|
|
3180 |
#函式說明:
|
|
|
3181 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
3182 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
3183 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
3184 |
#$result["function"],當前執行的函式名稱.
|
|
|
3185 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
3186 |
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
3187 |
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
3188 |
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
3189 |
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
|
|
|
3190 |
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
|
|
|
3191 |
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
|
|
|
3192 |
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
|
|
|
3193 |
#必填寫的參數:
|
|
|
3194 |
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
3195 |
$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
|
|
|
3196 |
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
3197 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("pid","cmd","fileArgu");
|
|
|
3198 |
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
|
|
|
3199 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string","string");
|
|
|
3200 |
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
3201 |
$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
3202 |
#可以省略的參數:
|
|
|
3203 |
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
|
|
|
3204 |
#$conf["canBeEmptyString"]="false";
|
|
|
3205 |
#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
|
|
|
3206 |
#$conf["canNotBeEmpty"]=array();
|
|
|
3207 |
#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
|
|
|
3208 |
#$conf["canBeEmpty"]=array();
|
|
|
3209 |
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
|
|
|
3210 |
#$conf["skipableVariableCanNotBeEmpty"]=array();
|
|
|
3211 |
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
|
|
3212 |
#$conf["skipableVariableName"]=array();
|
| 226 |
liveuser |
3213 |
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
3214 |
#$conf["skipableVariableType"]=array();
|
|
|
3215 |
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
|
|
3216 |
#$conf["skipableVarDefaultValue"]=array("");
|
|
|
3217 |
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
3218 |
#$conf["arrayCountEqualCheck"][]=array();
|
|
|
3219 |
#參考資料來源:
|
|
|
3220 |
#array_keys=>http://php.net/manual/en/function.array-keys.php
|
|
|
3221 |
#建議:
|
|
|
3222 |
#增加可省略參數全部不能為空字串或空陣列的參數功能.
|
|
|
3223 |
$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
|
|
|
3224 |
unset($conf["variableCheck::checkArguments"]);
|
| 226 |
liveuser |
3225 |
|
| 3 |
liveuser |
3226 |
#如果檢查參數失敗
|
|
|
3227 |
if($checkArguments["status"]==="false"){
|
| 226 |
liveuser |
3228 |
|
| 3 |
liveuser |
3229 |
#設置執行失敗
|
|
|
3230 |
$result["status"]="false";
|
| 226 |
liveuser |
3231 |
|
| 3 |
liveuser |
3232 |
#設置執行錯誤訊息
|
|
|
3233 |
$result["error"]=$checkArguments;
|
| 226 |
liveuser |
3234 |
|
| 3 |
liveuser |
3235 |
#回傳結果
|
|
|
3236 |
return $result;
|
| 226 |
liveuser |
3237 |
|
| 3 |
liveuser |
3238 |
}#if end
|
| 226 |
liveuser |
3239 |
|
| 3 |
liveuser |
3240 |
#如果參數檢查不過
|
|
|
3241 |
if($checkArguments["passed"]==="false"){
|
| 226 |
liveuser |
3242 |
|
| 3 |
liveuser |
3243 |
#設置執行失敗
|
|
|
3244 |
$result["status"]="false";
|
| 226 |
liveuser |
3245 |
|
| 3 |
liveuser |
3246 |
#設置執行錯誤訊息
|
|
|
3247 |
$result["error"]=$checkArguments;
|
| 226 |
liveuser |
3248 |
|
| 3 |
liveuser |
3249 |
#回傳結果
|
|
|
3250 |
return $result;
|
| 226 |
liveuser |
3251 |
|
| 3 |
liveuser |
3252 |
}#if end
|
| 226 |
liveuser |
3253 |
|
| 3 |
liveuser |
3254 |
#產生搜尋process的指令
|
| 226 |
liveuser |
3255 |
#執行檢查程序是否依然存在
|
| 3 |
liveuser |
3256 |
#函式說明:
|
|
|
3257 |
#呼叫shell執行系統命令,並取得回傳的內容.
|
|
|
3258 |
#回傳結果:
|
|
|
3259 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
3260 |
#$result["error"],錯誤訊息陣列.
|
|
|
3261 |
#$result["function"],當前執行的函數名稱.
|
|
|
3262 |
#$result["argu"],使用的參數.
|
|
|
3263 |
#$result["cmd"],執行的指令內容.
|
|
|
3264 |
#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
3265 |
#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
|
|
|
3266 |
#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
3267 |
#$result["running"],是否還在執行.
|
|
|
3268 |
#$result["pid"],pid.
|
|
|
3269 |
#$result["statusCode"],執行結束後的代碼.
|
|
|
3270 |
#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu").
|
|
|
3271 |
#必填參數:
|
|
|
3272 |
#$conf["command"],字串,要執行的指令與.
|
|
|
3273 |
$conf["external::callShell"]["command"]="ps";
|
|
|
3274 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
| 226 |
liveuser |
3275 |
$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];
|
| 3 |
liveuser |
3276 |
#可省略參數:
|
|
|
3277 |
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
|
|
|
3278 |
$conf["external::callShell"]["argu"]=array("-aux","|","cat","|","grep",$conf["cmd"],"|","grep",$conf["pid"]);
|
|
|
3279 |
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
|
|
|
3280 |
#$conf["arguIsAddr"]=array();
|
|
|
3281 |
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
|
|
|
3282 |
#$conf["enablePrintDescription"]="true";
|
|
|
3283 |
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容.
|
|
|
3284 |
#$conf["printDescription"]="";
|
|
|
3285 |
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".
|
|
|
3286 |
$conf["external::callShell"]["escapeshellarg"]="true";
|
|
|
3287 |
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
|
|
|
3288 |
#$conf["username"]="";
|
|
|
3289 |
#$conf["password"],字串,與$conf["username"]搭配的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
|
|
3290 |
#$conf["password"]="";
|
|
|
3291 |
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
|
|
|
3292 |
#$conf["useScript"]="";
|
|
|
3293 |
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
|
|
|
3294 |
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
|
|
|
3295 |
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
|
|
|
3296 |
#$conf["inBackGround"]="";
|
|
|
3297 |
#備註:
|
|
|
3298 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
|
|
|
3299 |
#參考資料:
|
|
|
3300 |
#exec=>http://php.net/manual/en/function.exec.php
|
|
|
3301 |
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
|
|
|
3302 |
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
|
|
|
3303 |
$callShell=external::callShell($conf["external::callShell"]);
|
|
|
3304 |
unset($conf["external::callShell"]);
|
| 226 |
liveuser |
3305 |
|
| 3 |
liveuser |
3306 |
#如果執行程式失敗,且不是因為沒有抓取到內容的錯誤.
|
|
|
3307 |
if($callShell["status"]==="false" && $callShell["statusCode"]!==1 && $callShell["output"]!==array() ){
|
| 226 |
liveuser |
3308 |
|
| 3 |
liveuser |
3309 |
#設置執行失敗
|
|
|
3310 |
$result["status"]="false";
|
| 226 |
liveuser |
3311 |
|
| 3 |
liveuser |
3312 |
#設置執行錯誤訊息
|
|
|
3313 |
$result["error"]=$callShell;
|
| 226 |
liveuser |
3314 |
|
| 3 |
liveuser |
3315 |
#回傳結果
|
|
|
3316 |
return $result;
|
| 226 |
liveuser |
3317 |
|
| 3 |
liveuser |
3318 |
}#if end
|
| 226 |
liveuser |
3319 |
|
| 3 |
liveuser |
3320 |
#預設假設程序已經執行完畢
|
|
|
3321 |
$result["exist"]="false";
|
|
|
3322 |
|
|
|
3323 |
#如果程序還在執行
|
|
|
3324 |
if(count($callShell["output"])>0){
|
| 226 |
liveuser |
3325 |
|
| 3 |
liveuser |
3326 |
#設置程序還在執行
|
|
|
3327 |
$result["exist"]="true";
|
| 226 |
liveuser |
3328 |
|
| 3 |
liveuser |
3329 |
}#if end
|
| 226 |
liveuser |
3330 |
|
| 3 |
liveuser |
3331 |
#設置執行正常
|
| 226 |
liveuser |
3332 |
$result["status"]="true";
|
|
|
3333 |
|
| 3 |
liveuser |
3334 |
#回傳結果
|
|
|
3335 |
return $result;
|
| 226 |
liveuser |
3336 |
|
|
|
3337 |
}#function processStatus end
|
|
|
3338 |
|
| 3 |
liveuser |
3339 |
/*
|
|
|
3340 |
#函式說明:
|
|
|
3341 |
#呼叫shell依序執行系統命令,並取得回傳的內容.
|
|
|
3342 |
#回傳結果:
|
|
|
3343 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
3344 |
#$result["error"],錯誤訊息陣列.
|
| 226 |
liveuser |
3345 |
#$result["function"],當前執行的函數名稱.
|
| 3 |
liveuser |
3346 |
#$result["content"],爲執行完每個指令後的回傳結果.
|
|
|
3347 |
#$result["cmd"],執行的指令內容,若"sameShell"參數為"true",則會有該數值.
|
|
|
3348 |
#$result["output"],執行後得到的輸出,若"sameShell"參數為"true",則會有該數值.
|
|
|
3349 |
#必填參數:
|
|
|
3350 |
#$conf["command"],字串陣列,要執行的指令.
|
|
|
3351 |
$conf["command"]=array("");
|
|
|
3352 |
#$conf["fileArgu"],字串,變數__FILE__的內容,預設為當前檔案的路徑與名稱.
|
| 226 |
liveuser |
3353 |
#$conf["fileArgu"]="";
|
| 3 |
liveuser |
3354 |
#可省略參數:
|
|
|
3355 |
#$conf["argu"],陣字串列,執行各個$conf["command"]時指令搭配的參數,預設為空陣列.
|
|
|
3356 |
#$conf["argu"]=array(array(""));
|
|
|
3357 |
#$conf["enablePrintDescription"],字串陣列,執行各個$conf["command"]時是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,其數量須與$conf["command"]的元素數量相同,若只有一個元素,則代表是每個$conf["command"]執行時都用此參數.
|
|
|
3358 |
#$conf["enablePrintDescription"]=array("false");
|
|
|
3359 |
#$conf["printDescription"],字串陣列,執行各個$conf["command"]前要印出來的的文字,預設為$conf["command"]的內容,其數量須與$conf["command"]的元素數量相同,若只有一個元素,則代表是每個$conf["command"]執行時都用此參數.
|
|
|
3360 |
#$conf["printDescription"]=array("");
|
|
|
3361 |
#$conf["escapeshellarg"],字串陣列,執行各個$conf["command"]時是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false",其數量須與$conf["command"]的元素數量相同,若只有一個元素,則代表是每個$conf["command"]執行時都用此參數.
|
|
|
3362 |
#$conf["escapeshellarg"]=array("false");
|
|
|
3363 |
#$conf["username"],陣列字串,每個指令要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
|
|
|
3364 |
#$conf["username"]=array("");
|
|
|
3365 |
#$conf["password"],陣列字串,每個指令與$conf["username"]搭配的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
|
|
3366 |
#$conf["password"]=array("");
|
|
|
3367 |
#$conf["useScript"],字串,每個指令的執行是否要啟用Linux的script指令來記錄輸出,"true"代表要;"false"代表不要,預設為"false".
|
|
|
3368 |
#$conf["useScript"]="";
|
|
|
3369 |
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 ".qbpwcf_tmp/external/callShell/".
|
|
|
3370 |
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
|
|
|
3371 |
#$conf["inBackGround"],字串,每個指令的執行是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用";"區隔的多個指令將會出錯.
|
|
|
3372 |
#$conf["inBackGround"]="";
|
|
|
3373 |
#$conf["sameShell"],字串,"true"代表每個指令都在同空shell環境下執行,亦即會繼承變數、位置等資訊;預設為"false"代表要在個別獨立的shell環境下執行.
|
|
|
3374 |
#$conf["sameShell"]="true";
|
|
|
3375 |
#參考資料:
|
|
|
3376 |
#exec=>http://php.net/manual/en/function.exec.php
|
|
|
3377 |
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
|
|
|
3378 |
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
|
|
|
3379 |
#備註:
|
|
|
3380 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行.
|
| 226 |
liveuser |
3381 |
*/
|
| 3 |
liveuser |
3382 |
public static function callShellMulti(&$conf){
|
| 226 |
liveuser |
3383 |
|
| 3 |
liveuser |
3384 |
#初始化要回傳的結果
|
|
|
3385 |
$result=array();
|
| 226 |
liveuser |
3386 |
|
| 3 |
liveuser |
3387 |
#取得當前執行的函數名稱
|
|
|
3388 |
$result["function"]=__FUNCTION__;
|
| 226 |
liveuser |
3389 |
|
| 3 |
liveuser |
3390 |
#如果 $conf 不為陣列
|
|
|
3391 |
if(gettype($conf)!="array"){
|
| 226 |
liveuser |
3392 |
|
| 3 |
liveuser |
3393 |
#設置執行失敗
|
|
|
3394 |
$result["status"]="false";
|
| 226 |
liveuser |
3395 |
|
| 3 |
liveuser |
3396 |
#設置執行錯誤訊息
|
|
|
3397 |
$result["error"][]="\$conf變數須為陣列形態";
|
|
|
3398 |
|
|
|
3399 |
#如果傳入的參數為 null
|
|
|
3400 |
if($conf==null){
|
| 226 |
liveuser |
3401 |
|
| 3 |
liveuser |
3402 |
#設置執行錯誤訊息
|
|
|
3403 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
| 226 |
liveuser |
3404 |
|
| 3 |
liveuser |
3405 |
}#if end
|
|
|
3406 |
|
|
|
3407 |
#回傳結果
|
|
|
3408 |
return $result;
|
| 226 |
liveuser |
3409 |
|
| 3 |
liveuser |
3410 |
}#if end
|
| 226 |
liveuser |
3411 |
|
| 3 |
liveuser |
3412 |
#參數檢查
|
|
|
3413 |
#函式說明:
|
|
|
3414 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
3415 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
3416 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
3417 |
#$result["function"],當前執行的函式名稱.
|
|
|
3418 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
3419 |
#$result[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
3420 |
#$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
3421 |
#$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
3422 |
#必填寫的參數:
|
|
|
3423 |
#$conf["variableCheck::checkArguments"]["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
3424 |
$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
|
|
|
3425 |
#$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
3426 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("command","fileArgu");
|
| 226 |
liveuser |
3427 |
#$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
3428 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("array","string");
|
|
|
3429 |
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
3430 |
$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
3431 |
#可以省略的參數:
|
|
|
3432 |
#$conf["variableCheck::checkArguments"]["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true"。
|
|
|
3433 |
#$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
|
|
|
3434 |
#$conf["variableCheck::checkArguments"]["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
|
|
3435 |
$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("enablePrintDescription","printDescription","argu","escapeshellarg","username","password","useScript","logFilePath","inBackGround","sameShell");
|
| 226 |
liveuser |
3436 |
#$conf["variableCheck::checkArguments"]["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
3437 |
$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("array","array","array","array","array","array","string","string","string","string");
|
|
|
3438 |
#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,"null"代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
| 218 |
liveuser |
3439 |
$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(null,null,null,array(),null,null,null,null,null,"false");
|
| 3 |
liveuser |
3440 |
#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
3441 |
#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"][]=array();
|
|
|
3442 |
$checkResult=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
|
|
|
3443 |
unset($conf["variableCheck::checkArguments"]);
|
| 226 |
liveuser |
3444 |
|
| 3 |
liveuser |
3445 |
#如果檢查參數失敗
|
|
|
3446 |
if($checkResult["status"]=="false"){
|
| 226 |
liveuser |
3447 |
|
| 3 |
liveuser |
3448 |
#設置執行失敗
|
|
|
3449 |
$result["status"]="false";
|
| 226 |
liveuser |
3450 |
|
| 3 |
liveuser |
3451 |
#設置執行錯誤
|
|
|
3452 |
$result["error"]=$checkResult;
|
| 226 |
liveuser |
3453 |
|
| 3 |
liveuser |
3454 |
#回傳結果
|
|
|
3455 |
return $result;
|
| 226 |
liveuser |
3456 |
|
| 3 |
liveuser |
3457 |
}#if end
|
| 226 |
liveuser |
3458 |
|
| 3 |
liveuser |
3459 |
#如果檢查參數不通過
|
|
|
3460 |
if($checkResult["passed"]=="false"){
|
| 226 |
liveuser |
3461 |
|
| 3 |
liveuser |
3462 |
#設置執行失敗
|
|
|
3463 |
$result["status"]="false";
|
| 226 |
liveuser |
3464 |
|
| 3 |
liveuser |
3465 |
#設置執行錯誤
|
|
|
3466 |
$result["error"]=$checkResult;
|
| 226 |
liveuser |
3467 |
|
| 3 |
liveuser |
3468 |
#回傳結果
|
|
|
3469 |
return $result;
|
| 226 |
liveuser |
3470 |
|
| 218 |
liveuser |
3471 |
}#if end
|
| 226 |
liveuser |
3472 |
|
| 3 |
liveuser |
3473 |
#有幾個 $conf["command"] 就執行幾次
|
|
|
3474 |
for($i=0;$i<count($conf["command"]);$i++){
|
| 226 |
liveuser |
3475 |
|
| 3 |
liveuser |
3476 |
#如果 $conf["argu"] 存在
|
|
|
3477 |
if(isset($conf["argu"])){
|
| 226 |
liveuser |
3478 |
|
| 3 |
liveuser |
3479 |
#如果 $conf["argu"] 為 array 型態,且其元素數量等於 $conf["command"] 的元素數量
|
|
|
3480 |
if(gettype($conf["argu"])=="array" && count($conf["argu"])==count($conf["command"])){
|
| 226 |
liveuser |
3481 |
|
| 3 |
liveuser |
3482 |
#設置 $conf["argu"] 參數
|
|
|
3483 |
$conf["external::callShell"]["argu"]=$conf["argu"][$i];
|
| 226 |
liveuser |
3484 |
|
| 3 |
liveuser |
3485 |
}#if end
|
| 226 |
liveuser |
3486 |
|
| 3 |
liveuser |
3487 |
}#if end
|
| 226 |
liveuser |
3488 |
|
| 3 |
liveuser |
3489 |
#如果 $conf["enablePrintDescription"] 存在
|
|
|
3490 |
if(isset($conf["enablePrintDescription"])){
|
| 226 |
liveuser |
3491 |
|
| 3 |
liveuser |
3492 |
#如果 $conf["enablePrintDescription"] 為 array 型態,且其元素數量等於 $conf["command"] 的元素數量
|
|
|
3493 |
if(gettype($conf["enablePrintDescription"])=="array" && count($conf["enablePrintDescription"])==count($conf["command"])){
|
| 226 |
liveuser |
3494 |
|
| 3 |
liveuser |
3495 |
#設置 $conf["enablePrintDescription"] 參數
|
|
|
3496 |
$conf["external::callShell"]["enablePrintDescription"]=$conf["enablePrintDescription"][$i];
|
| 226 |
liveuser |
3497 |
|
| 3 |
liveuser |
3498 |
}#if end
|
| 226 |
liveuser |
3499 |
|
| 3 |
liveuser |
3500 |
#反之如果 $conf["enablePrintDescription"] 為 array 型態,且只有一個
|
|
|
3501 |
else if(gettype($conf["enablePrintDescription"])=="array" && count($conf["enablePrintDescription"])==1){
|
| 226 |
liveuser |
3502 |
|
| 3 |
liveuser |
3503 |
#設置 $conf["external::callShell"]["enablePrintDescription"] 參數為 $conf["enablePrintDescription"] 的第一個元素
|
|
|
3504 |
$conf["external::callShell"]["enablePrintDescription"]=$conf["enablePrintDescription"][0];
|
| 226 |
liveuser |
3505 |
|
| 3 |
liveuser |
3506 |
}#if end
|
| 226 |
liveuser |
3507 |
|
| 3 |
liveuser |
3508 |
}#if end
|
| 226 |
liveuser |
3509 |
|
| 3 |
liveuser |
3510 |
#如果 $conf["printDescription"] 存在
|
|
|
3511 |
if(isset($conf["printDescription"])){
|
| 226 |
liveuser |
3512 |
|
| 3 |
liveuser |
3513 |
#如果 $conf["printDescription"] 為 array 型態,且其元素數量等於 $conf["command"] 的元素數量
|
|
|
3514 |
if(gettype($conf["printDescription"])=="array" && count($conf["printDescription"])==count($conf["command"])){
|
| 226 |
liveuser |
3515 |
|
| 3 |
liveuser |
3516 |
#設置 $conf["enablePrintDescription"] 參數
|
|
|
3517 |
$conf["external::callShell"]["printDescription"]=$conf["printDescription"][$i];
|
| 226 |
liveuser |
3518 |
|
| 3 |
liveuser |
3519 |
}#if end
|
| 226 |
liveuser |
3520 |
|
| 3 |
liveuser |
3521 |
#反之如果 $conf["printDescription"] 為 array 型態,且只有一個
|
|
|
3522 |
else if(gettype($conf["printDescription"])=="array" && count($conf["printDescription"])==1){
|
| 226 |
liveuser |
3523 |
|
| 3 |
liveuser |
3524 |
#設置 $conf["external::callShell"]["printDescription"] 參數為 $conf["printDescription"] 的第一個元素
|
|
|
3525 |
$conf["external::callShell"]["printDescription"]=$conf["printDescription"][0];
|
| 226 |
liveuser |
3526 |
|
| 3 |
liveuser |
3527 |
}#if end
|
| 226 |
liveuser |
3528 |
|
| 3 |
liveuser |
3529 |
}#if end
|
| 226 |
liveuser |
3530 |
|
| 3 |
liveuser |
3531 |
#如果 $conf["escapeshellarg"] 存在
|
|
|
3532 |
if(isset($conf["escapeshellarg"])){
|
| 226 |
liveuser |
3533 |
|
| 3 |
liveuser |
3534 |
#如果 $conf["escapeshellarg"] 為 array 型態,且其元素數量等於 $conf["command"] 的元素數量
|
|
|
3535 |
if(gettype($conf["escapeshellarg"])=="array" && count($conf["escapeshellarg"])==count($conf["command"])){
|
| 226 |
liveuser |
3536 |
|
| 3 |
liveuser |
3537 |
#設置 $conf["escapeshellarg"] 參數
|
|
|
3538 |
$conf["external::callShell"]["escapeshellarg"]=$conf["escapeshellarg"][$i];
|
| 226 |
liveuser |
3539 |
|
| 3 |
liveuser |
3540 |
}#if end
|
| 226 |
liveuser |
3541 |
|
| 3 |
liveuser |
3542 |
#反之如果 $conf["escapeshellarg"] 為 array 型態,且只有一個
|
|
|
3543 |
else if(gettype($conf["escapeshellarg"])=="array" && count($conf["escapeshellarg"])==1){
|
| 226 |
liveuser |
3544 |
|
| 3 |
liveuser |
3545 |
#設置 $conf["external::callShell"]["escapeshellarg"] 參數為 $conf["escapeshellarg"] 的第一個元素
|
|
|
3546 |
$conf["external::callShell"]["escapeshellarg"]=$conf["escapeshellarg"][0];
|
| 226 |
liveuser |
3547 |
|
| 3 |
liveuser |
3548 |
}#if end
|
| 226 |
liveuser |
3549 |
|
| 3 |
liveuser |
3550 |
}#if end
|
| 226 |
liveuser |
3551 |
|
| 3 |
liveuser |
3552 |
#如果 $conf["username"] 存在
|
|
|
3553 |
if(isset($conf["username"])){
|
| 226 |
liveuser |
3554 |
|
| 3 |
liveuser |
3555 |
#如果 $conf["username"] 為 array 型態,且其元素數量等於 $conf["command"] 的元素數量
|
|
|
3556 |
if(gettype($conf["username"])=="array" && count($conf["username"])==count($conf["command"])){
|
| 226 |
liveuser |
3557 |
|
| 3 |
liveuser |
3558 |
#設置 $conf["username"] 參數
|
|
|
3559 |
$conf["external::callShell"]["username"]=$conf["username"][$i];
|
| 226 |
liveuser |
3560 |
|
| 3 |
liveuser |
3561 |
}#if end
|
| 226 |
liveuser |
3562 |
|
| 3 |
liveuser |
3563 |
#反之如果 $conf["escapeshellarg"] 為 array 型態,且只有一個
|
|
|
3564 |
else if(gettype($conf["username"])=="array" && count($conf["username"])==1){
|
| 226 |
liveuser |
3565 |
|
| 3 |
liveuser |
3566 |
#設置 $conf["external::callShell"]["username"] 參數為 $conf["username"] 的第一個元素
|
|
|
3567 |
$conf["external::callShell"]["username"]=$conf["username"][0];
|
| 226 |
liveuser |
3568 |
|
| 3 |
liveuser |
3569 |
}#if end
|
| 226 |
liveuser |
3570 |
|
| 218 |
liveuser |
3571 |
}#if end
|
| 226 |
liveuser |
3572 |
|
| 3 |
liveuser |
3573 |
#如果 $conf["password"] 存在
|
|
|
3574 |
if(isset($conf["password"])){
|
| 226 |
liveuser |
3575 |
|
| 3 |
liveuser |
3576 |
#如果 $conf["password"] 為 array 型態,且其元素數量等於 $conf["command"] 的元素數量
|
|
|
3577 |
if(gettype($conf["password"])=="array" && count($conf["password"])==count($conf["command"])){
|
| 226 |
liveuser |
3578 |
|
| 3 |
liveuser |
3579 |
#設置 $conf["password"] 參數
|
|
|
3580 |
$conf["external::callShell"]["password"]=$conf["password"][$i];
|
| 226 |
liveuser |
3581 |
|
| 3 |
liveuser |
3582 |
}#if end
|
| 226 |
liveuser |
3583 |
|
| 3 |
liveuser |
3584 |
#反之如果 $conf["password"] 為 array 型態,且只有一個
|
|
|
3585 |
else if(gettype($conf["password"])=="array" && count($conf["password"])==1){
|
| 226 |
liveuser |
3586 |
|
| 3 |
liveuser |
3587 |
#設置 $conf["external::callShell"]["password"] 參數為 $conf["password"] 的第一個元素
|
|
|
3588 |
$conf["external::callShell"]["password"]=$conf["password"][0];
|
| 226 |
liveuser |
3589 |
|
| 3 |
liveuser |
3590 |
}#if end
|
| 226 |
liveuser |
3591 |
|
| 218 |
liveuser |
3592 |
}#if end
|
| 226 |
liveuser |
3593 |
|
| 3 |
liveuser |
3594 |
#如果有設置 $conf["logFilePath"]
|
|
|
3595 |
if(isset($conf["logFilePath"])){
|
| 226 |
liveuser |
3596 |
|
| 3 |
liveuser |
3597 |
#設置 $conf["logFilePath"] 參數
|
|
|
3598 |
$conf["external::callShell"]["logFilePath"]=$conf["logFilePath"];
|
| 226 |
liveuser |
3599 |
|
| 218 |
liveuser |
3600 |
}#if end
|
| 226 |
liveuser |
3601 |
|
| 3 |
liveuser |
3602 |
#如果有設置 $conf["fileArgu"]
|
|
|
3603 |
if(isset($conf["fileArgu"])){
|
| 226 |
liveuser |
3604 |
|
| 3 |
liveuser |
3605 |
#設置 $conf["fileArgu"] 參數
|
|
|
3606 |
$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];
|
| 226 |
liveuser |
3607 |
|
| 3 |
liveuser |
3608 |
}#if end
|
| 226 |
liveuser |
3609 |
|
| 3 |
liveuser |
3610 |
#如果有設置 $conf["inBackGround"]
|
|
|
3611 |
if(isset($conf["inBackGround"])){
|
| 226 |
liveuser |
3612 |
|
| 3 |
liveuser |
3613 |
#設置 $conf["fileArgu"] 參數
|
|
|
3614 |
$conf["external::callShell"]["inBackGround"]=$conf["inBackGround"];
|
| 226 |
liveuser |
3615 |
|
| 3 |
liveuser |
3616 |
}#if end
|
| 226 |
liveuser |
3617 |
|
| 3 |
liveuser |
3618 |
#如果有指定 sameShell
|
|
|
3619 |
if($conf["sameShell"]==="true"){
|
| 226 |
liveuser |
3620 |
|
| 3 |
liveuser |
3621 |
#設置不執行指令
|
|
|
3622 |
$conf["external::callShell"]["doNotRun"]="true";
|
| 226 |
liveuser |
3623 |
|
| 3 |
liveuser |
3624 |
}#if end
|
| 226 |
liveuser |
3625 |
|
| 218 |
liveuser |
3626 |
#如果沒有指定是否要 escapeshellarg
|
|
|
3627 |
if(!isset($conf["escapeshellarg"][$i])){
|
| 226 |
liveuser |
3628 |
|
| 218 |
liveuser |
3629 |
#設置為 "false"
|
|
|
3630 |
$conf["escapeshellarg"][$i]="false";
|
| 226 |
liveuser |
3631 |
|
| 218 |
liveuser |
3632 |
}#if end
|
| 226 |
liveuser |
3633 |
|
| 3 |
liveuser |
3634 |
#函式說明:
|
|
|
3635 |
#呼叫shell執行系統命令,並取得回傳的內容.
|
|
|
3636 |
#回傳結果:
|
|
|
3637 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
3638 |
#$result["error"],錯誤訊息陣列.
|
|
|
3639 |
#$result["function"],當前執行的函數名稱.
|
|
|
3640 |
#$result["cmd"],執行的指令內容.
|
|
|
3641 |
#$result["output"],爲執行完二元碼後的輸出陣列.
|
|
|
3642 |
#必填參數:
|
|
|
3643 |
#$conf["command"],字串,要執行的指令與.
|
|
|
3644 |
$conf["external::callShell"]["command"]=$conf["command"][$i];
|
|
|
3645 |
#可省略參數:
|
|
|
3646 |
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
|
|
|
3647 |
#$conf["argu"]=array("");
|
|
|
3648 |
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
|
|
|
3649 |
#$conf["enablePrintDescription"]="true";
|
|
|
3650 |
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容.
|
|
|
3651 |
#$conf["printDescription"]="";
|
|
|
3652 |
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".
|
| 218 |
liveuser |
3653 |
$conf["external::callShell"]["escapeshellarg"]=$conf["escapeshellarg"][$i];
|
| 3 |
liveuser |
3654 |
#$conf["username"],字串,要用什麼使用者來執行,預設為執行apache的使用者,該參數不適用於apache環境.
|
|
|
3655 |
#$conf["username"]="";
|
|
|
3656 |
#$conf["password"],字串,與$conf["username"]搭配的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
|
|
3657 |
#$conf["password"]="";
|
|
|
3658 |
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要;"false"代表不要,預設為"false".
|
|
|
3659 |
#$conf["useScript"]="";
|
|
|
3660 |
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 ".qbpwcf_tmp/external/callShell/".
|
|
|
3661 |
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
|
|
|
3662 |
#$conf["fileArgu"],字串,變數__FILE__的內容,預設為當前檔案的路徑與名稱.
|
| 226 |
liveuser |
3663 |
#$conf["fileArgu"]="";
|
| 3 |
liveuser |
3664 |
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false".
|
|
|
3665 |
#$conf["inBackGround"]="";
|
|
|
3666 |
#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
|
|
|
3667 |
#$conf["external::callShell"]["doNotRun"]="false";
|
|
|
3668 |
#備註:
|
|
|
3669 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行.
|
|
|
3670 |
#參考資料:
|
|
|
3671 |
#exec=>http://php.net/manual/en/function.exec.php
|
|
|
3672 |
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
|
|
|
3673 |
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
|
|
|
3674 |
$callShell=external::callShell($conf["external::callShell"]);
|
|
|
3675 |
unset($conf["external::callShell"]);
|
| 226 |
liveuser |
3676 |
|
| 3 |
liveuser |
3677 |
#如果執行shell失敗
|
|
|
3678 |
if($callShell["status"]=="false"){
|
| 226 |
liveuser |
3679 |
|
| 3 |
liveuser |
3680 |
#設置執行失敗
|
|
|
3681 |
$result["status"]="false";
|
| 226 |
liveuser |
3682 |
|
| 3 |
liveuser |
3683 |
#設置執行錯誤
|
|
|
3684 |
$result["error"]=$callShell;
|
| 226 |
liveuser |
3685 |
|
| 3 |
liveuser |
3686 |
#回傳結果
|
|
|
3687 |
return $result;
|
| 226 |
liveuser |
3688 |
|
| 3 |
liveuser |
3689 |
}#if end
|
| 226 |
liveuser |
3690 |
|
| 3 |
liveuser |
3691 |
#如果有指定 sameShell
|
|
|
3692 |
if($conf["sameShell"]==="true"){
|
| 226 |
liveuser |
3693 |
|
| 3 |
liveuser |
3694 |
#如果沒有指定 $rawCmd
|
|
|
3695 |
if(!isset($rawCmd)){
|
| 226 |
liveuser |
3696 |
|
| 3 |
liveuser |
3697 |
#初始化要執行的指令內容
|
|
|
3698 |
$rawCmd=$callShell["cmd"];
|
| 226 |
liveuser |
3699 |
|
| 3 |
liveuser |
3700 |
}#if end
|
| 226 |
liveuser |
3701 |
|
| 3 |
liveuser |
3702 |
#反之
|
|
|
3703 |
else{
|
| 226 |
liveuser |
3704 |
|
| 3 |
liveuser |
3705 |
#用";"串接指令
|
|
|
3706 |
$rawCmd=$rawCmd.";".$callShell["cmd"];
|
| 226 |
liveuser |
3707 |
|
| 3 |
liveuser |
3708 |
}#else end
|
| 226 |
liveuser |
3709 |
|
| 3 |
liveuser |
3710 |
}#if end
|
| 226 |
liveuser |
3711 |
|
| 3 |
liveuser |
3712 |
#反之在個別shell環境下執行
|
|
|
3713 |
else{
|
| 226 |
liveuser |
3714 |
|
| 3 |
liveuser |
3715 |
#紀錄每個指令執行的結果
|
|
|
3716 |
$result["content"][]=$callShell;
|
| 226 |
liveuser |
3717 |
|
| 3 |
liveuser |
3718 |
}#else end
|
| 226 |
liveuser |
3719 |
|
| 3 |
liveuser |
3720 |
}#for end
|
| 226 |
liveuser |
3721 |
|
| 3 |
liveuser |
3722 |
#如果有 $rawCmd 存在
|
|
|
3723 |
if(isset($rawCmd)){
|
| 226 |
liveuser |
3724 |
|
| 3 |
liveuser |
3725 |
#運行指令
|
|
|
3726 |
exec($rawCmd,$output,$status);
|
| 226 |
liveuser |
3727 |
|
| 3 |
liveuser |
3728 |
#設置執行的指令內容.
|
|
|
3729 |
$result["cmd"]=$rawCmd;
|
| 226 |
liveuser |
3730 |
|
| 3 |
liveuser |
3731 |
#執行後得到的輸出.
|
|
|
3732 |
$result["output"]=$output;
|
| 226 |
liveuser |
3733 |
|
| 3 |
liveuser |
3734 |
}#if end
|
| 226 |
liveuser |
3735 |
|
| 3 |
liveuser |
3736 |
#執行到這邊代表執行正常
|
|
|
3737 |
$result["status"]="true";
|
| 226 |
liveuser |
3738 |
|
| 3 |
liveuser |
3739 |
#回傳結果
|
|
|
3740 |
return $result;
|
| 226 |
liveuser |
3741 |
|
| 3 |
liveuser |
3742 |
}#function callShellMulti end
|
| 226 |
liveuser |
3743 |
|
| 3 |
liveuser |
3744 |
/*
|
|
|
3745 |
#函式說明:
|
|
|
3746 |
#編譯C++檔案,下載輸出的2元碼.
|
|
|
3747 |
#回傳結果:
|
|
|
3748 |
#當 $conf["noDownload"] 設為 "true" 或執行出錯時 才會有回傳的結果.
|
|
|
3749 |
#$result["status"],"true"代表執行正常,"false"代表執行失敗.
|
|
|
3750 |
#$result["error"],錯誤訊息陣列.
|
|
|
3751 |
#$result["function"],正在執行的函式
|
|
|
3752 |
#$result["content"],編譯好的檔案位置.
|
|
|
3753 |
#必填參數:
|
|
|
3754 |
#$conf["cppAddress"],字串,要編譯的cpp檔位置,請加上cpp副檔名.
|
|
|
3755 |
$conf["cppAddress"]="";
|
|
|
3756 |
#可省略參數:
|
|
|
3757 |
#$conf["outputAddress"],字串,輸出的檔案位置與名稱,請加上附檔名,預設為".bin/cppOriFileName.bin".
|
|
|
3758 |
#$conf["outputAddress"]="";
|
|
|
3759 |
#$conf["noDownload"],字串,"true"為不要下載,改成回傳編譯好的檔案位置.
|
|
|
3760 |
#$conf["noDownload"]="true";
|
|
|
3761 |
#參考資料:
|
|
|
3762 |
#無.
|
|
|
3763 |
#備註:
|
|
|
3764 |
#測試中...
|
|
|
3765 |
#為了抓取編譯cpp產生的錯誤,要改用script指令來抓取輸出的結果.
|
| 226 |
liveuser |
3766 |
*/
|
| 3 |
liveuser |
3767 |
public static function compileCppThenDownload(&$conf){
|
| 226 |
liveuser |
3768 |
|
| 3 |
liveuser |
3769 |
#初始化要回傳的變數
|
|
|
3770 |
$result=array();
|
| 226 |
liveuser |
3771 |
|
| 3 |
liveuser |
3772 |
#取得當前執行的函式
|
|
|
3773 |
$result["function"]=__FUNCTION__;
|
| 226 |
liveuser |
3774 |
|
| 3 |
liveuser |
3775 |
#如果 $conf 不為陣列
|
|
|
3776 |
if(gettype($conf)!="array"){
|
| 226 |
liveuser |
3777 |
|
| 3 |
liveuser |
3778 |
#設置執行失敗
|
|
|
3779 |
$result["status"]="false";
|
| 226 |
liveuser |
3780 |
|
| 3 |
liveuser |
3781 |
#設置執行錯誤訊息
|
|
|
3782 |
$result["error"][]="\$conf變數須為陣列形態";
|
|
|
3783 |
|
|
|
3784 |
#如果傳入的參數為 null
|
|
|
3785 |
if($conf==null){
|
| 226 |
liveuser |
3786 |
|
| 3 |
liveuser |
3787 |
#設置執行錯誤訊息
|
|
|
3788 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
| 226 |
liveuser |
3789 |
|
| 3 |
liveuser |
3790 |
}#if end
|
|
|
3791 |
|
|
|
3792 |
#回傳結果
|
|
|
3793 |
return $result;
|
| 226 |
liveuser |
3794 |
|
| 3 |
liveuser |
3795 |
}#if end
|
| 226 |
liveuser |
3796 |
|
| 3 |
liveuser |
3797 |
#檢查參數
|
|
|
3798 |
#函式說明:
|
|
|
3799 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
3800 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
3801 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
3802 |
#$result["function"],當前執行的函式名稱.
|
|
|
3803 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
3804 |
#$result[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
3805 |
#$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
3806 |
#$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
3807 |
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
|
|
|
3808 |
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
|
|
|
3809 |
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
|
|
|
3810 |
#必填寫的參數:
|
|
|
3811 |
#$conf["variableCheck::checkArguments"]["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
3812 |
$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
|
|
|
3813 |
#$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
3814 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("cppAddress");
|
| 226 |
liveuser |
3815 |
#$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
3816 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string");
|
|
|
3817 |
#$conf["variableCheck::checkArguments"]["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
3818 |
$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
3819 |
#可以省略的參數:
|
|
|
3820 |
#$conf["variableCheck::checkArguments"]["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true"。
|
|
|
3821 |
$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
|
|
|
3822 |
#$conf["variableCheck::checkArguments"]["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
|
|
3823 |
$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("outputAddress","noDownload");
|
| 226 |
liveuser |
3824 |
#$conf["variableCheck::checkArguments"]["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
3825 |
$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string");
|
|
|
3826 |
#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
|
|
3827 |
$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(".bin/".$conf["cppAddress"].".bin","true");
|
|
|
3828 |
#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
3829 |
#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"][]=array();
|
|
|
3830 |
#參考資料來源:
|
|
|
3831 |
#array_keys=>http://php.net/manual/en/function.array-keys.php
|
|
|
3832 |
$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
|
|
|
3833 |
unset($conf["variableCheck::checkArguments"]);
|
| 226 |
liveuser |
3834 |
|
| 3 |
liveuser |
3835 |
#如果 $checkArguments["status"] 等於 "false"
|
|
|
3836 |
if($checkArguments["status"]=="false"){
|
| 226 |
liveuser |
3837 |
|
| 3 |
liveuser |
3838 |
#設置 $result["status"] 為 "false"
|
|
|
3839 |
$result["status"]="false";
|
| 226 |
liveuser |
3840 |
|
| 3 |
liveuser |
3841 |
#設置 $result["error"]
|
|
|
3842 |
$result["error"]=$checkArguments;
|
| 226 |
liveuser |
3843 |
|
| 3 |
liveuser |
3844 |
#回傳結果
|
|
|
3845 |
return $result;
|
| 226 |
liveuser |
3846 |
|
| 3 |
liveuser |
3847 |
}#if end
|
| 226 |
liveuser |
3848 |
|
| 3 |
liveuser |
3849 |
#如果 $checkArguments["passed"] 等於 "false"
|
|
|
3850 |
if($checkArguments["passed"]=="false"){
|
| 226 |
liveuser |
3851 |
|
| 3 |
liveuser |
3852 |
#設置 $result["status"] 為 "false"
|
|
|
3853 |
$result["status"]="false";
|
| 226 |
liveuser |
3854 |
|
| 3 |
liveuser |
3855 |
#設置 $result["error"]
|
|
|
3856 |
$result["error"]=$checkArguments;
|
| 226 |
liveuser |
3857 |
|
| 3 |
liveuser |
3858 |
#回傳結果
|
|
|
3859 |
return $result;
|
| 226 |
liveuser |
3860 |
|
| 3 |
liveuser |
3861 |
}#if end
|
|
|
3862 |
|
|
|
3863 |
#涵是說明:
|
|
|
3864 |
#取得檔案路徑字串的路徑與檔案的名稱與檔案副檔名
|
|
|
3865 |
#回傳結果:
|
|
|
3866 |
#$result["status"],執行是否正常,"true"正常,"false"代表不正常.
|
|
|
3867 |
#$result["error"],錯誤訊息.
|
|
|
3868 |
#$result["filePath"],路徑字串.
|
|
|
3869 |
#$result["fileName"],檔案名稱字串.
|
|
|
3870 |
#$result["fileExtention"],檔案的副檔名.
|
|
|
3871 |
#$result["fullFileName"],含副檔名的檔案名稱.
|
|
|
3872 |
#$result["fullFilePathAndName"],完整的檔案路徑(含副檔名).
|
|
|
3873 |
#必填參數:
|
|
|
3874 |
#$conf["fileAccess"]["getFileAddressAndNameAndFileExtention"]["fileAddressAndName"],字串,檔案名稱與其路徑.
|
|
|
3875 |
$conf["fileAccess"]["getFileAddressAndNameAndFileExtention"]["fileAddressAndName"]=$conf["outputAddress"];
|
|
|
3876 |
#可省略參數:
|
|
|
3877 |
#無.
|
|
|
3878 |
$filePathInfo=fileAccess::getFileAddressAndNameAndFileExtention($conf["fileAccess"]["getFileAddressAndNameAndFileExtention"]);
|
| 226 |
liveuser |
3879 |
unset($conf["fileAccess"]["getFileAddressAndNameAndFileExtention"]);
|
|
|
3880 |
|
| 3 |
liveuser |
3881 |
#若取得檔案路徑字串的路徑與檔案的名稱與檔案副檔名失敗
|
|
|
3882 |
if($filePathInfo["status"]=="false"){
|
| 226 |
liveuser |
3883 |
|
| 3 |
liveuser |
3884 |
#設置 $result["status"] 為 "false"
|
|
|
3885 |
$result["status"]="false";
|
| 226 |
liveuser |
3886 |
|
| 3 |
liveuser |
3887 |
#設置 $result["error"]
|
|
|
3888 |
$result["error"]=$filePathInfo;
|
| 226 |
liveuser |
3889 |
|
| 3 |
liveuser |
3890 |
#回傳結果
|
|
|
3891 |
return $result;
|
| 226 |
liveuser |
3892 |
|
| 3 |
liveuser |
3893 |
}#if end
|
| 226 |
liveuser |
3894 |
|
| 3 |
liveuser |
3895 |
#如果 $filePathInfo["filePath"] 不等於 ""
|
|
|
3896 |
if($filePathInfo["filePath"]!=""){
|
| 226 |
liveuser |
3897 |
|
| 3 |
liveuser |
3898 |
#涵式說明:
|
|
|
3899 |
#建立資料夾,若要建立的資料夾所屬路徑不存在,則會自動嘗試建立,建立後的資料夾也可指定權限.
|
|
|
3900 |
#回傳的結果:
|
|
|
3901 |
#$result["status"],"true"爲建立成功,"false"爲建立失敗.
|
|
|
3902 |
#$result["error"],錯誤訊息陣列
|
|
|
3903 |
#必填參數::
|
|
|
3904 |
$conf["fileAccess"]["createFolderAfterCheck"]["dirPositionAndName"]=$filePathInfo["filePath"];#新建的位置與名稱
|
|
|
3905 |
#可省略參數:
|
|
|
3906 |
#$conf["fileAccess"]["createFolderAfterCheck"]["dirPermission"]="";#新建資料夾的權限設定,預設爲0770,亦即擁有者,同群組者可以讀,寫,存取,其他人僅能存取.
|
|
|
3907 |
$checkResult=fileAccess::createFolderAfterCheck($conf["fileAccess"]["createFolderAfterCheck"]);
|
|
|
3908 |
unset($conf["fileAccess"]["createFolderAfterCheck"]);
|
| 226 |
liveuser |
3909 |
|
| 3 |
liveuser |
3910 |
#如果 $checkResult["status"] 等於 "false"
|
|
|
3911 |
if($checkResult["status"]=="false"){
|
| 226 |
liveuser |
3912 |
|
| 3 |
liveuser |
3913 |
#設置錯誤識別
|
|
|
3914 |
$result["status"]="false";
|
| 226 |
liveuser |
3915 |
|
| 3 |
liveuser |
3916 |
#設置錯誤訊息
|
|
|
3917 |
$result["error"]=$checkResult;
|
| 226 |
liveuser |
3918 |
|
| 3 |
liveuser |
3919 |
#回傳結果
|
|
|
3920 |
return $result;
|
| 226 |
liveuser |
3921 |
|
| 3 |
liveuser |
3922 |
}#if end
|
| 226 |
liveuser |
3923 |
|
| 3 |
liveuser |
3924 |
}#if end
|
| 226 |
liveuser |
3925 |
|
| 3 |
liveuser |
3926 |
#檢查目標 cpp 檔案在不在
|
|
|
3927 |
#涵式說明:檢查多個檔案與資料夾是否存在.
|
|
|
3928 |
#回傳結果:
|
|
|
3929 |
#$result["varName"][$i],爲第$i個資料夾或檔案的名稱。
|
|
|
3930 |
#$result["varExist"][$i],爲第$i個資料夾或檔案是否存在,"true"代表存在,"false"代表不存在。
|
|
|
3931 |
#必填參數:
|
|
|
3932 |
$conf["fileAccess"]["checkMultiFileExist"]["fileArray"]=array($conf["cppAddress"]);#要檢查書否存在的檔案有哪些,須爲一維陣列數值。
|
|
|
3933 |
#參考資料來源:
|
|
|
3934 |
#http://php.net/manual/en/function.file-exists.php
|
|
|
3935 |
#http://php.net/manual/en/control-structures.foreach.php
|
|
|
3936 |
$checkResult=fileAccess::checkMultiFileExist($conf["fileAccess"]["checkMultiFileExist"]);
|
|
|
3937 |
unset($conf["fileAccess"]["checkMultiFileExist"]);
|
| 226 |
liveuser |
3938 |
|
| 3 |
liveuser |
3939 |
#如果 $checkResult["status"]為"false"
|
|
|
3940 |
if($checkResult["status"]=="false"){
|
| 226 |
liveuser |
3941 |
|
| 3 |
liveuser |
3942 |
#設置錯誤識別
|
|
|
3943 |
$result["status"]="false";
|
| 226 |
liveuser |
3944 |
|
| 3 |
liveuser |
3945 |
#設置錯誤訊息
|
|
|
3946 |
$result["error"]=$checkResult;
|
| 226 |
liveuser |
3947 |
|
| 3 |
liveuser |
3948 |
#回傳結果
|
|
|
3949 |
return $result;
|
| 226 |
liveuser |
3950 |
|
| 3 |
liveuser |
3951 |
}#if end
|
| 226 |
liveuser |
3952 |
|
| 3 |
liveuser |
3953 |
#如果 $checkResult["varExist"][0] 等於 "false"
|
|
|
3954 |
if($checkResult["varExist"][0]=="false"){
|
| 226 |
liveuser |
3955 |
|
| 3 |
liveuser |
3956 |
#設置錯誤識別
|
|
|
3957 |
$result["status"]="false";
|
| 226 |
liveuser |
3958 |
|
| 3 |
liveuser |
3959 |
#設置錯誤訊息
|
|
|
3960 |
$result["error"][]="要編譯的檔案 ".$conf["cppAddress"]." 不存在";
|
| 226 |
liveuser |
3961 |
|
| 3 |
liveuser |
3962 |
#回傳結果
|
|
|
3963 |
return $result;
|
| 226 |
liveuser |
3964 |
|
| 3 |
liveuser |
3965 |
}#if end
|
| 226 |
liveuser |
3966 |
|
| 3 |
liveuser |
3967 |
#執行系統命令 編譯 $conf["cppAddress"]
|
|
|
3968 |
#函式說明:
|
|
|
3969 |
#呼叫shell執行系統命令,並取得回傳的內容.
|
|
|
3970 |
#回傳結果:
|
|
|
3971 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
3972 |
#$result["error"],錯誤訊息陣列.
|
|
|
3973 |
#$result["function"],當前執行的函數名稱.
|
|
|
3974 |
#$result["argu"],使用的參數.
|
|
|
3975 |
#$result["cmd"],執行的指令內容.
|
|
|
3976 |
#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
3977 |
#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
|
|
|
3978 |
#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
3979 |
#$result["running"],是否還在執行.
|
|
|
3980 |
#$result["pid"],pid.
|
|
|
3981 |
#$result["statusCode"],執行結束後的代碼.
|
|
|
3982 |
#$result["escape"],陣列,儲存出新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu").
|
|
|
3983 |
#必填參數:
|
|
|
3984 |
#$conf["command"],字串,要執行的指令.
|
|
|
3985 |
$conf["external::callShell"]["command"]="g++";
|
|
|
3986 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
|
|
3987 |
$conf["external::callShell"]["fileArgu"]=__FILE__;
|
|
|
3988 |
#可省略參數:
|
|
|
3989 |
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
|
|
|
3990 |
$conf["external::callShell"]["argu"]=array($conf["cppAddress"],"-o",$conf["outputAddress"]);
|
|
|
3991 |
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
|
|
|
3992 |
#$conf["arguIsAddr"]=array();
|
|
|
3993 |
#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
|
|
|
3994 |
#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
|
|
|
3995 |
#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
|
|
|
3996 |
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
|
|
|
3997 |
#$conf["enablePrintDescription"]="true";
|
|
|
3998 |
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
|
|
|
3999 |
#$conf["printDescription"]="";
|
|
|
4000 |
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
|
|
|
4001 |
$conf["external::callShell"]["escapeshellarg"]="true";
|
|
|
4002 |
#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
|
|
|
4003 |
#$conf["thereIsShellVar"]=array();
|
|
|
4004 |
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
|
|
|
4005 |
#$conf["username"]="";
|
|
|
4006 |
#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
|
|
4007 |
#$conf["password"]="";
|
|
|
4008 |
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
|
|
|
4009 |
#$conf["useScript"]="";
|
|
|
4010 |
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
|
|
|
4011 |
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
|
|
|
4012 |
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
|
|
|
4013 |
#$conf["inBackGround"]="";
|
|
|
4014 |
#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
|
|
|
4015 |
#$conf["getErr"]="false";
|
|
|
4016 |
#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
|
|
|
4017 |
#$conf["doNotRun"]="false";
|
|
|
4018 |
#參考資料:
|
|
|
4019 |
#exec=>http://php.net/manual/en/function.exec.php
|
|
|
4020 |
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
|
|
|
4021 |
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
|
|
|
4022 |
#備註:
|
|
|
4023 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
|
|
|
4024 |
#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
|
|
|
4025 |
$execResult=external::callShell($conf["external::callShell"]);
|
|
|
4026 |
unset($conf["external::callShell"]);
|
| 226 |
liveuser |
4027 |
|
| 3 |
liveuser |
4028 |
#如果 $execResult["status"] 等於 "false"
|
|
|
4029 |
if($execResult["status"]=="false"){
|
| 226 |
liveuser |
4030 |
|
| 3 |
liveuser |
4031 |
#設置錯誤識別
|
|
|
4032 |
$result["status"]="false";
|
| 226 |
liveuser |
4033 |
|
| 3 |
liveuser |
4034 |
#設置錯誤訊息
|
|
|
4035 |
$result["error"]=$execResult;
|
| 226 |
liveuser |
4036 |
|
| 3 |
liveuser |
4037 |
#回傳結果
|
|
|
4038 |
return $result;
|
| 226 |
liveuser |
4039 |
|
| 3 |
liveuser |
4040 |
}#if end
|
| 226 |
liveuser |
4041 |
|
| 3 |
liveuser |
4042 |
#如果 $conf["noDownload"] 有設置了
|
|
|
4043 |
if(isset($conf["noDownload"])){
|
| 226 |
liveuser |
4044 |
|
| 3 |
liveuser |
4045 |
#如果$conf["noDownload"] 等於 "true"
|
|
|
4046 |
if($conf["noDownload"]=="true"){
|
| 226 |
liveuser |
4047 |
|
| 3 |
liveuser |
4048 |
#設置執行正常的識別
|
|
|
4049 |
$result["status"]="true";
|
| 226 |
liveuser |
4050 |
|
| 3 |
liveuser |
4051 |
#設置編譯好的檔案位置
|
|
|
4052 |
$result["content"]=$conf["outputAddress"];
|
| 226 |
liveuser |
4053 |
|
| 3 |
liveuser |
4054 |
#回傳結果
|
|
|
4055 |
return $result;
|
| 226 |
liveuser |
4056 |
|
| 3 |
liveuser |
4057 |
}#if end
|
| 226 |
liveuser |
4058 |
|
| 3 |
liveuser |
4059 |
}#if end
|
| 226 |
liveuser |
4060 |
|
| 3 |
liveuser |
4061 |
#說明:
|
|
|
4062 |
#要求使用者Download某檔案
|
|
|
4063 |
#回傳結果:
|
|
|
4064 |
#true爲成功,反之爲錯誤訊息
|
|
|
4065 |
#必填參數::
|
|
|
4066 |
$conf["header"]["askUserDownloadFile"]["filePositionAndName"]=$conf["outputAddress"];#檔案的位置與名稱
|
|
|
4067 |
#可省略參數:
|
|
|
4068 |
$conf["header"]["askUserDownloadFile"]["fileDisplayName"]=$filePathInfo["fullFileName"];#要顯示的檔案名稱,若要放中文請將其文字放在()裏面,這樣種文字才會顯現。預設為 basename($conf["filePositionAndName"])." date:".$系統時間;
|
|
|
4069 |
#參考資料來源:
|
|
|
4070 |
#http:#php.net/manual/en/function.readfile.php
|
|
|
4071 |
return header::askUserDownloadFile($conf["header"]["askUserDownloadFile"]);
|
|
|
4072 |
unset($conf["header"]["askUserDownloadFile"]);
|
| 226 |
liveuser |
4073 |
|
| 3 |
liveuser |
4074 |
}#function compileCppThenDownload end
|
|
|
4075 |
|
|
|
4076 |
/*
|
|
|
4077 |
#函式說明:
|
|
|
4078 |
#運用「nohup」與「&」來達到雖依序執行但不等待前一個指令執行完再執行下一個指令,形成在背景假多工的執行每個指令.
|
| 226 |
liveuser |
4079 |
#回傳結果:
|
| 3 |
liveuser |
4080 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
4081 |
#$result["error"],錯誤訊息陣列.
|
|
|
4082 |
#$result["function"],當前執行的函數名稱.
|
|
|
4083 |
#$result["cmd"],執行的指令內容.
|
|
|
4084 |
#$result["output"],爲執行完二元碼後的輸出陣列.
|
|
|
4085 |
#必填參數:
|
|
|
4086 |
#$conf["command"],字串陣列,要執行的指令
|
|
|
4087 |
$conf["command"]=array("");
|
|
|
4088 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
| 226 |
liveuser |
4089 |
$conf["fileArgu"]=__FILE__;
|
| 3 |
liveuser |
4090 |
#可省略參數:
|
|
|
4091 |
#$conf["argu"],二維陣列字串,每個指令搭配的參數,預設為空陣列.
|
|
|
4092 |
#$conf["argu"]=array(array());
|
|
|
4093 |
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
|
|
|
4094 |
#$conf["enablePrintDescription"]="true";
|
|
|
4095 |
#$conf["printDescription"],字串陣列,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容.
|
|
|
4096 |
#$conf["printDescription"]=array("");
|
|
|
4097 |
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".
|
|
|
4098 |
#$conf["escapeshellarg"]="false";
|
|
|
4099 |
#$conf["username"],字串陣列,每個指令要用什麼使用者來執行,預設為執行php使用者,該參數不適用於apache環境.
|
|
|
4100 |
#$conf["username"]=array("");
|
|
|
4101 |
#$conf["password"],字串陣列,與$conf["username"]搭配的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
|
|
4102 |
#$conf["password"]=array("");
|
|
|
4103 |
#$conf["web"],字串,腳本檔案是存放在web則為"true";若為檔案系統則為"false",預設為"false".
|
|
|
4104 |
#$conf["web"]=""
|
|
|
4105 |
#$conf["wait"],字串,執行時是否要等待前一個程序執行完,預設為"false"不等待,反之為"true"要等待.
|
|
|
4106 |
#$conf["wait"]="true";
|
|
|
4107 |
#參考資料:
|
|
|
4108 |
#無.
|
|
|
4109 |
#備註:
|
|
|
4110 |
#無.
|
|
|
4111 |
*/
|
|
|
4112 |
public static function multiThreadsShell(&$conf){
|
| 226 |
liveuser |
4113 |
|
| 3 |
liveuser |
4114 |
#初始化要回傳的結果
|
|
|
4115 |
$result=array();
|
| 226 |
liveuser |
4116 |
|
| 3 |
liveuser |
4117 |
#取得當前執行的函數名稱
|
|
|
4118 |
$result["function"]=__FUNCTION__;
|
| 226 |
liveuser |
4119 |
|
| 3 |
liveuser |
4120 |
#如果 $conf 不為陣列
|
|
|
4121 |
if(gettype($conf)!="array"){
|
| 226 |
liveuser |
4122 |
|
| 3 |
liveuser |
4123 |
#設置執行失敗
|
|
|
4124 |
$result["status"]="false";
|
| 226 |
liveuser |
4125 |
|
| 3 |
liveuser |
4126 |
#設置執行錯誤訊息
|
|
|
4127 |
$result["error"][]="\$conf變數須為陣列形態";
|
|
|
4128 |
|
|
|
4129 |
#如果傳入的參數為 null
|
|
|
4130 |
if($conf==null){
|
| 226 |
liveuser |
4131 |
|
| 3 |
liveuser |
4132 |
#設置執行錯誤訊息
|
|
|
4133 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
| 226 |
liveuser |
4134 |
|
| 3 |
liveuser |
4135 |
}#if end
|
|
|
4136 |
|
|
|
4137 |
#回傳結果
|
|
|
4138 |
return $result;
|
| 226 |
liveuser |
4139 |
|
| 3 |
liveuser |
4140 |
}#if end
|
| 226 |
liveuser |
4141 |
|
| 3 |
liveuser |
4142 |
#參數檢查
|
|
|
4143 |
#函式說明:
|
|
|
4144 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
4145 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
4146 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
4147 |
#$result["function"],當前執行的函式名稱.
|
|
|
4148 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
4149 |
#$result[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
4150 |
#$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
4151 |
#$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
4152 |
#必填寫的參數:
|
|
|
4153 |
#$conf["variableCheck::checkArguments"]["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
4154 |
$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
|
|
|
4155 |
#$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
4156 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("command","fileArgu");
|
| 226 |
liveuser |
4157 |
#$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
4158 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("array","string");
|
|
|
4159 |
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
4160 |
$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
4161 |
#可以省略的參數:
|
|
|
4162 |
#$conf["variableCheck::checkArguments"]["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true"。
|
|
|
4163 |
#$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
|
|
|
4164 |
#$conf["variableCheck::checkArguments"]["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
|
|
4165 |
$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("enablePrintDescription","printDescription","argu","escapeshellarg","username","password","web","wait");
|
| 226 |
liveuser |
4166 |
#$conf["variableCheck::checkArguments"]["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
4167 |
$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","array","array","string","array","array","string","string");
|
|
|
4168 |
#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,"null"代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
|
|
4169 |
$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("false",$conf["command"],null,"false",null,null,"false","false");
|
|
|
4170 |
#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
4171 |
#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"][]=array();
|
|
|
4172 |
$checkResult=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
|
|
|
4173 |
unset($conf["variableCheck::checkArguments"]);
|
| 226 |
liveuser |
4174 |
|
| 3 |
liveuser |
4175 |
#如果檢查參數失敗
|
|
|
4176 |
if($checkResult["status"]=="false"){
|
| 226 |
liveuser |
4177 |
|
| 3 |
liveuser |
4178 |
#設置執行失敗
|
|
|
4179 |
$result["status"]="false";
|
| 226 |
liveuser |
4180 |
|
| 3 |
liveuser |
4181 |
#設置執行錯誤
|
|
|
4182 |
$result["error"]=$checkResult;
|
| 226 |
liveuser |
4183 |
|
| 3 |
liveuser |
4184 |
#回傳結果
|
|
|
4185 |
return $result;
|
| 226 |
liveuser |
4186 |
|
| 3 |
liveuser |
4187 |
}#if end
|
| 226 |
liveuser |
4188 |
|
| 3 |
liveuser |
4189 |
#如果檢查參數不通過
|
|
|
4190 |
if($checkResult["passed"]=="false"){
|
| 226 |
liveuser |
4191 |
|
| 3 |
liveuser |
4192 |
#設置執行失敗
|
|
|
4193 |
$result["status"]="false";
|
| 226 |
liveuser |
4194 |
|
| 3 |
liveuser |
4195 |
#設置執行錯誤
|
|
|
4196 |
$result["error"]=$checkResult;
|
| 226 |
liveuser |
4197 |
|
| 3 |
liveuser |
4198 |
#回傳結果
|
|
|
4199 |
return $result;
|
| 226 |
liveuser |
4200 |
|
| 3 |
liveuser |
4201 |
}#if end
|
| 226 |
liveuser |
4202 |
|
| 3 |
liveuser |
4203 |
#初始化儲存要寫入到腳本檔案裡面的內容
|
|
|
4204 |
$cmdStr="";
|
| 226 |
liveuser |
4205 |
|
| 3 |
liveuser |
4206 |
#初始化儲存暫存輸出結果的檔案
|
| 226 |
liveuser |
4207 |
$scriptOutFileArray=array();
|
|
|
4208 |
|
| 3 |
liveuser |
4209 |
#針對每個 $conf["command"]
|
|
|
4210 |
for($i=0;$i<count($conf["command"]);$i++){
|
| 226 |
liveuser |
4211 |
|
| 3 |
liveuser |
4212 |
#如果有設定 $conf["enablePrintDescription"] 為 "true"
|
|
|
4213 |
if($conf["enablePrintDescription"]=="true"){
|
| 226 |
liveuser |
4214 |
|
| 3 |
liveuser |
4215 |
#如果存在 $conf["printDescription"][$i]
|
|
|
4216 |
if(isset($conf["printDescription"][$i])){
|
| 226 |
liveuser |
4217 |
|
| 3 |
liveuser |
4218 |
#儲存 印出描述與換行 的指令
|
|
|
4219 |
$cmdArray[]="echo ".$conf["printDescription"][$i]." \r\n";
|
| 226 |
liveuser |
4220 |
|
| 3 |
liveuser |
4221 |
}#if end
|
| 226 |
liveuser |
4222 |
|
| 3 |
liveuser |
4223 |
#如果存在 $conf["argu"]
|
|
|
4224 |
if(isset($conf["argu"])){
|
| 226 |
liveuser |
4225 |
|
| 3 |
liveuser |
4226 |
#如果存在 $conf["argu"][$i]
|
|
|
4227 |
if(isset($conf["argu"][$i])){
|
| 226 |
liveuser |
4228 |
|
| 3 |
liveuser |
4229 |
#如果 $conf["argu"][$i] 形態為 "array"
|
|
|
4230 |
if(gettype($conf["argu"][$i])=="array"){
|
| 226 |
liveuser |
4231 |
|
|
|
4232 |
#參數 $conf["argu"][$i] 有幾個元素就執行幾次
|
| 3 |
liveuser |
4233 |
for($j=0;$j<count($conf["argu"][$i]);$j++){
|
| 226 |
liveuser |
4234 |
|
| 3 |
liveuser |
4235 |
#如果 $conf["escapeshellarg"] 為 "true"
|
|
|
4236 |
if($conf["escapeshellarg"]=="true"){
|
| 226 |
liveuser |
4237 |
|
| 3 |
liveuser |
4238 |
#過濾參數
|
|
|
4239 |
$conf["argu"][$i]=escapeshellarg($conf["argu"][$i]);
|
| 226 |
liveuser |
4240 |
|
| 3 |
liveuser |
4241 |
}#if end
|
| 226 |
liveuser |
4242 |
|
| 3 |
liveuser |
4243 |
#附加參數
|
|
|
4244 |
$conf["command"][$i]=$conf["command"][$i]." ".$conf["argu"][$i];
|
| 226 |
liveuser |
4245 |
|
| 3 |
liveuser |
4246 |
}#for end
|
| 226 |
liveuser |
4247 |
|
| 3 |
liveuser |
4248 |
}#if end
|
| 226 |
liveuser |
4249 |
|
| 3 |
liveuser |
4250 |
}#if end
|
| 226 |
liveuser |
4251 |
|
| 3 |
liveuser |
4252 |
}#if end
|
| 226 |
liveuser |
4253 |
|
| 3 |
liveuser |
4254 |
}#if end
|
| 226 |
liveuser |
4255 |
|
| 3 |
liveuser |
4256 |
#如果 $conf["username"] 存在
|
|
|
4257 |
if(isset($conf["username"])){
|
| 226 |
liveuser |
4258 |
|
| 3 |
liveuser |
4259 |
#如果 $conf["username"][$i] 存在
|
|
|
4260 |
if(isset($conf["username"][$i])){
|
| 226 |
liveuser |
4261 |
|
| 3 |
liveuser |
4262 |
#如果 $conf["password"] 存在
|
|
|
4263 |
if(isset($conf["password"])){
|
| 226 |
liveuser |
4264 |
|
| 3 |
liveuser |
4265 |
#如果 $conf["password"][$i] 存在
|
|
|
4266 |
if(isset($conf["password"][$i])){
|
| 226 |
liveuser |
4267 |
|
| 3 |
liveuser |
4268 |
#加上使用特定使用者身份的語法與驗證的密碼
|
|
|
4269 |
$conf["command"][$i]="echo ".$conf["password"][$i]." | su ".$conf["username"][$i]." -c'".$conf["command"][$i]."'";
|
| 226 |
liveuser |
4270 |
|
| 3 |
liveuser |
4271 |
}#if end
|
| 226 |
liveuser |
4272 |
|
| 3 |
liveuser |
4273 |
}#if end
|
| 226 |
liveuser |
4274 |
|
| 3 |
liveuser |
4275 |
#反之
|
|
|
4276 |
else{
|
| 226 |
liveuser |
4277 |
|
| 3 |
liveuser |
4278 |
#加上使用特定使用者身份的語法
|
|
|
4279 |
$conf["command"][$i]="su ".$conf["username"][$i]." -c'".$conf["command"][$i]."'";
|
| 226 |
liveuser |
4280 |
|
| 3 |
liveuser |
4281 |
}#else end
|
| 226 |
liveuser |
4282 |
|
| 3 |
liveuser |
4283 |
}#if end
|
| 226 |
liveuser |
4284 |
|
| 3 |
liveuser |
4285 |
}#if end
|
| 226 |
liveuser |
4286 |
|
| 3 |
liveuser |
4287 |
#如果不等待前一個指令
|
|
|
4288 |
if($conf["wait"]==="false"){
|
| 226 |
liveuser |
4289 |
|
| 3 |
liveuser |
4290 |
#加上 & 到 $conf["command"][$i]
|
|
|
4291 |
$conf["command"][$i]=$conf["command"][$i]." &".PHP_EOL;
|
| 226 |
liveuser |
4292 |
|
| 3 |
liveuser |
4293 |
}#if end
|
| 226 |
liveuser |
4294 |
|
| 3 |
liveuser |
4295 |
#反之要等待前一個指令
|
|
|
4296 |
else{
|
| 226 |
liveuser |
4297 |
|
| 3 |
liveuser |
4298 |
#僅加上換行
|
|
|
4299 |
$conf["command"][$i]=$conf["command"][$i]." ".PHP_EOL;
|
| 226 |
liveuser |
4300 |
|
| 3 |
liveuser |
4301 |
}#else end
|
| 226 |
liveuser |
4302 |
|
| 3 |
liveuser |
4303 |
#儲存要執行的指令
|
|
|
4304 |
$cmdStr=$cmdStr.$conf["command"][$i];
|
| 226 |
liveuser |
4305 |
|
| 3 |
liveuser |
4306 |
}#for end
|
| 226 |
liveuser |
4307 |
|
| 3 |
liveuser |
4308 |
#debug
|
| 226 |
liveuser |
4309 |
#var_dump($cmdStr);
|
|
|
4310 |
|
| 3 |
liveuser |
4311 |
#建立用來儲存指令的腳本檔案
|
|
|
4312 |
#涵式說明:
|
|
|
4313 |
#將字串寫入到檔案
|
|
|
4314 |
#回傳的結果:
|
|
|
4315 |
#$result["status"],"true"表示檔案寫入成功,"false"表示檔案寫入失敗.
|
|
|
4316 |
#$result["error"],錯誤訊息陣列.
|
|
|
4317 |
#$result["function"],當前執行的函數名稱.
|
|
|
4318 |
#$result["fileInfo"],實際上寫入的檔案資訊陣列.
|
|
|
4319 |
#$result["fileInfo"]["createdFileName"],建立好的檔案名稱.
|
|
|
4320 |
#$result["fileInfo"]["createdFilePath"],檔案建立的路徑.
|
|
|
4321 |
#$result["fileInfo"]["createdFilePathAndName"].建立好的檔案名稱與路徑.
|
|
|
4322 |
#必填參數::
|
|
|
4323 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
|
|
|
4324 |
$conf["fileAccess::writeTextIntoFile"]["fileArgu"]=$conf["fileArgu"];
|
|
|
4325 |
#可省略參數:
|
|
|
4326 |
#$conf["fileName"],字串,爲要編輯的檔案名稱,預設為隨機產生的檔案名稱.
|
|
|
4327 |
#$conf["fileName"]="";
|
|
|
4328 |
#$conf["inputString"],字串,爲要寫入到裏面的內容,若要每筆資料寫入後換行,則可以在字串內容後面加上 \r\n 即可,預設為"".
|
|
|
4329 |
$conf["fileAccess::writeTextIntoFile"]["inputString"]=$cmdStr;#爲要寫入到裏面的內容,若要每筆資料寫入後換行,則可以在字串內容後面加上 \r\n 即可.
|
|
|
4330 |
#$conf["writeMethod"],字串,爲檔案撰寫的方式,可省略,是複寫'a'還是,重新寫入'w',預設爲'w',重新寫入.
|
|
|
4331 |
#$conf["writeMethod"]="a";
|
|
|
4332 |
#$conf["checkRepeat"],字串,"true"代表建立檔案之前要先檢查檔案是否存在,若存在則在原名稱後面加上從(1)開始的編號.
|
|
|
4333 |
#$conf["checkRepeat"]="";
|
|
|
4334 |
#$conf["filenameExtensionStartPoint"],字串,檔案的副檔名是從倒數第幾個小數點(dot)開始,預設為"1",最後一個小數點,必須與$conf["checkRepeat"]搭配才會生效.
|
|
|
4335 |
#$conf["filenameExtensionStartPoint"]="";
|
|
|
4336 |
#$conf["repeatNameRule"],字串,遇到相同名稱的檔案要如何加上識別的編號,編號用「\$i」表示,預設為"(\$i)",必須與$conf["checkRepeat"]搭配才會生效.
|
|
|
4337 |
#$conf["repeatNameRule"]="";
|
|
|
4338 |
#$conf["web"],檔案是否位於網站上"true",若是在檔案系統則為"false",預設為"true".
|
|
|
4339 |
$conf["fileAccess::writeTextIntoFile"]["web"]="false";
|
|
|
4340 |
$createShellScriptFile=fileAccess::writeTextIntoFile($conf["fileAccess::writeTextIntoFile"]);
|
|
|
4341 |
unset($conf["fileAccess::writeTextIntoFile"]);
|
| 226 |
liveuser |
4342 |
|
| 3 |
liveuser |
4343 |
#debug
|
|
|
4344 |
#var_dump($createShellScriptFile);
|
|
|
4345 |
#exit;
|
| 226 |
liveuser |
4346 |
|
| 3 |
liveuser |
4347 |
#如果建立腳本檔案失敗
|
|
|
4348 |
if($createShellScriptFile["status"]=="false"){
|
| 226 |
liveuser |
4349 |
|
| 3 |
liveuser |
4350 |
#設置執行不正常
|
|
|
4351 |
$result["status"]="false";
|
| 226 |
liveuser |
4352 |
|
| 3 |
liveuser |
4353 |
#設置執行錯誤
|
|
|
4354 |
$result["error"]=$createShellScriptFile;
|
| 226 |
liveuser |
4355 |
|
| 3 |
liveuser |
4356 |
#回傳結果
|
|
|
4357 |
return $result;
|
| 226 |
liveuser |
4358 |
|
| 3 |
liveuser |
4359 |
}#if end
|
| 226 |
liveuser |
4360 |
|
| 3 |
liveuser |
4361 |
#移除腳本的指令
|
|
|
4362 |
$delScript="rm ".$createShellScriptFile["fileInfo"]["createdFilePathAndName"].";";
|
| 226 |
liveuser |
4363 |
|
| 3 |
liveuser |
4364 |
#附加寫入移除腳本的指令
|
|
|
4365 |
#回傳的結果:
|
|
|
4366 |
#$result["status"],"true"表示檔案寫入成功,"false"表示檔案寫入失敗.
|
|
|
4367 |
#$result["error"],錯誤訊息陣列.
|
|
|
4368 |
#$result["function"],當前執行的函數名稱.
|
|
|
4369 |
#$result["fileInfo"],實際上寫入的檔案資訊陣列.
|
|
|
4370 |
#$result["fileInfo"]["createdFileName"],建立好的檔案名稱.
|
|
|
4371 |
#$result["fileInfo"]["createdFilePath"],檔案建立的路徑.
|
|
|
4372 |
#$result["fileInfo"]["createdFilePathAndName"].建立好的檔案名稱與路徑.
|
|
|
4373 |
#必填參數::
|
|
|
4374 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
|
|
|
4375 |
$conf["fileAccess::writeTextIntoFile"]["fileArgu"]=$conf["fileArgu"];
|
|
|
4376 |
#可省略參數:
|
|
|
4377 |
$conf["fileAccess::writeTextIntoFile"]["fileName"]=$createShellScriptFile["fileInfo"]["createdFilePathAndName"];#爲要編輯的檔案名稱
|
|
|
4378 |
$conf["fileAccess::writeTextIntoFile"]["inputString"]=$delScript;#爲要寫入到裏面的內容,若要每筆資料寫入後換行,則可以在字串內容後面加上 \r\n 即可.
|
|
|
4379 |
#$conf["writeMethod"],字串,爲檔案撰寫的方式,可省略,是複寫'a'還是,重新寫入'w',預設爲'w',重新寫入.
|
|
|
4380 |
$conf["fileAccess::writeTextIntoFile"]["writeMethod"]="a";
|
|
|
4381 |
#$conf["checkRepeat"],字串,"true"代表建立檔案之前要先檢查檔案是否存在,若存在則在原名稱後面加上從(1)開始的編號.
|
|
|
4382 |
#$conf["checkRepeat"]="";
|
|
|
4383 |
#$conf["filenameExtensionStartPoint"],字串,檔案的副檔名是從倒數第幾個小數點(dot)開始,預設為"1",最後一個小數點,必須與$conf["checkRepeat"]搭配才會生效.
|
|
|
4384 |
#$conf["filenameExtensionStartPoint"]="";
|
|
|
4385 |
#$conf["repeatNameRule"],字串,遇到相同名稱的檔案要如何加上識別的編號,編號用「\$i」表示,預設為"(\$i)",必須與$conf["checkRepeat"]搭配才會生效.
|
|
|
4386 |
#$conf["repeatNameRule"]="";
|
|
|
4387 |
#$conf["web"],檔案是否位於網站上"true",若是在檔案系統則為"false",預設為"true".
|
|
|
4388 |
$conf["fileAccess::writeTextIntoFile"]["web"]="false";
|
|
|
4389 |
$createShellScriptFile=fileAccess::writeTextIntoFile($conf["fileAccess::writeTextIntoFile"]);
|
|
|
4390 |
unset($conf["fileAccess::writeTextIntoFile"]);
|
| 226 |
liveuser |
4391 |
|
| 3 |
liveuser |
4392 |
#如果建立腳本檔案失敗
|
|
|
4393 |
if($createShellScriptFile["status"]=="false"){
|
| 226 |
liveuser |
4394 |
|
| 3 |
liveuser |
4395 |
#設置執行不正常
|
|
|
4396 |
$result["status"]="false";
|
| 226 |
liveuser |
4397 |
|
| 3 |
liveuser |
4398 |
#設置執行錯誤
|
|
|
4399 |
$result["error"]=$createShellScriptFile;
|
| 226 |
liveuser |
4400 |
|
| 3 |
liveuser |
4401 |
#回傳結果
|
|
|
4402 |
return $result;
|
| 226 |
liveuser |
4403 |
|
| 3 |
liveuser |
4404 |
}#if end
|
| 226 |
liveuser |
4405 |
|
| 3 |
liveuser |
4406 |
#debug
|
|
|
4407 |
#exec("echo '".$createShellScriptFile["fileInfo"]["createdFilePathAndName"]."' > /var/www/html/latest/tmp/debug1.log",$resultArray,$execStatus);
|
|
|
4408 |
#exec("cat '".$createShellScriptFile["fileInfo"]["createdFilePathAndName"]."' > /var/www/html/latest/tmp/debug2.log",$resultArray,$execStatus);
|
| 226 |
liveuser |
4409 |
|
| 3 |
liveuser |
4410 |
#執行腳本程式,將輸出結果儲存在 $resultArray 陣列裏面
|
|
|
4411 |
#exec("sh ".$createShellScriptFile["fileInfo"]["createdFilePathAndName"]." > /dev/null 2>&1 &",$resultArray,$execStatus);
|
| 226 |
liveuser |
4412 |
|
| 3 |
liveuser |
4413 |
#函式說明:
|
|
|
4414 |
#呼叫shell執行系統命令,並取得回傳的內容.
|
|
|
4415 |
#回傳結果:
|
|
|
4416 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
4417 |
#$result["error"],錯誤訊息陣列.
|
|
|
4418 |
#$result["function"],當前執行的函數名稱.
|
|
|
4419 |
#$result["argu"],使用的參數.
|
|
|
4420 |
#$result["cmd"],執行的指令內容.
|
|
|
4421 |
#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
4422 |
#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
|
|
|
4423 |
#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
4424 |
#$result["running"],是否還在執行.
|
|
|
4425 |
#$result["pid"],pid.
|
|
|
4426 |
#$result["statusCode"],執行結束後的代碼.
|
|
|
4427 |
#必填參數:
|
|
|
4428 |
#$conf["command"],字串,要執行的指令與.
|
|
|
4429 |
$conf["external::callShell"]["command"]="sh";
|
|
|
4430 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
|
|
4431 |
$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];
|
|
|
4432 |
#可省略參數:
|
|
|
4433 |
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
|
|
|
4434 |
$conf["external::callShell"]["argu"]=array($createShellScriptFile["fileInfo"]["createdFilePathAndName"],">","/dev/null","2>&1","&");
|
|
|
4435 |
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
|
|
|
4436 |
#$conf["arguIsAddr"]=array();
|
|
|
4437 |
#$conf["plainArgu"],字串陣列,哪幾個參數不要加上"",元算若為"true"則代表不用包;反之"false"則代表要包.
|
|
|
4438 |
#$conf["external::callShell"]["plainArgu"]=array("false","true","true","true","true");
|
|
|
4439 |
#$conf["useApostrophe"],字串陣列,如果有需要包住,則用「'」,而非「"」處理.前者為"true";後者為"false".
|
|
|
4440 |
#$conf["external::callShell"]["useApostrophe"]=array("true","false","false","false","false");
|
|
|
4441 |
#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
|
|
|
4442 |
#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
|
|
|
4443 |
#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
|
|
|
4444 |
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
|
|
|
4445 |
#$conf["enablePrintDescription"]="true";
|
|
|
4446 |
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
|
|
|
4447 |
#$conf["printDescription"]="";
|
|
|
4448 |
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".
|
|
|
4449 |
$conf["external::callShell"]["escapeshellarg"]="true";
|
|
|
4450 |
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
|
|
|
4451 |
#$conf["username"]="";
|
|
|
4452 |
#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
|
|
4453 |
#$conf["password"]="";
|
|
|
4454 |
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
|
|
|
4455 |
#$conf["useScript"]="";
|
|
|
4456 |
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
|
|
|
4457 |
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
|
|
|
4458 |
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
|
|
|
4459 |
#$conf["inBackGround"]="";
|
|
|
4460 |
#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
|
|
|
4461 |
#$conf["getErr"]="false";
|
|
|
4462 |
#參考資料:
|
|
|
4463 |
#exec=>http://php.net/manual/en/function.exec.php
|
|
|
4464 |
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
|
|
|
4465 |
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
|
|
|
4466 |
#備註:
|
|
|
4467 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
|
|
|
4468 |
$callShell=external::callShell($conf["external::callShell"]);
|
|
|
4469 |
unset($conf["external::callShell"]);
|
| 226 |
liveuser |
4470 |
|
| 3 |
liveuser |
4471 |
$fsock=fopen("/var/www/html/latest/tmp/debug","w");
|
|
|
4472 |
fwrite($fsock,print_r($callShell,true));
|
|
|
4473 |
fclose($fsock);
|
| 226 |
liveuser |
4474 |
|
| 3 |
liveuser |
4475 |
#如果執行失敗
|
|
|
4476 |
if($callShell["status"]==="false"){
|
| 226 |
liveuser |
4477 |
|
| 3 |
liveuser |
4478 |
#設置執行不正常
|
|
|
4479 |
$result["status"]="false";
|
| 226 |
liveuser |
4480 |
|
| 3 |
liveuser |
4481 |
#設置執行錯誤
|
|
|
4482 |
$result["error"]=$callShell;
|
| 226 |
liveuser |
4483 |
|
| 3 |
liveuser |
4484 |
#回傳結果
|
|
|
4485 |
return $result;
|
| 226 |
liveuser |
4486 |
|
| 3 |
liveuser |
4487 |
}#if end
|
| 226 |
liveuser |
4488 |
|
| 3 |
liveuser |
4489 |
#設置執行正常
|
|
|
4490 |
$result["status"]="true";
|
| 226 |
liveuser |
4491 |
|
| 3 |
liveuser |
4492 |
#回傳結果
|
|
|
4493 |
return $result;
|
| 226 |
liveuser |
4494 |
|
| 3 |
liveuser |
4495 |
}#function multiThreadsShell end
|
| 226 |
liveuser |
4496 |
|
| 3 |
liveuser |
4497 |
/*
|
|
|
4498 |
#函式說明:
|
|
|
4499 |
#呼叫shell執行系統命令,中止執行中的pid.
|
|
|
4500 |
#回傳結果:
|
|
|
4501 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
4502 |
#$result["error"],錯誤訊息陣列.
|
|
|
4503 |
#$result["function"],當前執行的函數名稱.
|
|
|
4504 |
#$result["warning"],警告訊息陣列.
|
|
|
4505 |
#$result["cmd"],執行的指令內容.
|
|
|
4506 |
#$result["output"],爲執行完二元碼後的輸出陣列.
|
|
|
4507 |
#$result["runing"],程式是否還在執行.
|
|
|
4508 |
#必填參數:
|
|
|
4509 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
| 226 |
liveuser |
4510 |
$conf["fileArgu"]=__FILE__;
|
| 3 |
liveuser |
4511 |
#$conf["pid"],字串,執行序的pid.
|
|
|
4512 |
$conf["pid"]="";
|
|
|
4513 |
#$conf["cmd"],字串,要中止的指令關鍵字.
|
| 226 |
liveuser |
4514 |
$conf["cmd"]="";
|
| 3 |
liveuser |
4515 |
#可省略參數:
|
|
|
4516 |
#無.
|
|
|
4517 |
#參考資料:
|
|
|
4518 |
#http://php.net/manual/en/function.exec.php
|
|
|
4519 |
#備註:
|
|
|
4520 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,透過su使用root身份可能會被selinux阻擋.
|
| 130 |
liveuser |
4521 |
*/
|
| 3 |
liveuser |
4522 |
public static function killProcess(&$conf){
|
| 226 |
liveuser |
4523 |
|
| 3 |
liveuser |
4524 |
#初始化要回傳的結果
|
|
|
4525 |
$result=array();
|
|
|
4526 |
|
|
|
4527 |
#取得當前執行的函數名稱
|
|
|
4528 |
$result["function"]=__FUNCTION__;
|
|
|
4529 |
|
|
|
4530 |
#如果沒有參數
|
|
|
4531 |
if(func_num_args()==0){
|
| 226 |
liveuser |
4532 |
|
| 3 |
liveuser |
4533 |
#設置執行失敗
|
|
|
4534 |
$result["status"]="false";
|
| 226 |
liveuser |
4535 |
|
| 3 |
liveuser |
4536 |
#設置執行錯誤訊息
|
|
|
4537 |
$result["error"]="函數".$result["function"]."需要參數";
|
| 226 |
liveuser |
4538 |
|
| 3 |
liveuser |
4539 |
#回傳結果
|
|
|
4540 |
return $result;
|
| 226 |
liveuser |
4541 |
|
| 3 |
liveuser |
4542 |
}#if end
|
| 226 |
liveuser |
4543 |
|
| 3 |
liveuser |
4544 |
#取得參數
|
|
|
4545 |
$result["argu"]=$conf;
|
|
|
4546 |
|
|
|
4547 |
#如果 $conf 不為陣列
|
|
|
4548 |
if(gettype($conf)!=="array"){
|
| 226 |
liveuser |
4549 |
|
| 3 |
liveuser |
4550 |
#設置執行失敗
|
|
|
4551 |
$result["status"]="false";
|
| 226 |
liveuser |
4552 |
|
| 3 |
liveuser |
4553 |
#設置執行錯誤訊息
|
|
|
4554 |
$result["error"][]="\$conf變數須為陣列形態";
|
| 226 |
liveuser |
4555 |
|
| 3 |
liveuser |
4556 |
#如果傳入的參數為 null
|
|
|
4557 |
if($conf===null){
|
| 226 |
liveuser |
4558 |
|
| 3 |
liveuser |
4559 |
#設置執行錯誤訊息
|
|
|
4560 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
| 226 |
liveuser |
4561 |
|
| 3 |
liveuser |
4562 |
}#if end
|
|
|
4563 |
|
|
|
4564 |
#回傳結果
|
|
|
4565 |
return $result;
|
| 226 |
liveuser |
4566 |
|
| 3 |
liveuser |
4567 |
}#if end
|
| 226 |
liveuser |
4568 |
|
| 3 |
liveuser |
4569 |
#檢查參數
|
|
|
4570 |
#函式說明:
|
|
|
4571 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
4572 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
4573 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
4574 |
#$result["function"],當前執行的函式名稱.
|
|
|
4575 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
4576 |
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
4577 |
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
4578 |
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
4579 |
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
|
|
|
4580 |
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
|
|
|
4581 |
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
|
|
|
4582 |
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
|
|
|
4583 |
#必填寫的參數:
|
|
|
4584 |
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
4585 |
$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
|
|
|
4586 |
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
4587 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("pid","cmd","fileArgu");
|
|
|
4588 |
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
|
|
|
4589 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string","string");
|
|
|
4590 |
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
4591 |
$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
4592 |
#可以省略的參數:
|
|
|
4593 |
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
|
|
|
4594 |
#$conf["canBeEmptyString"]="false";
|
|
|
4595 |
#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
|
|
|
4596 |
#$conf["canNotBeEmpty"]=array();
|
|
|
4597 |
#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
|
|
|
4598 |
#$conf["canBeEmpty"]=array();
|
|
|
4599 |
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
|
|
|
4600 |
#$conf["skipableVariableCanNotBeEmpty"]=array();
|
|
|
4601 |
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
|
|
4602 |
#$conf["skipableVariableName"]=array();
|
| 226 |
liveuser |
4603 |
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
4604 |
#$conf["skipableVariableType"]=array();
|
|
|
4605 |
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
|
|
4606 |
#$conf["skipableVarDefaultValue"]=array("");
|
|
|
4607 |
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
4608 |
#$conf["arrayCountEqualCheck"][]=array();
|
|
|
4609 |
#參考資料來源:
|
|
|
4610 |
#array_keys=>http://php.net/manual/en/function.array-keys.php
|
|
|
4611 |
#建議:
|
|
|
4612 |
#增加可省略參數全部不能為空字串或空陣列的參數功能.
|
|
|
4613 |
$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
|
|
|
4614 |
unset($conf["variableCheck::checkArguments"]);
|
| 226 |
liveuser |
4615 |
|
| 3 |
liveuser |
4616 |
#如果檢查參數失敗
|
|
|
4617 |
if($checkArguments["status"]==="false"){
|
| 226 |
liveuser |
4618 |
|
| 3 |
liveuser |
4619 |
#設置執行失敗
|
|
|
4620 |
$result["status"]="false";
|
| 226 |
liveuser |
4621 |
|
| 3 |
liveuser |
4622 |
#設置執行錯誤訊息
|
|
|
4623 |
$result["error"]=$checkArguments;
|
| 226 |
liveuser |
4624 |
|
| 3 |
liveuser |
4625 |
#回傳結果
|
|
|
4626 |
return $result;
|
| 226 |
liveuser |
4627 |
|
| 3 |
liveuser |
4628 |
}#if end
|
| 226 |
liveuser |
4629 |
|
| 3 |
liveuser |
4630 |
#如果參數檢查不過
|
|
|
4631 |
if($checkArguments["passed"]==="false"){
|
| 226 |
liveuser |
4632 |
|
| 3 |
liveuser |
4633 |
#設置執行失敗
|
|
|
4634 |
$result["status"]="false";
|
| 226 |
liveuser |
4635 |
|
| 3 |
liveuser |
4636 |
#設置執行錯誤訊息
|
|
|
4637 |
$result["error"]=$checkArguments;
|
| 226 |
liveuser |
4638 |
|
| 3 |
liveuser |
4639 |
#回傳結果
|
|
|
4640 |
return $result;
|
| 226 |
liveuser |
4641 |
|
| 3 |
liveuser |
4642 |
}#if end
|
| 226 |
liveuser |
4643 |
|
| 3 |
liveuser |
4644 |
#函式說明:
|
| 226 |
liveuser |
4645 |
#檢查程序是否還在執行
|
| 3 |
liveuser |
4646 |
#回傳結果
|
|
|
4647 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
4648 |
#$result["error"],錯誤訊息陣列.
|
| 226 |
liveuser |
4649 |
#$result["function"],當前執行的函數名稱.
|
| 3 |
liveuser |
4650 |
#$result["exist"],程序是否存在,"true"代表存在,"false"代表不存在.
|
|
|
4651 |
#必填參數:
|
|
|
4652 |
#$conf["pid"],字串,程序的id.
|
|
|
4653 |
$conf["external::processStatus"]["pid"]=$conf["pid"];
|
|
|
4654 |
#$conf["cmd"],字串,程序的指令.
|
|
|
4655 |
$conf["external::processStatus"]["cmd"]=$conf["cmd"];
|
|
|
4656 |
#$conf["fileArgu"],字串,__FILE__的內容,__FILE__的內容.
|
|
|
4657 |
$conf["external::processStatus"]["fileArgu"]=$conf["fileArgu"];
|
|
|
4658 |
$processStatus=external::processStatus($conf["external::processStatus"]);
|
|
|
4659 |
unset($conf["external::processStatus"]);
|
| 226 |
liveuser |
4660 |
|
| 3 |
liveuser |
4661 |
#如果檢查程序是否還在執行失敗
|
|
|
4662 |
if($processStatus["status"]==="false"){
|
| 226 |
liveuser |
4663 |
|
| 3 |
liveuser |
4664 |
#設置執行失敗
|
|
|
4665 |
$result["status"]="false";
|
| 226 |
liveuser |
4666 |
|
| 3 |
liveuser |
4667 |
#設置執行錯誤訊息
|
|
|
4668 |
$result["error"]=$processStatus;
|
| 226 |
liveuser |
4669 |
|
| 3 |
liveuser |
4670 |
#回傳結果
|
|
|
4671 |
return $result;
|
| 226 |
liveuser |
4672 |
|
| 3 |
liveuser |
4673 |
}#if end
|
| 226 |
liveuser |
4674 |
|
| 3 |
liveuser |
4675 |
#如果程序沒有在執行
|
|
|
4676 |
if($processStatus["exist"]==="false"){
|
| 226 |
liveuser |
4677 |
|
| 3 |
liveuser |
4678 |
#設置執行成功
|
|
|
4679 |
$result["status"]="true";
|
| 226 |
liveuser |
4680 |
|
| 3 |
liveuser |
4681 |
#設置警告訊息
|
|
|
4682 |
$result["warning"][]="要中止的程序不存在";
|
| 226 |
liveuser |
4683 |
|
| 3 |
liveuser |
4684 |
#回傳結果
|
|
|
4685 |
return $result;
|
| 226 |
liveuser |
4686 |
|
| 3 |
liveuser |
4687 |
}#if end
|
| 226 |
liveuser |
4688 |
|
|
|
4689 |
#中止程式
|
| 3 |
liveuser |
4690 |
#函式說明:
|
|
|
4691 |
#呼叫shell執行系統命令,並取得回傳的內容.
|
|
|
4692 |
#回傳結果:
|
|
|
4693 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
4694 |
#$result["error"],錯誤訊息陣列.
|
|
|
4695 |
#$result["function"],當前執行的函數名稱.
|
|
|
4696 |
#$result["cmd"],執行的指令內容.
|
|
|
4697 |
#$result["output"],爲執行完二元碼後的輸出陣列.
|
|
|
4698 |
#必填參數:
|
|
|
4699 |
#$conf["command"],字串,要執行的指令與.
|
|
|
4700 |
$conf["external::callShell"]["command"]="kill";
|
|
|
4701 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
| 226 |
liveuser |
4702 |
$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];
|
| 3 |
liveuser |
4703 |
#可省略參數:
|
|
|
4704 |
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
|
|
|
4705 |
$conf["external::callShell"]["argu"]=array($conf["pid"]);
|
|
|
4706 |
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
|
|
|
4707 |
#$conf["arguIsAddr"]=array();
|
|
|
4708 |
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
|
|
|
4709 |
#$conf["enablePrintDescription"]="true";
|
|
|
4710 |
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容.
|
|
|
4711 |
#$conf["printDescription"]="";
|
|
|
4712 |
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".
|
|
|
4713 |
$conf["external::callShell"]["escapeshellarg"]="true";
|
|
|
4714 |
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
|
|
|
4715 |
#$conf["username"]="";
|
|
|
4716 |
#$conf["password"],字串,與$conf["username"]搭配的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
|
|
4717 |
#$conf["password"]="";
|
|
|
4718 |
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
|
|
|
4719 |
#$conf["useScript"]="";
|
|
|
4720 |
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
|
|
|
4721 |
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
|
|
|
4722 |
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
|
|
|
4723 |
#$conf["inBackGround"]="";
|
|
|
4724 |
#備註:
|
|
|
4725 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
|
|
|
4726 |
#參考資料:
|
|
|
4727 |
#exec=>http://php.net/manual/en/function.exec.php
|
|
|
4728 |
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
|
|
|
4729 |
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
|
|
|
4730 |
$callShell=external::callShell($conf["external::callShell"]);
|
|
|
4731 |
unset($conf["external::callShell"]);
|
| 226 |
liveuser |
4732 |
|
| 3 |
liveuser |
4733 |
#如果執行程式失敗
|
|
|
4734 |
if($callShell["status"]==="false"){
|
| 226 |
liveuser |
4735 |
|
| 3 |
liveuser |
4736 |
#設置執行失敗
|
|
|
4737 |
$result["status"]="false";
|
| 226 |
liveuser |
4738 |
|
| 3 |
liveuser |
4739 |
#設置執行錯誤訊息
|
|
|
4740 |
$result["error"]=$callShell;
|
| 226 |
liveuser |
4741 |
|
| 3 |
liveuser |
4742 |
#回傳結果
|
|
|
4743 |
return $result;
|
| 226 |
liveuser |
4744 |
|
| 3 |
liveuser |
4745 |
}#if end
|
| 226 |
liveuser |
4746 |
|
| 3 |
liveuser |
4747 |
#檢查程序是否已經中止
|
|
|
4748 |
#函式說明:
|
| 226 |
liveuser |
4749 |
#檢查程序是否還在執行
|
| 3 |
liveuser |
4750 |
#回傳結果
|
|
|
4751 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
4752 |
#$result["error"],錯誤訊息陣列.
|
| 226 |
liveuser |
4753 |
#$result["function"],當前執行的函數名稱.
|
| 3 |
liveuser |
4754 |
#$result["exist"],程序是否存在,"true"代表存在,"false"代表不存在.
|
|
|
4755 |
#必填參數:
|
|
|
4756 |
#$conf["pid"],字串,程序的id.
|
|
|
4757 |
$conf["external::processStatus"]["pid"]=$conf["pid"];
|
|
|
4758 |
#$conf["cmd"],字串,程序的指令.
|
|
|
4759 |
$conf["external::processStatus"]["cmd"]=$conf["cmd"];
|
|
|
4760 |
#$conf["fileArgu"],字串,__FILE__的內容,__FILE__的內容.
|
|
|
4761 |
$conf["external::processStatus"]["fileArgu"]=$conf["fileArgu"];
|
|
|
4762 |
$processStatus=external::processStatus($conf["external::processStatus"]);
|
|
|
4763 |
unset($conf["external::processStatus"]);
|
| 226 |
liveuser |
4764 |
|
| 3 |
liveuser |
4765 |
#如果檢查程序是否還在執行失敗
|
|
|
4766 |
if($processStatus["status"]==="false"){
|
| 226 |
liveuser |
4767 |
|
| 3 |
liveuser |
4768 |
#設置執行失敗
|
|
|
4769 |
$result["status"]="false";
|
| 226 |
liveuser |
4770 |
|
| 3 |
liveuser |
4771 |
#設置執行錯誤訊息
|
|
|
4772 |
$result["error"]=$processStatus;
|
| 226 |
liveuser |
4773 |
|
| 3 |
liveuser |
4774 |
#回傳結果
|
|
|
4775 |
return $result;
|
| 226 |
liveuser |
4776 |
|
| 3 |
liveuser |
4777 |
}#if end
|
| 226 |
liveuser |
4778 |
|
| 3 |
liveuser |
4779 |
#如果程序沒有在執行
|
|
|
4780 |
if($processStatus["exist"]==="false"){
|
| 226 |
liveuser |
4781 |
|
| 3 |
liveuser |
4782 |
#設置執行成功
|
| 226 |
liveuser |
4783 |
$result["status"]="true";
|
|
|
4784 |
|
| 3 |
liveuser |
4785 |
#回傳結果
|
|
|
4786 |
return $result;
|
| 226 |
liveuser |
4787 |
|
| 3 |
liveuser |
4788 |
}#if end
|
| 226 |
liveuser |
4789 |
|
| 3 |
liveuser |
4790 |
#反之
|
|
|
4791 |
else{
|
| 226 |
liveuser |
4792 |
|
| 3 |
liveuser |
4793 |
#再度嘗試中止程式
|
|
|
4794 |
return external::killProcess($conf);
|
|
|
4795 |
unset($conf);
|
| 226 |
liveuser |
4796 |
|
|
|
4797 |
}#else end
|
|
|
4798 |
|
| 3 |
liveuser |
4799 |
}#function killProcess end
|
| 226 |
liveuser |
4800 |
|
| 3 |
liveuser |
4801 |
/*
|
|
|
4802 |
#函式說明:
|
| 226 |
liveuser |
4803 |
#檢查目標程式是否執行超過時間,將之中止.
|
| 3 |
liveuser |
4804 |
#回傳結果
|
|
|
4805 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
4806 |
#$result["error"],錯誤訊息陣列.
|
| 226 |
liveuser |
4807 |
#$result["function"],當前執行的函數名稱.
|
| 3 |
liveuser |
4808 |
#$result["timeout"],程序是否timeout,"true"代表是,"false"代表不是.
|
|
|
4809 |
#必填參數:
|
|
|
4810 |
#$conf["pid"],字串,程序的id.
|
|
|
4811 |
$conf["pid"]=$conf["pid"];
|
|
|
4812 |
#$conf["cmd"],字串,程序的指令.
|
|
|
4813 |
$conf["cmd"]=$conf["cmd"];
|
|
|
4814 |
#$conf["fileArgu"],字串,__FILE__的內容,__FILE__的內容.
|
|
|
4815 |
$conf["fileArgu"]=$conf["fileArgu"];
|
|
|
4816 |
#$conf["timeout"],整數,多少時間過後要timeout,單位為秒.
|
|
|
4817 |
$conf["timeout"]=60;
|
|
|
4818 |
#可省略參數:
|
|
|
4819 |
#無.
|
|
|
4820 |
#參考資料:
|
|
|
4821 |
#無.
|
|
|
4822 |
#備註:
|
|
|
4823 |
#無.
|
|
|
4824 |
*/
|
|
|
4825 |
public static function timeout(&$conf=array()){
|
| 226 |
liveuser |
4826 |
|
| 3 |
liveuser |
4827 |
#初始化要回傳的結果
|
|
|
4828 |
$result=array();
|
|
|
4829 |
|
|
|
4830 |
#取得當前執行的函數名稱
|
|
|
4831 |
$result["function"]=__FUNCTION__;
|
|
|
4832 |
|
|
|
4833 |
#如果沒有參數
|
|
|
4834 |
if(func_num_args()==0){
|
| 226 |
liveuser |
4835 |
|
| 3 |
liveuser |
4836 |
#設置執行失敗
|
|
|
4837 |
$result["status"]="false";
|
| 226 |
liveuser |
4838 |
|
| 3 |
liveuser |
4839 |
#設置執行錯誤訊息
|
|
|
4840 |
$result["error"]="函數".$result["function"]."需要參數";
|
| 226 |
liveuser |
4841 |
|
| 3 |
liveuser |
4842 |
#回傳結果
|
|
|
4843 |
return $result;
|
| 226 |
liveuser |
4844 |
|
| 3 |
liveuser |
4845 |
}#if end
|
|
|
4846 |
|
|
|
4847 |
#取得參數
|
|
|
4848 |
$result["argu"]=$conf;
|
|
|
4849 |
|
|
|
4850 |
#如果 $conf 不為陣列
|
|
|
4851 |
if(gettype($conf)!=="array"){
|
| 226 |
liveuser |
4852 |
|
| 3 |
liveuser |
4853 |
#設置執行失敗
|
|
|
4854 |
$result["status"]="false";
|
| 226 |
liveuser |
4855 |
|
| 3 |
liveuser |
4856 |
#設置執行錯誤訊息
|
|
|
4857 |
$result["error"][]="\$conf變數須為陣列形態";
|
| 226 |
liveuser |
4858 |
|
| 3 |
liveuser |
4859 |
#如果傳入的參數為 null
|
|
|
4860 |
if($conf===null){
|
| 226 |
liveuser |
4861 |
|
| 3 |
liveuser |
4862 |
#設置執行錯誤訊息
|
|
|
4863 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
| 226 |
liveuser |
4864 |
|
| 3 |
liveuser |
4865 |
}#if end
|
|
|
4866 |
|
|
|
4867 |
#回傳結果
|
|
|
4868 |
return $result;
|
| 226 |
liveuser |
4869 |
|
| 3 |
liveuser |
4870 |
}#if end
|
| 226 |
liveuser |
4871 |
|
| 3 |
liveuser |
4872 |
#檢查參數
|
|
|
4873 |
#函式說明:
|
|
|
4874 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
4875 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
4876 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
4877 |
#$result["function"],當前執行的函式名稱.
|
|
|
4878 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
4879 |
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
4880 |
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
4881 |
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
4882 |
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
|
|
|
4883 |
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
|
|
|
4884 |
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
|
|
|
4885 |
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
|
|
|
4886 |
#必填寫的參數:
|
|
|
4887 |
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
4888 |
$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
|
|
|
4889 |
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
4890 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("cmd","fileArgu","pid","timeout");
|
|
|
4891 |
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
|
|
|
4892 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string","string","integer");
|
|
|
4893 |
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
4894 |
$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
4895 |
#可以省略的參數:
|
|
|
4896 |
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
|
|
|
4897 |
#$conf["canBeEmptyString"]="false";
|
|
|
4898 |
#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
|
|
|
4899 |
#$conf["canNotBeEmpty"]=array();
|
|
|
4900 |
#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
|
|
|
4901 |
#$conf["canBeEmpty"]=array();
|
|
|
4902 |
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
|
|
|
4903 |
#$conf["skipableVariableCanNotBeEmpty"]=array();
|
|
|
4904 |
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
|
|
4905 |
#$conf["skipableVariableName"]=array();
|
| 226 |
liveuser |
4906 |
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
4907 |
#$conf["skipableVariableType"]=array();
|
|
|
4908 |
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
|
|
4909 |
#$conf["skipableVarDefaultValue"]=array("");
|
|
|
4910 |
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
4911 |
#$conf["arrayCountEqualCheck"][]=array();
|
|
|
4912 |
#參考資料來源:
|
|
|
4913 |
#array_keys=>http://php.net/manual/en/function.array-keys.php
|
|
|
4914 |
#建議:
|
|
|
4915 |
#增加可省略參數全部不能為空字串或空陣列的參數功能.
|
|
|
4916 |
$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
|
|
|
4917 |
unset($conf["variableCheck::checkArguments"]);
|
| 226 |
liveuser |
4918 |
|
| 3 |
liveuser |
4919 |
#如果檢查參數失敗
|
|
|
4920 |
if($checkArguments["status"]==="false"){
|
| 226 |
liveuser |
4921 |
|
| 3 |
liveuser |
4922 |
#設置執行失敗
|
|
|
4923 |
$result["status"]="false";
|
| 226 |
liveuser |
4924 |
|
| 3 |
liveuser |
4925 |
#設置執行錯誤訊息
|
|
|
4926 |
$result["error"]=$checkArguments;
|
| 226 |
liveuser |
4927 |
|
| 3 |
liveuser |
4928 |
#回傳結果
|
|
|
4929 |
return $result;
|
| 226 |
liveuser |
4930 |
|
| 3 |
liveuser |
4931 |
}#if end
|
| 226 |
liveuser |
4932 |
|
| 3 |
liveuser |
4933 |
#如果參數檢查不過
|
|
|
4934 |
if($checkArguments["passed"]==="false"){
|
| 226 |
liveuser |
4935 |
|
| 3 |
liveuser |
4936 |
#設置執行失敗
|
|
|
4937 |
$result["status"]="false";
|
| 226 |
liveuser |
4938 |
|
| 3 |
liveuser |
4939 |
#設置執行錯誤訊息
|
|
|
4940 |
$result["error"]=$checkArguments;
|
| 226 |
liveuser |
4941 |
|
| 3 |
liveuser |
4942 |
#回傳結果
|
|
|
4943 |
return $result;
|
| 226 |
liveuser |
4944 |
|
| 3 |
liveuser |
4945 |
}#if end
|
| 226 |
liveuser |
4946 |
|
| 3 |
liveuser |
4947 |
#無窮迴圈
|
|
|
4948 |
while(true){
|
| 226 |
liveuser |
4949 |
|
| 3 |
liveuser |
4950 |
#函式說明:
|
| 226 |
liveuser |
4951 |
#檢查程序是否還在執行
|
| 3 |
liveuser |
4952 |
#回傳結果
|
|
|
4953 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
4954 |
#$result["error"],錯誤訊息陣列.
|
| 226 |
liveuser |
4955 |
#$result["function"],當前執行的函數名稱.
|
| 3 |
liveuser |
4956 |
#$result["exist"],程序是否存在,"true"代表存在,"false"代表不存在.
|
|
|
4957 |
#必填參數:
|
|
|
4958 |
#$conf["pid"],字串,程序的id.
|
|
|
4959 |
$conf["external::processStatus"]["pid"]=$conf["pid"];
|
|
|
4960 |
#$conf["cmd"],字串,程序的指令.
|
|
|
4961 |
$conf["external::processStatus"]["cmd"]=$conf["cmd"];
|
|
|
4962 |
#$conf["fileArgu"],字串,__FILE__的內容,__FILE__的內容.
|
|
|
4963 |
$conf["external::processStatus"]["fileArgu"]=$conf["fileArgu"];
|
|
|
4964 |
$processStatus=external::processStatus($conf["external::processStatus"]);
|
|
|
4965 |
unset($conf["external::processStatus"]);
|
| 226 |
liveuser |
4966 |
|
| 3 |
liveuser |
4967 |
#如果執行失敗
|
|
|
4968 |
if($processStatus["status"]==="false"){
|
| 226 |
liveuser |
4969 |
|
| 3 |
liveuser |
4970 |
#設置執行失敗
|
|
|
4971 |
$result["status"]="false";
|
| 226 |
liveuser |
4972 |
|
| 3 |
liveuser |
4973 |
#設置執行錯誤訊息
|
|
|
4974 |
$result["error"]=$processStatus;
|
| 226 |
liveuser |
4975 |
|
| 3 |
liveuser |
4976 |
#回傳結果
|
|
|
4977 |
return $result;
|
| 226 |
liveuser |
4978 |
|
| 3 |
liveuser |
4979 |
}#if end
|
| 226 |
liveuser |
4980 |
|
| 3 |
liveuser |
4981 |
#如果程序已經不在
|
|
|
4982 |
if($processStatus["exist"]==="false"){
|
| 226 |
liveuser |
4983 |
|
| 3 |
liveuser |
4984 |
#設置執行正常
|
|
|
4985 |
$result["status"]="true";
|
| 226 |
liveuser |
4986 |
|
| 3 |
liveuser |
4987 |
#設置沒有timeout
|
|
|
4988 |
$result["timeout"]="false";
|
| 226 |
liveuser |
4989 |
|
| 3 |
liveuser |
4990 |
#回傳結果
|
|
|
4991 |
return $result;
|
| 226 |
liveuser |
4992 |
|
| 3 |
liveuser |
4993 |
}#if end
|
| 226 |
liveuser |
4994 |
|
| 3 |
liveuser |
4995 |
#如果已經timeout
|
|
|
4996 |
if($conf["timeout"]===0){
|
| 226 |
liveuser |
4997 |
|
| 3 |
liveuser |
4998 |
#設置執行正常
|
|
|
4999 |
$result["status"]="true";
|
| 226 |
liveuser |
5000 |
|
| 3 |
liveuser |
5001 |
#設置有timeout
|
|
|
5002 |
$result["timeout"]="true";
|
| 226 |
liveuser |
5003 |
|
| 3 |
liveuser |
5004 |
#中止該程序
|
|
|
5005 |
#函式說明:
|
|
|
5006 |
#呼叫shell執行系統命令,中止執行中的pid.
|
|
|
5007 |
#回傳結果:
|
|
|
5008 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
5009 |
#$result["error"],錯誤訊息陣列.
|
|
|
5010 |
#$result["function"],當前執行的函數名稱.
|
|
|
5011 |
#$result["warning"],警告訊息陣列.
|
|
|
5012 |
#$result["cmd"],執行的指令內容.
|
|
|
5013 |
#$result["output"],爲執行完二元碼後的輸出陣列.
|
|
|
5014 |
#$result["runing"],程式是否還在執行.
|
|
|
5015 |
#必填的參數
|
|
|
5016 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
| 226 |
liveuser |
5017 |
$conf["external::killProcess"]["fileArgu"]=$conf["fileArgu"];
|
| 3 |
liveuser |
5018 |
#$conf["pid"],字串,執行序的pid.
|
|
|
5019 |
$conf["external::killProcess"]["pid"]=$conf["pid"];
|
|
|
5020 |
#$conf["cmd"],字串,要中止的指令關鍵字.
|
| 226 |
liveuser |
5021 |
$conf["external::killProcess"]["cmd"]=$conf["cmd"];
|
| 3 |
liveuser |
5022 |
#備註:
|
|
|
5023 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,透過su使用root身份可能會被selinux阻擋.
|
|
|
5024 |
#參考資料:
|
|
|
5025 |
#http://php.net/manual/en/function.exec.php
|
|
|
5026 |
$killProcess=external::killProcess($conf["external::killProcess"]);
|
|
|
5027 |
unset($conf["external::killProcess"]);
|
| 226 |
liveuser |
5028 |
|
| 3 |
liveuser |
5029 |
#如果中止程序失敗
|
|
|
5030 |
if($killProcess["status"]==="false"){
|
| 226 |
liveuser |
5031 |
|
| 3 |
liveuser |
5032 |
#設置執行失敗
|
|
|
5033 |
$result["status"]="false";
|
| 226 |
liveuser |
5034 |
|
| 3 |
liveuser |
5035 |
#設置執行錯誤訊息
|
|
|
5036 |
$result["error"]=$killProcess;
|
| 226 |
liveuser |
5037 |
|
| 3 |
liveuser |
5038 |
#回傳結果
|
|
|
5039 |
return $result;
|
| 226 |
liveuser |
5040 |
|
| 3 |
liveuser |
5041 |
}#if end
|
| 226 |
liveuser |
5042 |
|
| 3 |
liveuser |
5043 |
#回傳結果
|
|
|
5044 |
return $result;
|
| 226 |
liveuser |
5045 |
|
| 3 |
liveuser |
5046 |
}#if end
|
| 226 |
liveuser |
5047 |
|
|
|
5048 |
#休息一秒
|
| 3 |
liveuser |
5049 |
sleep(1);
|
| 226 |
liveuser |
5050 |
|
| 3 |
liveuser |
5051 |
#剩餘timeout時間減一
|
| 226 |
liveuser |
5052 |
$conf["timeout"]--;
|
| 3 |
liveuser |
5053 |
|
| 226 |
liveuser |
5054 |
}#while end
|
|
|
5055 |
|
|
|
5056 |
}#function timeout end
|
|
|
5057 |
|
| 3 |
liveuser |
5058 |
}#class external end
|
|
|
5059 |
|
|
|
5060 |
?>
|