Subversion Repositories php-qbpwcf

Rev

Rev 226 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php

/*

        QBPWCF, Quick Build PHP website Component base on Fedora Linux.
    Copyright (C) 2014~2026 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 record{

        /*
        #函式說明:
        #當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
        #回傳結果:
        #$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

        /*
        #函式說明:
        #記錄當下用戶的瀏覽器與地理位置與時間點等資訊
        #回傳結果:
        #$result["status"],爲查詢是否成功,若爲"true"則成功,若爲"false"則表示失敗了.
        #$result["error"],錯誤訊息.
        #$result["sql"],執行的sql語法.
        #$result["lastInsertId"],新增資料的id.
        #$result["content"],記錄的資訊.
        #$result["function"],當前執行的涵式
        #必填參數:
        #$conf["dbAddress"],字串,爲mysql/mariadb server的位置
        $conf["dbAddress"]=$dbAddress;
        #$conf["dbAccount"],字串,爲用於連入mysql/mariadb server時要使用的帳號
        $conf["dbAccount"]=$dbAccount;
        #$conf["fileArgu"],字串,__FILE__的內容.
        $conf["fileArgu"]=__FILE__;
        #可省略參數:
        #$conf["dbName"],字串,存放資料表的資料庫名稱,預設為 "qbpwcf".
        #$conf["dbName"]="qbpwcf";
        #$conf["dbPassword"],字串,爲連線到mariadb server時要使用的密碼,可省略,若省略則代表不使用密碼
        #$conf["dbPassword"]=$dbPassword;#
        #參考資料:
        #無.
        #備註:
        #無.
        */
        public static function recordUserInfo(&$conf){

                #初始化要回傳的內容
                $result=array();

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

                #如果 $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[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
                #$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
                #$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息
                #必填寫的參數:
                #$conf["variableCheck.checkArguments"]["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
                $conf["variableCheck.checkArguments"]["varInput"]=&$conf;
                #$conf["variableCheck.checkArguments"]["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
                $conf["variableCheck.checkArguments"]["mustBeFilledVariableName"]=array("dbAddress","dbAccount","dbName","fileArgu");
                #$conf["variableCheck.checkArguments"]["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double");
                $conf["variableCheck.checkArguments"]["mustBeFilledVariableType"]=array("string","string","string","string");
                #$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
                $conf["variableCheck.checkArguments"]["referenceVarKey"]="variableCheck.checkArguments";
                #可以省略的參數:
                #$conf["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"false"。
                #$conf["canBeEmptyString"]="false";
                #$conf["variableCheck.checkArguments"]["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
                $conf["variableCheck.checkArguments"]["skipableVariableName"]=array("dbPassword","dbName");
                #$conf["variableCheck.checkArguments"]["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
                $conf["variableCheck.checkArguments"]["skipableVariableType"]=array("string","string");
                #$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,"null"代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
                $conf["variableCheck.checkArguments"]["skipableVarDefaultValue"]=array(null,"qbpwcf");
                $checkResult=variableCheck::checkArguments($conf["variableCheck.checkArguments"]);
                unset($conf["variableCheck.checkArguments"]);

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

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

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

                        #回傳節果
                        return $result;

                        }#if end

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

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

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

                        #回傳節果
                        return $result;

                        }#if end

                #涵式說明:
                #取得 dns/ip 的地理位置資訊
                #回傳結果:
                #$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
                #$result["error"],錯誤訊息陣列.
                #$result["function"],當前執行的函式名稱.
                #$result["content"],取得的回應內容.
                #必填參數:
                #$conf["fileArgu"],字串,__FILE__的內容.
                $conf["csInformation::getIpGeo"]["fileArgu"]=$conf["fileArgu"];
                #可省略參數:
                #$conf["addr"],字串,dns或ip.
                #$conf["addr"]="";
                #備註:
                #請檢查參數
                $getIpGeo=csInformation::getIpGeo($conf["csInformation::getIpGeo"]);
                unset($conf["csInformation::getIpGeo"]);

                #如果取得 geo 資訊失敗
                if($getIpGeo["status"]==="false"){

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

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

                        #回傳結果
                        return $result;

                        }#if end

                #取得地理位置資訊
                $geoJson=$getIpGeo["content"];

                #設置geo參數
                $result["content"]["geo"]=$geoJson;

                #取得瀏覽器的資訊
                #涵式說明:
                #取得用戶端的瀏覽器版本資訊,並回傳。
                #回傳結果:
                #$result,瀏覽器的資訊
                $getUserBrowserType=csInformation::getUserBrowserType();

                #設置瀏覽器資訊
                $result["content"]["agent"]=$getUserBrowserType;

                #記錄當下要求的頁面等資訊
                $result["content"]["_SERVER"]=$_SERVER;

                #設置當下的時間點(無時區)
                $result["content"]["time"]=time();

                #涵式說明:
                #插入資料到指定的資料表裡面,插入資料完畢可以選擇是否要進行轉址。
                #回傳的數值:
                #$result["status"],爲查詢是否成功,若爲"true"則成功,若爲"false"則表示失敗了.
                #$result["error"],錯誤訊息.
                #$result["sql"],執行的sql語法.
                #$result["lastInsertId"],新增的資料id.
                #$result["function"],當前執行的涵式
                #必填的參數:
                $conf["db::insertData"]["dbAddress"]=$conf["dbAddress"];#爲mysql-Server的位置
                $conf["db::insertData"]["dbAccount"]=$conf["dbAccount"];#爲用於連入mysql-Server時要使用的帳號
                $conf["db::insertData"]["selectedDataBaseName"]=$conf["dbName"];#要選取的資料庫名稱
                $conf["db::insertData"]["tableName"]="call_record";#爲要插入資料的資料表名稱
                $conf["db::insertData"]["columnName"]=array("json");#爲資料表的項目名稱,
                        #例如:$conf["columnName"]=array("columnName1","columnName2","columnName3",...);
                $conf["db::insertData"]["insertValue"]=array(json_encode($result["content"]));#爲要插入度數值,
                        #例如:$conf["insertValue"]=array("insertValue1","insertValue2","insertValue3",...);
                #可以省略的變數:

                #如果有設置密碼
                if(isset($conf["dbPassword"])){

                        #設置密碼
                        $conf["db::insertData"]["dbPassword"]=$conf["dbPassword"];#爲連線到mysql-Server時要使用的密碼,可省略,若省略則代表不使用密碼

                        }#if end

                #參考資料來源:
                #http://www.javaworld.com.tw/jute/post/view?bid=21&id=173738&sty=1
                $insertData=db::insertData($conf["db::insertData"]);
                unset($conf["db::insertData"]);

                #如果新增資料失敗
                if($insertData["status"]==="false"){

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

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

                        #回傳結果
                        return $result;

                        }#if end

                #新增的資料id.
                $result["lastInsertId"]=$insertData["lastInsertId"];

                #新增的資料id.
                $result["sql"]=$insertData["sql"];

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

                #回傳結果
                return $result;

                }#function recordUserInfo end

        /*
        #函式說明:
        #記錄程式輸出的結果.
        #回傳結果:
        #$result["status"],爲查詢是否成功,若爲"true"則成功,若爲"false"則表示失敗了.
        #$result["error"],錯誤訊息.
        #$result["content"],程式的輸出.
        #$result["function"],當前執行的涵式
        #必填參數:
        #$conf["code2run"],字串陣列,爲要運行的程式內容,每個元素代表一段php程式.內容不必含有php tag,但結尾必須為「;」.
        $conf["code2run"]=array();
        #可省略參數:
        #$conf["varPassed"],陣列,為要使用到的變數,在code2run參數裡面"\$varPassed[n]"代表第n+1個變數.
        #$conf["varPassed"]=array();
        #參考資料:
        #無.
        #備註:
        #無.
        */
        public static function getOutput(&$conf){

                #初始化要回傳的內容
                $result=array();

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

                #如果 $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[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
                #$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
                #$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息
                #必填寫的參數:
                #$conf["variableCheck.checkArguments"]["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
                $conf["variableCheck.checkArguments"]["varInput"]=&$conf;
                #$conf["variableCheck.checkArguments"]["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
                $conf["variableCheck.checkArguments"]["mustBeFilledVariableName"]=array("code2run");
                #$conf["variableCheck.checkArguments"]["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double");
                $conf["variableCheck.checkArguments"]["mustBeFilledVariableType"]=array("array");
                #$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
                $conf["variableCheck.checkArguments"]["referenceVarKey"]="variableCheck.checkArguments";
                #可以省略的參數:
                #$conf["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"false"。
                #$conf["canBeEmptyString"]="false";
                #$conf["variableCheck.checkArguments"]["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
                $conf["variableCheck.checkArguments"]["skipableVariableName"]=array("varPassed");
                #$conf["variableCheck.checkArguments"]["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
                $conf["variableCheck.checkArguments"]["skipableVariableType"]=array(null);
                #$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,"null"代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
                $conf["variableCheck.checkArguments"]["skipableVarDefaultValue"]=array(null);
                $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["passed"]==="false"){

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

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

                        #回傳節果
                        return $result;

                        }#if end

                #開始將輸出存入buffer
                ob_start();

                #取得要在eval中使用的變數
                $varPassed=&$conf["varPassed"];

                #針對每段要執行的程式
                foreach($conf["code2run"] as $phpCode){

                        #如果不是字串
                        if(gettype($phpCode)!=="string"){

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

                                #設置執行錯誤
                                $result["error"]="element of code2run should be String";

                                #回傳節果
                                return $result;

                                }#if end

                        #若不為「;」結尾
                        if($phpCode[strlen($phpCode)-1]!==";"){

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

                                #設置執行錯誤
                                $result["error"]="element of code2run should be end with 「;」";

                                #回傳節果
                                return $result;

                                }#if end

                        #運行php程式
                        eval($phpCode);

                        }#foreach end

                #取得buffer的內容
                $result["content"]=ob_get_contents();

                #清空buffer,結束buffer的使用
                ob_end_clean();

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

                #回傳結果
                return $result;

                }#function getOutput end

        }#class record end

?>