5 * Copyright (C) 2006-2007 matteo.nastasi@milug.org
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details. You should have received a
16 * copy of the GNU General Public License along with this program; if
17 * not, write to the Free Software Foundation, Inc, 59 Temple Place -
18 * Suite 330, Boston, MA 02111-1307, USA.
24 require_once("Obj/brisk.phh");
26 log_load($sess, "LOAD: index_rd.php ".$QUERY_STRING);
31 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
32 echo "Debugging time!";
38 log_rd2("SHUTTA!", connection_status());
42 register_shutdown_function(shutta);
46 GLOBAL $is_page_streaming;
48 $is_page_streaming = TRUE;
49 log_rd2("XXX", "UNREC_ERROR");
50 return (sprintf('the_end=true; window.onunload = null; document.location.assign("index.php");'));
53 function page_sync($sess, $page)
55 GLOBAL $is_page_streaming;
57 $is_page_streaming = TRUE;
58 log_rd2($sess, "PAGE_SYNC");
59 return (sprintf('the_end=true; window.onunload = null; document.location.assign("%s");', $page));
65 function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step)
67 GLOBAL $is_page_streaming, $first_loop;
72 // log_rd2($sess, "M");
73 /* Sync check (read only without modifications */
74 ignore_user_abort(TRUE);
75 if (($sem = Room::lock_data()) != FALSE) {
76 // Aggiorna l'expire time lato server
77 if ($first_loop == TRUE) {
79 $room = &Room::load_data();
80 if (($user = &$room->get_user($sess, $idx)) == FALSE) {
81 Room::unlock_data($sem);
82 ignore_user_abort(FALSE);
83 return (unrecerror());
85 log_auth($sess, "update lacc");
88 $room->garbage_manager(FALSE);
90 Room::save_data($room);
95 Room::unlock_data($sem);
96 ignore_user_abort(FALSE);
102 if (($proxy_step = step_get($sess)) != FALSE) {
103 // log_rd2($sess, "Postget".$proxy_step."zizi");
105 if ($cur_step == $proxy_step) {
106 log_only2($sess, "P");
110 log_only2($sess, "R");
114 log_only2($sess, "R");
117 if ($room == FALSE) {
119 ignore_user_abort(TRUE);
120 if (($sem = Room::lock_data()) == FALSE)
123 log_only($sess, "P");
124 if (($room = &Room::load_data()) == FALSE)
129 Room::unlock_data($sem);
131 ignore_user_abort(FALSE);
136 if (($user = &$room->get_user($sess, $idx)) == FALSE) {
137 return (unrecerror());
140 /* Nothing changed, return. */
141 if ($cur_step == $user->step)
144 log_rd2($sess, "do other ++".$cur_stat."++".$user->stat."++".$cur_step."++".$user->step);
146 if ($cur_step == -1) {
147 // FUNZIONE from_scratch DA QUI
148 ignore_user_abort(TRUE);
149 $sem = Room::lock_data();
150 $room = &Room::load_data();
151 if (($user = &$room->get_user($sess, $idx)) == FALSE) {
152 Room::unlock_data($sem);
153 ignore_user_abort(FALSE);
154 return (unrecerror());
157 $is_page_streaming = TRUE;
160 if ($user->trans_step != -1) {
161 log_rd2($sess, "TRANS USATO ".$user->trans_step);
162 $cur_step = $user->trans_step;
163 $user->trans_step = -1;
166 Room::save_data($room);
167 Room::unlock_data($sem);
168 ignore_user_abort(FALSE);
171 log_rd2($sess, "TRANS NON ATTIVATO");
172 Room::unlock_data($sem);
173 ignore_user_abort(FALSE);
177 if ($cur_step == -1) {
178 log_rd2($sess, "PRE-NEWSTAT: ".$user->stat);
180 if ($user->stat == 'room') {
181 log_rd($sess, "roomma");
182 $ret .= show_room(&$room, &$user);
184 /* NOTE the sets went common */
185 $new_stat = $user->stat;
186 $new_subst = $user->subst;
187 $new_step = $user->step;
194 else if ($user->stat == 'table') {
195 /* FIXME we need to decide what do in this case
197 if ($user->subst != "shutdowned" && $user->subst != "shutdowner")
198 $ret = show_table(&$room,&$user,$user->step,FALSE,FALSE);
200 log_rd2($sess, "SENDED TO THE STREAM: ".$ret);
203 $new_stat = $user->stat;
204 $new_subst = $user->subst;
205 $new_step = $user->step;
207 log_rd2($sess, "ALL COMMENTED: ".$ret);
211 log_rd2($sess, "NEWSTAT: ".$user->stat);
215 ignore_user_abort(TRUE);
216 $sem = Room::lock_data();
217 $room = &Room::load_data();
218 if (($user = &$room->get_user($sess, $idx)) == FALSE) {
219 Room::unlock_data($sem);
220 ignore_user_abort(FALSE);
221 return (unrecerror());
223 if ($cur_step < $user->step) {
225 if ($cur_step + COMM_N < $user->step) {
226 if (($cur_stat != $user->stat)) {
227 $to_stat = $user->stat;
228 Room::unlock_data($sem);
229 ignore_user_abort(FALSE);
230 log_load($user->sess, "RESYNC");
231 return (page_sync($user->sess, $to_stat == "table" ? "table.php" : "index.php"));
233 log_rd2($sess, "lost history, refresh from scratch");
237 for ($i = $cur_step ; $i < $user->step ; $i++) {
238 log_rd2($sess, "ADDED TO THE STREAM: ".$user->comm[$i % COMM_N]);
239 $ret .= $user->comm[$i % COMM_N];
241 $new_stat = $user->stat;
242 $new_subst = $user->subst;
243 $new_step = $user->step;
246 if ($user->the_end == TRUE) {
247 log_rd2($sess, "LOGOUT BYE BYE!!");
248 log_auth($user->sess, "Explicit logout.");
249 $tmp_sess = $user->sess;
251 step_unproxy($tmp_sess);
254 $user->the_end = FALSE;
256 if ($user->subst == 'sitdown') {
257 log_load($user->sess, "ROOM WAKEUP");
258 $room->room_wakeup(&$user);
260 else if ($user->subst == 'standup')
261 $room->room_outstandup(&$user);
263 log_rd2($sess, "LOGOUT FROM WHAT ???");
265 Room::save_data($room);
269 Room::unlock_data($sem);
270 ignore_user_abort(FALSE);
288 $is_page_streaming = ((stristr($HTTP_USER_AGENT, "linux") &&
289 (stristr($HTTP_USER_AGENT, "firefox") || stristr($HTTP_USER_AGENT, "iceweasel"))) ? FALSE : TRUE);
292 header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
293 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
299 log_rd2($sess, "FROM OUTSIDE - STAT: ".$stat." SUBST: ".$subst." STEP: ".$step." MYFROM: ".$myfrom. "IS_PAGE:" . $is_page_streaming);
302 $endtime = time() + STREAM_TIMEOUT;
305 $old_step = $ext_step = $step;
307 for ($i = 0 ; time() < $endtime ; $i++) {
308 // log_rd($sess, "PRE MAIN ".$step);;
309 if (($ret = maincheck($sess, $old_stat, $old_subst, $old_step, &$stat, &$subst, &$step)) != FALSE) {
311 // log_rd2($sess, 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));
314 log_send($sess, "EXT_STEP: ".$ext_step." ENDTIME: [".$endtime."] ".$ret);
316 if ($is_page_streaming)
322 // log_rd($sess, "POST MAIN ".$step);;
325 // log_rd2($sess, "TIME: ".time());