Subversion Repositories qbpwcf-lib(archive)

Rev

Rev 915 | Details | Compare with Previous | Last modification | View Log | RSS feed

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