Subversion Repositories qbpwcf-lib(archive)

Rev

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

Rev Author Line No. Line
501 liveuser 1
#!/bin/php
2
<?php
3
 
4
/*
5
	QBPWCF, Quick Build PHP website Component base on Fedora Linux.
622 liveuser 6
    Copyright (C) 2015~2024 Min-Jhin,Chen
501 liveuser 7
 
8
    This file is part of QBPWCF.
9
 
10
    QBPWCF is free software: you can redistribute it and/or modify
11
    it under the terms of the GNU General Public License as published by
12
    the Free Software Foundation, either version 3 of the License, or
13
    (at your option) any later version.
14
 
15
    QBPWCF is distributed in the hope that it will be useful,
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
    GNU General Public License for more details.
19
 
20
    You should have received a copy of the GNU General Public License
21
    along with QBPWCF.  If not, see <http://www.gnu.org/licenses/>.
22
 
23
*/
24
 
25
/*
26
 
27
說明:
28
提供QBPWCF套件執行任何指令的unix domain socket服務的用戶端.
29
 
30
*/
31
 
32
#使用命名空間qbpwcf
33
namespace qbpwcf;
34
 
579 liveuser 35
#取得該檔案的實際位置的 lib path
501 liveuser 36
exec("cd ".pathinfo(__FILE__)["dirname"]."/../../;pwd;",$output,$status);
37
 
579 liveuser 38
#保存 lib path
39
$libPath=$output[0];
40
 
41
#以該檔案的實際位置的 lib path 為 include path 首位
42
set_include_path($libPath.PATH_SEPARATOR.get_include_path());
43
 
501 liveuser 44
#匯入外部套件
45
include("allInOne.php");
46
 
579 liveuser 47
#建議的log位置
598 liveuser 48
$logFile=$libPath."/log".$_SERVER["PHP_SELF"].".log";
579 liveuser 49
 
501 liveuser 50
#說明函式
579 liveuser 51
function help(){
52
 
501 liveuser 53
	#印出指令說明
54
	echo "Usage of ".basename(__FILE__).":".PHP_EOL; 
503 liveuser 55
	echo "--unixDomainSocketPath 代表要連接的 unix doamin socket 路徑.預設為 ".qbpwcf_usock_path.PHP_EOL;
56
	echo "--cmd 代表要執行的指令".PHP_EOL;
501 liveuser 57
	echo "--param 代表要使用的參數".PHP_EOL;
579 liveuser 58
	echo "--debug true 代表要啟動 debug 模式".PHP_EOL;
501 liveuser 59
 
60
	#結束執行
61
	exit;
62
 
583 liveuser 63
	}#function help end
579 liveuser 64
 
501 liveuser 65
#函式說明:
66
#解析參數.
67
#回傳結果:
68
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
69
#$reuslt["error"],執行不正常結束的錯訊息陣列.
70
#$result["function"],當前執行的函式名稱.
71
#$result["content"],解析好的參數陣列.
72
#$result["content"][$key][$i],參數 $key 的 $i+1 個參數數值內容.
73
#$result["program"],字串,執行的程式名稱.
74
#必填參數:
75
#無
76
#可省略參數:
77
#$conf["helpFunc"],如果解析的參數不成對,則要執行的函式名稱.
78
$conf["helpFunc"]="help";
79
#備註:
80
#僅能在命令列底下執行.
81
#建議:
82
#以後可將參數 --a--b 的名稱與後面的數值 $value 存成 $result["a"]["b"][$i]=$value .
83
$parseArgu=cmd::parseArgu($conf);
84
unset($conf);
85
 
86
#如果解析參數失敗
579 liveuser 87
if($parseArgu["status"]==="false"){
88
 
501 liveuser 89
	#印出結果
90
	var_dump($parseArgu);
579 liveuser 91
 
92
	#結束執行,回傳錯誤代碼1
93
	exit(1);
501 liveuser 94
 
579 liveuser 95
	}#if end
501 liveuser 96
 
97
#break point
98
#var_dump($parseArgu);exit;
99
 
100
#break point
101
#var_dump(qbpwcf_usock_path);exit;
102
 
103
#檢查參數
104
#函式說明:
105
#檢查必填與可省略參數,可省略參數可指定預設要給與什麼數值內容。
106
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
107
#$reuslt["error"],執行不正常結束的錯訊息陣列.
108
#$result["function"],當前執行的函式名稱.
109
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
110
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
111
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
112
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
113
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
114
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
115
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
116
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
117
#必填寫的參數:
118
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
119
$conf["varInput"]=&$parseArgu["content"];
120
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
121
$conf["referenceVarKey"]="variableCheck::checkArguments";
122
#可以省略的參數:
123
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
124
$conf["mustBeFilledVariableName"]=array("cmd");
125
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
126
$conf["mustBeFilledVariableType"]=array("array");
127
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
128
$conf["canBeEmptyString"]="false";
129
#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
130
#$conf["canNotBeEmpty"]=array();
131
#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
132
#$conf["canBeEmpty"]=array();
133
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
134
#$conf["skipableVariableCanNotBeEmpty"]=array("backTime");
135
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
579 liveuser 136
$conf["skipableVariableName"]=array("unixDomainSocketPath","param","debug");
501 liveuser 137
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
579 liveuser 138
$conf["skipableVariableType"]=array("array","array","array");
501 liveuser 139
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
579 liveuser 140
$conf["skipableVarDefaultValue"]=array(array(qbpwcf_usock_path),null,array("false"));
501 liveuser 141
#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
142
#$conf["variableCheck::checkArguments"]["disallowAllSkipableVarIsEmpty"]="false";
143
#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
144
#$conf["disallowAllSkipableVarIsEmptyArray"]="";
145
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
146
#$conf["arrayCountEqualCheck"][]=array();
147
#參考資料來源:
148
#array_keys=>http://php.net/manual/en/function.array-keys.php
149
$checkArguments=variableCheck::checkArguments($conf);
150
unset($conf);
151
 
152
#若執行失敗
153
if($checkArguments["status"]==="false"){
154
 
155
	#印出結果
156
	var_dump($checkArguments);
157
 
579 liveuser 158
	#結束執行,回傳錯誤代碼1.
159
	exit(1);
501 liveuser 160
 
161
	}#if end
162
 
163
#若檢查不通過
164
if($checkArguments["passed"]==="false"){
165
 
166
	#印出結果
167
	var_dump($checkArguments);
168
 
579 liveuser 169
	#結束執行,回傳錯誤代碼1.
170
	exit(1);
501 liveuser 171
 
172
	}#if end
173
 
579 liveuser 174
#預設不debug
175
$debug=false;
176
 
177
#如果設置要debug
178
if($parseArgu["content"]["debug"][0]==="true")
179
{
180
	#設置要debug
181
	$debug=true;
583 liveuser 182
 
183
	#debug
590 liveuser 184
	system("echo ".$logFile." > /tmp/output.log");
583 liveuser 185
 
579 liveuser 186
}
187
 
501 liveuser 188
#取得要連接的 unix domain socket path
189
$unixDomainSocketPath=$parseArgu["content"]["unixDomainSocketPath"][0];
190
 
191
# 取得 id - start
192
 
193
#函式說明:
194
#連線到 unixDomainSockServer 提供的 unix domain socket.
195
#回傳結果:
196
#$result["status"],"true"代表執行正常;"false"代表執行不正常.
197
#$result["error"],錯誤訊息陣列.
198
#$result["function"],當前執行的函式名稱.
199
#$result["content"],取得的回應.
200
#必填參數:
201
#$conf["sock"],字串,要連線的unix domain socket.
202
$conf["sock"]=$unixDomainSocketPath;
203
#可省略參數:
204
#$conf["id"],字串,取得的id,若無此值,則會得到新的數值.
205
#$conf["id"]="";
206
#$conf["cmd"],字串,要執行的指令,當$conf["id"]參數合法時,才會執行.
207
#$conf["cmd"]="";
208
#$conf["param"],參數陣列.
209
#$conf["param"]=array();
210
#$conf["escaped"],字串,param參數是否已經escaped了,預設為"false",反之為"true".
211
#$conf["escaped"]="true";
212
#$conf["custom"],陣列,要客制化傳輸的內容,會覆蓋以上可省略參數.
213
#$conf["custom"]=array();
214
#參考資料:
215
#http://php.net/manual/en/function.stream-socket-client.php
216
#http://php.net/manual/en/function.stream-get-contents.php
217
#備註:
218
#要檢查 unix socket 檔案 $conf["sock"] 是否存在.
219
$unixDomainSockClient=sock::unixDomainSockClient($conf);
220
unset($conf);
221
 
222
#若執行失敗
223
if($unixDomainSockClient["status"]==="false"){
224
 
225
	#印出結果
226
	var_dump($unixDomainSockClient);
227
 
579 liveuser 228
	#如果要debug
229
	if($debug){
230
 
231
		#函式說明:
232
		#撰寫log
233
		#回傳結果:
234
		#$result["status"],狀態,"true"或"false".
235
		#$result["error"],錯誤訊息陣列.
236
		#$result["function"],當前函式的名稱.
237
		#$result["argu"],使用的參數.
238
		#必填參數:
239
		#$conf["path"],字串,log檔案的路徑與名稱.
240
		$conf["path"]=$logFile;
241
		#$conf["content"],any,要寫的內容,若內容不為字串則會用var_dump的格式寫入.
242
		$conf["content"]=$unixDomainSockClient;
243
		#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
244
		$conf["fileArgu"]=__FILE__;
245
		#可省略參數:
246
		#$conf["rewrite"],預設為"false",接續寫入;反之"true"代表重新寫入.
247
		#$conf["rewrite"]="false";
248
		#參考資料:
249
		#無.
250
		#備註:
251
		#無.
252
		$record=logs::record($conf);
253
		unset($conf);
254
 
255
		#如果寫log失敗
256
		if($record["status"]==="false"){
257
 
258
			#印出結果
259
			var_dump($record);
260
 
261
			}#if end
262
 
263
		}#if end
264
 
265
	#結束執行,回傳錯誤代碼1.
266
	exit(1);
501 liveuser 267
 
268
	}#if end
269
 
270
#解析結果
271
$res=(array)(json_decode($unixDomainSockClient["content"]));
272
 
273
#如果取得id失敗
579 liveuser 274
if($res["status"]==="false"){
501 liveuser 275
 
276
	#印出結果
277
	var_dump($res);
278
 
579 liveuser 279
	#如果要debug
280
	if($debug){
281
 
282
		#函式說明:
283
		#撰寫log
284
		#回傳結果:
285
		#$result["status"],狀態,"true"或"false".
286
		#$result["error"],錯誤訊息陣列.
287
		#$result["function"],當前函式的名稱.
288
		#$result["argu"],使用的參數.
289
		#必填參數:
290
		#$conf["path"],字串,log檔案的路徑與名稱.
291
		$conf["path"]=$logFile;
292
		#$conf["content"],any,要寫的內容,若內容不為字串則會用var_dump的格式寫入.
293
		$conf["content"]=$res;
294
		#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
295
		$conf["fileArgu"]=__FILE__;
296
		#可省略參數:
297
		#$conf["rewrite"],預設為"false",接續寫入;反之"true"代表重新寫入.
298
		#$conf["rewrite"]="false";
299
		#參考資料:
300
		#無.
301
		#備註:
302
		#無.
303
		$record=logs::record($conf);
304
		unset($conf);
305
 
306
		#如果寫log失敗
307
		if($record["status"]==="false"){
308
 
309
			#印出結果
310
			var_dump($record);
311
 
312
			}#if end
313
 
314
		}#if end
315
 
316
	#結束執行,回傳錯誤代碼1.
317
	exit(1);
501 liveuser 318
 
579 liveuser 319
	}#if end
501 liveuser 320
 
321
#取得 id
322
$id=$res["id"];
323
 
324
# 取得 id - end
325
 
326
#取得要傳送的訊息內容
327
$cmd=$parseArgu["content"]["cmd"][0];
328
 
329
#預設無參數
330
$params=array();
331
 
511 liveuser 332
#如果有 param 參數
579 liveuser 333
if(isset($parseArgu["content"]["param"])){
501 liveuser 334
 
511 liveuser 335
	#針對每個參數
579 liveuser 336
	for($i=0;$i<count($parseArgu["content"]["param"]);$i++){
501 liveuser 337
 
511 liveuser 338
		#儲存參數
339
		$params[]=$parseArgu["content"]["param"][$i];
501 liveuser 340
 
579 liveuser 341
		}#for end
511 liveuser 342
 
579 liveuser 343
	}#if end
511 liveuser 344
 
501 liveuser 345
#函式說明:
346
#連線到 unixDomainSockServer 提供的 unix domain socket.
347
#回傳結果:
348
#$result["status"],"true"代表執行正常;"false"代表執行不正常.
349
#$result["error"],錯誤訊息陣列.
350
#$result["function"],當前執行的函式名稱.
351
#$result["content"],取得的回應.
352
#必填參數:
353
#$conf["sock"],字串,要連線的unix domain socket.
354
$conf["sock"]=$unixDomainSocketPath;
355
#可省略參數:
356
#$conf["id"],字串,取得的id,若無此值,則會得到新的數值.
357
$conf["id"]=$id;
358
#$conf["cmd"],字串,要執行的指令,當$conf["id"]參數合法時,才會執行.
359
$conf["cmd"]=$cmd;
360
 
361
#如果有參數
579 liveuser 362
if(count($params)>0){
501 liveuser 363
 
364
	#$conf["param"],參數陣列.
365
	$conf["param"]=$params;
366
 
579 liveuser 367
	}#if end
501 liveuser 368
 
369
#$conf["escaped"],字串,param參數是否已經escaped了,預設為"false",反之為"true".
370
$conf["escaped"]="true";
371
#$conf["custom"],陣列,要客制化傳輸的內容,會覆蓋以上可省略參數.
372
#$conf["custom"]=array();
373
#參考資料:
374
#http://php.net/manual/en/function.stream-socket-client.php
375
#http://php.net/manual/en/function.stream-get-contents.php
376
#備註:
377
#要檢查 unix socket 檔案 $conf["sock"] 是否存在.
378
$unixDomainSockClient=sock::unixDomainSockClient($conf);
379
unset($conf);
380
 
381
#若執行失敗
382
if($unixDomainSockClient["status"]==="false"){
383
 
384
	#印出結果
385
	var_dump($unixDomainSockClient);
386
 
579 liveuser 387
	#如果要debug
388
	if($debug){
389
 
390
		#函式說明:
391
		#撰寫log
392
		#回傳結果:
393
		#$result["status"],狀態,"true"或"false".
394
		#$result["error"],錯誤訊息陣列.
395
		#$result["function"],當前函式的名稱.
396
		#$result["argu"],使用的參數.
397
		#必填參數:
398
		#$conf["path"],字串,log檔案的路徑與名稱.
399
		$conf["path"]=$logFile;
400
		#$conf["content"],any,要寫的內容,若內容不為字串則會用var_dump的格式寫入.
401
		$conf["content"]=$unixDomainSockClient;
402
		#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
403
		$conf["fileArgu"]=__FILE__;
404
		#可省略參數:
405
		#$conf["rewrite"],預設為"false",接續寫入;反之"true"代表重新寫入.
406
		#$conf["rewrite"]="false";
407
		#參考資料:
408
		#無.
409
		#備註:
410
		#無.
411
		$record=logs::record($conf);
412
		unset($conf);
413
 
414
		#如果寫log失敗
415
		if($record["status"]==="false"){
416
 
417
			#印出結果
418
			var_dump($record);
419
 
420
			}#if end
421
 
422
		}#if end
423
 
424
	#結束執行,回傳錯誤代碼1.
425
	exit(1);
501 liveuser 426
 
427
	}#if end
428
 
429
#解析結果
430
$res=(array)(json_decode($unixDomainSockClient["content"]));
431
 
432
#如果取得id失敗
579 liveuser 433
if($res["status"]==="false"){
501 liveuser 434
 
435
	#印出結果
436
	var_dump($res);
437
 
579 liveuser 438
	#如果要debug
439
	if($debug){
440
 
441
		#函式說明:
442
		#撰寫log
443
		#回傳結果:
444
		#$result["status"],狀態,"true"或"false".
445
		#$result["error"],錯誤訊息陣列.
446
		#$result["function"],當前函式的名稱.
447
		#$result["argu"],使用的參數.
448
		#必填參數:
449
		#$conf["path"],字串,log檔案的路徑與名稱.
450
		$conf["path"]=$logFile;
451
		#$conf["content"],any,要寫的內容,若內容不為字串則會用var_dump的格式寫入.
452
		$conf["content"]=$res;
453
		#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
454
		$conf["fileArgu"]=__FILE__;
455
		#可省略參數:
456
		#$conf["rewrite"],預設為"false",接續寫入;反之"true"代表重新寫入.
457
		#$conf["rewrite"]="false";
458
		#參考資料:
459
		#無.
460
		#備註:
461
		#無.
462
		$record=logs::record($conf);
463
		unset($conf);
464
 
465
		#如果寫log失敗
466
		if($record["status"]==="false"){
467
 
468
			#印出結果
469
			var_dump($record);
470
 
471
			}#if end
472
 
473
		}#if end
474
 
475
	#結束執行,回傳錯誤代碼1.
476
	exit(1);
501 liveuser 477
 
579 liveuser 478
	}#if end
501 liveuser 479
 
480
#印出回應
579 liveuser 481
for($i=0;$i<count($res["output"]);$i++){
482
 
501 liveuser 483
	#印出結果
484
	echo $res["output"][$i].PHP_EOL;
579 liveuser 485
 
486
	}#for end