Subversion Repositories qbpwcf-lib(archive)

Rev

Rev 663 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
660 liveuser 1
<?php
2
 
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
#使用命名空間qbpwcf
26
namespace qbpwcf;
27
 
28
#匯入套件
29
require_once("qbpwcf/allInOne.php");
30
 
31
#建議的log位置
32
$logFile=$_SERVER["DOCUMENT_ROOT"].$_SERVER["PHP_SELF"].".log";
33
 
34
#初始化儲存工作
35
$job_at_now_repeat_after_60sec=array();
36
 
37
#函式說明:
38
#呼叫shell執行系統命令,並取得回傳的內容.
39
#回傳結果:
40
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
41
#$result["error"],錯誤訊息陣列.
42
#$result["function"],當前執行的函數名稱.
43
#$result["argu"],使用的參數.
44
#$result["cmd"],執行的指令內容.
45
#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
46
#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
47
#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
48
#$result["running"],是否還在執行.
49
#$result["pid"],pid.
50
#$result["statusCode"],執行結束後的代碼.
51
#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
52
#必填參數:
53
#$conf["command"],字串,要執行的指令.
54
$conf["command"]="ps";
55
#$conf["fileArgu"],字串,變數__FILE__的內容.
56
$conf["fileArgu"]=__FILE__;
57
#可省略參數:
58
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
59
$conf["argu"]=array("auxwf");
60
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
61
#$conf["arguIsAddr"]=array();
62
#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
63
#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
64
#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
65
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
66
#$conf["enablePrintDescription"]="true";
67
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
68
#$conf["printDescription"]="";
69
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
70
$conf["escapeshellarg"]="true";
71
#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
72
#$conf["thereIsShellVar"]=array();
73
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
74
#$conf["username"]="";
75
#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
76
#$conf["password"]="";
77
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
78
#$conf["useScript"]="";
79
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
80
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
81
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
82
#$conf["inBackGround"]="";
83
#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
84
#$conf["getErr"]="false";
85
#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
86
$conf["doNotRun"]="true";
87
#參考資料:
88
#exec=>http://php.net/manual/en/function.exec.php
89
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
90
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
91
#備註:
92
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
93
#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
94
$callShell=external::callShell($conf);
95
unset($conf);
96
 
97
#如果執行失敗
98
if($callShell["status"]=="false"){
99
 
100
	#印出結果
101
	var_dump($callShell);
102
 
103
	#結束執行回傳1
104
	exit(1);
105
 
106
	}#if end
107
 
108
#記錄 action
109
$job_at_now_repeat_after_60sec["action"]=$callShell["escape"]["array"];
110
 
111
#函式說明:
112
#計算時間點距離1970/01/01多少秒.
113
#回傳結果:
114
#$result["stauts"],執行是否成功,"true"代表執行成功;"false"代表執行失敗
115
#$result["error"],錯誤訊息陣列
116
#$result["content"],時間點距離1970/01/01的多少秒
117
#必填參數:
118
#$conf["dateAndTime"],字串,格式爲 2011-05-27 23:59:59
119
$conf["dateAndTime"]="0000-00-00 00:00:00";
120
#可省略參數:
121
#無.
122
#參考資料:
123
#無.
124
#備註:
125
#無.
126
$caculateTimeAmount=time::caculateTimeAmount($conf);
127
unset($conf);
128
 
129
#如果執行失敗
130
if($caculateTimeAmount["status"]=="false"){
131
 
132
	#印出結果
133
	var_dump($caculateTimeAmount);
134
 
135
	#結束執行回傳1
136
	exit(1);
137
 
138
	}#if end
139
 
140
#記錄 time
141
$job_at_now_repeat_after_60sec["time"]=$caculateTimeAmount["content"];
142
 
143
#記錄 repeat 規則
144
$job_at_now_repeat_after_60sec["repeat"]=array("times"=>-1,"after"=>60);
145
 
146
#初始化儲存工作
147
$job_at_every_min=array();
148
 
149
#函式說明:
150
#呼叫shell執行系統命令,並取得回傳的內容.
151
#回傳結果:
152
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
153
#$result["error"],錯誤訊息陣列.
154
#$result["function"],當前執行的函數名稱.
155
#$result["argu"],使用的參數.
156
#$result["cmd"],執行的指令內容.
157
#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
158
#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
159
#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
160
#$result["running"],是否還在執行.
161
#$result["pid"],pid.
162
#$result["statusCode"],執行結束後的代碼.
163
#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
164
#必填參數:
165
#$conf["command"],字串,要執行的指令.
166
$conf["command"]="ps";
167
#$conf["fileArgu"],字串,變數__FILE__的內容.
168
$conf["fileArgu"]=__FILE__;
169
#可省略參數:
170
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
171
$conf["argu"]=array("auxwf");
172
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
173
#$conf["arguIsAddr"]=array();
174
#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
175
#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
176
#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
177
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
178
#$conf["enablePrintDescription"]="true";
179
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
180
#$conf["printDescription"]="";
181
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
182
$conf["escapeshellarg"]="true";
183
#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
184
#$conf["thereIsShellVar"]=array();
185
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
186
#$conf["username"]="";
187
#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
188
#$conf["password"]="";
189
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
190
#$conf["useScript"]="";
191
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
192
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
193
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
194
#$conf["inBackGround"]="";
195
#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
196
#$conf["getErr"]="false";
197
#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
198
$conf["doNotRun"]="true";
199
#參考資料:
200
#exec=>http://php.net/manual/en/function.exec.php
201
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
202
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
203
#備註:
204
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
205
#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
206
$callShell=external::callShell($conf);
207
unset($conf);
208
 
209
#如果執行失敗
210
if($callShell["status"]=="false"){
211
 
212
	#印出結果
213
	var_dump($callShell);
214
 
215
	#結束執行回傳1
216
	exit(1);
217
 
218
	}#if end
219
 
220
#記錄 action
221
$job_at_every_min["action"]=$callShell["escape"]["array"];
222
 
223
#函式說明:
224
#計算時間點距離1970/01/01多少秒.
225
#回傳結果:
226
#$result["stauts"],執行是否成功,"true"代表執行成功;"false"代表執行失敗
227
#$result["error"],錯誤訊息陣列
228
#$result["content"],時間點距離1970/01/01的多少秒
229
#必填參數:
230
#$conf["dateAndTime"],字串,格式爲 2011-05-27 23:59:59
231
$conf["dateAndTime"]="0000-00-00 00:01:00";
232
#可省略參數:
233
#無.
234
#參考資料:
235
#無.
236
#備註:
237
#無.
238
$caculateTimeAmount=time::caculateTimeAmount($conf);
239
unset($conf);
240
 
241
#如果執行失敗
242
if($caculateTimeAmount["status"]=="false"){
243
 
244
	#印出結果
245
	var_dump($caculateTimeAmount);
246
 
247
	#結束執行回傳1
248
	exit(1);
249
 
250
	}#if end
251
 
252
#記錄 time
253
$job_at_every_min["time"]=$caculateTimeAmount["content"];
254
 
255
#記錄 repeat 規則
256
$job_at_every_min["repeat"]=array("times"=>-1,"fixed"=>$job_at_every_min["time"]);
257
 
258
#函式說明:
259
#新增或覆蓋系統上有哪些要自動執行的工作.
260
#回傳結果:
261
#$result["ststus"],執行是否正常,"true"代表執行正常,"false"代表執行不正常.
262
#$result["error"],錯誤訊息陣列.
263
#$result["function"],當前執行的函式名稱.
264
#必填參數:
265
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
266
$conf["fileArgu"]=__FILE__;
267
#$conf["actions"],字串陣列,每個元素代表要執行的事情,每個事情為要執行的指令與參數所組成的一維陣列.
268
$conf["actions"]=array($job_at_now_repeat_after_60sec["action"],$job_at_every_min["action"]);
269
#$conf["times"],字串陣列,每個元素代表要執行事情的時間點,單為為秒.格式為unixtime,事情不存在於啟動的狀態且當前時間大於等於之就會執行.
270
$conf["times"]=array($job_at_now_repeat_after_60sec["time"],$job_at_every_min["time"]);
271
#$conf["repeat"],字串陣列,每個元素為一維陣列,key為"times"代表要執行事情的重複方式,若其value為"N"代表重複執行N次(若N等於-1,則代表無限重複執行;若N等於0,則代表執行後就會消失);key為"after"代表下次執行的時間為times+加其value;key為"fixed"代表下次執行的時間為一個循環的固定時間點,value為每次循環的固定時間點,例如每天01:05則為1h*3600s+5m*60s=3900,小於一天(86400秒)的時間,因此周期為天.
272
$conf["repeat"]=array($job_at_now_repeat_after_60sec["repeat"],$job_at_every_min["repeat"]);
273
#可省略參數:
274
#$conf["config"],字串,排程的設定檔位置,預設為"/usr/local/etc/qbpwcf-schedule.conf.php".
275
$conf["config"]="assets of autoService/qbpwcf-schedule.conf.php";
276
#$conf["append"],字串,是否要用append的方式增加資料,預設為"true";若要完全覆蓋既有的資料則要設置為"false".
277
#$conf["append"]="false";
278
#參考資料:
279
#無.
280
#備註:
281
#建構中
282
$createList=autoService::createList($conf);
283
unset($conf);
284
 
285
#如果執行失敗
286
if($createList["status"]=="false"){
287
 
288
	#印出結果
289
	var_dump($createList);
290
 
291
	#結束執行回傳1
292
	exit(1);
293
 
294
	}#if end
295
 
296
#debug
297
var_dump($createList);
298
 
299
?>