Subversion Repositories php-qbpwcf

Rev

Rev 81 | Rev 226 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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