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