Subversion Repositories qbpwcf-lib(archive)

Rev

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

Rev Author Line No. Line
669 liveuser 1
#!/bin/php
2
<?php
3
 
4
/*
5
	QBPWCF, Quick Build PHP website Component base on Fedora Linux.
6
    Copyright (C) 2015~2024 Min-Jhin,Chen
7
 
8
    This file is part of QBPWCF.
9
 
10
    QBPWCF is free software: you can redistribute it and/or modify
11
    it under the terms of the GNU General Public License as published by
12
    the Free Software Foundation, either version 3 of the License, or
13
    (at your option) any later version.
14
 
15
    QBPWCF is distributed in the hope that it will be useful,
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
    GNU General Public License for more details.
19
 
20
    You should have received a copy of the GNU General Public License
21
    along with QBPWCF.  If not, see <http://www.gnu.org/licenses/>.
22
 
23
*/
24
 
25
/*
26
說明:
27
用 \qbpwcf\sock::unixDomainSockServer 建立 unix doamin socket server
28
 
29
範例:
30
...
31
*/
32
 
33
#使用命名空間qbpwcf
34
namespace qbpwcf;
35
 
36
#以該檔案的實際位置的 lib path 為 include path 首位
37
exec("cd ".pathinfo(__FILE__)["dirname"]."/../../;pwd;",$output,$status);
38
set_include_path($output[0].PATH_SEPARATOR.get_include_path());
39
 
40
#匯入外部套件
41
include("allInOne.php");
42
 
43
#說明函式
44
function help(){
45
 
46
	#印出指令說明
47
	echo "Usage of ".basename(__FILE__).":".PHP_EOL; 
48
	echo "--sock unix domain socket 的位置與名稱.".PHP_EOL;
49
	echo "--addOnProcessFunc namespace\class::function 收到client訊息時要執行的函式.".PHP_EOL;
50
	echo "--funcToRunWhenIdle namespace\class::function 沒收到訊息時要執行的函式.".PHP_EOL;
51
	echo "--onlyOutputCache \"true\"代表輸出 sock::unixDomainSockServer 回傳的 serverCache 結果".PHP_EOL;
52
	echo "--cacheLayer 代表要輸出 sock::unixDomainSockServer 回傳的 serverCache 變數底下的陣列索引名稱,有幾層就使用幾次該參數".PHP_EOL;
53
	echo "--cacheLayerJson 代表要輸出 sock::unixDomainSockServer 回傳的 serverCache 變數底下的陣列索引名稱,數值為json_encode後urlencode後base64_encode後的字串".PHP_EOL;
54
 
55
	#結束執行
56
	exit;
57
 
58
	}#function help end
59
 
60
#函式說明:
61
#解析參數.
62
#回傳結果:
63
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
64
#$reuslt["error"],執行不正常結束的錯訊息陣列.
65
#$result["function"],當前執行的函式名稱.
66
#$result["content"],解析好的參數陣列.
67
#$result["content"][$key][$i],參數 $key 的 $i+1 個參數數值內容.
68
#$result["program"],字串,執行的程式名稱.
69
#必填參數:
70
#無
71
#可省略參數:
72
#$conf["helpFunc"],如果解析的參數不成對,則要執行的函式名稱.
73
$conf["helpFunc"]="help";
74
#備註:
75
#僅能在命令列底下執行.
76
#建議:
77
#以後可將參數 --a--b 的名稱與後面的數值 $value 存成 $result["a"]["b"][$i]=$value .
78
$parseArgu=cmd::parseArgu($conf);
79
unset($conf);
80
 
81
#如果解析參數失敗
82
if($parseArgu["status"]==="false")
83
{
84
	#印出結果
85
	var_dump($parseArgu);
86
 
87
	#結束執行
88
	exit(1);
89
 
90
}#if end
91
 
744 liveuser 92
#預設的 unix domain socket
669 liveuser 93
$defaultUnixDomainSocket="./".basename(__FILE__).".sock";
94
 
95
#檢查參數
96
#函式說明:
97
#檢查必填與可省略參數,可省略參數可指定預設要給與什麼數值內容。
98
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
99
#$reuslt["error"],執行不正常結束的錯訊息陣列.
100
#$result["function"],當前執行的函式名稱.
101
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
102
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
103
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
104
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
105
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
106
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
107
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
108
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
109
#必填寫的參數:
110
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
111
$conf["varInput"]=&$parseArgu["content"];
112
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
113
$conf["referenceVarKey"]="variableCheck::checkArguments";
114
#可以省略的參數:
115
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
116
#$conf["mustBeFilledVariableName"]=array("sock","addOnProcessFunc","funcToRunWhenIdle");
117
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
118
#$conf["mustBeFilledVariableType"]=array("array","array","array");
119
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
120
#$conf["canBeEmptyString"]="false";
121
#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
122
#$conf["canNotBeEmpty"]=array();
123
#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
124
#$conf["canBeEmpty"]=array();
125
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
126
$conf["skipableVariableCanNotBeEmpty"]=array("sock","addOnProcessFunc","funcToRunWhenIdle","onlyOutputCache","cacheLayer");
127
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
128
$conf["skipableVariableName"]=array("sock","addOnProcessFunc","funcToRunWhenIdle","onlyOutputCache","cacheLayer");
129
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
130
$conf["skipableVariableType"]=array("array","array","array","array","array");
131
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
132
$conf["skipableVarDefaultValue"]=array(array($defaultUnixDomainSocket),null,null,"false",null);
133
#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
134
#$conf["variableCheck::checkArguments"]["disallowAllSkipableVarIsEmpty"]="false";
135
#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
136
#$conf["disallowAllSkipableVarIsEmptyArray"]="";
137
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
138
#$conf["arrayCountEqualCheck"][]=array();
139
#參考資料來源:
140
#array_keys=>http://php.net/manual/en/function.array-keys.php
141
$checkArguments=variableCheck::checkArguments($conf);
142
unset($conf);
143
 
144
#若執行失敗
145
if($checkArguments["status"]==="false"){
146
 
147
	#印出結果
148
	var_dump($checkArguments);
149
 
150
	#結束執行
151
	exit(1);
152
 
153
	}#if end
154
 
155
#若檢查不通過
156
if($checkArguments["passed"]==="false"){
157
 
158
	#印出結果
159
	var_dump($checkArguments);
160
 
161
	#結束執行
162
	exit(1);
163
 
164
	}#if end
165
 
166
#開始將輸出存入buffer
167
ob_start();
168
 
169
#函式說明:
170
#建立 unix domain socket server, 僅提供具備檔案存取權限的用戶使用,預設提供可以下達任何指令的功能.
171
#回傳結果:
172
#$result["status"],"true"代表執行正常;"false"代表執行不正常.
173
#$result["error"],錯誤訊息陣列.
174
#$result["function"],當前執行的函式名稱.
175
#$result["serverCache"],函式結束前,儲存在serverCache的內容,若有元素"exit"存在則代表是正常結束.
176
#必填參數:
177
#$conf["sock"],字串,socket檔案要放在哪邊,名稱為何.
178
$conf["sock"]=$parseArgu["content"]["sock"][0];
179
#$conf["fileArgu"],字串,變數__FILE__的內容.
180
$conf["fileArgu"]=__FILE__;
181
#可省略參數:
182
#$conf["changeOwner"],字串,要將socket檔案的擁有着權限進行修改."user.group"代表擁有者帳號為user,群組為group.
183
#$conf["changeOwner"]="";
184
#$conf["changePermission"],字串,要將socket檔案的權限設為多少.ex: "0666"(所有帳戶都有存取的權限) 或 "0660"(僅有擁有者與群組帳戶有存取的權限) 或 "0600"(只有擁有者有權限執行).
185
#$conf["changePermission"]="";
186
#$conf["sessionTimeout"],字串,當連線結束後於下一次連線間隔多久就算session timeout,server端會將記錄移除,client端需要重新拿取id,預設為300秒.
187
#$conf["sessionTimeout"]="300";
188
 
189
#如果有設置 addOnProcessFunc
190
if(isset($parseArgu["content"]["addOnProcessFunc"])){
191
 
192
	#$conf["addOnProcessFunc"],字串陣列,增加用於處理 json request 的函式名稱,給予的參數為array("request"=>收到的json訊息,"sock"=>用戶的socket),回傳的內容必須為陣列,例如 $res["continue"]="true"代表要繼續執行下一個addOnProcessFunc;"false"代表代表到此為止. $res["content"]="replaced content";代表要將收到的訊息取代成"replaced content". 最少要有回傳 $res["status"]數值"true"代表執行正常;"false"代表執行不正常.
193
	$conf["addOnProcessFunc"]=$parseArgu["content"]["addOnProcessFunc"];
194
 
195
	}#if end
196
 
197
if(isset($parseArgu["content"]["funcToRunWhenIdle"])){
198
 
199
	#$conf["funcToRunWhenIdle"],字串陣列,當沒有事件產生時,要執行的函式名稱,給予參數為array("client"=>所有用戶,"clientCache"=>$result["serverCache"]["clientSide"]["addOnProcessFunc"][$funcToRunWhenIdle],"serverCache"=>$result["serverCache"]["serverSide"]["funcToRunWhenIdle"][$funcToRunWhenIdle]).
200
	$conf["funcToRunWhenIdle"]=$parseArgu["content"]["funcToRunWhenIdle"];
201
 
202
	}#if end
203
 
204
#$conf["paramsForFuncToRunWhenIdle"],2維陣列,每個元素代表指定給予funcToRunWhenIdle參數中的指定元素的參數.
205
#$conf["paramsForFuncToRunWhenIdle"]=array();
206
#$conf["infoToFunction"],陣列,需要增加給addOnProcessFunc跟funcToRunWhenIdle函式的資訊,在函式中其參數的info鍵值.
207
#$conf["infoToFunction"]=array("debug"=>$debug);
208
#參考資料:
209
#http://php.net/manual/en/function.stream-socket-server.php
210
#備註:
211
#無.
212
$unixDomainSockServer=sock::unixDomainSockServer($conf);
213
unset($conf);
214
 
215
#debug,印出 cache 的內容,清空buffer
216
echo ob_get_contents();ob_clean();
217
 
218
#如果程式
219
if($unixDomainSockServer["status"]==="false"){
220
 
221
	#印出 cache 的內容
222
	echo ob_get_contents();
223
 
224
	#清空buffer,結束buffer的使用
225
	ob_end_clean();
226
 
227
	#印出結果
228
	var_dump($unixDomainSockServer);
229
 
230
	#結束執行
231
	exit(1);
232
 
233
	}#if end
234
 
235
#如果有 onlyOutputCache 參數
236
if($parseArgu["content"]["onlyOutputCache"][0]==="true"){
237
 
238
	#debug
239
	#var_dump(__LINE__,$parseArgu["content"]);
240
 
241
	#如果不存在 serverCache
242
	if(!isset($unixDomainSockServer["serverCache"])){
243
 
244
		#印出 cache 的內容
245
		echo ob_get_contents();
246
 
247
		#清空buffer,結束buffer的使用
248
		ob_end_clean();
249
 
250
		#印出結果
251
		var_dump($unixDomainSockServer);
252
 
253
		#結束執行並回傳1
254
		exit(1);
255
 
256
		}#if end
257
 
258
	#如果無設置 cacheLayer
259
	if(!(isset($parseArgu["content"]["cacheLayer"]))){
260
 
261
		#印出json結果
262
		echo json_encode($unixDomainSockServer["serverCache"]);
263
 
264
		#結束執行並回傳0
265
		exit;
266
 
267
		}#if end
268
 
269
	#反之
270
	else{
271
 
272
		#儲存當前的cache參考
273
		$currentCache=&$unixDomainSockServer["serverCache"];
274
 
275
		#debug,印出 cache 的內容,清空buffer,結束buffer的使用.
276
		#echo ob_get_contents();ob_end_clean();exit;
277
 
278
		#針對每個 cacheLayer
279
		foreach($parseArgu["content"]["cacheLayer"] as $cacheLayer){
280
 
281
			#如果存在該階層
282
			if(isset($currentCache[$cacheLayer])){
283
 
284
				#取得當前階層的參考
285
				$currentCache=&$currentCache[$cacheLayer];
286
 
287
				}#if end
288
 
289
			}#foreach end
290
 
291
		#清空buffer,結束buffer的使用
292
		ob_end_clean();
293
 
294
		#印出指定的cache內容
295
		echo json_encode($currentCache);
296
 
297
		#結束執行並回傳0
298
		exit;
299
 
300
		}#else end
301
 
302
	}#if end
303
 
304
#印出 cache 的內容
305
echo ob_get_contents();
306
 
307
#清空buffer,結束buffer的使用
308
ob_end_clean();
309
 
310
#debug
311
#var_dump(__LINE__,$parseArgu);exit;
312
 
313
#印出json結果
314
echo json_encode($unixDomainSockServer);
315