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
<?php
2
 
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/>.
22
 
23
*/
24
namespace qbpwcf;
25
 
26
/*
27
類別說明:
28
跟維護有關的類別.
29
備註:
30
無.
31
*/
32
class maintain{
226 liveuser 33
 
3 liveuser 34
	/*
35
	#函式說明:
36
	#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
37
	#回傳結果:
38
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
39
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
40
	#$result["function"],當前執行的函式名稱.
41
	#必填參數:
42
	#$method,物件,為物件實體或類別名稱,會自動置入該參數.
43
	#$arguments,陣列,為呼叫方法時所用的參數.
44
	#可省略參數:
45
	#無.
46
	#參考資料:
47
	#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
48
	#備註:
49
	#無.
50
	*/
51
	public function __call($method,$arguments){
226 liveuser 52
 
3 liveuser 53
		#取得當前執行的函式
54
		$result["function"]=__FUNCTION__;
226 liveuser 55
 
3 liveuser 56
		#設置執行不正常
57
		$result["status"]="false";
226 liveuser 58
 
3 liveuser 59
		#設置執行錯誤
60
		$result["error"][]=__NAMESPACE__ ."/".$method."() 不存在!";
226 liveuser 61
 
3 liveuser 62
		#設置所丟入的參數
63
		$result["error"][]=$arguments;
226 liveuser 64
 
3 liveuser 65
		#回傳結果
66
		return $result;
226 liveuser 67
 
3 liveuser 68
		}#function __call end
226 liveuser 69
 
3 liveuser 70
	/*
71
	#函式說明:
72
	#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
73
	#回傳結果:
74
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
75
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
76
	#$result["function"],當前執行的函式名稱.
77
	#必填參數:
78
	#$method,物件,為物件實體或類別名稱,會自動置入該參數.
79
	#$arguments,陣列,為呼叫方法時所用的參數.
80
	#可省略參數:
81
	#無.
82
	#參考資料:
83
	#__callStatic=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
84
	#備註:
85
	#無.
86
	*/
87
	public static function __callStatic($method,$arguments){
226 liveuser 88
 
3 liveuser 89
		#取得當前執行的函式
90
		$result["function"]=__FUNCTION__;
226 liveuser 91
 
3 liveuser 92
		#設置執行不正常
93
		$result["status"]="false";
226 liveuser 94
 
3 liveuser 95
		#設置執行錯誤
96
		$result["error"][]="欲呼叫的". __NAMESPACE__ ."/".$method."() 不存在!";
226 liveuser 97
 
3 liveuser 98
		#設置所丟入的參數
99
		$result["error"][]=$arguments;
226 liveuser 100
 
3 liveuser 101
		#回傳結果
102
		return $result;
226 liveuser 103
 
3 liveuser 104
		}#function __callStatic end
226 liveuser 105
 
3 liveuser 106
	/*
107
	#函式說明:
108
	#寄送硬碟使用狀況給指定的信箱.
109
	#必填參數:
110
	#$conf["fileArgu"],字串,__FILE__的內容.
111
	$conf["fileArgu"]=__FILE__;
112
	#$conf["username"],字串,用於認證信箱的帳戶.
113
	$conf["username"]="";
114
	#$conf["password"],字串,用於認證信箱的帳戶密碼.
115
	$conf["password"]="";
116
	#$conf["receiverMail"],陣列,收件人的信箱.
117
	$conf["receiverMail"]=array("");
118
	#可省略參數:
119
	#$conf["sleep"],字串,要每幾秒執行一次,預設為60秒.
120
	#$conf["sleep"]="60";
121
	#可省略參數:
122
	#無.
123
	#參考資料:
124
	#無.
125
	#備註:
126
	#無.
127
	*/
128
	public static function sendDiskUsageReport(&$conf){
226 liveuser 129
 
3 liveuser 130
		#初始化要回傳的結果
131
		$result=array();
132
 
133
		#取得當前執行的函數名稱
134
		$result["function"]=__FUNCTION__;
135
 
136
		#如果沒有參數
137
		if(func_num_args()==0){
226 liveuser 138
 
3 liveuser 139
			#設置執行失敗
140
			$result["status"]="false";
226 liveuser 141
 
3 liveuser 142
			#設置執行錯誤訊息
143
			$result["error"]="函數".$result["function"]."需要參數";
226 liveuser 144
 
3 liveuser 145
			#回傳結果
146
			return $result;
226 liveuser 147
 
3 liveuser 148
			}#if end
226 liveuser 149
 
3 liveuser 150
		#取得參數
151
		$result["argu"]=$conf;
152
 
153
		#如果 $conf 不為陣列
154
		if(gettype($conf)!=="array"){
226 liveuser 155
 
3 liveuser 156
			#設置執行失敗
157
			$result["status"]="false";
226 liveuser 158
 
3 liveuser 159
			#設置執行錯誤訊息
160
			$result["error"][]="\$conf變數須為陣列形態";
226 liveuser 161
 
3 liveuser 162
			#如果傳入的參數為 null
163
			if($conf===null){
226 liveuser 164
 
3 liveuser 165
				#設置執行錯誤訊息
166
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 167
 
3 liveuser 168
				}#if end
169
 
170
			#回傳結果
171
			return $result;
226 liveuser 172
 
3 liveuser 173
			}#if end
226 liveuser 174
 
3 liveuser 175
		#檢查參數
176
		#函式說明:
177
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
178
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
179
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
180
		#$result["function"],當前執行的函式名稱.
181
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
182
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
183
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
184
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
185
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
186
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
187
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
188
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
189
		#必填寫的參數:
190
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
191
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
192
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
193
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("fileArgu","username","password","receiverMail");
194
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
195
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string","string","array");
196
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
197
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
198
		#可以省略的參數:
199
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
200
		#$conf["canBeEmptyString"]="false";
201
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
202
		#$conf["canNotBeEmpty"]=array();
203
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
204
		#$conf["canBeEmpty"]=array();
205
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
206
		#$conf["skipableVariableCanNotBeEmpty"]=array();
207
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
208
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("sleep");
226 liveuser 209
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
3 liveuser 210
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string");
211
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
212
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("60");
213
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
214
		#$conf["arrayCountEqualCheck"][]=array();
215
		#參考資料來源:
216
		#array_keys=>http://php.net/manual/en/function.array-keys.php
217
		#建議:
218
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
219
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
220
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 221
 
3 liveuser 222
		#如果檢查參數失敗
223
		if($checkArguments["status"]==="false"){
226 liveuser 224
 
3 liveuser 225
			#設置執行失敗
226
			$result["status"]="false";
226 liveuser 227
 
3 liveuser 228
			#設置執行錯誤
229
			$result["error"]=$checkArguments;
226 liveuser 230
 
3 liveuser 231
			#回傳結果
232
			return $result;
226 liveuser 233
 
3 liveuser 234
			}#if end
226 liveuser 235
 
3 liveuser 236
		#如果檢查參數不通過
237
		if($checkArguments["status"]==="false"){
226 liveuser 238
 
3 liveuser 239
			#設置執行失敗
240
			$result["status"]="false";
226 liveuser 241
 
3 liveuser 242
			#設置執行錯誤
243
			$result["error"]=$checkArguments;
226 liveuser 244
 
3 liveuser 245
			#回傳結果
246
			return $result;
226 liveuser 247
 
3 liveuser 248
			}#if end
226 liveuser 249
 
3 liveuser 250
		#函式說明:
251
		#透過"df -h"取得伺服器上的磁碟空間用量
252
		#回傳結果
253
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
254
		#$result["error"],錯誤訊息陣列.
255
		#$result["function"],當前執行的函數名稱.
256
		#$result["argu"],使用的參數.
257
		#$result["content"],磁碟空間用量.
258
		#$result["oriOutput"],原始輸出的逐行內容.
259
		#必填參數:
260
		#$conf["fileArgu"],字串,__FILE__的內容.
261
		$conf["cmd::getStorageUsage"]["fileArgu"]=$conf["fileArgu"];
262
		$getStorageUsage=cmd::getStorageUsage($conf["cmd::getStorageUsage"]);
263
		unset($conf["cmd::getStorageUsage"]);
226 liveuser 264
 
265
		#如果執行失敗
3 liveuser 266
		if($getStorageUsage["status"]==="false"){
226 liveuser 267
 
3 liveuser 268
			#設置執行失敗
269
			$result["status"]="false";
226 liveuser 270
 
3 liveuser 271
			#設置執行錯誤
272
			$result["error"]=$getStorageUsage;
226 liveuser 273
 
3 liveuser 274
			#回傳結果
275
			return $result;
226 liveuser 276
 
3 liveuser 277
			}#if end
226 liveuser 278
 
3 liveuser 279
		#初始化純文字內容
280
		$plainText="";
226 liveuser 281
 
3 liveuser 282
		#初始化網頁的內容
283
		$html="";
226 liveuser 284
 
3 liveuser 285
		#針對每行原始結果
286
		foreach($getStorageUsage["oriOutput"] as $line){
226 liveuser 287
 
3 liveuser 288
			#串接plain text內容
289
			$plainText=$plainText.$line."r\n";
226 liveuser 290
 
3 liveuser 291
			}#foreach end
226 liveuser 292
 
3 liveuser 293
		#識別是否需要警示
294
		$warning=false;
226 liveuser 295
 
296
		#列開始
297
		$html=$html."<table border=\"1px\">";
298
 
3 liveuser 299
		#初始化尚未設置 <thead>
226 liveuser 300
		$haveHeader=false;
301
 
3 liveuser 302
		#針對每行輸出結果
303
		foreach($getStorageUsage["content"] as $line){
226 liveuser 304
 
3 liveuser 305
			#如果尚未設置 haveHeader
306
			if(!$haveHeader){
226 liveuser 307
 
3 liveuser 308
				#開始 <thead>
309
				$html=$html."<thead>";
226 liveuser 310
 
3 liveuser 311
				#針對每個項目
312
				foreach($line as $item=>$val){
226 liveuser 313
 
3 liveuser 314
					$html=$html."<th>".$item."</th>";
226 liveuser 315
 
3 liveuser 316
					}#foreach end
226 liveuser 317
 
3 liveuser 318
				#結束 <thead>
319
				$html=$html."</thead>";
226 liveuser 320
 
3 liveuser 321
				#開始tbody
322
				$html=$html."<tbody>";
226 liveuser 323
 
3 liveuser 324
				#設置有<thead>了
325
				$haveHeader=true;
226 liveuser 326
 
3 liveuser 327
				}#if end
226 liveuser 328
 
329
			#列開始
3 liveuser 330
			$html=$html."<tr>";
226 liveuser 331
 
3 liveuser 332
			#針對每個項目
333
			foreach($line as $item=>$val){
226 liveuser 334
 
3 liveuser 335
				#初始化fontStyle
226 liveuser 336
				$fontStyle="";
337
 
3 liveuser 338
				#如果是 "Use%"
339
				if($item==="Use%"){
226 liveuser 340
 
3 liveuser 341
					#取的比例與%
342
					sscanf($val,'%d%s',$person,$sympol);
226 liveuser 343
 
3 liveuser 344
					#如果大於90
345
					if($person>=90){
226 liveuser 346
 
3 liveuser 347
						#設置文字顏色
348
						$fontStyle="style=\"color:red;font-weight:bold\";";
226 liveuser 349
 
3 liveuser 350
						#設置需要警示
351
						$warning=true;
226 liveuser 352
 
3 liveuser 353
						}#if end
226 liveuser 354
 
355
					}#if end
356
 
3 liveuser 357
				#欄位內容
358
				$html=$html."<td ".$fontStyle.">$val</td>";
226 liveuser 359
 
3 liveuser 360
				}#foreach end
226 liveuser 361
 
362
			#列結束
3 liveuser 363
			$html=$html."</tr>";
226 liveuser 364
 
3 liveuser 365
			}#foreach end
226 liveuser 366
 
367
		#表格結束
3 liveuser 368
		$html=$html."</tbody></table>";
226 liveuser 369
 
3 liveuser 370
		#如果需要警示
371
		if($warning){
226 liveuser 372
 
3 liveuser 373
			#涵式說明:
374
			#使用 curl 來透過SMTP伺服器寄信
375
			#回傳的接結果:
376
			#$result["status"],寄信的情況,若爲"true",則十之八九沒有問題.
377
			#$result["error"],錯誤訊息陣列
378
			#$result["function"],當前執行的函數
379
			#$result["warning"],警示訊息,一些不會導致失敗但不能說是完美的問題.
380
			#$result["cmd"],寄信的指令.
381
			#$result["detailCmd"],較詳細的寄信指令.
382
			#必填的參數:
383
			#$conf["username"],字串,用來登入郵件伺服器的帳號
384
			$conf["mail::curlSmtp"]["username"]=$conf["username"];
385
			#$conf["password"],字串,用來登入郵件伺服器的密碼
386
			$conf["mail::curlSmtp"]["password"]=$conf["password"];
387
			#$conf["receiverMail"],陣列,收件人的信箱
388
			$conf["mail::curlSmtp"]["receiverMail"]=$conf["receiverMail"];
389
			#$conf["subject"],字串,郵件的主題
390
			$conf["mail::curlSmtp"]["subject"]="磁碟空間使用狀況";
391
			#$conf["plainBody"],字串,郵件本文的純文字內容.
392
			$conf["mail::curlSmtp"]["plainBody"]=$plainText;
393
			#$conf["htmlBody"]="";,字串,郵件本文的html內容,多媒體盡量不要用base64的方式來嵌入,因為大部分的郵件服務(Gmail)都不支援,如果是在單機上的郵件軟體就可能有支援,例如:Evolution.
394
			$conf["mail::curlSmtp"]["htmlBody"]=$html;
395
			#$conf["fileArgu"],字串,php內建變數「__FILE__」的內容.
396
			$conf["mail::curlSmtp"]["fileArgu"]=$conf["fileArgu"];
397
			#可省略的參數:
398
			#$conf["mailServer"],字串,要使用的SMTP郵件伺服器網址與連接口,預設爲 smtps://smtp.gmail.com:465
399
			#$conf["mailServer"]="";
400
			#$conf["mailerMailDisplay"],字串,要顯示的寄件人信箱,預設爲 $conf["username"]
401
			#$conf["mailerMailDisplay"]="";
402
			#$conf["mailerNameDisplay"],字串,要顯示的寄件人姓名,預設爲 $conf["username"]
403
			#$conf["mailerNameDisplay"]="";
404
			#$conf["receiverMailDisplay"],陣列,要顯示的收件人信箱,預設爲 $conf["receiverMail"] 對應的元素.
405
			#$conf["receiverMailDisplay"]="";#
406
			#$conf["receiverNameDisplay"],陣列,要顯示的收件人姓名,預設爲 $conf["receiverMail"] 對應的元素.
407
			#$conf["receiverNameDisplay"]="";
408
			#$conf["attachment"],陣列,每個要寄送的附件路徑與檔案名稱
409
			#$conf["attachment"]=array();
410
			#$conf["attachmentName"],陣列,每個要寄送的附件顯示名稱,預設為$conf["attachment"]中的實際檔案名稱.
411
			#$conf["attachmentName"]=array();
412
			#$conf["attachmentMimeType"],陣列,每個附件的 mimeType,預設為"application/*".
413
			#$conf["attachmentMimeType"]array();
414
			#範例語句:
415
			#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
416
			#範例信件內容:
417
			#From: "userName" <username@gmail.com>
418
			#To: "receiverName" <userPassword@yahoo.com.tw>
419
			#Subject: This is a test
420
			#本文~
421
			#參考資料來源:
422
			#用curl來寄信=>http://stackoverflow.com/questions/14722556/using-curl-to-send-email
423
			#降低安全性標準讓curl可以使用=>https://www.google.com/settings/security/lesssecureapps
424
			#信件內容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/
425
			#檔案每列不得超超過70個字元=>http://php.net/manual/en/function.mail.php
426
			#寄信給多人=>https://github.com/curl/curl/issues/784
427
			$curlSmtp=mail::curlSmtp($conf["mail::curlSmtp"]);
428
			unset($conf["mail::curlSmtp"]);
226 liveuser 429
 
3 liveuser 430
			#如果寄信失敗
431
			if($curlSmtp["status"]==="false"){
226 liveuser 432
 
3 liveuser 433
				#設置執行失敗
434
				$result["status"]="false";
226 liveuser 435
 
3 liveuser 436
				#設置執行錯誤
437
				$result["error"]=$curlSmtp;
226 liveuser 438
 
3 liveuser 439
				#回傳結果
440
				return $result;
226 liveuser 441
 
3 liveuser 442
				}#if end
226 liveuser 443
 
3 liveuser 444
			}#if end
226 liveuser 445
 
3 liveuser 446
		#暫停 $conf["sleep"] 秒
447
		sleep($conf["sleep"]);
226 liveuser 448
 
3 liveuser 449
		#呼叫自己
450
		$sendDiskUsageReport=maintain::sendDiskUsageReport($conf);
451
		unset($conf);
226 liveuser 452
 
3 liveuser 453
		#如果呼叫自己失敗
454
		if($sendDiskUsageReport["status"]==="false"){
226 liveuser 455
 
3 liveuser 456
			#設置執行失敗
457
			$result["status"]="false";
226 liveuser 458
 
3 liveuser 459
			#設置執行錯誤
460
			$result["error"]=$sendDiskUsageReport;
226 liveuser 461
 
3 liveuser 462
			#回傳結果
463
			return $result;
226 liveuser 464
 
3 liveuser 465
			}#if end
226 liveuser 466
 
3 liveuser 467
		}#function sendDiskUsageReport end
226 liveuser 468
 
3 liveuser 469
	}#class maintain end
470
 
471
?>