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