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 * - 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
34 * DONE/FROZEN - problema con getpeer (HOSTADDR)
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
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");
59 define('SITE_PREFIX', '/brisk/');
61 function headers_render($header)
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);
75 $s .= "Mop: was/here\r\n";
82 * Caching system using ob php system to cache old style pages
83 * to a var and than send it with more calm
89 log_rd2("SHUTTA [".connection_status()."] !");
92 register_shutdown_function('shutta');
109 $FILE_SOCKET = "/tmp/brisk.sock";
110 $UNIX_SOCKET = "unix://$FILE_SOCKET";
115 $blocking_mode = 0; // 0 for non-blocking
117 if (($room = Room::create()) == FALSE) {
118 log_crit("load_data failed");
125 for ($i = 0 ; $i < 4096 ; $i++) {
126 $rndstr .= chr(mt_rand(65, 90));
129 if (file_exists($FILE_SOCKET)) {
130 unlink($FILE_SOCKET);
133 $old_umask = umask(0);
134 if (($list = stream_socket_server($UNIX_SOCKET, $err, $errs)) === FALSE) {
139 if (($in = fopen("php://stdin", "r")) === FALSE) {
143 stream_set_blocking($list, $blocking_mode); # Set the stream to non-blocking
147 printf("IN LOOP: Current opened: %d\n", count($socks));
149 /* Prepare the read array */
151 $read = array_merge(array("$in" => $in), $socks);
153 $read = array_merge(array("$list" => $list, "$in" => $in), $socks);
156 printf("PRE_SELECT\n");
161 $num_changed_sockets = stream_select($read, $write, $except, 0, 250000);
163 if ($num_changed_sockets === FALSE) {
164 printf("No data in 5 secs");
166 else if ($num_changed_sockets > 0) {
167 printf("num sock %d num_of_socket: %d\n", $num_changed_sockets, count($read));
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);
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);
195 $addr = stream_socket_get_name($new_socket, TRUE);
199 case SITE_PREFIX."index.php":
200 $header_out = array();
202 index_main($room, $header_out, $addr, $get, $post, $cookie);
203 $content = ob_get_contents();
205 // printf("OUT: [%s]\n", $G_content);
206 fwrite($new_socket, headers_render($header_out).$content);
209 case SITE_PREFIX."index_wr.php":
210 $header_out = array();
212 // $ret = socket_getpeername($new_socket, $addr);
213 printf("RET: %s\n", $addr);
216 index_wr_main($room, $addr, $get, $post, $cookie);
217 $content = ob_get_contents();
220 // printf("OUT: [%s]\n", $G_content);
221 fwrite($new_socket, headers_render($header_out).$content);
224 case SITE_PREFIX."index_rd_ifra.php":
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);
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);
245 index_rd_ifra_init($room, $user, $header_out, $body, $get, $post, $cookie);
246 fwrite($new_socket, headers_render($header_out).$body);
249 $s2u[intval($new_socket)] = $idx;
250 $socks[intval($new_socket)] = $new_socket;
251 $user->rd_socket_set($new_socket);
258 printf("WARNING: ancillary_getstream failed\n");
262 if (($buf = fread($sock, 512)) === FALSE) {
263 printf("error read\n");
266 else if (strlen($buf) === 0) {
267 if ($sock === $list) {
268 printf("Arrivati %d bytes da list\n", strlen($buf));
270 else if ($sock === $in) {
271 printf("Arrivati %d bytes da stdin\n", strlen($buf));
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);
278 unset($socks[intval($sock)]);
279 unset($s2u[intval($sock)]);
281 printf("CLOSE ON READ\n");
284 printf("post unset\n");
292 if ($sock === $list) {
293 printf("Arrivati %d bytes da list\n", strlen($buf));
295 else if ($sock === $in) {
296 printf("Arrivati %d bytes da stdin\n", strlen($buf));
299 $key = array_search("$sock", $socks);
300 printf("Arrivati %d bytes dalla socket n. %d\n", strlen($buf), $key);
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();
312 index_rd_ifra_main($room, $user, $body);
314 if ($body == "" && $user->rd_kalive_is_expired($curtime)) {
315 $body = index_rd_ifra_keepalive($user);
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"));
327 $user->rd_cache_set("");
330 $user->rd_kalive_reset($curtime);
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);
339 unset($socks[intval($sock)]);
340 unset($s2u[intval($sock)]);
342 printf("CLOSE ON LOOP\n");