e246dd884072e0f496f09b229aa35882d27786cb
[brisk.git] / index_rd_ifra.php
1 <?php
2 /*
3  *  brisk - index_rd.php
4  *
5  *  Copyright (C) 2006-2011 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  */
24
25 // require_once("Obj/brisk.phh");
26 // require_once("Obj/proxyscan.phh");
27 // require_once("briskin5/Obj/briskin5.phh");
28
29 $G_four_rnd_string = 
30  "GGZDGDRBXDWFXHGPYAAVDXEKFDUWSTEDTDVXBXQPMZAIFPNLRPTWMJAZRHXTIRNZ"
31 ."HTZXAUVJDCLXENDCLIZXSSEQQGUADYEKNNANJWEGRUFHPLUNSEROZQJSLMUYNUVG"
32 ."FPLQAGCMTTGIRRXZFBFQFTRBYPHTWFSXFKHKYBRDAOSOURCMEVGPVXJVUQAOCFPV"
33 ."OQSUBOYWOZLDGRPQQIBNOTHFUHTCRRZOBCUONAXIYUYCJBFTVNWIITVHBYLNMPWR"
34 ."LPMMIROZVRUNYXSJSMJINSXGACXDMJAEPNFFAERHVDJELXSPPJWKMNFGVGAPTIMR"
35 ."VMVMVCLWKMPOAJWZGPSPFEXVNQANXGLZIQSCBIYVFSTXZOODZYCXUVUZFCLFGHUJ"
36 ."TXZWFHSMFJQAZOQRANKZJAUJHFJIKHIIEQUSDXUBRPQBKMIAVTWYQEOIMYKKDTEV"
37 ."EYERPXKF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SUSHHTVYLFMLHFGKJNPYIYEMLBAZMLIDDSWRCVFOUSGC"
83 ."TUQHWTUHRJUZHYQOKTWTLRVOSWRPSYBOBSZHZLRZEIZKKCMLNYICCUTIXNXETTKL"
84 ."PYIFTUDCMWRQBZDROXTTVTAOOIQHIWOZESRHKPNGNCSHNAVEWUSGOTZCRNFSIXGF"
85 ."QJUMUFPEZBFPOVGZZDCGDKEODSWAPNDNHVJUQDYLCXWNXSCDFFFPBTDOIZIZLBBP"
86 ."ZXZYWTTPEUUCKXGBTXAJBAXYLMUVWWGWZYTXTKYVRXSOXOBHERLZJPRDXIQZRQLU"
87 ."TXCLIJNKIRWMVQEJTLHZIDTNGBOYSULGWUTSYTKGNMGZFWKXCCZXGOQGZNQFZJUK"
88 ."JMBQPQPZOPYTQMPEFTZUDLIIBZAZSZAPNILMIKMPKXJIRHRQMFSTJKAPIIQWOFYQ"
89 ."HGANJGJSUGDIENDVEQFEMUJBGMZBPHLOARCVGMUUCHLCDDNKSJSCKYSALUAGUZFN"
90 ."YVURVLREBESGXPANIDKGLPJZGXLKFKTPEQBQEINOYYZTBMPFBLBKIKVPAPFDKKRZ"
91 ."YMVUBYCYTRXCIICUPTXKGQOIJSVSWPYVALIROWGVXDXGNSRVZOLEJHNRSUKHNTRJ"
92 ."LQZQKBEFXYKIPXJRZMQMJKUGCNUKBZBWABPLHMAJJZVBCWOQYCLSGDUKJSTHTVXV"
93 ."OUGJVADGJWGVNBNXYRZDSNLOZDIQRKOQAKJJDKXRMMTXRVTLOMTETNROPDYNXAGR";
94
95 $S_load_stat = array( 'rU_heavy'      => 0,
96                       'lL_laccgarb'   => 0,
97                       'wU_lacc_upd'   => 0,
98                       'wR_garbage'    => 0,
99                       'wR_minusone'   => 0,
100                       'wR_the_end'    => 0 );
101
102 $mlang_indrd = array( 
103                      'btn_backtotab'  => array('it' => ' torna ai tavoli ',
104                                                'en' => ' back to tables '),
105                      'btn_btotabsup'  => array('it' => ' grazie della donazione, torna ai tavoli ',
106                                                'en' => ' thank you for donation, back to tables ') 
107                      );
108
109 // Use of proxies isn't allowed.
110 // if (is_proxy()) {
111 //   sleep(5);
112 //   exit;
113 //}
114 log_load("index_rd.php");
115
116 // $first_loop = TRUE;
117 $the_end = FALSE;
118
119 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
120   echo "Debugging time!";
121   exit;
122 }
123
124 function blocking_error($is_unrecoverable)
125 {
126   GLOBAL $is_page_streaming;
127
128   $is_page_streaming = TRUE;
129   log_crit("BLOCKING_ERROR UNREC: ".($is_unrecoverable ? "TRUE" : "FALSE"));
130   return (sprintf(($is_unrecoverable ? 'hstm.stop(); ' : '').'window.onbeforeunload = null; window.onunload = null; document.location.assign("index.php");'));
131 }
132
133 // FIXME TO SUPPORT iframe
134 function page_sync($sess, $page, $table_idx, $table_token)
135 {
136   GLOBAL $is_page_streaming;
137
138   log_rd2("page_sync:".var_export(debug_backtrace()));
139
140   $is_page_streaming = TRUE;
141
142   log_rd2("PAGE_SYNC");
143   return (sprintf('createCookie("table_idx", %d, 24*365, cookiepath); createCookie("table_token", "%s", 24*365, cookiepath); hstm.stop(); window.onunload = null; window.onbeforeunload = null; document.location.assign("%s");', $table_idx, $table_token, $page));
144 }
145
146
147
148
149 function maincheck(&$user, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step, $splashdate)
150 {
151     GLOBAL $G_lang, $mlang_indrd, $is_page_streaming;
152     // GLOBAL $first_loop;
153     GLOBAL $G_with_splash, $G_splash_content, $G_splash_interval, $G_splash_idx;
154     GLOBAL $G_splash_w, $G_splash_h, $G_splash_timeout;
155     $CO_splashdate = "CO_splashdate".$G_splash_idx;
156     $$CO_splashdate = $splashdate;
157     
158     GLOBAL $S_load_stat;
159
160     log_rd("maincheck begin");
161
162     $ret = FALSE;
163     // $user->room = FALSE;
164     // $user = FALSE;
165     $curtime = time();
166     
167     // NOTE: qui forse si potrebbe fallback-are a una User::load_data 
168     //       anche se non ce ne dovrebbe essere mai la necessità
169     /* if (($proxy_step = User::load_step($sess)) == FALSE) { */
170     /*     log_only2("R"); */
171     /*     ignore_user_abort(FALSE); */
172     /*     return (blocking_error(TRUE)); */
173     /* } */
174     
175     // log_rd2("M");
176     /* Sync check (read only without modifications */
177     ignore_user_abort(TRUE);
178
179
180     /* /\* shared locking to load info *\/ */
181     /* if (($sem = Room::lock_data(FALSE)) == FALSE) {  */
182     /*     // wait 20 secs, then restart the xhr  */
183     /*     ignore_user_abort(FALSE); */
184     /*     return ("sleep(gst,20000);|hstm.xhr_abort();"); */
185     /* } */
186     
187     /* // Verifica l'expire time lato server */
188     /* if (($user = User::load_data($proxy_step['i'], $sess)) == FALSE) { */
189     /*     Room::unlock_data($sem); */
190     /*     ignore_user_abort(FALSE); */
191     /*     return (blocking_error(TRUE)); */
192     /* } */
193     /* if lacc time great than STREAM_TIMEOUT or the room garbage_time is expired 
194          switch to exclusive locking and verify again the conditions */
195     if ((($curtime - $user->lacc) >  STREAM_TIMEOUT) || Room::garbage_time_is_expired($curtime)) {
196         /* there is some info that require to change data, switch to exclusive locking */
197         // Room::unlock_data($sem);
198         /* if (($sem = Room::lock_data(TRUE)) == FALSE) {  */
199         /*     // wait 20 secs, then restart the xhr  */
200         /*     ignore_user_abort(FALSE); */
201         /*     return ("sleep(gst,20000);|hstm.xhr_abort();"); */
202         /* } */
203         $S_load_stat['lL_laccgarb']++;
204
205         // load again the data after locking
206         // unset($user);
207         // Verifica l'expire time lato server
208         /* if (($user = User::load_data($proxy_step['i'], $sess)) == FALSE) { */
209         /*     Room::unlock_data($sem); */
210         /*     ignore_user_abort(FALSE); */
211         /*     return (blocking_error(TRUE)); */
212         /* } */
213
214         if (($curtime - $user->lacc) >=  STREAM_TIMEOUT) {
215             $S_load_stat['wU_lacc_upd']++;
216             $user->lacc = $curtime;
217             // lacc field updated
218             /* User::save_data($user, $user->idx); */
219         }
220         
221         if (Room::garbage_time_is_expired($curtime)) {
222             log_only("F");
223             
224             $S_load_stat['wR_garbage']++;
225             /* if (($user->room = Room::load_data()) == FALSE) { */
226             /*     Room::unlock_data($sem); */
227             /*     ignore_user_abort(FALSE); */
228             /*     return (blocking_error(TRUE)); */
229             /* } */
230             log_main("pre garbage_manager TRE");
231             $user->room->garbage_manager(FALSE);
232             /* Room::save_data($user->room); */
233             /* unset($user->room); */
234         }
235     }
236     log_main("infolock: U");
237     /* Room::unlock_data($sem); */
238     ignore_user_abort(FALSE);
239
240     
241     
242 //     if  ($first_loop == TRUE) {
243 //         if (($sem = Room::lock_data(TRUE)) != FALSE) { 
244 //             // Aggiorna l'expire time lato server
245 //             $S_load_stat['U_first_loop']++;
246 //             if (($user = User::load_data($proxy_step['i'], $sess)) == FALSE) {
247 //                 Room::unlock_data($sem);
248 //                 ignore_user_abort(FALSE);
249 //                 return (blocking_error(TRUE));
250 //             }
251 //             $user->lacc = $curtime;
252 //             // lacc field updated
253 //             User::save_data($user, $user->idx);
254             
255 //             if (Room::garbage_time_is_expired($curtime)) {
256 //                 log_only("F");
257                 
258 //                 $S_load_stat['R_garbage']++;
259 //                 if (($user->room = Room::load_data()) == FALSE) {
260 //                     Room::unlock_data($sem);
261 //                     ignore_user_abort(FALSE);
262 //                     return (blocking_error(TRUE));
263 //                 }
264 //                 log_main("pre garbage_manager TRE");
265 //                 $user->room->garbage_manager(FALSE);
266 //                 Room::save_data($user->room);
267 //                 unset($user->room);
268 //             }
269 //             log_main("infolock: U");
270 //             Room::unlock_data($sem);
271 //             ignore_user_abort(FALSE);
272 //         } // if (($sem = Room::lock_data(TRUE)) != FALSE) { 
273 //         else {
274 //             // wait 20 secs, then restart the xhr 
275 //             ignore_user_abort(FALSE);
276             
277 //             return ("sleep(gst,20000);|hstm.xhr_abort();");
278 //         }
279 //         $first_loop = FALSE;
280 //     } // if  ($first_loop == TRUE) {
281     
282     /* if ($cur_step == $proxy_step['s']) { */
283     /*     log_main("infolock: P"); */
284     /*     return (FALSE); */
285     /* } */
286     /* else { */
287     /*     log_only2("R"); */
288     /* } */
289
290     $S_load_stat['rU_heavy']++;
291     /* if ($user == FALSE) { */
292     /*     do { */
293     /*         ignore_user_abort(TRUE); */
294     /*         if (($sem = Room::lock_data(FALSE)) == FALSE)  */
295     /*             break; */
296             
297     /*         log_main("infolock: P"); */
298     /*         if (($user = User::load_data($proxy_step['i'], $sess)) == FALSE) { */
299     /*             break; */
300     /*         } */
301     /*     } while (0); */
302         
303     /*     if ($sem != FALSE) */
304     /*         Room::unlock_data($sem); */
305         
306     /*     ignore_user_abort(FALSE); */
307     /*     if ($user == FALSE) { */
308     /*         return (blocking_error(TRUE)); */
309     /*     } */
310     /* } */
311     
312     /* Nothing changed, return. */
313     if ($cur_step == $user->step) 
314         return (FALSE);
315     
316     log_rd2("do other cur_stat[".$cur_stat."] user->stat[".$user->stat."] cur_step[".$cur_step."] user_step[".$user->step."]");
317     
318     if ($cur_step == -1) {
319         /*
320          *  if $cur_step == -1 load the current state from the main struct
321          */
322
323         /* unset the $user var to reload it from main structure */
324         /* unset($user); */
325
326         ignore_user_abort(TRUE);
327         /* $sem = Room::lock_data(TRUE); */
328         /* if (($user->room = Room::load_data()) == FALSE) { */
329         /*     Room::unlock_data($sem); */
330         /*     ignore_user_abort(FALSE); */
331         /*     return (blocking_error(TRUE)); */
332         /* } */
333         $S_load_stat['wR_minusone']++;
334         
335         /* if (($user = $user->room->get_user($sess, $idx)) == FALSE) { */
336         /*     Room::unlock_data($sem); */
337         /*     ignore_user_abort(FALSE); */
338         /*     return (blocking_error(TRUE)); */
339         /* } */
340         
341         if ($user->the_end == TRUE) { 
342             log_rd2("main_check: the end".var_export(debug_backtrace()));
343             $is_page_streaming = TRUE;
344         }
345         
346         if ($user->trans_step != -1) {
347             log_rd2("TRANS USATO ".$user->trans_step);
348             $cur_step = $user->trans_step;
349             $user->trans_step = -1;
350             
351             /* Room::save_data($user->room); */
352             /* Room::unlock_data($sem); */
353             ignore_user_abort(FALSE);
354         }
355         else {
356             log_rd2("TRANS NON ATTIVATO");
357             //        ARRAY_POP DISABLED
358             //        log_rd2("TRANS NON ATTIVATO, clean del comm array");
359             //        while (($el = array_pop($user->comm)) != NULL) { 
360             //          log_rd2("clean element [".$el."]");
361             //        }
362             //        //        $user->step_inc(COMM_N + 1);
363             //        Room::save_data($user->room);
364             //        //        $new_step = $user->step;
365             
366             /* Room::unlock_data($sem); */
367             ignore_user_abort(FALSE);
368         }
369     }
370     
371     
372     /* this part I suppose is read only on $user->room structure */
373     if ($cur_step == -1) {
374         log_rd2("PRE-NEWSTAT: ".$user->stat);
375         
376         if ($user->stat == 'room') {
377             log_rd("roomma ".$user->step);
378             $curtime = time();
379             
380             if ($G_with_splash &&
381                 ($$CO_splashdate < $curtime - $G_splash_interval ||
382                  $$CO_splashdate > $curtime)) {
383                 $is_super = $user->flags & USER_FLAG_TY_SUPER;
384                 $ret .=  show_notify_ex(str_replace("\n", " ", $G_splash_content[$G_lang]), 
385                                         ($is_super ? 0 : $G_splash_timeout), 
386                                         $mlang_indrd[($is_super ? 'btn_btotabsup' : 'btn_backtotab')][$G_lang], 
387                                         $G_splash_w, $G_splash_h, true, 
388                                         ($is_super ? 0 : $G_splash_timeout));
389                 $ret .= sprintf('|createCookie("CO_splashdate%d", %d, 24*365, cookiepath);', $G_splash_idx, $curtime);
390             }
391             $ret .= $user->room->show_room($user->step, $user);
392             
393             // TODO uncomment and test
394             /* NOTE the sets went common */
395             $new_stat =  $user->stat;
396             $new_subst = $user->subst;
397             $new_step =  $user->step;
398         }
399         /***************
400          *             *
401          *    TABLE    *
402          *             *
403          ***************/
404         else if ($user->stat == 'table') {
405             log_load("RESYNC");
406             return (page_sync($user->sess, "briskin5/index.php", $user->table, $user->table_token));
407         }
408         log_rd2("NEWSTAT: ".$user->stat);
409     } /* if ($cur_step == -1) { */
410     else {
411         ignore_user_abort(TRUE);
412         /* $sem = Room::lock_data(FALSE); */
413         $S_load_stat['rU_heavy']++;
414         /* if (($user = User::load_data($proxy_step['i'], $sess)) == FALSE) { */
415         /*     Room::unlock_data($sem); */
416         /*     ignore_user_abort(FALSE); */
417         /*     return (blocking_error(TRUE)); */
418         /* } */
419         
420         if ($cur_step < $user->step) {
421             do {
422                 if ($cur_step + COMM_N < $user->step) {
423                     if (($cur_stat != $user->stat)) {
424                         $to_stat = $user->stat;
425                         /* Room::unlock_data($sem); */
426                         ignore_user_abort(FALSE);
427                         log_load("RESYNC");
428                         return (page_sync($user->sess, ($to_stat == "table" ? "briskin5/index.php" : "index.php"), $user->table, $user->table_token));
429                     }
430                     log_rd2("lost history, refresh from scratch");
431                     $new_step = -1;
432                     break;
433                 } 
434                 for ($i = $cur_step ; $i < $user->step ; $i++) {
435                     $ii = $i % COMM_N;
436                     log_rd2("ADDED TO THE STREAM: ".$user->comm[$ii]);
437                     $ret .= $user->comm[$ii];
438                 }
439                 $new_stat =  $user->stat;
440                 $new_subst = $user->subst;
441                 $new_step =  $user->step;
442             } while (0);
443             
444             log_rd2($user->step, 'index_rd.php: after ret set');
445             
446             if ($user->the_end == TRUE) {
447                 /* Room::unlock_data($sem); */
448                 
449                 /* Switch to exclusive locking */
450                 /* $sem = Room::lock_data(TRUE); */
451
452                 /* unset($user); */
453
454                 $S_load_stat['wR_the_end']++;
455                 /* if (($user->room = Room::load_data()) == FALSE) { */
456                 /*     Room::unlock_data($sem); */
457                 /*     ignore_user_abort(FALSE); */
458                 /*     return (blocking_error(TRUE)); */
459                 /* } */
460
461                 /* if (($user = $user->room->get_user($sess, $idx)) == FALSE) { */
462                 /*     Room::unlock_data($sem); */
463                 /*     ignore_user_abort(FALSE); */
464                 /*     return (blocking_error(TRUE)); */
465                 /* }               */
466
467                 log_rd2("LOGOUT BYE BYE!!");
468                 log_auth($user->sess, "Explicit logout.");
469                 
470                 if ($user->the_end == TRUE) {
471                     $user->reset();
472                     
473                     if ($user->subst == 'sitdown') {
474                         log_load("ROOM WAKEUP");
475                         $user->room->room_wakeup($user);
476                     }
477                     else if ($user->subst == 'standup')
478                         $user->room->room_outstandup($user);
479                     else
480                         log_rd2("LOGOUT FROM WHAT ???");
481                     
482                     /* Room::save_data($user->room); */
483                 } /* if ($user->the_end == TRUE) { ... */
484             } /* if ($user->the_end == TRUE) { ... */
485         } /* if ($cur_step < $user->step) { */
486         
487         /* Room::unlock_data($sem); */
488         ignore_user_abort(FALSE);
489     }  /* else of if ($cur_step == -1) { */
490     
491     
492     return ($ret);
493 }
494
495 function index_rd_ifra_fini($is_unrecoverable)
496 {
497     GLOBAL $G_four_rnd_string;
498
499     // IF IFRAME THEN:
500     $body = "";
501     $body .= sprintf("<html>
502 <head>
503 <script type=\"text/javascript\" src=\"commons.js\"></script>
504 <script type=\"text/javascript\" src=\"xynt-http-streaming-ifra.js\"></script>
505 <script type=\"text/javascript\">
506 var http_streaming = \"ready\";");
507     $body .= sprintf("
508 window.onload = function () { if (http_streaming != \"ready\") { http_streaming.reload(); } };
509 </script>
510 </head>
511 <body>");
512     $body .= sprintf("<!-- \n%s -->\n", $G_four_rnd_string);
513     $body .= sprintf("<script id='hs%d' type='text/javascript'><!--
514 push(\"%s\");
515 // -->
516 </script>", 0, escpush(blocking_error($is_unrecoverable)) );
517     // ELSE IF XHR THEN:
518     // return (blocking_error($is_unrecoverable));
519     return ($body);
520 }
521
522 /*
523  *  MAIN
524  */
525
526 /*
527    FROM THE EXTERN 
528    sess
529    stat
530    step
531 */
532 function index_rd_ifra_init(&$user, &$header_out, &$body, $get, $post, $cookie)
533 {
534     GLOBAL $G_four_rnd_string;
535
536     $curtime = time();
537
538     $is_page_streaming = FALSE; // (webservers_exceeded() || stristr($HTTP_USER_AGENT, "Mozilla/5.0 (Windows NT 6.1; rv:5.0)") || stristr($HTTP_USER_AGENT, "MSIE") || stristr($HTTP_USER_AGENT, "CHROME") ? TRUE : FALSE);
539
540     $header_out['Cache-Control'] = 'no-cache, must-revalidate';     // HTTP/1.1
541     $header_out['Expires']       = 'Mon, 26 Jul 1997 05:00:00 GMT'; // Date in the past
542     $header_out['Content-type']  = 'text/html; charset="utf-8"';
543
544     log_load("index_rd_ifra_init.php");
545
546     if (($from  = gpcs_var('from', $get, $post, $cookie)) === FALSE)
547         $from = "";
548     if (($stat  = gpcs_var('stat', $get, $post, $cookie)) === FALSE) 
549         $stat = "";
550     if (($subst = gpcs_var('subst', $get, $post, $cookie)) === FALSE) 
551         $subst = "";
552     if (($step  = gpcs_var('step', $get, $post, $cookie)) === FALSE) 
553         unset($step);
554     
555     $user->rd_data_set($curtime, $stat, $subst, $step, $from);
556
557     $body .= sprintf("<html>
558 <head>
559 <script type=\"text/javascript\" src=\"commons.js\"></script>
560 <script type=\"text/javascript\" src=\"xynt-http-streaming-ifra.js\"></script>
561 <script type=\"text/javascript\">
562 var http_streaming = \"ready\";");
563     if ($user->rd_scristp > 0)
564         $body .= sprintf("last_clean = %d;\n", ($user->rd_scristp-1));
565     $body .= sprintf("
566 window.onload = function () { if (http_streaming != \"ready\") { http_streaming.reload(); } };
567 </script> 
568 </head>
569 <body>");
570     $body .= sprintf("<!-- \n%s -->\n", $G_four_rnd_string);
571
572     return TRUE;
573 }
574
575 function index_rd_ifra_main(&$user, &$body, $get, $post, $cookie)
576 {
577     GLOBAL $is_page_streaming, $G_splash_idx;
578     // FIXME: only to test fwrite
579     // GLOBAL $G_four_rnd_string;
580
581     $CO_splashdate = "CO_splashdate".$G_splash_idx;
582     if (($splashdate = gpcs_var("$CO_splashdate", $get, $post, $cookie)) === FALSE)
583         $splashdate = ""; 
584
585     $is_page_streaming = FALSE;
586     log_rd2("FROM OUTSIDE - STAT: ".$user->rd_stat." SUBST: ".$user->rd_subst." STEP: ".$user->rd_step." FROM: ".$user->rd_from. "IS_PAGE:" . $is_page_streaming);
587     
588     
589     // $no_data = TRUE;
590     // for ($i = 0, $script_step = 0 ; time() < $endtime ; $i++) {
591         // log_rd("PRE MAIN ".$step);;
592
593         $pre_main = gettimeofday(TRUE);
594
595         $old_stat  = $user->rd_stat;
596         $old_subst = $user->rd_subst;
597         $old_step  = $user->rd_step;
598         if (($ret = maincheck($user, $old_stat, $old_subst, $old_step, $user->rd_stat, $user->rd_subst, $user->rd_step, $splashdate)) != FALSE) {
599             // $no_data = FALSE;
600             if (0 == 1) {
601                 echo '@BEGIN@';
602                 // 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));
603                 echo "$ret";
604                 echo ' @END@'; 
605             }
606
607             // FIXME only to test fwrite
608             // for ($u = 0 ; $u < 10 ; $u++) {
609             //    $body .= sprintf("<!-- \n%s -->\n", $G_four_rnd_string);
610             // }
611             $body .= sprintf("<script id='hs%d' type='text/javascript'><!--
612 push(\"%s\");
613 // -->
614 </script>", $user->rd_scristp++, escpush($ret) );
615             
616             // log_send("IS_PAGE: ".($is_page_streaming == TRUE ? "TRUE" : "FALSE")." ENDTIME: [".$endtime."] ".$ret);
617             // mop_flush();
618             log_rd2(0, 'index_rd.php: after mop_flush (begin: '.sprintf("%f", $pre_main).')');
619             if ($is_page_streaming) 
620                 return TRUE;
621         }
622         /* $old_stat =  $stat; */
623         /* $old_subst = $subst; */
624         /* $old_step =  $step; */
625         // log_rd("POST MAIN ".$step);;
626         // usleep(400000);
627 /*         if (($i % 10) == 0 && 1 == 0) { // TODO: reenable push(null); */
628 /*             if ($no_data == TRUE) { */
629 /*                 // log_rd2("TIME: ".time()); */
630 /*                 printf("<script id='hs%d' type='text/javascript'><!-- */
631 /* push(null); */
632 /* // --> */
633 /* alert(\"de che\"); */
634 /* </script>", $script_step++); */
635 /*                 // mop_flush(); */
636 /*                 log_crit("flush"); */
637 /*             } */
638 /*             $no_data = TRUE; */
639 /*         } */
640         // }
641     
642     /* $s = "";  */
643     /* $tr = 0; */
644     /* $tw = 0; */
645     /* foreach ($S_load_stat as $key => $value) { */
646     /*     $s .= sprintf("%s: %d - ", $key, $value); */
647     /*     if (substr($key, 0, 1) == "w") */
648     /*         $tw += $value; */
649     /*     else if (substr($key, 0, 1) == "r") */
650     /*         $tr += $value; */
651     /* } */
652     /* $s = sprintf("index_rd.php stats:  R: %d W: %d - %s", $tr, $tw, $s); */
653     /* log_crit($s); */
654
655     return TRUE;
656 }
657
658 function index_rd_ifra_keepalive(&$user)
659 {
660     return (sprintf("<script id='hs%d' type='text/javascript'><!--
661 push(null);
662 // -->
663 </script>", $user->rd_scristp++));
664
665 }
666 ?>