copyright updated and mop user status added
[brisk.git] / web / 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("briskin5/Obj/briskin5.phh");
28
29
30 $mlang_indrd = array( 
31                      'btn_backtotab'  => array('it' => 'torna ai tavoli',
32                                                'en' => 'back to tables') 
33                      );
34
35 // Use of proxies isn't allowed.
36 // if (is_proxy()) {
37 //   sleep(5);
38 //   exit;
39 //}
40 log_load("index_rd.php");
41
42 $first_loop = TRUE;
43 $the_end = FALSE;
44
45 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
46   echo "Debugging time!";
47   exit;
48 }
49
50 function shutta()
51 {
52   log_rd2("SHUTTA!".connection_status());
53 }
54
55
56 register_shutdown_function(shutta);
57
58 function unrecerror()
59 {
60   GLOBAL $is_page_streaming;
61
62   $is_page_streaming = TRUE;
63   log_rd2("UNREC_ERROR:".var_export(debug_backtrace()));
64   return (sprintf('the_end=true; window.onunload = null; window.onbeforeunload = null; document.location.assign("index.php");'));
65 }
66
67 function page_sync($sess, $page, $table_idx, $table_token)
68 {
69   GLOBAL $is_page_streaming;
70
71   log_rd2("page_sync:".var_export(debug_backtrace()));
72
73   $is_page_streaming = TRUE;
74
75   log_rd2("PAGE_SYNC");
76   return (sprintf('createCookie("table_idx", %d, 24*365, cookiepath); createCookie("table_token", "%s", 24*365, cookiepath); the_end=true; window.onunload = null; window.onbeforeunload = null; document.location.assign("%s");', $table_idx, $table_token, $page));
77 }
78
79
80
81
82 function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step)
83 {
84   GLOBAL $G_lang, $mlang_indrd, $is_page_streaming, $first_loop;
85   GLOBAL $G_with_splash, $G_splash_content, $G_splash_interval, $G_splash_idx;
86   GLOBAL $G_splash_w, $G_splash_h, $G_splash_timeout;
87   $CO_splashdate = "CO_splashdate".$G_splash_idx;
88   GLOBAL $$CO_splashdate;
89   
90   $ret = FALSE;
91   $room = FALSE;
92
93   // log_rd2("M");
94   /* Sync check (read only without modifications */
95   ignore_user_abort(TRUE);
96   if (($sem = Room::lock_data()) != FALSE) { 
97     // Aggiorna l'expire time lato server
98     if  ($first_loop == TRUE) {
99       log_only("F");
100       $room = &Room::load_data();
101       if (($user = &$room->get_user($sess, $idx)) == FALSE) {
102         Room::unlock_data($sem);
103         ignore_user_abort(FALSE);
104         return (unrecerror());
105       }
106       log_auth($sess, "update lacc");
107       $user->lacc = time();
108
109       log_main("pre garbage_manager TRE");
110       $room->garbage_manager(FALSE);
111       
112       Room::save_data($room);
113       $first_loop = FALSE;
114     }
115
116     log_lock("U");
117     Room::unlock_data($sem);
118     ignore_user_abort(FALSE);
119   }
120   else {
121     // wait 20 secs, then restart the xhr 
122     ignore_user_abort(FALSE);
123
124     return ("sleep(gst,20000);|xhr_rd_abort(xhr_rd);");
125     /*
126     ignore_user_abort(FALSE);
127     return (FALSE);
128     */
129   }
130     
131   if (($proxy_step = step_get($sess)) != FALSE) {
132     // log_rd2("Postget".$proxy_step."zizi");
133
134     if ($cur_step == $proxy_step) {
135       log_lock("P");
136       return (FALSE);
137     }
138     else {
139       log_only2("R");
140     }
141   }
142   else {
143       log_only2("R");
144   }
145
146   if ($room == FALSE) {
147     do {
148       ignore_user_abort(TRUE);
149       if (($sem = Room::lock_data()) == FALSE) 
150         break;
151       
152       log_lock("P");
153       if (($room = &Room::load_data()) == FALSE) 
154         break;
155     } while (0);
156     
157     if ($sem != FALSE)
158       Room::unlock_data($sem);
159     
160     ignore_user_abort(FALSE);
161     if ($room == FALSE) 
162       return (FALSE);
163   }
164   
165   if (($user = &$room->get_user($sess, $idx)) == FALSE) {
166     return (unrecerror());
167   }
168
169   /* Nothing changed, return. */
170   if ($cur_step == $user->step) 
171     return (FALSE);
172
173   log_rd2("do other ++".$cur_stat."++".$user->stat."++".$cur_step."++".$user->step);
174
175   if ($cur_step == -1) {
176     // FUNZIONE from_scratch DA QUI 
177     ignore_user_abort(TRUE);
178     $sem = Room::lock_data();
179     $room = &Room::load_data();
180     if (($user = &$room->get_user($sess, $idx)) == FALSE) {
181       Room::unlock_data($sem);
182       ignore_user_abort(FALSE);
183       return (unrecerror());
184     }
185     if ($user->the_end) { 
186       log_rd2("main_check: the end".var_export(debug_backtrace()));
187       $is_page_streaming = TRUE;
188     }
189
190     if ($user->trans_step != -1) {
191       log_rd2("TRANS USATO ".$user->trans_step);
192       $cur_step = $user->trans_step;
193       $user->trans_step = -1;
194
195
196       Room::save_data($room);
197       Room::unlock_data($sem);
198       ignore_user_abort(FALSE);
199     }
200     else {
201        log_rd2("TRANS NON ATTIVATO");
202 //        ARRAY_POP DISABLED
203 //        log_rd2("TRANS NON ATTIVATO, clean del comm array");
204 //        while (($el = array_pop($user->comm)) != NULL) { 
205 //          log_rd2("clean element [".$el."]");
206 //        }
207 //        //        $user->step_inc(COMM_N + 1);
208 //        Room::save_data($room);
209 //        //        $new_step = $user->step;
210          
211        Room::unlock_data($sem);
212        ignore_user_abort(FALSE);
213     }
214   }
215       
216   if ($cur_step == -1) {
217     log_rd2("PRE-NEWSTAT: ".$user->stat);
218
219     if ($user->stat == 'room') {
220       log_rd("roomma ".$user->step);
221       $curtime = time();
222
223       if ($G_with_splash &&
224           ($$CO_splashdate < $curtime - $G_splash_interval ||
225            $$CO_splashdate > $curtime)) {
226         $ret .=  show_notify_ex(str_replace("\n", " ", $G_splash_content[$G_lang]), $G_splash_timeout, $mlang_indrd['btn_backtotab'][$G_lang], $G_splash_w, $G_splash_h, true, $G_splash_timeout);
227         $ret .= sprintf('|createCookie("CO_splashdate%d", %d, 24*365, cookiepath);', $G_splash_idx, $curtime);
228       }
229       $ret .= $room->show_room($user->step, &$user);
230
231       // TODO uncomment and test
232       /* NOTE the sets went common */
233       $new_stat =  $user->stat;
234       $new_subst = $user->subst;
235       $new_step =  $user->step;
236     }
237     /***************
238      *             *
239      *    TABLE    *
240      *             *
241      ***************/
242     else if ($user->stat == 'table') {
243       log_load("RESYNC");
244       return (page_sync($user->sess, "briskin5/index.php", $user->table, $user->table_token));
245     }
246     log_rd2("NEWSTAT: ".$user->stat);
247   }
248   else {
249     ignore_user_abort(TRUE);
250     $sem = Room::lock_data();
251     $room = &Room::load_data();
252     if (($user = &$room->get_user($sess, $idx)) == FALSE) {
253       Room::unlock_data($sem);
254       ignore_user_abort(FALSE);
255       return (unrecerror());
256     }
257     if ($cur_step < $user->step) {
258       do {
259         if ($cur_step + COMM_N < $user->step) {
260           if (($cur_stat != $user->stat)) {
261             $to_stat = $user->stat;
262             Room::unlock_data($sem);
263             ignore_user_abort(FALSE);
264             log_load("RESYNC");
265             return (page_sync($user->sess, ($to_stat == "table" ? "briskin5/index.php" : "index.php"), $user->table, $user->table_token));
266           }
267           log_rd2("lost history, refresh from scratch");
268           $new_step = -1;
269           break;
270         } 
271         for ($i = $cur_step ; $i < $user->step ; $i++) {
272           log_rd2("ADDED TO THE STREAM: ".$user->comm[$i % COMM_N]);
273           $ret .= $user->comm[$i % COMM_N];
274         }
275         $new_stat =  $user->stat;
276         $new_subst = $user->subst;
277         $new_step =  $user->step;
278       } while (0);
279
280       log_mop($user->step, 'index_rd.php: after ret set');
281
282       if ($user->the_end == TRUE) {
283         log_rd2("LOGOUT BYE BYE!!");
284         log_auth($user->sess, "Explicit logout.");
285
286         $user->reset();
287
288         if ($user->subst == 'sitdown') {
289           log_load("ROOM WAKEUP");
290           $room->room_wakeup(&$user);
291         }
292         else if ($user->subst == 'standup')
293           $room->room_outstandup(&$user);
294         else
295           log_rd2("LOGOUT FROM WHAT ???");
296           
297         Room::save_data($room);
298       }
299     }
300           
301     Room::unlock_data($sem);
302     ignore_user_abort(FALSE);
303   }
304
305   
306   return ($ret);
307 }
308
309 /*
310  *  MAIN
311  */
312
313 /*
314    FROM THE EXTERN 
315    sess
316    stat
317    step
318 */
319
320 $is_page_streaming =  (stristr($HTTP_USER_AGENT, "MSIE") || stristr($HTTP_USER_AGENT, "CHROME") ? TRUE : FALSE);
321
322 header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
323 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
324 header('Content-type: application/xml; charset="utf-8"',true);
325 // header('Content-type: text/plain; charset="utf-8"',true);
326 // header('Content-type: text/html; charset="utf-8"',true);
327
328 if (!isset($myfrom))
329      $myfrom = "";
330 if (!isset($subst))
331      $subst = "";
332 log_rd2("FROM OUTSIDE - STAT: ".$stat." SUBST: ".$subst." STEP: ".$step." MYFROM: ".$myfrom. "IS_PAGE:" . $is_page_streaming);
333
334
335 $endtime = time() + STREAM_TIMEOUT;
336 $old_stat =  $stat;
337 $old_subst = $subst;
338 $old_step =  $ext_step = $step;
339
340 for ($i = 0 ; time() < $endtime ; $i++) {
341   // log_rd("PRE MAIN ".$step);;
342   $pre_main = gettimeofday(TRUE);
343   if (($ret = maincheck($sess, $old_stat, $old_subst, $old_step, &$stat, &$subst, &$step)) != FALSE) {
344     echo '@BEGIN@';
345     // 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));
346     echo "$ret";
347     echo ' @END@'; 
348     log_send("IS_PAGE: ".($is_page_streaming == TRUE ? "TRUE" : "FALSE")."EXT_STEP: ".$ext_step." ENDTIME: [".$endtime."] ".$ret);
349     flush();
350     log_mop(0, 'index_rd.php: after flush (begin: '.sprintf("%f", $pre_main).')');
351     if ($is_page_streaming)
352       break;
353   }
354   $old_stat =  $stat;
355   $old_subst = $subst;
356   $old_step =  $step;
357   // log_rd("POST MAIN ".$step);;
358   usleep(400000);
359   if (($i % 10) == 0) {
360     // log_rd2("TIME: ".time());
361     echo '_';
362     flush();
363   }
364 }
365
366 ?>