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