Subversion Repositories php-qbpwcf

Rev

Rev 226 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
#!/usr/bin/php
2
<?php
3
 
4
/*
5
	QBPWCF, Quick Build PHP website Component base on Fedora Linux.
239 liveuser 6
    Copyright (C) 2014~2026 MIN ZHI, CHEN
3 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/>.
226 liveuser 22
 
3 liveuser 23
*/
24
 
25
/*
26
 
27
備註:
28
要確認 fileAccess::parseVaraiableInPHPfile 能否解析多個同名的設定成一個陣列
29
 
30
*/
31
 
32
#指派命名空間
33
namespace qbpwcf;
34
 
35
#取得 lib path
36
exec("php -f ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/libexec/folderOfUsrLib.php"),$output,$status);
37
 
38
#如果執行失敗
39
if($status!==0){
40
 
41
	#debug
42
	var_dump(__LINE__,$output);
43
 
44
	#結束執行,回傳shell 1.
45
	exit(1);
46
 
47
	}#if end
48
 
49
#儲存lib path
50
$folderOfUsrLib=$output[0];
51
 
52
#以該檔案的實際位置的 lib path 為 include path 首位
53
$output=array();
54
exec("cd ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/../".$folderOfUsrLib."/qbpwcf").";pwd;",$output,$status);
55
 
56
#如果執行失敗
57
if($status!==0){
58
 
59
	#debug
60
	var_dump(__LINE__,$output);
61
 
62
	#結束執行,回傳shell 1.
63
	exit(1);
64
 
65
	}#if end
66
 
226 liveuser 67
#設置 include path
3 liveuser 68
set_include_path($output[0].PATH_SEPARATOR.get_include_path());
69
 
70
#匯入外部套件
71
require_once("allInOne.php");
72
 
73
#該指令的 help 文件
74
function help(){
75
 
76
	#印出指令說明
77
	echo basename($_SERVER["PHP_SELF"])." is a program to kepp sshd running ok.".PHP_EOL;
78
	echo basename($_SERVER["PHP_SELF"])." usage:".PHP_EOL;
79
	echo "--ipAndPort [sshd listen ip address and port]".PHP_EOL;
80
	echo "\texample: 169.254.1.1:22".PHP_EOL;
81
	echo "--reportOnly [true]".PHP_EOL;
82
	echo "\tset it to true will only output message, will not try to start or restart service automatically.".PHP_EOL;
83
	echo "--config addr 代表設定檔的位置,裡面寫需要使用的 ipAndPort 或 reportOnly 變數參數.".PHP_EOL;
84
	echo "若都沒有參數,則預設會使用 --config etc/qbpwcf/sshd_config.php 參數.".PHP_EOL;
85
	exit;
86
 
87
	}#function help end
88
 
89
#函式說明:
90
#解析參數.
91
#回傳結果:
92
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
93
#$reuslt["error"],執行不正常結束的錯訊息陣列.
94
#$result["function"],當前執行的函式名稱.
95
#$result["content"],解析好的參數陣列.
96
#$result["content"][$key][$i],參數 $key 的 $i+1 個參數數值內容.
97
#$result["program"],字串,執行的程式名稱.
98
#必填參數:
99
#無.
100
#可省略參數:
101
#$conf["helpFunc"],如果解析的參數不成對,則要執行的函式名稱.
102
$conf["helpFunc"]="help";
103
#參考資料:
104
#無.
105
#備註:
106
#僅能在命令列底下執行.
107
#以後可將參數 --a--b 的名稱與後面的數值 $value 存成 $result["a"]["b"][$i]=$value .
108
$parseArgu=cmd::parseArgu($conf);
109
unset($conf);
110
 
111
#如果執行失敗
112
if($parseArgu["status"]==="false"){
113
 
114
	#debug
115
	var_dump($parseArgu);
116
 
117
	#異常結束執行
118
	exit(1);
119
 
120
	}#if end
121
 
122
#檢查參數
123
#函式說明:
124
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
125
#回傳結果:
126
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
127
#$reuslt["error"],執行不正常結束的錯訊息陣列.
128
#$result["function"],當前執行的函式名稱.
129
#$result["argu"],設置給予的參數.
130
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
131
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
132
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
133
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
134
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
135
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
136
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
137
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
138
#必填參數:
139
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
140
$conf["varInput"]=&$parseArgu["content"];
141
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
142
$conf["referenceVarKey"]="variableCheck::checkArguments";
143
#可省略參數:
144
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
145
#$conf["mustBeFilledVariableName"]=array("");
146
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
147
#$conf["mustBeFilledVariableType"]=array();
148
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
149
#$conf["canBeEmptyString"]="false";
150
#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
151
#$conf["canNotBeEmpty"]=array();
152
#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
153
#$conf["canBeEmpty"]=array();
154
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
155
$conf["skipableVariableCanNotBeEmpty"]=array("ipAndPort","reportOnly","config");
156
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
157
$conf["skipableVariableName"]=array("ipAndPort","reportOnly","config");
158
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
159
$conf["skipableVariableType"]=array("string","string","string");
160
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
161
$conf["skipableVarDefaultValue"]=array(null,null,null);
162
#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
163
#$conf["disallowAllSkipableVarIsEmpty"]="";
164
#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
165
#$conf["disallowAllSkipableVarIsEmptyArray"]="";
166
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
167
#$conf["arrayCountEqualCheck"][]=array("rootPath","name","mode","ip","dbAcct","dbPass","dbName","dbAddr","dbPort","headless");
168
#參考資料:
169
#array_keys=>http://php.net/manual/en/function.array-keys.php
170
#備註:
171
#無.
172
$checkArguments=variableCheck::checkArguments($conf);
173
unset($conf);
174
 
175
#如果執行失敗
176
if($checkArguments["status"]==="false"){
177
 
178
	#debug
179
	var_dump($checkArguments);
180
 
181
	#異常結束執行
182
	exit(1);
183
 
184
	}#if end
185
 
186
#初始化 $checkListen 參數
187
$checkListen=array();
188
 
189
#如果有設置參數
190
if(count($parseArgu["content"])>0){
191
 
192
	#如果有 ipAndPort 參數
193
	if(isset($parseArgu["content"]["ipAndPort"])){
226 liveuser 194
 
3 liveuser 195
		#針對每個條件
196
		foreach($parseArgu["content"]["ipAndPort"] as $ipAndPort){
226 liveuser 197
 
3 liveuser 198
			#增加參數
199
			$checkListen[]=array("ip"=>$ipAndPort."/tcp");
226 liveuser 200
 
3 liveuser 201
			}#foreach end
226 liveuser 202
 
3 liveuser 203
		}#if end
226 liveuser 204
 
3 liveuser 205
	#如果有 reportOnly 參數
206
	if(isset($parseArgu["content"]["reportOnly"])){
226 liveuser 207
 
3 liveuser 208
		#針對每個條件
209
		foreach($parseArgu["content"]["reportOnly"] as $reportOnly){
226 liveuser 210
 
3 liveuser 211
			#設置 reportOnly 參數要啟用
212
			$reportOnly="true";
226 liveuser 213
 
3 liveuser 214
			}#foreach end
226 liveuser 215
 
3 liveuser 216
		}#if end
226 liveuser 217
 
3 liveuser 218
	#如果有 config 參數
219
	if(isset($parseArgu["content"]["config"])){
226 liveuser 220
 
3 liveuser 221
		#針對每個條件
222
		foreach($parseArgu["content"]["config"] as $config){
226 liveuser 223
 
3 liveuser 224
			#取得設定檔內容
225
			#函式說明:
226
			#解析PHP檔案裡面的變數.
227
			#回傳結果:
228
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
229
			#$result["error"],錯誤訊息.
230
			#$result["function"],當前執行的函數名稱.
226 liveuser 231
			#$result["argu"],所使用的參數.
3 liveuser 232
			#$result["content"],找到的變數內容陣列.
233
			#$result["content"]["value"],依找到變數順序的數值.
234
			#$result["content"]["struc"],依找到變數順序的階層結構.
235
			#$result["content"]["direct"],變數名稱對應的數值內容.
236
			#必填參數:
237
			#$conf["file"],字串,檔案的路徑與名稱.
238
			$conf["file"]=$config;
239
			#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
240
			$conf["fileArgu"]=__FILE__;
241
			#$conf["varName"],字串陣列,要搜尋的變數名稱,例如要搜尋變數$email則輸入"email".
242
			$conf["varName"]=array("ipAndPort","reportOnly");
243
			#可省略參數:
244
			#$conf["web"],是要取得網路上的檔案則為"true";反之則為"false",預設為"false".
245
			$conf["web"]="false";
246
			#參考資料:
247
			#https://www.php.net/manual/en/function.parse-str.php
248
			#備註:
249
			#無.
250
			$parseVaraiableInPHPfile=fileAccess::parseVaraiableInPHPfile($conf);
251
			unset($conf);
226 liveuser 252
 
3 liveuser 253
			#如果失敗
254
			if($parseVaraiableInPHPfile["status"]==="false"){
226 liveuser 255
 
3 liveuser 256
				#印出結果
257
				var_dump($parseVaraiableInPHPfile);
226 liveuser 258
 
3 liveuser 259
				#結束執行
260
				exit;
226 liveuser 261
 
3 liveuser 262
				}#if end
226 liveuser 263
 
3 liveuser 264
			#針對每個 ipAndPort 參數
265
			foreach($parseVaraiableInPHPfile["content"]["ipAndPort"] as $ipAndPort){
226 liveuser 266
 
3 liveuser 267
				#增加參數
268
				$checkListen[]=array("ip"=>$ipAndPort."/tcp");
226 liveuser 269
 
3 liveuser 270
				}#foreach end
226 liveuser 271
 
3 liveuser 272
			#針對每個 reportOnly 參數
273
			foreach($parseVaraiableInPHPfile["content"]["reportOnly"] as $reportOnly){
226 liveuser 274
 
3 liveuser 275
				#設置 reportOnly 參數要啟用
276
				$reportOnly="true";
226 liveuser 277
 
3 liveuser 278
				}#foreach end
226 liveuser 279
 
3 liveuser 280
			}#foreach end
226 liveuser 281
 
3 liveuser 282
		}#if end
283
 
284
	}#if end
285
 
286
#反之
287
else{
288
 
289
	#嘗試從 etc/qbpwcf/etc/sshd_config.php 取得設定
226 liveuser 290
 
3 liveuser 291
	#預設的設定檔案位置
292
	$defaultConfigAddr=pathinfo(__FILE__)["dirname"]."/../../etc/qbpwcf/sshd_config.php";
226 liveuser 293
 
3 liveuser 294
	#取得設定檔內容
295
	#函式說明:
296
	#解析PHP檔案裡面的變數.
297
	#回傳結果:
298
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
299
	#$result["error"],錯誤訊息.
300
	#$result["function"],當前執行的函數名稱.
226 liveuser 301
	#$result["argu"],所使用的參數.
3 liveuser 302
	#$result["content"],找到的變數內容陣列.
303
	#$result["content"]["value"],依找到變數順序的數值.
304
	#$result["content"]["struc"],依找到變數順序的階層結構.
305
	#$result["content"]["direct"],變數名稱對應的數值內容.
306
	#必填參數:
307
	#$conf["file"],字串,檔案的路徑與名稱.
308
	$conf["file"]=$defaultConfigAddr;
309
	#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
310
	$conf["fileArgu"]=__FILE__;
311
	#$conf["varName"],字串陣列,要搜尋的變數名稱,例如要搜尋變數$email則輸入"email".
312
	$conf["varName"]=array("ipAndPort","reportOnly","verbose");
313
	#可省略參數:
314
	#$conf["web"],是要取得網路上的檔案則為"true";反之則為"false",預設為"false".
315
	$conf["web"]="false";
316
	#參考資料:
317
	#https://www.php.net/manual/en/function.parse-str.php
318
	#備註:
319
	#無.
320
	$parseVaraiableInPHPfile=fileAccess::parseVaraiableInPHPfile($conf);
321
	unset($conf);
226 liveuser 322
 
3 liveuser 323
	#exit
324
	#var_dump(__LINE__,$parseVaraiableInPHPfile);
226 liveuser 325
 
3 liveuser 326
	#如果失敗
327
	if($parseVaraiableInPHPfile["status"]==="false"){
226 liveuser 328
 
3 liveuser 329
		#印出結果
330
		var_dump($parseVaraiableInPHPfile);
226 liveuser 331
 
3 liveuser 332
		#結束執行
333
		exit;
226 liveuser 334
 
3 liveuser 335
		}#if end
336
 
337
	#如果有指定 ipAndPort
338
	if(isset($parseVaraiableInPHPfile["content"]["ipAndPort"])){
339
 
340
		#針對每個 ipAndPort 參數
341
		foreach($parseVaraiableInPHPfile["content"]["ipAndPort"] as $ipAndPort){
226 liveuser 342
 
3 liveuser 343
			#增加參數
344
			$checkListen[]=array("ip"=>$ipAndPort."/tcp");
226 liveuser 345
 
3 liveuser 346
			}#foreach end
226 liveuser 347
 
3 liveuser 348
		}#if end
226 liveuser 349
 
3 liveuser 350
	#如果有指定 reportOnly
351
	if(isset($parseVaraiableInPHPfile["content"]["reportOnly"])){
226 liveuser 352
 
3 liveuser 353
		#針對每個 reportOnly 參數
354
		foreach($parseVaraiableInPHPfile["content"]["reportOnly"] as $reportOnly){
226 liveuser 355
 
3 liveuser 356
			#設置 reportOnly 參數要啟用
357
			$reportOnly="true";
226 liveuser 358
 
3 liveuser 359
			}#foreach end
226 liveuser 360
 
3 liveuser 361
		}#if end
226 liveuser 362
 
3 liveuser 363
	#如果有指定 verbose
364
	if(isset($parseVaraiableInPHPfile["content"]["verbose"])){
226 liveuser 365
 
3 liveuser 366
		#針對每個 verbose 參數
367
		foreach($parseVaraiableInPHPfile["content"]["verbose"] as $verbose){
226 liveuser 368
 
3 liveuser 369
			#設置 reportOnly 參數要啟用
370
			$verbose="true";
226 liveuser 371
 
3 liveuser 372
			}#foreach end
226 liveuser 373
 
3 liveuser 374
		}#if end
226 liveuser 375
 
3 liveuser 376
	}#else end
377
 
378
#函式說明:
379
#執行 systemd 程序來確保服務是運行中.
380
#回傳結果:
381
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
382
#$result["error"],錯誤訊息.
383
#$result["function"],當前執行的函式名稱.
384
#$result["argu"],所使用的參數.
385
#$result["content"],執行的結果.
386
#必填參數:
387
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.
388
$conf["fileArgu"]=__FILE__;
389
#$conf["name"],字串,服務名稱.
390
$conf["name"]="sshd";
391
#可省略參數:
392
#$conf["username"],字串,要用什麼使用者來執行,預設為root使用者
393
#$conf["username"]="";
394
#$conf["password"],字串,使用者的密碼,預設不使用.
395
#$conf["password"]="";
396
 
397
if(count($checkListen)>0){
398
 
399
	#$conf["checkListen"],陣列,用來判斷服務是否有正確啟動的條件,一個元素代表,其中要有一個socket info符合之,若元素的key為"ip",則其數值為[ipv4/ipv6]:port:protocol(tcp/udp);若元素的key為"addr",則其數值為unix domain socket 的位置與名稱;若元素的key為"name",則其數值為程序名稱.
400
	$conf["checkListen"]=$checkListen;
401
 
402
	}#if end
403
 
404
#$conf["interval"],字串,檢查沒問題後,要多少秒後再檢查一次,預設為30秒,亦即"30".
405
#$conf["interval"]="30";
406
 
407
#如果有 reportOnly 參數
408
if(isset($reportOnly)){
409
 
410
	#$conf["reportOnly"],字串,"true"代表不「啟動/重新啟動」服務,只是輸出訊息表示服務有正常或需要處理;預設為"false"代表直接「啟動/重新啟動」.
411
	$conf["reportOnly"]=$reportOnly;
412
 
413
	}#if end
414
 
415
#如果有 verbose 參數
416
if(isset($verbose)){
417
 
418
	#$conf["verbose"],字串,預設為"false",若為"true"則會印出過程訊息.
419
	$conf["verbose"]=$verbose;
420
 
421
	}#if end
422
 
423
#參考資料:
424
#無.
425
#備註:
426
#無.
427
$keepServiceUp=cmd::keepServiceUp($conf);
428
unset($conf);
429
 
192 liveuser 430
#如果執行異常
431
if($keepServiceUp["status"]==="false"){
3 liveuser 432
 
192 liveuser 433
	#debug
434
	var_dump($keepServiceUp);
435
 
436
	#結束執行,回傳1,代表異常.
437
	exit(1);
438
 
439
	}#if end
440
 
441
#結束執行,回傳0,代表正常結束執行.
442
exit();
443
 
3 liveuser 444
?>