Subversion Repositories php-qbpwcf

Rev

Rev 3 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3 Rev 226
Line 1... Line 1...
1
<?php
1
<?php
2
 
2
 
3
/*
3
/*
4
 
4
 
5
	QBPWCF, Quick Build PHP website Component base on Fedora Linux.
5
	QBPWCF, Quick Build PHP website Component base on Fedora Linux.
6
    Copyright (C) 2015~2024 Min-Jhin,Chen
6
    Copyright (C) 2014~2025 MIN ZHI, CHEN
7
 
7
 
8
    This file is part of QBPWCF.
8
    This file is part of QBPWCF.
9
 
9
 
10
    QBPWCF is free software: you can redistribute it and/or modify
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
11
    it under the terms of the GNU General Public License as published by
Line 28... Line 28...
28
跟維護有關的類別.
28
跟維護有關的類別.
29
備註:
29
備註:
30
無.
30
無.
31
*/
31
*/
32
class maintain{
32
class maintain{
33
	
33
 
34
	/*
34
	/*
35
	#函式說明:
35
	#函式說明:
36
	#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
36
	#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
37
	#回傳結果:
37
	#回傳結果:
38
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
38
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 47... Line 47...
47
	#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
47
	#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
48
	#備註:
48
	#備註:
49
	#無.
49
	#無.
50
	*/
50
	*/
51
	public function __call($method,$arguments){
51
	public function __call($method,$arguments){
52
		
52
 
53
		#取得當前執行的函式
53
		#取得當前執行的函式
54
		$result["function"]=__FUNCTION__;
54
		$result["function"]=__FUNCTION__;
55
		
55
 
56
		#設置執行不正常
56
		#設置執行不正常
57
		$result["status"]="false";
57
		$result["status"]="false";
58
		
58
 
59
		#設置執行錯誤
59
		#設置執行錯誤
60
		$result["error"][]=__NAMESPACE__ ."/".$method."() 不存在!";
60
		$result["error"][]=__NAMESPACE__ ."/".$method."() 不存在!";
61
		
61
 
62
		#設置所丟入的參數
62
		#設置所丟入的參數
63
		$result["error"][]=$arguments;
63
		$result["error"][]=$arguments;
64
		
64
 
65
		#回傳結果
65
		#回傳結果
66
		return $result;
66
		return $result;
67
		
67
 
68
		}#function __call end
68
		}#function __call end
69
		
69
 
70
	/*
70
	/*
71
	#函式說明:
71
	#函式說明:
72
	#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
72
	#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
73
	#回傳結果:
73
	#回傳結果:
74
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
74
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 83... Line 83...
83
	#__callStatic=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
83
	#__callStatic=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
84
	#備註:
84
	#備註:
85
	#無.
85
	#無.
86
	*/
86
	*/
87
	public static function __callStatic($method,$arguments){
87
	public static function __callStatic($method,$arguments){
88
		
88
 
89
		#取得當前執行的函式
89
		#取得當前執行的函式
90
		$result["function"]=__FUNCTION__;
90
		$result["function"]=__FUNCTION__;
91
		
91
 
92
		#設置執行不正常
92
		#設置執行不正常
93
		$result["status"]="false";
93
		$result["status"]="false";
94
		
94
 
95
		#設置執行錯誤
95
		#設置執行錯誤
96
		$result["error"][]="欲呼叫的". __NAMESPACE__ ."/".$method."() 不存在!";
96
		$result["error"][]="欲呼叫的". __NAMESPACE__ ."/".$method."() 不存在!";
97
		
97
 
98
		#設置所丟入的參數
98
		#設置所丟入的參數
99
		$result["error"][]=$arguments;
99
		$result["error"][]=$arguments;
100
		
100
 
101
		#回傳結果
101
		#回傳結果
102
		return $result;
102
		return $result;
103
		
103
 
104
		}#function __callStatic end
104
		}#function __callStatic end
105
		
105
 
106
	/*
106
	/*
107
	#函式說明:
107
	#函式說明:
108
	#寄送硬碟使用狀況給指定的信箱.
108
	#寄送硬碟使用狀況給指定的信箱.
109
	#必填參數:
109
	#必填參數:
110
	#$conf["fileArgu"],字串,__FILE__的內容.
110
	#$conf["fileArgu"],字串,__FILE__的內容.
Line 124... Line 124...
124
	#無.
124
	#無.
125
	#備註:
125
	#備註:
126
	#無.
126
	#無.
127
	*/
127
	*/
128
	public static function sendDiskUsageReport(&$conf){
128
	public static function sendDiskUsageReport(&$conf){
129
		
129
 
130
		#初始化要回傳的結果
130
		#初始化要回傳的結果
131
		$result=array();
131
		$result=array();
132
 
132
 
133
		#取得當前執行的函數名稱
133
		#取得當前執行的函數名稱
134
		$result["function"]=__FUNCTION__;
134
		$result["function"]=__FUNCTION__;
135
 
135
 
136
		#如果沒有參數
136
		#如果沒有參數
137
		if(func_num_args()==0){
137
		if(func_num_args()==0){
138
			
138
 
139
			#設置執行失敗
139
			#設置執行失敗
140
			$result["status"]="false";
140
			$result["status"]="false";
141
			
141
 
142
			#設置執行錯誤訊息
142
			#設置執行錯誤訊息
143
			$result["error"]="函數".$result["function"]."需要參數";
143
			$result["error"]="函數".$result["function"]."需要參數";
144
			
144
 
145
			#回傳結果
145
			#回傳結果
146
			return $result;
146
			return $result;
147
			
147
 
148
			}#if end
148
			}#if end
149
		
149
 
150
		#取得參數
150
		#取得參數
151
		$result["argu"]=$conf;
151
		$result["argu"]=$conf;
152
 
152
 
153
		#如果 $conf 不為陣列
153
		#如果 $conf 不為陣列
154
		if(gettype($conf)!=="array"){
154
		if(gettype($conf)!=="array"){
155
			
155
 
156
			#設置執行失敗
156
			#設置執行失敗
157
			$result["status"]="false";
157
			$result["status"]="false";
158
			
158
 
159
			#設置執行錯誤訊息
159
			#設置執行錯誤訊息
160
			$result["error"][]="\$conf變數須為陣列形態";
160
			$result["error"][]="\$conf變數須為陣列形態";
161
			
161
 
162
			#如果傳入的參數為 null
162
			#如果傳入的參數為 null
163
			if($conf===null){
163
			if($conf===null){
164
				
164
 
165
				#設置執行錯誤訊息
165
				#設置執行錯誤訊息
166
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
166
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
167
				
167
 
168
				}#if end
168
				}#if end
169
 
169
 
170
			#回傳結果
170
			#回傳結果
171
			return $result;
171
			return $result;
172
			
172
 
173
			}#if end
173
			}#if end
174
		
174
 
175
		#檢查參數
175
		#檢查參數
176
		#函式說明:
176
		#函式說明:
177
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
177
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
178
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
178
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
179
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
179
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
Line 204... Line 204...
204
		#$conf["canBeEmpty"]=array();
204
		#$conf["canBeEmpty"]=array();
205
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
205
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
206
		#$conf["skipableVariableCanNotBeEmpty"]=array();
206
		#$conf["skipableVariableCanNotBeEmpty"]=array();
207
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
207
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
208
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("sleep");
208
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("sleep");
209
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double"); 
209
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
210
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string");
210
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string");
211
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
211
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
212
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("60");
212
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("60");
213
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
213
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
214
		#$conf["arrayCountEqualCheck"][]=array();
214
		#$conf["arrayCountEqualCheck"][]=array();
Line 216... Line 216...
216
		#array_keys=>http://php.net/manual/en/function.array-keys.php
216
		#array_keys=>http://php.net/manual/en/function.array-keys.php
217
		#建議:
217
		#建議:
218
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
218
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
219
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
219
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
220
		unset($conf["variableCheck::checkArguments"]);
220
		unset($conf["variableCheck::checkArguments"]);
221
		
221
 
222
		#如果檢查參數失敗
222
		#如果檢查參數失敗
223
		if($checkArguments["status"]==="false"){
223
		if($checkArguments["status"]==="false"){
224
			
224
 
225
			#設置執行失敗
225
			#設置執行失敗
226
			$result["status"]="false";
226
			$result["status"]="false";
227
			
227
 
228
			#設置執行錯誤
228
			#設置執行錯誤
229
			$result["error"]=$checkArguments;
229
			$result["error"]=$checkArguments;
230
			
230
 
231
			#回傳結果
231
			#回傳結果
232
			return $result;
232
			return $result;
233
			
233
 
234
			}#if end
234
			}#if end
235
			
235
 
236
		#如果檢查參數不通過
236
		#如果檢查參數不通過
237
		if($checkArguments["status"]==="false"){
237
		if($checkArguments["status"]==="false"){
238
			
238
 
239
			#設置執行失敗
239
			#設置執行失敗
240
			$result["status"]="false";
240
			$result["status"]="false";
241
			
241
 
242
			#設置執行錯誤
242
			#設置執行錯誤
243
			$result["error"]=$checkArguments;
243
			$result["error"]=$checkArguments;
244
			
244
 
245
			#回傳結果
245
			#回傳結果
246
			return $result;
246
			return $result;
247
			
247
 
248
			}#if end
248
			}#if end
249
		
249
 
250
		#函式說明:
250
		#函式說明:
251
		#透過"df -h"取得伺服器上的磁碟空間用量
251
		#透過"df -h"取得伺服器上的磁碟空間用量
252
		#回傳結果
252
		#回傳結果
253
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
253
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
254
		#$result["error"],錯誤訊息陣列.
254
		#$result["error"],錯誤訊息陣列.
Line 259... Line 259...
259
		#必填參數:
259
		#必填參數:
260
		#$conf["fileArgu"],字串,__FILE__的內容.
260
		#$conf["fileArgu"],字串,__FILE__的內容.
261
		$conf["cmd::getStorageUsage"]["fileArgu"]=$conf["fileArgu"];
261
		$conf["cmd::getStorageUsage"]["fileArgu"]=$conf["fileArgu"];
262
		$getStorageUsage=cmd::getStorageUsage($conf["cmd::getStorageUsage"]);
262
		$getStorageUsage=cmd::getStorageUsage($conf["cmd::getStorageUsage"]);
263
		unset($conf["cmd::getStorageUsage"]);
263
		unset($conf["cmd::getStorageUsage"]);
264
		
264
 
265
		#如果執行失敗 
265
		#如果執行失敗
266
		if($getStorageUsage["status"]==="false"){
266
		if($getStorageUsage["status"]==="false"){
267
			
267
 
268
			#設置執行失敗
268
			#設置執行失敗
269
			$result["status"]="false";
269
			$result["status"]="false";
270
			
270
 
271
			#設置執行錯誤
271
			#設置執行錯誤
272
			$result["error"]=$getStorageUsage;
272
			$result["error"]=$getStorageUsage;
273
			
273
 
274
			#回傳結果
274
			#回傳結果
275
			return $result;
275
			return $result;
276
			
276
 
277
			}#if end
277
			}#if end
278
		
278
 
279
		#初始化純文字內容
279
		#初始化純文字內容
280
		$plainText="";
280
		$plainText="";
281
		
281
 
282
		#初始化網頁的內容
282
		#初始化網頁的內容
283
		$html="";
283
		$html="";
284
		
284
 
285
		#針對每行原始結果
285
		#針對每行原始結果
286
		foreach($getStorageUsage["oriOutput"] as $line){
286
		foreach($getStorageUsage["oriOutput"] as $line){
287
			
287
 
288
			#串接plain text內容
288
			#串接plain text內容
289
			$plainText=$plainText.$line."r\n";
289
			$plainText=$plainText.$line."r\n";
290
						
290
 
291
			}#foreach end
291
			}#foreach end
292
		
292
 
293
		#識別是否需要警示
293
		#識別是否需要警示
294
		$warning=false;
294
		$warning=false;
295
			
295
 
296
		#列開始			
296
		#列開始
297
		$html=$html."<table border=\"1px\">";	
297
		$html=$html."<table border=\"1px\">";
298
			
298
 
299
		#初始化尚未設置 <thead>
299
		#初始化尚未設置 <thead>
300
		$haveHeader=false;	
300
		$haveHeader=false;
301
			
301
 
302
		#針對每行輸出結果
302
		#針對每行輸出結果
303
		foreach($getStorageUsage["content"] as $line){
303
		foreach($getStorageUsage["content"] as $line){
304
			
304
 
305
			#如果尚未設置 haveHeader
305
			#如果尚未設置 haveHeader
306
			if(!$haveHeader){
306
			if(!$haveHeader){
307
				
307
 
308
				#開始 <thead>
308
				#開始 <thead>
309
				$html=$html."<thead>";
309
				$html=$html."<thead>";
310
				
310
 
311
				#針對每個項目
311
				#針對每個項目
312
				foreach($line as $item=>$val){
312
				foreach($line as $item=>$val){
313
					
313
 
314
					$html=$html."<th>".$item."</th>";
314
					$html=$html."<th>".$item."</th>";
315
					
315
 
316
					}#foreach end
316
					}#foreach end
317
				
317
 
318
				#結束 <thead>
318
				#結束 <thead>
319
				$html=$html."</thead>";
319
				$html=$html."</thead>";
320
				
320
 
321
				#開始tbody
321
				#開始tbody
322
				$html=$html."<tbody>";
322
				$html=$html."<tbody>";
323
				
323
 
324
				#設置有<thead>了
324
				#設置有<thead>了
325
				$haveHeader=true;
325
				$haveHeader=true;
326
				
326
 
327
				}#if end
327
				}#if end
328
			
328
 
329
			#列開始			
329
			#列開始
330
			$html=$html."<tr>";
330
			$html=$html."<tr>";
331
			
331
 
332
			#針對每個項目
332
			#針對每個項目
333
			foreach($line as $item=>$val){
333
			foreach($line as $item=>$val){
334
					
334
 
335
				#初始化fontStyle
335
				#初始化fontStyle
336
				$fontStyle="";	
336
				$fontStyle="";
337
						
337
 
338
				#如果是 "Use%"
338
				#如果是 "Use%"
339
				if($item==="Use%"){
339
				if($item==="Use%"){
340
					
340
 
341
					#取的比例與%
341
					#取的比例與%
342
					sscanf($val,'%d%s',$person,$sympol);
342
					sscanf($val,'%d%s',$person,$sympol);
343
					
343
 
344
					#如果大於90
344
					#如果大於90
345
					if($person>=90){
345
					if($person>=90){
346
						
346
 
347
						#設置文字顏色
347
						#設置文字顏色
348
						$fontStyle="style=\"color:red;font-weight:bold\";";
348
						$fontStyle="style=\"color:red;font-weight:bold\";";
349
						
349
 
350
						#設置需要警示
350
						#設置需要警示
351
						$warning=true;
351
						$warning=true;
352
						
352
 
353
						}#if end
353
						}#if end
354
					
354
 
355
					}#if end		
355
					}#if end
356
								
356
 
357
				#欄位內容
357
				#欄位內容
358
				$html=$html."<td ".$fontStyle.">$val</td>";
358
				$html=$html."<td ".$fontStyle.">$val</td>";
359
				
359
 
360
				}#foreach end
360
				}#foreach end
361
			
361
 
362
			#列結束	
362
			#列結束
363
			$html=$html."</tr>";
363
			$html=$html."</tr>";
364
			
364
 
365
			}#foreach end
365
			}#foreach end
366
		
366
 
367
		#表格結束			
367
		#表格結束
368
		$html=$html."</tbody></table>";
368
		$html=$html."</tbody></table>";
369
			
369
 
370
		#如果需要警示
370
		#如果需要警示
371
		if($warning){
371
		if($warning){
372
			
372
 
373
			#涵式說明:
373
			#涵式說明:
374
			#使用 curl 來透過SMTP伺服器寄信
374
			#使用 curl 來透過SMTP伺服器寄信
375
			#回傳的接結果:
375
			#回傳的接結果:
376
			#$result["status"],寄信的情況,若爲"true",則十之八九沒有問題.
376
			#$result["status"],寄信的情況,若爲"true",則十之八九沒有問題.
377
			#$result["error"],錯誤訊息陣列
377
			#$result["error"],錯誤訊息陣列
Line 424... Line 424...
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/
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
425
			#檔案每列不得超超過70個字元=>http://php.net/manual/en/function.mail.php
426
			#寄信給多人=>https://github.com/curl/curl/issues/784
426
			#寄信給多人=>https://github.com/curl/curl/issues/784
427
			$curlSmtp=mail::curlSmtp($conf["mail::curlSmtp"]);
427
			$curlSmtp=mail::curlSmtp($conf["mail::curlSmtp"]);
428
			unset($conf["mail::curlSmtp"]);
428
			unset($conf["mail::curlSmtp"]);
429
			
429
 
430
			#如果寄信失敗
430
			#如果寄信失敗
431
			if($curlSmtp["status"]==="false"){
431
			if($curlSmtp["status"]==="false"){
432
				
432
 
433
				#設置執行失敗
433
				#設置執行失敗
434
				$result["status"]="false";
434
				$result["status"]="false";
435
				
435
 
436
				#設置執行錯誤
436
				#設置執行錯誤
437
				$result["error"]=$curlSmtp;
437
				$result["error"]=$curlSmtp;
438
				
438
 
439
				#回傳結果
439
				#回傳結果
440
				return $result;
440
				return $result;
441
				
441
 
442
				}#if end
442
				}#if end
443
			
443
 
444
			}#if end
444
			}#if end
445
		
445
 
446
		#暫停 $conf["sleep"] 秒
446
		#暫停 $conf["sleep"] 秒
447
		sleep($conf["sleep"]);
447
		sleep($conf["sleep"]);
448
		
448
 
449
		#呼叫自己
449
		#呼叫自己
450
		$sendDiskUsageReport=maintain::sendDiskUsageReport($conf);
450
		$sendDiskUsageReport=maintain::sendDiskUsageReport($conf);
451
		unset($conf);
451
		unset($conf);
452
		
452
 
453
		#如果呼叫自己失敗
453
		#如果呼叫自己失敗
454
		if($sendDiskUsageReport["status"]==="false"){
454
		if($sendDiskUsageReport["status"]==="false"){
455
			
455
 
456
			#設置執行失敗
456
			#設置執行失敗
457
			$result["status"]="false";
457
			$result["status"]="false";
458
			
458
 
459
			#設置執行錯誤
459
			#設置執行錯誤
460
			$result["error"]=$sendDiskUsageReport;
460
			$result["error"]=$sendDiskUsageReport;
461
			
461
 
462
			#回傳結果
462
			#回傳結果
463
			return $result;
463
			return $result;
464
			
464
 
465
			}#if end
465
			}#if end
466
		
466
 
467
		}#function sendDiskUsageReport end
467
		}#function sendDiskUsageReport end
468
	
468
 
469
	}#class maintain end
469
	}#class maintain end
470
 
470
 
471
?>
471
?>