first support to return to index.php
[brisk.git] / web / index_rd.php
1 <?php
2 /*
3  *  brisk - index_rd.php
4  *
5  *  Copyright (C) 2006-2007 matteo.nastasi@milug.org
6  *
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.
11  *
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.
19  *
20  * $Id$
21  *
22  */
23
24 require_once("Obj/brisk.phh");
25
26 log_load($sess, "LOAD: index_rd.php ".$QUERY_STRING);
27
28 $first_loop = TRUE;
29 $the_end = FALSE;
30
31 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
32   echo "Debugging time!";
33   exit;
34 }
35
36 function shutta()
37 {
38   log_rd2("SHUTTA!", connection_status());
39 }
40
41
42 register_shutdown_function(shutta);
43
44 function unrecerror()
45 {
46   GLOBAL $is_page_streaming;
47
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");'));
51 }
52
53 function page_sync($sess, $page)
54 {
55   GLOBAL $is_page_streaming;
56
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));
60 }
61
62
63
64
65 function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step)
66 {
67   GLOBAL $is_page_streaming, $first_loop;
68   
69   $ret = FALSE;
70   $room = FALSE;
71
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) {
78       log_only($sess, "F");
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());
84       }
85       log_auth($sess, "update lacc");
86       $user->lacc = time();
87
88       // FIXME uncomment $room->garbage_manager(FALSE);
89       
90       Room::save_data($room);
91       $first_loop = FALSE;
92     }
93
94     log_only($sess, "U");
95     Room::unlock_data($sem);
96     ignore_user_abort(FALSE);
97   }
98   else {
99     return (FALSE);
100   }
101     
102   if (($proxy_step = step_get($sess)) != FALSE) {
103     // log_rd2($sess, "Postget".$proxy_step."zizi");
104
105     if ($cur_step == $proxy_step) {
106       log_only2($sess, "P");
107       return (FALSE);
108     }
109     else {
110       log_only2($sess, "R");
111     }
112   }
113   else {
114       log_only2($sess, "R");
115   }
116
117   if ($room == FALSE) {
118     ignore_user_abort(TRUE);
119     if (($sem = Room::lock_data()) != FALSE) { 
120       log_only($sess, "P");
121       $room = &Room::load_data();
122       Room::unlock_data($sem);
123       ignore_user_abort(FALSE);
124     }
125     else {
126       return (FALSE);
127     }
128   }
129   
130   if (($user = &$room->get_user($sess, $idx)) == FALSE) {
131     return (unrecerror());
132   }
133
134   /* Nothing changed, return. */
135   if ($cur_step == $user->step) 
136     return;
137
138   log_rd2($sess, "do other ++".$cur_stat."++".$user->stat."++".$cur_step."++".$user->step);
139
140   if ($cur_step == -1) {
141     // FUNZIONE from_scratch DA QUI 
142     ignore_user_abort(TRUE);
143     $sem = Room::lock_data();
144     $room = &Room::load_data();
145     if (($user = &$room->get_user($sess, $idx)) == FALSE) {
146       Room::unlock_data($sem);
147       ignore_user_abort(FALSE);
148       return (unrecerror());
149     }
150     if ($user->the_end) 
151       $is_page_streaming = TRUE;
152
153
154     if ($user->trans_step != -1) {
155       log_rd2($sess, "TRANS USATO ".$user->trans_step);
156       $cur_step = $user->trans_step;
157       $user->trans_step = -1;
158
159
160       Room::save_data($room);
161       Room::unlock_data($sem);
162       ignore_user_abort(FALSE);
163     }
164     else {
165       log_rd2($sess, "TRANS NON ATTIVATO");
166       Room::unlock_data($sem);
167       ignore_user_abort(FALSE);
168     }
169   }
170       
171   if ($cur_step == -1) {
172     log_rd2($sess, "PRE-NEWSTAT.");
173
174     if ($user->stat == 'room') {
175       log_rd($sess, "roomma");
176       $ret .= show_room(&$room, &$user);
177
178       /* NOTE the sets went common */
179       $new_stat =  $user->stat;
180       $new_subst = $user->subst;
181       $new_step =  $user->step;
182     }
183     /***************
184      *             *
185      *    TABLE    *
186      *             *
187      ***************/
188     else if ($user->stat == 'table') {      
189       /* FIXME we need to decide what do in this case 
190
191       if ($user->subst != "shutdowned" && $user->subst != "shutdowner")
192         $ret = show_table(&$room,&$user,$user->step,FALSE,FALSE);
193
194       log_rd2($sess, "SENDED TO THE STREAM: ".$ret);
195
196
197       $new_stat =  $user->stat;
198       $new_subst = $user->subst;
199       $new_step =  $user->step;
200       */
201     }
202     log_rd2($sess, "NEWSTAT: ".$user->stat);
203
204   }
205   else {
206     ignore_user_abort(TRUE);
207     $sem = Room::lock_data();
208     $room = &Room::load_data();
209     if (($user = &$room->get_user($sess, $idx)) == FALSE) {
210       Room::unlock_data($sem);
211       ignore_user_abort(FALSE);
212       return (unrecerror());
213     }
214     if ($cur_step < $user->step) {
215       do {
216         if ($cur_step + COMM_N < $user->step) {
217           if (($cur_stat != $user->stat)) {
218             $to_stat = $user->stat;
219             Room::unlock_data($sem);
220             ignore_user_abort(FALSE);
221             log_load($user->sess, "RESYNC");
222             return (page_sync($user->sess, $to_stat == "table" ? "table.php" : "index.php"));
223           }
224           log_rd2($sess, "lost history, refresh from scratch");
225           $new_step = -1;
226           break;
227         } 
228         for ($i = $cur_step ; $i < $user->step ; $i++) {
229           log_rd2($sess, "ADDED TO THE STREAM: ".$user->comm[$i % COMM_N]);
230           $ret .= $user->comm[$i % COMM_N];
231         }
232         $new_stat =  $user->stat;
233         $new_subst = $user->subst;
234         $new_step =  $user->step;
235       } while (0);
236       
237       if ($user->the_end == TRUE) {
238         log_rd2($sess, "LOGOUT BYE BYE!!");
239         log_auth($user->sess, "Explicit logout.");
240         $tmp_sess = $user->sess;
241         $user->sess = "";
242         step_unproxy($tmp_sess);
243         
244         $user->name = "";
245         $user->the_end = FALSE;
246         
247         if ($user->subst == 'sitdown')
248           $room->room_wakeup(&$user);
249         else if ($user->subst == 'standup')
250           $room->room_outstandup(&$user);
251         else
252           log_rd2($sess, "LOGOUT FROM WHAT ???");
253           
254         Room::save_data($room);
255       }
256     }
257           
258     Room::unlock_data($sem);
259     ignore_user_abort(FALSE);
260   }
261
262   
263   return ($ret);
264 }
265
266 /*
267  *  MAIN
268  */
269
270 /*
271    FROM THE EXTERN 
272    sess
273    stat
274    step
275 */
276
277 $is_page_streaming =  ((stristr($HTTP_USER_AGENT, "linux") && 
278                         stristr($HTTP_USER_AGENT, "firefox")) ? FALSE : TRUE);
279
280
281 header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
282 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
283
284 if (!isset($myfrom))
285      $myfrom = "";
286 if (!isset($subst))
287      $subst = "";
288 log_rd2($sess, "FROM OUTSIDE - STAT: ".$stat." SUBST: ".$subst." STEP: ".$step." MYFROM: ".$myfrom. "IS_PAGE:" . $is_page_streaming);
289
290
291 $endtime = time() + STREAM_TIMEOUT;
292 $old_stat =  $stat;
293 $old_subst = $subst;
294 $old_step =  $ext_step = $step;
295
296 for ($i = 0 ; time() < $endtime ; $i++) {
297   // log_rd($sess, "PRE MAIN ".$step);;
298   if (($ret = maincheck($sess, $old_stat, $old_subst, $old_step, &$stat, &$subst, &$step)) != FALSE) {
299     echo '@BEGIN@';
300     // 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));
301     echo "$ret";
302     echo ' @END@'; 
303     log_send($sess, "EXT_STEP: ".$ext_step." ENDTIME: [".$endtime."] ".$ret);
304     flush();
305     if ($is_page_streaming)
306       break;
307   }
308   $old_stat =  $stat;
309   $old_subst = $subst;
310   $old_step =  $step;
311   // log_rd($sess, "POST MAIN ".$step);;
312   usleep(400000);
313   if (($i % 5) == 0) {
314     // log_rd2($sess, "TIME: ".time());
315     echo '_';
316     flush();
317   }
318 }
319
320 ?>