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 * index_wr.php::reload
29 * setcookie (for tables only)
32 * BUG - after restart index_rd.php receive from prev clients a lot of req
33 * DONE/FROZEN - problema con getpeer (HOSTADDR)
35 * DONE - index_rd.php porting
36 * DONE - generic var management from internet
37 * DONE - index.php auth part
42 require_once("./sac-a-push.phh");
43 require_once("./brisk-spush.phh");
44 require_once($G_base."Obj/brisk.phh");
45 require_once($G_base."Obj/auth.phh");
46 // require_once("../Obj/proxyscan.phh");
47 require_once($G_base."index.php");
48 require_once($G_base."index_wr.php");
49 require_once($G_base."index_rd_ifra.php");
50 require_once($G_base."briskin5/Obj/briskin5.phh");
52 define('SITE_PREFIX', '/brisk/');
67 function enable($sock, $sess)
84 return ($this->cnt < 0 ? FALSE : TRUE);
92 function sock_set($sock)
118 function user_get_free($user_arr)
120 foreach ($user_arr as $i => $user) {
121 if (!$user->is_enable()) {
128 function user_get_sess($user_arr, $sess)
130 foreach ($user_arr as $i => $user) {
131 printf("SESS: [%s] cur: [%s]\n", $user->sess_get(), $sess);
132 if ($user->sess_get() == $sess) {
139 function headers_render($header)
143 $s .= "HTTP/1.1 200 OK\r\n";
144 if (!isset($header['Date']))
145 $s .= sprintf("Date: %s\r\n", date(DATE_RFC822));
146 if (!isset($header['Connection']))
147 $s .= "Connection: close\r\n";
148 if (!isset($header['Content-Type']))
149 $s .= "Content-Type: text/html\r\n";
150 foreach($header as $key => $value) {
151 $s .= sprintf("%s: %s\r\n", $key, $value);
153 $s .= "Mop: was/here\r\n";
161 * Caching system using ob php system to cache old style pages
162 * to a var and than send it with more calm
168 log_rd2("SHUTTA [".connection_status()."] !");
171 register_shutdown_function('shutta');
188 $FILE_SOCKET = "/tmp/brisk.sock";
189 $UNIX_SOCKET = "unix://$FILE_SOCKET";
194 $blocking_mode = 0; // 0 for non-blocking
196 if (($room = Room::create()) == FALSE) {
197 log_crit("load_data failed");
204 for ($i = 0 ; $i < 4096 ; $i++) {
205 $rndstr .= chr(mt_rand(65, 90));
208 if (file_exists($FILE_SOCKET)) {
209 unlink($FILE_SOCKET);
212 $old_umask = umask(0);
213 if (($list = stream_socket_server($UNIX_SOCKET, $err, $errs)) === FALSE) {
218 if (($in = fopen("php://stdin", "r")) === FALSE) {
222 stream_set_blocking($list, $blocking_mode); # Set the stream to non-blocking
226 /* Prepare the read array */
228 $read = array_merge(array("$in" => $in), $socks);
230 $read = array_merge(array("$list" => $list, "$in" => $in), $socks);
233 printf("PRE_SELECT\n");
238 $num_changed_sockets = stream_select($read, $write, $except, 1); // 0, 250000);
240 if ($num_changed_sockets === FALSE) {
241 printf("No data in 5 secs");
243 else if ($num_changed_sockets > 0) {
244 printf("num sock %d num_of_socket: %d\n", $num_changed_sockets, count($read));
248 /* At least at one of the sockets something interesting happened */
249 foreach ($read as $i => $sock) {
250 if ($sock === $list) {
251 printf("NUOVA CONNEX\n");
252 $new_unix = stream_socket_accept($list);
258 if (($new_socket = ancillary_getstream($new_unix, $stream_info)) !== FALSE) {
259 printf("RECEIVED HEADER:\n%s", $stream_info);
260 $path = spu_process_info($stream_info, $method, $header, $get, $post, $cookie);
261 printf("PATH: [%s]\n", $path);
262 printf("M: %s\nHEADER:\n", $method);
271 $addr = stream_socket_get_name($new_socket, TRUE);
275 case SITE_PREFIX."index.php":
276 $header_out = array();
278 index_main($room, $header_out, $addr, $get, $post, $cookie);
279 $content = ob_get_contents();
281 // printf("OUT: [%s]\n", $G_content);
282 fwrite($new_socket, headers_render($header_out).$content);
285 case SITE_PREFIX."index_wr.php":
286 $header_out = array();
288 // $ret = socket_getpeername($new_socket, $addr);
289 printf("RET: %s\n", $addr);
292 index_wr_main($room, $addr, $get, $post, $cookie);
293 $content = ob_get_contents();
296 // printf("OUT: [%s]\n", $G_content);
297 fwrite($new_socket, headers_render($header_out).$content);
300 case SITE_PREFIX."index_rd_ifra.php":
302 if (!isset($cookie['sess'])) {
306 if (($user = $room->get_user($cookie['sess'], $idx)) == FALSE) {
310 if (($prev = $user->rd_socket_get()) != NULL) {
311 unset($s2u[intval($user->rd_socket_get())]);
312 unset($socks[intval($user->rd_socket_get())]);
313 fclose($user->rd_socket_get());
314 $user->rd_socket_set(NULL);
317 $header_out = array();
319 index_rd_ifra_init($room, $user, $header_out, $body, $get, $post, $cookie);
320 stream_set_blocking($new_socket, $blocking_mode); // Set the stream to non-blocking
321 fwrite($new_socket, headers_render($header_out).$body);
324 $s2u[intval($new_socket)] = $idx;
325 $socks[intval($new_socket)] = $new_socket;
326 $user->rd_socket_set($new_socket);
336 /* TODO: here stuff to decide if it is old or new user */
337 if (($user_cur = user_get_sess($user_a, $get['sess'])) != FALSE) {
338 /* close the previous socket */
339 unset($s2u[intval($user_cur->sock_get())]);
340 unset($socks[intval($user_cur->sock_get())]);
341 fclose($user_cur->sock_get());
342 /* assign the new socket */
343 $user_cur->sock_set($new_socket);
344 $id = $user_cur->id_get();
345 $s2u[intval($new_socket)] = $id;
346 $socks[intval($new_socket)] = $new_socket;
347 fwrite($new_socket, $rndstr);
350 else if (($user_cur = user_get_free($user_a)) != FALSE) {
351 stream_set_blocking($new_socket, $blocking_mode); // Set the stream to non-blocking
352 $socks[intval($new_socket)] = $new_socket;
354 $id = $user_cur->id_get();
355 $user_a[$id]->enable($new_socket, $get['sess']);
356 printf("s2u: ci passo %d\n", intval($new_socket));
357 $s2u[intval($new_socket)] = $id;
359 fwrite($new_socket, $rndstr);
363 printf("Too many opened users\n");
369 printf("WARNING: ancillary_getstream failed\n");
373 if (($buf = fread($sock, 512)) === FALSE) {
374 printf("error read\n");
377 else if (strlen($buf) === 0) {
378 if ($sock === $list) {
379 printf("Arrivati %d bytes da list\n", strlen($buf));
381 else if ($sock === $in) {
382 printf("Arrivati %d bytes da stdin\n", strlen($buf));
385 // $user_a[$s2u[intval($sock)]]->disable();
386 if ($room->user[$s2u[intval($sock)]]->rd_socket_get() != NULL) {
387 $room->user[$s2u[intval($sock)]]->rd_socket_set(NULL);
389 unset($socks[intval($sock)]);
390 unset($s2u[intval($sock)]);
394 printf("post unset\n");
402 if ($sock === $list) {
403 printf("Arrivati %d bytes da list\n", strlen($buf));
405 else if ($sock === $in) {
406 printf("Arrivati %d bytes da stdin\n", strlen($buf));
409 $key = array_search("$sock", $socks);
410 printf("Arrivati %d bytes dalla socket n. %d\n", strlen($buf), $key);
420 foreach ($socks as $k => $sock) {
421 if (isset($s2u[intval($sock)])) {
426 $user = $room->user[$s2u[intval($sock)]];
427 index_rd_ifra_main($room, $user, $body);
428 if ($body == "" && $user->rd_tout_is_expired($curtime)) {
429 $body = index_rd_ifra_keepalive($user);
433 echo "SPIA: [".substr($body, 0, 60)."...]\n";
434 fwrite($sock, $body);
436 $user->rd_tout_reset($curtime);
439 // close socket after a while to prevent client memory consumption
440 if ($user->rd_endtime_is_expired($curtime)) {
441 // $user_a[$s2u[intval($sock)]]->disable();
442 if ($room->user[$s2u[intval($sock)]]->rd_socket_get() != NULL) {
443 $room->user[$s2u[intval($sock)]]->rd_socket_set(NULL);
445 unset($socks[intval($sock)]);
446 unset($s2u[intval($sock)]);