| 14 |
liveuser |
1 |
#!/bin/php
|
|
|
2 |
<?php
|
|
|
3 |
|
|
|
4 |
#使用命名空間qbpwcf
|
|
|
5 |
namespace qbpwcf;
|
|
|
6 |
|
|
|
7 |
#匯入外部套件
|
|
|
8 |
include("qbpwcf/allInOne.php");
|
|
|
9 |
|
|
|
10 |
#help訊息
|
|
|
11 |
function help(){
|
|
|
12 |
|
|
|
13 |
#提示用法
|
|
|
14 |
echo basename(__FILE__)." -h/--help 顯示本內容".PHP_EOL;
|
|
|
15 |
echo basename(__FILE__)." -ip 要連線到的IP位置 -port 要連接的port -dbFile 用於交換資料的檔案 -timeout 連線idle多少秒要斷開 -email 連線用的賬號".PHP_EOL;
|
|
|
16 |
|
|
|
17 |
}#function help end
|
|
|
18 |
|
|
|
19 |
#函式說明:
|
|
|
20 |
#抓取命令列的參數.
|
|
|
21 |
#回傳結果:
|
|
|
22 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
23 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
24 |
#$result["function"],當前執行的函式名稱.
|
|
|
25 |
#$result["argu"],使用的參數陣列.
|
|
|
26 |
#$result["content"],要回傳的參數陣列.
|
|
|
27 |
#$result["count"],參數的數量.
|
|
|
28 |
#必填參數:
|
|
|
29 |
#無
|
|
|
30 |
#可省略參數:
|
|
|
31 |
#$conf["echo"],"true"代表要將抓到的參數一個個印出來,"false"代表用回傳的方式,預設為"false".
|
|
|
32 |
$conf["echo"]="false";
|
|
|
33 |
$getArgu=cmd::getArgu($conf);
|
|
|
34 |
unset($conf);
|
|
|
35 |
|
|
|
36 |
#如果執行失敗
|
|
|
37 |
if($getArgu["status"]==="false"){
|
|
|
38 |
|
|
|
39 |
#印出結果
|
|
|
40 |
var_dump($getArgu);
|
|
|
41 |
|
|
|
42 |
#結束執行
|
|
|
43 |
exit;
|
|
|
44 |
|
|
|
45 |
}#if end
|
|
|
46 |
|
|
|
47 |
#如果沒有參數
|
|
|
48 |
if(!isset($getArgu["content"][1])){
|
|
|
49 |
|
|
|
50 |
#提示訊息
|
|
|
51 |
help();
|
|
|
52 |
|
|
|
53 |
#結束執行
|
|
|
54 |
exit;
|
|
|
55 |
|
|
|
56 |
}#if end
|
|
|
57 |
|
|
|
58 |
#反之如果參數為 -h 或 --help
|
|
|
59 |
else if( $getArgu["content"][1]==="-h" || $getArgu["content"][1]==="--help"){
|
|
|
60 |
|
|
|
61 |
#提示訊息
|
|
|
62 |
help();
|
|
|
63 |
|
|
|
64 |
#結束執行
|
|
|
65 |
exit;
|
|
|
66 |
|
|
|
67 |
}#if end
|
|
|
68 |
|
|
|
69 |
#函式說明:
|
|
|
70 |
#解析參數.
|
|
|
71 |
#回傳結果:
|
|
|
72 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
73 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
74 |
#$result["function"],當前執行的函式名稱.
|
|
|
75 |
#$result["content"],解析好的參數陣列.
|
|
|
76 |
#$result["content"][$key][$i],參數 $key 的 $i+1 個參數數值內容.
|
|
|
77 |
#$result["program"],字串,執行的程式名稱.
|
|
|
78 |
#必填參數:
|
|
|
79 |
#無
|
|
|
80 |
#可省略參數:
|
|
|
81 |
#無
|
|
|
82 |
#備註:
|
|
|
83 |
#僅能在命令列底下執行.
|
|
|
84 |
#建議:
|
|
|
85 |
#以後可將參數 --a--b 的名稱與後面的數值 $value 存成 $result["a"]["b"][$i]=$value .
|
|
|
86 |
$conf=array()l
|
|
|
87 |
$parseArgu=cmd::parseArgu($conf);
|
|
|
88 |
unset($conf);
|
|
|
89 |
|
|
|
90 |
#如果執行失敗
|
|
|
91 |
if($parseArgu["status"]==="false"){
|
|
|
92 |
|
|
|
93 |
#印出結果
|
|
|
94 |
var_dump($parseArgu);
|
|
|
95 |
|
|
|
96 |
#結束執行
|
|
|
97 |
exit;
|
|
|
98 |
|
|
|
99 |
}#if end
|
|
|
100 |
|
|
|
101 |
#如果參數ip或port或dbFile或timeout或email有一個不存在.
|
|
|
102 |
if( !isset($parseArgu["content"]["ip"]) || !isset($parseArgu["content"]["port"]) || !isset($parseArgu["content"]["dbFile"]) || !isset($parseArgu["content"]["timeout"]) || !isset($parseArgu["content"]["email"]) ){
|
|
|
103 |
|
|
|
104 |
#提示訊息
|
|
|
105 |
help();
|
|
|
106 |
|
|
|
107 |
#結束執行
|
|
|
108 |
exit;
|
|
|
109 |
|
|
|
110 |
}#if end
|
|
|
111 |
|
|
|
112 |
#取得目標 ip
|
|
|
113 |
$ip=$parseArgu["content"]["ip"][0];
|
|
|
114 |
|
|
|
115 |
#取得目標 port
|
|
|
116 |
$port=$parseArgu["content"]["port"][0];
|
|
|
117 |
|
|
|
118 |
#取得用於交換資料的檔案
|
|
|
119 |
$dbFile=$parseArgu["content"]["dbFile"][0];
|
|
|
120 |
|
|
|
121 |
#取得idle timeout的時間秒數
|
|
|
122 |
$timeout=$parseArgu["content"]["timeout"][0];
|
|
|
123 |
|
|
|
124 |
#取得email
|
|
|
125 |
$email=$parseArgu["content"]["email"][0];
|
|
|
126 |
|
|
|
127 |
#建立 tcp socket client
|
|
|
128 |
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
|
|
|
129 |
|
|
|
130 |
#如果建立 socket 失敗
|
|
|
131 |
if ($socket === false) {
|
|
|
132 |
|
|
|
133 |
#設置執行失敗
|
|
|
134 |
$result["status"]="false";
|
|
|
135 |
|
|
|
136 |
#設置錯誤訊息
|
|
|
137 |
$result["error"][]="socket_create() failed: reason: " . socket_strerror(socket_last_error());
|
|
|
138 |
|
|
|
139 |
#印出結果
|
|
|
140 |
var_dump($result);
|
|
|
141 |
|
|
|
142 |
#結束執行
|
|
|
143 |
exit;
|
|
|
144 |
|
|
|
145 |
}#if end
|
|
|
146 |
|
|
|
147 |
#連線到 tcp socket server
|
|
|
148 |
$socket_connect = @socket_connect($socket, $ip, $port);
|
|
|
149 |
|
|
|
150 |
#如果建立 socket 失敗
|
|
|
151 |
if ($socket_connect === false) {
|
|
|
152 |
|
|
|
153 |
#設置執行失敗
|
|
|
154 |
$result["status"]="false";
|
|
|
155 |
|
|
|
156 |
#設置錯誤訊息
|
|
|
157 |
$result["error"][]="socket_create() failed: reason: " . socket_strerror(socket_last_error($socket));
|
|
|
158 |
|
|
|
159 |
#印出結果
|
|
|
160 |
var_dump($result);
|
|
|
161 |
|
|
|
162 |
#結束執行
|
|
|
163 |
exit;
|
|
|
164 |
|
|
|
165 |
}#if end
|
|
|
166 |
|
|
|
167 |
#載入資料庫資訊
|
|
|
168 |
#涵式說明:
|
|
|
169 |
#解析PHP檔案裡面的變數.
|
|
|
170 |
#回傳結果:
|
|
|
171 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
172 |
#$result["error"],錯誤訊息.
|
|
|
173 |
#$result["function"],當前執行的函數名稱.
|
|
|
174 |
#$result["argu"],所使用的參數.
|
|
|
175 |
#$result["content"],找到的變數內容陣列.
|
|
|
176 |
#$result["content"]["value"],依找到變數順序的數值.
|
|
|
177 |
#$result["content"]["struc"],依找到變數順序的階層結構.
|
|
|
178 |
#$result["content"]["direct],變數名稱對應的數值內容.
|
|
|
179 |
#必填參數:
|
|
|
180 |
#$conf["file"],字串,檔案的路徑與名稱.
|
|
|
181 |
$conf["file"]="/var/www/html/latest/application/config/database.php";
|
|
|
182 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
|
|
|
183 |
$conf["fileArgu"]=__FILE__;
|
|
|
184 |
#$conf["varName"],字串陣列,要搜尋的變數名稱,例如要搜尋變數$email則輸入"email".
|
|
|
185 |
$conf["varName"]=array("db['default']['hostname']","db['default']['port']","db['default']['username']","db['default']['password']","db['default']['database']");
|
|
|
186 |
#參考資料:
|
|
|
187 |
#https://www.php.net/manual/en/function.parse-str.php
|
|
|
188 |
$parseVaraiableInPHPfile=fileAccess::parseVaraiableInPHPfile($conf);
|
|
|
189 |
unset($conf);
|
|
|
190 |
|
|
|
191 |
#如果解析檔案失敗
|
|
|
192 |
if($parseVaraiableInPHPfile["status"]==="false"){
|
|
|
193 |
|
|
|
194 |
#印出內容
|
|
|
195 |
var_dump($parseVaraiableInPHPfile);
|
|
|
196 |
|
|
|
197 |
#結束執行
|
|
|
198 |
exit;
|
|
|
199 |
|
|
|
200 |
}#if end
|
|
|
201 |
|
|
|
202 |
#取得資料庫位址
|
|
|
203 |
$dbAddress=$parseVaraiableInPHPfile["content"]["direct"]["db['default']['hostname']"];
|
|
|
204 |
|
|
|
205 |
#取得資料庫port
|
|
|
206 |
$dbPort=$parseVaraiableInPHPfile["content"]["direct"]["db['default']['port']"];
|
|
|
207 |
|
|
|
208 |
#取得資料庫使用者
|
|
|
209 |
$dbUser=$parseVaraiableInPHPfile["content"]["direct"]["db['default']['username']"];
|
|
|
210 |
|
|
|
211 |
#取得資料庫密碼
|
|
|
212 |
$dbPassword=$parseVaraiableInPHPfile["content"]["direct"]["db['default']['password']"];
|
|
|
213 |
|
|
|
214 |
#取得資料庫名稱
|
|
|
215 |
$dbName=$parseVaraiableInPHPfile["content"]["direct"]["db['default']['database']"];
|
|
|
216 |
|
|
|
217 |
#查尋賬號對應的密碼
|
|
|
218 |
#涵式說明:
|
|
|
219 |
#一次取得資料庫、表的資料
|
|
|
220 |
#回傳的結果
|
|
|
221 |
#$result["status"],執行結果"true"為成功;"false"為執行失敗。
|
|
|
222 |
#$result["error"],錯誤訊息陣列。
|
|
|
223 |
#$result["function"],當前執行的漢書名稱.
|
|
|
224 |
#$result["argu"],使用的參數.
|
|
|
225 |
#$result["dataColumnName"],抓取的資料欄位名稱陣列.
|
|
|
226 |
#$result["dataColumnName"][$i]代表第$i+1個欄位名稱
|
|
|
227 |
#$result["dataContent"],爲資料的內容。
|
|
|
228 |
#$result["dataContent"][$conf["WhereColumnName"][$i]][$dataSetNum]
|
|
|
229 |
#$dataSetNum 爲第$dataSetNum+1筆資料
|
|
|
230 |
#$conf["WhereColumnName"][$i] 爲第 $i+1 個欄位的名稱
|
|
|
231 |
#$result["dataCount"],爲取得的資料筆數。
|
|
|
232 |
#$result["sql"],執行的sql字串.
|
|
|
233 |
#必填的參數:
|
|
|
234 |
#$conf["dbAddress"],字串,爲dbServer的位置。
|
|
|
235 |
$conf["dbAddress"]=$dbAddress;
|
|
|
236 |
#$conf["dbAccount"],字串,爲登入dbServer的帳號。
|
|
|
237 |
$conf["dbAccount"]=$dbUser;
|
|
|
238 |
#$conf["dbName"],字串,爲要存取的資料庫名稱
|
|
|
239 |
$conf["dbName"]=$dbName;
|
|
|
240 |
#$conf["tableName"],字串,爲要存取的資料表名稱
|
|
|
241 |
$conf["tableName"]="auth_users";
|
|
|
242 |
#$conf["columnYouWant"],字串陣列,你想要的欄位!,若設為「array("*")」則代表全部欄位.
|
|
|
243 |
$conf["columnYouWant"]=array("password");
|
|
|
244 |
#可省略的參數:
|
|
|
245 |
#$conf["dbPassword"],字串,爲要存取dbServer的密碼.
|
|
|
246 |
$conf["dbPassword"]=$dbPassword;
|
|
|
247 |
#$conf["dbPort"],字串,爲連線到mysql-Server時要使用的port,可省略,若省略則代表使用預設的3306 port.
|
|
|
248 |
$conf["dbPort"]=$dbPort;
|
|
|
249 |
#$conf["WhereColumnName"],字串陣列,用於判斷語句的欄位項目陣列。
|
|
|
250 |
$conf["WhereColumnName"]=array("email");
|
|
|
251 |
#$conf["WhereColumnValue"],字串陣列,用於判斷語句的欄位數值陣列,若與LIKE搭配,則可以在關鍵自字串的左右名加上「%」符號,這樣就可以搜尋具有該字串的內容。
|
|
|
252 |
$conf["WhereColumnValue"]=array($email);
|
|
|
253 |
#$conf["WhereColumnCombine"],字串陣列,用於判斷語句當中需要()起來的判斷式,須爲陣列值,"s"代表「(」,"e"代表「)」 ,若無則須設爲""。
|
|
|
254 |
#$conf["WhereColumnCombine"]=array("");
|
|
|
255 |
#$conf["WhereColumnOperator"],字串陣列,用於判斷語句的比較符號陣列,可以用的符號有「"="、"!="、">"、"<"、"LIKE"、"NOT LIKE"」,預設都爲「=」。
|
|
|
256 |
#$conf["WhereColumnOperator"]=array("");
|
|
|
257 |
#$conf["WhereColumnAndOr"],字串陣列,用於判斷語句條件之間成立的條件是AND還是OR,須爲陣列值。其數量應爲要判斷的欄位數量減一。
|
|
|
258 |
#$conf["WhereColumnAndOr"]=array("");
|
|
|
259 |
#$conf["whereIn"],二維字串陣列,為每個in語句的內容,特定欄位數值等於陣列元素之一。array(array("colName",array("a","b","c")));代表欄位colName的值為a,b,c三者之一.
|
|
|
260 |
#$conf["whereIn"]=array(array("colName",array("a","b","c")));
|
|
|
261 |
#$conf["whereNotIn"],二維字串陣列,為每個not in語句的內容,array(array("colName",array("a","b","c")));代表欄位colName的值不為a,b,c三者之一.
|
|
|
262 |
#$conf["whereNotIn"]=array(array("colName",array("a","b","c")));
|
|
|
263 |
#$conf["orderItem"],字串,爲排序的項目依據,若要用隨機抽樣,可以用"rand()",可省略。
|
|
|
264 |
#$conf["orderItem"]="";
|
|
|
265 |
#$conf["ascORdesc"],字串,爲要低增還是遞減排序,asc爲遞增;desc爲遞減。
|
|
|
266 |
#$conf["ascORdesc"]="";
|
|
|
267 |
#$conf["numberStart"],字串,為從第幾筆開始讀取,預設為0,代筆第一筆。
|
|
|
268 |
#$conf["numberStart"]="0";
|
|
|
269 |
#$conf["numLimit"],字串,為要取幾筆資料,可以省略,省略則表示不限制數目。
|
|
|
270 |
#$conf["numLimit"]="30";
|
|
|
271 |
#$conf["groupBy"],字串陣列,爲要以哪幾個欄爲作爲分羣的依據(欄位相同的數值僅會取出一筆)。
|
|
|
272 |
#$conf["groupBy"]=array("");
|
|
|
273 |
$fastGetDbData=db::fastGetDbData($conf);
|
|
|
274 |
unset($conf);
|
|
|
275 |
|
|
|
276 |
#如果抓取資料失敗
|
|
|
277 |
if($fastGetDbData["status"]==="false"){
|
|
|
278 |
|
|
|
279 |
#印出內容
|
|
|
280 |
var_dump($fastGetDbData);
|
|
|
281 |
|
|
|
282 |
#結束執行
|
|
|
283 |
exit;
|
|
|
284 |
|
|
|
285 |
}#if end
|
|
|
286 |
|
|
|
287 |
#取得 password
|
|
|
288 |
$password=$fastGetDbData["dataContent"]["password"][0];
|
|
|
289 |
|
|
|
290 |
#get client info
|
|
|
291 |
$_webaddr = trim(`hostname`) . '(' . getHostByName(trim(`hostname`)) . ')';
|
|
|
292 |
|
|
|
293 |
#進行登入的動作
|
|
|
294 |
|
|
|
295 |
#登入的語法
|
|
|
296 |
$data="SIGNIN;".$_webaddr.";".$email.";".$password.";true;".time(0)."000";
|
|
|
297 |
|
|
|
298 |
#寫入到 devcom
|
|
|
299 |
socket_write($socket,$data,strlen($data));
|
|
|
300 |
|
|
|
301 |
#讀取回應
|
|
|
302 |
#$returnStr=socket_read($socket,2048);
|
|
|
303 |
|
|
|
304 |
#嘗試開啟dbFile
|
|
|
305 |
#涵式說明:
|
|
|
306 |
#將字串寫入到檔案
|
|
|
307 |
#回傳的結果:
|
|
|
308 |
#$result["status"],"true"表示檔案寫入成功,"false"表示檔案寫入失敗.
|
|
|
309 |
#$result["error"],錯誤訊息陣列.
|
|
|
310 |
#$result["function"],當前執行的函數名稱.
|
|
|
311 |
#$result["fileInfo"],實際上寫入的檔案資訊陣列.
|
|
|
312 |
#$result["fileInfo"]["createdFileName"],建立好的檔案名稱.
|
|
|
313 |
#$result["fileInfo"]["createdFilePath"],檔案建立的路徑.
|
|
|
314 |
#$result["fileInfo"]["createdFilePathAndName"].建立好的檔案名稱與路徑.
|
|
|
315 |
#$result["argu"],使用的參數.
|
|
|
316 |
#必填的參數:
|
|
|
317 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
|
|
|
318 |
$conf["fileArgu"]=__FILE__;
|
|
|
319 |
#可省略的參數:
|
|
|
320 |
#$conf["fileName"],字串,爲要編輯的檔案名稱,預設為隨機產生的檔案名稱.
|
|
|
321 |
$conf["fileName"]=$dbFile;
|
|
|
322 |
#$conf["inputString"],字串,爲要寫入到裏面的內容,若要每筆資料寫入後換行,則可以在字串內容後面加上 \r\n 即可,預設為"".
|
|
|
323 |
$data=array();
|
|
|
324 |
$data["connect_time"]=time(0); #連線建立的時間
|
|
|
325 |
$data["last_action_time"]=$data["connect_time"]; #初始化最後的連線時間.
|
|
|
326 |
$conf["inputString"]=json_encode($data).PHP_EOL;
|
|
|
327 |
#$conf["writeMethod"],字串,爲檔案撰寫的方式,可省略,是複寫'a'還是,重新寫入'w',預設爲'w',重新寫入.
|
|
|
328 |
#$conf["writeMethod"]="a";
|
|
|
329 |
#$conf["checkRepeat"],字串,"true"代表建立檔案之前要先檢查檔案是否存在,若存在則在原名稱後面加上從(1)開始的編號.
|
|
|
330 |
#$conf["checkRepeat"]="";
|
|
|
331 |
#$conf["filenameExtensionStartPoint"],字串,檔案的副檔名是從倒數第幾個小數點(dot)開始,預設為"1",最後一個小數點,必須與$conf["checkRepeat"]搭配才會生效.
|
|
|
332 |
#$conf["filenameExtensionStartPoint"]="";
|
|
|
333 |
#$conf["repeatNameRule"],字串,遇到相同名稱的檔案要如何加上識別的編號,編號用「\$i」表示,預設為"(\$i)",必須與$conf["checkRepeat"]搭配才會生效.
|
|
|
334 |
#$conf["repeatNameRule"]="";
|
|
|
335 |
$writeTextIntoFile=fileAccess::writeTextIntoFile($conf);
|
|
|
336 |
unset($conf);
|
|
|
337 |
|
|
|
338 |
#如果建立檔案失敗
|
|
|
339 |
if($writeTextIntoFile["status"]==="false"){
|
|
|
340 |
|
|
|
341 |
#印出結果
|
|
|
342 |
var_dump($result);
|
|
|
343 |
|
|
|
344 |
#結束執行
|
|
|
345 |
exit;
|
|
|
346 |
|
|
|
347 |
}#if end
|
|
|
348 |
|
|
|
349 |
#讓程式不會終止
|
|
|
350 |
while(true){
|
|
|
351 |
|
|
|
352 |
#函式說明:
|
|
|
353 |
#依據行號分隔抓取檔案的內容,結果會回傳一個陣列
|
|
|
354 |
#回傳的變數說明:
|
|
|
355 |
#$result["status"],執行是否成功,"true"代表成功;"fasle"代表失敗.
|
|
|
356 |
#$result["error"],錯誤訊息提示.
|
|
|
357 |
#$result["warning"],警告訊息.
|
|
|
358 |
#$result["function"],當前執行的函數名稱.
|
|
|
359 |
#$result["fileContent"],爲檔案的內容陣列.
|
|
|
360 |
#$result["lineCount"],爲檔案內容總共的行數.
|
|
|
361 |
#$result["fullContent"],為檔案的完整內容.
|
|
|
362 |
#$result["base64data"],為檔案的base64內容.
|
|
|
363 |
#$result["mimeType"],為檔案的mime type.
|
|
|
364 |
#必填的參數:
|
|
|
365 |
#$conf["filePositionAndName"],字串,爲檔案的位置以及名稱.
|
|
|
366 |
$conf["filePositionAndName"]=$dbFile;
|
|
|
367 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
|
|
|
368 |
$conf["fileArgu"]=__FILE__;
|
|
|
369 |
#參考資料:
|
|
|
370 |
#file(),取得檔案內容的行數.
|
|
|
371 |
#file=>http:#php.net/manual/en/function.file.php
|
|
|
372 |
#rtrim(),剔除透過file()取得每行內容結尾的換行符號.
|
|
|
373 |
#filesize=>http://php.net/manual/en/function.filesize.php
|
|
|
374 |
$getFileContent=fileAccess::getFileContent($conf);
|
|
|
375 |
unset($conf);
|
|
|
376 |
|
|
|
377 |
#如果建立檔案失敗
|
|
|
378 |
if($getFileContent["status"]==="false"){
|
|
|
379 |
|
|
|
380 |
#印出結果
|
|
|
381 |
var_dump($getFileContent);
|
|
|
382 |
|
|
|
383 |
#結束執行
|
|
|
384 |
exit;
|
|
|
385 |
|
|
|
386 |
}#if end
|
|
|
387 |
|
|
|
388 |
#如果讀取的行數小於1
|
|
|
389 |
if($getFileContent["lineCount"]<1){
|
|
|
390 |
|
|
|
391 |
#印出結果
|
|
|
392 |
var_dump($getFileContent);
|
|
|
393 |
|
|
|
394 |
#結束執行
|
|
|
395 |
exit;
|
|
|
396 |
|
|
|
397 |
}#if end
|
|
|
398 |
|
|
|
399 |
#初始化儲存第一行的內容
|
|
|
400 |
$firstLine="";
|
|
|
401 |
|
|
|
402 |
#初始化儲存要寫入的既有行內容
|
|
|
403 |
$oldLine=array();
|
|
|
404 |
|
|
|
405 |
#初始化儲存要寫入的新行內容
|
|
|
406 |
$newLine=array();
|
|
|
407 |
|
|
|
408 |
#針對每一行的結果
|
|
|
409 |
foreach($getFileContent["fileContent"] as $no=>$line){
|
|
|
410 |
|
|
|
411 |
#如果是第一行的內容
|
|
|
412 |
if($no===0){
|
|
|
413 |
|
|
|
414 |
#解析內容
|
|
|
415 |
$firstLine=json_decode($line);
|
|
|
416 |
|
|
|
417 |
#如果最後一次動作的時間記錄距離現在已經超過 $timeout 秒了
|
|
|
418 |
if(time(0)-$firstLine->last_action_time > $timeout){
|
|
|
419 |
|
|
|
420 |
#移除檔案
|
|
|
421 |
#涵式說明:
|
|
|
422 |
#移除檔案
|
|
|
423 |
#回傳的結果:
|
|
|
424 |
#$result["status"],"true"代表移除成功,"false"代表移除失敗.
|
|
|
425 |
#$result["error"],錯誤訊息陣列
|
|
|
426 |
#$result["warning"],警告訊息陣列
|
|
|
427 |
#$result["function"],當前執行的函數名稱
|
|
|
428 |
#必填的參數:
|
|
|
429 |
#$conf["fileAddress"],字串,要移除檔案的位置.
|
|
|
430 |
$conf["fileAddress"]=$dbFile;
|
|
|
431 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.
|
|
|
432 |
$conf["fileArgu"]=__FILE__;
|
|
|
433 |
#可省略參數:
|
|
|
434 |
#$conf["commentsArray"],字串陣列,提示的文字描述,$conf["commentsArray"][$i]代表第($+1)行的描述.
|
|
|
435 |
#$conf["commentsArray"]=array("");
|
|
|
436 |
$delFile=fileAccess::delFile($conf);
|
|
|
437 |
unset($conf);
|
|
|
438 |
|
|
|
439 |
#如果移除檔案失敗
|
|
|
440 |
if($delFile["status"]==="false"){
|
|
|
441 |
|
|
|
442 |
#印出內容
|
|
|
443 |
var_dump($delFile);
|
|
|
444 |
|
|
|
445 |
#結束執行
|
|
|
446 |
exit;
|
|
|
447 |
|
|
|
448 |
}#if end
|
|
|
449 |
|
|
|
450 |
#結束程式
|
|
|
451 |
exit;
|
|
|
452 |
|
|
|
453 |
}#if end
|
|
|
454 |
|
|
|
455 |
#json_encode first line
|
|
|
456 |
$firstLine=json_encode($firstLine);
|
|
|
457 |
|
|
|
458 |
}#if end
|
|
|
459 |
|
|
|
460 |
#反之不是第一行
|
|
|
461 |
else{
|
|
|
462 |
|
|
|
463 |
#解析內容
|
|
|
464 |
$line=json_decode($line);
|
|
|
465 |
|
|
|
466 |
#取得資料
|
|
|
467 |
$data=$line->data;
|
|
|
468 |
|
|
|
469 |
#取得類型
|
|
|
470 |
$type=$line->type;
|
|
|
471 |
|
|
|
472 |
#如果是要下發到gw
|
|
|
473 |
if($type==="tgw"){
|
|
|
474 |
|
|
|
475 |
#初始化取得的回應為 false 代表失敗
|
|
|
476 |
$returnStr=false;
|
|
|
477 |
|
|
|
478 |
#休息時間預設為 0 秒
|
|
|
479 |
$sleepSec=0;
|
|
|
480 |
|
|
|
481 |
#如果尚未取得回應
|
|
|
482 |
while( ($returnStr===false) && sleep($sleepSec)===0 ){
|
|
|
483 |
|
|
|
484 |
#取得gwId;
|
|
|
485 |
$gwId=$line->gwId;
|
|
|
486 |
|
|
|
487 |
#讀取資料庫設定
|
|
|
488 |
#涵式說明:
|
|
|
489 |
#解析PHP檔案裡面的變數.
|
|
|
490 |
#回傳結果:
|
|
|
491 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
492 |
#$result["error"],錯誤訊息.
|
|
|
493 |
#$result["function"],當前執行的函數名稱.
|
|
|
494 |
#$result["argu"],所使用的參數.
|
|
|
495 |
#$result["content"],找到的變數內容陣列.
|
|
|
496 |
#$result["content"]["value"],依找到變數順序的數值.
|
|
|
497 |
#$result["content"]["struc"],依找到變數順序的階層結構.
|
|
|
498 |
#$result["content"]["direct],變數名稱對應的數值內容.
|
|
|
499 |
#必填參數:
|
|
|
500 |
#$conf["file"],字串,檔案的路徑與名稱.
|
|
|
501 |
$conf["file"]="/var/www/html/latest/application/config/database.php";
|
|
|
502 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
|
|
|
503 |
$conf["fileArgu"]=__FILE__;
|
|
|
504 |
#$conf["varName"],字串陣列,要搜尋的變數名稱,例如要搜尋變數$email則輸入"email".
|
|
|
505 |
$conf["varName"]=array("db['default']['hostname']","db['default']['port']","db['default']['username']","db['default']['password']","db['default']['database']");
|
|
|
506 |
#參考資料:
|
|
|
507 |
#https://www.php.net/manual/en/function.parse-str.php
|
|
|
508 |
$parseVaraiableInPHPfile=fileAccess::parseVaraiableInPHPfile($conf);
|
|
|
509 |
unset($conf);
|
|
|
510 |
|
|
|
511 |
#如果解析檔案失敗
|
|
|
512 |
if($parseVaraiableInPHPfile["status"]==="false"){
|
|
|
513 |
|
|
|
514 |
#印出內容
|
|
|
515 |
var_dump($parseVaraiableInPHPfile);
|
|
|
516 |
|
|
|
517 |
#結束執行
|
|
|
518 |
exit;
|
|
|
519 |
|
|
|
520 |
}#if end
|
|
|
521 |
|
|
|
522 |
#取得資料庫位址
|
|
|
523 |
$dbAddress=$parseVaraiableInPHPfile["content"]["direct"]["db['default']['hostname']"];
|
|
|
524 |
|
|
|
525 |
#取得資料庫port
|
|
|
526 |
$dbPort=$parseVaraiableInPHPfile["content"]["direct"]["db['default']['port']"];
|
|
|
527 |
|
|
|
528 |
#取得資料庫使用者
|
|
|
529 |
$dbUser=$parseVaraiableInPHPfile["content"]["direct"]["db['default']['username']"];
|
|
|
530 |
|
|
|
531 |
#取得資料庫密碼
|
|
|
532 |
$dbPassword=$parseVaraiableInPHPfile["content"]["direct"]["db['default']['password']"];
|
|
|
533 |
|
|
|
534 |
#取得資料庫名稱
|
|
|
535 |
$dbName=$parseVaraiableInPHPfile["content"]["direct"]["db['default']['database']"];
|
|
|
536 |
|
|
|
537 |
#查尋gw識別碼
|
|
|
538 |
#涵式說明:
|
|
|
539 |
#一次取得資料庫、表的資料
|
|
|
540 |
#回傳的結果
|
|
|
541 |
#$result["status"],執行結果"true"為成功;"false"為執行失敗。
|
|
|
542 |
#$result["error"],錯誤訊息陣列。
|
|
|
543 |
#$result["function"],當前執行的漢書名稱.
|
|
|
544 |
#$result["argu"],使用的參數.
|
|
|
545 |
#$result["dataColumnName"],抓取的資料欄位名稱陣列.
|
|
|
546 |
#$result["dataColumnName"][$i]代表第$i+1個欄位名稱
|
|
|
547 |
#$result["dataContent"],爲資料的內容。
|
|
|
548 |
#$result["dataContent"][$conf["WhereColumnName"][$i]][$dataSetNum]
|
|
|
549 |
#$dataSetNum 爲第$dataSetNum+1筆資料
|
|
|
550 |
#$conf["WhereColumnName"][$i] 爲第 $i+1 個欄位的名稱
|
|
|
551 |
#$result["dataCount"],爲取得的資料筆數。
|
|
|
552 |
#$result["sql"],執行的sql字串.
|
|
|
553 |
#必填的參數:
|
|
|
554 |
#$conf["dbAddress"],字串,爲dbServer的位置。
|
|
|
555 |
$conf["dbAddress"]=$dbAddress;
|
|
|
556 |
#$conf["dbAccount"],字串,爲登入dbServer的帳號。
|
|
|
557 |
$conf["dbAccount"]=$dbUser;
|
|
|
558 |
#$conf["dbName"],字串,爲要存取的資料庫名稱
|
|
|
559 |
$conf["dbName"]=$dbName;
|
|
|
560 |
#$conf["tableName"],字串,爲要存取的資料表名稱
|
|
|
561 |
$conf["tableName"]="device";
|
|
|
562 |
#$conf["columnYouWant"],字串陣列,你想要的欄位!,若設為「array("*")」則代表全部欄位.
|
|
|
563 |
$conf["columnYouWant"]=array("telcom_gsn","lanmac","telcom_imi");
|
|
|
564 |
#可省略的參數:
|
|
|
565 |
#$conf["dbPassword"],字串,爲要存取dbServer的密碼.
|
|
|
566 |
$conf["dbPassword"]=$dbPassword;
|
|
|
567 |
#$conf["dbPort"],字串,爲連線到mysql-Server時要使用的port,可省略,若省略則代表使用預設的3306 port.
|
|
|
568 |
$conf["dbPort"]=$dbPort;
|
|
|
569 |
#$conf["WhereColumnName"],字串陣列,用於判斷語句的欄位項目陣列。
|
|
|
570 |
$conf["WhereColumnName"]=array("id");
|
|
|
571 |
#$conf["WhereColumnValue"],字串陣列,用於判斷語句的欄位數值陣列,若與LIKE搭配,則可以在關鍵自字串的左右名加上「%」符號,這樣就可以搜尋具有該字串的內容。
|
|
|
572 |
$conf["WhereColumnValue"]=array($gwId);
|
|
|
573 |
#$conf["WhereColumnCombine"],字串陣列,用於判斷語句當中需要()起來的判斷式,須爲陣列值,"s"代表「(」,"e"代表「)」 ,若無則須設爲""。
|
|
|
574 |
#$conf["WhereColumnCombine"]=array("");
|
|
|
575 |
#$conf["WhereColumnOperator"],字串陣列,用於判斷語句的比較符號陣列,可以用的符號有「"="、"!="、">"、"<"、"LIKE"、"NOT LIKE"」,預設都爲「=」。
|
|
|
576 |
#$conf["WhereColumnOperator"]=array("");
|
|
|
577 |
#$conf["WhereColumnAndOr"],字串陣列,用於判斷語句條件之間成立的條件是AND還是OR,須爲陣列值。其數量應爲要判斷的欄位數量減一。
|
|
|
578 |
#$conf["WhereColumnAndOr"]=array("");
|
|
|
579 |
#$conf["whereIn"],二維字串陣列,為每個in語句的內容,特定欄位數值等於陣列元素之一。array(array("colName",array("a","b","c")));代表欄位colName的值為a,b,c三者之一.
|
|
|
580 |
#$conf["whereIn"]=array(array("colName",array("a","b","c")));
|
|
|
581 |
#$conf["whereNotIn"],二維字串陣列,為每個not in語句的內容,array(array("colName",array("a","b","c")));代表欄位colName的值不為a,b,c三者之一.
|
|
|
582 |
#$conf["whereNotIn"]=array(array("colName",array("a","b","c")));
|
|
|
583 |
#$conf["orderItem"],字串,爲排序的項目依據,若要用隨機抽樣,可以用"rand()",可省略。
|
|
|
584 |
#$conf["orderItem"]="";
|
|
|
585 |
#$conf["ascORdesc"],字串,爲要低增還是遞減排序,asc爲遞增;desc爲遞減。
|
|
|
586 |
#$conf["ascORdesc"]="";
|
|
|
587 |
#$conf["numberStart"],字串,為從第幾筆開始讀取,預設為0,代筆第一筆。
|
|
|
588 |
#$conf["numberStart"]="0";
|
|
|
589 |
#$conf["numLimit"],字串,為要取幾筆資料,可以省略,省略則表示不限制數目。
|
|
|
590 |
#$conf["numLimit"]="30";
|
|
|
591 |
#$conf["groupBy"],字串陣列,爲要以哪幾個欄爲作爲分羣的依據(欄位相同的數值僅會取出一筆)。
|
|
|
592 |
#$conf["groupBy"]=array("");
|
|
|
593 |
$fastGetDbData=db::fastGetDbData($conf);
|
|
|
594 |
unset($conf);
|
|
|
595 |
|
|
|
596 |
#如果抓取資料失敗
|
|
|
597 |
if($fastGetDbData["status"]==="false"){
|
|
|
598 |
|
|
|
599 |
#印出內容
|
|
|
600 |
var_dump($fastGetDbData);
|
|
|
601 |
|
|
|
602 |
#結束執行
|
|
|
603 |
exit;
|
|
|
604 |
|
|
|
605 |
}#if end
|
|
|
606 |
|
|
|
607 |
#取得 gw gsn
|
|
|
608 |
$gsn=$fastGetDbData["dataContent"]["telcom_gsn"][0];
|
|
|
609 |
|
|
|
610 |
#取得 gw lanmac
|
|
|
611 |
$lanamc=$fastGetDbData["dataContent"]["lanmac"][0];
|
|
|
612 |
|
|
|
613 |
#取得 gw imi
|
|
|
614 |
$imi=$fastGetDbData["dataContent"]["telcom_imi"][0];
|
|
|
615 |
|
|
|
616 |
#如果有gsn
|
|
|
617 |
if(!empty($gsn)){
|
|
|
618 |
|
|
|
619 |
#設置gw真正的識別碼
|
|
|
620 |
$gwRiD=$gsn;
|
|
|
621 |
|
|
|
622 |
#設置gw的識別碼類型
|
|
|
623 |
$gwType="GSN";
|
|
|
624 |
|
|
|
625 |
}#if end
|
|
|
626 |
|
|
|
627 |
#如果有gsn
|
|
|
628 |
else if(!empty($lanamc)){
|
|
|
629 |
|
|
|
630 |
#設置gw真正的識別碼
|
|
|
631 |
$gwRiD=$lanamc;
|
|
|
632 |
|
|
|
633 |
#設置gw的識別碼類型
|
|
|
634 |
$gwType="MAC";
|
|
|
635 |
|
|
|
636 |
}#if end
|
|
|
637 |
|
|
|
638 |
#如果有gsn
|
|
|
639 |
else if(!empty($imi)){
|
|
|
640 |
|
|
|
641 |
#設置gw真正的識別碼
|
|
|
642 |
$gwRiD=$imi;
|
|
|
643 |
|
|
|
644 |
#設置gw的識別碼類型
|
|
|
645 |
$gwType="IMI";
|
|
|
646 |
|
|
|
647 |
}#if end
|
|
|
648 |
|
|
|
649 |
#設置要傳送給Devcom的訊息
|
|
|
650 |
$data="ACTION;".time()."000".";WEBCTL;".$gwRiD.";".$gwType.';'.$data."\r";
|
|
|
651 |
|
|
|
652 |
#寫入到 devcom
|
|
|
653 |
socket_write($socket,$data,strlen($data));
|
|
|
654 |
|
|
|
655 |
#讀取回應
|
|
|
656 |
$returnStr=socket_read($socket,2048);
|
|
|
657 |
|
|
|
658 |
#如果讀取回應失敗
|
|
|
659 |
if($returnStr===false){
|
|
|
660 |
|
|
|
661 |
#設置要休息5秒鐘
|
|
|
662 |
$sleepSec=5;
|
|
|
663 |
|
|
|
664 |
}#if end
|
|
|
665 |
|
|
|
666 |
}#while end
|
|
|
667 |
|
|
|
668 |
#儲存要寫入的新行
|
|
|
669 |
$newLine[]=json_encode(array("type"=>"fgw","gwId"=>$gwId,"data"=>$returnStr));
|
|
|
670 |
|
|
|
671 |
#解析第一行
|
|
|
672 |
$firstLine=json_decode($firstLine);
|
|
|
673 |
|
|
|
674 |
#更新第一行的內容
|
|
|
675 |
$firstLine->last_action_time=time(0);
|
|
|
676 |
|
|
|
677 |
#編碼第一行的內容
|
|
|
678 |
$firstLine=json_encode($firstLine);
|
|
|
679 |
|
|
|
680 |
}#if end
|
|
|
681 |
|
|
|
682 |
#反之是 gw 的回應
|
|
|
683 |
else{
|
|
|
684 |
#儲存既有的gw回應
|
|
|
685 |
$oldLine[]=$line;
|
|
|
686 |
|
|
|
687 |
}#else end
|
|
|
688 |
|
|
|
689 |
}#else end
|
|
|
690 |
|
|
|
691 |
}#foreach end
|
|
|
692 |
|
|
|
693 |
#合併要寫入的內容
|
|
|
694 |
#函式說明:
|
|
|
695 |
#將多個一維陣列串聯起來,key從0開始排序.
|
|
|
696 |
#回傳的結果:
|
|
|
697 |
#$result["status"],"true"表執行正常,"false"代表執行不正常.
|
|
|
698 |
#$result["error"],錯誤訊息陣列.
|
|
|
699 |
#$result["function"],當前執行的函數.
|
|
|
700 |
#$result["content"],合併好的一維陣列.
|
|
|
701 |
#必填的參數
|
|
|
702 |
#$conf["inputArray"],陣列,要合併的一維陣列變數,例如:=array($array1,$array2);
|
|
|
703 |
$conf["inputArray"]=array(array($firstLine),$oldLine,$newLine);
|
|
|
704 |
#可省略的參數:
|
|
|
705 |
#$conf["allowRepeat"],字串,預設為"true",允許重複的結果;若為"false"則不會出現重複的元素內容.
|
|
|
706 |
#$conf["allowRepeat"]="true";
|
|
|
707 |
$mergeArray=arrays::mergeArray($conf);
|
|
|
708 |
unset($conf);
|
|
|
709 |
|
|
|
710 |
#如果合併資料失敗
|
|
|
711 |
if($mergeArray["status"]==="false"){
|
|
|
712 |
|
|
|
713 |
#印出內容
|
|
|
714 |
var_dump($mergeArray);
|
|
|
715 |
|
|
|
716 |
#結束執行
|
|
|
717 |
exit;
|
|
|
718 |
|
|
|
719 |
}#if end
|
|
|
720 |
|
|
|
721 |
#另存要寫入的資料
|
|
|
722 |
$data=$mergeArray["content"];
|
|
|
723 |
|
|
|
724 |
#寫入新內容到dbFile
|
|
|
725 |
#涵式說明:
|
|
|
726 |
#將多行字串寫入到檔案
|
|
|
727 |
#回傳的結果:
|
|
|
728 |
#$result["status"],"true"表示檔案寫入成功,"false"表示檔案寫入失敗.
|
|
|
729 |
#$result["error"],錯誤訊息陣列.
|
|
|
730 |
#$result["function"],當前執行函數的名稱.
|
|
|
731 |
#必填的參數:
|
|
|
732 |
#$conf["fileName"],字串,爲要編輯的檔案名稱
|
|
|
733 |
$conf["fileName"]=$dbFile;
|
|
|
734 |
#$conf["inputString"],字串陣列,爲要寫入到 $conf["fileName"] 裏面的內容
|
|
|
735 |
#$conf["inputString"][$i] 代表第 $i+1 行。
|
|
|
736 |
$conf["inputString"]=$data;
|
|
|
737 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
|
|
|
738 |
$conf["fileArgu"]=__FILE__;
|
|
|
739 |
#可省略的參數:
|
|
|
740 |
#$conf["writeMethod"]="a";#爲檔案撰寫的方式,可省略,是複寫'a'還是,重新寫入'w',預設爲'w',重新寫入。
|
|
|
741 |
$writeMultiLine=fileAccess::writeMultiLine($conf);
|
|
|
742 |
unset($conf);
|
|
|
743 |
|
|
|
744 |
#休息0.1秒
|
|
|
745 |
usleep(100000);
|
|
|
746 |
|
|
|
747 |
}#while end
|
|
|
748 |
|
|
|
749 |
?>
|