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.
28 require_once("./brisk-spush.phh");
29 require_once("../Obj/brisk.phh");
30 require_once("../Obj/auth.phh");
31 // require_once("../Obj/proxyscan.phh");
32 require_once("./sac-a-push.phh");
33 require_once("../index.php");
35 define('SITE_PREFIX', '/brisk/');
50 function enable($sock, $sess)
67 return ($this->cnt < 0 ? FALSE : TRUE);
75 function sock_set($sock)
101 function user_get_free($user_arr)
103 foreach ($user_arr as $i => $user) {
104 if (!$user->is_enable()) {
111 function user_get_sess($user_arr, $sess)
113 foreach ($user_arr as $i => $user) {
114 printf("SESS: [%s] cur: [%s]\n", $user->sess_get(), $sess);
115 if ($user->sess_get() == $sess) {
122 function headers_get()
125 $s .= "HTTP/1.1 200 OK\r\n";
126 $s .= sprintf("Date: %s\r\n", date(DATE_RFC822));
127 $s .= "Connection: close\r\n";
128 $s .= "Content-Type: text/html\r\n";
129 $s .= "Mop: was/here\r\n";
137 * Caching system using ob php system to cache old style pages
138 * to a var and than send it with more calm
143 function spu_store($s)
153 log_rd2("SHUTTA [".connection_status()."] !");
156 register_shutdown_function('shutta');
165 GLOBAL $G_headers, $G_content;
173 $FILE_SOCKET = "/tmp/brisk.sock";
174 $UNIX_SOCKET = "unix://$FILE_SOCKET";
179 $blocking_mode = 0; // 0 for non-blocking
181 if (($room = Room::create()) == FALSE) {
182 log_crit("load_data failed");
189 for ($i = 0 ; $i < 4096 ; $i++) {
190 $rndstr .= chr(mt_rand(65, 90));
193 if (file_exists($FILE_SOCKET)) {
194 unlink($FILE_SOCKET);
197 $old_umask = umask(0);
198 if (($list = stream_socket_server($UNIX_SOCKET, $err, $errs)) === FALSE) {
203 if (($in = fopen("php://stdin", "r")) === FALSE) {
207 stream_set_blocking($list, $blocking_mode); # Set the stream to non-blocking
211 /* Prepare the read array */
213 $read = array_merge(array("$in" => $in), $socks);
215 $read = array_merge(array("$list" => $list, "$in" => $in), $socks);
218 printf("PRE_SELECT\n");
223 $num_changed_sockets = stream_select($read, $write, $except, 5);
225 if ($num_changed_sockets === FALSE) {
226 printf("No data in 5 secs");
228 else if ($num_changed_sockets > 0) {
229 printf("num sock %d num_of_socket: %d\n", $num_changed_sockets, count($read));
233 /* At least at one of the sockets something interesting happened */
234 foreach ($read as $i => $sock) {
235 if ($sock === $list) {
236 printf("NUOVA CONNEX\n");
237 $new_unix = stream_socket_accept($list);
242 if (($new_socket = ancillary_getstream($new_unix, $stream_info)) !== FALSE) {
243 printf("RECEIVED HEADER:\n%s", $stream_info);
244 $path = spu_process_info($stream_info, $method, $header, $get, $post, $cookie);
245 printf("PATH: [%s]\n", $path);
246 printf("M: %s\nHEADER:\n", $method);
257 case SITE_PREFIX."index.php":
261 $content = ob_get_flush();
263 printf("OUT: [%s]\n", $G_content);
264 fwrite($new_socket, headers_get().$content);
273 /* TODO: here stuff to decide if it is old or new user */
274 if (($user_cur = user_get_sess($user_a, $get['sess'])) != FALSE) {
275 /* close the previous socket */
276 unset($s2u[intval($user_cur->sock_get())]);
277 unset($socks[intval($user_cur->sock_get())]);
278 fclose($user_cur->sock_get());
279 /* assign the new socket */
280 $user_cur->sock_set($new_socket);
281 $id = $user_cur->id_get();
282 $s2u[intval($new_socket)] = $id;
283 $socks[intval($new_socket)] = $new_socket;
284 fwrite($new_socket, $rndstr);
287 else if (($user_cur = user_get_free($user_a)) != FALSE) {
288 stream_set_blocking($new_socket, $blocking_mode); // Set the stream to non-blocking
289 $socks[intval($new_socket)] = $new_socket;
291 $id = $user_cur->id_get();
292 $user_a[$id]->enable($new_socket, $get['sess']);
293 printf("s2u: ci passo %d\n", intval($new_socket));
294 $s2u[intval($new_socket)] = $id;
296 fwrite($new_socket, $rndstr);
300 printf("Too many opened users\n");
306 printf("WARNING: ancillary_getstream failed\n");
310 if (($buf = fread($sock, 512)) === FALSE) {
311 printf("error read\n");
314 else if (strlen($buf) === 0) {
315 if ($sock === $list) {
316 printf("Arrivati %d bytes da list\n", strlen($buf));
318 else if ($sock === $in) {
319 printf("Arrivati %d bytes da stdin\n", strlen($buf));
322 unset($socks[intval($sock)]);
323 $user_a[$s2u[intval($sock)]]->disable();
324 unset($s2u[intval($sock)]);
328 printf("post unset\n");
336 if ($sock === $list) {
337 printf("Arrivati %d bytes da list\n", strlen($buf));
339 else if ($sock === $in) {
340 printf("Arrivati %d bytes da stdin\n", strlen($buf));
343 $key = array_search("$sock", $socks);
344 printf("Arrivati %d bytes dalla socket n. %d\n", strlen($buf), $key);
356 if (0 == 1) { // WRITE PART EXAMPLE
358 foreach ($socks as $k => $sock) {
359 fwrite($sock, sprintf("DI QUI CI PASSO [%d]\n", $user_a[$s2u[intval($sock)]]->cnt_inc()));