Subversion Repositories php-qbpwcf

Rev

Rev 71 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 71 Rev 226
Line 1... Line 1...
1
<?php
1
<?php
2
 
2
 
3
/*
3
/*
4
 
4
 
5
	QBPWCF, Quick Build PHP website Component base on Fedora Linux.
5
	QBPWCF, Quick Build PHP website Component base on Fedora Linux.
6
    Copyright (C) 2015~2025 Min-Jhin,Chen
6
    Copyright (C) 2014~2025 MIN ZHI, CHEN
7
 
7
 
8
    This file is part of QBPWCF.
8
    This file is part of QBPWCF.
9
 
9
 
10
    QBPWCF is free software: you can redistribute it and/or modify
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
11
    it under the terms of the GNU General Public License as published by
Line 21... Line 21...
21
    along with QBPWCF.  If not, see <http://www.gnu.org/licenses/>.
21
    along with QBPWCF.  If not, see <http://www.gnu.org/licenses/>.
22
 
22
 
23
*/
23
*/
24
 
24
 
25
namespace qbpwcf;
25
namespace qbpwcf;
26
	
26
 
27
#import all lib by composer(include ratchet)
27
#import all lib by composer(include ratchet)
28
require 'composer/vendor/autoload.php';
28
require 'composer/vendor/autoload.php';
29
 
29
 
30
#使用 Ratchet\MessageComponentInterface 界面
30
#使用 Ratchet\MessageComponentInterface 界面
31
use Ratchet\MessageComponentInterface;
31
use Ratchet\MessageComponentInterface;
Line 33... Line 33...
33
#使用 Ratchet\ConnectionInterface 界面
33
#使用 Ratchet\ConnectionInterface 界面
34
use Ratchet\ConnectionInterface;
34
use Ratchet\ConnectionInterface;
35
 
35
 
36
#用 Chat 類別來實作 MessageComponentInterface 界面
36
#用 Chat 類別來實作 MessageComponentInterface 界面
37
class Chat implements MessageComponentInterface{
37
class Chat implements MessageComponentInterface{
38
		
38
 
39
	#初始化儲存使用者資訊的陣列
39
	#初始化儲存使用者資訊的陣列
40
	private $connInfo=array();	
40
	private $connInfo=array();
41
	
41
 
42
	#初始化儲存未傳送出去的留言訊息
42
	#初始化儲存未傳送出去的留言訊息
43
	#"fromId",訊息來源的id
43
	#"fromId",訊息來源的id
44
	#"toId",訊息目的的id
44
	#"toId",訊息目的的id
45
	#"fromUserId",訊息來源的userId
45
	#"fromUserId",訊息來源的userId
46
	#"toUserId",訊息目標的userId
46
	#"toUserId",訊息目標的userId
47
	#"msg",傳送的訊息
47
	#"msg",傳送的訊息
48
	private $unSendMsg=array();
48
	private $unSendMsg=array();
49
	
49
 
50
	#初始化一開始尚未能夠連線到資料庫
50
	#初始化一開始尚未能夠連線到資料庫
51
	private static $dbTouchable=false;
51
	private static $dbTouchable=false;
52
		
52
 
53
	#初始化db的位置
53
	#初始化db的位置
54
	public static $dbAddress="localhost";	
54
	public static $dbAddress="localhost";
55
		
55
 
56
	#初始化連線db用的帳戶
56
	#初始化連線db用的帳戶
57
	public static $dbAccount="root";
57
	public static $dbAccount="root";
58
	
58
 
59
	#初始化連線的db名稱
59
	#初始化連線的db名稱
60
	public static $dbName="test";
60
	public static $dbName="test";
61
	
61
 
62
	#初始化連線db時用的密碼
62
	#初始化連線db時用的密碼
63
	public static $dbPassword="";
63
	public static $dbPassword="";
64
	
64
 
65
	#初始化連線db的哪個資料表
65
	#初始化連線db的哪個資料表
66
	public static $memberTableName="member";
66
	public static $memberTableName="member";
67
		
67
 
68
	#初始化登入時用來驗證帳號的資料表欄位名稱
68
	#初始化登入時用來驗證帳號的資料表欄位名稱
69
	public static $accountCol="account";
69
	public static $accountCol="account";
70
	
70
 
71
	#初始化登入時用來驗證密碼的資料表欄位名稱,設為""代表不用密碼來認證
71
	#初始化登入時用來驗證密碼的資料表欄位名稱,設為""代表不用密碼來認證
72
	public static $passwordCol="password";
72
	public static $passwordCol="password";
73
		
73
 
74
	#建構子
74
	#建構子
75
	public function __construct(){
75
	public function __construct(){
76
		
76
 
77
		#如果尚未可以上資料庫
77
		#如果尚未可以上資料庫
78
		if(!(self::$dbTouchable)){
78
		if(!(self::$dbTouchable)){
79
			
79
 
80
			#嘗試連線到目標資料庫.資料表
80
			#嘗試連線到目標資料庫.資料表
81
			#涵式說明:
81
			#涵式說明:
82
			#一次取得資料庫、表的資料
82
			#一次取得資料庫、表的資料
83
			#回傳的結果
83
			#回傳的結果
84
			#$result["status"],執行結果"true"為成功;"false"為執行失敗。
84
			#$result["status"],執行結果"true"為成功;"false"為執行失敗。
Line 96... Line 96...
96
			$conf["db::fastGetDbData"]["dbAddress"]=self::$dbAddress;#爲dbServer的位置。
96
			$conf["db::fastGetDbData"]["dbAddress"]=self::$dbAddress;#爲dbServer的位置。
97
			$conf["db::fastGetDbData"]["dbAccount"]=self::$dbAccount;#爲登入dbServer的帳號。
97
			$conf["db::fastGetDbData"]["dbAccount"]=self::$dbAccount;#爲登入dbServer的帳號。
98
			$conf["db::fastGetDbData"]["dbName"]=self::$dbName;#爲要存取的資料庫名稱
98
			$conf["db::fastGetDbData"]["dbName"]=self::$dbName;#爲要存取的資料庫名稱
99
			$conf["db::fastGetDbData"]["tableName"]=self::$memberTableName;#爲要存取的資料表名稱
99
			$conf["db::fastGetDbData"]["tableName"]=self::$memberTableName;#爲要存取的資料表名稱
100
			$conf["db::fastGetDbData"]["columnYouWant"]=array("id",self::$accountCol);#你想要的欄位!,若設為「array("*")」則代表全部欄位.
100
			$conf["db::fastGetDbData"]["columnYouWant"]=array("id",self::$accountCol);#你想要的欄位!,若設為「array("*")」則代表全部欄位.
101
			
101
 
102
			#如果 $passwordCol 不為空
102
			#如果 $passwordCol 不為空
103
			if(self::$passwordCol!=""){
103
			if(self::$passwordCol!=""){
104
				
104
 
105
				#加上密碼欄位
105
				#加上密碼欄位
106
				$conf["db::fastGetDbData"]["columnYouWant"][]=self::$passwordCol;
106
				$conf["db::fastGetDbData"]["columnYouWant"][]=self::$passwordCol;
107
				
107
 
108
				}#if end
108
				}#if end
109
			
109
 
110
			#可省略的參數:
110
			#可省略的參數:
111
			$conf["db::fastGetDbData"]["dbPassword"]=self::$dbPassword;#爲要存取dbServer的密碼
111
			$conf["db::fastGetDbData"]["dbPassword"]=self::$dbPassword;#爲要存取dbServer的密碼
112
			#$conf["db::fastGetDbData"]["WhereColumnName"]=array("account","password");#用於判斷語句的欄位項目陣列。
112
			#$conf["db::fastGetDbData"]["WhereColumnName"]=array("account","password");#用於判斷語句的欄位項目陣列。
113
			#$conf["db::fastGetDbData"]["WhereColumnValue"]=array($ac,$pw);#用於判斷語句的欄位數值陣列,若與LIKE搭配,則可以在關鍵自字串的左右名加上「%」符號,這樣就可以搜尋具有該字串的內容。
113
			#$conf["db::fastGetDbData"]["WhereColumnValue"]=array($ac,$pw);#用於判斷語句的欄位數值陣列,若與LIKE搭配,則可以在關鍵自字串的左右名加上「%」符號,這樣就可以搜尋具有該字串的內容。
114
			#$conf["WhereColumnCombine"]=array("");#用於判斷語句當中需要()起來的判斷式,須爲陣列值,"s"代表「(」,"e"代表「)」 ,若無則須設爲""。
114
			#$conf["WhereColumnCombine"]=array("");#用於判斷語句當中需要()起來的判斷式,須爲陣列值,"s"代表「(」,"e"代表「)」 ,若無則須設爲""。
Line 121... Line 121...
121
			#$conf["groupBy"]=array("");#爲要以哪幾個欄爲作爲分羣的依據(欄位相同的數值僅會取出一筆)。
121
			#$conf["groupBy"]=array("");#爲要以哪幾個欄爲作爲分羣的依據(欄位相同的數值僅會取出一筆)。
122
			#備註:
122
			#備註:
123
			#建議在查詢資料前,能夠檢查是否每個欄位都存在.
123
			#建議在查詢資料前,能夠檢查是否每個欄位都存在.
124
			$fastGetDbData=db::fastGetDbData($conf["db::fastGetDbData"]);
124
			$fastGetDbData=db::fastGetDbData($conf["db::fastGetDbData"]);
125
			unset($conf["db::fastGetDbData"]);
125
			unset($conf["db::fastGetDbData"]);
126
			
126
 
127
			#如果取得資料失敗
127
			#如果取得資料失敗
128
			if($fastGetDbData["status"]==="false"){
128
			if($fastGetDbData["status"]==="false"){
129
				
129
 
130
				#設置執行失敗
130
				#設置執行失敗
131
				$result["status"]="false";
131
				$result["status"]="false";
132
			
132
 
133
				#設置執行錯誤訊息
133
				#設置執行錯誤訊息
134
				$result["error"]=$fastGetDbData;
134
				$result["error"]=$fastGetDbData;
135
				
135
 
136
				#印出debug訊息
136
				#印出debug訊息
137
				var_dump($result);
137
				var_dump($result);
138
				
138
 
139
				#結束程式
139
				#結束程式
140
				exit;
140
				exit;
141
				
141
 
142
				}#if end
142
				}#if end
143
				
143
 
144
			#連線成功,將self::dbTouchable設為true
144
			#連線成功,將self::dbTouchable設為true
145
			self::$dbTouchable=true;
145
			self::$dbTouchable=true;
146
			
146
 
147
			}#if end
147
			}#if end
148
		
148
 
149
        	$this->clients = new \SplObjectStorage;
149
        	$this->clients = new \SplObjectStorage;
150
        
150
 
151
		}#funciton __construct end
151
		}#funciton __construct end
152
	
152
 
153
	#當用戶與伺服器建立連線時
153
	#當用戶與伺服器建立連線時
154
    	public function onOpen(ConnectionInterface $conn){
154
    	public function onOpen(ConnectionInterface $conn){
155
		
155
 
156
		// Store the new connection to send messages to later
156
		// Store the new connection to send messages to later
157
        	$this->clients->attach($conn);
157
        	$this->clients->attach($conn);
158
 
158
 
159
		#提示server有clent連上web socket.
159
		#提示server有clent連上web socket.
160
        	echo "New connection! ({$conn->resourceId})\n";
160
        	echo "New connection! ({$conn->resourceId})\n";
161
        
161
 
162
		#用client的id為index來儲存額外的資訊
162
		#用client的id為index來儲存額外的資訊
163
        	#元素 "conn" 代表已經建立連線的ConnectionInterface物件
163
        	#元素 "conn" 代表已經建立連線的ConnectionInterface物件
164
 		#元素 "talkTo" 代表要跟誰講話
164
 		#元素 "talkTo" 代表要跟誰講話
165
		#元素 "userId" 代表辨識使用者的id
165
		#元素 "userId" 代表辨識使用者的id
166
		#元素 "loigin=array("ac","pw")" 代表client輸入的帳號與密碼
166
		#元素 "loigin=array("ac","pw")" 代表client輸入的帳號與密碼
167
     	  	 #$this->connInfo[$conn->resourceId]=array("conn"=>$conn,"talkTo"=>array(),"userId"=>"","login"=>array("ac"=>"","pw"=>""),"type"=>"");
167
     	  	 #$this->connInfo[$conn->resourceId]=array("conn"=>$conn,"talkTo"=>array(),"userId"=>"","login"=>array("ac"=>"","pw"=>""),"type"=>"");
168
        	$this->connInfo[$conn->resourceId]=array("conn"=>$conn,"talkTo"=>array(),"userId"=>"","login"=>array("ac"=>"","pw"=>""));
168
        	$this->connInfo[$conn->resourceId]=array("conn"=>$conn,"talkTo"=>array(),"userId"=>"","login"=>array("ac"=>"","pw"=>""));
169
		
169
 
170
		#提示登入		
170
		#提示登入
171
		$conn->send(json_encode("Please input your account! ex:account:ws1"));
171
		$conn->send(json_encode("Please input your account! ex:account:ws1"));
172
				
172
 
173
		}#function onOpen end
173
		}#function onOpen end
174
 
174
 
175
	#當伺服器收到訊息時
175
	#當伺服器收到訊息時
176
   	public function onMessage(ConnectionInterface $from, $msg){
176
   	public function onMessage(ConnectionInterface $from, $msg){
177
		
177
 
178
		#如果該連線沒有使用者id
178
		#如果該連線沒有使用者id
179
		if($this->connInfo[$from->resourceId]["userId"]===""){
179
		if($this->connInfo[$from->resourceId]["userId"]===""){
180
			
180
 
181
			#如果尚未輸入帳戶
181
			#如果尚未輸入帳戶
182
			if($this->connInfo[$from->resourceId]["login"]["ac"]===""){
182
			if($this->connInfo[$from->resourceId]["login"]["ac"]===""){
183
			
183
 
184
				#如果 $msg 長度大於 "account:"
184
				#如果 $msg 長度大於 "account:"
185
				if(strlen($msg) > strlen("account:")){
185
				if(strlen($msg) > strlen("account:")){
186
					
186
 
187
					#檢查有無前置字元 "account:"
187
					#檢查有無前置字元 "account:"
188
					#函式說明:
188
					#函式說明:
189
					#將字串特定關鍵字與其前面的內容剔除
189
					#將字串特定關鍵字與其前面的內容剔除
190
					#回傳結果:
190
					#回傳結果:
191
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
191
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 198... Line 198...
198
					#必填的參數:
198
					#必填的參數:
199
					$conf["stringProcess::delStrBeforeKeyWord"]["stringIn"]=$msg;#要處理的字串.
199
					$conf["stringProcess::delStrBeforeKeyWord"]["stringIn"]=$msg;#要處理的字串.
200
					$conf["stringProcess::delStrBeforeKeyWord"]["keyWord"]="account:";#特定字串.
200
					$conf["stringProcess::delStrBeforeKeyWord"]["keyWord"]="account:";#特定字串.
201
					$delStrBeforeKeyWord=stringProcess::delStrBeforeKeyWord($conf["stringProcess::delStrBeforeKeyWord"]);
201
					$delStrBeforeKeyWord=stringProcess::delStrBeforeKeyWord($conf["stringProcess::delStrBeforeKeyWord"]);
202
					unset($conf["stringProcess::delStrBeforeKeyWord"]);
202
					unset($conf["stringProcess::delStrBeforeKeyWord"]);
203
					
203
 
204
					#如果移除前置字元失敗
204
					#如果移除前置字元失敗
205
					if($delStrBeforeKeyWord["status"]==="false"){
205
					if($delStrBeforeKeyWord["status"]==="false"){
206
						
206
 
207
						#設置執行失敗
207
						#設置執行失敗
208
						$result["status"]="false";
208
						$result["status"]="false";
209
					
209
 
210
						#設置執行錯誤訊息
210
						#設置執行錯誤訊息
211
						$result["error"]=$delStrBeforeKeyWord;
211
						$result["error"]=$delStrBeforeKeyWord;
212
						
212
 
213
						#回傳結果
213
						#回傳結果
214
						return $result;
214
						return $result;
215
						
215
 
216
						}#if end
216
						}#if end
217
						
217
 
218
					#如果有符合條件的前置字元
218
					#如果有符合條件的前置字元
219
					if($delStrBeforeKeyWord["founded"]==="true"){
219
					if($delStrBeforeKeyWord["founded"]==="true"){
220
						
220
 
221
						#儲存帳戶
221
						#儲存帳戶
222
						$this->connInfo[$from->resourceId]["login"]["ac"]=$delStrBeforeKeyWord["content"];
222
						$this->connInfo[$from->resourceId]["login"]["ac"]=$delStrBeforeKeyWord["content"];
223
												
223
 
224
						}#if end
224
						}#if end
225
					
225
 
226
					#反之代表格式錯誤	
226
					#反之代表格式錯誤
227
					else{
227
					else{
228
						
228
 
229
						#提示輸入帳戶
229
						#提示輸入帳戶
230
						$from->send(json_encode("Please input your account! ex:account:ws1"));
230
						$from->send(json_encode("Please input your account! ex:account:ws1"));
231
						
231
 
232
						#結束程式
232
						#結束程式
233
						return true;
233
						return true;
234
						
234
 
235
						}#else end
235
						}#else end
236
					
236
 
237
					}#if end
237
					}#if end
238
					
238
 
239
				#反之代表輸入錯誤格式的account
239
				#反之代表輸入錯誤格式的account
240
				else{
240
				else{
241
					
241
 
242
					#提示輸入帳號	
242
					#提示輸入帳號
243
					$from->send(json_encode("Please input your account! ex:account:ws1"));
243
					$from->send(json_encode("Please input your account! ex:account:ws1"));
244
					
244
 
245
					#結束程式
245
					#結束程式
246
					return true;
246
					return true;
247
					
247
 
248
					}#else end
248
					}#else end
249
				
249
 
250
				}#if end
250
				}#if end
251
						
251
 
252
			#如果有設置密碼欄位
252
			#如果有設置密碼欄位
253
			if(self::$passwordCol!==""){
253
			if(self::$passwordCol!==""){
254
				
254
 
255
				#如果尚未輸入密碼
255
				#如果尚未輸入密碼
256
				if($this->connInfo[$from->resourceId]["login"]["pw"]===""){
256
				if($this->connInfo[$from->resourceId]["login"]["pw"]===""){
257
					
257
 
258
					#$msg 長度大於 "password:"
258
					#$msg 長度大於 "password:"
259
					if(strlen($msg) > strlen("password:")){
259
					if(strlen($msg) > strlen("password:")){
260
						
260
 
261
						#檢查有無前置字元 "password:"
261
						#檢查有無前置字元 "password:"
262
						#函式說明:
262
						#函式說明:
263
						#將字串特定關鍵字與其前面的內容剔除
263
						#將字串特定關鍵字與其前面的內容剔除
264
						#回傳結果:
264
						#回傳結果:
265
						#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
265
						#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 272... Line 272...
272
						#必填的參數:
272
						#必填的參數:
273
						$conf["stringProcess::delStrBeforeKeyWord"]["stringIn"]=$msg;#要處理的字串.
273
						$conf["stringProcess::delStrBeforeKeyWord"]["stringIn"]=$msg;#要處理的字串.
274
						$conf["stringProcess::delStrBeforeKeyWord"]["keyWord"]="password:";#特定字串.
274
						$conf["stringProcess::delStrBeforeKeyWord"]["keyWord"]="password:";#特定字串.
275
						$delStrBeforeKeyWord=stringProcess::delStrBeforeKeyWord($conf["stringProcess::delStrBeforeKeyWord"]);
275
						$delStrBeforeKeyWord=stringProcess::delStrBeforeKeyWord($conf["stringProcess::delStrBeforeKeyWord"]);
276
						unset($conf["stringProcess::delStrBeforeKeyWord"]);
276
						unset($conf["stringProcess::delStrBeforeKeyWord"]);
277
						
277
 
278
						#如果移除前置字元失敗
278
						#如果移除前置字元失敗
279
						if($delStrBeforeKeyWord["status"]==="false"){
279
						if($delStrBeforeKeyWord["status"]==="false"){
280
							
280
 
281
							#設置執行失敗
281
							#設置執行失敗
282
							$result["status"]="false";
282
							$result["status"]="false";
283
						
283
 
284
							#設置執行錯誤訊息
284
							#設置執行錯誤訊息
285
							$result["error"]=$delStrBeforeKeyWord;
285
							$result["error"]=$delStrBeforeKeyWord;
286
							
286
 
287
							#回傳結果
287
							#回傳結果
288
							return $result;
288
							return $result;
289
							
289
 
290
							}#if end
290
							}#if end
291
							
291
 
292
						#如果有符合條件的前置字元
292
						#如果有符合條件的前置字元
293
						if($delStrBeforeKeyWord["founded"]==="true"){
293
						if($delStrBeforeKeyWord["founded"]==="true"){
294
													
294
 
295
							#儲存密碼
295
							#儲存密碼
296
							$this->connInfo[$from->resourceId]["login"]["pw"]=$delStrBeforeKeyWord["content"];
296
							$this->connInfo[$from->resourceId]["login"]["pw"]=$delStrBeforeKeyWord["content"];
297
							
297
 
298
							}#if end
298
							}#if end
299
							
299
 
300
						#反之代表格式錯誤
300
						#反之代表格式錯誤
301
						else{
301
						else{
302
													
302
 
303
							#提示輸入密碼
303
							#提示輸入密碼
304
							$from->send(json_encode("Please input your password! ex:password:ws1"));
304
							$from->send(json_encode("Please input your password! ex:password:ws1"));
305
							
305
 
306
							#結束程式
306
							#結束程式
307
							return true;
307
							return true;
308
												
308
 
309
							}#else end
309
							}#else end
310
						
310
 
311
						}#if end
311
						}#if end
312
						
312
 
313
					#反之代表格式錯誤
313
					#反之代表格式錯誤
314
					else{
314
					else{
315
						
315
 
316
						#提示輸入密碼		
316
						#提示輸入密碼
317
						$from->send(json_encode("Please input your password! ex:password:ws1"));
317
						$from->send(json_encode("Please input your password! ex:password:ws1"));
318
						
318
 
319
						#結束程式
319
						#結束程式
320
						return true;
320
						return true;
321
						
321
 
322
						}#else end
322
						}#else end
323
					
323
 
324
					}#if end
324
					}#if end
325
				
325
 
326
				}#if end
326
				}#if end
327
			
327
 
328
			#如果已經輸入帳號了
328
			#如果已經輸入帳號了
329
			if($this->connInfo[$from->resourceId]["login"]["ac"]!=""){
329
			if($this->connInfo[$from->resourceId]["login"]["ac"]!=""){
330
				
330
 
331
				#設置可以進行驗證
331
				#設置可以進行驗證
332
				$startAuth=true;
332
				$startAuth=true;
333
				
333
 
334
				#另存帳號
334
				#另存帳號
335
				$ac=$this->connInfo[$from->resourceId]["login"]["ac"];
335
				$ac=$this->connInfo[$from->resourceId]["login"]["ac"];
336
				
336
 
337
				#初始化密碼
337
				#初始化密碼
338
				$pw="";
338
				$pw="";
339
				
339
 
340
				#如果有設置密碼欄位
340
				#如果有設置密碼欄位
341
				if(self::$passwordCol!==""){
341
				if(self::$passwordCol!==""){
342
					
342
 
343
					#如果client已經輸入密碼了
343
					#如果client已經輸入密碼了
344
					if($this->connInfo[$from->resourceId]["login"]["pw"]!=""){
344
					if($this->connInfo[$from->resourceId]["login"]["pw"]!=""){
345
						
345
 
346
						#另存密碼
346
						#另存密碼
347
						$pw=md5($this->connInfo[$from->resourceId]["login"]["pw"]);
347
						$pw=md5($this->connInfo[$from->resourceId]["login"]["pw"]);
348
						
348
 
349
						}#if end
349
						}#if end
350
						
350
 
351
					#反之
351
					#反之
352
					else{
352
					else{
353
						
353
 
354
						#設置尚不能進行認證
354
						#設置尚不能進行認證
355
						$startAuth=false;
355
						$startAuth=false;
356
						
356
 
357
						}#else end
357
						}#else end
358
										
358
 
359
					}#if end
359
					}#if end
360
				
360
 
361
				#如果尚不能進行驗證
361
				#如果尚不能進行驗證
362
				if(!$startAuth){
362
				if(!$startAuth){
363
					
363
 
364
					#結束程式
364
					#結束程式
365
					return true;
365
					return true;
366
					
366
 
367
					}#if end
367
					}#if end
368
				
368
 
369
				#檢查有無符合的帳戶密碼
369
				#檢查有無符合的帳戶密碼
370
				#涵式說明:
370
				#涵式說明:
371
				#一次取得資料庫、表的資料
371
				#一次取得資料庫、表的資料
372
				#回傳的結果
372
				#回傳的結果
373
				#$result["status"],執行結果"true"為成功;"false"為執行失敗。
373
				#$result["status"],執行結果"true"為成功;"false"為執行失敗。
Line 389... Line 389...
389
				$conf["db::fastGetDbData"]["columnYouWant"]=array("id");#你想要的欄位!,若設為「array("*")」則代表全部欄位.
389
				$conf["db::fastGetDbData"]["columnYouWant"]=array("id");#你想要的欄位!,若設為「array("*")」則代表全部欄位.
390
				#可省略的參數:
390
				#可省略的參數:
391
				$conf["db::fastGetDbData"]["dbPassword"]=self::$dbPassword;#爲要存取dbServer的密碼
391
				$conf["db::fastGetDbData"]["dbPassword"]=self::$dbPassword;#爲要存取dbServer的密碼
392
				$conf["db::fastGetDbData"]["WhereColumnName"]=array(self::$accountCol);#用於判斷語句的欄位項目陣列。
392
				$conf["db::fastGetDbData"]["WhereColumnName"]=array(self::$accountCol);#用於判斷語句的欄位項目陣列。
393
				$conf["db::fastGetDbData"]["WhereColumnValue"]=array($ac);#用於判斷語句的欄位數值陣列,若與LIKE搭配,則可以在關鍵自字串的左右名加上「%」符號,這樣就可以搜尋具有該字串的內容。
393
				$conf["db::fastGetDbData"]["WhereColumnValue"]=array($ac);#用於判斷語句的欄位數值陣列,若與LIKE搭配,則可以在關鍵自字串的左右名加上「%」符號,這樣就可以搜尋具有該字串的內容。
394
				
394
 
395
				#如果有輸入密碼
395
				#如果有輸入密碼
396
				if($pw!==""){
396
				if($pw!==""){
397
					
397
 
398
					#新增要判斷 self::$password 欄位,是否有於 $pw
398
					#新增要判斷 self::$password 欄位,是否有於 $pw
399
					$conf["db::fastGetDbData"]["WhereColumnName"][]=self::$passwordCol;
399
					$conf["db::fastGetDbData"]["WhereColumnName"][]=self::$passwordCol;
400
					$conf["db::fastGetDbData"]["WhereColumnValue"][]=$pw;
400
					$conf["db::fastGetDbData"]["WhereColumnValue"][]=$pw;
401
					
401
 
402
					}#if end
402
					}#if end
403
				
403
 
404
				#$conf["WhereColumnCombine"]=array("");#用於判斷語句當中需要()起來的判斷式,須爲陣列值,"s"代表「(」,"e"代表「)」 ,若無則須設爲""。
404
				#$conf["WhereColumnCombine"]=array("");#用於判斷語句當中需要()起來的判斷式,須爲陣列值,"s"代表「(」,"e"代表「)」 ,若無則須設爲""。
405
				#$conf["WhereColumnOperator"]=array("");#用於判斷語句的比較符號陣列,可以用的符號有「"="、"!="、">"、"<"、"LIKE"、"NOT LIKE"」,預設都爲「=」。
405
				#$conf["WhereColumnOperator"]=array("");#用於判斷語句的比較符號陣列,可以用的符號有「"="、"!="、">"、"<"、"LIKE"、"NOT LIKE"」,預設都爲「=」。
406
				#$conf["WhereColumnAndOr"]=array("");#用於判斷語句條件之間成立的條件是AND還是OR,須爲陣列值。其數量應爲要判斷的欄位數量減一。
406
				#$conf["WhereColumnAndOr"]=array("");#用於判斷語句條件之間成立的條件是AND還是OR,須爲陣列值。其數量應爲要判斷的欄位數量減一。
407
				#$conf["orderItem"]="";#爲排序的項目依據,若要用隨機抽樣,可以用"rand()",可省略。
407
				#$conf["orderItem"]="";#爲排序的項目依據,若要用隨機抽樣,可以用"rand()",可省略。
408
				#$conf["ascORdesc"]="";#爲要低增還是遞減排序,asc爲遞增;desc爲遞減。
408
				#$conf["ascORdesc"]="";#爲要低增還是遞減排序,asc爲遞增;desc爲遞減。
Line 411... Line 411...
411
				#$conf["groupBy"]=array("");#爲要以哪幾個欄爲作爲分羣的依據(欄位相同的數值僅會取出一筆)。
411
				#$conf["groupBy"]=array("");#爲要以哪幾個欄爲作爲分羣的依據(欄位相同的數值僅會取出一筆)。
412
				#備註:
412
				#備註:
413
				#建議在查詢資料前,能夠檢查是否每個欄位都存在.
413
				#建議在查詢資料前,能夠檢查是否每個欄位都存在.
414
				$fastGetDbData=db::fastGetDbData($conf["db::fastGetDbData"]);
414
				$fastGetDbData=db::fastGetDbData($conf["db::fastGetDbData"]);
415
				unset($conf["db::fastGetDbData"]);
415
				unset($conf["db::fastGetDbData"]);
416
				
416
 
417
				#如果取得資料失敗
417
				#如果取得資料失敗
418
				if($fastGetDbData["status"]==="false"){
418
				if($fastGetDbData["status"]==="false"){
419
					
419
 
420
					#設置執行失敗
420
					#設置執行失敗
421
					$result["status"]="false";
421
					$result["status"]="false";
422
				
422
 
423
					#設置執行錯誤訊息
423
					#設置執行錯誤訊息
424
					$result["error"]=$fastGetDbData;
424
					$result["error"]=$fastGetDbData;
425
					
425
 
426
					#印出結果
426
					#印出結果
427
					#var_dump($result);
427
					#var_dump($result);
428
					
428
 
429
					#提示server有資料庫錯誤
429
					#提示server有資料庫錯誤
430
					echo "dbError!";
430
					echo "dbError!";
431
					
431
 
432
					#結束程式
432
					#結束程式
433
					exit;
433
					exit;
434
					
434
 
435
					}#if end
435
					}#if end
436
					
436
 
437
				#如果沒有一筆資料
437
				#如果沒有一筆資料
438
				if($fastGetDbData["dataCount"]!==1){
438
				if($fastGetDbData["dataCount"]!==1){
439
					
439
 
440
					#代表登入失敗
440
					#代表登入失敗
441
					
441
 
442
					#清空登入用的帳戶
442
					#清空登入用的帳戶
443
					$this->connInfo[$from->resourceId]["login"]["ac"]="";
443
					$this->connInfo[$from->resourceId]["login"]["ac"]="";
444
				
444
 
445
					#清空登入用的密碼
445
					#清空登入用的密碼
446
					$this->connInfo[$from->resourceId]["login"]["pw"]="";
446
					$this->connInfo[$from->resourceId]["login"]["pw"]="";
447
					
447
 
448
					#提示登入失敗					
448
					#提示登入失敗
449
					$from->send(json_encode("Login failed, please input account. ex:account:ws1"));
449
					$from->send(json_encode("Login failed, please input account. ex:account:ws1"));
450
					
450
 
451
					#執行到這即可
451
					#執行到這即可
452
					return true;
452
					return true;
453
					
453
 
454
					}#if end
454
					}#if end
455
					
455
 
456
				#反之代表帳戶資訊正確
456
				#反之代表帳戶資訊正確
457
				else{
457
				else{
458
										
458
 
459
					#設置 $myUserId
459
					#設置 $myUserId
460
					$myUserId=$this->connInfo[$from->resourceId]["userId"]=$fastGetDbData["dataContent"]["id"][0];
460
					$myUserId=$this->connInfo[$from->resourceId]["userId"]=$fastGetDbData["dataContent"]["id"][0];
461
					
461
 
462
					#設置 $myId
462
					#設置 $myId
463
					$myId=$from->resourceId;
463
					$myId=$from->resourceId;
464
					
464
 
465
					#提示登入成功
465
					#提示登入成功
466
					$from->send(json_encode("Login successfully!"));
466
					$from->send(json_encode("Login successfully!"));
467
					
467
 
468
					#檢查是否有訊息是要給自己的
468
					#檢查是否有訊息是要給自己的
469
					foreach($this->unSendMsg as $index => $historyMsg){
469
					foreach($this->unSendMsg as $index => $historyMsg){
470
						
470
 
471
						#如果有要給自己的訊息
471
						#如果有要給自己的訊息
472
						if($myUserId===$historyMsg["toUserId"]){
472
						if($myUserId===$historyMsg["toUserId"]){
473
							
473
 
474
							#接收別人給的留言
474
							#接收別人給的留言
475
							$from->send(json_encode($historyMsg["msg"]));
475
							$from->send(json_encode($historyMsg["msg"]));
476
							
476
 
477
							#有幾個人在線上就執行幾次
477
							#有幾個人在線上就執行幾次
478
							foreach($this->connInfo as $toId => $cInfo){
478
							foreach($this->connInfo as $toId => $cInfo){
479
								
479
 
480
								#如果是自己
480
								#如果是自己
481
								if($myId===$toId ){
481
								if($myId===$toId ){
482
									
482
 
483
									#跳過
483
									#跳過
484
									continue;
484
									continue;
485
									
485
 
486
									}#if end
486
									}#if end
487
								
487
 
488
								#如果留言的對象有在線上
488
								#如果留言的對象有在線上
489
								if($historyMsg["fromUserId"]===$cInfo["userId"]){
489
								if($historyMsg["fromUserId"]===$cInfo["userId"]){
490
									
490
 
491
									#檢查對方是否已經在自己的通話清單裡面了
491
									#檢查對方是否已經在自己的通話清單裡面了
492
									
492
 
493
									#初始化要將對方加到自己的對話清單裡面
493
									#初始化要將對方加到自己的對話清單裡面
494
									$add=false;
494
									$add=false;
495
									
495
 
496
									#根據自己的每個通話對象
496
									#根據自己的每個通話對象
497
									foreach($this->connInfo[$myId]["talkTo"] as $myToIndex=>$myToCinfo ){
497
									foreach($this->connInfo[$myId]["talkTo"] as $myToIndex=>$myToCinfo ){
498
										
498
 
499
										#如果對對方已經存在於自己的對話清單裡面了
499
										#如果對對方已經存在於自己的對話清單裡面了
500
										if($myToCinfo["userId"]===$historyMsg["fromUserId"] && $myId===$historyMsg["fromId"]){
500
										if($myToCinfo["userId"]===$historyMsg["fromUserId"] && $myId===$historyMsg["fromId"]){
501
											
501
 
502
											#設置不要將對方加到自己的對話清單裡面
502
											#設置不要將對方加到自己的對話清單裡面
503
											$add=false;
503
											$add=false;
504
											
504
 
505
											#跳出foreach
505
											#跳出foreach
506
											break;
506
											break;
507
											
507
 
508
											}#if end
508
											}#if end
509
										
509
 
510
										}#foreach end
510
										}#foreach end
511
									
511
 
512
									#如果要將對方加到自己的對話清單裡面
512
									#如果要將對方加到自己的對話清單裡面
513
									if($add){
513
									if($add){
514
										
514
 
515
										#將對方加到自己的talkTo清單裡面
515
										#將對方加到自己的talkTo清單裡面
516
										$this->connInfo[$myId]["talkTo"][]=array("id"=>$toId,"userId"=>$historyMsg["fromUserId"]);
516
										$this->connInfo[$myId]["talkTo"][]=array("id"=>$toId,"userId"=>$historyMsg["fromUserId"]);
517
										
517
 
518
										}#if end
518
										}#if end
519
																										
-
 
-
 
519
 
520
									#檢查自己有沒有在對方talkTo清單裡面
520
									#檢查自己有沒有在對方talkTo清單裡面
521
																		
-
 
-
 
521
 
522
									#假設對方需要加自己到對話清單裡面
522
									#假設對方需要加自己到對話清單裡面
523
									$add=true;
523
									$add=true;
524
									
524
 
525
									#檢查通話對象能否跟自己對話
525
									#檢查通話對象能否跟自己對話
526
									
526
 
527
									#依據對方每個通話的對象
527
									#依據對方每個通話的對象
528
									foreach($this->connInfo[$toId]["talkTo"] as $talkToIndex=>$talkToCinfo){
528
									foreach($this->connInfo[$toId]["talkTo"] as $talkToIndex=>$talkToCinfo){
529
										
529
 
530
										#如果自己的userId已經在對方的對話清單裡面了
530
										#如果自己的userId已經在對方的對話清單裡面了
531
										if($myUserId===$talkToCinfo["userId"]){
531
										if($myUserId===$talkToCinfo["userId"]){
532
											
532
 
533
											#如果對方對話userId對應的id不在線上
533
											#如果對方對話userId對應的id不在線上
534
											if(!isset($this->connInfo[$toId])){
534
											if(!isset($this->connInfo[$toId])){
535
												
535
 
536
												#檢查對方對話清單是否已經含有自己的id與userId了
536
												#檢查對方對話清單是否已經含有自己的id與userId了
537
												
537
 
538
												#假設對方對話清單沒有自己的id與userId
538
												#假設對方對話清單沒有自己的id與userId
539
												$update=true;
539
												$update=true;
540
												
540
 
541
												#依據對方每個通話的對象
541
												#依據對方每個通話的對象
542
												foreach($this->connInfo[$toId]["talkTo"] as $ceToIndex=>$ceToCinfo){
542
												foreach($this->connInfo[$toId]["talkTo"] as $ceToIndex=>$ceToCinfo){
543
													
543
 
544
													#如果對方對話清單已經含有自己的id與userId了												
544
													#如果對方對話清單已經含有自己的id與userId了
545
													if($ceToCinfo["id"]===$myId && $ceToCinfo["userId"]===$myUserId){
545
													if($ceToCinfo["id"]===$myId && $ceToCinfo["userId"]===$myUserId){
546
														
546
 
547
														#設置不需要更新
547
														#設置不需要更新
548
														$update=false;
548
														$update=false;
549
														
549
 
550
														#設置對方不需要加自己到對話清單裡面
550
														#設置對方不需要加自己到對話清單裡面
551
														$add=false;
551
														$add=false;
552
														
552
 
553
														}#if end
553
														}#if end
554
													
554
 
555
													}#foreach end
555
													}#foreach end
556
												
556
 
557
												#如果對方對話清單沒有自己的id與userId
557
												#如果對方對話清單沒有自己的id與userId
558
												if($update){
558
												if($update){
559
													
559
 
560
													#將自己在對方對話清單裡面的id改成現在的id
560
													#將自己在對方對話清單裡面的id改成現在的id
561
													$this->connInfo[$toId]["talkTo"][$talkToIndex]["id"]=$myId;
561
													$this->connInfo[$toId]["talkTo"][$talkToIndex]["id"]=$myId;
562
																										
-
 
-
 
562
 
563
													}#if end
563
													}#if end
564
													
564
 
565
												#反之對方對話清單已經有自己的id與userId
565
												#反之對方對話清單已經有自己的id與userId
566
												else{
566
												else{
567
													
567
 
568
													#移除對方該已經斷線的對話id
568
													#移除對方該已經斷線的對話id
569
													unset($this->connInfo[$toId]["talkTo"][$talkToIndex]);
569
													unset($this->connInfo[$toId]["talkTo"][$talkToIndex]);
570
													
570
 
571
													}#else end
571
													}#else end
572
												
572
 
573
												}#if end
573
												}#if end
574
											
574
 
575
											}#if end
575
											}#if end
576
										
576
 
577
										}#foreach end
577
										}#foreach end
578
										
578
 
579
									#如果對方需要加自己到對話清單裡面
579
									#如果對方需要加自己到對話清單裡面
580
									if($add){
580
									if($add){
581
										
581
 
582
										#讓通話對象也可以傳送訊息回來
582
										#讓通話對象也可以傳送訊息回來
583
										$this->connInfo[$toId]["talkTo"][]=array("id"=>$myId,"userId"=>$myUserId);
583
										$this->connInfo[$toId]["talkTo"][]=array("id"=>$myId,"userId"=>$myUserId);
584
										
584
 
585
										}#if end
585
										}#if end
586
									
586
 
587
									}#if end
587
									}#if end
588
								
588
 
589
								}#foreach end
589
								}#foreach end
590
							
590
 
591
							#移除留言紀錄
591
							#移除留言紀錄
592
							unset($this->unSendMsg[$index]);
592
							unset($this->unSendMsg[$index]);
593
							
593
 
594
							}#if end
594
							}#if end
595
						
595
 
596
						}#foreach end
596
						}#foreach end
597
						
597
 
598
					#檢查線上是否有人想跟我對話
598
					#檢查線上是否有人想跟我對話
599
					
599
 
600
					#有幾個人在線上就執行幾次
600
					#有幾個人在線上就執行幾次
601
					foreach($this->connInfo as $toId => $cInfo){
601
					foreach($this->connInfo as $toId => $cInfo){
602
						
602
 
603
						#如果是自己
603
						#如果是自己
604
						if($myId===$toId){
604
						if($myId===$toId){
605
							
605
 
606
							#跳過
606
							#跳過
607
							continue;
607
							continue;
608
							
608
 
609
							}#if end
609
							}#if end
610
						
610
 
611
						#該cliet目前再跟幾個人通話,就執行幾次
611
						#該cliet目前再跟幾個人通話,就執行幾次
612
						foreach($cInfo["talkTo"] as $tIndex=>$tInfo){
612
						foreach($cInfo["talkTo"] as $tIndex=>$tInfo){
613
							
613
 
614
							#如果自己的userId在對方的對話清單裡面
614
							#如果自己的userId在對方的對話清單裡面
615
							if($myUserId===$tInfo["userId"]){
615
							if($myUserId===$tInfo["userId"]){
616
								
616
 
617
								#假設自己需要被對方加到對話清單裡面
617
								#假設自己需要被對方加到對話清單裡面
618
								$add=true;
618
								$add=true;
619
								
619
 
620
								#檢查自己是否已經在對方的對話清單裡面
620
								#檢查自己是否已經在對方的對話清單裡面
621
								
621
 
622
								#對方有幾個通話對象就執行幾次
622
								#對方有幾個通話對象就執行幾次
623
								foreach($this->connInfo[$toId]["talkTo"] as $toIndex=>$toCinfo){
623
								foreach($this->connInfo[$toId]["talkTo"] as $toIndex=>$toCinfo){
624
									
624
 
625
									#如果自己已經在對方的對話清單裡面
625
									#如果自己已經在對方的對話清單裡面
626
									if($myUserId===$toCinfo["userId"] && $myId===$toCinfo["id"]){
626
									if($myUserId===$toCinfo["userId"] && $myId===$toCinfo["id"]){
627
										
627
 
628
										#設置自己不需要被對方加到對話清單裡面
628
										#設置自己不需要被對方加到對話清單裡面
629
										$add=false;
629
										$add=false;
630
										
630
 
631
										#跳出迴圈
631
										#跳出迴圈
632
										break;
632
										break;
633
										
633
 
634
										}#if end
634
										}#if end
635
									
635
 
636
									}#foreach end
636
									}#foreach end
637
								
637
 
638
								#另存針對自己userId的連線id
638
								#另存針對自己userId的連線id
639
								$oneIdOfMyUserid=$this->connInfo[$toId]["talkTo"][$tIndex]["id"];
639
								$oneIdOfMyUserid=$this->connInfo[$toId]["talkTo"][$tIndex]["id"];
640
								
640
 
641
								#如果對方對話清單裡面的用戶id不在線
641
								#如果對方對話清單裡面的用戶id不在線
642
								if(!isset($this->connInfo[$oneIdOfMyUserid])){
642
								if(!isset($this->connInfo[$oneIdOfMyUserid])){
643
									
643
 
644
									#假設要將自己在對方對話清單裡面的id改成現在的id
644
									#假設要將自己在對方對話清單裡面的id改成現在的id
645
									$update=true;
645
									$update=true;
646
									
646
 
647
									#自己不需要被對方加到對話清單裡面
647
									#自己不需要被對方加到對話清單裡面
648
									$add=false;
648
									$add=false;
649
									
649
 
650
									#檢查自己的id是否已經在對方的對話id裡面
650
									#檢查自己的id是否已經在對方的對話id裡面
651
									foreach($this->connInfo[$toId]["talkTo"] as $ceIndex=>$ceInfo){
651
									foreach($this->connInfo[$toId]["talkTo"] as $ceIndex=>$ceInfo){
652
										
652
 
653
										#如果自己的id已經在對方的對話清單裡面
653
										#如果自己的id已經在對方的對話清單裡面
654
										if($myId===$ceInfo["id"]){
654
										if($myId===$ceInfo["id"]){
655
											
655
 
656
											#設置不要將自己在對方對話清單裡面的id改成現在的id(可以移除該對話id)
656
											#設置不要將自己在對方對話清單裡面的id改成現在的id(可以移除該對話id)
657
											$update=false;
657
											$update=false;
658
											
658
 
659
											#跳出迴圈
659
											#跳出迴圈
660
											break;
660
											break;
661
											
661
 
662
											}#if end
662
											}#if end
663
										
663
 
664
										}#foreach end
664
										}#foreach end
665
									
665
 
666
									#如果要將自己在對方對話清單裡面的id改成現在的id
666
									#如果要將自己在對方對話清單裡面的id改成現在的id
667
									if($update){
667
									if($update){
668
										
668
 
669
										#將自己在對方對話清單裡面的id改成現在的id
669
										#將自己在對方對話清單裡面的id改成現在的id
670
										$this->connInfo[$toId]["talkTo"][$tIndex]["id"]=$myId;
670
										$this->connInfo[$toId]["talkTo"][$tIndex]["id"]=$myId;
671
										
671
 
672
										}#if end
672
										}#if end
673
										
673
 
674
									#反之不要將自己在對方對話清單裡面的id改成現在的id(可以移除該對話id)
674
									#反之不要將自己在對方對話清單裡面的id改成現在的id(可以移除該對話id)
675
									else{
675
									else{
676
										
676
 
677
										#移除該對話id
677
										#移除該對話id
678
										unset($this->connInfo[$toId]["talkTo"][$tIndex]);
678
										unset($this->connInfo[$toId]["talkTo"][$tIndex]);
679
										
679
 
680
										}#else end
680
										}#else end
681
									
681
 
682
									}#if end	
682
									}#if end
683
									
683
 
684
								#如果要將自己加到對方的的通話清單裡面
684
								#如果要將自己加到對方的的通話清單裡面
685
								if($add){
685
								if($add){
686
									
686
 
687
									#將自己加到對方的的通話清單裡面
687
									#將自己加到對方的的通話清單裡面
688
									$this->connInfo[$toId]["talkTo"][]=array("id"=>$myId,"userId"=>$myUserId);			
688
									$this->connInfo[$toId]["talkTo"][]=array("id"=>$myId,"userId"=>$myUserId);
689
									
689
 
690
									}#if end								
690
									}#if end
691
								
691
 
692
								#檢查對方是否已經在自己的對話清單裡面
692
								#檢查對方是否已經在自己的對話清單裡面
693
																
-
 
-
 
693
 
694
								#設置要新增對方到自己的對話清單裡面.
694
								#設置要新增對方到自己的對話清單裡面.
695
								$add=true;
695
								$add=true;
696
								
696
 
697
								#自己有幾個對話對象就執行幾次
697
								#自己有幾個對話對象就執行幾次
698
								foreach($this->connInfo[$myId]["talkTo"] as $mtIndex=>$mtInfo){
698
								foreach($this->connInfo[$myId]["talkTo"] as $mtIndex=>$mtInfo){
699
									
699
 
700
									#對方的userId與id若在自己的對話清單裡面
700
									#對方的userId與id若在自己的對話清單裡面
701
									if($mtInfo["userId"]===$cInfo["userId"] && $mtInfo["id"]===$toId){
701
									if($mtInfo["userId"]===$cInfo["userId"] && $mtInfo["id"]===$toId){
702
										
702
 
703
										#設置不需要新增對方到自己的對話清單裡面
703
										#設置不需要新增對方到自己的對話清單裡面
704
										$add=false;
704
										$add=false;
705
										
705
 
706
										#跳出 foreach
706
										#跳出 foreach
707
										break;
707
										break;
708
										
708
 
709
										}#if end
709
										}#if end
710
									
710
 
711
									}#foreache end
711
									}#foreache end
712
								
712
 
713
								#如果要新增對方到自己的對話清單裡面.
713
								#如果要新增對方到自己的對話清單裡面.
714
								if($add){
714
								if($add){
715
									
715
 
716
									#將對方加到自己的對話清單裡面
716
									#將對方加到自己的對話清單裡面
717
									$this->connInfo[$myId]["talkTo"][]=array("id"=>$toId,"userId"=>$cInfo["userId"]);
717
									$this->connInfo[$myId]["talkTo"][]=array("id"=>$toId,"userId"=>$cInfo["userId"]);
718
									
718
 
719
									}#if end
719
									}#if end
720
								
720
 
721
								}#if end
721
								}#if end
722
							
722
 
723
							}#foreach end
723
							}#foreach end
724
						
724
 
725
						}#foreach end
725
						}#foreach end
726
					
726
 
727
					#結束認證成功後的流程
727
					#結束認證成功後的流程
728
					return true;
728
					return true;
729
					
729
 
730
					}#else end
730
					}#else end
731
					
731
 
732
				}#else end
732
				}#else end
733
				
733
 
734
			#結束認證流程
734
			#結束認證流程
735
			return true;	
735
			return true;
736
			
736
 
737
			}#if end
737
			}#if end
738
		
738
 
739
		#如果收到 "id?"
739
		#如果收到 "id?"
740
		else if($msg==="id?"){
740
		else if($msg==="id?"){
741
			
741
 
742
			#傳他人的id給client
742
			#傳他人的id給client
743
			$from->send(json_encode($from->resourceId));
743
			$from->send(json_encode($from->resourceId));
744
			
744
 
745
			#回傳成功
745
			#回傳成功
746
			return true;
746
			return true;
747
			
747
 
748
			}#if end
748
			}#if end
749
			
749
 
750
		#如果收到 "ids?"
750
		#如果收到 "ids?"
751
		else if($msg==="ids?"){
751
		else if($msg==="ids?"){
752
			
752
 
753
			#初始化儲存其他人的id
753
			#初始化儲存其他人的id
754
			$idsArray=array();
754
			$idsArray=array();
755
			
755
 
756
			#針對所的client
756
			#針對所的client
757
			foreach ($this->clients as $client){
757
			foreach ($this->clients as $client){
758
			
758
 
759
				#排除自己
759
				#排除自己
760
				if($from !== $client){
760
				if($from !== $client){
761
					
761
 
762
					#取得其他人的id
762
					#取得其他人的id
763
					$idsArray[]=$client->resourceId;
763
					$idsArray[]=$client->resourceId;
764
					
764
 
765
					}#if end
765
					}#if end
766
				
766
 
767
				}#foreach end			
767
				}#foreach end
768
						
768
 
769
			#傳他人的id給client
769
			#傳他人的id給client
770
			$from->send(json_encode($idsArray));
770
			$from->send(json_encode($idsArray));
771
			
771
 
772
			#回傳成功
772
			#回傳成功
773
			return true;
773
			return true;
774
		
774
 
775
			}#if end
775
			}#if end
776
		
776
 
777
		#如果收到 "talkTo?"
777
		#如果收到 "talkTo?"
778
		else if($msg==="talkTo?"){
778
		else if($msg==="talkTo?"){
779
			
779
 
780
			#正在對話的對象id給client
780
			#正在對話的對象id給client
781
			$from->send(json_encode($this->connInfo[$from->resourceId]["talkTo"]));
781
			$from->send(json_encode($this->connInfo[$from->resourceId]["talkTo"]));
782
			
782
 
783
			#回傳成功
783
			#回傳成功
784
			return true;
784
			return true;
785
			
785
 
786
			}#if end
786
			}#if end
787
		
787
 
788
		#如果收到的$msg長度大於 "talkTo:"
788
		#如果收到的$msg長度大於 "talkTo:"
789
		if(strlen($msg)>strlen("talkTo:")){
789
		if(strlen($msg)>strlen("talkTo:")){
790
			
790
 
791
			#如果收到開頭為 "talkTo:"    
791
			#如果收到開頭為 "talkTo:"
792
			#涵式說明:
792
			#涵式說明:
793
			#取得符合特定字首與字尾的字串
793
			#取得符合特定字首與字尾的字串
794
			#回傳的結果:
794
			#回傳的結果:
795
			#$result["status"],若爲"true"則代表執行正常;若爲"false"則代表執行失敗。
795
			#$result["status"],若爲"true"則代表執行正常;若爲"false"則代表執行失敗。
796
			#$result["function"],當前執行的函數名稱.
796
			#$result["function"],當前執行的函數名稱.
Line 807... Line 807...
807
			#$conf["tailWord"]="";
807
			#$conf["tailWord"]="";
808
			#參考資料:
808
			#參考資料:
809
			#str_spilt(),可以將字串依照字母分割成一個個陣列字串。
809
			#str_spilt(),可以將字串依照字母分割成一個個陣列字串。
810
			$getMeetConditionsString=search::getMeetConditionsString($conf["search::getMeetConditionsString"]);
810
			$getMeetConditionsString=search::getMeetConditionsString($conf["search::getMeetConditionsString"]);
811
			unset($conf["search::getMeetConditionsString"]);
811
			unset($conf["search::getMeetConditionsString"]);
812
				
812
 
813
			#如果選找前置字串 "talkTo:" 失敗
813
			#如果選找前置字串 "talkTo:" 失敗
814
			if($getMeetConditionsString["status"]==="false"){
814
			if($getMeetConditionsString["status"]==="false"){
815
				
815
 
816
				#設置執行失敗
816
				#設置執行失敗
817
				$result["status"]="false";
817
				$result["status"]="false";
818
			
818
 
819
				#設置執行錯誤訊息
819
				#設置執行錯誤訊息
820
				$result["error"]=$getMeetConditionsString;
820
				$result["error"]=$getMeetConditionsString;
821
				
821
 
822
				#回傳結果
822
				#回傳結果
823
				return $result;
823
				return $result;
824
				
824
 
825
				}#if end
825
				}#if end
826
				
826
 
827
			#如果存在 "talkTo:" 前置字串
827
			#如果存在 "talkTo:" 前置字串
828
			if($getMeetConditionsString["founded"]==="true"){
828
			if($getMeetConditionsString["founded"]==="true"){
829
				
829
 
830
				#用 "talkTo:" 分割 $buf
830
				#用 "talkTo:" 分割 $buf
831
				#涵式說明:
831
				#涵式說明:
832
				#將固定格式的字串分開,並回傳分開的結果。
832
				#將固定格式的字串分開,並回傳分開的結果。
833
				#回傳結果:
833
				#回傳結果:
834
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
834
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 844... Line 844...
844
				#可省略參數:
844
				#可省略參數:
845
				#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
845
				#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
846
				$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
846
				$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
847
				$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
847
				$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
848
				unset($conf["stringProcess::spiltString"]);
848
				unset($conf["stringProcess::spiltString"]);
849
				
849
 
850
				#如果分割字串失敗
850
				#如果分割字串失敗
851
				if($spiltString["status"]==="false"){
851
				if($spiltString["status"]==="false"){
852
					
852
 
853
					#設置執行失敗
853
					#設置執行失敗
854
					$result["status"]="false";
854
					$result["status"]="false";
855
				
855
 
856
					#設置執行錯誤訊息
856
					#設置執行錯誤訊息
857
					$result["error"]=$spiltString;
857
					$result["error"]=$spiltString;
858
					
858
 
859
					#回傳結果
859
					#回傳結果
860
					return $result;
860
					return $result;
861
					
861
 
862
					}#if end
862
					}#if end
863
				
863
 
864
				#如果剛好分割出一筆資料
864
				#如果剛好分割出一筆資料
865
				if($spiltString["dataCounts"]===1){
865
				if($spiltString["dataCounts"]===1){
866
					
866
 
867
					#取得自己的id
867
					#取得自己的id
868
					$myId=$from->resourceId;
868
					$myId=$from->resourceId;
869
					
869
 
870
					#取得講話對象的id
870
					#取得講話對象的id
871
					$toId=$spiltString["dataArray"][0];
871
					$toId=$spiltString["dataArray"][0];
872
					
872
 
873
					#設置對象不存在的識別
873
					#設置對象不存在的識別
874
					$targetExist=false;
874
					$targetExist=false;
875
					
875
 
876
					#設置通話對象的key
876
					#設置通話對象的key
877
					$targetKey="";
877
					$targetKey="";
878
					
878
 
879
					#如果要對話的對象存在
879
					#如果要對話的對象存在
880
					if(isset($this->connInfo[$toId])){
880
					if(isset($this->connInfo[$toId])){
881
						
881
 
882
						#取得對象的userId
882
						#取得對象的userId
883
						$toUserId=$this->connInfo[$toId]["userId"];
883
						$toUserId=$this->connInfo[$toId]["userId"];
884
						
884
 
885
						#取得同userId的對象id們
885
						#取得同userId的對象id們
886
						
886
 
887
						#初始化同userId的對象id
887
						#初始化同userId的對象id
888
						$targetC=array();
888
						$targetC=array();
889
						
889
 
890
						#針對每個連線
890
						#針對每個連線
891
						foreach($this->connInfo as $eachIndex=>$eachInfo){
891
						foreach($this->connInfo as $eachIndex=>$eachInfo){
892
							
892
 
893
							#如果是自己
893
							#如果是自己
894
							if($eachIndex===$myId){
894
							if($eachIndex===$myId){
895
								
895
 
896
								#跳到下一輪
896
								#跳到下一輪
897
								continue;
897
								continue;
898
								
898
 
899
								}#if end
899
								}#if end
900
							
900
 
901
							#如果是同userId的對象
901
							#如果是同userId的對象
902
							if($eachInfo["userId"]===$toUserId){
902
							if($eachInfo["userId"]===$toUserId){
903
								
903
 
904
								#取出連線
904
								#取出連線
905
								$targetC[$eachIndex]=$this->connInfo[$eachIndex];
905
								$targetC[$eachIndex]=$this->connInfo[$eachIndex];
906
								
906
 
907
								}#if end
907
								}#if end
908
							
908
 
909
							}#foreach end
909
							}#foreach end
910
										
910
 
911
						#針對每個同userId的連線
911
						#針對每個同userId的連線
912
						foreach($targetC as $tcIndex=>$tcInfo){
912
						foreach($targetC as $tcIndex=>$tcInfo){
913
							
913
 
914
							#檢查自己的對話對象是否已經在清單裡面了
914
							#檢查自己的對話對象是否已經在清單裡面了
915
						
915
 
916
							#假設對象可以加到自己對話清單裡面
916
							#假設對象可以加到自己對話清單裡面
917
							$add=true;
917
							$add=true;
918
							
918
 
919
							#針對每個要講話的對象
919
							#針對每個要講話的對象
920
							foreach($this->connInfo[$myId]["talkTo"] as $index=>$cInfo){
920
							foreach($this->connInfo[$myId]["talkTo"] as $index=>$cInfo){
921
								
921
 
922
								#如果對話對象已經在清單裡面了
922
								#如果對話對象已經在清單裡面了
923
								if($this->connInfo[$tcIndex]["userId"]===$cInfo["userId"] && $tcIndex===$cInfo["id"]){
923
								if($this->connInfo[$tcIndex]["userId"]===$cInfo["userId"] && $tcIndex===$cInfo["id"]){
924
									
924
 
925
									#設置不要再加到清單裡面
925
									#設置不要再加到清單裡面
926
									$add=false;
926
									$add=false;
927
									
927
 
928
									#跳出foreach
928
									#跳出foreach
929
									break;
929
									break;
930
									
930
 
931
									}#if end								
931
									}#if end
932
								
932
 
933
								}#foreach end
933
								}#foreach end
934
								
934
 
935
							#如果要加入到對話清單裡面
935
							#如果要加入到對話清單裡面
936
							if($add){
936
							if($add){
937
								
937
 
938
								#增加自己的對話對象
938
								#增加自己的對話對象
939
								$this->connInfo[$myId]["talkTo"][]=array("id"=>$tcIndex,"userId"=>$this->connInfo[$tcIndex]["userId"]);
939
								$this->connInfo[$myId]["talkTo"][]=array("id"=>$tcIndex,"userId"=>$this->connInfo[$tcIndex]["userId"]);
940
															
940
 
941
								#假設對方可以加自己到對話清單裡面
941
								#假設對方可以加自己到對話清單裡面
942
								$add=true;
942
								$add=true;
943
								
943
 
944
								#檢查通話對象能否跟自己對話
944
								#檢查通話對象能否跟自己對話
945
								foreach($this->connInfo[$tcIndex]["talkTo"] as $index=>$cInfo){
945
								foreach($this->connInfo[$tcIndex]["talkTo"] as $index=>$cInfo){
946
									
946
 
947
									#如果對話對象已經在清單裡面了
947
									#如果對話對象已經在清單裡面了
948
									if($this->connInfo[$myId]["userId"]===$cInfo["userId"]){
948
									if($this->connInfo[$myId]["userId"]===$cInfo["userId"]){
949
										
949
 
950
										#設置不要再加到清單裡面
950
										#設置不要再加到清單裡面
951
										$add=false;
951
										$add=false;
952
										
952
 
953
										#跳出 foreach
953
										#跳出 foreach
954
										break;
954
										break;
955
										
955
 
956
										}#if end
956
										}#if end
957
									
957
 
958
									}#foreach end
958
									}#foreach end
959
									
959
 
960
								#如果對方可以加自己到對話清單裡面
960
								#如果對方可以加自己到對話清單裡面
961
								if($add){
961
								if($add){
962
									
962
 
963
									#讓通話對象也可以傳送訊息回來
963
									#讓通話對象也可以傳送訊息回來
964
									$this->connInfo[$tcIndex]["talkTo"][]=array("id"=>$myId,"userId"=>$this->connInfo[$myId]["userId"]);
964
									$this->connInfo[$tcIndex]["talkTo"][]=array("id"=>$myId,"userId"=>$this->connInfo[$myId]["userId"]);
965
									
965
 
966
									}#if end
966
									}#if end
967
									
967
 
968
								#設置要給自己看的訊息
968
								#設置要給自己看的訊息
969
								$from->send(json_encode("true"));
969
								$from->send(json_encode("true"));
970
								
970
 
971
								}#if end
971
								}#if end
972
								
972
 
973
							#反之
973
							#反之
974
							else{
974
							else{
975
								
975
 
976
								#設置要給自己看的訊息
976
								#設置要給自己看的訊息
977
								$from->send(json_encode("false"));
977
								$from->send(json_encode("false"));
978
								
978
 
979
								}#else end
979
								}#else end
980
							
980
 
981
							}#foreach end
981
							}#foreach end
982
						
982
 
983
						#回傳成功
983
						#回傳成功
984
						return true;
984
						return true;
985
						
985
 
986
						}#if end
986
						}#if end
987
					
987
 
988
					#反之不存在
988
					#反之不存在
989
					else{
989
					else{
990
						
990
 
991
						#設置要給自己看的訊息
991
						#設置要給自己看的訊息
992
						$from->send(json_encode("false"));
992
						$from->send(json_encode("false"));
993
						
993
 
994
						#回傳成功
994
						#回傳成功
995
						return true;
995
						return true;
996
						
996
 
997
						}#else end
997
						}#else end
998
					
998
 
999
					}#if end
999
					}#if end
1000
				
1000
 
1001
				}#if end
1001
				}#if end
1002
			
1002
 
1003
			}#if end
1003
			}#if end
1004
		
1004
 
1005
		#如果有要講話的對象
1005
		#如果有要講話的對象
1006
		if(count($this->connInfo[$from->resourceId]["talkTo"])>0){
1006
		if(count($this->connInfo[$from->resourceId]["talkTo"])>0){
1007
						
1007
 
1008
			#依據每個對話對象
1008
			#依據每個對話對象
1009
			foreach($this->connInfo[$from->resourceId]["talkTo"] as $index => $cInfo){
1009
			foreach($this->connInfo[$from->resourceId]["talkTo"] as $index => $cInfo){
1010
				
1010
 
1011
				#如果要講話的對象不存在(socket已經斷開)
1011
				#如果要講話的對象不存在(socket已經斷開)
1012
				if(!isset($this->connInfo[$cInfo["id"]])){
1012
				if(!isset($this->connInfo[$cInfo["id"]])){
1013
					
1013
 
1014
					#將訊息儲存起來,等對象上線後再把訊息傳過去.
1014
					#將訊息儲存起來,等對象上線後再把訊息傳過去.
1015
					$this->unSendMsg[]=array(
1015
					$this->unSendMsg[]=array(
1016
						"fromId"=>$from->resourceId,
1016
						"fromId"=>$from->resourceId,
1017
						"toId"=>"",
1017
						"toId"=>"",
1018
						"fromUserId"=>$this->connInfo[$from->resourceId]["userId"],
1018
						"fromUserId"=>$this->connInfo[$from->resourceId]["userId"],
1019
						"toUserId"=>$cInfo["userId"],
1019
						"toUserId"=>$cInfo["userId"],
1020
						"msg"=>$msg
1020
						"msg"=>$msg
1021
						);
1021
						);
1022
					
1022
 
1023
					}#if end
1023
					}#if end
1024
					
1024
 
1025
				#反之
1025
				#反之
1026
				else{
1026
				else{
1027
					
1027
 
1028
					#另存 ConnectionInterface 物件
1028
					#另存 ConnectionInterface 物件
1029
					$connObject=$this->connInfo[$cInfo["id"]]["conn"];
1029
					$connObject=$this->connInfo[$cInfo["id"]]["conn"];
1030
					
1030
 
1031
					#傳送訊息給對方
1031
					#傳送訊息給對方
1032
					$snedResult=$connObject->send(json_encode($msg));
1032
					$snedResult=$connObject->send(json_encode($msg));
1033
					
1033
 
1034
					}#else end
1034
					}#else end
1035
				
1035
 
1036
				}#foreach end
1036
				}#foreach end
1037
			
1037
 
1038
			}#if end
1038
			}#if end
1039
			
1039
 
1040
		#反之沒有講話的對象
1040
		#反之沒有講話的對象
1041
		else{
1041
		else{
1042
			
1042
 
1043
			#提示server訊息被拋棄
1043
			#提示server訊息被拋棄
1044
			echo "Message 「".$msg."」 will not be received by any one";
1044
			echo "Message 「".$msg."」 will not be received by any one";
1045
			
1045
 
1046
			#設置要給自己看的訊息
1046
			#設置要給自己看的訊息
1047
			$from->send(json_encode("false"));
1047
			$from->send(json_encode("false"));
1048
			
1048
 
1049
			}#else end
1049
			}#else end
1050
			
1050
 
1051
		}#function onMessage end
1051
		}#function onMessage end
1052
 
1052
 
1053
	#當clinet的連線斷掉前
1053
	#當clinet的連線斷掉前
1054
	public function onClose(ConnectionInterface $conn){
1054
	public function onClose(ConnectionInterface $conn){
1055
		
1055
 
1056
		// The connection is closed, remove it, as we can no longer send it messages
1056
		// The connection is closed, remove it, as we can no longer send it messages
1057
        	$this->clients->detach($conn);
1057
        	$this->clients->detach($conn);
1058
 
1058
 
1059
        	echo "Connection {$conn->resourceId} has disconnected\n";
1059
        	echo "Connection {$conn->resourceId} has disconnected\n";
1060
		
1060
 
1061
		#移除連線的資訊
1061
		#移除連線的資訊
1062
		unset($this->connInfo[$conn->resourceId]);
1062
		unset($this->connInfo[$conn->resourceId]);
1063
		
1063
 
1064
		}#function onClose end
1064
		}#function onClose end
1065
 
1065
 
1066
	#當出現錯誤時
1066
	#當出現錯誤時
1067
    	public function onError(ConnectionInterface $conn, \Exception $e){
1067
    	public function onError(ConnectionInterface $conn, \Exception $e){
1068
		
1068
 
1069
		echo "An error has occurred: {$e->getMessage()}\n";
1069
		echo "An error has occurred: {$e->getMessage()}\n";
1070
 
1070
 
1071
        	$conn->close();
1071
        	$conn->close();
1072
        
1072
 
1073
		}#fucntion onError end
1073
		}#fucntion onError end
1074
    
1074
 
1075
	}#class Chat end
1075
	}#class Chat end
1076
	
1076
 
1077
#用 ChatV2 類別來實作 MessageComponentInterface 界面,
1077
#用 ChatV2 類別來實作 MessageComponentInterface 界面,
1078
#改善了用戶網路斷掉但Server端不會將連線斷掉的問題
1078
#改善了用戶網路斷掉但Server端不會將連線斷掉的問題
1079
#改善了用戶端網路恢復後無法收到之前未收到的訊息的問題
1079
#改善了用戶端網路恢復後無法收到之前未收到的訊息的問題
1080
class ChatV2 implements MessageComponentInterface{
1080
class ChatV2 implements MessageComponentInterface{
1081
		
1081
 
1082
	#初始化儲存使用者資訊的陣列
1082
	#初始化儲存使用者資訊的陣列
1083
	private $connInfo=array();	
1083
	private $connInfo=array();
1084
	
1084
 
1085
	#初始化儲存未傳送出去的留言訊息
1085
	#初始化儲存未傳送出去的留言訊息
1086
	#"fromId",訊息來源的id
1086
	#"fromId",訊息來源的id
1087
	#"toId",訊息目的的id
1087
	#"toId",訊息目的的id
1088
	#"fromUserId",訊息來源的userId
1088
	#"fromUserId",訊息來源的userId
1089
	#"toUserId",訊息目標的userId
1089
	#"toUserId",訊息目標的userId
1090
	#"msg",傳送的訊息
1090
	#"msg",傳送的訊息
1091
	private $unSendMsg=array();
1091
	private $unSendMsg=array();
1092
	
1092
 
1093
	#初始化儲存待確認是否送達到對方的訊息
1093
	#初始化儲存待確認是否送達到對方的訊息
1094
	#$unConfirmMsg[$id],$id為訊息接收方,亦即收到訊息後,要跟server說有收到訊息者.
1094
	#$unConfirmMsg[$id],$id為訊息接收方,亦即收到訊息後,要跟server說有收到訊息者.
1095
	#"fromId",訊息來源的id
1095
	#"fromId",訊息來源的id
1096
	#"toId",訊息目的的id
1096
	#"toId",訊息目的的id
1097
	#"fromUserId",訊息來源的userId
1097
	#"fromUserId",訊息來源的userId
1098
	#"toUserId",訊息目標的userId
1098
	#"toUserId",訊息目標的userId
1099
	#"msg",傳送的訊息
1099
	#"msg",傳送的訊息
1100
	private $unConfirmMsg=array();
1100
	private $unConfirmMsg=array();
1101
	
1101
 
1102
	#初始化一開始尚未能夠連線到資料庫
1102
	#初始化一開始尚未能夠連線到資料庫
1103
	private static $dbTouchable=false;
1103
	private static $dbTouchable=false;
1104
		
1104
 
1105
	#初始化db的位置
1105
	#初始化db的位置
1106
	public static $dbAddress="localhost";	
1106
	public static $dbAddress="localhost";
1107
		
1107
 
1108
	#初始化連線db用的帳戶
1108
	#初始化連線db用的帳戶
1109
	public static $dbAccount="root";
1109
	public static $dbAccount="root";
1110
	
1110
 
1111
	#初始化連線的db名稱
1111
	#初始化連線的db名稱
1112
	public static $dbName="test";
1112
	public static $dbName="test";
1113
	
1113
 
1114
	#初始化連線db時用的密碼
1114
	#初始化連線db時用的密碼
1115
	public static $dbPassword="";
1115
	public static $dbPassword="";
1116
	
1116
 
1117
	#初始化連線db的哪個資料表
1117
	#初始化連線db的哪個資料表
1118
	public static $memberTableName="member";
1118
	public static $memberTableName="member";
1119
		
1119
 
1120
	#初始化登入時用來驗證帳號的資料表欄位名稱
1120
	#初始化登入時用來驗證帳號的資料表欄位名稱
1121
	public static $accountCol="account";
1121
	public static $accountCol="account";
1122
	
1122
 
1123
	#初始化登入時用來驗證密碼的資料表欄位名稱,設為""代表不用密碼來認證
1123
	#初始化登入時用來驗證密碼的資料表欄位名稱,設為""代表不用密碼來認證
1124
	public static $passwordCol="password";
1124
	public static $passwordCol="password";
1125
		
1125
 
1126
	#建構子
1126
	#建構子
1127
	public function __construct(){
1127
	public function __construct(){
1128
		
1128
 
1129
		#如果尚未可以上資料庫
1129
		#如果尚未可以上資料庫
1130
		if(!(self::$dbTouchable)){
1130
		if(!(self::$dbTouchable)){
1131
			
1131
 
1132
			#嘗試連線到目標資料庫.資料表
1132
			#嘗試連線到目標資料庫.資料表
1133
			#涵式說明:
1133
			#涵式說明:
1134
			#一次取得資料庫、表的資料
1134
			#一次取得資料庫、表的資料
1135
			#回傳的結果
1135
			#回傳的結果
1136
			#$result["status"],執行結果"true"為成功;"false"為執行失敗。
1136
			#$result["status"],執行結果"true"為成功;"false"為執行失敗。
Line 1148... Line 1148...
1148
			$conf["db::fastGetDbData"]["dbAddress"]=self::$dbAddress;#爲dbServer的位置。
1148
			$conf["db::fastGetDbData"]["dbAddress"]=self::$dbAddress;#爲dbServer的位置。
1149
			$conf["db::fastGetDbData"]["dbAccount"]=self::$dbAccount;#爲登入dbServer的帳號。
1149
			$conf["db::fastGetDbData"]["dbAccount"]=self::$dbAccount;#爲登入dbServer的帳號。
1150
			$conf["db::fastGetDbData"]["dbName"]=self::$dbName;#爲要存取的資料庫名稱
1150
			$conf["db::fastGetDbData"]["dbName"]=self::$dbName;#爲要存取的資料庫名稱
1151
			$conf["db::fastGetDbData"]["tableName"]=self::$memberTableName;#爲要存取的資料表名稱
1151
			$conf["db::fastGetDbData"]["tableName"]=self::$memberTableName;#爲要存取的資料表名稱
1152
			$conf["db::fastGetDbData"]["columnYouWant"]=array("id",self::$accountCol);#你想要的欄位!,若設為「array("*")」則代表全部欄位.
1152
			$conf["db::fastGetDbData"]["columnYouWant"]=array("id",self::$accountCol);#你想要的欄位!,若設為「array("*")」則代表全部欄位.
1153
			
1153
 
1154
			#如果 $passwordCol 不為空
1154
			#如果 $passwordCol 不為空
1155
			if(self::$passwordCol!=""){
1155
			if(self::$passwordCol!=""){
1156
				
1156
 
1157
				#加上密碼欄位
1157
				#加上密碼欄位
1158
				$conf["db::fastGetDbData"]["columnYouWant"][]=self::$passwordCol;
1158
				$conf["db::fastGetDbData"]["columnYouWant"][]=self::$passwordCol;
1159
				
1159
 
1160
				}#if end
1160
				}#if end
1161
			
1161
 
1162
			#可省略的參數:
1162
			#可省略的參數:
1163
			$conf["db::fastGetDbData"]["dbPassword"]=self::$dbPassword;#爲要存取dbServer的密碼
1163
			$conf["db::fastGetDbData"]["dbPassword"]=self::$dbPassword;#爲要存取dbServer的密碼
1164
			#$conf["db::fastGetDbData"]["WhereColumnName"]=array("account","password");#用於判斷語句的欄位項目陣列。
1164
			#$conf["db::fastGetDbData"]["WhereColumnName"]=array("account","password");#用於判斷語句的欄位項目陣列。
1165
			#$conf["db::fastGetDbData"]["WhereColumnValue"]=array($ac,$pw);#用於判斷語句的欄位數值陣列,若與LIKE搭配,則可以在關鍵自字串的左右名加上「%」符號,這樣就可以搜尋具有該字串的內容。
1165
			#$conf["db::fastGetDbData"]["WhereColumnValue"]=array($ac,$pw);#用於判斷語句的欄位數值陣列,若與LIKE搭配,則可以在關鍵自字串的左右名加上「%」符號,這樣就可以搜尋具有該字串的內容。
1166
			#$conf["WhereColumnCombine"]=array("");#用於判斷語句當中需要()起來的判斷式,須爲陣列值,"s"代表「(」,"e"代表「)」 ,若無則須設爲""。
1166
			#$conf["WhereColumnCombine"]=array("");#用於判斷語句當中需要()起來的判斷式,須爲陣列值,"s"代表「(」,"e"代表「)」 ,若無則須設爲""。
Line 1173... Line 1173...
1173
			#$conf["groupBy"]=array("");#爲要以哪幾個欄爲作爲分羣的依據(欄位相同的數值僅會取出一筆)。
1173
			#$conf["groupBy"]=array("");#爲要以哪幾個欄爲作爲分羣的依據(欄位相同的數值僅會取出一筆)。
1174
			#備註:
1174
			#備註:
1175
			#建議在查詢資料前,能夠檢查是否每個欄位都存在.
1175
			#建議在查詢資料前,能夠檢查是否每個欄位都存在.
1176
			$fastGetDbData=db::fastGetDbData($conf["db::fastGetDbData"]);
1176
			$fastGetDbData=db::fastGetDbData($conf["db::fastGetDbData"]);
1177
			unset($conf["db::fastGetDbData"]);
1177
			unset($conf["db::fastGetDbData"]);
1178
			
1178
 
1179
			#如果取得資料失敗
1179
			#如果取得資料失敗
1180
			if($fastGetDbData["status"]==="false"){
1180
			if($fastGetDbData["status"]==="false"){
1181
				
1181
 
1182
				#設置執行失敗
1182
				#設置執行失敗
1183
				$result["status"]="false";
1183
				$result["status"]="false";
1184
			
1184
 
1185
				#設置執行錯誤訊息
1185
				#設置執行錯誤訊息
1186
				$result["error"]=$fastGetDbData;
1186
				$result["error"]=$fastGetDbData;
1187
				
1187
 
1188
				#印出debug訊息
1188
				#印出debug訊息
1189
				var_dump($result);
1189
				var_dump($result);
1190
				
1190
 
1191
				#結束程式
1191
				#結束程式
1192
				exit;
1192
				exit;
1193
				
1193
 
1194
				}#if end
1194
				}#if end
1195
				
1195
 
1196
			#連線成功,將self::dbTouchable設為true
1196
			#連線成功,將self::dbTouchable設為true
1197
			self::$dbTouchable=true;
1197
			self::$dbTouchable=true;
1198
			
1198
 
1199
			}#if end
1199
			}#if end
1200
		
1200
 
1201
		$this->clients = new \SplObjectStorage;
1201
		$this->clients = new \SplObjectStorage;
1202
        
1202
 
1203
		}#funciton __construct end
1203
		}#funciton __construct end
1204
	
1204
 
1205
	#當用戶與伺服器建立連線時
1205
	#當用戶與伺服器建立連線時
1206
	public function onOpen(ConnectionInterface $conn){
1206
	public function onOpen(ConnectionInterface $conn){
1207
		
1207
 
1208
		// Store the new connection to send messages to later
1208
		// Store the new connection to send messages to later
1209
        	$this->clients->attach($conn);
1209
        	$this->clients->attach($conn);
1210
 
1210
 
1211
		#提示server有clent連上web socket.
1211
		#提示server有clent連上web socket.
1212
        	echo "New connection! ({$conn->resourceId})\n";
1212
        	echo "New connection! ({$conn->resourceId})\n";
1213
        
1213
 
1214
 		#用client的id為index來儲存額外的資訊
1214
 		#用client的id為index來儲存額外的資訊
1215
 		#元素 "conn" 代表已經建立連線的ConnectionInterface物件
1215
 		#元素 "conn" 代表已經建立連線的ConnectionInterface物件
1216
        	#元素 "talkTo" 代表要跟誰講話
1216
        	#元素 "talkTo" 代表要跟誰講話
1217
		#元素 "userId" 代表辨識使用者的id
1217
		#元素 "userId" 代表辨識使用者的id
1218
		#元素 "loigin=array("ac","pw")" 代表client輸入的帳號與密碼
1218
		#元素 "loigin=array("ac","pw")" 代表client輸入的帳號與密碼
1219
		#元素 "msgId" 為用來識別待確認訊息的索引
1219
		#元素 "msgId" 為用來識別待確認訊息的索引
1220
		#元素 "gotMsgId" 為用來識別是否已經設置過 msgId 了
1220
		#元素 "gotMsgId" 為用來識別是否已經設置過 msgId 了
1221
 		#$this->connInfo[$conn->resourceId]=array("conn"=>$conn,"talkTo"=>array(),"userId"=>"","login"=>array("ac"=>"","pw"=>""),"type"=>"");
1221
 		#$this->connInfo[$conn->resourceId]=array("conn"=>$conn,"talkTo"=>array(),"userId"=>"","login"=>array("ac"=>"","pw"=>""),"type"=>"");
1222
        	$this->connInfo[$conn->resourceId]=array("conn"=>$conn,"talkTo"=>array(),"userId"=>"","login"=>array("ac"=>"","pw"=>""),"msgId"=>"","gotMsgId"=>"false");
1222
        	$this->connInfo[$conn->resourceId]=array("conn"=>$conn,"talkTo"=>array(),"userId"=>"","login"=>array("ac"=>"","pw"=>""),"msgId"=>"","gotMsgId"=>"false");
1223
		
1223
 
1224
		#包裝訊息
1224
		#包裝訊息
1225
		#"type"為"login"	
1225
		#"type"為"login"
1226
		#"status"為"true"		
1226
		#"status"為"true"
1227
		#"data"為實際的訊息內容
1227
		#"data"為實際的訊息內容
1228
		$packedMsg=array("type"=>"login","status"=>"true","data"=>"Please input your account! ex:account:ws1");
1228
		$packedMsg=array("type"=>"login","status"=>"true","data"=>"Please input your account! ex:account:ws1");
1229
		
1229
 
1230
		#提示輸入帳號	
1230
		#提示輸入帳號
1231
		$conn->send(json_encode($packedMsg));
1231
		$conn->send(json_encode($packedMsg));
1232
				
1232
 
1233
		}#function onOpen end
1233
		}#function onOpen end
1234
 
1234
 
1235
	#當伺服器收到訊息時
1235
	#當伺服器收到訊息時
1236
	public function onMessage(ConnectionInterface $from, $msg){
1236
	public function onMessage(ConnectionInterface $from, $msg){
1237
		
1237
 
1238
		#debug
1238
		#debug
1239
		#$array=array("msgId"=>$this->connInfo[$from->resourceId]["msgId"],"resourceId"=>$from->resourceId,"msg"=>$msg);
1239
		#$array=array("msgId"=>$this->connInfo[$from->resourceId]["msgId"],"resourceId"=>$from->resourceId,"msg"=>$msg);
1240
		#var_dump($array);
1240
		#var_dump($array);
1241
		
1241
 
1242
		#如果該連線沒有使用者id
1242
		#如果該連線沒有使用者id
1243
		if($this->connInfo[$from->resourceId]["userId"]===""){
1243
		if($this->connInfo[$from->resourceId]["userId"]===""){
1244
			
1244
 
1245
			#代表需要認證,認證的會員資料表 slef::memberTableName,需要以下三個欄位.
1245
			#代表需要認證,認證的會員資料表 slef::memberTableName,需要以下三個欄位.
1246
			#id		自訂
1246
			#id		自訂
1247
			#account  	自訂
1247
			#account  	自訂
1248
			#password varchar(33)
1248
			#password varchar(33)
1249
			
1249
 
1250
			/*
1250
			/*
1251
			
1251
 
1252
			#如果尚未輸入用戶端類型
1252
			#如果尚未輸入用戶端類型
1253
			if($this->connInfo[$from->resourceId]["type"]===""){
1253
			if($this->connInfo[$from->resourceId]["type"]===""){
1254
				
1254
 
1255
				#如果 $msg 長度大於 "type:"
1255
				#如果 $msg 長度大於 "type:"
1256
				if(strlen($msg) > strlen("type:")){
1256
				if(strlen($msg) > strlen("type:")){
1257
					
1257
 
1258
					#檢查有無前置字元 "account:"
1258
					#檢查有無前置字元 "account:"
1259
					#函式說明:
1259
					#函式說明:
1260
					#將字串特定關鍵字與其前面的內容剔除
1260
					#將字串特定關鍵字與其前面的內容剔除
1261
					#回傳結果:
1261
					#回傳結果:
1262
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1262
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 1269... Line 1269...
1269
					#必填的參數:
1269
					#必填的參數:
1270
					$conf["stringProcess::delStrBeforeKeyWord"]["stringIn"]=$msg;#要處理的字串.
1270
					$conf["stringProcess::delStrBeforeKeyWord"]["stringIn"]=$msg;#要處理的字串.
1271
					$conf["stringProcess::delStrBeforeKeyWord"]["keyWord"]="type:";#特定字串.
1271
					$conf["stringProcess::delStrBeforeKeyWord"]["keyWord"]="type:";#特定字串.
1272
					$delStrBeforeKeyWord=stringProcess::delStrBeforeKeyWord($conf["stringProcess::delStrBeforeKeyWord"]);
1272
					$delStrBeforeKeyWord=stringProcess::delStrBeforeKeyWord($conf["stringProcess::delStrBeforeKeyWord"]);
1273
					unset($conf["stringProcess::delStrBeforeKeyWord"]);
1273
					unset($conf["stringProcess::delStrBeforeKeyWord"]);
1274
					
1274
 
1275
					#如果移除前置字元失敗
1275
					#如果移除前置字元失敗
1276
					if($delStrBeforeKeyWord["status"]==="false"){
1276
					if($delStrBeforeKeyWord["status"]==="false"){
1277
						
1277
 
1278
						#設置執行失敗
1278
						#設置執行失敗
1279
						$result["status"]="false";
1279
						$result["status"]="false";
1280
					
1280
 
1281
						#設置執行錯誤訊息
1281
						#設置執行錯誤訊息
1282
						$result["error"]=$delStrBeforeKeyWord;
1282
						$result["error"]=$delStrBeforeKeyWord;
1283
						
1283
 
1284
						#回傳結果
1284
						#回傳結果
1285
						return $result;
1285
						return $result;
1286
						
1286
 
1287
						}#if end
1287
						}#if end
1288
						
1288
 
1289
					#如果有符合條件的前置字元
1289
					#如果有符合條件的前置字元
1290
					if($delStrBeforeKeyWord["founded"]==="true"){
1290
					if($delStrBeforeKeyWord["founded"]==="true"){
1291
						
1291
 
1292
						#儲存用戶端類型
1292
						#儲存用戶端類型
1293
						$this->connInfo[$from->resourceId]["type"]=$delStrBeforeKeyWord["content"];
1293
						$this->connInfo[$from->resourceId]["type"]=$delStrBeforeKeyWord["content"];
1294
																							
-
 
-
 
1294
 
1295
						}#if end
1295
						}#if end
1296
					
1296
 
1297
					#反之
1297
					#反之
1298
					else{
1298
					else{
1299
						
1299
 
1300
						#提示輸入用戶端類型
1300
						#提示輸入用戶端類型
1301
						$from->send(json_encode("Please input your type! ex:type:user"));
1301
						$from->send(json_encode("Please input your type! ex:type:user"));
1302
						
1302
 
1303
						#結束程式
1303
						#結束程式
1304
						return true;
1304
						return true;
1305
						
1305
 
1306
						}#else end
1306
						}#else end
1307
					
1307
 
1308
					}#if end
1308
					}#if end
1309
					
1309
 
1310
				#反之代表輸入錯誤格式的type
1310
				#反之代表輸入錯誤格式的type
1311
				else{
1311
				else{
1312
					
1312
 
1313
					#提示輸入用戶端類型
1313
					#提示輸入用戶端類型
1314
					$from->send(json_encode("Please input your type! ex:type:user"));
1314
					$from->send(json_encode("Please input your type! ex:type:user"));
1315
										
1315
 
1316
					#結束程式
1316
					#結束程式
1317
					return true;
1317
					return true;
1318
					
1318
 
1319
					}#else end
1319
					}#else end
1320
				
1320
 
1321
				}#if end
1321
				}#if end
1322
							
1322
 
1323
			#如果用戶端是"user"
1323
			#如果用戶端是"user"
1324
			if($this->connInfo[$from->resourceId]["type"]==="user"){
1324
			if($this->connInfo[$from->resourceId]["type"]==="user"){
1325
				
1325
 
1326
				#...
1326
				#...
1327
				
1327
 
1328
				}#if end
1328
				}#if end
1329
				
1329
 
1330
			#反之如果用戶端是"device"
1330
			#反之如果用戶端是"device"
1331
			else if($this->connInfo[$from->resourceId]["type"]==="device"){
1331
			else if($this->connInfo[$from->resourceId]["type"]==="device"){
1332
					
1332
 
1333
				#...
1333
				#...
1334
					
1334
 
1335
				}#if end
1335
				}#if end
1336
				
1336
 
1337
			*/
1337
			*/
1338
			
1338
 
1339
			#如果尚未輸入帳戶
1339
			#如果尚未輸入帳戶
1340
			if($this->connInfo[$from->resourceId]["login"]["ac"]===""){
1340
			if($this->connInfo[$from->resourceId]["login"]["ac"]===""){
1341
			
1341
 
1342
				#如果 $msg 長度大於 "account:"
1342
				#如果 $msg 長度大於 "account:"
1343
				if(strlen($msg) > strlen("account:")){
1343
				if(strlen($msg) > strlen("account:")){
1344
					
1344
 
1345
					#檢查有無前置字元 "account:"
1345
					#檢查有無前置字元 "account:"
1346
					#函式說明:
1346
					#函式說明:
1347
					#將字串特定關鍵字與其前面的內容剔除
1347
					#將字串特定關鍵字與其前面的內容剔除
1348
					#回傳結果:
1348
					#回傳結果:
1349
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1349
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 1356... Line 1356...
1356
					#必填的參數:
1356
					#必填的參數:
1357
					$conf["stringProcess::delStrBeforeKeyWord"]["stringIn"]=$msg;#要處理的字串.
1357
					$conf["stringProcess::delStrBeforeKeyWord"]["stringIn"]=$msg;#要處理的字串.
1358
					$conf["stringProcess::delStrBeforeKeyWord"]["keyWord"]="account:";#特定字串.
1358
					$conf["stringProcess::delStrBeforeKeyWord"]["keyWord"]="account:";#特定字串.
1359
					$delStrBeforeKeyWord=stringProcess::delStrBeforeKeyWord($conf["stringProcess::delStrBeforeKeyWord"]);
1359
					$delStrBeforeKeyWord=stringProcess::delStrBeforeKeyWord($conf["stringProcess::delStrBeforeKeyWord"]);
1360
					unset($conf["stringProcess::delStrBeforeKeyWord"]);
1360
					unset($conf["stringProcess::delStrBeforeKeyWord"]);
1361
					
1361
 
1362
					#如果移除前置字元失敗
1362
					#如果移除前置字元失敗
1363
					if($delStrBeforeKeyWord["status"]==="false"){
1363
					if($delStrBeforeKeyWord["status"]==="false"){
1364
						
1364
 
1365
						#設置執行失敗
1365
						#設置執行失敗
1366
						$result["status"]="false";
1366
						$result["status"]="false";
1367
					
1367
 
1368
						#設置執行錯誤訊息
1368
						#設置執行錯誤訊息
1369
						$result["error"]=$delStrBeforeKeyWord;
1369
						$result["error"]=$delStrBeforeKeyWord;
1370
						
1370
 
1371
						#回傳結果
1371
						#回傳結果
1372
						return $result;
1372
						return $result;
1373
						
1373
 
1374
						}#if end
1374
						}#if end
1375
						
1375
 
1376
					#如果有符合條件的前置字元
1376
					#如果有符合條件的前置字元
1377
					if($delStrBeforeKeyWord["founded"]==="true"){
1377
					if($delStrBeforeKeyWord["founded"]==="true"){
1378
						
1378
 
1379
						#儲存帳戶
1379
						#儲存帳戶
1380
						$this->connInfo[$from->resourceId]["login"]["ac"]=$delStrBeforeKeyWord["content"];
1380
						$this->connInfo[$from->resourceId]["login"]["ac"]=$delStrBeforeKeyWord["content"];
1381
												
1381
 
1382
						}#if end
1382
						}#if end
1383
					
1383
 
1384
					#反之代表格式錯誤	
1384
					#反之代表格式錯誤
1385
					else{
1385
					else{
1386
						
1386
 
1387
						#包裝訊息
1387
						#包裝訊息
1388
						#"type"為"login"	
1388
						#"type"為"login"
1389
						#"status"為"true"		
1389
						#"status"為"true"
1390
						#"data"為實際的訊息內容
1390
						#"data"為實際的訊息內容
1391
						$packedMsg=array("type"=>"login","status"=>"true","data"=>"Please input your account! ex:account:ws1");
1391
						$packedMsg=array("type"=>"login","status"=>"true","data"=>"Please input your account! ex:account:ws1");
1392
						
1392
 
1393
						#提示輸入帳號	
1393
						#提示輸入帳號
1394
						$from->send(json_encode($packedMsg));
1394
						$from->send(json_encode($packedMsg));
1395
						
1395
 
1396
						#結束程式
1396
						#結束程式
1397
						return true;
1397
						return true;
1398
						
1398
 
1399
						}#else end
1399
						}#else end
1400
					
1400
 
1401
					}#if end
1401
					}#if end
1402
					
1402
 
1403
				#反之代表輸入錯誤格式的account
1403
				#反之代表輸入錯誤格式的account
1404
				else{
1404
				else{
1405
					
1405
 
1406
					#包裝訊息
1406
					#包裝訊息
1407
					#"type"為"login"	
1407
					#"type"為"login"
1408
					#"status"為"true"		
1408
					#"status"為"true"
1409
					#"data"為實際的訊息內容
1409
					#"data"為實際的訊息內容
1410
					$packedMsg=array("type"=>"login","status"=>"true","data"=>"Please input your account! ex:account:ws1");
1410
					$packedMsg=array("type"=>"login","status"=>"true","data"=>"Please input your account! ex:account:ws1");
1411
					
1411
 
1412
					#提示輸入帳號	
1412
					#提示輸入帳號
1413
					$from->send(json_encode($packedMsg));
1413
					$from->send(json_encode($packedMsg));
1414
					
1414
 
1415
					#結束程式
1415
					#結束程式
1416
					return true;
1416
					return true;
1417
					
1417
 
1418
					}#else end
1418
					}#else end
1419
				
1419
 
1420
				}#if end
1420
				}#if end
1421
						
1421
 
1422
			#如果有設置密碼欄位
1422
			#如果有設置密碼欄位
1423
			if(self::$passwordCol!==""){
1423
			if(self::$passwordCol!==""){
1424
				
1424
 
1425
				#如果尚未輸入密碼
1425
				#如果尚未輸入密碼
1426
				if($this->connInfo[$from->resourceId]["login"]["pw"]===""){
1426
				if($this->connInfo[$from->resourceId]["login"]["pw"]===""){
1427
					
1427
 
1428
					#$msg 長度大於 "password:"
1428
					#$msg 長度大於 "password:"
1429
					if(strlen($msg) > strlen("password:")){
1429
					if(strlen($msg) > strlen("password:")){
1430
						
1430
 
1431
						#檢查有無前置字元 "password:"
1431
						#檢查有無前置字元 "password:"
1432
						#函式說明:
1432
						#函式說明:
1433
						#將字串特定關鍵字與其前面的內容剔除
1433
						#將字串特定關鍵字與其前面的內容剔除
1434
						#回傳結果:
1434
						#回傳結果:
1435
						#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1435
						#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 1442... Line 1442...
1442
						#必填的參數:
1442
						#必填的參數:
1443
						$conf["stringProcess::delStrBeforeKeyWord"]["stringIn"]=$msg;#要處理的字串.
1443
						$conf["stringProcess::delStrBeforeKeyWord"]["stringIn"]=$msg;#要處理的字串.
1444
						$conf["stringProcess::delStrBeforeKeyWord"]["keyWord"]="password:";#特定字串.
1444
						$conf["stringProcess::delStrBeforeKeyWord"]["keyWord"]="password:";#特定字串.
1445
						$delStrBeforeKeyWord=stringProcess::delStrBeforeKeyWord($conf["stringProcess::delStrBeforeKeyWord"]);
1445
						$delStrBeforeKeyWord=stringProcess::delStrBeforeKeyWord($conf["stringProcess::delStrBeforeKeyWord"]);
1446
						unset($conf["stringProcess::delStrBeforeKeyWord"]);
1446
						unset($conf["stringProcess::delStrBeforeKeyWord"]);
1447
						
1447
 
1448
						#如果移除前置字元失敗
1448
						#如果移除前置字元失敗
1449
						if($delStrBeforeKeyWord["status"]==="false"){
1449
						if($delStrBeforeKeyWord["status"]==="false"){
1450
							
1450
 
1451
							#設置執行失敗
1451
							#設置執行失敗
1452
							$result["status"]="false";
1452
							$result["status"]="false";
1453
						
1453
 
1454
							#設置執行錯誤訊息
1454
							#設置執行錯誤訊息
1455
							$result["error"]=$delStrBeforeKeyWord;
1455
							$result["error"]=$delStrBeforeKeyWord;
1456
							
1456
 
1457
							#回傳結果
1457
							#回傳結果
1458
							return $result;
1458
							return $result;
1459
							
1459
 
1460
							}#if end
1460
							}#if end
1461
							
1461
 
1462
						#如果有符合條件的前置字元
1462
						#如果有符合條件的前置字元
1463
						if($delStrBeforeKeyWord["founded"]==="true"){
1463
						if($delStrBeforeKeyWord["founded"]==="true"){
1464
													
1464
 
1465
							#儲存密碼
1465
							#儲存密碼
1466
							$this->connInfo[$from->resourceId]["login"]["pw"]=$delStrBeforeKeyWord["content"];
1466
							$this->connInfo[$from->resourceId]["login"]["pw"]=$delStrBeforeKeyWord["content"];
1467
							
1467
 
1468
							}#if end
1468
							}#if end
1469
							
1469
 
1470
						#反之代表格式錯誤
1470
						#反之代表格式錯誤
1471
						else{
1471
						else{
1472
													
1472
 
1473
							#包裝訊息
1473
							#包裝訊息
1474
							#"type"為"login"	
1474
							#"type"為"login"
1475
							#"status"為"true"		
1475
							#"status"為"true"
1476
							#"data"為實際的訊息內容
1476
							#"data"為實際的訊息內容
1477
							$packedMsg=array("type"=>"login","status"=>"true","data"=>"Please input your password! ex:password:ws1");
1477
							$packedMsg=array("type"=>"login","status"=>"true","data"=>"Please input your password! ex:password:ws1");
1478
													
1478
 
1479
							#提示輸入密碼		
1479
							#提示輸入密碼
1480
							$from->send(json_encode($packedMsg));
1480
							$from->send(json_encode($packedMsg));
1481
							
1481
 
1482
							#結束程式
1482
							#結束程式
1483
							return true;
1483
							return true;
1484
												
1484
 
1485
							}#else end
1485
							}#else end
1486
						
1486
 
1487
						}#if end
1487
						}#if end
1488
						
1488
 
1489
					#反之代表格式錯誤
1489
					#反之代表格式錯誤
1490
					else{
1490
					else{
1491
						
1491
 
1492
						#包裝訊息
1492
						#包裝訊息
1493
						#"type"為"login"	
1493
						#"type"為"login"
1494
						#"status"為"true"		
1494
						#"status"為"true"
1495
						#"data"為實際的訊息內容
1495
						#"data"為實際的訊息內容
1496
						$packedMsg=array("type"=>"login","status"=>"true","data"=>"Please input your password! ex:password:ws1");
1496
						$packedMsg=array("type"=>"login","status"=>"true","data"=>"Please input your password! ex:password:ws1");
1497
												
1497
 
1498
						#提示輸入密碼		
1498
						#提示輸入密碼
1499
						$from->send(json_encode($packedMsg));
1499
						$from->send(json_encode($packedMsg));
1500
						
1500
 
1501
						#結束程式
1501
						#結束程式
1502
						return true;
1502
						return true;
1503
						
1503
 
1504
						}#else end
1504
						}#else end
1505
					
1505
 
1506
					}#if end
1506
					}#if end
1507
				
1507
 
1508
				}#if end
1508
				}#if end
1509
			
1509
 
1510
			#如果已經輸入帳號了
1510
			#如果已經輸入帳號了
1511
			if($this->connInfo[$from->resourceId]["login"]["ac"]!=""){
1511
			if($this->connInfo[$from->resourceId]["login"]["ac"]!=""){
1512
				
1512
 
1513
				#設置可以進行驗證
1513
				#設置可以進行驗證
1514
				$startAuth=true;
1514
				$startAuth=true;
1515
				
1515
 
1516
				#另存帳號
1516
				#另存帳號
1517
				$ac=$this->connInfo[$from->resourceId]["login"]["ac"];
1517
				$ac=$this->connInfo[$from->resourceId]["login"]["ac"];
1518
				
1518
 
1519
				#初始化密碼
1519
				#初始化密碼
1520
				$pw="";
1520
				$pw="";
1521
				
1521
 
1522
				#如果有設置密碼欄位
1522
				#如果有設置密碼欄位
1523
				if(self::$passwordCol!==""){
1523
				if(self::$passwordCol!==""){
1524
					
1524
 
1525
					#如果client已經輸入密碼了
1525
					#如果client已經輸入密碼了
1526
					if($this->connInfo[$from->resourceId]["login"]["pw"]!=""){
1526
					if($this->connInfo[$from->resourceId]["login"]["pw"]!=""){
1527
						
1527
 
1528
						#另存密碼
1528
						#另存密碼
1529
						$pw=md5($this->connInfo[$from->resourceId]["login"]["pw"]);
1529
						$pw=md5($this->connInfo[$from->resourceId]["login"]["pw"]);
1530
						
1530
 
1531
						}#if end
1531
						}#if end
1532
						
1532
 
1533
					#反之
1533
					#反之
1534
					else{
1534
					else{
1535
						
1535
 
1536
						#設置尚不能進行認證
1536
						#設置尚不能進行認證
1537
						$startAuth=false;
1537
						$startAuth=false;
1538
						
1538
 
1539
						}#else end
1539
						}#else end
1540
										
1540
 
1541
					}#if end
1541
					}#if end
1542
				
1542
 
1543
				#如果尚不能進行驗證
1543
				#如果尚不能進行驗證
1544
				if(!$startAuth){
1544
				if(!$startAuth){
1545
					
1545
 
1546
					#結束程式
1546
					#結束程式
1547
					return true;
1547
					return true;
1548
					
1548
 
1549
					}#if end
1549
					}#if end
1550
				
1550
 
1551
				#檢查有無符合的帳戶密碼
1551
				#檢查有無符合的帳戶密碼
1552
				#涵式說明:
1552
				#涵式說明:
1553
				#一次取得資料庫、表的資料
1553
				#一次取得資料庫、表的資料
1554
				#回傳的結果
1554
				#回傳的結果
1555
				#$result["status"],執行結果"true"為成功;"false"為執行失敗。
1555
				#$result["status"],執行結果"true"為成功;"false"為執行失敗。
Line 1571... Line 1571...
1571
				$conf["db::fastGetDbData"]["columnYouWant"]=array("id");#你想要的欄位!,若設為「array("*")」則代表全部欄位.
1571
				$conf["db::fastGetDbData"]["columnYouWant"]=array("id");#你想要的欄位!,若設為「array("*")」則代表全部欄位.
1572
				#可省略的參數:
1572
				#可省略的參數:
1573
				$conf["db::fastGetDbData"]["dbPassword"]=self::$dbPassword;#爲要存取dbServer的密碼
1573
				$conf["db::fastGetDbData"]["dbPassword"]=self::$dbPassword;#爲要存取dbServer的密碼
1574
				$conf["db::fastGetDbData"]["WhereColumnName"]=array(self::$accountCol);#用於判斷語句的欄位項目陣列。
1574
				$conf["db::fastGetDbData"]["WhereColumnName"]=array(self::$accountCol);#用於判斷語句的欄位項目陣列。
1575
				$conf["db::fastGetDbData"]["WhereColumnValue"]=array($ac);#用於判斷語句的欄位數值陣列,若與LIKE搭配,則可以在關鍵自字串的左右名加上「%」符號,這樣就可以搜尋具有該字串的內容。
1575
				$conf["db::fastGetDbData"]["WhereColumnValue"]=array($ac);#用於判斷語句的欄位數值陣列,若與LIKE搭配,則可以在關鍵自字串的左右名加上「%」符號,這樣就可以搜尋具有該字串的內容。
1576
				
1576
 
1577
				#如果有輸入密碼
1577
				#如果有輸入密碼
1578
				if($pw!==""){
1578
				if($pw!==""){
1579
					
1579
 
1580
					#新增要判斷 self::$password 欄位,是否有於 $pw
1580
					#新增要判斷 self::$password 欄位,是否有於 $pw
1581
					$conf["db::fastGetDbData"]["WhereColumnName"][]=self::$passwordCol;
1581
					$conf["db::fastGetDbData"]["WhereColumnName"][]=self::$passwordCol;
1582
					$conf["db::fastGetDbData"]["WhereColumnValue"][]=$pw;
1582
					$conf["db::fastGetDbData"]["WhereColumnValue"][]=$pw;
1583
					
1583
 
1584
					}#if end
1584
					}#if end
1585
				
1585
 
1586
				#$conf["WhereColumnCombine"]=array("");#用於判斷語句當中需要()起來的判斷式,須爲陣列值,"s"代表「(」,"e"代表「)」 ,若無則須設爲""。
1586
				#$conf["WhereColumnCombine"]=array("");#用於判斷語句當中需要()起來的判斷式,須爲陣列值,"s"代表「(」,"e"代表「)」 ,若無則須設爲""。
1587
				#$conf["WhereColumnOperator"]=array("");#用於判斷語句的比較符號陣列,可以用的符號有「"="、"!="、">"、"<"、"LIKE"、"NOT LIKE"」,預設都爲「=」。
1587
				#$conf["WhereColumnOperator"]=array("");#用於判斷語句的比較符號陣列,可以用的符號有「"="、"!="、">"、"<"、"LIKE"、"NOT LIKE"」,預設都爲「=」。
1588
				#$conf["WhereColumnAndOr"]=array("");#用於判斷語句條件之間成立的條件是AND還是OR,須爲陣列值。其數量應爲要判斷的欄位數量減一。
1588
				#$conf["WhereColumnAndOr"]=array("");#用於判斷語句條件之間成立的條件是AND還是OR,須爲陣列值。其數量應爲要判斷的欄位數量減一。
1589
				#$conf["orderItem"]="";#爲排序的項目依據,若要用隨機抽樣,可以用"rand()",可省略。
1589
				#$conf["orderItem"]="";#爲排序的項目依據,若要用隨機抽樣,可以用"rand()",可省略。
1590
				#$conf["ascORdesc"]="";#爲要低增還是遞減排序,asc爲遞增;desc爲遞減。
1590
				#$conf["ascORdesc"]="";#爲要低增還是遞減排序,asc爲遞增;desc爲遞減。
Line 1593... Line 1593...
1593
				#$conf["groupBy"]=array("");#爲要以哪幾個欄爲作爲分羣的依據(欄位相同的數值僅會取出一筆)。
1593
				#$conf["groupBy"]=array("");#爲要以哪幾個欄爲作爲分羣的依據(欄位相同的數值僅會取出一筆)。
1594
				#備註:
1594
				#備註:
1595
				#建議在查詢資料前,能夠檢查是否每個欄位都存在.
1595
				#建議在查詢資料前,能夠檢查是否每個欄位都存在.
1596
				$fastGetDbData=db::fastGetDbData($conf["db::fastGetDbData"]);
1596
				$fastGetDbData=db::fastGetDbData($conf["db::fastGetDbData"]);
1597
				unset($conf["db::fastGetDbData"]);
1597
				unset($conf["db::fastGetDbData"]);
1598
				
1598
 
1599
				#如果取得資料失敗
1599
				#如果取得資料失敗
1600
				if($fastGetDbData["status"]==="false"){
1600
				if($fastGetDbData["status"]==="false"){
1601
					
1601
 
1602
					#設置執行失敗
1602
					#設置執行失敗
1603
					$result["status"]="false";
1603
					$result["status"]="false";
1604
				
1604
 
1605
					#設置執行錯誤訊息
1605
					#設置執行錯誤訊息
1606
					$result["error"]=$fastGetDbData;					
1606
					$result["error"]=$fastGetDbData;
1607
					
1607
 
1608
					#提示server有資料庫錯誤
1608
					#提示server有資料庫錯誤
1609
					echo "dbError!";
1609
					echo "dbError!";
1610
					
1610
 
1611
					#結束程式
1611
					#結束程式
1612
					exit;
1612
					exit;
1613
					
1613
 
1614
					}#if end
1614
					}#if end
1615
					
1615
 
1616
				#如果沒有一筆資料
1616
				#如果沒有一筆資料
1617
				if($fastGetDbData["dataCount"]!==1){
1617
				if($fastGetDbData["dataCount"]!==1){
1618
					
1618
 
1619
					#代表登入失敗
1619
					#代表登入失敗
1620
					
1620
 
1621
					#清空登入用的帳戶
1621
					#清空登入用的帳戶
1622
					$this->connInfo[$from->resourceId]["login"]["ac"]="";
1622
					$this->connInfo[$from->resourceId]["login"]["ac"]="";
1623
				
1623
 
1624
					#清空登入用的密碼
1624
					#清空登入用的密碼
1625
					$this->connInfo[$from->resourceId]["login"]["pw"]="";
1625
					$this->connInfo[$from->resourceId]["login"]["pw"]="";
1626
					
1626
 
1627
					#包裝訊息
1627
					#包裝訊息
1628
					#"type"為"login"	
1628
					#"type"為"login"
1629
					#"status"為"false"		
1629
					#"status"為"false"
1630
					#"data"為實際的訊息內容
1630
					#"data"為實際的訊息內容
1631
					$packedMsg=array("type"=>"login","status"=>"false","data"=>"Login failed, please input account. ex:account:ws1");
1631
					$packedMsg=array("type"=>"login","status"=>"false","data"=>"Login failed, please input account. ex:account:ws1");
1632
					
1632
 
1633
					#提示登入失敗					
1633
					#提示登入失敗
1634
					$from->send(json_encode($packedMsg));
1634
					$from->send(json_encode($packedMsg));
1635
					
1635
 
1636
					#執行到這即可
1636
					#執行到這即可
1637
					return true;
1637
					return true;
1638
					
1638
 
1639
					}#if end
1639
					}#if end
1640
					
1640
 
1641
				#反之代表帳戶資訊正確
1641
				#反之代表帳戶資訊正確
1642
				else{
1642
				else{
1643
										
1643
 
1644
					#設置 $myUserId
1644
					#設置 $myUserId
1645
					$myUserId=$this->connInfo[$from->resourceId]["userId"]=$fastGetDbData["dataContent"]["id"][0];
1645
					$myUserId=$this->connInfo[$from->resourceId]["userId"]=$fastGetDbData["dataContent"]["id"][0];
1646
					
1646
 
1647
					#設置 $myId
1647
					#設置 $myId
1648
					$myId=$from->resourceId;
1648
					$myId=$from->resourceId;
1649
					
1649
 
1650
					#初始化msgId的部份內容
1650
					#初始化msgId的部份內容
1651
					$msgId=$myUserId."-";
1651
					$msgId=$myUserId."-";
1652
					
1652
 
1653
					#初始化儲存msgId的陣列
1653
					#初始化儲存msgId的陣列
1654
					$msgIdArray=array();
1654
					$msgIdArray=array();
1655
					
1655
 
1656
					#取得所有client的msgId
1656
					#取得所有client的msgId
1657
					foreach($this->connInfo as $clientInfoArray){
1657
					foreach($this->connInfo as $clientInfoArray){
1658
						
1658
 
1659
						#如果msgId不為""
1659
						#如果msgId不為""
1660
						if($clientInfoArray["msgId"]!==""){
1660
						if($clientInfoArray["msgId"]!==""){
1661
							
1661
 
1662
							#取得msgId
1662
							#取得msgId
1663
							$msgIdArray[]=$clientInfoArray["msgId"];
1663
							$msgIdArray[]=$clientInfoArray["msgId"];
1664
							
1664
 
1665
							}#if end
1665
							}#if end
1666
						
1666
 
1667
						}#foreach end
1667
						}#foreach end
1668
					
1668
 
1669
					#如果尚未產生完整的msgId或msgId有重複
1669
					#如果尚未產生完整的msgId或msgId有重複
1670
					while($msgId===$myUserId."-" || in_array($msgId,$msgIdArray)){
1670
					while($msgId===$myUserId."-" || in_array($msgId,$msgIdArray)){
1671
						
1671
 
1672
						#涵式說明:
1672
						#涵式說明:
1673
						#建立以圖片(PNG格式)呈現的驗證碼.
1673
						#建立以圖片(PNG格式)呈現的驗證碼.
1674
						#回傳的解果:
1674
						#回傳的解果:
1675
						#$result["status"],執行是否正常,"true"代表執行成功,"false"代表執行失敗.
1675
						#$result["status"],執行是否正常,"true"代表執行成功,"false"代表執行失敗.
1676
						#$result["error"],錯誤訊息.
1676
						#$result["error"],錯誤訊息.
Line 1689... Line 1689...
1689
						$conf["authenticate::validationCode"]["disableImg"]="true";
1689
						$conf["authenticate::validationCode"]["disableImg"]="true";
1690
						#$conf["imgToData"],字串,預設為"true"代表將圖片轉存成base64圖片,並將原始圖片移除;反之為"false"
1690
						#$conf["imgToData"],字串,預設為"true"代表將圖片轉存成base64圖片,並將原始圖片移除;反之為"false"
1691
						#$conf["imgToData"]="true";
1691
						#$conf["imgToData"]="true";
1692
						$validationCode=authenticate::validationCode($conf["authenticate::validationCode"]);
1692
						$validationCode=authenticate::validationCode($conf["authenticate::validationCode"]);
1693
						unset($conf["authenticate::validationCode"]);
1693
						unset($conf["authenticate::validationCode"]);
1694
						
1694
 
1695
						#如果產生亂數失敗
1695
						#如果產生亂數失敗
1696
						if($validationCode["status"]==="false"){
1696
						if($validationCode["status"]==="false"){
1697
							
1697
 
1698
							#設置錯誤狀態
1698
							#設置錯誤狀態
1699
							$result["status"]="false";
1699
							$result["status"]="false";
1700
							
1700
 
1701
							#設置錯誤提示
1701
							#設置錯誤提示
1702
							$result["error"]=$validationCode;
1702
							$result["error"]=$validationCode;
1703
							
1703
 
1704
							#印出錯誤訊息
1704
							#印出錯誤訊息
1705
							var_dump($result);
1705
							var_dump($result);
1706
							
1706
 
1707
							#回傳結果			
1707
							#回傳結果
1708
							return $result;				
1708
							return $result;
1709
											
1709
 
1710
							}#if end
1710
							}#if end
1711
							
1711
 
1712
						#儲存產生好的msgId	
1712
						#儲存產生好的msgId
1713
						$msgId=$myUserId."-".$validationCode["randNumberWord"];
1713
						$msgId=$myUserId."-".$validationCode["randNumberWord"];
1714
						
1714
 
1715
						}#while end
1715
						}#while end
1716
					
1716
 
1717
					#設置獨立於id與userId且用於驗證訊息是否收到的msgId
1717
					#設置獨立於id與userId且用於驗證訊息是否收到的msgId
1718
					$this->connInfo[$from->resourceId]["msgId"]=$msgId;
1718
					$this->connInfo[$from->resourceId]["msgId"]=$msgId;
1719
					
1719
 
1720
					#包裝訊息
1720
					#包裝訊息
1721
					#"type"為"login"	
1721
					#"type"為"login"
1722
					#"status"為"true"		
1722
					#"status"為"true"
1723
					#"data"為實際的訊息內容
1723
					#"data"為實際的訊息內容
1724
					$packedMsg=array("type"=>"login","status"=>"true","data"=>"Login successfully!");
1724
					$packedMsg=array("type"=>"login","status"=>"true","data"=>"Login successfully!");
1725
					
1725
 
1726
					#提示登入成功
1726
					#提示登入成功
1727
					$from->send(json_encode($packedMsg));
1727
					$from->send(json_encode($packedMsg));
1728
					
1728
 
1729
					#檢查是否有訊息是要給自己的
1729
					#檢查是否有訊息是要給自己的
1730
					foreach($this->unSendMsg as $index => $historyMsg){
1730
					foreach($this->unSendMsg as $index => $historyMsg){
1731
						
1731
 
1732
						#如果有要給自己的訊息
1732
						#如果有要給自己的訊息
1733
						if($myUserId===$historyMsg["toUserId"]){
1733
						if($myUserId===$historyMsg["toUserId"]){
1734
							
1734
 
1735
							#包裝訊息
1735
							#包裝訊息
1736
							#"type"為"unSendMsg?"	
1736
							#"type"為"unSendMsg?"
1737
							#"status"為"true"		
1737
							#"status"為"true"
1738
							#"data"為實際的訊息內容
1738
							#"data"為實際的訊息內容
1739
							$packedMsg=array("type"=>"unSendMsg?","status"=>"true","data"=>$historyMsg["msg"]);
1739
							$packedMsg=array("type"=>"unSendMsg?","status"=>"true","data"=>$historyMsg["msg"]);
1740
							
1740
 
1741
							#接收別人給的留言
1741
							#接收別人給的留言
1742
							$from->send(json_encode($packedMsg));
1742
							$from->send(json_encode($packedMsg));
1743
							
1743
 
1744
							#有幾個人在線上就執行幾次
1744
							#有幾個人在線上就執行幾次
1745
							foreach($this->connInfo as $toId => $cInfo){
1745
							foreach($this->connInfo as $toId => $cInfo){
1746
								
1746
 
1747
								#如果是自己
1747
								#如果是自己
1748
								if($myId===$toId ){
1748
								if($myId===$toId ){
1749
									
1749
 
1750
									#跳過
1750
									#跳過
1751
									continue;
1751
									continue;
1752
									
1752
 
1753
									}#if end
1753
									}#if end
1754
								
1754
 
1755
								#如果留言的對象有在線上
1755
								#如果留言的對象有在線上
1756
								if($historyMsg["fromUserId"]===$cInfo["userId"]){
1756
								if($historyMsg["fromUserId"]===$cInfo["userId"]){
1757
									
1757
 
1758
									#檢查對方是否已經在自己的通話清單裡面了
1758
									#檢查對方是否已經在自己的通話清單裡面了
1759
									
1759
 
1760
									#初始化要將對方加到自己的對話清單裡面
1760
									#初始化要將對方加到自己的對話清單裡面
1761
									$add=false;
1761
									$add=false;
1762
									
1762
 
1763
									#根據自己的每個通話對象
1763
									#根據自己的每個通話對象
1764
									foreach($this->connInfo[$myId]["talkTo"] as $myToIndex=>$myToCinfo ){
1764
									foreach($this->connInfo[$myId]["talkTo"] as $myToIndex=>$myToCinfo ){
1765
										
1765
 
1766
										#如果對對方已經存在於自己的對話清單裡面了
1766
										#如果對對方已經存在於自己的對話清單裡面了
1767
										if($myToCinfo["userId"]===$historyMsg["fromUserId"] && $myId===$historyMsg["fromId"]){
1767
										if($myToCinfo["userId"]===$historyMsg["fromUserId"] && $myId===$historyMsg["fromId"]){
1768
											
1768
 
1769
											#設置不要將對方加到自己的對話清單裡面
1769
											#設置不要將對方加到自己的對話清單裡面
1770
											$add=false;
1770
											$add=false;
1771
											
1771
 
1772
											#跳出foreach
1772
											#跳出foreach
1773
											break;
1773
											break;
1774
											
1774
 
1775
											}#if end
1775
											}#if end
1776
										
1776
 
1777
										}#foreach end
1777
										}#foreach end
1778
									
1778
 
1779
									#如果要將對方加到自己的對話清單裡面
1779
									#如果要將對方加到自己的對話清單裡面
1780
									if($add){
1780
									if($add){
1781
										
1781
 
1782
										#將對方加到自己的talkTo清單裡面
1782
										#將對方加到自己的talkTo清單裡面
1783
										$this->connInfo[$myId]["talkTo"][]=array("id"=>$toId,"userId"=>$historyMsg["fromUserId"],"msgId"=>$this->connInfo[$toId]["msgId"]);
1783
										$this->connInfo[$myId]["talkTo"][]=array("id"=>$toId,"userId"=>$historyMsg["fromUserId"],"msgId"=>$this->connInfo[$toId]["msgId"]);
1784
										
1784
 
1785
										}#if end
1785
										}#if end
1786
																										
-
 
-
 
1786
 
1787
									#檢查自己有沒有在對方talkTo清單裡面
1787
									#檢查自己有沒有在對方talkTo清單裡面
1788
																		
-
 
-
 
1788
 
1789
									#假設對方需要加自己到對話清單裡面
1789
									#假設對方需要加自己到對話清單裡面
1790
									$add=true;
1790
									$add=true;
1791
									
1791
 
1792
									#檢查通話對象能否跟自己對話
1792
									#檢查通話對象能否跟自己對話
1793
									
1793
 
1794
									#依據對方每個通話的對象
1794
									#依據對方每個通話的對象
1795
									foreach($this->connInfo[$toId]["talkTo"] as $talkToIndex=>$talkToCinfo){
1795
									foreach($this->connInfo[$toId]["talkTo"] as $talkToIndex=>$talkToCinfo){
1796
										
1796
 
1797
										#如果自己的userId已經在對方的對話清單裡面了
1797
										#如果自己的userId已經在對方的對話清單裡面了
1798
										if($myUserId===$talkToCinfo["userId"]){
1798
										if($myUserId===$talkToCinfo["userId"]){
1799
											
1799
 
1800
											#如果對方對話userId對應的id不在線上
1800
											#如果對方對話userId對應的id不在線上
1801
											if(!isset($this->connInfo[$toId])){
1801
											if(!isset($this->connInfo[$toId])){
1802
												
1802
 
1803
												#debug
1803
												#debug
1804
												#var_dump("對方對話userId(".$talkToCinfo["userId"].")應的id(".$toId.")不在線上");	
1804
												#var_dump("對方對話userId(".$talkToCinfo["userId"].")應的id(".$toId.")不在線上");
1805
												
1805
 
1806
												#檢查對方對話清單是否已經含有自己的id與userId了
1806
												#檢查對方對話清單是否已經含有自己的id與userId了
1807
												
1807
 
1808
												#假設對方對話清單沒有自己的id與userId
1808
												#假設對方對話清單沒有自己的id與userId
1809
												$update=true;
1809
												$update=true;
1810
												
1810
 
1811
												#依據對方每個通話的對象
1811
												#依據對方每個通話的對象
1812
												foreach($this->connInfo[$toId]["talkTo"] as $ceToIndex=>$ceToCinfo){
1812
												foreach($this->connInfo[$toId]["talkTo"] as $ceToIndex=>$ceToCinfo){
1813
													
1813
 
1814
													#如果對方對話清單已經含有自己的id與userId了												
1814
													#如果對方對話清單已經含有自己的id與userId了
1815
													if($ceToCinfo["id"]===$myId && $ceToCinfo["userId"]===$myUserId){
1815
													if($ceToCinfo["id"]===$myId && $ceToCinfo["userId"]===$myUserId){
1816
														
1816
 
1817
														#debug
1817
														#debug
1818
														#var_dump("對方對話清單已經含有自己的id(".$myId.")與userId了(".$myUserId.")");
1818
														#var_dump("對方對話清單已經含有自己的id(".$myId.")與userId了(".$myUserId.")");
1819
														
1819
 
1820
														#設置不需要更新
1820
														#設置不需要更新
1821
														$update=false;
1821
														$update=false;
1822
														
1822
 
1823
														#debug
1823
														#debug
1824
														#var_dump($toId."要加".$myId."到通話清單裡面");
1824
														#var_dump($toId."要加".$myId."到通話清單裡面");
1825
														
1825
 
1826
														#設置對方不需要加自己到對話清單裡面
1826
														#設置對方不需要加自己到對話清單裡面
1827
														$add=false;
1827
														$add=false;
1828
														
1828
 
1829
														}#if end
1829
														}#if end
1830
													
1830
 
1831
													}#foreach end
1831
													}#foreach end
1832
												
1832
 
1833
												#如果對方對話清單沒有自己的id與userId與msgId
1833
												#如果對方對話清單沒有自己的id與userId與msgId
1834
												if($update){
1834
												if($update){
1835
													
1835
 
1836
													#debug
1836
													#debug
1837
													#var_dump($toId."要更新對話清單中索引為".$talkToIndex."的id(".$this->connInfo[$toId]["talkTo"][$talkToIndex]["id"].")為".$myId);
1837
													#var_dump($toId."要更新對話清單中索引為".$talkToIndex."的id(".$this->connInfo[$toId]["talkTo"][$talkToIndex]["id"].")為".$myId);
1838
													
1838
 
1839
													#將自己在對方對話清單裡面的id改成現在的id
1839
													#將自己在對方對話清單裡面的id改成現在的id
1840
													$this->connInfo[$toId]["talkTo"][$talkToIndex]["id"]=$myId;
1840
													$this->connInfo[$toId]["talkTo"][$talkToIndex]["id"]=$myId;
1841
																										
-
 
-
 
1841
 
1842
													}#if end
1842
													}#if end
1843
													
1843
 
1844
												#反之對方對話清單已經有自己的id與userId
1844
												#反之對方對話清單已經有自己的id與userId
1845
												else{
1845
												else{
1846
													
1846
 
1847
													#移除對方該已經斷線的對話id
1847
													#移除對方該已經斷線的對話id
1848
													unset($this->connInfo[$toId]["talkTo"][$talkToIndex]);
1848
													unset($this->connInfo[$toId]["talkTo"][$talkToIndex]);
1849
													
1849
 
1850
													}#else end
1850
													}#else end
1851
												
1851
 
1852
												}#if end
1852
												}#if end
1853
											
1853
 
1854
											}#if end
1854
											}#if end
1855
										
1855
 
1856
										}#foreach end
1856
										}#foreach end
1857
										
1857
 
1858
									#如果對方需要加自己到對話清單裡面
1858
									#如果對方需要加自己到對話清單裡面
1859
									if($add){
1859
									if($add){
1860
										
1860
 
1861
										#檢查對方每個通話對象
1861
										#檢查對方每個通話對象
1862
										foreach($this->connInfo[$toId]["talkTo"] as $reCktIndex=>$reCktInfo){
1862
										foreach($this->connInfo[$toId]["talkTo"] as $reCktIndex=>$reCktInfo){
1863
											
1863
 
1864
											#如果已經有將自己加到對方的對話清單裡面
1864
											#如果已經有將自己加到對方的對話清單裡面
1865
											if($reCktInfo["id"]===$myId && $reCktInfo["userId"]===$myUserId){
1865
											if($reCktInfo["id"]===$myId && $reCktInfo["userId"]===$myUserId){
1866
												
1866
 
1867
												#設置不用將自己加到對方的對話清單裡面
1867
												#設置不用將自己加到對方的對話清單裡面
1868
												$add=false;
1868
												$add=false;
1869
												
1869
 
1870
												}#if end
1870
												}#if end
1871
											
1871
 
1872
											}#foreach end
1872
											}#foreach end
1873
										
1873
 
1874
										#如果對方需要加自己到對話清單裡面
1874
										#如果對方需要加自己到對話清單裡面
1875
										if($add){
1875
										if($add){
1876
											
1876
 
1877
											#debug
1877
											#debug
1878
											#var_dump($toId."要加".$myId."(msgId為".$this->connInfo[$myId]["msgId"].")到通話清單裡面");
1878
											#var_dump($toId."要加".$myId."(msgId為".$this->connInfo[$myId]["msgId"].")到通話清單裡面");
1879
										
1879
 
1880
											#讓通話對象也可以傳送訊息回來
1880
											#讓通話對象也可以傳送訊息回來
1881
											$this->connInfo[$toId]["talkTo"][]=array("id"=>$myId,"userId"=>$myUserId,"msgId"=>$this->connInfo[$myId]["msgId"]);
1881
											$this->connInfo[$toId]["talkTo"][]=array("id"=>$myId,"userId"=>$myUserId,"msgId"=>$this->connInfo[$myId]["msgId"]);
1882
											
1882
 
1883
											}#if end
1883
											}#if end
1884
										
1884
 
1885
										}#if end
1885
										}#if end
1886
									
1886
 
1887
									}#if end
1887
									}#if end
1888
								
1888
 
1889
								}#foreach end
1889
								}#foreach end
1890
							
1890
 
1891
							#移除留言紀錄
1891
							#移除留言紀錄
1892
							unset($this->unSendMsg[$index]);
1892
							unset($this->unSendMsg[$index]);
1893
							
1893
 
1894
							}#if end
1894
							}#if end
1895
						
1895
 
1896
						}#foreach end
1896
						}#foreach end
1897
						
1897
 
1898
					#檢查線上是否有人想跟我對話
1898
					#檢查線上是否有人想跟我對話
1899
					
1899
 
1900
					#有幾個人在線上就執行幾次
1900
					#有幾個人在線上就執行幾次
1901
					foreach($this->connInfo as $toId => $cInfo){
1901
					foreach($this->connInfo as $toId => $cInfo){
1902
						
1902
 
1903
						#如果是自己
1903
						#如果是自己
1904
						if($myId===$toId){
1904
						if($myId===$toId){
1905
							
1905
 
1906
							#跳過
1906
							#跳過
1907
							continue;
1907
							continue;
1908
							
1908
 
1909
							}#if end
1909
							}#if end
1910
						
1910
 
1911
						#該cliet目前再跟幾個人通話,就執行幾次
1911
						#該cliet目前再跟幾個人通話,就執行幾次
1912
						foreach($cInfo["talkTo"] as $tIndex=>$tInfo){
1912
						foreach($cInfo["talkTo"] as $tIndex=>$tInfo){
1913
							
1913
 
1914
							#如果自己的userId在對方的對話清單裡面
1914
							#如果自己的userId在對方的對話清單裡面
1915
							if($myUserId===$tInfo["userId"]){
1915
							if($myUserId===$tInfo["userId"]){
1916
								
1916
 
1917
								#假設自己需要被對方加到對話清單裡面
1917
								#假設自己需要被對方加到對話清單裡面
1918
								$add=true;
1918
								$add=true;
1919
								
1919
 
1920
								#檢查自己是否已經在對方的對話清單裡面
1920
								#檢查自己是否已經在對方的對話清單裡面
1921
								
1921
 
1922
								#對方有幾個通話對象就執行幾次
1922
								#對方有幾個通話對象就執行幾次
1923
								foreach($this->connInfo[$toId]["talkTo"] as $toIndex=>$toCinfo){
1923
								foreach($this->connInfo[$toId]["talkTo"] as $toIndex=>$toCinfo){
1924
									
1924
 
1925
									#如果自己已經在對方的對話清單裡面
1925
									#如果自己已經在對方的對話清單裡面
1926
									if($myUserId===$toCinfo["userId"] && $myId===$toCinfo["id"]){
1926
									if($myUserId===$toCinfo["userId"] && $myId===$toCinfo["id"]){
1927
										
1927
 
1928
										#設置自己不需要被對方加到對話清單裡面
1928
										#設置自己不需要被對方加到對話清單裡面
1929
										$add=false;
1929
										$add=false;
1930
										
1930
 
1931
										#跳出迴圈
1931
										#跳出迴圈
1932
										break;
1932
										break;
1933
										
1933
 
1934
										}#if end
1934
										}#if end
1935
									
1935
 
1936
									}#foreach end
1936
									}#foreach end
1937
								
1937
 
1938
								#另存針對自己userId的連線id
1938
								#另存針對自己userId的連線id
1939
								$oneIdOfMyUserid=$this->connInfo[$toId]["talkTo"][$tIndex]["id"];
1939
								$oneIdOfMyUserid=$this->connInfo[$toId]["talkTo"][$tIndex]["id"];
1940
								
1940
 
1941
								#如果對方對話清單裡面的用戶id不在線
1941
								#如果對方對話清單裡面的用戶id不在線
1942
								if(!isset($this->connInfo[$oneIdOfMyUserid])){
1942
								if(!isset($this->connInfo[$oneIdOfMyUserid])){
1943
									
1943
 
1944
									#假設要將自己在對方對話清單裡面的id改成現在的id
1944
									#假設要將自己在對方對話清單裡面的id改成現在的id
1945
									$update=true;
1945
									$update=true;
1946
									
1946
 
1947
									#自己不需要被對方加到對話清單裡面
1947
									#自己不需要被對方加到對話清單裡面
1948
									$add=false;
1948
									$add=false;
1949
									
1949
 
1950
									#檢查自己的id是否已經在對方的對話id裡面
1950
									#檢查自己的id是否已經在對方的對話id裡面
1951
									foreach($this->connInfo[$toId]["talkTo"] as $ceIndex=>$ceInfo){
1951
									foreach($this->connInfo[$toId]["talkTo"] as $ceIndex=>$ceInfo){
1952
										
1952
 
1953
										#如果自己的id已經在對方的對話清單裡面
1953
										#如果自己的id已經在對方的對話清單裡面
1954
										if($myId===$ceInfo["id"]){
1954
										if($myId===$ceInfo["id"]){
1955
											
1955
 
1956
											#設置不要將自己在對方對話清單裡面的id改成現在的id(可以移除該對話id)
1956
											#設置不要將自己在對方對話清單裡面的id改成現在的id(可以移除該對話id)
1957
											$update=false;
1957
											$update=false;
1958
											
1958
 
1959
											#跳出迴圈
1959
											#跳出迴圈
1960
											break;
1960
											break;
1961
											
1961
 
1962
											}#if end
1962
											}#if end
1963
										
1963
 
1964
										}#foreach end
1964
										}#foreach end
1965
									
1965
 
1966
									#如果要將自己在對方對話清單裡面的id改成現在的id
1966
									#如果要將自己在對方對話清單裡面的id改成現在的id
1967
									if($update){
1967
									if($update){
1968
										
1968
 
1969
										#debug
1969
										#debug
1970
										#var_dump($toId."要更新對話清單中索引為".$tIndex."的id(".$this->connInfo[$toId]["talkTo"][$tIndex]["id"].")為".$myId);
1970
										#var_dump($toId."要更新對話清單中索引為".$tIndex."的id(".$this->connInfo[$toId]["talkTo"][$tIndex]["id"].")為".$myId);
1971
																				
-
 
-
 
1971
 
1972
										#將自己在對方對話清單裡面的id改成現在的id
1972
										#將自己在對方對話清單裡面的id改成現在的id
1973
										$this->connInfo[$toId]["talkTo"][$tIndex]["id"]=$myId;
1973
										$this->connInfo[$toId]["talkTo"][$tIndex]["id"]=$myId;
1974
										
1974
 
1975
										}#if end
1975
										}#if end
1976
										
1976
 
1977
									#反之不要將自己在對方對話清單裡面的id改成現在的id(可以移除該對話id)
1977
									#反之不要將自己在對方對話清單裡面的id改成現在的id(可以移除該對話id)
1978
									else{
1978
									else{
1979
										
1979
 
1980
										#移除該對話id
1980
										#移除該對話id
1981
										unset($this->connInfo[$toId]["talkTo"][$tIndex]);
1981
										unset($this->connInfo[$toId]["talkTo"][$tIndex]);
1982
										
1982
 
1983
										}#else end
1983
										}#else end
1984
									
1984
 
1985
									}#if end	
1985
									}#if end
1986
									
1986
 
1987
								#如果要將自己加到對方的的通話清單裡面
1987
								#如果要將自己加到對方的的通話清單裡面
1988
								if($add){
1988
								if($add){
1989
									
1989
 
1990
									#debug
1990
									#debug
1991
									#var_dump($toId."要加".$myId."(msgId為".$this->connInfo[$myId]["msgId"].")到通話清單裡面");
1991
									#var_dump($toId."要加".$myId."(msgId為".$this->connInfo[$myId]["msgId"].")到通話清單裡面");
1992
									
1992
 
1993
									#將自己加到對方的的通話清單裡面
1993
									#將自己加到對方的的通話清單裡面
1994
									$this->connInfo[$toId]["talkTo"][]=array("id"=>$myId,"userId"=>$myUserId,"msgId"=>$this->connInfo[$myId]["msgId"]);			
1994
									$this->connInfo[$toId]["talkTo"][]=array("id"=>$myId,"userId"=>$myUserId,"msgId"=>$this->connInfo[$myId]["msgId"]);
1995
									
1995
 
1996
									}#if end								
1996
									}#if end
1997
								
1997
 
1998
								#檢查對方是否已經在自己的對話清單裡面
1998
								#檢查對方是否已經在自己的對話清單裡面
1999
																
-
 
-
 
1999
 
2000
								#設置要新增對方到自己的對話清單裡面.
2000
								#設置要新增對方到自己的對話清單裡面.
2001
								$add=true;
2001
								$add=true;
2002
								
2002
 
2003
								#自己有幾個對話對象就執行幾次
2003
								#自己有幾個對話對象就執行幾次
2004
								foreach($this->connInfo[$myId]["talkTo"] as $mtIndex=>$mtInfo){
2004
								foreach($this->connInfo[$myId]["talkTo"] as $mtIndex=>$mtInfo){
2005
									
2005
 
2006
									#對方的userId與id若在自己的對話清單裡面
2006
									#對方的userId與id若在自己的對話清單裡面
2007
									if($mtInfo["userId"]===$cInfo["userId"] && $mtInfo["id"]===$toId){
2007
									if($mtInfo["userId"]===$cInfo["userId"] && $mtInfo["id"]===$toId){
2008
										
2008
 
2009
										#設置不需要新增對方到自己的對話清單裡面
2009
										#設置不需要新增對方到自己的對話清單裡面
2010
										$add=false;
2010
										$add=false;
2011
										
2011
 
2012
										#跳出 foreach
2012
										#跳出 foreach
2013
										break;
2013
										break;
2014
										
2014
 
2015
										}#if end
2015
										}#if end
2016
									
2016
 
2017
									}#foreache end
2017
									}#foreache end
2018
								
2018
 
2019
								#如果要新增對方到自己的對話清單裡面.
2019
								#如果要新增對方到自己的對話清單裡面.
2020
								if($add){
2020
								if($add){
2021
									
2021
 
2022
									#自己有幾個對話對象就執行幾次
2022
									#自己有幾個對話對象就執行幾次
2023
									foreach($this->connInfo[$myId]["talkTo"] as $mtIndex=>$mtInfo){
2023
									foreach($this->connInfo[$myId]["talkTo"] as $mtIndex=>$mtInfo){
2024
										
2024
 
2025
										#如果對方已經在自己的通話清單
2025
										#如果對方已經在自己的通話清單
2026
										if($mtInfo["userId"]===$cInfo["userId"] && $mtInfo["id"]===$toId){
2026
										if($mtInfo["userId"]===$cInfo["userId"] && $mtInfo["id"]===$toId){
2027
 
2027
 
2028
											#設置不要將對方到自己的對話清單裡面.
2028
											#設置不要將對方到自己的對話清單裡面.
2029
											$add=false;
2029
											$add=false;
2030
											
2030
 
2031
											#跳出 foreach end
2031
											#跳出 foreach end
2032
											break;
2032
											break;
2033
 
2033
 
2034
											}#if end
2034
											}#if end
2035
										
2035
 
2036
										}#foreach end
2036
										}#foreach end
2037
									
2037
 
2038
									#如果要新增對方到自己的對話清單裡面.
2038
									#如果要新增對方到自己的對話清單裡面.
2039
									if($add){
2039
									if($add){
2040
																				
-
 
-
 
2040
 
2041
										#將對方加到自己的對話清單裡面
2041
										#將對方加到自己的對話清單裡面
2042
										$this->connInfo[$myId]["talkTo"][]=array("id"=>$toId,"userId"=>$cInfo["userId"],"msgId"=>$this->connInfo[$toId]["msgId"]);
2042
										$this->connInfo[$myId]["talkTo"][]=array("id"=>$toId,"userId"=>$cInfo["userId"],"msgId"=>$this->connInfo[$toId]["msgId"]);
2043
										
2043
 
2044
										}#if end
2044
										}#if end
2045
									
2045
 
2046
									}#if end
2046
									}#if end
2047
								
2047
 
2048
								}#if end
2048
								}#if end
2049
							
2049
 
2050
							}#foreach end
2050
							}#foreach end
2051
						
2051
 
2052
						}#foreach end
2052
						}#foreach end
2053
					
2053
 
2054
					#結束認證成功後的流程
2054
					#結束認證成功後的流程
2055
					return true;
2055
					return true;
2056
					
2056
 
2057
					}#else end
2057
					}#else end
2058
					
2058
 
2059
				}#else end
2059
				}#else end
2060
				
2060
 
2061
			#結束認證流程
2061
			#結束認證流程
2062
			return true;	
2062
			return true;
2063
			
2063
 
2064
			}#if end
2064
			}#if end
2065
		
2065
 
2066
		#如果收到 "id?"
2066
		#如果收到 "id?"
2067
		else if($msg==="id?"){
2067
		else if($msg==="id?"){
2068
			
2068
 
2069
			#包裝訊息
2069
			#包裝訊息
2070
			#"type"為"id?"	
2070
			#"type"為"id?"
2071
			#"status"為"true"		
2071
			#"status"為"true"
2072
			#"data"為實際的訊息內容
2072
			#"data"為實際的訊息內容
2073
			$packedMsg=array("type"=>"id?","status"=>"true","data"=>$from->resourceId);
2073
			$packedMsg=array("type"=>"id?","status"=>"true","data"=>$from->resourceId);
2074
			
2074
 
2075
			#傳自己的id給client
2075
			#傳自己的id給client
2076
			$from->send(json_encode($packedMsg));
2076
			$from->send(json_encode($packedMsg));
2077
			
2077
 
2078
			#回傳成功
2078
			#回傳成功
2079
			return true;
2079
			return true;
2080
			
2080
 
2081
			}#if end
2081
			}#if end
2082
			
2082
 
2083
		#如果收到 "ids?"
2083
		#如果收到 "ids?"
2084
		else if($msg==="ids?"){
2084
		else if($msg==="ids?"){
2085
			
2085
 
2086
			#初始化儲存其他人的id
2086
			#初始化儲存其他人的id
2087
			$idsArray=array();
2087
			$idsArray=array();
2088
			
2088
 
2089
			#針對所的client
2089
			#針對所的client
2090
			foreach ($this->clients as $client){
2090
			foreach ($this->clients as $client){
2091
			
2091
 
2092
				#排除自己
2092
				#排除自己
2093
				if($from !== $client){
2093
				if($from !== $client){
2094
					
2094
 
2095
					#取得其他人的id
2095
					#取得其他人的id
2096
					$idsArray[]=$client->resourceId;
2096
					$idsArray[]=$client->resourceId;
2097
					
2097
 
2098
					}#if end
2098
					}#if end
2099
				
2099
 
2100
				}#foreach end			
2100
				}#foreach end
2101
			
2101
 
2102
			#包裝訊息
2102
			#包裝訊息
2103
			#"type"為"ids?"	
2103
			#"type"為"ids?"
2104
			#"status"為"true"		
2104
			#"status"為"true"
2105
			#"data"為實際的訊息內容
2105
			#"data"為實際的訊息內容
2106
			$packedMsg=array("type"=>"ids?","status"=>"true","data"=>$idsArray);
2106
			$packedMsg=array("type"=>"ids?","status"=>"true","data"=>$idsArray);
2107
			
2107
 
2108
			#傳他人的id給client
2108
			#傳他人的id給client
2109
			$from->send(json_encode($packedMsg));
2109
			$from->send(json_encode($packedMsg));
2110
			
2110
 
2111
			#回傳成功
2111
			#回傳成功
2112
			return true;
2112
			return true;
2113
		
2113
 
2114
			}#if end
2114
			}#if end
2115
		
2115
 
2116
		#如果收到 "talkTo?"
2116
		#如果收到 "talkTo?"
2117
		else if($msg==="talkTo?"){
2117
		else if($msg==="talkTo?"){
2118
			
2118
 
2119
			#包裝訊息
2119
			#包裝訊息
2120
			#"type"為"talkTo?"	
2120
			#"type"為"talkTo?"
2121
			#"status"為"true"		
2121
			#"status"為"true"
2122
			#"data"為實際的訊息內容
2122
			#"data"為實際的訊息內容
2123
			$packedMsg=array("type"=>"talkTo?","status"=>"true","data"=>$this->connInfo[$from->resourceId]["talkTo"]);
2123
			$packedMsg=array("type"=>"talkTo?","status"=>"true","data"=>$this->connInfo[$from->resourceId]["talkTo"]);
2124
			
2124
 
2125
			#正在對話的對象id給client
2125
			#正在對話的對象id給client
2126
			$from->send(json_encode($packedMsg));
2126
			$from->send(json_encode($packedMsg));
2127
			
2127
 
2128
			#回傳成功
2128
			#回傳成功
2129
			return true;
2129
			return true;
2130
			
2130
 
2131
			}#if end
2131
			}#if end
2132
		
2132
 
2133
		#反之如果是 "msgId?"
2133
		#反之如果是 "msgId?"
2134
		else if($msg==="msgId?"){
2134
		else if($msg==="msgId?"){
2135
			
2135
 
2136
			#包裝訊息
2136
			#包裝訊息
2137
			#"type"為"talkTo?"	
2137
			#"type"為"talkTo?"
2138
			#"status"為"true"		
2138
			#"status"為"true"
2139
			#"data"為實際的訊息內容
2139
			#"data"為實際的訊息內容
2140
			$packedMsg=array("type"=>"msgId?","status"=>"true","data"=>$this->connInfo[$from->resourceId]["msgId"]);
2140
			$packedMsg=array("type"=>"msgId?","status"=>"true","data"=>$this->connInfo[$from->resourceId]["msgId"]);
2141
			
2141
 
2142
			#取得自己的msgId
2142
			#取得自己的msgId
2143
			$from->send(json_encode($packedMsg));
2143
			$from->send(json_encode($packedMsg));
2144
			
2144
 
2145
			#回傳成功
2145
			#回傳成功
2146
			return true;
2146
			return true;
2147
			
2147
 
2148
			}#if end
2148
			}#if end
2149
		
2149
 
2150
		#如果收到的$msg長度大於 "talkTo:"
2150
		#如果收到的$msg長度大於 "talkTo:"
2151
		if(strlen($msg)>strlen("talkTo:")){
2151
		if(strlen($msg)>strlen("talkTo:")){
2152
			
2152
 
2153
			#如果收到開頭為 "talkTo:"    
2153
			#如果收到開頭為 "talkTo:"
2154
			#涵式說明:
2154
			#涵式說明:
2155
			#取得符合特定字首與字尾的字串
2155
			#取得符合特定字首與字尾的字串
2156
			#回傳的結果:
2156
			#回傳的結果:
2157
			#$result["status"],若爲"true"則代表執行正常;若爲"false"則代表執行失敗。
2157
			#$result["status"],若爲"true"則代表執行正常;若爲"false"則代表執行失敗。
2158
			#$result["function"],當前執行的函數名稱.
2158
			#$result["function"],當前執行的函數名稱.
Line 2169... Line 2169...
2169
			#$conf["tailWord"]="";
2169
			#$conf["tailWord"]="";
2170
			#參考資料:
2170
			#參考資料:
2171
			#str_spilt(),可以將字串依照字母分割成一個個陣列字串。
2171
			#str_spilt(),可以將字串依照字母分割成一個個陣列字串。
2172
			$getMeetConditionsString=search::getMeetConditionsString($conf["search::getMeetConditionsString"]);
2172
			$getMeetConditionsString=search::getMeetConditionsString($conf["search::getMeetConditionsString"]);
2173
			unset($conf["search::getMeetConditionsString"]);
2173
			unset($conf["search::getMeetConditionsString"]);
2174
				
2174
 
2175
			#如果選找前置字串 "talkTo:" 失敗
2175
			#如果選找前置字串 "talkTo:" 失敗
2176
			if($getMeetConditionsString["status"]==="false"){
2176
			if($getMeetConditionsString["status"]==="false"){
2177
				
2177
 
2178
				#設置執行失敗
2178
				#設置執行失敗
2179
				$result["status"]="false";
2179
				$result["status"]="false";
2180
			
2180
 
2181
				#設置執行錯誤訊息
2181
				#設置執行錯誤訊息
2182
				$result["error"]=$getMeetConditionsString;
2182
				$result["error"]=$getMeetConditionsString;
2183
				
2183
 
2184
				#回傳結果
2184
				#回傳結果
2185
				return $result;
2185
				return $result;
2186
				
2186
 
2187
				}#if end
2187
				}#if end
2188
				
2188
 
2189
			#如果存在 "talkTo:" 前置字串
2189
			#如果存在 "talkTo:" 前置字串
2190
			if($getMeetConditionsString["founded"]==="true"){
2190
			if($getMeetConditionsString["founded"]==="true"){
2191
				
2191
 
2192
				#用 "talkTo:" 分割 $buf
2192
				#用 "talkTo:" 分割 $buf
2193
				#涵式說明:
2193
				#涵式說明:
2194
				#將固定格式的字串分開,並回傳分開的結果。
2194
				#將固定格式的字串分開,並回傳分開的結果。
2195
				#回傳結果:
2195
				#回傳結果:
2196
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
2196
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 2206... Line 2206...
2206
				#可省略參數:
2206
				#可省略參數:
2207
				#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
2207
				#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
2208
				$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
2208
				$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
2209
				$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
2209
				$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
2210
				unset($conf["stringProcess::spiltString"]);
2210
				unset($conf["stringProcess::spiltString"]);
2211
				
2211
 
2212
				#如果分割字串失敗
2212
				#如果分割字串失敗
2213
				if($spiltString["status"]==="false"){
2213
				if($spiltString["status"]==="false"){
2214
					
2214
 
2215
					#設置執行失敗
2215
					#設置執行失敗
2216
					$result["status"]="false";
2216
					$result["status"]="false";
2217
				
2217
 
2218
					#設置執行錯誤訊息
2218
					#設置執行錯誤訊息
2219
					$result["error"]=$spiltString;
2219
					$result["error"]=$spiltString;
2220
					
2220
 
2221
					#回傳結果
2221
					#回傳結果
2222
					return $result;
2222
					return $result;
2223
					
2223
 
2224
					}#if end
2224
					}#if end
2225
				
2225
 
2226
				#如果剛好分割出一筆資料
2226
				#如果剛好分割出一筆資料
2227
				if($spiltString["dataCounts"]===1){
2227
				if($spiltString["dataCounts"]===1){
2228
					
2228
 
2229
					#取得自己的id
2229
					#取得自己的id
2230
					$myId=$from->resourceId;
2230
					$myId=$from->resourceId;
2231
					
2231
 
2232
					#取得講話對象的id
2232
					#取得講話對象的id
2233
					$toId=$spiltString["dataArray"][0];
2233
					$toId=$spiltString["dataArray"][0];
2234
					
2234
 
2235
					#設置對象不存在的識別
2235
					#設置對象不存在的識別
2236
					$targetExist=false;
2236
					$targetExist=false;
2237
					
2237
 
2238
					#設置通話對象的key
2238
					#設置通話對象的key
2239
					$targetKey="";					
2239
					$targetKey="";
2240
					
2240
 
2241
					#如果要對話的對象存在
2241
					#如果要對話的對象存在
2242
					if(isset($this->connInfo[$toId])){
2242
					if(isset($this->connInfo[$toId])){
2243
						
2243
 
2244
						#取得對象的userId
2244
						#取得對象的userId
2245
						$toUserId=$this->connInfo[$toId]["userId"];
2245
						$toUserId=$this->connInfo[$toId]["userId"];
2246
						
2246
 
2247
						#取得同userId的對象id們
2247
						#取得同userId的對象id們
2248
						
2248
 
2249
						#初始化同userId的對象id
2249
						#初始化同userId的對象id
2250
						$targetC=array();
2250
						$targetC=array();
2251
						
2251
 
2252
						#針對每個連線
2252
						#針對每個連線
2253
						foreach($this->connInfo as $eachIndex=>$eachInfo){
2253
						foreach($this->connInfo as $eachIndex=>$eachInfo){
2254
							
2254
 
2255
							#如果是自己
2255
							#如果是自己
2256
							if($eachIndex===$myId){
2256
							if($eachIndex===$myId){
2257
								
2257
 
2258
								#跳到下一輪
2258
								#跳到下一輪
2259
								continue;
2259
								continue;
2260
								
2260
 
2261
								}#if end
2261
								}#if end
2262
							
2262
 
2263
							#如果是同userId的對象
2263
							#如果是同userId的對象
2264
							if($eachInfo["userId"]===$toUserId){
2264
							if($eachInfo["userId"]===$toUserId){
2265
								
2265
 
2266
								#取出連線
2266
								#取出連線
2267
								$targetC[$eachIndex]=$this->connInfo[$eachIndex];
2267
								$targetC[$eachIndex]=$this->connInfo[$eachIndex];
2268
								
2268
 
2269
								}#if end
2269
								}#if end
2270
							
2270
 
2271
							}#foreach end
2271
							}#foreach end
2272
										
2272
 
2273
						#針對每個同userId的連線
2273
						#針對每個同userId的連線
2274
						foreach($targetC as $tcIndex=>$tcInfo){
2274
						foreach($targetC as $tcIndex=>$tcInfo){
2275
							
2275
 
2276
							#檢查自己的對話對象是否已經在清單裡面了
2276
							#檢查自己的對話對象是否已經在清單裡面了
2277
						
2277
 
2278
							#假設對象可以加到自己對話清單裡面
2278
							#假設對象可以加到自己對話清單裡面
2279
							$add=true;
2279
							$add=true;
2280
							
2280
 
2281
							#針對每個要講話的對象
2281
							#針對每個要講話的對象
2282
							foreach($this->connInfo[$myId]["talkTo"] as $index=>$cInfo){
2282
							foreach($this->connInfo[$myId]["talkTo"] as $index=>$cInfo){
2283
								
2283
 
2284
								#如果對話對象已經在清單裡面了
2284
								#如果對話對象已經在清單裡面了
2285
								if($this->connInfo[$tcIndex]["userId"]===$cInfo["userId"] && $tcIndex===$cInfo["id"] && $tcInfo["msgId"]===$cInfo["msgId"]){
2285
								if($this->connInfo[$tcIndex]["userId"]===$cInfo["userId"] && $tcIndex===$cInfo["id"] && $tcInfo["msgId"]===$cInfo["msgId"]){
2286
									
2286
 
2287
									#設置不要再加到清單裡面
2287
									#設置不要再加到清單裡面
2288
									$add=false;
2288
									$add=false;
2289
									
2289
 
2290
									#跳出foreach
2290
									#跳出foreach
2291
									break;
2291
									break;
2292
									
2292
 
2293
									}#if end								
2293
									}#if end
2294
								
2294
 
2295
								}#foreach end
2295
								}#foreach end
2296
								
2296
 
2297
							#如果要加入到對話清單裡面
2297
							#如果要加入到對話清單裡面
2298
							if($add){
2298
							if($add){
2299
								
2299
 
2300
								#增加自己的對話對象
2300
								#增加自己的對話對象
2301
								$this->connInfo[$myId]["talkTo"][]=array("id"=>$tcIndex,"userId"=>$this->connInfo[$tcIndex]["userId"],"msgId"=>$this->connInfo[$tcIndex]["msgId"]);
2301
								$this->connInfo[$myId]["talkTo"][]=array("id"=>$tcIndex,"userId"=>$this->connInfo[$tcIndex]["userId"],"msgId"=>$this->connInfo[$tcIndex]["msgId"]);
2302
															
2302
 
2303
								#假設對方可以加自己到對話清單裡面
2303
								#假設對方可以加自己到對話清單裡面
2304
								$add=true;
2304
								$add=true;
2305
								
2305
 
2306
								#檢查通話對象能否跟自己對話
2306
								#檢查通話對象能否跟自己對話
2307
								foreach($this->connInfo[$tcIndex]["talkTo"] as $index=>$cInfo){
2307
								foreach($this->connInfo[$tcIndex]["talkTo"] as $index=>$cInfo){
2308
									
2308
 
2309
									#如果對話對象已經在清單裡面了
2309
									#如果對話對象已經在清單裡面了
2310
									if($this->connInfo[$myId]["userId"]===$cInfo["userId"]){
2310
									if($this->connInfo[$myId]["userId"]===$cInfo["userId"]){
2311
										
2311
 
2312
										#設置不要再加到清單裡面
2312
										#設置不要再加到清單裡面
2313
										$add=false;
2313
										$add=false;
2314
										
2314
 
2315
										#跳出 foreach
2315
										#跳出 foreach
2316
										break;
2316
										break;
2317
										
2317
 
2318
										}#if end
2318
										}#if end
2319
									
2319
 
2320
									}#foreach end
2320
									}#foreach end
2321
									
2321
 
2322
								#如果對方可以加自己到對話清單裡面
2322
								#如果對方可以加自己到對話清單裡面
2323
								if($add){
2323
								if($add){
2324
									
2324
 
2325
									#讓通話對象也可以傳送訊息回來
2325
									#讓通話對象也可以傳送訊息回來
2326
									$this->connInfo[$tcIndex]["talkTo"][]=array("id"=>$myId,"userId"=>$this->connInfo[$myId]["userId"],"msgId"=>$this->connInfo[$myId]["msgId"]);
2326
									$this->connInfo[$tcIndex]["talkTo"][]=array("id"=>$myId,"userId"=>$this->connInfo[$myId]["userId"],"msgId"=>$this->connInfo[$myId]["msgId"]);
2327
									
2327
 
2328
									}#if end
2328
									}#if end
2329
									
2329
 
2330
								#設置成功訊息
2330
								#設置成功訊息
2331
								$msg="client id ".$toId." added to talkTo list";
2331
								$msg="client id ".$toId." added to talkTo list";
2332
								
2332
 
2333
								#包裝訊息
2333
								#包裝訊息
2334
								#"type"為"status"	
2334
								#"type"為"status"
2335
								#"status"為"true"		
2335
								#"status"為"true"
2336
								#"data"為實際的訊息內容
2336
								#"data"為實際的訊息內容
2337
								$packedMsg=array("type"=>"status","status"=>"true","data"=>$msg);
2337
								$packedMsg=array("type"=>"status","status"=>"true","data"=>$msg);
2338
								
2338
 
2339
								#設置要給自己看的訊息
2339
								#設置要給自己看的訊息
2340
								$from->send(json_encode($packedMsg));							
2340
								$from->send(json_encode($packedMsg));
2341
								
2341
 
2342
								}#if end
2342
								}#if end
2343
								
2343
 
2344
							#反之
2344
							#反之
2345
							else{
2345
							else{
2346
								
2346
 
2347
								#設置錯誤訊息
2347
								#設置錯誤訊息
2348
								$msg="client id ".$toId." already in talkTo list";
2348
								$msg="client id ".$toId." already in talkTo list";
2349
								
2349
 
2350
								#包裝訊息
2350
								#包裝訊息
2351
								#"type"為"status"	
2351
								#"type"為"status"
2352
								#"status"為"false"		
2352
								#"status"為"false"
2353
								#"data"為實際的訊息內容
2353
								#"data"為實際的訊息內容
2354
								$packedMsg=array("type"=>"status","status"=>"false","data"=>$msg);
2354
								$packedMsg=array("type"=>"status","status"=>"false","data"=>$msg);
2355
								
2355
 
2356
								#設置要給自己看的訊息
2356
								#設置要給自己看的訊息
2357
								$from->send(json_encode($packedMsg));
2357
								$from->send(json_encode($packedMsg));
2358
								
2358
 
2359
								}#else end
2359
								}#else end
2360
							
2360
 
2361
							}#foreach end
2361
							}#foreach end
2362
						
2362
 
2363
						#回傳成功
2363
						#回傳成功
2364
						return true;
2364
						return true;
2365
						
2365
 
2366
						}#if end
2366
						}#if end
2367
					
2367
 
2368
					#反之不存在
2368
					#反之不存在
2369
					else{
2369
					else{
2370
						
2370
 
2371
						#設置錯誤訊息
2371
						#設置錯誤訊息
2372
						$msg="client id ".$toId." doesn't exist";
2372
						$msg="client id ".$toId." doesn't exist";
2373
						
2373
 
2374
						#包裝訊息
2374
						#包裝訊息
2375
						#"type"為"status"	
2375
						#"type"為"status"
2376
						#"status"為"false"		
2376
						#"status"為"false"
2377
						#"data"為實際的訊息內容
2377
						#"data"為實際的訊息內容
2378
						$packedMsg=array("type"=>"status","status"=>"false","data"=>$msg);
2378
						$packedMsg=array("type"=>"status","status"=>"false","data"=>$msg);
2379
						
2379
 
2380
						#設置要給自己看的訊息
2380
						#設置要給自己看的訊息
2381
						$from->send(json_encode($packedMsg));
2381
						$from->send(json_encode($packedMsg));
2382
						
2382
 
2383
						#回傳成功
2383
						#回傳成功
2384
						return true;
2384
						return true;
2385
						
2385
 
2386
						}#else end		
2386
						}#else end
2387
					
2387
 
2388
					}#if end
2388
					}#if end
2389
				
2389
 
2390
				}#if end
2390
				}#if end
2391
			
2391
 
2392
			}#if end
2392
			}#if end
2393
		
2393
 
2394
		#預設沒有收到確認收到訊息的回應訊息
2394
		#預設沒有收到確認收到訊息的回應訊息
2395
		$mIndex="false";
2395
		$mIndex="false";
2396
		
2396
 
2397
		#如果收到的$msg長度大於 "msgId:"
2397
		#如果收到的$msg長度大於 "msgId:"
2398
		if(strlen($msg)>strlen("msgId:")){
2398
		if(strlen($msg)>strlen("msgId:")){
2399
			
2399
 
2400
			#解析 $msg
2400
			#解析 $msg
2401
			
2401
 
2402
			#如果收到開頭為 "msgId:"    
2402
			#如果收到開頭為 "msgId:"
2403
			#涵式說明:
2403
			#涵式說明:
2404
			#取得符合特定字首與字尾的字串
2404
			#取得符合特定字首與字尾的字串
2405
			#回傳的結果:
2405
			#回傳的結果:
2406
			#$result["status"],若爲"true"則代表執行正常;若爲"false"則代表執行失敗。
2406
			#$result["status"],若爲"true"則代表執行正常;若爲"false"則代表執行失敗。
2407
			#$result["function"],當前執行的函數名稱.
2407
			#$result["function"],當前執行的函數名稱.
Line 2418... Line 2418...
2418
			#$conf["tailWord"]="";
2418
			#$conf["tailWord"]="";
2419
			#參考資料:
2419
			#參考資料:
2420
			#str_spilt(),可以將字串依照字母分割成一個個陣列字串。
2420
			#str_spilt(),可以將字串依照字母分割成一個個陣列字串。
2421
			$getMeetConditionsString=search::getMeetConditionsString($conf["search::getMeetConditionsString"]);
2421
			$getMeetConditionsString=search::getMeetConditionsString($conf["search::getMeetConditionsString"]);
2422
			unset($conf["search::getMeetConditionsString"]);
2422
			unset($conf["search::getMeetConditionsString"]);
2423
				
2423
 
2424
			#如果選找前置字串 "msgId:" 失敗
2424
			#如果選找前置字串 "msgId:" 失敗
2425
			if($getMeetConditionsString["status"]==="false"){
2425
			if($getMeetConditionsString["status"]==="false"){
2426
				
2426
 
2427
				#設置執行失敗
2427
				#設置執行失敗
2428
				$result["status"]="false";
2428
				$result["status"]="false";
2429
			
2429
 
2430
				#設置執行錯誤訊息
2430
				#設置執行錯誤訊息
2431
				$result["error"]=$getMeetConditionsString;
2431
				$result["error"]=$getMeetConditionsString;
2432
				
2432
 
2433
				#回傳結果
2433
				#回傳結果
2434
				return $result;
2434
				return $result;
2435
				
2435
 
2436
				}#if end
2436
				}#if end
2437
				
2437
 
2438
			#如果存在	 "msgId:" 前置字串
2438
			#如果存在	 "msgId:" 前置字串
2439
			if($getMeetConditionsString["founded"]==="true"){
2439
			if($getMeetConditionsString["founded"]==="true"){
2440
				
2440
 
2441
				#用 "msgId:" 分割 $msg
2441
				#用 "msgId:" 分割 $msg
2442
				#涵式說明:
2442
				#涵式說明:
2443
				#將固定格式的字串分開,並回傳分開的結果。
2443
				#將固定格式的字串分開,並回傳分開的結果。
2444
				#回傳結果:
2444
				#回傳結果:
2445
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
2445
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 2455... Line 2455...
2455
				#可省略參數:
2455
				#可省略參數:
2456
				#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
2456
				#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
2457
				$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
2457
				$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
2458
				$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
2458
				$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
2459
				unset($conf["stringProcess::spiltString"]);
2459
				unset($conf["stringProcess::spiltString"]);
2460
				
2460
 
2461
				#如果分割字串失敗
2461
				#如果分割字串失敗
2462
				if($spiltString["status"]==="false"){
2462
				if($spiltString["status"]==="false"){
2463
					
2463
 
2464
					#設置執行失敗
2464
					#設置執行失敗
2465
					$result["status"]="false";
2465
					$result["status"]="false";
2466
				
2466
 
2467
					#設置執行錯誤訊息
2467
					#設置執行錯誤訊息
2468
					$result["error"]=$spiltString;
2468
					$result["error"]=$spiltString;
2469
					
2469
 
2470
					#回傳結果
2470
					#回傳結果
2471
					return $result;
2471
					return $result;
2472
					
2472
 
2473
					}#if end
2473
					}#if end
2474
				
2474
 
2475
				#如果剛好分割出一筆資料
2475
				#如果剛好分割出一筆資料
2476
				if($spiltString["dataCounts"]===1){
2476
				if($spiltString["dataCounts"]===1){
2477
					
2477
 
2478
					#如果尚未設置過msgId
2478
					#如果尚未設置過msgId
2479
					if($this->connInfo[$from->resourceId]["gotMsgId"]==="false"){
2479
					if($this->connInfo[$from->resourceId]["gotMsgId"]==="false"){
2480
													
2480
 
2481
						#取得待確認訊息id
2481
						#取得待確認訊息id
2482
						$msgId=$spiltString["dataArray"][0];
2482
						$msgId=$spiltString["dataArray"][0];
2483
						
2483
 
2484
						#初始化儲存現有msgId的變數
2484
						#初始化儲存現有msgId的變數
2485
						$msgIdArray=array();
2485
						$msgIdArray=array();
2486
						
2486
 
2487
						#取得現有的msgId
2487
						#取得現有的msgId
2488
						foreach($this->connInfo as $cInfo){
2488
						foreach($this->connInfo as $cInfo){
2489
							
2489
 
2490
							#如果msgId不為空
2490
							#如果msgId不為空
2491
							if($cInfo["msgId"]!==""){
2491
							if($cInfo["msgId"]!==""){
2492
								
2492
 
2493
								#儲存msgId
2493
								#儲存msgId
2494
								$msgIdArray[]=$cInfo["msgId"];
2494
								$msgIdArray[]=$cInfo["msgId"];
2495
								
2495
 
2496
								}#if end
2496
								}#if end
2497
							
2497
 
2498
							}#foreach end
2498
							}#foreach end
2499
						
2499
 
2500
						#檢查msgId是否存在於$msgIdArray裡面
2500
						#檢查msgId是否存在於$msgIdArray裡面
2501
						if(in_array($msgId,$msgIdArray)){
2501
						if(in_array($msgId,$msgIdArray)){
2502
							
2502
 
2503
							#設置自己的msgId
2503
							#設置自己的msgId
2504
							$this->connInfo[$from->resourceId]["msgId"]=$msgId;						
2504
							$this->connInfo[$from->resourceId]["msgId"]=$msgId;
2505
							
2505
 
2506
							#設置已經設置過msgId
2506
							#設置已經設置過msgId
2507
							$this->connInfo[$from->resourceId]["gotMsgId"]="true";
2507
							$this->connInfo[$from->resourceId]["gotMsgId"]="true";
2508
							
2508
 
2509
							#設置成功訊息
2509
							#設置成功訊息
2510
							$msg="set msgId successfully";
2510
							$msg="set msgId successfully";
2511
							
2511
 
2512
							#包裝訊息
2512
							#包裝訊息
2513
							#"type"為"status"	
2513
							#"type"為"status"
2514
							#"status"為"false"		
2514
							#"status"為"false"
2515
							#"data"為實際的訊息內容
2515
							#"data"為實際的訊息內容
2516
							$packedMsg=array("type"=>"status","status"=>"true","data"=>$msg);
2516
							$packedMsg=array("type"=>"status","status"=>"true","data"=>$msg);
2517
							
2517
 
2518
							#設置要給自己看的訊息
2518
							#設置要給自己看的訊息
2519
							$from->send(json_encode($packedMsg));
2519
							$from->send(json_encode($packedMsg));
2520
							
2520
 
2521
							#針對每個連線
2521
							#針對每個連線
2522
							foreach($this->connInfo as $connIndex=>$connInfo){
2522
							foreach($this->connInfo as $connIndex=>$connInfo){
2523
							
2523
 
2524
								#如果是跟發訊息者同msgId但id不一樣的連線
2524
								#如果是跟發訊息者同msgId但id不一樣的連線
2525
								if($connInfo["msgId"]===$msgId && $connIndex!==$from->resourceId){
2525
								if($connInfo["msgId"]===$msgId && $connIndex!==$from->resourceId){
2526
									
2526
 
2527
									#將id為$connIndex的連線斷線
2527
									#將id為$connIndex的連線斷線
2528
									$this->clients->detach($this->connInfo[$connIndex]["conn"]);
2528
									$this->clients->detach($this->connInfo[$connIndex]["conn"]);
2529
 
2529
 
2530
									#提示已經將連線斷開
2530
									#提示已經將連線斷開
2531
									echo "Connection {$connIndex} has disconnected\n";
2531
									echo "Connection {$connIndex} has disconnected\n";
2532
									
2532
 
2533
									#移除先前同msgId但id不一樣的連線
2533
									#移除先前同msgId但id不一樣的連線
2534
									unset($this->connInfo[$connIndex]);
2534
									unset($this->connInfo[$connIndex]);
2535
									
2535
 
2536
									#針對每個連線
2536
									#針對每個連線
2537
									foreach($this->connInfo as $cIndexForTD=>$cInfoForTD){
2537
									foreach($this->connInfo as $cIndexForTD=>$cInfoForTD){
2538
										
2538
 
2539
										#針對 talkTo 的每個項目
2539
										#針對 talkTo 的每個項目
2540
										foreach($cInfoForTD["talkTo"] as $ttIndex=>$ttInfo){
2540
										foreach($cInfoForTD["talkTo"] as $ttIndex=>$ttInfo){
2541
											
2541
 
2542
											#如果是不存在的通話對象
2542
											#如果是不存在的通話對象
2543
											if($ttInfo["id"]===$connIndex){
2543
											if($ttInfo["id"]===$connIndex){
2544
												
2544
 
2545
												#移除之
2545
												#移除之
2546
												unset($this->connInfo[$cIndexForTD]["talkTo"][$ttIndex]);
2546
												unset($this->connInfo[$cIndexForTD]["talkTo"][$ttIndex]);
2547
												
2547
 
2548
												}#if end
2548
												}#if end
2549
											
2549
 
2550
											}#foreach end
2550
											}#foreach end
2551
										
2551
 
2552
										}#foreach end
2552
										}#foreach end
2553
									
2553
 
2554
									}#if end
2554
									}#if end
2555
								
2555
 
2556
								}#foreach end
2556
								}#foreach end
2557
							
2557
 
2558
							#如果有要確認的訊息陣列
2558
							#如果有要確認的訊息陣列
2559
							if(isset($this->unConfirmMsg[$msgId])){
2559
							if(isset($this->unConfirmMsg[$msgId])){
2560
								
2560
 
2561
								#初始化要傳送的待確認訊息
2561
								#初始化要傳送的待確認訊息
2562
								$packedMsg=array();
2562
								$packedMsg=array();
2563
								
2563
 
2564
								#有幾個要確認的訊息就執行幾次
2564
								#有幾個要確認的訊息就執行幾次
2565
								foreach($this->unConfirmMsg[$msgId] as $unCMindex=>$unCMinfo){
2565
								foreach($this->unConfirmMsg[$msgId] as $unCMindex=>$unCMinfo){
2566
									
2566
 
2567
									#如果待確認訊息的傳送目標id不等於剛要求更換msgId的用戶id
2567
									#如果待確認訊息的傳送目標id不等於剛要求更換msgId的用戶id
2568
									if($unCMinfo["toId"]!==$from->resourceId){
2568
									if($unCMinfo["toId"]!==$from->resourceId){
2569
										
2569
 
2570
										#設置傳送目標id為要求更換msgId的用戶id
2570
										#設置傳送目標id為要求更換msgId的用戶id
2571
										$this->unConfirmMsg[$msgId][$unCMindex]["toId"]=$from->resourceId;
2571
										$this->unConfirmMsg[$msgId][$unCMindex]["toId"]=$from->resourceId;
2572
										
2572
 
2573
										#檢查所有連線資訊
2573
										#檢查所有連線資訊
2574
										foreach($this->connInfo as $connId=>$connInfo){
2574
										foreach($this->connInfo as $connId=>$connInfo){
2575
											
2575
 
2576
											#依據talkTo清單的每個對象
2576
											#依據talkTo清單的每個對象
2577
											foreach($connInfo["talkTo"] as $talk2Index=>$talk2Info){
2577
											foreach($connInfo["talkTo"] as $talk2Index=>$talk2Info){
2578
												
2578
 
2579
												#若對象的id為$unCMinfo["toId"]且userId為$this->connInfo[$from->resourceId]["userId"]且msgId為$this->connInfo[$from->resourceId]["msgId"]者
2579
												#若對象的id為$unCMinfo["toId"]且userId為$this->connInfo[$from->resourceId]["userId"]且msgId為$this->connInfo[$from->resourceId]["msgId"]者
2580
												if($talk2Info["id"]===$unCMinfo["toId"] && $talk2Info["userId"]===$this->connInfo[$from->resourceId]["userId"] && $talk2Info["msgId"]===$this->connInfo[$from->resourceId]["msgId"]){
2580
												if($talk2Info["id"]===$unCMinfo["toId"] && $talk2Info["userId"]===$this->connInfo[$from->resourceId]["userId"] && $talk2Info["msgId"]===$this->connInfo[$from->resourceId]["msgId"]){
2581
													
2581
 
2582
													#移除該talkTo對象
2582
													#移除該talkTo對象
2583
													unset($this->connInfo[$connId]["talkTo"][$talk2Index]);
2583
													unset($this->connInfo[$connId]["talkTo"][$talk2Index]);
2584
													
2584
 
2585
													}#if ded
2585
													}#if ded
2586
												
2586
 
2587
												}#foreach end
2587
												}#foreach end
2588
											
2588
 
2589
											}#foreach end
2589
											}#foreach end
2590
										
2590
 
2591
										}#if end
2591
										}#if end
2592
									
2592
 
2593
									#如果要傳送的待確認訊息為空陣列
2593
									#如果要傳送的待確認訊息為空陣列
2594
									if($packedMsg===array()){
2594
									if($packedMsg===array()){
2595
										
2595
 
2596
										#包裝訊息
2596
										#包裝訊息
2597
										#"type"為"msg"
2597
										#"type"為"msg"
2598
										#"index"為$cInfo["id"]
2598
										#"index"為$cInfo["id"]
2599
										#"data"為實際的訊息內容
2599
										#"data"為實際的訊息內容
2600
										$packedMsg=array("type"=>"msg","index"=>$unCMindex,"data"=>$unCMinfo["msg"]);
2600
										$packedMsg=array("type"=>"msg","index"=>$unCMindex,"data"=>$unCMinfo["msg"]);
2601
										
2601
 
2602
										}#if end
2602
										}#if end
2603
																		
-
 
-
 
2603
 
2604
									}#foreach end
2604
									}#foreach end
2605
									
2605
 
2606
								#如果要傳送的待確認訊息不為空陣列
2606
								#如果要傳送的待確認訊息不為空陣列
2607
								if($packedMsg!==array()){
2607
								if($packedMsg!==array()){
2608
									
2608
 
2609
									#var_dump("重新登入更改msgId後要傳送的未確認訊息:".print_r($packedMsg,true));
2609
									#var_dump("重新登入更改msgId後要傳送的未確認訊息:".print_r($packedMsg,true));
2610
									
2610
 
2611
									#傳送包裝好的待確認訊息給對方
2611
									#傳送包裝好的待確認訊息給對方
2612
									$from->send(json_encode($packedMsg));
2612
									$from->send(json_encode($packedMsg));
2613
									
2613
 
2614
									}#if end
2614
									}#if end
2615
								
2615
 
2616
								}#if end
2616
								}#if end
2617
							
2617
 
2618
							#結束程式
2618
							#結束程式
2619
							return true;
2619
							return true;
2620
							
2620
 
2621
							}#if end
2621
							}#if end
2622
						
2622
 
2623
						}#if end
2623
						}#if end
2624
					
2624
 
2625
					#反之已經設置過 msgId 了
2625
					#反之已經設置過 msgId 了
2626
					else{
2626
					else{
2627
						
2627
 
2628
						#設置錯誤訊息
2628
						#設置錯誤訊息
2629
						$msg="msgId already set";
2629
						$msg="msgId already set";
2630
						
2630
 
2631
						#包裝訊息
2631
						#包裝訊息
2632
						#"type"為"status"	
2632
						#"type"為"status"
2633
						#"status"為"false"		
2633
						#"status"為"false"
2634
						#"data"為實際的訊息內容
2634
						#"data"為實際的訊息內容
2635
						$packedMsg=array("type"=>"status","status"=>"false","data"=>$msg);
2635
						$packedMsg=array("type"=>"status","status"=>"false","data"=>$msg);
2636
						
2636
 
2637
						#設置要給自己看的訊息
2637
						#設置要給自己看的訊息
2638
						$from->send(json_encode($packedMsg));
2638
						$from->send(json_encode($packedMsg));
2639
 
2639
 
2640
						#結束程式
2640
						#結束程式
2641
						return true;
2641
						return true;
2642
						
2642
 
2643
						}#else end
2643
						}#else end
2644
					
2644
 
2645
					}#if end
2645
					}#if end
2646
					
2646
 
2647
				#設置失敗訊息
2647
				#設置失敗訊息
2648
				$msg="set msgId failed";
2648
				$msg="set msgId failed";
2649
				
2649
 
2650
				#包裝訊息
2650
				#包裝訊息
2651
				#"type"為"status"	
2651
				#"type"為"status"
2652
				#"status"為"false"		
2652
				#"status"為"false"
2653
				#"data"為實際的訊息內容
2653
				#"data"為實際的訊息內容
2654
				$packedMsg=array("type"=>"status","status"=>"false","data"=>$msg);
2654
				$packedMsg=array("type"=>"status","status"=>"false","data"=>$msg);
2655
				
2655
 
2656
				#設置要給自己看的訊息
2656
				#設置要給自己看的訊息
2657
				$from->send(json_encode($packedMsg));
2657
				$from->send(json_encode($packedMsg));
2658
 
2658
 
2659
				#結束程式
2659
				#結束程式
2660
				return true;
2660
				return true;
2661
					
2661
 
2662
				}#if end
2662
				}#if end
2663
			
2663
 
2664
			}#if end
2664
			}#if end
2665
		
2665
 
2666
		#如果訊息長度大於 strlen("mIndex:")
2666
		#如果訊息長度大於 strlen("mIndex:")
2667
		if(strlen($msg)>strlen("mIndex:")){
2667
		if(strlen($msg)>strlen("mIndex:")){
2668
			
2668
 
2669
			#如果是確認收到訊息的回應訊息開頭 "mIndex:"    
2669
			#如果是確認收到訊息的回應訊息開頭 "mIndex:"
2670
			#涵式說明:
2670
			#涵式說明:
2671
			#取得符合特定字首與字尾的字串
2671
			#取得符合特定字首與字尾的字串
2672
			#回傳的結果:
2672
			#回傳的結果:
2673
			#$result["status"],若爲"true"則代表執行正常;若爲"false"則代表執行失敗。
2673
			#$result["status"],若爲"true"則代表執行正常;若爲"false"則代表執行失敗。
2674
			#$result["function"],當前執行的函數名稱.
2674
			#$result["function"],當前執行的函數名稱.
Line 2685... Line 2685...
2685
			#$conf["tailWord"]="";
2685
			#$conf["tailWord"]="";
2686
			#參考資料:
2686
			#參考資料:
2687
			#str_spilt(),可以將字串依照字母分割成一個個陣列字串。
2687
			#str_spilt(),可以將字串依照字母分割成一個個陣列字串。
2688
			$getMeetConditionsString=search::getMeetConditionsString($conf["search::getMeetConditionsString"]);
2688
			$getMeetConditionsString=search::getMeetConditionsString($conf["search::getMeetConditionsString"]);
2689
			unset($conf["search::getMeetConditionsString"]);
2689
			unset($conf["search::getMeetConditionsString"]);
2690
				
2690
 
2691
			#如果選找前置字串 "mIndex:" 失敗
2691
			#如果選找前置字串 "mIndex:" 失敗
2692
			if($getMeetConditionsString["status"]==="false"){
2692
			if($getMeetConditionsString["status"]==="false"){
2693
				
2693
 
2694
				#設置執行失敗
2694
				#設置執行失敗
2695
				$result["status"]="false";
2695
				$result["status"]="false";
2696
			
2696
 
2697
				#設置執行錯誤訊息
2697
				#設置執行錯誤訊息
2698
				$result["error"]=$getMeetConditionsString;
2698
				$result["error"]=$getMeetConditionsString;
2699
				
2699
 
2700
				#回傳結果
2700
				#回傳結果
2701
				return $result;
2701
				return $result;
2702
				
2702
 
2703
				}#if end
2703
				}#if end
2704
			
2704
 
2705
			#如果存在	 "mIndex:" 前置字串
2705
			#如果存在	 "mIndex:" 前置字串
2706
			if($getMeetConditionsString["founded"]==="true"){
2706
			if($getMeetConditionsString["founded"]==="true"){
2707
				
2707
 
2708
				#用 "mIndex:" 分割 $buf
2708
				#用 "mIndex:" 分割 $buf
2709
				#涵式說明:
2709
				#涵式說明:
2710
				#將固定格式的字串分開,並回傳分開的結果。
2710
				#將固定格式的字串分開,並回傳分開的結果。
2711
				#回傳結果:
2711
				#回傳結果:
2712
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
2712
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 2722... Line 2722...
2722
				#可省略參數:
2722
				#可省略參數:
2723
				#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
2723
				#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
2724
				$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
2724
				$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
2725
				$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
2725
				$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
2726
				unset($conf["stringProcess::spiltString"]);
2726
				unset($conf["stringProcess::spiltString"]);
2727
				
2727
 
2728
				#如果分割字串失敗
2728
				#如果分割字串失敗
2729
				if($spiltString["status"]==="false"){
2729
				if($spiltString["status"]==="false"){
2730
					
2730
 
2731
					#設置執行失敗
2731
					#設置執行失敗
2732
					$result["status"]="false";
2732
					$result["status"]="false";
2733
				
2733
 
2734
					#設置執行錯誤訊息
2734
					#設置執行錯誤訊息
2735
					$result["error"]=$spiltString;
2735
					$result["error"]=$spiltString;
2736
					
2736
 
2737
					#回傳結果
2737
					#回傳結果
2738
					return $result;
2738
					return $result;
2739
					
2739
 
2740
					}#if end
2740
					}#if end
2741
					
2741
 
2742
				#如果存在分割的識別字串
2742
				#如果存在分割的識別字串
2743
				if($spiltString["found"]==="true"){
2743
				if($spiltString["found"]==="true"){
2744
					
2744
 
2745
					#如果只分割出一段
2745
					#如果只分割出一段
2746
					if($spiltString["dataCounts"]===1){
2746
					if($spiltString["dataCounts"]===1){
2747
						
2747
 
2748
						#取得待確認訊息的索引
2748
						#取得待確認訊息的索引
2749
						$mIndex=$spiltString["dataArray"][0];
2749
						$mIndex=$spiltString["dataArray"][0];
2750
						
2750
 
2751
						}#if end
2751
						}#if end
2752
					
2752
 
2753
					}#if end
2753
					}#if end
2754
				
2754
 
2755
				}#if end
2755
				}#if end
2756
			
2756
 
2757
			}#if end
2757
			}#if end
2758
		
2758
 
2759
		#如果有要講話的對象
2759
		#如果有要講話的對象
2760
		if(count($this->connInfo[$from->resourceId]["talkTo"])>0){
2760
		if(count($this->connInfo[$from->resourceId]["talkTo"])>0){
2761
						
2761
 
2762
			#依據每個對話對象
2762
			#依據每個對話對象
2763
			foreach($this->connInfo[$from->resourceId]["talkTo"] as $index => $cInfo){
2763
			foreach($this->connInfo[$from->resourceId]["talkTo"] as $index => $cInfo){
2764
								
2764
 
2765
				#如果要講話的對象不存在(socket已經斷開)
2765
				#如果要講話的對象不存在(socket已經斷開)
2766
				if(!isset($this->connInfo[$cInfo["id"]])){
2766
				if(!isset($this->connInfo[$cInfo["id"]])){
2767
					
2767
 
2768
					#將訊息儲存起來,等對象上線後再把訊息傳過去.
2768
					#將訊息儲存起來,等對象上線後再把訊息傳過去.
2769
					$this->unSendMsg[]=array(
2769
					$this->unSendMsg[]=array(
2770
						"fromId"=>$from->resourceId,
2770
						"fromId"=>$from->resourceId,
2771
						"toId"=>"",
2771
						"toId"=>"",
2772
						"fromUserId"=>$this->connInfo[$from->resourceId]["userId"],
2772
						"fromUserId"=>$this->connInfo[$from->resourceId]["userId"],
2773
						"toUserId"=>$cInfo["userId"],
2773
						"toUserId"=>$cInfo["userId"],
2774
						"msg"=>$msg
2774
						"msg"=>$msg
2775
						);
2775
						);
2776
					
2776
 
2777
					}#if end
2777
					}#if end
2778
					
2778
 
2779
				#反之,講話的對象存在
2779
				#反之,講話的對象存在
2780
				else{
2780
				else{
2781
					
2781
 
2782
					#如果收到確認收到訊息的回應
2782
					#如果收到確認收到訊息的回應
2783
					if($mIndex!=="false"){
2783
					if($mIndex!=="false"){
2784
						
2784
 
2785
						#儲存對話目標的msgId
2785
						#儲存對話目標的msgId
2786
						$targetMsgId=$this->connInfo[$from->resourceId]["msgId"];
2786
						$targetMsgId=$this->connInfo[$from->resourceId]["msgId"];
2787
							
2787
 
2788
						#針對每個連線的待確認訊息
2788
						#針對每個連線的待確認訊息
2789
						foreach($this->unConfirmMsg as $unCMindex=>$unCMinfo){
2789
						foreach($this->unConfirmMsg as $unCMindex=>$unCMinfo){
2790
							
2790
 
2791
							#debug
2791
							#debug
2792
							#var_dump($unCMindex."!==".$targetMsgId." && count(\$unCMinfo):".count($unCMinfo));
2792
							#var_dump($unCMindex."!==".$targetMsgId." && count(\$unCMinfo):".count($unCMinfo));
2793
							
2793
 
2794
							#如果 未確認訊息達到10則以上 且不是接收訊息者的msgId
2794
							#如果 未確認訊息達到10則以上 且不是接收訊息者的msgId
2795
							if($unCMindex!==$targetMsgId && count($unCMinfo)>10){
2795
							if($unCMindex!==$targetMsgId && count($unCMinfo)>10){
2796
								
2796
 
2797
								#代表接收訊息的用戶無法透過網路跟server溝通
2797
								#代表接收訊息的用戶無法透過網路跟server溝通
2798
														
2798
 
2799
								#提示 server 連線已結束
2799
								#提示 server 連線已結束
2800
								echo "Connection {$unCMinfo["id"]} has disconnected\n";
2800
								echo "Connection {$unCMinfo["id"]} has disconnected\n";
2801
								
2801
 
2802
								#將待驗證的訊息變成留言訊息
2802
								#將待驗證的訊息變成留言訊息
2803
								
2803
 
2804
								#有幾個未確認訊息就執行幾次
2804
								#有幾個未確認訊息就執行幾次
2805
								foreach($this->unConfirmMsg[$unCMindex] as $unConMsgIndex => $unConMsgInfo){
2805
								foreach($this->unConfirmMsg[$unCMindex] as $unConMsgIndex => $unConMsgInfo){
2806
									
2806
 
2807
									#將訊息儲存起來,等對象上線後再把訊息傳過去.
2807
									#將訊息儲存起來,等對象上線後再把訊息傳過去.
2808
									$this->unSendMsg[]=array(
2808
									$this->unSendMsg[]=array(
2809
										"fromId"=>$unConMsgInfo["fromId"],
2809
										"fromId"=>$unConMsgInfo["fromId"],
2810
										"toId"=>"",
2810
										"toId"=>"",
2811
										"fromUserId"=>$unConMsgInfo["fromUserId"],
2811
										"fromUserId"=>$unConMsgInfo["fromUserId"],
2812
										"toUserId"=>$unConMsgInfo["toUserId"],
2812
										"toUserId"=>$unConMsgInfo["toUserId"],
2813
										"msg"=>$unConMsgInfo["msg"]
2813
										"msg"=>$unConMsgInfo["msg"]
2814
										);
2814
										);
2815
									
2815
 
2816
									}#foreach end
2816
									}#foreach end
2817
								
2817
 
2818
								#移除待驗證的訊息資訊
2818
								#移除待驗證的訊息資訊
2819
								unset($this->unConfirmMsg[$unCMindex]);
2819
								unset($this->unConfirmMsg[$unCMindex]);
2820
								
2820
 
2821
								#將client的連線斷開
2821
								#將client的連線斷開
2822
								$this->clients->detach($this->connInfo[$unConMsgInfo["fromId"]]["conn"]);
2822
								$this->clients->detach($this->connInfo[$unConMsgInfo["fromId"]]["conn"]);
2823
								
2823
 
2824
								#移除連線的資訊
2824
								#移除連線的資訊
2825
								unset($this->connInfo[$unConMsgInfo["fromId"]]);								
2825
								unset($this->connInfo[$unConMsgInfo["fromId"]]);
2826
								
2826
 
2827
								}#if end
2827
								}#if end
2828
							
2828
 
2829
							}#foreach end
2829
							}#foreach end
2830
																		
-
 
-
 
2830
 
2831
						#存在收到訊息用戶id的待確認訊息
2831
						#存在收到訊息用戶id的待確認訊息
2832
						if(isset($this->unConfirmMsg[$targetMsgId])){
2832
						if(isset($this->unConfirmMsg[$targetMsgId])){
2833
							
2833
 
2834
							#如果該未確認訊息存在
2834
							#如果該未確認訊息存在
2835
							if(isset($this->unConfirmMsg[$targetMsgId][$mIndex])){
2835
							if(isset($this->unConfirmMsg[$targetMsgId][$mIndex])){
2836
								
2836
 
2837
								#debug
2837
								#debug
2838
								#var_dump("移除待確認的訊息".print_r($this->unConfirmMsg[$targetMsgId][$mIndex],true));
2838
								#var_dump("移除待確認的訊息".print_r($this->unConfirmMsg[$targetMsgId][$mIndex],true));
2839
								
2839
 
2840
								#移除該確認訊息
2840
								#移除該確認訊息
2841
								unset($this->unConfirmMsg[$targetMsgId][$mIndex]);
2841
								unset($this->unConfirmMsg[$targetMsgId][$mIndex]);
2842
								
2842
 
2843
								#還剩下幾個待確認訊息就執行幾次
2843
								#還剩下幾個待確認訊息就執行幾次
2844
								foreach($this->unConfirmMsg[$targetMsgId] as $unCMindex=>$unCMinfo){
2844
								foreach($this->unConfirmMsg[$targetMsgId] as $unCMindex=>$unCMinfo){
2845
									
2845
 
2846
									#如果後面還有訊息
2846
									#如果後面還有訊息
2847
									if(isset($this->unConfirmMsg[$targetMsgId][$unCMindex+1])){
2847
									if(isset($this->unConfirmMsg[$targetMsgId][$unCMindex+1])){
2848
										
2848
 
2849
										#如果與下一份訊息的 fromUserId一樣, toUserId 一樣, fromId一樣, toId 不同, msg 一樣.
2849
										#如果與下一份訊息的 fromUserId一樣, toUserId 一樣, fromId一樣, toId 不同, msg 一樣.
2850
										if(
2850
										if(
2851
											(
2851
											(
2852
												$this->unConfirmMsg[$targetMsgId][$unCMindex]["fromUserId"]===$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["fromUserId"]
2852
												$this->unConfirmMsg[$targetMsgId][$unCMindex]["fromUserId"]===$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["fromUserId"]
2853
												)
2853
												)
Line 2860... Line 2860...
2860
												$this->unConfirmMsg[$targetMsgId][$unCMindex]["fromId"]===$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["fromId"]
2860
												$this->unConfirmMsg[$targetMsgId][$unCMindex]["fromId"]===$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["fromId"]
2861
												)
2861
												)
2862
											&&
2862
											&&
2863
											(
2863
											(
2864
												$this->unConfirmMsg[$targetMsgId][$unCMindex]["toId"]!==$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["toId"]
2864
												$this->unConfirmMsg[$targetMsgId][$unCMindex]["toId"]!==$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["toId"]
2865
												)	
2865
												)
2866
											&&
2866
											&&
2867
											(
2867
											(
2868
												$this->unConfirmMsg[$targetMsgId][$unCMindex]["msg"]===$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["msg"]
2868
												$this->unConfirmMsg[$targetMsgId][$unCMindex]["msg"]===$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["msg"]
2869
												)									
2869
												)
2870
											){
2870
											){
2871
											
2871
 
2872
											#卸除該筆重複的訊息
2872
											#卸除該筆重複的訊息
2873
											unset($this->unConfirmMsg[$targetMsgId][$unCMindex]);
2873
											unset($this->unConfirmMsg[$targetMsgId][$unCMindex]);
2874
												
2874
 
2875
											#跳到下一則訊息
2875
											#跳到下一則訊息
2876
											continue;	
2876
											continue;
2877
												
2877
 
2878
											}#if end
2878
											}#if end
2879
											
2879
 
2880
										/*	
2880
										/*
2881
										#debug
2881
										#debug
2882
										else{
2882
										else{
2883
											
2883
 
2884
											var_dump($this->unConfirmMsg[$targetMsgId][$unCMindex]["fromUserId"].":".$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["fromUserId"]);
2884
											var_dump($this->unConfirmMsg[$targetMsgId][$unCMindex]["fromUserId"].":".$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["fromUserId"]);
2885
											var_dump($this->unConfirmMsg[$targetMsgId][$unCMindex]["fromId"].":".$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["fromId"]);
2885
											var_dump($this->unConfirmMsg[$targetMsgId][$unCMindex]["fromId"].":".$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["fromId"]);
2886
											
2886
 
2887
											var_dump($this->unConfirmMsg[$targetMsgId][$unCMindex]["toUserId"].":".$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["toUserId"]);
2887
											var_dump($this->unConfirmMsg[$targetMsgId][$unCMindex]["toUserId"].":".$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["toUserId"]);
2888
											var_dump($this->unConfirmMsg[$targetMsgId][$unCMindex]["toId"].":".$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["toId"]);
2888
											var_dump($this->unConfirmMsg[$targetMsgId][$unCMindex]["toId"].":".$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["toId"]);
2889
											
2889
 
2890
											var_dump($this->unConfirmMsg[$targetMsgId][$unCMindex]["msg"].":".$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["msg"]);
2890
											var_dump($this->unConfirmMsg[$targetMsgId][$unCMindex]["msg"].":".$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["msg"]);
2891
											
2891
 
2892
											}#else end
2892
											}#else end
2893
										*/
2893
										*/
2894
												
2894
 
2895
										}#if end
2895
										}#if end
2896
									
2896
 
2897
									#包裝訊息
2897
									#包裝訊息
2898
									#"type"為"msg"
2898
									#"type"為"msg"
2899
									#"index"為$cInfo["id"]
2899
									#"index"為$cInfo["id"]
2900
									#"data"為實際的訊息內容
2900
									#"data"為實際的訊息內容
2901
									$packedMsg=array("type"=>"msg","index"=>$unCMindex,"data"=>$unCMinfo["msg"]);
2901
									$packedMsg=array("type"=>"msg","index"=>$unCMindex,"data"=>$unCMinfo["msg"]);
2902
										
2902
 
2903
									#傳送包裝好的待確認訊息給對方
2903
									#傳送包裝好的待確認訊息給對方
2904
									$from->send(json_encode($packedMsg));
2904
									$from->send(json_encode($packedMsg));
2905
									
2905
 
2906
									#一次只送一則訊息
2906
									#一次只送一則訊息
2907
									break;
2907
									break;
2908
									
2908
 
2909
									}#foreach end
2909
									}#foreach end
2910
								
2910
 
2911
								}#if end
2911
								}#if end
2912
							
2912
 
2913
							}#if end
2913
							}#if end
2914
						
2914
 
2915
						}#if end
2915
						}#if end
2916
					
2916
 
2917
					#反之為要傳送的訊息
2917
					#反之為要傳送的訊息
2918
					else{
2918
					else{
2919
						
2919
 
2920
						#儲存對話目標的msgId
2920
						#儲存對話目標的msgId
2921
						$targetMsgId=$this->connInfo[$cInfo["id"]]["msgId"];
2921
						$targetMsgId=$this->connInfo[$cInfo["id"]]["msgId"];
2922
						
2922
 
2923
						#$unConfirmMsg[$msgId],為訊息接收方的$msgId,亦即收到訊息後,要跟server說有收到訊息者.
2923
						#$unConfirmMsg[$msgId],為訊息接收方的$msgId,亦即收到訊息後,要跟server說有收到訊息者.
2924
						#"fromId",訊息來源的id
2924
						#"fromId",訊息來源的id
2925
						#"toId",訊息目的的id
2925
						#"toId",訊息目的的id
2926
						#"fromUserId",訊息來源的userId
2926
						#"fromUserId",訊息來源的userId
2927
						#"toUserId",訊息目標的userId
2927
						#"toUserId",訊息目標的userId
2928
						#"msg",傳送的訊息
2928
						#"msg",傳送的訊息
2929
						#"msgId",訊息目標連線的msgId
2929
						#"msgId",訊息目標連線的msgId
2930
						#儲存待確認傳送的訊息到 $unConfirmMsg
2930
						#儲存待確認傳送的訊息到 $unConfirmMsg
2931
						$this->unConfirmMsg[$targetMsgId][]=array('fromId'=>$from->resourceId,'toId'=>$cInfo["id"],'fromUserId'=>$this->connInfo[$from->resourceId]["userId"],'toUserId'=>$cInfo["userId"],'msg'=>$msg,'msgId'=>$this->connInfo[$cInfo["id"]]["msgId"]);
2931
						$this->unConfirmMsg[$targetMsgId][]=array('fromId'=>$from->resourceId,'toId'=>$cInfo["id"],'fromUserId'=>$this->connInfo[$from->resourceId]["userId"],'toUserId'=>$cInfo["userId"],'msg'=>$msg,'msgId'=>$this->connInfo[$cInfo["id"]]["msgId"]);
2932
									
2932
 
2933
						#var_dump($this->unConfirmMsg[$targetMsgId]);			
2933
						#var_dump($this->unConfirmMsg[$targetMsgId]);
2934
												
2934
 
2935
						#如果未確認的訊息數量已經達到10則
2935
						#如果未確認的訊息數量已經達到10則
2936
						if(count($this->unConfirmMsg[$targetMsgId])>10){
2936
						if(count($this->unConfirmMsg[$targetMsgId])>10){
2937
							
2937
 
2938
							#代表用戶無法透過網路跟server溝通
2938
							#代表用戶無法透過網路跟server溝通
2939
														
2939
 
2940
							#提示 server 連線已結束
2940
							#提示 server 連線已結束
2941
							echo "Connection {$cInfo["id"]} has disconnected\n";
2941
							echo "Connection {$cInfo["id"]} has disconnected\n";
2942
							
2942
 
2943
							#將待驗證的訊息變成留言訊息
2943
							#將待驗證的訊息變成留言訊息
2944
							
2944
 
2945
							#有幾個未確認訊息就執行幾次
2945
							#有幾個未確認訊息就執行幾次
2946
							foreach($this->unConfirmMsg[$targetMsgId] as $unCMindex => $unCMinfo){
2946
							foreach($this->unConfirmMsg[$targetMsgId] as $unCMindex => $unCMinfo){
2947
								
2947
 
2948
								#將訊息儲存起來,等對象上線後再把訊息傳過去.
2948
								#將訊息儲存起來,等對象上線後再把訊息傳過去.
2949
								$this->unSendMsg[]=array(
2949
								$this->unSendMsg[]=array(
2950
									"fromId"=>$unCMinfo["fromId"],
2950
									"fromId"=>$unCMinfo["fromId"],
2951
									"toId"=>"",
2951
									"toId"=>"",
2952
									"fromUserId"=>$unCMinfo["fromUserId"],
2952
									"fromUserId"=>$unCMinfo["fromUserId"],
2953
									"toUserId"=>$unCMinfo["toUserId"],
2953
									"toUserId"=>$unCMinfo["toUserId"],
2954
									"msg"=>$unCMinfo["msg"]
2954
									"msg"=>$unCMinfo["msg"]
2955
									);
2955
									);
2956
								
2956
 
2957
								}#foreach end
2957
								}#foreach end
2958
							
2958
 
2959
							#移除待驗證的訊息資訊
2959
							#移除待驗證的訊息資訊
2960
							unset($this->unConfirmMsg[$targetMsgId]);
2960
							unset($this->unConfirmMsg[$targetMsgId]);
2961
							
2961
 
2962
							#將client的連線斷開
2962
							#將client的連線斷開
2963
							$this->clients->detach($this->connInfo[$cInfo["id"]]["conn"]);
2963
							$this->clients->detach($this->connInfo[$cInfo["id"]]["conn"]);
2964
							
2964
 
2965
							#移除連線的資訊
2965
							#移除連線的資訊
2966
							unset($this->connInfo[$cInfo["id"]]);
2966
							unset($this->connInfo[$cInfo["id"]]);
2967
							
2967
 
2968
							}#if end
2968
							}#if end
2969
						
2969
 
2970
						#反之
2970
						#反之
2971
						else{
2971
						else{
2972
							
2972
 
2973
							#裡面有幾個待確認的訊息就執行幾次
2973
							#裡面有幾個待確認的訊息就執行幾次
2974
							foreach($this->unConfirmMsg[$targetMsgId] as $unCMindex=>$unCMinfo){
2974
							foreach($this->unConfirmMsg[$targetMsgId] as $unCMindex=>$unCMinfo){
2975
								
2975
 
2976
								#如果後面還有訊息
2976
								#如果後面還有訊息
2977
								if(isset($this->unConfirmMsg[$targetMsgId][$unCMindex+1])){
2977
								if(isset($this->unConfirmMsg[$targetMsgId][$unCMindex+1])){
2978
									
2978
 
2979
									#debug
2979
									#debug
2980
									#var_dump($this->unConfirmMsg[$targetMsgId]);
2980
									#var_dump($this->unConfirmMsg[$targetMsgId]);
2981
									
2981
 
2982
									#如果與下一份訊息的 fromUserId一樣, toUserId 一樣, fromId一樣, toId 不同, msg 一樣.
2982
									#如果與下一份訊息的 fromUserId一樣, toUserId 一樣, fromId一樣, toId 不同, msg 一樣.
2983
									if(
2983
									if(
2984
										(
2984
										(
2985
											$this->unConfirmMsg[$targetMsgId][$unCMindex]["fromUserId"]===$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["fromUserId"]
2985
											$this->unConfirmMsg[$targetMsgId][$unCMindex]["fromUserId"]===$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["fromUserId"]
2986
											)
2986
											)
Line 2993... Line 2993...
2993
											$this->unConfirmMsg[$targetMsgId][$unCMindex]["fromId"]===$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["fromId"]
2993
											$this->unConfirmMsg[$targetMsgId][$unCMindex]["fromId"]===$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["fromId"]
2994
											)
2994
											)
2995
										&&
2995
										&&
2996
										(
2996
										(
2997
											$this->unConfirmMsg[$targetMsgId][$unCMindex]["toId"]!==$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["toId"]
2997
											$this->unConfirmMsg[$targetMsgId][$unCMindex]["toId"]!==$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["toId"]
2998
											)	
2998
											)
2999
										&&
2999
										&&
3000
										(
3000
										(
3001
											$this->unConfirmMsg[$targetMsgId][$unCMindex]["msg"]===$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["msg"]
3001
											$this->unConfirmMsg[$targetMsgId][$unCMindex]["msg"]===$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["msg"]
3002
											)									
3002
											)
3003
										){
3003
										){
3004
										
3004
 
3005
										#卸除該筆重複的訊息
3005
										#卸除該筆重複的訊息
3006
										unset($this->unConfirmMsg[$targetMsgId][$unCMindex]);
3006
										unset($this->unConfirmMsg[$targetMsgId][$unCMindex]);
3007
											
3007
 
3008
										#跳到下一則訊息
3008
										#跳到下一則訊息
3009
										continue;	
3009
										continue;
3010
											
3010
 
3011
										}#if end
3011
										}#if end
3012
									
3012
 
3013
									/*	
3013
									/*
3014
									#debug
3014
									#debug
3015
									else{
3015
									else{
3016
										
3016
 
3017
										var_dump($this->unConfirmMsg[$targetMsgId][$unCMindex]["toUserId"].":".$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["toUserId"]);
3017
										var_dump($this->unConfirmMsg[$targetMsgId][$unCMindex]["toUserId"].":".$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["toUserId"]);
3018
										var_dump($this->unConfirmMsg[$targetMsgId][$unCMindex]["toId"].":".$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["toId"]);
3018
										var_dump($this->unConfirmMsg[$targetMsgId][$unCMindex]["toId"].":".$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["toId"]);
3019
										var_dump($this->unConfirmMsg[$targetMsgId][$unCMindex]["msg"].":".$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["msg"]);
3019
										var_dump($this->unConfirmMsg[$targetMsgId][$unCMindex]["msg"].":".$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["msg"]);
3020
										
3020
 
3021
										}#else end
3021
										}#else end
3022
									*/
3022
									*/
3023
									
3023
 
3024
									}#if end
3024
									}#if end
3025
								
3025
 
3026
								#debug
3026
								#debug
3027
								#var_dump("index:".$targetMsgId.print_r($this->unConfirmMsg[$targetMsgId],true));
3027
								#var_dump("index:".$targetMsgId.print_r($this->unConfirmMsg[$targetMsgId],true));
3028
																
-
 
-
 
3028
 
3029
								#另存 ConnectionInterface 物件
3029
								#另存 ConnectionInterface 物件
3030
								$connObject=$this->connInfo[$cInfo["id"]]["conn"];
3030
								$connObject=$this->connInfo[$cInfo["id"]]["conn"];
3031
									
3031
 
3032
								#包裝訊息
3032
								#包裝訊息
3033
								#"type"為"msg"
3033
								#"type"為"msg"
3034
								#"index"為$cInfo["id"]
3034
								#"index"為$cInfo["id"]
3035
								#"data"為實際的訊息內容
3035
								#"data"為實際的訊息內容
3036
								$packedMsg=array("type"=>"msg","index"=>$unCMindex,"data"=>$unCMinfo["msg"]);
3036
								$packedMsg=array("type"=>"msg","index"=>$unCMindex,"data"=>$unCMinfo["msg"]);
3037
									
3037
 
3038
								#傳送包裝好的訊息給對方
3038
								#傳送包裝好的訊息給對方
3039
								$connObject->send(json_encode($packedMsg));
3039
								$connObject->send(json_encode($packedMsg));
3040
								
3040
 
3041
								#一次只送一則訊息
3041
								#一次只送一則訊息
3042
								break;
3042
								break;
3043
								
3043
 
3044
								}#foreach end
3044
								}#foreach end
3045
							
3045
 
3046
							}#else end
3046
							}#else end
3047
						
3047
 
3048
						}#else end	
3048
						}#else end
3049
					
3049
 
3050
					}#else end
3050
					}#else end
3051
				
3051
 
3052
				}#foreach end
3052
				}#foreach end
3053
			
3053
 
3054
			}#if end
3054
			}#if end
3055
			
3055
 
3056
		#反之沒有講話的對象
3056
		#反之沒有講話的對象
3057
		else{
3057
		else{
3058
			
3058
 
3059
			$msg="Message 「".$msg."」 will not be received by any one";
3059
			$msg="Message 「".$msg."」 will not be received by any one";
3060
			
3060
 
3061
			#提示server訊息被拋棄
3061
			#提示server訊息被拋棄
3062
			echo $msg;
3062
			echo $msg;
3063
			
3063
 
3064
			#包裝訊息
3064
			#包裝訊息
3065
			#"type"為"status"	
3065
			#"type"為"status"
3066
			#"status"為"false"		
3066
			#"status"為"false"
3067
			#"data"為實際的訊息內容
3067
			#"data"為實際的訊息內容
3068
			$packedMsg=array("type"=>"status","status"=>"false","data"=>$msg);
3068
			$packedMsg=array("type"=>"status","status"=>"false","data"=>$msg);
3069
			
3069
 
3070
			#設置要給自己看的訊息
3070
			#設置要給自己看的訊息
3071
			$from->send(json_encode($packedMsg));
3071
			$from->send(json_encode($packedMsg));
3072
			
3072
 
3073
			}#else end
3073
			}#else end
3074
			
3074
 
3075
		}#function onMessage end
3075
		}#function onMessage end
3076
 
3076
 
3077
	#當clinet的連線斷掉前
3077
	#當clinet的連線斷掉前
3078
    	public function onClose(ConnectionInterface $conn){
3078
    	public function onClose(ConnectionInterface $conn){
3079
		
3079
 
3080
		// The connection is closed, remove it, as we can no longer send it messages
3080
		// The connection is closed, remove it, as we can no longer send it messages
3081
        	$this->clients->detach($conn);
3081
        	$this->clients->detach($conn);
3082
	
3082
 
3083
		#提示 server 連線已結束
3083
		#提示 server 連線已結束
3084
        	echo "Connection {$conn->resourceId} has disconnected\n";
3084
        	echo "Connection {$conn->resourceId} has disconnected\n";
3085
		
3085
 
3086
		#取得斷線目標的待確認訊息id
3086
		#取得斷線目標的待確認訊息id
3087
		$msgId=$this->connInfo[$conn->resourceId]["msgId"];
3087
		$msgId=$this->connInfo[$conn->resourceId]["msgId"];
3088
		
3088
 
3089
		#移除目標id的待確認訊息
3089
		#移除目標id的待確認訊息
3090
		unset($this->unConfirmMsg[$msgId]);
3090
		unset($this->unConfirmMsg[$msgId]);
3091
		
3091
 
3092
		#移除連線的資訊
3092
		#移除連線的資訊
3093
		unset($this->connInfo[$conn->resourceId]);
3093
		unset($this->connInfo[$conn->resourceId]);
3094
		
3094
 
3095
		}#function onClose end
3095
		}#function onClose end
3096
 
3096
 
3097
		#當出現錯誤時
3097
		#當出現錯誤時
3098
    		public function onError(ConnectionInterface $conn, \Exception $e){
3098
    		public function onError(ConnectionInterface $conn, \Exception $e){
3099
		
3099
 
3100
		#提示出現連線錯誤
3100
		#提示出現連線錯誤
3101
		echo "An error has occurred: {$e->getMessage()}\n";
3101
		echo "An error has occurred: {$e->getMessage()}\n";
3102
 
3102
 
3103
		#關閉socket
3103
		#關閉socket
3104
        	$conn->close();
3104
        	$conn->close();
3105
        
3105
 
3106
		}#fucntion onError end
3106
		}#fucntion onError end
3107
    
3107
 
3108
	}#class ChatV2 end
3108
	}#class ChatV2 end
3109
	
3109
 
3110
#用 ChatV3 類別來實作 MessageComponentInterface 界面,
3110
#用 ChatV3 類別來實作 MessageComponentInterface 界面,
3111
#新增可以對Server下達command的功能
3111
#新增可以對Server下達command的功能
3112
#修正要設置的msgId不存在時不會回傳錯誤訊息且繼續執行的問題
3112
#修正要設置的msgId不存在時不會回傳錯誤訊息且繼續執行的問題
3113
class ChatV3 implements MessageComponentInterface{
3113
class ChatV3 implements MessageComponentInterface{
3114
		
3114
 
3115
	#初始化儲存使用者資訊的陣列
3115
	#初始化儲存使用者資訊的陣列
3116
	private $connInfo=array();	
3116
	private $connInfo=array();
3117
	
3117
 
3118
	#初始化儲存未傳送出去的留言訊息
3118
	#初始化儲存未傳送出去的留言訊息
3119
	#"fromId",訊息來源的id
3119
	#"fromId",訊息來源的id
3120
	#"toId",訊息目的的id
3120
	#"toId",訊息目的的id
3121
	#"fromUserId",訊息來源的userId
3121
	#"fromUserId",訊息來源的userId
3122
	#"toUserId",訊息目標的userId
3122
	#"toUserId",訊息目標的userId
3123
	#"msg",傳送的訊息
3123
	#"msg",傳送的訊息
3124
	private $unSendMsg=array();
3124
	private $unSendMsg=array();
3125
	
3125
 
3126
	#初始化儲存待確認是否送達到對方的訊息
3126
	#初始化儲存待確認是否送達到對方的訊息
3127
	#$unConfirmMsg[$id],$id為訊息接收方,亦即收到訊息後,要跟server說有收到訊息者.
3127
	#$unConfirmMsg[$id],$id為訊息接收方,亦即收到訊息後,要跟server說有收到訊息者.
3128
	#"fromId",訊息來源的id
3128
	#"fromId",訊息來源的id
3129
	#"toId",訊息目的的id
3129
	#"toId",訊息目的的id
3130
	#"fromUserId",訊息來源的userId
3130
	#"fromUserId",訊息來源的userId
3131
	#"toUserId",訊息目標的userId
3131
	#"toUserId",訊息目標的userId
3132
	#"msg",傳送的訊息
3132
	#"msg",傳送的訊息
3133
	private $unConfirmMsg=array();
3133
	private $unConfirmMsg=array();
3134
	
3134
 
3135
	#初始化一開始尚未能夠連線到資料庫
3135
	#初始化一開始尚未能夠連線到資料庫
3136
	private static $dbTouchable=false;
3136
	private static $dbTouchable=false;
3137
		
3137
 
3138
	#初始化db的位置
3138
	#初始化db的位置
3139
	public static $dbAddress="localhost";	
3139
	public static $dbAddress="localhost";
3140
		
3140
 
3141
	#初始化連線db用的帳戶
3141
	#初始化連線db用的帳戶
3142
	public static $dbAccount="root";
3142
	public static $dbAccount="root";
3143
	
3143
 
3144
	#初始化連線的db名稱
3144
	#初始化連線的db名稱
3145
	public static $dbName="test";
3145
	public static $dbName="test";
3146
	
3146
 
3147
	#初始化連線db時用的密碼
3147
	#初始化連線db時用的密碼
3148
	public static $dbPassword="";
3148
	public static $dbPassword="";
3149
	
3149
 
3150
	#初始化連線db的哪個資料表
3150
	#初始化連線db的哪個資料表
3151
	public static $memberTableName="member";
3151
	public static $memberTableName="member";
3152
		
3152
 
3153
	#初始化登入時用來驗證帳號的資料表欄位名稱
3153
	#初始化登入時用來驗證帳號的資料表欄位名稱
3154
	public static $accountCol="account";
3154
	public static $accountCol="account";
3155
	
3155
 
3156
	#初始化登入時用來驗證密碼的資料表欄位名稱,設為""代表不用密碼來認證
3156
	#初始化登入時用來驗證密碼的資料表欄位名稱,設為""代表不用密碼來認證
3157
	public static $passwordCol="password";
3157
	public static $passwordCol="password";
3158
		
3158
 
3159
	#初始化用來儲存 __FILE__ 內容的變數 fileArgu
3159
	#初始化用來儲存 __FILE__ 內容的變數 fileArgu
3160
	public static $fileArgu=__FILE__;	
3160
	public static $fileArgu=__FILE__;
3161
		
3161
 
3162
	#建構子
3162
	#建構子
3163
	public function __construct(){
3163
	public function __construct(){
3164
		
3164
 
3165
		#如果尚未可以上資料庫
3165
		#如果尚未可以上資料庫
3166
		if(!(self::$dbTouchable)){
3166
		if(!(self::$dbTouchable)){
3167
			
3167
 
3168
			#嘗試連線到目標資料庫.資料表
3168
			#嘗試連線到目標資料庫.資料表
3169
			#涵式說明:
3169
			#涵式說明:
3170
			#一次取得資料庫、表的資料
3170
			#一次取得資料庫、表的資料
3171
			#回傳的結果
3171
			#回傳的結果
3172
			#$result["status"],執行結果"true"為成功;"false"為執行失敗。
3172
			#$result["status"],執行結果"true"為成功;"false"為執行失敗。
Line 3184... Line 3184...
3184
			$conf["db::fastGetDbData"]["dbAddress"]=self::$dbAddress;#爲dbServer的位置。
3184
			$conf["db::fastGetDbData"]["dbAddress"]=self::$dbAddress;#爲dbServer的位置。
3185
			$conf["db::fastGetDbData"]["dbAccount"]=self::$dbAccount;#爲登入dbServer的帳號。
3185
			$conf["db::fastGetDbData"]["dbAccount"]=self::$dbAccount;#爲登入dbServer的帳號。
3186
			$conf["db::fastGetDbData"]["dbName"]=self::$dbName;#爲要存取的資料庫名稱
3186
			$conf["db::fastGetDbData"]["dbName"]=self::$dbName;#爲要存取的資料庫名稱
3187
			$conf["db::fastGetDbData"]["tableName"]=self::$memberTableName;#爲要存取的資料表名稱
3187
			$conf["db::fastGetDbData"]["tableName"]=self::$memberTableName;#爲要存取的資料表名稱
3188
			$conf["db::fastGetDbData"]["columnYouWant"]=array("id",self::$accountCol);#你想要的欄位!,若設為「array("*")」則代表全部欄位.
3188
			$conf["db::fastGetDbData"]["columnYouWant"]=array("id",self::$accountCol);#你想要的欄位!,若設為「array("*")」則代表全部欄位.
3189
			
3189
 
3190
			#如果 $passwordCol 不為空
3190
			#如果 $passwordCol 不為空
3191
			if(self::$passwordCol!=""){
3191
			if(self::$passwordCol!=""){
3192
				
3192
 
3193
				#加上密碼欄位
3193
				#加上密碼欄位
3194
				$conf["db::fastGetDbData"]["columnYouWant"][]=self::$passwordCol;
3194
				$conf["db::fastGetDbData"]["columnYouWant"][]=self::$passwordCol;
3195
				
3195
 
3196
				}#if end
3196
				}#if end
3197
			
3197
 
3198
			#可省略的參數:
3198
			#可省略的參數:
3199
			$conf["db::fastGetDbData"]["dbPassword"]=self::$dbPassword;#爲要存取dbServer的密碼
3199
			$conf["db::fastGetDbData"]["dbPassword"]=self::$dbPassword;#爲要存取dbServer的密碼
3200
			#$conf["db::fastGetDbData"]["WhereColumnName"]=array("account","password");#用於判斷語句的欄位項目陣列。
3200
			#$conf["db::fastGetDbData"]["WhereColumnName"]=array("account","password");#用於判斷語句的欄位項目陣列。
3201
			#$conf["db::fastGetDbData"]["WhereColumnValue"]=array($ac,$pw);#用於判斷語句的欄位數值陣列,若與LIKE搭配,則可以在關鍵自字串的左右名加上「%」符號,這樣就可以搜尋具有該字串的內容。
3201
			#$conf["db::fastGetDbData"]["WhereColumnValue"]=array($ac,$pw);#用於判斷語句的欄位數值陣列,若與LIKE搭配,則可以在關鍵自字串的左右名加上「%」符號,這樣就可以搜尋具有該字串的內容。
3202
			#$conf["WhereColumnCombine"]=array("");#用於判斷語句當中需要()起來的判斷式,須爲陣列值,"s"代表「(」,"e"代表「)」 ,若無則須設爲""。
3202
			#$conf["WhereColumnCombine"]=array("");#用於判斷語句當中需要()起來的判斷式,須爲陣列值,"s"代表「(」,"e"代表「)」 ,若無則須設爲""。
Line 3209... Line 3209...
3209
			#$conf["groupBy"]=array("");#爲要以哪幾個欄爲作爲分羣的依據(欄位相同的數值僅會取出一筆)。
3209
			#$conf["groupBy"]=array("");#爲要以哪幾個欄爲作爲分羣的依據(欄位相同的數值僅會取出一筆)。
3210
			#備註:
3210
			#備註:
3211
			#建議在查詢資料前,能夠檢查是否每個欄位都存在.
3211
			#建議在查詢資料前,能夠檢查是否每個欄位都存在.
3212
			$fastGetDbData=db::fastGetDbData($conf["db::fastGetDbData"]);
3212
			$fastGetDbData=db::fastGetDbData($conf["db::fastGetDbData"]);
3213
			unset($conf["db::fastGetDbData"]);
3213
			unset($conf["db::fastGetDbData"]);
3214
			
3214
 
3215
			#如果取得資料失敗
3215
			#如果取得資料失敗
3216
			if($fastGetDbData["status"]==="false"){
3216
			if($fastGetDbData["status"]==="false"){
3217
				
3217
 
3218
				#設置執行失敗
3218
				#設置執行失敗
3219
				$result["status"]="false";
3219
				$result["status"]="false";
3220
			
3220
 
3221
				#設置執行錯誤訊息
3221
				#設置執行錯誤訊息
3222
				$result["error"]=$fastGetDbData;
3222
				$result["error"]=$fastGetDbData;
3223
				
3223
 
3224
				#印出debug訊息
3224
				#印出debug訊息
3225
				var_dump($result);
3225
				var_dump($result);
3226
				
3226
 
3227
				#結束程式
3227
				#結束程式
3228
				exit;
3228
				exit;
3229
				
3229
 
3230
				}#if end
3230
				}#if end
3231
				
3231
 
3232
			#連線成功,將self::dbTouchable設為true
3232
			#連線成功,將self::dbTouchable設為true
3233
			self::$dbTouchable=true;
3233
			self::$dbTouchable=true;
3234
			
3234
 
3235
			}#if end
3235
			}#if end
3236
		
3236
 
3237
		$this->clients = new \SplObjectStorage;
3237
		$this->clients = new \SplObjectStorage;
3238
 
3238
 
3239
		}#funciton __construct end
3239
		}#funciton __construct end
3240
	
3240
 
3241
	#當用戶與伺服器建立連線時
3241
	#當用戶與伺服器建立連線時
3242
	public function onOpen(ConnectionInterface $conn){
3242
	public function onOpen(ConnectionInterface $conn){
3243
		
3243
 
3244
		// Store the new connection to send messages to later
3244
		// Store the new connection to send messages to later
3245
		 $this->clients->attach($conn);
3245
		 $this->clients->attach($conn);
3246
 
3246
 
3247
		#提示server有clent連上web socket.
3247
		#提示server有clent連上web socket.
3248
		echo "New connection! ({$conn->resourceId})\n";
3248
		echo "New connection! ({$conn->resourceId})\n";
Line 3254... Line 3254...
3254
		#元素 "loigin=array("ac","pw")" 代表client輸入的帳號與密碼
3254
		#元素 "loigin=array("ac","pw")" 代表client輸入的帳號與密碼
3255
		#元素 "msgId" 為用來識別待確認訊息的索引
3255
		#元素 "msgId" 為用來識別待確認訊息的索引
3256
		#元素 "gotMsgId" 為用來識別是否已經設置過 msgId 了
3256
		#元素 "gotMsgId" 為用來識別是否已經設置過 msgId 了
3257
	 	#$this->connInfo[$conn->resourceId]=array("conn"=>$conn,"talkTo"=>array(),"userId"=>"","login"=>array("ac"=>"","pw"=>""),"type"=>"");
3257
	 	#$this->connInfo[$conn->resourceId]=array("conn"=>$conn,"talkTo"=>array(),"userId"=>"","login"=>array("ac"=>"","pw"=>""),"type"=>"");
3258
		$this->connInfo[$conn->resourceId]=array("conn"=>$conn,"talkTo"=>array(),"userId"=>"","login"=>array("ac"=>"","pw"=>""),"msgId"=>"","gotMsgId"=>"false");
3258
		$this->connInfo[$conn->resourceId]=array("conn"=>$conn,"talkTo"=>array(),"userId"=>"","login"=>array("ac"=>"","pw"=>""),"msgId"=>"","gotMsgId"=>"false");
3259
		
3259
 
3260
		#包裝訊息
3260
		#包裝訊息
3261
		#"type"為"login"	
3261
		#"type"為"login"
3262
		#"status"為"true"		
3262
		#"status"為"true"
3263
		#"data"為實際的訊息內容
3263
		#"data"為實際的訊息內容
3264
		$packedMsg=array("type"=>"login","status"=>"true","data"=>"Please input your account! ex:account:ws1");
3264
		$packedMsg=array("type"=>"login","status"=>"true","data"=>"Please input your account! ex:account:ws1");
3265
		
3265
 
3266
		#提示輸入帳號	
3266
		#提示輸入帳號
3267
		$conn->send(json_encode($packedMsg));
3267
		$conn->send(json_encode($packedMsg));
3268
				
3268
 
3269
		}#function onOpen end
3269
		}#function onOpen end
3270
 
3270
 
3271
	#當伺服器收到訊息時
3271
	#當伺服器收到訊息時
3272
	public function onMessage(ConnectionInterface $from, $msg){
3272
	public function onMessage(ConnectionInterface $from, $msg){
3273
		
3273
 
3274
		#debug
3274
		#debug
3275
		#$array=array("msgId"=>$this->connInfo[$from->resourceId]["msgId"],"resourceId"=>$from->resourceId,"msg"=>$msg);
3275
		#$array=array("msgId"=>$this->connInfo[$from->resourceId]["msgId"],"resourceId"=>$from->resourceId,"msg"=>$msg);
3276
		#var_dump($array);
3276
		#var_dump($array);
3277
		
3277
 
3278
		#如果該連線沒有使用者id
3278
		#如果該連線沒有使用者id
3279
		if($this->connInfo[$from->resourceId]["userId"]===""){
3279
		if($this->connInfo[$from->resourceId]["userId"]===""){
3280
			
3280
 
3281
			#代表需要認證,認證的會員資料表 slef::memberTableName,需要以下三個欄位.
3281
			#代表需要認證,認證的會員資料表 slef::memberTableName,需要以下三個欄位.
3282
			#id		  自訂
3282
			#id		  自訂
3283
			#account  自訂
3283
			#account  自訂
3284
			#password varchar(33)
3284
			#password varchar(33)
3285
			
3285
 
3286
			/*
3286
			/*
3287
			
3287
 
3288
			#如果尚未輸入用戶端類型
3288
			#如果尚未輸入用戶端類型
3289
			if($this->connInfo[$from->resourceId]["type"]===""){
3289
			if($this->connInfo[$from->resourceId]["type"]===""){
3290
				
3290
 
3291
				#如果 $msg 長度大於 "type:"
3291
				#如果 $msg 長度大於 "type:"
3292
				if(strlen($msg) > strlen("type:")){
3292
				if(strlen($msg) > strlen("type:")){
3293
					
3293
 
3294
					#檢查有無前置字元 "account:"
3294
					#檢查有無前置字元 "account:"
3295
					#函式說明:
3295
					#函式說明:
3296
					#將字串特定關鍵字與其前面的內容剔除
3296
					#將字串特定關鍵字與其前面的內容剔除
3297
					#回傳結果:
3297
					#回傳結果:
3298
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
3298
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 3305... Line 3305...
3305
					#必填的參數:
3305
					#必填的參數:
3306
					$conf["stringProcess::delStrBeforeKeyWord"]["stringIn"]=$msg;#要處理的字串.
3306
					$conf["stringProcess::delStrBeforeKeyWord"]["stringIn"]=$msg;#要處理的字串.
3307
					$conf["stringProcess::delStrBeforeKeyWord"]["keyWord"]="type:";#特定字串.
3307
					$conf["stringProcess::delStrBeforeKeyWord"]["keyWord"]="type:";#特定字串.
3308
					$delStrBeforeKeyWord=stringProcess::delStrBeforeKeyWord($conf["stringProcess::delStrBeforeKeyWord"]);
3308
					$delStrBeforeKeyWord=stringProcess::delStrBeforeKeyWord($conf["stringProcess::delStrBeforeKeyWord"]);
3309
					unset($conf["stringProcess::delStrBeforeKeyWord"]);
3309
					unset($conf["stringProcess::delStrBeforeKeyWord"]);
3310
					
3310
 
3311
					#如果移除前置字元失敗
3311
					#如果移除前置字元失敗
3312
					if($delStrBeforeKeyWord["status"]==="false"){
3312
					if($delStrBeforeKeyWord["status"]==="false"){
3313
						
3313
 
3314
						#設置執行失敗
3314
						#設置執行失敗
3315
						$result["status"]="false";
3315
						$result["status"]="false";
3316
					
3316
 
3317
						#設置執行錯誤訊息
3317
						#設置執行錯誤訊息
3318
						$result["error"]=$delStrBeforeKeyWord;
3318
						$result["error"]=$delStrBeforeKeyWord;
3319
						
3319
 
3320
						#回傳結果
3320
						#回傳結果
3321
						return $result;
3321
						return $result;
3322
						
3322
 
3323
						}#if end
3323
						}#if end
3324
						
3324
 
3325
					#如果有符合條件的前置字元
3325
					#如果有符合條件的前置字元
3326
					if($delStrBeforeKeyWord["founded"]==="true"){
3326
					if($delStrBeforeKeyWord["founded"]==="true"){
3327
						
3327
 
3328
						#儲存用戶端類型
3328
						#儲存用戶端類型
3329
						$this->connInfo[$from->resourceId]["type"]=$delStrBeforeKeyWord["content"];
3329
						$this->connInfo[$from->resourceId]["type"]=$delStrBeforeKeyWord["content"];
3330
																							
-
 
-
 
3330
 
3331
						}#if end
3331
						}#if end
3332
					
3332
 
3333
					#反之
3333
					#反之
3334
					else{
3334
					else{
3335
						
3335
 
3336
						#提示輸入用戶端類型
3336
						#提示輸入用戶端類型
3337
						$from->send(json_encode("Please input your type! ex:type:user"));
3337
						$from->send(json_encode("Please input your type! ex:type:user"));
3338
						
3338
 
3339
						#結束程式
3339
						#結束程式
3340
						return true;
3340
						return true;
3341
						
3341
 
3342
						}#else end
3342
						}#else end
3343
					
3343
 
3344
					}#if end
3344
					}#if end
3345
					
3345
 
3346
				#反之代表輸入錯誤格式的type
3346
				#反之代表輸入錯誤格式的type
3347
				else{
3347
				else{
3348
					
3348
 
3349
					#提示輸入用戶端類型
3349
					#提示輸入用戶端類型
3350
					$from->send(json_encode("Please input your type! ex:type:user"));
3350
					$from->send(json_encode("Please input your type! ex:type:user"));
3351
										
3351
 
3352
					#結束程式
3352
					#結束程式
3353
					return true;
3353
					return true;
3354
					
3354
 
3355
					}#else end
3355
					}#else end
3356
				
3356
 
3357
				}#if end
3357
				}#if end
3358
							
3358
 
3359
			#如果用戶端是"user"
3359
			#如果用戶端是"user"
3360
			if($this->connInfo[$from->resourceId]["type"]==="user"){
3360
			if($this->connInfo[$from->resourceId]["type"]==="user"){
3361
				
3361
 
3362
				#...
3362
				#...
3363
				
3363
 
3364
				}#if end
3364
				}#if end
3365
				
3365
 
3366
			#反之如果用戶端是"device"
3366
			#反之如果用戶端是"device"
3367
			else if($this->connInfo[$from->resourceId]["type"]==="device"){
3367
			else if($this->connInfo[$from->resourceId]["type"]==="device"){
3368
					
3368
 
3369
				#...
3369
				#...
3370
					
3370
 
3371
				}#if end
3371
				}#if end
3372
				
3372
 
3373
			*/
3373
			*/
3374
			
3374
 
3375
			#如果尚未輸入帳戶
3375
			#如果尚未輸入帳戶
3376
			if($this->connInfo[$from->resourceId]["login"]["ac"]===""){
3376
			if($this->connInfo[$from->resourceId]["login"]["ac"]===""){
3377
			
3377
 
3378
				#如果 $msg 長度大於 "account:"
3378
				#如果 $msg 長度大於 "account:"
3379
				if(strlen($msg) > strlen("account:")){
3379
				if(strlen($msg) > strlen("account:")){
3380
					
3380
 
3381
					#檢查有無前置字元 "account:"
3381
					#檢查有無前置字元 "account:"
3382
					#函式說明:
3382
					#函式說明:
3383
					#將字串特定關鍵字與其前面的內容剔除
3383
					#將字串特定關鍵字與其前面的內容剔除
3384
					#回傳結果:
3384
					#回傳結果:
3385
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
3385
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 3392... Line 3392...
3392
					#必填的參數:
3392
					#必填的參數:
3393
					$conf["stringProcess::delStrBeforeKeyWord"]["stringIn"]=$msg;#要處理的字串.
3393
					$conf["stringProcess::delStrBeforeKeyWord"]["stringIn"]=$msg;#要處理的字串.
3394
					$conf["stringProcess::delStrBeforeKeyWord"]["keyWord"]="account:";#特定字串.
3394
					$conf["stringProcess::delStrBeforeKeyWord"]["keyWord"]="account:";#特定字串.
3395
					$delStrBeforeKeyWord=stringProcess::delStrBeforeKeyWord($conf["stringProcess::delStrBeforeKeyWord"]);
3395
					$delStrBeforeKeyWord=stringProcess::delStrBeforeKeyWord($conf["stringProcess::delStrBeforeKeyWord"]);
3396
					unset($conf["stringProcess::delStrBeforeKeyWord"]);
3396
					unset($conf["stringProcess::delStrBeforeKeyWord"]);
3397
					
3397
 
3398
					#如果移除前置字元失敗
3398
					#如果移除前置字元失敗
3399
					if($delStrBeforeKeyWord["status"]==="false"){
3399
					if($delStrBeforeKeyWord["status"]==="false"){
3400
						
3400
 
3401
						#設置執行失敗
3401
						#設置執行失敗
3402
						$result["status"]="false";
3402
						$result["status"]="false";
3403
					
3403
 
3404
						#設置執行錯誤訊息
3404
						#設置執行錯誤訊息
3405
						$result["error"]=$delStrBeforeKeyWord;
3405
						$result["error"]=$delStrBeforeKeyWord;
3406
						
3406
 
3407
						#回傳結果
3407
						#回傳結果
3408
						return $result;
3408
						return $result;
3409
						
3409
 
3410
						}#if end
3410
						}#if end
3411
						
3411
 
3412
					#如果有符合條件的前置字元
3412
					#如果有符合條件的前置字元
3413
					if($delStrBeforeKeyWord["founded"]==="true"){
3413
					if($delStrBeforeKeyWord["founded"]==="true"){
3414
						
3414
 
3415
						#儲存帳戶
3415
						#儲存帳戶
3416
						$this->connInfo[$from->resourceId]["login"]["ac"]=$delStrBeforeKeyWord["content"];
3416
						$this->connInfo[$from->resourceId]["login"]["ac"]=$delStrBeforeKeyWord["content"];
3417
												
3417
 
3418
						}#if end
3418
						}#if end
3419
					
3419
 
3420
					#反之代表格式錯誤	
3420
					#反之代表格式錯誤
3421
					else{
3421
					else{
3422
						
3422
 
3423
						#包裝訊息
3423
						#包裝訊息
3424
						#"type"為"login"	
3424
						#"type"為"login"
3425
						#"status"為"true"		
3425
						#"status"為"true"
3426
						#"data"為實際的訊息內容
3426
						#"data"為實際的訊息內容
3427
						$packedMsg=array("type"=>"login","status"=>"true","data"=>"Please input your account! ex:account:ws1");
3427
						$packedMsg=array("type"=>"login","status"=>"true","data"=>"Please input your account! ex:account:ws1");
3428
						
3428
 
3429
						#提示輸入帳號	
3429
						#提示輸入帳號
3430
						$from->send(json_encode($packedMsg));
3430
						$from->send(json_encode($packedMsg));
3431
						
3431
 
3432
						#結束程式
3432
						#結束程式
3433
						return true;
3433
						return true;
3434
						
3434
 
3435
						}#else end
3435
						}#else end
3436
					
3436
 
3437
					}#if end
3437
					}#if end
3438
					
3438
 
3439
				#反之代表輸入錯誤格式的account
3439
				#反之代表輸入錯誤格式的account
3440
				else{
3440
				else{
3441
					
3441
 
3442
					#包裝訊息
3442
					#包裝訊息
3443
					#"type"為"login"	
3443
					#"type"為"login"
3444
					#"status"為"true"		
3444
					#"status"為"true"
3445
					#"data"為實際的訊息內容
3445
					#"data"為實際的訊息內容
3446
					$packedMsg=array("type"=>"login","status"=>"true","data"=>"Please input your account! ex:account:ws1");
3446
					$packedMsg=array("type"=>"login","status"=>"true","data"=>"Please input your account! ex:account:ws1");
3447
					
3447
 
3448
					#提示輸入帳號	
3448
					#提示輸入帳號
3449
					$from->send(json_encode($packedMsg));
3449
					$from->send(json_encode($packedMsg));
3450
					
3450
 
3451
					#結束程式
3451
					#結束程式
3452
					return true;
3452
					return true;
3453
					
3453
 
3454
					}#else end
3454
					}#else end
3455
				
3455
 
3456
				}#if end
3456
				}#if end
3457
						
3457
 
3458
			#如果有設置密碼欄位
3458
			#如果有設置密碼欄位
3459
			if(self::$passwordCol!==""){
3459
			if(self::$passwordCol!==""){
3460
				
3460
 
3461
				#如果尚未輸入密碼
3461
				#如果尚未輸入密碼
3462
				if($this->connInfo[$from->resourceId]["login"]["pw"]===""){
3462
				if($this->connInfo[$from->resourceId]["login"]["pw"]===""){
3463
					
3463
 
3464
					#$msg 長度大於 "password:"
3464
					#$msg 長度大於 "password:"
3465
					if(strlen($msg) > strlen("password:")){
3465
					if(strlen($msg) > strlen("password:")){
3466
						
3466
 
3467
						#檢查有無前置字元 "password:"
3467
						#檢查有無前置字元 "password:"
3468
						#函式說明:
3468
						#函式說明:
3469
						#將字串特定關鍵字與其前面的內容剔除
3469
						#將字串特定關鍵字與其前面的內容剔除
3470
						#回傳結果:
3470
						#回傳結果:
3471
						#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
3471
						#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 3478... Line 3478...
3478
						#必填的參數:
3478
						#必填的參數:
3479
						$conf["stringProcess::delStrBeforeKeyWord"]["stringIn"]=$msg;#要處理的字串.
3479
						$conf["stringProcess::delStrBeforeKeyWord"]["stringIn"]=$msg;#要處理的字串.
3480
						$conf["stringProcess::delStrBeforeKeyWord"]["keyWord"]="password:";#特定字串.
3480
						$conf["stringProcess::delStrBeforeKeyWord"]["keyWord"]="password:";#特定字串.
3481
						$delStrBeforeKeyWord=stringProcess::delStrBeforeKeyWord($conf["stringProcess::delStrBeforeKeyWord"]);
3481
						$delStrBeforeKeyWord=stringProcess::delStrBeforeKeyWord($conf["stringProcess::delStrBeforeKeyWord"]);
3482
						unset($conf["stringProcess::delStrBeforeKeyWord"]);
3482
						unset($conf["stringProcess::delStrBeforeKeyWord"]);
3483
						
3483
 
3484
						#如果移除前置字元失敗
3484
						#如果移除前置字元失敗
3485
						if($delStrBeforeKeyWord["status"]==="false"){
3485
						if($delStrBeforeKeyWord["status"]==="false"){
3486
							
3486
 
3487
							#設置執行失敗
3487
							#設置執行失敗
3488
							$result["status"]="false";
3488
							$result["status"]="false";
3489
						
3489
 
3490
							#設置執行錯誤訊息
3490
							#設置執行錯誤訊息
3491
							$result["error"]=$delStrBeforeKeyWord;
3491
							$result["error"]=$delStrBeforeKeyWord;
3492
							
3492
 
3493
							#回傳結果
3493
							#回傳結果
3494
							return $result;
3494
							return $result;
3495
							
3495
 
3496
							}#if end
3496
							}#if end
3497
							
3497
 
3498
						#如果有符合條件的前置字元
3498
						#如果有符合條件的前置字元
3499
						if($delStrBeforeKeyWord["founded"]==="true"){
3499
						if($delStrBeforeKeyWord["founded"]==="true"){
3500
													
3500
 
3501
							#儲存密碼
3501
							#儲存密碼
3502
							$this->connInfo[$from->resourceId]["login"]["pw"]=$delStrBeforeKeyWord["content"];
3502
							$this->connInfo[$from->resourceId]["login"]["pw"]=$delStrBeforeKeyWord["content"];
3503
							
3503
 
3504
							}#if end
3504
							}#if end
3505
							
3505
 
3506
						#反之代表格式錯誤
3506
						#反之代表格式錯誤
3507
						else{
3507
						else{
3508
													
3508
 
3509
							#包裝訊息
3509
							#包裝訊息
3510
							#"type"為"login"	
3510
							#"type"為"login"
3511
							#"status"為"true"		
3511
							#"status"為"true"
3512
							#"data"為實際的訊息內容
3512
							#"data"為實際的訊息內容
3513
							$packedMsg=array("type"=>"login","status"=>"true","data"=>"Please input your password! ex:password:ws1");
3513
							$packedMsg=array("type"=>"login","status"=>"true","data"=>"Please input your password! ex:password:ws1");
3514
													
3514
 
3515
							#提示輸入密碼		
3515
							#提示輸入密碼
3516
							$from->send(json_encode($packedMsg));
3516
							$from->send(json_encode($packedMsg));
3517
							
3517
 
3518
							#結束程式
3518
							#結束程式
3519
							return true;
3519
							return true;
3520
												
3520
 
3521
							}#else end
3521
							}#else end
3522
						
3522
 
3523
						}#if end
3523
						}#if end
3524
						
3524
 
3525
					#反之代表格式錯誤
3525
					#反之代表格式錯誤
3526
					else{
3526
					else{
3527
						
3527
 
3528
						#包裝訊息
3528
						#包裝訊息
3529
						#"type"為"login"	
3529
						#"type"為"login"
3530
						#"status"為"true"		
3530
						#"status"為"true"
3531
						#"data"為實際的訊息內容
3531
						#"data"為實際的訊息內容
3532
						$packedMsg=array("type"=>"login","status"=>"true","data"=>"Please input your password! ex:password:ws1");
3532
						$packedMsg=array("type"=>"login","status"=>"true","data"=>"Please input your password! ex:password:ws1");
3533
												
3533
 
3534
						#提示輸入密碼		
3534
						#提示輸入密碼
3535
						$from->send(json_encode($packedMsg));
3535
						$from->send(json_encode($packedMsg));
3536
						
3536
 
3537
						#結束程式
3537
						#結束程式
3538
						return true;
3538
						return true;
3539
						
3539
 
3540
						}#else end
3540
						}#else end
3541
					
3541
 
3542
					}#if end
3542
					}#if end
3543
				
3543
 
3544
				}#if end
3544
				}#if end
3545
			
3545
 
3546
			#如果已經輸入帳號了
3546
			#如果已經輸入帳號了
3547
			if($this->connInfo[$from->resourceId]["login"]["ac"]!=""){
3547
			if($this->connInfo[$from->resourceId]["login"]["ac"]!=""){
3548
				
3548
 
3549
				#設置可以進行驗證
3549
				#設置可以進行驗證
3550
				$startAuth=true;
3550
				$startAuth=true;
3551
				
3551
 
3552
				#另存帳號
3552
				#另存帳號
3553
				$ac=$this->connInfo[$from->resourceId]["login"]["ac"];
3553
				$ac=$this->connInfo[$from->resourceId]["login"]["ac"];
3554
				
3554
 
3555
				#初始化密碼
3555
				#初始化密碼
3556
				$pw="";
3556
				$pw="";
3557
				
3557
 
3558
				#如果有設置密碼欄位
3558
				#如果有設置密碼欄位
3559
				if(self::$passwordCol!==""){
3559
				if(self::$passwordCol!==""){
3560
					
3560
 
3561
					#如果client已經輸入密碼了
3561
					#如果client已經輸入密碼了
3562
					if($this->connInfo[$from->resourceId]["login"]["pw"]!=""){
3562
					if($this->connInfo[$from->resourceId]["login"]["pw"]!=""){
3563
						
3563
 
3564
						#另存密碼
3564
						#另存密碼
3565
						$pw=md5($this->connInfo[$from->resourceId]["login"]["pw"]);
3565
						$pw=md5($this->connInfo[$from->resourceId]["login"]["pw"]);
3566
						
3566
 
3567
						}#if end
3567
						}#if end
3568
						
3568
 
3569
					#反之
3569
					#反之
3570
					else{
3570
					else{
3571
						
3571
 
3572
						#設置尚不能進行認證
3572
						#設置尚不能進行認證
3573
						$startAuth=false;
3573
						$startAuth=false;
3574
						
3574
 
3575
						}#else end
3575
						}#else end
3576
										
3576
 
3577
					}#if end
3577
					}#if end
3578
				
3578
 
3579
				#如果尚不能進行驗證
3579
				#如果尚不能進行驗證
3580
				if(!$startAuth){
3580
				if(!$startAuth){
3581
					
3581
 
3582
					#結束程式
3582
					#結束程式
3583
					return true;
3583
					return true;
3584
					
3584
 
3585
					}#if end
3585
					}#if end
3586
				
3586
 
3587
				#檢查有無符合的帳戶密碼
3587
				#檢查有無符合的帳戶密碼
3588
				#涵式說明:
3588
				#涵式說明:
3589
				#一次取得資料庫、表的資料
3589
				#一次取得資料庫、表的資料
3590
				#回傳的結果
3590
				#回傳的結果
3591
				#$result["status"],執行結果"true"為成功;"false"為執行失敗。
3591
				#$result["status"],執行結果"true"為成功;"false"為執行失敗。
Line 3607... Line 3607...
3607
				$conf["db::fastGetDbData"]["columnYouWant"]=array("id");#你想要的欄位!,若設為「array("*")」則代表全部欄位.
3607
				$conf["db::fastGetDbData"]["columnYouWant"]=array("id");#你想要的欄位!,若設為「array("*")」則代表全部欄位.
3608
				#可省略的參數:
3608
				#可省略的參數:
3609
				$conf["db::fastGetDbData"]["dbPassword"]=self::$dbPassword;#爲要存取dbServer的密碼
3609
				$conf["db::fastGetDbData"]["dbPassword"]=self::$dbPassword;#爲要存取dbServer的密碼
3610
				$conf["db::fastGetDbData"]["WhereColumnName"]=array(self::$accountCol);#用於判斷語句的欄位項目陣列。
3610
				$conf["db::fastGetDbData"]["WhereColumnName"]=array(self::$accountCol);#用於判斷語句的欄位項目陣列。
3611
				$conf["db::fastGetDbData"]["WhereColumnValue"]=array($ac);#用於判斷語句的欄位數值陣列,若與LIKE搭配,則可以在關鍵自字串的左右名加上「%」符號,這樣就可以搜尋具有該字串的內容。
3611
				$conf["db::fastGetDbData"]["WhereColumnValue"]=array($ac);#用於判斷語句的欄位數值陣列,若與LIKE搭配,則可以在關鍵自字串的左右名加上「%」符號,這樣就可以搜尋具有該字串的內容。
3612
				
3612
 
3613
				#如果有輸入密碼
3613
				#如果有輸入密碼
3614
				if($pw!==""){
3614
				if($pw!==""){
3615
					
3615
 
3616
					#新增要判斷 self::$password 欄位,是否有於 $pw
3616
					#新增要判斷 self::$password 欄位,是否有於 $pw
3617
					$conf["db::fastGetDbData"]["WhereColumnName"][]=self::$passwordCol;
3617
					$conf["db::fastGetDbData"]["WhereColumnName"][]=self::$passwordCol;
3618
					$conf["db::fastGetDbData"]["WhereColumnValue"][]=$pw;
3618
					$conf["db::fastGetDbData"]["WhereColumnValue"][]=$pw;
3619
					
3619
 
3620
					}#if end
3620
					}#if end
3621
				
3621
 
3622
				#$conf["WhereColumnCombine"]=array("");#用於判斷語句當中需要()起來的判斷式,須爲陣列值,"s"代表「(」,"e"代表「)」 ,若無則須設爲""。
3622
				#$conf["WhereColumnCombine"]=array("");#用於判斷語句當中需要()起來的判斷式,須爲陣列值,"s"代表「(」,"e"代表「)」 ,若無則須設爲""。
3623
				#$conf["WhereColumnOperator"]=array("");#用於判斷語句的比較符號陣列,可以用的符號有「"="、"!="、">"、"<"、"LIKE"、"NOT LIKE"」,預設都爲「=」。
3623
				#$conf["WhereColumnOperator"]=array("");#用於判斷語句的比較符號陣列,可以用的符號有「"="、"!="、">"、"<"、"LIKE"、"NOT LIKE"」,預設都爲「=」。
3624
				#$conf["WhereColumnAndOr"]=array("");#用於判斷語句條件之間成立的條件是AND還是OR,須爲陣列值。其數量應爲要判斷的欄位數量減一。
3624
				#$conf["WhereColumnAndOr"]=array("");#用於判斷語句條件之間成立的條件是AND還是OR,須爲陣列值。其數量應爲要判斷的欄位數量減一。
3625
				#$conf["orderItem"]="";#爲排序的項目依據,若要用隨機抽樣,可以用"rand()",可省略。
3625
				#$conf["orderItem"]="";#爲排序的項目依據,若要用隨機抽樣,可以用"rand()",可省略。
3626
				#$conf["ascORdesc"]="";#爲要低增還是遞減排序,asc爲遞增;desc爲遞減。
3626
				#$conf["ascORdesc"]="";#爲要低增還是遞減排序,asc爲遞增;desc爲遞減。
Line 3629... Line 3629...
3629
				#$conf["groupBy"]=array("");#爲要以哪幾個欄爲作爲分羣的依據(欄位相同的數值僅會取出一筆)。
3629
				#$conf["groupBy"]=array("");#爲要以哪幾個欄爲作爲分羣的依據(欄位相同的數值僅會取出一筆)。
3630
				#備註:
3630
				#備註:
3631
				#建議在查詢資料前,能夠檢查是否每個欄位都存在.
3631
				#建議在查詢資料前,能夠檢查是否每個欄位都存在.
3632
				$fastGetDbData=db::fastGetDbData($conf["db::fastGetDbData"]);
3632
				$fastGetDbData=db::fastGetDbData($conf["db::fastGetDbData"]);
3633
				unset($conf["db::fastGetDbData"]);
3633
				unset($conf["db::fastGetDbData"]);
3634
				
3634
 
3635
				#如果取得資料失敗
3635
				#如果取得資料失敗
3636
				if($fastGetDbData["status"]==="false"){
3636
				if($fastGetDbData["status"]==="false"){
3637
					
3637
 
3638
					#設置執行失敗
3638
					#設置執行失敗
3639
					$result["status"]="false";
3639
					$result["status"]="false";
3640
				
3640
 
3641
					#設置執行錯誤訊息
3641
					#設置執行錯誤訊息
3642
					$result["error"]=$fastGetDbData;					
3642
					$result["error"]=$fastGetDbData;
3643
					
3643
 
3644
					#提示server有資料庫錯誤
3644
					#提示server有資料庫錯誤
3645
					echo "dbError!";
3645
					echo "dbError!";
3646
					
3646
 
3647
					#結束程式
3647
					#結束程式
3648
					exit;
3648
					exit;
3649
					
3649
 
3650
					}#if end
3650
					}#if end
3651
					
3651
 
3652
				#如果沒有一筆資料
3652
				#如果沒有一筆資料
3653
				if($fastGetDbData["dataCount"]!==1){
3653
				if($fastGetDbData["dataCount"]!==1){
3654
					
3654
 
3655
					#代表登入失敗
3655
					#代表登入失敗
3656
					
3656
 
3657
					#清空登入用的帳戶
3657
					#清空登入用的帳戶
3658
					$this->connInfo[$from->resourceId]["login"]["ac"]="";
3658
					$this->connInfo[$from->resourceId]["login"]["ac"]="";
3659
				
3659
 
3660
					#清空登入用的密碼
3660
					#清空登入用的密碼
3661
					$this->connInfo[$from->resourceId]["login"]["pw"]="";
3661
					$this->connInfo[$from->resourceId]["login"]["pw"]="";
3662
					
3662
 
3663
					#包裝訊息
3663
					#包裝訊息
3664
					#"type"為"login"	
3664
					#"type"為"login"
3665
					#"status"為"false"		
3665
					#"status"為"false"
3666
					#"data"為實際的訊息內容
3666
					#"data"為實際的訊息內容
3667
					$packedMsg=array("type"=>"login","status"=>"false","data"=>"Login failed, please input account. ex:account:ws1");
3667
					$packedMsg=array("type"=>"login","status"=>"false","data"=>"Login failed, please input account. ex:account:ws1");
3668
					
3668
 
3669
					#提示登入失敗					
3669
					#提示登入失敗
3670
					$from->send(json_encode($packedMsg));
3670
					$from->send(json_encode($packedMsg));
3671
					
3671
 
3672
					#執行到這即可
3672
					#執行到這即可
3673
					return true;
3673
					return true;
3674
					
3674
 
3675
					}#if end
3675
					}#if end
3676
					
3676
 
3677
				#反之代表帳戶資訊正確
3677
				#反之代表帳戶資訊正確
3678
				else{
3678
				else{
3679
										
3679
 
3680
					#設置 $myUserId
3680
					#設置 $myUserId
3681
					$myUserId=$this->connInfo[$from->resourceId]["userId"]=$fastGetDbData["dataContent"]["id"][0];
3681
					$myUserId=$this->connInfo[$from->resourceId]["userId"]=$fastGetDbData["dataContent"]["id"][0];
3682
					
3682
 
3683
					#設置 $myId
3683
					#設置 $myId
3684
					$myId=$from->resourceId;
3684
					$myId=$from->resourceId;
3685
					
3685
 
3686
					#初始化msgId的部份內容
3686
					#初始化msgId的部份內容
3687
					$msgId=$myUserId."-";
3687
					$msgId=$myUserId."-";
3688
					
3688
 
3689
					#初始化儲存msgId的陣列
3689
					#初始化儲存msgId的陣列
3690
					$msgIdArray=array();
3690
					$msgIdArray=array();
3691
					
3691
 
3692
					#取得所有client的msgId
3692
					#取得所有client的msgId
3693
					foreach($this->connInfo as $clientInfoArray){
3693
					foreach($this->connInfo as $clientInfoArray){
3694
						
3694
 
3695
						#如果msgId不為""
3695
						#如果msgId不為""
3696
						if($clientInfoArray["msgId"]!==""){
3696
						if($clientInfoArray["msgId"]!==""){
3697
							
3697
 
3698
							#取得msgId
3698
							#取得msgId
3699
							$msgIdArray[]=$clientInfoArray["msgId"];
3699
							$msgIdArray[]=$clientInfoArray["msgId"];
3700
							
3700
 
3701
							}#if end
3701
							}#if end
3702
						
3702
 
3703
						}#foreach end
3703
						}#foreach end
3704
					
3704
 
3705
					#如果尚未產生完整的msgId或msgId有重複
3705
					#如果尚未產生完整的msgId或msgId有重複
3706
					while($msgId===$myUserId."-" || in_array($msgId,$msgIdArray)){
3706
					while($msgId===$myUserId."-" || in_array($msgId,$msgIdArray)){
3707
						
3707
 
3708
						#涵式說明:
3708
						#涵式說明:
3709
						#建立以圖片(PNG格式)呈現的驗證碼.
3709
						#建立以圖片(PNG格式)呈現的驗證碼.
3710
						#回傳的解果:
3710
						#回傳的解果:
3711
						#$result["status"],執行是否正常,"true"代表執行成功,"false"代表執行失敗.
3711
						#$result["status"],執行是否正常,"true"代表執行成功,"false"代表執行失敗.
3712
						#$result["error"],錯誤訊息.
3712
						#$result["error"],錯誤訊息.
Line 3725... Line 3725...
3725
						$conf["authenticate::validationCode"]["disableImg"]="true";
3725
						$conf["authenticate::validationCode"]["disableImg"]="true";
3726
						#$conf["imgToData"],字串,預設為"true"代表將圖片轉存成base64圖片,並將原始圖片移除;反之為"false"
3726
						#$conf["imgToData"],字串,預設為"true"代表將圖片轉存成base64圖片,並將原始圖片移除;反之為"false"
3727
						#$conf["imgToData"]="true";
3727
						#$conf["imgToData"]="true";
3728
						$validationCode=authenticate::validationCode($conf["authenticate::validationCode"]);
3728
						$validationCode=authenticate::validationCode($conf["authenticate::validationCode"]);
3729
						unset($conf["authenticate::validationCode"]);
3729
						unset($conf["authenticate::validationCode"]);
3730
						
3730
 
3731
						#如果產生亂數失敗
3731
						#如果產生亂數失敗
3732
						if($validationCode["status"]==="false"){
3732
						if($validationCode["status"]==="false"){
3733
							
3733
 
3734
							#設置錯誤狀態
3734
							#設置錯誤狀態
3735
							$result["status"]="false";
3735
							$result["status"]="false";
3736
							
3736
 
3737
							#設置錯誤提示
3737
							#設置錯誤提示
3738
							$result["error"]=$validationCode;
3738
							$result["error"]=$validationCode;
3739
							
3739
 
3740
							#印出錯誤訊息
3740
							#印出錯誤訊息
3741
							var_dump($result);
3741
							var_dump($result);
3742
							
3742
 
3743
							#回傳結果			
3743
							#回傳結果
3744
							return $result;				
3744
							return $result;
3745
											
3745
 
3746
							}#if end
3746
							}#if end
3747
							
3747
 
3748
						#儲存產生好的msgId	
3748
						#儲存產生好的msgId
3749
						$msgId=$myUserId."-".$validationCode["randNumberWord"];
3749
						$msgId=$myUserId."-".$validationCode["randNumberWord"];
3750
						
3750
 
3751
						}#while end
3751
						}#while end
3752
					
3752
 
3753
					#設置獨立於id與userId且用於驗證訊息是否收到的msgId
3753
					#設置獨立於id與userId且用於驗證訊息是否收到的msgId
3754
					$this->connInfo[$from->resourceId]["msgId"]=$msgId;
3754
					$this->connInfo[$from->resourceId]["msgId"]=$msgId;
3755
					
3755
 
3756
					#包裝訊息
3756
					#包裝訊息
3757
					#"type"為"login"	
3757
					#"type"為"login"
3758
					#"status"為"true"		
3758
					#"status"為"true"
3759
					#"data"為實際的訊息內容
3759
					#"data"為實際的訊息內容
3760
					$packedMsg=array("type"=>"login","status"=>"true","data"=>"Login successfully!");
3760
					$packedMsg=array("type"=>"login","status"=>"true","data"=>"Login successfully!");
3761
					
3761
 
3762
					#提示登入成功
3762
					#提示登入成功
3763
					$from->send(json_encode($packedMsg));
3763
					$from->send(json_encode($packedMsg));
3764
					
3764
 
3765
					#檢查是否有訊息是要給自己的
3765
					#檢查是否有訊息是要給自己的
3766
					foreach($this->unSendMsg as $index => $historyMsg){
3766
					foreach($this->unSendMsg as $index => $historyMsg){
3767
						
3767
 
3768
						#如果有要給自己的訊息
3768
						#如果有要給自己的訊息
3769
						if($myUserId===$historyMsg["toUserId"]){
3769
						if($myUserId===$historyMsg["toUserId"]){
3770
							
3770
 
3771
							#包裝訊息
3771
							#包裝訊息
3772
							#"type"為"unSendMsg?"	
3772
							#"type"為"unSendMsg?"
3773
							#"status"為"true"		
3773
							#"status"為"true"
3774
							#"data"為實際的訊息內容
3774
							#"data"為實際的訊息內容
3775
							$packedMsg=array("type"=>"unSendMsg?","status"=>"true","data"=>$historyMsg["msg"]);
3775
							$packedMsg=array("type"=>"unSendMsg?","status"=>"true","data"=>$historyMsg["msg"]);
3776
							
3776
 
3777
							#接收別人給的留言
3777
							#接收別人給的留言
3778
							$from->send(json_encode($packedMsg));
3778
							$from->send(json_encode($packedMsg));
3779
							
3779
 
3780
							#有幾個人在線上就執行幾次
3780
							#有幾個人在線上就執行幾次
3781
							foreach($this->connInfo as $toId => $cInfo){
3781
							foreach($this->connInfo as $toId => $cInfo){
3782
								
3782
 
3783
								#如果是自己
3783
								#如果是自己
3784
								if($myId===$toId ){
3784
								if($myId===$toId ){
3785
									
3785
 
3786
									#跳過
3786
									#跳過
3787
									continue;
3787
									continue;
3788
									
3788
 
3789
									}#if end
3789
									}#if end
3790
								
3790
 
3791
								#如果留言的對象有在線上
3791
								#如果留言的對象有在線上
3792
								if($historyMsg["fromUserId"]===$cInfo["userId"]){
3792
								if($historyMsg["fromUserId"]===$cInfo["userId"]){
3793
									
3793
 
3794
									#檢查對方是否已經在自己的通話清單裡面了
3794
									#檢查對方是否已經在自己的通話清單裡面了
3795
									
3795
 
3796
									#初始化要將對方加到自己的對話清單裡面
3796
									#初始化要將對方加到自己的對話清單裡面
3797
									$add=false;
3797
									$add=false;
3798
									
3798
 
3799
									#根據自己的每個通話對象
3799
									#根據自己的每個通話對象
3800
									foreach($this->connInfo[$myId]["talkTo"] as $myToIndex=>$myToCinfo ){
3800
									foreach($this->connInfo[$myId]["talkTo"] as $myToIndex=>$myToCinfo ){
3801
										
3801
 
3802
										#如果對對方已經存在於自己的對話清單裡面了
3802
										#如果對對方已經存在於自己的對話清單裡面了
3803
										if($myToCinfo["userId"]===$historyMsg["fromUserId"] && $myId===$historyMsg["fromId"]){
3803
										if($myToCinfo["userId"]===$historyMsg["fromUserId"] && $myId===$historyMsg["fromId"]){
3804
											
3804
 
3805
											#設置不要將對方加到自己的對話清單裡面
3805
											#設置不要將對方加到自己的對話清單裡面
3806
											$add=false;
3806
											$add=false;
3807
											
3807
 
3808
											#跳出foreach
3808
											#跳出foreach
3809
											break;
3809
											break;
3810
											
3810
 
3811
											}#if end
3811
											}#if end
3812
										
3812
 
3813
										}#foreach end
3813
										}#foreach end
3814
									
3814
 
3815
									#如果要將對方加到自己的對話清單裡面
3815
									#如果要將對方加到自己的對話清單裡面
3816
									if($add){
3816
									if($add){
3817
										
3817
 
3818
										#將對方加到自己的talkTo清單裡面
3818
										#將對方加到自己的talkTo清單裡面
3819
										$this->connInfo[$myId]["talkTo"][]=array("id"=>$toId,"userId"=>$historyMsg["fromUserId"],"msgId"=>$this->connInfo[$toId]["msgId"]);
3819
										$this->connInfo[$myId]["talkTo"][]=array("id"=>$toId,"userId"=>$historyMsg["fromUserId"],"msgId"=>$this->connInfo[$toId]["msgId"]);
3820
										
3820
 
3821
										}#if end
3821
										}#if end
3822
																										
-
 
-
 
3822
 
3823
									#檢查自己有沒有在對方talkTo清單裡面
3823
									#檢查自己有沒有在對方talkTo清單裡面
3824
																		
-
 
-
 
3824
 
3825
									#假設對方需要加自己到對話清單裡面
3825
									#假設對方需要加自己到對話清單裡面
3826
									$add=true;
3826
									$add=true;
3827
									
3827
 
3828
									#檢查通話對象能否跟自己對話
3828
									#檢查通話對象能否跟自己對話
3829
									
3829
 
3830
									#依據對方每個通話的對象
3830
									#依據對方每個通話的對象
3831
									foreach($this->connInfo[$toId]["talkTo"] as $talkToIndex=>$talkToCinfo){
3831
									foreach($this->connInfo[$toId]["talkTo"] as $talkToIndex=>$talkToCinfo){
3832
										
3832
 
3833
										#如果自己的userId已經在對方的對話清單裡面了
3833
										#如果自己的userId已經在對方的對話清單裡面了
3834
										if($myUserId===$talkToCinfo["userId"]){
3834
										if($myUserId===$talkToCinfo["userId"]){
3835
											
3835
 
3836
											#如果對方對話userId對應的id不在線上
3836
											#如果對方對話userId對應的id不在線上
3837
											if(!isset($this->connInfo[$toId])){
3837
											if(!isset($this->connInfo[$toId])){
3838
												
3838
 
3839
												#debug
3839
												#debug
3840
												#var_dump("對方對話userId(".$talkToCinfo["userId"].")應的id(".$toId.")不在線上");	
3840
												#var_dump("對方對話userId(".$talkToCinfo["userId"].")應的id(".$toId.")不在線上");
3841
												
3841
 
3842
												#檢查對方對話清單是否已經含有自己的id與userId了
3842
												#檢查對方對話清單是否已經含有自己的id與userId了
3843
												
3843
 
3844
												#假設對方對話清單沒有自己的id與userId
3844
												#假設對方對話清單沒有自己的id與userId
3845
												$update=true;
3845
												$update=true;
3846
												
3846
 
3847
												#依據對方每個通話的對象
3847
												#依據對方每個通話的對象
3848
												foreach($this->connInfo[$toId]["talkTo"] as $ceToIndex=>$ceToCinfo){
3848
												foreach($this->connInfo[$toId]["talkTo"] as $ceToIndex=>$ceToCinfo){
3849
													
3849
 
3850
													#如果對方對話清單已經含有自己的id與userId了												
3850
													#如果對方對話清單已經含有自己的id與userId了
3851
													if($ceToCinfo["id"]===$myId && $ceToCinfo["userId"]===$myUserId){
3851
													if($ceToCinfo["id"]===$myId && $ceToCinfo["userId"]===$myUserId){
3852
														
3852
 
3853
														#debug
3853
														#debug
3854
														#var_dump("對方對話清單已經含有自己的id(".$myId.")與userId了(".$myUserId.")");
3854
														#var_dump("對方對話清單已經含有自己的id(".$myId.")與userId了(".$myUserId.")");
3855
														
3855
 
3856
														#設置不需要更新
3856
														#設置不需要更新
3857
														$update=false;
3857
														$update=false;
3858
														
3858
 
3859
														#debug
3859
														#debug
3860
														#var_dump($toId."要加".$myId."到通話清單裡面");
3860
														#var_dump($toId."要加".$myId."到通話清單裡面");
3861
														
3861
 
3862
														#設置對方不需要加自己到對話清單裡面
3862
														#設置對方不需要加自己到對話清單裡面
3863
														$add=false;
3863
														$add=false;
3864
														
3864
 
3865
														}#if end
3865
														}#if end
3866
													
3866
 
3867
													}#foreach end
3867
													}#foreach end
3868
												
3868
 
3869
												#如果對方對話清單沒有自己的id與userId與msgId
3869
												#如果對方對話清單沒有自己的id與userId與msgId
3870
												if($update){
3870
												if($update){
3871
													
3871
 
3872
													#debug
3872
													#debug
3873
													#var_dump($toId."要更新對話清單中索引為".$talkToIndex."的id(".$this->connInfo[$toId]["talkTo"][$talkToIndex]["id"].")為".$myId);
3873
													#var_dump($toId."要更新對話清單中索引為".$talkToIndex."的id(".$this->connInfo[$toId]["talkTo"][$talkToIndex]["id"].")為".$myId);
3874
													
3874
 
3875
													#將自己在對方對話清單裡面的id改成現在的id
3875
													#將自己在對方對話清單裡面的id改成現在的id
3876
													$this->connInfo[$toId]["talkTo"][$talkToIndex]["id"]=$myId;
3876
													$this->connInfo[$toId]["talkTo"][$talkToIndex]["id"]=$myId;
3877
																										
-
 
-
 
3877
 
3878
													}#if end
3878
													}#if end
3879
													
3879
 
3880
												#反之對方對話清單已經有自己的id與userId
3880
												#反之對方對話清單已經有自己的id與userId
3881
												else{
3881
												else{
3882
													
3882
 
3883
													#移除對方該已經斷線的對話id
3883
													#移除對方該已經斷線的對話id
3884
													unset($this->connInfo[$toId]["talkTo"][$talkToIndex]);
3884
													unset($this->connInfo[$toId]["talkTo"][$talkToIndex]);
3885
													
3885
 
3886
													}#else end
3886
													}#else end
3887
												
3887
 
3888
												}#if end
3888
												}#if end
3889
											
3889
 
3890
											}#if end
3890
											}#if end
3891
										
3891
 
3892
										}#foreach end
3892
										}#foreach end
3893
										
3893
 
3894
									#如果對方需要加自己到對話清單裡面
3894
									#如果對方需要加自己到對話清單裡面
3895
									if($add){
3895
									if($add){
3896
										
3896
 
3897
										#檢查對方每個通話對象
3897
										#檢查對方每個通話對象
3898
										foreach($this->connInfo[$toId]["talkTo"] as $reCktIndex=>$reCktInfo){
3898
										foreach($this->connInfo[$toId]["talkTo"] as $reCktIndex=>$reCktInfo){
3899
											
3899
 
3900
											#如果已經有將自己加到對方的對話清單裡面
3900
											#如果已經有將自己加到對方的對話清單裡面
3901
											if($reCktInfo["id"]===$myId && $reCktInfo["userId"]===$myUserId){
3901
											if($reCktInfo["id"]===$myId && $reCktInfo["userId"]===$myUserId){
3902
												
3902
 
3903
												#設置不用將自己加到對方的對話清單裡面
3903
												#設置不用將自己加到對方的對話清單裡面
3904
												$add=false;
3904
												$add=false;
3905
												
3905
 
3906
												}#if end
3906
												}#if end
3907
											
3907
 
3908
											}#foreach end
3908
											}#foreach end
3909
										
3909
 
3910
										#如果對方需要加自己到對話清單裡面
3910
										#如果對方需要加自己到對話清單裡面
3911
										if($add){
3911
										if($add){
3912
											
3912
 
3913
											#debug
3913
											#debug
3914
											#var_dump($toId."要加".$myId."(msgId為".$this->connInfo[$myId]["msgId"].")到通話清單裡面");
3914
											#var_dump($toId."要加".$myId."(msgId為".$this->connInfo[$myId]["msgId"].")到通話清單裡面");
3915
										
3915
 
3916
											#讓通話對象也可以傳送訊息回來
3916
											#讓通話對象也可以傳送訊息回來
3917
											$this->connInfo[$toId]["talkTo"][]=array("id"=>$myId,"userId"=>$myUserId,"msgId"=>$this->connInfo[$myId]["msgId"]);
3917
											$this->connInfo[$toId]["talkTo"][]=array("id"=>$myId,"userId"=>$myUserId,"msgId"=>$this->connInfo[$myId]["msgId"]);
3918
											
3918
 
3919
											}#if end
3919
											}#if end
3920
										
3920
 
3921
										}#if end
3921
										}#if end
3922
									
3922
 
3923
									}#if end
3923
									}#if end
3924
								
3924
 
3925
								}#foreach end
3925
								}#foreach end
3926
							
3926
 
3927
							#移除留言紀錄
3927
							#移除留言紀錄
3928
							unset($this->unSendMsg[$index]);
3928
							unset($this->unSendMsg[$index]);
3929
							
3929
 
3930
							}#if end
3930
							}#if end
3931
						
3931
 
3932
						}#foreach end
3932
						}#foreach end
3933
						
3933
 
3934
					#檢查線上是否有人想跟我對話
3934
					#檢查線上是否有人想跟我對話
3935
					
3935
 
3936
					#有幾個人在線上就執行幾次
3936
					#有幾個人在線上就執行幾次
3937
					foreach($this->connInfo as $toId => $cInfo){
3937
					foreach($this->connInfo as $toId => $cInfo){
3938
						
3938
 
3939
						#如果是自己
3939
						#如果是自己
3940
						if($myId===$toId){
3940
						if($myId===$toId){
3941
							
3941
 
3942
							#跳過
3942
							#跳過
3943
							continue;
3943
							continue;
3944
							
3944
 
3945
							}#if end
3945
							}#if end
3946
						
3946
 
3947
						#該cliet目前再跟幾個人通話,就執行幾次
3947
						#該cliet目前再跟幾個人通話,就執行幾次
3948
						foreach($cInfo["talkTo"] as $tIndex=>$tInfo){
3948
						foreach($cInfo["talkTo"] as $tIndex=>$tInfo){
3949
							
3949
 
3950
							#如果自己的userId在對方的對話清單裡面
3950
							#如果自己的userId在對方的對話清單裡面
3951
							if($myUserId===$tInfo["userId"]){
3951
							if($myUserId===$tInfo["userId"]){
3952
								
3952
 
3953
								#假設自己需要被對方加到對話清單裡面
3953
								#假設自己需要被對方加到對話清單裡面
3954
								$add=true;
3954
								$add=true;
3955
								
3955
 
3956
								#檢查自己是否已經在對方的對話清單裡面
3956
								#檢查自己是否已經在對方的對話清單裡面
3957
								
3957
 
3958
								#對方有幾個通話對象就執行幾次
3958
								#對方有幾個通話對象就執行幾次
3959
								foreach($this->connInfo[$toId]["talkTo"] as $toIndex=>$toCinfo){
3959
								foreach($this->connInfo[$toId]["talkTo"] as $toIndex=>$toCinfo){
3960
									
3960
 
3961
									#如果自己已經在對方的對話清單裡面
3961
									#如果自己已經在對方的對話清單裡面
3962
									if($myUserId===$toCinfo["userId"] && $myId===$toCinfo["id"]){
3962
									if($myUserId===$toCinfo["userId"] && $myId===$toCinfo["id"]){
3963
										
3963
 
3964
										#設置自己不需要被對方加到對話清單裡面
3964
										#設置自己不需要被對方加到對話清單裡面
3965
										$add=false;
3965
										$add=false;
3966
										
3966
 
3967
										#跳出迴圈
3967
										#跳出迴圈
3968
										break;
3968
										break;
3969
										
3969
 
3970
										}#if end
3970
										}#if end
3971
									
3971
 
3972
									}#foreach end
3972
									}#foreach end
3973
								
3973
 
3974
								#另存針對自己userId的連線id
3974
								#另存針對自己userId的連線id
3975
								$oneIdOfMyUserid=$this->connInfo[$toId]["talkTo"][$tIndex]["id"];
3975
								$oneIdOfMyUserid=$this->connInfo[$toId]["talkTo"][$tIndex]["id"];
3976
								
3976
 
3977
								#如果對方對話清單裡面的用戶id不在線
3977
								#如果對方對話清單裡面的用戶id不在線
3978
								if(!isset($this->connInfo[$oneIdOfMyUserid])){
3978
								if(!isset($this->connInfo[$oneIdOfMyUserid])){
3979
									
3979
 
3980
									#假設要將自己在對方對話清單裡面的id改成現在的id
3980
									#假設要將自己在對方對話清單裡面的id改成現在的id
3981
									$update=true;
3981
									$update=true;
3982
									
3982
 
3983
									#自己不需要被對方加到對話清單裡面
3983
									#自己不需要被對方加到對話清單裡面
3984
									$add=false;
3984
									$add=false;
3985
									
3985
 
3986
									#檢查自己的id是否已經在對方的對話id裡面
3986
									#檢查自己的id是否已經在對方的對話id裡面
3987
									foreach($this->connInfo[$toId]["talkTo"] as $ceIndex=>$ceInfo){
3987
									foreach($this->connInfo[$toId]["talkTo"] as $ceIndex=>$ceInfo){
3988
										
3988
 
3989
										#如果自己的id已經在對方的對話清單裡面
3989
										#如果自己的id已經在對方的對話清單裡面
3990
										if($myId===$ceInfo["id"]){
3990
										if($myId===$ceInfo["id"]){
3991
											
3991
 
3992
											#設置不要將自己在對方對話清單裡面的id改成現在的id(可以移除該對話id)
3992
											#設置不要將自己在對方對話清單裡面的id改成現在的id(可以移除該對話id)
3993
											$update=false;
3993
											$update=false;
3994
											
3994
 
3995
											#跳出迴圈
3995
											#跳出迴圈
3996
											break;
3996
											break;
3997
											
3997
 
3998
											}#if end
3998
											}#if end
3999
										
3999
 
4000
										}#foreach end
4000
										}#foreach end
4001
									
4001
 
4002
									#如果要將自己在對方對話清單裡面的id改成現在的id
4002
									#如果要將自己在對方對話清單裡面的id改成現在的id
4003
									if($update){
4003
									if($update){
4004
										
4004
 
4005
										#debug
4005
										#debug
4006
										#var_dump($toId."要更新對話清單中索引為".$tIndex."的id(".$this->connInfo[$toId]["talkTo"][$tIndex]["id"].")為".$myId);
4006
										#var_dump($toId."要更新對話清單中索引為".$tIndex."的id(".$this->connInfo[$toId]["talkTo"][$tIndex]["id"].")為".$myId);
4007
																				
-
 
-
 
4007
 
4008
										#將自己在對方對話清單裡面的id改成現在的id
4008
										#將自己在對方對話清單裡面的id改成現在的id
4009
										$this->connInfo[$toId]["talkTo"][$tIndex]["id"]=$myId;
4009
										$this->connInfo[$toId]["talkTo"][$tIndex]["id"]=$myId;
4010
										
4010
 
4011
										}#if end
4011
										}#if end
4012
										
4012
 
4013
									#反之不要將自己在對方對話清單裡面的id改成現在的id(可以移除該對話id)
4013
									#反之不要將自己在對方對話清單裡面的id改成現在的id(可以移除該對話id)
4014
									else{
4014
									else{
4015
										
4015
 
4016
										#移除該對話id
4016
										#移除該對話id
4017
										unset($this->connInfo[$toId]["talkTo"][$tIndex]);
4017
										unset($this->connInfo[$toId]["talkTo"][$tIndex]);
4018
										
4018
 
4019
										}#else end
4019
										}#else end
4020
									
4020
 
4021
									}#if end	
4021
									}#if end
4022
									
4022
 
4023
								#如果要將自己加到對方的的通話清單裡面
4023
								#如果要將自己加到對方的的通話清單裡面
4024
								if($add){
4024
								if($add){
4025
									
4025
 
4026
									#debug
4026
									#debug
4027
									#var_dump($toId."要加".$myId."(msgId為".$this->connInfo[$myId]["msgId"].")到通話清單裡面");
4027
									#var_dump($toId."要加".$myId."(msgId為".$this->connInfo[$myId]["msgId"].")到通話清單裡面");
4028
									
4028
 
4029
									#將自己加到對方的的通話清單裡面
4029
									#將自己加到對方的的通話清單裡面
4030
									$this->connInfo[$toId]["talkTo"][]=array("id"=>$myId,"userId"=>$myUserId,"msgId"=>$this->connInfo[$myId]["msgId"]);			
4030
									$this->connInfo[$toId]["talkTo"][]=array("id"=>$myId,"userId"=>$myUserId,"msgId"=>$this->connInfo[$myId]["msgId"]);
4031
									
4031
 
4032
									}#if end								
4032
									}#if end
4033
								
4033
 
4034
								#檢查對方是否已經在自己的對話清單裡面
4034
								#檢查對方是否已經在自己的對話清單裡面
4035
																
-
 
-
 
4035
 
4036
								#設置要新增對方到自己的對話清單裡面.
4036
								#設置要新增對方到自己的對話清單裡面.
4037
								$add=true;
4037
								$add=true;
4038
								
4038
 
4039
								#自己有幾個對話對象就執行幾次
4039
								#自己有幾個對話對象就執行幾次
4040
								foreach($this->connInfo[$myId]["talkTo"] as $mtIndex=>$mtInfo){
4040
								foreach($this->connInfo[$myId]["talkTo"] as $mtIndex=>$mtInfo){
4041
									
4041
 
4042
									#對方的userId與id若在自己的對話清單裡面
4042
									#對方的userId與id若在自己的對話清單裡面
4043
									if($mtInfo["userId"]===$cInfo["userId"] && $mtInfo["id"]===$toId){
4043
									if($mtInfo["userId"]===$cInfo["userId"] && $mtInfo["id"]===$toId){
4044
										
4044
 
4045
										#設置不需要新增對方到自己的對話清單裡面
4045
										#設置不需要新增對方到自己的對話清單裡面
4046
										$add=false;
4046
										$add=false;
4047
										
4047
 
4048
										#跳出 foreach
4048
										#跳出 foreach
4049
										break;
4049
										break;
4050
										
4050
 
4051
										}#if end
4051
										}#if end
4052
									
4052
 
4053
									}#foreache end
4053
									}#foreache end
4054
								
4054
 
4055
								#如果要新增對方到自己的對話清單裡面.
4055
								#如果要新增對方到自己的對話清單裡面.
4056
								if($add){
4056
								if($add){
4057
									
4057
 
4058
									#自己有幾個對話對象就執行幾次
4058
									#自己有幾個對話對象就執行幾次
4059
									foreach($this->connInfo[$myId]["talkTo"] as $mtIndex=>$mtInfo){
4059
									foreach($this->connInfo[$myId]["talkTo"] as $mtIndex=>$mtInfo){
4060
										
4060
 
4061
										#如果對方已經在自己的通話清單
4061
										#如果對方已經在自己的通話清單
4062
										if($mtInfo["userId"]===$cInfo["userId"] && $mtInfo["id"]===$toId){
4062
										if($mtInfo["userId"]===$cInfo["userId"] && $mtInfo["id"]===$toId){
4063
 
4063
 
4064
											#設置不要將對方到自己的對話清單裡面.
4064
											#設置不要將對方到自己的對話清單裡面.
4065
											$add=false;
4065
											$add=false;
4066
											
4066
 
4067
											#跳出 foreach end
4067
											#跳出 foreach end
4068
											break;
4068
											break;
4069
 
4069
 
4070
											}#if end
4070
											}#if end
4071
										
4071
 
4072
										}#foreach end
4072
										}#foreach end
4073
									
4073
 
4074
									#如果要新增對方到自己的對話清單裡面.
4074
									#如果要新增對方到自己的對話清單裡面.
4075
									if($add){
4075
									if($add){
4076
																				
-
 
-
 
4076
 
4077
										#將對方加到自己的對話清單裡面
4077
										#將對方加到自己的對話清單裡面
4078
										$this->connInfo[$myId]["talkTo"][]=array("id"=>$toId,"userId"=>$cInfo["userId"],"msgId"=>$this->connInfo[$toId]["msgId"]);
4078
										$this->connInfo[$myId]["talkTo"][]=array("id"=>$toId,"userId"=>$cInfo["userId"],"msgId"=>$this->connInfo[$toId]["msgId"]);
4079
										
4079
 
4080
										}#if end
4080
										}#if end
4081
									
4081
 
4082
									}#if end
4082
									}#if end
4083
								
4083
 
4084
								}#if end
4084
								}#if end
4085
							
4085
 
4086
							}#foreach end
4086
							}#foreach end
4087
						
4087
 
4088
						}#foreach end
4088
						}#foreach end
4089
					
4089
 
4090
					#結束認證成功後的流程
4090
					#結束認證成功後的流程
4091
					return true;
4091
					return true;
4092
					
4092
 
4093
					}#else end
4093
					}#else end
4094
					
4094
 
4095
				}#else end
4095
				}#else end
4096
				
4096
 
4097
			#結束認證流程
4097
			#結束認證流程
4098
			return true;	
4098
			return true;
4099
			
4099
 
4100
			}#if end
4100
			}#if end
4101
		
4101
 
4102
		#如果收到 "id?"
4102
		#如果收到 "id?"
4103
		else if($msg==="id?"){
4103
		else if($msg==="id?"){
4104
			
4104
 
4105
			#包裝訊息
4105
			#包裝訊息
4106
			#"type"為"id?"	
4106
			#"type"為"id?"
4107
			#"status"為"true"		
4107
			#"status"為"true"
4108
			#"data"為實際的訊息內容
4108
			#"data"為實際的訊息內容
4109
			$packedMsg=array("type"=>"id?","status"=>"true","data"=>$from->resourceId);
4109
			$packedMsg=array("type"=>"id?","status"=>"true","data"=>$from->resourceId);
4110
			
4110
 
4111
			#傳自己的id給client
4111
			#傳自己的id給client
4112
			$from->send(json_encode($packedMsg));
4112
			$from->send(json_encode($packedMsg));
4113
			
4113
 
4114
			#回傳成功
4114
			#回傳成功
4115
			return true;
4115
			return true;
4116
			
4116
 
4117
			}#if end
4117
			}#if end
4118
			
4118
 
4119
		#如果收到 "ids?"
4119
		#如果收到 "ids?"
4120
		else if($msg==="ids?"){
4120
		else if($msg==="ids?"){
4121
			
4121
 
4122
			#初始化儲存其他人的id
4122
			#初始化儲存其他人的id
4123
			$idsArray=array();
4123
			$idsArray=array();
4124
			
4124
 
4125
			#針對所的client
4125
			#針對所的client
4126
			foreach ($this->clients as $client){
4126
			foreach ($this->clients as $client){
4127
			
4127
 
4128
				#排除自己
4128
				#排除自己
4129
				if($from !== $client){
4129
				if($from !== $client){
4130
					
4130
 
4131
					#取得其他人的id
4131
					#取得其他人的id
4132
					$idsArray[]=$client->resourceId;
4132
					$idsArray[]=$client->resourceId;
4133
					
4133
 
4134
					}#if end
4134
					}#if end
4135
				
4135
 
4136
				}#foreach end			
4136
				}#foreach end
4137
			
4137
 
4138
			#包裝訊息
4138
			#包裝訊息
4139
			#"type"為"ids?"	
4139
			#"type"為"ids?"
4140
			#"status"為"true"		
4140
			#"status"為"true"
4141
			#"data"為實際的訊息內容
4141
			#"data"為實際的訊息內容
4142
			$packedMsg=array("type"=>"ids?","status"=>"true","data"=>$idsArray);
4142
			$packedMsg=array("type"=>"ids?","status"=>"true","data"=>$idsArray);
4143
			
4143
 
4144
			#傳他人的id給client
4144
			#傳他人的id給client
4145
			$from->send(json_encode($packedMsg));
4145
			$from->send(json_encode($packedMsg));
4146
			
4146
 
4147
			#回傳成功
4147
			#回傳成功
4148
			return true;
4148
			return true;
4149
		
4149
 
4150
			}#if end
4150
			}#if end
4151
		
4151
 
4152
		#如果收到 "talkTo?"
4152
		#如果收到 "talkTo?"
4153
		else if($msg==="talkTo?"){
4153
		else if($msg==="talkTo?"){
4154
			
4154
 
4155
			#包裝訊息
4155
			#包裝訊息
4156
			#"type"為"talkTo?"	
4156
			#"type"為"talkTo?"
4157
			#"status"為"true"		
4157
			#"status"為"true"
4158
			#"data"為實際的訊息內容
4158
			#"data"為實際的訊息內容
4159
			$packedMsg=array("type"=>"talkTo?","status"=>"true","data"=>$this->connInfo[$from->resourceId]["talkTo"]);
4159
			$packedMsg=array("type"=>"talkTo?","status"=>"true","data"=>$this->connInfo[$from->resourceId]["talkTo"]);
4160
			
4160
 
4161
			#正在對話的對象id給client
4161
			#正在對話的對象id給client
4162
			$from->send(json_encode($packedMsg));
4162
			$from->send(json_encode($packedMsg));
4163
			
4163
 
4164
			#回傳成功
4164
			#回傳成功
4165
			return true;
4165
			return true;
4166
			
4166
 
4167
			}#if end
4167
			}#if end
4168
		
4168
 
4169
		#反之如果是 "msgId?"
4169
		#反之如果是 "msgId?"
4170
		else if($msg==="msgId?"){
4170
		else if($msg==="msgId?"){
4171
			
4171
 
4172
			#包裝訊息
4172
			#包裝訊息
4173
			#"type"為"talkTo?"	
4173
			#"type"為"talkTo?"
4174
			#"status"為"true"		
4174
			#"status"為"true"
4175
			#"data"為實際的訊息內容
4175
			#"data"為實際的訊息內容
4176
			$packedMsg=array("type"=>"msgId?","status"=>"true","data"=>$this->connInfo[$from->resourceId]["msgId"]);
4176
			$packedMsg=array("type"=>"msgId?","status"=>"true","data"=>$this->connInfo[$from->resourceId]["msgId"]);
4177
			
4177
 
4178
			#取得自己的msgId
4178
			#取得自己的msgId
4179
			$from->send(json_encode($packedMsg));
4179
			$from->send(json_encode($packedMsg));
4180
			
4180
 
4181
			#回傳成功
4181
			#回傳成功
4182
			return true;
4182
			return true;
4183
			
4183
 
4184
			}#if end
4184
			}#if end
4185
		
4185
 
4186
		#如果收到的$msg長度大於 "talkTo:"
4186
		#如果收到的$msg長度大於 "talkTo:"
4187
		if(strlen($msg)>strlen("talkTo:")){
4187
		if(strlen($msg)>strlen("talkTo:")){
4188
			
4188
 
4189
			#如果收到開頭為 "talkTo:"    
4189
			#如果收到開頭為 "talkTo:"
4190
			#涵式說明:
4190
			#涵式說明:
4191
			#取得符合特定字首與字尾的字串
4191
			#取得符合特定字首與字尾的字串
4192
			#回傳的結果:
4192
			#回傳的結果:
4193
			#$result["status"],若爲"true"則代表執行正常;若爲"false"則代表執行失敗。
4193
			#$result["status"],若爲"true"則代表執行正常;若爲"false"則代表執行失敗。
4194
			#$result["function"],當前執行的函數名稱.
4194
			#$result["function"],當前執行的函數名稱.
Line 4205... Line 4205...
4205
			#$conf["tailWord"]="";
4205
			#$conf["tailWord"]="";
4206
			#參考資料:
4206
			#參考資料:
4207
			#str_spilt(),可以將字串依照字母分割成一個個陣列字串。
4207
			#str_spilt(),可以將字串依照字母分割成一個個陣列字串。
4208
			$getMeetConditionsString=search::getMeetConditionsString($conf["search::getMeetConditionsString"]);
4208
			$getMeetConditionsString=search::getMeetConditionsString($conf["search::getMeetConditionsString"]);
4209
			unset($conf["search::getMeetConditionsString"]);
4209
			unset($conf["search::getMeetConditionsString"]);
4210
				
4210
 
4211
			#如果選找前置字串 "talkTo:" 失敗
4211
			#如果選找前置字串 "talkTo:" 失敗
4212
			if($getMeetConditionsString["status"]==="false"){
4212
			if($getMeetConditionsString["status"]==="false"){
4213
				
4213
 
4214
				#設置執行失敗
4214
				#設置執行失敗
4215
				$result["status"]="false";
4215
				$result["status"]="false";
4216
			
4216
 
4217
				#設置執行錯誤訊息
4217
				#設置執行錯誤訊息
4218
				$result["error"]=$getMeetConditionsString;
4218
				$result["error"]=$getMeetConditionsString;
4219
				
4219
 
4220
				#回傳結果
4220
				#回傳結果
4221
				return $result;
4221
				return $result;
4222
				
4222
 
4223
				}#if end
4223
				}#if end
4224
				
4224
 
4225
			#如果存在 "talkTo:" 前置字串
4225
			#如果存在 "talkTo:" 前置字串
4226
			if($getMeetConditionsString["founded"]==="true"){
4226
			if($getMeetConditionsString["founded"]==="true"){
4227
				
4227
 
4228
				#用 "talkTo:" 分割 $buf
4228
				#用 "talkTo:" 分割 $buf
4229
				#涵式說明:
4229
				#涵式說明:
4230
				#將固定格式的字串分開,並回傳分開的結果。
4230
				#將固定格式的字串分開,並回傳分開的結果。
4231
				#回傳結果:
4231
				#回傳結果:
4232
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
4232
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 4242... Line 4242...
4242
				#可省略參數:
4242
				#可省略參數:
4243
				#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
4243
				#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
4244
				$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
4244
				$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
4245
				$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
4245
				$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
4246
				unset($conf["stringProcess::spiltString"]);
4246
				unset($conf["stringProcess::spiltString"]);
4247
				
4247
 
4248
				#如果分割字串失敗
4248
				#如果分割字串失敗
4249
				if($spiltString["status"]==="false"){
4249
				if($spiltString["status"]==="false"){
4250
					
4250
 
4251
					#設置執行失敗
4251
					#設置執行失敗
4252
					$result["status"]="false";
4252
					$result["status"]="false";
4253
				
4253
 
4254
					#設置執行錯誤訊息
4254
					#設置執行錯誤訊息
4255
					$result["error"]=$spiltString;
4255
					$result["error"]=$spiltString;
4256
					
4256
 
4257
					#回傳結果
4257
					#回傳結果
4258
					return $result;
4258
					return $result;
4259
					
4259
 
4260
					}#if end
4260
					}#if end
4261
				
4261
 
4262
				#如果剛好分割出一筆資料
4262
				#如果剛好分割出一筆資料
4263
				if($spiltString["dataCounts"]===1){
4263
				if($spiltString["dataCounts"]===1){
4264
					
4264
 
4265
					#取得自己的id
4265
					#取得自己的id
4266
					$myId=$from->resourceId;
4266
					$myId=$from->resourceId;
4267
					
4267
 
4268
					#取得講話對象的id
4268
					#取得講話對象的id
4269
					$toId=$spiltString["dataArray"][0];
4269
					$toId=$spiltString["dataArray"][0];
4270
					
4270
 
4271
					#設置對象不存在的識別
4271
					#設置對象不存在的識別
4272
					$targetExist=false;
4272
					$targetExist=false;
4273
					
4273
 
4274
					#設置通話對象的key
4274
					#設置通話對象的key
4275
					$targetKey="";					
4275
					$targetKey="";
4276
					
4276
 
4277
					#如果要對話的對象存在
4277
					#如果要對話的對象存在
4278
					if(isset($this->connInfo[$toId])){
4278
					if(isset($this->connInfo[$toId])){
4279
						
4279
 
4280
						#取得對象的userId
4280
						#取得對象的userId
4281
						$toUserId=$this->connInfo[$toId]["userId"];
4281
						$toUserId=$this->connInfo[$toId]["userId"];
4282
						
4282
 
4283
						#取得同userId的對象id們
4283
						#取得同userId的對象id們
4284
						
4284
 
4285
						#初始化同userId的對象id
4285
						#初始化同userId的對象id
4286
						$targetC=array();
4286
						$targetC=array();
4287
						
4287
 
4288
						#針對每個連線
4288
						#針對每個連線
4289
						foreach($this->connInfo as $eachIndex=>$eachInfo){
4289
						foreach($this->connInfo as $eachIndex=>$eachInfo){
4290
							
4290
 
4291
							#如果是自己
4291
							#如果是自己
4292
							if($eachIndex===$myId){
4292
							if($eachIndex===$myId){
4293
								
4293
 
4294
								#跳到下一輪
4294
								#跳到下一輪
4295
								continue;
4295
								continue;
4296
								
4296
 
4297
								}#if end
4297
								}#if end
4298
							
4298
 
4299
							#如果是同userId的對象
4299
							#如果是同userId的對象
4300
							if($eachInfo["userId"]===$toUserId){
4300
							if($eachInfo["userId"]===$toUserId){
4301
								
4301
 
4302
								#取出連線
4302
								#取出連線
4303
								$targetC[$eachIndex]=$this->connInfo[$eachIndex];
4303
								$targetC[$eachIndex]=$this->connInfo[$eachIndex];
4304
								
4304
 
4305
								}#if end
4305
								}#if end
4306
							
4306
 
4307
							}#foreach end
4307
							}#foreach end
4308
										
4308
 
4309
						#針對每個同userId的連線
4309
						#針對每個同userId的連線
4310
						foreach($targetC as $tcIndex=>$tcInfo){
4310
						foreach($targetC as $tcIndex=>$tcInfo){
4311
							
4311
 
4312
							#檢查自己的對話對象是否已經在清單裡面了
4312
							#檢查自己的對話對象是否已經在清單裡面了
4313
						
4313
 
4314
							#假設對象可以加到自己對話清單裡面
4314
							#假設對象可以加到自己對話清單裡面
4315
							$add=true;
4315
							$add=true;
4316
							
4316
 
4317
							#針對每個要講話的對象
4317
							#針對每個要講話的對象
4318
							foreach($this->connInfo[$myId]["talkTo"] as $index=>$cInfo){
4318
							foreach($this->connInfo[$myId]["talkTo"] as $index=>$cInfo){
4319
								
4319
 
4320
								#如果對話對象已經在清單裡面了
4320
								#如果對話對象已經在清單裡面了
4321
								if($this->connInfo[$tcIndex]["userId"]===$cInfo["userId"] && $tcIndex===$cInfo["id"] && $tcInfo["msgId"]===$cInfo["msgId"]){
4321
								if($this->connInfo[$tcIndex]["userId"]===$cInfo["userId"] && $tcIndex===$cInfo["id"] && $tcInfo["msgId"]===$cInfo["msgId"]){
4322
									
4322
 
4323
									#設置不要再加到清單裡面
4323
									#設置不要再加到清單裡面
4324
									$add=false;
4324
									$add=false;
4325
									
4325
 
4326
									#跳出foreach
4326
									#跳出foreach
4327
									break;
4327
									break;
4328
									
4328
 
4329
									}#if end								
4329
									}#if end
4330
								
4330
 
4331
								}#foreach end
4331
								}#foreach end
4332
								
4332
 
4333
							#如果要加入到對話清單裡面
4333
							#如果要加入到對話清單裡面
4334
							if($add){
4334
							if($add){
4335
								
4335
 
4336
								#增加自己的對話對象
4336
								#增加自己的對話對象
4337
								$this->connInfo[$myId]["talkTo"][]=array("id"=>$tcIndex,"userId"=>$this->connInfo[$tcIndex]["userId"],"msgId"=>$this->connInfo[$tcIndex]["msgId"]);
4337
								$this->connInfo[$myId]["talkTo"][]=array("id"=>$tcIndex,"userId"=>$this->connInfo[$tcIndex]["userId"],"msgId"=>$this->connInfo[$tcIndex]["msgId"]);
4338
															
4338
 
4339
								#假設對方可以加自己到對話清單裡面
4339
								#假設對方可以加自己到對話清單裡面
4340
								$add=true;
4340
								$add=true;
4341
								
4341
 
4342
								#檢查通話對象能否跟自己對話
4342
								#檢查通話對象能否跟自己對話
4343
								foreach($this->connInfo[$tcIndex]["talkTo"] as $index=>$cInfo){
4343
								foreach($this->connInfo[$tcIndex]["talkTo"] as $index=>$cInfo){
4344
									
4344
 
4345
									#如果對話對象已經在清單裡面了
4345
									#如果對話對象已經在清單裡面了
4346
									if($this->connInfo[$myId]["userId"]===$cInfo["userId"]){
4346
									if($this->connInfo[$myId]["userId"]===$cInfo["userId"]){
4347
										
4347
 
4348
										#設置不要再加到清單裡面
4348
										#設置不要再加到清單裡面
4349
										$add=false;
4349
										$add=false;
4350
										
4350
 
4351
										#跳出 foreach
4351
										#跳出 foreach
4352
										break;
4352
										break;
4353
										
4353
 
4354
										}#if end
4354
										}#if end
4355
									
4355
 
4356
									}#foreach end
4356
									}#foreach end
4357
									
4357
 
4358
								#如果對方可以加自己到對話清單裡面
4358
								#如果對方可以加自己到對話清單裡面
4359
								if($add){
4359
								if($add){
4360
									
4360
 
4361
									#讓通話對象也可以傳送訊息回來
4361
									#讓通話對象也可以傳送訊息回來
4362
									$this->connInfo[$tcIndex]["talkTo"][]=array("id"=>$myId,"userId"=>$this->connInfo[$myId]["userId"],"msgId"=>$this->connInfo[$myId]["msgId"]);
4362
									$this->connInfo[$tcIndex]["talkTo"][]=array("id"=>$myId,"userId"=>$this->connInfo[$myId]["userId"],"msgId"=>$this->connInfo[$myId]["msgId"]);
4363
									
4363
 
4364
									}#if end
4364
									}#if end
4365
									
4365
 
4366
								#設置成功訊息
4366
								#設置成功訊息
4367
								$msg="client id ".$toId." added to talkTo list";
4367
								$msg="client id ".$toId." added to talkTo list";
4368
								
4368
 
4369
								#包裝訊息
4369
								#包裝訊息
4370
								#"type"為"status"	
4370
								#"type"為"status"
4371
								#"status"為"true"		
4371
								#"status"為"true"
4372
								#"data"為實際的訊息內容
4372
								#"data"為實際的訊息內容
4373
								$packedMsg=array("type"=>"status","status"=>"true","data"=>$msg);
4373
								$packedMsg=array("type"=>"status","status"=>"true","data"=>$msg);
4374
								
4374
 
4375
								#設置要給自己看的訊息
4375
								#設置要給自己看的訊息
4376
								$from->send(json_encode($packedMsg));							
4376
								$from->send(json_encode($packedMsg));
4377
								
4377
 
4378
								}#if end
4378
								}#if end
4379
								
4379
 
4380
							#反之
4380
							#反之
4381
							else{
4381
							else{
4382
								
4382
 
4383
								#設置錯誤訊息
4383
								#設置錯誤訊息
4384
								$msg="client id ".$toId." already in talkTo list";
4384
								$msg="client id ".$toId." already in talkTo list";
4385
								
4385
 
4386
								#包裝訊息
4386
								#包裝訊息
4387
								#"type"為"status"	
4387
								#"type"為"status"
4388
								#"status"為"false"		
4388
								#"status"為"false"
4389
								#"data"為實際的訊息內容
4389
								#"data"為實際的訊息內容
4390
								$packedMsg=array("type"=>"status","status"=>"false","data"=>$msg);
4390
								$packedMsg=array("type"=>"status","status"=>"false","data"=>$msg);
4391
								
4391
 
4392
								#設置要給自己看的訊息
4392
								#設置要給自己看的訊息
4393
								$from->send(json_encode($packedMsg));
4393
								$from->send(json_encode($packedMsg));
4394
								
4394
 
4395
								}#else end
4395
								}#else end
4396
							
4396
 
4397
							}#foreach end
4397
							}#foreach end
4398
						
4398
 
4399
						#回傳成功
4399
						#回傳成功
4400
						return true;
4400
						return true;
4401
						
4401
 
4402
						}#if end
4402
						}#if end
4403
					
4403
 
4404
					#反之不存在
4404
					#反之不存在
4405
					else{
4405
					else{
4406
						
4406
 
4407
						#設置錯誤訊息
4407
						#設置錯誤訊息
4408
						$msg="client id ".$toId." doesn't exist";
4408
						$msg="client id ".$toId." doesn't exist";
4409
						
4409
 
4410
						#包裝訊息
4410
						#包裝訊息
4411
						#"type"為"status"	
4411
						#"type"為"status"
4412
						#"status"為"false"		
4412
						#"status"為"false"
4413
						#"data"為實際的訊息內容
4413
						#"data"為實際的訊息內容
4414
						$packedMsg=array("type"=>"status","status"=>"false","data"=>$msg);
4414
						$packedMsg=array("type"=>"status","status"=>"false","data"=>$msg);
4415
						
4415
 
4416
						#設置要給自己看的訊息
4416
						#設置要給自己看的訊息
4417
						$from->send(json_encode($packedMsg));
4417
						$from->send(json_encode($packedMsg));
4418
						
4418
 
4419
						#回傳成功
4419
						#回傳成功
4420
						return true;
4420
						return true;
4421
						
4421
 
4422
						}#else end		
4422
						}#else end
4423
					
4423
 
4424
					}#if end
4424
					}#if end
4425
				
4425
 
4426
				}#if end
4426
				}#if end
4427
			
4427
 
4428
			}#if end
4428
			}#if end
4429
		
4429
 
4430
		#預設沒有收到確認收到訊息的回應訊息
4430
		#預設沒有收到確認收到訊息的回應訊息
4431
		$mIndex="false";
4431
		$mIndex="false";
4432
		
4432
 
4433
		#如果收到的$msg長度大於 "msgId:"
4433
		#如果收到的$msg長度大於 "msgId:"
4434
		if(strlen($msg)>strlen("msgId:")){
4434
		if(strlen($msg)>strlen("msgId:")){
4435
			
4435
 
4436
			#解析 $msg
4436
			#解析 $msg
4437
			
4437
 
4438
			#如果收到開頭為 "msgId:"    
4438
			#如果收到開頭為 "msgId:"
4439
			#涵式說明:
4439
			#涵式說明:
4440
			#取得符合特定字首與字尾的字串
4440
			#取得符合特定字首與字尾的字串
4441
			#回傳的結果:
4441
			#回傳的結果:
4442
			#$result["status"],若爲"true"則代表執行正常;若爲"false"則代表執行失敗。
4442
			#$result["status"],若爲"true"則代表執行正常;若爲"false"則代表執行失敗。
4443
			#$result["function"],當前執行的函數名稱.
4443
			#$result["function"],當前執行的函數名稱.
Line 4454... Line 4454...
4454
			#$conf["tailWord"]="";
4454
			#$conf["tailWord"]="";
4455
			#參考資料:
4455
			#參考資料:
4456
			#str_spilt(),可以將字串依照字母分割成一個個陣列字串。
4456
			#str_spilt(),可以將字串依照字母分割成一個個陣列字串。
4457
			$getMeetConditionsString=search::getMeetConditionsString($conf["search::getMeetConditionsString"]);
4457
			$getMeetConditionsString=search::getMeetConditionsString($conf["search::getMeetConditionsString"]);
4458
			unset($conf["search::getMeetConditionsString"]);
4458
			unset($conf["search::getMeetConditionsString"]);
4459
				
4459
 
4460
			#如果選找前置字串 "msgId:" 失敗
4460
			#如果選找前置字串 "msgId:" 失敗
4461
			if($getMeetConditionsString["status"]==="false"){
4461
			if($getMeetConditionsString["status"]==="false"){
4462
				
4462
 
4463
				#設置執行失敗
4463
				#設置執行失敗
4464
				$result["status"]="false";
4464
				$result["status"]="false";
4465
			
4465
 
4466
				#設置執行錯誤訊息
4466
				#設置執行錯誤訊息
4467
				$result["error"]=$getMeetConditionsString;
4467
				$result["error"]=$getMeetConditionsString;
4468
				
4468
 
4469
				#回傳結果
4469
				#回傳結果
4470
				return $result;
4470
				return $result;
4471
				
4471
 
4472
				}#if end
4472
				}#if end
4473
				
4473
 
4474
			#如果存在	 "msgId:" 前置字串
4474
			#如果存在	 "msgId:" 前置字串
4475
			if($getMeetConditionsString["founded"]==="true"){
4475
			if($getMeetConditionsString["founded"]==="true"){
4476
				
4476
 
4477
				#用 "msgId:" 分割 $msg
4477
				#用 "msgId:" 分割 $msg
4478
				#涵式說明:
4478
				#涵式說明:
4479
				#將固定格式的字串分開,並回傳分開的結果。
4479
				#將固定格式的字串分開,並回傳分開的結果。
4480
				#回傳結果:
4480
				#回傳結果:
4481
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
4481
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 4491... Line 4491...
4491
				#可省略參數:
4491
				#可省略參數:
4492
				#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
4492
				#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
4493
				$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
4493
				$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
4494
				$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
4494
				$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
4495
				unset($conf["stringProcess::spiltString"]);
4495
				unset($conf["stringProcess::spiltString"]);
4496
				
4496
 
4497
				#如果分割字串失敗
4497
				#如果分割字串失敗
4498
				if($spiltString["status"]==="false"){
4498
				if($spiltString["status"]==="false"){
4499
					
4499
 
4500
					#設置執行失敗
4500
					#設置執行失敗
4501
					$result["status"]="false";
4501
					$result["status"]="false";
4502
				
4502
 
4503
					#設置執行錯誤訊息
4503
					#設置執行錯誤訊息
4504
					$result["error"]=$spiltString;
4504
					$result["error"]=$spiltString;
4505
					
4505
 
4506
					#回傳結果
4506
					#回傳結果
4507
					return $result;
4507
					return $result;
4508
					
4508
 
4509
					}#if end
4509
					}#if end
4510
				
4510
 
4511
				#如果剛好分割出一筆資料
4511
				#如果剛好分割出一筆資料
4512
				if($spiltString["dataCounts"]===1){
4512
				if($spiltString["dataCounts"]===1){
4513
					
4513
 
4514
					#如果尚未設置過msgId
4514
					#如果尚未設置過msgId
4515
					if($this->connInfo[$from->resourceId]["gotMsgId"]==="false"){
4515
					if($this->connInfo[$from->resourceId]["gotMsgId"]==="false"){
4516
													
4516
 
4517
						#取得待確認訊息id
4517
						#取得待確認訊息id
4518
						$msgId=$spiltString["dataArray"][0];
4518
						$msgId=$spiltString["dataArray"][0];
4519
						
4519
 
4520
						#初始化儲存現有msgId的變數
4520
						#初始化儲存現有msgId的變數
4521
						$msgIdArray=array();
4521
						$msgIdArray=array();
4522
						
4522
 
4523
						#取得現有的msgId
4523
						#取得現有的msgId
4524
						foreach($this->connInfo as $cInfo){
4524
						foreach($this->connInfo as $cInfo){
4525
							
4525
 
4526
							#如果msgId不為空
4526
							#如果msgId不為空
4527
							if($cInfo["msgId"]!==""){
4527
							if($cInfo["msgId"]!==""){
4528
								
4528
 
4529
								#儲存msgId
4529
								#儲存msgId
4530
								$msgIdArray[]=$cInfo["msgId"];
4530
								$msgIdArray[]=$cInfo["msgId"];
4531
								
4531
 
4532
								}#if end
4532
								}#if end
4533
							
4533
 
4534
							}#foreach end
4534
							}#foreach end
4535
						
4535
 
4536
						#檢查msgId是否存在於$msgIdArray裡面
4536
						#檢查msgId是否存在於$msgIdArray裡面
4537
						if(in_array($msgId,$msgIdArray)){
4537
						if(in_array($msgId,$msgIdArray)){
4538
							
4538
 
4539
							#設置自己的msgId
4539
							#設置自己的msgId
4540
							$this->connInfo[$from->resourceId]["msgId"]=$msgId;						
4540
							$this->connInfo[$from->resourceId]["msgId"]=$msgId;
4541
							
4541
 
4542
							#設置已經設置過msgId
4542
							#設置已經設置過msgId
4543
							$this->connInfo[$from->resourceId]["gotMsgId"]="true";
4543
							$this->connInfo[$from->resourceId]["gotMsgId"]="true";
4544
							
4544
 
4545
							#設置成功訊息
4545
							#設置成功訊息
4546
							$msg="set msgId successfully";
4546
							$msg="set msgId successfully";
4547
							
4547
 
4548
							#包裝訊息
4548
							#包裝訊息
4549
							#"type"為"status"	
4549
							#"type"為"status"
4550
							#"status"為"false"		
4550
							#"status"為"false"
4551
							#"data"為實際的訊息內容
4551
							#"data"為實際的訊息內容
4552
							$packedMsg=array("type"=>"status","status"=>"true","data"=>$msg);
4552
							$packedMsg=array("type"=>"status","status"=>"true","data"=>$msg);
4553
							
4553
 
4554
							#設置要給自己看的訊息
4554
							#設置要給自己看的訊息
4555
							$from->send(json_encode($packedMsg));
4555
							$from->send(json_encode($packedMsg));
4556
							
4556
 
4557
							#針對每個連線
4557
							#針對每個連線
4558
							foreach($this->connInfo as $connIndex=>$connInfo){
4558
							foreach($this->connInfo as $connIndex=>$connInfo){
4559
							
4559
 
4560
								#如果是跟發訊息者同msgId但id不一樣的連線
4560
								#如果是跟發訊息者同msgId但id不一樣的連線
4561
								if($connInfo["msgId"]===$msgId && $connIndex!==$from->resourceId){
4561
								if($connInfo["msgId"]===$msgId && $connIndex!==$from->resourceId){
4562
									
4562
 
4563
									#將id為$connIndex的連線斷線
4563
									#將id為$connIndex的連線斷線
4564
									$this->clients->detach($this->connInfo[$connIndex]["conn"]);
4564
									$this->clients->detach($this->connInfo[$connIndex]["conn"]);
4565
 
4565
 
4566
									#提示已經將連線斷開
4566
									#提示已經將連線斷開
4567
									echo "Connection {$connIndex} has disconnected\n";
4567
									echo "Connection {$connIndex} has disconnected\n";
4568
									
4568
 
4569
									#移除先前同msgId但id不一樣的連線
4569
									#移除先前同msgId但id不一樣的連線
4570
									unset($this->connInfo[$connIndex]);
4570
									unset($this->connInfo[$connIndex]);
4571
									
4571
 
4572
									#針對每個連線
4572
									#針對每個連線
4573
									foreach($this->connInfo as $cIndexForTD=>$cInfoForTD){
4573
									foreach($this->connInfo as $cIndexForTD=>$cInfoForTD){
4574
										
4574
 
4575
										#針對 talkTo 的每個項目
4575
										#針對 talkTo 的每個項目
4576
										foreach($cInfoForTD["talkTo"] as $ttIndex=>$ttInfo){
4576
										foreach($cInfoForTD["talkTo"] as $ttIndex=>$ttInfo){
4577
											
4577
 
4578
											#如果是不存在的通話對象
4578
											#如果是不存在的通話對象
4579
											if($ttInfo["id"]===$connIndex){
4579
											if($ttInfo["id"]===$connIndex){
4580
												
4580
 
4581
												#移除之
4581
												#移除之
4582
												unset($this->connInfo[$cIndexForTD]["talkTo"][$ttIndex]);
4582
												unset($this->connInfo[$cIndexForTD]["talkTo"][$ttIndex]);
4583
												
4583
 
4584
												}#if end
4584
												}#if end
4585
											
4585
 
4586
											}#foreach end
4586
											}#foreach end
4587
										
4587
 
4588
										}#foreach end
4588
										}#foreach end
4589
									
4589
 
4590
									}#if end
4590
									}#if end
4591
								
4591
 
4592
								}#foreach end
4592
								}#foreach end
4593
							
4593
 
4594
							#如果有要確認的訊息陣列
4594
							#如果有要確認的訊息陣列
4595
							if(isset($this->unConfirmMsg[$msgId])){
4595
							if(isset($this->unConfirmMsg[$msgId])){
4596
								
4596
 
4597
								#初始化要傳送的待確認訊息
4597
								#初始化要傳送的待確認訊息
4598
								$packedMsg=array();
4598
								$packedMsg=array();
4599
								
4599
 
4600
								#有幾個要確認的訊息就執行幾次
4600
								#有幾個要確認的訊息就執行幾次
4601
								foreach($this->unConfirmMsg[$msgId] as $unCMindex=>$unCMinfo){
4601
								foreach($this->unConfirmMsg[$msgId] as $unCMindex=>$unCMinfo){
4602
									
4602
 
4603
									#如果待確認訊息的傳送目標id不等於剛要求更換msgId的用戶id
4603
									#如果待確認訊息的傳送目標id不等於剛要求更換msgId的用戶id
4604
									if($unCMinfo["toId"]!==$from->resourceId){
4604
									if($unCMinfo["toId"]!==$from->resourceId){
4605
										
4605
 
4606
										#設置傳送目標id為要求更換msgId的用戶id
4606
										#設置傳送目標id為要求更換msgId的用戶id
4607
										$this->unConfirmMsg[$msgId][$unCMindex]["toId"]=$from->resourceId;
4607
										$this->unConfirmMsg[$msgId][$unCMindex]["toId"]=$from->resourceId;
4608
										
4608
 
4609
										#檢查所有連線資訊
4609
										#檢查所有連線資訊
4610
										foreach($this->connInfo as $connId=>$connInfo){
4610
										foreach($this->connInfo as $connId=>$connInfo){
4611
											
4611
 
4612
											#依據talkTo清單的每個對象
4612
											#依據talkTo清單的每個對象
4613
											foreach($connInfo["talkTo"] as $talk2Index=>$talk2Info){
4613
											foreach($connInfo["talkTo"] as $talk2Index=>$talk2Info){
4614
												
4614
 
4615
												#若對象的id為$unCMinfo["toId"]且userId為$this->connInfo[$from->resourceId]["userId"]且msgId為$this->connInfo[$from->resourceId]["msgId"]者
4615
												#若對象的id為$unCMinfo["toId"]且userId為$this->connInfo[$from->resourceId]["userId"]且msgId為$this->connInfo[$from->resourceId]["msgId"]者
4616
												if($talk2Info["id"]===$unCMinfo["toId"] && $talk2Info["userId"]===$this->connInfo[$from->resourceId]["userId"] && $talk2Info["msgId"]===$this->connInfo[$from->resourceId]["msgId"]){
4616
												if($talk2Info["id"]===$unCMinfo["toId"] && $talk2Info["userId"]===$this->connInfo[$from->resourceId]["userId"] && $talk2Info["msgId"]===$this->connInfo[$from->resourceId]["msgId"]){
4617
													
4617
 
4618
													#移除該talkTo對象
4618
													#移除該talkTo對象
4619
													unset($this->connInfo[$connId]["talkTo"][$talk2Index]);
4619
													unset($this->connInfo[$connId]["talkTo"][$talk2Index]);
4620
													
4620
 
4621
													}#if ded
4621
													}#if ded
4622
												
4622
 
4623
												}#foreach end
4623
												}#foreach end
4624
											
4624
 
4625
											}#foreach end
4625
											}#foreach end
4626
										
4626
 
4627
										}#if end
4627
										}#if end
4628
									
4628
 
4629
									#如果要傳送的待確認訊息為空陣列
4629
									#如果要傳送的待確認訊息為空陣列
4630
									if($packedMsg===array()){
4630
									if($packedMsg===array()){
4631
										
4631
 
4632
										#包裝訊息
4632
										#包裝訊息
4633
										#"type"為"msg"
4633
										#"type"為"msg"
4634
										#"index"為$cInfo["id"]
4634
										#"index"為$cInfo["id"]
4635
										#"data"為實際的訊息內容
4635
										#"data"為實際的訊息內容
4636
										$packedMsg=array("type"=>"msg","index"=>$unCMindex,"data"=>$unCMinfo["msg"]);
4636
										$packedMsg=array("type"=>"msg","index"=>$unCMindex,"data"=>$unCMinfo["msg"]);
4637
										
4637
 
4638
										}#if end
4638
										}#if end
4639
																		
-
 
-
 
4639
 
4640
									}#foreach end
4640
									}#foreach end
4641
									
4641
 
4642
								#如果要傳送的待確認訊息不為空陣列
4642
								#如果要傳送的待確認訊息不為空陣列
4643
								if($packedMsg!==array()){
4643
								if($packedMsg!==array()){
4644
									
4644
 
4645
									#var_dump("重新登入更改msgId後要傳送的未確認訊息:".print_r($packedMsg,true));
4645
									#var_dump("重新登入更改msgId後要傳送的未確認訊息:".print_r($packedMsg,true));
4646
									
4646
 
4647
									#傳送包裝好的待確認訊息給對方
4647
									#傳送包裝好的待確認訊息給對方
4648
									$from->send(json_encode($packedMsg));
4648
									$from->send(json_encode($packedMsg));
4649
									
4649
 
4650
									}#if end
4650
									}#if end
4651
								
4651
 
4652
								}#if end
4652
								}#if end
4653
							
4653
 
4654
							#結束程式
4654
							#結束程式
4655
							return true;
4655
							return true;
4656
							
4656
 
4657
							}#if end
4657
							}#if end
4658
							
4658
 
4659
							#反之想要用的msgId並不存在於server
4659
							#反之想要用的msgId並不存在於server
4660
							else{
4660
							else{
4661
								
4661
 
4662
								#設置錯誤訊息
4662
								#設置錯誤訊息
4663
								$msg="msgId 「".$msgId."」 doesn't exist";
4663
								$msg="msgId 「".$msgId."」 doesn't exist";
4664
								
4664
 
4665
								#包裝訊息
4665
								#包裝訊息
4666
								#"type"為"status"	
4666
								#"type"為"status"
4667
								#"status"為"false"		
4667
								#"status"為"false"
4668
								#"data"為實際的訊息內容
4668
								#"data"為實際的訊息內容
4669
								$packedMsg=array("type"=>"status","status"=>"false","data"=>$msg);
4669
								$packedMsg=array("type"=>"status","status"=>"false","data"=>$msg);
4670
								
4670
 
4671
								#設置要給自己看的訊息
4671
								#設置要給自己看的訊息
4672
								$from->send(json_encode($packedMsg));
4672
								$from->send(json_encode($packedMsg));
4673
 
4673
 
4674
								#結束程式
4674
								#結束程式
4675
								return true;
4675
								return true;
4676
								
4676
 
4677
								}#else end
4677
								}#else end
4678
						
4678
 
4679
						}#if end
4679
						}#if end
4680
					
4680
 
4681
					#反之已經設置過 msgId 了
4681
					#反之已經設置過 msgId 了
4682
					else{
4682
					else{
4683
						
4683
 
4684
						#設置錯誤訊息
4684
						#設置錯誤訊息
4685
						$msg="msgId already set";
4685
						$msg="msgId already set";
4686
						
4686
 
4687
						#包裝訊息
4687
						#包裝訊息
4688
						#"type"為"status"	
4688
						#"type"為"status"
4689
						#"status"為"false"		
4689
						#"status"為"false"
4690
						#"data"為實際的訊息內容
4690
						#"data"為實際的訊息內容
4691
						$packedMsg=array("type"=>"status","status"=>"false","data"=>$msg);
4691
						$packedMsg=array("type"=>"status","status"=>"false","data"=>$msg);
4692
						
4692
 
4693
						#設置要給自己看的訊息
4693
						#設置要給自己看的訊息
4694
						$from->send(json_encode($packedMsg));
4694
						$from->send(json_encode($packedMsg));
4695
 
4695
 
4696
						#結束程式
4696
						#結束程式
4697
						return true;
4697
						return true;
4698
						
4698
 
4699
						}#else end
4699
						}#else end
4700
					
4700
 
4701
					}#if end
4701
					}#if end
4702
					
4702
 
4703
				#設置失敗訊息
4703
				#設置失敗訊息
4704
				$msg="set msgId failed";
4704
				$msg="set msgId failed";
4705
				
4705
 
4706
				#包裝訊息
4706
				#包裝訊息
4707
				#"type"為"status"	
4707
				#"type"為"status"
4708
				#"status"為"false"		
4708
				#"status"為"false"
4709
				#"data"為實際的訊息內容
4709
				#"data"為實際的訊息內容
4710
				$packedMsg=array("type"=>"status","status"=>"false","data"=>$msg);
4710
				$packedMsg=array("type"=>"status","status"=>"false","data"=>$msg);
4711
				
4711
 
4712
				#設置要給自己看的訊息
4712
				#設置要給自己看的訊息
4713
				$from->send(json_encode($packedMsg));
4713
				$from->send(json_encode($packedMsg));
4714
 
4714
 
4715
				#結束程式
4715
				#結束程式
4716
				return true;
4716
				return true;
4717
					
4717
 
4718
				}#if end
4718
				}#if end
4719
			
4719
 
4720
			}#if end
4720
			}#if end
4721
		
4721
 
4722
		#如果訊息長度大於 strlen("mIndex:")
4722
		#如果訊息長度大於 strlen("mIndex:")
4723
		if(strlen($msg)>strlen("mIndex:")){
4723
		if(strlen($msg)>strlen("mIndex:")){
4724
			
4724
 
4725
			#如果是確認收到訊息的回應訊息開頭 "mIndex:"    
4725
			#如果是確認收到訊息的回應訊息開頭 "mIndex:"
4726
			#涵式說明:
4726
			#涵式說明:
4727
			#取得符合特定字首與字尾的字串
4727
			#取得符合特定字首與字尾的字串
4728
			#回傳的結果:
4728
			#回傳的結果:
4729
			#$result["status"],若爲"true"則代表執行正常;若爲"false"則代表執行失敗。
4729
			#$result["status"],若爲"true"則代表執行正常;若爲"false"則代表執行失敗。
4730
			#$result["function"],當前執行的函數名稱.
4730
			#$result["function"],當前執行的函數名稱.
Line 4741... Line 4741...
4741
			#$conf["tailWord"]="";
4741
			#$conf["tailWord"]="";
4742
			#參考資料:
4742
			#參考資料:
4743
			#str_spilt(),可以將字串依照字母分割成一個個陣列字串。
4743
			#str_spilt(),可以將字串依照字母分割成一個個陣列字串。
4744
			$getMeetConditionsString=search::getMeetConditionsString($conf["search::getMeetConditionsString"]);
4744
			$getMeetConditionsString=search::getMeetConditionsString($conf["search::getMeetConditionsString"]);
4745
			unset($conf["search::getMeetConditionsString"]);
4745
			unset($conf["search::getMeetConditionsString"]);
4746
				
4746
 
4747
			#如果選找前置字串 "mIndex:" 失敗
4747
			#如果選找前置字串 "mIndex:" 失敗
4748
			if($getMeetConditionsString["status"]==="false"){
4748
			if($getMeetConditionsString["status"]==="false"){
4749
				
4749
 
4750
				#設置執行失敗
4750
				#設置執行失敗
4751
				$result["status"]="false";
4751
				$result["status"]="false";
4752
			
4752
 
4753
				#設置執行錯誤訊息
4753
				#設置執行錯誤訊息
4754
				$result["error"]=$getMeetConditionsString;
4754
				$result["error"]=$getMeetConditionsString;
4755
				
4755
 
4756
				#回傳結果
4756
				#回傳結果
4757
				return $result;
4757
				return $result;
4758
				
4758
 
4759
				}#if end
4759
				}#if end
4760
			
4760
 
4761
			#如果存在	 "mIndex:" 前置字串
4761
			#如果存在	 "mIndex:" 前置字串
4762
			if($getMeetConditionsString["founded"]==="true"){
4762
			if($getMeetConditionsString["founded"]==="true"){
4763
				
4763
 
4764
				#用 "mIndex:" 分割 $buf
4764
				#用 "mIndex:" 分割 $buf
4765
				#涵式說明:
4765
				#涵式說明:
4766
				#將固定格式的字串分開,並回傳分開的結果。
4766
				#將固定格式的字串分開,並回傳分開的結果。
4767
				#回傳結果:
4767
				#回傳結果:
4768
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
4768
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 4778... Line 4778...
4778
				#可省略參數:
4778
				#可省略參數:
4779
				#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
4779
				#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
4780
				$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
4780
				$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
4781
				$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
4781
				$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
4782
				unset($conf["stringProcess::spiltString"]);
4782
				unset($conf["stringProcess::spiltString"]);
4783
				
4783
 
4784
				#如果分割字串失敗
4784
				#如果分割字串失敗
4785
				if($spiltString["status"]==="false"){
4785
				if($spiltString["status"]==="false"){
4786
					
4786
 
4787
					#設置執行失敗
4787
					#設置執行失敗
4788
					$result["status"]="false";
4788
					$result["status"]="false";
4789
				
4789
 
4790
					#設置執行錯誤訊息
4790
					#設置執行錯誤訊息
4791
					$result["error"]=$spiltString;
4791
					$result["error"]=$spiltString;
4792
					
4792
 
4793
					#回傳結果
4793
					#回傳結果
4794
					return $result;
4794
					return $result;
4795
					
4795
 
4796
					}#if end
4796
					}#if end
4797
					
4797
 
4798
				#如果存在分割的識別字串
4798
				#如果存在分割的識別字串
4799
				if($spiltString["found"]==="true"){
4799
				if($spiltString["found"]==="true"){
4800
					
4800
 
4801
					#如果只分割出一段
4801
					#如果只分割出一段
4802
					if($spiltString["dataCounts"]===1){
4802
					if($spiltString["dataCounts"]===1){
4803
						
4803
 
4804
						#取得待確認訊息的索引
4804
						#取得待確認訊息的索引
4805
						$mIndex=$spiltString["dataArray"][0];
4805
						$mIndex=$spiltString["dataArray"][0];
4806
						
4806
 
4807
						}#if end
4807
						}#if end
4808
					
4808
 
4809
					}#if end
4809
					}#if end
4810
				
4810
 
4811
				}#if end
4811
				}#if end
4812
			
4812
 
4813
			}#if end
4813
			}#if end
4814
		
4814
 
4815
		#如果server收到訊息長度大於 "cmd:"
4815
		#如果server收到訊息長度大於 "cmd:"
4816
		if(strlen($msg)>strlen("cmd:")){
4816
		if(strlen($msg)>strlen("cmd:")){
4817
			
4817
 
4818
			#如果$msg開頭為"cmd:"  
4818
			#如果$msg開頭為"cmd:"
4819
			#涵式說明:
4819
			#涵式說明:
4820
			#取得符合特定字首與字尾的字串
4820
			#取得符合特定字首與字尾的字串
4821
			#回傳的結果:
4821
			#回傳的結果:
4822
			#$result["status"],若爲"true"則代表執行正常;若爲"false"則代表執行失敗。
4822
			#$result["status"],若爲"true"則代表執行正常;若爲"false"則代表執行失敗。
4823
			#$result["function"],當前執行的函數名稱.
4823
			#$result["function"],當前執行的函數名稱.
Line 4834... Line 4834...
4834
			#$conf["tailWord"]="";
4834
			#$conf["tailWord"]="";
4835
			#參考資料:
4835
			#參考資料:
4836
			#str_spilt(),可以將字串依照字母分割成一個個陣列字串。
4836
			#str_spilt(),可以將字串依照字母分割成一個個陣列字串。
4837
			$getMeetConditionsString=search::getMeetConditionsString($conf["search::getMeetConditionsString"]);
4837
			$getMeetConditionsString=search::getMeetConditionsString($conf["search::getMeetConditionsString"]);
4838
			unset($conf["search::getMeetConditionsString"]);
4838
			unset($conf["search::getMeetConditionsString"]);
4839
				
4839
 
4840
			#如果選找前置字串 "cmd:" 失敗
4840
			#如果選找前置字串 "cmd:" 失敗
4841
			if($getMeetConditionsString["status"]==="false"){
4841
			if($getMeetConditionsString["status"]==="false"){
4842
				
4842
 
4843
				#設置執行失敗
4843
				#設置執行失敗
4844
				$result["status"]="false";
4844
				$result["status"]="false";
4845
			
4845
 
4846
				#設置執行錯誤訊息
4846
				#設置執行錯誤訊息
4847
				$result["error"]=$getMeetConditionsString;
4847
				$result["error"]=$getMeetConditionsString;
4848
				
4848
 
4849
				#回傳結果
4849
				#回傳結果
4850
				return $result;
4850
				return $result;
4851
				
4851
 
4852
				}#if end
4852
				}#if end
4853
				
4853
 
4854
			#如果存在	 "cmd:" 前置字串
4854
			#如果存在	 "cmd:" 前置字串
4855
			if($getMeetConditionsString["founded"]==="true"){
4855
			if($getMeetConditionsString["founded"]==="true"){
4856
				
4856
 
4857
				#用 "cmd:" 分割 $buf
4857
				#用 "cmd:" 分割 $buf
4858
				#涵式說明:
4858
				#涵式說明:
4859
				#將固定格式的字串分開,並回傳分開的結果。
4859
				#將固定格式的字串分開,並回傳分開的結果。
4860
				#回傳結果:
4860
				#回傳結果:
4861
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
4861
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 4871... Line 4871...
4871
				#可省略參數:
4871
				#可省略參數:
4872
				#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
4872
				#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
4873
				$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
4873
				$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
4874
				$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
4874
				$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
4875
				unset($conf["stringProcess::spiltString"]);
4875
				unset($conf["stringProcess::spiltString"]);
4876
				
4876
 
4877
				#如果分割字串失敗
4877
				#如果分割字串失敗
4878
				if($spiltString["status"]==="false"){
4878
				if($spiltString["status"]==="false"){
4879
					
4879
 
4880
					#設置執行失敗
4880
					#設置執行失敗
4881
					$result["status"]="false";
4881
					$result["status"]="false";
4882
				
4882
 
4883
					#設置執行錯誤訊息
4883
					#設置執行錯誤訊息
4884
					$result["error"]=$spiltString;
4884
					$result["error"]=$spiltString;
4885
					
4885
 
4886
					#回傳結果
4886
					#回傳結果
4887
					return $result;
4887
					return $result;
4888
					
4888
 
4889
					}#if end
4889
					}#if end
4890
				
4890
 
4891
				#如果剛好分割出一筆資料
4891
				#如果剛好分割出一筆資料
4892
				if($spiltString["dataCounts"]===1){
4892
				if($spiltString["dataCounts"]===1){
4893
										
4893
 
4894
					#取的要執行的指令
4894
					#取的要執行的指令
4895
					$cmd=$spiltString["dataArray"][0];
4895
					$cmd=$spiltString["dataArray"][0];
4896
					
4896
 
4897
					#函式說明:
4897
					#函式說明:
4898
					#呼叫shell執行系統命令,並取得回傳的內容.
4898
					#呼叫shell執行系統命令,並取得回傳的內容.
4899
					#回傳的結果:
4899
					#回傳的結果:
4900
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
4900
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
4901
					#$result["error"],錯誤訊息陣列.
4901
					#$result["error"],錯誤訊息陣列.
Line 4904... Line 4904...
4904
					#$result["output"],爲執行完二元碼後的輸出陣列.
4904
					#$result["output"],爲執行完二元碼後的輸出陣列.
4905
					#必填的參數
4905
					#必填的參數
4906
					#$conf["command"],字串,要執行的指令與.
4906
					#$conf["command"],字串,要執行的指令與.
4907
					$conf["external::callShell"]["command"]=$cmd;
4907
					$conf["external::callShell"]["command"]=$cmd;
4908
					#$conf["fileArgu"],字串,變數__FILE__的內容.
4908
					#$conf["fileArgu"],字串,變數__FILE__的內容.
4909
					$conf["external::callShell"]["fileArgu"]=self::$fileArgu;		
4909
					$conf["external::callShell"]["fileArgu"]=self::$fileArgu;
4910
					#可省略參數:
4910
					#可省略參數:
4911
					#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
4911
					#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
4912
					#$conf["argu"]=array("");
4912
					#$conf["argu"]=array("");
4913
					#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
4913
					#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
4914
					#$conf["arguIsAddr"]=array();
4914
					#$conf["arguIsAddr"]=array();
Line 4934... Line 4934...
4934
					#exec=>http://php.net/manual/en/function.exec.php
4934
					#exec=>http://php.net/manual/en/function.exec.php
4935
					#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
4935
					#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
4936
					#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
4936
					#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
4937
					$callShell=external::callShell($conf["external::callShell"]);
4937
					$callShell=external::callShell($conf["external::callShell"]);
4938
					unset($conf["external::callShell"]);
4938
					unset($conf["external::callShell"]);
4939
					
4939
 
4940
					#如果執行外部指令失敗
4940
					#如果執行外部指令失敗
4941
					if($callShell["status"]==="false"){
4941
					if($callShell["status"]==="false"){
4942
						
4942
 
4943
						#設置要傳給client的訊息
4943
						#設置要傳給client的訊息
4944
						$result=array("data"=>$callShell,"type"=>"status","status"=>"false");
4944
						$result=array("data"=>$callShell,"type"=>"status","status"=>"false");
4945
						
4945
 
4946
						#將程式執行後的錯誤輸出傳給client					
4946
						#將程式執行後的錯誤輸出傳給client
4947
						$from->send(json_encode($result));
4947
						$from->send(json_encode($result));
4948
						
4948
 
4949
						#回傳結果
4949
						#回傳結果
4950
						return true;
4950
						return true;
4951
						
4951
 
4952
						}#if end
4952
						}#if end
4953
					
4953
 
4954
					#設置要傳給client的訊息
4954
					#設置要傳給client的訊息
4955
					$result=array("data"=>$callShell["output"],"type"=>"cmd");
4955
					$result=array("data"=>$callShell["output"],"type"=>"cmd");
4956
					
4956
 
4957
					#將程式執行後的輸出傳給client					
4957
					#將程式執行後的輸出傳給client
4958
					$from->send(json_encode($result));
4958
					$from->send(json_encode($result));
4959
					
4959
 
4960
					#結束程式
4960
					#結束程式
4961
					return true;
4961
					return true;
4962
					
4962
 
4963
					}#if end
4963
					}#if end
4964
					
4964
 
4965
				}#if end
4965
				}#if end
4966
			
4966
 
4967
			}#if end
4967
			}#if end
4968
		
4968
 
4969
		#如果有要講話的對象
4969
		#如果有要講話的對象
4970
		if(count($this->connInfo[$from->resourceId]["talkTo"])>0){
4970
		if(count($this->connInfo[$from->resourceId]["talkTo"])>0){
4971
						
4971
 
4972
			#依據每個對話對象
4972
			#依據每個對話對象
4973
			foreach($this->connInfo[$from->resourceId]["talkTo"] as $index => $cInfo){
4973
			foreach($this->connInfo[$from->resourceId]["talkTo"] as $index => $cInfo){
4974
								
4974
 
4975
				#如果要講話的對象不存在(socket已經斷開)
4975
				#如果要講話的對象不存在(socket已經斷開)
4976
				if(!isset($this->connInfo[$cInfo["id"]])){
4976
				if(!isset($this->connInfo[$cInfo["id"]])){
4977
					
4977
 
4978
					#將訊息儲存起來,等對象上線後再把訊息傳過去.
4978
					#將訊息儲存起來,等對象上線後再把訊息傳過去.
4979
					$this->unSendMsg[]=array(
4979
					$this->unSendMsg[]=array(
4980
						"fromId"=>$from->resourceId,
4980
						"fromId"=>$from->resourceId,
4981
						"toId"=>"",
4981
						"toId"=>"",
4982
						"fromUserId"=>$this->connInfo[$from->resourceId]["userId"],
4982
						"fromUserId"=>$this->connInfo[$from->resourceId]["userId"],
4983
						"toUserId"=>$cInfo["userId"],
4983
						"toUserId"=>$cInfo["userId"],
4984
						"msg"=>$msg
4984
						"msg"=>$msg
4985
						);
4985
						);
4986
					
4986
 
4987
					}#if end
4987
					}#if end
4988
					
4988
 
4989
				#反之,講話的對象存在
4989
				#反之,講話的對象存在
4990
				else{
4990
				else{
4991
					
4991
 
4992
					#如果收到確認收到訊息的回應
4992
					#如果收到確認收到訊息的回應
4993
					if($mIndex!=="false"){
4993
					if($mIndex!=="false"){
4994
						
4994
 
4995
						#儲存對話目標的msgId
4995
						#儲存對話目標的msgId
4996
						$targetMsgId=$this->connInfo[$from->resourceId]["msgId"];
4996
						$targetMsgId=$this->connInfo[$from->resourceId]["msgId"];
4997
							
4997
 
4998
						#針對每個連線的待確認訊息
4998
						#針對每個連線的待確認訊息
4999
						foreach($this->unConfirmMsg as $unCMindex=>$unCMinfo){
4999
						foreach($this->unConfirmMsg as $unCMindex=>$unCMinfo){
5000
							
5000
 
5001
							#debug
5001
							#debug
5002
							#var_dump($unCMindex."!==".$targetMsgId." && count(\$unCMinfo):".count($unCMinfo));
5002
							#var_dump($unCMindex."!==".$targetMsgId." && count(\$unCMinfo):".count($unCMinfo));
5003
							
5003
 
5004
							#如果 未確認訊息達到10則以上 且不是接收訊息者的msgId
5004
							#如果 未確認訊息達到10則以上 且不是接收訊息者的msgId
5005
							if($unCMindex!==$targetMsgId && count($unCMinfo)>10){
5005
							if($unCMindex!==$targetMsgId && count($unCMinfo)>10){
5006
								
5006
 
5007
								#代表接收訊息的用戶無法透過網路跟server溝通
5007
								#代表接收訊息的用戶無法透過網路跟server溝通
5008
														
5008
 
5009
								#提示 server 連線已結束
5009
								#提示 server 連線已結束
5010
								echo "Connection {$unCMinfo["id"]} has disconnected\n";
5010
								echo "Connection {$unCMinfo["id"]} has disconnected\n";
5011
								
5011
 
5012
								#將待驗證的訊息變成留言訊息
5012
								#將待驗證的訊息變成留言訊息
5013
								
5013
 
5014
								#有幾個未確認訊息就執行幾次
5014
								#有幾個未確認訊息就執行幾次
5015
								foreach($this->unConfirmMsg[$unCMindex] as $unConMsgIndex => $unConMsgInfo){
5015
								foreach($this->unConfirmMsg[$unCMindex] as $unConMsgIndex => $unConMsgInfo){
5016
									
5016
 
5017
									#將訊息儲存起來,等對象上線後再把訊息傳過去.
5017
									#將訊息儲存起來,等對象上線後再把訊息傳過去.
5018
									$this->unSendMsg[]=array(
5018
									$this->unSendMsg[]=array(
5019
										"fromId"=>$unConMsgInfo["fromId"],
5019
										"fromId"=>$unConMsgInfo["fromId"],
5020
										"toId"=>"",
5020
										"toId"=>"",
5021
										"fromUserId"=>$unConMsgInfo["fromUserId"],
5021
										"fromUserId"=>$unConMsgInfo["fromUserId"],
5022
										"toUserId"=>$unConMsgInfo["toUserId"],
5022
										"toUserId"=>$unConMsgInfo["toUserId"],
5023
										"msg"=>$unConMsgInfo["msg"]
5023
										"msg"=>$unConMsgInfo["msg"]
5024
										);
5024
										);
5025
									
5025
 
5026
									}#foreach end
5026
									}#foreach end
5027
								
5027
 
5028
								#移除待驗證的訊息資訊
5028
								#移除待驗證的訊息資訊
5029
								unset($this->unConfirmMsg[$unCMindex]);
5029
								unset($this->unConfirmMsg[$unCMindex]);
5030
								
5030
 
5031
								#將client的連線斷開
5031
								#將client的連線斷開
5032
								$this->clients->detach($this->connInfo[$unConMsgInfo["fromId"]]["conn"]);
5032
								$this->clients->detach($this->connInfo[$unConMsgInfo["fromId"]]["conn"]);
5033
								
5033
 
5034
								#移除連線的資訊
5034
								#移除連線的資訊
5035
								unset($this->connInfo[$unConMsgInfo["fromId"]]);								
5035
								unset($this->connInfo[$unConMsgInfo["fromId"]]);
5036
								
5036
 
5037
								}#if end
5037
								}#if end
5038
							
5038
 
5039
							}#foreach end
5039
							}#foreach end
5040
																		
-
 
-
 
5040
 
5041
						#存在收到訊息用戶id的待確認訊息
5041
						#存在收到訊息用戶id的待確認訊息
5042
						if(isset($this->unConfirmMsg[$targetMsgId])){
5042
						if(isset($this->unConfirmMsg[$targetMsgId])){
5043
							
5043
 
5044
							#如果該未確認訊息存在
5044
							#如果該未確認訊息存在
5045
							if(isset($this->unConfirmMsg[$targetMsgId][$mIndex])){
5045
							if(isset($this->unConfirmMsg[$targetMsgId][$mIndex])){
5046
								
5046
 
5047
								#debug
5047
								#debug
5048
								#var_dump("移除待確認的訊息".print_r($this->unConfirmMsg[$targetMsgId][$mIndex],true));
5048
								#var_dump("移除待確認的訊息".print_r($this->unConfirmMsg[$targetMsgId][$mIndex],true));
5049
								
5049
 
5050
								#移除該確認訊息
5050
								#移除該確認訊息
5051
								unset($this->unConfirmMsg[$targetMsgId][$mIndex]);
5051
								unset($this->unConfirmMsg[$targetMsgId][$mIndex]);
5052
								
5052
 
5053
								#還剩下幾個待確認訊息就執行幾次
5053
								#還剩下幾個待確認訊息就執行幾次
5054
								foreach($this->unConfirmMsg[$targetMsgId] as $unCMindex=>$unCMinfo){
5054
								foreach($this->unConfirmMsg[$targetMsgId] as $unCMindex=>$unCMinfo){
5055
									
5055
 
5056
									#如果後面還有訊息
5056
									#如果後面還有訊息
5057
									if(isset($this->unConfirmMsg[$targetMsgId][$unCMindex+1])){
5057
									if(isset($this->unConfirmMsg[$targetMsgId][$unCMindex+1])){
5058
										
5058
 
5059
										#如果與下一份訊息的 fromUserId一樣, toUserId 一樣, fromId一樣, toId 不同, msg 一樣.
5059
										#如果與下一份訊息的 fromUserId一樣, toUserId 一樣, fromId一樣, toId 不同, msg 一樣.
5060
										if(
5060
										if(
5061
											(
5061
											(
5062
												$this->unConfirmMsg[$targetMsgId][$unCMindex]["fromUserId"]===$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["fromUserId"]
5062
												$this->unConfirmMsg[$targetMsgId][$unCMindex]["fromUserId"]===$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["fromUserId"]
5063
												)
5063
												)
Line 5070... Line 5070...
5070
												$this->unConfirmMsg[$targetMsgId][$unCMindex]["fromId"]===$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["fromId"]
5070
												$this->unConfirmMsg[$targetMsgId][$unCMindex]["fromId"]===$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["fromId"]
5071
												)
5071
												)
5072
											&&
5072
											&&
5073
											(
5073
											(
5074
												$this->unConfirmMsg[$targetMsgId][$unCMindex]["toId"]!==$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["toId"]
5074
												$this->unConfirmMsg[$targetMsgId][$unCMindex]["toId"]!==$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["toId"]
5075
												)	
5075
												)
5076
											&&
5076
											&&
5077
											(
5077
											(
5078
												$this->unConfirmMsg[$targetMsgId][$unCMindex]["msg"]===$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["msg"]
5078
												$this->unConfirmMsg[$targetMsgId][$unCMindex]["msg"]===$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["msg"]
5079
												)									
5079
												)
5080
											){
5080
											){
5081
											
5081
 
5082
											#卸除該筆重複的訊息
5082
											#卸除該筆重複的訊息
5083
											unset($this->unConfirmMsg[$targetMsgId][$unCMindex]);
5083
											unset($this->unConfirmMsg[$targetMsgId][$unCMindex]);
5084
												
5084
 
5085
											#跳到下一則訊息
5085
											#跳到下一則訊息
5086
											continue;	
5086
											continue;
5087
												
5087
 
5088
											}#if end
5088
											}#if end
5089
											
5089
 
5090
										/*	
5090
										/*
5091
										#debug
5091
										#debug
5092
										else{
5092
										else{
5093
											
5093
 
5094
											var_dump($this->unConfirmMsg[$targetMsgId][$unCMindex]["fromUserId"].":".$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["fromUserId"]);
5094
											var_dump($this->unConfirmMsg[$targetMsgId][$unCMindex]["fromUserId"].":".$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["fromUserId"]);
5095
											var_dump($this->unConfirmMsg[$targetMsgId][$unCMindex]["fromId"].":".$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["fromId"]);
5095
											var_dump($this->unConfirmMsg[$targetMsgId][$unCMindex]["fromId"].":".$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["fromId"]);
5096
											
5096
 
5097
											var_dump($this->unConfirmMsg[$targetMsgId][$unCMindex]["toUserId"].":".$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["toUserId"]);
5097
											var_dump($this->unConfirmMsg[$targetMsgId][$unCMindex]["toUserId"].":".$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["toUserId"]);
5098
											var_dump($this->unConfirmMsg[$targetMsgId][$unCMindex]["toId"].":".$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["toId"]);
5098
											var_dump($this->unConfirmMsg[$targetMsgId][$unCMindex]["toId"].":".$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["toId"]);
5099
											
5099
 
5100
											var_dump($this->unConfirmMsg[$targetMsgId][$unCMindex]["msg"].":".$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["msg"]);
5100
											var_dump($this->unConfirmMsg[$targetMsgId][$unCMindex]["msg"].":".$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["msg"]);
5101
											
5101
 
5102
											}#else end
5102
											}#else end
5103
										*/
5103
										*/
5104
												
5104
 
5105
										}#if end
5105
										}#if end
5106
									
5106
 
5107
									#包裝訊息
5107
									#包裝訊息
5108
									#"type"為"msg"
5108
									#"type"為"msg"
5109
									#"index"為$cInfo["id"]
5109
									#"index"為$cInfo["id"]
5110
									#"data"為實際的訊息內容
5110
									#"data"為實際的訊息內容
5111
									$packedMsg=array("type"=>"msg","index"=>$unCMindex,"data"=>$unCMinfo["msg"]);
5111
									$packedMsg=array("type"=>"msg","index"=>$unCMindex,"data"=>$unCMinfo["msg"]);
5112
										
5112
 
5113
									#傳送包裝好的待確認訊息給對方
5113
									#傳送包裝好的待確認訊息給對方
5114
									$from->send(json_encode($packedMsg));
5114
									$from->send(json_encode($packedMsg));
5115
									
5115
 
5116
									#一次只送一則訊息
5116
									#一次只送一則訊息
5117
									break;
5117
									break;
5118
									
5118
 
5119
									}#foreach end
5119
									}#foreach end
5120
								
5120
 
5121
								}#if end
5121
								}#if end
5122
							
5122
 
5123
							}#if end
5123
							}#if end
5124
						
5124
 
5125
						}#if end
5125
						}#if end
5126
					
5126
 
5127
					#反之為要傳送的訊息
5127
					#反之為要傳送的訊息
5128
					else{
5128
					else{
5129
						
5129
 
5130
						#儲存對話目標的msgId
5130
						#儲存對話目標的msgId
5131
						$targetMsgId=$this->connInfo[$cInfo["id"]]["msgId"];
5131
						$targetMsgId=$this->connInfo[$cInfo["id"]]["msgId"];
5132
						
5132
 
5133
						#$unConfirmMsg[$msgId],為訊息接收方的$msgId,亦即收到訊息後,要跟server說有收到訊息者.
5133
						#$unConfirmMsg[$msgId],為訊息接收方的$msgId,亦即收到訊息後,要跟server說有收到訊息者.
5134
						#"fromId",訊息來源的id
5134
						#"fromId",訊息來源的id
5135
						#"toId",訊息目的的id
5135
						#"toId",訊息目的的id
5136
						#"fromUserId",訊息來源的userId
5136
						#"fromUserId",訊息來源的userId
5137
						#"toUserId",訊息目標的userId
5137
						#"toUserId",訊息目標的userId
5138
						#"msg",傳送的訊息
5138
						#"msg",傳送的訊息
5139
						#"msgId",訊息目標連線的msgId
5139
						#"msgId",訊息目標連線的msgId
5140
						#儲存待確認傳送的訊息到 $unConfirmMsg
5140
						#儲存待確認傳送的訊息到 $unConfirmMsg
5141
						$this->unConfirmMsg[$targetMsgId][]=array('fromId'=>$from->resourceId,'toId'=>$cInfo["id"],'fromUserId'=>$this->connInfo[$from->resourceId]["userId"],'toUserId'=>$cInfo["userId"],'msg'=>$msg,'msgId'=>$this->connInfo[$cInfo["id"]]["msgId"]);
5141
						$this->unConfirmMsg[$targetMsgId][]=array('fromId'=>$from->resourceId,'toId'=>$cInfo["id"],'fromUserId'=>$this->connInfo[$from->resourceId]["userId"],'toUserId'=>$cInfo["userId"],'msg'=>$msg,'msgId'=>$this->connInfo[$cInfo["id"]]["msgId"]);
5142
									
5142
 
5143
						#var_dump($this->unConfirmMsg[$targetMsgId]);			
5143
						#var_dump($this->unConfirmMsg[$targetMsgId]);
5144
												
5144
 
5145
						#如果未確認的訊息數量已經達到10則
5145
						#如果未確認的訊息數量已經達到10則
5146
						if(count($this->unConfirmMsg[$targetMsgId])>10){
5146
						if(count($this->unConfirmMsg[$targetMsgId])>10){
5147
							
5147
 
5148
							#代表用戶無法透過網路跟server溝通
5148
							#代表用戶無法透過網路跟server溝通
5149
														
5149
 
5150
							#提示 server 連線已結束
5150
							#提示 server 連線已結束
5151
							echo "Connection {$cInfo["id"]} has disconnected\n";
5151
							echo "Connection {$cInfo["id"]} has disconnected\n";
5152
							
5152
 
5153
							#將待驗證的訊息變成留言訊息
5153
							#將待驗證的訊息變成留言訊息
5154
							
5154
 
5155
							#有幾個未確認訊息就執行幾次
5155
							#有幾個未確認訊息就執行幾次
5156
							foreach($this->unConfirmMsg[$targetMsgId] as $unCMindex => $unCMinfo){
5156
							foreach($this->unConfirmMsg[$targetMsgId] as $unCMindex => $unCMinfo){
5157
								
5157
 
5158
								#將訊息儲存起來,等對象上線後再把訊息傳過去.
5158
								#將訊息儲存起來,等對象上線後再把訊息傳過去.
5159
								$this->unSendMsg[]=array(
5159
								$this->unSendMsg[]=array(
5160
									"fromId"=>$unCMinfo["fromId"],
5160
									"fromId"=>$unCMinfo["fromId"],
5161
									"toId"=>"",
5161
									"toId"=>"",
5162
									"fromUserId"=>$unCMinfo["fromUserId"],
5162
									"fromUserId"=>$unCMinfo["fromUserId"],
5163
									"toUserId"=>$unCMinfo["toUserId"],
5163
									"toUserId"=>$unCMinfo["toUserId"],
5164
									"msg"=>$unCMinfo["msg"]
5164
									"msg"=>$unCMinfo["msg"]
5165
									);
5165
									);
5166
								
5166
 
5167
								}#foreach end
5167
								}#foreach end
5168
							
5168
 
5169
							#移除待驗證的訊息資訊
5169
							#移除待驗證的訊息資訊
5170
							unset($this->unConfirmMsg[$targetMsgId]);
5170
							unset($this->unConfirmMsg[$targetMsgId]);
5171
							
5171
 
5172
							#將client的連線斷開
5172
							#將client的連線斷開
5173
							$this->clients->detach($this->connInfo[$cInfo["id"]]["conn"]);
5173
							$this->clients->detach($this->connInfo[$cInfo["id"]]["conn"]);
5174
							
5174
 
5175
							#移除連線的資訊
5175
							#移除連線的資訊
5176
							unset($this->connInfo[$cInfo["id"]]);
5176
							unset($this->connInfo[$cInfo["id"]]);
5177
							
5177
 
5178
							}#if end
5178
							}#if end
5179
						
5179
 
5180
						#反之
5180
						#反之
5181
						else{
5181
						else{
5182
							
5182
 
5183
							#裡面有幾個待確認的訊息就執行幾次
5183
							#裡面有幾個待確認的訊息就執行幾次
5184
							foreach($this->unConfirmMsg[$targetMsgId] as $unCMindex=>$unCMinfo){
5184
							foreach($this->unConfirmMsg[$targetMsgId] as $unCMindex=>$unCMinfo){
5185
								
5185
 
5186
								#如果後面還有訊息
5186
								#如果後面還有訊息
5187
								if(isset($this->unConfirmMsg[$targetMsgId][$unCMindex+1])){
5187
								if(isset($this->unConfirmMsg[$targetMsgId][$unCMindex+1])){
5188
									
5188
 
5189
									#debug
5189
									#debug
5190
									#var_dump($this->unConfirmMsg[$targetMsgId]);
5190
									#var_dump($this->unConfirmMsg[$targetMsgId]);
5191
									
5191
 
5192
									#如果與下一份訊息的 fromUserId一樣, toUserId 一樣, fromId一樣, toId 不同, msg 一樣.
5192
									#如果與下一份訊息的 fromUserId一樣, toUserId 一樣, fromId一樣, toId 不同, msg 一樣.
5193
									if(
5193
									if(
5194
										(
5194
										(
5195
											$this->unConfirmMsg[$targetMsgId][$unCMindex]["fromUserId"]===$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["fromUserId"]
5195
											$this->unConfirmMsg[$targetMsgId][$unCMindex]["fromUserId"]===$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["fromUserId"]
5196
											)
5196
											)
Line 5203... Line 5203...
5203
											$this->unConfirmMsg[$targetMsgId][$unCMindex]["fromId"]===$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["fromId"]
5203
											$this->unConfirmMsg[$targetMsgId][$unCMindex]["fromId"]===$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["fromId"]
5204
											)
5204
											)
5205
										&&
5205
										&&
5206
										(
5206
										(
5207
											$this->unConfirmMsg[$targetMsgId][$unCMindex]["toId"]!==$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["toId"]
5207
											$this->unConfirmMsg[$targetMsgId][$unCMindex]["toId"]!==$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["toId"]
5208
											)	
5208
											)
5209
										&&
5209
										&&
5210
										(
5210
										(
5211
											$this->unConfirmMsg[$targetMsgId][$unCMindex]["msg"]===$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["msg"]
5211
											$this->unConfirmMsg[$targetMsgId][$unCMindex]["msg"]===$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["msg"]
5212
											)									
5212
											)
5213
										){
5213
										){
5214
										
5214
 
5215
										#卸除該筆重複的訊息
5215
										#卸除該筆重複的訊息
5216
										unset($this->unConfirmMsg[$targetMsgId][$unCMindex]);
5216
										unset($this->unConfirmMsg[$targetMsgId][$unCMindex]);
5217
											
5217
 
5218
										#跳到下一則訊息
5218
										#跳到下一則訊息
5219
										continue;	
5219
										continue;
5220
											
5220
 
5221
										}#if end
5221
										}#if end
5222
									
5222
 
5223
									/*	
5223
									/*
5224
									#debug
5224
									#debug
5225
									else{
5225
									else{
5226
										
5226
 
5227
										var_dump($this->unConfirmMsg[$targetMsgId][$unCMindex]["toUserId"].":".$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["toUserId"]);
5227
										var_dump($this->unConfirmMsg[$targetMsgId][$unCMindex]["toUserId"].":".$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["toUserId"]);
5228
										var_dump($this->unConfirmMsg[$targetMsgId][$unCMindex]["toId"].":".$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["toId"]);
5228
										var_dump($this->unConfirmMsg[$targetMsgId][$unCMindex]["toId"].":".$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["toId"]);
5229
										var_dump($this->unConfirmMsg[$targetMsgId][$unCMindex]["msg"].":".$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["msg"]);
5229
										var_dump($this->unConfirmMsg[$targetMsgId][$unCMindex]["msg"].":".$this->unConfirmMsg[$targetMsgId][$unCMindex+1]["msg"]);
5230
										
5230
 
5231
										}#else end
5231
										}#else end
5232
									*/
5232
									*/
5233
									
5233
 
5234
									}#if end
5234
									}#if end
5235
								
5235
 
5236
								#debug
5236
								#debug
5237
								#var_dump("index:".$targetMsgId.print_r($this->unConfirmMsg[$targetMsgId],true));
5237
								#var_dump("index:".$targetMsgId.print_r($this->unConfirmMsg[$targetMsgId],true));
5238
																
-
 
-
 
5238
 
5239
								#另存 ConnectionInterface 物件
5239
								#另存 ConnectionInterface 物件
5240
								$connObject=$this->connInfo[$cInfo["id"]]["conn"];
5240
								$connObject=$this->connInfo[$cInfo["id"]]["conn"];
5241
									
5241
 
5242
								#包裝訊息
5242
								#包裝訊息
5243
								#"type"為"msg"
5243
								#"type"為"msg"
5244
								#"index"為$cInfo["id"]
5244
								#"index"為$cInfo["id"]
5245
								#"data"為實際的訊息內容
5245
								#"data"為實際的訊息內容
5246
								$packedMsg=array("type"=>"msg","index"=>$unCMindex,"data"=>$unCMinfo["msg"]);
5246
								$packedMsg=array("type"=>"msg","index"=>$unCMindex,"data"=>$unCMinfo["msg"]);
5247
									
5247
 
5248
								#傳送包裝好的訊息給對方
5248
								#傳送包裝好的訊息給對方
5249
								$connObject->send(json_encode($packedMsg));
5249
								$connObject->send(json_encode($packedMsg));
5250
								
5250
 
5251
								#一次只送一則訊息
5251
								#一次只送一則訊息
5252
								break;
5252
								break;
5253
								
5253
 
5254
								}#foreach end
5254
								}#foreach end
5255
							
5255
 
5256
							}#else end
5256
							}#else end
5257
						
5257
 
5258
						}#else end	
5258
						}#else end
5259
					
5259
 
5260
					}#else end
5260
					}#else end
5261
				
5261
 
5262
				}#foreach end
5262
				}#foreach end
5263
			
5263
 
5264
			}#if end
5264
			}#if end
5265
			
5265
 
5266
		#反之沒有講話的對象
5266
		#反之沒有講話的對象
5267
		else{
5267
		else{
5268
			
5268
 
5269
			$msg="Message 「".$msg."」 will not be received by any one";
5269
			$msg="Message 「".$msg."」 will not be received by any one";
5270
			
5270
 
5271
			#提示server訊息被拋棄
5271
			#提示server訊息被拋棄
5272
			echo $msg;
5272
			echo $msg;
5273
			
5273
 
5274
			#包裝訊息
5274
			#包裝訊息
5275
			#"type"為"status"	
5275
			#"type"為"status"
5276
			#"status"為"false"		
5276
			#"status"為"false"
5277
			#"data"為實際的訊息內容
5277
			#"data"為實際的訊息內容
5278
			$packedMsg=array("type"=>"status","status"=>"false","data"=>$msg);
5278
			$packedMsg=array("type"=>"status","status"=>"false","data"=>$msg);
5279
			
5279
 
5280
			#設置要給自己看的訊息
5280
			#設置要給自己看的訊息
5281
			$from->send(json_encode($packedMsg));
5281
			$from->send(json_encode($packedMsg));
5282
			
5282
 
5283
			}#else end
5283
			}#else end
5284
			
5284
 
5285
		}#function onMessage end
5285
		}#function onMessage end
5286
 
5286
 
5287
	#當clinet的連線斷掉前
5287
	#當clinet的連線斷掉前
5288
	public function onClose(ConnectionInterface $conn){
5288
	public function onClose(ConnectionInterface $conn){
5289
		
5289
 
5290
		// The connection is closed, remove it, as we can no longer send it messages
5290
		// The connection is closed, remove it, as we can no longer send it messages
5291
		$this->clients->detach($conn);
5291
		$this->clients->detach($conn);
5292
 
5292
 
5293
		#提示 server 連線已結束
5293
		#提示 server 連線已結束
5294
	 	echo "Connection {$conn->resourceId} has disconnected\n";
5294
	 	echo "Connection {$conn->resourceId} has disconnected\n";
5295
		
5295
 
5296
		#取得斷線目標的待確認訊息id
5296
		#取得斷線目標的待確認訊息id
5297
		$msgId=$this->connInfo[$conn->resourceId]["msgId"];
5297
		$msgId=$this->connInfo[$conn->resourceId]["msgId"];
5298
		
5298
 
5299
		#移除目標id的待確認訊息
5299
		#移除目標id的待確認訊息
5300
		unset($this->unConfirmMsg[$msgId]);
5300
		unset($this->unConfirmMsg[$msgId]);
5301
		
5301
 
5302
		#移除連線的資訊
5302
		#移除連線的資訊
5303
		unset($this->connInfo[$conn->resourceId]);
5303
		unset($this->connInfo[$conn->resourceId]);
5304
		
5304
 
5305
		}#function onClose end
5305
		}#function onClose end
5306
 
5306
 
5307
	#當出現錯誤時
5307
	#當出現錯誤時
5308
	public function onError(ConnectionInterface $conn, \Exception $e){
5308
	public function onError(ConnectionInterface $conn, \Exception $e){
5309
		
5309
 
5310
		#提示出現連線錯誤
5310
		#提示出現連線錯誤
5311
		echo "An error has occurred: {$e->getMessage()}\n";
5311
		echo "An error has occurred: {$e->getMessage()}\n";
5312
 
5312
 
5313
		#關閉socket
5313
		#關閉socket
5314
        	$conn->close();
5314
        	$conn->close();
5315
        
5315
 
5316
		}#fucntion onError end
5316
		}#fucntion onError end
5317
    
5317
 
5318
	}#class ChatV3 end
5318
	}#class ChatV3 end
5319
    
5319
 
5320
#用 soldier 類別來實作 MessageComponentInterface 界面,提供驗證身份後就可以執行系統命令的界面.
5320
#用 soldier 類別來實作 MessageComponentInterface 界面,提供驗證身份後就可以執行系統命令的界面.
5321
class soldier implements MessageComponentInterface{
5321
class soldier implements MessageComponentInterface{
5322
	
5322
 
5323
	#初始化儲存使用者資訊的陣列
5323
	#初始化儲存使用者資訊的陣列
5324
	private $connInfo=array();	
5324
	private $connInfo=array();
5325
	
5325
 
5326
	#初始化一開始尚未能夠連線到資料庫
5326
	#初始化一開始尚未能夠連線到資料庫
5327
	private static $dbTouchable=false;
5327
	private static $dbTouchable=false;
5328
		
5328
 
5329
	#初始化db的位置
5329
	#初始化db的位置
5330
	public static $dbAddress="localhost";	
5330
	public static $dbAddress="localhost";
5331
		
5331
 
5332
	#初始化連線db用的帳戶
5332
	#初始化連線db用的帳戶
5333
	public static $dbAccount="root";
5333
	public static $dbAccount="root";
5334
	
5334
 
5335
	#初始化連線的db名稱
5335
	#初始化連線的db名稱
5336
	public static $dbName="test";
5336
	public static $dbName="test";
5337
	
5337
 
5338
	#初始化連線db時用的密碼
5338
	#初始化連線db時用的密碼
5339
	public static $dbPassword="";
5339
	public static $dbPassword="";
5340
	
5340
 
5341
	#初始化連線db的哪個資料表
5341
	#初始化連線db的哪個資料表
5342
	public static $memberTableName="member";
5342
	public static $memberTableName="member";
5343
		
5343
 
5344
	#初始化登入時用來驗證帳號的資料表欄位名稱
5344
	#初始化登入時用來驗證帳號的資料表欄位名稱
5345
	public static $accountCol="account";
5345
	public static $accountCol="account";
5346
	
5346
 
5347
	#初始化登入時用來驗證密碼的資料表欄位名稱,設為""代表不用密碼來認證
5347
	#初始化登入時用來驗證密碼的資料表欄位名稱,設為""代表不用密碼來認證
5348
	public static $passwordCol="password";
5348
	public static $passwordCol="password";
5349
	
5349
 
5350
	#初始化用來儲存 __FILE__ 內容的變數 fileArgu
5350
	#初始化用來儲存 __FILE__ 內容的變數 fileArgu
5351
	public static $fileArgu=__FILE__;
5351
	public static $fileArgu=__FILE__;
5352
	
5352
 
5353
	#當用戶與伺服器建立連線時
5353
	#當用戶與伺服器建立連線時
5354
    	public function onOpen(ConnectionInterface $conn){
5354
    	public function onOpen(ConnectionInterface $conn){
5355
		
5355
 
5356
		// Store the new connection to send messages to later
5356
		// Store the new connection to send messages to later
5357
        	$this->clients->attach($conn);
5357
        	$this->clients->attach($conn);
5358
 
5358
 
5359
		#提示server有clent連上web socket.
5359
		#提示server有clent連上web socket.
5360
       		echo "New connection! ({$conn->resourceId})\n";
5360
       		echo "New connection! ({$conn->resourceId})\n";
5361
        
5361
 
5362
        	#用client的id為index來儲存額外的資訊
5362
        	#用client的id為index來儲存額外的資訊
5363
        	#元素 "conn" 代表已經建立連線的ConnectionInterface物件
5363
        	#元素 "conn" 代表已經建立連線的ConnectionInterface物件
5364
        	#元素 "talkTo" 代表要跟誰講話
5364
        	#元素 "talkTo" 代表要跟誰講話
5365
		#元素 "userId" 代表辨識使用者的id
5365
		#元素 "userId" 代表辨識使用者的id
5366
		#元素 "loigin=array("ac","pw")" 代表client輸入的帳號與密碼
5366
		#元素 "loigin=array("ac","pw")" 代表client輸入的帳號與密碼
5367
        	$this->connInfo[$conn->resourceId]=array("conn"=>$conn,"userId"=>"","login"=>array("ac"=>"","pw"=>""));
5367
        	$this->connInfo[$conn->resourceId]=array("conn"=>$conn,"userId"=>"","login"=>array("ac"=>"","pw"=>""));
5368
		
5368
 
5369
		#包裝訊息
5369
		#包裝訊息
5370
		#"type"為"login"	
5370
		#"type"為"login"
5371
		#"status"為"true"		
5371
		#"status"為"true"
5372
		#"data"為實際的訊息內容
5372
		#"data"為實際的訊息內容
5373
		$packedMsg=array("type"=>"login","status"=>"true","data"=>"Please input your account! ex:account:ws1");
5373
		$packedMsg=array("type"=>"login","status"=>"true","data"=>"Please input your account! ex:account:ws1");
5374
		
5374
 
5375
		#提示輸入帳號	
5375
		#提示輸入帳號
5376
		$conn->send(json_encode($packedMsg));
5376
		$conn->send(json_encode($packedMsg));
5377
				
5377
 
5378
		}#function onOpen end
5378
		}#function onOpen end
5379
	
5379
 
5380
	#當伺服器收到訊息時
5380
	#當伺服器收到訊息時
5381
	public function onMessage(ConnectionInterface $from, $msg){
5381
	public function onMessage(ConnectionInterface $from, $msg){
5382
				
5382
 
5383
		#如果該連線沒有使用者id
5383
		#如果該連線沒有使用者id
5384
		if($this->connInfo[$from->resourceId]["userId"]===""){
5384
		if($this->connInfo[$from->resourceId]["userId"]===""){
5385
			
5385
 
5386
			#如果尚未輸入帳戶
5386
			#如果尚未輸入帳戶
5387
			if($this->connInfo[$from->resourceId]["login"]["ac"]===""){
5387
			if($this->connInfo[$from->resourceId]["login"]["ac"]===""){
5388
			
5388
 
5389
				#如果 $msg 長度大於 "account:"
5389
				#如果 $msg 長度大於 "account:"
5390
				if(strlen($msg) > strlen("account:")){
5390
				if(strlen($msg) > strlen("account:")){
5391
					
5391
 
5392
					#檢查有無前置字元 "account:"
5392
					#檢查有無前置字元 "account:"
5393
					#函式說明:
5393
					#函式說明:
5394
					#將字串特定關鍵字與其前面的內容剔除
5394
					#將字串特定關鍵字與其前面的內容剔除
5395
					#回傳結果:
5395
					#回傳結果:
5396
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
5396
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 5403... Line 5403...
5403
					#必填的參數:
5403
					#必填的參數:
5404
					$conf["stringProcess::delStrBeforeKeyWord"]["stringIn"]=$msg;#要處理的字串.
5404
					$conf["stringProcess::delStrBeforeKeyWord"]["stringIn"]=$msg;#要處理的字串.
5405
					$conf["stringProcess::delStrBeforeKeyWord"]["keyWord"]="account:";#特定字串.
5405
					$conf["stringProcess::delStrBeforeKeyWord"]["keyWord"]="account:";#特定字串.
5406
					$delStrBeforeKeyWord=stringProcess::delStrBeforeKeyWord($conf["stringProcess::delStrBeforeKeyWord"]);
5406
					$delStrBeforeKeyWord=stringProcess::delStrBeforeKeyWord($conf["stringProcess::delStrBeforeKeyWord"]);
5407
					unset($conf["stringProcess::delStrBeforeKeyWord"]);
5407
					unset($conf["stringProcess::delStrBeforeKeyWord"]);
5408
					
5408
 
5409
					#如果移除前置字元失敗
5409
					#如果移除前置字元失敗
5410
					if($delStrBeforeKeyWord["status"]==="false"){
5410
					if($delStrBeforeKeyWord["status"]==="false"){
5411
						
5411
 
5412
						#設置執行失敗
5412
						#設置執行失敗
5413
						$result["status"]="false";
5413
						$result["status"]="false";
5414
					
5414
 
5415
						#設置執行錯誤訊息
5415
						#設置執行錯誤訊息
5416
						$result["error"]=$delStrBeforeKeyWord;
5416
						$result["error"]=$delStrBeforeKeyWord;
5417
						
5417
 
5418
						#回傳結果
5418
						#回傳結果
5419
						return $result;
5419
						return $result;
5420
						
5420
 
5421
						}#if end
5421
						}#if end
5422
						
5422
 
5423
					#如果有符合條件的前置字元
5423
					#如果有符合條件的前置字元
5424
					if($delStrBeforeKeyWord["founded"]==="true"){
5424
					if($delStrBeforeKeyWord["founded"]==="true"){
5425
						
5425
 
5426
						#儲存帳戶
5426
						#儲存帳戶
5427
						$this->connInfo[$from->resourceId]["login"]["ac"]=$delStrBeforeKeyWord["content"];
5427
						$this->connInfo[$from->resourceId]["login"]["ac"]=$delStrBeforeKeyWord["content"];
5428
												
5428
 
5429
						}#if end
5429
						}#if end
5430
					
5430
 
5431
					#反之代表格式錯誤	
5431
					#反之代表格式錯誤
5432
					else{
5432
					else{
5433
						
5433
 
5434
						#包裝訊息
5434
						#包裝訊息
5435
						#"type"為"login"	
5435
						#"type"為"login"
5436
						#"status"為"true"		
5436
						#"status"為"true"
5437
						#"data"為實際的訊息內容
5437
						#"data"為實際的訊息內容
5438
						$packedMsg=array("type"=>"login","status"=>"true","data"=>"Please input your account! ex:account:ws1");
5438
						$packedMsg=array("type"=>"login","status"=>"true","data"=>"Please input your account! ex:account:ws1");
5439
						
5439
 
5440
						#提示輸入帳號	
5440
						#提示輸入帳號
5441
						$from->send(json_encode($packedMsg));
5441
						$from->send(json_encode($packedMsg));
5442
						
5442
 
5443
						#結束程式
5443
						#結束程式
5444
						return true;
5444
						return true;
5445
						
5445
 
5446
						}#else end
5446
						}#else end
5447
					
5447
 
5448
					}#if end
5448
					}#if end
5449
					
5449
 
5450
				#反之代表輸入錯誤格式的account
5450
				#反之代表輸入錯誤格式的account
5451
				else{
5451
				else{
5452
					
5452
 
5453
					#包裝訊息
5453
					#包裝訊息
5454
					#"type"為"login"	
5454
					#"type"為"login"
5455
					#"status"為"true"		
5455
					#"status"為"true"
5456
					#"data"為實際的訊息內容
5456
					#"data"為實際的訊息內容
5457
					$packedMsg=array("type"=>"login","status"=>"true","data"=>"Please input your account! ex:account:ws1");
5457
					$packedMsg=array("type"=>"login","status"=>"true","data"=>"Please input your account! ex:account:ws1");
5458
					
5458
 
5459
					#提示輸入帳號	
5459
					#提示輸入帳號
5460
					$from->send(json_encode($packedMsg));
5460
					$from->send(json_encode($packedMsg));
5461
					
5461
 
5462
					#結束程式
5462
					#結束程式
5463
					return true;
5463
					return true;
5464
					
5464
 
5465
					}#else end
5465
					}#else end
5466
				
5466
 
5467
				}#if end
5467
				}#if end
5468
						
5468
 
5469
			#如果有設置密碼欄位
5469
			#如果有設置密碼欄位
5470
			if(self::$passwordCol!==""){
5470
			if(self::$passwordCol!==""){
5471
				
5471
 
5472
				#如果尚未輸入密碼
5472
				#如果尚未輸入密碼
5473
				if($this->connInfo[$from->resourceId]["login"]["pw"]===""){
5473
				if($this->connInfo[$from->resourceId]["login"]["pw"]===""){
5474
					
5474
 
5475
					#$msg 長度大於 "password:"
5475
					#$msg 長度大於 "password:"
5476
					if(strlen($msg) > strlen("password:")){
5476
					if(strlen($msg) > strlen("password:")){
5477
						
5477
 
5478
						#檢查有無前置字元 "password:"
5478
						#檢查有無前置字元 "password:"
5479
						#函式說明:
5479
						#函式說明:
5480
						#將字串特定關鍵字與其前面的內容剔除
5480
						#將字串特定關鍵字與其前面的內容剔除
5481
						#回傳結果:
5481
						#回傳結果:
5482
						#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
5482
						#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 5489... Line 5489...
5489
						#必填的參數:
5489
						#必填的參數:
5490
						$conf["stringProcess::delStrBeforeKeyWord"]["stringIn"]=$msg;#要處理的字串.
5490
						$conf["stringProcess::delStrBeforeKeyWord"]["stringIn"]=$msg;#要處理的字串.
5491
						$conf["stringProcess::delStrBeforeKeyWord"]["keyWord"]="password:";#特定字串.
5491
						$conf["stringProcess::delStrBeforeKeyWord"]["keyWord"]="password:";#特定字串.
5492
						$delStrBeforeKeyWord=stringProcess::delStrBeforeKeyWord($conf["stringProcess::delStrBeforeKeyWord"]);
5492
						$delStrBeforeKeyWord=stringProcess::delStrBeforeKeyWord($conf["stringProcess::delStrBeforeKeyWord"]);
5493
						unset($conf["stringProcess::delStrBeforeKeyWord"]);
5493
						unset($conf["stringProcess::delStrBeforeKeyWord"]);
5494
						
5494
 
5495
						#如果移除前置字元失敗
5495
						#如果移除前置字元失敗
5496
						if($delStrBeforeKeyWord["status"]==="false"){
5496
						if($delStrBeforeKeyWord["status"]==="false"){
5497
							
5497
 
5498
							#設置執行失敗
5498
							#設置執行失敗
5499
							$result["status"]="false";
5499
							$result["status"]="false";
5500
						
5500
 
5501
							#設置執行錯誤訊息
5501
							#設置執行錯誤訊息
5502
							$result["error"]=$delStrBeforeKeyWord;
5502
							$result["error"]=$delStrBeforeKeyWord;
5503
							
5503
 
5504
							#回傳結果
5504
							#回傳結果
5505
							return $result;
5505
							return $result;
5506
							
5506
 
5507
							}#if end
5507
							}#if end
5508
							
5508
 
5509
						#如果有符合條件的前置字元
5509
						#如果有符合條件的前置字元
5510
						if($delStrBeforeKeyWord["founded"]==="true"){
5510
						if($delStrBeforeKeyWord["founded"]==="true"){
5511
													
5511
 
5512
							#儲存密碼
5512
							#儲存密碼
5513
							$this->connInfo[$from->resourceId]["login"]["pw"]=$delStrBeforeKeyWord["content"];
5513
							$this->connInfo[$from->resourceId]["login"]["pw"]=$delStrBeforeKeyWord["content"];
5514
							
5514
 
5515
							}#if end
5515
							}#if end
5516
							
5516
 
5517
						#反之代表格式錯誤
5517
						#反之代表格式錯誤
5518
						else{
5518
						else{
5519
													
5519
 
5520
							#包裝訊息
5520
							#包裝訊息
5521
							#"type"為"login"	
5521
							#"type"為"login"
5522
							#"status"為"true"		
5522
							#"status"為"true"
5523
							#"data"為實際的訊息內容
5523
							#"data"為實際的訊息內容
5524
							$packedMsg=array("type"=>"login","status"=>"true","data"=>"Please input your password! ex:password:ws1");
5524
							$packedMsg=array("type"=>"login","status"=>"true","data"=>"Please input your password! ex:password:ws1");
5525
													
5525
 
5526
							#提示輸入密碼		
5526
							#提示輸入密碼
5527
							$from->send(json_encode($packedMsg));
5527
							$from->send(json_encode($packedMsg));
5528
							
5528
 
5529
							#結束程式
5529
							#結束程式
5530
							return true;
5530
							return true;
5531
												
5531
 
5532
							}#else end
5532
							}#else end
5533
						
5533
 
5534
						}#if end
5534
						}#if end
5535
						
5535
 
5536
					#反之代表格式錯誤
5536
					#反之代表格式錯誤
5537
					else{
5537
					else{
5538
						
5538
 
5539
						#包裝訊息
5539
						#包裝訊息
5540
						#"type"為"login"	
5540
						#"type"為"login"
5541
						#"status"為"true"		
5541
						#"status"為"true"
5542
						#"data"為實際的訊息內容
5542
						#"data"為實際的訊息內容
5543
						$packedMsg=array("type"=>"login","status"=>"true","data"=>"Please input your password! ex:password:ws1");
5543
						$packedMsg=array("type"=>"login","status"=>"true","data"=>"Please input your password! ex:password:ws1");
5544
												
5544
 
5545
						#提示輸入密碼		
5545
						#提示輸入密碼
5546
						$from->send(json_encode($packedMsg));
5546
						$from->send(json_encode($packedMsg));
5547
						
5547
 
5548
						#結束程式
5548
						#結束程式
5549
						return true;
5549
						return true;
5550
						
5550
 
5551
						}#else end
5551
						}#else end
5552
					
5552
 
5553
					}#if end
5553
					}#if end
5554
				
5554
 
5555
				}#if end
5555
				}#if end
5556
			
5556
 
5557
			#如果已經輸入帳號了
5557
			#如果已經輸入帳號了
5558
			if($this->connInfo[$from->resourceId]["login"]["ac"]!=""){
5558
			if($this->connInfo[$from->resourceId]["login"]["ac"]!=""){
5559
				
5559
 
5560
				#設置可以進行驗證
5560
				#設置可以進行驗證
5561
				$startAuth=true;
5561
				$startAuth=true;
5562
				
5562
 
5563
				#另存帳號
5563
				#另存帳號
5564
				$ac=$this->connInfo[$from->resourceId]["login"]["ac"];
5564
				$ac=$this->connInfo[$from->resourceId]["login"]["ac"];
5565
				
5565
 
5566
				#初始化密碼
5566
				#初始化密碼
5567
				$pw="";
5567
				$pw="";
5568
				
5568
 
5569
				#如果有設置密碼欄位
5569
				#如果有設置密碼欄位
5570
				if(self::$passwordCol!==""){
5570
				if(self::$passwordCol!==""){
5571
					
5571
 
5572
					#如果client已經輸入密碼了
5572
					#如果client已經輸入密碼了
5573
					if($this->connInfo[$from->resourceId]["login"]["pw"]!=""){
5573
					if($this->connInfo[$from->resourceId]["login"]["pw"]!=""){
5574
						
5574
 
5575
						#取得密碼
5575
						#取得密碼
5576
						$pw=$this->connInfo[$from->resourceId]["login"]["pw"];
5576
						$pw=$this->connInfo[$from->resourceId]["login"]["pw"];
5577
						
5577
 
5578
						}#if end
5578
						}#if end
5579
						
5579
 
5580
					#反之
5580
					#反之
5581
					else{
5581
					else{
5582
						
5582
 
5583
						#設置尚不能進行認證
5583
						#設置尚不能進行認證
5584
						$startAuth=false;
5584
						$startAuth=false;
5585
						
5585
 
5586
						}#else end
5586
						}#else end
5587
										
5587
 
5588
					}#if end
5588
					}#if end
5589
				
5589
 
5590
				#如果尚不能進行驗證
5590
				#如果尚不能進行驗證
5591
				if(!$startAuth){
5591
				if(!$startAuth){
5592
					
5592
 
5593
					#結束程式
5593
					#結束程式
5594
					return true;
5594
					return true;
5595
					
5595
 
5596
					}#if end
5596
					}#if end
5597
				
5597
 
5598
				#檢查有無符合的帳戶密碼
5598
				#檢查有無符合的帳戶密碼
5599
				#涵式說明:
5599
				#涵式說明:
5600
				#一次取得資料庫、表的資料
5600
				#一次取得資料庫、表的資料
5601
				#回傳的結果
5601
				#回傳的結果
5602
				#$result["status"],執行結果"true"為成功;"false"為執行失敗。
5602
				#$result["status"],執行結果"true"為成功;"false"為執行失敗。
Line 5618... Line 5618...
5618
				$conf["db::fastGetDbData"]["columnYouWant"]=array("id","password");#你想要的欄位!,若設為「array("*")」則代表全部欄位.
5618
				$conf["db::fastGetDbData"]["columnYouWant"]=array("id","password");#你想要的欄位!,若設為「array("*")」則代表全部欄位.
5619
				#可省略的參數:
5619
				#可省略的參數:
5620
				$conf["db::fastGetDbData"]["dbPassword"]=self::$dbPassword;#爲要存取dbServer的密碼
5620
				$conf["db::fastGetDbData"]["dbPassword"]=self::$dbPassword;#爲要存取dbServer的密碼
5621
				$conf["db::fastGetDbData"]["WhereColumnName"]=array(self::$accountCol);#用於判斷語句的欄位項目陣列。
5621
				$conf["db::fastGetDbData"]["WhereColumnName"]=array(self::$accountCol);#用於判斷語句的欄位項目陣列。
5622
				$conf["db::fastGetDbData"]["WhereColumnValue"]=array($ac);#用於判斷語句的欄位數值陣列,若與LIKE搭配,則可以在關鍵自字串的左右名加上「%」符號,這樣就可以搜尋具有該字串的內容。
5622
				$conf["db::fastGetDbData"]["WhereColumnValue"]=array($ac);#用於判斷語句的欄位數值陣列,若與LIKE搭配,則可以在關鍵自字串的左右名加上「%」符號,這樣就可以搜尋具有該字串的內容。
5623
				
5623
 
5624
				/*
5624
				/*
5625
				
5625
 
5626
				#如果有輸入密碼
5626
				#如果有輸入密碼
5627
				if($pw!==""){
5627
				if($pw!==""){
5628
					
5628
 
5629
					#新增要判斷 self::$password 欄位,是否有於 $pw
5629
					#新增要判斷 self::$password 欄位,是否有於 $pw
5630
					$conf["db::fastGetDbData"]["WhereColumnName"][]=self::$passwordCol;
5630
					$conf["db::fastGetDbData"]["WhereColumnName"][]=self::$passwordCol;
5631
					$conf["db::fastGetDbData"]["WhereColumnValue"][]=$pw;
5631
					$conf["db::fastGetDbData"]["WhereColumnValue"][]=$pw;
5632
					
5632
 
5633
					}#if end
5633
					}#if end
5634
				
5634
 
5635
				*/
5635
				*/
5636
				
5636
 
5637
				#$conf["WhereColumnCombine"]=array("");#用於判斷語句當中需要()起來的判斷式,須爲陣列值,"s"代表「(」,"e"代表「)」 ,若無則須設爲""。
5637
				#$conf["WhereColumnCombine"]=array("");#用於判斷語句當中需要()起來的判斷式,須爲陣列值,"s"代表「(」,"e"代表「)」 ,若無則須設爲""。
5638
				#$conf["WhereColumnOperator"]=array("");#用於判斷語句的比較符號陣列,可以用的符號有「"="、"!="、">"、"<"、"LIKE"、"NOT LIKE"」,預設都爲「=」。
5638
				#$conf["WhereColumnOperator"]=array("");#用於判斷語句的比較符號陣列,可以用的符號有「"="、"!="、">"、"<"、"LIKE"、"NOT LIKE"」,預設都爲「=」。
5639
				#$conf["WhereColumnAndOr"]=array("");#用於判斷語句條件之間成立的條件是AND還是OR,須爲陣列值。其數量應爲要判斷的欄位數量減一。
5639
				#$conf["WhereColumnAndOr"]=array("");#用於判斷語句條件之間成立的條件是AND還是OR,須爲陣列值。其數量應爲要判斷的欄位數量減一。
5640
				#$conf["orderItem"]="";#爲排序的項目依據,若要用隨機抽樣,可以用"rand()",可省略。
5640
				#$conf["orderItem"]="";#爲排序的項目依據,若要用隨機抽樣,可以用"rand()",可省略。
5641
				#$conf["ascORdesc"]="";#爲要低增還是遞減排序,asc爲遞增;desc爲遞減。
5641
				#$conf["ascORdesc"]="";#爲要低增還是遞減排序,asc爲遞增;desc爲遞減。
Line 5644... Line 5644...
5644
				#$conf["groupBy"]=array("");#爲要以哪幾個欄爲作爲分羣的依據(欄位相同的數值僅會取出一筆)。
5644
				#$conf["groupBy"]=array("");#爲要以哪幾個欄爲作爲分羣的依據(欄位相同的數值僅會取出一筆)。
5645
				#備註:
5645
				#備註:
5646
				#建議在查詢資料前,能夠檢查是否每個欄位都存在.
5646
				#建議在查詢資料前,能夠檢查是否每個欄位都存在.
5647
				$fastGetDbData=db::fastGetDbData($conf["db::fastGetDbData"]);
5647
				$fastGetDbData=db::fastGetDbData($conf["db::fastGetDbData"]);
5648
				unset($conf["db::fastGetDbData"]);
5648
				unset($conf["db::fastGetDbData"]);
5649
				
5649
 
5650
				#如果取得資料失敗
5650
				#如果取得資料失敗
5651
				if($fastGetDbData["status"]==="false"){
5651
				if($fastGetDbData["status"]==="false"){
5652
					
5652
 
5653
					#設置執行失敗
5653
					#設置執行失敗
5654
					$result["status"]="false";
5654
					$result["status"]="false";
5655
				
5655
 
5656
					#設置執行錯誤訊息
5656
					#設置執行錯誤訊息
5657
					$result["error"]=$fastGetDbData;
5657
					$result["error"]=$fastGetDbData;
5658
					
5658
 
5659
					#debug in server
5659
					#debug in server
5660
					var_dump($result);
5660
					var_dump($result);
5661
					
5661
 
5662
					#提示server有資料庫錯誤
5662
					#提示server有資料庫錯誤
5663
					echo "dbError!";
5663
					echo "dbError!";
5664
					
5664
 
5665
					#結束程式
5665
					#結束程式
5666
					exit;
5666
					exit;
5667
					
5667
 
5668
					}#if end
5668
					}#if end
5669
					
5669
 
5670
				#如果沒有一筆資料
5670
				#如果沒有一筆資料
5671
				if($fastGetDbData["dataCount"]!==1){
5671
				if($fastGetDbData["dataCount"]!==1){
5672
					
5672
 
5673
					#代表登入失敗
5673
					#代表登入失敗
5674
					
5674
 
5675
					#清空登入用的帳戶
5675
					#清空登入用的帳戶
5676
					$this->connInfo[$from->resourceId]["login"]["ac"]="";
5676
					$this->connInfo[$from->resourceId]["login"]["ac"]="";
5677
				
5677
 
5678
					#清空登入用的密碼
5678
					#清空登入用的密碼
5679
					$this->connInfo[$from->resourceId]["login"]["pw"]="";
5679
					$this->connInfo[$from->resourceId]["login"]["pw"]="";
5680
					
5680
 
5681
					#包裝訊息
5681
					#包裝訊息
5682
					#"type"為"login"	
5682
					#"type"為"login"
5683
					#"status"為"false"		
5683
					#"status"為"false"
5684
					#"data"為實際的訊息內容
5684
					#"data"為實際的訊息內容
5685
					$packedMsg=array("type"=>"login","status"=>"false","data"=>"Login failed, please input account. ex:account:ws1");
5685
					$packedMsg=array("type"=>"login","status"=>"false","data"=>"Login failed, please input account. ex:account:ws1");
5686
					
5686
 
5687
					#提示登入失敗					
5687
					#提示登入失敗
5688
					$from->send(json_encode($packedMsg));
5688
					$from->send(json_encode($packedMsg));
5689
					
5689
 
5690
					#執行到這即可
5690
					#執行到這即可
5691
					return true;
5691
					return true;
5692
					
5692
 
5693
					}#if end
5693
					}#if end
5694
					
5694
 
5695
				#反之代表帳戶資訊正確
5695
				#反之代表帳戶資訊正確
5696
				else{
5696
				else{
5697
					#驗證密碼是否正確.
5697
					#驗證密碼是否正確.
5698
					#函式說明:
5698
					#函式說明:
5699
					#加密字串,可以用的方法有sha1,md5,password_sha,sha1可以回傳20或40的數值字串,md5可以回傳32個數值字串,password_hash可以回傳60~255個字元.
5699
					#加密字串,可以用的方法有sha1,md5,password_sha,sha1可以回傳20或40的數值字串,md5可以回傳32個數值字串,password_hash可以回傳60~255個字元.
5700
					#回傳結果:
5700
					#回傳結果:
5701
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常. 
5701
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
5702
					#$result["function"],當前執行的函數名稱.
5702
					#$result["function"],當前執行的函數名稱.
5703
					#$result["content"],加密後的結果.
5703
					#$result["content"],加密後的結果.
5704
					#$result["error"],錯誤訊息陣列.
5704
					#$result["error"],錯誤訊息陣列.
5705
					#$result["argu"],使用的參數.
5705
					#$result["argu"],使用的參數.
5706
					#必填參數:
5706
					#必填參數:
Line 5718... Line 5718...
5718
					#md5=>http://php.net/manual/en/function.md5.php
5718
					#md5=>http://php.net/manual/en/function.md5.php
5719
					#password_hash=>http://php.net/manual/en/function.password-hash.php
5719
					#password_hash=>http://php.net/manual/en/function.password-hash.php
5720
					#password_verify=>http://php.net/manual/en/function.password-verify.php
5720
					#password_verify=>http://php.net/manual/en/function.password-verify.php
5721
					$enCodeStr=authenticate::enCodeStr($conf["authenticate::enCodeStr"]);
5721
					$enCodeStr=authenticate::enCodeStr($conf["authenticate::enCodeStr"]);
5722
					unset($conf["authenticate::enCodeStr"]);
5722
					unset($conf["authenticate::enCodeStr"]);
5723
					
5723
 
5724
					#如果加密密碼失敗
5724
					#如果加密密碼失敗
5725
					if($enCodeStr["status"]==="false"){
5725
					if($enCodeStr["status"]==="false"){
5726
					
5726
 
5727
						#debug from server
5727
						#debug from server
5728
						var_dump($enCodeStr);
5728
						var_dump($enCodeStr);
5729
					
5729
 
5730
						#提示server有資料庫錯誤
5730
						#提示server有資料庫錯誤
5731
						echo "password verify error!";
5731
						echo "password verify error!";
5732
						
5732
 
5733
						#代表登入失敗
5733
						#代表登入失敗
5734
					
5734
 
5735
						#清空登入用的帳戶
5735
						#清空登入用的帳戶
5736
						$this->connInfo[$from->resourceId]["login"]["ac"]="";
5736
						$this->connInfo[$from->resourceId]["login"]["ac"]="";
5737
					
5737
 
5738
						#清空登入用的密碼
5738
						#清空登入用的密碼
5739
						$this->connInfo[$from->resourceId]["login"]["pw"]="";
5739
						$this->connInfo[$from->resourceId]["login"]["pw"]="";
5740
						
5740
 
5741
						#包裝訊息
5741
						#包裝訊息
5742
						#"type"為"login"	
5742
						#"type"為"login"
5743
						#"status"為"false"		
5743
						#"status"為"false"
5744
						#"data"為實際的訊息內容
5744
						#"data"為實際的訊息內容
5745
						$packedMsg=array("type"=>"login","status"=>"false","data"=>"Login failed, please input account. ex:account:ws1");
5745
						$packedMsg=array("type"=>"login","status"=>"false","data"=>"Login failed, please input account. ex:account:ws1");
5746
						
5746
 
5747
						#提示登入失敗					
5747
						#提示登入失敗
5748
						$from->send(json_encode($packedMsg));
5748
						$from->send(json_encode($packedMsg));
5749
						
5749
 
5750
						#執行到這即可
5750
						#執行到這即可
5751
						return true;
5751
						return true;
5752
					
5752
 
5753
						}#if end
5753
						}#if end
5754
					
5754
 
5755
					#反之認證成功
5755
					#反之認證成功
5756
					else{
5756
					else{
5757
						
5757
 
5758
						#debug for server
5758
						#debug for server
5759
						echo "new login:".PHP_EOL;
5759
						echo "new login:".PHP_EOL;
5760
						var_dump($enCodeStr);
5760
						var_dump($enCodeStr);
5761
					
5761
 
5762
						}#else end
5762
						}#else end
5763
					
5763
 
5764
					#設置 $myUserId
5764
					#設置 $myUserId
5765
					$myUserId=$this->connInfo[$from->resourceId]["userId"]=$fastGetDbData["dataContent"]["id"][0];
5765
					$myUserId=$this->connInfo[$from->resourceId]["userId"]=$fastGetDbData["dataContent"]["id"][0];
5766
					
5766
 
5767
					#設置 $myId
5767
					#設置 $myId
5768
					$myId=$from->resourceId;
5768
					$myId=$from->resourceId;
5769
					
5769
 
5770
					#包裝訊息
5770
					#包裝訊息
5771
					#"type"為"login"	
5771
					#"type"為"login"
5772
					#"status"為"true"		
5772
					#"status"為"true"
5773
					#"data"為實際的訊息內容
5773
					#"data"為實際的訊息內容
5774
					$packedMsg=array("type"=>"login","status"=>"true","data"=>"Login successfully!");
5774
					$packedMsg=array("type"=>"login","status"=>"true","data"=>"Login successfully!");
5775
					
5775
 
5776
					#提示登入成功
5776
					#提示登入成功
5777
					$from->send(json_encode($packedMsg));
5777
					$from->send(json_encode($packedMsg));
5778
					
5778
 
5779
					#結束認證成功後的流程
5779
					#結束認證成功後的流程
5780
					return true;
5780
					return true;
5781
					
5781
 
5782
					}#else end
5782
					}#else end
5783
					
5783
 
5784
				}#else end
5784
				}#else end
5785
				
5785
 
5786
			#結束認證流程
5786
			#結束認證流程
5787
			return true;	
5787
			return true;
5788
			
5788
 
5789
			}#if end
5789
			}#if end
5790
			
5790
 
5791
		#如果server收到訊息長度大於 "cmd:"
5791
		#如果server收到訊息長度大於 "cmd:"
5792
		if(strlen($msg)>strlen("cmd:")){
5792
		if(strlen($msg)>strlen("cmd:")){
5793
			
5793
 
5794
			#如果$msg開頭為"cmd:"  
5794
			#如果$msg開頭為"cmd:"
5795
			#涵式說明:
5795
			#涵式說明:
5796
			#取得符合特定字首與字尾的字串
5796
			#取得符合特定字首與字尾的字串
5797
			#回傳的結果:
5797
			#回傳的結果:
5798
			#$result["status"],若爲"true"則代表執行正常;若爲"false"則代表執行失敗。
5798
			#$result["status"],若爲"true"則代表執行正常;若爲"false"則代表執行失敗。
5799
			#$result["function"],當前執行的函數名稱.
5799
			#$result["function"],當前執行的函數名稱.
Line 5810... Line 5810...
5810
			#$conf["tailWord"]="";
5810
			#$conf["tailWord"]="";
5811
			#參考資料:
5811
			#參考資料:
5812
			#str_spilt(),可以將字串依照字母分割成一個個陣列字串。
5812
			#str_spilt(),可以將字串依照字母分割成一個個陣列字串。
5813
			$getMeetConditionsString=search::getMeetConditionsString($conf["search::getMeetConditionsString"]);
5813
			$getMeetConditionsString=search::getMeetConditionsString($conf["search::getMeetConditionsString"]);
5814
			unset($conf["search::getMeetConditionsString"]);
5814
			unset($conf["search::getMeetConditionsString"]);
5815
				
5815
 
5816
			#如果選找前置字串 "cmd:" 失敗
5816
			#如果選找前置字串 "cmd:" 失敗
5817
			if($getMeetConditionsString["status"]==="false"){
5817
			if($getMeetConditionsString["status"]==="false"){
5818
				
5818
 
5819
				#設置執行失敗
5819
				#設置執行失敗
5820
				$result["status"]="false";
5820
				$result["status"]="false";
5821
			
5821
 
5822
				#設置執行錯誤訊息
5822
				#設置執行錯誤訊息
5823
				$result["error"]=$getMeetConditionsString;
5823
				$result["error"]=$getMeetConditionsString;
5824
				
5824
 
5825
				#回傳結果
5825
				#回傳結果
5826
				return $result;
5826
				return $result;
5827
				
5827
 
5828
				}#if end
5828
				}#if end
5829
				
5829
 
5830
			#如果存在 "cmd:" 前置字串
5830
			#如果存在 "cmd:" 前置字串
5831
			if($getMeetConditionsString["founded"]==="true"){
5831
			if($getMeetConditionsString["founded"]==="true"){
5832
				
5832
 
5833
				#用 "talkTo:" 分割 $buf
5833
				#用 "talkTo:" 分割 $buf
5834
				#涵式說明:
5834
				#涵式說明:
5835
				#將固定格式的字串分開,並回傳分開的結果。
5835
				#將固定格式的字串分開,並回傳分開的結果。
5836
				#回傳結果:
5836
				#回傳結果:
5837
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
5837
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 5847... Line 5847...
5847
				#可省略參數:
5847
				#可省略參數:
5848
				#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
5848
				#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
5849
				$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
5849
				$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
5850
				$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
5850
				$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
5851
				unset($conf["stringProcess::spiltString"]);
5851
				unset($conf["stringProcess::spiltString"]);
5852
				
5852
 
5853
				#如果分割字串失敗
5853
				#如果分割字串失敗
5854
				if($spiltString["status"]==="false"){
5854
				if($spiltString["status"]==="false"){
5855
					
5855
 
5856
					#設置執行失敗
5856
					#設置執行失敗
5857
					$result["status"]="false";
5857
					$result["status"]="false";
5858
				
5858
 
5859
					#設置執行錯誤訊息
5859
					#設置執行錯誤訊息
5860
					$result["error"]=$spiltString;
5860
					$result["error"]=$spiltString;
5861
					
5861
 
5862
					#回傳結果
5862
					#回傳結果
5863
					return $result;
5863
					return $result;
5864
					
5864
 
5865
					}#if end
5865
					}#if end
5866
				
5866
 
5867
				#如果剛好分割出一筆資料
5867
				#如果剛好分割出一筆資料
5868
				if($spiltString["dataCounts"]===1){
5868
				if($spiltString["dataCounts"]===1){
5869
					
5869
 
5870
					#取的要執行的指令
5870
					#取的要執行的指令
5871
					$cmd=$spiltString["dataArray"][0];
5871
					$cmd=$spiltString["dataArray"][0];
5872
					
5872
 
5873
					#函式說明:
5873
					#函式說明:
5874
					#呼叫shell執行系統命令,並取得回傳的內容.
5874
					#呼叫shell執行系統命令,並取得回傳的內容.
5875
					#回傳的結果:
5875
					#回傳的結果:
5876
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
5876
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
5877
					#$result["error"],錯誤訊息陣列.
5877
					#$result["error"],錯誤訊息陣列.
Line 5880... Line 5880...
5880
					#$result["output"],爲執行完二元碼後的輸出陣列.
5880
					#$result["output"],爲執行完二元碼後的輸出陣列.
5881
					#必填的參數
5881
					#必填的參數
5882
					#$conf["command"],字串,要執行的指令與.
5882
					#$conf["command"],字串,要執行的指令與.
5883
					$conf["external::callShell"]["command"]=$cmd;
5883
					$conf["external::callShell"]["command"]=$cmd;
5884
					#$conf["fileArgu"],字串,變數__FILE__的內容.
5884
					#$conf["fileArgu"],字串,變數__FILE__的內容.
5885
					$conf["external::callShell"]["fileArgu"]=self::$fileArgu;		
5885
					$conf["external::callShell"]["fileArgu"]=self::$fileArgu;
5886
					#可省略參數:
5886
					#可省略參數:
5887
					#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
5887
					#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
5888
					#$conf["argu"]=array("");
5888
					#$conf["argu"]=array("");
5889
					#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
5889
					#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
5890
					#$conf["arguIsAddr"]=array();
5890
					#$conf["arguIsAddr"]=array();
Line 5910... Line 5910...
5910
					#exec=>http://php.net/manual/en/function.exec.php
5910
					#exec=>http://php.net/manual/en/function.exec.php
5911
					#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
5911
					#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
5912
					#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
5912
					#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
5913
					$callShell=external::callShell($conf["external::callShell"]);
5913
					$callShell=external::callShell($conf["external::callShell"]);
5914
					unset($conf["external::callShell"]);
5914
					unset($conf["external::callShell"]);
5915
					
5915
 
5916
					#如果執行外部指令失敗
5916
					#如果執行外部指令失敗
5917
					if($callShell["status"]==="false"){
5917
					if($callShell["status"]==="false"){
5918
						
5918
 
5919
						#設置執行失敗
5919
						#設置執行失敗
5920
						$result["status"]="false";
5920
						$result["status"]="false";
5921
					
5921
 
5922
						#設置執行錯誤訊息
5922
						#設置執行錯誤訊息
5923
						$result["error"]=$callShell;
5923
						$result["error"]=$callShell;
5924
						
5924
 
5925
						#回傳結果
5925
						#回傳結果
5926
						return $result;
5926
						return $result;
5927
						
5927
 
5928
						}#if end
5928
						}#if end
5929
					
5929
 
5930
					#設置要傳給client的訊息
5930
					#設置要傳給client的訊息
5931
					$result=array("data"=>$callShell["output"],"type"=>"cmd");
5931
					$result=array("data"=>$callShell["output"],"type"=>"cmd");
5932
					
5932
 
5933
					#將程式執行後的輸出傳給client					
5933
					#將程式執行後的輸出傳給client
5934
					$from->send(json_encode($result));
5934
					$from->send(json_encode($result));
5935
					
5935
 
5936
					}#if end
5936
					}#if end
5937
					
5937
 
5938
				}#if end
5938
				}#if end
5939
			
5939
 
5940
			}#if end
5940
			}#if end
5941
			
5941
 
5942
		#如果server收到訊息長度大於 "sql:"
5942
		#如果server收到訊息長度大於 "sql:"
5943
		if(strlen($msg)>strlen("sql:")){
5943
		if(strlen($msg)>strlen("sql:")){
5944
		
5944
 
5945
			#如果$msg開頭為"sql:"  
5945
			#如果$msg開頭為"sql:"
5946
			#涵式說明:
5946
			#涵式說明:
5947
			#取得符合特定字首與字尾的字串
5947
			#取得符合特定字首與字尾的字串
5948
			#回傳的結果:
5948
			#回傳的結果:
5949
			#$result["status"],若爲"true"則代表執行正常;若爲"false"則代表執行失敗。
5949
			#$result["status"],若爲"true"則代表執行正常;若爲"false"則代表執行失敗。
5950
			#$result["function"],當前執行的函數名稱.
5950
			#$result["function"],當前執行的函數名稱.
Line 5961... Line 5961...
5961
			#$conf["tailWord"]="";
5961
			#$conf["tailWord"]="";
5962
			#參考資料:
5962
			#參考資料:
5963
			#str_spilt(),可以將字串依照字母分割成一個個陣列字串。
5963
			#str_spilt(),可以將字串依照字母分割成一個個陣列字串。
5964
			$getMeetConditionsString=search::getMeetConditionsString($conf["search::getMeetConditionsString"]);
5964
			$getMeetConditionsString=search::getMeetConditionsString($conf["search::getMeetConditionsString"]);
5965
			unset($conf["search::getMeetConditionsString"]);
5965
			unset($conf["search::getMeetConditionsString"]);
5966
				
5966
 
5967
			#如果選找前置字串 "sql:" 失敗
5967
			#如果選找前置字串 "sql:" 失敗
5968
			if($getMeetConditionsString["status"]==="false"){
5968
			if($getMeetConditionsString["status"]==="false"){
5969
				
5969
 
5970
				#設置執行失敗
5970
				#設置執行失敗
5971
				$result["status"]="false";
5971
				$result["status"]="false";
5972
			
5972
 
5973
				#設置執行錯誤訊息
5973
				#設置執行錯誤訊息
5974
				$result["error"]=$getMeetConditionsString;
5974
				$result["error"]=$getMeetConditionsString;
5975
				
5975
 
5976
				#回傳結果
5976
				#回傳結果
5977
				return $result;
5977
				return $result;
5978
				
5978
 
5979
				}#if end
5979
				}#if end
5980
				
5980
 
5981
			#如果存在 "sql:" 前置字串
5981
			#如果存在 "sql:" 前置字串
5982
			if($getMeetConditionsString["founded"]==="true"){
5982
			if($getMeetConditionsString["founded"]==="true"){
5983
				
5983
 
5984
				#用 "talkTo:" 分割 $buf
5984
				#用 "talkTo:" 分割 $buf
5985
				#涵式說明:
5985
				#涵式說明:
5986
				#將固定格式的字串分開,並回傳分開的結果。
5986
				#將固定格式的字串分開,並回傳分開的結果。
5987
				#回傳結果:
5987
				#回傳結果:
5988
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
5988
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 5998... Line 5998...
5998
				#可省略參數:
5998
				#可省略參數:
5999
				#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
5999
				#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
6000
				$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
6000
				$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
6001
				$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
6001
				$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
6002
				unset($conf["stringProcess::spiltString"]);
6002
				unset($conf["stringProcess::spiltString"]);
6003
				
6003
 
6004
				#如果分割字串失敗
6004
				#如果分割字串失敗
6005
				if($spiltString["status"]==="false"){
6005
				if($spiltString["status"]==="false"){
6006
					
6006
 
6007
					#設置執行失敗
6007
					#設置執行失敗
6008
					$result["status"]="false";
6008
					$result["status"]="false";
6009
				
6009
 
6010
					#設置執行錯誤訊息
6010
					#設置執行錯誤訊息
6011
					$result["error"]=$spiltString;
6011
					$result["error"]=$spiltString;
6012
					
6012
 
6013
					#回傳結果
6013
					#回傳結果
6014
					return $result;
6014
					return $result;
6015
					
6015
 
6016
					}#if end
6016
					}#if end
6017
				
6017
 
6018
				#如果剛好分割出一筆資料
6018
				#如果剛好分割出一筆資料
6019
				if($spiltString["dataCounts"]===1){
6019
				if($spiltString["dataCounts"]===1){
6020
					
6020
 
6021
					#取的要執行的sql指令
6021
					#取的要執行的sql指令
6022
					$sql=$spiltString["dataArray"][0];
6022
					$sql=$spiltString["dataArray"][0];
6023
					
6023
 
6024
					#建立要執行的指令
6024
					#建立要執行的指令
6025
					#$cmd="echo \"".$sql."\" | mysql -u".self::$dbAccount." -p".self::$dbPassword;
6025
					#$cmd="echo \"".$sql."\" | mysql -u".self::$dbAccount." -p".self::$dbPassword;
6026
					
6026
 
6027
					#函式說明:
6027
					#函式說明:
6028
					#呼叫shell執行系統命令,並取得回傳的內容.
6028
					#呼叫shell執行系統命令,並取得回傳的內容.
6029
					#回傳的結果:
6029
					#回傳的結果:
6030
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
6030
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
6031
					#$result["error"],錯誤訊息陣列.
6031
					#$result["error"],錯誤訊息陣列.
Line 6034... Line 6034...
6034
					#$result["output"],爲執行完二元碼後的輸出陣列.
6034
					#$result["output"],爲執行完二元碼後的輸出陣列.
6035
					#必填的參數
6035
					#必填的參數
6036
					#$conf["command"],字串,要執行的指令與.
6036
					#$conf["command"],字串,要執行的指令與.
6037
					$conf["external::callShell"]["command"]="echo";
6037
					$conf["external::callShell"]["command"]="echo";
6038
					#$conf["fileArgu"],字串,變數__FILE__的內容.
6038
					#$conf["fileArgu"],字串,變數__FILE__的內容.
6039
					$conf["external::callShell"]["fileArgu"]=self::$fileArgu;		
6039
					$conf["external::callShell"]["fileArgu"]=self::$fileArgu;
6040
					#可省略參數:
6040
					#可省略參數:
6041
					#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
6041
					#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
6042
					$conf["external::callShell"]["argu"]=array($sql,"|","mysql","-u".self::$dbAccount,"-p".self::$dbPassword);
6042
					$conf["external::callShell"]["argu"]=array($sql,"|","mysql","-u".self::$dbAccount,"-p".self::$dbPassword);
6043
					#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
6043
					#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
6044
					#$conf["arguIsAddr"]=array();
6044
					#$conf["arguIsAddr"]=array();
Line 6064... Line 6064...
6064
					#exec=>http://php.net/manual/en/function.exec.php
6064
					#exec=>http://php.net/manual/en/function.exec.php
6065
					#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
6065
					#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
6066
					#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
6066
					#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
6067
					$callShell=external::callShell($conf["external::callShell"]);
6067
					$callShell=external::callShell($conf["external::callShell"]);
6068
					unset($conf["external::callShell"]);
6068
					unset($conf["external::callShell"]);
6069
					
6069
 
6070
					#如果執行外部指令失敗
6070
					#如果執行外部指令失敗
6071
					if($callShell["status"]==="false"){
6071
					if($callShell["status"]==="false"){
6072
						
6072
 
6073
						#設置執行失敗
6073
						#設置執行失敗
6074
						$result["status"]="false";
6074
						$result["status"]="false";
6075
					
6075
 
6076
						#設置執行錯誤訊息
6076
						#設置執行錯誤訊息
6077
						$result["error"]=$callShell;
6077
						$result["error"]=$callShell;
6078
						
6078
 
6079
						#回傳結果
6079
						#回傳結果
6080
						return $result;
6080
						return $result;
6081
						
6081
 
6082
						}#if end
6082
						}#if end
6083
					
6083
 
6084
					#設置要傳給client的訊息
6084
					#設置要傳給client的訊息
6085
					$result=array("data"=>$callShell["output"],"type"=>"sql");
6085
					$result=array("data"=>$callShell["output"],"type"=>"sql");
6086
					
6086
 
6087
					#將程式執行後的輸出傳給client					
6087
					#將程式執行後的輸出傳給client
6088
					$from->send(json_encode($result));
6088
					$from->send(json_encode($result));
6089
										
6089
 
6090
					}#if end
6090
					}#if end
6091
					
6091
 
6092
				}#if end
6092
				}#if end
6093
					
6093
 
6094
			}#if end	
6094
			}#if end
6095
				
6095
 
6096
		}#function onMessage end
6096
		}#function onMessage end
6097
	
6097
 
6098
	#建構子
6098
	#建構子
6099
	function __construct(){
6099
	function __construct(){
6100
		
6100
 
6101
		#實做 SplObjectStorage
6101
		#實做 SplObjectStorage
6102
		$this->clients = new \SplObjectStorage;
6102
		$this->clients = new \SplObjectStorage;
6103
		
6103
 
6104
		}#function __construct end
6104
		}#function __construct end
6105
		
6105
 
6106
	#在clinet的連線斷掉前
6106
	#在clinet的連線斷掉前
6107
    	public function onClose(ConnectionInterface $conn){
6107
    	public function onClose(ConnectionInterface $conn){
6108
		
6108
 
6109
		// The connection is closed, remove it, as we can no longer send it messages
6109
		// The connection is closed, remove it, as we can no longer send it messages
6110
        $this->clients->detach($conn);
6110
        $this->clients->detach($conn);
6111
 
6111
 
6112
        echo "Connection {$conn->resourceId} has disconnected\n";
6112
        echo "Connection {$conn->resourceId} has disconnected\n";
6113
		
6113
 
6114
		#移除連線的資訊
6114
		#移除連線的資訊
6115
		unset($this->connInfo[$conn->resourceId]);
6115
		unset($this->connInfo[$conn->resourceId]);
6116
		
6116
 
6117
		}#function onClose end
6117
		}#function onClose end
6118
	
6118
 
6119
	#當出現錯誤時
6119
	#當出現錯誤時
6120
    	public function onError(ConnectionInterface $conn, \Exception $e){
6120
    	public function onError(ConnectionInterface $conn, \Exception $e){
6121
		
6121
 
6122
		#印出錯誤訊息
6122
		#印出錯誤訊息
6123
		echo "An error has occurred: {$e->getMessage()}\n";
6123
		echo "An error has occurred: {$e->getMessage()}\n";
6124
 
6124
 
6125
		#關閉與client的socket
6125
		#關閉與client的socket
6126
        	$conn->close();
6126
        	$conn->close();
6127
        
6127
 
6128
		}#fucntion onError end
6128
		}#fucntion onError end
6129
	
6129
 
6130
	}#class soldier end 
6130
	}#class soldier end
6131
    
6131
 
6132
#使用 Ratchet\Server\IoServer 類別
6132
#使用 Ratchet\Server\IoServer 類別
6133
use Ratchet\Server\IoServer;
6133
use Ratchet\Server\IoServer;
6134
 
6134
 
6135
#使用 Ratchet\WebSocket\WsServer 類別
6135
#使用 Ratchet\WebSocket\WsServer 類別
6136
use Ratchet\WebSocket\WsServer;
6136
use Ratchet\WebSocket\WsServer;
Line 6142... Line 6142...
6142
類別說明:
6142
類別說明:
6143
跟webSocket應用相關的類別.
6143
跟webSocket應用相關的類別.
6144
備註:
6144
備註:
6145
無.
6145
無.
6146
*/
6146
*/
6147
class webSock{ 
6147
class webSock{
6148
 
6148
 
6149
	/*
6149
	/*
6150
	#函式說明:
6150
	#函式說明:
6151
	#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
6151
	#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
6152
	#回傳結果:
6152
	#回傳結果:
Line 6158... Line 6158...
6158
	#$arguments,陣列,為呼叫方法時所用的參數.
6158
	#$arguments,陣列,為呼叫方法時所用的參數.
6159
	#參考資料:
6159
	#參考資料:
6160
	#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
6160
	#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
6161
	*/
6161
	*/
6162
	public function __call($method,$arguments){
6162
	public function __call($method,$arguments){
6163
		
6163
 
6164
		#取得當前執行的函式
6164
		#取得當前執行的函式
6165
		$result["function"]=__FUNCTION__;
6165
		$result["function"]=__FUNCTION__;
6166
		
6166
 
6167
		#設置執行不正常
6167
		#設置執行不正常
6168
		$result["status"]="false";
6168
		$result["status"]="false";
6169
		
6169
 
6170
		#設置執行錯誤
6170
		#設置執行錯誤
6171
		$result["error"][]=__NAMESPACE__ ."/".$method."() 不存在!";
6171
		$result["error"][]=__NAMESPACE__ ."/".$method."() 不存在!";
6172
		
6172
 
6173
		#設置所丟入的參數
6173
		#設置所丟入的參數
6174
		$result["error"][]=$arguments;
6174
		$result["error"][]=$arguments;
6175
		
6175
 
6176
		#回傳結果
6176
		#回傳結果
6177
		return $result;
6177
		return $result;
6178
		
6178
 
6179
		}#function __call end
6179
		}#function __call end
6180
		
6180
 
6181
	/*
6181
	/*
6182
	#函式說明:
6182
	#函式說明:
6183
	#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
6183
	#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
6184
	#回傳結果:
6184
	#回傳結果:
6185
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
6185
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 6190... Line 6190...
6190
	#$arguments,陣列,為呼叫方法時所用的參數.
6190
	#$arguments,陣列,為呼叫方法時所用的參數.
6191
	#參考資料:
6191
	#參考資料:
6192
	#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
6192
	#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
6193
	*/
6193
	*/
6194
	public static function __callStatic($method,$arguments){
6194
	public static function __callStatic($method,$arguments){
6195
		
6195
 
6196
		#取得當前執行的函式
6196
		#取得當前執行的函式
6197
		$result["function"]=__FUNCTION__;
6197
		$result["function"]=__FUNCTION__;
6198
		
6198
 
6199
		#設置執行不正常
6199
		#設置執行不正常
6200
		$result["status"]="false";
6200
		$result["status"]="false";
6201
		
6201
 
6202
		#設置執行錯誤
6202
		#設置執行錯誤
6203
		$result["error"][]="欲呼叫的". __NAMESPACE__ ."/".$method."() 不存在!";
6203
		$result["error"][]="欲呼叫的". __NAMESPACE__ ."/".$method."() 不存在!";
6204
		
6204
 
6205
		#設置所丟入的參數
6205
		#設置所丟入的參數
6206
		$result["error"][]=$arguments;
6206
		$result["error"][]=$arguments;
6207
		
6207
 
6208
		#回傳結果
6208
		#回傳結果
6209
		return $result;
6209
		return $result;
6210
		
6210
 
6211
		}#function __callStatic end
6211
		}#function __callStatic end
6212
	
6212
 
6213
	/*
6213
	/*
6214
	#函式說明:
6214
	#函式說明:
6215
	#擴充過的Ratchet的聊天室用戶端與伺服端範例,port為8080.
6215
	#擴充過的Ratchet的聊天室用戶端與伺服端範例,port為8080.
6216
	#回傳結果:
6216
	#回傳結果:
6217
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
6217
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
6218
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
6218
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
6219
	#$result["function"],當前執行的函式名稱.		
6219
	#$result["function"],當前執行的函式名稱.
6220
	#必填參數:
6220
	#必填參數:
6221
	#$conf["fileArgu"],字串,__FILE__的內容.
6221
	#$conf["fileArgu"],字串,__FILE__的內容.
6222
	$conf["fileArgu"]=__FILE__;
6222
	$conf["fileArgu"]=__FILE__;
6223
	#可省略參數:
6223
	#可省略參數:
6224
	#$conf["disReg"],字串,是否要取消apache的passProxy檢查與設定,預設為"false"不取消,"true"為取消.
6224
	#$conf["disReg"],字串,是否要取消apache的passProxy檢查與設定,預設為"false"不取消,"true"為取消.
Line 6241... Line 6241...
6241
		#涵式說明:
6241
		#涵式說明:
6242
		#判斷當前環境為web還是cmd
6242
		#判斷當前環境為web還是cmd
6243
		#回傳結果:
6243
		#回傳結果:
6244
		#$result,"web"或"cmd"
6244
		#$result,"web"或"cmd"
6245
		if(csInformation::getEnv()==="web"){
6245
		if(csInformation::getEnv()==="web"){
6246
			
6246
 
6247
			#建立 web socket client 的 js 語法
6247
			#建立 web socket client 的 js 語法
6248
			#函式說明:
6248
			#函式說明:
6249
			#聊天室用戶端js範例
6249
			#聊天室用戶端js範例
6250
			#回傳結果:
6250
			#回傳結果:
6251
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
6251
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
6252
			#$reuslt["error"],執行不正常結束的錯訊息陣列.
6252
			#$reuslt["error"],執行不正常結束的錯訊息陣列.
6253
			#$result["function"],當前執行的函式名稱.		
6253
			#$result["function"],當前執行的函式名稱.
6254
			#$result["content"],js語法.
6254
			#$result["content"],js語法.
6255
			#參考資料:
6255
			#參考資料:
6256
			#Ratchet官網=>http://socketo.me/
6256
			#Ratchet官網=>http://socketo.me/
6257
			#聊天室範例=>http://socketo.me/docs/hello-world
6257
			#聊天室範例=>http://socketo.me/docs/hello-world
6258
			$chatRoomClientDemo=webSock::chatRoomClientDemo();
6258
			$chatRoomClientDemo=webSock::chatRoomClientDemo();
6259
			
6259
 
6260
			#如果建立聊天室用戶端失敗
6260
			#如果建立聊天室用戶端失敗
6261
			if($chatRoomClientDemo["status"]==="false"){
6261
			if($chatRoomClientDemo["status"]==="false"){
6262
				
6262
 
6263
				#設置執行失敗
6263
				#設置執行失敗
6264
				$result["status"]="false";
6264
				$result["status"]="false";
6265
				
6265
 
6266
				#設置執行錯誤訊息
6266
				#設置執行錯誤訊息
6267
				$result["error"]=$chatRoomClientDemo;
6267
				$result["error"]=$chatRoomClientDemo;
6268
				
6268
 
6269
				#回傳結果
6269
				#回傳結果
6270
				return $result;
6270
				return $result;
6271
				
6271
 
6272
				}#if end
6272
				}#if end
6273
				
6273
 
6274
			#取得聊天室的語法
6274
			#取得聊天室的語法
6275
			$result["content"]=$chatRoomClientDemo["content"];
6275
			$result["content"]=$chatRoomClientDemo["content"];
6276
			
6276
 
6277
			#設置執行正常
6277
			#設置執行正常
6278
			$result["status"]="true";
6278
			$result["status"]="true";
6279
			
6279
 
6280
			#回傳結果
6280
			#回傳結果
6281
			return $result;
6281
			return $result;
6282
			
6282
 
6283
			}#if end
6283
			}#if end
6284
 
6284
 
6285
		#如果沒有參數
6285
		#如果沒有參數
6286
		if(func_num_args()==0){
6286
		if(func_num_args()==0){
6287
			
6287
 
6288
			#設置執行失敗
6288
			#設置執行失敗
6289
			$result["status"]="false";
6289
			$result["status"]="false";
6290
			
6290
 
6291
			#設置執行錯誤訊息
6291
			#設置執行錯誤訊息
6292
			$result["error"]="函數".$result["function"]."需要參數";
6292
			$result["error"]="函數".$result["function"]."需要參數";
6293
			
6293
 
6294
			#回傳結果
6294
			#回傳結果
6295
			return $result;
6295
			return $result;
6296
			
6296
 
6297
			}#if end
6297
			}#if end
6298
			
6298
 
6299
		#如果沒有參數
6299
		#如果沒有參數
6300
		if(func_num_args()==0){
6300
		if(func_num_args()==0){
6301
			
6301
 
6302
			#設置執行失敗
6302
			#設置執行失敗
6303
			$result["status"]="false";
6303
			$result["status"]="false";
6304
			
6304
 
6305
			#設置執行錯誤訊息
6305
			#設置執行錯誤訊息
6306
			$result["error"]="函數".$result["function"]."需要參數";
6306
			$result["error"]="函數".$result["function"]."需要參數";
6307
			
6307
 
6308
			#回傳結果
6308
			#回傳結果
6309
			return $result;
6309
			return $result;
6310
			
6310
 
6311
			}#if end
6311
			}#if end
6312
			
6312
 
6313
		#取得參數
6313
		#取得參數
6314
		$result["argu"]=$conf;
6314
		$result["argu"]=$conf;
6315
 
6315
 
6316
		#如果 $conf 不為陣列
6316
		#如果 $conf 不為陣列
6317
		if(gettype($conf)!=="array"){
6317
		if(gettype($conf)!=="array"){
6318
			
6318
 
6319
			#設置執行失敗
6319
			#設置執行失敗
6320
			$result["status"]="false";
6320
			$result["status"]="false";
6321
			
6321
 
6322
			#設置執行錯誤訊息
6322
			#設置執行錯誤訊息
6323
			$result["error"][]="\$conf變數須為陣列形態";
6323
			$result["error"][]="\$conf變數須為陣列形態";
6324
			
6324
 
6325
			#如果傳入的參數為 null
6325
			#如果傳入的參數為 null
6326
			if($conf===null){
6326
			if($conf===null){
6327
				
6327
 
6328
				#設置執行錯誤訊息
6328
				#設置執行錯誤訊息
6329
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
6329
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
6330
				
6330
 
6331
				}#if end
6331
				}#if end
6332
 
6332
 
6333
			#回傳結果
6333
			#回傳結果
6334
			return $result;
6334
			return $result;
6335
			
6335
 
6336
			}#if end
6336
			}#if end
6337
		
6337
 
6338
		#檢查參數
6338
		#檢查參數
6339
		#函式說明:
6339
		#函式說明:
6340
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
6340
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
6341
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
6341
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
6342
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
6342
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
Line 6367... Line 6367...
6367
		#$conf["canBeEmpty"]=array();
6367
		#$conf["canBeEmpty"]=array();
6368
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
6368
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
6369
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array();
6369
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array();
6370
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
6370
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
6371
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("disReg");
6371
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("disReg");
6372
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double"); 
6372
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
6373
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string");
6373
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string");
6374
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
6374
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
6375
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("false");
6375
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("false");
6376
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
6376
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
6377
		#$conf["arrayCountEqualCheck"][]=array();
6377
		#$conf["arrayCountEqualCheck"][]=array();
Line 6379... Line 6379...
6379
		#array_keys=>http://php.net/manual/en/function.array-keys.php
6379
		#array_keys=>http://php.net/manual/en/function.array-keys.php
6380
		#建議:
6380
		#建議:
6381
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
6381
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
6382
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
6382
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
6383
		unset($conf["variableCheck::checkArguments"]);
6383
		unset($conf["variableCheck::checkArguments"]);
6384
		
6384
 
6385
		#如果檢查參數失敗
6385
		#如果檢查參數失敗
6386
		if($checkArguments["status"]==="false"){
6386
		if($checkArguments["status"]==="false"){
6387
			
6387
 
6388
			#設置執行失敗
6388
			#設置執行失敗
6389
			$result["status"]="false";
6389
			$result["status"]="false";
6390
		
6390
 
6391
			#設置執行錯誤訊息
6391
			#設置執行錯誤訊息
6392
			$result["error"]=$checkArguments;
6392
			$result["error"]=$checkArguments;
6393
			
6393
 
6394
			#回傳結果
6394
			#回傳結果
6395
			return $result;
6395
			return $result;
6396
			
6396
 
6397
			}#if end
6397
			}#if end
6398
			
6398
 
6399
		#如果參數檢查不通過
6399
		#如果參數檢查不通過
6400
		if($checkArguments["passed"]==="false"){
6400
		if($checkArguments["passed"]==="false"){
6401
			
6401
 
6402
			#設置執行失敗
6402
			#設置執行失敗
6403
			$result["status"]="false";
6403
			$result["status"]="false";
6404
		
6404
 
6405
			#設置執行錯誤訊息
6405
			#設置執行錯誤訊息
6406
			$result["error"]=$checkArguments;
6406
			$result["error"]=$checkArguments;
6407
			
6407
 
6408
			#回傳結果
6408
			#回傳結果
6409
			return $result;
6409
			return $result;
6410
			
6410
 
6411
			}#if end
6411
			}#if end
6412
 
6412
 
6413
		#執行到這邊代表是命令列環境
6413
		#執行到這邊代表是命令列環境
6414
		
6414
 
6415
		#如果沒有要取消設定 passProxy
6415
		#如果沒有要取消設定 passProxy
6416
		if($conf["disReg"]==="false"){
6416
		if($conf["disReg"]==="false"){
6417
			
6417
 
6418
			#確認設定 passProxy
6418
			#確認設定 passProxy
6419
			#函式說明:
6419
			#函式說明:
6420
			#註冊聊天室範例的 passProxy 設定,本函數會用root帳戶去新增.
6420
			#註冊聊天室範例的 passProxy 設定,本函數會用root帳戶去新增.
6421
			#會在 /etc/httpd/conf/httpd.conf 檔案裡面加上以下內容
6421
			#會在 /etc/httpd/conf/httpd.conf 檔案裡面加上以下內容
6422
			##Enable the mod_proxy modules in the HTTPD
6422
			##Enable the mod_proxy modules in the HTTPD
Line 6435... Line 6435...
6435
			$conf["webSock::registerChatDemo"]["fileArgu"]=$conf["fileArgu"];
6435
			$conf["webSock::registerChatDemo"]["fileArgu"]=$conf["fileArgu"];
6436
			#可省略參數:
6436
			#可省略參數:
6437
			#$conf["rootPasswd"],字串,root帳戶的密碼,預設為"password".
6437
			#$conf["rootPasswd"],字串,root帳戶的密碼,預設為"password".
6438
			#$conf["rootPasswd"]="password";
6438
			#$conf["rootPasswd"]="password";
6439
			#$conf["httpd.conf"],字串,apache設定檔的路徑與名稱,預設為"/etc/httpd/conf/httpd.conf".
6439
			#$conf["httpd.conf"],字串,apache設定檔的路徑與名稱,預設為"/etc/httpd/conf/httpd.conf".
6440
			#$conf["httpd.conf"]="/etc/httpd/conf/httpd.conf";		
6440
			#$conf["httpd.conf"]="/etc/httpd/conf/httpd.conf";
6441
			#備註:
6441
			#備註:
6442
			#僅能在命令列環境下執行.
6442
			#僅能在命令列環境下執行.
6443
			#改變暫存檔案權限的功能異常.
6443
			#改變暫存檔案權限的功能異常.
6444
			#參考資料:
6444
			#參考資料:
6445
			#用apache的passProxy來識別服務並轉到正確的port=>https://groups.google.com/forum/#!topic/ratchet-php/dj-PgPPO_J0
6445
			#用apache的passProxy來識別服務並轉到正確的port=>https://groups.google.com/forum/#!topic/ratchet-php/dj-PgPPO_J0
6446
			$registerChatDemo=webSock::registerChatDemo($conf["webSock::registerChatDemo"]);
6446
			$registerChatDemo=webSock::registerChatDemo($conf["webSock::registerChatDemo"]);
6447
			unset($conf["webSock::registerChatDemo"]);
6447
			unset($conf["webSock::registerChatDemo"]);
6448
			
6448
 
6449
			#如果註冊 ChatDemo 服務失敗
6449
			#如果註冊 ChatDemo 服務失敗
6450
			if($registerChatDemo["status"]==="false"){
6450
			if($registerChatDemo["status"]==="false"){
6451
				
6451
 
6452
				#設置執行失敗
6452
				#設置執行失敗
6453
				$result["status"]="false";
6453
				$result["status"]="false";
6454
				
6454
 
6455
				#設置執行錯誤訊息
6455
				#設置執行錯誤訊息
6456
				$result["error"]=$registerChatDemo;
6456
				$result["error"]=$registerChatDemo;
6457
				
6457
 
6458
				#回傳結果
6458
				#回傳結果
6459
				return $result;
6459
				return $result;
6460
				
6460
 
6461
				}#if end
6461
				}#if end
6462
			
6462
 
6463
			}#if end
6463
			}#if end
6464
				
6464
 
6465
		#函式說明:
6465
		#函式說明:
6466
		#擴充過的Ratchet聊天室伺服端範例,port為8080.
6466
		#擴充過的Ratchet聊天室伺服端範例,port為8080.
6467
		#回傳結果:
6467
		#回傳結果:
6468
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
6468
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
6469
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
6469
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
6470
		#$result["function"],當前執行的函式名稱.		
6470
		#$result["function"],當前執行的函式名稱.
6471
		#參考資料:
6471
		#參考資料:
6472
		#Ratchet官網=>http://socketo.me/
6472
		#Ratchet官網=>http://socketo.me/
6473
		#聊天室範例=>http://socketo.me/docs/hello-world
6473
		#聊天室範例=>http://socketo.me/docs/hello-world
6474
		#備註:
6474
		#備註:
6475
		#必須要在命令列環境下執行才行.
6475
		#必須要在命令列環境下執行才行.
6476
		$chatRoomServerDemo=self::chatRoomServerDemo();
6476
		$chatRoomServerDemo=self::chatRoomServerDemo();
6477
			
6477
 
6478
		#如果運行 chatRoomServerDemo 失敗
6478
		#如果運行 chatRoomServerDemo 失敗
6479
		if($chatRoomServerDemo["status"]==="false"){
6479
		if($chatRoomServerDemo["status"]==="false"){
6480
			
6480
 
6481
			#設置執行失敗
6481
			#設置執行失敗
6482
			$result["status"]="false";
6482
			$result["status"]="false";
6483
			
6483
 
6484
			#設置執行錯誤訊息
6484
			#設置執行錯誤訊息
6485
			$result["error"]=$chatRoomServerDemo;
6485
			$result["error"]=$chatRoomServerDemo;
6486
			
6486
 
6487
			#回傳結果
6487
			#回傳結果
6488
			return $result;
6488
			return $result;
6489
			
6489
 
6490
			}#if end	
6490
			}#if end
6491
			
6491
 
6492
		#設置執行正常
6492
		#設置執行正常
6493
		$result["status"]="true";
6493
		$result["status"]="true";
6494
			
6494
 
6495
		#回傳結果	
6495
		#回傳結果
6496
		return $result;
6496
		return $result;
6497
	
6497
 
6498
		}#function chatRoomDemo end
6498
		}#function chatRoomDemo end
6499
 
6499
 
6500
	/*
6500
	/*
6501
	#函式說明:
6501
	#函式說明:
6502
	#擴充過的Ratchet的聊天室用戶端與伺服端範例第二版,port為8080.
6502
	#擴充過的Ratchet的聊天室用戶端與伺服端範例第二版,port為8080.
6503
	#回傳結果:
6503
	#回傳結果:
6504
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
6504
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
6505
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
6505
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
6506
	#$result["function"],當前執行的函式名稱.		
6506
	#$result["function"],當前執行的函式名稱.
6507
	#必填參數:
6507
	#必填參數:
6508
	#$conf["fileArgu"],字串,__FILE__的內容.
6508
	#$conf["fileArgu"],字串,__FILE__的內容.
6509
	$conf["fileArgu"]=__FILE__;
6509
	$conf["fileArgu"]=__FILE__;
6510
	#可省略參數:
6510
	#可省略參數:
6511
	#$conf["disReg"],字串,是否要取消apache的passProxy檢查與設定,預設為"false"不取消,"true"為取消.
6511
	#$conf["disReg"],字串,是否要取消apache的passProxy檢查與設定,預設為"false"不取消,"true"為取消.
Line 6528... Line 6528...
6528
		#涵式說明:
6528
		#涵式說明:
6529
		#判斷當前環境為web還是cmd
6529
		#判斷當前環境為web還是cmd
6530
		#回傳結果:
6530
		#回傳結果:
6531
		#$result,"web"或"cmd"
6531
		#$result,"web"或"cmd"
6532
		if(csInformation::getEnv()==="web"){
6532
		if(csInformation::getEnv()==="web"){
6533
			
6533
 
6534
			#建立 web socket client 的 js 語法
6534
			#建立 web socket client 的 js 語法
6535
			#函式說明:
6535
			#函式說明:
6536
			#聊天室用戶端js範例
6536
			#聊天室用戶端js範例
6537
			#回傳結果:
6537
			#回傳結果:
6538
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
6538
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
6539
			#$reuslt["error"],執行不正常結束的錯訊息陣列.
6539
			#$reuslt["error"],執行不正常結束的錯訊息陣列.
6540
			#$result["function"],當前執行的函式名稱.		
6540
			#$result["function"],當前執行的函式名稱.
6541
			#$result["content"],js語法.
6541
			#$result["content"],js語法.
6542
			#參考資料:
6542
			#參考資料:
6543
			#Ratchet官網=>http://socketo.me/
6543
			#Ratchet官網=>http://socketo.me/
6544
			#聊天室範例=>http://socketo.me/docs/hello-world
6544
			#聊天室範例=>http://socketo.me/docs/hello-world
6545
			$chatRoomClientDemo=webSock::chatRoomClientDemoV2();
6545
			$chatRoomClientDemo=webSock::chatRoomClientDemoV2();
6546
			
6546
 
6547
			#如果建立聊天室用戶端失敗
6547
			#如果建立聊天室用戶端失敗
6548
			if($chatRoomClientDemo["status"]==="false"){
6548
			if($chatRoomClientDemo["status"]==="false"){
6549
				
6549
 
6550
				#設置執行失敗
6550
				#設置執行失敗
6551
				$result["status"]="false";
6551
				$result["status"]="false";
6552
				
6552
 
6553
				#設置執行錯誤訊息
6553
				#設置執行錯誤訊息
6554
				$result["error"]=$chatRoomClientDemo;
6554
				$result["error"]=$chatRoomClientDemo;
6555
				
6555
 
6556
				#回傳結果
6556
				#回傳結果
6557
				return $result;
6557
				return $result;
6558
				
6558
 
6559
				}#if end
6559
				}#if end
6560
				
6560
 
6561
			#取得聊天室的語法
6561
			#取得聊天室的語法
6562
			$result["content"]=$chatRoomClientDemo["content"];
6562
			$result["content"]=$chatRoomClientDemo["content"];
6563
			
6563
 
6564
			#設置執行正常
6564
			#設置執行正常
6565
			$result["status"]="true";
6565
			$result["status"]="true";
6566
			
6566
 
6567
			#回傳結果
6567
			#回傳結果
6568
			return $result;
6568
			return $result;
6569
			
6569
 
6570
			}#if end
6570
			}#if end
6571
 
6571
 
6572
		#如果沒有參數
6572
		#如果沒有參數
6573
		if(func_num_args()==0){
6573
		if(func_num_args()==0){
6574
			
6574
 
6575
			#設置執行失敗
6575
			#設置執行失敗
6576
			$result["status"]="false";
6576
			$result["status"]="false";
6577
			
6577
 
6578
			#設置執行錯誤訊息
6578
			#設置執行錯誤訊息
6579
			$result["error"]="函數".$result["function"]."需要參數";
6579
			$result["error"]="函數".$result["function"]."需要參數";
6580
			
6580
 
6581
			#回傳結果
6581
			#回傳結果
6582
			return $result;
6582
			return $result;
6583
			
6583
 
6584
			}#if end
6584
			}#if end
6585
			
6585
 
6586
		#如果沒有參數
6586
		#如果沒有參數
6587
		if(func_num_args()==0){
6587
		if(func_num_args()==0){
6588
			
6588
 
6589
			#設置執行失敗
6589
			#設置執行失敗
6590
			$result["status"]="false";
6590
			$result["status"]="false";
6591
			
6591
 
6592
			#設置執行錯誤訊息
6592
			#設置執行錯誤訊息
6593
			$result["error"]="函數".$result["function"]."需要參數";
6593
			$result["error"]="函數".$result["function"]."需要參數";
6594
			
6594
 
6595
			#回傳結果
6595
			#回傳結果
6596
			return $result;
6596
			return $result;
6597
			
6597
 
6598
			}#if end
6598
			}#if end
6599
			
6599
 
6600
		#取得參數
6600
		#取得參數
6601
		$result["argu"]=$conf;
6601
		$result["argu"]=$conf;
6602
 
6602
 
6603
		#如果 $conf 不為陣列
6603
		#如果 $conf 不為陣列
6604
		if(gettype($conf)!=="array"){
6604
		if(gettype($conf)!=="array"){
6605
			
6605
 
6606
			#設置執行失敗
6606
			#設置執行失敗
6607
			$result["status"]="false";
6607
			$result["status"]="false";
6608
			
6608
 
6609
			#設置執行錯誤訊息
6609
			#設置執行錯誤訊息
6610
			$result["error"][]="\$conf變數須為陣列形態";
6610
			$result["error"][]="\$conf變數須為陣列形態";
6611
			
6611
 
6612
			#如果傳入的參數為 null
6612
			#如果傳入的參數為 null
6613
			if($conf===null){
6613
			if($conf===null){
6614
				
6614
 
6615
				#設置執行錯誤訊息
6615
				#設置執行錯誤訊息
6616
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
6616
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
6617
				
6617
 
6618
				}#if end
6618
				}#if end
6619
 
6619
 
6620
			#回傳結果
6620
			#回傳結果
6621
			return $result;
6621
			return $result;
6622
			
6622
 
6623
			}#if end
6623
			}#if end
6624
		
6624
 
6625
		#檢查參數
6625
		#檢查參數
6626
		#函式說明:
6626
		#函式說明:
6627
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
6627
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
6628
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
6628
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
6629
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
6629
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
Line 6654... Line 6654...
6654
		#$conf["canBeEmpty"]=array();
6654
		#$conf["canBeEmpty"]=array();
6655
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
6655
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
6656
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array();
6656
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array();
6657
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
6657
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
6658
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("disReg");
6658
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("disReg");
6659
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double"); 
6659
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
6660
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string");
6660
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string");
6661
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
6661
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
6662
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("false");
6662
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("false");
6663
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
6663
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
6664
		#$conf["arrayCountEqualCheck"][]=array();
6664
		#$conf["arrayCountEqualCheck"][]=array();
Line 6666... Line 6666...
6666
		#array_keys=>http://php.net/manual/en/function.array-keys.php
6666
		#array_keys=>http://php.net/manual/en/function.array-keys.php
6667
		#建議:
6667
		#建議:
6668
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
6668
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
6669
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
6669
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
6670
		unset($conf["variableCheck::checkArguments"]);
6670
		unset($conf["variableCheck::checkArguments"]);
6671
		
6671
 
6672
		#如果檢查參數失敗
6672
		#如果檢查參數失敗
6673
		if($checkArguments["status"]==="false"){
6673
		if($checkArguments["status"]==="false"){
6674
			
6674
 
6675
			#設置執行失敗
6675
			#設置執行失敗
6676
			$result["status"]="false";
6676
			$result["status"]="false";
6677
		
6677
 
6678
			#設置執行錯誤訊息
6678
			#設置執行錯誤訊息
6679
			$result["error"]=$checkArguments;
6679
			$result["error"]=$checkArguments;
6680
			
6680
 
6681
			#回傳結果
6681
			#回傳結果
6682
			return $result;
6682
			return $result;
6683
			
6683
 
6684
			}#if end
6684
			}#if end
6685
			
6685
 
6686
		#如果參數檢查不通過
6686
		#如果參數檢查不通過
6687
		if($checkArguments["passed"]==="false"){
6687
		if($checkArguments["passed"]==="false"){
6688
			
6688
 
6689
			#設置執行失敗
6689
			#設置執行失敗
6690
			$result["status"]="false";
6690
			$result["status"]="false";
6691
		
6691
 
6692
			#設置執行錯誤訊息
6692
			#設置執行錯誤訊息
6693
			$result["error"]=$checkArguments;
6693
			$result["error"]=$checkArguments;
6694
			
6694
 
6695
			#回傳結果
6695
			#回傳結果
6696
			return $result;
6696
			return $result;
6697
			
6697
 
6698
			}#if end
6698
			}#if end
6699
 
6699
 
6700
		#執行到這邊代表是命令列環境
6700
		#執行到這邊代表是命令列環境
6701
		
6701
 
6702
		#如果沒有要取消設定 passProxy
6702
		#如果沒有要取消設定 passProxy
6703
		if($conf["disReg"]==="false"){
6703
		if($conf["disReg"]==="false"){
6704
			
6704
 
6705
			#確認設定 passProxy
6705
			#確認設定 passProxy
6706
			#函式說明:
6706
			#函式說明:
6707
			#註冊聊天室範例的 passProxy 設定,本函數會用root帳戶去新增.
6707
			#註冊聊天室範例的 passProxy 設定,本函數會用root帳戶去新增.
6708
			#會在 /etc/httpd/conf/httpd.conf 檔案裡面加上以下內容
6708
			#會在 /etc/httpd/conf/httpd.conf 檔案裡面加上以下內容
6709
			##Enable the mod_proxy modules in the HTTPD
6709
			##Enable the mod_proxy modules in the HTTPD
Line 6722... Line 6722...
6722
			$conf["webSock::registerChatDemo"]["fileArgu"]=$conf["fileArgu"];
6722
			$conf["webSock::registerChatDemo"]["fileArgu"]=$conf["fileArgu"];
6723
			#可省略參數:
6723
			#可省略參數:
6724
			#$conf["rootPasswd"],字串,root帳戶的密碼,預設為"password".
6724
			#$conf["rootPasswd"],字串,root帳戶的密碼,預設為"password".
6725
			#$conf["rootPasswd"]="password";
6725
			#$conf["rootPasswd"]="password";
6726
			#$conf["httpd.conf"],字串,apache設定檔的路徑與名稱,預設為"/etc/httpd/conf/httpd.conf".
6726
			#$conf["httpd.conf"],字串,apache設定檔的路徑與名稱,預設為"/etc/httpd/conf/httpd.conf".
6727
			#$conf["httpd.conf"]="/etc/httpd/conf/httpd.conf";		
6727
			#$conf["httpd.conf"]="/etc/httpd/conf/httpd.conf";
6728
			#備註:
6728
			#備註:
6729
			#僅能在命令列環境下執行.
6729
			#僅能在命令列環境下執行.
6730
			#改變暫存檔案權限的功能異常.
6730
			#改變暫存檔案權限的功能異常.
6731
			#參考資料:
6731
			#參考資料:
6732
			#用apache的passProxy來識別服務並轉到正確的port=>https://groups.google.com/forum/#!topic/ratchet-php/dj-PgPPO_J0
6732
			#用apache的passProxy來識別服務並轉到正確的port=>https://groups.google.com/forum/#!topic/ratchet-php/dj-PgPPO_J0
6733
			$registerChatDemo=webSock::registerChatDemo($conf["webSock::registerChatDemo"]);
6733
			$registerChatDemo=webSock::registerChatDemo($conf["webSock::registerChatDemo"]);
6734
			unset($conf["webSock::registerChatDemo"]);
6734
			unset($conf["webSock::registerChatDemo"]);
6735
			
6735
 
6736
			#如果註冊 ChatDemo 服務失敗
6736
			#如果註冊 ChatDemo 服務失敗
6737
			if($registerChatDemo["status"]==="false"){
6737
			if($registerChatDemo["status"]==="false"){
6738
				
6738
 
6739
				#設置執行失敗
6739
				#設置執行失敗
6740
				$result["status"]="false";
6740
				$result["status"]="false";
6741
				
6741
 
6742
				#設置執行錯誤訊息
6742
				#設置執行錯誤訊息
6743
				$result["error"]=$registerChatDemo;
6743
				$result["error"]=$registerChatDemo;
6744
				
6744
 
6745
				#回傳結果
6745
				#回傳結果
6746
				return $result;
6746
				return $result;
6747
				
6747
 
6748
				}#if end
6748
				}#if end
6749
			
6749
 
6750
			}#if end
6750
			}#if end
6751
				
6751
 
6752
		#函式說明:
6752
		#函式說明:
6753
		#擴充過的Ratchet聊天室伺服端範例,port為8080.
6753
		#擴充過的Ratchet聊天室伺服端範例,port為8080.
6754
		#回傳結果:
6754
		#回傳結果:
6755
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
6755
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
6756
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
6756
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
6757
		#$result["function"],當前執行的函式名稱.		
6757
		#$result["function"],當前執行的函式名稱.
6758
		#參考資料:
6758
		#參考資料:
6759
		#Ratchet官網=>http://socketo.me/
6759
		#Ratchet官網=>http://socketo.me/
6760
		#聊天室範例=>http://socketo.me/docs/hello-world
6760
		#聊天室範例=>http://socketo.me/docs/hello-world
6761
		#備註:
6761
		#備註:
6762
		#必須要在命令列環境下執行才行.
6762
		#必須要在命令列環境下執行才行.
6763
		$chatRoomServerDemo=self::chatRoomServerDemoV2();
6763
		$chatRoomServerDemo=self::chatRoomServerDemoV2();
6764
			
6764
 
6765
		#如果運行 chatRoomServerDemo 失敗
6765
		#如果運行 chatRoomServerDemo 失敗
6766
		if($chatRoomServerDemo["status"]==="false"){
6766
		if($chatRoomServerDemo["status"]==="false"){
6767
			
6767
 
6768
			#設置執行失敗
6768
			#設置執行失敗
6769
			$result["status"]="false";
6769
			$result["status"]="false";
6770
			
6770
 
6771
			#設置執行錯誤訊息
6771
			#設置執行錯誤訊息
6772
			$result["error"]=$chatRoomServerDemo;
6772
			$result["error"]=$chatRoomServerDemo;
6773
			
6773
 
6774
			#回傳結果
6774
			#回傳結果
6775
			return $result;
6775
			return $result;
6776
			
6776
 
6777
			}#if end	
6777
			}#if end
6778
			
6778
 
6779
		#設置執行正常
6779
		#設置執行正常
6780
		$result["status"]="true";
6780
		$result["status"]="true";
6781
			
6781
 
6782
		#回傳結果	
6782
		#回傳結果
6783
		return $result;
6783
		return $result;
6784
	
6784
 
6785
		}#function chatRoomDemoV2 end
6785
		}#function chatRoomDemoV2 end
6786
 
6786
 
6787
	/*
6787
	/*
6788
	#函式說明:
6788
	#函式說明:
6789
	#擴充過的Ratchet的聊天室用戶端與伺服端範例第二版,port為8080.
6789
	#擴充過的Ratchet的聊天室用戶端與伺服端範例第二版,port為8080.
6790
	#回傳結果:
6790
	#回傳結果:
6791
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
6791
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
6792
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
6792
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
6793
	#$result["function"],當前執行的函式名稱.		
6793
	#$result["function"],當前執行的函式名稱.
6794
	#必填參數:
6794
	#必填參數:
6795
	#$conf["fileArgu"],字串,__FILE__的內容.
6795
	#$conf["fileArgu"],字串,__FILE__的內容.
6796
	$conf["fileArgu"]=__FILE__;
6796
	$conf["fileArgu"]=__FILE__;
6797
	#可省略參數:
6797
	#可省略參數:
6798
	#$conf["disReg"],字串,是否要取消apache的passProxy檢查與設定,預設為"false"不取消,"true"為取消.
6798
	#$conf["disReg"],字串,是否要取消apache的passProxy檢查與設定,預設為"false"不取消,"true"為取消.
Line 6815... Line 6815...
6815
		#涵式說明:
6815
		#涵式說明:
6816
		#判斷當前環境為web還是cmd
6816
		#判斷當前環境為web還是cmd
6817
		#回傳結果:
6817
		#回傳結果:
6818
		#$result,"web"或"cmd"
6818
		#$result,"web"或"cmd"
6819
		if(csInformation::getEnv()==="web"){
6819
		if(csInformation::getEnv()==="web"){
6820
			
6820
 
6821
			#建立 web socket client 的 js 語法
6821
			#建立 web socket client 的 js 語法
6822
			#函式說明:
6822
			#函式說明:
6823
			#聊天室用戶端js範例
6823
			#聊天室用戶端js範例
6824
			#回傳結果:
6824
			#回傳結果:
6825
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
6825
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
6826
			#$reuslt["error"],執行不正常結束的錯訊息陣列.
6826
			#$reuslt["error"],執行不正常結束的錯訊息陣列.
6827
			#$result["function"],當前執行的函式名稱.		
6827
			#$result["function"],當前執行的函式名稱.
6828
			#$result["content"],js語法.
6828
			#$result["content"],js語法.
6829
			#參考資料:
6829
			#參考資料:
6830
			#Ratchet官網=>http://socketo.me/
6830
			#Ratchet官網=>http://socketo.me/
6831
			#聊天室範例=>http://socketo.me/docs/hello-world
6831
			#聊天室範例=>http://socketo.me/docs/hello-world
6832
			$chatRoomClientDemo=webSock::chatRoomClientDemoV3();
6832
			$chatRoomClientDemo=webSock::chatRoomClientDemoV3();
6833
			
6833
 
6834
			#如果建立聊天室用戶端失敗
6834
			#如果建立聊天室用戶端失敗
6835
			if($chatRoomClientDemo["status"]==="false"){
6835
			if($chatRoomClientDemo["status"]==="false"){
6836
				
6836
 
6837
				#設置執行失敗
6837
				#設置執行失敗
6838
				$result["status"]="false";
6838
				$result["status"]="false";
6839
				
6839
 
6840
				#設置執行錯誤訊息
6840
				#設置執行錯誤訊息
6841
				$result["error"]=$chatRoomClientDemo;
6841
				$result["error"]=$chatRoomClientDemo;
6842
				
6842
 
6843
				#回傳結果
6843
				#回傳結果
6844
				return $result;
6844
				return $result;
6845
				
6845
 
6846
				}#if end
6846
				}#if end
6847
				
6847
 
6848
			#取得聊天室的語法
6848
			#取得聊天室的語法
6849
			$result["content"]=$chatRoomClientDemo["content"];
6849
			$result["content"]=$chatRoomClientDemo["content"];
6850
			
6850
 
6851
			#設置執行正常
6851
			#設置執行正常
6852
			$result["status"]="true";
6852
			$result["status"]="true";
6853
			
6853
 
6854
			#回傳結果
6854
			#回傳結果
6855
			return $result;
6855
			return $result;
6856
			
6856
 
6857
			}#if end
6857
			}#if end
6858
 
6858
 
6859
		#如果沒有參數
6859
		#如果沒有參數
6860
		if(func_num_args()==0){
6860
		if(func_num_args()==0){
6861
			
6861
 
6862
			#設置執行失敗
6862
			#設置執行失敗
6863
			$result["status"]="false";
6863
			$result["status"]="false";
6864
			
6864
 
6865
			#設置執行錯誤訊息
6865
			#設置執行錯誤訊息
6866
			$result["error"]="函數".$result["function"]."需要參數";
6866
			$result["error"]="函數".$result["function"]."需要參數";
6867
			
6867
 
6868
			#回傳結果
6868
			#回傳結果
6869
			return $result;
6869
			return $result;
6870
			
6870
 
6871
			}#if end
6871
			}#if end
6872
			
6872
 
6873
		#如果沒有參數
6873
		#如果沒有參數
6874
		if(func_num_args()==0){
6874
		if(func_num_args()==0){
6875
			
6875
 
6876
			#設置執行失敗
6876
			#設置執行失敗
6877
			$result["status"]="false";
6877
			$result["status"]="false";
6878
			
6878
 
6879
			#設置執行錯誤訊息
6879
			#設置執行錯誤訊息
6880
			$result["error"]="函數".$result["function"]."需要參數";
6880
			$result["error"]="函數".$result["function"]."需要參數";
6881
			
6881
 
6882
			#回傳結果
6882
			#回傳結果
6883
			return $result;
6883
			return $result;
6884
			
6884
 
6885
			}#if end
6885
			}#if end
6886
			
6886
 
6887
		#取得參數
6887
		#取得參數
6888
		$result["argu"]=$conf;
6888
		$result["argu"]=$conf;
6889
 
6889
 
6890
		#如果 $conf 不為陣列
6890
		#如果 $conf 不為陣列
6891
		if(gettype($conf)!=="array"){
6891
		if(gettype($conf)!=="array"){
6892
			
6892
 
6893
			#設置執行失敗
6893
			#設置執行失敗
6894
			$result["status"]="false";
6894
			$result["status"]="false";
6895
			
6895
 
6896
			#設置執行錯誤訊息
6896
			#設置執行錯誤訊息
6897
			$result["error"][]="\$conf變數須為陣列形態";
6897
			$result["error"][]="\$conf變數須為陣列形態";
6898
			
6898
 
6899
			#如果傳入的參數為 null
6899
			#如果傳入的參數為 null
6900
			if($conf===null){
6900
			if($conf===null){
6901
				
6901
 
6902
				#設置執行錯誤訊息
6902
				#設置執行錯誤訊息
6903
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
6903
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
6904
				
6904
 
6905
				}#if end
6905
				}#if end
6906
 
6906
 
6907
			#回傳結果
6907
			#回傳結果
6908
			return $result;
6908
			return $result;
6909
			
6909
 
6910
			}#if end
6910
			}#if end
6911
		
6911
 
6912
		#檢查參數
6912
		#檢查參數
6913
		#函式說明:
6913
		#函式說明:
6914
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
6914
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
6915
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
6915
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
6916
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
6916
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
Line 6941... Line 6941...
6941
		#$conf["canBeEmpty"]=array();
6941
		#$conf["canBeEmpty"]=array();
6942
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
6942
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
6943
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array();
6943
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array();
6944
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
6944
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
6945
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("disReg");
6945
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("disReg");
6946
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double"); 
6946
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
6947
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string");
6947
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string");
6948
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
6948
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
6949
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("false");
6949
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("false");
6950
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
6950
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
6951
		#$conf["arrayCountEqualCheck"][]=array();
6951
		#$conf["arrayCountEqualCheck"][]=array();
Line 6953... Line 6953...
6953
		#array_keys=>http://php.net/manual/en/function.array-keys.php
6953
		#array_keys=>http://php.net/manual/en/function.array-keys.php
6954
		#建議:
6954
		#建議:
6955
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
6955
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
6956
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
6956
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
6957
		unset($conf["variableCheck::checkArguments"]);
6957
		unset($conf["variableCheck::checkArguments"]);
6958
		
6958
 
6959
		#如果檢查參數失敗
6959
		#如果檢查參數失敗
6960
		if($checkArguments["status"]==="false"){
6960
		if($checkArguments["status"]==="false"){
6961
			
6961
 
6962
			#設置執行失敗
6962
			#設置執行失敗
6963
			$result["status"]="false";
6963
			$result["status"]="false";
6964
		
6964
 
6965
			#設置執行錯誤訊息
6965
			#設置執行錯誤訊息
6966
			$result["error"]=$checkArguments;
6966
			$result["error"]=$checkArguments;
6967
			
6967
 
6968
			#回傳結果
6968
			#回傳結果
6969
			return $result;
6969
			return $result;
6970
			
6970
 
6971
			}#if end
6971
			}#if end
6972
			
6972
 
6973
		#如果參數檢查不通過
6973
		#如果參數檢查不通過
6974
		if($checkArguments["passed"]==="false"){
6974
		if($checkArguments["passed"]==="false"){
6975
			
6975
 
6976
			#設置執行失敗
6976
			#設置執行失敗
6977
			$result["status"]="false";
6977
			$result["status"]="false";
6978
		
6978
 
6979
			#設置執行錯誤訊息
6979
			#設置執行錯誤訊息
6980
			$result["error"]=$checkArguments;
6980
			$result["error"]=$checkArguments;
6981
			
6981
 
6982
			#回傳結果
6982
			#回傳結果
6983
			return $result;
6983
			return $result;
6984
			
6984
 
6985
			}#if end
6985
			}#if end
6986
 
6986
 
6987
		#執行到這邊代表是命令列環境
6987
		#執行到這邊代表是命令列環境
6988
		
6988
 
6989
		#如果沒有要取消設定 passProxy
6989
		#如果沒有要取消設定 passProxy
6990
		if($conf["disReg"]==="false"){
6990
		if($conf["disReg"]==="false"){
6991
			
6991
 
6992
			#確認設定 passProxy
6992
			#確認設定 passProxy
6993
			#函式說明:
6993
			#函式說明:
6994
			#註冊聊天室範例的 passProxy 設定,本函數會用root帳戶去新增.
6994
			#註冊聊天室範例的 passProxy 設定,本函數會用root帳戶去新增.
6995
			#會在 /etc/httpd/conf/httpd.conf 檔案裡面加上以下內容
6995
			#會在 /etc/httpd/conf/httpd.conf 檔案裡面加上以下內容
6996
			##Enable the mod_proxy modules in the HTTPD
6996
			##Enable the mod_proxy modules in the HTTPD
Line 7009... Line 7009...
7009
			$conf["webSock::registerChatDemo"]["fileArgu"]=$conf["fileArgu"];
7009
			$conf["webSock::registerChatDemo"]["fileArgu"]=$conf["fileArgu"];
7010
			#可省略參數:
7010
			#可省略參數:
7011
			#$conf["rootPasswd"],字串,root帳戶的密碼,預設為"password".
7011
			#$conf["rootPasswd"],字串,root帳戶的密碼,預設為"password".
7012
			#$conf["rootPasswd"]="password";
7012
			#$conf["rootPasswd"]="password";
7013
			#$conf["httpd.conf"],字串,apache設定檔的路徑與名稱,預設為"/etc/httpd/conf/httpd.conf".
7013
			#$conf["httpd.conf"],字串,apache設定檔的路徑與名稱,預設為"/etc/httpd/conf/httpd.conf".
7014
			#$conf["httpd.conf"]="/etc/httpd/conf/httpd.conf";		
7014
			#$conf["httpd.conf"]="/etc/httpd/conf/httpd.conf";
7015
			#備註:
7015
			#備註:
7016
			#僅能在命令列環境下執行.
7016
			#僅能在命令列環境下執行.
7017
			#改變暫存檔案權限的功能異常.
7017
			#改變暫存檔案權限的功能異常.
7018
			#參考資料:
7018
			#參考資料:
7019
			#用apache的passProxy來識別服務並轉到正確的port=>https://groups.google.com/forum/#!topic/ratchet-php/dj-PgPPO_J0
7019
			#用apache的passProxy來識別服務並轉到正確的port=>https://groups.google.com/forum/#!topic/ratchet-php/dj-PgPPO_J0
7020
			$registerChatDemo=webSock::registerChatDemo($conf["webSock::registerChatDemo"]);
7020
			$registerChatDemo=webSock::registerChatDemo($conf["webSock::registerChatDemo"]);
7021
			unset($conf["webSock::registerChatDemo"]);
7021
			unset($conf["webSock::registerChatDemo"]);
7022
			
7022
 
7023
			#如果註冊 ChatDemo 服務失敗
7023
			#如果註冊 ChatDemo 服務失敗
7024
			if($registerChatDemo["status"]==="false"){
7024
			if($registerChatDemo["status"]==="false"){
7025
				
7025
 
7026
				#設置執行失敗
7026
				#設置執行失敗
7027
				$result["status"]="false";
7027
				$result["status"]="false";
7028
				
7028
 
7029
				#設置執行錯誤訊息
7029
				#設置執行錯誤訊息
7030
				$result["error"]=$registerChatDemo;
7030
				$result["error"]=$registerChatDemo;
7031
				
7031
 
7032
				#回傳結果
7032
				#回傳結果
7033
				return $result;
7033
				return $result;
7034
				
7034
 
7035
				}#if end
7035
				}#if end
7036
			
7036
 
7037
			}#if end
7037
			}#if end
7038
				
7038
 
7039
		#函式說明:
7039
		#函式說明:
7040
		#擴充過的Ratchet聊天室伺服端範例,port為8080.
7040
		#擴充過的Ratchet聊天室伺服端範例,port為8080.
7041
		#回傳結果:
7041
		#回傳結果:
7042
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7042
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7043
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
7043
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
7044
		#$result["function"],當前執行的函式名稱.
7044
		#$result["function"],當前執行的函式名稱.
7045
		#必填參數:
7045
		#必填參數:
7046
		#$conf["fileArgu"],__FILE__的內容.
7046
		#$conf["fileArgu"],__FILE__的內容.
7047
		$conf["webSock"]["chatRoomServerDemoV3"]["fileArgu"]=$conf["fileArgu"];		
7047
		$conf["webSock"]["chatRoomServerDemoV3"]["fileArgu"]=$conf["fileArgu"];
7048
		#參考資料:
7048
		#參考資料:
7049
		#Ratchet官網=>http://socketo.me/
7049
		#Ratchet官網=>http://socketo.me/
7050
		#聊天室範例=>http://socketo.me/docs/hello-world
7050
		#聊天室範例=>http://socketo.me/docs/hello-world
7051
		#備註:
7051
		#備註:
7052
		#必須要在命令列環境下執行才行.
7052
		#必須要在命令列環境下執行才行.
7053
		$chatRoomServerDemo=self::chatRoomServerDemoV3($conf["webSock"]["chatRoomServerDemoV3"]);
7053
		$chatRoomServerDemo=self::chatRoomServerDemoV3($conf["webSock"]["chatRoomServerDemoV3"]);
7054
		unset($conf["webSock"]["chatRoomServerDemoV3"]);
7054
		unset($conf["webSock"]["chatRoomServerDemoV3"]);
7055
			
7055
 
7056
		#如果運行 chatRoomServerDemo 失敗
7056
		#如果運行 chatRoomServerDemo 失敗
7057
		if($chatRoomServerDemo["status"]==="false"){
7057
		if($chatRoomServerDemo["status"]==="false"){
7058
			
7058
 
7059
			#設置執行失敗
7059
			#設置執行失敗
7060
			$result["status"]="false";
7060
			$result["status"]="false";
7061
			
7061
 
7062
			#設置執行錯誤訊息
7062
			#設置執行錯誤訊息
7063
			$result["error"]=$chatRoomServerDemo;
7063
			$result["error"]=$chatRoomServerDemo;
7064
			
7064
 
7065
			#回傳結果
7065
			#回傳結果
7066
			return $result;
7066
			return $result;
7067
			
7067
 
7068
			}#if end	
7068
			}#if end
7069
			
7069
 
7070
		#設置執行正常
7070
		#設置執行正常
7071
		$result["status"]="true";
7071
		$result["status"]="true";
7072
			
7072
 
7073
		#回傳結果	
7073
		#回傳結果
7074
		return $result;
7074
		return $result;
7075
	
7075
 
7076
		}#function chatRoomDemoV3 end
7076
		}#function chatRoomDemoV3 end
7077
 
7077
 
7078
	/*
7078
	/*
7079
	#函式說明:
7079
	#函式說明:
7080
	#擴充過的Ratchet聊天室伺服端範例,port為8080.
7080
	#擴充過的Ratchet聊天室伺服端範例,port為8080.
7081
	#回傳結果:
7081
	#回傳結果:
7082
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7082
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7083
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
7083
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
7084
	#$result["function"],當前執行的函式名稱.		
7084
	#$result["function"],當前執行的函式名稱.
7085
	#必填參數:
7085
	#必填參數:
7086
	#無.
7086
	#無.
7087
	#可省略參數:
7087
	#可省略參數:
7088
	#無.
7088
	#無.
7089
	#參考資料:
7089
	#參考資料:
Line 7104... Line 7104...
7104
		#涵式說明:
7104
		#涵式說明:
7105
		#判斷當前環境為web還是cmd
7105
		#判斷當前環境為web還是cmd
7106
		#回傳結果:
7106
		#回傳結果:
7107
		#$result,"web"或"cmd"
7107
		#$result,"web"或"cmd"
7108
		if(csInformation::getEnv()==="web"){
7108
		if(csInformation::getEnv()==="web"){
7109
			
7109
 
7110
			#設置執行失敗
7110
			#設置執行失敗
7111
			$result["status"]="false";
7111
			$result["status"]="false";
7112
			
7112
 
7113
			#設置執行錯誤訊息
7113
			#設置執行錯誤訊息
7114
			$result["error"][]="函數". __FUNCTION__ ."僅能在命令列環境下執行!";
7114
			$result["error"][]="函數". __FUNCTION__ ."僅能在命令列環境下執行!";
7115
			
7115
 
7116
			#回傳結果
7116
			#回傳結果
7117
			return $result;
7117
			return $result;
7118
			
7118
 
7119
			}#if end
7119
			}#if end
7120
 
7120
 
7121
		#建立 Ratchet 提供的聊天室範例(適用於telnet)
7121
		#建立 Ratchet 提供的聊天室範例(適用於telnet)
7122
		#It stores all the established connections, mediates data sent between each client and our Chat application, and catches errors. 
7122
		#It stores all the established connections, mediates data sent between each client and our Chat application, and catches errors.
7123
		#we tell the server to enter an event loop, listening for any incoming requests on port 8080. 
7123
		#we tell the server to enter an event loop, listening for any incoming requests on port 8080.
7124
		#$server = IoServer::factory(new Chat(),8080);
7124
		#$server = IoServer::factory(new Chat(),8080);
7125
		
7125
 
7126
		#建立 Ratchet 提供的聊天室範例(適用於用webServer)
7126
		#建立 Ratchet 提供的聊天室範例(適用於用webServer)
7127
		$server = IoServer::factory(
7127
		$server = IoServer::factory(
7128
			new HttpServer(
7128
			new HttpServer(
7129
				new WsServer(
7129
				new WsServer(
7130
					new Chat()
7130
					new Chat()
Line 7133... Line 7133...
7133
			8080
7133
			8080
7134
			);
7134
			);
7135
 
7135
 
7136
		#執行 server 端程式
7136
		#執行 server 端程式
7137
		$server->run();
7137
		$server->run();
7138
			
7138
 
7139
		#設置執行正常
7139
		#設置執行正常
7140
		$result["status"]="true";
7140
		$result["status"]="true";
7141
			
7141
 
7142
		#回傳結果	
7142
		#回傳結果
7143
		return $result;
7143
		return $result;
7144
		
7144
 
7145
		}#function chatRoomServerDemo end
7145
		}#function chatRoomServerDemo end
7146
		
7146
 
7147
	/*
7147
	/*
7148
	#函式說明:
7148
	#函式說明:
7149
	#擴充過的Ratchet聊天室伺服端範例第二版,port為8080.
7149
	#擴充過的Ratchet聊天室伺服端範例第二版,port為8080.
7150
	#回傳結果:
7150
	#回傳結果:
7151
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7151
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7152
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
7152
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
7153
	#$result["function"],當前執行的函式名稱.		
7153
	#$result["function"],當前執行的函式名稱.
7154
	#必填參數:
7154
	#必填參數:
7155
	#無.
7155
	#無.
7156
	#可省略參數:
7156
	#可省略參數:
7157
	#無.
7157
	#無.
7158
	#參考資料:
7158
	#參考資料:
Line 7173... Line 7173...
7173
		#涵式說明:
7173
		#涵式說明:
7174
		#判斷當前環境為web還是cmd
7174
		#判斷當前環境為web還是cmd
7175
		#回傳結果:
7175
		#回傳結果:
7176
		#$result,"web"或"cmd"
7176
		#$result,"web"或"cmd"
7177
		if(csInformation::getEnv()==="web"){
7177
		if(csInformation::getEnv()==="web"){
7178
			
7178
 
7179
			#設置執行失敗
7179
			#設置執行失敗
7180
			$result["status"]="false";
7180
			$result["status"]="false";
7181
			
7181
 
7182
			#設置執行錯誤訊息
7182
			#設置執行錯誤訊息
7183
			$result["error"][]="函數". __FUNCTION__ ."僅能在命令列環境下執行!";
7183
			$result["error"][]="函數". __FUNCTION__ ."僅能在命令列環境下執行!";
7184
			
7184
 
7185
			#回傳結果
7185
			#回傳結果
7186
			return $result;
7186
			return $result;
7187
			
7187
 
7188
			}#if end
7188
			}#if end
7189
 
7189
 
7190
		#建立 Ratchet 提供的聊天室範例(適用於telnet)
7190
		#建立 Ratchet 提供的聊天室範例(適用於telnet)
7191
		#It stores all the established connections, mediates data sent between each client and our Chat application, and catches errors. 
7191
		#It stores all the established connections, mediates data sent between each client and our Chat application, and catches errors.
7192
		#we tell the server to enter an event loop, listening for any incoming requests on port 8080. 
7192
		#we tell the server to enter an event loop, listening for any incoming requests on port 8080.
7193
		#$server = IoServer::factory(new Chat(),8080);
7193
		#$server = IoServer::factory(new Chat(),8080);
7194
		
7194
 
7195
		#建立 Ratchet 提供的聊天室範例(適用於用webServer)
7195
		#建立 Ratchet 提供的聊天室範例(適用於用webServer)
7196
		$server = IoServer::factory(
7196
		$server = IoServer::factory(
7197
			new HttpServer(
7197
			new HttpServer(
7198
				new WsServer(
7198
				new WsServer(
7199
					new ChatV2()
7199
					new ChatV2()
Line 7202... Line 7202...
7202
			8080
7202
			8080
7203
			);
7203
			);
7204
 
7204
 
7205
		#執行 server 端程式
7205
		#執行 server 端程式
7206
		$server->run();
7206
		$server->run();
7207
			
7207
 
7208
		#設置執行正常
7208
		#設置執行正常
7209
		$result["status"]="true";
7209
		$result["status"]="true";
7210
			
7210
 
7211
		#回傳結果	
7211
		#回傳結果
7212
		return $result;
7212
		return $result;
7213
		
7213
 
7214
		}#function chatRoomServerDemoV2 end	
7214
		}#function chatRoomServerDemoV2 end
7215
	
7215
 
7216
	/*
7216
	/*
7217
	#函式說明:
7217
	#函式說明:
7218
	#擴充過的Ratchet聊天室伺服端範例第三版,port為8080.
7218
	#擴充過的Ratchet聊天室伺服端範例第三版,port為8080.
7219
	#回傳結果:
7219
	#回傳結果:
7220
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7220
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7221
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
7221
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
7222
	#$result["function"],當前執行的函式名稱.		
7222
	#$result["function"],當前執行的函式名稱.
7223
	#必填參數:
7223
	#必填參數:
7224
	#$conf["fileArgu"],__FILE__的內容.
7224
	#$conf["fileArgu"],__FILE__的內容.
7225
	$conf["fileArgu"]=__FILE__;
7225
	$conf["fileArgu"]=__FILE__;
7226
	#可省略參數:
7226
	#可省略參數:
7227
	#無.
7227
	#無.
Line 7239... Line 7239...
7239
		#取得當前執行的函數名稱
7239
		#取得當前執行的函數名稱
7240
		$result["function"]=__FUNCTION__;
7240
		$result["function"]=__FUNCTION__;
7241
 
7241
 
7242
		#如果沒有參數
7242
		#如果沒有參數
7243
		if(func_num_args()==0){
7243
		if(func_num_args()==0){
7244
			
7244
 
7245
			#設置執行失敗
7245
			#設置執行失敗
7246
			$result["status"]="false";
7246
			$result["status"]="false";
7247
			
7247
 
7248
			#設置執行錯誤訊息
7248
			#設置執行錯誤訊息
7249
			$result["error"]="函數".$result["function"]."需要參數";
7249
			$result["error"]="函數".$result["function"]."需要參數";
7250
			
7250
 
7251
			#回傳結果
7251
			#回傳結果
7252
			return $result;
7252
			return $result;
7253
			
7253
 
7254
			}#if end
7254
			}#if end
7255
 
7255
 
7256
		#涵式說明:
7256
		#涵式說明:
7257
		#判斷當前環境為web還是cmd
7257
		#判斷當前環境為web還是cmd
7258
		#回傳結果:
7258
		#回傳結果:
7259
		#$result,"web"或"cmd"
7259
		#$result,"web"或"cmd"
7260
		if(csInformation::getEnv()==="web"){
7260
		if(csInformation::getEnv()==="web"){
7261
			
7261
 
7262
			#設置執行失敗
7262
			#設置執行失敗
7263
			$result["status"]="false";
7263
			$result["status"]="false";
7264
			
7264
 
7265
			#設置執行錯誤訊息
7265
			#設置執行錯誤訊息
7266
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
7266
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
7267
			
7267
 
7268
			#回傳結果
7268
			#回傳結果
7269
			return $result;
7269
			return $result;
7270
			
7270
 
7271
			}#if end
7271
			}#if end
7272
 
7272
 
7273
		#取得參數
7273
		#取得參數
7274
		$result["argu"]=$conf;
7274
		$result["argu"]=$conf;
7275
 
7275
 
7276
		#如果 $conf 不為陣列
7276
		#如果 $conf 不為陣列
7277
		if(gettype($conf)!=="array"){
7277
		if(gettype($conf)!=="array"){
7278
			
7278
 
7279
			#設置執行失敗
7279
			#設置執行失敗
7280
			$result["status"]="false";
7280
			$result["status"]="false";
7281
			
7281
 
7282
			#設置執行錯誤訊息
7282
			#設置執行錯誤訊息
7283
			$result["error"][]="\$conf變數須為陣列形態";
7283
			$result["error"][]="\$conf變數須為陣列形態";
7284
			
7284
 
7285
			#如果傳入的參數為 null
7285
			#如果傳入的參數為 null
7286
			if($conf===null){
7286
			if($conf===null){
7287
				
7287
 
7288
				#設置執行錯誤訊息
7288
				#設置執行錯誤訊息
7289
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
7289
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
7290
				
7290
 
7291
				}#if end
7291
				}#if end
7292
 
7292
 
7293
			#回傳結果
7293
			#回傳結果
7294
			return $result;
7294
			return $result;
7295
			
7295
 
7296
			}#if end
7296
			}#if end
7297
 
7297
 
7298
		#檢查參數
7298
		#檢查參數
7299
		#函式說明:
7299
		#函式說明:
7300
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
7300
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
Line 7327... Line 7327...
7327
		#$conf["canBeEmpty"]=array();
7327
		#$conf["canBeEmpty"]=array();
7328
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
7328
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
7329
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array();
7329
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array();
7330
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
7330
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
7331
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("entry","connVar","disReg");
7331
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("entry","connVar","disReg");
7332
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double"); 
7332
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
7333
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string","string");
7333
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string","string");
7334
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
7334
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
7335
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("false","conn","false");
7335
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("false","conn","false");
7336
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
7336
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
7337
		#$conf["arrayCountEqualCheck"][]=array();
7337
		#$conf["arrayCountEqualCheck"][]=array();
Line 7339... Line 7339...
7339
		#array_keys=>http://php.net/manual/en/function.array-keys.php
7339
		#array_keys=>http://php.net/manual/en/function.array-keys.php
7340
		#建議:
7340
		#建議:
7341
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
7341
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
7342
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
7342
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
7343
		unset($conf["variableCheck::checkArguments"]);
7343
		unset($conf["variableCheck::checkArguments"]);
7344
		
7344
 
7345
		#如果檢查參數失敗
7345
		#如果檢查參數失敗
7346
		if($checkArguments["status"]==="false"){
7346
		if($checkArguments["status"]==="false"){
7347
			
7347
 
7348
			#設置執行失敗
7348
			#設置執行失敗
7349
			$result["status"]="false";
7349
			$result["status"]="false";
7350
		
7350
 
7351
			#設置執行錯誤訊息
7351
			#設置執行錯誤訊息
7352
			$result["error"]=$checkArguments;
7352
			$result["error"]=$checkArguments;
7353
			
7353
 
7354
			#回傳結果
7354
			#回傳結果
7355
			return $result;
7355
			return $result;
7356
			
7356
 
7357
			}#if end
7357
			}#if end
7358
			
7358
 
7359
		#如果參數檢查不通過
7359
		#如果參數檢查不通過
7360
		if($checkArguments["passed"]==="false"){
7360
		if($checkArguments["passed"]==="false"){
7361
			
7361
 
7362
			#設置執行失敗
7362
			#設置執行失敗
7363
			$result["status"]="false";
7363
			$result["status"]="false";
7364
		
7364
 
7365
			#設置執行錯誤訊息
7365
			#設置執行錯誤訊息
7366
			$result["error"]=$checkArguments;
7366
			$result["error"]=$checkArguments;
7367
			
7367
 
7368
			#回傳結果
7368
			#回傳結果
7369
			return $result;
7369
			return $result;
7370
			
7370
 
7371
			}#if end
7371
			}#if end
7372
 
7372
 
7373
		#物件化ChatV3類別
7373
		#物件化ChatV3類別
7374
		$Chat3=new ChatV3();
7374
		$Chat3=new ChatV3();
7375
		
7375
 
7376
		#設置屬性 fileArgu
7376
		#設置屬性 fileArgu
7377
		$Chat3::$fileArgu=$conf["fileArgu"];
7377
		$Chat3::$fileArgu=$conf["fileArgu"];
7378
 
7378
 
7379
		#建立 Ratchet 提供的聊天室範例(適用於telnet)
7379
		#建立 Ratchet 提供的聊天室範例(適用於telnet)
7380
		#It stores all the established connections, mediates data sent between each client and our Chat application, and catches errors. 
7380
		#It stores all the established connections, mediates data sent between each client and our Chat application, and catches errors.
7381
		#we tell the server to enter an event loop, listening for any incoming requests on port 8080. 
7381
		#we tell the server to enter an event loop, listening for any incoming requests on port 8080.
7382
		#$server = IoServer::factory(new Chat(),8080);
7382
		#$server = IoServer::factory(new Chat(),8080);
7383
		
7383
 
7384
		#建立 Ratchet 提供的聊天室範例(適用於用webServer)
7384
		#建立 Ratchet 提供的聊天室範例(適用於用webServer)
7385
		$server = IoServer::factory(
7385
		$server = IoServer::factory(
7386
			new HttpServer(
7386
			new HttpServer(
7387
				new WsServer(
7387
				new WsServer(
7388
					$Chat3
7388
					$Chat3
Line 7391... Line 7391...
7391
			8080
7391
			8080
7392
			);
7392
			);
7393
 
7393
 
7394
		#執行 server 端程式
7394
		#執行 server 端程式
7395
		$server->run();
7395
		$server->run();
7396
			
7396
 
7397
		#設置執行正常
7397
		#設置執行正常
7398
		$result["status"]="true";
7398
		$result["status"]="true";
7399
			
7399
 
7400
		#回傳結果	
7400
		#回傳結果
7401
		return $result;
7401
		return $result;
7402
		
7402
 
7403
		}#function chatRoomServerDemoV3 end	
7403
		}#function chatRoomServerDemoV3 end
7404
	
7404
 
7405
	/*
7405
	/*
7406
	#函式說明:
7406
	#函式說明:
7407
	#擴充過的Ratchet聊天室伺服端soldier範例.提供cmd:跟sql:指令支援.
7407
	#擴充過的Ratchet聊天室伺服端soldier範例.提供cmd:跟sql:指令支援.
7408
	#回傳結果:
7408
	#回傳結果:
7409
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7409
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7410
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
7410
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
7411
	#$result["function"],當前執行的函式名稱.		
7411
	#$result["function"],當前執行的函式名稱.
7412
	#必填參數:
7412
	#必填參數:
7413
	#$conf["fileArgu"],__FILE__的內容.
7413
	#$conf["fileArgu"],__FILE__的內容.
7414
	$conf["fileArgu"]=__FILE__;
7414
	$conf["fileArgu"]=__FILE__;
7415
	#可省略參數:
7415
	#可省略參數:
7416
	#$conf["port"],要 listen 的 port
7416
	#$conf["port"],要 listen 的 port
Line 7429... Line 7429...
7429
		#取得當前執行的函數名稱
7429
		#取得當前執行的函數名稱
7430
		$result["function"]=__FUNCTION__;
7430
		$result["function"]=__FUNCTION__;
7431
 
7431
 
7432
		#如果沒有參數
7432
		#如果沒有參數
7433
		if(func_num_args()==0){
7433
		if(func_num_args()==0){
7434
			
7434
 
7435
			#設置執行失敗
7435
			#設置執行失敗
7436
			$result["status"]="false";
7436
			$result["status"]="false";
7437
			
7437
 
7438
			#設置執行錯誤訊息
7438
			#設置執行錯誤訊息
7439
			$result["error"]="函數".$result["function"]."需要參數";
7439
			$result["error"]="函數".$result["function"]."需要參數";
7440
			
7440
 
7441
			#回傳結果
7441
			#回傳結果
7442
			return $result;
7442
			return $result;
7443
			
7443
 
7444
			}#if end
7444
			}#if end
7445
 
7445
 
7446
		#涵式說明:
7446
		#涵式說明:
7447
		#判斷當前環境為web還是cmd
7447
		#判斷當前環境為web還是cmd
7448
		#回傳結果:
7448
		#回傳結果:
7449
		#$result,"web"或"cmd"
7449
		#$result,"web"或"cmd"
7450
		if(csInformation::getEnv()==="web"){
7450
		if(csInformation::getEnv()==="web"){
7451
			
7451
 
7452
			#設置執行失敗
7452
			#設置執行失敗
7453
			$result["status"]="false";
7453
			$result["status"]="false";
7454
			
7454
 
7455
			#設置執行錯誤訊息
7455
			#設置執行錯誤訊息
7456
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
7456
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
7457
			
7457
 
7458
			#回傳結果
7458
			#回傳結果
7459
			return $result;
7459
			return $result;
7460
			
7460
 
7461
			}#if end
7461
			}#if end
7462
 
7462
 
7463
		#取得參數
7463
		#取得參數
7464
		$result["argu"]=$conf;
7464
		$result["argu"]=$conf;
7465
 
7465
 
7466
		#如果 $conf 不為陣列
7466
		#如果 $conf 不為陣列
7467
		if(gettype($conf)!=="array"){
7467
		if(gettype($conf)!=="array"){
7468
			
7468
 
7469
			#設置執行失敗
7469
			#設置執行失敗
7470
			$result["status"]="false";
7470
			$result["status"]="false";
7471
			
7471
 
7472
			#設置執行錯誤訊息
7472
			#設置執行錯誤訊息
7473
			$result["error"][]="\$conf變數須為陣列形態";
7473
			$result["error"][]="\$conf變數須為陣列形態";
7474
			
7474
 
7475
			#如果傳入的參數為 null
7475
			#如果傳入的參數為 null
7476
			if($conf===null){
7476
			if($conf===null){
7477
				
7477
 
7478
				#設置執行錯誤訊息
7478
				#設置執行錯誤訊息
7479
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
7479
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
7480
				
7480
 
7481
				}#if end
7481
				}#if end
7482
 
7482
 
7483
			#回傳結果
7483
			#回傳結果
7484
			return $result;
7484
			return $result;
7485
			
7485
 
7486
			}#if end
7486
			}#if end
7487
 
7487
 
7488
		#檢查參數
7488
		#檢查參數
7489
		#函式說明:
7489
		#函式說明:
7490
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
7490
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
Line 7517... Line 7517...
7517
		#$conf["canBeEmpty"]=array();
7517
		#$conf["canBeEmpty"]=array();
7518
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
7518
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
7519
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array();
7519
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array();
7520
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
7520
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
7521
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("port");
7521
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("port");
7522
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double"); 
7522
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
7523
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("integer");
7523
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("integer");
7524
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
7524
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
7525
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(8080);
7525
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(8080);
7526
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
7526
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
7527
		#$conf["arrayCountEqualCheck"][]=array();
7527
		#$conf["arrayCountEqualCheck"][]=array();
Line 7529... Line 7529...
7529
		#array_keys=>http://php.net/manual/en/function.array-keys.php
7529
		#array_keys=>http://php.net/manual/en/function.array-keys.php
7530
		#建議:
7530
		#建議:
7531
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
7531
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
7532
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
7532
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
7533
		unset($conf["variableCheck::checkArguments"]);
7533
		unset($conf["variableCheck::checkArguments"]);
7534
		
7534
 
7535
		#如果檢查參數失敗
7535
		#如果檢查參數失敗
7536
		if($checkArguments["status"]==="false"){
7536
		if($checkArguments["status"]==="false"){
7537
			
7537
 
7538
			#設置執行失敗
7538
			#設置執行失敗
7539
			$result["status"]="false";
7539
			$result["status"]="false";
7540
		
7540
 
7541
			#設置執行錯誤訊息
7541
			#設置執行錯誤訊息
7542
			$result["error"]=$checkArguments;
7542
			$result["error"]=$checkArguments;
7543
			
7543
 
7544
			#回傳結果
7544
			#回傳結果
7545
			return $result;
7545
			return $result;
7546
			
7546
 
7547
			}#if end
7547
			}#if end
7548
			
7548
 
7549
		#如果參數檢查不通過
7549
		#如果參數檢查不通過
7550
		if($checkArguments["passed"]==="false"){
7550
		if($checkArguments["passed"]==="false"){
7551
			
7551
 
7552
			#設置執行失敗
7552
			#設置執行失敗
7553
			$result["status"]="false";
7553
			$result["status"]="false";
7554
		
7554
 
7555
			#設置執行錯誤訊息
7555
			#設置執行錯誤訊息
7556
			$result["error"]=$checkArguments;
7556
			$result["error"]=$checkArguments;
7557
			
7557
 
7558
			#回傳結果
7558
			#回傳結果
7559
			return $result;
7559
			return $result;
7560
			
7560
 
7561
			}#if end
7561
			}#if end
7562
 
7562
 
7563
		#建立 Ratchet 提供的聊天室範例(適用於telnet)
7563
		#建立 Ratchet 提供的聊天室範例(適用於telnet)
7564
		#It stores all the established connections, mediates data sent between each client and our Chat application, and catches errors. 
7564
		#It stores all the established connections, mediates data sent between each client and our Chat application, and catches errors.
7565
		#we tell the server to enter an event loop, listening for any incoming requests on port 8080. 
7565
		#we tell the server to enter an event loop, listening for any incoming requests on port 8080.
7566
		#$server = IoServer::factory(new Chat(),8080);
7566
		#$server = IoServer::factory(new Chat(),8080);
7567
				
7567
 
7568
		#建立 soldier 物件
7568
		#建立 soldier 物件
7569
		$soldier=new soldier();
7569
		$soldier=new soldier();
7570
		
7570
 
7571
		#設置fileArgu屬性
7571
		#設置fileArgu屬性
7572
		$soldier::$fileArgu=$conf["fileArgu"];
7572
		$soldier::$fileArgu=$conf["fileArgu"];
7573
		
7573
 
7574
		#建立 Ratchet 提供的聊天室範例(適用於用webServer)
7574
		#建立 Ratchet 提供的聊天室範例(適用於用webServer)
7575
		$server = IoServer::factory(
7575
		$server = IoServer::factory(
7576
			new HttpServer(
7576
			new HttpServer(
7577
				new WsServer(
7577
				new WsServer(
7578
					$soldier
7578
					$soldier
Line 7581... Line 7581...
7581
			$conf["port"]
7581
			$conf["port"]
7582
			);
7582
			);
7583
 
7583
 
7584
		#執行 server 端程式
7584
		#執行 server 端程式
7585
		$server->run();
7585
		$server->run();
7586
			
7586
 
7587
		#設置執行正常
7587
		#設置執行正常
7588
		$result["status"]="true";
7588
		$result["status"]="true";
7589
			
7589
 
7590
		#回傳結果	
7590
		#回傳結果
7591
		return $result;
7591
		return $result;
7592
		
7592
 
7593
		}#function soldierServerDemo end	
7593
		}#function soldierServerDemo end
7594
		
7594
 
7595
	/*
7595
	/*
7596
	#函式說明:
7596
	#函式說明:
7597
	#擴充過的Ratchet聊天室用戶端js範例
7597
	#擴充過的Ratchet聊天室用戶端js範例
7598
	#回傳結果:
7598
	#回傳結果:
7599
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7599
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7600
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
7600
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
7601
	#$result["function"],當前執行的函式名稱.		
7601
	#$result["function"],當前執行的函式名稱.
7602
	#$result["content"],html.
7602
	#$result["content"],html.
7603
	#必填參數:
7603
	#必填參數:
7604
	#無.
7604
	#無.
7605
	#可省略參數:
7605
	#可省略參數:
7606
	#無.
7606
	#無.
7607
	#參考資料:
7607
	#參考資料:
7608
	#Ratchet官網=>http://socketo.me/
7608
	#Ratchet官網=>http://socketo.me/
7609
	#用apache的proxy來判斷為哪個服務,導到對應的port裡面=>https://groups.google.com/forum/#!topic/ratchet-php/dj-PgPPO_J0
7609
	#用apache的proxy來判斷為哪個服務,導到對應的port裡面=>https://groups.google.com/forum/#!topic/ratchet-php/dj-PgPPO_J0
7610
	#聊天室範例=>http://socketo.me/docs/hello-world
7610
	#聊天室範例=>http://socketo.me/docs/hello-world
7611
	#備註:
7611
	#備註:
7612
	#僅適用於https網頁透過wss連線到web socket server.	
7612
	#僅適用於https網頁透過wss連線到web socket server.
7613
	*/
7613
	*/
7614
	public static function chatRoomClientDemo(){
7614
	public static function chatRoomClientDemo(){
7615
		
7615
 
7616
		#初始化要回傳的結果
7616
		#初始化要回傳的結果
7617
		$result=array();
7617
		$result=array();
7618
 
7618
 
7619
		#初始化儲存html的變數
7619
		#初始化儲存html的變數
7620
		$result["content"]="";
7620
		$result["content"]="";
7621
 
7621
 
7622
		#取得當前執行的函數名稱
7622
		#取得當前執行的函數名稱
7623
		$result["function"]=__FUNCTION__;
7623
		$result["function"]=__FUNCTION__;
7624
		
7624
 
7625
		#初始化帳號密碼
7625
		#初始化帳號密碼
7626
		$ac="";
7626
		$ac="";
7627
		$pw="";
7627
		$pw="";
7628
		
7628
 
7629
		#如果存在 $_SESSION["account"]
7629
		#如果存在 $_SESSION["account"]
7630
		if(isset($_SESSION["account"])){
7630
		if(isset($_SESSION["account"])){
7631
			
7631
 
7632
			#取得帳戶
7632
			#取得帳戶
7633
			$ac=$_SESSION["account"];
7633
			$ac=$_SESSION["account"];
7634
			
7634
 
7635
			}#if end
7635
			}#if end
7636
		
7636
 
7637
		#如果 this->$passwordCol 不為 ""
7637
		#如果 this->$passwordCol 不為 ""
7638
		if(Chat::$passwordCol!==""){
7638
		if(Chat::$passwordCol!==""){
7639
			
7639
 
7640
			#如果存在 $_SESSION["password"]
7640
			#如果存在 $_SESSION["password"]
7641
			if(isset($_SESSION["password"])){
7641
			if(isset($_SESSION["password"])){
7642
				
7642
 
7643
				#取得帳戶
7643
				#取得帳戶
7644
				$pw=$_SESSION["password"];
7644
				$pw=$_SESSION["password"];
7645
				
7645
 
7646
				}#if end
7646
				}#if end
7647
			
7647
 
7648
			}#if end
7648
			}#if end
7649
		
7649
 
7650
		#取得 server 的 ip
7650
		#取得 server 的 ip
7651
		#涵式說明:
7651
		#涵式說明:
7652
		#取得伺服器的IP,並回傳。
7652
		#取得伺服器的IP,並回傳。
7653
		#回傳的結果:
7653
		#回傳的結果:
7654
		#$result,伺服端的IP
7654
		#$result,伺服端的IP
7655
		#備註:
7655
		#備註:
7656
		#伺服端必須被 localhost 以外的網址連結才會出現正確的IP
7656
		#伺服端必須被 localhost 以外的網址連結才會出現正確的IP
7657
		$getServerIP=csInformation::getServerIP();
7657
		$getServerIP=csInformation::getServerIP();
7658
		
7658
 
7659
		#設置用戶端連線到webSocket的範例
7659
		#設置用戶端連線到webSocket的範例
7660
		$script="<script>
7660
		$script="<script>
7661
		
7661
 
7662
			//取得帳號密碼
7662
			//取得帳號密碼
7663
			ac='".$ac."';
7663
			ac='".$ac."';
7664
			pw='".$pw."';
7664
			pw='".$pw."';
7665
		
7665
 
7666
			//設置目前尚為連線到 webSocket Server
7666
			//設置目前尚為連線到 webSocket Server
7667
			connected='false';
7667
			connected='false';
7668
			
7668
 
7669
			//設置儲存應該要斷線的webSocket,避免太多連線在伺服器.
7669
			//設置儲存應該要斷線的webSocket,避免太多連線在伺服器.
7670
			//wbClosed='';
7670
			//wbClosed='';
7671
			
7671
 
7672
			//設置儲存webSocket連線的變數
7672
			//設置儲存webSocket連線的變數
7673
			wb='';
7673
			wb='';
7674
			
7674
 
7675
			//連線到 web socket
7675
			//連線到 web socket
7676
			connWebSock();
7676
			connWebSock();
7677
		
7677
 
7678
			//連線到 web socket
7678
			//連線到 web socket
7679
			function connWebSock()
7679
			function connWebSock()
7680
			{
7680
			{
7681
				//建立 web socket 連線到 wss://localhost/wss/chatDemo
7681
				//建立 web socket 連線到 wss://localhost/wss/chatDemo
7682
				var conn = new WebSocket('wss://".$getServerIP."/wss/chatDemo');
7682
				var conn = new WebSocket('wss://".$getServerIP."/wss/chatDemo');
7683
				
7683
 
7684
				//當連線成功後
7684
				//當連線成功後
7685
				conn.onopen = function(e){
7685
				conn.onopen = function(e){
7686
				
7686
 
7687
					//印出連線成功訊息到console
7687
					//印出連線成功訊息到console
7688
					console.log(\"Connection established!\");
7688
					console.log(\"Connection established!\");
7689
					
7689
 
7690
					//設置已經連上 webSocket server
7690
					//設置已經連上 webSocket server
7691
					connected='true';
7691
					connected='true';
7692
					
7692
 
7693
					//另存 webSocket 物件
7693
					//另存 webSocket 物件
7694
					wb=conn;
7694
					wb=conn;
7695
					
7695
 
7696
					/*
7696
					/*
7697
					//如果存在前一個連線物件
7697
					//如果存在前一個連線物件
7698
					if(wbClosed!==''){
7698
					if(wbClosed!==''){
7699
					
7699
 
7700
						//提示關閉前個連線
7700
						//提示關閉前個連線
7701
						console.log(\"close last connection!\");
7701
						console.log(\"close last connection!\");
7702
					
7702
 
7703
						//關閉之前的連線
7703
						//關閉之前的連線
7704
						wbClosed.close();
7704
						wbClosed.close();
7705
						
7705
 
7706
						//清空
7706
						//清空
7707
						wbClosed='';
7707
						wbClosed='';
7708
					
7708
 
7709
						}
7709
						}
7710
					*/
7710
					*/
7711
											
7711
 
7712
					}
7712
					}
7713
 
7713
 
7714
				//當有收到訊息時
7714
				//當有收到訊息時
7715
				conn.onmessage = function(e){
7715
				conn.onmessage = function(e){
7716
					
7716
 
7717
					//將訊息顯現在console
7717
					//將訊息顯現在console
7718
					console.log(e.data);
7718
					console.log(e.data);
7719
					
7719
 
7720
					//將訊息寫到文字方框裡面
7720
					//將訊息寫到文字方框裡面
7721
					$('#history').val(e.data+'\\r\\n'+$('#history').val());
7721
					$('#history').val(e.data+'\\r\\n'+$('#history').val());
7722
					
7722
 
7723
					//解析json字串
7723
					//解析json字串
7724
					data=JSON.parse(e.data);				
7724
					data=JSON.parse(e.data);
7725
					
7725
 
7726
					//如果有查到帳號
7726
					//如果有查到帳號
7727
					if(ac!==''){
7727
					if(ac!==''){
7728
					
7728
 
7729
						//如果要求輸入帳號
7729
						//如果要求輸入帳號
7730
						if(data==='Please input your account! ex:account:ws1'){
7730
						if(data==='Please input your account! ex:account:ws1'){
7731
						
7731
 
7732
							//傳送帳號
7732
							//傳送帳號
7733
							conn.send('account:'+ac);
7733
							conn.send('account:'+ac);
7734
						
7734
 
7735
							}
7735
							}
7736
					
7736
 
7737
						//如果有查到密碼
7737
						//如果有查到密碼
7738
						if(pw!==''){
7738
						if(pw!==''){
7739
						
7739
 
7740
							//如果要求輸入密碼	
7740
							//如果要求輸入密碼
7741
							if(data==='Please input your password! ex:password:ws1'){
7741
							if(data==='Please input your password! ex:password:ws1'){
7742
							
7742
 
7743
								//傳送密碼
7743
								//傳送密碼
7744
								conn.send('password:'+pw);
7744
								conn.send('password:'+pw);
7745
							
7745
 
7746
								}
7746
								}
7747
								
7747
 
7748
							}
7748
							}
7749
							
7749
 
7750
						}
7750
						}
7751
						
7751
 
7752
					}
7752
					}
7753
					
7753
 
7754
				//當連線斷開後
7754
				//當連線斷開後
7755
				conn.onclose = function(e){
7755
				conn.onclose = function(e){
7756
				
7756
 
7757
					//印出連線斷開的訊息到console
7757
					//印出連線斷開的訊息到console
7758
					//console.log(\"Connection end!\");
7758
					//console.log(\"Connection end!\");
7759
				
7759
 
7760
					//設置未連上 webSocket server
7760
					//設置未連上 webSocket server
7761
					connected='false';
7761
					connected='false';
7762
					
7762
 
7763
					/*
7763
					/*
7764
					//如果有之前的連線物件
7764
					//如果有之前的連線物件
7765
					if(wb!==''){
7765
					if(wb!==''){
7766
					
7766
 
7767
						//儲存應該要斷線的webSocket,避免太多連線在伺服器.
7767
						//儲存應該要斷線的webSocket,避免太多連線在伺服器.
7768
						wbClosed=wb;
7768
						wbClosed=wb;
7769
					
7769
 
7770
						}
7770
						}
7771
					*/
7771
					*/
7772
					
7772
 
7773
					//清空 webSocket 物件
7773
					//清空 webSocket 物件
7774
					wb='';
7774
					wb='';
7775
				
7775
 
7776
					//指定1秒刷新一次
7776
					//指定1秒刷新一次
7777
					setTimeout('connWebSock()',1000); 
7777
					setTimeout('connWebSock()',1000);
7778
				
7778
 
7779
					}
7779
					}
-
 
7780
 
7780
					
7781
				}
7781
				}							
-
 
7782
			
7782
 
7783
			//當按下送出按鈕
7783
			//當按下送出按鈕
7784
			$('#send').on('click',function(){
7784
			$('#send').on('click',function(){
7785
			
7785
 
7786
				//如果已經連到 webSocket server 了
7786
				//如果已經連到 webSocket server 了
7787
				if(connected==='true'){
7787
				if(connected==='true'){
7788
				
7788
 
7789
					//傳送訊息
7789
					//傳送訊息
7790
					wb.send($('#input').val());
7790
					wb.send($('#input').val());
7791
					
7791
 
7792
					//清空訊息輸入欄
7792
					//清空訊息輸入欄
7793
					$('#input').val('');
7793
					$('#input').val('');
7794
				
7794
 
7795
					}
7795
					}
7796
			
7796
 
7797
				});
7797
				});
7798
				
7798
 
7799
			//重新整理頁面
7799
			//重新整理頁面
7800
			function myrefresh(){
7800
			function myrefresh(){
7801
				
7801
 
7802
				//重新整理頁面
7802
				//重新整理頁面
7803
				window.location.reload();
7803
				window.location.reload();
7804
			
7804
 
7805
				}
7805
				}
7806
			
7806
 
7807
			</script>";
7807
			</script>";
7808
		
7808
 
7809
		#設置存放歷史訊息的方框
7809
		#設置存放歷史訊息的方框
7810
		#涵式說明:
7810
		#涵式說明:
7811
		#可以輸入文字的區塊
7811
		#可以輸入文字的區塊
7812
		#回傳結果:
7812
		#回傳結果:
7813
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7813
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7814
		#$result["function"],當前執行的函數.
7814
		#$result["function"],當前執行的函數.
7815
		#$result["error"],錯誤訊息陣列.
7815
		#$result["error"],錯誤訊息陣列.
7816
		#$result["content"],按鈕語法. 		
7816
		#$result["content"],按鈕語法.
7817
		#必填的參數:
7817
		#必填的參數:
7818
		$conf["form::inputTextArea"]["name"]="history";#爲文字輸入框的名稱,供接收端辨識用。
7818
		$conf["form::inputTextArea"]["name"]="history";#爲文字輸入框的名稱,供接收端辨識用。
7819
		$conf["form::inputTextArea"]["readOnly"]="true";#爲是否要爲唯讀,如果爲"true",則爲唯讀。反之則爲"false"。
7819
		$conf["form::inputTextArea"]["readOnly"]="true";#爲是否要爲唯讀,如果爲"true",則爲唯讀。反之則爲"false"。
7820
		#可省略的參數:
7820
		#可省略的參數:
7821
		#$conf["id"],字串,文字區塊的id,供javaScript呼叫用.
7821
		#$conf["id"],字串,文字區塊的id,供javaScript呼叫用.
Line 7846... Line 7846...
7846
		#$conf["trEnd"]="true";#爲是否要以</tr>結尾,"true"表示"是",也可以看作該列結束,預設為"false".
7846
		#$conf["trEnd"]="true";#爲是否要以</tr>結尾,"true"表示"是",也可以看作該列結束,預設為"false".
7847
		#參考資料來源:
7847
		#參考資料來源:
7848
		#input=>http://www.w3schools.com/tags/tag_input.asp
7848
		#input=>http://www.w3schools.com/tags/tag_input.asp
7849
		$inputTextArea=form::inputTextArea($conf["form::inputTextArea"]);
7849
		$inputTextArea=form::inputTextArea($conf["form::inputTextArea"]);
7850
		unset($conf["form::inputTextArea"]);
7850
		unset($conf["form::inputTextArea"]);
7851
			
7851
 
7852
		#如果建立文字方框失敗
7852
		#如果建立文字方框失敗
7853
		if($inputTextArea["status"]==="false"){
7853
		if($inputTextArea["status"]==="false"){
7854
			
7854
 
7855
			#設置執行失敗
7855
			#設置執行失敗
7856
			$result["status"]="false";
7856
			$result["status"]="false";
7857
			
7857
 
7858
			#設置執行錯誤訊息
7858
			#設置執行錯誤訊息
7859
			$result["error"]=$inputTextArea;
7859
			$result["error"]=$inputTextArea;
7860
			
7860
 
7861
			#回傳結果
7861
			#回傳結果
7862
			return $result;
7862
			return $result;
7863
			
7863
 
7864
			}#if end	
7864
			}#if end
7865
			
7865
 
7866
		#串連文字訊息方框
7866
		#串連文字訊息方框
7867
		$result["content"]=$result["content"].$inputTextArea["content"];
7867
		$result["content"]=$result["content"].$inputTextArea["content"];
7868
			
7868
 
7869
		#設置填寫要傳送的訊息輸入方框
7869
		#設置填寫要傳送的訊息輸入方框
7870
		#涵式說明:
7870
		#涵式說明:
7871
		#可以輸入文字的表單
7871
		#可以輸入文字的表單
7872
		#回傳的結果:
7872
		#回傳的結果:
7873
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7873
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7874
		#$result["function"],當前執行的函數.
7874
		#$result["function"],當前執行的函數.
7875
		#$result["error"],錯誤訊息陣列.
7875
		#$result["error"],錯誤訊息陣列.
7876
		#$result["content"],按鈕語法. 
7876
		#$result["content"],按鈕語法.
7877
		#必填的參數
7877
		#必填的參數
7878
		$conf["form::inputText"]["name"]="data";#爲該文字輸入框的名稱,用於讓接收頁面讀取的名稱。
7878
		$conf["form::inputText"]["name"]="data";#爲該文字輸入框的名稱,用於讓接收頁面讀取的名稱。
7879
		$conf["form::inputText"]["readOnly"]="false";#爲該文字框是否可以填寫資料,若要只能觀看不能填寫,那麼就必須將其值設爲"true",反之要設爲"false"
7879
		$conf["form::inputText"]["readOnly"]="false";#爲該文字框是否可以填寫資料,若要只能觀看不能填寫,那麼就必須將其值設爲"true",反之要設爲"false"
7880
		#可省略的參數:
7880
		#可省略的參數:
7881
		#$conf["width"]="";#爲文字框的外觀長度,預設為"100%".
7881
		#$conf["width"]="";#爲文字框的外觀長度,預設為"100%".
7882
		#$conf["maxInputLength"]="";#爲可輸入的最大位元長度,預設不限制。
7882
		#$conf["maxInputLength"]="";#爲可輸入的最大位元長度,預設不限制。
7883
		#$conf["autoFocus"]="true";#是否為將填寫的指標移到該表單,"true"代表要,預設為"false".
7883
		#$conf["autoFocus"]="true";#是否為將填寫的指標移到該表單,"true"代表要,預設為"false".
7884
		#$conf["value"]="";#爲該文字框預設要顯示的文字,預設爲""。	
7884
		#$conf["value"]="";#爲該文字框預設要顯示的文字,預設爲""。
7885
		#$conf["class"]="";#爲要套用的css樣式,若省略,則會套用預設的 "__inputTextCssStyle" 樣式,其屬性爲 "width:100%","font-size:30px"
7885
		#$conf["class"]="";#爲要套用的css樣式,若省略,則會套用預設的 "__inputTextCssStyle" 樣式,其屬性爲 "width:100%","font-size:30px"
7886
		#$conf["jsActivitor"]="";#爲觸發js的條件,可以是"onChange"(已改變內容時)、"onClick"(按下按鈕時)、"onkeyup"(當鍵盤按下放開後)、"onmouseover"(當滑鼠移過去的時候)...,須搭配$conf["jsSubmitActionTarget"]參數。
7886
		#$conf["jsActivitor"]="";#爲觸發js的條件,可以是"onChange"(已改變內容時)、"onClick"(按下按鈕時)、"onkeyup"(當鍵盤按下放開後)、"onmouseover"(當滑鼠移過去的時候)...,須搭配$conf["jsSubmitActionTarget"]參數。
7887
		#$conf["jsAction"]="";#爲該js是要做什麼,可以是"document.testForm.submit()"(傳送名爲testForm的表單內容)...,須搭配$conf["jsActivitor"]參數。		
7887
		#$conf["jsAction"]="";#爲該js是要做什麼,可以是"document.testForm.submit()"(傳送名爲testForm的表單內容)...,須搭配$conf["jsActivitor"]參數。
7888
		#$conf["trStart"]="true";#爲是否要以<tr>開頭,"true"表示"是"。也可以看作新的一列開始,預設為"false".
7888
		#$conf["trStart"]="true";#爲是否要以<tr>開頭,"true"表示"是"。也可以看作新的一列開始,預設為"false".
7889
		#$conf["tdStart"]="true";#爲是否要以<td>開頭,"true"表示"是"。也可以看成列裏面的元素開始,預設為"false".
7889
		#$conf["tdStart"]="true";#爲是否要以<td>開頭,"true"表示"是"。也可以看成列裏面的元素開始,預設為"false".
7890
		#$conf["formStart"]="true";#爲是否要以<form>開頭,"true"表示"是",也可以看成表單的開始,預設為"false".
7890
		#$conf["formStart"]="true";#爲是否要以<form>開頭,"true"表示"是",也可以看成表單的開始,預設為"false".
7891
		#$conf["formAction"]="";#表單遞交的目的地,若$conf["formStart"]為"true",則該參數不能省略.
7891
		#$conf["formAction"]="";#表單遞交的目的地,若$conf["formStart"]為"true",則該參數不能省略.
7892
		#$conf["formName"]="";#爲該表單的名稱
7892
		#$conf["formName"]="";#爲該表單的名稱
Line 7914... Line 7914...
7914
		$conf["form::inputText"]["id"]="input";
7914
		$conf["form::inputText"]["id"]="input";
7915
		#參考資料來源:
7915
		#參考資料來源:
7916
		#input=>http://www.w3schools.com/tags/tag_input.asp
7916
		#input=>http://www.w3schools.com/tags/tag_input.asp
7917
		$inputText=form::inputText($conf["form::inputText"]);
7917
		$inputText=form::inputText($conf["form::inputText"]);
7918
		unset($conf["form::inputText"]);
7918
		unset($conf["form::inputText"]);
7919
		
7919
 
7920
		#如果輸入文字框失敗
7920
		#如果輸入文字框失敗
7921
		if($inputText["status"]==="false"){
7921
		if($inputText["status"]==="false"){
7922
			
7922
 
7923
			#設置執行失敗
7923
			#設置執行失敗
7924
			$result["status"]="false";
7924
			$result["status"]="false";
7925
			
7925
 
7926
			#設置執行錯誤訊息
7926
			#設置執行錯誤訊息
7927
			$result["error"]=$inputText;
7927
			$result["error"]=$inputText;
7928
			
7928
 
7929
			#回傳結果
7929
			#回傳結果
7930
			return $result;
7930
			return $result;
7931
			
7931
 
7932
			}#if end	
7932
			}#if end
7933
		
7933
 
7934
		#文字輸入框
7934
		#文字輸入框
7935
		$result["content"]=$result["content"].$inputText["content"];
7935
		$result["content"]=$result["content"].$inputText["content"];
7936
		
7936
 
7937
		#設置觸發的按鈕
7937
		#設置觸發的按鈕
7938
		#函式說明:
7938
		#函式說明:
7939
		#放置按鈕
7939
		#放置按鈕
7940
		#回傳結果:
7940
		#回傳結果:
7941
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7941
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7942
		#$result["function"],當前執行的函數.
7942
		#$result["function"],當前執行的函數.
7943
		#$result["error"],錯誤訊息陣列.
7943
		#$result["error"],錯誤訊息陣列.
7944
		#$result["content"],按鈕語法. 
7944
		#$result["content"],按鈕語法.
7945
		#必要的參數:
7945
		#必要的參數:
7946
		$conf["form::button"]["buttonDisplayValue"]="Send";#爲按鈕上顯示的文字。
7946
		$conf["form::button"]["buttonDisplayValue"]="Send";#爲按鈕上顯示的文字。
7947
		#可省略的參數:
7947
		#可省略的參數:
7948
		#$conf["buttonStyleName"]="";#可省略,爲按鈕所要使用的css樣式類別名稱,預設的css樣式爲 __simpleButtonLinkDefaultButtonCssStyle 。
7948
		#$conf["buttonStyleName"]="";#可省略,爲按鈕所要使用的css樣式類別名稱,預設的css樣式爲 __simpleButtonLinkDefaultButtonCssStyle 。
7949
			#其屬性爲   "width","height","font-size","text-align"
7949
			#其屬性爲   "width","height","font-size","text-align"
Line 7973... Line 7973...
7973
		$conf["form::button"]["buttonId"]="send";
7973
		$conf["form::button"]["buttonId"]="send";
7974
		#參考資料來源:
7974
		#參考資料來源:
7975
		#http://stackoverflow.com/questions/3014649/how-to-disable-html-button-using-javascript
7975
		#http://stackoverflow.com/questions/3014649/how-to-disable-html-button-using-javascript
7976
		$button=form::button($conf["form::button"]);
7976
		$button=form::button($conf["form::button"]);
7977
		unset($conf["form::button"]);
7977
		unset($conf["form::button"]);
7978
			
7978
 
7979
		#如果建立按鈕失敗
7979
		#如果建立按鈕失敗
7980
		if($button["status"]==="false"){
7980
		if($button["status"]==="false"){
7981
			
7981
 
7982
			#設置執行失敗
7982
			#設置執行失敗
7983
			$result["status"]="false";
7983
			$result["status"]="false";
7984
			
7984
 
7985
			#設置執行錯誤訊息
7985
			#設置執行錯誤訊息
7986
			$result["error"]=$button;
7986
			$result["error"]=$button;
7987
			
7987
 
7988
			#回傳結果
7988
			#回傳結果
7989
			return $result;
7989
			return $result;
7990
			
7990
 
7991
			}#if end	
7991
			}#if end
7992
		
7992
 
7993
		#建立跳到別的頁面的連結
7993
		#建立跳到別的頁面的連結
7994
		#涵式說明:
7994
		#涵式說明:
7995
		#放置超鏈結
7995
		#放置超鏈結
7996
		#回傳的結果:
7996
		#回傳的結果:
7997
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7997
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
7998
		#$result["error"],錯誤訊息
7998
		#$result["error"],錯誤訊息
7999
		#$result["content"],語法
7999
		#$result["content"],語法
8000
		#$result["function"],當前執行的函數名稱
8000
		#$result["function"],當前執行的函數名稱
8001
		#必填的參數:
8001
		#必填的參數:
8002
		#$conf["position"],字串,爲要連結到的位置,若留空,將回自動變成"#",則可以搭配ajax傳值.	
8002
		#$conf["position"],字串,爲要連結到的位置,若留空,將回自動變成"#",則可以搭配ajax傳值.
8003
		$conf["link::show_link"]["position"]="webSocketT/hangFewSecs.php";
8003
		$conf["link::show_link"]["position"]="webSocketT/hangFewSecs.php";
8004
		#$conf["linkName",字串,爲連結的顯示名稱
8004
		#$conf["linkName",字串,爲連結的顯示名稱
8005
		$conf["link::show_link"]["linkName"]="到別的頁面轉轉";
8005
		$conf["link::show_link"]["linkName"]="到別的頁面轉轉";
8006
		#可省略的參數:
8006
		#可省略的參數:
8007
		#$conf["method"],字串,爲點選連結後,新畫面要如何呈現,可省略預設爲"_self",可用的選項有 _top(覆蓋目前的視窗來顯現新內容) _parent _self _blank(跳新視窗)
8007
		#$conf["method"],字串,爲點選連結後,新畫面要如何呈現,可省略預設爲"_self",可用的選項有 _top(覆蓋目前的視窗來顯現新內容) _parent _self _blank(跳新視窗)
8008
		#$conf["method"]=""; 
8008
		#$conf["method"]="";
8009
		#$conf["class"],字串,爲要套用的css超連節樣式,可省略.
8009
		#$conf["class"],字串,爲要套用的css超連節樣式,可省略.
8010
		#$conf["class"]="";
8010
		#$conf["class"]="";
8011
		#$conf["id"],字串,超連結的id.
8011
		#$conf["id"],字串,超連結的id.
8012
		#$conf["id"]="";
8012
		#$conf["id"]="";
8013
		#$conf["no_outline"],字串,是否要取消連結的框線,"false為不取消,"true"代表要取消,預設為"false".
8013
		#$conf["no_outline"],字串,是否要取消連結的框線,"false為不取消,"true"代表要取消,預設為"false".
8014
		#$conf["no_outline"]="";
8014
		#$conf["no_outline"]="";
8015
		$show_link=link::show_link($conf["link::show_link"]);
8015
		$show_link=link::show_link($conf["link::show_link"]);
8016
		unset($conf["link::show_link"]);
8016
		unset($conf["link::show_link"]);
8017
		
8017
 
8018
		#串接超連結
8018
		#串接超連結
8019
		$result["content"]=$result["content"].$show_link["content"];
8019
		$result["content"]=$result["content"].$show_link["content"];
8020
			
8020
 
8021
		#設置用戶端的html與js語法
8021
		#設置用戶端的html與js語法
8022
		$result["content"]=$result["content"].$button["content"].$script;
8022
		$result["content"]=$result["content"].$button["content"].$script;
8023
			
8023
 
8024
		#設置執行正常
8024
		#設置執行正常
8025
		$result["status"]="true";
8025
		$result["status"]="true";
8026
		
8026
 
8027
		#回傳結果
8027
		#回傳結果
8028
		return $result;
8028
		return $result;
8029
		
8029
 
8030
		}#function chatRoomClientDemo end
8030
		}#function chatRoomClientDemo end
8031
		
8031
 
8032
	/*
8032
	/*
8033
	#函式說明:
8033
	#函式說明:
8034
	#擴充過的Ratchet聊天室用戶端js範例第二版
8034
	#擴充過的Ratchet聊天室用戶端js範例第二版
8035
	#回傳結果:
8035
	#回傳結果:
8036
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
8036
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
8037
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
8037
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
8038
	#$result["function"],當前執行的函式名稱.		
8038
	#$result["function"],當前執行的函式名稱.
8039
	#$result["content"],html.
8039
	#$result["content"],html.
8040
	#必填參數:
8040
	#必填參數:
8041
	#無.
8041
	#無.
8042
	#可省略參數:
8042
	#可省略參數:
8043
	#無.
8043
	#無.
8044
	#參考資料:
8044
	#參考資料:
8045
	#Ratchet官網=>http://socketo.me/
8045
	#Ratchet官網=>http://socketo.me/
8046
	#用apache的proxy來判斷為哪個服務,導到對應的port裡面=>https://groups.google.com/forum/#!topic/ratchet-php/dj-PgPPO_J0
8046
	#用apache的proxy來判斷為哪個服務,導到對應的port裡面=>https://groups.google.com/forum/#!topic/ratchet-php/dj-PgPPO_J0
8047
	#聊天室範例=>http://socketo.me/docs/hello-world
8047
	#聊天室範例=>http://socketo.me/docs/hello-world
8048
	#備註:
8048
	#備註:
8049
	#僅適用於https網頁透過wss連線到web socket server.	
8049
	#僅適用於https網頁透過wss連線到web socket server.
8050
	*/
8050
	*/
8051
	public static function chatRoomClientDemoV2(){
8051
	public static function chatRoomClientDemoV2(){
8052
		
8052
 
8053
		#初始化要回傳的結果
8053
		#初始化要回傳的結果
8054
		$result=array();
8054
		$result=array();
8055
 
8055
 
8056
		#初始化儲存html的變數
8056
		#初始化儲存html的變數
8057
		$result["content"]="";
8057
		$result["content"]="";
8058
 
8058
 
8059
		#取得當前執行的函數名稱
8059
		#取得當前執行的函數名稱
8060
		$result["function"]=__FUNCTION__;
8060
		$result["function"]=__FUNCTION__;
8061
		
8061
 
8062
		#初始化帳號密碼
8062
		#初始化帳號密碼
8063
		$ac="";
8063
		$ac="";
8064
		$pw="";
8064
		$pw="";
8065
		
8065
 
8066
		#如果存在 $_SESSION["account"]
8066
		#如果存在 $_SESSION["account"]
8067
		if(isset($_SESSION["account"])){
8067
		if(isset($_SESSION["account"])){
8068
			
8068
 
8069
			#取得帳戶
8069
			#取得帳戶
8070
			$ac=$_SESSION["account"];
8070
			$ac=$_SESSION["account"];
8071
			
8071
 
8072
			}#if end
8072
			}#if end
8073
		
8073
 
8074
		#如果 this->$passwordCol 不為 ""
8074
		#如果 this->$passwordCol 不為 ""
8075
		if(Chat::$passwordCol!==""){
8075
		if(Chat::$passwordCol!==""){
8076
			
8076
 
8077
			#如果存在 $_SESSION["password"]
8077
			#如果存在 $_SESSION["password"]
8078
			if(isset($_SESSION["password"])){
8078
			if(isset($_SESSION["password"])){
8079
				
8079
 
8080
				#取得帳戶
8080
				#取得帳戶
8081
				$pw=$_SESSION["password"];
8081
				$pw=$_SESSION["password"];
8082
				
8082
 
8083
				}#if end
8083
				}#if end
8084
			
8084
 
8085
			}#if end
8085
			}#if end
8086
		
8086
 
8087
		#取得 server 的 ip
8087
		#取得 server 的 ip
8088
		#涵式說明:
8088
		#涵式說明:
8089
		#取得伺服器的IP,並回傳。
8089
		#取得伺服器的IP,並回傳。
8090
		#回傳的結果:
8090
		#回傳的結果:
8091
		#$result,伺服端的IP
8091
		#$result,伺服端的IP
8092
		#備註:
8092
		#備註:
8093
		#伺服端必須被 localhost 以外的網址連結才會出現正確的IP
8093
		#伺服端必須被 localhost 以外的網址連結才會出現正確的IP
8094
		$getServerIP=csInformation::getServerIP();
8094
		$getServerIP=csInformation::getServerIP();
8095
		
8095
 
8096
		#設置用戶端連線到webSocket的範例
8096
		#設置用戶端連線到webSocket的範例
8097
		$script="<script>
8097
		$script="<script>
8098
		
8098
 
8099
			//取得帳號密碼
8099
			//取得帳號密碼
8100
			ac='".$ac."';
8100
			ac='".$ac."';
8101
			pw='".$pw."';
8101
			pw='".$pw."';
8102
		
8102
 
8103
			//設置目前尚為連線到 webSocket Server
8103
			//設置目前尚為連線到 webSocket Server
8104
			connected='false';
8104
			connected='false';
8105
			
8105
 
8106
			//設置儲存應該要斷線的webSocket,避免太多連線在伺服器.
8106
			//設置儲存應該要斷線的webSocket,避免太多連線在伺服器.
8107
			wbClosed='';
8107
			wbClosed='';
8108
			
8108
 
8109
			//設置儲存webSocket連線的變數
8109
			//設置儲存webSocket連線的變數
8110
			wb='';
8110
			wb='';
8111
			
8111
 
8112
			//設置儲存msgId的變數
8112
			//設置儲存msgId的變數
8113
			msgId='';
8113
			msgId='';
8114
			
8114
 
8115
			//連線到 web socket
8115
			//連線到 web socket
8116
			connWebSock();
8116
			connWebSock();
8117
		
8117
 
8118
			//連線到 web socket
8118
			//連線到 web socket
8119
			function connWebSock()
8119
			function connWebSock()
8120
			{
8120
			{
8121
				//建立 web socket 連線到 wss://localhost/wss/chatDemo
8121
				//建立 web socket 連線到 wss://localhost/wss/chatDemo
8122
				var conn = new WebSocket('wss://".$getServerIP."/wss/chatDemo');
8122
				var conn = new WebSocket('wss://".$getServerIP."/wss/chatDemo');
8123
				
8123
 
8124
				//當連線成功後
8124
				//當連線成功後
8125
				conn.onopen = function(e){
8125
				conn.onopen = function(e){
8126
				
8126
 
8127
					//印出連線成功訊息到console
8127
					//印出連線成功訊息到console
8128
					console.log(\"Connection established!\");
8128
					console.log(\"Connection established!\");
8129
					
8129
 
8130
					//設置已經連上 webSocket server
8130
					//設置已經連上 webSocket server
8131
					connected='true';
8131
					connected='true';
8132
					
8132
 
8133
					//另存 webSocket 物件
8133
					//另存 webSocket 物件
8134
					wb=conn;
8134
					wb=conn;
8135
					
8135
 
8136
					//如果存在前一個連線物件
8136
					//如果存在前一個連線物件
8137
					if(wbClosed!==''){
8137
					if(wbClosed!==''){
8138
					
8138
 
8139
						//提示關閉前個連線
8139
						//提示關閉前個連線
8140
						console.log(\"close last connection!\");
8140
						console.log(\"close last connection!\");
8141
					
8141
 
8142
						//關閉之前的連線
8142
						//關閉之前的連線
8143
						wbClosed.close();
8143
						wbClosed.close();
8144
						
8144
 
8145
						//清空
8145
						//清空
8146
						wbClosed='';
8146
						wbClosed='';
8147
					
8147
 
8148
						}
8148
						}
8149
											
8149
 
8150
					}
8150
					}
8151
 
8151
 
8152
				//當有收到訊息時
8152
				//當有收到訊息時
8153
				conn.onmessage = function(e){
8153
				conn.onmessage = function(e){
8154
					
8154
 
8155
					//將訊息顯現在console
8155
					//將訊息顯現在console
8156
					console.log(e.data);
8156
					console.log(e.data);
8157
					
8157
 
8158
					//將訊息寫到文字方框裡面
8158
					//將訊息寫到文字方框裡面
8159
					$('#history').val(e.data+'\\r\\n'+$('#history').val());
8159
					$('#history').val(e.data+'\\r\\n'+$('#history').val());
8160
					
8160
 
8161
					//解析json字串
8161
					//解析json字串
8162
					data=JSON.parse(e.data);
8162
					data=JSON.parse(e.data);
8163
					
8163
 
8164
					//如果要要求login的訊息
8164
					//如果要要求login的訊息
8165
					if(data.type==='login'){
8165
					if(data.type==='login'){
8166
					
8166
 
8167
						//如果有查到帳號
8167
						//如果有查到帳號
8168
						if(ac!==''){
8168
						if(ac!==''){
8169
						
8169
 
8170
							//如果要求輸入帳號
8170
							//如果要求輸入帳號
8171
							if(data.data==='Please input your account! ex:account:ws1'){
8171
							if(data.data==='Please input your account! ex:account:ws1'){
8172
							
8172
 
8173
								//傳送帳號
8173
								//傳送帳號
8174
								conn.send('account:'+ac);
8174
								conn.send('account:'+ac);
8175
							
8175
 
8176
								}
8176
								}
8177
						
8177
 
8178
							//如果有查到密碼
8178
							//如果有查到密碼
8179
							if(pw!==''){
8179
							if(pw!==''){
8180
							
8180
 
8181
								//如果要求輸入密碼	
8181
								//如果要求輸入密碼
8182
								if(data.data==='Please input your password! ex:password:ws1'){
8182
								if(data.data==='Please input your password! ex:password:ws1'){
8183
								
8183
 
8184
									//傳送密碼
8184
									//傳送密碼
8185
									conn.send('password:'+pw);
8185
									conn.send('password:'+pw);
8186
								
8186
 
8187
									}
8187
									}
8188
									
8188
 
8189
								}
8189
								}
8190
							
8190
 
8191
							//如果存在既有的msgId,且剛登入成功
8191
							//如果存在既有的msgId,且剛登入成功
8192
							if(msgId!=='' && data.data==='Login successfully!'){
8192
							if(msgId!=='' && data.data==='Login successfully!'){
8193
							
8193
 
8194
								//設置既有的msgId
8194
								//設置既有的msgId
8195
								conn.send('msgId:'+msgId);
8195
								conn.send('msgId:'+msgId);
8196
							
8196
 
8197
								}
8197
								}
8198
							
8198
 
8199
							//反之如果登入成功
8199
							//反之如果登入成功
8200
							else if(data.data==='Login successfully!'){
8200
							else if(data.data==='Login successfully!'){
8201
							
8201
 
8202
								//查詢自己的msgId
8202
								//查詢自己的msgId
8203
								conn.send('msgId?');
8203
								conn.send('msgId?');
8204
							
8204
 
8205
								}
8205
								}
8206
															
8206
 
8207
							}
8207
							}
8208
						
8208
 
8209
						}
8209
						}
8210
					
8210
 
8211
					//如果收到的是 'msgId?'
8211
					//如果收到的是 'msgId?'
8212
					else if(data.type==='msgId?'){
8212
					else if(data.type==='msgId?'){
8213
					
8213
 
8214
						//設置自己的msgId
8214
						//設置自己的msgId
8215
						msgId=data.data;
8215
						msgId=data.data;
8216
					
8216
 
8217
						}
8217
						}
8218
						
8218
 
8219
					//如果收到的是 'msg' 類型
8219
					//如果收到的是 'msg' 類型
8220
					else if(data.type==='msg'){
8220
					else if(data.type==='msg'){
8221
					
8221
 
8222
						//回傳有收到訊息的確認
8222
						//回傳有收到訊息的確認
8223
						conn.send('mIndex:'+data.index);
8223
						conn.send('mIndex:'+data.index);
8224
									
8224
 
8225
						}
8225
						}
8226
						
8226
 
8227
					//如果收到的是 'status' 類型	
8227
					//如果收到的是 'status' 類型
8228
					else if(data.type==='status'){
8228
					else if(data.type==='status'){
8229
					
8229
 
8230
						//如果內容為設置msgId失敗
8230
						//如果內容為設置msgId失敗
8231
						if(data.data==='set msgId failed'){
8231
						if(data.data==='set msgId failed'){
8232
						
8232
 
8233
							//查詢自己的msgId
8233
							//查詢自己的msgId
8234
							conn.send('msgId?');
8234
							conn.send('msgId?');
8235
						
8235
 
8236
							}
8236
							}
8237
					
8237
 
8238
						}
8238
						}
8239
											
8239
 
8240
					}
8240
					}
8241
					
8241
 
8242
				//當連線斷開後
8242
				//當連線斷開後
8243
				conn.onclose = function(e){
8243
				conn.onclose = function(e){
8244
									
8244
 
8245
					//設置未連上 webSocket server
8245
					//設置未連上 webSocket server
8246
					connected='false';
8246
					connected='false';
8247
					
8247
 
8248
					//如果有之前的連線物件
8248
					//如果有之前的連線物件
8249
					if(wb!==''){
8249
					if(wb!==''){
8250
					
8250
 
8251
						//儲存應該要斷線的webSocket,避免太多連線在伺服器.
8251
						//儲存應該要斷線的webSocket,避免太多連線在伺服器.
8252
						wbClosed=wb;
8252
						wbClosed=wb;
8253
					
8253
 
8254
						}
8254
						}
8255
										
8255
 
8256
					//清空 webSocket 物件
8256
					//清空 webSocket 物件
8257
					wb='';
8257
					wb='';
8258
				
8258
 
8259
					//指定1秒刷新一次
8259
					//指定1秒刷新一次
8260
					setTimeout('connWebSock()',1000); 
8260
					setTimeout('connWebSock()',1000);
8261
				
8261
 
8262
					}
8262
					}
-
 
8263
 
8263
					
8264
				}
8264
				}							
-
 
8265
			
8265
 
8266
			//當按下送出按鈕
8266
			//當按下送出按鈕
8267
			$('#send').on('click',function(){
8267
			$('#send').on('click',function(){
8268
			
8268
 
8269
				//如果已經連到 webSocket server 了
8269
				//如果已經連到 webSocket server 了
8270
				if(connected==='true'){
8270
				if(connected==='true'){
8271
				
8271
 
8272
					//傳送訊息
8272
					//傳送訊息
8273
					wb.send($('#input').val());
8273
					wb.send($('#input').val());
8274
					
8274
 
8275
					//清空訊息輸入欄
8275
					//清空訊息輸入欄
8276
					$('#input').val('');
8276
					$('#input').val('');
8277
				
8277
 
8278
					}
8278
					}
8279
			
8279
 
8280
				});
8280
				});
8281
				
8281
 
8282
			//重新整理頁面
8282
			//重新整理頁面
8283
			function myrefresh(){
8283
			function myrefresh(){
8284
				
8284
 
8285
				//重新整理頁面
8285
				//重新整理頁面
8286
				window.location.reload();
8286
				window.location.reload();
8287
			
8287
 
8288
				}
8288
				}
8289
			
8289
 
8290
			</script>";
8290
			</script>";
8291
		
8291
 
8292
		#設置存放歷史訊息的方框
8292
		#設置存放歷史訊息的方框
8293
		#涵式說明:
8293
		#涵式說明:
8294
		#可以輸入文字的區塊
8294
		#可以輸入文字的區塊
8295
		#回傳結果:
8295
		#回傳結果:
8296
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
8296
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
8297
		#$result["function"],當前執行的函數.
8297
		#$result["function"],當前執行的函數.
8298
		#$result["error"],錯誤訊息陣列.
8298
		#$result["error"],錯誤訊息陣列.
8299
		#$result["content"],按鈕語法. 		
8299
		#$result["content"],按鈕語法.
8300
		#必填的參數:
8300
		#必填的參數:
8301
		$conf["form::inputTextArea"]["name"]="history";#爲文字輸入框的名稱,供接收端辨識用。
8301
		$conf["form::inputTextArea"]["name"]="history";#爲文字輸入框的名稱,供接收端辨識用。
8302
		$conf["form::inputTextArea"]["readOnly"]="true";#爲是否要爲唯讀,如果爲"true",則爲唯讀。反之則爲"false"。
8302
		$conf["form::inputTextArea"]["readOnly"]="true";#爲是否要爲唯讀,如果爲"true",則爲唯讀。反之則爲"false"。
8303
		#可省略的參數:
8303
		#可省略的參數:
8304
		#$conf["id"],字串,文字區塊的id,供javaScript呼叫用.
8304
		#$conf["id"],字串,文字區塊的id,供javaScript呼叫用.
Line 8329... Line 8329...
8329
		#$conf["trEnd"]="true";#爲是否要以</tr>結尾,"true"表示"是",也可以看作該列結束,預設為"false".
8329
		#$conf["trEnd"]="true";#爲是否要以</tr>結尾,"true"表示"是",也可以看作該列結束,預設為"false".
8330
		#參考資料來源:
8330
		#參考資料來源:
8331
		#input=>http://www.w3schools.com/tags/tag_input.asp
8331
		#input=>http://www.w3schools.com/tags/tag_input.asp
8332
		$inputTextArea=form::inputTextArea($conf["form::inputTextArea"]);
8332
		$inputTextArea=form::inputTextArea($conf["form::inputTextArea"]);
8333
		unset($conf["form::inputTextArea"]);
8333
		unset($conf["form::inputTextArea"]);
8334
			
8334
 
8335
		#如果建立文字方框失敗
8335
		#如果建立文字方框失敗
8336
		if($inputTextArea["status"]==="false"){
8336
		if($inputTextArea["status"]==="false"){
8337
			
8337
 
8338
			#設置執行失敗
8338
			#設置執行失敗
8339
			$result["status"]="false";
8339
			$result["status"]="false";
8340
			
8340
 
8341
			#設置執行錯誤訊息
8341
			#設置執行錯誤訊息
8342
			$result["error"]=$inputTextArea;
8342
			$result["error"]=$inputTextArea;
8343
			
8343
 
8344
			#回傳結果
8344
			#回傳結果
8345
			return $result;
8345
			return $result;
8346
			
8346
 
8347
			}#if end	
8347
			}#if end
8348
			
8348
 
8349
		#串連文字訊息方框
8349
		#串連文字訊息方框
8350
		$result["content"]=$result["content"].$inputTextArea["content"];
8350
		$result["content"]=$result["content"].$inputTextArea["content"];
8351
			
8351
 
8352
		#設置填寫要傳送的訊息輸入方框
8352
		#設置填寫要傳送的訊息輸入方框
8353
		#涵式說明:
8353
		#涵式說明:
8354
		#可以輸入文字的表單
8354
		#可以輸入文字的表單
8355
		#回傳的結果:
8355
		#回傳的結果:
8356
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
8356
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
8357
		#$result["function"],當前執行的函數.
8357
		#$result["function"],當前執行的函數.
8358
		#$result["error"],錯誤訊息陣列.
8358
		#$result["error"],錯誤訊息陣列.
8359
		#$result["content"],按鈕語法. 
8359
		#$result["content"],按鈕語法.
8360
		#必填的參數
8360
		#必填的參數
8361
		$conf["form::inputText"]["name"]="data";#爲該文字輸入框的名稱,用於讓接收頁面讀取的名稱。
8361
		$conf["form::inputText"]["name"]="data";#爲該文字輸入框的名稱,用於讓接收頁面讀取的名稱。
8362
		$conf["form::inputText"]["readOnly"]="false";#爲該文字框是否可以填寫資料,若要只能觀看不能填寫,那麼就必須將其值設爲"true",反之要設爲"false"
8362
		$conf["form::inputText"]["readOnly"]="false";#爲該文字框是否可以填寫資料,若要只能觀看不能填寫,那麼就必須將其值設爲"true",反之要設爲"false"
8363
		#可省略的參數:
8363
		#可省略的參數:
8364
		#$conf["width"]="";#爲文字框的外觀長度,預設為"100%".
8364
		#$conf["width"]="";#爲文字框的外觀長度,預設為"100%".
8365
		#$conf["maxInputLength"]="";#爲可輸入的最大位元長度,預設不限制。
8365
		#$conf["maxInputLength"]="";#爲可輸入的最大位元長度,預設不限制。
8366
		#$conf["autoFocus"]="true";#是否為將填寫的指標移到該表單,"true"代表要,預設為"false".
8366
		#$conf["autoFocus"]="true";#是否為將填寫的指標移到該表單,"true"代表要,預設為"false".
8367
		#$conf["value"]="";#爲該文字框預設要顯示的文字,預設爲""。	
8367
		#$conf["value"]="";#爲該文字框預設要顯示的文字,預設爲""。
8368
		#$conf["class"]="";#爲要套用的css樣式,若省略,則會套用預設的 "__inputTextCssStyle" 樣式,其屬性爲 "width:100%","font-size:30px"
8368
		#$conf["class"]="";#爲要套用的css樣式,若省略,則會套用預設的 "__inputTextCssStyle" 樣式,其屬性爲 "width:100%","font-size:30px"
8369
		#$conf["jsActivitor"]="";#爲觸發js的條件,可以是"onChange"(已改變內容時)、"onClick"(按下按鈕時)、"onkeyup"(當鍵盤按下放開後)、"onmouseover"(當滑鼠移過去的時候)...,須搭配$conf["jsSubmitActionTarget"]參數。
8369
		#$conf["jsActivitor"]="";#爲觸發js的條件,可以是"onChange"(已改變內容時)、"onClick"(按下按鈕時)、"onkeyup"(當鍵盤按下放開後)、"onmouseover"(當滑鼠移過去的時候)...,須搭配$conf["jsSubmitActionTarget"]參數。
8370
		#$conf["jsAction"]="";#爲該js是要做什麼,可以是"document.testForm.submit()"(傳送名爲testForm的表單內容)...,須搭配$conf["jsActivitor"]參數。		
8370
		#$conf["jsAction"]="";#爲該js是要做什麼,可以是"document.testForm.submit()"(傳送名爲testForm的表單內容)...,須搭配$conf["jsActivitor"]參數。
8371
		#$conf["trStart"]="true";#爲是否要以<tr>開頭,"true"表示"是"。也可以看作新的一列開始,預設為"false".
8371
		#$conf["trStart"]="true";#爲是否要以<tr>開頭,"true"表示"是"。也可以看作新的一列開始,預設為"false".
8372
		#$conf["tdStart"]="true";#爲是否要以<td>開頭,"true"表示"是"。也可以看成列裏面的元素開始,預設為"false".
8372
		#$conf["tdStart"]="true";#爲是否要以<td>開頭,"true"表示"是"。也可以看成列裏面的元素開始,預設為"false".
8373
		#$conf["formStart"]="true";#爲是否要以<form>開頭,"true"表示"是",也可以看成表單的開始,預設為"false".
8373
		#$conf["formStart"]="true";#爲是否要以<form>開頭,"true"表示"是",也可以看成表單的開始,預設為"false".
8374
		#$conf["formAction"]="";#表單遞交的目的地,若$conf["formStart"]為"true",則該參數不能省略.
8374
		#$conf["formAction"]="";#表單遞交的目的地,若$conf["formStart"]為"true",則該參數不能省略.
8375
		#$conf["formName"]="";#爲該表單的名稱
8375
		#$conf["formName"]="";#爲該表單的名稱
Line 8397... Line 8397...
8397
		$conf["form::inputText"]["id"]="input";
8397
		$conf["form::inputText"]["id"]="input";
8398
		#參考資料來源:
8398
		#參考資料來源:
8399
		#input=>http://www.w3schools.com/tags/tag_input.asp
8399
		#input=>http://www.w3schools.com/tags/tag_input.asp
8400
		$inputText=form::inputText($conf["form::inputText"]);
8400
		$inputText=form::inputText($conf["form::inputText"]);
8401
		unset($conf["form::inputText"]);
8401
		unset($conf["form::inputText"]);
8402
		
8402
 
8403
		#如果輸入文字框失敗
8403
		#如果輸入文字框失敗
8404
		if($inputText["status"]==="false"){
8404
		if($inputText["status"]==="false"){
8405
			
8405
 
8406
			#設置執行失敗
8406
			#設置執行失敗
8407
			$result["status"]="false";
8407
			$result["status"]="false";
8408
			
8408
 
8409
			#設置執行錯誤訊息
8409
			#設置執行錯誤訊息
8410
			$result["error"]=$inputText;
8410
			$result["error"]=$inputText;
8411
			
8411
 
8412
			#回傳結果
8412
			#回傳結果
8413
			return $result;
8413
			return $result;
8414
			
8414
 
8415
			}#if end	
8415
			}#if end
8416
		
8416
 
8417
		#文字輸入框
8417
		#文字輸入框
8418
		$result["content"]=$result["content"].$inputText["content"];
8418
		$result["content"]=$result["content"].$inputText["content"];
8419
		
8419
 
8420
		#設置觸發的按鈕
8420
		#設置觸發的按鈕
8421
		#函式說明:
8421
		#函式說明:
8422
		#放置按鈕
8422
		#放置按鈕
8423
		#回傳結果:
8423
		#回傳結果:
8424
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
8424
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
8425
		#$result["function"],當前執行的函數.
8425
		#$result["function"],當前執行的函數.
8426
		#$result["error"],錯誤訊息陣列.
8426
		#$result["error"],錯誤訊息陣列.
8427
		#$result["content"],按鈕語法. 
8427
		#$result["content"],按鈕語法.
8428
		#必要的參數:
8428
		#必要的參數:
8429
		$conf["form::button"]["buttonDisplayValue"]="Send";#爲按鈕上顯示的文字。
8429
		$conf["form::button"]["buttonDisplayValue"]="Send";#爲按鈕上顯示的文字。
8430
		#可省略的參數:
8430
		#可省略的參數:
8431
		#$conf["buttonStyleName"]="";#可省略,爲按鈕所要使用的css樣式類別名稱,預設的css樣式爲 __simpleButtonLinkDefaultButtonCssStyle 。
8431
		#$conf["buttonStyleName"]="";#可省略,爲按鈕所要使用的css樣式類別名稱,預設的css樣式爲 __simpleButtonLinkDefaultButtonCssStyle 。
8432
			#其屬性爲   "width","height","font-size","text-align"
8432
			#其屬性爲   "width","height","font-size","text-align"
Line 8456... Line 8456...
8456
		$conf["form::button"]["buttonId"]="send";
8456
		$conf["form::button"]["buttonId"]="send";
8457
		#參考資料來源:
8457
		#參考資料來源:
8458
		#http://stackoverflow.com/questions/3014649/how-to-disable-html-button-using-javascript
8458
		#http://stackoverflow.com/questions/3014649/how-to-disable-html-button-using-javascript
8459
		$button=form::button($conf["form::button"]);
8459
		$button=form::button($conf["form::button"]);
8460
		unset($conf["form::button"]);
8460
		unset($conf["form::button"]);
8461
			
8461
 
8462
		#如果建立按鈕失敗
8462
		#如果建立按鈕失敗
8463
		if($button["status"]==="false"){
8463
		if($button["status"]==="false"){
8464
			
8464
 
8465
			#設置執行失敗
8465
			#設置執行失敗
8466
			$result["status"]="false";
8466
			$result["status"]="false";
8467
			
8467
 
8468
			#設置執行錯誤訊息
8468
			#設置執行錯誤訊息
8469
			$result["error"]=$button;
8469
			$result["error"]=$button;
8470
			
8470
 
8471
			#回傳結果
8471
			#回傳結果
8472
			return $result;
8472
			return $result;
8473
			
8473
 
8474
			}#if end	
8474
			}#if end
8475
		
8475
 
8476
		#建立跳到別的頁面的連結
8476
		#建立跳到別的頁面的連結
8477
		#涵式說明:
8477
		#涵式說明:
8478
		#放置超鏈結
8478
		#放置超鏈結
8479
		#回傳的結果:
8479
		#回傳的結果:
8480
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
8480
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
8481
		#$result["error"],錯誤訊息
8481
		#$result["error"],錯誤訊息
8482
		#$result["content"],語法
8482
		#$result["content"],語法
8483
		#$result["function"],當前執行的函數名稱
8483
		#$result["function"],當前執行的函數名稱
8484
		#必填的參數:
8484
		#必填的參數:
8485
		#$conf["position"],字串,爲要連結到的位置,若留空,將回自動變成"#",則可以搭配ajax傳值.	
8485
		#$conf["position"],字串,爲要連結到的位置,若留空,將回自動變成"#",則可以搭配ajax傳值.
8486
		$conf["link::show_link"]["position"]="webSocketT/hangFewSecs.php";
8486
		$conf["link::show_link"]["position"]="webSocketT/hangFewSecs.php";
8487
		#$conf["linkName",字串,爲連結的顯示名稱
8487
		#$conf["linkName",字串,爲連結的顯示名稱
8488
		$conf["link::show_link"]["linkName"]="到別的頁面轉轉";
8488
		$conf["link::show_link"]["linkName"]="到別的頁面轉轉";
8489
		#可省略的參數:
8489
		#可省略的參數:
8490
		#$conf["method"],字串,爲點選連結後,新畫面要如何呈現,可省略預設爲"_self",可用的選項有 _top(覆蓋目前的視窗來顯現新內容) _parent _self _blank(跳新視窗)
8490
		#$conf["method"],字串,爲點選連結後,新畫面要如何呈現,可省略預設爲"_self",可用的選項有 _top(覆蓋目前的視窗來顯現新內容) _parent _self _blank(跳新視窗)
8491
		#$conf["method"]=""; 
8491
		#$conf["method"]="";
8492
		#$conf["class"],字串,爲要套用的css超連節樣式,可省略.
8492
		#$conf["class"],字串,爲要套用的css超連節樣式,可省略.
8493
		#$conf["class"]="";
8493
		#$conf["class"]="";
8494
		#$conf["id"],字串,超連結的id.
8494
		#$conf["id"],字串,超連結的id.
8495
		#$conf["id"]="";
8495
		#$conf["id"]="";
8496
		#$conf["no_outline"],字串,是否要取消連結的框線,"false為不取消,"true"代表要取消,預設為"false".
8496
		#$conf["no_outline"],字串,是否要取消連結的框線,"false為不取消,"true"代表要取消,預設為"false".
8497
		#$conf["no_outline"]="";
8497
		#$conf["no_outline"]="";
8498
		$show_link=link::show_link($conf["link::show_link"]);
8498
		$show_link=link::show_link($conf["link::show_link"]);
8499
		unset($conf["link::show_link"]);
8499
		unset($conf["link::show_link"]);
8500
		
8500
 
8501
		#串接超連結
8501
		#串接超連結
8502
		$result["content"]=$result["content"].$show_link["content"];
8502
		$result["content"]=$result["content"].$show_link["content"];
8503
			
8503
 
8504
		#設置用戶端的html與js語法
8504
		#設置用戶端的html與js語法
8505
		$result["content"]=$result["content"].$button["content"].$script;
8505
		$result["content"]=$result["content"].$button["content"].$script;
8506
			
8506
 
8507
		#設置執行正常
8507
		#設置執行正常
8508
		$result["status"]="true";
8508
		$result["status"]="true";
8509
		
8509
 
8510
		#回傳結果
8510
		#回傳結果
8511
		return $result;
8511
		return $result;
8512
		
8512
 
8513
		}#function chatRoomClientDemoV2 end	
8513
		}#function chatRoomClientDemoV2 end
8514
	
8514
 
8515
	/*
8515
	/*
8516
	#函式說明:
8516
	#函式說明:
8517
	#擴充過的Ratchet聊天室用戶端js範例第三版,擁有在伺服器上執行指令的功能.
8517
	#擴充過的Ratchet聊天室用戶端js範例第三版,擁有在伺服器上執行指令的功能.
8518
	#回傳結果:
8518
	#回傳結果:
8519
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
8519
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
8520
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
8520
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
8521
	#$result["function"],當前執行的函式名稱.		
8521
	#$result["function"],當前執行的函式名稱.
8522
	#$result["content"],html.
8522
	#$result["content"],html.
8523
	#必填參數:
8523
	#必填參數:
8524
	#無.
8524
	#無.
8525
	#可省略參數:
8525
	#可省略參數:
8526
	#無.
8526
	#無.
8527
	#參考資料:
8527
	#參考資料:
8528
	#Ratchet官網=>http://socketo.me/
8528
	#Ratchet官網=>http://socketo.me/
8529
	#用apache的proxy來判斷為哪個服務,導到對應的port裡面=>https://groups.google.com/forum/#!topic/ratchet-php/dj-PgPPO_J0
8529
	#用apache的proxy來判斷為哪個服務,導到對應的port裡面=>https://groups.google.com/forum/#!topic/ratchet-php/dj-PgPPO_J0
8530
	#聊天室範例=>http://socketo.me/docs/hello-world
8530
	#聊天室範例=>http://socketo.me/docs/hello-world
8531
	#備註:
8531
	#備註:
8532
	#僅適用於https網頁透過wss連線到web socket server.	
8532
	#僅適用於https網頁透過wss連線到web socket server.
8533
	*/
8533
	*/
8534
	public static function chatRoomClientDemoV3(){
8534
	public static function chatRoomClientDemoV3(){
8535
		
8535
 
8536
		#初始化要回傳的結果
8536
		#初始化要回傳的結果
8537
		$result=array();
8537
		$result=array();
8538
 
8538
 
8539
		#初始化儲存html的變數
8539
		#初始化儲存html的變數
8540
		$result["content"]="";
8540
		$result["content"]="";
8541
 
8541
 
8542
		#取得當前執行的函數名稱
8542
		#取得當前執行的函數名稱
8543
		$result["function"]=__FUNCTION__;
8543
		$result["function"]=__FUNCTION__;
8544
		
8544
 
8545
		#初始化帳號密碼
8545
		#初始化帳號密碼
8546
		$ac="";
8546
		$ac="";
8547
		$pw="";
8547
		$pw="";
8548
		
8548
 
8549
		#如果存在 $_SESSION["account"]
8549
		#如果存在 $_SESSION["account"]
8550
		if(isset($_SESSION["account"])){
8550
		if(isset($_SESSION["account"])){
8551
			
8551
 
8552
			#取得帳戶
8552
			#取得帳戶
8553
			$ac=$_SESSION["account"];
8553
			$ac=$_SESSION["account"];
8554
			
8554
 
8555
			}#if end
8555
			}#if end
8556
		
8556
 
8557
		#如果 this->$passwordCol 不為 ""
8557
		#如果 this->$passwordCol 不為 ""
8558
		if(Chat::$passwordCol!==""){
8558
		if(Chat::$passwordCol!==""){
8559
			
8559
 
8560
			#如果存在 $_SESSION["password"]
8560
			#如果存在 $_SESSION["password"]
8561
			if(isset($_SESSION["password"])){
8561
			if(isset($_SESSION["password"])){
8562
				
8562
 
8563
				#取得帳戶
8563
				#取得帳戶
8564
				$pw=$_SESSION["password"];
8564
				$pw=$_SESSION["password"];
8565
				
8565
 
8566
				}#if end
8566
				}#if end
8567
			
8567
 
8568
			}#if end
8568
			}#if end
8569
		
8569
 
8570
		#取得 server 的 ip
8570
		#取得 server 的 ip
8571
		#涵式說明:
8571
		#涵式說明:
8572
		#取得伺服器的IP,並回傳。
8572
		#取得伺服器的IP,並回傳。
8573
		#回傳的結果:
8573
		#回傳的結果:
8574
		#$result,伺服端的IP
8574
		#$result,伺服端的IP
8575
		#備註:
8575
		#備註:
8576
		#伺服端必須被 localhost 以外的網址連結才會出現正確的IP
8576
		#伺服端必須被 localhost 以外的網址連結才會出現正確的IP
8577
		$getServerIP=csInformation::getServerIP();
8577
		$getServerIP=csInformation::getServerIP();
8578
		
8578
 
8579
		#設置用戶端連線到webSocket的範例
8579
		#設置用戶端連線到webSocket的範例
8580
		$script="<script>
8580
		$script="<script>
8581
		
8581
 
8582
			//ready 後執行
8582
			//ready 後執行
8583
			$(document).ready(function()
8583
			$(document).ready(function()
8584
			{
8584
			{
8585
			
8585
 
8586
				//取得帳號密碼
8586
				//取得帳號密碼
8587
				ac='".$ac."';
8587
				ac='".$ac."';
8588
				pw='".$pw."';
8588
				pw='".$pw."';
8589
			
8589
 
8590
				//設置目前尚為連線到 webSocket Server
8590
				//設置目前尚為連線到 webSocket Server
8591
				connected='false';
8591
				connected='false';
8592
				
8592
 
8593
				//設置儲存應該要斷線的webSocket,避免太多連線在伺服器.
8593
				//設置儲存應該要斷線的webSocket,避免太多連線在伺服器.
8594
				wbClosed='';
8594
				wbClosed='';
8595
				
8595
 
8596
				//設置儲存webSocket連線的變數
8596
				//設置儲存webSocket連線的變數
8597
				wb='';
8597
				wb='';
8598
				
8598
 
8599
				//設置儲存msgId的變數
8599
				//設置儲存msgId的變數
8600
				msgId='';
8600
				msgId='';
8601
				
8601
 
8602
				//連線到 web socket
8602
				//連線到 web socket
8603
				connWebSock();
8603
				connWebSock();
8604
			
8604
 
8605
			});
8605
			});
8606
		
8606
 
8607
			//連線到 web socket
8607
			//連線到 web socket
8608
			function connWebSock()
8608
			function connWebSock()
8609
			{
8609
			{
8610
				//建立 web socket 連線到 wss://localhost/wss/chatDemo
8610
				//建立 web socket 連線到 wss://localhost/wss/chatDemo
8611
				var conn = new WebSocket('wss://".$getServerIP."/wss/chatDemo');
8611
				var conn = new WebSocket('wss://".$getServerIP."/wss/chatDemo');
8612
				
8612
 
8613
				//當連線成功後
8613
				//當連線成功後
8614
				conn.onopen = function(e){
8614
				conn.onopen = function(e){
8615
				
8615
 
8616
					//印出連線成功訊息到console
8616
					//印出連線成功訊息到console
8617
					console.log(\"Connection established!\");
8617
					console.log(\"Connection established!\");
8618
					
8618
 
8619
					//設置已經連上 webSocket server
8619
					//設置已經連上 webSocket server
8620
					connected='true';
8620
					connected='true';
8621
					
8621
 
8622
					//另存 webSocket 物件
8622
					//另存 webSocket 物件
8623
					wb=conn;
8623
					wb=conn;
8624
					
8624
 
8625
					//如果存在前一個連線物件
8625
					//如果存在前一個連線物件
8626
					if(wbClosed!==''){
8626
					if(wbClosed!==''){
8627
					
8627
 
8628
						//提示關閉前個連線
8628
						//提示關閉前個連線
8629
						console.log(\"close last connection!\");
8629
						console.log(\"close last connection!\");
8630
					
8630
 
8631
						//關閉之前的連線
8631
						//關閉之前的連線
8632
						wbClosed.close();
8632
						wbClosed.close();
8633
						
8633
 
8634
						//清空
8634
						//清空
8635
						wbClosed='';
8635
						wbClosed='';
8636
					
8636
 
8637
						}
8637
						}
8638
											
8638
 
8639
					}
8639
					}
8640
 
8640
 
8641
				//當有收到訊息時
8641
				//當有收到訊息時
8642
				conn.onmessage = function(e){
8642
				conn.onmessage = function(e){
8643
					
8643
 
8644
					//將訊息顯現在console
8644
					//將訊息顯現在console
8645
					console.log(e.data);
8645
					console.log(e.data);
8646
					
8646
 
8647
					//將訊息寫到文字方框裡面
8647
					//將訊息寫到文字方框裡面
8648
					$('#history').val(e.data+'\\r\\n'+$('#history').val());
8648
					$('#history').val(e.data+'\\r\\n'+$('#history').val());
8649
					
8649
 
8650
					//解析json字串
8650
					//解析json字串
8651
					data=JSON.parse(e.data);
8651
					data=JSON.parse(e.data);
8652
					
8652
 
8653
					//如果要要求login的訊息
8653
					//如果要要求login的訊息
8654
					if(data.type==='login'){
8654
					if(data.type==='login'){
8655
					
8655
 
8656
						//如果有查到帳號
8656
						//如果有查到帳號
8657
						if(ac!==''){
8657
						if(ac!==''){
8658
						
8658
 
8659
							//如果要求輸入帳號
8659
							//如果要求輸入帳號
8660
							if(data.data==='Please input your account! ex:account:ws1'){
8660
							if(data.data==='Please input your account! ex:account:ws1'){
8661
							
8661
 
8662
								//傳送帳號
8662
								//傳送帳號
8663
								conn.send('account:'+ac);
8663
								conn.send('account:'+ac);
8664
							
8664
 
8665
								}
8665
								}
8666
						
8666
 
8667
							//如果有查到密碼
8667
							//如果有查到密碼
8668
							if(pw!==''){
8668
							if(pw!==''){
8669
							
8669
 
8670
								//如果要求輸入密碼	
8670
								//如果要求輸入密碼
8671
								if(data.data==='Please input your password! ex:password:ws1'){
8671
								if(data.data==='Please input your password! ex:password:ws1'){
8672
								
8672
 
8673
									//傳送密碼
8673
									//傳送密碼
8674
									conn.send('password:'+pw);
8674
									conn.send('password:'+pw);
8675
								
8675
 
8676
									}
8676
									}
8677
									
8677
 
8678
								}
8678
								}
8679
							
8679
 
8680
							//如果存在既有的msgId,且剛登入成功
8680
							//如果存在既有的msgId,且剛登入成功
8681
							if(msgId!=='' && data.data==='Login successfully!'){
8681
							if(msgId!=='' && data.data==='Login successfully!'){
8682
							
8682
 
8683
								//設置既有的msgId
8683
								//設置既有的msgId
8684
								conn.send('msgId:'+msgId);
8684
								conn.send('msgId:'+msgId);
8685
							
8685
 
8686
								}
8686
								}
8687
							
8687
 
8688
							//反之如果登入成功
8688
							//反之如果登入成功
8689
							else if(data.data==='Login successfully!'){
8689
							else if(data.data==='Login successfully!'){
8690
							
8690
 
8691
								//查詢自己的msgId
8691
								//查詢自己的msgId
8692
								conn.send('msgId?');
8692
								conn.send('msgId?');
8693
							
8693
 
8694
								}
8694
								}
8695
															
8695
 
8696
							}
8696
							}
8697
						
8697
 
8698
						}
8698
						}
8699
					
8699
 
8700
					//如果收到的是 'msgId?'
8700
					//如果收到的是 'msgId?'
8701
					else if(data.type==='msgId?'){
8701
					else if(data.type==='msgId?'){
8702
					
8702
 
8703
						//設置自己的msgId
8703
						//設置自己的msgId
8704
						msgId=data.data;
8704
						msgId=data.data;
8705
					
8705
 
8706
						}
8706
						}
8707
						
8707
 
8708
					//如果收到的是 'msg' 類型
8708
					//如果收到的是 'msg' 類型
8709
					else if(data.type==='msg'){
8709
					else if(data.type==='msg'){
8710
					
8710
 
8711
						//回傳有收到訊息的確認
8711
						//回傳有收到訊息的確認
8712
						conn.send('mIndex:'+data.index);
8712
						conn.send('mIndex:'+data.index);
8713
									
8713
 
8714
						}
8714
						}
8715
						
8715
 
8716
					//如果收到的是 'status' 類型	
8716
					//如果收到的是 'status' 類型
8717
					else if(data.type==='status'){
8717
					else if(data.type==='status'){
8718
					
8718
 
8719
						//如果內容為設置msgId失敗
8719
						//如果內容為設置msgId失敗
8720
						if(data.data==='set msgId failed'){
8720
						if(data.data==='set msgId failed'){
8721
						
8721
 
8722
							//查詢自己的msgId
8722
							//查詢自己的msgId
8723
							conn.send('msgId?');
8723
							conn.send('msgId?');
8724
						
8724
 
8725
							}
8725
							}
8726
					
8726
 
8727
						}
8727
						}
8728
						
8728
 
8729
					//如果收到的是 'cmd' 類型	
8729
					//如果收到的是 'cmd' 類型
8730
					else if(data.type==='cmd'){
8730
					else if(data.type==='cmd'){
8731
					
8731
 
8732
						//將訊息顯現在console
8732
						//將訊息顯現在console
8733
						//console.log(e.data);
8733
						//console.log(e.data);
8734
					
8734
 
8735
						}
8735
						}
8736
											
8736
 
8737
					}
8737
					}
8738
					
8738
 
8739
				//當連線斷開後
8739
				//當連線斷開後
8740
				conn.onclose = function(e){
8740
				conn.onclose = function(e){
8741
									
8741
 
8742
					//設置未連上 webSocket server
8742
					//設置未連上 webSocket server
8743
					connected='false';
8743
					connected='false';
8744
					
8744
 
8745
					//如果有之前的連線物件
8745
					//如果有之前的連線物件
8746
					if(wb!==''){
8746
					if(wb!==''){
8747
					
8747
 
8748
						//儲存應該要斷線的webSocket,避免太多連線在伺服器.
8748
						//儲存應該要斷線的webSocket,避免太多連線在伺服器.
8749
						wbClosed=wb;
8749
						wbClosed=wb;
8750
					
8750
 
8751
						}
8751
						}
8752
										
8752
 
8753
					//清空 webSocket 物件
8753
					//清空 webSocket 物件
8754
					wb='';
8754
					wb='';
8755
				
8755
 
8756
					//指定1秒刷新一次
8756
					//指定1秒刷新一次
8757
					setTimeout('connWebSock()',1000); 
8757
					setTimeout('connWebSock()',1000);
8758
				
8758
 
8759
					}
8759
					}
-
 
8760
 
8760
					
8761
				}
8761
				}							
-
 
8762
			
8762
 
8763
			//當按下送出按鈕
8763
			//當按下送出按鈕
8764
			$('#send').on('click',function(){
8764
			$('#send').on('click',function(){
8765
			
8765
 
8766
				//如果已經連到 webSocket server 了
8766
				//如果已經連到 webSocket server 了
8767
				if(connected==='true'){
8767
				if(connected==='true'){
8768
				
8768
 
8769
					//傳送訊息
8769
					//傳送訊息
8770
					wb.send($('#input').val());
8770
					wb.send($('#input').val());
8771
					
8771
 
8772
					//清空訊息輸入欄
8772
					//清空訊息輸入欄
8773
					$('#input').val('');
8773
					$('#input').val('');
8774
				
8774
 
8775
					}
8775
					}
8776
			
8776
 
8777
				});
8777
				});
8778
				
8778
 
8779
			//重新整理頁面
8779
			//重新整理頁面
8780
			function myrefresh(){
8780
			function myrefresh(){
8781
				
8781
 
8782
				//重新整理頁面
8782
				//重新整理頁面
8783
				window.location.reload();
8783
				window.location.reload();
8784
			
8784
 
8785
				}
8785
				}
8786
			
8786
 
8787
			</script>";
8787
			</script>";
8788
		
8788
 
8789
		#設置存放歷史訊息的方框
8789
		#設置存放歷史訊息的方框
8790
		#涵式說明:
8790
		#涵式說明:
8791
		#可以輸入文字的區塊
8791
		#可以輸入文字的區塊
8792
		#回傳結果:
8792
		#回傳結果:
8793
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
8793
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
8794
		#$result["function"],當前執行的函數.
8794
		#$result["function"],當前執行的函數.
8795
		#$result["error"],錯誤訊息陣列.
8795
		#$result["error"],錯誤訊息陣列.
8796
		#$result["content"],按鈕語法. 		
8796
		#$result["content"],按鈕語法.
8797
		#必填的參數:
8797
		#必填的參數:
8798
		$conf["form::inputTextArea"]["name"]="history";#爲文字輸入框的名稱,供接收端辨識用。
8798
		$conf["form::inputTextArea"]["name"]="history";#爲文字輸入框的名稱,供接收端辨識用。
8799
		$conf["form::inputTextArea"]["readOnly"]="true";#爲是否要爲唯讀,如果爲"true",則爲唯讀。反之則爲"false"。
8799
		$conf["form::inputTextArea"]["readOnly"]="true";#爲是否要爲唯讀,如果爲"true",則爲唯讀。反之則爲"false"。
8800
		#可省略的參數:
8800
		#可省略的參數:
8801
		#$conf["id"],字串,文字區塊的id,供javaScript呼叫用.
8801
		#$conf["id"],字串,文字區塊的id,供javaScript呼叫用.
Line 8826... Line 8826...
8826
		#$conf["trEnd"]="true";#爲是否要以</tr>結尾,"true"表示"是",也可以看作該列結束,預設為"false".
8826
		#$conf["trEnd"]="true";#爲是否要以</tr>結尾,"true"表示"是",也可以看作該列結束,預設為"false".
8827
		#參考資料來源:
8827
		#參考資料來源:
8828
		#input=>http://www.w3schools.com/tags/tag_input.asp
8828
		#input=>http://www.w3schools.com/tags/tag_input.asp
8829
		$inputTextArea=form::inputTextArea($conf["form::inputTextArea"]);
8829
		$inputTextArea=form::inputTextArea($conf["form::inputTextArea"]);
8830
		unset($conf["form::inputTextArea"]);
8830
		unset($conf["form::inputTextArea"]);
8831
			
8831
 
8832
		#如果建立文字方框失敗
8832
		#如果建立文字方框失敗
8833
		if($inputTextArea["status"]==="false"){
8833
		if($inputTextArea["status"]==="false"){
8834
			
8834
 
8835
			#設置執行失敗
8835
			#設置執行失敗
8836
			$result["status"]="false";
8836
			$result["status"]="false";
8837
			
8837
 
8838
			#設置執行錯誤訊息
8838
			#設置執行錯誤訊息
8839
			$result["error"]=$inputTextArea;
8839
			$result["error"]=$inputTextArea;
8840
			
8840
 
8841
			#回傳結果
8841
			#回傳結果
8842
			return $result;
8842
			return $result;
8843
			
8843
 
8844
			}#if end	
8844
			}#if end
8845
			
8845
 
8846
		#串連文字訊息方框
8846
		#串連文字訊息方框
8847
		$result["content"]=$result["content"].$inputTextArea["content"];
8847
		$result["content"]=$result["content"].$inputTextArea["content"];
8848
			
8848
 
8849
		#設置填寫要傳送的訊息輸入方框
8849
		#設置填寫要傳送的訊息輸入方框
8850
		#涵式說明:
8850
		#涵式說明:
8851
		#可以輸入文字的表單
8851
		#可以輸入文字的表單
8852
		#回傳的結果:
8852
		#回傳的結果:
8853
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
8853
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
8854
		#$result["function"],當前執行的函數.
8854
		#$result["function"],當前執行的函數.
8855
		#$result["error"],錯誤訊息陣列.
8855
		#$result["error"],錯誤訊息陣列.
8856
		#$result["content"],按鈕語法. 
8856
		#$result["content"],按鈕語法.
8857
		#必填的參數
8857
		#必填的參數
8858
		$conf["form::inputText"]["name"]="data";#爲該文字輸入框的名稱,用於讓接收頁面讀取的名稱。
8858
		$conf["form::inputText"]["name"]="data";#爲該文字輸入框的名稱,用於讓接收頁面讀取的名稱。
8859
		$conf["form::inputText"]["readOnly"]="false";#爲該文字框是否可以填寫資料,若要只能觀看不能填寫,那麼就必須將其值設爲"true",反之要設爲"false"
8859
		$conf["form::inputText"]["readOnly"]="false";#爲該文字框是否可以填寫資料,若要只能觀看不能填寫,那麼就必須將其值設爲"true",反之要設爲"false"
8860
		#可省略的參數:
8860
		#可省略的參數:
8861
		#$conf["width"]="";#爲文字框的外觀長度,預設為"100%".
8861
		#$conf["width"]="";#爲文字框的外觀長度,預設為"100%".
8862
		#$conf["maxInputLength"]="";#爲可輸入的最大位元長度,預設不限制。
8862
		#$conf["maxInputLength"]="";#爲可輸入的最大位元長度,預設不限制。
8863
		#$conf["autoFocus"]="true";#是否為將填寫的指標移到該表單,"true"代表要,預設為"false".
8863
		#$conf["autoFocus"]="true";#是否為將填寫的指標移到該表單,"true"代表要,預設為"false".
8864
		#$conf["value"]="";#爲該文字框預設要顯示的文字,預設爲""。	
8864
		#$conf["value"]="";#爲該文字框預設要顯示的文字,預設爲""。
8865
		#$conf["class"]="";#爲要套用的css樣式,若省略,則會套用預設的 "__inputTextCssStyle" 樣式,其屬性爲 "width:100%","font-size:30px"
8865
		#$conf["class"]="";#爲要套用的css樣式,若省略,則會套用預設的 "__inputTextCssStyle" 樣式,其屬性爲 "width:100%","font-size:30px"
8866
		#$conf["jsActivitor"]="";#爲觸發js的條件,可以是"onChange"(已改變內容時)、"onClick"(按下按鈕時)、"onkeyup"(當鍵盤按下放開後)、"onmouseover"(當滑鼠移過去的時候)...,須搭配$conf["jsSubmitActionTarget"]參數。
8866
		#$conf["jsActivitor"]="";#爲觸發js的條件,可以是"onChange"(已改變內容時)、"onClick"(按下按鈕時)、"onkeyup"(當鍵盤按下放開後)、"onmouseover"(當滑鼠移過去的時候)...,須搭配$conf["jsSubmitActionTarget"]參數。
8867
		#$conf["jsAction"]="";#爲該js是要做什麼,可以是"document.testForm.submit()"(傳送名爲testForm的表單內容)...,須搭配$conf["jsActivitor"]參數。		
8867
		#$conf["jsAction"]="";#爲該js是要做什麼,可以是"document.testForm.submit()"(傳送名爲testForm的表單內容)...,須搭配$conf["jsActivitor"]參數。
8868
		#$conf["trStart"]="true";#爲是否要以<tr>開頭,"true"表示"是"。也可以看作新的一列開始,預設為"false".
8868
		#$conf["trStart"]="true";#爲是否要以<tr>開頭,"true"表示"是"。也可以看作新的一列開始,預設為"false".
8869
		#$conf["tdStart"]="true";#爲是否要以<td>開頭,"true"表示"是"。也可以看成列裏面的元素開始,預設為"false".
8869
		#$conf["tdStart"]="true";#爲是否要以<td>開頭,"true"表示"是"。也可以看成列裏面的元素開始,預設為"false".
8870
		#$conf["formStart"]="true";#爲是否要以<form>開頭,"true"表示"是",也可以看成表單的開始,預設為"false".
8870
		#$conf["formStart"]="true";#爲是否要以<form>開頭,"true"表示"是",也可以看成表單的開始,預設為"false".
8871
		#$conf["formAction"]="";#表單遞交的目的地,若$conf["formStart"]為"true",則該參數不能省略.
8871
		#$conf["formAction"]="";#表單遞交的目的地,若$conf["formStart"]為"true",則該參數不能省略.
8872
		#$conf["formName"]="";#爲該表單的名稱
8872
		#$conf["formName"]="";#爲該表單的名稱
Line 8894... Line 8894...
8894
		$conf["form::inputText"]["id"]="input";
8894
		$conf["form::inputText"]["id"]="input";
8895
		#參考資料來源:
8895
		#參考資料來源:
8896
		#input=>http://www.w3schools.com/tags/tag_input.asp
8896
		#input=>http://www.w3schools.com/tags/tag_input.asp
8897
		$inputText=form::inputText($conf["form::inputText"]);
8897
		$inputText=form::inputText($conf["form::inputText"]);
8898
		unset($conf["form::inputText"]);
8898
		unset($conf["form::inputText"]);
8899
		
8899
 
8900
		#如果輸入文字框失敗
8900
		#如果輸入文字框失敗
8901
		if($inputText["status"]==="false"){
8901
		if($inputText["status"]==="false"){
8902
			
8902
 
8903
			#設置執行失敗
8903
			#設置執行失敗
8904
			$result["status"]="false";
8904
			$result["status"]="false";
8905
			
8905
 
8906
			#設置執行錯誤訊息
8906
			#設置執行錯誤訊息
8907
			$result["error"]=$inputText;
8907
			$result["error"]=$inputText;
8908
			
8908
 
8909
			#回傳結果
8909
			#回傳結果
8910
			return $result;
8910
			return $result;
8911
			
8911
 
8912
			}#if end	
8912
			}#if end
8913
		
8913
 
8914
		#文字輸入框
8914
		#文字輸入框
8915
		$result["content"]=$result["content"].$inputText["content"];
8915
		$result["content"]=$result["content"].$inputText["content"];
8916
		
8916
 
8917
		#設置觸發的按鈕
8917
		#設置觸發的按鈕
8918
		#函式說明:
8918
		#函式說明:
8919
		#放置按鈕
8919
		#放置按鈕
8920
		#回傳結果:
8920
		#回傳結果:
8921
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
8921
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
8922
		#$result["function"],當前執行的函數.
8922
		#$result["function"],當前執行的函數.
8923
		#$result["error"],錯誤訊息陣列.
8923
		#$result["error"],錯誤訊息陣列.
8924
		#$result["content"],按鈕語法. 
8924
		#$result["content"],按鈕語法.
8925
		#必要的參數:
8925
		#必要的參數:
8926
		$conf["form::button"]["buttonDisplayValue"]="Send";#爲按鈕上顯示的文字。
8926
		$conf["form::button"]["buttonDisplayValue"]="Send";#爲按鈕上顯示的文字。
8927
		#可省略的參數:
8927
		#可省略的參數:
8928
		#$conf["buttonStyleName"]="";#可省略,爲按鈕所要使用的css樣式類別名稱,預設的css樣式爲 __simpleButtonLinkDefaultButtonCssStyle 。
8928
		#$conf["buttonStyleName"]="";#可省略,爲按鈕所要使用的css樣式類別名稱,預設的css樣式爲 __simpleButtonLinkDefaultButtonCssStyle 。
8929
			#其屬性爲   "width","height","font-size","text-align"
8929
			#其屬性爲   "width","height","font-size","text-align"
Line 8953... Line 8953...
8953
		$conf["form::button"]["buttonId"]="send";
8953
		$conf["form::button"]["buttonId"]="send";
8954
		#參考資料來源:
8954
		#參考資料來源:
8955
		#http://stackoverflow.com/questions/3014649/how-to-disable-html-button-using-javascript
8955
		#http://stackoverflow.com/questions/3014649/how-to-disable-html-button-using-javascript
8956
		$button=form::button($conf["form::button"]);
8956
		$button=form::button($conf["form::button"]);
8957
		unset($conf["form::button"]);
8957
		unset($conf["form::button"]);
8958
			
8958
 
8959
		#如果建立按鈕失敗
8959
		#如果建立按鈕失敗
8960
		if($button["status"]==="false"){
8960
		if($button["status"]==="false"){
8961
			
8961
 
8962
			#設置執行失敗
8962
			#設置執行失敗
8963
			$result["status"]="false";
8963
			$result["status"]="false";
8964
			
8964
 
8965
			#設置執行錯誤訊息
8965
			#設置執行錯誤訊息
8966
			$result["error"]=$button;
8966
			$result["error"]=$button;
8967
			
8967
 
8968
			#回傳結果
8968
			#回傳結果
8969
			return $result;
8969
			return $result;
8970
			
8970
 
8971
			}#if end	
8971
			}#if end
8972
		
8972
 
8973
		#建立跳到別的頁面的連結
8973
		#建立跳到別的頁面的連結
8974
		#涵式說明:
8974
		#涵式說明:
8975
		#放置超鏈結
8975
		#放置超鏈結
8976
		#回傳的結果:
8976
		#回傳的結果:
8977
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
8977
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
8978
		#$result["error"],錯誤訊息
8978
		#$result["error"],錯誤訊息
8979
		#$result["content"],語法
8979
		#$result["content"],語法
8980
		#$result["function"],當前執行的函數名稱
8980
		#$result["function"],當前執行的函數名稱
8981
		#必填的參數:
8981
		#必填的參數:
8982
		#$conf["position"],字串,爲要連結到的位置,若留空,將回自動變成"#",則可以搭配ajax傳值.	
8982
		#$conf["position"],字串,爲要連結到的位置,若留空,將回自動變成"#",則可以搭配ajax傳值.
8983
		$conf["link::show_link"]["position"]="webSocketT/hangFewSecs.php";
8983
		$conf["link::show_link"]["position"]="webSocketT/hangFewSecs.php";
8984
		#$conf["linkName",字串,爲連結的顯示名稱
8984
		#$conf["linkName",字串,爲連結的顯示名稱
8985
		$conf["link::show_link"]["linkName"]="到別的頁面轉轉";
8985
		$conf["link::show_link"]["linkName"]="到別的頁面轉轉";
8986
		#可省略的參數:
8986
		#可省略的參數:
8987
		#$conf["method"],字串,爲點選連結後,新畫面要如何呈現,可省略預設爲"_self",可用的選項有 _top(覆蓋目前的視窗來顯現新內容) _parent _self _blank(跳新視窗)
8987
		#$conf["method"],字串,爲點選連結後,新畫面要如何呈現,可省略預設爲"_self",可用的選項有 _top(覆蓋目前的視窗來顯現新內容) _parent _self _blank(跳新視窗)
8988
		#$conf["method"]=""; 
8988
		#$conf["method"]="";
8989
		#$conf["class"],字串,爲要套用的css超連節樣式,可省略.
8989
		#$conf["class"],字串,爲要套用的css超連節樣式,可省略.
8990
		#$conf["class"]="";
8990
		#$conf["class"]="";
8991
		#$conf["id"],字串,超連結的id.
8991
		#$conf["id"],字串,超連結的id.
8992
		#$conf["id"]="";
8992
		#$conf["id"]="";
8993
		#$conf["no_outline"],字串,是否要取消連結的框線,"false為不取消,"true"代表要取消,預設為"false".
8993
		#$conf["no_outline"],字串,是否要取消連結的框線,"false為不取消,"true"代表要取消,預設為"false".
8994
		#$conf["no_outline"]="";
8994
		#$conf["no_outline"]="";
8995
		$show_link=link::show_link($conf["link::show_link"]);
8995
		$show_link=link::show_link($conf["link::show_link"]);
8996
		unset($conf["link::show_link"]);
8996
		unset($conf["link::show_link"]);
8997
		
8997
 
8998
		#串接超連結
8998
		#串接超連結
8999
		$result["content"]=$result["content"].$show_link["content"];
8999
		$result["content"]=$result["content"].$show_link["content"];
9000
			
9000
 
9001
		#設置用戶端的html與js語法
9001
		#設置用戶端的html與js語法
9002
		$result["content"]=$result["content"].$button["content"].$script;
9002
		$result["content"]=$result["content"].$button["content"].$script;
9003
			
9003
 
9004
		#設置執行正常
9004
		#設置執行正常
9005
		$result["status"]="true";
9005
		$result["status"]="true";
9006
		
9006
 
9007
		#回傳結果
9007
		#回傳結果
9008
		return $result;
9008
		return $result;
9009
		
9009
 
9010
		}#function chatRoomClientDemoV3 end	
9010
		}#function chatRoomClientDemoV3 end
9011
	
9011
 
9012
	/*
9012
	/*
9013
	#函式說明:
9013
	#函式說明:
9014
	#擴充過的Ratchet聊天室silder用戶端js範例,可以不跟ajax排隊.
9014
	#擴充過的Ratchet聊天室silder用戶端js範例,可以不跟ajax排隊.
9015
	#回傳結果:
9015
	#回傳結果:
9016
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
9016
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
9017
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
9017
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
9018
	#$result["function"],當前執行的函式名稱.		
9018
	#$result["function"],當前執行的函式名稱.
9019
	#$result["content"],html.
9019
	#$result["content"],html.
9020
	#必填參數:
9020
	#必填參數:
9021
	#無.
9021
	#無.
9022
	#可省略參數:
9022
	#可省略參數:
9023
	#無.
9023
	#無.
9024
	#參考資料:
9024
	#參考資料:
9025
	#Ratchet官網=>http://socketo.me/
9025
	#Ratchet官網=>http://socketo.me/
9026
	#用apache的proxy來判斷為哪個服務,導到對應的port裡面=>https://groups.google.com/forum/#!topic/ratchet-php/dj-PgPPO_J0
9026
	#用apache的proxy來判斷為哪個服務,導到對應的port裡面=>https://groups.google.com/forum/#!topic/ratchet-php/dj-PgPPO_J0
9027
	#聊天室範例=>http://socketo.me/docs/hello-world
9027
	#聊天室範例=>http://socketo.me/docs/hello-world
9028
	#備註:
9028
	#備註:
9029
	#僅適用於https網頁透過wss連線到web socket server.	
9029
	#僅適用於https網頁透過wss連線到web socket server.
9030
	*/
9030
	*/
9031
	public static function soldierClientDemo(){
9031
	public static function soldierClientDemo(){
9032
		
9032
 
9033
		#初始化要回傳的結果
9033
		#初始化要回傳的結果
9034
		$result=array();
9034
		$result=array();
9035
 
9035
 
9036
		#初始化儲存html的變數
9036
		#初始化儲存html的變數
9037
		$result["content"]="";
9037
		$result["content"]="";
9038
 
9038
 
9039
		#取得當前執行的函數名稱
9039
		#取得當前執行的函數名稱
9040
		$result["function"]=__FUNCTION__;
9040
		$result["function"]=__FUNCTION__;
9041
		
9041
 
9042
		#初始化帳號密碼
9042
		#初始化帳號密碼
9043
		$ac="";
9043
		$ac="";
9044
		$pw="";
9044
		$pw="";
9045
		
9045
 
9046
		#如果存在 $_SESSION["account"]
9046
		#如果存在 $_SESSION["account"]
9047
		if(isset($_SESSION["account"])){
9047
		if(isset($_SESSION["account"])){
9048
			
9048
 
9049
			#取得帳戶
9049
			#取得帳戶
9050
			$ac=$_SESSION["account"];
9050
			$ac=$_SESSION["account"];
9051
			
9051
 
9052
			}#if end
9052
			}#if end
9053
		
9053
 
9054
		#如果 this->$passwordCol 不為 ""
9054
		#如果 this->$passwordCol 不為 ""
9055
		if(Chat::$passwordCol!==""){
9055
		if(Chat::$passwordCol!==""){
9056
			
9056
 
9057
			#如果存在 $_SESSION["password"]
9057
			#如果存在 $_SESSION["password"]
9058
			if(isset($_SESSION["password"])){
9058
			if(isset($_SESSION["password"])){
9059
				
9059
 
9060
				#取得帳戶
9060
				#取得帳戶
9061
				$pw=$_SESSION["password"];
9061
				$pw=$_SESSION["password"];
9062
				
9062
 
9063
				}#if end
9063
				}#if end
9064
			
9064
 
9065
			}#if end
9065
			}#if end
9066
		
9066
 
9067
		#取得 server 的 ip
9067
		#取得 server 的 ip
9068
		#涵式說明:
9068
		#涵式說明:
9069
		#取得伺服器的IP,並回傳。
9069
		#取得伺服器的IP,並回傳。
9070
		#回傳的結果:
9070
		#回傳的結果:
9071
		#$result,伺服端的IP
9071
		#$result,伺服端的IP
9072
		#備註:
9072
		#備註:
9073
		#伺服端必須被 localhost 以外的網址連結才會出現正確的IP
9073
		#伺服端必須被 localhost 以外的網址連結才會出現正確的IP
9074
		$getServerIP=csInformation::getServerIP();
9074
		$getServerIP=csInformation::getServerIP();
9075
		
9075
 
9076
		#設置用戶端連線到webSocket的範例
9076
		#設置用戶端連線到webSocket的範例
9077
		$script="<script>
9077
		$script="<script>
9078
		
9078
 
9079
			//ready 後執行
9079
			//ready 後執行
9080
			$(document).ready(function(){
9080
			$(document).ready(function(){
9081
			
9081
 
9082
				//取得帳號密碼
9082
				//取得帳號密碼
9083
				ac='".$ac."';
9083
				ac='".$ac."';
9084
				pw='".$pw."';
9084
				pw='".$pw."';
9085
			
9085
 
9086
				//設置目前尚為連線到 webSocket Server
9086
				//設置目前尚為連線到 webSocket Server
9087
				connected='false';
9087
				connected='false';
9088
				
9088
 
9089
				//設置儲存應該要斷線的webSocket,避免太多連線在伺服器.
9089
				//設置儲存應該要斷線的webSocket,避免太多連線在伺服器.
9090
				wbClosed='';
9090
				wbClosed='';
9091
				
9091
 
9092
				//設置儲存webSocket連線的變數
9092
				//設置儲存webSocket連線的變數
9093
				wb='';
9093
				wb='';
9094
				
9094
 
9095
				//告訴server要執行的指令
9095
				//告訴server要執行的指令
9096
				cmd='ls -al';
9096
				cmd='ls -al';
9097
				
9097
 
9098
				//連線到 web socket
9098
				//連線到 web socket
9099
				connWebSock();
9099
				connWebSock();
9100
			
9100
 
9101
				//榜定按鈕事件
9101
				//榜定按鈕事件
9102
				buttonEvent();
9102
				buttonEvent();
9103
			
9103
 
9104
				});
9104
				});
9105
			
9105
 
9106
			//連線到 web socket
9106
			//連線到 web socket
9107
			function connWebSock()
9107
			function connWebSock()
9108
			{
9108
			{
9109
				//建立 web socket 連線到 wss://localhost/wss/chatDemo
9109
				//建立 web socket 連線到 wss://localhost/wss/chatDemo
9110
				var conn = new WebSocket('wss://".$getServerIP."/wss/chatDemo');
9110
				var conn = new WebSocket('wss://".$getServerIP."/wss/chatDemo');
9111
				
9111
 
9112
				//當連線成功後
9112
				//當連線成功後
9113
				conn.onopen = function(e){
9113
				conn.onopen = function(e){
9114
				
9114
 
9115
					//印出連線成功訊息到console
9115
					//印出連線成功訊息到console
9116
					console.log(\"Connection established!\");
9116
					console.log(\"Connection established!\");
9117
					
9117
 
9118
					//設置已經連上 webSocket server
9118
					//設置已經連上 webSocket server
9119
					connected='true';
9119
					connected='true';
9120
					
9120
 
9121
					//另存 webSocket 物件
9121
					//另存 webSocket 物件
9122
					wb=conn;
9122
					wb=conn;
9123
					
9123
 
9124
					//如果存在前一個連線物件
9124
					//如果存在前一個連線物件
9125
					if(wbClosed!==''){
9125
					if(wbClosed!==''){
9126
					
9126
 
9127
						//提示關閉前個連線
9127
						//提示關閉前個連線
9128
						console.log(\"close last connection!\");
9128
						console.log(\"close last connection!\");
9129
					
9129
 
9130
						//關閉之前的連線
9130
						//關閉之前的連線
9131
						wbClosed.close();
9131
						wbClosed.close();
9132
						
9132
 
9133
						//清空
9133
						//清空
9134
						wbClosed='';
9134
						wbClosed='';
9135
					
9135
 
9136
						}
9136
						}
9137
											
9137
 
9138
					}
9138
					}
9139
 
9139
 
9140
				//當有收到訊息時
9140
				//當有收到訊息時
9141
				conn.onmessage = function(e){
9141
				conn.onmessage = function(e){
9142
					
9142
 
9143
					//將訊息顯現在console
9143
					//將訊息顯現在console
9144
					console.log(e.data);
9144
					console.log(e.data);
9145
										
9145
 
9146
					//解析json字串
9146
					//解析json字串
9147
					data=JSON.parse(e.data);
9147
					data=JSON.parse(e.data);
9148
					
9148
 
9149
					//如果要要求login的訊息
9149
					//如果要要求login的訊息
9150
					if(data.type==='login'){
9150
					if(data.type==='login'){
9151
										
9151
 
9152
						//如果有查到帳號
9152
						//如果有查到帳號
9153
						if(ac!==''){
9153
						if(ac!==''){
9154
						
9154
 
9155
							//如果要求輸入帳號
9155
							//如果要求輸入帳號
9156
							if(data.data==='Please input your account! ex:account:ws1'){
9156
							if(data.data==='Please input your account! ex:account:ws1'){
9157
															
9157
 
9158
								//傳送帳號
9158
								//傳送帳號
9159
								conn.send('account:'+ac);
9159
								conn.send('account:'+ac);
9160
							
9160
 
9161
								}
9161
								}
9162
						
9162
 
9163
							//如果有查到密碼
9163
							//如果有查到密碼
9164
							if(pw!==''){
9164
							if(pw!==''){
9165
							
9165
 
9166
								//如果要求輸入密碼	
9166
								//如果要求輸入密碼
9167
								if(data.data==='Please input your password! ex:password:ws1'){
9167
								if(data.data==='Please input your password! ex:password:ws1'){
9168
								
9168
 
9169
									//傳送密碼
9169
									//傳送密碼
9170
									conn.send('password:'+pw);
9170
									conn.send('password:'+pw);
9171
								
9171
 
9172
									}
9172
									}
9173
									
9173
 
9174
								}
9174
								}
9175
															
9175
 
9176
							}
9176
							}
9177
							
9177
 
9178
						//反之如果登入成功
9178
						//反之如果登入成功
9179
						else if(data.data==='Login successfully!'){
9179
						else if(data.data==='Login successfully!'){
9180
							
9180
 
9181
							//設置已經驗證成功
9181
							//設置已經驗證成功
9182
							connected='true';
9182
							connected='true';
9183
							
9183
 
9184
							}
9184
							}
9185
						
9185
 
9186
						}
9186
						}
9187
					
9187
 
9188
					//如果收到的是 'cmd' 類型	
9188
					//如果收到的是 'cmd' 類型
9189
					else if(data.type==='cmd'){
9189
					else if(data.type==='cmd'){
9190
					
9190
 
9191
						//將訊息顯現在console
9191
						//將訊息顯現在console
9192
						//console.log(data.data);
9192
						//console.log(data.data);
9193
					
9193
 
9194
						}
9194
						}
9195
										
9195
 
9196
					}
9196
					}
9197
					
9197
 
9198
				//當連線斷開後
9198
				//當連線斷開後
9199
				conn.onclose = function(e){
9199
				conn.onclose = function(e){
9200
									
9200
 
9201
					//設置未連上 webSocket server
9201
					//設置未連上 webSocket server
9202
					connected='false';
9202
					connected='false';
9203
					
9203
 
9204
					//如果有之前的連線物件
9204
					//如果有之前的連線物件
9205
					if(wb!==''){
9205
					if(wb!==''){
9206
					
9206
 
9207
						//儲存應該要斷線的webSocket,避免太多連線在伺服器.
9207
						//儲存應該要斷線的webSocket,避免太多連線在伺服器.
9208
						wbClosed=wb;
9208
						wbClosed=wb;
9209
					
9209
 
9210
						}
9210
						}
9211
										
9211
 
9212
					//清空 webSocket 物件
9212
					//清空 webSocket 物件
9213
					wb='';
9213
					wb='';
9214
				
9214
 
9215
					//指定1秒刷新一次
9215
					//指定1秒刷新一次
9216
					setTimeout('connWebSock()',1000); 
9216
					setTimeout('connWebSock()',1000);
9217
				
9217
 
9218
					}
9218
					}
-
 
9219
 
9219
					
9220
				}
9220
				}							
-
 
9221
			
9221
 
9222
			//榜定下達命令的按鈕事件
9222
			//榜定下達命令的按鈕事件
9223
			function buttonEvent(){
9223
			function buttonEvent(){
9224
							
9224
 
9225
				//當按下送出按鈕
9225
				//當按下送出按鈕
9226
				$('#send').on('click',function(){
9226
				$('#send').on('click',function(){
9227
								
9227
 
9228
					//如果已經連到 webSocket server 了
9228
					//如果已經連到 webSocket server 了
9229
					if(connected==='true'){
9229
					if(connected==='true'){
9230
					
9230
 
9231
						//傳送訊息
9231
						//傳送訊息
9232
						wb.send('cmd:'+cmd);
9232
						wb.send('cmd:'+cmd);
9233
										
9233
 
9234
						}
9234
						}
9235
				
9235
 
9236
					});
9236
					});
9237
					
9237
 
9238
				//榜定ajax要求	
9238
				//榜定ajax要求
9239
				$('#run').on('click',function(){
9239
				$('#run').on('click',function(){
9240
				
9240
 
9241
					$.ajax({
9241
					$.ajax({
9242
					
9242
 
9243
						url: 'webSocketT/sleep.php',
9243
						url: 'webSocketT/sleep.php',
9244
						type:'GET',
9244
						type:'GET',
9245
						async:true,
9245
						async:true,
9246
						data:{},
9246
						data:{},
9247
						success:function(data, status){
9247
						success:function(data, status){
9248
												
9248
 
9249
							console.log(data.toSource());
9249
							console.log(data.toSource());
9250
							
9250
 
9251
							}
9251
							}
9252
							
9252
 
9253
						});
9253
						});
9254
						
9254
 
9255
					});
9255
					});
9256
			
9256
 
9257
				}
9257
				}
9258
					
9258
 
9259
			</script>";		
9259
			</script>";
9260
		
9260
 
9261
		#串接script語法
9261
		#串接script語法
9262
		$result["content"]=$result["content"].$script;
9262
		$result["content"]=$result["content"].$script;
9263
		
9263
 
9264
		#設置觸發ajax的按鈕
9264
		#設置觸發ajax的按鈕
9265
		#函式說明:
9265
		#函式說明:
9266
		#放置按鈕
9266
		#放置按鈕
9267
		#回傳結果:
9267
		#回傳結果:
9268
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
9268
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
9269
		#$result["function"],當前執行的函數.
9269
		#$result["function"],當前執行的函數.
9270
		#$result["error"],錯誤訊息陣列.
9270
		#$result["error"],錯誤訊息陣列.
9271
		#$result["content"],按鈕語法. 
9271
		#$result["content"],按鈕語法.
9272
		#必要的參數:
9272
		#必要的參數:
9273
		$conf["form::button"]["buttonDisplayValue"]="ajax";#爲按鈕上顯示的文字。
9273
		$conf["form::button"]["buttonDisplayValue"]="ajax";#爲按鈕上顯示的文字。
9274
		#可省略的參數:
9274
		#可省略的參數:
9275
		#$conf["buttonStyleName"]="";#可省略,爲按鈕所要使用的css樣式類別名稱,預設的css樣式爲 __simpleButtonLinkDefaultButtonCssStyle 。
9275
		#$conf["buttonStyleName"]="";#可省略,爲按鈕所要使用的css樣式類別名稱,預設的css樣式爲 __simpleButtonLinkDefaultButtonCssStyle 。
9276
			#其屬性爲   "width","height","font-size","text-align"
9276
			#其屬性爲   "width","height","font-size","text-align"
Line 9300... Line 9300...
9300
		$conf["form::button"]["buttonId"]="run";
9300
		$conf["form::button"]["buttonId"]="run";
9301
		#參考資料來源:
9301
		#參考資料來源:
9302
		#http://stackoverflow.com/questions/3014649/how-to-disable-html-button-using-javascript
9302
		#http://stackoverflow.com/questions/3014649/how-to-disable-html-button-using-javascript
9303
		$button=form::button($conf["form::button"]);
9303
		$button=form::button($conf["form::button"]);
9304
		unset($conf["form::button"]);
9304
		unset($conf["form::button"]);
9305
			
9305
 
9306
		#如果建立按鈕失敗
9306
		#如果建立按鈕失敗
9307
		if($button["status"]==="false"){
9307
		if($button["status"]==="false"){
9308
			
9308
 
9309
			#設置執行失敗
9309
			#設置執行失敗
9310
			$result["status"]="false";
9310
			$result["status"]="false";
9311
			
9311
 
9312
			#設置執行錯誤訊息
9312
			#設置執行錯誤訊息
9313
			$result["error"]=$button;
9313
			$result["error"]=$button;
9314
			
9314
 
9315
			#回傳結果
9315
			#回傳結果
9316
			return $result;
9316
			return $result;
9317
			
9317
 
9318
			}#if end
9318
			}#if end
9319
			
9319
 
9320
		#串接按鈕語法
9320
		#串接按鈕語法
9321
		$result["content"]=$result["content"].$button["content"];
9321
		$result["content"]=$result["content"].$button["content"];
9322
		
9322
 
9323
		#設置觸發命令的按鈕
9323
		#設置觸發命令的按鈕
9324
		#函式說明:
9324
		#函式說明:
9325
		#放置按鈕
9325
		#放置按鈕
9326
		#回傳結果:
9326
		#回傳結果:
9327
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
9327
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
9328
		#$result["function"],當前執行的函數.
9328
		#$result["function"],當前執行的函數.
9329
		#$result["error"],錯誤訊息陣列.
9329
		#$result["error"],錯誤訊息陣列.
9330
		#$result["content"],按鈕語法. 
9330
		#$result["content"],按鈕語法.
9331
		#必要的參數:
9331
		#必要的參數:
9332
		$conf["form::button"]["buttonDisplayValue"]="Send cmd";#爲按鈕上顯示的文字。
9332
		$conf["form::button"]["buttonDisplayValue"]="Send cmd";#爲按鈕上顯示的文字。
9333
		#可省略的參數:
9333
		#可省略的參數:
9334
		#$conf["buttonStyleName"]="";#可省略,爲按鈕所要使用的css樣式類別名稱,預設的css樣式爲 __simpleButtonLinkDefaultButtonCssStyle 。
9334
		#$conf["buttonStyleName"]="";#可省略,爲按鈕所要使用的css樣式類別名稱,預設的css樣式爲 __simpleButtonLinkDefaultButtonCssStyle 。
9335
			#其屬性爲   "width","height","font-size","text-align"
9335
			#其屬性爲   "width","height","font-size","text-align"
Line 9359... Line 9359...
9359
		$conf["form::button"]["buttonId"]="send";
9359
		$conf["form::button"]["buttonId"]="send";
9360
		#參考資料來源:
9360
		#參考資料來源:
9361
		#http://stackoverflow.com/questions/3014649/how-to-disable-html-button-using-javascript
9361
		#http://stackoverflow.com/questions/3014649/how-to-disable-html-button-using-javascript
9362
		$button=form::button($conf["form::button"]);
9362
		$button=form::button($conf["form::button"]);
9363
		unset($conf["form::button"]);
9363
		unset($conf["form::button"]);
9364
			
9364
 
9365
		#如果建立按鈕失敗
9365
		#如果建立按鈕失敗
9366
		if($button["status"]==="false"){
9366
		if($button["status"]==="false"){
9367
			
9367
 
9368
			#設置執行失敗
9368
			#設置執行失敗
9369
			$result["status"]="false";
9369
			$result["status"]="false";
9370
			
9370
 
9371
			#設置執行錯誤訊息
9371
			#設置執行錯誤訊息
9372
			$result["error"]=$button;
9372
			$result["error"]=$button;
9373
			
9373
 
9374
			#回傳結果
9374
			#回傳結果
9375
			return $result;
9375
			return $result;
9376
			
9376
 
9377
			}#if end
9377
			}#if end
9378
			
9378
 
9379
		#串接按鈕語法
9379
		#串接按鈕語法
9380
		$result["content"]=$result["content"].$button["content"];
9380
		$result["content"]=$result["content"].$button["content"];
9381
			
9381
 
9382
		#設置執行正常
9382
		#設置執行正常
9383
		$result["status"]="true";
9383
		$result["status"]="true";
9384
		
9384
 
9385
		#回傳結果
9385
		#回傳結果
9386
		return $result;
9386
		return $result;
9387
		
9387
 
9388
		}#function soldierClientDemo end
9388
		}#function soldierClientDemo end
9389
	
9389
 
9390
	/*
9390
	/*
9391
	#函式說明:
9391
	#函式說明:
9392
	#擴充過的Ratchet的聊天室soilier用戶端與伺服端範例,port為8080.
9392
	#擴充過的Ratchet的聊天室soilier用戶端與伺服端範例,port為8080.
9393
	#回傳結果:
9393
	#回傳結果:
9394
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
9394
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
9395
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
9395
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
9396
	#$result["function"],當前執行的函式名稱.		
9396
	#$result["function"],當前執行的函式名稱.
9397
	#必填參數:
9397
	#必填參數:
9398
	#$conf["fileArgu"],字串,__FILE__的內容.
9398
	#$conf["fileArgu"],字串,__FILE__的內容.
9399
	$conf["fileArgu"]=__FILE__;
9399
	$conf["fileArgu"]=__FILE__;
9400
	#可省略參數:
9400
	#可省略參數:
9401
	#$conf["disReg"],字串,是否要取消apache的passProxy檢查與設定,預設為"false"不取消,"true"為取消.
9401
	#$conf["disReg"],字串,是否要取消apache的passProxy檢查與設定,預設為"false"不取消,"true"為取消.
Line 9406... Line 9406...
9406
	#備註:
9406
	#備註:
9407
	#必須要在命令列環境下執行一次,建立webSocket Server後,再於網頁端執行即可產生webSocket Client端的code.
9407
	#必須要在命令列環境下執行一次,建立webSocket Server後,再於網頁端執行即可產生webSocket Client端的code.
9408
	#僅適用於https網頁透過wss連線到web socket server.
9408
	#僅適用於https網頁透過wss連線到web socket server.
9409
	*/
9409
	*/
9410
	public static function soldierDemo(&$conf){
9410
	public static function soldierDemo(&$conf){
9411
		
9411
 
9412
		#初始化要回傳的結果
9412
		#初始化要回傳的結果
9413
		$result=array();
9413
		$result=array();
9414
 
9414
 
9415
		#取得當前執行的函數名稱
9415
		#取得當前執行的函數名稱
9416
		$result["function"]=__FUNCTION__;
9416
		$result["function"]=__FUNCTION__;
Line 9418... Line 9418...
9418
		#涵式說明:
9418
		#涵式說明:
9419
		#判斷當前環境為web還是cmd
9419
		#判斷當前環境為web還是cmd
9420
		#回傳結果:
9420
		#回傳結果:
9421
		#$result,"web"或"cmd"
9421
		#$result,"web"或"cmd"
9422
		if(csInformation::getEnv()==="web"){
9422
		if(csInformation::getEnv()==="web"){
9423
			
9423
 
9424
			#建立 web socket client 的 js 語法
9424
			#建立 web socket client 的 js 語法
9425
			#函式說明:
9425
			#函式說明:
9426
			#聊天室用戶端js範例
9426
			#聊天室用戶端js範例
9427
			#回傳結果:
9427
			#回傳結果:
9428
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
9428
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
9429
			#$reuslt["error"],執行不正常結束的錯訊息陣列.
9429
			#$reuslt["error"],執行不正常結束的錯訊息陣列.
9430
			#$result["function"],當前執行的函式名稱.		
9430
			#$result["function"],當前執行的函式名稱.
9431
			#$result["content"],js語法.
9431
			#$result["content"],js語法.
9432
			#參考資料:
9432
			#參考資料:
9433
			#Ratchet官網=>http://socketo.me/
9433
			#Ratchet官網=>http://socketo.me/
9434
			#聊天室範例=>http://socketo.me/docs/hello-world
9434
			#聊天室範例=>http://socketo.me/docs/hello-world
9435
			$soldierClientDemo=webSock::soldierClientDemo();
9435
			$soldierClientDemo=webSock::soldierClientDemo();
9436
			
9436
 
9437
			#如果建立聊天室用戶端失敗
9437
			#如果建立聊天室用戶端失敗
9438
			if($soldierClientDemo["status"]==="false"){
9438
			if($soldierClientDemo["status"]==="false"){
9439
				
9439
 
9440
				#設置執行失敗
9440
				#設置執行失敗
9441
				$result["status"]="false";
9441
				$result["status"]="false";
9442
				
9442
 
9443
				#設置執行錯誤訊息
9443
				#設置執行錯誤訊息
9444
				$result["error"]=$soldierClientDemo;
9444
				$result["error"]=$soldierClientDemo;
9445
				
9445
 
9446
				#回傳結果
9446
				#回傳結果
9447
				return $result;
9447
				return $result;
9448
				
9448
 
9449
				}#if end
9449
				}#if end
9450
				
9450
 
9451
			#取得聊天室的語法
9451
			#取得聊天室的語法
9452
			$result["content"]=$soldierClientDemo["content"];
9452
			$result["content"]=$soldierClientDemo["content"];
9453
			
9453
 
9454
			#設置執行正常
9454
			#設置執行正常
9455
			$result["status"]="true";
9455
			$result["status"]="true";
9456
			
9456
 
9457
			#回傳結果
9457
			#回傳結果
9458
			return $result;
9458
			return $result;
9459
			
9459
 
9460
			}#if end
9460
			}#if end
9461
 
9461
 
9462
		#如果沒有參數
9462
		#如果沒有參數
9463
		if(func_num_args()==0){
9463
		if(func_num_args()==0){
9464
			
9464
 
9465
			#設置執行失敗
9465
			#設置執行失敗
9466
			$result["status"]="false";
9466
			$result["status"]="false";
9467
			
9467
 
9468
			#設置執行錯誤訊息
9468
			#設置執行錯誤訊息
9469
			$result["error"]="函數".$result["function"]."需要參數";
9469
			$result["error"]="函數".$result["function"]."需要參數";
9470
			
9470
 
9471
			#回傳結果
9471
			#回傳結果
9472
			return $result;
9472
			return $result;
9473
			
9473
 
9474
			}#if end
9474
			}#if end
9475
			
9475
 
9476
		#如果沒有參數
9476
		#如果沒有參數
9477
		if(func_num_args()==0){
9477
		if(func_num_args()==0){
9478
			
9478
 
9479
			#設置執行失敗
9479
			#設置執行失敗
9480
			$result["status"]="false";
9480
			$result["status"]="false";
9481
			
9481
 
9482
			#設置執行錯誤訊息
9482
			#設置執行錯誤訊息
9483
			$result["error"]="函數".$result["function"]."需要參數";
9483
			$result["error"]="函數".$result["function"]."需要參數";
9484
			
9484
 
9485
			#回傳結果
9485
			#回傳結果
9486
			return $result;
9486
			return $result;
9487
			
9487
 
9488
			}#if end
9488
			}#if end
9489
			
9489
 
9490
		#取得參數
9490
		#取得參數
9491
		$result["argu"]=$conf;
9491
		$result["argu"]=$conf;
9492
 
9492
 
9493
		#如果 $conf 不為陣列
9493
		#如果 $conf 不為陣列
9494
		if(gettype($conf)!=="array"){
9494
		if(gettype($conf)!=="array"){
9495
			
9495
 
9496
			#設置執行失敗
9496
			#設置執行失敗
9497
			$result["status"]="false";
9497
			$result["status"]="false";
9498
			
9498
 
9499
			#設置執行錯誤訊息
9499
			#設置執行錯誤訊息
9500
			$result["error"][]="\$conf變數須為陣列形態";
9500
			$result["error"][]="\$conf變數須為陣列形態";
9501
			
9501
 
9502
			#如果傳入的參數為 null
9502
			#如果傳入的參數為 null
9503
			if($conf===null){
9503
			if($conf===null){
9504
				
9504
 
9505
				#設置執行錯誤訊息
9505
				#設置執行錯誤訊息
9506
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
9506
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
9507
				
9507
 
9508
				}#if end
9508
				}#if end
9509
 
9509
 
9510
			#回傳結果
9510
			#回傳結果
9511
			return $result;
9511
			return $result;
9512
			
9512
 
9513
			}#if end
9513
			}#if end
9514
		
9514
 
9515
		#檢查參數
9515
		#檢查參數
9516
		#函式說明:
9516
		#函式說明:
9517
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
9517
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
9518
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
9518
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
9519
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
9519
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
Line 9544... Line 9544...
9544
		#$conf["canBeEmpty"]=array();
9544
		#$conf["canBeEmpty"]=array();
9545
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
9545
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
9546
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array();
9546
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array();
9547
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
9547
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
9548
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("disReg");
9548
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("disReg");
9549
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double"); 
9549
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
9550
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string");
9550
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string");
9551
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
9551
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
9552
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("false");
9552
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("false");
9553
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
9553
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
9554
		#$conf["arrayCountEqualCheck"][]=array();
9554
		#$conf["arrayCountEqualCheck"][]=array();
Line 9556... Line 9556...
9556
		#array_keys=>http://php.net/manual/en/function.array-keys.php
9556
		#array_keys=>http://php.net/manual/en/function.array-keys.php
9557
		#建議:
9557
		#建議:
9558
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
9558
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
9559
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
9559
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
9560
		unset($conf["variableCheck::checkArguments"]);
9560
		unset($conf["variableCheck::checkArguments"]);
9561
		
9561
 
9562
		#如果檢查參數失敗
9562
		#如果檢查參數失敗
9563
		if($checkArguments["status"]==="false"){
9563
		if($checkArguments["status"]==="false"){
9564
			
9564
 
9565
			#設置執行失敗
9565
			#設置執行失敗
9566
			$result["status"]="false";
9566
			$result["status"]="false";
9567
		
9567
 
9568
			#設置執行錯誤訊息
9568
			#設置執行錯誤訊息
9569
			$result["error"]=$checkArguments;
9569
			$result["error"]=$checkArguments;
9570
			
9570
 
9571
			#回傳結果
9571
			#回傳結果
9572
			return $result;
9572
			return $result;
9573
			
9573
 
9574
			}#if end
9574
			}#if end
9575
			
9575
 
9576
		#如果參數檢查不通過
9576
		#如果參數檢查不通過
9577
		if($checkArguments["passed"]==="false"){
9577
		if($checkArguments["passed"]==="false"){
9578
			
9578
 
9579
			#設置執行失敗
9579
			#設置執行失敗
9580
			$result["status"]="false";
9580
			$result["status"]="false";
9581
		
9581
 
9582
			#設置執行錯誤訊息
9582
			#設置執行錯誤訊息
9583
			$result["error"]=$checkArguments;
9583
			$result["error"]=$checkArguments;
9584
			
9584
 
9585
			#回傳結果
9585
			#回傳結果
9586
			return $result;
9586
			return $result;
9587
			
9587
 
9588
			}#if end
9588
			}#if end
9589
 
9589
 
9590
		#執行到這邊代表是命令列環境
9590
		#執行到這邊代表是命令列環境
9591
		
9591
 
9592
		#如果沒有要取消設定 passProxy
9592
		#如果沒有要取消設定 passProxy
9593
		if($conf["disReg"]==="false"){
9593
		if($conf["disReg"]==="false"){
9594
			
9594
 
9595
			#確認設定 passProxy
9595
			#確認設定 passProxy
9596
			#函式說明:
9596
			#函式說明:
9597
			#註冊聊天室範例的 passProxy 設定,本函數會用root帳戶去新增.
9597
			#註冊聊天室範例的 passProxy 設定,本函數會用root帳戶去新增.
9598
			#會在 /etc/httpd/conf/httpd.conf 檔案裡面加上以下內容
9598
			#會在 /etc/httpd/conf/httpd.conf 檔案裡面加上以下內容
9599
			##Enable the mod_proxy modules in the HTTPD
9599
			##Enable the mod_proxy modules in the HTTPD
Line 9612... Line 9612...
9612
			$conf["webSock::registerChatDemo"]["fileArgu"]=$conf["fileArgu"];
9612
			$conf["webSock::registerChatDemo"]["fileArgu"]=$conf["fileArgu"];
9613
			#可省略參數:
9613
			#可省略參數:
9614
			#$conf["rootPasswd"],字串,root帳戶的密碼,預設為"password".
9614
			#$conf["rootPasswd"],字串,root帳戶的密碼,預設為"password".
9615
			#$conf["rootPasswd"]="password";
9615
			#$conf["rootPasswd"]="password";
9616
			#$conf["httpd.conf"],字串,apache設定檔的路徑與名稱,預設為"/etc/httpd/conf/httpd.conf".
9616
			#$conf["httpd.conf"],字串,apache設定檔的路徑與名稱,預設為"/etc/httpd/conf/httpd.conf".
9617
			#$conf["httpd.conf"]="/etc/httpd/conf/httpd.conf";		
9617
			#$conf["httpd.conf"]="/etc/httpd/conf/httpd.conf";
9618
			#備註:
9618
			#備註:
9619
			#僅能在命令列環境下執行.
9619
			#僅能在命令列環境下執行.
9620
			#改變暫存檔案權限的功能異常.
9620
			#改變暫存檔案權限的功能異常.
9621
			#參考資料:
9621
			#參考資料:
9622
			#用apache的passProxy來識別服務並轉到正確的port=>https://groups.google.com/forum/#!topic/ratchet-php/dj-PgPPO_J0
9622
			#用apache的passProxy來識別服務並轉到正確的port=>https://groups.google.com/forum/#!topic/ratchet-php/dj-PgPPO_J0
9623
			$registerChatDemo=webSock::registerChatDemo($conf["webSock::registerChatDemo"]);
9623
			$registerChatDemo=webSock::registerChatDemo($conf["webSock::registerChatDemo"]);
9624
			unset($conf["webSock::registerChatDemo"]);
9624
			unset($conf["webSock::registerChatDemo"]);
9625
			
9625
 
9626
			#如果註冊 ChatDemo 服務失敗
9626
			#如果註冊 ChatDemo 服務失敗
9627
			if($registerChatDemo["status"]==="false"){
9627
			if($registerChatDemo["status"]==="false"){
9628
				
9628
 
9629
				#設置執行失敗
9629
				#設置執行失敗
9630
				$result["status"]="false";
9630
				$result["status"]="false";
9631
				
9631
 
9632
				#設置執行錯誤訊息
9632
				#設置執行錯誤訊息
9633
				$result["error"]=$registerChatDemo;
9633
				$result["error"]=$registerChatDemo;
9634
				
9634
 
9635
				#回傳結果
9635
				#回傳結果
9636
				return $result;
9636
				return $result;
9637
				
9637
 
9638
				}#if end
9638
				}#if end
9639
			
9639
 
9640
			}#if end
9640
			}#if end
9641
				
9641
 
9642
		#函式說明:
9642
		#函式說明:
9643
		#擴充過的Ratchet聊天室伺服端範例,port為8080.
9643
		#擴充過的Ratchet聊天室伺服端範例,port為8080.
9644
		#回傳結果:
9644
		#回傳結果:
9645
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
9645
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
9646
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
9646
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
9647
		#$result["function"],當前執行的函式名稱.		
9647
		#$result["function"],當前執行的函式名稱.
9648
		#參考資料:
9648
		#參考資料:
9649
		#Ratchet官網=>http://socketo.me/
9649
		#Ratchet官網=>http://socketo.me/
9650
		#聊天室範例=>http://socketo.me/docs/hello-world
9650
		#聊天室範例=>http://socketo.me/docs/hello-world
9651
		#備註:
9651
		#備註:
9652
		#必須要在命令列環境下執行才行.
9652
		#必須要在命令列環境下執行才行.
9653
		$conf["webSock"]["soldierServerDemo"]["fileArgu"]=$conf["fileArgu"];
9653
		$conf["webSock"]["soldierServerDemo"]["fileArgu"]=$conf["fileArgu"];
9654
		$soldierServerDemo=webSock::soldierServerDemo($conf["webSock"]["soldierServerDemo"]);
9654
		$soldierServerDemo=webSock::soldierServerDemo($conf["webSock"]["soldierServerDemo"]);
9655
		unset($conf["webSock"]["soldierServerDemo"]);	
9655
		unset($conf["webSock"]["soldierServerDemo"]);
9656
			
9656
 
9657
		#如果運行 chatRoomServerDemo 失敗
9657
		#如果運行 chatRoomServerDemo 失敗
9658
		if($soldierServerDemo["status"]==="false"){
9658
		if($soldierServerDemo["status"]==="false"){
9659
			
9659
 
9660
			#設置執行失敗
9660
			#設置執行失敗
9661
			$result["status"]="false";
9661
			$result["status"]="false";
9662
			
9662
 
9663
			#設置執行錯誤訊息
9663
			#設置執行錯誤訊息
9664
			$result["error"]=$soldierServerDemo;
9664
			$result["error"]=$soldierServerDemo;
9665
			
9665
 
9666
			#回傳結果
9666
			#回傳結果
9667
			return $result;
9667
			return $result;
9668
			
9668
 
9669
			}#if end	
9669
			}#if end
9670
			
9670
 
9671
		#設置執行正常
9671
		#設置執行正常
9672
		$result["status"]="true";
9672
		$result["status"]="true";
9673
			
9673
 
9674
		#回傳結果	
9674
		#回傳結果
9675
		return $result;
9675
		return $result;
9676
		
9676
 
9677
		}#function soldierDemo end
9677
		}#function soldierDemo end
9678
		
9678
 
9679
	/*
9679
	/*
9680
	#函式說明:
9680
	#函式說明:
9681
	#註冊Ratchet聊天室範例的 passProxy 設定,本函數會用root帳戶去新增.
9681
	#註冊Ratchet聊天室範例的 passProxy 設定,本函數會用root帳戶去新增.
9682
	#會在 /etc/httpd/conf/httpd.conf 檔案裡面加上以下內容
9682
	#會在 /etc/httpd/conf/httpd.conf 檔案裡面加上以下內容
9683
	##Enable the mod_proxy modules in the HTTPD
9683
	##Enable the mod_proxy modules in the HTTPD
Line 9696... Line 9696...
9696
	$conf["fileArgu"]=__FILE__;
9696
	$conf["fileArgu"]=__FILE__;
9697
	#可省略參數:
9697
	#可省略參數:
9698
	#$conf["rootPasswd"],字串,root帳戶的密碼,預設為"password".
9698
	#$conf["rootPasswd"],字串,root帳戶的密碼,預設為"password".
9699
	#$conf["rootPasswd"]="password";
9699
	#$conf["rootPasswd"]="password";
9700
	#$conf["httpd.conf"],字串,apache設定檔的路徑與名稱,預設為"/etc/httpd/conf/httpd.conf".
9700
	#$conf["httpd.conf"],字串,apache設定檔的路徑與名稱,預設為"/etc/httpd/conf/httpd.conf".
9701
	#$conf["httpd.conf"]="/etc/httpd/conf/httpd.conf";	
9701
	#$conf["httpd.conf"]="/etc/httpd/conf/httpd.conf";
9702
	#備註:
9702
	#備註:
9703
	#僅能在命令列環境下執行.
9703
	#僅能在命令列環境下執行.
9704
	#改變暫存檔案權限的功能異常.
9704
	#改變暫存檔案權限的功能異常.
9705
	#參考資料:
9705
	#參考資料:
9706
	#用apache的passProxy來識別服務並轉到正確的port=>https://groups.google.com/forum/#!topic/ratchet-php/dj-PgPPO_J0
9706
	#用apache的passProxy來識別服務並轉到正確的port=>https://groups.google.com/forum/#!topic/ratchet-php/dj-PgPPO_J0
9707
	*/
9707
	*/
9708
	public static function registerChatDemo(&$conf){
9708
	public static function registerChatDemo(&$conf){
9709
		
9709
 
9710
		#初始化要回傳的結果
9710
		#初始化要回傳的結果
9711
		$result=array();
9711
		$result=array();
9712
 
9712
 
9713
		#取得當前執行的函數名稱
9713
		#取得當前執行的函數名稱
9714
		$result["function"]=__FUNCTION__;
9714
		$result["function"]=__FUNCTION__;
Line 9716... Line 9716...
9716
		#涵式說明:
9716
		#涵式說明:
9717
		#判斷當前環境為web還是cmd
9717
		#判斷當前環境為web還是cmd
9718
		#回傳結果:
9718
		#回傳結果:
9719
		#$result,"web"或"cmd"
9719
		#$result,"web"或"cmd"
9720
		if(csInformation::getEnv()=="web"){
9720
		if(csInformation::getEnv()=="web"){
9721
			
9721
 
9722
			#設置執行失敗
9722
			#設置執行失敗
9723
			$result["status"]="false";
9723
			$result["status"]="false";
9724
			
9724
 
9725
			#設置執行錯誤訊息
9725
			#設置執行錯誤訊息
9726
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
9726
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
9727
			
9727
 
9728
			#回傳結果
9728
			#回傳結果
9729
			return $result;
9729
			return $result;
9730
			
9730
 
9731
			}#if end
9731
			}#if end
9732
		
9732
 
9733
		#取得參數
9733
		#取得參數
9734
		$result["argu"]=$conf;
9734
		$result["argu"]=$conf;
9735
 
9735
 
9736
		#如果 $conf 不為陣列
9736
		#如果 $conf 不為陣列
9737
		if(gettype($conf)!="array"){
9737
		if(gettype($conf)!="array"){
9738
		
9738
 
9739
			#設置執行失敗
9739
			#設置執行失敗
9740
			$result["status"]="false";
9740
			$result["status"]="false";
9741
		
9741
 
9742
			#設置執行錯誤訊息
9742
			#設置執行錯誤訊息
9743
			$result["error"][]="\$conf變數須為陣列形態";
9743
			$result["error"][]="\$conf變數須為陣列形態";
9744
		
9744
 
9745
			#如果傳入的參數為 null
9745
			#如果傳入的參數為 null
9746
			if($conf==null){
9746
			if($conf==null){
9747
			
9747
 
9748
				#設置執行錯誤訊息
9748
				#設置執行錯誤訊息
9749
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
9749
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
9750
			
9750
 
9751
				}#if end
9751
				}#if end
9752
 
9752
 
9753
			#回傳結果
9753
			#回傳結果
9754
			return $result;
9754
			return $result;
9755
		
9755
 
9756
			}#if end
9756
			}#if end
9757
		
9757
 
9758
		#檢查參數
9758
		#檢查參數
9759
		#函式說明:
9759
		#函式說明:
9760
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
9760
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
9761
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
9761
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
9762
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
9762
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
Line 9787... Line 9787...
9787
		#$conf["canBeEmpty"]=array();
9787
		#$conf["canBeEmpty"]=array();
9788
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
9788
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
9789
		#$conf["skipableVariableCanNotBeEmpty"]=array();
9789
		#$conf["skipableVariableCanNotBeEmpty"]=array();
9790
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
9790
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
9791
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("httpd.conf","rootPasswd");
9791
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("httpd.conf","rootPasswd");
9792
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double"); 
9792
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
9793
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string");
9793
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string");
9794
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
9794
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
9795
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("/etc/httpd/conf/httpd.conf","password");
9795
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("/etc/httpd/conf/httpd.conf","password");
9796
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
9796
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
9797
		#$conf["arrayCountEqualCheck"][]=array();
9797
		#$conf["arrayCountEqualCheck"][]=array();
Line 9799... Line 9799...
9799
		#array_keys=>http://php.net/manual/en/function.array-keys.php
9799
		#array_keys=>http://php.net/manual/en/function.array-keys.php
9800
		#建議:
9800
		#建議:
9801
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
9801
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
9802
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
9802
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
9803
		unset($conf["variableCheck::checkArguments"]);
9803
		unset($conf["variableCheck::checkArguments"]);
9804
		
9804
 
9805
		#如果檢查參數失敗
9805
		#如果檢查參數失敗
9806
		if($checkArguments["status"]==="false"){
9806
		if($checkArguments["status"]==="false"){
9807
			
9807
 
9808
			#設置執行失敗
9808
			#設置執行失敗
9809
			$result["status"]="false";
9809
			$result["status"]="false";
9810
			
9810
 
9811
			#設置執行錯誤訊息
9811
			#設置執行錯誤訊息
9812
			$result["error"]=$checkArguments;
9812
			$result["error"]=$checkArguments;
9813
			
9813
 
9814
			#回傳結果
9814
			#回傳結果
9815
			return $result;
9815
			return $result;
9816
			
9816
 
9817
			}#if end
9817
			}#if end
9818
			
9818
 
9819
		#如果參數檢查不通過
9819
		#如果參數檢查不通過
9820
		if($checkArguments["passed"]==="false"){
9820
		if($checkArguments["passed"]==="false"){
9821
			
9821
 
9822
			#設置執行失敗
9822
			#設置執行失敗
9823
			$result["status"]="false";
9823
			$result["status"]="false";
9824
			
9824
 
9825
			#設置執行錯誤訊息
9825
			#設置執行錯誤訊息
9826
			$result["error"]=$checkArguments;
9826
			$result["error"]=$checkArguments;
9827
			
9827
 
9828
			#回傳結果
9828
			#回傳結果
9829
			return $result;
9829
			return $result;
9830
			
9830
 
9831
			}#if end
9831
			}#if end
9832
				
9832
 
9833
		#取得root密碼
9833
		#取得root密碼
9834
		#函式說明:
9834
		#函式說明:
9835
		#藉由使用者的輸入來確認root使用者密碼是否正確.
9835
		#藉由使用者的輸入來確認root使用者密碼是否正確.
9836
		#回傳結果:
9836
		#回傳結果:
9837
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常. 
9837
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
9838
		#$result["function"],當前執行的函數名稱.
9838
		#$result["function"],當前執行的函數名稱.
9839
		#$result["error"],錯誤訊息陣列.
9839
		#$result["error"],錯誤訊息陣列.
9840
		#$result["content"],root密碼.		
9840
		#$result["content"],root密碼.
9841
		#必填參數:
9841
		#必填參數:
9842
		#$conf["fileArgu"],字串,__FILE__的內容.
9842
		#$conf["fileArgu"],字串,__FILE__的內容.
9843
		$conf["cmd::validRootPasswd"]["fileArgu"]=$conf["fileArgu"];
9843
		$conf["cmd::validRootPasswd"]["fileArgu"]=$conf["fileArgu"];
9844
		#可省略參數:
9844
		#可省略參數:
9845
		#$conf["password"],字串,用於驗證root使用者的密碼,若未設定則預設為"rootPasswd".
9845
		#$conf["password"],字串,用於驗證root使用者的密碼,若未設定則預設為"rootPasswd".
9846
		$conf["cmd::validRootPasswd"]["password"]=$conf["rootPasswd"];
9846
		$conf["cmd::validRootPasswd"]["password"]=$conf["rootPasswd"];
9847
		#備註:
9847
		#備註:
9848
		#建構中
9848
		#建構中
9849
		$validRootPasswd=cmd::validRootPasswd($conf["cmd::validRootPasswd"]);
9849
		$validRootPasswd=cmd::validRootPasswd($conf["cmd::validRootPasswd"]);
9850
		unset($conf["cmd::validRootPasswd"]);
9850
		unset($conf["cmd::validRootPasswd"]);
9851
		
9851
 
9852
		#如果取得root密碼失敗
9852
		#如果取得root密碼失敗
9853
		if($validRootPasswd["status"]==="false"){
9853
		if($validRootPasswd["status"]==="false"){
9854
			
9854
 
9855
			#設置執行失敗
9855
			#設置執行失敗
9856
			$result["status"]="false";
9856
			$result["status"]="false";
9857
			
9857
 
9858
			#設置執行錯誤訊息
9858
			#設置執行錯誤訊息
9859
			$result["error"]=$validRootPasswd;
9859
			$result["error"]=$validRootPasswd;
9860
			
9860
 
9861
			#回傳結果
9861
			#回傳結果
9862
			return $result;
9862
			return $result;
9863
			
9863
 
9864
			}#if end
9864
			}#if end
9865
			
9865
 
9866
		#取得root密碼
9866
		#取得root密碼
9867
		$conf["rootPasswd"]=$validRootPasswd["content"];
9867
		$conf["rootPasswd"]=$validRootPasswd["content"];
9868
				
9868
 
9869
		#檢查httpd設定檔案有無設定載入 proxy 模組與設定 ProxyPass
9869
		#檢查httpd設定檔案有無設定載入 proxy 模組與設定 ProxyPass
9870
		#函式說明:
9870
		#函式說明:
9871
		#取得檔的內容並以列為單位搜尋多個關鍵字.
9871
		#取得檔的內容並以列為單位搜尋多個關鍵字.
9872
		#回傳結果:
9872
		#回傳結果:
9873
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
9873
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 9878... Line 9878...
9878
		#$result["foundedLine"],每個找到的關鍵字是在第幾列找到的,key值與$result["founded"]的key值一樣.
9878
		#$result["foundedLine"],每個找到的關鍵字是在第幾列找到的,key值與$result["founded"]的key值一樣.
9879
		#必填的參數:
9879
		#必填的參數:
9880
		#$conf["file"],字串,檔案的位置與名稱.
9880
		#$conf["file"],字串,檔案的位置與名稱.
9881
		$conf["cmd::checkFileKeyStrLBL"]["file"]=$conf["httpd.conf"];
9881
		$conf["cmd::checkFileKeyStrLBL"]["file"]=$conf["httpd.conf"];
9882
		#$conf["keyStr"],陣列字串,要搜尋每列有哪些關鍵字串.
9882
		#$conf["keyStr"],陣列字串,要搜尋每列有哪些關鍵字串.
9883
		$conf["cmd::checkFileKeyStrLBL"]["keyStr"]=array("LoadModule proxy_module modules/mod_proxy.so","LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so","ProxyPass /wss/chatDemo ws://localhost:8080");	
9883
		$conf["cmd::checkFileKeyStrLBL"]["keyStr"]=array("LoadModule proxy_module modules/mod_proxy.so","LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so","ProxyPass /wss/chatDemo ws://localhost:8080");
9884
		#可省略參數:
9884
		#可省略參數:
9885
		#$conf["username"],字串,要以哪個使用者來執行,預設為執行該php程式的使用者.
9885
		#$conf["username"],字串,要以哪個使用者來執行,預設為執行該php程式的使用者.
9886
		$conf["cmd::checkFileKeyStrLBL"]["username"]="root";
9886
		$conf["cmd::checkFileKeyStrLBL"]["username"]="root";
9887
		#$conf["password"],字串,使用者對應的密碼,預設不使用.
9887
		#$conf["password"],字串,使用者對應的密碼,預設不使用.
9888
		$conf["cmd::checkFileKeyStrLBL"]["password"]=$conf["rootPasswd"];
9888
		$conf["cmd::checkFileKeyStrLBL"]["password"]=$conf["rootPasswd"];
Line 9890... Line 9890...
9890
		$conf["cmd::checkFileKeyStrLBL"]["fileArgu"]=$conf["fileArgu"];
9890
		$conf["cmd::checkFileKeyStrLBL"]["fileArgu"]=$conf["fileArgu"];
9891
		#$conf["tempDir"],字串,暫存資料的目錄位置,預設為".cmd/checkFileKeyStrLBL".
9891
		#$conf["tempDir"],字串,暫存資料的目錄位置,預設為".cmd/checkFileKeyStrLBL".
9892
		#$conf["tempDir"]="";
9892
		#$conf["tempDir"]="";
9893
		$checkFileKeyStrLBL=cmd::checkFileKeyStrLBL($conf["cmd::checkFileKeyStrLBL"]);
9893
		$checkFileKeyStrLBL=cmd::checkFileKeyStrLBL($conf["cmd::checkFileKeyStrLBL"]);
9894
		unset($conf["cmd::checkFileKeyStrLBL"]);
9894
		unset($conf["cmd::checkFileKeyStrLBL"]);
9895
		
9895
 
9896
		#如果檢查設定值失敗
9896
		#如果檢查設定值失敗
9897
		if($checkFileKeyStrLBL["status"]==="false"){
9897
		if($checkFileKeyStrLBL["status"]==="false"){
9898
			
9898
 
9899
			#設置執行失敗
9899
			#設置執行失敗
9900
			$result["status"]="false";
9900
			$result["status"]="false";
9901
			
9901
 
9902
			#設置執行錯誤訊息
9902
			#設置執行錯誤訊息
9903
			$result["error"]=$checkFileKeyStrLBL;
9903
			$result["error"]=$checkFileKeyStrLBL;
9904
			
9904
 
9905
			#回傳結果
9905
			#回傳結果
9906
			return $result;
9906
			return $result;
9907
			
9907
 
9908
			}#if end
9908
			}#if end
9909
		
9909
 
9910
		#初始化要加入的設定
9910
		#初始化要加入的設定
9911
		$settingStr=array("LoadModule proxy_module modules/mod_proxy.so","LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so","ProxyPass /wss/chatDemo ws://localhost:8080");
9911
		$settingStr=array("LoadModule proxy_module modules/mod_proxy.so","LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so","ProxyPass /wss/chatDemo ws://localhost:8080");
9912
		
9912
 
9913
		#針對每個關鍵字串列
9913
		#針對每個關鍵字串列
9914
		foreach($checkFileKeyStrLBL["founded"] as $index=>$status){
9914
		foreach($checkFileKeyStrLBL["founded"] as $index=>$status){
9915
			
9915
 
9916
			#如果沒有找到符合的設定
9916
			#如果沒有找到符合的設定
9917
			if($status==="true"){
9917
			if($status==="true"){
9918
				
9918
 
9919
				#移除要增加到設定檔的變數
9919
				#移除要增加到設定檔的變數
9920
				unset($settingStr[$index]);
9920
				unset($settingStr[$index]);
9921
				
9921
 
9922
				}#if end
9922
				}#if end
9923
			
9923
 
9924
			}#foreach end		
9924
			}#foreach end
9925
			
9925
 
9926
		#如果有要增加的設定
9926
		#如果有要增加的設定
9927
		if(count($settingStr)>0){
9927
		if(count($settingStr)>0){
9928
			
9928
 
9929
			#加入設定到 httpd 設定檔裡面
9929
			#加入設定到 httpd 設定檔裡面
9930
			#函式說明:
9930
			#函式說明:
9931
			#新增文字內容到檔案裡面
9931
			#新增文字內容到檔案裡面
9932
			#回傳結果:
9932
			#回傳結果:
9933
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
9933
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 9936... Line 9936...
9936
			#$result["content"],新增好後的檔案內容.
9936
			#$result["content"],新增好後的檔案內容.
9937
			#必填的參數:
9937
			#必填的參數:
9938
			#$conf["file"],字串,檔案的位置與名稱.
9938
			#$conf["file"],字串,檔案的位置與名稱.
9939
			$conf["cmd::addTextToFile"]["file"]=$conf["httpd.conf"];
9939
			$conf["cmd::addTextToFile"]["file"]=$conf["httpd.conf"];
9940
			#$conf["addStr"],陣列字串,要新增的字串內容.
9940
			#$conf["addStr"],陣列字串,要新增的字串內容.
9941
			$conf["cmd::addTextToFile"]["addStr"]=$settingStr;	
9941
			$conf["cmd::addTextToFile"]["addStr"]=$settingStr;
9942
			#可省略參數:
9942
			#可省略參數:
9943
			#$conf["username"],字串,要以哪個使用者來執行,預設為執行該php程式的使用者.
9943
			#$conf["username"],字串,要以哪個使用者來執行,預設為執行該php程式的使用者.
9944
			$conf["cmd::addTextToFile"]["username"]="root";
9944
			$conf["cmd::addTextToFile"]["username"]="root";
9945
			#$conf["password"],字串,使用者對應的密碼,預設為"password".
9945
			#$conf["password"],字串,使用者對應的密碼,預設為"password".
9946
			$conf["cmd::addTextToFile"]["password"]=$conf["rootPasswd"];
9946
			$conf["cmd::addTextToFile"]["password"]=$conf["rootPasswd"];
9947
			#$conf["fileArgu"],字串,__FILE__的內容,預設為當前檔案的位置.
9947
			#$conf["fileArgu"],字串,__FILE__的內容,預設為當前檔案的位置.
9948
			$conf["cmd::addTextToFile"]["fileArgu"]=$conf["fileArgu"];
9948
			$conf["cmd::addTextToFile"]["fileArgu"]=$conf["fileArgu"];
9949
			$addTextToFile=cmd::addTextToFile($conf["cmd::addTextToFile"]);
9949
			$addTextToFile=cmd::addTextToFile($conf["cmd::addTextToFile"]);
9950
			unset($conf["cmd::addTextToFile"]);
9950
			unset($conf["cmd::addTextToFile"]);
9951
			
9951
 
9952
			#如果新增內容失敗
9952
			#如果新增內容失敗
9953
			if($addTextToFile["status"]==="false"){
9953
			if($addTextToFile["status"]==="false"){
9954
				
9954
 
9955
				#設置執行失敗
9955
				#設置執行失敗
9956
				$result["status"]="false";
9956
				$result["status"]="false";
9957
				
9957
 
9958
				#設置執行錯誤訊息
9958
				#設置執行錯誤訊息
9959
				$result["error"]=$addTextToFile;
9959
				$result["error"]=$addTextToFile;
9960
				
9960
 
9961
				#回傳結果
9961
				#回傳結果
9962
				return $result;
9962
				return $result;
9963
				
9963
 
9964
				}#if end
9964
				}#if end
9965
			
9965
 
9966
			#檢查是否增加成功
9966
			#檢查是否增加成功
9967
			#檢查httpd設定檔案有無設定載入 proxy 模組與設定 ProxyPass
9967
			#檢查httpd設定檔案有無設定載入 proxy 模組與設定 ProxyPass
9968
			#函式說明:
9968
			#函式說明:
9969
			#取得檔的內容並以列為單位搜尋多個關鍵字.
9969
			#取得檔的內容並以列為單位搜尋多個關鍵字.
9970
			#回傳結果:
9970
			#回傳結果:
Line 9988... Line 9988...
9988
			$conf["cmd::checkFileKeyStrLBL"]["fileArgu"]=$conf["fileArgu"];
9988
			$conf["cmd::checkFileKeyStrLBL"]["fileArgu"]=$conf["fileArgu"];
9989
			#$conf["tempDir"],字串,暫存資料的目錄位置,預設為".cmd/checkFileKeyStrLBL".
9989
			#$conf["tempDir"],字串,暫存資料的目錄位置,預設為".cmd/checkFileKeyStrLBL".
9990
			#$conf["tempDir"]="";
9990
			#$conf["tempDir"]="";
9991
			$checkFileKeyStrLBL=cmd::checkFileKeyStrLBL($conf["cmd::checkFileKeyStrLBL"]);
9991
			$checkFileKeyStrLBL=cmd::checkFileKeyStrLBL($conf["cmd::checkFileKeyStrLBL"]);
9992
			unset($conf["cmd::checkFileKeyStrLBL"]);
9992
			unset($conf["cmd::checkFileKeyStrLBL"]);
9993
			
9993
 
9994
			#如果檢查設定值失敗
9994
			#如果檢查設定值失敗
9995
			if($checkFileKeyStrLBL["status"]==="false"){
9995
			if($checkFileKeyStrLBL["status"]==="false"){
9996
				
9996
 
9997
				#設置執行失敗
9997
				#設置執行失敗
9998
				$result["status"]="false";
9998
				$result["status"]="false";
9999
				
9999
 
10000
				#設置執行錯誤訊息
10000
				#設置執行錯誤訊息
10001
				$result["error"]=$checkFileKeyStrLBL;
10001
				$result["error"]=$checkFileKeyStrLBL;
10002
				
10002
 
10003
				#回傳結果
10003
				#回傳結果
10004
				return $result;
10004
				return $result;
10005
				
10005
 
10006
				}#if end
10006
				}#if end
10007
				
10007
 
10008
			#如果沒有找到該有設定字串
10008
			#如果沒有找到該有設定字串
10009
			if($checkFileKeyStrLBL["founded"]==="false"){
10009
			if($checkFileKeyStrLBL["founded"]==="false"){
10010
				
10010
 
10011
				#設置執行失敗
10011
				#設置執行失敗
10012
				$result["status"]="false";
10012
				$result["status"]="false";
10013
				
10013
 
10014
				#設置執行錯誤訊息
10014
				#設置執行錯誤訊息
10015
				$result["error"]=$checkFileKeyStrLBL;
10015
				$result["error"]=$checkFileKeyStrLBL;
10016
				
10016
 
10017
				#設置錯誤訊息
10017
				#設置錯誤訊息
10018
				$result["error"][]="寫入設定值失敗";
10018
				$result["error"][]="寫入設定值失敗";
10019
				
10019
 
10020
				#回傳結果
10020
				#回傳結果
10021
				return $result;
10021
				return $result;
10022
				
10022
 
10023
				}#if end
10023
				}#if end
10024
				
10024
 
10025
			#重新啟動 httpd 伺服器
10025
			#重新啟動 httpd 伺服器
10026
			#函式說明:
10026
			#函式說明:
10027
			#呼叫shell執行系統命令,並取得回傳的內容.
10027
			#呼叫shell執行系統命令,並取得回傳的內容.
10028
			#回傳的結果:
10028
			#回傳的結果:
10029
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
10029
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 10063... Line 10063...
10063
			#exec=>http://php.net/manual/en/function.exec.php
10063
			#exec=>http://php.net/manual/en/function.exec.php
10064
			#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
10064
			#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
10065
			#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
10065
			#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
10066
			$callShell=external::callShell($conf["external::callShell"]);
10066
			$callShell=external::callShell($conf["external::callShell"]);
10067
			unset($conf["external::callShell"]);
10067
			unset($conf["external::callShell"]);
10068
			
10068
 
10069
			#如果重新啟動apache失敗
10069
			#如果重新啟動apache失敗
10070
			if($callShell["status"]==="false"){
10070
			if($callShell["status"]==="false"){
10071
				
10071
 
10072
				#設置執行失敗
10072
				#設置執行失敗
10073
				$result["status"]="false";
10073
				$result["status"]="false";
10074
				
10074
 
10075
				#設置執行錯誤訊息
10075
				#設置執行錯誤訊息
10076
				$result["error"]=$callShell;
10076
				$result["error"]=$callShell;
10077
				
10077
 
10078
				#回傳結果
10078
				#回傳結果
10079
				return $result;
10079
				return $result;
10080
				
10080
 
10081
				}#if end
10081
				}#if end
10082
			
10082
 
10083
			}#if end
10083
			}#if end
10084
			
10084
 
10085
		#設置執行正常
10085
		#設置執行正常
10086
		$result["status"]="true";
10086
		$result["status"]="true";
10087
		
10087
 
10088
		#回傳結果
10088
		#回傳結果
10089
		return $result;
10089
		return $result;
10090
		
10090
 
10091
		}#function registerChatDemo end
10091
		}#function registerChatDemo end
10092
		
10092
 
10093
	/*
10093
	/*
10094
	#函式說明:
10094
	#函式說明:
10095
	#建立php原生的socket tcp/ip server,提供webSocket server支援.
10095
	#建立php原生的socket tcp/ip server,提供webSocket server支援.
10096
	#回傳結果:
10096
	#回傳結果:
10097
	#$result["status"],執行正常與否,"true"代表正常,"false"代表不正常.
10097
	#$result["status"],執行正常與否,"true"代表正常,"false"代表不正常.
Line 10122... Line 10122...
10122
	#備註:
10122
	#備註:
10123
	#僅能在命令列執行.
10123
	#僅能在命令列執行.
10124
	#runPhpWhenIdle的功能尚未實作.
10124
	#runPhpWhenIdle的功能尚未實作.
10125
	*/
10125
	*/
10126
	public static function nativeSocketTcpIpServer(&$conf){
10126
	public static function nativeSocketTcpIpServer(&$conf){
10127
		
10127
 
10128
		#初始化要回傳的結果
10128
		#初始化要回傳的結果
10129
		$result=array();
10129
		$result=array();
10130
 
10130
 
10131
		#取得當前執行的函數名稱
10131
		#取得當前執行的函數名稱
10132
		$result["function"]=__FUNCTION__;
10132
		$result["function"]=__FUNCTION__;
Line 10134... Line 10134...
10134
		#涵式說明:
10134
		#涵式說明:
10135
		#判斷當前環境為web還是cmd
10135
		#判斷當前環境為web還是cmd
10136
		#回傳結果:
10136
		#回傳結果:
10137
		#$result,"web"或"cmd"
10137
		#$result,"web"或"cmd"
10138
		if(csInformation::getEnv()==="web"){
10138
		if(csInformation::getEnv()==="web"){
10139
			
10139
 
10140
			#設置執行失敗
10140
			#設置執行失敗
10141
			$result["status"]="false";
10141
			$result["status"]="false";
10142
			
10142
 
10143
			#設置執行錯誤訊息
10143
			#設置執行錯誤訊息
10144
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
10144
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
10145
			
10145
 
10146
			#回傳結果
10146
			#回傳結果
10147
			return $result;
10147
			return $result;
10148
			
10148
 
10149
			}#if end
10149
			}#if end
10150
 
10150
 
10151
		#取得參數
10151
		#取得參數
10152
		$result["argu"]=$conf;
10152
		$result["argu"]=$conf;
10153
 
10153
 
10154
		#如果 $conf 不為陣列
10154
		#如果 $conf 不為陣列
10155
		if(gettype($conf)!="array"){
10155
		if(gettype($conf)!="array"){
10156
		
10156
 
10157
			#設置執行失敗
10157
			#設置執行失敗
10158
			$result["status"]="false";
10158
			$result["status"]="false";
10159
		
10159
 
10160
			#設置執行錯誤訊息
10160
			#設置執行錯誤訊息
10161
			$result["error"][]="\$conf變數須為陣列形態";
10161
			$result["error"][]="\$conf變數須為陣列形態";
10162
		
10162
 
10163
			#如果傳入的參數為 null
10163
			#如果傳入的參數為 null
10164
			if($conf==null){
10164
			if($conf==null){
10165
			
10165
 
10166
				#設置執行錯誤訊息
10166
				#設置執行錯誤訊息
10167
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
10167
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
10168
			
10168
 
10169
				}#if end
10169
				}#if end
10170
 
10170
 
10171
			#回傳結果
10171
			#回傳結果
10172
			return $result;
10172
			return $result;
10173
		
10173
 
10174
			}#if end
10174
			}#if end
10175
		
10175
 
10176
		#檢查參數
10176
		#檢查參數
10177
		#函式說明:
10177
		#函式說明:
10178
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
10178
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
10179
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
10179
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
10180
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
10180
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
Line 10205... Line 10205...
10205
		#$conf["canBeEmpty"]=array();
10205
		#$conf["canBeEmpty"]=array();
10206
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
10206
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
10207
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array();
10207
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array();
10208
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
10208
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
10209
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("listenIp","listenPort","wsMode","processFuncs","idleFunc");
10209
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("listenIp","listenPort","wsMode","processFuncs","idleFunc");
10210
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double"); 
10210
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
10211
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string","string","array","array");
10211
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string","string","array","array");
10212
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
10212
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
10213
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("127.0.0.1",null,"false",array("noAuth"),array());
10213
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("127.0.0.1",null,"false",array("noAuth"),array());
10214
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
10214
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
10215
		#$conf["arrayCountEqualCheck"][]=array();
10215
		#$conf["arrayCountEqualCheck"][]=array();
Line 10217... Line 10217...
10217
		#array_keys=>http://php.net/manual/en/function.array-keys.php
10217
		#array_keys=>http://php.net/manual/en/function.array-keys.php
10218
		#建議:
10218
		#建議:
10219
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
10219
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
10220
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
10220
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
10221
		unset($conf["variableCheck::checkArguments"]);
10221
		unset($conf["variableCheck::checkArguments"]);
10222
				
10222
 
10223
		#如果檢查參數失敗
10223
		#如果檢查參數失敗
10224
		if($checkArguments["status"]==="false"){
10224
		if($checkArguments["status"]==="false"){
10225
			
10225
 
10226
			#設置執行失敗
10226
			#設置執行失敗
10227
			$result["status"]="false";
10227
			$result["status"]="false";
10228
		
10228
 
10229
			#設置執行錯誤訊息
10229
			#設置執行錯誤訊息
10230
			$result["error"]=$checkArguments;
10230
			$result["error"]=$checkArguments;
10231
			
10231
 
10232
			#回傳結果
10232
			#回傳結果
10233
			return $result;
10233
			return $result;
10234
			
10234
 
10235
			}#if end
10235
			}#if end
10236
			
10236
 
10237
		#如果檢查參數不通過
10237
		#如果檢查參數不通過
10238
		if($checkArguments["passed"]==="false"){
10238
		if($checkArguments["passed"]==="false"){
10239
			
10239
 
10240
			#設置執行失敗
10240
			#設置執行失敗
10241
			$result["status"]="false";
10241
			$result["status"]="false";
10242
		
10242
 
10243
			#設置執行錯誤訊息
10243
			#設置執行錯誤訊息
10244
			$result["error"]=$checkArguments;
10244
			$result["error"]=$checkArguments;
10245
			
10245
 
10246
			#回傳結果
10246
			#回傳結果
10247
			return $result;
10247
			return $result;
10248
			
10248
 
10249
			}#if end
10249
			}#if end
10250
		
10250
 
10251
		#如果 $conf["listenPort"] 不存在
10251
		#如果 $conf["listenPort"] 不存在
10252
		if(!isset($conf["listenPort"])){
10252
		if(!isset($conf["listenPort"])){
10253
			
10253
 
10254
			#用nmap掃port
10254
			#用nmap掃port
10255
			#函式說明:
10255
			#函式說明:
10256
			#掃port的程式
10256
			#掃port的程式
10257
			#回傳結果:
10257
			#回傳結果:
10258
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
10258
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 10267... Line 10267...
10267
			$conf["cmd::nmap"]["target"]=$conf["listenIp"];
10267
			$conf["cmd::nmap"]["target"]=$conf["listenIp"];
10268
			#$conf["-Pn"],字串,是否要啟用避免ping不到就會掃port失敗的功能,預設為"false".
10268
			#$conf["-Pn"],字串,是否要啟用避免ping不到就會掃port失敗的功能,預設為"false".
10269
			#$conf["-Pn"]="false";
10269
			#$conf["-Pn"]="false";
10270
			$nmap=cmd::nmap($conf["cmd::nmap"]);
10270
			$nmap=cmd::nmap($conf["cmd::nmap"]);
10271
			unset($conf["cmd::nmap"]);
10271
			unset($conf["cmd::nmap"]);
10272
			
10272
 
10273
			#如果掃port失敗
10273
			#如果掃port失敗
10274
			if($nmap["status"]==="false"){
10274
			if($nmap["status"]==="false"){
10275
				
10275
 
10276
				#設置執行失敗
10276
				#設置執行失敗
10277
				$result["status"]="false";
10277
				$result["status"]="false";
10278
			
10278
 
10279
				#設置執行錯誤訊息
10279
				#設置執行錯誤訊息
10280
				$result["error"]=$nmap;
10280
				$result["error"]=$nmap;
10281
				
10281
 
10282
				#回傳結果
10282
				#回傳結果
10283
				return $result;
10283
				return $result;
10284
				
10284
 
10285
				}#if end
10285
				}#if end
10286
				
10286
 
10287
			#如果有掃到port
10287
			#如果有掃到port
10288
			if(count($nmap["content"])>0){
10288
			if(count($nmap["content"])>0){
10289
				
10289
 
10290
				#找最大的port
10290
				#找最大的port
10291
				#涵式說明:
10291
				#涵式說明:
10292
				#找最大值,結果會回傳最大值.
10292
				#找最大值,結果會回傳最大值.
10293
				#回傳的結果:
10293
				#回傳的結果:
10294
				#$result["status"],執行是否正常,"true"為正常,"false"為不正常.
10294
				#$result["status"],執行是否正常,"true"為正常,"false"為不正常.
Line 10298... Line 10298...
10298
				#$result["value"],最大值的數字.
10298
				#$result["value"],最大值的數字.
10299
				#必填的參數:
10299
				#必填的參數:
10300
				$conf["math::getMaxValue"]["rawDataArray"]=$nmap["content"];#爲原始數據
10300
				$conf["math::getMaxValue"]["rawDataArray"]=$nmap["content"];#爲原始數據
10301
				$getMaxValue=math::getMaxValue($conf["math::getMaxValue"]);
10301
				$getMaxValue=math::getMaxValue($conf["math::getMaxValue"]);
10302
				unset($conf["math::getMaxValue"]);
10302
				unset($conf["math::getMaxValue"]);
10303
				
10303
 
10304
				#如果找最大port失敗
10304
				#如果找最大port失敗
10305
				if($getMaxValue["status"]==="false"){
10305
				if($getMaxValue["status"]==="false"){
10306
					
10306
 
10307
					#設置執行失敗
10307
					#設置執行失敗
10308
					$result["status"]="false";
10308
					$result["status"]="false";
10309
				
10309
 
10310
					#設置執行錯誤訊息
10310
					#設置執行錯誤訊息
10311
					$result["error"]=$getMaxValue;
10311
					$result["error"]=$getMaxValue;
10312
					
10312
 
10313
					#回傳結果
10313
					#回傳結果
10314
					return $result;
10314
					return $result;
10315
					
10315
 
10316
					}#if end
10316
					}#if end
10317
					
10317
 
10318
				#設置 $conf["listenPort"] 為最大 port+1
10318
				#設置 $conf["listenPort"] 為最大 port+1
10319
				$conf["listenPort"]=$getMaxValue["value"]+1;
10319
				$conf["listenPort"]=$getMaxValue["value"]+1;
10320
				
10320
 
10321
				#如果port小於4000,會無法讓nmap偵測到有使用
10321
				#如果port小於4000,會無法讓nmap偵測到有使用
10322
				if($conf["listenPort"]<4000){
10322
				if($conf["listenPort"]<4000){
10323
					
10323
 
10324
					#設為4000
10324
					#設為4000
10325
					$conf["listenPort"]="4000";
10325
					$conf["listenPort"]="4000";
10326
					
10326
 
10327
					}#if end
10327
					}#if end
10328
				
10328
 
10329
				}#if end
10329
				}#if end
10330
				
10330
 
10331
			}#if end
10331
			}#if end
10332
			
10332
 
10333
		#反之
10333
		#反之
10334
		else{
10334
		else{
10335
			
10335
 
10336
			#用nmap掃port
10336
			#用nmap掃port
10337
			#函式說明:
10337
			#函式說明:
10338
			#掃port的程式
10338
			#掃port的程式
10339
			#回傳結果:
10339
			#回傳結果:
10340
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
10340
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 10351... Line 10351...
10351
			#$conf["-Pn"]="false";
10351
			#$conf["-Pn"]="false";
10352
			#$conf["fileArgu"],字串,變數__FILE__的內容.
10352
			#$conf["fileArgu"],字串,變數__FILE__的內容.
10353
			$conf["cmd::nmap"]["fileArgu"]=$conf["fileArgu"];
10353
			$conf["cmd::nmap"]["fileArgu"]=$conf["fileArgu"];
10354
			$nmap=cmd::nmap($conf["cmd::nmap"]);
10354
			$nmap=cmd::nmap($conf["cmd::nmap"]);
10355
			unset($conf["cmd::nmap"]);
10355
			unset($conf["cmd::nmap"]);
10356
			
10356
 
10357
			#如果掃port失敗
10357
			#如果掃port失敗
10358
			if($nmap["status"]==="false"){
10358
			if($nmap["status"]==="false"){
10359
				
10359
 
10360
				#設置執行失敗
10360
				#設置執行失敗
10361
				$result["status"]="false";
10361
				$result["status"]="false";
10362
			
10362
 
10363
				#設置執行錯誤訊息
10363
				#設置執行錯誤訊息
10364
				$result["error"]=$nmap;
10364
				$result["error"]=$nmap;
10365
				
10365
 
10366
				#回傳結果
10366
				#回傳結果
10367
				return $result;
10367
				return $result;
10368
				
10368
 
10369
				}#if end
10369
				}#if end
10370
				
10370
 
10371
			#如果有掃到port
10371
			#如果有掃到port
10372
			if(count($nmap["content"])>0){
10372
			if(count($nmap["content"])>0){
10373
				
10373
 
10374
				#如果要用來建立socket的port已被使用
10374
				#如果要用來建立socket的port已被使用
10375
				if(in_array($conf["listenPort"],$nmap["content"])){
10375
				if(in_array($conf["listenPort"],$nmap["content"])){
10376
					
10376
 
10377
					#找最大的port
10377
					#找最大的port
10378
					#涵式說明:
10378
					#涵式說明:
10379
					#找最大值,結果會回傳最大值.
10379
					#找最大值,結果會回傳最大值.
10380
					#回傳的結果:
10380
					#回傳的結果:
10381
					#$result["status"],執行是否正常,"true"為正常,"false"為不正常.
10381
					#$result["status"],執行是否正常,"true"為正常,"false"為不正常.
Line 10385... Line 10385...
10385
					#$result["value"],最大值的數字.
10385
					#$result["value"],最大值的數字.
10386
					#必填的參數:
10386
					#必填的參數:
10387
					$conf["math::getMaxValue"]["rawDataArray"]=$nmap["content"];#爲原始數據
10387
					$conf["math::getMaxValue"]["rawDataArray"]=$nmap["content"];#爲原始數據
10388
					$getMaxValue=math::getMaxValue($conf["math::getMaxValue"]);
10388
					$getMaxValue=math::getMaxValue($conf["math::getMaxValue"]);
10389
					unset($conf["math::getMaxValue"]);
10389
					unset($conf["math::getMaxValue"]);
10390
					
10390
 
10391
					#如果找最大port失敗
10391
					#如果找最大port失敗
10392
					if($getMaxValue["status"]==="false"){
10392
					if($getMaxValue["status"]==="false"){
10393
						
10393
 
10394
						#設置執行失敗
10394
						#設置執行失敗
10395
						$result["status"]="false";
10395
						$result["status"]="false";
10396
					
10396
 
10397
						#設置執行錯誤訊息
10397
						#設置執行錯誤訊息
10398
						$result["error"]=$getMaxValue;
10398
						$result["error"]=$getMaxValue;
10399
						
10399
 
10400
						#回傳結果
10400
						#回傳結果
10401
						return $result;
10401
						return $result;
10402
						
10402
 
10403
						}#if end
10403
						}#if end
10404
						
10404
 
10405
					#設置 $conf["listenPort"] 為最大 port+1
10405
					#設置 $conf["listenPort"] 為最大 port+1
10406
					$conf["listenPort"]=$getMaxValue["value"]+1;
10406
					$conf["listenPort"]=$getMaxValue["value"]+1;
10407
					
10407
 
10408
					#如果port小於4000,會無法讓nmap偵測到有使用
10408
					#如果port小於4000,會無法讓nmap偵測到有使用
10409
					if($conf["listenPort"]<4000){
10409
					if($conf["listenPort"]<4000){
10410
						
10410
 
10411
						#設為4000
10411
						#設為4000
10412
						$conf["listenPort"]="4000";
10412
						$conf["listenPort"]="4000";
10413
						
10413
 
10414
						}#if end
10414
						}#if end
10415
					
10415
 
10416
					}#if end
10416
					}#if end
10417
				
10417
 
10418
				}#if end
10418
				}#if end
10419
			
10419
 
10420
			}#else end
10420
			}#else end
10421
		
10421
 
10422
		#回報任何錯誤
10422
		#回報任何錯誤
10423
		#error_reporting(E_ALL);
10423
		#error_reporting(E_ALL);
10424
 
10424
 
10425
		/* Allow the script to hang around waiting for connections. */
10425
		/* Allow the script to hang around waiting for connections. */
10426
		set_time_limit(0);
10426
		set_time_limit(0);
Line 10428... Line 10428...
10428
		/* Turn on implicit output flushing so we see what we're getting as it comes in. */
10428
		/* Turn on implicit output flushing so we see what we're getting as it comes in. */
10429
		#ob_implicit_flush();
10429
		#ob_implicit_flush();
10430
 
10430
 
10431
		#設定要listen的主機ip
10431
		#設定要listen的主機ip
10432
		$address = $conf["listenIp"];
10432
		$address = $conf["listenIp"];
10433
		
10433
 
10434
		#設定要listen的port
10434
		#設定要listen的port
10435
		$port = $conf["listenPort"];
10435
		$port = $conf["listenPort"];
10436
 
10436
 
10437
		#重複做,讓socket服務不會結束.
10437
		#重複做,讓socket服務不會結束.
10438
		do{
10438
		do{
10439
			
10439
 
10440
			#如果建立 socket 失敗
10440
			#如果建立 socket 失敗
10441
			if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false){
10441
			if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false){
10442
				
10442
 
10443
				#印出錯誤訊息
10443
				#印出錯誤訊息
10444
				echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
10444
				echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
10445
				
10445
 
10446
				}#if end
10446
				}#if end
10447
			
10447
 
10448
			#設置不等待連線與訊息的到來,變成不會卡位
10448
			#設置不等待連線與訊息的到來,變成不會卡位
10449
			socket_set_nonblock($sock);
10449
			socket_set_nonblock($sock);
10450
			
10450
 
10451
			#SOL_SOCKET => socket level
10451
			#SOL_SOCKET => socket level
10452
			#SO_REUSEADDR => Reports whether local addresses can be reused. 
10452
			#SO_REUSEADDR => Reports whether local addresses can be reused.
10453
			socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);
10453
			socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);
10454
			
10454
 
10455
			#如果 Binds a name to a socket 失敗
10455
			#如果 Binds a name to a socket 失敗
10456
			if (@socket_bind($sock, $address, $port) === false){
10456
			if (@socket_bind($sock, $address, $port) === false){
10457
				
10457
 
10458
				#儲存錯誤訊息
10458
				#儲存錯誤訊息
10459
				$error=socket_strerror(socket_last_error($sock));
10459
				$error=socket_strerror(socket_last_error($sock));
10460
				
10460
 
10461
				#印出錯誤訊息
10461
				#印出錯誤訊息
10462
				echo "socket_bind() failed: reason: " .$error. PHP_EOL;
10462
				echo "socket_bind() failed: reason: " .$error. PHP_EOL;
10463
				
10463
 
10464
				#判斷錯誤訊息的是否有 "Address already in use"
10464
				#判斷錯誤訊息的是否有 "Address already in use"
10465
				#函式說明:
10465
				#函式說明:
10466
				#檢查字串裡面有無指定的關鍵字
10466
				#檢查字串裡面有無指定的關鍵字
10467
				#回傳的結果:
10467
				#回傳的結果:
10468
				#$result["status"],"true"代表執行成功,"false"代表執行失敗。
10468
				#$result["status"],"true"代表執行成功,"false"代表執行失敗。
Line 10475... Line 10475...
10475
				$conf["search::findKeyWord"]["string"]=$error;#要被搜尋的字串內容
10475
				$conf["search::findKeyWord"]["string"]=$error;#要被搜尋的字串內容
10476
				#可省略的參數:
10476
				#可省略的參數:
10477
				#$conf["completeEqual"]="true";#是否內容要完全符合,不能多出任何不符合的內容,預設為不需要完全符合。
10477
				#$conf["completeEqual"]="true";#是否內容要完全符合,不能多出任何不符合的內容,預設為不需要完全符合。
10478
				$findKeyWord=search::findKeyWord($conf["search::findKeyWord"]);
10478
				$findKeyWord=search::findKeyWord($conf["search::findKeyWord"]);
10479
				unset($conf["search::findKeyWord"]);
10479
				unset($conf["search::findKeyWord"]);
10480
				
10480
 
10481
				#如果解析錯誤訊息失敗
10481
				#如果解析錯誤訊息失敗
10482
				if($findKeyWord["status"]==="false"){
10482
				if($findKeyWord["status"]==="false"){
10483
					
10483
 
10484
					#設置執行失敗
10484
					#設置執行失敗
10485
					$result["status"]="false";
10485
					$result["status"]="false";
10486
				
10486
 
10487
					#設置執行錯誤訊息
10487
					#設置執行錯誤訊息
10488
					$result["error"]=$getMeetConditionsString;
10488
					$result["error"]=$getMeetConditionsString;
10489
					
10489
 
10490
					#回傳結果
10490
					#回傳結果
10491
					return $result;
10491
					return $result;
10492
					
10492
 
10493
					}#if end
10493
					}#if end
10494
					
10494
 
10495
				#如果錯誤原因為socket的port已經被使用了.
10495
				#如果錯誤原因為socket的port已經被使用了.
10496
				if($findKeyWord["founded"]==="true"){
10496
				if($findKeyWord["founded"]==="true"){
10497
					
10497
 
10498
					#port加1
10498
					#port加1
10499
					$port++;
10499
					$port++;
10500
					
10500
 
10501
					#再次建立socket服務
10501
					#再次建立socket服務
10502
					continue;
10502
					continue;
10503
					
10503
 
10504
					}#if end
10504
					}#if end
10505
					
10505
 
10506
				#反之
10506
				#反之
10507
				else{
10507
				else{
10508
					
10508
 
10509
					#結束程式
10509
					#結束程式
10510
					exit;
10510
					exit;
10511
					
10511
 
10512
					}#else 
10512
					}#else
10513
				
10513
 
10514
				}#if end
10514
				}#if end
10515
 
10515
 
10516
			#如果 listen socket 經過嘗試5次後仍然失敗
10516
			#如果 listen socket 經過嘗試5次後仍然失敗
10517
			if (socket_listen($sock, 5) === false) {
10517
			if (socket_listen($sock, 5) === false) {
10518
				
10518
 
10519
				#印出錯誤訊息
10519
				#印出錯誤訊息
10520
				echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . PHP_EOL;
10520
				echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . PHP_EOL;
10521
			
10521
 
10522
				}#if end
10522
				}#if end
10523
 
10523
 
10524
			#初始化儲存已經連線到 socket service 的服務
10524
			#初始化儲存已經連線到 socket service 的服務
10525
			$connections=array();
10525
			$connections=array();
10526
 
10526
 
Line 10530... Line 10530...
10530
			#提示 listen 的 ip 與 port
10530
			#提示 listen 的 ip 與 port
10531
			echo "Listen to:".$address.":".$port.PHP_EOL;
10531
			echo "Listen to:".$address.":".$port.PHP_EOL;
10532
 
10532
 
10533
			#重複做,等待別人連線.
10533
			#重複做,等待別人連線.
10534
			do{
10534
			do{
10535
				
10535
 
10536
				#初始化儲存有需要處理的socket連線
10536
				#初始化儲存有需要處理的socket連線
10537
				$read = array();
10537
				$read = array();
10538
				
10538
 
10539
				#初始化一開始建立的socket為要處理的項目
10539
				#初始化一開始建立的socket為要處理的項目
10540
				$read[] = $sock;
10540
				$read[] = $sock;
10541
			
10541
 
10542
				#有幾個連線就執行幾次
10542
				#有幾個連線就執行幾次
10543
				foreach($connections as $conForSockSelect){
10543
				foreach($connections as $conForSockSelect){
10544
					
10544
 
10545
					#產生要合併的陣列
10545
					#產生要合併的陣列
10546
					$arrayToMerage=array($conForSockSelect["connect"]);
10546
					$arrayToMerage=array($conForSockSelect["connect"]);
10547
					
10547
 
10548
					#合併陣列
10548
					#合併陣列
10549
					$read = array_merge($read,$arrayToMerage);
10549
					$read = array_merge($read,$arrayToMerage);
10550
					
10550
 
10551
					}#foreach end
10551
					}#foreach end
10552
			
10552
 
10553
				#從socket物件陣列($read)裡面取出有需要處理連線的socket物件.
10553
				#從socket物件陣列($read)裡面取出有需要處理連線的socket物件.
10554
				#Set up a blocking call to socket_select
10554
				#Set up a blocking call to socket_select
10555
				$write=array();
10555
				$write=array();
10556
				$except=array();
10556
				$except=array();
10557
				$tv_sec = 1;
10557
				$tv_sec = 1;
10558
				if(socket_select($read, $write, $except, $tv_sec) < 1){
10558
				if(socket_select($read, $write, $except, $tv_sec) < 1){
10559
				
10559
 
10560
					#沒有事情發生
10560
					#沒有事情發生
10561
					echo ".";
10561
					echo ".";
10562
					
10562
 
10563
					#函式說明:
10563
					#函式說明:
10564
					#加密 handshake 後要傳送的訊息 
10564
					#加密 handshake 後要傳送的訊息
10565
					#回傳結果:
10565
					#回傳結果:
10566
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
10566
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
10567
					#$result["error"],執行不正常結束的錯訊息陣列.
10567
					#$result["error"],執行不正常結束的錯訊息陣列.
10568
					#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
10568
					#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
10569
					#$result["argu"],陣列,使用的參數.
10569
					#$result["argu"],陣列,使用的參數.
10570
					#必填參數:
10570
					#必填參數:
10571
					#$conf["text"],字串,要加密的訊息.
10571
					#$conf["text"],字串,要加密的訊息.
10572
					$conf["webSock::encode"]["text"]=""; 
10572
					$conf["webSock::encode"]["text"]="";
10573
					#可省略參數:
10573
					#可省略參數:
10574
					#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
10574
					#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
10575
					$conf["webSock::encode"]["ping"]="true";
10575
					$conf["webSock::encode"]["ping"]="true";
10576
					#$conf["frames"],陣列,目前既有的訊息frame,預設為空陣列,代表沒有.
10576
					#$conf["frames"],陣列,目前既有的訊息frame,預設為空陣列,代表沒有.
10577
					#$conf["frames"]=array();
10577
					#$conf["frames"]=array();
Line 10579... Line 10579...
10579
					#https://www.rfc-editor.org/rfc/rfc6455#page-28, Web Socket Base Framing Protocol.
10579
					#https://www.rfc-editor.org/rfc/rfc6455#page-28, Web Socket Base Framing Protocol.
10580
					#備註:
10580
					#備註:
10581
					#無.
10581
					#無.
10582
					$encode=webSock::encode($conf["webSock::encode"]);
10582
					$encode=webSock::encode($conf["webSock::encode"]);
10583
					unset($conf["webSock::encode"]);
10583
					unset($conf["webSock::encode"]);
10584
					
10584
 
10585
					#debug
10585
					#debug
10586
					#var_dump(__LINE__,$encode);
10586
					#var_dump(__LINE__,$encode);
10587
					
10587
 
10588
					#如果執行失敗
10588
					#如果執行失敗
10589
					if($encode["status"]==="false"){
10589
					if($encode["status"]==="false"){
10590
					
10590
 
10591
						#設置執行失敗
10591
						#設置執行失敗
10592
						$result["status"]="false";
10592
						$result["status"]="false";
10593
						
10593
 
10594
						#設置執行錯誤訊息
10594
						#設置執行錯誤訊息
10595
						$result["error"]=$encode;
10595
						$result["error"]=$encode;
10596
						
10596
 
10597
						#回傳結果
10597
						#回傳結果
10598
						return $result;
10598
						return $result;
10599
					
10599
 
10600
						}#if end
10600
						}#if end
10601
					
10601
 
10602
					#針對每個連線的 client
10602
					#針對每個連線的 client
10603
					foreach($connections as $client){
10603
					foreach($connections as $client){
10604
					
10604
 
10605
						#如果有 last ping time
10605
						#如果有 last ping time
10606
						if(isset($client["lastPongTime"])){
10606
						if(isset($client["lastPongTime"])){
10607
						
10607
 
10608
							#如果小於5秒沒有收到pong
10608
							#如果小於5秒沒有收到pong
10609
							if(time()-$client["lastPongTime"]<5){
10609
							if(time()-$client["lastPongTime"]<5){
10610
							
10610
 
10611
								#看下一個連線,亦即視為用戶已經斷線.
10611
								#看下一個連線,亦即視為用戶已經斷線.
10612
								continue;
10612
								continue;
10613
							
10613
 
10614
								}#if end
10614
								}#if end
10615
						
10615
 
10616
							}#if end
10616
							}#if end
10617
					
10617
 
10618
						#取得用戶socket
10618
						#取得用戶socket
10619
						$clientSock=$client["connect"];
10619
						$clientSock=$client["connect"];
10620
					
10620
 
10621
						#傳送ping給client
10621
						#傳送ping給client
10622
						$socket_write=socket_write($clientSock, $encode["content"][0], strlen($encode["content"][0]));
10622
						$socket_write=socket_write($clientSock, $encode["content"][0], strlen($encode["content"][0]));
10623
				
10623
 
10624
						#debug
10624
						#debug
10625
						#var_dump(__LINE__,"send ping",$socket_write,socket_strerror(socket_last_error($clientSock)));
10625
						#var_dump(__LINE__,"send ping",$socket_write,socket_strerror(socket_last_error($clientSock)));
10626
					
10626
 
10627
						}#foreach end
10627
						}#foreach end
10628
					
10628
 
10629
					#如果有idle時要做的事情
10629
					#如果有idle時要做的事情
10630
					if(count($conf["idleFunc"])>0){
10630
					if(count($conf["idleFunc"])>0){
10631
					
10631
 
10632
						#取得idleFunc設定
10632
						#取得idleFunc設定
10633
						$idleFunc=$conf["idleFunc"];
10633
						$idleFunc=$conf["idleFunc"];
10634
						
10634
 
10635
						#移除初始的idelFunc設定
10635
						#移除初始的idelFunc設定
10636
						unset($conf["idleFunc"]);
10636
						unset($conf["idleFunc"]);
10637
					
10637
 
10638
						}#if end
10638
						}#if end
10639
					
10639
 
10640
					#如果有idle時要做的事情
10640
					#如果有idle時要做的事情
10641
					if(count($idleFunc)>0){
10641
					if(count($idleFunc)>0){
10642
					
10642
 
10643
						#針對每個要執行的函式
10643
						#針對每個要執行的函式
10644
						foreach($idleFunc as $if){
10644
						foreach($idleFunc as $if){
10645
						
10645
 
10646
							#設置要給予函式的參數陣列
10646
							#設置要給予函式的參數陣列
10647
							$param=array("idleFunc"=>&$idleFunc,"serverSock"=>&$sock,"allConn"=>&$connections);
10647
							$param=array("idleFunc"=>&$idleFunc,"serverSock"=>&$sock,"allConn"=>&$connections);
10648
							
10648
 
10649
							#初始化給予 call_user_func_array 的第2個參數
10649
							#初始化給予 call_user_func_array 的第2個參數
10650
							$params=array();
10650
							$params=array();
10651
							
10651
 
10652
							#設置呼叫函式 $if 時要傳遞的參數為一個 $param
10652
							#設置呼叫函式 $if 時要傳遞的參數為一個 $param
10653
							$params[]=&$param;
10653
							$params[]=&$param;
10654
							
10654
 
10655
							#呼叫函式 $if 與要傳遞的參數.
10655
							#呼叫函式 $if 與要傳遞的參數.
10656
							$result=call_user_func_array($if,$params);
10656
							$result=call_user_func_array($if,$params);
10657
					
10657
 
10658
							}#foreach end
10658
							}#foreach end
10659
					
10659
 
10660
						}#if end
10660
						}#if end
10661
					
10661
 
10662
					#如果有 $php2runWhenIdle
10662
					#如果有 $php2runWhenIdle
10663
					if(isset($php2runWhenIdle)){
10663
					if(isset($php2runWhenIdle)){
10664
					
10664
 
10665
						#如果有php程式要於idle時執行
10665
						#如果有php程式要於idle時執行
10666
						while(count($php2runWhenIdle)>0){
10666
						while(count($php2runWhenIdle)>0){
10667
						
10667
 
10668
							#取得要執行的 php code
10668
							#取得要執行的 php code
10669
							$phpCode=array_shift($php2runWhenIdle);
10669
							$phpCode=array_shift($php2runWhenIdle);
10670
						
10670
 
10671
							#...
10671
							#...
10672
						
10672
 
10673
							}#while end
10673
							}#while end
10674
					
10674
 
10675
						}#if end
10675
						}#if end
10676
						
10676
 
10677
					#下一輪
10677
					#下一輪
10678
					continue;
10678
					continue;
10679
					
10679
 
10680
					}#if end
10680
					}#if end
10681
				
10681
 
10682
				#有事情發生
10682
				#有事情發生
10683
				echo "something happen".PHP_EOL;
10683
				echo "something happen".PHP_EOL;
10684
				
10684
 
10685
				#如果有socket要處理
10685
				#如果有socket要處理
10686
				if(in_array($sock,$read)){
10686
				if(in_array($sock,$read)){
10687
					
10687
 
10688
					#如果有接收到 socket 連線
10688
					#如果有接收到 socket 連線
10689
					if (($msgsock = socket_accept($sock)) !== false) {
10689
					if (($msgsock = socket_accept($sock)) !== false) {
10690
						
10690
 
10691
						#初始化不是既有的 sock
10691
						#初始化不是既有的 sock
10692
						$existSock=false;
10692
						$existSock=false;
10693
						
10693
 
10694
						#尋找是哪個 socket
10694
						#尋找是哪個 socket
10695
						for($i=0;$i<count($connections);$i++){
10695
						for($i=0;$i<count($connections);$i++){
10696
							
10696
 
10697
							#如果是既有的 sock
10697
							#如果是既有的 sock
10698
							if($connections[$i]["connect"]===$msgsock){
10698
							if($connections[$i]["connect"]===$msgsock){
10699
								
10699
 
10700
								#設置是既有的 sock
10700
								#設置是既有的 sock
10701
								$existSock=true;
10701
								$existSock=true;
10702
								
10702
 
10703
								}#if end
10703
								}#if end
10704
							
10704
 
10705
							}#for end
10705
							}#for end
10706
							
10706
 
10707
						#如果該 socket 是新的
10707
						#如果該 socket 是新的
10708
						if(!$existSock){
10708
						if(!$existSock){
10709
							
10709
 
10710
							#重複做
10710
							#重複做
10711
							do{
10711
							do{
10712
								
10712
 
10713
								#亂數產生連線id
10713
								#亂數產生連線id
10714
								#涵式說明:
10714
								#涵式說明:
10715
								#建立以圖片(PNG格式)呈現的驗證碼.
10715
								#建立以圖片(PNG格式)呈現的驗證碼.
10716
								#回傳的解果:
10716
								#回傳的解果:
10717
								#$result["status"],執行是否正常,"true"代表執行成功,"false"代表執行失敗.
10717
								#$result["status"],執行是否正常,"true"代表執行成功,"false"代表執行失敗.
Line 10731... Line 10731...
10731
								$conf["authenticate::validationCode"]["disableImg"]="true";
10731
								$conf["authenticate::validationCode"]["disableImg"]="true";
10732
								#$conf["imgToData"],字串,預設為"true"代表將圖片轉存成base64圖片,並將原始圖片移除;反之為"false"
10732
								#$conf["imgToData"],字串,預設為"true"代表將圖片轉存成base64圖片,並將原始圖片移除;反之為"false"
10733
								#$conf["authenticate::validationCode"]["imgToData"]="true";
10733
								#$conf["authenticate::validationCode"]["imgToData"]="true";
10734
								$validationCode=authenticate::validationCode($conf["authenticate::validationCode"]);
10734
								$validationCode=authenticate::validationCode($conf["authenticate::validationCode"]);
10735
								unset($conf["authenticate::validationCode"]);
10735
								unset($conf["authenticate::validationCode"]);
10736
									
10736
 
10737
								#如果產生亂數失敗
10737
								#如果產生亂數失敗
10738
								if($validationCode["status"]==="false"){
10738
								if($validationCode["status"]==="false"){
10739
										
10739
 
10740
									#設置執行失敗
10740
									#設置執行失敗
10741
									$result["status"]="false";
10741
									$result["status"]="false";
10742
									
10742
 
10743
									#設置執行錯誤訊息
10743
									#設置執行錯誤訊息
10744
									$result["error"]=$validationCode;
10744
									$result["error"]=$validationCode;
10745
										
10745
 
10746
									#回傳結果
10746
									#回傳結果
10747
									return $result;
10747
									return $result;
10748
										
10748
 
10749
									}#if end
10749
									}#if end
10750
									
10750
 
10751
								#如果不存在既有的連線資訊
10751
								#如果不存在既有的連線資訊
10752
								if(count($connections)<1){
10752
								if(count($connections)<1){
10753
										
10753
 
10754
									#跳出迴圈
10754
									#跳出迴圈
10755
									break;
10755
									break;
10756
										
10756
 
10757
									}#if end
10757
									}#if end
10758
									
10758
 
10759
								#初始化連線id的陣列
10759
								#初始化連線id的陣列
10760
								$idArray=array();
10760
								$idArray=array();
10761
									
10761
 
10762
								#針對既有的連線
10762
								#針對既有的連線
10763
								foreach($connections as $exist_conn){
10763
								foreach($connections as $exist_conn){
10764
										
10764
 
10765
									#儲存連線id
10765
									#儲存連線id
10766
									$idArray[]=$exist_conn["id"];
10766
									$idArray[]=$exist_conn["id"];
10767
										
10767
 
10768
									}#foreach end
10768
									}#foreach end
10769
									
10769
 
10770
								}#do end
10770
								}#do end
10771
									
10771
 
10772
							#檢查id有無重複
10772
							#檢查id有無重複
10773
							while(in_array($validationCode["randNumberWord"],$idArray));
10773
							while(in_array($validationCode["randNumberWord"],$idArray));
10774
							
10774
 
10775
							#如果不是 ws 模式
10775
							#如果不是 ws 模式
10776
							if($conf["wsMode"]==="false"){
10776
							if($conf["wsMode"]==="false"){
10777
								
10777
 
10778
								#儲存連線到陣列裡面
10778
								#儲存連線到陣列裡面
10779
								$connections[]=array("connect"=>$msgsock,"buf"=>"","id"=>$validationCode["randNumberWord"],"talkTo"=>array());
10779
								$connections[]=array("connect"=>$msgsock,"buf"=>"","id"=>$validationCode["randNumberWord"],"talkTo"=>array());
10780
								
10780
 
10781
								}#if end
10781
								}#if end
10782
							
10782
 
10783
							#如果是 ws 模式
10783
							#如果是 ws 模式
10784
							if($conf["wsMode"]==="true"){
10784
							if($conf["wsMode"]==="true"){
10785
							
10785
 
10786
								#儲存連線到陣列裡面
10786
								#儲存連線到陣列裡面
10787
								$connections[]=array("connect"=>$msgsock,"buf"=>"","id"=>$validationCode["randNumberWord"],"talkTo"=>array(),"handshaked"=>"false","unmask"=>"false","userAgent"=>"false","3rn"=>0,"receivedMsgFromServer"=>array());
10787
								$connections[]=array("connect"=>$msgsock,"buf"=>"","id"=>$validationCode["randNumberWord"],"talkTo"=>array(),"handshaked"=>"false","unmask"=>"false","userAgent"=>"false","3rn"=>0,"receivedMsgFromServer"=>array());
10788
								
10788
 
10789
								}#if end
10789
								}#if end
10790
							
10790
 
10791
							#debug,提示 Server 給予的新 Id
10791
							#debug,提示 Server 給予的新 Id
10792
							echo "new connection:".PHP_EOL;
10792
							echo "new connection:".PHP_EOL;
10793
						
10793
 
10794
							#初始化連線後要回傳的訊息
10794
							#初始化連線後要回傳的訊息
10795
							$msg="";
10795
							$msg="";
10796
							
10796
 
10797
							#如果不是 ws 模式
10797
							#如果不是 ws 模式
10798
							if($conf["wsMode"]!=="true"){
10798
							if($conf["wsMode"]!=="true"){
10799
									
10799
 
10800
								#提示client連上線後得到的訊息
10800
								#提示client連上線後得到的訊息
10801
								$msg = $msg.PHP_EOL."Welcome to the PHP Test Server.".PHP_EOL;
10801
								$msg = $msg.PHP_EOL."Welcome to the PHP Test Server.".PHP_EOL;
10802
								$msg = $msg."To quit, type 'quit'. To shut down the server type 'shutdown'.".PHP_EOL;
10802
								$msg = $msg."To quit, type 'quit'. To shut down the server type 'shutdown'.".PHP_EOL;
10803
								socket_write($msgsock, $msg, strlen($msg));
10803
								socket_write($msgsock, $msg, strlen($msg));
10804
									
10804
 
10805
								}#if end
10805
								}#if end
10806
							
10806
 
10807
							}#if end
10807
							}#if end
10808
							
10808
 
10809
						}#if end
10809
						}#if end
10810
					
10810
 
10811
					}#if end
10811
					}#if end
10812
				
10812
 
10813
				#重新排序 $connections
10813
				#重新排序 $connections
10814
				#函式說明:
10814
				#函式說明:
10815
				#將陣列的key重新排序,然後回傳,以便後續存取,也可以指定key的內容,但一定要跟元素數量相同。
10815
				#將陣列的key重新排序,然後回傳,以便後續存取,也可以指定key的內容,但一定要跟元素數量相同。
10816
				#回傳的解果:
10816
				#回傳的解果:
10817
				#$result["status"],執行是否成功,"true"代表成功,"false"代表失敗.
10817
				#$result["status"],執行是否成功,"true"代表成功,"false"代表失敗.
Line 10824... Line 10824...
10824
				$conf["arrays::createV2"]["arrayContent"]=$connections;#陣列變數
10824
				$conf["arrays::createV2"]["arrayContent"]=$connections;#陣列變數
10825
				#可省略的參數:
10825
				#可省略的參數:
10826
				#$conf["specifiesKeyArray"]=array();#指定key的名稱,須與$conf["arrayContent"]的元素數量相同
10826
				#$conf["specifiesKeyArray"]=array();#指定key的名稱,須與$conf["arrayContent"]的元素數量相同
10827
				$createV2=arrays::createV2($conf["arrays::createV2"]);
10827
				$createV2=arrays::createV2($conf["arrays::createV2"]);
10828
				unset($conf["arrays::createV2"]);
10828
				unset($conf["arrays::createV2"]);
10829
				
10829
 
10830
				#如果重新排序失敗
10830
				#如果重新排序失敗
10831
				if($createV2["status"]==="false"){
10831
				if($createV2["status"]==="false"){
10832
					
10832
 
10833
					#設置執行失敗
10833
					#設置執行失敗
10834
					$result["status"]="false";
10834
					$result["status"]="false";
10835
				
10835
 
10836
					#設置執行錯誤訊息
10836
					#設置執行錯誤訊息
10837
					$result["error"]=$createV2;
10837
					$result["error"]=$createV2;
10838
					
10838
 
10839
					#回傳結果
10839
					#回傳結果
10840
					return $result;
10840
					return $result;
10841
					
10841
 
10842
					}#if end
10842
					}#if end
10843
				
10843
 
10844
				#取得重新排序好的陣列
10844
				#取得重新排序好的陣列
10845
				$connections=$createV2["dataContent"];
10845
				$connections=$createV2["dataContent"];
10846
				
10846
 
10847
				#依據每個 client 的連線
10847
				#依據每個 client 的連線
10848
				foreach($connections as $conIndex => $con){
10848
				foreach($connections as $conIndex => $con){
10849
					
10849
 
10850
					#如果有要處理的socket連線項目
10850
					#如果有要處理的socket連線項目
10851
					if(in_array($con["connect"], $read)){
10851
					if(in_array($con["connect"], $read)){
10852
						
10852
 
10853
						#如果不存在 handshaked
10853
						#如果不存在 handshaked
10854
						if(!isset($con["handshaked"])){
10854
						if(!isset($con["handshaked"])){
10855
						
10855
 
10856
							#預設為 "false"
10856
							#預設為 "false"
10857
							$con["handshaked"]="false";
10857
							$con["handshaked"]="false";
10858
						
10858
 
10859
							}#if end
10859
							}#if end
10860
						
10860
 
10861
						#如果有 handshaked
10861
						#如果有 handshaked
10862
						if($con["handshaked"]==="true"){
10862
						if($con["handshaked"]==="true"){
10863
						
10863
 
10864
							#讀取內容
10864
							#讀取內容
10865
							$buf = @socket_read($con["connect"], 2048, PHP_BINARY_READ);
10865
							$buf = @socket_read($con["connect"], 2048, PHP_BINARY_READ);
10866
						
10866
 
10867
							#設置要 unmask
10867
							#設置要 unmask
10868
							$connections[$conIndex]["unmask"]="true";
10868
							$connections[$conIndex]["unmask"]="true";
10869
							
10869
 
10870
							#設置尚未 auth
10870
							#設置尚未 auth
10871
							$connections[$conIndex]["auth"]="false";
10871
							$connections[$conIndex]["auth"]="false";
10872
						
10872
 
10873
							}#if end
10873
							}#if end
10874
						
10874
 
10875
						#反之
10875
						#反之
10876
						else{
10876
						else{
10877
						
10877
 
10878
							#讀取內容
10878
							#讀取內容
10879
							$buf = @socket_read($con["connect"], 2048, PHP_NORMAL_READ);
10879
							$buf = @socket_read($con["connect"], 2048, PHP_NORMAL_READ);
10880
						
10880
 
10881
							}#else end
10881
							}#else end
10882
						
10882
 
10883
						#如果讀取出錯
10883
						#如果讀取出錯
10884
						if($buf===false){
10884
						if($buf===false){
10885
							
10885
 
10886
							#印出錯誤訊息
10886
							#印出錯誤訊息
10887
							echo "socket_recv() failed: reason: " . socket_strerror(socket_last_error($con["connect"])) . PHP_EOL;
10887
							echo "socket_recv() failed: reason: " . socket_strerror(socket_last_error($con["connect"])) . PHP_EOL;
10888
							
10888
 
10889
							#移除該連線的資料
10889
							#移除該連線的資料
10890
							unset($connections[$conIndex]);	
10890
							unset($connections[$conIndex]);
10891
							
10891
 
10892
							#跳到下一輪
10892
							#跳到下一輪
10893
							continue;
10893
							continue;
10894
							
10894
 
10895
							}#if end
10895
							}#if end
10896
						
10896
 
10897
						#如果收到的資料長度為 0 bytes
10897
						#如果收到的資料長度為 0 bytes
10898
						if($buf===""){
10898
						if($buf===""){
10899
						
10899
 
10900
							#關閉 socket msg
10900
							#關閉 socket msg
10901
							socket_close($con["connect"]);	
10901
							socket_close($con["connect"]);
10902
							
10902
 
10903
							#移除該連線的資料
10903
							#移除該連線的資料
10904
							unset($connections[$conIndex]);
10904
							unset($connections[$conIndex]);
10905
							
10905
 
10906
							#跳到下一輪
10906
							#跳到下一輪
10907
							continue;
10907
							continue;
10908
						
10908
 
10909
							}#if end
10909
							}#if end
10910
							
10910
 
10911
						#印出接受的非換行符號內容
10911
						#印出接受的非換行符號內容
10912
						if($buf!==PHP_EOL){
10912
						if($buf!==PHP_EOL){
10913
						
10913
 
10914
							#debug
10914
							#debug
10915
							echo "received:".$buf.PHP_EOL;
10915
							echo "received:".$buf.PHP_EOL;
10916
							
10916
 
10917
							}#if end
10917
							}#if end
10918
						
10918
 
10919
						#如果無 handshaked
10919
						#如果無 handshaked
10920
						if($con["handshaked"]!=="true"){
10920
						if($con["handshaked"]!=="true"){
10921
						
10921
 
10922
							#清除換行字元
10922
							#清除換行字元
10923
							$buf=trim($buf);
10923
							$buf=trim($buf);
10924
						
10924
 
10925
							}#if end
10925
							}#if end
10926
								
10926
 
10927
						#web socket mode
10927
						#web socket mode
10928
						if($conf["wsMode"]==="true"){
10928
						if($conf["wsMode"]==="true"){
10929
						
10929
 
10930
							#如果不需要 unmask
10930
							#如果不需要 unmask
10931
							if($connections[$conIndex]["unmask"]==="false"){
10931
							if($connections[$conIndex]["unmask"]==="false"){
10932
							
10932
 
10933
								#提示還不用 unmask
10933
								#提示還不用 unmask
10934
								echo "don't need unmask now".PHP_EOL;
10934
								echo "don't need unmask now".PHP_EOL;
10935
							
10935
 
10936
								#重新計數 \r\n
10936
								#重新計數 \r\n
10937
								$connections[$conIndex]["3rn"]=0;
10937
								$connections[$conIndex]["3rn"]=0;
10938
							
10938
 
10939
								}#if end
10939
								}#if end
10940
						
10940
 
10941
							}#if end
10941
							}#if end
10942
						
10942
 
10943
						#如果有內容(不是換行符號)
10943
						#如果有內容(不是換行符號)
10944
						$st=trim($buf);
10944
						$st=trim($buf);
10945
						
10945
 
10946
						#如果是 ws 模式
10946
						#如果是 ws 模式
10947
						if($conf["wsMode"]==="true"){
10947
						if($conf["wsMode"]==="true"){
10948
							
10948
 
10949
							#如果尚未 handshake
10949
							#如果尚未 handshake
10950
							if($connections[$conIndex]["handshaked"]==="false"){
10950
							if($connections[$conIndex]["handshaked"]==="false"){
10951
							
10951
 
10952
								#提示 server 在檢查 handshake 用的 key 是否存在.
10952
								#提示 server 在檢查 handshake 用的 key 是否存在.
10953
								echo "check Sec-WebSocket-Key ..".PHP_EOL;
10953
								echo "check Sec-WebSocket-Key ..".PHP_EOL;
10954
							
10954
 
10955
								#handshake
10955
								#handshake
10956
								#如果收到的內容$buf含有 Sec-WebSocket-Key: 字串開頭,擷取後面的內容存到$matchs裡面.
10956
								#如果收到的內容$buf含有 Sec-WebSocket-Key: 字串開頭,擷取後面的內容存到$matchs裡面.
10957
								if(preg_match("/Sec-WebSocket-Key: (.*)/",$buf,$matchs)){
10957
								if(preg_match("/Sec-WebSocket-Key: (.*)/",$buf,$matchs)){
10958
								
10958
 
10959
									#提示serve在handshake
10959
									#提示serve在handshake
10960
									echo "do handshake...".PHP_EOL.PHP_EOL;
10960
									echo "do handshake...".PHP_EOL.PHP_EOL;
10961
								
10961
 
10962
									#handshake
10962
									#handshake
10963
									$key = $matchs[1].'258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
10963
									$key = $matchs[1].'258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
10964
									$key =  base64_encode(sha1($key, true)); 
10964
									$key =  base64_encode(sha1($key, true));
10965
								
10965
 
10966
									#設置 handshake 的 header
10966
									#設置 handshake 的 header
10967
									$headers = 
10967
									$headers =
10968
									"HTTP/1.1 101 Switching Protocols".PHP_EOL.
10968
									"HTTP/1.1 101 Switching Protocols".PHP_EOL.
10969
									"Upgrade: websocket".PHP_EOL.
10969
									"Upgrade: websocket".PHP_EOL.
10970
									"Connection: Upgrade".PHP_EOL.
10970
									"Connection: Upgrade".PHP_EOL.
10971
									"Sec-WebSocket-Accept: ".$key.PHP_EOL.
10971
									"Sec-WebSocket-Accept: ".$key.PHP_EOL.
10972
									"X-Powered-By: qbpwcf".PHP_EOL.
10972
									"X-Powered-By: qbpwcf".PHP_EOL.
10973
									PHP_EOL;
10973
									PHP_EOL;
10974
								
10974
 
10975
									#傳送header給client
10975
									#傳送header給client
10976
									socket_write($con["connect"], $headers);
10976
									socket_write($con["connect"], $headers);
10977
								
10977
 
10978
									#儲存server傳送給client的訊息
10978
									#儲存server傳送給client的訊息
10979
									#$connections[$conIndex]["receivedMsgFromServer"][]=$headers;
10979
									#$connections[$conIndex]["receivedMsgFromServer"][]=$headers;
10980
								
10980
 
10981
									#提示serve handshake 完畢
10981
									#提示serve handshake 完畢
10982
									echo "handshak done...".PHP_EOL;
10982
									echo "handshak done...".PHP_EOL;
10983
								
10983
 
10984
									#設置已經 handshake
10984
									#設置已經 handshake
10985
									$connections[$conIndex]["handshaked"]="true";
10985
									$connections[$conIndex]["handshaked"]="true";
10986
								
10986
 
10987
									}#if end
10987
									}#if end
10988
								
10988
 
10989
								}#if end
10989
								}#if end
10990
							
10990
 
10991
							}#if end
10991
							}#if end
10992
						
10992
 
10993
						#如果是 ws 模式
10993
						#如果是 ws 模式
10994
						if($conf["wsMode"]==="true"){
10994
						if($conf["wsMode"]==="true"){
10995
						
10995
 
10996
							#如果尚未 handshake 過
10996
							#如果尚未 handshake 過
10997
							if($connections[$conIndex]["handshaked"]==="false"){
10997
							if($connections[$conIndex]["handshaked"]==="false"){
10998
							
10998
 
10999
								#跳過
10999
								#跳過
11000
								continue;
11000
								continue;
11001
							
11001
 
11002
								}#if end
11002
								}#if end
11003
							
11003
 
11004
							#如果還不用要 unmask
11004
							#如果還不用要 unmask
11005
							if($connections[$conIndex]["unmask"]==="false"){
11005
							if($connections[$conIndex]["unmask"]==="false"){
11006
							
11006
 
11007
								#跳過
11007
								#跳過
11008
								continue;
11008
								continue;
11009
							
11009
 
11010
								}#if end
11010
								}#if end
11011
							
11011
 
11012
							}#if end
11012
							}#if end
11013
						
11013
 
11014
						#如果是 ws 模式
11014
						#如果是 ws 模式
11015
						if($conf["wsMode"]==="true"){
11015
						if($conf["wsMode"]==="true"){
11016
							
11016
 
11017
							#debug
11017
							#debug
11018
							#var_dump(__LINE__,$connections[$conIndex]);
11018
							#var_dump(__LINE__,$connections[$conIndex]);
11019
							
11019
 
11020
							#如果已經 handshake 過
11020
							#如果已經 handshake 過
11021
							if($connections[$conIndex]["handshaked"]==="true"){
11021
							if($connections[$conIndex]["handshaked"]==="true"){
11022
							
11022
 
11023
								#如果要 unmask	
11023
								#如果要 unmask
11024
								if($connections[$conIndex]["unmask"]==="true"){
11024
								if($connections[$conIndex]["unmask"]==="true"){
11025
								
11025
 
11026
									#如果收到的訊息不為空
11026
									#如果收到的訊息不為空
11027
									if(!empty($buf)){
11027
									if(!empty($buf)){
11028
									
11028
 
11029
										echo "start unmask...".PHP_EOL;
11029
										echo "start unmask...".PHP_EOL;
11030
								
11030
 
11031
										#debug
11031
										#debug
11032
										#var_dump(__LINE__,$buf);
11032
										#var_dump(__LINE__,$buf);
11033
								
11033
 
11034
										#解密 $buf
11034
										#解密 $buf
11035
										#回傳結果:
11035
										#回傳結果:
11036
										#$result["function"],當前函式的名稱.
11036
										#$result["function"],當前函式的名稱.
11037
										#$result["status"],執行結果狀態,"true"代表正常;"false"代表不正常.
11037
										#$result["status"],執行結果狀態,"true"代表正常;"false"代表不正常.
11038
										#$result["content"],unmask後的內容.
11038
										#$result["content"],unmask後的內容.
Line 11040... Line 11040...
11040
										#$result["error"],錯誤訊息陣列.
11040
										#$result["error"],錯誤訊息陣列.
11041
										$conf["self::unmask"]["payload"]=$buf;
11041
										$conf["self::unmask"]["payload"]=$buf;
11042
										#$conf["self::unmask"]["debug"]="true";
11042
										#$conf["self::unmask"]["debug"]="true";
11043
										$buf=webSock::unmask($conf["self::unmask"]);
11043
										$buf=webSock::unmask($conf["self::unmask"]);
11044
										unset($conf["self::unmask"]);
11044
										unset($conf["self::unmask"]);
11045
										
11045
 
11046
										#如果執行失敗
11046
										#如果執行失敗
11047
										if($buf["status"]==="false"){
11047
										if($buf["status"]==="false"){
11048
										
11048
 
11049
											#印出結果
11049
											#印出結果
11050
											var_dump($buf);
11050
											var_dump($buf);
11051
										
11051
 
11052
											#結束執行
11052
											#結束執行
11053
											exit;
11053
											exit;
11054
										
11054
 
11055
											}#if end
11055
											}#if end
11056
										
11056
 
11057
										#如果是 ping 的回應 pong
11057
										#如果是 ping 的回應 pong
11058
										if($buf["type"]==="pong"){
11058
										if($buf["type"]==="pong"){
11059
										
11059
 
11060
											#提示收到 pong
11060
											#提示收到 pong
11061
											echo "got pong".PHP_EOL;
11061
											echo "got pong".PHP_EOL;
11062
										
11062
 
11063
											#更新的連線的 last pong time
11063
											#更新的連線的 last pong time
11064
											$connections[$conIndex]["lastPongTime"]=time();
11064
											$connections[$conIndex]["lastPongTime"]=time();
11065
										
11065
 
11066
											#換處理下一則訊息
11066
											#換處理下一則訊息
11067
											continue;
11067
											continue;
11068
										
11068
 
11069
											}#if end
11069
											}#if end
11070
											
11070
 
11071
										#如果不是 web socket frame
11071
										#如果不是 web socket frame
11072
										if($buf["type"]==="invalid"){
11072
										if($buf["type"]==="invalid"){
11073
										
11073
 
11074
											#提示收到非 web socket frame
11074
											#提示收到非 web socket frame
11075
											echo "not web socket frame".PHP_EOL;
11075
											echo "not web socket frame".PHP_EOL;
11076
											
11076
 
11077
											#debug
11077
											#debug
11078
											#var_dump(__LINE__,$buf);
11078
											#var_dump(__LINE__,$buf);
11079
										
11079
 
11080
											#換處理下一則訊息
11080
											#換處理下一則訊息
11081
											continue;
11081
											continue;
11082
										
11082
 
11083
											}#if end
11083
											}#if end
11084
										
11084
 
11085
										#取得umask後的內容
11085
										#取得umask後的內容
11086
										$buf=$buf["content"];
11086
										$buf=$buf["content"];
11087
									
11087
 
11088
										#提示 unmask 結束
11088
										#提示 unmask 結束
11089
										echo "end unmask()".PHP_EOL;
11089
										echo "end unmask()".PHP_EOL;
11090
										
11090
 
11091
										#提示 web socket server 收到訊息的長度(bytes)
11091
										#提示 web socket server 收到訊息的長度(bytes)
11092
										echo "Server receviced plain content(".strlen($buf)."):".PHP_EOL;
11092
										echo "Server receviced plain content(".strlen($buf)."):".PHP_EOL;
11093
										
11093
 
11094
										#提示 web socket server 收到的訊息內容
11094
										#提示 web socket server 收到的訊息內容
11095
										echo $buf.PHP_EOL;
11095
										echo $buf.PHP_EOL;
11096
									
11096
 
11097
										}#if end
11097
										}#if end
11098
								
11098
 
11099
									}#if end
11099
									}#if end
11100
							
11100
 
11101
								}#if end
11101
								}#if end
11102
								
11102
 
11103
							}#if end
11103
							}#if end
11104
						
11104
 
11105
						/*
11105
						/*
11106
						
11106
 
11107
						#如果收到的是 id?
11107
						#如果收到的是 id?
11108
						if($buf === 'id?'){
11108
						if($buf === 'id?'){
11109
							
11109
 
11110
							#如果尚未認證
11110
							#如果尚未認證
11111
							if($connections[$conIndex]["auth"]==="false"){
11111
							if($connections[$conIndex]["auth"]==="false"){
11112
							
11112
 
11113
								#設置要回傳的訊息
11113
								#設置要回傳的訊息
11114
								$talkback="permission denied";
11114
								$talkback="permission denied";
11115
							
11115
 
11116
								}#if end
11116
								}#if end
11117
							
11117
 
11118
							else{
11118
							else{
11119
							
11119
 
11120
								#設置要回傳的訊息
11120
								#設置要回傳的訊息
11121
								$talkback=$connections[$conIndex]["id"];
11121
								$talkback=$connections[$conIndex]["id"];
11122
							
11122
 
11123
								}#else end
11123
								}#else end
11124
 
11124
 
11125
							#debug
11125
							#debug
11126
							#var_dump(__LINE__,$talkback);
11126
							#var_dump(__LINE__,$talkback);
11127
 
11127
 
11128
							#json encode 要回傳的訊息
11128
							#json encode 要回傳的訊息
11129
							$talkback=json_encode($talkback);
11129
							$talkback=json_encode($talkback);
11130
								
11130
 
11131
							#debug
11131
							#debug
11132
							#var_dump(__LINE__,$talkback);
11132
							#var_dump(__LINE__,$talkback);
11133
							
11133
 
11134
							#如果是 ws 模式
11134
							#如果是 ws 模式
11135
							if($conf["wsMode"]==="true"){
11135
							if($conf["wsMode"]==="true"){
11136
							
11136
 
11137
								#如果已經 handshake 過
11137
								#如果已經 handshake 過
11138
								if($connections[$conIndex]["handshaked"]==="true"){
11138
								if($connections[$conIndex]["handshaked"]==="true"){
11139
								
11139
 
11140
									#如果要 unmask	
11140
									#如果要 unmask
11141
									if($connections[$conIndex]["unmask"]==="true"){
11141
									if($connections[$conIndex]["unmask"]==="true"){
11142
								
11142
 
11143
										#函式說明:
11143
										#函式說明:
11144
										#加密 handshake 後要傳送的訊息 
11144
										#加密 handshake 後要傳送的訊息
11145
										#回傳結果:
11145
										#回傳結果:
11146
										#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
11146
										#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
11147
										#$result["error"],執行不正常結束的錯訊息陣列.
11147
										#$result["error"],執行不正常結束的錯訊息陣列.
11148
										#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
11148
										#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
11149
										#$result["argu"],陣列,使用的參數.
11149
										#$result["argu"],陣列,使用的參數.
11150
										#必填參數:
11150
										#必填參數:
11151
										#$conf["text"],字串,要加密的訊息.
11151
										#$conf["text"],字串,要加密的訊息.
11152
										$conf["webSock::encode"]["text"]=$talkback; 
11152
										$conf["webSock::encode"]["text"]=$talkback;
11153
										#可省略參數:
11153
										#可省略參數:
11154
										#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
11154
										#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
11155
										#$conf["payloadIsBin"]="false";
11155
										#$conf["payloadIsBin"]="false";
11156
										#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
11156
										#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
11157
										#$conf["ping"]="false";
11157
										#$conf["ping"]="false";
Line 11167... Line 11167...
11167
										#無.
11167
										#無.
11168
										#備註:
11168
										#備註:
11169
										#目前$conf["text"]長度超過125會出錯.
11169
										#目前$conf["text"]長度超過125會出錯.
11170
										$talkback=webSock::encode($conf["webSock::encode"]);
11170
										$talkback=webSock::encode($conf["webSock::encode"]);
11171
										unset($conf["webSock::encode"]);
11171
										unset($conf["webSock::encode"]);
11172
										
11172
 
11173
										#如果執行失敗
11173
										#如果執行失敗
11174
										if($talkback["status"]==="false"){
11174
										if($talkback["status"]==="false"){
11175
										
11175
 
11176
											#印出結果
11176
											#印出結果
11177
											var_dump($talkback);
11177
											var_dump($talkback);
11178
										
11178
 
11179
											#結束執行
11179
											#結束執行
11180
											exit;
11180
											exit;
11181
										
11181
 
11182
											}#if end
11182
											}#if end
11183
										
11183
 
11184
										#debug
11184
										#debug
11185
										#var_dump(__LINE__,$talkback);
11185
										#var_dump(__LINE__,$talkback);
11186
										
11186
 
11187
										}#if end
11187
										}#if end
11188
									
11188
 
11189
									}#if end
11189
									}#if end
11190
							
11190
 
11191
								}#if end
11191
								}#if end
11192
								
11192
 
11193
							#反之不為 web socket 訊息
11193
							#反之不為 web socket 訊息
11194
							else{
11194
							else{
11195
							
11195
 
11196
								#儲存成只有一個訊息
11196
								#儲存成只有一個訊息
11197
								$talkback["content"][]=$talkback;
11197
								$talkback["content"][]=$talkback;
11198
							
11198
 
11199
								}#else
11199
								}#else
11200
							
11200
 
11201
							#針對每個訊息的分段
11201
							#針對每個訊息的分段
11202
							foreach($talkback["content"] as $msg){
11202
							foreach($talkback["content"] as $msg){
11203
								
11203
 
11204
								#debug
11204
								#debug
11205
								var_dump(__LINE__,$msg);
11205
								var_dump(__LINE__,$msg);
11206
								
11206
 
11207
								#回傳訊息
11207
								#回傳訊息
11208
								$socket_write=socket_write($connections[$conIndex]["connect"], $msg, strlen($msg));
11208
								$socket_write=socket_write($connections[$conIndex]["connect"], $msg, strlen($msg));
11209
						
11209
 
11210
								#debug
11210
								#debug
11211
								var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
11211
								var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
11212
								
11212
 
11213
								}#foreach end
11213
								}#foreach end
11214
								
11214
 
11215
							#跳到下一輪
11215
							#跳到下一輪
11216
							continue;
11216
							continue;
11217
							
11217
 
11218
							}#if end
11218
							}#if end
11219
							
11219
 
11220
						*/	
11220
						*/
11221
						
11221
 
11222
						/*
11222
						/*
11223
						
11223
 
11224
						#如果收到的是 ids?
11224
						#如果收到的是 ids?
11225
						if($buf === 'ids?'){
11225
						if($buf === 'ids?'){
11226
							
11226
 
11227
							#如果尚未認證
11227
							#如果尚未認證
11228
							if($connections[$conIndex]["auth"]==="false"){
11228
							if($connections[$conIndex]["auth"]==="false"){
11229
							
11229
 
11230
								#設置要回傳的訊息
11230
								#設置要回傳的訊息
11231
								$talkback=json_encode("permission denied");
11231
								$talkback=json_encode("permission denied");
11232
							
11232
 
11233
								}#if end
11233
								}#if end
11234
								
11234
 
11235
							#反之
11235
							#反之
11236
							else{
11236
							else{
11237
							
11237
 
11238
								#設置要回傳的訊息
11238
								#設置要回傳的訊息
11239
								$talkback=array();
11239
								$talkback=array();
11240
								
11240
 
11241
								#針對每個連線
11241
								#針對每個連線
11242
								foreach($connections as $conIndex_ids => $con_ids){
11242
								foreach($connections as $conIndex_ids => $con_ids){
11243
 
11243
 
11244
									#如果不是自己
11244
									#如果不是自己
11245
									if($con_ids["connect"]!==$connections[$conIndex]["connect"]){
11245
									if($con_ids["connect"]!==$connections[$conIndex]["connect"]){
11246
									
11246
 
11247
										#記錄其他人的id
11247
										#記錄其他人的id
11248
										$talkback[]=$con_ids["id"];
11248
										$talkback[]=$con_ids["id"];
11249
									
11249
 
11250
										}#if end
11250
										}#if end
11251
								
11251
 
11252
									}#foreach end
11252
									}#foreach end
11253
									
11253
 
11254
								#json encode要回傳的id陣列
11254
								#json encode要回傳的id陣列
11255
								$talkback=json_encode($talkback);
11255
								$talkback=json_encode($talkback);
11256
							
11256
 
11257
								}#else end
11257
								}#else end
11258
							
11258
 
11259
							#如果是 ws 模式
11259
							#如果是 ws 模式
11260
							if($conf["wsMode"]==="true"){
11260
							if($conf["wsMode"]==="true"){
11261
							
11261
 
11262
								#如果已經 handshake 過
11262
								#如果已經 handshake 過
11263
								if($connections[$conIndex]["handshaked"]==="true"){
11263
								if($connections[$conIndex]["handshaked"]==="true"){
11264
								
11264
 
11265
									#如果要 unmask
11265
									#如果要 unmask
11266
									if($connections[$conIndex]["unmask"]==="true"){
11266
									if($connections[$conIndex]["unmask"]==="true"){
11267
								
11267
 
11268
										#函式說明:
11268
										#函式說明:
11269
										#加密 handshake 後要傳送的訊息 
11269
										#加密 handshake 後要傳送的訊息
11270
										#回傳結果:
11270
										#回傳結果:
11271
										#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
11271
										#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
11272
										#$result["error"],執行不正常結束的錯訊息陣列.
11272
										#$result["error"],執行不正常結束的錯訊息陣列.
11273
										#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
11273
										#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
11274
										#$result["argu"],陣列,使用的參數.
11274
										#$result["argu"],陣列,使用的參數.
11275
										#必填參數:
11275
										#必填參數:
11276
										#$conf["text"],字串,要加密的訊息.
11276
										#$conf["text"],字串,要加密的訊息.
11277
										$conf["webSock::encode"]["text"]=$talkback; 
11277
										$conf["webSock::encode"]["text"]=$talkback;
11278
										#可省略參數:
11278
										#可省略參數:
11279
										#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
11279
										#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
11280
										#$conf["payloadIsBin"]="false";
11280
										#$conf["payloadIsBin"]="false";
11281
										#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
11281
										#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
11282
										#$conf["ping"]="false";
11282
										#$conf["ping"]="false";
Line 11292... Line 11292...
11292
										#無.
11292
										#無.
11293
										#備註:
11293
										#備註:
11294
										#目前$conf["text"]長度超過125會出錯.
11294
										#目前$conf["text"]長度超過125會出錯.
11295
										$talkback=webSock::encode($conf["webSock::encode"]);
11295
										$talkback=webSock::encode($conf["webSock::encode"]);
11296
										unset($conf["webSock::encode"]);
11296
										unset($conf["webSock::encode"]);
11297
										
11297
 
11298
										#如果執行失敗
11298
										#如果執行失敗
11299
										if($talkback["status"]==="false"){
11299
										if($talkback["status"]==="false"){
11300
										
11300
 
11301
											#印出結果
11301
											#印出結果
11302
											var_dump($talkback);
11302
											var_dump($talkback);
11303
										
11303
 
11304
											#結束執行
11304
											#結束執行
11305
											exit;
11305
											exit;
11306
										
11306
 
11307
											}#if end
11307
											}#if end
11308
										
11308
 
11309
										#debug
11309
										#debug
11310
										#var_dump(__LINE__,$talkback);
11310
										#var_dump(__LINE__,$talkback);
11311
										
11311
 
11312
										}#if end
11312
										}#if end
11313
									
11313
 
11314
									}#if end
11314
									}#if end
11315
							
11315
 
11316
								}#if end
11316
								}#if end
11317
							
11317
 
11318
							#反之不為 web socket 訊息
11318
							#反之不為 web socket 訊息
11319
							else{
11319
							else{
11320
							
11320
 
11321
								#儲存成只有一個訊息
11321
								#儲存成只有一個訊息
11322
								$talkback["content"][]=$talkback;
11322
								$talkback["content"][]=$talkback;
11323
							
11323
 
11324
								}#else
11324
								}#else
11325
							
11325
 
11326
							#針對每個訊息的分段
11326
							#針對每個訊息的分段
11327
							foreach($talkback["content"] as $msg){
11327
							foreach($talkback["content"] as $msg){
11328
								
11328
 
11329
								#回傳訊息
11329
								#回傳訊息
11330
								$socket_write=socket_write($connections[$conIndex]["connect"], $msg, strlen($msg));
11330
								$socket_write=socket_write($connections[$conIndex]["connect"], $msg, strlen($msg));
11331
						
11331
 
11332
								#debug
11332
								#debug
11333
								var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
11333
								var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
11334
								
11334
 
11335
								}#foreach end
11335
								}#foreach end
11336
						
11336
 
11337
							#跳到下一輪
11337
							#跳到下一輪
11338
							continue;
11338
							continue;
11339
							
11339
 
11340
							}#if end
11340
							}#if end
11341
						
11341
 
11342
						#如果收到 "talkTo?"
11342
						#如果收到 "talkTo?"
11343
						if($buf==="talkTo?"){
11343
						if($buf==="talkTo?"){
11344
							
11344
 
11345
							#如果尚未認證
11345
							#如果尚未認證
11346
							if($connections[$conIndex]["auth"]==="false"){
11346
							if($connections[$conIndex]["auth"]==="false"){
11347
							
11347
 
11348
								#設置要回傳的訊息
11348
								#設置要回傳的訊息
11349
								$talkback=json_encode("permission denied");
11349
								$talkback=json_encode("permission denied");
11350
							
11350
 
11351
								}#if end
11351
								}#if end
11352
							
11352
 
11353
							#反之
11353
							#反之
11354
							else{
11354
							else{
11355
							
11355
 
11356
								#初始化要講話的id陣列
11356
								#初始化要講話的id陣列
11357
								$talkback=array();
11357
								$talkback=array();
11358
								
11358
 
11359
								#有幾個要講話的人就執行幾次
11359
								#有幾個要講話的人就執行幾次
11360
								foreach($connections[$conIndex]["talkTo"] as $to){
11360
								foreach($connections[$conIndex]["talkTo"] as $to){
11361
									
11361
 
11362
									#串街要講話的人
11362
									#串街要講話的人
11363
									$talkback[]=$to;
11363
									$talkback[]=$to;
11364
									
11364
 
11365
									}#foreach end
11365
									}#foreach end
11366
								
11366
 
11367
								#設置要回傳的訊息
11367
								#設置要回傳的訊息
11368
								$talkback=json_encode($talkback).PHP_EOL;
11368
								$talkback=json_encode($talkback).PHP_EOL;
11369
							
11369
 
11370
								}#else end
11370
								}#else end
11371
							
11371
 
11372
							#如果是 ws 模式
11372
							#如果是 ws 模式
11373
							if($conf["wsMode"]==="true"){
11373
							if($conf["wsMode"]==="true"){
11374
							
11374
 
11375
								#如果已經 handshake 過
11375
								#如果已經 handshake 過
11376
								if($connections[$conIndex]["handshaked"]==="true"){
11376
								if($connections[$conIndex]["handshaked"]==="true"){
11377
								
11377
 
11378
									#如果要 unmask	
11378
									#如果要 unmask
11379
									if($connections[$conIndex]["unmask"]==="true"){
11379
									if($connections[$conIndex]["unmask"]==="true"){
11380
								
11380
 
11381
										#函式說明:
11381
										#函式說明:
11382
										#加密 handshake 後要傳送的訊息 
11382
										#加密 handshake 後要傳送的訊息
11383
										#回傳結果:
11383
										#回傳結果:
11384
										#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
11384
										#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
11385
										#$result["error"],執行不正常結束的錯訊息陣列.
11385
										#$result["error"],執行不正常結束的錯訊息陣列.
11386
										#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
11386
										#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
11387
										#$result["argu"],陣列,使用的參數.
11387
										#$result["argu"],陣列,使用的參數.
11388
										#必填參數:
11388
										#必填參數:
11389
										#$conf["text"],字串,要加密的訊息.
11389
										#$conf["text"],字串,要加密的訊息.
11390
										$conf["webSock::encode"]["text"]=$talkback; 
11390
										$conf["webSock::encode"]["text"]=$talkback;
11391
										#可省略參數:
11391
										#可省略參數:
11392
										#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
11392
										#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
11393
										#$conf["payloadIsBin"]="false";
11393
										#$conf["payloadIsBin"]="false";
11394
										#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
11394
										#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
11395
										#$conf["ping"]="false";
11395
										#$conf["ping"]="false";
Line 11405... Line 11405...
11405
										#無.
11405
										#無.
11406
										#備註:
11406
										#備註:
11407
										#目前$conf["text"]長度超過125會出錯.
11407
										#目前$conf["text"]長度超過125會出錯.
11408
										$talkback=webSock::encode($conf["webSock::encode"]);
11408
										$talkback=webSock::encode($conf["webSock::encode"]);
11409
										unset($conf["webSock::encode"]);
11409
										unset($conf["webSock::encode"]);
11410
										
11410
 
11411
										#如果執行失敗
11411
										#如果執行失敗
11412
										if($talkback["status"]==="false"){
11412
										if($talkback["status"]==="false"){
11413
										
11413
 
11414
											#印出結果
11414
											#印出結果
11415
											var_dump($talkback);
11415
											var_dump($talkback);
11416
										
11416
 
11417
											#結束執行
11417
											#結束執行
11418
											exit;
11418
											exit;
11419
										
11419
 
11420
											}#if end
11420
											}#if end
11421
										
11421
 
11422
										#debug
11422
										#debug
11423
										#var_dump(__LINE__,$talkback);
11423
										#var_dump(__LINE__,$talkback);
11424
										
11424
 
11425
										}#if end
11425
										}#if end
11426
										
11426
 
11427
									}#if end
11427
									}#if end
11428
							
11428
 
11429
								}#if end
11429
								}#if end
11430
							
11430
 
11431
							#反之不為 web socket 訊息
11431
							#反之不為 web socket 訊息
11432
							else{
11432
							else{
11433
							
11433
 
11434
								#儲存成只有一個訊息
11434
								#儲存成只有一個訊息
11435
								$talkback["content"][]=$talkback;
11435
								$talkback["content"][]=$talkback;
11436
							
11436
 
11437
								}#else
11437
								}#else
11438
							
11438
 
11439
							#針對每個訊息的分段
11439
							#針對每個訊息的分段
11440
							foreach($talkback["content"] as $msg){
11440
							foreach($talkback["content"] as $msg){
11441
								
11441
 
11442
								#回傳訊息
11442
								#回傳訊息
11443
								$socket_write=socket_write($connections[$conIndex]["connect"], $msg, strlen($msg));
11443
								$socket_write=socket_write($connections[$conIndex]["connect"], $msg, strlen($msg));
11444
						
11444
 
11445
								#debug
11445
								#debug
11446
								#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
11446
								#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
11447
								
11447
 
11448
								}#foreach end
11448
								}#foreach end
11449
							
11449
 
11450
							#跳到下一輪
11450
							#跳到下一輪
11451
							continue;
11451
							continue;
11452
							
11452
 
11453
							}#if end
11453
							}#if end
11454
							
11454
 
11455
						#如果收到的$buf長度大於 "talkTo:"
11455
						#如果收到的$buf長度大於 "talkTo:"
11456
						if(strlen($buf)>strlen("talkTo:")){
11456
						if(strlen($buf)>strlen("talkTo:")){
11457
							
11457
 
11458
							#如果收到開頭為 "talkTo:"
11458
							#如果收到開頭為 "talkTo:"
11459
							#涵式說明:
11459
							#涵式說明:
11460
							#取得符合特定字首與字尾的字串
11460
							#取得符合特定字首與字尾的字串
11461
							#回傳的結果:
11461
							#回傳的結果:
11462
							#$result["status"],若爲"true"則代表執行正常;若爲"false"則代表執行失敗。
11462
							#$result["status"],若爲"true"則代表執行正常;若爲"false"則代表執行失敗。
Line 11474... Line 11474...
11474
							#$conf["tailWord"]="";
11474
							#$conf["tailWord"]="";
11475
							#參考資料:
11475
							#參考資料:
11476
							#str_spilt(),可以將字串依照字母分割成一個個陣列字串。
11476
							#str_spilt(),可以將字串依照字母分割成一個個陣列字串。
11477
							$getMeetConditionsString=search::getMeetConditionsString($conf["search::getMeetConditionsString"]);
11477
							$getMeetConditionsString=search::getMeetConditionsString($conf["search::getMeetConditionsString"]);
11478
							unset($conf["search::getMeetConditionsString"]);
11478
							unset($conf["search::getMeetConditionsString"]);
11479
								
11479
 
11480
							#如果選找前置字串 "talkTo:" 失敗
11480
							#如果選找前置字串 "talkTo:" 失敗
11481
							if($getMeetConditionsString["status"]==="false"){
11481
							if($getMeetConditionsString["status"]==="false"){
11482
								
11482
 
11483
								#設置執行失敗
11483
								#設置執行失敗
11484
								$result["status"]="false";
11484
								$result["status"]="false";
11485
							
11485
 
11486
								#設置執行錯誤訊息
11486
								#設置執行錯誤訊息
11487
								$result["error"]=$getMeetConditionsString;
11487
								$result["error"]=$getMeetConditionsString;
11488
								
11488
 
11489
								#回傳結果
11489
								#回傳結果
11490
								return $result;
11490
								return $result;
11491
								
11491
 
11492
								}#if end
11492
								}#if end
11493
								
11493
 
11494
							#如果存在 "talkTo:" 前置字串
11494
							#如果存在 "talkTo:" 前置字串
11495
							if($getMeetConditionsString["founded"]==="true"){
11495
							if($getMeetConditionsString["founded"]==="true"){
11496
								
11496
 
11497
								#如果尚未認證
11497
								#如果尚未認證
11498
								if($connections[$conIndex]["auth"]==="false"){
11498
								if($connections[$conIndex]["auth"]==="false"){
11499
								
11499
 
11500
									#設置要回傳的訊息
11500
									#設置要回傳的訊息
11501
									$talkback=json_encode("permission denied");
11501
									$talkback=json_encode("permission denied");
11502
									
11502
 
11503
									#如果是 ws 模式
11503
									#如果是 ws 模式
11504
									if($conf["wsMode"]==="true"){
11504
									if($conf["wsMode"]==="true"){
11505
									
11505
 
11506
										#如果已經 handshake 過
11506
										#如果已經 handshake 過
11507
										if($connections[$conIndex]["handshaked"]==="true"){
11507
										if($connections[$conIndex]["handshaked"]==="true"){
11508
										
11508
 
11509
											#如果要 unmask	
11509
											#如果要 unmask
11510
											if($connections[$conIndex]["unmask"]==="true"){
11510
											if($connections[$conIndex]["unmask"]==="true"){
11511
										
11511
 
11512
												#函式說明:
11512
												#函式說明:
11513
												#加密 handshake 後要傳送的訊息 
11513
												#加密 handshake 後要傳送的訊息
11514
												#回傳結果:
11514
												#回傳結果:
11515
												#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
11515
												#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
11516
												#$result["error"],執行不正常結束的錯訊息陣列.
11516
												#$result["error"],執行不正常結束的錯訊息陣列.
11517
												#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
11517
												#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
11518
												#$result["argu"],陣列,使用的參數.
11518
												#$result["argu"],陣列,使用的參數.
11519
												#必填參數:
11519
												#必填參數:
11520
												#$conf["text"],字串,要加密的訊息.
11520
												#$conf["text"],字串,要加密的訊息.
11521
												$conf["webSock::encode"]["text"]=$talkback; 
11521
												$conf["webSock::encode"]["text"]=$talkback;
11522
												#可省略參數:
11522
												#可省略參數:
11523
												#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
11523
												#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
11524
												#$conf["payloadIsBin"]="false";
11524
												#$conf["payloadIsBin"]="false";
11525
												#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
11525
												#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
11526
												#$conf["ping"]="false";
11526
												#$conf["ping"]="false";
Line 11536... Line 11536...
11536
												#無.
11536
												#無.
11537
												#備註:
11537
												#備註:
11538
												#目前$conf["text"]長度超過125會出錯.
11538
												#目前$conf["text"]長度超過125會出錯.
11539
												$talkback=webSock::encode($conf["webSock::encode"]);
11539
												$talkback=webSock::encode($conf["webSock::encode"]);
11540
												unset($conf["webSock::encode"]);
11540
												unset($conf["webSock::encode"]);
11541
												
11541
 
11542
												#如果執行失敗
11542
												#如果執行失敗
11543
												if($talkback["status"]==="false"){
11543
												if($talkback["status"]==="false"){
11544
												
11544
 
11545
													#印出結果
11545
													#印出結果
11546
													var_dump($talkback);
11546
													var_dump($talkback);
11547
												
11547
 
11548
													#結束執行
11548
													#結束執行
11549
													exit;
11549
													exit;
11550
												
11550
 
11551
													}#if end
11551
													}#if end
11552
												
11552
 
11553
												#debug
11553
												#debug
11554
												#var_dump(__LINE__,$talkback);
11554
												#var_dump(__LINE__,$talkback);
11555
												
11555
 
11556
												}#if end
11556
												}#if end
11557
												
11557
 
11558
											}#if end
11558
											}#if end
11559
									
11559
 
11560
										}#if end
11560
										}#if end
11561
									
11561
 
11562
									#反之不為 web socket 訊息
11562
									#反之不為 web socket 訊息
11563
									else{
11563
									else{
11564
									
11564
 
11565
										#儲存成只有一個訊息
11565
										#儲存成只有一個訊息
11566
										$talkback["content"][]=$talkback;
11566
										$talkback["content"][]=$talkback;
11567
									
11567
 
11568
										}#else
11568
										}#else
11569
									
11569
 
11570
									#針對每個訊息的分段
11570
									#針對每個訊息的分段
11571
									foreach($talkback["content"] as $msg){
11571
									foreach($talkback["content"] as $msg){
11572
										
11572
 
11573
										#回傳訊息
11573
										#回傳訊息
11574
										$socket_write=socket_write($connections[$conIndex]["connect"], $msg, strlen($msg));
11574
										$socket_write=socket_write($connections[$conIndex]["connect"], $msg, strlen($msg));
11575
								
11575
 
11576
										#debug
11576
										#debug
11577
										#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
11577
										#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
11578
										
11578
 
11579
										}#foreach end
11579
										}#foreach end
11580
									
11580
 
11581
									#跳到下一輪
11581
									#跳到下一輪
11582
									continue;
11582
									continue;
11583
								
11583
 
11584
									}#if end
11584
									}#if end
11585
								
11585
 
11586
								#用 "talkTo:" 分割 $buf
11586
								#用 "talkTo:" 分割 $buf
11587
								#涵式說明:
11587
								#涵式說明:
11588
								#將固定格式的字串分開,並回傳分開的結果。
11588
								#將固定格式的字串分開,並回傳分開的結果。
11589
								#回傳結果:
11589
								#回傳結果:
11590
								#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
11590
								#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 11600... Line 11600...
11600
								#可省略參數:
11600
								#可省略參數:
11601
								#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
11601
								#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
11602
								$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
11602
								$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
11603
								$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
11603
								$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
11604
								unset($conf["stringProcess::spiltString"]);
11604
								unset($conf["stringProcess::spiltString"]);
11605
								
11605
 
11606
								#如果分割字串失敗
11606
								#如果分割字串失敗
11607
								if($spiltString["status"]==="false"){
11607
								if($spiltString["status"]==="false"){
11608
									
11608
 
11609
									#設置執行失敗
11609
									#設置執行失敗
11610
									$result["status"]="false";
11610
									$result["status"]="false";
11611
								
11611
 
11612
									#設置執行錯誤訊息
11612
									#設置執行錯誤訊息
11613
									$result["error"]=$spiltString;
11613
									$result["error"]=$spiltString;
11614
									
11614
 
11615
									#回傳結果
11615
									#回傳結果
11616
									return $result;
11616
									return $result;
11617
									
11617
 
11618
									}#if end
11618
									}#if end
11619
								
11619
 
11620
								#如果剛好分割出一筆資料
11620
								#如果剛好分割出一筆資料
11621
								if($spiltString["dataCounts"]===1){
11621
								if($spiltString["dataCounts"]===1){
11622
									
11622
 
11623
									#取得自己的id
11623
									#取得自己的id
11624
									$myId=$connections[$conIndex]["id"];
11624
									$myId=$connections[$conIndex]["id"];
11625
									
11625
 
11626
									#取得講話對象的id
11626
									#取得講話對象的id
11627
									$toId=$spiltString["dataArray"][0];
11627
									$toId=$spiltString["dataArray"][0];
11628
									
11628
 
11629
									#設置對象不存在的識別
11629
									#設置對象不存在的識別
11630
									$targetExist=false;
11630
									$targetExist=false;
11631
									
11631
 
11632
									#設置通話對象的key
11632
									#設置通話對象的key
11633
									$targetKey="";
11633
									$targetKey="";
11634
									
11634
 
11635
									#取得除了自己的id以外的所有連線
11635
									#取得除了自己的id以外的所有連線
11636
									foreach($connections as $index=>$talkInfo){
11636
									foreach($connections as $index=>$talkInfo){
11637
										
11637
 
11638
										#除了自己的id
11638
										#除了自己的id
11639
										if($talkInfo["id"]!==$myId){
11639
										if($talkInfo["id"]!==$myId){
11640
											
11640
 
11641
											#如果存在要對話的對象
11641
											#如果存在要對話的對象
11642
											if($talkInfo["id"]===$toId){
11642
											if($talkInfo["id"]===$toId){
11643
												
11643
 
11644
												#設置對象存在的識別
11644
												#設置對象存在的識別
11645
												$targetExist=true;
11645
												$targetExist=true;
11646
												
11646
 
11647
												#通話對象的key
11647
												#通話對象的key
11648
												$targetKey=$index;
11648
												$targetKey=$index;
11649
												
11649
 
11650
												}#if end
11650
												}#if end
11651
											
11651
 
11652
											}#if end
11652
											}#if end
11653
										
11653
 
11654
										}#for each end
11654
										}#for each end
11655
 
11655
 
11656
									#如果通話對象存在
11656
									#如果通話對象存在
11657
									if($targetExist){
11657
									if($targetExist){
11658
										
11658
 
11659
										#保存自己的通話對象
11659
										#保存自己的通話對象
11660
										$connections[$conIndex]["talkTo"][]=$toId;
11660
										$connections[$conIndex]["talkTo"][]=$toId;
11661
										
11661
 
11662
										#設置要給自己看的訊息
11662
										#設置要給自己看的訊息
11663
										$talkback="您與 ".$toId." 展開對話";
11663
										$talkback="您與 ".$toId." 展開對話";
11664
										
11664
 
11665
										#json encode 要給自己看的訊息
11665
										#json encode 要給自己看的訊息
11666
										$talkback=json_encode($talkback).PHP_EOL;
11666
										$talkback=json_encode($talkback).PHP_EOL;
11667
										
11667
 
11668
										#如果是 ws 模式
11668
										#如果是 ws 模式
11669
										if($conf["wsMode"]==="true"){
11669
										if($conf["wsMode"]==="true"){
11670
										
11670
 
11671
											#如果已經 handshake 過
11671
											#如果已經 handshake 過
11672
											if($connections[$conIndex]["handshaked"]==="true"){
11672
											if($connections[$conIndex]["handshaked"]==="true"){
11673
											
11673
 
11674
												#如果要 unmask	
11674
												#如果要 unmask
11675
												if($connections[$conIndex]["unmask"]==="true"){
11675
												if($connections[$conIndex]["unmask"]==="true"){
11676
											
11676
 
11677
													#函式說明:
11677
													#函式說明:
11678
													#加密 handshake 後要傳送的訊息 
11678
													#加密 handshake 後要傳送的訊息
11679
													#回傳結果:
11679
													#回傳結果:
11680
													#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
11680
													#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
11681
													#$result["error"],執行不正常結束的錯訊息陣列.
11681
													#$result["error"],執行不正常結束的錯訊息陣列.
11682
													#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
11682
													#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
11683
													#$result["argu"],陣列,使用的參數.
11683
													#$result["argu"],陣列,使用的參數.
11684
													#必填參數:
11684
													#必填參數:
11685
													#$conf["text"],字串,要加密的訊息.
11685
													#$conf["text"],字串,要加密的訊息.
11686
													$conf["webSock::encode"]["text"]=$talkback; 
11686
													$conf["webSock::encode"]["text"]=$talkback;
11687
													#可省略參數:
11687
													#可省略參數:
11688
													#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
11688
													#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
11689
													#$conf["payloadIsBin"]="false";
11689
													#$conf["payloadIsBin"]="false";
11690
													#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
11690
													#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
11691
													#$conf["ping"]="false";
11691
													#$conf["ping"]="false";
Line 11701... Line 11701...
11701
													#無.
11701
													#無.
11702
													#備註:
11702
													#備註:
11703
													#目前$conf["text"]長度超過125會出錯.
11703
													#目前$conf["text"]長度超過125會出錯.
11704
													$talkback=webSock::encode($conf["webSock::encode"]);
11704
													$talkback=webSock::encode($conf["webSock::encode"]);
11705
													unset($conf["webSock::encode"]);
11705
													unset($conf["webSock::encode"]);
11706
													
11706
 
11707
													#如果執行失敗
11707
													#如果執行失敗
11708
													if($talkback["status"]==="false"){
11708
													if($talkback["status"]==="false"){
11709
													
11709
 
11710
														#印出結果
11710
														#印出結果
11711
														var_dump($talkback);
11711
														var_dump($talkback);
11712
													
11712
 
11713
														#結束執行
11713
														#結束執行
11714
														exit;
11714
														exit;
11715
													
11715
 
11716
														}#if end
11716
														}#if end
11717
													
11717
 
11718
													#debug
11718
													#debug
11719
													#var_dump(__LINE__,$talkback);
11719
													#var_dump(__LINE__,$talkback);
11720
													
11720
 
11721
													}#if end
11721
													}#if end
11722
												
11722
 
11723
												}#if end
11723
												}#if end
11724
										
11724
 
11725
											}#if end
11725
											}#if end
11726
											
11726
 
11727
										#反之不為 web socket 訊息
11727
										#反之不為 web socket 訊息
11728
										else{
11728
										else{
11729
										
11729
 
11730
											#儲存成只有一個訊息
11730
											#儲存成只有一個訊息
11731
											$talkback["content"][]=$talkback;
11731
											$talkback["content"][]=$talkback;
11732
										
11732
 
11733
											}#else
11733
											}#else
11734
										
11734
 
11735
										#針對每個訊息的分段
11735
										#針對每個訊息的分段
11736
										foreach($talkback["content"] as $msg){
11736
										foreach($talkback["content"] as $msg){
11737
											
11737
 
11738
											#回傳訊息
11738
											#回傳訊息
11739
											$socket_write=socket_write($connections[$conIndex]["connect"], $msg, strlen($msg));
11739
											$socket_write=socket_write($connections[$conIndex]["connect"], $msg, strlen($msg));
11740
									
11740
 
11741
											#debug
11741
											#debug
11742
											#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
11742
											#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
11743
											
11743
 
11744
											}#foreach end
11744
											}#foreach end
11745
										
11745
 
11746
										#讓對方可以跟自己對話
11746
										#讓對方可以跟自己對話
11747
										$connections[$targetKey]["talkTo"][]=$myId;
11747
										$connections[$targetKey]["talkTo"][]=$myId;
11748
										
11748
 
11749
										#設置要給對方看的訊息
11749
										#設置要給對方看的訊息
11750
										$talkback=$connections[$conIndex]["id"]." 與您展開對話";
11750
										$talkback=$connections[$conIndex]["id"]." 與您展開對話";
11751
										
11751
 
11752
										#json encode 設置要給對方看的訊息
11752
										#json encode 設置要給對方看的訊息
11753
										$talkback=json_encode($talkback);
11753
										$talkback=json_encode($talkback);
11754
										
11754
 
11755
										#如果是 ws 模式
11755
										#如果是 ws 模式
11756
										if($conf["wsMode"]==="true"){
11756
										if($conf["wsMode"]==="true"){
11757
										
11757
 
11758
											#如果已經 handshake 過
11758
											#如果已經 handshake 過
11759
											if($connections[$conIndex]["handshaked"]==="true"){
11759
											if($connections[$conIndex]["handshaked"]==="true"){
11760
											
11760
 
11761
												#如果要 unmask	
11761
												#如果要 unmask
11762
												if($connections[$conIndex]["unmask"]==="true"){
11762
												if($connections[$conIndex]["unmask"]==="true"){
11763
											
11763
 
11764
													#函式說明:
11764
													#函式說明:
11765
													#加密 handshake 後要傳送的訊息 
11765
													#加密 handshake 後要傳送的訊息
11766
													#回傳結果:
11766
													#回傳結果:
11767
													#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
11767
													#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
11768
													#$result["error"],執行不正常結束的錯訊息陣列.
11768
													#$result["error"],執行不正常結束的錯訊息陣列.
11769
													#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
11769
													#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
11770
													#$result["argu"],陣列,使用的參數.
11770
													#$result["argu"],陣列,使用的參數.
11771
													#必填參數:
11771
													#必填參數:
11772
													#$conf["text"],字串,要加密的訊息.
11772
													#$conf["text"],字串,要加密的訊息.
11773
													$conf["webSock::encode"]["text"]=$talkback; 
11773
													$conf["webSock::encode"]["text"]=$talkback;
11774
													#可省略參數:
11774
													#可省略參數:
11775
													#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
11775
													#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
11776
													#$conf["payloadIsBin"]="false";
11776
													#$conf["payloadIsBin"]="false";
11777
													#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
11777
													#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
11778
													#$conf["ping"]="false";
11778
													#$conf["ping"]="false";
Line 11788... Line 11788...
11788
													#無.
11788
													#無.
11789
													#備註:
11789
													#備註:
11790
													#目前$conf["text"]長度超過125會出錯.
11790
													#目前$conf["text"]長度超過125會出錯.
11791
													$talkback=webSock::encode($conf["webSock::encode"]);
11791
													$talkback=webSock::encode($conf["webSock::encode"]);
11792
													unset($conf["webSock::encode"]);
11792
													unset($conf["webSock::encode"]);
11793
													
11793
 
11794
													#如果執行失敗
11794
													#如果執行失敗
11795
													if($talkback["status"]==="false"){
11795
													if($talkback["status"]==="false"){
11796
													
11796
 
11797
														#印出結果
11797
														#印出結果
11798
														var_dump($talkback);
11798
														var_dump($talkback);
11799
													
11799
 
11800
														#結束執行
11800
														#結束執行
11801
														exit;
11801
														exit;
11802
													
11802
 
11803
														}#if end
11803
														}#if end
11804
													
11804
 
11805
													#debug
11805
													#debug
11806
													#var_dump(__LINE__,$talkback);
11806
													#var_dump(__LINE__,$talkback);
11807
													
11807
 
11808
													}#if end
11808
													}#if end
11809
													
11809
 
11810
												}#if end
11810
												}#if end
11811
										
11811
 
11812
											}#if end
11812
											}#if end
11813
											
11813
 
11814
										#反之不為 web socket 訊息
11814
										#反之不為 web socket 訊息
11815
										else{
11815
										else{
11816
										
11816
 
11817
											#儲存成只有一個訊息
11817
											#儲存成只有一個訊息
11818
											$talkback["content"][]=$talkback;
11818
											$talkback["content"][]=$talkback;
11819
										
11819
 
11820
											}#else
11820
											}#else
11821
										
11821
 
11822
										#針對每個訊息的分段
11822
										#針對每個訊息的分段
11823
										foreach($talkback["content"] as $msg){
11823
										foreach($talkback["content"] as $msg){
11824
											
11824
 
11825
											#給對方看的訊息
11825
											#給對方看的訊息
11826
											$socket_write=socket_write($connections[$targetKey]["connect"], $msg, strlen($msg));
11826
											$socket_write=socket_write($connections[$targetKey]["connect"], $msg, strlen($msg));
11827
									
11827
 
11828
											#debug
11828
											#debug
11829
											#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
11829
											#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
11830
											
11830
 
11831
											}#foreach end
11831
											}#foreach end
11832
											
11832
 
11833
										#換一個連線
11833
										#換一個連線
11834
										continue;
11834
										continue;
11835
										
11835
 
11836
										}#if end
11836
										}#if end
11837
									
11837
 
11838
									}#if end
11838
									}#if end
11839
								
11839
 
11840
								}#if end
11840
								}#if end
11841
							
11841
 
11842
							}#if end
11842
							}#if end
11843
						
11843
 
11844
						*/
11844
						*/
11845
						
11845
 
11846
						#如果收到的訊息是 runPhpWhenIdle: 開頭
11846
						#如果收到的訊息是 runPhpWhenIdle: 開頭
11847
						if(strpos($buf,"runPhpWhenIdle:")===0){
11847
						if(strpos($buf,"runPhpWhenIdle:")===0){
11848
						
11848
 
11849
							#如果尚未認證
11849
							#如果尚未認證
11850
							if($connections[$conIndex]["auth"]==="false"){
11850
							if($connections[$conIndex]["auth"]==="false"){
11851
							
11851
 
11852
								#設置要回傳的訊息
11852
								#設置要回傳的訊息
11853
								$talkback=json_encode("permission denied");
11853
								$talkback=json_encode("permission denied");
11854
								
11854
 
11855
								#如果是 ws 模式
11855
								#如果是 ws 模式
11856
								if($conf["wsMode"]==="true"){
11856
								if($conf["wsMode"]==="true"){
11857
								
11857
 
11858
									#如果已經 handshake 過
11858
									#如果已經 handshake 過
11859
									if($connections[$conIndex]["handshaked"]==="true"){
11859
									if($connections[$conIndex]["handshaked"]==="true"){
11860
									
11860
 
11861
										#如果要 unmask	
11861
										#如果要 unmask
11862
										if($connections[$conIndex]["unmask"]==="true"){
11862
										if($connections[$conIndex]["unmask"]==="true"){
11863
									
11863
 
11864
											#函式說明:
11864
											#函式說明:
11865
											#加密 handshake 後要傳送的訊息 
11865
											#加密 handshake 後要傳送的訊息
11866
											#回傳結果:
11866
											#回傳結果:
11867
											#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
11867
											#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
11868
											#$result["error"],執行不正常結束的錯訊息陣列.
11868
											#$result["error"],執行不正常結束的錯訊息陣列.
11869
											#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
11869
											#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
11870
											#$result["argu"],陣列,使用的參數.
11870
											#$result["argu"],陣列,使用的參數.
11871
											#必填參數:
11871
											#必填參數:
11872
											#$conf["text"],字串,要加密的訊息.
11872
											#$conf["text"],字串,要加密的訊息.
11873
											$conf["webSock::encode"]["text"]=$talkback; 
11873
											$conf["webSock::encode"]["text"]=$talkback;
11874
											#可省略參數:
11874
											#可省略參數:
11875
											#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
11875
											#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
11876
											#$conf["payloadIsBin"]="false";
11876
											#$conf["payloadIsBin"]="false";
11877
											#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
11877
											#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
11878
											#$conf["ping"]="false";
11878
											#$conf["ping"]="false";
Line 11888... Line 11888...
11888
											#無.
11888
											#無.
11889
											#備註:
11889
											#備註:
11890
											#目前$conf["text"]長度超過125會出錯.
11890
											#目前$conf["text"]長度超過125會出錯.
11891
											$talkback=webSock::encode($conf["webSock::encode"]);
11891
											$talkback=webSock::encode($conf["webSock::encode"]);
11892
											unset($conf["webSock::encode"]);
11892
											unset($conf["webSock::encode"]);
11893
											
11893
 
11894
											#如果執行失敗
11894
											#如果執行失敗
11895
											if($talkback["status"]==="false"){
11895
											if($talkback["status"]==="false"){
11896
											
11896
 
11897
												#印出結果
11897
												#印出結果
11898
												var_dump($talkback);
11898
												var_dump($talkback);
11899
											
11899
 
11900
												#結束執行
11900
												#結束執行
11901
												exit;
11901
												exit;
11902
											
11902
 
11903
												}#if end
11903
												}#if end
11904
											
11904
 
11905
											#debug
11905
											#debug
11906
											#var_dump(__LINE__,$talkback);
11906
											#var_dump(__LINE__,$talkback);
11907
											
11907
 
11908
											}#if end
11908
											}#if end
11909
											
11909
 
11910
										}#if end
11910
										}#if end
11911
								
11911
 
11912
									}#if end
11912
									}#if end
11913
								
11913
 
11914
								#反之不為 web socket 訊息
11914
								#反之不為 web socket 訊息
11915
								else{
11915
								else{
11916
								
11916
 
11917
									#儲存成只有一個訊息
11917
									#儲存成只有一個訊息
11918
									$talkback["content"][]=$talkback;
11918
									$talkback["content"][]=$talkback;
11919
								
11919
 
11920
									}#else
11920
									}#else
11921
								
11921
 
11922
								#針對每個訊息的分段
11922
								#針對每個訊息的分段
11923
								foreach($talkback["content"] as $msg){
11923
								foreach($talkback["content"] as $msg){
11924
									
11924
 
11925
									#回傳訊息
11925
									#回傳訊息
11926
									$socket_write=socket_write($connections[$conIndex]["connect"], $msg, strlen($msg));
11926
									$socket_write=socket_write($connections[$conIndex]["connect"], $msg, strlen($msg));
11927
							
11927
 
11928
									#debug
11928
									#debug
11929
									#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
11929
									#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
11930
									
11930
 
11931
									}#foreach end
11931
									}#foreach end
11932
								
11932
 
11933
								#跳到下一輪
11933
								#跳到下一輪
11934
								continue;
11934
								continue;
11935
							
11935
 
11936
								}#if end
11936
								}#if end
11937
						
11937
 
11938
							#剔除開頭的 "runPhpWhenIdle:"
11938
							#剔除開頭的 "runPhpWhenIdle:"
11939
							#函式說明:
11939
							#函式說明:
11940
							#將字串特定關鍵字與其前面的內容剔除
11940
							#將字串特定關鍵字與其前面的內容剔除
11941
							#回傳結果:
11941
							#回傳結果:
11942
							#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
11942
							#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 11962... Line 11962...
11962
							#無.
11962
							#無.
11963
							#備註:
11963
							#備註:
11964
							#無.
11964
							#無.
11965
							$delStrBeforeKeyWord=stringProcess::delStrBeforeKeyWord($conf["stringProcess::delStrBeforeKeyWord"]);
11965
							$delStrBeforeKeyWord=stringProcess::delStrBeforeKeyWord($conf["stringProcess::delStrBeforeKeyWord"]);
11966
							unset($conf["stringProcess::delStrBeforeKeyWord"]);
11966
							unset($conf["stringProcess::delStrBeforeKeyWord"]);
11967
						
11967
 
11968
							#如果執行失敗
11968
							#如果執行失敗
11969
							if($delStrBeforeKeyWord["status"]==="false"){
11969
							if($delStrBeforeKeyWord["status"]==="false"){
11970
							
11970
 
11971
								#印出結果
11971
								#印出結果
11972
								var_dump($delStrBeforeKeyWord);
11972
								var_dump($delStrBeforeKeyWord);
11973
							
11973
 
11974
								#結束執行
11974
								#結束執行
11975
								exit;
11975
								exit;
11976
							
11976
 
11977
								}#if end
11977
								}#if end
11978
							
11978
 
11979
							#設置要執行的php程式
11979
							#設置要執行的php程式
11980
							$php2runWhenIdle[]=$delStrBeforeKeyWord["content"];
11980
							$php2runWhenIdle[]=$delStrBeforeKeyWord["content"];
11981
							
11981
 
11982
							#跳到下一輪
11982
							#跳到下一輪
11983
							continue;
11983
							continue;
11984
							
11984
 
11985
							}#if end
11985
							}#if end
11986
						
11986
 
11987
						/* 舊的寫法
11987
						/* 舊的寫法
11988
						
11988
 
11989
						#如果收到的是 quit
11989
						#如果收到的是 quit
11990
						if ($buf === 'quit' ) {
11990
						if ($buf === 'quit' ) {
11991
							
11991
 
11992
							#設置要回傳的訊息
11992
							#設置要回傳的訊息
11993
							$talkback="結束與 server 的連線";
11993
							$talkback="結束與 server 的連線";
11994
							
11994
 
11995
							#json encode 要回傳的訊息
11995
							#json encode 要回傳的訊息
11996
							$talkback=json_encode($talkback).PHP_EOL;
11996
							$talkback=json_encode($talkback).PHP_EOL;
11997
							
11997
 
11998
							#如果是 ws 模式
11998
							#如果是 ws 模式
11999
							if($conf["wsMode"]==="true"){
11999
							if($conf["wsMode"]==="true"){
12000
							
12000
 
12001
								#如果已經 handshake 過
12001
								#如果已經 handshake 過
12002
								if($connections[$conIndex]["handshaked"]==="true"){
12002
								if($connections[$conIndex]["handshaked"]==="true"){
12003
								
12003
 
12004
									#如果要 unmask	
12004
									#如果要 unmask
12005
									if($connections[$conIndex]["unmask"]==="true"){
12005
									if($connections[$conIndex]["unmask"]==="true"){
12006
								
12006
 
12007
										#函式說明:
12007
										#函式說明:
12008
										#加密 handshake 後要傳送的訊息 
12008
										#加密 handshake 後要傳送的訊息
12009
										#回傳結果:
12009
										#回傳結果:
12010
										#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
12010
										#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
12011
										#$result["error"],執行不正常結束的錯訊息陣列.
12011
										#$result["error"],執行不正常結束的錯訊息陣列.
12012
										#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
12012
										#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
12013
										#$result["argu"],陣列,使用的參數.
12013
										#$result["argu"],陣列,使用的參數.
12014
										#必填參數:
12014
										#必填參數:
12015
										#$conf["text"],字串,要加密的訊息.
12015
										#$conf["text"],字串,要加密的訊息.
12016
										$conf["webSock::encode"]["text"]=$talkback; 
12016
										$conf["webSock::encode"]["text"]=$talkback;
12017
										#可省略參數:
12017
										#可省略參數:
12018
										#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
12018
										#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
12019
										#$conf["payloadIsBin"]="false";
12019
										#$conf["payloadIsBin"]="false";
12020
										#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
12020
										#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
12021
										#$conf["ping"]="false";
12021
										#$conf["ping"]="false";
Line 12031... Line 12031...
12031
										#無.
12031
										#無.
12032
										#備註:
12032
										#備註:
12033
										#目前$conf["text"]長度超過125會出錯.
12033
										#目前$conf["text"]長度超過125會出錯.
12034
										$talkback=webSock::encode($conf["webSock::encode"]);
12034
										$talkback=webSock::encode($conf["webSock::encode"]);
12035
										unset($conf["webSock::encode"]);
12035
										unset($conf["webSock::encode"]);
12036
										
12036
 
12037
										#如果執行失敗
12037
										#如果執行失敗
12038
										if($talkback["status"]==="false"){
12038
										if($talkback["status"]==="false"){
12039
										
12039
 
12040
											#印出結果
12040
											#印出結果
12041
											var_dump($talkback);
12041
											var_dump($talkback);
12042
										
12042
 
12043
											#結束執行
12043
											#結束執行
12044
											exit;
12044
											exit;
12045
										
12045
 
12046
											}#if end
12046
											}#if end
12047
										
12047
 
12048
										#debug
12048
										#debug
12049
										#var_dump(__LINE__,$talkback);
12049
										#var_dump(__LINE__,$talkback);
12050
										
12050
 
12051
										}#if end
12051
										}#if end
12052
										
12052
 
12053
									}#if end
12053
									}#if end
12054
							
12054
 
12055
								}#if end
12055
								}#if end
12056
								
12056
 
12057
							#反之不為 web socket 訊息
12057
							#反之不為 web socket 訊息
12058
							else{
12058
							else{
12059
							
12059
 
12060
								#儲存成只有一個訊息
12060
								#儲存成只有一個訊息
12061
								$talkback["content"][]=$talkback;
12061
								$talkback["content"][]=$talkback;
12062
							
12062
 
12063
								}#else
12063
								}#else
12064
							
12064
 
12065
							#針對每個訊息的分段
12065
							#針對每個訊息的分段
12066
							foreach($talkback["content"] as $msg){
12066
							foreach($talkback["content"] as $msg){
12067
								
12067
 
12068
								#回傳訊息
12068
								#回傳訊息
12069
								$socket_write=socket_write($con["connect"], $msg, strlen($msg));
12069
								$socket_write=socket_write($con["connect"], $msg, strlen($msg));
12070
						
12070
 
12071
								#debug
12071
								#debug
12072
								#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
12072
								#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
12073
								
12073
 
12074
								}#foreach end
12074
								}#foreach end
12075
								
12075
 
12076
							#關閉被 client 的連線	
12076
							#關閉被 client 的連線
12077
							socket_close($con["connect"]);
12077
							socket_close($con["connect"]);
12078
							
12078
 
12079
							#清理連線的陣列
12079
							#清理連線的陣列
12080
							unset($connections[$conIndex]);
12080
							unset($connections[$conIndex]);
12081
							
12081
 
12082
							#跳到下一輪
12082
							#跳到下一輪
12083
							continue;
12083
							continue;
12084
							
12084
 
12085
							}#if end
12085
							}#if end
12086
							
12086
 
12087
						*/
12087
						*/
12088
						
12088
 
12089
						/* 舊的寫法
12089
						/* 舊的寫法
12090
						
12090
 
12091
						#如果收到的是 shutdown	
12091
						#如果收到的是 shutdown
12092
						if ($buf === 'shutdown') {
12092
						if ($buf === 'shutdown') {
12093
							
12093
 
12094
							#如果尚未認證
12094
							#如果尚未認證
12095
							if($connections[$conIndex]["auth"]==="false"){
12095
							if($connections[$conIndex]["auth"]==="false"){
12096
							
12096
 
12097
								#設置要回傳的訊息
12097
								#設置要回傳的訊息
12098
								$talkback=json_encode("permission denied");
12098
								$talkback=json_encode("permission denied");
12099
								
12099
 
12100
								#如果是 ws 模式
12100
								#如果是 ws 模式
12101
								if($conf["wsMode"]==="true"){
12101
								if($conf["wsMode"]==="true"){
12102
								
12102
 
12103
									#如果已經 handshake 過
12103
									#如果已經 handshake 過
12104
									if($connections[$conIndex]["handshaked"]==="true"){
12104
									if($connections[$conIndex]["handshaked"]==="true"){
12105
									
12105
 
12106
										#如果要 unmask	
12106
										#如果要 unmask
12107
										if($connections[$conIndex]["unmask"]==="true"){
12107
										if($connections[$conIndex]["unmask"]==="true"){
12108
									
12108
 
12109
											#函式說明:
12109
											#函式說明:
12110
											#加密 handshake 後要傳送的訊息 
12110
											#加密 handshake 後要傳送的訊息
12111
											#回傳結果:
12111
											#回傳結果:
12112
											#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
12112
											#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
12113
											#$result["error"],執行不正常結束的錯訊息陣列.
12113
											#$result["error"],執行不正常結束的錯訊息陣列.
12114
											#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
12114
											#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
12115
											#$result["argu"],陣列,使用的參數.
12115
											#$result["argu"],陣列,使用的參數.
12116
											#必填參數:
12116
											#必填參數:
12117
											#$conf["text"],字串,要加密的訊息.
12117
											#$conf["text"],字串,要加密的訊息.
12118
											$conf["webSock::encode"]["text"]=$talkback; 
12118
											$conf["webSock::encode"]["text"]=$talkback;
12119
											#可省略參數:
12119
											#可省略參數:
12120
											#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
12120
											#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
12121
											#$conf["payloadIsBin"]="false";
12121
											#$conf["payloadIsBin"]="false";
12122
											#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
12122
											#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
12123
											#$conf["ping"]="false";
12123
											#$conf["ping"]="false";
Line 12133... Line 12133...
12133
											#無.
12133
											#無.
12134
											#備註:
12134
											#備註:
12135
											#目前$conf["text"]長度超過125會出錯.
12135
											#目前$conf["text"]長度超過125會出錯.
12136
											$talkback=webSock::encode($conf["webSock::encode"]);
12136
											$talkback=webSock::encode($conf["webSock::encode"]);
12137
											unset($conf["webSock::encode"]);
12137
											unset($conf["webSock::encode"]);
12138
											
12138
 
12139
											#如果執行失敗
12139
											#如果執行失敗
12140
											if($talkback["status"]==="false"){
12140
											if($talkback["status"]==="false"){
12141
											
12141
 
12142
												#印出結果
12142
												#印出結果
12143
												var_dump($talkback);
12143
												var_dump($talkback);
12144
											
12144
 
12145
												#結束執行
12145
												#結束執行
12146
												exit;
12146
												exit;
12147
											
12147
 
12148
												}#if end
12148
												}#if end
12149
											
12149
 
12150
											#debug
12150
											#debug
12151
											#var_dump(__LINE__,$talkback);
12151
											#var_dump(__LINE__,$talkback);
12152
											
12152
 
12153
											}#if end
12153
											}#if end
12154
											
12154
 
12155
										}#if end
12155
										}#if end
12156
								
12156
 
12157
									}#if end
12157
									}#if end
12158
								
12158
 
12159
								#反之不為 web socket 訊息
12159
								#反之不為 web socket 訊息
12160
								else{
12160
								else{
12161
								
12161
 
12162
									#儲存成只有一個訊息
12162
									#儲存成只有一個訊息
12163
									$talkback["content"][]=$talkback;
12163
									$talkback["content"][]=$talkback;
12164
								
12164
 
12165
									}#else
12165
									}#else
12166
								
12166
 
12167
								#針對每個訊息的分段
12167
								#針對每個訊息的分段
12168
								foreach($talkback["content"] as $msg){
12168
								foreach($talkback["content"] as $msg){
12169
									
12169
 
12170
									#回傳訊息
12170
									#回傳訊息
12171
									$socket_write=socket_write($connections[$conIndex]["connect"], $msg, strlen($msg));
12171
									$socket_write=socket_write($connections[$conIndex]["connect"], $msg, strlen($msg));
12172
							
12172
 
12173
									#debug
12173
									#debug
12174
									#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
12174
									#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
12175
									
12175
 
12176
									}#foreach end
12176
									}#foreach end
12177
								
12177
 
12178
								#跳到下一輪
12178
								#跳到下一輪
12179
								continue;
12179
								continue;
12180
							
12180
 
12181
								}#if end
12181
								}#if end
12182
							
12182
 
12183
							#設置要回傳的訊息
12183
							#設置要回傳的訊息
12184
							$talkback="Server 即將關閉";
12184
							$talkback="Server 即將關閉";
12185
							
12185
 
12186
							#json encode 要回傳的訊息
12186
							#json encode 要回傳的訊息
12187
							$talkback=json_encode($talkback);
12187
							$talkback=json_encode($talkback);
12188
							
12188
 
12189
							#如果是 ws 模式
12189
							#如果是 ws 模式
12190
							if($conf["wsMode"]==="true"){
12190
							if($conf["wsMode"]==="true"){
12191
							
12191
 
12192
								#如果已經 handshake 過
12192
								#如果已經 handshake 過
12193
								if($connections[$conIndex]["handshaked"]==="true"){
12193
								if($connections[$conIndex]["handshaked"]==="true"){
12194
								
12194
 
12195
									#如果要 unmask	
12195
									#如果要 unmask
12196
									if($connections[$conIndex]["unmask"]==="true"){
12196
									if($connections[$conIndex]["unmask"]==="true"){
12197
								
12197
 
12198
										#函式說明:
12198
										#函式說明:
12199
										#加密 handshake 後要傳送的訊息 
12199
										#加密 handshake 後要傳送的訊息
12200
										#回傳結果:
12200
										#回傳結果:
12201
										#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
12201
										#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
12202
										#$result["error"],執行不正常結束的錯訊息陣列.
12202
										#$result["error"],執行不正常結束的錯訊息陣列.
12203
										#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
12203
										#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
12204
										#$result["argu"],陣列,使用的參數.
12204
										#$result["argu"],陣列,使用的參數.
12205
										#必填參數:
12205
										#必填參數:
12206
										#$conf["text"],字串,要加密的訊息.
12206
										#$conf["text"],字串,要加密的訊息.
12207
										$conf["webSock::encode"]["text"]=$talkback; 
12207
										$conf["webSock::encode"]["text"]=$talkback;
12208
										#可省略參數:
12208
										#可省略參數:
12209
										#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
12209
										#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
12210
										#$conf["payloadIsBin"]="false";
12210
										#$conf["payloadIsBin"]="false";
12211
										#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
12211
										#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
12212
										#$conf["ping"]="false";
12212
										#$conf["ping"]="false";
Line 12222... Line 12222...
12222
										#無.
12222
										#無.
12223
										#備註:
12223
										#備註:
12224
										#無.
12224
										#無.
12225
										$talkback=webSock::encode($conf["webSock::encode"]);
12225
										$talkback=webSock::encode($conf["webSock::encode"]);
12226
										unset($conf["webSock::encode"]);
12226
										unset($conf["webSock::encode"]);
12227
										
12227
 
12228
										#如果執行失敗
12228
										#如果執行失敗
12229
										if($talkback["status"]==="false"){
12229
										if($talkback["status"]==="false"){
12230
										
12230
 
12231
											#印出結果
12231
											#印出結果
12232
											var_dump($talkback);
12232
											var_dump($talkback);
12233
										
12233
 
12234
											#結束執行
12234
											#結束執行
12235
											exit;
12235
											exit;
12236
										
12236
 
12237
											}#if end
12237
											}#if end
12238
										
12238
 
12239
										#debug
12239
										#debug
12240
										#var_dump(__LINE__,$talkback);
12240
										#var_dump(__LINE__,$talkback);
12241
										
12241
 
12242
										}#if end
12242
										}#if end
12243
									
12243
 
12244
									}#if end
12244
									}#if end
12245
							
12245
 
12246
								}#if end
12246
								}#if end
12247
							
12247
 
12248
							#反之不為 web socket 訊息
12248
							#反之不為 web socket 訊息
12249
							else{
12249
							else{
12250
							
12250
 
12251
								#儲存成只有一個訊息
12251
								#儲存成只有一個訊息
12252
								$talkback["content"][]=$talkback;
12252
								$talkback["content"][]=$talkback;
12253
							
12253
 
12254
								}#else
12254
								}#else
12255
							
12255
 
12256
							#針對每個訊息的分段
12256
							#針對每個訊息的分段
12257
							foreach($talkback["content"] as $msg){
12257
							foreach($talkback["content"] as $msg){
12258
								
12258
 
12259
								#回傳訊息
12259
								#回傳訊息
12260
								$socket_write=socket_write($con["connect"], $msg, strlen($msg));
12260
								$socket_write=socket_write($con["connect"], $msg, strlen($msg));
12261
						
12261
 
12262
								#debug
12262
								#debug
12263
								#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
12263
								#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
12264
								
12264
 
12265
								}#foreach end
12265
								}#foreach end
12266
							
12266
 
12267
							#關閉用戶的連線
12267
							#關閉用戶的連線
12268
							socket_close($con["connect"]);
12268
							socket_close($con["connect"]);
12269
								
12269
 
12270
							#跳到最外層,結束webSocket server
12270
							#跳到最外層,結束webSocket server
12271
							break 2;
12271
							break 2;
12272
							
12272
 
12273
							}#if end
12273
							}#if end
12274
						
12274
 
12275
						*/
12275
						*/
12276
						
12276
 
12277
						/* 舊的寫法
12277
						/* 舊的寫法
12278
						
12278
 
12279
						#如果收到的訊息為"auth:"開頭
12279
						#如果收到的訊息為"auth:"開頭
12280
						if(strpos($buf,"auth:")){
12280
						if(strpos($buf,"auth:")){
12281
						
12281
 
12282
							#剔除 "auth:" 前面的內容
12282
							#剔除 "auth:" 前面的內容
12283
							#函式說明:
12283
							#函式說明:
12284
							#將字串特定關鍵字與其前面的內容剔除
12284
							#將字串特定關鍵字與其前面的內容剔除
12285
							#回傳結果:
12285
							#回傳結果:
12286
							#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
12286
							#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 12306... Line 12306...
12306
							#無.
12306
							#無.
12307
							#備註:
12307
							#備註:
12308
							#無.
12308
							#無.
12309
							$delStrBeforeKeyWord=stringProcess::delStrBeforeKeyWord($conf["stringProcess::delStrBeforeKeyWord"]);
12309
							$delStrBeforeKeyWord=stringProcess::delStrBeforeKeyWord($conf["stringProcess::delStrBeforeKeyWord"]);
12310
							unset($conf["stringProcess::delStrBeforeKeyWord"]);
12310
							unset($conf["stringProcess::delStrBeforeKeyWord"]);
12311
						
12311
 
12312
							#如果執行失敗
12312
							#如果執行失敗
12313
							if($delStrBeforeKeyWord["status"]==="false"){
12313
							if($delStrBeforeKeyWord["status"]==="false"){
12314
							
12314
 
12315
								#印出結果
12315
								#印出結果
12316
								var_dump($delStrBeforeKeyWord);
12316
								var_dump($delStrBeforeKeyWord);
12317
							
12317
 
12318
								#結束執行
12318
								#結束執行
12319
								exit;
12319
								exit;
12320
							
12320
 
12321
								}#if end
12321
								}#if end
12322
							
12322
 
12323
							#還原編碼
12323
							#還原編碼
12324
							#$delStrBeforeKeyWord["content"];
12324
							#$delStrBeforeKeyWord["content"];
12325
						
12325
 
12326
							#gpg解密
12326
							#gpg解密
12327
							#...
12327
							#...
12328
							
12328
 
12329
							#檢查賬號密碼
12329
							#檢查賬號密碼
12330
							#...
12330
							#...
12331
						
12331
 
12332
							}#if end
12332
							}#if end
12333
							
12333
 
12334
						*/
12334
						*/
12335
						
12335
 
12336
						#以下為其他收到的訊息內容
12336
						#以下為其他收到的訊息內容
12337
						
12337
 
12338
						/* 舊的寫法
12338
						/* 舊的寫法
12339
						
12339
 
12340
						#如果尚未認證
12340
						#如果尚未認證
12341
						if($connections[$conIndex]["auth"]==="false"){
12341
						if($connections[$conIndex]["auth"]==="false"){
12342
						
12342
 
12343
							#設置要回傳的訊息
12343
							#設置要回傳的訊息
12344
							$talkback=json_encode("permission denied");
12344
							$talkback=json_encode("permission denied");
12345
							
12345
 
12346
							#如果是 ws 模式
12346
							#如果是 ws 模式
12347
							if($conf["wsMode"]==="true"){
12347
							if($conf["wsMode"]==="true"){
12348
							
12348
 
12349
								#如果已經 handshake 過
12349
								#如果已經 handshake 過
12350
								if($connections[$conIndex]["handshaked"]==="true"){
12350
								if($connections[$conIndex]["handshaked"]==="true"){
12351
								
12351
 
12352
									#如果要 unmask	
12352
									#如果要 unmask
12353
									if($connections[$conIndex]["unmask"]==="true"){
12353
									if($connections[$conIndex]["unmask"]==="true"){
12354
								
12354
 
12355
										#函式說明:
12355
										#函式說明:
12356
										#加密 handshake 後要傳送的訊息 
12356
										#加密 handshake 後要傳送的訊息
12357
										#回傳結果:
12357
										#回傳結果:
12358
										#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
12358
										#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
12359
										#$result["error"],執行不正常結束的錯訊息陣列.
12359
										#$result["error"],執行不正常結束的錯訊息陣列.
12360
										#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
12360
										#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
12361
										#$result["argu"],陣列,使用的參數.
12361
										#$result["argu"],陣列,使用的參數.
12362
										#必填參數:
12362
										#必填參數:
12363
										#$conf["text"],字串,要加密的訊息.
12363
										#$conf["text"],字串,要加密的訊息.
12364
										$conf["webSock::encode"]["text"]=$talkback; 
12364
										$conf["webSock::encode"]["text"]=$talkback;
12365
										#可省略參數:
12365
										#可省略參數:
12366
										#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
12366
										#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
12367
										#$conf["payloadIsBin"]="false";
12367
										#$conf["payloadIsBin"]="false";
12368
										#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
12368
										#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
12369
										#$conf["ping"]="false";
12369
										#$conf["ping"]="false";
Line 12379... Line 12379...
12379
										#無.
12379
										#無.
12380
										#備註:
12380
										#備註:
12381
										#目前$conf["text"]長度超過125會出錯.
12381
										#目前$conf["text"]長度超過125會出錯.
12382
										$talkback=webSock::encode($conf["webSock::encode"]);
12382
										$talkback=webSock::encode($conf["webSock::encode"]);
12383
										unset($conf["webSock::encode"]);
12383
										unset($conf["webSock::encode"]);
12384
										
12384
 
12385
										#如果執行失敗
12385
										#如果執行失敗
12386
										if($talkback["status"]==="false"){
12386
										if($talkback["status"]==="false"){
12387
										
12387
 
12388
											#印出結果
12388
											#印出結果
12389
											var_dump($talkback);
12389
											var_dump($talkback);
12390
										
12390
 
12391
											#結束執行
12391
											#結束執行
12392
											exit;
12392
											exit;
12393
										
12393
 
12394
											}#if end
12394
											}#if end
12395
										
12395
 
12396
										#debug
12396
										#debug
12397
										#var_dump(__LINE__,$talkback);
12397
										#var_dump(__LINE__,$talkback);
12398
										
12398
 
12399
										}#if end
12399
										}#if end
12400
										
12400
 
12401
									}#if end
12401
									}#if end
12402
							
12402
 
12403
								}#if end
12403
								}#if end
12404
							
12404
 
12405
							#反之不為 web socket 訊息
12405
							#反之不為 web socket 訊息
12406
							else{
12406
							else{
12407
							
12407
 
12408
								#儲存成只有一個訊息
12408
								#儲存成只有一個訊息
12409
								$talkback["content"][]=$talkback;
12409
								$talkback["content"][]=$talkback;
12410
							
12410
 
12411
								}#else
12411
								}#else
12412
							
12412
 
12413
							#針對每個訊息的分段
12413
							#針對每個訊息的分段
12414
							foreach($talkback["content"] as $msg){
12414
							foreach($talkback["content"] as $msg){
12415
								
12415
 
12416
								#回傳訊息
12416
								#回傳訊息
12417
								$socket_write=socket_write($connections[$conIndex]["connect"], $msg, strlen($msg));
12417
								$socket_write=socket_write($connections[$conIndex]["connect"], $msg, strlen($msg));
12418
						
12418
 
12419
								#debug
12419
								#debug
12420
								#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
12420
								#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
12421
								
12421
 
12422
								}#foreach end
12422
								}#foreach end
12423
							
12423
 
12424
							#跳到下一輪
12424
							#跳到下一輪
12425
							continue;
12425
							continue;
12426
						
12426
 
12427
							}#if end
12427
							}#if end
12428
						
12428
 
12429
						*/
12429
						*/
12430
						
12430
 
12431
						#如果有指定的外掛函式
12431
						#如果有指定的外掛函式
12432
						if(isset($conf["processFuncs"])){
12432
						if(isset($conf["processFuncs"])){
12433
						
12433
 
12434
							#針對每個用來處理輸入字串的函式
12434
							#針對每個用來處理輸入字串的函式
12435
							foreach($conf["processFuncs"] as $proFunc){
12435
							foreach($conf["processFuncs"] as $proFunc){
12436
							
12436
 
12437
								#debug
12437
								#debug
12438
								#var_dump(__LINE__,$proFunc);
12438
								#var_dump(__LINE__,$proFunc);
12439
								
12439
 
12440
								#函式說明:
12440
								#函式說明:
12441
								#提供webSock::nativeSocketTcpIpServer用於接受檢查權限的功能.
12441
								#提供webSock::nativeSocketTcpIpServer用於接受檢查權限的功能.
12442
								#回傳結果:
12442
								#回傳結果:
12443
								#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
12443
								#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
12444
								#$result["error"],執行不正常結束的錯訊息陣列.
12444
								#$result["error"],執行不正常結束的錯訊息陣列.
Line 12474... Line 12474...
12474
								$checkAuth=webSocket::checkAuth($conf["webSocket::checkAuth"]);
12474
								$checkAuth=webSocket::checkAuth($conf["webSocket::checkAuth"]);
12475
								unset($conf["webSocket::checkAuth"]);
12475
								unset($conf["webSocket::checkAuth"]);
12476
 
12476
 
12477
								#如果沒有權限
12477
								#如果沒有權限
12478
								if($checkAuth["status"]==="false"){
12478
								if($checkAuth["status"]==="false"){
12479
								
12479
 
12480
									#換看下個 $proFunc
12480
									#換看下個 $proFunc
12481
									continue;
12481
									continue;
12482
								
12482
 
12483
									}#if end
12483
									}#if end
12484
 
12484
 
12485
								#呼叫函式,參數為輸入的字串data跟client socket, server socket, 每個 socket
12485
								#呼叫函式,參數為輸入的字串data跟client socket, server socket, 每個 socket
12486
								#https://www.php.net/manual/en/function.call-user-func.php
12486
								#https://www.php.net/manual/en/function.call-user-func.php
12487
								$param=array("data"=>&$buf,"serverSock"=>&$sock,"clientSock"=>&$con["connect"],"clientInfo"=>&$connections[$conIndex],"clientIndex"=>&$conIndex,"allConn"=>&$connections);
12487
								$param=array("data"=>&$buf,"serverSock"=>&$sock,"clientSock"=>&$con["connect"],"clientInfo"=>&$connections[$conIndex],"clientIndex"=>&$conIndex,"allConn"=>&$connections);
Line 12492... Line 12492...
12492
								#debug
12492
								#debug
12493
								#var_dump(__LINE__,$result);
12493
								#var_dump(__LINE__,$result);
12494
 
12494
 
12495
								#如果有回傳結果
12495
								#如果有回傳結果
12496
								if($result!==NULL){
12496
								if($result!==NULL){
12497
								
12497
 
12498
									#如果回傳的形態為陣列
12498
									#如果回傳的形態為陣列
12499
									if(gettype($result)==="array"){
12499
									if(gettype($result)==="array"){
12500
 
12500
 
12501
										#如果執行出錯
12501
										#如果執行出錯
12502
										if($result["status"]==="false"){
12502
										if($result["status"]==="false"){
12503
 
12503
 
12504
											#提示錯誤
12504
											#提示錯誤
12505
											echo "run function ".$proFunc." with params:".print_r($params,true)." failed!".PHP_EOL;
12505
											echo "run function ".$proFunc." with params:".print_r($params,true)." failed!".PHP_EOL;
12506
											
12506
 
12507
											#提示會往後面執行
12507
											#提示會往後面執行
12508
											echo "go next function.".PHP_EOL;
12508
											echo "go next function.".PHP_EOL;
12509
											
12509
 
12510
											#跳到下一輪
12510
											#跳到下一輪
12511
											#continue;
12511
											#continue;
12512
 
12512
 
12513
											}#if end
12513
											}#if end
12514
											
12514
 
12515
										#如果結果為跳下一輪
12515
										#如果結果為跳下一輪
12516
										else if($result["status"]==="continue"){
12516
										else if($result["status"]==="continue"){
12517
										
12517
 
12518
											#提示成功
12518
											#提示成功
12519
											echo "run function ".$proFunc." with params:".print_r($params,true)." success.".PHP_EOL;
12519
											echo "run function ".$proFunc." with params:".print_r($params,true)." success.".PHP_EOL;
12520
											
12520
 
12521
											#提示會往後面執行
12521
											#提示會往後面執行
12522
											echo "go next function.".PHP_EOL;
12522
											echo "go next function.".PHP_EOL;
12523
										
12523
 
12524
											#跳下一輪
12524
											#跳下一輪
12525
											#continue;
12525
											#continue;
12526
										
12526
 
12527
											}#if end
12527
											}#if end
12528
										
12528
 
12529
										#如果已經被處理好了且不准移交給後面的處理
12529
										#如果已經被處理好了且不准移交給後面的處理
12530
										else if($result["status"]==="true"){
12530
										else if($result["status"]==="true"){
12531
										
12531
 
12532
											#提示成功
12532
											#提示成功
12533
											echo "run function ".$proFunc." with params:".print_r($params,true)." success.".PHP_EOL;
12533
											echo "run function ".$proFunc." with params:".print_r($params,true)." success.".PHP_EOL;
12534
											
12534
 
12535
											#提示在此打住.
12535
											#提示在此打住.
12536
											echo "end process,".PHP_EOL;
12536
											echo "end process,".PHP_EOL;
12537
											
12537
 
12538
											#後面就不做了
12538
											#後面就不做了
12539
											break;
12539
											break;
12540
										
12540
 
12541
											}#if end
12541
											}#if end
12542
										
12542
 
12543
										}#if end
12543
										}#if end
12544
 
12544
 
12545
									#反之回傳形態錯誤
12545
									#反之回傳形態錯誤
12546
									else{
12546
									else{
12547
									
12547
 
12548
										#提示錯誤
12548
										#提示錯誤
12549
										echo "run function ".$proFunc." with params:".print_r($params,true)." success, but format of return is incorrect!".PHP_EOL;
12549
										echo "run function ".$proFunc." with params:".print_r($params,true)." success, but format of return is incorrect!".PHP_EOL;
12550
										
12550
 
12551
										#提示會往後面執行
12551
										#提示會往後面執行
12552
										echo "go next function.".PHP_EOL;
12552
										echo "go next function.".PHP_EOL;
12553
									
12553
 
12554
										}#else end
12554
										}#else end
12555
 
12555
 
12556
									}#if end
12556
									}#if end
12557
									
12557
 
12558
								#如果沒有回傳結果
12558
								#如果沒有回傳結果
12559
								else{
12559
								else{
12560
								
12560
 
12561
									#提示錯誤
12561
									#提示錯誤
12562
									echo "run function ".$proFunc." with params:".print_r($params,true)." success, but there is no return!".PHP_EOL;
12562
									echo "run function ".$proFunc." with params:".print_r($params,true)." success, but there is no return!".PHP_EOL;
12563
									
12563
 
12564
									#跳下一輪
12564
									#跳下一輪
12565
									#continue;
12565
									#continue;
12566
								
12566
 
12567
									}#else end
12567
									}#else end
12568
 
12568
 
12569
								}#foreach end
12569
								}#foreach end
12570
							
12570
 
12571
							}#if end
12571
							}#if end
12572
						
12572
 
12573
						#如果有要接收訊息的id
12573
						#如果有要接收訊息的id
12574
						if(count($connections[$conIndex]["talkTo"])>0){
12574
						if(count($connections[$conIndex]["talkTo"])>0){
12575
							
12575
 
12576
							#初始化要傳送給哪些id
12576
							#初始化要傳送給哪些id
12577
							$broadcastId=array();
12577
							$broadcastId=array();
12578
							
12578
 
12579
							#依據每個連線資訊
12579
							#依據每個連線資訊
12580
							foreach($connections as $index=>$broadcast){
12580
							foreach($connections as $index=>$broadcast){
12581
								
12581
 
12582
								#儲存每個
12582
								#儲存每個
12583
								$broadcastId[$index]=$broadcast["id"];
12583
								$broadcastId[$index]=$broadcast["id"];
12584
								
12584
 
12585
								}#foreach end
12585
								}#foreach end
12586
						
12586
 
12587
							#檢查哪些人是要接收到訊息的	
12587
							#檢查哪些人是要接收到訊息的
12588
							#涵式說明:
12588
							#涵式說明:
12589
							#檢查多個數值,是否與陣列裏面的某些元素相同。
12589
							#檢查多個數值,是否與陣列裏面的某些元素相同。
12590
							#回傳的結果:
12590
							#回傳的結果:
12591
							#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
12591
							#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
12592
							#$result["function"],當前執行的函數
12592
							#$result["function"],當前執行的函數
12593
							#$result["error"],涵式錯誤訊息,若爲""則表示沒有錯誤 
12593
							#$result["error"],涵式錯誤訊息,若爲""則表示沒有錯誤
12594
							#$result["founded"][$i],若爲"false"代表沒有相同的元素;反之"true"代表有相同的元素,$i爲第i+1個$conf["inputElement"]參數判斷後的結果.
12594
							#$result["founded"][$i],若爲"false"代表沒有相同的元素;反之"true"代表有相同的元素,$i爲第i+1個$conf["inputElement"]參數判斷後的結果.
12595
							#$result["key"][$i],為找到$conf["inputElement"]第$i+1個元素的值是在$conf["blackBoxElement"]中的哪個元素.
12595
							#$result["key"][$i],為找到$conf["inputElement"]第$i+1個元素的值是在$conf["blackBoxElement"]中的哪個元素.
12596
							#必填的參數:
12596
							#必填的參數:
12597
							$conf["search::checkEquality"]["inputElement"]=$connections[$conIndex]["talkTo"];#想要找到的變數/數值.
12597
							$conf["search::checkEquality"]["inputElement"]=$connections[$conIndex]["talkTo"];#想要找到的變數/數值.
12598
							$conf["search::checkEquality"]["blackBoxElement"]=$broadcastId;#要從哪些變數/數值去尋找.
12598
							$conf["search::checkEquality"]["blackBoxElement"]=$broadcastId;#要從哪些變數/數值去尋找.
12599
							$checkEquality=search::checkEquality($conf["search::checkEquality"]);
12599
							$checkEquality=search::checkEquality($conf["search::checkEquality"]);
12600
							unset($conf["search::checkEquality"]);
12600
							unset($conf["search::checkEquality"]);
12601
							
12601
 
12602
							#如果檢查失敗
12602
							#如果檢查失敗
12603
							if($checkEquality["status"]==="false"){
12603
							if($checkEquality["status"]==="false"){
12604
								
12604
 
12605
								#設置執行失敗
12605
								#設置執行失敗
12606
								$result["status"]="false";
12606
								$result["status"]="false";
12607
							
12607
 
12608
								#設置執行錯誤訊息
12608
								#設置執行錯誤訊息
12609
								$result["error"]=$checkEquality;
12609
								$result["error"]=$checkEquality;
12610
								
12610
 
12611
								#回傳結果
12611
								#回傳結果
12612
								return $result;
12612
								return $result;
12613
								
12613
 
12614
								}#if end
12614
								}#if end
12615
							
12615
 
12616
							#針對每個搜尋的結果
12616
							#針對每個搜尋的結果
12617
							foreach($checkEquality["founded"] as $key=>$found){
12617
							foreach($checkEquality["founded"] as $key=>$found){
12618
								
12618
 
12619
								#如果有找到
12619
								#如果有找到
12620
								if($found==="true"){
12620
								if($found==="true"){
12621
									
12621
 
12622
									#取得要接收訊息的msgSock
12622
									#取得要接收訊息的msgSock
12623
									$receiveMsgSock=$connections[$checkEquality["key"][$key]]["connect"];
12623
									$receiveMsgSock=$connections[$checkEquality["key"][$key]]["connect"];
12624
									
12624
 
12625
									#設置要傳送的訊息
12625
									#設置要傳送的訊息
12626
									$talkback = $connections[$conIndex]["id"]." said ".$buf;
12626
									$talkback = $connections[$conIndex]["id"]." said ".$buf;
12627
									
12627
 
12628
									#json encode 要傳送的訊息
12628
									#json encode 要傳送的訊息
12629
									$talkback=json_encode($talkback).PHP_EOL;
12629
									$talkback=json_encode($talkback).PHP_EOL;
12630
									
12630
 
12631
									#如果是 ws 模式
12631
									#如果是 ws 模式
12632
									if($conf["wsMode"]==="true"){
12632
									if($conf["wsMode"]==="true"){
12633
									
12633
 
12634
										#如果已經 handshake 過
12634
										#如果已經 handshake 過
12635
										if($connections[$conIndex]["handshaked"]==="true"){
12635
										if($connections[$conIndex]["handshaked"]==="true"){
12636
										
12636
 
12637
											#如果要 unmask	
12637
											#如果要 unmask
12638
											if($connections[$conIndex]["unmask"]==="true"){
12638
											if($connections[$conIndex]["unmask"]==="true"){
12639
										
12639
 
12640
												#加密
12640
												#加密
12641
												$conf["webSock::encode"]["text"]=$talkback;
12641
												$conf["webSock::encode"]["text"]=$talkback;
12642
												$talkback=webSock::encode($conf["webSock::encode"]);
12642
												$talkback=webSock::encode($conf["webSock::encode"]);
12643
												unset($conf["webSock::encode"]);
12643
												unset($conf["webSock::encode"]);
12644
												
12644
 
12645
												}#if end
12645
												}#if end
12646
											
12646
 
12647
											}#if end
12647
											}#if end
12648
									
12648
 
12649
										}#if end
12649
										}#if end
12650
										
12650
 
12651
									#反之不為 web socket 訊息
12651
									#反之不為 web socket 訊息
12652
									else{
12652
									else{
12653
									
12653
 
12654
										#儲存成只有一個訊息
12654
										#儲存成只有一個訊息
12655
										$talkback["content"][]=$talkback;
12655
										$talkback["content"][]=$talkback;
12656
									
12656
 
12657
										}#else
12657
										}#else
12658
									
12658
 
12659
									
12659
 
12660
									#針對每個訊息的分段
12660
									#針對每個訊息的分段
12661
									foreach($talkback["content"] as $msg){
12661
									foreach($talkback["content"] as $msg){
12662
										
12662
 
12663
										#回傳訊息
12663
										#回傳訊息
12664
										$socket_write=socket_write($receiveMsgSock, $msg, strlen($msg));
12664
										$socket_write=socket_write($receiveMsgSock, $msg, strlen($msg));
12665
								
12665
 
12666
										#debug
12666
										#debug
12667
										#var_dump(__LINE__,$receiveMsgSock,socket_strerror(socket_last_error($receiveMsgSock)));
12667
										#var_dump(__LINE__,$receiveMsgSock,socket_strerror(socket_last_error($receiveMsgSock)));
12668
										
12668
 
12669
										}#foreach end
12669
										}#foreach end
12670
									
12670
 
12671
									}#if end
12671
									}#if end
12672
								
12672
 
12673
								}#foreach end
12673
								}#foreach end
12674
							
12674
 
12675
							}#if end
12675
							}#if end
12676
						
12676
 
12677
						}#if end
12677
						}#if end
12678
					
12678
 
12679
					}#foreach end
12679
					}#foreach end
12680
				
12680
 
12681
				#do end 
12681
				#do end
12682
				}while(true);
12682
				}while(true);
12683
 
12683
 
12684
			#提示關閉 server socket
12684
			#提示關閉 server socket
12685
			echo "關閉 server socket".PHP_EOL;
12685
			echo "關閉 server socket".PHP_EOL;
12686
 
12686
 
12687
			#關閉 socket
12687
			#關閉 socket
12688
			socket_close($sock);
12688
			socket_close($sock);
12689
			
12689
 
12690
			#do end
12690
			#do end
12691
			}while(true);
12691
			}while(true);
12692
		
12692
 
12693
		#設置執行正常
12693
		#設置執行正常
12694
		$result["status"]="true";
12694
		$result["status"]="true";
12695
		
12695
 
12696
		#回傳結果
12696
		#回傳結果
12697
		return $result;
12697
		return $result;
12698
		
12698
 
12699
		}#function nativeSocketTcpIpServer end
12699
		}#function nativeSocketTcpIpServer end
12700
	
12700
 
12701
	/*	
12701
	/*
12702
	#函式說明:
12702
	#函式說明:
12703
	#連線到webSocket,會印出建立好的unixDomainSocket位置與名稱.
12703
	#連線到webSocket,會印出建立好的unixDomainSocket位置與名稱.
12704
	#回傳結果:
12704
	#回傳結果:
12705
	#$result["status"],執行正常與否,"true"代表正常,"false"代表不正常.
12705
	#$result["status"],執行正常與否,"true"代表正常,"false"代表不正常.
12706
	#$result["error"],錯誤訊息陣列.
12706
	#$result["error"],錯誤訊息陣列.
Line 12725... Line 12725...
12725
	#https://www.php.net/manual/en/context.ssl.php
12725
	#https://www.php.net/manual/en/context.ssl.php
12726
	#備註:
12726
	#備註:
12727
	#建構中...
12727
	#建構中...
12728
	*/
12728
	*/
12729
	public static function clientDaemon(&$conf){
12729
	public static function clientDaemon(&$conf){
12730
		
12730
 
12731
		#初始化要回傳的結果
12731
		#初始化要回傳的結果
12732
		$result=array();
12732
		$result=array();
12733
 
12733
 
12734
		#取得當前執行的函數名稱
12734
		#取得當前執行的函數名稱
12735
		$result["function"]=__FUNCTION__;
12735
		$result["function"]=__FUNCTION__;
Line 12737... Line 12737...
12737
		#涵式說明:
12737
		#涵式說明:
12738
		#判斷當前環境為web還是cmd
12738
		#判斷當前環境為web還是cmd
12739
		#回傳結果:
12739
		#回傳結果:
12740
		#$result,"web"或"cmd"
12740
		#$result,"web"或"cmd"
12741
		if(csInformation::getEnv()==="web"){
12741
		if(csInformation::getEnv()==="web"){
12742
			
12742
 
12743
			#設置執行失敗
12743
			#設置執行失敗
12744
			$result["status"]="false";
12744
			$result["status"]="false";
12745
			
12745
 
12746
			#設置執行錯誤訊息
12746
			#設置執行錯誤訊息
12747
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
12747
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
12748
			
12748
 
12749
			#回傳結果
12749
			#回傳結果
12750
			return $result;
12750
			return $result;
12751
			
12751
 
12752
			}#if end
12752
			}#if end
12753
 
12753
 
12754
		#取得參數
12754
		#取得參數
12755
		$result["argu"]=$conf;
12755
		$result["argu"]=$conf;
12756
 
12756
 
12757
		#如果 $conf 不為陣列
12757
		#如果 $conf 不為陣列
12758
		if(gettype($conf)!="array"){
12758
		if(gettype($conf)!="array"){
12759
		
12759
 
12760
			#設置執行失敗
12760
			#設置執行失敗
12761
			$result["status"]="false";
12761
			$result["status"]="false";
12762
		
12762
 
12763
			#設置執行錯誤訊息
12763
			#設置執行錯誤訊息
12764
			$result["error"][]="\$conf變數須為陣列形態";
12764
			$result["error"][]="\$conf變數須為陣列形態";
12765
		
12765
 
12766
			#如果傳入的參數為 null
12766
			#如果傳入的參數為 null
12767
			if($conf==null){
12767
			if($conf==null){
12768
			
12768
 
12769
				#設置執行錯誤訊息
12769
				#設置執行錯誤訊息
12770
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
12770
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
12771
			
12771
 
12772
				}#if end
12772
				}#if end
12773
 
12773
 
12774
			#回傳結果
12774
			#回傳結果
12775
			return $result;
12775
			return $result;
12776
		
12776
 
12777
			}#if end
12777
			}#if end
12778
		
12778
 
12779
		#檢查參數
12779
		#檢查參數
12780
		#函式說明:
12780
		#函式說明:
12781
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
12781
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
12782
		#回傳結果:
12782
		#回傳結果:
12783
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
12783
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 12829... Line 12829...
12829
		#array_keys=>http://php.net/manual/en/function.array-keys.php
12829
		#array_keys=>http://php.net/manual/en/function.array-keys.php
12830
		#備註:
12830
		#備註:
12831
		#無.
12831
		#無.
12832
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
12832
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
12833
		unset($conf["variableCheck::checkArguments"]);
12833
		unset($conf["variableCheck::checkArguments"]);
12834
		
12834
 
12835
		#如果檢查參數失敗
12835
		#如果檢查參數失敗
12836
		if($checkArguments["status"]==="false"){
12836
		if($checkArguments["status"]==="false"){
12837
			
12837
 
12838
			#設置執行失敗
12838
			#設置執行失敗
12839
			$result["status"]="false";
12839
			$result["status"]="false";
12840
		
12840
 
12841
			#設置執行錯誤訊息
12841
			#設置執行錯誤訊息
12842
			$result["error"]=$checkArguments;
12842
			$result["error"]=$checkArguments;
12843
			
12843
 
12844
			#回傳結果
12844
			#回傳結果
12845
			return $result;
12845
			return $result;
12846
			
12846
 
12847
			}#if end
12847
			}#if end
12848
			
12848
 
12849
		#如果檢查參數不通過
12849
		#如果檢查參數不通過
12850
		if($checkArguments["passed"]==="false"){
12850
		if($checkArguments["passed"]==="false"){
12851
			
12851
 
12852
			#設置執行失敗
12852
			#設置執行失敗
12853
			$result["status"]="false";
12853
			$result["status"]="false";
12854
		
12854
 
12855
			#設置執行錯誤訊息
12855
			#設置執行錯誤訊息
12856
			$result["error"]=$checkArguments;
12856
			$result["error"]=$checkArguments;
12857
			
12857
 
12858
			#回傳結果
12858
			#回傳結果
12859
			return $result;
12859
			return $result;
12860
			
12860
 
12861
			}#if end
12861
			}#if end
12862
		
12862
 
12863
		#如果targetPort沒有設置
12863
		#如果targetPort沒有設置
12864
		if(!isset($conf["targetPort"])){
12864
		if(!isset($conf["targetPort"])){
12865
		
12865
 
12866
			#port預設為80
12866
			#port預設為80
12867
			$conf["targetPort"]="80";
12867
			$conf["targetPort"]="80";
12868
		
12868
 
12869
			#如果有啟用 ssl
12869
			#如果有啟用 ssl
12870
			if($conf["ssl"]==="true"){
12870
			if($conf["ssl"]==="true"){
12871
			
12871
 
12872
				#port預設為443
12872
				#port預設為443
12873
				$conf["targetPort"]="443";
12873
				$conf["targetPort"]="443";
12874
			
12874
 
12875
				}#if end
12875
				}#if end
12876
		
12876
 
12877
			}#if end
12877
			}#if end
12878
		
12878
 
12879
		#回報任何錯誤
12879
		#回報任何錯誤
12880
		#error_reporting(E_ALL);
12880
		#error_reporting(E_ALL);
12881
		
12881
 
12882
		#如果要使用 ssl
12882
		#如果要使用 ssl
12883
		if($conf["ssl"]==="true"){
12883
		if($conf["ssl"]==="true"){
12884
		
12884
 
12885
			#預設的path
12885
			#預設的path
12886
			$path="/";
12886
			$path="/";
12887
		
12887
 
12888
			#分割出server位置與對應GET的path
12888
			#分割出server位置與對應GET的path
12889
			#函式說明:
12889
			#函式說明:
12890
			#將固定格式的字串分開,並回傳分開的結果.
12890
			#將固定格式的字串分開,並回傳分開的結果.
12891
			#回傳結果:
12891
			#回傳結果:
12892
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
12892
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 12909... Line 12909...
12909
			#無.
12909
			#無.
12910
			#備註:
12910
			#備註:
12911
			#無.
12911
			#無.
12912
			$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
12912
			$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
12913
			unset($conf["stringProcess::spiltString"]);
12913
			unset($conf["stringProcess::spiltString"]);
12914
		
12914
 
12915
			#如果檢查執行失敗
12915
			#如果檢查執行失敗
12916
			if($spiltString["status"]==="false"){
12916
			if($spiltString["status"]==="false"){
12917
				
12917
 
12918
				#設置執行失敗
12918
				#設置執行失敗
12919
				$result["status"]="false";
12919
				$result["status"]="false";
12920
			
12920
 
12921
				#設置執行錯誤訊息
12921
				#設置執行錯誤訊息
12922
				$result["error"]=$spiltString;
12922
				$result["error"]=$spiltString;
12923
				
12923
 
12924
				#回傳結果
12924
				#回傳結果
12925
				return $result;
12925
				return $result;
12926
				
12926
 
12927
				}#if end
12927
				}#if end
12928
				
12928
 
12929
			#如果含有 /
12929
			#如果含有 /
12930
			if($spiltString["found"]==="true"){
12930
			if($spiltString["found"]==="true"){
12931
				
12931
 
12932
				#函式說明:
12932
				#函式說明:
12933
				#將字串特定關鍵字與其前面的內容剔除
12933
				#將字串特定關鍵字與其前面的內容剔除
12934
				#回傳結果:
12934
				#回傳結果:
12935
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
12935
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
12936
				#$result["error"],錯誤訊息陣列.
12936
				#$result["error"],錯誤訊息陣列.
12937
				#$result["warning"],警告訊息鎮列.
12937
				#$result["warning"],警告訊息鎮列.
12938
				#$result["founded"],有無找到定字串"true"代表有,"false"代表沒有.
12938
				#$result["founded"],有無找到定字串"true"代表有,"false"代表沒有.
12939
				#$result["function"],當前執行的函數名稱.
12939
				#$result["function"],當前執行的函數名稱.
12940
				#$result["argu"],使用的參數.
12940
				#$result["argu"],使用的參數.
12941
				#$result["oriStr"],要處理的原始字串內容.
12941
				#$result["oriStr"],要處理的原始字串內容.
12942
				#$result["content"],處理好的的字串內容.	
12942
				#$result["content"],處理好的的字串內容.
12943
				#必填參數:
12943
				#必填參數:
12944
				#$conf["stringIn"],字串,要處理的字串.
12944
				#$conf["stringIn"],字串,要處理的字串.
12945
				$conf["stringProcess::delStrBeforeKeyWord"]["stringIn"]=$conf["targetServr"];
12945
				$conf["stringProcess::delStrBeforeKeyWord"]["stringIn"]=$conf["targetServr"];
12946
				#$conf["keyWord"],字串,特定字串.
12946
				#$conf["keyWord"],字串,特定字串.
12947
				$conf["stringProcess::delStrBeforeKeyWord"]["keyWord"]="/";
12947
				$conf["stringProcess::delStrBeforeKeyWord"]["keyWord"]="/";
Line 12954... Line 12954...
12954
				#無.
12954
				#無.
12955
				#備註:
12955
				#備註:
12956
				#無.
12956
				#無.
12957
				$delStrBeforeKeyWord=stringProcess::delStrBeforeKeyWord($conf["stringProcess::delStrBeforeKeyWord"]);
12957
				$delStrBeforeKeyWord=stringProcess::delStrBeforeKeyWord($conf["stringProcess::delStrBeforeKeyWord"]);
12958
				unset($conf["stringProcess::delStrBeforeKeyWord"]);
12958
				unset($conf["stringProcess::delStrBeforeKeyWord"]);
12959
				
12959
 
12960
				#如果檢查執行失敗
12960
				#如果檢查執行失敗
12961
				if($delStrBeforeKeyWord["status"]==="false"){
12961
				if($delStrBeforeKeyWord["status"]==="false"){
12962
					
12962
 
12963
					#設置執行失敗
12963
					#設置執行失敗
12964
					$result["status"]="false";
12964
					$result["status"]="false";
12965
				
12965
 
12966
					#設置執行錯誤訊息
12966
					#設置執行錯誤訊息
12967
					$result["error"]=$delStrBeforeKeyWord;
12967
					$result["error"]=$delStrBeforeKeyWord;
12968
					
12968
 
12969
					#回傳結果
12969
					#回傳結果
12970
					return $result;
12970
					return $result;
12971
					
12971
 
12972
					}#if end
12972
					}#if end
12973
					
12973
 
12974
				#如果該存在的 / 不存在
12974
				#如果該存在的 / 不存在
12975
				if($delStrBeforeKeyWord["founded"]==="false"){
12975
				if($delStrBeforeKeyWord["founded"]==="false"){
12976
					
12976
 
12977
					#設置執行失敗
12977
					#設置執行失敗
12978
					$result["status"]="false";
12978
					$result["status"]="false";
12979
				
12979
 
12980
					#設置執行錯誤訊息
12980
					#設置執行錯誤訊息
12981
					$result["error"]=$delStrBeforeKeyWord;
12981
					$result["error"]=$delStrBeforeKeyWord;
12982
					
12982
 
12983
					#回傳結果
12983
					#回傳結果
12984
					return $result;
12984
					return $result;
12985
					
12985
 
12986
					}#if end
12986
					}#if end
12987
					
12987
 
12988
				#設置 path
12988
				#設置 path
12989
				$path="/".$delStrBeforeKeyWord["content"];
12989
				$path="/".$delStrBeforeKeyWord["content"];
12990
				
12990
 
12991
				#如果有切出 path
12991
				#如果有切出 path
12992
				if($spiltString["dataCounts"]>=2){
12992
				if($spiltString["dataCounts"]>=2){
12993
				
12993
 
12994
					#另存peer host name
12994
					#另存peer host name
12995
					$peerHostName=$spiltString["dataArray"][0];
12995
					$peerHostName=$spiltString["dataArray"][0];
12996
				
12996
 
12997
					#更新 targetServr
12997
					#更新 targetServr
12998
					$conf["targetServr"]=$peerHostName;
12998
					$conf["targetServr"]=$peerHostName;
12999
				
12999
 
13000
					}#if end
13000
					}#if end
13001
				
13001
 
13002
				}#if end
13002
				}#if end
13003
		
13003
 
13004
			#初始化給 stream_context_create 用的參數
13004
			#初始化給 stream_context_create 用的參數
13005
			$paramsForStream_context_create=array();
13005
			$paramsForStream_context_create=array();
13006
			
13006
 
13007
			#設置為http相關的參數
13007
			#設置為http相關的參數
13008
			#$paramsForStream_context_create["http"]=array();
13008
			#$paramsForStream_context_create["http"]=array();
13009
			
13009
 
13010
			#method為HEAD
13010
			#method為HEAD
13011
			#$paramsForStream_context_create["http"]["method"]="HEAD ".$path;
13011
			#$paramsForStream_context_create["http"]["method"]="HEAD ".$path;
13012
			
13012
 
13013
			#HTTP/1.1
13013
			#HTTP/1.1
13014
			#$paramsForStream_context_create["http"]["protocol_version"]="1.1";
13014
			#$paramsForStream_context_create["http"]["protocol_version"]="1.1";
13015
			
13015
 
13016
			#初始化header
13016
			#初始化header
13017
			#$paramsForStream_context_create["http"]["header"]=array();
13017
			#$paramsForStream_context_create["http"]["header"]=array();
13018
			
13018
 
13019
			#設置header Host
13019
			#設置header Host
13020
			#$paramsForStream_context_create["http"]["header"][]="Host: ".$conf["targetServr"];
13020
			#$paramsForStream_context_create["http"]["header"][]="Host: ".$conf["targetServr"];
13021
			
13021
 
13022
			#設置header Upgrade
13022
			#設置header Upgrade
13023
			#$paramsForStream_context_create["http"]["header"][]="Upgrade: websocket";
13023
			#$paramsForStream_context_create["http"]["header"][]="Upgrade: websocket";
13024
			
13024
 
13025
			#設置header Connection
13025
			#設置header Connection
13026
			#$paramsForStream_context_create["http"]["header"][]="Connection: Upgrade";
13026
			#$paramsForStream_context_create["http"]["header"][]="Connection: Upgrade";
13027
			
13027
 
13028
			#設置header Sec-WebSocket-Version
13028
			#設置header Sec-WebSocket-Version
13029
			#$paramsForStream_context_create["http"]["header"][]="Sec-WebSocket-Version: 13";
13029
			#$paramsForStream_context_create["http"]["header"][]="Sec-WebSocket-Version: 13";
13030
			
13030
 
13031
			#設置header Sec-WebSocket-Key
13031
			#設置header Sec-WebSocket-Key
13032
			#$paramsForStream_context_create["http"]["header"][]="Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==";
13032
			#$paramsForStream_context_create["http"]["header"][]="Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==";
13033
			
13033
 
13034
			#body content
13034
			#body content
13035
			#$paramsForStream_context_create["http"]["content"]="";
13035
			#$paramsForStream_context_create["http"]["content"]="";
13036
			
13036
 
13037
			#設置為ssl相關的參數
13037
			#設置為ssl相關的參數
13038
			$paramsForStream_context_create["ssl"]=array();
13038
			$paramsForStream_context_create["ssl"]=array();
13039
			
13039
 
13040
			#被連線端的網域名稱
13040
			#被連線端的網域名稱
13041
			#$paramsForStream_context_create["ssl"]["peer_name"]=$conf["targetServr"];
13041
			#$paramsForStream_context_create["ssl"]["peer_name"]=$conf["targetServr"];
13042
			
13042
 
13043
			#verify_peer
13043
			#verify_peer
13044
			#$paramsForStream_context_create["ssl"]["verify_peer"]=true;
13044
			#$paramsForStream_context_create["ssl"]["verify_peer"]=true;
13045
			#$paramsForStream_context_create["ssl"]["verify_peer"]=false;
13045
			#$paramsForStream_context_create["ssl"]["verify_peer"]=false;
13046
		
13046
 
13047
			#verify_peer_name
13047
			#verify_peer_name
13048
			#$paramsForStream_context_create["ssl"]["verify_peer_name"]=true;
13048
			#$paramsForStream_context_create["ssl"]["verify_peer_name"]=true;
13049
			#$paramsForStream_context_create["ssl"]["verify_peer_name"]=false;
13049
			#$paramsForStream_context_create["ssl"]["verify_peer_name"]=false;
13050
		
13050
 
13051
			#allow_self_signed,允許沒有受到認可的憑證
13051
			#allow_self_signed,允許沒有受到認可的憑證
13052
			#$paramsForStream_context_create["ssl"]["allow_self_signed"]=false; 
13052
			#$paramsForStream_context_create["ssl"]["allow_self_signed"]=false;
13053
		
13053
 
13054
			#cafile
13054
			#cafile
13055
			$paramsForStream_context_create["ssl"]["cafile"]="/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem";
13055
			$paramsForStream_context_create["ssl"]["cafile"]="/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem";
13056
		
13056
 
13057
			#capath
13057
			#capath
13058
			#$paramsForStream_context_create["ssl"]["capath"]="/etc/pki/ca-trust/extracted/pem/directory-hash/";
13058
			#$paramsForStream_context_create["ssl"]["capath"]="/etc/pki/ca-trust/extracted/pem/directory-hash/";
13059
			
13059
 
13060
			#local_cert
13060
			#local_cert
13061
			#$paramsForStream_context_create["ssl"]["local_cert"]="/etc/pki/tls/certs/php-selfsigned-and-key.crt";
13061
			#$paramsForStream_context_create["ssl"]["local_cert"]="/etc/pki/tls/certs/php-selfsigned-and-key.crt";
13062
			
13062
 
13063
			#local_pk
13063
			#local_pk
13064
			#$paramsForStream_context_create["ssl"]["local_pk"]="/etc/pki/tls/private/php-selfsigned.key";
13064
			#$paramsForStream_context_create["ssl"]["local_pk"]="/etc/pki/tls/private/php-selfsigned.key";
13065
			
13065
 
13066
			#capture_peer_cert
13066
			#capture_peer_cert
13067
			$paramsForStream_context_create["ssl"]["capture_peer_cert"]=true;
13067
			$paramsForStream_context_create["ssl"]["capture_peer_cert"]=true;
13068
			
13068
 
13069
			#capture_peer_cert_chain
13069
			#capture_peer_cert_chain
13070
			$paramsForStream_context_create["ssl"]["capture_peer_cert_chain"]=true;
13070
			$paramsForStream_context_create["ssl"]["capture_peer_cert_chain"]=true;
13071
			
13071
 
13072
			#security_level
13072
			#security_level
13073
			#$paramsForStream_context_create["ssl"]["security_level"]=5;
13073
			#$paramsForStream_context_create["ssl"]["security_level"]=5;
13074
		
13074
 
13075
			#如果要 debug
13075
			#如果要 debug
13076
			if($conf["debug"]==="true"){
13076
			if($conf["debug"]==="true"){
13077
			
13077
 
13078
				#debug
13078
				#debug
13079
				var_dump(__LINE__,$paramsForStream_context_create);
13079
				var_dump(__LINE__,$paramsForStream_context_create);
13080
			
13080
 
13081
				}#if end
13081
				}#if end
13082
		
13082
 
13083
			#建立ssl設定
13083
			#建立ssl設定
13084
			$stream_context_create=stream_context_create($paramsForStream_context_create);
13084
			$stream_context_create=stream_context_create($paramsForStream_context_create);
13085
			
13085
 
13086
			#如果要 debug
13086
			#如果要 debug
13087
			if($conf["debug"]==="true"){
13087
			if($conf["debug"]==="true"){
13088
			
13088
 
13089
				#debug
13089
				#debug
13090
				var_dump(__LINE__,$stream_context_create);
13090
				var_dump(__LINE__,$stream_context_create);
13091
			
13091
 
13092
				}#if end
13092
				}#if end
13093
			
13093
 
13094
			#連線到 $conf["targetServr"].":".$conf["targetPort"] ,若有 error 可從 $error_code,$error_message 取得資訊, timeout 為 30 秒, connection flags 為 STREAM_CLIENT_CONNECT, 連線的屬性記載在 $stream_context_create.
13094
			#連線到 $conf["targetServr"].":".$conf["targetPort"] ,若有 error 可從 $error_code,$error_message 取得資訊, timeout 為 30 秒, connection flags 為 STREAM_CLIENT_CONNECT, 連線的屬性記載在 $stream_context_create.
13095
			$stream_socket_client=stream_socket_client("ssl://".$conf["targetServr"].":".$conf["targetPort"],$error_code,$error_message,0,STREAM_CLIENT_CONNECT,$stream_context_create);
13095
			$stream_socket_client=stream_socket_client("ssl://".$conf["targetServr"].":".$conf["targetPort"],$error_code,$error_message,0,STREAM_CLIENT_CONNECT,$stream_context_create);
13096
 
13096
 
13097
			#如果連線到 peer 失敗
13097
			#如果連線到 peer 失敗
13098
			if($stream_socket_client===false){
13098
			if($stream_socket_client===false){
13099
				
13099
 
13100
				#設置執行失敗
13100
				#設置執行失敗
13101
				$result["status"]="false";
13101
				$result["status"]="false";
13102
			
13102
 
13103
				#設置執行錯誤訊息
13103
				#設置執行錯誤訊息
13104
				$result["error"][]="stream_socket_client() failed.".PHP_EOL."Reason: ".$error_message."(".$error_code.")".PHP_EOL;
13104
				$result["error"][]="stream_socket_client() failed.".PHP_EOL."Reason: ".$error_message."(".$error_code.")".PHP_EOL;
13105
				
13105
 
13106
				#回傳結果
13106
				#回傳結果
13107
				return $result;
13107
				return $result;
13108
				
13108
 
13109
				}#if end
13109
				}#if end
13110
			
13110
 
13111
			#取得連線的資訊
13111
			#取得連線的資訊
13112
			$result["info"]=stream_get_meta_data($stream_socket_client);
13112
			$result["info"]=stream_get_meta_data($stream_socket_client);
13113
			
13113
 
13114
			#write header - start
13114
			#write header - start
13115
			
13115
 
13116
			#initial header
13116
			#initial header
13117
			$header="";
13117
			$header="";
13118
			
13118
 
13119
			#method、path、protocol
13119
			#method、path、protocol
13120
			$header=$header."HEAD ".$path." HTTP/1.1"."\r\n";
13120
			$header=$header."HEAD ".$path." HTTP/1.1"."\r\n";
13121
			
13121
 
13122
			#Host
13122
			#Host
13123
			$header=$header."Host: ".$conf["targetServr"]."\r\n";
13123
			$header=$header."Host: ".$conf["targetServr"]."\r\n";
13124
			
13124
 
13125
			#Upgrade
13125
			#Upgrade
13126
			$header=$header."Upgrade: websocket"."\r\n";
13126
			$header=$header."Upgrade: websocket"."\r\n";
13127
			
13127
 
13128
			#Connection
13128
			#Connection
13129
			$header=$header."Connection: Upgrade"."\r\n";
13129
			$header=$header."Connection: Upgrade"."\r\n";
13130
			
13130
 
13131
			#Sec-WebSocket-Version
13131
			#Sec-WebSocket-Version
13132
			$header=$header."Sec-WebSocket-Version: 13"."\r\n";
13132
			$header=$header."Sec-WebSocket-Version: 13"."\r\n";
13133
			
13133
 
13134
			#Sec-WebSocket-Key
13134
			#Sec-WebSocket-Key
13135
			$header=$header."Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ=="."\r\n";
13135
			$header=$header."Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ=="."\r\n";
13136
			
13136
 
13137
			#send header					
13137
			#send header
13138
			fwrite($stream_socket_client,$header."\r\n");
13138
			fwrite($stream_socket_client,$header."\r\n");
13139
					
13139
 
13140
			#write header - close
13140
			#write header - close
13141
			
13141
 
13142
			#GET server reponse
13142
			#GET server reponse
13143
			while(!feof($stream_socket_client)){
13143
			while(!feof($stream_socket_client)){
13144
			
13144
 
13145
				#讀取內容
13145
				#讀取內容
13146
				$fgets=fgets($stream_socket_client, 1024);
13146
				$fgets=fgets($stream_socket_client, 1024);
13147
				
13147
 
13148
				#如果要 debug
13148
				#如果要 debug
13149
				if($conf["debug"]==="true"){
13149
				if($conf["debug"]==="true"){
13150
				
13150
 
13151
					#debug
13151
					#debug
13152
					var_dump(__LINE__,$fgets);
13152
					var_dump(__LINE__,$fgets);
13153
				
13153
 
13154
					}#if end
13154
					}#if end
13155
				
13155
 
13156
				#如果 server 已經回應要用 websocket 協定了
13156
				#如果 server 已經回應要用 websocket 協定了
13157
				if(trim($fgets)==="Upgrade: websocket"){
13157
				if(trim($fgets)==="Upgrade: websocket"){
13158
				
13158
 
13159
					#跳出while
13159
					#跳出while
13160
					break;
13160
					break;
13161
				
13161
 
13162
					}#if end
13162
					}#if end
13163
				
13163
 
13164
				}#if end
13164
				}#if end
13165
			
13165
 
13166
			#將建立的 sock 放到 $master 陣列
13166
			#將建立的 sock 放到 $master 陣列
13167
			$master[] = $stream_socket_client;
13167
			$master[] = $stream_socket_client;
13168
			
13168
 
13169
			#函式說明:
13169
			#函式說明:
13170
			#取得目前瀏覽的頁面的路徑或執行的php程式路徑.
13170
			#取得目前瀏覽的頁面的路徑或執行的php程式路徑.
13171
			#回傳結果:
13171
			#回傳結果:
13172
			#$result["status"],執行是否正常,"true"代表執行成功,"false"代表執行失敗.
13172
			#$result["status"],執行是否正常,"true"代表執行成功,"false"代表執行失敗.
13173
			#$result["error"],錯誤訊息.
13173
			#$result["error"],錯誤訊息.
Line 13185... Line 13185...
13185
			#http://blog.longwin.com.tw/2009/01/php-get-directory-file-path-dirname-2008/
13185
			#http://blog.longwin.com.tw/2009/01/php-get-directory-file-path-dirname-2008/
13186
			#備註:
13186
			#備註:
13187
			#無.
13187
			#無.
13188
			$getPathOfThisPhpFile=csInformation::getPathOfThisPhpFile($conf["csInformation::getPathOfThisPhpFile"]);
13188
			$getPathOfThisPhpFile=csInformation::getPathOfThisPhpFile($conf["csInformation::getPathOfThisPhpFile"]);
13189
			unset($conf["csInformation::getPathOfThisPhpFile"]);
13189
			unset($conf["csInformation::getPathOfThisPhpFile"]);
13190
			
13190
 
13191
			#如果執行失敗
13191
			#如果執行失敗
13192
			if($getPathOfThisPhpFile["status"]=="false"){
13192
			if($getPathOfThisPhpFile["status"]=="false"){
13193
				
13193
 
13194
				#設置錯誤識別
13194
				#設置錯誤識別
13195
				$result["status"]="false";
13195
				$result["status"]="false";
13196
				
13196
 
13197
				#設置錯誤訊息
13197
				#設置錯誤訊息
13198
				$result["error"]=$getPathOfThisPhpFile;
13198
				$result["error"]=$getPathOfThisPhpFile;
13199
				
13199
 
13200
				#回傳結果
13200
				#回傳結果
13201
				return $result;
13201
				return $result;
13202
				
13202
 
13203
				}#if end
13203
				}#if end
13204
			
13204
 
13205
			#函式說明:
13205
			#函式說明:
13206
			#使用 linux 的 uuid 指令來產生 uuid 字串
13206
			#使用 linux 的 uuid 指令來產生 uuid 字串
13207
			#回傳結果:
13207
			#回傳結果:
13208
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
13208
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
13209
			#$result["error"],錯誤訊息.
13209
			#$result["error"],錯誤訊息.
Line 13216... Line 13216...
13216
			#參考資料:
13216
			#參考資料:
13217
			#無.
13217
			#無.
13218
			#備註:
13218
			#備註:
13219
			#無.
13219
			#無.
13220
			$uuid=cmd::uuid();
13220
			$uuid=cmd::uuid();
13221
			
13221
 
13222
			#如果執行失敗
13222
			#如果執行失敗
13223
			if($uuid["status"]=="false"){
13223
			if($uuid["status"]=="false"){
13224
				
13224
 
13225
				#設置錯誤識別
13225
				#設置錯誤識別
13226
				$result["status"]="false";
13226
				$result["status"]="false";
13227
				
13227
 
13228
				#設置錯誤訊息
13228
				#設置錯誤訊息
13229
				$result["error"]=$uuid;
13229
				$result["error"]=$uuid;
13230
				
13230
 
13231
				#回傳結果
13231
				#回傳結果
13232
				return $result;
13232
				return $result;
13233
				
13233
 
13234
				}#if end
13234
				}#if end
13235
			
13235
 
13236
			#設置存放socket的path
13236
			#設置存放socket的path
13237
			$socketPath=$getPathOfThisPhpFile["content"]."var/webSock::clientDaemon";
13237
			$socketPath=$getPathOfThisPhpFile["content"]."var/webSock::clientDaemon";
13238
			
13238
 
13239
			#讓存放unix domain socket的路徑存在
13239
			#讓存放unix domain socket的路徑存在
13240
			#函式說明:
13240
			#函式說明:
13241
			#確保路徑存在.
13241
			#確保路徑存在.
13242
			#回傳結果:
13242
			#回傳結果:
13243
			#$result["status"],執行正常與否,"true"代表正常,"false"代表不正常.
13243
			#$result["status"],執行正常與否,"true"代表正常,"false"代表不正常.
Line 13262... Line 13262...
13262
			#無.
13262
			#無.
13263
			#備註:
13263
			#備註:
13264
			#無.
13264
			#無.
13265
			$validatePath=fileAccess::validatePath($conf["fileAccess::validatePath"]);
13265
			$validatePath=fileAccess::validatePath($conf["fileAccess::validatePath"]);
13266
			unset($conf["fileAccess::validatePath"]);
13266
			unset($conf["fileAccess::validatePath"]);
13267
			
13267
 
13268
			#如果執行失敗
13268
			#如果執行失敗
13269
			if($validatePath["status"]==="false"){
13269
			if($validatePath["status"]==="false"){
13270
				
13270
 
13271
				#設置錯誤識別
13271
				#設置錯誤識別
13272
				$result["status"]="false";
13272
				$result["status"]="false";
13273
				
13273
 
13274
				#設置錯誤訊息
13274
				#設置錯誤訊息
13275
				$result["error"]=$validatePath;
13275
				$result["error"]=$validatePath;
13276
				
13276
 
13277
				#回傳結果
13277
				#回傳結果
13278
				return $result;
13278
				return $result;
13279
				
13279
 
13280
				}#if end
13280
				}#if end
13281
			
13281
 
13282
			#變更 working dir
13282
			#變更 working dir
13283
			$chdir=chdir($socketPath);
13283
			$chdir=chdir($socketPath);
13284
			
13284
 
13285
			#設置sock檔案名稱
13285
			#設置sock檔案名稱
13286
			$socketName=$uuid["content"].".sock";
13286
			$socketName=$uuid["content"].".sock";
13287
			
13287
 
13288
			#設置sock檔案位置與名稱
13288
			#設置sock檔案位置與名稱
13289
			$socketAddr=$socketPath."/".$socketName;
13289
			$socketAddr=$socketPath."/".$socketName;
13290
			
13290
 
13291
			#如果要 debug
13291
			#如果要 debug
13292
			if($conf["debug"]==="true"){
13292
			if($conf["debug"]==="true"){
13293
			
13293
 
13294
				#debug
13294
				#debug
13295
				var_dump(__LINE__,$socketAddr);
13295
				var_dump(__LINE__,$socketAddr);
13296
			
13296
 
13297
				}#if end
13297
				}#if end
13298
			
13298
 
13299
			#建立 unix domain socket
13299
			#建立 unix domain socket
13300
			$usock=stream_socket_server("unix://".$socketName, $errno, $errstr);
13300
			$usock=stream_socket_server("unix://".$socketName, $errno, $errstr);
13301
			
13301
 
13302
			#如果建立 unix domain socket 失敗
13302
			#如果建立 unix domain socket 失敗
13303
			if(!$usock){
13303
			if(!$usock){
13304
				
13304
 
13305
				#設置執行失敗
13305
				#設置執行失敗
13306
				$result["status"]="false";
13306
				$result["status"]="false";
13307
				
13307
 
13308
				#設置錯誤訊息
13308
				#設置錯誤訊息
13309
				$result["error"][]=$errstr." (".$errno.")";
13309
				$result["error"][]=$errstr." (".$errno.")";
13310
				
13310
 
13311
				#設置錯誤訊息
13311
				#設置錯誤訊息
13312
				$result["error"][]="建立 unix domain socket(".$socketAddr.") 失敗";
13312
				$result["error"][]="建立 unix domain socket(".$socketAddr.") 失敗";
13313
				
13313
 
13314
				#回傳結果
13314
				#回傳結果
13315
				return $result;
13315
				return $result;
13316
		
13316
 
13317
				}#if end
13317
				}#if end
13318
			
13318
 
13319
			#印出socket addr
13319
			#印出socket addr
13320
			echo "unixDomainSocket:".$socketAddr;
13320
			echo "unixDomainSocket:".$socketAddr;
13321
			
13321
 
13322
			#將建立的 unix domain socket 放到 $master 陣列
13322
			#將建立的 unix domain socket 放到 $master 陣列
13323
			$master[] = $usock;
13323
			$master[] = $usock;
13324
			
13324
 
13325
			#永久監聽, 不 timeout
13325
			#永久監聽, 不 timeout
13326
			while(true){
13326
			while(true){
13327
				
13327
 
13328
				#設置給 stream_select 監控的 sock 變數
13328
				#設置給 stream_select 監控的 sock 變數
13329
				$read = $write = $except = $master;
13329
				$read = $write = $except = $master;
13330
				
13330
 
13331
				#等待1秒,看有沒有連線進來讀寫資料
13331
				#等待1秒,看有沒有連線進來讀寫資料
13332
				$mod_fd = stream_select($read, $write, $except, 1);
13332
				$mod_fd = stream_select($read, $write, $except, 1);
13333
				
13333
 
13334
				#如果沒有偵測到讀寫動作失敗
13334
				#如果沒有偵測到讀寫動作失敗
13335
				if($mod_fd===FALSE){
13335
				if($mod_fd===FALSE){
13336
					
13336
 
13337
					#關閉 web socket 連線
13337
					#關閉 web socket 連線
13338
					@fclose($stream_socket_client);
13338
					@fclose($stream_socket_client);
13339
					
13339
 
13340
					#關閉unix domain socket
13340
					#關閉unix domain socket
13341
					@fclose($usock);
13341
					@fclose($usock);
13342
					
13342
 
13343
					#移除unix domain socket
13343
					#移除unix domain socket
13344
					@unlink($socketAddr);
13344
					@unlink($socketAddr);
13345
					
13345
 
13346
					#設置執行失敗
13346
					#設置執行失敗
13347
					$result["status"]="false";
13347
					$result["status"]="false";
13348
					
13348
 
13349
					#設置錯誤訊息
13349
					#設置錯誤訊息
13350
					$result["error"][]="監聽資源異動失敗";
13350
					$result["error"][]="監聽資源異動失敗";
13351
					
13351
 
13352
					#回傳結果
13352
					#回傳結果
13353
					return $result;
13353
					return $result;
13354
					
13354
 
13355
					}#if end
13355
					}#if end
13356
					
13356
 
13357
				#如果沒有訊息,代表閒置時可以做的事情
13357
				#如果沒有訊息,代表閒置時可以做的事情
13358
				else if($mod_fd===0){
13358
				else if($mod_fd===0){
13359
				
13359
 
13360
					#代表沒收到訊息
13360
					#代表沒收到訊息
13361
					echo ".";
13361
					echo ".";
13362
				
13362
 
13363
					}#if end
13363
					}#if end
13364
				
13364
 
13365
				#反之有收到來自 web socket server 或 unix socket 的訊息
13365
				#反之有收到來自 web socket server 或 unix socket 的訊息
13366
				else{
13366
				else{
13367
				
13367
 
13368
					#針對每個 sock 陣列
13368
					#針對每個 sock 陣列
13369
					foreach($read as $readStream){
13369
					foreach($read as $readStream){
13370
					
13370
 
13371
						#如果是來自 web socket server 的訊息
13371
						#如果是來自 web socket server 的訊息
13372
						if($readStream===$stream_socket_client){
13372
						if($readStream===$stream_socket_client){
13373
						
13373
 
13374
							#當等待別人連線時,若有人連線進來.
13374
							#當等待別人連線時,若有人連線進來.
13375
							#$webConn = stream_socket_accept($stream_socket_client);
13375
							#$webConn = stream_socket_accept($stream_socket_client);
13376
						
13376
 
13377
							#如果要 debug
13377
							#如果要 debug
13378
							if($conf["debug"]==="true"){
13378
							if($conf["debug"]==="true"){
13379
						
13379
 
13380
								#debug
13380
								#debug
13381
								var_dump(__LINE__,"有收到來自 web socket server 的訊息");
13381
								var_dump(__LINE__,"有收到來自 web socket server 的訊息");
13382
						
13382
 
13383
								}#if end
13383
								}#if end
13384
						
13384
 
13385
							#讀取內容
13385
							#讀取內容
13386
							$fread=fread($stream_socket_client,1024);
13386
							$fread=fread($stream_socket_client,1024);
13387
							
13387
 
13388
							#如果要 debug
13388
							#如果要 debug
13389
							if($conf["debug"]==="true"){
13389
							if($conf["debug"]==="true"){
13390
							
13390
 
13391
								#debug
13391
								#debug
13392
								var_dump(__LINE__,strlen($fread),$fread);
13392
								var_dump(__LINE__,strlen($fread),$fread);
13393
							
13393
 
13394
								}#if end
13394
								}#if end
13395
							
13395
 
13396
							#函式說明:
13396
							#函式說明:
13397
							#將 handshake 後含有 mask bit 的 web socket 訊息進行  mask/unmask.
13397
							#將 handshake 後含有 mask bit 的 web socket 訊息進行  mask/unmask.
13398
							#回傳結果:
13398
							#回傳結果:
13399
							#$result["function"],當前函式的名稱.
13399
							#$result["function"],當前函式的名稱.
13400
							#$result["argu"],使用的參數.
13400
							#$result["argu"],使用的參數.
Line 13402... Line 13402...
13402
							#$result["content"],unmask後的內容.
13402
							#$result["content"],unmask後的內容.
13403
							#$result["type"],訊息的類型,"unknow":代表未定義;"text":代表為文字訊息;"pong":代表為pong;"invalid":代表不為web socket frame.
13403
							#$result["type"],訊息的類型,"unknow":代表未定義;"text":代表為文字訊息;"pong":代表為pong;"invalid":代表不為web socket frame.
13404
							#$result["error"],錯誤訊息陣列.
13404
							#$result["error"],錯誤訊息陣列.
13405
							#必填參數:
13405
							#必填參數:
13406
							#$conf["payload"],字串,要解密的訊息.
13406
							#$conf["payload"],字串,要解密的訊息.
13407
							$conf["webSock::unmask"]["payload"]=$fread; 
13407
							$conf["webSock::unmask"]["payload"]=$fread;
13408
							#可省略參數:
13408
							#可省略參數:
13409
							#$conf["allowUnmask"],字串,若發現沒有設置mask bit,也要繼續執行,則為"true";反之為"false".
13409
							#$conf["allowUnmask"],字串,若發現沒有設置mask bit,也要繼續執行,則為"true";反之為"false".
13410
							$conf["webSock::unmask"]["allowUnmask"]="true";
13410
							$conf["webSock::unmask"]["allowUnmask"]="true";
13411
							#$conf["webSock::unmask"]["debug"]="true";
13411
							#$conf["webSock::unmask"]["debug"]="true";
13412
							#參考資料
13412
							#參考資料
Line 13415... Line 13415...
13415
							#https://tools.ietf.org/html/rfc5234
13415
							#https://tools.ietf.org/html/rfc5234
13416
							#備註:
13416
							#備註:
13417
							#無.
13417
							#無.
13418
							$unmask=webSock::unmask($conf["webSock::unmask"]);
13418
							$unmask=webSock::unmask($conf["webSock::unmask"]);
13419
							unset($conf["webSock::unmask"]);
13419
							unset($conf["webSock::unmask"]);
13420
							
13420
 
13421
							#debug
13421
							#debug
13422
							#var_dump(__LINE__,$unmask);
13422
							#var_dump(__LINE__,$unmask);
13423
							
13423
 
13424
							#如果出錯
13424
							#如果出錯
13425
							if($unmask["status"]==="false"){
13425
							if($unmask["status"]==="false"){
13426
							
13426
 
13427
								#關閉 web socket 連線
13427
								#關閉 web socket 連線
13428
								@fclose($stream_socket_client);
13428
								@fclose($stream_socket_client);
13429
								
13429
 
13430
								#關閉unix domain socket
13430
								#關閉unix domain socket
13431
								@fclose($usock);
13431
								@fclose($usock);
13432
								
13432
 
13433
								#移除unix domain socket
13433
								#移除unix domain socket
13434
								@unlink($socketAddr);
13434
								@unlink($socketAddr);
13435
							
13435
 
13436
								#設置執行失敗
13436
								#設置執行失敗
13437
								$result["status"]="false";
13437
								$result["status"]="false";
13438
							
13438
 
13439
								#設置執行錯誤訊息
13439
								#設置執行錯誤訊息
13440
								$result["error"]=$unmask;
13440
								$result["error"]=$unmask;
13441
								
13441
 
13442
								#回傳結果
13442
								#回傳結果
13443
								return $result;
13443
								return $result;
13444
							
13444
 
13445
								}#if end
13445
								}#if end
13446
							
13446
 
13447
							#如果收到文字訊息
13447
							#如果收到文字訊息
13448
							if($unmask["type"]==="text"){
13448
							if($unmask["type"]==="text"){
13449
							
13449
 
13450
								#如果有解析出內容
13450
								#如果有解析出內容
13451
								if(isset($unmask["content"])){
13451
								if(isset($unmask["content"])){
13452
								
13452
 
13453
									#提示收到的訊息
13453
									#提示收到的訊息
13454
									echo $unmask["content"];
13454
									echo $unmask["content"];
13455
									
13455
 
13456
									#傳遞給unix domain client
13456
									#傳遞給unix domain client
13457
									$fwrite=fwrite($unixConn,$unmask["content"]);
13457
									$fwrite=fwrite($unixConn,$unmask["content"]);
13458
								
13458
 
13459
									#如果要 debug
13459
									#如果要 debug
13460
									if($conf["debug"]==="true"){
13460
									if($conf["debug"]==="true"){
13461
									
13461
 
13462
										#debug
13462
										#debug
13463
										var_dump(__LINE__,$fwrite);
13463
										var_dump(__LINE__,$fwrite);
13464
									
13464
 
13465
										}#if end
13465
										}#if end
13466
									
13466
 
13467
									#關閉連線
13467
									#關閉連線
13468
									fclose($unixConn);
13468
									fclose($unixConn);
13469
										
13469
 
13470
									}#if end
13470
									}#if end
13471
							
13471
 
13472
								}#if end
13472
								}#if end
13473
								
13473
 
13474
							#如果收到ping
13474
							#如果收到ping
13475
							if($unmask["type"]==="ping"){
13475
							if($unmask["type"]==="ping"){
13476
							
13476
 
13477
								#函式說明:
13477
								#函式說明:
13478
								#加密 handshake 後要傳送的 webSocket 訊息 
13478
								#加密 handshake 後要傳送的 webSocket 訊息
13479
								#回傳結果:
13479
								#回傳結果:
13480
								#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
13480
								#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
13481
								#$result["error"],執行不正常結束的錯訊息陣列.
13481
								#$result["error"],執行不正常結束的錯訊息陣列.
13482
								#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
13482
								#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
13483
								#$result["argu"],陣列,使用的參數.
13483
								#$result["argu"],陣列,使用的參數.
13484
								#必填參數:
13484
								#必填參數:
13485
								#$conf["text"],字串,要加密的訊息.
13485
								#$conf["text"],字串,要加密的訊息.
13486
								$conf["webSock::encode"]["text"]=""; 
13486
								$conf["webSock::encode"]["text"]="";
13487
								#可省略參數:
13487
								#可省略參數:
13488
								#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
13488
								#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
13489
								#$conf["ping"]="true";
13489
								#$conf["ping"]="true";
13490
								#$conf["pong"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
13490
								#$conf["pong"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
13491
								$conf["webSock::encode"]["pong"]="true";
13491
								$conf["webSock::encode"]["pong"]="true";
Line 13497... Line 13497...
13497
								#https://www.rfc-editor.org/rfc/rfc6455#page-28, Web Socket Base Framing Protocol.
13497
								#https://www.rfc-editor.org/rfc/rfc6455#page-28, Web Socket Base Framing Protocol.
13498
								#備註:
13498
								#備註:
13499
								#無.
13499
								#無.
13500
								$encode=webSock::encode($conf["webSock::encode"]);
13500
								$encode=webSock::encode($conf["webSock::encode"]);
13501
								unset($conf["webSock::encode"]);
13501
								unset($conf["webSock::encode"]);
13502
							
13502
 
13503
								#如果要 debug
13503
								#如果要 debug
13504
								if($conf["debug"]==="true"){
13504
								if($conf["debug"]==="true"){
13505
							
13505
 
13506
									#debug
13506
									#debug
13507
									var_dump(__LINE__,$encode);
13507
									var_dump(__LINE__,$encode);
13508
							
13508
 
13509
									}#if end
13509
									}#if end
13510
							
13510
 
13511
								#如果出錯
13511
								#如果出錯
13512
								if($encode["status"]==="false"){
13512
								if($encode["status"]==="false"){
13513
								
13513
 
13514
									#設置執行失敗
13514
									#設置執行失敗
13515
									$result["status"]="false";
13515
									$result["status"]="false";
13516
								
13516
 
13517
									#設置執行錯誤訊息
13517
									#設置執行錯誤訊息
13518
									$result["error"]=$encode;
13518
									$result["error"]=$encode;
13519
									
13519
 
13520
									#回傳結果
13520
									#回傳結果
13521
									return $result;
13521
									return $result;
13522
								
13522
 
13523
									}#if end
13523
									}#if end
13524
								
13524
 
13525
								#如果要 debug
13525
								#如果要 debug
13526
								if($conf["debug"]==="true"){
13526
								if($conf["debug"]==="true"){
13527
								
13527
 
13528
									#debug
13528
									#debug
13529
									var_dump(__LINE__,$encode);
13529
									var_dump(__LINE__,$encode);
13530
								
13530
 
13531
									}#if end
13531
									}#if end
13532
									
13532
 
13533
								#針對每個 frame
13533
								#針對每個 frame
13534
								foreach($encode["content"] as $fNo => $frame){
13534
								foreach($encode["content"] as $fNo => $frame){
13535
								
13535
 
13536
									#傳遞給 web socket server
13536
									#傳遞給 web socket server
13537
									$fwrite=fwrite($stream_socket_client,$frame);
13537
									$fwrite=fwrite($stream_socket_client,$frame);
13538
								
13538
 
13539
									#如果傳遞失敗
13539
									#如果傳遞失敗
13540
									if($fwrite===false){
13540
									if($fwrite===false){
13541
									
13541
 
13542
										#關閉 web socket 連線
13542
										#關閉 web socket 連線
13543
										@fclose($stream_socket_client);
13543
										@fclose($stream_socket_client);
13544
										
13544
 
13545
										#關閉unix domain socket
13545
										#關閉unix domain socket
13546
										@fclose($usock);
13546
										@fclose($usock);
13547
										
13547
 
13548
										#移除unix domain socket
13548
										#移除unix domain socket
13549
										@unlink($socketAddr);
13549
										@unlink($socketAddr);
13550
									
13550
 
13551
										#設置執行失敗
13551
										#設置執行失敗
13552
										$result["status"]="false";
13552
										$result["status"]="false";
13553
									
13553
 
13554
										#設置執行錯誤訊息
13554
										#設置執行錯誤訊息
13555
										$result["error"][]="傳遞第 ".($fNo+1)." 個 frame 時出錯(總共有".count($encode["content"])."個frame)";
13555
										$result["error"][]="傳遞第 ".($fNo+1)." 個 frame 時出錯(總共有".count($encode["content"])."個frame)";
13556
										
13556
 
13557
										#回傳結果
13557
										#回傳結果
13558
										return $result;
13558
										return $result;
13559
									
13559
 
13560
										}#if end
13560
										}#if end
13561
								
13561
 
13562
									}#foreach end
13562
									}#foreach end
13563
							
13563
 
13564
								}#if end
13564
								}#if end
13565
						
13565
 
13566
							}#if end
13566
							}#if end
13567
							
13567
 
13568
						#反之若為來自 unix domain socket 的訊息
13568
						#反之若為來自 unix domain socket 的訊息
13569
						else if($readStream===$usock){
13569
						else if($readStream===$usock){
13570
						
13570
 
13571
							#當等待別人連線時,若有人連線進來.
13571
							#當等待別人連線時,若有人連線進來.
13572
							$unixConn = stream_socket_accept($usock);
13572
							$unixConn = stream_socket_accept($usock);
13573
						
13573
 
13574
							#如果要 debug
13574
							#如果要 debug
13575
							if($conf["debug"]==="true"){
13575
							if($conf["debug"]==="true"){
13576
						
13576
 
13577
								#debug
13577
								#debug
13578
								var_dump(__LINE__,"有收到來自 unix domain socket 的訊息");
13578
								var_dump(__LINE__,"有收到來自 unix domain socket 的訊息");
13579
						
13579
 
13580
								}#if end
13580
								}#if end
13581
						
13581
 
13582
							#讀取內容
13582
							#讀取內容
13583
							$fgets=trim(fgets($unixConn,1024));
13583
							$fgets=trim(fgets($unixConn,1024));
13584
							
13584
 
13585
							#如果要 debug
13585
							#如果要 debug
13586
							if($conf["debug"]==="true"){
13586
							if($conf["debug"]==="true"){
13587
						
13587
 
13588
								#debug
13588
								#debug
13589
								var_dump(__LINE__,strlen($fgets),$fgets);
13589
								var_dump(__LINE__,strlen($fgets),$fgets);
13590
						
13590
 
13591
								}#if end
13591
								}#if end
13592
							
13592
 
13593
							#轉換成 web socket 的格式
13593
							#轉換成 web socket 的格式
13594
							#函式說明:
13594
							#函式說明:
13595
							#加密 handshake 後要傳送的 webSocket 訊息 
13595
							#加密 handshake 後要傳送的 webSocket 訊息
13596
							#回傳結果:
13596
							#回傳結果:
13597
							#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
13597
							#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
13598
							#$result["error"],執行不正常結束的錯訊息陣列.
13598
							#$result["error"],執行不正常結束的錯訊息陣列.
13599
							#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
13599
							#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
13600
							#$result["argu"],陣列,使用的參數.
13600
							#$result["argu"],陣列,使用的參數.
13601
							#必填參數:
13601
							#必填參數:
13602
							#$conf["text"],字串,要加密的訊息.
13602
							#$conf["text"],字串,要加密的訊息.
13603
							$conf["webSock::encode"]["text"]=$fgets; 
13603
							$conf["webSock::encode"]["text"]=$fgets;
13604
							#可省略參數:
13604
							#可省略參數:
13605
							#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
13605
							#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
13606
							#$conf["ping"]="true";
13606
							#$conf["ping"]="true";
13607
							#$conf["frames"],陣列,目前既有的訊息frame,預設為空陣列,代表沒有.
13607
							#$conf["frames"],陣列,目前既有的訊息frame,預設為空陣列,代表沒有.
13608
							#$conf["frames"]=array();
13608
							#$conf["frames"]=array();
Line 13612... Line 13612...
13612
							#https://www.rfc-editor.org/rfc/rfc6455#page-28, Web Socket Base Framing Protocol.
13612
							#https://www.rfc-editor.org/rfc/rfc6455#page-28, Web Socket Base Framing Protocol.
13613
							#備註:
13613
							#備註:
13614
							#無.
13614
							#無.
13615
							$encode=webSock::encode($conf["webSock::encode"]);
13615
							$encode=webSock::encode($conf["webSock::encode"]);
13616
							unset($conf["webSock::encode"]);
13616
							unset($conf["webSock::encode"]);
13617
						
13617
 
13618
							#如果要 debug
13618
							#如果要 debug
13619
							if($conf["debug"]==="true"){
13619
							if($conf["debug"]==="true"){
13620
						
13620
 
13621
								#debug
13621
								#debug
13622
								var_dump(__LINE__,$encode);
13622
								var_dump(__LINE__,$encode);
13623
						
13623
 
13624
								}#if end
13624
								}#if end
13625
						
13625
 
13626
							#如果出錯
13626
							#如果出錯
13627
							if($encode["status"]==="false"){
13627
							if($encode["status"]==="false"){
13628
							
13628
 
13629
								#設置執行失敗
13629
								#設置執行失敗
13630
								$result["status"]="false";
13630
								$result["status"]="false";
13631
							
13631
 
13632
								#設置執行錯誤訊息
13632
								#設置執行錯誤訊息
13633
								$result["error"]=$encode;
13633
								$result["error"]=$encode;
13634
								
13634
 
13635
								#回傳結果
13635
								#回傳結果
13636
								return $result;
13636
								return $result;
13637
							
13637
 
13638
								}#if end
13638
								}#if end
13639
								
13639
 
13640
							#針對每個frame
13640
							#針對每個frame
13641
							foreach($encode["content"] as $fNo => $frame){
13641
							foreach($encode["content"] as $fNo => $frame){
13642
							
13642
 
13643
								#傳遞給 web socket server
13643
								#傳遞給 web socket server
13644
								$fwrite=fwrite($stream_socket_client,$frame);
13644
								$fwrite=fwrite($stream_socket_client,$frame);
13645
							
13645
 
13646
								#如果要 debug
13646
								#如果要 debug
13647
								if($conf["debug"]==="true"){
13647
								if($conf["debug"]==="true"){
13648
							
13648
 
13649
									#debug
13649
									#debug
13650
									var_dump(__LINE__,$fwrite,"傳遞".strlen($frame)."bytes給webSocketServer",$frame);
13650
									var_dump(__LINE__,$fwrite,"傳遞".strlen($frame)."bytes給webSocketServer",$frame);
13651
							
13651
 
13652
									}#if end
13652
									}#if end
13653
							
13653
 
13654
								#如果傳遞失敗
13654
								#如果傳遞失敗
13655
								if($fwrite===false){
13655
								if($fwrite===false){
13656
								
13656
 
13657
									#關閉 web socket 連線
13657
									#關閉 web socket 連線
13658
									@fclose($stream_socket_client);
13658
									@fclose($stream_socket_client);
13659
									
13659
 
13660
									#關閉unix domain socket
13660
									#關閉unix domain socket
13661
									@fclose($usock);
13661
									@fclose($usock);
13662
									
13662
 
13663
									#移除unix domain socket
13663
									#移除unix domain socket
13664
									@unlink($socketAddr);
13664
									@unlink($socketAddr);
13665
								
13665
 
13666
									#設置執行失敗
13666
									#設置執行失敗
13667
									$result["status"]="false";
13667
									$result["status"]="false";
13668
								
13668
 
13669
									#設置執行錯誤訊息
13669
									#設置執行錯誤訊息
13670
									$result["error"][]="傳遞第 ".($fNo+1)." 個 frame 時出錯(總共有".count($encode["content"])."個frame)";
13670
									$result["error"][]="傳遞第 ".($fNo+1)." 個 frame 時出錯(總共有".count($encode["content"])."個frame)";
13671
									
13671
 
13672
									#回傳結果
13672
									#回傳結果
13673
									return $result;
13673
									return $result;
13674
								
13674
 
13675
									}#if end
13675
									}#if end
13676
							
13676
 
13677
								}#foreach end
13677
								}#foreach end
13678
						
13678
 
13679
							}#if end
13679
							}#if end
13680
							
13680
 
13681
						#例外狀況
13681
						#例外狀況
13682
						else{
13682
						else{
13683
						
13683
 
13684
							#如果要 debug
13684
							#如果要 debug
13685
							if($conf["debug"]==="true"){
13685
							if($conf["debug"]==="true"){
13686
						
13686
 
13687
								#debug
13687
								#debug
13688
								var_dump(__LINE__,$readStream);
13688
								var_dump(__LINE__,$readStream);
13689
						
13689
 
13690
								}#if end
13690
								}#if end
13691
						
13691
 
13692
							#關閉 web socket 連線
13692
							#關閉 web socket 連線
13693
							@fclose($stream_socket_client);
13693
							@fclose($stream_socket_client);
13694
							
13694
 
13695
							#關閉unix domain socket
13695
							#關閉unix domain socket
13696
							@fclose($usock);
13696
							@fclose($usock);
13697
							
13697
 
13698
							#移除unix domain socket
13698
							#移除unix domain socket
13699
							@unlink($socketAddr);
13699
							@unlink($socketAddr);
13700
						
13700
 
13701
							#顯示 socket 的狀態
13701
							#顯示 socket 的狀態
13702
							echo "非預期的錯誤,偵測到不應存在資源有異動".PHP_EOL;
13702
							echo "非預期的錯誤,偵測到不應存在資源有異動".PHP_EOL;
13703
							
13703
 
13704
							#結束執行並回傳1,代表異常結束
13704
							#結束執行並回傳1,代表異常結束
13705
							exit(1);
13705
							exit(1);
13706
						
13706
 
13707
							}#else end
13707
							}#else end
13708
					
13708
 
13709
						}#foreach end
13709
						}#foreach end
13710
				
13710
 
13711
					}#else
13711
					}#else
13712
				
13712
 
13713
				#等待1秒
13713
				#等待1秒
13714
				sleep(1);
13714
				sleep(1);
13715
				
13715
 
13716
				}#while end
13716
				}#while end
13717
			
13717
 
13718
			#設置執行正常
13718
			#設置執行正常
13719
			$result["status"]="true";
13719
			$result["status"]="true";
13720
			
13720
 
13721
			#回傳結果
13721
			#回傳結果
13722
			return $result;
13722
			return $result;
13723
		
13723
 
13724
			}#if end
13724
			}#if end
13725
		
13725
 
13726
		#反之不用
13726
		#反之不用
13727
		else{
13727
		else{
13728
		
13728
 
13729
			#如果要 debug
13729
			#如果要 debug
13730
			if($conf["debug"]==="true"){
13730
			if($conf["debug"]==="true"){
13731
		
13731
 
13732
				#提示用戶端的文字
13732
				#提示用戶端的文字
13733
				echo "TCP/IP Connection".PHP_EOL;
13733
				echo "TCP/IP Connection".PHP_EOL;
13734
 
13734
 
13735
				}#if end
13735
				}#if end
13736
 
13736
 
13737
			/* Get the port for the WWW service. */
13737
			/* Get the port for the WWW service. */
13738
			#http://php.net/manual/en/function.getservbyname.php
13738
			#http://php.net/manual/en/function.getservbyname.php
13739
			#$service_port = getservbyname('www', 'tcp');
13739
			#$service_port = getservbyname('www', 'tcp');
13740
			
13740
 
13741
			/* Get the IP address for the target host. */
13741
			/* Get the IP address for the target host. */
13742
			#http://php.net/manual/en/function.gethostbyname.php
13742
			#http://php.net/manual/en/function.gethostbyname.php
13743
			$ServerAddress = gethostbyname($conf["targetServr"]);
13743
			$ServerAddress = gethostbyname($conf["targetServr"]);
13744
 
13744
 
13745
			/* Create a TCP/IP socket. */
13745
			/* Create a TCP/IP socket. */
13746
			$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
13746
			$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
13747
			
13747
 
13748
			#如果建立 socket 失敗
13748
			#如果建立 socket 失敗
13749
			if ($socket === false) {
13749
			if ($socket === false) {
13750
				
13750
 
13751
				#設置執行失敗
13751
				#設置執行失敗
13752
				$result["status"]="false";
13752
				$result["status"]="false";
13753
			
13753
 
13754
				#設置執行錯誤訊息
13754
				#設置執行錯誤訊息
13755
				$result["error"][]="socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
13755
				$result["error"][]="socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
13756
				
13756
 
13757
				#回傳結果
13757
				#回傳結果
13758
				return $result;
13758
				return $result;
13759
				
13759
 
13760
				}#if end
13760
				}#if end
13761
				
13761
 
13762
			#反之
13762
			#反之
13763
			else {
13763
			else {
13764
				
13764
 
13765
				#如果要 debug
13765
				#如果要 debug
13766
				if($conf["debug"]==="true"){
13766
				if($conf["debug"]==="true"){
13767
				
13767
 
13768
					#印出建立成功訊息
13768
					#印出建立成功訊息
13769
					echo "OK.".PHP_EOL;
13769
					echo "OK.".PHP_EOL;
13770
				
13770
 
13771
					}#if end
13771
					}#if end
13772
				
13772
 
13773
				}#else end
13773
				}#else end
13774
			
13774
 
13775
			#如果要 debug
13775
			#如果要 debug
13776
			if($conf["debug"]==="true"){
13776
			if($conf["debug"]==="true"){
13777
			
13777
 
13778
				#提示要連線到哪個位址的哪個port
13778
				#提示要連線到哪個位址的哪個port
13779
				echo "Attempting to connect to ".$ServerAddress." on port ".$conf["targetPort"]."...";
13779
				echo "Attempting to connect to ".$ServerAddress." on port ".$conf["targetPort"]."...";
13780
		
13780
 
13781
				}#if end
13781
				}#if end
13782
		
13782
 
13783
			#連線到目標 socket
13783
			#連線到目標 socket
13784
			$socket_connect=socket_connect($socket, $ServerAddress, $conf["targetPort"]);
13784
			$socket_connect=socket_connect($socket, $ServerAddress, $conf["targetPort"]);
13785
			
13785
 
13786
			#如果連線到 socket 失敗
13786
			#如果連線到 socket 失敗
13787
			if($socket_connect===false){
13787
			if($socket_connect===false){
13788
				
13788
 
13789
				#設置執行失敗
13789
				#設置執行失敗
13790
				$result["status"]="false";
13790
				$result["status"]="false";
13791
			
13791
 
13792
				#設置執行錯誤訊息
13792
				#設置執行錯誤訊息
13793
				$result["error"][]="socket_connect() failed.".PHP_EOL."Reason: ($socket_connect) " . socket_strerror(socket_last_error($socket)) . PHP_EOL;
13793
				$result["error"][]="socket_connect() failed.".PHP_EOL."Reason: ($socket_connect) " . socket_strerror(socket_last_error($socket)) . PHP_EOL;
13794
				
13794
 
13795
				#回傳結果
13795
				#回傳結果
13796
				return $result;
13796
				return $result;
13797
				
13797
 
13798
				}#if end
13798
				}#if end
13799
				
13799
 
13800
			#反之
13800
			#反之
13801
			else {
13801
			else {
13802
				
13802
 
13803
				#如果要 debug
13803
				#如果要 debug
13804
				if($conf["debug"]==="true"){
13804
				if($conf["debug"]==="true"){
13805
				
13805
 
13806
					#印出連線成功訊息
13806
					#印出連線成功訊息
13807
					echo "OK.".PHP_EOL;
13807
					echo "OK.".PHP_EOL;
13808
					
13808
 
13809
					}#if end
13809
					}#if end
13810
				
13810
 
13811
				}#else end
13811
				}#else end
13812
		
13812
 
13813
			}#else end
13813
			}#else end
13814
		
13814
 
13815
		#初始化要傳送的HEAD資訊
13815
		#初始化要傳送的HEAD資訊
13816
		$in="HEAD / HTTP/1.1"."\r\n";
13816
		$in="HEAD / HTTP/1.1"."\r\n";
13817
		$in=$in."Host: ".$ServerAddress.":".$conf["targetPort"]."\r\n";
13817
		$in=$in."Host: ".$ServerAddress.":".$conf["targetPort"]."\r\n";
13818
		$in=$in."Upgrade: websocket"."\r\n";
13818
		$in=$in."Upgrade: websocket"."\r\n";
13819
		$in=$in."Connection: Upgrade"."\r\n";
13819
		$in=$in."Connection: Upgrade"."\r\n";
13820
		$in=$in."Sec-WebSocket-Version: 13"."\r\n";
13820
		$in=$in."Sec-WebSocket-Version: 13"."\r\n";
13821
		$in=$in."Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ=="."\r\n";
13821
		$in=$in."Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ=="."\r\n";
13822
		$in=$in."\r\n";
13822
		$in=$in."\r\n";
13823
				
13823
 
13824
		#初始化回傳的內容
13824
		#初始化回傳的內容
13825
		$out = '';
13825
		$out = '';
13826
 
13826
 
13827
		#如果連線到 socket 失敗
13827
		#如果連線到 socket 失敗
13828
		if($socket_connect===false){
13828
		if($socket_connect===false){
13829
 
13829
 
13830
			#提示傳送HTTP HEAD中
13830
			#提示傳送HTTP HEAD中
13831
			echo "Sending HTTP HEAD request...";
13831
			echo "Sending HTTP HEAD request...";
13832
		
13832
 
13833
			}#if end
13833
			}#if end
13834
		
13834
 
13835
		#將訊息傳送到socket去
13835
		#將訊息傳送到socket去
13836
		socket_write($socket, $in, strlen($in));
13836
		socket_write($socket, $in, strlen($in));
13837
		
13837
 
13838
		#如果連線到 socket 失敗
13838
		#如果連線到 socket 失敗
13839
		if($socket_connect===false){
13839
		if($socket_connect===false){
13840
		
13840
 
13841
			#提示傳送完畢
13841
			#提示傳送完畢
13842
			echo "OK.".PHP_EOL;
13842
			echo "OK.".PHP_EOL;
13843
 
13843
 
13844
			#提示讀取回應中
13844
			#提示讀取回應中
13845
			echo "Reading response:".PHP_EOL;
13845
			echo "Reading response:".PHP_EOL;
13846
			
13846
 
13847
			}#if end
13847
			}#if end
13848
			
13848
 
13849
		#無窮迴圈
13849
		#無窮迴圈
13850
		while(true){
13850
		while(true){
13851
			
13851
 
13852
			#一次讀取2048bytes,判斷有無回應,並取得其內容.
13852
			#一次讀取2048bytes,判斷有無回應,並取得其內容.
13853
			$out = socket_read($socket, 2048);
13853
			$out = socket_read($socket, 2048);
13854
			
13854
 
13855
			#如果尚未取得 Sec-WebSocket-Accept
13855
			#如果尚未取得 Sec-WebSocket-Accept
13856
			if(!isset($result["Sec-WebSocket-Accept"])){
13856
			if(!isset($result["Sec-WebSocket-Accept"])){
13857
			
13857
 
13858
				#如果連線到 socket 失敗
13858
				#如果連線到 socket 失敗
13859
				if($socket_connect===false){
13859
				if($socket_connect===false){
13860
			
13860
 
13861
					#印出收到的內容
13861
					#印出收到的內容
13862
					echo $out;
13862
					echo $out;
13863
					
13863
 
13864
					}#if end
13864
					}#if end
13865
				
13865
 
13866
				#如果 $out 有資料
13866
				#如果 $out 有資料
13867
				if($out){
13867
				if($out){
13868
					
13868
 
13869
					#如果接收到的是字串,且不為空
13869
					#如果接收到的是字串,且不為空
13870
					if(gettype($out)==="string" && !empty($out)){
13870
					if(gettype($out)==="string" && !empty($out)){
13871
					
13871
 
13872
						#如果有既有的前次回應內容
13872
						#如果有既有的前次回應內容
13873
						if(isset($result["content"])){
13873
						if(isset($result["content"])){
13874
							
13874
 
13875
							#儲存回應的內容
13875
							#儲存回應的內容
13876
							$result["content"]=$result["content"].$out;
13876
							$result["content"]=$result["content"].$out;
13877
							
13877
 
13878
							}#if end
13878
							}#if end
13879
					
13879
 
13880
						#反之
13880
						#反之
13881
						else{
13881
						else{
13882
							
13882
 
13883
							#儲存回應的內容
13883
							#儲存回應的內容
13884
							$result["content"]=$out;
13884
							$result["content"]=$out;
13885
							
13885
 
13886
							}#else end
13886
							}#else end
13887
						
13887
 
13888
						#涵式說明:
13888
						#涵式說明:
13889
						#將固定格式的字串分開,並回傳分開的結果。
13889
						#將固定格式的字串分開,並回傳分開的結果。
13890
						#回傳結果:
13890
						#回傳結果:
13891
						#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
13891
						#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
13892
						#$result["error"],錯誤訊息陣列
13892
						#$result["error"],錯誤訊息陣列
Line 13904... Line 13904...
13904
						#可省略參數:
13904
						#可省略參數:
13905
						#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
13905
						#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
13906
						$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
13906
						$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
13907
						$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
13907
						$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
13908
						unset($conf["stringProcess::spiltString"]);
13908
						unset($conf["stringProcess::spiltString"]);
13909
						
13909
 
13910
						#如果執行失敗
13910
						#如果執行失敗
13911
						if($spiltString["status"]==="false"){
13911
						if($spiltString["status"]==="false"){
13912
							
13912
 
13913
							#設置執行失敗
13913
							#設置執行失敗
13914
							$result["status"]="false";
13914
							$result["status"]="false";
13915
						
13915
 
13916
							#設置執行錯誤訊息
13916
							#設置執行錯誤訊息
13917
							$result["error"]=$spiltString;
13917
							$result["error"]=$spiltString;
13918
							
13918
 
13919
							#回傳結果
13919
							#回傳結果
13920
							return $result;
13920
							return $result;
13921
							
13921
 
13922
							}#if end
13922
							}#if end
13923
						
13923
 
13924
						#取得Sec-WebSocket-Accept
13924
						#取得Sec-WebSocket-Accept
13925
						$result["Sec-WebSocket-Accept"]=trim($spiltString["dataArray"][1]);
13925
						$result["Sec-WebSocket-Accept"]=trim($spiltString["dataArray"][1]);
13926
						
13926
 
13927
						#初始化要傳遞的內容			
13927
						#初始化要傳遞的內容
13928
						$in="id?";
13928
						$in="id?";
13929
						
13929
 
13930
						#初始化傳遞的內容長度
13930
						#初始化傳遞的內容長度
13931
						$len=strlen($in);
13931
						$len=strlen($in);
13932
						
13932
 
13933
						#設置儲存每個元素為8bit字串的陣列
13933
						#設置儲存每個元素為8bit字串的陣列
13934
						$bit8=array();
13934
						$bit8=array();
13935
						$bit8[0]="10000001";
13935
						$bit8[0]="10000001";
13936
						$bit8[1]="1".sprintf("%07s",base_convert($len,10,2));
13936
						$bit8[1]="1".sprintf("%07s",base_convert($len,10,2));
13937
						
13937
 
13938
						#mask key
13938
						#mask key
13939
						$bit8[2]="00000000";
13939
						$bit8[2]="00000000";
13940
						$bit8[3]="00000000";
13940
						$bit8[3]="00000000";
13941
						$bit8[4]="00000000";
13941
						$bit8[4]="00000000";
13942
						$bit8[5]="00000000";
13942
						$bit8[5]="00000000";
13943
						
13943
 
13944
						#mask key
13944
						#mask key
13945
						$mask_key=$bit8[2].$bit8[3].$bit8[4].$bit8[5];
13945
						$mask_key=$bit8[2].$bit8[3].$bit8[4].$bit8[5];
13946
						
13946
 
13947
						#針對每個字,進行mask
13947
						#針對每個字,進行mask
13948
						for($i=0;$i<$len;$i++){
13948
						for($i=0;$i<$len;$i++){
13949
						
13949
 
13950
							$j=$i%4;
13950
							$j=$i%4;
13951
							
13951
 
13952
							$in[$i]=$in[$i] xor $mask_key[$j];
13952
							$in[$i]=$in[$i] xor $mask_key[$j];
13953
						
13953
 
13954
							}#for end
13954
							}#for end
13955
						
13955
 
13956
						#如果連線到 socket 失敗
13956
						#如果連線到 socket 失敗
13957
						if($socket_connect===false){
13957
						if($socket_connect===false){
13958
						
13958
 
13959
							#提示傳送資料
13959
							#提示傳送資料
13960
							echo "Sending Data...".PHP_EOL;
13960
							echo "Sending Data...".PHP_EOL;
13961
						
13961
 
13962
							}#if end
13962
							}#if end
13963
						
13963
 
13964
						#data frame
13964
						#data frame
13965
						$in=chr(base_convert($bit8[0],2,10)).chr(base_convert($bit8[1],2,10)).chr(base_convert($bit8[2],2,10)).chr(base_convert($bit8[3],2,10)).chr(base_convert($bit8[4],2,10)).chr(base_convert($bit8[5],2,10)).$in;
13965
						$in=chr(base_convert($bit8[0],2,10)).chr(base_convert($bit8[1],2,10)).chr(base_convert($bit8[2],2,10)).chr(base_convert($bit8[3],2,10)).chr(base_convert($bit8[4],2,10)).chr(base_convert($bit8[5],2,10)).$in;
13966
						
13966
 
13967
						#將訊息傳送到socket去
13967
						#將訊息傳送到socket去
13968
						socket_write($socket, $in, strlen($in));
13968
						socket_write($socket, $in, strlen($in));
13969
						
13969
 
13970
						}#if end
13970
						}#if end
13971
					
13971
 
13972
					}#if end	
13972
					}#if end
13973
				
13973
 
13974
				}#if end
13974
				}#if end
13975
				
13975
 
13976
			#反之取得了 Sec-WebSocket-Accept 就結束 HTTP/1.1
13976
			#反之取得了 Sec-WebSocket-Accept 就結束 HTTP/1.1
13977
			else{
13977
			else{
13978
			
13978
 
13979
				#如果連線到 socket 失敗
13979
				#如果連線到 socket 失敗
13980
				if($socket_connect===false){
13980
				if($socket_connect===false){
13981
			
13981
 
13982
					echo "解密前的內容:".PHP_EOL;
13982
					echo "解密前的內容:".PHP_EOL;
13983
					
13983
 
13984
					}#if end
13984
					}#if end
13985
				
13985
 
13986
				#函式說明:
13986
				#函式說明:
13987
				#將字串中的每個字變成bytes陣列
13987
				#將字串中的每個字變成bytes陣列
13988
				#回傳結果:
13988
				#回傳結果:
13989
				#$result...
13989
				#$result...
13990
				#必填參數:
13990
				#必填參數:
Line 13993... Line 13993...
13993
				#可省略參數:
13993
				#可省略參數:
13994
				#無
13994
				#無
13995
				#建構中...
13995
				#建構中...
13996
				$str2bytesArray=stringProcess::str2bytesArray($conf["stringProcess::str2bytesArray"]);
13996
				$str2bytesArray=stringProcess::str2bytesArray($conf["stringProcess::str2bytesArray"]);
13997
				unset($conf["stringProcess::str2bytesArray"]);
13997
				unset($conf["stringProcess::str2bytesArray"]);
13998
				
13998
 
13999
				#針對每個bytes				
13999
				#針對每個bytes
14000
				foreach($str2bytesArray["content"] as $byte){
14000
				foreach($str2bytesArray["content"] as $byte){
14001
				
14001
 
14002
					#函式說明:
14002
					#函式說明:
14003
					#將bytes數字轉換為bit字串(0跟1來表述)
14003
					#將bytes數字轉換為bit字串(0跟1來表述)
14004
					#回傳結果:
14004
					#回傳結果:
14005
					#$result...
14005
					#$result...
14006
					#必填參數:
14006
					#必填參數:
Line 14009... Line 14009...
14009
					#可省略參數:
14009
					#可省略參數:
14010
					#無
14010
					#無
14011
					#建構中...
14011
					#建構中...
14012
					$bytes2bitString=stringProcess::bytes2bitString($conf["stringProcess::str2bytesArray"]);
14012
					$bytes2bitString=stringProcess::bytes2bitString($conf["stringProcess::str2bytesArray"]);
14013
					unset($conf["stringProcess::str2bytesArray"]);
14013
					unset($conf["stringProcess::str2bytesArray"]);
14014
					
14014
 
14015
					#如果連線到 socket 失敗
14015
					#如果連線到 socket 失敗
14016
					if($socket_connect===false){
14016
					if($socket_connect===false){
14017
					
14017
 
14018
						#debug
14018
						#debug
14019
						var_dump($bytes2bitString["content"]);
14019
						var_dump($bytes2bitString["content"]);
14020
				
14020
 
14021
						}#if end
14021
						}#if end
14022
				
14022
 
14023
					}#foreach end
14023
					}#foreach end
14024
				
14024
 
14025
				#如果連線到 socket 失敗
14025
				#如果連線到 socket 失敗
14026
				if($socket_connect===false){
14026
				if($socket_connect===false){
14027
				
14027
 
14028
					echo "解密後接收的內容:".PHP_EOL;
14028
					echo "解密後接收的內容:".PHP_EOL;
14029
					
14029
 
14030
					}#if end
14030
					}#if end
14031
			
14031
 
14032
				#函式說明:
14032
				#函式說明:
14033
				#解密 handshake 後要接收的訊息
14033
				#解密 handshake 後要接收的訊息
14034
				#回傳結果:
14034
				#回傳結果:
14035
				#$result["function"],當前函式的名稱.
14035
				#$result["function"],當前函式的名稱.
14036
				#$result["status"],執行結果狀態,"true"代表正常;"false"代表不正常.
14036
				#$result["status"],執行結果狀態,"true"代表正常;"false"代表不正常.
14037
				#$result["content"],unmask後的內容.
14037
				#$result["content"],unmask後的內容.
14038
				#$result["type"],訊息的類型,"unknow":代表未定義;"text":代表為文字訊息;"pong":代表為pong;"invalid":代表不為web socket frame.
14038
				#$result["type"],訊息的類型,"unknow":代表未定義;"text":代表為文字訊息;"pong":代表為pong;"invalid":代表不為web socket frame.
14039
				#$result["error"],錯誤訊息陣列.
14039
				#$result["error"],錯誤訊息陣列.
14040
				#必填參數:
14040
				#必填參數:
14041
				#$conf["payload"],字串,要解密的訊息.
14041
				#$conf["payload"],字串,要解密的訊息.
14042
				$conf["webSock::unmask"]["payload"]=$out; 
14042
				$conf["webSock::unmask"]["payload"]=$out;
14043
				#可省略參數:
14043
				#可省略參數:
14044
				#$conf["allowUnmask"],字串,若發現沒有設置mask bit,也要繼續執行,則為"true";反之為"false".
14044
				#$conf["allowUnmask"],字串,若發現沒有設置mask bit,也要繼續執行,則為"true";反之為"false".
14045
				$conf["webSock::unmask"]["allowUnmask"]="true";
14045
				$conf["webSock::unmask"]["allowUnmask"]="true";
14046
				#參考資料
14046
				#參考資料
14047
				#http://www.inanzzz.com/index.php/post/swf8/converting-string-to-binary-and-binary-to-string-with-php
14047
				#http://www.inanzzz.com/index.php/post/swf8/converting-string-to-binary-and-binary-to-string-with-php
Line 14049... Line 14049...
14049
				#https://tools.ietf.org/html/rfc5234
14049
				#https://tools.ietf.org/html/rfc5234
14050
				#備註:
14050
				#備註:
14051
				#無.
14051
				#無.
14052
				$unmask=webSock::unmask($conf["webSock::unmask"]);
14052
				$unmask=webSock::unmask($conf["webSock::unmask"]);
14053
				unset($conf["webSock::unmask"]);
14053
				unset($conf["webSock::unmask"]);
14054
							
14054
 
14055
				#如果執行失敗
14055
				#如果執行失敗
14056
				if($unmask["status"]==="false"){
14056
				if($unmask["status"]==="false"){
14057
					
14057
 
14058
					#設置執行失敗
14058
					#設置執行失敗
14059
					$result["status"]="false";
14059
					$result["status"]="false";
14060
				
14060
 
14061
					#設置執行錯誤訊息
14061
					#設置執行錯誤訊息
14062
					$result["error"]=$unmask;
14062
					$result["error"]=$unmask;
14063
					
14063
 
14064
					#回傳結果
14064
					#回傳結果
14065
					return $result;
14065
					return $result;
14066
					
14066
 
14067
					}#if end
14067
					}#if end
14068
							
14068
 
14069
				#記錄接收到的內容
14069
				#記錄接收到的內容
14070
				$received=$unmask["content"];
14070
				$received=$unmask["content"];
14071
					
14071
 
14072
				#如果連線到 socket 失敗
14072
				#如果連線到 socket 失敗
14073
				if($socket_connect===false){	
14073
				if($socket_connect===false){
14074
					
14074
 
14075
					#debug
14075
					#debug
14076
					var_dump($received);
14076
					var_dump($received);
14077
			
14077
 
14078
					}#if end
14078
					}#if end
14079
			
14079
 
14080
				}#else end
14080
				}#else end
14081
			
14081
 
14082
			#休息1秒
14082
			#休息1秒
14083
			sleep(1);
14083
			sleep(1);
14084
			
14084
 
14085
			}#while end
14085
			}#while end
14086
 
14086
 
14087
		#如果連線到 socket 失敗
14087
		#如果連線到 socket 失敗
14088
		if($socket_connect===false){
14088
		if($socket_connect===false){
14089
 
14089
 
14090
			#提示關閉與 socket 的連線
14090
			#提示關閉與 socket 的連線
14091
			echo "Closing socket...";
14091
			echo "Closing socket...";
14092
		
14092
 
14093
			}#if end
14093
			}#if end
14094
		
14094
 
14095
		#關閉與 socket 的連線
14095
		#關閉與 socket 的連線
14096
		socket_close($socket);
14096
		socket_close($socket);
14097
		
14097
 
14098
		#如果連線到 socket 失敗
14098
		#如果連線到 socket 失敗
14099
		if($socket_connect===false){
14099
		if($socket_connect===false){
14100
		
14100
 
14101
			#提示關閉連線成功
14101
			#提示關閉連線成功
14102
			echo "OK.".PHP_EOL;
14102
			echo "OK.".PHP_EOL;
14103
		
14103
 
14104
			}#if end
14104
			}#if end
14105
		
14105
 
14106
		#設置執行正常
14106
		#設置執行正常
14107
		$result["status"]="true";
14107
		$result["status"]="true";
14108
		
14108
 
14109
		#回傳結果
14109
		#回傳結果
14110
		return $result;
14110
		return $result;
14111
		
14111
 
14112
		}#function clientDaemon end
14112
		}#function clientDaemon end
14113
	
14113
 
14114
	/*	
14114
	/*
14115
	#函式說明:
14115
	#函式說明:
14116
	#webSocket的javaScript用戶端
14116
	#webSocket的javaScript用戶端
14117
	#回傳結果:
14117
	#回傳結果:
14118
	#$result["status"],執行正常與否,"true"代表正常,"false"代表不正常.
14118
	#$result["status"],執行正常與否,"true"代表正常,"false"代表不正常.
14119
	#$result["error"],錯誤訊息陣列.
14119
	#$result["error"],錯誤訊息陣列.
Line 14132... Line 14132...
14132
	#無.
14132
	#無.
14133
	#備註:
14133
	#備註:
14134
	#無.
14134
	#無.
14135
	*/
14135
	*/
14136
	public static function client(&$conf){
14136
	public static function client(&$conf){
14137
		
14137
 
14138
		#初始化要回傳的結果
14138
		#初始化要回傳的結果
14139
		$result=array();
14139
		$result=array();
14140
 
14140
 
14141
		#取得當前執行的函數名稱
14141
		#取得當前執行的函數名稱
14142
		$result["function"]=__FUNCTION__;
14142
		$result["function"]=__FUNCTION__;
14143
 
14143
 
14144
		#如果沒有參數
14144
		#如果沒有參數
14145
		if(func_num_args()==0){
14145
		if(func_num_args()==0){
14146
			
14146
 
14147
			#設置執行失敗
14147
			#設置執行失敗
14148
			$result["status"]="false";
14148
			$result["status"]="false";
14149
			
14149
 
14150
			#設置執行錯誤訊息
14150
			#設置執行錯誤訊息
14151
			$result["error"]="函數".$result["function"]."需要參數";
14151
			$result["error"]="函數".$result["function"]."需要參數";
14152
			
14152
 
14153
			#回傳結果
14153
			#回傳結果
14154
			return $result;
14154
			return $result;
14155
			
14155
 
14156
			}#if end
14156
			}#if end
14157
 
14157
 
14158
		#取得參數
14158
		#取得參數
14159
		$result["argu"]=$conf;
14159
		$result["argu"]=$conf;
14160
 
14160
 
14161
		#如果 $conf 不為陣列
14161
		#如果 $conf 不為陣列
14162
		if(gettype($conf)!=="array"){
14162
		if(gettype($conf)!=="array"){
14163
			
14163
 
14164
			#設置執行失敗
14164
			#設置執行失敗
14165
			$result["status"]="false";
14165
			$result["status"]="false";
14166
			
14166
 
14167
			#設置執行錯誤訊息
14167
			#設置執行錯誤訊息
14168
			$result["error"][]="\$conf變數須為陣列形態";
14168
			$result["error"][]="\$conf變數須為陣列形態";
14169
			
14169
 
14170
			#如果傳入的參數為 null
14170
			#如果傳入的參數為 null
14171
			if($conf===null){
14171
			if($conf===null){
14172
				
14172
 
14173
				#設置執行錯誤訊息
14173
				#設置執行錯誤訊息
14174
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
14174
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
14175
				
14175
 
14176
				}#if end
14176
				}#if end
14177
 
14177
 
14178
			#回傳結果
14178
			#回傳結果
14179
			return $result;
14179
			return $result;
14180
			
14180
 
14181
			}#if end
14181
			}#if end
14182
		
14182
 
14183
		#檢查參數
14183
		#檢查參數
14184
		#函式說明:
14184
		#函式說明:
14185
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
14185
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
14186
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
14186
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
14187
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
14187
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
Line 14212... Line 14212...
14212
		#$conf["canBeEmpty"]=array();
14212
		#$conf["canBeEmpty"]=array();
14213
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
14213
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
14214
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array();
14214
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array();
14215
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
14215
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
14216
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("entry","connVar");
14216
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("entry","connVar");
14217
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double"); 
14217
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
14218
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string");
14218
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string");
14219
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
14219
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
14220
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("false","conn");
14220
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("false","conn");
14221
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
14221
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
14222
		#$conf["arrayCountEqualCheck"][]=array();
14222
		#$conf["arrayCountEqualCheck"][]=array();
Line 14224... Line 14224...
14224
		#array_keys=>http://php.net/manual/en/function.array-keys.php
14224
		#array_keys=>http://php.net/manual/en/function.array-keys.php
14225
		#建議:
14225
		#建議:
14226
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
14226
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
14227
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
14227
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
14228
		unset($conf["variableCheck::checkArguments"]);
14228
		unset($conf["variableCheck::checkArguments"]);
14229
		
14229
 
14230
		#如果檢查參數失敗
14230
		#如果檢查參數失敗
14231
		if($checkArguments["status"]==="false"){
14231
		if($checkArguments["status"]==="false"){
14232
			
14232
 
14233
			#設置執行失敗
14233
			#設置執行失敗
14234
			$result["status"]="false";
14234
			$result["status"]="false";
14235
		
14235
 
14236
			#設置執行錯誤訊息
14236
			#設置執行錯誤訊息
14237
			$result["error"]=$checkArguments;
14237
			$result["error"]=$checkArguments;
14238
			
14238
 
14239
			#回傳結果
14239
			#回傳結果
14240
			return $result;
14240
			return $result;
14241
			
14241
 
14242
			}#if end
14242
			}#if end
14243
			
14243
 
14244
		#如果參數檢查不通過
14244
		#如果參數檢查不通過
14245
		if($checkArguments["passed"]==="false"){
14245
		if($checkArguments["passed"]==="false"){
14246
			
14246
 
14247
			#設置執行失敗
14247
			#設置執行失敗
14248
			$result["status"]="false";
14248
			$result["status"]="false";
14249
		
14249
 
14250
			#設置執行錯誤訊息
14250
			#設置執行錯誤訊息
14251
			$result["error"]=$checkArguments;
14251
			$result["error"]=$checkArguments;
14252
			
14252
 
14253
			#回傳結果
14253
			#回傳結果
14254
			return $result;
14254
			return $result;
14255
			
14255
 
14256
			}#if end		
14256
			}#if end
14257
		
14257
 
14258
		#初始化 web socket 的協定
14258
		#初始化 web socket 的協定
14259
		$webSockUrl="ws://";
14259
		$webSockUrl="ws://";
14260
		
14260
 
14261
		#如果要加密
14261
		#如果要加密
14262
		if($conf["entry"]==="true"){
14262
		if($conf["entry"]==="true"){
14263
			
14263
 
14264
			$webSockUrl="wss://";
14264
			$webSockUrl="wss://";
14265
			
14265
 
14266
			}#if end
14266
			}#if end
14267
		
14267
 
14268
		#設置用戶端連線到webSocket的範例
14268
		#設置用戶端連線到webSocket的範例
14269
		$script="<script>
14269
		$script="<script>
14270
		
14270
 
14271
			//建立 web socket 連線到 ".$webSockUrl.$conf["url"]."
14271
			//建立 web socket 連線到 ".$webSockUrl.$conf["url"]."
14272
			var ".$conf["connVar"]." = new WebSocket('".$webSockUrl.$conf["url"]."');
14272
			var ".$conf["connVar"]." = new WebSocket('".$webSockUrl.$conf["url"]."');
14273
			
14273
 
14274
			//當連線成功後
14274
			//當連線成功後
14275
			".$conf["connVar"].".onopen = function(e)
14275
			".$conf["connVar"].".onopen = function(e)
14276
			{
14276
			{
14277
				//印出連線成功訊息到console
14277
				//印出連線成功訊息到console
14278
				console.log(\"Connection established!\");
14278
				console.log(\"Connection established!\");
14279
			};
14279
			};
14280
 
14280
 
14281
			//當有收到訊息時
14281
			//當有收到訊息時
14282
			".$conf["connVar"].".onmessage = function(e) {
14282
			".$conf["connVar"].".onmessage = function(e) {
14283
				
14283
 
14284
				//將訊息顯現在console
14284
				//將訊息顯現在console
14285
				console.log(e.data);
14285
				console.log(e.data);
14286
			};
14286
			};
14287
			</script>";
14287
			</script>";
14288
			
14288
 
14289
		#設置建立webSocket連線的按鈕
14289
		#設置建立webSocket連線的按鈕
14290
		#函式說明:
14290
		#函式說明:
14291
		#放置按鈕
14291
		#放置按鈕
14292
		#回傳結果:
14292
		#回傳結果:
14293
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
14293
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
14294
		#$result["function"],當前執行的函數.
14294
		#$result["function"],當前執行的函數.
14295
		#$result["error"],錯誤訊息陣列.
14295
		#$result["error"],錯誤訊息陣列.
14296
		#$result["content"],按鈕語法. 
14296
		#$result["content"],按鈕語法.
14297
		#必要的參數:
14297
		#必要的參數:
14298
		$conf["form::button"]["buttonDisplayValue"]="Send Hello!";#爲按鈕上顯示的文字。
14298
		$conf["form::button"]["buttonDisplayValue"]="Send Hello!";#爲按鈕上顯示的文字。
14299
		#可省略的參數:
14299
		#可省略的參數:
14300
		#$conf["buttonStyleName"]="";#可省略,爲按鈕所要使用的css樣式類別名稱,預設的css樣式爲 __simpleButtonLinkDefaultButtonCssStyle 。
14300
		#$conf["buttonStyleName"]="";#可省略,爲按鈕所要使用的css樣式類別名稱,預設的css樣式爲 __simpleButtonLinkDefaultButtonCssStyle 。
14301
			#其屬性爲   "width","height","font-size","text-align"
14301
			#其屬性爲   "width","height","font-size","text-align"
Line 14325... Line 14325...
14325
		#$conf["buttonId"]="";
14325
		#$conf["buttonId"]="";
14326
		#參考資料來源:
14326
		#參考資料來源:
14327
		#http://stackoverflow.com/questions/3014649/how-to-disable-html-button-using-javascript
14327
		#http://stackoverflow.com/questions/3014649/how-to-disable-html-button-using-javascript
14328
		$button=form::button($conf["form::button"]);
14328
		$button=form::button($conf["form::button"]);
14329
		unset($conf["form::button"]);
14329
		unset($conf["form::button"]);
14330
			
14330
 
14331
		#如果建立按鈕失敗
14331
		#如果建立按鈕失敗
14332
		if($button["status"]==="false"){
14332
		if($button["status"]==="false"){
14333
			
14333
 
14334
			#設置執行失敗
14334
			#設置執行失敗
14335
			$result["status"]="false";
14335
			$result["status"]="false";
14336
			
14336
 
14337
			#設置執行錯誤訊息
14337
			#設置執行錯誤訊息
14338
			$result["error"]=$button;
14338
			$result["error"]=$button;
14339
			
14339
 
14340
			#回傳結果
14340
			#回傳結果
14341
			return $result;
14341
			return $result;
14342
			
14342
 
14343
			}#if end	
14343
			}#if end
14344
			
14344
 
14345
		#設置用戶端的html與js語法
14345
		#設置用戶端的html與js語法
14346
		$result["content"]=$button["content"].$script;
14346
		$result["content"]=$button["content"].$script;
14347
			
14347
 
14348
		#設置執行正常
14348
		#設置執行正常
14349
		$result["status"]="true";
14349
		$result["status"]="true";
14350
		
14350
 
14351
		#回傳結果
14351
		#回傳結果
14352
		return $result;
14352
		return $result;
14353
		
14353
 
14354
		}#function client end
14354
		}#function client end
14355
	
14355
 
14356
	/*
14356
	/*
14357
	#函式說明:
14357
	#函式說明:
14358
	#將 handshake 後含有 mask bit 的 web socket 訊息進行  mask/unmask.
14358
	#將 handshake 後含有 mask bit 的 web socket 訊息進行  mask/unmask.
14359
	#回傳結果:
14359
	#回傳結果:
14360
	#$result["function"],當前函式的名稱.
14360
	#$result["function"],當前函式的名稱.
Line 14363... Line 14363...
14363
	#$result["content"],unmask後的內容.
14363
	#$result["content"],unmask後的內容.
14364
	#$result["type"],訊息的類型,"unknow":代表未定義;"text":代表為文字訊息;"pong":代表為pong;"invalid":代表不為web socket frame.
14364
	#$result["type"],訊息的類型,"unknow":代表未定義;"text":代表為文字訊息;"pong":代表為pong;"invalid":代表不為web socket frame.
14365
	#$result["error"],錯誤訊息陣列.
14365
	#$result["error"],錯誤訊息陣列.
14366
	#必填參數:
14366
	#必填參數:
14367
	#$conf["payload"],字串,要解密的訊息.
14367
	#$conf["payload"],字串,要解密的訊息.
14368
	$conf["payload"]=""; 
14368
	$conf["payload"]="";
14369
	#可省略參數:
14369
	#可省略參數:
14370
	$conf["allowUnmask"],字串,若發現沒有設置mask bit,也要繼續執行,則為"true";反之為"false".
14370
	$conf["allowUnmask"],字串,若發現沒有設置mask bit,也要繼續執行,則為"true";反之為"false".
14371
	$conf["allowUnmask"]="false";
14371
	$conf["allowUnmask"]="false";
14372
	#參考資料
14372
	#參考資料
14373
	#http://www.inanzzz.com/index.php/post/swf8/converting-string-to-binary-and-binary-to-string-with-php
14373
	#http://www.inanzzz.com/index.php/post/swf8/converting-string-to-binary-and-binary-to-string-with-php
Line 14375... Line 14375...
14375
	#https://tools.ietf.org/html/rfc5234
14375
	#https://tools.ietf.org/html/rfc5234
14376
	#備註:
14376
	#備註:
14377
	#無.
14377
	#無.
14378
	*/
14378
	*/
14379
	public static function mask(&$conf){
14379
	public static function mask(&$conf){
14380
	
14380
 
14381
		#改呼叫 webSock::umask
14381
		#改呼叫 webSock::umask
14382
		return webSock::unmask($conf);
14382
		return webSock::unmask($conf);
14383
	
14383
 
14384
		}#function mask end
14384
		}#function mask end
14385
	
14385
 
14386
	/*
14386
	/*
14387
	#函式說明:
14387
	#函式說明:
14388
	#將 handshake 後含有 mask bit 的 web socket 訊息進行  mask/unmask.
14388
	#將 handshake 後含有 mask bit 的 web socket 訊息進行  mask/unmask.
14389
	#回傳結果:
14389
	#回傳結果:
14390
	#$result["function"],當前函式的名稱.
14390
	#$result["function"],當前函式的名稱.
Line 14393... Line 14393...
14393
	#$result["content"],unmask後的內容.
14393
	#$result["content"],unmask後的內容.
14394
	#$result["type"],訊息的類型,"unknow":代表未定義;"text":代表為文字訊息;"pong":代表為pong;"invalid":代表不為web socket frame.
14394
	#$result["type"],訊息的類型,"unknow":代表未定義;"text":代表為文字訊息;"pong":代表為pong;"invalid":代表不為web socket frame.
14395
	#$result["error"],錯誤訊息陣列.
14395
	#$result["error"],錯誤訊息陣列.
14396
	#必填參數:
14396
	#必填參數:
14397
	#$conf["payload"],字串,要解密的訊息.
14397
	#$conf["payload"],字串,要解密的訊息.
14398
	$conf["payload"]=""; 
14398
	$conf["payload"]="";
14399
	#可省略參數:
14399
	#可省略參數:
14400
	#$conf["allowUnmask"],字串,若發現沒有設置mask bit,也要繼續執行,則為"true";反之為"false".
14400
	#$conf["allowUnmask"],字串,若發現沒有設置mask bit,也要繼續執行,則為"true";反之為"false".
14401
	#$conf["allowUnmask"]="false";
14401
	#$conf["allowUnmask"]="false";
14402
	#$conf["debug"],字串,"true"代表要show debug訊息;反之為預設"false".
14402
	#$conf["debug"],字串,"true"代表要show debug訊息;反之為預設"false".
14403
	#$conf["debug"]="false";
14403
	#$conf["debug"]="false";
Line 14407... Line 14407...
14407
	#https://tools.ietf.org/html/rfc5234
14407
	#https://tools.ietf.org/html/rfc5234
14408
	#備註:
14408
	#備註:
14409
	#無.
14409
	#無.
14410
	*/
14410
	*/
14411
	public static function unmask(&$conf) {
14411
	public static function unmask(&$conf) {
14412
		
14412
 
14413
		#初始化要回傳的結果
14413
		#初始化要回傳的結果
14414
		$result=array();
14414
		$result=array();
14415
 
14415
 
14416
		#取得當前執行的函數名稱
14416
		#取得當前執行的函數名稱
14417
		$result["function"]=__FUNCTION__;
14417
		$result["function"]=__FUNCTION__;
14418
		
14418
 
14419
		#取得參數內容
14419
		#取得參數內容
14420
		$result["argu"]=$conf;
14420
		$result["argu"]=$conf;
14421
		
14421
 
14422
		#檢查參數
14422
		#檢查參數
14423
		#函式說明:
14423
		#函式說明:
14424
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
14424
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
14425
		#回傳結果:
14425
		#回傳結果:
14426
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
14426
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 14472... Line 14472...
14472
		#array_keys=>http://php.net/manual/en/function.array-keys.php
14472
		#array_keys=>http://php.net/manual/en/function.array-keys.php
14473
		#備註:
14473
		#備註:
14474
		#無.
14474
		#無.
14475
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
14475
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
14476
		unset($conf["variableCheck::checkArguments"]);
14476
		unset($conf["variableCheck::checkArguments"]);
14477
		
14477
 
14478
		#如果檢查參數失敗
14478
		#如果檢查參數失敗
14479
		if($checkArguments["status"]==="false"){
14479
		if($checkArguments["status"]==="false"){
14480
		
14480
 
14481
			#設置執行不正常
14481
			#設置執行不正常
14482
			$result["status"]="false";
14482
			$result["status"]="false";
14483
			
14483
 
14484
			#設置錯誤訊息
14484
			#設置錯誤訊息
14485
			$result["error"]=$checkArguments;
14485
			$result["error"]=$checkArguments;
14486
			
14486
 
14487
			#回傳結果
14487
			#回傳結果
14488
			return $result;
14488
			return $result;
14489
		
14489
 
14490
			}#if end
14490
			}#if end
14491
			
14491
 
14492
		#如果檢查參數不通過
14492
		#如果檢查參數不通過
14493
		if($checkArguments["passed"]==="false"){
14493
		if($checkArguments["passed"]==="false"){
14494
		
14494
 
14495
			#設置執行不正常
14495
			#設置執行不正常
14496
			$result["status"]="false";
14496
			$result["status"]="false";
14497
			
14497
 
14498
			#設置錯誤訊息
14498
			#設置錯誤訊息
14499
			$result["error"]=$checkArguments;
14499
			$result["error"]=$checkArguments;
14500
			
14500
 
14501
			#回傳結果
14501
			#回傳結果
14502
			return $result;
14502
			return $result;
14503
		
14503
 
14504
			}#if end
14504
			}#if end
14505
		
14505
 
14506
		#取得變數
14506
		#取得變數
14507
		$payload=$conf["payload"];
14507
		$payload=$conf["payload"];
14508
		
14508
 
14509
		#如果要debug
14509
		#如果要debug
14510
		if($conf["debug"]==="true"){
14510
		if($conf["debug"]==="true"){
14511
				
14511
 
14512
			#debug payload length
14512
			#debug payload length
14513
			echo __LINE__." strlen(\$payload)=".strlen($payload).PHP_EOL;
14513
			echo __LINE__." strlen(\$payload)=".strlen($payload).PHP_EOL;
14514
		
14514
 
14515
			}#if end
14515
			}#if end
14516
			
14516
 
14517
		#web socket frame 最少為 2bytes
14517
		#web socket frame 最少為 2bytes
14518
		if(strlen($payload)<2){
14518
		if(strlen($payload)<2){
14519
			
14519
 
14520
			#函式說明:
14520
			#函式說明:
14521
			#將字串中的每個字變成hex的字串陣列
14521
			#將字串中的每個字變成hex的字串陣列
14522
			#回傳結果:
14522
			#回傳結果:
14523
			#$result["status"],執行正常與否,"false"代表不正常,"true"代表正常.
14523
			#$result["status"],執行正常與否,"false"代表不正常,"true"代表正常.
14524
			#$result["function"],當前執行的函式內容.
14524
			#$result["function"],當前執行的函式內容.
Line 14533... Line 14533...
14533
			#無.
14533
			#無.
14534
			#備註:
14534
			#備註:
14535
			#無.
14535
			#無.
14536
			$str2bytesArray=stringProcess::str2bytesArray($conf["stringProcess::str2bytesArray"]);
14536
			$str2bytesArray=stringProcess::str2bytesArray($conf["stringProcess::str2bytesArray"]);
14537
			unset($conf["stringProcess::str2bytesArray"]);
14537
			unset($conf["stringProcess::str2bytesArray"]);
14538
			
14538
 
14539
			#如果檢查參數失敗
14539
			#如果檢查參數失敗
14540
			if($str2bytesArray["status"]==="false"){
14540
			if($str2bytesArray["status"]==="false"){
14541
			
14541
 
14542
				#設置執行不正常
14542
				#設置執行不正常
14543
				$result["status"]="false";
14543
				$result["status"]="false";
14544
				
14544
 
14545
				#設置錯誤訊息
14545
				#設置錯誤訊息
14546
				$result["error"]=$str2bytesArray;
14546
				$result["error"]=$str2bytesArray;
14547
				
14547
 
14548
				#回傳結果
14548
				#回傳結果
14549
				return $result;
14549
				return $result;
14550
			
14550
 
14551
				}#if end
14551
				}#if end
14552
			
14552
 
14553
			#設置警告訊息
14553
			#設置警告訊息
14554
			$result["warning"][]="web socket frame 最少為 2 bytes,目前收到得為 ".strlen($payload)." bytes";
14554
			$result["warning"][]="web socket frame 最少為 2 bytes,目前收到得為 ".strlen($payload)." bytes";
14555
			
14555
 
14556
			#設置警告訊息
14556
			#設置警告訊息
14557
			$result["warning"][]="接收到的 web socket frame 內容";
14557
			$result["warning"][]="接收到的 web socket frame 內容";
14558
			
14558
 
14559
			#設置警告訊息
14559
			#設置警告訊息
14560
			$result["warning"][]="(ASCII):".$payload;
14560
			$result["warning"][]="(ASCII):".$payload;
14561
			
14561
 
14562
			#開始截取輸出
14562
			#開始截取輸出
14563
			ob_start();
14563
			ob_start();
14564
			
14564
 
14565
			#針對每個bytes
14565
			#針對每個bytes
14566
			for($i=0;$i<count($str2bytesArray["content"]);$i++){
14566
			for($i=0;$i<count($str2bytesArray["content"]);$i++){
14567
			
14567
 
14568
				#印出其內容
14568
				#印出其內容
14569
				echo "[".$str2bytesArray["content"][$i]."]";
14569
				echo "[".$str2bytesArray["content"][$i]."]";
14570
			
14570
 
14571
				#若不是最後一個byte
14571
				#若不是最後一個byte
14572
				if($i===count($str2bytesArray["content"])-1){
14572
				if($i===count($str2bytesArray["content"])-1){
14573
				
14573
 
14574
					#空一格
14574
					#空一格
14575
					echo " ";
14575
					echo " ";
14576
				
14576
 
14577
					}#if end
14577
					}#if end
14578
			
14578
 
14579
				}#if end
14579
				}#if end
14580
			
14580
 
14581
			#取得用HEX表示的內容
14581
			#取得用HEX表示的內容
14582
			$hexStr=ob_get_contents();
14582
			$hexStr=ob_get_contents();
14583
			
14583
 
14584
			#結束截取輸出
14584
			#結束截取輸出
14585
			ob_end_clean();
14585
			ob_end_clean();
14586
			
14586
 
14587
			#設置警告訊息
14587
			#設置警告訊息
14588
			$result["warning"][]="(HEX):".$hexStr;
14588
			$result["warning"][]="(HEX):".$hexStr;
14589
			
14589
 
14590
			#設置為非 web socket frame
14590
			#設置為非 web socket frame
14591
			$result["type"]="invalid";
14591
			$result["type"]="invalid";
14592
			
14592
 
14593
			#設置執行正常
14593
			#設置執行正常
14594
			$result["status"]="true";
14594
			$result["status"]="true";
14595
			
14595
 
14596
			#回傳結果
14596
			#回傳結果
14597
			return $result;
14597
			return $result;
14598
		
14598
 
14599
			}#if end
14599
			}#if end
14600
		
14600
 
14601
		#如果要debug
14601
		#如果要debug
14602
		if($conf["debug"]==="true"){
14602
		if($conf["debug"]==="true"){
14603
		
14603
 
14604
			#debug data length
14604
			#debug data length
14605
			echo __LINE__." \$length=".(ord($payload[1]) & 127).PHP_EOL;
14605
			echo __LINE__." \$length=".(ord($payload[1]) & 127).PHP_EOL;
14606
		
14606
 
14607
			}#if end
14607
			}#if end
14608
		
14608
 
14609
		#取得字串長度
14609
		#取得字串長度
14610
		$psl=strlen($payload);
14610
		$psl=strlen($payload);
14611
		
14611
 
14612
		#初始化儲存每個bytes
14612
		#初始化儲存每個bytes
14613
		$bytes=array();
14613
		$bytes=array();
14614
		
14614
 
14615
		#函式說明:
14615
		#函式說明:
14616
		#將字串中的每個字變成bytes陣列
14616
		#將字串中的每個字變成bytes陣列
14617
		#回傳結果:
14617
		#回傳結果:
14618
		#$result["status"],執行正常與否,"false"代表不正常,"true"代表正常.
14618
		#$result["status"],執行正常與否,"false"代表不正常,"true"代表正常.
14619
		#$result["function"],當前執行的函式內容.
14619
		#$result["function"],當前執行的函式內容.
Line 14624... Line 14624...
14624
		$conf["stringProcess::str2bytesArray"]["input"]=$payload;
14624
		$conf["stringProcess::str2bytesArray"]["input"]=$payload;
14625
		#可省略參數:
14625
		#可省略參數:
14626
		#無
14626
		#無
14627
		$str2bytesArray=stringProcess::str2bytesArray($conf["stringProcess::str2bytesArray"]);
14627
		$str2bytesArray=stringProcess::str2bytesArray($conf["stringProcess::str2bytesArray"]);
14628
		unset($conf["stringProcess::str2bytesArray"]);
14628
		unset($conf["stringProcess::str2bytesArray"]);
14629
		
14629
 
14630
		#如果執行失敗
14630
		#如果執行失敗
14631
		if($str2bytesArray["status"]==="false"){
14631
		if($str2bytesArray["status"]==="false"){
14632
		
14632
 
14633
			#設置執行不正常
14633
			#設置執行不正常
14634
			$result["status"]="false";
14634
			$result["status"]="false";
14635
			
14635
 
14636
			#設置錯誤訊息
14636
			#設置錯誤訊息
14637
			$result["error"]=$str2bytesArray;
14637
			$result["error"]=$str2bytesArray;
14638
			
14638
 
14639
			#回傳結果
14639
			#回傳結果
14640
			return $result;
14640
			return $result;
14641
		
14641
 
14642
			}#if end
14642
			}#if end
14643
		
14643
 
14644
		#針對每個bytes
14644
		#針對每個bytes
14645
		foreach($str2bytesArray["content"] as $bytesNum => $oneByte){
14645
		foreach($str2bytesArray["content"] as $bytesNum => $oneByte){
14646
		
14646
 
14647
			#函式說明:
14647
			#函式說明:
14648
			#將bytes數字轉換為bit陣列(0跟1來表述)
14648
			#將bytes數字轉換為bit陣列(0跟1來表述)
14649
			#回傳結果:
14649
			#回傳結果:
14650
			#$result...
14650
			#$result...
14651
			#必填參數:
14651
			#必填參數:
Line 14653... Line 14653...
14653
			$conf["stringProcess::bytes2byte"]["bytes"]=$oneByte;
14653
			$conf["stringProcess::bytes2byte"]["bytes"]=$oneByte;
14654
			#可省略參數:
14654
			#可省略參數:
14655
			#無
14655
			#無
14656
			$bytes2byte=stringProcess::bytes2bitString($conf["stringProcess::bytes2byte"]);
14656
			$bytes2byte=stringProcess::bytes2bitString($conf["stringProcess::bytes2byte"]);
14657
			unset($conf["stringProcess::bytes2byte"]);
14657
			unset($conf["stringProcess::bytes2byte"]);
14658
		
14658
 
14659
			#儲存bytes與其bit
14659
			#儲存bytes與其bit
14660
			$bytes[$bytesNum]["content"]=$bytes2byte["content"];
14660
			$bytes[$bytesNum]["content"]=$bytes2byte["content"];
14661
			
14661
 
14662
			}#foreach end
14662
			}#foreach end
14663
			
14663
 
14664
		#frame-fin 1 bit
14664
		#frame-fin 1 bit
14665
		#%x0 ; more frames of this message follow
14665
		#%x0 ; more frames of this message follow
14666
		#%x1 ; final frame of this message
14666
		#%x1 ; final frame of this message
14667
		$frameFin=$bytes[0]["content"][0];
14667
		$frameFin=$bytes[0]["content"][0];
14668
 
14668
 
14669
		#如果不是最後一個 frame
14669
		#如果不是最後一個 frame
14670
		if($frameFin==="0"){
14670
		if($frameFin==="0"){
14671
		
14671
 
14672
			#如果要debug
14672
			#如果要debug
14673
			if($conf["debug"]==="true"){
14673
			if($conf["debug"]==="true"){
14674
		
14674
 
14675
				#debug
14675
				#debug
14676
				echo __LINE__." frame-fin = 0, not supported".PHP_EOL;
14676
				echo __LINE__." frame-fin = 0, not supported".PHP_EOL;
14677
			
14677
 
14678
				}#if end
14678
				}#if end
14679
			
14679
 
14680
			#設置警告訊息
14680
			#設置警告訊息
14681
			$result["warning"][]="目前不支援大於一個frame的mask/unmask功能";
14681
			$result["warning"][]="目前不支援大於一個frame的mask/unmask功能";
14682
			
14682
 
14683
			#設置警告訊息
14683
			#設置警告訊息
14684
			$result["warning"][]=$bytes;
14684
			$result["warning"][]=$bytes;
14685
			
14685
 
14686
			#設置為非 web socket frame
14686
			#設置為非 web socket frame
14687
			$result["type"]="invalid";
14687
			$result["type"]="invalid";
14688
			
14688
 
14689
			#設置執行正常
14689
			#設置執行正常
14690
			$result["status"]="true";
14690
			$result["status"]="true";
14691
			
14691
 
14692
			#回傳結果
14692
			#回傳結果
14693
			return $result;
14693
			return $result;
14694
		
14694
 
14695
			}#if end
14695
			}#if end
14696
			
14696
 
14697
		#反之是最後一個 data frame
14697
		#反之是最後一個 data frame
14698
		else{
14698
		else{
14699
		
14699
 
14700
			#如果要debug
14700
			#如果要debug
14701
			if($conf["debug"]==="true"){
14701
			if($conf["debug"]==="true"){
14702
		
14702
 
14703
				#debug
14703
				#debug
14704
				echo __LINE__." frame-fin = 1, means this is a last data frame".PHP_EOL;
14704
				echo __LINE__." frame-fin = 1, means this is a last data frame".PHP_EOL;
14705
		
14705
 
14706
				}#if end
14706
				}#if end
14707
			
14707
 
14708
			}#else end
14708
			}#else end
14709
		
14709
 
14710
		#frame-rsv1 1 bit
14710
		#frame-rsv1 1 bit
14711
		#MUST be 0 unless negotiated otherwise
14711
		#MUST be 0 unless negotiated otherwise
14712
		$frameRsv1=$bytes[0]["content"][1];
14712
		$frameRsv1=$bytes[0]["content"][1];
14713
		       
14713
 
14714
		#frame-rsv2 1 bit
14714
		#frame-rsv2 1 bit
14715
		#MUST be 0 unless negotiated otherwise
14715
		#MUST be 0 unless negotiated otherwise
14716
		$frameRsv2=$bytes[0]["content"][2];
14716
		$frameRsv2=$bytes[0]["content"][2];
14717
		
14717
 
14718
		#frame-rsv3 1 bit
14718
		#frame-rsv3 1 bit
14719
		#MUST be 0 unless negotiated otherwise
14719
		#MUST be 0 unless negotiated otherwise
14720
		$frameRsv3=$bytes[0]["content"][3];
14720
		$frameRsv3=$bytes[0]["content"][3];
14721
		
14721
 
14722
		#如果bit[1~3]都為0
14722
		#如果bit[1~3]都為0
14723
		if($frameRsv1===$frameRsv2 && $frameRsv2===$frameRsv3 && $frameRsv3==="0"){
14723
		if($frameRsv1===$frameRsv2 && $frameRsv2===$frameRsv3 && $frameRsv3==="0"){
14724
		
14724
 
14725
			#如果要debug
14725
			#如果要debug
14726
			if($conf["debug"]==="true"){
14726
			if($conf["debug"]==="true"){
14727
			
14727
 
14728
				#debug
14728
				#debug
14729
				echo __LINE__." frame-rsv1~3 = 0".PHP_EOL;
14729
				echo __LINE__." frame-rsv1~3 = 0".PHP_EOL;
14730
			
14730
 
14731
				}#if end
14731
				}#if end
14732
				
14732
 
14733
			}#if end
14733
			}#if end
14734
		
14734
 
14735
		#如果不存在第5個bit
14735
		#如果不存在第5個bit
14736
		if(!isset($bytes[0]["content"][4])){
14736
		if(!isset($bytes[0]["content"][4])){
14737
		
14737
 
14738
			#如果要debug
14738
			#如果要debug
14739
			if($conf["debug"]==="true"){
14739
			if($conf["debug"]==="true"){
14740
		
14740
 
14741
				#debug
14741
				#debug
14742
				echo __LINE__." $bytes[0]['content'][4] not set!".PHP_EOL;
14742
				echo __LINE__." $bytes[0]['content'][4] not set!".PHP_EOL;
14743
		
14743
 
14744
				}#if end
14744
				}#if end
14745
		
14745
 
14746
			}#if end
14746
			}#if end
14747
		
14747
 
14748
		/*
14748
		/*
14749
		frame-opcode 4 bit
14749
		frame-opcode 4 bit
14750
		frame-opcode-cont       = %x0 ; frame continuation
14750
		frame-opcode-cont       = %x0 ; frame continuation
14751
 
14751
 
14752
		frame-opcode-non-control= %x1 ; text frame
14752
		frame-opcode-non-control= %x1 ; text frame
Line 14762... Line 14762...
14762
					; frames
14762
					; frames
14763
					; 4 bits in length
14763
					; 4 bits in length
14764
		*/
14764
		*/
14765
		#如果 bytes[0] 長度不足 8
14765
		#如果 bytes[0] 長度不足 8
14766
		if(gettype($bytes[0]["content"])==="array"){
14766
		if(gettype($bytes[0]["content"])==="array"){
14767
		
14767
 
14768
			if(count($bytes[0]["content"])!==8){
14768
			if(count($bytes[0]["content"])!==8){
14769
			
14769
 
14770
				#$i為4~7
14770
				#$i為4~7
14771
				for($i=4;$i<8;$i++){
14771
				for($i=4;$i<8;$i++){
14772
				
14772
 
14773
					#如果未設置
14773
					#如果未設置
14774
					if(!isset($bytes[0]["content"][$i])){
14774
					if(!isset($bytes[0]["content"][$i])){
14775
					
14775
 
14776
						#用 "0" 去補
14776
						#用 "0" 去補
14777
						$bytes[0]["content"][$i]="0";
14777
						$bytes[0]["content"][$i]="0";
14778
					
14778
 
14779
						}#if end
14779
						}#if end
14780
				
14780
 
14781
					}#for end
14781
					}#for end
14782
			
14782
 
14783
				}#if end
14783
				}#if end
14784
				
14784
 
14785
			}#if end
14785
			}#if end
14786
			
14786
 
14787
		$frameOpcode=$bytes[0]["content"][4].$bytes[0]["content"][5].$bytes[0]["content"][6].$bytes[0]["content"][7];
14787
		$frameOpcode=$bytes[0]["content"][4].$bytes[0]["content"][5].$bytes[0]["content"][6].$bytes[0]["content"][7];
14788
		
14788
 
14789
		#如果要debug
14789
		#如果要debug
14790
		if($conf["debug"]==="true"){
14790
		if($conf["debug"]==="true"){
14791
		
14791
 
14792
			#debug
14792
			#debug
14793
			echo __LINE__." frame-opcode = ".$frameOpcode.PHP_EOL;
14793
			echo __LINE__." frame-opcode = ".$frameOpcode.PHP_EOL;
14794
		
14794
 
14795
			}#if end
14795
			}#if end
14796
		
14796
 
14797
		$frameOpcodeIntX=$bytes[0]["content"][4]*pow(2,3)+$bytes[0]["content"][5]*pow(2,2)+$bytes[0]["content"][6]*pow(2,1)+$bytes[0]["content"][7]*pow(2,0); 
14797
		$frameOpcodeIntX=$bytes[0]["content"][4]*pow(2,3)+$bytes[0]["content"][5]*pow(2,2)+$bytes[0]["content"][6]*pow(2,1)+$bytes[0]["content"][7]*pow(2,0);
14798
		
14798
 
14799
		#設置預設訊息類型為 unknow
14799
		#設置預設訊息類型為 unknow
14800
		$result["type"]="unknow";
14800
		$result["type"]="unknow";
14801
		
14801
 
14802
		#如果 Opcode 等於 1
14802
		#如果 Opcode 等於 1
14803
		if($frameOpcodeIntX===1){
14803
		if($frameOpcodeIntX===1){
14804
		
14804
 
14805
			#如果要debug
14805
			#如果要debug
14806
			if($conf["debug"]==="true"){
14806
			if($conf["debug"]==="true"){
14807
		
14807
 
14808
				#debug
14808
				#debug
14809
				echo "It is a text frame".PHP_EOL;
14809
				echo "It is a text frame".PHP_EOL;
14810
			
14810
 
14811
				}#if end
14811
				}#if end
14812
			
14812
 
14813
			#設置該訊息類型為 text
14813
			#設置該訊息類型為 text
14814
			$result["type"]="text";
14814
			$result["type"]="text";
14815
		
14815
 
14816
			}#if end
14816
			}#if end
14817
			
14817
 
14818
		#如果 Opcode 等於 9
14818
		#如果 Opcode 等於 9
14819
		else if($frameOpcodeIntX===9){
14819
		else if($frameOpcodeIntX===9){
14820
		
14820
 
14821
			#如果要debug
14821
			#如果要debug
14822
			if($conf["debug"]==="true"){
14822
			if($conf["debug"]==="true"){
14823
		
14823
 
14824
				#debug
14824
				#debug
14825
				echo "It is a ping frame".PHP_EOL;
14825
				echo "It is a ping frame".PHP_EOL;
14826
		
14826
 
14827
				}#if end
14827
				}#if end
14828
		
14828
 
14829
			#設置該訊息類型為 text
14829
			#設置該訊息類型為 text
14830
			$result["type"]="ping";
14830
			$result["type"]="ping";
14831
		
14831
 
14832
			}#if end
14832
			}#if end
14833
		
14833
 
14834
		#如果是 pong
14834
		#如果是 pong
14835
		else if($frameOpcodeIntX===10){
14835
		else if($frameOpcodeIntX===10){
14836
		
14836
 
14837
			#如果要debug
14837
			#如果要debug
14838
			if($conf["debug"]==="true"){
14838
			if($conf["debug"]==="true"){
14839
		
14839
 
14840
				#debug
14840
				#debug
14841
				echo "It is a pong frame".PHP_EOL;
14841
				echo "It is a pong frame".PHP_EOL;
14842
 
14842
 
14843
				}#if end
14843
				}#if end
14844
 
14844
 
14845
			#設置該訊息類型為 pong
14845
			#設置該訊息類型為 pong
14846
			$result["type"]="pong";
14846
			$result["type"]="pong";
14847
		
14847
 
14848
			}#if end
14848
			}#if end
14849
		
14849
 
14850
		#frame-masked 1 bit
14850
		#frame-masked 1 bit
14851
		#%x0 ;frame is not masked, no frame-masking-key
14851
		#%x0 ;frame is not masked, no frame-masking-key
14852
		#%x1 ;frame is masked, frame-masking-key present 1 bit in lengt
14852
		#%x1 ;frame is masked, frame-masking-key present 1 bit in lengt
14853
		$frameMasked=$bytes[1]["content"][0]; 
14853
		$frameMasked=$bytes[1]["content"][0];
14854
		
14854
 
14855
		#預設收到的訊息有mask過.
14855
		#預設收到的訊息有mask過.
14856
		$masked=true;	
14856
		$masked=true;
14857
	
14857
 
14858
		#如果沒有 masked 
14858
		#如果沒有 masked
14859
		if($frameMasked!=='1'){
14859
		if($frameMasked!=='1'){
14860
		
14860
 
14861
			#如果不允許沒有設置 masked bit 
14861
			#如果不允許沒有設置 masked bit
14862
			if($conf["allowUnmask"]!=="true"){
14862
			if($conf["allowUnmask"]!=="true"){
14863
		
14863
 
14864
				#如果要debug
14864
				#如果要debug
14865
				if($conf["debug"]==="true"){
14865
				if($conf["debug"]==="true"){
14866
		
14866
 
14867
					#debug
14867
					#debug
14868
					echo __LINE__." frame-masked !== 1, differ with https://tools.ietf.org/html/rfc6455#section-5.3".PHP_EOL;
14868
					echo __LINE__." frame-masked !== 1, differ with https://tools.ietf.org/html/rfc6455#section-5.3".PHP_EOL;
14869
				
14869
 
14870
					}#if end
14870
					}#if end
14871
				
14871
 
14872
				#設置為非 web socket frame
14872
				#設置為非 web socket frame
14873
				$result["type"]="invalid";
14873
				$result["type"]="invalid";
14874
				
14874
 
14875
				#設置執行正常
14875
				#設置執行正常
14876
				$result["status"]="true";
14876
				$result["status"]="true";
14877
				
14877
 
14878
				#回傳結果
14878
				#回傳結果
14879
				return $result;
14879
				return $result;
14880
				
14880
 
14881
				}#if end
14881
				}#if end
14882
				
14882
 
14883
			#設置收到的訊息沒有mask過.
14883
			#設置收到的訊息沒有mask過.
14884
			$masked=false;
14884
			$masked=false;
14885
					
14885
 
14886
			}#if end
14886
			}#if end
14887
			
14887
 
14888
		#debug
14888
		#debug
14889
		#echo __LINE__." frame-masked = ".$frameMasked.PHP_EOL;
14889
		#echo __LINE__." frame-masked = ".$frameMasked.PHP_EOL;
14890
		
14890
 
14891
		#frame-payload-length 7 bit, 7+16 bits, or 7+64 bits
14891
		#frame-payload-length 7 bit, 7+16 bits, or 7+64 bits
14892
		/*
14892
		/*
14893
		The length of the "Payload data", in bytes: if 0-125, that is the
14893
		The length of the "Payload data", in bytes: if 0-125, that is the
14894
		payload length.  If 126, the following 2 bytes interpreted as a
14894
		payload length.  If 126, the following 2 bytes interpreted as a
14895
		16-bit unsigned integer are the payload length.  If 127, the
14895
		16-bit unsigned integer are the payload length.  If 127, the
Line 14903... Line 14903...
14903
		"Application data".  The length of the "Extension data" may be
14903
		"Application data".  The length of the "Extension data" may be
14904
		zero, in which case the payload length is the length of the
14904
		zero, in which case the payload length is the length of the
14905
		"Application data".
14905
		"Application data".
14906
		*/
14906
		*/
14907
		if(gettype($bytes[1]["content"])==="array"){
14907
		if(gettype($bytes[1]["content"])==="array"){
14908
		
14908
 
14909
			if(count($bytes[1]["content"])!==8){	
14909
			if(count($bytes[1]["content"])!==8){
14910
				
14910
 
14911
				#如果要debug
14911
				#如果要debug
14912
				if($conf["debug"]==="true"){
14912
				if($conf["debug"]==="true"){
14913
				
14913
 
14914
					#debug
14914
					#debug
14915
					echo __LINE__." payloadLengthBit length !== 8".PHP_EOL;
14915
					echo __LINE__." payloadLengthBit length !== 8".PHP_EOL;
14916
				
14916
 
14917
					}#if end
14917
					}#if end
14918
				
14918
 
14919
				#如果 bit 不存在,用 0 補上
14919
				#如果 bit 不存在,用 0 補上
14920
				for($i=1;$i<8;$i++){
14920
				for($i=1;$i<8;$i++){
14921
				
14921
 
14922
					#如果未設置
14922
					#如果未設置
14923
					if(!isset($bytes[1]["content"][$i])){
14923
					if(!isset($bytes[1]["content"][$i])){
14924
					
14924
 
14925
						#用"0"替代
14925
						#用"0"替代
14926
						$bytes[1]["content"][$i]="0";
14926
						$bytes[1]["content"][$i]="0";
14927
					
14927
 
14928
						}#if end
14928
						}#if end
14929
				
14929
 
14930
					}#for end
14930
					}#for end
14931
				
14931
 
14932
				#代表是用戶在連線.
14932
				#代表是用戶在連線.
14933
				#return "client connected!";
14933
				#return "client connected!";
14934
			
14934
 
14935
				}#if end
14935
				}#if end
14936
				
14936
 
14937
			}#if end
14937
			}#if end
14938
				
14938
 
14939
		$payloadLengthBit=$bytes[1]["content"][1].$bytes[1]["content"][2].$bytes[1]["content"][3].$bytes[1]["content"][4].$bytes[1]["content"][5].$bytes[1]["content"][6].$bytes[1]["content"][7];
14939
		$payloadLengthBit=$bytes[1]["content"][1].$bytes[1]["content"][2].$bytes[1]["content"][3].$bytes[1]["content"][4].$bytes[1]["content"][5].$bytes[1]["content"][6].$bytes[1]["content"][7];
14940
				
14940
 
14941
		#length from 0~127(0~125)
14941
		#length from 0~127(0~125)
14942
		$payloadLength=$bytes[1]["content"][7]*pow(2,0)+$bytes[1]["content"][6]*pow(2,1)+$bytes[1]["content"][5]*pow(2,2)+$bytes[1]["content"][4]*pow(2,3)+$bytes[1]["content"][3]*pow(2,4)+$bytes[1]["content"][2]*pow(2,5)+$bytes[1]["content"][1]*pow(2,6);
14942
		$payloadLength=$bytes[1]["content"][7]*pow(2,0)+$bytes[1]["content"][6]*pow(2,1)+$bytes[1]["content"][5]*pow(2,2)+$bytes[1]["content"][4]*pow(2,3)+$bytes[1]["content"][3]*pow(2,4)+$bytes[1]["content"][2]*pow(2,5)+$bytes[1]["content"][1]*pow(2,6);
14943
		
14943
 
14944
		#如果長度小於 126
14944
		#如果長度小於 126
14945
		if($payloadLength<126){
14945
		if($payloadLength<126){
14946
		
14946
 
14947
			#mask bit start bytes index 
14947
			#mask bit start bytes index
14948
			$maskBitStartBytesIndex=2;
14948
			$maskBitStartBytesIndex=2;
14949
			
14949
 
14950
			#如果要debug
14950
			#如果要debug
14951
			if($conf["debug"]==="true"){
14951
			if($conf["debug"]==="true"){
14952
			
14952
 
14953
				#debug
14953
				#debug
14954
				echo __LINE__." mask bit start bytes index = ".$maskBitStartBytesIndex.PHP_EOL;
14954
				echo __LINE__." mask bit start bytes index = ".$maskBitStartBytesIndex.PHP_EOL;
14955
		
14955
 
14956
				}#if end
14956
				}#if end
14957
		
14957
 
14958
			}#if end
14958
			}#if end
14959
			
14959
 
14960
		#如果長度是 126
14960
		#如果長度是 126
14961
		if($payloadLength===126){
14961
		if($payloadLength===126){
14962
					
14962
 
14963
			$payloadLengthBit=$bytes[2]["content"][0].$bytes[2]["content"][1].$bytes[2]["content"][2].$bytes[2]["content"][3].$bytes[2]["content"][4].$bytes[2]["content"][5].$bytes[2]["content"][6].$bytes[2]["content"][7].$bytes[3]["content"][0].$bytes[3]["content"][1].$bytes[3]["content"][2].$bytes[3]["content"][3].$bytes[3]["content"][4].$bytes[3]["content"][5].$bytes[3]["content"][6].$bytes[3]["content"][7];
14963
			$payloadLengthBit=$bytes[2]["content"][0].$bytes[2]["content"][1].$bytes[2]["content"][2].$bytes[2]["content"][3].$bytes[2]["content"][4].$bytes[2]["content"][5].$bytes[2]["content"][6].$bytes[2]["content"][7].$bytes[3]["content"][0].$bytes[3]["content"][1].$bytes[3]["content"][2].$bytes[3]["content"][3].$bytes[3]["content"][4].$bytes[3]["content"][5].$bytes[3]["content"][6].$bytes[3]["content"][7];
14964
		
14964
 
14965
			#初始化 payload 長度
14965
			#初始化 payload 長度
14966
			$payloadLength=0;
14966
			$payloadLength=0;
14967
			
14967
 
14968
			#2次迴圈,兩個bytes
14968
			#2次迴圈,兩個bytes
14969
			for($i=0;$i<2;$i++){
14969
			for($i=0;$i<2;$i++){
14970
			
14970
 
14971
				#8次迴圈,1個byte等於8個bit
14971
				#8次迴圈,1個byte等於8個bit
14972
				for($j=0;$j<8;$j++){
14972
				for($j=0;$j<8;$j++){
14973
			
14973
 
14974
					#次方數
14974
					#次方數
14975
					$k=$j+8*$i;
14975
					$k=$j+8*$i;
14976
					
14976
 
14977
					#加總 payload 長度
14977
					#加總 payload 長度
14978
					$payloadLength=$payloadLength+$bytes[3-$i]["content"][7-$j]*pow(2,$k);
14978
					$payloadLength=$payloadLength+$bytes[3-$i]["content"][7-$j]*pow(2,$k);
14979
			
14979
 
14980
					}#for end
14980
					}#for end
14981
			
14981
 
14982
				}#for end
14982
				}#for end
14983
		
14983
 
14984
			#mask bit start bytes index 
14984
			#mask bit start bytes index
14985
			$maskBitStartBytesIndex=4;
14985
			$maskBitStartBytesIndex=4;
14986
			
14986
 
14987
			#如果要debug
14987
			#如果要debug
14988
			if($conf["debug"]==="true"){
14988
			if($conf["debug"]==="true"){
14989
			
14989
 
14990
				#debug
14990
				#debug
14991
				echo __LINE__." mask bit start bytes index = ".$maskBitStartBytesIndex.PHP_EOL;
14991
				echo __LINE__." mask bit start bytes index = ".$maskBitStartBytesIndex.PHP_EOL;
14992
		
14992
 
14993
				}#if end
14993
				}#if end
14994
		
14994
 
14995
			}#if end
14995
			}#if end
14996
		
14996
 
14997
		#如果長度是 127	
14997
		#如果長度是 127
14998
		else if($payloadLength===127){
14998
		else if($payloadLength===127){
14999
		
14999
 
15000
			$payloadLengthBit="";
15000
			$payloadLengthBit="";
15001
			
15001
 
15002
			#四次迴圈(4個bytes)
15002
			#四次迴圈(4個bytes)
15003
			for($i=0;$i<4;$i++){
15003
			for($i=0;$i<4;$i++){
15004
			
15004
 
15005
				#8次迴圈,1個byte等於8個bit
15005
				#8次迴圈,1個byte等於8個bit
15006
				for($j=0;$j<8;$j++){
15006
				for($j=0;$j<8;$j++){
15007
			
15007
 
15008
					$payloadLengthBit=$payloadLengthBit.$bytes[4+$i]["content"][0+$j];
15008
					$payloadLengthBit=$payloadLengthBit.$bytes[4+$i]["content"][0+$j];
15009
					
15009
 
15010
					}#for end
15010
					}#for end
15011
					
15011
 
15012
				}#for end
15012
				}#for end
15013
			
15013
 
15014
			#初始化 payload 長度
15014
			#初始化 payload 長度
15015
			$payloadLength=0;
15015
			$payloadLength=0;
15016
			
15016
 
15017
			#四次迴圈(4個bytes)
15017
			#四次迴圈(4個bytes)
15018
			for($i=0;$i<4;$i++){
15018
			for($i=0;$i<4;$i++){
15019
			
15019
 
15020
				#8次迴圈,1個byte等於8個bit
15020
				#8次迴圈,1個byte等於8個bit
15021
				for($j=0;$j<8;$j++){
15021
				for($j=0;$j<8;$j++){
15022
			
15022
 
15023
					#次方數
15023
					#次方數
15024
					$k=$j+8*$i;
15024
					$k=$j+8*$i;
15025
					
15025
 
15026
					$payloadLength=$payloadLength+$bytes[7-$i]["content"][7-$j]*pow(2,$k);
15026
					$payloadLength=$payloadLength+$bytes[7-$i]["content"][7-$j]*pow(2,$k);
15027
					
15027
 
15028
					}#for end
15028
					}#for end
15029
					
15029
 
15030
				}#for end
15030
				}#for end
15031
				
15031
 
15032
			#mask bit start bytes index 
15032
			#mask bit start bytes index
15033
			$maskBitStartBytesIndex=8;
15033
			$maskBitStartBytesIndex=8;
15034
			
15034
 
15035
			#如果要debug
15035
			#如果要debug
15036
			if($conf["debug"]==="true"){
15036
			if($conf["debug"]==="true"){
15037
			
15037
 
15038
				#debug
15038
				#debug
15039
				echo __LINE__." mask bit start bytes index = ".$maskBitStartBytesIndex.PHP_EOL;
15039
				echo __LINE__." mask bit start bytes index = ".$maskBitStartBytesIndex.PHP_EOL;
15040
			
15040
 
15041
				}#if end
15041
				}#if end
15042
			
15042
 
15043
			}#if end	
15043
			}#if end
15044
		
15044
 
15045
		#如果要debug
15045
		#如果要debug
15046
		if($conf["debug"]==="true"){
15046
		if($conf["debug"]==="true"){
15047
		
15047
 
15048
			#debug
15048
			#debug
15049
			var_dump(__LINE__,$bytes);
15049
			var_dump(__LINE__,$bytes);
15050
		
15050
 
15051
			#debug 提示payload的長度的2進位表示法
15051
			#debug 提示payload的長度的2進位表示法
15052
			echo __LINE__." payload data payloadLengthBit is ".$payloadLengthBit.PHP_EOL;	
15052
			echo __LINE__." payload data payloadLengthBit is ".$payloadLengthBit.PHP_EOL;
15053
		
15053
 
15054
			#debug 提示payload的長度
15054
			#debug 提示payload的長度
15055
			echo __LINE__." payload data length is ".$payloadLength." bytes".PHP_EOL;
15055
			echo __LINE__." payload data length is ".$payloadLength." bytes".PHP_EOL;
15056
		
15056
 
15057
			}#if end
15057
			}#if end
15058
		
15058
 
15059
		#如果有mask
15059
		#如果有mask
15060
		if($masked){
15060
		if($masked){
15061
		
15061
 
15062
			#組合 maskBit 字串
15062
			#組合 maskBit 字串
15063
			$maskBitStr=
15063
			$maskBitStr=
15064
				chr(base_convert($bytes[$maskBitStartBytesIndex]["content"],2,10)).
15064
				chr(base_convert($bytes[$maskBitStartBytesIndex]["content"],2,10)).
15065
				chr(base_convert($bytes[$maskBitStartBytesIndex+1]["content"],2,10)).
15065
				chr(base_convert($bytes[$maskBitStartBytesIndex+1]["content"],2,10)).
15066
				chr(base_convert($bytes[$maskBitStartBytesIndex+2]["content"],2,10)).
15066
				chr(base_convert($bytes[$maskBitStartBytesIndex+2]["content"],2,10)).
15067
				chr(base_convert($bytes[$maskBitStartBytesIndex+3]["content"],2,10));
15067
				chr(base_convert($bytes[$maskBitStartBytesIndex+3]["content"],2,10));
15068
			
15068
 
15069
			#如果要debug
15069
			#如果要debug
15070
			if($conf["debug"]==="true"){
15070
			if($conf["debug"]==="true"){
15071
			
15071
 
15072
				#debug 提示 mask bit 字串
15072
				#debug 提示 mask bit 字串
15073
				echo __LINE__." maskBit str is ".$maskBitStr.PHP_EOL;
15073
				echo __LINE__." maskBit str is ".$maskBitStr.PHP_EOL;
15074
			
15074
 
15075
				}#if end
15075
				}#if end
15076
			
15076
 
15077
			}#if end
15077
			}#if end
15078
			
15078
 
15079
		#初始化 payload data 字串
15079
		#初始化 payload data 字串
15080
		$payloadDataStr="";
15080
		$payloadDataStr="";
15081
		
15081
 
15082
		#如果有mask
15082
		#如果有mask
15083
		if($masked){
15083
		if($masked){
15084
		
15084
 
15085
			#有多長就跑幾次
15085
			#有多長就跑幾次
15086
			for($i=0;$i<$payloadLength;$i++){
15086
			for($i=0;$i<$payloadLength;$i++){
15087
			
15087
 
15088
				#串接 masked 的 payload data.
15088
				#串接 masked 的 payload data.
15089
				$payloadDataStr=$payloadDataStr.chr(base_convert($bytes[$maskBitStartBytesIndex+4+$i]["content"],2,10));
15089
				$payloadDataStr=$payloadDataStr.chr(base_convert($bytes[$maskBitStartBytesIndex+4+$i]["content"],2,10));
15090
			
15090
 
15091
				}#for end
15091
				}#for end
15092
		
15092
 
15093
			#如果要debug
15093
			#如果要debug
15094
			if($conf["debug"]==="true"){
15094
			if($conf["debug"]==="true"){
15095
		
15095
 
15096
				#debug 提示 mask bit 字串
15096
				#debug 提示 mask bit 字串
15097
				echo __LINE__." masked payload data str is ".$payloadDataStr.PHP_EOL;
15097
				echo __LINE__." masked payload data str is ".$payloadDataStr.PHP_EOL;
15098
			
15098
 
15099
				}#if end
15099
				}#if end
15100
			
15100
 
15101
			#unmask payload data
15101
			#unmask payload data
15102
			for($i=0;$i<$payloadLength;$i++){
15102
			for($i=0;$i<$payloadLength;$i++){
15103
			
15103
 
15104
				$payloadDataStr[$i]=$payloadDataStr[$i]^$maskBitStr[$i%4];
15104
				$payloadDataStr[$i]=$payloadDataStr[$i]^$maskBitStr[$i%4];
15105
				
15105
 
15106
				}#for end
15106
				}#for end
15107
				
15107
 
15108
			}#if end
15108
			}#if end
15109
		
15109
 
15110
		#反之
15110
		#反之
15111
		else{
15111
		else{
15112
		
15112
 
15113
			#有多長就跑幾次
15113
			#有多長就跑幾次
15114
			for($i=0;$i<$payloadLength;$i++){
15114
			for($i=0;$i<$payloadLength;$i++){
15115
			
15115
 
15116
				#串接 masked 的 payload data.
15116
				#串接 masked 的 payload data.
15117
				$payloadDataStr=$payloadDataStr.chr(base_convert($bytes[$maskBitStartBytesIndex+$i]["content"],2,10));
15117
				$payloadDataStr=$payloadDataStr.chr(base_convert($bytes[$maskBitStartBytesIndex+$i]["content"],2,10));
15118
			
15118
 
15119
				}#for end
15119
				}#for end
15120
		
15120
 
15121
			}#else end
15121
			}#else end
15122
			
15122
 
15123
		#取得 umask 後的內容
15123
		#取得 umask 後的內容
15124
		$result["content"]=$payloadDataStr;
15124
		$result["content"]=$payloadDataStr;
15125
		
15125
 
15126
		#設置執行正常
15126
		#設置執行正常
15127
		$result["status"]="true";
15127
		$result["status"]="true";
15128
		
15128
 
15129
		#回傳結果	
15129
		#回傳結果
15130
		return $result;
15130
		return $result;
15131
		
15131
 
15132
		}#function unmask end
15132
		}#function unmask end
15133
 
15133
 
15134
	/*
15134
	/*
15135
	#函式說明:
15135
	#函式說明:
15136
	#加密 handshake 後要傳送的 webSocket 訊息 
15136
	#加密 handshake 後要傳送的 webSocket 訊息
15137
	#回傳結果:
15137
	#回傳結果:
15138
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
15138
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
15139
	#$result["error"],執行不正常結束的錯訊息陣列.
15139
	#$result["error"],執行不正常結束的錯訊息陣列.
15140
	#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
15140
	#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
15141
	#$result["argu"],陣列,使用的參數.
15141
	#$result["argu"],陣列,使用的參數.
15142
	#必填參數:
15142
	#必填參數:
15143
	#$conf["text"],字串,要加密的訊息.
15143
	#$conf["text"],字串,要加密的訊息.
15144
	$conf["text"]=""; 
15144
	$conf["text"]="";
15145
	#可省略參數:
15145
	#可省略參數:
15146
	#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
15146
	#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
15147
	#$conf["payloadIsBin"]="false";
15147
	#$conf["payloadIsBin"]="false";
15148
	#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
15148
	#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
15149
	#$conf["ping"]="false";
15149
	#$conf["ping"]="false";
Line 15159... Line 15159...
15159
	#https://www.rfc-editor.org/rfc/rfc6455#page-28, Web Socket Base Framing Protocol.
15159
	#https://www.rfc-editor.org/rfc/rfc6455#page-28, Web Socket Base Framing Protocol.
15160
	#備註:
15160
	#備註:
15161
	#無.
15161
	#無.
15162
	*/
15162
	*/
15163
	public static function encode(&$conf){
15163
	public static function encode(&$conf){
15164
		
15164
 
15165
		#初始化要回傳的結果
15165
		#初始化要回傳的結果
15166
		$result=array();
15166
		$result=array();
15167
 
15167
 
15168
		#設置當其函數名稱
15168
		#設置當其函數名稱
15169
		$result["function"]=__FUNCTION__;
15169
		$result["function"]=__FUNCTION__;
15170
		
15170
 
15171
		#初始化分割好的 web socket 封包
15171
		#初始化分割好的 web socket 封包
15172
		$result["content"]=array();
15172
		$result["content"]=array();
15173
 
15173
 
15174
		#如果 $conf 不為陣列
15174
		#如果 $conf 不為陣列
15175
		if(gettype($conf)!="array"){
15175
		if(gettype($conf)!="array"){
Line 15185... Line 15185...
15185
 
15185
 
15186
				#設置執行錯誤訊息
15186
				#設置執行錯誤訊息
15187
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
15187
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
15188
 
15188
 
15189
				}#if end
15189
				}#if end
15190
				
15190
 
15191
			}#if end
15191
			}#if end
15192
		
15192
 
15193
		#設置放置的參數
15193
		#設置放置的參數
15194
		$result["argu"]=$conf;
15194
		$result["argu"]=$conf;
15195
		
15195
 
15196
		#函式說明:
15196
		#函式說明:
15197
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
15197
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
15198
		#回傳結果:
15198
		#回傳結果:
15199
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
15199
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
15200
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
15200
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
Line 15243... Line 15243...
15243
		#array_keys=>http://php.net/manual/en/function.array-keys.php
15243
		#array_keys=>http://php.net/manual/en/function.array-keys.php
15244
		#備註:
15244
		#備註:
15245
		#無.
15245
		#無.
15246
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
15246
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
15247
		unset($conf["variableCheck::checkArguments"]);
15247
		unset($conf["variableCheck::checkArguments"]);
15248
		
15248
 
15249
		#如果執行失敗
15249
		#如果執行失敗
15250
		if($checkArguments["status"]==="false"){
15250
		if($checkArguments["status"]==="false"){
15251
		
15251
 
15252
			#設置執行失敗
15252
			#設置執行失敗
15253
			$result["status"]="false";
15253
			$result["status"]="false";
15254
 
15254
 
15255
			#設置執行錯誤訊息
15255
			#設置執行錯誤訊息
15256
			$result["error"]=$checkArguments;
15256
			$result["error"]=$checkArguments;
15257
			
15257
 
15258
			#回傳結果
15258
			#回傳結果
15259
			return $result;
15259
			return $result;
15260
		
15260
 
15261
			}#if end
15261
			}#if end
15262
		
15262
 
15263
		#如果參數檢查不通過
15263
		#如果參數檢查不通過
15264
		if($checkArguments["passed"]==="false"){
15264
		if($checkArguments["passed"]==="false"){
15265
		
15265
 
15266
			#設置執行失敗
15266
			#設置執行失敗
15267
			$result["status"]="false";
15267
			$result["status"]="false";
15268
 
15268
 
15269
			#設置執行錯誤訊息
15269
			#設置執行錯誤訊息
15270
			$result["error"]=$checkArguments;
15270
			$result["error"]=$checkArguments;
15271
			
15271
 
15272
			#回傳結果
15272
			#回傳結果
15273
			return $result;
15273
			return $result;
15274
		
15274
 
15275
			}#if end
15275
			}#if end
15276
		
15276
 
15277
		#如果是要產生 ping/pong 的訊息
15277
		#如果是要產生 ping/pong 的訊息
15278
		if($conf["ping"]==="true" || $conf["pong"]==="true"){
15278
		if($conf["ping"]==="true" || $conf["pong"]==="true"){
15279
		
15279
 
15280
			#強制將payload設置為空
15280
			#強制將payload設置為空
15281
			$conf["text"]="";
15281
			$conf["text"]="";
15282
		
15282
 
15283
			}#if end
15283
			}#if end
15284
		
15284
 
15285
		#取得payload
15285
		#取得payload
15286
		$text=$conf["text"];
15286
		$text=$conf["text"];
15287
		
15287
 
15288
		#取得 payload length
15288
		#取得 payload length
15289
		$length=strlen($text);
15289
		$length=strlen($text);
15290
		
15290
 
15291
		#如果沒有payload
15291
		#如果沒有payload
15292
		if($length===0){
15292
		if($length===0){
15293
		
15293
 
15294
			#初始化第一個 frame 為 最後一個 frame.
15294
			#初始化第一個 frame 為 最後一個 frame.
15295
			$header="1000";
15295
			$header="1000";
15296
	
15296
 
15297
			#如果是ping
15297
			#如果是ping
15298
			if($conf["ping"]==="true"){
15298
			if($conf["ping"]==="true"){
15299
			
15299
 
15300
				#其訊息類別為 ping
15300
				#其訊息類別為 ping
15301
				$header=$header."1001";
15301
				$header=$header."1001";
15302
			
15302
 
15303
				}#if end
15303
				}#if end
15304
				
15304
 
15305
			#如果是pong
15305
			#如果是pong
15306
			else if($conf["pong"]==="true"){
15306
			else if($conf["pong"]==="true"){
15307
			
15307
 
15308
				#其訊息類別為 pong
15308
				#其訊息類別為 pong
15309
				$header=$header."1010";
15309
				$header=$header."1010";
15310
			
15310
 
15311
				}#if end
15311
				}#if end
15312
	
15312
 
15313
			#例外狀況
15313
			#例外狀況
15314
			else{
15314
			else{
15315
			
15315
 
15316
				#設置執行失敗
15316
				#設置執行失敗
15317
				$result["status"]="false";
15317
				$result["status"]="false";
15318
 
15318
 
15319
				#設置執行錯誤訊息
15319
				#設置執行錯誤訊息
15320
				$result["error"][]="參數 text 為空時,需要 ping or pong 參數擇一";
15320
				$result["error"][]="參數 text 為空時,需要 ping or pong 參數擇一";
15321
 
15321
 
15322
				#設置執行錯誤訊息
15322
				#設置執行錯誤訊息
15323
				$result["error"][]=$checkArguments;
15323
				$result["error"][]=$checkArguments;
15324
				
15324
 
15325
				#回傳結果
15325
				#回傳結果
15326
				return $result;
15326
				return $result;
15327
			
15327
 
15328
				}#else end
15328
				}#else end
15329
	
15329
 
15330
			}#if end
15330
			}#if end
15331
			
15331
 
15332
		#反之有payload
15332
		#反之有payload
15333
		else{
15333
		else{
15334
		
15334
 
15335
			#初始化第一個 frame 為 最後一個 frame.
15335
			#初始化第一個 frame 為 最後一個 frame.
15336
			$header="1000";
15336
			$header="1000";
15337
			
15337
 
15338
			#如果 payload 是 2元碼
15338
			#如果 payload 是 2元碼
15339
			if($conf["payloadIsBin"]==="true"){
15339
			if($conf["payloadIsBin"]==="true"){
15340
			
15340
 
15341
				#設置 opecode 為 0010
15341
				#設置 opecode 為 0010
15342
				$header=$header."0010";
15342
				$header=$header."0010";
15343
			
15343
 
15344
				}#if end
15344
				}#if end
15345
			
15345
 
15346
			#反之為 text
15346
			#反之為 text
15347
			else{
15347
			else{
15348
			
15348
 
15349
				#設置 opecode 為 0001
15349
				#設置 opecode 為 0001
15350
				$header=$header."0001";
15350
				$header=$header."0001";
15351
			
15351
 
15352
				}#else end
15352
				}#else end
15353
		
15353
 
15354
			}#else end
15354
			}#else end
15355
		
15355
 
15356
		#如果 payload 長度大於 125
15356
		#如果 payload 長度大於 125
15357
		while($length > 125){
15357
		while($length > 125){
15358
		
15358
 
15359
			#初始化第一個 frame 為 不為最後一個 frame, 
15359
			#初始化第一個 frame 為 不為最後一個 frame,
15360
			$header="0000";
15360
			$header="0000";
15361
			
15361
 
15362
			#如果 payload 是 2元碼
15362
			#如果 payload 是 2元碼
15363
			if($conf["payloadIsBin"]==="true"){
15363
			if($conf["payloadIsBin"]==="true"){
15364
			
15364
 
15365
				#設置 opecode 為 0010
15365
				#設置 opecode 為 0010
15366
				$header=$header."0010";
15366
				$header=$header."0010";
15367
			
15367
 
15368
				}#if end
15368
				}#if end
15369
			
15369
 
15370
			#反之為 text
15370
			#反之為 text
15371
			else{
15371
			else{
15372
			
15372
 
15373
				#設置 opecode 為 0001
15373
				#設置 opecode 為 0001
15374
				$header=$header."0001";
15374
				$header=$header."0001";
15375
			
15375
 
15376
				}#else end
15376
				}#else end
15377
			
15377
 
15378
			#如果已經有前面的內容了
15378
			#如果已經有前面的內容了
15379
			if(count($result["content"])>0){
15379
			if(count($result["content"])>0){
15380
				
15380
 
15381
				#設置為中間的 frame, 其 opcode 為 continue data.
15381
				#設置為中間的 frame, 其 opcode 為 continue data.
15382
				$header="00000000";
15382
				$header="00000000";
15383
				
15383
 
15384
				}#if end
15384
				}#if end
15385
			
15385
 
15386
			#取得125bytes
15386
			#取得125bytes
15387
			$body=substr($text,0,125);
15387
			$body=substr($text,0,125);
15388
			
15388
 
15389
			#函式說明:
15389
			#函式說明:
15390
			#將2進位的8位數字字串,變成bytes字串.
15390
			#將2進位的8位數字字串,變成bytes字串.
15391
			#回傳結果:
15391
			#回傳結果:
15392
			#$result["status"],執行正常與否,"false"代表不正常,"true"代表正常.
15392
			#$result["status"],執行正常與否,"false"代表不正常,"true"代表正常.
15393
			#$result["function"],當前執行的函式內容.
15393
			#$result["function"],當前執行的函式內容.
Line 15402... Line 15402...
15402
			#無.
15402
			#無.
15403
			#備註:
15403
			#備註:
15404
			#無.
15404
			#無.
15405
			$bit2byteStr=stringProcess::bit2byteStr($conf["stringProcess::bit2byteStr"]);
15405
			$bit2byteStr=stringProcess::bit2byteStr($conf["stringProcess::bit2byteStr"]);
15406
			unset($conf["stringProcess::bit2byteStr"]);
15406
			unset($conf["stringProcess::bit2byteStr"]);
15407
			
15407
 
15408
			#如果執行失敗
15408
			#如果執行失敗
15409
			if($bit2byteStr["status"]==="false"){
15409
			if($bit2byteStr["status"]==="false"){
15410
			
15410
 
15411
				#設置執行失敗
15411
				#設置執行失敗
15412
				$result["status"]="false";
15412
				$result["status"]="false";
15413
 
15413
 
15414
				#設置執行錯誤訊息
15414
				#設置執行錯誤訊息
15415
				$result["error"]=$bit2byteStr;
15415
				$result["error"]=$bit2byteStr;
15416
				
15416
 
15417
				#回傳結果
15417
				#回傳結果
15418
				return $result;
15418
				return $result;
15419
			
15419
 
15420
				}#if end
15420
				}#if end
15421
			
15421
 
15422
			#取得該 byte 字串
15422
			#取得該 byte 字串
15423
			$headerInByte=$bit2byteStr["content"];
15423
			$headerInByte=$bit2byteStr["content"];
15424
			
15424
 
15425
			#預設不用mask
15425
			#預設不用mask
15426
			$maskBitAndLengthIntString=(string)(125);
15426
			$maskBitAndLengthIntString=(string)(125);
15427
			
15427
 
15428
			#如果需要mask
15428
			#如果需要mask
15429
			if($conf["mask"]==="true"){
15429
			if($conf["mask"]==="true"){
15430
			
15430
 
15431
				$maskBitAndLengthIntString=(string)(125+128);
15431
				$maskBitAndLengthIntString=(string)(125+128);
15432
			
15432
 
15433
				}#if end
15433
				}#if end
15434
			
15434
 
15435
			#函式說明:
15435
			#函式說明:
15436
			#將2進位的8位數字字串,變成bytes字串.
15436
			#將2進位的8位數字字串,變成bytes字串.
15437
			#回傳結果:
15437
			#回傳結果:
15438
			#$result["status"],執行正常與否,"false"代表不正常,"true"代表正常.
15438
			#$result["status"],執行正常與否,"false"代表不正常,"true"代表正常.
15439
			#$result["function"],當前執行的函式內容.
15439
			#$result["function"],當前執行的函式內容.
Line 15448... Line 15448...
15448
			#無.
15448
			#無.
15449
			#備註:
15449
			#備註:
15450
			#無.
15450
			#無.
15451
			$bit2byteStr=stringProcess::bit2byteStr($conf["stringProcess::bit2byteStr"]);
15451
			$bit2byteStr=stringProcess::bit2byteStr($conf["stringProcess::bit2byteStr"]);
15452
			unset($conf["stringProcess::bit2byteStr"]);
15452
			unset($conf["stringProcess::bit2byteStr"]);
15453
			
15453
 
15454
			#如果要 debug
15454
			#如果要 debug
15455
			if($conf["debug"]==="true"){
15455
			if($conf["debug"]==="true"){
15456
			
15456
 
15457
				#debug
15457
				#debug
15458
				var_dump(__LINE__,$bit2byteStr);
15458
				var_dump(__LINE__,$bit2byteStr);
15459
			
15459
 
15460
				}#if end
15460
				}#if end
15461
			
15461
 
15462
			#如果執行失敗
15462
			#如果執行失敗
15463
			if($bit2byteStr["status"]==="false"){
15463
			if($bit2byteStr["status"]==="false"){
15464
			
15464
 
15465
				#設置執行失敗
15465
				#設置執行失敗
15466
				$result["status"]="false";
15466
				$result["status"]="false";
15467
 
15467
 
15468
				#設置執行錯誤訊息
15468
				#設置執行錯誤訊息
15469
				$result["error"]=$bit2byteStr;
15469
				$result["error"]=$bit2byteStr;
15470
				
15470
 
15471
				#回傳結果
15471
				#回傳結果
15472
				return $result;
15472
				return $result;
15473
			
15473
 
15474
				}#if end
15474
				}#if end
15475
			
15475
 
15476
			#取得該 byte 字串
15476
			#取得該 byte 字串
15477
			$lengthInByte=$bit2byteStr["content"];
15477
			$lengthInByte=$bit2byteStr["content"];
15478
			
15478
 
15479
			#初始化 4 bytes mask key.
15479
			#初始化 4 bytes mask key.
15480
			$maskKey4bytes="";
15480
			$maskKey4bytes="";
15481
			
15481
 
15482
			#如果需要mask
15482
			#如果需要mask
15483
			if($conf["mask"]==="true"){
15483
			if($conf["mask"]==="true"){
15484
			
15484
 
15485
				#函式說明:
15485
				#函式說明:
15486
				#隨機產生任意byte(s)組成的字串.
15486
				#隨機產生任意byte(s)組成的字串.
15487
				#回傳結果:
15487
				#回傳結果:
15488
				#$result["status"],執行正常與否,"false"代表不正常,"true"代表正常.
15488
				#$result["status"],執行正常與否,"false"代表不正常,"true"代表正常.
15489
				#$result["function"],當前執行的函式內容.
15489
				#$result["function"],當前執行的函式內容.
Line 15498... Line 15498...
15498
				#無.
15498
				#無.
15499
				#備註:
15499
				#備註:
15500
				#無.
15500
				#無.
15501
				$randomByte=stringProcess::randomByte($conf["stringProcess::randomByte"]);
15501
				$randomByte=stringProcess::randomByte($conf["stringProcess::randomByte"]);
15502
				unset($conf["stringProcess::randomByte"]);
15502
				unset($conf["stringProcess::randomByte"]);
15503
			
15503
 
15504
				#如果執行失敗
15504
				#如果執行失敗
15505
				if($randomByte["status"]==="false"){
15505
				if($randomByte["status"]==="false"){
15506
				
15506
 
15507
					#設置執行失敗
15507
					#設置執行失敗
15508
					$result["status"]="false";
15508
					$result["status"]="false";
15509
 
15509
 
15510
					#設置執行錯誤訊息
15510
					#設置執行錯誤訊息
15511
					$result["error"]=$randomByte;
15511
					$result["error"]=$randomByte;
15512
					
15512
 
15513
					#回傳結果
15513
					#回傳結果
15514
					return $result;
15514
					return $result;
15515
				
15515
 
15516
					}#if end
15516
					}#if end
15517
			
15517
 
15518
				#取得隨機產生的 mask key
15518
				#取得隨機產生的 mask key
15519
				$maskKey4bytes=$randomByte["content"];
15519
				$maskKey4bytes=$randomByte["content"];
15520
			
15520
 
15521
				#函式說明:
15521
				#函式說明:
15522
				#將 handshake 後含有 mask bit 的 web socket 訊息進行  mask/unmask.
15522
				#將 handshake 後含有 mask bit 的 web socket 訊息進行  mask/unmask.
15523
				#回傳結果:
15523
				#回傳結果:
15524
				#$result["function"],當前函式的名稱.
15524
				#$result["function"],當前函式的名稱.
15525
				#$result["argu"],使用的參數.
15525
				#$result["argu"],使用的參數.
Line 15527... Line 15527...
15527
				#$result["content"],unmask後的內容.
15527
				#$result["content"],unmask後的內容.
15528
				#$result["type"],訊息的類型,"unknow":代表未定義;"text":代表為文字訊息;"pong":代表為pong;"invalid":代表不為web socket frame.
15528
				#$result["type"],訊息的類型,"unknow":代表未定義;"text":代表為文字訊息;"pong":代表為pong;"invalid":代表不為web socket frame.
15529
				#$result["error"],錯誤訊息陣列.
15529
				#$result["error"],錯誤訊息陣列.
15530
				#必填參數:
15530
				#必填參數:
15531
				#$conf["payload"],字串,要解密的訊息.
15531
				#$conf["payload"],字串,要解密的訊息.
15532
				$conf["webSock::unmask"]["payload"]=$headerInByte.$lengthInByte.$maskKey4bytes.$body; 
15532
				$conf["webSock::unmask"]["payload"]=$headerInByte.$lengthInByte.$maskKey4bytes.$body;
15533
				#可省略參數:
15533
				#可省略參數:
15534
				#$conf["allowUnmask"],字串,若發現沒有設置mask bit,也要繼續執行,則為"true";反之為"false".
15534
				#$conf["allowUnmask"],字串,若發現沒有設置mask bit,也要繼續執行,則為"true";反之為"false".
15535
				$conf["webSock::unmask"]["allowUnmask"]="true";
15535
				$conf["webSock::unmask"]["allowUnmask"]="true";
15536
				#參考資料
15536
				#參考資料
15537
				#http://www.inanzzz.com/index.php/post/swf8/converting-string-to-binary-and-binary-to-string-with-php
15537
				#http://www.inanzzz.com/index.php/post/swf8/converting-string-to-binary-and-binary-to-string-with-php
Line 15539... Line 15539...
15539
				#https://tools.ietf.org/html/rfc5234
15539
				#https://tools.ietf.org/html/rfc5234
15540
				#備註:
15540
				#備註:
15541
				#無.
15541
				#無.
15542
				$unmask=webSock::unmask($conf["webSock::unmask"]);
15542
				$unmask=webSock::unmask($conf["webSock::unmask"]);
15543
				unset($conf["webSock::unmask"]);
15543
				unset($conf["webSock::unmask"]);
15544
				
15544
 
15545
				#debug
15545
				#debug
15546
				#var_dump(__LINE__,$unmask);
15546
				#var_dump(__LINE__,$unmask);
15547
				
15547
 
15548
				#如果執行失敗
15548
				#如果執行失敗
15549
				if($unmask["status"]==="false"){
15549
				if($unmask["status"]==="false"){
15550
				
15550
 
15551
					#設置執行失敗
15551
					#設置執行失敗
15552
					$result["status"]="false";
15552
					$result["status"]="false";
15553
 
15553
 
15554
					#設置執行錯誤訊息
15554
					#設置執行錯誤訊息
15555
					$result["error"]=$unmask;
15555
					$result["error"]=$unmask;
15556
					
15556
 
15557
					#回傳結果
15557
					#回傳結果
15558
					return $result;
15558
					return $result;
15559
				
15559
 
15560
					}#if end
15560
					}#if end
15561
			
15561
 
15562
				#取得該段的結果
15562
				#取得該段的結果
15563
				$result["content"][]=$headerInByte.$lengthInByte.$maskKey4bytes.$unmask["content"];
15563
				$result["content"][]=$headerInByte.$lengthInByte.$maskKey4bytes.$unmask["content"];
15564
			
15564
 
15565
				}#if end
15565
				}#if end
15566
			
15566
 
15567
			#反之
15567
			#反之
15568
			else{
15568
			else{
15569
			
15569
 
15570
				#取得該段的結果
15570
				#取得該段的結果
15571
				$result["content"][]=$headerInByte.$lengthInByte.$maskKey4bytes.$body;
15571
				$result["content"][]=$headerInByte.$lengthInByte.$maskKey4bytes.$body;
15572
				
15572
 
15573
				}#else end
15573
				}#else end
15574
			
15574
 
15575
			#取得剩下的內容
15575
			#取得剩下的內容
15576
			$text=substr($text,125);
15576
			$text=substr($text,125);
15577
			
15577
 
15578
			#取得 payload length
15578
			#取得 payload length
15579
			$length=strlen($text);
15579
			$length=strlen($text);
15580
			
15580
 
15581
			}#while end
15581
			}#while end
15582
		
15582
 
15583
		#執行到這邊代表剩下的長度用一個 frame 即可表達
15583
		#執行到這邊代表剩下的長度用一個 frame 即可表達
15584
		
15584
 
15585
		#如果已經有內容了
15585
		#如果已經有內容了
15586
		if(count($result["content"])>0){
15586
		if(count($result["content"])>0){
15587
			
15587
 
15588
			#代表不用表示為 text frame 但要表示為最後一個 frame.
15588
			#代表不用表示為 text frame 但要表示為最後一個 frame.
15589
			$header="10000000";
15589
			$header="10000000";
15590
		
15590
 
15591
			}#if end
15591
			}#if end
15592
		
15592
 
15593
		#函式說明:
15593
		#函式說明:
15594
		#將2進位的8位數字字串,變成bytes字串.
15594
		#將2進位的8位數字字串,變成bytes字串.
15595
		#回傳結果:
15595
		#回傳結果:
15596
		#$result["status"],執行正常與否,"false"代表不正常,"true"代表正常.
15596
		#$result["status"],執行正常與否,"false"代表不正常,"true"代表正常.
15597
		#$result["function"],當前執行的函式內容.
15597
		#$result["function"],當前執行的函式內容.
Line 15606... Line 15606...
15606
		#無.
15606
		#無.
15607
		#備註:
15607
		#備註:
15608
		#無.
15608
		#無.
15609
		$bit2byteStr=stringProcess::bit2byteStr($conf["stringProcess::bit2byteStr"]);
15609
		$bit2byteStr=stringProcess::bit2byteStr($conf["stringProcess::bit2byteStr"]);
15610
		unset($conf["stringProcess::bit2byteStr"]);
15610
		unset($conf["stringProcess::bit2byteStr"]);
15611
		
15611
 
15612
		#如果執行失敗
15612
		#如果執行失敗
15613
		if($bit2byteStr["status"]==="false"){
15613
		if($bit2byteStr["status"]==="false"){
15614
		
15614
 
15615
			#設置執行失敗
15615
			#設置執行失敗
15616
			$result["status"]="false";
15616
			$result["status"]="false";
15617
 
15617
 
15618
			#設置執行錯誤訊息
15618
			#設置執行錯誤訊息
15619
			$result["error"]=$bit2byteStr;
15619
			$result["error"]=$bit2byteStr;
15620
			
15620
 
15621
			#回傳結果
15621
			#回傳結果
15622
			return $result;
15622
			return $result;
15623
		
15623
 
15624
			}#if end
15624
			}#if end
15625
		
15625
 
15626
		#取得該 byte 字串
15626
		#取得該 byte 字串
15627
		$headerInByte=$bit2byteStr["content"];
15627
		$headerInByte=$bit2byteStr["content"];
15628
		
15628
 
15629
		#如果要 debug
15629
		#如果要 debug
15630
		if($conf["debug"]==="true"){
15630
		if($conf["debug"]==="true"){
15631
		
15631
 
15632
			#debug
15632
			#debug
15633
			var_dump(__LINE__,$length,base_convert($length,10,2));
15633
			var_dump(__LINE__,$length,base_convert($length,10,2));
15634
		
15634
 
15635
			}#if end
15635
			}#if end
15636
		
15636
 
15637
		#如果需要mask
15637
		#如果需要mask
15638
		if($conf["mask"]==="true"){
15638
		if($conf["mask"]==="true"){
15639
		
15639
 
15640
			#增加maskbit
15640
			#增加maskbit
15641
			$length=$length+128;
15641
			$length=$length+128;
15642
		
15642
 
15643
			}#if end
15643
			}#if end
15644
		
15644
 
15645
		#函式說明:
15645
		#函式說明:
15646
		#將2進位的8位數字字串,變成bytes字串.
15646
		#將2進位的8位數字字串,變成bytes字串.
15647
		#回傳結果:
15647
		#回傳結果:
15648
		#$result["status"],執行正常與否,"false"代表不正常,"true"代表正常.
15648
		#$result["status"],執行正常與否,"false"代表不正常,"true"代表正常.
15649
		#$result["function"],當前執行的函式內容.
15649
		#$result["function"],當前執行的函式內容.
Line 15658... Line 15658...
15658
		#無.
15658
		#無.
15659
		#備註:
15659
		#備註:
15660
		#無.
15660
		#無.
15661
		$bit2byteStr=stringProcess::bit2byteStr($conf["stringProcess::bit2byteStr"]);
15661
		$bit2byteStr=stringProcess::bit2byteStr($conf["stringProcess::bit2byteStr"]);
15662
		unset($conf["stringProcess::bit2byteStr"]);
15662
		unset($conf["stringProcess::bit2byteStr"]);
15663
		
15663
 
15664
		#如果要 debug
15664
		#如果要 debug
15665
		if($conf["debug"]==="true"){
15665
		if($conf["debug"]==="true"){
15666
		
15666
 
15667
			#debug
15667
			#debug
15668
			var_dump(__LINE__,$bit2byteStr);
15668
			var_dump(__LINE__,$bit2byteStr);
15669
		
15669
 
15670
			}#if end
15670
			}#if end
15671
		
15671
 
15672
		#如果執行失敗
15672
		#如果執行失敗
15673
		if($bit2byteStr["status"]==="false"){
15673
		if($bit2byteStr["status"]==="false"){
15674
		
15674
 
15675
			#設置執行失敗
15675
			#設置執行失敗
15676
			$result["status"]="false";
15676
			$result["status"]="false";
15677
 
15677
 
15678
			#設置執行錯誤訊息
15678
			#設置執行錯誤訊息
15679
			$result["error"]=$bit2byteStr;
15679
			$result["error"]=$bit2byteStr;
15680
			
15680
 
15681
			#回傳結果
15681
			#回傳結果
15682
			return $result;
15682
			return $result;
15683
		
15683
 
15684
			}#if end
15684
			}#if end
15685
		
15685
 
15686
		#取得該 byte 字串
15686
		#取得該 byte 字串
15687
		$lengthInByte=$bit2byteStr["content"];
15687
		$lengthInByte=$bit2byteStr["content"];
15688
	
15688
 
15689
		#初始化 4 bytes mask key.
15689
		#初始化 4 bytes mask key.
15690
		$maskKey4bytes="";
15690
		$maskKey4bytes="";
15691
		
15691
 
15692
		#如果需要mask
15692
		#如果需要mask
15693
		if($conf["mask"]==="true"){
15693
		if($conf["mask"]==="true"){
15694
		
15694
 
15695
			#函式說明:
15695
			#函式說明:
15696
			#隨機產生任意byte(s)組成的字串.
15696
			#隨機產生任意byte(s)組成的字串.
15697
			#回傳結果:
15697
			#回傳結果:
15698
			#$result["status"],執行正常與否,"false"代表不正常,"true"代表正常.
15698
			#$result["status"],執行正常與否,"false"代表不正常,"true"代表正常.
15699
			#$result["function"],當前執行的函式內容.
15699
			#$result["function"],當前執行的函式內容.
Line 15708... Line 15708...
15708
			#無.
15708
			#無.
15709
			#備註:
15709
			#備註:
15710
			#無.
15710
			#無.
15711
			$randomByte=stringProcess::randomByte($conf["stringProcess::randomByte"]);
15711
			$randomByte=stringProcess::randomByte($conf["stringProcess::randomByte"]);
15712
			unset($conf["stringProcess::randomByte"]);
15712
			unset($conf["stringProcess::randomByte"]);
15713
		
15713
 
15714
			#如果執行失敗
15714
			#如果執行失敗
15715
			if($randomByte["status"]==="false"){
15715
			if($randomByte["status"]==="false"){
15716
			
15716
 
15717
				#設置執行失敗
15717
				#設置執行失敗
15718
				$result["status"]="false";
15718
				$result["status"]="false";
15719
 
15719
 
15720
				#設置執行錯誤訊息
15720
				#設置執行錯誤訊息
15721
				$result["error"]=$randomByte;
15721
				$result["error"]=$randomByte;
15722
				
15722
 
15723
				#回傳結果
15723
				#回傳結果
15724
				return $result;
15724
				return $result;
15725
			
15725
 
15726
				}#if end
15726
				}#if end
15727
		
15727
 
15728
			#取得隨機產生的 mask key
15728
			#取得隨機產生的 mask key
15729
			$maskKey4bytes=$randomByte["content"];
15729
			$maskKey4bytes=$randomByte["content"];
15730
		
15730
 
15731
			}#if end
15731
			}#if end
15732
	
15732
 
15733
		#函式說明:
15733
		#函式說明:
15734
		#將 handshake 後含有 mask bit 的 web socket 訊息進行  mask/unmask.
15734
		#將 handshake 後含有 mask bit 的 web socket 訊息進行  mask/unmask.
15735
		#回傳結果:
15735
		#回傳結果:
15736
		#$result["function"],當前函式的名稱.
15736
		#$result["function"],當前函式的名稱.
15737
		#$result["argu"],使用的參數.
15737
		#$result["argu"],使用的參數.
Line 15739... Line 15739...
15739
		#$result["content"],unmask後的內容.
15739
		#$result["content"],unmask後的內容.
15740
		#$result["type"],訊息的類型,"unknow":代表未定義;"text":代表為文字訊息;"pong":代表為pong;"invalid":代表不為web socket frame.
15740
		#$result["type"],訊息的類型,"unknow":代表未定義;"text":代表為文字訊息;"pong":代表為pong;"invalid":代表不為web socket frame.
15741
		#$result["error"],錯誤訊息陣列.
15741
		#$result["error"],錯誤訊息陣列.
15742
		#必填參數:
15742
		#必填參數:
15743
		#$conf["payload"],字串,要解密的訊息.
15743
		#$conf["payload"],字串,要解密的訊息.
15744
		$conf["webSock::unmask"]["payload"]=$headerInByte.$lengthInByte.$maskKey4bytes.$text; 
15744
		$conf["webSock::unmask"]["payload"]=$headerInByte.$lengthInByte.$maskKey4bytes.$text;
15745
		#可省略參數:
15745
		#可省略參數:
15746
		#$conf["allowUnmask"],字串,若發現沒有設置mask bit,也要繼續執行,則為"true";反之為"false".
15746
		#$conf["allowUnmask"],字串,若發現沒有設置mask bit,也要繼續執行,則為"true";反之為"false".
15747
		$conf["webSock::unmask"]["allowUnmask"]="true";
15747
		$conf["webSock::unmask"]["allowUnmask"]="true";
15748
		#參考資料
15748
		#參考資料
15749
		#http://www.inanzzz.com/index.php/post/swf8/converting-string-to-binary-and-binary-to-string-with-php
15749
		#http://www.inanzzz.com/index.php/post/swf8/converting-string-to-binary-and-binary-to-string-with-php
Line 15751... Line 15751...
15751
		#https://tools.ietf.org/html/rfc5234
15751
		#https://tools.ietf.org/html/rfc5234
15752
		#備註:
15752
		#備註:
15753
		#無.
15753
		#無.
15754
		$unmask=webSock::unmask($conf["webSock::unmask"]);
15754
		$unmask=webSock::unmask($conf["webSock::unmask"]);
15755
		unset($conf["webSock::unmask"]);
15755
		unset($conf["webSock::unmask"]);
15756
		
15756
 
15757
		#如果要 debug
15757
		#如果要 debug
15758
		if($conf["debug"]==="true"){
15758
		if($conf["debug"]==="true"){
15759
		
15759
 
15760
			#debug
15760
			#debug
15761
			var_dump(__LINE__,$unmask);
15761
			var_dump(__LINE__,$unmask);
15762
		
15762
 
15763
			}#if end
15763
			}#if end
15764
		
15764
 
15765
		#如果執行失敗
15765
		#如果執行失敗
15766
		if($unmask["status"]==="false"){
15766
		if($unmask["status"]==="false"){
15767
		
15767
 
15768
			#設置執行失敗
15768
			#設置執行失敗
15769
			$result["status"]="false";
15769
			$result["status"]="false";
15770
 
15770
 
15771
			#設置執行錯誤訊息
15771
			#設置執行錯誤訊息
15772
			$result["error"]=$unmask;
15772
			$result["error"]=$unmask;
15773
			
15773
 
15774
			#回傳結果
15774
			#回傳結果
15775
			return $result;
15775
			return $result;
15776
		
15776
 
15777
			}#if end
15777
			}#if end
15778
		
15778
 
15779
		#儲存訊息
15779
		#儲存訊息
15780
		$result["content"][]=$headerInByte.$lengthInByte.$maskKey4bytes.$unmask["content"];
15780
		$result["content"][]=$headerInByte.$lengthInByte.$maskKey4bytes.$unmask["content"];
15781
		
15781
 
15782
		#設置執行正常
15782
		#設置執行正常
15783
		$result["status"]="true";
15783
		$result["status"]="true";
15784
		
15784
 
15785
		#回傳結果
15785
		#回傳結果
15786
		return $result;
15786
		return $result;
15787
	
15787
 
15788
		}#function encode end
15788
		}#function encode end
15789
		
15789
 
15790
	/*
15790
	/*
15791
	函式說明:
15791
	函式說明:
15792
	提供webSock::nativeSocketTcpIpServer的processFuncs參數所用的函式,可以接受要求傳遞檔案的內容給用戶.
15792
	提供webSock::nativeSocketTcpIpServer的processFuncs參數所用的函式,可以接受要求傳遞檔案的內容給用戶.
15793
	#回傳結果:
15793
	#回傳結果:
15794
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常,"continue"代表不適用.
15794
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常,"continue"代表不適用.
Line 15813... Line 15813...
15813
	#https://www.rfc-editor.org/rfc/rfc6455#page-28, Web Socket Base Framing Protocol.
15813
	#https://www.rfc-editor.org/rfc/rfc6455#page-28, Web Socket Base Framing Protocol.
15814
	#備註:
15814
	#備註:
15815
	#無.
15815
	#無.
15816
	*/
15816
	*/
15817
	public static function getFileContent(&$conf){
15817
	public static function getFileContent(&$conf){
15818
	
15818
 
15819
		#初始化要回傳的結果
15819
		#初始化要回傳的結果
15820
		$result=array();
15820
		$result=array();
15821
 
15821
 
15822
		#取得當前執行的函數名稱
15822
		#取得當前執行的函數名稱
15823
		$result["function"]=__FUNCTION__;
15823
		$result["function"]=__FUNCTION__;
Line 15850... Line 15850...
15850
 
15850
 
15851
			#回傳結果
15851
			#回傳結果
15852
			return $result;
15852
			return $result;
15853
 
15853
 
15854
			}#if end
15854
			}#if end
15855
		
15855
 
15856
		#取得參數
15856
		#取得參數
15857
		$result["argu"]=$conf;
15857
		$result["argu"]=$conf;
15858
 
15858
 
15859
		#如果 $conf 不為陣列
15859
		#如果 $conf 不為陣列
15860
		if(gettype($conf)!=="array"){
15860
		if(gettype($conf)!=="array"){
Line 15875... Line 15875...
15875
 
15875
 
15876
			#回傳結果
15876
			#回傳結果
15877
			return $result;
15877
			return $result;
15878
 
15878
 
15879
			}#if end
15879
			}#if end
15880
	
15880
 
15881
		#debug
15881
		#debug
15882
		#var_dump(__FUNCTION__,__LINE__,$conf);
15882
		#var_dump(__FUNCTION__,__LINE__,$conf);
15883
	
15883
 
15884
		#檢查參數
15884
		#檢查參數
15885
		#函式說明:
15885
		#函式說明:
15886
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
15886
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
15887
		#回傳結果:
15887
		#回傳結果:
15888
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
15888
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 15934... Line 15934...
15934
		#array_keys=>http://php.net/manual/en/function.array-keys.php
15934
		#array_keys=>http://php.net/manual/en/function.array-keys.php
15935
		#備註:
15935
		#備註:
15936
		#無.
15936
		#無.
15937
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
15937
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
15938
		unset($conf["variableCheck::checkArguments"]);
15938
		unset($conf["variableCheck::checkArguments"]);
15939
		
15939
 
15940
		#debug
15940
		#debug
15941
		#var_dump(__FUNCTION__,__LINE__,$checkArguments);
15941
		#var_dump(__FUNCTION__,__LINE__,$checkArguments);
15942
		
15942
 
15943
		#如果執行失敗
15943
		#如果執行失敗
15944
		if($checkArguments["status"]==="false"){
15944
		if($checkArguments["status"]==="false"){
15945
		
15945
 
15946
			#設置錯誤訊息
15946
			#設置錯誤訊息
15947
			$result["error"]=$checkArguments;
15947
			$result["error"]=$checkArguments;
15948
		
15948
 
15949
			#var_dump,交給下個 processFuncs 處理
15949
			#var_dump,交給下個 processFuncs 處理
15950
			$result["status"]="false";
15950
			$result["status"]="false";
15951
			
15951
 
15952
			#回傳結果
15952
			#回傳結果
15953
			return $result;
15953
			return $result;
15954
		
15954
 
15955
			}#if end
15955
			}#if end
15956
			
15956
 
15957
		#如果檢查不通過
15957
		#如果檢查不通過
15958
		if($checkArguments["passed"]==="false"){
15958
		if($checkArguments["passed"]==="false"){
15959
		
15959
 
15960
			#不處理,略過,交給下個 processFuncs 處理
15960
			#不處理,略過,交給下個 processFuncs 處理
15961
			$result["status"]="continue";
15961
			$result["status"]="continue";
15962
			
15962
 
15963
			#回傳結果
15963
			#回傳結果
15964
			return $result;
15964
			return $result;
15965
		
15965
 
15966
			}#if end
15966
			}#if end
15967
		
15967
 
15968
		#檢查收到的訊息是否為json格式
15968
		#檢查收到的訊息是否為json格式
15969
		$revJson=@json_decode($conf["data"]);
15969
		$revJson=@json_decode($conf["data"]);
15970
 
15970
 
15971
		#如果不是 json
15971
		#如果不是 json
15972
		if($revJson===null){
15972
		if($revJson===null){
15973
		
15973
 
15974
			#不處理,略過,交給下個 processFuncs 處理
15974
			#不處理,略過,交給下個 processFuncs 處理
15975
			$result["status"]="continue";
15975
			$result["status"]="continue";
15976
			
15976
 
15977
			#回傳結果
15977
			#回傳結果
15978
			return $result;
15978
			return $result;
15979
		
15979
 
15980
			}#if enf
15980
			}#if enf
15981
			
15981
 
15982
		#轉換為陣列
15982
		#轉換為陣列
15983
		$revJson=(array)($revJson);
15983
		$revJson=(array)($revJson);
15984
		
15984
 
15985
		#檢查參數
15985
		#檢查參數
15986
		#函式說明:
15986
		#函式說明:
15987
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
15987
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
15988
		#回傳結果:
15988
		#回傳結果:
15989
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
15989
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 16035... Line 16035...
16035
		#array_keys=>http://php.net/manual/en/function.array-keys.php
16035
		#array_keys=>http://php.net/manual/en/function.array-keys.php
16036
		#備註:
16036
		#備註:
16037
		#無.
16037
		#無.
16038
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
16038
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
16039
		unset($conf["variableCheck::checkArguments"]);
16039
		unset($conf["variableCheck::checkArguments"]);
16040
		
16040
 
16041
		#debug
16041
		#debug
16042
		#var_dump(__FUNCTION__,__LINE__,$checkArguments);
16042
		#var_dump(__FUNCTION__,__LINE__,$checkArguments);
16043
		
16043
 
16044
		#如果執行失敗
16044
		#如果執行失敗
16045
		if($checkArguments["status"]==="false"){
16045
		if($checkArguments["status"]==="false"){
16046
		
16046
 
16047
			#設置錯誤訊息
16047
			#設置錯誤訊息
16048
			$result["error"]=$checkArguments;
16048
			$result["error"]=$checkArguments;
16049
		
16049
 
16050
			#var_dump,交給下個 processFuncs 處理
16050
			#var_dump,交給下個 processFuncs 處理
16051
			$result["status"]="false";
16051
			$result["status"]="false";
16052
			
16052
 
16053
			#回傳結果
16053
			#回傳結果
16054
			return $result;
16054
			return $result;
16055
		
16055
 
16056
			}#if end
16056
			}#if end
16057
			
16057
 
16058
		#如果檢查不通過
16058
		#如果檢查不通過
16059
		if($checkArguments["passed"]==="false"){
16059
		if($checkArguments["passed"]==="false"){
16060
		
16060
 
16061
			#不處理,略過,交給下個 processFuncs 處理
16061
			#不處理,略過,交給下個 processFuncs 處理
16062
			$result["status"]="continue";
16062
			$result["status"]="continue";
16063
			
16063
 
16064
			#回傳結果
16064
			#回傳結果
16065
			return $result;
16065
			return $result;
16066
		
16066
 
16067
			}#if end
16067
			}#if end
16068
		
16068
 
16069
		#如果是 GET Method
16069
		#如果是 GET Method
16070
		if($revJson["Method"]==="GET"){
16070
		if($revJson["Method"]==="GET"){
16071
		
16071
 
16072
			#解析 apache vhost info
16072
			#解析 apache vhost info
16073
			#函式說明:
16073
			#函式說明:
16074
			#使用 linux 的 httpd 指令來取得 vhost 資訊.
16074
			#使用 linux 的 httpd 指令來取得 vhost 資訊.
16075
			#回傳結果:
16075
			#回傳結果:
16076
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
16076
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 16086... Line 16086...
16086
			#無
16086
			#無
16087
			#備註:
16087
			#備註:
16088
			#無.
16088
			#無.
16089
			$httpdGetVhostInfo=cmd::httpdGetVhostInfo($conf["cmd::httpdGetVhostInfo"]);
16089
			$httpdGetVhostInfo=cmd::httpdGetVhostInfo($conf["cmd::httpdGetVhostInfo"]);
16090
			unset($conf["cmd::httpdGetVhostInfo"]);
16090
			unset($conf["cmd::httpdGetVhostInfo"]);
16091
		
16091
 
16092
			#debug
16092
			#debug
16093
			#var_dump(__FUNCTION__,__LINE__,$httpdGetVhostInfo);
16093
			#var_dump(__FUNCTION__,__LINE__,$httpdGetVhostInfo);
16094
		
16094
 
16095
			#如果執行失敗
16095
			#如果執行失敗
16096
			if($httpdGetVhostInfo["status"]==="false"){
16096
			if($httpdGetVhostInfo["status"]==="false"){
16097
			
16097
 
16098
				#設置錯誤訊息
16098
				#設置錯誤訊息
16099
				$result["error"]=$httpdGetVhostInfo;
16099
				$result["error"]=$httpdGetVhostInfo;
16100
			
16100
 
16101
				#var_dump,交給下個 processFuncs 處理
16101
				#var_dump,交給下個 processFuncs 處理
16102
				$result["status"]="false";
16102
				$result["status"]="false";
16103
				
16103
 
16104
				#回傳結果
16104
				#回傳結果
16105
				return $result;
16105
				return $result;
16106
			
16106
 
16107
				}#if end
16107
				}#if end
16108
				
16108
 
16109
			#針對每個結果
16109
			#針對每個結果
16110
			foreach($httpdGetVhostInfo["content"] as $vhost){
16110
			foreach($httpdGetVhostInfo["content"] as $vhost){
16111
			
16111
 
16112
				#如果有 ServerName
16112
				#如果有 ServerName
16113
				if(isset($vhost["ServerName"])){
16113
				if(isset($vhost["ServerName"])){
16114
				
16114
 
16115
					#如果是要找的 Host
16115
					#如果是要找的 Host
16116
					if($vhost["ServerName"]===$revJson["Host"]){
16116
					if($vhost["ServerName"]===$revJson["Host"]){
16117
					
16117
 
16118
						#如果有 DocumentRoot
16118
						#如果有 DocumentRoot
16119
						if(isset($vhost["DocumentRoot"])){
16119
						if(isset($vhost["DocumentRoot"])){
16120
						
16120
 
16121
							#組合要取得的檔案路徑與名稱
16121
							#組合要取得的檔案路徑與名稱
16122
							$file=$vhost["DocumentRoot"]."/".$revJson["File"];
16122
							$file=$vhost["DocumentRoot"]."/".$revJson["File"];
16123
						
16123
 
16124
							#debug
16124
							#debug
16125
							#var_dump(__LINE__,$file);
16125
							#var_dump(__LINE__,$file);
16126
						
16126
 
16127
							#如果有 Range 參數
16127
							#如果有 Range 參數
16128
							if(isset($revJson["Range"])){
16128
							if(isset($revJson["Range"])){
16129
							
16129
 
16130
								#格式參考
16130
								#格式參考
16131
								#bytes=0-499
16131
								#bytes=0-499
16132
								
16132
 
16133
								#函式說明:
16133
								#函式說明:
16134
								#將字串特定關鍵字與其前面的內容剔除
16134
								#將字串特定關鍵字與其前面的內容剔除
16135
								#回傳結果:
16135
								#回傳結果:
16136
								#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
16136
								#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
16137
								#$result["error"],錯誤訊息陣列.
16137
								#$result["error"],錯誤訊息陣列.
16138
								#$result["warning"],警告訊息鎮列.
16138
								#$result["warning"],警告訊息鎮列.
16139
								#$result["founded"],有無找到定字串"true"代表有,"false"代表沒有.
16139
								#$result["founded"],有無找到定字串"true"代表有,"false"代表沒有.
16140
								#$result["function"],當前執行的函數名稱.
16140
								#$result["function"],當前執行的函數名稱.
16141
								#$result["argu"],使用的參數.
16141
								#$result["argu"],使用的參數.
16142
								#$result["oriStr"],要處理的原始字串內容.
16142
								#$result["oriStr"],要處理的原始字串內容.
16143
								#$result["content"],處理好的的字串內容.	
16143
								#$result["content"],處理好的的字串內容.
16144
								#必填參數:
16144
								#必填參數:
16145
								#$conf["stringIn"],字串,要處理的字串.
16145
								#$conf["stringIn"],字串,要處理的字串.
16146
								$conf["stringProcess::delStrBeforeKeyWord"]["stringIn"]=$revJson["Range"];
16146
								$conf["stringProcess::delStrBeforeKeyWord"]["stringIn"]=$revJson["Range"];
16147
								#$conf["keyWord"],字串,特定字串.
16147
								#$conf["keyWord"],字串,特定字串.
16148
								$conf["stringProcess::delStrBeforeKeyWord"]["keyWord"]="bytes=";
16148
								$conf["stringProcess::delStrBeforeKeyWord"]["keyWord"]="bytes=";
Line 16155... Line 16155...
16155
								#無.
16155
								#無.
16156
								#備註:
16156
								#備註:
16157
								#無.
16157
								#無.
16158
								$delStrBeforeKeyWord=stringProcess::delStrBeforeKeyWord($conf["stringProcess::delStrBeforeKeyWord"]);
16158
								$delStrBeforeKeyWord=stringProcess::delStrBeforeKeyWord($conf["stringProcess::delStrBeforeKeyWord"]);
16159
								unset($conf["stringProcess::delStrBeforeKeyWord"]);
16159
								unset($conf["stringProcess::delStrBeforeKeyWord"]);
16160
							
16160
 
16161
								#如果執行失敗
16161
								#如果執行失敗
16162
								if($delStrBeforeKeyWord["status"]==="false"){
16162
								if($delStrBeforeKeyWord["status"]==="false"){
16163
								
16163
 
16164
									#設置錯誤訊息
16164
									#設置錯誤訊息
16165
									$result["error"]=$delStrBeforeKeyWord;
16165
									$result["error"]=$delStrBeforeKeyWord;
16166
								
16166
 
16167
									#var_dump,交給下個 processFuncs 處理
16167
									#var_dump,交給下個 processFuncs 處理
16168
									$result["status"]="false";
16168
									$result["status"]="false";
16169
									
16169
 
16170
									#回傳結果
16170
									#回傳結果
16171
									return $result;
16171
									return $result;
16172
								
16172
 
16173
									}#if end
16173
									}#if end
16174
									
16174
 
16175
								#如果沒有該有的關鍵字
16175
								#如果沒有該有的關鍵字
16176
								if($delStrBeforeKeyWord["founded"]==="false"){
16176
								if($delStrBeforeKeyWord["founded"]==="false"){
16177
								
16177
 
16178
									#設置錯誤訊息
16178
									#設置錯誤訊息
16179
									$result["error"]=$delStrBeforeKeyWord;
16179
									$result["error"]=$delStrBeforeKeyWord;
16180
								
16180
 
16181
									#var_dump,交給下個 processFuncs 處理
16181
									#var_dump,交給下個 processFuncs 處理
16182
									$result["status"]="false";
16182
									$result["status"]="false";
16183
									
16183
 
16184
									#回傳結果
16184
									#回傳結果
16185
									return $result;
16185
									return $result;
16186
								
16186
 
16187
									}#if end
16187
									}#if end
16188
							
16188
 
16189
								#解析 bytes start and end
16189
								#解析 bytes start and end
16190
								#函式說明:
16190
								#函式說明:
16191
								#將固定格式的字串分開,並回傳分開的結果.
16191
								#將固定格式的字串分開,並回傳分開的結果.
16192
								#回傳結果:
16192
								#回傳結果:
16193
								#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
16193
								#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 16210... Line 16210...
16210
								#無.
16210
								#無.
16211
								#備註:
16211
								#備註:
16212
								#無.
16212
								#無.
16213
								$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
16213
								$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
16214
								unset($conf["stringProcess::spiltString"]);
16214
								unset($conf["stringProcess::spiltString"]);
16215
							
16215
 
16216
								#如果執行失敗
16216
								#如果執行失敗
16217
								if($spiltString["status"]==="false"){
16217
								if($spiltString["status"]==="false"){
16218
								
16218
 
16219
									#設置錯誤訊息
16219
									#設置錯誤訊息
16220
									$result["error"]=$spiltString;
16220
									$result["error"]=$spiltString;
16221
								
16221
 
16222
									#var_dump,交給下個 processFuncs 處理
16222
									#var_dump,交給下個 processFuncs 處理
16223
									$result["status"]="false";
16223
									$result["status"]="false";
16224
									
16224
 
16225
									#回傳結果
16225
									#回傳結果
16226
									return $result;
16226
									return $result;
16227
								
16227
 
16228
									}#if end
16228
									}#if end
16229
									
16229
 
16230
								#如果沒有應該有的 - 符號
16230
								#如果沒有應該有的 - 符號
16231
								if($spiltString["found"]==="false"){
16231
								if($spiltString["found"]==="false"){
16232
								
16232
 
16233
									#設置錯誤訊息
16233
									#設置錯誤訊息
16234
									$result["error"]=$spiltString;
16234
									$result["error"]=$spiltString;
16235
								
16235
 
16236
									#var_dump,交給下個 processFuncs 處理
16236
									#var_dump,交給下個 processFuncs 處理
16237
									$result["status"]="false";
16237
									$result["status"]="false";
16238
									
16238
 
16239
									#回傳結果
16239
									#回傳結果
16240
									return $result;
16240
									return $result;
16241
								
16241
 
16242
									}#if end
16242
									}#if end
16243
									
16243
 
16244
								#如果沒有分割成2段
16244
								#如果沒有分割成2段
16245
								if($spiltString["dataCounts"]!==2){
16245
								if($spiltString["dataCounts"]!==2){
16246
								
16246
 
16247
									#設置錯誤訊息
16247
									#設置錯誤訊息
16248
									$result["error"]=$spiltString;
16248
									$result["error"]=$spiltString;
16249
								
16249
 
16250
									#var_dump,交給下個 processFuncs 處理
16250
									#var_dump,交給下個 processFuncs 處理
16251
									$result["status"]="false";
16251
									$result["status"]="false";
16252
									
16252
 
16253
									#回傳結果
16253
									#回傳結果
16254
									return $result;
16254
									return $result;
16255
								
16255
 
16256
									}#if end
16256
									}#if end
16257
									
16257
 
16258
								#依照Range來取得檔案內容
16258
								#依照Range來取得檔案內容
16259
								#函式說明:
16259
								#函式說明:
16260
								#取得檔案的部分內容
16260
								#取得檔案的部分內容
16261
								#回傳結果:
16261
								#回傳結果:
16262
								#$result["status"],"true"代表移除成功,"false"代表移除失敗.
16262
								#$result["status"],"true"代表移除成功,"false"代表移除失敗.
Line 16282... Line 16282...
16282
								#無.
16282
								#無.
16283
								#備註:
16283
								#備註:
16284
								#無
16284
								#無
16285
								$getFilePart=fileAccess::getFilePart($conf["fileAccess::getFilePart"]);
16285
								$getFilePart=fileAccess::getFilePart($conf["fileAccess::getFilePart"]);
16286
								unset($conf["fileAccess::getFilePart"]);
16286
								unset($conf["fileAccess::getFilePart"]);
16287
								
16287
 
16288
								#如果執行失敗
16288
								#如果執行失敗
16289
								if($getFilePart["status"]==="false"){
16289
								if($getFilePart["status"]==="false"){
16290
								
16290
 
16291
									#設置錯誤訊息
16291
									#設置錯誤訊息
16292
									$result["error"]=$getFilePart;
16292
									$result["error"]=$getFilePart;
16293
								
16293
 
16294
									#var_dump,交給下個 processFuncs 處理
16294
									#var_dump,交給下個 processFuncs 處理
16295
									$result["status"]="false";
16295
									$result["status"]="false";
16296
									
16296
 
16297
									#回傳結果
16297
									#回傳結果
16298
									return $result;
16298
									return $result;
16299
								
16299
 
16300
									}#if end
16300
									}#if end
16301
								
16301
 
16302
								#初始化要傳遞的訊息
16302
								#初始化要傳遞的訊息
16303
								$msg=array();
16303
								$msg=array();
16304
								
16304
 
16305
								#設置為最後一份了
16305
								#設置為最後一份了
16306
								$msg["LastPart"]="true";
16306
								$msg["LastPart"]="true";
16307
								
16307
 
16308
								#設置資料內容類型為binary
16308
								#設置資料內容類型為binary
16309
								$msg["dataType"]="binary";
16309
								$msg["dataType"]="binary";
16310
								
16310
 
16311
								#設置內容
16311
								#設置內容
16312
								$msg["content"]=$getFilePart["content"];
16312
								$msg["content"]=$getFilePart["content"];
16313
						
16313
 
16314
								#設置 mimeType
16314
								#設置 mimeType
16315
								$msg["mimeType"]=$getFilePart["mimeType"];
16315
								$msg["mimeType"]=$getFilePart["mimeType"];
16316
								
16316
 
16317
								#如果有設置 id
16317
								#如果有設置 id
16318
								if(isset($revJson["id"])){
16318
								if(isset($revJson["id"])){
16319
								
16319
 
16320
									#設置資料的 id
16320
									#設置資料的 id
16321
									$msg["id"]=$revJson["id"];
16321
									$msg["id"]=$revJson["id"];
16322
								
16322
 
16323
									}#if end
16323
									}#if end
16324
								
16324
 
16325
								#函式說明:
16325
								#函式說明:
16326
								#加密 handshake 後要傳送的訊息 
16326
								#加密 handshake 後要傳送的訊息
16327
								#回傳結果:
16327
								#回傳結果:
16328
								#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
16328
								#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
16329
								#$result["error"],執行不正常結束的錯訊息陣列.
16329
								#$result["error"],執行不正常結束的錯訊息陣列.
16330
								#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
16330
								#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
16331
								#$result["argu"],陣列,使用的參數.
16331
								#$result["argu"],陣列,使用的參數.
16332
								#必填參數:
16332
								#必填參數:
16333
								#$conf["text"],字串,要加密的訊息.
16333
								#$conf["text"],字串,要加密的訊息.
16334
								$conf["webSock::encode"]["text"]=base64_encode(urlencode(json_encode($msg))); 
16334
								$conf["webSock::encode"]["text"]=base64_encode(urlencode(json_encode($msg)));
16335
								#可省略參數:
16335
								#可省略參數:
16336
								#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
16336
								#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
16337
								#$conf["webSock::encode"]["payloadIsBin"]="true";
16337
								#$conf["webSock::encode"]["payloadIsBin"]="true";
16338
								#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
16338
								#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
16339
								#$conf["ping"]="false";
16339
								#$conf["ping"]="false";
Line 16349... Line 16349...
16349
								#無.
16349
								#無.
16350
								#備註:
16350
								#備註:
16351
								#無.
16351
								#無.
16352
								$talkback=webSock::encode($conf["webSock::encode"]);
16352
								$talkback=webSock::encode($conf["webSock::encode"]);
16353
								unset($conf["webSock::encode"]);
16353
								unset($conf["webSock::encode"]);
16354
								
16354
 
16355
								#如果執行失敗
16355
								#如果執行失敗
16356
								if($talkback["status"]==="false"){
16356
								if($talkback["status"]==="false"){
16357
								
16357
 
16358
									#印出結果
16358
									#印出結果
16359
									var_dump($talkback);
16359
									var_dump($talkback);
16360
								
16360
 
16361
									#結束執行
16361
									#結束執行
16362
									exit;
16362
									exit;
16363
								
16363
 
16364
									}#if end
16364
									}#if end
16365
								
16365
 
16366
								#針對每個訊息的分段
16366
								#針對每個訊息的分段
16367
								foreach($talkback["content"] as $msg){
16367
								foreach($talkback["content"] as $msg){
16368
									
16368
 
16369
									#回傳訊息
16369
									#回傳訊息
16370
									$socket_write=socket_write($conf["clientSock"], $msg, strlen($msg));
16370
									$socket_write=socket_write($conf["clientSock"], $msg, strlen($msg));
16371
									
16371
 
16372
									#如果傳遞訊息給用戶失敗
16372
									#如果傳遞訊息給用戶失敗
16373
									if($socket_write===false){
16373
									if($socket_write===false){
16374
									
16374
 
16375
										#取得錯誤代碼
16375
										#取得錯誤代碼
16376
										$errorCode=socket_last_error($conf["clientSock"]);
16376
										$errorCode=socket_last_error($conf["clientSock"]);
16377
									
16377
 
16378
										#設置執行失敗
16378
										#設置執行失敗
16379
										$result["status"]="false";
16379
										$result["status"]="false";
16380
										
16380
 
16381
										#設置錯誤代碼
16381
										#設置錯誤代碼
16382
										$result["error"][]="error code:".$errorCode;
16382
										$result["error"][]="error code:".$errorCode;
16383
									
16383
 
16384
										#設置錯誤代碼說明
16384
										#設置錯誤代碼說明
16385
										$result["erorr"][]="error description:".socket_strerror($errorCode);
16385
										$result["erorr"][]="error description:".socket_strerror($errorCode);
16386
									
16386
 
16387
										#回傳結果
16387
										#回傳結果
16388
										return $result;
16388
										return $result;
16389
									
16389
 
16390
										}#if end
16390
										}#if end
16391
									
16391
 
16392
									}#foreach end
16392
									}#foreach end
16393
									
16393
 
16394
								#設置正常,結束後面函式的執行
16394
								#設置正常,結束後面函式的執行
16395
								$result["status"]="true";
16395
								$result["status"]="true";
16396
								
16396
 
16397
								#回傳結果
16397
								#回傳結果
16398
								return $result;
16398
								return $result;
16399
								
16399
 
16400
								}#if end
16400
								}#if end
16401
						
16401
 
16402
							#反之沒有 Rnage 參數
16402
							#反之沒有 Rnage 參數
16403
							else{
16403
							else{
16404
							
16404
 
16405
								#執行到這邊代表要取得全部的檔案內容
16405
								#執行到這邊代表要取得全部的檔案內容
16406
					
16406
 
16407
								#debug
16407
								#debug
16408
								#var_dump(__LINE__,"get full file content");
16408
								#var_dump(__LINE__,"get full file content");
16409
					
16409
 
16410
								#第一部分的編號
16410
								#第一部分的編號
16411
								$partNo=0;
16411
								$partNo=0;
16412
						
16412
 
16413
								#無窮迴圈
16413
								#無窮迴圈
16414
								while(true){
16414
								while(true){
16415
								
16415
 
16416
									#分段取得檔案
16416
									#分段取得檔案
16417
									#函式說明:
16417
									#函式說明:
16418
									#取得檔案的部分內容
16418
									#取得檔案的部分內容
16419
									#回傳結果:
16419
									#回傳結果:
16420
									#$result["status"],"true"代表移除成功,"false"代表移除失敗.
16420
									#$result["status"],"true"代表移除成功,"false"代表移除失敗.
Line 16440... Line 16440...
16440
									#無.
16440
									#無.
16441
									#備註:
16441
									#備註:
16442
									#無
16442
									#無
16443
									$getFilePart=fileAccess::getFilePart($conf["fileAccess::getFilePart"]);
16443
									$getFilePart=fileAccess::getFilePart($conf["fileAccess::getFilePart"]);
16444
									unset($conf["fileAccess::getFilePart"]);
16444
									unset($conf["fileAccess::getFilePart"]);
16445
									
16445
 
16446
									#debug
16446
									#debug
16447
									#var_dump(__LINE__,$getFilePart);
16447
									#var_dump(__LINE__,$getFilePart);
16448
									
16448
 
16449
									#如果執行失敗
16449
									#如果執行失敗
16450
									if($getFilePart["status"]==="false"){
16450
									if($getFilePart["status"]==="false"){
16451
									
16451
 
16452
										#設置錯誤訊息
16452
										#設置錯誤訊息
16453
										$result["error"]=$getFilePart;
16453
										$result["error"]=$getFilePart;
16454
									
16454
 
16455
										#var_dump,交給下個 processFuncs 處理
16455
										#var_dump,交給下個 processFuncs 處理
16456
										$result["status"]="false";
16456
										$result["status"]="false";
16457
										
16457
 
16458
										#回傳結果
16458
										#回傳結果
16459
										return $result;
16459
										return $result;
16460
									
16460
 
16461
										}#if end
16461
										}#if end
16462
									
16462
 
16463
									#初始化要傳遞的訊息
16463
									#初始化要傳遞的訊息
16464
									$msg=array();
16464
									$msg=array();
16465
									
16465
 
16466
									#設置不為最後一份
16466
									#設置不為最後一份
16467
									$msg["LastPart"]="false";
16467
									$msg["LastPart"]="false";
16468
									
16468
 
16469
									#設置資料內容類型為binary
16469
									#設置資料內容類型為binary
16470
									$msg["dataType"]="binary";
16470
									$msg["dataType"]="binary";
16471
									
16471
 
16472
									#設置內容
16472
									#設置內容
16473
									$msg["content"]=$getFilePart["content"];
16473
									$msg["content"]=$getFilePart["content"];
16474
									
16474
 
16475
									#設置 mimeType
16475
									#設置 mimeType
16476
									$msg["mimeType"]=$getFilePart["mimeType"];
16476
									$msg["mimeType"]=$getFilePart["mimeType"];
16477
									
16477
 
16478
									#如果有設置 id
16478
									#如果有設置 id
16479
									if(isset($revJson["id"])){
16479
									if(isset($revJson["id"])){
16480
									
16480
 
16481
										#設置資料的 id
16481
										#設置資料的 id
16482
										$msg["id"]=$revJson["id"];
16482
										$msg["id"]=$revJson["id"];
16483
									
16483
 
16484
										}#if end
16484
										}#if end
16485
									
16485
 
16486
									#如果偵測到檔案結尾
16486
									#如果偵測到檔案結尾
16487
									if($getFilePart["EOF"]==="true"){
16487
									if($getFilePart["EOF"]==="true"){
16488
									
16488
 
16489
										#設置為最後一份了
16489
										#設置為最後一份了
16490
										$msg["LastPart"]="true";
16490
										$msg["LastPart"]="true";
16491
									
16491
 
16492
										#如果沒有內容
16492
										#如果沒有內容
16493
										if($getFilePart["length"]===0){
16493
										if($getFilePart["length"]===0){
16494
											
16494
 
16495
											#檔案區塊數字-1
16495
											#檔案區塊數字-1
16496
											$partNo--;
16496
											$partNo--;
16497
										
16497
 
16498
											}#if end
16498
											}#if end
16499
 
16499
 
16500
										#傳遞給 client - start
16500
										#傳遞給 client - start
16501
 
16501
 
16502
										#編碼後的訊息
16502
										#編碼後的訊息
Line 16504... Line 16504...
16504
 
16504
 
16505
										#debug
16505
										#debug
16506
										#var_dump(__LINE__,$msg,$msgEncoded);
16506
										#var_dump(__LINE__,$msg,$msgEncoded);
16507
 
16507
 
16508
										#函式說明:
16508
										#函式說明:
16509
										#加密 handshake 後要傳送的訊息 
16509
										#加密 handshake 後要傳送的訊息
16510
										#回傳結果:
16510
										#回傳結果:
16511
										#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
16511
										#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
16512
										#$result["error"],執行不正常結束的錯訊息陣列.
16512
										#$result["error"],執行不正常結束的錯訊息陣列.
16513
										#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
16513
										#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
16514
										#$result["argu"],陣列,使用的參數.
16514
										#$result["argu"],陣列,使用的參數.
16515
										#必填參數:
16515
										#必填參數:
16516
										#$conf["text"],字串,要加密的訊息.
16516
										#$conf["text"],字串,要加密的訊息.
16517
										$conf["webSock::encode"]["text"]=$msgEncoded; 
16517
										$conf["webSock::encode"]["text"]=$msgEncoded;
16518
										#可省略參數:
16518
										#可省略參數:
16519
										#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
16519
										#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
16520
										#$conf["webSock::encode"]["payloadIsBin"]="false";
16520
										#$conf["webSock::encode"]["payloadIsBin"]="false";
16521
										#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
16521
										#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
16522
										#$conf["ping"]="false";
16522
										#$conf["ping"]="false";
Line 16532... Line 16532...
16532
										#無.
16532
										#無.
16533
										#備註:
16533
										#備註:
16534
										#無.
16534
										#無.
16535
										$talkback=webSock::encode($conf["webSock::encode"]);
16535
										$talkback=webSock::encode($conf["webSock::encode"]);
16536
										unset($conf["webSock::encode"]);
16536
										unset($conf["webSock::encode"]);
16537
										
16537
 
16538
										#debug
16538
										#debug
16539
										#var_dump(__LINE__,$talkback);
16539
										#var_dump(__LINE__,$talkback);
16540
										
16540
 
16541
										#如果執行失敗
16541
										#如果執行失敗
16542
										if($talkback["status"]==="false"){
16542
										if($talkback["status"]==="false"){
16543
										
16543
 
16544
											#印出結果
16544
											#印出結果
16545
											var_dump($talkback);
16545
											var_dump($talkback);
16546
										
16546
 
16547
											#結束執行
16547
											#結束執行
16548
											exit;
16548
											exit;
16549
										
16549
 
16550
											}#if end
16550
											}#if end
16551
										
16551
 
16552
										#針對每個訊息的分段
16552
										#針對每個訊息的分段
16553
										foreach($talkback["content"] as $msg){
16553
										foreach($talkback["content"] as $msg){
16554
											
16554
 
16555
											#回傳訊息
16555
											#回傳訊息
16556
											$socket_write=socket_write($conf["clientSock"], $msg, strlen($msg));
16556
											$socket_write=socket_write($conf["clientSock"], $msg, strlen($msg));
16557
											
16557
 
16558
											#debug
16558
											#debug
16559
											#var_dump(__LINE__,$socket_write,$msg);
16559
											#var_dump(__LINE__,$socket_write,$msg);
16560
											
16560
 
16561
											#如果傳遞訊息給用戶失敗
16561
											#如果傳遞訊息給用戶失敗
16562
											if($socket_write===false){
16562
											if($socket_write===false){
16563
											
16563
 
16564
												#取得錯誤代碼
16564
												#取得錯誤代碼
16565
												$errorCode=socket_last_error($conf["clientSock"]);
16565
												$errorCode=socket_last_error($conf["clientSock"]);
16566
											
16566
 
16567
												#設置執行失敗
16567
												#設置執行失敗
16568
												$result["status"]="false";
16568
												$result["status"]="false";
16569
												
16569
 
16570
												#設置錯誤代碼
16570
												#設置錯誤代碼
16571
												$result["error"][]="error code:".$errorCode;
16571
												$result["error"][]="error code:".$errorCode;
16572
											
16572
 
16573
												#設置錯誤代碼說明
16573
												#設置錯誤代碼說明
16574
												$result["erorr"][]="error description:".socket_strerror($errorCode);
16574
												$result["erorr"][]="error description:".socket_strerror($errorCode);
16575
											
16575
 
16576
												#回傳結果
16576
												#回傳結果
16577
												return $result;
16577
												return $result;
16578
											
16578
 
16579
												}#if end
16579
												}#if end
16580
											
16580
 
16581
											}#foreach end
16581
											}#foreach end
16582
											
16582
 
16583
										#傳遞給 client - end
16583
										#傳遞給 client - end
16584
										
16584
 
16585
										#結束 while
16585
										#結束 while
16586
										break;
16586
										break;
16587
									
16587
 
16588
										}#if end
16588
										}#if end
16589
									
16589
 
16590
									#傳遞給 client - start
16590
									#傳遞給 client - start
16591
									
16591
 
16592
									#debug
16592
									#debug
16593
									#var_dump(__LINE__,"start tranfer");
16593
									#var_dump(__LINE__,"start tranfer");
16594
									
16594
 
16595
									#函式說明:
16595
									#函式說明:
16596
									#加密 handshake 後要傳送的訊息 
16596
									#加密 handshake 後要傳送的訊息
16597
									#回傳結果:
16597
									#回傳結果:
16598
									#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
16598
									#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
16599
									#$result["error"],執行不正常結束的錯訊息陣列.
16599
									#$result["error"],執行不正常結束的錯訊息陣列.
16600
									#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
16600
									#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
16601
									#$result["argu"],陣列,使用的參數.
16601
									#$result["argu"],陣列,使用的參數.
16602
									#必填參數:
16602
									#必填參數:
16603
									#$conf["text"],字串,要加密的訊息.
16603
									#$conf["text"],字串,要加密的訊息.
16604
									$conf["webSock::encode"]["text"]=base64_encode(urlencode(json_encode($msg))); 
16604
									$conf["webSock::encode"]["text"]=base64_encode(urlencode(json_encode($msg)));
16605
									#可省略參數:
16605
									#可省略參數:
16606
									#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
16606
									#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
16607
									#$conf["webSock::encode"]["payloadIsBin"]="true";
16607
									#$conf["webSock::encode"]["payloadIsBin"]="true";
16608
									#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
16608
									#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
16609
									#$conf["ping"]="false";
16609
									#$conf["ping"]="false";
Line 16619... Line 16619...
16619
									#無.
16619
									#無.
16620
									#備註:
16620
									#備註:
16621
									#無.
16621
									#無.
16622
									$talkback=webSock::encode($conf["webSock::encode"]);
16622
									$talkback=webSock::encode($conf["webSock::encode"]);
16623
									unset($conf["webSock::encode"]);
16623
									unset($conf["webSock::encode"]);
16624
									
16624
 
16625
									#如果執行失敗
16625
									#如果執行失敗
16626
									if($talkback["status"]==="false"){
16626
									if($talkback["status"]==="false"){
16627
									
16627
 
16628
										#印出結果
16628
										#印出結果
16629
										var_dump($talkback);
16629
										var_dump($talkback);
16630
									
16630
 
16631
										#結束執行
16631
										#結束執行
16632
										exit;
16632
										exit;
16633
									
16633
 
16634
										}#if end
16634
										}#if end
16635
									
16635
 
16636
									#針對每個訊息的分段
16636
									#針對每個訊息的分段
16637
									foreach($talkback["content"] as $msg){
16637
									foreach($talkback["content"] as $msg){
16638
										
16638
 
16639
										#回傳訊息
16639
										#回傳訊息
16640
										$socket_write=socket_write($conf["clientSock"], $msg, strlen($msg));
16640
										$socket_write=socket_write($conf["clientSock"], $msg, strlen($msg));
16641
										
16641
 
16642
										#如果傳遞訊息給用戶失敗
16642
										#如果傳遞訊息給用戶失敗
16643
										if($socket_write===false){
16643
										if($socket_write===false){
16644
										
16644
 
16645
											#取得錯誤代碼
16645
											#取得錯誤代碼
16646
											$errorCode=socket_last_error($conf["clientSock"]);
16646
											$errorCode=socket_last_error($conf["clientSock"]);
16647
										
16647
 
16648
											#設置執行失敗
16648
											#設置執行失敗
16649
											$result["status"]="false";
16649
											$result["status"]="false";
16650
											
16650
 
16651
											#設置錯誤代碼
16651
											#設置錯誤代碼
16652
											$result["error"][]="error code:".$errorCode;
16652
											$result["error"][]="error code:".$errorCode;
16653
										
16653
 
16654
											#設置錯誤代碼說明
16654
											#設置錯誤代碼說明
16655
											$result["erorr"][]="error description:".socket_strerror($errorCode);
16655
											$result["erorr"][]="error description:".socket_strerror($errorCode);
16656
										
16656
 
16657
											#回傳結果
16657
											#回傳結果
16658
											return $result;
16658
											return $result;
16659
										
16659
 
16660
											}#if end
16660
											}#if end
16661
										
16661
 
16662
										}#foreach end
16662
										}#foreach end
16663
									
16663
 
16664
									#傳遞給 client - end
16664
									#傳遞給 client - end
16665
									
16665
 
16666
									#檔案區塊數字+1
16666
									#檔案區塊數字+1
16667
									$partNo++;
16667
									$partNo++;
16668
								
16668
 
16669
									}#while end
16669
									}#while end
16670
								
16670
 
16671
								#設置執行正常
16671
								#設置執行正常
16672
								$result["status"]="true";
16672
								$result["status"]="true";
16673
		
16673
 
16674
								#回傳結果
16674
								#回傳結果
16675
								return $result;
16675
								return $result;
16676
		
16676
 
16677
								}#else end
16677
								}#else end
16678
						
16678
 
16679
							}#if end
16679
							}#if end
16680
					
16680
 
16681
						}#if end
16681
						}#if end
16682
				
16682
 
16683
					}#if end
16683
					}#if end
16684
			
16684
 
16685
				}#foreach end
16685
				}#foreach end
16686
				
16686
 
16687
			#設置執行不正常
16687
			#設置執行不正常
16688
			$result["status"]="false";
16688
			$result["status"]="false";
16689
			
16689
 
16690
			#設置執行錯誤
16690
			#設置執行錯誤
16691
			$result["error"]="no matched ServerName";
16691
			$result["error"]="no matched ServerName";
16692
			
16692
 
16693
			#debug
16693
			#debug
16694
			var_dump(__FUNCTION__,__LINE__,$result);
16694
			var_dump(__FUNCTION__,__LINE__,$result);
16695
			
16695
 
16696
			#回傳結果
16696
			#回傳結果
16697
			return $result;
16697
			return $result;
16698
		
16698
 
16699
			}#if end
16699
			}#if end
16700
			
16700
 
16701
		#設置執行不正常
16701
		#設置執行不正常
16702
		$result["status"]="false";
16702
		$result["status"]="false";
16703
		
16703
 
16704
		#設置執行錯誤
16704
		#設置執行錯誤
16705
		$result["error"]="Method should be GET";
16705
		$result["error"]="Method should be GET";
16706
		
16706
 
16707
		#回傳結果
16707
		#回傳結果
16708
		return $result;
16708
		return $result;
16709
	
16709
 
16710
		}#function getFileContent end
16710
		}#function getFileContent end
16711
	
16711
 
16712
	/*
16712
	/*
16713
	#函式說明:
16713
	#函式說明:
16714
	#提供webSock::nativeSocketTcpIpServer用於接受檢查權限的功能.
16714
	#提供webSock::nativeSocketTcpIpServer用於接受檢查權限的功能.
16715
	#回傳結果:
16715
	#回傳結果:
16716
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
16716
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 16744... Line 16744...
16744
	#https://www.rfc-editor.org/rfc/rfc6455#page-28, Web Socket Base Framing Protocol.
16744
	#https://www.rfc-editor.org/rfc/rfc6455#page-28, Web Socket Base Framing Protocol.
16745
	#備註:
16745
	#備註:
16746
	#無.
16746
	#無.
16747
	*/
16747
	*/
16748
	public static function checkAuth(&$conf){
16748
	public static function checkAuth(&$conf){
16749
	
16749
 
16750
		#初始化要回傳的結果
16750
		#初始化要回傳的結果
16751
		$result=array();
16751
		$result=array();
16752
 
16752
 
16753
		#取得當前執行的函數名稱
16753
		#取得當前執行的函數名稱
16754
		$result["function"]=__FUNCTION__;
16754
		$result["function"]=__FUNCTION__;
Line 16756... Line 16756...
16756
		#涵式說明:
16756
		#涵式說明:
16757
		#判斷當前環境為web還是cmd
16757
		#判斷當前環境為web還是cmd
16758
		#回傳結果:
16758
		#回傳結果:
16759
		#$result,"web"或"cmd"
16759
		#$result,"web"或"cmd"
16760
		if(csInformation::getEnv()==="web"){
16760
		if(csInformation::getEnv()==="web"){
16761
			
16761
 
16762
			#設置執行失敗
16762
			#設置執行失敗
16763
			$result["status"]="false";
16763
			$result["status"]="false";
16764
			
16764
 
16765
			#設置執行錯誤訊息
16765
			#設置執行錯誤訊息
16766
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
16766
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
16767
			
16767
 
16768
			#回傳結果
16768
			#回傳結果
16769
			return $result;
16769
			return $result;
16770
			
16770
 
16771
			}#if end
16771
			}#if end
16772
 
16772
 
16773
		#取得參數
16773
		#取得參數
16774
		$result["argu"]=$conf;
16774
		$result["argu"]=$conf;
16775
 
16775
 
16776
		#如果 $conf 不為陣列
16776
		#如果 $conf 不為陣列
16777
		if(gettype($conf)!="array"){
16777
		if(gettype($conf)!="array"){
16778
		
16778
 
16779
			#設置執行失敗
16779
			#設置執行失敗
16780
			$result["status"]="false";
16780
			$result["status"]="false";
16781
		
16781
 
16782
			#設置執行錯誤訊息
16782
			#設置執行錯誤訊息
16783
			$result["error"][]="\$conf變數須為陣列形態";
16783
			$result["error"][]="\$conf變數須為陣列形態";
16784
		
16784
 
16785
			#如果傳入的參數為 null
16785
			#如果傳入的參數為 null
16786
			if($conf==null){
16786
			if($conf==null){
16787
			
16787
 
16788
				#設置執行錯誤訊息
16788
				#設置執行錯誤訊息
16789
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
16789
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
16790
			
16790
 
16791
				}#if end
16791
				}#if end
16792
 
16792
 
16793
			#回傳結果
16793
			#回傳結果
16794
			return $result;
16794
			return $result;
16795
		
16795
 
16796
			}#if end
16796
			}#if end
16797
		
16797
 
16798
		#檢查參數
16798
		#檢查參數
16799
		#函式說明:
16799
		#函式說明:
16800
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
16800
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
16801
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
16801
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
16802
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
16802
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
Line 16827... Line 16827...
16827
		#$conf["canBeEmpty"]=array();
16827
		#$conf["canBeEmpty"]=array();
16828
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
16828
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
16829
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("wsMode");
16829
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("wsMode");
16830
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
16830
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
16831
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("wsMode","gpgId","falseAllowFuncPlus","publicAllowFuncPlus");
16831
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("wsMode","gpgId","falseAllowFuncPlus","publicAllowFuncPlus");
16832
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double"); 
16832
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
16833
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string","array","array");
16833
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string","array","array");
16834
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
16834
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
16835
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("true",gnupgId,array(),array());
16835
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("true",gnupgId,array(),array());
16836
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
16836
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
16837
		#$conf["arrayCountEqualCheck"][]=array();
16837
		#$conf["arrayCountEqualCheck"][]=array();
Line 16839... Line 16839...
16839
		#array_keys=>http://php.net/manual/en/function.array-keys.php
16839
		#array_keys=>http://php.net/manual/en/function.array-keys.php
16840
		#建議:
16840
		#建議:
16841
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
16841
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
16842
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
16842
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
16843
		unset($conf["variableCheck::checkArguments"]);
16843
		unset($conf["variableCheck::checkArguments"]);
16844
		
16844
 
16845
		#如果檢查參數失敗
16845
		#如果檢查參數失敗
16846
		if($checkArguments["status"]==="false"){
16846
		if($checkArguments["status"]==="false"){
16847
			
16847
 
16848
			#設置執行失敗
16848
			#設置執行失敗
16849
			$result["status"]="false";
16849
			$result["status"]="false";
16850
		
16850
 
16851
			#設置執行錯誤訊息
16851
			#設置執行錯誤訊息
16852
			$result["error"]=$checkArguments;
16852
			$result["error"]=$checkArguments;
16853
			
16853
 
16854
			#回傳結果
16854
			#回傳結果
16855
			return $result;
16855
			return $result;
16856
			
16856
 
16857
			}#if end
16857
			}#if end
16858
			
16858
 
16859
		#如果檢查參數不通過
16859
		#如果檢查參數不通過
16860
		if($checkArguments["passed"]==="false"){
16860
		if($checkArguments["passed"]==="false"){
16861
			
16861
 
16862
			#設置執行失敗
16862
			#設置執行失敗
16863
			$result["status"]="false";
16863
			$result["status"]="false";
16864
		
16864
 
16865
			#設置執行錯誤訊息
16865
			#設置執行錯誤訊息
16866
			$result["error"]=$checkArguments;
16866
			$result["error"]=$checkArguments;
16867
			
16867
 
16868
			#回傳結果
16868
			#回傳結果
16869
			return $result;
16869
			return $result;
16870
			
16870
 
16871
			}#if end
16871
			}#if end
16872
 
16872
 
16873
		#取得收到的訊息
16873
		#取得收到的訊息
16874
		$receivedData=$conf["data"];
16874
		$receivedData=$conf["data"];
16875
		
16875
 
16876
		#取得 server 的 socket
16876
		#取得 server 的 socket
16877
		$serverSocket=$conf["serverSock"];
16877
		$serverSocket=$conf["serverSock"];
16878
		
16878
 
16879
		#取得 client 的 socket
16879
		#取得 client 的 socket
16880
		$clientSocket=$conf["clientSock"];
16880
		$clientSocket=$conf["clientSock"];
16881
		
16881
 
16882
		#取得 client 的資訊
16882
		#取得 client 的資訊
16883
		$clientInfo=$conf["clientInfo"];
16883
		$clientInfo=$conf["clientInfo"];
16884
		
16884
 
16885
		#取得 client 的索引
16885
		#取得 client 的索引
16886
		$clientIndex=$conf["clientIndex"];
16886
		$clientIndex=$conf["clientIndex"];
16887
		
16887
 
16888
		#取得 all clients 的資訊
16888
		#取得 all clients 的資訊
16889
		$allConn=&$conf["allConn"];
16889
		$allConn=&$conf["allConn"];
16890
	
16890
 
16891
		/* 檢查權限不用看收到的訊息
16891
		/* 檢查權限不用看收到的訊息
16892
	
16892
 
16893
		#hex2bin
16893
		#hex2bin
16894
		#函式說明:
16894
		#函式說明:
16895
		#加密或編碼字串,可以用的方法有sha1,md5,password_sha,qbpwcf,bin2hex,hex2bin,sha1可以回傳20或40的數值字串,md5可以回傳32個數值字串,password_hash可以回傳60~255個字元.
16895
		#加密或編碼字串,可以用的方法有sha1,md5,password_sha,qbpwcf,bin2hex,hex2bin,sha1可以回傳20或40的數值字串,md5可以回傳32個數值字串,password_hash可以回傳60~255個字元.
16896
		#回傳結果:
16896
		#回傳結果:
16897
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常. 
16897
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
16898
		#$result["function"],當前執行的函數名稱.
16898
		#$result["function"],當前執行的函數名稱.
16899
		#$result["content"],加密後的結果.
16899
		#$result["content"],加密後的結果.
16900
		#$result["error"],錯誤訊息陣列.
16900
		#$result["error"],錯誤訊息陣列.
16901
		#$result["argu"],使用的參數.
16901
		#$result["argu"],使用的參數.
16902
		#必填參數:
16902
		#必填參數:
Line 16944... Line 16944...
16944
 
16944
 
16945
			}#if end
16945
			}#if end
16946
 
16946
 
16947
		#取得hex2bin後的字串
16947
		#取得hex2bin後的字串
16948
		$receivedData=$enCodeStr["content"];
16948
		$receivedData=$enCodeStr["content"];
16949
		
16949
 
16950
		#gpg decrypt
16950
		#gpg decrypt
16951
		#函式說明:
16951
		#函式說明:
16952
		#加密或編碼字串,可以用的方法有sha1,md5,password_sha,qbpwcf,bin2hex,hex2bin,sha1可以回傳20或40的數值字串,md5可以回傳32個數值字串,password_hash可以回傳60~255個字元.
16952
		#加密或編碼字串,可以用的方法有sha1,md5,password_sha,qbpwcf,bin2hex,hex2bin,sha1可以回傳20或40的數值字串,md5可以回傳32個數值字串,password_hash可以回傳60~255個字元.
16953
		#回傳結果:
16953
		#回傳結果:
16954
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常. 
16954
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
16955
		#$result["function"],當前執行的函數名稱.
16955
		#$result["function"],當前執行的函數名稱.
16956
		#$result["content"],加密後的結果.
16956
		#$result["content"],加密後的結果.
16957
		#$result["error"],錯誤訊息陣列.
16957
		#$result["error"],錯誤訊息陣列.
16958
		#$result["argu"],使用的參數.
16958
		#$result["argu"],使用的參數.
16959
		#必填參數:
16959
		#必填參數:
Line 17001... Line 17001...
17001
 
17001
 
17002
			}#if end
17002
			}#if end
17003
 
17003
 
17004
		#取得解密後的字串
17004
		#取得解密後的字串
17005
		$receivedData=$enCodeStr["content"];
17005
		$receivedData=$enCodeStr["content"];
17006
		
17006
 
17007
		#json_decode
17007
		#json_decode
17008
		$receivedData=json_decode($receivedData);
17008
		$receivedData=json_decode($receivedData);
17009
	
17009
 
17010
		*/
17010
		*/
17011
	
17011
 
17012
		#取得認證狀態
17012
		#取得認證狀態
17013
		$authStatus=$conf["clientInfo"][$conf["clientIndex"]]["auth"];
17013
		$authStatus=$conf["clientInfo"][$conf["clientIndex"]]["auth"];
17014
	
17014
 
17015
		#判斷 authStatus
17015
		#判斷 authStatus
17016
		switch($authStatus){
17016
		switch($authStatus){
17017
		
17017
 
17018
			#如果未經過認證
17018
			#如果未經過認證
17019
			case "false":
17019
			case "false":
17020
				
17020
 
17021
				#預設可以使用的功能只有"auth"跟"checkAuth"
17021
				#預設可以使用的功能只有"auth"跟"checkAuth"
17022
				$authFunc=array("auth","checkAuth");
17022
				$authFunc=array("auth","checkAuth");
17023
			
17023
 
17024
				#如果有 $conf["falseAllowFuncPlus"]
17024
				#如果有 $conf["falseAllowFuncPlus"]
17025
				if(count($conf["falseAllowFuncPlus"])>0){
17025
				if(count($conf["falseAllowFuncPlus"])>0){
17026
				
17026
 
17027
					#合併 array
17027
					#合併 array
17028
					#函式說明:
17028
					#函式說明:
17029
					#將多個一維陣列串聯起來,key從0開始排序.
17029
					#將多個一維陣列串聯起來,key從0開始排序.
17030
					#回傳的結果:
17030
					#回傳的結果:
17031
					#$result["status"],"true"表執行正常,"false"代表執行不正常.
17031
					#$result["status"],"true"表執行正常,"false"代表執行不正常.
Line 17045... Line 17045...
17045
					#無.
17045
					#無.
17046
					#備註:
17046
					#備註:
17047
					#無.
17047
					#無.
17048
					$mergeArray=arrays::mergeArray($conf["arrays::mergeArray"]);
17048
					$mergeArray=arrays::mergeArray($conf["arrays::mergeArray"]);
17049
					unset($conf["arrays::mergeArray"]);
17049
					unset($conf["arrays::mergeArray"]);
17050
				
17050
 
17051
					#如果執行失敗
17051
					#如果執行失敗
17052
					if($mergeArray["status"]==="false"){
17052
					if($mergeArray["status"]==="false"){
17053
 
17053
 
17054
						#設置執行異常
17054
						#設置執行異常
17055
						$result["status"]="false";
17055
						$result["status"]="false";
Line 17059... Line 17059...
17059
 
17059
 
17060
						#回傳結果
17060
						#回傳結果
17061
						return $result;
17061
						return $result;
17062
 
17062
 
17063
						}#if end
17063
						}#if end
17064
				
17064
 
17065
					#取得合併好允許使用的清單
17065
					#取得合併好允許使用的清單
17066
					$authFunc=$mergeArray["content"];
17066
					$authFunc=$mergeArray["content"];
17067
				
17067
 
17068
					}#if end
17068
					}#if end
17069
			
17069
 
17070
				#如果沒有權限使用 
17070
				#如果沒有權限使用
17071
				if(!in_array($conf["func"],$authFunc)){
17071
				if(!in_array($conf["func"],$authFunc)){
17072
				
17072
 
17073
					#設置執行異常
17073
					#設置執行異常
17074
					$result["status"]="false";
17074
					$result["status"]="false";
17075
 
17075
 
17076
					#設置執行錯誤
17076
					#設置執行錯誤
17077
					$result["error"][]="no ".$conf["func"]." permission";
17077
					$result["error"][]="no ".$conf["func"]." permission";
17078
					
17078
 
17079
					#回傳結果
17079
					#回傳結果
17080
					return $result;
17080
					return $result;
17081
				
17081
 
17082
					}#if end
17082
					}#if end
17083
			
17083
 
17084
				#結束 switch
17084
				#結束 switch
17085
				break;
17085
				break;
17086
			
17086
 
17087
			#如果是"root"賬戶,代表系統管理者,擁有所有權限.
17087
			#如果是"root"賬戶,代表系統管理者,擁有所有權限.
17088
			case "root":
17088
			case "root":
17089
			
17089
 
17090
				#預設可以使用的功能只有"*",代表所有功能都能用.
17090
				#預設可以使用的功能只有"*",代表所有功能都能用.
17091
				$authFunc=array("*");
17091
				$authFunc=array("*");
17092
			
17092
 
17093
				/*	允許使用所有函式
17093
				/*	允許使用所有函式
17094
			
17094
 
17095
				#如果沒有權限使用 
17095
				#如果沒有權限使用
17096
				if(!in_array($conf["func"],$authFunc)){
17096
				if(!in_array($conf["func"],$authFunc)){
17097
				
17097
 
17098
					#設置執行異常
17098
					#設置執行異常
17099
					$result["status"]="false";
17099
					$result["status"]="false";
17100
 
17100
 
17101
					#設置執行錯誤
17101
					#設置執行錯誤
17102
					$result["error"][]="no ".$conf["func"]." permission";
17102
					$result["error"][]="no ".$conf["func"]." permission";
17103
					
17103
 
17104
					#回傳結果
17104
					#回傳結果
17105
					return $result;
17105
					return $result;
17106
				
17106
 
17107
					}#if end
17107
					}#if end
17108
			
17108
 
17109
				*/
17109
				*/
17110
			
17110
 
17111
				#結束 switch
17111
				#結束 switch
17112
				break;
17112
				break;
17113
			
17113
 
17114
			#如果是"public"賬戶,代表具備一般使用權限的公用使用者.
17114
			#如果是"public"賬戶,代表具備一般使用權限的公用使用者.
17115
			case "public":
17115
			case "public":
17116
			
17116
 
17117
				#預設可以使用的功能只有"*",代表所有功能都能用.
17117
				#預設可以使用的功能只有"*",代表所有功能都能用.
17118
				$authFunc=array("auth","checkAuth","basic");
17118
				$authFunc=array("auth","checkAuth","basic");
17119
				
17119
 
17120
				#如果有 publicAllowFuncPlus
17120
				#如果有 publicAllowFuncPlus
17121
				if(count($conf["publicAllowFuncPlus"])>0){
17121
				if(count($conf["publicAllowFuncPlus"])>0){
17122
			
17122
 
17123
					#合併 array
17123
					#合併 array
17124
					#函式說明:
17124
					#函式說明:
17125
					#將多個一維陣列串聯起來,key從0開始排序.
17125
					#將多個一維陣列串聯起來,key從0開始排序.
17126
					#回傳的結果:
17126
					#回傳的結果:
17127
					#$result["status"],"true"表執行正常,"false"代表執行不正常.
17127
					#$result["status"],"true"表執行正常,"false"代表執行不正常.
Line 17141... Line 17141...
17141
					#無.
17141
					#無.
17142
					#備註:
17142
					#備註:
17143
					#無.
17143
					#無.
17144
					$mergeArray=arrays::mergeArray($conf["arrays::mergeArray"]);
17144
					$mergeArray=arrays::mergeArray($conf["arrays::mergeArray"]);
17145
					unset($conf["arrays::mergeArray"]);
17145
					unset($conf["arrays::mergeArray"]);
17146
				
17146
 
17147
					#如果執行失敗
17147
					#如果執行失敗
17148
					if($mergeArray["status"]==="false"){
17148
					if($mergeArray["status"]==="false"){
17149
 
17149
 
17150
						#設置執行異常
17150
						#設置執行異常
17151
						$result["status"]="false";
17151
						$result["status"]="false";
Line 17155... Line 17155...
17155
 
17155
 
17156
						#回傳結果
17156
						#回傳結果
17157
						return $result;
17157
						return $result;
17158
 
17158
 
17159
						}#if end
17159
						}#if end
17160
				
17160
 
17161
					#取得合併好允許使用的清單
17161
					#取得合併好允許使用的清單
17162
					$authFunc=$mergeArray["content"];
17162
					$authFunc=$mergeArray["content"];
17163
					
17163
 
17164
					}#if end
17164
					}#if end
17165
			
17165
 
17166
				#如果沒有權限使用 
17166
				#如果沒有權限使用
17167
				if(!in_array($conf["func"],$authFunc)){
17167
				if(!in_array($conf["func"],$authFunc)){
17168
				
17168
 
17169
					#設置執行異常
17169
					#設置執行異常
17170
					$result["status"]="false";
17170
					$result["status"]="false";
17171
 
17171
 
17172
					#設置執行錯誤
17172
					#設置執行錯誤
17173
					$result["error"][]="no ".$conf["func"]." permission";
17173
					$result["error"][]="no ".$conf["func"]." permission";
17174
					
17174
 
17175
					#回傳結果
17175
					#回傳結果
17176
					return $result;
17176
					return $result;
17177
				
17177
 
17178
					}#if end
17178
					}#if end
17179
			
17179
 
17180
				break;
17180
				break;
17181
			
17181
 
17182
			#其他結果
17182
			#其他結果
17183
			default:
17183
			default:
17184
			
17184
 
17185
				#依照指定的資料來源進行權限與可用功能的檢查
17185
				#依照指定的資料來源進行權限與可用功能的檢查
17186
				#...
17186
				#...
17187
				
17187
 
17188
				#設置執行異常
17188
				#設置執行異常
17189
				$result["status"]="false";
17189
				$result["status"]="false";
17190
 
17190
 
17191
				#設置執行錯誤
17191
				#設置執行錯誤
17192
				$result["error"][]="not supported authStatus(".$authStatus.")";
17192
				$result["error"][]="not supported authStatus(".$authStatus.")";
17193
				
17193
 
17194
				#回傳結果
17194
				#回傳結果
17195
				return $result;
17195
				return $result;
17196
				
17196
 
17197
			}#switch end
17197
			}#switch end
17198
	
17198
 
17199
			#設置執行正常
17199
			#設置執行正常
17200
			$result["status"]="true";
17200
			$result["status"]="true";
17201
 
17201
 
17202
			#回傳結果
17202
			#回傳結果
17203
			return $result;
17203
			return $result;
17204
	
17204
 
17205
		}#function checkAuth end
17205
		}#function checkAuth end
17206
	
17206
 
17207
	/*
17207
	/*
17208
	#函式說明:
17208
	#函式說明:
17209
	#提供webSock::nativeSocketTcpIpServer用於接受"auth:"開頭的訊息進行認證使用者的功能.
17209
	#提供webSock::nativeSocketTcpIpServer用於接受"auth:"開頭的訊息進行認證使用者的功能.
17210
	#回傳結果:
17210
	#回傳結果:
17211
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常,"continue"代表不適用.
17211
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常,"continue"代表不適用.
Line 17233... Line 17233...
17233
	#https://www.rfc-editor.org/rfc/rfc6455#page-28, Web Socket Base Framing Protocol.
17233
	#https://www.rfc-editor.org/rfc/rfc6455#page-28, Web Socket Base Framing Protocol.
17234
	#備註:
17234
	#備註:
17235
	#無.
17235
	#無.
17236
	*/
17236
	*/
17237
	public static function auth(&$conf){
17237
	public static function auth(&$conf){
17238
	
17238
 
17239
		#初始化要回傳的結果
17239
		#初始化要回傳的結果
17240
		$result=array();
17240
		$result=array();
17241
 
17241
 
17242
		#取得當前執行的函數名稱
17242
		#取得當前執行的函數名稱
17243
		$result["function"]=__FUNCTION__;
17243
		$result["function"]=__FUNCTION__;
Line 17245... Line 17245...
17245
		#涵式說明:
17245
		#涵式說明:
17246
		#判斷當前環境為web還是cmd
17246
		#判斷當前環境為web還是cmd
17247
		#回傳結果:
17247
		#回傳結果:
17248
		#$result,"web"或"cmd"
17248
		#$result,"web"或"cmd"
17249
		if(csInformation::getEnv()==="web"){
17249
		if(csInformation::getEnv()==="web"){
17250
			
17250
 
17251
			#設置執行失敗
17251
			#設置執行失敗
17252
			$result["status"]="false";
17252
			$result["status"]="false";
17253
			
17253
 
17254
			#設置執行錯誤訊息
17254
			#設置執行錯誤訊息
17255
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
17255
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
17256
			
17256
 
17257
			#回傳結果
17257
			#回傳結果
17258
			return $result;
17258
			return $result;
17259
			
17259
 
17260
			}#if end
17260
			}#if end
17261
 
17261
 
17262
		#取得參數
17262
		#取得參數
17263
		$result["argu"]=$conf;
17263
		$result["argu"]=$conf;
17264
 
17264
 
17265
		#如果 $conf 不為陣列
17265
		#如果 $conf 不為陣列
17266
		if(gettype($conf)!="array"){
17266
		if(gettype($conf)!="array"){
17267
		
17267
 
17268
			#設置執行失敗
17268
			#設置執行失敗
17269
			$result["status"]="false";
17269
			$result["status"]="false";
17270
		
17270
 
17271
			#設置執行錯誤訊息
17271
			#設置執行錯誤訊息
17272
			$result["error"][]="\$conf變數須為陣列形態";
17272
			$result["error"][]="\$conf變數須為陣列形態";
17273
		
17273
 
17274
			#如果傳入的參數為 null
17274
			#如果傳入的參數為 null
17275
			if($conf==null){
17275
			if($conf==null){
17276
			
17276
 
17277
				#設置執行錯誤訊息
17277
				#設置執行錯誤訊息
17278
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
17278
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
17279
			
17279
 
17280
				}#if end
17280
				}#if end
17281
 
17281
 
17282
			#回傳結果
17282
			#回傳結果
17283
			return $result;
17283
			return $result;
17284
		
17284
 
17285
			}#if end
17285
			}#if end
17286
		
17286
 
17287
		#檢查參數
17287
		#檢查參數
17288
		#函式說明:
17288
		#函式說明:
17289
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
17289
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
17290
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
17290
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
17291
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
17291
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
Line 17316... Line 17316...
17316
		#$conf["canBeEmpty"]=array();
17316
		#$conf["canBeEmpty"]=array();
17317
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
17317
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
17318
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("wsMode");
17318
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("wsMode");
17319
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
17319
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
17320
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("wsMode","gpgId");
17320
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("wsMode","gpgId");
17321
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double"); 
17321
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
17322
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string");
17322
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string");
17323
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
17323
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
17324
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("true",gnupgId);
17324
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("true",gnupgId);
17325
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
17325
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
17326
		#$conf["arrayCountEqualCheck"][]=array();
17326
		#$conf["arrayCountEqualCheck"][]=array();
Line 17328... Line 17328...
17328
		#array_keys=>http://php.net/manual/en/function.array-keys.php
17328
		#array_keys=>http://php.net/manual/en/function.array-keys.php
17329
		#建議:
17329
		#建議:
17330
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
17330
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
17331
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
17331
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
17332
		unset($conf["variableCheck::checkArguments"]);
17332
		unset($conf["variableCheck::checkArguments"]);
17333
		
17333
 
17334
		#如果檢查參數失敗
17334
		#如果檢查參數失敗
17335
		if($checkArguments["status"]==="false"){
17335
		if($checkArguments["status"]==="false"){
17336
			
17336
 
17337
			#設置執行失敗
17337
			#設置執行失敗
17338
			$result["status"]="false";
17338
			$result["status"]="false";
17339
		
17339
 
17340
			#設置執行錯誤訊息
17340
			#設置執行錯誤訊息
17341
			$result["error"]=$checkArguments;
17341
			$result["error"]=$checkArguments;
17342
			
17342
 
17343
			#回傳結果
17343
			#回傳結果
17344
			return $result;
17344
			return $result;
17345
			
17345
 
17346
			}#if end
17346
			}#if end
17347
			
17347
 
17348
		#如果檢查參數不通過
17348
		#如果檢查參數不通過
17349
		if($checkArguments["passed"]==="false"){
17349
		if($checkArguments["passed"]==="false"){
17350
			
17350
 
17351
			#設置執行失敗
17351
			#設置執行失敗
17352
			$result["status"]="false";
17352
			$result["status"]="false";
17353
		
17353
 
17354
			#設置執行錯誤訊息
17354
			#設置執行錯誤訊息
17355
			$result["error"]=$checkArguments;
17355
			$result["error"]=$checkArguments;
17356
			
17356
 
17357
			#回傳結果
17357
			#回傳結果
17358
			return $result;
17358
			return $result;
17359
			
17359
 
17360
			}#if end
17360
			}#if end
17361
 
17361
 
17362
		#取得收到的訊息
17362
		#取得收到的訊息
17363
		$receivedData=$conf["data"];
17363
		$receivedData=$conf["data"];
17364
		
17364
 
17365
		#取得 server 的 socket
17365
		#取得 server 的 socket
17366
		$serverSocket=$conf["serverSock"];
17366
		$serverSocket=$conf["serverSock"];
17367
		
17367
 
17368
		#取得 client 的 socket
17368
		#取得 client 的 socket
17369
		$clientSocket=$conf["clientSock"];
17369
		$clientSocket=$conf["clientSock"];
17370
		
17370
 
17371
		#取得 client 的資訊
17371
		#取得 client 的資訊
17372
		$clientInfo=$conf["clientInfo"];
17372
		$clientInfo=$conf["clientInfo"];
17373
		
17373
 
17374
		#取得 client 的索引
17374
		#取得 client 的索引
17375
		$clientIndex=$conf["clientIndex"];
17375
		$clientIndex=$conf["clientIndex"];
17376
		
17376
 
17377
		#取得認證狀態
17377
		#取得認證狀態
17378
		$authStatus=&$conf["clientInfo"][$conf["clientIndex"]]["auth"];
17378
		$authStatus=&$conf["clientInfo"][$conf["clientIndex"]]["auth"];
17379
		
17379
 
17380
		#取得 all clients 的資訊
17380
		#取得 all clients 的資訊
17381
		$allConn=&$conf["allConn"];
17381
		$allConn=&$conf["allConn"];
17382
		
17382
 
17383
		#hex2bin
17383
		#hex2bin
17384
		#函式說明:
17384
		#函式說明:
17385
		#加密或編碼字串,可以用的方法有sha1,md5,password_sha,qbpwcf,bin2hex,hex2bin,sha1可以回傳20或40的數值字串,md5可以回傳32個數值字串,password_hash可以回傳60~255個字元.
17385
		#加密或編碼字串,可以用的方法有sha1,md5,password_sha,qbpwcf,bin2hex,hex2bin,sha1可以回傳20或40的數值字串,md5可以回傳32個數值字串,password_hash可以回傳60~255個字元.
17386
		#回傳結果:
17386
		#回傳結果:
17387
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常. 
17387
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
17388
		#$result["function"],當前執行的函數名稱.
17388
		#$result["function"],當前執行的函數名稱.
17389
		#$result["content"],加密後的結果.
17389
		#$result["content"],加密後的結果.
17390
		#$result["error"],錯誤訊息陣列.
17390
		#$result["error"],錯誤訊息陣列.
17391
		#$result["argu"],使用的參數.
17391
		#$result["argu"],使用的參數.
17392
		#必填參數:
17392
		#必填參數:
Line 17434... Line 17434...
17434
 
17434
 
17435
			}#if end
17435
			}#if end
17436
 
17436
 
17437
		#取得hex2bin後的字串
17437
		#取得hex2bin後的字串
17438
		$receivedData=$enCodeStr["content"];
17438
		$receivedData=$enCodeStr["content"];
17439
		
17439
 
17440
		#gpg decrypt
17440
		#gpg decrypt
17441
		#函式說明:
17441
		#函式說明:
17442
		#加密或編碼字串,可以用的方法有sha1,md5,password_sha,qbpwcf,bin2hex,hex2bin,sha1可以回傳20或40的數值字串,md5可以回傳32個數值字串,password_hash可以回傳60~255個字元.
17442
		#加密或編碼字串,可以用的方法有sha1,md5,password_sha,qbpwcf,bin2hex,hex2bin,sha1可以回傳20或40的數值字串,md5可以回傳32個數值字串,password_hash可以回傳60~255個字元.
17443
		#回傳結果:
17443
		#回傳結果:
17444
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常. 
17444
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
17445
		#$result["function"],當前執行的函數名稱.
17445
		#$result["function"],當前執行的函數名稱.
17446
		#$result["content"],加密後的結果.
17446
		#$result["content"],加密後的結果.
17447
		#$result["error"],錯誤訊息陣列.
17447
		#$result["error"],錯誤訊息陣列.
17448
		#$result["argu"],使用的參數.
17448
		#$result["argu"],使用的參數.
17449
		#必填參數:
17449
		#必填參數:
Line 17491... Line 17491...
17491
 
17491
 
17492
			}#if end
17492
			}#if end
17493
 
17493
 
17494
		#取得解密後的字串
17494
		#取得解密後的字串
17495
		$receivedData=$enCodeStr["content"];
17495
		$receivedData=$enCodeStr["content"];
17496
		
17496
 
17497
		#json_decode
17497
		#json_decode
17498
		$receivedData=json_decode($receivedData);
17498
		$receivedData=json_decode($receivedData);
17499
		
17499
 
17500
		#如果decode後的內容不為物件
17500
		#如果decode後的內容不為物件
17501
		if(gettype($receivedData)!=="Object"){
17501
		if(gettype($receivedData)!=="Object"){
17502
		
17502
 
17503
			#設置不適用
17503
			#設置不適用
17504
			$result["status"]="continue";
17504
			$result["status"]="continue";
17505
		
17505
 
17506
			#回傳結果
17506
			#回傳結果
17507
			return $result;
17507
			return $result;
17508
		
17508
 
17509
			}#if end
17509
			}#if end
17510
		
17510
 
17511
		#檢查物件是否符合格式
17511
		#檢查物件是否符合格式
17512
		#函式說明:
17512
		#函式說明:
17513
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
17513
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
17514
		#回傳結果:
17514
		#回傳結果:
17515
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
17515
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 17561... Line 17561...
17561
		#array_keys=>http://php.net/manual/en/function.array-keys.php
17561
		#array_keys=>http://php.net/manual/en/function.array-keys.php
17562
		#備註:
17562
		#備註:
17563
		#無.
17563
		#無.
17564
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
17564
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
17565
		unset($conf["variableCheck::checkArguments"]);
17565
		unset($conf["variableCheck::checkArguments"]);
17566
		
17566
 
17567
		#如果執行異常
17567
		#如果執行異常
17568
		if($checkArguments["status"]==="false"){
17568
		if($checkArguments["status"]==="false"){
17569
		
17569
 
17570
			#設置異常
17570
			#設置異常
17571
			$result["status"]="false";
17571
			$result["status"]="false";
17572
		
17572
 
17573
			#設置錯誤訊息
17573
			#設置錯誤訊息
17574
			$result["error"]=$checkArguments;
17574
			$result["error"]=$checkArguments;
17575
		
17575
 
17576
			#回傳結果
17576
			#回傳結果
17577
			return $result;
17577
			return $result;
17578
		
17578
 
17579
			}#if end
17579
			}#if end
17580
			
17580
 
17581
		#如果參數不合法
17581
		#如果參數不合法
17582
		if($checkArguments["passed"]==="false"){
17582
		if($checkArguments["passed"]==="false"){
17583
		
17583
 
17584
			#設置不適用
17584
			#設置不適用
17585
			$result["status"]="continue";
17585
			$result["status"]="continue";
17586
		
17586
 
17587
			#回傳結果
17587
			#回傳結果
17588
			return $result;
17588
			return $result;
17589
		
17589
 
17590
			}#if end
17590
			}#if end
17591
		
17591
 
17592
		#取得使用者名稱
17592
		#取得使用者名稱
17593
		$user=$receivedData->user;
17593
		$user=$receivedData->user;
17594
		
17594
 
17595
		#取得密碼或金鑰內容
17595
		#取得密碼或金鑰內容
17596
		$secrect=$receivedData->secrect;
17596
		$secrect=$receivedData->secrect;
17597
		
17597
 
17598
		#依照驗證方式
17598
		#依照驗證方式
17599
		switch($receivedData->type){
17599
		switch($receivedData->type){
17600
		
17600
 
17601
			#若是密碼認證
17601
			#若是密碼認證
17602
			case "password":
17602
			case "password":
17603
			
17603
 
17604
				#測試 $user 與 password 的組合是否能夠認證.
17604
				#測試 $user 與 password 的組合是否能夠認證.
17605
				#函式說明:
17605
				#函式說明:
17606
				#驗證Linux使用者的密碼是否正確.
17606
				#驗證Linux使用者的密碼是否正確.
17607
				#回傳結果:
17607
				#回傳結果:
17608
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常. 
17608
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
17609
				#$result["function"],當前執行的函數名稱.
17609
				#$result["function"],當前執行的函數名稱.
17610
				#$result["error"],錯誤訊息陣列.
17610
				#$result["error"],錯誤訊息陣列.
17611
				#$result["warninig"],警告訊息陣列.
17611
				#$result["warninig"],警告訊息陣列.
17612
				#$result["valid"],是否為存在的使用者且密碼正確.
17612
				#$result["valid"],是否為存在的使用者且密碼正確.
17613
				#必填參數:
17613
				#必填參數:
Line 17623... Line 17623...
17623
				#無.
17623
				#無.
17624
				#備註:
17624
				#備註:
17625
				#僅能在命令列環境下運行.
17625
				#僅能在命令列環境下運行.
17626
				$validUser=authenticate::validUser($conf["authenticate::validUser"]);
17626
				$validUser=authenticate::validUser($conf["authenticate::validUser"]);
17627
				unset($conf["authenticate::validUser"]);
17627
				unset($conf["authenticate::validUser"]);
17628
			
17628
 
17629
				#如果執行異常
17629
				#如果執行異常
17630
				if($validUser["status"]==="false"){
17630
				if($validUser["status"]==="false"){
17631
				
17631
 
17632
					#設置異常
17632
					#設置異常
17633
					$result["status"]="false";
17633
					$result["status"]="false";
17634
				
17634
 
17635
					#設置錯誤訊息
17635
					#設置錯誤訊息
17636
					$result["error"]=$validUser;
17636
					$result["error"]=$validUser;
17637
				
17637
 
17638
					#回傳結果
17638
					#回傳結果
17639
					return $result;
17639
					return $result;
17640
				
17640
 
17641
					}#if end
17641
					}#if end
17642
					
17642
 
17643
				#如果認證失敗
17643
				#如果認證失敗
17644
				if($validUser["valid"]==="false"){
17644
				if($validUser["valid"]==="false"){
17645
				
17645
 
17646
					#記錄該 client 認證失敗的事件
17646
					#記錄該 client 認證失敗的事件
17647
					$allConn[$clientIndex]["authFailed"][]=array("time"=>time(),"user"=>$user);
17647
					$allConn[$clientIndex]["authFailed"][]=array("time"=>time(),"user"=>$user);
17648
					
17648
 
17649
					#針對每次認證失敗的記錄
17649
					#針對每次認證失敗的記錄
17650
					for($i=0;$i<count($allConn[$clientIndex]["authFailed"]);$i++){
17650
					for($i=0;$i<count($allConn[$clientIndex]["authFailed"]);$i++){
17651
					
17651
 
17652
						#如果有下一個事件
17652
						#如果有下一個事件
17653
						if(isset($allConn[$clientIndex]["authFailed"][$i+1])){
17653
						if(isset($allConn[$clientIndex]["authFailed"][$i+1])){
17654
						
17654
 
17655
							#如果本次跟下次事件的時間間隔小於60秒
17655
							#如果本次跟下次事件的時間間隔小於60秒
17656
							if($allConn[$clientIndex]["authFailed"][$i+1]["time"] - $allConn[$clientIndex]["authFailed"][$i]["time"] > 60){
17656
							if($allConn[$clientIndex]["authFailed"][$i+1]["time"] - $allConn[$clientIndex]["authFailed"][$i]["time"] > 60){
17657
							
17657
 
17658
								#移除本次記錄
17658
								#移除本次記錄
17659
								array_shift($allConn[$clientIndex]["authFailed"]);
17659
								array_shift($allConn[$clientIndex]["authFailed"]);
17660
								
17660
 
17661
								#新記錄的索引減少了1,因此更新$i
17661
								#新記錄的索引減少了1,因此更新$i
17662
								$i--;
17662
								$i--;
17663
							
17663
 
17664
								}#if end
17664
								}#if end
17665
						
17665
 
17666
							}#if end
17666
							}#if end
17667
					
17667
 
17668
						}#foreach end
17668
						}#foreach end
17669
						
17669
 
17670
					#如果總事件次數大於3
17670
					#如果總事件次數大於3
17671
					if(count($allConn[$clientIndex]["authFailed"])>3){
17671
					if(count($allConn[$clientIndex]["authFailed"])>3){
17672
					
17672
 
17673
						#設置異常
17673
						#設置異常
17674
						$result["status"]="false";
17674
						$result["status"]="false";
17675
						
17675
 
17676
						#設置錯誤訊息
17676
						#設置錯誤訊息
17677
						$result["error"]=$allConn[$clientIndex]["authFailed"];
17677
						$result["error"]=$allConn[$clientIndex]["authFailed"];
17678
						
17678
 
17679
						#回傳結果
17679
						#回傳結果
17680
						return $result;
17680
						return $result;
17681
					
17681
 
17682
						}#if end
17682
						}#if end
17683
				
17683
 
17684
					#設置正常
17684
					#設置正常
17685
					$result["status"]="true";
17685
					$result["status"]="true";
17686
				
17686
 
17687
					#回傳結果
17687
					#回傳結果
17688
					return $result;
17688
					return $result;
17689
				
17689
 
17690
					}#if end
17690
					}#if end
17691
					
17691
 
17692
				#執行到這邊代表使用者認證通過
17692
				#執行到這邊代表使用者認證通過
17693
				
17693
 
17694
				#依照 user 取得其所屬 group
17694
				#依照 user 取得其所屬 group
17695
				#函式說明:
17695
				#函式說明:
17696
				#id指令來取得目標賬號的群組資訊
17696
				#id指令來取得目標賬號的群組資訊
17697
				#回傳結果:
17697
				#回傳結果:
17698
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
17698
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 17711... Line 17711...
17711
				#無.
17711
				#無.
17712
				#備註:
17712
				#備註:
17713
				#無.
17713
				#無.
17714
				$getUserGroup=authenticate::getUserGroup($conf["authenticate::getUserGroup"]);
17714
				$getUserGroup=authenticate::getUserGroup($conf["authenticate::getUserGroup"]);
17715
				unset($conf["authenticate::getUserGroup"]);
17715
				unset($conf["authenticate::getUserGroup"]);
17716
				
17716
 
17717
				#如果執行異常
17717
				#如果執行異常
17718
				if($getUserGroup["status"]==="false"){
17718
				if($getUserGroup["status"]==="false"){
17719
				
17719
 
17720
					#設置異常
17720
					#設置異常
17721
					$result["status"]="false";
17721
					$result["status"]="false";
17722
				
17722
 
17723
					#設置錯誤訊息
17723
					#設置錯誤訊息
17724
					$result["error"]=$getUserGroup;
17724
					$result["error"]=$getUserGroup;
17725
				
17725
 
17726
					#回傳結果
17726
					#回傳結果
17727
					return $result;
17727
					return $result;
17728
				
17728
 
17729
					}#if end
17729
					}#if end
17730
				
17730
 
17731
				#設置預設的權限為 "false"
17731
				#設置預設的權限為 "false"
17732
				$authStatus="false";
17732
				$authStatus="false";
17733
				
17733
 
17734
				#針對每個取得的群組名稱
17734
				#針對每個取得的群組名稱
17735
				foreach($getUserGroup["content"]["belongGroupName"] as $groupName){
17735
				foreach($getUserGroup["content"]["belongGroupName"] as $groupName){
17736
				
17736
 
17737
					#如果是 root
17737
					#如果是 root
17738
					if($groupName==="root"){
17738
					if($groupName==="root"){
17739
					
17739
 
17740
						#設置得到的權限
17740
						#設置得到的權限
17741
						$authStatus=$groupName;
17741
						$authStatus=$groupName;
17742
					
17742
 
17743
						#結束 foreach
17743
						#結束 foreach
17744
						break;
17744
						break;
17745
					
17745
 
17746
						}#if end
17746
						}#if end
17747
						
17747
 
17748
					#反之如果是公開的使用者
17748
					#反之如果是公開的使用者
17749
					else if($groupName==="public"){
17749
					else if($groupName==="public"){
17750
					
17750
 
17751
						#設置得到的權限
17751
						#設置得到的權限
17752
						$authStatus=$groupName;
17752
						$authStatus=$groupName;
17753
						
17753
 
17754
						}#if end
17754
						}#if end
17755
					
17755
 
17756
					#其他群組名稱
17756
					#其他群組名稱
17757
					else{
17757
					else{
17758
					
17758
 
17759
						#跳過
17759
						#跳過
17760
						continue;
17760
						continue;
17761
					
17761
 
17762
						}#else end
17762
						}#else end
17763
				
17763
 
17764
					}#foreach end
17764
					}#foreach end
17765
				
17765
 
17766
				#設置正常
17766
				#設置正常
17767
				$result["status"]="true";
17767
				$result["status"]="true";
17768
			
17768
 
17769
				#回傳結果
17769
				#回傳結果
17770
				return $result;
17770
				return $result;
17771
			
17771
 
17772
				#跳出 switch
17772
				#跳出 switch
17773
				break;
17773
				break;
17774
		
17774
 
17775
			#若是金鑰認證
17775
			#若是金鑰認證
17776
			case "key":
17776
			case "key":
17777
			
17777
 
17778
				#測試 $user 與 key 的組合是否能夠認證.
17778
				#測試 $user 與 key 的組合是否能夠認證.
17779
				#函式說明:
17779
				#函式說明:
17780
				#驗證Linux使用者的密碼或ssh private key是否正確.
17780
				#驗證Linux使用者的密碼或ssh private key是否正確.
17781
				#回傳結果:
17781
				#回傳結果:
17782
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常. 
17782
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
17783
				#$result["function"],當前執行的函數名稱.
17783
				#$result["function"],當前執行的函數名稱.
17784
				#$result["error"],錯誤訊息陣列.
17784
				#$result["error"],錯誤訊息陣列.
17785
				#$result["warninig"],警告訊息陣列.
17785
				#$result["warninig"],警告訊息陣列.
17786
				#$result["valid"],是否為存在的使用者且密碼正確.
17786
				#$result["valid"],是否為存在的使用者且密碼正確.
17787
				#必填參數:
17787
				#必填參數:
Line 17799... Line 17799...
17799
				#備註:
17799
				#備註:
17800
				#僅能在命令列環境下運.
17800
				#僅能在命令列環境下運.
17801
				#目前僅支援驗證執行php端的使用者.
17801
				#目前僅支援驗證執行php端的使用者.
17802
				$validUser=authenticate::validUser($conf["authenticate::validUser"]);
17802
				$validUser=authenticate::validUser($conf["authenticate::validUser"]);
17803
				unset($conf["authenticate::validUser"]);
17803
				unset($conf["authenticate::validUser"]);
17804
			
17804
 
17805
				#如果執行異常
17805
				#如果執行異常
17806
				if($validUser["status"]==="false"){
17806
				if($validUser["status"]==="false"){
17807
				
17807
 
17808
					#設置異常
17808
					#設置異常
17809
					$result["status"]="false";
17809
					$result["status"]="false";
17810
				
17810
 
17811
					#設置錯誤訊息
17811
					#設置錯誤訊息
17812
					$result["error"]=$validUser;
17812
					$result["error"]=$validUser;
17813
				
17813
 
17814
					#回傳結果
17814
					#回傳結果
17815
					return $result;
17815
					return $result;
17816
				
17816
 
17817
					}#if end
17817
					}#if end
17818
					
17818
 
17819
				#如果認證失敗
17819
				#如果認證失敗
17820
				if($validUser["valid"]==="false"){
17820
				if($validUser["valid"]==="false"){
17821
				
17821
 
17822
					#記錄該 client 認證失敗的事件
17822
					#記錄該 client 認證失敗的事件
17823
					$allConn[$clientIndex]["authFailed"][]=array("time"=>time(),"user"=>$user);
17823
					$allConn[$clientIndex]["authFailed"][]=array("time"=>time(),"user"=>$user);
17824
					
17824
 
17825
					#針對每次認證失敗的記錄
17825
					#針對每次認證失敗的記錄
17826
					for($i=0;$i<count($allConn[$clientIndex]["authFailed"]);$i++){
17826
					for($i=0;$i<count($allConn[$clientIndex]["authFailed"]);$i++){
17827
					
17827
 
17828
						#如果有下一個事件
17828
						#如果有下一個事件
17829
						if(isset($allConn[$clientIndex]["authFailed"][$i+1])){
17829
						if(isset($allConn[$clientIndex]["authFailed"][$i+1])){
17830
						
17830
 
17831
							#如果本次跟下次事件的時間間隔小於60秒
17831
							#如果本次跟下次事件的時間間隔小於60秒
17832
							if($allConn[$clientIndex]["authFailed"][$i+1]["time"] - $allConn[$clientIndex]["authFailed"][$i]["time"] > 60){
17832
							if($allConn[$clientIndex]["authFailed"][$i+1]["time"] - $allConn[$clientIndex]["authFailed"][$i]["time"] > 60){
17833
							
17833
 
17834
								#移除本次記錄
17834
								#移除本次記錄
17835
								array_shift($allConn[$clientIndex]["authFailed"]);
17835
								array_shift($allConn[$clientIndex]["authFailed"]);
17836
								
17836
 
17837
								#新記錄的索引減少了1,因此更新$i
17837
								#新記錄的索引減少了1,因此更新$i
17838
								$i--;
17838
								$i--;
17839
							
17839
 
17840
								}#if end
17840
								}#if end
17841
						
17841
 
17842
							}#if end
17842
							}#if end
17843
					
17843
 
17844
						}#foreach end
17844
						}#foreach end
17845
						
17845
 
17846
					#如果總事件次數大於3
17846
					#如果總事件次數大於3
17847
					if(count($allConn[$clientIndex]["authFailed"])>3){
17847
					if(count($allConn[$clientIndex]["authFailed"])>3){
17848
					
17848
 
17849
						#設置異常
17849
						#設置異常
17850
						$result["status"]="false";
17850
						$result["status"]="false";
17851
						
17851
 
17852
						#設置錯誤訊息
17852
						#設置錯誤訊息
17853
						$result["error"]=$allConn[$clientIndex]["authFailed"];
17853
						$result["error"]=$allConn[$clientIndex]["authFailed"];
17854
						
17854
 
17855
						#回傳結果
17855
						#回傳結果
17856
						return $result;
17856
						return $result;
17857
					
17857
 
17858
						}#if end
17858
						}#if end
17859
				
17859
 
17860
					#設置正常
17860
					#設置正常
17861
					$result["status"]="true";
17861
					$result["status"]="true";
17862
				
17862
 
17863
					#回傳結果
17863
					#回傳結果
17864
					return $result;
17864
					return $result;
17865
				
17865
 
17866
					}#if end
17866
					}#if end
17867
					
17867
 
17868
				#執行到這邊代表使用者認證通過
17868
				#執行到這邊代表使用者認證通過
17869
				
17869
 
17870
				#依照 user 取得其所屬 group
17870
				#依照 user 取得其所屬 group
17871
				#函式說明:
17871
				#函式說明:
17872
				#id指令來取得目標賬號的群組資訊
17872
				#id指令來取得目標賬號的群組資訊
17873
				#回傳結果:
17873
				#回傳結果:
17874
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
17874
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 17887... Line 17887...
17887
				#無.
17887
				#無.
17888
				#備註:
17888
				#備註:
17889
				#無.
17889
				#無.
17890
				$getUserGroup=authenticate::getUserGroup($conf["authenticate::getUserGroup"]);
17890
				$getUserGroup=authenticate::getUserGroup($conf["authenticate::getUserGroup"]);
17891
				unset($conf["authenticate::getUserGroup"]);
17891
				unset($conf["authenticate::getUserGroup"]);
17892
				
17892
 
17893
				#如果執行異常
17893
				#如果執行異常
17894
				if($getUserGroup["status"]==="false"){
17894
				if($getUserGroup["status"]==="false"){
17895
				
17895
 
17896
					#設置異常
17896
					#設置異常
17897
					$result["status"]="false";
17897
					$result["status"]="false";
17898
				
17898
 
17899
					#設置錯誤訊息
17899
					#設置錯誤訊息
17900
					$result["error"]=$getUserGroup;
17900
					$result["error"]=$getUserGroup;
17901
				
17901
 
17902
					#回傳結果
17902
					#回傳結果
17903
					return $result;
17903
					return $result;
17904
				
17904
 
17905
					}#if end
17905
					}#if end
17906
				
17906
 
17907
				#設置預設的權限為 "false"
17907
				#設置預設的權限為 "false"
17908
				$authStatus="false";
17908
				$authStatus="false";
17909
				
17909
 
17910
				#針對每個取得的群組名稱
17910
				#針對每個取得的群組名稱
17911
				foreach($getUserGroup["content"]["belongGroupName"] as $groupName){
17911
				foreach($getUserGroup["content"]["belongGroupName"] as $groupName){
17912
				
17912
 
17913
					#如果是 root
17913
					#如果是 root
17914
					if($groupName==="root"){
17914
					if($groupName==="root"){
17915
					
17915
 
17916
						#設置得到的權限
17916
						#設置得到的權限
17917
						$authStatus=$groupName;
17917
						$authStatus=$groupName;
17918
					
17918
 
17919
						#結束 foreach
17919
						#結束 foreach
17920
						break;
17920
						break;
17921
					
17921
 
17922
						}#if end
17922
						}#if end
17923
						
17923
 
17924
					#反之如果是公開的使用者
17924
					#反之如果是公開的使用者
17925
					else if($groupName==="public"){
17925
					else if($groupName==="public"){
17926
					
17926
 
17927
						#設置得到的權限
17927
						#設置得到的權限
17928
						$authStatus=$groupName;
17928
						$authStatus=$groupName;
17929
						
17929
 
17930
						}#if end
17930
						}#if end
17931
					
17931
 
17932
					#其他群組名稱
17932
					#其他群組名稱
17933
					else{
17933
					else{
17934
					
17934
 
17935
						#跳過
17935
						#跳過
17936
						continue;
17936
						continue;
17937
					
17937
 
17938
						}#else end
17938
						}#else end
17939
				
17939
 
17940
					}#foreach end
17940
					}#foreach end
17941
					
17941
 
17942
				#設置正常
17942
				#設置正常
17943
				$result["status"]="true";
17943
				$result["status"]="true";
17944
			
17944
 
17945
				#回傳結果
17945
				#回傳結果
17946
				return $result;
17946
				return $result;
17947
			
17947
 
17948
				#跳出 switch
17948
				#跳出 switch
17949
				break;
17949
				break;
17950
				
17950
 
17951
			#其他類型
17951
			#其他類型
17952
			default:
17952
			default:
17953
			
17953
 
17954
				#設置不適用
17954
				#設置不適用
17955
				$result["status"]="continue";
17955
				$result["status"]="continue";
17956
			
17956
 
17957
				#回傳結果
17957
				#回傳結果
17958
				return $result;
17958
				return $result;
17959
		
17959
 
17960
			}#switch end
17960
			}#switch end
17961
		
17961
 
17962
		#執行到這邊代表異常
17962
		#執行到這邊代表異常
17963
 
17963
 
17964
		#設置異常
17964
		#設置異常
17965
		$result["status"]="false";
17965
		$result["status"]="false";
17966
 
17966
 
17967
		#回傳結果
17967
		#回傳結果
17968
		return $result;
17968
		return $result;
17969
	
17969
 
17970
		}#function auth end
17970
		}#function auth end
17971
	
17971
 
17972
	/*
17972
	/*
17973
	#函式說明:
17973
	#函式說明:
17974
	#提供webSock::nativeSocketTcpIpServer使用基本功能(basic).
17974
	#提供webSock::nativeSocketTcpIpServer使用基本功能(basic).
17975
	#回傳結果:
17975
	#回傳結果:
17976
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常,"continue"代表不適用.
17976
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常,"continue"代表不適用.
Line 18002... Line 18002...
18002
	#收到用戶的"talkTo?"訊息,會回傳目前在跟誰談話.
18002
	#收到用戶的"talkTo?"訊息,會回傳目前在跟誰談話.
18003
	#收到用戶的"talkTo:client-id"訊息,會建立與id對應的用戶對話,此後傳遞的訊息均會給對方.
18003
	#收到用戶的"talkTo:client-id"訊息,會建立與id對應的用戶對話,此後傳遞的訊息均會給對方.
18004
	#結束連線的訊息"quit".
18004
	#結束連線的訊息"quit".
18005
	*/
18005
	*/
18006
	public static function basic(&$conf){
18006
	public static function basic(&$conf){
18007
		
18007
 
18008
		#初始化要回傳的結果
18008
		#初始化要回傳的結果
18009
		$result=array();
18009
		$result=array();
18010
 
18010
 
18011
		#取得當前執行的函數名稱
18011
		#取得當前執行的函數名稱
18012
		$result["function"]=__FUNCTION__;
18012
		$result["function"]=__FUNCTION__;
Line 18014... Line 18014...
18014
		#涵式說明:
18014
		#涵式說明:
18015
		#判斷當前環境為web還是cmd
18015
		#判斷當前環境為web還是cmd
18016
		#回傳結果:
18016
		#回傳結果:
18017
		#$result,"web"或"cmd"
18017
		#$result,"web"或"cmd"
18018
		if(csInformation::getEnv()==="web"){
18018
		if(csInformation::getEnv()==="web"){
18019
			
18019
 
18020
			#設置執行失敗
18020
			#設置執行失敗
18021
			$result["status"]="false";
18021
			$result["status"]="false";
18022
			
18022
 
18023
			#設置執行錯誤訊息
18023
			#設置執行錯誤訊息
18024
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
18024
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
18025
			
18025
 
18026
			#回傳結果
18026
			#回傳結果
18027
			return $result;
18027
			return $result;
18028
			
18028
 
18029
			}#if end
18029
			}#if end
18030
 
18030
 
18031
		#取得參數
18031
		#取得參數
18032
		$result["argu"]=$conf;
18032
		$result["argu"]=$conf;
18033
 
18033
 
18034
		#如果 $conf 不為陣列
18034
		#如果 $conf 不為陣列
18035
		if(gettype($conf)!="array"){
18035
		if(gettype($conf)!="array"){
18036
		
18036
 
18037
			#設置執行失敗
18037
			#設置執行失敗
18038
			$result["status"]="false";
18038
			$result["status"]="false";
18039
		
18039
 
18040
			#設置執行錯誤訊息
18040
			#設置執行錯誤訊息
18041
			$result["error"][]="\$conf變數須為陣列形態";
18041
			$result["error"][]="\$conf變數須為陣列形態";
18042
		
18042
 
18043
			#如果傳入的參數為 null
18043
			#如果傳入的參數為 null
18044
			if($conf==null){
18044
			if($conf==null){
18045
			
18045
 
18046
				#設置執行錯誤訊息
18046
				#設置執行錯誤訊息
18047
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
18047
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
18048
			
18048
 
18049
				}#if end
18049
				}#if end
18050
 
18050
 
18051
			#回傳結果
18051
			#回傳結果
18052
			return $result;
18052
			return $result;
18053
		
18053
 
18054
			}#if end
18054
			}#if end
18055
		
18055
 
18056
		#檢查參數
18056
		#檢查參數
18057
		#函式說明:
18057
		#函式說明:
18058
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
18058
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
18059
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
18059
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
18060
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
18060
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
Line 18085... Line 18085...
18085
		#$conf["canBeEmpty"]=array();
18085
		#$conf["canBeEmpty"]=array();
18086
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
18086
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
18087
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("wsMode");
18087
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("wsMode");
18088
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
18088
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
18089
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("wsMode","gpgId");
18089
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("wsMode","gpgId");
18090
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double"); 
18090
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
18091
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string");
18091
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string");
18092
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
18092
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
18093
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("true",gnupgId);
18093
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("true",gnupgId);
18094
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
18094
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
18095
		#$conf["arrayCountEqualCheck"][]=array();
18095
		#$conf["arrayCountEqualCheck"][]=array();
Line 18097... Line 18097...
18097
		#array_keys=>http://php.net/manual/en/function.array-keys.php
18097
		#array_keys=>http://php.net/manual/en/function.array-keys.php
18098
		#建議:
18098
		#建議:
18099
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
18099
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
18100
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
18100
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
18101
		unset($conf["variableCheck::checkArguments"]);
18101
		unset($conf["variableCheck::checkArguments"]);
18102
		
18102
 
18103
		#如果檢查參數失敗
18103
		#如果檢查參數失敗
18104
		if($checkArguments["status"]==="false"){
18104
		if($checkArguments["status"]==="false"){
18105
			
18105
 
18106
			#設置執行失敗
18106
			#設置執行失敗
18107
			$result["status"]="false";
18107
			$result["status"]="false";
18108
		
18108
 
18109
			#設置執行錯誤訊息
18109
			#設置執行錯誤訊息
18110
			$result["error"]=$checkArguments;
18110
			$result["error"]=$checkArguments;
18111
			
18111
 
18112
			#回傳結果
18112
			#回傳結果
18113
			return $result;
18113
			return $result;
18114
			
18114
 
18115
			}#if end
18115
			}#if end
18116
			
18116
 
18117
		#如果檢查參數不通過
18117
		#如果檢查參數不通過
18118
		if($checkArguments["passed"]==="false"){
18118
		if($checkArguments["passed"]==="false"){
18119
			
18119
 
18120
			#設置執行失敗
18120
			#設置執行失敗
18121
			$result["status"]="false";
18121
			$result["status"]="false";
18122
		
18122
 
18123
			#設置執行錯誤訊息
18123
			#設置執行錯誤訊息
18124
			$result["error"]=$checkArguments;
18124
			$result["error"]=$checkArguments;
18125
			
18125
 
18126
			#回傳結果
18126
			#回傳結果
18127
			return $result;
18127
			return $result;
18128
			
18128
 
18129
			}#if end
18129
			}#if end
18130
 
18130
 
18131
		#取得收到的訊息
18131
		#取得收到的訊息
18132
		$receivedData=$conf["data"];
18132
		$receivedData=$conf["data"];
18133
		
18133
 
18134
		#取得 server 的 socket
18134
		#取得 server 的 socket
18135
		$serverSocket=$conf["serverSock"];
18135
		$serverSocket=$conf["serverSock"];
18136
		
18136
 
18137
		#取得 client 的 socket
18137
		#取得 client 的 socket
18138
		$clientSocket=$conf["clientSock"];
18138
		$clientSocket=$conf["clientSock"];
18139
		
18139
 
18140
		#取得 client 的資訊
18140
		#取得 client 的資訊
18141
		$clientInfo=$conf["clientInfo"];
18141
		$clientInfo=$conf["clientInfo"];
18142
		
18142
 
18143
		#取得 client 的索引
18143
		#取得 client 的索引
18144
		$clientIndex=$conf["clientIndex"];
18144
		$clientIndex=$conf["clientIndex"];
18145
		
18145
 
18146
		#取得 all clients 的資訊
18146
		#取得 all clients 的資訊
18147
		$allConn=&$conf["allConn"];
18147
		$allConn=&$conf["allConn"];
18148
	
18148
 
18149
		#hex2bin
18149
		#hex2bin
18150
		#函式說明:
18150
		#函式說明:
18151
		#加密或編碼字串,可以用的方法有sha1,md5,password_sha,qbpwcf,bin2hex,hex2bin,sha1可以回傳20或40的數值字串,md5可以回傳32個數值字串,password_hash可以回傳60~255個字元.
18151
		#加密或編碼字串,可以用的方法有sha1,md5,password_sha,qbpwcf,bin2hex,hex2bin,sha1可以回傳20或40的數值字串,md5可以回傳32個數值字串,password_hash可以回傳60~255個字元.
18152
		#回傳結果:
18152
		#回傳結果:
18153
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常. 
18153
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
18154
		#$result["function"],當前執行的函數名稱.
18154
		#$result["function"],當前執行的函數名稱.
18155
		#$result["content"],加密後的結果.
18155
		#$result["content"],加密後的結果.
18156
		#$result["error"],錯誤訊息陣列.
18156
		#$result["error"],錯誤訊息陣列.
18157
		#$result["argu"],使用的參數.
18157
		#$result["argu"],使用的參數.
18158
		#必填參數:
18158
		#必填參數:
Line 18200... Line 18200...
18200
 
18200
 
18201
			}#if end
18201
			}#if end
18202
 
18202
 
18203
		#取得hex2bin後的字串
18203
		#取得hex2bin後的字串
18204
		$receivedData=$enCodeStr["content"];
18204
		$receivedData=$enCodeStr["content"];
18205
		
18205
 
18206
		#gpg decrypt
18206
		#gpg decrypt
18207
		#函式說明:
18207
		#函式說明:
18208
		#加密或編碼字串,可以用的方法有sha1,md5,password_sha,qbpwcf,bin2hex,hex2bin,sha1可以回傳20或40的數值字串,md5可以回傳32個數值字串,password_hash可以回傳60~255個字元.
18208
		#加密或編碼字串,可以用的方法有sha1,md5,password_sha,qbpwcf,bin2hex,hex2bin,sha1可以回傳20或40的數值字串,md5可以回傳32個數值字串,password_hash可以回傳60~255個字元.
18209
		#回傳結果:
18209
		#回傳結果:
18210
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常. 
18210
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
18211
		#$result["function"],當前執行的函數名稱.
18211
		#$result["function"],當前執行的函數名稱.
18212
		#$result["content"],加密後的結果.
18212
		#$result["content"],加密後的結果.
18213
		#$result["error"],錯誤訊息陣列.
18213
		#$result["error"],錯誤訊息陣列.
18214
		#$result["argu"],使用的參數.
18214
		#$result["argu"],使用的參數.
18215
		#必填參數:
18215
		#必填參數:
Line 18257... Line 18257...
18257
 
18257
 
18258
			}#if end
18258
			}#if end
18259
 
18259
 
18260
		#取得解密後的字串
18260
		#取得解密後的字串
18261
		$receivedData=$enCodeStr["content"];
18261
		$receivedData=$enCodeStr["content"];
18262
		
18262
 
18263
		#json_decode
18263
		#json_decode
18264
		$receivedData=json_decode($receivedData);
18264
		$receivedData=json_decode($receivedData);
18265
	
18265
 
18266
		#如果收到的是 id?
18266
		#如果收到的是 id?
18267
		if($receivedData==="id?"){
18267
		if($receivedData==="id?"){
18268
			
18268
 
18269
			#如果尚未認證
18269
			#如果尚未認證
18270
			if($connections[$conIndex]["auth"]==="false"){
18270
			if($connections[$conIndex]["auth"]==="false"){
18271
			
18271
 
18272
				#設置要回傳的訊息
18272
				#設置要回傳的訊息
18273
				$talkback="permission denied";
18273
				$talkback="permission denied";
18274
			
18274
 
18275
				}#if end
18275
				}#if end
18276
			
18276
 
18277
			else{
18277
			else{
18278
			
18278
 
18279
				#設置要回傳的訊息
18279
				#設置要回傳的訊息
18280
				$talkback=$connections[$conIndex]["id"];
18280
				$talkback=$connections[$conIndex]["id"];
18281
			
18281
 
18282
				}#else end
18282
				}#else end
18283
 
18283
 
18284
			#debug
18284
			#debug
18285
			#var_dump(__LINE__,$talkback);
18285
			#var_dump(__LINE__,$talkback);
18286
 
18286
 
18287
			#json encode 要回傳的訊息
18287
			#json encode 要回傳的訊息
18288
			$talkback=json_encode($talkback);
18288
			$talkback=json_encode($talkback);
18289
				
18289
 
18290
			#debug
18290
			#debug
18291
			#var_dump(__LINE__,$talkback);
18291
			#var_dump(__LINE__,$talkback);
18292
			
18292
 
18293
			#如果是 ws 模式
18293
			#如果是 ws 模式
18294
			if($conf["wsMode"]==="true"){
18294
			if($conf["wsMode"]==="true"){
18295
			
18295
 
18296
				#如果已經 handshake 過
18296
				#如果已經 handshake 過
18297
				if($connections[$conIndex]["handshaked"]==="true"){
18297
				if($connections[$conIndex]["handshaked"]==="true"){
18298
				
18298
 
18299
					#如果要 unmask	
18299
					#如果要 unmask
18300
					if($connections[$conIndex]["unmask"]==="true"){
18300
					if($connections[$conIndex]["unmask"]==="true"){
18301
				
18301
 
18302
						#函式說明:
18302
						#函式說明:
18303
						#加密 handshake 後要傳送的訊息 
18303
						#加密 handshake 後要傳送的訊息
18304
						#回傳結果:
18304
						#回傳結果:
18305
						#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
18305
						#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
18306
						#$result["error"],執行不正常結束的錯訊息陣列.
18306
						#$result["error"],執行不正常結束的錯訊息陣列.
18307
						#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
18307
						#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
18308
						#$result["argu"],陣列,使用的參數.
18308
						#$result["argu"],陣列,使用的參數.
18309
						#必填參數:
18309
						#必填參數:
18310
						#$conf["text"],字串,要加密的訊息.
18310
						#$conf["text"],字串,要加密的訊息.
18311
						$conf["webSock::encode"]["text"]=$talkback; 
18311
						$conf["webSock::encode"]["text"]=$talkback;
18312
						#可省略參數:
18312
						#可省略參數:
18313
						#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
18313
						#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
18314
						#$conf["payloadIsBin"]="false";
18314
						#$conf["payloadIsBin"]="false";
18315
						#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
18315
						#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
18316
						#$conf["ping"]="false";
18316
						#$conf["ping"]="false";
Line 18326... Line 18326...
18326
						#無.
18326
						#無.
18327
						#備註:
18327
						#備註:
18328
						#目前$conf["text"]長度超過125會出錯.
18328
						#目前$conf["text"]長度超過125會出錯.
18329
						$talkback=webSock::encode($conf["webSock::encode"]);
18329
						$talkback=webSock::encode($conf["webSock::encode"]);
18330
						unset($conf["webSock::encode"]);
18330
						unset($conf["webSock::encode"]);
18331
						
18331
 
18332
						#如果執行失敗
18332
						#如果執行失敗
18333
						if($talkback["status"]==="false"){
18333
						if($talkback["status"]==="false"){
18334
						
18334
 
18335
							#印出結果
18335
							#印出結果
18336
							var_dump($talkback);
18336
							var_dump($talkback);
18337
						
18337
 
18338
							#結束執行
18338
							#結束執行
18339
							exit;
18339
							exit;
18340
						
18340
 
18341
							}#if end
18341
							}#if end
18342
						
18342
 
18343
						#debug
18343
						#debug
18344
						#var_dump(__LINE__,$talkback);
18344
						#var_dump(__LINE__,$talkback);
18345
						
18345
 
18346
						}#if end
18346
						}#if end
18347
					
18347
 
18348
					}#if end
18348
					}#if end
18349
			
18349
 
18350
				}#if end
18350
				}#if end
18351
				
18351
 
18352
			#反之不為 web socket 訊息
18352
			#反之不為 web socket 訊息
18353
			else{
18353
			else{
18354
			
18354
 
18355
				#儲存成只有一個訊息
18355
				#儲存成只有一個訊息
18356
				$talkback["content"][]=$talkback;
18356
				$talkback["content"][]=$talkback;
18357
			
18357
 
18358
				}#else
18358
				}#else
18359
			
18359
 
18360
			#針對每個訊息的分段
18360
			#針對每個訊息的分段
18361
			foreach($talkback["content"] as $msg){
18361
			foreach($talkback["content"] as $msg){
18362
				
18362
 
18363
				#debug
18363
				#debug
18364
				var_dump(__LINE__,$msg);
18364
				var_dump(__LINE__,$msg);
18365
				
18365
 
18366
				#回傳訊息
18366
				#回傳訊息
18367
				$socket_write=socket_write($connections[$conIndex]["connect"], $msg, strlen($msg));
18367
				$socket_write=socket_write($connections[$conIndex]["connect"], $msg, strlen($msg));
18368
		
18368
 
18369
				#debug
18369
				#debug
18370
				var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
18370
				var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
18371
				
18371
 
18372
				}#foreach end
18372
				}#foreach end
18373
				
18373
 
18374
			#設置執行正常
18374
			#設置執行正常
18375
			$result["status"]="true";
18375
			$result["status"]="true";
18376
			
18376
 
18377
			#回傳結果
18377
			#回傳結果
18378
			return $result;
18378
			return $result;
18379
			
18379
 
18380
			}#if end
18380
			}#if end
18381
		
18381
 
18382
		#反之如果收到的是 "quit"
18382
		#反之如果收到的是 "quit"
18383
		else if($receivedData==="quit"){
18383
		else if($receivedData==="quit"){
18384
			
18384
 
18385
			#設置要回傳的訊息
18385
			#設置要回傳的訊息
18386
			$talkback="結束與 server 的連線";
18386
			$talkback="結束與 server 的連線";
18387
			
18387
 
18388
			#json encode 要回傳的訊息
18388
			#json encode 要回傳的訊息
18389
			$talkback=json_encode($talkback).PHP_EOL;
18389
			$talkback=json_encode($talkback).PHP_EOL;
18390
			
18390
 
18391
			#如果是 ws 模式
18391
			#如果是 ws 模式
18392
			if($conf["wsMode"]==="true"){
18392
			if($conf["wsMode"]==="true"){
18393
			
18393
 
18394
				#如果已經 handshake 過
18394
				#如果已經 handshake 過
18395
				if($connections[$conIndex]["handshaked"]==="true"){
18395
				if($connections[$conIndex]["handshaked"]==="true"){
18396
				
18396
 
18397
					#如果要 unmask	
18397
					#如果要 unmask
18398
					if($connections[$conIndex]["unmask"]==="true"){
18398
					if($connections[$conIndex]["unmask"]==="true"){
18399
				
18399
 
18400
						#函式說明:
18400
						#函式說明:
18401
						#加密 handshake 後要傳送的訊息 
18401
						#加密 handshake 後要傳送的訊息
18402
						#回傳結果:
18402
						#回傳結果:
18403
						#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
18403
						#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
18404
						#$result["error"],執行不正常結束的錯訊息陣列.
18404
						#$result["error"],執行不正常結束的錯訊息陣列.
18405
						#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
18405
						#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
18406
						#$result["argu"],陣列,使用的參數.
18406
						#$result["argu"],陣列,使用的參數.
18407
						#必填參數:
18407
						#必填參數:
18408
						#$conf["text"],字串,要加密的訊息.
18408
						#$conf["text"],字串,要加密的訊息.
18409
						$conf["webSock::encode"]["text"]=$talkback; 
18409
						$conf["webSock::encode"]["text"]=$talkback;
18410
						#可省略參數:
18410
						#可省略參數:
18411
						#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
18411
						#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
18412
						#$conf["payloadIsBin"]="false";
18412
						#$conf["payloadIsBin"]="false";
18413
						#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
18413
						#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
18414
						#$conf["ping"]="false";
18414
						#$conf["ping"]="false";
Line 18424... Line 18424...
18424
						#無.
18424
						#無.
18425
						#備註:
18425
						#備註:
18426
						#目前$conf["text"]長度超過125會出錯.
18426
						#目前$conf["text"]長度超過125會出錯.
18427
						$talkback=webSock::encode($conf["webSock::encode"]);
18427
						$talkback=webSock::encode($conf["webSock::encode"]);
18428
						unset($conf["webSock::encode"]);
18428
						unset($conf["webSock::encode"]);
18429
						
18429
 
18430
						#如果執行失敗
18430
						#如果執行失敗
18431
						if($talkback["status"]==="false"){
18431
						if($talkback["status"]==="false"){
18432
						
18432
 
18433
							#印出結果
18433
							#印出結果
18434
							var_dump($talkback);
18434
							var_dump($talkback);
18435
						
18435
 
18436
							#結束執行
18436
							#結束執行
18437
							exit;
18437
							exit;
18438
						
18438
 
18439
							}#if end
18439
							}#if end
18440
						
18440
 
18441
						#debug
18441
						#debug
18442
						#var_dump(__LINE__,$talkback);
18442
						#var_dump(__LINE__,$talkback);
18443
						
18443
 
18444
						}#if end
18444
						}#if end
18445
						
18445
 
18446
					}#if end
18446
					}#if end
18447
			
18447
 
18448
				}#if end
18448
				}#if end
18449
				
18449
 
18450
			#反之不為 web socket 訊息
18450
			#反之不為 web socket 訊息
18451
			else{
18451
			else{
18452
			
18452
 
18453
				#儲存成只有一個訊息
18453
				#儲存成只有一個訊息
18454
				$talkback["content"][]=$talkback;
18454
				$talkback["content"][]=$talkback;
18455
			
18455
 
18456
				}#else
18456
				}#else
18457
			
18457
 
18458
			#針對每個訊息的分段
18458
			#針對每個訊息的分段
18459
			foreach($talkback["content"] as $msg){
18459
			foreach($talkback["content"] as $msg){
18460
				
18460
 
18461
				#回傳訊息
18461
				#回傳訊息
18462
				$socket_write=socket_write($con["connect"], $msg, strlen($msg));
18462
				$socket_write=socket_write($con["connect"], $msg, strlen($msg));
18463
		
18463
 
18464
				#debug
18464
				#debug
18465
				#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
18465
				#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
18466
				
18466
 
18467
				}#foreach end
18467
				}#foreach end
18468
				
18468
 
18469
			#關閉被 client 的連線	
18469
			#關閉被 client 的連線
18470
			socket_close($con["connect"]);
18470
			socket_close($con["connect"]);
18471
			
18471
 
18472
			#清理連線的陣列
18472
			#清理連線的陣列
18473
			unset($connections[$conIndex]);
18473
			unset($connections[$conIndex]);
18474
			
18474
 
18475
			#設置執行正常
18475
			#設置執行正常
18476
			$result["status"]="true";
18476
			$result["status"]="true";
18477
			
18477
 
18478
			#回傳結果
18478
			#回傳結果
18479
			return $result;
18479
			return $result;
18480
		
18480
 
18481
			}#if end
18481
			}#if end
18482
		
18482
 
18483
		#反之是如果收到的是 ids?
18483
		#反之是如果收到的是 ids?
18484
		else if($receivedData === 'ids?'){
18484
		else if($receivedData === 'ids?'){
18485
			
18485
 
18486
			#如果尚未認證
18486
			#如果尚未認證
18487
			if($connections[$conIndex]["auth"]==="false"){
18487
			if($connections[$conIndex]["auth"]==="false"){
18488
			
18488
 
18489
				#設置要回傳的訊息
18489
				#設置要回傳的訊息
18490
				$talkback=json_encode("permission denied");
18490
				$talkback=json_encode("permission denied");
18491
			
18491
 
18492
				}#if end
18492
				}#if end
18493
				
18493
 
18494
			#反之
18494
			#反之
18495
			else{
18495
			else{
18496
			
18496
 
18497
				#設置要回傳的訊息
18497
				#設置要回傳的訊息
18498
				$talkback=array();
18498
				$talkback=array();
18499
				
18499
 
18500
				#針對每個連線
18500
				#針對每個連線
18501
				foreach($connections as $conIndex_ids => $con_ids){
18501
				foreach($connections as $conIndex_ids => $con_ids){
18502
 
18502
 
18503
					#如果不是自己
18503
					#如果不是自己
18504
					if($con_ids["connect"]!==$connections[$conIndex]["connect"]){
18504
					if($con_ids["connect"]!==$connections[$conIndex]["connect"]){
18505
					
18505
 
18506
						#記錄其他人的id
18506
						#記錄其他人的id
18507
						$talkback[]=$con_ids["id"];
18507
						$talkback[]=$con_ids["id"];
18508
					
18508
 
18509
						}#if end
18509
						}#if end
18510
				
18510
 
18511
					}#foreach end
18511
					}#foreach end
18512
					
18512
 
18513
				#json encode要回傳的id陣列
18513
				#json encode要回傳的id陣列
18514
				$talkback=json_encode($talkback);
18514
				$talkback=json_encode($talkback);
18515
			
18515
 
18516
				}#else end
18516
				}#else end
18517
			
18517
 
18518
			#如果是 ws 模式
18518
			#如果是 ws 模式
18519
			if($conf["wsMode"]==="true"){
18519
			if($conf["wsMode"]==="true"){
18520
			
18520
 
18521
				#如果已經 handshake 過
18521
				#如果已經 handshake 過
18522
				if($connections[$conIndex]["handshaked"]==="true"){
18522
				if($connections[$conIndex]["handshaked"]==="true"){
18523
				
18523
 
18524
					#如果要 unmask
18524
					#如果要 unmask
18525
					if($connections[$conIndex]["unmask"]==="true"){
18525
					if($connections[$conIndex]["unmask"]==="true"){
18526
				
18526
 
18527
						#函式說明:
18527
						#函式說明:
18528
						#加密 handshake 後要傳送的訊息 
18528
						#加密 handshake 後要傳送的訊息
18529
						#回傳結果:
18529
						#回傳結果:
18530
						#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
18530
						#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
18531
						#$result["error"],執行不正常結束的錯訊息陣列.
18531
						#$result["error"],執行不正常結束的錯訊息陣列.
18532
						#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
18532
						#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
18533
						#$result["argu"],陣列,使用的參數.
18533
						#$result["argu"],陣列,使用的參數.
18534
						#必填參數:
18534
						#必填參數:
18535
						#$conf["text"],字串,要加密的訊息.
18535
						#$conf["text"],字串,要加密的訊息.
18536
						$conf["webSock::encode"]["text"]=$talkback; 
18536
						$conf["webSock::encode"]["text"]=$talkback;
18537
						#可省略參數:
18537
						#可省略參數:
18538
						#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
18538
						#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
18539
						#$conf["payloadIsBin"]="false";
18539
						#$conf["payloadIsBin"]="false";
18540
						#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
18540
						#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
18541
						#$conf["ping"]="false";
18541
						#$conf["ping"]="false";
Line 18551... Line 18551...
18551
						#無.
18551
						#無.
18552
						#備註:
18552
						#備註:
18553
						#目前$conf["text"]長度超過125會出錯.
18553
						#目前$conf["text"]長度超過125會出錯.
18554
						$talkback=webSock::encode($conf["webSock::encode"]);
18554
						$talkback=webSock::encode($conf["webSock::encode"]);
18555
						unset($conf["webSock::encode"]);
18555
						unset($conf["webSock::encode"]);
18556
						
18556
 
18557
						#如果執行失敗
18557
						#如果執行失敗
18558
						if($talkback["status"]==="false"){
18558
						if($talkback["status"]==="false"){
18559
						
18559
 
18560
							#印出結果
18560
							#印出結果
18561
							var_dump($talkback);
18561
							var_dump($talkback);
18562
						
18562
 
18563
							#結束執行
18563
							#結束執行
18564
							exit;
18564
							exit;
18565
						
18565
 
18566
							}#if end
18566
							}#if end
18567
						
18567
 
18568
						#debug
18568
						#debug
18569
						#var_dump(__LINE__,$talkback);
18569
						#var_dump(__LINE__,$talkback);
18570
						
18570
 
18571
						}#if end
18571
						}#if end
18572
					
18572
 
18573
					}#if end
18573
					}#if end
18574
			
18574
 
18575
				}#if end
18575
				}#if end
18576
			
18576
 
18577
			#反之不為 web socket 訊息
18577
			#反之不為 web socket 訊息
18578
			else{
18578
			else{
18579
			
18579
 
18580
				#儲存成只有一個訊息
18580
				#儲存成只有一個訊息
18581
				$talkback["content"][]=$talkback;
18581
				$talkback["content"][]=$talkback;
18582
			
18582
 
18583
				}#else
18583
				}#else
18584
			
18584
 
18585
			#針對每個訊息的分段
18585
			#針對每個訊息的分段
18586
			foreach($talkback["content"] as $msg){
18586
			foreach($talkback["content"] as $msg){
18587
				
18587
 
18588
				#回傳訊息
18588
				#回傳訊息
18589
				$socket_write=socket_write($connections[$conIndex]["connect"], $msg, strlen($msg));
18589
				$socket_write=socket_write($connections[$conIndex]["connect"], $msg, strlen($msg));
18590
		
18590
 
18591
				#debug
18591
				#debug
18592
				var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
18592
				var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
18593
				
18593
 
18594
				}#foreach end
18594
				}#foreach end
18595
		
18595
 
18596
			#設置執行正常
18596
			#設置執行正常
18597
			$result["status"]="true";
18597
			$result["status"]="true";
18598
			
18598
 
18599
			#回傳結果
18599
			#回傳結果
18600
			return $result;
18600
			return $result;
18601
			
18601
 
18602
			}#if end
18602
			}#if end
18603
		
18603
 
18604
		#反之如果收到 "talkTo?"
18604
		#反之如果收到 "talkTo?"
18605
		else if($receivedData==="talkTo?"){
18605
		else if($receivedData==="talkTo?"){
18606
			
18606
 
18607
			#如果尚未認證
18607
			#如果尚未認證
18608
			if($connections[$conIndex]["auth"]==="false"){
18608
			if($connections[$conIndex]["auth"]==="false"){
18609
			
18609
 
18610
				#設置要回傳的訊息
18610
				#設置要回傳的訊息
18611
				$talkback=json_encode("permission denied");
18611
				$talkback=json_encode("permission denied");
18612
			
18612
 
18613
				}#if end
18613
				}#if end
18614
			
18614
 
18615
			#反之
18615
			#反之
18616
			else{
18616
			else{
18617
			
18617
 
18618
				#初始化要講話的id陣列
18618
				#初始化要講話的id陣列
18619
				$talkback=array();
18619
				$talkback=array();
18620
				
18620
 
18621
				#有幾個要講話的人就執行幾次
18621
				#有幾個要講話的人就執行幾次
18622
				foreach($connections[$conIndex]["talkTo"] as $to){
18622
				foreach($connections[$conIndex]["talkTo"] as $to){
18623
					
18623
 
18624
					#串街要講話的人
18624
					#串街要講話的人
18625
					$talkback[]=$to;
18625
					$talkback[]=$to;
18626
					
18626
 
18627
					}#foreach end
18627
					}#foreach end
18628
				
18628
 
18629
				#設置要回傳的訊息
18629
				#設置要回傳的訊息
18630
				$talkback=json_encode($talkback).PHP_EOL;
18630
				$talkback=json_encode($talkback).PHP_EOL;
18631
			
18631
 
18632
				}#else end
18632
				}#else end
18633
			
18633
 
18634
			#如果是 ws 模式
18634
			#如果是 ws 模式
18635
			if($conf["wsMode"]==="true"){
18635
			if($conf["wsMode"]==="true"){
18636
			
18636
 
18637
				#如果已經 handshake 過
18637
				#如果已經 handshake 過
18638
				if($connections[$conIndex]["handshaked"]==="true"){
18638
				if($connections[$conIndex]["handshaked"]==="true"){
18639
				
18639
 
18640
					#如果要 unmask	
18640
					#如果要 unmask
18641
					if($connections[$conIndex]["unmask"]==="true"){
18641
					if($connections[$conIndex]["unmask"]==="true"){
18642
				
18642
 
18643
						#函式說明:
18643
						#函式說明:
18644
						#加密 handshake 後要傳送的訊息 
18644
						#加密 handshake 後要傳送的訊息
18645
						#回傳結果:
18645
						#回傳結果:
18646
						#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
18646
						#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
18647
						#$result["error"],執行不正常結束的錯訊息陣列.
18647
						#$result["error"],執行不正常結束的錯訊息陣列.
18648
						#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
18648
						#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
18649
						#$result["argu"],陣列,使用的參數.
18649
						#$result["argu"],陣列,使用的參數.
18650
						#必填參數:
18650
						#必填參數:
18651
						#$conf["text"],字串,要加密的訊息.
18651
						#$conf["text"],字串,要加密的訊息.
18652
						$conf["webSock::encode"]["text"]=$talkback; 
18652
						$conf["webSock::encode"]["text"]=$talkback;
18653
						#可省略參數:
18653
						#可省略參數:
18654
						#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
18654
						#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
18655
						#$conf["payloadIsBin"]="false";
18655
						#$conf["payloadIsBin"]="false";
18656
						#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
18656
						#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
18657
						#$conf["ping"]="false";
18657
						#$conf["ping"]="false";
Line 18667... Line 18667...
18667
						#無.
18667
						#無.
18668
						#備註:
18668
						#備註:
18669
						#目前$conf["text"]長度超過125會出錯.
18669
						#目前$conf["text"]長度超過125會出錯.
18670
						$talkback=webSock::encode($conf["webSock::encode"]);
18670
						$talkback=webSock::encode($conf["webSock::encode"]);
18671
						unset($conf["webSock::encode"]);
18671
						unset($conf["webSock::encode"]);
18672
						
18672
 
18673
						#如果執行失敗
18673
						#如果執行失敗
18674
						if($talkback["status"]==="false"){
18674
						if($talkback["status"]==="false"){
18675
						
18675
 
18676
							#印出結果
18676
							#印出結果
18677
							var_dump($talkback);
18677
							var_dump($talkback);
18678
						
18678
 
18679
							#結束執行
18679
							#結束執行
18680
							exit;
18680
							exit;
18681
						
18681
 
18682
							}#if end
18682
							}#if end
18683
						
18683
 
18684
						#debug
18684
						#debug
18685
						#var_dump(__LINE__,$talkback);
18685
						#var_dump(__LINE__,$talkback);
18686
						
18686
 
18687
						}#if end
18687
						}#if end
18688
						
18688
 
18689
					}#if end
18689
					}#if end
18690
			
18690
 
18691
				}#if end
18691
				}#if end
18692
			
18692
 
18693
			#反之不為 web socket 訊息
18693
			#反之不為 web socket 訊息
18694
			else{
18694
			else{
18695
			
18695
 
18696
				#儲存成只有一個訊息
18696
				#儲存成只有一個訊息
18697
				$talkback["content"][]=$talkback;
18697
				$talkback["content"][]=$talkback;
18698
			
18698
 
18699
				}#else
18699
				}#else
18700
			
18700
 
18701
			#針對每個訊息的分段
18701
			#針對每個訊息的分段
18702
			foreach($talkback["content"] as $msg){
18702
			foreach($talkback["content"] as $msg){
18703
				
18703
 
18704
				#回傳訊息
18704
				#回傳訊息
18705
				$socket_write=socket_write($connections[$conIndex]["connect"], $msg, strlen($msg));
18705
				$socket_write=socket_write($connections[$conIndex]["connect"], $msg, strlen($msg));
18706
		
18706
 
18707
				#debug
18707
				#debug
18708
				#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
18708
				#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
18709
				
18709
 
18710
				}#foreach end
18710
				}#foreach end
18711
			
18711
 
18712
			#設置執行正常
18712
			#設置執行正常
18713
			$result["status"]="true";
18713
			$result["status"]="true";
18714
			
18714
 
18715
			#回傳結果
18715
			#回傳結果
18716
			return $result;
18716
			return $result;
18717
			
18717
 
18718
			}#if end
18718
			}#if end
18719
			
18719
 
18720
		#如果收到的訊息長度大於 "talkTo:"
18720
		#如果收到的訊息長度大於 "talkTo:"
18721
		if(strlen($receivedData)>strlen("talkTo:")){
18721
		if(strlen($receivedData)>strlen("talkTo:")){
18722
			
18722
 
18723
			#如果收到開頭為 "talkTo:"
18723
			#如果收到開頭為 "talkTo:"
18724
			#涵式說明:
18724
			#涵式說明:
18725
			#取得符合特定字首與字尾的字串
18725
			#取得符合特定字首與字尾的字串
18726
			#回傳的結果:
18726
			#回傳的結果:
18727
			#$result["status"],若爲"true"則代表執行正常;若爲"false"則代表執行失敗。
18727
			#$result["status"],若爲"true"則代表執行正常;若爲"false"則代表執行失敗。
Line 18739... Line 18739...
18739
			#$conf["tailWord"]="";
18739
			#$conf["tailWord"]="";
18740
			#參考資料:
18740
			#參考資料:
18741
			#str_spilt(),可以將字串依照字母分割成一個個陣列字串。
18741
			#str_spilt(),可以將字串依照字母分割成一個個陣列字串。
18742
			$getMeetConditionsString=search::getMeetConditionsString($conf["search::getMeetConditionsString"]);
18742
			$getMeetConditionsString=search::getMeetConditionsString($conf["search::getMeetConditionsString"]);
18743
			unset($conf["search::getMeetConditionsString"]);
18743
			unset($conf["search::getMeetConditionsString"]);
18744
				
18744
 
18745
			#如果選找前置字串 "talkTo:" 失敗
18745
			#如果選找前置字串 "talkTo:" 失敗
18746
			if($getMeetConditionsString["status"]==="false"){
18746
			if($getMeetConditionsString["status"]==="false"){
18747
				
18747
 
18748
				#設置執行失敗
18748
				#設置執行失敗
18749
				$result["status"]="false";
18749
				$result["status"]="false";
18750
			
18750
 
18751
				#設置執行錯誤訊息
18751
				#設置執行錯誤訊息
18752
				$result["error"]=$getMeetConditionsString;
18752
				$result["error"]=$getMeetConditionsString;
18753
				
18753
 
18754
				#回傳結果
18754
				#回傳結果
18755
				return $result;
18755
				return $result;
18756
				
18756
 
18757
				}#if end
18757
				}#if end
18758
				
18758
 
18759
			#如果存在 "talkTo:" 前置字串
18759
			#如果存在 "talkTo:" 前置字串
18760
			if($getMeetConditionsString["founded"]==="true"){
18760
			if($getMeetConditionsString["founded"]==="true"){
18761
				
18761
 
18762
				#如果尚未認證
18762
				#如果尚未認證
18763
				if($connections[$conIndex]["auth"]==="false"){
18763
				if($connections[$conIndex]["auth"]==="false"){
18764
				
18764
 
18765
					#設置要回傳的訊息
18765
					#設置要回傳的訊息
18766
					$talkback=json_encode("permission denied");
18766
					$talkback=json_encode("permission denied");
18767
					
18767
 
18768
					#如果是 ws 模式
18768
					#如果是 ws 模式
18769
					if($conf["wsMode"]==="true"){
18769
					if($conf["wsMode"]==="true"){
18770
					
18770
 
18771
						#如果已經 handshake 過
18771
						#如果已經 handshake 過
18772
						if($connections[$conIndex]["handshaked"]==="true"){
18772
						if($connections[$conIndex]["handshaked"]==="true"){
18773
						
18773
 
18774
							#如果要 unmask	
18774
							#如果要 unmask
18775
							if($connections[$conIndex]["unmask"]==="true"){
18775
							if($connections[$conIndex]["unmask"]==="true"){
18776
						
18776
 
18777
								#函式說明:
18777
								#函式說明:
18778
								#加密 handshake 後要傳送的訊息 
18778
								#加密 handshake 後要傳送的訊息
18779
								#回傳結果:
18779
								#回傳結果:
18780
								#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
18780
								#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
18781
								#$result["error"],執行不正常結束的錯訊息陣列.
18781
								#$result["error"],執行不正常結束的錯訊息陣列.
18782
								#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
18782
								#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
18783
								#$result["argu"],陣列,使用的參數.
18783
								#$result["argu"],陣列,使用的參數.
18784
								#必填參數:
18784
								#必填參數:
18785
								#$conf["text"],字串,要加密的訊息.
18785
								#$conf["text"],字串,要加密的訊息.
18786
								$conf["webSock::encode"]["text"]=$talkback; 
18786
								$conf["webSock::encode"]["text"]=$talkback;
18787
								#可省略參數:
18787
								#可省略參數:
18788
								#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
18788
								#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
18789
								#$conf["payloadIsBin"]="false";
18789
								#$conf["payloadIsBin"]="false";
18790
								#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
18790
								#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
18791
								#$conf["ping"]="false";
18791
								#$conf["ping"]="false";
Line 18801... Line 18801...
18801
								#無.
18801
								#無.
18802
								#備註:
18802
								#備註:
18803
								#目前$conf["text"]長度超過125會出錯.
18803
								#目前$conf["text"]長度超過125會出錯.
18804
								$talkback=webSock::encode($conf["webSock::encode"]);
18804
								$talkback=webSock::encode($conf["webSock::encode"]);
18805
								unset($conf["webSock::encode"]);
18805
								unset($conf["webSock::encode"]);
18806
								
18806
 
18807
								#如果執行失敗
18807
								#如果執行失敗
18808
								if($talkback["status"]==="false"){
18808
								if($talkback["status"]==="false"){
18809
								
18809
 
18810
									#印出結果
18810
									#印出結果
18811
									var_dump($talkback);
18811
									var_dump($talkback);
18812
								
18812
 
18813
									#結束執行
18813
									#結束執行
18814
									exit;
18814
									exit;
18815
								
18815
 
18816
									}#if end
18816
									}#if end
18817
								
18817
 
18818
								#debug
18818
								#debug
18819
								#var_dump(__LINE__,$talkback);
18819
								#var_dump(__LINE__,$talkback);
18820
								
18820
 
18821
								}#if end
18821
								}#if end
18822
								
18822
 
18823
							}#if end
18823
							}#if end
18824
					
18824
 
18825
						}#if end
18825
						}#if end
18826
					
18826
 
18827
					#反之不為 web socket 訊息
18827
					#反之不為 web socket 訊息
18828
					else{
18828
					else{
18829
					
18829
 
18830
						#儲存成只有一個訊息
18830
						#儲存成只有一個訊息
18831
						$talkback["content"][]=$talkback;
18831
						$talkback["content"][]=$talkback;
18832
					
18832
 
18833
						}#else
18833
						}#else
18834
					
18834
 
18835
					#針對每個訊息的分段
18835
					#針對每個訊息的分段
18836
					foreach($talkback["content"] as $msg){
18836
					foreach($talkback["content"] as $msg){
18837
						
18837
 
18838
						#回傳訊息
18838
						#回傳訊息
18839
						$socket_write=socket_write($connections[$conIndex]["connect"], $msg, strlen($msg));
18839
						$socket_write=socket_write($connections[$conIndex]["connect"], $msg, strlen($msg));
18840
				
18840
 
18841
						#debug
18841
						#debug
18842
						#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
18842
						#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
18843
						
18843
 
18844
						}#foreach end
18844
						}#foreach end
18845
					
18845
 
18846
					#設置執行正常
18846
					#設置執行正常
18847
					$result["status"]="true";
18847
					$result["status"]="true";
18848
					
18848
 
18849
					#回傳結果
18849
					#回傳結果
18850
					return $result;
18850
					return $result;
18851
				
18851
 
18852
					}#if end
18852
					}#if end
18853
				
18853
 
18854
				#用 "talkTo:" 分割收到的訊息
18854
				#用 "talkTo:" 分割收到的訊息
18855
				#涵式說明:
18855
				#涵式說明:
18856
				#將固定格式的字串分開,並回傳分開的結果。
18856
				#將固定格式的字串分開,並回傳分開的結果。
18857
				#回傳結果:
18857
				#回傳結果:
18858
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
18858
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 18868... Line 18868...
18868
				#可省略參數:
18868
				#可省略參數:
18869
				#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
18869
				#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
18870
				$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
18870
				$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
18871
				$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
18871
				$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
18872
				unset($conf["stringProcess::spiltString"]);
18872
				unset($conf["stringProcess::spiltString"]);
18873
				
18873
 
18874
				#如果分割字串失敗
18874
				#如果分割字串失敗
18875
				if($spiltString["status"]==="false"){
18875
				if($spiltString["status"]==="false"){
18876
					
18876
 
18877
					#設置執行失敗
18877
					#設置執行失敗
18878
					$result["status"]="false";
18878
					$result["status"]="false";
18879
				
18879
 
18880
					#設置執行錯誤訊息
18880
					#設置執行錯誤訊息
18881
					$result["error"]=$spiltString;
18881
					$result["error"]=$spiltString;
18882
					
18882
 
18883
					#回傳結果
18883
					#回傳結果
18884
					return $result;
18884
					return $result;
18885
					
18885
 
18886
					}#if end
18886
					}#if end
18887
				
18887
 
18888
				#如果剛好分割出一筆資料
18888
				#如果剛好分割出一筆資料
18889
				if($spiltString["dataCounts"]===1){
18889
				if($spiltString["dataCounts"]===1){
18890
					
18890
 
18891
					#取得自己的id
18891
					#取得自己的id
18892
					$myId=$connections[$conIndex]["id"];
18892
					$myId=$connections[$conIndex]["id"];
18893
					
18893
 
18894
					#取得講話對象的id
18894
					#取得講話對象的id
18895
					$toId=$spiltString["dataArray"][0];
18895
					$toId=$spiltString["dataArray"][0];
18896
					
18896
 
18897
					#設置對象不存在的識別
18897
					#設置對象不存在的識別
18898
					$targetExist=false;
18898
					$targetExist=false;
18899
					
18899
 
18900
					#設置通話對象的key
18900
					#設置通話對象的key
18901
					$targetKey="";
18901
					$targetKey="";
18902
					
18902
 
18903
					#取得除了自己的id以外的所有連線
18903
					#取得除了自己的id以外的所有連線
18904
					foreach($connections as $index=>$talkInfo){
18904
					foreach($connections as $index=>$talkInfo){
18905
						
18905
 
18906
						#除了自己的id
18906
						#除了自己的id
18907
						if($talkInfo["id"]!==$myId){
18907
						if($talkInfo["id"]!==$myId){
18908
							
18908
 
18909
							#如果存在要對話的對象
18909
							#如果存在要對話的對象
18910
							if($talkInfo["id"]===$toId){
18910
							if($talkInfo["id"]===$toId){
18911
								
18911
 
18912
								#設置對象存在的識別
18912
								#設置對象存在的識別
18913
								$targetExist=true;
18913
								$targetExist=true;
18914
								
18914
 
18915
								#通話對象的key
18915
								#通話對象的key
18916
								$targetKey=$index;
18916
								$targetKey=$index;
18917
								
18917
 
18918
								}#if end
18918
								}#if end
18919
							
18919
 
18920
							}#if end
18920
							}#if end
18921
						
18921
 
18922
						}#for each end
18922
						}#for each end
18923
 
18923
 
18924
					#如果通話對象存在
18924
					#如果通話對象存在
18925
					if($targetExist){
18925
					if($targetExist){
18926
						
18926
 
18927
						#保存自己的通話對象
18927
						#保存自己的通話對象
18928
						$connections[$conIndex]["talkTo"][]=$toId;
18928
						$connections[$conIndex]["talkTo"][]=$toId;
18929
						
18929
 
18930
						#設置要給自己看的訊息
18930
						#設置要給自己看的訊息
18931
						$talkback="您與 ".$toId." 展開對話";
18931
						$talkback="您與 ".$toId." 展開對話";
18932
						
18932
 
18933
						#json encode 要給自己看的訊息
18933
						#json encode 要給自己看的訊息
18934
						$talkback=json_encode($talkback).PHP_EOL;
18934
						$talkback=json_encode($talkback).PHP_EOL;
18935
						
18935
 
18936
						#如果是 ws 模式
18936
						#如果是 ws 模式
18937
						if($conf["wsMode"]==="true"){
18937
						if($conf["wsMode"]==="true"){
18938
						
18938
 
18939
							#如果已經 handshake 過
18939
							#如果已經 handshake 過
18940
							if($connections[$conIndex]["handshaked"]==="true"){
18940
							if($connections[$conIndex]["handshaked"]==="true"){
18941
							
18941
 
18942
								#如果要 unmask	
18942
								#如果要 unmask
18943
								if($connections[$conIndex]["unmask"]==="true"){
18943
								if($connections[$conIndex]["unmask"]==="true"){
18944
							
18944
 
18945
									#函式說明:
18945
									#函式說明:
18946
									#加密 handshake 後要傳送的訊息 
18946
									#加密 handshake 後要傳送的訊息
18947
									#回傳結果:
18947
									#回傳結果:
18948
									#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
18948
									#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
18949
									#$result["error"],執行不正常結束的錯訊息陣列.
18949
									#$result["error"],執行不正常結束的錯訊息陣列.
18950
									#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
18950
									#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
18951
									#$result["argu"],陣列,使用的參數.
18951
									#$result["argu"],陣列,使用的參數.
18952
									#必填參數:
18952
									#必填參數:
18953
									#$conf["text"],字串,要加密的訊息.
18953
									#$conf["text"],字串,要加密的訊息.
18954
									$conf["webSock::encode"]["text"]=$talkback; 
18954
									$conf["webSock::encode"]["text"]=$talkback;
18955
									#可省略參數:
18955
									#可省略參數:
18956
									#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
18956
									#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
18957
									#$conf["payloadIsBin"]="false";
18957
									#$conf["payloadIsBin"]="false";
18958
									#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
18958
									#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
18959
									#$conf["ping"]="false";
18959
									#$conf["ping"]="false";
Line 18969... Line 18969...
18969
									#無.
18969
									#無.
18970
									#備註:
18970
									#備註:
18971
									#目前$conf["text"]長度超過125會出錯.
18971
									#目前$conf["text"]長度超過125會出錯.
18972
									$talkback=webSock::encode($conf["webSock::encode"]);
18972
									$talkback=webSock::encode($conf["webSock::encode"]);
18973
									unset($conf["webSock::encode"]);
18973
									unset($conf["webSock::encode"]);
18974
									
18974
 
18975
									#如果執行失敗
18975
									#如果執行失敗
18976
									if($talkback["status"]==="false"){
18976
									if($talkback["status"]==="false"){
18977
									
18977
 
18978
										#印出結果
18978
										#印出結果
18979
										var_dump($talkback);
18979
										var_dump($talkback);
18980
									
18980
 
18981
										#結束執行
18981
										#結束執行
18982
										exit;
18982
										exit;
18983
									
18983
 
18984
										}#if end
18984
										}#if end
18985
									
18985
 
18986
									#debug
18986
									#debug
18987
									#var_dump(__LINE__,$talkback);
18987
									#var_dump(__LINE__,$talkback);
18988
									
18988
 
18989
									}#if end
18989
									}#if end
18990
								
18990
 
18991
								}#if end
18991
								}#if end
18992
						
18992
 
18993
							}#if end
18993
							}#if end
18994
							
18994
 
18995
						#反之不為 web socket 訊息
18995
						#反之不為 web socket 訊息
18996
						else{
18996
						else{
18997
						
18997
 
18998
							#儲存成只有一個訊息
18998
							#儲存成只有一個訊息
18999
							$talkback["content"][]=$talkback;
18999
							$talkback["content"][]=$talkback;
19000
						
19000
 
19001
							}#else
19001
							}#else
19002
						
19002
 
19003
						#針對每個訊息的分段
19003
						#針對每個訊息的分段
19004
						foreach($talkback["content"] as $msg){
19004
						foreach($talkback["content"] as $msg){
19005
							
19005
 
19006
							#回傳訊息
19006
							#回傳訊息
19007
							$socket_write=socket_write($connections[$conIndex]["connect"], $msg, strlen($msg));
19007
							$socket_write=socket_write($connections[$conIndex]["connect"], $msg, strlen($msg));
19008
					
19008
 
19009
							#debug
19009
							#debug
19010
							#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
19010
							#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
19011
							
19011
 
19012
							}#foreach end
19012
							}#foreach end
19013
						
19013
 
19014
						#讓對方可以跟自己對話
19014
						#讓對方可以跟自己對話
19015
						$connections[$targetKey]["talkTo"][]=$myId;
19015
						$connections[$targetKey]["talkTo"][]=$myId;
19016
						
19016
 
19017
						#設置要給對方看的訊息
19017
						#設置要給對方看的訊息
19018
						$talkback=$connections[$conIndex]["id"]." 與您展開對話";
19018
						$talkback=$connections[$conIndex]["id"]." 與您展開對話";
19019
						
19019
 
19020
						#json encode 設置要給對方看的訊息
19020
						#json encode 設置要給對方看的訊息
19021
						$talkback=json_encode($talkback);
19021
						$talkback=json_encode($talkback);
19022
						
19022
 
19023
						#如果是 ws 模式
19023
						#如果是 ws 模式
19024
						if($conf["wsMode"]==="true"){
19024
						if($conf["wsMode"]==="true"){
19025
						
19025
 
19026
							#如果已經 handshake 過
19026
							#如果已經 handshake 過
19027
							if($connections[$conIndex]["handshaked"]==="true"){
19027
							if($connections[$conIndex]["handshaked"]==="true"){
19028
							
19028
 
19029
								#如果要 unmask	
19029
								#如果要 unmask
19030
								if($connections[$conIndex]["unmask"]==="true"){
19030
								if($connections[$conIndex]["unmask"]==="true"){
19031
							
19031
 
19032
									#函式說明:
19032
									#函式說明:
19033
									#加密 handshake 後要傳送的訊息 
19033
									#加密 handshake 後要傳送的訊息
19034
									#回傳結果:
19034
									#回傳結果:
19035
									#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
19035
									#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
19036
									#$result["error"],執行不正常結束的錯訊息陣列.
19036
									#$result["error"],執行不正常結束的錯訊息陣列.
19037
									#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
19037
									#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
19038
									#$result["argu"],陣列,使用的參數.
19038
									#$result["argu"],陣列,使用的參數.
19039
									#必填參數:
19039
									#必填參數:
19040
									#$conf["text"],字串,要加密的訊息.
19040
									#$conf["text"],字串,要加密的訊息.
19041
									$conf["webSock::encode"]["text"]=$talkback; 
19041
									$conf["webSock::encode"]["text"]=$talkback;
19042
									#可省略參數:
19042
									#可省略參數:
19043
									#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
19043
									#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
19044
									#$conf["payloadIsBin"]="false";
19044
									#$conf["payloadIsBin"]="false";
19045
									#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
19045
									#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
19046
									#$conf["ping"]="false";
19046
									#$conf["ping"]="false";
Line 19056... Line 19056...
19056
									#無.
19056
									#無.
19057
									#備註:
19057
									#備註:
19058
									#目前$conf["text"]長度超過125會出錯.
19058
									#目前$conf["text"]長度超過125會出錯.
19059
									$talkback=webSock::encode($conf["webSock::encode"]);
19059
									$talkback=webSock::encode($conf["webSock::encode"]);
19060
									unset($conf["webSock::encode"]);
19060
									unset($conf["webSock::encode"]);
19061
									
19061
 
19062
									#如果執行失敗
19062
									#如果執行失敗
19063
									if($talkback["status"]==="false"){
19063
									if($talkback["status"]==="false"){
19064
									
19064
 
19065
										#印出結果
19065
										#印出結果
19066
										var_dump($talkback);
19066
										var_dump($talkback);
19067
									
19067
 
19068
										#結束執行
19068
										#結束執行
19069
										exit;
19069
										exit;
19070
									
19070
 
19071
										}#if end
19071
										}#if end
19072
									
19072
 
19073
									#debug
19073
									#debug
19074
									#var_dump(__LINE__,$talkback);
19074
									#var_dump(__LINE__,$talkback);
19075
									
19075
 
19076
									}#if end
19076
									}#if end
19077
									
19077
 
19078
								}#if end
19078
								}#if end
19079
						
19079
 
19080
							}#if end
19080
							}#if end
19081
							
19081
 
19082
						#反之不為 web socket 訊息
19082
						#反之不為 web socket 訊息
19083
						else{
19083
						else{
19084
						
19084
 
19085
							#儲存成只有一個訊息
19085
							#儲存成只有一個訊息
19086
							$talkback["content"][]=$talkback;
19086
							$talkback["content"][]=$talkback;
19087
						
19087
 
19088
							}#else
19088
							}#else
19089
						
19089
 
19090
						#針對每個訊息的分段
19090
						#針對每個訊息的分段
19091
						foreach($talkback["content"] as $msg){
19091
						foreach($talkback["content"] as $msg){
19092
							
19092
 
19093
							#給對方看的訊息
19093
							#給對方看的訊息
19094
							$socket_write=socket_write($connections[$targetKey]["connect"], $msg, strlen($msg));
19094
							$socket_write=socket_write($connections[$targetKey]["connect"], $msg, strlen($msg));
19095
					
19095
 
19096
							#debug
19096
							#debug
19097
							#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
19097
							#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
19098
							
19098
 
19099
							}#foreach end
19099
							}#foreach end
19100
							
19100
 
19101
						#設置執行正常
19101
						#設置執行正常
19102
						$result["status"]="true";
19102
						$result["status"]="true";
19103
						
19103
 
19104
						#回傳結果
19104
						#回傳結果
19105
						return $result;
19105
						return $result;
19106
						
19106
 
19107
						}#if end
19107
						}#if end
19108
					
19108
 
19109
					}#if end
19109
					}#if end
19110
				
19110
 
19111
				}#if end
19111
				}#if end
19112
			
19112
 
19113
			}#if end
19113
			}#if end
19114
		
19114
 
19115
		#不是支援的訊息
19115
		#不是支援的訊息
19116
		$result["status"]="continue";
19116
		$result["status"]="continue";
19117
	
19117
 
19118
		#回傳結果
19118
		#回傳結果
19119
		return $result;
19119
		return $result;
19120
	
19120
 
19121
		}#function basic end
19121
		}#function basic end
19122
	
19122
 
19123
	/*
19123
	/*
19124
	#函式說明:
19124
	#函式說明:
19125
	#提供webSock::nativeSocketTcpIpServer使用系統功能(system).
19125
	#提供webSock::nativeSocketTcpIpServer使用系統功能(system).
19126
	#回傳結果:
19126
	#回傳結果:
19127
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常,"continue"代表不適用.
19127
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常,"continue"代表不適用.
Line 19150... Line 19150...
19150
	#備註:
19150
	#備註:
19151
	#收到用戶的"shutdown"訊息,會重新啟動webSocket server.
19151
	#收到用戶的"shutdown"訊息,會重新啟動webSocket server.
19152
	#無.
19152
	#無.
19153
	*/
19153
	*/
19154
	public static function system(&$conf){
19154
	public static function system(&$conf){
19155
	
19155
 
19156
		#初始化要回傳的結果
19156
		#初始化要回傳的結果
19157
		$result=array();
19157
		$result=array();
19158
 
19158
 
19159
		#取得當前執行的函數名稱
19159
		#取得當前執行的函數名稱
19160
		$result["function"]=__FUNCTION__;
19160
		$result["function"]=__FUNCTION__;
Line 19162... Line 19162...
19162
		#涵式說明:
19162
		#涵式說明:
19163
		#判斷當前環境為web還是cmd
19163
		#判斷當前環境為web還是cmd
19164
		#回傳結果:
19164
		#回傳結果:
19165
		#$result,"web"或"cmd"
19165
		#$result,"web"或"cmd"
19166
		if(csInformation::getEnv()==="web"){
19166
		if(csInformation::getEnv()==="web"){
19167
			
19167
 
19168
			#設置執行失敗
19168
			#設置執行失敗
19169
			$result["status"]="false";
19169
			$result["status"]="false";
19170
			
19170
 
19171
			#設置執行錯誤訊息
19171
			#設置執行錯誤訊息
19172
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
19172
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
19173
			
19173
 
19174
			#回傳結果
19174
			#回傳結果
19175
			return $result;
19175
			return $result;
19176
			
19176
 
19177
			}#if end
19177
			}#if end
19178
 
19178
 
19179
		#取得參數
19179
		#取得參數
19180
		$result["argu"]=$conf;
19180
		$result["argu"]=$conf;
19181
 
19181
 
19182
		#如果 $conf 不為陣列
19182
		#如果 $conf 不為陣列
19183
		if(gettype($conf)!="array"){
19183
		if(gettype($conf)!="array"){
19184
		
19184
 
19185
			#設置執行失敗
19185
			#設置執行失敗
19186
			$result["status"]="false";
19186
			$result["status"]="false";
19187
		
19187
 
19188
			#設置執行錯誤訊息
19188
			#設置執行錯誤訊息
19189
			$result["error"][]="\$conf變數須為陣列形態";
19189
			$result["error"][]="\$conf變數須為陣列形態";
19190
		
19190
 
19191
			#如果傳入的參數為 null
19191
			#如果傳入的參數為 null
19192
			if($conf==null){
19192
			if($conf==null){
19193
			
19193
 
19194
				#設置執行錯誤訊息
19194
				#設置執行錯誤訊息
19195
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
19195
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
19196
			
19196
 
19197
				}#if end
19197
				}#if end
19198
 
19198
 
19199
			#回傳結果
19199
			#回傳結果
19200
			return $result;
19200
			return $result;
19201
		
19201
 
19202
			}#if end
19202
			}#if end
19203
		
19203
 
19204
		#檢查參數
19204
		#檢查參數
19205
		#函式說明:
19205
		#函式說明:
19206
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
19206
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
19207
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
19207
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
19208
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
19208
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
Line 19233... Line 19233...
19233
		#$conf["canBeEmpty"]=array();
19233
		#$conf["canBeEmpty"]=array();
19234
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
19234
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
19235
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("wsMode");
19235
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("wsMode");
19236
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
19236
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
19237
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("wsMode","gpgId");
19237
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("wsMode","gpgId");
19238
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double"); 
19238
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
19239
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string");
19239
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string");
19240
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
19240
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
19241
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("true",gnupgId);
19241
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("true",gnupgId);
19242
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
19242
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
19243
		#$conf["arrayCountEqualCheck"][]=array();
19243
		#$conf["arrayCountEqualCheck"][]=array();
Line 19245... Line 19245...
19245
		#array_keys=>http://php.net/manual/en/function.array-keys.php
19245
		#array_keys=>http://php.net/manual/en/function.array-keys.php
19246
		#建議:
19246
		#建議:
19247
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
19247
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
19248
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
19248
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
19249
		unset($conf["variableCheck::checkArguments"]);
19249
		unset($conf["variableCheck::checkArguments"]);
19250
		
19250
 
19251
		#如果檢查參數失敗
19251
		#如果檢查參數失敗
19252
		if($checkArguments["status"]==="false"){
19252
		if($checkArguments["status"]==="false"){
19253
			
19253
 
19254
			#設置執行失敗
19254
			#設置執行失敗
19255
			$result["status"]="false";
19255
			$result["status"]="false";
19256
		
19256
 
19257
			#設置執行錯誤訊息
19257
			#設置執行錯誤訊息
19258
			$result["error"]=$checkArguments;
19258
			$result["error"]=$checkArguments;
19259
			
19259
 
19260
			#回傳結果
19260
			#回傳結果
19261
			return $result;
19261
			return $result;
19262
			
19262
 
19263
			}#if end
19263
			}#if end
19264
			
19264
 
19265
		#如果檢查參數不通過
19265
		#如果檢查參數不通過
19266
		if($checkArguments["passed"]==="false"){
19266
		if($checkArguments["passed"]==="false"){
19267
			
19267
 
19268
			#設置執行失敗
19268
			#設置執行失敗
19269
			$result["status"]="false";
19269
			$result["status"]="false";
19270
		
19270
 
19271
			#設置執行錯誤訊息
19271
			#設置執行錯誤訊息
19272
			$result["error"]=$checkArguments;
19272
			$result["error"]=$checkArguments;
19273
			
19273
 
19274
			#回傳結果
19274
			#回傳結果
19275
			return $result;
19275
			return $result;
19276
			
19276
 
19277
			}#if end
19277
			}#if end
19278
 
19278
 
19279
		#取得收到的訊息
19279
		#取得收到的訊息
19280
		$receivedData=$conf["data"];
19280
		$receivedData=$conf["data"];
19281
		
19281
 
19282
		#取得 server 的 socket
19282
		#取得 server 的 socket
19283
		$serverSocket=$conf["serverSock"];
19283
		$serverSocket=$conf["serverSock"];
19284
		
19284
 
19285
		#取得 client 的 socket
19285
		#取得 client 的 socket
19286
		$clientSocket=$conf["clientSock"];
19286
		$clientSocket=$conf["clientSock"];
19287
		
19287
 
19288
		#取得 client 的資訊
19288
		#取得 client 的資訊
19289
		$clientInfo=$conf["clientInfo"];
19289
		$clientInfo=$conf["clientInfo"];
19290
		
19290
 
19291
		#取得 client 的索引
19291
		#取得 client 的索引
19292
		$clientIndex=$conf["clientIndex"];
19292
		$clientIndex=$conf["clientIndex"];
19293
		
19293
 
19294
		#取得 all clients 的資訊
19294
		#取得 all clients 的資訊
19295
		$allConn=&$conf["allConn"];
19295
		$allConn=&$conf["allConn"];
19296
	
19296
 
19297
		#hex2bin
19297
		#hex2bin
19298
		#函式說明:
19298
		#函式說明:
19299
		#加密或編碼字串,可以用的方法有sha1,md5,password_sha,qbpwcf,bin2hex,hex2bin,sha1可以回傳20或40的數值字串,md5可以回傳32個數值字串,password_hash可以回傳60~255個字元.
19299
		#加密或編碼字串,可以用的方法有sha1,md5,password_sha,qbpwcf,bin2hex,hex2bin,sha1可以回傳20或40的數值字串,md5可以回傳32個數值字串,password_hash可以回傳60~255個字元.
19300
		#回傳結果:
19300
		#回傳結果:
19301
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常. 
19301
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
19302
		#$result["function"],當前執行的函數名稱.
19302
		#$result["function"],當前執行的函數名稱.
19303
		#$result["content"],加密後的結果.
19303
		#$result["content"],加密後的結果.
19304
		#$result["error"],錯誤訊息陣列.
19304
		#$result["error"],錯誤訊息陣列.
19305
		#$result["argu"],使用的參數.
19305
		#$result["argu"],使用的參數.
19306
		#必填參數:
19306
		#必填參數:
Line 19348... Line 19348...
19348
 
19348
 
19349
			}#if end
19349
			}#if end
19350
 
19350
 
19351
		#取得hex2bin後的字串
19351
		#取得hex2bin後的字串
19352
		$receivedData=$enCodeStr["content"];
19352
		$receivedData=$enCodeStr["content"];
19353
		
19353
 
19354
		#gpg decrypt
19354
		#gpg decrypt
19355
		#函式說明:
19355
		#函式說明:
19356
		#加密或編碼字串,可以用的方法有sha1,md5,password_sha,qbpwcf,bin2hex,hex2bin,sha1可以回傳20或40的數值字串,md5可以回傳32個數值字串,password_hash可以回傳60~255個字元.
19356
		#加密或編碼字串,可以用的方法有sha1,md5,password_sha,qbpwcf,bin2hex,hex2bin,sha1可以回傳20或40的數值字串,md5可以回傳32個數值字串,password_hash可以回傳60~255個字元.
19357
		#回傳結果:
19357
		#回傳結果:
19358
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常. 
19358
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
19359
		#$result["function"],當前執行的函數名稱.
19359
		#$result["function"],當前執行的函數名稱.
19360
		#$result["content"],加密後的結果.
19360
		#$result["content"],加密後的結果.
19361
		#$result["error"],錯誤訊息陣列.
19361
		#$result["error"],錯誤訊息陣列.
19362
		#$result["argu"],使用的參數.
19362
		#$result["argu"],使用的參數.
19363
		#必填參數:
19363
		#必填參數:
Line 19405... Line 19405...
19405
 
19405
 
19406
			}#if end
19406
			}#if end
19407
 
19407
 
19408
		#取得解密後的字串
19408
		#取得解密後的字串
19409
		$receivedData=$enCodeStr["content"];
19409
		$receivedData=$enCodeStr["content"];
19410
		
19410
 
19411
		#json_decode
19411
		#json_decode
19412
		$receivedData=json_decode($receivedData);
19412
		$receivedData=json_decode($receivedData);
19413
		
19413
 
19414
		#如果收到 "shutdown"
19414
		#如果收到 "shutdown"
19415
		if($receivedData==="shutdown"){
19415
		if($receivedData==="shutdown"){
19416
			
19416
 
19417
			#設置要回傳的訊息
19417
			#設置要回傳的訊息
19418
			$talkback="Server 即將關閉";
19418
			$talkback="Server 即將關閉";
19419
			
19419
 
19420
			#json encode 要回傳的訊息
19420
			#json encode 要回傳的訊息
19421
			$talkback=json_encode($talkback);
19421
			$talkback=json_encode($talkback);
19422
			
19422
 
19423
			#如果是 ws 模式
19423
			#如果是 ws 模式
19424
			if($conf["wsMode"]==="true"){
19424
			if($conf["wsMode"]==="true"){
19425
			
19425
 
19426
				#如果已經 handshake 過
19426
				#如果已經 handshake 過
19427
				if($connections[$conIndex]["handshaked"]==="true"){
19427
				if($connections[$conIndex]["handshaked"]==="true"){
19428
				
19428
 
19429
					#如果要 unmask	
19429
					#如果要 unmask
19430
					if($connections[$conIndex]["unmask"]==="true"){
19430
					if($connections[$conIndex]["unmask"]==="true"){
19431
				
19431
 
19432
						#函式說明:
19432
						#函式說明:
19433
						#加密 handshake 後要傳送的訊息 
19433
						#加密 handshake 後要傳送的訊息
19434
						#回傳結果:
19434
						#回傳結果:
19435
						#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
19435
						#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
19436
						#$result["error"],執行不正常結束的錯訊息陣列.
19436
						#$result["error"],執行不正常結束的錯訊息陣列.
19437
						#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
19437
						#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
19438
						#$result["argu"],陣列,使用的參數.
19438
						#$result["argu"],陣列,使用的參數.
19439
						#必填參數:
19439
						#必填參數:
19440
						#$conf["text"],字串,要加密的訊息.
19440
						#$conf["text"],字串,要加密的訊息.
19441
						$conf["webSock::encode"]["text"]=$talkback; 
19441
						$conf["webSock::encode"]["text"]=$talkback;
19442
						#可省略參數:
19442
						#可省略參數:
19443
						#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
19443
						#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
19444
						#$conf["payloadIsBin"]="false";
19444
						#$conf["payloadIsBin"]="false";
19445
						#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
19445
						#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
19446
						#$conf["ping"]="false";
19446
						#$conf["ping"]="false";
Line 19456... Line 19456...
19456
						#無.
19456
						#無.
19457
						#備註:
19457
						#備註:
19458
						#無.
19458
						#無.
19459
						$talkback=webSock::encode($conf["webSock::encode"]);
19459
						$talkback=webSock::encode($conf["webSock::encode"]);
19460
						unset($conf["webSock::encode"]);
19460
						unset($conf["webSock::encode"]);
19461
						
19461
 
19462
						#如果執行失敗
19462
						#如果執行失敗
19463
						if($talkback["status"]==="false"){
19463
						if($talkback["status"]==="false"){
19464
						
19464
 
19465
							#印出結果
19465
							#印出結果
19466
							var_dump($talkback);
19466
							var_dump($talkback);
19467
						
19467
 
19468
							#結束執行
19468
							#結束執行
19469
							exit;
19469
							exit;
19470
						
19470
 
19471
							}#if end
19471
							}#if end
19472
						
19472
 
19473
						#debug
19473
						#debug
19474
						#var_dump(__LINE__,$talkback);
19474
						#var_dump(__LINE__,$talkback);
19475
						
19475
 
19476
						}#if end
19476
						}#if end
19477
					
19477
 
19478
					}#if end
19478
					}#if end
19479
			
19479
 
19480
				}#if end
19480
				}#if end
19481
			
19481
 
19482
			#反之不為 web socket 訊息
19482
			#反之不為 web socket 訊息
19483
			else{
19483
			else{
19484
			
19484
 
19485
				#儲存成只有一個訊息
19485
				#儲存成只有一個訊息
19486
				$talkback["content"][]=$talkback;
19486
				$talkback["content"][]=$talkback;
19487
			
19487
 
19488
				}#else
19488
				}#else
19489
			
19489
 
19490
			#針對每個訊息的分段
19490
			#針對每個訊息的分段
19491
			foreach($talkback["content"] as $msg){
19491
			foreach($talkback["content"] as $msg){
19492
				
19492
 
19493
				#回傳訊息
19493
				#回傳訊息
19494
				$socket_write=socket_write($con["connect"], $msg, strlen($msg));
19494
				$socket_write=socket_write($con["connect"], $msg, strlen($msg));
19495
		
19495
 
19496
				#debug
19496
				#debug
19497
				#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
19497
				#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
19498
				
19498
 
19499
				}#foreach end
19499
				}#foreach end
19500
			
19500
 
19501
			#關閉用戶的連線
19501
			#關閉用戶的連線
19502
			socket_close($con["connect"]);
19502
			socket_close($con["connect"]);
19503
		
19503
 
19504
			}#if end
19504
			}#if end
19505
			
19505
 
19506
		#關閉 server socket
19506
		#關閉 server socket
19507
		socket_close($serverSocket);
19507
		socket_close($serverSocket);
19508
		
19508
 
19509
		#設置執行正常
19509
		#設置執行正常
19510
		$result["status"]="true";
19510
		$result["status"]="true";
19511
		
19511
 
19512
		#回傳結果
19512
		#回傳結果
19513
		return $result;
19513
		return $result;
19514
	
19514
 
19515
		}#function system end
19515
		}#function system end
19516
	
19516
 
19517
	/*
19517
	/*
19518
	#函式說明:
19518
	#函式說明:
19519
	#提供webSock::nativeSocketTcpIpServer使用系統功能(system).
19519
	#提供webSock::nativeSocketTcpIpServer使用系統功能(system).
19520
	#回傳結果:
19520
	#回傳結果:
19521
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常,"continue"代表不適用.
19521
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常,"continue"代表不適用.
Line 19543... Line 19543...
19543
	#https://www.rfc-editor.org/rfc/rfc6455#page-28, Web Socket Base Framing Protocol.
19543
	#https://www.rfc-editor.org/rfc/rfc6455#page-28, Web Socket Base Framing Protocol.
19544
	#備註:
19544
	#備註:
19545
	#收到用戶的"quit"訊息,會結束與用戶的連線.
19545
	#收到用戶的"quit"訊息,會結束與用戶的連線.
19546
	*/
19546
	*/
19547
	public static function noAuth(&$conf){
19547
	public static function noAuth(&$conf){
19548
	
19548
 
19549
		#初始化要回傳的結果
19549
		#初始化要回傳的結果
19550
		$result=array();
19550
		$result=array();
19551
 
19551
 
19552
		#取得當前執行的函數名稱
19552
		#取得當前執行的函數名稱
19553
		$result["function"]=__FUNCTION__;
19553
		$result["function"]=__FUNCTION__;
Line 19555... Line 19555...
19555
		#涵式說明:
19555
		#涵式說明:
19556
		#判斷當前環境為web還是cmd
19556
		#判斷當前環境為web還是cmd
19557
		#回傳結果:
19557
		#回傳結果:
19558
		#$result,"web"或"cmd"
19558
		#$result,"web"或"cmd"
19559
		if(csInformation::getEnv()==="web"){
19559
		if(csInformation::getEnv()==="web"){
19560
			
19560
 
19561
			#設置執行失敗
19561
			#設置執行失敗
19562
			$result["status"]="false";
19562
			$result["status"]="false";
19563
			
19563
 
19564
			#設置執行錯誤訊息
19564
			#設置執行錯誤訊息
19565
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
19565
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
19566
			
19566
 
19567
			#回傳結果
19567
			#回傳結果
19568
			return $result;
19568
			return $result;
19569
			
19569
 
19570
			}#if end
19570
			}#if end
19571
 
19571
 
19572
		#取得參數
19572
		#取得參數
19573
		$result["argu"]=$conf;
19573
		$result["argu"]=$conf;
19574
 
19574
 
19575
		#如果 $conf 不為陣列
19575
		#如果 $conf 不為陣列
19576
		if(gettype($conf)!="array"){
19576
		if(gettype($conf)!="array"){
19577
		
19577
 
19578
			#設置執行失敗
19578
			#設置執行失敗
19579
			$result["status"]="false";
19579
			$result["status"]="false";
19580
		
19580
 
19581
			#設置執行錯誤訊息
19581
			#設置執行錯誤訊息
19582
			$result["error"][]="\$conf變數須為陣列形態";
19582
			$result["error"][]="\$conf變數須為陣列形態";
19583
		
19583
 
19584
			#如果傳入的參數為 null
19584
			#如果傳入的參數為 null
19585
			if($conf==null){
19585
			if($conf==null){
19586
			
19586
 
19587
				#設置執行錯誤訊息
19587
				#設置執行錯誤訊息
19588
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
19588
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
19589
			
19589
 
19590
				}#if end
19590
				}#if end
19591
 
19591
 
19592
			#回傳結果
19592
			#回傳結果
19593
			return $result;
19593
			return $result;
19594
		
19594
 
19595
			}#if end
19595
			}#if end
19596
		
19596
 
19597
		#檢查參數
19597
		#檢查參數
19598
		#函式說明:
19598
		#函式說明:
19599
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
19599
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
19600
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
19600
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
19601
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
19601
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
Line 19626... Line 19626...
19626
		#$conf["canBeEmpty"]=array();
19626
		#$conf["canBeEmpty"]=array();
19627
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
19627
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
19628
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("wsMode");
19628
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("wsMode");
19629
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
19629
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
19630
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("wsMode","gpgId");
19630
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("wsMode","gpgId");
19631
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double"); 
19631
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
19632
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string");
19632
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string");
19633
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
19633
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
19634
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("true",gnupgId);
19634
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("true",gnupgId);
19635
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
19635
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
19636
		#$conf["arrayCountEqualCheck"][]=array();
19636
		#$conf["arrayCountEqualCheck"][]=array();
Line 19638... Line 19638...
19638
		#array_keys=>http://php.net/manual/en/function.array-keys.php
19638
		#array_keys=>http://php.net/manual/en/function.array-keys.php
19639
		#建議:
19639
		#建議:
19640
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
19640
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
19641
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
19641
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
19642
		unset($conf["variableCheck::checkArguments"]);
19642
		unset($conf["variableCheck::checkArguments"]);
19643
		
19643
 
19644
		#如果檢查參數失敗
19644
		#如果檢查參數失敗
19645
		if($checkArguments["status"]==="false"){
19645
		if($checkArguments["status"]==="false"){
19646
			
19646
 
19647
			#設置執行失敗
19647
			#設置執行失敗
19648
			$result["status"]="false";
19648
			$result["status"]="false";
19649
		
19649
 
19650
			#設置執行錯誤訊息
19650
			#設置執行錯誤訊息
19651
			$result["error"]=$checkArguments;
19651
			$result["error"]=$checkArguments;
19652
			
19652
 
19653
			#回傳結果
19653
			#回傳結果
19654
			return $result;
19654
			return $result;
19655
			
19655
 
19656
			}#if end
19656
			}#if end
19657
			
19657
 
19658
		#如果檢查參數不通過
19658
		#如果檢查參數不通過
19659
		if($checkArguments["passed"]==="false"){
19659
		if($checkArguments["passed"]==="false"){
19660
			
19660
 
19661
			#設置執行失敗
19661
			#設置執行失敗
19662
			$result["status"]="false";
19662
			$result["status"]="false";
19663
		
19663
 
19664
			#設置執行錯誤訊息
19664
			#設置執行錯誤訊息
19665
			$result["error"]=$checkArguments;
19665
			$result["error"]=$checkArguments;
19666
			
19666
 
19667
			#回傳結果
19667
			#回傳結果
19668
			return $result;
19668
			return $result;
19669
			
19669
 
19670
			}#if end
19670
			}#if end
19671
 
19671
 
19672
		#取得收到的訊息
19672
		#取得收到的訊息
19673
		$receivedData=$conf["data"];
19673
		$receivedData=$conf["data"];
19674
		
19674
 
19675
		#取得 server 的 socket
19675
		#取得 server 的 socket
19676
		$serverSocket=$conf["serverSock"];
19676
		$serverSocket=$conf["serverSock"];
19677
		
19677
 
19678
		#取得 client 的 socket
19678
		#取得 client 的 socket
19679
		$clientSocket=$conf["clientSock"];
19679
		$clientSocket=$conf["clientSock"];
19680
		
19680
 
19681
		#取得 client 的資訊
19681
		#取得 client 的資訊
19682
		$clientInfo=$conf["clientInfo"];
19682
		$clientInfo=$conf["clientInfo"];
19683
		
19683
 
19684
		#取得 client 的索引
19684
		#取得 client 的索引
19685
		$clientIndex=$conf["clientIndex"];
19685
		$clientIndex=$conf["clientIndex"];
19686
		
19686
 
19687
		#取得 all clients 的資訊
19687
		#取得 all clients 的資訊
19688
		$allConn=&$conf["allConn"];
19688
		$allConn=&$conf["allConn"];
19689
	
19689
 
19690
		#hex2bin
19690
		#hex2bin
19691
		#函式說明:
19691
		#函式說明:
19692
		#加密或編碼字串,可以用的方法有sha1,md5,password_sha,qbpwcf,bin2hex,hex2bin,sha1可以回傳20或40的數值字串,md5可以回傳32個數值字串,password_hash可以回傳60~255個字元.
19692
		#加密或編碼字串,可以用的方法有sha1,md5,password_sha,qbpwcf,bin2hex,hex2bin,sha1可以回傳20或40的數值字串,md5可以回傳32個數值字串,password_hash可以回傳60~255個字元.
19693
		#回傳結果:
19693
		#回傳結果:
19694
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常. 
19694
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
19695
		#$result["function"],當前執行的函數名稱.
19695
		#$result["function"],當前執行的函數名稱.
19696
		#$result["content"],加密後的結果.
19696
		#$result["content"],加密後的結果.
19697
		#$result["error"],錯誤訊息陣列.
19697
		#$result["error"],錯誤訊息陣列.
19698
		#$result["argu"],使用的參數.
19698
		#$result["argu"],使用的參數.
19699
		#必填參數:
19699
		#必填參數:
Line 19741... Line 19741...
19741
 
19741
 
19742
			}#if end
19742
			}#if end
19743
 
19743
 
19744
		#取得hex2bin後的字串
19744
		#取得hex2bin後的字串
19745
		$receivedData=$enCodeStr["content"];
19745
		$receivedData=$enCodeStr["content"];
19746
		
19746
 
19747
		#gpg decrypt
19747
		#gpg decrypt
19748
		#函式說明:
19748
		#函式說明:
19749
		#加密或編碼字串,可以用的方法有sha1,md5,password_sha,qbpwcf,bin2hex,hex2bin,sha1可以回傳20或40的數值字串,md5可以回傳32個數值字串,password_hash可以回傳60~255個字元.
19749
		#加密或編碼字串,可以用的方法有sha1,md5,password_sha,qbpwcf,bin2hex,hex2bin,sha1可以回傳20或40的數值字串,md5可以回傳32個數值字串,password_hash可以回傳60~255個字元.
19750
		#回傳結果:
19750
		#回傳結果:
19751
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常. 
19751
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
19752
		#$result["function"],當前執行的函數名稱.
19752
		#$result["function"],當前執行的函數名稱.
19753
		#$result["content"],加密後的結果.
19753
		#$result["content"],加密後的結果.
19754
		#$result["error"],錯誤訊息陣列.
19754
		#$result["error"],錯誤訊息陣列.
19755
		#$result["argu"],使用的參數.
19755
		#$result["argu"],使用的參數.
19756
		#必填參數:
19756
		#必填參數:
Line 19798... Line 19798...
19798
 
19798
 
19799
			}#if end
19799
			}#if end
19800
 
19800
 
19801
		#取得解密後的字串
19801
		#取得解密後的字串
19802
		$receivedData=$enCodeStr["content"];
19802
		$receivedData=$enCodeStr["content"];
19803
		
19803
 
19804
		#json_decode
19804
		#json_decode
19805
		$receivedData=json_decode($receivedData);
19805
		$receivedData=json_decode($receivedData);
19806
	
19806
 
19807
		#如果訊息內容為 "quit"
19807
		#如果訊息內容為 "quit"
19808
		if($receivedData==="quit"){
19808
		if($receivedData==="quit"){
19809
		
19809
 
19810
			#設置要回傳的訊息
19810
			#設置要回傳的訊息
19811
			$talkback="結束與 server 的連線";
19811
			$talkback="結束與 server 的連線";
19812
			
19812
 
19813
			#json encode 要回傳的訊息
19813
			#json encode 要回傳的訊息
19814
			$talkback=json_encode($talkback).PHP_EOL;
19814
			$talkback=json_encode($talkback).PHP_EOL;
19815
			
19815
 
19816
			#如果是 ws 模式
19816
			#如果是 ws 模式
19817
			if($conf["wsMode"]==="true"){
19817
			if($conf["wsMode"]==="true"){
19818
			
19818
 
19819
				#如果已經 handshake 過
19819
				#如果已經 handshake 過
19820
				if($connections[$conIndex]["handshaked"]==="true"){
19820
				if($connections[$conIndex]["handshaked"]==="true"){
19821
				
19821
 
19822
					#如果要 unmask	
19822
					#如果要 unmask
19823
					if($connections[$conIndex]["unmask"]==="true"){
19823
					if($connections[$conIndex]["unmask"]==="true"){
19824
				
19824
 
19825
						#函式說明:
19825
						#函式說明:
19826
						#加密 handshake 後要傳送的訊息 
19826
						#加密 handshake 後要傳送的訊息
19827
						#回傳結果:
19827
						#回傳結果:
19828
						#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
19828
						#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
19829
						#$result["error"],執行不正常結束的錯訊息陣列.
19829
						#$result["error"],執行不正常結束的錯訊息陣列.
19830
						#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
19830
						#$result["content"],陣列,加密 handshake 後要傳送給 client 的訊息,若有多個代表要分為多個 Fragmentation 來依序傳送.
19831
						#$result["argu"],陣列,使用的參數.
19831
						#$result["argu"],陣列,使用的參數.
19832
						#必填參數:
19832
						#必填參數:
19833
						#$conf["text"],字串,要加密的訊息.
19833
						#$conf["text"],字串,要加密的訊息.
19834
						$conf["webSock::encode"]["text"]=$talkback; 
19834
						$conf["webSock::encode"]["text"]=$talkback;
19835
						#可省略參數:
19835
						#可省略參數:
19836
						#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
19836
						#$conf["payloadIsBin"],字串,"true"定義Payload data是為2元碼;預設為"false"代表為文字.
19837
						#$conf["payloadIsBin"]="false";
19837
						#$conf["payloadIsBin"]="false";
19838
						#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
19838
						#$conf["ping"],字串,"true"代表為ping訊息;反之為"false",預設為"false".
19839
						#$conf["ping"]="false";
19839
						#$conf["ping"]="false";
Line 19849... Line 19849...
19849
						#無.
19849
						#無.
19850
						#備註:
19850
						#備註:
19851
						#目前$conf["text"]長度超過125會出錯.
19851
						#目前$conf["text"]長度超過125會出錯.
19852
						$talkback=webSock::encode($conf["webSock::encode"]);
19852
						$talkback=webSock::encode($conf["webSock::encode"]);
19853
						unset($conf["webSock::encode"]);
19853
						unset($conf["webSock::encode"]);
19854
						
19854
 
19855
						#如果執行失敗
19855
						#如果執行失敗
19856
						if($talkback["status"]==="false"){
19856
						if($talkback["status"]==="false"){
19857
						
19857
 
19858
							#印出結果
19858
							#印出結果
19859
							var_dump($talkback);
19859
							var_dump($talkback);
19860
						
19860
 
19861
							#結束執行
19861
							#結束執行
19862
							exit;
19862
							exit;
19863
						
19863
 
19864
							}#if end
19864
							}#if end
19865
						
19865
 
19866
						#debug
19866
						#debug
19867
						#var_dump(__LINE__,$talkback);
19867
						#var_dump(__LINE__,$talkback);
19868
						
19868
 
19869
						}#if end
19869
						}#if end
19870
						
19870
 
19871
					}#if end
19871
					}#if end
19872
			
19872
 
19873
				}#if end
19873
				}#if end
19874
				
19874
 
19875
			#反之不為 web socket 訊息
19875
			#反之不為 web socket 訊息
19876
			else{
19876
			else{
19877
			
19877
 
19878
				#儲存成只有一個訊息
19878
				#儲存成只有一個訊息
19879
				$talkback["content"][]=$talkback;
19879
				$talkback["content"][]=$talkback;
19880
			
19880
 
19881
				}#else
19881
				}#else
19882
			
19882
 
19883
			#針對每個訊息的分段
19883
			#針對每個訊息的分段
19884
			foreach($talkback["content"] as $msg){
19884
			foreach($talkback["content"] as $msg){
19885
				
19885
 
19886
				#回傳訊息
19886
				#回傳訊息
19887
				$socket_write=socket_write($con["connect"], $msg, strlen($msg));
19887
				$socket_write=socket_write($con["connect"], $msg, strlen($msg));
19888
		
19888
 
19889
				#debug
19889
				#debug
19890
				#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
19890
				#var_dump(__LINE__,$socket_write,socket_strerror(socket_last_error($con["connect"])));
19891
				
19891
 
19892
				}#foreach end
19892
				}#foreach end
19893
				
19893
 
19894
			#關閉被 client 的連線
19894
			#關閉被 client 的連線
19895
			socket_close($con["connect"]);
19895
			socket_close($con["connect"]);
19896
			
19896
 
19897
			#清理連線的陣列
19897
			#清理連線的陣列
19898
			unset($connections[$conIndex]);
19898
			unset($connections[$conIndex]);
19899
			
19899
 
19900
			#設置執行正常
19900
			#設置執行正常
19901
			$result["status"]="true";
19901
			$result["status"]="true";
19902
			
19902
 
19903
			#回傳結果
19903
			#回傳結果
19904
			return $result;
19904
			return $result;
19905
			
19905
 
19906
			}#if end
19906
			}#if end
19907
	
19907
 
19908
		}#function noAuth end
19908
		}#function noAuth end
19909
	
19909
 
19910
	/*
19910
	/*
19911
	#函式說明:
19911
	#函式說明:
19912
	#依照參數來執行對應的函式.
19912
	#依照參數來執行對應的函式.
19913
	#回傳結果:
19913
	#回傳結果:
19914
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常. 
19914
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
19915
	#$result["function"],當前執行的函數名稱.
19915
	#$result["function"],當前執行的函數名稱.
19916
	#$result["content"],加密後的結果.
19916
	#$result["content"],加密後的結果.
19917
	#$result["error"],錯誤訊息陣列.
19917
	#$result["error"],錯誤訊息陣列.
19918
	#$result["argu"],使用的參數.
19918
	#$result["argu"],使用的參數.
19919
	#必填參數:
19919
	#必填參數:
Line 19946... Line 19946...
19946
		#涵式說明:
19946
		#涵式說明:
19947
		#判斷當前環境為web還是cmd
19947
		#判斷當前環境為web還是cmd
19948
		#回傳結果:
19948
		#回傳結果:
19949
		#$result,"web"或"cmd"
19949
		#$result,"web"或"cmd"
19950
		if(csInformation::getEnv()==="web"){
19950
		if(csInformation::getEnv()==="web"){
19951
			
19951
 
19952
			#設置執行失敗
19952
			#設置執行失敗
19953
			$result["status"]="false";
19953
			$result["status"]="false";
19954
			
19954
 
19955
			#設置執行錯誤訊息
19955
			#設置執行錯誤訊息
19956
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
19956
			$result["error"][]="函數 ".$result["function"]." 僅能在命令列環境下運行!";
19957
			
19957
 
19958
			#回傳結果
19958
			#回傳結果
19959
			return $result;
19959
			return $result;
19960
			
19960
 
19961
			}#if end
19961
			}#if end
19962
 
19962
 
19963
		#取得參數
19963
		#取得參數
19964
		$result["argu"]=$conf;
19964
		$result["argu"]=$conf;
19965
 
19965
 
19966
		#如果 $conf 不為陣列
19966
		#如果 $conf 不為陣列
19967
		if(gettype($conf)!="array"){
19967
		if(gettype($conf)!="array"){
19968
		
19968
 
19969
			#設置執行失敗
19969
			#設置執行失敗
19970
			$result["status"]="false";
19970
			$result["status"]="false";
19971
		
19971
 
19972
			#設置執行錯誤訊息
19972
			#設置執行錯誤訊息
19973
			$result["error"][]="\$conf變數須為陣列形態";
19973
			$result["error"][]="\$conf變數須為陣列形態";
19974
		
19974
 
19975
			#如果傳入的參數為 null
19975
			#如果傳入的參數為 null
19976
			if($conf==null){
19976
			if($conf==null){
19977
			
19977
 
19978
				#設置執行錯誤訊息
19978
				#設置執行錯誤訊息
19979
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
19979
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
19980
			
19980
 
19981
				}#if end
19981
				}#if end
19982
 
19982
 
19983
			#回傳結果
19983
			#回傳結果
19984
			return $result;
19984
			return $result;
19985
		
19985
 
19986
			}#if end
19986
			}#if end
19987
			
19987
 
19988
		#檢查參數
19988
		#檢查參數
19989
		#函式說明:
19989
		#函式說明:
19990
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
19990
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
19991
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
19991
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
19992
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
19992
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
Line 20017... Line 20017...
20017
		#$conf["canBeEmpty"]=array();
20017
		#$conf["canBeEmpty"]=array();
20018
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
20018
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
20019
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array();
20019
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array();
20020
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
20020
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
20021
		#$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("listenIp","listenPort","wsMode","processFuncs");
20021
		#$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("listenIp","listenPort","wsMode","processFuncs");
20022
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double"); 
20022
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
20023
		#$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string","string","array");
20023
		#$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string","string","array");
20024
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
20024
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
20025
		#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("127.0.0.1",null,"false",null);
20025
		#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("127.0.0.1",null,"false",null);
20026
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
20026
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
20027
		#$conf["arrayCountEqualCheck"][]=array();
20027
		#$conf["arrayCountEqualCheck"][]=array();
Line 20029... Line 20029...
20029
		#array_keys=>http://php.net/manual/en/function.array-keys.php
20029
		#array_keys=>http://php.net/manual/en/function.array-keys.php
20030
		#建議:
20030
		#建議:
20031
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
20031
		#增加可省略參數全部不能為空字串或空陣列的參數功能.
20032
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
20032
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
20033
		unset($conf["variableCheck::checkArguments"]);
20033
		unset($conf["variableCheck::checkArguments"]);
20034
				
20034
 
20035
		#如果檢查參數失敗
20035
		#如果檢查參數失敗
20036
		if($checkArguments["status"]==="false"){
20036
		if($checkArguments["status"]==="false"){
20037
			
20037
 
20038
			#設置執行失敗
20038
			#設置執行失敗
20039
			$result["status"]="false";
20039
			$result["status"]="false";
20040
		
20040
 
20041
			#設置執行錯誤訊息
20041
			#設置執行錯誤訊息
20042
			$result["error"]=$checkArguments;
20042
			$result["error"]=$checkArguments;
20043
			
20043
 
20044
			#回傳結果
20044
			#回傳結果
20045
			return $result;
20045
			return $result;
20046
			
20046
 
20047
			}#if end
20047
			}#if end
20048
			
20048
 
20049
		#如果檢查參數不通過
20049
		#如果檢查參數不通過
20050
		if($checkArguments["passed"]==="false"){
20050
		if($checkArguments["passed"]==="false"){
20051
			
20051
 
20052
			#設置執行失敗
20052
			#設置執行失敗
20053
			$result["status"]="false";
20053
			$result["status"]="false";
20054
		
20054
 
20055
			#設置執行錯誤訊息
20055
			#設置執行錯誤訊息
20056
			$result["error"]=$checkArguments;
20056
			$result["error"]=$checkArguments;
20057
			
20057
 
20058
			#回傳結果
20058
			#回傳結果
20059
			return $result;
20059
			return $result;
20060
			
20060
 
20061
			}#if end
20061
			}#if end
20062
 
20062
 
20063
		#取得收到的訊息
20063
		#取得收到的訊息
20064
		$receivedData=$conf["data"];
20064
		$receivedData=$conf["data"];
20065
		
20065
 
20066
		#取得 server 的 socket
20066
		#取得 server 的 socket
20067
		$serverSocket=$conf["serverSock"];
20067
		$serverSocket=$conf["serverSock"];
20068
		
20068
 
20069
		#取得 client 的 socket
20069
		#取得 client 的 socket
20070
		$clientSocket=$conf["clientSock"];
20070
		$clientSocket=$conf["clientSock"];
20071
		
20071
 
20072
		#取得 client 的資訊
20072
		#取得 client 的資訊
20073
		$clientInfo=$conf["clientInfo"];
20073
		$clientInfo=$conf["clientInfo"];
20074
		
20074
 
20075
		#取得 client 的索引
20075
		#取得 client 的索引
20076
		$clientIndex=$conf["clientIndex"];
20076
		$clientIndex=$conf["clientIndex"];
20077
		
20077
 
20078
		#取得 all clients 的資訊
20078
		#取得 all clients 的資訊
20079
		$allConn=$conf["allConn"];
20079
		$allConn=$conf["allConn"];
20080
		
20080
 
20081
		#hex2bin
20081
		#hex2bin
20082
		#函式說明:
20082
		#函式說明:
20083
		#加密或編碼字串,可以用的方法有sha1,md5,password_sha,qbpwcf,bin2hex,hex2bin,gpg,sha1可以回傳20或40的數值字串,md5可以回傳32個數值字串,password_hash可以回傳60~255個字元.
20083
		#加密或編碼字串,可以用的方法有sha1,md5,password_sha,qbpwcf,bin2hex,hex2bin,gpg,sha1可以回傳20或40的數值字串,md5可以回傳32個數值字串,password_hash可以回傳60~255個字元.
20084
		#回傳結果:
20084
		#回傳結果:
20085
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常. 
20085
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
20086
		#$result["function"],當前執行的函數名稱.
20086
		#$result["function"],當前執行的函數名稱.
20087
		#$result["content"],加密後的結果.
20087
		#$result["content"],加密後的結果.
20088
		#$result["error"],錯誤訊息陣列.
20088
		#$result["error"],錯誤訊息陣列.
20089
		#$result["argu"],使用的參數.
20089
		#$result["argu"],使用的參數.
20090
		#必填參數:
20090
		#必填參數:
Line 20115... Line 20115...
20115
		#json_decode=>https://www.php.net/manual/en/function.json-decode.php
20115
		#json_decode=>https://www.php.net/manual/en/function.json-decode.php
20116
		#備註:
20116
		#備註:
20117
		#無.
20117
		#無.
20118
		$enCodeStr=authenticate::enCodeStr($conf["authenticate::enCodeStr"]);
20118
		$enCodeStr=authenticate::enCodeStr($conf["authenticate::enCodeStr"]);
20119
		unset($conf["authenticate::enCodeStr"]);
20119
		unset($conf["authenticate::enCodeStr"]);
20120
		
20120
 
20121
		#如果執行失敗
20121
		#如果執行失敗
20122
		if($enCodeStr["status"]==="false"){
20122
		if($enCodeStr["status"]==="false"){
20123
		
20123
 
20124
			#印出結果
20124
			#印出結果
20125
			return $enCodeStr;
20125
			return $enCodeStr;
20126
			
20126
 
20127
			}#if end
20127
			}#if end
20128
		
20128
 
20129
		#用GnuPG解密
20129
		#用GnuPG解密
20130
		#函式說明:
20130
		#函式說明:
20131
		#加密或編碼字串,可以用的方法有sha1,md5,password_sha,qbpwcf,bin2hex,hex2bin,gpg,sha1可以回傳20或40的數值字串,md5可以回傳32個數值字串,password_hash可以回傳60~255個字元.
20131
		#加密或編碼字串,可以用的方法有sha1,md5,password_sha,qbpwcf,bin2hex,hex2bin,gpg,sha1可以回傳20或40的數值字串,md5可以回傳32個數值字串,password_hash可以回傳60~255個字元.
20132
		#回傳結果:
20132
		#回傳結果:
20133
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常. 
20133
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
20134
		#$result["function"],當前執行的函數名稱.
20134
		#$result["function"],當前執行的函數名稱.
20135
		#$result["content"],加密後的結果.
20135
		#$result["content"],加密後的結果.
20136
		#$result["error"],錯誤訊息陣列.
20136
		#$result["error"],錯誤訊息陣列.
20137
		#$result["argu"],使用的參數.
20137
		#$result["argu"],使用的參數.
20138
		#必填參數:
20138
		#必填參數:
Line 20163... Line 20163...
20163
		#json_decode=>https://www.php.net/manual/en/function.json-decode.php
20163
		#json_decode=>https://www.php.net/manual/en/function.json-decode.php
20164
		#備註:
20164
		#備註:
20165
		#無.
20165
		#無.
20166
		$enCodeStr=authenticate::enCodeStr($conf["authenticate::enCodeStr"]);
20166
		$enCodeStr=authenticate::enCodeStr($conf["authenticate::enCodeStr"]);
20167
		unset($conf["authenticate::enCodeStr"]);
20167
		unset($conf["authenticate::enCodeStr"]);
20168
		
20168
 
20169
		#如果執行失敗
20169
		#如果執行失敗
20170
		if($enCodeStr["status"]==="false"){
20170
		if($enCodeStr["status"]==="false"){
20171
		
20171
 
20172
			#印出結果
20172
			#印出結果
20173
			return $enCodeStr;
20173
			return $enCodeStr;
20174
			
20174
 
20175
			}#if end
20175
			}#if end
20176
		
20176
 
20177
		#解碼取得的內容
20177
		#解碼取得的內容
20178
		$receivedData=json_decode($enCodeStr["content"]);
20178
		$receivedData=json_decode($enCodeStr["content"]);
20179
		
20179
 
20180
		#如果有設置 callFunc
20180
		#如果有設置 callFunc
20181
		if(isset($receivedData->callFunc)){
20181
		if(isset($receivedData->callFunc)){
20182
		
20182
 
20183
			#取得要呼叫的函式
20183
			#取得要呼叫的函式
20184
			$func=$receivedData->callFunc;
20184
			$func=$receivedData->callFunc;
20185
			
20185
 
20186
			#檢查是否有權限執行該函式
20186
			#檢查是否有權限執行該函式
20187
			#函式說明:
20187
			#函式說明:
20188
			#提供webSock::nativeSocketTcpIpServer用於接受檢查權限的功能.
20188
			#提供webSock::nativeSocketTcpIpServer用於接受檢查權限的功能.
20189
			#回傳結果:
20189
			#回傳結果:
20190
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
20190
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 20218... Line 20218...
20218
			#https://www.rfc-editor.org/rfc/rfc6455#page-28, Web Socket Base Framing Protocol.
20218
			#https://www.rfc-editor.org/rfc/rfc6455#page-28, Web Socket Base Framing Protocol.
20219
			#備註:
20219
			#備註:
20220
			#無.
20220
			#無.
20221
			$checkAuth=self::checkAuth($conf["self::checkAuth"]);
20221
			$checkAuth=self::checkAuth($conf["self::checkAuth"]);
20222
			unset($conf["self::checkAuth"]);
20222
			unset($conf["self::checkAuth"]);
20223
			
20223
 
20224
			#如果異常
20224
			#如果異常
20225
			if($checkAuth["status"]==="false"){
20225
			if($checkAuth["status"]==="false"){
20226
			
20226
 
20227
				#回傳結果
20227
				#回傳結果
20228
				return $checkAuth;
20228
				return $checkAuth;
20229
				
20229
 
20230
				}#if end
20230
				}#if end
20231
			
20231
 
20232
			#移除使用好的元素
20232
			#移除使用好的元素
20233
			unset($receivedData["callFunc"]);
20233
			unset($receivedData["callFunc"]);
20234
			
20234
 
20235
			#置換data為未加密的內容
20235
			#置換data為未加密的內容
20236
			$conf["data"]=json_encode($receivedData);
20236
			$conf["data"]=json_encode($receivedData);
20237
			
20237
 
20238
			#呼叫對應的函式
20238
			#呼叫對應的函式
20239
			return eval($func."(&\$conf);");
20239
			return eval($func."(&\$conf);");
20240
			
20240
 
20241
			}#if end
20241
			}#if end
20242
		
20242
 
20243
		#設置執行失敗
20243
		#設置執行失敗
20244
		$result["ststus"]="false";
20244
		$result["ststus"]="false";
20245
		
20245
 
20246
		#回傳結果
20246
		#回傳結果
20247
		return $result;
20247
		return $result;
20248
 
20248
 
20249
		}#function callFunc end
20249
		}#function callFunc end
20250
	
20250
 
20251
	}#class webSock end
20251
	}#class webSock end
20252
 
20252
 
20253
?>
-
 
20254
20253
?>
-
 
20254