Subversion Repositories php-qbpwcf

Rev

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

<?php

/*

        QBPWCF, Quick Build PHP website Component base on Fedora Linux.
    Copyright (C) 2014~2025 MIN ZHI, CHEN

    This file is part of QBPWCF.

    QBPWCF is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    QBPWCF is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with QBPWCF.  If not, see <http://www.gnu.org/licenses/>.

*/
namespace qbpwcf;

/*
類別說明:
跟維護有關的類別.
備註:
無.
*/
class maintain{

        /*
        #函式說明:
        #當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
        #回傳結果:
        #$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
        #$reuslt["error"],執行不正常結束的錯訊息陣列.
        #$result["function"],當前執行的函式名稱.
        #必填參數:
        #$method,物件,為物件實體或類別名稱,會自動置入該參數.
        #$arguments,陣列,為呼叫方法時所用的參數.
        #可省略參數:
        #無.
        #參考資料:
        #__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
        #備註:
        #無.
        */
        public function __call($method,$arguments){

                #取得當前執行的函式
                $result["function"]=__FUNCTION__;

                #設置執行不正常
                $result["status"]="false";

                #設置執行錯誤
                $result["error"][]=__NAMESPACE__ ."/".$method."() 不存在!";

                #設置所丟入的參數
                $result["error"][]=$arguments;

                #回傳結果
                return $result;

                }#function __call end

        /*
        #函式說明:
        #當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
        #回傳結果:
        #$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
        #$reuslt["error"],執行不正常結束的錯訊息陣列.
        #$result["function"],當前執行的函式名稱.
        #必填參數:
        #$method,物件,為物件實體或類別名稱,會自動置入該參數.
        #$arguments,陣列,為呼叫方法時所用的參數.
        #可省略參數:
        #無.
        #參考資料:
        #__callStatic=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
        #備註:
        #無.
        */
        public static function __callStatic($method,$arguments){

                #取得當前執行的函式
                $result["function"]=__FUNCTION__;

                #設置執行不正常
                $result["status"]="false";

                #設置執行錯誤
                $result["error"][]="欲呼叫的". __NAMESPACE__ ."/".$method."() 不存在!";

                #設置所丟入的參數
                $result["error"][]=$arguments;

                #回傳結果
                return $result;

                }#function __callStatic end

        /*
        #函式說明:
        #寄送硬碟使用狀況給指定的信箱.
        #必填參數:
        #$conf["fileArgu"],字串,__FILE__的內容.
        $conf["fileArgu"]=__FILE__;
        #$conf["username"],字串,用於認證信箱的帳戶.
        $conf["username"]="";
        #$conf["password"],字串,用於認證信箱的帳戶密碼.
        $conf["password"]="";
        #$conf["receiverMail"],陣列,收件人的信箱.
        $conf["receiverMail"]=array("");
        #可省略參數:
        #$conf["sleep"],字串,要每幾秒執行一次,預設為60秒.
        #$conf["sleep"]="60";
        #可省略參數:
        #無.
        #參考資料:
        #無.
        #備註:
        #無.
        */
        public static function sendDiskUsageReport(&$conf){

                #初始化要回傳的結果
                $result=array();

                #取得當前執行的函數名稱
                $result["function"]=__FUNCTION__;

                #如果沒有參數
                if(func_num_args()==0){

                        #設置執行失敗
                        $result["status"]="false";

                        #設置執行錯誤訊息
                        $result["error"]="函數".$result["function"]."需要參數";

                        #回傳結果
                        return $result;

                        }#if end

                #取得參數
                $result["argu"]=$conf;

                #如果 $conf 不為陣列
                if(gettype($conf)!=="array"){

                        #設置執行失敗
                        $result["status"]="false";

                        #設置執行錯誤訊息
                        $result["error"][]="\$conf變數須為陣列形態";

                        #如果傳入的參數為 null
                        if($conf===null){

                                #設置執行錯誤訊息
                                $result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";

                                }#if end

                        #回傳結果
                        return $result;

                        }#if end

                #檢查參數
                #函式說明:
                #檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
                #$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
                #$reuslt["error"],執行不正常結束的錯訊息陣列.
                #$result["function"],當前執行的函式名稱.
                #$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
                #$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
                #$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
                #$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
                #$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
                #$result["argu"],字串陣列,目前輸入的參數名稱陣列.
                #$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
                #$result["notNeedVar"],字串陣列,多餘的參數名稱.
                #必填寫的參數:
                #$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
                $conf["variableCheck::checkArguments"]["varInput"]=&$conf;
                #$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
                $conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("fileArgu","username","password","receiverMail");
                #$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
                $conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string","string","array");
                #$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
                $conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
                #可以省略的參數:
                #$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
                #$conf["canBeEmptyString"]="false";
                #$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
                #$conf["canNotBeEmpty"]=array();
                #$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
                #$conf["canBeEmpty"]=array();
                #$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
                #$conf["skipableVariableCanNotBeEmpty"]=array();
                #$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
                $conf["variableCheck::checkArguments"]["skipableVariableName"]=array("sleep");
                #$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
                $conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string");
                #$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
                $conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("60");
                #$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
                #$conf["arrayCountEqualCheck"][]=array();
                #參考資料來源:
                #array_keys=>http://php.net/manual/en/function.array-keys.php
                #建議:
                #增加可省略參數全部不能為空字串或空陣列的參數功能.
                $checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
                unset($conf["variableCheck::checkArguments"]);

                #如果檢查參數失敗
                if($checkArguments["status"]==="false"){

                        #設置執行失敗
                        $result["status"]="false";

                        #設置執行錯誤
                        $result["error"]=$checkArguments;

                        #回傳結果
                        return $result;

                        }#if end

                #如果檢查參數不通過
                if($checkArguments["status"]==="false"){

                        #設置執行失敗
                        $result["status"]="false";

                        #設置執行錯誤
                        $result["error"]=$checkArguments;

                        #回傳結果
                        return $result;

                        }#if end

                #函式說明:
                #透過"df -h"取得伺服器上的磁碟空間用量
                #回傳結果
                #$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
                #$result["error"],錯誤訊息陣列.
                #$result["function"],當前執行的函數名稱.
                #$result["argu"],使用的參數.
                #$result["content"],磁碟空間用量.
                #$result["oriOutput"],原始輸出的逐行內容.
                #必填參數:
                #$conf["fileArgu"],字串,__FILE__的內容.
                $conf["cmd::getStorageUsage"]["fileArgu"]=$conf["fileArgu"];
                $getStorageUsage=cmd::getStorageUsage($conf["cmd::getStorageUsage"]);
                unset($conf["cmd::getStorageUsage"]);

                #如果執行失敗
                if($getStorageUsage["status"]==="false"){

                        #設置執行失敗
                        $result["status"]="false";

                        #設置執行錯誤
                        $result["error"]=$getStorageUsage;

                        #回傳結果
                        return $result;

                        }#if end

                #初始化純文字內容
                $plainText="";

                #初始化網頁的內容
                $html="";

                #針對每行原始結果
                foreach($getStorageUsage["oriOutput"] as $line){

                        #串接plain text內容
                        $plainText=$plainText.$line."r\n";

                        }#foreach end

                #識別是否需要警示
                $warning=false;

                #列開始
                $html=$html."<table border=\"1px\">";

                #初始化尚未設置 <thead>
                $haveHeader=false;

                #針對每行輸出結果
                foreach($getStorageUsage["content"] as $line){

                        #如果尚未設置 haveHeader
                        if(!$haveHeader){

                                #開始 <thead>
                                $html=$html."<thead>";

                                #針對每個項目
                                foreach($line as $item=>$val){

                                        $html=$html."<th>".$item."</th>";

                                        }#foreach end

                                #結束 <thead>
                                $html=$html."</thead>";

                                #開始tbody
                                $html=$html."<tbody>";

                                #設置有<thead>了
                                $haveHeader=true;

                                }#if end

                        #列開始
                        $html=$html."<tr>";

                        #針對每個項目
                        foreach($line as $item=>$val){

                                #初始化fontStyle
                                $fontStyle="";

                                #如果是 "Use%"
                                if($item==="Use%"){

                                        #取的比例與%
                                        sscanf($val,'%d%s',$person,$sympol);

                                        #如果大於90
                                        if($person>=90){

                                                #設置文字顏色
                                                $fontStyle="style=\"color:red;font-weight:bold\";";

                                                #設置需要警示
                                                $warning=true;

                                                }#if end

                                        }#if end

                                #欄位內容
                                $html=$html."<td ".$fontStyle.">$val</td>";

                                }#foreach end

                        #列結束
                        $html=$html."</tr>";

                        }#foreach end

                #表格結束
                $html=$html."</tbody></table>";

                #如果需要警示
                if($warning){

                        #涵式說明:
                        #使用 curl 來透過SMTP伺服器寄信
                        #回傳的接結果:
                        #$result["status"],寄信的情況,若爲"true",則十之八九沒有問題.
                        #$result["error"],錯誤訊息陣列
                        #$result["function"],當前執行的函數
                        #$result["warning"],警示訊息,一些不會導致失敗但不能說是完美的問題.
                        #$result["cmd"],寄信的指令.
                        #$result["detailCmd"],較詳細的寄信指令.
                        #必填的參數:
                        #$conf["username"],字串,用來登入郵件伺服器的帳號
                        $conf["mail::curlSmtp"]["username"]=$conf["username"];
                        #$conf["password"],字串,用來登入郵件伺服器的密碼
                        $conf["mail::curlSmtp"]["password"]=$conf["password"];
                        #$conf["receiverMail"],陣列,收件人的信箱
                        $conf["mail::curlSmtp"]["receiverMail"]=$conf["receiverMail"];
                        #$conf["subject"],字串,郵件的主題
                        $conf["mail::curlSmtp"]["subject"]="磁碟空間使用狀況";
                        #$conf["plainBody"],字串,郵件本文的純文字內容.
                        $conf["mail::curlSmtp"]["plainBody"]=$plainText;
                        #$conf["htmlBody"]="";,字串,郵件本文的html內容,多媒體盡量不要用base64的方式來嵌入,因為大部分的郵件服務(Gmail)都不支援,如果是在單機上的郵件軟體就可能有支援,例如:Evolution.
                        $conf["mail::curlSmtp"]["htmlBody"]=$html;
                        #$conf["fileArgu"],字串,php內建變數「__FILE__」的內容.
                        $conf["mail::curlSmtp"]["fileArgu"]=$conf["fileArgu"];
                        #可省略的參數:
                        #$conf["mailServer"],字串,要使用的SMTP郵件伺服器網址與連接口,預設爲 smtps://smtp.gmail.com:465
                        #$conf["mailServer"]="";
                        #$conf["mailerMailDisplay"],字串,要顯示的寄件人信箱,預設爲 $conf["username"]
                        #$conf["mailerMailDisplay"]="";
                        #$conf["mailerNameDisplay"],字串,要顯示的寄件人姓名,預設爲 $conf["username"]
                        #$conf["mailerNameDisplay"]="";
                        #$conf["receiverMailDisplay"],陣列,要顯示的收件人信箱,預設爲 $conf["receiverMail"] 對應的元素.
                        #$conf["receiverMailDisplay"]="";#
                        #$conf["receiverNameDisplay"],陣列,要顯示的收件人姓名,預設爲 $conf["receiverMail"] 對應的元素.
                        #$conf["receiverNameDisplay"]="";
                        #$conf["attachment"],陣列,每個要寄送的附件路徑與檔案名稱
                        #$conf["attachment"]=array();
                        #$conf["attachmentName"],陣列,每個要寄送的附件顯示名稱,預設為$conf["attachment"]中的實際檔案名稱.
                        #$conf["attachmentName"]=array();
                        #$conf["attachmentMimeType"],陣列,每個附件的 mimeType,預設為"application/*".
                        #$conf["attachmentMimeType"]array();
                        #範例語句:
                        #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
                        #範例信件內容:
                        #From: "userName" <username@gmail.com>
                        #To: "receiverName" <userPassword@yahoo.com.tw>
                        #Subject: This is a test
                        #本文~
                        #參考資料來源:
                        #用curl來寄信=>http://stackoverflow.com/questions/14722556/using-curl-to-send-email
                        #降低安全性標準讓curl可以使用=>https://www.google.com/settings/security/lesssecureapps
                        #信件內容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/
                        #檔案每列不得超超過70個字元=>http://php.net/manual/en/function.mail.php
                        #寄信給多人=>https://github.com/curl/curl/issues/784
                        $curlSmtp=mail::curlSmtp($conf["mail::curlSmtp"]);
                        unset($conf["mail::curlSmtp"]);

                        #如果寄信失敗
                        if($curlSmtp["status"]==="false"){

                                #設置執行失敗
                                $result["status"]="false";

                                #設置執行錯誤
                                $result["error"]=$curlSmtp;

                                #回傳結果
                                return $result;

                                }#if end

                        }#if end

                #暫停 $conf["sleep"] 秒
                sleep($conf["sleep"]);

                #呼叫自己
                $sendDiskUsageReport=maintain::sendDiskUsageReport($conf);
                unset($conf);

                #如果呼叫自己失敗
                if($sendDiskUsageReport["status"]==="false"){

                        #設置執行失敗
                        $result["status"]="false";

                        #設置執行錯誤
                        $result["error"]=$sendDiskUsageReport;

                        #回傳結果
                        return $result;

                        }#if end

                }#function sendDiskUsageReport end

        }#class maintain end

?>