Subversion Repositories qbpwcf-lib(archive)

Rev

Rev 906 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 liveuser 1
#!/usr/bin/php
2
<?php
3
 
4
/*
464 liveuser 5
	QBPWCF, Quick Build PHP website Component base on Fedora Linux.
619 liveuser 6
    Copyright (C) 2015~2024 Min-Jhin,Chen
1 liveuser 7
 
464 liveuser 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
 
1 liveuser 27
說明:
28
終止指定的一個或多個程序,然後再啟動其程式.
29
 
30
*/
31
 
32
#指派命名空間
33
namespace qbpwcf;
34
 
466 liveuser 35
#以該檔案的實際位置的 lib path 為 include path 首位
920 liveuser 36
exec("cd ".pathinfo(__FILE__)["dirname"]."/../lib/qbpwcf;pwd;",$output,$status);
466 liveuser 37
set_include_path($output[0].PATH_SEPARATOR.get_include_path());
38
 
464 liveuser 39
#匯入外部套件
466 liveuser 40
include("allInOne.php");
1 liveuser 41
 
42
#函式說明:
43
#抓取命令列的參數.
44
#回傳結果:
45
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
46
#$reuslt["error"],執行不正常結束的錯訊息陣列.
47
#$result["function"],當前執行的函式名稱.
48
#$result["argu"],使用的參數陣列.
49
#$result["content"],要回傳的參數陣列.
50
#$result["count"],參數的數量.
51
#必填參數:
52
#無
53
#可省略參數:
54
#$conf["echo"],"true"代表要將抓到的參數一個個印出來,"false"代表用回傳的方式,預設為"false".
55
#$conf["echo"]="false";
56
$conf=array();
57
$getArgu=cmd::getArgu($conf);
58
unset($conf);
59
 
60
#如果執行失敗
61
if($getArgu["status"]==="false"){
62
 
63
	#設置執行失敗
64
	$result["status"]="false";
65
 
66
	#設置執行錯誤訊息
67
	$result["error"]=$getArgu;
68
 
69
	#印出訊息
70
	var_dump($result);
71
 
72
	#結束執行
73
	exit;
74
 
75
	}#if end
76
 
77
#如果沒參數
78
if($getArgu["count"]===1){
79
 
80
	#印出用法
81
	#函式說明:
82
	#印出多行文字,結尾自動換行.
83
	#回傳的結果:
84
	#$result["status"],執行是否成功,"true"代表成功,"false"代表失敗.
85
	#$result["function"],當前執行的函數名稱.
86
	#$result["error"],錯誤訊息陣列.
87
	#必填的參數:
88
	#$conf["outputStringArray"],字串陣列,每行要印出的文字內容.
89
	$conf["outputStringArray"]=array();
90
	$conf["outputStringArray"][]="-h/--help 顯示本說明";
91
	$conf["outputStringArray"][]="--program2kill 要結束的特定程式關鍵字,若有多個則代表有多個程序要結束";
92
	$conf["outputStringArray"][]="--program2run 要執行的程式位置與名稱";
93
	$echoMultiLine=cmd::echoMultiLine($conf);
94
	unset($conf);
95
 
96
	#結束執行
97
	exit;
98
 
99
	}
100
 
101
#第一個參數
102
$firsArgu=$getArgu["content"][1];
103
 
104
#如果程式後面的參數爲 -h 或 --help 參
105
if($firsArgu==="-h" || $firsArgu==="--help"){
106
 
107
	#印出用法
108
	#函式說明:
109
	#印出多行文字,結尾自動換行.
110
	#回傳的結果:
111
	#$result["status"],執行是否成功,"true"代表成功,"false"代表失敗.
112
	#$result["function"],當前執行的函數名稱.
113
	#$result["error"],錯誤訊息陣列.
114
	#必填的參數:
115
	#$conf["outputStringArray"],字串陣列,每行要印出的文字內容.
116
	$conf["outputStringArray"]=array();
117
	$conf["outputStringArray"][]="-h/--help 顯示本說明";
118
	$conf["outputStringArray"][]="--program2kill 要結束的程式關鍵字";
119
	$conf["outputStringArray"][]="--program2run 要執行的程式位置與名稱";
120
	$echoMultiLine=cmd::echoMultiLine($conf);
121
	unset($conf);
122
 
123
	#結束執行
124
	exit;
125
 
126
	}
127
 
128
#函式說明:
129
#解析參數.
130
#回傳結果:
131
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
132
#$reuslt["error"],執行不正常結束的錯訊息陣列.
133
#$result["function"],當前執行的函式名稱.
134
#$result["content"],解析好的參數陣列.
135
#$result["content"][$key][$i],參數 $key 的 $i+1 個參數數值內容.
136
#$result["program"],字串,執行的程式名稱.
137
#必填參數:
138
#無
139
#可省略參數:
140
#無
141
#備註:
142
#僅能在命令列底下執行.
143
#建議:
144
#以後可將參數 --a--b 的名稱與後面的數值 $value 存成 $result["a"]["b"][$i]=$value .
104 liveuser 145
$conf=array();
146
$parseArgu=cmd::parseArgu($conf);
147
unset($conf);
1 liveuser 148
 
149
#要終止的程式關鍵字
150
$programs2kill=$parseArgu["content"]["program2kill"];
151
 
152
#要執行的程式位置與名稱
153
$program2run=$parseArgu["content"]["program2run"][0];
154
 
155
#針對每個要中止程序的關鍵字
156
foreach($programs2kill as $program2kill){
157
 
158
	#透過 ps auxwf 檢查執行中的程式 pid
159
	#涵式說明:
160
	#呼叫shell執行系統命令,並取得回傳的內容.
161
	#回傳的結果:
162
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
163
	#$result["error"],錯誤訊息陣列.
164
	#$result["function"],當前執行的函數名稱.
165
	#$result["argu"],使用的參數.
166
	#$result["cmd"],執行的指令內容.
167
	#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
168
	#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
169
	#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
170
	#$result["running"],是否還在執行.
171
	#$result["pid"],pid.
172
	#$result["statusCode"],執行結束後的代碼.
173
	#必填的參數
174
	#$conf["command"],字串,要執行的指令與.
175
	$conf["command"]="ps";
176
	#$conf["fileArgu"],字串,變數__FILE__的內容.
177
	$conf["fileArgu"]=__FILE__;		
178
	#可省略參數:
179
	#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
180
	$conf["argu"]=array("auxwf","|","grep",$program2kill);
181
	#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
182
	#$conf["arguIsAddr"]=array();	
183
	#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
184
	#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
185
	#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
186
	#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
187
	$conf["enablePrintDescription"]="true";
188
	#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
189
	#$conf["printDescription"]="";
190
	#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".
453 liveuser 191
	$conf["escapeshellarg"]="true";
1 liveuser 192
	#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
193
	#$conf["username"]="";
194
	#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
195
	#$conf["password"]="";
196
	#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
197
	#$conf["useScript"]="";
198
	#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
199
	#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
200
	#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
201
	#$conf["inBackGround"]="";
202
	#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
203
	#$conf["getErr"]="false";
204
	#備註:
205
	#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
206
	#參考資料:
207
	#exec=>http://php.net/manual/en/function.exec.php
208
	#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
209
	#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
210
	$callShell=external::callShell($conf);
211
	unset($conf);
212
 
213
	#如果執行失敗
214
	if($callShell["status"]==="false"){
215
 
216
		#設置執行失敗
217
		$result["status"]="false";
218
 
219
		#設置執行錯誤訊息
220
		$result["error"]=$getArgu;
221
 
222
		#印出訊息
223
		var_dump($result);
224
 
225
		#結束執行
226
		exit;
227
 
228
		}
229
 
230
	#取得輸出
231
	$output=$callShell["output"];
232
 
233
	#剔除有grep關鍵字的程式	
234
	#函式說明:
235
	#檢查多個字串中的每個字串是否有多個關鍵字
236
	#回傳的結果:
237
	#$result["status"],整體來說,執行是否成功,"true"代表執行成功,"false"代表執行失敗。
238
	#$result["function"],當前執行的函數名稱.
239
	#$result["error"],錯誤訊息.
240
	#$result["argu"],使用的參數.
241
	#$result["foundedTrueKey"],結果為"true"的被搜尋元素key陣列.
242
	#$result["foundedKeyWords"].找到的關鍵字.
243
	#$result["foundedFalseKey"],結果為"false"的被搜尋元素key陣列.
244
	#$result["foundedTrueKeyWords"],二維陣列,各個字串有找到的關鍵字陣列.
245
	#$result["foundedAll"],是否每個關鍵字都有找到,"true"代表每個都有找到,"false"代表沒有每個都找到.
246
	#必填的參數:
247
	$conf["keyWords"]=array("grep","rerunProgram.php");#想要搜尋的關鍵字
248
	$conf["stringArray"]=$output;#要被搜尋的字串內容陣列
249
	#可省略的參數:
250
	#$conf["completeEqual"]="true";#是否內容要完全符合,不能多出任何不符合的內容,預設為"false"不需要完全符合.
251
	$findManyKeyWordsFromManyString=search::findManyKeyWordsFromManyString($conf);
252
	unset($conf);
253
 
254
	#如果執行失敗
255
	if($findManyKeyWordsFromManyString["status"]==="false"){
256
 
257
		#設置執行失敗
258
		$result["status"]="false";
259
 
260
		#設置執行錯誤訊息
261
		$result["error"]=$findManyKeyWordsFromManyString;
262
 
263
		#印出訊息
264
		var_dump($result);
265
 
266
		#結束執行
267
		exit;
268
 
269
		}
270
 
271
	#如果沒有找到要移除的程序
272
	if(!isset($findManyKeyWordsFromManyString["foundedFalseKey"]))
273
	{
274
		#跳下一個
275
		continue;
276
	}
277
 
278
	#取得程序的輸出
279
	$pidsInfo=$findManyKeyWordsFromManyString["foundedFalseKey"];
280
 
281
	#有幾個程序就執行幾次
282
	foreach($pidsInfo as $pidInfo){
283
 
284
		#取得 pid
285
		#涵式說明:
286
		#將固定格式的字串分開,並回傳分開的結果。
287
		#回傳結果:
288
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
289
		#$result["error"],錯誤訊息陣列
290
		#$result["function"],當前執行的函數名稱.
291
		#$result["argu"],使用的參數.
292
		#$result["oriStr"],要分割的原始字串內容
293
		#$result["dataArray"],爲分割好字串的陣列內容,$result["dataArray"][$i]爲第($i+1)段的內容。
294
		#$result["dataCounts"],爲總共分成幾段
295
		#$result["found"],是否有在$conf["stringIn"]找到$conf["spiltSymbol"],"true"代表有找到,"false"代表沒有找到.
296
		#必填的參數:
297
		$conf["stringIn"]=$pidInfo;#要處理的字串。
298
		$conf["spiltSymbol"]=" ";#爲以哪個符號作爲分割
299
		#可省略參數:
300
		#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
301
		$conf["allowEmptyStr"]="false";
302
		$spiltString=stringProcess::spiltString($conf);
303
		unset($conf);
304
 
305
		#如果執行失敗
306
		if($spiltString["status"]==="false"){
307
 
308
			#設置執行失敗
309
			$result["status"]="false";
310
 
311
			#設置執行錯誤訊息
312
			$result["error"]=$spiltString;
313
 
314
			#印出訊息
315
			var_dump($result);
316
 
317
			#結束執行
318
			exit;
319
 
320
			}
321
 
322
		#取得程序的pid
323
		$pid=$spiltString["dataArray"][1];
324
 
325
		#涵式說明:
326
		#呼叫shell執行系統命令,並取得回傳的內容.
327
		#回傳的結果:
328
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
329
		#$result["error"],錯誤訊息陣列.
330
		#$result["function"],當前執行的函數名稱.
331
		#$result["argu"],使用的參數.
332
		#$result["cmd"],執行的指令內容.
333
		#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
334
		#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
335
		#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
336
		#$result["running"],是否還在執行.
337
		#$result["pid"],pid.
338
		#$result["statusCode"],執行結束後的代碼.
339
		#必填的參數
340
		#$conf["command"],字串,要執行的指令與.
341
		$conf["command"]="kill";
342
		#$conf["fileArgu"],字串,變數__FILE__的內容.
343
		$conf["fileArgu"]=__FILE__;		
344
		#可省略參數:
345
		#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
346
		$conf["argu"]=array($pid);
347
		#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
348
		#$conf["arguIsAddr"]=array();	
349
		#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
350
		#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
351
		#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
352
		#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
353
		$conf["enablePrintDescription"]="true";
354
		#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
355
		#$conf["printDescription"]="";
356
		#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".
453 liveuser 357
		$conf["escapeshellarg"]="true";
1 liveuser 358
		#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
359
		#$conf["username"]="";
360
		#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
361
		#$conf["password"]="";
362
		#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
363
		#$conf["useScript"]="";
364
		#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
365
		#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
366
		#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
367
		#$conf["inBackGround"]="";
368
		#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
369
		#$conf["getErr"]="false";
370
		#備註:
371
		#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
372
		#參考資料:
373
		#exec=>http://php.net/manual/en/function.exec.php
374
		#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
375
		#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
376
		$callShell=external::callShell($conf);
377
		unset($conf);
378
 
379
		#如果執行失敗
380
		if($callShell["status"]==="false"){
381
 
382
			#設置執行失敗
383
			$result["status"]="false";
384
 
385
			#設置執行錯誤訊息
386
			$result["error"]=$getArgu;
387
 
388
			#印出訊息
389
			var_dump($result);
390
 
391
			#結束執行
392
			exit;
393
 
394
			}
395
 
396
		#印出執行的結果
397
		var_dump($callShell);
398
 
399
		}
400
 
401
	}#foreach end
402
 
403
#執行程式 $program2run
404
#涵式說明:
405
#呼叫shell執行系統命令,並取得回傳的內容.
406
#回傳的結果:
407
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
408
#$result["error"],錯誤訊息陣列.
409
#$result["function"],當前執行的函數名稱.
410
#$result["argu"],使用的參數.
411
#$result["cmd"],執行的指令內容.
412
#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
413
#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
414
#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
415
#$result["running"],是否還在執行.
416
#$result["pid"],pid.
417
#$result["statusCode"],執行結束後的代碼.
418
#必填的參數
419
#$conf["command"],字串,要執行的指令與.
420
$conf["command"]=$program2run;
421
#$conf["fileArgu"],字串,變數__FILE__的內容.
422
$conf["fileArgu"]=__FILE__;		
423
#可省略參數:
424
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
425
#$conf["argu"]=array($pid);
426
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
427
#$conf["arguIsAddr"]=array();	
428
#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
429
#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
430
#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
431
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
432
$conf["enablePrintDescription"]="true";
433
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
434
#$conf["printDescription"]="";
435
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".
453 liveuser 436
$conf["escapeshellarg"]="true";
1 liveuser 437
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
438
#$conf["username"]="";
439
#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
440
#$conf["password"]="";
441
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
442
#$conf["useScript"]="";
443
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
444
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
445
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
446
$conf["inBackGround"]="true";
447
#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
448
#$conf["getErr"]="false";
449
#備註:
450
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
451
#參考資料:
452
#exec=>http://php.net/manual/en/function.exec.php
453
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
454
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
455
$callShell=external::callShell($conf);
456
unset($conf);
457
 
458
#如果執行失敗
459
if($callShell["status"]==="false"){
460
 
461
	#設置執行失敗
462
	$result["status"]="false";
463
 
464
	#設置執行錯誤訊息
465
	$result["error"]=$getArgu;
466
 
467
	#印出訊息
468
	var_dump($result);
469
 
470
	#結束執行
471
	exit;
472
 
453 liveuser 473
	}#if end
1 liveuser 474
 
475
#印出執行的結果
476
var_dump($callShell);
477
 
478
?>