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