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