Subversion Repositories qbpwcf-lib(archive)

Rev

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

#!/bin/php
<?php

/*
        QBPWCF, Quick Build PHP website Component base on Fedora Linux.
    Copyright (C) 2015~2024 Min-Jhin,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/>.
    
*/

/*
說明:
用 \qbpwcf\sock::unixDomainSockServer 建立 unix doamin socket server

範例:
...
*/

#使用命名空間qbpwcf
namespace qbpwcf;

#以該檔案的實際位置的 lib path 為 include path 首位
exec("cd ".pathinfo(__FILE__)["dirname"]."/../lib;pwd;",$output,$status);
set_include_path($output[0].PATH_SEPARATOR.get_include_path());

#匯入外部套件
include("allInOne.php");

#說明函式
function help(){

        #印出指令說明
        echo "Usage of ".basename(__FILE__).":".PHP_EOL; 
        echo "--sock unix domain socket 的位置與名稱.".PHP_EOL;
        echo "--addOnProcessFunc namespace\class::function 收到client訊息時要執行的函式.".PHP_EOL;
        echo "--funcToRunWhenIdle namespace\class::function 沒收到訊息時要執行的函式.".PHP_EOL;
        echo "--onlyOutputCache \"true\"代表輸出 sock::unixDomainSockServer 回傳的 serverCache 結果".PHP_EOL;
        echo "--cacheLayer 代表要輸出 sock::unixDomainSockServer 回傳的 serverCache 變數底下的陣列索引名稱,有幾層就使用幾次該參數".PHP_EOL;
        echo "--cacheLayerJson 代表要輸出 sock::unixDomainSockServer 回傳的 serverCache 變數底下的陣列索引名稱,數值為json_encode後urlencode後base64_encode後的字串".PHP_EOL;
        
        #結束執行
        exit;

        }#function help end

#函式說明:
#解析參數.
#回傳結果:
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
#$reuslt["error"],執行不正常結束的錯訊息陣列.
#$result["function"],當前執行的函式名稱.
#$result["content"],解析好的參數陣列.
#$result["content"][$key][$i],參數 $key 的 $i+1 個參數數值內容.
#$result["program"],字串,執行的程式名稱.
#必填參數:
#無
#可省略參數:
#$conf["helpFunc"],如果解析的參數不成對,則要執行的函式名稱.
$conf["helpFunc"]="help";
#備註:
#僅能在命令列底下執行.
#建議:
#以後可將參數 --a--b 的名稱與後面的數值 $value 存成 $result["a"]["b"][$i]=$value .
$parseArgu=cmd::parseArgu($conf);
unset($conf);

#如果解析參數失敗
if($parseArgu["status"]==="false")
{
        #印出結果
        var_dump($parseArgu);
        
        #結束執行
        exit(1);
        
}#if end

#預設的 unix domain socket
$defaultUnixDomainSocket="./".basename(__FILE__).".sock";

#檢查參數
#函式說明:
#檢查必填與可省略參數,可省略參數可指定預設要給與什麼數值內容。
#$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["varInput"]=&$parseArgu["content"];
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
$conf["referenceVarKey"]="variableCheck::checkArguments";
#可以省略的參數:
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
#$conf["mustBeFilledVariableName"]=array("sock","addOnProcessFunc","funcToRunWhenIdle");
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
#$conf["mustBeFilledVariableType"]=array("array","array","array");
#$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("sock","addOnProcessFunc","funcToRunWhenIdle","onlyOutputCache","cacheLayer");
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
$conf["skipableVariableName"]=array("sock","addOnProcessFunc","funcToRunWhenIdle","onlyOutputCache","cacheLayer");
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
$conf["skipableVariableType"]=array("array","array","array","array","array");
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
$conf["skipableVarDefaultValue"]=array(array($defaultUnixDomainSocket),null,null,"false",null);
#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
#$conf["variableCheck::checkArguments"]["disallowAllSkipableVarIsEmpty"]="false";
#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
#$conf["disallowAllSkipableVarIsEmptyArray"]="";
#$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);
unset($conf);

#若執行失敗
if($checkArguments["status"]==="false"){

        #印出結果
        var_dump($checkArguments);
        
        #結束執行
        exit(1);

        }#if end

#若檢查不通過
if($checkArguments["passed"]==="false"){

        #印出結果
        var_dump($checkArguments);
        
        #結束執行
        exit(1);

        }#if end

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

#函式說明:
#建立 unix domain socket server, 僅提供具備檔案存取權限的用戶使用,預設提供可以下達任何指令的功能.
#回傳結果:
#$result["status"],"true"代表執行正常;"false"代表執行不正常.
#$result["error"],錯誤訊息陣列.
#$result["function"],當前執行的函式名稱.
#$result["serverCache"],函式結束前,儲存在serverCache的內容,若有元素"exit"存在則代表是正常結束.
#必填參數:
#$conf["sock"],字串,socket檔案要放在哪邊,名稱為何.
$conf["sock"]=$parseArgu["content"]["sock"][0];
#$conf["fileArgu"],字串,變數__FILE__的內容.
$conf["fileArgu"]=__FILE__;
#可省略參數:
#$conf["changeOwner"],字串,要將socket檔案的擁有着權限進行修改."user.group"代表擁有者帳號為user,群組為group.
#$conf["changeOwner"]="";
#$conf["changePermission"],字串,要將socket檔案的權限設為多少.ex: "0666"(所有帳戶都有存取的權限) 或 "0660"(僅有擁有者與群組帳戶有存取的權限) 或 "0600"(只有擁有者有權限執行).
#$conf["changePermission"]="";
#$conf["sessionTimeout"],字串,當連線結束後於下一次連線間隔多久就算session timeout,server端會將記錄移除,client端需要重新拿取id,預設為300秒.
#$conf["sessionTimeout"]="300";

#如果有設置 addOnProcessFunc
if(isset($parseArgu["content"]["addOnProcessFunc"])){

        #$conf["addOnProcessFunc"],字串陣列,增加用於處理 json request 的函式名稱,給予的參數為array("request"=>收到的json訊息,"sock"=>用戶的socket),回傳的內容必須為陣列,例如 $res["continue"]="true"代表要繼續執行下一個addOnProcessFunc;"false"代表代表到此為止. $res["content"]="replaced content";代表要將收到的訊息取代成"replaced content". 最少要有回傳 $res["status"]數值"true"代表執行正常;"false"代表執行不正常.
        $conf["addOnProcessFunc"]=$parseArgu["content"]["addOnProcessFunc"];

        }#if end

if(isset($parseArgu["content"]["funcToRunWhenIdle"])){

        #$conf["funcToRunWhenIdle"],字串陣列,當沒有事件產生時,要執行的函式名稱,給予參數為array("client"=>所有用戶,"clientCache"=>$result["serverCache"]["clientSide"]["addOnProcessFunc"][$funcToRunWhenIdle],"serverCache"=>$result["serverCache"]["serverSide"]["funcToRunWhenIdle"][$funcToRunWhenIdle]).
        $conf["funcToRunWhenIdle"]=$parseArgu["content"]["funcToRunWhenIdle"];

        }#if end

#$conf["paramsForFuncToRunWhenIdle"],2維陣列,每個元素代表指定給予funcToRunWhenIdle參數中的指定元素的參數.
#$conf["paramsForFuncToRunWhenIdle"]=array();
#$conf["infoToFunction"],陣列,需要增加給addOnProcessFunc跟funcToRunWhenIdle函式的資訊,在函式中其參數的info鍵值.
#$conf["infoToFunction"]=array("debug"=>$debug);
#參考資料:
#http://php.net/manual/en/function.stream-socket-server.php
#備註:
#無.
$unixDomainSockServer=sock::unixDomainSockServer($conf);
unset($conf);

#debug,印出 cache 的內容,清空buffer
echo ob_get_contents();ob_clean();

#如果程式
if($unixDomainSockServer["status"]==="false"){

        #印出 cache 的內容
        echo ob_get_contents();
                        
        #清空buffer,結束buffer的使用
        ob_end_clean();

        #印出結果
        var_dump($unixDomainSockServer);
        
        #結束執行
        exit(1);

        }#if end

#如果有 onlyOutputCache 參數
if($parseArgu["content"]["onlyOutputCache"][0]==="true"){

        #debug
        #var_dump(__LINE__,$parseArgu["content"]);

        #如果不存在 serverCache
        if(!isset($unixDomainSockServer["serverCache"])){
        
                #印出 cache 的內容
                echo ob_get_contents();
                                
                #清空buffer,結束buffer的使用
                ob_end_clean();
        
                #印出結果
                var_dump($unixDomainSockServer);
                
                #結束執行並回傳1
                exit(1);
        
                }#if end
        
        #如果無設置 cacheLayer
        if(!(isset($parseArgu["content"]["cacheLayer"]))){
        
                #印出json結果
                echo json_encode($unixDomainSockServer["serverCache"]);
        
                #結束執行並回傳0
                exit;
        
                }#if end
        
        #反之
        else{
        
                #儲存當前的cache參考
                $currentCache=&$unixDomainSockServer["serverCache"];
                
                #debug,印出 cache 的內容,清空buffer,結束buffer的使用.
                #echo ob_get_contents();ob_end_clean();exit;
                
                #針對每個 cacheLayer
                foreach($parseArgu["content"]["cacheLayer"] as $cacheLayer){
                
                        #如果存在該階層
                        if(isset($currentCache[$cacheLayer])){
                        
                                #取得當前階層的參考
                                $currentCache=&$currentCache[$cacheLayer];
                        
                                }#if end
                
                        }#foreach end
                
                #清空buffer,結束buffer的使用
                ob_end_clean();
                
                #印出指定的cache內容
                echo json_encode($currentCache);
                
                #結束執行並回傳0
                exit;
        
                }#else end

        }#if end

#印出 cache 的內容
echo ob_get_contents();
                
#清空buffer,結束buffer的使用
ob_end_clean();

#debug
#var_dump(__LINE__,$parseArgu);exit;

#印出json結果
echo json_encode($unixDomainSockServer);