-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.php
274 lines (266 loc) · 15.3 KB
/
server.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
// //========================================================================
// //========================PHP SERVER SCRIPT===============================
// // ==========SOCKET PROGRAMING============NETWORK PROJECT=================
// //==============================TadavomnisT===============================
// //=========================GNU LICENCED SCRIPT============================
//display error if you didn't enable the extention.
if (!extension_loaded("sockets")) die ("couldn't load socket extention.");
// setting some variables
ini_set('max_execution_time', 0);//preventingphp default timeout
define('HOST_NAME', "localhost");//running localhost
define('PORT', "15000");//defining port number 15000
$null = NULL;//define NULL in order to put in *** function
$names = [];//client names store here
$resources = [];//active client resources store here
// create socket - this is a TCP protocol socket
$socketResource = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Could not create socket\n");
// setting some options on the socket
socket_set_option($socketResource, SOL_SOCKET, SO_REUSEADDR, 1) or die("Could not set options\n");
// bind socket to port
socket_bind($socketResource, 0, PORT) or die("Could not bind to the port\n");
// start listening for connections
socket_listen($socketResource) or die("Could not listen for connections\n");
// passing sockets into an array in order to fetch it in while and communicate fo ever
$clientSocketArray = array(
$socketResource
);
// this endless loop, will handle sockets
while (true)
{
// getting current clients anew
$newSocketArray = $clientSocketArray;
// switching between sockets
socket_select($newSocketArray, $null, $null, 0, 10);
if (in_array($socketResource, $newSocketArray))// checking if the socket exist in new sockets lists
{
// accept incoming connections
// client another socket to handle communication
$newSocket = socket_accept($socketResource);
$clientSocketArray[] = $newSocket;// adding accepted socket connection to global socket arrray
$header = socket_read($newSocket, 1024);//reading data from socket
if( !doHandshake($header, $newSocket, HOST_NAME, PORT) )//hand shaking with socket
{
$newSocketIndex = array_search($newSocket, $clientSocketArray);
unset($clientSocketArray[$newSocketIndex]);//delete client's socket
continue;
}
socket_getpeername($newSocket, $client_ip_address);//getting client ip address and store it in $client_ip_address
$newSocketIndex = array_search($socketResource, $newSocketArray);//search for new sockets's index
unset($newSocketArray[$newSocketIndex]);//deleting fetched socket
}
foreach ($newSocketArray as $newSocketArrayResource)
{ //Query on every remained socket
while (@socket_recv($newSocketArrayResource, $socketData, 1024, 0) >= 1)
{ // recieving data ,while current each client is sending it
$socketMessage = unseal($socketData); //removing headers , and fetching pure data in json
$decoded_data = json_decode($socketMessage, true); //decoding json into array
if (isset($decoded_data["request"]) && $decoded_data["request"] != "")
{//checking if client sent a valid request
dump($decoded_data , "Request"); //dump data in order to store logs in file, developers only, comment this if you want
if ($decoded_data["request"] == "sign_up")
{//checking if client sent a valid sign_up request
if (isset($decoded_data["name"]) && trim($decoded_data["name"]) != "")
{//checking if client sent a valid name in request
$name = $decoded_data["name"]; //fetching choosed name from request
if (!in_array($name, $names)) //checking if name doesn't exist
{
$names[] = $name;//adding client name to global names
$resources[$name] = $newSocketArrayResource;//adding client resource to global resources , indexed name
// prepare a response request in orderd to send to new client
$response = json_encode(["request" => "message", "message" => ["message" => "Hi $name, welcome to the chat room.", "type" => "server", "from" => $name]]);
// prepare a join request in orderd to send to other clients
$join = json_encode(["request" => "message", "message" => ["message" => "$name joined the chat room.", "type" => "server", "from" => $name]]);
if (isset($response)) send($resources[$name], seal($response)); //send welcome notifnamesication
if (isset($join)) send2all(seal($join)); //send join notification to all
}
else
{// if name exists
// prepare a Error request in orderd to send to new client
$response = json_encode(["request" => "error", "error" => "Username already taken! Pick Somthing else."]);
if (isset($response)) send($newSocketArrayResource, seal($response));//send Error request
}
}
else
{// if username was empty
// prepare a Error request in orderd to send to new client
$response = json_encode(["request" => "error", "error" => "Username cannot be empty!"]);
if (isset($response)) send($newSocketArrayResource, seal($response));//send Error request
}
}
elseif ($decoded_data["request"] == "public_message")
{//checking if request was a public message
if (trim($decoded_data["message"]["message"]) != "")
{//fetching public message from request
// prepare a response request in orderd to send to every clients
$response = json_encode(["request" => "message", "message" => ["message" => trim($decoded_data["message"]["message"]) , "type" => "public", "from" => getName($newSocketArrayResource) ]]);
if (isset($response)) send2all(seal($response)); //send message to all
}
else
{// if message was empty
// prepare a Error request in orderd to send to new client
$response = json_encode(["request" => "error", "error" => "Message text was empty."]);
if (isset($response)) send($resources[getName($newSocketArrayResource) ], seal($response));//send Error request
}
}
elseif ($decoded_data["request"] == "private_message")
{//checking if request was a private message
if (trim($decoded_data["message"]["message"]) != "")
{//fetching private message from request
// prepare a response request in orderd to send to wanted clients
$response = json_encode(["request" => "message", "message" => ["message" => trim($decoded_data["message"]["message"]) , "type" => "private", "from" => getName($newSocketArrayResource) ]]);
if (isset($response)) foreach ($names as $name)//send private message to every spesific reciever
{
if (in_array($name, $decoded_data["message"]["to"]))
{
send($resources[$name], seal($response));
}
}// prepare a response request in orderd to send to client
$response = json_encode(["request" => "message", "message" => ["message" => trim($decoded_data["message"]["message"]) , "type" => "private_response", "from" => getName($newSocketArrayResource) , "to" => $decoded_data["message"]["to"]]]);
if (isset($response)) send($resources[getName($newSocketArrayResource) ], seal($response));//send response request
}
else
{// if message was empty
// prepare a Error request in orderd to send to new client
$response = json_encode(["request" => "error", "error" => "Message text was empty."]);
if (isset($response)) send($resources[getName($newSocketArrayResource) ], seal($response));//send Error request
}
}
elseif ($decoded_data["request"] == "participants")
{//checking if request was a get paticipants
// prepare a response request in orderd to send to client
$response = json_encode(["request" => "participants", "participants" => array_values($names) ]);
if (isset($response)) send($resources[getName($newSocketArrayResource) ], seal($response));//send response request
}
elseif ($decoded_data["request"] == "leave")
{//checking if request was a leave chatroom
// prepare a response request in orderd to send to every clients
$leave = json_encode(["request" => "message", "message" => ["message" => getName($newSocketArrayResource) . " left the chat room.", "type" => "server", "from" => getName($newSocketArrayResource) ]]);
if (isset($leave)) send2all(seal($leave)); //send leave notification to all
// unset($resources[getName($newSocketArrayResource)]);//delete client's resource
$newNameIndex = array_search(getName($newSocketArrayResource) , $names);
unset($names[$newNameIndex]);//delete client's name
$newSocketIndex = array_search($newSocketArrayResource, $clientSocketArray);
unset($clientSocketArray[$newSocketIndex]);//delete client's socket
}
}
break 2; //break from while , not the foreach
}
$socketData = @socket_read($newSocketArrayResource, 1024, PHP_NORMAL_READ);
if ($socketData === false)
{//testing if any of clients left the chatroom suddenly
// prepare a response request in orderd to send to every clients
$leave = json_encode(["request" => "message", "message" => ["message" => getName($newSocketArrayResource) . " left the chat room.", "type" => "server", "from" => getName($newSocketArrayResource) ]]);
if (isset($leave)) send2all(seal($leave)); //send leave notification to all
// unset($resources[getName($newSocketArrayResource)]);//delete client's resource
$newNameIndex = array_search(getName($newSocketArrayResource) , $names);
unset($names[$newNameIndex]);//delete client's name
$newSocketIndex = array_search($newSocketArrayResource, $clientSocketArray);
unset($clientSocketArray[$newSocketIndex]);//delete client's socket
}
}
}
//close socket, if every thing works fine, script never reaches here
socket_close($socketResource);
// ======================================================Functions======================================================
function getName($currentResource)
{//returns name of a resource
global $resources;
foreach ($resources as $key => $value) if ($value == $currentResource) return $key;
return false;
}
// ---------------------------------------------------------------------------------
function send2all($message)
{//sends every clients a message
global $resources;
$messageLength = strlen($message);
foreach ($resources as $clientSocket)
{
@socket_write($clientSocket, $message, $messageLength);
}
return true;
}
// ---------------------------------------------------------------------------------
function send($clientSocket, $message)
{//sends a client a message
$messageLength = strlen($message);
@socket_write($clientSocket, $message, $messageLength);
return true;
}
// ---------------------------------------------------------------------------------
function unseal($socketData)
{//an opensource GNU licenced function to Unmask pure data from websocket,
$length = ord($socketData[1]) & 127;
if ($length == 126)
{
$masks = substr($socketData, 4, 4);
$data = substr($socketData, 8);
}
elseif ($length == 127)
{
$masks = substr($socketData, 10, 4);
$data = substr($socketData, 14);
}
else
{
$masks = substr($socketData, 2, 4);
$data = substr($socketData, 6);
}
$socketData = "";
for ($i = 0;$i < strlen($data);++$i)
{
$socketData .= $data[$i] ^ $masks[$i % 4];
}
return $socketData;
}
// ---------------------------------------------------------------------------------
function seal($socketData)
{//an opensource GNU licenced function to Mask headers into data for websocket,
$b1 = 0x80 | (0x1 & 0x0f);
$length = strlen($socketData);
if ($length <= 125) $header = pack('CC', $b1, $length);
elseif ($length > 125 && $length < 65536) $header = pack('CCn', $b1, 126, $length);
elseif ($length >= 65536) $header = pack('CCNN', $b1, 127, $length);
return $header . $socketData;
}
function doHandshake($received_header, $client_socket_resource, $host_name, $port)
{//an opensource GNU licenced function to send an recieve headers in order to handshaking in websocket,
try {
$headers = array();
$lines = preg_split("/\r\n/", $received_header);
foreach ($lines as $line)
{
$line = chop($line);
if (preg_match('/\A(\S+): (.*)\z/', $line, $matches))
{
$headers[$matches[1]] = $matches[2];
}
}
$secKey = ( empty($headers['Sec-WebSocket-Key']) )? $headers['Sec-WebSocket-Key'] : false;
if ( !$secKey ) return false;
$secAccept = base64_encode(pack('H*', sha1($secKey . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));
$buffer = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" . "Upgrade: websocket\r\n" . "Connection: Upgrade\r\n" . "WebSocket-Origin: $host_name\r\n" . "WebSocket-Location: ws://$host_name:$port/demo/shout.php\r\n" . "Sec-WebSocket-Accept:$secAccept\r\n\r\n";
socket_write($client_socket_resource, $buffer, strlen($buffer));
return true;
} catch (\Throwable $th) {}
return false;
}
// ---------------------------------------------------------------------------------
function dump($dumpedVar, $dumpInfo = "dumped Variable")
{//dumps a variable/object in logs.txt in order to save logs
ob_start();
echo " $dumpInfo : ";
var_dump($dumpedVar);
echo PHP_EOL;
$rtempy = ob_get_clean();
$file = fopen("logs.txt", "a+") or die;
fwrite($file, $rtempy);
fclose($file);
}
// ====================================================End of Functions=======================================================
// //========================================================================
// //========================PHP SERVER SCRIPT===============================
// // ==========SOCKET PROGRAMING============NETWORK PROJECT=================
// //==============================TadavomnisT===============================
// //=========================GNU LICENCED SCRIPT============================
?>