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.
25 require_once("Obj/brisk.phh");
26 // require_once("Obj/proxyscan.phh");
27 require_once("briskin5/Obj/briskin5.phh");
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 ')
37 // Use of proxies isn't allowed.
42 log_load("index_rd.php");
47 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
48 echo "Debugging time!";
54 log_rd2("SHUTTA!".connection_status());
58 register_shutdown_function(shutta);
62 GLOBAL $is_page_streaming;
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");'));
69 function page_sync($sess, $page, $table_idx, $table_token)
71 GLOBAL $is_page_streaming;
73 log_rd2("page_sync:".var_export(debug_backtrace()));
75 $is_page_streaming = TRUE;
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));
84 function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step)
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;
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) {
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());
108 log_auth($sess, "update lacc");
109 $user->lacc = time();
111 log_main("pre garbage_manager TRE");
112 $room->garbage_manager(FALSE);
114 Room::save_data($room);
119 Room::unlock_data($sem);
120 ignore_user_abort(FALSE);
123 // wait 20 secs, then restart the xhr
124 ignore_user_abort(FALSE);
126 return ("sleep(gst,20000);|xhr_rd_abort(xhr_rd);");
128 ignore_user_abort(FALSE);
133 if (($proxy_step = step_get($sess)) != FALSE) {
134 // log_rd2("Postget".$proxy_step."zizi");
136 if ($cur_step == $proxy_step) {
148 if ($room == FALSE) {
150 ignore_user_abort(TRUE);
151 if (($sem = Room::lock_data()) == FALSE)
155 if (($room = &Room::load_data()) == FALSE)
160 Room::unlock_data($sem);
162 ignore_user_abort(FALSE);
167 if (($user = &$room->get_user($sess, $idx)) == FALSE) {
168 return (unrecerror());
171 /* Nothing changed, return. */
172 if ($cur_step == $user->step)
175 log_rd2("do other ++".$cur_stat."++".$user->stat."++".$cur_step."++".$user->step);
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());
187 if ($user->the_end) {
188 log_rd2("main_check: the end".var_export(debug_backtrace()));
189 $is_page_streaming = TRUE;
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;
198 Room::save_data($room);
199 Room::unlock_data($sem);
200 ignore_user_abort(FALSE);
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."]");
209 // // $user->step_inc(COMM_N + 1);
210 // Room::save_data($room);
211 // // $new_step = $user->step;
213 Room::unlock_data($sem);
214 ignore_user_abort(FALSE);
218 if ($cur_step == -1) {
219 log_rd2("PRE-NEWSTAT: ".$user->stat);
221 if ($user->stat == 'room') {
222 log_rd("roomma ".$user->step);
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);
236 $ret .= $room->show_room($user->step, &$user);
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;
249 else if ($user->stat == 'table') {
251 return (page_sync($user->sess, "briskin5/index.php", $user->table, $user->table_token));
253 log_rd2("NEWSTAT: ".$user->stat);
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());
264 if ($cur_step < $user->step) {
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);
272 return (page_sync($user->sess, ($to_stat == "table" ? "briskin5/index.php" : "index.php"), $user->table, $user->table_token));
274 log_rd2("lost history, refresh from scratch");
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];
282 $new_stat = $user->stat;
283 $new_subst = $user->subst;
284 $new_step = $user->step;
287 log_mop($user->step, 'index_rd.php: after ret set');
289 if ($user->the_end == TRUE) {
290 log_rd2("LOGOUT BYE BYE!!");
291 log_auth($user->sess, "Explicit logout.");
295 if ($user->subst == 'sitdown') {
296 log_load("ROOM WAKEUP");
297 $room->room_wakeup(&$user);
299 else if ($user->subst == 'standup')
300 $room->room_outstandup(&$user);
302 log_rd2("LOGOUT FROM WHAT ???");
304 Room::save_data($room);
308 Room::unlock_data($sem);
309 ignore_user_abort(FALSE);
327 $is_page_streaming = (stristr($HTTP_USER_AGENT, "MSIE") || stristr($HTTP_USER_AGENT, "CHROME") ? TRUE : FALSE);
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);
339 log_rd2("FROM OUTSIDE - STAT: ".$stat." SUBST: ".$subst." STEP: ".$step." MYFROM: ".$myfrom. "IS_PAGE:" . $is_page_streaming);
342 $endtime = time() + STREAM_TIMEOUT;
345 $old_step = $ext_step = $step;
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) {
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));
355 log_send("IS_PAGE: ".($is_page_streaming == TRUE ? "TRUE" : "FALSE")."EXT_STEP: ".$ext_step." ENDTIME: [".$endtime."] ".$ret);
357 log_mop(0, 'index_rd.php: after flush (begin: '.sprintf("%f", $pre_main).')');
358 if ($is_page_streaming)
364 // log_rd("POST MAIN ".$step);;
366 if (($i % 10) == 0) {
367 // log_rd2("TIME: ".time());