| 391 |
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 fileAccess::getTextFilePart_20260708-1";
|
|
|
30 |
|
|
|
31 |
#產生一份 20 行資料的檔案
|
|
|
32 |
#函式說明:
|
|
|
33 |
#呼叫shell執行系統命令,並取得回傳的內容.
|
|
|
34 |
#回傳結果:
|
|
|
35 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
36 |
#$result["error"],錯誤訊息陣列.
|
|
|
37 |
#$result["function"],當前執行的函數名稱.
|
|
|
38 |
#$result["argu"],使用的參數.
|
|
|
39 |
#$result["cmd"],執行的指令內容.
|
|
|
40 |
#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
41 |
#$result["output"],爲執行完後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
|
|
|
42 |
#$result["content"],為執行完後的輸出字串.
|
|
|
43 |
#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
44 |
#$result["running"],是否還在執行.
|
|
|
45 |
#$result["pid"],pid.
|
|
|
46 |
#$result["statusCode"],執行結束後的代碼.
|
|
|
47 |
#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
|
|
|
48 |
#$result["noEscaped"],陣列,儲存重新排序過未經過escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
|
|
|
49 |
#必填參數:
|
|
|
50 |
#$conf["command"],字串,要執行的指令.
|
|
|
51 |
$conf["command"]="cat";
|
|
|
52 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
|
|
53 |
$conf["fileArgu"]=__FILE__;
|
|
|
54 |
#可省略參數:
|
|
|
55 |
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
|
|
|
56 |
$conf["argu"]=array($assetsDir."/readOnly_ssl_access_log","|","head","-n","20",">",$assetsDir."/ssl_access_log");
|
|
|
57 |
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
|
|
|
58 |
#$conf["arguIsAddr"]=array();
|
|
|
59 |
#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
|
|
|
60 |
#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
|
|
|
61 |
#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
|
|
|
62 |
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
|
|
|
63 |
#$conf["enablePrintDescription"]="true";
|
|
|
64 |
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
|
|
|
65 |
#$conf["printDescription"]="";
|
|
|
66 |
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
|
|
|
67 |
$conf["escapeshellarg"]="true";
|
|
|
68 |
#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
|
|
|
69 |
#$conf["thereIsShellVar"]=array();
|
|
|
70 |
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
|
|
|
71 |
#$conf["username"]="";
|
|
|
72 |
#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
|
|
73 |
#$conf["password"]="";
|
|
|
74 |
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
|
|
|
75 |
#$conf["useScript"]="";
|
|
|
76 |
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
|
|
|
77 |
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
|
|
|
78 |
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
|
|
|
79 |
#$conf["inBackGround"]="";
|
|
|
80 |
#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
|
|
|
81 |
#$conf["getErr"]="false";
|
|
|
82 |
#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
|
|
|
83 |
#$conf["doNotRun"]="false";
|
|
|
84 |
#$conf["cmdContainArgu"],字串,cmd參數是否已經含有參數,預設為"false"代表沒有;反之為"true".
|
|
|
85 |
#$conf["cmdContainArgu"]="false";
|
|
|
86 |
#$conf["remoteIp"],字串,遠端ip或domainName,預設不使用,若存在則會透過ssh指令進行連線,然後執行指令.
|
|
|
87 |
#$conf["remoteIp"]="";
|
|
|
88 |
#$conf["remoteUser"],字串,遠端的使用者賬戶,預設不指定,亦即跟為運行該php程式的使用者.
|
|
|
89 |
#$conf["remoteUser"]="";
|
|
|
90 |
#$conf["sshOptions"],陣列,當具備 "remoteIp" 參數時,該參數每個元素為ssh指令的參數.
|
|
|
91 |
#$conf["sshOptions"]=array();
|
|
|
92 |
#參考資料:
|
|
|
93 |
#exec=>http://php.net/manual/en/function.exec.php
|
|
|
94 |
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
|
|
|
95 |
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
|
|
|
96 |
#備註:
|
|
|
97 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
|
|
|
98 |
#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
|
|
|
99 |
$callShell=external::callShell($conf);
|
|
|
100 |
unset($conf);
|
|
|
101 |
|
|
|
102 |
#如果執行異常
|
|
|
103 |
if($callShell["status"]==="false"){
|
|
|
104 |
|
|
|
105 |
#debug
|
|
|
106 |
var_dump($callShell);
|
|
|
107 |
|
|
|
108 |
#結束執行,回傳 shell 1
|
|
|
109 |
exit(1);
|
|
|
110 |
|
|
|
111 |
}#if end
|
|
|
112 |
|
|
|
113 |
#debug
|
|
|
114 |
#var_dump(__LINE__,$callShell);exit;
|
|
|
115 |
|
|
|
116 |
#初始化給 threads::proc 的 cmds 參數
|
|
|
117 |
$cmds=array();
|
|
|
118 |
|
|
|
119 |
#讀取檔案
|
|
|
120 |
$cmds[]="php -f ./loadLogFile.php";
|
|
|
121 |
|
|
|
122 |
#rotate檔案
|
|
|
123 |
$cmds[]="php -f ./rotateLogFile.php";
|
|
|
124 |
|
|
|
125 |
#設置工作目錄為 asset 目錄
|
|
|
126 |
$workPath=pathinfo(__FILE__)["dirname"]."/../".$assetsDir;
|
|
|
127 |
|
|
|
128 |
#函式說明:
|
|
|
129 |
#透過proc來多執行序運作.
|
|
|
130 |
#回傳結果:
|
|
|
131 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
132 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
133 |
#$result["function"],當前執行的函式名稱.
|
|
|
134 |
#$result["argu"],使用的參數.
|
|
|
135 |
#$result["content"],陣列,每個元素為其指令執行的結果訊息陣列,key為"status"代表執行是否正常的識別;key為"statusCode"代表程式結束後回傳給對應executeBy程式的數值,若為"?"則代表程序尚未結束,若為整數0,則代表順利執行結束,可透過proc_update函式進行資訊的更新與取得;key為"content"代表標準輸出,若為resource,則代表為pipe;key為"error"代表非標準輸出,若為resource,則代表為pipe;key為"input"代表成功輸入的指令;key為"process"代表該程序經proc_open後的process source;key為"proc_get_status"代表程序的資訊.
|
|
|
136 |
#必填參數:
|
|
|
137 |
#$conf["cmds"],字串陣列,每個元素代表單一程序要執行的指令與參數.
|
|
|
138 |
$conf["cmds"]=$cmds;
|
|
|
139 |
#可省略參數:
|
|
|
140 |
#$conf["wait"],字串,是否需要等待所有程序結束,預設為"true"要等待;反之為"false"不要等待.
|
|
|
141 |
$conf["wait"]="false";
|
|
|
142 |
#$conf["timeout"],字串陣列,每個元素代表單一程序執行的最大等待秒數,超過後將會強迫停止執行,僅當wait參數為"true"時生效.
|
|
|
143 |
#$conf["timeout"]=array("10");
|
|
|
144 |
#$conf["workingDir"],字串陣列,個別程式執行時的家目錄,預設不指定.
|
|
|
145 |
$conf["workingDir"]=array($workPath,$workPath);
|
|
|
146 |
#$conf["envs"],2維字串陣列,每個元素代表個別程式執行時的指定環境變數,key變數名稱;value為變數內容.預設為array("QBPWCF" => "Quick Build PHP Website Componment base on Fedora Linux");
|
|
|
147 |
#$conf["envs"]=array(array("key"=>"value"));
|
|
|
148 |
#$conf["executeBy"],字串陣列,每個元素代表個別指令要用什麼程式執行,預設為"bash".
|
|
|
149 |
#$conf["executeBy"]=array("bash");
|
|
|
150 |
#參考資料:
|
|
|
151 |
#https://www.php.net/manual/en/function.proc-open.php
|
|
|
152 |
#https://www.php.net/manual/en/function.proc-get-status.php
|
|
|
153 |
#https://www.php.net/manual/en/function.proc-terminate.php
|
|
|
154 |
#備註:
|
|
|
155 |
#當wait參數為"true"時,會自動解析stdout與stderr,因此不用再透過 self::proc_update 來更新.
|
|
|
156 |
#當wait參數不為"true"時,若需要取得當下的執行狀況,請使用 self::proc_update 來更新.
|
|
|
157 |
$proc=threads::proc($conf);
|
|
|
158 |
unset($conf);
|
|
|
159 |
|
|
|
160 |
#若執行異常
|
|
|
161 |
if($proc["status"]==="false"){
|
|
|
162 |
|
|
|
163 |
#debug
|
|
|
164 |
var_dump($callShell);
|
|
|
165 |
|
|
|
166 |
#結束執行,回傳 shell 1
|
|
|
167 |
exit(1);
|
|
|
168 |
|
|
|
169 |
}#if end
|
|
|
170 |
|
|
|
171 |
#儲存讀取檔案的std
|
|
|
172 |
$readStd=$proc["content"][0]["content"];
|
|
|
173 |
|
|
|
174 |
#儲存讀取檔案的err
|
|
|
175 |
$readErr=$proc["content"][0]["error"];
|
|
|
176 |
|
|
|
177 |
#儲存rotate檔案的std
|
|
|
178 |
$rotateStd=$proc["content"][1]["content"];
|
|
|
179 |
|
|
|
180 |
#儲存rotate檔案的err
|
|
|
181 |
$rotateErr=$proc["content"][1]["error"];
|
|
|
182 |
|
|
|
183 |
#設置給予 threads::proc_stream_select 的 streams 參數
|
|
|
184 |
$streams=array("readStd"=>$readStd,"readErr"=>$readErr,"rotateStd"=>$rotateStd,"rotateErr"=>$rotateErr);
|
|
|
185 |
|
|
|
186 |
#無窮迴圈
|
|
|
187 |
while(true){
|
|
|
188 |
|
|
|
189 |
#函式說明:
|
|
|
190 |
#類似stream_select的替代方案, 用來檢查透過proc運行的多執行序其標準輸出、錯誤輸出是否有訊息,程序是否已經結束.
|
|
|
191 |
#回傳結果:
|
|
|
192 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
193 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
194 |
#$result["function"],當前執行的函式名稱.
|
|
|
195 |
#$result["argu"],使用的參數.
|
|
|
196 |
#$result["content"],陣列,每個元素的key代表有訊息的channel名稱,數值為陣列.
|
|
|
197 |
#$result["content"][$channel],陣列,$channel為通道的名稱亦即streams參數陣列中的key.陣列的key有"content",代表得到的訊息;"alive"則代表通道是否還處於開啟的狀態.
|
|
|
198 |
#$result["streams"],陣列,依然開啟的通道(運行中),元素的key為該channel的名稱;數值為channel.
|
|
|
199 |
#$result["allChannelClosed"],字串,"true"代表streams參數的通道已經全部關閉;反之為"false".
|
|
|
200 |
#必填參數:
|
|
|
201 |
#$conf["streams"],陣列,每個元素為threads::proc回傳結果content中每個元素的content跟error,key為該stream的名稱,例如:"client_1_std"、"client_1_err".
|
|
|
202 |
$conf["streams"]=$streams;
|
|
|
203 |
#可省略參數:
|
|
|
204 |
#$conf["periodToWait"],整數,要等待多少秒來嘗試取得訊息,預設為1秒鐘.
|
|
|
205 |
#$conf["periodToWait"]=1;
|
|
|
206 |
#參考資料:
|
|
|
207 |
#https://www.php.net/manual/en/function.proc-open.php
|
|
|
208 |
#https://www.php.net/manual/en/function.proc-get-status.php
|
|
|
209 |
#https://www.php.net/manual/en/function.proc-terminate.php
|
|
|
210 |
#備註:
|
|
|
211 |
#無.
|
|
|
212 |
$proc_stream_select=threads::proc_stream_select($conf);
|
|
|
213 |
unset($conf);
|
|
|
214 |
|
|
|
215 |
#如果執行異常
|
|
|
216 |
if($proc_stream_select["status"]==="false"){
|
|
|
217 |
|
|
|
218 |
#debug
|
|
|
219 |
var_dump($proc_stream_select);
|
|
|
220 |
|
|
|
221 |
#結束執行,回傳shell 1
|
|
|
222 |
exit(1);
|
|
|
223 |
|
|
|
224 |
}#if end
|
|
|
225 |
|
|
|
226 |
#如果有訊息
|
|
|
227 |
if(!empty($proc_stream_select["content"])){
|
|
|
228 |
|
|
|
229 |
#針對每個stream
|
|
|
230 |
foreach($proc_stream_select["content"] as $channelName => $channelInfo){
|
|
|
231 |
|
|
|
232 |
#如果通道已經關閉
|
|
|
233 |
if($channelInfo["alive"]==="false"){
|
|
|
234 |
|
|
|
235 |
#加上已經關閉 channel 的提示文字
|
|
|
236 |
$channelName=$channelName."(closed)";
|
|
|
237 |
|
|
|
238 |
}#if end
|
|
|
239 |
|
|
|
240 |
#印出通道名稱與輸出的內容
|
|
|
241 |
echo $channelName." > ".$channelInfo["content"];
|
|
|
242 |
|
|
|
243 |
}#foreach end
|
|
|
244 |
|
|
|
245 |
}#if end
|
|
|
246 |
|
|
|
247 |
#如果通道都已經關閉
|
|
|
248 |
if($proc_stream_select["allChannelClosed"]==="true"){
|
|
|
249 |
|
|
|
250 |
#正常結束執行
|
|
|
251 |
exit;
|
|
|
252 |
|
|
|
253 |
}#if end
|
|
|
254 |
|
|
|
255 |
#更新給予 threads::proc_stream_select 的 streams 參數
|
|
|
256 |
$streams=$proc_stream_select["streams"];
|
|
|
257 |
|
|
|
258 |
}#while end
|
|
|
259 |
|
|
|
260 |
#移除log檔案
|
|
|
261 |
#函式說明:
|
|
|
262 |
#移除檔案
|
|
|
263 |
#回傳結果:
|
|
|
264 |
#$result["status"],"true"代表移除成功,"false"代表移除失敗.
|
|
|
265 |
#$result["error"],錯誤訊息陣列.
|
|
|
266 |
#$result["warning"],警告訊息陣列.
|
|
|
267 |
#$result["function"],當前執行的函數名稱.
|
|
|
268 |
#$result["argu"],當前函式使用的參數.
|
|
|
269 |
#必填參數:
|
|
|
270 |
#$conf["fileAddress"],字串,要移除檔案的位置.
|
|
|
271 |
$conf["fileAddress"]=$workPath."/ssl_access_log";
|
|
|
272 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.
|
|
|
273 |
$conf["fileArgu"]=__FILE__;
|
|
|
274 |
#可省略參數:
|
|
|
275 |
#$conf["commentsArray"],字串陣列,提示的文字描述,$conf["commentsArray"][$i]代表第($+1)行的描述.
|
|
|
276 |
#$conf["commentsArray"]=array("");
|
|
|
277 |
#$conf["allowDelSymlink"],字串,預設為"false",不移除軟連結;"true"代表要移除軟連結.
|
|
|
278 |
#$conf["allowDelSymlink"]="true";
|
|
|
279 |
#$conf["allowDelFolder"],字串,預設為"false",不移除目錄;"true"代表要移除目錄.
|
|
|
280 |
#$conf["allowDelFolder"]="true";
|
|
|
281 |
#參考資料:
|
|
|
282 |
#無.
|
|
|
283 |
#備註:
|
|
|
284 |
#無.
|
|
|
285 |
$delFile=fileAccess::delFile($conf);
|
|
|
286 |
unset($conf);
|
|
|
287 |
|
|
|
288 |
#如果執行異常
|
|
|
289 |
if($delFile["status"]==="false"){
|
|
|
290 |
|
|
|
291 |
#debug
|
|
|
292 |
var_dump($delFile);
|
|
|
293 |
|
|
|
294 |
#結束執行,回傳shell 1
|
|
|
295 |
exit(1);
|
|
|
296 |
|
|
|
297 |
}#if end
|