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