Subversion Repositories qbpwcf-lib(archive)

Rev

Rev 923 | 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.
621 liveuser 6
    Copyright (C) 2015~2024 Min-Jhin,Chen
464 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
/*
1 liveuser 26
封鎖來自http、https、smtp、named的惡意ip. 
27
*/
28
 
456 liveuser 29
/*
30
 
31
bug log:
32
 
705 liveuser 33
journalctl -a -e -f
34
Mar 09 13:40:20 qbpwcf.org postfix/smtpd[1058247]: connect from unknown[194.48.251.15]
35
Mar 09 13:40:21 qbpwcf.org postfix/smtpd[1058247]: disconnect from unknown[194.48.251.15] ehlo=1 auth=0/1 quit=1 commands=2/3
36
Mar 09 13:40:45 qbpwcf.org postfix/smtpd[1050779]: connect from unknown[194.48.251.15]
37
Mar 09 13:40:46 qbpwcf.org postfix/smtpd[1050779]: disconnect from unknown[194.48.251.15] ehlo=1 auth=0/1 quit=1 commands=2/3
38
Mar 09 13:41:07 qbpwcf.org postfix/smtpd[1058247]: connect from unknown[194.48.251.15]
39
Mar 09 13:41:08 qbpwcf.org postfix/smtpd[1058247]: disconnect from unknown[194.48.251.15] ehlo=1 auth=0/1 quit=1 commands=2/3
456 liveuser 40
 
41
*/
42
 
1 liveuser 43
#指派命名空間
44
namespace qbpwcf;
45
 
954 liveuser 46
#取得 lib path
47
exec("php -f ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/libexec/folderOfUsrLib.php"),$output,$status);
48
 
49
#如果執行失敗
50
if($status!==0){
51
 
52
	#debug
53
	var_dump(__LINE__,$output);
54
 
55
	#結束執行,回傳shell 1.
56
	exit(1);
57
 
58
	}#if end
59
 
60
#儲存lib path
61
$folderOfUsrLib=$output[0];
62
 
466 liveuser 63
#以該檔案的實際位置的 lib path 為 include path 首位
954 liveuser 64
$output=array();
65
exec("cd ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/../".$folderOfUsrLib."/qbpwcf").";pwd;",$output,$status);
66
 
67
#如果執行失敗
68
if($status!==0){
69
 
70
	#debug
71
	var_dump(__LINE__,$output);
72
 
73
	#結束執行,回傳shell 1.
74
	exit(1);
75
 
76
	}#if end
77
 
78
#設置 include path 
466 liveuser 79
set_include_path($output[0].PATH_SEPARATOR.get_include_path());
80
 
464 liveuser 81
#匯入外部套件
466 liveuser 82
include("allInOne.php");
1 liveuser 83
 
565 liveuser 84
#當前路徑
85
$pwd=$output[0];
86
 
87
#unix socket 的位置
954 liveuser 88
$unixSocket=$pwd."../../var/qbpwcf/".basename(__FILE__).".sock";
565 liveuser 89
 
570 liveuser 90
#提示unix domain socket位置
91
echo "unix domain socket used at ".$unixSocket;
92
 
565 liveuser 93
#初始化儲存上次各log的時間
94
$preTimeFloat=array();
95
 
1 liveuser 96
#初始化記錄 http log 最後變更的時間
565 liveuser 97
$preTimeFloat["http"]=0;
1 liveuser 98
 
99
#初始化記錄 https log 最後變更的時間
565 liveuser 100
$preTimeFloat["https"]=0;
1 liveuser 101
 
102
#初始化記錄 smtpd log 最後變更的時間
565 liveuser 103
$preTimeFloat["smtp"]=0;
1 liveuser 104
 
105
#初始化記錄 dovecot/imap log 最後變更的時間
565 liveuser 106
$preTimeFloat["imap"]=0;
1 liveuser 107
 
108
#初始化記錄 named log 最後變更的時間
565 liveuser 109
$preTimeFloat["named"]=0;
1 liveuser 110
 
111
#初始化不啟用debug模式
37 liveuser 112
$debug=false;
1 liveuser 113
 
565 liveuser 114
#預設暫存的白名單ip有效時間為1小時
115
$tmp_white_ip_valid_time=1;
116
 
117
#說明函式
118
function help(){
119
 
120
	#印出指令說明
121
	echo "Usage of ".basename(__FILE__).":".PHP_EOL; 
122
	echo "--debug [true/false] 代表是否要啟動 debug 模式".PHP_EOL;
123
	echo "--exclude [white list ip] 代表白名單IP,一次只能放一個IP.".PHP_EOL;
124
	echo "--tmp-white-ip-valid-time [white ip value time] 指定暫時白名單的有效小時".PHP_EOL;
125
	echo "--client [true/false] 代表是否為 client 端,而非 server 端".PHP_EOL;
126
	echo "--client-add-tmp-white-ip [while ip for while] client端指令,代表要增加的暫時白名單IP".PHP_EOL;
127
	echo "--client-update-tmp-white-ip-valid-time [white ip value time] client端指令,代表要更新暫時白名單的有效時間,單位為小時".PHP_EOL;
128
 
129
	#結束執行
130
	exit;
131
 
132
	}#function help end
133
 
84 liveuser 134
#函式說明:
135
#解析參數.
136
#回傳結果:
137
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
138
#$reuslt["error"],執行不正常結束的錯訊息陣列.
139
#$result["function"],當前執行的函式名稱.
140
#$result["content"],解析好的參數陣列.
141
#$result["content"][$key][$i],參數 $key 的 $i+1 個參數數值內容.
142
#$result["program"],字串,執行的程式名稱.
143
#必填參數:
565 liveuser 144
#無.
84 liveuser 145
#可省略參數:
565 liveuser 146
#$conf["helpFunc"],如果解析的參數不成對,則要執行的函式名稱.
147
$conf["helpFunc"]="help";
148
#參考資料:
149
#無.
84 liveuser 150
#備註:
151
#僅能在命令列底下執行.
152
#以後可將參數 --a--b 的名稱與後面的數值 $value 存成 $result["a"]["b"][$i]=$value .
105 liveuser 153
$parseArgu=cmd::parseArgu($conf);
154
unset($conf);
1 liveuser 155
 
565 liveuser 156
#如果執行失敗
157
if($parseArgu["status"]==="false"){
158
 
159
	#設置執行失敗
160
	$result["status"]="false";
161
 
162
	#設置錯誤訊息
163
	$result["error"]=$parseArgu;
164
 
165
	#印出結果
166
	var_dump($result);
167
 
168
	#結束執行
169
	exit(1);
170
 
171
	}#if end
172
 
173
#檢查參數
174
#函式說明:
175
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
176
#回傳結果:
177
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
178
#$result["error"],執行不正常結束的錯訊息陣列.
179
#$result["simpleError"],簡單表示的錯誤訊息.
180
#$result["function"],當前執行的函式名稱.
181
#$result["argu"],設置給予的參數.
182
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
183
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
184
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
185
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
186
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
187
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
188
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
189
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
190
#必填參數:
191
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
192
$conf["varInput"]=&$parseArgu["content"];
193
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
194
$conf["referenceVarKey"]="variableCheck::checkArguments";
195
#可省略參數:
196
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
197
#$conf["mustBeFilledVariableName"]=array();
198
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
199
#$conf["mustBeFilledVariableType"]=array();
200
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
201
#$conf["canBeEmptyString"]="false";
202
#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
203
#$conf["canNotBeEmpty"]=array();
204
#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
205
#$conf["canBeEmpty"]=array();
206
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
207
$conf["skipableVariableCanNotBeEmpty"]=array("debug","exclude","tmp-white-ip-valid-time","client","tmp-white-ip-valid-time","client-add-tmp-white-ip","client-update-tmp-white-ip-valid-time");
208
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
209
$conf["skipableVariableName"]=array("debug","exclude","tmp-white-ip-valid-time","client","tmp-white-ip-valid-time","client-add-tmp-white-ip","client-update-tmp-white-ip-valid-time");
210
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
211
$conf["skipableVariableType"]=array("array","array","array","array","array","array","array");
212
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
213
$conf["skipableVarDefaultValue"]=array(array("false"),null,array("1"),null,null,null,null);
214
#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
215
$conf["disallowAllSkipableVarIsEmpty"]="false";
216
#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
217
$conf["disallowAllSkipableVarIsEmptyArray"]="false";
218
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
219
#$conf["arrayCountEqualCheck"][]=array();
220
#參考資料:
221
#array_keys=>http://php.net/manual/en/function.array-keys.php
222
#備註:
223
#無.
224
$checkArguments=variableCheck::checkArguments($conf);
225
unset($conf);
226
 
227
#如果檢查參數失敗
228
if($checkArguments["status"]==="false"){
229
 
230
	#設置執行失敗
231
	$result["status"]="false";
232
 
233
	#設置錯誤訊息
234
	$result["error"]=$checkArguments;
235
 
236
	#印出結果
237
	var_dump($result);
238
 
239
	#結束執行,跟回傳錯誤代碼.
240
	exit(1);
241
 
242
	}#if end
243
 
244
#如果檢查參數不通過
245
if($checkArguments["passed"]==="false"){
246
 
247
	#設置執行失敗
248
	$result["status"]="false";
249
 
250
	#設置錯誤訊息
251
	$result["error"]=$checkArguments;
252
 
253
	#印出結果
254
	var_dump($result);
255
 
256
	#結束執行,跟回傳錯誤代碼.
257
	exit(1);
258
 
259
	}#if end
260
 
84 liveuser 261
#如果有debug參數
262
if(isset($parseArgu["content"]["debug"])){
1 liveuser 263
 
84 liveuser 264
	#如果第一個參數為 "--debug true"
265
	if($parseArgu["content"]["debug"][0]==="true"){
1 liveuser 266
 
267
		#設置debug模式
268
		$debug=true;
269
 
270
		#comment
271
		echo "debug mode".PHP_EOL;
272
 
273
		}#if end	
274
 
275
	}#if end
565 liveuser 276
 
277
#如果有client參數
278
if(isset($parseArgu["content"]["client"])){
279
 
280
	#預設沒有id
281
	$clientId="";
282
 
283
	#如果第一個參數為 "--client true"
284
	if($parseArgu["content"]["client"][0]==="true"){
285
 
286
		#函式說明:
287
		#連線到 unixDomainSockServer 提供的 unix domain socket.
288
		#回傳結果:
289
		#$result["status"],"true"代表執行正常;"false"代表執行不正常.
290
		#$result["error"],錯誤訊息陣列.
291
		#$result["function"],當前執行的函式名稱.
292
		#$result["content"],取得的回應.
293
		#必填參數:
294
		#$conf["sock"],字串,要連線的unix domain socket.
295
		$conf["sock"]=$unixSocket;
296
		#可省略參數:
297
		#$conf["id"],字串,取得的id,若無此值,則會得到新的數值.
298
		#$conf["id"]="";
299
		#$conf["cmd"],字串,要執行的指令,當$conf["id"]參數合法時,才會執行.
300
		#$conf["cmd"]="";
301
		#$conf["param"],參數陣列.
302
		#$conf["param"]=array();
303
		#$conf["escaped"],字串,param參數是否已經escaped了,預設為"false",反之為"true".
304
		#$conf["escaped"]="true";
305
		#$conf["custom"],陣列,要客制化傳輸的內容,會覆蓋以上除了$conf["id"]以外的可省略參數.
306
		#$conf["custom"]=array();
307
		#參考資料:
308
		#http://php.net/manual/en/function.stream-socket-client.php
309
		#http://php.net/manual/en/function.stream-get-contents.php
310
		#備註:
311
		#無.
312
		$unixDomainSockClient=sock::unixDomainSockClient($conf);
313
		unset($conf);
314
 
315
		#如果執行失敗
316
		if($unixDomainSockClient["status"]==="false"){
317
 
318
			#設置執行失敗
319
			$result["status"]="false";
320
 
321
			#設置錯誤訊息
322
			$result["error"]=$unixDomainSockClient;
323
 
324
			#印出結果
325
			var_dump($result);
326
 
327
			#結束執行,跟回傳錯誤代碼.
328
			exit(1);
329
 
330
			}#if end
331
 
332
		#取得會應
333
		$res=$unixDomainSockClient["content"];
334
 
335
		#解析回應
336
		$res=(array)json_decode(trim($res));
337
 
570 liveuser 338
		#debug
339
		#var_dump($res);
340
 
565 liveuser 341
		#如果沒有給予 id
342
		if(!isset($res["id"])){
343
 
344
			#設置執行失敗
345
			$result["status"]="false";
346
 
347
			#設置錯誤訊息
348
			$result["error"][]="id not found";
349
 
350
			#設置錯誤訊息
351
			$result["error"][]=$res;
352
 
353
			#印出結果
354
			var_dump($result);
355
 
356
			#結束執行,跟回傳錯誤代碼.
357
			exit(1);
358
 
359
			}#if end
360
 
361
		#取得用戶端的id	
362
		$clientId=$res["id"];	
570 liveuser 363
 
565 liveuser 364
		}#if end
365
 
366
	#如果有要增加暫時的白名單
367
	if(isset($parseArgu["content"]["client-add-tmp-white-ip"])){
84 liveuser 368
 
565 liveuser 369
		#針對每個要增加暫時的白名單
370
		foreach($parseArgu["content"]["client-add-tmp-white-ip"] as $tmpWhilteIp){
371
 
372
			#要求 server 端增加暫時白名單 - start
373
 
374
			#產生要傳遞的陣列
375
			$request=array();
376
 
377
			#設置要新增暫時白名單的ip
378
			$request["add-tmp-white-ip"]=$tmpWhilteIp;
379
 
380
			#函式說明:
381
			#連線到 unixDomainSockServer 提供的 unix domain socket.
382
			#回傳結果:
383
			#$result["status"],"true"代表執行正常;"false"代表執行不正常.
384
			#$result["error"],錯誤訊息陣列.
385
			#$result["function"],當前執行的函式名稱.
386
			#$result["content"],取得的回應.
387
			#必填參數:
388
			#$conf["sock"],字串,要連線的unix domain socket.
389
			$conf["sock"]=$unixSocket;
390
			#可省略參數:
391
			#$conf["id"],字串,取得的id,若無此值,則會得到新的數值.
392
			$conf["id"]=$clientId;
393
			#$conf["cmd"],字串,要執行的指令,當$conf["id"]參數合法時,才會執行.
394
			#$conf["cmd"]="";
395
			#$conf["param"],參數陣列.
396
			#$conf["param"]=array();
397
			#$conf["escaped"],字串,param參數是否已經escaped了,預設為"false",反之為"true".
398
			#$conf["escaped"]="true";
399
			#$conf["custom"],陣列,要客制化傳輸的內容,會覆蓋以上除了$conf["id"]以外的可省略參數.
400
			$conf["custom"]=$request;
401
			#參考資料:
402
			#http://php.net/manual/en/function.stream-socket-client.php
403
			#http://php.net/manual/en/function.stream-get-contents.php
404
			#備註:
405
			#無.
406
			$unixDomainSockClient=sock::unixDomainSockClient($conf);
407
			unset($conf);
408
 
570 liveuser 409
			#debug
410
			#var_dump($unixDomainSockClient);
411
 
565 liveuser 412
			#如果執行失敗
413
			if($unixDomainSockClient["status"]==="false"){
414
 
415
				#設置執行失敗
416
				$result["status"]="false";
417
 
418
				#設置錯誤訊息
419
				$result["error"]=$unixDomainSockClient;
420
 
421
				#印出結果
422
				var_dump($result);
423
 
424
				#結束執行,跟回傳錯誤代碼.
425
				exit(1);
426
 
427
				}#if end
428
 
429
			#取得回應
430
			$res=$unixDomainSockClient["content"];
431
 
432
			#解析回應
433
			$res=(array)json_decode(trim($res));
434
 
435
			#要求 server 端增加暫時白名單 - end	
436
 
437
			}#foreach end
438
 
439
		}#if end
440
 
441
	#如果有要更新暫存白名單的有效時間
570 liveuser 442
	if(isset($parseArgu["content"]["client-update-tmp-white-ip-valid-time"])){
565 liveuser 443
 
444
		#要求 server 端更新暫時白名單的期限 - start
445
 
446
		#產生要傳遞的陣列
447
		$request=array();
448
 
570 liveuser 449
		#設置要更新暫時白名單ip的有效時間
572 liveuser 450
		$request["update-tmp-white-ip-valid-time"]=$parseArgu["content"]["client-update-tmp-white-ip-valid-time"][0];
565 liveuser 451
 
452
		#函式說明:
453
		#連線到 unixDomainSockServer 提供的 unix domain socket.
454
		#回傳結果:
455
		#$result["status"],"true"代表執行正常;"false"代表執行不正常.
456
		#$result["error"],錯誤訊息陣列.
457
		#$result["function"],當前執行的函式名稱.
458
		#$result["content"],取得的回應.
459
		#必填參數:
460
		#$conf["sock"],字串,要連線的unix domain socket.
461
		$conf["sock"]=$unixSocket;
462
		#可省略參數:
463
		#$conf["id"],字串,取得的id,若無此值,則會得到新的數值.
464
		$conf["id"]=$clientId;
465
		#$conf["cmd"],字串,要執行的指令,當$conf["id"]參數合法時,才會執行.
466
		#$conf["cmd"]="";
467
		#$conf["param"],參數陣列.
468
		#$conf["param"]=array();
469
		#$conf["escaped"],字串,param參數是否已經escaped了,預設為"false",反之為"true".
470
		#$conf["escaped"]="true";
471
		#$conf["custom"],陣列,要客制化傳輸的內容,會覆蓋以上除了$conf["id"]以外的可省略參數.
472
		$conf["custom"]=$request;
473
		#參考資料:
474
		#http://php.net/manual/en/function.stream-socket-client.php
475
		#http://php.net/manual/en/function.stream-get-contents.php
476
		#備註:
477
		#無.
478
		$unixDomainSockClient=sock::unixDomainSockClient($conf);
479
		unset($conf);
480
 
481
		#如果執行失敗
482
		if($unixDomainSockClient["status"]==="false"){
483
 
484
			#設置執行失敗
485
			$result["status"]="false";
486
 
487
			#設置錯誤訊息
488
			$result["error"]=$unixDomainSockClient;
489
 
490
			#印出結果
491
			var_dump($result);
492
 
493
			#結束執行,跟回傳錯誤代碼.
494
			exit(1);
495
 
496
			}#if end
497
 
498
		#取得回應
499
		$res=$unixDomainSockClient["content"];
500
 
501
		#解析回應
502
		$res=(array)json_decode(trim($res));
503
 
504
		#要求 server 端更新暫時白名單的期限 - end
505
 
506
		}#if end
507
 
508
	#client端程式結束,結束執行
509
	exit;
510
 
511
	}#if end
512
 
93 liveuser 513
#取得自己對外的ip
514
#函式說明:
515
#依據提供查詢IP服務的網站取得伺服器對外的IP.
516
#回傳結果:
517
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
518
#$result["function"],當前執行的函數名稱.
519
#$result["error"],錯誤訊息陣列.
520
#$result["content"],伺服端對外的IP.
521
#必填參數:
522
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
523
$conf["fileArgu"]=__FILE__;
524
#可省略參數:
525
#無.
526
#備註:
527
#需要有網站支援此服務.
528
$getServerRealIP=csInformation::getServerRealIP($conf);
529
unset($conf);	
530
 
531
#如果執行失敗
532
if($getServerRealIP["status"]==="false"){
533
 
534
	#設置執行失敗
535
	$result["status"]="false";
536
 
537
	#設置錯誤訊息
538
	$result["error"]=$getServerRealIP;
539
 
540
	#印出結果
541
	var_dump($result);
84 liveuser 542
 
565 liveuser 543
	#結束執行,跟回傳錯誤代碼.
544
	exit(1);
93 liveuser 545
 
546
	}#if end
547
 
548
#預設的白名單為自己對外的IP
549
$excludeIp=array($getServerRealIP["content"][0]);	
550
 
551
#提示內建白名單
552
echo "內建白名單:".$excludeIp[0].PHP_EOL;
553
 
84 liveuser 554
#如果有 exclude 參數
555
if(isset($parseArgu["content"]["exclude"])){
1 liveuser 556
 
93 liveuser 557
	#針對每個白名單 IP
558
	foreach($parseArgu["content"]["exclude"] as $whiteIp){
559
 
560
		#提示內建白名單
561
		echo "附加白名單:".$whiteIp.PHP_EOL;
562
 
563
		#取得每個指定要排除的ip
564
		$excludeIp[]=$whiteIp;
84 liveuser 565
 
93 liveuser 566
		}#foreach end
567
 
84 liveuser 568
	}#if end
569
 
565 liveuser 570
#暫時的白名單
571
$excludeIpsTmp=array();
1 liveuser 572
 
565 liveuser 573
#函式說明:
574
#建立 unix domain socket server, 僅提供具備檔案存取權限的用戶使用,預設提供可以下達任何指令的功能.
575
#回傳結果:
576
#$result["status"],"true"代表執行正常;"false"代表執行不正常.
577
#$result["error"],錯誤訊息陣列.
578
#$result["function"],當前執行的函式名稱.
579
#必填參數:
580
#$conf["sock"],字串,socket檔案要放在哪邊,名稱為何.
581
$conf["sock"]=$unixSocket;
582
#$conf["fileArgu"],字串,變數__FILE__的內容.
583
$conf["fileArgu"]=__FILE__;
584
#可省略參數:
585
#$conf["changeOwner"],字串,要將socket檔案的擁有着權限進行修改."user.group"代表擁有者帳號為user,群組為group.
586
#$conf["changeOwner"]="";
570 liveuser 587
#$conf["changePermission"],字串,要將socket檔案的權限設為多少.ex: 0666(所有帳戶都有存取的權限) 或 0660(僅有擁有者與群組帳戶有存取的權限) 或 0600(只有擁有者有權限執行).
588
$conf["changePermission"]="0666";
565 liveuser 589
#$conf["sessionTimeout"],字串,當連線結束後於下一次連線間隔多久就算session timeout,server端會將記錄移除,client端需要重新拿取id,預設為300秒.
590
#$conf["sessionTimeout"]="300";
591
#$conf["addOnProcessFunc"],字串陣列,增加用於處理 json request 的函式名稱,給予的參數為array("request"=>收到的json訊息,"sock"=>用戶的socket).
592
$conf["addOnProcessFunc"]=array("\qbpwcf\addOnProcessFunc");
593
#$conf["funcToRunWhenIdle"],字串陣列,當沒有事件產生時,要執行的函式名稱,給予參數為array("client"=>所有用戶).
594
$conf["funcToRunWhenIdle"]=array("\qbpwcf\\funcToRunWhenIdle");
595
#$conf["infoToFunction"],陣列,需要增加給addOnProcessFunc跟funcToRunWhenIdle函式的資訊,在函式中其參數的info鍵值.
570 liveuser 596
$conf["infoToFunction"]=array("debug"=>&$debug,"preTimeFloat"=>&$preTimeFloat,"excludeIpsTmp"=>&$excludeIpsTmp,"excludeIp"=>&$excludeIp,"tmp-white-ip-valid-time"=>&$tmp_white_ip_valid_time);
565 liveuser 597
#參考資料:
598
#http://php.net/manual/en/function.stream-socket-server.php
599
#備註:
600
#無.
601
$unixDomainSockServer=sock::unixDomainSockServer($conf);
602
unset($conf);
603
 
570 liveuser 604
#當執行失敗
605
if($unixDomainSockServer["status"]==="false"){
606
 
607
	#設置執行失敗
608
	$result["status"]="false";
609
 
610
	#設置錯誤訊息
611
	$result["error"]=$unixDomainSockServer;
612
 
613
	#印出結果
614
	var_dump($result);
615
 
616
	#結束執行,跟回傳錯誤代碼.
617
	exit(1);
618
 
619
	}#if end
620
 
565 liveuser 621
#收到訊息要怎麼處理的函式
622
function addOnProcessFunc($params){
623
 
720 liveuser 624
	#debug
625
	#var_dump(__LINE__,$params["request"]);
565 liveuser 626
 
720 liveuser 627
	#取得收到的訊息
628
	$receivedArray=$params["request"];
629
 
565 liveuser 630
	#取得用戶的 socket
570 liveuser 631
	$clientSock=$params["socket"];
632
 
700 liveuser 633
	#預設不 debug
634
	$debug=false;
635
 
636
	#如果有debug鍵數值.
637
	if(isset($params["info"]["debug"])){
638
 
639
		#設置之
640
		$debug=$params["info"]["debug"];
641
 
642
		}#if end
643
 
699 liveuser 644
	#debug mode
645
	if($debug){
646
 
647
		#debug
648
		var_dump(__LINE__,$params);
565 liveuser 649
 
699 liveuser 650
		}#if end
701 liveuser 651
 
565 liveuser 652
	#如果解析 json 失敗
653
	if(json_last_error()!==JSON_ERROR_NONE){
654
 
655
		#初始化結果
656
		$result=array();
657
 
658
		#設置執行失敗
659
		$result["status"]="false";
660
 
661
		#記錄錯誤訊息
662
		$result["error"]=json_last_error_msg();
663
 
664
		#回覆錯誤訊息
665
		fwrite($clientSock,json_encode($result).PHP_EOL);
666
 
667
		#初始化要回傳的訊息
668
		$result=array();
669
 
670
		#設置不繼續執行
671
		$result["continue"]="false";
672
 
673
		#回傳結果
674
		return $result;
675
 
676
		}#if end
677
 
699 liveuser 678
	#debug mode
679
	if($debug){
680
 
681
		#debug
682
		var_dump(__LINE__,$receivedArray);
570 liveuser 683
 
699 liveuser 684
		}#if end
685
 
565 liveuser 686
	#如果要新增暫時的ip白名單
687
	if(isset($receivedArray["add-tmp-white-ip"])){
688
 
689
		#函式說明:
690
		#將多個多維陣列串聯起來,key從0開始排序.
691
		#回傳的結果:
692
		#$result["status"],"true"表執行正常,"false"代表執行不正常.
693
		#$result["error"],錯誤訊息陣列.
694
		#$result["function"],當前執行的函數.
695
		#$result["content"],合併好的一維陣列.
696
		#必填參數
697
		#$conf["inputArray"],陣列,要合併的一維陣列變數,例如:=array($array1,$array2);
570 liveuser 698
		$conf["inputArray"]=array($params["info"]["excludeIpsTmp"],array(array("ip"=>$receivedArray["add-tmp-white-ip"],"unixtime"=>time())));
565 liveuser 699
		#可省略參數:
700
		#$conf["allowRepeat"],字串,預設為"false",不允許重複的結果;反之為"true".
701
		#$conf["allowRepeat"]="true";
702
		#$conf["equalKeyStruc"],字串陣列,若 allowRepeat 參數為 "false", 這該參數生效.該參數為用來判斷每個陣列的哪個鍵值路徑底下的數值相同時要進行取代,後者會取代前者.
703
		$conf["equalKeyStruc"]=array("ip");
704
		#參考資料:
705
		#無.
706
		#備註:
707
		#無.
708
		$mergeMultiDimensionArray=arrays::mergeMultiDimensionArray($conf);
709
		unset($conf);
710
 
699 liveuser 711
		#debug mode
712
		if($debug){
570 liveuser 713
 
699 liveuser 714
			#debug
715
			var_dump(__LINE__,$mergeMultiDimensionArray);
716
 
717
			}#if end
718
 
565 liveuser 719
		#如果運行失敗
720
		if($mergeMultiDimensionArray["status"]==="false"){
721
 
722
			#設置執行失敗
723
			$result["status"]="false";
724
 
725
			#設置錯誤訊息
726
			$result["error"]=$mergeMultiDimensionArray;
727
 
728
			#印出結果
729
			var_dump($result);
730
 
731
			#結束執行
732
			exit(1);
733
 
734
			}#if end
735
 
736
		#更新白名單
737
		$params["info"]["excludeIpsTmp"]=$mergeMultiDimensionArray["content"];
738
 
699 liveuser 739
		#debug mode
740
		if($debug){
741
 
742
			#debug
743
			var_dump(__LINE__,$params["info"]["excludeIpsTmp"]);
572 liveuser 744
 
699 liveuser 745
			}#if end
746
 
565 liveuser 747
		}#if end
570 liveuser 748
 
749
	#反之收到 update-tmp-white-ip-valid-time
572 liveuser 750
	if(isset($receivedArray["update-tmp-white-ip-valid-time"])){
570 liveuser 751
 
752
		#更新白名單有效時間
753
		$params["info"]["tmp-white-ip-valid-time"]=$receivedArray["update-tmp-white-ip-valid-time"];
572 liveuser 754
 
699 liveuser 755
		#debug mode
756
		if($debug){
757
 
758
			#debug
759
			var_dump(__LINE__,$params["info"]["tmp-white-ip-valid-time"]);
570 liveuser 760
 
699 liveuser 761
			}#if end
762
 
570 liveuser 763
		}#if end
764
 
765
	#初始化要回傳的結果
766
	$result=array();	
767
 
768
	#設置執行正常
769
	$result["status"]="true";	
770
 
771
	#設置可以繼續執行後面的函式
772
	$result["continue"]="true";
773
 
774
	#回傳結果
775
	return $result;
565 liveuser 776
 
777
	}#function addOnProcessFunc end
778
 
779
#沒事時要重複執行的函式
780
function funcToRunWhenIdle($params){
781
 
782
	#預設不 debug
783
	$debug=false;
784
 
785
	#如果有debug鍵數值.
786
	if(isset($params["info"]["debug"])){
787
 
788
		#設置之
789
		$debug=$params["info"]["debug"];
790
 
791
		}#if end
792
 
1 liveuser 793
	#debug mode
794
	if($debug){
795
 
796
		#comment
565 liveuser 797
		echo "new round".PHP_EOL;
1 liveuser 798
 
799
		}#if end
800
 
565 liveuser 801
	#debug mode
802
	if($debug){
803
 
804
		#comment
805
		echo "\$preHttpTimeFloat:".$params["info"]["preTimeFloat"]["http"].PHP_EOL;
806
		echo "\$preHttpsTimeFloat:".$params["info"]["preTimeFloat"]["https"].PHP_EOL;
807
		echo "\$preSmtpTimeFloat:".$params["info"]["preTimeFloat"]["smtp"].PHP_EOL;
808
		echo "\$preImapTimeFloat:".$params["info"]["preTimeFloat"]["imap"].PHP_EOL;
809
		echo "\$preNamedTimeFloat:".$params["info"]["preTimeFloat"]["named"].PHP_EOL;
810
 
811
		}#if end
812
 
736 liveuser 813
	#函式說明:
814
	#檢查多個檔案與資料夾是否存在.
815
	#回傳的結果:
816
	#$result["status"],執行正常與否,"true"代表正常,"false"代表不正常.
1 liveuser 817
	#$result["error"],錯誤訊息陣列.
736 liveuser 818
	#$resutl["function"],當前執行的涵式名稱.
819
	#$result["argu"],使用的參數.
820
	#$result["allExist"],所有檔案皆存在的識別,"true"代表皆存在,"false"代表沒有全部都存在.
821
	#$result["varName"][$i],爲第$i個資料夾或檔案的路徑與名稱。
822
	#$result["varNameFullPath"][$i],爲第$i個資料夾或檔案的完整檔案系統路徑與名稱,如果不存在則代表路徑是網址.
823
	#$result["varNameWebPath"][$i],為第$i個資料夾或檔案的網址,若"web"參數為"true",才會有該內容.
824
	#$result["varExist"][$i],爲第$i個資料夾或檔案是否存在,"true"代表存在,"false"代表不存在。
1 liveuser 825
	#必填參數:
736 liveuser 826
	#$conf["fileArray"],陣列字串,要檢查是否存在的檔案有哪些,須爲一維陣列數值。
827
	$conf["fileArray"]=array("/var/log/httpd/access_log");
828
	#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
1 liveuser 829
	$conf["fileArgu"]=__FILE__;
736 liveuser 830
	#可省略參數:
831
	#$conf["disableWebSearch"],"字串",是否取消「當檔案找不到時,改用catchWebContent類別的wget函數來檢查檔案是否存在於網路上」的功能,"false"不取消,若要取消該功能請設為"true",若抓到的內容為空字串則會視為檔案不存在,預設為"true".
832
	#$conf["disableWebSearch"]="false";
833
	#$conf["userDir"],字串,網頁是否置放於家目錄底下,"true"為是,"false"為不是,預設為"true".
834
	$conf["userDir"]="false";
835
	#$conf["web"],字串,檔案是放在web就是"true",反之為檔案系統"false",預設為"true".
1 liveuser 836
	$conf["web"]="false";
837
	#參考資料:
736 liveuser 838
	#http://php.net/manual/en/function.file-exists.php
839
	#http://php.net/manual/en/control-structures.foreach.php
840
	#備註:
841
	#函數file_exists檢查的路徑為檔案系統的路徑
842
	#$result["varName"][$i]結果未實作
843
	$checkMultiFileExist=fileAccess::checkMultiFileExist($conf);
1 liveuser 844
	unset($conf);
736 liveuser 845
 
1 liveuser 846
	#如果運行失敗
736 liveuser 847
	if($checkMultiFileExist["status"]==="false"){
1 liveuser 848
 
849
		#設置執行失敗
850
		$result["status"]="false";
851
 
852
		#設置錯誤訊息
736 liveuser 853
		$result["error"]=$checkMultiFileExist;
1 liveuser 854
 
855
		#印出結果
736 liveuser 856
		var_dump($checkMultiFileExist);
1 liveuser 857
 
858
		#結束執行
565 liveuser 859
		exit(1);
1 liveuser 860
 
861
		}#if end
736 liveuser 862
 
863
	#初始化 http log 的最後變更時間
864
	$httpTimeFloat=0;
865
 
866
	#如果存在 http log
867
	if($checkMultiFileExist["allExist"]==="true"){
868
 
869
		#函數說明:
870
		#依據取得檔案的擁有着資訊
871
		#回傳結果:
872
		#$result["status"],"true"爲建立成功,"false"爲建立失敗.
873
		#$result["error"],錯誤訊息陣列.
874
		#$result["function"],函數名稱. 
875
		#$result["content"],檔案資訊陣列.
876
		#$result["content"]["is_folder"],是否為目錄,"true"代表是,"false"代表不是.	
877
		#$result["content"]["ownerPerm"],檔案擁有者權限資訊.
878
		#$result["content"]["groupPerm"],檔案歸屬群組權限資訊.
879
		#$result["content"]["otherPerm"],檔案對於其他身份使用者的權限資訊.
880
		#$result["content"]["subElementCount"],目錄底下的檔案目錄數量.
881
		#$result["content"]["ownerName"],檔案擁有着資訊.
882
		#$result["content"]["groupName"],檔案所屬擁有着資訊.
883
		#$result["content"]["size"],檔案大小.
884
		#$result["content"]["modifyDate"],檔案變更年月日.
885
		#$result["content"]["modifyTime"],檔案變更時分秒.
886
		#$result["content"]["modifyTimeFloat"],檔案變更時間秒的float數值.
887
		#$result["content"]["timezone"],檔案變更時間的時區與UTC的差距.
888
		#必填參數:
889
		#$conf["fileArgu"],字串,當前檔案的位置亦即__FILE__的內容.
890
		$conf["fileArgu"]=__FILE__;
891
		#$conf["file"],字串,要查看擁有者資訊的檔案.
892
		$conf["file"]="/var/log/httpd/access_log";
893
		$conf["web"]="false";
894
		#參考資料:
895
		#fileowner=>http://php.net/manual/en/function.fileowner.php
896
		#posix_getpwuid=>http://php.net/manual/en/function.posix-getpwuid.php
897
		$httpFileInfo=fileAccess::fileInfo($conf);
898
		unset($conf);
1 liveuser 899
 
736 liveuser 900
		#如果運行失敗
901
		if($httpFileInfo["status"]==="false"){
1 liveuser 902
 
736 liveuser 903
			#設置執行失敗
904
			$result["status"]="false";
905
 
906
			#設置錯誤訊息
907
			$result["error"]=$httpFileInfo;
908
 
909
			#印出結果
910
			var_dump($result);
911
 
912
			#結束執行
913
			exit(1);
1 liveuser 914
 
736 liveuser 915
			}#if end
916
 
917
		#debug
918
		if($debug){
919
 
920
			#comment
921
			echo "accessed file /var/log/httpd/access_log".PHP_EOL;
922
 
923
			}#if end
924
 
925
		#記錄 http log 的最後變更時間
926
		$httpTimeFloat=$httpFileInfo["content"]["modifyTimeFloat"];
927
 
1 liveuser 928
		}#if end
736 liveuser 929
 
930
	#函式說明:
931
	#檢查多個檔案與資料夾是否存在.
932
	#回傳的結果:
933
	#$result["status"],執行正常與否,"true"代表正常,"false"代表不正常.
1 liveuser 934
	#$result["error"],錯誤訊息陣列.
736 liveuser 935
	#$resutl["function"],當前執行的涵式名稱.
936
	#$result["argu"],使用的參數.
937
	#$result["allExist"],所有檔案皆存在的識別,"true"代表皆存在,"false"代表沒有全部都存在.
938
	#$result["varName"][$i],爲第$i個資料夾或檔案的路徑與名稱。
939
	#$result["varNameFullPath"][$i],爲第$i個資料夾或檔案的完整檔案系統路徑與名稱,如果不存在則代表路徑是網址.
940
	#$result["varNameWebPath"][$i],為第$i個資料夾或檔案的網址,若"web"參數為"true",才會有該內容.
941
	#$result["varExist"][$i],爲第$i個資料夾或檔案是否存在,"true"代表存在,"false"代表不存在。
1 liveuser 942
	#必填參數:
736 liveuser 943
	#$conf["fileArray"],陣列字串,要檢查是否存在的檔案有哪些,須爲一維陣列數值。
944
	$conf["fileArray"]=array("/var/log/httpd/ssl_access_log");
945
	#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
1 liveuser 946
	$conf["fileArgu"]=__FILE__;
736 liveuser 947
	#可省略參數:
948
	#$conf["disableWebSearch"],"字串",是否取消「當檔案找不到時,改用catchWebContent類別的wget函數來檢查檔案是否存在於網路上」的功能,"false"不取消,若要取消該功能請設為"true",若抓到的內容為空字串則會視為檔案不存在,預設為"true".
949
	#$conf["disableWebSearch"]="false";
950
	#$conf["userDir"],字串,網頁是否置放於家目錄底下,"true"為是,"false"為不是,預設為"true".
951
	$conf["userDir"]="false";
952
	#$conf["web"],字串,檔案是放在web就是"true",反之為檔案系統"false",預設為"true".
1 liveuser 953
	$conf["web"]="false";
954
	#參考資料:
736 liveuser 955
	#http://php.net/manual/en/function.file-exists.php
956
	#http://php.net/manual/en/control-structures.foreach.php
957
	#備註:
958
	#函數file_exists檢查的路徑為檔案系統的路徑
959
	#$result["varName"][$i]結果未實作
960
	$checkMultiFileExist=fileAccess::checkMultiFileExist($conf);
1 liveuser 961
	unset($conf);
962
 
963
	#如果運行失敗
736 liveuser 964
	if($checkMultiFileExist["status"]==="false"){
1 liveuser 965
 
966
		#設置執行失敗
967
		$result["status"]="false";
968
 
969
		#設置錯誤訊息
736 liveuser 970
		$result["error"]=$checkMultiFileExist;
1 liveuser 971
 
972
		#印出結果
736 liveuser 973
		var_dump($checkMultiFileExist);
1 liveuser 974
 
975
		#結束執行
565 liveuser 976
		exit(1);
1 liveuser 977
 
978
		}#if end
736 liveuser 979
 
980
	#初始化 https log 的最後變更時間
981
	$httpsTimeFloat=0;	
982
 
983
	#如果存在 https log
984
	if($checkMultiFileExist["allExist"]==="true"){
985
 
986
		#函數說明:
987
		#依據取得檔案的擁有着資訊
988
		#回傳結果:
989
		#$result["status"],"true"爲建立成功,"false"爲建立失敗.
990
		#$result["error"],錯誤訊息陣列.
991
		#$result["function"],函數名稱. 
992
		#$result["content"],檔案資訊陣列.
993
		#$result["content"]["is_folder"],是否為目錄,"true"代表是,"false"代表不是.	
994
		#$result["content"]["ownerPerm"],檔案擁有者權限資訊.
995
		#$result["content"]["groupPerm"],檔案歸屬群組權限資訊.
996
		#$result["content"]["otherPerm"],檔案對於其他身份使用者的權限資訊.
997
		#$result["content"]["subElementCount"],目錄底下的檔案目錄數量.
998
		#$result["content"]["ownerName"],檔案擁有着資訊.
999
		#$result["content"]["groupName"],檔案所屬擁有着資訊.
1000
		#$result["content"]["size"],檔案大小.
1001
		#$result["content"]["modifyDate"],檔案變更年月日.
1002
		#$result["content"]["modifyTime"],檔案變更時分秒.
1003
		#$result["content"]["modifyTimeFloat"],檔案變更時間秒的float數值.
1004
		#$result["content"]["timezone"],檔案變更時間的時區與UTC的差距.
1005
		#必填參數:
1006
		#$conf["fileArgu"],字串,當前檔案的位置亦即__FILE__的內容.
1007
		$conf["fileArgu"]=__FILE__;
1008
		#$conf["file"],字串,要查看擁有者資訊的檔案.
1009
		$conf["file"]="/var/log/httpd/ssl_access_log";
1010
		$conf["web"]="false";
1011
		#參考資料:
1012
		#fileowner=>http://php.net/manual/en/function.fileowner.php
1013
		#posix_getpwuid=>http://php.net/manual/en/function.posix-getpwuid.php
1014
		$httpsFileInfo=fileAccess::fileInfo($conf);
1015
		unset($conf);
1 liveuser 1016
 
736 liveuser 1017
		#如果運行失敗
1018
		if($httpsFileInfo["status"]==="false"){
1 liveuser 1019
 
736 liveuser 1020
			#設置執行失敗
1021
			$result["status"]="false";
1022
 
1023
			#設置錯誤訊息
1024
			$result["error"]=$httpsFileInfo;
1025
 
1026
			#印出結果
1027
			var_dump($result);
1028
 
1029
			#結束執行
1030
			exit(1);
1 liveuser 1031
 
736 liveuser 1032
			}#if end
1033
 
1034
		#debug
1035
		if($debug){
1036
 
1037
			#comment
1038
			echo "accessed file /var/log/httpd/ssl_access_log".PHP_EOL;
1039
 
1040
			}#if end
1041
 
1042
		#記錄 https log 的最後變更時間
1043
		$httpsTimeFloat=$httpsFileInfo["content"]["modifyTimeFloat"];
1044
 
1 liveuser 1045
		}#if end
565 liveuser 1046
 
1 liveuser 1047
	#透過運行以下指令取得最後變更時間
1048
	#journalctl -a -e | grep postfix/smtpd | grep " connect from unknown" | tail -n 1
1049
	#涵式說明:
1050
	#呼叫shell執行系統命令,並取得回傳的內容.
1051
	#回傳的結果:
1052
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1053
	#$result["error"],錯誤訊息陣列.
1054
	#$result["function"],當前執行的函數名稱.
1055
	#$result["argu"],使用的參數.
1056
	#$result["cmd"],執行的指令內容.
1057
	#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
1058
	#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
1059
	#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
1060
	#$result["running"],是否還在執行.
1061
	#$result["pid"],pid.
1062
	#$result["statusCode"],執行結束後的代碼.
1063
	#必填的參數
1064
	#$conf["command"],字串,要執行的指令與.
1065
	$conf["external::callShell"]["command"]="journalctl";
1066
	#$conf["fileArgu"],字串,變數__FILE__的內容.
1067
	$conf["external::callShell"]["fileArgu"]=__FILE__;		
1068
	#可省略參數:
1069
	#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
33 liveuser 1070
	$conf["external::callShell"]["argu"]=array("-a","-e","|","grep","postfix/smtps/smtpd","|","grep"," connect from unknown","|","tail","-n","1");
1 liveuser 1071
	#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
1072
	#$conf["arguIsAddr"]=array();	
1073
	#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
1074
	#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
1075
	#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
1076
	#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
1077
	#$conf["enablePrintDescription"]="true";
1078
	#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
1079
	#$conf["printDescription"]="";
1080
	#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".
453 liveuser 1081
	$conf["external::callShell"]["escapeshellarg"]="true";
1 liveuser 1082
	#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
1083
	#$conf["username"]="";
1084
	#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
1085
	#$conf["password"]="";
1086
	#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
1087
	#$conf["useScript"]="";
1088
	#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
1089
	#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
1090
	#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
1091
	#$conf["inBackGround"]="";
1092
	#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
1093
	#$conf["getErr"]="false";
1094
	#備註:
1095
	#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
1096
	#參考資料:
1097
	#exec=>http://php.net/manual/en/function.exec.php
1098
	#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
1099
	#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
1100
	$callShell=external::callShell($conf["external::callShell"]);
1101
	unset($conf["external::callShell"]);
1102
 
1103
	#如果執行失敗
1104
	if($callShell["status"]==="false"){
1105
 
1106
		#設置執行失敗
1107
		$result["status"]="false";
1108
 
1109
		#設置錯誤訊息
1110
		$result["error"]=$callShell;
1111
 
1112
		#回傳結果
1113
		var_dump($result);
1114
 
1115
		#結束執行
565 liveuser 1116
		exit(1);
1 liveuser 1117
 
1118
		}#if end
1119
 
1120
	#debug
1121
	if($debug){
1122
 
1123
		#comment
1124
		echo "accessed file postfix/smtp log".PHP_EOL;
1125
 
1126
		}#if end
1127
 
1128
	#取得行數
1129
	$lineCount=count($callShell["output"]);
1130
 
1131
	#如果行數等於1
1132
	if($lineCount===1){
1133
 
1134
		#debug
1135
		if($debug){
1136
 
1137
			#最新一筆惡意IP
37 liveuser 1138
			#echo "最新一筆惡意IP:".PHP_EOL;
36 liveuser 1139
			#var_dump($callShell["output"]);
1 liveuser 1140
 
1141
			}#if end
1142
 
1143
		#Aug 09 07:12:09 www.qbpwcf.org postfix/smtpd[20829]: connect from unknown[185.234.219.62]
28 liveuser 1144
		#Apr 04 15:11:16 mail.qbpwcf.org postfix/smtps/smtpd[10145]: connect from unknown[212.70.149.72]
1 liveuser 1145
		$unProcesedStr=$callShell["output"];
1146
 
1147
		#分割字串
1148
		#涵式說明:
1149
		#將多個固定格式的字串分開,並回傳分開的結果
1150
		#回傳的參數:
1151
		#$result["status"],執行成功與否,若爲"true",代表執行成功,若爲"false"代表執失敗。
1152
		#$result["error"],錯誤訊息陣列.
1153
		#$result["function"],當前執行的函數名稱.
1154
		#$result["spiltString"][$i]["oriStr"],爲第i個字串的原始內容
1155
		#$result["spiltString"][$i]["dataArray"],爲第($i+1)個字串分割後的字串陣列
1156
		#$result["spiltString"][$i]["dataArray"][$j],爲第($i+1)的分割好的字串的第($j+1)段內容
1157
		#$result["spiltString"][$i]["dataCounts"],爲第($i+1)個字串分割後總共分成幾段
1158
		#必填的參數:
1159
		#$conf["stringIn"],字串陣列,要處理的字串陣列.
1160
		$conf["stringProcess::spiltMutiString"]["stringIn"]=$unProcesedStr;
1161
		#$conf["spiltSymbol"],字串,爲要以哪個符號作爲分割.
1162
		$conf["stringProcess::spiltMutiString"]["spiltSymbol"]=" ";
1163
		$spiltMutiString=stringProcess::spiltMutiString($conf["stringProcess::spiltMutiString"]);
1164
		unset($conf["stringProcess::spiltMutiString"]);
1165
 
1166
		#如果分割失敗
1167
		if($spiltMutiString["status"]==="false"){
1168
 
1169
			#設置執行失敗
1170
			$result["status"]="false";
1171
 
1172
			#設置錯誤訊息
1173
			$result["error"]=$spiltMutiString;
1174
 
1175
			#印出結果
1176
			var_dump($result);
1177
 
1178
			#停止執行
565 liveuser 1179
			exit(1);
1 liveuser 1180
 
1181
			}#if end
565 liveuser 1182
 
1 liveuser 1183
		#記錄 smtpd log 最後變更的時間
1184
		$smtpTimeFloat=$spiltMutiString["spiltString"][0]["dataArray"][2];
1185
 
565 liveuser 1186
		}#if end	
1 liveuser 1187
 
565 liveuser 1188
	#反之沒變動
1 liveuser 1189
	else{
1190
 
565 liveuser 1191
		#沿用上次時間
1192
		$smtpTimeFloat=$params["info"]["preTimeFloat"]["smtp"];
1 liveuser 1193
 
565 liveuser 1194
		}#else ebd
1195
 
1 liveuser 1196
	#透過運行以下指令取得最後變更時間
1197
	#journalctl -a -e | grep dovecot | grep " failed" | tail -n 1
1198
	#涵式說明:
1199
	#呼叫shell執行系統命令,並取得回傳的內容.
1200
	#回傳的結果:
1201
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1202
	#$result["error"],錯誤訊息陣列.
1203
	#$result["function"],當前執行的函數名稱.
1204
	#$result["argu"],使用的參數.
1205
	#$result["cmd"],執行的指令內容.
1206
	#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
1207
	#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
1208
	#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
1209
	#$result["running"],是否還在執行.
1210
	#$result["pid"],pid.
1211
	#$result["statusCode"],執行結束後的代碼.
1212
	#必填的參數
1213
	#$conf["command"],字串,要執行的指令與.
1214
	$conf["external::callShell"]["command"]="journalctl";
1215
	#$conf["fileArgu"],字串,變數__FILE__的內容.
1216
	$conf["external::callShell"]["fileArgu"]=__FILE__;		
1217
	#可省略參數:
1218
	#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
1219
	$conf["external::callShell"]["argu"]=array("-a","-e","|","grep","dovecot","|","grep"," failed","|","tail","-n","1");
1220
	#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
1221
	#$conf["arguIsAddr"]=array();	
1222
	#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
1223
	#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
1224
	#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
1225
	#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
1226
	#$conf["enablePrintDescription"]="true";
1227
	#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
1228
	#$conf["printDescription"]="";
1229
	#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".
453 liveuser 1230
	$conf["external::callShell"]["escapeshellarg"]="true";
1 liveuser 1231
	#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
1232
	#$conf["username"]="";
1233
	#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
1234
	#$conf["password"]="";
1235
	#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
1236
	#$conf["useScript"]="";
1237
	#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
1238
	#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
1239
	#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
1240
	#$conf["inBackGround"]="";
1241
	#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
1242
	#$conf["getErr"]="false";
1243
	#備註:
1244
	#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
1245
	#參考資料:
1246
	#exec=>http://php.net/manual/en/function.exec.php
1247
	#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
1248
	#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
1249
	$callShell=external::callShell($conf["external::callShell"]);
1250
	unset($conf["external::callShell"]);
1251
 
1252
	#如果執行失敗
1253
	if($callShell["status"]==="false"){
1254
 
1255
		#設置執行失敗
1256
		$result["status"]="false";
1257
 
1258
		#設置錯誤訊息
1259
		$result["error"]=$callShell;
1260
 
1261
		#回傳結果
1262
		var_dump($result);
1263
 
1264
		#結束執行
565 liveuser 1265
		exit(1);
1 liveuser 1266
 
1267
		}#if end
1268
 
1269
	#debug
1270
	if($debug){
1271
 
1272
		#comment
1273
		echo "accessed file dovecot/imap log".PHP_EOL;
1274
 
1275
		}#if end
1276
 
1277
	#取得行數
1278
	$lineCount=count($callShell["output"]);
1279
 
1280
	#如果行數等於1
1281
	if($lineCount===1){
1282
 
1283
		#debug
1284
		if($debug){
1285
 
1286
			#最新一筆惡意IP
705 liveuser 1287
			echo "最新一筆惡意IP:".PHP_EOL;
1288
			var_dump($callShell["output"]);
1 liveuser 1289
 
1290
			}#if end
1291
 
1292
		#Feb 23 07:44:29 localhost.localdomain dovecot[1427]: imap-login: Disconnected (no auth attempts in 0 secs): user=<>, rip=101.136.215.61, lip=169.254.1.1, TLS handshaking: SSL_accept() failed: error:14094416:SSL routines:ssl3_read_bytes:sslv3 alert certificate unknown: SSL alert number 46, session=<9oMMYvW7ihhliNc9>
1293
		$unProcesedStr=$callShell["output"];
1294
 
1295
		#分割字串
1296
		#涵式說明:
1297
		#將多個固定格式的字串分開,並回傳分開的結果
1298
		#回傳的參數:
1299
		#$result["status"],執行成功與否,若爲"true",代表執行成功,若爲"false"代表執失敗。
1300
		#$result["error"],錯誤訊息陣列.
1301
		#$result["function"],當前執行的函數名稱.
1302
		#$result["spiltString"][$i]["oriStr"],爲第i個字串的原始內容
1303
		#$result["spiltString"][$i]["dataArray"],爲第($i+1)個字串分割後的字串陣列
1304
		#$result["spiltString"][$i]["dataArray"][$j],爲第($i+1)的分割好的字串的第($j+1)段內容
1305
		#$result["spiltString"][$i]["dataCounts"],爲第($i+1)個字串分割後總共分成幾段
1306
		#必填的參數:
1307
		#$conf["stringIn"],字串陣列,要處理的字串陣列.
1308
		$conf["stringProcess::spiltMutiString"]["stringIn"]=$unProcesedStr;
1309
		#$conf["spiltSymbol"],字串,爲要以哪個符號作爲分割.
1310
		$conf["stringProcess::spiltMutiString"]["spiltSymbol"]=" ";
1311
		$spiltMutiString=stringProcess::spiltMutiString($conf["stringProcess::spiltMutiString"]);
1312
		unset($conf["stringProcess::spiltMutiString"]);
1313
 
1314
		#如果分割失敗
1315
		if($spiltMutiString["status"]==="false"){
1316
 
1317
			#設置執行失敗
1318
			$result["status"]="false";
1319
 
1320
			#設置錯誤訊息
1321
			$result["error"]=$spiltMutiString;
1322
 
1323
			#印出結果
1324
			var_dump($result);
1325
 
1326
			#停止執行
565 liveuser 1327
			exit(1);
1 liveuser 1328
 
1329
			}#if end
1330
 
1331
		#記錄 smtpd log 最後變更的時間
1332
		$imapTimeFloat=$spiltMutiString["spiltString"][0]["dataArray"][2];
1333
 
1334
		}#if end
565 liveuser 1335
 
1336
	#反之沒變動
1 liveuser 1337
	else{
1338
 
565 liveuser 1339
		#沿用上次時間
1340
		$imapTimeFloat=$params["info"]["preTimeFloat"]["imap"];
1341
 
1342
		}#else ebd
1343
 
35 liveuser 1344
	#透過 journalctl -a -e | grep named | tail -n 1 來取得最後一筆 dns 查詢記錄	
1 liveuser 1345
	#涵式說明:
1346
	#呼叫shell執行系統命令,並取得回傳的內容.
1347
	#回傳的結果:
1348
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1349
	#$result["error"],錯誤訊息陣列.
1350
	#$result["function"],當前執行的函數名稱.
1351
	#$result["argu"],使用的參數.
1352
	#$result["cmd"],執行的指令內容.
1353
	#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
1354
	#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
1355
	#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
1356
	#$result["running"],是否還在執行.
1357
	#$result["pid"],pid.
1358
	#$result["statusCode"],執行結束後的代碼.
1359
	#必填的參數
1360
	#$conf["command"],字串,要執行的指令與.
1361
	$conf["external::callShell"]["command"]="journalctl";
1362
	#$conf["fileArgu"],字串,變數__FILE__的內容.
1363
	$conf["external::callShell"]["fileArgu"]=__FILE__;		
1364
	#可省略參數:
1365
	#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
28 liveuser 1366
	$conf["external::callShell"]["argu"]=array("-a","-e","|","grep"," named","|","tail","-n","1");
1 liveuser 1367
	#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
1368
	#$conf["arguIsAddr"]=array();	
1369
	#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
1370
	#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
1371
	#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
1372
	#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
1373
	#$conf["enablePrintDescription"]="true";
1374
	#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
1375
	#$conf["printDescription"]="";
1376
	#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".
453 liveuser 1377
	$conf["external::callShell"]["escapeshellarg"]="true";
1 liveuser 1378
	#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
1379
	#$conf["username"]="";
1380
	#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
1381
	#$conf["password"]="";
1382
	#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
1383
	#$conf["useScript"]="";
1384
	#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
1385
	#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
1386
	#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
1387
	#$conf["inBackGround"]="";
1388
	#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
1389
	#$conf["getErr"]="false";
1390
	#備註:
1391
	#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
1392
	#參考資料:
1393
	#exec=>http://php.net/manual/en/function.exec.php
1394
	#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
1395
	#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
1396
	$callShell=external::callShell($conf["external::callShell"]);
1397
	unset($conf["external::callShell"]);
1398
 
1399
	#如果執行失敗
1400
	if($callShell["status"]==="false"){
1401
 
1402
		#設置執行失敗
1403
		$result["status"]="false";
1404
 
1405
		#設置錯誤訊息
1406
		$result["error"]=$callShell;
1407
 
1408
		#回傳結果
1409
		var_dump($result);
1410
 
1411
		#結束執行
565 liveuser 1412
		exit(1);
1 liveuser 1413
 
1414
		}#if end
1415
 
1416
	#debug
1417
	if($debug){
1418
 
1419
		#comment
1420
		echo "accessed file named log".PHP_EOL;
1421
 
1422
		}#if end
1423
 
1424
	#取得行數
1425
	$lineCount=count($callShell["output"]);
1426
 
1427
	#如果行數等於1
1428
	if($lineCount===1){
1429
 
1430
		#debug
1431
		if($debug){
1432
 
1433
			#最新一筆惡意IP
705 liveuser 1434
			echo "最新一筆惡意IP:".PHP_EOL;
1435
			var_dump($callShell["output"]);
1 liveuser 1436
 
1437
			}#if end
1438
 
1439
		#Aug 09 07:12:09 www.qbpwcf.org postfix/smtpd[20829]: connect from unknown[185.234.219.62]
1440
		$unProcesedStr=$callShell["output"];
1441
 
1442
		#分割字串
1443
		#涵式說明:
1444
		#將多個固定格式的字串分開,並回傳分開的結果
1445
		#回傳的參數:
1446
		#$result["status"],執行成功與否,若爲"true",代表執行成功,若爲"false"代表執失敗。
1447
		#$result["error"],錯誤訊息陣列.
1448
		#$result["function"],當前執行的函數名稱.
1449
		#$result["spiltString"][$i]["oriStr"],爲第i個字串的原始內容
1450
		#$result["spiltString"][$i]["dataArray"],爲第($i+1)個字串分割後的字串陣列
1451
		#$result["spiltString"][$i]["dataArray"][$j],爲第($i+1)的分割好的字串的第($j+1)段內容
1452
		#$result["spiltString"][$i]["dataCounts"],爲第($i+1)個字串分割後總共分成幾段
1453
		#必填的參數:
1454
		#$conf["stringIn"],字串陣列,要處理的字串陣列.
1455
		$conf["stringProcess::spiltMutiString"]["stringIn"]=$unProcesedStr;
1456
		#$conf["spiltSymbol"],字串,爲要以哪個符號作爲分割.
1457
		$conf["stringProcess::spiltMutiString"]["spiltSymbol"]=" ";
1458
		$spiltMutiString=stringProcess::spiltMutiString($conf["stringProcess::spiltMutiString"]);
1459
		unset($conf["stringProcess::spiltMutiString"]);
1460
 
1461
		#如果分割失敗
1462
		if($spiltMutiString["status"]==="false"){
1463
 
1464
			#設置執行失敗
1465
			$result["status"]="false";
1466
 
1467
			#設置錯誤訊息
1468
			$result["error"]=$spiltMutiString;
1469
 
1470
			#印出結果
1471
			var_dump($result);
1472
 
1473
			#停止執行
565 liveuser 1474
			exit(1);
1 liveuser 1475
 
1476
			}#if end
1477
 
28 liveuser 1478
		#記錄 named log 最後變更的時間
565 liveuser 1479
		$namedTimeFloat=$spiltMutiString["spiltString"][0]["dataArray"][2];	
1 liveuser 1480
 
1481
		}#if end
565 liveuser 1482
 
1483
	#反之無變動
1 liveuser 1484
	else{
565 liveuser 1485
 
1486
		#沿用上次的時間
1487
		$namedTimeFloat=$params["info"]["preTimeFloat"]["named"];
1 liveuser 1488
 
1489
		}#else end
565 liveuser 1490
 
1 liveuser 1491
	#debug mode
1492
	if($debug){
1493
 
1494
		#comment
1495
		echo "\$httpTimeFloat:".$httpTimeFloat.PHP_EOL;
1496
		echo "\$httpsTimeFloat:".$httpsTimeFloat.PHP_EOL;
1497
		echo "\$smtpTimeFloat:".$smtpTimeFloat.PHP_EOL;
1498
		echo "\$imapTimeFloat:".$imapTimeFloat.PHP_EOL;
1499
		echo "\$namedTimeFloat:".$namedTimeFloat.PHP_EOL;
1500
 
1501
		}#if end
1502
 
1503
	#第一次運行,不做事.
565 liveuser 1504
	if($params["info"]["preTimeFloat"]["http"]===0 && $params["info"]["preTimeFloat"]["https"]===0 && $params["info"]["preTimeFloat"]["smtp"]===0 && $params["info"]["preTimeFloat"]["imap"]===0 && $params["info"]["preTimeFloat"]["named"]===0){
1 liveuser 1505
 
1506
		#將本次http記錄覆寫到上次的記錄
565 liveuser 1507
		$params["info"]["preTimeFloat"]["http"]=$httpTimeFloat;
1 liveuser 1508
 
1509
		#將本次https記錄覆寫到上次的記錄
565 liveuser 1510
		$params["info"]["preTimeFloat"]["https"]=$httpsTimeFloat;
1 liveuser 1511
 
1512
		#將本次smtps記錄覆寫到上次的記錄
565 liveuser 1513
		$params["info"]["preTimeFloat"]["smtp"]=$smtpTimeFloat;
1 liveuser 1514
 
1515
		#將本次imaps記錄覆寫到上次的記錄
565 liveuser 1516
		$params["info"]["preTimeFloat"]["imap"]=$imapTimeFloat;
1 liveuser 1517
 
1518
		#將本次nmaed記錄覆寫到上次的記錄
565 liveuser 1519
		$params["info"]["preTimeFloat"]["named"]=$namedTimeFloat;
1 liveuser 1520
 
1521
		#睡一秒
1522
		sleep(1);
1523
 
1524
		#debug mode
1525
		if($debug){
1526
 
1527
			#comment
1528
			echo "第一次運行,不做事".PHP_EOL;
1529
 
1530
			}#if end
1531
 
565 liveuser 1532
		#結束執行
1533
		return;
1 liveuser 1534
 
1535
		}#if end
1536
 
705 liveuser 1537
	#非第一次運行,且 http 與 https 與 imap 與 smtp 與 named log 都沒變動過.
565 liveuser 1538
	else if($params["info"]["preTimeFloat"]["http"]===$httpTimeFloat && $params["info"]["preTimeFloat"]["https"]===$httpsTimeFloat && $params["info"]["preTimeFloat"]["smtp"]===$smtpTimeFloat && $params["info"]["preTimeFloat"]["imap"]===$imapTimeFloat && $params["info"]["preTimeFloat"]["named"]===$namedTimeFloat ){
1 liveuser 1539
 
1540
		#睡一秒
1541
		sleep(1);
1542
 
1543
		#debug mode
1544
		if($debug){
1545
 
1546
			#comment
1547
			echo "Log檔無變動,不做事".PHP_EOL;
1548
 
1549
			}#if end
1550
 
565 liveuser 1551
		#結束執行
1552
		return;
1 liveuser 1553
 
1554
		}#else end
1555
 
1556
	#debug mode
1557
	if($debug){
1558
 
1559
		#comment
1560
		echo "Log檔有變動,要做事".PHP_EOL;
565 liveuser 1561
		echo "\$preHttpTimeFloat:".$params["info"]["preTimeFloat"]["http"].PHP_EOL;
1562
		echo "\$preHttpsTimeFloat:".$params["info"]["preTimeFloat"]["https"].PHP_EOL;
1563
		echo "\$preSmtpTimeFloat:".$params["info"]["preTimeFloat"]["smtp"].PHP_EOL;
1564
		echo "\$preImapTimeFloat:".$params["info"]["preTimeFloat"]["imap"].PHP_EOL;
1565
		echo "\$preNamedTimeFloat:".$params["info"]["preTimeFloat"]["named"].PHP_EOL;
1 liveuser 1566
		echo "\$httpTimeFloat:".$httpTimeFloat.PHP_EOL;
1567
		echo "\$httpsTimeFloat:".$httpsTimeFloat.PHP_EOL;
1568
		echo "\$smtpTimeFloat:".$smtpTimeFloat.PHP_EOL;
1569
		echo "\$imapTimeFloat:".$imapTimeFloat.PHP_EOL;
1570
		echo "\$namedTimeFloat:".$namedTimeFloat.PHP_EOL;
1571
 
1572
		}#if end
1573
 
1574
	#將本次http記錄覆寫到上次的記錄
565 liveuser 1575
	$params["info"]["preTimeFloat"]["http"]=$httpTimeFloat;
1 liveuser 1576
 
1577
	#將本次https記錄覆寫到上次的記錄
565 liveuser 1578
	$params["info"]["preTimeFloat"]["https"]=$httpsTimeFloat;
1 liveuser 1579
 
576 liveuser 1580
	#初始化儲存暫時ip白名單的陣列
1581
	$excludeIpsTmp=array();
1582
 
565 liveuser 1583
	#針對每個暫時白名單ip
1584
	foreach($params["info"]["excludeIpsTmp"] as $index=>$tmpIpInfo){
1585
 
1586
		#若暫存白名單已經過期
570 liveuser 1587
		if($tmpIpInfo["unixtime"]<time()-$params["info"]["tmp-white-ip-valid-time"]*3600){
1 liveuser 1588
 
565 liveuser 1589
			#卸除之
1590
			unset($params["info"]["excludeIpsTmp"][$index]);
1591
 
1592
			}#if end
576 liveuser 1593
 
1594
		#反之
1595
		else{
565 liveuser 1596
 
576 liveuser 1597
			#變成 $excludeIpsTmp 的內容	
1598
			$excludeIpsTmp[]=$params["info"]["excludeIpsTmp"][$index]["ip"];
1599
 
1600
			}#else end
1601
 
565 liveuser 1602
		}#foreach end
1603
 
1604
	#函式說明:
1605
	#將多個多維陣列串聯起來,key從0開始排序.
1606
	#回傳的結果:
1607
	#$result["status"],"true"表執行正常,"false"代表執行不正常.
1608
	#$result["error"],錯誤訊息陣列.
1609
	#$result["function"],當前執行的函數.
1610
	#$result["content"],合併好的一維陣列.
1611
	#必填參數
1612
	#$conf["inputArray"],陣列,要合併的一維陣列變數,例如:=array($array1,$array2);
576 liveuser 1613
	$conf["inputArray"]=array($params["info"]["excludeIp"],$excludeIpsTmp);
565 liveuser 1614
	#可省略參數:
1615
	#$conf["allowRepeat"],字串,預設為"false",不允許重複的結果;反之為"true".
1616
	#$conf["allowRepeat"]="true";
1617
	#$conf["equalKeyStruc"],字串陣列,若 allowRepeat 參數為 "false", 這該參數生效.該參數為用來判斷每個陣列的哪個鍵值路徑底下的數值相同時要進行取代,後者會取代前者.
1618
	#$conf["equalKeyStruc"]=array();
1619
	#參考資料:
1620
	#無.
1621
	#備註:
1622
	#無.
1623
	$mergeMultiDimensionArray=arrays::mergeMultiDimensionArray($conf);
1624
	unset($conf);
1625
 
1626
	#如果執行失敗
1627
	if($mergeMultiDimensionArray["status"]==="false"){
1628
 
1629
		#設置執行失敗
1630
		$result["status"]="false";
1631
 
1632
		#設置錯誤訊息
1633
		$result["error"]=$mergeMultiDimensionArray;
1634
 
1635
		#回傳結果
1636
		var_dump($result);
1637
 
1638
		#結束執行
1639
		exit(1);
1640
 
1641
		}#if end
1642
 
576 liveuser 1643
	#debug
1644
	#var_dump(__LINE__,$mergeMultiDimensionArray);
1645
 
1 liveuser 1646
	#涵式說明:
705 liveuser 1647
	#檢查 httpd 與 postfix/smtpd 的 log 與 named 的 log 與 sshd 的 log 把惡意連線的 IP 用防火牆阻阻擋
1 liveuser 1648
	#回傳結果:
1649
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1650
	#$result["error"],錯誤訊息.
1651
	#$result["function"],當前執行的函數名稱.
1652
	#$result["argu"],所使用的參數.
1653
	#$result["found"],是否有找到符合的檔案,"true"代表有;"false"代表沒有.
1654
	#$result["content"],找到的檔案陣列.
1655
	#必填參數:
1656
	#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
1657
	$conf["fileArgu"]=__FILE__;
1658
	#可省略參數:
84 liveuser 1659
	#$conf["excludeIp"],字串陣列,白名單ip陣列.
565 liveuser 1660
	$conf["excludeIp"]=$mergeMultiDimensionArray["content"];
1 liveuser 1661
	#$conf["logPath"],字串,httpd的log位置,預設為 "/var/log/httpd"
1662
	#$conf["logPath"]="";
1663
	#$conf["username"],字串,要用什麼使用者來執行,預設為root使用者
1664
	#$conf["username"]="";
1665
	#$conf["password"],字串,root使用者的密碼,
1666
	#$conf["password"]="";
1667
	#$conf["getIplistOnly"],字串,是否不阻擋IP只取得有問題的IP,預設為"false",要阻擋IP;"true"代表只取得有問題的IP.
1668
	#$conf["getIplistOnly"]="true";
1669
	$blockAcctackIp=cmd::blockAcctackIp($conf);
1670
	unset($conf);
1671
 
1672
	#如果執行失敗
1673
	if($blockAcctackIp["status"]==="false"){
1674
 
1675
		#設置執行失敗
1676
		$result["status"]="false";
1677
 
1678
		#設置錯誤訊息
1679
		$result["error"]=$blockAcctackIp;
1680
 
1681
		#印出結果
1682
		var_dump($result);
1683
 
1684
		#結束執行
565 liveuser 1685
		exit(1);
1 liveuser 1686
 
1687
		}#if end
1688
 
1689
	#如果有執行封鎖IP的情形
1690
	if(isset($blockAcctackIp["content"])){
1691
 
1692
		#comment
1693
		echo "本次封鎖IP的結果如下:".PHP_EOL;
1694
		var_dump($blockAcctackIp["content"]);
556 liveuser 1695
		var_dump($blockAcctackIp["reason"]);
1 liveuser 1696
 
1697
		}#if end
1698
 
1699
	#睡一秒
562 liveuser 1700
	sleep(1);
1 liveuser 1701
 
565 liveuser 1702
	#結束執行
1703
	return;
1 liveuser 1704
 
565 liveuser 1705
	}//function funcToRunWhenIdle end
1 liveuser 1706
 
565 liveuser 1707
?>