| 3 |
liveuser |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
|
|
|
5 |
QBPWCF, Quick Build PHP website Component base on Fedora Linux.
|
| 239 |
liveuser |
6 |
Copyright (C) 2014~2026 MIN ZHI, CHEN
|
| 3 |
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 |
namespace qbpwcf;
|
|
|
25 |
|
|
|
26 |
/*
|
|
|
27 |
類別說明:
|
|
|
28 |
跟抓取網頁內容有關的類別.
|
|
|
29 |
備註:
|
|
|
30 |
無.
|
|
|
31 |
*/
|
|
|
32 |
class catchWebContent{
|
|
|
33 |
|
|
|
34 |
/*
|
|
|
35 |
#函式說明:
|
|
|
36 |
#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
|
|
|
37 |
#回傳結果:
|
|
|
38 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
39 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
40 |
#$result["function"],當前執行的函式名稱.
|
|
|
41 |
#必填參數:
|
|
|
42 |
#$method,物件,為物件實體或類別名稱,會自動置入該參數.
|
|
|
43 |
#$arguments,陣列,為呼叫方法時所用的參數.
|
|
|
44 |
#參考資料:
|
|
|
45 |
#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
|
|
|
46 |
*/
|
|
|
47 |
public function __call($method,$arguments){
|
|
|
48 |
|
|
|
49 |
#取得當前執行的函式
|
|
|
50 |
$result["function"]=__FUNCTION__;
|
|
|
51 |
|
|
|
52 |
#設置執行不正常
|
|
|
53 |
$result["status"]="false";
|
|
|
54 |
|
|
|
55 |
#設置執行錯誤
|
|
|
56 |
$result["error"][]=__NAMESPACE__ ."/".$method."() 不存在!";
|
|
|
57 |
|
|
|
58 |
#設置所丟入的參數
|
|
|
59 |
$result["error"][]=$arguments;
|
|
|
60 |
|
|
|
61 |
#回傳結果
|
|
|
62 |
return $result;
|
|
|
63 |
|
|
|
64 |
}#function __call end
|
|
|
65 |
|
|
|
66 |
/*
|
|
|
67 |
#函式說明:
|
|
|
68 |
#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
|
|
|
69 |
#回傳結果:
|
|
|
70 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
71 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
72 |
#$result["function"],當前執行的函式名稱.
|
|
|
73 |
#必填參數:
|
|
|
74 |
#$method,物件,為物件實體或類別名稱,會自動置入該參數.
|
|
|
75 |
#$arguments,陣列,為呼叫方法時所用的參數.
|
|
|
76 |
#參考資料:
|
|
|
77 |
#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
|
|
|
78 |
*/
|
|
|
79 |
public static function __callStatic($method,$arguments){
|
|
|
80 |
|
|
|
81 |
#取得當前執行的函式
|
|
|
82 |
$result["function"]=__FUNCTION__;
|
|
|
83 |
|
|
|
84 |
#設置執行不正常
|
|
|
85 |
$result["status"]="false";
|
|
|
86 |
|
|
|
87 |
#設置執行錯誤
|
|
|
88 |
$result["error"][]="欲呼叫的". __NAMESPACE__ ."/".$method."() 不存在!";
|
|
|
89 |
|
|
|
90 |
#設置所丟入的參數
|
|
|
91 |
$result["error"][]=$arguments;
|
|
|
92 |
|
|
|
93 |
#回傳結果
|
|
|
94 |
return $result;
|
|
|
95 |
|
|
|
96 |
}#function __callStatic end
|
|
|
97 |
|
|
|
98 |
/*
|
|
|
99 |
#函式說明:
|
|
|
100 |
#抓取的特定網頁的某段內容
|
|
|
101 |
#回傳的結果:
|
|
|
102 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
103 |
#$result["error"],錯誤訊息陣列.
|
|
|
104 |
#$result["function"],函數名稱.
|
|
|
105 |
#$result["webContent"]["lineCount"],爲總共有機行原始碼。
|
|
|
106 |
#$result["webContent"]["full"],爲抓取的全部網頁程式碼
|
|
|
107 |
#$result["webContent"][i],爲第i+1行的程式碼
|
|
|
108 |
#必填參數:
|
|
|
109 |
#$conf["loadingPotision"],字串,要抓取的網頁網址
|
|
|
110 |
$conf["loadingPotision"]="";#要抓取的網頁網址
|
|
|
111 |
#可省略參數:
|
|
|
112 |
#$conf["startLineNum"],從第幾行開始擷取,預設爲從第1行(第0個元素)。
|
|
|
113 |
#$conf["startLineNum"]="";
|
|
|
114 |
#$conf["endLineNum"],到第幾行結束截取,預設爲從最後一行(最後一個元素)。
|
|
|
115 |
#$conf["endLineNum"]="";
|
|
|
116 |
#$conf["processString"],爲是否要變更特定字串內容,預設爲false,不做變更,若設爲"true"則爲要進行處理字串的功能。
|
|
|
117 |
#$conf["processString"]="";
|
|
|
118 |
#$conf["selectedCharacter"],d爲被選擇要處理的字串/字元,須爲陣列值。
|
|
|
119 |
#$conf["selectedCharacter"]=array("");
|
|
|
120 |
#若不設定則預設爲要將這些字串作替換(" ","href","iframe","<",">","<a","/>",";","/","=","#",".","'","$","%","&","|")。
|
|
|
121 |
#$conf["changeTo"],爲被選擇的字元要換成什麼字串/字元,須爲陣列值。若不設定,則預設爲更換成""(空字串)。
|
|
|
122 |
#$conf["changeTo"]=array("");
|
|
|
123 |
#$conf["changeCharSet"],字元陣列,轉換全文編碼,預設不使用,$conf["changeCharSet"][0]為原始編碼,$conf["changeCharSet"][0]為新的編碼.
|
|
|
124 |
#$conf["changeCharSet"]=array("big5","utf-8");
|
|
|
125 |
#備註:
|
|
|
126 |
#$conf["changeCharSet"]參數沒有效果.
|
|
|
127 |
#有些網站會無法透過此方法取得網站內容.
|
|
|
128 |
#參考資料:
|
|
|
129 |
#iconv用法 => http://php.net/manual/en/function.iconv.php
|
|
|
130 |
*/
|
|
|
131 |
public static function getFullWeb($conf){
|
|
|
132 |
|
|
|
133 |
#初始化要回傳的變數
|
|
|
134 |
$result=array();
|
|
|
135 |
|
|
|
136 |
#取得當前函數名稱
|
|
|
137 |
$result["function"]=__FUNCTION__;
|
|
|
138 |
|
|
|
139 |
#初始化回傳的變數
|
|
|
140 |
$result["webContent"]["full"]="";
|
|
|
141 |
|
|
|
142 |
#如果 $conf 不為陣列
|
|
|
143 |
if(gettype($conf)!="array"){
|
|
|
144 |
|
|
|
145 |
#設置執行失敗
|
|
|
146 |
$result["status"]="false";
|
|
|
147 |
|
|
|
148 |
#設置執行錯誤訊息
|
|
|
149 |
$result["error"][]="\$conf變數須為陣列形態";
|
|
|
150 |
|
|
|
151 |
#如果傳入的參數為 null
|
|
|
152 |
if($conf==null){
|
|
|
153 |
|
|
|
154 |
#設置執行錯誤訊息
|
|
|
155 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
|
|
156 |
|
|
|
157 |
}#if end
|
|
|
158 |
|
|
|
159 |
#回傳結果
|
|
|
160 |
return $result;
|
|
|
161 |
|
|
|
162 |
}#if end
|
|
|
163 |
|
|
|
164 |
#檢查參數
|
|
|
165 |
#函式說明:
|
|
|
166 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
167 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
168 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
169 |
#$result["function"],當前執行的函式名稱.
|
|
|
170 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
171 |
#$result[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
172 |
#$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
173 |
#$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
174 |
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
|
|
|
175 |
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
|
|
|
176 |
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
|
|
|
177 |
#必填寫的參數:
|
|
|
178 |
#$conf["checkArguments::checkArguments"]["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
179 |
$conf["checkArguments::checkArguments"]["varInput"]=&$conf;
|
|
|
180 |
#$conf["checkArguments::checkArguments"]["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
181 |
$conf["checkArguments::checkArguments"]["mustBeFilledVariableName"]=array("loadingPotision");
|
|
|
182 |
#$conf["checkArguments::checkArguments"]["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double");
|
|
|
183 |
$conf["checkArguments::checkArguments"]["mustBeFilledVariableType"]=array("string");
|
|
|
184 |
#$conf["checkArguments::checkArguments"]["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
185 |
$conf["checkArguments::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
186 |
#可以省略的參數:
|
|
|
187 |
#$conf["checkArguments::checkArguments"]["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true"。
|
|
|
188 |
$conf["checkArguments::checkArguments"]["canBeEmptyString"]="false";
|
|
|
189 |
#$conf["checkArguments::checkArguments"]["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
|
|
190 |
$conf["checkArguments::checkArguments"]["skipableVariableName"]=array("startLineNum","endLineNum","processString","selectedCharacter","changeTo");
|
|
|
191 |
#$conf["checkArguments::checkArguments"]["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
|
|
192 |
$conf["checkArguments::checkArguments"]["skipableVariableType"]=array("integer","integer","string","array","array");
|
|
|
193 |
#$conf["checkArguments::checkArguments"]["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
|
|
194 |
$conf["checkArguments::checkArguments"]["skipableVarDefaultValue"]=array(0,null,"false",null,null);
|
|
|
195 |
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
196 |
#$conf["arrayCountEqualCheck"][]=array();
|
|
|
197 |
#參考資料來源:
|
|
|
198 |
#array_keys=>http://php.net/manual/en/function.array-keys.php
|
|
|
199 |
$checkResult=variableCheck::checkArguments($conf["checkArguments::checkArguments"]);
|
|
|
200 |
unset($conf["checkArguments::checkArguments"]);
|
|
|
201 |
|
|
|
202 |
#如果檢查參數失敗
|
|
|
203 |
if($checkResult["status"]=="false"){
|
|
|
204 |
|
|
|
205 |
#設置執行失敗
|
|
|
206 |
$result["status"]="false";
|
|
|
207 |
|
|
|
208 |
#設置錯誤訊息
|
|
|
209 |
$result["error"]=$checkResult;
|
|
|
210 |
|
|
|
211 |
#回傳結果
|
|
|
212 |
return $result;
|
|
|
213 |
|
|
|
214 |
}#if end
|
|
|
215 |
|
|
|
216 |
#將網頁原始碼依據行來存成陣列
|
|
|
217 |
$webArrayContent=file($conf["loadingPotision"]);
|
|
|
218 |
|
|
|
219 |
#如果取得網頁內容失敗
|
|
|
220 |
if($webArrayContent==FALSE){
|
|
|
221 |
|
|
|
222 |
#設置執行失敗
|
|
|
223 |
$result["status"]="false";
|
|
|
224 |
|
|
|
225 |
#設置錯誤訊息
|
|
|
226 |
$result["error"][]="取得網頁內容失敗";
|
|
|
227 |
|
|
|
228 |
#回傳結果
|
|
|
229 |
return $result;
|
|
|
230 |
|
|
|
231 |
}#if end
|
|
|
232 |
|
|
|
233 |
#計算總共有幾行
|
|
|
234 |
$fileLines=count($webArrayContent);
|
|
|
235 |
|
|
|
236 |
#如果$conf["endLineNum"]沒有設定
|
|
|
237 |
if(!isset($conf["endLineNum"])){
|
|
|
238 |
|
|
|
239 |
#則預設爲最後一行
|
|
|
240 |
$conf["endLineNum"]=$fileLines;
|
|
|
241 |
|
|
|
242 |
}#if end
|
|
|
243 |
|
|
|
244 |
#用迴圈將陣列印出來
|
|
|
245 |
for($i=$conf["startLineNum"];$i<$conf["endLineNum"];$i++){
|
|
|
246 |
|
|
|
247 |
#如果 $conf["processString"] 爲 "true";
|
|
|
248 |
if($conf["processString"]=="true"){
|
|
|
249 |
|
|
|
250 |
#函式說明:
|
|
|
251 |
#處理字串避免網頁出錯
|
|
|
252 |
#回傳的結果:
|
|
|
253 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
254 |
#$result["function"],當前執行的函數.
|
|
|
255 |
#$result["content"],爲處理好的字串.
|
|
|
256 |
#$result["error"],錯誤訊息陣列.
|
|
|
257 |
|
|
|
258 |
#則將特定的內容改變
|
|
|
259 |
$conf_correctCharacter["stringIn"]=$webArrayContent[$i];#爲要處理的字串
|
|
|
260 |
|
|
|
261 |
#如果 $conf["selectedCharacter"] 有設定則套用設定值
|
|
|
262 |
if(isset($conf["selectedCharacter"])){
|
|
|
263 |
|
|
|
264 |
#爲被選擇要處理的字串/字元,須爲陣列值。
|
|
|
265 |
$conf_correctCharacter["selectedCharacter"]=$conf["selectedCharacter"];
|
|
|
266 |
|
|
|
267 |
}#if end
|
|
|
268 |
|
|
|
269 |
#如果 $conf["changeTo"] 有設定則套用設定值
|
|
|
270 |
if(isset($conf["changeTo"])){
|
|
|
271 |
|
|
|
272 |
#爲被選擇的字元要換成什麼字串/字元,須爲陣列值。若不設定,則預設爲更換成""(空字串)。
|
|
|
273 |
$conf_correctCharacter["changeTo"]=$conf["changeTo"];
|
|
|
274 |
|
|
|
275 |
}#if end
|
|
|
276 |
|
|
|
277 |
$correctCharacter=stringProcess::correctCharacter($conf_correctCharacter);
|
|
|
278 |
unset($conf_correctCharacter);#清空變數避免錯誤
|
|
|
279 |
|
|
|
280 |
#如果修正字串失敗
|
|
|
281 |
if($correctCharacter["status"]=="false"){
|
|
|
282 |
|
|
|
283 |
#設置執行失敗
|
|
|
284 |
$result["status"]="false";
|
|
|
285 |
|
|
|
286 |
#設置錯誤訊息
|
|
|
287 |
$result["error"]=$correctCharacter;
|
|
|
288 |
|
|
|
289 |
#回傳結果
|
|
|
290 |
return $result;
|
|
|
291 |
|
|
|
292 |
}#if end
|
|
|
293 |
|
|
|
294 |
#取得該行處理好的字串
|
|
|
295 |
$webArrayContent[$i]=$correctCharacter["content"];
|
|
|
296 |
|
|
|
297 |
}#if end
|
|
|
298 |
|
|
|
299 |
#則儲存該行的內容
|
|
|
300 |
$result["webContent"][$i]=$webArrayContent[$i];
|
|
|
301 |
$result["webContent"]["full"]=$result["webContent"]["full"].$webArrayContent[$i];
|
|
|
302 |
|
|
|
303 |
}#for end
|
|
|
304 |
|
|
|
305 |
#儲存有幾行程式碼
|
|
|
306 |
$result["webContent"]["lineCount"]=$fileLines;
|
|
|
307 |
|
|
|
308 |
#/* 似乎沒有效果,所以先註解掉
|
|
|
309 |
|
|
|
310 |
#如果 $conf["changeCharSet"][0、1] 有設定
|
|
|
311 |
if(isset($conf["changeCharSet"][0]) && isset($conf["changeCharSet"][1])){
|
|
|
312 |
|
|
|
313 |
#將全文的編碼轉換
|
|
|
314 |
$result["webContent"]["full"]=iconv($conf["changeCharSet"][0],$conf["changeCharSet"][1],$result["webContent"]["full"]);
|
|
|
315 |
|
|
|
316 |
}#if end
|
|
|
317 |
|
|
|
318 |
#*/
|
|
|
319 |
|
|
|
320 |
#執行到這邊代表執行正常
|
|
|
321 |
$result["status"]="true";
|
|
|
322 |
|
|
|
323 |
#回傳變數內容
|
|
|
324 |
return $result;
|
|
|
325 |
|
|
|
326 |
}#function getFullWeb end
|
|
|
327 |
|
|
|
328 |
/*
|
|
|
329 |
#函式說明:
|
|
|
330 |
#抓取的特定網頁的某段內容
|
|
|
331 |
#回傳的結果:
|
|
|
332 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
333 |
#$result["error"],錯誤訊息陣列.
|
|
|
334 |
#$result["function"],函數名稱.
|
|
|
335 |
#$result["oriWebContent"]["full"],原始檔案的完整內容.
|
|
|
336 |
#$result["oriWebContent"]["byLine"],用陣列儲存的原始檔案逐行內容.
|
|
|
337 |
#$result["oriWebContent"]["lineCount"],原始檔案的總共行數.
|
|
|
338 |
#$result["webContent"]["lineCount"],爲總共有機行原始碼。
|
|
|
339 |
#$result["webContent"]["full"],為檔案的完整內容,爲抓取的全部網頁程式碼
|
|
|
340 |
#$result["webContent"][i],爲第i+1行的程式碼
|
|
|
341 |
#必填參數:
|
|
|
342 |
#$conf["loadingPotision"],字串,要抓取的網頁網址.
|
|
|
343 |
$conf["loadingPotision"]="";
|
|
|
344 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.
|
|
|
345 |
$conf["fileArgu"]=__FILE__;
|
|
|
346 |
#可省略參數:
|
|
|
347 |
#$conf["startLineNum"]="";#從第幾行開始擷取,預設爲從第1行(第0個元素)。
|
|
|
348 |
#$conf["endLineNum"]="";#到第幾行結束截取,預設爲從最後一行(最後一個元素)。
|
|
|
349 |
#$conf["processString"]="";#爲是否要變更特定字串內容,預設爲false,不做變更,若設爲"true"則爲要進行處理字串的功能。
|
|
|
350 |
#$conf["selectedCharacter"]=array("");#爲被選擇要處理的字串/字元,須爲陣列值。
|
|
|
351 |
#若不設定則預設爲要將這些字串作替換(" ","href","iframe","<",">","<a","/>",";","/","=","#",".","'","$","%","&","|")。
|
|
|
352 |
#$conf["changeTo"]="";#爲被選擇的字元要換成什麼字串/字元,須爲陣列值。若不設定,則預設爲更換成""(空字串)。
|
|
|
353 |
#參考資料:
|
|
|
354 |
#無.
|
|
|
355 |
#備註:
|
|
|
356 |
#如果網址包含中文會出錯,網址若有空格也會失敗.
|
|
|
357 |
*/
|
|
|
358 |
public static function wget(&$conf){
|
|
|
359 |
|
|
|
360 |
#初始化要回傳的結果
|
|
|
361 |
$result=array();
|
|
|
362 |
|
|
|
363 |
#取得當前執行的函數名稱
|
|
|
364 |
$result["function"]=__FUNCTION__;
|
|
|
365 |
|
|
|
366 |
#如果 $conf 不為陣列
|
|
|
367 |
if(gettype($conf)!="array"){
|
|
|
368 |
|
|
|
369 |
#設置執行失敗
|
|
|
370 |
$result["status"]="false";
|
|
|
371 |
|
|
|
372 |
#設置執行錯誤訊息
|
|
|
373 |
$result["error"][]="\$conf變數須為陣列形態";
|
|
|
374 |
|
|
|
375 |
#如果傳入的參數為 null
|
|
|
376 |
if($conf==null){
|
|
|
377 |
|
|
|
378 |
#設置執行錯誤訊息
|
|
|
379 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
|
|
380 |
|
|
|
381 |
}#if end
|
|
|
382 |
|
|
|
383 |
#回傳結果
|
|
|
384 |
return $result;
|
|
|
385 |
|
|
|
386 |
}#if end
|
|
|
387 |
|
|
|
388 |
#檢查參數
|
|
|
389 |
#函式說明:
|
|
|
390 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
391 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
392 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
393 |
#$result["function"],當前執行的函式名稱.
|
|
|
394 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
395 |
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
396 |
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
397 |
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
398 |
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
|
|
|
399 |
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
|
|
|
400 |
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
|
|
|
401 |
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
|
|
|
402 |
#必填寫的參數:
|
|
|
403 |
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
404 |
$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
|
|
|
405 |
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
406 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("loadingPotision","fileArgu");
|
|
|
407 |
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
|
|
|
408 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string");
|
|
|
409 |
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
410 |
$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
411 |
#可以省略的參數:
|
|
|
412 |
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
|
|
|
413 |
#$conf["canBeEmptyString"]="false";
|
|
|
414 |
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或集合.
|
|
|
415 |
$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("startLineNum","endLineNum","selectedCharacter");
|
|
|
416 |
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
|
|
417 |
$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("startLineNum","endLineNum","processString","selectedCharacter","changeTo");
|
|
|
418 |
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
|
|
419 |
$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string","string","array","string");
|
|
|
420 |
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
|
|
421 |
$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(null,null,"false",null,"");
|
|
|
422 |
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
423 |
#$conf["arrayCountEqualCheck"][]=array();
|
|
|
424 |
#參考資料來源:
|
|
|
425 |
#array_keys=>http://php.net/manual/en/function.array-keys.php
|
|
|
426 |
$checkResult=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
|
|
|
427 |
unset($conf["variableCheck::checkArguments"]);
|
|
|
428 |
|
|
|
429 |
#如果檢查失敗
|
|
|
430 |
if($checkResult["status"]=="false"){
|
|
|
431 |
|
|
|
432 |
#設置執行失敗的識別
|
|
|
433 |
$result["status"]="false";
|
|
|
434 |
|
|
|
435 |
#設置執行失敗的訊息
|
|
|
436 |
$result["error"]=$checkResult;
|
|
|
437 |
|
|
|
438 |
#回傳結果
|
|
|
439 |
return $result;
|
|
|
440 |
|
|
|
441 |
}#if end
|
|
|
442 |
|
|
|
443 |
#如果檢查不通過
|
|
|
444 |
if($checkResult["passed"]=="false"){
|
|
|
445 |
|
|
|
446 |
#設置執行失敗的識別
|
|
|
447 |
$result["status"]="false";
|
|
|
448 |
|
|
|
449 |
#設置執行失敗的訊息
|
|
|
450 |
$result["error"]=$checkResult;
|
|
|
451 |
|
|
|
452 |
#回傳結果
|
|
|
453 |
return $result;
|
|
|
454 |
|
|
|
455 |
}#if end
|
|
|
456 |
|
|
|
457 |
#建立暫存資料夾是否存在
|
|
|
458 |
#函式說明:
|
|
|
459 |
#建立資料夾,若要建立的資料夾所屬路徑不存在,則會自動嘗試建立,建立後的資料夾也可指定權限.
|
|
|
460 |
#回傳的結果:
|
|
|
461 |
#$result["status"],"true"爲建立成功,"false"爲建立失敗.
|
|
|
462 |
#$result["error"],錯誤訊息陣列
|
|
|
463 |
#必填參數:
|
|
|
464 |
$conf["fileAccess::createFolderAfterCheck"]["dirPositionAndName"]=".catchWebContentTemp";#新建的位置與名稱
|
|
|
465 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.
|
|
|
466 |
$conf["fileAccess::createFolderAfterCheck"]["fileArgu"]=$conf["fileArgu"];
|
|
|
467 |
#可省略參數:
|
|
|
468 |
#$conf["fileAccess::createFolderAfterCheck"]["dirPermission"]="";#新建資料夾的權限設定,預設爲0770,亦即擁有者,同群組者可以讀,寫,存取,其他人僅能存取.
|
|
|
469 |
#$conf["disableWebSearch"],"字串",是否取消「當檔案找不到時,改用catchWebContent類別的wget函數來檢查檔案是否存在於網路上」的功能,預設為"false"不取消,若要取消該功能請設為"true".
|
|
|
470 |
#$conf["fileAccess::createFolderAfterCheck"]["disableWebSearch"]="true";
|
|
|
471 |
$callShell=fileAccess::createFolderAfterCheck($conf["fileAccess::createFolderAfterCheck"]);
|
|
|
472 |
unset($conf["fileAccess::createFolderAfterCheck"]);
|
|
|
473 |
|
|
|
474 |
#如果執行外部命令失敗
|
|
|
475 |
if($callShell["status"]=="false"){
|
|
|
476 |
|
|
|
477 |
#設置執行失敗的識別
|
|
|
478 |
$result["status"]="false";
|
|
|
479 |
|
|
|
480 |
#設置執行失敗的訊息
|
|
|
481 |
$result["error"]=$callShell;
|
|
|
482 |
|
|
|
483 |
#回傳結果
|
|
|
484 |
return $result;
|
|
|
485 |
|
|
|
486 |
}#if end
|
|
|
487 |
|
|
|
488 |
#涵式說明:
|
|
|
489 |
#取得系統時間目前的西元年、月、日、時、分、秒.
|
|
|
490 |
#回傳的結果:
|
|
|
491 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
492 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
493 |
#$result["function"],當前執行的函式名稱.
|
|
|
494 |
#$result["thisWestYear"],當前西元年
|
|
|
495 |
#$result["thisMonth"],當前月份
|
|
|
496 |
#$result["weekDay"],為一個星期的第幾天,0代表第一天(星期日),6代表第七天(星期六)
|
|
|
497 |
#$result["thisDay"],當前日
|
|
|
498 |
#$result["thisHour"],當前小時
|
|
|
499 |
#$result["thisMin"],當前分鐘
|
|
|
500 |
#$result["thisSec"],當前秒數
|
|
|
501 |
#必填參數:
|
|
|
502 |
#$conf["time::getSystemDateAndTime"]["timeZone"],字串,時區代號,可以設定的時區列表:「http://www.php.net/manual/en/timezones.php」,台灣可用"Asia/Taipei".
|
|
|
503 |
$conf["time::getSystemDateAndTime"]["timeZone"]="Asia/Taipei";
|
|
|
504 |
#參考資料:
|
|
|
505 |
#php的date函數用法=>http://php.net/manual/en/function.date.php
|
|
|
506 |
$systemTime=time::getSystemDateAndTime($conf["time::getSystemDateAndTime"]);
|
|
|
507 |
unset($conf["time::getSystemDateAndTime"]);
|
|
|
508 |
|
|
|
509 |
#如果取得完整的系統時間失敗
|
|
|
510 |
if($systemTime["status"]=="false"){
|
|
|
511 |
|
|
|
512 |
#設置執行失敗的識別
|
|
|
513 |
$result["status"]="false";
|
|
|
514 |
|
|
|
515 |
#設置執行失敗的訊息
|
|
|
516 |
$result["error"]=$systemTime;
|
|
|
517 |
|
|
|
518 |
#回傳結果
|
|
|
519 |
return $result;
|
|
|
520 |
|
|
|
521 |
}#if end
|
|
|
522 |
|
|
|
523 |
#初始化下載好的資料要放在哪裡,若有空格則會出錯
|
|
|
524 |
$tempDownloadDFile=".catchWebContentTemp/".$systemTime["thisWestYear"]."-".$systemTime["thisMonth"]."-".$systemTime["thisDay"]."(".$systemTime["weekDay"].")"."_".$systemTime["thisHour"].":".$systemTime["thisMin"].":".$systemTime["thisSec"].".php";
|
|
|
525 |
|
|
|
526 |
#轉換 $tempDownloadDFile 為正確的相對路徑
|
|
|
527 |
#函式說明:
|
|
|
528 |
#將多個路徑字串變成相對於當前路徑的相對路徑字串
|
|
|
529 |
#回傳結果:
|
|
|
530 |
#$result["status"],"true"爲建立成功,"false"爲建立失敗.
|
|
|
531 |
#$result["error"],錯誤訊息陣列.
|
|
|
532 |
#$result["function"],函數名稱.
|
|
|
533 |
#$result["content"],字串陣列,多個轉換好的相對路徑字串.
|
|
|
534 |
#必填參數:
|
|
|
535 |
#$conf["path"],陣列字串,要轉換成相對路徑的字串.;
|
|
|
536 |
$conf["fileAccess::getRelativePath"]["path"]=array($tempDownloadDFile);
|
|
|
537 |
#$conf["fileArgu"],字串,當前路徑.
|
|
|
538 |
$conf["fileAccess::getRelativePath"]["fileArgu"]=$conf["fileArgu"];
|
|
|
539 |
$getRelativePath=fileAccess::getRelativePath($conf["fileAccess::getRelativePath"]);
|
|
|
540 |
unset($conf["fileAccess::getRelativePath"]);
|
|
|
541 |
|
|
|
542 |
#如果轉換路徑失敗
|
|
|
543 |
if($getRelativePath["status"]==="false"){
|
|
|
544 |
|
|
|
545 |
#設置執行失敗的識別
|
|
|
546 |
$result["status"]="false";
|
|
|
547 |
|
|
|
548 |
#設置執行失敗的訊息
|
|
|
549 |
$result["error"]=$getRelativePath;
|
|
|
550 |
|
|
|
551 |
#回傳結果
|
|
|
552 |
return $result;
|
|
|
553 |
|
|
|
554 |
}#if end
|
|
|
555 |
|
|
|
556 |
#儲存轉換好的路徑
|
|
|
557 |
$tempDownloadDFile=$getRelativePath["content"][0];
|
|
|
558 |
|
|
|
559 |
#使用 wget 指令來下載網頁,要將擷取的結果儲存到檔案裡面
|
|
|
560 |
#涵式說明:
|
|
|
561 |
#呼叫shell執行系統命令,並取得回傳的內容.
|
|
|
562 |
#回傳的結果:
|
|
|
563 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
564 |
#$result["error"],錯誤訊息陣列
|
|
|
565 |
#$result["output"],爲執行完二元碼後的輸出陣列
|
|
|
566 |
#$result["function"],當前執行的函數名稱
|
|
|
567 |
#必填的參數
|
|
|
568 |
#$conf["external::callShell"]["command"],字串.
|
|
|
569 |
$conf["external::callShell"]["command"]="wget";#要執行的指令與參數.
|
|
|
570 |
#可省略參數:
|
|
|
571 |
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
|
|
|
572 |
$conf["external::callShell"]["argu"]=array("--no-check-certificate","-O",$tempDownloadDFile,$conf["loadingPotision"],"2>&1");
|
|
|
573 |
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
|
|
|
574 |
$conf["external::callShell"]["arguIsAddr"]=array("false","false","true","false","false");
|
|
|
575 |
#$conf["external::callShell"]["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
|
|
|
576 |
#$conf["external::callShell"]["enablePrintDescription"]="true";
|
|
|
577 |
#$conf["external::callShell"]["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容.
|
|
|
578 |
#$conf["external::callShell"]["printDescription"]="";
|
|
|
579 |
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"true".
|
|
|
580 |
$conf["external::callShell"]["escapeshellarg"]="true";
|
|
|
581 |
$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];
|
|
|
582 |
#參考資料:
|
|
|
583 |
#http://php.net/manual/en/function.exec.php
|
|
|
584 |
$callShell=external::callShell($conf["external::callShell"]);
|
|
|
585 |
unset($conf["external::callShell"]);
|
|
|
586 |
|
|
|
587 |
#如果執行外部命令失敗
|
|
|
588 |
if($callShell["status"]==="false"){
|
|
|
589 |
|
|
|
590 |
#設置執行失敗的識別
|
|
|
591 |
$result["status"]="false";
|
|
|
592 |
|
|
|
593 |
#設置執行失敗的訊息
|
|
|
594 |
$result["error"]=$callShell;
|
|
|
595 |
|
|
|
596 |
#回傳結果
|
|
|
597 |
return $result;
|
|
|
598 |
|
|
|
599 |
}#if end
|
|
|
600 |
|
|
|
601 |
#然後將檔案內容存到變數裡面
|
|
|
602 |
#函式說明:
|
|
|
603 |
#依據行號分隔抓取檔案的內容,結果會回傳一個陣列
|
|
|
604 |
#回傳的變數說明:
|
|
|
605 |
#$result["status"],執行是否成功,"true"代表成功;"fasle"代表失敗。
|
|
|
606 |
#$result["error"],錯誤訊息提示
|
|
|
607 |
#$result["fileContent"],爲檔案的內容陣列
|
|
|
608 |
#$result["lineCount"],爲檔案內容總共的行數
|
|
|
609 |
#$result["fullContent"],為檔案的完整內容
|
|
|
610 |
#必填參數:
|
|
|
611 |
$conf["fileAccess"]["getFileContent"]["filePositionAndName"]=$tempDownloadDFile;#爲檔案的位置以及名稱
|
|
|
612 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.
|
|
|
613 |
$conf["fileAccess"]["getFileContent"]["fileArgu"]=$conf["fileArgu"];
|
|
|
614 |
#參考資料:
|
|
|
615 |
#file():取得檔案內容的行數
|
|
|
616 |
#http:#php.net/manual/en/function.file.php
|
|
|
617 |
$oriFileContent=fileAccess::getFileContent($conf["fileAccess"]["getFileContent"]);
|
|
|
618 |
unset($conf["fileAccess"]["getFileContent"]);
|
|
|
619 |
|
|
|
620 |
#如果 $result["oriWebContent"] 等於 "false"
|
|
|
621 |
if($oriFileContent["status"]=="false"){
|
|
|
622 |
|
|
|
623 |
#代表取得檔案內容失敗
|
|
|
624 |
$result["status"]="false";
|
|
|
625 |
|
|
|
626 |
#設置執行失敗的訊息
|
|
|
627 |
$result["error"]=$oriFileContent;
|
|
|
628 |
|
|
|
629 |
#回傳結果
|
|
|
630 |
return $result;
|
|
|
631 |
|
|
|
632 |
}#if end
|
|
|
633 |
|
|
|
634 |
#原始檔案的完整內容
|
|
|
635 |
$result["oriWebContent"]["full"]=$oriFileContent["fullContent"];
|
|
|
636 |
|
|
|
637 |
#用陣列儲存的原始檔案逐行內容
|
|
|
638 |
$result["oriWebContent"]["byLine"]=$oriFileContent["fileContent"];
|
|
|
639 |
|
|
|
640 |
#原始檔案的總共行數
|
|
|
641 |
$result["oriWebContent"]["lineCount"]=$oriFileContent["lineCount"];
|
|
|
642 |
|
|
|
643 |
#移除暫存檔案
|
|
|
644 |
#函式說明:
|
|
|
645 |
#移除檔案
|
|
|
646 |
#回傳的結果:
|
|
|
647 |
#$result["status"],"true"代表移除成功,"false"代表移除失敗.
|
|
|
648 |
#$result["error"],錯誤訊息陣列
|
|
|
649 |
#$result["warning"],警告訊息陣列
|
|
|
650 |
#$result["function"],當前執行的函數名稱
|
|
|
651 |
#必填參數:
|
|
|
652 |
$conf["fileAccess"]["delFile"]["fileAddress"]=$tempDownloadDFile;#要移除檔案的位置
|
|
|
653 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.
|
|
|
654 |
$conf["fileAccess"]["delFile"]["fileArgu"]=$conf["fileArgu"];
|
|
|
655 |
$fileDeleteResult=fileAccess::delFile($conf["fileAccess"]["delFile"]);
|
|
|
656 |
unset($conf["fileAccess"]["delFile"]);
|
|
|
657 |
|
|
|
658 |
#如果移除暫存檔案失敗
|
|
|
659 |
if($fileDeleteResult["status"]=="false"){
|
|
|
660 |
|
|
|
661 |
#代表取得檔案內容失敗
|
|
|
662 |
$result["status"]="false";
|
|
|
663 |
|
|
|
664 |
#設置執行失敗的訊息
|
|
|
665 |
$result["error"]=$fileDeleteResult;
|
|
|
666 |
|
|
|
667 |
#回傳結果
|
|
|
668 |
return $result;
|
|
|
669 |
|
|
|
670 |
}#if end
|
|
|
671 |
|
|
|
672 |
#如果 $conf["startLineNum"] 沒有設定
|
|
|
673 |
if(!isset($conf["startLineNum"])){
|
|
|
674 |
|
|
|
675 |
#預測從第1行開始
|
|
|
676 |
$conf["startLineNum"]=1;
|
|
|
677 |
|
|
|
678 |
}#if end
|
|
|
679 |
|
|
|
680 |
#如果 $conf["endLineNum"] 沒有設定
|
|
|
681 |
if(!isset($conf["endLineNum"])){
|
|
|
682 |
|
|
|
683 |
#預測到最後1行
|
|
|
684 |
$conf["endLineNum"]=$result["oriWebContent"]["lineCount"];
|
|
|
685 |
|
|
|
686 |
}#if end
|
|
|
687 |
|
|
|
688 |
#初始化要回傳的網頁擷取內容
|
|
|
689 |
$result["webContent"]=array();
|
|
|
690 |
|
|
|
691 |
#取得 $conf["startLineNum"] 到 $conf["endLineNum"] 的內容
|
|
|
692 |
for($i=$conf["startLineNum"]-1;$i<$conf["endLineNum"];$i++){
|
|
|
693 |
|
|
|
694 |
#取得該行內容
|
|
|
695 |
$result["webContent"][]=$result["oriWebContent"]["byLine"][$i];
|
|
|
696 |
|
|
|
697 |
}#for end
|
|
|
698 |
|
|
|
699 |
#取得行數
|
|
|
700 |
$result["lineCount"]=count($result["webContent"]);
|
|
|
701 |
|
|
|
702 |
#如果 $conf["processString"] 沒有設定
|
|
|
703 |
if(!isset($conf["processString"])){
|
|
|
704 |
|
|
|
705 |
#則預設為 "false"
|
|
|
706 |
$conf["processString"]="false";
|
|
|
707 |
|
|
|
708 |
}#if end
|
|
|
709 |
|
|
|
710 |
#如果 $conf["processString"] 等於 "fasle"
|
|
|
711 |
if($conf["processString"]=="true"){
|
|
|
712 |
|
|
|
713 |
#剔除多餘的內容
|
|
|
714 |
#函式說明:
|
|
|
715 |
#處理多個字串避免網頁出錯
|
|
|
716 |
#回傳的結果:
|
|
|
717 |
#$result["status"],"true"代表執行成功,"false"代表執行失敗。
|
|
|
718 |
#$result["error"],錯誤訊息
|
|
|
719 |
#$result["processedStrArray"],處理好的字串陣列
|
|
|
720 |
#必填參數:
|
|
|
721 |
$conf["stringProcess"]["correctMutiStrCharacter"]["stringIn"]=$result["webContent"];#爲要處理的字串陣列
|
|
|
722 |
#可省略的參數:
|
|
|
723 |
|
|
|
724 |
#如果 #$conf["selectedCharacter"] 有設定
|
|
|
725 |
if(isset($conf["selectedCharacter"])){
|
|
|
726 |
|
|
|
727 |
#則套用設定值
|
|
|
728 |
$conf["stringProcess"]["correctMutiStrCharacter"]["selectedCharacter"]=$conf["selectedCharacter"];#爲被選擇要處理的字串/字元,須爲陣列值。
|
|
|
729 |
#若不設定則預設爲要將這些字串作替換(" ","href","iframe","<",">","<a","/>",";","/","=","#",".","'","$","%","&","|")。
|
|
|
730 |
|
|
|
731 |
}#if end
|
|
|
732 |
|
|
|
733 |
#如果 $conf["changeTo"] 有設定
|
|
|
734 |
if(isset($conf["changeTo"])){
|
|
|
735 |
|
|
|
736 |
#函式說明:
|
|
|
737 |
#建立一個陣列,可以指派陣列的內容,然後回傳
|
|
|
738 |
#回傳的結果:
|
|
|
739 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
740 |
#$result["function"],當前執行的函數名稱.
|
|
|
741 |
#$result["error"],錯誤訊息陣列.
|
|
|
742 |
#$result["content"],爲陣列變數內容
|
|
|
743 |
#必填參數:
|
|
|
744 |
$conf["arrays::create"]["arrayContent"]=array($conf["changeTo"]);#陣列元素的內容,須爲陣列值。
|
|
|
745 |
#可省略參數:
|
|
|
746 |
$conf["arrays::create"]["arrayCounts"]=count($conf["selectedCharacter"]);#爲陣列的元素有幾個,若沒設定,則數量爲$conf["arrayContent"]裏的元素數量
|
|
|
747 |
$conf["arrays::create"]["theSameAs"]="true";#若設爲"true",則所有元素內容都跟第一個元素內容相同
|
|
|
748 |
$createArray=arrays::create($conf["arrays::create"]);
|
|
|
749 |
unset($conf["arrays::create"]);
|
|
|
750 |
|
|
|
751 |
#如果建立陣列失敗
|
|
|
752 |
if($createArray["status"]=="false"){
|
|
|
753 |
|
|
|
754 |
#代表取得檔案內容失敗
|
|
|
755 |
$result["status"]="false";
|
|
|
756 |
|
|
|
757 |
#設置執行失敗的訊息
|
|
|
758 |
$result["error"]=$createArray;
|
|
|
759 |
|
|
|
760 |
#回傳結果
|
|
|
761 |
return $result;
|
|
|
762 |
|
|
|
763 |
}#if end
|
|
|
764 |
|
|
|
765 |
#則套用設定值
|
|
|
766 |
#爲被選擇要處理的字串/字元,須爲陣列值。
|
|
|
767 |
$conf["stringProcess"]["correctMutiStrCharacter"]["changeTo"]=$createArray["content"];#爲被選擇的字元要換成什麼字串/字元,須爲陣列值。若不設定,則預設爲更換成""(空字串)。
|
|
|
768 |
#若不設定則預設爲要將這些字串作替換("<",">",";","=","//","'","$","%","&","|","/*","*\/","#")。
|
|
|
769 |
#特殊字元,「\n」代表換行,「\t」代表tab鍵的間隔
|
|
|
770 |
|
|
|
771 |
}#if end
|
|
|
772 |
|
|
|
773 |
$stringProcessResult=stringProcess::correctMutiStrCharacter($conf["stringProcess"]["correctMutiStrCharacter"]);
|
|
|
774 |
unset($conf["stringProcess"]["correctMutiStrCharacter"]);
|
|
|
775 |
|
|
|
776 |
#如果執行失敗
|
|
|
777 |
if($stringProcessResult["status"]=="false"){
|
|
|
778 |
|
|
|
779 |
#設置執行錯誤的識別
|
|
|
780 |
$result["status"]="false";
|
|
|
781 |
|
|
|
782 |
#設置錯誤訊息
|
|
|
783 |
$result["error"]=$stringProcessResult;
|
|
|
784 |
|
|
|
785 |
#回傳結果
|
|
|
786 |
return $result;
|
|
|
787 |
|
|
|
788 |
}#if end
|
|
|
789 |
|
|
|
790 |
#執行到這邊,代表執行成功
|
|
|
791 |
else{
|
|
|
792 |
|
|
|
793 |
#抓取處理好的內容
|
|
|
794 |
$result["webContent"]=$stringProcessResult["processedStrArray"];
|
|
|
795 |
|
|
|
796 |
}#else end
|
|
|
797 |
|
|
|
798 |
}#if end
|
|
|
799 |
|
|
|
800 |
#初始化擷取的網頁全文
|
|
|
801 |
$result["webContent"]["full"]="";
|
|
|
802 |
|
|
|
803 |
#將每行串連起來
|
|
|
804 |
for($i=0;$i<$result["lineCount"];$i++){
|
|
|
805 |
|
|
|
806 |
$result["webContent"]["full"]=$result["webContent"]["full"].$result["webContent"][$i];
|
|
|
807 |
|
|
|
808 |
}#for end
|
|
|
809 |
|
|
|
810 |
#執行到這邊代表執行成功
|
|
|
811 |
$result["status"]="true";
|
|
|
812 |
|
|
|
813 |
#回傳結果
|
|
|
814 |
return $result;
|
|
|
815 |
|
|
|
816 |
}#function wget end
|
|
|
817 |
|
|
|
818 |
/*
|
|
|
819 |
#函式說明:
|
|
|
820 |
#將特定網址的網頁內容儲存成html檔案
|
|
|
821 |
#回傳結果:
|
|
|
822 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
823 |
#$result["error"],錯誤訊息陣列.
|
|
|
824 |
#$result["function"],當前執行的函式名稱.
|
|
|
825 |
#$result["html"],取得的網頁檔位置.
|
|
|
826 |
#必填參數:
|
|
|
827 |
#$conf["saveLocation"],字串,下載好的網頁要儲存到哪邊,副檔名會自動加上.
|
|
|
828 |
$conf["saveLocation"]="";
|
|
|
829 |
#$conf["webPageLocation"],字串,要下載的網頁目錄位置與檔案名稱.ex:「/about.php」,若是要下載首頁則可以用「/」.
|
|
|
830 |
$conf["webPageLocation"]="";
|
|
|
831 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.
|
|
|
832 |
$conf["fileArgu"]=__FILE__;
|
|
|
833 |
#可省略參數:
|
|
|
834 |
#$conf["type"],字串,網頁的協定,預設為"http",可以用的還有"https".
|
|
|
835 |
#$conf["type"]="http";
|
|
|
836 |
#$conf["webHostAddressOrDNS"],字串,網頁所處的IP或DNS,預設為"127.0.0.1".
|
|
|
837 |
#$conf["webHostAddressOrDNS"]="127.0.0.1";
|
|
|
838 |
#$conf["port"],字串,要使用的port,預設"80",一般來說http為80,https為443.
|
|
|
839 |
#$conf["port"]="80";
|
|
|
840 |
#參考資料:
|
|
|
841 |
#無.
|
|
|
842 |
#備註:
|
|
|
843 |
#無.
|
|
|
844 |
*/
|
|
|
845 |
public static function toHtml(&$conf){
|
|
|
846 |
|
|
|
847 |
#設置要回傳的變數
|
|
|
848 |
$result=array();
|
|
|
849 |
|
|
|
850 |
#設置當前執行的函式名稱
|
|
|
851 |
$result["function"]=__FUNCTION__;
|
|
|
852 |
|
|
|
853 |
#如果 $conf 不為陣列
|
|
|
854 |
if(gettype($conf)!="array"){
|
|
|
855 |
|
|
|
856 |
#設置執行失敗
|
|
|
857 |
$result["status"]="false";
|
|
|
858 |
|
|
|
859 |
#設置執行錯誤訊息
|
|
|
860 |
$result["error"][]="\$conf變數須為陣列形態";
|
|
|
861 |
|
|
|
862 |
#如果傳入的參數為 null
|
|
|
863 |
if($conf==null){
|
|
|
864 |
|
|
|
865 |
#設置執行錯誤訊息
|
|
|
866 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
|
|
867 |
|
|
|
868 |
}#if end
|
|
|
869 |
|
|
|
870 |
#回傳結果
|
|
|
871 |
return $result;
|
|
|
872 |
|
|
|
873 |
}#if end
|
|
|
874 |
|
|
|
875 |
#檢查參數
|
|
|
876 |
#函式說明:
|
|
|
877 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
878 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
879 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
880 |
#$result["function"],當前執行的函式名稱.
|
|
|
881 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
882 |
#$result[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
883 |
#$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
884 |
#$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
885 |
#必填寫的參數:
|
|
|
886 |
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
887 |
$conf["variableCheck.checkArguments"]["varInput"]=&$conf;
|
|
|
888 |
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
889 |
$conf["variableCheck.checkArguments"]["mustBeFilledVariableName"]=array("saveLocation","webPageLocation","fileArgu");
|
|
|
890 |
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double");
|
|
|
891 |
$conf["variableCheck.checkArguments"]["mustBeFilledVariableType"]=array("string","string","string");
|
|
|
892 |
#$conf["variableCheck.checkArguments"]["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
893 |
$conf["variableCheck.checkArguments"]["referenceVarKey"]="variableCheck.checkArguments";
|
|
|
894 |
#可以省略的參數:
|
|
|
895 |
#$conf["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"false"。
|
|
|
896 |
#$conf["canBeEmptyString"]="false";
|
|
|
897 |
#$conf["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
|
|
898 |
$conf["variableCheck.checkArguments"]["skipableVariableName"]=array("type","webHostAddressOrDNS","port");
|
|
|
899 |
#$conf["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
|
|
900 |
$conf["variableCheck.checkArguments"]["skipableVariableType"]=array("string","string","string");
|
|
|
901 |
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,"null"代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
|
|
902 |
$conf["variableCheck.checkArguments"]["skipableVarDefaultValue"]=array("http","127.0.0.1","80");
|
|
|
903 |
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
904 |
#$conf["arrayCountEqualCheck"][]=array();
|
|
|
905 |
$checkArguments=variableCheck::checkArguments($conf["variableCheck.checkArguments"]);
|
|
|
906 |
unset($conf["variableCheck.checkArguments"]);
|
|
|
907 |
|
|
|
908 |
#如果檢查失敗
|
|
|
909 |
if($checkArguments["status"]=="false"){
|
|
|
910 |
|
|
|
911 |
#設置執行不正常
|
|
|
912 |
$result["status"]="false";
|
|
|
913 |
|
|
|
914 |
#設置執行錯誤訊息
|
|
|
915 |
$result["error"]=$checkArguments;
|
|
|
916 |
|
|
|
917 |
#回傳結果
|
|
|
918 |
return $result;
|
|
|
919 |
|
|
|
920 |
}#if end
|
|
|
921 |
|
|
|
922 |
#如果檢查不通過
|
|
|
923 |
if($checkArguments["passed"]=="false"){
|
|
|
924 |
|
|
|
925 |
#設置執行不正常
|
|
|
926 |
$result["status"]="false";
|
|
|
927 |
|
|
|
928 |
#設置執行錯誤訊息
|
|
|
929 |
$result["error"]=$checkArguments;
|
|
|
930 |
|
|
|
931 |
#回傳結果
|
|
|
932 |
return $result;
|
|
|
933 |
|
|
|
934 |
}#if end
|
|
|
935 |
|
|
|
936 |
#如果 $conf["type"] 不等於 "http" 與 "https"
|
|
|
937 |
if($conf["type"]!="http" && $conf["type"]!="https"){
|
|
|
938 |
|
|
|
939 |
#設置執行不正常
|
|
|
940 |
$result["status"]="false";
|
|
|
941 |
|
|
|
942 |
#設置執行錯誤訊息
|
|
|
943 |
$result["error"][]="不明的port:".$conf["type"];
|
|
|
944 |
|
|
|
945 |
#回傳結果
|
|
|
946 |
return $result;
|
|
|
947 |
|
|
|
948 |
}#if end
|
|
|
949 |
|
|
|
950 |
#檢查儲存路徑是否存在
|
|
|
951 |
#函式說明:
|
|
|
952 |
#確保路徑存在.
|
|
|
953 |
#回傳的結果:
|
|
|
954 |
#$result["status"],執行正常與否,"true"代表正常,"false"代表不正常.
|
|
|
955 |
#$result["error"],錯誤訊息陣列.
|
|
|
956 |
#$resutl["function"],當前執行的涵式名稱.
|
|
|
957 |
#必填參數:
|
|
|
958 |
#$conf["path"],要檢查的路徑
|
|
|
959 |
$conf["fileAccess::validatePath"]["path"]=$conf["saveLocation"];
|
|
|
960 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
|
|
|
961 |
$conf["fileAccess::validatePath"]["fileArgu"]=$conf["fileArgu"];
|
|
|
962 |
#可省略參數:
|
|
|
963 |
#$conf["haveFileName"],字串,"true"代表有$conf["path"]檔案名稱,"false"代表$conf["path"]為純路徑,預設為"false".
|
|
|
964 |
$conf["fileAccess::validatePath"]["haveFileName"]="true";
|
|
|
965 |
#$conf["dirPermission"],字串,新建資料夾的權限設定,預設爲0770,亦即擁有者,同群組者可以讀,寫,存取,其他人僅能存取.
|
|
|
966 |
#$conf["dirPermission"]="";
|
|
|
967 |
$validatePath=fileAccess::validatePath($conf["fileAccess::validatePath"]);
|
|
|
968 |
unset($conf["fileAccess::validatePath"]);
|
|
|
969 |
|
|
|
970 |
#如果確保路徑失敗
|
|
|
971 |
if($validatePath["status"]=="false"){
|
|
|
972 |
|
|
|
973 |
#設置執行不正常
|
|
|
974 |
$result["status"]="false";
|
|
|
975 |
|
|
|
976 |
#設置執行錯誤訊息
|
|
|
977 |
$result["error"]=$validatePath;
|
|
|
978 |
|
|
|
979 |
#回傳結果
|
|
|
980 |
return $result;
|
|
|
981 |
|
|
|
982 |
}#if end
|
|
|
983 |
|
|
|
984 |
#組合 wget 指令
|
|
|
985 |
$paramsArray=array("--no-check-certificate","-O",$conf["saveLocation"].".html",$conf["type"]."://".$conf["webHostAddressOrDNS"].":".$conf["port"]."/".$conf["webPageLocation"]);
|
| 226 |
liveuser |
986 |
|
| 3 |
liveuser |
987 |
#函式說明:
|
|
|
988 |
#呼叫shell執行系統命令,並取得回傳的內容.
|
|
|
989 |
#回傳結果:
|
|
|
990 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
991 |
#$result["error"],錯誤訊息陣列.
|
|
|
992 |
#$result["function"],當前執行的函數名稱.
|
|
|
993 |
#$result["argu"],使用的參數.
|
|
|
994 |
#$result["cmd"],執行的指令內容.
|
|
|
995 |
#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
996 |
#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
|
|
|
997 |
#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
998 |
#$result["running"],是否還在執行.
|
|
|
999 |
#$result["pid"],pid.
|
|
|
1000 |
#$result["statusCode"],執行結束後的代碼.
|
|
|
1001 |
#$result["escape"],陣列,儲存出新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu").
|
|
|
1002 |
#必填參數:
|
|
|
1003 |
#$conf["command"],字串,要執行的指令.
|
|
|
1004 |
$conf["external.callShell"]["command"]="wget";
|
|
|
1005 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
|
|
1006 |
$conf["external.callShell"]["fileArgu"]=__FILE__;
|
|
|
1007 |
#可省略參數:
|
|
|
1008 |
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
|
|
|
1009 |
$conf["external.callShell"]["argu"]=$paramsArray;
|
|
|
1010 |
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
|
|
|
1011 |
#$conf["arguIsAddr"]=array();
|
|
|
1012 |
#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
|
|
|
1013 |
#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
|
|
|
1014 |
#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
|
|
|
1015 |
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
|
|
|
1016 |
#$conf["enablePrintDescription"]="true";
|
|
|
1017 |
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
|
|
|
1018 |
#$conf["printDescription"]="";
|
|
|
1019 |
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
|
|
|
1020 |
#$conf["escapeshellarg"]="false";
|
|
|
1021 |
#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
|
|
|
1022 |
#$conf["thereIsShellVar"]=array();
|
|
|
1023 |
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
|
|
|
1024 |
#$conf["username"]="";
|
|
|
1025 |
#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
|
|
1026 |
#$conf["password"]="";
|
|
|
1027 |
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
|
|
|
1028 |
#$conf["useScript"]="";
|
|
|
1029 |
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
|
|
|
1030 |
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
|
|
|
1031 |
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
|
|
|
1032 |
#$conf["inBackGround"]="";
|
|
|
1033 |
#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
|
|
|
1034 |
#$conf["getErr"]="false";
|
|
|
1035 |
#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
|
|
|
1036 |
#$conf["doNotRun"]="false";
|
|
|
1037 |
#參考資料:
|
|
|
1038 |
#exec=>http://php.net/manual/en/function.exec.php
|
|
|
1039 |
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
|
|
|
1040 |
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
|
|
|
1041 |
#備註:
|
|
|
1042 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
|
|
|
1043 |
#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
|
|
|
1044 |
$callSell=external::callShell($conf["external.callShell"]);
|
|
|
1045 |
unset($conf["external.callShell"]);
|
|
|
1046 |
|
|
|
1047 |
#如果執行 shell 失敗
|
|
|
1048 |
if($callSell["status"]=="false"){
|
|
|
1049 |
|
|
|
1050 |
#設置執行不正常
|
|
|
1051 |
$result["status"]="false";
|
|
|
1052 |
|
|
|
1053 |
#設置執行錯誤訊息
|
|
|
1054 |
$result["error"]=$callSell;
|
|
|
1055 |
|
|
|
1056 |
#回傳結果
|
|
|
1057 |
return $result;
|
|
|
1058 |
|
|
|
1059 |
}#if end
|
|
|
1060 |
|
|
|
1061 |
#設置取得的網頁檔位置
|
|
|
1062 |
$result["html"]=$conf["saveLocation"].".html";
|
|
|
1063 |
|
|
|
1064 |
#設置執行正常
|
|
|
1065 |
$result["status"]="status";
|
|
|
1066 |
|
|
|
1067 |
#回傳結果
|
|
|
1068 |
return $result;
|
|
|
1069 |
|
|
|
1070 |
}#toHtml function end
|
|
|
1071 |
|
|
|
1072 |
/*
|
|
|
1073 |
#函式說明:
|
|
|
1074 |
#php內建的curl多執行序方法.
|
|
|
1075 |
#回傳結果:
|
|
|
1076 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
1077 |
#$result["error"],錯誤訊息.
|
|
|
1078 |
#$result["function"],當前執行的函數名稱.
|
|
|
1079 |
#$result["content"][$i]["info"],第$i+1個要求的回應資訊.
|
|
|
1080 |
#$result["content"][$i]["res"],第$i+1個要求的回應.
|
|
|
1081 |
#必填參數:
|
|
|
1082 |
#$conf["url"],字串陣列,有哪些網址要拜訪.
|
|
|
1083 |
$conf["url"]=array();
|
|
|
1084 |
#可省略參數:
|
|
|
1085 |
#$conf["opts"],一維字串陣列,要設定的curl連線參數,元素的key為參數的名稱,切記要為long形態(沒有""框住的文字),元素的value為參數的數值內容.
|
|
|
1086 |
#$conf["opts"];
|
|
|
1087 |
#參考資料:
|
|
|
1088 |
#curl_multi_ini(多執行緒執行curl)=>http://php.net/manual/en/function.curl-multi-init.php
|
|
|
1089 |
#$conf["opts"]可以用的參數=>http://php.net/manual/en/function.curl-setopt.php
|
|
|
1090 |
#等待與檢查curl_multi_exec還剩下幾個未執行完畢=>http://php.net/manual/en/function.curl-multi-select.php
|
|
|
1091 |
#備註:
|
|
|
1092 |
#重複執行會執行失敗
|
|
|
1093 |
*/
|
|
|
1094 |
public static function nativeMulti(&$conf){
|
|
|
1095 |
|
|
|
1096 |
#初始化要回傳的結果
|
|
|
1097 |
$result=array();
|
|
|
1098 |
|
|
|
1099 |
#取得當前執行的函數名稱
|
|
|
1100 |
$result["function"]=__FUNCTION__;
|
|
|
1101 |
|
|
|
1102 |
#如果沒有參數
|
|
|
1103 |
if(func_num_args()==0){
|
|
|
1104 |
|
|
|
1105 |
#設置執行失敗
|
|
|
1106 |
$result["status"]="false";
|
|
|
1107 |
|
|
|
1108 |
#設置執行錯誤訊息
|
|
|
1109 |
$result["error"]="函數".$result["function"]."需要參數";
|
|
|
1110 |
|
|
|
1111 |
#回傳結果
|
|
|
1112 |
return $result;
|
|
|
1113 |
|
|
|
1114 |
}#if end
|
|
|
1115 |
|
|
|
1116 |
#取得參數
|
|
|
1117 |
$result["argu"]=$conf;
|
|
|
1118 |
|
|
|
1119 |
#如果 $conf 不為陣列
|
|
|
1120 |
if(gettype($conf)!=="array"){
|
|
|
1121 |
|
|
|
1122 |
#設置執行失敗
|
|
|
1123 |
$result["status"]="false";
|
|
|
1124 |
|
|
|
1125 |
#設置執行錯誤訊息
|
|
|
1126 |
$result["error"][]="\$conf變數須為陣列形態";
|
|
|
1127 |
|
|
|
1128 |
#如果傳入的參數為 null
|
|
|
1129 |
if($conf===null){
|
|
|
1130 |
|
|
|
1131 |
#設置執行錯誤訊息
|
|
|
1132 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
|
|
1133 |
|
|
|
1134 |
}#if end
|
|
|
1135 |
|
|
|
1136 |
#回傳結果
|
|
|
1137 |
return $result;
|
|
|
1138 |
|
|
|
1139 |
}#if end
|
|
|
1140 |
|
|
|
1141 |
#檢查參數
|
|
|
1142 |
#函式說明:
|
|
|
1143 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
1144 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
1145 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
1146 |
#$result["function"],當前執行的函式名稱.
|
|
|
1147 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
1148 |
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
1149 |
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
1150 |
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
1151 |
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
|
|
|
1152 |
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
|
|
|
1153 |
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
|
|
|
1154 |
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
|
|
|
1155 |
#必填寫的參數:
|
|
|
1156 |
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
1157 |
$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
|
|
|
1158 |
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
1159 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("url");
|
|
|
1160 |
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
|
|
|
1161 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("array");
|
|
|
1162 |
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
1163 |
$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
1164 |
#可以省略的參數:
|
|
|
1165 |
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
|
|
|
1166 |
$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
|
|
|
1167 |
#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
|
|
|
1168 |
#$conf["canNotBeEmpty"]=array();
|
|
|
1169 |
#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
|
|
|
1170 |
#$conf["canBeEmpty"]=array();
|
|
|
1171 |
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
|
|
|
1172 |
#$conf["skipableVariableCanNotBeEmpty"]=array();
|
|
|
1173 |
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
|
|
1174 |
$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("opts");
|
|
|
1175 |
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
|
|
1176 |
$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("array");
|
|
|
1177 |
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
|
|
1178 |
$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(null);
|
|
|
1179 |
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
1180 |
#$conf["arrayCountEqualCheck"][]=array();
|
|
|
1181 |
#參考資料來源:
|
|
|
1182 |
#array_keys=>http://php.net/manual/en/function.array-keys.php
|
|
|
1183 |
$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
|
|
|
1184 |
unset($conf["variableCheck::checkArguments"]);
|
|
|
1185 |
|
|
|
1186 |
#如果參數檢查失敗
|
|
|
1187 |
if($checkArguments["status"]==="false"){
|
|
|
1188 |
|
|
|
1189 |
#設置執行不正常
|
|
|
1190 |
$result["status"]="false";
|
|
|
1191 |
|
|
|
1192 |
#設置執行錯誤
|
|
|
1193 |
$result["error"]=$checkArguments;
|
|
|
1194 |
|
|
|
1195 |
#回傳結果
|
|
|
1196 |
return $result;
|
|
|
1197 |
|
|
|
1198 |
}#if end
|
|
|
1199 |
|
|
|
1200 |
#如果參數檢查不通過
|
|
|
1201 |
if($checkArguments["passed"]==="false"){
|
|
|
1202 |
|
|
|
1203 |
#設置執行不正常
|
|
|
1204 |
$result["status"]="false";
|
|
|
1205 |
|
|
|
1206 |
#設置執行錯誤
|
|
|
1207 |
$result["error"]=$checkArguments;
|
|
|
1208 |
|
|
|
1209 |
#回傳結果
|
|
|
1210 |
return $result;
|
|
|
1211 |
|
|
|
1212 |
}#if end
|
|
|
1213 |
|
|
|
1214 |
//create the multiple cURL handle
|
|
|
1215 |
$mh = curl_multi_init();
|
|
|
1216 |
|
|
|
1217 |
#初始化儲存 curl handles 陣列的變數
|
|
|
1218 |
$ch=array();
|
|
|
1219 |
|
|
|
1220 |
#建立多個 cURL resources
|
|
|
1221 |
for($i=0;$i<count($conf["url"]);$i++){
|
|
|
1222 |
|
|
|
1223 |
#儲存 初始化後的 curl handles
|
|
|
1224 |
$ch[$i]=curl_init();
|
|
|
1225 |
|
|
|
1226 |
#設置curl要拜訪的網址
|
|
|
1227 |
curl_setopt($ch[$i], CURLOPT_URL, $conf["url"][$i]);
|
|
|
1228 |
|
|
|
1229 |
#設置要將回應儲存到變數裡面
|
|
|
1230 |
curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, 1);
|
|
|
1231 |
|
|
|
1232 |
#設置連線的timeout
|
|
|
1233 |
curl_setopt($ch[$i], CURLOPT_CONNECTTIMEOUT, 30);
|
|
|
1234 |
|
|
|
1235 |
#允許轉址
|
|
|
1236 |
curl_setopt($ch[$i], CURLOPT_AUTOREFERER, true);
|
|
|
1237 |
|
|
|
1238 |
#設置連線允許的轉址次數
|
|
|
1239 |
curl_setopt($ch[$i], CURLOPT_MAXREDIRS, 5);
|
|
|
1240 |
|
|
|
1241 |
#設置用戶端的資訊
|
|
|
1242 |
curl_setopt($ch[$i], CURLOPT_USERAGENT, "qbpwcf-curl::native");
|
|
|
1243 |
|
|
|
1244 |
#不檢查 ssl 是否可信
|
|
|
1245 |
curl_setopt($ch[$i], CURLOPT_SSL_VERIFYPEER, false);
|
|
|
1246 |
curl_setopt($ch[$i], CURLOPT_SSL_VERIFYSTATUS, false);
|
|
|
1247 |
curl_setopt($ch[$i], CURLOPT_SSL_VERIFYHOST, 0);
|
|
|
1248 |
|
|
|
1249 |
#如果有參數 $conf["opts"]
|
|
|
1250 |
if(isset($conf["opts"])){
|
|
|
1251 |
|
|
|
1252 |
#依據每個參數
|
|
|
1253 |
foreach($conf["opts"] as $optName => $optVal){
|
|
|
1254 |
|
|
|
1255 |
#設置該參數
|
|
|
1256 |
$setopt=curl_setopt($ch[$i], $optName, $optVal);
|
|
|
1257 |
|
|
|
1258 |
#如果參數設定失敗
|
|
|
1259 |
if($setopt===false){
|
|
|
1260 |
|
|
|
1261 |
#設定執行失敗
|
|
|
1262 |
$result["status"]="false";
|
|
|
1263 |
|
|
|
1264 |
#設定執行錯誤訊息
|
|
|
1265 |
$result["error"][]="參數「".$optName."」, 不存在或參數「".$optVal."」有誤。";
|
|
|
1266 |
|
|
|
1267 |
#回傳結果
|
|
|
1268 |
return $result;
|
|
|
1269 |
|
|
|
1270 |
}#if end
|
|
|
1271 |
|
|
|
1272 |
}#foreach end
|
|
|
1273 |
|
|
|
1274 |
}#if end
|
|
|
1275 |
|
|
|
1276 |
#add the two handles
|
|
|
1277 |
curl_multi_add_handle($mh,$ch[$i]);
|
|
|
1278 |
|
|
|
1279 |
}#for end
|
|
|
1280 |
|
|
|
1281 |
#初始化儲存還有幾個連線未執行完畢
|
|
|
1282 |
$active = 0;
|
|
|
1283 |
|
|
|
1284 |
#不斷地檢查是否有執行完畢
|
|
|
1285 |
do{
|
|
|
1286 |
|
|
|
1287 |
#執行每個curl連線
|
|
|
1288 |
$mrc = curl_multi_exec($mh,$active);
|
|
|
1289 |
|
|
|
1290 |
#等一秒,回傳還剩下幾個連線未完成
|
|
|
1291 |
$active=curl_multi_select($mh);
|
|
|
1292 |
|
|
|
1293 |
}#do end
|
|
|
1294 |
|
|
|
1295 |
#如果還有連線未完成
|
|
|
1296 |
while($active>0);
|
|
|
1297 |
|
|
|
1298 |
#有幾個 handles 就執行幾次
|
|
|
1299 |
for($i=0;$i<count($conf["url"]);$i++){
|
|
|
1300 |
|
|
|
1301 |
#取得回應
|
|
|
1302 |
$res=curl_multi_getcontent($ch[$i]);
|
|
|
1303 |
|
|
|
1304 |
#取得每個 curl 要求的結果資訊陣列
|
|
|
1305 |
$info=curl_getinfo($ch[$i]);
|
|
|
1306 |
|
|
|
1307 |
#儲存資訊與回應
|
|
|
1308 |
$result["content"][]=array("info"=>$info,"res"=>$res);
|
|
|
1309 |
|
|
|
1310 |
#關閉 handles
|
|
|
1311 |
curl_multi_remove_handle($mh, $ch[$i]);
|
|
|
1312 |
|
|
|
1313 |
}#for end
|
|
|
1314 |
|
|
|
1315 |
#關閉 multi handles
|
|
|
1316 |
curl_multi_close($mh);
|
|
|
1317 |
|
|
|
1318 |
#設置執行正常
|
|
|
1319 |
$result["status"]="true";
|
|
|
1320 |
|
|
|
1321 |
#回傳結果
|
|
|
1322 |
return $result;
|
|
|
1323 |
|
|
|
1324 |
}#function nativeMulti end
|
|
|
1325 |
|
|
|
1326 |
/*
|
|
|
1327 |
#函式說明:
|
|
|
1328 |
#運行curl cmd
|
|
|
1329 |
#回傳結果:
|
|
|
1330 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
1331 |
#$result["error"],錯誤訊息陣列.
|
|
|
1332 |
#$result["function"],當前執行的函式名稱.
|
|
|
1333 |
#$result["founded"],識別網址找不找得到.
|
|
|
1334 |
#$result["content"],取得的回應內容陣列;若$conf["bgInProc"]為"true",則每個元素為其指令執行的結果訊息陣列,key為"status"代表執行是否正常的識別;key為"statusCode"代表程式結束後回傳給對應executeBy程式的數值;key為"output"代表標準輸出,若為resource,則代表為pipe;key為"error"代表非標準輸出,若為resource,則代表為pipe;key為"input"代表成功輸入的指令;key為"process"代表該程序經proc_open後的process source;key為"proc_get_status"代表程序的資訊.
|
|
|
1335 |
#$result["fullContent"],取得回應的完整字串內容.
|
|
|
1336 |
#$result["cookie"],cookie檔案的位置與名稱.
|
|
|
1337 |
#$result["cmd"],執行的command.
|
|
|
1338 |
#$result["argu],使用的參數.
|
|
|
1339 |
#必填參數:
|
|
|
1340 |
#$conf["url"],字串,目標url.
|
|
|
1341 |
$conf["url"]="";
|
|
|
1342 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
|
|
1343 |
$conf["fileArgu"]=__FILE__;
|
|
|
1344 |
#可省略參數:
|
|
|
1345 |
#$conf["header"],字串陣列,要傳送的header.
|
|
|
1346 |
#$conf["header"]=array();
|
|
|
1347 |
#$conf["allowAnySSLcertificate"],字串,是否允許不可信任的SSL憑證,預設為"true".
|
|
|
1348 |
#$conf["allowAnySSLcertificate"]="";
|
|
|
1349 |
#$conf["postVar"],字串陣列,每個要傳送的post變數名稱(陣列的key值)與數值.
|
|
|
1350 |
#$conf["postVar"]=array();
|
|
|
1351 |
#$conf["rawPost"]="字串",要傳送的raw post內容.
|
|
|
1352 |
#$conf["rawPost"]="";
|
|
|
1353 |
#$conf["urlEncode"],字串,post的內容是否要url_encode,"true"代表要,預設為"false"代表不要.
|
|
|
1354 |
#$conf["urlEncode"]="false";
|
|
|
1355 |
#$conf["agent"],字串,user agent的名稱.
|
|
|
1356 |
#$conf["agent"]="";
|
|
|
1357 |
#$conf["cookie"],字串,cookie位置與檔案位置.
|
|
|
1358 |
#$conf["cookie"]="";
|
|
|
1359 |
#$conf["forceNewCookie"],字串,是否要重置cookie,"true"代表要,"false"代表不要,預設為"false".
|
|
|
1360 |
#$conf["forceNewCookie"]="";
|
|
|
1361 |
#$conf["inBg"],字串,"true"代表要在背景中執行;反之則為"false".
|
|
|
1362 |
#$conf["inBg"]="true";
|
|
|
1363 |
#$conf["bgInPorc"],字串,若跟inBg一樣為"true",則會透過proc放在背景執行且回傳proc資訊,以便監控執行狀況.預設為"false",不使用proc.
|
|
|
1364 |
#$conf["bgInProc"]="false";
|
|
|
1365 |
#$conf["followRedirection"],字串,是否允許轉址,預設要為"true";反之為"false".
|
|
|
1366 |
#$conf["followRedirection"]="true";
|
|
|
1367 |
#參考資料:
|
|
|
1368 |
#無.
|
|
|
1369 |
#備註:
|
|
|
1370 |
#無.
|
|
|
1371 |
*/
|
|
|
1372 |
public static function curlCmd(&$conf=array()){
|
|
|
1373 |
|
|
|
1374 |
#初始化要回傳的結果
|
|
|
1375 |
$result=array();
|
|
|
1376 |
|
|
|
1377 |
#取得當前執行的函數名稱
|
|
|
1378 |
$result["function"]=__FUNCTION__;
|
|
|
1379 |
|
|
|
1380 |
#如果沒有參數
|
|
|
1381 |
if(func_num_args()==0){
|
|
|
1382 |
|
|
|
1383 |
#設置執行失敗
|
|
|
1384 |
$result["status"]="false";
|
|
|
1385 |
|
|
|
1386 |
#設置執行錯誤訊息
|
|
|
1387 |
$result["error"]="函數".$result["function"]."需要參數";
|
|
|
1388 |
|
|
|
1389 |
#回傳結果
|
|
|
1390 |
return $result;
|
|
|
1391 |
|
|
|
1392 |
}#if end
|
|
|
1393 |
|
|
|
1394 |
#取得參數
|
|
|
1395 |
$result["argu"]=$conf;
|
|
|
1396 |
|
|
|
1397 |
#如果 $conf 不為陣列
|
|
|
1398 |
if(gettype($conf)!=="array"){
|
|
|
1399 |
|
|
|
1400 |
#設置執行失敗
|
|
|
1401 |
$result["status"]="false";
|
|
|
1402 |
|
|
|
1403 |
#設置執行錯誤訊息
|
|
|
1404 |
$result["error"][]="\$conf變數須為陣列形態";
|
|
|
1405 |
|
|
|
1406 |
#如果傳入的參數為 null
|
|
|
1407 |
if($conf===null){
|
|
|
1408 |
|
|
|
1409 |
#設置執行錯誤訊息
|
|
|
1410 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
|
|
1411 |
|
|
|
1412 |
}#if end
|
|
|
1413 |
|
|
|
1414 |
#回傳結果
|
|
|
1415 |
return $result;
|
|
|
1416 |
|
|
|
1417 |
}#if end
|
|
|
1418 |
|
|
|
1419 |
#檢查參數
|
|
|
1420 |
#函式說明:
|
|
|
1421 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
1422 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
1423 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
1424 |
#$result["function"],當前執行的函式名稱.
|
|
|
1425 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
1426 |
#$result[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
1427 |
#$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
1428 |
#$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
1429 |
#必填寫的參數:
|
|
|
1430 |
#$conf["variableCheck::checkArguments"]["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
1431 |
$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
|
|
|
1432 |
#$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
1433 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("url","fileArgu");
|
|
|
1434 |
#$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double");
|
|
|
1435 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string");
|
|
|
1436 |
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
1437 |
$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
1438 |
#可以省略的參數:
|
|
|
1439 |
#$conf["variableCheck::checkArguments"]["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true"。
|
|
|
1440 |
$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
|
|
|
1441 |
#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
|
|
|
1442 |
#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("followRedirection");
|
|
|
1443 |
#$conf["variableCheck::checkArguments"]["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
| 291 |
liveuser |
1444 |
$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("urlEncodeOnly","header","allowAnySSLcertificate","postVar","urlEncode","agent","cookie","forceNewCookie","rawPost","inBg","bgInProc","followRedirection");
|
| 3 |
liveuser |
1445 |
#$conf["variableCheck::checkArguments"]["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
| 291 |
liveuser |
1446 |
$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","array","string","array","string","string","string","string","string","string","string","string");
|
| 3 |
liveuser |
1447 |
#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,"null"代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
| 291 |
liveuser |
1448 |
$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("false",array(),null,array(),"false",null,null,"false",null,"false","false","true");
|
| 3 |
liveuser |
1449 |
#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
1450 |
#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"][]=array();
|
|
|
1451 |
$checkResult=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
|
|
|
1452 |
unset($conf["variableCheck::checkArguments"]);
|
|
|
1453 |
|
|
|
1454 |
#如果檢查參數失敗
|
| 291 |
liveuser |
1455 |
if($checkResult["status"]==="false"){
|
| 3 |
liveuser |
1456 |
|
|
|
1457 |
#設置執行失敗
|
|
|
1458 |
$result["status"]="false";
|
|
|
1459 |
|
|
|
1460 |
#設置執行錯誤
|
|
|
1461 |
$result["error"]=$checkResult;
|
|
|
1462 |
|
|
|
1463 |
#回傳結果
|
|
|
1464 |
return $result;
|
|
|
1465 |
|
|
|
1466 |
}#if end
|
|
|
1467 |
|
|
|
1468 |
#如果檢查參數不通過
|
| 291 |
liveuser |
1469 |
if($checkResult["passed"]==="false"){
|
| 3 |
liveuser |
1470 |
|
|
|
1471 |
#設置執行失敗
|
|
|
1472 |
$result["status"]="false";
|
|
|
1473 |
|
|
|
1474 |
#設置執行錯誤
|
|
|
1475 |
$result["error"]=$checkResult;
|
|
|
1476 |
|
|
|
1477 |
#回傳結果
|
|
|
1478 |
return $result;
|
|
|
1479 |
|
|
|
1480 |
}#if end
|
|
|
1481 |
|
|
|
1482 |
#要使用的參數
|
|
|
1483 |
$arguments=array();
|
|
|
1484 |
|
|
|
1485 |
#是否不要用「"」保住參數
|
|
|
1486 |
$plainArgu=array();
|
|
|
1487 |
|
|
|
1488 |
#改用「'」包住參數
|
|
|
1489 |
$useApostrophe=array();
|
|
|
1490 |
|
|
|
1491 |
#判斷 $conf["url"] 為 http 還是 https
|
|
|
1492 |
#函式說明:
|
|
|
1493 |
#取得符合特定字首與字尾的字串
|
|
|
1494 |
#回傳的結果:
|
|
|
1495 |
#$result["status"],若爲"true"則代表執行正常;若爲"false"則代表執行失敗。
|
|
|
1496 |
#$result["function"],當前執行的函數名稱.
|
|
|
1497 |
#$result["error"],錯誤訊息陣列.
|
|
|
1498 |
#$result["founded"],若為"true"則代表有找到符合字首條件的結果;若爲"false"則代表沒有找到。
|
|
|
1499 |
#$result["returnString"],爲符合字首條件的字串內容。
|
|
|
1500 |
#必填參數:
|
|
|
1501 |
#$conf["checkString"],字串,要檢查的字串.
|
|
|
1502 |
$conf["search::getMeetConditionsString"]["checkString"]=$conf["url"];
|
|
|
1503 |
#可省略參數:
|
|
|
1504 |
#$conf["frontWord"],字串,用來檢查字首應該要有什麼字串,預設不指定.
|
|
|
1505 |
$conf["search::getMeetConditionsString"]["frontWord"]="https://";
|
|
|
1506 |
#$conf["tailWord"],字串,用來檢查字尾應該要有什麼字串,預設不指定.
|
|
|
1507 |
#$conf["search::getMeetConditionsString"]["tailWord"]="";
|
|
|
1508 |
#參考資料:
|
|
|
1509 |
#str_spilt(),可以將字串依照字母分割成一個個陣列字串。
|
|
|
1510 |
$getMeetConditionsString=search::getMeetConditionsString($conf["search::getMeetConditionsString"]);
|
|
|
1511 |
unset($conf["search::getMeetConditionsString"]);
|
|
|
1512 |
|
|
|
1513 |
#如果尋找關鍵字失敗
|
|
|
1514 |
if($getMeetConditionsString["status"]==="false"){
|
|
|
1515 |
|
|
|
1516 |
#設置執行失敗
|
|
|
1517 |
$result["status"]="false";
|
|
|
1518 |
|
|
|
1519 |
#設置執行錯誤
|
|
|
1520 |
$result["error"]=$getMeetConditionsString;
|
|
|
1521 |
|
|
|
1522 |
#回傳結果
|
|
|
1523 |
return $result;
|
|
|
1524 |
|
|
|
1525 |
}#if end
|
|
|
1526 |
|
|
|
1527 |
#初始化沒有使用ssl
|
|
|
1528 |
$enableSSL="false";
|
|
|
1529 |
|
|
|
1530 |
#如果有 https:// 關鍵字
|
|
|
1531 |
if($getMeetConditionsString["founded"]==="true"){
|
|
|
1532 |
|
|
|
1533 |
#如果未設定 $conf["allowAnySSLcertificate"]
|
|
|
1534 |
if(!isset($conf["allowAnySSLcertificate"])){
|
|
|
1535 |
|
|
|
1536 |
#使用ssl
|
|
|
1537 |
$enableSSL="true";
|
|
|
1538 |
|
|
|
1539 |
#預設為接受不可信任的ssl憑證
|
|
|
1540 |
$conf["allowAnySSLcertificate"]="true";
|
|
|
1541 |
|
|
|
1542 |
}#if end
|
|
|
1543 |
|
|
|
1544 |
}#if end
|
|
|
1545 |
|
|
|
1546 |
#如果有使用ssl
|
|
|
1547 |
if($enableSSL==="true"){
|
|
|
1548 |
|
|
|
1549 |
#設置 --ssl-reqd 參數,代表強制為https
|
|
|
1550 |
$arguments[]="--ssl-reqd";
|
|
|
1551 |
|
|
|
1552 |
#要包住參數
|
|
|
1553 |
$plainArgu[]="false";
|
| 226 |
liveuser |
1554 |
|
| 3 |
liveuser |
1555 |
#不要改用「'」包住參數
|
|
|
1556 |
$useApostrophe[]="false";
|
| 226 |
liveuser |
1557 |
|
| 3 |
liveuser |
1558 |
#如果允許不被信任的憑證
|
|
|
1559 |
if($conf["allowAnySSLcertificate"]==="true"){
|
|
|
1560 |
|
|
|
1561 |
#設置 -k 參數
|
|
|
1562 |
$arguments[]="-k";
|
|
|
1563 |
|
|
|
1564 |
#要包住參數
|
|
|
1565 |
$plainArgu[]="false";
|
| 226 |
liveuser |
1566 |
|
| 3 |
liveuser |
1567 |
#不要改用「'」包住參數
|
|
|
1568 |
$useApostrophe[]="false";
|
|
|
1569 |
|
|
|
1570 |
}#if end
|
|
|
1571 |
|
|
|
1572 |
}#if end
|
|
|
1573 |
|
|
|
1574 |
#針對每個 $conf["header"] 元素
|
|
|
1575 |
foreach($conf["header"] as $header){
|
|
|
1576 |
|
|
|
1577 |
#設置post參數
|
|
|
1578 |
$arguments[]="-H";
|
|
|
1579 |
|
|
|
1580 |
#要包住參數
|
|
|
1581 |
$plainArgu[]="false";
|
|
|
1582 |
|
|
|
1583 |
#不要改用「'」包住參數
|
|
|
1584 |
$useApostrophe[]="false";
|
|
|
1585 |
|
|
|
1586 |
#設置post變數
|
|
|
1587 |
$arguments[]=$header;
|
|
|
1588 |
|
|
|
1589 |
#要包住參數
|
|
|
1590 |
$plainArgu[]="false";
|
| 226 |
liveuser |
1591 |
|
| 3 |
liveuser |
1592 |
#不要改用「'」包住參數
|
|
|
1593 |
$useApostrophe[]="false";
|
|
|
1594 |
|
|
|
1595 |
}#foreach end
|
|
|
1596 |
|
|
|
1597 |
#針對每個 $conf["postVar"] 元素
|
|
|
1598 |
foreach($conf["postVar"] as $name=>$value){
|
|
|
1599 |
|
|
|
1600 |
#如果沒有要」 url encode
|
|
|
1601 |
if($conf["urlEncode"]==="false"){
|
|
|
1602 |
|
|
|
1603 |
#設置post參數
|
|
|
1604 |
$arguments[]="-d";
|
|
|
1605 |
|
|
|
1606 |
#要包住參數
|
|
|
1607 |
$plainArgu[]="false";
|
|
|
1608 |
|
|
|
1609 |
#不要改用「'」包住參數
|
|
|
1610 |
$useApostrophe[]="false";
|
|
|
1611 |
|
|
|
1612 |
}#if end
|
|
|
1613 |
|
|
|
1614 |
#反之要 url encode
|
|
|
1615 |
else{
|
|
|
1616 |
|
|
|
1617 |
#設置post參數
|
|
|
1618 |
$arguments[]="--data-urlencode";
|
|
|
1619 |
|
|
|
1620 |
#要包住參數
|
|
|
1621 |
$plainArgu[]="false";
|
|
|
1622 |
|
|
|
1623 |
#不要改用「'」包住參數
|
|
|
1624 |
$useApostrophe[]="false";
|
|
|
1625 |
|
|
|
1626 |
}#else end
|
|
|
1627 |
|
|
|
1628 |
#設置post變數
|
|
|
1629 |
$arguments[]=$name."=".$value;
|
|
|
1630 |
|
|
|
1631 |
#要包住參數
|
|
|
1632 |
$plainArgu[]="false";
|
| 226 |
liveuser |
1633 |
|
| 3 |
liveuser |
1634 |
#不要改用「'」包住參數
|
|
|
1635 |
$useApostrophe[]="false";
|
|
|
1636 |
|
|
|
1637 |
}#foreach end
|
| 226 |
liveuser |
1638 |
|
| 3 |
liveuser |
1639 |
#如果有設置 $conf["rawPost"]
|
|
|
1640 |
if(isset($conf["rawPost"])){
|
| 226 |
liveuser |
1641 |
|
| 3 |
liveuser |
1642 |
#如果沒有要url encode
|
|
|
1643 |
if($conf["urlEncode"]==="false"){
|
|
|
1644 |
|
|
|
1645 |
#設置post參數
|
|
|
1646 |
$arguments[]="-d";
|
| 226 |
liveuser |
1647 |
|
| 3 |
liveuser |
1648 |
#要包住參數
|
|
|
1649 |
$plainArgu[]="false";
|
| 226 |
liveuser |
1650 |
|
| 3 |
liveuser |
1651 |
#不要改用「'」包住參數
|
|
|
1652 |
$useApostrophe[]="false";
|
| 226 |
liveuser |
1653 |
|
| 3 |
liveuser |
1654 |
#數值post vlaue
|
|
|
1655 |
$arguments[]=$conf["rawPost"];
|
|
|
1656 |
|
|
|
1657 |
#要包住參數
|
|
|
1658 |
$plainArgu[]="false";
|
| 226 |
liveuser |
1659 |
|
| 3 |
liveuser |
1660 |
#改用「'」包住參數
|
|
|
1661 |
$useApostrophe[]="true";
|
|
|
1662 |
|
|
|
1663 |
}#if end
|
|
|
1664 |
|
|
|
1665 |
#反之要 url encode
|
|
|
1666 |
else{
|
|
|
1667 |
|
|
|
1668 |
#設置post參數
|
|
|
1669 |
$arguments[]="--data-urlencode";
|
| 226 |
liveuser |
1670 |
|
| 3 |
liveuser |
1671 |
#要包住參數
|
|
|
1672 |
$plainArgu[]="false";
|
| 226 |
liveuser |
1673 |
|
| 3 |
liveuser |
1674 |
#不要改用「'」包住參數
|
|
|
1675 |
$useApostrophe[]="false";
|
| 226 |
liveuser |
1676 |
|
| 3 |
liveuser |
1677 |
#數值post vlaue
|
|
|
1678 |
$arguments[]=$conf["rawPost"];
|
|
|
1679 |
|
|
|
1680 |
#要包住參數
|
|
|
1681 |
$plainArgu[]="false";
|
| 226 |
liveuser |
1682 |
|
| 3 |
liveuser |
1683 |
#改用「'」包住參數
|
|
|
1684 |
$useApostrophe[]="true";
|
|
|
1685 |
|
|
|
1686 |
}#else end
|
| 226 |
liveuser |
1687 |
|
| 3 |
liveuser |
1688 |
}#if end
|
|
|
1689 |
|
|
|
1690 |
#如果有設置 agent
|
|
|
1691 |
if(isset($conf["agent"])){
|
|
|
1692 |
|
|
|
1693 |
#使用agent參數
|
|
|
1694 |
$arguments[]="-A";
|
|
|
1695 |
|
|
|
1696 |
#要包住參數
|
|
|
1697 |
$plainArgu[]="false";
|
|
|
1698 |
|
|
|
1699 |
#不要改用「'」包住參數
|
|
|
1700 |
$useApostrophe[]="false";
|
|
|
1701 |
|
|
|
1702 |
#設置agent參數值
|
|
|
1703 |
$arguments[]=$conf["agent"];
|
|
|
1704 |
|
|
|
1705 |
#要包住參數
|
|
|
1706 |
$plainArgu[]="false";
|
| 226 |
liveuser |
1707 |
|
| 3 |
liveuser |
1708 |
#不要改用「'」包住參數
|
|
|
1709 |
$useApostrophe[]="false";
|
| 226 |
liveuser |
1710 |
|
| 3 |
liveuser |
1711 |
}#if end
|
| 226 |
liveuser |
1712 |
|
| 3 |
liveuser |
1713 |
#遇到大於 400 的 http 回應要回傳異常
|
|
|
1714 |
$arguments[]="--fail";
|
|
|
1715 |
|
|
|
1716 |
#要求的網址用 " " 包起來
|
|
|
1717 |
$arguments[]=$conf["url"];
|
|
|
1718 |
|
|
|
1719 |
#要包住參數
|
|
|
1720 |
$plainArgu[]="false";
|
|
|
1721 |
|
|
|
1722 |
#不要改用「'」包住參數
|
|
|
1723 |
$useApostrophe[]="false";
|
|
|
1724 |
|
|
|
1725 |
#如果有設置 cookie 參數
|
|
|
1726 |
if(isset($conf["cookie"])){
|
|
|
1727 |
|
|
|
1728 |
#如果 cookie 檔案存在
|
|
|
1729 |
if(file_exists($conf["cookie"])){
|
|
|
1730 |
|
|
|
1731 |
#如果沒有要重置cookie
|
|
|
1732 |
if($conf["forceNewCookie"]==="false"){
|
|
|
1733 |
|
|
|
1734 |
#設置使用既有的cookie
|
|
|
1735 |
$arguments[]="-b ".$conf["cookie"];
|
|
|
1736 |
|
|
|
1737 |
#不要包住參數
|
|
|
1738 |
$plainArgu[]="true";
|
|
|
1739 |
|
|
|
1740 |
#不要改用「'」包住參數
|
|
|
1741 |
$useApostrophe[]="false";
|
|
|
1742 |
|
|
|
1743 |
}#if end
|
| 226 |
liveuser |
1744 |
|
| 3 |
liveuser |
1745 |
# 反之使用新的cookie
|
|
|
1746 |
else{
|
|
|
1747 |
|
|
|
1748 |
$arguments[]="-c ".$conf["cookie"];
|
|
|
1749 |
|
|
|
1750 |
#不要包住參數
|
|
|
1751 |
$plainArgu[]="true";
|
|
|
1752 |
|
|
|
1753 |
#不要改用「'」包住參數
|
|
|
1754 |
$useApostrophe[]="false";
|
|
|
1755 |
|
|
|
1756 |
}#else end
|
|
|
1757 |
|
|
|
1758 |
}#if end
|
|
|
1759 |
|
|
|
1760 |
#反之不存在 cookie 檔案
|
|
|
1761 |
else{
|
|
|
1762 |
|
|
|
1763 |
#設置使用新的cookie
|
|
|
1764 |
$arguments[]="-c ".$conf["cookie"];
|
| 226 |
liveuser |
1765 |
|
| 3 |
liveuser |
1766 |
#不要包住參數
|
|
|
1767 |
$plainArgu[]="true";
|
|
|
1768 |
|
|
|
1769 |
#不要改用「'」包住參數
|
|
|
1770 |
$useApostrophe[]="false";
|
|
|
1771 |
|
|
|
1772 |
}#els end
|
|
|
1773 |
|
|
|
1774 |
}#if end
|
|
|
1775 |
|
|
|
1776 |
#如果要允許轉址
|
|
|
1777 |
if($conf["followRedirection"]==="true"){
|
| 226 |
liveuser |
1778 |
|
| 3 |
liveuser |
1779 |
#follow redirection - start
|
|
|
1780 |
$arguments[]="-L";
|
| 226 |
liveuser |
1781 |
|
| 3 |
liveuser |
1782 |
#不要包住參數
|
|
|
1783 |
$plainArgu[]="true";
|
|
|
1784 |
|
|
|
1785 |
#不要改用「'」包住參數
|
|
|
1786 |
$useApostrophe[]="false";
|
| 226 |
liveuser |
1787 |
|
| 3 |
liveuser |
1788 |
}#if end
|
|
|
1789 |
|
|
|
1790 |
#要執行的command
|
|
|
1791 |
$cmd="curl";
|
|
|
1792 |
|
|
|
1793 |
#如果要透過 proc 放在背景中運行
|
|
|
1794 |
if( $conf["inBg"]==="true" && $conf["bgInProc"]==="true" ){
|
| 226 |
liveuser |
1795 |
|
| 3 |
liveuser |
1796 |
#涵式說明:
|
|
|
1797 |
#呼叫shell執行系統命令,並取得回傳的內容.
|
|
|
1798 |
#回傳的結果:
|
|
|
1799 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
1800 |
#$result["error"],錯誤訊息陣列.
|
|
|
1801 |
#$result["function"],當前執行的函數名稱.
|
|
|
1802 |
#$result["argu"],使用的參數.
|
|
|
1803 |
#$result["cmd"],執行的指令內容.
|
|
|
1804 |
#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
1805 |
#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
|
|
|
1806 |
#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
1807 |
#$result["running"],是否還在執行.
|
|
|
1808 |
#$result["pid"],pid.
|
|
|
1809 |
#$result["statusCode"],執行結束後的代碼.
|
|
|
1810 |
#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
|
|
|
1811 |
#必填的參數
|
|
|
1812 |
#$conf["command"],字串,要執行的指令與.
|
|
|
1813 |
$conf["external::callShell"]["command"]=$cmd;
|
|
|
1814 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
|
|
1815 |
$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];
|
|
|
1816 |
#可省略參數:
|
|
|
1817 |
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
|
|
|
1818 |
$conf["external::callShell"]["argu"]=$arguments;
|
|
|
1819 |
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
|
|
|
1820 |
#$conf["arguIsAddr"]=array();
|
|
|
1821 |
#$conf["plainArgu"],字串陣列,哪幾個參數不要加上"",元算若為"true"則代表不用包;反之"false"則代表要包.
|
|
|
1822 |
#$conf["external::callShell"]["plainArgu"]=$plainArgu;
|
|
|
1823 |
#$conf["useApostrophe"],字串陣列,如果有需要包住,則用「'」,而非「"」處理.前者為"true";後者為"false".
|
|
|
1824 |
#$conf["external::callShell"]["useApostrophe"]=$useApostrophe;
|
|
|
1825 |
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
|
|
|
1826 |
#$conf["external::callShell"]["enablePrintDescription"]="true";
|
|
|
1827 |
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容.
|
|
|
1828 |
#$conf["printDescription"]="";
|
|
|
1829 |
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".
|
|
|
1830 |
$conf["external::callShell"]["escapeshellarg"]="true";
|
|
|
1831 |
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
|
|
|
1832 |
#$conf["username"]="";
|
|
|
1833 |
#$conf["password"],字串,與$conf["username"]搭配的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
|
|
1834 |
#$conf["password"]="";
|
|
|
1835 |
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
|
|
|
1836 |
#$conf["useScript"]="";
|
|
|
1837 |
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
|
|
|
1838 |
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
|
|
|
1839 |
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
|
|
|
1840 |
#$conf["external::callShell"]["inBackGround"]=$conf["inBg"];
|
|
|
1841 |
#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
|
|
|
1842 |
#$conf["getErr"]="false";
|
|
|
1843 |
#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
|
|
|
1844 |
$conf["external::callShell"]["doNotRun"]="true";
|
|
|
1845 |
#備註:
|
|
|
1846 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
|
|
|
1847 |
#參考資料:
|
|
|
1848 |
#exec=>http://php.net/manual/en/function.exec.php
|
|
|
1849 |
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
|
|
|
1850 |
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
|
|
|
1851 |
$callShell=external::callShell($conf["external::callShell"]);
|
|
|
1852 |
unset($conf["external::callShell"]);
|
| 226 |
liveuser |
1853 |
|
| 3 |
liveuser |
1854 |
#如果尋找關鍵字失敗
|
|
|
1855 |
if($callShell["status"]==="false"){
|
|
|
1856 |
|
|
|
1857 |
#設置執行失敗
|
|
|
1858 |
$result["status"]="false";
|
|
|
1859 |
|
|
|
1860 |
#設置執行錯誤
|
|
|
1861 |
$result["error"]=$callShell;
|
|
|
1862 |
|
|
|
1863 |
#回傳結果
|
|
|
1864 |
return $result;
|
|
|
1865 |
|
|
|
1866 |
}#if end
|
| 226 |
liveuser |
1867 |
|
| 3 |
liveuser |
1868 |
#初始化要透過proc執行的指令
|
|
|
1869 |
$cmdAndArguForThreadsProc=$callShell["escape"]["array"];
|
| 226 |
liveuser |
1870 |
|
| 3 |
liveuser |
1871 |
#函式說明:
|
|
|
1872 |
#透過proc來多執行序運作.
|
|
|
1873 |
#回傳結果:
|
|
|
1874 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
1875 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
1876 |
#$result["function"],當前執行的函式名稱.
|
|
|
1877 |
#$result["argu"],使用的參數.
|
|
|
1878 |
#$result["content"],陣列,每個元素為其指令執行的結果訊息陣列,key為"status"代表執行是否正常的識別;key為"statusCode"代表程式結束後回傳給對應executeBy程式的數值;key為"output"代表標準輸出,若為resource,則代表為pipe;key為"error"代表非標準輸出,若為resource,則代表為pipe;key為"input"代表成功輸入的指令;key為"process"代表該程序經proc_open後的process source;key為"proc_get_status"代表程序的資訊.
|
|
|
1879 |
#必填參數:
|
|
|
1880 |
#$conf["cmds"],字串陣列,每個元素代表要執行的指令與參數.
|
|
|
1881 |
$conf["threads::proc"]["cmds"]=array($cmdAndArguForThreadsProc);
|
|
|
1882 |
#可省略參數:
|
|
|
1883 |
#$conf["wait"],字串,是否需要等待所有程序結束,預設為"true"要等待;反之為"false"不要等待.
|
|
|
1884 |
$conf["threads::proc"]["wait"]="true";
|
|
|
1885 |
#$conf["workingDir"],字串陣列,個別程式執行時的家目錄,預設不指定.
|
|
|
1886 |
#$conf["workingDir"]=array("path");
|
|
|
1887 |
#$conf["envs"],2維字串陣列,每個元素代表個別程式執行時的指定環境變數,key變數名稱;value為變數內容.預設為array("QBPWCF" => "Quick Build PHP Website Componment base on Fedora Linux");
|
|
|
1888 |
#$conf["envs"]=array(array("key"=>"value"));
|
|
|
1889 |
#$conf["executeBy"],字串陣列,每個元素代表個別指令要用什麼程式執行,預設為"bash".
|
|
|
1890 |
#$conf["executeBy"]=array("bash");
|
|
|
1891 |
#參考資料:
|
|
|
1892 |
#https://www.php.net/manual/en/function.proc-open.php
|
|
|
1893 |
#https://www.php.net/manual/en/function.proc-get-status.php
|
|
|
1894 |
#備註:
|
|
|
1895 |
#無.
|
|
|
1896 |
$proc=threads::proc($conf["threads::proc"]);
|
|
|
1897 |
unset($conf["threads::proc"]);
|
| 226 |
liveuser |
1898 |
|
| 3 |
liveuser |
1899 |
#如果尋找關鍵字失敗
|
|
|
1900 |
if($proc["status"]==="false"){
|
|
|
1901 |
|
|
|
1902 |
#設置執行失敗
|
|
|
1903 |
$result["status"]="false";
|
|
|
1904 |
|
|
|
1905 |
#設置執行錯誤
|
|
|
1906 |
$result["error"]=$proc;
|
|
|
1907 |
|
|
|
1908 |
#回傳結果
|
|
|
1909 |
return $result;
|
|
|
1910 |
|
|
|
1911 |
}#if end
|
| 226 |
liveuser |
1912 |
|
| 3 |
liveuser |
1913 |
#取得 proc 資訊
|
|
|
1914 |
$result["content"]=$proc["content"];
|
| 226 |
liveuser |
1915 |
|
| 3 |
liveuser |
1916 |
#設置執行正常
|
|
|
1917 |
$result["status"]="true";
|
| 226 |
liveuser |
1918 |
|
| 3 |
liveuser |
1919 |
#回傳結果
|
|
|
1920 |
return $result;
|
| 226 |
liveuser |
1921 |
|
| 3 |
liveuser |
1922 |
}#if end
|
|
|
1923 |
|
|
|
1924 |
#涵式說明:
|
|
|
1925 |
#呼叫shell執行系統命令,並取得回傳的內容.
|
|
|
1926 |
#回傳的結果:
|
|
|
1927 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
1928 |
#$result["error"],錯誤訊息陣列.
|
|
|
1929 |
#$result["function"],當前執行的函數名稱.
|
|
|
1930 |
#$result["cmd"],執行的指令內容.
|
|
|
1931 |
#$result["output"],爲執行完二元碼後的輸出陣列.
|
|
|
1932 |
#$result["pid"],pid
|
|
|
1933 |
#$result["statusCode"],執行結束後的代碼.
|
|
|
1934 |
#必填的參數
|
|
|
1935 |
#$conf["command"],字串,要執行的指令與.
|
|
|
1936 |
$conf["external::callShell"]["command"]=$cmd;
|
|
|
1937 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
|
|
1938 |
$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];
|
|
|
1939 |
#可省略參數:
|
|
|
1940 |
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
|
|
|
1941 |
$conf["external::callShell"]["argu"]=$arguments;
|
|
|
1942 |
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
|
|
|
1943 |
#$conf["arguIsAddr"]=array();
|
|
|
1944 |
#$conf["plainArgu"],字串陣列,哪幾個參數不要加上"",元算若為"true"則代表不用包;反之"false"則代表要包.
|
|
|
1945 |
#$conf["external::callShell"]["plainArgu"]=$plainArgu;
|
|
|
1946 |
#$conf["useApostrophe"],字串陣列,如果有需要包住,則用「'」,而非「"」處理.前者為"true";後者為"false".
|
|
|
1947 |
#$conf["external::callShell"]["useApostrophe"]=$useApostrophe;
|
|
|
1948 |
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
|
|
|
1949 |
#$conf["external::callShell"]["enablePrintDescription"]="true";
|
|
|
1950 |
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容.
|
|
|
1951 |
#$conf["printDescription"]="";
|
|
|
1952 |
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".
|
|
|
1953 |
$conf["external::callShell"]["escapeshellarg"]="true";
|
|
|
1954 |
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
|
|
|
1955 |
#$conf["username"]="";
|
|
|
1956 |
#$conf["password"],字串,與$conf["username"]搭配的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
|
|
1957 |
#$conf["password"]="";
|
|
|
1958 |
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
|
|
|
1959 |
#$conf["useScript"]="";
|
|
|
1960 |
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
|
|
|
1961 |
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
|
|
|
1962 |
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
|
|
|
1963 |
$conf["external::callShell"]["inBackGround"]=$conf["inBg"];
|
|
|
1964 |
#備註:
|
|
|
1965 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
|
|
|
1966 |
#參考資料:
|
|
|
1967 |
#exec=>http://php.net/manual/en/function.exec.php
|
|
|
1968 |
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
|
|
|
1969 |
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
|
|
|
1970 |
$callShell=external::callShell($conf["external::callShell"]);
|
|
|
1971 |
unset($conf["external::callShell"]);
|
|
|
1972 |
|
|
|
1973 |
#預設網址找不到
|
|
|
1974 |
$result["founded"]="false";
|
|
|
1975 |
|
|
|
1976 |
#如果執行失敗
|
|
|
1977 |
if($callShell["status"]==="false"){
|
|
|
1978 |
|
|
|
1979 |
#如果不是response header >= 400
|
|
|
1980 |
if($callShell["statusCode"]!==22){
|
| 226 |
liveuser |
1981 |
|
| 3 |
liveuser |
1982 |
#設置執行失敗
|
|
|
1983 |
$result["status"]="false";
|
|
|
1984 |
|
|
|
1985 |
#設置執行錯誤
|
|
|
1986 |
$result["error"]=$callShell;
|
|
|
1987 |
|
|
|
1988 |
#回傳結果
|
|
|
1989 |
return $result;
|
| 226 |
liveuser |
1990 |
|
| 3 |
liveuser |
1991 |
}#if end
|
|
|
1992 |
|
|
|
1993 |
}#if end
|
| 226 |
liveuser |
1994 |
|
| 3 |
liveuser |
1995 |
#反之
|
|
|
1996 |
else{
|
| 226 |
liveuser |
1997 |
|
| 3 |
liveuser |
1998 |
#設置網址找到
|
|
|
1999 |
$result["founded"]="true";
|
| 226 |
liveuser |
2000 |
|
| 3 |
liveuser |
2001 |
}#else end
|
|
|
2002 |
|
|
|
2003 |
#儲存得到的回應
|
|
|
2004 |
$result["content"]=$callShell["output"];
|
|
|
2005 |
|
|
|
2006 |
#初始化完整回應為空字串
|
|
|
2007 |
$result["fullContent"]="";
|
|
|
2008 |
|
|
|
2009 |
#針對每列回應
|
|
|
2010 |
foreach($result["content"] as $line){
|
| 226 |
liveuser |
2011 |
|
| 3 |
liveuser |
2012 |
#串接結果
|
|
|
2013 |
$result["fullContent"]=$result["fullContent"].$line;
|
| 226 |
liveuser |
2014 |
|
| 3 |
liveuser |
2015 |
}#foreach end
|
|
|
2016 |
|
|
|
2017 |
#儲存執行的command
|
|
|
2018 |
$result["cmd"]=$callShell["cmd"];
|
|
|
2019 |
|
|
|
2020 |
#如果有設置 cookie 檔案
|
|
|
2021 |
if(isset($conf["cookie"])){
|
|
|
2022 |
|
|
|
2023 |
#回傳 cookie 檔案的位置與名稱
|
|
|
2024 |
$result["cookie"]=$conf["cookie"];
|
|
|
2025 |
|
|
|
2026 |
}#if end
|
|
|
2027 |
|
|
|
2028 |
#設置執行正常
|
|
|
2029 |
$result["status"]="true";
|
|
|
2030 |
|
|
|
2031 |
#回傳結果
|
|
|
2032 |
return $result;
|
|
|
2033 |
|
|
|
2034 |
}#function curlCmd end
|
|
|
2035 |
|
|
|
2036 |
}#class catchWebContent end
|
|
|
2037 |
|
|
|
2038 |
?>
|