removed proxy check from _wr and _rd
[brisk.git] / web / briskin5 / 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("Obj/briskin5.phh");
27
28 // Use of proxies isn't allowed.
29 // if (is_proxy()) {
30 //   sleep(5);
31 //   exit;
32 // }
33
34 log_load("LOAD: bin5/index_rd.php ".$QUERY_STRING);
35
36 $first_loop = TRUE;
37 $the_end = FALSE;
38
39 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
40   echo "Debugging time!";
41   exit;
42 }
43
44 function shutta()
45 {
46   log_rd2("bin5 SHUTTA!".connection_status());
47 }
48
49
50 register_shutdown_function(shutta);
51
52 function unrecerror()
53 {
54   GLOBAL $is_page_streaming;
55
56   $is_page_streaming = TRUE;
57   log_rd2("UNREC_ERROR");
58   return (sprintf('the_end=true; window.onunload = null; document.location.assign("../index.php");'));
59 }
60
61 function page_sync($sess, $page)
62 {
63   GLOBAL $is_page_streaming;
64
65   $is_page_streaming = TRUE;
66   log_rd2("PAGE_SYNC");
67   return (sprintf('the_end=true; window.onunload = null; document.location.assign("%s");', $page));
68 }
69
70
71
72
73 function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step, $table_idx, $table_token)
74 {
75   GLOBAL $is_page_streaming, $first_loop;
76   
77   $ret = FALSE;
78   $bri = FALSE;
79
80   // log_rd2("M");
81   /* Sync check (read only without modifications */
82   ignore_user_abort(TRUE);
83   if (($sem = Briskin5::lock_data($table_idx)) != FALSE) { 
84     // Aggiorna l'expire time lato server
85     if  ($first_loop == TRUE) {
86       log_only("F");
87
88       // VERIFICARE TUTTE LE LOAD_DATA E PRENDERE CONTROMISURE NEL CASO FALLISCANO //
89
90       if (($bri = &Briskin5::load_data($table_idx, $table_token)) == FALSE) {
91         Briskin5::unlock_data($sem);
92         ignore_user_abort(FALSE);
93         return (unrecerror());
94       }
95       if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
96         Briskin5::unlock_data($sem);
97         ignore_user_abort(FALSE);
98         return (unrecerror());
99       }
100       log_auth($sess, "bin5::update lacc");
101       $user->lacc = time();
102
103       $bri->garbage_manager(FALSE);
104       
105       Briskin5::save_data($bri);
106       $first_loop = FALSE;
107     }
108
109     log_lock("U");
110     Briskin5::unlock_data($sem);
111     ignore_user_abort(FALSE);
112   }
113   else {
114     return (FALSE);
115   }
116   
117   if (($proxy_step = step_get($sess)) != FALSE) {
118     // log_rd2("Postget".$proxy_step."zizi");
119     
120     if ($cur_step == $proxy_step) {
121       log_lock("P");
122       return (FALSE);
123     }
124     else {
125       log_only2("R");
126     }
127   }
128   else {
129     log_only2("R");
130   }
131   
132   if ($bri == FALSE) {
133     do {
134       ignore_user_abort(TRUE);
135       if (($sem = Briskin5::lock_data($table_idx)) == FALSE) 
136         break;
137       
138       log_lock("P");
139       if (($bri = &Briskin5::load_data($table_idx, $table_token)) == FALSE) 
140         break;
141     } while (0);
142     
143     if ($sem != FALSE)
144       Briskin5::unlock_data($sem);
145     
146     ignore_user_abort(FALSE);
147     if ($bri == FALSE) 
148       return (unrecerror());
149   }
150   
151   if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
152     return (unrecerror());
153   }
154
155   /* Nothing changed, return. */
156   if ($cur_step == $user->step) 
157     return;
158
159   log_rd2("do other ++".$cur_stat."++".$user->stat."++".$cur_step."++".$user->step);
160
161   if ($cur_step == -1) {
162     // FUNZIONE from_scratch DA QUI 
163     ignore_user_abort(TRUE);
164     $sem = Briskin5::lock_data($table_idx);
165     $bri = &Briskin5::load_data($table_idx, $table_token);
166     if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
167       Briskin5::unlock_data($sem);
168       ignore_user_abort(FALSE);
169       return (unrecerror());
170     }
171     if ($user->the_end) 
172       $is_page_streaming = TRUE;
173
174
175     if ($user->trans_step != -1) {
176       log_rd2("TRANS USATO ".$user->trans_step);
177       $cur_step = $user->trans_step;
178       $user->trans_step = -1;
179
180
181       Briskin5::save_data($bri);
182       Briskin5::unlock_data($sem);
183       ignore_user_abort(FALSE);
184     }
185     else {
186       log_rd2("TRANS NON ATTIVATO");
187
188 //       ARRAY_POP DISABLED
189 //       while (array_pop($user->comm) != NULL);
190 //       // $user->step_inc(COMM_N + 1);
191 //       Briskin5::save_data($bri);
192
193       Briskin5::unlock_data($sem);
194       ignore_user_abort(FALSE);
195     }
196   }
197       
198   if ($cur_step == -1) {
199     log_rd2("PRE-NEWSTAT.");
200
201     /***************
202      *             *
203      *    TABLE    *
204      *             *
205      ***************/
206     if ($user->stat == "table") {      
207       $ret = show_table(&$bri,&$user,$user->step,FALSE,FALSE);
208
209       log_rd2("SENDED TO THE STREAM: ".$ret);
210     }
211     log_rd2("NEWSTAT: ".$user->stat);
212
213     $new_stat =  $user->stat;
214     $new_subst = $user->subst;
215     $new_step =  $user->step;
216   }
217   else {
218     ignore_user_abort(TRUE);
219     $sem = Briskin5::lock_data($table_idx);
220     if (($bri = &Briskin5::load_data($table_idx, $table_token)) == FALSE) {
221       Briskin5::unlock_data($sem);
222       ignore_user_abort(FALSE);
223       return (unrecerror());
224     }
225     if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
226       Briskin5::unlock_data($sem);
227       ignore_user_abort(FALSE);
228       return (unrecerror());
229     }
230     if ($cur_step < $user->step) {
231       do {
232         if ($cur_step + COMM_N < $user->step) {
233           if (($cur_stat != $user->stat)) {
234             $to_stat = $user->stat;
235             Briskin5::unlock_data($sem);
236             ignore_user_abort(FALSE);
237             return (page_sync($user->sess, $to_stat == "table" ? "index.php" : "../index.php"));
238           }
239           log_rd2("lost history, refresh from scratch");
240           $new_step = -1;
241           break;
242         } 
243         for ($i = $cur_step ; $i < $user->step ; $i++) {
244           $ii = $i % COMM_N;
245           log_wr("TRY RET ".$i."  COMM_N ".COMM_N."  II ".$ii);
246           $ret .= $user->comm[$ii];
247         }
248         $new_stat =  $user->stat;
249         $new_subst = $user->subst;
250         $new_step =  $user->step;
251       } while (0);
252       
253       if ($user->the_end == TRUE) {
254         log_rd2("LOGOUT BYE BYE!!");
255         log_auth($user->sess, "Explicit logout.");
256         $tmp_sess = $user->sess;
257         $user->sess = "";
258         step_unproxy($tmp_sess);
259         
260         $user->name = "";
261         $user->the_end = FALSE;
262         
263         if ($user->subst == 'sitdown')
264           $bri->room_wakeup(&$user);
265         else if ($user->subst == 'standup')
266           $bri->room_outstandup(&$user);
267         else
268           log_rd2("LOGOUT FROM WHAT ???");
269           
270         Briskin5::save_data($bri);
271       }
272     }
273           
274     Briskin5::unlock_data($sem);
275     ignore_user_abort(FALSE);
276   }
277
278   
279   return ($ret);
280 }
281
282 /*
283  *  MAIN
284  */
285
286 /*
287    FROM THE EXTERN 
288    sess
289    stat
290    step
291 */
292
293 $is_page_streaming =  ((stristr($HTTP_USER_AGENT, "linux") && 
294                         (stristr($HTTP_USER_AGENT, "firefox") || stristr($HTTP_USER_AGENT, "iceweasel"))) ? FALSE : TRUE);
295
296
297 header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
298 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
299
300 if (!isset($myfrom))
301      $myfrom = "";
302 if (!isset($subst))
303      $subst = "";
304 log_rd2("FROM OUTSIDE - STAT: ".$stat." SUBST: ".$subst." STEP: ".$step." MYFROM: ".$myfrom. "IS_PAGE:" . $is_page_streaming."USER_AGENT:".$HTTP_USER_AGENT);
305
306
307 $endtime = time() + STREAM_TIMEOUT;
308 $old_stat =  $stat;
309 $old_subst = $subst;
310 $old_step =  $ext_step = $step;
311
312 for ($i = 0 ; time() < $endtime ; $i++) {
313   // log_rd("PRE MAIN ".$step);;
314   if (($ret = maincheck($sess, $old_stat, $old_subst, $old_step, &$stat, &$subst, &$step, $table_idx, $table_token)) != FALSE) {
315     echo '@BEGIN@';
316     // 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));
317     echo "$ret";
318     echo ' @END@'; 
319     log_send("EXT_STEP: ".$ext_step." ENDTIME: [".$endtime."] ".$ret);
320     flush();
321     if ($is_page_streaming)
322       break;
323   }
324   $old_stat =  $stat;
325   $old_subst = $subst;
326   $old_step =  $step;
327   // log_rd("POST MAIN ".$step);;
328   usleep(200000);
329   if (($i % 10) == 0) {
330     // log_rd2("TIME: ".time());
331     echo '_';
332     flush();
333   }
334 }
335
336 ?>