Subversion Repositories php-qbpwcf

Rev

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

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