Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
256 liveuser 1
<?php
2
 
3
/*
4
 
5
	QBPWCF, Quick Build PHP website Component base on Fedora Linux.
6
    Copyright (C) 2014~2026 MIN ZHI, CHEN
7
 
8
    This file is part of QBPWCF.
9
 
10
    QBPWCF is free software: you can redistribute it and/or modify
11
    it under the terms of the GNU General Public License as published by
12
    the Free Software Foundation, either version 3 of the License, or
13
    (at your option) any later version.
14
 
15
    QBPWCF is distributed in the hope that it will be useful,
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
    GNU General Public License for more details.
19
 
20
    You should have received a copy of the GNU General Public License
21
    along with QBPWCF.  If not, see <http://www.gnu.org/licenses/>.
22
 
23
*/
24
 
25
#使用命名空間qbpwcf
26
namespace qbpwcf;
27
 
28
#assets dir
29
$assetsDir="assets_of_zerossl::verifyDomain_20260206";
30
 
31
#要求認證並取得ssl憑證的domain name.
32
$domain="qbpwcf.org";
33
 
34
#dns zone file 
35
$dnsRecordFile="../../../../etc/named/db.qbpwcf.org";
36
 
37
#dns server ip
38
$remoteIp="169.254.2.2";
39
 
40
#dns server ssh username
41
$remoteUser="root";
42
 
43
/*
44
 
45
#函式說明:
46
#產生certificate sign request(CSR).
47
#回傳結果:
48
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
49
#$reuslt["error"],執行不正常結束的錯訊息陣列.
50
#$result["function"],當前執行的函式名稱.
51
#$result["argu"],所使用的參數.
52
#$result["content"],csr內容.
53
#$result["curl_verbose_info"],curl執行的詳細資訊.
54
#必填參數:
55
#$conf["domain"],字串,csr內容的網域名稱.
56
$conf["domain"]=$domain;
57
#可省略參數:
58
#$conf["country"],字串,csr內容的國家名稱,預設為"TW".
59
#$conf["country"]="";
60
#$conf["state"],字串,csr內容的State名稱,預設為"Taiwan".
61
#$conf["state"]="";
62
#$conf["org"],字串,csr內容的組織名稱,預設為"QBPWCF".
63
#$conf["org"]="";
64
#$conf["unit"],字串,csr內容的組織所屬單位,預設為"RD".
65
#$conf["unit"]="";
66
#參考資料:
67
#無.
68
#備註:
69
#無.
70
$createCSR=zerossl::createCSR($conf);
71
unset($conf);
72
 
73
#如果執行異常
74
if($createCSR["status"]==="false"){
75
 
76
	#debug
77
	var_dump($createCSR);
78
 
79
	#結束執行回傳1
80
	exit(1);
81
 
82
	}#if end
83
 
84
#函式說明:
85
#提供certificate sign request(CSR)透過zerossl產生cergificate.
86
#回傳結果:
87
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
88
#$reuslt["error"],執行不正常結束的錯訊息陣列.
89
#$result["function"],當前執行的函式名稱.
90
#$result["argu"],所使用的參數.
91
#$result["curl_verbose_info"],curl執行的詳細資訊.
92
#$result["id"],用於zerossl api驗證domain的id.
93
#$result["content"],字串,得到的json字串回應.
94
#$result["cnameKey"],用於dns CNAME驗證鍵名.
95
#$result["cnameVal"],用於dms CNAME驗證鍵值.
96
#必填參數:
97
#$conf["certificate_domains"],字串,要簽署的doamin名稱.
98
$conf["certificate_domains"]=$domain;
99
#$conf["certificate_csr"],字串,簽署的資訊.
100
$conf["certificate_csr"]=$createCSR["content"];
101
#$conf["fileArgu"],字串,變數__FILE__的內容.
102
$conf["fileArgu"]=__FILE__;
103
#可省略參數:
104
#$conf["apiDomain"],字串,提供服務的domain,預設為 self::getApiInfo()["doamin"].
105
#$conf["apiDomain"]=self::getApiInfo()["doamin"];
106
#$conf["path"],字串,提供服務的path,預設為 self::getApiInfo()["csrPath"].
107
#$conf["path"]=self::getApiInfo()["csrPath"];
108
#$conf["key"],字串,api key,預設為 self::getApiInfo()["apiKey"].
109
#$conf["key"]=self::getApiInfo()["apiKey"];
110
#參考資料:
111
#無.
112
#備註:
113
#無.
114
$createCertificate=zerossl::createCertificate($conf);
115
unset($conf);
116
 
117
#如果執行異常
118
if($createCertificate["status"]==="false"){
119
 
120
	#debug
121
	var_dump($createCertificate);
122
 
123
	#結束執行回傳1
124
	exit(1);
125
 
126
	}#if end
127
 
128
#certificate id
129
$createCertificate["id"];
130
 
131
#CNAME query name
132
$createCertificate["cnameKey"];
133
 
134
#CNAME query result
135
$createCertificate["cnameVal"];
136
 
137
#初始化要給 zerossl::updateDnsRecordFile 的 add 參數
138
$rr2add=array();
139
 
140
#設置為 CNAME
141
$rr2add["type"]="CNAME";
142
 
143
#設置 query 內容
144
$rr2add["query"]=$createCertificate["cnameKey"];
145
 
146
#設置 value 內容
147
$rr2add["value"]=$createCertificate["cnameVal"];
148
 
149
#函式說明:
150
#新增DNS記錄到檔案裡面.
151
#回傳結果:
152
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
153
#$reuslt["error"],執行不正常結束的錯訊息陣列.
154
#$result["function"],當前執行的函式名稱.
155
#$result["argu"],所使用的參數.
156
#$result["content"],字串陣列,更新後的檔案RR(DNS記錄).
157
#$result["content"]["domain"],字串,RR所屬domain.
158
#$result["content"]["defaultTTL"],字串,預設的RR更新時間.
159
#$result["comment"],字串陣列,逐行的註解.
160
#必填參數:
161
#$conf["add"],多維陣列,每個元素有為有三個元素的陣列,key有"query"跟"update"跟"type"跟"value"以及"comment",分別代表要查詢的數值、更新周期、類型、答案、註解.
162
$conf["add"]=array($rr2add);
163
#$conf["dnsRecordFile"],字串,該ZONE檔案位置與名稱.
164
$conf["dnsRecordFile"]=$dnsRecordFile;
165
#可省略參數:
166
#$conf["writeNow"],字串,是否要直接寫入既有ZONE檔案,預設為"false",不寫入;"true"為直接覆寫內容.
167
$conf["writeNow"]="true";
168
#$conf["debug"],字串,是否要開啟debug模式,預設為"false"不開啟;反之為"true"要開啟,會在/tmp/建立debug檔案zerossl\:\:updateDnsRecordFile.
169
#$conf["debug"]="false";
170
#參考資料:
171
#無.
172
#備註:
173
#目前支援的DNS記錄檔案是給bind(Berkeley Internet Name Domain)套件使用的.
174
#通常qbpwcf使用者沒有權限存取RR(dns記錄檔案),本套件是透過sshfs掛載來繞過該問題.
175
#php-pear上的Net_DNS2也可以解析RR.
176
$updateDnsRecordFile=zerossl::updateDnsRecordFile($conf);
177
unset($conf);
178
 
179
#如果執行異常
180
if($updateDnsRecordFile["status"]==="false"){
181
 
182
	#debug
183
	var_dump($updateDnsRecordFile);
184
 
185
	#結束執行回傳1
186
	exit(1);
187
 
188
	}#if end
189
 
190
#連線到dns server,下達指令重新啟動dns server.
191
#函式說明:
192
#呼叫shell執行系統命令,並取得回傳的內容.
193
#回傳結果:
194
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
195
#$result["error"],錯誤訊息陣列.
196
#$result["function"],當前執行的函數名稱.
197
#$result["argu"],使用的參數.
198
#$result["cmd"],執行的指令內容.
199
#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
200
#$result["output"],爲執行完後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
201
#$result["content"],為執行完後的輸出字串.
202
#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
203
#$result["running"],是否還在執行.
204
#$result["pid"],pid.
205
#$result["statusCode"],執行結束後的代碼.
206
#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
207
#$result["noEcaped"],陣列,儲存重新排序過未經過escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
208
#必填參數:
209
#$conf["command"],字串,要執行的指令.
210
$conf["command"]="systemctl";
211
#$conf["fileArgu"],字串,變數__FILE__的內容.
212
$conf["fileArgu"]=__FILE__;
213
#可省略參數:
214
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
215
$conf["argu"]=array("restart","named");
216
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
217
#$conf["arguIsAddr"]=array();
218
#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
219
#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
220
#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
221
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
222
#$conf["enablePrintDescription"]="true";
223
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
224
#$conf["printDescription"]="";
225
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
226
$conf["escapeshellarg"]="true";
227
#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
228
#$conf["thereIsShellVar"]=array();
229
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
230
#$conf["username"]="";
231
#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
232
#$conf["password"]="";
233
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
234
#$conf["useScript"]="";
235
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
236
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
237
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
238
#$conf["inBackGround"]="";
239
#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
240
#$conf["getErr"]="false";
241
#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
242
#$conf["doNotRun"]="false";
243
#$conf["remoteIp"],字串,遠端ip或domainName,預設不使用,若存在則會透過ssh指令進行連線,然後執行指令.
244
$conf["remoteIp"]=$remoteIp;
245
#$conf["remoteUser"],字串,遠端的使用者賬戶,預設不指定,亦即跟為運行該php程式的使用者.
246
$conf["remoteUser"]=$remoteUser;
247
#參考資料:
248
#exec=>http://php.net/manual/en/function.exec.php
249
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
250
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
251
#備註:
252
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
253
#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
254
$callShell=external::callShell($conf);
255
unset($conf);
256
 
257
#如果執行異常
258
if($callShell["status"]==="false"){
259
 
260
	#debug
261
	var_dump($callShell);
262
 
263
	#結束執行回傳1
264
	exit(1);
265
 
266
	}#if end
267
 
268
*/
269
 
270
#函式說明:
271
#請求驗證已經透過zerossl::createCertificate要求的domain.
272
#回傳結果:
273
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
274
#$reuslt["error"],執行不正常結束的錯訊息陣列.
275
#$result["function"],當前執行的函式名稱.
276
#$result["argu"],所使用的參數.
277
#$result["curl_verbose_info"],curl執行的詳細資訊.
278
#$result["content"],字串,得到的json字串回應.
279
#必填參數:
280
#$conf["id"],字串,zerossl::createCertificate回傳的id.
281
#$conf["id"]=$createCertificate["id"];
282
$conf["id"]="1c88454b0004cb8a12b1c35cdd5eaf64";
283
#可省略參數:
284
#$conf["apiDomain"],字串,提供服務的domain,預設為 self::getApiInfo()["doamin"].
285
#$conf["apiDomain"]=self::getApiInfo()["doamin"];
286
#$conf["key"],字串,api key,預設為 self::getApiInfo()["apiKey"].
287
#$conf["key"]=self::getApiInfo()["apiKey"];
288
#參考資料:
289
#無.
290
#備註:
291
#無.
292
$verifyDomain=zerossl::verifyDomain($conf);
293
unset($conf);
294
 
295
#debug
296
var_dump($verifyDomain);