Subversion Repositories php-qbpwcf

Rev

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