Subversion Repositories qbpwcf-lib(archive)

Rev

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

Rev Author Line No. Line
833 liveuser 1
#!/usr/bin/php
2
<?php
3
 
4
/*
5
	QBPWCF, Quick Build PHP website Component base on Fedora Linux.
6
    Copyright (C) 2015~2024 Min-Jhin,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
 
27
說明:
28
註冊服務.
29
 
30
範例:
31
 
32
#每次執行 ps auxwf | cat 後,下一秒就刷新畫面重新執行.
33
registerService.php --cmds clear --cmds 'ps auxwf | cat'  --cmds 'sleep 1'
34
 
953 liveuser 35
備註:
36
目前不支援在 ostree 環境下運行.
37
 
833 liveuser 38
*/
39
 
40
#指派命名空間
41
namespace qbpwcf;
42
 
953 liveuser 43
#取得 lib path
44
exec("php -f ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/libexec/folderOfUsrLib.php"),$output,$status);
45
 
46
#如果執行失敗
47
if($status!==0){
48
 
49
	#debug
50
	var_dump(__LINE__,$output);
51
 
52
	#結束執行,回傳shell 1.
53
	exit(1);
54
 
55
	}#if end
56
 
57
#儲存lib path
58
$folderOfUsrLib=$output[0];
59
 
833 liveuser 60
#以該檔案的實際位置的 lib path 為 include path 首位
953 liveuser 61
$output=array();
62
exec("cd ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/../".$folderOfUsrLib."/qbpwcf").";pwd;",$output,$status);
63
 
64
#如果執行失敗
65
if($status!==0){
66
 
67
	#debug
68
	var_dump(__LINE__,$output);
69
 
70
	#結束執行,回傳shell 1.
71
	exit(1);
72
 
73
	}#if end
74
 
75
#設置 include path 
833 liveuser 76
set_include_path($output[0].PATH_SEPARATOR.get_include_path());
77
 
78
#匯入外部套件
79
include("allInOne.php");
80
 
81
#說明函式
82
function help()
83
{
84
	#印出指令說明
85
	echo "Usage of ".basename(__FILE__).":".PHP_EOL; 
86
	echo "--cmd [command to run] 代表要執行的指令,若有多個則會按照順序執行.".PHP_EOL;
87
	echo "--name [service name] 服務的名稱".PHP_EOL;
88
	echo "--runOnBoot [yes/no] 開機好就要啟動該服務,預設為yes".PHP_EOL;
89
	echo "--runNow [yes/no] 馬上運行服務,預設為yes".PHP_EOL;
90
 
91
	#結束執行
92
	exit;
93
}
94
 
95
#函式說明:
96
#解析參數.
97
#回傳結果:
98
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
99
#$reuslt["error"],執行不正常結束的錯訊息陣列.
100
#$result["function"],當前執行的函式名稱.
101
#$result["content"],解析好的參數陣列.
102
#$result["content"][$key][$i],參數 $key 的 $i+1 個參數數值內容.
103
#$result["program"],字串,執行的程式名稱.
104
#必填參數:
105
#無.
106
#可省略參數:
107
#$conf["helpFunc"],如果解析的參數不成對,則要執行的函式名稱.
108
$conf["helpFunc"]="help";
109
#$conf["parseFormVar"],解析表單變數(get\post),預設為"false"不使用;反之設置為"true".
110
$conf["parseFormVar"]="false";
111
#參考資料:
112
#無.
113
#備註:
114
#僅能在命令列底下執行.
115
#以後可將參數 --a--b 的名稱與後面的數值 $value 存成 $result["a"]["b"][$i]=$value .
116
$parseArgu=cmd::parseArgu($conf);
117
unset($conf);
118
 
119
#如果取得參數失敗
120
if($parseArgu["status"]==="false"){
121
 
122
	#設置執行失敗
123
	$result["status"]="false";
124
 
125
	#設置執行錯誤訊息
126
	$result["error"]=$parseArgu;
127
 
128
	#印出訊息
129
	var_dump($result);
130
 
131
	#結束執行
132
	exit;
133
 
134
	}#if end
135
 
136
#檢查參數
137
#函式說明:
138
#檢查必填與可省略參數,可省略參數可指定預設要給與什麼數值內容。
139
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
140
#$reuslt["error"],執行不正常結束的錯訊息陣列.
141
#$result["function"],當前執行的函式名稱.
142
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
143
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
144
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
145
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
146
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
147
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
148
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
149
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
150
#必填寫的參數:
151
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
152
$conf["varInput"]=&$parseArgu["content"];
153
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
154
$conf["referenceVarKey"]="variableCheck::checkArguments";
155
#可以省略的參數:
156
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
157
$conf["mustBeFilledVariableName"]=array("cmd","name");
158
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
159
$conf["mustBeFilledVariableType"]=array("array","array");
160
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
161
#$conf["canBeEmptyString"]="false";
162
#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
163
#$conf["canNotBeEmpty"]=array();
164
#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
165
#$conf["canBeEmpty"]=array();
166
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
167
#$conf["skipableVariableCanNotBeEmpty"]=array("backTime");
168
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
169
$conf["skipableVariableName"]=array("runOnBoot","runNow");
170
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
171
$conf["skipableVariableType"]=array("array","array");
172
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
173
$conf["skipableVarDefaultValue"]=array(array("yes"),array("yes"));
174
#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
175
#$conf["variableCheck::checkArguments"]["disallowAllSkipableVarIsEmpty"]="false";
176
#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
177
#$conf["disallowAllSkipableVarIsEmptyArray"]="";
178
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
179
#$conf["arrayCountEqualCheck"][]=array();
180
#參考資料來源:
181
#array_keys=>http://php.net/manual/en/function.array-keys.php
182
$checkArguments=variableCheck::checkArguments($conf);
183
unset($conf);
184
 
185
#若執行失敗
186
if($checkArguments["status"]==="false"){
187
 
188
	#印出結果
189
	var_dump($checkArguments);
190
 
191
	#結束執行
192
	exit;
193
 
194
	}#if end
195
 
196
#若檢查不通過
197
if($checkArguments["passed"]==="false"){
198
 
199
	#debug
200
	var_dump($checkArguments);
201
 
202
	#提示用法
203
	help();
204
 
205
	#結束執行
206
	exit;
207
 
208
	}#if end
209
 
210
#取得指令陣列
211
$cmds=$parseArgu["content"]["cmd"];
212
 
213
#取得目標參數
214
$serviceName=$parseArgu["content"]["name"][0];
215
 
216
#取得runOnBoot參數
217
$runOnBoot=$parseArgu["content"]["runOnBoot"][0];
218
 
219
#預設為 false
220
$runOnBoot="false";
221
 
222
#如果是 yes
223
if($runOnBoot==="yes"){
224
 
225
	#轉換為 "true"
226
	$runOnBoot="true";
227
 
228
	}#if end
229
 
230
#取得runNow參數
231
$runNow=$parseArgu["content"]["runNow"][0];
232
 
233
#預設為 false
234
$runNow="false";
235
 
236
#如果是 yes
237
if($runNow==="yes"){
238
 
239
	#轉換為 "true"
240
	$runNow="true";
241
 
242
	}#if end
243
 
244
#建立暫存檔案
245
#函式說明:
246
#於本套件位置底下的tmp資料夾下建立與回傳暫存檔案名稱路徑 
247
#回傳結果:
248
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
249
#$result["error"],錯誤訊息.
250
#$result["function"],當前執行的函數名稱.
251
#$result["content"],暫存檔案的路徑與名稱.
252
#$result["fileName"],暫存檔案的名稱.
253
#$result["path"],暫存檔案的路徑.
254
#必填參數:
255
#無.
256
#可省略參數:
257
#無.
258
#參考資料:
259
#無.
260
#備註:
261
#無.
262
$createTempFile=fileAccess::createTempFile();
263
 
264
#如果建立暫存檔案失敗
265
if($createTempFile["status"]==="false"){
266
 
267
	#debug
268
	var_dump($createTempFile);
269
 
270
	#結束執行,回傳 shell 1
271
	exit(1);
272
 
273
	}#if end
274
 
275
#撰寫要執行的程式內容
276
#函式說明:
277
#將多行字串寫入到檔案
278
#回傳結果:
279
#$result["status"],"true"表示檔案寫入成功,"false"表示檔案寫入失敗.
280
#$result["error"],錯誤訊息陣列.
281
#$result["function"],當前執行函數的名稱.
282
#必填參數:
283
#$conf["fileName"],字串,爲要編輯的檔案名稱
284
$conf["fileName"]=$createTempFile["content"];
285
#$conf["inputString"],字串陣列,爲要寫入到 $conf["fileName"] 裏面的內容. $conf["inputString"][$i] 代表第 $i+1 行。
286
$conf["inputString"]=$cmds;
287
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
288
$conf["fileArgu"]=__FILE__;
289
#可省略參數:
290
#$conf["writeMethod"]="a";#爲檔案撰寫的方式,可省略,是複寫'a'還是,重新寫入'w',預設爲'w',重新寫入。
291
#參考資料:
292
#無.
293
#備註:
294
#無.
295
$writeMultiLine=fileAccess::writeMultiLine($conf);
296
unset($conf);
297
 
298
#如果建立檔案失敗
299
if($writeMultiLine["status"]==="false"){
300
 
301
	#debug
302
	var_dump($writeMultiLine);
303
 
304
	#結束執行,回傳 shell 1
305
	exit(1);
306
 
307
	}#if end
308
 
309
#移動程式到 /usr/bin/ 底下
310
#函式說明:
311
#移動檔案
312
#回傳結果:
313
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
314
#$result["function"],當前執行的函數名稱
315
#$result["error"],錯誤訊息.
316
#$result["content"],檔案輸出後的位置與名稱.
317
#必填參數:
318
#$conf["from"],字串,要移動的檔案名稱與位置.
319
$conf["from"]=$createTempFile["content"];
320
#$conf["to"],字串,要移動到的位置與名稱
321
$conf["to"]="/usr/bin/".$serviceName.".sh";
322
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
323
$conf["fileArgu"]=__FILE__;
324
#可省略參數:
325
#$conf["commentsArray"],字串陣列,提示的文字描述,$conf["commentsArray"][$i]代表第($+1)行的描述.
326
#$conf["commentsArray"]=array("");
327
#參考資料:
328
#無.
329
#備註:
330
#無.
331
$mv=fileAccess::mv($conf);
332
unset($conf);
333
 
334
#如果移動檔案失敗
335
if($mv["status"]==="false"){
336
 
337
	#debug
338
	var_dump($mv);
339
 
340
	#結束執行,回傳 shell 1
341
	exit(1);
342
 
343
	}#if end
344
 
345
#函式說明:
346
#將要執行的程式變成透過 systemd 來運行.
347
#回傳結果:
348
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
349
#$result["error"],錯誤訊息.
350
#$result["function"],當前執行的函式名稱.
351
#$result["argu"],所使用的參數.
352
#$result["content"],執行的結果.
353
#必填參數:
354
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.
355
$conf["fileArgu"]=__FILE__;
356
#$conf["name"],字串,服務名稱,實際產生的system名稱會加上".service".
357
$conf["name"]=$serviceName;
358
#$conf["cmd"],字串,要執行的指令.
359
$conf["cmd"]=$serviceName.".sh";
360
#可省略參數:
361
#$conf["params"],字串陣列,指令要使用的參數.
362
#$conf["params"]=array("");
363
#$conf["enable"],字串,預設為"true",代表該服務為enable.
364
$conf["enable"]=$runOnBoot;
365
#$conf["startNow"],字串,預設為"true",代表該服務要立刻執行.
366
$conf["startNow"]=$runNow;
367
#參考資料:
368
#無.
369
#備註:
370
#無.
371
$registerService=cmd::registerService($conf);
372
unset($conf);
373
 
374
#如果執行失敗
375
if($registerService["status"]==="false"){
376
 
377
	#debug
378
	var_dump($registerService);
379
 
380
	#結束執行,回傳 shell 1
381
	exit(1);
382
 
383
	}#if end