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");
31 $S_load_stat = array( 'U_first_loop' => 0,
38 'btn_backtotab' => array('it' => ' torna ai tavoli ',
39 'en' => ' back to tables '),
40 'btn_btotabsup' => array('it' => ' grazie della donazione, torna ai tavoli ',
41 'en' => ' thank you for donation, back to tables ')
44 // Use of proxies isn't allowed.
49 log_load("index_rd.php");
54 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
55 echo "Debugging time!";
61 log_rd2("SHUTTA!".connection_status());
65 register_shutdown_function(shutta);
69 GLOBAL $is_page_streaming;
71 $is_page_streaming = TRUE;
72 log_rd2("UNREC_ERROR:".var_export(debug_backtrace()));
73 return (sprintf('the_end=true; window.onunload = null; window.onbeforeunload = null; document.location.assign("index.php");'));
76 function page_sync($sess, $page, $table_idx, $table_token)
78 GLOBAL $is_page_streaming;
80 log_rd2("page_sync:".var_export(debug_backtrace()));
82 $is_page_streaming = TRUE;
85 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));
91 function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step)
93 GLOBAL $G_lang, $mlang_indrd, $is_page_streaming, $first_loop;
94 GLOBAL $G_with_splash, $G_splash_content, $G_splash_interval, $G_splash_idx;
95 GLOBAL $G_splash_w, $G_splash_h, $G_splash_timeout;
96 $CO_splashdate = "CO_splashdate".$G_splash_idx;
97 GLOBAL $$CO_splashdate;
101 log_rd("maincheck begin");
108 if (($proxy_step = User::step_get($sess)) == FALSE) {
114 /* Sync check (read only without modifications */
115 ignore_user_abort(TRUE);
116 if ($first_loop == TRUE) {
117 if (($sem = Room::lock_data()) != FALSE) {
118 // Aggiorna l'expire time lato server
119 $S_load_stat['U_first_loop']++;
120 if (($user = User::load_data($proxy_step['i'], $sess)) == FALSE) {
122 ignore_user_abort(FALSE);
123 return (unrecerror());
125 $user->lacc = $curtime;
126 User::save_data($user, $user->idx);
128 if (Room::garbage_time_is_expired($curtime)) {
131 $S_load_stat['R_garbage']++;
132 if (($room = Room::load_data()) == FALSE) {
134 ignore_user_abort(FALSE);
135 return (unrecerror());
137 log_main("pre garbage_manager TRE");
138 $room->garbage_manager(FALSE);
139 Room::save_data($room);
143 Room::unlock_data($sem);
144 ignore_user_abort(FALSE);
145 } // if (($sem = Room::lock_data()) != FALSE) {
147 // wait 20 secs, then restart the xhr
148 ignore_user_abort(FALSE);
150 return ("sleep(gst,20000);|xhr_rd_abort(xhr_rd);");
153 } // if ($first_loop == TRUE) {
155 if ($cur_step == $proxy_step['s']) {
164 if ($user == FALSE) {
166 ignore_user_abort(TRUE);
167 if (($sem = Room::lock_data()) == FALSE)
171 $S_load_stat['U_heavy']++;
172 if (($user = User::load_data($proxy_step['i'], $sess)) == FALSE) {
178 Room::unlock_data($sem);
180 ignore_user_abort(FALSE);
181 if ($user == FALSE) {
182 return (unrecerror());
186 /* Nothing changed, return. */
187 if ($cur_step == $user->step)
190 log_rd2("do other cur_stat[".$cur_stat."] user->stat[".$user->stat."] cur_step[".$cur_step."] user_step[".$user->step."]");
192 if ($cur_step == -1) {
194 * if $cur_step == -1 load the current state from the main struct
196 ignore_user_abort(TRUE);
197 $sem = Room::lock_data();
198 $room = Room::load_data();
199 $S_load_stat['R_minusone']++;
201 /* unset the $user var to reload it from main structure */
203 if (($user = $room->get_user($sess, $idx)) == FALSE) {
204 Room::unlock_data($sem);
205 ignore_user_abort(FALSE);
206 return (unrecerror());
209 if ($user->the_end) {
210 log_rd2("main_check: the end".var_export(debug_backtrace()));
211 $is_page_streaming = TRUE;
214 if ($user->trans_step != -1) {
215 log_rd2("TRANS USATO ".$user->trans_step);
216 $cur_step = $user->trans_step;
217 $user->trans_step = -1;
219 Room::save_data($room);
220 Room::unlock_data($sem);
221 ignore_user_abort(FALSE);
224 log_rd2("TRANS NON ATTIVATO");
225 // ARRAY_POP DISABLED
226 // log_rd2("TRANS NON ATTIVATO, clean del comm array");
227 // while (($el = array_pop($user->comm)) != NULL) {
228 // log_rd2("clean element [".$el."]");
230 // // $user->step_inc(COMM_N + 1);
231 // Room::save_data($room);
232 // // $new_step = $user->step;
234 Room::unlock_data($sem);
235 ignore_user_abort(FALSE);
240 /* this part I suppose is read only on $room structure */
241 if ($cur_step == -1) {
242 log_rd2("PRE-NEWSTAT: ".$user->stat);
244 if ($user->stat == 'room') {
245 log_rd("roomma ".$user->step);
248 if ($G_with_splash &&
249 ($$CO_splashdate < $curtime - $G_splash_interval ||
250 $$CO_splashdate > $curtime)) {
251 $is_super = $user->flags & USER_FLAG_TY_SUPER;
252 $ret .= show_notify_ex(str_replace("\n", " ", $G_splash_content[$G_lang]),
253 ($is_super ? 0 : $G_splash_timeout),
254 $mlang_indrd[($is_super ? 'btn_btotabsup' : 'btn_backtotab')][$G_lang],
255 $G_splash_w, $G_splash_h, true,
256 ($is_super ? 0 : $G_splash_timeout));
257 $ret .= sprintf('|createCookie("CO_splashdate%d", %d, 24*365, cookiepath);', $G_splash_idx, $curtime);
259 $ret .= $room->show_room($user->step, $user);
261 // TODO uncomment and test
262 /* NOTE the sets went common */
263 $new_stat = $user->stat;
264 $new_subst = $user->subst;
265 $new_step = $user->step;
272 else if ($user->stat == 'table') {
274 return (page_sync($user->sess, "briskin5/index.php", $user->table, $user->table_token));
276 log_rd2("NEWSTAT: ".$user->stat);
279 // TODO: verify if we can use only $user struct
280 ignore_user_abort(TRUE);
281 $sem = Room::lock_data();
282 $S_load_stat['U_heavy']++;
283 if (($user = User::load_data($proxy_step['i'], $sess)) == FALSE) {
285 ignore_user_abort(FALSE);
286 return (unrecerror());
289 if ($cur_step < $user->step) {
291 if ($cur_step + COMM_N < $user->step) {
292 if (($cur_stat != $user->stat)) {
293 $to_stat = $user->stat;
294 Room::unlock_data($sem);
295 ignore_user_abort(FALSE);
297 return (page_sync($user->sess, ($to_stat == "table" ? "briskin5/index.php" : "index.php"), $user->table, $user->table_token));
299 log_rd2("lost history, refresh from scratch");
303 for ($i = $cur_step ; $i < $user->step ; $i++) {
305 log_rd2("ADDED TO THE STREAM: ".$user->comm[$ii]);
306 $ret .= $user->comm[$ii];
308 $new_stat = $user->stat;
309 $new_subst = $user->subst;
310 $new_step = $user->step;
313 log_mop($user->step, 'index_rd.php: after ret set');
315 if ($user->the_end == TRUE) {
316 log_rd2("LOGOUT BYE BYE!!");
317 log_auth($user->sess, "Explicit logout.");
319 $S_load_stat['R_the_end']++;
320 $room = Room::load_data();
322 if (($user = $room->get_user($sess, $idx)) == FALSE) {
323 Room::unlock_data($sem);
324 ignore_user_abort(FALSE);
325 return (unrecerror());
329 if ($user->subst == 'sitdown') {
330 log_load("ROOM WAKEUP");
331 $room->room_wakeup($user);
333 else if ($user->subst == 'standup')
334 $room->room_outstandup($user);
336 log_rd2("LOGOUT FROM WHAT ???");
338 Room::save_data($room);
342 Room::unlock_data($sem);
343 ignore_user_abort(FALSE);
361 $is_page_streaming = (stristr($HTTP_USER_AGENT, "MSIE") || stristr($HTTP_USER_AGENT, "CHROME") ? TRUE : FALSE);
363 header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
364 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
365 header('Content-type: application/xml; charset="utf-8"',true);
371 log_rd2("FROM OUTSIDE - STAT: ".$stat." SUBST: ".$subst." STEP: ".$step." MYFROM: ".$myfrom. "IS_PAGE:" . $is_page_streaming);
374 $endtime = time() + STREAM_TIMEOUT;
377 $old_step = $ext_step = $step;
379 for ($i = 0 ; time() < $endtime ; $i++) {
380 // log_rd("PRE MAIN ".$step);;
381 $pre_main = gettimeofday(TRUE);
382 if (($ret = maincheck($sess, $old_stat, $old_subst, $old_step, &$stat, &$subst, &$step)) != FALSE) {
384 // 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));
387 log_send("IS_PAGE: ".($is_page_streaming == TRUE ? "TRUE" : "FALSE")."EXT_STEP: ".$ext_step." ENDTIME: [".$endtime."] ".$ret);
389 log_mop(0, 'index_rd.php: after flush (begin: '.sprintf("%f", $pre_main).')');
390 if ($is_page_streaming)
396 // log_rd("POST MAIN ".$step);;
398 if (($i % 10) == 0) {
399 // log_rd2("TIME: ".time());
405 $s = "[".$sess."] index_rd.php stats: ";
406 foreach ($S_load_stat as $key => $value) {
407 $s .= sprintf("%s: %d - ", $key, $value);