TODO updated
[brisk.git] / web / spush / brisk-spush.php
1 #!/usr/bin/php
2 <?php
3 /*
4  *  brisk - spush/brisk-spush.php
5  *
6  *  Copyright (C) 2012 Matteo Nastasi
7  *                          mailto: nastasi@alternativeoutput.it 
8  *                                  matteo.nastasi@milug.org
9  *                          web: http://www.alternativeoutput.it
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * General Public License for more details. You should have received a
20  * copy of the GNU General Public License along with this program; if
21  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
22  * Suite 330, Boston, MA 02111-1307, USA.
23  *
24  * TODO
25  *
26  *   - from room to table
27  *   - from table to room
28  *   - fwrite other issues
29  *   - manage and test cross forwarder between table and room
30  *   - setcookie (for tables only)
31  *   - keepalive management
32  *   - chunked
33  *
34  *   DONE/FROZEN - problema con getpeer (HOSTADDR)
35  *
36  *   DONE - index_rd_ifra: last_clean issue
37  *   DONE - fwrite failed error management (select, buffer where store unsent data, and fwrite check and retry)
38  *   ABRT - index_wr.php::reload - reload is js-only function
39  *   DONE - bug: after restart index_rd.php receive from prev clients a lot of req
40  *   DONE - index_wr.php::chat
41  *   DONE - index_wr.php::exit
42  *   DONE - index_rd.php porting
43  *   DONE - generic var management from internet
44  *   DONE - index.php auth part
45  */
46
47 $G_base = "../";
48
49 require_once("./sac-a-push.phh");
50 require_once("./brisk-spush.phh");
51 require_once($G_base."Obj/brisk.phh");
52 require_once($G_base."Obj/auth.phh");
53 // require_once("../Obj/proxyscan.phh");
54 require_once($G_base."index.php");
55 require_once($G_base."index_wr.php");
56 require_once($G_base."index_rd_ifra.php");
57 require_once($G_base."briskin5/Obj/briskin5.phh");
58
59 define('SITE_PREFIX', '/brisk/');
60
61 function headers_render($header)
62 {
63     
64     $s = "";
65     $s .= "HTTP/1.1 200 OK\r\n";
66     if (!isset($header['Date']))
67         $s .= sprintf("Date: %s\r\n", date(DATE_RFC822));
68     if (!isset($header['Connection']))
69         $s .= "Connection: close\r\n";
70     if (!isset($header['Content-Type']))
71         $s .= "Content-Type: text/html\r\n";
72     foreach($header as $key => $value) {
73         $s .= sprintf("%s: %s\r\n", $key, $value);
74     }
75     $s .= "Mop: was/here\r\n";
76     $s .= "\r\n";
77
78     return ($s);
79 }
80
81 /*
82  *  Caching system using ob php system to cache old style pages
83  *  to a var and than send it with more calm
84  */
85 $G_headers = "";
86
87 function shutta()
88 {
89   log_rd2("SHUTTA [".connection_status()."] !");
90 }
91
92 register_shutdown_function('shutta');
93
94 /*
95  *  MAIN
96  */
97 $shutdown = FALSE;
98
99 function main()
100 {
101     GLOBAL $G_headers;
102     GLOBAL $shutdown;
103     $main_loop = TRUE;
104
105     /*
106      *  INIT
107      */
108
109     $FILE_SOCKET = "/tmp/brisk.sock";
110     $UNIX_SOCKET = "unix://$FILE_SOCKET";
111     $debug = 0;
112     $fixed_fd = 2;
113     $socks = array();
114
115     $blocking_mode = 0; // 0 for non-blocking
116
117     if (($room = Room::create()) == FALSE) {
118         log_crit("load_data failed");
119         return FALSE;
120     }
121
122     $s2u  = array();
123
124     $rndstr = "";
125     for ($i = 0 ; $i < 4096 ; $i++) {
126         $rndstr .= chr(mt_rand(65, 90));
127     }
128
129     if (file_exists($FILE_SOCKET)) {
130         unlink($FILE_SOCKET);
131     }
132     
133     $old_umask = umask(0);
134     if (($list = stream_socket_server($UNIX_SOCKET, $err, $errs)) === FALSE) {
135         exit(11);
136     }
137     umask($old_umask);
138
139     if (($in = fopen("php://stdin", "r")) === FALSE) {
140         exit(11);
141     }
142
143     stream_set_blocking($list, $blocking_mode); # Set the stream to non-blocking
144
145     while ($main_loop) {
146         $curtime = time();
147         printf("IN LOOP: Current opened: %d\n", count($socks));
148
149         /* Prepare the read array */
150         if ($shutdown) 
151             $read   = array_merge(array("$in" => $in), $socks);
152         else
153             $read   = array_merge(array("$list" => $list, "$in" => $in), $socks);
154
155         if ($debug > 1) {
156             printf("PRE_SELECT\n");
157             print_r($read);
158         }
159         $write  = NULL;
160         $except = NULL;
161         $num_changed_sockets = stream_select($read, $write, $except, 0, 250000);
162         
163         if ($num_changed_sockets === FALSE) {
164             printf("No data in 5 secs");
165         } 
166         else if ($num_changed_sockets > 0) {
167             printf("num sock %d num_of_socket: %d\n", $num_changed_sockets, count($read));
168             if ($debug > 1) {
169                 print_r($read);
170             }
171             /* At least at one of the sockets something interesting happened */
172             foreach ($read as $i => $sock) {
173                 if ($sock === $list) {
174                     printf("NUOVA CONNEX\n");
175                     $new_unix = stream_socket_accept($list);
176                     $stream_info = "";
177                     $method      = "";
178                     $get         = array();
179                     $post        = array();
180                     $cookie      = array();
181                     if (($new_socket = ancillary_getstream($new_unix, $stream_info)) !== FALSE) {
182                         stream_set_blocking($new_socket, $blocking_mode); // Set the stream to non-blocking
183                         printf("RECEIVED HEADER:\n%s", $stream_info);
184                         $path = spu_process_info($stream_info, $method, $header, $get, $post, $cookie);
185                         printf("PATH: [%s]\n", $path);
186                         printf("M: %s\nHEADER:\n", $method);
187                         print_r($header);
188                         printf("GET:\n");
189                         print_r($get);
190                         printf("POST:\n");
191                         print_r($post);
192                         printf("COOKIE:\n");
193                         print_r($cookie);
194
195                         $addr = stream_socket_get_name($new_socket, TRUE);
196
197                         switch ($path) {
198                         case SITE_PREFIX:
199                         case SITE_PREFIX."index.php":
200                             $header_out = array();
201                             ob_start();
202                             index_main($room, $header_out, $addr, $get, $post, $cookie);
203                             $content = ob_get_contents();
204                             ob_end_clean();
205                             // printf("OUT: [%s]\n", $G_content);
206                             fwrite($new_socket, headers_render($header_out).$content);
207                             fclose($new_socket);
208                             break;
209                         case SITE_PREFIX."index_wr.php":
210                             $header_out = array();
211                             $addr = "";
212                             // $ret = socket_getpeername($new_socket, $addr);
213                             printf("RET: %s\n", $addr);
214                             // exit(123);
215                             ob_start();
216                             index_wr_main($room, $addr, $get, $post, $cookie);
217                             $content = ob_get_contents();
218                             ob_end_clean();
219                             
220                             // printf("OUT: [%s]\n", $G_content);
221                             fwrite($new_socket, headers_render($header_out).$content);
222                             fclose($new_socket);
223                             break;
224                         case SITE_PREFIX."index_rd_ifra.php":
225                             do {
226                                 $header_out = array();
227                                 if (!isset($cookie['sess'])
228                                     || (($user = $room->get_user($cookie['sess'], $idx)) == FALSE)) {
229                                     $body = index_rd_ifra_fini(TRUE);
230                                     fwrite($new_socket, headers_render($header_out).$body);
231                                     fflush($new_socket);
232                                     fclose($new_socket);
233                                     break;
234                                 }
235                                 // close a previous opened index_read_ifra socket, if exists
236                                 if (($prev = $user->rd_socket_get()) != NULL) {
237                                     unset($s2u[intval($user->rd_socket_get())]);
238                                     unset($socks[intval($user->rd_socket_get())]);
239                                     fclose($user->rd_socket_get());
240                                     printf("CLOSE AND OPEN AGAIN ON IFRA2\n");
241                                     $user->rd_socket_set(NULL);
242                                 }
243
244                                 $body = "";
245                                 index_rd_ifra_init($room, $user, $header_out, $body, $get, $post, $cookie);
246                                 fwrite($new_socket, headers_render($header_out).$body);
247                                 fflush($new_socket);
248
249                                 $s2u[intval($new_socket)] = $idx;
250                                 $socks[intval($new_socket)] = $new_socket;                                
251                                 $user->rd_socket_set($new_socket);
252                             } while (FALSE);
253
254                             break;
255                         }
256                     }
257                     else {
258                         printf("WARNING: ancillary_getstream failed\n");
259                     }
260                 }
261                 else {
262                     if (($buf = fread($sock, 512)) === FALSE) {
263                         printf("error read\n");
264                         exit(123);
265                     }
266                     else if (strlen($buf) === 0) {
267                         if ($sock === $list) {
268                             printf("Arrivati %d bytes da list\n", strlen($buf));
269                         }
270                         else if ($sock === $in) {
271                             printf("Arrivati %d bytes da stdin\n", strlen($buf));
272                         }
273                         else {
274                             // $user_a[$s2u[intval($sock)]]->disable();
275                             if ($room->user[$s2u[intval($sock)]]->rd_socket_get() != NULL) {
276                                 $room->user[$s2u[intval($sock)]]->rd_socket_set(NULL);
277                             }
278                             unset($socks[intval($sock)]);
279                             unset($s2u[intval($sock)]);
280                             fclose($sock);
281                             printf("CLOSE ON READ\n");
282                         }
283                         if ($debug > 1) {
284                             printf("post unset\n");
285                             print_r($socks);
286                         }
287                     }
288                     else {
289                         if ($debug > 1) {
290                             print_r($read);
291                         }
292                         if ($sock === $list) {
293                             printf("Arrivati %d bytes da list\n", strlen($buf));
294                         }
295                         else if ($sock === $in) {
296                             printf("Arrivati %d bytes da stdin\n", strlen($buf));
297                         }
298                         else {
299                             $key = array_search("$sock", $socks);
300                             printf("Arrivati %d bytes dalla socket n. %d\n", strlen($buf), $key);
301                         }
302                     }
303                 }
304             }
305         }
306
307         foreach ($socks as $k => $sock) {
308             if (isset($s2u[intval($sock)])) {
309                 $user = $room->user[$s2u[intval($sock)]];
310                 $body = $user->rd_cache_get();
311                 if ($body == "")
312                     index_rd_ifra_main($room, $user, $body);
313
314                 if ($body == "" && $user->rd_kalive_is_expired($curtime)) {
315                     $body = index_rd_ifra_keepalive($user);
316                 }
317
318                 if ($body != "") {
319                     echo "SPIA: [".substr($body, 0, 60)."...]\n";
320                     $body_l = mb_strlen($body, "LATIN1");
321                     $ret = @fwrite($sock, $body);
322                     if ($ret < $body_l) {
323                         printf("TROUBLE WITH FWRITE: %d\n", $ret);
324                         $user->rd_cache_set(mb_substr($body, $ret, $body_l - $ret, "LATIN1"));
325                     }
326                     else {
327                         $user->rd_cache_set("");
328                     }
329                     fflush($sock);
330                     $user->rd_kalive_reset($curtime);
331                 }
332
333                 // close socket after a while to prevent client memory consumption
334                 if ($user->rd_endtime_is_expired($curtime)) {
335                     // $user_a[$s2u[intval($sock)]]->disable();
336                     if ($room->user[$s2u[intval($sock)]]->rd_socket_get() != NULL) {
337                         $room->user[$s2u[intval($sock)]]->rd_socket_set(NULL);
338                     }
339                     unset($socks[intval($sock)]);
340                     unset($s2u[intval($sock)]);
341                     fclose($sock);
342                     printf("CLOSE ON LOOP\n");
343                 }
344             }
345         }
346     }
347     
348     exit(0);
349 }
350
351 main();
352 ?>