| 495 |
liveuser |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
|
|
|
5 |
QBPWCF, Quick Build PHP website Component base on Fedora Linux.
|
| 639 |
liveuser |
6 |
Copyright (C) 2015~2024 Min-Jhin,Chen
|
| 495 |
liveuser |
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 |
|
| 529 |
liveuser |
31 |
#建議的log位置
|
|
|
32 |
$logFile=$_SERVER["DOCUMENT_ROOT"].$_SERVER["PHP_SELF"].".log";
|
|
|
33 |
|
| 495 |
liveuser |
34 |
#函式說明:
|
|
|
35 |
#建立腳本檔案.
|
|
|
36 |
#回傳結果:
|
|
|
37 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
38 |
#$result["error"],錯誤訊息陣列.
|
|
|
39 |
#$result["function"],當前執行的函數名稱.
|
|
|
40 |
#$result["content"],腳本檔案的位置與名稱.
|
|
|
41 |
#必填參數:
|
|
|
42 |
#$conf["cmd"],要執行的指令字串陣列.
|
|
|
43 |
$conf["cmd"]=array("ps");
|
|
|
44 |
#可省略參數:
|
|
|
45 |
#$conf["params"],要執行對應指令所屬的參數字串陣列.
|
|
|
46 |
$conf["params"]=array(array("auxwf","|","grep","httpd"));
|
|
|
47 |
#$conf["excuteBy"],字串,預設不指定,反之為執行該腳本的程式絕對路徑,例如"bash"、"php"之類的.
|
|
|
48 |
$conf["excuteBy"]="bash";
|
|
|
49 |
#$conf["fileName"],字串,指定要輸出的檔案名稱.
|
|
|
50 |
$conf["fileName"]="assets of cmd/createBatch.sh";
|
|
|
51 |
#參考資料:
|
|
|
52 |
#無.
|
|
|
53 |
#備註:
|
|
|
54 |
#無.
|
|
|
55 |
$createBatch=cmd::createBatch($conf);
|
|
|
56 |
unset($conf);
|
|
|
57 |
|
|
|
58 |
#印出結果
|
|
|
59 |
var_dump($createBatch);
|
|
|
60 |
|
|
|
61 |
#如果執行失敗
|
|
|
62 |
if($createBatch["status"]==="false"){
|
|
|
63 |
|
|
|
64 |
#結束執行,錯誤代碼1
|
|
|
65 |
exit(1);
|
|
|
66 |
|
|
|
67 |
}#if end
|
|
|
68 |
|
|
|
69 |
#取得腳本檔案的內容
|
|
|
70 |
#函式說明:
|
|
|
71 |
#依據行號分隔抓取檔案的內容,結果會回傳一個陣列
|
|
|
72 |
#回傳的變數說明:
|
|
|
73 |
#$result["status"],執行是否成功,"true"代表成功;"fasle"代表失敗.
|
|
|
74 |
#$result["error"],錯誤訊息提示.
|
|
|
75 |
#$result["warning"],警告訊息.
|
|
|
76 |
#$result["function"],當前執行的函數名稱.
|
|
|
77 |
#$result["fileContent"],爲檔案的內容陣列.
|
|
|
78 |
#$result["lineCount"],爲檔案內容總共的行數.
|
|
|
79 |
#$result["fullContent"],為檔案的完整內容.
|
|
|
80 |
#$result["base64data"],為檔案的base64內容.
|
|
|
81 |
#$result["mimeType"],為檔案的mime type.
|
|
|
82 |
#必填參數:
|
|
|
83 |
#$conf["filePositionAndName"],字串,爲檔案的位置以及名稱.
|
|
|
84 |
$conf["filePositionAndName"]=$createBatch["content"];
|
|
|
85 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
|
|
|
86 |
$conf["fileArgu"]=__FILE__;
|
|
|
87 |
#可省略參數:
|
|
|
88 |
#$conf["web"],是要取得網路上的檔案則為"true";反之則為"false".
|
|
|
89 |
$conf["web"]="false";
|
|
|
90 |
#參考資料:
|
|
|
91 |
#file(),取得檔案內容的行數.
|
|
|
92 |
#file=>http:#php.net/manual/en/function.file.php
|
|
|
93 |
#rtrim(),剔除透過file()取得每行內容結尾的換行符號.
|
|
|
94 |
#filesize=>http://php.net/manual/en/function.filesize.php
|
|
|
95 |
#參考資料:
|
|
|
96 |
#無.
|
|
|
97 |
#備註:
|
|
|
98 |
#無.
|
|
|
99 |
$getFileContent=fileAccess::getFileContent($conf);
|
|
|
100 |
unset($conf);
|
|
|
101 |
|
|
|
102 |
#印出結果
|
|
|
103 |
var_dump($getFileContent);
|
|
|
104 |
|
|
|
105 |
#如果執行失敗
|
|
|
106 |
if($getFileContent["status"]==="false"){
|
|
|
107 |
|
|
|
108 |
#結束執行,錯誤代碼1
|
|
|
109 |
exit(1);
|
|
|
110 |
|
|
|
111 |
}#if end
|
|
|
112 |
|
|
|
113 |
#將腳本給予執行的權限
|
|
|
114 |
#函式說明:
|
|
|
115 |
#呼叫shell執行系統命令,並取得回傳的內容.
|
|
|
116 |
#回傳結果:
|
|
|
117 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
118 |
#$result["error"],錯誤訊息陣列.
|
|
|
119 |
#$result["function"],當前執行的函數名稱.
|
|
|
120 |
#$result["argu"],使用的參數.
|
|
|
121 |
#$result["cmd"],執行的指令內容.
|
|
|
122 |
#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
123 |
#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
|
|
|
124 |
#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
125 |
#$result["running"],是否還在執行.
|
|
|
126 |
#$result["pid"],pid.
|
|
|
127 |
#$result["statusCode"],執行結束後的代碼.
|
|
|
128 |
#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu").
|
|
|
129 |
#必填參數:
|
|
|
130 |
#$conf["command"],字串,要執行的指令.
|
|
|
131 |
$conf["command"]="chmod";
|
|
|
132 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
|
|
133 |
$conf["fileArgu"]=__FILE__;
|
|
|
134 |
#可省略參數:
|
|
|
135 |
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
|
|
|
136 |
$conf["argu"]=array("+x",$createBatch["content"]);
|
|
|
137 |
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
|
|
|
138 |
#$conf["arguIsAddr"]=array();
|
|
|
139 |
#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
|
|
|
140 |
#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
|
|
|
141 |
#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
|
|
|
142 |
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
|
|
|
143 |
#$conf["enablePrintDescription"]="true";
|
|
|
144 |
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
|
|
|
145 |
#$conf["printDescription"]="";
|
|
|
146 |
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
|
|
|
147 |
$conf["escapeshellarg"]="true";
|
|
|
148 |
#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
|
|
|
149 |
#$conf["thereIsShellVar"]=array();
|
|
|
150 |
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
|
|
|
151 |
#$conf["username"]="";
|
|
|
152 |
#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
|
|
153 |
#$conf["password"]="";
|
|
|
154 |
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
|
|
|
155 |
#$conf["useScript"]="";
|
|
|
156 |
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
|
|
|
157 |
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
|
|
|
158 |
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
|
|
|
159 |
#$conf["inBackGround"]="";
|
|
|
160 |
#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
|
|
|
161 |
#$conf["getErr"]="false";
|
|
|
162 |
#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
|
|
|
163 |
#$conf["doNotRun"]="false";
|
|
|
164 |
#參考資料:
|
|
|
165 |
#exec=>http://php.net/manual/en/function.exec.php
|
|
|
166 |
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
|
|
|
167 |
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
|
|
|
168 |
#備註:
|
|
|
169 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
|
|
|
170 |
#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
|
|
|
171 |
$callShell=external::callShell($conf);
|
|
|
172 |
unset($conf);
|
|
|
173 |
|
|
|
174 |
#印出結果
|
|
|
175 |
var_dump($callShell);
|
|
|
176 |
|
|
|
177 |
#如果執行失敗
|
|
|
178 |
if($callShell["status"]==="false"){
|
|
|
179 |
|
|
|
180 |
#結束執行,錯誤代碼1
|
|
|
181 |
exit(1);
|
|
|
182 |
|
|
|
183 |
}#if end
|
|
|
184 |
|
|
|
185 |
#執行腳本
|
|
|
186 |
#函式說明:
|
|
|
187 |
#呼叫shell執行編譯過的2元碼程式或具備執行權限的檔案後回傳的結果
|
|
|
188 |
#回傳結果:
|
|
|
189 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
190 |
#$result["function"],正在執行的函式
|
|
|
191 |
#$result["error"],錯誤訊息陣列
|
|
|
192 |
#$result["error"]["returnCode"],錯誤代碼.
|
|
|
193 |
#$result["output"],爲執行完二元碼後的輸出陣列.
|
|
|
194 |
#$result["escapedCmd"],實際執行的指令.
|
|
|
195 |
#必填參數:
|
|
|
196 |
$conf["exeFileAddress"]=$createBatch["content"];#可執行檔的位置與名稱
|
|
|
197 |
#可省略參數:
|
|
|
198 |
#$conf["arguments"]="";#要給執行檔的參數
|
|
|
199 |
#參考資料:
|
|
|
200 |
#http://php.net/manual/en/function.exec.php
|
|
|
201 |
#備註:
|
|
|
202 |
#無.
|
|
|
203 |
$execByteCode=external::execByteCode($conf);
|
|
|
204 |
unset($conf);
|
|
|
205 |
|
|
|
206 |
#印出結果
|
|
|
207 |
var_dump($execByteCode);
|
|
|
208 |
|
|
|
209 |
#如果執行失敗
|
|
|
210 |
if($execByteCode["status"]==="false"){
|
|
|
211 |
|
|
|
212 |
#結束執行,錯誤代碼1
|
|
|
213 |
exit(1);
|
|
|
214 |
|
|
|
215 |
}#if end
|
|
|
216 |
|
|
|
217 |
#移除腳本檔案
|
|
|
218 |
#函式說明:
|
|
|
219 |
#移除檔案
|
|
|
220 |
#回傳結果:
|
|
|
221 |
#$result["status"],"true"代表移除成功,"false"代表移除失敗.
|
|
|
222 |
#$result["error"],錯誤訊息陣列.
|
|
|
223 |
#$result["warning"],警告訊息陣列.
|
|
|
224 |
#$result["function"],當前執行的函數名稱.
|
|
|
225 |
#$result["argu"],當前函式使用的參數.
|
|
|
226 |
#必填參數:
|
|
|
227 |
#$conf["fileAddress"],字串,要移除檔案的位置.
|
|
|
228 |
$conf["fileAddress"]=$createBatch["content"];
|
|
|
229 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.
|
|
|
230 |
$conf["fileArgu"]=__FILE__;
|
|
|
231 |
#可省略參數:
|
|
|
232 |
#$conf["commentsArray"],字串陣列,提示的文字描述,$conf["commentsArray"][$i]代表第($+1)行的描述.
|
|
|
233 |
#$conf["commentsArray"]=array("");
|
|
|
234 |
#$conf["allowDelSymlink"],字串,預設為"false",不移除軟連結;"true"代表要移除軟連結.
|
|
|
235 |
#$conf["allowDelSymlink"]="true";
|
|
|
236 |
#$conf["allowDelFolder"],字串,預設為"false",不移除目錄;"true"代表要移除目錄.
|
|
|
237 |
#$conf["allowDelFolder"]="true";
|
|
|
238 |
#參考資料:
|
|
|
239 |
#無.
|
|
|
240 |
#備註:
|
|
|
241 |
#無.
|
|
|
242 |
$delFile=fileAccess::delFile($conf);
|
|
|
243 |
unset($conf);
|
|
|
244 |
|
|
|
245 |
#印出結果
|
|
|
246 |
var_dump($delFile);
|
|
|
247 |
|
|
|
248 |
#如果執行失敗
|
|
|
249 |
if($delFile["status"]==="false"){
|
|
|
250 |
|
|
|
251 |
#結束執行,錯誤代碼1
|
|
|
252 |
exit(1);
|
|
|
253 |
|
|
|
254 |
}#if end
|
|
|
255 |
|
|
|
256 |
?>
|