body* vars renamed into content*
[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  *   - BUG: logout failed
27  *   - BUG: fast loop on stream index_rd_ifra page
28  *
29  *   - garbage management
30  *   - log_legal address fix
31  *   - from room to table
32  *   - from table to room
33  *   - fwrite other issues
34  *   - manage and test cross forwarder between table and room
35  *   - setcookie (for tables only)
36  *   - keepalive management
37  *   - chunked
38  *
39  *   DONE/FROZEN - problema con getpeer (HOSTADDR)
40  *
41  *   DONE - bug: read from a not resource handle (already closed because a new socket substitute it)
42  *   DONE - partial write for normal page management
43  *   DONE - index_rd_ifra: last_clean issue
44  *   DONE - fwrite failed error management (select, buffer where store unsent data, and fwrite check and retry)
45  *   ABRT - index_wr.php::reload - reload is js-only function
46  *   DONE - bug: after restart index_rd.php receive from prev clients a lot of req
47  *   DONE - index_wr.php::chat
48  *   DONE - index_wr.php::exit
49  *   DONE - index_rd.php porting
50  *   DONE - generic var management from internet
51  *   DONE - index.php auth part
52  */
53
54 $G_base = "../";
55
56 require_once("./sac-a-push.phh");
57 require_once("./brisk-spush.phh");
58 require_once($G_base."Obj/brisk.phh");
59 require_once($G_base."Obj/auth.phh");
60 // require_once("../Obj/proxyscan.phh");
61 require_once($G_base."index.php");
62 require_once($G_base."index_wr.php");
63 require_once($G_base."index_rd_ifra.php");
64 require_once($G_base."briskin5/Obj/briskin5.phh");
65
66 define('SITE_PREFIX', '/brisk/');
67
68 function headers_render($header)
69 {
70     
71     $s = "";
72     $s .= "HTTP/1.1 200 OK\r\n";
73     if (!isset($header['Date']))
74         $s .= sprintf("Date: %s\r\n", date(DATE_RFC822));
75     if (!isset($header['Connection']))
76         $s .= "Connection: close\r\n";
77     if (!isset($header['Content-Type']))
78         $s .= "Content-Type: text/html\r\n";
79     foreach($header as $key => $value) {
80         $s .= sprintf("%s: %s\r\n", $key, $value);
81     }
82     $s .= "Mop: was/here\r\n";
83     $s .= "\r\n";
84
85     return ($s);
86 }
87
88 /*
89  *  Caching system using ob php system to cache old style pages
90  *  to a var and than send it with more calm
91  */
92 $G_headers = "";
93
94 function shutta()
95 {
96   log_rd2("SHUTTA [".connection_status()."] !");
97 }
98
99 register_shutdown_function('shutta');
100
101 /*
102  *  MAIN
103  */
104 $shutdown = FALSE;
105
106 function main()
107 {
108     GLOBAL $G_headers;
109     GLOBAL $shutdown;
110     $main_loop = TRUE;
111
112     /*
113      *  INIT
114      */
115
116     $FILE_SOCKET = "/tmp/brisk.sock";
117     $UNIX_SOCKET = "unix://$FILE_SOCKET";
118     $debug = 0;
119     $fixed_fd = 2;
120     $socks = array();
121
122     $blocking_mode = 0; // 0 for non-blocking
123
124     if (($room = Room::create()) == FALSE) {
125         log_crit("load_data failed");
126         return FALSE;
127     }
128
129     $s2u  = array();
130     $pages_flush = array();
131
132     $rndstr = "";
133     for ($i = 0 ; $i < 4096 ; $i++) {
134         $rndstr .= chr(mt_rand(65, 90));
135     }
136
137     if (file_exists($FILE_SOCKET)) {
138         unlink($FILE_SOCKET);
139     }
140     
141     $old_umask = umask(0);
142     if (($list = stream_socket_server($UNIX_SOCKET, $err, $errs)) === FALSE) {
143         exit(11);
144     }
145     umask($old_umask);
146
147     if (($in = fopen("php://stdin", "r")) === FALSE) {
148         exit(11);
149     }
150
151     stream_set_blocking($list, $blocking_mode); # Set the stream to non-blocking
152
153     while ($main_loop) {
154         $curtime = time();
155         printf("IN LOOP: Current opened: %d  pages_flush: %d\n", count($socks), count($pages_flush));
156
157         /* Prepare the read array */
158         if ($shutdown) 
159             $read   = array_merge(array("$in" => $in), $socks);
160         else
161             $read   = array_merge(array("$list" => $list, "$in" => $in), $socks);
162
163         if ($debug > 1) {
164             printf("PRE_SELECT\n");
165             print_r($read);
166         }
167         $write  = NULL;
168         $except = NULL;
169         $num_changed_sockets = stream_select($read, $write, $except, 0, 250000);
170         
171         if ($num_changed_sockets === FALSE) {
172             printf("No data in 5 secs");
173         } 
174         else if ($num_changed_sockets > 0) {
175             printf("num sock %d num_of_socket: %d\n", $num_changed_sockets, count($read));
176             if ($debug > 1) {
177                 print_r($read);
178             }
179             /* At least at one of the sockets something interesting happened */
180             foreach ($read as $i => $sock) {
181                 /* is_resource check is required because there is the possibility that
182                    during new request an old connection is closed */
183                 if (!is_resource($sock)) {
184                     continue;
185                 }
186                 if ($sock === $list) {
187                     printf("NUOVA CONNEX\n");
188                     $new_unix = stream_socket_accept($list);
189                     $stream_info = "";
190                     $method      = "";
191                     $get         = array();
192                     $post        = array();
193                     $cookie      = array();
194                     if (($new_socket = ancillary_getstream($new_unix, $stream_info)) !== FALSE) {
195                         stream_set_blocking($new_socket, $blocking_mode); // Set the stream to non-blocking
196                         printf("RECEIVED HEADER:\n%s", $stream_info);
197                         $path = spu_process_info($stream_info, $method, $header, $get, $post, $cookie);
198                         printf("PATH: [%s]\n", $path);
199                         printf("M: %s\nHEADER:\n", $method);
200                         print_r($header);
201                         printf("GET:\n");
202                         print_r($get);
203                         printf("POST:\n");
204                         print_r($post);
205                         printf("COOKIE:\n");
206                         print_r($cookie);
207
208                         $addr = stream_socket_get_name($new_socket, TRUE);
209
210                         switch ($path) {
211                         case SITE_PREFIX:
212                         case SITE_PREFIX."index.php":
213                             $header_out = array();
214                             ob_start();
215                             index_main($room, $header_out, $addr, $get, $post, $cookie);
216                             $content = ob_get_contents();
217                             ob_end_clean();
218
219                             $pgflush = new PageFlush($new_socket, $curtime, 20, $header_out, $content);
220
221                             if ($pgflush->try_flush($curtime) == FALSE) {
222                                 // Add $pgflush to the pgflush array
223                                 array_push($pages_flush, $pgflush);
224                             }
225
226                             break;
227                         case SITE_PREFIX."index_wr.php":
228                             $header_out = array();
229                             ob_start();
230                             index_wr_main($room, $addr, $get, $post, $cookie);
231                             $content = ob_get_contents();
232                             ob_end_clean();
233
234                             $pgflush = new PageFlush($new_socket, $curtime, 20, $header_out, $content);
235
236                             if ($pgflush->try_flush($curtime) == FALSE) {
237                                 // Add $pgflush to the pgflush array
238                                 array_push($pages_flush, $pgflush);
239                             }
240                             break;
241                         case SITE_PREFIX."index_rd_ifra.php":
242                             do {
243                                 $header_out = array();
244                                 if (!isset($cookie['sess'])
245                                     || (($user = $room->get_user($cookie['sess'], $idx)) == FALSE)) {
246                                     $content = index_rd_ifra_fini(TRUE);
247                                     
248                                     $pgflush = new PageFlush($new_socket, $curtime, 20, $header_out, $content);
249
250                                     if ($pgflush->try_flush($curtime) == FALSE) {
251                                         // Add $pgflush to the pgflush array
252                                         array_push($pages_flush, $pgflush);
253                                     }
254                                     break;
255                                 }
256                                 // close a previous opened index_read_ifra socket, if exists
257                                 if (($prev = $user->rd_socket_get()) != NULL) {
258                                     unset($s2u[intval($user->rd_socket_get())]);
259                                     unset($socks[intval($user->rd_socket_get())]);
260                                     fclose($user->rd_socket_get());
261                                     printf("CLOSE AND OPEN AGAIN ON IFRA2\n");
262                                     $user->rd_socket_set(NULL);
263                                 }
264
265                                 $content = "";
266                                 index_rd_ifra_init($room, $user, $header_out, $content, $get, $post, $cookie);
267                                 $content_l = mb_strlen($content, "ASCII");
268
269                                 $wret = @fwrite($new_socket, headers_render($header_out).$content);
270                                 if ($wret < $content_l) {
271                                     printf("TROUBLES WITH FWRITE: %d\n", $ret);
272                                     $user->rd_cache_set(mb_substr($content, $ret, $content_l - $ret, "ASCII"));
273                                 }
274                                 else {
275                                     $user->rd_cache_set("");
276                                 }
277                                 fflush($new_socket);
278
279                                 $s2u[intval($new_socket)] = $idx;
280                                 $socks[intval($new_socket)] = $new_socket;                                
281                                 $user->rd_socket_set($new_socket);
282                             } while (FALSE);
283
284                             break;
285                         }
286                     }
287                     else {
288                         printf("WARNING: ancillary_getstream failed\n");
289                     }
290                 }
291                 else {
292                     if (($buf = fread($sock, 512)) === FALSE) {
293                         printf("error read\n");
294                         exit(123);
295                     }
296                     else if (strlen($buf) === 0) {
297                         if ($sock === $list) {
298                             printf("Arrivati %d bytes da list\n", strlen($buf));
299                         }
300                         else if ($sock === $in) {
301                             printf("Arrivati %d bytes da stdin\n", strlen($buf));
302                         }
303                         else {
304                             // $user_a[$s2u[intval($sock)]]->disable();
305                             if ($room->user[$s2u[intval($sock)]]->rd_socket_get() != NULL) {
306                                 $room->user[$s2u[intval($sock)]]->rd_socket_set(NULL);
307                             }
308                             unset($socks[intval($sock)]);
309                             unset($s2u[intval($sock)]);
310                             fclose($sock);
311                             printf("CLOSE ON READ\n");
312                         }
313                         if ($debug > 1) {
314                             printf("post unset\n");
315                             print_r($socks);
316                         }
317                     }
318                     else {
319                         if ($debug > 1) {
320                             print_r($read);
321                         }
322                         if ($sock === $list) {
323                             printf("Arrivati %d bytes da list\n", strlen($buf));
324                         }
325                         else if ($sock === $in) {
326                             printf("Arrivati %d bytes da stdin\n", strlen($buf));
327                         }
328                         else {
329                             $key = array_search("$sock", $socks);
330                             printf("Arrivati %d bytes dalla socket n. %d\n", strlen($buf), $key);
331                         }
332                     }
333                 }
334             }
335         }
336
337
338         foreach ($pages_flush as $k => $pgflush) {
339             if ($pgflush->try_flush($curtime) == TRUE) {
340                 unset($pages_flush[$k]);
341             }
342         }
343
344         foreach ($socks as $k => $sock) {
345             if (isset($s2u[intval($sock)])) {
346                 $user = $room->user[$s2u[intval($sock)]];
347                 $content = $user->rd_cache_get();
348                 if ($content == "")
349                     index_rd_ifra_main($room, $user, $content);
350
351                 if ($content == "" && $user->rd_kalive_is_expired($curtime)) {
352                     $content = index_rd_ifra_keepalive($user);
353                 }
354
355                 if ($content != "") {
356                     echo "SPIA: [".substr($content, 0, 60)."...]\n";
357                     $content_l = mb_strlen($content, "ASCII");
358                     $wret = @fwrite($sock, $content);
359                     if ($wret < $content_l) {
360                         printf("TROUBLE WITH FWRITE: %d\n", $wret);
361                         $user->rd_cache_set(mb_substr($content, $wret, $content_l - $wret, "ASCII"));
362                     }
363                     else {
364                         $user->rd_cache_set("");
365                     }
366                     fflush($sock);
367                     $user->rd_kalive_reset($curtime);
368                 }
369
370                 // close socket after a while to prevent client memory consumption
371                 if ($user->rd_endtime_is_expired($curtime)) {
372                     // $user_a[$s2u[intval($sock)]]->disable();
373                     if ($room->user[$s2u[intval($sock)]]->rd_socket_get() != NULL) {
374                         $room->user[$s2u[intval($sock)]]->rd_socket_set(NULL);
375                     }
376                     unset($socks[intval($sock)]);
377                     unset($s2u[intval($sock)]);
378                     fclose($sock);
379                     printf("CLOSE ON LOOP\n");
380                 }
381             }
382         }
383     }
384     
385     exit(0);
386 }
387
388 main();
389 ?>