Subversion Repositories php-qbpwcf

Rev

Rev 226 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
66 liveuser 1
#!/usr/bin/php
2
<?php
3
 
4
/*
5
	QBPWCF, Quick Build PHP website Component base on Fedora Linux.
6
    Copyright (C) 2014~2025 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
一次檢查多個節點是否存在,存在的node清單會以jsone格式印出.
28
 
29
範例:
30
 
31
檢查名為 nodoeToCheckExist 的節點(檔案/目錄)是否存在
32
checkNodeExist.php --node nodoeToCheckExist
33
 
34
檢查名為 nodoeToCheckExist 的節點(檔案/目錄)是否存在於路徑 ./path 底下
35
checkNodeExist.php --node ./path/nodoeToCheckExist
36
 
37
*/
38
 
39
#使用命名空間qbpwcf
40
namespace qbpwcf;
41
 
42
#取得 lib path
43
exec("php -f ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/libexec/folderOfUsrLib.php"),$output,$status);
44
 
45
#如果執行失敗
46
if($status!==0){
47
 
48
	#debug
49
	var_dump(__LINE__,$output);
50
 
51
	#結束執行,回傳shell 1.
52
	exit(1);
53
 
54
	}#if end
55
 
56
#儲存lib path
57
$folderOfUsrLib=$output[0];
58
 
59
#以該檔案的實際位置的 lib path 為 include path 首位
60
$output=array();
61
exec("cd ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/../".$folderOfUsrLib."/qbpwcf").";pwd;",$output,$status);
62
 
63
#如果執行失敗
64
if($status!==0){
65
 
66
	#debug
67
	var_dump(__LINE__,$output);
68
 
69
	#結束執行,回傳shell 1.
70
	exit(1);
71
 
72
	}#if end
73
 
74
#設置 include path 
75
set_include_path($output[0].PATH_SEPARATOR.get_include_path());
76
 
77
#匯入外部套件
78
include("allInOne.php");
79
 
80
#說明函式
81
function help(){
82
 
83
	#印出指令說明
84
	echo "Usage of ".basename(__FILE__).":".PHP_EOL; 
85
	echo "--node [path/node] 代表要確認位於 path 底下的 node 是否存在".PHP_EOL;
86
 
87
	#結束執行
88
	exit;
89
 
90
	}#function help end
91
 
92
#函式說明:
93
#解析參數.
94
#回傳結果:
95
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
96
#$reuslt["error"],執行不正常結束的錯訊息陣列.
97
#$result["function"],當前執行的函式名稱.
98
#$result["content"],解析好的參數陣列.
99
#$result["content"][$key][$i],參數 $key 的 $i+1 個參數數值內容.
100
#$result["program"],字串,執行的程式名稱.
101
#必填參數:
102
#無
103
#可省略參數:
104
#$conf["helpFunc"],如果解析的參數不成對,則要執行的函式名稱.
105
$conf["helpFunc"]="help";
106
#備註:
107
#僅能在命令列底下執行.
108
#建議:
109
#以後可將參數 --a--b 的名稱與後面的數值 $value 存成 $result["a"]["b"][$i]=$value .
110
$parseArgu=cmd::parseArgu($conf);
111
unset($conf);
112
 
113
#如果解析參數失敗
114
if($parseArgu["status"]==="false"){
115
 
116
	#印出結果
117
	var_dump($parseArgu);
118
 
119
	#結束執行,回傳shell 1,代表異常.
120
	exit(1);
121
 
122
	}#if end
123
 
124
#檢查參數
125
#函式說明:
126
#檢查必填與可省略參數,可省略參數可指定預設要給與什麼數值內容。
127
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
128
#$reuslt["error"],執行不正常結束的錯訊息陣列.
129
#$result["function"],當前執行的函式名稱.
130
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
131
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
132
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
133
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
134
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
135
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
136
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
137
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
138
#必填寫的參數:
139
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
140
$conf["varInput"]=&$parseArgu["content"];
141
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
142
$conf["referenceVarKey"]="variableCheck::checkArguments";
143
#可以省略的參數:
144
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
145
#$conf["mustBeFilledVariableName"]=array("backupAddr");
146
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
147
#$conf["mustBeFilledVariableType"]=array("array");
148
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
149
#$conf["canBeEmptyString"]="false";
150
#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
151
#$conf["canNotBeEmpty"]=array();
152
#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
153
#$conf["canBeEmpty"]=array();
154
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
155
#$conf["skipableVariableCanNotBeEmpty"]=array("backTime");
156
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
157
$conf["skipableVariableName"]=array("node");
158
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
159
$conf["skipableVariableType"]=array("array");
160
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
161
$conf["skipableVarDefaultValue"]=array(null);
162
#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
163
#$conf["variableCheck::checkArguments"]["disallowAllSkipableVarIsEmpty"]="false";
164
#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
165
#$conf["disallowAllSkipableVarIsEmptyArray"]="";
166
#$conf["disallowAllSkipableVarNotExist"],字串,是否不允許每個可省略參數都不存在,預設為"false"代表允許,反之為"true".
167
$conf["disallowAllSkipableVarNotExist"]="true";
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);
180
 
181
	#結束執行,回傳shell 1,代表異常.
182
	exit(1);
183
 
184
	}#if end
185
 
186
#若檢查不通過
187
if($checkArguments["passed"]==="false"){
188
 
189
	#印出結果
190
	#var_dump($checkArguments);
191
 
192
	#提示用法
193
	help();
194
 
195
	#結束執行
196
	exit;
197
 
198
	}#if end
199
 
200
#初始化儲存運行的 proc資訊 
201
$procs=array();
202
 
203
#針對每個 node 參數
204
foreach($parseArgu["content"]["node"] as $node){
205
 
206
	#函式說明:
207
	#用shell檢查檔案是否存在,可以指定查詢時用的身份.
208
	#回傳的結果:
209
	#$result["status"],執行是否成功,"true"代表成功,"false"代表失敗.
210
	#$result["function"],當前執行的函式名稱.
211
	#$result["error"],錯誤訊息陣列.
212
	#$result["founded"],"true"代表有找到檔案,"false"代表沒有找到檔案,"?"代表沒有執行指令.
213
	#$result["cmdStr"],查詢檔案是否存在的指令與參數字串.
214
	#$result["escapedCmdArray"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
215
	#必填參數:
216
	#$conf["fileName"],字串,要檢查的檔案名稱.
217
	$conf["cmd::checkFileExist"]["fileName"]=$node;
218
	#$conf["fileArgu"],字串,__FILE__的內容,預設為當前檔案的位置.
219
	$conf["cmd::checkFileExist"]["fileArgu"]=__FILE__;
220
	#可省略參數:
221
	#$conf["username"],字串,要用哪個身份來檢查檔案是否存在,預設不使用.
222
	#$conf["username"]="";
223
	#$conf["password"],字串,要用哪個身份來檢查檔案是否存在,預設不使用,若沒有設定好不用密碼即可登入,則在web端會直接出錯,在命令列則會提示輸入密碼.
224
	#$conf["password"]="";
225
	#$conf["getCmdOnly"],字串,若不要執行指令,只要取得要執行的指令,則設置為"true";反之為預設值"false".
226
	$conf["cmd::checkFileExist"]["getCmdOnly"]="true";
227
	#參考資料:
228
	#無.
229
	#備註:
230
	#僅能在命令列環境下執行.
231
	$checkFileExist=cmd::checkFileExist($conf["cmd::checkFileExist"]);
232
	unset($conf["cmd::checkFileExist"]);
233
 
234
	#若執行失敗
235
	if($checkFileExist["status"]==="false"){
236
 
237
		#印出結果
238
		var_dump($checkFileExist);
239
 
240
		#結束執行,回傳shell 1,代表異常.
241
		exit(1);
242
 
243
		}#if end
244
 
245
	#函式說明:
246
	#透過proc來多執行序運作.
247
	#回傳結果:
248
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
249
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
250
	#$result["function"],當前執行的函式名稱.
251
	#$result["argu"],使用的參數.
252
	#$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"代表程序的資訊.
253
	#必填參數:
254
	#$conf["cmds"],字串陣列,每個元素代表要執行的指令與參數.
255
	$conf["threads::proc"]["cmds"]=array($checkFileExist["cmdStr"]);
256
	#可省略參數:
257
	#$conf["wait"],字串,是否需要等待所有程序結束,預設為"true"要等待;反之為"false"不要等待.
258
	$conf["threads::proc"]["wait"]="false";
259
	#$conf["workingDir"],字串陣列,個別程式執行時的家目錄,預設不指定.
260
	#$conf["workingDir"]=array("path");
261
	#$conf["envs"],2維字串陣列,每個元素代表個別程式執行時的指定環境變數,key變數名稱;value為變數內容.預設為array("QBPWCF" => "Quick Build PHP Website Componment base on Fedora Linux");
262
	#$conf["envs"]=array(array("key"=>"value"));
263
	#$conf["executeBy"],字串陣列,每個元素代表個別指令要用什麼程式執行,預設為"bash".
264
	#$conf["executeBy"]=array("bash");
265
	#參考資料:
266
	#https://www.php.net/manual/en/function.proc-open.php
267
	#https://www.php.net/manual/en/function.proc-get-status.php
268
	#備註:
269
	#若需要取得當下的執行狀況,請使用 self::proc_update 來更新.
270
	$proc=threads::proc($conf["threads::proc"]);
271
	unset($conf["threads::proc"]);
272
 
273
	#如果異常
274
	if($proc["status"]==="false"){
275
 
276
		#印出結果
277
		var_dump($proc);
278
 
279
		#結束執行,回傳shell 1,代表異常.
280
		exit(1);
281
 
282
		}#if end
283
 
284
	#儲存執行的proc資訊
285
	$procs[]=$proc;
286
 
287
	#記錄查詢的node資訊
288
	$nodes2check[]=$node;
289
 
290
	}#if end
291
 
292
#初始化記錄執行正常的 proc 程序
293
$success_procs=array();
294
 
295
#初始化記錄執行失敗的 proc 程序
296
$error_procs=array();
297
 
298
#初始化記錄找到的node清單
299
$found_node=array();
300
 
301
#初始化記錄沒有找到的node清單
302
$not_found_node=array();
303
 
304
#無窮迴圈
305
while(true){
306
 
307
	#針對每個 proc 資訊
308
	foreach($procs as $pIndex => $proc){
309
 
310
		#函式說明:
311
		#更新透過proc執行的多程序資訊.
312
		#回傳結果:
313
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
314
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
315
		#$result["function"],當前執行的函式名稱.
316
		#$result["argu"],使用的參數.
317
		#$result["content"],陣列,每個元素為其指令執行的結果訊息陣列,key為"status"代表執行是否正常的識別;key為"statusCode"代表程式結束後回傳給對應executeBy程式的數值,若為"?"則代表程序尚未結束,可透過proc_update函式進行資訊的更新與取得;key為"output"代表標準輸出,若為resource,則代表為pipe;key為"error"代表非標準輸出,若為resource,則代表為pipe;key為"input"代表成功輸入的指令;key為"process"代表該程序經proc_open後的process source;key為"proc_get_status"代表程序的資訊.
318
		#必填參數:
319
		#$conf["procs"],陣列,運行self::proc後回傳的content.
320
		$conf["threads::proc_update"]["procs"]=$proc["content"];
321
		#可省略參數:
322
		#無.
323
		#參考資料:
324
		#無.
325
		#備註:
326
		#無.
327
		$proc_update=threads::proc_update($conf["threads::proc_update"]);
328
		unset($conf["threads::proc_update"]);
329
 
330
		#如果異常
331
		if($proc_update["status"]==="false"){
332
 
333
			#印出結果
334
			var_dump($proc_update);
335
 
336
			#結束執行,回傳shell 1,代表異常.
337
			exit(1);
338
 
339
			}#if end
340
 
341
		#更新第一個程序的proc資訊
342
		$procs[$pIndex]["content"]=$proc_update["content"];
343
 
344
		#給予別名
345
		$pInfo=&$procs[$pIndex]["content"][0];
346
 
347
		#如果已經執行結束
348
		if($pInfo["statusCode"]!=="?"){
349
 
350
			#若程序執行異常
351
			if($pInfo["statusCode"]!==0){
352
 
353
				#記錄之
354
				$error_procs[]=$pInfo;
355
 
356
				#移除其資訊
357
				unset($procs[$pIndex]);
358
 
359
				#換下個proc程序
360
				continue;
361
 
362
				}#if end
363
 
364
			#記錄之
365
			$success_procs[]=$pInfo;
366
 
367
			#移除其資訊
368
			unset($procs[$pIndex]);
369
 
370
			#取得輸出的結果
371
			$stdOut=trim($pInfo["content"]);
372
 
373
			#如果有找到
374
			if($stdOut==="found"){
375
 
376
				#記錄找到的 node
377
				$found_node[]=$nodes2check[$pIndex];
378
 
379
				}#if end
380
 
381
			#反之若沒有找到
382
			else if($stdOut==="not found"){
383
 
384
				#記錄沒找到的 node
385
				$not_found_node[]=$nodes2check[$pIndex];
386
 
387
				}#if end
388
 
389
			#換下個proc程序
390
			continue;
391
 
392
			}#if end
393
 
394
		#執行到這邊,代表程序尚未結束.
395
 
396
		#換下個proc程序
397
		continue;
398
 
399
		}#foreach end
400
 
401
	#如果已經執行完所有的 proc 程序
402
	if(empty($procs)){
403
 
404
		#結束執行
405
		break;
406
 
407
		}#if end
408
 
409
	#休息0.1秒
410
	usleep(100000);
411
 
412
	}#while end
413
 
414
#印出 json 結果
415
echo json_encode($found_node);