Subversion Repositories php-qbpwcf

Rev

Rev 3 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
#!/usr/bin/php
2
<?php
3
 
4
/*
5
	QBPWCF, Quick Build PHP website Component base on Fedora Linux.
226 liveuser 6
    Copyright (C) 2014~2025 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/>.
226 liveuser 22
 
3 liveuser 23
*/
24
 
25
/*
26
說明:
27
啟動 用來接收 request 來跟 web socket server 互動.
28
 
29
範例:
30
webSocketClientDaemon.php [--domain server.wss.org] [--path wss] [--port 443]
31
*/
32
 
33
#使用命名空間qbpwcf
34
namespace qbpwcf;
35
 
36
#取得 lib path
37
exec("php -f ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/folderOfUsrLib.php"),$output,$status);
38
 
39
#如果執行失敗
40
if($status!==0){
41
 
42
	#debug
43
	var_dump($cmd,$ouput,$status);
44
 
45
	#結束執行,回傳shell 1.
46
	exit(1);
47
 
48
	}#if end
49
 
50
#儲存lib path
51
$folderOfUsrLib=$output[0];
52
 
53
#以該檔案的實際位置的 lib path 為 include path 首位
54
exec("cd ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/../../".$folderOfUsrLib."/qbpwcf);pwd;",$output,$status);
55
set_include_path($output[0].PATH_SEPARATOR.get_include_path());
56
 
57
#匯入外部套件
58
include("allInOne.php");
59
 
60
#說明函式
61
function help()
62
{
63
	#印出指令說明
226 liveuser 64
	echo "Usage of ".basename(__FILE__).":".PHP_EOL;
3 liveuser 65
	echo "--domain 代表要連線的 web socket 服務域名".PHP_EOL;
66
	echo "--path 代表要連線的 web socket 服務path,預設為 wss".PHP_EOL;
67
	echo "--port 代表要連線的 web socket 服務port,預設為 443".PHP_EOL;
226 liveuser 68
 
3 liveuser 69
	#結束執行
70
	exit;
71
}
72
 
73
#函式說明:
74
#解析參數.
75
#回傳結果:
76
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
77
#$reuslt["error"],執行不正常結束的錯訊息陣列.
78
#$result["function"],當前執行的函式名稱.
79
#$result["content"],解析好的參數陣列.
80
#$result["content"][$key][$i],參數 $key 的 $i+1 個參數數值內容.
81
#$result["program"],字串,執行的程式名稱.
82
#必填參數:
83
#無
84
#可省略參數:
85
#$conf["helpFunc"],如果解析的參數不成對,則要執行的函式名稱.
86
$conf["helpFunc"]="help";
87
#備註:
88
#僅能在命令列底下執行.
89
#建議:
90
#以後可將參數 --a--b 的名稱與後面的數值 $value 存成 $result["a"]["b"][$i]=$value .
91
$parseArgu=cmd::parseArgu($conf);
92
unset($conf);
93
 
94
#如果解析參數失敗
95
if($parseArgu["status"]==="false")
96
{
97
	#印出結果
98
	var_dump($parseArgu);
226 liveuser 99
 
3 liveuser 100
	#結束執行,回傳shell 1
101
	exit(1);
226 liveuser 102
 
3 liveuser 103
}#if end
104
 
105
#檢查參數
106
#函式說明:
107
#檢查必填與可省略參數,可省略參數可指定預設要給與什麼數值內容。
108
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
109
#$reuslt["error"],執行不正常結束的錯訊息陣列.
110
#$result["function"],當前執行的函式名稱.
111
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
112
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
113
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
114
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
115
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
116
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
117
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
118
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
119
#必填寫的參數:
120
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
121
$conf["varInput"]=&$parseArgu["content"];
122
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
123
$conf["referenceVarKey"]="variableCheck::checkArguments";
124
#可以省略的參數:
125
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
126
$conf["mustBeFilledVariableName"]=array("domain");
127
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
128
$conf["mustBeFilledVariableType"]=array("array");
129
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
130
$conf["canBeEmptyString"]="false";
131
#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
132
#$conf["canNotBeEmpty"]=array();
133
#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
134
#$conf["canBeEmpty"]=array();
135
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
136
$conf["skipableVariableCanNotBeEmpty"]=array("path","port");
137
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
138
$conf["skipableVariableName"]=array("path","port");
139
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
140
$conf["skipableVariableType"]=array("array","array");
141
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
142
$conf["skipableVarDefaultValue"]=array("wss",443);
143
#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
144
#$conf["variableCheck::checkArguments"]["disallowAllSkipableVarIsEmpty"]="false";
145
#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
146
#$conf["disallowAllSkipableVarIsEmptyArray"]="";
147
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
148
#$conf["arrayCountEqualCheck"][]=array();
149
#參考資料來源:
150
#array_keys=>http://php.net/manual/en/function.array-keys.php
151
$checkArguments=variableCheck::checkArguments($conf);
152
unset($conf);
153
 
154
#若執行失敗
155
if($checkArguments["status"]==="false"){
156
 
157
	#印出結果
158
	var_dump($checkArguments);
226 liveuser 159
 
3 liveuser 160
	#結束執行,回傳shell 1
161
	exit(1);
162
 
163
	}#if end
164
 
165
#若檢查不通過
166
if($checkArguments["passed"]==="false"){
167
 
168
	#印出結果
169
	var_dump($checkArguments);
226 liveuser 170
 
3 liveuser 171
	#結束執行,回傳shell 1
172
	exit(1);
173
 
174
	}#if end
175
 
176
#預設的 unix doamin socket
177
$defaultUnixDomainSocket="./".basename(__FILE__).".sock";
178
 
179
#取得各項參數
180
$addr=$parseArgu["content"]["domain"][0];
181
$path=$parseArgu["content"]["path"][0];
182
$port=$parseArgu["content"]["port"][0];
183
 
184
#函式說明:
185
#連線到webSocket.
186
#回傳結果:
187
#$result["status"],執行正常與否,"true"代表正常,"false"代表不正常.
188
#$result["error"],錯誤訊息陣列.
189
#$resutl["function"],當前執行的涵式名稱.
190
#$result["content"],取得的回應.
191
#$result["argu"],所使用的參數.
192
#$result["Sec-WebSocket-Accept"],所得到的Sec-WebSocket-Accept.
193
#必填參數:
194
#無.
195
#可省略參數:
196
#$conf["targetServr"],字串,要連線的目標,預設為"localhost".
197
$conf["targetServr"]=$addr."/".$path;
198
#$conf["targetPort"],字串,要連線的目標port.
199
$conf["targetPort"]=$port;
200
#$conf["ssl"],字串,是否要走wss,預設為"false",不走wss;反之為"true".
201
$conf["ssl"]="true";
202
#$conf["debug"],字串,"true"代表要show debug訊息;反之為預設"false".
203
#$conf["debug"]="true";
204
#參考資料:
205
#https://www.php.net/manual/en/function.stream-socket-client.php
206
#https://www.php.net/manual/en/function.stream-context-create.php
207
#https://www.php.net/manual/en/context.ssl.php
208
#備註:
209
#建構中...
210
$clientDaemon=webSock::clientDaemon($conf);
211
unset($conf);
212
 
213
#若執行失敗
214
if($clientDaemon["status"]==="false"){
215
 
216
	#印出結果
217
	var_dump($clientDaemon);
226 liveuser 218
 
3 liveuser 219
	#結束執行,回傳shell 1.
220
	exit(1);
221
 
226 liveuser 222
	}#if end