| 513 |
liveuser |
1 |
#!/bin/php
|
|
|
2 |
<?php
|
|
|
3 |
|
|
|
4 |
/*
|
|
|
5 |
QBPWCF, Quick Build PHP website Component base on Fedora Linux.
|
| 618 |
liveuser |
6 |
Copyright (C) 2015~2024 Min-Jhin,Chen
|
| 513 |
liveuser |
7 |
|
|
|
8 |
This file is part of QBPWCF.
|
|
|
9 |
|
|
|
10 |
QBPWCF is free software: you can redistribute it and/or modify
|
|
|
11 |
it under the terms of the GNU General Public License as published by
|
|
|
12 |
the Free Software Foundation, either version 3 of the License, or
|
|
|
13 |
(at your option) any later version.
|
|
|
14 |
|
|
|
15 |
QBPWCF is distributed in the hope that it will be useful,
|
|
|
16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
18 |
GNU General Public License for more details.
|
|
|
19 |
|
|
|
20 |
You should have received a copy of the GNU General Public License
|
|
|
21 |
along with QBPWCF. If not, see <http://www.gnu.org/licenses/>.
|
|
|
22 |
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
/*
|
|
|
26 |
說明:
|
| 520 |
liveuser |
27 |
啟動 web socket server.
|
|
|
28 |
預設 addr 為 127.0.0.1.
|
|
|
29 |
預設 port 為 8080.
|
| 513 |
liveuser |
30 |
|
|
|
31 |
範例:
|
| 520 |
liveuser |
32 |
webSocketServer.php [--addr 127.0.0.1] [--port 8080]
|
| 513 |
liveuser |
33 |
*/
|
|
|
34 |
|
|
|
35 |
#使用命名空間qbpwcf
|
|
|
36 |
namespace qbpwcf;
|
|
|
37 |
|
|
|
38 |
#以該檔案的實際位置的 lib path 為 include path 首位
|
| 906 |
liveuser |
39 |
exec("cd ".pathinfo(__FILE__)["dirname"]."/../lib;pwd;",$output,$status);
|
| 513 |
liveuser |
40 |
set_include_path($output[0].PATH_SEPARATOR.get_include_path());
|
|
|
41 |
|
|
|
42 |
#匯入外部套件
|
|
|
43 |
include("allInOne.php");
|
|
|
44 |
|
|
|
45 |
#說明函式
|
|
|
46 |
function help()
|
|
|
47 |
{
|
|
|
48 |
#印出指令說明
|
|
|
49 |
echo "Usage of ".basename(__FILE__).":".PHP_EOL;
|
| 520 |
liveuser |
50 |
echo "--addr 代表 Server 要 Listen 的 address".PHP_EOL;
|
| 513 |
liveuser |
51 |
echo "--port 代表 Server 要 Listen 的 port".PHP_EOL;
|
|
|
52 |
|
|
|
53 |
#結束執行
|
|
|
54 |
exit;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
#函式說明:
|
|
|
58 |
#解析參數.
|
|
|
59 |
#回傳結果:
|
|
|
60 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
61 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
62 |
#$result["function"],當前執行的函式名稱.
|
|
|
63 |
#$result["content"],解析好的參數陣列.
|
|
|
64 |
#$result["content"][$key][$i],參數 $key 的 $i+1 個參數數值內容.
|
|
|
65 |
#$result["program"],字串,執行的程式名稱.
|
|
|
66 |
#必填參數:
|
|
|
67 |
#無
|
|
|
68 |
#可省略參數:
|
|
|
69 |
#$conf["helpFunc"],如果解析的參數不成對,則要執行的函式名稱.
|
|
|
70 |
$conf["helpFunc"]="help";
|
|
|
71 |
#備註:
|
|
|
72 |
#僅能在命令列底下執行.
|
|
|
73 |
#建議:
|
|
|
74 |
#以後可將參數 --a--b 的名稱與後面的數值 $value 存成 $result["a"]["b"][$i]=$value .
|
|
|
75 |
$parseArgu=cmd::parseArgu($conf);
|
|
|
76 |
unset($conf);
|
|
|
77 |
|
|
|
78 |
#如果解析參數失敗
|
|
|
79 |
if($parseArgu["status"]==="false")
|
|
|
80 |
{
|
|
|
81 |
#印出結果
|
|
|
82 |
var_dump($parseArgu);
|
|
|
83 |
|
|
|
84 |
#結束執行
|
|
|
85 |
exit;
|
|
|
86 |
|
|
|
87 |
}#if end
|
|
|
88 |
|
|
|
89 |
#檢查參數
|
|
|
90 |
#函式說明:
|
|
|
91 |
#檢查必填與可省略參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
92 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
93 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
94 |
#$result["function"],當前執行的函式名稱.
|
|
|
95 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
96 |
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
97 |
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
98 |
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
99 |
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
|
|
|
100 |
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
|
|
|
101 |
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
|
|
|
102 |
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
|
|
|
103 |
#必填寫的參數:
|
|
|
104 |
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
105 |
$conf["varInput"]=&$parseArgu["content"];
|
|
|
106 |
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
107 |
$conf["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
108 |
#可以省略的參數:
|
|
|
109 |
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
110 |
#$conf["mustBeFilledVariableName"]=array("config","acctVarName","passVarName","dbVarName","dbAddrVarName","dbPortVarName","backupAddr");
|
|
|
111 |
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
|
|
|
112 |
#$conf["mustBeFilledVariableType"]=array("array","array","array","array","array","array","array");
|
|
|
113 |
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
|
|
|
114 |
#$conf["canBeEmptyString"]="false";
|
|
|
115 |
#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
|
|
|
116 |
#$conf["canNotBeEmpty"]=array();
|
|
|
117 |
#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
|
|
|
118 |
#$conf["canBeEmpty"]=array();
|
|
|
119 |
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
|
|
|
120 |
#$conf["skipableVariableCanNotBeEmpty"]=array("backTime");
|
|
|
121 |
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
| 520 |
liveuser |
122 |
$conf["skipableVariableName"]=array("addr","port");
|
| 513 |
liveuser |
123 |
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
| 520 |
liveuser |
124 |
$conf["skipableVariableType"]=array("array","array");
|
| 513 |
liveuser |
125 |
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
| 520 |
liveuser |
126 |
$conf["skipableVarDefaultValue"]=array(array("127.0.0.1"),array("8080"));
|
| 513 |
liveuser |
127 |
#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
|
|
|
128 |
#$conf["variableCheck::checkArguments"]["disallowAllSkipableVarIsEmpty"]="false";
|
|
|
129 |
#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
|
|
|
130 |
#$conf["disallowAllSkipableVarIsEmptyArray"]="";
|
|
|
131 |
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
132 |
#$conf["arrayCountEqualCheck"][]=array();
|
|
|
133 |
#參考資料來源:
|
|
|
134 |
#array_keys=>http://php.net/manual/en/function.array-keys.php
|
|
|
135 |
$checkArguments=variableCheck::checkArguments($conf);
|
|
|
136 |
unset($conf);
|
|
|
137 |
|
|
|
138 |
#若執行失敗
|
|
|
139 |
if($checkArguments["status"]==="false"){
|
|
|
140 |
|
|
|
141 |
#印出結果
|
|
|
142 |
var_dump($checkArguments);
|
|
|
143 |
|
|
|
144 |
#結束執行
|
|
|
145 |
exit;
|
|
|
146 |
|
|
|
147 |
}#if end
|
|
|
148 |
|
|
|
149 |
#若檢查不通過
|
|
|
150 |
if($checkArguments["passed"]==="false"){
|
|
|
151 |
|
|
|
152 |
#印出結果
|
|
|
153 |
var_dump($checkArguments);
|
|
|
154 |
|
|
|
155 |
#結束執行
|
|
|
156 |
exit;
|
|
|
157 |
|
|
|
158 |
}#if end
|
|
|
159 |
|
|
|
160 |
#取得各項參數
|
| 520 |
liveuser |
161 |
$addr=$parseArgu["content"]["addr"][0];
|
| 513 |
liveuser |
162 |
$port=$parseArgu["content"]["port"][0];
|
|
|
163 |
|
|
|
164 |
#函式說明:
|
|
|
165 |
#建立php原生的socket tcp/ip server,進而實作webSocket
|
|
|
166 |
#回傳結果:
|
|
|
167 |
#$result["status"],執行正常與否,"true"代表正常,"false"代表不正常.
|
|
|
168 |
#$result["error"],錯誤訊息陣列.
|
|
|
169 |
#$resutl["function"],當前執行的涵式名稱.
|
|
|
170 |
#$result["argu"],所使用的參數.
|
|
|
171 |
#必填參數:
|
|
|
172 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
|
|
173 |
$conf["fileArgu"]=__FILE__;
|
|
|
174 |
#可省略參數:
|
|
|
175 |
#$conf["listenIp"],字串,要接聽的主機ip,預設為本機的ip.
|
| 520 |
liveuser |
176 |
$conf["listenIp"]=$addr;
|
| 513 |
liveuser |
177 |
#$conf["listenPort"],字串,要接聽的port,預設為已使用port+1.
|
|
|
178 |
$conf["listenPort"]=$port;
|
| 744 |
liveuser |
179 |
#$conf["wsMode"],字串,是否要用webSocket模式,預設為"false",不使用,若為"true"則要使用.
|
|
|
180 |
$conf["wsMode"]="true";
|
| 513 |
liveuser |
181 |
#$conf["processFuncs"],陣列,針對收到的訊息要呼叫的函式,會帶入一個參數陣列,array("data"=>收到的資料,"serverSock"=>serverSock,"clientSock"=>clientSock,"clientInfo"=>用戶端的資訊,"clientIndex"=>用戶端的索引,"allConn"=>所有連線的用戶端的連線資訊),回傳的結果若為陣列$result,其$result["status"]為"false"時,會結束執行(等待下個訊息).
|
|
|
182 |
$conf["processFuncs"]=array("\qbpwcf\pwd");
|
|
|
183 |
#參考資料:
|
|
|
184 |
#http://php.net/manual/en/sockets.examples.php
|
|
|
185 |
#http://us3.php.net/manual/en/function.socket-select.php
|
|
|
186 |
#response should at least end with "\r"=>http://stackoverflow.com/questions/25739768/websocket-communication-between-chromeclient-and-hotspotserver-status-line
|
|
|
187 |
#response status code should be 101=>http://stackoverflow.com/questions/29829597/i-get-a-status-200-when-connecting-to-the-websocket-but-it-is-an-error
|
|
|
188 |
#webSocket實做=>http://srchea.com/build-a-real-time-application-using-html5-websockets
|
|
|
189 |
#webSocketServer實做=>http://www.cuelogic.com/blog/php-and-html5-websocket-server-and-client-communication/
|
|
|
190 |
#備註:
|
|
|
191 |
#僅能在命令列執行.
|
|
|
192 |
$nativeSocketTcpIpServer=webSock::nativeSocketTcpIpServer($conf);
|
|
|
193 |
unset($conf);
|
|
|
194 |
|
|
|
195 |
#如果 啟動失敗
|
|
|
196 |
if($nativeSocketTcpIpServer["status"]==="false"){
|
|
|
197 |
|
|
|
198 |
#印出結果
|
|
|
199 |
var_dump($nativeSocketTcpIpServer);
|
|
|
200 |
|
|
|
201 |
#結束執行
|
|
|
202 |
exit;
|
|
|
203 |
|
|
|
204 |
}#if end
|
|
|
205 |
|
|
|
206 |
/*
|
|
|
207 |
函式說明:
|
|
|
208 |
提供取得當前路徑的功能
|
|
|
209 |
*/
|
| 520 |
liveuser |
210 |
function pwd(&$params){
|
| 513 |
liveuser |
211 |
|
|
|
212 |
#取得收到的訊息
|
|
|
213 |
$receivedData=$params["data"];
|
|
|
214 |
|
|
|
215 |
#取得 server 的 socket
|
|
|
216 |
$serverSocket=$params["serverSock"];
|
|
|
217 |
|
|
|
218 |
#取得 client 的 socket
|
|
|
219 |
$clientSocket=$params["clientSock"];
|
|
|
220 |
|
|
|
221 |
#取得 client 的資訊
|
|
|
222 |
$clientInfo=$params["clientInfo"];
|
|
|
223 |
|
|
|
224 |
#取得 client 的索引
|
|
|
225 |
$clientIndex=$params["clientIndex"];
|
|
|
226 |
|
|
|
227 |
#取得 all clients 的資訊
|
|
|
228 |
$allConn=$params["allConn"];
|
|
|
229 |
|
|
|
230 |
#函式說明:
|
|
|
231 |
#呼叫shell執行系統命令,並取得回傳的內容.
|
|
|
232 |
#回傳結果:
|
|
|
233 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
234 |
#$result["error"],錯誤訊息陣列.
|
|
|
235 |
#$result["function"],當前執行的函數名稱.
|
|
|
236 |
#$result["argu"],使用的參數.
|
|
|
237 |
#$result["cmd"],執行的指令內容.
|
|
|
238 |
#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
239 |
#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
|
|
|
240 |
#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
241 |
#$result["running"],是否還在執行.
|
|
|
242 |
#$result["pid"],pid.
|
|
|
243 |
#$result["statusCode"],執行結束後的代碼.
|
|
|
244 |
#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu").
|
|
|
245 |
#必填參數:
|
|
|
246 |
#$conf["command"],字串,要執行的指令.
|
|
|
247 |
$conf["command"]="pwd";
|
|
|
248 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
|
|
249 |
$conf["fileArgu"]=__FILE__;
|
|
|
250 |
#可省略參數:
|
|
|
251 |
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
|
|
|
252 |
#$conf["argu"]=array("");
|
|
|
253 |
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
|
|
|
254 |
#$conf["arguIsAddr"]=array();
|
|
|
255 |
#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
|
|
|
256 |
#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
|
|
|
257 |
#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
|
|
|
258 |
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
|
|
|
259 |
#$conf["enablePrintDescription"]="true";
|
|
|
260 |
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
|
|
|
261 |
#$conf["printDescription"]="";
|
|
|
262 |
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
|
|
|
263 |
#$conf["escapeshellarg"]="false";
|
|
|
264 |
#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
|
|
|
265 |
#$conf["thereIsShellVar"]=array();
|
|
|
266 |
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
|
|
|
267 |
#$conf["username"]="";
|
|
|
268 |
#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
|
|
269 |
#$conf["password"]="";
|
|
|
270 |
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
|
|
|
271 |
#$conf["useScript"]="";
|
|
|
272 |
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
|
|
|
273 |
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
|
|
|
274 |
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
|
|
|
275 |
#$conf["inBackGround"]="";
|
|
|
276 |
#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
|
|
|
277 |
#$conf["getErr"]="false";
|
|
|
278 |
#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
|
|
|
279 |
#$conf["doNotRun"]="false";
|
|
|
280 |
#參考資料:
|
|
|
281 |
#exec=>http://php.net/manual/en/function.exec.php
|
|
|
282 |
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
|
|
|
283 |
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
|
|
|
284 |
#備註:
|
|
|
285 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
|
|
|
286 |
#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
|
|
|
287 |
$callShell=external::callShell($conf);
|
|
|
288 |
unset($conf);
|
|
|
289 |
|
|
|
290 |
#如果執行失敗
|
|
|
291 |
if($callShell["status"]==="false"){
|
|
|
292 |
|
|
|
293 |
#印出結果
|
| 520 |
liveuser |
294 |
return $callShell;
|
| 513 |
liveuser |
295 |
|
|
|
296 |
}#if end
|
|
|
297 |
|
| 520 |
liveuser |
298 |
#取得經過編碼的執行後結果字串
|
|
|
299 |
$callShellResultStr=json_encode($callShell);
|
|
|
300 |
|
|
|
301 |
#debug
|
|
|
302 |
#var_dump(__LINE__,$callShellResultStr);
|
|
|
303 |
|
|
|
304 |
#函式說明:
|
|
|
305 |
#加密 handshake 後要傳送的訊息
|
|
|
306 |
#回傳結果:
|
|
|
307 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
308 |
#$result["error"],執行不正常結束的錯訊息陣列.
|
|
|
309 |
#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
|
|
|
310 |
#$result["argu"],陣列,使用的參數.
|
|
|
311 |
#必填參數:
|
|
|
312 |
#$conf["text"],字串,要加密的訊息.
|
|
|
313 |
$conf["webSock::encode"]["text"]=$callShellResultStr;
|
|
|
314 |
#可省略參數:
|
|
|
315 |
#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
|
|
|
316 |
#$conf["ping"]="true";
|
|
|
317 |
#參考資料:
|
|
|
318 |
#https://www.rfc-editor.org/rfc/rfc6455#page-28, Web Socket Base Framing Protocol.
|
|
|
319 |
#備註:
|
|
|
320 |
#目前$conf["text"]長度超過125會出錯.
|
|
|
321 |
$talkback=webSock::encode($conf["webSock::encode"]);
|
| 513 |
liveuser |
322 |
unset($conf["webSock::encode"]);
|
| 520 |
liveuser |
323 |
|
|
|
324 |
#如果產生 web socket 格式封包 失敗
|
|
|
325 |
if($talkback["status"]==="false"){
|
|
|
326 |
|
|
|
327 |
#印出結果
|
|
|
328 |
return $talkback;
|
|
|
329 |
|
|
|
330 |
}#if end
|
|
|
331 |
|
|
|
332 |
#針對每個訊息的分段
|
|
|
333 |
foreach($talkback["content"] as $msgIndex=>$msg){
|
|
|
334 |
|
|
|
335 |
#提示第幾個訊息
|
|
|
336 |
echo "第 ".($msgIndex+1)." 個訊息的內容:".PHP_EOL;
|
|
|
337 |
|
|
|
338 |
#函式說明:
|
|
|
339 |
#將字串中的每個字變成bytes陣列
|
|
|
340 |
#回傳結果:
|
|
|
341 |
#$result["content"],bytes陣列.
|
|
|
342 |
#必填參數:
|
|
|
343 |
#$conf["input"],字串,要轉換成bytes陣列的字串.
|
|
|
344 |
$conf["stringProcess::str2bytesArray"]["input"]=$msg;
|
|
|
345 |
#可省略參數:
|
|
|
346 |
#無.
|
|
|
347 |
#參考資料:
|
|
|
348 |
#無.
|
|
|
349 |
#備註:
|
|
|
350 |
#無.
|
|
|
351 |
$str2bytesArray=stringProcess::str2bytesArray($conf["stringProcess::str2bytesArray"]);
|
|
|
352 |
unset($conf["stringProcess::str2bytesArray"]);
|
|
|
353 |
|
|
|
354 |
#如果執行失敗
|
|
|
355 |
if($str2bytesArray["status"]==="false"){
|
|
|
356 |
|
|
|
357 |
#印出結果
|
|
|
358 |
return $str2bytesArray;
|
|
|
359 |
|
|
|
360 |
}#if end
|
|
|
361 |
|
|
|
362 |
#針對每個 bytes
|
|
|
363 |
foreach($str2bytesArray["content"] as $byteIndex=>$bytes){
|
|
|
364 |
|
|
|
365 |
#函式說明:
|
|
|
366 |
#將bytes數字(16進位)轉換為bit字串(0跟1來表述)
|
|
|
367 |
#回傳結果:
|
|
|
368 |
#$result["content"],bit字串(0跟1來表述).
|
|
|
369 |
#必填參數:
|
|
|
370 |
#$conf["bytes"],字串,要轉換成bit的bytes數字.
|
|
|
371 |
$conf["stringProcess::bytes2bitString"]["bytes"]=$bytes;
|
|
|
372 |
#可省略參數:
|
|
|
373 |
#無.
|
|
|
374 |
#參考資料:
|
|
|
375 |
#無.
|
|
|
376 |
#備註:
|
|
|
377 |
#無.
|
|
|
378 |
$bytes2bitString=stringProcess::bytes2bitString($conf["stringProcess::bytes2bitString"]);
|
|
|
379 |
unset($conf["stringProcess::bytes2bitString"]);
|
|
|
380 |
|
|
|
381 |
#如果執行失敗
|
|
|
382 |
if($bytes2bitString["status"]==="false"){
|
|
|
383 |
|
|
|
384 |
#印出結果
|
|
|
385 |
return $bytes2bitString;
|
|
|
386 |
|
|
|
387 |
}#if end
|
|
|
388 |
|
|
|
389 |
#印出 8bit
|
|
|
390 |
echo $bytes2bitString["content"];
|
|
|
391 |
|
|
|
392 |
#如果為 32 bit 結束
|
|
|
393 |
if(($byteIndex+1)%4===0){
|
|
|
394 |
|
|
|
395 |
#印出換行符號
|
|
|
396 |
echo PHP_EOL;
|
|
|
397 |
|
|
|
398 |
}#if end
|
|
|
399 |
|
|
|
400 |
}#foreach end
|
|
|
401 |
|
|
|
402 |
#回傳訊息
|
|
|
403 |
$socket_write=socket_write($clientSocket, $msg, strlen($msg));
|
| 513 |
liveuser |
404 |
|
| 520 |
liveuser |
405 |
#換行
|
|
|
406 |
echo PHP_EOL;
|
| 513 |
liveuser |
407 |
|
| 520 |
liveuser |
408 |
#debug
|
|
|
409 |
#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($clientSocket)));
|
|
|
410 |
|
|
|
411 |
}#foreach end
|
|
|
412 |
|
| 513 |
liveuser |
413 |
#初始化結果
|
|
|
414 |
$result=array();
|
|
|
415 |
|
|
|
416 |
#設置執行正常
|
| 520 |
liveuser |
417 |
$result["status"]="continue";
|
|
|
418 |
|
| 513 |
liveuser |
419 |
#回傳結果
|
|
|
420 |
return $result;
|
|
|
421 |
|
| 520 |
liveuser |
422 |
}#function pwd end
|
|
|
423 |
|