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.
26 * - BUG: logout failed
27 * - BUG: fast loop on stream index_rd_ifra page
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
39 * DONE/FROZEN - problema con getpeer (HOSTADDR)
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
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");
66 define('SITE_PREFIX', '/brisk/');
68 function headers_render($header)
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);
82 $s .= "Mop: was/here\r\n";
89 * Caching system using ob php system to cache old style pages
90 * to a var and than send it with more calm
96 log_rd2("SHUTTA [".connection_status()."] !");
99 register_shutdown_function('shutta');
116 $FILE_SOCKET = "/tmp/brisk.sock";
117 $UNIX_SOCKET = "unix://$FILE_SOCKET";
122 $blocking_mode = 0; // 0 for non-blocking
124 if (($room = Room::create()) == FALSE) {
125 log_crit("load_data failed");
130 $pages_flush = array();
133 for ($i = 0 ; $i < 4096 ; $i++) {
134 $rndstr .= chr(mt_rand(65, 90));
137 if (file_exists($FILE_SOCKET)) {
138 unlink($FILE_SOCKET);
141 $old_umask = umask(0);
142 if (($list = stream_socket_server($UNIX_SOCKET, $err, $errs)) === FALSE) {
147 if (($in = fopen("php://stdin", "r")) === FALSE) {
151 stream_set_blocking($list, $blocking_mode); # Set the stream to non-blocking
155 printf("IN LOOP: Current opened: %d pages_flush: %d\n", count($socks), count($pages_flush));
157 /* Prepare the read array */
159 $read = array_merge(array("$in" => $in), $socks);
161 $read = array_merge(array("$list" => $list, "$in" => $in), $socks);
164 printf("PRE_SELECT\n");
169 $num_changed_sockets = stream_select($read, $write, $except, 0, 250000);
171 if ($num_changed_sockets === FALSE) {
172 printf("No data in 5 secs");
174 else if ($num_changed_sockets > 0) {
175 printf("num sock %d num_of_socket: %d\n", $num_changed_sockets, count($read));
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)) {
186 if ($sock === $list) {
187 printf("NUOVA CONNEX\n");
188 $new_unix = stream_socket_accept($list);
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);
208 $addr = stream_socket_get_name($new_socket, TRUE);
212 case SITE_PREFIX."index.php":
213 $header_out = array();
215 index_main($room, $header_out, $addr, $get, $post, $cookie);
216 $content = ob_get_contents();
219 $pgflush = new PageFlush($new_socket, $curtime, 20, $header_out, $content);
221 if ($pgflush->try_flush($curtime) == FALSE) {
222 // Add $pgflush to the pgflush array
223 array_push($pages_flush, $pgflush);
227 case SITE_PREFIX."index_wr.php":
228 $header_out = array();
230 index_wr_main($room, $addr, $get, $post, $cookie);
231 $content = ob_get_contents();
234 $pgflush = new PageFlush($new_socket, $curtime, 20, $header_out, $content);
236 if ($pgflush->try_flush($curtime) == FALSE) {
237 // Add $pgflush to the pgflush array
238 array_push($pages_flush, $pgflush);
241 case SITE_PREFIX."index_rd_ifra.php":
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);
248 $pgflush = new PageFlush($new_socket, $curtime, 20, $header_out, $content);
250 if ($pgflush->try_flush($curtime) == FALSE) {
251 // Add $pgflush to the pgflush array
252 array_push($pages_flush, $pgflush);
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);
266 index_rd_ifra_init($room, $user, $header_out, $body, $get, $post, $cookie);
267 fwrite($new_socket, headers_render($header_out).$body);
270 $s2u[intval($new_socket)] = $idx;
271 $socks[intval($new_socket)] = $new_socket;
272 $user->rd_socket_set($new_socket);
279 printf("WARNING: ancillary_getstream failed\n");
283 if (($buf = fread($sock, 512)) === FALSE) {
284 printf("error read\n");
287 else if (strlen($buf) === 0) {
288 if ($sock === $list) {
289 printf("Arrivati %d bytes da list\n", strlen($buf));
291 else if ($sock === $in) {
292 printf("Arrivati %d bytes da stdin\n", strlen($buf));
295 // $user_a[$s2u[intval($sock)]]->disable();
296 if ($room->user[$s2u[intval($sock)]]->rd_socket_get() != NULL) {
297 $room->user[$s2u[intval($sock)]]->rd_socket_set(NULL);
299 unset($socks[intval($sock)]);
300 unset($s2u[intval($sock)]);
302 printf("CLOSE ON READ\n");
305 printf("post unset\n");
313 if ($sock === $list) {
314 printf("Arrivati %d bytes da list\n", strlen($buf));
316 else if ($sock === $in) {
317 printf("Arrivati %d bytes da stdin\n", strlen($buf));
320 $key = array_search("$sock", $socks);
321 printf("Arrivati %d bytes dalla socket n. %d\n", strlen($buf), $key);
329 foreach ($pages_flush as $k => $pgflush) {
330 if ($pgflush->try_flush($curtime) == TRUE) {
331 unset($pages_flush[$k]);
335 foreach ($socks as $k => $sock) {
336 if (isset($s2u[intval($sock)])) {
337 $user = $room->user[$s2u[intval($sock)]];
338 $body = $user->rd_cache_get();
340 index_rd_ifra_main($room, $user, $body);
342 if ($body == "" && $user->rd_kalive_is_expired($curtime)) {
343 $body = index_rd_ifra_keepalive($user);
347 echo "SPIA: [".substr($body, 0, 60)."...]\n";
348 $body_l = mb_strlen($body, "ASCII");
349 $ret = @fwrite($sock, $body);
350 if ($ret < $body_l) {
351 printf("TROUBLE WITH FWRITE: %d\n", $ret);
352 $user->rd_cache_set(mb_substr($body, $ret, $body_l - $ret, "ASCII"));
355 $user->rd_cache_set("");
358 $user->rd_kalive_reset($curtime);
361 // close socket after a while to prevent client memory consumption
362 if ($user->rd_endtime_is_expired($curtime)) {
363 // $user_a[$s2u[intval($sock)]]->disable();
364 if ($room->user[$s2u[intval($sock)]]->rd_socket_get() != NULL) {
365 $room->user[$s2u[intval($sock)]]->rd_socket_set(NULL);
367 unset($socks[intval($sock)]);
368 unset($s2u[intval($sock)]);
370 printf("CLOSE ON LOOP\n");