Subversion Repositories qbpwcf-lib(archive)

Rev

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