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