Subversion Repositories php-qbpwcf

Rev

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

*/

/*

本程式說明:

將通過email驗證後的用戶,將其填寫好的表單資訊寄送給管理者的系統.

1.使用者提供email進行認證
1-1. web socket 等待 server 通知認證通過

2.使用者點選email中的一次性超連結認證
2-1. 超連結的頁面會讓 server 跟 web socket client 端說通過認證了.
2-1-1. 若重複拜訪該頁面將無效果.
2-1-2. 若之前有同 email 的一次性超連結認證未註銷,則註銷之.
2-1-3. 若距離產生一次性超連結時間大於300秒,則無效且註銷之.

3.頁面呈現其他資訊表單給使用者填寫

4.表單填寫好送出後,該一次性表單就註銷,然後管理者就會收到表單內容的 email.

*/

#使用命名空間qbpwcf
namespace qbpwcf;

#初始化輸出
$output=array();

#取得 lib path
exec("php -f ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/../../../../../usr/bin/libexec/folderOfUsrLib.php"),$output,$status);

#如果執行失敗
if($status!==0){

        #debug
        var_dump(__LINE__,$output);

        #結束執行,回傳shell 1.
        exit(1);

        }#if end

#儲存lib path
$folderOfUsrLib=$output[0];

#初始化輸出
$output=array();

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

#匯入套件
require_once("allInOneForJson.php");

#建議的log位置
$logFile=$_SERVER["DOCUMENT_ROOT"].$_SERVER["PHP_SELF"].".log";

#取得用戶端資訊
#函式說明:
#取得用戶端的資訊,並依據需要寫入到資料表裡面
#回傳的結果:
#$result["status"],執行是否正常,"true"代表執行成功,"false"代表執行失敗.
#$result["error"],錯誤訊息.
#$result["function"],檔前執行的函數名稱.
#$result["mode"],當前的模式是"cmd"還是"web".
#$result["userBrowserType"],爲使用者的瀏覽器資訊
#$result["userIp"],爲使用者的IP
#$result["serverIp"],為伺服器的IP
#$result["server_name"],伺服器的 domain name
#$result["scheme"],通訊協定
#$result["serverPort"],伺服器給對外下載網頁的port
#$result["requestUri"],爲使用者要求的網址/php檔案.
#$result["requestUriFull"],為使用者要求的完整網址/php檔案路徑.
#$result["clientRequestIP"],用戶端要求的ip與port
#$result["username"],爲使用者目前的帳戶,若爲""則表示尚未登入成功
#$result["phpUser"],運行該php的使用者帳戶.若為空字串則代表非使用者直接觸發.
#$result["phpUserType"],運行該php的使用者帳戶類型,可能有"regular(no wheel member)","wheel(can use sudo)","intrinsic(root)","system(qemu,apache,...)".
#$result["header"],接收到的 header 陣列.
#$result["body"],接收到的 body 字串.
#必填參數:
#$conf["getAccount"],字串,是否要取得帳號,"true"代表要;"false"代表不要.
$conf["getAccount"]="false";
#可省略參數:
#$conf["accountVar"],字串,帳號儲存在哪個變數裏面,預設爲$_SESSION["username"].
#$conf["accountVar"]=$_SESSION["username"];
#$conf["saveToDb"],字串,是否要除儲存到資料庫,"true"為要儲存",預設為"false"不儲存.
#$conf["saveToDb"]="true";
#$conf["dbAddress"],字串,爲mysql/mariadb server的位置,若saveToDb設為"true",則該參數為必填.
#$conf["dbAddress"]=$dbAddress;
#$conf["dbAccount"],字串,爲用於連入mysql/mariadb server時要使用的帳號,若saveToDb設為"true",則該參數為必填.
#$conf["dbAccount"]=$dbAccount;
#$conf["dbName"],字串,要選取的資料庫名稱,若saveToDb設為"true",則該參數為必填.
#$conf["dbName"]=$dbName;
#$conf["tableName"],字串,爲要插入資料的資料表名稱,若saveToDb設為"true",則該參數為必填.
#$conf["tableName"]="visitorInfo";
#$conf["columnName"],字串陣列,爲資料表的項目名稱,例如:$conf["columnName"]=array("columnName1","columnName2","columnName3",...);寫入的資料依序為,使用者帳戶、瀏覽器資訊、使用者IP、觀看的網址、當時的時間.若saveToDb設為"true",則該參數為必填.
#$conf["columnName"]=array("username","userWebBrowser","userIp","requestUri","systemDateAndTime");
#$conf["dbPassword"],字串,爲連線到mysql/mariadb server時要使用的密碼,可省略,若省略則代表不使用密碼.
#$conf["dbPassword"]=$dbPassword;
#參考資料:
#$_SERVER=>http://php.net/manual/zh/reserved.variables.server.php
#取得伺服器名稱與IP=>http://php.net/manual/en/function.gethostname.php
#備註:
#無.
$getConnectionInfo=csInformation::getConnectionInfo($conf);
unset($conf);

#如果執行失敗
if($getConnectionInfo["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"]=$getConnectionInfo;
        #$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

#如果有 body
if(!empty($getConnectionInfo["body"])){
        
        #異常結束
        exit(1);

        }#if end

#取得用戶的瀏覽器資訊
$clientAgent=$getConnectionInfo["userBrowserType"];

#取得用戶的ip
$clientIp=$getConnectionInfo["userIp"];

#取得用戶的heade
$clientHeader=$getConnectionInfo["header"];



#等待1秒,避免太頻繁運行
#sleep(1);

#函式說明:
#抓取命令列的參數.
#回傳結果:
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
#$reuslt["error"],執行不正常結束的錯訊息陣列.
#$result["function"],當前執行的函式名稱.
#$result["argu"],使用的參數陣列.
#$result["content"],要回傳的參數陣列.
#$result["count"],參數的數量.
#$result["_GET"],收到的 HTTP GET 參數陣列.
#$result["_POST"],收到的 HTTP POST 參數陣列.
#必填參數:
#無
#可省略參數:
#$conf["echo"],字串,"true"代表要將抓到的參數一個個印出來,"false"代表用回傳的方式,預設為"false".
#$conf["echo"]="false";
#$conf["httpGetToArgu"],字串,"true"代表要將 http get 參數變成參數來取得,像是 $_GET["p1"] 會變成 $result["content"]["p1"] 回傳,預設為"false"代表不使用.
$conf["httpGetToArgu"]="true";
#$conf["httpPostToArgu"],字串,"true"代表要將 http post 參數變成參數來取得,像是 $_POST["p1"] 會變成 $result["content"]["p1"] 回傳,預設為"false"代表不使用.
#$conf["httpPostToArgu"]="false";
#$conf["arguEqual2http"],字串,預設為"false",代表命令列環境的參數依序為程式名稱本身、第n個參數,反之為"true",代表命令列的第一個參數為第n個參數,亦即沒有程式本身名稱的參數.
$conf["arguEqual2http"]="true";
#參考資料:
#無.
#備註:
#pipe要用''包住,才會被解析,例如'|'、'>'、'>>'.
$getArgu=cmd::getArgu($conf);
unset($conf);

#函式說明:
#將要執行的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"];