b88ce9949d592e183c9e440013cc5359b021810f
[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($sess, $page)
54 {
55   GLOBAL $is_page_streaming;
56
57   $is_page_streaming = TRUE;
58   log_rd2($sess, "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_only($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
94     log_only($sess, "U");
95     unlock_data($sem);
96     ignore_user_abort(FALSE);
97   }
98   else {
99     return (FALSE);
100   }
101     
102   if (($proxy_step = step_get($sess)) != FALSE) {
103     // log_rd2($sess, "Postget".$proxy_step."zizi");
104
105     if ($cur_step == $proxy_step) {
106       log_only2($sess, "P");
107       return (FALSE);
108     }
109     else {
110       log_only2($sess, "R");
111     }
112   }
113   else {
114       log_only2($sess, "R");
115   }
116
117   if ($bri == FALSE) {
118     ignore_user_abort(TRUE);
119     if (($sem = lock_data()) != FALSE) { 
120       log_only($sess, "P");
121       $bri = &load_data();
122       unlock_data($sem);
123       ignore_user_abort(FALSE);
124     }
125     else {
126       return (FALSE);
127     }
128   }
129   
130   if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
131     return (unrecerror());
132   }
133
134   /* Nothing changed, return. */
135   if ($cur_step == $user->step) 
136     return;
137
138   log_rd2($sess, "do other ++".$cur_stat."++".$user->stat."++".$cur_step."++".$user->step);
139
140   if ($cur_step == -1) {
141     // FUNZIONE from_scratch DA QUI 
142     ignore_user_abort(TRUE);
143     $sem = lock_data();
144     $bri = &load_data();
145     if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
146       unlock_data($sem);
147       ignore_user_abort(FALSE);
148       return (unrecerror());
149     }
150     if ($user->the_end) 
151       $is_page_streaming = TRUE;
152
153
154     if ($user->trans_step != -1) {
155       log_rd2($sess, "TRANS USATO ".$user->trans_step);
156       $cur_step = $user->trans_step;
157       $user->trans_step = -1;
158
159
160       save_data($bri);
161       unlock_data($sem);
162       ignore_user_abort(FALSE);
163     }
164     else {
165       log_rd2($sess, "TRANS NON ATTIVATO");
166       unlock_data($sem);
167       ignore_user_abort(FALSE);
168     }
169   }
170       
171   if ($cur_step == -1) {
172     log_rd2($sess, "PRE-NEWSTAT.");
173
174     if ($user->stat == 'room') {
175       log_rd($sess, "roomma");
176       $ret .= show_room(&$bri, &$user);
177     }
178     /***************
179      *             *
180      *    TABLE    *
181      *             *
182      ***************/
183     else if ($user->stat == 'table') {      
184       $ret = show_table(&$bri,&$user,$user->step,FALSE,FALSE);
185
186       log_rd2($sess, "SENDED TO THE STREAM: ".$ret);
187     }
188     log_rd2($sess, "NEWSTAT: ".$user->stat);
189
190     $new_stat =  $user->stat;
191     $new_subst = $user->subst;
192     $new_step =  $user->step;
193   }
194   else {
195     ignore_user_abort(TRUE);
196     $sem = lock_data();
197     $bri = &load_data();
198     if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
199       unlock_data($sem);
200       ignore_user_abort(FALSE);
201       return (unrecerror());
202     }
203     if ($cur_step < $user->step) {
204       do {
205         if ($cur_step + COMM_N < $user->step) {
206           if (($cur_stat != $user->stat)) {
207             $to_stat = $user->stat;
208             unlock_data($sem);
209             ignore_user_abort(FALSE);
210             return (page_sync($user->sess, $to_stat == "table" ? "table.php" : "index.php"));
211           }
212           log_rd2($sess, "lost history, refresh from scratch");
213           $new_step = -1;
214           break;
215         } 
216         for ($i = $cur_step ; $i < $user->step ; $i++) {
217           log_rd2($sess, "ADDED TO THE STREAM: ".$user->comm[$i % COMM_N]);
218           $ret .= $user->comm[$i % COMM_N];
219         }
220         $new_stat =  $user->stat;
221         $new_subst = $user->subst;
222         $new_step =  $user->step;
223       } while (0);
224       
225       if ($user->the_end == TRUE) {
226         log_rd2($sess, "LOGOUT BYE BYE!!");
227         log_auth($user->sess, "Explicit logout.");
228         $tmp_sess = $user->sess;
229         $user->sess = "";
230         step_unproxy($tmp_sess);
231         
232         $user->name = "";
233         $user->the_end = FALSE;
234         
235         if ($user->subst == 'sitdown')
236           $bri->room_wakeup(&$user);
237         else if ($user->subst == 'standup')
238           $bri->room_outstandup(&$user);
239         else
240           log_rd2($sess, "LOGOUT FROM WHAT ???");
241           
242         save_data($bri);
243       }
244     }
245           
246     unlock_data($sem);
247     ignore_user_abort(FALSE);
248   }
249
250   
251   return ($ret);
252 }
253
254 /*
255  *  MAIN
256  */
257
258 /*
259    FROM THE EXTERN 
260    sess
261    stat
262    step
263 */
264
265 $is_page_streaming =  ((stristr($HTTP_USER_AGENT, "linux") && 
266                         stristr($HTTP_USER_AGENT, "firefox")) ? FALSE : TRUE);
267
268
269 header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
270 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
271
272 if (!isset($myfrom))
273      $myfrom = "";
274 if (!isset($subst))
275      $subst = "";
276 log_rd2($sess, "FROM OUTSIDE - STAT: ".$stat." SUBST: ".$subst." STEP: ".$step." MYFROM: ".$myfrom. "IS_PAGE:" . $is_page_streaming);
277
278
279 $endtime = time() + STREAM_TIMEOUT;
280 $old_stat =  $stat;
281 $old_subst = $subst;
282 $old_step =  $ext_step = $step;
283
284 for ($i = 0 ; time() < $endtime ; $i++) {
285   // log_rd($sess, "PRE MAIN ".$step);;
286   if (($ret = maincheck($sess, $old_stat, $old_subst, $old_step, &$stat, &$subst, &$step)) != FALSE) {
287     echo '@BEGIN@';
288     // 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));
289     echo "$ret";
290     echo ' @END@'; 
291     log_send($sess, "EXT_STEP: ".$ext_step." ENDTIME: [".$endtime."] ".$ret);
292     flush();
293     if ($is_page_streaming)
294       break;
295   }
296   $old_stat =  $stat;
297   $old_subst = $subst;
298   $old_step =  $step;
299   // log_rd($sess, "POST MAIN ".$step);;
300   usleep(400000);
301   if (($i % 5) == 0) {
302     // log_rd2($sess, "TIME: ".time());
303     echo '_';
304     flush();
305   }
306 }
307
308 ?>