Subversion Repositories php-qbpwcf

Rev

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