4 * brisk - spush/brisk-spush.php
6 * Copyright (C) 2012 Matteo Nastasi
7 * mailto: nastasi@alternativeoutput.it
8 * matteo.nastasi@milug.org
9 * web: http://www.alternativeoutput.it
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.
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.
27 * - BUG: logout failed
28 * - BUG: fast loop on stream index_rd_ifra page
30 * - garbage management
31 * - log_legal address fix
32 * - from room to table
33 * - from table to room
34 * - index_wr other issues
35 * - manage and test cross forwarder between table and room
36 * - setcookie (for tables only)
37 * - keepalive management
39 * DONE/FROZEN - problema con getpeer (HOSTADDR)
42 * DONE - bug: read from a not resource handle (already closed because a new socket substitute it)
43 * DONE - partial write for normal page management
44 * DONE - index_rd_ifra: last_clean issue
45 * DONE - fwrite failed error management (select, buffer where store unsent data, and fwrite check and retry)
46 * ABRT - index_wr.php::reload - reload is js-only function
47 * DONE - bug: after restart index_rd.php receive from prev clients a lot of req
48 * DONE - index_wr.php::chat
49 * DONE - index_wr.php::exit
50 * DONE - index_rd.php porting
51 * DONE - generic var management from internet
52 * DONE - index.php auth part
57 require_once("./sac-a-push.phh");
58 require_once("./brisk-spush.phh");
59 require_once($G_base."Obj/brisk.phh");
60 require_once($G_base."Obj/auth.phh");
61 // require_once("../Obj/proxyscan.phh");
62 require_once($G_base."index.php");
63 require_once($G_base."index_wr.php");
64 require_once($G_base."index_rd_ifra.php");
65 require_once($G_base."briskin5/Obj/briskin5.phh");
67 define('SITE_PREFIX', '/brisk/');
69 function headers_render($header, $len)
73 $s .= "HTTP/1.1 200 OK\r\n";
74 if (!isset($header['Date']))
75 $s .= sprintf("Date: %s\r\n", date(DATE_RFC822));
76 if (!isset($header['Connection']))
77 $s .= "Connection: close\r\n";
78 if (!isset($header['Content-Type']))
79 $s .= "Content-Type: text/html\r\n";
80 foreach($header as $key => $value) {
81 $s .= sprintf("%s: %s\r\n", $key, $value);
84 $s .= "Cache-Control: no-cache, must-revalidate\r\n";
85 $s .= "Expires: Mon, 26 Jul 1997 05:00:00 GMT\r\n";
86 $s .= "Content-Encoding: chunked\r\n";
87 $s .= "Transfer-Encoding: chunked\r\n";
90 $s .= sprintf("Content-Length: %d\r\n", $len);
98 * Caching system using ob php system to cache old style pages
99 * to a var and than send it with more calm
104 log_rd2("SHUTTA [".connection_status()."] !");
107 register_shutdown_function('shutta');
113 function chunked_content($content)
115 $content_l = mb_strlen($content, "ASCII");
117 return (sprintf("%X\r\n%s\r\n", $content_l, $content));
120 function chunked_fini()
122 return sprintf("0\r\n");
135 $FILE_SOCKET = "/tmp/brisk.sock";
136 $UNIX_SOCKET = "unix://$FILE_SOCKET";
141 $blocking_mode = 0; // 0 for non-blocking
143 if (($room = Room::create()) == FALSE) {
144 log_crit("load_data failed");
149 $pages_flush = array();
152 for ($i = 0 ; $i < 4096 ; $i++) {
153 $rndstr .= chr(mt_rand(65, 90));
156 if (file_exists($FILE_SOCKET)) {
157 unlink($FILE_SOCKET);
160 $old_umask = umask(0);
161 if (($list = stream_socket_server($UNIX_SOCKET, $err, $errs)) === FALSE) {
166 if (($in = fopen("php://stdin", "r")) === FALSE) {
170 stream_set_blocking($list, $blocking_mode); # Set the stream to non-blocking
174 printf("IN LOOP: Current opened: %d pages_flush: %d\n", count($socks), count($pages_flush));
176 /* Prepare the read array */
178 $read = array_merge(array("$in" => $in), $socks);
180 $read = array_merge(array("$list" => $list, "$in" => $in), $socks);
183 printf("PRE_SELECT\n");
188 $num_changed_sockets = stream_select($read, $write, $except, 0, 250000);
190 if ($num_changed_sockets === FALSE) {
191 printf("No data in 5 secs");
193 else if ($num_changed_sockets > 0) {
194 printf("num sock %d num_of_socket: %d\n", $num_changed_sockets, count($read));
198 /* At least at one of the sockets something interesting happened */
199 foreach ($read as $i => $sock) {
200 /* is_resource check is required because there is the possibility that
201 during new request an old connection is closed */
202 if (!is_resource($sock)) {
205 if ($sock === $list) {
206 printf("NUOVA CONNEX\n");
207 $new_unix = stream_socket_accept($list);
213 if (($new_socket = ancillary_getstream($new_unix, $stream_info)) !== FALSE) {
214 stream_set_blocking($new_socket, $blocking_mode); // Set the stream to non-blocking
215 printf("RECEIVED HEADER:\n%s", $stream_info);
216 $path = spu_process_info($stream_info, $method, $header, $get, $post, $cookie);
217 printf("PATH: [%s]\n", $path);
218 printf("M: %s\nHEADER:\n", $method);
227 $addr = stream_socket_get_name($new_socket, TRUE);
231 case SITE_PREFIX."index.php":
232 $header_out = array();
234 index_main($room, $header_out, $addr, $get, $post, $cookie);
235 $content = ob_get_contents();
238 $pgflush = new PageFlush($new_socket, $curtime, 20, $header_out, $content);
240 if ($pgflush->try_flush($curtime) == FALSE) {
241 // Add $pgflush to the pgflush array
242 array_push($pages_flush, $pgflush);
246 case SITE_PREFIX."index_wr.php":
247 $header_out = array();
249 index_wr_main($room, $addr, $get, $post, $cookie);
250 $content = ob_get_contents();
253 $pgflush = new PageFlush($new_socket, $curtime, 20, $header_out, $content);
255 if ($pgflush->try_flush($curtime) == FALSE) {
256 // Add $pgflush to the pgflush array
257 array_push($pages_flush, $pgflush);
260 case SITE_PREFIX."index_rd_ifra.php":
262 $header_out = array();
263 if (!isset($cookie['sess'])
264 || (($user = $room->get_user($cookie['sess'], $idx)) == FALSE)) {
265 $content = index_rd_ifra_fini(TRUE);
267 $pgflush = new PageFlush($new_socket, $curtime, 20, $header_out, $content);
269 if ($pgflush->try_flush($curtime) == FALSE) {
270 // Add $pgflush to the pgflush array
271 array_push($pages_flush, $pgflush);
275 // close a previous opened index_read_ifra socket, if exists
276 if (($prev = $user->rd_socket_get()) != NULL) {
277 unset($s2u[intval($user->rd_socket_get())]);
278 unset($socks[intval($user->rd_socket_get())]);
279 fclose($user->rd_socket_get());
280 printf("CLOSE AND OPEN AGAIN ON IFRA2\n");
281 $user->rd_socket_set(NULL);
285 index_rd_ifra_init($room, $user, $header_out, $content, $get, $post, $cookie);
287 $response = headers_render($header_out, -1).chunked_content($content);
288 $response_l = mb_strlen($response, "ASCII");
290 $wret = @fwrite($new_socket, $response, $response_l);
291 if ($wret < $response_l) {
292 printf("TROUBLES WITH FWRITE: %d\n", $wret);
293 $user->rd_cache_set(mb_substr($content, $wret, $response_l - $wret, "ASCII"));
296 $user->rd_cache_set("");
300 $s2u[intval($new_socket)] = $idx;
301 $socks[intval($new_socket)] = $new_socket;
302 $user->rd_socket_set($new_socket);
309 printf("WARNING: ancillary_getstream failed\n");
313 if (($buf = fread($sock, 512)) === FALSE) {
314 printf("error read\n");
317 else if (strlen($buf) === 0) {
318 if ($sock === $list) {
319 printf("Arrivati %d bytes da list\n", strlen($buf));
321 else if ($sock === $in) {
322 printf("Arrivati %d bytes da stdin\n", strlen($buf));
325 // $user_a[$s2u[intval($sock)]]->disable();
326 if ($room->user[$s2u[intval($sock)]]->rd_socket_get() != NULL) {
327 $room->user[$s2u[intval($sock)]]->rd_socket_set(NULL);
329 unset($socks[intval($sock)]);
330 unset($s2u[intval($sock)]);
332 printf("CLOSE ON READ\n");
335 printf("post unset\n");
343 if ($sock === $list) {
344 printf("Arrivati %d bytes da list\n", strlen($buf));
346 else if ($sock === $in) {
347 printf("Arrivati %d bytes da stdin\n", strlen($buf));
350 $key = array_search("$sock", $socks);
351 printf("Arrivati %d bytes dalla socket n. %d\n", strlen($buf), $key);
359 foreach ($pages_flush as $k => $pgflush) {
360 if ($pgflush->try_flush($curtime) == TRUE) {
361 unset($pages_flush[$k]);
365 foreach ($socks as $k => $sock) {
366 if (isset($s2u[intval($sock)])) {
367 $user = $room->user[$s2u[intval($sock)]];
368 $response = $user->rd_cache_get();
369 if ($response == "") {
371 index_rd_ifra_main($room, $user, $content, $get, $post, $cookie);
373 if ($content == "" && $user->rd_kalive_is_expired($curtime)) {
374 $content = index_rd_ifra_keepalive($user);
376 if ($content != "") {
377 $response = chunked_content($content);
381 if ($response != "") {
382 echo "SPIA: [".substr($response, 0, 60)."...]\n";
383 $response_l = mb_strlen($response, "ASCII");
384 $wret = @fwrite($sock, $response);
385 if ($wret < $response_l) {
386 printf("TROUBLE WITH FWRITE: %d\n", $wret);
387 $user->rd_cache_set(mb_substr($response, $wret, $response_l - $wret, "ASCII"));
390 $user->rd_cache_set("");
393 $user->rd_kalive_reset($curtime);
396 // close socket after a while to prevent client memory consumption
397 if ($user->rd_endtime_is_expired($curtime)) {
398 // $user_a[$s2u[intval($sock)]]->disable();
399 if ($room->user[$s2u[intval($sock)]]->rd_socket_get() != NULL) {
400 $room->user[$s2u[intval($sock)]]->rd_socket_set(NULL);
402 unset($socks[intval($sock)]);
403 unset($s2u[intval($sock)]);
405 printf("CLOSE ON LOOP\n");