Subversion Repositories php-qbpwcf

Rev

Blame | 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_zerossl::verifyDomain_20260206";

#要求認證並取得ssl憑證的domain name.
$domain="qbpwcf.org";

#dns zone file 
$dnsRecordFile="../../../../etc/named/db.qbpwcf.org";

#dns server ip
$remoteIp="169.254.2.2";

#dns server ssh username
$remoteUser="root";

/*

#函式說明:
#產生certificate sign request(CSR).
#回傳結果:
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
#$reuslt["error"],執行不正常結束的錯訊息陣列.
#$result["function"],當前執行的函式名稱.
#$result["argu"],所使用的參數.
#$result["content"],csr內容.
#$result["curl_verbose_info"],curl執行的詳細資訊.
#必填參數:
#$conf["domain"],字串,csr內容的網域名稱.
$conf["domain"]=$domain;
#可省略參數:
#$conf["country"],字串,csr內容的國家名稱,預設為"TW".
#$conf["country"]="";
#$conf["state"],字串,csr內容的State名稱,預設為"Taiwan".
#$conf["state"]="";
#$conf["org"],字串,csr內容的組織名稱,預設為"QBPWCF".
#$conf["org"]="";
#$conf["unit"],字串,csr內容的組織所屬單位,預設為"RD".
#$conf["unit"]="";
#參考資料:
#無.
#備註:
#無.
$createCSR=zerossl::createCSR($conf);
unset($conf);

#如果執行異常
if($createCSR["status"]==="false"){

        #debug
        var_dump($createCSR);
        
        #結束執行回傳1
        exit(1);

        }#if end
        
#函式說明:
#提供certificate sign request(CSR)透過zerossl產生cergificate.
#回傳結果:
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
#$reuslt["error"],執行不正常結束的錯訊息陣列.
#$result["function"],當前執行的函式名稱.
#$result["argu"],所使用的參數.
#$result["curl_verbose_info"],curl執行的詳細資訊.
#$result["id"],用於zerossl api驗證domain的id.
#$result["content"],字串,得到的json字串回應.
#$result["cnameKey"],用於dns CNAME驗證鍵名.
#$result["cnameVal"],用於dms CNAME驗證鍵值.
#必填參數:
#$conf["certificate_domains"],字串,要簽署的doamin名稱.
$conf["certificate_domains"]=$domain;
#$conf["certificate_csr"],字串,簽署的資訊.
$conf["certificate_csr"]=$createCSR["content"];
#$conf["fileArgu"],字串,變數__FILE__的內容.
$conf["fileArgu"]=__FILE__;
#可省略參數:
#$conf["apiDomain"],字串,提供服務的domain,預設為 self::getApiInfo()["doamin"].
#$conf["apiDomain"]=self::getApiInfo()["doamin"];
#$conf["path"],字串,提供服務的path,預設為 self::getApiInfo()["csrPath"].
#$conf["path"]=self::getApiInfo()["csrPath"];
#$conf["key"],字串,api key,預設為 self::getApiInfo()["apiKey"].
#$conf["key"]=self::getApiInfo()["apiKey"];
#參考資料:
#無.
#備註:
#無.
$createCertificate=zerossl::createCertificate($conf);
unset($conf);

#如果執行異常
if($createCertificate["status"]==="false"){

        #debug
        var_dump($createCertificate);
        
        #結束執行回傳1
        exit(1);

        }#if end

#certificate id
$createCertificate["id"];

#CNAME query name
$createCertificate["cnameKey"];

#CNAME query result
$createCertificate["cnameVal"];

#初始化要給 zerossl::updateDnsRecordFile 的 add 參數
$rr2add=array();

#設置為 CNAME
$rr2add["type"]="CNAME";

#設置 query 內容
$rr2add["query"]=$createCertificate["cnameKey"];

#設置 value 內容
$rr2add["value"]=$createCertificate["cnameVal"];

#函式說明:
#新增DNS記錄到檔案裡面.
#回傳結果:
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
#$reuslt["error"],執行不正常結束的錯訊息陣列.
#$result["function"],當前執行的函式名稱.
#$result["argu"],所使用的參數.
#$result["content"],字串陣列,更新後的檔案RR(DNS記錄).
#$result["content"]["domain"],字串,RR所屬domain.
#$result["content"]["defaultTTL"],字串,預設的RR更新時間.
#$result["comment"],字串陣列,逐行的註解.
#必填參數:
#$conf["add"],多維陣列,每個元素有為有三個元素的陣列,key有"query"跟"update"跟"type"跟"value"以及"comment",分別代表要查詢的數值、更新周期、類型、答案、註解.
$conf["add"]=array($rr2add);
#$conf["dnsRecordFile"],字串,該ZONE檔案位置與名稱.
$conf["dnsRecordFile"]=$dnsRecordFile;
#可省略參數:
#$conf["writeNow"],字串,是否要直接寫入既有ZONE檔案,預設為"false",不寫入;"true"為直接覆寫內容.
$conf["writeNow"]="true";
#$conf["debug"],字串,是否要開啟debug模式,預設為"false"不開啟;反之為"true"要開啟,會在/tmp/建立debug檔案zerossl\:\:updateDnsRecordFile.
#$conf["debug"]="false";
#參考資料:
#無.
#備註:
#目前支援的DNS記錄檔案是給bind(Berkeley Internet Name Domain)套件使用的.
#通常qbpwcf使用者沒有權限存取RR(dns記錄檔案),本套件是透過sshfs掛載來繞過該問題.
#php-pear上的Net_DNS2也可以解析RR.
$updateDnsRecordFile=zerossl::updateDnsRecordFile($conf);
unset($conf);

#如果執行異常
if($updateDnsRecordFile["status"]==="false"){

        #debug
        var_dump($updateDnsRecordFile);
        
        #結束執行回傳1
        exit(1);

        }#if end

#連線到dns server,下達指令重新啟動dns server.
#函式說明:
#呼叫shell執行系統命令,並取得回傳的內容.
#回傳結果:
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
#$result["error"],錯誤訊息陣列.
#$result["function"],當前執行的函數名稱.
#$result["argu"],使用的參數.
#$result["cmd"],執行的指令內容.
#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
#$result["output"],爲執行完後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
#$result["content"],為執行完後的輸出字串.
#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
#$result["running"],是否還在執行.
#$result["pid"],pid.
#$result["statusCode"],執行結束後的代碼.
#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
#$result["noEcaped"],陣列,儲存重新排序過未經過escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
#必填參數:
#$conf["command"],字串,要執行的指令.
$conf["command"]="systemctl";
#$conf["fileArgu"],字串,變數__FILE__的內容.
$conf["fileArgu"]=__FILE__;
#可省略參數:
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
$conf["argu"]=array("restart","named");
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
#$conf["arguIsAddr"]=array();
#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
#$conf["enablePrintDescription"]="true";
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
#$conf["printDescription"]="";
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
$conf["escapeshellarg"]="true";
#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
#$conf["thereIsShellVar"]=array();
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
#$conf["username"]="";
#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
#$conf["password"]="";
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
#$conf["useScript"]="";
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
#$conf["inBackGround"]="";
#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
#$conf["getErr"]="false";
#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
#$conf["doNotRun"]="false";
#$conf["remoteIp"],字串,遠端ip或domainName,預設不使用,若存在則會透過ssh指令進行連線,然後執行指令.
$conf["remoteIp"]=$remoteIp;
#$conf["remoteUser"],字串,遠端的使用者賬戶,預設不指定,亦即跟為運行該php程式的使用者.
$conf["remoteUser"]=$remoteUser;
#參考資料:
#exec=>http://php.net/manual/en/function.exec.php
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
#備註:
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
$callShell=external::callShell($conf);
unset($conf);

#如果執行異常
if($callShell["status"]==="false"){

        #debug
        var_dump($callShell);
        
        #結束執行回傳1
        exit(1);

        }#if end

*/

#函式說明:
#請求驗證已經透過zerossl::createCertificate要求的domain.
#回傳結果:
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
#$reuslt["error"],執行不正常結束的錯訊息陣列.
#$result["function"],當前執行的函式名稱.
#$result["argu"],所使用的參數.
#$result["curl_verbose_info"],curl執行的詳細資訊.
#$result["content"],字串,得到的json字串回應.
#必填參數:
#$conf["id"],字串,zerossl::createCertificate回傳的id.
#$conf["id"]=$createCertificate["id"];
$conf["id"]="1c88454b0004cb8a12b1c35cdd5eaf64";
#可省略參數:
#$conf["apiDomain"],字串,提供服務的domain,預設為 self::getApiInfo()["doamin"].
#$conf["apiDomain"]=self::getApiInfo()["doamin"];
#$conf["key"],字串,api key,預設為 self::getApiInfo()["apiKey"].
#$conf["key"]=self::getApiInfo()["apiKey"];
#參考資料:
#無.
#備註:
#無.
$verifyDomain=zerossl::verifyDomain($conf);
unset($conf);

#debug
var_dump($verifyDomain);