473a6fdcfba48b163cf8b63c5e4992f1d72e8add
[brisk.git] / web / index_rd.php
1 <?php
2 /*
3  *  brisk - index_rd.php
4  *
5  *  Copyright (C) 2006-2008 Matteo Nastasi
6  *                          mailto: nastasi@alternativeoutput.it 
7  *                                  matteo.nastasi@milug.org
8  *                          web: http://www.alternativeoutput.it
9  *
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.
14  *
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.
22  *
23  * $Id$
24  *
25  */
26
27 require_once("Obj/brisk.phh");
28 // require_once("Obj/proxyscan.phh");
29 require_once("briskin5/Obj/briskin5.phh");
30
31 // Use of proxies isn't allowed.
32 // if (is_proxy()) {
33 //   sleep(5);
34 //   exit;
35 //}
36 log_load("index_rd.php");
37
38 $first_loop = TRUE;
39 $the_end = FALSE;
40
41 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
42   echo "Debugging time!";
43   exit;
44 }
45
46 function shutta()
47 {
48   log_rd2("SHUTTA!".connection_status());
49 }
50
51
52 register_shutdown_function(shutta);
53
54 function unrecerror()
55 {
56   GLOBAL $is_page_streaming;
57
58   $is_page_streaming = TRUE;
59   log_rd2("UNREC_ERROR:".var_export(debug_backtrace()));
60   return (sprintf('the_end=true; window.onunload = null; window.onbeforeunload = null; document.location.assign("index.php");'));
61 }
62
63 function page_sync($sess, $page, $table_idx, $table_token)
64 {
65   GLOBAL $is_page_streaming;
66
67   log_rd2("page_sync:".var_export(debug_backtrace()));
68
69   $is_page_streaming = TRUE;
70
71   log_rd2("PAGE_SYNC");
72   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));
73 }
74
75
76
77
78 function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step)
79 {
80   GLOBAL $is_page_streaming, $first_loop;
81   
82   $ret = FALSE;
83   $room = FALSE;
84
85   // log_rd2("M");
86   /* Sync check (read only without modifications */
87   ignore_user_abort(TRUE);
88   if (($sem = Room::lock_data()) != FALSE) { 
89     // Aggiorna l'expire time lato server
90     if  ($first_loop == TRUE) {
91       log_only("F");
92       $room = &Room::load_data();
93       if (($user = &$room->get_user($sess, $idx)) == FALSE) {
94         Room::unlock_data($sem);
95         ignore_user_abort(FALSE);
96         return (unrecerror());
97       }
98       log_auth($sess, "update lacc");
99       $user->lacc = time();
100
101       log_main("pre garbage_manager TRE");
102       $room->garbage_manager(FALSE);
103       
104       Room::save_data($room);
105       $first_loop = FALSE;
106     }
107
108     log_lock("U");
109     Room::unlock_data($sem);
110     ignore_user_abort(FALSE);
111   }
112   else {
113     // wait 20 secs, then restart the xhr 
114     ignore_user_abort(FALSE);
115
116     return ("sleep(gst,20000);|xhr_rd_abort(xhr_rd);");
117     /*
118     ignore_user_abort(FALSE);
119     return (FALSE);
120     */
121   }
122     
123   if (($proxy_step = step_get($sess)) != FALSE) {
124     // log_rd2("Postget".$proxy_step."zizi");
125
126     if ($cur_step == $proxy_step) {
127       log_lock("P");
128       return (FALSE);
129     }
130     else {
131       log_only2("R");
132     }
133   }
134   else {
135       log_only2("R");
136   }
137
138   if ($room == FALSE) {
139     do {
140       ignore_user_abort(TRUE);
141       if (($sem = Room::lock_data()) == FALSE) 
142         break;
143       
144       log_lock("P");
145       if (($room = &Room::load_data()) == FALSE) 
146         break;
147     } while (0);
148     
149     if ($sem != FALSE)
150       Room::unlock_data($sem);
151     
152     ignore_user_abort(FALSE);
153     if ($room == FALSE) 
154       return (FALSE);
155   }
156   
157   if (($user = &$room->get_user($sess, $idx)) == FALSE) {
158     return (unrecerror());
159   }
160
161   /* Nothing changed, return. */
162   if ($cur_step == $user->step) 
163     return (FALSE);
164
165   log_rd2("do other ++".$cur_stat."++".$user->stat."++".$cur_step."++".$user->step);
166
167   if ($cur_step == -1) {
168     // FUNZIONE from_scratch DA QUI 
169     ignore_user_abort(TRUE);
170     $sem = Room::lock_data();
171     $room = &Room::load_data();
172     if (($user = &$room->get_user($sess, $idx)) == FALSE) {
173       Room::unlock_data($sem);
174       ignore_user_abort(FALSE);
175       return (unrecerror());
176     }
177     if ($user->the_end) { 
178       log_rd2("main_check: the end".var_export(debug_backtrace()));
179       $is_page_streaming = TRUE;
180     }
181
182     if ($user->trans_step != -1) {
183       log_rd2("TRANS USATO ".$user->trans_step);
184       $cur_step = $user->trans_step;
185       $user->trans_step = -1;
186
187
188       Room::save_data($room);
189       Room::unlock_data($sem);
190       ignore_user_abort(FALSE);
191     }
192     else {
193        log_rd2("TRANS NON ATTIVATO");
194 //        ARRAY_POP DISABLED
195 //        log_rd2("TRANS NON ATTIVATO, clean del comm array");
196 //        while (($el = array_pop($user->comm)) != NULL) { 
197 //          log_rd2("clean element [".$el."]");
198 //        }
199 //        //        $user->step_inc(COMM_N + 1);
200 //        Room::save_data($room);
201 //        //        $new_step = $user->step;
202          
203        Room::unlock_data($sem);
204        ignore_user_abort(FALSE);
205     }
206   }
207       
208   if ($cur_step == -1) {
209     log_rd2("PRE-NEWSTAT: ".$user->stat);
210
211     if ($user->stat == 'room') {
212       log_rd("roomma ".$user->step);
213       $ret .= $room->show_room($user->step, &$user);
214
215       // TODO uncomment and test
216       /* NOTE the sets went common */
217       $new_stat =  $user->stat;
218       $new_subst = $user->subst;
219       $new_step =  $user->step;
220     }
221     /***************
222      *             *
223      *    TABLE    *
224      *             *
225      ***************/
226     else if ($user->stat == 'table') {
227       log_load("RESYNC");
228       return (page_sync($user->sess, "briskin5/index.php", $user->table, $user->table_token));
229     }
230     log_rd2("NEWSTAT: ".$user->stat);
231   }
232   else {
233     ignore_user_abort(TRUE);
234     $sem = Room::lock_data();
235     $room = &Room::load_data();
236     if (($user = &$room->get_user($sess, $idx)) == FALSE) {
237       Room::unlock_data($sem);
238       ignore_user_abort(FALSE);
239       return (unrecerror());
240     }
241     if ($cur_step < $user->step) {
242       do {
243         if ($cur_step + COMM_N < $user->step) {
244           if (($cur_stat != $user->stat)) {
245             $to_stat = $user->stat;
246             Room::unlock_data($sem);
247             ignore_user_abort(FALSE);
248             log_load("RESYNC");
249             return (page_sync($user->sess, ($to_stat == "table" ? "briskin5/index.php" : "index.php"), $user->table, $user->table_token));
250           }
251           log_rd2("lost history, refresh from scratch");
252           $new_step = -1;
253           break;
254         } 
255         for ($i = $cur_step ; $i < $user->step ; $i++) {
256           log_rd2("ADDED TO THE STREAM: ".$user->comm[$i % COMM_N]);
257           $ret .= $user->comm[$i % COMM_N];
258         }
259         $new_stat =  $user->stat;
260         $new_subst = $user->subst;
261         $new_step =  $user->step;
262       } while (0);
263       
264       if ($user->the_end == TRUE) {
265         log_rd2("LOGOUT BYE BYE!!");
266         log_auth($user->sess, "Explicit logout.");
267
268         $user->reset();
269
270         if ($user->subst == 'sitdown') {
271           log_load("ROOM WAKEUP");
272           $room->room_wakeup(&$user);
273         }
274         else if ($user->subst == 'standup')
275           $room->room_outstandup(&$user);
276         else
277           log_rd2("LOGOUT FROM WHAT ???");
278           
279         Room::save_data($room);
280       }
281     }
282           
283     Room::unlock_data($sem);
284     ignore_user_abort(FALSE);
285   }
286
287   
288   return ($ret);
289 }
290
291 /*
292  *  MAIN
293  */
294
295 /*
296    FROM THE EXTERN 
297    sess
298    stat
299    step
300 */
301
302 $is_page_streaming =  ((stristr($HTTP_USER_AGENT, "linux") && 
303                         (stristr($HTTP_USER_AGENT, "firefox") || stristr($HTTP_USER_AGENT, "iceweasel"))) ? FALSE : TRUE);
304
305
306 header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
307 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
308 header('Content-type: application/xml; charset="utf-8"',true);
309 // header('Content-type: text/plain; charset="utf-8"',true);
310 // header('Content-type: text/html; charset="utf-8"',true);
311
312 if (!isset($myfrom))
313      $myfrom = "";
314 if (!isset($subst))
315      $subst = "";
316 log_rd2("FROM OUTSIDE - STAT: ".$stat." SUBST: ".$subst." STEP: ".$step." MYFROM: ".$myfrom. "IS_PAGE:" . $is_page_streaming);
317
318
319 $endtime = time() + STREAM_TIMEOUT;
320 $old_stat =  $stat;
321 $old_subst = $subst;
322 $old_step =  $ext_step = $step;
323
324 for ($i = 0 ; time() < $endtime ; $i++) {
325   // log_rd("PRE MAIN ".$step);;
326   if (($ret = maincheck($sess, $old_stat, $old_subst, $old_step, &$stat, &$subst, &$step)) != FALSE) {
327     echo '@BEGIN@';
328     // 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));
329     echo "$ret";
330     echo ' @END@'; 
331     log_send("IS_PAGE: ".($is_page_streaming == TRUE ? "TRUE" : "FALSE")."EXT_STEP: ".$ext_step." ENDTIME: [".$endtime."] ".$ret);
332     flush();
333     if ($is_page_streaming)
334       break;
335   }
336   $old_stat =  $stat;
337   $old_subst = $subst;
338   $old_step =  $step;
339   // log_rd("POST MAIN ".$step);;
340   usleep(400000);
341   if (($i % 5) == 0) {
342     // log_rd2("TIME: ".time());
343     echo '_';
344     flush();
345   }
346 }
347
348 ?>