Subversion Repositories qbpwcf-lib(archive)

Rev

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