Subversion Repositories php-qbpwcf

Rev

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

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