Subversion Repositories php-qbpwcf

Rev

Rev 291 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
290 liveuser 1
#!/usr/bin/php
2
<?php
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
/*
26
透過zerossl api確保ssl沒有過期.
27
*/
28
 
29
#指派命名空間
30
namespace qbpwcf;
31
 
32
#取得 lib path
33
exec("php -f ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/libexec/folderOfUsrLib.php"),$output,$status);
34
 
35
#如果執行失敗
36
if($status!==0){
37
 
38
	#debug
39
	var_dump(__LINE__,$output);
40
 
41
	#結束執行,回傳shell 1.
42
	exit(1);
43
 
44
	}#if end
45
 
46
#儲存lib path
47
$folderOfUsrLib=$output[0];
48
 
49
#以該檔案的實際位置的 lib path 為 include path 首位
50
$output=array();
51
exec("cd ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/../".$folderOfUsrLib."/qbpwcf").";pwd;",$output,$status);
52
 
53
#如果執行失敗
54
if($status!==0){
55
 
56
	#debug
57
	var_dump(__LINE__,$output);
58
 
59
	#結束執行,回傳shell 1.
60
	exit(1);
61
 
62
	}#if end
63
 
64
#設置 include path
65
set_include_path($output[0].PATH_SEPARATOR.get_include_path());
66
 
67
#匯入外部套件
68
include("allInOne.php");
69
 
70
#當前lib路徑
71
$pwd=$output[0];
72
 
73
#unix socket 的位置
74
$unixSocket=$pwd."/../../../var/qbpwcf/".basename(__FILE__).".sock";
75
 
76
#說明函式
77
function help(){
78
 
79
	#印出指令說明
80
	echo "Usage of ".basename(__FILE__).":".PHP_EOL;
81
	echo "--sock [sslUpdater socket] unix domain socket 的位置與名稱,預設為相對於 QBPWCF套件的 \"/var/qbpwcf/sslUpdater.sock\"".PHP_EOL;
82
	echo "--daemon [\"on\"] 代表是要運行 server 端程式.".PHP_EOL;
83
	echo "--config [config file apth] 指定設定檔案的位置,預設為 相對於QBPWCF套件的 \"/etc/qbpwcf/sslUpdater.php\" 位置.".PHP_EOL;
291 liveuser 84
	echo "\tconfig 檔案的格式為一個網域名稱搭配一組變數".PHP_EOL;
293 liveuser 85
	echo "--multiThreads [\"on\"] 代表要啟用多執行序".PHP_EOL;
290 liveuser 86
	echo "--debug [\"on\"] 代表要開啟 debug 模式.".PHP_EOL;
87
 
88
	#結束執行
89
	exit;
90
 
91
	}#function help end
92
 
93
#函式說明:
94
#解析參數.
95
#回傳結果:
96
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
97
#$reuslt["error"],執行不正常結束的錯訊息陣列.
98
#$result["function"],當前執行的函式名稱.
99
#$result["content"],解析好的參數陣列.
100
#$result["content"][$key][$i],參數 $key 的 $i+1 個參數數值內容.
101
#$result["program"],字串,執行的程式名稱.
102
#必填參數:
103
#無
104
#可省略參數:
105
#$conf["helpFunc"],如果解析的參數不成對,則要執行的函式名稱.
106
$conf["helpFunc"]="help";
107
#備註:
108
#僅能在命令列底下執行.
109
#建議:
110
#以後可將參數 --a--b 的名稱與後面的數值 $value 存成 $result["a"]["b"][$i]=$value .
111
$parseArgu=cmd::parseArgu($conf);
112
unset($conf);
113
 
114
#如果解析參數失敗
115
if($parseArgu["status"]==="false"){
116
 
117
	#印出結果
118
	var_dump($parseArgu);
119
 
120
	#結束執行
121
	exit(1);
122
 
123
	}#if end
124
 
125
#檢查參數
126
#函式說明:
127
#檢查必填與可省略參數,可省略參數可指定預設要給與什麼數值內容。
128
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
129
#$reuslt["error"],執行不正常結束的錯訊息陣列.
130
#$result["function"],當前執行的函式名稱.
131
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
132
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
133
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
134
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
135
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
136
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
137
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
138
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
139
#必填寫的參數:
140
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
141
$conf["varInput"]=&$parseArgu["content"];
142
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
143
$conf["referenceVarKey"]="variableCheck::checkArguments";
144
#可以省略的參數:
145
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
146
#$conf["mustBeFilledVariableName"]=array("sock",);
147
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
148
#$conf["mustBeFilledVariableType"]=array("array");
149
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
150
#$conf["canBeEmptyString"]="false";
151
#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
152
#$conf["canNotBeEmpty"]=array();
153
#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
154
#$conf["canBeEmpty"]=array();
155
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
293 liveuser 156
$conf["skipableVariableCanNotBeEmpty"]=array("sock","daemon","config","debug","multiThreads");
290 liveuser 157
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
293 liveuser 158
$conf["skipableVariableName"]=array("sock","daemon","config","debug","multiThreads");
290 liveuser 159
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
293 liveuser 160
$conf["skipableVariableType"]=array("array","array","array","array","array");
290 liveuser 161
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
293 liveuser 162
$conf["skipableVarDefaultValue"]=array(array(null),array(null),array($pwd."/../../../etc/qbpwcf/sslUpdater.php"),array("on"),array(null));
290 liveuser 163
#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
164
#$conf["variableCheck::checkArguments"]["disallowAllSkipableVarIsEmpty"]="false";
165
#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
166
#$conf["disallowAllSkipableVarIsEmptyArray"]="";
167
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
168
#$conf["arrayCountEqualCheck"][]=array();
169
#參考資料來源:
170
#array_keys=>http://php.net/manual/en/function.array-keys.php
171
$checkArguments=variableCheck::checkArguments($conf);
172
unset($conf);
173
 
174
#若執行失敗
175
if($checkArguments["status"]==="false"){
176
 
177
	#印出結果
178
	var_dump($checkArguments);
179
 
180
	#結束執行
181
	exit(1);
182
 
183
	}#if end
184
 
185
#若檢查不通過
186
if($checkArguments["passed"]==="false"){
187
 
188
	#印出結果
189
	var_dump($checkArguments);
190
 
191
	#結束執行
192
	exit(1);
193
 
194
	}#if end
195
 
196
#取得debug flag
197
$debug=$parseArgu["content"]["debug"][0];
198
 
199
#如果是 debug 模式
200
if($debug==="on"){
201
 
202
	#設置為 true
203
	$debug=true;
204
 
205
	}#if end
206
 
207
#反之
208
else{
209
 
210
	#設置為 false
211
	$debug=false;
212
 
213
	}#else end
214
 
215
#取得設定檔案路徑與名稱
216
$config=$parseArgu["content"]["config"][0];
217
 
218
#檢查設定檔案是否存在
219
#函式說明:
220
#檢查多個檔案與資料夾是否存在.
221
#回傳的結果:
222
#$result["status"],執行正常與否,"true"代表正常,"false"代表不正常.
223
#$result["error"],錯誤訊息陣列.
224
#$resutl["function"],當前執行的涵式名稱.
225
#$result["argu"],使用的參數.
226
#$result["allExist"],所有檔案皆存在的識別,"true"代表皆存在,"false"代表沒有全部都存在.
227
#$result["varName"][$i],爲第$i個資料夾或檔案的路徑與名稱。
228
#$result["varNameFullPath"][$i],爲第$i個資料夾或檔案的完整檔案系統路徑與名稱,如果不存在則代表路徑是網址.
229
#$result["varNameWebPath"][$i],為第$i個資料夾或檔案的網址,若"web"參數為"true",才會有該內容.
230
#$result["varExist"][$i],爲第$i個資料夾或檔案是否存在,"true"代表存在,"false"代表不存在。
231
#必填參數:
232
#$conf["fileArray"],陣列字串,要檢查是否存在的檔案有哪些,須爲一維陣列數值。
233
$conf["fileArray"]=array($config);
234
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
235
$conf["fileArgu"]=__FILE__;
236
#可省略參數:
237
#$conf["disableWebSearch"],"字串",是否取消「當檔案找不到時,改用catchWebContent類別的wget函數來檢查檔案是否存在於網路上」的功能,"false"不取消,若要取消該功能請設為"true",若抓到的內容為空字串則會視為檔案不存在,預設為"true".
238
$conf["disableWebSearch"]="true";
239
#$conf["userDir"],字串,網頁是否置放於家目錄底下,"true"為是,"false"為不是,預設為"true".
240
$conf["userDir"]="false";
241
#$conf["web"],字串,檔案是放在web就是"true",反之為檔案系統"false",預設為"true".
242
$conf["web"]="false";
243
#參考資料:
244
#http://php.net/manual/en/function.file-exists.php
245
#http://php.net/manual/en/control-structures.foreach.php
246
#備註:
247
#函數file_exists檢查的路徑為檔案系統的路徑
248
#$result["varName"][$i]結果未實作
249
$checkMultiFileExist=fileAccess::checkMultiFileExist($conf);
250
unset($conf);
251
 
252
#若執行異常
253
if($checkMultiFileExist["status"]==="false"){
254
 
255
	#印出結果
256
	var_dump($checkMultiFileExist);
257
 
258
	#結束執行
259
	exit(1);
260
 
261
	}#if end
262
 
263
#若設定檔案不存在
264
if($checkMultiFileExist["allExist"]==="false"){
265
 
266
	#印出結果
267
	var_dump($checkMultiFileExist);
268
 
269
	#結束執行
270
	exit(1);
271
 
272
	}#if end
273
 
274
#載入設定檔案
275
#函式說明:
276
#解析PHP檔案裡面的變數.
277
#回傳結果:
278
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
279
#$result["error"],錯誤訊息.
280
#$result["function"],當前執行的函數名稱.
281
#$result["argu"],所使用的參數.
282
#$result["content"],找到的變數內容陣列,若有多個變數則會用array來表示.
283
#必填參數:
284
#$conf["file"],字串,檔案的路徑與名稱.
285
$conf["file"]=$config;
286
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
287
$conf["fileArgu"]=__FILE__;
288
#$conf["varName"],字串陣列,要搜尋的變數名稱,例如要搜尋變數$email則輸入"email".
289
$conf["varName"]=array("domainName","certificatePath","privateKey","ca_bundlePath","cert_and_ca_bundle_path","RR_file_path","dns_server_addr","action_after_update_certs");
290
#可省略參數:
291
#$conf["web"],是要取得網路上的檔案則為"true";反之則為"false",預設為"false".
292
$conf["web"]="false";
293
#$conf["allVarCountSame"],字串,是否每個參數的數量要一致,預設為"false"不要;反之為"true"代表要.
294
$conf["allVarCountSame"]="true";
295
#參考資料:
296
#https://www.php.net/manual/en/function.parse-str.php
297
#備註:
298
#無.
299
$parseVaraiableInPHPfile=fileAccess::parseVaraiableInPHPfile($conf);
300
unset($conf);
301
 
302
#如果執行異常
303
if($parseVaraiableInPHPfile["status"]==="false"){
304
 
305
	#印出結果
306
	var_dump($parseVaraiableInPHPfile);
307
 
308
	#結束執行
309
	exit(1);
310
 
311
	}#if end
312
 
313
#有多少個 domain name 要處理就執行幾次
314
for($i=0;$i<count($parseVaraiableInPHPfile["content"]["domainName"]);$i++){
315
 
316
	#設置 cname
317
	$conf["cName"][]=$parseVaraiableInPHPfile["content"]["domainName"][$i];
318
 
319
	#設置 certPath
320
	$conf["certInfo"][$i]["certPath"]=$parseVaraiableInPHPfile["content"]["certificatePath"][$i];
321
 
322
	#設置 pKey
323
	$conf["certInfo"][$i]["pKey"]=$parseVaraiableInPHPfile["content"]["privateKey"][$i];
324
 
325
	#設置 caPath
326
	$conf["certInfo"][$i]["caPath"]=$parseVaraiableInPHPfile["content"]["ca_bundlePath"][$i];
327
 
328
	#設置 certWithCaPath
329
	$conf["certInfo"][$i]["certWithCaPath"]=$parseVaraiableInPHPfile["content"]["cert_and_ca_bundle_path"][$i];
330
 
331
	#設置 rrFile
332
	$conf["rrFile"][]=$parseVaraiableInPHPfile["content"]["RR_file_path"][$i];
333
 
334
	#設置 dnsSerAddr
335
	$conf["dnsSerAddr"][]=$parseVaraiableInPHPfile["content"]["dns_server_addr"][$i];
336
 
337
	#設置 actionAfterUpdateCert
338
	$conf["actionAfterUpdateCert"][]=$parseVaraiableInPHPfile["content"]["action_after_update_certs"][$i];
339
 
340
	}#for end
341
 
342
#if enable debug
343
if($debug){
344
 
345
	echo "serivce config data:".PHP_EOL;
346
 
347
	#debug
348
	var_dump($conf);
349
 
350
	}#if end
351
 
352
#如果有 sock 參數
353
if(isset($parseArgu["content"]["sock"])){
354
 
355
	#取得 unix domain socket 的位置
356
	$sock=$parseArgu["content"]["sock"][0];
357
 
358
	}#if end
359
 
360
#取得 daemon 參數的內容
361
$dameon=$parseArgu["content"]["daemon"][0];
362
 
363
#如果為 "on"
364
if($dameon==="on"){
365
 
366
	#if enable debug
367
	if($debug){
368
 
369
		#設定 debug 參數
370
		$conf["debug"]="true";
371
 
372
		}#if end
373
 
374
	#如果有設置 sock
375
	if(isset($sock)){
376
 
377
		#設置 sock 參數
378
		$conf["sock"]=$sock;
379
 
380
		}#if end
381
 
293 liveuser 382
	#取得多執行序的要求與否
383
	$multiThreads=$parseArgu["content"]["multiThreads"][0];
384
 
385
	#如果有啟用多執行序
386
	if($multiThreads==="on"){
387
 
388
		#設置 multiThreads 參數
389
		$conf["multiThreads"]="true";
390
 
391
		}#if end
392
 
290 liveuser 393
	#函式說明:
394
	#透過zerossl api確保ssl沒有過期.
395
	#回傳結果:
396
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
397
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
398
	#$result["function"],當前執行的函式名稱.
399
	#$result["argu"],所使用的參數.
400
	#必填參數:
401
	#$conf["cName"],陣列,每個ssl的common domain name.
402
	#$conf["cName"]=array();
403
	#$conf["certInfo"],陣列,每個cName參數對應的憑證資訊,$conf["certInfo"][$i]["certPath"]為第$+1個cName的certifate.crt的檔案位置與名稱;$conf["certInfo"][$i]["pKey"]為第$i+1個cName的private key;$conf["certInfo"][$i]["caPath"]為第$i+1個cName的ca_bundle.crt的檔案位置與名稱;$conf["certInfo"][$i]["certWithCaPath"]為第$+1個cName的certifate.crt跟ca_bundle.crt內容合併後的檔案位置與名稱.
404
	#$conf["certInfo"]=array();
405
	#$conf["rrFile"],陣列,每個cName參數對應的RR檔案路徑與名稱.
406
	#$conf["rrFile"]=array();
407
	#$conf["dnsSerAddr"],陣列,每個cName參數對應的dns server位置,亦即ip或dns,用於透過root@dnsSerAddr將dns server重新啟動.
408
	#$conf["dnsSerAddr"]=array();
409
	#$conf["actionAfterUpdateCert"],陣列,每個元素代表對應cName所屬的憑證更新後要執行的指令,例如重新啟動apache(array("systemctl","restart","httpd").若要執行多個指令,用陣列表示即可,例如重新啟動apache跟qbpwcf-wss(array(array("systemctl","restart","httpd"),array("podman","restart","qbpwcf-wss")).
410
	#$conf["actionAfterUpdateCert"]=array();
411
	#可省略參數:
412
	#$conf["apiDomain"],字串,提供服務的domain,預設為 self::getApiInfo()["doamin"].
413
	#$conf["apiDomain"]=self::getApiInfo()["doamin"];
414
	#$conf["key"],字串,api key,預設為 self::getApiInfo()["apiKey"].
415
	#$conf["key"]=self::getApiInfo()["apiKey"];
416
	#$conf["debug"],字串,是否要開啟debug模式,"true"代表要;反之為預設"false"代表不要.
417
	#$conf["debug"]="";
418
	#$conf["addOnProcessFunc"],陣列,收到訊息後,要依順執行的函式,預設為array("\qbpwcf\zerosll::sslUpdater_cmd").
419
	#$conf["addOnProcessFunc"]=array();
420
	#$conf["funcToRunWhenIdle"],陣列,閒置時要依序執行的函式,預設為array("/qbpwcf/zerossl::sslUpdater_idle");
421
	#$conf["funcToRunWhenIdle"]=array();
422
	#參考資料:
423
	#無.
424
	#備註:
425
	#無.
426
	$sslUpdater=zerossl::sslUpdater($conf);
427
	unset($conf);
428
 
429
	#如果執行異常
430
	if($sslUpdater["status"]==="false"){
431
 
432
		#印出結果
433
		var_dump($sslUpdater);
434
 
435
		#結束執行
436
		exit(1);
437
 
438
		}#if end
439
 
440
	}#if end