5 * Copyright (C) 2006-2011 Matteo Nastasi
6 * mailto: nastasi@alternativeoutput.it
7 * matteo.nastasi@milug.org
8 * web: http://www.alternativeoutput.it
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.
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.
27 require_once("Obj/brisk.phh");
28 // require_once("Obj/proxyscan.phh");
29 require_once("briskin5/Obj/briskin5.phh");
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 ')
39 // Use of proxies isn't allowed.
44 log_load("index_rd.php");
49 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
50 echo "Debugging time!";
56 log_rd2("SHUTTA!".connection_status());
60 register_shutdown_function(shutta);
64 GLOBAL $is_page_streaming;
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");'));
71 function page_sync($sess, $page, $table_idx, $table_token)
73 GLOBAL $is_page_streaming;
75 log_rd2("page_sync:".var_export(debug_backtrace()));
77 $is_page_streaming = TRUE;
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));
86 function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step)
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;
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) {
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());
110 log_auth($sess, "update lacc");
111 $user->lacc = time();
113 log_main("pre garbage_manager TRE");
114 $room->garbage_manager(FALSE);
116 Room::save_data($room);
121 Room::unlock_data($sem);
122 ignore_user_abort(FALSE);
125 // wait 20 secs, then restart the xhr
126 ignore_user_abort(FALSE);
128 return ("sleep(gst,20000);|xhr_rd_abort(xhr_rd);");
130 ignore_user_abort(FALSE);
135 if (($proxy_step = step_get($sess)) != FALSE) {
136 // log_rd2("Postget".$proxy_step."zizi");
138 if ($cur_step == $proxy_step) {
150 if ($room == FALSE) {
152 ignore_user_abort(TRUE);
153 if (($sem = Room::lock_data()) == FALSE)
157 if (($room = &Room::load_data()) == FALSE)
162 Room::unlock_data($sem);
164 ignore_user_abort(FALSE);
169 if (($user = &$room->get_user($sess, $idx)) == FALSE) {
170 return (unrecerror());
173 /* Nothing changed, return. */
174 if ($cur_step == $user->step)
177 log_rd2("do other ++".$cur_stat."++".$user->stat."++".$cur_step."++".$user->step);
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());
189 if ($user->the_end) {
190 log_rd2("main_check: the end".var_export(debug_backtrace()));
191 $is_page_streaming = TRUE;
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;
200 Room::save_data($room);
201 Room::unlock_data($sem);
202 ignore_user_abort(FALSE);
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."]");
211 // // $user->step_inc(COMM_N + 1);
212 // Room::save_data($room);
213 // // $new_step = $user->step;
215 Room::unlock_data($sem);
216 ignore_user_abort(FALSE);
220 if ($cur_step == -1) {
221 log_rd2("PRE-NEWSTAT: ".$user->stat);
223 if ($user->stat == 'room') {
224 log_rd("roomma ".$user->step);
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);
238 $ret .= $room->show_room($user->step, &$user);
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;
251 else if ($user->stat == 'table') {
253 return (page_sync($user->sess, "briskin5/index.php", $user->table, $user->table_token));
255 log_rd2("NEWSTAT: ".$user->stat);
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());
266 if ($cur_step < $user->step) {
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);
274 return (page_sync($user->sess, ($to_stat == "table" ? "briskin5/index.php" : "index.php"), $user->table, $user->table_token));
276 log_rd2("lost history, refresh from scratch");
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];
284 $new_stat = $user->stat;
285 $new_subst = $user->subst;
286 $new_step = $user->step;
289 log_mop($user->step, 'index_rd.php: after ret set');
291 if ($user->the_end == TRUE) {
292 log_rd2("LOGOUT BYE BYE!!");
293 log_auth($user->sess, "Explicit logout.");
297 if ($user->subst == 'sitdown') {
298 log_load("ROOM WAKEUP");
299 $room->room_wakeup(&$user);
301 else if ($user->subst == 'standup')
302 $room->room_outstandup(&$user);
304 log_rd2("LOGOUT FROM WHAT ???");
306 Room::save_data($room);
310 Room::unlock_data($sem);
311 ignore_user_abort(FALSE);
329 $is_page_streaming = (stristr($HTTP_USER_AGENT, "MSIE") || stristr($HTTP_USER_AGENT, "CHROME") ? TRUE : FALSE);
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);
341 log_rd2("FROM OUTSIDE - STAT: ".$stat." SUBST: ".$subst." STEP: ".$step." MYFROM: ".$myfrom. "IS_PAGE:" . $is_page_streaming);
344 $endtime = time() + STREAM_TIMEOUT;
347 $old_step = $ext_step = $step;
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) {
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));
357 log_send("IS_PAGE: ".($is_page_streaming == TRUE ? "TRUE" : "FALSE")."EXT_STEP: ".$ext_step." ENDTIME: [".$endtime."] ".$ret);
359 log_mop(0, 'index_rd.php: after flush (begin: '.sprintf("%f", $pre_main).')');
360 if ($is_page_streaming)
366 // log_rd("POST MAIN ".$step);;
368 if (($i % 10) == 0) {
369 // log_rd2("TIME: ".time());