Subversion Repositories qbpwcf-lib(archive)

Rev

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

Rev Author Line No. Line
102 liveuser 1
#!/bin/php
2
<?php
3
 
4
/*
464 liveuser 5
	QBPWCF, Quick Build PHP website Component base on Fedora Linux.
619 liveuser 6
    Copyright (C) 2015~2024 Min-Jhin,Chen
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
/*
102 liveuser 26
說明:
27
用來檢測本機跟1.1.1.1、8.8.8.8是否連線正常,若不正常則用email提示或輸出訊息就好.檢查頻率為60秒一次.
28
*/
29
 
30
#使用命名空間qbpwcf
31
namespace qbpwcf;
32
 
466 liveuser 33
#以該檔案的實際位置的 lib path 為 include path 首位
906 liveuser 34
exec("cd ".pathinfo(__FILE__)["dirname"]."/../lib;pwd;",$output,$status);
466 liveuser 35
set_include_path($output[0].PATH_SEPARATOR.get_include_path());
102 liveuser 36
 
466 liveuser 37
#匯入外部套件
38
include("allInOne.php");
39
 
102 liveuser 40
#說明用法的函式
41
function help(){
42
 
43
	#函式說明:
44
	#印出多行文字,結尾自動換行.
45
	#回傳的結果:
46
	#$result["status"],執行是否成功,"true"代表成功,"false"代表失敗.
47
	#$result["function"],當前執行的函數名稱.
48
	#$result["error"],錯誤訊息陣列.
49
	#必填的參數:
50
	#$conf["outputStringArray"],字串陣列,每行要印出的文字內容.
51
	$conf["outputStringArray"][]=$_SERVER["argv"][0]." 用法:";
52
	$conf["outputStringArray"][]="--addPingTarget pingTarget #新增 pingTarget 為要增加檢測是否可以連線的目標.";
53
	$conf["outputStringArray"][]="--emailAcct email account #設置寄信用的帳號,若無設置則為用本機直接寄送.";
54
	$conf["outputStringArray"][]="--emailPass email password #設置寄信用帳號的密碼.";
55
	$conf["outputStringArray"][]="--receiver email #收件人的信箱,若有多個收件人請重複使用該參數.若無該參數則不會寄信只會印出訊息";
687 liveuser 56
	$conf["outputStringArray"][]="--levelForCheckSSL force/losse/none #ssl憑證的規範,\"force\"代表一定要符合;\"loose\"代表第2次嘗試可以不符合;\"none\"代表不用符合";
102 liveuser 57
	$echoMultiLine=cmd::echoMultiLine($conf);
58
	unset($conf);
59
 
60
	#如果解析內容失敗
61
	if($echoMultiLine["status"]==="false"){
62
 
63
		#印出結果
64
		var_dump($echoMultiLine);
65
 
66
		#結束程式
67
		exit;
68
 
69
		}#if end
70
 
71
	#結束執行
72
	exit;
73
 
74
	}#function end
75
 
76
#函式說明:
77
#解析參數.
78
#回傳結果:
79
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
80
#$reuslt["error"],執行不正常結束的錯訊息陣列.
81
#$result["function"],當前執行的函式名稱.
82
#$result["content"],解析好的參數陣列.
83
#$result["content"][$key][$i],參數 $key 的 $i+1 個參數數值內容.
84
#$result["program"],字串,執行的程式名稱.
85
#必填參數:
86
#無
87
#可省略參數:
88
#$conf["helpFunc"],如果解析的參數不成對,或沒有參數則要執行的函式名稱.
89
$conf["helpFunc"]="help";
90
#備註:
91
#僅能在命令列底下執行.
92
#建議:
93
#以後可將參數 --a--b 的名稱與後面的數值 $value 存成 $result["a"]["b"][$i]=$value .
94
$parseArgu=cmd::parseArgu($conf);
95
unset($conf);
96
 
97
#如果執行失敗
98
if($parseArgu["status"]==="false"){
99
 
100
	#debug
101
	var_dump($parseArgu);
102
 
103
	#結束執行
104
	exit;
105
 
106
	}#if end
107
 
108
#初始化ping的目標為 1.1.1.1 跟 8.8.8.8
109
$pingTarget=array("1.1.1.1","8.8.8.8");
110
 
111
#如果有新增ping的目標
112
if(isset($parseArgu["content"]["addPingTarget"])){
113
 
114
	#針對每個要增加ping的對象
115
	foreach($parseArgu["content"]["addPingTarget"] as $newPingTarget){
116
 
117
		#增加新的ping目標
118
		$pingTarget[]=$newPingTarget;
119
 
120
		}#foreach end
121
 
122
	}#if end
123
 
124
#如果沒有設置收件人
125
if(!isset($parseArgu["content"]["receiver"])){
126
 
127
	#設置不寄信只輸出訊息就好
128
	$echoOnly="true";
129
 
130
	}#if end
131
 
132
#反之
133
else{
134
 
135
	#不要只輸出訊息
136
	$echoOnly="false";
137
 
138
	#取得收件人
139
	$receiver=$parseArgu["content"]["receiver"];
140
 
141
	}#else end
142
 
143
#如果沒有設置寄信用的 email 跟 密碼
144
if( !isset($parseArgu["content"]["emailAcct"]) || !isset($parseArgu["content"]["emailPass"]) ){
145
 
146
	#設置不要用smtp來寄信
147
	$sendBySmtp="false";
148
 
149
	}#if end
150
 
151
#反之	
152
else{
153
 
154
	#設置要用smtp寄信
155
	$sendBySmtp="true";
156
 
157
	}#else end
158
 
687 liveuser 159
#預設為寬鬆的設定
160
$levelForCheckSSL="loose";
161
 
162
#如果有設置 levelForCheckSSL 參數
163
if(isset($parseArgu["content"]["levelForCheckSSL"])){
164
 
165
	#如果確實有該參數
166
	if(isset($parseArgu["content"]["levelForCheckSSL"][0])){
167
 
168
		#取得其設定
169
		$levelForCheckSSL=$parseArgu["content"]["levelForCheckSSL"][0];
170
 
171
		}#if end
172
 
173
	}#if end
174
 
102 liveuser 175
#無窮迴圈
176
while(true){
177
 
178
	#初始化不用警報
179
	$needAlarm="false";
180
 
181
	#針對每個 ping target
182
	foreach($pingTarget as $pt){
183
 
184
		#函式說明:
185
		#呼叫shell執行系統命令,並取得回傳的內容.
186
		#回傳結果:
187
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
188
		#$result["error"],錯誤訊息陣列.
189
		#$result["function"],當前執行的函數名稱.
190
		#$result["argu"],使用的參數.
191
		#$result["cmd"],執行的指令內容.
192
		#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
193
		#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
194
		#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
195
		#$result["running"],是否還在執行.
196
		#$result["pid"],pid.
197
		#$result["statusCode"],執行結束後的代碼.
198
		#必填參數:
199
		#$conf["command"],字串,要執行的指令與.
200
		$conf["command"]="ping";
201
		#$conf["fileArgu"],字串,變數__FILE__的內容.
202
		$conf["fileArgu"]=__FILE__;
203
		#可省略參數:
204
		#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
114 liveuser 205
		$conf["argu"]=array("-q","-W","1","-c","1",$pt);
102 liveuser 206
		#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
207
		#$conf["arguIsAddr"]=array();
208
		#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
209
		#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
210
		#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
211
		#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
212
		#$conf["enablePrintDescription"]="true";
213
		#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
214
		#$conf["printDescription"]="";
215
		#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".
453 liveuser 216
		$conf["escapeshellarg"]="true";
102 liveuser 217
		#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
218
		#$conf["username"]="";
219
		#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
220
		#$conf["password"]="";
221
		#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
222
		#$conf["useScript"]="";
223
		#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
224
		#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
225
		#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
226
		#$conf["inBackGround"]="";
227
		#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
228
		#$conf["getErr"]="false";
229
		#備註:
230
		#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
231
		#參考資料:
232
		#exec=>http://php.net/manual/en/function.exec.php
233
		#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
234
		#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
235
		$callShell=external::callShell($conf);
236
		unset($conf);
237
 
114 liveuser 238
		#如果執行失敗,且不是狀態1或狀態2
239
		if($callShell["status"]==="false" && $callShell["statusCode"]!==1 && $callShell["statusCode"]!==2){
102 liveuser 240
 
241
			#debug
242
			var_dump($callShell);
243
 
244
			#結束執行
245
			exit;
246
 
247
			}#if end
248
 
249
		#針對每行輸出
250
		/*
251
		PING 1.1.1.1 (1.1.1.1) 56(84) bytes of data.
252
 
253
		--- 1.1.1.1 ping statistics ---
254
		1 packets transmitted, 1 received, 0% packet loss, time 0ms
255
		rtt min/avg/max/mdev = 46.251/46.251/46.251/0.000 ms
256
		*/
257
 
258
		#函式說明:
259
		#檢查多個字串中的每個字串是否有多個關鍵字
260
		#回傳結果:
261
		#$result["status"],整體來說,執行是否成功,"true"代表執行成功,"false"代表執行失敗。
262
		#$result["function"],當前執行的函數名稱.
263
		#$result["error"],錯誤訊息.
264
		#$result["argu"],使用的參數.
265
		#$result["foundedTrueKey"],結果為"true"的被搜尋元素key陣列.
106 liveuser 266
		#$result["foundedKeyWords"],找到的關鍵字.
102 liveuser 267
		#$result["foundedFalseKey"],結果為"false"的被搜尋元素key陣列.
268
		#$result["foundedTrueKeyWords"],二維陣列,各個字串有找到的關鍵字陣列.
269
		#$result["foundedAll"],是否每個關鍵字都有找到,"true"代表每個都有找到,"false"代表沒有每個都找到.
270
		#必填參數:
271
		$conf["keyWords"]=array("0% packet loss");#想要搜尋的關鍵字
272
		$conf["stringArray"]=$callShell["output"];#要被搜尋的字串內容陣列
273
		#可省略參數:
274
		#$conf["completeEqual"]="true";#是否內容要完全符合,不能多出任何不符合的內容,預設為"false"不需要完全符合.
275
		#備註:
276
		#無.
277
		$findManyKeyWordsFromManyString=search::findManyKeyWordsFromManyString($conf);
278
		unset($conf);
279
 
280
		#如果執行失敗
281
		if($findManyKeyWordsFromManyString["status"]==="false"){
282
 
283
			#debug
284
			var_dump($findManyKeyWordsFromManyString);
285
 
286
			#結束執行
287
			exit;
288
 
289
			}#if end
290
 
291
		#若沒有無遺失封包
292
		if($findManyKeyWordsFromManyString["foundedAll"]==="false"){
293
 
294
			#設置需要警報
295
			$needAlarm="true";
296
 
297
			#跳出foreach
298
			break;
299
 
300
			}#if end
301
 
302
		}#foreach end
303
 
304
	#如果需要警報
305
	if($needAlarm==="true"){
306
 
307
		#如果只要印出訊息
308
		if($echoOnly==="true"){
309
 
310
			#提示
311
			echo "目前無法連線到指定的伺服器,請確認網路環境!".PHP_EOL;
312
 
313
			#休息60秒
314
			sleep(60);
315
 
316
			#繼續執行
317
			continue;
318
 
319
			}#if end
320
 
321
		#如果不透過smtp來寄信
322
		if($sendBySmtp==="false"){
323
 
324
			#函式說明:
325
			#透過php的mail函數寄信
326
			#回傳結果:
327
			#$result["status"],寄信的情況,若爲"true",則十之八九沒有問題.
328
			#$result["error"],錯誤訊息陣列
329
			#$result["function"],當前執行的函數
330
			#必填參數:
331
			#$conf["to"],字串陣列,收件人們的信箱.
332
			$conf["to"]=$receiver;
333
			#$conf["subject"],字串,郵件標題.
334
			$conf["subject"]="目前無法連線到指定的伺服器,請確認網路環境!";
335
			#$conf["content"],字串,要寄送的訊息內容.
336
			$conf["content"]="目前無法連線到指定的伺服器,請確認網路環境!";
337
			#可省略參數: 
338
			#$conf["mailerEmail"],字串,顯示的寄件人.
339
			#$conf["mailerEmail"]="";
340
			#參考資料:
341
			#mail=>https://www.php.net/manual/en/function.mail.php
342
			#Internet Message Format=>http://www.faqs.org/rfcs/rfc2822
343
			$sendMail=mail::sendMail($conf);
344
			unset($conf);
345
 
346
			#如果執行失敗
347
			if($sendMail["status"]==="false"){
348
 
349
				#debug
350
				var_dump($sendMail);
351
 
352
				#結束執行
353
				exit;
354
 
355
				}#if end
356
 
357
			#休息60秒
358
			sleep(60);
359
 
360
			#繼續執行
361
			continue;
362
 
363
			}#if end
364
 
365
		#到這邊代表要用smtp寄信
366
		#函式說明:
367
		#使用 curl 來透過SMTP伺服器寄信
368
		#回傳結果:
369
		#$result["status"],寄信的情況,若爲"true",則十之八九沒有問題.
370
		#$result["error"],錯誤訊息陣列
371
		#$result["function"],當前執行的函數
372
		#$result["warning"],警示訊息,一些不會導致失敗但不能說是完美的問題.
373
		#$result["cmd"],寄信的指令.
374
		#$result["detailCmd"],較詳細的寄信指令.
375
		#必填參數:
376
		#$conf["username"],字串,用來登入郵件伺服器的帳號
377
		$conf["username"]=$parseArgu["content"]["emailAcct"];
378
		#$conf["password"],字串,用來登入郵件伺服器的密碼
379
		$conf["password"]=$parseArgu["content"]["emailPass"];
380
		#$conf["receiverMail"],陣列,收件人的信箱
381
		$conf["receiverMail"]=$parseArgu["content"]["receiver"];
382
		#$conf["subject"],字串,郵件的主題
383
		$conf["subject"]="目前無法連線到指定的伺服器,請確認網路環境!";
384
		#$conf["plainBody"],字串,郵件本文的純文字內容.
385
		$conf["plainBody"]="目前無法連線到指定的伺服器,請確認網路環境!";
386
		#$conf["htmlBody"]="";,字串,郵件本文的html內容,多媒體盡量不要用base64的方式來嵌入,因為大部分的郵件服務(Gmail)都不支援,如果是在單機上的郵件軟體就可能有支援,例如:Evolution.
387
		$conf["htmlBody"]="目前無法連線到指定的伺服器,請確認網路環境!";
388
		#$conf["fileArgu"],字串,php內建變數「__FILE__」的內容.
389
		$conf["fileArgu"]=__FILE__;
390
		#可省略參數:
391
		#$conf["mailServer"],字串,要使用的SMTP郵件伺服器網址與連接口,預設爲 smtps://smtp.gmail.com:465
392
		#$conf["mailServer"]="";
393
		#$conf["mailerMailDisplay"],字串,要顯示的寄件人信箱,預設爲 $conf["username"]
394
		#$conf["mailerMailDisplay"]="";
395
		#$conf["mailerNameDisplay"],字串,要顯示的寄件人姓名,預設爲 $conf["username"]
396
		#$conf["mailerNameDisplay"]="";
397
		#$conf["receiverMailDisplay"],陣列,要顯示的收件人信箱,預設爲 $conf["receiverMail"] 對應的元素.
398
		#$conf["receiverMailDisplay"]="";#
399
		#$conf["receiverNameDisplay"],陣列,要顯示的收件人姓名,預設爲 $conf["receiverMail"] 對應的元素.
400
		#$conf["receiverNameDisplay"]="";
401
		#$conf["attachment"],陣列,每個要寄送的附件路徑與檔案名稱
402
		#$conf["attachment"]=array();
403
		#$conf["attachmentName"],陣列,每個要寄送的附件顯示名稱,預設為$conf["attachment"]中的實際檔案名稱.
404
		#$conf["attachmentName"]=array();
405
		#$conf["attachmentMimeType"],陣列,每個附件的 mimeType,預設為"application/*".
406
		#$conf["attachmentMimeType"]array();
687 liveuser 407
		#$conf["levelForCheckSSL"],"字串",預設為"force",代表ssl一定要符合規範,"losse"代表先嘗試ssl要符合規範,若不符合則第2次就允許不符合規範的ssl,"none"代表直接允許不符合規範的ssl.
408
		$conf["levelForCheckSSL"]=$levelForCheckSSL;
102 liveuser 409
		#範例語句:
410
		#curl --url smtps://smtp.gmail.com:465 -u username@gmail.com:userPassword --mail-from username@gmail.com --mail-rcpt receiver@mail.com --upload-file mail.txt -v
411
		#範例信件內容:
412
		#From: "userName" <username@gmail.com>
413
		#To: "receiverName" <userPassword@yahoo.com.tw>
414
		#Subject: This is a test
415
		#本文~
416
		#參考資料來源:
417
		#用curl來寄信=>http://stackoverflow.com/questions/14722556/using-curl-to-send-email
418
		#降低安全性標準讓curl可以使用=>https://www.google.com/settings/security/lesssecureapps
419
		#信件內容header的設定=>http://jjdai.zhupiter.com/2010/10/php-%E5%A6%82%E4%BD%95%E5%82%B3%E9%80%81%E4%B8%80%E5%B0%81-mime-html-%E6%A0%BC%E5%BC%8F%E7%9A%84-email-%E4%BF%A1%E4%BB%B6/
420
		#檔案每列不得超超過70個字元=>http://php.net/manual/en/function.mail.php
421
		#寄信給多人=>https://github.com/curl/curl/issues/784
422
		#備註:
423
		#信件的內容格式有錯,導致本文無法顯示.
424
		$curlSmtp=mail::curlSmtp($conf);
425
		unset($conf);
426
 
427
		#如果執行失敗
428
		if($curlSmtp["status"]==="false"){
429
 
430
			#debug
431
			var_dump($curlSmtpcurlSmtp);
432
 
433
			#結束執行
434
			exit;
435
 
436
			}#if end
437
 
438
		#休息60秒
439
		sleep(60);
440
 
441
		#繼續執行
442
		continue;
443
 
444
		}#if end
445
 
446
	}#while end
447