Subversion Repositories qbpwcf-lib(archive)

Rev

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

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