copyright updated and mop user status added
[brisk.git] / web / briskin5 / index_rd.php
1 <?php
2 /*
3  *  brisk - index_rd.php
4  *
5  *  Copyright (C) 2006-2011 Matteo Nastasi
6  *                          mailto: nastasi@alternativeoutput.it 
7  *                                  matteo.nastasi@milug.org
8  *                          web: http://www.alternativeoutput.it
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details. You should have received a
19  * copy of the GNU General Public License along with this program; if
20  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
21  * Suite 330, Boston, MA 02111-1307, USA.
22  *
23  */
24
25 require_once("../Obj/brisk.phh");
26 // require_once("../Obj/proxyscan.phh");
27 require_once("Obj/briskin5.phh");
28
29 // Use of proxies isn't allowed.
30 // if (is_proxy()) {
31 //   sleep(5);
32 //   exit;
33 // }
34
35 log_load("LOAD: bin5/index_rd.php ".$QUERY_STRING);
36
37 $first_loop = TRUE;
38 $the_end = FALSE;
39
40 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
41   echo "Debugging time!";
42   exit;
43 }
44
45 function shutta()
46 {
47   log_rd2("bin5 SHUTTA!".connection_status());
48 }
49
50
51 register_shutdown_function(shutta);
52
53 function unrecerror()
54 {
55   GLOBAL $is_page_streaming;
56
57   $is_page_streaming = TRUE;
58   log_rd2("UNREC_ERROR");
59   return (sprintf('the_end=true; window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");'));
60 }
61
62 function page_sync($sess, $page)
63 {
64   GLOBAL $is_page_streaming;
65
66   $is_page_streaming = TRUE;
67   log_rd2("PAGE_SYNC");
68   return (sprintf('the_end=true; window.onbeforeunload = null; window.onunload = null; document.location.assign("%s");', $page));
69 }
70
71
72
73
74 function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step, $table_idx, $table_token)
75 {
76   GLOBAL $is_page_streaming, $first_loop;
77   
78   $ret = FALSE;
79   $bri = FALSE;
80
81   // log_rd2("M");
82   /* Sync check (read only without modifications */
83   ignore_user_abort(TRUE);
84   if (($sem = Briskin5::lock_data($table_idx)) != FALSE) { 
85     // Aggiorna l'expire time lato server
86     if  ($first_loop == TRUE) {
87       log_only("F");
88
89       // VERIFICARE TUTTE LE LOAD_DATA E PRENDERE CONTROMISURE NEL CASO FALLISCANO //
90
91       if (($bri = &Briskin5::load_data($table_idx, $table_token)) == FALSE) {
92         Briskin5::unlock_data($sem);
93         ignore_user_abort(FALSE);
94         return (unrecerror());
95       }
96       if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
97         Briskin5::unlock_data($sem);
98         ignore_user_abort(FALSE);
99         return (unrecerror());
100       }
101       log_auth($sess, "bin5::update lacc");
102       $user->lacc = time();
103
104       $bri->garbage_manager(FALSE);
105       
106       Briskin5::save_data($bri);
107       $first_loop = FALSE;
108     }
109
110     log_lock("U");
111     Briskin5::unlock_data($sem);
112     ignore_user_abort(FALSE);
113   }
114   else {
115     return (FALSE);
116   }
117   
118   if (($proxy_step = step_get($sess)) != FALSE) {
119     // log_rd2("Postget".$proxy_step."zizi");
120     
121     if ($cur_step == $proxy_step) {
122       log_lock("P");
123       return (FALSE);
124     }
125     else {
126       log_only2("R");
127     }
128   }
129   else {
130     log_only2("R");
131   }
132   
133   if ($bri == FALSE) {
134     do {
135       ignore_user_abort(TRUE);
136       if (($sem = Briskin5::lock_data($table_idx)) == FALSE) 
137         break;
138       
139       log_lock("P");
140       if (($bri = &Briskin5::load_data($table_idx, $table_token)) == FALSE) 
141         break;
142     } while (0);
143     
144     if ($sem != FALSE)
145       Briskin5::unlock_data($sem);
146     
147     ignore_user_abort(FALSE);
148     if ($bri == FALSE) 
149       return (unrecerror());
150   }
151   
152   if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
153     return (unrecerror());
154   }
155
156   /* Nothing changed, return. */
157   if ($cur_step == $user->step) 
158     return;
159
160   log_rd2("do other ++".$cur_stat."++".$user->stat."++".$cur_step."++".$user->step);
161
162   if ($cur_step == -1) {
163     // FUNZIONE from_scratch DA QUI 
164     ignore_user_abort(TRUE);
165     $sem = Briskin5::lock_data($table_idx);
166     $bri = &Briskin5::load_data($table_idx, $table_token);
167     if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
168       Briskin5::unlock_data($sem);
169       ignore_user_abort(FALSE);
170       return (unrecerror());
171     }
172     if ($user->the_end) 
173       $is_page_streaming = TRUE;
174
175
176     if ($user->trans_step != -1) {
177       log_rd2("TRANS USATO ".$user->trans_step);
178       $cur_step = $user->trans_step;
179       $user->trans_step = -1;
180
181
182       Briskin5::save_data($bri);
183       Briskin5::unlock_data($sem);
184       ignore_user_abort(FALSE);
185     }
186     else {
187       log_rd2("TRANS NON ATTIVATO");
188
189 //       ARRAY_POP DISABLED
190 //       while (array_pop($user->comm) != NULL);
191 //       // $user->step_inc(COMM_N + 1);
192 //       Briskin5::save_data($bri);
193
194       Briskin5::unlock_data($sem);
195       ignore_user_abort(FALSE);
196     }
197   }
198       
199   if ($cur_step == -1) {
200     log_rd2("PRE-NEWSTAT.");
201
202     /***************
203      *             *
204      *    TABLE    *
205      *             *
206      ***************/
207     if ($user->stat == "table") {      
208       $ret = show_table(&$bri,&$user,$user->step,FALSE,FALSE);
209
210       log_rd2("SENDED TO THE STREAM: ".$ret);
211     }
212     log_rd2("NEWSTAT: ".$user->stat);
213
214     $new_stat =  $user->stat;
215     $new_subst = $user->subst;
216     $new_step =  $user->step;
217   }
218   else {
219     ignore_user_abort(TRUE);
220     $sem = Briskin5::lock_data($table_idx);
221     if (($bri = &Briskin5::load_data($table_idx, $table_token)) == FALSE) {
222       Briskin5::unlock_data($sem);
223       ignore_user_abort(FALSE);
224       return (unrecerror());
225     }
226     if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
227       Briskin5::unlock_data($sem);
228       ignore_user_abort(FALSE);
229       return (unrecerror());
230     }
231     if ($cur_step < $user->step) {
232       do {
233         if ($cur_step + COMM_N < $user->step) {
234           if (($cur_stat != $user->stat)) {
235             $to_stat = $user->stat;
236             Briskin5::unlock_data($sem);
237             ignore_user_abort(FALSE);
238             return (page_sync($user->sess, $to_stat == "table" ? "index.php" : "../index.php"));
239           }
240           log_rd2("lost history, refresh from scratch");
241           $new_step = -1;
242           break;
243         } 
244         for ($i = $cur_step ; $i < $user->step ; $i++) {
245           $ii = $i % COMM_N;
246           log_wr("TRY RET ".$i."  COMM_N ".COMM_N."  II ".$ii);
247           $ret .= $user->comm[$ii];
248         }
249         $new_stat =  $user->stat;
250         $new_subst = $user->subst;
251         $new_step =  $user->step;
252       } while (0);
253
254       log_mop($user->step, 'bin::index_rd.php: after ret set');
255       
256       if ($user->the_end == TRUE) {
257         log_rd2("LOGOUT BYE BYE!!");
258         log_auth($user->sess, "Explicit logout.");
259         $tmp_sess = $user->sess;
260         $user->sess = "";
261         step_unproxy($tmp_sess);
262         
263         $user->name = "";
264         $user->the_end = FALSE;
265         
266         if ($user->subst == 'sitdown')
267           $bri->room_wakeup(&$user);
268         else if ($user->subst == 'standup')
269           $bri->room_outstandup(&$user);
270         else
271           log_rd2("LOGOUT FROM WHAT ???");
272           
273         Briskin5::save_data($bri);
274       }
275     }
276           
277     Briskin5::unlock_data($sem);
278     ignore_user_abort(FALSE);
279   }
280
281   
282   return ($ret);
283 }
284
285 /*
286  *  MAIN
287  */
288
289 /*
290    FROM THE EXTERN 
291    sess
292    stat
293    step
294 */
295
296 $is_page_streaming =  (stristr($HTTP_USER_AGENT, "MSIE") || stristr($HTTP_USER_AGENT, "CHROME") ? TRUE : FALSE);
297
298 header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
299 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
300 header('Content-type: application/xml; charset="utf-8"',true);
301 // header('Content-type: text/plain; charset="utf-8"',true);
302 // header('Content-type: text/html; charset="utf-8"',true);
303
304 if (!isset($myfrom))
305      $myfrom = "";
306 if (!isset($subst))
307      $subst = "";
308 log_rd2("FROM OUTSIDE - STAT: ".$stat." SUBST: ".$subst." STEP: ".$step." MYFROM: ".$myfrom. "IS_PAGE:" . $is_page_streaming."USER_AGENT:".$HTTP_USER_AGENT);
309
310
311 $endtime = time() + STREAM_TIMEOUT;
312 $old_stat =  $stat;
313 $old_subst = $subst;
314 $old_step =  $ext_step = $step;
315
316 for ($i = 0 ; time() < $endtime ; $i++) {
317   // log_rd("PRE MAIN ".$step);;
318   $pre_main = gettimeofday(TRUE);
319   if (($ret = maincheck($sess, $old_stat, $old_subst, $old_step, &$stat, &$subst, &$step, $table_idx, $table_token)) != FALSE) {
320     echo '@BEGIN@';
321     // log_rd2(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));
322     echo "$ret";
323     echo ' @END@'; 
324     log_send("EXT_STEP: ".$ext_step." ENDTIME: [".$endtime."] ".$ret);
325     flush();
326     log_mop(0, 'bin::index_rd.php: after flush (begin: '.sprintf("%f", $pre_main).')');
327     if ($is_page_streaming)
328       break;
329   }
330   $old_stat =  $stat;
331   $old_subst = $subst;
332   $old_step =  $step;
333   // log_rd("POST MAIN ".$step);;
334   usleep(400000);
335   if (($i % 10) == 0) {
336     // log_rd2("TIME: ".time());
337     echo '_';
338     flush();
339   }
340 }
341
342 ?>