proxyscan to check (and deny) proxy connections
[brisk.git] / web / index_rd.php
1 <?php
2 /*
3  *  brisk - index_rd.php
4  *
5  *  Copyright (C) 2006-2007 matteo.nastasi@milug.org
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details. You should have received a
16  * copy of the GNU General Public License along with this program; if
17  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
18  * Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * $Id$
21  *
22  */
23
24 require_once("Obj/brisk.phh");
25 require_once("Obj/proxyscan.phh");
26 require_once("briskin5/Obj/briskin5.phh");
27
28 // Use of proxies isn't allowed.
29 if (is_proxy()) {
30   sleep(5);
31   exit;
32 }
33 log_load("index_rd.php");
34
35 $first_loop = TRUE;
36 $the_end = FALSE;
37
38 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
39   echo "Debugging time!";
40   exit;
41 }
42
43 function shutta()
44 {
45   log_rd2("SHUTTA!".connection_status());
46 }
47
48
49 register_shutdown_function(shutta);
50
51 function unrecerror()
52 {
53   GLOBAL $is_page_streaming;
54
55   $is_page_streaming = TRUE;
56   log_rd2("UNREC_ERROR:".var_export(debug_backtrace()));
57   return (sprintf('the_end=true; window.onunload = null; document.location.assign("index.php");'));
58 }
59
60 function page_sync($sess, $page, $table_idx, $table_token)
61 {
62   GLOBAL $is_page_streaming;
63
64   log_rd2("page_sync:".var_export(debug_backtrace()));
65
66   $is_page_streaming = TRUE;
67
68   log_rd2("PAGE_SYNC");
69   return (sprintf('createCookie("table_idx", %d, 24*365, cookiepath); createCookie("table_token", "%s", 24*365, cookiepath); the_end=true; window.onunload = null; document.location.assign("%s");', $table_idx, $table_token, $page));
70 }
71
72
73
74
75 function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step)
76 {
77   GLOBAL $is_page_streaming, $first_loop;
78   
79   $ret = FALSE;
80   $room = FALSE;
81
82   // log_rd2("M");
83   /* Sync check (read only without modifications */
84   ignore_user_abort(TRUE);
85   if (($sem = Room::lock_data()) != FALSE) { 
86     // Aggiorna l'expire time lato server
87     if  ($first_loop == TRUE) {
88       log_only("F");
89       $room = &Room::load_data();
90       if (($user = &$room->get_user($sess, $idx)) == FALSE) {
91         Room::unlock_data($sem);
92         ignore_user_abort(FALSE);
93         return (unrecerror());
94       }
95       log_auth($sess, "update lacc");
96       $user->lacc = time();
97
98       log_main("pre garbage_manager TRE");
99       $room->garbage_manager(FALSE);
100       
101       Room::save_data($room);
102       $first_loop = FALSE;
103     }
104
105     log_lock("U");
106     Room::unlock_data($sem);
107     ignore_user_abort(FALSE);
108   }
109   else {
110     return (FALSE);
111   }
112     
113   if (($proxy_step = step_get($sess)) != FALSE) {
114     // log_rd2("Postget".$proxy_step."zizi");
115
116     if ($cur_step == $proxy_step) {
117       log_lock("P");
118       return (FALSE);
119     }
120     else {
121       log_only2("R");
122     }
123   }
124   else {
125       log_only2("R");
126   }
127
128   if ($room == FALSE) {
129     do {
130       ignore_user_abort(TRUE);
131       if (($sem = Room::lock_data()) == FALSE) 
132         break;
133       
134       log_lock("P");
135       if (($room = &Room::load_data()) == FALSE) 
136         break;
137     } while (0);
138     
139     if ($sem != FALSE)
140       Room::unlock_data($sem);
141     
142     ignore_user_abort(FALSE);
143     if ($room == FALSE) 
144       return (FALSE);
145   }
146   
147   if (($user = &$room->get_user($sess, $idx)) == FALSE) {
148     return (unrecerror());
149   }
150
151   /* Nothing changed, return. */
152   if ($cur_step == $user->step) 
153     return (FALSE);
154
155   log_rd2("do other ++".$cur_stat."++".$user->stat."++".$cur_step."++".$user->step);
156
157   if ($cur_step == -1) {
158     // FUNZIONE from_scratch DA QUI 
159     ignore_user_abort(TRUE);
160     $sem = Room::lock_data();
161     $room = &Room::load_data();
162     if (($user = &$room->get_user($sess, $idx)) == FALSE) {
163       Room::unlock_data($sem);
164       ignore_user_abort(FALSE);
165       return (unrecerror());
166     }
167     if ($user->the_end) { 
168       log_rd2("main_check: the end".var_export(debug_backtrace()));
169       $is_page_streaming = TRUE;
170     }
171
172     if ($user->trans_step != -1) {
173       log_rd2("TRANS USATO ".$user->trans_step);
174       $cur_step = $user->trans_step;
175       $user->trans_step = -1;
176
177
178       Room::save_data($room);
179       Room::unlock_data($sem);
180       ignore_user_abort(FALSE);
181     }
182     else {
183        log_rd2("TRANS NON ATTIVATO");
184 //        ARRAY_POP DISABLED
185 //        log_rd2("TRANS NON ATTIVATO, clean del comm array");
186 //        while (($el = array_pop($user->comm)) != NULL) { 
187 //          log_rd2("clean element [".$el."]");
188 //        }
189 //        //        $user->step_inc(COMM_N + 1);
190 //        Room::save_data($room);
191 //        //        $new_step = $user->step;
192          
193        Room::unlock_data($sem);
194        ignore_user_abort(FALSE);
195     }
196   }
197       
198   if ($cur_step == -1) {
199     log_rd2("PRE-NEWSTAT: ".$user->stat);
200
201     if ($user->stat == 'room') {
202       log_rd("roomma ".$user->step);
203       $ret .= show_room(&$room, $user->step, &$user);
204
205       // TODO uncomment and test
206       /* NOTE the sets went common */
207       $new_stat =  $user->stat;
208       $new_subst = $user->subst;
209       $new_step =  $user->step;
210     }
211     /***************
212      *             *
213      *    TABLE    *
214      *             *
215      ***************/
216     else if ($user->stat == 'table') {
217       log_load("RESYNC");
218       return (page_sync($user->sess, "briskin5/index.php", $user->table, $user->table_token));
219     }
220     log_rd2("NEWSTAT: ".$user->stat);
221   }
222   else {
223     ignore_user_abort(TRUE);
224     $sem = Room::lock_data();
225     $room = &Room::load_data();
226     if (($user = &$room->get_user($sess, $idx)) == FALSE) {
227       Room::unlock_data($sem);
228       ignore_user_abort(FALSE);
229       return (unrecerror());
230     }
231     if ($cur_step < $user->step) {
232       do {
233         if ($cur_step + COMM_N < $user->step) {
234           if (($cur_stat != $user->stat)) {
235             $to_stat = $user->stat;
236             Room::unlock_data($sem);
237             ignore_user_abort(FALSE);
238             log_load("RESYNC");
239             return (page_sync($user->sess, ($to_stat == "table" ? "briskin5/index.php" : "index.php"), $user->table, $user->table_token));
240           }
241           log_rd2("lost history, refresh from scratch");
242           $new_step = -1;
243           break;
244         } 
245         for ($i = $cur_step ; $i < $user->step ; $i++) {
246           log_rd2("ADDED TO THE STREAM: ".$user->comm[$i % COMM_N]);
247           $ret .= $user->comm[$i % COMM_N];
248         }
249         $new_stat =  $user->stat;
250         $new_subst = $user->subst;
251         $new_step =  $user->step;
252       } while (0);
253       
254       if ($user->the_end == TRUE) {
255         log_rd2("LOGOUT BYE BYE!!");
256         log_auth($user->sess, "Explicit logout.");
257
258         $user->reset();
259         /* factorized with ->reset()
260         $tmp_sess = $user->sess;
261         $user->sess = "";
262         step_unproxy($tmp_sess);
263         $user->name = "";
264         while (array_pop($user->comm) != NULL); 
265         $user->step = 0;
266         $user->the_end = FALSE;
267         */
268
269         if ($user->subst == 'sitdown') {
270           log_load("ROOM WAKEUP");
271           $room->room_wakeup(&$user);
272         }
273         else if ($user->subst == 'standup')
274           $room->room_outstandup(&$user);
275         else
276           log_rd2("LOGOUT FROM WHAT ???");
277           
278         Room::save_data($room);
279       }
280     }
281           
282     Room::unlock_data($sem);
283     ignore_user_abort(FALSE);
284   }
285
286   
287   return ($ret);
288 }
289
290 /*
291  *  MAIN
292  */
293
294 /*
295    FROM THE EXTERN 
296    sess
297    stat
298    step
299 */
300
301 $is_page_streaming =  ((stristr($HTTP_USER_AGENT, "linux") && 
302                         (stristr($HTTP_USER_AGENT, "firefox") || stristr($HTTP_USER_AGENT, "iceweasel"))) ? FALSE : TRUE);
303
304
305 header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
306 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
307
308 if (!isset($myfrom))
309      $myfrom = "";
310 if (!isset($subst))
311      $subst = "";
312 log_rd2("FROM OUTSIDE - STAT: ".$stat." SUBST: ".$subst." STEP: ".$step." MYFROM: ".$myfrom. "IS_PAGE:" . $is_page_streaming);
313
314
315 $endtime = time() + STREAM_TIMEOUT;
316 $old_stat =  $stat;
317 $old_subst = $subst;
318 $old_step =  $ext_step = $step;
319
320 for ($i = 0 ; time() < $endtime ; $i++) {
321   // log_rd("PRE MAIN ".$step);;
322   if (($ret = maincheck($sess, $old_stat, $old_subst, $old_step, &$stat, &$subst, &$step)) != FALSE) {
323     echo '@BEGIN@';
324     // log_rd2(sprintf("\nSESS: [%s]\nOLD_STAT: [%s] OLD_SUBST: [%s] OLD_STEP: [%s] \nSTAT: [%s] SUBST: [%s] STEP: [%s] \nCOMM: [%s]\n", $sess, $old_stat, $old_subst, $old_step, $stat, $subst, $step, $ret));
325     echo "$ret";
326     echo ' @END@'; 
327     log_send("IS_PAGE: ".($is_page_streaming == TRUE ? "TRUE" : "FALSE")."EXT_STEP: ".$ext_step." ENDTIME: [".$endtime."] ".$ret);
328     flush();
329     if ($is_page_streaming)
330       break;
331   }
332   $old_stat =  $stat;
333   $old_subst = $subst;
334   $old_step =  $step;
335   // log_rd("POST MAIN ".$step);;
336   usleep(200000);
337   if (($i % 10) == 0) {
338     // log_rd2("TIME: ".time());
339     echo '_';
340     flush();
341   }
342 }
343
344 ?>