Subversion Repositories php-qbpwcf

Rev

Rev 371 | 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";
396 liveuser 128
	#$conf["addOnProcessFunc"],字串陣列,增加用於處理 json request 的函式名稱,給予的參數為array("id"=>用於識別用戶的id,"request"=>收到的json訊息,"sock"=>用戶的socket,"clientCache"=>給予所有用戶的cache),若收到的不是json而是"quit"則代表用戶要結束連線;若收到的是$shutdownStr則代表要結束本函式.回傳的內容必須為陣列,例如 $res["continue"]="true"代表要繼續執行下一個addOnProcessFunc;"false"代表代表到此為止. $res["content"]="replaced content";代表要將收到的訊息取代成"replaced content". $res["cache"]="server side client cache";代表要更新 #用收到 cache 元素內容來更新 server side 的 cache,亦即 $result["serverCache"]["clientSide"]["addOnProcessFunc"][$addOnProcessFunc]=$res["cache"]; 最少要有回傳 $res["status"]數值"true"代表執行正常;"false"代表執行不正常.
3 liveuser 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
371 liveuser 645
 
646
				#改變shutdownStr檔案的擁有者
647
				#函式說明:
648
				#使用 linux 的 chown 指令來修改目標檔案或目錄的擁有者跟群組擁有者資訊.
649
				#回傳結果:
650
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
651
				#$result["error"],錯誤訊息.
652
				#$result["function"],當前執行的函式名稱.
653
				#$result["cmd"],執行的指令.
654
				#$result["content"],執行的結果陣列,如果參數 "recursive" 跟 "excludeSelf" 都有設定的話,就會回傳該結果.
655
				#必填參數:
656
				#$conf["owner"],字串,要變哪個使用者擁有.
657
				$conf["cmd::chown"]["owner"]=explode(".",$conf["changeOwner"])[0];
658
				#$conf["target"],字串,需要變更擁有者、 群組的目標.
659
				$conf["cmd::chown"]["target"]=$conf["shutdownStrAddr"];
660
				#可省略參數:
661
				#$conf["group"],字串,要變成什麼群組擁有,預設跟"owner"一樣.
662
				$conf["cmd::chown"]["group"]=explode(".",$conf["changeOwner"])[1];
663
				#$conf["recursive"],字串,"true"代表目標目錄底下的內容都要套用,預設為"false".
664
				#$conf["recursive"]="true";
665
				#$conf["excludeSelf"],字串,預設為"false"代表不處理;若為"true"則會排除目標自己(資料夾).
666
				#$conf["excludeSelf"]="true";
667
				#參考資料:
668
				#無.
669
				#備註:
670
				#無.
671
				$chown=cmd::chown($conf["cmd::chown"]);
672
				unset($conf["cmd::chown"]);
226 liveuser 673
 
371 liveuser 674
				#如果改變檔案擁有者限失敗
675
				if($chown===false){
676
 
677
					#設置執行失敗
678
					$result["status"]="false";
679
 
680
					#設置錯誤訊息
681
					$result["error"][]="改變 ".$conf["sock"]." 檔案的擁有資訊為 ".explode(".",$conf["changeOwner"])[0]." 失敗,通常只有系統帳戶才能變更之.";
682
 
683
					#印出錯誤訊息
684
					echo print_r($result,true);
685
 
686
					#回傳結果
687
					return $result;
688
 
689
					}#if end
690
 
3 liveuser 691
				}#if end
226 liveuser 692
 
3 liveuser 693
			#如果有設置 $conf["changePermission"]
694
			if(isset($conf["changePermission"])){
226 liveuser 695
 
3 liveuser 696
				#函式說明:
697
				#使用 linux 的 chmod 指令來修改目標檔案或目錄的權限.
698
				#回傳結果:
699
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
700
				#$result["error"],錯誤訊息.
701
				#$result["function"],當前執行的函式名稱.
702
				#$result["cmd"],執行的指令.
703
				#$result["content"],執行的結果陣列,如果參數 "recursive" 跟 "excludeSelf" 都有設定的話,就會回傳該結果.
704
				#必填參數:
705
				#$conf["mode"],字串,要變成什麼權限.
706
				$conf["cmd::chmod"]["mode"]=$conf["changePermission"];
707
				#$conf["target"],字串,需要變更權限的目標.
708
				$conf["cmd::chmod"]["target"]=$conf["sock"];
709
				#可省略參數:
710
				#$conf["recursive"],字串,"true"代表目標目錄底下的內容都要套用,預設為"false".
711
				#$conf["recursive"]="true";
712
				#$conf["excludeSelf"],字串,預設為"false"代表不處理;若為"true"則會排除目標自己(資料夾).
713
				#$conf["excludeSelf"]="true";
714
				#參考資料:
715
				#無.
716
				#備註:
717
				#無.
718
				$chmod=cmd::chmod($conf["cmd::chmod"]);
719
				unset($conf["cmd::chmod"]);
226 liveuser 720
 
3 liveuser 721
				#如果變更檔案權限失敗
722
				if($chmod["status"]==="false"){
226 liveuser 723
 
3 liveuser 724
					#設置執行失敗
725
					$result["status"]="false";
226 liveuser 726
 
3 liveuser 727
					#設置錯誤訊息
728
					$result["error"]=$chmod;
226 liveuser 729
 
3 liveuser 730
					#印出錯誤訊息
731
					echo print_r($result,true);
226 liveuser 732
 
3 liveuser 733
					#回傳結果
226 liveuser 734
					return $result;
735
 
3 liveuser 736
					}#if end
371 liveuser 737
 
738
				#改變 shutdownStr 的權限
739
				#函式說明:
740
				#使用 linux 的 chmod 指令來修改目標檔案或目錄的權限.
741
				#回傳結果:
742
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
743
				#$result["error"],錯誤訊息.
744
				#$result["function"],當前執行的函式名稱.
745
				#$result["cmd"],執行的指令.
746
				#$result["content"],執行的結果陣列,如果參數 "recursive" 跟 "excludeSelf" 都有設定的話,就會回傳該結果.
747
				#必填參數:
748
				#$conf["mode"],字串,要變成什麼權限.
749
				$conf["cmd::chmod"]["mode"]=$conf["changePermission"];
750
				#$conf["target"],字串,需要變更權限的目標.
751
				$conf["cmd::chmod"]["target"]=$conf["shutdownStrAddr"];
752
				#可省略參數:
753
				#$conf["recursive"],字串,"true"代表目標目錄底下的內容都要套用,預設為"false".
754
				#$conf["recursive"]="true";
755
				#$conf["excludeSelf"],字串,預設為"false"代表不處理;若為"true"則會排除目標自己(資料夾).
756
				#$conf["excludeSelf"]="true";
757
				#參考資料:
758
				#無.
759
				#備註:
760
				#無.
761
				$chmod=cmd::chmod($conf["cmd::chmod"]);
762
				unset($conf["cmd::chmod"]);
226 liveuser 763
 
371 liveuser 764
				#如果變更檔案權限失敗
765
				if($chmod["status"]==="false"){
766
 
767
					#設置執行失敗
768
					$result["status"]="false";
769
 
770
					#設置錯誤訊息
771
					$result["error"]=$chmod;
772
 
773
					#印出錯誤訊息
774
					echo print_r($result,true);
775
 
776
					#回傳結果
777
					return $result;
778
 
779
					}#if end
780
 
3 liveuser 781
				}#if end
226 liveuser 782
 
3 liveuser 783
			#初始化儲存 server 資訊的變數,預設數值為 $result 自己
784
			$result["serverCache"]=&$result;
226 liveuser 785
 
3 liveuser 786
			#初始化方便識別 server 資訊的變數
787
			$serverCache=&$result["serverCache"];
226 liveuser 788
 
3 liveuser 789
			#初始化 server site cache
790
			$serverCache["serverSide"]=array();
226 liveuser 791
 
792
			#初始化 儲存 透過 proc 執行的子程序
3 liveuser 793
			$serverCache["serverSide"]["procs"]=array();
226 liveuser 794
 
3 liveuser 795
			#初始化 client site cache
796
			$serverCache["clientSide"]=array();
226 liveuser 797
 
3 liveuser 798
			#初始化方便識別 client site cache 資訊的變數
799
			$clientCache=&$serverCache["clientSide"];
226 liveuser 800
 
3 liveuser 801
			#將建立的 listen sock 放到 $master 陣列
802
			$master[] = $socket;
226 liveuser 803
 
3 liveuser 804
			#永久監聽, 不 timeout
805
			while(true){
226 liveuser 806
 
3 liveuser 807
				#debug
808
				#var_dump(__LINE__,gmdate("Y-m-d H:i:s",time()));
226 liveuser 809
 
3 liveuser 810
				#設置給 stream_select 監控的sock變數
811
				$read = $write = $except = $master;
226 liveuser 812
 
3 liveuser 813
				#等待5秒,看有沒有連線進來讀寫資料
814
				$mod_fd = stream_select($read, $write, $except, 5);
226 liveuser 815
 
3 liveuser 816
				#如果執行失敗
817
				if($mod_fd===FALSE){
226 liveuser 818
 
3 liveuser 819
					#提示錯誤訊息
820
					echo "Stream_select failed at ".gmdate("Y-m-d H:i:s",time());
226 liveuser 821
 
3 liveuser 822
					#結束監聽
823
					exit(1);
226 liveuser 824
 
3 liveuser 825
					}#if end
226 liveuser 826
 
3 liveuser 827
				#如果沒有訊息,代表閒置時可以做的事情
828
				else if($mod_fd===0){
226 liveuser 829
 
3 liveuser 830
					#提示idle
831
					echo ".";
226 liveuser 832
 
3 liveuser 833
					#清除 session timeout 的連線 - start
226 liveuser 834
 
3 liveuser 835
					#初始化要給 server 的回應
836
					$res=array();
226 liveuser 837
 
3 liveuser 838
					#初始化計數移除幾個用戶
839
					$res["delIdleConCount"]=0;
226 liveuser 840
 
3 liveuser 841
					#針對每個 $client
842
					foreach($client as $index=>$uncheckClient){
226 liveuser 843
 
3 liveuser 844
						#如果上次的要求時間為300秒前.
845
						if(time()-(int)$uncheckClient["last_req_time"]>$conf["sessionTimeout"]){
226 liveuser 846
 
3 liveuser 847
							#移除該用戶變數
848
							unset($client["$index"]);
226 liveuser 849
 
3 liveuser 850
							#計數移除了幾個用戶
851
							$res["delIdleConCount"]++;
226 liveuser 852
 
3 liveuser 853
							}#if end
226 liveuser 854
 
3 liveuser 855
						}#foreach end
226 liveuser 856
 
3 liveuser 857
					#設置執行正常
858
					$res["status"]="true";
226 liveuser 859
 
3 liveuser 860
					#如果有移除閒置的連線
861
					if($res["delIdleConCount"]>0){
226 liveuser 862
 
3 liveuser 863
						#印出給 server 的訊息
864
						echo PHP_EOL."response:".PHP_EOL.print_r($res,true);
226 liveuser 865
 
3 liveuser 866
						}#if end
226 liveuser 867
 
3 liveuser 868
					#清除 session timeout 的連線 - end
226 liveuser 869
 
3 liveuser 870
					#清除 太久沒有 被查詢的 proc(子程序) - start
226 liveuser 871
 
3 liveuser 872
					#初始化要給 server 的回應
873
					$res=array();
226 liveuser 874
 
3 liveuser 875
					#初始化計數移除幾個沒被關注的子程序
876
					$res["delIdleProcCount"]=0;
226 liveuser 877
 
3 liveuser 878
					#針對每個 子程序
879
					foreach($serverCache["serverSide"]["procs"] as $procIndex=>$procInfo){
226 liveuser 880
 
3 liveuser 881
						#debug
882
						#var_dump(__FUNCTION__,__LINE__,$procInfo);
226 liveuser 883
 
3 liveuser 884
						#如果上次的要求時間為300秒前.
885
						if(time()-(int)$procInfo["latestAccessTime"]>$conf["sessionTimeout"]){
226 liveuser 886
 
3 liveuser 887
							#若運行中
888
							if($procInfo["content"][0]["statusCode"]==="?"){
226 liveuser 889
 
3 liveuser 890
								#更新資訊
891
								#函式說明:
892
								#更新透過proc執行的多程序資訊.
893
								#回傳結果:
894
								#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
895
								#$reuslt["error"],執行不正常結束的錯訊息陣列.
896
								#$result["function"],當前執行的函式名稱.
897
								#$result["argu"],使用的參數.
898
								#$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"代表程序的資訊.
899
								#必填參數:
900
								#$conf["procs"],陣列,運行self::proc後回傳的content.
901
								$conf["threads::proc_update"]["procs"]=$procInfo["content"];
902
								#可省略參數:
903
								#無.
904
								#參考資料:
905
								#無.
906
								#備註:
907
								#無.
908
								$proc_update=threads::proc_update($conf["threads::proc_update"]);
909
								unset($conf["threads::proc_update"]);
226 liveuser 910
 
3 liveuser 911
								#若執行不正常
912
								if($proc_update["status"]==="false"){
226 liveuser 913
 
3 liveuser 914
									#設置執行失敗
915
									$result["status"]="false";
226 liveuser 916
 
3 liveuser 917
									#設置錯誤訊息
918
									$result["error"]=$proc_update;
226 liveuser 919
 
3 liveuser 920
									#印出錯誤訊息
921
									echo print_r($result,true);
226 liveuser 922
 
3 liveuser 923
									#回傳結果
226 liveuser 924
									return $result;
925
 
3 liveuser 926
									}#if end
226 liveuser 927
 
3 liveuser 928
								#更新程序狀態
929
								$procInfo["content"]=$proc_update["content"][0];
226 liveuser 930
 
3 liveuser 931
								#更新會後存取時間
226 liveuser 932
 
3 liveuser 933
								#若已經執行結束
934
								if($procInfo["content"]["statusCode"]!=="?"){
226 liveuser 935
 
3 liveuser 936
									#換下一個
937
									continue;
226 liveuser 938
 
3 liveuser 939
									}#if end
226 liveuser 940
 
3 liveuser 941
								#執行到這代表一樣再運行中
226 liveuser 942
 
3 liveuser 943
								#終止程序
944
								proc_close($procInfo["content"]["process"]);
226 liveuser 945
 
3 liveuser 946
								#移除該用戶變數
226 liveuser 947
								unset($serverCache["serverSide"]["procs"][$procIndex]);
948
 
3 liveuser 949
								#換下一個
950
								continue;
226 liveuser 951
 
3 liveuser 952
								}#if end
226 liveuser 953
 
3 liveuser 954
							#反之為已運行結束
226 liveuser 955
 
3 liveuser 956
							#移除該用戶變數
957
							unset($serverCache["serverSide"]["procs"][$procIndex]);
226 liveuser 958
 
3 liveuser 959
							#計數移除幾個沒被關注的子程序
960
							$res["delIdleProcCount"]++;
226 liveuser 961
 
3 liveuser 962
							}#if end
226 liveuser 963
 
3 liveuser 964
						}#foreach end
226 liveuser 965
 
3 liveuser 966
					#設置執行正常
967
					$res["status"]="true";
226 liveuser 968
 
3 liveuser 969
					#如果有移除沒被關注的子程序
970
					if($res["delIdleProcCount"]>0){
226 liveuser 971
 
3 liveuser 972
						#印出給 server 的訊息
973
						echo PHP_EOL."response:".PHP_EOL.print_r($res,true);
226 liveuser 974
 
3 liveuser 975
						}#if end
226 liveuser 976
 
3 liveuser 977
					#清除 太久沒有 被查詢的 proc(子程序) - end
226 liveuser 978
 
3 liveuser 979
					#如果有設置 idle 要執行的函式
980
					if(isset($conf["funcToRunWhenIdle"])){
226 liveuser 981
 
3 liveuser 982
						#提示有idle要執行的函式
983
						echo "response:".PHP_EOL."There is funcToRunWhenIdle param set.".PHP_EOL;
226 liveuser 984
 
3 liveuser 985
						#針對每個 funcToRunWhenIdle
986
						foreach($conf["funcToRunWhenIdle"] as $index => $funcToRunWhenIdle){
226 liveuser 987
 
3 liveuser 988
							#提示正要執行的函式
989
							echo "response:".PHP_EOL."Executeing ".$funcToRunWhenIdle."...".PHP_EOL;
226 liveuser 990
 
3 liveuser 991
							#初始化要給 funcToRunWhenIdle 函式的參數
992
							$parasmForFuncToRunWhenIdle=array();
226 liveuser 993
 
3 liveuser 994
							#取得所有用戶
995
							$parasmForFuncToRunWhenIdle["client"]=&$client;
226 liveuser 996
 
3 liveuser 997
							#如果有設置 infoToFunction
998
							if(isset($conf["infoToFunction"])){
226 liveuser 999
 
3 liveuser 1000
								#設置info
1001
								$parasmForFuncToRunWhenIdle["info"]=&$conf["infoToFunction"];
226 liveuser 1002
 
3 liveuser 1003
								}#if end
226 liveuser 1004
 
3 liveuser 1005
							#debug
1006
							#var_dump(__FILE__,__LINE__,$serverCache["serverSide"]);
226 liveuser 1007
 
3 liveuser 1008
							#如果屬於該 $funcToRunWhenIdle 的 server side cache 不存在
1009
							if(!isset($serverCache["serverSide"]["funcToRunWhenIdle"][$funcToRunWhenIdle])){
226 liveuser 1010
 
3 liveuser 1011
								#初始化為空陣列
1012
								$serverCache["serverSide"]["funcToRunWhenIdle"][$funcToRunWhenIdle]=array();
226 liveuser 1013
 
3 liveuser 1014
								}#if end
226 liveuser 1015
 
3 liveuser 1016
							#設置給予 $funcToRunWhenIdle 函式的 serverCache 參數
1017
							$parasmForFuncToRunWhenIdle["serverCache"]=&$serverCache;
226 liveuser 1018
 
3 liveuser 1019
							#儲存 $funcToRunWhenIdle 函式 的運行結束的時間點到 server side 的 cache
1020
							$serverCache["serverSide"]["funcToRunWhenIdle"][$funcToRunWhenIdle]["lastRunTime"]=time::unix()["content"];
226 liveuser 1021
 
3 liveuser 1022
							#如果有要指定給 $funcToRunWhenIdle 的參數
1023
							if(isset($conf["paramsForFuncToRunWhenIdle"][$index])){
226 liveuser 1024
 
3 liveuser 1025
								#針對每個 要給予 funcToRunWhenIdle 的參數
1026
								foreach($conf["paramsForFuncToRunWhenIdle"][$index] as $arguKeyForFuncToRunWhenIdle => $arguValueForFuncToRunWhenIdle){
226 liveuser 1027
 
3 liveuser 1028
									#設置給予 funcToRunWhenIdle 的參數
1029
									$parasmForFuncToRunWhenIdle[$arguKeyForFuncToRunWhenIdle]=$arguValueForFuncToRunWhenIdle;
226 liveuser 1030
 
3 liveuser 1031
									#debug
1032
									#var_dump(__LINE__,$parasmForFuncToRunWhenIdle);
226 liveuser 1033
 
3 liveuser 1034
									}#foreach end
226 liveuser 1035
 
3 liveuser 1036
								}#if end
226 liveuser 1037
 
3 liveuser 1038
							/* debug
226 liveuser 1039
 
3 liveuser 1040
							#函式說明:
1041
							#撰寫log
1042
							#回傳結果:
1043
							#$result["status"],狀態,"true"或"false".
1044
							#$result["error"],錯誤訊息陣列.
1045
							#$result["function"],當前函式的名稱.
1046
							#$result["argu"],使用的參數.
1047
							#$result["content"],要寫入log的內容字串.
1048
							#必填參數:
1049
							#$conf["path"],字串,log檔案的路徑與名稱.
1050
							$conf["logs::record"]["path"]=$conf["sock"].".log";
1051
							#$conf["content"],any,要寫的內容,若內容不為字串則會用var_dump的格式寫入.
1052
							$conf["logs::record"]["content"]=$parasmForFuncToRunWhenIdle;
1053
							#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
1054
							$conf["logs::record"]["fileArgu"]=__FILE__;
1055
							#可省略參數:
1056
							#$conf["rewrite"],預設為"false",接續寫入;反之"true"代表重新寫入.
1057
							#$conf["rewrite"]="false";
1058
							#$conf["returnOnly"],預設為"false",會寫入到log檔案.若為"true"則不會寫入log.
1059
							#$conf["returnOnly"]="true";
1060
							#參考資料:
1061
							#無.
1062
							#備註:
1063
							#無.
1064
							$record=logs::record($conf["logs::record"]);
1065
							unset($conf["logs::record"]);
226 liveuser 1066
 
3 liveuser 1067
							#如果執行失敗
1068
							if($record["status"]==="false"){
226 liveuser 1069
 
3 liveuser 1070
								#設置執行錯誤識別
1071
								$result["status"]="false";
226 liveuser 1072
 
3 liveuser 1073
								#設置執行錯誤
1074
								$result["error"]=$record;
226 liveuser 1075
 
3 liveuser 1076
								#回傳結果
1077
								return $result;
226 liveuser 1078
 
3 liveuser 1079
								}#if end
226 liveuser 1080
 
3 liveuser 1081
							*/
226 liveuser 1082
 
3 liveuser 1083
							#debug
1084
							#var_dump(__LINE__,$parasmForFuncToRunWhenIdle);
226 liveuser 1085
 
3 liveuser 1086
							#debug
1087
							#var_dump(__LINE__,"\$call_user_func=".$funcToRunWhenIdle."(\$parasmForFuncToRunWhenIdle);");
226 liveuser 1088
 
3 liveuser 1089
							#呼叫 $funcToRunWhenIdle 函式,參數用 $client[$request->id], 取得執行後的回應.
1090
							eval("\$call_user_func=".$funcToRunWhenIdle."(\$parasmForFuncToRunWhenIdle);");
226 liveuser 1091
 
3 liveuser 1092
							/* debug
226 liveuser 1093
 
3 liveuser 1094
							#函式說明:
1095
							#撰寫log
1096
							#回傳結果:
1097
							#$result["status"],狀態,"true"或"false".
1098
							#$result["error"],錯誤訊息陣列.
1099
							#$result["function"],當前函式的名稱.
1100
							#$result["argu"],使用的參數.
1101
							#$result["content"],要寫入log的內容字串.
1102
							#必填參數:
1103
							#$conf["path"],字串,log檔案的路徑與名稱.
1104
							$conf["logs::record"]["path"]=$conf["sock"].".log";
1105
							#$conf["content"],any,要寫的內容,若內容不為字串則會用var_dump的格式寫入.
1106
							$conf["logs::record"]["content"]=$call_user_func;
1107
							#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
1108
							$conf["logs::record"]["fileArgu"]=__FILE__;
1109
							#可省略參數:
1110
							#$conf["rewrite"],預設為"false",接續寫入;反之"true"代表重新寫入.
1111
							#$conf["rewrite"]="false";
1112
							#$conf["returnOnly"],預設為"false",會寫入到log檔案.若為"true"則不會寫入log.
1113
							#$conf["returnOnly"]="true";
1114
							#參考資料:
1115
							#無.
1116
							#備註:
1117
							#無.
1118
							$record=logs::record($conf["logs::record"]);
1119
							unset($conf["logs::record"]);
226 liveuser 1120
 
3 liveuser 1121
							#如果執行失敗
1122
							if($record["status"]==="false"){
226 liveuser 1123
 
3 liveuser 1124
								#設置執行錯誤識別
1125
								$result["status"]="false";
226 liveuser 1126
 
3 liveuser 1127
								#設置執行錯誤
1128
								$result["error"]=$record;
226 liveuser 1129
 
3 liveuser 1130
								#回傳結果
1131
								return $result;
226 liveuser 1132
 
3 liveuser 1133
								}#if end
226 liveuser 1134
 
1135
							*/
1136
 
3 liveuser 1137
							#儲存 $funcToRunWhenIdle 函式 的運行結果資訊到 server side 的 cache
1138
							$serverCache["serverSide"]["funcToRunWhenIdle"][$funcToRunWhenIdle]["result"]=&$call_user_func;
226 liveuser 1139
 
3 liveuser 1140
							#儲存 $funcToRunWhenIdle 函式 的運行結束的時間點到 server side 的 cache
1141
							$serverCache["serverSide"]["funcToRunWhenIdle"][$funcToRunWhenIdle]["lastEndTime"]=time::unix()["content"];
226 liveuser 1142
 
3 liveuser 1143
							#debug
1144
							#var_dump(__FILE__,__LINE__,$serverCache);
226 liveuser 1145
 
3 liveuser 1146
							#如果 運行 $funcToRunWhenIdle 後回傳的內容含有 key 為 status 的元素
1147
							if(isset($call_user_func["status"])){
226 liveuser 1148
 
3 liveuser 1149
								#如果其數值為 "false"
1150
								if($call_user_func["status"]==="false"){
226 liveuser 1151
 
3 liveuser 1152
									#印出訊息,結束執行
1153
									var_dump(__FILE__,__LINE__,$call_user_func);exit;
226 liveuser 1154
 
3 liveuser 1155
									}#if end
226 liveuser 1156
 
3 liveuser 1157
								}#if end
226 liveuser 1158
 
3 liveuser 1159
							#如果有訊息要提示
1160
							if(isset($call_user_func["content"])){
226 liveuser 1161
 
3 liveuser 1162
								#印出提示訊息
1163
								var_dump($call_user_func["content"]);
226 liveuser 1164
 
3 liveuser 1165
								}#if end
226 liveuser 1166
 
3 liveuser 1167
							#如果 運行 $funcToRunWhenIdle 後回傳的內容含有 key 為 cache 的元素
1168
							if(isset($call_user_func["cache"])){
226 liveuser 1169
 
3 liveuser 1170
								#debug
1171
								#var_dump(__FILE__,__LINE__,$call_user_func["cache"]);
226 liveuser 1172
 
3 liveuser 1173
								#儲存到 server side 的 cache
1174
								$serverCache["serverSide"]["funcToRunWhenIdle"][$funcToRunWhenIdle]["cache"]=&$call_user_func["cache"];
226 liveuser 1175
 
3 liveuser 1176
								/*
1177
								#debug
1178
								#函式說明:
1179
								#撰寫log
1180
								#回傳結果:
1181
								#$result["status"],狀態,"true"或"false".
1182
								#$result["error"],錯誤訊息陣列.
1183
								#$result["function"],當前函式的名稱.
1184
								#$result["argu"],使用的參數.
1185
								#$result["content"],要寫入log的內容字串.
1186
								#必填參數:
1187
								#$conf["path"],字串,log檔案的路徑與名稱.
1188
								$conf["logs::record"]["path"]=$conf["sock"].".log";
1189
								#$conf["content"],any,要寫的內容,若內容不為字串則會用var_dump的格式寫入.
1190
								$conf["logs::record"]["content"]=$call_user_func;
1191
								#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
1192
								$conf["logs::record"]["fileArgu"]=__FILE__;
1193
								#可省略參數:
1194
								#$conf["rewrite"],預設為"false",接續寫入;反之"true"代表重新寫入.
1195
								#$conf["rewrite"]="false";
1196
								#$conf["returnOnly"],預設為"false",會寫入到log檔案.若為"true"則不會寫入log.
1197
								#$conf["returnOnly"]="true";
1198
								#參考資料:
1199
								#無.
1200
								#備註:
1201
								#無.
1202
								$record=logs::record($conf["logs::record"]);
1203
								unset($conf["logs::record"]);
226 liveuser 1204
 
3 liveuser 1205
								#如果執行失敗
1206
								if($record["status"]==="false"){
226 liveuser 1207
 
3 liveuser 1208
									#設置執行錯誤識別
1209
									$result["status"]="false";
226 liveuser 1210
 
3 liveuser 1211
									#設置執行錯誤
1212
									$result["error"]=$record;
226 liveuser 1213
 
3 liveuser 1214
									#回傳結果
1215
									return $result;
226 liveuser 1216
 
3 liveuser 1217
									}#if end
1218
								*/
226 liveuser 1219
 
3 liveuser 1220
								}#if end
226 liveuser 1221
 
3 liveuser 1222
							#如果回傳結果含有 exit 元素
1223
							if(isset($call_user_func["exit"])){
226 liveuser 1224
 
3 liveuser 1225
								/*
1226
								#debug
1227
								#函式說明:
1228
								#撰寫log
1229
								#回傳結果:
1230
								#$result["status"],狀態,"true"或"false".
1231
								#$result["error"],錯誤訊息陣列.
1232
								#$result["function"],當前函式的名稱.
1233
								#$result["argu"],使用的參數.
1234
								#$result["content"],要寫入log的內容字串.
1235
								#必填參數:
1236
								#$conf["path"],字串,log檔案的路徑與名稱.
1237
								$conf["logs::record"]["path"]=$conf["sock"].".log";
1238
								#$conf["content"],any,要寫的內容,若內容不為字串則會用var_dump的格式寫入.
1239
								$conf["logs::record"]["content"]=$call_user_func["exit"];
1240
								#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
1241
								$conf["logs::record"]["fileArgu"]=__FILE__;
1242
								#可省略參數:
1243
								#$conf["rewrite"],預設為"false",接續寫入;反之"true"代表重新寫入.
1244
								#$conf["rewrite"]="false";
1245
								#$conf["returnOnly"],預設為"false",會寫入到log檔案.若為"true"則不會寫入log.
1246
								#$conf["returnOnly"]="true";
1247
								#參考資料:
1248
								#無.
1249
								#備註:
1250
								#無.
1251
								$record=logs::record($conf["logs::record"]);
1252
								unset($conf["logs::record"]);
226 liveuser 1253
 
3 liveuser 1254
								#如果執行失敗
1255
								if($record["status"]==="false"){
226 liveuser 1256
 
3 liveuser 1257
									#設置執行錯誤識別
1258
									$result["status"]="false";
226 liveuser 1259
 
3 liveuser 1260
									#設置執行錯誤
1261
									$result["error"]=$record;
226 liveuser 1262
 
3 liveuser 1263
									#回傳結果
1264
									return $result;
226 liveuser 1265
 
3 liveuser 1266
									}#if end
1267
								*/
226 liveuser 1268
 
3 liveuser 1269
								#如果是要結束daemon執行
1270
								if($call_user_func["exit"]==="true"){
226 liveuser 1271
 
3 liveuser 1272
									#設置daemon結束的原因
1273
									$call_user_func["exit"]="exit by funcToRunWhenIdle(".$funcToRunWhenIdle.")";
226 liveuser 1274
 
3 liveuser 1275
									#設置執行正常
1276
									$result["status"]="true";
226 liveuser 1277
 
3 liveuser 1278
									#結束執行
1279
									return $result;
226 liveuser 1280
 
3 liveuser 1281
									}#if end
226 liveuser 1282
 
3 liveuser 1283
								}#if end
226 liveuser 1284
 
3 liveuser 1285
							#提示正要執行的函式
1286
							echo "response:".PHP_EOL.$funcToRunWhenIdle." Executed.".PHP_EOL;
226 liveuser 1287
 
3 liveuser 1288
							}#foreach end
226 liveuser 1289
 
3 liveuser 1290
						}#if end
226 liveuser 1291
 
3 liveuser 1292
					#等待下個要求
1293
					continue;
226 liveuser 1294
 
3 liveuser 1295
					}#if end
226 liveuser 1296
 
3 liveuser 1297
				#有偵測到讀寫的動作
1298
				else{
226 liveuser 1299
 
3 liveuser 1300
					#針對每個 sock 陣列
1301
					foreach($read as $readStream){
226 liveuser 1302
 
3 liveuser 1303
						#如果元素是 listen 的 socket
1304
						if($readStream === $socket){
226 liveuser 1305
 
3 liveuser 1306
							#當等待別人連線時,若有人連線進來.
1307
							$conn = stream_socket_accept($socket);
226 liveuser 1308
 
3 liveuser 1309
							#取得 meta data
1310
							#$stream_meta_data=stream_get_meta_data($conn);
226 liveuser 1311
 
3 liveuser 1312
							#debug
1313
							#var_dump(__LINE__,$stream_meta_data);
226 liveuser 1314
 
3 liveuser 1315
							#印出有連線進來的訊息
1316
							echo "received a connection".PHP_EOL;
226 liveuser 1317
 
3 liveuser 1318
							#初始化取得的內容
1319
							$request="";
226 liveuser 1320
 
3 liveuser 1321
							#初始化要回應的內容
1322
							$res=array();
226 liveuser 1323
 
3 liveuser 1324
							#取得 request 的時間
1325
							$last_req_time=time();
226 liveuser 1326
 
3 liveuser 1327
							#當有要求時
1328
							while(!feof($conn)){
226 liveuser 1329
 
3 liveuser 1330
								#讀取要求
1331
								$tmp=fgets($conn, 1024);
226 liveuser 1332
 
3 liveuser 1333
								#如果讀到 PHP_EOL
1334
								if($tmp===PHP_EOL){
226 liveuser 1335
 
3 liveuser 1336
									#結束讀取要求
226 liveuser 1337
									break;
1338
 
3 liveuser 1339
									}#if end
226 liveuser 1340
 
3 liveuser 1341
								#串接回應
1342
								$request=$request.$tmp;
226 liveuser 1343
 
183 liveuser 1344
								}#while end
226 liveuser 1345
 
3 liveuser 1346
							#印出有連線進來的訊息
1347
							echo "received content:".PHP_EOL.$request.PHP_EOL;
226 liveuser 1348
 
3 liveuser 1349
							#parse request json
1350
							$request=json_decode($request);
226 liveuser 1351
 
3 liveuser 1352
							#如果 parse json 失敗
1353
							if($request===false){
226 liveuser 1354
 
3 liveuser 1355
								#設置錯誤訊息
1356
								$res["error"]="要求格式必須為json";
226 liveuser 1357
 
3 liveuser 1358
								#設置執行失敗
1359
								$res["status"]="false";
226 liveuser 1360
 
3 liveuser 1361
								#印出回的訊息
1362
								echo "res:".print_r($res,true);
226 liveuser 1363
 
3 liveuser 1364
								#寫入回應的內容
1365
								fwrite($conn,json_encode($res).PHP_EOL);
226 liveuser 1366
 
3 liveuser 1367
								#結束回應
1368
								fwrite($conn,PHP_EOL.PHP_EOL);
226 liveuser 1369
 
3 liveuser 1370
								#關閉socket連線,將回應輸出.
1371
								fclose($conn);
226 liveuser 1372
 
3 liveuser 1373
								#等待下個要求
1374
								continue;
226 liveuser 1375
 
3 liveuser 1376
								}#if end
226 liveuser 1377
 
3 liveuser 1378
							#若無ID資訊
1379
							if(!isset($request->id)){
226 liveuser 1380
 
3 liveuser 1381
								#無窮迴圈
1382
								while(true){
226 liveuser 1383
 
3 liveuser 1384
									#set client id 設置要回應的訊息
1385
									#函式說明:
1386
									#呼叫shell執行系統命令,並取得回傳的內容.
1387
									#回傳結果:
1388
									#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1389
									#$result["error"],錯誤訊息陣列.
1390
									#$result["function"],當前執行的函數名稱.
1391
									#$result["argu"],使用的參數.
1392
									#$result["cmd"],執行的指令內容.
1393
									#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
1394
									#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
1395
									#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
1396
									#$result["running"],是否還在執行.
1397
									#$result["pid"],pid.
1398
									#$result["statusCode"],執行結束後的代碼.
1399
									#必填參數:
1400
									#$conf["command"],字串,要執行的指令.
1401
									$conf["external::callShell"]["command"]="uuid";
1402
									#$conf["fileArgu"],字串,變數__FILE__的內容.
1403
									$conf["external::callShell"]["fileArgu"]=__FILE__;
1404
									#可省略參數:
1405
									#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
1406
									#$conf["argu"]=array("");
1407
									#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
1408
									#$conf["arguIsAddr"]=array();
1409
									#$conf["plainArgu"],字串陣列,哪幾個參數不要加上"",若為"true"則代表不用包;反之"false"則代表要包.
1410
									#$conf["plainArgu"]=array();
1411
									#$conf["useApostrophe"],字串陣列,如果有需要包住,則用「'」,而非「"」處理.前者為"true";後者為"false".
1412
									#$conf["useApostrophe"]=array();
1413
									#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
1414
									#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
1415
									#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
1416
									#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
1417
									#$conf["enablePrintDescription"]="true";
1418
									#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
1419
									#$conf["printDescription"]="";
1420
									#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
1421
									$conf["external::callShell"]["escapeshellarg"]="true";
1422
									#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
1423
									#$conf["thereIsShellVar"]=array();
1424
									#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
1425
									#$conf["username"]="";
1426
									#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
1427
									#$conf["password"]="";
1428
									#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
1429
									#$conf["useScript"]="";
1430
									#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
1431
									#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
1432
									#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
1433
									#$conf["inBackGround"]="";
1434
									#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
1435
									#$conf["getErr"]="false";
1436
									#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
1437
									#$conf["doNotRun"]="false";
1438
									#參考資料:
1439
									#exec=>http://php.net/manual/en/function.exec.php
1440
									#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
1441
									#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
1442
									#備註:
1443
									#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
1444
									#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
1445
									$callShell=external::callShell($conf["external::callShell"]);
1446
									unset($conf["external::callShell"]);
226 liveuser 1447
 
3 liveuser 1448
									#如果執行失敗
1449
									if($callShell["status"]==="false"){
226 liveuser 1450
 
3 liveuser 1451
										#設置執行失敗
1452
										$result["status"]="false";
226 liveuser 1453
 
3 liveuser 1454
										#設置錯誤訊息
1455
										$result["error"]=$callShell;
226 liveuser 1456
 
3 liveuser 1457
										#回傳結果
226 liveuser 1458
										return $result;
1459
 
3 liveuser 1460
										}#if end
226 liveuser 1461
 
3 liveuser 1462
									#如果沒有預期的 uuid 回應
1463
									if(!isset($callShell["output"][0])){
226 liveuser 1464
 
3 liveuser 1465
										#設置執行失敗
1466
										$result["status"]="false";
226 liveuser 1467
 
3 liveuser 1468
										#設置錯誤訊息
1469
										$result["error"][]="get uuid failed!";
226 liveuser 1470
 
3 liveuser 1471
										#設置錯誤訊息
1472
										$result["error"][]=$callShell;
226 liveuser 1473
 
3 liveuser 1474
										#回傳結果
226 liveuser 1475
										return $result;
1476
 
3 liveuser 1477
										}#if end
226 liveuser 1478
 
3 liveuser 1479
									#檢查 client id 是否已經存在
1480
									if(isset($client[$callShell["output"][0]])){
226 liveuser 1481
 
3 liveuser 1482
										#休息一微秒
1483
										usleep(1);
226 liveuser 1484
 
3 liveuser 1485
										#再次建立id試試
1486
										continue;
226 liveuser 1487
 
3 liveuser 1488
										}#if end
226 liveuser 1489
 
3 liveuser 1490
									#設置回應的id
1491
									$res["id"]=$callShell["output"][0];
226 liveuser 1492
 
3 liveuser 1493
									#建立id完畢
1494
									break;
226 liveuser 1495
 
3 liveuser 1496
									}#while end
226 liveuser 1497
 
3 liveuser 1498
								#儲存使用者id與最後一次來request的時間
1499
								$client[$res["id"]]=array("last_req_time"=>$last_req_time);
226 liveuser 1500
 
3 liveuser 1501
								#印出用戶端列表
1502
								echo "clients:".print_r($client,true);
226 liveuser 1503
 
3 liveuser 1504
								#設置執行正常
1505
								$res["status"]="true";
226 liveuser 1506
 
3 liveuser 1507
								#印出回的訊息
1508
								echo "response:".PHP_EOL.print_r($res,true);
226 liveuser 1509
 
3 liveuser 1510
								#寫入回應的內容
1511
								fwrite($conn,json_encode($res).PHP_EOL);
226 liveuser 1512
 
3 liveuser 1513
								#結束回應
1514
								fwrite($conn,PHP_EOL.PHP_EOL);
226 liveuser 1515
 
3 liveuser 1516
								#關閉socket連線,將回應輸出.
1517
								fclose($conn);
226 liveuser 1518
 
3 liveuser 1519
								#等待下個要求
1520
								continue;
226 liveuser 1521
 
57 liveuser 1522
								}#if end
226 liveuser 1523
 
3 liveuser 1524
							#若ID資訊不存在
1525
							if(!isset($client[$request->id])){
226 liveuser 1526
 
3 liveuser 1527
								#設置執行失敗
1528
								$res["status"]="false";
226 liveuser 1529
 
3 liveuser 1530
								#設置ID不存在的訊息
1531
								$res["error"]="ID:".$request->id." 不存在,請再要一次ID.";
226 liveuser 1532
 
3 liveuser 1533
								#印出回的訊息
1534
								echo "response:".PHP_EOL.print_r($res,true);
226 liveuser 1535
 
3 liveuser 1536
								#寫入回應的內容
1537
								fwrite($conn,json_encode($res).PHP_EOL);
226 liveuser 1538
 
3 liveuser 1539
								#結束回應
1540
								fwrite($conn,PHP_EOL.PHP_EOL);
226 liveuser 1541
 
3 liveuser 1542
								#關閉socket連線,將回應輸出.
1543
								fclose($conn);
226 liveuser 1544
 
3 liveuser 1545
								#等待下個要求
1546
								continue;
226 liveuser 1547
 
3 liveuser 1548
								}#if end
226 liveuser 1549
 
3 liveuser 1550
							#如果有客製化訊息
1551
							if(isset($request->custom)){
226 liveuser 1552
 
3 liveuser 1553
								#取代之
1554
								$request=$request->custom;
226 liveuser 1555
 
3 liveuser 1556
								}#if end
226 liveuser 1557
 
3 liveuser 1558
							#若有要執行的指令 cmd,且非客製化.
183 liveuser 1559
							if(isset($request->cmd)){
226 liveuser 1560
 
3 liveuser 1561
								#如果是要結束連線
1562
								if($request->cmd==="quit"){
226 liveuser 1563
 
3 liveuser 1564
									#設置執行正常
1565
									$res["status"]="true";
226 liveuser 1566
 
3 liveuser 1567
									#設置訊息
1568
									$res["content"]="將關閉與您的連線";
226 liveuser 1569
 
3 liveuser 1570
									#寫入回應的內容
1571
									fwrite($conn,json_encode($res).PHP_EOL);
226 liveuser 1572
 
3 liveuser 1573
									#結束回應
1574
									fwrite($conn,PHP_EOL.PHP_EOL);
226 liveuser 1575
 
3 liveuser 1576
									#關閉socket連線,將回應輸出.
1577
									fclose($conn);
226 liveuser 1578
 
3 liveuser 1579
									#移除該用戶變數
1580
									unset($client[$index]);
226 liveuser 1581
 
3 liveuser 1582
									#計數移除了幾個用戶
1583
									$res["delCount"]++;
226 liveuser 1584
 
3 liveuser 1585
									#設置執行正常
1586
									$res["status"]="true";
226 liveuser 1587
 
3 liveuser 1588
									#印出回的訊息
1589
									echo "response:".PHP_EOL.print_r($res,true);
226 liveuser 1590
 
3 liveuser 1591
									#等待下個要求
1592
									continue;
226 liveuser 1593
 
3 liveuser 1594
									}#if end
226 liveuser 1595
 
3 liveuser 1596
								#如果是要結束server
1597
								if($request->cmd===$shutdownStr){
226 liveuser 1598
 
339 liveuser 1599
									#如果有設置 shutdownCallback
1600
									if(isset($conf["shutdownCallback"])){
1601
 
1602
										#初始化要作為 $conf["shutdownCallback"] 函式的陣列參數
1603
										$shutdownCallbackArgu=array();
1604
 
1605
										#設置 serveCache 參數
1606
										$shutdownCallbackArgu["serverCache"]=$result["serverCache"];
1607
 
1608
										#設置 clientCache 參數
1609
										$shutdownCallbackArgu["clientCache"]=$result["clientCache"];
1610
 
1611
										#設置 info 參數
1612
										$shutdownCallbackArgu["info"]=$conf["infoToFunction"];
1613
 
1614
										#呼叫 $addOnProcessFunc 函式,參數用 $shutdownCallbackArgu
1615
										eval("\$call_user_func=".$conf["shutdownCallback"]."(\$shutdownCallbackArgu);");
1616
 
1617
										}#if end
1618
 
3 liveuser 1619
									#設置執行正常
1620
									$res["status"]="true";
226 liveuser 1621
 
3 liveuser 1622
									#設置訊息
1623
									$res["content"]="本服務將結束";
226 liveuser 1624
 
3 liveuser 1625
									#寫入回應的內容
1626
									fwrite($conn,json_encode($res).PHP_EOL);
226 liveuser 1627
 
3 liveuser 1628
									#結束回應
1629
									fwrite($conn,PHP_EOL.PHP_EOL);
226 liveuser 1630
 
3 liveuser 1631
									#關閉socket連線,將回應輸出.
1632
									fclose($conn);
226 liveuser 1633
 
3 liveuser 1634
									#設置執行正常
1635
									$result["status"]="true";
226 liveuser 1636
 
3 liveuser 1637
									#結束本程式
1638
									return $result;
226 liveuser 1639
 
3 liveuser 1640
									}#if end
226 liveuser 1641
 
3 liveuser 1642
								#涵式說明:
1643
								#呼叫shell執行系統命令,並取得回傳的內容.
1644
								#回傳的結果:
1645
								#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1646
								#$result["error"],錯誤訊息陣列.
1647
								#$result["function"],當前執行的函數名稱.
1648
								#$result["argu"],使用的參數.
1649
								#$result["cmd"],執行的指令內容.
1650
								#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
1651
								#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
1652
								#$result["tmpFileOutput"],儲存輸出的暫村檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
1653
								#$result["running"],是否還在執行.
1654
								#$result["pid"],pid
1655
								#必填的參數
1656
								#$conf["command"],字串,要執行的指令與.
1657
								$conf["external::callShell"]["command"]=$request->cmd;
1658
								#$conf["fileArgu"],字串,變數__FILE__的內容.
57 liveuser 1659
								$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];
3 liveuser 1660
								#可省略參數:
226 liveuser 1661
 
3 liveuser 1662
								#如果有指定參數
1663
								if(isset($request->param)){
226 liveuser 1664
 
3 liveuser 1665
									#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
1666
									$conf["external::callShell"]["argu"]=$request->param;
226 liveuser 1667
 
3 liveuser 1668
									}#if end
226 liveuser 1669
 
3 liveuser 1670
								#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
226 liveuser 1671
								#$conf["arguIsAddr"]=array();
3 liveuser 1672
								#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
1673
								#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
1674
								#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
1675
								#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
1676
								#$conf["enablePrintDescription"]="true";
1677
								#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容.
1678
								#$conf["printDescription"]="";
226 liveuser 1679
 
3 liveuser 1680
								#如果有參數
1681
								if(isset($request->param)){
226 liveuser 1682
 
3 liveuser 1683
									#如果有識別是否已經 escaped 參數
1684
									if(isset($request->escaped)){
226 liveuser 1685
 
3 liveuser 1686
										#設置預設要 escape
1687
										$needEscaped="true";
226 liveuser 1688
 
3 liveuser 1689
										#如果已經 ecaped
259 liveuser 1690
										if($request->escaped==="true"){
226 liveuser 1691
 
3 liveuser 1692
											#設置不用 escape
1693
											$needEscaped="false";
226 liveuser 1694
 
3 liveuser 1695
											}#if end
226 liveuser 1696
 
3 liveuser 1697
										#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".
1698
										$conf["external::callShell"]["escapeshellarg"]=$needEscaped;
226 liveuser 1699
 
3 liveuser 1700
										}#if end
226 liveuser 1701
 
3 liveuser 1702
									}#if end
226 liveuser 1703
 
3 liveuser 1704
								#如果有設置要用特定的使用者身份執行
1705
								if(isset($request->user) && isset($request->password)){
226 liveuser 1706
 
3 liveuser 1707
									#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
1708
									$conf["external::callShell"]["username"]=$request->user;
226 liveuser 1709
 
3 liveuser 1710
									#$conf["password"],字串,與$conf["username"]搭配的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
1711
									$conf["external::callShell"]["password"]=$request->password;
226 liveuser 1712
 
3 liveuser 1713
									}#if end
226 liveuser 1714
 
3 liveuser 1715
								#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
1716
								#$conf["useScript"]="";
1717
								#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
1718
								#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
1719
								#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
1720
								#$conf["inBackGround"]="";
1721
								#備註:
1722
								#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
1723
								#參考資料:
1724
								#exec=>http://php.net/manual/en/function.exec.php
1725
								#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
1726
								#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
1727
								$callShell=external::callShell($conf["external::callShell"]);
1728
								unset($conf["external::callShell"]);
226 liveuser 1729
 
3 liveuser 1730
								#設置要回覆的內容
1731
								$res=$callShell;
226 liveuser 1732
 
3 liveuser 1733
								#印出回的訊息
1734
								echo "response:".PHP_EOL.print_r($res,true);
226 liveuser 1735
 
3 liveuser 1736
								#寫入回應的內容
1737
								fwrite($conn,json_encode($res).PHP_EOL);
226 liveuser 1738
 
3 liveuser 1739
								#結束回應
1740
								fwrite($conn,PHP_EOL.PHP_EOL);
226 liveuser 1741
 
3 liveuser 1742
								#關閉socket連線,將回應輸出.
1743
								fclose($conn);
226 liveuser 1744
 
3 liveuser 1745
								#更新使用者id與最後一次來request的時間
226 liveuser 1746
								$client[$request->id]["last_req_time"]=$last_req_time;
1747
 
3 liveuser 1748
								#等待下個要求
1749
								continue;
226 liveuser 1750
 
3 liveuser 1751
								}#if end
226 liveuser 1752
 
3 liveuser 1753
							#設置接收到json字串資訊
1754
							$client[$request->id]["request"]=(array)($request);
226 liveuser 1755
 
3 liveuser 1756
							#設置用戶端的socket
1757
							$client[$request->id]["socket"]=$conn;
226 liveuser 1758
 
3 liveuser 1759
							#如果有要求在背景中執行指令的訊息
1760
							if(isset($request->cmdInBg)){
226 liveuser 1761
 
3 liveuser 1762
								#debug
1763
								#var_dump(__FUNCTION__,__LINE__,$request);
226 liveuser 1764
 
3 liveuser 1765
								#如果有參數
1766
								if(isset($request->param)){
226 liveuser 1767
 
3 liveuser 1768
									#函式說明:
1769
									#將一維陣列轉換為用特定符號間隔的字串,ex:array("1","2","3") to "a;b;c;".
1770
									#回傳的結果:
1771
									#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1772
									#$result["function"],當前執行的function名稱
1773
									#$result["error"],錯誤訊息陣列.
1774
									#$result["content"],處理好的字串.
1775
									#$result["argu"],使用的參數.
1776
									#必填參數:
1777
									#$conf["inputArray"],字串陣列,要轉成字串的一維陣列.
1778
									$conf["arrays::arrayToString"]["inputArray"]=$request->param;
1779
									#可省略參數:
1780
									#$conf["spiltSymbol"],字串,用來區隔字串的符號,預設為;
1781
									$conf["arrays::arrayToString"]["spiltSymbol"]=" ";
1782
									#$conf["skipEnd"],字串,結尾是否不要加上符號,預設為"false",要加上符號,"true"代表不要加上符號。
1783
									$conf["arrays::arrayToString"]["skipEnd"]="true";
1784
									#$conf["spiltSymbolAtStart"],字串,是否要在開頭加上spiltSymbol,預設為"false",代表不要;反之為“true”.
1785
									$conf["arrays::arrayToString"]["spiltSymbolAtStart"]="true";
1786
									#參考資料:
1787
									#無.
1788
									#備註:
1789
									#無.
1790
									$arrayToString=arrays::arrayToString($conf["arrays::arrayToString"]);
1791
									unset($conf["arrays::arrayToString"]);
226 liveuser 1792
 
3 liveuser 1793
									#如果執行失敗
1794
									if($arrayToString["status"]==="false"){
226 liveuser 1795
 
3 liveuser 1796
										#設置執行失敗
1797
										$result["status"]="false";
226 liveuser 1798
 
3 liveuser 1799
										#設置錯誤訊息
1800
										$result["error"]=$arrayToString;
226 liveuser 1801
 
3 liveuser 1802
										#回傳結果
1803
										return $result;
226 liveuser 1804
 
3 liveuser 1805
										}#if end
226 liveuser 1806
 
3 liveuser 1807
									#串接參數
1808
									$request->cmdInBg=$request->cmdInBg.$arrayToString["content"];
226 liveuser 1809
 
3 liveuser 1810
									}#if end
226 liveuser 1811
 
3 liveuser 1812
								#函式說明:
1813
								#透過proc來多執行序運作.
1814
								#回傳結果:
1815
								#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1816
								#$reuslt["error"],執行不正常結束的錯訊息陣列.
1817
								#$result["function"],當前執行的函式名稱.
1818
								#$result["argu"],使用的參數.
1819
								#$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"代表程序的資訊.
1820
								#必填參數:
1821
								#$conf["cmds"],字串陣列,每個元素代表要執行的指令與參數.
1822
								$conf["threads::proc"]["cmds"]=array($request->cmdInBg);
1823
								#可省略參數:
1824
								#$conf["wait"],字串,是否需要等待所有程序結束,預設為"true"要等待;反之為"false"不要等待.
1825
								$conf["threads::proc"]["wait"]="false";
1826
								#$conf["workingDir"],字串陣列,個別程式執行時的家目錄,預設不指定.
1827
								#$conf["workingDir"]=array("path");
1828
								#$conf["envs"],2維字串陣列,每個元素代表個別程式執行時的指定環境變數,key變數名稱;value為變數內容.預設為array("QBPWCF" => "Quick Build PHP Website Componment base on Fedora Linux");
1829
								#$conf["envs"]=array(array("key"=>"value"));
1830
								#$conf["executeBy"],字串陣列,每個元素代表個別指令要用什麼程式執行,預設為"bash".
1831
								#$conf["executeBy"]=array("bash");
1832
								#參考資料:
1833
								#https://www.php.net/manual/en/function.proc-open.php
1834
								#https://www.php.net/manual/en/function.proc-get-status.php
1835
								#備註:
1836
								#若需要取得當下的執行狀況,請使用 self::proc_update 來更新.
1837
								$proc=threads::proc($conf["threads::proc"]);
1838
								unset($conf["threads::proc"]);
226 liveuser 1839
 
3 liveuser 1840
								#如果執行失敗
1841
								if($proc["status"]==="false"){
226 liveuser 1842
 
3 liveuser 1843
									#設置執行失敗
1844
									$result["status"]="false";
226 liveuser 1845
 
3 liveuser 1846
									#設置錯誤訊息
1847
									$result["error"]=$proc;
226 liveuser 1848
 
3 liveuser 1849
									#回傳結果
1850
									return $result;
226 liveuser 1851
 
3 liveuser 1852
									}#if end
226 liveuser 1853
 
3 liveuser 1854
								#函式說明:
1855
								#使用 linux 的 uuid 指令來產生 uuid 字串
1856
								#回傳結果:
1857
								#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1858
								#$result["error"],錯誤訊息.
1859
								#$result["function"],當前執行的函式名稱.
1860
								#$result["content"],uuid.
1861
								#必填參數:
1862
								#無.
1863
								#可省略參數:
1864
								#無.
1865
								#參考資料:
1866
								#無.
1867
								#備註:
1868
								#無.
1869
								$uuid=cmd::uuid();
226 liveuser 1870
 
3 liveuser 1871
								#如果執行失敗
1872
								if($uuid["status"]==="false"){
226 liveuser 1873
 
3 liveuser 1874
									#設置執行失敗
1875
									$result["status"]="false";
226 liveuser 1876
 
3 liveuser 1877
									#設置錯誤訊息
1878
									$result["error"]=$uuid;
226 liveuser 1879
 
3 liveuser 1880
									#回傳結果
1881
									return $result;
226 liveuser 1882
 
3 liveuser 1883
									}#if end
226 liveuser 1884
 
3 liveuser 1885
								#取得 uuid
1886
								$uuid=$uuid["content"];
226 liveuser 1887
 
3 liveuser 1888
								#儲存到 server side 的 procs
1889
								$result["serverCache"]["serverSide"]["procs"][$uuid]=$proc;
226 liveuser 1890
 
3 liveuser 1891
								#設置最後的檢查時間為當下
1892
								$result["serverCache"]["serverSide"]["procs"][$uuid]["latestAccessTime"]=time();
226 liveuser 1893
 
3 liveuser 1894
								#設置要回覆的內容
1895
								$res=array("status"=>"true","content"=>$uuid);
226 liveuser 1896
 
3 liveuser 1897
								#印出回的訊息
1898
								echo "response:".PHP_EOL.print_r($res,true);
226 liveuser 1899
 
3 liveuser 1900
								#寫入回應的內容
1901
								fwrite($conn,json_encode($res).PHP_EOL);
226 liveuser 1902
 
3 liveuser 1903
								#結束回應
1904
								fwrite($conn,PHP_EOL.PHP_EOL);
226 liveuser 1905
 
3 liveuser 1906
								#關閉socket連線,將回應輸出.
1907
								fclose($conn);
226 liveuser 1908
 
3 liveuser 1909
								#更新使用者id與最後一次來request的時間
1910
								$client[$request->id]["last_req_time"]=$last_req_time;
226 liveuser 1911
 
3 liveuser 1912
								#等待下個要求
1913
								continue;
226 liveuser 1914
 
3 liveuser 1915
								}#if end
226 liveuser 1916
 
3 liveuser 1917
							#如果要查詢程序執行的狀況
1918
							if(isset($request->QueryProcByUUID)){
226 liveuser 1919
 
3 liveuser 1920
								#如果沒有 proc 資訊
1921
								if(!isset($result["serverCache"]["serverSide"]["procs"])){
226 liveuser 1922
 
3 liveuser 1923
									#設置要回覆的內容
1924
									$res=array("status"=>"true","content"=>"");
226 liveuser 1925
 
3 liveuser 1926
									#印出回的訊息
1927
									echo "response:".PHP_EOL.print_r($res,true);
226 liveuser 1928
 
3 liveuser 1929
									#寫入回應的內容
1930
									fwrite($conn,json_encode($res).PHP_EOL);
226 liveuser 1931
 
3 liveuser 1932
									#結束回應
1933
									fwrite($conn,PHP_EOL.PHP_EOL);
226 liveuser 1934
 
3 liveuser 1935
									#關閉socket連線,將回應輸出.
1936
									fclose($conn);
226 liveuser 1937
 
3 liveuser 1938
									#更新使用者id與最後一次來request的時間
1939
									$client[$request->id]["last_req_time"]=$last_req_time;
226 liveuser 1940
 
3 liveuser 1941
									#等待下個要求
1942
									continue;
226 liveuser 1943
 
3 liveuser 1944
									}#if end
226 liveuser 1945
 
3 liveuser 1946
								#函式說明:
1947
								#更新透過proc執行的多程序資訊.
1948
								#回傳結果:
1949
								#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1950
								#$reuslt["error"],執行不正常結束的錯訊息陣列.
1951
								#$result["function"],當前執行的函式名稱.
1952
								#$result["argu"],使用的參數.
1953
								#$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"代表程序的資訊.
1954
								#必填參數:
1955
								#$conf["procs"],陣列,運行self::proc後回傳的content.
1956
								$conf["threads::proc_update"]["procs"]=$result["serverCache"]["serverSide"]["procs"][$request->QueryProcByUUID]["content"];
1957
								#可省略參數:
1958
								#無.
1959
								#參考資料:
1960
								#無.
1961
								#備註:
1962
								#無.
1963
								$proc_update=threads::proc_update($conf["threads::proc_update"]);
1964
								unset($conf["threads::proc_update"]);
226 liveuser 1965
 
3 liveuser 1966
								#如果執行失敗
1967
								if($proc_update["status"]==="false"){
226 liveuser 1968
 
3 liveuser 1969
									#設置執行失敗
1970
									$result["status"]="false";
226 liveuser 1971
 
3 liveuser 1972
									#設置錯誤訊息
1973
									$result["error"]=$proc_update;
226 liveuser 1974
 
3 liveuser 1975
									#回傳結果
1976
									return $result;
226 liveuser 1977
 
3 liveuser 1978
									}#if end
226 liveuser 1979
 
3 liveuser 1980
								#更新 serverSide 的 proc cache
1981
								$result["serverCache"]["serverSide"]["procs"][$request->QueryProcByUUID]["content"]=$proc_update["content"];
226 liveuser 1982
 
3 liveuser 1983
								#設置最後檢查的時間
1984
								$result["serverCache"]["serverSide"]["procs"][$request->QueryProcByUUID]["latestAccessTime"]=time();
226 liveuser 1985
 
3 liveuser 1986
								#印出子程序的資訊
1987
								echo "proc info:".PHP_EOL.print_r($proc_update["content"][0],true);
226 liveuser 1988
 
3 liveuser 1989
								#初始化要回傳的內容
1990
								$res=array();
226 liveuser 1991
 
3 liveuser 1992
								#設置 pid
1993
								$res["pid"]=$proc_update["content"][0]["proc_get_status"]["pid"];
226 liveuser 1994
 
3 liveuser 1995
								#設置輸入的內容
1996
								$res["input"]=$proc_update["content"][0]["input"];
226 liveuser 1997
 
3 liveuser 1998
								#預設設置已經結束執行
1999
								$res["running"]="false";
226 liveuser 2000
 
3 liveuser 2001
								#如果尚在執行
2002
								if($proc_update["content"][0]["statusCode"]==="?"){
226 liveuser 2003
 
3 liveuser 2004
									#設置正在執行
2005
									$res["running"]="true";
226 liveuser 2006
 
3 liveuser 2007
									}#if end
226 liveuser 2008
 
3 liveuser 2009
								#如果已經結束執行
2010
								if($res["running"]==="false"){
226 liveuser 2011
 
3 liveuser 2012
									#設置執行後得到的標準輸出
2013
									$res["output"]=$proc_update["content"][0]["content"];
226 liveuser 2014
 
3 liveuser 2015
									#設置執行後得到的錯誤輸出
2016
									$res["error"]=$proc_update["content"][0]["error"];
226 liveuser 2017
 
3 liveuser 2018
									}#if end
226 liveuser 2019
 
3 liveuser 2020
								#設置回應正常
2021
								$res["status"]="true";
226 liveuser 2022
 
3 liveuser 2023
								#印出回的訊息
2024
								echo "response:".PHP_EOL.print_r($res,true);
226 liveuser 2025
 
3 liveuser 2026
								#寫入回應的內容
2027
								fwrite($conn,json_encode($res).PHP_EOL);
226 liveuser 2028
 
3 liveuser 2029
								#結束回應
2030
								fwrite($conn,PHP_EOL.PHP_EOL);
226 liveuser 2031
 
3 liveuser 2032
								#關閉socket連線,將回應輸出.
2033
								fclose($conn);
226 liveuser 2034
 
3 liveuser 2035
								#更新使用者id與最後一次來request的時間
2036
								$client[$request->id]["last_req_time"]=$last_req_time;
226 liveuser 2037
 
3 liveuser 2038
								#等待下個要求
2039
								continue;
226 liveuser 2040
 
3 liveuser 2041
								}#if end
226 liveuser 2042
 
3 liveuser 2043
							#debug
2044
							#var_dump(__LINE__,$conn,$client[$request->id]);
226 liveuser 2045
 
3 liveuser 2046
							#如果存在 addOnProcessFunc
2047
							if(isset($conf["addOnProcessFunc"])){
226 liveuser 2048
 
3 liveuser 2049
								#針對每個 addOnProcessFunc
2050
								foreach($conf["addOnProcessFunc"] as $addOnProcessFunc){
226 liveuser 2051
 
3 liveuser 2052
									#如果有設置 infoToFunction
2053
									if(isset($conf["infoToFunction"])){
226 liveuser 2054
 
3 liveuser 2055
										#設置info
2056
										$client[$request->id]["info"]=&$conf["infoToFunction"];
226 liveuser 2057
 
3 liveuser 2058
										}#if end
226 liveuser 2059
 
3 liveuser 2060
									#設置client cache
2061
									$client[$request->id]["clientCache"]=&$clientCache;
226 liveuser 2062
 
3 liveuser 2063
									#呼叫 $addOnProcessFunc 函式,參數用 $client[$request->id], 取得執行後的回應.
2064
									eval("\$call_user_func=".$addOnProcessFunc."(\$client[\$request->id]);");
226 liveuser 2065
 
3 liveuser 2066
									/* debug
226 liveuser 2067
 
3 liveuser 2068
									#函式說明:
2069
									#撰寫log
2070
									#回傳結果:
2071
									#$result["status"],狀態,"true"或"false".
2072
									#$result["error"],錯誤訊息陣列.
2073
									#$result["function"],當前函式的名稱.
2074
									#$result["argu"],使用的參數.
2075
									#$result["content"],要寫入log的內容字串.
2076
									#必填參數:
2077
									#$conf["path"],字串,log檔案的路徑與名稱.
2078
									$conf["logs::record"]["path"]=$conf["sock"].".log";
2079
									#$conf["content"],any,要寫的內容,若內容不為字串則會用var_dump的格式寫入.
2080
									$conf["logs::record"]["content"]=$call_user_func;
2081
									#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
2082
									$conf["logs::record"]["fileArgu"]=__FILE__;
2083
									#可省略參數:
2084
									#$conf["rewrite"],預設為"false",接續寫入;反之"true"代表重新寫入.
2085
									#$conf["rewrite"]="false";
2086
									#$conf["returnOnly"],預設為"false",會寫入到log檔案.若為"true"則不會寫入log.
2087
									#$conf["returnOnly"]="true";
2088
									#參考資料:
2089
									#無.
2090
									#備註:
2091
									#無.
2092
									$record=logs::record($conf["logs::record"]);
2093
									unset($conf["logs::record"]);
226 liveuser 2094
 
3 liveuser 2095
									#如果執行失敗
2096
									if($record["status"]==="false"){
226 liveuser 2097
 
3 liveuser 2098
										#設置執行錯誤識別
2099
										$result["status"]="false";
226 liveuser 2100
 
3 liveuser 2101
										#設置執行錯誤
2102
										$result["error"]=$record;
226 liveuser 2103
 
3 liveuser 2104
										#回傳結果
2105
										return $result;
226 liveuser 2106
 
3 liveuser 2107
										}#if end
226 liveuser 2108
 
3 liveuser 2109
									*/
226 liveuser 2110
 
3 liveuser 2111
									#如果回應不是 array
2112
									if(gettype($call_user_func)!=="array"){
226 liveuser 2113
 
3 liveuser 2114
										#設置執行失敗
2115
										$result["status"]="false";
226 liveuser 2116
 
3 liveuser 2117
										#設置錯誤訊息
2118
										$result["error"][]=$call_user_func;
226 liveuser 2119
 
3 liveuser 2120
										#設置錯誤訊息
2121
										$result["error"][]=$conf;
226 liveuser 2122
 
3 liveuser 2123
										#印出error資訊
2124
										echo "error:".print_r($result,true);
226 liveuser 2125
 
3 liveuser 2126
										#回應錯誤訊息給用戶
2127
										fwrite($conn,json_encode($result).PHP_EOL);
226 liveuser 2128
 
3 liveuser 2129
										#結束回應
2130
										fwrite($conn,PHP_EOL.PHP_EOL);
226 liveuser 2131
 
3 liveuser 2132
										#關閉socket連線,將回應輸出.
2133
										fclose($conn);
226 liveuser 2134
 
3 liveuser 2135
										#回傳結果
2136
										return $result;
226 liveuser 2137
 
3 liveuser 2138
										}#if end
226 liveuser 2139
 
3 liveuser 2140
									#檢查回應的格式
2141
									#函式說明:
2142
									#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
2143
									#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
2144
									#$reuslt["error"],執行不正常結束的錯訊息陣列.
2145
									#$result["function"],當前執行的函式名稱.
2146
									#$result["argu"],設置給予的參數.
2147
									#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
2148
									#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
2149
									#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
2150
									#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
2151
									#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
2152
									#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
2153
									#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
2154
									#$result["notNeedVar"],字串陣列,多餘的參數名稱.
2155
									#必填寫的參數:
2156
									#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
226 liveuser 2157
									$conf["variableCheck::checkArguments"]["varInput"]=&$call_user_func;
3 liveuser 2158
									#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
2159
									$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
2160
									#可以省略的參數:
2161
									#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
2162
									$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("status");
2163
									#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
2164
									$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string");
2165
									#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
2166
									$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
2167
									#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
2168
									#$conf["canNotBeEmpty"]=array();
2169
									#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
2170
									#$conf["canBeEmpty"]=array();
2171
									#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
2172
									#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("function","continue");
2173
									#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
2174
									$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("function","continue","content","cache","argu","error");
226 liveuser 2175
									#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 2176
									$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string",null,"array","array","array");
2177
									#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
2178
									$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array($addOnProcessFunc,"true",null,null,null,null);
2179
									#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
2180
									#$conf["disallowAllSkipableVarIsEmpty"]="";
2181
									#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
2182
									#$conf["disallowAllSkipableVarIsEmptyArray"]="";
2183
									#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
2184
									#$conf["arrayCountEqualCheck"][]=array();
2185
									#參考資料來源:
2186
									#array_keys=>http://php.net/manual/en/function.array-keys.php
2187
									$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
2188
									unset($conf["variableCheck::checkArguments"]);
226 liveuser 2189
 
3 liveuser 2190
									#如果檢查參數失敗
2191
									if($checkArguments["status"]==="false"){
226 liveuser 2192
 
3 liveuser 2193
										#設置執行失敗
2194
										$result["status"]="false";
226 liveuser 2195
 
3 liveuser 2196
										#設置錯誤訊息
2197
										$result["error"]=$checkArguments;
226 liveuser 2198
 
3 liveuser 2199
										#印出error資訊
2200
										echo "error:".print_r($result,true);
226 liveuser 2201
 
3 liveuser 2202
										#回應錯誤訊息給用戶
2203
										fwrite($conn,json_encode($result).PHP_EOL);
226 liveuser 2204
 
3 liveuser 2205
										#結束回應
2206
										fwrite($conn,PHP_EOL.PHP_EOL);
226 liveuser 2207
 
3 liveuser 2208
										#關閉socket連線,將回應輸出.
2209
										fclose($conn);
226 liveuser 2210
 
3 liveuser 2211
										#回傳結果
2212
										return $result;
226 liveuser 2213
 
3 liveuser 2214
										}#if end
226 liveuser 2215
 
3 liveuser 2216
									#如果檢查參數不通過
2217
									if($checkArguments["passed"]==="false"){
226 liveuser 2218
 
3 liveuser 2219
										#設置執行失敗
2220
										$result["status"]="false";
226 liveuser 2221
 
3 liveuser 2222
										#設置錯誤訊息
2223
										$result["error"]=$checkArguments;
226 liveuser 2224
 
3 liveuser 2225
										#印出error資訊
2226
										echo "error:".print_r($result,true);
226 liveuser 2227
 
3 liveuser 2228
										#回應錯誤訊息給用戶
2229
										fwrite($conn,json_encode($result).PHP_EOL);
226 liveuser 2230
 
3 liveuser 2231
										#結束回應
2232
										fwrite($conn,PHP_EOL.PHP_EOL);
226 liveuser 2233
 
3 liveuser 2234
										#關閉socket連線,將回應輸出.
2235
										fclose($conn);
226 liveuser 2236
 
3 liveuser 2237
										#回傳結果
2238
										return $result;
226 liveuser 2239
 
3 liveuser 2240
										}#if end
226 liveuser 2241
 
3 liveuser 2242
									#如果回應不正常
2243
									if($call_user_func["status"]==="false"){
226 liveuser 2244
 
3 liveuser 2245
										#設置執行失敗
2246
										$result["status"]="false";
226 liveuser 2247
 
3 liveuser 2248
										#設置錯誤訊息
2249
										$result["error"][]=$call_user_func;
226 liveuser 2250
 
3 liveuser 2251
										#設置錯誤訊息
2252
										$result["error"][]=$conf;
226 liveuser 2253
 
3 liveuser 2254
										#印出error資訊
2255
										echo "error:".print_r($result,true);
226 liveuser 2256
 
3 liveuser 2257
										#回應錯誤訊息給用戶
2258
										fwrite($conn,json_encode($result).PHP_EOL);
226 liveuser 2259
 
3 liveuser 2260
										#結束回應
2261
										fwrite($conn,PHP_EOL.PHP_EOL);
226 liveuser 2262
 
3 liveuser 2263
										#關閉socket連線,將回應輸出.
2264
										fclose($conn);
226 liveuser 2265
 
3 liveuser 2266
										#回傳結果
2267
										return $result;
226 liveuser 2268
 
3 liveuser 2269
										}#if end
226 liveuser 2270
 
3 liveuser 2271
									#如果回應有 content 元素
2272
									if(isset($call_user_func["content"])){
226 liveuser 2273
 
3 liveuser 2274
										#用收到 content 元素內容來更新 $client[id]["request"]
2275
										$client[$request->id]["request"]=$call_user_func["content"];
226 liveuser 2276
 
3 liveuser 2277
										}#if end
226 liveuser 2278
 
3 liveuser 2279
									#如果回傳 cache 元素
2280
									if(isset($call_user_func["cache"])){
226 liveuser 2281
 
3 liveuser 2282
										#用收到 cache 元素內容來更新 server side 的 cache
2283
										$result["serverCache"]["clientSide"]["addOnProcessFunc"][$addOnProcessFunc]=&$call_user_func["cache"];
226 liveuser 2284
 
3 liveuser 2285
										}#if end
226 liveuser 2286
 
3 liveuser 2287
									#如果回應不要往後面執行
2288
									if($call_user_func["continue"]==="false"){
226 liveuser 2289
 
3 liveuser 2290
										#結束 foreach
2291
										break;
226 liveuser 2292
 
3 liveuser 2293
										}#if end
226 liveuser 2294
 
3 liveuser 2295
									}#foreach end
226 liveuser 2296
 
3 liveuser 2297
								}#if end
226 liveuser 2298
 
3 liveuser 2299
							#執行到這代表是要保持連線
226 liveuser 2300
 
3 liveuser 2301
							#設置執行正常
2302
							$res["status"]="true";
226 liveuser 2303
 
3 liveuser 2304
							#印出回的訊息
2305
							echo "response:".PHP_EOL.print_r($res,true);
226 liveuser 2306
 
3 liveuser 2307
							#debug
2308
							#var_dump(__LINE__,$conn,get_resource_type($conn));
226 liveuser 2309
 
3 liveuser 2310
							#如果資源形態沒有異常(用戶關閉連線後,會有resource type變成"Unknown"的結果)
2311
							if(get_resource_type($conn)!=="Unknown"){
226 liveuser 2312
 
3 liveuser 2313
								#函式說明:
2314
								#用fwrite寫入到resource,可以指定每個寫入字串的結尾方式.
2315
								#回傳結果:
2316
								#$result["status"],執行是否正常,"true"為正常,"false"為不正常.
2317
								#$result["error"],錯誤訊息陣列.
2318
								#$result["function"],當前執行的函數名稱.
2319
								#$result["argu"],使用的參數.
2320
								#$result["is_writable"],參數stream是否為可寫入的resource,"true"代表可寫入;"false"代表不可寫入.
2321
								#必填參數:
2322
								#$conf["stream"],resource,寫入的目標.
2323
								$conf["sock::fwrite"]["stream"]=&$conn;
2324
								#$conf["data"],字串陣列,要寫入的內容,每個元素代表每段要寫入的內容.
2325
								$conf["sock::fwrite"]["data"]=array(json_encode($res).PHP_EOL);
2326
								#可省略參數:
2327
								#$conf["msgEndType"],字串,訊息結尾要怎麼寫,預設為"none",代表不做處理;"EOL"代表結尾要用PHP_EOL;"HTTP/1.1"代表"data"參數最多為2個,分別為header跟body,結尾方式為兩個PHP_EOL.
2328
								$conf["sock::fwrite"]["msgEndType"]="HTTP/1.1";
2329
								#$conf["autoClose"],字串,寫入結束後是否要關閉resource,預設為"false"代表保持開着;反之為"true".
2330
								$conf["sock::fwrite"]["autoClose"]="true";
2331
								#參考資料:
2332
								#https://stackoverflow.com/questions/5294305/how-to-check-if-a-php-stream-resource-is-readable-or-writable#5294425
2333
								#https://www.php.net/manual/en/function.stream-get-meta-data.php
2334
								#https://www.php.net/manual/en/function.is-writable.php
2335
								#https://www.php.net/manual/en/function.socket-get-status.php
2336
								#備註:
2337
								#無.
2338
								$fwrite=sock::fwrite($conf["sock::fwrite"]);
2339
								unset($conf["sock::fwrite"]);
226 liveuser 2340
 
3 liveuser 2341
								#如果執行失敗
2342
								if($fwrite["status"]==="false"){
226 liveuser 2343
 
3 liveuser 2344
									#如果有 is_writable
2345
									if(isset($fwrite["is_writable"])){
226 liveuser 2346
 
3 liveuser 2347
										#如果有 is_writable
2348
										if($fwrite["is_writable"]==="true"){
226 liveuser 2349
 
3 liveuser 2350
											#設置執行失敗
2351
											$result["status"]="false";
226 liveuser 2352
 
3 liveuser 2353
											#設置錯誤訊息
2354
											$result["error"]=$fwrite;
226 liveuser 2355
 
3 liveuser 2356
											#印出error資訊
2357
											echo "error:".print_r($result,true);
226 liveuser 2358
 
3 liveuser 2359
											#結束並回傳1給shell
2360
											exit(1);
226 liveuser 2361
 
3 liveuser 2362
											}#if end
226 liveuser 2363
 
3 liveuser 2364
										}#if end
226 liveuser 2365
 
3 liveuser 2366
									#反之
2367
									else{
226 liveuser 2368
 
3 liveuser 2369
										#設置執行失敗
2370
										$result["status"]="false";
226 liveuser 2371
 
3 liveuser 2372
										#設置錯誤訊息
2373
										$result["error"]=$fwrite;
226 liveuser 2374
 
3 liveuser 2375
										#印出error資訊
2376
										echo "error:".print_r($result,true);
226 liveuser 2377
 
3 liveuser 2378
										#結束並回傳1給shell
2379
										exit(1);
226 liveuser 2380
 
3 liveuser 2381
										}#else end
226 liveuser 2382
 
3 liveuser 2383
									}#if end
226 liveuser 2384
 
3 liveuser 2385
								}#if end
226 liveuser 2386
 
3 liveuser 2387
							#更新使用者id與最後一次來request的時間
183 liveuser 2388
							$client[$request->id]["last_req_time"]=$last_req_time;
226 liveuser 2389
 
3 liveuser 2390
							#印出用戶端列表
183 liveuser 2391
							echo "clients:".print_r($client,true);
226 liveuser 2392
 
3 liveuser 2393
							#等待下個要求
2394
							continue;
226 liveuser 2395
 
3 liveuser 2396
							}#if end
226 liveuser 2397
 
3 liveuser 2398
						#socket 陣列不是我們建立的 socket
2399
						else{
226 liveuser 2400
 
3 liveuser 2401
							#顯示 socket 的狀態
2402
							var_dump("socket_get_status".socket_get_status($conn));
226 liveuser 2403
 
3 liveuser 2404
							}#else end
226 liveuser 2405
 
3 liveuser 2406
						}#foreach end
226 liveuser 2407
 
3 liveuser 2408
					}#else end
226 liveuser 2409
 
3 liveuser 2410
				}#while end
226 liveuser 2411
 
3 liveuser 2412
			#關閉 unix domain socket
2413
			fclose($socket);
226 liveuser 2414
 
3 liveuser 2415
			}#else end
226 liveuser 2416
 
3 liveuser 2417
		}#function unixDomainSockServer end
226 liveuser 2418
 
3 liveuser 2419
	/*
2420
	#函式說明:
306 liveuser 2421
	#建立 unix domain socket server, 僅提供具備檔案存取權限的用戶使用,預設提供可以下達任何指令的功能.
2422
	#回傳結果:
2423
	#$result["status"],"true"代表執行正常;"false"代表執行不正常.
2424
	#$result["error"],錯誤訊息陣列.
2425
	#$result["function"],當前執行的函式名稱.
2426
	#$result["serverCache"],函式結束前,儲存在serverCache的內容,若有元素"exit"存在則代表是正常結束.
2427
	#$result["serverCache"]["serverSide"],server side 的 cache.
2428
	#$result["serverCache"]["serverSide"]["procs"], server side 的 procs cache,儲存執行的子程序資訊.
2429
	#$result["serverCache"]["clientSide"],client site 的 cache.
2430
	#必填參數:
2431
	#$conf["sockAddr"],字串,socket檔案要放在哪邊,名稱為何.
2432
	$conf["sockAddr"]="";
2433
	#conf["listenInfo"],字串,要listen的 ip跟port,預設為"127.0.0.1:54321".
2434
	$conf["listenInfo"]="127.0.0.1:54321":
2435
	#可省略參數:
2436
	#$conf["changeOwner"],字串,要將socket檔案的擁有着權限進行修改."user.group"代表擁有者帳號為user,群組為group.
2437
	#$conf["changeOwner"]="";
2438
	#$conf["changePermission"],字串,要將socket檔案的權限設為多少.ex: "0666"(所有帳戶都有存取的權限) 或 "0660"(僅有擁有者與群組帳戶有存取的權限) 或 "0600"(只有擁有者有權限執行).
2439
	#$conf["changePermission"]="";
2440
	#$conf["sessionTimeout"],字串,當連線結束後於下一次連線間隔多久就算session timeout,server端會將記錄移除,client端需要重新拿取id,預設為300秒.
2441
	#$conf["sessionTimeout"]="300";
2442
	#$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"代表執行不正常.
2443
	#$conf["addOnProcessFunc"]=array();
2444
	#$conf["funcToRunWhenIdle"],字串陣列,當沒有事件產生時,要執行的函式名稱,給予參數為array("client"=>所有用戶,"clientCache"=>$result["serverCache"]["clientSide"]["addOnProcessFunc"][$funcToRunWhenIdle],"serverCache"=>$result["serverCache"]["serverSide"]["funcToRunWhenIdle"][$funcToRunWhenIdle]).
2445
	#$conf["funcToRunWhenIdle"]=array();
2446
	#$conf["paramsForFuncToRunWhenIdle"],2維陣列,每個元素代表指定給予funcToRunWhenIdle參數中的指定元素的參數.
2447
	#$conf["paramsForFuncToRunWhenIdle"]=array();
2448
	#$conf["infoToFunction"],陣列,需要增加給addOnProcessFunc跟funcToRunWhenIdle函式的資訊,在函式中其參數的info鍵值.
2449
	#$conf["infoToFunction"]=array("debug"=>$debug);
2450
	#$conf["shutdownStrAddr"],字串,儲存收到用戶傳什麼樣的字串會結束本函式的檔案位置與名稱,預設為 $conf["sock"].".shutdown".
2451
	#$conf["shutdownStrAddr"]="";
2452
	#參考資料:
2453
	#http://php.net/manual/en/function.stream-socket-server.php
2454
	#備註:
2455
	#建構中...
2456
	*/
2457
	public static function internetAndIntranetServer(&$conf){
2458
 
2459
		#初始化要回傳的結果
2460
		$result=array();
2461
 
2462
		#取得當前執行的函數名稱
2463
		$result["function"]=__FUNCTION__;
2464
 
2465
		#如果沒有參數
2466
		if(func_num_args()==0){
2467
 
2468
			#設置執行失敗
2469
			$result["status"]="false";
2470
 
2471
			#設置執行錯誤訊息
2472
			$result["error"]="函數".$result["function"]."需要參數";
2473
 
2474
			#回傳結果
2475
			return $result;
2476
 
2477
			}#if end
2478
 
2479
		#/* 請依據實際狀況使用
2480
		#涵式說明:
2481
		#判斷當前環境為web還是cmd
2482
		#回傳結果:
2483
		#$result,"web"或"cmd"
2484
		if(csInformation::getEnv()==="web"){
2485
 
2486
			#設置執行失敗
2487
			$result["status"]="false";
2488
 
2489
			#設置執行錯誤訊息
2490
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
2491
 
2492
			#回傳結果
2493
			return $result;
2494
 
2495
			}#if end
2496
		#*/
2497
 
2498
		#取得參數
2499
		$result["argu"]=$conf;
2500
 
2501
		#如果 $conf 不為陣列
2502
		if(gettype($conf)!=="array"){
2503
 
2504
			#設置執行失敗
2505
			$result["status"]="false";
2506
 
2507
			#設置執行錯誤訊息
2508
			$result["error"][]="\$conf變數須為陣列形態";
2509
 
2510
			#如果傳入的參數為 null
2511
			if(is_null($conf)){
2512
 
2513
				#設置執行錯誤訊息
2514
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
2515
 
2516
				}#if end
2517
 
2518
			#回傳結果
2519
			return $result;
2520
 
2521
			}#if end
2522
 
2523
		#預設的 unix domain socket addr
2524
		$defaultSockAddr=pathinfo(__FILE__)["dirname"]."/../../../var/qbpwcf/internetAndIntranetServer.sock";
2525
 
2526
		#檢查參數
2527
		#檢查參數
2528
		#函式說明:
2529
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
2530
		#回傳結果:
2531
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
2532
		#$result["error"],執行不正常結束的錯訊息陣列.
2533
		#$result["simpleError"],簡單表示的錯誤訊息.
2534
		#$result["function"],當前執行的函式名稱.
2535
		#$result["argu"],設置給予的參數.
2536
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
2537
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
2538
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
2539
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
2540
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
2541
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
2542
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
2543
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
2544
		#必填參數:
2545
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
2546
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
2547
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
2548
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
2549
		#可省略參數:
2550
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
2551
		#$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array();
2552
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null、any代表不指定變數形態.其中 resource也包含"resource (closed)".
2553
		#$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array();
2554
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
2555
		#$conf["canBeEmptyString"]="false";
2556
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
2557
		#$conf["canNotBeEmpty"]=array();
2558
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
2559
		#$conf["canBeEmpty"]=array();
2560
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
2561
		$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("listenInfo","sockAddr");
2562
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
2563
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("string","string");
2564
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
2565
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("127.0.0.1:54321",$defaultSockAddr);
2566
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
2567
		#$conf["skipableVarDefaultValue"]=array("");
2568
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
2569
		#$conf["disallowAllSkipableVarIsEmpty"]="";
2570
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
2571
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
2572
		#$conf["disallowAllSkipableVarNotExist"],字串,是否不允許每個可省略參數都不存在,預設為"false"代表允許,反之為"true".
2573
		$conf["variableCheck::checkArguments"]["disallowAllSkipableVarNotExist"]="true";
2574
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
2575
		#$conf["arrayCountEqualCheck"][]=array();
2576
		#參考資料:
2577
		#array_keys=>http://php.net/manual/en/function.array-keys.php
2578
		#備註:
2579
		#無.
2580
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
2581
		unset($conf["variableCheck::checkArguments"]);
2582
 
2583
		#如果執行異常
2584
		if($checkArguments["status"]==="false"){
2585
 
2586
			#設置執行失敗
2587
			$result["status"]="false";
2588
 
2589
			#設置執行錯誤訊息
2590
			$result["error"]=$checkArguments;
2591
 
2592
			#回傳結果
2593
			return $result;
2594
 
2595
			}#if end
2596
 
2597
		#如果檢查參數不通過
2598
		if($checkArguments["passed"]==="false"){
2599
 
2600
			#設置執行失敗
2601
			$result["status"]="false";
2602
 
2603
			#設置執行錯誤訊息
2604
			$result["error"]=$checkArguments;
2605
 
2606
			#回傳結果
2607
			return $result;
2608
 
2609
			}#if end
2610
 
2611
		#縮減 socket 的路徑,避免出錯.
2612
		#函式說明:
2613
		#將檔案目錄的絕對位置中的 "../" 剔除變成直觀的路徑.
2614
		#回傳結果:
2615
		#$result["status"],執行是否成功,"true"代表執行成功,"false"代表執行失敗.
2616
		#$result["function"],當前執行的函數.
2617
		#$result["error"],錯誤訊息陣列.
2618
		#$result["argu"],使用者參數.
2619
		#$result["changedPath"],處理完後回傳的目錄字串.
2620
		#$result["oriPath"],原始的路徑字串
2621
		#必填參數:
2622
		#$conf["dirStr"],字串,要處理的檔案目錄字串.
2623
		$conf["stringProcess::changeDirByDotDotSolidus"]["dirStr"]=$conf["sockAddr"];
2624
		#可省略參數:
2625
		#無.
2626
		#參考資料:
2627
		#無.
2628
		#備註:
2629
		#考慮用realpath取代
2630
		$changeDirByDotDotSolidus=stringProcess::changeDirByDotDotSolidus($conf["stringProcess::changeDirByDotDotSolidus"]);
2631
		unset($conf["stringProcess::changeDirByDotDotSolidus"]);
2632
 
2633
		#如果執行異常
2634
		if($changeDirByDotDotSolidus["status"]==="false"){
2635
 
2636
			#設置執行失敗
2637
			$result["status"]="false";
2638
 
2639
			#設置錯誤訊息
2640
			$result["error"]=$changeDirByDotDotSolidus;
2641
 
2642
			#回傳結果
2643
			return $result;
2644
 
2645
			}#if end
2646
 
2647
		#取得整理過後的直觀路徑
2648
		$conf["sockAddr"]=$changeDirByDotDotSolidus["changedPath"];
2649
 
2650
		#取得 socket 的路徑
2651
		#函式說明:
2652
		#將字串特定關鍵字與其後面的內容剔除
2653
		#回傳結果:
2654
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
2655
		#$result["error"],錯誤訊息陣列.
2656
		#$result["warning"],警告訊息鎮列.
2657
		#$result["founded"],有無找到定字串"true"代表有,"false"代表沒有.
2658
		#$result["function"],當前執行的函數名稱.
2659
		#$result["oriStr"],要處理的原始字串內容.
2660
		#$result["content"],處理好的的字串內容.
2661
		#$result["argu"],使用的參數.
2662
		#必填參數:
2663
		#$conf["stringIn"],字串,要處理的字串.
2664
		$conf["stringProcess::delStrAfterKeyWord"]["stringIn"]=$conf["sockAddr"];
2665
		#$conf["keyWord"],字串,特定字串.
2666
		$conf["stringProcess::delStrAfterKeyWord"]["keyWord"]="/";
2667
		#可省略參數:
2668
		#$conf["deleteLastRepeatedOne"],字串,預設為"false";若為"true"則代表連續遇到同 $conf["keyWord"] 的內容,要將移除內容的起點往後移動到為後一個 $conf["keyWord"].
2669
		$conf["stringProcess::delStrAfterKeyWord"]["deleteLastRepeatedOne"]="true";
2670
		#參考資料:
2671
		#無.
2672
		#備註:
2673
		#無.
2674
		$delStrAfterKeyWord=stringProcess::delStrAfterKeyWord($conf["stringProcess::delStrAfterKeyWord"]);
2675
		unset($conf["stringProcess::delStrAfterKeyWord"]);
2676
 
2677
		#如果執行失敗
2678
		if($delStrAfterKeyWord["status"]==="false"){
2679
 
2680
			#設置執行失敗
2681
			$result["status"]="false";
2682
 
2683
			#設置錯誤訊息
2684
			$result["error"][]=$delStrAfterKeyWord;
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
		if($delStrAfterKeyWord["founded"]==="true"){
2699
 
2700
			#確保建立socket的路徑有存在
2701
			#函式說明:
2702
			#確保路徑存在.
2703
			#回傳結果:
2704
			#$result["status"],執行正常與否,"true"代表正常,"false"代表不正常.
2705
			#$result["error"],錯誤訊息陣列.
2706
			#$resutl["function"],當前執行的涵式名稱.
2707
			#$result["path"],建立好的路徑字串.
2708
			#$result["fileName"],檔案名稱,若 $conf["haveFileName"] 為 "true" 則會回傳.
2709
			#$result["argu"],使用的參數.
2710
			#必填參數:
2711
			#$conf["path"],要檢查的路徑
2712
			$conf["fileAccess::validatePath"]["path"]=$delStrAfterKeyWord["content"];
2713
			#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
2714
			$conf["fileAccess::validatePath"]["fileArgu"]=__FILE__;
2715
			#可省略參數:
2716
			#$conf["haveFileName"],字串,"true"代表有$conf["path"]檔案名稱,"false"代表$conf["path"]為純路徑,預設為"false".
2717
			$conf["fileAccess::validatePath"]["haveFileName"]="false";
2718
			#$conf["dirPermission"],字串,新建資料夾的權限設定,預設爲0770,亦即擁有者,同群組者可以讀,寫,存取,其他人僅能存取.
2719
			#$conf["dirPermission"]="";
2720
			#$conf["web"],是否為檔案系統,"true"為網頁路徑,"false"為網頁系統,預設為"false".
2721
			$conf["fileAccess::validatePath"]["web"]="false";
2722
			#參考資料:
2723
			#無.
2724
			#備註:
2725
			#無.
2726
			$validatePath=fileAccess::validatePath($conf["fileAccess::validatePath"]);
2727
			unset($conf["fileAccess::validatePath"]);
2728
 
2729
			#如果執行失敗
2730
			if($validatePath["status"]==="false"){
2731
 
2732
				#設置執行失敗
2733
				$result["status"]="false";
2734
 
2735
				#設置錯誤訊息
2736
				$result["error"][]=$validatePath;
2737
 
2738
				#設置錯誤訊息
2739
				$result["error"][]="建立 unix domain socket(".$conf["sock"].") 失敗";
2740
 
2741
				#印出錯誤訊息
2742
				echo print_r($result,true);
2743
 
2744
				#回傳結果
2745
				return $result;
2746
 
2747
				}#if end
2748
 
2749
			}#if end
2750
 
2751
		#如果同名 unix domain socket file 存在
2752
		if(file_exists($conf["sock"])){
2753
 
2754
			#則移除之.
2755
			unlink($conf["sock"]);
2756
 
2757
			}#if end
2758
 
2759
		#預設的 unix socket addr
2760
		$unixAddr="unix://".$conf["sockAddr"];
2761
 
2762
		#建立 unix domain socket
2763
		$socket=stream_socket_server($unixAddr, $errno, $errstr);
2764
 
2765
		#如果建立 unix domain socket 失敗
2766
		if(!$socket){
2767
 
2768
			#設置執行失敗
2769
			$result["status"]="false";
2770
 
2771
			#設置錯誤訊息
2772
			$result["error"][]=$errstr." (".$errno.")";
2773
 
2774
			#設置錯誤訊息
2775
			$result["error"][]="建立 unix domain socket(".$conf["sock"].") 失敗";
2776
 
2777
			#印出錯誤訊息
2778
			echo print_r($result,true);
2779
 
2780
			#回傳結果
2781
			return $result;
2782
 
2783
			}#if end
2784
 
2785
		#檢查參數
2786
		#檢查參數
2787
		#函式說明:
2788
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
2789
		#回傳結果:
2790
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
2791
		#$result["error"],執行不正常結束的錯訊息陣列.
2792
		#$result["simpleError"],簡單表示的錯誤訊息.
2793
		#$result["function"],當前執行的函式名稱.
2794
		#$result["argu"],設置給予的參數.
2795
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
2796
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
2797
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
2798
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
2799
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
2800
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
2801
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
2802
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
2803
		#必填參數:
2804
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
2805
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
2806
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
2807
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
2808
		#可省略參數:
2809
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
2810
		#$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array();
2811
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null、any代表不指定變數形態.其中 resource也包含"resource (closed)".
2812
		#$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array();
2813
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
2814
		#$conf["canBeEmptyString"]="false";
2815
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
2816
		#$conf["canNotBeEmpty"]=array();
2817
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
2818
		#$conf["canBeEmpty"]=array();
2819
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
2820
		$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("listenInfo","sockAddr");
2821
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
2822
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("string","string");
2823
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
2824
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("127.0.0.1:54321",$defaultSockAddr);
2825
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
2826
		#$conf["skipableVarDefaultValue"]=array("");
2827
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
2828
		#$conf["disallowAllSkipableVarIsEmpty"]="";
2829
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
2830
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
2831
		#$conf["disallowAllSkipableVarNotExist"],字串,是否不允許每個可省略參數都不存在,預設為"false"代表允許,反之為"true".
2832
		$conf["variableCheck::checkArguments"]["disallowAllSkipableVarNotExist"]="true";
2833
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
2834
		#$conf["arrayCountEqualCheck"][]=array();
2835
		#參考資料:
2836
		#array_keys=>http://php.net/manual/en/function.array-keys.php
2837
		#備註:
2838
		#無.
2839
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
2840
		unset($conf["variableCheck::checkArguments"]);
2841
 
2842
		#如果執行異常
2843
		if($checkArguments["status"]==="false"){
2844
 
2845
			#設置執行失敗
2846
			$result["status"]="false";
2847
 
2848
			#設置執行錯誤訊息
2849
			$result["error"]=$checkArguments;
2850
 
2851
			#回傳結果
2852
			return $result;
2853
 
2854
			}#if end
2855
 
2856
		#如果檢查參數不通過
2857
		if($checkArguments["passed"]==="false"){
2858
 
2859
			#設置執行失敗
2860
			$result["status"]="false";
2861
 
2862
			#設置執行錯誤訊息
2863
			$result["error"]=$checkArguments;
2864
 
2865
			#回傳結果
2866
			return $result;
2867
 
2868
			}#if end
2869
 
2870
		#檢查 unix socket 檔案 $conf["sockAddr"] 是否存在.
2871
		#函式說明:
2872
		#檢查多個檔案與資料夾是否存在.
2873
		#回傳的結果:
2874
		#$result["status"],執行正常與否,"true"代表正常,"false"代表不正常.
2875
		#$result["error"],錯誤訊息陣列.
2876
		#$resutl["function"],當前執行的涵式名稱.
2877
		#$result["argu"],使用的參數.
2878
		#$result["allExist"],所有檔案皆存在的識別,"true"代表皆存在,"false"代表沒有全部都存在.
2879
		#$result["varName"][$i],爲第$i個資料夾或檔案的路徑與名稱。
2880
		#$result["varNameFullPath"][$i],爲第$i個資料夾或檔案的完整檔案系統路徑與名稱,如果不存在則代表路徑是網址.
2881
		#$result["varNameWebPath"][$i],為第$i個資料夾或檔案的網址
2882
		#$result["varExist"][$i],爲第$i個資料夾或檔案是否存在,"true"代表存在,"false"代表不存在。
2883
		#必填參數:
2884
		#$conf["fileArray"],陣列字串,要檢查是否存在的檔案有哪些,須爲一維陣列數值。
2885
		$conf["variableCheck::checkArguments"]["fileArray"]=array($conf["sockAddr"]);
2886
		#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
2887
		$conf["variableCheck::checkArguments"]["fileArgu"]=__FILE__;
2888
		#可省略參數:
2889
		#$conf["disableWebSearch"],"字串",是否取消「當檔案找不到時,改用catchWebContent類別的wget函數來檢查檔案是否存在於網路上」的功能,"false"不取消,若要取消該功能請設為"true",若抓到的內容為空字串則會視為檔案不存在,預設為"true".
2890
		#$conf["disableWebSearch"]="false";
2891
		#$conf["userDir"],字串,網頁是否置放於家目錄底下,"true"為是,"false"為不是,預設為"true".
2892
		$conf["variableCheck::checkArguments"]["userDir"]="false";
2893
		#$conf["web"],字串,檔案是放在web就是"true",反之為檔案系統"false",預設為"true".
2894
		$conf["variableCheck::checkArguments"]["web"]="false";
2895
		#參考資料:
2896
		#http://php.net/manual/en/function.file-exists.php
2897
		#http://php.net/manual/en/control-structures.foreach.php
2898
		#備註:
2899
		#函數file_exists檢查的路徑為檔案系統的路徑
2900
		#$result["varName"][$i]結果未實作
2901
		$checkMultiFileExist=fileAccess::checkMultiFileExist($conf["variableCheck::checkArguments"]);
2902
		unset($conf["variableCheck::checkArguments"]);
2903
 
2904
		#如果檢查失敗
2905
		if($checkMultiFileExist["status"]==="false"){
2906
 
2907
			#設置執行失敗
2908
			$result["status"]="false";
2909
 
2910
			#設置錯誤訊息
2911
			$result["error"]=$checkMultiFileExist;
2912
 
2913
			#回傳結果
2914
			return $result;
2915
 
2916
			}#if end
2917
 
2918
		#如果socket檔案已經存在
2919
		if($checkMultiFileExist["allExist"]==="true"){
2920
 
2921
			#移除之
2922
			unlink($conf["sockAddr"]);
2923
 
2924
			}#if end
2925
 
2926
		#建立 unix domain socket
2927
		$socket=stream_socket_server($unixAddr, $errno, $errstr);
2928
 
2929
		#如果建立 unix domain socket 失敗
2930
		if(!$socket){
2931
 
2932
			#設置執行失敗
2933
			$result["status"]="false";
2934
 
2935
			#設置錯誤訊息
2936
			$result["error"][]=$errstr." (".$errno.")";
2937
 
2938
			#設置錯誤訊息
2939
			$result["error"][]="建立 unix domain socket(".$conf["sock"].") 失敗";
2940
 
2941
			#印出錯誤訊息
2942
			echo print_r($result,true);
2943
 
2944
			#回傳結果
2945
			return $result;
2946
 
2947
			}#if end
2948
 
2949
		#如果建立 unix domain socket 成功
2950
		else{
2951
 
2952
			#產生用於代表結束本函式的字串
2953
			#函式說明:
2954
			#使用 linux 的 uuid 指令來產生 uuid 字串
2955
			#回傳結果:
2956
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
2957
			#$result["error"],錯誤訊息.
2958
			#$result["function"],當前執行的函式名稱.
2959
			#$result["content"],uuid.
2960
			#必填參數:
2961
			#無.
2962
			#可省略參數:
2963
			#無.
2964
			#參考資料:
2965
			#無.
2966
			#備註:
2967
			#無.
2968
			$uuid=cmd::uuid();
2969
 
2970
			#如果執行失敗
2971
			if($uuid["status"]==="false"){
2972
 
2973
				#設置執行失敗
2974
				$result["status"]="false";
2975
 
2976
				#設置錯誤訊息
2977
				$result["error"]=$uuid;
2978
 
2979
				#回傳結果
2980
				return $result;
2981
 
2982
				}#if end
2983
 
2984
			#設置代表結束本服務的uuid字串
2985
			$shutdownStr=$uuid["content"];
2986
 
2987
			#如果沒有設置
2988
			if(!isset($conf["shutdownStrAddr"])){
2989
 
2990
				#初始化存放 shutdownStr 的檔案
2991
				$conf["shutdownStrAddr"]=$conf["sock"].".shutdownStr";
2992
 
2993
				}#if end
2994
 
2995
			#如果檔案位置名稱跟socket一樣
2996
			else if($conf["sock"]===$conf["shutdownStrAddr"]){
2997
 
2998
				#設置執行失敗
2999
				$result["status"]="false";
3000
 
3001
				#設置錯誤訊息
3002
				$result["error"][]="參數shutdownStrAddr跟sock不可一樣";
3003
 
3004
				#設置錯誤訊息
3005
				$result["error"][]=$checkArguments;
3006
 
3007
				#回傳結果
3008
				return $result;
3009
 
3010
				}#else end
3011
 
3012
			#建立裡面儲存若要結束本程式需要接收到什麼樣的字串
3013
			#函式說明:
3014
			#將字串寫入到檔案
3015
			#回傳結果:
3016
			#$result["status"],"true"表示檔案寫入成功,"false"表示檔案寫入失敗.
3017
			#$result["error"],錯誤訊息陣列.
3018
			#$result["function"],當前執行的函數名稱.
3019
			#$result["fileInfo"],實際上寫入的檔案資訊陣列.
3020
			#$result["fileInfo"]["createdFileName"],建立好的檔案名稱.
3021
			#$result["fileInfo"]["createdFilePath"],檔案建立的路徑.
3022
			#$result["fileInfo"]["createdFilePathAndName"].建立好的檔案名稱與路徑.
3023
			#$result["argu"],使用的參數.
3024
			#必填參數:
3025
			#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
3026
			$conf["fileAccess::writeTextIntoFile"]["fileArgu"]=$conf["fileArgu"];
3027
			#可省略參數:
3028
			#$conf["fileName"],字串,爲要編輯的檔案名稱,預設為隨機產生的檔案名稱.
3029
			$conf["fileAccess::writeTextIntoFile"]["fileName"]=$conf["shutdownStrAddr"];
3030
			#$conf["inputString"],字串,爲要寫入到裏面的內容,若要每筆資料寫入後換行,則可以在字串內容後面加上 \r\n 即可,預設為"".
3031
			$conf["fileAccess::writeTextIntoFile"]["inputString"]=$shutdownStr;
3032
			#$conf["writeMethod"],字串,爲檔案撰寫的方式,可省略,是複寫'a'還是,重新寫入'w',預設爲'w',重新寫入.
3033
			#$conf["writeMethod"]="a";
3034
			#$conf["checkRepeat"],字串,"true"代表建立檔案之前要先檢查檔案是否存在,若存在則在原名稱後面加上從(1)開始的編號.
3035
			#$conf["checkRepeat"]="";
3036
			#$conf["filenameExtensionStartPoint"],字串,檔案的副檔名是從倒數第幾個小數點(dot)開始,預設為"1",最後一個小數點,必須與$conf["checkRepeat"]搭配才會生效.
3037
			#$conf["filenameExtensionStartPoint"]="";
3038
			#$conf["repeatNameRule"],字串,遇到相同名稱的檔案要如何加上識別的編號,編號用「\$i」表示,預設為"(\$i)",必須與$conf["checkRepeat"]搭配才會生效.
3039
			#$conf["repeatNameRule"]="";
3040
			#$conf["web"],檔案是否位於網站上"true",若是在檔案系統則為"false",預設為"true".
3041
			$conf["fileAccess::writeTextIntoFile"]["web"]="false";
3042
			#參考資料:
3043
			#無.
3044
			#備註:
3045
			#無.
3046
			$writeTextIntoFile=fileAccess::writeTextIntoFile($conf["fileAccess::writeTextIntoFile"]);
3047
			unset($conf["fileAccess::writeTextIntoFile"]);
3048
 
3049
			#如果執行失敗
3050
			if($writeTextIntoFile["status"]==="false"){
3051
 
3052
				#設置執行失敗
3053
				$result["status"]="false";
3054
 
3055
				#設置錯誤訊息
3056
				$result["error"]=$writeTextIntoFile;
3057
 
3058
				#回傳結果
3059
				return $result;
3060
 
3061
				}#if end
3062
 
3063
			#如果有設置$conf["changeOwner"]
3064
			if(isset($conf["changeOwner"])){
3065
 
3066
				#如果裡面的"."不是一個
3067
				if(substr_count($conf["changeOwner"],".")!==1){
3068
 
3069
					#設置執行失敗
3070
					$result["status"]="false";
3071
 
3072
					#設置錯誤訊息
3073
					$result["error"][]="參數 changeOwner 應為 username.groupname";
3074
 
3075
					#印出錯誤訊息
3076
					echo print_r($result,true);
3077
 
3078
					#回傳結果
3079
					return $result;
3080
 
3081
					}#if end
3082
 
3083
				#改變檔案的擁有者
3084
				#函式說明:
3085
				#使用 linux 的 chown 指令來修改目標檔案或目錄的擁有者跟群組擁有者資訊.
3086
				#回傳結果:
3087
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
3088
				#$result["error"],錯誤訊息.
3089
				#$result["function"],當前執行的函式名稱.
3090
				#$result["cmd"],執行的指令.
3091
				#$result["content"],執行的結果陣列,如果參數 "recursive" 跟 "excludeSelf" 都有設定的話,就會回傳該結果.
3092
				#必填參數:
3093
				#$conf["owner"],字串,要變哪個使用者擁有.
3094
				$conf["cmd::chown"]["owner"]=explode(".",$conf["changeOwner"])[0];
3095
				#$conf["target"],字串,需要變更擁有者、 群組的目標.
3096
				$conf["cmd::chown"]["target"]=$conf["sock"];
3097
				#可省略參數:
3098
				#$conf["group"],字串,要變成什麼群組擁有,預設跟"owner"一樣.
3099
				$conf["cmd::chown"]["group"]=explode(".",$conf["changeOwner"])[1];
3100
				#$conf["recursive"],字串,"true"代表目標目錄底下的內容都要套用,預設為"false".
3101
				#$conf["recursive"]="true";
3102
				#$conf["excludeSelf"],字串,預設為"false"代表不處理;若為"true"則會排除目標自己(資料夾).
3103
				#$conf["excludeSelf"]="true";
3104
				#參考資料:
3105
				#無.
3106
				#備註:
3107
				#無.
3108
				$chown=cmd::chown($conf["cmd::chown"]);
3109
				unset($conf["cmd::chown"]);
3110
 
3111
				#如果改變檔案擁有者限失敗
3112
				if($chown===false){
3113
 
3114
					#設置執行失敗
3115
					$result["status"]="false";
3116
 
3117
					#設置錯誤訊息
3118
					$result["error"][]="改變 ".$conf["sock"]." 檔案的擁有資訊為 ".explode(".",$conf["changeOwner"])[0]." 失敗,通常只有系統帳戶才能變更之.";
3119
 
3120
					#印出錯誤訊息
3121
					echo print_r($result,true);
3122
 
3123
					#回傳結果
3124
					return $result;
3125
 
3126
					}#if end
3127
 
3128
				}#if end
3129
 
3130
			#如果有設置 $conf["changePermission"]
3131
			if(isset($conf["changePermission"])){
3132
 
3133
				#函式說明:
3134
				#使用 linux 的 chmod 指令來修改目標檔案或目錄的權限.
3135
				#回傳結果:
3136
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
3137
				#$result["error"],錯誤訊息.
3138
				#$result["function"],當前執行的函式名稱.
3139
				#$result["cmd"],執行的指令.
3140
				#$result["content"],執行的結果陣列,如果參數 "recursive" 跟 "excludeSelf" 都有設定的話,就會回傳該結果.
3141
				#必填參數:
3142
				#$conf["mode"],字串,要變成什麼權限.
3143
				$conf["cmd::chmod"]["mode"]=$conf["changePermission"];
3144
				#$conf["target"],字串,需要變更權限的目標.
3145
				$conf["cmd::chmod"]["target"]=$conf["sock"];
3146
				#可省略參數:
3147
				#$conf["recursive"],字串,"true"代表目標目錄底下的內容都要套用,預設為"false".
3148
				#$conf["recursive"]="true";
3149
				#$conf["excludeSelf"],字串,預設為"false"代表不處理;若為"true"則會排除目標自己(資料夾).
3150
				#$conf["excludeSelf"]="true";
3151
				#參考資料:
3152
				#無.
3153
				#備註:
3154
				#無.
3155
				$chmod=cmd::chmod($conf["cmd::chmod"]);
3156
				unset($conf["cmd::chmod"]);
3157
 
3158
				#如果變更檔案權限失敗
3159
				if($chmod["status"]==="false"){
3160
 
3161
					#設置執行失敗
3162
					$result["status"]="false";
3163
 
3164
					#設置錯誤訊息
3165
					$result["error"]=$chmod;
3166
 
3167
					#印出錯誤訊息
3168
					echo print_r($result,true);
3169
 
3170
					#回傳結果
3171
					return $result;
3172
 
3173
					}#if end
3174
 
3175
				}#if end
3176
 
3177
			#初始化儲存 server 資訊的變數,預設數值為 $result 自己
3178
			$result["intranet"]["serverCache"]=&$result;
3179
 
3180
			#初始化方便識別 server 資訊的變數
3181
			$intranetServerCache=&$result["intranet"]["serverCache"];
3182
 
3183
			#初始化 server site cache
3184
			$intranetServerCache["serverSide"]=array();
3185
 
3186
			#初始化 儲存 透過 proc 執行的子程序
3187
			$intranetServerCache["serverSide"]["procs"]=array();
3188
 
3189
			#初始化 client site cache
3190
			$intranetServerCache["clientSide"]=array();
3191
 
3192
			#初始化方便識別 client site cache 資訊的變數
3193
			$intranetClientCache=&$intranetServerCache["clientSide"];
3194
 
3195
			#初始化儲存unix domain socket clients的陣列
3196
			$intranetClients=array();
3197
 
3198
			}#else end
3199
 
3200
		#程式不會 timeout
3201
		set_time_limit(0);
3202
 
3203
		#Turn on implicit output flushing so we see what we're getting as it comes in.
3204
		ob_implicit_flush();
3205
 
3206
		#解析 $ip 跟 $port
3207
		#函式說明:
3208
		#尋找字串中是否含有符合格式的內容,且回傳解析好的變數數值.
3209
		#回傳結果:
3210
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
3211
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
3212
		#$result["function"],當前執行的函式名稱.
3213
		#$result["argu"],所使用的參數.
3214
		#$result["found"],是否有找到符合格式的字串內容,"true"代表有找到,"false"代表沒有找到.
3215
		#$result["content"],陣列,若為n個${*},則當found為"true"時,就會回傳n個元素.
3216
		#$result["parsedVar"][varName],陣列,解析好的變數陣列,varName為${}中的內容,變數的key從0開始.
3217
		#必填參數:
3218
		#$conf["input"],字串,要檢查的字串.
3219
		$conf["search::findSpecifyStrFormat"]["input"]=$conf["listenInfo"];
3220
		#$conf["format"],格式字串,要尋找的格式字串.格式為固定的字串("fixedStr format")與變數("${keyWordVarName}")組成.
3221
		$conf["search::findSpecifyStrFormat"]["format"]="${ip}:${port}";
3222
		#可省略參數:
3223
		#$conf["varEqual"],陣列,變數對應的數值,null代表不指定,其他內容代表該變數解析出來必須要為該內容.
3224
		#$conf["varEqual"]=array(null,"found");
3225
		#$conf["varCon"],陣列,每個varEqual為null者,其是否有其他條件,預設為null代表無其他條件,條件的表示是用陣列的key與value來表達,例如:array("no_tail"=>" not"),就代表變數的結尾不能為" not",可以用的key有"head",代表開頭要有什麼;"no_head",代表不能為什麼開頭;"tail",代表要什麼結尾;"no_tail",代表不能什麼結尾.
3226
		#$conf["varCon"]=array("no_tail"=>" not");
3227
		#參考資料:
3228
		#無.
3229
		#備註:
3230
		#無.
3231
		$findSpecifyStrFormat=search::findSpecifyStrFormat($conf["search::findSpecifyStrFormat"]);
3232
		unset($conf["search::findSpecifyStrFormat"]);
3233
 
3234
		#如果執行異常
3235
		if($findSpecifyStrFormat["status"]==="false"){
3236
 
3237
			#設置執行異常識別
3238
			$result["status"]="false";
3239
 
3240
			#設置執行錯誤訊息
3241
			$result["error"]=$findSpecifyStrFormat;
3242
 
3243
			#回傳結果
3244
			return $return;
3245
 
3246
			}#if end
3247
 
3248
		#如果沒有符合格式的內容
3249
		if($findSpecifyStrFormat["found"]==="false"){
3250
 
3251
			#設置執行異常識別
3252
			$result["status"]="false";
3253
 
3254
			#設置執行錯誤訊息
3255
			$result["error"]=$findSpecifyStrFormat;
3256
 
3257
			#回傳結果
3258
			return $return;
3259
 
3260
			}#if end
3261
 
3262
		#set listen ip and port
3263
		$address = $findSpecifyStrFormat["parsedVar"]["ip"][0];
3264
 
3265
		#set listen port
3266
		$port = $findSpecifyStrFormat["parsedVar"]["port"][0];
3267
 
3268
		#如果建立 socket 失敗
3269
		if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
3270
 
3271
			#設置錯誤訊息
3272
			$errorMsg="socket_create() failed: reason: " . socket_strerror(socket_last_error()) . PHP_EOL;
3273
 
3274
			#設置執行異常識別
3275
			$result["status"]="false";
3276
 
3277
			#設置執行錯誤訊息
3278
			$result["error"]=$errorMsg;
3279
 
3280
			#回傳結果
3281
			return $return;
3282
 
3283
			}#if end
3284
 
3285
		#如果 bind socket 失敗
3286
		if (socket_bind($sock, $address, $port) === false) {
3287
 
3288
			#設置錯誤訊息
3289
			$errorMsg="socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . PHP_EOL;
3290
 
3291
			#設置執行異常識別
3292
			$result["status"]="false";
3293
 
3294
			#設置執行錯誤訊息
3295
			$result["error"]=$errorMsg;
3296
 
3297
			#回傳結果
3298
			return $return;
3299
 
3300
			}#if end
3301
 
3302
		#如果 socket listen 超過 5 秒仍未成功
3303
		if (socket_listen($sock, 5) === false) {
3304
 
3305
			#設置錯誤訊息
3306
			$errorMsg="socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . PHP_EOL;
3307
 
3308
			#設置執行異常識別
3309
			$result["status"]="false";
3310
 
3311
			#設置執行錯誤訊息
3312
			$result["error"]=$errorMsg;
3313
 
3314
			#回傳結果
3315
			return $return;
3316
 
3317
			}#if end
3318
 
3319
		#初始化儲存 server 資訊的變數,預設數值為 $result 自己
3320
		$result["internet"]["serverCache"]=&$result;
3321
 
3322
		#初始化方便識別 server 資訊的變數
3323
		$internetServerCache=&$result["internet"]["serverCache"];
3324
 
3325
		#初始化 server site cache
3326
		$internetServerCache["serverSide"]=array();
3327
 
3328
		#初始化 儲存 透過 proc 執行的子程序
3329
		$internetServerCache["serverSide"]["procs"]=array();
3330
 
3331
		#初始化 client site cache
3332
		$internetServerCache["clientSide"]=array();
3333
 
3334
		#初始化方便識別 client site cache 資訊的變數
3335
		$internetClientCache=&$internetServerCache["clientSide"];
3336
 
3337
		#create a list of all the clients that will be connected to us..
3338
		#add the listening socket to this list
3339
		$internetClients = array("serverSock"=>$sock);
3340
 
3341
		#初始化 client 陣列
3342
		$client=array("intranet"=>$intranetClients,"internet"=>$internetClients);
3343
 
3344
		#設置無窮圈圈
3345
		while(true){
3346
 
3347
 
3348
 
3349
			}#while end
3350
 
3351
		}#function internetAndIntranetServer end
3352
 
3353
	/*
3354
	#函式說明:
3 liveuser 3355
	#連線到 unixDomainSockServer 提供的 unix domain socket.
3356
	#回傳結果:
3357
	#$result["status"],"true"代表執行正常;"false"代表執行不正常.
3358
	#$result["error"],錯誤訊息陣列.
3359
	#$result["function"],當前執行的函式名稱.
3360
	#$result["content"],取得的回應,讀到EOL,才會結束.
3361
	#必填參數:
3362
	#$conf["sock"],字串,要連線的unix domain socket.
3363
	$conf["sock"]="";
3364
	#可省略參數:
3365
	#$conf["id"],字串,取得的id,若無此值,則會得到新的數值.
3366
	#$conf["id"]="";
3367
	#$conf["cmd"],字串,要執行的指令,當$conf["id"]參數合法時,才會執行.
3368
	#$conf["cmd"]="";
3369
	#$conf["param"],參數陣列.
3370
	#$conf["param"]=array();
3371
	#$conf["escaped"],字串,param參數是否已經escaped了,預設為"false",反之為"true".
3372
	#$conf["escaped"]="true";
3373
	#$conf["custom"],陣列,要客制化傳輸的內容,會覆蓋以上除了$conf["id"]以外的可省略參數.
3374
	#$conf["custom"]=array();
3375
	#$conf["raw"],字串,要直接傳送的字串內容,會忽略其他可省略參數.
3376
	#$conf["raw"]="";
3377
	#參考資料:
3378
	#http://php.net/manual/en/function.stream-socket-client.php
3379
	#http://php.net/manual/en/function.stream-get-contents.php
3380
	#備註:
3381
	#無.
3382
	*/
3383
	public static function unixDomainSockClient(&$conf){
226 liveuser 3384
 
3 liveuser 3385
		#初始化要回傳的結果
3386
		$result=array();
3387
 
3388
		#取得當前執行的函數名稱
3389
		$result["function"]=__FUNCTION__;
226 liveuser 3390
 
3 liveuser 3391
		#如果沒有參數
3392
		if(func_num_args()==0){
226 liveuser 3393
 
3 liveuser 3394
			#設置執行失敗
3395
			$result["status"]="false";
226 liveuser 3396
 
3 liveuser 3397
			#設置執行錯誤訊息
3398
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 3399
 
3 liveuser 3400
			#回傳結果
3401
			return $result;
226 liveuser 3402
 
3 liveuser 3403
			}#if end
3404
 
3405
		#取得參數
3406
		$result["argu"]=$conf;
3407
 
3408
		#如果 $conf 不為陣列
3409
		if(gettype($conf)!="array"){
226 liveuser 3410
 
3 liveuser 3411
			#設置執行失敗
3412
			$result["status"]="false";
226 liveuser 3413
 
3 liveuser 3414
			#設置執行錯誤訊息
3415
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 3416
 
3 liveuser 3417
			#如果傳入的參數為 null
3418
			if($conf==null){
226 liveuser 3419
 
3 liveuser 3420
				#設置執行錯誤訊息
3421
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 3422
 
3 liveuser 3423
				}#if end
3424
 
3425
			#回傳結果
3426
			return $result;
226 liveuser 3427
 
3 liveuser 3428
			}#if end
226 liveuser 3429
 
3 liveuser 3430
		#檢查參數
3431
		#函式說明:
3432
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
3433
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
3434
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
3435
		#$result["function"],當前執行的函式名稱.
3436
		#$result["argu"],設置給予的參數.
3437
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
3438
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
3439
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
3440
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
3441
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
3442
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
3443
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
3444
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
3445
		#必填寫的參數:
3446
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
226 liveuser 3447
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
3 liveuser 3448
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
3449
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
3450
		#可以省略的參數:
3451
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
3452
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("sock");
3453
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
3454
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string");
3455
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
3456
		#$conf["canBeEmptyString"]="false";
3457
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
3458
		#$conf["canNotBeEmpty"]=array();
3459
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
3460
		#$conf["canBeEmpty"]=array();
3461
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
3462
		$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("id","cmd","param","escaped","custom");
3463
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
3464
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("id","cmd","param","escaped","custom","raw");
226 liveuser 3465
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 3466
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string","array","string","array","string");
3467
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
3468
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(null,null,null,"false",null,null);
3469
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
3470
		#$conf["disallowAllSkipableVarIsEmpty"]="";
3471
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
3472
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
3473
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
3474
		#$conf["arrayCountEqualCheck"][]=array();
3475
		#參考資料來源:
3476
		#array_keys=>http://php.net/manual/en/function.array-keys.php
3477
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
3478
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 3479
 
3 liveuser 3480
		#如果檢查參數失敗
3481
		if($checkArguments["status"]==="false"){
226 liveuser 3482
 
3 liveuser 3483
			#設置執行失敗
3484
			$result["status"]="false";
226 liveuser 3485
 
3 liveuser 3486
			#設置錯誤訊息
3487
			$result["error"]=$checkArguments;
226 liveuser 3488
 
3 liveuser 3489
			#回傳結果
3490
			return $result;
226 liveuser 3491
 
3 liveuser 3492
			}#if end
226 liveuser 3493
 
3 liveuser 3494
		#如果檢查參數不通過
3495
		if($checkArguments["passed"]==="false"){
226 liveuser 3496
 
3 liveuser 3497
			#設置執行失敗
3498
			$result["status"]="false";
226 liveuser 3499
 
3 liveuser 3500
			#設置錯誤訊息
3501
			$result["error"]=$checkArguments;
226 liveuser 3502
 
3 liveuser 3503
			#回傳結果
3504
			return $result;
226 liveuser 3505
 
3 liveuser 3506
			}#if end
226 liveuser 3507
 
3 liveuser 3508
		#檢查 unix socket 檔案 $conf["sock"] 是否存在.
3509
		#函式說明:
3510
		#檢查多個檔案與資料夾是否存在.
3511
		#回傳的結果:
3512
		#$result["status"],執行正常與否,"true"代表正常,"false"代表不正常.
3513
		#$result["error"],錯誤訊息陣列.
3514
		#$resutl["function"],當前執行的涵式名稱.
3515
		#$result["argu"],使用的參數.
3516
		#$result["allExist"],所有檔案皆存在的識別,"true"代表皆存在,"false"代表沒有全部都存在.
3517
		#$result["varName"][$i],爲第$i個資料夾或檔案的路徑與名稱。
3518
		#$result["varNameFullPath"][$i],爲第$i個資料夾或檔案的完整檔案系統路徑與名稱,如果不存在則代表路徑是網址.
3519
		#$result["varNameWebPath"][$i],為第$i個資料夾或檔案的網址
3520
		#$result["varExist"][$i],爲第$i個資料夾或檔案是否存在,"true"代表存在,"false"代表不存在。
3521
		#必填參數:
3522
		#$conf["fileArray"],陣列字串,要檢查是否存在的檔案有哪些,須爲一維陣列數值。
3523
		$conf["variableCheck::checkArguments"]["fileArray"]=array($conf["sock"]);
3524
		#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
3525
		$conf["variableCheck::checkArguments"]["fileArgu"]=__FILE__;
3526
		#可省略參數:
3527
		#$conf["disableWebSearch"],"字串",是否取消「當檔案找不到時,改用catchWebContent類別的wget函數來檢查檔案是否存在於網路上」的功能,"false"不取消,若要取消該功能請設為"true",若抓到的內容為空字串則會視為檔案不存在,預設為"true".
3528
		#$conf["disableWebSearch"]="false";
3529
		#$conf["userDir"],字串,網頁是否置放於家目錄底下,"true"為是,"false"為不是,預設為"true".
3530
		$conf["variableCheck::checkArguments"]["userDir"]="false";
3531
		#$conf["web"],字串,檔案是放在web就是"true",反之為檔案系統"false",預設為"true".
3532
		$conf["variableCheck::checkArguments"]["web"]="false";
3533
		#參考資料:
3534
		#http://php.net/manual/en/function.file-exists.php
3535
		#http://php.net/manual/en/control-structures.foreach.php
3536
		#備註:
3537
		#函數file_exists檢查的路徑為檔案系統的路徑
3538
		#$result["varName"][$i]結果未實作
3539
		$checkMultiFileExist=fileAccess::checkMultiFileExist($conf["variableCheck::checkArguments"]);
3540
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 3541
 
3 liveuser 3542
		#如果檢查失敗
3543
		if($checkMultiFileExist["status"]==="false"){
226 liveuser 3544
 
3 liveuser 3545
			#設置執行失敗
3546
			$result["status"]="false";
226 liveuser 3547
 
3 liveuser 3548
			#設置錯誤訊息
3549
			$result["error"]=$checkMultiFileExist;
226 liveuser 3550
 
3 liveuser 3551
			#回傳結果
3552
			return $result;
226 liveuser 3553
 
3 liveuser 3554
			}#if end
226 liveuser 3555
 
3 liveuser 3556
		#如果檢查失敗
3557
		if($checkMultiFileExist["allExist"]==="false"){
226 liveuser 3558
 
3 liveuser 3559
			#設置執行失敗
3560
			$result["status"]="false";
226 liveuser 3561
 
3 liveuser 3562
			#設置錯誤訊息
3563
			$result["error"]=$checkMultiFileExist;
226 liveuser 3564
 
3 liveuser 3565
			#回傳結果
3566
			return $result;
226 liveuser 3567
 
3 liveuser 3568
			}#if end
226 liveuser 3569
 
3 liveuser 3570
		#初始化取得的回應
3571
		$result["content"]="";
226 liveuser 3572
 
3 liveuser 3573
		#debug
3574
		#var_dump(__LINE__,"before connect");
226 liveuser 3575
 
3 liveuser 3576
		#嘗試30秒內連線到 unix domain socket
3577
		$fp=@stream_socket_client("unix://".$conf["sock"], $errno, $errstr, 30, STREAM_CLIENT_ASYNC_CONNECT );
226 liveuser 3578
 
3 liveuser 3579
		#如果開啟 unix domain socket 失敗
3580
		if(!$fp){
226 liveuser 3581
 
3 liveuser 3582
			#設置執行錯誤識別
3583
			$result["status"]="false";
226 liveuser 3584
 
3 liveuser 3585
			#設置錯誤訊息
3586
			$result["error"]=$errstr." (".$errno.")";
226 liveuser 3587
 
3 liveuser 3588
			#回傳結果
3589
			return $result;
226 liveuser 3590
 
3 liveuser 3591
			}#if end
226 liveuser 3592
 
3 liveuser 3593
		#反之開啟 unix domain socket 成功,且沒有raw參數存在
3594
		else if(!isset($conf["raw"])){
226 liveuser 3595
 
3 liveuser 3596
			#初始化要傳送的要求
3597
			$req=array();
226 liveuser 3598
 
3 liveuser 3599
			#如果有設置 $conf["id"]
3600
			if(isset($conf["id"])){
226 liveuser 3601
 
3 liveuser 3602
				#設置識別自己的id
3603
				$req["id"]=$conf["id"];
226 liveuser 3604
 
3 liveuser 3605
				}#if end
226 liveuser 3606
 
3 liveuser 3607
			#如果有設置 $conf["cmd"]
3608
			if(isset($conf["cmd"])){
226 liveuser 3609
 
3 liveuser 3610
				#設置要執行的指令
3611
				$req["cmd"]=$conf["cmd"];
226 liveuser 3612
 
3 liveuser 3613
				}#if end
226 liveuser 3614
 
3 liveuser 3615
			#如果有設置 $conf["param"]
3616
			if(isset($conf["param"])){
226 liveuser 3617
 
3 liveuser 3618
				#設置要執行的指令參數
3619
				$req["param"]=$conf["param"];
226 liveuser 3620
 
3 liveuser 3621
				#設置參數是否已經escaped
3622
				$req["escaped"]=$conf["escaped"];
226 liveuser 3623
 
3 liveuser 3624
				}#if end
226 liveuser 3625
 
3 liveuser 3626
			#如果有設置 $conf["custom"]
3627
			if(isset($conf["custom"])){
226 liveuser 3628
 
3 liveuser 3629
				#用 $conf["custom"] 來取代 $req
3630
				$req=$conf["custom"];
226 liveuser 3631
 
3 liveuser 3632
				#如果有設置 $conf["id"]
3633
				if(isset($conf["id"])){
226 liveuser 3634
 
3 liveuser 3635
					#設置識別自己的id
3636
					$req["id"]=$conf["id"];
226 liveuser 3637
 
3 liveuser 3638
					}#if end
226 liveuser 3639
 
3 liveuser 3640
				}#if end
226 liveuser 3641
 
3 liveuser 3642
			#debug
3643
			#var_dump(__LINE__,"before write ".json_encode($req));
226 liveuser 3644
 
3 liveuser 3645
			#傳送要求
3646
			fwrite($fp,json_encode($req).PHP_EOL);
226 liveuser 3647
 
3 liveuser 3648
			#結束要求
3649
			fwrite($fp,PHP_EOL.PHP_EOL);
226 liveuser 3650
 
3651
			#如果回應為空
3 liveuser 3652
			while(strlen($result["content"])<2){
226 liveuser 3653
 
3 liveuser 3654
				#當有回應時
3655
				while(!feof($fp)){
226 liveuser 3656
 
3 liveuser 3657
					#讀取回應
3658
					$tmp=fgets($fp, 1024);
226 liveuser 3659
 
3 liveuser 3660
					#debug
3661
					#var_dump($tmp);
226 liveuser 3662
 
3 liveuser 3663
					#debug
3664
					#var_dump(__LINE__,"got ".$tmp);
226 liveuser 3665
 
3 liveuser 3666
					#如果讀到 PHP_EOL
3667
					if($tmp===PHP_EOL){
226 liveuser 3668
 
3 liveuser 3669
						#結束讀取回應
3670
						break 2;
226 liveuser 3671
 
3 liveuser 3672
						}#if end
226 liveuser 3673
 
3 liveuser 3674
					#串接回應
3675
					$result["content"]=$result["content"].$tmp;
226 liveuser 3676
 
3 liveuser 3677
					}#while end
226 liveuser 3678
 
3 liveuser 3679
				}#while end
226 liveuser 3680
 
3 liveuser 3681
			#關閉 socket
3682
			fclose($fp);
226 liveuser 3683
 
3 liveuser 3684
			}#else end
226 liveuser 3685
 
3 liveuser 3686
		#反之有有raw參數存在
3687
		else{
226 liveuser 3688
 
3 liveuser 3689
			#傳送要求
3690
			fwrite($fp,trim($conf["raw"]).PHP_EOL);
226 liveuser 3691
 
3 liveuser 3692
			#當有回應時
3693
			while(!feof($fp)){
226 liveuser 3694
 
3 liveuser 3695
				#讀取回應
3696
				$tmp=fgets($fp, 1024);
226 liveuser 3697
 
3 liveuser 3698
				#debug
3699
				#var_dump($tmp);
226 liveuser 3700
 
3 liveuser 3701
				#debug
3702
				#var_dump(__LINE__,"got ".$tmp);
226 liveuser 3703
 
3 liveuser 3704
				#如果讀到 PHP_EOL
3705
				if($tmp===PHP_EOL){
226 liveuser 3706
 
3 liveuser 3707
					#結束讀取回應
226 liveuser 3708
					break;
3709
 
3 liveuser 3710
					}#if end
226 liveuser 3711
 
3 liveuser 3712
				#串接回應
3713
				$result["content"]=$result["content"].$tmp;
226 liveuser 3714
 
3 liveuser 3715
				}#while end
226 liveuser 3716
 
3 liveuser 3717
			#關閉 socket
226 liveuser 3718
			fclose($fp);
3719
 
3 liveuser 3720
			}#else end
226 liveuser 3721
 
3 liveuser 3722
		#設置執行正常
3723
		$result["status"]="true";
226 liveuser 3724
 
3 liveuser 3725
		#回傳結果
3726
		return $result;
226 liveuser 3727
 
3 liveuser 3728
		}#function unixDomainSockClient end
226 liveuser 3729
 
3 liveuser 3730
	/*
3731
	#函式說明:
3732
	#連線到 usr/bin/qbpwcf-usock.php 產生的  unix domain socket,運行指定的指令.
3733
	#回傳結果:
3734
	#$result["status"],"true"代表執行正常;"false"代表執行不正常.
3735
	#$result["error"],錯誤訊息陣列.
3736
	#$result["function"],當前執行的函式名稱.
3737
	#$result["argu"],使用的參數.
121 liveuser 3738
	#$result["content"],執行完指令的結果,回傳的格式為external::callShell的回傳結果.
3 liveuser 3739
	#必填參數:
3740
	#$conf["fileArgu"],字串,變數__FILE__的內容.
3741
	$conf["fileArgu"]=__FILE__;
3742
	#$conf["command"],字串,要執行的指令名稱.
3743
	$conf["command"]="";
3744
	#可省略參數:
3745
	#$conf["sock"],字串,要連線的 usr/bin/qbpwcf-sock.php(sock::unixDomainSockServer) 所產生的 unix domain socket 路徑與名稱,預設為 qbpwcf_usock_path.
3746
	#$conf["sock"]=qbpwcf_usock_path;
3747
	#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
3748
	#$conf["argu"]=array();
3749
	#$conf["commandIncludeArgu"],字串,是否command含有參數,預設為"false"代表沒有;反之為"true".
3750
	#$conf["commandIncludeArgu"]="false";
3751
	#$conf["commandInBg"],字串,是否要將程序放在背景執行,再取得相關資訊,預設為"false"代表不要;反之為"true".
3752
	#$conf["commandInBg"]="false";
3753
	#參考資料:
3754
	#無.
3755
	#備註:
3756
	#無.
3757
	*/
3758
	public static function execAnyCmdByQBPWCFunixSocket(&$conf){
226 liveuser 3759
 
3 liveuser 3760
		#初始化要回傳的結果
3761
		$result=array();
3762
 
3763
		#取得當前執行的函數名稱
3764
		$result["function"]=__FUNCTION__;
226 liveuser 3765
 
3 liveuser 3766
		#如果沒有參數
3767
		if(func_num_args()==0){
226 liveuser 3768
 
3 liveuser 3769
			#設置執行失敗
3770
			$result["status"]="false";
226 liveuser 3771
 
3 liveuser 3772
			#設置執行錯誤訊息
3773
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 3774
 
3 liveuser 3775
			#回傳結果
3776
			return $result;
226 liveuser 3777
 
3 liveuser 3778
			}#if end
226 liveuser 3779
 
3 liveuser 3780
		#取得參數
3781
		$result["argu"]=$conf;
3782
 
3783
		#如果 $conf 不為陣列
3784
		if(gettype($conf)!="array"){
226 liveuser 3785
 
3 liveuser 3786
			#設置執行失敗
3787
			$result["status"]="false";
226 liveuser 3788
 
3 liveuser 3789
			#設置執行錯誤訊息
3790
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 3791
 
3 liveuser 3792
			#如果傳入的參數為 null
3793
			if($conf==null){
226 liveuser 3794
 
3 liveuser 3795
				#設置執行錯誤訊息
3796
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 3797
 
3 liveuser 3798
				}#if end
3799
 
3800
			#回傳結果
3801
			return $result;
226 liveuser 3802
 
3 liveuser 3803
			}#if end
226 liveuser 3804
 
3 liveuser 3805
		#檢查參數
3806
		#函式說明:
3807
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
3808
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
3809
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
3810
		#$result["function"],當前執行的函式名稱.
3811
		#$result["argu"],設置給予的參數.
3812
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
3813
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
3814
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
3815
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
3816
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
3817
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
3818
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
3819
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
3820
		#必填寫的參數:
3821
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
226 liveuser 3822
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
3 liveuser 3823
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
3824
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
3825
		#可以省略的參數:
3826
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
3827
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("fileArgu","command");
3828
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
3829
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string");
3830
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
3831
		#$conf["canBeEmptyString"]="false";
3832
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
3833
		#$conf["canNotBeEmpty"]=array();
3834
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
3835
		#$conf["canBeEmpty"]=array();
3836
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
3837
		$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("sock");
3838
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
3839
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("sock","argu","commandIncludeArgu","commandInBg");
226 liveuser 3840
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 3841
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","array","string","string");
3842
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
3843
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(qbpwcf_usock_path,array(),"false","false");
3844
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
3845
		#$conf["disallowAllSkipableVarIsEmpty"]="";
3846
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
3847
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
3848
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
3849
		#$conf["arrayCountEqualCheck"][]=array();
3850
		#參考資料來源:
3851
		#array_keys=>http://php.net/manual/en/function.array-keys.php
3852
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
3853
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 3854
 
3 liveuser 3855
		#如果檢查參數失敗
3856
		if($checkArguments["status"]==="false"){
226 liveuser 3857
 
3 liveuser 3858
			#設置執行失敗
3859
			$result["status"]="false";
226 liveuser 3860
 
3 liveuser 3861
			#設置錯誤訊息
3862
			$result["error"]=$checkArguments;
226 liveuser 3863
 
3 liveuser 3864
			#回傳結果
3865
			return $result;
226 liveuser 3866
 
3 liveuser 3867
			}#if end
226 liveuser 3868
 
3 liveuser 3869
		#如果檢查參數不通過
3870
		if($checkArguments["passed"]==="false"){
226 liveuser 3871
 
3 liveuser 3872
			#設置執行失敗
3873
			$result["status"]="false";
226 liveuser 3874
 
3 liveuser 3875
			#設置錯誤訊息
3876
			$result["error"]=$checkArguments;
226 liveuser 3877
 
3 liveuser 3878
			#回傳結果
3879
			return $result;
226 liveuser 3880
 
3 liveuser 3881
			}#if end
226 liveuser 3882
 
3 liveuser 3883
		#如果指令已經包含參數了
3884
		if($conf["commandIncludeArgu"]==="true"){
226 liveuser 3885
 
3 liveuser 3886
			#函式說明:
226 liveuser 3887
			#將指令字串解析成陣列,方便給予 external::callShell 使用
3 liveuser 3888
			#回傳結果:
3889
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
3890
			#$reuslt["error"],執行不正常結束的錯訊息陣列.
3891
			#$result["function"],當前執行的函式名稱.
3892
			#$result["content"],解析好的指令陣列.
3893
			#$result["cmd"],解析好的指令名稱.
3894
			#$result["argus"],解析好的參數陣列.
3895
			#$result["argu"],所使用的參數.
3896
			#必填參數
3897
			#$conf["cmdStr"],字串,要解析的指令字串
3898
			$conf["cmd::parseCmdString"]["cmdStr"]=$conf["command"];
3899
			#可省略參數:
3900
			#無.
3901
			#參考資料:
3902
			#無.
3903
			#備註:
3904
			#無.
3905
			$parseCmdString=cmd::parseCmdString($conf["cmd::parseCmdString"]);
3906
			unset($conf["cmd::parseCmdString"]);
226 liveuser 3907
 
3 liveuser 3908
			#debug
220 liveuser 3909
			#var_dump(__FUNCTION__,__LINE__,$parseCmdString);exit;
226 liveuser 3910
 
3 liveuser 3911
			#如果執行失敗
3912
			if($parseCmdString["status"]==="false"){
226 liveuser 3913
 
3 liveuser 3914
				#設置執行失敗
3915
				$result["status"]="false";
226 liveuser 3916
 
3 liveuser 3917
				#設置錯誤訊息
3918
				$result["error"]=$parseCmdString;
226 liveuser 3919
 
3 liveuser 3920
				#回傳結果
3921
				return $result;
226 liveuser 3922
 
3 liveuser 3923
				}#if end
226 liveuser 3924
 
3 liveuser 3925
			#取得解析好的指令名稱
3926
			$conf["command"]=$parseCmdString["cmd"];
226 liveuser 3927
 
3 liveuser 3928
			#取得解析好的參數陣列
3929
			$conf["argu"]=$parseCmdString["argus"];
226 liveuser 3930
 
3 liveuser 3931
			#debug
3932
			#var_dump(__LINE__,$parseCmdString);
226 liveuser 3933
 
3 liveuser 3934
			}#if end
226 liveuser 3935
 
3 liveuser 3936
		#函式說明:
3937
		#呼叫shell執行系統命令,並取得回傳的內容.
3938
		#回傳結果:
3939
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
3940
		#$result["error"],錯誤訊息陣列.
3941
		#$result["function"],當前執行的函數名稱.
3942
		#$result["argu"],使用的參數.
3943
		#$result["cmd"],執行的指令內容.
3944
		#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
3945
		#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
3946
		#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
3947
		#$result["running"],是否還在執行.
3948
		#$result["pid"],pid.
3949
		#$result["statusCode"],執行結束後的代碼.
3950
		#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu").
3951
		#必填參數:
3952
		#$conf["command"],字串,要執行的指令.
3953
		$conf["external::callShell"]["command"]=$conf["command"];
3954
		#$conf["fileArgu"],字串,變數__FILE__的內容.
3955
		$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];
3956
		#可省略參數:
3957
		#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
3958
		$conf["external::callShell"]["argu"]=$conf["argu"];
3959
		#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
3960
		#$conf["arguIsAddr"]=array();
3961
		#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
3962
		#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
3963
		#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
3964
		#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
3965
		#$conf["enablePrintDescription"]="true";
3966
		#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
3967
		#$conf["printDescription"]="";
3968
		#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
3969
		$conf["external::callShell"]["escapeshellarg"]="true";
3970
		#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
3971
		#$conf["thereIsShellVar"]=array();
3972
		#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
3973
		#$conf["username"]="";
3974
		#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
3975
		#$conf["password"]="";
3976
		#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
3977
		#$conf["useScript"]="";
3978
		#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
3979
		#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
3980
		#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
3981
		#$conf["inBackGround"]="";
3982
		#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
3983
		#$conf["getErr"]="false";
3984
		#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
3985
		$conf["external::callShell"]["doNotRun"]="true";
3986
		#參考資料:
3987
		#exec=>http://php.net/manual/en/function.exec.php
3988
		#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
3989
		#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
3990
		#備註:
3991
		#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
3992
		#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
3993
		$callShell=external::callShell($conf["external::callShell"]);
3994
		unset($conf["external::callShell"]);
226 liveuser 3995
 
3 liveuser 3996
		#debug
3997
		#var_dump(__LINE__,$callShell);
226 liveuser 3998
 
3 liveuser 3999
		#如果檢查參數作業出錯
4000
		if($callShell["status"]=="false"){
226 liveuser 4001
 
3 liveuser 4002
			#設置執行錯誤識別
4003
			$result["status"]="false";
226 liveuser 4004
 
3 liveuser 4005
			#設置錯誤訊息
4006
			$result["error"]=$callShell;
226 liveuser 4007
 
3 liveuser 4008
			#回傳結果
4009
			return $result;
226 liveuser 4010
 
3 liveuser 4011
			}#if end
226 liveuser 4012
 
3 liveuser 4013
		#函式說明:
4014
		#連線到 unixDomainSockServer 提供的 unix domain socket.
4015
		#回傳結果:
4016
		#$result["status"],"true"代表執行正常;"false"代表執行不正常.
4017
		#$result["error"],錯誤訊息陣列.
4018
		#$result["function"],當前執行的函式名稱.
4019
		#$result["content"],取得的回應.
4020
		#必填參數:
4021
		#$conf["sock"],字串,要連線的unix domain socket.
4022
		$conf["sock::unixDomainSockClient"]["sock"]=$conf["sock"];
4023
		#可省略參數:
4024
		#$conf["id"],字串,取得的id,若無此值,則會得到新的數值.
4025
		#$conf["id"]="";
4026
		#$conf["cmd"],字串,要執行的指令,當$conf["id"]參數合法時,才會執行.
259 liveuser 4027
		$conf["sock::unixDomainSockClient"]["cmd"]=$callShell["noEscaped"]["cmd"];
3 liveuser 4028
		#$conf["param"],參數陣列.
259 liveuser 4029
		$conf["sock::unixDomainSockClient"]["param"]=$callShell["noEscaped"]["argu"];
3 liveuser 4030
		#$conf["escaped"],字串,param參數是否已經escaped了,預設為"false",反之為"true".
4031
		$conf["sock::unixDomainSockClient"]["escaped"]="true";
4032
		#$conf["custom"],陣列,要客制化傳輸的內容,會覆蓋以上除了$conf["id"]以外的可省略參數.
226 liveuser 4033
 
3 liveuser 4034
		#如果要在背景執行指令
4035
		if($conf["commandInBg"]==="true" && isset($conf["sock::unixDomainSockClient"]["cmd"]) ){
226 liveuser 4036
 
3 liveuser 4037
			#設定要在背景執行的指令
4038
			$conf["sock::unixDomainSockClient"]["custom"]["cmdInBg"]=$conf["sock::unixDomainSockClient"]["cmd"];
226 liveuser 4039
 
3 liveuser 4040
			#設定要在背景執行的指令
4041
			$conf["sock::unixDomainSockClient"]["custom"]["param"]=$conf["sock::unixDomainSockClient"]["param"];
226 liveuser 4042
 
3 liveuser 4043
			#移除不要的參數
4044
			unset($conf["sock::unixDomainSockClient"]["cmd"]);
226 liveuser 4045
 
3 liveuser 4046
			#移除不要的參數
4047
			unset($conf["sock::unixDomainSockClient"]["param"]);
226 liveuser 4048
 
3 liveuser 4049
			#保存 custom 參數
4050
			$custom=$conf["sock::unixDomainSockClient"]["custom"];
226 liveuser 4051
 
3 liveuser 4052
			#debug
4053
			#var_dump(__LINE__,$custom);
226 liveuser 4054
 
3 liveuser 4055
			}#if end
226 liveuser 4056
 
3 liveuser 4057
		#$conf["raw"],字串,要直接傳送的字串內容,會忽略其他可省略參數.
4058
		#$conf["raw"]="";
4059
		#參考資料:
4060
		#http://php.net/manual/en/function.stream-socket-client.php
4061
		#http://php.net/manual/en/function.stream-get-contents.php
4062
		#備註:
4063
		#無.
4064
		$paramsOfUnixDomainSockClient=$conf["sock::unixDomainSockClient"];
4065
		$unixDomainSockClient=sock::unixDomainSockClient($paramsOfUnixDomainSockClient);
4066
		unset($paramsOfUnixDomainSockClient);
226 liveuser 4067
 
3 liveuser 4068
		#如果執行失敗
4069
		if($unixDomainSockClient["status"]==="false"){
226 liveuser 4070
 
3 liveuser 4071
			#設置執行錯誤識別
4072
			$result["status"]="false";
226 liveuser 4073
 
3 liveuser 4074
			#設置錯誤訊息
4075
			$result["error"]=$unixDomainSockClient;
226 liveuser 4076
 
3 liveuser 4077
			#回傳結果
4078
			return $result;
226 liveuser 4079
 
3 liveuser 4080
			}#if end
226 liveuser 4081
 
3 liveuser 4082
		#debug
4083
		#var_dump(__LINE__,$unixDomainSockClient);
226 liveuser 4084
 
3 liveuser 4085
		#如果不是 json 字串
4086
		if(json_validate(trim($unixDomainSockClient["content"]))==="false"){
226 liveuser 4087
 
3 liveuser 4088
			#設置執行錯誤識別
4089
			$result["status"]="false";
226 liveuser 4090
 
3 liveuser 4091
			#設置錯誤訊息
4092
			$result["error"][]=trim($unixDomainSockClient["content"])." is not json string!";
226 liveuser 4093
 
3 liveuser 4094
			#設置錯誤訊息
4095
			$result["error"][]=$unixDomainSockClient;
226 liveuser 4096
 
3 liveuser 4097
			#回傳結果
4098
			return $result;
226 liveuser 4099
 
3 liveuser 4100
			}#if end
226 liveuser 4101
 
3 liveuser 4102
		#取得json回應
4103
		$jsonRes=json_decode(trim($unixDomainSockClient["content"]));
226 liveuser 4104
 
3 liveuser 4105
		#debug
4106
		#var_dump(__LINE__,$jsonRes);
226 liveuser 4107
 
3 liveuser 4108
		#如果執行失敗
4109
		if($jsonRes===null){
226 liveuser 4110
 
3 liveuser 4111
			#設置執行錯誤識別
4112
			$result["status"]="false";
226 liveuser 4113
 
3 liveuser 4114
			#設置錯誤訊息
4115
			$result["error"]=$unixDomainSockClient;
226 liveuser 4116
 
3 liveuser 4117
			#回傳結果
4118
			return $result;
226 liveuser 4119
 
3 liveuser 4120
			}#if end
226 liveuser 4121
 
3 liveuser 4122
		#如果沒有產生新id
4123
		if(!isset($jsonRes->id)){
226 liveuser 4124
 
3 liveuser 4125
			#設置執行錯誤識別
4126
			$result["status"]="false";
226 liveuser 4127
 
3 liveuser 4128
			#設置錯誤訊息
4129
			$result["error"]=$unixDomainSockClient;
226 liveuser 4130
 
3 liveuser 4131
			#回傳結果
4132
			return $result;
226 liveuser 4133
 
3 liveuser 4134
			}#if end
226 liveuser 4135
 
3 liveuser 4136
		#用新的id再傳送一次要求給 qbpwcf_usock_path
4137
		$paramsOfUnixDomainSockClient=$conf["sock::unixDomainSockClient"];
4138
		$paramsOfUnixDomainSockClient["id"]=$jsonRes->id;
4139
		#var_dump(__LINE__,$paramsOfUnixDomainSockClient);
4140
		$unixDomainSockClient=sock::unixDomainSockClient($paramsOfUnixDomainSockClient);
4141
		unset($paramsOfUnixDomainSockClient);
226 liveuser 4142
 
3 liveuser 4143
		#如果執行失敗
4144
		if($unixDomainSockClient["status"]==="false"){
226 liveuser 4145
 
3 liveuser 4146
			#設置執行錯誤識別
4147
			$result["status"]="false";
226 liveuser 4148
 
3 liveuser 4149
			#設置錯誤訊息
4150
			$result["error"]=$unixDomainSockClient;
226 liveuser 4151
 
3 liveuser 4152
			#回傳結果
4153
			return $result;
226 liveuser 4154
 
3 liveuser 4155
			}#if end
226 liveuser 4156
 
3 liveuser 4157
		#debug
4158
		#var_dump(__LINE__,$unixDomainSockClient["content"]);
226 liveuser 4159
 
4160
		#如果輸出非 json
3 liveuser 4161
		if(json_validate(trim($unixDomainSockClient["content"]))===false){
226 liveuser 4162
 
3 liveuser 4163
			#設置錯誤識別
4164
			$result["status"]="false";
226 liveuser 4165
 
3 liveuser 4166
			#設置錯誤訊息
4167
			$result["error"][]="got not json output";
226 liveuser 4168
 
3 liveuser 4169
			#設置錯誤訊息
4170
			$result["error"][]=json_last_error().":".json_last_error_msg();
226 liveuser 4171
 
3 liveuser 4172
			#回傳結果
4173
			return $result;
226 liveuser 4174
 
3 liveuser 4175
			}#if end
226 liveuser 4176
 
3 liveuser 4177
		#取得json回應
4178
		$jsonRes=(array)json_decode($unixDomainSockClient["content"]);
226 liveuser 4179
 
3 liveuser 4180
		#如果執行失敗
4181
		if($jsonRes===null){
226 liveuser 4182
 
3 liveuser 4183
			#設置執行錯誤識別
4184
			$result["status"]="false";
226 liveuser 4185
 
3 liveuser 4186
			#設置錯誤訊息
4187
			$result["error"]=$unixDomainSockClient;
226 liveuser 4188
 
3 liveuser 4189
			#回傳結果
4190
			return $result;
226 liveuser 4191
 
3 liveuser 4192
			}#if end
226 liveuser 4193
 
3 liveuser 4194
		#如果執行指令失敗
4195
		if($jsonRes["status"]==="false"){
226 liveuser 4196
 
3 liveuser 4197
			#設置執行錯誤識別
4198
			$result["status"]="false";
226 liveuser 4199
 
3 liveuser 4200
			#設置錯誤訊息
4201
			$result["error"]=$jsonRes["error"];
226 liveuser 4202
 
3 liveuser 4203
			#回傳結果
4204
			return $result;
226 liveuser 4205
 
3 liveuser 4206
			}#if end
226 liveuser 4207
 
3 liveuser 4208
		#執行正常
4209
		$result["status"]="true";
226 liveuser 4210
 
3 liveuser 4211
		#設置執行的結果
4212
		$result["content"]=$jsonRes;
226 liveuser 4213
 
3 liveuser 4214
		#回傳結果
4215
		return $result;
226 liveuser 4216
 
3 liveuser 4217
		}#public function execAnyCmdByQBPWCFunixSocket end
226 liveuser 4218
 
3 liveuser 4219
	/*
4220
	#函式說明:
4221
	#詢問透過 sock::unixDomainSockServer 執行的程序狀況
4222
	#回傳結果:
4223
	#$result["status"],"true"代表執行正常;"false"代表執行不正常.
4224
	#$result["error"],錯誤訊息陣列.
4225
	#$result["function"],當前執行的函式名稱.
4226
	#$result["argu"],使用的參數.
4227
	#$result["content"],陣列,程序的資訊.
4228
	#$result["content"]["pid"],字串,程序的pid.
4229
	#$result["content"]["running"],字串,是否正常執行中.
4230
	#$result["content"]["statusCode"],字串,回傳給 shell 的代碼.
4231
	#$result["content"]["input"],字串,輸入的內容.
4232
	#$result["content"]["output"],字串,標準輸出的內容.
4233
	#$result["content"]["error"],字串,錯誤輸出的內容.
4234
	#必填參數:
4235
	#$conf["uuid"],字串,proc的uuid.
4236
	$conf["uuid"]="";
4237
	#可省略參數:
4238
	#$conf["sock"],字串,要連線的 usr/bin/qbpwcf-sock.php(sock::unixDomainSockServer) 所產生的 unix domain socket 路徑與名稱,預設為 qbpwcf_usock_path.
4239
	#$conf["sock"]=qbpwcf_usock_path;
4240
	#參考資料:
4241
	#無.
4242
	#備註:
4243
	#無.
4244
	*/
4245
	public static function getProcInfo(&$conf){
226 liveuser 4246
 
3 liveuser 4247
		#初始化要回傳的結果
4248
		$result=array();
4249
 
4250
		#取得當前執行的函數名稱
4251
		$result["function"]=__FUNCTION__;
226 liveuser 4252
 
3 liveuser 4253
		#如果沒有參數
4254
		if(func_num_args()==0){
226 liveuser 4255
 
3 liveuser 4256
			#設置執行失敗
4257
			$result["status"]="false";
226 liveuser 4258
 
3 liveuser 4259
			#設置執行錯誤訊息
4260
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 4261
 
3 liveuser 4262
			#回傳結果
4263
			return $result;
226 liveuser 4264
 
3 liveuser 4265
			}#if end
226 liveuser 4266
 
3 liveuser 4267
		#取得參數
4268
		$result["argu"]=$conf;
4269
 
4270
		#如果 $conf 不為陣列
4271
		if(gettype($conf)!="array"){
226 liveuser 4272
 
3 liveuser 4273
			#設置執行失敗
4274
			$result["status"]="false";
226 liveuser 4275
 
3 liveuser 4276
			#設置執行錯誤訊息
4277
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 4278
 
3 liveuser 4279
			#如果傳入的參數為 null
4280
			if($conf==null){
226 liveuser 4281
 
3 liveuser 4282
				#設置執行錯誤訊息
4283
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 4284
 
3 liveuser 4285
				}#if end
4286
 
4287
			#回傳結果
4288
			return $result;
226 liveuser 4289
 
3 liveuser 4290
			}#if end
226 liveuser 4291
 
3 liveuser 4292
		#檢查參數
4293
		#函式說明:
4294
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
4295
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
4296
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
4297
		#$result["function"],當前執行的函式名稱.
4298
		#$result["argu"],設置給予的參數.
4299
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
4300
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
4301
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
4302
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
4303
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
4304
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
4305
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
4306
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
4307
		#必填寫的參數:
4308
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
226 liveuser 4309
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
3 liveuser 4310
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
4311
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
4312
		#可以省略的參數:
4313
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
4314
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("uuid");
4315
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
4316
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string");
4317
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
4318
		#$conf["canBeEmptyString"]="false";
4319
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
4320
		#$conf["canNotBeEmpty"]=array();
4321
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
4322
		#$conf["canBeEmpty"]=array();
4323
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
4324
		$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("sock");
4325
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
4326
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("sock");
226 liveuser 4327
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 4328
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string");
4329
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
4330
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(qbpwcf_usock_path);
4331
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
4332
		#$conf["disallowAllSkipableVarIsEmpty"]="";
4333
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
4334
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
4335
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
4336
		#$conf["arrayCountEqualCheck"][]=array();
4337
		#參考資料來源:
4338
		#array_keys=>http://php.net/manual/en/function.array-keys.php
4339
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
4340
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 4341
 
3 liveuser 4342
		#如果檢查參數失敗
4343
		if($checkArguments["status"]==="false"){
226 liveuser 4344
 
3 liveuser 4345
			#設置執行失敗
4346
			$result["status"]="false";
226 liveuser 4347
 
3 liveuser 4348
			#設置錯誤訊息
4349
			$result["error"]=$checkArguments;
226 liveuser 4350
 
3 liveuser 4351
			#回傳結果
4352
			return $result;
226 liveuser 4353
 
3 liveuser 4354
			}#if end
226 liveuser 4355
 
3 liveuser 4356
		#如果檢查參數不通過
4357
		if($checkArguments["passed"]==="false"){
226 liveuser 4358
 
3 liveuser 4359
			#設置執行失敗
4360
			$result["status"]="false";
226 liveuser 4361
 
3 liveuser 4362
			#設置錯誤訊息
4363
			$result["error"]=$checkArguments;
226 liveuser 4364
 
3 liveuser 4365
			#回傳結果
4366
			return $result;
226 liveuser 4367
 
3 liveuser 4368
			}#if end
226 liveuser 4369
 
3 liveuser 4370
		函式說明:
4371
		#連線到 unixDomainSockServer 提供的 unix domain socket.
4372
		#回傳結果:
4373
		#$result["status"],"true"代表執行正常;"false"代表執行不正常.
4374
		#$result["error"],錯誤訊息陣列.
4375
		#$result["function"],當前執行的函式名稱.
4376
		#$result["content"],取得的回應.
4377
		#必填參數:
4378
		#$conf["sock"],字串,要連線的unix domain socket.
4379
		$conf["sock::unixDomainSockClient"]["sock"]=$conf["sock"];
4380
		#可省略參數:
4381
		#$conf["id"],字串,取得的id,若無此值,則會得到新的數值.
4382
		#$conf["id"]="";
4383
		#$conf["cmd"],字串,要執行的指令,當$conf["id"]參數合法時,才會執行.
4384
		#$conf["cmd"]="";
4385
		#$conf["param"],參數陣列.
4386
		#$conf["param"]=array();
4387
		#$conf["escaped"],字串,param參數是否已經escaped了,預設為"false",反之為"true".
4388
		#$conf["escaped"]="true";
4389
		#$conf["custom"],陣列,要客制化傳輸的內容,會覆蓋以上除了$conf["id"]以外的可省略參數.
4390
		$conf["sock::unixDomainSockClient"]["custom"]=array("QueryProcByUUID"=>$conf["uuid"]);
4391
		#$conf["raw"],字串,要直接傳送的字串內容,會忽略其他可省略參數.
4392
		#$conf["raw"]="";
4393
		#參考資料:
4394
		#http://php.net/manual/en/function.stream-socket-client.php
4395
		#http://php.net/manual/en/function.stream-get-contents.php
4396
		#備註:
4397
		#無.
4398
		$paramsOfUnixDomainSockClient=$conf["sock::unixDomainSockClient"];
4399
		$unixDomainSockClient=sock::unixDomainSockClient($paramsOfUnixDomainSockClient);
4400
		unset($paramsOfUnixDomainSockClient);
226 liveuser 4401
 
3 liveuser 4402
		#如果執行失敗
4403
		if($unixDomainSockClient["status"]==="false"){
226 liveuser 4404
 
3 liveuser 4405
			#設置執行錯誤識別
4406
			$result["status"]="false";
226 liveuser 4407
 
3 liveuser 4408
			#設置錯誤訊息
4409
			$result["error"]=$unixDomainSockClient;
226 liveuser 4410
 
3 liveuser 4411
			#回傳結果
4412
			return $result;
226 liveuser 4413
 
3 liveuser 4414
			}#if end
226 liveuser 4415
 
3 liveuser 4416
		#debug
4417
		#var_dump(__LINE__,$unixDomainSockClient);
226 liveuser 4418
 
3 liveuser 4419
		#如果不是 json 字串
4420
		if(json_validate(trim($unixDomainSockClient["content"]))==="false"){
226 liveuser 4421
 
3 liveuser 4422
			#設置執行錯誤識別
4423
			$result["status"]="false";
226 liveuser 4424
 
3 liveuser 4425
			#設置錯誤訊息
4426
			$result["error"][]=trim($unixDomainSockClient["content"])." is not json string!";
226 liveuser 4427
 
3 liveuser 4428
			#設置錯誤訊息
4429
			$result["error"][]=$unixDomainSockClient;
226 liveuser 4430
 
3 liveuser 4431
			#回傳結果
4432
			return $result;
226 liveuser 4433
 
3 liveuser 4434
			}#if end
226 liveuser 4435
 
3 liveuser 4436
		#取得json回應
4437
		$jsonRes=json_decode(trim($unixDomainSockClient["content"]));
226 liveuser 4438
 
3 liveuser 4439
		#debug
4440
		#var_dump(__LINE__,$jsonRes);
226 liveuser 4441
 
3 liveuser 4442
		#如果執行失敗
4443
		if($jsonRes===null){
226 liveuser 4444
 
3 liveuser 4445
			#設置執行錯誤識別
4446
			$result["status"]="false";
226 liveuser 4447
 
3 liveuser 4448
			#設置錯誤訊息
4449
			$result["error"]=$unixDomainSockClient;
226 liveuser 4450
 
3 liveuser 4451
			#回傳結果
4452
			return $result;
226 liveuser 4453
 
3 liveuser 4454
			}#if end
226 liveuser 4455
 
3 liveuser 4456
		#如果沒有產生新id
4457
		if(!isset($jsonRes->id)){
226 liveuser 4458
 
3 liveuser 4459
			#設置執行錯誤識別
4460
			$result["status"]="false";
226 liveuser 4461
 
3 liveuser 4462
			#設置錯誤訊息
4463
			$result["error"]=$unixDomainSockClient;
226 liveuser 4464
 
3 liveuser 4465
			#回傳結果
4466
			return $result;
226 liveuser 4467
 
3 liveuser 4468
			}#if end
226 liveuser 4469
 
3 liveuser 4470
		#用新的id再傳送一次要求給 qbpwcf_usock_path
4471
		$paramsOfUnixDomainSockClient=$conf["sock::unixDomainSockClient"];
4472
		$paramsOfUnixDomainSockClient["id"]=$jsonRes->id;
4473
		$unixDomainSockClient=sock::unixDomainSockClient($paramsOfUnixDomainSockClient);
4474
		unset($paramsOfUnixDomainSockClient);
226 liveuser 4475
 
3 liveuser 4476
		#如果執行失敗
4477
		if($unixDomainSockClient["status"]==="false"){
226 liveuser 4478
 
3 liveuser 4479
			#設置執行錯誤識別
4480
			$result["status"]="false";
226 liveuser 4481
 
3 liveuser 4482
			#設置錯誤訊息
4483
			$result["error"]=$unixDomainSockClient;
226 liveuser 4484
 
3 liveuser 4485
			#回傳結果
4486
			return $result;
226 liveuser 4487
 
3 liveuser 4488
			}#if end
226 liveuser 4489
 
3 liveuser 4490
		#debug
4491
		#var_dump(__LINE__,$unixDomainSockClient["content"]);
226 liveuser 4492
 
4493
		#如果輸出非 json
3 liveuser 4494
		if(json_validate(trim($unixDomainSockClient["content"]))===false){
226 liveuser 4495
 
3 liveuser 4496
			#設置錯誤識別
4497
			$result["status"]="false";
226 liveuser 4498
 
3 liveuser 4499
			#設置錯誤訊息
4500
			$result["error"][]="got not json output";
226 liveuser 4501
 
3 liveuser 4502
			#設置錯誤訊息
4503
			$result["error"][]=json_last_error().":".json_last_error_msg();
226 liveuser 4504
 
3 liveuser 4505
			#設置原始訊息
4506
			$result["error"][]="ori res:".$unixDomainSockClient["content"];
226 liveuser 4507
 
3 liveuser 4508
			#回傳結果
4509
			return $result;
226 liveuser 4510
 
3 liveuser 4511
			}#if end
226 liveuser 4512
 
3 liveuser 4513
		#取得json回應
4514
		$jsonRes=(array)json_decode($unixDomainSockClient["content"]);
226 liveuser 4515
 
3 liveuser 4516
		#如果執行失敗
4517
		if($jsonRes===null){
226 liveuser 4518
 
3 liveuser 4519
			#設置執行錯誤識別
4520
			$result["status"]="false";
226 liveuser 4521
 
3 liveuser 4522
			#設置錯誤訊息
4523
			$result["error"]=$unixDomainSockClient;
226 liveuser 4524
 
3 liveuser 4525
			#回傳結果
4526
			return $result;
226 liveuser 4527
 
3 liveuser 4528
			}#if end
226 liveuser 4529
 
3 liveuser 4530
		#如果執行指令失敗
4531
		if($jsonRes["status"]==="false"){
226 liveuser 4532
 
3 liveuser 4533
			#設置執行錯誤識別
4534
			$result["status"]="false";
226 liveuser 4535
 
3 liveuser 4536
			#設置錯誤訊息
4537
			$result["error"]=$jsonRes["error"];
226 liveuser 4538
 
3 liveuser 4539
			#回傳結果
4540
			return $result;
226 liveuser 4541
 
3 liveuser 4542
			}#if end
226 liveuser 4543
 
3 liveuser 4544
		#移除不需要的元素
4545
		unset($jsonRes["status"]);
226 liveuser 4546
 
3 liveuser 4547
		#執行正常
4548
		$result["status"]="true";
226 liveuser 4549
 
3 liveuser 4550
		#$result["content"],陣列,程序的資訊.
4551
		#$result["content"]["pid"],字串,程序的pid.
4552
		#$result["content"]["running"],字串,是否正常執行中.
4553
		#$result["content"]["statusCode"],字串,回傳給 shell 的代碼.
4554
		#$result["content"]["input"],字串,輸入的內容.
4555
		#$result["content"]["output"],字串,標準輸出的內容.
4556
		#$result["content"]["error"],字串,錯誤輸出的內容.
226 liveuser 4557
 
3 liveuser 4558
		#初始執行的結果
4559
		$result["content"]=$jsonRes;
226 liveuser 4560
 
3 liveuser 4561
		#回傳結果
4562
		return $result;
226 liveuser 4563
 
3 liveuser 4564
		}#function getProcInfo end
226 liveuser 4565
 
3 liveuser 4566
	/*
4567
	#函式說明:
4568
	#傳送 raw 訊息到  unix domain socket.
4569
	#回傳結果:
4570
	#$result["status"],"true"代表執行正常;"false"代表執行不正常.
4571
	#$result["error"],錯誤訊息陣列.
4572
	#$result["function"],當前執行的函式名稱.
4573
	#$result["argu"],使用的參數.
4574
	#$result["content"],得到的回應.
4575
	#必填參數:
4576
	#$conf["msg"],字串,要傳送的訊息.
4577
	$conf["msg"]="";
4578
	#$conf["sock"],字串,要連線的的 unix domain socket 路徑與名稱
4579
	#$conf["sock"]="";
4580
	#可省略參數:
4581
	#無.
4582
	#參考資料:
4583
	#無.
4584
	#備註:
4585
	#無.
4586
	*/
4587
	public static function sendRawMsgUnixSocket(&$conf){
226 liveuser 4588
 
3 liveuser 4589
		#初始化要回傳的結果
4590
		$result=array();
4591
 
4592
		#取得當前執行的函數名稱
4593
		$result["function"]=__FUNCTION__;
226 liveuser 4594
 
3 liveuser 4595
		#如果沒有參數
4596
		if(func_num_args()==0){
226 liveuser 4597
 
3 liveuser 4598
			#設置執行失敗
4599
			$result["status"]="false";
226 liveuser 4600
 
3 liveuser 4601
			#設置執行錯誤訊息
4602
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 4603
 
3 liveuser 4604
			#回傳結果
4605
			return $result;
226 liveuser 4606
 
3 liveuser 4607
			}#if end
226 liveuser 4608
 
3 liveuser 4609
		#取得參數
4610
		$result["argu"]=$conf;
4611
 
4612
		#如果 $conf 不為陣列
4613
		if(gettype($conf)!="array"){
226 liveuser 4614
 
3 liveuser 4615
			#設置執行失敗
4616
			$result["status"]="false";
226 liveuser 4617
 
3 liveuser 4618
			#設置執行錯誤訊息
4619
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 4620
 
3 liveuser 4621
			#如果傳入的參數為 null
4622
			if($conf==null){
226 liveuser 4623
 
3 liveuser 4624
				#設置執行錯誤訊息
4625
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 4626
 
3 liveuser 4627
				}#if end
4628
 
4629
			#回傳結果
4630
			return $result;
226 liveuser 4631
 
3 liveuser 4632
			}#if end
226 liveuser 4633
 
3 liveuser 4634
		#函式說明:
4635
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
4636
		#回傳結果:
4637
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
4638
		#$result["error"],執行不正常結束的錯訊息陣列.
4639
		#$result["simpleError"],簡單表示的錯誤訊息.
4640
		#$result["function"],當前執行的函式名稱.
4641
		#$result["argu"],設置給予的參數.
4642
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
4643
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
4644
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
4645
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
4646
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
4647
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
4648
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
4649
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
4650
		#必填參數:
4651
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
4652
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
4653
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
4654
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
4655
		#可省略參數:
4656
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
4657
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("msg","sock");
4658
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
4659
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string");
4660
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
4661
		#$conf["canBeEmptyString"]="false";
4662
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
4663
		#$conf["canNotBeEmpty"]=array();
4664
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
4665
		#$conf["canBeEmpty"]=array();
4666
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
4667
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("sock");
4668
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
4669
		#$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("sock");
4670
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
4671
		#$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string");
4672
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
4673
		#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(qbpwcf_usock_path);
4674
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
4675
		#$conf["disallowAllSkipableVarIsEmpty"]="";
4676
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
4677
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
4678
		#$conf["disallowAllSkipableVarNotExist"],字串,是否不允許每個可省略參數都不存在,預設為"false"代表允許,反之為"true".
4679
		#$conf["disallowAllSkipableVarNotExist"]="";
4680
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
4681
		#$conf["arrayCountEqualCheck"][]=array();
4682
		#參考資料:
4683
		#array_keys=>http://php.net/manual/en/function.array-keys.php
4684
		#備註:
4685
		#無.
4686
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
4687
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 4688
 
3 liveuser 4689
		#如果執行失敗
4690
		if($checkArguments["status"]==="false"){
226 liveuser 4691
 
3 liveuser 4692
			#設置執行錯誤識別
4693
			$result["status"]="false";
226 liveuser 4694
 
3 liveuser 4695
			#設置錯誤訊息
4696
			$result["error"]=$checkArguments["error"];
226 liveuser 4697
 
3 liveuser 4698
			#回傳結果
4699
			return $result;
226 liveuser 4700
 
3 liveuser 4701
			}#if end
226 liveuser 4702
 
3 liveuser 4703
		#如果檢查不通過
4704
		if($checkArguments["status"]==="false"){
226 liveuser 4705
 
3 liveuser 4706
			#設置執行錯誤識別
4707
			$result["status"]="false";
226 liveuser 4708
 
3 liveuser 4709
			#設置錯誤訊息
4710
			$result["error"]=$checkArguments["error"];
226 liveuser 4711
 
3 liveuser 4712
			#回傳結果
4713
			return $result;
226 liveuser 4714
 
3 liveuser 4715
			}#if end
226 liveuser 4716
 
3 liveuser 4717
		#讓存放unix domain socket的路徑存在
4718
		#函式說明:
4719
		#確保路徑存在.
4720
		#回傳結果:
4721
		#$result["status"],執行正常與否,"true"代表正常,"false"代表不正常.
4722
		#$result["error"],錯誤訊息陣列.
4723
		#$resutl["function"],當前執行的涵式名稱.
4724
		#$result["path"],建立好的路徑字串.
4725
		#$result["fileName"],檔案名稱,若 $conf["haveFileName"] 為 "true" 則會回傳.
4726
		#$result["argu"],使用的參數.
4727
		#必填參數:
4728
		#$conf["path"],要檢查的路徑
4729
		$conf["fileAccess::validatePath"]["path"]=$conf["sock"];
4730
		#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
4731
		$conf["fileAccess::validatePath"]["fileArgu"]=__FILE__;
4732
		#可省略參數:
4733
		#$conf["haveFileName"],字串,"true"代表有$conf["path"]檔案名稱,"false"代表$conf["path"]為純路徑,預設為"false".
4734
		$conf["fileAccess::validatePath"]["haveFileName"]="true";
4735
		#$conf["dirPermission"],字串,新建資料夾的權限設定,預設爲0770,亦即擁有者,同群組者可以讀,寫,存取,其他人無法使用.
4736
		#$conf["dirPermission"]="";
4737
		#$conf["web"],是否為檔案系統,"true"為網頁路徑,"false"為網頁系統,預設為"false".
4738
		$conf["fileAccess::validatePath"]["web"]="false";
4739
		#參考資料:
4740
		#無.
4741
		#備註:
4742
		#無.
4743
		$validatePath=fileAccess::validatePath($conf["fileAccess::validatePath"]);
4744
		unset($conf["fileAccess::validatePath"]);
226 liveuser 4745
 
3 liveuser 4746
		#如果執行失敗
4747
		if($validatePath["status"]==="false"){
226 liveuser 4748
 
3 liveuser 4749
			#設置錯誤識別
4750
			$result["status"]="false";
226 liveuser 4751
 
3 liveuser 4752
			#設置錯誤訊息
4753
			$result["error"]=$validatePath;
226 liveuser 4754
 
3 liveuser 4755
			#回傳結果
4756
			return $result;
226 liveuser 4757
 
3 liveuser 4758
			}#if end
226 liveuser 4759
 
3 liveuser 4760
		#變更 working dir
4761
		$chdir=chdir($validatePath["path"]);
226 liveuser 4762
 
3 liveuser 4763
		#嘗試30秒內連線到 unix domain socket
4764
		$fp=stream_socket_client("unix://".$validatePath["fileName"], $errno, $errstr, 30, STREAM_CLIENT_ASYNC_CONNECT );
226 liveuser 4765
 
3 liveuser 4766
		#如果開啟 unix domain socket 失敗
4767
		if(!$fp){
226 liveuser 4768
 
3 liveuser 4769
			#設置執行錯誤識別
4770
			$result["status"]="false";
226 liveuser 4771
 
3 liveuser 4772
			#設置錯誤訊息
4773
			$result["error"]=$errstr." (".$errno.")";
226 liveuser 4774
 
3 liveuser 4775
			#回傳結果
4776
			return $result;
226 liveuser 4777
 
3 liveuser 4778
			}#if end
226 liveuser 4779
 
3 liveuser 4780
		#傳遞訊息
4781
		$fwrite=fwrite($fp,$conf["msg"]);
226 liveuser 4782
 
3 liveuser 4783
		#如果傳遞失敗
4784
		if($fwrite===false){
226 liveuser 4785
 
3 liveuser 4786
			#關閉unix domain socket
4787
			@fclose($fp);
226 liveuser 4788
 
3 liveuser 4789
			#設置執行失敗
4790
			$result["status"]="false";
226 liveuser 4791
 
3 liveuser 4792
			#設置執行錯誤訊息
4793
			$result["error"][]="傳遞訊息(".$conf["msg"].")失敗";
226 liveuser 4794
 
3 liveuser 4795
			#回傳結果
4796
			return $result;
226 liveuser 4797
 
3 liveuser 4798
			}#if end
226 liveuser 4799
 
3 liveuser 4800
		#設置執行正常
4801
		$result["status"]="true";
226 liveuser 4802
 
3 liveuser 4803
		#回傳結果
4804
		return $result;
226 liveuser 4805
 
3 liveuser 4806
		}#function sendRawMsgUnixSocket
226 liveuser 4807
 
3 liveuser 4808
	/*
4809
	#連線到 sock::unixDomainSocketServer 產生的  unix domain socket,傳送指定的訊息.
4810
	#回傳結果:
4811
	#$result["status"],"true"代表執行正常;"false"代表執行不正常.
4812
	#$result["error"],錯誤訊息陣列.
4813
	#$result["function"],當前執行的函式名稱.
4814
	#$result["argu"],使用的參數.
4815
	#$result["content"],得到的回應.
4816
	#必填參數:
4817
	#$conf["msg"],陣列,要傳送的訊息,其主$conf["msg"]["id"],是無法指定的.
4818
	$conf["msg"]=array();
4819
	#可省略參數:
4820
	#$conf["sock"],字串,要連線的 usr/bin/qbpwcf-sock.php 所產生的 unix domain socket 路徑與名稱,預設為 qbpwcf_usock_path.
4821
	#$conf["sock"]=qbpwcf_usock_path;
4822
	#參考資料:
4823
	#無.
4824
	#備註:
4825
	#無.
4826
	*/
4827
	public static function sendAnyMsgToQBPWCFunixSocket(&$conf){
226 liveuser 4828
 
3 liveuser 4829
		#初始化要回傳的結果
4830
		$result=array();
4831
 
4832
		#取得當前執行的函數名稱
4833
		$result["function"]=__FUNCTION__;
226 liveuser 4834
 
3 liveuser 4835
		#如果沒有參數
4836
		if(func_num_args()==0){
226 liveuser 4837
 
3 liveuser 4838
			#設置執行失敗
4839
			$result["status"]="false";
226 liveuser 4840
 
3 liveuser 4841
			#設置執行錯誤訊息
4842
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 4843
 
3 liveuser 4844
			#回傳結果
4845
			return $result;
226 liveuser 4846
 
3 liveuser 4847
			}#if end
226 liveuser 4848
 
3 liveuser 4849
		#取得參數
4850
		$result["argu"]=$conf;
4851
 
4852
		#如果 $conf 不為陣列
4853
		if(gettype($conf)!="array"){
226 liveuser 4854
 
3 liveuser 4855
			#設置執行失敗
4856
			$result["status"]="false";
226 liveuser 4857
 
3 liveuser 4858
			#設置執行錯誤訊息
4859
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 4860
 
3 liveuser 4861
			#如果傳入的參數為 null
4862
			if($conf==null){
226 liveuser 4863
 
3 liveuser 4864
				#設置執行錯誤訊息
4865
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 4866
 
3 liveuser 4867
				}#if end
4868
 
4869
			#回傳結果
4870
			return $result;
226 liveuser 4871
 
3 liveuser 4872
			}#if end
226 liveuser 4873
 
3 liveuser 4874
		#函式說明:
4875
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
4876
		#回傳結果:
4877
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
4878
		#$result["error"],執行不正常結束的錯訊息陣列.
4879
		#$result["simpleError"],簡單表示的錯誤訊息.
4880
		#$result["function"],當前執行的函式名稱.
4881
		#$result["argu"],設置給予的參數.
4882
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
4883
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
4884
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
4885
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
4886
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
4887
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
4888
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
4889
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
4890
		#必填參數:
4891
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
4892
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
4893
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
4894
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
4895
		#可省略參數:
4896
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
4897
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("msg");
4898
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
4899
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("array");
4900
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
4901
		#$conf["canBeEmptyString"]="false";
4902
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
4903
		#$conf["canNotBeEmpty"]=array();
4904
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
4905
		#$conf["canBeEmpty"]=array();
4906
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
4907
		$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("sock");
4908
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
4909
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("sock");
4910
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
4911
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string");
4912
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
4913
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(qbpwcf_usock_path);
4914
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
4915
		#$conf["disallowAllSkipableVarIsEmpty"]="";
4916
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
4917
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
4918
		#$conf["disallowAllSkipableVarNotExist"],字串,是否不允許每個可省略參數都不存在,預設為"false"代表允許,反之為"true".
4919
		#$conf["disallowAllSkipableVarNotExist"]="";
4920
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
4921
		#$conf["arrayCountEqualCheck"][]=array();
4922
		#參考資料:
4923
		#array_keys=>http://php.net/manual/en/function.array-keys.php
4924
		#備註:
4925
		#無.
4926
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
4927
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 4928
 
3 liveuser 4929
		#如果執行失敗
4930
		if($checkArguments["status"]==="false"){
226 liveuser 4931
 
3 liveuser 4932
			#設置執行錯誤識別
4933
			$result["status"]="false";
226 liveuser 4934
 
3 liveuser 4935
			#設置錯誤訊息
4936
			$result["error"]=$checkArguments["error"];
226 liveuser 4937
 
3 liveuser 4938
			#回傳結果
4939
			return $result;
226 liveuser 4940
 
3 liveuser 4941
			}#if end
226 liveuser 4942
 
3 liveuser 4943
		#如果檢查不通過
4944
		if($checkArguments["status"]==="false"){
226 liveuser 4945
 
3 liveuser 4946
			#設置執行錯誤識別
4947
			$result["status"]="false";
226 liveuser 4948
 
3 liveuser 4949
			#設置錯誤訊息
4950
			$result["error"]=$checkArguments["error"];
226 liveuser 4951
 
3 liveuser 4952
			#回傳結果
4953
			return $result;
226 liveuser 4954
 
3 liveuser 4955
			}#if end
226 liveuser 4956
 
3 liveuser 4957
		#函式說明:
4958
		#連線到 unixDomainSockServer 提供的 unix domain socket.
4959
		#回傳結果:
4960
		#$result["status"],"true"代表執行正常;"false"代表執行不正常.
4961
		#$result["error"],錯誤訊息陣列.
4962
		#$result["function"],當前執行的函式名稱.
4963
		#$result["content"],取得的回應,讀到EOL,才會結束.
4964
		#必填參數:
4965
		#$conf["sock"],字串,要連線的unix domain socket.
4966
		$conf["sock::unixDomainSockClient"]["sock"]=$conf["sock"];
4967
		#可省略參數:
4968
		#$conf["id"],字串,取得的id,若無此值,則會得到新的數值.
4969
		#$conf["id"]="";
4970
		#$conf["cmd"],字串,要執行的指令,當$conf["id"]參數合法時,才會執行.
4971
		#$conf["cmd"]="";
4972
		#$conf["param"],參數陣列.
4973
		#$conf["param"]=array();
4974
		#$conf["escaped"],字串,param參數是否已經escaped了,預設為"false",反之為"true".
4975
		#$conf["escaped"]="true";
4976
		#$conf["custom"],陣列,要客制化傳輸的內容,會覆蓋以上除了$conf["id"]以外的可省略參數.
4977
		$conf["sock::unixDomainSockClient"]["custom"]=$conf["msg"];
4978
		#參考資料:
4979
		#http://php.net/manual/en/function.stream-socket-client.php
4980
		#http://php.net/manual/en/function.stream-get-contents.php
4981
		#備註:
4982
		#無.
4983
		$paramsOfUnixDomainSockClient=$conf["sock::unixDomainSockClient"];
4984
		$unixDomainSockClient=sock::unixDomainSockClient($conf["sock::unixDomainSockClient"]);
4985
		unset($conf["sock::unixDomainSockClient"]);
226 liveuser 4986
 
3 liveuser 4987
		#如果執行失敗
4988
		if($unixDomainSockClient["status"]==="false"){
226 liveuser 4989
 
3 liveuser 4990
			#設置執行錯誤識別
4991
			$result["status"]="false";
226 liveuser 4992
 
3 liveuser 4993
			#設置錯誤訊息
4994
			$result["error"]=$unixDomainSockClient;
226 liveuser 4995
 
3 liveuser 4996
			#回傳結果
4997
			return $result;
226 liveuser 4998
 
3 liveuser 4999
			}#if end
226 liveuser 5000
 
3 liveuser 5001
		#取得json回應
5002
		$jsonRes=json_decode($unixDomainSockClient["content"]);
226 liveuser 5003
 
3 liveuser 5004
		#如果執行失敗
5005
		if($jsonRes===null){
226 liveuser 5006
 
3 liveuser 5007
			#設置執行錯誤識別
5008
			$result["status"]="false";
226 liveuser 5009
 
3 liveuser 5010
			#設置錯誤訊息
5011
			$result["error"]=$unixDomainSockClient;
226 liveuser 5012
 
3 liveuser 5013
			#回傳結果
5014
			return $result;
226 liveuser 5015
 
3 liveuser 5016
			}#if end
226 liveuser 5017
 
3 liveuser 5018
		#如果沒有產生新id
5019
		if(!isset($jsonRes->id)){
226 liveuser 5020
 
3 liveuser 5021
			#設置執行錯誤識別
5022
			$result["status"]="false";
226 liveuser 5023
 
3 liveuser 5024
			#設置錯誤訊息
5025
			$result["error"]=$unixDomainSockClient;
226 liveuser 5026
 
3 liveuser 5027
			#回傳結果
5028
			return $result;
226 liveuser 5029
 
3 liveuser 5030
			}#if end
226 liveuser 5031
 
3 liveuser 5032
		#用新的id再傳送一次要求給 qbpwcf_usock_path
226 liveuser 5033
		$paramsOfUnixDomainSockClient["id"]=$jsonRes->id;
3 liveuser 5034
		$unixDomainSockClient=sock::unixDomainSockClient($paramsOfUnixDomainSockClient);
5035
		unset($paramsOfUnixDomainSockClient);
226 liveuser 5036
 
3 liveuser 5037
		#如果執行失敗
5038
		if($unixDomainSockClient["status"]==="false"){
226 liveuser 5039
 
3 liveuser 5040
			#設置執行錯誤識別
5041
			$result["status"]="false";
226 liveuser 5042
 
3 liveuser 5043
			#設置錯誤訊息
5044
			$result["error"]=$unixDomainSockClient;
226 liveuser 5045
 
3 liveuser 5046
			#回傳結果
5047
			return $result;
226 liveuser 5048
 
3 liveuser 5049
			}#if end
226 liveuser 5050
 
3 liveuser 5051
		#取得回應
5052
		$result["content"]=$unixDomainSockClient["content"];
226 liveuser 5053
 
3 liveuser 5054
		#設置執行正常
5055
		$result["status"]="true";
226 liveuser 5056
 
3 liveuser 5057
		#回傳結果
5058
		return $result;
226 liveuser 5059
 
3 liveuser 5060
		}#function sendAnyMsgToQBPWCFunixSocket end
226 liveuser 5061
 
3 liveuser 5062
	/*
5063
	#函式說明:
5064
	#重複連線到 unixDomainSockServer 提供的 unix domain socket, 避免其 listen timeout, 並清除逾時的連線.
5065
	#回傳結果:
5066
	#$result["status"],"true"代表執行正常;"false"代表執行不正常.
5067
	#$result["error"],錯誤訊息陣列.
5068
	#$result["function"],當前執行的函式名稱.
5069
	#必填參數:
5070
	#$conf["sock"],字串,要連線的unix domain socket.
5071
	$conf["sock"]="";
5072
	#可省略參數:
5073
	#無.
5074
	#參考資料:
5075
	#無.
5076
	#備註:
5077
	#無.
5078
	*/
5079
	public static function keepAliveUnixDomainSockConnection(&$conf){
226 liveuser 5080
 
3 liveuser 5081
		#初始化要回傳的結果
5082
		$result=array();
5083
 
5084
		#取得當前執行的函數名稱
5085
		$result["function"]=__FUNCTION__;
226 liveuser 5086
 
3 liveuser 5087
		#如果沒有參數
5088
		if(func_num_args()==0){
226 liveuser 5089
 
3 liveuser 5090
			#設置執行失敗
5091
			$result["status"]="false";
226 liveuser 5092
 
3 liveuser 5093
			#設置執行錯誤訊息
5094
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 5095
 
3 liveuser 5096
			#回傳結果
5097
			return $result;
226 liveuser 5098
 
3 liveuser 5099
			}#if end
5100
 
5101
		#涵式說明:
5102
		#判斷當前環境為web還是cmd
5103
		#回傳結果:
5104
		#$result,"web"或"cmd"
5105
		if(csInformation::getEnv()==="web"){
226 liveuser 5106
 
3 liveuser 5107
			#設置執行失敗
5108
			$result["status"]="false";
226 liveuser 5109
 
3 liveuser 5110
			#設置執行錯誤訊息
5111
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
226 liveuser 5112
 
3 liveuser 5113
			#回傳結果
5114
			return $result;
226 liveuser 5115
 
3 liveuser 5116
			}#if end
226 liveuser 5117
 
3 liveuser 5118
		#取得參數
5119
		$result["argu"]=$conf;
5120
 
5121
		#如果 $conf 不為陣列
5122
		if(gettype($conf)!="array"){
226 liveuser 5123
 
3 liveuser 5124
			#設置執行失敗
5125
			$result["status"]="false";
226 liveuser 5126
 
3 liveuser 5127
			#設置執行錯誤訊息
5128
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 5129
 
3 liveuser 5130
			#如果傳入的參數為 null
5131
			if($conf==null){
226 liveuser 5132
 
3 liveuser 5133
				#設置執行錯誤訊息
5134
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 5135
 
3 liveuser 5136
				}#if end
5137
 
5138
			#回傳結果
5139
			return $result;
226 liveuser 5140
 
3 liveuser 5141
			}#if end
226 liveuser 5142
 
3 liveuser 5143
		#檢查參數
5144
		#函式說明:
5145
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
5146
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
5147
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
5148
		#$result["function"],當前執行的函式名稱.
5149
		#$result["argu"],設置給予的參數.
5150
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
5151
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
5152
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
5153
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
5154
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
5155
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
5156
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
5157
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
5158
		#必填寫的參數:
5159
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
226 liveuser 5160
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
3 liveuser 5161
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
5162
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
5163
		#可以省略的參數:
5164
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
5165
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("sock");
5166
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
5167
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string");
5168
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
5169
		#$conf["canBeEmptyString"]="false";
5170
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
5171
		#$conf["canNotBeEmpty"]=array();
5172
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
5173
		#$conf["canBeEmpty"]=array();
5174
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
5175
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("id","cmd","param","clear");
5176
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
5177
		#$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("id","cmd","param","clear");
226 liveuser 5178
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 5179
		#$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string","array","string");
5180
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
5181
		#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array();
5182
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
5183
		#$conf["disallowAllSkipableVarIsEmpty"]="";
5184
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
5185
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
5186
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
5187
		#$conf["arrayCountEqualCheck"][]=array();
5188
		#參考資料來源:
5189
		#array_keys=>http://php.net/manual/en/function.array-keys.php
5190
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
5191
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 5192
 
3 liveuser 5193
		#如果檢查參數失敗
5194
		if($checkArguments["status"]==="false"){
226 liveuser 5195
 
3 liveuser 5196
			#設置執行失敗
5197
			$result["status"]="false";
226 liveuser 5198
 
3 liveuser 5199
			#設置錯誤訊息
5200
			$result["error"]=$checkArguments;
226 liveuser 5201
 
3 liveuser 5202
			#回傳結果
5203
			return $result;
226 liveuser 5204
 
3 liveuser 5205
			}#if end
226 liveuser 5206
 
3 liveuser 5207
		#如果檢查參數不通過
5208
		if($checkArguments["passed"]==="false"){
226 liveuser 5209
 
3 liveuser 5210
			#設置執行失敗
5211
			$result["status"]="false";
226 liveuser 5212
 
3 liveuser 5213
			#設置錯誤訊息
5214
			$result["error"]=$checkArguments;
226 liveuser 5215
 
3 liveuser 5216
			#回傳結果
5217
			return $result;
226 liveuser 5218
 
3 liveuser 5219
			}#if end
226 liveuser 5220
 
3 liveuser 5221
		#函式說明:
5222
		#連線到 unixDomainSockServer 提供的 unix domain socket.
5223
		#回傳結果:
5224
		#$result["status"],"true"代表執行正常;"false"代表執行不正常.
5225
		#$result["error"],錯誤訊息陣列.
5226
		#$result["function"],當前執行的函式名稱.
5227
		#$result["content"],取得的回應.
5228
		#必填參數:
5229
		#$conf["sock"],字串,要連線的unix domain socket.
5230
		$conf["sock::unixDomainSockClient"]["sock"]=$conf["sock"];
5231
		#可省略參數:
5232
		#$conf["id"],字串,取得的id,若無此值,則會得到新的數值.
5233
		#$conf["id"]="";
5234
		#$conf["cmd"],字串,要執行的指令.
5235
		#$conf["cmd"]="";
5236
		#$conf["param"],參數陣列.
5237
		#$conf["param"]=array();
5238
		#參考資料:
5239
		#http://php.net/manual/en/function.stream-socket-client.php
5240
		#http://php.net/manual/en/function.stream-get-contents.php
5241
		$unixDomainSockClient=sock::unixDomainSockClient($conf["sock::unixDomainSockClient"]);
5242
		unset($conf["sock::unixDomainSockClient"]);
226 liveuser 5243
 
3 liveuser 5244
		#建立連線失敗
5245
		if($unixDomainSockClient["status"]==="false"){
226 liveuser 5246
 
3 liveuser 5247
			#設置執行失敗
5248
			$result["status"]="false";
226 liveuser 5249
 
3 liveuser 5250
			#設置執行錯誤訊息
5251
			$result["error"]=$unixDomainSockClient;
226 liveuser 5252
 
3 liveuser 5253
			#回傳結果
5254
			return $result;
226 liveuser 5255
 
3 liveuser 5256
			}#if end
226 liveuser 5257
 
5258
		#取得回應
3 liveuser 5259
		$res=$unixDomainSockClient["content"];
226 liveuser 5260
 
3 liveuser 5261
		#解析回應
5262
		$res=json_decode(trim($res));
226 liveuser 5263
 
3 liveuser 5264
		#如果解析失敗
5265
		if($res===false){
226 liveuser 5266
 
3 liveuser 5267
			#設置執行失敗
5268
			$result["status"]="false";
226 liveuser 5269
 
3 liveuser 5270
			#設置執行錯誤訊息
5271
			$result["error"][]="解析回應失敗";
226 liveuser 5272
 
3 liveuser 5273
			#回傳結果
5274
			return $result;
226 liveuser 5275
 
3 liveuser 5276
			}#if end
226 liveuser 5277
 
5278
		#取得id
3 liveuser 5279
		$id=$res->id;
226 liveuser 5280
 
3 liveuser 5281
		#無窮迴圈
5282
		while(true){
226 liveuser 5283
 
3 liveuser 5284
			#函式說明:
5285
			#連線到 unixDomainSockServer 提供的 unix domain socket.
5286
			#回傳結果:
5287
			#$result["status"],"true"代表執行正常;"false"代表執行不正常.
5288
			#$result["error"],錯誤訊息陣列.
5289
			#$result["function"],當前執行的函式名稱.
5290
			#$result["content"],取得的回應.
5291
			#必填參數:
5292
			#$conf["sock"],字串,要連線的unix domain socket.
5293
			$conf["sock::unixDomainSockClient"]["sock"]=$conf["sock"];
5294
			#可省略參數:
5295
			#$conf["id"],字串,取得的id,若無此值,則會得到新的數值.
5296
			$conf["sock::unixDomainSockClient"]["id"]=$id;
5297
			#$conf["cmd"],字串,要執行的指令.
5298
			#$conf["cmd"]="";
5299
			#$conf["param"],參數陣列.
5300
			#$conf["param"]=array();
5301
			#參考資料:
5302
			#http://php.net/manual/en/function.stream-socket-client.php
5303
			#http://php.net/manual/en/function.stream-get-contents.php
5304
			$unixDomainSockClient=sock::unixDomainSockClient($conf["sock::unixDomainSockClient"]);
5305
			unset($conf["sock::unixDomainSockClient"]);
226 liveuser 5306
 
3 liveuser 5307
			#建立連線失敗
5308
			if($unixDomainSockClient["status"]==="false"){
226 liveuser 5309
 
3 liveuser 5310
				#設置執行失敗
5311
				$result["status"]="false";
226 liveuser 5312
 
3 liveuser 5313
				#設置執行錯誤訊息
5314
				$result["error"]=$unixDomainSockClient;
226 liveuser 5315
 
3 liveuser 5316
				#回傳結果
5317
				return $result;
226 liveuser 5318
 
3 liveuser 5319
				}#if end
226 liveuser 5320
 
5321
			#取得回應
3 liveuser 5322
			$res=$unixDomainSockClient["content"];
226 liveuser 5323
 
3 liveuser 5324
			#解析回應
5325
			$res=json_decode($res);
226 liveuser 5326
 
3 liveuser 5327
			#如果解析失敗
5328
			if($res===false){
226 liveuser 5329
 
3 liveuser 5330
				#設置執行失敗
5331
				$result["status"]="false";
226 liveuser 5332
 
3 liveuser 5333
				#設置執行錯誤訊息
5334
				$result["error"][]="解析回應失敗";
226 liveuser 5335
 
3 liveuser 5336
				#回傳結果
5337
				return $result;
226 liveuser 5338
 
3 liveuser 5339
				}#if end
226 liveuser 5340
 
3 liveuser 5341
			#show res
5342
			var_dump($res);
226 liveuser 5343
 
3 liveuser 5344
			#停止執行
5345
			sleep(60);
226 liveuser 5346
 
3 liveuser 5347
			}#while end
226 liveuser 5348
 
3 liveuser 5349
		#設置執行不正常
5350
		$result["status"]="false";
226 liveuser 5351
 
3 liveuser 5352
		#設置錯誤訊息
5353
		$result["error"][]="程式不應該中斷";
226 liveuser 5354
 
3 liveuser 5355
		#回傳結果
5356
		return $reuslt;
226 liveuser 5357
 
3 liveuser 5358
		}#function keepAliveUnixDomainSockConnection end
5359
 
5360
	/*
5361
	#函式說明:
5362
	#tcp client
5363
	#回傳結果:
5364
	#$result["status"],"true"代表執行正常;"false"代表執行不正常.
5365
	#$result["error"],錯誤訊息陣列.
5366
	#$result["function"],當前執行的函式名稱.
5367
	#$result["argu"],使用的參數.
5368
	#$result["content"],取得的回應內容.
5369
	#必填參數:
5370
	#$conf["port"],整數,tcp server listen port.
5371
	$conf["port"]=;
5372
	#$conf["dataToSend"],字串,要傳送的資訊.
5373
	$conf["dataToSend"]="";
5374
	#可省略參數:
5375
	#$conf["ip"],整數字串,tcp serer listen ip, default is 127.0.0.1.
5376
	#$conf["ip"]="127.0.0.1";
5377
	#$conf["sec"],整數,等待回應的秒數,預設為5秒.
5378
	#$conf["sec"]=5;
5379
	#$conf["microSec"],整數,等待回應的毫秒數,預設為0毫秒
5380
	#$conf["microSec"]=0;
5381
	#參考資料:
5382
	#https://www.php.net/manual/en/sockets.examples.php
5383
	#https://www.php.net/manual/en/function.socket-get-option.php
5384
	#備註:
5385
	#無.
5386
	*/
5387
	public static function tcpClient(&$conf){
226 liveuser 5388
 
3 liveuser 5389
		#初始化要回傳的結果
5390
		$result=array();
5391
 
5392
		#取得當前執行的函數名稱
5393
		$result["function"]=__FUNCTION__;
226 liveuser 5394
 
3 liveuser 5395
		#如果沒有參數
5396
		if(func_num_args()==0){
226 liveuser 5397
 
3 liveuser 5398
			#設置執行失敗
5399
			$result["status"]="false";
226 liveuser 5400
 
3 liveuser 5401
			#設置執行錯誤訊息
5402
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 5403
 
3 liveuser 5404
			#回傳結果
5405
			return $result;
226 liveuser 5406
 
3 liveuser 5407
			}#if end
5408
 
5409
		#涵式說明:
5410
		#判斷當前環境為web還是cmd
5411
		#回傳結果:
5412
		#$result,"web"或"cmd"
5413
		if(csInformation::getEnv()==="web"){
226 liveuser 5414
 
3 liveuser 5415
			#設置執行失敗
5416
			$result["status"]="false";
226 liveuser 5417
 
3 liveuser 5418
			#設置執行錯誤訊息
5419
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
226 liveuser 5420
 
3 liveuser 5421
			#回傳結果
5422
			return $result;
226 liveuser 5423
 
3 liveuser 5424
			}#if end
226 liveuser 5425
 
3 liveuser 5426
		#取得參數
5427
		$result["argu"]=$conf;
5428
 
5429
		#如果 $conf 不為陣列
5430
		if(gettype($conf)!="array"){
226 liveuser 5431
 
3 liveuser 5432
			#設置執行失敗
5433
			$result["status"]="false";
226 liveuser 5434
 
3 liveuser 5435
			#設置執行錯誤訊息
5436
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 5437
 
3 liveuser 5438
			#如果傳入的參數為 null
5439
			if($conf==null){
226 liveuser 5440
 
3 liveuser 5441
				#設置執行錯誤訊息
5442
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 5443
 
3 liveuser 5444
				}#if end
5445
 
5446
			#回傳結果
5447
			return $result;
226 liveuser 5448
 
3 liveuser 5449
			}#if end
226 liveuser 5450
 
3 liveuser 5451
		#初始化取得的回應
226 liveuser 5452
		$result["content"]="";
5453
 
3 liveuser 5454
		#檢查參數
5455
		#函式說明:
5456
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
5457
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
5458
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
5459
		#$result["function"],當前執行的函式名稱.
5460
		#$result["argu"],設置給予的參數.
5461
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
5462
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
5463
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
5464
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
5465
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
5466
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
5467
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
5468
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
5469
		#必填寫的參數:
5470
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
226 liveuser 5471
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
3 liveuser 5472
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
5473
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
5474
		#可以省略的參數:
5475
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
5476
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("port","dataToSend");
5477
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
5478
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("integer","string");
5479
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
5480
		$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
5481
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
5482
		#$conf["canNotBeEmpty"]=array();
5483
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
5484
		#$conf["canBeEmpty"]=array();
5485
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
5486
		$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("ip");
5487
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
5488
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("ip","sec","microSec");
226 liveuser 5489
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 5490
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","integer","integer");
5491
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
5492
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("127.0.0.1",5,0);
5493
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
5494
		#$conf["disallowAllSkipableVarIsEmpty"]="";
5495
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
5496
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
5497
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
5498
		#$conf["arrayCountEqualCheck"][]=array();
5499
		#參考資料來源:
5500
		#array_keys=>http://php.net/manual/en/function.array-keys.php
5501
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
5502
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 5503
 
3 liveuser 5504
		#如果檢查參數失敗
5505
		if($checkArguments["status"]==="false"){
226 liveuser 5506
 
3 liveuser 5507
			#設置執行失敗
5508
			$result["status"]="false";
226 liveuser 5509
 
3 liveuser 5510
			#設置錯誤訊息
5511
			$result["error"]=$checkArguments;
226 liveuser 5512
 
3 liveuser 5513
			#回傳結果
5514
			return $result;
226 liveuser 5515
 
3 liveuser 5516
			}#if end
226 liveuser 5517
 
3 liveuser 5518
		#如果檢查參數不通過
5519
		if($checkArguments["passed"]==="false"){
226 liveuser 5520
 
3 liveuser 5521
			#設置執行失敗
5522
			$result["status"]="false";
226 liveuser 5523
 
3 liveuser 5524
			#設置錯誤訊息
5525
			$result["error"]=$checkArguments;
226 liveuser 5526
 
3 liveuser 5527
			#回傳結果
5528
			return $result;
226 liveuser 5529
 
3 liveuser 5530
			}#if end
226 liveuser 5531
 
5532
		#建立 tcp socket client
3 liveuser 5533
		$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
226 liveuser 5534
 
3 liveuser 5535
		#設置接收資料的最長等待時間
5536
		socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>$conf["sec"],"usec"=>$conf["microSec"]));
226 liveuser 5537
 
3 liveuser 5538
		#如果建立 socket 失敗
5539
		if ($socket === false) {
226 liveuser 5540
 
3 liveuser 5541
			#設置執行失敗
5542
			$result["status"]="false";
226 liveuser 5543
 
3 liveuser 5544
			#設置錯誤訊息
5545
			$result["error"][]="socket_create() failed: reason: " . socket_strerror(socket_last_error());
226 liveuser 5546
 
3 liveuser 5547
			#回傳結果
5548
			return $result;
226 liveuser 5549
 
5550
			}#if end
5551
 
5552
		#連線到 tcp socket server
3 liveuser 5553
		$socket_connect = socket_connect($socket, $conf["ip"], $conf["port"]);
226 liveuser 5554
 
3 liveuser 5555
		#如果連線到 tcp socket server 失敗
5556
		if ($socket_connect === false) {
226 liveuser 5557
 
3 liveuser 5558
			#設置執行失敗
5559
			$result["status"]="false";
226 liveuser 5560
 
3 liveuser 5561
	    		#設置錯誤訊息
5562
			$result["error"][]="socket_connect() failed. Reason: ($result) " . socket_strerror(socket_last_error($socket));
226 liveuser 5563
 
3 liveuser 5564
			#回傳結果
5565
			return $result;
226 liveuser 5566
 
3 liveuser 5567
			}#if end
226 liveuser 5568
 
3 liveuser 5569
		#傳送訊息到 tcp server
5570
		socket_write($socket, $conf["dataToSend"].PHP_EOL, strlen($conf["dataToSend"].PHP_EOL));
226 liveuser 5571
 
3 liveuser 5572
		#取得回應
5573
		while ($out = socket_read($socket, 2048)) {
226 liveuser 5574
 
3 liveuser 5575
			#設置取得的回應
5576
			$result["content"]=$result["content"].$out;
226 liveuser 5577
 
3 liveuser 5578
			#debug
5579
			#echo $result["content"];
226 liveuser 5580
 
3 liveuser 5581
			}#while end
5582
 
5583
		#關閉socket
5584
		socket_close($socket);
226 liveuser 5585
 
3 liveuser 5586
		#設置處理正常
5587
		$result["status"]="true";
226 liveuser 5588
 
3 liveuser 5589
		#回傳結果
226 liveuser 5590
		return $result;
5591
 
3 liveuser 5592
		}#function tcpClient end
5593
 
5594
	/*
5595
	#函式說明:
5596
	#udp client
5597
	#回傳結果:
5598
	#$result["status"],"true"代表執行正常;"false"代表執行不正常.
5599
	#$result["error"],錯誤訊息陣列.
5600
	#$result["function"],當前執行的函式名稱.
5601
	#$result["argu"],使用的參數.
5602
	#$result["content"],取得的回應內容字串,為每個回應的內容直接串接.
5603
	#$result["contentArray"],取得的回應字串陣列,為每個回應的內容,各別儲存成陣列中的元素.
5604
	#$result["sock"],可重複使用的socket資源.
5605
	#必填參數:
5606
	#$conf["port"],整數,target port.
5607
	$conf["port"]=;
5608
	#$conf["dataToSend"],陣列字串,要傳送的多個訊息,每個訊息傳送結束後才會讀取回應.
5609
	$conf["dataToSend"]=array("");
5610
	#可省略參數:
5611
	#$conf["ip"],整數字串,tcp serer listen ip, default is 127.0.0.1.
5612
	#$conf["ip"]="127.0.0.1";
5613
	#$conf["timeout"],整數,預設為10秒,代表接收訊息的等待時間上限.
5614
	#$conf["timeout"]=10;
5615
	#$conf["secSleepAfterSend"],整數,預設為1秒.
5616
	#$conf["secSleepAfterSend"]=1;
5617
	#$conf["closeAtEnd"],字串,預設為"true",代表函式結束後要斷開連線.
5618
	#$conf["closeAtEnd"]="true";
5619
	#$conf["sock"],resource,要重複使用的socket資源,預設不指定.
5620
	#$conf["sock"]=;
5621
	#參考資料:
5622
	#https://www.php.net/manual/en/sockets.examples.php
5623
	#備註:
5624
	#無.
5625
	*/
5626
	public static function udpClient(&$conf){
226 liveuser 5627
 
3 liveuser 5628
		#初始化要回傳的結果
5629
		$result=array();
5630
 
5631
		#取得當前執行的函數名稱
5632
		$result["function"]=__FUNCTION__;
226 liveuser 5633
 
3 liveuser 5634
		#如果沒有參數
5635
		if(func_num_args()==0){
226 liveuser 5636
 
3 liveuser 5637
			#設置執行失敗
5638
			$result["status"]="false";
226 liveuser 5639
 
3 liveuser 5640
			#設置執行錯誤訊息
5641
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 5642
 
3 liveuser 5643
			#回傳結果
5644
			return $result;
226 liveuser 5645
 
3 liveuser 5646
			}#if end
5647
 
5648
		#涵式說明:
5649
		#判斷當前環境為web還是cmd
5650
		#回傳結果:
5651
		#$result,"web"或"cmd"
5652
		if(csInformation::getEnv()==="web"){
226 liveuser 5653
 
3 liveuser 5654
			#設置執行失敗
5655
			$result["status"]="false";
226 liveuser 5656
 
3 liveuser 5657
			#設置執行錯誤訊息
5658
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
226 liveuser 5659
 
3 liveuser 5660
			#回傳結果
5661
			return $result;
226 liveuser 5662
 
3 liveuser 5663
			}#if end
226 liveuser 5664
 
3 liveuser 5665
		#取得參數
5666
		$result["argu"]=$conf;
5667
 
5668
		#如果 $conf 不為陣列
5669
		if(gettype($conf)!="array"){
226 liveuser 5670
 
3 liveuser 5671
			#設置執行失敗
5672
			$result["status"]="false";
226 liveuser 5673
 
3 liveuser 5674
			#設置執行錯誤訊息
5675
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 5676
 
3 liveuser 5677
			#如果傳入的參數為 null
5678
			if($conf==null){
226 liveuser 5679
 
3 liveuser 5680
				#設置執行錯誤訊息
5681
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 5682
 
3 liveuser 5683
				}#if end
5684
 
5685
			#回傳結果
5686
			return $result;
226 liveuser 5687
 
3 liveuser 5688
			}#if end
226 liveuser 5689
 
3 liveuser 5690
		#初始化取得的回應
226 liveuser 5691
		$result["content"]="";
5692
 
3 liveuser 5693
		#檢查參數
5694
		#函式說明:
5695
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
5696
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
5697
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
5698
		#$result["function"],當前執行的函式名稱.
5699
		#$result["argu"],設置給予的參數.
5700
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
5701
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
5702
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
5703
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
5704
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
5705
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
5706
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
5707
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
5708
		#必填寫的參數:
5709
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
226 liveuser 5710
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
3 liveuser 5711
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
5712
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
5713
		#可以省略的參數:
5714
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
5715
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("port","dataToSend");
5716
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
5717
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("integer","array");
5718
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
5719
		$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
5720
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
5721
		#$conf["canNotBeEmpty"]=array();
5722
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
5723
		#$conf["canBeEmpty"]=array();
5724
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
5725
		$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("ip","secSleepAfterSend","closeAtEnd","timeout");
5726
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
5727
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("ip","secSleepAfterSend","closeAtEnd","sock","timeout");
226 liveuser 5728
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 5729
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","integer","string","object","integer");
5730
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
5731
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("127.0.0.1",1,"false",null,10);
5732
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
5733
		#$conf["disallowAllSkipableVarIsEmpty"]="";
5734
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
5735
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
5736
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
5737
		#$conf["arrayCountEqualCheck"][]=array();
5738
		#參考資料來源:
5739
		#array_keys=>http://php.net/manual/en/function.array-keys.php
5740
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
5741
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 5742
 
3 liveuser 5743
		#如果檢查參數失敗
5744
		if($checkArguments["status"]==="false"){
226 liveuser 5745
 
3 liveuser 5746
			#設置執行失敗
5747
			$result["status"]="false";
226 liveuser 5748
 
3 liveuser 5749
			#設置錯誤訊息
5750
			$result["error"]=$checkArguments;
226 liveuser 5751
 
3 liveuser 5752
			#回傳結果
5753
			return $result;
226 liveuser 5754
 
3 liveuser 5755
			}#if end
226 liveuser 5756
 
3 liveuser 5757
		#如果檢查參數不通過
5758
		if($checkArguments["passed"]==="false"){
226 liveuser 5759
 
3 liveuser 5760
			#設置執行失敗
5761
			$result["status"]="false";
226 liveuser 5762
 
3 liveuser 5763
			#設置錯誤訊息
5764
			$result["error"]=$checkArguments;
226 liveuser 5765
 
3 liveuser 5766
			#回傳結果
5767
			return $result;
226 liveuser 5768
 
3 liveuser 5769
			}#if end
226 liveuser 5770
 
5771
		#如果沒有指定要用既有的 socket
3 liveuser 5772
		if(!isset($conf["sock"])){
226 liveuser 5773
 
5774
			#建立 udp socket client
3 liveuser 5775
			$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
226 liveuser 5776
 
3 liveuser 5777
			#設置 timeout 為 $conf["timeout"] 秒
5778
			$timeout = array('sec'=>$conf["timeout"],'usec'=>0);
5779
 			socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,$timeout);
226 liveuser 5780
 
3 liveuser 5781
 			#如果建立 socket 失敗
5782
			if ($socket === false) {
226 liveuser 5783
 
3 liveuser 5784
				#設置執行失敗
5785
				$result["status"]="false";
226 liveuser 5786
 
3 liveuser 5787
				#設置錯誤訊息
5788
				$result["error"][]="socket_create() failed: reason: " . socket_strerror(socket_last_error());
226 liveuser 5789
 
3 liveuser 5790
				#回傳結果
5791
				return $result;
226 liveuser 5792
 
5793
				}#if end
5794
 
5795
			#連線到 udp socket server
3 liveuser 5796
			$socket_connect = socket_connect($socket, $conf["ip"], $conf["port"]);
226 liveuser 5797
 
3 liveuser 5798
			#如果連線到 udp socket server 失敗
5799
			if ($socket_connect === false) {
226 liveuser 5800
 
3 liveuser 5801
				#設置執行失敗
5802
				$result["status"]="false";
226 liveuser 5803
 
3 liveuser 5804
				#設置錯誤訊息
5805
				$result["error"][]="socket_connect() failed. Reason: ($result) " . socket_strerror(socket_last_error($socket));
226 liveuser 5806
 
3 liveuser 5807
				#回傳結果
5808
				return $result;
226 liveuser 5809
 
3 liveuser 5810
				}#if end
226 liveuser 5811
 
5812
			}#if end
5813
 
3 liveuser 5814
		#反之
5815
		else{
5816
			#取得要重複使用的socket
5817
			$socket=&$conf["sock"];
226 liveuser 5818
 
3 liveuser 5819
			}#else end
226 liveuser 5820
 
3 liveuser 5821
		#有幾個訊息就跑幾次
5822
		for($i=0;$i<count($conf["dataToSend"]);$i++){
226 liveuser 5823
 
3 liveuser 5824
			#傳送訊息到 tcp server
5825
			socket_write($socket, $conf["dataToSend"][$i], strlen($conf["dataToSend"][$i]));
226 liveuser 5826
 
3 liveuser 5827
			#取得回應
5828
			while ($out = socket_read($socket, 2048)) {
226 liveuser 5829
 
3 liveuser 5830
				#設置取得的回應,儲存到陣列裡面.
5831
				$result["contentArray"][]=$out;
226 liveuser 5832
 
3 liveuser 5833
				#設置取得的回應,儲存成字串,串接在一塊.
5834
				$result["content"]=$result["content"].$out;
226 liveuser 5835
 
3 liveuser 5836
				}#while end
226 liveuser 5837
 
3 liveuser 5838
			#如果有多個訊息要傳遞,且不是最後一個訊息
5839
			if(count($conf["dataToSend"])>1 && $i!==count($conf["dataToSend"])-1){
226 liveuser 5840
 
3 liveuser 5841
				#休息一下
5842
				sleep($conf["secSleepAfterSend"]);
226 liveuser 5843
 
3 liveuser 5844
				}#if end
226 liveuser 5845
 
3 liveuser 5846
			}#for end
226 liveuser 5847
 
3 liveuser 5848
		#如果要關閉連線
5849
		if($conf["closeAtEnd"]==="true"){
226 liveuser 5850
 
3 liveuser 5851
			#關閉socket
5852
			socket_close($socket);
226 liveuser 5853
 
3 liveuser 5854
			}#if end
226 liveuser 5855
 
3 liveuser 5856
		#取得使用的socket
226 liveuser 5857
		$result["sock"]=$socket;
5858
 
3 liveuser 5859
		#設置處理正常
5860
		$result["status"]="true";
226 liveuser 5861
 
3 liveuser 5862
		#回傳結果
226 liveuser 5863
		return $result;
5864
 
3 liveuser 5865
		}#function udpClient end
5866
 
5867
	/*
5868
	#函式說明:
5869
	#建立tcp server
5870
	#必填參數:
5871
	#$conf["port"],整數,tcp server listen port.
5872
	$conf["port"]=;
5873
	#可省略參數:
5874
	#$conf["ip"],整數字串,tcp server listen ip, default is 0.0.0.0.
5875
	#$conf["ip"]="0.0.0.0";
5876
	#$conf["processFuncs"],字串陣列,要將收到的訊息作什麼事情的函式們,預設為空陣列,裡面若是自己建立的涵式,可這樣呼叫"\qbpwcf\your function name".
5877
	#$conf["processFuncs"]=array();
5878
	#$conf["serverFuncs"],字串陣列,於idle時,server要執行的函式們,預設為空陣列.
5879
	#$conf["serverFuncs"]=array();
5880
	#$conf["welcomeMsg"],字串,是否要顯示歡迎訊息,預設為"true"代表要顯示歡迎訊息;"false"代表不要顯示歡迎訊息.
5881
	#$conf["welcomeMsg"]="true";
5882
	#$conf["socketSelectTimeountSec"],整數,代表要等候幾秒才抓取有異動的連線,預設為0代表立即抓取有異動的連線,但是會吃很多cpu.
5883
	#$conf["socketSelectTimeountSec"]=0;
5884
	#$conf["socketSelectTimeountUsec"],整數,代表要等候幾微秒才抓取有異動的連線,預設不使用.
5885
	#$conf["socketSelectTimeountUsec"]=5000;
5886
	#參考資料:
5887
	#無.
5888
	#備註:
5889
	#無.
5890
	*/
5891
	public static function tcpServer(&$conf){
5892
 
5893
		#初始化要回傳的結果
5894
		$result=array();
5895
 
5896
		#取得當前執行的函數名稱
5897
		$result["function"]=__FUNCTION__;
226 liveuser 5898
 
3 liveuser 5899
		#如果沒有參數
5900
		if(func_num_args()==0){
226 liveuser 5901
 
3 liveuser 5902
			#設置執行失敗
5903
			$result["status"]="false";
226 liveuser 5904
 
3 liveuser 5905
			#設置執行錯誤訊息
5906
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 5907
 
3 liveuser 5908
			#回傳結果
5909
			return $result;
226 liveuser 5910
 
3 liveuser 5911
			}#if end
5912
 
5913
		#涵式說明:
5914
		#判斷當前環境為web還是cmd
5915
		#回傳結果:
5916
		#$result,"web"或"cmd"
5917
		if(csInformation::getEnv()==="web"){
226 liveuser 5918
 
3 liveuser 5919
			#設置執行失敗
5920
			$result["status"]="false";
226 liveuser 5921
 
3 liveuser 5922
			#設置執行錯誤訊息
5923
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
226 liveuser 5924
 
3 liveuser 5925
			#回傳結果
5926
			return $result;
226 liveuser 5927
 
3 liveuser 5928
			}#if end
226 liveuser 5929
 
3 liveuser 5930
		#取得參數
5931
		$result["argu"]=$conf;
5932
 
5933
		#如果 $conf 不為陣列
5934
		if(gettype($conf)!="array"){
226 liveuser 5935
 
3 liveuser 5936
			#設置執行失敗
5937
			$result["status"]="false";
226 liveuser 5938
 
3 liveuser 5939
			#設置執行錯誤訊息
5940
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 5941
 
3 liveuser 5942
			#如果傳入的參數為 null
5943
			if($conf==null){
226 liveuser 5944
 
3 liveuser 5945
				#設置執行錯誤訊息
5946
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 5947
 
3 liveuser 5948
				}#if end
5949
 
5950
			#回傳結果
5951
			return $result;
226 liveuser 5952
 
3 liveuser 5953
			}#if end
226 liveuser 5954
 
3 liveuser 5955
		#檢查參數
5956
		#函式說明:
5957
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
5958
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
5959
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
5960
		#$result["function"],當前執行的函式名稱.
5961
		#$result["argu"],設置給予的參數.
5962
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
5963
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
5964
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
5965
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
5966
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
5967
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
5968
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
5969
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
5970
		#必填寫的參數:
5971
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
226 liveuser 5972
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
3 liveuser 5973
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
5974
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
5975
		#可以省略的參數:
5976
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
5977
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("port");
5978
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
5979
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("integer");
5980
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
5981
		#$conf["canBeEmptyString"]="false";
5982
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
5983
		#$conf["canNotBeEmpty"]=array();
5984
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
5985
		#$conf["canBeEmpty"]=array();
5986
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
5987
		$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("ip","welcomeMsg");
5988
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
5989
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("ip","processFuncs","welcomeMsg","socketSelectTimeountSec","socketSelectTimeountUsec","serverFuncs");
226 liveuser 5990
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 5991
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","array","string","integer","integer","array");
5992
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
5993
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("0.0.0.0",array(),"true",0,null,null);
5994
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
5995
		#$conf["disallowAllSkipableVarIsEmpty"]="";
5996
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
5997
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
5998
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
5999
		#$conf["arrayCountEqualCheck"][]=array();
6000
		#參考資料來源:
6001
		#array_keys=>http://php.net/manual/en/function.array-keys.php
6002
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
6003
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 6004
 
3 liveuser 6005
		#如果檢查參數失敗
6006
		if($checkArguments["status"]==="false"){
226 liveuser 6007
 
3 liveuser 6008
			#設置執行失敗
6009
			$result["status"]="false";
226 liveuser 6010
 
3 liveuser 6011
			#設置錯誤訊息
6012
			$result["error"]=$checkArguments;
226 liveuser 6013
 
3 liveuser 6014
			#回傳結果
6015
			return $result;
226 liveuser 6016
 
3 liveuser 6017
			}#if end
226 liveuser 6018
 
3 liveuser 6019
		#如果檢查參數不通過
6020
		if($checkArguments["passed"]==="false"){
226 liveuser 6021
 
3 liveuser 6022
			#設置執行失敗
6023
			$result["status"]="false";
226 liveuser 6024
 
3 liveuser 6025
			#設置錯誤訊息
6026
			$result["error"]=$checkArguments;
226 liveuser 6027
 
3 liveuser 6028
			#回傳結果
6029
			return $result;
226 liveuser 6030
 
3 liveuser 6031
			}#if end
226 liveuser 6032
 
3 liveuser 6033
		#程式不會 timeout
6034
		set_time_limit(0);
6035
 
6036
		#Turn on implicit output flushing so we see what we're getting as it comes in.
6037
		ob_implicit_flush();
6038
 
6039
		#set listen ip and port
6040
		$address = $conf["ip"];
6041
 
6042
		#set listen port
6043
		$port = $conf["port"];
6044
 
6045
		//如果建立 socket 失敗
6046
		if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
6047
 
306 liveuser 6048
			echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . PHP_EOL;
3 liveuser 6049
 
6050
			}#if end
6051
 
6052
		//如果 bind socket 失敗
6053
		if (socket_bind($sock, $address, $port) === false) {
6054
 
306 liveuser 6055
			echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . PHP_EOL;
3 liveuser 6056
 
6057
			}#if end
6058
 
6059
		//如果 socket listen 超過 5 秒仍未成功
6060
		if (socket_listen($sock, 5) === false) {
6061
 
306 liveuser 6062
			echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . PHP_EOL;
3 liveuser 6063
 
6064
			}#if end
6065
 
6066
		// create a list of all the clients that will be connected to us..
306 liveuser 6067
		// add the listening socket to this list
6068
		$clients = array("serverSock"=>$sock);
3 liveuser 6069
 
6070
		#無窮迴圈
6071
		do {
6072
			#each resource
6073
			foreach($clients as $rk=>$csock){
226 liveuser 6074
 
3 liveuser 6075
				#if not socket resource
6076
				if(get_resource_type($csock)!=="Socket"){
6077
 
6078
					#delete
6079
					unset($clients[$rk]);
6080
 
6081
					}#if end
226 liveuser 6082
 
3 liveuser 6083
				}#foreach end
6084
 
6085
			#found server sock
6086
			$serverSock=false;
6087
 
6088
			#each resource
6089
			foreach($clients as $rk=>$csock){
226 liveuser 6090
 
3 liveuser 6091
				#if not socket resource
6092
				if($rk==="serverSock"){
6093
 
6094
					#found server sock
6095
					$serverSock=true;
6096
 
6097
					}#if end
226 liveuser 6098
 
3 liveuser 6099
				}#if end
226 liveuser 6100
 
3 liveuser 6101
			#not found server sock
6102
			if(!$serverSock){
226 liveuser 6103
 
3 liveuser 6104
				#end
6105
				exit;
6106
 
6107
				}#if end
226 liveuser 6108
 
3 liveuser 6109
			#for socket select
6110
			$read   = $clients;
6111
			$write  = NULL;
6112
			$except = NULL;
6113
 
6114
			#如果沒設定微妙
226 liveuser 6115
			if(!isset($conf["socketSelectTimeountUsec"])){
3 liveuser 6116
 
226 liveuser 6117
				#get interesting socket
3 liveuser 6118
				$num_changed_sockets = socket_select($read, $write, $except, $conf["socketSelectTimeountSec"]);
6119
 
6120
				}#if end
6121
 
6122
			#反之有設定微妙
6123
			else{
6124
 
6125
				#get interesting socket
6126
				$num_changed_sockets = socket_select($read, $write, $except, $conf["socketSelectTimeountSec"], $conf["socketSelectTimeountUsec"]);
6127
 
6128
				}#else end
6129
 
6130
			#socket_select failed
6131
			if ($num_changed_sockets === false) {
226 liveuser 6132
 
3 liveuser 6133
				#NOTICE to server
6134
				echo "socket_select failed".PHP_EOL;
6135
 
226 liveuser 6136
				}
6137
 
3 liveuser 6138
			#At least at one of the sockets something interesting happened
6139
			else if ($num_changed_sockets > 0) {
226 liveuser 6140
 
6141
				/* debug
3 liveuser 6142
				echo "At least at one of the sockets something interesting happened".PHP_EOL;
6143
				echo "\$read:".PHP_EOL;var_dump($read);
6144
				echo "\$write:".PHP_EOL;var_dump($write);
6145
				echo "\$except:".PHP_EOL;var_dump($except);
6146
				*/
6147
 
6148
				// check if there is a client trying to connect
6149
				if (in_array($sock, $read)) {
226 liveuser 6150
 
3 liveuser 6151
					#accept the client, and add him to the $clients array
6152
				   	$clients[] = $newsock = socket_accept($sock);
226 liveuser 6153
 
3 liveuser 6154
				   	#如果要顯示歡迎訊息
6155
					if($conf["welcomeMsg"]==="true"){
226 liveuser 6156
 
3 liveuser 6157
				   		#send the client a welcome message
6158
				   		socket_write($newsock, "Welcome to QBPWCF tcpServer".PHP_EOL.
6159
				   		"There are ".(count($clients) - 1)." client(s) connected to the server".PHP_EOL);
226 liveuser 6160
 
3 liveuser 6161
				   		}#if end
226 liveuser 6162
 
3 liveuser 6163
					#嘗試取得遠端的ip與port
6164
					#參考資料:https://www.php.net/manual/en/function.socket-getpeername.php
6165
				 	@socket_getpeername($newsock, $ip);
6166
 
6167
					#如果有抓到 $ip
6168
					if(isset($ip)){
226 liveuser 6169
 
3 liveuser 6170
						#提示有新的使用者
6171
						echo "New client connected: ".$ip.PHP_EOL;
226 liveuser 6172
 
3 liveuser 6173
						}#if end
6174
 
6175
					// remove the listening socket from the clients-with-data array
6176
					$key = array_search($sock, $read);
6177
					unset($read[$key]);
226 liveuser 6178
 
3 liveuser 6179
					}
6180
 
6181
				#message received
6182
				else{
226 liveuser 6183
 
3 liveuser 6184
					// loop through all the clients that have data to read from
6185
					foreach ($read as $read_sock) {
226 liveuser 6186
 
3 liveuser 6187
						// read until newline or 1024 bytes
6188
						// socket_read while show errors when the client is disconnected, so silence the error messages
6189
						$data = @socket_read($read_sock, 1024, PHP_NORMAL_READ);
226 liveuser 6190
 
3 liveuser 6191
						// check if the client is disconnected
226 liveuser 6192
						if ($data === false)
3 liveuser 6193
						{
6194
							// remove client for $clients array
6195
							$key = array_search($read_sock, $clients);
6196
							unset($clients[$key]);
6197
							echo "client disconnected.".PHP_EOL;
226 liveuser 6198
 
3 liveuser 6199
							// continue to the next client to read from, if any
6200
							continue;
6201
						}
226 liveuser 6202
 
3 liveuser 6203
						// trim off the trailing/beginning white spaces
6204
						$data = trim($data);
226 liveuser 6205
 
3 liveuser 6206
						// check if there is any data after trimming off the spaces
6207
						if(!empty($data)){
226 liveuser 6208
 
3 liveuser 6209
							// send this to all the clients in the $clients array (except the first one, which is a listening socket)
6210
							foreach ($clients as $send_sock) {
226 liveuser 6211
 
3 liveuser 6212
								// if its the listening sock or the client that we got the message from, go to the next one in the list
226 liveuser 6213
 
6214
								#如果是Server Socket
3 liveuser 6215
								if ($send_sock === $sock){
6216
 
6217
									#server debug
226 liveuser 6218
									echo "reveiced:".$data.PHP_EOL;
3 liveuser 6219
 
6220
									#針對每個用來處理輸入字串的函式
6221
									foreach($conf["processFuncs"] as $proFunc){
6222
 
6223
										#呼叫函式,參數為輸入的字串data跟client socket, server socket, 每個 socket
6224
										#https://www.php.net/manual/en/function.call-user-func.php
6225
										$param=array("data"=>$data,"serverSock"=>$sock,"clientSock"=>$read_sock,"allSockArray"=>$clients);
6226
										$params=array();
226 liveuser 6227
										$params[]=&$param;
6228
										$result=call_user_func_array($proFunc,$params);
6229
 
3 liveuser 6230
										#如果有回傳結果
6231
										if($result!==NULL){
6232
 
6233
											#如果執行出錯
6234
											if($result["status"]==="false"){
6235
 
6236
												#印出結果
6237
												var_dump($result);
6238
 
6239
												}
6240
 
6241
											}
6242
 
6243
										}
6244
									}
6245
 
6246
								/*
6247
								#如果是Sender Socket
6248
								else if ($send_sock === $read_sock){
226 liveuser 6249
 
3 liveuser 6250
									continue;
6251
 
6252
									}
226 liveuser 6253
								*/
6254
 
3 liveuser 6255
								/*
6256
								#msg to others/broadcast
6257
								else{
6258
 
6259
						    			// write the message to the client -- add a newline character to the end of the message
6260
						    			socket_write($send_sock, $data.PHP_EOL);
226 liveuser 6261
 
3 liveuser 6262
									}//end of broadcast
226 liveuser 6263
								*/
3 liveuser 6264
 
6265
								} //foreach end
226 liveuser 6266
 
3 liveuser 6267
							}#if end
226 liveuser 6268
 
3 liveuser 6269
						} // end of reading foreach
226 liveuser 6270
 
3 liveuser 6271
					}#msg received end
6272
 
6273
				}#something happen end
6274
 
6275
			#反之沒有用戶插手
6276
			else{
6277
				#提示idle
6278
				#echo "idle:".time().PHP_EOL;
226 liveuser 6279
 
3 liveuser 6280
				#如果沒有設置 $conf["serverFuncs"]
6281
				if(!isset($conf["serverFuncs"])){
226 liveuser 6282
 
3 liveuser 6283
					#設置為空陣列
6284
					$conf["serverFuncs"]=array();
226 liveuser 6285
 
3 liveuser 6286
					}#if end
226 liveuser 6287
 
3 liveuser 6288
				#針對每個用來處理輸入字串的函式
6289
				foreach($conf["serverFuncs"] as $proFunc){
6290
 
6291
					#呼叫函式,參數為輸入的字串data跟client socket, server socket, 每個 socket
6292
					#https://www.php.net/manual/en/function.call-user-func.php
226 liveuser 6293
					$result=call_user_func_array($proFunc,array());
3 liveuser 6294
 
6295
					#如果有回傳結果
6296
					if($result!==NULL){
6297
 
6298
						#如果執行出錯
6299
						if($result["status"]==="false"){
6300
 
6301
							#印出結果
6302
							var_dump($result);
6303
 
6304
							}//if end
6305
 
6306
						}//if end
6307
 
6308
					}//foreach end
226 liveuser 6309
 
3 liveuser 6310
				}#else end
6311
 
6312
			} while (true);
6313
 
6314
		#關閉listen的socket
6315
		socket_close($sock);
6316
 
6317
		}
6318
		#function tcpServer end
6319
 
6320
	/*
6321
	#函式說明:
6322
	#建立 udp server
6323
	#必填參數:
6324
	#$conf["port"],整數,udp server listen port.
6325
	$conf["port"]=;
6326
	#可省略參數:
6327
	#$conf["ip"],整數字串,udp serer listen ip, default is 0.0.0.0.
6328
	#$conf["ip"]="0.0.0.0";
6329
	#$conf["processFuncs"],字串陣列,要將收到的訊息作什麼事情的函式們,預設為空陣列.
6330
	#$conf["processFuncs"]=array();
6331
	#$conf["welcomeMsg"],字串,是否要顯示歡迎訊息,預設為"true"代表要顯示歡迎訊息;"false"代表不要顯示歡迎訊息.
6332
	#$conf["welcomeMsg"]="true";
6333
	#$conf["socketSelectTimeountSec"],整數,代表要等候幾秒才抓取有異動的連線,預設為0代表立即抓取有異動的連線,但是會吃很多cpu.
6334
	#$conf["socketSelectTimeountSec"]=0;
6335
	#$conf["socketSelectTimeountUsec"],整數,代表要等候幾微秒才抓取有異動的連線,預設不使用.
6336
	#$conf["socketSelectTimeountUsec"]=5000;
6337
	#參考資料:
6338
	#https://www.jianshu.com/p/097463d08664
6339
	#備註:
6340
	#無.
6341
	*/
6342
	public static function udpServer(&$conf){
6343
 
6344
		#初始化要回傳的結果
6345
		$result=array();
6346
 
6347
		#取得當前執行的函數名稱
6348
		$result["function"]=__FUNCTION__;
226 liveuser 6349
 
3 liveuser 6350
		#如果沒有參數
6351
		if(func_num_args()==0){
226 liveuser 6352
 
3 liveuser 6353
			#設置執行失敗
6354
			$result["status"]="false";
226 liveuser 6355
 
3 liveuser 6356
			#設置執行錯誤訊息
6357
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 6358
 
3 liveuser 6359
			#回傳結果
6360
			return $result;
226 liveuser 6361
 
3 liveuser 6362
			}#if end
6363
 
6364
		#涵式說明:
6365
		#判斷當前環境為web還是cmd
6366
		#回傳結果:
6367
		#$result,"web"或"cmd"
6368
		if(csInformation::getEnv()==="web"){
226 liveuser 6369
 
3 liveuser 6370
			#設置執行失敗
6371
			$result["status"]="false";
226 liveuser 6372
 
3 liveuser 6373
			#設置執行錯誤訊息
6374
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
226 liveuser 6375
 
3 liveuser 6376
			#回傳結果
6377
			return $result;
226 liveuser 6378
 
3 liveuser 6379
			}#if end
226 liveuser 6380
 
3 liveuser 6381
		#取得參數
6382
		$result["argu"]=$conf;
6383
 
6384
		#如果 $conf 不為陣列
6385
		if(gettype($conf)!="array"){
226 liveuser 6386
 
3 liveuser 6387
			#設置執行失敗
6388
			$result["status"]="false";
226 liveuser 6389
 
3 liveuser 6390
			#設置執行錯誤訊息
6391
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 6392
 
3 liveuser 6393
			#如果傳入的參數為 null
6394
			if($conf==null){
226 liveuser 6395
 
3 liveuser 6396
				#設置執行錯誤訊息
6397
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 6398
 
3 liveuser 6399
				}#if end
6400
 
6401
			#回傳結果
6402
			return $result;
226 liveuser 6403
 
3 liveuser 6404
			}#if end
226 liveuser 6405
 
3 liveuser 6406
		#檢查參數
6407
		#函式說明:
6408
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
6409
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
6410
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
6411
		#$result["function"],當前執行的函式名稱.
6412
		#$result["argu"],設置給予的參數.
6413
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
6414
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
6415
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
6416
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
6417
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
6418
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
6419
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
6420
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
6421
		#必填寫的參數:
6422
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
226 liveuser 6423
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
3 liveuser 6424
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
6425
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
6426
		#可以省略的參數:
6427
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
6428
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("port");
6429
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
6430
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("integer");
6431
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
6432
		#$conf["canBeEmptyString"]="false";
6433
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
6434
		#$conf["canNotBeEmpty"]=array();
6435
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
6436
		#$conf["canBeEmpty"]=array();
6437
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
6438
		$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("ip","welcomeMsg","socketSelectTimeountUsec");
6439
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
6440
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("ip","processFuncs","welcomeMsg","socketSelectTimeountSec","socketSelectTimeountUsec");
226 liveuser 6441
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 6442
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","array","string","integer","integer");
6443
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
6444
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("0.0.0.0",array(),"true",0,null);
6445
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
6446
		#$conf["disallowAllSkipableVarIsEmpty"]="";
6447
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
6448
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
6449
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
6450
		#$conf["arrayCountEqualCheck"][]=array();
6451
		#參考資料來源:
6452
		#array_keys=>http://php.net/manual/en/function.array-keys.php
6453
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
6454
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 6455
 
3 liveuser 6456
		#如果檢查參數失敗
6457
		if($checkArguments["status"]==="false"){
226 liveuser 6458
 
3 liveuser 6459
			#設置執行失敗
6460
			$result["status"]="false";
226 liveuser 6461
 
3 liveuser 6462
			#設置錯誤訊息
6463
			$result["error"]=$checkArguments;
226 liveuser 6464
 
3 liveuser 6465
			#回傳結果
6466
			return $result;
226 liveuser 6467
 
3 liveuser 6468
			}#if end
226 liveuser 6469
 
3 liveuser 6470
		#如果檢查參數不通過
6471
		if($checkArguments["passed"]==="false"){
226 liveuser 6472
 
3 liveuser 6473
			#設置執行失敗
6474
			$result["status"]="false";
226 liveuser 6475
 
3 liveuser 6476
			#設置錯誤訊息
6477
			$result["error"]=$checkArguments;
226 liveuser 6478
 
3 liveuser 6479
			#回傳結果
6480
			return $result;
226 liveuser 6481
 
3 liveuser 6482
			}#if end
226 liveuser 6483
 
3 liveuser 6484
		#Allow the script to hang around waiting for connections.
6485
		set_time_limit(0);
6486
 
6487
		#Turn on implicit output flushing so we see what we're getting as it comes in.
6488
		ob_implicit_flush();
6489
 
6490
		#set listen ip and port
6491
		$address = $conf["ip"];
6492
 
6493
		#set listen port
6494
		$port = $conf["port"];
6495
 
6496
		//如果建立 socket 失敗
6497
		if (($sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) === false) {
6498
 
6499
		    	echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . PHP_EOL;
6500
 
6501
			}#if end
6502
 
6503
		//如果 bind socket 失敗
6504
		if (socket_bind($sock, $address, $port) === false) {
6505
 
6506
		    	echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . PHP_EOL;
6507
 
6508
			}#if end
6509
 
6510
		// create a list of all the clients that will be connected to us..
6511
    		// add the listening socket to this list
6512
    		$clients = array("serverSock"=>$sock);
6513
 
6514
		#無窮迴圈
6515
		do {
6516
			#each resource
6517
			foreach($clients as $rk=>$csock){
6518
 
226 liveuser 6519
				#if not socket resource
3 liveuser 6520
				if(gettype($csock)==="boolean"){
6521
 
6522
					#delete
6523
					unset($clients[$rk]);
6524
 
6525
					}
6526
 
6527
				#if not socket resource
6528
				else if(get_resource_type($csock)!=="Socket"){
6529
 
6530
					#delete
6531
					unset($clients[$rk]);
6532
 
226 liveuser 6533
					}
3 liveuser 6534
				}
6535
 
6536
			#found server sock
6537
			$serverSock=false;
6538
 
6539
			#each resource
6540
			foreach($clients as $rk=>$csock){
226 liveuser 6541
 
3 liveuser 6542
				#if not socket resource
6543
				if($rk==="serverSock"){
6544
 
6545
					#found server sock
6546
					$serverSock=true;
6547
 
226 liveuser 6548
					}
3 liveuser 6549
				}
226 liveuser 6550
 
3 liveuser 6551
			#not found server sock
6552
			if(!$serverSock){
226 liveuser 6553
 
3 liveuser 6554
				#end
6555
				exit;
6556
 
6557
				}
226 liveuser 6558
 
3 liveuser 6559
			#for socket select
6560
			$read   = $clients;
6561
			$write  = NULL;
6562
			$except = NULL;
6563
 
6564
			#如果未設置 $conf["socketSelectTimeountUsec"]
6565
			if(!isset($conf["socketSelectTimeountUsec"])){
226 liveuser 6566
 
3 liveuser 6567
				#get interesting socket
6568
				$num_changed_sockets = socket_select($read, $write, $except, $conf["socketSelectTimeountSec"]);
226 liveuser 6569
 
3 liveuser 6570
				}#if end
6571
 
6572
			#反之有設置 $conf["socketSelectTimeountUsec"]
6573
			else{
6574
 
6575
				#get interesting socket
6576
				$num_changed_sockets = socket_select($read, $write, $except, $conf["socketSelectTimeountSec"], $conf["socketSelectTimeountUsec"]);
6577
 
6578
				}#else end
6579
 
6580
			#socket_select failed
6581
			if ($num_changed_sockets === false) {
226 liveuser 6582
 
3 liveuser 6583
				#NOTICE to server
6584
				echo "socket_select failed".PHP_EOL;
6585
 
6586
				}#if end
226 liveuser 6587
 
3 liveuser 6588
			#At least at one of the sockets something interesting happened
6589
			else if ($num_changed_sockets > 0) {
226 liveuser 6590
 
3 liveuser 6591
				// check if there is a client trying to connect
6592
				if (in_array($sock, $read)) {
226 liveuser 6593
 
3 liveuser 6594
					#accept the client, and add him to the $clients array
226 liveuser 6595
				   	$receivedBytes=socket_recvfrom($sock,$data,1024,0,$remote_ip,$remote_port);
3 liveuser 6596
					$key=$remote_ip.":".$remote_port;
6597
					$clients[$key] = $newsock = $sock;
6598
 
6599
					#如果要顯示歡迎訊息
6600
					if($conf["welcomeMsg"]==="true"){
6601
 
6602
						# send the client a welcome message
6603
					   	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);
6604
 
6605
						}#if end
6606
 
6607
					#debug
6608
					echo "client connected:".$key.PHP_EOL;
6609
					echo "client sended:".$data;
6610
					echo "all client:".PHP_EOL;
226 liveuser 6611
					var_dump($clients);
3 liveuser 6612
 
6613
					#針對每個用來處理輸入字串的函式
6614
					foreach($conf["processFuncs"] as $proFunc){
6615
 
6616
						#呼叫函式,參數為輸入的字串data跟client socket, server socket, 每個 socket
6617
						#https://www.php.net/manual/en/function.call-user-func.php
6618
						$param=array("data"=>trim($data),"serverSock"=>$sock,"clientKey"=>$key,"allSockArray"=>&$clients);
6619
						$params=array();
226 liveuser 6620
						$params[]=&$param;
6621
						$result=call_user_func_array($proFunc,$params);
3 liveuser 6622
 
6623
						#如果有回傳結果
6624
						if($result!==NULL){
6625
 
6626
							#如果執行出錯
6627
							if($result["status"]==="false"){
6628
 
6629
								#印出結果
6630
								var_dump($result);
6631
 
6632
								}
6633
 
6634
							}
6635
 
6636
						}
6637
 
6638
					#debug
6639
					echo "last all client:".PHP_EOL;
6640
					var_dump($clients);
226 liveuser 6641
 
3 liveuser 6642
					# remove the listening socket from the clients-with-data array
6643
					$key = array_search($sock, $read);
6644
					unset($read[$key]);
226 liveuser 6645
 
3 liveuser 6646
					}#if end
6647
 
6648
				}#something happen end
6649
 
6650
			} while (true);
6651
 
6652
		#關閉listen的socket
6653
		socket_close($sock);
6654
 
6655
		}
6656
		#function tcpServer end
6657
 
6658
	/*
6659
	#函式說明:
6660
	#針對收到的內容,原封不動地傳回給用戶.
6661
	#必填參數:
6662
	#$conf["data"],字串,要處理的內容.
6663
	$conf["data"]="";
6664
	#$conf["clientSock"],resource socket.
6665
	$conf["clientSock"]=;
6666
	#$conf["serverSock"],resource socket.
6667
	$conf["serverSock"]=;
6668
	#$conf["allSockArray"],resource陣列.
6669
	$conf["allSockArray"]=array();
6670
	#可省略參數:
6671
	#無.
6672
	#參考資料:
6673
	#無.
6674
	#備註:
6675
	#無.
6676
	*/
6677
	public function echoService(&$conf){
226 liveuser 6678
 
3 liveuser 6679
		#初始化要回傳的結果
6680
		$result=array();
6681
 
6682
		#取得當前執行的函數名稱
6683
		$result["function"]=__FUNCTION__;
226 liveuser 6684
 
3 liveuser 6685
		#如果沒有參數
6686
		if(func_num_args()==0){
226 liveuser 6687
 
3 liveuser 6688
			#設置執行失敗
6689
			$result["status"]="false";
226 liveuser 6690
 
3 liveuser 6691
			#設置執行錯誤訊息
6692
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 6693
 
3 liveuser 6694
			#回傳結果
6695
			return $result;
226 liveuser 6696
 
3 liveuser 6697
			}#if end
6698
 
6699
		#涵式說明:
6700
		#判斷當前環境為web還是cmd
6701
		#回傳結果:
6702
		#$result,"web"或"cmd"
6703
		if(csInformation::getEnv()==="web"){
226 liveuser 6704
 
3 liveuser 6705
			#設置執行失敗
6706
			$result["status"]="false";
226 liveuser 6707
 
3 liveuser 6708
			#設置執行錯誤訊息
6709
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
226 liveuser 6710
 
3 liveuser 6711
			#回傳結果
6712
			return $result;
226 liveuser 6713
 
3 liveuser 6714
			}#if end
226 liveuser 6715
 
3 liveuser 6716
		#取得參數
6717
		$result["argu"]=$conf;
6718
 
6719
		#如果 $conf 不為陣列
6720
		if(gettype($conf)!="array"){
226 liveuser 6721
 
3 liveuser 6722
			#設置執行失敗
6723
			$result["status"]="false";
226 liveuser 6724
 
3 liveuser 6725
			#設置執行錯誤訊息
6726
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 6727
 
3 liveuser 6728
			#如果傳入的參數為 null
6729
			if($conf==null){
226 liveuser 6730
 
3 liveuser 6731
				#設置執行錯誤訊息
6732
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 6733
 
3 liveuser 6734
				}#if end
6735
 
6736
			#回傳結果
6737
			return $result;
226 liveuser 6738
 
3 liveuser 6739
			}#if end
226 liveuser 6740
 
3 liveuser 6741
		#檢查參數
6742
		#函式說明:
6743
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
6744
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
6745
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
6746
		#$result["function"],當前執行的函式名稱.
6747
		#$result["argu"],設置給予的參數.
6748
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
6749
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
6750
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
6751
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
6752
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
6753
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
6754
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
6755
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
6756
		#必填寫的參數:
6757
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
226 liveuser 6758
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
3 liveuser 6759
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
6760
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
6761
		#可以省略的參數:
6762
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
6763
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("data","clientSock","serverSock","allSockArray");
6764
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
6765
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","resource","resource","array");
6766
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
6767
		#$conf["canBeEmptyString"]="false";
6768
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
6769
		#$conf["canNotBeEmpty"]=array();
6770
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
6771
		#$conf["canBeEmpty"]=array();
6772
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
6773
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("ip");
6774
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
6775
		#$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("ip","processFuncs");
226 liveuser 6776
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 6777
		#$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","array");
6778
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
6779
		#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("0.0.0.0",array());
6780
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
6781
		#$conf["disallowAllSkipableVarIsEmpty"]="";
6782
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
6783
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
6784
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
6785
		#$conf["arrayCountEqualCheck"][]=array();
6786
		#參考資料來源:
6787
		#array_keys=>http://php.net/manual/en/function.array-keys.php
6788
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
6789
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 6790
 
3 liveuser 6791
		#如果檢查參數失敗
6792
		if($checkArguments["status"]==="false"){
226 liveuser 6793
 
3 liveuser 6794
			#設置執行失敗
6795
			$result["status"]="false";
226 liveuser 6796
 
3 liveuser 6797
			#設置錯誤訊息
6798
			$result["error"]=$checkArguments;
226 liveuser 6799
 
3 liveuser 6800
			#回傳結果
6801
			return $result;
226 liveuser 6802
 
3 liveuser 6803
			}#if end
226 liveuser 6804
 
3 liveuser 6805
		#如果檢查參數不通過
6806
		if($checkArguments["passed"]==="false"){
226 liveuser 6807
 
3 liveuser 6808
			#設置執行失敗
6809
			$result["status"]="false";
226 liveuser 6810
 
3 liveuser 6811
			#設置錯誤訊息
6812
			$result["error"]=$checkArguments;
226 liveuser 6813
 
3 liveuser 6814
			#回傳結果
6815
			return $result;
226 liveuser 6816
 
3 liveuser 6817
			}#if end
6818
 
6819
		#將內容原封不動還給 client
6820
		@socket_write($conf["clientSock"], $conf["data"].PHP_EOL);
6821
 
6822
		}#function echoService end
6823
 
6824
	/*
6825
	#函式說明:
6826
	#針對收到的內容,原封不動地傳回給用戶.協定為UDP.
6827
	#回傳結果:
6828
	#無.
6829
	#必填參數:
6830
	#$conf["data"],字串,要處理的內容.
6831
	$conf["data"]="";
6832
	#$conf["clientKey"],resource socket key
6833
	$conf["clientKey"]=;
6834
	#$conf["serverSock"],resource socket.
6835
	$conf["serverSock"]=;
6836
	#$conf["allSockArray"],resource陣列.
6837
	$conf["allSockArray"]=array();
6838
	#可省略參數:
6839
	#無.
6840
	#參考資料:
6841
	#無.
6842
	#備註:
6843
	#無.
6844
	*/
6845
	public function echoServiceU(&$conf){
226 liveuser 6846
 
3 liveuser 6847
		#初始化要回傳的結果
6848
		$result=array();
6849
 
6850
		#取得當前執行的函數名稱
6851
		$result["function"]=__FUNCTION__;
226 liveuser 6852
 
3 liveuser 6853
		#如果沒有參數
6854
		if(func_num_args()==0){
226 liveuser 6855
 
3 liveuser 6856
			#設置執行失敗
6857
			$result["status"]="false";
226 liveuser 6858
 
3 liveuser 6859
			#設置執行錯誤訊息
6860
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 6861
 
3 liveuser 6862
			#回傳結果
6863
			return $result;
226 liveuser 6864
 
3 liveuser 6865
			}#if end
6866
 
6867
		#涵式說明:
6868
		#判斷當前環境為web還是cmd
6869
		#回傳結果:
6870
		#$result,"web"或"cmd"
6871
		if(csInformation::getEnv()==="web"){
226 liveuser 6872
 
3 liveuser 6873
			#設置執行失敗
6874
			$result["status"]="false";
226 liveuser 6875
 
3 liveuser 6876
			#設置執行錯誤訊息
6877
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
226 liveuser 6878
 
3 liveuser 6879
			#回傳結果
6880
			return $result;
226 liveuser 6881
 
3 liveuser 6882
			}#if end
226 liveuser 6883
 
3 liveuser 6884
		#取得參數
6885
		$result["argu"]=$conf;
6886
 
6887
		#如果 $conf 不為陣列
6888
		if(gettype($conf)!="array"){
226 liveuser 6889
 
3 liveuser 6890
			#設置執行失敗
6891
			$result["status"]="false";
226 liveuser 6892
 
3 liveuser 6893
			#設置執行錯誤訊息
6894
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 6895
 
3 liveuser 6896
			#如果傳入的參數為 null
6897
			if($conf==null){
226 liveuser 6898
 
3 liveuser 6899
				#設置執行錯誤訊息
6900
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 6901
 
3 liveuser 6902
				}#if end
6903
 
6904
			#回傳結果
6905
			return $result;
226 liveuser 6906
 
3 liveuser 6907
			}#if end
226 liveuser 6908
 
3 liveuser 6909
		#檢查參數
6910
		#函式說明:
6911
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
6912
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
6913
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
6914
		#$result["function"],當前執行的函式名稱.
6915
		#$result["argu"],設置給予的參數.
6916
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
6917
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
6918
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
6919
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
6920
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
6921
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
6922
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
6923
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
6924
		#必填寫的參數:
6925
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
226 liveuser 6926
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
3 liveuser 6927
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
6928
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
6929
		#可以省略的參數:
6930
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
6931
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("data","clientKey","serverSock","allSockArray");
6932
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
6933
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string","resource","array");
6934
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
6935
		#$conf["canBeEmptyString"]="false";
6936
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
6937
		#$conf["canNotBeEmpty"]=array();
6938
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
6939
		#$conf["canBeEmpty"]=array();
6940
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
6941
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("ip");
6942
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
6943
		#$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("ip","processFuncs");
226 liveuser 6944
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 6945
		#$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","array");
6946
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
6947
		#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("0.0.0.0",array());
6948
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
6949
		#$conf["disallowAllSkipableVarIsEmpty"]="";
6950
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
6951
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
6952
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
6953
		#$conf["arrayCountEqualCheck"][]=array();
6954
		#參考資料來源:
6955
		#array_keys=>http://php.net/manual/en/function.array-keys.php
6956
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
6957
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 6958
 
3 liveuser 6959
		#如果檢查參數失敗
6960
		if($checkArguments["status"]==="false"){
226 liveuser 6961
 
3 liveuser 6962
			#設置執行失敗
6963
			$result["status"]="false";
226 liveuser 6964
 
3 liveuser 6965
			#設置錯誤訊息
6966
			$result["error"]=$checkArguments;
226 liveuser 6967
 
3 liveuser 6968
			#回傳結果
6969
			return $result;
226 liveuser 6970
 
3 liveuser 6971
			}#if end
226 liveuser 6972
 
3 liveuser 6973
		#如果檢查參數不通過
6974
		if($checkArguments["passed"]==="false"){
226 liveuser 6975
 
3 liveuser 6976
			#設置執行失敗
6977
			$result["status"]="false";
226 liveuser 6978
 
3 liveuser 6979
			#設置錯誤訊息
6980
			$result["error"]=$checkArguments;
226 liveuser 6981
 
3 liveuser 6982
			#回傳結果
6983
			return $result;
226 liveuser 6984
 
3 liveuser 6985
			}#if end
6986
 
6987
		#將內容原封不動還給 client
6988
		$strArray=explode(":",$conf["clientKey"]);
6989
		$remote_ip=$strArray[0];
6990
		$remote_port=$strArray[1];
6991
		socket_sendto($conf["serverSock"], $conf["data"].PHP_EOL, 1024, 0, $remote_ip, $remote_port);
6992
 
6993
		}#function echoServiceU end
6994
 
6995
	/*
6996
	#函式說明:
6997
	#針對收到的內容,若是"quit"則斷開用戶的連線,若是"shutdown"則關閉Server Listen的socket.
6998
	#回傳結果:
6999
	#無.
7000
	#必填參數:
7001
	#$conf["data"],字串,要處理的內容.
7002
	$conf["data"]="";
7003
	#$conf["clientSock"],resource socket.
7004
	$conf["clientSock"]=;
7005
	#$conf["serverSock"],resource socket.
7006
	$conf["serverSock"]=;
7007
	#$conf["allSockArray"],resource陣列.
7008
	$conf["allSockArray"]=array();
7009
	#可省略參數:
7010
	#無.
7011
	#參考資料:
7012
	#無.
7013
	#備註:
7014
	#無.
7015
	*/
7016
	public function endService(&$conf){
226 liveuser 7017
 
3 liveuser 7018
		#初始化要回傳的結果
7019
		$result=array();
7020
 
7021
		#取得當前執行的函數名稱
7022
		$result["function"]=__FUNCTION__;
226 liveuser 7023
 
3 liveuser 7024
		#如果沒有參數
7025
		if(func_num_args()==0){
226 liveuser 7026
 
3 liveuser 7027
			#設置執行失敗
7028
			$result["status"]="false";
226 liveuser 7029
 
3 liveuser 7030
			#設置執行錯誤訊息
7031
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 7032
 
3 liveuser 7033
			#回傳結果
7034
			return $result;
226 liveuser 7035
 
3 liveuser 7036
			}#if end
7037
 
7038
		#涵式說明:
7039
		#判斷當前環境為web還是cmd
7040
		#回傳結果:
7041
		#$result,"web"或"cmd"
7042
		if(csInformation::getEnv()==="web"){
226 liveuser 7043
 
3 liveuser 7044
			#設置執行失敗
7045
			$result["status"]="false";
226 liveuser 7046
 
3 liveuser 7047
			#設置執行錯誤訊息
7048
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
226 liveuser 7049
 
3 liveuser 7050
			#回傳結果
7051
			return $result;
226 liveuser 7052
 
3 liveuser 7053
			}#if end
226 liveuser 7054
 
3 liveuser 7055
		#取得參數
7056
		$result["argu"]=$conf;
7057
 
7058
		#如果 $conf 不為陣列
7059
		if(gettype($conf)!="array"){
226 liveuser 7060
 
3 liveuser 7061
			#設置執行失敗
7062
			$result["status"]="false";
226 liveuser 7063
 
3 liveuser 7064
			#設置執行錯誤訊息
7065
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 7066
 
3 liveuser 7067
			#如果傳入的參數為 null
7068
			if($conf==null){
226 liveuser 7069
 
3 liveuser 7070
				#設置執行錯誤訊息
7071
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 7072
 
3 liveuser 7073
				}#if end
7074
 
7075
			#回傳結果
7076
			return $result;
226 liveuser 7077
 
3 liveuser 7078
			}#if end
226 liveuser 7079
 
3 liveuser 7080
		#檢查參數
7081
		#函式說明:
7082
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
7083
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7084
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
7085
		#$result["function"],當前執行的函式名稱.
7086
		#$result["argu"],設置給予的參數.
7087
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
7088
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
7089
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
7090
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
7091
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
7092
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
7093
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
7094
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
7095
		#必填寫的參數:
7096
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
226 liveuser 7097
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
3 liveuser 7098
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
7099
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
7100
		#可以省略的參數:
7101
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
7102
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("data","clientSock","serverSock","allSockArray");
7103
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
7104
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","resource","resource","array");
7105
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
7106
		#$conf["canBeEmptyString"]="false";
7107
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
7108
		#$conf["canNotBeEmpty"]=array();
7109
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
7110
		#$conf["canBeEmpty"]=array();
7111
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
7112
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("ip");
7113
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
7114
		#$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("ip","processFuncs");
226 liveuser 7115
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 7116
		#$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","array");
7117
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
7118
		#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("0.0.0.0",array());
7119
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
7120
		#$conf["disallowAllSkipableVarIsEmpty"]="";
7121
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
7122
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
7123
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
7124
		#$conf["arrayCountEqualCheck"][]=array();
7125
		#參考資料來源:
7126
		#array_keys=>http://php.net/manual/en/function.array-keys.php
7127
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
7128
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 7129
 
3 liveuser 7130
		#如果檢查參數失敗
7131
		if($checkArguments["status"]==="false"){
226 liveuser 7132
 
3 liveuser 7133
			#設置執行失敗
7134
			$result["status"]="false";
226 liveuser 7135
 
3 liveuser 7136
			#設置錯誤訊息
7137
			$result["error"]=$checkArguments;
226 liveuser 7138
 
3 liveuser 7139
			#回傳結果
7140
			return $result;
226 liveuser 7141
 
3 liveuser 7142
			}#if end
226 liveuser 7143
 
3 liveuser 7144
		#如果檢查參數不通過
7145
		if($checkArguments["passed"]==="false"){
226 liveuser 7146
 
3 liveuser 7147
			#設置執行失敗
7148
			$result["status"]="false";
226 liveuser 7149
 
3 liveuser 7150
			#設置錯誤訊息
7151
			$result["error"]=$checkArguments;
226 liveuser 7152
 
3 liveuser 7153
			#回傳結果
7154
			return $result;
226 liveuser 7155
 
3 liveuser 7156
			}#if end
7157
 
7158
		#針對輸入的內容
7159
		switch($conf["data"]){
7160
 
7161
			#如果是 "quit"
7162
			case "quit":
7163
 
7164
				#關閉與用戶之間的連線
7165
				socket_close($conf["clientSock"]);
7166
 
7167
				break;
7168
 
7169
			#如果是 "shutdown"
7170
			case "shutdown":
7171
 
7172
				#關閉 Server Listen 的 sock
7173
				socket_close($conf["serverSock"]);
7174
 
7175
				break;
7176
 
7177
			default:
7178
 
7179
			}#switch end
7180
 
7181
		}#function endService end
7182
 
7183
	/*
7184
	#函式說明:
7185
	#針對收到的內容,若是"quit"則斷開用戶的連線,若是"shutdown"則關閉Server Listen的socket,協定為UDP.
7186
	#回傳結果:
7187
	#無.
7188
	#必填參數:
7189
	#$conf["data"],字串,要處理的內容.
7190
	$conf["data"]="";
7191
	#$conf["clientKey"],resource socket key.
7192
	$conf["clientKey"]=;
7193
	#$conf["serverSock"],resource socket.
7194
	$conf["serverSock"]=;
7195
	#$conf["allSockArray"],resource陣列.
7196
	$conf["allSockArray"]=array();
7197
	#可省略參數:
7198
	#無.
7199
	#參考資料:
7200
	#無.
7201
	#備註:
7202
	#無.
7203
	*/
7204
	public function endServiceU(&$conf){
226 liveuser 7205
 
3 liveuser 7206
		#初始化要回傳的結果
7207
		$result=array();
7208
 
7209
		#取得當前執行的函數名稱
7210
		$result["function"]=__FUNCTION__;
226 liveuser 7211
 
3 liveuser 7212
		#如果沒有參數
7213
		if(func_num_args()==0){
226 liveuser 7214
 
3 liveuser 7215
			#設置執行失敗
7216
			$result["status"]="false";
226 liveuser 7217
 
3 liveuser 7218
			#設置執行錯誤訊息
7219
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 7220
 
3 liveuser 7221
			#回傳結果
7222
			return $result;
226 liveuser 7223
 
3 liveuser 7224
			}#if end
7225
 
7226
		#涵式說明:
7227
		#判斷當前環境為web還是cmd
7228
		#回傳結果:
7229
		#$result,"web"或"cmd"
7230
		if(csInformation::getEnv()==="web"){
226 liveuser 7231
 
3 liveuser 7232
			#設置執行失敗
7233
			$result["status"]="false";
226 liveuser 7234
 
3 liveuser 7235
			#設置執行錯誤訊息
7236
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
226 liveuser 7237
 
3 liveuser 7238
			#回傳結果
7239
			return $result;
226 liveuser 7240
 
3 liveuser 7241
			}#if end
226 liveuser 7242
 
3 liveuser 7243
		#取得參數
7244
		$result["argu"]=$conf;
7245
 
7246
		#如果 $conf 不為陣列
7247
		if(gettype($conf)!="array"){
226 liveuser 7248
 
3 liveuser 7249
			#設置執行失敗
7250
			$result["status"]="false";
226 liveuser 7251
 
3 liveuser 7252
			#設置執行錯誤訊息
7253
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 7254
 
3 liveuser 7255
			#如果傳入的參數為 null
7256
			if($conf==null){
226 liveuser 7257
 
3 liveuser 7258
				#設置執行錯誤訊息
7259
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 7260
 
3 liveuser 7261
				}#if end
7262
 
7263
			#回傳結果
7264
			return $result;
226 liveuser 7265
 
3 liveuser 7266
			}#if end
226 liveuser 7267
 
3 liveuser 7268
		#檢查參數
7269
		#函式說明:
7270
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
7271
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7272
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
7273
		#$result["function"],當前執行的函式名稱.
7274
		#$result["argu"],設置給予的參數.
7275
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
7276
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
7277
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
7278
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
7279
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
7280
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
7281
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
7282
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
7283
		#必填寫的參數:
7284
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
226 liveuser 7285
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
3 liveuser 7286
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
7287
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
7288
		#可以省略的參數:
7289
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
7290
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("data","clientKey","serverSock","allSockArray");
7291
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
7292
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string","resource","array");
7293
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
7294
		#$conf["canBeEmptyString"]="false";
7295
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
7296
		#$conf["canNotBeEmpty"]=array();
7297
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
7298
		#$conf["canBeEmpty"]=array();
7299
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
7300
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("ip");
7301
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
7302
		#$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("ip","processFuncs");
226 liveuser 7303
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 7304
		#$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","array");
7305
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
7306
		#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("0.0.0.0",array());
7307
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
7308
		#$conf["disallowAllSkipableVarIsEmpty"]="";
7309
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
7310
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
7311
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
7312
		#$conf["arrayCountEqualCheck"][]=array();
7313
		#參考資料來源:
7314
		#array_keys=>http://php.net/manual/en/function.array-keys.php
7315
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
7316
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 7317
 
3 liveuser 7318
		#如果檢查參數失敗
7319
		if($checkArguments["status"]==="false"){
226 liveuser 7320
 
3 liveuser 7321
			#設置執行失敗
7322
			$result["status"]="false";
226 liveuser 7323
 
3 liveuser 7324
			#設置錯誤訊息
7325
			$result["error"]=$checkArguments;
226 liveuser 7326
 
3 liveuser 7327
			#回傳結果
7328
			return $result;
226 liveuser 7329
 
3 liveuser 7330
			}#if end
226 liveuser 7331
 
3 liveuser 7332
		#如果檢查參數不通過
7333
		if($checkArguments["passed"]==="false"){
226 liveuser 7334
 
3 liveuser 7335
			#設置執行失敗
7336
			$result["status"]="false";
226 liveuser 7337
 
3 liveuser 7338
			#設置錯誤訊息
7339
			$result["error"]=$checkArguments;
226 liveuser 7340
 
3 liveuser 7341
			#回傳結果
7342
			return $result;
226 liveuser 7343
 
3 liveuser 7344
			}#if end
7345
 
7346
		#針對輸入的內容
7347
		switch($conf["data"]){
7348
 
7349
			#如果是 "quit"
7350
			case "quit":
7351
 
7352
				#從 $conf["clientKey"] 取得用戶的IP與port
7353
				$strArray=explode(":",$conf["clientKey"]);
7354
				$remote_ip=$strArray[0];
7355
				$remote_port=$strArray[1];
7356
 
7357
				#卸除儲存與用戶之間連線的socket變數
7358
				unset($conf["allSockArray"][$conf["clientKey"]]);
7359
 
7360
				break;
7361
 
7362
			#如果是 "shutdown"
7363
			case "shutdown":
7364
 
7365
				#關閉 Server Listen 的 sock
7366
				socket_close($conf["serverSock"]);
7367
 
7368
				break;
7369
 
7370
			default:
7371
 
7372
			}#switch end
7373
 
7374
		}#function endServiceU end
226 liveuser 7375
 
3 liveuser 7376
	/*
7377
	#函式說明:
7378
	#給予unixDomainSockServer函式的addOnProcessFunc參數用的函式,在背景執行指令.
7379
	#回傳結果:
7380
	#$result["status"],執行是否正常,"true"為正常,"false"為不正常.
7381
	#$result["error"],錯誤訊息陣列.
7382
	#$result["function"],當前執行的函數名稱.
7383
	#$result["argu"],使用的參數.
7384
	#$result["continue"],"true"代表執行到此函式為止;"false"代表允許後面的函式繼續執行.
7385
	#$result["cache"],要暫存在server端的內容.
7386
	#必填參數:
7387
	$conf["request"],json字串,key為"cmd2run"者,代表要執行的指令;key為"params"者,為要傳入的參數,一個元素代表一個參數.
7388
	#$conf["request"]="";
7389
	$conf["sock"],resource,用戶socket.
7390
	#$conf["sock"]=;
7391
	#可省略參數:
7392
	#$conf["clientCache"],陣列,讓client使用的cache.
7393
	#$conf["clientCache"]=array();
7394
	#參考資料:
7395
	#無.
7396
	#備註:
7397
	#無.
7398
	*/
7399
	public static function addOnProcessFuncForUnixDomainSockServerInRunWithAutoAddMemoryDaemon(&$conf=array()){
226 liveuser 7400
 
3 liveuser 7401
		#初始化要回傳的結果
7402
		$result=array();
7403
 
7404
		#取得當前執行的函數名稱
7405
		$result["function"]=__FUNCTION__;
226 liveuser 7406
 
3 liveuser 7407
		#取得參數
7408
		$result["argu"]=$conf;
226 liveuser 7409
 
3 liveuser 7410
		#允許後面的函式繼續執行
7411
		$result["continue"]="true";
7412
 
7413
		#如果 $conf 不為陣列
7414
		if(gettype($conf)!="array"){
7415
 
7416
			#設置執行失敗
7417
			$result["status"]="false";
7418
 
7419
			#設置執行錯誤訊息
7420
			$result["error"][]="\$conf變數須為陣列形態";
7421
 
7422
			#如果傳入的參數為 null
7423
			if(is_null($conf)){
7424
 
7425
				#設置執行錯誤訊息
7426
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
7427
 
7428
				}#if end
7429
 
7430
			#回傳結果
7431
			return $result;
7432
 
7433
			}#if end
226 liveuser 7434
 
3 liveuser 7435
		#函式說明:
7436
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
7437
		#回傳結果:
7438
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7439
		#$result["error"],執行不正常結束的錯訊息陣列.
7440
		#$result["simpleError"],簡單表示的錯誤訊息.
7441
		#$result["function"],當前執行的函式名稱.
7442
		#$result["argu"],設置給予的參數.
7443
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
7444
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
7445
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
7446
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
7447
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
7448
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
7449
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
7450
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
7451
		#必填參數:
7452
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
7453
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
7454
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
7455
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
7456
		#可省略參數:
7457
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
7458
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("request","socket");
7459
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
7460
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","resource");
7461
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
7462
		#$conf["canBeEmptyString"]="false";
7463
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
7464
		#$conf["canNotBeEmpty"]=array();
7465
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
7466
		#$conf["canBeEmpty"]=array();
7467
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
7468
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("last_req_time","info");
7469
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
7470
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("last_req_time","info","clientCache");
7471
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
7472
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","array","array");
7473
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
7474
		#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array($phpMaxMemInConfig,"10",null);
7475
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
7476
		#$conf["disallowAllSkipableVarIsEmpty"]="";
7477
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
7478
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
7479
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
7480
		#$conf["arrayCountEqualCheck"][]=array();
7481
		#參考資料:
7482
		#array_keys=>http://php.net/manual/en/function.array-keys.php
7483
		#備註:
7484
		#無.
7485
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
7486
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 7487
 
3 liveuser 7488
		#debug
7489
		#var_dump(__LINE__,$checkArguments);
226 liveuser 7490
 
3 liveuser 7491
		#如果執行失敗
7492
		if($checkArguments["status"]==="false"){
226 liveuser 7493
 
3 liveuser 7494
			#設置執行失敗
7495
			$result["status"]="false";
7496
 
7497
			#設置執行錯誤訊息
7498
			$result["error"]=$checkArguments;
226 liveuser 7499
 
3 liveuser 7500
			#回傳結果
7501
			return $result;
226 liveuser 7502
 
3 liveuser 7503
			}#if end
226 liveuser 7504
 
3 liveuser 7505
		#如果檢查不通過
7506
		if($checkArguments["passed"]==="false"){
226 liveuser 7507
 
3 liveuser 7508
			#設置執行失敗
7509
			$result["status"]="false";
7510
 
7511
			#設置執行錯誤訊息
7512
			$result["error"]=$checkArguments;
226 liveuser 7513
 
3 liveuser 7514
			#回傳結果
7515
			return $result;
226 liveuser 7516
 
7517
			}#if end
7518
 
3 liveuser 7519
		#將需求變成陣列
7520
		$request=(array)json_decode($conf["request"]);
226 liveuser 7521
 
3 liveuser 7522
		#函式說明:
7523
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
7524
		#回傳結果:
7525
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7526
		#$result["error"],執行不正常結束的錯訊息陣列.
7527
		#$result["simpleError"],簡單表示的錯誤訊息.
7528
		#$result["function"],當前執行的函式名稱.
7529
		#$result["argu"],設置給予的參數.
7530
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
7531
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
7532
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
7533
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
7534
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
7535
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
7536
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
7537
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
7538
		#必填參數:
7539
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
7540
		$conf["variableCheck::checkArguments"]["varInput"]=&$request;
7541
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
7542
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
7543
		#可省略參數:
7544
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
7545
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("cmd2run");
7546
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
7547
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string");
7548
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
7549
		#$conf["variableCheck::checkArguments"]["canBeEmptyString"]="true";
7550
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
7551
		#$conf["variableCheck::checkArguments"]["canNotBeEmpty"]=array("cmd");
7552
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
7553
		#$conf["canBeEmpty"]=array();
7554
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
7555
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("params");
7556
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
7557
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("id","params");
7558
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
7559
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","array");
7560
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
7561
		#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array($phpMaxMemInConfig,"10",null);
7562
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
7563
		#$conf["disallowAllSkipableVarIsEmpty"]="";
7564
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
7565
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
7566
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
7567
		#$conf["arrayCountEqualCheck"][]=array();
7568
		#參考資料:
7569
		#array_keys=>http://php.net/manual/en/function.array-keys.php
7570
		#備註:
7571
		#無.
7572
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
7573
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 7574
 
3 liveuser 7575
		#debug
7576
		#var_dump(__LINE__,$checkArguments);
226 liveuser 7577
 
3 liveuser 7578
		#如果執行失敗
7579
		if($checkArguments["status"]==="false"){
226 liveuser 7580
 
3 liveuser 7581
			#設置執行失敗
7582
			$result["status"]="false";
7583
 
7584
			#設置執行錯誤訊息
7585
			$result["error"]=$checkArguments;
226 liveuser 7586
 
3 liveuser 7587
			#回傳結果
7588
			return $result;
226 liveuser 7589
 
3 liveuser 7590
			}#if end
226 liveuser 7591
 
3 liveuser 7592
		#如果檢查不通過
7593
		if($checkArguments["passed"]==="false"){
226 liveuser 7594
 
3 liveuser 7595
			#設置執行失敗
7596
			$result["status"]="false";
7597
 
7598
			#設置執行錯誤訊息
7599
			$result["error"]=$checkArguments;
226 liveuser 7600
 
3 liveuser 7601
			#回傳結果
7602
			return $result;
226 liveuser 7603
 
3 liveuser 7604
			}#if end
226 liveuser 7605
 
3 liveuser 7606
		#函式說明:
7607
		#呼叫shell執行系統命令,並取得回傳的內容.
7608
		#回傳結果:
7609
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7610
		#$result["error"],錯誤訊息陣列.
7611
		#$result["function"],當前執行的函數名稱.
7612
		#$result["argu"],使用的參數.
7613
		#$result["cmd"],執行的指令內容.
7614
		#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
7615
		#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
7616
		#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
7617
		#$result["running"],是否還在執行.
7618
		#$result["pid"],pid.
7619
		#$result["statusCode"],執行結束後的代碼.
7620
		#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
7621
		#必填參數:
7622
		#$conf["command"],字串,要執行的指令.
7623
		$conf["external::callShell"]["command"]=$request["cmd2run"];
7624
		#$conf["fileArgu"],字串,變數__FILE__的內容.
7625
		$conf["external::callShell"]["fileArgu"]=__FILE__;
7626
		#可省略參數:
7627
		#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
7628
		$conf["external::callShell"]["argu"]=$request["params"];
7629
		#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
7630
		#$conf["arguIsAddr"]=array();
7631
		#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
7632
		#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
7633
		#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
7634
		#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
7635
		#$conf["enablePrintDescription"]="true";
7636
		#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
7637
		#$conf["printDescription"]="";
7638
		#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
7639
		$conf["external::callShell"]["escapeshellarg"]="true";
7640
		#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
7641
		#$conf["thereIsShellVar"]=array();
7642
		#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
7643
		#$conf["username"]="";
7644
		#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
7645
		#$conf["password"]="";
7646
		#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
7647
		#$conf["useScript"]="";
7648
		#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
7649
		#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
7650
		#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
7651
		#$conf["inBackGround"]="";
7652
		#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
7653
		#$conf["getErr"]="false";
7654
		#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
7655
		$conf["external::callShell"]["doNotRun"]="true";
7656
		#參考資料:
7657
		#exec=>http://php.net/manual/en/function.exec.php
7658
		#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
7659
		#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
7660
		#備註:
7661
		#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
7662
		#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
7663
		$callShell=external::callShell($conf["external::callShell"]);
7664
		unset($conf["external::callShell"]);
226 liveuser 7665
 
3 liveuser 7666
		#如果執行失敗
7667
		if($callShell["status"]==="false"){
226 liveuser 7668
 
3 liveuser 7669
			#設置執行失敗
7670
			$result["status"]="false";
226 liveuser 7671
 
3 liveuser 7672
			#設置執行錯誤訊息
7673
			$result["error"]=$callShell;
226 liveuser 7674
 
3 liveuser 7675
			#回傳結果
7676
			return $result;
226 liveuser 7677
 
3 liveuser 7678
			}#if end
226 liveuser 7679
 
3 liveuser 7680
		#函式說明:
7681
		#將一維陣列轉換為用特定符號間隔的字串,ex:array("1","2","3") to "a;b;c;".
7682
		#回傳的結果:
7683
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7684
		#$result["function"],當前執行的function名稱
7685
		#$result["error"],錯誤訊息陣列.
7686
		#$result["content"],處理好的字串.
7687
		#$result["argu"],使用的參數.
7688
		#必填參數:
7689
		#$conf["inputArray"],字串陣列,要轉成字串的一維陣列.
7690
		$conf["arrays::arrayToString"]["inputArray"]=$callShell["escape"]["array"];
7691
		#可省略參數:
7692
		#$conf["spiltSymbol"],字串,用來區隔字串的符號,預設為;
7693
		$conf["arrays::arrayToString"]["spiltSymbol"]=" ";
7694
		#$conf["skipEnd"],字串,結尾是否不要加上符號,預設為"false",要加上符號,"true"代表不要加上符號。
7695
		$conf["arrays::arrayToString"]["skipEnd"]="true";
7696
		#參考資料:
7697
		#無.
7698
		#備註:
7699
		#無.
7700
		$arrayToString=arrays::arrayToString($conf["arrays::arrayToString"]);
7701
		unset($conf["arrays::arrayToString"]);
226 liveuser 7702
 
3 liveuser 7703
		#如果執行失敗
7704
		if($arrayToString["status"]==="false"){
226 liveuser 7705
 
3 liveuser 7706
			#設置執行失敗
7707
			$result["status"]="false";
226 liveuser 7708
 
3 liveuser 7709
			#設置執行錯誤訊息
7710
			$result["error"]=$arrayToString;
226 liveuser 7711
 
3 liveuser 7712
			#回傳結果
7713
			return $result;
226 liveuser 7714
 
3 liveuser 7715
			}#if end
226 liveuser 7716
 
3 liveuser 7717
		#函式說明:
7718
		#透過proc來多執行序運作.
7719
		#回傳結果:
7720
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7721
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
7722
		#$result["function"],當前執行的函式名稱.
7723
		#$result["argu"],使用的參數.
7724
		#$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"代表程序的資訊.
7725
		#必填參數:
7726
		#$conf["cmds"],字串陣列,每個元素代表要執行的指令與參數.
7727
		$conf["threads::proc"]["cmds"]=array($arrayToString["content"]);
7728
		#可省略參數:
7729
		#$conf["wait"],字串,是否需要等待所有程序結束,預設為"true"要等待;反之為"false"不要等待.
7730
		#$conf["threads::proc"]["wait"]="false";
7731
		#$conf["workingDir"],字串陣列,個別程式執行時的家目錄,預設不指定.
7732
		#$conf["workingDir"]=array("path");
7733
		#$conf["envs"],2維字串陣列,每個元素代表個別程式執行時的指定環境變數,key變數名稱;value為變數內容.預設為array("QBPWCF" => "Quick Build PHP Website Componment base on Fedora Linux");
7734
		#$conf["envs"]=array(array("key"=>"value"));
7735
		#$conf["executeBy"],字串陣列,每個元素代表個別指令要用什麼程式執行,預設為"bash".
7736
		#$conf["executeBy"]=array("bash");
7737
		#參考資料:
7738
		#https://www.php.net/manual/en/function.proc-open.php
7739
		#https://www.php.net/manual/en/function.proc-get-status.php
7740
		#備註:
7741
		#無.
7742
		$proc=threads::proc($conf["threads::proc"]);
7743
		unset($conf["threads::proc"]);
226 liveuser 7744
 
3 liveuser 7745
		/* debug
226 liveuser 7746
 
3 liveuser 7747
		#函式說明:
7748
		#撰寫log
7749
		#回傳結果:
7750
		#$result["status"],狀態,"true"或"false".
7751
		#$result["error"],錯誤訊息陣列.
7752
		#$result["function"],當前函式的名稱.
7753
		#$result["argu"],使用的參數.
7754
		#$result["content"],要寫入log的內容字串.
7755
		#必填參數:
7756
		#$conf["path"],字串,log檔案的路徑與名稱.
7757
		$conf["logs::record"]["path"]=$conf["socket"]."(sock::addOnProcessFuncForUnixDomainSockServerInRunWithAutoAddMemoryDaemon).log";
7758
		#$conf["content"],any,要寫的內容,若內容不為字串則會用var_dump的格式寫入.
7759
		$conf["logs::record"]["content"]=$proc;
7760
		#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
7761
		$conf["logs::record"]["fileArgu"]=__FILE__;
7762
		#可省略參數:
7763
		#$conf["rewrite"],預設為"false",接續寫入;反之"true"代表重新寫入.
7764
		#$conf["rewrite"]="false";
7765
		#$conf["returnOnly"],預設為"false",會寫入到log檔案.若為"true"則不會寫入log.
7766
		#$conf["returnOnly"]="true";
7767
		#參考資料:
7768
		#無.
7769
		#備註:
7770
		#無.
7771
		$record=logs::record($conf["logs::record"]);
7772
		unset($conf["logs::record"]);
226 liveuser 7773
 
3 liveuser 7774
		#如果執行失敗
7775
		if($record["status"]==="false"){
226 liveuser 7776
 
3 liveuser 7777
			#設置執行錯誤識別
7778
			$result["status"]="false";
226 liveuser 7779
 
3 liveuser 7780
			#設置執行錯誤
7781
			$result["error"]=$record;
226 liveuser 7782
 
3 liveuser 7783
			#回傳結果
7784
			return $result;
226 liveuser 7785
 
3 liveuser 7786
			}#if end
226 liveuser 7787
 
3 liveuser 7788
		*/
226 liveuser 7789
 
3 liveuser 7790
		#設置要暫存在server端的內容
7791
		$result["cache"]=&$proc;
226 liveuser 7792
 
3 liveuser 7793
		#如果執行失敗
7794
		if($proc["status"]==="false"){
226 liveuser 7795
 
3 liveuser 7796
			#設置執行失敗
7797
			$result["status"]="false";
226 liveuser 7798
 
3 liveuser 7799
			#設置執行錯誤訊息
7800
			$result["error"]=$proc;
226 liveuser 7801
 
3 liveuser 7802
			#回傳結果
7803
			return $result;
226 liveuser 7804
 
3 liveuser 7805
			}#if end
226 liveuser 7806
 
3 liveuser 7807
		#設置執行正常
7808
		$result["status"]="true";
226 liveuser 7809
 
3 liveuser 7810
		#後面的程式不要執行
7811
		$result["continue"]="false";
226 liveuser 7812
 
3 liveuser 7813
		#回傳結果
7814
		return $result;
226 liveuser 7815
 
3 liveuser 7816
		}#function addOnProcessFuncForUnixDomainSockServerInRunWithAutoAddMemoryDaemon end
226 liveuser 7817
 
3 liveuser 7818
	/*
7819
	#函式說明:
7820
	#執行程式時,依照需要增加記憶體的上限.
7821
	#回傳結果:
7822
	#$result["status"],執行是否正常,"true"為正常,"false"為不正常.
7823
	#$result["error"],錯誤訊息陣列.
7824
	#$result["function"],當前執行的函數名稱.
7825
	#$result["argu"],使用的參數.
7826
	#$result["content"],程式執行後的結果(unix domain socket server的輸出).
7827
	#必填參數:
7828
	#$conf["cmd"],字串參數,要執行的指令.
7829
	$conf["cmd"]="";
7830
	#可省略參數:
7831
	#$conf["params"],陣列字串參數,每個元素代表依序哦昂使用的參數.
7832
	#$conf["params"]=array("");
7833
	#$conf["fileArgu"],字串,__FILE__的內容.
7834
	#$conf["fileArgu"]="";
7835
	#參考資料:
7836
	#https://www.php.net/manual/en/function.memory-get-usage.php
7837
	#https://www.php.net/manual/en/ini.core.php#ini.memory-limit
7838
	#https://www.php.net/manual/en/function.stream-get-meta-data.php
7839
	#備註:
7840
	#無.
7841
	*/
7842
	public static function runWithAutoAddMemoryDaemon(&$conf=array()){
226 liveuser 7843
 
3 liveuser 7844
		#初始化要回傳的結果
7845
		$result=array();
7846
 
7847
		#取得當前執行的函數名稱
7848
		$result["function"]=__FUNCTION__;
226 liveuser 7849
 
3 liveuser 7850
		#取得參數
7851
		$result["argu"]=$conf;
7852
 
7853
		#如果 $conf 不為陣列
7854
		if(gettype($conf)!="array"){
7855
 
7856
			#設置執行失敗
7857
			$result["status"]="false";
7858
 
7859
			#設置執行錯誤訊息
7860
			$result["error"][]="\$conf變數須為陣列形態";
7861
 
7862
			#如果傳入的參數為 null
7863
			if(is_null($conf)){
7864
 
7865
				#設置執行錯誤訊息
7866
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
7867
 
7868
				}#if end
7869
 
7870
			#回傳結果
7871
			return $result;
7872
 
7873
			}#if end
226 liveuser 7874
 
3 liveuser 7875
		#檢查參數
7876
		#函式說明:
7877
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
7878
		#回傳結果:
7879
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7880
		#$result["error"],執行不正常結束的錯訊息陣列.
7881
		#$result["simpleError"],簡單表示的錯誤訊息.
7882
		#$result["function"],當前執行的函式名稱.
7883
		#$result["argu"],設置給予的參數.
7884
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
7885
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
7886
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
7887
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
7888
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
7889
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
7890
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
7891
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
7892
		#必填參數:
7893
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
7894
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
7895
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
7896
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
7897
		#可省略參數:
7898
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
7899
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("cmd");
7900
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
7901
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string");
7902
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
7903
		$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
7904
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
7905
		#$conf["canNotBeEmpty"]=array();
7906
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
7907
		#$conf["canBeEmpty"]=array();
7908
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
7909
		$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("params","fileArgu");
7910
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
7911
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("params","fileArgu");
7912
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
7913
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("array","string");
7914
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
7915
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(null,__FILE__);
7916
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
7917
		#$conf["disallowAllSkipableVarIsEmpty"]="";
7918
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
7919
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
7920
		#$conf["disallowAllSkipableVarNotExist"],字串,是否不允許每個可省略參數都不存在,預設為"false"代表允許,反之為"true".
7921
		#$conf["disallowAllSkipableVarNotExist"]="";
7922
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
7923
		#$conf["arrayCountEqualCheck"][]=array();
7924
		#參考資料:
7925
		#array_keys=>http://php.net/manual/en/function.array-keys.php
7926
		#備註:
7927
		#無.
7928
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
7929
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 7930
 
3 liveuser 7931
		#如果執行失敗
7932
		if($checkArguments["status"]==="false"){
226 liveuser 7933
 
3 liveuser 7934
			#設置執行失敗
7935
			$result["status"]="false";
226 liveuser 7936
 
3 liveuser 7937
			#設置錯誤資訊
7938
			$result["error"]=$checkArguments;
226 liveuser 7939
 
3 liveuser 7940
			#回傳結果
7941
			return $result;
226 liveuser 7942
 
3 liveuser 7943
			}#if end
226 liveuser 7944
 
3 liveuser 7945
		#如果檢查不通過
7946
		if($checkArguments["passed"]==="false"){
226 liveuser 7947
 
3 liveuser 7948
			#設置執行失敗
7949
			$result["status"]="false";
226 liveuser 7950
 
3 liveuser 7951
			#設置錯誤資訊
7952
			$result["error"]=$checkArguments;
226 liveuser 7953
 
3 liveuser 7954
			#回傳結果
7955
			return $result;
226 liveuser 7956
 
3 liveuser 7957
			}#if end
226 liveuser 7958
 
3 liveuser 7959
		/*
7960
		#函式說明:
226 liveuser 7961
		#於 ./tmp 底下建立暫存目錄與回傳暫存檔案名稱路徑
3 liveuser 7962
		#回傳結果:
7963
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7964
		#$result["error"],錯誤訊息.
7965
		#$result["function"],當前執行的函數名稱.
7966
		#$result["content"],暫存檔案的路徑與名稱.
7967
		#必填參數:
7968
		#無.
7969
		#可省略參數:
7970
		#無.
7971
		#參考資料:
7972
		#無.
7973
		#備註:
7974
		#無.
7975
		*/
34 liveuser 7976
		$conf["fileAccess::createTempFile"]=array();
7977
		$createTempFile=fileAccess::createTempFile($conf["fileAccess::createTempFile"]);
7978
		unset($conf["fileAccess::createTempFile"]);
226 liveuser 7979
 
3 liveuser 7980
		#如果執行失敗
7981
		if($createTempFile["status"]==="false"){
226 liveuser 7982
 
3 liveuser 7983
			#設置執行失敗
7984
			$result["status"]="false";
7985
 
7986
			#設置執行錯誤訊息
7987
			$result["error"]=$createTempFile;
226 liveuser 7988
 
3 liveuser 7989
			#回傳結果
7990
			return $result;
226 liveuser 7991
 
3 liveuser 7992
			}#if end
226 liveuser 7993
 
3 liveuser 7994
		#取得暫存的 unix domain socket 檔案
7995
		$tmpUnixDomainSock=$createTempFile["content"];
226 liveuser 7996
 
3 liveuser 7997
		#初始化用來運行  unixDomainSockServer.php 的參數
7998
		$argu=array();
226 liveuser 7999
 
3 liveuser 8000
		#設置參數
8001
		$argu[]="--sock";
226 liveuser 8002
 
3 liveuser 8003
		#設置參數
8004
		$argu[]=$tmpUnixDomainSock;
226 liveuser 8005
 
3 liveuser 8006
		#設置參數
8007
		$argu[]="--addOnProcessFunc";
226 liveuser 8008
 
3 liveuser 8009
		#設置參數
8010
		$argu[]="\qbpwcf\sock::addOnProcessFuncForUnixDomainSockServerInRunWithAutoAddMemoryDaemon";
226 liveuser 8011
 
3 liveuser 8012
		#設置參數
8013
		$argu[]="--funcToRunWhenIdle";
226 liveuser 8014
 
3 liveuser 8015
		#設置參數
8016
		$argu[]="\qbpwcf\config::autoAddMemory";
226 liveuser 8017
 
3 liveuser 8018
		#設置參數
8019
		$argu[]="--onlyOutputCache";
226 liveuser 8020
 
3 liveuser 8021
		#設置參數
8022
		$argu[]="true";
226 liveuser 8023
 
3 liveuser 8024
		#設置參數
8025
		$argu[]="--cacheLayer";
226 liveuser 8026
 
3 liveuser 8027
		#設置參數
8028
		$argu[]="serverCache";
226 liveuser 8029
 
3 liveuser 8030
		#設置參數
8031
		$argu[]="--cacheLayer";
226 liveuser 8032
 
3 liveuser 8033
		#設置參數
8034
		$argu[]="serverSide";
226 liveuser 8035
 
3 liveuser 8036
		#設置參數
8037
		$argu[]="--cacheLayer";
226 liveuser 8038
 
3 liveuser 8039
		#設置參數
8040
		$argu[]="funcToRunWhenIdle";
226 liveuser 8041
 
3 liveuser 8042
		#設置參數
8043
		$argu[]="--cacheLayer";
226 liveuser 8044
 
3 liveuser 8045
		#設置參數
8046
		$argu[]="\qbpwcf\config::autoAddMemory";
226 liveuser 8047
 
3 liveuser 8048
		#設置參數
8049
		$argu[]="--cacheLayer";
226 liveuser 8050
 
3 liveuser 8051
		#設置參數
8052
		$argu[]="cache";
226 liveuser 8053
 
3 liveuser 8054
		#設置參數
8055
		$argu[]="--cacheLayer";
226 liveuser 8056
 
3 liveuser 8057
		#設置參數
8058
		$argu[]="stdout";
226 liveuser 8059
 
3 liveuser 8060
		#函式說明:
8061
		#呼叫shell執行系統命令,並取得回傳的內容.
8062
		#回傳結果:
8063
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
8064
		#$result["error"],錯誤訊息陣列.
8065
		#$result["function"],當前執行的函數名稱.
8066
		#$result["argu"],使用的參數.
8067
		#$result["cmd"],執行的指令內容.
8068
		#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
8069
		#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
8070
		#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
8071
		#$result["running"],是否還在執行.
8072
		#$result["pid"],pid.
8073
		#$result["statusCode"],執行結束後的代碼.
8074
		#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
8075
		#必填參數:
8076
		#$conf["command"],字串,要執行的指令.
8077
		$conf["external::callShell"]["command"]="unixDomainSockServer.php";
8078
		#$conf["fileArgu"],字串,變數__FILE__的內容.
8079
		$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];
8080
		#可省略參數:
8081
		#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
8082
		$conf["external::callShell"]["argu"]=$argu;
8083
		#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
8084
		#$conf["arguIsAddr"]=array();
8085
		#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
8086
		#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
8087
		#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
8088
		#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
8089
		#$conf["enablePrintDescription"]="true";
8090
		#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
8091
		#$conf["printDescription"]="";
8092
		#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
8093
		$conf["external::callShell"]["escapeshellarg"]="true";
8094
		#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
8095
		#$conf["thereIsShellVar"]=array();
8096
		#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
8097
		#$conf["username"]="";
8098
		#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
8099
		#$conf["password"]="";
8100
		#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
8101
		#$conf["useScript"]="";
8102
		#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
8103
		#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
8104
		#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
8105
		#$conf["inBackGround"]="";
8106
		#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
8107
		#$conf["getErr"]="false";
8108
		#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
8109
		$conf["external::callShell"]["doNotRun"]="true";
8110
		#參考資料:
8111
		#exec=>http://php.net/manual/en/function.exec.php
8112
		#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
8113
		#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
8114
		#備註:
8115
		#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
8116
		#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
8117
		$callShell=external::callShell($conf["external::callShell"]);
8118
		unset($conf["external::callShell"]);
226 liveuser 8119
 
3 liveuser 8120
		#debug
8121
		#var_dump(__LINE__,$callShell);exit;
226 liveuser 8122
 
3 liveuser 8123
		#如果執行失敗
8124
		if($callShell["status"]==="false"){
226 liveuser 8125
 
3 liveuser 8126
			#如果socket存在
8127
			if(file_exists($tmpUnixDomainSock)){
226 liveuser 8128
 
3 liveuser 8129
				#移除之
8130
				unlink($tmpUnixDomainSock);
226 liveuser 8131
 
3 liveuser 8132
				}#if end
226 liveuser 8133
 
3 liveuser 8134
			#設置執行失敗
8135
			$result["status"]="false";
8136
 
8137
			#設置執行錯誤訊息
8138
			$result["error"]=$callShell;
226 liveuser 8139
 
3 liveuser 8140
			#回傳結果
8141
			return $result;
226 liveuser 8142
 
3 liveuser 8143
			}#if end
226 liveuser 8144
 
3 liveuser 8145
		#函式說明:
8146
		#將一維陣列轉換為用特定符號間隔的字串,ex:array("1","2","3") to "a;b;c;".
8147
		#回傳的結果:
8148
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
8149
		#$result["function"],當前執行的function名稱
8150
		#$result["error"],錯誤訊息陣列.
8151
		#$result["content"],處理好的字串.
8152
		#$result["argu"],使用的參數.
8153
		#必填參數:
8154
		#$conf["inputArray"],字串陣列,要轉成字串的一維陣列.
8155
		$conf["arrays::arrayToString"]["inputArray"]=$callShell["escape"]["array"];
8156
		#可省略參數:
8157
		#$conf["spiltSymbol"],字串,用來區隔字串的符號,預設為;
8158
		$conf["arrays::arrayToString"]["spiltSymbol"]=" ";
8159
		#$conf["skipEnd"],字串,結尾是否不要加上符號,預設為"false",要加上符號,"true"代表不要加上符號。
8160
		$conf["arrays::arrayToString"]["skipEnd"]="true";
8161
		#參考資料:
8162
		#無.
8163
		#備註:
8164
		#無.
8165
		$arrayToString=arrays::arrayToString($conf["arrays::arrayToString"]);
8166
		unset($conf["arrays::arrayToString"]);
226 liveuser 8167
 
3 liveuser 8168
		#如果執行失敗
8169
		if($arrayToString["status"]==="false"){
226 liveuser 8170
 
3 liveuser 8171
			#如果socket存在
8172
			if(file_exists($tmpUnixDomainSock)){
226 liveuser 8173
 
3 liveuser 8174
				#移除之
8175
				unlink($tmpUnixDomainSock);
226 liveuser 8176
 
3 liveuser 8177
				}#if end
226 liveuser 8178
 
3 liveuser 8179
			#設置執行失敗
8180
			$result["status"]="false";
8181
 
8182
			#設置執行錯誤訊息
8183
			$result["error"]=$arrayToString;
226 liveuser 8184
 
3 liveuser 8185
			#回傳結果
8186
			return $result;
226 liveuser 8187
 
3 liveuser 8188
			}#if end
226 liveuser 8189
 
3 liveuser 8190
		#函式說明:
8191
		#透過proc來多執行序運作.
8192
		#回傳結果:
8193
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
8194
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
8195
		#$result["function"],當前執行的函式名稱.
8196
		#$result["argu"],使用的參數.
8197
		#$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"代表程序的資訊.
8198
		#必填參數:
8199
		#$conf["cmds"],字串陣列,每個元素代表要執行的指令與參數.
8200
		$conf["threads::proc"]["cmds"]=array($arrayToString["content"]);
8201
		#可省略參數:
8202
		#$conf["wait"],字串,是否需要等待所有程序結束,預設為"true"要等待;反之為"false"不要等待.
8203
		$conf["threads::proc"]["wait"]="false";
8204
		#$conf["workingDir"],字串陣列,個別程式執行時的家目錄,預設不指定.
8205
		#$conf["workingDir"]=array("path");
8206
		#$conf["envs"],2維字串陣列,每個元素代表個別程式執行時的指定環境變數,key變數名稱;value為變數內容.預設為array("QBPWCF" => "Quick Build PHP Website Componment base on Fedora Linux");
8207
		#$conf["envs"]=array(array("key"=>"value"));
8208
		#$conf["executeBy"],字串陣列,每個元素代表個別指令要用什麼程式執行,預設為"bash".
8209
		#$conf["executeBy"]=array("bash");
8210
		#參考資料:
8211
		#https://www.php.net/manual/en/function.proc-open.php
8212
		#https://www.php.net/manual/en/function.proc-get-status.php
8213
		#備註:
8214
		#無.
226 liveuser 8215
		$procForUnixDoaminSocket=threads::proc($conf["threads::proc"]);
8216
		unset($conf["threads::proc"]);
8217
 
3 liveuser 8218
		#如果執行失敗
8219
		if($procForUnixDoaminSocket["status"]==="false"){
226 liveuser 8220
 
3 liveuser 8221
			#如果socket存在
8222
			if(file_exists($tmpUnixDomainSock)){
226 liveuser 8223
 
3 liveuser 8224
				#移除之
8225
				unlink($tmpUnixDomainSock);
226 liveuser 8226
 
3 liveuser 8227
				}#if end
226 liveuser 8228
 
3 liveuser 8229
			#設置執行失敗
8230
			$result["status"]="false";
8231
 
8232
			#設置執行錯誤訊息
8233
			$result["error"]=$procForUnixDoaminSocket;
226 liveuser 8234
 
3 liveuser 8235
			#回傳結果
8236
			return $result;
226 liveuser 8237
 
3 liveuser 8238
			}#if end
226 liveuser 8239
 
3 liveuser 8240
		#var_dump($procForUnixDoaminSocket);exit;
226 liveuser 8241
 
3 liveuser 8242
		#如果 unix doamin socket server 沒有在運行
8243
		if($procForUnixDoaminSocket["content"][0]["proc_get_status"]["running"]===false){
226 liveuser 8244
 
3 liveuser 8245
			#如果socket存在
8246
			if(file_exists($tmpUnixDomainSock)){
226 liveuser 8247
 
3 liveuser 8248
				#移除之
8249
				unlink($tmpUnixDomainSock);
226 liveuser 8250
 
3 liveuser 8251
				}#if end
226 liveuser 8252
 
3 liveuser 8253
			#設置執行失敗
8254
			$result["status"]="false";
8255
 
8256
			#設置執行錯誤訊息
8257
			$result["error"]=$procForUnixDoaminSocket;
226 liveuser 8258
 
3 liveuser 8259
			#回傳結果
8260
			return $result;
226 liveuser 8261
 
3 liveuser 8262
			}#if end
226 liveuser 8263
 
3 liveuser 8264
		#如果當下 unix doamin socket server 沒有在運行
8265
		if(proc_get_status($procForUnixDoaminSocket["content"][0]["process"])["running"]===false){
226 liveuser 8266
 
3 liveuser 8267
			#如果socket存在
8268
			if(file_exists($tmpUnixDomainSock)){
226 liveuser 8269
 
3 liveuser 8270
				#移除之
8271
				unlink($tmpUnixDomainSock);
226 liveuser 8272
 
3 liveuser 8273
				}#if end
226 liveuser 8274
 
3 liveuser 8275
			#設置執行失敗
8276
			$result["status"]="false";
8277
 
8278
			#設置執行錯誤訊息
8279
			$result["error"]=$procForUnixDoaminSocket;
226 liveuser 8280
 
3 liveuser 8281
			#回傳結果
8282
			return $result;
226 liveuser 8283
 
3 liveuser 8284
			}#if end
226 liveuser 8285
 
3 liveuser 8286
		#debug
8287
		#var_dump(__LINE__,"before send msg");
226 liveuser 8288
 
3 liveuser 8289
		#初始化要求執行程式的訊息
8290
		$msg=array();
226 liveuser 8291
 
3 liveuser 8292
		#設置執行的指令
8293
		$msg["cmd2run"]=$conf["cmd"];
226 liveuser 8294
 
3 liveuser 8295
		#如果有參數
8296
		if(isset($conf["params"])){
226 liveuser 8297
 
3 liveuser 8298
			#設置參數
8299
			$msg["params"]=$conf["params"];
226 liveuser 8300
 
3 liveuser 8301
			}#if end
226 liveuser 8302
 
3 liveuser 8303
		#初始化儲存等待了幾秒
8304
		$waitSec=0;
226 liveuser 8305
 
3 liveuser 8306
		#無窮迴圈
8307
		while(true){
226 liveuser 8308
 
3 liveuser 8309
			#函式說明:
8310
			#連線到 unixDomainSockServer 提供的 unix domain socket.
8311
			#回傳結果:
8312
			#$result["status"],"true"代表執行正常;"false"代表執行不正常.
8313
			#$result["error"],錯誤訊息陣列.
8314
			#$result["function"],當前執行的函式名稱.
8315
			#$result["content"],取得的回應.
8316
			#必填參數:
8317
			#$conf["sock"],字串,要連線的unix domain socket.
8318
			$conf["sock::unixDomainSockClient"]["sock"]=$tmpUnixDomainSock;
8319
			#可省略參數:
8320
			#$conf["id"],字串,取得的id,若無此值,則會得到新的數值.
8321
			#$conf["id"]="";
8322
			#$conf["cmd"],字串,要執行的指令,當$conf["id"]參數合法時,才會執行.
8323
			#$conf["sock::unixDomainSockClient"]["cmd"]=$callShell["escape"]["cmd"];
8324
			#$conf["param"],參數陣列.
8325
			#$conf["sock::unixDomainSockClient"]["param"]=$callShell["escape"]["argu"];
8326
			#$conf["escaped"],字串,param參數是否已經escaped了,預設為"false",反之為"true".
8327
			#$conf["sock::unixDomainSockClient"]["escaped"]="true";
8328
			#$conf["custom"],陣列,要客制化傳輸的內容,會覆蓋以上除了$conf["id"]以外的可省略參數.
8329
			#$conf["custom"]=array();
8330
			#參考資料:
8331
			#http://php.net/manual/en/function.stream-socket-client.php
8332
			#http://php.net/manual/en/function.stream-get-contents.php
8333
			#備註:
8334
			#無.
8335
			$paramsOfUnixDomainSockClient=$conf["sock::unixDomainSockClient"];
8336
			$unixDomainSockClient=sock::unixDomainSockClient($paramsOfUnixDomainSockClient);
8337
			unset($paramsOfUnixDomainSockClient);
226 liveuser 8338
 
3 liveuser 8339
			#如果執行成功
8340
			if($unixDomainSockClient["status"]==="true"){
226 liveuser 8341
 
3 liveuser 8342
				#debug
8343
				#var_dump(__LINE__,"before parse id");
226 liveuser 8344
 
3 liveuser 8345
				#跳出while
8346
				break;
226 liveuser 8347
 
3 liveuser 8348
				}#if end
226 liveuser 8349
 
3 liveuser 8350
			#提示錯誤
8351
			else{
226 liveuser 8352
 
3 liveuser 8353
				#debug
8354
				#var_dump(__LINE__,$unixDomainSockClient);
226 liveuser 8355
 
3 liveuser 8356
				}#else end
226 liveuser 8357
 
3 liveuser 8358
			#如果當下 unix doamin socket server 沒有在運行
8359
			if(proc_get_status($procForUnixDoaminSocket["content"][0]["process"])["running"]===false){
226 liveuser 8360
 
3 liveuser 8361
				#如果socket存在
8362
				if(file_exists($tmpUnixDomainSock)){
226 liveuser 8363
 
3 liveuser 8364
					#移除之
8365
					unlink($tmpUnixDomainSock);
226 liveuser 8366
 
3 liveuser 8367
					}#if end
226 liveuser 8368
 
3 liveuser 8369
				#設置執行失敗
8370
				$result["status"]="false";
8371
 
8372
				#設置執行錯誤訊息
8373
				$result["error"][]="client取得回應前server就意外結束了";
8374
 
8375
				#設置執行錯誤訊息
8376
				$result["error"][]=$procForUnixDoaminSocket;
226 liveuser 8377
 
3 liveuser 8378
				#回傳結果
8379
				return $result;
226 liveuser 8380
 
3 liveuser 8381
				}#if end
226 liveuser 8382
 
3 liveuser 8383
			#等後1秒,擔心unix domain socket server 尚未跑起來.
8384
			sleep(1);
226 liveuser 8385
 
3 liveuser 8386
			#已等待時間加1秒
8387
			$waitSec=$waitSec+1;
226 liveuser 8388
 
3 liveuser 8389
			}#while end
226 liveuser 8390
 
3 liveuser 8391
		#debug
8392
		#var_dump(__LINE__,"json string got:".$unixDomainSockClient["content"]);
226 liveuser 8393
 
3 liveuser 8394
		#取得json回應
8395
		$jsonRes=json_decode($unixDomainSockClient["content"]);
226 liveuser 8396
 
3 liveuser 8397
		#如果執行失敗
8398
		if($jsonRes===null){
226 liveuser 8399
 
3 liveuser 8400
			#如果socket存在
8401
			if(file_exists($tmpUnixDomainSock)){
226 liveuser 8402
 
3 liveuser 8403
				#移除之
8404
				unlink($tmpUnixDomainSock);
226 liveuser 8405
 
3 liveuser 8406
				}#if end
226 liveuser 8407
 
3 liveuser 8408
			#設置執行錯誤識別
8409
			$result["status"]="false";
226 liveuser 8410
 
3 liveuser 8411
			#設置錯誤訊息
8412
			$result["error"]=$unixDomainSockClient;
226 liveuser 8413
 
3 liveuser 8414
			#回傳結果
8415
			return $result;
226 liveuser 8416
 
3 liveuser 8417
			}#if end
226 liveuser 8418
 
3 liveuser 8419
		#如果沒有產生新id
8420
		if(!isset($jsonRes->id)){
226 liveuser 8421
 
3 liveuser 8422
			#如果socket存在
8423
			if(file_exists($tmpUnixDomainSock)){
226 liveuser 8424
 
3 liveuser 8425
				#移除之
8426
				unlink($tmpUnixDomainSock);
226 liveuser 8427
 
3 liveuser 8428
				}#if end
226 liveuser 8429
 
3 liveuser 8430
			#設置執行錯誤識別
8431
			$result["status"]="false";
226 liveuser 8432
 
3 liveuser 8433
			#設置錯誤訊息
8434
			$result["error"]=$unixDomainSockClient;
226 liveuser 8435
 
3 liveuser 8436
			#回傳結果
8437
			return $result;
226 liveuser 8438
 
3 liveuser 8439
			}#if end
226 liveuser 8440
 
3 liveuser 8441
		#debug
8442
		#var_dump(__LINE__,"got id:".$jsonRes->id);
226 liveuser 8443
 
3 liveuser 8444
		#debug
8445
		#while(true){};
226 liveuser 8446
 
3 liveuser 8447
		#用新的id再傳送一次要求給 qbpwcf_usock_path
8448
		$paramsOfUnixDomainSockClient=array();
8449
		$paramsOfUnixDomainSockClient["sock"]=$tmpUnixDomainSock;
8450
		$paramsOfUnixDomainSockClient["id"]=$jsonRes->id;
8451
		$paramsOfUnixDomainSockClient["custom"]=$msg;
8452
		$unixDomainSockClient=sock::unixDomainSockClient($paramsOfUnixDomainSockClient);
8453
		unset($paramsOfUnixDomainSockClient);
226 liveuser 8454
 
3 liveuser 8455
		#debug
8456
		#var_dump(__LINE__,$unixDomainSockClient);
226 liveuser 8457
 
3 liveuser 8458
		#如果執行失敗
8459
		if($unixDomainSockClient["status"]==="false"){
226 liveuser 8460
 
3 liveuser 8461
			#如果socket存在
8462
			if(file_exists($tmpUnixDomainSock)){
226 liveuser 8463
 
3 liveuser 8464
				#移除之
8465
				unlink($tmpUnixDomainSock);
226 liveuser 8466
 
3 liveuser 8467
				}#if end
226 liveuser 8468
 
3 liveuser 8469
			#設置執行錯誤識別
8470
			$result["status"]="false";
226 liveuser 8471
 
3 liveuser 8472
			#設置錯誤訊息
8473
			$result["error"]=$unixDomainSockClient;
226 liveuser 8474
 
3 liveuser 8475
			#回傳結果
8476
			return $result;
226 liveuser 8477
 
3 liveuser 8478
			}#if end
226 liveuser 8479
 
3 liveuser 8480
		#debug
8481
		#var_dump(__LINE__,$unixDomainSockClient);
226 liveuser 8482
 
3 liveuser 8483
		#如果有回應的內容
8484
		if(!empty($unixDomainSockClient["content"])){
226 liveuser 8485
 
3 liveuser 8486
			#取得json回應
8487
			$jsonRes=(array)json_decode($unixDomainSockClient["content"]);
226 liveuser 8488
 
3 liveuser 8489
			#如果執行失敗
8490
			if($jsonRes===null){
226 liveuser 8491
 
3 liveuser 8492
				#如果socket存在
8493
				if(file_exists($tmpUnixDomainSock)){
226 liveuser 8494
 
3 liveuser 8495
					#移除之
8496
					unlink($tmpUnixDomainSock);
226 liveuser 8497
 
3 liveuser 8498
					}#if end
226 liveuser 8499
 
3 liveuser 8500
				#設置執行錯誤識別
8501
				$result["status"]="false";
226 liveuser 8502
 
3 liveuser 8503
				#設置錯誤訊息
8504
				$result["error"]=$unixDomainSockClient;
226 liveuser 8505
 
3 liveuser 8506
				#回傳結果
8507
				return $result;
226 liveuser 8508
 
3 liveuser 8509
				}#if end
226 liveuser 8510
 
3 liveuser 8511
			#如果執行指令失敗
8512
			if($jsonRes["status"]==="false"){
226 liveuser 8513
 
3 liveuser 8514
				#如果socket存在
8515
				if(file_exists($tmpUnixDomainSock)){
226 liveuser 8516
 
3 liveuser 8517
					#移除之
8518
					unlink($tmpUnixDomainSock);
226 liveuser 8519
 
3 liveuser 8520
					}#if end
226 liveuser 8521
 
3 liveuser 8522
				#設置執行錯誤識別
8523
				$result["status"]="false";
226 liveuser 8524
 
3 liveuser 8525
				#設置錯誤訊息
8526
				$result["error"]=$jsonRes["error"];
226 liveuser 8527
 
3 liveuser 8528
				#回傳結果
8529
				return $result;
226 liveuser 8530
 
3 liveuser 8531
				}#if end
226 liveuser 8532
 
3 liveuser 8533
			#印出client收到的結果
8534
			#var_dump($jsonRes);
226 liveuser 8535
 
3 liveuser 8536
			}#if end
226 liveuser 8537
 
3 liveuser 8538
		#debug
8539
		#var_dump(__LINE__,$procForUnixDoaminSocket);
226 liveuser 8540
 
3 liveuser 8541
		#初始化儲存標準輸出
8542
		$result["content"]["stdout"]="";
226 liveuser 8543
 
3 liveuser 8544
		#初始化儲存非標準輸出
8545
		$result["content"]["stderr"]="";
226 liveuser 8546
 
3 liveuser 8547
		#等待 unixDomainSockServer 結束執行
8548
		while(true){
226 liveuser 8549
 
3 liveuser 8550
			#取得程序狀態
8551
			$proc_get_status=proc_get_status($procForUnixDoaminSocket["content"][0]["process"]);
226 liveuser 8552
 
3 liveuser 8553
			/* debug
226 liveuser 8554
 
3 liveuser 8555
			#函式說明:
8556
			#撰寫log
8557
			#回傳結果:
8558
			#$result["status"],狀態,"true"或"false".
8559
			#$result["error"],錯誤訊息陣列.
8560
			#$result["function"],當前函式的名稱.
8561
			#$result["argu"],使用的參數.
8562
			#$result["content"],要寫入log的內容字串.
8563
			#必填參數:
8564
			#$conf["path"],字串,log檔案的路徑與名稱.
8565
			$conf["logs::record"]["path"]="/tmp/sock::runWithAutoAddMemoryDaemon.log";
8566
			#$conf["content"],any,要寫的內容,若內容不為字串則會用var_dump的格式寫入.
8567
			$conf["logs::record"]["content"]=$proc_get_status;
8568
			#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
8569
			$conf["logs::record"]["fileArgu"]=__FILE__;
8570
			#可省略參數:
8571
			#$conf["rewrite"],預設為"false",接續寫入;反之"true"代表重新寫入.
8572
			#$conf["rewrite"]="false";
8573
			#$conf["returnOnly"],預設為"false",會寫入到log檔案.若為"true"則不會寫入log.
8574
			#$conf["returnOnly"]="true";
8575
			#參考資料:
8576
			#無.
8577
			#備註:
8578
			#無.
8579
			$record=logs::record($conf["logs::record"]);
8580
			unset($conf["logs::record"]);
226 liveuser 8581
 
3 liveuser 8582
			#如果執行失敗
8583
			if($record["status"]==="false"){
226 liveuser 8584
 
3 liveuser 8585
				#設置執行錯誤識別
8586
				$result["status"]="false";
226 liveuser 8587
 
3 liveuser 8588
				#設置執行錯誤
8589
				$result["error"]=$record;
226 liveuser 8590
 
3 liveuser 8591
				#回傳結果
8592
				return $result;
226 liveuser 8593
 
3 liveuser 8594
				}#if end
226 liveuser 8595
 
3 liveuser 8596
			*/
226 liveuser 8597
 
3 liveuser 8598
			#確認process是否存在
8599
			#ps auxwf | grep sshd | grep -v 'grep' | wc -l
8600
			#函式說明:
8601
			#呼叫shell執行系統命令,並取得回傳的內容.
8602
			#回傳結果:
8603
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
8604
			#$result["error"],錯誤訊息陣列.
8605
			#$result["function"],當前執行的函數名稱.
8606
			#$result["argu"],使用的參數.
8607
			#$result["cmd"],執行的指令內容.
8608
			#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
8609
			#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
8610
			#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
8611
			#$result["running"],是否還在執行.
8612
			#$result["pid"],pid.
8613
			#$result["statusCode"],執行結束後的代碼.
8614
			#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
8615
			#必填參數:
8616
			#$conf["command"],字串,要執行的指令.
8617
			$conf["external::callShell"]["command"]="ps";
8618
			#$conf["fileArgu"],字串,變數__FILE__的內容.
8619
			$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];
8620
			#可省略參數:
8621
			#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
8622
			$conf["external::callShell"]["argu"]=array("auxwf","|","grep",$proc_get_status["pid"],"|","grep","-v","'grep'","|","wc","-l");
8623
			#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
8624
			#$conf["arguIsAddr"]=array();
8625
			#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
8626
			#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
8627
			#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
8628
			#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
8629
			#$conf["enablePrintDescription"]="true";
8630
			#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
8631
			#$conf["printDescription"]="";
8632
			#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
8633
			#$conf["escapeshellarg"]="false";
8634
			#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
8635
			#$conf["thereIsShellVar"]=array();
8636
			#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
8637
			#$conf["username"]="";
8638
			#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
8639
			#$conf["password"]="";
8640
			#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
8641
			#$conf["useScript"]="";
8642
			#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
8643
			#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
8644
			#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
8645
			#$conf["inBackGround"]="";
8646
			#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
8647
			#$conf["getErr"]="false";
8648
			#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
8649
			#$conf["doNotRun"]="false";
8650
			#參考資料:
8651
			#exec=>http://php.net/manual/en/function.exec.php
8652
			#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
8653
			#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
8654
			#備註:
8655
			#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
8656
			#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
8657
			$callShell=external::callShell($conf["external::callShell"]);
8658
			unset($conf["external::callShell"]);
226 liveuser 8659
 
3 liveuser 8660
			#如果執行失敗
8661
			if($callShell["status"]==="false"){
226 liveuser 8662
 
3 liveuser 8663
				#設置執行失敗
8664
				$result["status"]="false";
8665
 
8666
				#設置執行錯誤訊息
8667
				$result["error"]=$callShell;
8668
 
8669
				#回傳結果
8670
				return $result;
226 liveuser 8671
 
3 liveuser 8672
				}#if end
226 liveuser 8673
 
3 liveuser 8674
			#如果沒有輸出結果
8675
			if(!isset($callShell["output"][0])){
226 liveuser 8676
 
3 liveuser 8677
				#設置執行失敗
8678
				$result["status"]="false";
8679
 
8680
				#設置執行錯誤訊息
8681
				$result["error"]=$callShell;
8682
 
8683
				#回傳結果
8684
				return $result;
226 liveuser 8685
 
3 liveuser 8686
				}#if end
226 liveuser 8687
 
3 liveuser 8688
			#如果執行結束了
8689
			if( $proc_get_status["running"]===false || $callShell["output"][0]==="0" ){
226 liveuser 8690
 
3 liveuser 8691
				#取得程式執行過程產生的標準輸出
8692
				$result["content"]["stdout"]=$procForUnixDoaminSocket["content"][0]["content"];
226 liveuser 8693
 
3 liveuser 8694
				#如果是resource
8695
				if(gettype($result["content"]["stdout"])==="resource"){
226 liveuser 8696
 
3 liveuser 8697
					#讀取內容
8698
					$result["content"]["stdout"]=stream_get_contents($result["content"]["stdout"]);
226 liveuser 8699
 
3 liveuser 8700
					}#if end
226 liveuser 8701
 
3 liveuser 8702
				#取得程式執行過程產生的非標準輸出
8703
				$result["content"]["stderr"]=$procForUnixDoaminSocket["content"][0]["error"];
226 liveuser 8704
 
3 liveuser 8705
				#如果是resource
8706
				if(gettype($result["content"]["stderr"])==="resource"){
226 liveuser 8707
 
3 liveuser 8708
					#讀取內容
8709
					$result["content"]["stderr"]=stream_get_contents($result["content"]["stderr"]);
226 liveuser 8710
 
3 liveuser 8711
					}#if end
226 liveuser 8712
 
3 liveuser 8713
				#跳出 while
8714
				break;
226 liveuser 8715
 
3 liveuser 8716
				}#if end
226 liveuser 8717
 
3 liveuser 8718
			#休息1秒
8719
			sleep(1);
226 liveuser 8720
 
3 liveuser 8721
			}#while end
226 liveuser 8722
 
3 liveuser 8723
		#如果socket存在
8724
		if(file_exists($tmpUnixDomainSock)){
226 liveuser 8725
 
3 liveuser 8726
			#移除之
8727
			unlink($tmpUnixDomainSock);
226 liveuser 8728
 
3 liveuser 8729
			}#if end
226 liveuser 8730
 
3 liveuser 8731
		#設置執行正常
8732
		$result["status"]="true";
226 liveuser 8733
 
3 liveuser 8734
		#回傳結果
8735
		return $result;
226 liveuser 8736
 
3 liveuser 8737
		}#function runWithAutoAddMemoryDaemon end
226 liveuser 8738
 
3 liveuser 8739
	/*
8740
	#函式說明:
8741
	#用fwrite寫入到resource,可以指定每個寫入字串的結尾方式.
8742
	#回傳結果:
8743
	#$result["status"],執行是否正常,"true"為正常,"false"為不正常.
8744
	#$result["error"],錯誤訊息陣列.
8745
	#$result["function"],當前執行的函數名稱.
8746
	#$result["argu"],使用的參數.
8747
	#$result["is_writable"],參數stream是否為可寫入的resource,"true"代表可寫入;"false"代表不可寫入.
8748
	#必填參數:
8749
	#$conf["stream"],resource,寫入的目標.
8750
	$conf["stream"]="";
8751
	#$conf["data"],字串陣列,要寫入的內容,每個元素代表每段要寫入的內容.
8752
	$conf["data"]=array();
8753
	#可省略參數:
8754
	#$conf["msgEndType"],字串,訊息結尾要怎麼寫,預設為"none",代表不做處理;"EOL"代表結尾要用PHP_EOL;"HTTP/1.1"代表"data"參數最多為2個,分別為header跟body,結尾方式為兩個PHP_EOL.
8755
	#$conf["msgEndType"]="";
8756
	#$conf["autoClose"],字串,寫入結束後是否要關閉resource,預設為"false"代表保持開着;反之為"true".
8757
	#$conf["autoClose"]="false";
8758
	#參考資料:
8759
	#https://stackoverflow.com/questions/5294305/how-to-check-if-a-php-stream-resource-is-readable-or-writable#5294425
8760
	#https://www.php.net/manual/en/function.stream-get-meta-data.php
8761
	#https://www.php.net/manual/en/function.is-writable.php
8762
	#https://www.php.net/manual/en/function.socket-get-status.php
8763
	#https://www.php.net/manual/en/function.fopen.php
8764
	#備註:
8765
	#無.
8766
	*/
8767
	public static function fwrite(&$conf=array()){
226 liveuser 8768
 
3 liveuser 8769
		#初始化要回傳的結果
8770
		$result=array();
8771
 
8772
		#取得當前執行的函數名稱
8773
		$result["function"]=__FUNCTION__;
226 liveuser 8774
 
3 liveuser 8775
		#取得參數
8776
		$result["argu"]=$conf;
8777
 
8778
		#如果 $conf 不為陣列
8779
		if(gettype($conf)!="array"){
8780
 
8781
			#設置執行失敗
8782
			$result["status"]="false";
8783
 
8784
			#設置執行錯誤訊息
8785
			$result["error"][]="\$conf變數須為陣列形態";
8786
 
8787
			#如果傳入的參數為 null
8788
			if(is_null($conf)){
8789
 
8790
				#設置執行錯誤訊息
8791
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
8792
 
8793
				}#if end
8794
 
8795
			#回傳結果
8796
			return $result;
8797
 
8798
			}#if end
226 liveuser 8799
 
3 liveuser 8800
		#檢查參數
8801
		#函式說明:
8802
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
8803
		#回傳結果:
8804
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
8805
		#$result["error"],執行不正常結束的錯訊息陣列.
8806
		#$result["simpleError"],簡單表示的錯誤訊息.
8807
		#$result["function"],當前執行的函式名稱.
8808
		#$result["argu"],設置給予的參數.
8809
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
8810
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
8811
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
8812
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
8813
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
8814
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
8815
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
8816
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
8817
		#必填參數:
8818
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
8819
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
8820
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
8821
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
8822
		#可省略參數:
8823
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
8824
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("stream","data");
8825
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
8826
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("resource","array");
8827
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
8828
		#$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
8829
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
8830
		$conf["variableCheck::checkArguments"]["canNotBeEmpty"]=array("stream");
8831
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
8832
		#$conf["canBeEmpty"]=array();
8833
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
8834
		$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("msgEndType","autoClose");
8835
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
8836
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("msgEndType","autoClose");
8837
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
8838
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string");
8839
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
8840
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("none","false");
8841
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
8842
		#$conf["disallowAllSkipableVarIsEmpty"]="";
8843
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
8844
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
8845
		#$conf["disallowAllSkipableVarNotExist"],字串,是否不允許每個可省略參數都不存在,預設為"false"代表允許,反之為"true".
8846
		#$conf["disallowAllSkipableVarNotExist"]="";
8847
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
8848
		#$conf["arrayCountEqualCheck"][]=array();
8849
		#參考資料:
8850
		#array_keys=>http://php.net/manual/en/function.array-keys.php
8851
		#備註:
8852
		#無.
8853
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
8854
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 8855
 
3 liveuser 8856
		#如果執行失敗
8857
		if($checkArguments["status"]==="false"){
226 liveuser 8858
 
3 liveuser 8859
			#設置執行失敗
8860
			$result["status"]="false";
226 liveuser 8861
 
3 liveuser 8862
			#設置錯誤資訊
8863
			$result["error"]=$checkArguments;
226 liveuser 8864
 
3 liveuser 8865
			#回傳結果
8866
			return $result;
226 liveuser 8867
 
3 liveuser 8868
			}#if end
226 liveuser 8869
 
3 liveuser 8870
		#如果檢查不通過
8871
		if($checkArguments["passed"]==="false"){
226 liveuser 8872
 
3 liveuser 8873
			#設置執行失敗
8874
			$result["status"]="false";
226 liveuser 8875
 
3 liveuser 8876
			#設置錯誤資訊
8877
			$result["error"]=$checkArguments;
226 liveuser 8878
 
3 liveuser 8879
			#回傳結果
8880
			return $result;
226 liveuser 8881
 
3 liveuser 8882
			}#if end
226 liveuser 8883
 
3 liveuser 8884
		#debug
8885
		#var_dump(__LINE__,$conf["stream"]);
226 liveuser 8886
 
3 liveuser 8887
		/*
226 liveuser 8888
 
3 liveuser 8889
		#函式說明:
8890
		#處理各種錯誤與例外.
8891
		#回傳結果:
8892
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
8893
		#$result["passed"],檢查是否通過,"true"代表通過,"false"代表不通過.
8894
		#$result["argu"],使用的參數.
8895
		#$result["function"],當前執行的函數.
8896
		#$result["error"],涵式錯誤訊息陣列.
8897
		#必填參數:
8898
		#無.
8899
		#可省略參數:
8900
		#$conf["allowCodition"],陣列,滿足什麼條件的錯誤跟例外要繼續執行而不中斷.
8901
		$conf["variableCheck::setErrorHandler"]["allowCodition"]=array("stream_get_meta_data(): supplied resource is not a valid stream resource","[function] => stream_get_meta_data");
8902
		#$conf["debug"],字串,"true"代表遇到不允許繼續執行的例外時要輸出debug資訊;預設為"false";
8903
		$conf["variableCheck::setErrorHandler"]["debug"]="true";
8904
		#$conf["format"],字串,遇到不允許的錯誤時,要用什麼格式呈現,預設為"html",其他選擇有"cmd".
8905
		#$conf["format"]="html";
8906
		#參考資料:
8907
		#無.
8908
		#備註:
8909
		#無.
8910
		$setErrorHandler=variableCheck::setErrorHandler($conf["variableCheck::setErrorHandler"]);
8911
		unset($conf["variableCheck::setErrorHandler"]);
226 liveuser 8912
 
3 liveuser 8913
		#如果有回傳內容
8914
		if($setErrorHandler!==null){
226 liveuser 8915
 
3 liveuser 8916
			#設置執行失敗
8917
			$result["status"]="false";
226 liveuser 8918
 
3 liveuser 8919
			#設置錯誤資訊
8920
			$result["error"]=$setErrorHandler;
226 liveuser 8921
 
3 liveuser 8922
			#回傳結果
8923
			return $result;
226 liveuser 8924
 
3 liveuser 8925
			}#if end
226 liveuser 8926
 
3 liveuser 8927
		*/
226 liveuser 8928
 
3 liveuser 8929
		#取得 stream 狀態
8930
		$stream_get_meta_data=stream_get_meta_data($conf["stream"]);
226 liveuser 8931
 
3 liveuser 8932
		#debug
8933
		#var_dump(__LINE__,$stream_get_meta_data);
226 liveuser 8934
 
3 liveuser 8935
		#如果存在 uri
8936
		if(isset($stream_get_meta_data["uri"])){
226 liveuser 8937
 
3 liveuser 8938
			#取得能否寫入
8939
			$is_writable=is_writable($stream_get_meta_data["uri"]);
226 liveuser 8940
 
3 liveuser 8941
			}#if end
226 liveuser 8942
 
3 liveuser 8943
		#反之用 mode 來判斷
8944
		else if(isset($stream_get_meta_data["mode"])){
226 liveuser 8945
 
3 liveuser 8946
			#函式說明:
8947
			#檢查一個字串裡面是否有多個關鍵字
8948
			#回傳結果:
8949
			#$result["status"],"true"代表執行成功,"false"代表執行失敗.
8950
			#$result["error"],錯誤訊息.
8951
			#$result["function"],當前執行的函數名稱.
8952
			#$result["founded"],是否有找到的關鍵字,"true"代表有找到關鍵字;"false"代表沒有找到關鍵字.
8953
			#$result["foundedKeyWords"],找到的關鍵字.
8954
			#$result["foundedAll"],是否有找到全部的關鍵字,"true"代表有;"false"代表沒有.
8955
			#$result["argu"],使用的參數.
8956
			#必填參數:
8957
			#$conf["keyWords"],字串陣列,想要搜尋的關鍵字.
8958
			$conf["search::findManyKeyWords"]["keyWords"]=array("r+","w","a","x","c");
8959
			#$conf["string"],字串,要被搜尋的字串內容.
8960
			$conf["search::findManyKeyWords"]["string"]=$stream_get_meta_data["mode"];
8961
			#可省略參數:
8962
			#$conf["completeEqual"],字串,是否內容要完全符合,不能多出任何不符合的內容,預設為不需要完全符合.
8963
			#$conf["completeEqual"]="true";
8964
			#參考資料:
8965
			#無.
8966
			#備註:
8967
			#無.
8968
			$findManyKeyWords=search::findManyKeyWords($conf["search::findManyKeyWords"]);
8969
			unset($conf["search::findManyKeyWords"]);
226 liveuser 8970
 
3 liveuser 8971
			#如果執行失敗
8972
			if($findManyKeyWords["status"]==="false"){
226 liveuser 8973
 
3 liveuser 8974
				#設置執行失敗
8975
				$result["status"]="false";
226 liveuser 8976
 
3 liveuser 8977
				#設置錯誤資訊
8978
				$result["error"]=$findManyKeyWords;
226 liveuser 8979
 
3 liveuser 8980
				#回傳結果
8981
				return $result;
226 liveuser 8982
 
3 liveuser 8983
				}#if end
226 liveuser 8984
 
3 liveuser 8985
			#如果模式含有寫入的屬性
8986
			if($findManyKeyWords["founded"]==="true"){
226 liveuser 8987
 
3 liveuser 8988
				#設置可以寫入
8989
				$is_writable=true;
226 liveuser 8990
 
3 liveuser 8991
				}#if end
226 liveuser 8992
 
8993
			}#else
8994
 
3 liveuser 8995
		#反之需要其他判斷方式
8996
		else{
226 liveuser 8997
 
3 liveuser 8998
			#設置警示訊息
8999
			$result["warning"][]="無法判別 resource 能否寫入";
226 liveuser 9000
 
3 liveuser 9001
			#設置警示訊息
9002
			$result["warning"][]=$stream_get_meta_data;
226 liveuser 9003
 
3 liveuser 9004
			#直接放行
9005
			$is_writable=true;
226 liveuser 9006
 
3 liveuser 9007
			}#else end
226 liveuser 9008
 
3 liveuser 9009
		#如果不可寫入
9010
		if($is_writable!==true){
226 liveuser 9011
 
3 liveuser 9012
			#設置執行失敗
9013
			$result["status"]="false";
226 liveuser 9014
 
3 liveuser 9015
			#設置錯誤資訊
9016
			$result["error"][]="resource 無法寫入";
226 liveuser 9017
 
3 liveuser 9018
			#設置錯誤資訊
9019
			$result["error"][]=$stream_get_meta_data;
226 liveuser 9020
 
3 liveuser 9021
			#設置resource不可寫入
9022
			$result["is_writable"]="false";
226 liveuser 9023
 
3 liveuser 9024
			#回傳結果
9025
			return $result;
226 liveuser 9026
 
3 liveuser 9027
			}#if end
226 liveuser 9028
 
3 liveuser 9029
		#設置resource可寫入
9030
		$result["is_writable"]="true";
226 liveuser 9031
 
3 liveuser 9032
		#判斷訊息結尾的格式
9033
		switch($conf["msgEndType"]){
226 liveuser 9034
 
3 liveuser 9035
			#如果是 "none"
9036
			case "none":
226 liveuser 9037
 
3 liveuser 9038
				#要寫入的每個字串
9039
				foreach($conf["data"] as $msg){
226 liveuser 9040
 
3 liveuser 9041
					#寫入內容
9042
					fwrite($conf["stream"],$msg);
226 liveuser 9043
 
3 liveuser 9044
					}#foreach end
226 liveuser 9045
 
3 liveuser 9046
				break;
226 liveuser 9047
 
3 liveuser 9048
			#如果是 "EOL"
9049
			case "EOL":
226 liveuser 9050
 
3 liveuser 9051
				#要寫入的每個字串
9052
				foreach($conf["data"] as $msg){
226 liveuser 9053
 
3 liveuser 9054
					#寫入內容,用PHP_EOL結尾.
9055
					fwrite($conf["stream"],$msg.PHP_EOL);
226 liveuser 9056
 
3 liveuser 9057
					}#foreach end
226 liveuser 9058
 
3 liveuser 9059
				break;
226 liveuser 9060
 
3 liveuser 9061
			#如果是 "HTTP/1.1"
9062
			case "HTTP/1.1":
226 liveuser 9063
 
3 liveuser 9064
				#如果要寫入的字串大於2段
9065
				if(count($conf["data"])>2){
226 liveuser 9066
 
3 liveuser 9067
					#設置執行失敗
9068
					$result["status"]="false";
226 liveuser 9069
 
3 liveuser 9070
					#設置錯誤資訊
9071
					$result["error"][]="當參數 msgEndType 為 ".$conf["msgEndType"]." 時,參數 data 的數量不得超過2個.";
226 liveuser 9072
 
3 liveuser 9073
					#設置resource不可寫入
9074
					$result["is_writable"]="false";
226 liveuser 9075
 
3 liveuser 9076
					#回傳結果
9077
					return $result;
226 liveuser 9078
 
3 liveuser 9079
					}#if end
226 liveuser 9080
 
3 liveuser 9081
				#要寫入的每個字串
9082
				foreach($conf["data"] as $msg){
226 liveuser 9083
 
3 liveuser 9084
					#寫入 header/body
9085
					fwrite($conf["stream"],$msg.PHP_EOL.PHP_EOL);
226 liveuser 9086
 
3 liveuser 9087
					}#foreach end
226 liveuser 9088
 
3 liveuser 9089
				#跳出switch
9090
				break;
226 liveuser 9091
 
3 liveuser 9092
			#非預期的類型
9093
			default:
226 liveuser 9094
 
3 liveuser 9095
				#設置執行失敗
9096
				$result["status"]="false";
226 liveuser 9097
 
3 liveuser 9098
				#設置錯誤資訊
9099
				$result["error"][]="unsupported msgEndType ".$conf["msgEndType"];
226 liveuser 9100
 
3 liveuser 9101
				#回傳結果
9102
				return $result;
226 liveuser 9103
 
3 liveuser 9104
			}#switch end
226 liveuser 9105
 
3 liveuser 9106
		#若要關閉resource
9107
		if($conf["autoClose"]==="true"){
226 liveuser 9108
 
3 liveuser 9109
			#關閉resouce.
9110
			fclose($conf["stream"]);
226 liveuser 9111
 
3 liveuser 9112
			}#if end
226 liveuser 9113
 
3 liveuser 9114
		#設置執行正常
9115
		$result["status"]="true";
226 liveuser 9116
 
3 liveuser 9117
		#回傳結果
9118
		return $result;
226 liveuser 9119
 
3 liveuser 9120
		}#function fwrite end
226 liveuser 9121
 
3 liveuser 9122
	}#class sock end
9123
 
9124
?>