Subversion Repositories php-qbpwcf

Rev

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