allignment pre merge in master
[brisk.git] / web / index_rd.php
1 <?php
2 /*
3  *  brisk - index_rd.php
4  *
5  *  Copyright (C) 2006-2009 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  * $Id$
24  *
25  */
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                      );
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         $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);
229         $ret .= sprintf('|createCookie("CO_splashdate%d", %d, 24*365, cookiepath);', $G_splash_idx, $curtime);
230       }
231       $ret .= $room->show_room($user->step, &$user);
232
233       // TODO uncomment and test
234       /* NOTE the sets went common */
235       $new_stat =  $user->stat;
236       $new_subst = $user->subst;
237       $new_step =  $user->step;
238     }
239     /***************
240      *             *
241      *    TABLE    *
242      *             *
243      ***************/
244     else if ($user->stat == 'table') {
245       log_load("RESYNC");
246       return (page_sync($user->sess, "briskin5/index.php", $user->table, $user->table_token));
247     }
248     log_rd2("NEWSTAT: ".$user->stat);
249   }
250   else {
251     ignore_user_abort(TRUE);
252     $sem = Room::lock_data();
253     $room = &Room::load_data();
254     if (($user = &$room->get_user($sess, $idx)) == FALSE) {
255       Room::unlock_data($sem);
256       ignore_user_abort(FALSE);
257       return (unrecerror());
258     }
259     if ($cur_step < $user->step) {
260       do {
261         if ($cur_step + COMM_N < $user->step) {
262           if (($cur_stat != $user->stat)) {
263             $to_stat = $user->stat;
264             Room::unlock_data($sem);
265             ignore_user_abort(FALSE);
266             log_load("RESYNC");
267             return (page_sync($user->sess, ($to_stat == "table" ? "briskin5/index.php" : "index.php"), $user->table, $user->table_token));
268           }
269           log_rd2("lost history, refresh from scratch");
270           $new_step = -1;
271           break;
272         } 
273         for ($i = $cur_step ; $i < $user->step ; $i++) {
274           log_rd2("ADDED TO THE STREAM: ".$user->comm[$i % COMM_N]);
275           $ret .= $user->comm[$i % COMM_N];
276         }
277         $new_stat =  $user->stat;
278         $new_subst = $user->subst;
279         $new_step =  $user->step;
280       } while (0);
281
282       log_mop($user->step, 'index_rd.php: after ret set');
283
284       if ($user->the_end == TRUE) {
285         log_rd2("LOGOUT BYE BYE!!");
286         log_auth($user->sess, "Explicit logout.");
287
288         $user->reset();
289
290         if ($user->subst == 'sitdown') {
291           log_load("ROOM WAKEUP");
292           $room->room_wakeup(&$user);
293         }
294         else if ($user->subst == 'standup')
295           $room->room_outstandup(&$user);
296         else
297           log_rd2("LOGOUT FROM WHAT ???");
298           
299         Room::save_data($room);
300       }
301     }
302           
303     Room::unlock_data($sem);
304     ignore_user_abort(FALSE);
305   }
306
307   
308   return ($ret);
309 }
310
311 /*
312  *  MAIN
313  */
314
315 /*
316    FROM THE EXTERN 
317    sess
318    stat
319    step
320 */
321
322 $is_page_streaming =  (stristr($HTTP_USER_AGENT, "MSIE") || stristr($HTTP_USER_AGENT, "CHROME") ? TRUE : FALSE);
323
324 header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
325 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
326 header('Content-type: application/xml; charset="utf-8"',true);
327 // header('Content-type: text/plain; charset="utf-8"',true);
328 // header('Content-type: text/html; charset="utf-8"',true);
329
330 if (!isset($myfrom))
331      $myfrom = "";
332 if (!isset($subst))
333      $subst = "";
334 log_rd2("FROM OUTSIDE - STAT: ".$stat." SUBST: ".$subst." STEP: ".$step." MYFROM: ".$myfrom. "IS_PAGE:" . $is_page_streaming);
335
336
337 $endtime = time() + STREAM_TIMEOUT;
338 $old_stat =  $stat;
339 $old_subst = $subst;
340 $old_step =  $ext_step = $step;
341
342 for ($i = 0 ; time() < $endtime ; $i++) {
343   // log_rd("PRE MAIN ".$step);;
344   $pre_main = gettimeofday(TRUE);
345   if (($ret = maincheck($sess, $old_stat, $old_subst, $old_step, &$stat, &$subst, &$step)) != FALSE) {
346     echo '@BEGIN@';
347     // 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));
348     echo "$ret";
349     echo ' @END@'; 
350     log_send("IS_PAGE: ".($is_page_streaming == TRUE ? "TRUE" : "FALSE")."EXT_STEP: ".$ext_step." ENDTIME: [".$endtime."] ".$ret);
351     flush();
352     log_mop(0, 'index_rd.php: after flush (begin: '.sprintf("%f", $pre_main).')');
353     if ($is_page_streaming)
354       break;
355   }
356   $old_stat =  $stat;
357   $old_subst = $subst;
358   $old_step =  $step;
359   // log_rd("POST MAIN ".$step);;
360   usleep(400000);
361   if (($i % 10) == 0) {
362     // log_rd2("TIME: ".time());
363     echo '_';
364     flush();
365   }
366 }
367
368 ?>