Subversion Repositories php-qbpwcf

Rev

Rev 323 | 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/>.

*/

#使用命名空間qbpwcf
namespace qbpwcf;

#assets dir
$assetsDir="assets of system_sendMailAgent_20250528";

#函式說明:
#將要執行的script語法透過該函式執行(會在程式外層用<script></script>包起來).
#回傳結果:
#$result["status"],執行是否正常,"true"為正常,"false"為不正常.
#$result["error"],錯誤訊息陣列
#$result["function"],當前執行的函數名稱
#$result["content"],要執行的javaScript語法
#必填參數:
#$conf["script"],字串,要執行的javaScript語法.
$conf["script"]="

        //需要認證email
        var mail='".systemMailAcct."';

        //將需要加密的內容 ajax 給 api
        //傳送表單
        /*
        設置 window.qbpwcf.ajaxP 函數
        說明:
        傳送ajax,指定傳送前要做什麼事情,然後回傳一個Promise.
        參數:
        params={};
        params.method='POST' or 'GET' or 'HEAD' or 'OPTIONS';
        params.headers={name:value};
        params.url='url to request';
        params.preAc='function name to call before send request';
        params.data='data to contain','key1=encodeURIComponent(val1)&key2=encodeURIComponent(val2)';if no data please use {};
        參考資料:
        https://eyesofkids.gitbooks.io/javascript-start-es6-promise/content/contents/ch11_snippets.html
        https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
        */
        ajaxP_params={};
        ajaxP_params.data='mail='+encodeURIComponent(mail);
        ajaxP_params.method='POST';
        ajaxP_params.url='../system/sendMailAgent/encrypt.php';
        var res = await window.qbpwcf.ajaxP.call(this,ajaxP_params);

        //如果執行異常
        if(res.status==='false'){

                //console erorr message
                console.error(res.error);
                
                //結束執行
                return;

                }//if end

        //如果不是合法的 json 格式
        if(res.compliantJson==='false'){

                //console erorr message
                console.error('invalid json response');
                
                //結束執行
                return;

                }//if end

        //debug
        //console.log(res);
        
        //另存 加密後的 session
        var sess=res.json.sess;

        //要傳送給 api 的 mail 資料
        var data={

                sid:sess,

                mail:res.json.post,

                }//data end

        //儲存用於識別用戶資源的id
        var wsClientId=crypto.randomUUID();

        //傳送要求認證的 ajax 給 api
        //傳送表單
        /*
        設置 window.qbpwcf.ajaxP 函數
        說明:
        傳送ajax,指定傳送前要做什麼事情,然後回傳一個Promise.
        參數:
        params={};
        params.method='POST' or 'GET' or 'HEAD' or 'OPTIONS';
        params.headers={name:value};
        params.url='url to request';
        params.preAc='function name to call before send request';
        params.data='data to contain','key1=encodeURIComponent(val1)&key2=encodeURIComponent(val2)';if no data please use {};
        參考資料:
        https://eyesofkids.gitbooks.io/javascript-start-es6-promise/content/contents/ch11_snippets.html
        https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
        */
        ajaxP_params={};
        ajaxP_params.data='sid='+encodeURIComponent(res.json.sess)+'&mail='+encodeURIComponent(res.json.post)+'&id='+encodeURIComponent(wsClientId);
        ajaxP_params.method='POST';
        ajaxP_params.url='../system/sendMailAgent/mail.php';
        var res = await window.qbpwcf.ajaxP.call(this,ajaxP_params);

        //如果執行異常
        if(res.status==='false'){

                //console erorr message
                console.error(res.error);
                
                //結束執行
                return;

                }//if end

        //如果不是合法的 json 格式
        if(res.compliantJson==='false'){

                //console erorr message
                console.error('invalid json response');
                
                //結束執行
                return;

                }//if end

        //連線到web socket,並傳輸資料,會回傳promise.亦即要在 async function 中使用,且使用 await 來取得結果.
        //params,參數物件.
        //params.url,web socket的url,加密的連線為wss://url,預設為當前頁面的 domain name/wss
        //params.callback,函式,參數為收到的訊息,收到訊息後要怎麼處理.
        //params.data,要傳輸的資料物件.
        //params.data.id,用於識別wss連線是屬於誰的id資訊,預設為自動產生.
        var params={};
        params.url='wss://".\system_sendMailAgent_ws_domain."';
        params.data={};
        params.data.sess=sess;
        params.data.id=wsClientId;
        var res=await window.qbpwcf.ws.init(params);
        
        //debug
        console.log(res);
        
        //如果來自server說認證通過
        while(res.from!='server' || res.msg!='authenticated'){
        
                //連線到web socket,並傳輸資料,會回傳promise.亦即要在 async function 中使用,且使用 await 來取得結果.
                //params,參數物件.
                //params.url,web socket的url,加密的連線為wss://url,預設為當前頁面的 domain name/wss
                //params.callback,函式,參數為收到的訊息,收到訊息後要怎麼處理.
                //params.data,要傳輸的資料物件.
                //params.data.id,用於識別wss連線是屬於誰的id資訊,預設為自動產生.
                var params={};
                params.url='wss://".\system_sendMailAgent_ws_domain."';
                params.data={};
                params.data.id=window.qbpwcf.ws.wss[params.url].params.data.id;
                res=await window.qbpwcf.ws.init(params);
        
                //debug
                console.log(res);
        
                }//if end
                
        //提示通過email認證
        console.log('email validated');
        
        ";
#可省略參數:
#$conf["onReady"],字串,是否要在網頁完全載入後再執行,"false"為不等載入完就先執行,預設為"true"要等載入完再執行.
#$conf["onReady"]="true";
#$conf["globalJs"],字串陣列,為要放入<script>標籤的js全域變數.
#$conf["globalJs"]=array();
#$conf["jsFunciton"],字串陣列,為要放入<script>標籤的js函數.
#$conf["jsFunciton"]=array();
#$conf["noScriptTag"],字串,是否不要輸出<script></script>,預設為"false",代表要輸出;反之為"true",代表不要輸出.
#$conf["noScriptTag"]="false";
#參考資料:
#http://stackoverflow.com/questions/9899372/pure-javascript-equivalent-to-jquerys-ready-how-to-call-a-function-when-the
#備註:
#無.
$toScript=javaScript::toScript($conf);
unset($conf);

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

        #函式說明:
        #撰寫log
        #回傳結果:
        #$result["status"],狀態,"true"或"false".
        #$result["error"],錯誤訊息陣列.
        #$result["function"],當前函式的名稱.
        #$result["argu"],使用的參數.
        #必填參數:
        #$conf["path"],字串,log檔案的路徑與名稱.
        $conf["path"]=$logFile;
        #$conf["content"],any,要寫的內容,若內容不為字串則會用var_dump的格式寫入.
        $conf["content"]=$toScript;
        #$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
        $conf["fileArgu"]=__FILE__;
        #可省略參數:
        #$conf["rewrite"],預設為"false",接續寫入;反之"true"代表重新寫入.
        #$conf["rewrite"]="false";
        #參考資料:
        #無.
        #備註:
        #無.
        $record=logs::record($conf);
        unset($conf);

        #如果寫log失敗
        if($record["status"]==="false"){

                #印出結果
                var_dump($record);

                }#if end

        #結束執行,回傳錯誤代碼1
        exit(1);

        }#if end

#debug
echo $toScript["content"];