Subversion Repositories qbpwcf-lib(archive)

Rev

Rev 373 | Rev 375 | 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
 
212
			#輸出結果
374 liveuser 213
			echo $line["name"];
373 liveuser 214
 
371 liveuser 215
			}#foreach end
216
 
217
		#正常結束執行
218
		exit(0);
219
 
220
		}#if end
221
 
222
	#反之要 指定程式 的用法
223
	else{
224
 
225
		#函式說明:
226
		#呼叫shell執行系統命令,並取得回傳的內容.
227
		#回傳結果:
228
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
229
		#$result["error"],錯誤訊息陣列.
230
		#$result["function"],當前執行的函數名稱.
231
		#$result["argu"],使用的參數.
232
		#$result["cmd"],執行的指令內容.
233
		#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
234
		#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
235
		#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
236
		#$result["running"],是否還在執行.
237
		#$result["pid"],pid.
238
		#$result["statusCode"],執行結束後的代碼.
239
		#必填參數:
240
		#$conf["command"],字串,要執行的指令與.
241
		$conf["command"]=$parseArgu["content"]["list"][0];
242
		#$conf["fileArgu"],字串,變數__FILE__的內容.
243
		$conf["fileArgu"]=__FILE__;
244
		#可省略參數:
245
		#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
246
		$conf["argu"]=array("-h");
247
		#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
248
		#$conf["arguIsAddr"]=array();
249
		#$conf["plainArgu"],字串陣列,哪幾個參數不要加上"",若為"true"則代表不用包;反之"false"則代表要包.
250
		#$conf["plainArgu"]=array();
251
		#$conf["useApostrophe"],字串陣列,如果有需要包住,則用「'」,而非「"」處理.前者為"true";後者為"false".
252
		#$conf["useApostrophe"]=array();
253
		#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
254
		#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
255
		#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
256
		#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
257
		#$conf["enablePrintDescription"]="true";
258
		#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
259
		#$conf["printDescription"]="";
260
		#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
261
		$conf["escapeshellarg"]="true";
262
		#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
263
		#$conf["thereIsShellVar"]=array();
264
		#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
265
		#$conf["username"]="";
266
		#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
267
		#$conf["password"]="";
268
		#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
269
		#$conf["useScript"]="";
270
		#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
271
		#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
272
		#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
273
		#$conf["inBackGround"]="";
274
		#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
275
		#$conf["getErr"]="false";
276
		#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
277
		#$conf["doNotRun"]="false";
278
		#參考資料:
279
		#exec=>http://php.net/manual/en/function.exec.php
280
		#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
281
		#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
282
		#備註:
283
		#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
284
		$callShell=external::callShell($conf);
285
		unset($conf);
286
 
287
		#如果執行失敗
288
		if($callShell["status"]==="false"){
289
 
290
			#印出結果
291
			var_dump($callShell);
292
 
293
			#一般錯誤結束執行
294
			exit(1);
295
 
296
			}#if end
297
 
298
		#針對每行輸出
299
		foreach($result["output"] as $line){
300
 
301
			#輸出結果
302
			echo $line;
303
 
304
			}#foreach end
305
 
306
		#正常結束執行
307
		exit(0);
308
 
309
		}#else end
310
 
311
	}#if end
312
 
313
#如果有 cmd 參數
314
if(isset($parseArgu["content"]["cmd"])){
315
 
316
	#取得第一個 cmd 參數
317
	$cmd=$parseArgu["content"]["cmd"][0];
318
 
319
	#初始化參數
320
	$params=array();
321
 
322
	#如果有 paramName 參數
323
	if(isset($parseArgu["content"]["paramName"])){
324
 
325
		#針對每個參數名稱
326
		for($i=0;$i<count($parseArgu["content"]["paramName"]);$i++){
327
 
328
			#取得第 params 名稱
329
			$params[]=$parseArgu["content"]["paramName"][$i];
330
 
331
			#取得第 params 數值
332
			$params[]=$parseArgu["content"]["paramValue"][$i];
333
 
334
			}#if end
335
 
336
		}#if end
337
 
338
	#函式說明:
339
	#呼叫shell執行系統命令,並取得回傳的內容.
340
	#回傳結果:
341
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
342
	#$result["error"],錯誤訊息陣列.
343
	#$result["function"],當前執行的函數名稱.
344
	#$result["argu"],使用的參數.
345
	#$result["cmd"],執行的指令內容.
346
	#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
347
	#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
348
	#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
349
	#$result["running"],是否還在執行.
350
	#$result["pid"],pid.
351
	#$result["statusCode"],執行結束後的代碼.
352
	#必填參數:
353
	#$conf["command"],字串,要執行的指令與.
354
	$conf["command"]=$cmd;
355
	#$conf["fileArgu"],字串,變數__FILE__的內容.
356
	$conf["fileArgu"]=__FILE__;
357
	#可省略參數:
358
	#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
359
	$conf["argu"]=$params;
360
	#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
361
	#$conf["arguIsAddr"]=array();
362
	#$conf["plainArgu"],字串陣列,哪幾個參數不要加上"",若為"true"則代表不用包;反之"false"則代表要包.
363
	#$conf["plainArgu"]=array();
364
	#$conf["useApostrophe"],字串陣列,如果有需要包住,則用「'」,而非「"」處理.前者為"true";後者為"false".
365
	#$conf["useApostrophe"]=array();
366
	#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
367
	#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
368
	#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
369
	#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
370
	#$conf["enablePrintDescription"]="true";
371
	#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
372
	#$conf["printDescription"]="";
373
	#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
374
	$conf["escapeshellarg"]="true";
375
	#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
376
	#$conf["thereIsShellVar"]=array();
377
	#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
378
	#$conf["username"]="";
379
	#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
380
	#$conf["password"]="";
381
	#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
382
	#$conf["useScript"]="";
383
	#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
384
	#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
385
	#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
386
	#$conf["inBackGround"]="";
387
	#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
388
	#$conf["getErr"]="false";
389
	#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
390
	#$conf["doNotRun"]="false";
391
	#參考資料:
392
	#exec=>http://php.net/manual/en/function.exec.php
393
	#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
394
	#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
395
	#備註:
396
	#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
397
	$callShell=external::callShell($conf);
398
	unset($conf);
399
 
400
	#如果執行失敗
401
	if($callShell["status"]==="false"){
402
 
403
		#印出結果
404
		var_dump($callShell);
405
 
406
		#一般錯誤結束執行
407
		exit(1);
408
 
409
		}#if end
410
 
411
	#針對每行輸出
412
	foreach($result["output"] as $line){
413
 
414
		#輸出結果
415
		echo $line;
416
 
417
		}#foreach end
418
 
419
	#正常結束執行
420
	exit(0);
421
 
422
	}#if end
423
 
424
?>