Subversion Repositories php-qbpwcf

Rev

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

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