Added really lame script to update copyright year.
[hub.git] / contrib / chat-server.php
1 <?php
2
3 error_reporting(E_ALL | E_STRICT);
4 set_time_limit(0);
5
6 require('udp-inc.php');
7
8 $port = 60825;
9 $host = '0.0.0.0';
10
11 $leaving = array('/leave', '/logout', '/bye', '/quit');
12 $shutdown = array('/shutdown', '/halt');
13 $masters = array('127.0.0.1');
14
15 // create a streaming socket, of type TCP/IP
16 $main_sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
17
18 // set the option to reuse the port
19 socket_set_option($main_sock, SOL_SOCKET, SO_REUSEADDR, 1);
20
21 // "bind" the socket to the address to "localhost", on port $port
22 // so this means that all connections on this port are now our resposibility to send/recv data, disconnect, etc..
23 socket_bind($main_sock, $host, $port);
24
25 // start listen for connections
26 socket_listen($main_sock);
27
28 // create a list of all the clients that will be connected to us..
29 // add the listening socket to this list
30 $clients = array($main_sock);
31
32 out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:Server listens on {$host}:{$port}");
33
34 while (count($clients) > 0) {
35         // create a copy, so $clients doesn't get modified by socket_select()
36         $read = $clients;
37
38         // get a list of all the clients that have data to be read from
39         // if there are no clients with data, go to next iteration
40         $left = @socket_select($read, $write = null, $except = null, 0, 150);
41         if ($left < 1) {
42                 continue;
43         }
44
45         // check if there is a client trying to connect
46         if (in_array($main_sock, $read)) {
47                 // accept the client, and add him to the $clients array
48                 $new_sock = socket_accept($main_sock);
49                 $clients[] = $new_sock;
50
51                 // send the client a welcome message
52                 socket_write($new_sock, "No noobs, but I'll make an exception. :)\n".
53                 "There are ".(count($clients) - 1)." client(s) connected to the server.\n");
54
55                 socket_getpeername($new_sock, $ip);
56                 out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:New client connected: {$ip}");
57
58                 // Notify all chatter
59                 if (count($clients) > 2) {
60                         foreach ($clients as $send_sock) {
61                                 if ($send_sock != $main_sock && $send_sock != $new_sock) {
62                                         socket_write($send_sock, "Server: Chatter has joined from {$ip}. There are now ".(count($clients) - 1)." clients.\n");
63                                 }
64                         }
65                 }
66
67                 // remove the listening socket from the clients-with-data array
68                 $key = array_search($main_sock, $read);
69                 unset($read[$key]);
70         }
71
72         // loop through all the clients that have data to read from
73         foreach ($read as $read_sock) {
74                 // Get client data
75                 socket_getpeername($read_sock, $ip);
76
77                 // read until newline or 1024 bytes
78                 // socket_read while show errors when the client is disconnected, so silence the error messages
79                 $data = @socket_read($read_sock, 1024, PHP_NORMAL_READ);
80
81                 // check if the client is disconnected
82                 if (($data === FALSE) || (in_array(strtolower(trim($data)), $leaving))) {
83
84                         // remove client for $clients array
85                         $key = array_search($read_sock, $clients);
86                         unset($clients[$key]);
87                         out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:Client from {$ip} disconnected. Left: ".(count($clients) - 1)."");
88
89                         // Notify all chatter
90                         if (count($clients) > 1) {
91                                 foreach ($clients as $send_sock) {
92                                         if ($send_sock != $main_sock) {
93                                                 socket_write($send_sock, "Server: Chatter from {$ip} has logged out. ".(count($clients) - 1)." client(s) left.\n");
94                                         }
95                                 }
96                         }
97
98                         // continue to the next client to read from, if any
99                         socket_write($read_sock, "Server: Good bye.\n");
100                         socket_shutdown($read_sock, 2);
101                         socket_close($read_sock);
102                         continue;
103                 } elseif (in_array(trim($data), $shutdown)) {
104                         // Is he allowed to shutdown?
105                         if (!in_array($ip, $masters)) {
106                                 out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:Client {$ip} has tried to shutdown the server!");
107                                 socket_write($read_sock, "Server: You are not allowed to shutdown the server!\n");
108                                 $data = "";
109                                 continue;
110                         }
111
112                         // Close all connections a leave here
113                         foreach ($clients as $client) {
114                                 // Send message to client
115                                 if ($client !== $main_sock && $client != $read_sock) {
116                                         socket_write($client, "Server: Shutting down! Thank you for joining us.\n");
117                                 }
118
119                                 // Quit him
120                                 socket_shutdown($client, 2);
121                                 socket_close($client);
122                         } // end foreach
123
124                         // Leave the loop
125                         $data = "";
126                         $clients = array();
127                         continue;
128                 }
129
130                 // trim off the trailing/beginning white spaces
131                 $data = trim($data);
132
133                 // Test for HTML codes
134                 $tags = strip_tags($data);
135
136                 // check if there is any data after trimming off the spaces
137                 if (!empty($data) && $tags == $data && count($clients) > 2) {
138                         // Send confirmation to "chatter"
139                         socket_write($read_sock, "\nServer: Message accepted.\n");
140
141                         // send this to all the clients in the $clients array (except the first one, which is a listening socket)
142                         foreach ($clients as $send_sock) {
143
144                                 // if its the listening sock or the client that we got the message from, go to the next one in the list
145                                 if ($send_sock == $main_sock || $send_sock == $read_sock)
146                                         continue;
147
148                                 // write the message to the client -- add a newline character to the end of the message
149                                 socket_write($send_sock, "{$ip}:{$data}\n");
150
151                         } // end of broadcast foreach
152                 } elseif ($tags != $data) {
153                         // HTML codes are not allowed
154                         out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:Client {$ip} has entered HTML code!");
155                         socket_write($read_sock, "Server: HTML is forbidden!\n");
156                 } elseif ((count($clients) == 2) && ($read_sock != $main_sock)) {
157                         // No one else will hear the "chatter"
158                         out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:Client {$ip} speaks with himself.");
159                         socket_write($read_sock, "Server: No one will hear you!\n");
160                 }
161         } // end of reading foreach
162 }
163
164 // close the listening socket
165 socket_close($main_sock);
166
167 ?>