first page from index.php with sac-a-push technology (no authentication part)
[brisk.git] / web / spush / brisk-spush.php
1 #!/usr/bin/php
2 <?php
3 /*
4  *  brisk - spush/brisk-spush.php
5  *
6  *  Copyright (C) 2012 Matteo Nastasi
7  *                          mailto: nastasi@alternativeoutput.it 
8  *                                  matteo.nastasi@milug.org
9  *                          web: http://www.alternativeoutput.it
10  *
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.
15  *
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.
23  *
24  */
25
26 $G_base = "../";
27
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");
34
35 define('SITE_PREFIX', '/brisk/');
36
37 class SPUser {
38     var $id;
39     var $sess;
40     var $cnt;
41     var $sock;
42     
43     function SPUser($id)
44     {
45         $this->id = $id;
46         $this->cnt = -1;
47         $this->sock = NULL;
48     }
49
50     function enable($sock, $sess)
51     {
52         $this->sess = $sess;
53         $this->cnt = 0;
54         $this->sock = $sock;
55
56         return ($this->id);
57     }
58
59     function disable()
60     {
61         $this->cnt = -1;
62         $this->sock = NULL;
63     }
64
65     function is_enable()
66     {
67         return ($this->cnt < 0 ? FALSE : TRUE);
68     }
69
70     function sock_get()
71     {
72         return $this->sock;
73     }
74
75     function sock_set($sock)
76     {
77         $this->sock = $sock;
78     }
79
80     function id_get()
81     {
82         return $this->id;
83     }
84     
85     function sess_get()
86     {
87         return $this->sess;
88     }
89
90     function cnt_get()
91     {
92         return $this->cnt;
93     }
94
95     function cnt_inc()
96     {
97         return $this->cnt++;
98     }
99 }
100
101 function user_get_free($user_arr)
102 {
103     foreach ($user_arr as $i => $user) {
104         if (!$user->is_enable()) {
105             return ($user);
106         }
107     }
108     return FALSE;
109 }
110
111 function user_get_sess($user_arr, $sess)
112 {
113     foreach ($user_arr as $i => $user) {
114         printf("SESS: [%s]  cur: [%s]\n", $user->sess_get(), $sess);
115         if ($user->sess_get() == $sess) {
116             return ($user);
117         }
118     }
119     return FALSE;
120 }
121
122 function headers_get()
123 {
124     $s = "";
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";
130     $s .= "\r\n";
131
132     return ($s);
133 }
134
135
136 /*
137  *  Caching system using ob php system to cache old style pages
138  *  to a var and than send it with more calm
139  */
140 $G_headers = "";
141 $G_content = "";
142
143 function spu_store($s)
144 {
145     GLOBAL $G_content;
146
147     $G_content .= $s;
148
149     return '';
150 }
151 function shutta()
152 {
153   log_rd2("SHUTTA [".connection_status()."] !");
154 }
155
156 register_shutdown_function('shutta');
157
158 /*
159  *  MAIN
160  */
161 $shutdown = FALSE;
162
163 function main()
164 {
165     GLOBAL $G_headers, $G_content;
166     GLOBAL $shutdown;
167     $main_loop = TRUE;
168
169     /*
170      *  INIT
171      */
172
173     $FILE_SOCKET = "/tmp/brisk.sock";
174     $UNIX_SOCKET = "unix://$FILE_SOCKET";
175     $debug = 0;
176     $fixed_fd = 2;
177     $socks = array();
178
179     $blocking_mode = 0; // 0 for non-blocking
180
181     if (($room = Room::create()) == FALSE) {
182         log_crit("load_data failed");
183         return FALSE;
184     }
185
186     $s2u  = array();
187
188     $rndstr = "";
189     for ($i = 0 ; $i < 4096 ; $i++) {
190         $rndstr .= chr(mt_rand(65, 90));
191     }
192
193     if (file_exists($FILE_SOCKET)) {
194         unlink($FILE_SOCKET);
195     }
196     
197     $old_umask = umask(0);
198     if (($list = stream_socket_server($UNIX_SOCKET, $err, $errs)) === FALSE) {
199         exit(11);
200     }
201     umask($old_umask);
202
203     if (($in = fopen("php://stdin", "r")) === FALSE) {
204         exit(11);
205     }
206
207     stream_set_blocking($list, $blocking_mode); # Set the stream to non-blocking
208
209     while ($main_loop) {
210         echo "IN LOOP\n";
211         /* Prepare the read array */
212         if ($shutdown) 
213             $read   = array_merge(array("$in" => $in), $socks);
214         else
215             $read   = array_merge(array("$list" => $list, "$in" => $in), $socks);
216
217         if ($debug > 1) {
218             printf("PRE_SELECT\n");
219             print_r($read);
220         }
221         $write  = NULL;
222         $except = NULL;
223         $num_changed_sockets = stream_select($read, $write, $except, 5);
224         
225         if ($num_changed_sockets === FALSE) {
226             printf("No data in 5 secs");
227         } 
228         else if ($num_changed_sockets > 0) {
229             printf("num sock %d num_of_socket: %d\n", $num_changed_sockets, count($read));
230             if ($debug > 1) {
231                 print_r($read);
232             }
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);
238                     $stream_info = "";
239                     $method      = "";
240                     $get         = "";
241                     $post        = "";
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);
247                         print_r($header);
248                         printf("GET:\n");
249                         print_r($get);
250                         printf("POST:\n");
251                         print_r($post);
252                         printf("COOKIE:\n");
253                         print_r($cookie);
254
255                         switch ($path) {
256                         case SITE_PREFIX:
257                         case SITE_PREFIX."index.php":
258                             $G_headers = "";
259                             ob_start();
260                             index_main($room);
261                             $content = ob_get_flush();
262
263                             printf("OUT: [%s]\n", $G_content);
264                             fwrite($new_socket, headers_get().$content);
265                             fclose($new_socket);
266                             break;
267                         }
268                             
269
270
271
272                         if (0 == 1) {
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);
285                                 fflush($new_socket);
286                             }
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;
290
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;
295
296                                 fwrite($new_socket, $rndstr);
297                                 fflush($new_socket);
298                             }
299                             else {
300                                 printf("Too many opened users\n");
301                                 fclose($new_socket);
302                             }
303                         }
304                     }
305                     else {
306                         printf("WARNING: ancillary_getstream failed\n");
307                     }
308                 }
309                 else {
310                     if (($buf = fread($sock, 512)) === FALSE) {
311                         printf("error read\n");
312                         exit(123);
313                     }
314                     else if (strlen($buf) === 0) {
315                         if ($sock === $list) {
316                             printf("Arrivati %d bytes da list\n", strlen($buf));
317                         }
318                         else if ($sock === $in) {
319                             printf("Arrivati %d bytes da stdin\n", strlen($buf));
320                         }
321                         else {
322                             unset($socks[intval($sock)]);
323                             $user_a[$s2u[intval($sock)]]->disable();
324                             unset($s2u[intval($sock)]);
325                             fclose($sock);
326                         }
327                         if ($debug > 1) {
328                             printf("post unset\n");
329                             print_r($socks);
330                         }
331                     }
332                     else {
333                         if ($debug > 1) {
334                             print_r($read);
335                         }
336                         if ($sock === $list) {
337                             printf("Arrivati %d bytes da list\n", strlen($buf));
338                         }
339                         else if ($sock === $in) {
340                             printf("Arrivati %d bytes da stdin\n", strlen($buf));
341                         }
342                         else {
343                             $key = array_search("$sock", $socks);
344                             printf("Arrivati %d bytes dalla socket n. %d\n", strlen($buf), $key);
345                         }
346                     }
347                 }
348             }
349         }
350
351
352
353
354
355
356         if (0 == 1) { // WRITE PART EXAMPLE 
357             // sleep(3);
358             foreach ($socks as $k => $sock) {
359                 fwrite($sock, sprintf("DI QUI CI PASSO [%d]\n", $user_a[$s2u[intval($sock)]]->cnt_inc()));
360                 fflush($sock);
361             }
362         }
363     }
364     
365     exit(0);
366 }
367
368 main();
369 ?>