Subversion Repositories qbpwcf-lib(archive)

Rev

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

Rev Author Line No. Line
371 liveuser 1
#!/bin/php
2
<?php
3
 
4
/*
5
	QBPWCF, Quick Build PHP website Component base on Fedora Linux.
6
    Copyright (C) 2015~2023 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
測試 usr/bin/ 底下的所有程式
28
 
29
*/
30
 
31
#使用命名空間qbpwcf
32
namespace qbpwcf;
33
 
34
#匯入外部套件
35
include("/usr/lib/qbpwcf/allInOne.php");
36
 
37
#說明函式
38
function help()
39
{
40
	#印出指令說明
41
	echo "用於測試 usr/bin/ 底下程式的測試程式".PHP_EOL; 
42
	echo "Usage of ".basename(__FILE__).":".PHP_EOL; 
43
	echo "\t--list all/name of program under 'usr/bin/' 代表顯示所有可以執行的程式或顯示特定程式的用法.".PHP_EOL;
44
	echo "\t--cmd name of program under 'usr/bin/' 代表要執行的程式名稱.".PHP_EOL;
45
	echo "\t--paramName a param name of program under 'usr/bin/' 代表執行程式要用的參數名稱.".PHP_EOL;
46
	echo "\t--paramValue a param value of program under 'usr/bin/' 代表執行程式要用的參數數值.".PHP_EOL;
47
 
48
	#一般錯誤結束執行
49
	exit(1);
50
}
51
 
52
#函式說明:
53
#解析參數.
54
#回傳結果:
55
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
56
#$reuslt["error"],執行不正常結束的錯訊息陣列.
57
#$result["function"],當前執行的函式名稱.
58
#$result["content"],解析好的參數陣列.
59
#$result["content"][$key][$i],參數 $key 的 $i+1 個參數數值內容.
60
#$result["program"],字串,執行的程式名稱.
61
#必填參數:
62
#無
63
#可省略參數:
64
#$conf["helpFunc"],如果解析的參數不成對,則要執行的函式名稱.
65
$conf["helpFunc"]="help";
66
#備註:
67
#僅能在命令列底下執行.
68
#建議:
69
#以後可將參數 --a--b 的名稱與後面的數值 $value 存成 $result["a"]["b"][$i]=$value .
70
$parseArgu=cmd::parseArgu($conf);
71
unset($conf);
72
 
73
#如果解析參數失敗
74
if($parseArgu["status"]==="false")
75
{
76
	#印出結果
77
	var_dump($parseArgu);
78
 
79
	#一般錯誤結束執行
80
	exit(1);
81
 
82
}#if end
83
 
84
#檢查參數
85
#函式說明:
86
#檢查必填與可省略參數,可省略參數可指定預設要給與什麼數值內容。
87
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
88
#$reuslt["error"],執行不正常結束的錯訊息陣列.
89
#$result["function"],當前執行的函式名稱.
90
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
91
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
92
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
93
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
94
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
95
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
96
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
97
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
98
#必填寫的參數:
99
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
100
$conf["varInput"]=&$parseArgu["content"];
101
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
102
$conf["referenceVarKey"]="variableCheck::checkArguments";
103
#可以省略的參數:
104
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
105
#$conf["mustBeFilledVariableName"]=array("config","acctVarName","passVarName","dbVarName","dbAddrVarName","dbPortVarName","backupAddr");
106
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
107
#$conf["mustBeFilledVariableType"]=array("array","array","array","array","array","array","array");
108
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
109
#$conf["canBeEmptyString"]="false";
110
#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
111
#$conf["canNotBeEmpty"]=array();
112
#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
113
#$conf["canBeEmpty"]=array();
114
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
115
#$conf["skipableVariableCanNotBeEmpty"]=array("backTime");
116
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
117
$conf["skipableVariableName"]=array("list","cmd","paramName","paramValue");
118
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
119
$conf["skipableVariableType"]=array("array","array","array","array");
120
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
121
$conf["skipableVarDefaultValue"]=array(null,null,null,null);
122
#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
123
#$conf["variableCheck::checkArguments"]["disallowAllSkipableVarIsEmpty"]="false";
124
#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
125
#$conf["disallowAllSkipableVarIsEmptyArray"]="";
126
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
127
$conf["arrayCountEqualCheck"][]=array("paramName","paramValue");
128
#參考資料來源:
129
#array_keys=>http://php.net/manual/en/function.array-keys.php
130
$checkArguments=variableCheck::checkArguments($conf);
131
unset($conf);
132
 
133
#若執行失敗
134
if($checkArguments["status"]==="false"){
135
 
136
	#印出結果
137
	var_dump($checkArguments);
138
 
139
	#一般錯誤結束執行
140
	exit(1);
141
 
142
	}#if end
143
 
144
#若檢查不通過
145
if($checkArguments["passed"]==="false"){
146
 
147
	#印出結果
148
	var_dump($checkArguments);
149
 
150
	#一般錯誤結束執行
151
	exit(1);
152
 
153
	}#if end
154
 
155
#如果有 list 參數
156
if(isset($parseArgu["content"]["list"])){
157
 
158
	#如果第一個 list 參數為 all
159
	if($parseArgu["content"]["list"][0]==="all"){
160
 
161
		#取得 usr/bin 底下的程式清單
162
		#函式說明:
163
		#開啟特定目錄,取得底下的檔案路徑清單.
164
		#回傳結果:
165
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
166
		#$result["error"],錯誤訊息.
167
		#$result["function"],當前執行的函數名稱.
168
		#$result["argu"],所使用的參數.	
169
		#$result["content"],讀取到的內容陣列.
170
		#必填參數:
171
		#$conf["path"],字串,要取得檔案資訊的所屬路徑.
172
		$conf["path"]="/usr/lib/qbpwcf/usr/bin/";
173
		#可省略參數:
174
		#無.
175
		#參考資料
176
		#無.
177
		#備註:
178
		#無.
179
		$listInfo=fileAccess::listInfo($conf);
180
		unset($conf);
181
 
182
		#如果執行失敗
183
		if($listInfo["status"]==="false"){
184
 
185
			#印出結果
186
			var_dump($listInfo);
187
 
188
			#一般錯誤結束執行
189
			exit(1);
190
 
191
			}#if end
192
 
193
		#針對每行輸出
194
		foreach($listInfo["content"] as $line){
195
 
196
			#輸出結果
197
			echo $line;
198
 
199
			}#foreach end
200
 
201
		#正常結束執行
202
		exit(0);
203
 
204
		}#if end
205
 
206
	#反之要 指定程式 的用法
207
	else{
208
 
209
		#函式說明:
210
		#呼叫shell執行系統命令,並取得回傳的內容.
211
		#回傳結果:
212
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
213
		#$result["error"],錯誤訊息陣列.
214
		#$result["function"],當前執行的函數名稱.
215
		#$result["argu"],使用的參數.
216
		#$result["cmd"],執行的指令內容.
217
		#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
218
		#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
219
		#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
220
		#$result["running"],是否還在執行.
221
		#$result["pid"],pid.
222
		#$result["statusCode"],執行結束後的代碼.
223
		#必填參數:
224
		#$conf["command"],字串,要執行的指令與.
225
		$conf["command"]=$parseArgu["content"]["list"][0];
226
		#$conf["fileArgu"],字串,變數__FILE__的內容.
227
		$conf["fileArgu"]=__FILE__;
228
		#可省略參數:
229
		#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
230
		$conf["argu"]=array("-h");
231
		#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
232
		#$conf["arguIsAddr"]=array();
233
		#$conf["plainArgu"],字串陣列,哪幾個參數不要加上"",若為"true"則代表不用包;反之"false"則代表要包.
234
		#$conf["plainArgu"]=array();
235
		#$conf["useApostrophe"],字串陣列,如果有需要包住,則用「'」,而非「"」處理.前者為"true";後者為"false".
236
		#$conf["useApostrophe"]=array();
237
		#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
238
		#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
239
		#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
240
		#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
241
		#$conf["enablePrintDescription"]="true";
242
		#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
243
		#$conf["printDescription"]="";
244
		#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
245
		$conf["escapeshellarg"]="true";
246
		#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
247
		#$conf["thereIsShellVar"]=array();
248
		#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
249
		#$conf["username"]="";
250
		#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
251
		#$conf["password"]="";
252
		#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
253
		#$conf["useScript"]="";
254
		#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
255
		#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
256
		#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
257
		#$conf["inBackGround"]="";
258
		#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
259
		#$conf["getErr"]="false";
260
		#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
261
		#$conf["doNotRun"]="false";
262
		#參考資料:
263
		#exec=>http://php.net/manual/en/function.exec.php
264
		#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
265
		#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
266
		#備註:
267
		#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
268
		$callShell=external::callShell($conf);
269
		unset($conf);
270
 
271
		#如果執行失敗
272
		if($callShell["status"]==="false"){
273
 
274
			#印出結果
275
			var_dump($callShell);
276
 
277
			#一般錯誤結束執行
278
			exit(1);
279
 
280
			}#if end
281
 
282
		#針對每行輸出
283
		foreach($result["output"] as $line){
284
 
285
			#輸出結果
286
			echo $line;
287
 
288
			}#foreach end
289
 
290
		#正常結束執行
291
		exit(0);
292
 
293
		}#else end
294
 
295
	}#if end
296
 
297
#如果有 cmd 參數
298
if(isset($parseArgu["content"]["cmd"])){
299
 
300
	#取得第一個 cmd 參數
301
	$cmd=$parseArgu["content"]["cmd"][0];
302
 
303
	#初始化參數
304
	$params=array();
305
 
306
	#如果有 paramName 參數
307
	if(isset($parseArgu["content"]["paramName"])){
308
 
309
		#針對每個參數名稱
310
		for($i=0;$i<count($parseArgu["content"]["paramName"]);$i++){
311
 
312
			#取得第 params 名稱
313
			$params[]=$parseArgu["content"]["paramName"][$i];
314
 
315
			#取得第 params 數值
316
			$params[]=$parseArgu["content"]["paramValue"][$i];
317
 
318
			}#if end
319
 
320
		}#if end
321
 
322
	#函式說明:
323
	#呼叫shell執行系統命令,並取得回傳的內容.
324
	#回傳結果:
325
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
326
	#$result["error"],錯誤訊息陣列.
327
	#$result["function"],當前執行的函數名稱.
328
	#$result["argu"],使用的參數.
329
	#$result["cmd"],執行的指令內容.
330
	#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
331
	#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
332
	#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
333
	#$result["running"],是否還在執行.
334
	#$result["pid"],pid.
335
	#$result["statusCode"],執行結束後的代碼.
336
	#必填參數:
337
	#$conf["command"],字串,要執行的指令與.
338
	$conf["command"]=$cmd;
339
	#$conf["fileArgu"],字串,變數__FILE__的內容.
340
	$conf["fileArgu"]=__FILE__;
341
	#可省略參數:
342
	#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
343
	$conf["argu"]=$params;
344
	#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
345
	#$conf["arguIsAddr"]=array();
346
	#$conf["plainArgu"],字串陣列,哪幾個參數不要加上"",若為"true"則代表不用包;反之"false"則代表要包.
347
	#$conf["plainArgu"]=array();
348
	#$conf["useApostrophe"],字串陣列,如果有需要包住,則用「'」,而非「"」處理.前者為"true";後者為"false".
349
	#$conf["useApostrophe"]=array();
350
	#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
351
	#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
352
	#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
353
	#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
354
	#$conf["enablePrintDescription"]="true";
355
	#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
356
	#$conf["printDescription"]="";
357
	#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
358
	$conf["escapeshellarg"]="true";
359
	#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
360
	#$conf["thereIsShellVar"]=array();
361
	#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
362
	#$conf["username"]="";
363
	#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
364
	#$conf["password"]="";
365
	#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
366
	#$conf["useScript"]="";
367
	#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
368
	#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
369
	#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
370
	#$conf["inBackGround"]="";
371
	#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
372
	#$conf["getErr"]="false";
373
	#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
374
	#$conf["doNotRun"]="false";
375
	#參考資料:
376
	#exec=>http://php.net/manual/en/function.exec.php
377
	#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
378
	#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
379
	#備註:
380
	#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
381
	$callShell=external::callShell($conf);
382
	unset($conf);
383
 
384
	#如果執行失敗
385
	if($callShell["status"]==="false"){
386
 
387
		#印出結果
388
		var_dump($callShell);
389
 
390
		#一般錯誤結束執行
391
		exit(1);
392
 
393
		}#if end
394
 
395
	#針對每行輸出
396
	foreach($result["output"] as $line){
397
 
398
		#輸出結果
399
		echo $line;
400
 
401
		}#foreach end
402
 
403
	#正常結束執行
404
	exit(0);
405
 
406
	}#if end
407
 
408
?>