Subversion Repositories php-qbpwcf

Rev

Rev 3 | Rev 66 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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