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