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 * - partial write for normal page management
27 * - log legal address fix
28 * - from room to table
29 * - from table to room
30 * - fwrite other issues
31 * - manage and test cross forwarder between table and room
32 * - setcookie (for tables only)
33 * - keepalive management
36 * DONE/FROZEN - problema con getpeer (HOSTADDR)
38 * DONE - index_rd_ifra: last_clean issue
39 * DONE - fwrite failed error management (select, buffer where store unsent data, and fwrite check and retry)
40 * ABRT - index_wr.php::reload - reload is js-only function
41 * DONE - bug: after restart index_rd.php receive from prev clients a lot of req
42 * DONE - index_wr.php::chat
43 * DONE - index_wr.php::exit
44 * DONE - index_rd.php porting
45 * DONE - generic var management from internet
46 * DONE - index.php auth part
51 require_once("./sac-a-push.phh");
52 require_once("./brisk-spush.phh");
53 require_once($G_base."Obj/brisk.phh");
54 require_once($G_base."Obj/auth.phh");
55 // require_once("../Obj/proxyscan.phh");
56 require_once($G_base."index.php");
57 require_once($G_base."index_wr.php");
58 require_once($G_base."index_rd_ifra.php");
59 require_once($G_base."briskin5/Obj/briskin5.phh");
61 define('SITE_PREFIX', '/brisk/');
63 function headers_render($header)
67 $s .= "HTTP/1.1 200 OK\r\n";
68 if (!isset($header['Date']))
69 $s .= sprintf("Date: %s\r\n", date(DATE_RFC822));
70 if (!isset($header['Connection']))
71 $s .= "Connection: close\r\n";
72 if (!isset($header['Content-Type']))
73 $s .= "Content-Type: text/html\r\n";
74 foreach($header as $key => $value) {
75 $s .= sprintf("%s: %s\r\n", $key, $value);
77 $s .= "Mop: was/here\r\n";
84 * Caching system using ob php system to cache old style pages
85 * to a var and than send it with more calm
91 log_rd2("SHUTTA [".connection_status()."] !");
94 register_shutdown_function('shutta');
111 $FILE_SOCKET = "/tmp/brisk.sock";
112 $UNIX_SOCKET = "unix://$FILE_SOCKET";
117 $blocking_mode = 0; // 0 for non-blocking
119 if (($room = Room::create()) == FALSE) {
120 log_crit("load_data failed");
125 $pages_flush = array();
128 for ($i = 0 ; $i < 4096 ; $i++) {
129 $rndstr .= chr(mt_rand(65, 90));
132 if (file_exists($FILE_SOCKET)) {
133 unlink($FILE_SOCKET);
136 $old_umask = umask(0);
137 if (($list = stream_socket_server($UNIX_SOCKET, $err, $errs)) === FALSE) {
142 if (($in = fopen("php://stdin", "r")) === FALSE) {
146 stream_set_blocking($list, $blocking_mode); # Set the stream to non-blocking
150 printf("IN LOOP: Current opened: %d pages_flush: %d\n", count($socks), count($pages_flush));
152 /* Prepare the read array */
154 $read = array_merge(array("$in" => $in), $socks);
156 $read = array_merge(array("$list" => $list, "$in" => $in), $socks);
159 printf("PRE_SELECT\n");
164 $num_changed_sockets = stream_select($read, $write, $except, 0, 250000);
166 if ($num_changed_sockets === FALSE) {
167 printf("No data in 5 secs");
169 else if ($num_changed_sockets > 0) {
170 printf("num sock %d num_of_socket: %d\n", $num_changed_sockets, count($read));
174 /* At least at one of the sockets something interesting happened */
175 foreach ($read as $i => $sock) {
176 /* is_resource check is required because there is the possibility that
177 during new request an old connection is closed */
178 if (!is_resource($sock)) {
181 if ($sock === $list) {
182 printf("NUOVA CONNEX\n");
183 $new_unix = stream_socket_accept($list);
189 if (($new_socket = ancillary_getstream($new_unix, $stream_info)) !== FALSE) {
190 stream_set_blocking($new_socket, $blocking_mode); // Set the stream to non-blocking
191 printf("RECEIVED HEADER:\n%s", $stream_info);
192 $path = spu_process_info($stream_info, $method, $header, $get, $post, $cookie);
193 printf("PATH: [%s]\n", $path);
194 printf("M: %s\nHEADER:\n", $method);
203 $addr = stream_socket_get_name($new_socket, TRUE);
207 case SITE_PREFIX."index.php":
208 $header_out = array();
210 index_main($room, $header_out, $addr, $get, $post, $cookie);
211 $content = ob_get_contents();
214 $pgflush = new PageFlush($new_socket, $curtime, 20, $header_out, $content);
216 if ($pgflush->try_flush($curtime) == FALSE) {
217 // Add $pgflush to the pgflush array
218 array_push($pages_flush, $pgflush);
222 case SITE_PREFIX."index_wr.php":
223 $header_out = array();
225 // $ret = socket_getpeername($new_socket, $addr);
226 printf("RET: %s\n", $addr);
229 index_wr_main($room, $addr, $get, $post, $cookie);
230 $content = ob_get_contents();
233 // printf("OUT: [%s]\n", $G_content);
234 fwrite($new_socket, headers_render($header_out).$content);
237 case SITE_PREFIX."index_rd_ifra.php":
239 $header_out = array();
240 if (!isset($cookie['sess'])
241 || (($user = $room->get_user($cookie['sess'], $idx)) == FALSE)) {
242 $body = index_rd_ifra_fini(TRUE);
243 fwrite($new_socket, headers_render($header_out).$body);
248 // close a previous opened index_read_ifra socket, if exists
249 if (($prev = $user->rd_socket_get()) != NULL) {
250 unset($s2u[intval($user->rd_socket_get())]);
251 unset($socks[intval($user->rd_socket_get())]);
252 fclose($user->rd_socket_get());
253 printf("CLOSE AND OPEN AGAIN ON IFRA2\n");
254 $user->rd_socket_set(NULL);
258 index_rd_ifra_init($room, $user, $header_out, $body, $get, $post, $cookie);
259 fwrite($new_socket, headers_render($header_out).$body);
262 $s2u[intval($new_socket)] = $idx;
263 $socks[intval($new_socket)] = $new_socket;
264 $user->rd_socket_set($new_socket);
271 printf("WARNING: ancillary_getstream failed\n");
275 if (($buf = fread($sock, 512)) === FALSE) {
276 printf("error read\n");
279 else if (strlen($buf) === 0) {
280 if ($sock === $list) {
281 printf("Arrivati %d bytes da list\n", strlen($buf));
283 else if ($sock === $in) {
284 printf("Arrivati %d bytes da stdin\n", strlen($buf));
287 // $user_a[$s2u[intval($sock)]]->disable();
288 if ($room->user[$s2u[intval($sock)]]->rd_socket_get() != NULL) {
289 $room->user[$s2u[intval($sock)]]->rd_socket_set(NULL);
291 unset($socks[intval($sock)]);
292 unset($s2u[intval($sock)]);
294 printf("CLOSE ON READ\n");
297 printf("post unset\n");
305 if ($sock === $list) {
306 printf("Arrivati %d bytes da list\n", strlen($buf));
308 else if ($sock === $in) {
309 printf("Arrivati %d bytes da stdin\n", strlen($buf));
312 $key = array_search("$sock", $socks);
313 printf("Arrivati %d bytes dalla socket n. %d\n", strlen($buf), $key);
321 foreach ($pages_flush as $k => $pgflush) {
322 if ($pgflush->try_flush($curtime) == TRUE) {
323 unset($pages_flush[$k]);
327 foreach ($socks as $k => $sock) {
328 if (isset($s2u[intval($sock)])) {
329 $user = $room->user[$s2u[intval($sock)]];
330 $body = $user->rd_cache_get();
332 index_rd_ifra_main($room, $user, $body);
334 if ($body == "" && $user->rd_kalive_is_expired($curtime)) {
335 $body = index_rd_ifra_keepalive($user);
339 echo "SPIA: [".substr($body, 0, 60)."...]\n";
340 $body_l = mb_strlen($body, "ASCII");
341 $ret = @fwrite($sock, $body);
342 if ($ret < $body_l) {
343 printf("TROUBLE WITH FWRITE: %d\n", $ret);
344 $user->rd_cache_set(mb_substr($body, $ret, $body_l - $ret, "ASCII"));
347 $user->rd_cache_set("");
350 $user->rd_kalive_reset($curtime);
353 // close socket after a while to prevent client memory consumption
354 if ($user->rd_endtime_is_expired($curtime)) {
355 // $user_a[$s2u[intval($sock)]]->disable();
356 if ($room->user[$s2u[intval($sock)]]->rd_socket_get() != NULL) {
357 $room->user[$s2u[intval($sock)]]->rd_socket_set(NULL);
359 unset($socks[intval($sock)]);
360 unset($s2u[intval($sock)]);
362 printf("CLOSE ON LOOP\n");