Subversion Repositories php-qbpwcf

Rev

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