Subversion Repositories php-qbpwcf

Rev

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

Rev Author Line No. Line
3 liveuser 1
<?php
2
/*
3
 
4
	QBPWCF, Quick Build PHP website Component base on Fedora Linux.
239 liveuser 5
    Copyright (C) 2014~2026 MIN ZHI, CHEN
3 liveuser 6
 
7
    This file is part of QBPWCF.
8
 
9
    QBPWCF is free software: you can redistribute it and/or modify
10
    it under the terms of the GNU General Public License as published by
11
    the Free Software Foundation, either version 3 of the License, or
12
    (at your option) any later version.
13
 
14
    QBPWCF is distributed in the hope that it will be useful,
15
    but WITHOUT ANY WARRANTY; without even the implied warranty of
16
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
    GNU General Public License for more details.
18
 
19
    You should have received a copy of the GNU General Public License
20
    along with QBPWCF.  If not, see <http://www.gnu.org/licenses/>.
21
 
22
*/
23
namespace qbpwcf;
226 liveuser 24
 
3 liveuser 25
/*
26
類別說明:
27
跟socket應用相關的類別.
28
備註:
29
無.
30
*/
31
class sock{
32
 
33
	/*
34
	#函式說明:
35
	#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
36
	#回傳結果:
37
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
38
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
39
	#$result["function"],當前執行的函式名稱.
40
	#必填參數:
41
	#$method,物件,為物件實體或類別名稱,會自動置入該參數.
42
	#$arguments,陣列,為呼叫方法時所用的參數.
43
	#可省略參數:
44
	#無.
45
	#參考資料:
46
	#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
47
	#備註:
48
	#無.
49
	*/
50
	public function __call($method,$arguments){
226 liveuser 51
 
3 liveuser 52
		#取得當前執行的函式
53
		$result["function"]=__FUNCTION__;
226 liveuser 54
 
3 liveuser 55
		#設置執行不正常
56
		$result["status"]="false";
226 liveuser 57
 
3 liveuser 58
		#設置執行錯誤
59
		$result["error"][]=__NAMESPACE__ ."/".$method."() 不存在!";
226 liveuser 60
 
3 liveuser 61
		#設置所丟入的參數
62
		$result["error"][]=$arguments;
226 liveuser 63
 
3 liveuser 64
		#回傳結果
65
		return $result;
226 liveuser 66
 
3 liveuser 67
		}#function __call end
226 liveuser 68
 
3 liveuser 69
	/*
70
	#函式說明:
71
	#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
72
	#回傳結果:
73
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
74
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
75
	#$result["function"],當前執行的函式名稱.
76
	#必填參數:
77
	#$method,物件,為物件實體或類別名稱,會自動置入該參數.
78
	#$arguments,陣列,為呼叫方法時所用的參數.
79
	#可省略參數:
80
	#無.
81
	#參考資料:
82
	#__callStatic=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
83
	#備註:
84
	#無.
85
	*/
86
	public static function __callStatic($method,$arguments){
226 liveuser 87
 
3 liveuser 88
		#取得當前執行的函式
89
		$result["function"]=__FUNCTION__;
226 liveuser 90
 
3 liveuser 91
		#設置執行不正常
92
		$result["status"]="false";
226 liveuser 93
 
3 liveuser 94
		#設置執行錯誤
95
		$result["error"][]="欲呼叫的". __NAMESPACE__ ."/".$method."() 不存在!";
226 liveuser 96
 
3 liveuser 97
		#設置所丟入的參數
98
		$result["error"][]=$arguments;
226 liveuser 99
 
3 liveuser 100
		#回傳結果
101
		return $result;
226 liveuser 102
 
103
		}#function __callStatic end
104
 
3 liveuser 105
	/*
106
	#函式說明:
107
	#建立 unix domain socket server, 僅提供具備檔案存取權限的用戶使用,預設提供可以下達任何指令的功能.
108
	#回傳結果:
109
	#$result["status"],"true"代表執行正常;"false"代表執行不正常.
110
	#$result["error"],錯誤訊息陣列.
111
	#$result["function"],當前執行的函式名稱.
112
	#$result["serverCache"],函式結束前,儲存在serverCache的內容,若有元素"exit"存在則代表是正常結束.
113
	#$result["serverCache"]["serverSide"],server side 的 cache.
114
	#$result["serverCache"]["serverSide"]["procs"], server side 的 procs cache,儲存執行的子程序資訊.
115
	#$result["serverCache"]["clientSide"],client site 的 cache.
116
	#必填參數:
117
	#$conf["sock"],字串,socket檔案要放在哪邊,名稱為何.
118
	$conf["sock"]="";
119
	#$conf["fileArgu"],字串,變數__FILE__的內容.
120
	$conf["fileArgu"]=__FILE__;
121
	#可省略參數:
122
	#$conf["changeOwner"],字串,要將socket檔案的擁有着權限進行修改."user.group"代表擁有者帳號為user,群組為group.
123
	#$conf["changeOwner"]="";
124
	#$conf["changePermission"],字串,要將socket檔案的權限設為多少.ex: "0666"(所有帳戶都有存取的權限) 或 "0660"(僅有擁有者與群組帳戶有存取的權限) 或 "0600"(只有擁有者有權限執行).
125
	#$conf["changePermission"]="";
126
	#$conf["sessionTimeout"],字串,當連線結束後於下一次連線間隔多久就算session timeout,server端會將記錄移除,client端需要重新拿取id,預設為300秒.
127
	#$conf["sessionTimeout"]="300";
128
	#$conf["addOnProcessFunc"],字串陣列,增加用於處理 json request 的函式名稱,給予的參數為array("request"=>收到的json訊息,"sock"=>用戶的socket,"clientCache"=>給予所有用戶的cache),若收到的不是json而是"quit"則代表用戶要結束連線;若收到的是$shutdownStr則代表要結束本函式.回傳的內容必須為陣列,例如 $res["continue"]="true"代表要繼續執行下一個addOnProcessFunc;"false"代表代表到此為止. $res["content"]="replaced content";代表要將收到的訊息取代成"replaced content". 最少要有回傳 $res["status"]數值"true"代表執行正常;"false"代表執行不正常.
129
	#$conf["addOnProcessFunc"]=array();
130
	#$conf["funcToRunWhenIdle"],字串陣列,當沒有事件產生時,要執行的函式名稱,給予參數為array("client"=>所有用戶,"clientCache"=>$result["serverCache"]["clientSide"]["addOnProcessFunc"][$funcToRunWhenIdle],"serverCache"=>$result["serverCache"]["serverSide"]["funcToRunWhenIdle"][$funcToRunWhenIdle]).
131
	#$conf["funcToRunWhenIdle"]=array();
132
	#$conf["paramsForFuncToRunWhenIdle"],2維陣列,每個元素代表指定給予funcToRunWhenIdle參數中的指定元素的參數.
133
	#$conf["paramsForFuncToRunWhenIdle"]=array();
134
	#$conf["infoToFunction"],陣列,需要增加給addOnProcessFunc跟funcToRunWhenIdle函式的資訊,在函式中其參數的info鍵值.
135
	#$conf["infoToFunction"]=array("debug"=>$debug);
339 liveuser 136
	#$conf["shutdownStrAddr"],字串,儲存收到用戶傳什麼樣的cmd字串會結束本函式的檔案位置與名稱,預設為 $conf["sock"].".shutdown".
3 liveuser 137
	#$conf["shutdownStrAddr"]="";
339 liveuser 138
	#$conf["shutdownCallback"],字串,當收到 shutdownStrAddr 參數的字串後,會先執行該函式,傳遞的參數為一陣列,key值有clientCache、serverCache、info,再結束執行,預設不使用.
139
	#$conf["shutdownCallback"]="";
3 liveuser 140
	#參考資料:
141
	#http://php.net/manual/en/function.stream-socket-server.php
142
	#備註:
143
	#無.
144
	*/
145
	public static function unixDomainSockServer(&$conf){
226 liveuser 146
 
3 liveuser 147
		#初始化要回傳的結果
148
		$result=array();
149
 
150
		#取得當前執行的函數名稱
151
		$result["function"]=__FUNCTION__;
226 liveuser 152
 
3 liveuser 153
		#如果沒有參數
154
		if(func_num_args()==0){
226 liveuser 155
 
3 liveuser 156
			#設置執行失敗
157
			$result["status"]="false";
226 liveuser 158
 
3 liveuser 159
			#設置執行錯誤訊息
160
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 161
 
3 liveuser 162
			#回傳結果
163
			return $result;
226 liveuser 164
 
3 liveuser 165
			}#if end
166
 
167
		#涵式說明:
168
		#判斷當前環境為web還是cmd
169
		#回傳結果:
170
		#$result,"web"或"cmd"
171
		if(csInformation::getEnv()==="web"){
226 liveuser 172
 
3 liveuser 173
			#設置執行失敗
174
			$result["status"]="false";
226 liveuser 175
 
3 liveuser 176
			#設置執行錯誤訊息
177
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
226 liveuser 178
 
3 liveuser 179
			#回傳結果
180
			return $result;
226 liveuser 181
 
3 liveuser 182
			}#if end
226 liveuser 183
 
3 liveuser 184
		#取得參數
185
		$result["argu"]=$conf;
186
 
187
		#如果 $conf 不為陣列
188
		if(gettype($conf)!="array"){
226 liveuser 189
 
3 liveuser 190
			#設置執行失敗
191
			$result["status"]="false";
226 liveuser 192
 
3 liveuser 193
			#設置執行錯誤訊息
194
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 195
 
3 liveuser 196
			#如果傳入的參數為 null
197
			if($conf==null){
226 liveuser 198
 
3 liveuser 199
				#設置執行錯誤訊息
200
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 201
 
3 liveuser 202
				}#if end
203
 
204
			#回傳結果
205
			return $result;
226 liveuser 206
 
3 liveuser 207
			}#if end
226 liveuser 208
 
3 liveuser 209
		#檢查參數
210
		#函式說明:
211
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
212
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
213
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
214
		#$result["function"],當前執行的函式名稱.
215
		#$result["argu"],設置給予的參數.
216
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
217
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
218
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
219
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
220
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
221
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
222
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
223
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
224
		#必填寫的參數:
225
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
226 liveuser 226
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
3 liveuser 227
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
228
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
229
		#可以省略的參數:
230
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
231
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("sock","fileArgu");
232
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
233
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string");
234
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
235
		#$conf["canBeEmptyString"]="false";
236
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
237
		#$conf["canNotBeEmpty"]=array();
238
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
239
		#$conf["canBeEmpty"]=array();
240
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
339 liveuser 241
		$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("changeOwner","changePermission","sessionTimeout","addOnProcessFunc","funcToRunWhenIdle","infoToFunction","shutdownStrAddr","shutdownCallback");
3 liveuser 242
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
339 liveuser 243
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("changeOwner","changePermission","sessionTimeout","addOnProcessFunc","funcToRunWhenIdle","infoToFunction","paramsForFuncToRunWhenIdle","shutdownStrAddr","shutdownCallback");
226 liveuser 244
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
339 liveuser 245
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string","string","array","array","array","array","array","string");
3 liveuser 246
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf[\"mustBeFilledVar\"]".
339 liveuser 247
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("root.".webUser,null,"300",null,null,null,array(),null,null);
3 liveuser 248
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
249
		#$conf["disallowAllSkipableVarIsEmpty"]="";
250
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
251
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
252
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
253
		#$conf["arrayCountEqualCheck"][]=array();
254
		#參考資料來源:
255
		#array_keys=>http://php.net/manual/en/function.array-keys.php
256
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
257
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 258
 
3 liveuser 259
		#如果檢查參數失敗
260
		if($checkArguments["status"]==="false"){
226 liveuser 261
 
3 liveuser 262
			#設置執行失敗
263
			$result["status"]="false";
226 liveuser 264
 
3 liveuser 265
			#設置錯誤訊息
266
			$result["error"]=$checkArguments;
226 liveuser 267
 
3 liveuser 268
			#回傳結果
269
			return $result;
226 liveuser 270
 
3 liveuser 271
			}#if end
226 liveuser 272
 
3 liveuser 273
		#如果檢查參數不通過
274
		if($checkArguments["passed"]==="false"){
226 liveuser 275
 
3 liveuser 276
			#設置執行失敗
277
			$result["status"]="false";
226 liveuser 278
 
3 liveuser 279
			#設置錯誤訊息
280
			$result["error"]=$checkArguments;
226 liveuser 281
 
3 liveuser 282
			#回傳結果
283
			return $result;
226 liveuser 284
 
3 liveuser 285
			}#if end
226 liveuser 286
 
3 liveuser 287
		#縮減 socket 的路徑,避免出錯.
288
		#函式說明:
289
		#將檔案目錄的絕對位置中的 "../" 剔除變成直觀的路徑.
290
		#回傳結果:
291
		#$result["status"],執行是否成功,"true"代表執行成功,"false"代表執行失敗.
292
		#$result["function"],當前執行的函數.
293
		#$result["error"],錯誤訊息陣列.
294
		#$result["argu"],使用者參數.
295
		#$result["changedPath"],處理完後回傳的目錄字串.
296
		#$result["oriPath"],原始的路徑字串
297
		#必填參數:
298
		#$conf["dirStr"],字串,要處理的檔案目錄字串.
299
		$conf["stringProcess::changeDirByDotDotSolidus"]["dirStr"]=$conf["sock"];
300
		#可省略參數:
301
		#無.
302
		#參考資料:
303
		#無.
304
		#備註:
305
		#考慮用realpath取代
306
		$changeDirByDotDotSolidus=stringProcess::changeDirByDotDotSolidus($conf["stringProcess::changeDirByDotDotSolidus"]);
307
		unset($conf["stringProcess::changeDirByDotDotSolidus"]);
226 liveuser 308
 
3 liveuser 309
		#如果執行異常
310
		if($changeDirByDotDotSolidus["status"]==="false"){
226 liveuser 311
 
3 liveuser 312
			#設置執行失敗
313
			$result["status"]="false";
226 liveuser 314
 
3 liveuser 315
			#設置錯誤訊息
316
			$result["error"]=$changeDirByDotDotSolidus;
226 liveuser 317
 
3 liveuser 318
			#回傳結果
319
			return $result;
226 liveuser 320
 
3 liveuser 321
			}#if end
226 liveuser 322
 
3 liveuser 323
		#取得整理過後的直觀路徑
324
		$conf["sock"]=$changeDirByDotDotSolidus["changedPath"];
226 liveuser 325
 
3 liveuser 326
		#取得 socket 的路徑
327
		#函式說明:
328
		#將字串特定關鍵字與其後面的內容剔除
329
		#回傳結果:
330
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
331
		#$result["error"],錯誤訊息陣列.
332
		#$result["warning"],警告訊息鎮列.
333
		#$result["founded"],有無找到定字串"true"代表有,"false"代表沒有.
334
		#$result["function"],當前執行的函數名稱.
335
		#$result["oriStr"],要處理的原始字串內容.
336
		#$result["content"],處理好的的字串內容.
337
		#$result["argu"],使用的參數.
338
		#必填參數:
339
		#$conf["stringIn"],字串,要處理的字串.
340
		$conf["stringProcess::delStrAfterKeyWord"]["stringIn"]=$conf["sock"];
341
		#$conf["keyWord"],字串,特定字串.
342
		$conf["stringProcess::delStrAfterKeyWord"]["keyWord"]="/";
343
		#可省略參數:
344
		#$conf["deleteLastRepeatedOne"],字串,預設為"false";若為"true"則代表連續遇到同 $conf["keyWord"] 的內容,要將移除內容的起點往後移動到為後一個 $conf["keyWord"].
345
		$conf["stringProcess::delStrAfterKeyWord"]["deleteLastRepeatedOne"]="true";
346
		#參考資料:
347
		#無.
348
		#備註:
349
		#無.
350
		$delStrAfterKeyWord=stringProcess::delStrAfterKeyWord($conf["stringProcess::delStrAfterKeyWord"]);
351
		unset($conf["stringProcess::delStrAfterKeyWord"]);
226 liveuser 352
 
3 liveuser 353
		#如果執行失敗
354
		if($delStrAfterKeyWord["status"]==="false"){
226 liveuser 355
 
3 liveuser 356
			#設置執行失敗
357
			$result["status"]="false";
226 liveuser 358
 
3 liveuser 359
			#設置錯誤訊息
360
			$result["error"][]=$delStrAfterKeyWord;
226 liveuser 361
 
3 liveuser 362
			#設置錯誤訊息
363
			$result["error"][]="建立 unix domain socket(".$conf["sock"].") 失敗";
226 liveuser 364
 
3 liveuser 365
			#印出錯誤訊息
366
			echo print_r($result,true);
226 liveuser 367
 
3 liveuser 368
			#回傳結果
369
			return $result;
226 liveuser 370
 
3 liveuser 371
			}#if end
226 liveuser 372
 
3 liveuser 373
		#如果有 "/"
374
		if($delStrAfterKeyWord["founded"]==="true"){
226 liveuser 375
 
3 liveuser 376
			#確保建立socket的路徑有存在
377
			#函式說明:
378
			#確保路徑存在.
379
			#回傳結果:
380
			#$result["status"],執行正常與否,"true"代表正常,"false"代表不正常.
381
			#$result["error"],錯誤訊息陣列.
382
			#$resutl["function"],當前執行的涵式名稱.
383
			#$result["path"],建立好的路徑字串.
384
			#$result["fileName"],檔案名稱,若 $conf["haveFileName"] 為 "true" 則會回傳.
385
			#$result["argu"],使用的參數.
386
			#必填參數:
387
			#$conf["path"],要檢查的路徑
388
			$conf["fileAccess::validatePath"]["path"]=$delStrAfterKeyWord["content"];
389
			#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
390
			$conf["fileAccess::validatePath"]["fileArgu"]=__FILE__;
391
			#可省略參數:
392
			#$conf["haveFileName"],字串,"true"代表有$conf["path"]檔案名稱,"false"代表$conf["path"]為純路徑,預設為"false".
393
			$conf["fileAccess::validatePath"]["haveFileName"]="false";
394
			#$conf["dirPermission"],字串,新建資料夾的權限設定,預設爲0770,亦即擁有者,同群組者可以讀,寫,存取,其他人僅能存取.
395
			#$conf["dirPermission"]="";
396
			#$conf["web"],是否為檔案系統,"true"為網頁路徑,"false"為網頁系統,預設為"false".
397
			$conf["fileAccess::validatePath"]["web"]="false";
398
			#參考資料:
399
			#無.
400
			#備註:
401
			#無.
402
			$validatePath=fileAccess::validatePath($conf["fileAccess::validatePath"]);
403
			unset($conf["fileAccess::validatePath"]);
226 liveuser 404
 
3 liveuser 405
			#如果執行失敗
406
			if($validatePath["status"]==="false"){
226 liveuser 407
 
3 liveuser 408
				#設置執行失敗
409
				$result["status"]="false";
226 liveuser 410
 
3 liveuser 411
				#設置錯誤訊息
412
				$result["error"][]=$validatePath;
226 liveuser 413
 
3 liveuser 414
				#設置錯誤訊息
415
				$result["error"][]="建立 unix domain socket(".$conf["sock"].") 失敗";
226 liveuser 416
 
3 liveuser 417
				#印出錯誤訊息
418
				echo print_r($result,true);
226 liveuser 419
 
3 liveuser 420
				#回傳結果
421
				return $result;
226 liveuser 422
 
3 liveuser 423
				}#if end
226 liveuser 424
 
3 liveuser 425
			}#if end
226 liveuser 426
 
3 liveuser 427
		#如果同名 unix domain socket file 存在
428
		if(file_exists($conf["sock"])){
226 liveuser 429
 
3 liveuser 430
			#則移除之.
431
			unlink($conf["sock"]);
226 liveuser 432
 
3 liveuser 433
			}#if end
226 liveuser 434
 
3 liveuser 435
		#預設的 unix socket addr
436
		$unixAddr="unix://".$conf["sock"];
226 liveuser 437
 
3 liveuser 438
		#建立 unix domain socket
439
		$socket=stream_socket_server($unixAddr, $errno, $errstr);
226 liveuser 440
 
3 liveuser 441
		#如果建立 unix domain socket 失敗
442
		if(!$socket){
226 liveuser 443
 
3 liveuser 444
			#設置執行失敗
445
			$result["status"]="false";
226 liveuser 446
 
3 liveuser 447
			#設置錯誤訊息
448
			$result["error"][]=$errstr." (".$errno.")";
226 liveuser 449
 
3 liveuser 450
			#設置錯誤訊息
451
			$result["error"][]="建立 unix domain socket(".$conf["sock"].") 失敗";
226 liveuser 452
 
3 liveuser 453
			#印出錯誤訊息
454
			echo print_r($result,true);
226 liveuser 455
 
3 liveuser 456
			#回傳結果
457
			return $result;
226 liveuser 458
 
3 liveuser 459
			}#if end
226 liveuser 460
 
3 liveuser 461
		#如果建立 unix domain socket 成功
462
		else{
226 liveuser 463
 
3 liveuser 464
			#產生用於代表結束本函式的字串
465
			#函式說明:
466
			#使用 linux 的 uuid 指令來產生 uuid 字串
467
			#回傳結果:
468
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
469
			#$result["error"],錯誤訊息.
470
			#$result["function"],當前執行的函式名稱.
471
			#$result["content"],uuid.
472
			#必填參數:
473
			#無.
474
			#可省略參數:
475
			#無.
476
			#參考資料:
477
			#無.
478
			#備註:
479
			#無.
480
			$uuid=cmd::uuid();
226 liveuser 481
 
3 liveuser 482
			#如果執行失敗
483
			if($uuid["status"]==="false"){
226 liveuser 484
 
3 liveuser 485
				#設置執行失敗
486
				$result["status"]="false";
226 liveuser 487
 
3 liveuser 488
				#設置錯誤訊息
489
				$result["error"]=$uuid;
226 liveuser 490
 
3 liveuser 491
				#回傳結果
492
				return $result;
226 liveuser 493
 
3 liveuser 494
				}#if end
226 liveuser 495
 
3 liveuser 496
			#設置代表結束本服務的uuid字串
497
			$shutdownStr=$uuid["content"];
226 liveuser 498
 
3 liveuser 499
			#如果沒有設置
500
			if(!isset($conf["shutdownStrAddr"])){
226 liveuser 501
 
3 liveuser 502
				#初始化存放 shutdownStr 的檔案
503
				$conf["shutdownStrAddr"]=$conf["sock"].".shutdownStr";
226 liveuser 504
 
3 liveuser 505
				}#if end
226 liveuser 506
 
3 liveuser 507
			#如果檔案位置名稱跟socket一樣
508
			else if($conf["sock"]===$conf["shutdownStrAddr"]){
226 liveuser 509
 
3 liveuser 510
				#設置執行失敗
511
				$result["status"]="false";
226 liveuser 512
 
3 liveuser 513
				#設置錯誤訊息
514
				$result["error"][]="參數shutdownStrAddr跟sock不可一樣";
226 liveuser 515
 
3 liveuser 516
				#設置錯誤訊息
517
				$result["error"][]=$checkArguments;
226 liveuser 518
 
3 liveuser 519
				#回傳結果
520
				return $result;
226 liveuser 521
 
3 liveuser 522
				}#else end
226 liveuser 523
 
3 liveuser 524
			#建立裡面儲存若要結束本程式需要接收到什麼樣的字串
525
			#函式說明:
526
			#將字串寫入到檔案
527
			#回傳結果:
528
			#$result["status"],"true"表示檔案寫入成功,"false"表示檔案寫入失敗.
529
			#$result["error"],錯誤訊息陣列.
530
			#$result["function"],當前執行的函數名稱.
531
			#$result["fileInfo"],實際上寫入的檔案資訊陣列.
532
			#$result["fileInfo"]["createdFileName"],建立好的檔案名稱.
533
			#$result["fileInfo"]["createdFilePath"],檔案建立的路徑.
534
			#$result["fileInfo"]["createdFilePathAndName"].建立好的檔案名稱與路徑.
535
			#$result["argu"],使用的參數.
536
			#必填參數:
537
			#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
538
			$conf["fileAccess::writeTextIntoFile"]["fileArgu"]=$conf["fileArgu"];
539
			#可省略參數:
540
			#$conf["fileName"],字串,爲要編輯的檔案名稱,預設為隨機產生的檔案名稱.
541
			$conf["fileAccess::writeTextIntoFile"]["fileName"]=$conf["shutdownStrAddr"];
542
			#$conf["inputString"],字串,爲要寫入到裏面的內容,若要每筆資料寫入後換行,則可以在字串內容後面加上 \r\n 即可,預設為"".
543
			$conf["fileAccess::writeTextIntoFile"]["inputString"]=$shutdownStr;
544
			#$conf["writeMethod"],字串,爲檔案撰寫的方式,可省略,是複寫'a'還是,重新寫入'w',預設爲'w',重新寫入.
545
			#$conf["writeMethod"]="a";
546
			#$conf["checkRepeat"],字串,"true"代表建立檔案之前要先檢查檔案是否存在,若存在則在原名稱後面加上從(1)開始的編號.
547
			#$conf["checkRepeat"]="";
548
			#$conf["filenameExtensionStartPoint"],字串,檔案的副檔名是從倒數第幾個小數點(dot)開始,預設為"1",最後一個小數點,必須與$conf["checkRepeat"]搭配才會生效.
549
			#$conf["filenameExtensionStartPoint"]="";
550
			#$conf["repeatNameRule"],字串,遇到相同名稱的檔案要如何加上識別的編號,編號用「\$i」表示,預設為"(\$i)",必須與$conf["checkRepeat"]搭配才會生效.
551
			#$conf["repeatNameRule"]="";
552
			#$conf["web"],檔案是否位於網站上"true",若是在檔案系統則為"false",預設為"true".
553
			$conf["fileAccess::writeTextIntoFile"]["web"]="false";
554
			#參考資料:
555
			#無.
556
			#備註:
557
			#無.
558
			$writeTextIntoFile=fileAccess::writeTextIntoFile($conf["fileAccess::writeTextIntoFile"]);
559
			unset($conf["fileAccess::writeTextIntoFile"]);
226 liveuser 560
 
3 liveuser 561
			#如果執行失敗
562
			if($writeTextIntoFile["status"]==="false"){
226 liveuser 563
 
3 liveuser 564
				#設置執行失敗
565
				$result["status"]="false";
226 liveuser 566
 
3 liveuser 567
				#設置錯誤訊息
568
				$result["error"]=$writeTextIntoFile;
226 liveuser 569
 
3 liveuser 570
				#回傳結果
571
				return $result;
226 liveuser 572
 
3 liveuser 573
				}#if end
226 liveuser 574
 
3 liveuser 575
			#debug
576
			#var_dump(__LINE__,$writeTextIntoFile);exit;
226 liveuser 577
 
3 liveuser 578
			#初始化 client 陣列
579
			$client=array();
226 liveuser 580
 
3 liveuser 581
			#如果有設置$conf["changeOwner"]
582
			if(isset($conf["changeOwner"])){
226 liveuser 583
 
3 liveuser 584
				#如果裡面的"."不是一個
585
				if(substr_count($conf["changeOwner"],".")!==1){
226 liveuser 586
 
3 liveuser 587
					#設置執行失敗
588
					$result["status"]="false";
226 liveuser 589
 
3 liveuser 590
					#設置錯誤訊息
591
					$result["error"][]="參數 changeOwner 應為 username.groupname";
226 liveuser 592
 
3 liveuser 593
					#印出錯誤訊息
594
					echo print_r($result,true);
226 liveuser 595
 
3 liveuser 596
					#回傳結果
597
					return $result;
226 liveuser 598
 
3 liveuser 599
					}#if end
226 liveuser 600
 
3 liveuser 601
				#改變檔案的擁有者
602
				#函式說明:
603
				#使用 linux 的 chown 指令來修改目標檔案或目錄的擁有者跟群組擁有者資訊.
604
				#回傳結果:
605
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
606
				#$result["error"],錯誤訊息.
607
				#$result["function"],當前執行的函式名稱.
608
				#$result["cmd"],執行的指令.
609
				#$result["content"],執行的結果陣列,如果參數 "recursive" 跟 "excludeSelf" 都有設定的話,就會回傳該結果.
610
				#必填參數:
611
				#$conf["owner"],字串,要變哪個使用者擁有.
612
				$conf["cmd::chown"]["owner"]=explode(".",$conf["changeOwner"])[0];
613
				#$conf["target"],字串,需要變更擁有者、 群組的目標.
614
				$conf["cmd::chown"]["target"]=$conf["sock"];
615
				#可省略參數:
616
				#$conf["group"],字串,要變成什麼群組擁有,預設跟"owner"一樣.
617
				$conf["cmd::chown"]["group"]=explode(".",$conf["changeOwner"])[1];
618
				#$conf["recursive"],字串,"true"代表目標目錄底下的內容都要套用,預設為"false".
619
				#$conf["recursive"]="true";
620
				#$conf["excludeSelf"],字串,預設為"false"代表不處理;若為"true"則會排除目標自己(資料夾).
621
				#$conf["excludeSelf"]="true";
622
				#參考資料:
623
				#無.
624
				#備註:
625
				#無.
626
				$chown=cmd::chown($conf["cmd::chown"]);
627
				unset($conf["cmd::chown"]);
226 liveuser 628
 
3 liveuser 629
				#如果改變檔案擁有者限失敗
630
				if($chown===false){
226 liveuser 631
 
3 liveuser 632
					#設置執行失敗
633
					$result["status"]="false";
226 liveuser 634
 
3 liveuser 635
					#設置錯誤訊息
636
					$result["error"][]="改變 ".$conf["sock"]." 檔案的擁有資訊為 ".explode(".",$conf["changeOwner"])[0]." 失敗,通常只有系統帳戶才能變更之.";
226 liveuser 637
 
3 liveuser 638
					#印出錯誤訊息
639
					echo print_r($result,true);
226 liveuser 640
 
3 liveuser 641
					#回傳結果
226 liveuser 642
					return $result;
643
 
3 liveuser 644
					}#if end
226 liveuser 645
 
3 liveuser 646
				}#if end
226 liveuser 647
 
3 liveuser 648
			#如果有設置 $conf["changePermission"]
649
			if(isset($conf["changePermission"])){
226 liveuser 650
 
3 liveuser 651
				#函式說明:
652
				#使用 linux 的 chmod 指令來修改目標檔案或目錄的權限.
653
				#回傳結果:
654
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
655
				#$result["error"],錯誤訊息.
656
				#$result["function"],當前執行的函式名稱.
657
				#$result["cmd"],執行的指令.
658
				#$result["content"],執行的結果陣列,如果參數 "recursive" 跟 "excludeSelf" 都有設定的話,就會回傳該結果.
659
				#必填參數:
660
				#$conf["mode"],字串,要變成什麼權限.
661
				$conf["cmd::chmod"]["mode"]=$conf["changePermission"];
662
				#$conf["target"],字串,需要變更權限的目標.
663
				$conf["cmd::chmod"]["target"]=$conf["sock"];
664
				#可省略參數:
665
				#$conf["recursive"],字串,"true"代表目標目錄底下的內容都要套用,預設為"false".
666
				#$conf["recursive"]="true";
667
				#$conf["excludeSelf"],字串,預設為"false"代表不處理;若為"true"則會排除目標自己(資料夾).
668
				#$conf["excludeSelf"]="true";
669
				#參考資料:
670
				#無.
671
				#備註:
672
				#無.
673
				$chmod=cmd::chmod($conf["cmd::chmod"]);
674
				unset($conf["cmd::chmod"]);
226 liveuser 675
 
3 liveuser 676
				#如果變更檔案權限失敗
677
				if($chmod["status"]==="false"){
226 liveuser 678
 
3 liveuser 679
					#設置執行失敗
680
					$result["status"]="false";
226 liveuser 681
 
3 liveuser 682
					#設置錯誤訊息
683
					$result["error"]=$chmod;
226 liveuser 684
 
3 liveuser 685
					#印出錯誤訊息
686
					echo print_r($result,true);
226 liveuser 687
 
3 liveuser 688
					#回傳結果
226 liveuser 689
					return $result;
690
 
3 liveuser 691
					}#if end
226 liveuser 692
 
3 liveuser 693
				}#if end
226 liveuser 694
 
3 liveuser 695
			#初始化儲存 server 資訊的變數,預設數值為 $result 自己
696
			$result["serverCache"]=&$result;
226 liveuser 697
 
3 liveuser 698
			#初始化方便識別 server 資訊的變數
699
			$serverCache=&$result["serverCache"];
226 liveuser 700
 
3 liveuser 701
			#初始化 server site cache
702
			$serverCache["serverSide"]=array();
226 liveuser 703
 
704
			#初始化 儲存 透過 proc 執行的子程序
3 liveuser 705
			$serverCache["serverSide"]["procs"]=array();
226 liveuser 706
 
3 liveuser 707
			#初始化 client site cache
708
			$serverCache["clientSide"]=array();
226 liveuser 709
 
3 liveuser 710
			#初始化方便識別 client site cache 資訊的變數
711
			$clientCache=&$serverCache["clientSide"];
226 liveuser 712
 
3 liveuser 713
			#將建立的 listen sock 放到 $master 陣列
714
			$master[] = $socket;
226 liveuser 715
 
3 liveuser 716
			#永久監聽, 不 timeout
717
			while(true){
226 liveuser 718
 
3 liveuser 719
				#debug
720
				#var_dump(__LINE__,gmdate("Y-m-d H:i:s",time()));
226 liveuser 721
 
3 liveuser 722
				#設置給 stream_select 監控的sock變數
723
				$read = $write = $except = $master;
226 liveuser 724
 
3 liveuser 725
				#等待5秒,看有沒有連線進來讀寫資料
726
				$mod_fd = stream_select($read, $write, $except, 5);
226 liveuser 727
 
3 liveuser 728
				#如果執行失敗
729
				if($mod_fd===FALSE){
226 liveuser 730
 
3 liveuser 731
					#提示錯誤訊息
732
					echo "Stream_select failed at ".gmdate("Y-m-d H:i:s",time());
226 liveuser 733
 
3 liveuser 734
					#結束監聽
735
					exit(1);
226 liveuser 736
 
3 liveuser 737
					}#if end
226 liveuser 738
 
3 liveuser 739
				#如果沒有訊息,代表閒置時可以做的事情
740
				else if($mod_fd===0){
226 liveuser 741
 
3 liveuser 742
					#提示idle
743
					echo ".";
226 liveuser 744
 
3 liveuser 745
					#清除 session timeout 的連線 - start
226 liveuser 746
 
3 liveuser 747
					#初始化要給 server 的回應
748
					$res=array();
226 liveuser 749
 
3 liveuser 750
					#初始化計數移除幾個用戶
751
					$res["delIdleConCount"]=0;
226 liveuser 752
 
3 liveuser 753
					#針對每個 $client
754
					foreach($client as $index=>$uncheckClient){
226 liveuser 755
 
3 liveuser 756
						#如果上次的要求時間為300秒前.
757
						if(time()-(int)$uncheckClient["last_req_time"]>$conf["sessionTimeout"]){
226 liveuser 758
 
3 liveuser 759
							#移除該用戶變數
760
							unset($client["$index"]);
226 liveuser 761
 
3 liveuser 762
							#計數移除了幾個用戶
763
							$res["delIdleConCount"]++;
226 liveuser 764
 
3 liveuser 765
							}#if end
226 liveuser 766
 
3 liveuser 767
						}#foreach end
226 liveuser 768
 
3 liveuser 769
					#設置執行正常
770
					$res["status"]="true";
226 liveuser 771
 
3 liveuser 772
					#如果有移除閒置的連線
773
					if($res["delIdleConCount"]>0){
226 liveuser 774
 
3 liveuser 775
						#印出給 server 的訊息
776
						echo PHP_EOL."response:".PHP_EOL.print_r($res,true);
226 liveuser 777
 
3 liveuser 778
						}#if end
226 liveuser 779
 
3 liveuser 780
					#清除 session timeout 的連線 - end
226 liveuser 781
 
3 liveuser 782
					#清除 太久沒有 被查詢的 proc(子程序) - start
226 liveuser 783
 
3 liveuser 784
					#初始化要給 server 的回應
785
					$res=array();
226 liveuser 786
 
3 liveuser 787
					#初始化計數移除幾個沒被關注的子程序
788
					$res["delIdleProcCount"]=0;
226 liveuser 789
 
3 liveuser 790
					#針對每個 子程序
791
					foreach($serverCache["serverSide"]["procs"] as $procIndex=>$procInfo){
226 liveuser 792
 
3 liveuser 793
						#debug
794
						#var_dump(__FUNCTION__,__LINE__,$procInfo);
226 liveuser 795
 
3 liveuser 796
						#如果上次的要求時間為300秒前.
797
						if(time()-(int)$procInfo["latestAccessTime"]>$conf["sessionTimeout"]){
226 liveuser 798
 
3 liveuser 799
							#若運行中
800
							if($procInfo["content"][0]["statusCode"]==="?"){
226 liveuser 801
 
3 liveuser 802
								#更新資訊
803
								#函式說明:
804
								#更新透過proc執行的多程序資訊.
805
								#回傳結果:
806
								#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
807
								#$reuslt["error"],執行不正常結束的錯訊息陣列.
808
								#$result["function"],當前執行的函式名稱.
809
								#$result["argu"],使用的參數.
810
								#$result["content"],陣列,每個元素為其指令執行的結果訊息陣列,key為"status"代表執行是否正常的識別;key為"statusCode"代表程式結束後回傳給對應executeBy程式的數值;key為"output"代表標準輸出,若為resource,則代表為pipe;key為"error"代表非標準輸出,若為resource,則代表為pipe;key為"input"代表成功輸入的指令;key為"process"代表該程序經proc_open後的process source;key為"proc_get_status"代表程序的資訊.
811
								#必填參數:
812
								#$conf["procs"],陣列,運行self::proc後回傳的content.
813
								$conf["threads::proc_update"]["procs"]=$procInfo["content"];
814
								#可省略參數:
815
								#無.
816
								#參考資料:
817
								#無.
818
								#備註:
819
								#無.
820
								$proc_update=threads::proc_update($conf["threads::proc_update"]);
821
								unset($conf["threads::proc_update"]);
226 liveuser 822
 
3 liveuser 823
								#若執行不正常
824
								if($proc_update["status"]==="false"){
226 liveuser 825
 
3 liveuser 826
									#設置執行失敗
827
									$result["status"]="false";
226 liveuser 828
 
3 liveuser 829
									#設置錯誤訊息
830
									$result["error"]=$proc_update;
226 liveuser 831
 
3 liveuser 832
									#印出錯誤訊息
833
									echo print_r($result,true);
226 liveuser 834
 
3 liveuser 835
									#回傳結果
226 liveuser 836
									return $result;
837
 
3 liveuser 838
									}#if end
226 liveuser 839
 
3 liveuser 840
								#更新程序狀態
841
								$procInfo["content"]=$proc_update["content"][0];
226 liveuser 842
 
3 liveuser 843
								#更新會後存取時間
226 liveuser 844
 
3 liveuser 845
								#若已經執行結束
846
								if($procInfo["content"]["statusCode"]!=="?"){
226 liveuser 847
 
3 liveuser 848
									#換下一個
849
									continue;
226 liveuser 850
 
3 liveuser 851
									}#if end
226 liveuser 852
 
3 liveuser 853
								#執行到這代表一樣再運行中
226 liveuser 854
 
3 liveuser 855
								#終止程序
856
								proc_close($procInfo["content"]["process"]);
226 liveuser 857
 
3 liveuser 858
								#移除該用戶變數
226 liveuser 859
								unset($serverCache["serverSide"]["procs"][$procIndex]);
860
 
3 liveuser 861
								#換下一個
862
								continue;
226 liveuser 863
 
3 liveuser 864
								}#if end
226 liveuser 865
 
3 liveuser 866
							#反之為已運行結束
226 liveuser 867
 
3 liveuser 868
							#移除該用戶變數
869
							unset($serverCache["serverSide"]["procs"][$procIndex]);
226 liveuser 870
 
3 liveuser 871
							#計數移除幾個沒被關注的子程序
872
							$res["delIdleProcCount"]++;
226 liveuser 873
 
3 liveuser 874
							}#if end
226 liveuser 875
 
3 liveuser 876
						}#foreach end
226 liveuser 877
 
3 liveuser 878
					#設置執行正常
879
					$res["status"]="true";
226 liveuser 880
 
3 liveuser 881
					#如果有移除沒被關注的子程序
882
					if($res["delIdleProcCount"]>0){
226 liveuser 883
 
3 liveuser 884
						#印出給 server 的訊息
885
						echo PHP_EOL."response:".PHP_EOL.print_r($res,true);
226 liveuser 886
 
3 liveuser 887
						}#if end
226 liveuser 888
 
3 liveuser 889
					#清除 太久沒有 被查詢的 proc(子程序) - end
226 liveuser 890
 
3 liveuser 891
					#如果有設置 idle 要執行的函式
892
					if(isset($conf["funcToRunWhenIdle"])){
226 liveuser 893
 
3 liveuser 894
						#提示有idle要執行的函式
895
						echo "response:".PHP_EOL."There is funcToRunWhenIdle param set.".PHP_EOL;
226 liveuser 896
 
3 liveuser 897
						#針對每個 funcToRunWhenIdle
898
						foreach($conf["funcToRunWhenIdle"] as $index => $funcToRunWhenIdle){
226 liveuser 899
 
3 liveuser 900
							#提示正要執行的函式
901
							echo "response:".PHP_EOL."Executeing ".$funcToRunWhenIdle."...".PHP_EOL;
226 liveuser 902
 
3 liveuser 903
							#初始化要給 funcToRunWhenIdle 函式的參數
904
							$parasmForFuncToRunWhenIdle=array();
226 liveuser 905
 
3 liveuser 906
							#取得所有用戶
907
							$parasmForFuncToRunWhenIdle["client"]=&$client;
226 liveuser 908
 
3 liveuser 909
							#如果有設置 infoToFunction
910
							if(isset($conf["infoToFunction"])){
226 liveuser 911
 
3 liveuser 912
								#設置info
913
								$parasmForFuncToRunWhenIdle["info"]=&$conf["infoToFunction"];
226 liveuser 914
 
3 liveuser 915
								}#if end
226 liveuser 916
 
3 liveuser 917
							#debug
918
							#var_dump(__FILE__,__LINE__,$serverCache["serverSide"]);
226 liveuser 919
 
3 liveuser 920
							#如果屬於該 $funcToRunWhenIdle 的 server side cache 不存在
921
							if(!isset($serverCache["serverSide"]["funcToRunWhenIdle"][$funcToRunWhenIdle])){
226 liveuser 922
 
3 liveuser 923
								#初始化為空陣列
924
								$serverCache["serverSide"]["funcToRunWhenIdle"][$funcToRunWhenIdle]=array();
226 liveuser 925
 
3 liveuser 926
								}#if end
226 liveuser 927
 
3 liveuser 928
							#設置給予 $funcToRunWhenIdle 函式的 serverCache 參數
929
							$parasmForFuncToRunWhenIdle["serverCache"]=&$serverCache;
226 liveuser 930
 
3 liveuser 931
							#儲存 $funcToRunWhenIdle 函式 的運行結束的時間點到 server side 的 cache
932
							$serverCache["serverSide"]["funcToRunWhenIdle"][$funcToRunWhenIdle]["lastRunTime"]=time::unix()["content"];
226 liveuser 933
 
3 liveuser 934
							#如果有要指定給 $funcToRunWhenIdle 的參數
935
							if(isset($conf["paramsForFuncToRunWhenIdle"][$index])){
226 liveuser 936
 
3 liveuser 937
								#針對每個 要給予 funcToRunWhenIdle 的參數
938
								foreach($conf["paramsForFuncToRunWhenIdle"][$index] as $arguKeyForFuncToRunWhenIdle => $arguValueForFuncToRunWhenIdle){
226 liveuser 939
 
3 liveuser 940
									#設置給予 funcToRunWhenIdle 的參數
941
									$parasmForFuncToRunWhenIdle[$arguKeyForFuncToRunWhenIdle]=$arguValueForFuncToRunWhenIdle;
226 liveuser 942
 
3 liveuser 943
									#debug
944
									#var_dump(__LINE__,$parasmForFuncToRunWhenIdle);
226 liveuser 945
 
3 liveuser 946
									}#foreach end
226 liveuser 947
 
3 liveuser 948
								}#if end
226 liveuser 949
 
3 liveuser 950
							/* debug
226 liveuser 951
 
3 liveuser 952
							#函式說明:
953
							#撰寫log
954
							#回傳結果:
955
							#$result["status"],狀態,"true"或"false".
956
							#$result["error"],錯誤訊息陣列.
957
							#$result["function"],當前函式的名稱.
958
							#$result["argu"],使用的參數.
959
							#$result["content"],要寫入log的內容字串.
960
							#必填參數:
961
							#$conf["path"],字串,log檔案的路徑與名稱.
962
							$conf["logs::record"]["path"]=$conf["sock"].".log";
963
							#$conf["content"],any,要寫的內容,若內容不為字串則會用var_dump的格式寫入.
964
							$conf["logs::record"]["content"]=$parasmForFuncToRunWhenIdle;
965
							#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
966
							$conf["logs::record"]["fileArgu"]=__FILE__;
967
							#可省略參數:
968
							#$conf["rewrite"],預設為"false",接續寫入;反之"true"代表重新寫入.
969
							#$conf["rewrite"]="false";
970
							#$conf["returnOnly"],預設為"false",會寫入到log檔案.若為"true"則不會寫入log.
971
							#$conf["returnOnly"]="true";
972
							#參考資料:
973
							#無.
974
							#備註:
975
							#無.
976
							$record=logs::record($conf["logs::record"]);
977
							unset($conf["logs::record"]);
226 liveuser 978
 
3 liveuser 979
							#如果執行失敗
980
							if($record["status"]==="false"){
226 liveuser 981
 
3 liveuser 982
								#設置執行錯誤識別
983
								$result["status"]="false";
226 liveuser 984
 
3 liveuser 985
								#設置執行錯誤
986
								$result["error"]=$record;
226 liveuser 987
 
3 liveuser 988
								#回傳結果
989
								return $result;
226 liveuser 990
 
3 liveuser 991
								}#if end
226 liveuser 992
 
3 liveuser 993
							*/
226 liveuser 994
 
3 liveuser 995
							#debug
996
							#var_dump(__LINE__,$parasmForFuncToRunWhenIdle);
226 liveuser 997
 
3 liveuser 998
							#debug
999
							#var_dump(__LINE__,"\$call_user_func=".$funcToRunWhenIdle."(\$parasmForFuncToRunWhenIdle);");
226 liveuser 1000
 
3 liveuser 1001
							#呼叫 $funcToRunWhenIdle 函式,參數用 $client[$request->id], 取得執行後的回應.
1002
							eval("\$call_user_func=".$funcToRunWhenIdle."(\$parasmForFuncToRunWhenIdle);");
226 liveuser 1003
 
3 liveuser 1004
							/* debug
226 liveuser 1005
 
3 liveuser 1006
							#函式說明:
1007
							#撰寫log
1008
							#回傳結果:
1009
							#$result["status"],狀態,"true"或"false".
1010
							#$result["error"],錯誤訊息陣列.
1011
							#$result["function"],當前函式的名稱.
1012
							#$result["argu"],使用的參數.
1013
							#$result["content"],要寫入log的內容字串.
1014
							#必填參數:
1015
							#$conf["path"],字串,log檔案的路徑與名稱.
1016
							$conf["logs::record"]["path"]=$conf["sock"].".log";
1017
							#$conf["content"],any,要寫的內容,若內容不為字串則會用var_dump的格式寫入.
1018
							$conf["logs::record"]["content"]=$call_user_func;
1019
							#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
1020
							$conf["logs::record"]["fileArgu"]=__FILE__;
1021
							#可省略參數:
1022
							#$conf["rewrite"],預設為"false",接續寫入;反之"true"代表重新寫入.
1023
							#$conf["rewrite"]="false";
1024
							#$conf["returnOnly"],預設為"false",會寫入到log檔案.若為"true"則不會寫入log.
1025
							#$conf["returnOnly"]="true";
1026
							#參考資料:
1027
							#無.
1028
							#備註:
1029
							#無.
1030
							$record=logs::record($conf["logs::record"]);
1031
							unset($conf["logs::record"]);
226 liveuser 1032
 
3 liveuser 1033
							#如果執行失敗
1034
							if($record["status"]==="false"){
226 liveuser 1035
 
3 liveuser 1036
								#設置執行錯誤識別
1037
								$result["status"]="false";
226 liveuser 1038
 
3 liveuser 1039
								#設置執行錯誤
1040
								$result["error"]=$record;
226 liveuser 1041
 
3 liveuser 1042
								#回傳結果
1043
								return $result;
226 liveuser 1044
 
3 liveuser 1045
								}#if end
226 liveuser 1046
 
1047
							*/
1048
 
3 liveuser 1049
							#儲存 $funcToRunWhenIdle 函式 的運行結果資訊到 server side 的 cache
1050
							$serverCache["serverSide"]["funcToRunWhenIdle"][$funcToRunWhenIdle]["result"]=&$call_user_func;
226 liveuser 1051
 
3 liveuser 1052
							#儲存 $funcToRunWhenIdle 函式 的運行結束的時間點到 server side 的 cache
1053
							$serverCache["serverSide"]["funcToRunWhenIdle"][$funcToRunWhenIdle]["lastEndTime"]=time::unix()["content"];
226 liveuser 1054
 
3 liveuser 1055
							#debug
1056
							#var_dump(__FILE__,__LINE__,$serverCache);
226 liveuser 1057
 
3 liveuser 1058
							#如果 運行 $funcToRunWhenIdle 後回傳的內容含有 key 為 status 的元素
1059
							if(isset($call_user_func["status"])){
226 liveuser 1060
 
3 liveuser 1061
								#如果其數值為 "false"
1062
								if($call_user_func["status"]==="false"){
226 liveuser 1063
 
3 liveuser 1064
									#印出訊息,結束執行
1065
									var_dump(__FILE__,__LINE__,$call_user_func);exit;
226 liveuser 1066
 
3 liveuser 1067
									}#if end
226 liveuser 1068
 
3 liveuser 1069
								}#if end
226 liveuser 1070
 
3 liveuser 1071
							#如果有訊息要提示
1072
							if(isset($call_user_func["content"])){
226 liveuser 1073
 
3 liveuser 1074
								#印出提示訊息
1075
								var_dump($call_user_func["content"]);
226 liveuser 1076
 
3 liveuser 1077
								}#if end
226 liveuser 1078
 
3 liveuser 1079
							#如果 運行 $funcToRunWhenIdle 後回傳的內容含有 key 為 cache 的元素
1080
							if(isset($call_user_func["cache"])){
226 liveuser 1081
 
3 liveuser 1082
								#debug
1083
								#var_dump(__FILE__,__LINE__,$call_user_func["cache"]);
226 liveuser 1084
 
3 liveuser 1085
								#儲存到 server side 的 cache
1086
								$serverCache["serverSide"]["funcToRunWhenIdle"][$funcToRunWhenIdle]["cache"]=&$call_user_func["cache"];
226 liveuser 1087
 
3 liveuser 1088
								/*
1089
								#debug
1090
								#函式說明:
1091
								#撰寫log
1092
								#回傳結果:
1093
								#$result["status"],狀態,"true"或"false".
1094
								#$result["error"],錯誤訊息陣列.
1095
								#$result["function"],當前函式的名稱.
1096
								#$result["argu"],使用的參數.
1097
								#$result["content"],要寫入log的內容字串.
1098
								#必填參數:
1099
								#$conf["path"],字串,log檔案的路徑與名稱.
1100
								$conf["logs::record"]["path"]=$conf["sock"].".log";
1101
								#$conf["content"],any,要寫的內容,若內容不為字串則會用var_dump的格式寫入.
1102
								$conf["logs::record"]["content"]=$call_user_func;
1103
								#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
1104
								$conf["logs::record"]["fileArgu"]=__FILE__;
1105
								#可省略參數:
1106
								#$conf["rewrite"],預設為"false",接續寫入;反之"true"代表重新寫入.
1107
								#$conf["rewrite"]="false";
1108
								#$conf["returnOnly"],預設為"false",會寫入到log檔案.若為"true"則不會寫入log.
1109
								#$conf["returnOnly"]="true";
1110
								#參考資料:
1111
								#無.
1112
								#備註:
1113
								#無.
1114
								$record=logs::record($conf["logs::record"]);
1115
								unset($conf["logs::record"]);
226 liveuser 1116
 
3 liveuser 1117
								#如果執行失敗
1118
								if($record["status"]==="false"){
226 liveuser 1119
 
3 liveuser 1120
									#設置執行錯誤識別
1121
									$result["status"]="false";
226 liveuser 1122
 
3 liveuser 1123
									#設置執行錯誤
1124
									$result["error"]=$record;
226 liveuser 1125
 
3 liveuser 1126
									#回傳結果
1127
									return $result;
226 liveuser 1128
 
3 liveuser 1129
									}#if end
1130
								*/
226 liveuser 1131
 
3 liveuser 1132
								}#if end
226 liveuser 1133
 
3 liveuser 1134
							#如果回傳結果含有 exit 元素
1135
							if(isset($call_user_func["exit"])){
226 liveuser 1136
 
3 liveuser 1137
								/*
1138
								#debug
1139
								#函式說明:
1140
								#撰寫log
1141
								#回傳結果:
1142
								#$result["status"],狀態,"true"或"false".
1143
								#$result["error"],錯誤訊息陣列.
1144
								#$result["function"],當前函式的名稱.
1145
								#$result["argu"],使用的參數.
1146
								#$result["content"],要寫入log的內容字串.
1147
								#必填參數:
1148
								#$conf["path"],字串,log檔案的路徑與名稱.
1149
								$conf["logs::record"]["path"]=$conf["sock"].".log";
1150
								#$conf["content"],any,要寫的內容,若內容不為字串則會用var_dump的格式寫入.
1151
								$conf["logs::record"]["content"]=$call_user_func["exit"];
1152
								#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
1153
								$conf["logs::record"]["fileArgu"]=__FILE__;
1154
								#可省略參數:
1155
								#$conf["rewrite"],預設為"false",接續寫入;反之"true"代表重新寫入.
1156
								#$conf["rewrite"]="false";
1157
								#$conf["returnOnly"],預設為"false",會寫入到log檔案.若為"true"則不會寫入log.
1158
								#$conf["returnOnly"]="true";
1159
								#參考資料:
1160
								#無.
1161
								#備註:
1162
								#無.
1163
								$record=logs::record($conf["logs::record"]);
1164
								unset($conf["logs::record"]);
226 liveuser 1165
 
3 liveuser 1166
								#如果執行失敗
1167
								if($record["status"]==="false"){
226 liveuser 1168
 
3 liveuser 1169
									#設置執行錯誤識別
1170
									$result["status"]="false";
226 liveuser 1171
 
3 liveuser 1172
									#設置執行錯誤
1173
									$result["error"]=$record;
226 liveuser 1174
 
3 liveuser 1175
									#回傳結果
1176
									return $result;
226 liveuser 1177
 
3 liveuser 1178
									}#if end
1179
								*/
226 liveuser 1180
 
3 liveuser 1181
								#如果是要結束daemon執行
1182
								if($call_user_func["exit"]==="true"){
226 liveuser 1183
 
3 liveuser 1184
									#設置daemon結束的原因
1185
									$call_user_func["exit"]="exit by funcToRunWhenIdle(".$funcToRunWhenIdle.")";
226 liveuser 1186
 
3 liveuser 1187
									#設置執行正常
1188
									$result["status"]="true";
226 liveuser 1189
 
3 liveuser 1190
									#結束執行
1191
									return $result;
226 liveuser 1192
 
3 liveuser 1193
									}#if end
226 liveuser 1194
 
3 liveuser 1195
								}#if end
226 liveuser 1196
 
3 liveuser 1197
							#提示正要執行的函式
1198
							echo "response:".PHP_EOL.$funcToRunWhenIdle." Executed.".PHP_EOL;
226 liveuser 1199
 
3 liveuser 1200
							}#foreach end
226 liveuser 1201
 
3 liveuser 1202
						}#if end
226 liveuser 1203
 
3 liveuser 1204
					#等待下個要求
1205
					continue;
226 liveuser 1206
 
3 liveuser 1207
					}#if end
226 liveuser 1208
 
3 liveuser 1209
				#有偵測到讀寫的動作
1210
				else{
226 liveuser 1211
 
3 liveuser 1212
					#針對每個 sock 陣列
1213
					foreach($read as $readStream){
226 liveuser 1214
 
3 liveuser 1215
						#如果元素是 listen 的 socket
1216
						if($readStream === $socket){
226 liveuser 1217
 
3 liveuser 1218
							#當等待別人連線時,若有人連線進來.
1219
							$conn = stream_socket_accept($socket);
226 liveuser 1220
 
3 liveuser 1221
							#取得 meta data
1222
							#$stream_meta_data=stream_get_meta_data($conn);
226 liveuser 1223
 
3 liveuser 1224
							#debug
1225
							#var_dump(__LINE__,$stream_meta_data);
226 liveuser 1226
 
3 liveuser 1227
							#印出有連線進來的訊息
1228
							echo "received a connection".PHP_EOL;
226 liveuser 1229
 
3 liveuser 1230
							#初始化取得的內容
1231
							$request="";
226 liveuser 1232
 
3 liveuser 1233
							#初始化要回應的內容
1234
							$res=array();
226 liveuser 1235
 
3 liveuser 1236
							#取得 request 的時間
1237
							$last_req_time=time();
226 liveuser 1238
 
3 liveuser 1239
							#當有要求時
1240
							while(!feof($conn)){
226 liveuser 1241
 
3 liveuser 1242
								#讀取要求
1243
								$tmp=fgets($conn, 1024);
226 liveuser 1244
 
3 liveuser 1245
								#如果讀到 PHP_EOL
1246
								if($tmp===PHP_EOL){
226 liveuser 1247
 
3 liveuser 1248
									#結束讀取要求
226 liveuser 1249
									break;
1250
 
3 liveuser 1251
									}#if end
226 liveuser 1252
 
3 liveuser 1253
								#串接回應
1254
								$request=$request.$tmp;
226 liveuser 1255
 
183 liveuser 1256
								}#while end
226 liveuser 1257
 
3 liveuser 1258
							#印出有連線進來的訊息
1259
							echo "received content:".PHP_EOL.$request.PHP_EOL;
226 liveuser 1260
 
3 liveuser 1261
							#parse request json
1262
							$request=json_decode($request);
226 liveuser 1263
 
3 liveuser 1264
							#如果 parse json 失敗
1265
							if($request===false){
226 liveuser 1266
 
3 liveuser 1267
								#設置錯誤訊息
1268
								$res["error"]="要求格式必須為json";
226 liveuser 1269
 
3 liveuser 1270
								#設置執行失敗
1271
								$res["status"]="false";
226 liveuser 1272
 
3 liveuser 1273
								#印出回的訊息
1274
								echo "res:".print_r($res,true);
226 liveuser 1275
 
3 liveuser 1276
								#寫入回應的內容
1277
								fwrite($conn,json_encode($res).PHP_EOL);
226 liveuser 1278
 
3 liveuser 1279
								#結束回應
1280
								fwrite($conn,PHP_EOL.PHP_EOL);
226 liveuser 1281
 
3 liveuser 1282
								#關閉socket連線,將回應輸出.
1283
								fclose($conn);
226 liveuser 1284
 
3 liveuser 1285
								#等待下個要求
1286
								continue;
226 liveuser 1287
 
3 liveuser 1288
								}#if end
226 liveuser 1289
 
3 liveuser 1290
							#若無ID資訊
1291
							if(!isset($request->id)){
226 liveuser 1292
 
3 liveuser 1293
								#無窮迴圈
1294
								while(true){
226 liveuser 1295
 
3 liveuser 1296
									#set client id 設置要回應的訊息
1297
									#函式說明:
1298
									#呼叫shell執行系統命令,並取得回傳的內容.
1299
									#回傳結果:
1300
									#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1301
									#$result["error"],錯誤訊息陣列.
1302
									#$result["function"],當前執行的函數名稱.
1303
									#$result["argu"],使用的參數.
1304
									#$result["cmd"],執行的指令內容.
1305
									#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
1306
									#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
1307
									#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
1308
									#$result["running"],是否還在執行.
1309
									#$result["pid"],pid.
1310
									#$result["statusCode"],執行結束後的代碼.
1311
									#必填參數:
1312
									#$conf["command"],字串,要執行的指令.
1313
									$conf["external::callShell"]["command"]="uuid";
1314
									#$conf["fileArgu"],字串,變數__FILE__的內容.
1315
									$conf["external::callShell"]["fileArgu"]=__FILE__;
1316
									#可省略參數:
1317
									#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
1318
									#$conf["argu"]=array("");
1319
									#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
1320
									#$conf["arguIsAddr"]=array();
1321
									#$conf["plainArgu"],字串陣列,哪幾個參數不要加上"",若為"true"則代表不用包;反之"false"則代表要包.
1322
									#$conf["plainArgu"]=array();
1323
									#$conf["useApostrophe"],字串陣列,如果有需要包住,則用「'」,而非「"」處理.前者為"true";後者為"false".
1324
									#$conf["useApostrophe"]=array();
1325
									#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
1326
									#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
1327
									#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
1328
									#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
1329
									#$conf["enablePrintDescription"]="true";
1330
									#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
1331
									#$conf["printDescription"]="";
1332
									#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
1333
									$conf["external::callShell"]["escapeshellarg"]="true";
1334
									#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
1335
									#$conf["thereIsShellVar"]=array();
1336
									#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
1337
									#$conf["username"]="";
1338
									#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
1339
									#$conf["password"]="";
1340
									#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
1341
									#$conf["useScript"]="";
1342
									#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
1343
									#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
1344
									#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
1345
									#$conf["inBackGround"]="";
1346
									#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
1347
									#$conf["getErr"]="false";
1348
									#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
1349
									#$conf["doNotRun"]="false";
1350
									#參考資料:
1351
									#exec=>http://php.net/manual/en/function.exec.php
1352
									#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
1353
									#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
1354
									#備註:
1355
									#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
1356
									#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
1357
									$callShell=external::callShell($conf["external::callShell"]);
1358
									unset($conf["external::callShell"]);
226 liveuser 1359
 
3 liveuser 1360
									#如果執行失敗
1361
									if($callShell["status"]==="false"){
226 liveuser 1362
 
3 liveuser 1363
										#設置執行失敗
1364
										$result["status"]="false";
226 liveuser 1365
 
3 liveuser 1366
										#設置錯誤訊息
1367
										$result["error"]=$callShell;
226 liveuser 1368
 
3 liveuser 1369
										#回傳結果
226 liveuser 1370
										return $result;
1371
 
3 liveuser 1372
										}#if end
226 liveuser 1373
 
3 liveuser 1374
									#如果沒有預期的 uuid 回應
1375
									if(!isset($callShell["output"][0])){
226 liveuser 1376
 
3 liveuser 1377
										#設置執行失敗
1378
										$result["status"]="false";
226 liveuser 1379
 
3 liveuser 1380
										#設置錯誤訊息
1381
										$result["error"][]="get uuid failed!";
226 liveuser 1382
 
3 liveuser 1383
										#設置錯誤訊息
1384
										$result["error"][]=$callShell;
226 liveuser 1385
 
3 liveuser 1386
										#回傳結果
226 liveuser 1387
										return $result;
1388
 
3 liveuser 1389
										}#if end
226 liveuser 1390
 
3 liveuser 1391
									#檢查 client id 是否已經存在
1392
									if(isset($client[$callShell["output"][0]])){
226 liveuser 1393
 
3 liveuser 1394
										#休息一微秒
1395
										usleep(1);
226 liveuser 1396
 
3 liveuser 1397
										#再次建立id試試
1398
										continue;
226 liveuser 1399
 
3 liveuser 1400
										}#if end
226 liveuser 1401
 
3 liveuser 1402
									#設置回應的id
1403
									$res["id"]=$callShell["output"][0];
226 liveuser 1404
 
3 liveuser 1405
									#建立id完畢
1406
									break;
226 liveuser 1407
 
3 liveuser 1408
									}#while end
226 liveuser 1409
 
3 liveuser 1410
								#儲存使用者id與最後一次來request的時間
1411
								$client[$res["id"]]=array("last_req_time"=>$last_req_time);
226 liveuser 1412
 
3 liveuser 1413
								#印出用戶端列表
1414
								echo "clients:".print_r($client,true);
226 liveuser 1415
 
3 liveuser 1416
								#設置執行正常
1417
								$res["status"]="true";
226 liveuser 1418
 
3 liveuser 1419
								#印出回的訊息
1420
								echo "response:".PHP_EOL.print_r($res,true);
226 liveuser 1421
 
3 liveuser 1422
								#寫入回應的內容
1423
								fwrite($conn,json_encode($res).PHP_EOL);
226 liveuser 1424
 
3 liveuser 1425
								#結束回應
1426
								fwrite($conn,PHP_EOL.PHP_EOL);
226 liveuser 1427
 
3 liveuser 1428
								#關閉socket連線,將回應輸出.
1429
								fclose($conn);
226 liveuser 1430
 
3 liveuser 1431
								#等待下個要求
1432
								continue;
226 liveuser 1433
 
57 liveuser 1434
								}#if end
226 liveuser 1435
 
3 liveuser 1436
							#若ID資訊不存在
1437
							if(!isset($client[$request->id])){
226 liveuser 1438
 
3 liveuser 1439
								#設置執行失敗
1440
								$res["status"]="false";
226 liveuser 1441
 
3 liveuser 1442
								#設置ID不存在的訊息
1443
								$res["error"]="ID:".$request->id." 不存在,請再要一次ID.";
226 liveuser 1444
 
3 liveuser 1445
								#印出回的訊息
1446
								echo "response:".PHP_EOL.print_r($res,true);
226 liveuser 1447
 
3 liveuser 1448
								#寫入回應的內容
1449
								fwrite($conn,json_encode($res).PHP_EOL);
226 liveuser 1450
 
3 liveuser 1451
								#結束回應
1452
								fwrite($conn,PHP_EOL.PHP_EOL);
226 liveuser 1453
 
3 liveuser 1454
								#關閉socket連線,將回應輸出.
1455
								fclose($conn);
226 liveuser 1456
 
3 liveuser 1457
								#等待下個要求
1458
								continue;
226 liveuser 1459
 
3 liveuser 1460
								}#if end
226 liveuser 1461
 
3 liveuser 1462
							#如果有客製化訊息
1463
							if(isset($request->custom)){
226 liveuser 1464
 
3 liveuser 1465
								#取代之
1466
								$request=$request->custom;
226 liveuser 1467
 
3 liveuser 1468
								}#if end
226 liveuser 1469
 
3 liveuser 1470
							#若有要執行的指令 cmd,且非客製化.
183 liveuser 1471
							if(isset($request->cmd)){
226 liveuser 1472
 
3 liveuser 1473
								#如果是要結束連線
1474
								if($request->cmd==="quit"){
226 liveuser 1475
 
3 liveuser 1476
									#設置執行正常
1477
									$res["status"]="true";
226 liveuser 1478
 
3 liveuser 1479
									#設置訊息
1480
									$res["content"]="將關閉與您的連線";
226 liveuser 1481
 
3 liveuser 1482
									#寫入回應的內容
1483
									fwrite($conn,json_encode($res).PHP_EOL);
226 liveuser 1484
 
3 liveuser 1485
									#結束回應
1486
									fwrite($conn,PHP_EOL.PHP_EOL);
226 liveuser 1487
 
3 liveuser 1488
									#關閉socket連線,將回應輸出.
1489
									fclose($conn);
226 liveuser 1490
 
3 liveuser 1491
									#移除該用戶變數
1492
									unset($client[$index]);
226 liveuser 1493
 
3 liveuser 1494
									#計數移除了幾個用戶
1495
									$res["delCount"]++;
226 liveuser 1496
 
3 liveuser 1497
									#設置執行正常
1498
									$res["status"]="true";
226 liveuser 1499
 
3 liveuser 1500
									#印出回的訊息
1501
									echo "response:".PHP_EOL.print_r($res,true);
226 liveuser 1502
 
3 liveuser 1503
									#等待下個要求
1504
									continue;
226 liveuser 1505
 
3 liveuser 1506
									}#if end
226 liveuser 1507
 
3 liveuser 1508
								#如果是要結束server
1509
								if($request->cmd===$shutdownStr){
226 liveuser 1510
 
339 liveuser 1511
									#如果有設置 shutdownCallback
1512
									if(isset($conf["shutdownCallback"])){
1513
 
1514
										#初始化要作為 $conf["shutdownCallback"] 函式的陣列參數
1515
										$shutdownCallbackArgu=array();
1516
 
1517
										#設置 serveCache 參數
1518
										$shutdownCallbackArgu["serverCache"]=$result["serverCache"];
1519
 
1520
										#設置 clientCache 參數
1521
										$shutdownCallbackArgu["clientCache"]=$result["clientCache"];
1522
 
1523
										#設置 info 參數
1524
										$shutdownCallbackArgu["info"]=$conf["infoToFunction"];
1525
 
1526
										#呼叫 $addOnProcessFunc 函式,參數用 $shutdownCallbackArgu
1527
										eval("\$call_user_func=".$conf["shutdownCallback"]."(\$shutdownCallbackArgu);");
1528
 
1529
										}#if end
1530
 
3 liveuser 1531
									#設置執行正常
1532
									$res["status"]="true";
226 liveuser 1533
 
3 liveuser 1534
									#設置訊息
1535
									$res["content"]="本服務將結束";
226 liveuser 1536
 
3 liveuser 1537
									#寫入回應的內容
1538
									fwrite($conn,json_encode($res).PHP_EOL);
226 liveuser 1539
 
3 liveuser 1540
									#結束回應
1541
									fwrite($conn,PHP_EOL.PHP_EOL);
226 liveuser 1542
 
3 liveuser 1543
									#關閉socket連線,將回應輸出.
1544
									fclose($conn);
226 liveuser 1545
 
3 liveuser 1546
									#設置執行正常
1547
									$result["status"]="true";
226 liveuser 1548
 
3 liveuser 1549
									#結束本程式
1550
									return $result;
226 liveuser 1551
 
3 liveuser 1552
									}#if end
226 liveuser 1553
 
3 liveuser 1554
								#涵式說明:
1555
								#呼叫shell執行系統命令,並取得回傳的內容.
1556
								#回傳的結果:
1557
								#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1558
								#$result["error"],錯誤訊息陣列.
1559
								#$result["function"],當前執行的函數名稱.
1560
								#$result["argu"],使用的參數.
1561
								#$result["cmd"],執行的指令內容.
1562
								#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
1563
								#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
1564
								#$result["tmpFileOutput"],儲存輸出的暫村檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
1565
								#$result["running"],是否還在執行.
1566
								#$result["pid"],pid
1567
								#必填的參數
1568
								#$conf["command"],字串,要執行的指令與.
1569
								$conf["external::callShell"]["command"]=$request->cmd;
1570
								#$conf["fileArgu"],字串,變數__FILE__的內容.
57 liveuser 1571
								$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];
3 liveuser 1572
								#可省略參數:
226 liveuser 1573
 
3 liveuser 1574
								#如果有指定參數
1575
								if(isset($request->param)){
226 liveuser 1576
 
3 liveuser 1577
									#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
1578
									$conf["external::callShell"]["argu"]=$request->param;
226 liveuser 1579
 
3 liveuser 1580
									}#if end
226 liveuser 1581
 
3 liveuser 1582
								#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
226 liveuser 1583
								#$conf["arguIsAddr"]=array();
3 liveuser 1584
								#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
1585
								#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
1586
								#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
1587
								#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
1588
								#$conf["enablePrintDescription"]="true";
1589
								#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容.
1590
								#$conf["printDescription"]="";
226 liveuser 1591
 
3 liveuser 1592
								#如果有參數
1593
								if(isset($request->param)){
226 liveuser 1594
 
3 liveuser 1595
									#如果有識別是否已經 escaped 參數
1596
									if(isset($request->escaped)){
226 liveuser 1597
 
3 liveuser 1598
										#設置預設要 escape
1599
										$needEscaped="true";
226 liveuser 1600
 
3 liveuser 1601
										#如果已經 ecaped
259 liveuser 1602
										if($request->escaped==="true"){
226 liveuser 1603
 
3 liveuser 1604
											#設置不用 escape
1605
											$needEscaped="false";
226 liveuser 1606
 
3 liveuser 1607
											}#if end
226 liveuser 1608
 
3 liveuser 1609
										#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".
1610
										$conf["external::callShell"]["escapeshellarg"]=$needEscaped;
226 liveuser 1611
 
3 liveuser 1612
										}#if end
226 liveuser 1613
 
3 liveuser 1614
									}#if end
226 liveuser 1615
 
3 liveuser 1616
								#如果有設置要用特定的使用者身份執行
1617
								if(isset($request->user) && isset($request->password)){
226 liveuser 1618
 
3 liveuser 1619
									#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
1620
									$conf["external::callShell"]["username"]=$request->user;
226 liveuser 1621
 
3 liveuser 1622
									#$conf["password"],字串,與$conf["username"]搭配的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
1623
									$conf["external::callShell"]["password"]=$request->password;
226 liveuser 1624
 
3 liveuser 1625
									}#if end
226 liveuser 1626
 
3 liveuser 1627
								#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
1628
								#$conf["useScript"]="";
1629
								#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
1630
								#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
1631
								#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
1632
								#$conf["inBackGround"]="";
1633
								#備註:
1634
								#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
1635
								#參考資料:
1636
								#exec=>http://php.net/manual/en/function.exec.php
1637
								#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
1638
								#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
1639
								$callShell=external::callShell($conf["external::callShell"]);
1640
								unset($conf["external::callShell"]);
226 liveuser 1641
 
3 liveuser 1642
								#設置要回覆的內容
1643
								$res=$callShell;
226 liveuser 1644
 
3 liveuser 1645
								#印出回的訊息
1646
								echo "response:".PHP_EOL.print_r($res,true);
226 liveuser 1647
 
3 liveuser 1648
								#寫入回應的內容
1649
								fwrite($conn,json_encode($res).PHP_EOL);
226 liveuser 1650
 
3 liveuser 1651
								#結束回應
1652
								fwrite($conn,PHP_EOL.PHP_EOL);
226 liveuser 1653
 
3 liveuser 1654
								#關閉socket連線,將回應輸出.
1655
								fclose($conn);
226 liveuser 1656
 
3 liveuser 1657
								#更新使用者id與最後一次來request的時間
226 liveuser 1658
								$client[$request->id]["last_req_time"]=$last_req_time;
1659
 
3 liveuser 1660
								#等待下個要求
1661
								continue;
226 liveuser 1662
 
3 liveuser 1663
								}#if end
226 liveuser 1664
 
3 liveuser 1665
							#設置接收到json字串資訊
1666
							$client[$request->id]["request"]=(array)($request);
226 liveuser 1667
 
3 liveuser 1668
							#設置用戶端的socket
1669
							$client[$request->id]["socket"]=$conn;
226 liveuser 1670
 
3 liveuser 1671
							#如果有要求在背景中執行指令的訊息
1672
							if(isset($request->cmdInBg)){
226 liveuser 1673
 
3 liveuser 1674
								#debug
1675
								#var_dump(__FUNCTION__,__LINE__,$request);
226 liveuser 1676
 
3 liveuser 1677
								#如果有參數
1678
								if(isset($request->param)){
226 liveuser 1679
 
3 liveuser 1680
									#函式說明:
1681
									#將一維陣列轉換為用特定符號間隔的字串,ex:array("1","2","3") to "a;b;c;".
1682
									#回傳的結果:
1683
									#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1684
									#$result["function"],當前執行的function名稱
1685
									#$result["error"],錯誤訊息陣列.
1686
									#$result["content"],處理好的字串.
1687
									#$result["argu"],使用的參數.
1688
									#必填參數:
1689
									#$conf["inputArray"],字串陣列,要轉成字串的一維陣列.
1690
									$conf["arrays::arrayToString"]["inputArray"]=$request->param;
1691
									#可省略參數:
1692
									#$conf["spiltSymbol"],字串,用來區隔字串的符號,預設為;
1693
									$conf["arrays::arrayToString"]["spiltSymbol"]=" ";
1694
									#$conf["skipEnd"],字串,結尾是否不要加上符號,預設為"false",要加上符號,"true"代表不要加上符號。
1695
									$conf["arrays::arrayToString"]["skipEnd"]="true";
1696
									#$conf["spiltSymbolAtStart"],字串,是否要在開頭加上spiltSymbol,預設為"false",代表不要;反之為“true”.
1697
									$conf["arrays::arrayToString"]["spiltSymbolAtStart"]="true";
1698
									#參考資料:
1699
									#無.
1700
									#備註:
1701
									#無.
1702
									$arrayToString=arrays::arrayToString($conf["arrays::arrayToString"]);
1703
									unset($conf["arrays::arrayToString"]);
226 liveuser 1704
 
3 liveuser 1705
									#如果執行失敗
1706
									if($arrayToString["status"]==="false"){
226 liveuser 1707
 
3 liveuser 1708
										#設置執行失敗
1709
										$result["status"]="false";
226 liveuser 1710
 
3 liveuser 1711
										#設置錯誤訊息
1712
										$result["error"]=$arrayToString;
226 liveuser 1713
 
3 liveuser 1714
										#回傳結果
1715
										return $result;
226 liveuser 1716
 
3 liveuser 1717
										}#if end
226 liveuser 1718
 
3 liveuser 1719
									#串接參數
1720
									$request->cmdInBg=$request->cmdInBg.$arrayToString["content"];
226 liveuser 1721
 
3 liveuser 1722
									}#if end
226 liveuser 1723
 
3 liveuser 1724
								#函式說明:
1725
								#透過proc來多執行序運作.
1726
								#回傳結果:
1727
								#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1728
								#$reuslt["error"],執行不正常結束的錯訊息陣列.
1729
								#$result["function"],當前執行的函式名稱.
1730
								#$result["argu"],使用的參數.
1731
								#$result["content"],陣列,每個元素為其指令執行的結果訊息陣列,key為"status"代表執行是否正常的識別;key為"statusCode"代表程式結束後回傳給對應executeBy程式的數值;key為"output"代表標準輸出,若為resource,則代表為pipe;key為"error"代表非標準輸出,若為resource,則代表為pipe;key為"input"代表成功輸入的指令;key為"process"代表該程序經proc_open後的process source;key為"proc_get_status"代表程序的資訊.
1732
								#必填參數:
1733
								#$conf["cmds"],字串陣列,每個元素代表要執行的指令與參數.
1734
								$conf["threads::proc"]["cmds"]=array($request->cmdInBg);
1735
								#可省略參數:
1736
								#$conf["wait"],字串,是否需要等待所有程序結束,預設為"true"要等待;反之為"false"不要等待.
1737
								$conf["threads::proc"]["wait"]="false";
1738
								#$conf["workingDir"],字串陣列,個別程式執行時的家目錄,預設不指定.
1739
								#$conf["workingDir"]=array("path");
1740
								#$conf["envs"],2維字串陣列,每個元素代表個別程式執行時的指定環境變數,key變數名稱;value為變數內容.預設為array("QBPWCF" => "Quick Build PHP Website Componment base on Fedora Linux");
1741
								#$conf["envs"]=array(array("key"=>"value"));
1742
								#$conf["executeBy"],字串陣列,每個元素代表個別指令要用什麼程式執行,預設為"bash".
1743
								#$conf["executeBy"]=array("bash");
1744
								#參考資料:
1745
								#https://www.php.net/manual/en/function.proc-open.php
1746
								#https://www.php.net/manual/en/function.proc-get-status.php
1747
								#備註:
1748
								#若需要取得當下的執行狀況,請使用 self::proc_update 來更新.
1749
								$proc=threads::proc($conf["threads::proc"]);
1750
								unset($conf["threads::proc"]);
226 liveuser 1751
 
3 liveuser 1752
								#如果執行失敗
1753
								if($proc["status"]==="false"){
226 liveuser 1754
 
3 liveuser 1755
									#設置執行失敗
1756
									$result["status"]="false";
226 liveuser 1757
 
3 liveuser 1758
									#設置錯誤訊息
1759
									$result["error"]=$proc;
226 liveuser 1760
 
3 liveuser 1761
									#回傳結果
1762
									return $result;
226 liveuser 1763
 
3 liveuser 1764
									}#if end
226 liveuser 1765
 
3 liveuser 1766
								#函式說明:
1767
								#使用 linux 的 uuid 指令來產生 uuid 字串
1768
								#回傳結果:
1769
								#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1770
								#$result["error"],錯誤訊息.
1771
								#$result["function"],當前執行的函式名稱.
1772
								#$result["content"],uuid.
1773
								#必填參數:
1774
								#無.
1775
								#可省略參數:
1776
								#無.
1777
								#參考資料:
1778
								#無.
1779
								#備註:
1780
								#無.
1781
								$uuid=cmd::uuid();
226 liveuser 1782
 
3 liveuser 1783
								#如果執行失敗
1784
								if($uuid["status"]==="false"){
226 liveuser 1785
 
3 liveuser 1786
									#設置執行失敗
1787
									$result["status"]="false";
226 liveuser 1788
 
3 liveuser 1789
									#設置錯誤訊息
1790
									$result["error"]=$uuid;
226 liveuser 1791
 
3 liveuser 1792
									#回傳結果
1793
									return $result;
226 liveuser 1794
 
3 liveuser 1795
									}#if end
226 liveuser 1796
 
3 liveuser 1797
								#取得 uuid
1798
								$uuid=$uuid["content"];
226 liveuser 1799
 
3 liveuser 1800
								#儲存到 server side 的 procs
1801
								$result["serverCache"]["serverSide"]["procs"][$uuid]=$proc;
226 liveuser 1802
 
3 liveuser 1803
								#設置最後的檢查時間為當下
1804
								$result["serverCache"]["serverSide"]["procs"][$uuid]["latestAccessTime"]=time();
226 liveuser 1805
 
3 liveuser 1806
								#設置要回覆的內容
1807
								$res=array("status"=>"true","content"=>$uuid);
226 liveuser 1808
 
3 liveuser 1809
								#印出回的訊息
1810
								echo "response:".PHP_EOL.print_r($res,true);
226 liveuser 1811
 
3 liveuser 1812
								#寫入回應的內容
1813
								fwrite($conn,json_encode($res).PHP_EOL);
226 liveuser 1814
 
3 liveuser 1815
								#結束回應
1816
								fwrite($conn,PHP_EOL.PHP_EOL);
226 liveuser 1817
 
3 liveuser 1818
								#關閉socket連線,將回應輸出.
1819
								fclose($conn);
226 liveuser 1820
 
3 liveuser 1821
								#更新使用者id與最後一次來request的時間
1822
								$client[$request->id]["last_req_time"]=$last_req_time;
226 liveuser 1823
 
3 liveuser 1824
								#等待下個要求
1825
								continue;
226 liveuser 1826
 
3 liveuser 1827
								}#if end
226 liveuser 1828
 
3 liveuser 1829
							#如果要查詢程序執行的狀況
1830
							if(isset($request->QueryProcByUUID)){
226 liveuser 1831
 
3 liveuser 1832
								#如果沒有 proc 資訊
1833
								if(!isset($result["serverCache"]["serverSide"]["procs"])){
226 liveuser 1834
 
3 liveuser 1835
									#設置要回覆的內容
1836
									$res=array("status"=>"true","content"=>"");
226 liveuser 1837
 
3 liveuser 1838
									#印出回的訊息
1839
									echo "response:".PHP_EOL.print_r($res,true);
226 liveuser 1840
 
3 liveuser 1841
									#寫入回應的內容
1842
									fwrite($conn,json_encode($res).PHP_EOL);
226 liveuser 1843
 
3 liveuser 1844
									#結束回應
1845
									fwrite($conn,PHP_EOL.PHP_EOL);
226 liveuser 1846
 
3 liveuser 1847
									#關閉socket連線,將回應輸出.
1848
									fclose($conn);
226 liveuser 1849
 
3 liveuser 1850
									#更新使用者id與最後一次來request的時間
1851
									$client[$request->id]["last_req_time"]=$last_req_time;
226 liveuser 1852
 
3 liveuser 1853
									#等待下個要求
1854
									continue;
226 liveuser 1855
 
3 liveuser 1856
									}#if end
226 liveuser 1857
 
3 liveuser 1858
								#函式說明:
1859
								#更新透過proc執行的多程序資訊.
1860
								#回傳結果:
1861
								#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1862
								#$reuslt["error"],執行不正常結束的錯訊息陣列.
1863
								#$result["function"],當前執行的函式名稱.
1864
								#$result["argu"],使用的參數.
1865
								#$result["content"],陣列,每個元素為其指令執行的結果訊息陣列,key為"status"代表執行是否正常的識別;key為"statusCode"代表程式結束後回傳給對應executeBy程式的數值;key為"output"代表標準輸出,若為resource,則代表為pipe;key為"error"代表非標準輸出,若為resource,則代表為pipe;key為"input"代表成功輸入的指令;key為"process"代表該程序經proc_open後的process source;key為"proc_get_status"代表程序的資訊.
1866
								#必填參數:
1867
								#$conf["procs"],陣列,運行self::proc後回傳的content.
1868
								$conf["threads::proc_update"]["procs"]=$result["serverCache"]["serverSide"]["procs"][$request->QueryProcByUUID]["content"];
1869
								#可省略參數:
1870
								#無.
1871
								#參考資料:
1872
								#無.
1873
								#備註:
1874
								#無.
1875
								$proc_update=threads::proc_update($conf["threads::proc_update"]);
1876
								unset($conf["threads::proc_update"]);
226 liveuser 1877
 
3 liveuser 1878
								#如果執行失敗
1879
								if($proc_update["status"]==="false"){
226 liveuser 1880
 
3 liveuser 1881
									#設置執行失敗
1882
									$result["status"]="false";
226 liveuser 1883
 
3 liveuser 1884
									#設置錯誤訊息
1885
									$result["error"]=$proc_update;
226 liveuser 1886
 
3 liveuser 1887
									#回傳結果
1888
									return $result;
226 liveuser 1889
 
3 liveuser 1890
									}#if end
226 liveuser 1891
 
3 liveuser 1892
								#更新 serverSide 的 proc cache
1893
								$result["serverCache"]["serverSide"]["procs"][$request->QueryProcByUUID]["content"]=$proc_update["content"];
226 liveuser 1894
 
3 liveuser 1895
								#設置最後檢查的時間
1896
								$result["serverCache"]["serverSide"]["procs"][$request->QueryProcByUUID]["latestAccessTime"]=time();
226 liveuser 1897
 
3 liveuser 1898
								#印出子程序的資訊
1899
								echo "proc info:".PHP_EOL.print_r($proc_update["content"][0],true);
226 liveuser 1900
 
3 liveuser 1901
								#初始化要回傳的內容
1902
								$res=array();
226 liveuser 1903
 
3 liveuser 1904
								#設置 pid
1905
								$res["pid"]=$proc_update["content"][0]["proc_get_status"]["pid"];
226 liveuser 1906
 
3 liveuser 1907
								#設置輸入的內容
1908
								$res["input"]=$proc_update["content"][0]["input"];
226 liveuser 1909
 
3 liveuser 1910
								#預設設置已經結束執行
1911
								$res["running"]="false";
226 liveuser 1912
 
3 liveuser 1913
								#如果尚在執行
1914
								if($proc_update["content"][0]["statusCode"]==="?"){
226 liveuser 1915
 
3 liveuser 1916
									#設置正在執行
1917
									$res["running"]="true";
226 liveuser 1918
 
3 liveuser 1919
									}#if end
226 liveuser 1920
 
3 liveuser 1921
								#如果已經結束執行
1922
								if($res["running"]==="false"){
226 liveuser 1923
 
3 liveuser 1924
									#設置執行後得到的標準輸出
1925
									$res["output"]=$proc_update["content"][0]["content"];
226 liveuser 1926
 
3 liveuser 1927
									#設置執行後得到的錯誤輸出
1928
									$res["error"]=$proc_update["content"][0]["error"];
226 liveuser 1929
 
3 liveuser 1930
									}#if end
226 liveuser 1931
 
3 liveuser 1932
								#設置回應正常
1933
								$res["status"]="true";
226 liveuser 1934
 
3 liveuser 1935
								#印出回的訊息
1936
								echo "response:".PHP_EOL.print_r($res,true);
226 liveuser 1937
 
3 liveuser 1938
								#寫入回應的內容
1939
								fwrite($conn,json_encode($res).PHP_EOL);
226 liveuser 1940
 
3 liveuser 1941
								#結束回應
1942
								fwrite($conn,PHP_EOL.PHP_EOL);
226 liveuser 1943
 
3 liveuser 1944
								#關閉socket連線,將回應輸出.
1945
								fclose($conn);
226 liveuser 1946
 
3 liveuser 1947
								#更新使用者id與最後一次來request的時間
1948
								$client[$request->id]["last_req_time"]=$last_req_time;
226 liveuser 1949
 
3 liveuser 1950
								#等待下個要求
1951
								continue;
226 liveuser 1952
 
3 liveuser 1953
								}#if end
226 liveuser 1954
 
3 liveuser 1955
							#debug
1956
							#var_dump(__LINE__,$conn,$client[$request->id]);
226 liveuser 1957
 
3 liveuser 1958
							#如果存在 addOnProcessFunc
1959
							if(isset($conf["addOnProcessFunc"])){
226 liveuser 1960
 
3 liveuser 1961
								#針對每個 addOnProcessFunc
1962
								foreach($conf["addOnProcessFunc"] as $addOnProcessFunc){
226 liveuser 1963
 
3 liveuser 1964
									#如果有設置 infoToFunction
1965
									if(isset($conf["infoToFunction"])){
226 liveuser 1966
 
3 liveuser 1967
										#設置info
1968
										$client[$request->id]["info"]=&$conf["infoToFunction"];
226 liveuser 1969
 
3 liveuser 1970
										}#if end
226 liveuser 1971
 
3 liveuser 1972
									#設置client cache
1973
									$client[$request->id]["clientCache"]=&$clientCache;
226 liveuser 1974
 
3 liveuser 1975
									#呼叫 $addOnProcessFunc 函式,參數用 $client[$request->id], 取得執行後的回應.
1976
									eval("\$call_user_func=".$addOnProcessFunc."(\$client[\$request->id]);");
226 liveuser 1977
 
3 liveuser 1978
									/* debug
226 liveuser 1979
 
3 liveuser 1980
									#函式說明:
1981
									#撰寫log
1982
									#回傳結果:
1983
									#$result["status"],狀態,"true"或"false".
1984
									#$result["error"],錯誤訊息陣列.
1985
									#$result["function"],當前函式的名稱.
1986
									#$result["argu"],使用的參數.
1987
									#$result["content"],要寫入log的內容字串.
1988
									#必填參數:
1989
									#$conf["path"],字串,log檔案的路徑與名稱.
1990
									$conf["logs::record"]["path"]=$conf["sock"].".log";
1991
									#$conf["content"],any,要寫的內容,若內容不為字串則會用var_dump的格式寫入.
1992
									$conf["logs::record"]["content"]=$call_user_func;
1993
									#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
1994
									$conf["logs::record"]["fileArgu"]=__FILE__;
1995
									#可省略參數:
1996
									#$conf["rewrite"],預設為"false",接續寫入;反之"true"代表重新寫入.
1997
									#$conf["rewrite"]="false";
1998
									#$conf["returnOnly"],預設為"false",會寫入到log檔案.若為"true"則不會寫入log.
1999
									#$conf["returnOnly"]="true";
2000
									#參考資料:
2001
									#無.
2002
									#備註:
2003
									#無.
2004
									$record=logs::record($conf["logs::record"]);
2005
									unset($conf["logs::record"]);
226 liveuser 2006
 
3 liveuser 2007
									#如果執行失敗
2008
									if($record["status"]==="false"){
226 liveuser 2009
 
3 liveuser 2010
										#設置執行錯誤識別
2011
										$result["status"]="false";
226 liveuser 2012
 
3 liveuser 2013
										#設置執行錯誤
2014
										$result["error"]=$record;
226 liveuser 2015
 
3 liveuser 2016
										#回傳結果
2017
										return $result;
226 liveuser 2018
 
3 liveuser 2019
										}#if end
226 liveuser 2020
 
3 liveuser 2021
									*/
226 liveuser 2022
 
3 liveuser 2023
									#如果回應不是 array
2024
									if(gettype($call_user_func)!=="array"){
226 liveuser 2025
 
3 liveuser 2026
										#設置執行失敗
2027
										$result["status"]="false";
226 liveuser 2028
 
3 liveuser 2029
										#設置錯誤訊息
2030
										$result["error"][]=$call_user_func;
226 liveuser 2031
 
3 liveuser 2032
										#設置錯誤訊息
2033
										$result["error"][]=$conf;
226 liveuser 2034
 
3 liveuser 2035
										#印出error資訊
2036
										echo "error:".print_r($result,true);
226 liveuser 2037
 
3 liveuser 2038
										#回應錯誤訊息給用戶
2039
										fwrite($conn,json_encode($result).PHP_EOL);
226 liveuser 2040
 
3 liveuser 2041
										#結束回應
2042
										fwrite($conn,PHP_EOL.PHP_EOL);
226 liveuser 2043
 
3 liveuser 2044
										#關閉socket連線,將回應輸出.
2045
										fclose($conn);
226 liveuser 2046
 
3 liveuser 2047
										#回傳結果
2048
										return $result;
226 liveuser 2049
 
3 liveuser 2050
										}#if end
226 liveuser 2051
 
3 liveuser 2052
									#檢查回應的格式
2053
									#函式說明:
2054
									#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
2055
									#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
2056
									#$reuslt["error"],執行不正常結束的錯訊息陣列.
2057
									#$result["function"],當前執行的函式名稱.
2058
									#$result["argu"],設置給予的參數.
2059
									#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
2060
									#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
2061
									#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
2062
									#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
2063
									#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
2064
									#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
2065
									#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
2066
									#$result["notNeedVar"],字串陣列,多餘的參數名稱.
2067
									#必填寫的參數:
2068
									#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
226 liveuser 2069
									$conf["variableCheck::checkArguments"]["varInput"]=&$call_user_func;
3 liveuser 2070
									#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
2071
									$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
2072
									#可以省略的參數:
2073
									#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
2074
									$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("status");
2075
									#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
2076
									$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string");
2077
									#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
2078
									$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
2079
									#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
2080
									#$conf["canNotBeEmpty"]=array();
2081
									#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
2082
									#$conf["canBeEmpty"]=array();
2083
									#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
2084
									#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("function","continue");
2085
									#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
2086
									$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("function","continue","content","cache","argu","error");
226 liveuser 2087
									#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 2088
									$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string",null,"array","array","array");
2089
									#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
2090
									$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array($addOnProcessFunc,"true",null,null,null,null);
2091
									#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
2092
									#$conf["disallowAllSkipableVarIsEmpty"]="";
2093
									#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
2094
									#$conf["disallowAllSkipableVarIsEmptyArray"]="";
2095
									#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
2096
									#$conf["arrayCountEqualCheck"][]=array();
2097
									#參考資料來源:
2098
									#array_keys=>http://php.net/manual/en/function.array-keys.php
2099
									$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
2100
									unset($conf["variableCheck::checkArguments"]);
226 liveuser 2101
 
3 liveuser 2102
									#如果檢查參數失敗
2103
									if($checkArguments["status"]==="false"){
226 liveuser 2104
 
3 liveuser 2105
										#設置執行失敗
2106
										$result["status"]="false";
226 liveuser 2107
 
3 liveuser 2108
										#設置錯誤訊息
2109
										$result["error"]=$checkArguments;
226 liveuser 2110
 
3 liveuser 2111
										#印出error資訊
2112
										echo "error:".print_r($result,true);
226 liveuser 2113
 
3 liveuser 2114
										#回應錯誤訊息給用戶
2115
										fwrite($conn,json_encode($result).PHP_EOL);
226 liveuser 2116
 
3 liveuser 2117
										#結束回應
2118
										fwrite($conn,PHP_EOL.PHP_EOL);
226 liveuser 2119
 
3 liveuser 2120
										#關閉socket連線,將回應輸出.
2121
										fclose($conn);
226 liveuser 2122
 
3 liveuser 2123
										#回傳結果
2124
										return $result;
226 liveuser 2125
 
3 liveuser 2126
										}#if end
226 liveuser 2127
 
3 liveuser 2128
									#如果檢查參數不通過
2129
									if($checkArguments["passed"]==="false"){
226 liveuser 2130
 
3 liveuser 2131
										#設置執行失敗
2132
										$result["status"]="false";
226 liveuser 2133
 
3 liveuser 2134
										#設置錯誤訊息
2135
										$result["error"]=$checkArguments;
226 liveuser 2136
 
3 liveuser 2137
										#印出error資訊
2138
										echo "error:".print_r($result,true);
226 liveuser 2139
 
3 liveuser 2140
										#回應錯誤訊息給用戶
2141
										fwrite($conn,json_encode($result).PHP_EOL);
226 liveuser 2142
 
3 liveuser 2143
										#結束回應
2144
										fwrite($conn,PHP_EOL.PHP_EOL);
226 liveuser 2145
 
3 liveuser 2146
										#關閉socket連線,將回應輸出.
2147
										fclose($conn);
226 liveuser 2148
 
3 liveuser 2149
										#回傳結果
2150
										return $result;
226 liveuser 2151
 
3 liveuser 2152
										}#if end
226 liveuser 2153
 
3 liveuser 2154
									#如果回應不正常
2155
									if($call_user_func["status"]==="false"){
226 liveuser 2156
 
3 liveuser 2157
										#設置執行失敗
2158
										$result["status"]="false";
226 liveuser 2159
 
3 liveuser 2160
										#設置錯誤訊息
2161
										$result["error"][]=$call_user_func;
226 liveuser 2162
 
3 liveuser 2163
										#設置錯誤訊息
2164
										$result["error"][]=$conf;
226 liveuser 2165
 
3 liveuser 2166
										#印出error資訊
2167
										echo "error:".print_r($result,true);
226 liveuser 2168
 
3 liveuser 2169
										#回應錯誤訊息給用戶
2170
										fwrite($conn,json_encode($result).PHP_EOL);
226 liveuser 2171
 
3 liveuser 2172
										#結束回應
2173
										fwrite($conn,PHP_EOL.PHP_EOL);
226 liveuser 2174
 
3 liveuser 2175
										#關閉socket連線,將回應輸出.
2176
										fclose($conn);
226 liveuser 2177
 
3 liveuser 2178
										#回傳結果
2179
										return $result;
226 liveuser 2180
 
3 liveuser 2181
										}#if end
226 liveuser 2182
 
3 liveuser 2183
									#如果回應有 content 元素
2184
									if(isset($call_user_func["content"])){
226 liveuser 2185
 
3 liveuser 2186
										#用收到 content 元素內容來更新 $client[id]["request"]
2187
										$client[$request->id]["request"]=$call_user_func["content"];
226 liveuser 2188
 
3 liveuser 2189
										}#if end
226 liveuser 2190
 
3 liveuser 2191
									#如果回傳 cache 元素
2192
									if(isset($call_user_func["cache"])){
226 liveuser 2193
 
3 liveuser 2194
										#用收到 cache 元素內容來更新 server side 的 cache
2195
										$result["serverCache"]["clientSide"]["addOnProcessFunc"][$addOnProcessFunc]=&$call_user_func["cache"];
226 liveuser 2196
 
3 liveuser 2197
										}#if end
226 liveuser 2198
 
3 liveuser 2199
									#如果回應不要往後面執行
2200
									if($call_user_func["continue"]==="false"){
226 liveuser 2201
 
3 liveuser 2202
										#結束 foreach
2203
										break;
226 liveuser 2204
 
3 liveuser 2205
										}#if end
226 liveuser 2206
 
3 liveuser 2207
									}#foreach end
226 liveuser 2208
 
3 liveuser 2209
								}#if end
226 liveuser 2210
 
3 liveuser 2211
							#執行到這代表是要保持連線
226 liveuser 2212
 
3 liveuser 2213
							#設置執行正常
2214
							$res["status"]="true";
226 liveuser 2215
 
3 liveuser 2216
							#印出回的訊息
2217
							echo "response:".PHP_EOL.print_r($res,true);
226 liveuser 2218
 
3 liveuser 2219
							#debug
2220
							#var_dump(__LINE__,$conn,get_resource_type($conn));
226 liveuser 2221
 
3 liveuser 2222
							#如果資源形態沒有異常(用戶關閉連線後,會有resource type變成"Unknown"的結果)
2223
							if(get_resource_type($conn)!=="Unknown"){
226 liveuser 2224
 
3 liveuser 2225
								#函式說明:
2226
								#用fwrite寫入到resource,可以指定每個寫入字串的結尾方式.
2227
								#回傳結果:
2228
								#$result["status"],執行是否正常,"true"為正常,"false"為不正常.
2229
								#$result["error"],錯誤訊息陣列.
2230
								#$result["function"],當前執行的函數名稱.
2231
								#$result["argu"],使用的參數.
2232
								#$result["is_writable"],參數stream是否為可寫入的resource,"true"代表可寫入;"false"代表不可寫入.
2233
								#必填參數:
2234
								#$conf["stream"],resource,寫入的目標.
2235
								$conf["sock::fwrite"]["stream"]=&$conn;
2236
								#$conf["data"],字串陣列,要寫入的內容,每個元素代表每段要寫入的內容.
2237
								$conf["sock::fwrite"]["data"]=array(json_encode($res).PHP_EOL);
2238
								#可省略參數:
2239
								#$conf["msgEndType"],字串,訊息結尾要怎麼寫,預設為"none",代表不做處理;"EOL"代表結尾要用PHP_EOL;"HTTP/1.1"代表"data"參數最多為2個,分別為header跟body,結尾方式為兩個PHP_EOL.
2240
								$conf["sock::fwrite"]["msgEndType"]="HTTP/1.1";
2241
								#$conf["autoClose"],字串,寫入結束後是否要關閉resource,預設為"false"代表保持開着;反之為"true".
2242
								$conf["sock::fwrite"]["autoClose"]="true";
2243
								#參考資料:
2244
								#https://stackoverflow.com/questions/5294305/how-to-check-if-a-php-stream-resource-is-readable-or-writable#5294425
2245
								#https://www.php.net/manual/en/function.stream-get-meta-data.php
2246
								#https://www.php.net/manual/en/function.is-writable.php
2247
								#https://www.php.net/manual/en/function.socket-get-status.php
2248
								#備註:
2249
								#無.
2250
								$fwrite=sock::fwrite($conf["sock::fwrite"]);
2251
								unset($conf["sock::fwrite"]);
226 liveuser 2252
 
3 liveuser 2253
								#如果執行失敗
2254
								if($fwrite["status"]==="false"){
226 liveuser 2255
 
3 liveuser 2256
									#如果有 is_writable
2257
									if(isset($fwrite["is_writable"])){
226 liveuser 2258
 
3 liveuser 2259
										#如果有 is_writable
2260
										if($fwrite["is_writable"]==="true"){
226 liveuser 2261
 
3 liveuser 2262
											#設置執行失敗
2263
											$result["status"]="false";
226 liveuser 2264
 
3 liveuser 2265
											#設置錯誤訊息
2266
											$result["error"]=$fwrite;
226 liveuser 2267
 
3 liveuser 2268
											#印出error資訊
2269
											echo "error:".print_r($result,true);
226 liveuser 2270
 
3 liveuser 2271
											#結束並回傳1給shell
2272
											exit(1);
226 liveuser 2273
 
3 liveuser 2274
											}#if end
226 liveuser 2275
 
3 liveuser 2276
										}#if end
226 liveuser 2277
 
3 liveuser 2278
									#反之
2279
									else{
226 liveuser 2280
 
3 liveuser 2281
										#設置執行失敗
2282
										$result["status"]="false";
226 liveuser 2283
 
3 liveuser 2284
										#設置錯誤訊息
2285
										$result["error"]=$fwrite;
226 liveuser 2286
 
3 liveuser 2287
										#印出error資訊
2288
										echo "error:".print_r($result,true);
226 liveuser 2289
 
3 liveuser 2290
										#結束並回傳1給shell
2291
										exit(1);
226 liveuser 2292
 
3 liveuser 2293
										}#else end
226 liveuser 2294
 
3 liveuser 2295
									}#if end
226 liveuser 2296
 
3 liveuser 2297
								}#if end
226 liveuser 2298
 
3 liveuser 2299
							#更新使用者id與最後一次來request的時間
183 liveuser 2300
							$client[$request->id]["last_req_time"]=$last_req_time;
226 liveuser 2301
 
3 liveuser 2302
							#印出用戶端列表
183 liveuser 2303
							echo "clients:".print_r($client,true);
226 liveuser 2304
 
3 liveuser 2305
							#等待下個要求
2306
							continue;
226 liveuser 2307
 
3 liveuser 2308
							}#if end
226 liveuser 2309
 
3 liveuser 2310
						#socket 陣列不是我們建立的 socket
2311
						else{
226 liveuser 2312
 
3 liveuser 2313
							#顯示 socket 的狀態
2314
							var_dump("socket_get_status".socket_get_status($conn));
226 liveuser 2315
 
3 liveuser 2316
							}#else end
226 liveuser 2317
 
3 liveuser 2318
						}#foreach end
226 liveuser 2319
 
3 liveuser 2320
					}#else end
226 liveuser 2321
 
3 liveuser 2322
				}#while end
226 liveuser 2323
 
3 liveuser 2324
			#關閉 unix domain socket
2325
			fclose($socket);
226 liveuser 2326
 
3 liveuser 2327
			}#else end
226 liveuser 2328
 
3 liveuser 2329
		}#function unixDomainSockServer end
226 liveuser 2330
 
3 liveuser 2331
	/*
2332
	#函式說明:
306 liveuser 2333
	#建立 unix domain socket server, 僅提供具備檔案存取權限的用戶使用,預設提供可以下達任何指令的功能.
2334
	#回傳結果:
2335
	#$result["status"],"true"代表執行正常;"false"代表執行不正常.
2336
	#$result["error"],錯誤訊息陣列.
2337
	#$result["function"],當前執行的函式名稱.
2338
	#$result["serverCache"],函式結束前,儲存在serverCache的內容,若有元素"exit"存在則代表是正常結束.
2339
	#$result["serverCache"]["serverSide"],server side 的 cache.
2340
	#$result["serverCache"]["serverSide"]["procs"], server side 的 procs cache,儲存執行的子程序資訊.
2341
	#$result["serverCache"]["clientSide"],client site 的 cache.
2342
	#必填參數:
2343
	#$conf["sockAddr"],字串,socket檔案要放在哪邊,名稱為何.
2344
	$conf["sockAddr"]="";
2345
	#conf["listenInfo"],字串,要listen的 ip跟port,預設為"127.0.0.1:54321".
2346
	$conf["listenInfo"]="127.0.0.1:54321":
2347
	#可省略參數:
2348
	#$conf["changeOwner"],字串,要將socket檔案的擁有着權限進行修改."user.group"代表擁有者帳號為user,群組為group.
2349
	#$conf["changeOwner"]="";
2350
	#$conf["changePermission"],字串,要將socket檔案的權限設為多少.ex: "0666"(所有帳戶都有存取的權限) 或 "0660"(僅有擁有者與群組帳戶有存取的權限) 或 "0600"(只有擁有者有權限執行).
2351
	#$conf["changePermission"]="";
2352
	#$conf["sessionTimeout"],字串,當連線結束後於下一次連線間隔多久就算session timeout,server端會將記錄移除,client端需要重新拿取id,預設為300秒.
2353
	#$conf["sessionTimeout"]="300";
2354
	#$conf["addOnProcessFunc"],字串陣列,增加用於處理 json request 的函式名稱,給予的參數為array("request"=>收到的json訊息,"sock"=>用戶的socket,"clientCache"=>給予所有用戶的cache),若收到的不是json而是"quit"則代表用戶要結束連線;若收到的是$shutdownStr則代表要結束本函式.回傳的內容必須為陣列,例如 $res["continue"]="true"代表要繼續執行下一個addOnProcessFunc;"false"代表代表到此為止. $res["content"]="replaced content";代表要將收到的訊息取代成"replaced content". 最少要有回傳 $res["status"]數值"true"代表執行正常;"false"代表執行不正常.
2355
	#$conf["addOnProcessFunc"]=array();
2356
	#$conf["funcToRunWhenIdle"],字串陣列,當沒有事件產生時,要執行的函式名稱,給予參數為array("client"=>所有用戶,"clientCache"=>$result["serverCache"]["clientSide"]["addOnProcessFunc"][$funcToRunWhenIdle],"serverCache"=>$result["serverCache"]["serverSide"]["funcToRunWhenIdle"][$funcToRunWhenIdle]).
2357
	#$conf["funcToRunWhenIdle"]=array();
2358
	#$conf["paramsForFuncToRunWhenIdle"],2維陣列,每個元素代表指定給予funcToRunWhenIdle參數中的指定元素的參數.
2359
	#$conf["paramsForFuncToRunWhenIdle"]=array();
2360
	#$conf["infoToFunction"],陣列,需要增加給addOnProcessFunc跟funcToRunWhenIdle函式的資訊,在函式中其參數的info鍵值.
2361
	#$conf["infoToFunction"]=array("debug"=>$debug);
2362
	#$conf["shutdownStrAddr"],字串,儲存收到用戶傳什麼樣的字串會結束本函式的檔案位置與名稱,預設為 $conf["sock"].".shutdown".
2363
	#$conf["shutdownStrAddr"]="";
2364
	#參考資料:
2365
	#http://php.net/manual/en/function.stream-socket-server.php
2366
	#備註:
2367
	#建構中...
2368
	*/
2369
	public static function internetAndIntranetServer(&$conf){
2370
 
2371
		#初始化要回傳的結果
2372
		$result=array();
2373
 
2374
		#取得當前執行的函數名稱
2375
		$result["function"]=__FUNCTION__;
2376
 
2377
		#如果沒有參數
2378
		if(func_num_args()==0){
2379
 
2380
			#設置執行失敗
2381
			$result["status"]="false";
2382
 
2383
			#設置執行錯誤訊息
2384
			$result["error"]="函數".$result["function"]."需要參數";
2385
 
2386
			#回傳結果
2387
			return $result;
2388
 
2389
			}#if end
2390
 
2391
		#/* 請依據實際狀況使用
2392
		#涵式說明:
2393
		#判斷當前環境為web還是cmd
2394
		#回傳結果:
2395
		#$result,"web"或"cmd"
2396
		if(csInformation::getEnv()==="web"){
2397
 
2398
			#設置執行失敗
2399
			$result["status"]="false";
2400
 
2401
			#設置執行錯誤訊息
2402
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
2403
 
2404
			#回傳結果
2405
			return $result;
2406
 
2407
			}#if end
2408
		#*/
2409
 
2410
		#取得參數
2411
		$result["argu"]=$conf;
2412
 
2413
		#如果 $conf 不為陣列
2414
		if(gettype($conf)!=="array"){
2415
 
2416
			#設置執行失敗
2417
			$result["status"]="false";
2418
 
2419
			#設置執行錯誤訊息
2420
			$result["error"][]="\$conf變數須為陣列形態";
2421
 
2422
			#如果傳入的參數為 null
2423
			if(is_null($conf)){
2424
 
2425
				#設置執行錯誤訊息
2426
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
2427
 
2428
				}#if end
2429
 
2430
			#回傳結果
2431
			return $result;
2432
 
2433
			}#if end
2434
 
2435
		#預設的 unix domain socket addr
2436
		$defaultSockAddr=pathinfo(__FILE__)["dirname"]."/../../../var/qbpwcf/internetAndIntranetServer.sock";
2437
 
2438
		#檢查參數
2439
		#檢查參數
2440
		#函式說明:
2441
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
2442
		#回傳結果:
2443
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
2444
		#$result["error"],執行不正常結束的錯訊息陣列.
2445
		#$result["simpleError"],簡單表示的錯誤訊息.
2446
		#$result["function"],當前執行的函式名稱.
2447
		#$result["argu"],設置給予的參數.
2448
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
2449
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
2450
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
2451
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
2452
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
2453
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
2454
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
2455
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
2456
		#必填參數:
2457
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
2458
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
2459
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
2460
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
2461
		#可省略參數:
2462
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
2463
		#$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array();
2464
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null、any代表不指定變數形態.其中 resource也包含"resource (closed)".
2465
		#$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array();
2466
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
2467
		#$conf["canBeEmptyString"]="false";
2468
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
2469
		#$conf["canNotBeEmpty"]=array();
2470
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
2471
		#$conf["canBeEmpty"]=array();
2472
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
2473
		$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("listenInfo","sockAddr");
2474
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
2475
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("string","string");
2476
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
2477
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("127.0.0.1:54321",$defaultSockAddr);
2478
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
2479
		#$conf["skipableVarDefaultValue"]=array("");
2480
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
2481
		#$conf["disallowAllSkipableVarIsEmpty"]="";
2482
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
2483
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
2484
		#$conf["disallowAllSkipableVarNotExist"],字串,是否不允許每個可省略參數都不存在,預設為"false"代表允許,反之為"true".
2485
		$conf["variableCheck::checkArguments"]["disallowAllSkipableVarNotExist"]="true";
2486
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
2487
		#$conf["arrayCountEqualCheck"][]=array();
2488
		#參考資料:
2489
		#array_keys=>http://php.net/manual/en/function.array-keys.php
2490
		#備註:
2491
		#無.
2492
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
2493
		unset($conf["variableCheck::checkArguments"]);
2494
 
2495
		#如果執行異常
2496
		if($checkArguments["status"]==="false"){
2497
 
2498
			#設置執行失敗
2499
			$result["status"]="false";
2500
 
2501
			#設置執行錯誤訊息
2502
			$result["error"]=$checkArguments;
2503
 
2504
			#回傳結果
2505
			return $result;
2506
 
2507
			}#if end
2508
 
2509
		#如果檢查參數不通過
2510
		if($checkArguments["passed"]==="false"){
2511
 
2512
			#設置執行失敗
2513
			$result["status"]="false";
2514
 
2515
			#設置執行錯誤訊息
2516
			$result["error"]=$checkArguments;
2517
 
2518
			#回傳結果
2519
			return $result;
2520
 
2521
			}#if end
2522
 
2523
		#縮減 socket 的路徑,避免出錯.
2524
		#函式說明:
2525
		#將檔案目錄的絕對位置中的 "../" 剔除變成直觀的路徑.
2526
		#回傳結果:
2527
		#$result["status"],執行是否成功,"true"代表執行成功,"false"代表執行失敗.
2528
		#$result["function"],當前執行的函數.
2529
		#$result["error"],錯誤訊息陣列.
2530
		#$result["argu"],使用者參數.
2531
		#$result["changedPath"],處理完後回傳的目錄字串.
2532
		#$result["oriPath"],原始的路徑字串
2533
		#必填參數:
2534
		#$conf["dirStr"],字串,要處理的檔案目錄字串.
2535
		$conf["stringProcess::changeDirByDotDotSolidus"]["dirStr"]=$conf["sockAddr"];
2536
		#可省略參數:
2537
		#無.
2538
		#參考資料:
2539
		#無.
2540
		#備註:
2541
		#考慮用realpath取代
2542
		$changeDirByDotDotSolidus=stringProcess::changeDirByDotDotSolidus($conf["stringProcess::changeDirByDotDotSolidus"]);
2543
		unset($conf["stringProcess::changeDirByDotDotSolidus"]);
2544
 
2545
		#如果執行異常
2546
		if($changeDirByDotDotSolidus["status"]==="false"){
2547
 
2548
			#設置執行失敗
2549
			$result["status"]="false";
2550
 
2551
			#設置錯誤訊息
2552
			$result["error"]=$changeDirByDotDotSolidus;
2553
 
2554
			#回傳結果
2555
			return $result;
2556
 
2557
			}#if end
2558
 
2559
		#取得整理過後的直觀路徑
2560
		$conf["sockAddr"]=$changeDirByDotDotSolidus["changedPath"];
2561
 
2562
		#取得 socket 的路徑
2563
		#函式說明:
2564
		#將字串特定關鍵字與其後面的內容剔除
2565
		#回傳結果:
2566
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
2567
		#$result["error"],錯誤訊息陣列.
2568
		#$result["warning"],警告訊息鎮列.
2569
		#$result["founded"],有無找到定字串"true"代表有,"false"代表沒有.
2570
		#$result["function"],當前執行的函數名稱.
2571
		#$result["oriStr"],要處理的原始字串內容.
2572
		#$result["content"],處理好的的字串內容.
2573
		#$result["argu"],使用的參數.
2574
		#必填參數:
2575
		#$conf["stringIn"],字串,要處理的字串.
2576
		$conf["stringProcess::delStrAfterKeyWord"]["stringIn"]=$conf["sockAddr"];
2577
		#$conf["keyWord"],字串,特定字串.
2578
		$conf["stringProcess::delStrAfterKeyWord"]["keyWord"]="/";
2579
		#可省略參數:
2580
		#$conf["deleteLastRepeatedOne"],字串,預設為"false";若為"true"則代表連續遇到同 $conf["keyWord"] 的內容,要將移除內容的起點往後移動到為後一個 $conf["keyWord"].
2581
		$conf["stringProcess::delStrAfterKeyWord"]["deleteLastRepeatedOne"]="true";
2582
		#參考資料:
2583
		#無.
2584
		#備註:
2585
		#無.
2586
		$delStrAfterKeyWord=stringProcess::delStrAfterKeyWord($conf["stringProcess::delStrAfterKeyWord"]);
2587
		unset($conf["stringProcess::delStrAfterKeyWord"]);
2588
 
2589
		#如果執行失敗
2590
		if($delStrAfterKeyWord["status"]==="false"){
2591
 
2592
			#設置執行失敗
2593
			$result["status"]="false";
2594
 
2595
			#設置錯誤訊息
2596
			$result["error"][]=$delStrAfterKeyWord;
2597
 
2598
			#設置錯誤訊息
2599
			$result["error"][]="建立 unix domain socket(".$conf["sock"].") 失敗";
2600
 
2601
			#印出錯誤訊息
2602
			echo print_r($result,true);
2603
 
2604
			#回傳結果
2605
			return $result;
2606
 
2607
			}#if end
2608
 
2609
		#如果有 "/"
2610
		if($delStrAfterKeyWord["founded"]==="true"){
2611
 
2612
			#確保建立socket的路徑有存在
2613
			#函式說明:
2614
			#確保路徑存在.
2615
			#回傳結果:
2616
			#$result["status"],執行正常與否,"true"代表正常,"false"代表不正常.
2617
			#$result["error"],錯誤訊息陣列.
2618
			#$resutl["function"],當前執行的涵式名稱.
2619
			#$result["path"],建立好的路徑字串.
2620
			#$result["fileName"],檔案名稱,若 $conf["haveFileName"] 為 "true" 則會回傳.
2621
			#$result["argu"],使用的參數.
2622
			#必填參數:
2623
			#$conf["path"],要檢查的路徑
2624
			$conf["fileAccess::validatePath"]["path"]=$delStrAfterKeyWord["content"];
2625
			#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
2626
			$conf["fileAccess::validatePath"]["fileArgu"]=__FILE__;
2627
			#可省略參數:
2628
			#$conf["haveFileName"],字串,"true"代表有$conf["path"]檔案名稱,"false"代表$conf["path"]為純路徑,預設為"false".
2629
			$conf["fileAccess::validatePath"]["haveFileName"]="false";
2630
			#$conf["dirPermission"],字串,新建資料夾的權限設定,預設爲0770,亦即擁有者,同群組者可以讀,寫,存取,其他人僅能存取.
2631
			#$conf["dirPermission"]="";
2632
			#$conf["web"],是否為檔案系統,"true"為網頁路徑,"false"為網頁系統,預設為"false".
2633
			$conf["fileAccess::validatePath"]["web"]="false";
2634
			#參考資料:
2635
			#無.
2636
			#備註:
2637
			#無.
2638
			$validatePath=fileAccess::validatePath($conf["fileAccess::validatePath"]);
2639
			unset($conf["fileAccess::validatePath"]);
2640
 
2641
			#如果執行失敗
2642
			if($validatePath["status"]==="false"){
2643
 
2644
				#設置執行失敗
2645
				$result["status"]="false";
2646
 
2647
				#設置錯誤訊息
2648
				$result["error"][]=$validatePath;
2649
 
2650
				#設置錯誤訊息
2651
				$result["error"][]="建立 unix domain socket(".$conf["sock"].") 失敗";
2652
 
2653
				#印出錯誤訊息
2654
				echo print_r($result,true);
2655
 
2656
				#回傳結果
2657
				return $result;
2658
 
2659
				}#if end
2660
 
2661
			}#if end
2662
 
2663
		#如果同名 unix domain socket file 存在
2664
		if(file_exists($conf["sock"])){
2665
 
2666
			#則移除之.
2667
			unlink($conf["sock"]);
2668
 
2669
			}#if end
2670
 
2671
		#預設的 unix socket addr
2672
		$unixAddr="unix://".$conf["sockAddr"];
2673
 
2674
		#建立 unix domain socket
2675
		$socket=stream_socket_server($unixAddr, $errno, $errstr);
2676
 
2677
		#如果建立 unix domain socket 失敗
2678
		if(!$socket){
2679
 
2680
			#設置執行失敗
2681
			$result["status"]="false";
2682
 
2683
			#設置錯誤訊息
2684
			$result["error"][]=$errstr." (".$errno.")";
2685
 
2686
			#設置錯誤訊息
2687
			$result["error"][]="建立 unix domain socket(".$conf["sock"].") 失敗";
2688
 
2689
			#印出錯誤訊息
2690
			echo print_r($result,true);
2691
 
2692
			#回傳結果
2693
			return $result;
2694
 
2695
			}#if end
2696
 
2697
		#檢查參數
2698
		#檢查參數
2699
		#函式說明:
2700
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
2701
		#回傳結果:
2702
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
2703
		#$result["error"],執行不正常結束的錯訊息陣列.
2704
		#$result["simpleError"],簡單表示的錯誤訊息.
2705
		#$result["function"],當前執行的函式名稱.
2706
		#$result["argu"],設置給予的參數.
2707
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
2708
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
2709
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
2710
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
2711
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
2712
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
2713
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
2714
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
2715
		#必填參數:
2716
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
2717
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
2718
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
2719
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
2720
		#可省略參數:
2721
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
2722
		#$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array();
2723
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null、any代表不指定變數形態.其中 resource也包含"resource (closed)".
2724
		#$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array();
2725
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
2726
		#$conf["canBeEmptyString"]="false";
2727
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
2728
		#$conf["canNotBeEmpty"]=array();
2729
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
2730
		#$conf["canBeEmpty"]=array();
2731
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
2732
		$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("listenInfo","sockAddr");
2733
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
2734
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("string","string");
2735
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
2736
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("127.0.0.1:54321",$defaultSockAddr);
2737
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
2738
		#$conf["skipableVarDefaultValue"]=array("");
2739
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
2740
		#$conf["disallowAllSkipableVarIsEmpty"]="";
2741
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
2742
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
2743
		#$conf["disallowAllSkipableVarNotExist"],字串,是否不允許每個可省略參數都不存在,預設為"false"代表允許,反之為"true".
2744
		$conf["variableCheck::checkArguments"]["disallowAllSkipableVarNotExist"]="true";
2745
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
2746
		#$conf["arrayCountEqualCheck"][]=array();
2747
		#參考資料:
2748
		#array_keys=>http://php.net/manual/en/function.array-keys.php
2749
		#備註:
2750
		#無.
2751
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
2752
		unset($conf["variableCheck::checkArguments"]);
2753
 
2754
		#如果執行異常
2755
		if($checkArguments["status"]==="false"){
2756
 
2757
			#設置執行失敗
2758
			$result["status"]="false";
2759
 
2760
			#設置執行錯誤訊息
2761
			$result["error"]=$checkArguments;
2762
 
2763
			#回傳結果
2764
			return $result;
2765
 
2766
			}#if end
2767
 
2768
		#如果檢查參數不通過
2769
		if($checkArguments["passed"]==="false"){
2770
 
2771
			#設置執行失敗
2772
			$result["status"]="false";
2773
 
2774
			#設置執行錯誤訊息
2775
			$result["error"]=$checkArguments;
2776
 
2777
			#回傳結果
2778
			return $result;
2779
 
2780
			}#if end
2781
 
2782
		#檢查 unix socket 檔案 $conf["sockAddr"] 是否存在.
2783
		#函式說明:
2784
		#檢查多個檔案與資料夾是否存在.
2785
		#回傳的結果:
2786
		#$result["status"],執行正常與否,"true"代表正常,"false"代表不正常.
2787
		#$result["error"],錯誤訊息陣列.
2788
		#$resutl["function"],當前執行的涵式名稱.
2789
		#$result["argu"],使用的參數.
2790
		#$result["allExist"],所有檔案皆存在的識別,"true"代表皆存在,"false"代表沒有全部都存在.
2791
		#$result["varName"][$i],爲第$i個資料夾或檔案的路徑與名稱。
2792
		#$result["varNameFullPath"][$i],爲第$i個資料夾或檔案的完整檔案系統路徑與名稱,如果不存在則代表路徑是網址.
2793
		#$result["varNameWebPath"][$i],為第$i個資料夾或檔案的網址
2794
		#$result["varExist"][$i],爲第$i個資料夾或檔案是否存在,"true"代表存在,"false"代表不存在。
2795
		#必填參數:
2796
		#$conf["fileArray"],陣列字串,要檢查是否存在的檔案有哪些,須爲一維陣列數值。
2797
		$conf["variableCheck::checkArguments"]["fileArray"]=array($conf["sockAddr"]);
2798
		#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
2799
		$conf["variableCheck::checkArguments"]["fileArgu"]=__FILE__;
2800
		#可省略參數:
2801
		#$conf["disableWebSearch"],"字串",是否取消「當檔案找不到時,改用catchWebContent類別的wget函數來檢查檔案是否存在於網路上」的功能,"false"不取消,若要取消該功能請設為"true",若抓到的內容為空字串則會視為檔案不存在,預設為"true".
2802
		#$conf["disableWebSearch"]="false";
2803
		#$conf["userDir"],字串,網頁是否置放於家目錄底下,"true"為是,"false"為不是,預設為"true".
2804
		$conf["variableCheck::checkArguments"]["userDir"]="false";
2805
		#$conf["web"],字串,檔案是放在web就是"true",反之為檔案系統"false",預設為"true".
2806
		$conf["variableCheck::checkArguments"]["web"]="false";
2807
		#參考資料:
2808
		#http://php.net/manual/en/function.file-exists.php
2809
		#http://php.net/manual/en/control-structures.foreach.php
2810
		#備註:
2811
		#函數file_exists檢查的路徑為檔案系統的路徑
2812
		#$result["varName"][$i]結果未實作
2813
		$checkMultiFileExist=fileAccess::checkMultiFileExist($conf["variableCheck::checkArguments"]);
2814
		unset($conf["variableCheck::checkArguments"]);
2815
 
2816
		#如果檢查失敗
2817
		if($checkMultiFileExist["status"]==="false"){
2818
 
2819
			#設置執行失敗
2820
			$result["status"]="false";
2821
 
2822
			#設置錯誤訊息
2823
			$result["error"]=$checkMultiFileExist;
2824
 
2825
			#回傳結果
2826
			return $result;
2827
 
2828
			}#if end
2829
 
2830
		#如果socket檔案已經存在
2831
		if($checkMultiFileExist["allExist"]==="true"){
2832
 
2833
			#移除之
2834
			unlink($conf["sockAddr"]);
2835
 
2836
			}#if end
2837
 
2838
		#建立 unix domain socket
2839
		$socket=stream_socket_server($unixAddr, $errno, $errstr);
2840
 
2841
		#如果建立 unix domain socket 失敗
2842
		if(!$socket){
2843
 
2844
			#設置執行失敗
2845
			$result["status"]="false";
2846
 
2847
			#設置錯誤訊息
2848
			$result["error"][]=$errstr." (".$errno.")";
2849
 
2850
			#設置錯誤訊息
2851
			$result["error"][]="建立 unix domain socket(".$conf["sock"].") 失敗";
2852
 
2853
			#印出錯誤訊息
2854
			echo print_r($result,true);
2855
 
2856
			#回傳結果
2857
			return $result;
2858
 
2859
			}#if end
2860
 
2861
		#如果建立 unix domain socket 成功
2862
		else{
2863
 
2864
			#產生用於代表結束本函式的字串
2865
			#函式說明:
2866
			#使用 linux 的 uuid 指令來產生 uuid 字串
2867
			#回傳結果:
2868
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
2869
			#$result["error"],錯誤訊息.
2870
			#$result["function"],當前執行的函式名稱.
2871
			#$result["content"],uuid.
2872
			#必填參數:
2873
			#無.
2874
			#可省略參數:
2875
			#無.
2876
			#參考資料:
2877
			#無.
2878
			#備註:
2879
			#無.
2880
			$uuid=cmd::uuid();
2881
 
2882
			#如果執行失敗
2883
			if($uuid["status"]==="false"){
2884
 
2885
				#設置執行失敗
2886
				$result["status"]="false";
2887
 
2888
				#設置錯誤訊息
2889
				$result["error"]=$uuid;
2890
 
2891
				#回傳結果
2892
				return $result;
2893
 
2894
				}#if end
2895
 
2896
			#設置代表結束本服務的uuid字串
2897
			$shutdownStr=$uuid["content"];
2898
 
2899
			#如果沒有設置
2900
			if(!isset($conf["shutdownStrAddr"])){
2901
 
2902
				#初始化存放 shutdownStr 的檔案
2903
				$conf["shutdownStrAddr"]=$conf["sock"].".shutdownStr";
2904
 
2905
				}#if end
2906
 
2907
			#如果檔案位置名稱跟socket一樣
2908
			else if($conf["sock"]===$conf["shutdownStrAddr"]){
2909
 
2910
				#設置執行失敗
2911
				$result["status"]="false";
2912
 
2913
				#設置錯誤訊息
2914
				$result["error"][]="參數shutdownStrAddr跟sock不可一樣";
2915
 
2916
				#設置錯誤訊息
2917
				$result["error"][]=$checkArguments;
2918
 
2919
				#回傳結果
2920
				return $result;
2921
 
2922
				}#else end
2923
 
2924
			#建立裡面儲存若要結束本程式需要接收到什麼樣的字串
2925
			#函式說明:
2926
			#將字串寫入到檔案
2927
			#回傳結果:
2928
			#$result["status"],"true"表示檔案寫入成功,"false"表示檔案寫入失敗.
2929
			#$result["error"],錯誤訊息陣列.
2930
			#$result["function"],當前執行的函數名稱.
2931
			#$result["fileInfo"],實際上寫入的檔案資訊陣列.
2932
			#$result["fileInfo"]["createdFileName"],建立好的檔案名稱.
2933
			#$result["fileInfo"]["createdFilePath"],檔案建立的路徑.
2934
			#$result["fileInfo"]["createdFilePathAndName"].建立好的檔案名稱與路徑.
2935
			#$result["argu"],使用的參數.
2936
			#必填參數:
2937
			#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
2938
			$conf["fileAccess::writeTextIntoFile"]["fileArgu"]=$conf["fileArgu"];
2939
			#可省略參數:
2940
			#$conf["fileName"],字串,爲要編輯的檔案名稱,預設為隨機產生的檔案名稱.
2941
			$conf["fileAccess::writeTextIntoFile"]["fileName"]=$conf["shutdownStrAddr"];
2942
			#$conf["inputString"],字串,爲要寫入到裏面的內容,若要每筆資料寫入後換行,則可以在字串內容後面加上 \r\n 即可,預設為"".
2943
			$conf["fileAccess::writeTextIntoFile"]["inputString"]=$shutdownStr;
2944
			#$conf["writeMethod"],字串,爲檔案撰寫的方式,可省略,是複寫'a'還是,重新寫入'w',預設爲'w',重新寫入.
2945
			#$conf["writeMethod"]="a";
2946
			#$conf["checkRepeat"],字串,"true"代表建立檔案之前要先檢查檔案是否存在,若存在則在原名稱後面加上從(1)開始的編號.
2947
			#$conf["checkRepeat"]="";
2948
			#$conf["filenameExtensionStartPoint"],字串,檔案的副檔名是從倒數第幾個小數點(dot)開始,預設為"1",最後一個小數點,必須與$conf["checkRepeat"]搭配才會生效.
2949
			#$conf["filenameExtensionStartPoint"]="";
2950
			#$conf["repeatNameRule"],字串,遇到相同名稱的檔案要如何加上識別的編號,編號用「\$i」表示,預設為"(\$i)",必須與$conf["checkRepeat"]搭配才會生效.
2951
			#$conf["repeatNameRule"]="";
2952
			#$conf["web"],檔案是否位於網站上"true",若是在檔案系統則為"false",預設為"true".
2953
			$conf["fileAccess::writeTextIntoFile"]["web"]="false";
2954
			#參考資料:
2955
			#無.
2956
			#備註:
2957
			#無.
2958
			$writeTextIntoFile=fileAccess::writeTextIntoFile($conf["fileAccess::writeTextIntoFile"]);
2959
			unset($conf["fileAccess::writeTextIntoFile"]);
2960
 
2961
			#如果執行失敗
2962
			if($writeTextIntoFile["status"]==="false"){
2963
 
2964
				#設置執行失敗
2965
				$result["status"]="false";
2966
 
2967
				#設置錯誤訊息
2968
				$result["error"]=$writeTextIntoFile;
2969
 
2970
				#回傳結果
2971
				return $result;
2972
 
2973
				}#if end
2974
 
2975
			#如果有設置$conf["changeOwner"]
2976
			if(isset($conf["changeOwner"])){
2977
 
2978
				#如果裡面的"."不是一個
2979
				if(substr_count($conf["changeOwner"],".")!==1){
2980
 
2981
					#設置執行失敗
2982
					$result["status"]="false";
2983
 
2984
					#設置錯誤訊息
2985
					$result["error"][]="參數 changeOwner 應為 username.groupname";
2986
 
2987
					#印出錯誤訊息
2988
					echo print_r($result,true);
2989
 
2990
					#回傳結果
2991
					return $result;
2992
 
2993
					}#if end
2994
 
2995
				#改變檔案的擁有者
2996
				#函式說明:
2997
				#使用 linux 的 chown 指令來修改目標檔案或目錄的擁有者跟群組擁有者資訊.
2998
				#回傳結果:
2999
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
3000
				#$result["error"],錯誤訊息.
3001
				#$result["function"],當前執行的函式名稱.
3002
				#$result["cmd"],執行的指令.
3003
				#$result["content"],執行的結果陣列,如果參數 "recursive" 跟 "excludeSelf" 都有設定的話,就會回傳該結果.
3004
				#必填參數:
3005
				#$conf["owner"],字串,要變哪個使用者擁有.
3006
				$conf["cmd::chown"]["owner"]=explode(".",$conf["changeOwner"])[0];
3007
				#$conf["target"],字串,需要變更擁有者、 群組的目標.
3008
				$conf["cmd::chown"]["target"]=$conf["sock"];
3009
				#可省略參數:
3010
				#$conf["group"],字串,要變成什麼群組擁有,預設跟"owner"一樣.
3011
				$conf["cmd::chown"]["group"]=explode(".",$conf["changeOwner"])[1];
3012
				#$conf["recursive"],字串,"true"代表目標目錄底下的內容都要套用,預設為"false".
3013
				#$conf["recursive"]="true";
3014
				#$conf["excludeSelf"],字串,預設為"false"代表不處理;若為"true"則會排除目標自己(資料夾).
3015
				#$conf["excludeSelf"]="true";
3016
				#參考資料:
3017
				#無.
3018
				#備註:
3019
				#無.
3020
				$chown=cmd::chown($conf["cmd::chown"]);
3021
				unset($conf["cmd::chown"]);
3022
 
3023
				#如果改變檔案擁有者限失敗
3024
				if($chown===false){
3025
 
3026
					#設置執行失敗
3027
					$result["status"]="false";
3028
 
3029
					#設置錯誤訊息
3030
					$result["error"][]="改變 ".$conf["sock"]." 檔案的擁有資訊為 ".explode(".",$conf["changeOwner"])[0]." 失敗,通常只有系統帳戶才能變更之.";
3031
 
3032
					#印出錯誤訊息
3033
					echo print_r($result,true);
3034
 
3035
					#回傳結果
3036
					return $result;
3037
 
3038
					}#if end
3039
 
3040
				}#if end
3041
 
3042
			#如果有設置 $conf["changePermission"]
3043
			if(isset($conf["changePermission"])){
3044
 
3045
				#函式說明:
3046
				#使用 linux 的 chmod 指令來修改目標檔案或目錄的權限.
3047
				#回傳結果:
3048
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
3049
				#$result["error"],錯誤訊息.
3050
				#$result["function"],當前執行的函式名稱.
3051
				#$result["cmd"],執行的指令.
3052
				#$result["content"],執行的結果陣列,如果參數 "recursive" 跟 "excludeSelf" 都有設定的話,就會回傳該結果.
3053
				#必填參數:
3054
				#$conf["mode"],字串,要變成什麼權限.
3055
				$conf["cmd::chmod"]["mode"]=$conf["changePermission"];
3056
				#$conf["target"],字串,需要變更權限的目標.
3057
				$conf["cmd::chmod"]["target"]=$conf["sock"];
3058
				#可省略參數:
3059
				#$conf["recursive"],字串,"true"代表目標目錄底下的內容都要套用,預設為"false".
3060
				#$conf["recursive"]="true";
3061
				#$conf["excludeSelf"],字串,預設為"false"代表不處理;若為"true"則會排除目標自己(資料夾).
3062
				#$conf["excludeSelf"]="true";
3063
				#參考資料:
3064
				#無.
3065
				#備註:
3066
				#無.
3067
				$chmod=cmd::chmod($conf["cmd::chmod"]);
3068
				unset($conf["cmd::chmod"]);
3069
 
3070
				#如果變更檔案權限失敗
3071
				if($chmod["status"]==="false"){
3072
 
3073
					#設置執行失敗
3074
					$result["status"]="false";
3075
 
3076
					#設置錯誤訊息
3077
					$result["error"]=$chmod;
3078
 
3079
					#印出錯誤訊息
3080
					echo print_r($result,true);
3081
 
3082
					#回傳結果
3083
					return $result;
3084
 
3085
					}#if end
3086
 
3087
				}#if end
3088
 
3089
			#初始化儲存 server 資訊的變數,預設數值為 $result 自己
3090
			$result["intranet"]["serverCache"]=&$result;
3091
 
3092
			#初始化方便識別 server 資訊的變數
3093
			$intranetServerCache=&$result["intranet"]["serverCache"];
3094
 
3095
			#初始化 server site cache
3096
			$intranetServerCache["serverSide"]=array();
3097
 
3098
			#初始化 儲存 透過 proc 執行的子程序
3099
			$intranetServerCache["serverSide"]["procs"]=array();
3100
 
3101
			#初始化 client site cache
3102
			$intranetServerCache["clientSide"]=array();
3103
 
3104
			#初始化方便識別 client site cache 資訊的變數
3105
			$intranetClientCache=&$intranetServerCache["clientSide"];
3106
 
3107
			#初始化儲存unix domain socket clients的陣列
3108
			$intranetClients=array();
3109
 
3110
			}#else end
3111
 
3112
		#程式不會 timeout
3113
		set_time_limit(0);
3114
 
3115
		#Turn on implicit output flushing so we see what we're getting as it comes in.
3116
		ob_implicit_flush();
3117
 
3118
		#解析 $ip 跟 $port
3119
		#函式說明:
3120
		#尋找字串中是否含有符合格式的內容,且回傳解析好的變數數值.
3121
		#回傳結果:
3122
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
3123
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
3124
		#$result["function"],當前執行的函式名稱.
3125
		#$result["argu"],所使用的參數.
3126
		#$result["found"],是否有找到符合格式的字串內容,"true"代表有找到,"false"代表沒有找到.
3127
		#$result["content"],陣列,若為n個${*},則當found為"true"時,就會回傳n個元素.
3128
		#$result["parsedVar"][varName],陣列,解析好的變數陣列,varName為${}中的內容,變數的key從0開始.
3129
		#必填參數:
3130
		#$conf["input"],字串,要檢查的字串.
3131
		$conf["search::findSpecifyStrFormat"]["input"]=$conf["listenInfo"];
3132
		#$conf["format"],格式字串,要尋找的格式字串.格式為固定的字串("fixedStr format")與變數("${keyWordVarName}")組成.
3133
		$conf["search::findSpecifyStrFormat"]["format"]="${ip}:${port}";
3134
		#可省略參數:
3135
		#$conf["varEqual"],陣列,變數對應的數值,null代表不指定,其他內容代表該變數解析出來必須要為該內容.
3136
		#$conf["varEqual"]=array(null,"found");
3137
		#$conf["varCon"],陣列,每個varEqual為null者,其是否有其他條件,預設為null代表無其他條件,條件的表示是用陣列的key與value來表達,例如:array("no_tail"=>" not"),就代表變數的結尾不能為" not",可以用的key有"head",代表開頭要有什麼;"no_head",代表不能為什麼開頭;"tail",代表要什麼結尾;"no_tail",代表不能什麼結尾.
3138
		#$conf["varCon"]=array("no_tail"=>" not");
3139
		#參考資料:
3140
		#無.
3141
		#備註:
3142
		#無.
3143
		$findSpecifyStrFormat=search::findSpecifyStrFormat($conf["search::findSpecifyStrFormat"]);
3144
		unset($conf["search::findSpecifyStrFormat"]);
3145
 
3146
		#如果執行異常
3147
		if($findSpecifyStrFormat["status"]==="false"){
3148
 
3149
			#設置執行異常識別
3150
			$result["status"]="false";
3151
 
3152
			#設置執行錯誤訊息
3153
			$result["error"]=$findSpecifyStrFormat;
3154
 
3155
			#回傳結果
3156
			return $return;
3157
 
3158
			}#if end
3159
 
3160
		#如果沒有符合格式的內容
3161
		if($findSpecifyStrFormat["found"]==="false"){
3162
 
3163
			#設置執行異常識別
3164
			$result["status"]="false";
3165
 
3166
			#設置執行錯誤訊息
3167
			$result["error"]=$findSpecifyStrFormat;
3168
 
3169
			#回傳結果
3170
			return $return;
3171
 
3172
			}#if end
3173
 
3174
		#set listen ip and port
3175
		$address = $findSpecifyStrFormat["parsedVar"]["ip"][0];
3176
 
3177
		#set listen port
3178
		$port = $findSpecifyStrFormat["parsedVar"]["port"][0];
3179
 
3180
		#如果建立 socket 失敗
3181
		if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
3182
 
3183
			#設置錯誤訊息
3184
			$errorMsg="socket_create() failed: reason: " . socket_strerror(socket_last_error()) . PHP_EOL;
3185
 
3186
			#設置執行異常識別
3187
			$result["status"]="false";
3188
 
3189
			#設置執行錯誤訊息
3190
			$result["error"]=$errorMsg;
3191
 
3192
			#回傳結果
3193
			return $return;
3194
 
3195
			}#if end
3196
 
3197
		#如果 bind socket 失敗
3198
		if (socket_bind($sock, $address, $port) === false) {
3199
 
3200
			#設置錯誤訊息
3201
			$errorMsg="socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . PHP_EOL;
3202
 
3203
			#設置執行異常識別
3204
			$result["status"]="false";
3205
 
3206
			#設置執行錯誤訊息
3207
			$result["error"]=$errorMsg;
3208
 
3209
			#回傳結果
3210
			return $return;
3211
 
3212
			}#if end
3213
 
3214
		#如果 socket listen 超過 5 秒仍未成功
3215
		if (socket_listen($sock, 5) === false) {
3216
 
3217
			#設置錯誤訊息
3218
			$errorMsg="socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . PHP_EOL;
3219
 
3220
			#設置執行異常識別
3221
			$result["status"]="false";
3222
 
3223
			#設置執行錯誤訊息
3224
			$result["error"]=$errorMsg;
3225
 
3226
			#回傳結果
3227
			return $return;
3228
 
3229
			}#if end
3230
 
3231
		#初始化儲存 server 資訊的變數,預設數值為 $result 自己
3232
		$result["internet"]["serverCache"]=&$result;
3233
 
3234
		#初始化方便識別 server 資訊的變數
3235
		$internetServerCache=&$result["internet"]["serverCache"];
3236
 
3237
		#初始化 server site cache
3238
		$internetServerCache["serverSide"]=array();
3239
 
3240
		#初始化 儲存 透過 proc 執行的子程序
3241
		$internetServerCache["serverSide"]["procs"]=array();
3242
 
3243
		#初始化 client site cache
3244
		$internetServerCache["clientSide"]=array();
3245
 
3246
		#初始化方便識別 client site cache 資訊的變數
3247
		$internetClientCache=&$internetServerCache["clientSide"];
3248
 
3249
		#create a list of all the clients that will be connected to us..
3250
		#add the listening socket to this list
3251
		$internetClients = array("serverSock"=>$sock);
3252
 
3253
		#初始化 client 陣列
3254
		$client=array("intranet"=>$intranetClients,"internet"=>$internetClients);
3255
 
3256
		#設置無窮圈圈
3257
		while(true){
3258
 
3259
 
3260
 
3261
			}#while end
3262
 
3263
		}#function internetAndIntranetServer end
3264
 
3265
	/*
3266
	#函式說明:
3 liveuser 3267
	#連線到 unixDomainSockServer 提供的 unix domain socket.
3268
	#回傳結果:
3269
	#$result["status"],"true"代表執行正常;"false"代表執行不正常.
3270
	#$result["error"],錯誤訊息陣列.
3271
	#$result["function"],當前執行的函式名稱.
3272
	#$result["content"],取得的回應,讀到EOL,才會結束.
3273
	#必填參數:
3274
	#$conf["sock"],字串,要連線的unix domain socket.
3275
	$conf["sock"]="";
3276
	#可省略參數:
3277
	#$conf["id"],字串,取得的id,若無此值,則會得到新的數值.
3278
	#$conf["id"]="";
3279
	#$conf["cmd"],字串,要執行的指令,當$conf["id"]參數合法時,才會執行.
3280
	#$conf["cmd"]="";
3281
	#$conf["param"],參數陣列.
3282
	#$conf["param"]=array();
3283
	#$conf["escaped"],字串,param參數是否已經escaped了,預設為"false",反之為"true".
3284
	#$conf["escaped"]="true";
3285
	#$conf["custom"],陣列,要客制化傳輸的內容,會覆蓋以上除了$conf["id"]以外的可省略參數.
3286
	#$conf["custom"]=array();
3287
	#$conf["raw"],字串,要直接傳送的字串內容,會忽略其他可省略參數.
3288
	#$conf["raw"]="";
3289
	#參考資料:
3290
	#http://php.net/manual/en/function.stream-socket-client.php
3291
	#http://php.net/manual/en/function.stream-get-contents.php
3292
	#備註:
3293
	#無.
3294
	*/
3295
	public static function unixDomainSockClient(&$conf){
226 liveuser 3296
 
3 liveuser 3297
		#初始化要回傳的結果
3298
		$result=array();
3299
 
3300
		#取得當前執行的函數名稱
3301
		$result["function"]=__FUNCTION__;
226 liveuser 3302
 
3 liveuser 3303
		#如果沒有參數
3304
		if(func_num_args()==0){
226 liveuser 3305
 
3 liveuser 3306
			#設置執行失敗
3307
			$result["status"]="false";
226 liveuser 3308
 
3 liveuser 3309
			#設置執行錯誤訊息
3310
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 3311
 
3 liveuser 3312
			#回傳結果
3313
			return $result;
226 liveuser 3314
 
3 liveuser 3315
			}#if end
3316
 
3317
		#取得參數
3318
		$result["argu"]=$conf;
3319
 
3320
		#如果 $conf 不為陣列
3321
		if(gettype($conf)!="array"){
226 liveuser 3322
 
3 liveuser 3323
			#設置執行失敗
3324
			$result["status"]="false";
226 liveuser 3325
 
3 liveuser 3326
			#設置執行錯誤訊息
3327
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 3328
 
3 liveuser 3329
			#如果傳入的參數為 null
3330
			if($conf==null){
226 liveuser 3331
 
3 liveuser 3332
				#設置執行錯誤訊息
3333
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 3334
 
3 liveuser 3335
				}#if end
3336
 
3337
			#回傳結果
3338
			return $result;
226 liveuser 3339
 
3 liveuser 3340
			}#if end
226 liveuser 3341
 
3 liveuser 3342
		#檢查參數
3343
		#函式說明:
3344
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
3345
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
3346
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
3347
		#$result["function"],當前執行的函式名稱.
3348
		#$result["argu"],設置給予的參數.
3349
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
3350
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
3351
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
3352
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
3353
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
3354
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
3355
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
3356
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
3357
		#必填寫的參數:
3358
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
226 liveuser 3359
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
3 liveuser 3360
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
3361
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
3362
		#可以省略的參數:
3363
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
3364
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("sock");
3365
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
3366
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string");
3367
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
3368
		#$conf["canBeEmptyString"]="false";
3369
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
3370
		#$conf["canNotBeEmpty"]=array();
3371
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
3372
		#$conf["canBeEmpty"]=array();
3373
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
3374
		$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("id","cmd","param","escaped","custom");
3375
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
3376
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("id","cmd","param","escaped","custom","raw");
226 liveuser 3377
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 3378
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string","array","string","array","string");
3379
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
3380
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(null,null,null,"false",null,null);
3381
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
3382
		#$conf["disallowAllSkipableVarIsEmpty"]="";
3383
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
3384
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
3385
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
3386
		#$conf["arrayCountEqualCheck"][]=array();
3387
		#參考資料來源:
3388
		#array_keys=>http://php.net/manual/en/function.array-keys.php
3389
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
3390
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 3391
 
3 liveuser 3392
		#如果檢查參數失敗
3393
		if($checkArguments["status"]==="false"){
226 liveuser 3394
 
3 liveuser 3395
			#設置執行失敗
3396
			$result["status"]="false";
226 liveuser 3397
 
3 liveuser 3398
			#設置錯誤訊息
3399
			$result["error"]=$checkArguments;
226 liveuser 3400
 
3 liveuser 3401
			#回傳結果
3402
			return $result;
226 liveuser 3403
 
3 liveuser 3404
			}#if end
226 liveuser 3405
 
3 liveuser 3406
		#如果檢查參數不通過
3407
		if($checkArguments["passed"]==="false"){
226 liveuser 3408
 
3 liveuser 3409
			#設置執行失敗
3410
			$result["status"]="false";
226 liveuser 3411
 
3 liveuser 3412
			#設置錯誤訊息
3413
			$result["error"]=$checkArguments;
226 liveuser 3414
 
3 liveuser 3415
			#回傳結果
3416
			return $result;
226 liveuser 3417
 
3 liveuser 3418
			}#if end
226 liveuser 3419
 
3 liveuser 3420
		#檢查 unix socket 檔案 $conf["sock"] 是否存在.
3421
		#函式說明:
3422
		#檢查多個檔案與資料夾是否存在.
3423
		#回傳的結果:
3424
		#$result["status"],執行正常與否,"true"代表正常,"false"代表不正常.
3425
		#$result["error"],錯誤訊息陣列.
3426
		#$resutl["function"],當前執行的涵式名稱.
3427
		#$result["argu"],使用的參數.
3428
		#$result["allExist"],所有檔案皆存在的識別,"true"代表皆存在,"false"代表沒有全部都存在.
3429
		#$result["varName"][$i],爲第$i個資料夾或檔案的路徑與名稱。
3430
		#$result["varNameFullPath"][$i],爲第$i個資料夾或檔案的完整檔案系統路徑與名稱,如果不存在則代表路徑是網址.
3431
		#$result["varNameWebPath"][$i],為第$i個資料夾或檔案的網址
3432
		#$result["varExist"][$i],爲第$i個資料夾或檔案是否存在,"true"代表存在,"false"代表不存在。
3433
		#必填參數:
3434
		#$conf["fileArray"],陣列字串,要檢查是否存在的檔案有哪些,須爲一維陣列數值。
3435
		$conf["variableCheck::checkArguments"]["fileArray"]=array($conf["sock"]);
3436
		#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
3437
		$conf["variableCheck::checkArguments"]["fileArgu"]=__FILE__;
3438
		#可省略參數:
3439
		#$conf["disableWebSearch"],"字串",是否取消「當檔案找不到時,改用catchWebContent類別的wget函數來檢查檔案是否存在於網路上」的功能,"false"不取消,若要取消該功能請設為"true",若抓到的內容為空字串則會視為檔案不存在,預設為"true".
3440
		#$conf["disableWebSearch"]="false";
3441
		#$conf["userDir"],字串,網頁是否置放於家目錄底下,"true"為是,"false"為不是,預設為"true".
3442
		$conf["variableCheck::checkArguments"]["userDir"]="false";
3443
		#$conf["web"],字串,檔案是放在web就是"true",反之為檔案系統"false",預設為"true".
3444
		$conf["variableCheck::checkArguments"]["web"]="false";
3445
		#參考資料:
3446
		#http://php.net/manual/en/function.file-exists.php
3447
		#http://php.net/manual/en/control-structures.foreach.php
3448
		#備註:
3449
		#函數file_exists檢查的路徑為檔案系統的路徑
3450
		#$result["varName"][$i]結果未實作
3451
		$checkMultiFileExist=fileAccess::checkMultiFileExist($conf["variableCheck::checkArguments"]);
3452
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 3453
 
3 liveuser 3454
		#如果檢查失敗
3455
		if($checkMultiFileExist["status"]==="false"){
226 liveuser 3456
 
3 liveuser 3457
			#設置執行失敗
3458
			$result["status"]="false";
226 liveuser 3459
 
3 liveuser 3460
			#設置錯誤訊息
3461
			$result["error"]=$checkMultiFileExist;
226 liveuser 3462
 
3 liveuser 3463
			#回傳結果
3464
			return $result;
226 liveuser 3465
 
3 liveuser 3466
			}#if end
226 liveuser 3467
 
3 liveuser 3468
		#如果檢查失敗
3469
		if($checkMultiFileExist["allExist"]==="false"){
226 liveuser 3470
 
3 liveuser 3471
			#設置執行失敗
3472
			$result["status"]="false";
226 liveuser 3473
 
3 liveuser 3474
			#設置錯誤訊息
3475
			$result["error"]=$checkMultiFileExist;
226 liveuser 3476
 
3 liveuser 3477
			#回傳結果
3478
			return $result;
226 liveuser 3479
 
3 liveuser 3480
			}#if end
226 liveuser 3481
 
3 liveuser 3482
		#初始化取得的回應
3483
		$result["content"]="";
226 liveuser 3484
 
3 liveuser 3485
		#debug
3486
		#var_dump(__LINE__,"before connect");
226 liveuser 3487
 
3 liveuser 3488
		#嘗試30秒內連線到 unix domain socket
3489
		$fp=@stream_socket_client("unix://".$conf["sock"], $errno, $errstr, 30, STREAM_CLIENT_ASYNC_CONNECT );
226 liveuser 3490
 
3 liveuser 3491
		#如果開啟 unix domain socket 失敗
3492
		if(!$fp){
226 liveuser 3493
 
3 liveuser 3494
			#設置執行錯誤識別
3495
			$result["status"]="false";
226 liveuser 3496
 
3 liveuser 3497
			#設置錯誤訊息
3498
			$result["error"]=$errstr." (".$errno.")";
226 liveuser 3499
 
3 liveuser 3500
			#回傳結果
3501
			return $result;
226 liveuser 3502
 
3 liveuser 3503
			}#if end
226 liveuser 3504
 
3 liveuser 3505
		#反之開啟 unix domain socket 成功,且沒有raw參數存在
3506
		else if(!isset($conf["raw"])){
226 liveuser 3507
 
3 liveuser 3508
			#初始化要傳送的要求
3509
			$req=array();
226 liveuser 3510
 
3 liveuser 3511
			#如果有設置 $conf["id"]
3512
			if(isset($conf["id"])){
226 liveuser 3513
 
3 liveuser 3514
				#設置識別自己的id
3515
				$req["id"]=$conf["id"];
226 liveuser 3516
 
3 liveuser 3517
				}#if end
226 liveuser 3518
 
3 liveuser 3519
			#如果有設置 $conf["cmd"]
3520
			if(isset($conf["cmd"])){
226 liveuser 3521
 
3 liveuser 3522
				#設置要執行的指令
3523
				$req["cmd"]=$conf["cmd"];
226 liveuser 3524
 
3 liveuser 3525
				}#if end
226 liveuser 3526
 
3 liveuser 3527
			#如果有設置 $conf["param"]
3528
			if(isset($conf["param"])){
226 liveuser 3529
 
3 liveuser 3530
				#設置要執行的指令參數
3531
				$req["param"]=$conf["param"];
226 liveuser 3532
 
3 liveuser 3533
				#設置參數是否已經escaped
3534
				$req["escaped"]=$conf["escaped"];
226 liveuser 3535
 
3 liveuser 3536
				}#if end
226 liveuser 3537
 
3 liveuser 3538
			#如果有設置 $conf["custom"]
3539
			if(isset($conf["custom"])){
226 liveuser 3540
 
3 liveuser 3541
				#用 $conf["custom"] 來取代 $req
3542
				$req=$conf["custom"];
226 liveuser 3543
 
3 liveuser 3544
				#如果有設置 $conf["id"]
3545
				if(isset($conf["id"])){
226 liveuser 3546
 
3 liveuser 3547
					#設置識別自己的id
3548
					$req["id"]=$conf["id"];
226 liveuser 3549
 
3 liveuser 3550
					}#if end
226 liveuser 3551
 
3 liveuser 3552
				}#if end
226 liveuser 3553
 
3 liveuser 3554
			#debug
3555
			#var_dump(__LINE__,"before write ".json_encode($req));
226 liveuser 3556
 
3 liveuser 3557
			#傳送要求
3558
			fwrite($fp,json_encode($req).PHP_EOL);
226 liveuser 3559
 
3 liveuser 3560
			#結束要求
3561
			fwrite($fp,PHP_EOL.PHP_EOL);
226 liveuser 3562
 
3563
			#如果回應為空
3 liveuser 3564
			while(strlen($result["content"])<2){
226 liveuser 3565
 
3 liveuser 3566
				#當有回應時
3567
				while(!feof($fp)){
226 liveuser 3568
 
3 liveuser 3569
					#讀取回應
3570
					$tmp=fgets($fp, 1024);
226 liveuser 3571
 
3 liveuser 3572
					#debug
3573
					#var_dump($tmp);
226 liveuser 3574
 
3 liveuser 3575
					#debug
3576
					#var_dump(__LINE__,"got ".$tmp);
226 liveuser 3577
 
3 liveuser 3578
					#如果讀到 PHP_EOL
3579
					if($tmp===PHP_EOL){
226 liveuser 3580
 
3 liveuser 3581
						#結束讀取回應
3582
						break 2;
226 liveuser 3583
 
3 liveuser 3584
						}#if end
226 liveuser 3585
 
3 liveuser 3586
					#串接回應
3587
					$result["content"]=$result["content"].$tmp;
226 liveuser 3588
 
3 liveuser 3589
					}#while end
226 liveuser 3590
 
3 liveuser 3591
				}#while end
226 liveuser 3592
 
3 liveuser 3593
			#關閉 socket
3594
			fclose($fp);
226 liveuser 3595
 
3 liveuser 3596
			}#else end
226 liveuser 3597
 
3 liveuser 3598
		#反之有有raw參數存在
3599
		else{
226 liveuser 3600
 
3 liveuser 3601
			#傳送要求
3602
			fwrite($fp,trim($conf["raw"]).PHP_EOL);
226 liveuser 3603
 
3 liveuser 3604
			#當有回應時
3605
			while(!feof($fp)){
226 liveuser 3606
 
3 liveuser 3607
				#讀取回應
3608
				$tmp=fgets($fp, 1024);
226 liveuser 3609
 
3 liveuser 3610
				#debug
3611
				#var_dump($tmp);
226 liveuser 3612
 
3 liveuser 3613
				#debug
3614
				#var_dump(__LINE__,"got ".$tmp);
226 liveuser 3615
 
3 liveuser 3616
				#如果讀到 PHP_EOL
3617
				if($tmp===PHP_EOL){
226 liveuser 3618
 
3 liveuser 3619
					#結束讀取回應
226 liveuser 3620
					break;
3621
 
3 liveuser 3622
					}#if end
226 liveuser 3623
 
3 liveuser 3624
				#串接回應
3625
				$result["content"]=$result["content"].$tmp;
226 liveuser 3626
 
3 liveuser 3627
				}#while end
226 liveuser 3628
 
3 liveuser 3629
			#關閉 socket
226 liveuser 3630
			fclose($fp);
3631
 
3 liveuser 3632
			}#else end
226 liveuser 3633
 
3 liveuser 3634
		#設置執行正常
3635
		$result["status"]="true";
226 liveuser 3636
 
3 liveuser 3637
		#回傳結果
3638
		return $result;
226 liveuser 3639
 
3 liveuser 3640
		}#function unixDomainSockClient end
226 liveuser 3641
 
3 liveuser 3642
	/*
3643
	#函式說明:
3644
	#連線到 usr/bin/qbpwcf-usock.php 產生的  unix domain socket,運行指定的指令.
3645
	#回傳結果:
3646
	#$result["status"],"true"代表執行正常;"false"代表執行不正常.
3647
	#$result["error"],錯誤訊息陣列.
3648
	#$result["function"],當前執行的函式名稱.
3649
	#$result["argu"],使用的參數.
121 liveuser 3650
	#$result["content"],執行完指令的結果,回傳的格式為external::callShell的回傳結果.
3 liveuser 3651
	#必填參數:
3652
	#$conf["fileArgu"],字串,變數__FILE__的內容.
3653
	$conf["fileArgu"]=__FILE__;
3654
	#$conf["command"],字串,要執行的指令名稱.
3655
	$conf["command"]="";
3656
	#可省略參數:
3657
	#$conf["sock"],字串,要連線的 usr/bin/qbpwcf-sock.php(sock::unixDomainSockServer) 所產生的 unix domain socket 路徑與名稱,預設為 qbpwcf_usock_path.
3658
	#$conf["sock"]=qbpwcf_usock_path;
3659
	#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
3660
	#$conf["argu"]=array();
3661
	#$conf["commandIncludeArgu"],字串,是否command含有參數,預設為"false"代表沒有;反之為"true".
3662
	#$conf["commandIncludeArgu"]="false";
3663
	#$conf["commandInBg"],字串,是否要將程序放在背景執行,再取得相關資訊,預設為"false"代表不要;反之為"true".
3664
	#$conf["commandInBg"]="false";
3665
	#參考資料:
3666
	#無.
3667
	#備註:
3668
	#無.
3669
	*/
3670
	public static function execAnyCmdByQBPWCFunixSocket(&$conf){
226 liveuser 3671
 
3 liveuser 3672
		#初始化要回傳的結果
3673
		$result=array();
3674
 
3675
		#取得當前執行的函數名稱
3676
		$result["function"]=__FUNCTION__;
226 liveuser 3677
 
3 liveuser 3678
		#如果沒有參數
3679
		if(func_num_args()==0){
226 liveuser 3680
 
3 liveuser 3681
			#設置執行失敗
3682
			$result["status"]="false";
226 liveuser 3683
 
3 liveuser 3684
			#設置執行錯誤訊息
3685
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 3686
 
3 liveuser 3687
			#回傳結果
3688
			return $result;
226 liveuser 3689
 
3 liveuser 3690
			}#if end
226 liveuser 3691
 
3 liveuser 3692
		#取得參數
3693
		$result["argu"]=$conf;
3694
 
3695
		#如果 $conf 不為陣列
3696
		if(gettype($conf)!="array"){
226 liveuser 3697
 
3 liveuser 3698
			#設置執行失敗
3699
			$result["status"]="false";
226 liveuser 3700
 
3 liveuser 3701
			#設置執行錯誤訊息
3702
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 3703
 
3 liveuser 3704
			#如果傳入的參數為 null
3705
			if($conf==null){
226 liveuser 3706
 
3 liveuser 3707
				#設置執行錯誤訊息
3708
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 3709
 
3 liveuser 3710
				}#if end
3711
 
3712
			#回傳結果
3713
			return $result;
226 liveuser 3714
 
3 liveuser 3715
			}#if end
226 liveuser 3716
 
3 liveuser 3717
		#檢查參數
3718
		#函式說明:
3719
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
3720
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
3721
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
3722
		#$result["function"],當前執行的函式名稱.
3723
		#$result["argu"],設置給予的參數.
3724
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
3725
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
3726
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
3727
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
3728
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
3729
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
3730
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
3731
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
3732
		#必填寫的參數:
3733
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
226 liveuser 3734
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
3 liveuser 3735
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
3736
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
3737
		#可以省略的參數:
3738
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
3739
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("fileArgu","command");
3740
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
3741
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string");
3742
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
3743
		#$conf["canBeEmptyString"]="false";
3744
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
3745
		#$conf["canNotBeEmpty"]=array();
3746
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
3747
		#$conf["canBeEmpty"]=array();
3748
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
3749
		$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("sock");
3750
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
3751
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("sock","argu","commandIncludeArgu","commandInBg");
226 liveuser 3752
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 3753
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","array","string","string");
3754
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
3755
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(qbpwcf_usock_path,array(),"false","false");
3756
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
3757
		#$conf["disallowAllSkipableVarIsEmpty"]="";
3758
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
3759
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
3760
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
3761
		#$conf["arrayCountEqualCheck"][]=array();
3762
		#參考資料來源:
3763
		#array_keys=>http://php.net/manual/en/function.array-keys.php
3764
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
3765
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 3766
 
3 liveuser 3767
		#如果檢查參數失敗
3768
		if($checkArguments["status"]==="false"){
226 liveuser 3769
 
3 liveuser 3770
			#設置執行失敗
3771
			$result["status"]="false";
226 liveuser 3772
 
3 liveuser 3773
			#設置錯誤訊息
3774
			$result["error"]=$checkArguments;
226 liveuser 3775
 
3 liveuser 3776
			#回傳結果
3777
			return $result;
226 liveuser 3778
 
3 liveuser 3779
			}#if end
226 liveuser 3780
 
3 liveuser 3781
		#如果檢查參數不通過
3782
		if($checkArguments["passed"]==="false"){
226 liveuser 3783
 
3 liveuser 3784
			#設置執行失敗
3785
			$result["status"]="false";
226 liveuser 3786
 
3 liveuser 3787
			#設置錯誤訊息
3788
			$result["error"]=$checkArguments;
226 liveuser 3789
 
3 liveuser 3790
			#回傳結果
3791
			return $result;
226 liveuser 3792
 
3 liveuser 3793
			}#if end
226 liveuser 3794
 
3 liveuser 3795
		#如果指令已經包含參數了
3796
		if($conf["commandIncludeArgu"]==="true"){
226 liveuser 3797
 
3 liveuser 3798
			#函式說明:
226 liveuser 3799
			#將指令字串解析成陣列,方便給予 external::callShell 使用
3 liveuser 3800
			#回傳結果:
3801
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
3802
			#$reuslt["error"],執行不正常結束的錯訊息陣列.
3803
			#$result["function"],當前執行的函式名稱.
3804
			#$result["content"],解析好的指令陣列.
3805
			#$result["cmd"],解析好的指令名稱.
3806
			#$result["argus"],解析好的參數陣列.
3807
			#$result["argu"],所使用的參數.
3808
			#必填參數
3809
			#$conf["cmdStr"],字串,要解析的指令字串
3810
			$conf["cmd::parseCmdString"]["cmdStr"]=$conf["command"];
3811
			#可省略參數:
3812
			#無.
3813
			#參考資料:
3814
			#無.
3815
			#備註:
3816
			#無.
3817
			$parseCmdString=cmd::parseCmdString($conf["cmd::parseCmdString"]);
3818
			unset($conf["cmd::parseCmdString"]);
226 liveuser 3819
 
3 liveuser 3820
			#debug
220 liveuser 3821
			#var_dump(__FUNCTION__,__LINE__,$parseCmdString);exit;
226 liveuser 3822
 
3 liveuser 3823
			#如果執行失敗
3824
			if($parseCmdString["status"]==="false"){
226 liveuser 3825
 
3 liveuser 3826
				#設置執行失敗
3827
				$result["status"]="false";
226 liveuser 3828
 
3 liveuser 3829
				#設置錯誤訊息
3830
				$result["error"]=$parseCmdString;
226 liveuser 3831
 
3 liveuser 3832
				#回傳結果
3833
				return $result;
226 liveuser 3834
 
3 liveuser 3835
				}#if end
226 liveuser 3836
 
3 liveuser 3837
			#取得解析好的指令名稱
3838
			$conf["command"]=$parseCmdString["cmd"];
226 liveuser 3839
 
3 liveuser 3840
			#取得解析好的參數陣列
3841
			$conf["argu"]=$parseCmdString["argus"];
226 liveuser 3842
 
3 liveuser 3843
			#debug
3844
			#var_dump(__LINE__,$parseCmdString);
226 liveuser 3845
 
3 liveuser 3846
			}#if end
226 liveuser 3847
 
3 liveuser 3848
		#函式說明:
3849
		#呼叫shell執行系統命令,並取得回傳的內容.
3850
		#回傳結果:
3851
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
3852
		#$result["error"],錯誤訊息陣列.
3853
		#$result["function"],當前執行的函數名稱.
3854
		#$result["argu"],使用的參數.
3855
		#$result["cmd"],執行的指令內容.
3856
		#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
3857
		#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
3858
		#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
3859
		#$result["running"],是否還在執行.
3860
		#$result["pid"],pid.
3861
		#$result["statusCode"],執行結束後的代碼.
3862
		#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu").
3863
		#必填參數:
3864
		#$conf["command"],字串,要執行的指令.
3865
		$conf["external::callShell"]["command"]=$conf["command"];
3866
		#$conf["fileArgu"],字串,變數__FILE__的內容.
3867
		$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];
3868
		#可省略參數:
3869
		#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
3870
		$conf["external::callShell"]["argu"]=$conf["argu"];
3871
		#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
3872
		#$conf["arguIsAddr"]=array();
3873
		#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
3874
		#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
3875
		#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
3876
		#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
3877
		#$conf["enablePrintDescription"]="true";
3878
		#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
3879
		#$conf["printDescription"]="";
3880
		#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
3881
		$conf["external::callShell"]["escapeshellarg"]="true";
3882
		#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
3883
		#$conf["thereIsShellVar"]=array();
3884
		#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
3885
		#$conf["username"]="";
3886
		#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
3887
		#$conf["password"]="";
3888
		#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
3889
		#$conf["useScript"]="";
3890
		#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
3891
		#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
3892
		#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
3893
		#$conf["inBackGround"]="";
3894
		#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
3895
		#$conf["getErr"]="false";
3896
		#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
3897
		$conf["external::callShell"]["doNotRun"]="true";
3898
		#參考資料:
3899
		#exec=>http://php.net/manual/en/function.exec.php
3900
		#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
3901
		#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
3902
		#備註:
3903
		#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
3904
		#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
3905
		$callShell=external::callShell($conf["external::callShell"]);
3906
		unset($conf["external::callShell"]);
226 liveuser 3907
 
3 liveuser 3908
		#debug
3909
		#var_dump(__LINE__,$callShell);
226 liveuser 3910
 
3 liveuser 3911
		#如果檢查參數作業出錯
3912
		if($callShell["status"]=="false"){
226 liveuser 3913
 
3 liveuser 3914
			#設置執行錯誤識別
3915
			$result["status"]="false";
226 liveuser 3916
 
3 liveuser 3917
			#設置錯誤訊息
3918
			$result["error"]=$callShell;
226 liveuser 3919
 
3 liveuser 3920
			#回傳結果
3921
			return $result;
226 liveuser 3922
 
3 liveuser 3923
			}#if end
226 liveuser 3924
 
3 liveuser 3925
		#函式說明:
3926
		#連線到 unixDomainSockServer 提供的 unix domain socket.
3927
		#回傳結果:
3928
		#$result["status"],"true"代表執行正常;"false"代表執行不正常.
3929
		#$result["error"],錯誤訊息陣列.
3930
		#$result["function"],當前執行的函式名稱.
3931
		#$result["content"],取得的回應.
3932
		#必填參數:
3933
		#$conf["sock"],字串,要連線的unix domain socket.
3934
		$conf["sock::unixDomainSockClient"]["sock"]=$conf["sock"];
3935
		#可省略參數:
3936
		#$conf["id"],字串,取得的id,若無此值,則會得到新的數值.
3937
		#$conf["id"]="";
3938
		#$conf["cmd"],字串,要執行的指令,當$conf["id"]參數合法時,才會執行.
259 liveuser 3939
		$conf["sock::unixDomainSockClient"]["cmd"]=$callShell["noEscaped"]["cmd"];
3 liveuser 3940
		#$conf["param"],參數陣列.
259 liveuser 3941
		$conf["sock::unixDomainSockClient"]["param"]=$callShell["noEscaped"]["argu"];
3 liveuser 3942
		#$conf["escaped"],字串,param參數是否已經escaped了,預設為"false",反之為"true".
3943
		$conf["sock::unixDomainSockClient"]["escaped"]="true";
3944
		#$conf["custom"],陣列,要客制化傳輸的內容,會覆蓋以上除了$conf["id"]以外的可省略參數.
226 liveuser 3945
 
3 liveuser 3946
		#如果要在背景執行指令
3947
		if($conf["commandInBg"]==="true" && isset($conf["sock::unixDomainSockClient"]["cmd"]) ){
226 liveuser 3948
 
3 liveuser 3949
			#設定要在背景執行的指令
3950
			$conf["sock::unixDomainSockClient"]["custom"]["cmdInBg"]=$conf["sock::unixDomainSockClient"]["cmd"];
226 liveuser 3951
 
3 liveuser 3952
			#設定要在背景執行的指令
3953
			$conf["sock::unixDomainSockClient"]["custom"]["param"]=$conf["sock::unixDomainSockClient"]["param"];
226 liveuser 3954
 
3 liveuser 3955
			#移除不要的參數
3956
			unset($conf["sock::unixDomainSockClient"]["cmd"]);
226 liveuser 3957
 
3 liveuser 3958
			#移除不要的參數
3959
			unset($conf["sock::unixDomainSockClient"]["param"]);
226 liveuser 3960
 
3 liveuser 3961
			#保存 custom 參數
3962
			$custom=$conf["sock::unixDomainSockClient"]["custom"];
226 liveuser 3963
 
3 liveuser 3964
			#debug
3965
			#var_dump(__LINE__,$custom);
226 liveuser 3966
 
3 liveuser 3967
			}#if end
226 liveuser 3968
 
3 liveuser 3969
		#$conf["raw"],字串,要直接傳送的字串內容,會忽略其他可省略參數.
3970
		#$conf["raw"]="";
3971
		#參考資料:
3972
		#http://php.net/manual/en/function.stream-socket-client.php
3973
		#http://php.net/manual/en/function.stream-get-contents.php
3974
		#備註:
3975
		#無.
3976
		$paramsOfUnixDomainSockClient=$conf["sock::unixDomainSockClient"];
3977
		$unixDomainSockClient=sock::unixDomainSockClient($paramsOfUnixDomainSockClient);
3978
		unset($paramsOfUnixDomainSockClient);
226 liveuser 3979
 
3 liveuser 3980
		#如果執行失敗
3981
		if($unixDomainSockClient["status"]==="false"){
226 liveuser 3982
 
3 liveuser 3983
			#設置執行錯誤識別
3984
			$result["status"]="false";
226 liveuser 3985
 
3 liveuser 3986
			#設置錯誤訊息
3987
			$result["error"]=$unixDomainSockClient;
226 liveuser 3988
 
3 liveuser 3989
			#回傳結果
3990
			return $result;
226 liveuser 3991
 
3 liveuser 3992
			}#if end
226 liveuser 3993
 
3 liveuser 3994
		#debug
3995
		#var_dump(__LINE__,$unixDomainSockClient);
226 liveuser 3996
 
3 liveuser 3997
		#如果不是 json 字串
3998
		if(json_validate(trim($unixDomainSockClient["content"]))==="false"){
226 liveuser 3999
 
3 liveuser 4000
			#設置執行錯誤識別
4001
			$result["status"]="false";
226 liveuser 4002
 
3 liveuser 4003
			#設置錯誤訊息
4004
			$result["error"][]=trim($unixDomainSockClient["content"])." is not json string!";
226 liveuser 4005
 
3 liveuser 4006
			#設置錯誤訊息
4007
			$result["error"][]=$unixDomainSockClient;
226 liveuser 4008
 
3 liveuser 4009
			#回傳結果
4010
			return $result;
226 liveuser 4011
 
3 liveuser 4012
			}#if end
226 liveuser 4013
 
3 liveuser 4014
		#取得json回應
4015
		$jsonRes=json_decode(trim($unixDomainSockClient["content"]));
226 liveuser 4016
 
3 liveuser 4017
		#debug
4018
		#var_dump(__LINE__,$jsonRes);
226 liveuser 4019
 
3 liveuser 4020
		#如果執行失敗
4021
		if($jsonRes===null){
226 liveuser 4022
 
3 liveuser 4023
			#設置執行錯誤識別
4024
			$result["status"]="false";
226 liveuser 4025
 
3 liveuser 4026
			#設置錯誤訊息
4027
			$result["error"]=$unixDomainSockClient;
226 liveuser 4028
 
3 liveuser 4029
			#回傳結果
4030
			return $result;
226 liveuser 4031
 
3 liveuser 4032
			}#if end
226 liveuser 4033
 
3 liveuser 4034
		#如果沒有產生新id
4035
		if(!isset($jsonRes->id)){
226 liveuser 4036
 
3 liveuser 4037
			#設置執行錯誤識別
4038
			$result["status"]="false";
226 liveuser 4039
 
3 liveuser 4040
			#設置錯誤訊息
4041
			$result["error"]=$unixDomainSockClient;
226 liveuser 4042
 
3 liveuser 4043
			#回傳結果
4044
			return $result;
226 liveuser 4045
 
3 liveuser 4046
			}#if end
226 liveuser 4047
 
3 liveuser 4048
		#用新的id再傳送一次要求給 qbpwcf_usock_path
4049
		$paramsOfUnixDomainSockClient=$conf["sock::unixDomainSockClient"];
4050
		$paramsOfUnixDomainSockClient["id"]=$jsonRes->id;
4051
		#var_dump(__LINE__,$paramsOfUnixDomainSockClient);
4052
		$unixDomainSockClient=sock::unixDomainSockClient($paramsOfUnixDomainSockClient);
4053
		unset($paramsOfUnixDomainSockClient);
226 liveuser 4054
 
3 liveuser 4055
		#如果執行失敗
4056
		if($unixDomainSockClient["status"]==="false"){
226 liveuser 4057
 
3 liveuser 4058
			#設置執行錯誤識別
4059
			$result["status"]="false";
226 liveuser 4060
 
3 liveuser 4061
			#設置錯誤訊息
4062
			$result["error"]=$unixDomainSockClient;
226 liveuser 4063
 
3 liveuser 4064
			#回傳結果
4065
			return $result;
226 liveuser 4066
 
3 liveuser 4067
			}#if end
226 liveuser 4068
 
3 liveuser 4069
		#debug
4070
		#var_dump(__LINE__,$unixDomainSockClient["content"]);
226 liveuser 4071
 
4072
		#如果輸出非 json
3 liveuser 4073
		if(json_validate(trim($unixDomainSockClient["content"]))===false){
226 liveuser 4074
 
3 liveuser 4075
			#設置錯誤識別
4076
			$result["status"]="false";
226 liveuser 4077
 
3 liveuser 4078
			#設置錯誤訊息
4079
			$result["error"][]="got not json output";
226 liveuser 4080
 
3 liveuser 4081
			#設置錯誤訊息
4082
			$result["error"][]=json_last_error().":".json_last_error_msg();
226 liveuser 4083
 
3 liveuser 4084
			#回傳結果
4085
			return $result;
226 liveuser 4086
 
3 liveuser 4087
			}#if end
226 liveuser 4088
 
3 liveuser 4089
		#取得json回應
4090
		$jsonRes=(array)json_decode($unixDomainSockClient["content"]);
226 liveuser 4091
 
3 liveuser 4092
		#如果執行失敗
4093
		if($jsonRes===null){
226 liveuser 4094
 
3 liveuser 4095
			#設置執行錯誤識別
4096
			$result["status"]="false";
226 liveuser 4097
 
3 liveuser 4098
			#設置錯誤訊息
4099
			$result["error"]=$unixDomainSockClient;
226 liveuser 4100
 
3 liveuser 4101
			#回傳結果
4102
			return $result;
226 liveuser 4103
 
3 liveuser 4104
			}#if end
226 liveuser 4105
 
3 liveuser 4106
		#如果執行指令失敗
4107
		if($jsonRes["status"]==="false"){
226 liveuser 4108
 
3 liveuser 4109
			#設置執行錯誤識別
4110
			$result["status"]="false";
226 liveuser 4111
 
3 liveuser 4112
			#設置錯誤訊息
4113
			$result["error"]=$jsonRes["error"];
226 liveuser 4114
 
3 liveuser 4115
			#回傳結果
4116
			return $result;
226 liveuser 4117
 
3 liveuser 4118
			}#if end
226 liveuser 4119
 
3 liveuser 4120
		#執行正常
4121
		$result["status"]="true";
226 liveuser 4122
 
3 liveuser 4123
		#設置執行的結果
4124
		$result["content"]=$jsonRes;
226 liveuser 4125
 
3 liveuser 4126
		#回傳結果
4127
		return $result;
226 liveuser 4128
 
3 liveuser 4129
		}#public function execAnyCmdByQBPWCFunixSocket end
226 liveuser 4130
 
3 liveuser 4131
	/*
4132
	#函式說明:
4133
	#詢問透過 sock::unixDomainSockServer 執行的程序狀況
4134
	#回傳結果:
4135
	#$result["status"],"true"代表執行正常;"false"代表執行不正常.
4136
	#$result["error"],錯誤訊息陣列.
4137
	#$result["function"],當前執行的函式名稱.
4138
	#$result["argu"],使用的參數.
4139
	#$result["content"],陣列,程序的資訊.
4140
	#$result["content"]["pid"],字串,程序的pid.
4141
	#$result["content"]["running"],字串,是否正常執行中.
4142
	#$result["content"]["statusCode"],字串,回傳給 shell 的代碼.
4143
	#$result["content"]["input"],字串,輸入的內容.
4144
	#$result["content"]["output"],字串,標準輸出的內容.
4145
	#$result["content"]["error"],字串,錯誤輸出的內容.
4146
	#必填參數:
4147
	#$conf["uuid"],字串,proc的uuid.
4148
	$conf["uuid"]="";
4149
	#可省略參數:
4150
	#$conf["sock"],字串,要連線的 usr/bin/qbpwcf-sock.php(sock::unixDomainSockServer) 所產生的 unix domain socket 路徑與名稱,預設為 qbpwcf_usock_path.
4151
	#$conf["sock"]=qbpwcf_usock_path;
4152
	#參考資料:
4153
	#無.
4154
	#備註:
4155
	#無.
4156
	*/
4157
	public static function getProcInfo(&$conf){
226 liveuser 4158
 
3 liveuser 4159
		#初始化要回傳的結果
4160
		$result=array();
4161
 
4162
		#取得當前執行的函數名稱
4163
		$result["function"]=__FUNCTION__;
226 liveuser 4164
 
3 liveuser 4165
		#如果沒有參數
4166
		if(func_num_args()==0){
226 liveuser 4167
 
3 liveuser 4168
			#設置執行失敗
4169
			$result["status"]="false";
226 liveuser 4170
 
3 liveuser 4171
			#設置執行錯誤訊息
4172
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 4173
 
3 liveuser 4174
			#回傳結果
4175
			return $result;
226 liveuser 4176
 
3 liveuser 4177
			}#if end
226 liveuser 4178
 
3 liveuser 4179
		#取得參數
4180
		$result["argu"]=$conf;
4181
 
4182
		#如果 $conf 不為陣列
4183
		if(gettype($conf)!="array"){
226 liveuser 4184
 
3 liveuser 4185
			#設置執行失敗
4186
			$result["status"]="false";
226 liveuser 4187
 
3 liveuser 4188
			#設置執行錯誤訊息
4189
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 4190
 
3 liveuser 4191
			#如果傳入的參數為 null
4192
			if($conf==null){
226 liveuser 4193
 
3 liveuser 4194
				#設置執行錯誤訊息
4195
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 4196
 
3 liveuser 4197
				}#if end
4198
 
4199
			#回傳結果
4200
			return $result;
226 liveuser 4201
 
3 liveuser 4202
			}#if end
226 liveuser 4203
 
3 liveuser 4204
		#檢查參數
4205
		#函式說明:
4206
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
4207
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
4208
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
4209
		#$result["function"],當前執行的函式名稱.
4210
		#$result["argu"],設置給予的參數.
4211
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
4212
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
4213
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
4214
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
4215
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
4216
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
4217
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
4218
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
4219
		#必填寫的參數:
4220
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
226 liveuser 4221
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
3 liveuser 4222
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
4223
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
4224
		#可以省略的參數:
4225
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
4226
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("uuid");
4227
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
4228
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string");
4229
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
4230
		#$conf["canBeEmptyString"]="false";
4231
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
4232
		#$conf["canNotBeEmpty"]=array();
4233
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
4234
		#$conf["canBeEmpty"]=array();
4235
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
4236
		$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("sock");
4237
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
4238
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("sock");
226 liveuser 4239
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 4240
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string");
4241
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
4242
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(qbpwcf_usock_path);
4243
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
4244
		#$conf["disallowAllSkipableVarIsEmpty"]="";
4245
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
4246
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
4247
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
4248
		#$conf["arrayCountEqualCheck"][]=array();
4249
		#參考資料來源:
4250
		#array_keys=>http://php.net/manual/en/function.array-keys.php
4251
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
4252
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 4253
 
3 liveuser 4254
		#如果檢查參數失敗
4255
		if($checkArguments["status"]==="false"){
226 liveuser 4256
 
3 liveuser 4257
			#設置執行失敗
4258
			$result["status"]="false";
226 liveuser 4259
 
3 liveuser 4260
			#設置錯誤訊息
4261
			$result["error"]=$checkArguments;
226 liveuser 4262
 
3 liveuser 4263
			#回傳結果
4264
			return $result;
226 liveuser 4265
 
3 liveuser 4266
			}#if end
226 liveuser 4267
 
3 liveuser 4268
		#如果檢查參數不通過
4269
		if($checkArguments["passed"]==="false"){
226 liveuser 4270
 
3 liveuser 4271
			#設置執行失敗
4272
			$result["status"]="false";
226 liveuser 4273
 
3 liveuser 4274
			#設置錯誤訊息
4275
			$result["error"]=$checkArguments;
226 liveuser 4276
 
3 liveuser 4277
			#回傳結果
4278
			return $result;
226 liveuser 4279
 
3 liveuser 4280
			}#if end
226 liveuser 4281
 
3 liveuser 4282
		函式說明:
4283
		#連線到 unixDomainSockServer 提供的 unix domain socket.
4284
		#回傳結果:
4285
		#$result["status"],"true"代表執行正常;"false"代表執行不正常.
4286
		#$result["error"],錯誤訊息陣列.
4287
		#$result["function"],當前執行的函式名稱.
4288
		#$result["content"],取得的回應.
4289
		#必填參數:
4290
		#$conf["sock"],字串,要連線的unix domain socket.
4291
		$conf["sock::unixDomainSockClient"]["sock"]=$conf["sock"];
4292
		#可省略參數:
4293
		#$conf["id"],字串,取得的id,若無此值,則會得到新的數值.
4294
		#$conf["id"]="";
4295
		#$conf["cmd"],字串,要執行的指令,當$conf["id"]參數合法時,才會執行.
4296
		#$conf["cmd"]="";
4297
		#$conf["param"],參數陣列.
4298
		#$conf["param"]=array();
4299
		#$conf["escaped"],字串,param參數是否已經escaped了,預設為"false",反之為"true".
4300
		#$conf["escaped"]="true";
4301
		#$conf["custom"],陣列,要客制化傳輸的內容,會覆蓋以上除了$conf["id"]以外的可省略參數.
4302
		$conf["sock::unixDomainSockClient"]["custom"]=array("QueryProcByUUID"=>$conf["uuid"]);
4303
		#$conf["raw"],字串,要直接傳送的字串內容,會忽略其他可省略參數.
4304
		#$conf["raw"]="";
4305
		#參考資料:
4306
		#http://php.net/manual/en/function.stream-socket-client.php
4307
		#http://php.net/manual/en/function.stream-get-contents.php
4308
		#備註:
4309
		#無.
4310
		$paramsOfUnixDomainSockClient=$conf["sock::unixDomainSockClient"];
4311
		$unixDomainSockClient=sock::unixDomainSockClient($paramsOfUnixDomainSockClient);
4312
		unset($paramsOfUnixDomainSockClient);
226 liveuser 4313
 
3 liveuser 4314
		#如果執行失敗
4315
		if($unixDomainSockClient["status"]==="false"){
226 liveuser 4316
 
3 liveuser 4317
			#設置執行錯誤識別
4318
			$result["status"]="false";
226 liveuser 4319
 
3 liveuser 4320
			#設置錯誤訊息
4321
			$result["error"]=$unixDomainSockClient;
226 liveuser 4322
 
3 liveuser 4323
			#回傳結果
4324
			return $result;
226 liveuser 4325
 
3 liveuser 4326
			}#if end
226 liveuser 4327
 
3 liveuser 4328
		#debug
4329
		#var_dump(__LINE__,$unixDomainSockClient);
226 liveuser 4330
 
3 liveuser 4331
		#如果不是 json 字串
4332
		if(json_validate(trim($unixDomainSockClient["content"]))==="false"){
226 liveuser 4333
 
3 liveuser 4334
			#設置執行錯誤識別
4335
			$result["status"]="false";
226 liveuser 4336
 
3 liveuser 4337
			#設置錯誤訊息
4338
			$result["error"][]=trim($unixDomainSockClient["content"])." is not json string!";
226 liveuser 4339
 
3 liveuser 4340
			#設置錯誤訊息
4341
			$result["error"][]=$unixDomainSockClient;
226 liveuser 4342
 
3 liveuser 4343
			#回傳結果
4344
			return $result;
226 liveuser 4345
 
3 liveuser 4346
			}#if end
226 liveuser 4347
 
3 liveuser 4348
		#取得json回應
4349
		$jsonRes=json_decode(trim($unixDomainSockClient["content"]));
226 liveuser 4350
 
3 liveuser 4351
		#debug
4352
		#var_dump(__LINE__,$jsonRes);
226 liveuser 4353
 
3 liveuser 4354
		#如果執行失敗
4355
		if($jsonRes===null){
226 liveuser 4356
 
3 liveuser 4357
			#設置執行錯誤識別
4358
			$result["status"]="false";
226 liveuser 4359
 
3 liveuser 4360
			#設置錯誤訊息
4361
			$result["error"]=$unixDomainSockClient;
226 liveuser 4362
 
3 liveuser 4363
			#回傳結果
4364
			return $result;
226 liveuser 4365
 
3 liveuser 4366
			}#if end
226 liveuser 4367
 
3 liveuser 4368
		#如果沒有產生新id
4369
		if(!isset($jsonRes->id)){
226 liveuser 4370
 
3 liveuser 4371
			#設置執行錯誤識別
4372
			$result["status"]="false";
226 liveuser 4373
 
3 liveuser 4374
			#設置錯誤訊息
4375
			$result["error"]=$unixDomainSockClient;
226 liveuser 4376
 
3 liveuser 4377
			#回傳結果
4378
			return $result;
226 liveuser 4379
 
3 liveuser 4380
			}#if end
226 liveuser 4381
 
3 liveuser 4382
		#用新的id再傳送一次要求給 qbpwcf_usock_path
4383
		$paramsOfUnixDomainSockClient=$conf["sock::unixDomainSockClient"];
4384
		$paramsOfUnixDomainSockClient["id"]=$jsonRes->id;
4385
		$unixDomainSockClient=sock::unixDomainSockClient($paramsOfUnixDomainSockClient);
4386
		unset($paramsOfUnixDomainSockClient);
226 liveuser 4387
 
3 liveuser 4388
		#如果執行失敗
4389
		if($unixDomainSockClient["status"]==="false"){
226 liveuser 4390
 
3 liveuser 4391
			#設置執行錯誤識別
4392
			$result["status"]="false";
226 liveuser 4393
 
3 liveuser 4394
			#設置錯誤訊息
4395
			$result["error"]=$unixDomainSockClient;
226 liveuser 4396
 
3 liveuser 4397
			#回傳結果
4398
			return $result;
226 liveuser 4399
 
3 liveuser 4400
			}#if end
226 liveuser 4401
 
3 liveuser 4402
		#debug
4403
		#var_dump(__LINE__,$unixDomainSockClient["content"]);
226 liveuser 4404
 
4405
		#如果輸出非 json
3 liveuser 4406
		if(json_validate(trim($unixDomainSockClient["content"]))===false){
226 liveuser 4407
 
3 liveuser 4408
			#設置錯誤識別
4409
			$result["status"]="false";
226 liveuser 4410
 
3 liveuser 4411
			#設置錯誤訊息
4412
			$result["error"][]="got not json output";
226 liveuser 4413
 
3 liveuser 4414
			#設置錯誤訊息
4415
			$result["error"][]=json_last_error().":".json_last_error_msg();
226 liveuser 4416
 
3 liveuser 4417
			#設置原始訊息
4418
			$result["error"][]="ori res:".$unixDomainSockClient["content"];
226 liveuser 4419
 
3 liveuser 4420
			#回傳結果
4421
			return $result;
226 liveuser 4422
 
3 liveuser 4423
			}#if end
226 liveuser 4424
 
3 liveuser 4425
		#取得json回應
4426
		$jsonRes=(array)json_decode($unixDomainSockClient["content"]);
226 liveuser 4427
 
3 liveuser 4428
		#如果執行失敗
4429
		if($jsonRes===null){
226 liveuser 4430
 
3 liveuser 4431
			#設置執行錯誤識別
4432
			$result["status"]="false";
226 liveuser 4433
 
3 liveuser 4434
			#設置錯誤訊息
4435
			$result["error"]=$unixDomainSockClient;
226 liveuser 4436
 
3 liveuser 4437
			#回傳結果
4438
			return $result;
226 liveuser 4439
 
3 liveuser 4440
			}#if end
226 liveuser 4441
 
3 liveuser 4442
		#如果執行指令失敗
4443
		if($jsonRes["status"]==="false"){
226 liveuser 4444
 
3 liveuser 4445
			#設置執行錯誤識別
4446
			$result["status"]="false";
226 liveuser 4447
 
3 liveuser 4448
			#設置錯誤訊息
4449
			$result["error"]=$jsonRes["error"];
226 liveuser 4450
 
3 liveuser 4451
			#回傳結果
4452
			return $result;
226 liveuser 4453
 
3 liveuser 4454
			}#if end
226 liveuser 4455
 
3 liveuser 4456
		#移除不需要的元素
4457
		unset($jsonRes["status"]);
226 liveuser 4458
 
3 liveuser 4459
		#執行正常
4460
		$result["status"]="true";
226 liveuser 4461
 
3 liveuser 4462
		#$result["content"],陣列,程序的資訊.
4463
		#$result["content"]["pid"],字串,程序的pid.
4464
		#$result["content"]["running"],字串,是否正常執行中.
4465
		#$result["content"]["statusCode"],字串,回傳給 shell 的代碼.
4466
		#$result["content"]["input"],字串,輸入的內容.
4467
		#$result["content"]["output"],字串,標準輸出的內容.
4468
		#$result["content"]["error"],字串,錯誤輸出的內容.
226 liveuser 4469
 
3 liveuser 4470
		#初始執行的結果
4471
		$result["content"]=$jsonRes;
226 liveuser 4472
 
3 liveuser 4473
		#回傳結果
4474
		return $result;
226 liveuser 4475
 
3 liveuser 4476
		}#function getProcInfo end
226 liveuser 4477
 
3 liveuser 4478
	/*
4479
	#函式說明:
4480
	#傳送 raw 訊息到  unix domain socket.
4481
	#回傳結果:
4482
	#$result["status"],"true"代表執行正常;"false"代表執行不正常.
4483
	#$result["error"],錯誤訊息陣列.
4484
	#$result["function"],當前執行的函式名稱.
4485
	#$result["argu"],使用的參數.
4486
	#$result["content"],得到的回應.
4487
	#必填參數:
4488
	#$conf["msg"],字串,要傳送的訊息.
4489
	$conf["msg"]="";
4490
	#$conf["sock"],字串,要連線的的 unix domain socket 路徑與名稱
4491
	#$conf["sock"]="";
4492
	#可省略參數:
4493
	#無.
4494
	#參考資料:
4495
	#無.
4496
	#備註:
4497
	#無.
4498
	*/
4499
	public static function sendRawMsgUnixSocket(&$conf){
226 liveuser 4500
 
3 liveuser 4501
		#初始化要回傳的結果
4502
		$result=array();
4503
 
4504
		#取得當前執行的函數名稱
4505
		$result["function"]=__FUNCTION__;
226 liveuser 4506
 
3 liveuser 4507
		#如果沒有參數
4508
		if(func_num_args()==0){
226 liveuser 4509
 
3 liveuser 4510
			#設置執行失敗
4511
			$result["status"]="false";
226 liveuser 4512
 
3 liveuser 4513
			#設置執行錯誤訊息
4514
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 4515
 
3 liveuser 4516
			#回傳結果
4517
			return $result;
226 liveuser 4518
 
3 liveuser 4519
			}#if end
226 liveuser 4520
 
3 liveuser 4521
		#取得參數
4522
		$result["argu"]=$conf;
4523
 
4524
		#如果 $conf 不為陣列
4525
		if(gettype($conf)!="array"){
226 liveuser 4526
 
3 liveuser 4527
			#設置執行失敗
4528
			$result["status"]="false";
226 liveuser 4529
 
3 liveuser 4530
			#設置執行錯誤訊息
4531
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 4532
 
3 liveuser 4533
			#如果傳入的參數為 null
4534
			if($conf==null){
226 liveuser 4535
 
3 liveuser 4536
				#設置執行錯誤訊息
4537
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 4538
 
3 liveuser 4539
				}#if end
4540
 
4541
			#回傳結果
4542
			return $result;
226 liveuser 4543
 
3 liveuser 4544
			}#if end
226 liveuser 4545
 
3 liveuser 4546
		#函式說明:
4547
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
4548
		#回傳結果:
4549
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
4550
		#$result["error"],執行不正常結束的錯訊息陣列.
4551
		#$result["simpleError"],簡單表示的錯誤訊息.
4552
		#$result["function"],當前執行的函式名稱.
4553
		#$result["argu"],設置給予的參數.
4554
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
4555
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
4556
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
4557
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
4558
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
4559
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
4560
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
4561
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
4562
		#必填參數:
4563
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
4564
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
4565
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
4566
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
4567
		#可省略參數:
4568
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
4569
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("msg","sock");
4570
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
4571
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string");
4572
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
4573
		#$conf["canBeEmptyString"]="false";
4574
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
4575
		#$conf["canNotBeEmpty"]=array();
4576
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
4577
		#$conf["canBeEmpty"]=array();
4578
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
4579
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("sock");
4580
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
4581
		#$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("sock");
4582
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
4583
		#$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string");
4584
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
4585
		#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(qbpwcf_usock_path);
4586
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
4587
		#$conf["disallowAllSkipableVarIsEmpty"]="";
4588
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
4589
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
4590
		#$conf["disallowAllSkipableVarNotExist"],字串,是否不允許每個可省略參數都不存在,預設為"false"代表允許,反之為"true".
4591
		#$conf["disallowAllSkipableVarNotExist"]="";
4592
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
4593
		#$conf["arrayCountEqualCheck"][]=array();
4594
		#參考資料:
4595
		#array_keys=>http://php.net/manual/en/function.array-keys.php
4596
		#備註:
4597
		#無.
4598
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
4599
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 4600
 
3 liveuser 4601
		#如果執行失敗
4602
		if($checkArguments["status"]==="false"){
226 liveuser 4603
 
3 liveuser 4604
			#設置執行錯誤識別
4605
			$result["status"]="false";
226 liveuser 4606
 
3 liveuser 4607
			#設置錯誤訊息
4608
			$result["error"]=$checkArguments["error"];
226 liveuser 4609
 
3 liveuser 4610
			#回傳結果
4611
			return $result;
226 liveuser 4612
 
3 liveuser 4613
			}#if end
226 liveuser 4614
 
3 liveuser 4615
		#如果檢查不通過
4616
		if($checkArguments["status"]==="false"){
226 liveuser 4617
 
3 liveuser 4618
			#設置執行錯誤識別
4619
			$result["status"]="false";
226 liveuser 4620
 
3 liveuser 4621
			#設置錯誤訊息
4622
			$result["error"]=$checkArguments["error"];
226 liveuser 4623
 
3 liveuser 4624
			#回傳結果
4625
			return $result;
226 liveuser 4626
 
3 liveuser 4627
			}#if end
226 liveuser 4628
 
3 liveuser 4629
		#讓存放unix domain socket的路徑存在
4630
		#函式說明:
4631
		#確保路徑存在.
4632
		#回傳結果:
4633
		#$result["status"],執行正常與否,"true"代表正常,"false"代表不正常.
4634
		#$result["error"],錯誤訊息陣列.
4635
		#$resutl["function"],當前執行的涵式名稱.
4636
		#$result["path"],建立好的路徑字串.
4637
		#$result["fileName"],檔案名稱,若 $conf["haveFileName"] 為 "true" 則會回傳.
4638
		#$result["argu"],使用的參數.
4639
		#必填參數:
4640
		#$conf["path"],要檢查的路徑
4641
		$conf["fileAccess::validatePath"]["path"]=$conf["sock"];
4642
		#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
4643
		$conf["fileAccess::validatePath"]["fileArgu"]=__FILE__;
4644
		#可省略參數:
4645
		#$conf["haveFileName"],字串,"true"代表有$conf["path"]檔案名稱,"false"代表$conf["path"]為純路徑,預設為"false".
4646
		$conf["fileAccess::validatePath"]["haveFileName"]="true";
4647
		#$conf["dirPermission"],字串,新建資料夾的權限設定,預設爲0770,亦即擁有者,同群組者可以讀,寫,存取,其他人無法使用.
4648
		#$conf["dirPermission"]="";
4649
		#$conf["web"],是否為檔案系統,"true"為網頁路徑,"false"為網頁系統,預設為"false".
4650
		$conf["fileAccess::validatePath"]["web"]="false";
4651
		#參考資料:
4652
		#無.
4653
		#備註:
4654
		#無.
4655
		$validatePath=fileAccess::validatePath($conf["fileAccess::validatePath"]);
4656
		unset($conf["fileAccess::validatePath"]);
226 liveuser 4657
 
3 liveuser 4658
		#如果執行失敗
4659
		if($validatePath["status"]==="false"){
226 liveuser 4660
 
3 liveuser 4661
			#設置錯誤識別
4662
			$result["status"]="false";
226 liveuser 4663
 
3 liveuser 4664
			#設置錯誤訊息
4665
			$result["error"]=$validatePath;
226 liveuser 4666
 
3 liveuser 4667
			#回傳結果
4668
			return $result;
226 liveuser 4669
 
3 liveuser 4670
			}#if end
226 liveuser 4671
 
3 liveuser 4672
		#變更 working dir
4673
		$chdir=chdir($validatePath["path"]);
226 liveuser 4674
 
3 liveuser 4675
		#嘗試30秒內連線到 unix domain socket
4676
		$fp=stream_socket_client("unix://".$validatePath["fileName"], $errno, $errstr, 30, STREAM_CLIENT_ASYNC_CONNECT );
226 liveuser 4677
 
3 liveuser 4678
		#如果開啟 unix domain socket 失敗
4679
		if(!$fp){
226 liveuser 4680
 
3 liveuser 4681
			#設置執行錯誤識別
4682
			$result["status"]="false";
226 liveuser 4683
 
3 liveuser 4684
			#設置錯誤訊息
4685
			$result["error"]=$errstr." (".$errno.")";
226 liveuser 4686
 
3 liveuser 4687
			#回傳結果
4688
			return $result;
226 liveuser 4689
 
3 liveuser 4690
			}#if end
226 liveuser 4691
 
3 liveuser 4692
		#傳遞訊息
4693
		$fwrite=fwrite($fp,$conf["msg"]);
226 liveuser 4694
 
3 liveuser 4695
		#如果傳遞失敗
4696
		if($fwrite===false){
226 liveuser 4697
 
3 liveuser 4698
			#關閉unix domain socket
4699
			@fclose($fp);
226 liveuser 4700
 
3 liveuser 4701
			#設置執行失敗
4702
			$result["status"]="false";
226 liveuser 4703
 
3 liveuser 4704
			#設置執行錯誤訊息
4705
			$result["error"][]="傳遞訊息(".$conf["msg"].")失敗";
226 liveuser 4706
 
3 liveuser 4707
			#回傳結果
4708
			return $result;
226 liveuser 4709
 
3 liveuser 4710
			}#if end
226 liveuser 4711
 
3 liveuser 4712
		#設置執行正常
4713
		$result["status"]="true";
226 liveuser 4714
 
3 liveuser 4715
		#回傳結果
4716
		return $result;
226 liveuser 4717
 
3 liveuser 4718
		}#function sendRawMsgUnixSocket
226 liveuser 4719
 
3 liveuser 4720
	/*
4721
	#連線到 sock::unixDomainSocketServer 產生的  unix domain socket,傳送指定的訊息.
4722
	#回傳結果:
4723
	#$result["status"],"true"代表執行正常;"false"代表執行不正常.
4724
	#$result["error"],錯誤訊息陣列.
4725
	#$result["function"],當前執行的函式名稱.
4726
	#$result["argu"],使用的參數.
4727
	#$result["content"],得到的回應.
4728
	#必填參數:
4729
	#$conf["msg"],陣列,要傳送的訊息,其主$conf["msg"]["id"],是無法指定的.
4730
	$conf["msg"]=array();
4731
	#可省略參數:
4732
	#$conf["sock"],字串,要連線的 usr/bin/qbpwcf-sock.php 所產生的 unix domain socket 路徑與名稱,預設為 qbpwcf_usock_path.
4733
	#$conf["sock"]=qbpwcf_usock_path;
4734
	#參考資料:
4735
	#無.
4736
	#備註:
4737
	#無.
4738
	*/
4739
	public static function sendAnyMsgToQBPWCFunixSocket(&$conf){
226 liveuser 4740
 
3 liveuser 4741
		#初始化要回傳的結果
4742
		$result=array();
4743
 
4744
		#取得當前執行的函數名稱
4745
		$result["function"]=__FUNCTION__;
226 liveuser 4746
 
3 liveuser 4747
		#如果沒有參數
4748
		if(func_num_args()==0){
226 liveuser 4749
 
3 liveuser 4750
			#設置執行失敗
4751
			$result["status"]="false";
226 liveuser 4752
 
3 liveuser 4753
			#設置執行錯誤訊息
4754
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 4755
 
3 liveuser 4756
			#回傳結果
4757
			return $result;
226 liveuser 4758
 
3 liveuser 4759
			}#if end
226 liveuser 4760
 
3 liveuser 4761
		#取得參數
4762
		$result["argu"]=$conf;
4763
 
4764
		#如果 $conf 不為陣列
4765
		if(gettype($conf)!="array"){
226 liveuser 4766
 
3 liveuser 4767
			#設置執行失敗
4768
			$result["status"]="false";
226 liveuser 4769
 
3 liveuser 4770
			#設置執行錯誤訊息
4771
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 4772
 
3 liveuser 4773
			#如果傳入的參數為 null
4774
			if($conf==null){
226 liveuser 4775
 
3 liveuser 4776
				#設置執行錯誤訊息
4777
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 4778
 
3 liveuser 4779
				}#if end
4780
 
4781
			#回傳結果
4782
			return $result;
226 liveuser 4783
 
3 liveuser 4784
			}#if end
226 liveuser 4785
 
3 liveuser 4786
		#函式說明:
4787
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
4788
		#回傳結果:
4789
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
4790
		#$result["error"],執行不正常結束的錯訊息陣列.
4791
		#$result["simpleError"],簡單表示的錯誤訊息.
4792
		#$result["function"],當前執行的函式名稱.
4793
		#$result["argu"],設置給予的參數.
4794
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
4795
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
4796
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
4797
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
4798
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
4799
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
4800
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
4801
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
4802
		#必填參數:
4803
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
4804
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
4805
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
4806
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
4807
		#可省略參數:
4808
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
4809
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("msg");
4810
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
4811
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("array");
4812
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
4813
		#$conf["canBeEmptyString"]="false";
4814
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
4815
		#$conf["canNotBeEmpty"]=array();
4816
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
4817
		#$conf["canBeEmpty"]=array();
4818
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
4819
		$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("sock");
4820
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
4821
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("sock");
4822
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
4823
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string");
4824
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
4825
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(qbpwcf_usock_path);
4826
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
4827
		#$conf["disallowAllSkipableVarIsEmpty"]="";
4828
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
4829
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
4830
		#$conf["disallowAllSkipableVarNotExist"],字串,是否不允許每個可省略參數都不存在,預設為"false"代表允許,反之為"true".
4831
		#$conf["disallowAllSkipableVarNotExist"]="";
4832
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
4833
		#$conf["arrayCountEqualCheck"][]=array();
4834
		#參考資料:
4835
		#array_keys=>http://php.net/manual/en/function.array-keys.php
4836
		#備註:
4837
		#無.
4838
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
4839
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 4840
 
3 liveuser 4841
		#如果執行失敗
4842
		if($checkArguments["status"]==="false"){
226 liveuser 4843
 
3 liveuser 4844
			#設置執行錯誤識別
4845
			$result["status"]="false";
226 liveuser 4846
 
3 liveuser 4847
			#設置錯誤訊息
4848
			$result["error"]=$checkArguments["error"];
226 liveuser 4849
 
3 liveuser 4850
			#回傳結果
4851
			return $result;
226 liveuser 4852
 
3 liveuser 4853
			}#if end
226 liveuser 4854
 
3 liveuser 4855
		#如果檢查不通過
4856
		if($checkArguments["status"]==="false"){
226 liveuser 4857
 
3 liveuser 4858
			#設置執行錯誤識別
4859
			$result["status"]="false";
226 liveuser 4860
 
3 liveuser 4861
			#設置錯誤訊息
4862
			$result["error"]=$checkArguments["error"];
226 liveuser 4863
 
3 liveuser 4864
			#回傳結果
4865
			return $result;
226 liveuser 4866
 
3 liveuser 4867
			}#if end
226 liveuser 4868
 
3 liveuser 4869
		#函式說明:
4870
		#連線到 unixDomainSockServer 提供的 unix domain socket.
4871
		#回傳結果:
4872
		#$result["status"],"true"代表執行正常;"false"代表執行不正常.
4873
		#$result["error"],錯誤訊息陣列.
4874
		#$result["function"],當前執行的函式名稱.
4875
		#$result["content"],取得的回應,讀到EOL,才會結束.
4876
		#必填參數:
4877
		#$conf["sock"],字串,要連線的unix domain socket.
4878
		$conf["sock::unixDomainSockClient"]["sock"]=$conf["sock"];
4879
		#可省略參數:
4880
		#$conf["id"],字串,取得的id,若無此值,則會得到新的數值.
4881
		#$conf["id"]="";
4882
		#$conf["cmd"],字串,要執行的指令,當$conf["id"]參數合法時,才會執行.
4883
		#$conf["cmd"]="";
4884
		#$conf["param"],參數陣列.
4885
		#$conf["param"]=array();
4886
		#$conf["escaped"],字串,param參數是否已經escaped了,預設為"false",反之為"true".
4887
		#$conf["escaped"]="true";
4888
		#$conf["custom"],陣列,要客制化傳輸的內容,會覆蓋以上除了$conf["id"]以外的可省略參數.
4889
		$conf["sock::unixDomainSockClient"]["custom"]=$conf["msg"];
4890
		#參考資料:
4891
		#http://php.net/manual/en/function.stream-socket-client.php
4892
		#http://php.net/manual/en/function.stream-get-contents.php
4893
		#備註:
4894
		#無.
4895
		$paramsOfUnixDomainSockClient=$conf["sock::unixDomainSockClient"];
4896
		$unixDomainSockClient=sock::unixDomainSockClient($conf["sock::unixDomainSockClient"]);
4897
		unset($conf["sock::unixDomainSockClient"]);
226 liveuser 4898
 
3 liveuser 4899
		#如果執行失敗
4900
		if($unixDomainSockClient["status"]==="false"){
226 liveuser 4901
 
3 liveuser 4902
			#設置執行錯誤識別
4903
			$result["status"]="false";
226 liveuser 4904
 
3 liveuser 4905
			#設置錯誤訊息
4906
			$result["error"]=$unixDomainSockClient;
226 liveuser 4907
 
3 liveuser 4908
			#回傳結果
4909
			return $result;
226 liveuser 4910
 
3 liveuser 4911
			}#if end
226 liveuser 4912
 
3 liveuser 4913
		#取得json回應
4914
		$jsonRes=json_decode($unixDomainSockClient["content"]);
226 liveuser 4915
 
3 liveuser 4916
		#如果執行失敗
4917
		if($jsonRes===null){
226 liveuser 4918
 
3 liveuser 4919
			#設置執行錯誤識別
4920
			$result["status"]="false";
226 liveuser 4921
 
3 liveuser 4922
			#設置錯誤訊息
4923
			$result["error"]=$unixDomainSockClient;
226 liveuser 4924
 
3 liveuser 4925
			#回傳結果
4926
			return $result;
226 liveuser 4927
 
3 liveuser 4928
			}#if end
226 liveuser 4929
 
3 liveuser 4930
		#如果沒有產生新id
4931
		if(!isset($jsonRes->id)){
226 liveuser 4932
 
3 liveuser 4933
			#設置執行錯誤識別
4934
			$result["status"]="false";
226 liveuser 4935
 
3 liveuser 4936
			#設置錯誤訊息
4937
			$result["error"]=$unixDomainSockClient;
226 liveuser 4938
 
3 liveuser 4939
			#回傳結果
4940
			return $result;
226 liveuser 4941
 
3 liveuser 4942
			}#if end
226 liveuser 4943
 
3 liveuser 4944
		#用新的id再傳送一次要求給 qbpwcf_usock_path
226 liveuser 4945
		$paramsOfUnixDomainSockClient["id"]=$jsonRes->id;
3 liveuser 4946
		$unixDomainSockClient=sock::unixDomainSockClient($paramsOfUnixDomainSockClient);
4947
		unset($paramsOfUnixDomainSockClient);
226 liveuser 4948
 
3 liveuser 4949
		#如果執行失敗
4950
		if($unixDomainSockClient["status"]==="false"){
226 liveuser 4951
 
3 liveuser 4952
			#設置執行錯誤識別
4953
			$result["status"]="false";
226 liveuser 4954
 
3 liveuser 4955
			#設置錯誤訊息
4956
			$result["error"]=$unixDomainSockClient;
226 liveuser 4957
 
3 liveuser 4958
			#回傳結果
4959
			return $result;
226 liveuser 4960
 
3 liveuser 4961
			}#if end
226 liveuser 4962
 
3 liveuser 4963
		#取得回應
4964
		$result["content"]=$unixDomainSockClient["content"];
226 liveuser 4965
 
3 liveuser 4966
		#設置執行正常
4967
		$result["status"]="true";
226 liveuser 4968
 
3 liveuser 4969
		#回傳結果
4970
		return $result;
226 liveuser 4971
 
3 liveuser 4972
		}#function sendAnyMsgToQBPWCFunixSocket end
226 liveuser 4973
 
3 liveuser 4974
	/*
4975
	#函式說明:
4976
	#重複連線到 unixDomainSockServer 提供的 unix domain socket, 避免其 listen timeout, 並清除逾時的連線.
4977
	#回傳結果:
4978
	#$result["status"],"true"代表執行正常;"false"代表執行不正常.
4979
	#$result["error"],錯誤訊息陣列.
4980
	#$result["function"],當前執行的函式名稱.
4981
	#必填參數:
4982
	#$conf["sock"],字串,要連線的unix domain socket.
4983
	$conf["sock"]="";
4984
	#可省略參數:
4985
	#無.
4986
	#參考資料:
4987
	#無.
4988
	#備註:
4989
	#無.
4990
	*/
4991
	public static function keepAliveUnixDomainSockConnection(&$conf){
226 liveuser 4992
 
3 liveuser 4993
		#初始化要回傳的結果
4994
		$result=array();
4995
 
4996
		#取得當前執行的函數名稱
4997
		$result["function"]=__FUNCTION__;
226 liveuser 4998
 
3 liveuser 4999
		#如果沒有參數
5000
		if(func_num_args()==0){
226 liveuser 5001
 
3 liveuser 5002
			#設置執行失敗
5003
			$result["status"]="false";
226 liveuser 5004
 
3 liveuser 5005
			#設置執行錯誤訊息
5006
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 5007
 
3 liveuser 5008
			#回傳結果
5009
			return $result;
226 liveuser 5010
 
3 liveuser 5011
			}#if end
5012
 
5013
		#涵式說明:
5014
		#判斷當前環境為web還是cmd
5015
		#回傳結果:
5016
		#$result,"web"或"cmd"
5017
		if(csInformation::getEnv()==="web"){
226 liveuser 5018
 
3 liveuser 5019
			#設置執行失敗
5020
			$result["status"]="false";
226 liveuser 5021
 
3 liveuser 5022
			#設置執行錯誤訊息
5023
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
226 liveuser 5024
 
3 liveuser 5025
			#回傳結果
5026
			return $result;
226 liveuser 5027
 
3 liveuser 5028
			}#if end
226 liveuser 5029
 
3 liveuser 5030
		#取得參數
5031
		$result["argu"]=$conf;
5032
 
5033
		#如果 $conf 不為陣列
5034
		if(gettype($conf)!="array"){
226 liveuser 5035
 
3 liveuser 5036
			#設置執行失敗
5037
			$result["status"]="false";
226 liveuser 5038
 
3 liveuser 5039
			#設置執行錯誤訊息
5040
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 5041
 
3 liveuser 5042
			#如果傳入的參數為 null
5043
			if($conf==null){
226 liveuser 5044
 
3 liveuser 5045
				#設置執行錯誤訊息
5046
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 5047
 
3 liveuser 5048
				}#if end
5049
 
5050
			#回傳結果
5051
			return $result;
226 liveuser 5052
 
3 liveuser 5053
			}#if end
226 liveuser 5054
 
3 liveuser 5055
		#檢查參數
5056
		#函式說明:
5057
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
5058
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
5059
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
5060
		#$result["function"],當前執行的函式名稱.
5061
		#$result["argu"],設置給予的參數.
5062
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
5063
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
5064
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
5065
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
5066
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
5067
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
5068
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
5069
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
5070
		#必填寫的參數:
5071
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
226 liveuser 5072
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
3 liveuser 5073
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
5074
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
5075
		#可以省略的參數:
5076
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
5077
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("sock");
5078
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
5079
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string");
5080
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
5081
		#$conf["canBeEmptyString"]="false";
5082
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
5083
		#$conf["canNotBeEmpty"]=array();
5084
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
5085
		#$conf["canBeEmpty"]=array();
5086
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
5087
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("id","cmd","param","clear");
5088
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
5089
		#$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("id","cmd","param","clear");
226 liveuser 5090
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 5091
		#$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string","array","string");
5092
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
5093
		#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array();
5094
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
5095
		#$conf["disallowAllSkipableVarIsEmpty"]="";
5096
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
5097
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
5098
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
5099
		#$conf["arrayCountEqualCheck"][]=array();
5100
		#參考資料來源:
5101
		#array_keys=>http://php.net/manual/en/function.array-keys.php
5102
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
5103
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 5104
 
3 liveuser 5105
		#如果檢查參數失敗
5106
		if($checkArguments["status"]==="false"){
226 liveuser 5107
 
3 liveuser 5108
			#設置執行失敗
5109
			$result["status"]="false";
226 liveuser 5110
 
3 liveuser 5111
			#設置錯誤訊息
5112
			$result["error"]=$checkArguments;
226 liveuser 5113
 
3 liveuser 5114
			#回傳結果
5115
			return $result;
226 liveuser 5116
 
3 liveuser 5117
			}#if end
226 liveuser 5118
 
3 liveuser 5119
		#如果檢查參數不通過
5120
		if($checkArguments["passed"]==="false"){
226 liveuser 5121
 
3 liveuser 5122
			#設置執行失敗
5123
			$result["status"]="false";
226 liveuser 5124
 
3 liveuser 5125
			#設置錯誤訊息
5126
			$result["error"]=$checkArguments;
226 liveuser 5127
 
3 liveuser 5128
			#回傳結果
5129
			return $result;
226 liveuser 5130
 
3 liveuser 5131
			}#if end
226 liveuser 5132
 
3 liveuser 5133
		#函式說明:
5134
		#連線到 unixDomainSockServer 提供的 unix domain socket.
5135
		#回傳結果:
5136
		#$result["status"],"true"代表執行正常;"false"代表執行不正常.
5137
		#$result["error"],錯誤訊息陣列.
5138
		#$result["function"],當前執行的函式名稱.
5139
		#$result["content"],取得的回應.
5140
		#必填參數:
5141
		#$conf["sock"],字串,要連線的unix domain socket.
5142
		$conf["sock::unixDomainSockClient"]["sock"]=$conf["sock"];
5143
		#可省略參數:
5144
		#$conf["id"],字串,取得的id,若無此值,則會得到新的數值.
5145
		#$conf["id"]="";
5146
		#$conf["cmd"],字串,要執行的指令.
5147
		#$conf["cmd"]="";
5148
		#$conf["param"],參數陣列.
5149
		#$conf["param"]=array();
5150
		#參考資料:
5151
		#http://php.net/manual/en/function.stream-socket-client.php
5152
		#http://php.net/manual/en/function.stream-get-contents.php
5153
		$unixDomainSockClient=sock::unixDomainSockClient($conf["sock::unixDomainSockClient"]);
5154
		unset($conf["sock::unixDomainSockClient"]);
226 liveuser 5155
 
3 liveuser 5156
		#建立連線失敗
5157
		if($unixDomainSockClient["status"]==="false"){
226 liveuser 5158
 
3 liveuser 5159
			#設置執行失敗
5160
			$result["status"]="false";
226 liveuser 5161
 
3 liveuser 5162
			#設置執行錯誤訊息
5163
			$result["error"]=$unixDomainSockClient;
226 liveuser 5164
 
3 liveuser 5165
			#回傳結果
5166
			return $result;
226 liveuser 5167
 
3 liveuser 5168
			}#if end
226 liveuser 5169
 
5170
		#取得回應
3 liveuser 5171
		$res=$unixDomainSockClient["content"];
226 liveuser 5172
 
3 liveuser 5173
		#解析回應
5174
		$res=json_decode(trim($res));
226 liveuser 5175
 
3 liveuser 5176
		#如果解析失敗
5177
		if($res===false){
226 liveuser 5178
 
3 liveuser 5179
			#設置執行失敗
5180
			$result["status"]="false";
226 liveuser 5181
 
3 liveuser 5182
			#設置執行錯誤訊息
5183
			$result["error"][]="解析回應失敗";
226 liveuser 5184
 
3 liveuser 5185
			#回傳結果
5186
			return $result;
226 liveuser 5187
 
3 liveuser 5188
			}#if end
226 liveuser 5189
 
5190
		#取得id
3 liveuser 5191
		$id=$res->id;
226 liveuser 5192
 
3 liveuser 5193
		#無窮迴圈
5194
		while(true){
226 liveuser 5195
 
3 liveuser 5196
			#函式說明:
5197
			#連線到 unixDomainSockServer 提供的 unix domain socket.
5198
			#回傳結果:
5199
			#$result["status"],"true"代表執行正常;"false"代表執行不正常.
5200
			#$result["error"],錯誤訊息陣列.
5201
			#$result["function"],當前執行的函式名稱.
5202
			#$result["content"],取得的回應.
5203
			#必填參數:
5204
			#$conf["sock"],字串,要連線的unix domain socket.
5205
			$conf["sock::unixDomainSockClient"]["sock"]=$conf["sock"];
5206
			#可省略參數:
5207
			#$conf["id"],字串,取得的id,若無此值,則會得到新的數值.
5208
			$conf["sock::unixDomainSockClient"]["id"]=$id;
5209
			#$conf["cmd"],字串,要執行的指令.
5210
			#$conf["cmd"]="";
5211
			#$conf["param"],參數陣列.
5212
			#$conf["param"]=array();
5213
			#參考資料:
5214
			#http://php.net/manual/en/function.stream-socket-client.php
5215
			#http://php.net/manual/en/function.stream-get-contents.php
5216
			$unixDomainSockClient=sock::unixDomainSockClient($conf["sock::unixDomainSockClient"]);
5217
			unset($conf["sock::unixDomainSockClient"]);
226 liveuser 5218
 
3 liveuser 5219
			#建立連線失敗
5220
			if($unixDomainSockClient["status"]==="false"){
226 liveuser 5221
 
3 liveuser 5222
				#設置執行失敗
5223
				$result["status"]="false";
226 liveuser 5224
 
3 liveuser 5225
				#設置執行錯誤訊息
5226
				$result["error"]=$unixDomainSockClient;
226 liveuser 5227
 
3 liveuser 5228
				#回傳結果
5229
				return $result;
226 liveuser 5230
 
3 liveuser 5231
				}#if end
226 liveuser 5232
 
5233
			#取得回應
3 liveuser 5234
			$res=$unixDomainSockClient["content"];
226 liveuser 5235
 
3 liveuser 5236
			#解析回應
5237
			$res=json_decode($res);
226 liveuser 5238
 
3 liveuser 5239
			#如果解析失敗
5240
			if($res===false){
226 liveuser 5241
 
3 liveuser 5242
				#設置執行失敗
5243
				$result["status"]="false";
226 liveuser 5244
 
3 liveuser 5245
				#設置執行錯誤訊息
5246
				$result["error"][]="解析回應失敗";
226 liveuser 5247
 
3 liveuser 5248
				#回傳結果
5249
				return $result;
226 liveuser 5250
 
3 liveuser 5251
				}#if end
226 liveuser 5252
 
3 liveuser 5253
			#show res
5254
			var_dump($res);
226 liveuser 5255
 
3 liveuser 5256
			#停止執行
5257
			sleep(60);
226 liveuser 5258
 
3 liveuser 5259
			}#while end
226 liveuser 5260
 
3 liveuser 5261
		#設置執行不正常
5262
		$result["status"]="false";
226 liveuser 5263
 
3 liveuser 5264
		#設置錯誤訊息
5265
		$result["error"][]="程式不應該中斷";
226 liveuser 5266
 
3 liveuser 5267
		#回傳結果
5268
		return $reuslt;
226 liveuser 5269
 
3 liveuser 5270
		}#function keepAliveUnixDomainSockConnection end
5271
 
5272
	/*
5273
	#函式說明:
5274
	#tcp client
5275
	#回傳結果:
5276
	#$result["status"],"true"代表執行正常;"false"代表執行不正常.
5277
	#$result["error"],錯誤訊息陣列.
5278
	#$result["function"],當前執行的函式名稱.
5279
	#$result["argu"],使用的參數.
5280
	#$result["content"],取得的回應內容.
5281
	#必填參數:
5282
	#$conf["port"],整數,tcp server listen port.
5283
	$conf["port"]=;
5284
	#$conf["dataToSend"],字串,要傳送的資訊.
5285
	$conf["dataToSend"]="";
5286
	#可省略參數:
5287
	#$conf["ip"],整數字串,tcp serer listen ip, default is 127.0.0.1.
5288
	#$conf["ip"]="127.0.0.1";
5289
	#$conf["sec"],整數,等待回應的秒數,預設為5秒.
5290
	#$conf["sec"]=5;
5291
	#$conf["microSec"],整數,等待回應的毫秒數,預設為0毫秒
5292
	#$conf["microSec"]=0;
5293
	#參考資料:
5294
	#https://www.php.net/manual/en/sockets.examples.php
5295
	#https://www.php.net/manual/en/function.socket-get-option.php
5296
	#備註:
5297
	#無.
5298
	*/
5299
	public static function tcpClient(&$conf){
226 liveuser 5300
 
3 liveuser 5301
		#初始化要回傳的結果
5302
		$result=array();
5303
 
5304
		#取得當前執行的函數名稱
5305
		$result["function"]=__FUNCTION__;
226 liveuser 5306
 
3 liveuser 5307
		#如果沒有參數
5308
		if(func_num_args()==0){
226 liveuser 5309
 
3 liveuser 5310
			#設置執行失敗
5311
			$result["status"]="false";
226 liveuser 5312
 
3 liveuser 5313
			#設置執行錯誤訊息
5314
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 5315
 
3 liveuser 5316
			#回傳結果
5317
			return $result;
226 liveuser 5318
 
3 liveuser 5319
			}#if end
5320
 
5321
		#涵式說明:
5322
		#判斷當前環境為web還是cmd
5323
		#回傳結果:
5324
		#$result,"web"或"cmd"
5325
		if(csInformation::getEnv()==="web"){
226 liveuser 5326
 
3 liveuser 5327
			#設置執行失敗
5328
			$result["status"]="false";
226 liveuser 5329
 
3 liveuser 5330
			#設置執行錯誤訊息
5331
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
226 liveuser 5332
 
3 liveuser 5333
			#回傳結果
5334
			return $result;
226 liveuser 5335
 
3 liveuser 5336
			}#if end
226 liveuser 5337
 
3 liveuser 5338
		#取得參數
5339
		$result["argu"]=$conf;
5340
 
5341
		#如果 $conf 不為陣列
5342
		if(gettype($conf)!="array"){
226 liveuser 5343
 
3 liveuser 5344
			#設置執行失敗
5345
			$result["status"]="false";
226 liveuser 5346
 
3 liveuser 5347
			#設置執行錯誤訊息
5348
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 5349
 
3 liveuser 5350
			#如果傳入的參數為 null
5351
			if($conf==null){
226 liveuser 5352
 
3 liveuser 5353
				#設置執行錯誤訊息
5354
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 5355
 
3 liveuser 5356
				}#if end
5357
 
5358
			#回傳結果
5359
			return $result;
226 liveuser 5360
 
3 liveuser 5361
			}#if end
226 liveuser 5362
 
3 liveuser 5363
		#初始化取得的回應
226 liveuser 5364
		$result["content"]="";
5365
 
3 liveuser 5366
		#檢查參數
5367
		#函式說明:
5368
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
5369
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
5370
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
5371
		#$result["function"],當前執行的函式名稱.
5372
		#$result["argu"],設置給予的參數.
5373
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
5374
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
5375
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
5376
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
5377
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
5378
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
5379
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
5380
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
5381
		#必填寫的參數:
5382
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
226 liveuser 5383
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
3 liveuser 5384
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
5385
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
5386
		#可以省略的參數:
5387
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
5388
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("port","dataToSend");
5389
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
5390
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("integer","string");
5391
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
5392
		$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
5393
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
5394
		#$conf["canNotBeEmpty"]=array();
5395
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
5396
		#$conf["canBeEmpty"]=array();
5397
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
5398
		$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("ip");
5399
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
5400
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("ip","sec","microSec");
226 liveuser 5401
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 5402
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","integer","integer");
5403
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
5404
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("127.0.0.1",5,0);
5405
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
5406
		#$conf["disallowAllSkipableVarIsEmpty"]="";
5407
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
5408
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
5409
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
5410
		#$conf["arrayCountEqualCheck"][]=array();
5411
		#參考資料來源:
5412
		#array_keys=>http://php.net/manual/en/function.array-keys.php
5413
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
5414
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 5415
 
3 liveuser 5416
		#如果檢查參數失敗
5417
		if($checkArguments["status"]==="false"){
226 liveuser 5418
 
3 liveuser 5419
			#設置執行失敗
5420
			$result["status"]="false";
226 liveuser 5421
 
3 liveuser 5422
			#設置錯誤訊息
5423
			$result["error"]=$checkArguments;
226 liveuser 5424
 
3 liveuser 5425
			#回傳結果
5426
			return $result;
226 liveuser 5427
 
3 liveuser 5428
			}#if end
226 liveuser 5429
 
3 liveuser 5430
		#如果檢查參數不通過
5431
		if($checkArguments["passed"]==="false"){
226 liveuser 5432
 
3 liveuser 5433
			#設置執行失敗
5434
			$result["status"]="false";
226 liveuser 5435
 
3 liveuser 5436
			#設置錯誤訊息
5437
			$result["error"]=$checkArguments;
226 liveuser 5438
 
3 liveuser 5439
			#回傳結果
5440
			return $result;
226 liveuser 5441
 
3 liveuser 5442
			}#if end
226 liveuser 5443
 
5444
		#建立 tcp socket client
3 liveuser 5445
		$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
226 liveuser 5446
 
3 liveuser 5447
		#設置接收資料的最長等待時間
5448
		socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>$conf["sec"],"usec"=>$conf["microSec"]));
226 liveuser 5449
 
3 liveuser 5450
		#如果建立 socket 失敗
5451
		if ($socket === false) {
226 liveuser 5452
 
3 liveuser 5453
			#設置執行失敗
5454
			$result["status"]="false";
226 liveuser 5455
 
3 liveuser 5456
			#設置錯誤訊息
5457
			$result["error"][]="socket_create() failed: reason: " . socket_strerror(socket_last_error());
226 liveuser 5458
 
3 liveuser 5459
			#回傳結果
5460
			return $result;
226 liveuser 5461
 
5462
			}#if end
5463
 
5464
		#連線到 tcp socket server
3 liveuser 5465
		$socket_connect = socket_connect($socket, $conf["ip"], $conf["port"]);
226 liveuser 5466
 
3 liveuser 5467
		#如果連線到 tcp socket server 失敗
5468
		if ($socket_connect === false) {
226 liveuser 5469
 
3 liveuser 5470
			#設置執行失敗
5471
			$result["status"]="false";
226 liveuser 5472
 
3 liveuser 5473
	    		#設置錯誤訊息
5474
			$result["error"][]="socket_connect() failed. Reason: ($result) " . socket_strerror(socket_last_error($socket));
226 liveuser 5475
 
3 liveuser 5476
			#回傳結果
5477
			return $result;
226 liveuser 5478
 
3 liveuser 5479
			}#if end
226 liveuser 5480
 
3 liveuser 5481
		#傳送訊息到 tcp server
5482
		socket_write($socket, $conf["dataToSend"].PHP_EOL, strlen($conf["dataToSend"].PHP_EOL));
226 liveuser 5483
 
3 liveuser 5484
		#取得回應
5485
		while ($out = socket_read($socket, 2048)) {
226 liveuser 5486
 
3 liveuser 5487
			#設置取得的回應
5488
			$result["content"]=$result["content"].$out;
226 liveuser 5489
 
3 liveuser 5490
			#debug
5491
			#echo $result["content"];
226 liveuser 5492
 
3 liveuser 5493
			}#while end
5494
 
5495
		#關閉socket
5496
		socket_close($socket);
226 liveuser 5497
 
3 liveuser 5498
		#設置處理正常
5499
		$result["status"]="true";
226 liveuser 5500
 
3 liveuser 5501
		#回傳結果
226 liveuser 5502
		return $result;
5503
 
3 liveuser 5504
		}#function tcpClient end
5505
 
5506
	/*
5507
	#函式說明:
5508
	#udp client
5509
	#回傳結果:
5510
	#$result["status"],"true"代表執行正常;"false"代表執行不正常.
5511
	#$result["error"],錯誤訊息陣列.
5512
	#$result["function"],當前執行的函式名稱.
5513
	#$result["argu"],使用的參數.
5514
	#$result["content"],取得的回應內容字串,為每個回應的內容直接串接.
5515
	#$result["contentArray"],取得的回應字串陣列,為每個回應的內容,各別儲存成陣列中的元素.
5516
	#$result["sock"],可重複使用的socket資源.
5517
	#必填參數:
5518
	#$conf["port"],整數,target port.
5519
	$conf["port"]=;
5520
	#$conf["dataToSend"],陣列字串,要傳送的多個訊息,每個訊息傳送結束後才會讀取回應.
5521
	$conf["dataToSend"]=array("");
5522
	#可省略參數:
5523
	#$conf["ip"],整數字串,tcp serer listen ip, default is 127.0.0.1.
5524
	#$conf["ip"]="127.0.0.1";
5525
	#$conf["timeout"],整數,預設為10秒,代表接收訊息的等待時間上限.
5526
	#$conf["timeout"]=10;
5527
	#$conf["secSleepAfterSend"],整數,預設為1秒.
5528
	#$conf["secSleepAfterSend"]=1;
5529
	#$conf["closeAtEnd"],字串,預設為"true",代表函式結束後要斷開連線.
5530
	#$conf["closeAtEnd"]="true";
5531
	#$conf["sock"],resource,要重複使用的socket資源,預設不指定.
5532
	#$conf["sock"]=;
5533
	#參考資料:
5534
	#https://www.php.net/manual/en/sockets.examples.php
5535
	#備註:
5536
	#無.
5537
	*/
5538
	public static function udpClient(&$conf){
226 liveuser 5539
 
3 liveuser 5540
		#初始化要回傳的結果
5541
		$result=array();
5542
 
5543
		#取得當前執行的函數名稱
5544
		$result["function"]=__FUNCTION__;
226 liveuser 5545
 
3 liveuser 5546
		#如果沒有參數
5547
		if(func_num_args()==0){
226 liveuser 5548
 
3 liveuser 5549
			#設置執行失敗
5550
			$result["status"]="false";
226 liveuser 5551
 
3 liveuser 5552
			#設置執行錯誤訊息
5553
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 5554
 
3 liveuser 5555
			#回傳結果
5556
			return $result;
226 liveuser 5557
 
3 liveuser 5558
			}#if end
5559
 
5560
		#涵式說明:
5561
		#判斷當前環境為web還是cmd
5562
		#回傳結果:
5563
		#$result,"web"或"cmd"
5564
		if(csInformation::getEnv()==="web"){
226 liveuser 5565
 
3 liveuser 5566
			#設置執行失敗
5567
			$result["status"]="false";
226 liveuser 5568
 
3 liveuser 5569
			#設置執行錯誤訊息
5570
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
226 liveuser 5571
 
3 liveuser 5572
			#回傳結果
5573
			return $result;
226 liveuser 5574
 
3 liveuser 5575
			}#if end
226 liveuser 5576
 
3 liveuser 5577
		#取得參數
5578
		$result["argu"]=$conf;
5579
 
5580
		#如果 $conf 不為陣列
5581
		if(gettype($conf)!="array"){
226 liveuser 5582
 
3 liveuser 5583
			#設置執行失敗
5584
			$result["status"]="false";
226 liveuser 5585
 
3 liveuser 5586
			#設置執行錯誤訊息
5587
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 5588
 
3 liveuser 5589
			#如果傳入的參數為 null
5590
			if($conf==null){
226 liveuser 5591
 
3 liveuser 5592
				#設置執行錯誤訊息
5593
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 5594
 
3 liveuser 5595
				}#if end
5596
 
5597
			#回傳結果
5598
			return $result;
226 liveuser 5599
 
3 liveuser 5600
			}#if end
226 liveuser 5601
 
3 liveuser 5602
		#初始化取得的回應
226 liveuser 5603
		$result["content"]="";
5604
 
3 liveuser 5605
		#檢查參數
5606
		#函式說明:
5607
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
5608
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
5609
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
5610
		#$result["function"],當前執行的函式名稱.
5611
		#$result["argu"],設置給予的參數.
5612
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
5613
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
5614
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
5615
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
5616
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
5617
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
5618
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
5619
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
5620
		#必填寫的參數:
5621
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
226 liveuser 5622
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
3 liveuser 5623
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
5624
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
5625
		#可以省略的參數:
5626
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
5627
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("port","dataToSend");
5628
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
5629
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("integer","array");
5630
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
5631
		$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
5632
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
5633
		#$conf["canNotBeEmpty"]=array();
5634
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
5635
		#$conf["canBeEmpty"]=array();
5636
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
5637
		$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("ip","secSleepAfterSend","closeAtEnd","timeout");
5638
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
5639
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("ip","secSleepAfterSend","closeAtEnd","sock","timeout");
226 liveuser 5640
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 5641
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","integer","string","object","integer");
5642
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
5643
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("127.0.0.1",1,"false",null,10);
5644
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
5645
		#$conf["disallowAllSkipableVarIsEmpty"]="";
5646
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
5647
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
5648
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
5649
		#$conf["arrayCountEqualCheck"][]=array();
5650
		#參考資料來源:
5651
		#array_keys=>http://php.net/manual/en/function.array-keys.php
5652
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
5653
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 5654
 
3 liveuser 5655
		#如果檢查參數失敗
5656
		if($checkArguments["status"]==="false"){
226 liveuser 5657
 
3 liveuser 5658
			#設置執行失敗
5659
			$result["status"]="false";
226 liveuser 5660
 
3 liveuser 5661
			#設置錯誤訊息
5662
			$result["error"]=$checkArguments;
226 liveuser 5663
 
3 liveuser 5664
			#回傳結果
5665
			return $result;
226 liveuser 5666
 
3 liveuser 5667
			}#if end
226 liveuser 5668
 
3 liveuser 5669
		#如果檢查參數不通過
5670
		if($checkArguments["passed"]==="false"){
226 liveuser 5671
 
3 liveuser 5672
			#設置執行失敗
5673
			$result["status"]="false";
226 liveuser 5674
 
3 liveuser 5675
			#設置錯誤訊息
5676
			$result["error"]=$checkArguments;
226 liveuser 5677
 
3 liveuser 5678
			#回傳結果
5679
			return $result;
226 liveuser 5680
 
3 liveuser 5681
			}#if end
226 liveuser 5682
 
5683
		#如果沒有指定要用既有的 socket
3 liveuser 5684
		if(!isset($conf["sock"])){
226 liveuser 5685
 
5686
			#建立 udp socket client
3 liveuser 5687
			$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
226 liveuser 5688
 
3 liveuser 5689
			#設置 timeout 為 $conf["timeout"] 秒
5690
			$timeout = array('sec'=>$conf["timeout"],'usec'=>0);
5691
 			socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,$timeout);
226 liveuser 5692
 
3 liveuser 5693
 			#如果建立 socket 失敗
5694
			if ($socket === false) {
226 liveuser 5695
 
3 liveuser 5696
				#設置執行失敗
5697
				$result["status"]="false";
226 liveuser 5698
 
3 liveuser 5699
				#設置錯誤訊息
5700
				$result["error"][]="socket_create() failed: reason: " . socket_strerror(socket_last_error());
226 liveuser 5701
 
3 liveuser 5702
				#回傳結果
5703
				return $result;
226 liveuser 5704
 
5705
				}#if end
5706
 
5707
			#連線到 udp socket server
3 liveuser 5708
			$socket_connect = socket_connect($socket, $conf["ip"], $conf["port"]);
226 liveuser 5709
 
3 liveuser 5710
			#如果連線到 udp socket server 失敗
5711
			if ($socket_connect === false) {
226 liveuser 5712
 
3 liveuser 5713
				#設置執行失敗
5714
				$result["status"]="false";
226 liveuser 5715
 
3 liveuser 5716
				#設置錯誤訊息
5717
				$result["error"][]="socket_connect() failed. Reason: ($result) " . socket_strerror(socket_last_error($socket));
226 liveuser 5718
 
3 liveuser 5719
				#回傳結果
5720
				return $result;
226 liveuser 5721
 
3 liveuser 5722
				}#if end
226 liveuser 5723
 
5724
			}#if end
5725
 
3 liveuser 5726
		#反之
5727
		else{
5728
			#取得要重複使用的socket
5729
			$socket=&$conf["sock"];
226 liveuser 5730
 
3 liveuser 5731
			}#else end
226 liveuser 5732
 
3 liveuser 5733
		#有幾個訊息就跑幾次
5734
		for($i=0;$i<count($conf["dataToSend"]);$i++){
226 liveuser 5735
 
3 liveuser 5736
			#傳送訊息到 tcp server
5737
			socket_write($socket, $conf["dataToSend"][$i], strlen($conf["dataToSend"][$i]));
226 liveuser 5738
 
3 liveuser 5739
			#取得回應
5740
			while ($out = socket_read($socket, 2048)) {
226 liveuser 5741
 
3 liveuser 5742
				#設置取得的回應,儲存到陣列裡面.
5743
				$result["contentArray"][]=$out;
226 liveuser 5744
 
3 liveuser 5745
				#設置取得的回應,儲存成字串,串接在一塊.
5746
				$result["content"]=$result["content"].$out;
226 liveuser 5747
 
3 liveuser 5748
				}#while end
226 liveuser 5749
 
3 liveuser 5750
			#如果有多個訊息要傳遞,且不是最後一個訊息
5751
			if(count($conf["dataToSend"])>1 && $i!==count($conf["dataToSend"])-1){
226 liveuser 5752
 
3 liveuser 5753
				#休息一下
5754
				sleep($conf["secSleepAfterSend"]);
226 liveuser 5755
 
3 liveuser 5756
				}#if end
226 liveuser 5757
 
3 liveuser 5758
			}#for end
226 liveuser 5759
 
3 liveuser 5760
		#如果要關閉連線
5761
		if($conf["closeAtEnd"]==="true"){
226 liveuser 5762
 
3 liveuser 5763
			#關閉socket
5764
			socket_close($socket);
226 liveuser 5765
 
3 liveuser 5766
			}#if end
226 liveuser 5767
 
3 liveuser 5768
		#取得使用的socket
226 liveuser 5769
		$result["sock"]=$socket;
5770
 
3 liveuser 5771
		#設置處理正常
5772
		$result["status"]="true";
226 liveuser 5773
 
3 liveuser 5774
		#回傳結果
226 liveuser 5775
		return $result;
5776
 
3 liveuser 5777
		}#function udpClient end
5778
 
5779
	/*
5780
	#函式說明:
5781
	#建立tcp server
5782
	#必填參數:
5783
	#$conf["port"],整數,tcp server listen port.
5784
	$conf["port"]=;
5785
	#可省略參數:
5786
	#$conf["ip"],整數字串,tcp server listen ip, default is 0.0.0.0.
5787
	#$conf["ip"]="0.0.0.0";
5788
	#$conf["processFuncs"],字串陣列,要將收到的訊息作什麼事情的函式們,預設為空陣列,裡面若是自己建立的涵式,可這樣呼叫"\qbpwcf\your function name".
5789
	#$conf["processFuncs"]=array();
5790
	#$conf["serverFuncs"],字串陣列,於idle時,server要執行的函式們,預設為空陣列.
5791
	#$conf["serverFuncs"]=array();
5792
	#$conf["welcomeMsg"],字串,是否要顯示歡迎訊息,預設為"true"代表要顯示歡迎訊息;"false"代表不要顯示歡迎訊息.
5793
	#$conf["welcomeMsg"]="true";
5794
	#$conf["socketSelectTimeountSec"],整數,代表要等候幾秒才抓取有異動的連線,預設為0代表立即抓取有異動的連線,但是會吃很多cpu.
5795
	#$conf["socketSelectTimeountSec"]=0;
5796
	#$conf["socketSelectTimeountUsec"],整數,代表要等候幾微秒才抓取有異動的連線,預設不使用.
5797
	#$conf["socketSelectTimeountUsec"]=5000;
5798
	#參考資料:
5799
	#無.
5800
	#備註:
5801
	#無.
5802
	*/
5803
	public static function tcpServer(&$conf){
5804
 
5805
		#初始化要回傳的結果
5806
		$result=array();
5807
 
5808
		#取得當前執行的函數名稱
5809
		$result["function"]=__FUNCTION__;
226 liveuser 5810
 
3 liveuser 5811
		#如果沒有參數
5812
		if(func_num_args()==0){
226 liveuser 5813
 
3 liveuser 5814
			#設置執行失敗
5815
			$result["status"]="false";
226 liveuser 5816
 
3 liveuser 5817
			#設置執行錯誤訊息
5818
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 5819
 
3 liveuser 5820
			#回傳結果
5821
			return $result;
226 liveuser 5822
 
3 liveuser 5823
			}#if end
5824
 
5825
		#涵式說明:
5826
		#判斷當前環境為web還是cmd
5827
		#回傳結果:
5828
		#$result,"web"或"cmd"
5829
		if(csInformation::getEnv()==="web"){
226 liveuser 5830
 
3 liveuser 5831
			#設置執行失敗
5832
			$result["status"]="false";
226 liveuser 5833
 
3 liveuser 5834
			#設置執行錯誤訊息
5835
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
226 liveuser 5836
 
3 liveuser 5837
			#回傳結果
5838
			return $result;
226 liveuser 5839
 
3 liveuser 5840
			}#if end
226 liveuser 5841
 
3 liveuser 5842
		#取得參數
5843
		$result["argu"]=$conf;
5844
 
5845
		#如果 $conf 不為陣列
5846
		if(gettype($conf)!="array"){
226 liveuser 5847
 
3 liveuser 5848
			#設置執行失敗
5849
			$result["status"]="false";
226 liveuser 5850
 
3 liveuser 5851
			#設置執行錯誤訊息
5852
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 5853
 
3 liveuser 5854
			#如果傳入的參數為 null
5855
			if($conf==null){
226 liveuser 5856
 
3 liveuser 5857
				#設置執行錯誤訊息
5858
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 5859
 
3 liveuser 5860
				}#if end
5861
 
5862
			#回傳結果
5863
			return $result;
226 liveuser 5864
 
3 liveuser 5865
			}#if end
226 liveuser 5866
 
3 liveuser 5867
		#檢查參數
5868
		#函式說明:
5869
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
5870
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
5871
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
5872
		#$result["function"],當前執行的函式名稱.
5873
		#$result["argu"],設置給予的參數.
5874
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
5875
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
5876
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
5877
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
5878
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
5879
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
5880
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
5881
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
5882
		#必填寫的參數:
5883
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
226 liveuser 5884
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
3 liveuser 5885
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
5886
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
5887
		#可以省略的參數:
5888
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
5889
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("port");
5890
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
5891
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("integer");
5892
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
5893
		#$conf["canBeEmptyString"]="false";
5894
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
5895
		#$conf["canNotBeEmpty"]=array();
5896
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
5897
		#$conf["canBeEmpty"]=array();
5898
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
5899
		$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("ip","welcomeMsg");
5900
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
5901
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("ip","processFuncs","welcomeMsg","socketSelectTimeountSec","socketSelectTimeountUsec","serverFuncs");
226 liveuser 5902
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 5903
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","array","string","integer","integer","array");
5904
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
5905
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("0.0.0.0",array(),"true",0,null,null);
5906
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
5907
		#$conf["disallowAllSkipableVarIsEmpty"]="";
5908
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
5909
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
5910
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
5911
		#$conf["arrayCountEqualCheck"][]=array();
5912
		#參考資料來源:
5913
		#array_keys=>http://php.net/manual/en/function.array-keys.php
5914
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
5915
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 5916
 
3 liveuser 5917
		#如果檢查參數失敗
5918
		if($checkArguments["status"]==="false"){
226 liveuser 5919
 
3 liveuser 5920
			#設置執行失敗
5921
			$result["status"]="false";
226 liveuser 5922
 
3 liveuser 5923
			#設置錯誤訊息
5924
			$result["error"]=$checkArguments;
226 liveuser 5925
 
3 liveuser 5926
			#回傳結果
5927
			return $result;
226 liveuser 5928
 
3 liveuser 5929
			}#if end
226 liveuser 5930
 
3 liveuser 5931
		#如果檢查參數不通過
5932
		if($checkArguments["passed"]==="false"){
226 liveuser 5933
 
3 liveuser 5934
			#設置執行失敗
5935
			$result["status"]="false";
226 liveuser 5936
 
3 liveuser 5937
			#設置錯誤訊息
5938
			$result["error"]=$checkArguments;
226 liveuser 5939
 
3 liveuser 5940
			#回傳結果
5941
			return $result;
226 liveuser 5942
 
3 liveuser 5943
			}#if end
226 liveuser 5944
 
3 liveuser 5945
		#程式不會 timeout
5946
		set_time_limit(0);
5947
 
5948
		#Turn on implicit output flushing so we see what we're getting as it comes in.
5949
		ob_implicit_flush();
5950
 
5951
		#set listen ip and port
5952
		$address = $conf["ip"];
5953
 
5954
		#set listen port
5955
		$port = $conf["port"];
5956
 
5957
		//如果建立 socket 失敗
5958
		if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
5959
 
306 liveuser 5960
			echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . PHP_EOL;
3 liveuser 5961
 
5962
			}#if end
5963
 
5964
		//如果 bind socket 失敗
5965
		if (socket_bind($sock, $address, $port) === false) {
5966
 
306 liveuser 5967
			echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . PHP_EOL;
3 liveuser 5968
 
5969
			}#if end
5970
 
5971
		//如果 socket listen 超過 5 秒仍未成功
5972
		if (socket_listen($sock, 5) === false) {
5973
 
306 liveuser 5974
			echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . PHP_EOL;
3 liveuser 5975
 
5976
			}#if end
5977
 
5978
		// create a list of all the clients that will be connected to us..
306 liveuser 5979
		// add the listening socket to this list
5980
		$clients = array("serverSock"=>$sock);
3 liveuser 5981
 
5982
		#無窮迴圈
5983
		do {
5984
			#each resource
5985
			foreach($clients as $rk=>$csock){
226 liveuser 5986
 
3 liveuser 5987
				#if not socket resource
5988
				if(get_resource_type($csock)!=="Socket"){
5989
 
5990
					#delete
5991
					unset($clients[$rk]);
5992
 
5993
					}#if end
226 liveuser 5994
 
3 liveuser 5995
				}#foreach end
5996
 
5997
			#found server sock
5998
			$serverSock=false;
5999
 
6000
			#each resource
6001
			foreach($clients as $rk=>$csock){
226 liveuser 6002
 
3 liveuser 6003
				#if not socket resource
6004
				if($rk==="serverSock"){
6005
 
6006
					#found server sock
6007
					$serverSock=true;
6008
 
6009
					}#if end
226 liveuser 6010
 
3 liveuser 6011
				}#if end
226 liveuser 6012
 
3 liveuser 6013
			#not found server sock
6014
			if(!$serverSock){
226 liveuser 6015
 
3 liveuser 6016
				#end
6017
				exit;
6018
 
6019
				}#if end
226 liveuser 6020
 
3 liveuser 6021
			#for socket select
6022
			$read   = $clients;
6023
			$write  = NULL;
6024
			$except = NULL;
6025
 
6026
			#如果沒設定微妙
226 liveuser 6027
			if(!isset($conf["socketSelectTimeountUsec"])){
3 liveuser 6028
 
226 liveuser 6029
				#get interesting socket
3 liveuser 6030
				$num_changed_sockets = socket_select($read, $write, $except, $conf["socketSelectTimeountSec"]);
6031
 
6032
				}#if end
6033
 
6034
			#反之有設定微妙
6035
			else{
6036
 
6037
				#get interesting socket
6038
				$num_changed_sockets = socket_select($read, $write, $except, $conf["socketSelectTimeountSec"], $conf["socketSelectTimeountUsec"]);
6039
 
6040
				}#else end
6041
 
6042
			#socket_select failed
6043
			if ($num_changed_sockets === false) {
226 liveuser 6044
 
3 liveuser 6045
				#NOTICE to server
6046
				echo "socket_select failed".PHP_EOL;
6047
 
226 liveuser 6048
				}
6049
 
3 liveuser 6050
			#At least at one of the sockets something interesting happened
6051
			else if ($num_changed_sockets > 0) {
226 liveuser 6052
 
6053
				/* debug
3 liveuser 6054
				echo "At least at one of the sockets something interesting happened".PHP_EOL;
6055
				echo "\$read:".PHP_EOL;var_dump($read);
6056
				echo "\$write:".PHP_EOL;var_dump($write);
6057
				echo "\$except:".PHP_EOL;var_dump($except);
6058
				*/
6059
 
6060
				// check if there is a client trying to connect
6061
				if (in_array($sock, $read)) {
226 liveuser 6062
 
3 liveuser 6063
					#accept the client, and add him to the $clients array
6064
				   	$clients[] = $newsock = socket_accept($sock);
226 liveuser 6065
 
3 liveuser 6066
				   	#如果要顯示歡迎訊息
6067
					if($conf["welcomeMsg"]==="true"){
226 liveuser 6068
 
3 liveuser 6069
				   		#send the client a welcome message
6070
				   		socket_write($newsock, "Welcome to QBPWCF tcpServer".PHP_EOL.
6071
				   		"There are ".(count($clients) - 1)." client(s) connected to the server".PHP_EOL);
226 liveuser 6072
 
3 liveuser 6073
				   		}#if end
226 liveuser 6074
 
3 liveuser 6075
					#嘗試取得遠端的ip與port
6076
					#參考資料:https://www.php.net/manual/en/function.socket-getpeername.php
6077
				 	@socket_getpeername($newsock, $ip);
6078
 
6079
					#如果有抓到 $ip
6080
					if(isset($ip)){
226 liveuser 6081
 
3 liveuser 6082
						#提示有新的使用者
6083
						echo "New client connected: ".$ip.PHP_EOL;
226 liveuser 6084
 
3 liveuser 6085
						}#if end
6086
 
6087
					// remove the listening socket from the clients-with-data array
6088
					$key = array_search($sock, $read);
6089
					unset($read[$key]);
226 liveuser 6090
 
3 liveuser 6091
					}
6092
 
6093
				#message received
6094
				else{
226 liveuser 6095
 
3 liveuser 6096
					// loop through all the clients that have data to read from
6097
					foreach ($read as $read_sock) {
226 liveuser 6098
 
3 liveuser 6099
						// read until newline or 1024 bytes
6100
						// socket_read while show errors when the client is disconnected, so silence the error messages
6101
						$data = @socket_read($read_sock, 1024, PHP_NORMAL_READ);
226 liveuser 6102
 
3 liveuser 6103
						// check if the client is disconnected
226 liveuser 6104
						if ($data === false)
3 liveuser 6105
						{
6106
							// remove client for $clients array
6107
							$key = array_search($read_sock, $clients);
6108
							unset($clients[$key]);
6109
							echo "client disconnected.".PHP_EOL;
226 liveuser 6110
 
3 liveuser 6111
							// continue to the next client to read from, if any
6112
							continue;
6113
						}
226 liveuser 6114
 
3 liveuser 6115
						// trim off the trailing/beginning white spaces
6116
						$data = trim($data);
226 liveuser 6117
 
3 liveuser 6118
						// check if there is any data after trimming off the spaces
6119
						if(!empty($data)){
226 liveuser 6120
 
3 liveuser 6121
							// send this to all the clients in the $clients array (except the first one, which is a listening socket)
6122
							foreach ($clients as $send_sock) {
226 liveuser 6123
 
3 liveuser 6124
								// if its the listening sock or the client that we got the message from, go to the next one in the list
226 liveuser 6125
 
6126
								#如果是Server Socket
3 liveuser 6127
								if ($send_sock === $sock){
6128
 
6129
									#server debug
226 liveuser 6130
									echo "reveiced:".$data.PHP_EOL;
3 liveuser 6131
 
6132
									#針對每個用來處理輸入字串的函式
6133
									foreach($conf["processFuncs"] as $proFunc){
6134
 
6135
										#呼叫函式,參數為輸入的字串data跟client socket, server socket, 每個 socket
6136
										#https://www.php.net/manual/en/function.call-user-func.php
6137
										$param=array("data"=>$data,"serverSock"=>$sock,"clientSock"=>$read_sock,"allSockArray"=>$clients);
6138
										$params=array();
226 liveuser 6139
										$params[]=&$param;
6140
										$result=call_user_func_array($proFunc,$params);
6141
 
3 liveuser 6142
										#如果有回傳結果
6143
										if($result!==NULL){
6144
 
6145
											#如果執行出錯
6146
											if($result["status"]==="false"){
6147
 
6148
												#印出結果
6149
												var_dump($result);
6150
 
6151
												}
6152
 
6153
											}
6154
 
6155
										}
6156
									}
6157
 
6158
								/*
6159
								#如果是Sender Socket
6160
								else if ($send_sock === $read_sock){
226 liveuser 6161
 
3 liveuser 6162
									continue;
6163
 
6164
									}
226 liveuser 6165
								*/
6166
 
3 liveuser 6167
								/*
6168
								#msg to others/broadcast
6169
								else{
6170
 
6171
						    			// write the message to the client -- add a newline character to the end of the message
6172
						    			socket_write($send_sock, $data.PHP_EOL);
226 liveuser 6173
 
3 liveuser 6174
									}//end of broadcast
226 liveuser 6175
								*/
3 liveuser 6176
 
6177
								} //foreach end
226 liveuser 6178
 
3 liveuser 6179
							}#if end
226 liveuser 6180
 
3 liveuser 6181
						} // end of reading foreach
226 liveuser 6182
 
3 liveuser 6183
					}#msg received end
6184
 
6185
				}#something happen end
6186
 
6187
			#反之沒有用戶插手
6188
			else{
6189
				#提示idle
6190
				#echo "idle:".time().PHP_EOL;
226 liveuser 6191
 
3 liveuser 6192
				#如果沒有設置 $conf["serverFuncs"]
6193
				if(!isset($conf["serverFuncs"])){
226 liveuser 6194
 
3 liveuser 6195
					#設置為空陣列
6196
					$conf["serverFuncs"]=array();
226 liveuser 6197
 
3 liveuser 6198
					}#if end
226 liveuser 6199
 
3 liveuser 6200
				#針對每個用來處理輸入字串的函式
6201
				foreach($conf["serverFuncs"] as $proFunc){
6202
 
6203
					#呼叫函式,參數為輸入的字串data跟client socket, server socket, 每個 socket
6204
					#https://www.php.net/manual/en/function.call-user-func.php
226 liveuser 6205
					$result=call_user_func_array($proFunc,array());
3 liveuser 6206
 
6207
					#如果有回傳結果
6208
					if($result!==NULL){
6209
 
6210
						#如果執行出錯
6211
						if($result["status"]==="false"){
6212
 
6213
							#印出結果
6214
							var_dump($result);
6215
 
6216
							}//if end
6217
 
6218
						}//if end
6219
 
6220
					}//foreach end
226 liveuser 6221
 
3 liveuser 6222
				}#else end
6223
 
6224
			} while (true);
6225
 
6226
		#關閉listen的socket
6227
		socket_close($sock);
6228
 
6229
		}
6230
		#function tcpServer end
6231
 
6232
	/*
6233
	#函式說明:
6234
	#建立 udp server
6235
	#必填參數:
6236
	#$conf["port"],整數,udp server listen port.
6237
	$conf["port"]=;
6238
	#可省略參數:
6239
	#$conf["ip"],整數字串,udp serer listen ip, default is 0.0.0.0.
6240
	#$conf["ip"]="0.0.0.0";
6241
	#$conf["processFuncs"],字串陣列,要將收到的訊息作什麼事情的函式們,預設為空陣列.
6242
	#$conf["processFuncs"]=array();
6243
	#$conf["welcomeMsg"],字串,是否要顯示歡迎訊息,預設為"true"代表要顯示歡迎訊息;"false"代表不要顯示歡迎訊息.
6244
	#$conf["welcomeMsg"]="true";
6245
	#$conf["socketSelectTimeountSec"],整數,代表要等候幾秒才抓取有異動的連線,預設為0代表立即抓取有異動的連線,但是會吃很多cpu.
6246
	#$conf["socketSelectTimeountSec"]=0;
6247
	#$conf["socketSelectTimeountUsec"],整數,代表要等候幾微秒才抓取有異動的連線,預設不使用.
6248
	#$conf["socketSelectTimeountUsec"]=5000;
6249
	#參考資料:
6250
	#https://www.jianshu.com/p/097463d08664
6251
	#備註:
6252
	#無.
6253
	*/
6254
	public static function udpServer(&$conf){
6255
 
6256
		#初始化要回傳的結果
6257
		$result=array();
6258
 
6259
		#取得當前執行的函數名稱
6260
		$result["function"]=__FUNCTION__;
226 liveuser 6261
 
3 liveuser 6262
		#如果沒有參數
6263
		if(func_num_args()==0){
226 liveuser 6264
 
3 liveuser 6265
			#設置執行失敗
6266
			$result["status"]="false";
226 liveuser 6267
 
3 liveuser 6268
			#設置執行錯誤訊息
6269
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 6270
 
3 liveuser 6271
			#回傳結果
6272
			return $result;
226 liveuser 6273
 
3 liveuser 6274
			}#if end
6275
 
6276
		#涵式說明:
6277
		#判斷當前環境為web還是cmd
6278
		#回傳結果:
6279
		#$result,"web"或"cmd"
6280
		if(csInformation::getEnv()==="web"){
226 liveuser 6281
 
3 liveuser 6282
			#設置執行失敗
6283
			$result["status"]="false";
226 liveuser 6284
 
3 liveuser 6285
			#設置執行錯誤訊息
6286
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
226 liveuser 6287
 
3 liveuser 6288
			#回傳結果
6289
			return $result;
226 liveuser 6290
 
3 liveuser 6291
			}#if end
226 liveuser 6292
 
3 liveuser 6293
		#取得參數
6294
		$result["argu"]=$conf;
6295
 
6296
		#如果 $conf 不為陣列
6297
		if(gettype($conf)!="array"){
226 liveuser 6298
 
3 liveuser 6299
			#設置執行失敗
6300
			$result["status"]="false";
226 liveuser 6301
 
3 liveuser 6302
			#設置執行錯誤訊息
6303
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 6304
 
3 liveuser 6305
			#如果傳入的參數為 null
6306
			if($conf==null){
226 liveuser 6307
 
3 liveuser 6308
				#設置執行錯誤訊息
6309
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 6310
 
3 liveuser 6311
				}#if end
6312
 
6313
			#回傳結果
6314
			return $result;
226 liveuser 6315
 
3 liveuser 6316
			}#if end
226 liveuser 6317
 
3 liveuser 6318
		#檢查參數
6319
		#函式說明:
6320
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
6321
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
6322
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
6323
		#$result["function"],當前執行的函式名稱.
6324
		#$result["argu"],設置給予的參數.
6325
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
6326
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
6327
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
6328
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
6329
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
6330
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
6331
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
6332
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
6333
		#必填寫的參數:
6334
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
226 liveuser 6335
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
3 liveuser 6336
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
6337
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
6338
		#可以省略的參數:
6339
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
6340
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("port");
6341
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
6342
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("integer");
6343
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
6344
		#$conf["canBeEmptyString"]="false";
6345
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
6346
		#$conf["canNotBeEmpty"]=array();
6347
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
6348
		#$conf["canBeEmpty"]=array();
6349
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
6350
		$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("ip","welcomeMsg","socketSelectTimeountUsec");
6351
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
6352
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("ip","processFuncs","welcomeMsg","socketSelectTimeountSec","socketSelectTimeountUsec");
226 liveuser 6353
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 6354
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","array","string","integer","integer");
6355
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
6356
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("0.0.0.0",array(),"true",0,null);
6357
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
6358
		#$conf["disallowAllSkipableVarIsEmpty"]="";
6359
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
6360
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
6361
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
6362
		#$conf["arrayCountEqualCheck"][]=array();
6363
		#參考資料來源:
6364
		#array_keys=>http://php.net/manual/en/function.array-keys.php
6365
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
6366
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 6367
 
3 liveuser 6368
		#如果檢查參數失敗
6369
		if($checkArguments["status"]==="false"){
226 liveuser 6370
 
3 liveuser 6371
			#設置執行失敗
6372
			$result["status"]="false";
226 liveuser 6373
 
3 liveuser 6374
			#設置錯誤訊息
6375
			$result["error"]=$checkArguments;
226 liveuser 6376
 
3 liveuser 6377
			#回傳結果
6378
			return $result;
226 liveuser 6379
 
3 liveuser 6380
			}#if end
226 liveuser 6381
 
3 liveuser 6382
		#如果檢查參數不通過
6383
		if($checkArguments["passed"]==="false"){
226 liveuser 6384
 
3 liveuser 6385
			#設置執行失敗
6386
			$result["status"]="false";
226 liveuser 6387
 
3 liveuser 6388
			#設置錯誤訊息
6389
			$result["error"]=$checkArguments;
226 liveuser 6390
 
3 liveuser 6391
			#回傳結果
6392
			return $result;
226 liveuser 6393
 
3 liveuser 6394
			}#if end
226 liveuser 6395
 
3 liveuser 6396
		#Allow the script to hang around waiting for connections.
6397
		set_time_limit(0);
6398
 
6399
		#Turn on implicit output flushing so we see what we're getting as it comes in.
6400
		ob_implicit_flush();
6401
 
6402
		#set listen ip and port
6403
		$address = $conf["ip"];
6404
 
6405
		#set listen port
6406
		$port = $conf["port"];
6407
 
6408
		//如果建立 socket 失敗
6409
		if (($sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) === false) {
6410
 
6411
		    	echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . PHP_EOL;
6412
 
6413
			}#if end
6414
 
6415
		//如果 bind socket 失敗
6416
		if (socket_bind($sock, $address, $port) === false) {
6417
 
6418
		    	echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . PHP_EOL;
6419
 
6420
			}#if end
6421
 
6422
		// create a list of all the clients that will be connected to us..
6423
    		// add the listening socket to this list
6424
    		$clients = array("serverSock"=>$sock);
6425
 
6426
		#無窮迴圈
6427
		do {
6428
			#each resource
6429
			foreach($clients as $rk=>$csock){
6430
 
226 liveuser 6431
				#if not socket resource
3 liveuser 6432
				if(gettype($csock)==="boolean"){
6433
 
6434
					#delete
6435
					unset($clients[$rk]);
6436
 
6437
					}
6438
 
6439
				#if not socket resource
6440
				else if(get_resource_type($csock)!=="Socket"){
6441
 
6442
					#delete
6443
					unset($clients[$rk]);
6444
 
226 liveuser 6445
					}
3 liveuser 6446
				}
6447
 
6448
			#found server sock
6449
			$serverSock=false;
6450
 
6451
			#each resource
6452
			foreach($clients as $rk=>$csock){
226 liveuser 6453
 
3 liveuser 6454
				#if not socket resource
6455
				if($rk==="serverSock"){
6456
 
6457
					#found server sock
6458
					$serverSock=true;
6459
 
226 liveuser 6460
					}
3 liveuser 6461
				}
226 liveuser 6462
 
3 liveuser 6463
			#not found server sock
6464
			if(!$serverSock){
226 liveuser 6465
 
3 liveuser 6466
				#end
6467
				exit;
6468
 
6469
				}
226 liveuser 6470
 
3 liveuser 6471
			#for socket select
6472
			$read   = $clients;
6473
			$write  = NULL;
6474
			$except = NULL;
6475
 
6476
			#如果未設置 $conf["socketSelectTimeountUsec"]
6477
			if(!isset($conf["socketSelectTimeountUsec"])){
226 liveuser 6478
 
3 liveuser 6479
				#get interesting socket
6480
				$num_changed_sockets = socket_select($read, $write, $except, $conf["socketSelectTimeountSec"]);
226 liveuser 6481
 
3 liveuser 6482
				}#if end
6483
 
6484
			#反之有設置 $conf["socketSelectTimeountUsec"]
6485
			else{
6486
 
6487
				#get interesting socket
6488
				$num_changed_sockets = socket_select($read, $write, $except, $conf["socketSelectTimeountSec"], $conf["socketSelectTimeountUsec"]);
6489
 
6490
				}#else end
6491
 
6492
			#socket_select failed
6493
			if ($num_changed_sockets === false) {
226 liveuser 6494
 
3 liveuser 6495
				#NOTICE to server
6496
				echo "socket_select failed".PHP_EOL;
6497
 
6498
				}#if end
226 liveuser 6499
 
3 liveuser 6500
			#At least at one of the sockets something interesting happened
6501
			else if ($num_changed_sockets > 0) {
226 liveuser 6502
 
3 liveuser 6503
				// check if there is a client trying to connect
6504
				if (in_array($sock, $read)) {
226 liveuser 6505
 
3 liveuser 6506
					#accept the client, and add him to the $clients array
226 liveuser 6507
				   	$receivedBytes=socket_recvfrom($sock,$data,1024,0,$remote_ip,$remote_port);
3 liveuser 6508
					$key=$remote_ip.":".$remote_port;
6509
					$clients[$key] = $newsock = $sock;
6510
 
6511
					#如果要顯示歡迎訊息
6512
					if($conf["welcomeMsg"]==="true"){
6513
 
6514
						# send the client a welcome message
6515
					   	socket_sendto($newsock, "Welcome to QBPWCF udpServer".PHP_EOL."There are ".(count($clients) - 1)." client(s) connected to the server".PHP_EOL, 1024, 0, $remote_ip, $remote_port);
6516
 
6517
						}#if end
6518
 
6519
					#debug
6520
					echo "client connected:".$key.PHP_EOL;
6521
					echo "client sended:".$data;
6522
					echo "all client:".PHP_EOL;
226 liveuser 6523
					var_dump($clients);
3 liveuser 6524
 
6525
					#針對每個用來處理輸入字串的函式
6526
					foreach($conf["processFuncs"] as $proFunc){
6527
 
6528
						#呼叫函式,參數為輸入的字串data跟client socket, server socket, 每個 socket
6529
						#https://www.php.net/manual/en/function.call-user-func.php
6530
						$param=array("data"=>trim($data),"serverSock"=>$sock,"clientKey"=>$key,"allSockArray"=>&$clients);
6531
						$params=array();
226 liveuser 6532
						$params[]=&$param;
6533
						$result=call_user_func_array($proFunc,$params);
3 liveuser 6534
 
6535
						#如果有回傳結果
6536
						if($result!==NULL){
6537
 
6538
							#如果執行出錯
6539
							if($result["status"]==="false"){
6540
 
6541
								#印出結果
6542
								var_dump($result);
6543
 
6544
								}
6545
 
6546
							}
6547
 
6548
						}
6549
 
6550
					#debug
6551
					echo "last all client:".PHP_EOL;
6552
					var_dump($clients);
226 liveuser 6553
 
3 liveuser 6554
					# remove the listening socket from the clients-with-data array
6555
					$key = array_search($sock, $read);
6556
					unset($read[$key]);
226 liveuser 6557
 
3 liveuser 6558
					}#if end
6559
 
6560
				}#something happen end
6561
 
6562
			} while (true);
6563
 
6564
		#關閉listen的socket
6565
		socket_close($sock);
6566
 
6567
		}
6568
		#function tcpServer end
6569
 
6570
	/*
6571
	#函式說明:
6572
	#針對收到的內容,原封不動地傳回給用戶.
6573
	#必填參數:
6574
	#$conf["data"],字串,要處理的內容.
6575
	$conf["data"]="";
6576
	#$conf["clientSock"],resource socket.
6577
	$conf["clientSock"]=;
6578
	#$conf["serverSock"],resource socket.
6579
	$conf["serverSock"]=;
6580
	#$conf["allSockArray"],resource陣列.
6581
	$conf["allSockArray"]=array();
6582
	#可省略參數:
6583
	#無.
6584
	#參考資料:
6585
	#無.
6586
	#備註:
6587
	#無.
6588
	*/
6589
	public function echoService(&$conf){
226 liveuser 6590
 
3 liveuser 6591
		#初始化要回傳的結果
6592
		$result=array();
6593
 
6594
		#取得當前執行的函數名稱
6595
		$result["function"]=__FUNCTION__;
226 liveuser 6596
 
3 liveuser 6597
		#如果沒有參數
6598
		if(func_num_args()==0){
226 liveuser 6599
 
3 liveuser 6600
			#設置執行失敗
6601
			$result["status"]="false";
226 liveuser 6602
 
3 liveuser 6603
			#設置執行錯誤訊息
6604
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 6605
 
3 liveuser 6606
			#回傳結果
6607
			return $result;
226 liveuser 6608
 
3 liveuser 6609
			}#if end
6610
 
6611
		#涵式說明:
6612
		#判斷當前環境為web還是cmd
6613
		#回傳結果:
6614
		#$result,"web"或"cmd"
6615
		if(csInformation::getEnv()==="web"){
226 liveuser 6616
 
3 liveuser 6617
			#設置執行失敗
6618
			$result["status"]="false";
226 liveuser 6619
 
3 liveuser 6620
			#設置執行錯誤訊息
6621
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
226 liveuser 6622
 
3 liveuser 6623
			#回傳結果
6624
			return $result;
226 liveuser 6625
 
3 liveuser 6626
			}#if end
226 liveuser 6627
 
3 liveuser 6628
		#取得參數
6629
		$result["argu"]=$conf;
6630
 
6631
		#如果 $conf 不為陣列
6632
		if(gettype($conf)!="array"){
226 liveuser 6633
 
3 liveuser 6634
			#設置執行失敗
6635
			$result["status"]="false";
226 liveuser 6636
 
3 liveuser 6637
			#設置執行錯誤訊息
6638
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 6639
 
3 liveuser 6640
			#如果傳入的參數為 null
6641
			if($conf==null){
226 liveuser 6642
 
3 liveuser 6643
				#設置執行錯誤訊息
6644
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 6645
 
3 liveuser 6646
				}#if end
6647
 
6648
			#回傳結果
6649
			return $result;
226 liveuser 6650
 
3 liveuser 6651
			}#if end
226 liveuser 6652
 
3 liveuser 6653
		#檢查參數
6654
		#函式說明:
6655
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
6656
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
6657
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
6658
		#$result["function"],當前執行的函式名稱.
6659
		#$result["argu"],設置給予的參數.
6660
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
6661
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
6662
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
6663
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
6664
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
6665
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
6666
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
6667
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
6668
		#必填寫的參數:
6669
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
226 liveuser 6670
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
3 liveuser 6671
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
6672
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
6673
		#可以省略的參數:
6674
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
6675
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("data","clientSock","serverSock","allSockArray");
6676
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
6677
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","resource","resource","array");
6678
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
6679
		#$conf["canBeEmptyString"]="false";
6680
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
6681
		#$conf["canNotBeEmpty"]=array();
6682
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
6683
		#$conf["canBeEmpty"]=array();
6684
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
6685
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("ip");
6686
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
6687
		#$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("ip","processFuncs");
226 liveuser 6688
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 6689
		#$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","array");
6690
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
6691
		#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("0.0.0.0",array());
6692
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
6693
		#$conf["disallowAllSkipableVarIsEmpty"]="";
6694
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
6695
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
6696
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
6697
		#$conf["arrayCountEqualCheck"][]=array();
6698
		#參考資料來源:
6699
		#array_keys=>http://php.net/manual/en/function.array-keys.php
6700
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
6701
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 6702
 
3 liveuser 6703
		#如果檢查參數失敗
6704
		if($checkArguments["status"]==="false"){
226 liveuser 6705
 
3 liveuser 6706
			#設置執行失敗
6707
			$result["status"]="false";
226 liveuser 6708
 
3 liveuser 6709
			#設置錯誤訊息
6710
			$result["error"]=$checkArguments;
226 liveuser 6711
 
3 liveuser 6712
			#回傳結果
6713
			return $result;
226 liveuser 6714
 
3 liveuser 6715
			}#if end
226 liveuser 6716
 
3 liveuser 6717
		#如果檢查參數不通過
6718
		if($checkArguments["passed"]==="false"){
226 liveuser 6719
 
3 liveuser 6720
			#設置執行失敗
6721
			$result["status"]="false";
226 liveuser 6722
 
3 liveuser 6723
			#設置錯誤訊息
6724
			$result["error"]=$checkArguments;
226 liveuser 6725
 
3 liveuser 6726
			#回傳結果
6727
			return $result;
226 liveuser 6728
 
3 liveuser 6729
			}#if end
6730
 
6731
		#將內容原封不動還給 client
6732
		@socket_write($conf["clientSock"], $conf["data"].PHP_EOL);
6733
 
6734
		}#function echoService end
6735
 
6736
	/*
6737
	#函式說明:
6738
	#針對收到的內容,原封不動地傳回給用戶.協定為UDP.
6739
	#回傳結果:
6740
	#無.
6741
	#必填參數:
6742
	#$conf["data"],字串,要處理的內容.
6743
	$conf["data"]="";
6744
	#$conf["clientKey"],resource socket key
6745
	$conf["clientKey"]=;
6746
	#$conf["serverSock"],resource socket.
6747
	$conf["serverSock"]=;
6748
	#$conf["allSockArray"],resource陣列.
6749
	$conf["allSockArray"]=array();
6750
	#可省略參數:
6751
	#無.
6752
	#參考資料:
6753
	#無.
6754
	#備註:
6755
	#無.
6756
	*/
6757
	public function echoServiceU(&$conf){
226 liveuser 6758
 
3 liveuser 6759
		#初始化要回傳的結果
6760
		$result=array();
6761
 
6762
		#取得當前執行的函數名稱
6763
		$result["function"]=__FUNCTION__;
226 liveuser 6764
 
3 liveuser 6765
		#如果沒有參數
6766
		if(func_num_args()==0){
226 liveuser 6767
 
3 liveuser 6768
			#設置執行失敗
6769
			$result["status"]="false";
226 liveuser 6770
 
3 liveuser 6771
			#設置執行錯誤訊息
6772
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 6773
 
3 liveuser 6774
			#回傳結果
6775
			return $result;
226 liveuser 6776
 
3 liveuser 6777
			}#if end
6778
 
6779
		#涵式說明:
6780
		#判斷當前環境為web還是cmd
6781
		#回傳結果:
6782
		#$result,"web"或"cmd"
6783
		if(csInformation::getEnv()==="web"){
226 liveuser 6784
 
3 liveuser 6785
			#設置執行失敗
6786
			$result["status"]="false";
226 liveuser 6787
 
3 liveuser 6788
			#設置執行錯誤訊息
6789
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
226 liveuser 6790
 
3 liveuser 6791
			#回傳結果
6792
			return $result;
226 liveuser 6793
 
3 liveuser 6794
			}#if end
226 liveuser 6795
 
3 liveuser 6796
		#取得參數
6797
		$result["argu"]=$conf;
6798
 
6799
		#如果 $conf 不為陣列
6800
		if(gettype($conf)!="array"){
226 liveuser 6801
 
3 liveuser 6802
			#設置執行失敗
6803
			$result["status"]="false";
226 liveuser 6804
 
3 liveuser 6805
			#設置執行錯誤訊息
6806
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 6807
 
3 liveuser 6808
			#如果傳入的參數為 null
6809
			if($conf==null){
226 liveuser 6810
 
3 liveuser 6811
				#設置執行錯誤訊息
6812
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 6813
 
3 liveuser 6814
				}#if end
6815
 
6816
			#回傳結果
6817
			return $result;
226 liveuser 6818
 
3 liveuser 6819
			}#if end
226 liveuser 6820
 
3 liveuser 6821
		#檢查參數
6822
		#函式說明:
6823
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
6824
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
6825
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
6826
		#$result["function"],當前執行的函式名稱.
6827
		#$result["argu"],設置給予的參數.
6828
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
6829
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
6830
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
6831
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
6832
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
6833
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
6834
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
6835
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
6836
		#必填寫的參數:
6837
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
226 liveuser 6838
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
3 liveuser 6839
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
6840
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
6841
		#可以省略的參數:
6842
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
6843
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("data","clientKey","serverSock","allSockArray");
6844
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
6845
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string","resource","array");
6846
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
6847
		#$conf["canBeEmptyString"]="false";
6848
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
6849
		#$conf["canNotBeEmpty"]=array();
6850
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
6851
		#$conf["canBeEmpty"]=array();
6852
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
6853
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("ip");
6854
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
6855
		#$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("ip","processFuncs");
226 liveuser 6856
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 6857
		#$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","array");
6858
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
6859
		#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("0.0.0.0",array());
6860
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
6861
		#$conf["disallowAllSkipableVarIsEmpty"]="";
6862
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
6863
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
6864
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
6865
		#$conf["arrayCountEqualCheck"][]=array();
6866
		#參考資料來源:
6867
		#array_keys=>http://php.net/manual/en/function.array-keys.php
6868
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
6869
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 6870
 
3 liveuser 6871
		#如果檢查參數失敗
6872
		if($checkArguments["status"]==="false"){
226 liveuser 6873
 
3 liveuser 6874
			#設置執行失敗
6875
			$result["status"]="false";
226 liveuser 6876
 
3 liveuser 6877
			#設置錯誤訊息
6878
			$result["error"]=$checkArguments;
226 liveuser 6879
 
3 liveuser 6880
			#回傳結果
6881
			return $result;
226 liveuser 6882
 
3 liveuser 6883
			}#if end
226 liveuser 6884
 
3 liveuser 6885
		#如果檢查參數不通過
6886
		if($checkArguments["passed"]==="false"){
226 liveuser 6887
 
3 liveuser 6888
			#設置執行失敗
6889
			$result["status"]="false";
226 liveuser 6890
 
3 liveuser 6891
			#設置錯誤訊息
6892
			$result["error"]=$checkArguments;
226 liveuser 6893
 
3 liveuser 6894
			#回傳結果
6895
			return $result;
226 liveuser 6896
 
3 liveuser 6897
			}#if end
6898
 
6899
		#將內容原封不動還給 client
6900
		$strArray=explode(":",$conf["clientKey"]);
6901
		$remote_ip=$strArray[0];
6902
		$remote_port=$strArray[1];
6903
		socket_sendto($conf["serverSock"], $conf["data"].PHP_EOL, 1024, 0, $remote_ip, $remote_port);
6904
 
6905
		}#function echoServiceU end
6906
 
6907
	/*
6908
	#函式說明:
6909
	#針對收到的內容,若是"quit"則斷開用戶的連線,若是"shutdown"則關閉Server Listen的socket.
6910
	#回傳結果:
6911
	#無.
6912
	#必填參數:
6913
	#$conf["data"],字串,要處理的內容.
6914
	$conf["data"]="";
6915
	#$conf["clientSock"],resource socket.
6916
	$conf["clientSock"]=;
6917
	#$conf["serverSock"],resource socket.
6918
	$conf["serverSock"]=;
6919
	#$conf["allSockArray"],resource陣列.
6920
	$conf["allSockArray"]=array();
6921
	#可省略參數:
6922
	#無.
6923
	#參考資料:
6924
	#無.
6925
	#備註:
6926
	#無.
6927
	*/
6928
	public function endService(&$conf){
226 liveuser 6929
 
3 liveuser 6930
		#初始化要回傳的結果
6931
		$result=array();
6932
 
6933
		#取得當前執行的函數名稱
6934
		$result["function"]=__FUNCTION__;
226 liveuser 6935
 
3 liveuser 6936
		#如果沒有參數
6937
		if(func_num_args()==0){
226 liveuser 6938
 
3 liveuser 6939
			#設置執行失敗
6940
			$result["status"]="false";
226 liveuser 6941
 
3 liveuser 6942
			#設置執行錯誤訊息
6943
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 6944
 
3 liveuser 6945
			#回傳結果
6946
			return $result;
226 liveuser 6947
 
3 liveuser 6948
			}#if end
6949
 
6950
		#涵式說明:
6951
		#判斷當前環境為web還是cmd
6952
		#回傳結果:
6953
		#$result,"web"或"cmd"
6954
		if(csInformation::getEnv()==="web"){
226 liveuser 6955
 
3 liveuser 6956
			#設置執行失敗
6957
			$result["status"]="false";
226 liveuser 6958
 
3 liveuser 6959
			#設置執行錯誤訊息
6960
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
226 liveuser 6961
 
3 liveuser 6962
			#回傳結果
6963
			return $result;
226 liveuser 6964
 
3 liveuser 6965
			}#if end
226 liveuser 6966
 
3 liveuser 6967
		#取得參數
6968
		$result["argu"]=$conf;
6969
 
6970
		#如果 $conf 不為陣列
6971
		if(gettype($conf)!="array"){
226 liveuser 6972
 
3 liveuser 6973
			#設置執行失敗
6974
			$result["status"]="false";
226 liveuser 6975
 
3 liveuser 6976
			#設置執行錯誤訊息
6977
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 6978
 
3 liveuser 6979
			#如果傳入的參數為 null
6980
			if($conf==null){
226 liveuser 6981
 
3 liveuser 6982
				#設置執行錯誤訊息
6983
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 6984
 
3 liveuser 6985
				}#if end
6986
 
6987
			#回傳結果
6988
			return $result;
226 liveuser 6989
 
3 liveuser 6990
			}#if end
226 liveuser 6991
 
3 liveuser 6992
		#檢查參數
6993
		#函式說明:
6994
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
6995
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
6996
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
6997
		#$result["function"],當前執行的函式名稱.
6998
		#$result["argu"],設置給予的參數.
6999
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
7000
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
7001
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
7002
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
7003
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
7004
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
7005
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
7006
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
7007
		#必填寫的參數:
7008
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
226 liveuser 7009
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
3 liveuser 7010
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
7011
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
7012
		#可以省略的參數:
7013
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
7014
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("data","clientSock","serverSock","allSockArray");
7015
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
7016
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","resource","resource","array");
7017
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
7018
		#$conf["canBeEmptyString"]="false";
7019
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
7020
		#$conf["canNotBeEmpty"]=array();
7021
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
7022
		#$conf["canBeEmpty"]=array();
7023
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
7024
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("ip");
7025
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
7026
		#$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("ip","processFuncs");
226 liveuser 7027
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 7028
		#$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","array");
7029
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
7030
		#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("0.0.0.0",array());
7031
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
7032
		#$conf["disallowAllSkipableVarIsEmpty"]="";
7033
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
7034
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
7035
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
7036
		#$conf["arrayCountEqualCheck"][]=array();
7037
		#參考資料來源:
7038
		#array_keys=>http://php.net/manual/en/function.array-keys.php
7039
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
7040
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 7041
 
3 liveuser 7042
		#如果檢查參數失敗
7043
		if($checkArguments["status"]==="false"){
226 liveuser 7044
 
3 liveuser 7045
			#設置執行失敗
7046
			$result["status"]="false";
226 liveuser 7047
 
3 liveuser 7048
			#設置錯誤訊息
7049
			$result["error"]=$checkArguments;
226 liveuser 7050
 
3 liveuser 7051
			#回傳結果
7052
			return $result;
226 liveuser 7053
 
3 liveuser 7054
			}#if end
226 liveuser 7055
 
3 liveuser 7056
		#如果檢查參數不通過
7057
		if($checkArguments["passed"]==="false"){
226 liveuser 7058
 
3 liveuser 7059
			#設置執行失敗
7060
			$result["status"]="false";
226 liveuser 7061
 
3 liveuser 7062
			#設置錯誤訊息
7063
			$result["error"]=$checkArguments;
226 liveuser 7064
 
3 liveuser 7065
			#回傳結果
7066
			return $result;
226 liveuser 7067
 
3 liveuser 7068
			}#if end
7069
 
7070
		#針對輸入的內容
7071
		switch($conf["data"]){
7072
 
7073
			#如果是 "quit"
7074
			case "quit":
7075
 
7076
				#關閉與用戶之間的連線
7077
				socket_close($conf["clientSock"]);
7078
 
7079
				break;
7080
 
7081
			#如果是 "shutdown"
7082
			case "shutdown":
7083
 
7084
				#關閉 Server Listen 的 sock
7085
				socket_close($conf["serverSock"]);
7086
 
7087
				break;
7088
 
7089
			default:
7090
 
7091
			}#switch end
7092
 
7093
		}#function endService end
7094
 
7095
	/*
7096
	#函式說明:
7097
	#針對收到的內容,若是"quit"則斷開用戶的連線,若是"shutdown"則關閉Server Listen的socket,協定為UDP.
7098
	#回傳結果:
7099
	#無.
7100
	#必填參數:
7101
	#$conf["data"],字串,要處理的內容.
7102
	$conf["data"]="";
7103
	#$conf["clientKey"],resource socket key.
7104
	$conf["clientKey"]=;
7105
	#$conf["serverSock"],resource socket.
7106
	$conf["serverSock"]=;
7107
	#$conf["allSockArray"],resource陣列.
7108
	$conf["allSockArray"]=array();
7109
	#可省略參數:
7110
	#無.
7111
	#參考資料:
7112
	#無.
7113
	#備註:
7114
	#無.
7115
	*/
7116
	public function endServiceU(&$conf){
226 liveuser 7117
 
3 liveuser 7118
		#初始化要回傳的結果
7119
		$result=array();
7120
 
7121
		#取得當前執行的函數名稱
7122
		$result["function"]=__FUNCTION__;
226 liveuser 7123
 
3 liveuser 7124
		#如果沒有參數
7125
		if(func_num_args()==0){
226 liveuser 7126
 
3 liveuser 7127
			#設置執行失敗
7128
			$result["status"]="false";
226 liveuser 7129
 
3 liveuser 7130
			#設置執行錯誤訊息
7131
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 7132
 
3 liveuser 7133
			#回傳結果
7134
			return $result;
226 liveuser 7135
 
3 liveuser 7136
			}#if end
7137
 
7138
		#涵式說明:
7139
		#判斷當前環境為web還是cmd
7140
		#回傳結果:
7141
		#$result,"web"或"cmd"
7142
		if(csInformation::getEnv()==="web"){
226 liveuser 7143
 
3 liveuser 7144
			#設置執行失敗
7145
			$result["status"]="false";
226 liveuser 7146
 
3 liveuser 7147
			#設置執行錯誤訊息
7148
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
226 liveuser 7149
 
3 liveuser 7150
			#回傳結果
7151
			return $result;
226 liveuser 7152
 
3 liveuser 7153
			}#if end
226 liveuser 7154
 
3 liveuser 7155
		#取得參數
7156
		$result["argu"]=$conf;
7157
 
7158
		#如果 $conf 不為陣列
7159
		if(gettype($conf)!="array"){
226 liveuser 7160
 
3 liveuser 7161
			#設置執行失敗
7162
			$result["status"]="false";
226 liveuser 7163
 
3 liveuser 7164
			#設置執行錯誤訊息
7165
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 7166
 
3 liveuser 7167
			#如果傳入的參數為 null
7168
			if($conf==null){
226 liveuser 7169
 
3 liveuser 7170
				#設置執行錯誤訊息
7171
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 7172
 
3 liveuser 7173
				}#if end
7174
 
7175
			#回傳結果
7176
			return $result;
226 liveuser 7177
 
3 liveuser 7178
			}#if end
226 liveuser 7179
 
3 liveuser 7180
		#檢查參數
7181
		#函式說明:
7182
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
7183
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7184
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
7185
		#$result["function"],當前執行的函式名稱.
7186
		#$result["argu"],設置給予的參數.
7187
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
7188
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
7189
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
7190
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
7191
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
7192
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
7193
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
7194
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
7195
		#必填寫的參數:
7196
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
226 liveuser 7197
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
3 liveuser 7198
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
7199
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
7200
		#可以省略的參數:
7201
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
7202
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("data","clientKey","serverSock","allSockArray");
7203
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
7204
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string","resource","array");
7205
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
7206
		#$conf["canBeEmptyString"]="false";
7207
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
7208
		#$conf["canNotBeEmpty"]=array();
7209
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
7210
		#$conf["canBeEmpty"]=array();
7211
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
7212
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("ip");
7213
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
7214
		#$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("ip","processFuncs");
226 liveuser 7215
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 7216
		#$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","array");
7217
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
7218
		#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("0.0.0.0",array());
7219
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
7220
		#$conf["disallowAllSkipableVarIsEmpty"]="";
7221
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
7222
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
7223
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
7224
		#$conf["arrayCountEqualCheck"][]=array();
7225
		#參考資料來源:
7226
		#array_keys=>http://php.net/manual/en/function.array-keys.php
7227
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
7228
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 7229
 
3 liveuser 7230
		#如果檢查參數失敗
7231
		if($checkArguments["status"]==="false"){
226 liveuser 7232
 
3 liveuser 7233
			#設置執行失敗
7234
			$result["status"]="false";
226 liveuser 7235
 
3 liveuser 7236
			#設置錯誤訊息
7237
			$result["error"]=$checkArguments;
226 liveuser 7238
 
3 liveuser 7239
			#回傳結果
7240
			return $result;
226 liveuser 7241
 
3 liveuser 7242
			}#if end
226 liveuser 7243
 
3 liveuser 7244
		#如果檢查參數不通過
7245
		if($checkArguments["passed"]==="false"){
226 liveuser 7246
 
3 liveuser 7247
			#設置執行失敗
7248
			$result["status"]="false";
226 liveuser 7249
 
3 liveuser 7250
			#設置錯誤訊息
7251
			$result["error"]=$checkArguments;
226 liveuser 7252
 
3 liveuser 7253
			#回傳結果
7254
			return $result;
226 liveuser 7255
 
3 liveuser 7256
			}#if end
7257
 
7258
		#針對輸入的內容
7259
		switch($conf["data"]){
7260
 
7261
			#如果是 "quit"
7262
			case "quit":
7263
 
7264
				#從 $conf["clientKey"] 取得用戶的IP與port
7265
				$strArray=explode(":",$conf["clientKey"]);
7266
				$remote_ip=$strArray[0];
7267
				$remote_port=$strArray[1];
7268
 
7269
				#卸除儲存與用戶之間連線的socket變數
7270
				unset($conf["allSockArray"][$conf["clientKey"]]);
7271
 
7272
				break;
7273
 
7274
			#如果是 "shutdown"
7275
			case "shutdown":
7276
 
7277
				#關閉 Server Listen 的 sock
7278
				socket_close($conf["serverSock"]);
7279
 
7280
				break;
7281
 
7282
			default:
7283
 
7284
			}#switch end
7285
 
7286
		}#function endServiceU end
226 liveuser 7287
 
3 liveuser 7288
	/*
7289
	#函式說明:
7290
	#給予unixDomainSockServer函式的addOnProcessFunc參數用的函式,在背景執行指令.
7291
	#回傳結果:
7292
	#$result["status"],執行是否正常,"true"為正常,"false"為不正常.
7293
	#$result["error"],錯誤訊息陣列.
7294
	#$result["function"],當前執行的函數名稱.
7295
	#$result["argu"],使用的參數.
7296
	#$result["continue"],"true"代表執行到此函式為止;"false"代表允許後面的函式繼續執行.
7297
	#$result["cache"],要暫存在server端的內容.
7298
	#必填參數:
7299
	$conf["request"],json字串,key為"cmd2run"者,代表要執行的指令;key為"params"者,為要傳入的參數,一個元素代表一個參數.
7300
	#$conf["request"]="";
7301
	$conf["sock"],resource,用戶socket.
7302
	#$conf["sock"]=;
7303
	#可省略參數:
7304
	#$conf["clientCache"],陣列,讓client使用的cache.
7305
	#$conf["clientCache"]=array();
7306
	#參考資料:
7307
	#無.
7308
	#備註:
7309
	#無.
7310
	*/
7311
	public static function addOnProcessFuncForUnixDomainSockServerInRunWithAutoAddMemoryDaemon(&$conf=array()){
226 liveuser 7312
 
3 liveuser 7313
		#初始化要回傳的結果
7314
		$result=array();
7315
 
7316
		#取得當前執行的函數名稱
7317
		$result["function"]=__FUNCTION__;
226 liveuser 7318
 
3 liveuser 7319
		#取得參數
7320
		$result["argu"]=$conf;
226 liveuser 7321
 
3 liveuser 7322
		#允許後面的函式繼續執行
7323
		$result["continue"]="true";
7324
 
7325
		#如果 $conf 不為陣列
7326
		if(gettype($conf)!="array"){
7327
 
7328
			#設置執行失敗
7329
			$result["status"]="false";
7330
 
7331
			#設置執行錯誤訊息
7332
			$result["error"][]="\$conf變數須為陣列形態";
7333
 
7334
			#如果傳入的參數為 null
7335
			if(is_null($conf)){
7336
 
7337
				#設置執行錯誤訊息
7338
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
7339
 
7340
				}#if end
7341
 
7342
			#回傳結果
7343
			return $result;
7344
 
7345
			}#if end
226 liveuser 7346
 
3 liveuser 7347
		#函式說明:
7348
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
7349
		#回傳結果:
7350
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7351
		#$result["error"],執行不正常結束的錯訊息陣列.
7352
		#$result["simpleError"],簡單表示的錯誤訊息.
7353
		#$result["function"],當前執行的函式名稱.
7354
		#$result["argu"],設置給予的參數.
7355
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
7356
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
7357
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
7358
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
7359
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
7360
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
7361
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
7362
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
7363
		#必填參數:
7364
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
7365
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
7366
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
7367
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
7368
		#可省略參數:
7369
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
7370
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("request","socket");
7371
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
7372
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","resource");
7373
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
7374
		#$conf["canBeEmptyString"]="false";
7375
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
7376
		#$conf["canNotBeEmpty"]=array();
7377
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
7378
		#$conf["canBeEmpty"]=array();
7379
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
7380
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("last_req_time","info");
7381
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
7382
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("last_req_time","info","clientCache");
7383
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
7384
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","array","array");
7385
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
7386
		#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array($phpMaxMemInConfig,"10",null);
7387
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
7388
		#$conf["disallowAllSkipableVarIsEmpty"]="";
7389
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
7390
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
7391
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
7392
		#$conf["arrayCountEqualCheck"][]=array();
7393
		#參考資料:
7394
		#array_keys=>http://php.net/manual/en/function.array-keys.php
7395
		#備註:
7396
		#無.
7397
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
7398
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 7399
 
3 liveuser 7400
		#debug
7401
		#var_dump(__LINE__,$checkArguments);
226 liveuser 7402
 
3 liveuser 7403
		#如果執行失敗
7404
		if($checkArguments["status"]==="false"){
226 liveuser 7405
 
3 liveuser 7406
			#設置執行失敗
7407
			$result["status"]="false";
7408
 
7409
			#設置執行錯誤訊息
7410
			$result["error"]=$checkArguments;
226 liveuser 7411
 
3 liveuser 7412
			#回傳結果
7413
			return $result;
226 liveuser 7414
 
3 liveuser 7415
			}#if end
226 liveuser 7416
 
3 liveuser 7417
		#如果檢查不通過
7418
		if($checkArguments["passed"]==="false"){
226 liveuser 7419
 
3 liveuser 7420
			#設置執行失敗
7421
			$result["status"]="false";
7422
 
7423
			#設置執行錯誤訊息
7424
			$result["error"]=$checkArguments;
226 liveuser 7425
 
3 liveuser 7426
			#回傳結果
7427
			return $result;
226 liveuser 7428
 
7429
			}#if end
7430
 
3 liveuser 7431
		#將需求變成陣列
7432
		$request=(array)json_decode($conf["request"]);
226 liveuser 7433
 
3 liveuser 7434
		#函式說明:
7435
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
7436
		#回傳結果:
7437
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7438
		#$result["error"],執行不正常結束的錯訊息陣列.
7439
		#$result["simpleError"],簡單表示的錯誤訊息.
7440
		#$result["function"],當前執行的函式名稱.
7441
		#$result["argu"],設置給予的參數.
7442
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
7443
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
7444
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
7445
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
7446
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
7447
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
7448
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
7449
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
7450
		#必填參數:
7451
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
7452
		$conf["variableCheck::checkArguments"]["varInput"]=&$request;
7453
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
7454
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
7455
		#可省略參數:
7456
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
7457
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("cmd2run");
7458
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
7459
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string");
7460
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
7461
		#$conf["variableCheck::checkArguments"]["canBeEmptyString"]="true";
7462
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
7463
		#$conf["variableCheck::checkArguments"]["canNotBeEmpty"]=array("cmd");
7464
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
7465
		#$conf["canBeEmpty"]=array();
7466
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
7467
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("params");
7468
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
7469
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("id","params");
7470
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
7471
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","array");
7472
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
7473
		#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array($phpMaxMemInConfig,"10",null);
7474
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
7475
		#$conf["disallowAllSkipableVarIsEmpty"]="";
7476
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
7477
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
7478
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
7479
		#$conf["arrayCountEqualCheck"][]=array();
7480
		#參考資料:
7481
		#array_keys=>http://php.net/manual/en/function.array-keys.php
7482
		#備註:
7483
		#無.
7484
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
7485
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 7486
 
3 liveuser 7487
		#debug
7488
		#var_dump(__LINE__,$checkArguments);
226 liveuser 7489
 
3 liveuser 7490
		#如果執行失敗
7491
		if($checkArguments["status"]==="false"){
226 liveuser 7492
 
3 liveuser 7493
			#設置執行失敗
7494
			$result["status"]="false";
7495
 
7496
			#設置執行錯誤訊息
7497
			$result["error"]=$checkArguments;
226 liveuser 7498
 
3 liveuser 7499
			#回傳結果
7500
			return $result;
226 liveuser 7501
 
3 liveuser 7502
			}#if end
226 liveuser 7503
 
3 liveuser 7504
		#如果檢查不通過
7505
		if($checkArguments["passed"]==="false"){
226 liveuser 7506
 
3 liveuser 7507
			#設置執行失敗
7508
			$result["status"]="false";
7509
 
7510
			#設置執行錯誤訊息
7511
			$result["error"]=$checkArguments;
226 liveuser 7512
 
3 liveuser 7513
			#回傳結果
7514
			return $result;
226 liveuser 7515
 
3 liveuser 7516
			}#if end
226 liveuser 7517
 
3 liveuser 7518
		#函式說明:
7519
		#呼叫shell執行系統命令,並取得回傳的內容.
7520
		#回傳結果:
7521
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7522
		#$result["error"],錯誤訊息陣列.
7523
		#$result["function"],當前執行的函數名稱.
7524
		#$result["argu"],使用的參數.
7525
		#$result["cmd"],執行的指令內容.
7526
		#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
7527
		#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
7528
		#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
7529
		#$result["running"],是否還在執行.
7530
		#$result["pid"],pid.
7531
		#$result["statusCode"],執行結束後的代碼.
7532
		#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
7533
		#必填參數:
7534
		#$conf["command"],字串,要執行的指令.
7535
		$conf["external::callShell"]["command"]=$request["cmd2run"];
7536
		#$conf["fileArgu"],字串,變數__FILE__的內容.
7537
		$conf["external::callShell"]["fileArgu"]=__FILE__;
7538
		#可省略參數:
7539
		#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
7540
		$conf["external::callShell"]["argu"]=$request["params"];
7541
		#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
7542
		#$conf["arguIsAddr"]=array();
7543
		#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
7544
		#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
7545
		#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
7546
		#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
7547
		#$conf["enablePrintDescription"]="true";
7548
		#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
7549
		#$conf["printDescription"]="";
7550
		#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
7551
		$conf["external::callShell"]["escapeshellarg"]="true";
7552
		#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
7553
		#$conf["thereIsShellVar"]=array();
7554
		#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
7555
		#$conf["username"]="";
7556
		#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
7557
		#$conf["password"]="";
7558
		#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
7559
		#$conf["useScript"]="";
7560
		#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
7561
		#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
7562
		#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
7563
		#$conf["inBackGround"]="";
7564
		#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
7565
		#$conf["getErr"]="false";
7566
		#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
7567
		$conf["external::callShell"]["doNotRun"]="true";
7568
		#參考資料:
7569
		#exec=>http://php.net/manual/en/function.exec.php
7570
		#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
7571
		#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
7572
		#備註:
7573
		#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
7574
		#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
7575
		$callShell=external::callShell($conf["external::callShell"]);
7576
		unset($conf["external::callShell"]);
226 liveuser 7577
 
3 liveuser 7578
		#如果執行失敗
7579
		if($callShell["status"]==="false"){
226 liveuser 7580
 
3 liveuser 7581
			#設置執行失敗
7582
			$result["status"]="false";
226 liveuser 7583
 
3 liveuser 7584
			#設置執行錯誤訊息
7585
			$result["error"]=$callShell;
226 liveuser 7586
 
3 liveuser 7587
			#回傳結果
7588
			return $result;
226 liveuser 7589
 
3 liveuser 7590
			}#if end
226 liveuser 7591
 
3 liveuser 7592
		#函式說明:
7593
		#將一維陣列轉換為用特定符號間隔的字串,ex:array("1","2","3") to "a;b;c;".
7594
		#回傳的結果:
7595
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7596
		#$result["function"],當前執行的function名稱
7597
		#$result["error"],錯誤訊息陣列.
7598
		#$result["content"],處理好的字串.
7599
		#$result["argu"],使用的參數.
7600
		#必填參數:
7601
		#$conf["inputArray"],字串陣列,要轉成字串的一維陣列.
7602
		$conf["arrays::arrayToString"]["inputArray"]=$callShell["escape"]["array"];
7603
		#可省略參數:
7604
		#$conf["spiltSymbol"],字串,用來區隔字串的符號,預設為;
7605
		$conf["arrays::arrayToString"]["spiltSymbol"]=" ";
7606
		#$conf["skipEnd"],字串,結尾是否不要加上符號,預設為"false",要加上符號,"true"代表不要加上符號。
7607
		$conf["arrays::arrayToString"]["skipEnd"]="true";
7608
		#參考資料:
7609
		#無.
7610
		#備註:
7611
		#無.
7612
		$arrayToString=arrays::arrayToString($conf["arrays::arrayToString"]);
7613
		unset($conf["arrays::arrayToString"]);
226 liveuser 7614
 
3 liveuser 7615
		#如果執行失敗
7616
		if($arrayToString["status"]==="false"){
226 liveuser 7617
 
3 liveuser 7618
			#設置執行失敗
7619
			$result["status"]="false";
226 liveuser 7620
 
3 liveuser 7621
			#設置執行錯誤訊息
7622
			$result["error"]=$arrayToString;
226 liveuser 7623
 
3 liveuser 7624
			#回傳結果
7625
			return $result;
226 liveuser 7626
 
3 liveuser 7627
			}#if end
226 liveuser 7628
 
3 liveuser 7629
		#函式說明:
7630
		#透過proc來多執行序運作.
7631
		#回傳結果:
7632
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7633
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
7634
		#$result["function"],當前執行的函式名稱.
7635
		#$result["argu"],使用的參數.
7636
		#$result["content"],陣列,每個元素為其指令執行的結果訊息陣列,key為"status"代表執行是否正常的識別;key為"statusCode"代表程式結束後回傳給對應executeBy程式的數值;key為"output"代表標準輸出,若為resource,則代表為pipe;key為"error"代表非標準輸出,若為resource,則代表為pipe;key為"input"代表成功輸入的指令;key為"process"代表該程序經proc_open後的process source;key為"proc_get_status"代表程序的資訊.
7637
		#必填參數:
7638
		#$conf["cmds"],字串陣列,每個元素代表要執行的指令與參數.
7639
		$conf["threads::proc"]["cmds"]=array($arrayToString["content"]);
7640
		#可省略參數:
7641
		#$conf["wait"],字串,是否需要等待所有程序結束,預設為"true"要等待;反之為"false"不要等待.
7642
		#$conf["threads::proc"]["wait"]="false";
7643
		#$conf["workingDir"],字串陣列,個別程式執行時的家目錄,預設不指定.
7644
		#$conf["workingDir"]=array("path");
7645
		#$conf["envs"],2維字串陣列,每個元素代表個別程式執行時的指定環境變數,key變數名稱;value為變數內容.預設為array("QBPWCF" => "Quick Build PHP Website Componment base on Fedora Linux");
7646
		#$conf["envs"]=array(array("key"=>"value"));
7647
		#$conf["executeBy"],字串陣列,每個元素代表個別指令要用什麼程式執行,預設為"bash".
7648
		#$conf["executeBy"]=array("bash");
7649
		#參考資料:
7650
		#https://www.php.net/manual/en/function.proc-open.php
7651
		#https://www.php.net/manual/en/function.proc-get-status.php
7652
		#備註:
7653
		#無.
7654
		$proc=threads::proc($conf["threads::proc"]);
7655
		unset($conf["threads::proc"]);
226 liveuser 7656
 
3 liveuser 7657
		/* debug
226 liveuser 7658
 
3 liveuser 7659
		#函式說明:
7660
		#撰寫log
7661
		#回傳結果:
7662
		#$result["status"],狀態,"true"或"false".
7663
		#$result["error"],錯誤訊息陣列.
7664
		#$result["function"],當前函式的名稱.
7665
		#$result["argu"],使用的參數.
7666
		#$result["content"],要寫入log的內容字串.
7667
		#必填參數:
7668
		#$conf["path"],字串,log檔案的路徑與名稱.
7669
		$conf["logs::record"]["path"]=$conf["socket"]."(sock::addOnProcessFuncForUnixDomainSockServerInRunWithAutoAddMemoryDaemon).log";
7670
		#$conf["content"],any,要寫的內容,若內容不為字串則會用var_dump的格式寫入.
7671
		$conf["logs::record"]["content"]=$proc;
7672
		#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
7673
		$conf["logs::record"]["fileArgu"]=__FILE__;
7674
		#可省略參數:
7675
		#$conf["rewrite"],預設為"false",接續寫入;反之"true"代表重新寫入.
7676
		#$conf["rewrite"]="false";
7677
		#$conf["returnOnly"],預設為"false",會寫入到log檔案.若為"true"則不會寫入log.
7678
		#$conf["returnOnly"]="true";
7679
		#參考資料:
7680
		#無.
7681
		#備註:
7682
		#無.
7683
		$record=logs::record($conf["logs::record"]);
7684
		unset($conf["logs::record"]);
226 liveuser 7685
 
3 liveuser 7686
		#如果執行失敗
7687
		if($record["status"]==="false"){
226 liveuser 7688
 
3 liveuser 7689
			#設置執行錯誤識別
7690
			$result["status"]="false";
226 liveuser 7691
 
3 liveuser 7692
			#設置執行錯誤
7693
			$result["error"]=$record;
226 liveuser 7694
 
3 liveuser 7695
			#回傳結果
7696
			return $result;
226 liveuser 7697
 
3 liveuser 7698
			}#if end
226 liveuser 7699
 
3 liveuser 7700
		*/
226 liveuser 7701
 
3 liveuser 7702
		#設置要暫存在server端的內容
7703
		$result["cache"]=&$proc;
226 liveuser 7704
 
3 liveuser 7705
		#如果執行失敗
7706
		if($proc["status"]==="false"){
226 liveuser 7707
 
3 liveuser 7708
			#設置執行失敗
7709
			$result["status"]="false";
226 liveuser 7710
 
3 liveuser 7711
			#設置執行錯誤訊息
7712
			$result["error"]=$proc;
226 liveuser 7713
 
3 liveuser 7714
			#回傳結果
7715
			return $result;
226 liveuser 7716
 
3 liveuser 7717
			}#if end
226 liveuser 7718
 
3 liveuser 7719
		#設置執行正常
7720
		$result["status"]="true";
226 liveuser 7721
 
3 liveuser 7722
		#後面的程式不要執行
7723
		$result["continue"]="false";
226 liveuser 7724
 
3 liveuser 7725
		#回傳結果
7726
		return $result;
226 liveuser 7727
 
3 liveuser 7728
		}#function addOnProcessFuncForUnixDomainSockServerInRunWithAutoAddMemoryDaemon end
226 liveuser 7729
 
3 liveuser 7730
	/*
7731
	#函式說明:
7732
	#執行程式時,依照需要增加記憶體的上限.
7733
	#回傳結果:
7734
	#$result["status"],執行是否正常,"true"為正常,"false"為不正常.
7735
	#$result["error"],錯誤訊息陣列.
7736
	#$result["function"],當前執行的函數名稱.
7737
	#$result["argu"],使用的參數.
7738
	#$result["content"],程式執行後的結果(unix domain socket server的輸出).
7739
	#必填參數:
7740
	#$conf["cmd"],字串參數,要執行的指令.
7741
	$conf["cmd"]="";
7742
	#可省略參數:
7743
	#$conf["params"],陣列字串參數,每個元素代表依序哦昂使用的參數.
7744
	#$conf["params"]=array("");
7745
	#$conf["fileArgu"],字串,__FILE__的內容.
7746
	#$conf["fileArgu"]="";
7747
	#參考資料:
7748
	#https://www.php.net/manual/en/function.memory-get-usage.php
7749
	#https://www.php.net/manual/en/ini.core.php#ini.memory-limit
7750
	#https://www.php.net/manual/en/function.stream-get-meta-data.php
7751
	#備註:
7752
	#無.
7753
	*/
7754
	public static function runWithAutoAddMemoryDaemon(&$conf=array()){
226 liveuser 7755
 
3 liveuser 7756
		#初始化要回傳的結果
7757
		$result=array();
7758
 
7759
		#取得當前執行的函數名稱
7760
		$result["function"]=__FUNCTION__;
226 liveuser 7761
 
3 liveuser 7762
		#取得參數
7763
		$result["argu"]=$conf;
7764
 
7765
		#如果 $conf 不為陣列
7766
		if(gettype($conf)!="array"){
7767
 
7768
			#設置執行失敗
7769
			$result["status"]="false";
7770
 
7771
			#設置執行錯誤訊息
7772
			$result["error"][]="\$conf變數須為陣列形態";
7773
 
7774
			#如果傳入的參數為 null
7775
			if(is_null($conf)){
7776
 
7777
				#設置執行錯誤訊息
7778
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
7779
 
7780
				}#if end
7781
 
7782
			#回傳結果
7783
			return $result;
7784
 
7785
			}#if end
226 liveuser 7786
 
3 liveuser 7787
		#檢查參數
7788
		#函式說明:
7789
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
7790
		#回傳結果:
7791
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7792
		#$result["error"],執行不正常結束的錯訊息陣列.
7793
		#$result["simpleError"],簡單表示的錯誤訊息.
7794
		#$result["function"],當前執行的函式名稱.
7795
		#$result["argu"],設置給予的參數.
7796
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
7797
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
7798
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
7799
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
7800
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
7801
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
7802
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
7803
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
7804
		#必填參數:
7805
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
7806
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
7807
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
7808
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
7809
		#可省略參數:
7810
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
7811
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("cmd");
7812
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
7813
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string");
7814
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
7815
		$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
7816
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
7817
		#$conf["canNotBeEmpty"]=array();
7818
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
7819
		#$conf["canBeEmpty"]=array();
7820
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
7821
		$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("params","fileArgu");
7822
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
7823
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("params","fileArgu");
7824
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
7825
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("array","string");
7826
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
7827
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(null,__FILE__);
7828
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
7829
		#$conf["disallowAllSkipableVarIsEmpty"]="";
7830
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
7831
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
7832
		#$conf["disallowAllSkipableVarNotExist"],字串,是否不允許每個可省略參數都不存在,預設為"false"代表允許,反之為"true".
7833
		#$conf["disallowAllSkipableVarNotExist"]="";
7834
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
7835
		#$conf["arrayCountEqualCheck"][]=array();
7836
		#參考資料:
7837
		#array_keys=>http://php.net/manual/en/function.array-keys.php
7838
		#備註:
7839
		#無.
7840
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
7841
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 7842
 
3 liveuser 7843
		#如果執行失敗
7844
		if($checkArguments["status"]==="false"){
226 liveuser 7845
 
3 liveuser 7846
			#設置執行失敗
7847
			$result["status"]="false";
226 liveuser 7848
 
3 liveuser 7849
			#設置錯誤資訊
7850
			$result["error"]=$checkArguments;
226 liveuser 7851
 
3 liveuser 7852
			#回傳結果
7853
			return $result;
226 liveuser 7854
 
3 liveuser 7855
			}#if end
226 liveuser 7856
 
3 liveuser 7857
		#如果檢查不通過
7858
		if($checkArguments["passed"]==="false"){
226 liveuser 7859
 
3 liveuser 7860
			#設置執行失敗
7861
			$result["status"]="false";
226 liveuser 7862
 
3 liveuser 7863
			#設置錯誤資訊
7864
			$result["error"]=$checkArguments;
226 liveuser 7865
 
3 liveuser 7866
			#回傳結果
7867
			return $result;
226 liveuser 7868
 
3 liveuser 7869
			}#if end
226 liveuser 7870
 
3 liveuser 7871
		/*
7872
		#函式說明:
226 liveuser 7873
		#於 ./tmp 底下建立暫存目錄與回傳暫存檔案名稱路徑
3 liveuser 7874
		#回傳結果:
7875
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7876
		#$result["error"],錯誤訊息.
7877
		#$result["function"],當前執行的函數名稱.
7878
		#$result["content"],暫存檔案的路徑與名稱.
7879
		#必填參數:
7880
		#無.
7881
		#可省略參數:
7882
		#無.
7883
		#參考資料:
7884
		#無.
7885
		#備註:
7886
		#無.
7887
		*/
34 liveuser 7888
		$conf["fileAccess::createTempFile"]=array();
7889
		$createTempFile=fileAccess::createTempFile($conf["fileAccess::createTempFile"]);
7890
		unset($conf["fileAccess::createTempFile"]);
226 liveuser 7891
 
3 liveuser 7892
		#如果執行失敗
7893
		if($createTempFile["status"]==="false"){
226 liveuser 7894
 
3 liveuser 7895
			#設置執行失敗
7896
			$result["status"]="false";
7897
 
7898
			#設置執行錯誤訊息
7899
			$result["error"]=$createTempFile;
226 liveuser 7900
 
3 liveuser 7901
			#回傳結果
7902
			return $result;
226 liveuser 7903
 
3 liveuser 7904
			}#if end
226 liveuser 7905
 
3 liveuser 7906
		#取得暫存的 unix domain socket 檔案
7907
		$tmpUnixDomainSock=$createTempFile["content"];
226 liveuser 7908
 
3 liveuser 7909
		#初始化用來運行  unixDomainSockServer.php 的參數
7910
		$argu=array();
226 liveuser 7911
 
3 liveuser 7912
		#設置參數
7913
		$argu[]="--sock";
226 liveuser 7914
 
3 liveuser 7915
		#設置參數
7916
		$argu[]=$tmpUnixDomainSock;
226 liveuser 7917
 
3 liveuser 7918
		#設置參數
7919
		$argu[]="--addOnProcessFunc";
226 liveuser 7920
 
3 liveuser 7921
		#設置參數
7922
		$argu[]="\qbpwcf\sock::addOnProcessFuncForUnixDomainSockServerInRunWithAutoAddMemoryDaemon";
226 liveuser 7923
 
3 liveuser 7924
		#設置參數
7925
		$argu[]="--funcToRunWhenIdle";
226 liveuser 7926
 
3 liveuser 7927
		#設置參數
7928
		$argu[]="\qbpwcf\config::autoAddMemory";
226 liveuser 7929
 
3 liveuser 7930
		#設置參數
7931
		$argu[]="--onlyOutputCache";
226 liveuser 7932
 
3 liveuser 7933
		#設置參數
7934
		$argu[]="true";
226 liveuser 7935
 
3 liveuser 7936
		#設置參數
7937
		$argu[]="--cacheLayer";
226 liveuser 7938
 
3 liveuser 7939
		#設置參數
7940
		$argu[]="serverCache";
226 liveuser 7941
 
3 liveuser 7942
		#設置參數
7943
		$argu[]="--cacheLayer";
226 liveuser 7944
 
3 liveuser 7945
		#設置參數
7946
		$argu[]="serverSide";
226 liveuser 7947
 
3 liveuser 7948
		#設置參數
7949
		$argu[]="--cacheLayer";
226 liveuser 7950
 
3 liveuser 7951
		#設置參數
7952
		$argu[]="funcToRunWhenIdle";
226 liveuser 7953
 
3 liveuser 7954
		#設置參數
7955
		$argu[]="--cacheLayer";
226 liveuser 7956
 
3 liveuser 7957
		#設置參數
7958
		$argu[]="\qbpwcf\config::autoAddMemory";
226 liveuser 7959
 
3 liveuser 7960
		#設置參數
7961
		$argu[]="--cacheLayer";
226 liveuser 7962
 
3 liveuser 7963
		#設置參數
7964
		$argu[]="cache";
226 liveuser 7965
 
3 liveuser 7966
		#設置參數
7967
		$argu[]="--cacheLayer";
226 liveuser 7968
 
3 liveuser 7969
		#設置參數
7970
		$argu[]="stdout";
226 liveuser 7971
 
3 liveuser 7972
		#函式說明:
7973
		#呼叫shell執行系統命令,並取得回傳的內容.
7974
		#回傳結果:
7975
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7976
		#$result["error"],錯誤訊息陣列.
7977
		#$result["function"],當前執行的函數名稱.
7978
		#$result["argu"],使用的參數.
7979
		#$result["cmd"],執行的指令內容.
7980
		#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
7981
		#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
7982
		#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
7983
		#$result["running"],是否還在執行.
7984
		#$result["pid"],pid.
7985
		#$result["statusCode"],執行結束後的代碼.
7986
		#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
7987
		#必填參數:
7988
		#$conf["command"],字串,要執行的指令.
7989
		$conf["external::callShell"]["command"]="unixDomainSockServer.php";
7990
		#$conf["fileArgu"],字串,變數__FILE__的內容.
7991
		$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];
7992
		#可省略參數:
7993
		#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
7994
		$conf["external::callShell"]["argu"]=$argu;
7995
		#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
7996
		#$conf["arguIsAddr"]=array();
7997
		#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
7998
		#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
7999
		#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
8000
		#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
8001
		#$conf["enablePrintDescription"]="true";
8002
		#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
8003
		#$conf["printDescription"]="";
8004
		#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
8005
		$conf["external::callShell"]["escapeshellarg"]="true";
8006
		#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
8007
		#$conf["thereIsShellVar"]=array();
8008
		#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
8009
		#$conf["username"]="";
8010
		#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
8011
		#$conf["password"]="";
8012
		#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
8013
		#$conf["useScript"]="";
8014
		#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
8015
		#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
8016
		#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
8017
		#$conf["inBackGround"]="";
8018
		#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
8019
		#$conf["getErr"]="false";
8020
		#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
8021
		$conf["external::callShell"]["doNotRun"]="true";
8022
		#參考資料:
8023
		#exec=>http://php.net/manual/en/function.exec.php
8024
		#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
8025
		#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
8026
		#備註:
8027
		#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
8028
		#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
8029
		$callShell=external::callShell($conf["external::callShell"]);
8030
		unset($conf["external::callShell"]);
226 liveuser 8031
 
3 liveuser 8032
		#debug
8033
		#var_dump(__LINE__,$callShell);exit;
226 liveuser 8034
 
3 liveuser 8035
		#如果執行失敗
8036
		if($callShell["status"]==="false"){
226 liveuser 8037
 
3 liveuser 8038
			#如果socket存在
8039
			if(file_exists($tmpUnixDomainSock)){
226 liveuser 8040
 
3 liveuser 8041
				#移除之
8042
				unlink($tmpUnixDomainSock);
226 liveuser 8043
 
3 liveuser 8044
				}#if end
226 liveuser 8045
 
3 liveuser 8046
			#設置執行失敗
8047
			$result["status"]="false";
8048
 
8049
			#設置執行錯誤訊息
8050
			$result["error"]=$callShell;
226 liveuser 8051
 
3 liveuser 8052
			#回傳結果
8053
			return $result;
226 liveuser 8054
 
3 liveuser 8055
			}#if end
226 liveuser 8056
 
3 liveuser 8057
		#函式說明:
8058
		#將一維陣列轉換為用特定符號間隔的字串,ex:array("1","2","3") to "a;b;c;".
8059
		#回傳的結果:
8060
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
8061
		#$result["function"],當前執行的function名稱
8062
		#$result["error"],錯誤訊息陣列.
8063
		#$result["content"],處理好的字串.
8064
		#$result["argu"],使用的參數.
8065
		#必填參數:
8066
		#$conf["inputArray"],字串陣列,要轉成字串的一維陣列.
8067
		$conf["arrays::arrayToString"]["inputArray"]=$callShell["escape"]["array"];
8068
		#可省略參數:
8069
		#$conf["spiltSymbol"],字串,用來區隔字串的符號,預設為;
8070
		$conf["arrays::arrayToString"]["spiltSymbol"]=" ";
8071
		#$conf["skipEnd"],字串,結尾是否不要加上符號,預設為"false",要加上符號,"true"代表不要加上符號。
8072
		$conf["arrays::arrayToString"]["skipEnd"]="true";
8073
		#參考資料:
8074
		#無.
8075
		#備註:
8076
		#無.
8077
		$arrayToString=arrays::arrayToString($conf["arrays::arrayToString"]);
8078
		unset($conf["arrays::arrayToString"]);
226 liveuser 8079
 
3 liveuser 8080
		#如果執行失敗
8081
		if($arrayToString["status"]==="false"){
226 liveuser 8082
 
3 liveuser 8083
			#如果socket存在
8084
			if(file_exists($tmpUnixDomainSock)){
226 liveuser 8085
 
3 liveuser 8086
				#移除之
8087
				unlink($tmpUnixDomainSock);
226 liveuser 8088
 
3 liveuser 8089
				}#if end
226 liveuser 8090
 
3 liveuser 8091
			#設置執行失敗
8092
			$result["status"]="false";
8093
 
8094
			#設置執行錯誤訊息
8095
			$result["error"]=$arrayToString;
226 liveuser 8096
 
3 liveuser 8097
			#回傳結果
8098
			return $result;
226 liveuser 8099
 
3 liveuser 8100
			}#if end
226 liveuser 8101
 
3 liveuser 8102
		#函式說明:
8103
		#透過proc來多執行序運作.
8104
		#回傳結果:
8105
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
8106
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
8107
		#$result["function"],當前執行的函式名稱.
8108
		#$result["argu"],使用的參數.
8109
		#$result["content"],陣列,每個元素為其指令執行的結果訊息陣列,key為"status"代表執行是否正常的識別;key為"statusCode"代表程式結束後回傳給對應executeBy程式的數值;key為"output"代表標準輸出,若為resource,則代表為pipe;key為"error"代表非標準輸出,若為resource,則代表為pipe;key為"input"代表成功輸入的指令;key為"process"代表該程序經proc_open後的process source;key為"proc_get_status"代表程序的資訊.
8110
		#必填參數:
8111
		#$conf["cmds"],字串陣列,每個元素代表要執行的指令與參數.
8112
		$conf["threads::proc"]["cmds"]=array($arrayToString["content"]);
8113
		#可省略參數:
8114
		#$conf["wait"],字串,是否需要等待所有程序結束,預設為"true"要等待;反之為"false"不要等待.
8115
		$conf["threads::proc"]["wait"]="false";
8116
		#$conf["workingDir"],字串陣列,個別程式執行時的家目錄,預設不指定.
8117
		#$conf["workingDir"]=array("path");
8118
		#$conf["envs"],2維字串陣列,每個元素代表個別程式執行時的指定環境變數,key變數名稱;value為變數內容.預設為array("QBPWCF" => "Quick Build PHP Website Componment base on Fedora Linux");
8119
		#$conf["envs"]=array(array("key"=>"value"));
8120
		#$conf["executeBy"],字串陣列,每個元素代表個別指令要用什麼程式執行,預設為"bash".
8121
		#$conf["executeBy"]=array("bash");
8122
		#參考資料:
8123
		#https://www.php.net/manual/en/function.proc-open.php
8124
		#https://www.php.net/manual/en/function.proc-get-status.php
8125
		#備註:
8126
		#無.
226 liveuser 8127
		$procForUnixDoaminSocket=threads::proc($conf["threads::proc"]);
8128
		unset($conf["threads::proc"]);
8129
 
3 liveuser 8130
		#如果執行失敗
8131
		if($procForUnixDoaminSocket["status"]==="false"){
226 liveuser 8132
 
3 liveuser 8133
			#如果socket存在
8134
			if(file_exists($tmpUnixDomainSock)){
226 liveuser 8135
 
3 liveuser 8136
				#移除之
8137
				unlink($tmpUnixDomainSock);
226 liveuser 8138
 
3 liveuser 8139
				}#if end
226 liveuser 8140
 
3 liveuser 8141
			#設置執行失敗
8142
			$result["status"]="false";
8143
 
8144
			#設置執行錯誤訊息
8145
			$result["error"]=$procForUnixDoaminSocket;
226 liveuser 8146
 
3 liveuser 8147
			#回傳結果
8148
			return $result;
226 liveuser 8149
 
3 liveuser 8150
			}#if end
226 liveuser 8151
 
3 liveuser 8152
		#var_dump($procForUnixDoaminSocket);exit;
226 liveuser 8153
 
3 liveuser 8154
		#如果 unix doamin socket server 沒有在運行
8155
		if($procForUnixDoaminSocket["content"][0]["proc_get_status"]["running"]===false){
226 liveuser 8156
 
3 liveuser 8157
			#如果socket存在
8158
			if(file_exists($tmpUnixDomainSock)){
226 liveuser 8159
 
3 liveuser 8160
				#移除之
8161
				unlink($tmpUnixDomainSock);
226 liveuser 8162
 
3 liveuser 8163
				}#if end
226 liveuser 8164
 
3 liveuser 8165
			#設置執行失敗
8166
			$result["status"]="false";
8167
 
8168
			#設置執行錯誤訊息
8169
			$result["error"]=$procForUnixDoaminSocket;
226 liveuser 8170
 
3 liveuser 8171
			#回傳結果
8172
			return $result;
226 liveuser 8173
 
3 liveuser 8174
			}#if end
226 liveuser 8175
 
3 liveuser 8176
		#如果當下 unix doamin socket server 沒有在運行
8177
		if(proc_get_status($procForUnixDoaminSocket["content"][0]["process"])["running"]===false){
226 liveuser 8178
 
3 liveuser 8179
			#如果socket存在
8180
			if(file_exists($tmpUnixDomainSock)){
226 liveuser 8181
 
3 liveuser 8182
				#移除之
8183
				unlink($tmpUnixDomainSock);
226 liveuser 8184
 
3 liveuser 8185
				}#if end
226 liveuser 8186
 
3 liveuser 8187
			#設置執行失敗
8188
			$result["status"]="false";
8189
 
8190
			#設置執行錯誤訊息
8191
			$result["error"]=$procForUnixDoaminSocket;
226 liveuser 8192
 
3 liveuser 8193
			#回傳結果
8194
			return $result;
226 liveuser 8195
 
3 liveuser 8196
			}#if end
226 liveuser 8197
 
3 liveuser 8198
		#debug
8199
		#var_dump(__LINE__,"before send msg");
226 liveuser 8200
 
3 liveuser 8201
		#初始化要求執行程式的訊息
8202
		$msg=array();
226 liveuser 8203
 
3 liveuser 8204
		#設置執行的指令
8205
		$msg["cmd2run"]=$conf["cmd"];
226 liveuser 8206
 
3 liveuser 8207
		#如果有參數
8208
		if(isset($conf["params"])){
226 liveuser 8209
 
3 liveuser 8210
			#設置參數
8211
			$msg["params"]=$conf["params"];
226 liveuser 8212
 
3 liveuser 8213
			}#if end
226 liveuser 8214
 
3 liveuser 8215
		#初始化儲存等待了幾秒
8216
		$waitSec=0;
226 liveuser 8217
 
3 liveuser 8218
		#無窮迴圈
8219
		while(true){
226 liveuser 8220
 
3 liveuser 8221
			#函式說明:
8222
			#連線到 unixDomainSockServer 提供的 unix domain socket.
8223
			#回傳結果:
8224
			#$result["status"],"true"代表執行正常;"false"代表執行不正常.
8225
			#$result["error"],錯誤訊息陣列.
8226
			#$result["function"],當前執行的函式名稱.
8227
			#$result["content"],取得的回應.
8228
			#必填參數:
8229
			#$conf["sock"],字串,要連線的unix domain socket.
8230
			$conf["sock::unixDomainSockClient"]["sock"]=$tmpUnixDomainSock;
8231
			#可省略參數:
8232
			#$conf["id"],字串,取得的id,若無此值,則會得到新的數值.
8233
			#$conf["id"]="";
8234
			#$conf["cmd"],字串,要執行的指令,當$conf["id"]參數合法時,才會執行.
8235
			#$conf["sock::unixDomainSockClient"]["cmd"]=$callShell["escape"]["cmd"];
8236
			#$conf["param"],參數陣列.
8237
			#$conf["sock::unixDomainSockClient"]["param"]=$callShell["escape"]["argu"];
8238
			#$conf["escaped"],字串,param參數是否已經escaped了,預設為"false",反之為"true".
8239
			#$conf["sock::unixDomainSockClient"]["escaped"]="true";
8240
			#$conf["custom"],陣列,要客制化傳輸的內容,會覆蓋以上除了$conf["id"]以外的可省略參數.
8241
			#$conf["custom"]=array();
8242
			#參考資料:
8243
			#http://php.net/manual/en/function.stream-socket-client.php
8244
			#http://php.net/manual/en/function.stream-get-contents.php
8245
			#備註:
8246
			#無.
8247
			$paramsOfUnixDomainSockClient=$conf["sock::unixDomainSockClient"];
8248
			$unixDomainSockClient=sock::unixDomainSockClient($paramsOfUnixDomainSockClient);
8249
			unset($paramsOfUnixDomainSockClient);
226 liveuser 8250
 
3 liveuser 8251
			#如果執行成功
8252
			if($unixDomainSockClient["status"]==="true"){
226 liveuser 8253
 
3 liveuser 8254
				#debug
8255
				#var_dump(__LINE__,"before parse id");
226 liveuser 8256
 
3 liveuser 8257
				#跳出while
8258
				break;
226 liveuser 8259
 
3 liveuser 8260
				}#if end
226 liveuser 8261
 
3 liveuser 8262
			#提示錯誤
8263
			else{
226 liveuser 8264
 
3 liveuser 8265
				#debug
8266
				#var_dump(__LINE__,$unixDomainSockClient);
226 liveuser 8267
 
3 liveuser 8268
				}#else end
226 liveuser 8269
 
3 liveuser 8270
			#如果當下 unix doamin socket server 沒有在運行
8271
			if(proc_get_status($procForUnixDoaminSocket["content"][0]["process"])["running"]===false){
226 liveuser 8272
 
3 liveuser 8273
				#如果socket存在
8274
				if(file_exists($tmpUnixDomainSock)){
226 liveuser 8275
 
3 liveuser 8276
					#移除之
8277
					unlink($tmpUnixDomainSock);
226 liveuser 8278
 
3 liveuser 8279
					}#if end
226 liveuser 8280
 
3 liveuser 8281
				#設置執行失敗
8282
				$result["status"]="false";
8283
 
8284
				#設置執行錯誤訊息
8285
				$result["error"][]="client取得回應前server就意外結束了";
8286
 
8287
				#設置執行錯誤訊息
8288
				$result["error"][]=$procForUnixDoaminSocket;
226 liveuser 8289
 
3 liveuser 8290
				#回傳結果
8291
				return $result;
226 liveuser 8292
 
3 liveuser 8293
				}#if end
226 liveuser 8294
 
3 liveuser 8295
			#等後1秒,擔心unix domain socket server 尚未跑起來.
8296
			sleep(1);
226 liveuser 8297
 
3 liveuser 8298
			#已等待時間加1秒
8299
			$waitSec=$waitSec+1;
226 liveuser 8300
 
3 liveuser 8301
			}#while end
226 liveuser 8302
 
3 liveuser 8303
		#debug
8304
		#var_dump(__LINE__,"json string got:".$unixDomainSockClient["content"]);
226 liveuser 8305
 
3 liveuser 8306
		#取得json回應
8307
		$jsonRes=json_decode($unixDomainSockClient["content"]);
226 liveuser 8308
 
3 liveuser 8309
		#如果執行失敗
8310
		if($jsonRes===null){
226 liveuser 8311
 
3 liveuser 8312
			#如果socket存在
8313
			if(file_exists($tmpUnixDomainSock)){
226 liveuser 8314
 
3 liveuser 8315
				#移除之
8316
				unlink($tmpUnixDomainSock);
226 liveuser 8317
 
3 liveuser 8318
				}#if end
226 liveuser 8319
 
3 liveuser 8320
			#設置執行錯誤識別
8321
			$result["status"]="false";
226 liveuser 8322
 
3 liveuser 8323
			#設置錯誤訊息
8324
			$result["error"]=$unixDomainSockClient;
226 liveuser 8325
 
3 liveuser 8326
			#回傳結果
8327
			return $result;
226 liveuser 8328
 
3 liveuser 8329
			}#if end
226 liveuser 8330
 
3 liveuser 8331
		#如果沒有產生新id
8332
		if(!isset($jsonRes->id)){
226 liveuser 8333
 
3 liveuser 8334
			#如果socket存在
8335
			if(file_exists($tmpUnixDomainSock)){
226 liveuser 8336
 
3 liveuser 8337
				#移除之
8338
				unlink($tmpUnixDomainSock);
226 liveuser 8339
 
3 liveuser 8340
				}#if end
226 liveuser 8341
 
3 liveuser 8342
			#設置執行錯誤識別
8343
			$result["status"]="false";
226 liveuser 8344
 
3 liveuser 8345
			#設置錯誤訊息
8346
			$result["error"]=$unixDomainSockClient;
226 liveuser 8347
 
3 liveuser 8348
			#回傳結果
8349
			return $result;
226 liveuser 8350
 
3 liveuser 8351
			}#if end
226 liveuser 8352
 
3 liveuser 8353
		#debug
8354
		#var_dump(__LINE__,"got id:".$jsonRes->id);
226 liveuser 8355
 
3 liveuser 8356
		#debug
8357
		#while(true){};
226 liveuser 8358
 
3 liveuser 8359
		#用新的id再傳送一次要求給 qbpwcf_usock_path
8360
		$paramsOfUnixDomainSockClient=array();
8361
		$paramsOfUnixDomainSockClient["sock"]=$tmpUnixDomainSock;
8362
		$paramsOfUnixDomainSockClient["id"]=$jsonRes->id;
8363
		$paramsOfUnixDomainSockClient["custom"]=$msg;
8364
		$unixDomainSockClient=sock::unixDomainSockClient($paramsOfUnixDomainSockClient);
8365
		unset($paramsOfUnixDomainSockClient);
226 liveuser 8366
 
3 liveuser 8367
		#debug
8368
		#var_dump(__LINE__,$unixDomainSockClient);
226 liveuser 8369
 
3 liveuser 8370
		#如果執行失敗
8371
		if($unixDomainSockClient["status"]==="false"){
226 liveuser 8372
 
3 liveuser 8373
			#如果socket存在
8374
			if(file_exists($tmpUnixDomainSock)){
226 liveuser 8375
 
3 liveuser 8376
				#移除之
8377
				unlink($tmpUnixDomainSock);
226 liveuser 8378
 
3 liveuser 8379
				}#if end
226 liveuser 8380
 
3 liveuser 8381
			#設置執行錯誤識別
8382
			$result["status"]="false";
226 liveuser 8383
 
3 liveuser 8384
			#設置錯誤訊息
8385
			$result["error"]=$unixDomainSockClient;
226 liveuser 8386
 
3 liveuser 8387
			#回傳結果
8388
			return $result;
226 liveuser 8389
 
3 liveuser 8390
			}#if end
226 liveuser 8391
 
3 liveuser 8392
		#debug
8393
		#var_dump(__LINE__,$unixDomainSockClient);
226 liveuser 8394
 
3 liveuser 8395
		#如果有回應的內容
8396
		if(!empty($unixDomainSockClient["content"])){
226 liveuser 8397
 
3 liveuser 8398
			#取得json回應
8399
			$jsonRes=(array)json_decode($unixDomainSockClient["content"]);
226 liveuser 8400
 
3 liveuser 8401
			#如果執行失敗
8402
			if($jsonRes===null){
226 liveuser 8403
 
3 liveuser 8404
				#如果socket存在
8405
				if(file_exists($tmpUnixDomainSock)){
226 liveuser 8406
 
3 liveuser 8407
					#移除之
8408
					unlink($tmpUnixDomainSock);
226 liveuser 8409
 
3 liveuser 8410
					}#if end
226 liveuser 8411
 
3 liveuser 8412
				#設置執行錯誤識別
8413
				$result["status"]="false";
226 liveuser 8414
 
3 liveuser 8415
				#設置錯誤訊息
8416
				$result["error"]=$unixDomainSockClient;
226 liveuser 8417
 
3 liveuser 8418
				#回傳結果
8419
				return $result;
226 liveuser 8420
 
3 liveuser 8421
				}#if end
226 liveuser 8422
 
3 liveuser 8423
			#如果執行指令失敗
8424
			if($jsonRes["status"]==="false"){
226 liveuser 8425
 
3 liveuser 8426
				#如果socket存在
8427
				if(file_exists($tmpUnixDomainSock)){
226 liveuser 8428
 
3 liveuser 8429
					#移除之
8430
					unlink($tmpUnixDomainSock);
226 liveuser 8431
 
3 liveuser 8432
					}#if end
226 liveuser 8433
 
3 liveuser 8434
				#設置執行錯誤識別
8435
				$result["status"]="false";
226 liveuser 8436
 
3 liveuser 8437
				#設置錯誤訊息
8438
				$result["error"]=$jsonRes["error"];
226 liveuser 8439
 
3 liveuser 8440
				#回傳結果
8441
				return $result;
226 liveuser 8442
 
3 liveuser 8443
				}#if end
226 liveuser 8444
 
3 liveuser 8445
			#印出client收到的結果
8446
			#var_dump($jsonRes);
226 liveuser 8447
 
3 liveuser 8448
			}#if end
226 liveuser 8449
 
3 liveuser 8450
		#debug
8451
		#var_dump(__LINE__,$procForUnixDoaminSocket);
226 liveuser 8452
 
3 liveuser 8453
		#初始化儲存標準輸出
8454
		$result["content"]["stdout"]="";
226 liveuser 8455
 
3 liveuser 8456
		#初始化儲存非標準輸出
8457
		$result["content"]["stderr"]="";
226 liveuser 8458
 
3 liveuser 8459
		#等待 unixDomainSockServer 結束執行
8460
		while(true){
226 liveuser 8461
 
3 liveuser 8462
			#取得程序狀態
8463
			$proc_get_status=proc_get_status($procForUnixDoaminSocket["content"][0]["process"]);
226 liveuser 8464
 
3 liveuser 8465
			/* debug
226 liveuser 8466
 
3 liveuser 8467
			#函式說明:
8468
			#撰寫log
8469
			#回傳結果:
8470
			#$result["status"],狀態,"true"或"false".
8471
			#$result["error"],錯誤訊息陣列.
8472
			#$result["function"],當前函式的名稱.
8473
			#$result["argu"],使用的參數.
8474
			#$result["content"],要寫入log的內容字串.
8475
			#必填參數:
8476
			#$conf["path"],字串,log檔案的路徑與名稱.
8477
			$conf["logs::record"]["path"]="/tmp/sock::runWithAutoAddMemoryDaemon.log";
8478
			#$conf["content"],any,要寫的內容,若內容不為字串則會用var_dump的格式寫入.
8479
			$conf["logs::record"]["content"]=$proc_get_status;
8480
			#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
8481
			$conf["logs::record"]["fileArgu"]=__FILE__;
8482
			#可省略參數:
8483
			#$conf["rewrite"],預設為"false",接續寫入;反之"true"代表重新寫入.
8484
			#$conf["rewrite"]="false";
8485
			#$conf["returnOnly"],預設為"false",會寫入到log檔案.若為"true"則不會寫入log.
8486
			#$conf["returnOnly"]="true";
8487
			#參考資料:
8488
			#無.
8489
			#備註:
8490
			#無.
8491
			$record=logs::record($conf["logs::record"]);
8492
			unset($conf["logs::record"]);
226 liveuser 8493
 
3 liveuser 8494
			#如果執行失敗
8495
			if($record["status"]==="false"){
226 liveuser 8496
 
3 liveuser 8497
				#設置執行錯誤識別
8498
				$result["status"]="false";
226 liveuser 8499
 
3 liveuser 8500
				#設置執行錯誤
8501
				$result["error"]=$record;
226 liveuser 8502
 
3 liveuser 8503
				#回傳結果
8504
				return $result;
226 liveuser 8505
 
3 liveuser 8506
				}#if end
226 liveuser 8507
 
3 liveuser 8508
			*/
226 liveuser 8509
 
3 liveuser 8510
			#確認process是否存在
8511
			#ps auxwf | grep sshd | grep -v 'grep' | wc -l
8512
			#函式說明:
8513
			#呼叫shell執行系統命令,並取得回傳的內容.
8514
			#回傳結果:
8515
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
8516
			#$result["error"],錯誤訊息陣列.
8517
			#$result["function"],當前執行的函數名稱.
8518
			#$result["argu"],使用的參數.
8519
			#$result["cmd"],執行的指令內容.
8520
			#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
8521
			#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
8522
			#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
8523
			#$result["running"],是否還在執行.
8524
			#$result["pid"],pid.
8525
			#$result["statusCode"],執行結束後的代碼.
8526
			#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
8527
			#必填參數:
8528
			#$conf["command"],字串,要執行的指令.
8529
			$conf["external::callShell"]["command"]="ps";
8530
			#$conf["fileArgu"],字串,變數__FILE__的內容.
8531
			$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];
8532
			#可省略參數:
8533
			#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
8534
			$conf["external::callShell"]["argu"]=array("auxwf","|","grep",$proc_get_status["pid"],"|","grep","-v","'grep'","|","wc","-l");
8535
			#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
8536
			#$conf["arguIsAddr"]=array();
8537
			#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
8538
			#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
8539
			#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
8540
			#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
8541
			#$conf["enablePrintDescription"]="true";
8542
			#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
8543
			#$conf["printDescription"]="";
8544
			#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
8545
			#$conf["escapeshellarg"]="false";
8546
			#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
8547
			#$conf["thereIsShellVar"]=array();
8548
			#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
8549
			#$conf["username"]="";
8550
			#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
8551
			#$conf["password"]="";
8552
			#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
8553
			#$conf["useScript"]="";
8554
			#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
8555
			#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
8556
			#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
8557
			#$conf["inBackGround"]="";
8558
			#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
8559
			#$conf["getErr"]="false";
8560
			#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
8561
			#$conf["doNotRun"]="false";
8562
			#參考資料:
8563
			#exec=>http://php.net/manual/en/function.exec.php
8564
			#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
8565
			#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
8566
			#備註:
8567
			#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
8568
			#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
8569
			$callShell=external::callShell($conf["external::callShell"]);
8570
			unset($conf["external::callShell"]);
226 liveuser 8571
 
3 liveuser 8572
			#如果執行失敗
8573
			if($callShell["status"]==="false"){
226 liveuser 8574
 
3 liveuser 8575
				#設置執行失敗
8576
				$result["status"]="false";
8577
 
8578
				#設置執行錯誤訊息
8579
				$result["error"]=$callShell;
8580
 
8581
				#回傳結果
8582
				return $result;
226 liveuser 8583
 
3 liveuser 8584
				}#if end
226 liveuser 8585
 
3 liveuser 8586
			#如果沒有輸出結果
8587
			if(!isset($callShell["output"][0])){
226 liveuser 8588
 
3 liveuser 8589
				#設置執行失敗
8590
				$result["status"]="false";
8591
 
8592
				#設置執行錯誤訊息
8593
				$result["error"]=$callShell;
8594
 
8595
				#回傳結果
8596
				return $result;
226 liveuser 8597
 
3 liveuser 8598
				}#if end
226 liveuser 8599
 
3 liveuser 8600
			#如果執行結束了
8601
			if( $proc_get_status["running"]===false || $callShell["output"][0]==="0" ){
226 liveuser 8602
 
3 liveuser 8603
				#取得程式執行過程產生的標準輸出
8604
				$result["content"]["stdout"]=$procForUnixDoaminSocket["content"][0]["content"];
226 liveuser 8605
 
3 liveuser 8606
				#如果是resource
8607
				if(gettype($result["content"]["stdout"])==="resource"){
226 liveuser 8608
 
3 liveuser 8609
					#讀取內容
8610
					$result["content"]["stdout"]=stream_get_contents($result["content"]["stdout"]);
226 liveuser 8611
 
3 liveuser 8612
					}#if end
226 liveuser 8613
 
3 liveuser 8614
				#取得程式執行過程產生的非標準輸出
8615
				$result["content"]["stderr"]=$procForUnixDoaminSocket["content"][0]["error"];
226 liveuser 8616
 
3 liveuser 8617
				#如果是resource
8618
				if(gettype($result["content"]["stderr"])==="resource"){
226 liveuser 8619
 
3 liveuser 8620
					#讀取內容
8621
					$result["content"]["stderr"]=stream_get_contents($result["content"]["stderr"]);
226 liveuser 8622
 
3 liveuser 8623
					}#if end
226 liveuser 8624
 
3 liveuser 8625
				#跳出 while
8626
				break;
226 liveuser 8627
 
3 liveuser 8628
				}#if end
226 liveuser 8629
 
3 liveuser 8630
			#休息1秒
8631
			sleep(1);
226 liveuser 8632
 
3 liveuser 8633
			}#while end
226 liveuser 8634
 
3 liveuser 8635
		#如果socket存在
8636
		if(file_exists($tmpUnixDomainSock)){
226 liveuser 8637
 
3 liveuser 8638
			#移除之
8639
			unlink($tmpUnixDomainSock);
226 liveuser 8640
 
3 liveuser 8641
			}#if end
226 liveuser 8642
 
3 liveuser 8643
		#設置執行正常
8644
		$result["status"]="true";
226 liveuser 8645
 
3 liveuser 8646
		#回傳結果
8647
		return $result;
226 liveuser 8648
 
3 liveuser 8649
		}#function runWithAutoAddMemoryDaemon end
226 liveuser 8650
 
3 liveuser 8651
	/*
8652
	#函式說明:
8653
	#用fwrite寫入到resource,可以指定每個寫入字串的結尾方式.
8654
	#回傳結果:
8655
	#$result["status"],執行是否正常,"true"為正常,"false"為不正常.
8656
	#$result["error"],錯誤訊息陣列.
8657
	#$result["function"],當前執行的函數名稱.
8658
	#$result["argu"],使用的參數.
8659
	#$result["is_writable"],參數stream是否為可寫入的resource,"true"代表可寫入;"false"代表不可寫入.
8660
	#必填參數:
8661
	#$conf["stream"],resource,寫入的目標.
8662
	$conf["stream"]="";
8663
	#$conf["data"],字串陣列,要寫入的內容,每個元素代表每段要寫入的內容.
8664
	$conf["data"]=array();
8665
	#可省略參數:
8666
	#$conf["msgEndType"],字串,訊息結尾要怎麼寫,預設為"none",代表不做處理;"EOL"代表結尾要用PHP_EOL;"HTTP/1.1"代表"data"參數最多為2個,分別為header跟body,結尾方式為兩個PHP_EOL.
8667
	#$conf["msgEndType"]="";
8668
	#$conf["autoClose"],字串,寫入結束後是否要關閉resource,預設為"false"代表保持開着;反之為"true".
8669
	#$conf["autoClose"]="false";
8670
	#參考資料:
8671
	#https://stackoverflow.com/questions/5294305/how-to-check-if-a-php-stream-resource-is-readable-or-writable#5294425
8672
	#https://www.php.net/manual/en/function.stream-get-meta-data.php
8673
	#https://www.php.net/manual/en/function.is-writable.php
8674
	#https://www.php.net/manual/en/function.socket-get-status.php
8675
	#https://www.php.net/manual/en/function.fopen.php
8676
	#備註:
8677
	#無.
8678
	*/
8679
	public static function fwrite(&$conf=array()){
226 liveuser 8680
 
3 liveuser 8681
		#初始化要回傳的結果
8682
		$result=array();
8683
 
8684
		#取得當前執行的函數名稱
8685
		$result["function"]=__FUNCTION__;
226 liveuser 8686
 
3 liveuser 8687
		#取得參數
8688
		$result["argu"]=$conf;
8689
 
8690
		#如果 $conf 不為陣列
8691
		if(gettype($conf)!="array"){
8692
 
8693
			#設置執行失敗
8694
			$result["status"]="false";
8695
 
8696
			#設置執行錯誤訊息
8697
			$result["error"][]="\$conf變數須為陣列形態";
8698
 
8699
			#如果傳入的參數為 null
8700
			if(is_null($conf)){
8701
 
8702
				#設置執行錯誤訊息
8703
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
8704
 
8705
				}#if end
8706
 
8707
			#回傳結果
8708
			return $result;
8709
 
8710
			}#if end
226 liveuser 8711
 
3 liveuser 8712
		#檢查參數
8713
		#函式說明:
8714
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
8715
		#回傳結果:
8716
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
8717
		#$result["error"],執行不正常結束的錯訊息陣列.
8718
		#$result["simpleError"],簡單表示的錯誤訊息.
8719
		#$result["function"],當前執行的函式名稱.
8720
		#$result["argu"],設置給予的參數.
8721
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
8722
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
8723
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
8724
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
8725
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
8726
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
8727
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
8728
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
8729
		#必填參數:
8730
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
8731
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
8732
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
8733
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
8734
		#可省略參數:
8735
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
8736
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("stream","data");
8737
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
8738
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("resource","array");
8739
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
8740
		#$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
8741
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
8742
		$conf["variableCheck::checkArguments"]["canNotBeEmpty"]=array("stream");
8743
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
8744
		#$conf["canBeEmpty"]=array();
8745
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
8746
		$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("msgEndType","autoClose");
8747
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
8748
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("msgEndType","autoClose");
8749
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
8750
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string");
8751
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
8752
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("none","false");
8753
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
8754
		#$conf["disallowAllSkipableVarIsEmpty"]="";
8755
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
8756
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
8757
		#$conf["disallowAllSkipableVarNotExist"],字串,是否不允許每個可省略參數都不存在,預設為"false"代表允許,反之為"true".
8758
		#$conf["disallowAllSkipableVarNotExist"]="";
8759
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
8760
		#$conf["arrayCountEqualCheck"][]=array();
8761
		#參考資料:
8762
		#array_keys=>http://php.net/manual/en/function.array-keys.php
8763
		#備註:
8764
		#無.
8765
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
8766
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 8767
 
3 liveuser 8768
		#如果執行失敗
8769
		if($checkArguments["status"]==="false"){
226 liveuser 8770
 
3 liveuser 8771
			#設置執行失敗
8772
			$result["status"]="false";
226 liveuser 8773
 
3 liveuser 8774
			#設置錯誤資訊
8775
			$result["error"]=$checkArguments;
226 liveuser 8776
 
3 liveuser 8777
			#回傳結果
8778
			return $result;
226 liveuser 8779
 
3 liveuser 8780
			}#if end
226 liveuser 8781
 
3 liveuser 8782
		#如果檢查不通過
8783
		if($checkArguments["passed"]==="false"){
226 liveuser 8784
 
3 liveuser 8785
			#設置執行失敗
8786
			$result["status"]="false";
226 liveuser 8787
 
3 liveuser 8788
			#設置錯誤資訊
8789
			$result["error"]=$checkArguments;
226 liveuser 8790
 
3 liveuser 8791
			#回傳結果
8792
			return $result;
226 liveuser 8793
 
3 liveuser 8794
			}#if end
226 liveuser 8795
 
3 liveuser 8796
		#debug
8797
		#var_dump(__LINE__,$conf["stream"]);
226 liveuser 8798
 
3 liveuser 8799
		/*
226 liveuser 8800
 
3 liveuser 8801
		#函式說明:
8802
		#處理各種錯誤與例外.
8803
		#回傳結果:
8804
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
8805
		#$result["passed"],檢查是否通過,"true"代表通過,"false"代表不通過.
8806
		#$result["argu"],使用的參數.
8807
		#$result["function"],當前執行的函數.
8808
		#$result["error"],涵式錯誤訊息陣列.
8809
		#必填參數:
8810
		#無.
8811
		#可省略參數:
8812
		#$conf["allowCodition"],陣列,滿足什麼條件的錯誤跟例外要繼續執行而不中斷.
8813
		$conf["variableCheck::setErrorHandler"]["allowCodition"]=array("stream_get_meta_data(): supplied resource is not a valid stream resource","[function] => stream_get_meta_data");
8814
		#$conf["debug"],字串,"true"代表遇到不允許繼續執行的例外時要輸出debug資訊;預設為"false";
8815
		$conf["variableCheck::setErrorHandler"]["debug"]="true";
8816
		#$conf["format"],字串,遇到不允許的錯誤時,要用什麼格式呈現,預設為"html",其他選擇有"cmd".
8817
		#$conf["format"]="html";
8818
		#參考資料:
8819
		#無.
8820
		#備註:
8821
		#無.
8822
		$setErrorHandler=variableCheck::setErrorHandler($conf["variableCheck::setErrorHandler"]);
8823
		unset($conf["variableCheck::setErrorHandler"]);
226 liveuser 8824
 
3 liveuser 8825
		#如果有回傳內容
8826
		if($setErrorHandler!==null){
226 liveuser 8827
 
3 liveuser 8828
			#設置執行失敗
8829
			$result["status"]="false";
226 liveuser 8830
 
3 liveuser 8831
			#設置錯誤資訊
8832
			$result["error"]=$setErrorHandler;
226 liveuser 8833
 
3 liveuser 8834
			#回傳結果
8835
			return $result;
226 liveuser 8836
 
3 liveuser 8837
			}#if end
226 liveuser 8838
 
3 liveuser 8839
		*/
226 liveuser 8840
 
3 liveuser 8841
		#取得 stream 狀態
8842
		$stream_get_meta_data=stream_get_meta_data($conf["stream"]);
226 liveuser 8843
 
3 liveuser 8844
		#debug
8845
		#var_dump(__LINE__,$stream_get_meta_data);
226 liveuser 8846
 
3 liveuser 8847
		#如果存在 uri
8848
		if(isset($stream_get_meta_data["uri"])){
226 liveuser 8849
 
3 liveuser 8850
			#取得能否寫入
8851
			$is_writable=is_writable($stream_get_meta_data["uri"]);
226 liveuser 8852
 
3 liveuser 8853
			}#if end
226 liveuser 8854
 
3 liveuser 8855
		#反之用 mode 來判斷
8856
		else if(isset($stream_get_meta_data["mode"])){
226 liveuser 8857
 
3 liveuser 8858
			#函式說明:
8859
			#檢查一個字串裡面是否有多個關鍵字
8860
			#回傳結果:
8861
			#$result["status"],"true"代表執行成功,"false"代表執行失敗.
8862
			#$result["error"],錯誤訊息.
8863
			#$result["function"],當前執行的函數名稱.
8864
			#$result["founded"],是否有找到的關鍵字,"true"代表有找到關鍵字;"false"代表沒有找到關鍵字.
8865
			#$result["foundedKeyWords"],找到的關鍵字.
8866
			#$result["foundedAll"],是否有找到全部的關鍵字,"true"代表有;"false"代表沒有.
8867
			#$result["argu"],使用的參數.
8868
			#必填參數:
8869
			#$conf["keyWords"],字串陣列,想要搜尋的關鍵字.
8870
			$conf["search::findManyKeyWords"]["keyWords"]=array("r+","w","a","x","c");
8871
			#$conf["string"],字串,要被搜尋的字串內容.
8872
			$conf["search::findManyKeyWords"]["string"]=$stream_get_meta_data["mode"];
8873
			#可省略參數:
8874
			#$conf["completeEqual"],字串,是否內容要完全符合,不能多出任何不符合的內容,預設為不需要完全符合.
8875
			#$conf["completeEqual"]="true";
8876
			#參考資料:
8877
			#無.
8878
			#備註:
8879
			#無.
8880
			$findManyKeyWords=search::findManyKeyWords($conf["search::findManyKeyWords"]);
8881
			unset($conf["search::findManyKeyWords"]);
226 liveuser 8882
 
3 liveuser 8883
			#如果執行失敗
8884
			if($findManyKeyWords["status"]==="false"){
226 liveuser 8885
 
3 liveuser 8886
				#設置執行失敗
8887
				$result["status"]="false";
226 liveuser 8888
 
3 liveuser 8889
				#設置錯誤資訊
8890
				$result["error"]=$findManyKeyWords;
226 liveuser 8891
 
3 liveuser 8892
				#回傳結果
8893
				return $result;
226 liveuser 8894
 
3 liveuser 8895
				}#if end
226 liveuser 8896
 
3 liveuser 8897
			#如果模式含有寫入的屬性
8898
			if($findManyKeyWords["founded"]==="true"){
226 liveuser 8899
 
3 liveuser 8900
				#設置可以寫入
8901
				$is_writable=true;
226 liveuser 8902
 
3 liveuser 8903
				}#if end
226 liveuser 8904
 
8905
			}#else
8906
 
3 liveuser 8907
		#反之需要其他判斷方式
8908
		else{
226 liveuser 8909
 
3 liveuser 8910
			#設置警示訊息
8911
			$result["warning"][]="無法判別 resource 能否寫入";
226 liveuser 8912
 
3 liveuser 8913
			#設置警示訊息
8914
			$result["warning"][]=$stream_get_meta_data;
226 liveuser 8915
 
3 liveuser 8916
			#直接放行
8917
			$is_writable=true;
226 liveuser 8918
 
3 liveuser 8919
			}#else end
226 liveuser 8920
 
3 liveuser 8921
		#如果不可寫入
8922
		if($is_writable!==true){
226 liveuser 8923
 
3 liveuser 8924
			#設置執行失敗
8925
			$result["status"]="false";
226 liveuser 8926
 
3 liveuser 8927
			#設置錯誤資訊
8928
			$result["error"][]="resource 無法寫入";
226 liveuser 8929
 
3 liveuser 8930
			#設置錯誤資訊
8931
			$result["error"][]=$stream_get_meta_data;
226 liveuser 8932
 
3 liveuser 8933
			#設置resource不可寫入
8934
			$result["is_writable"]="false";
226 liveuser 8935
 
3 liveuser 8936
			#回傳結果
8937
			return $result;
226 liveuser 8938
 
3 liveuser 8939
			}#if end
226 liveuser 8940
 
3 liveuser 8941
		#設置resource可寫入
8942
		$result["is_writable"]="true";
226 liveuser 8943
 
3 liveuser 8944
		#判斷訊息結尾的格式
8945
		switch($conf["msgEndType"]){
226 liveuser 8946
 
3 liveuser 8947
			#如果是 "none"
8948
			case "none":
226 liveuser 8949
 
3 liveuser 8950
				#要寫入的每個字串
8951
				foreach($conf["data"] as $msg){
226 liveuser 8952
 
3 liveuser 8953
					#寫入內容
8954
					fwrite($conf["stream"],$msg);
226 liveuser 8955
 
3 liveuser 8956
					}#foreach end
226 liveuser 8957
 
3 liveuser 8958
				break;
226 liveuser 8959
 
3 liveuser 8960
			#如果是 "EOL"
8961
			case "EOL":
226 liveuser 8962
 
3 liveuser 8963
				#要寫入的每個字串
8964
				foreach($conf["data"] as $msg){
226 liveuser 8965
 
3 liveuser 8966
					#寫入內容,用PHP_EOL結尾.
8967
					fwrite($conf["stream"],$msg.PHP_EOL);
226 liveuser 8968
 
3 liveuser 8969
					}#foreach end
226 liveuser 8970
 
3 liveuser 8971
				break;
226 liveuser 8972
 
3 liveuser 8973
			#如果是 "HTTP/1.1"
8974
			case "HTTP/1.1":
226 liveuser 8975
 
3 liveuser 8976
				#如果要寫入的字串大於2段
8977
				if(count($conf["data"])>2){
226 liveuser 8978
 
3 liveuser 8979
					#設置執行失敗
8980
					$result["status"]="false";
226 liveuser 8981
 
3 liveuser 8982
					#設置錯誤資訊
8983
					$result["error"][]="當參數 msgEndType 為 ".$conf["msgEndType"]." 時,參數 data 的數量不得超過2個.";
226 liveuser 8984
 
3 liveuser 8985
					#設置resource不可寫入
8986
					$result["is_writable"]="false";
226 liveuser 8987
 
3 liveuser 8988
					#回傳結果
8989
					return $result;
226 liveuser 8990
 
3 liveuser 8991
					}#if end
226 liveuser 8992
 
3 liveuser 8993
				#要寫入的每個字串
8994
				foreach($conf["data"] as $msg){
226 liveuser 8995
 
3 liveuser 8996
					#寫入 header/body
8997
					fwrite($conf["stream"],$msg.PHP_EOL.PHP_EOL);
226 liveuser 8998
 
3 liveuser 8999
					}#foreach end
226 liveuser 9000
 
3 liveuser 9001
				#跳出switch
9002
				break;
226 liveuser 9003
 
3 liveuser 9004
			#非預期的類型
9005
			default:
226 liveuser 9006
 
3 liveuser 9007
				#設置執行失敗
9008
				$result["status"]="false";
226 liveuser 9009
 
3 liveuser 9010
				#設置錯誤資訊
9011
				$result["error"][]="unsupported msgEndType ".$conf["msgEndType"];
226 liveuser 9012
 
3 liveuser 9013
				#回傳結果
9014
				return $result;
226 liveuser 9015
 
3 liveuser 9016
			}#switch end
226 liveuser 9017
 
3 liveuser 9018
		#若要關閉resource
9019
		if($conf["autoClose"]==="true"){
226 liveuser 9020
 
3 liveuser 9021
			#關閉resouce.
9022
			fclose($conf["stream"]);
226 liveuser 9023
 
3 liveuser 9024
			}#if end
226 liveuser 9025
 
3 liveuser 9026
		#設置執行正常
9027
		$result["status"]="true";
226 liveuser 9028
 
3 liveuser 9029
		#回傳結果
9030
		return $result;
226 liveuser 9031
 
3 liveuser 9032
		}#function fwrite end
226 liveuser 9033
 
3 liveuser 9034
	}#class sock end
9035
 
9036
?>