Subversion Repositories php-qbpwcf

Rev

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