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