proper way to fix static inheritance bug
[brisk.git] / web / Obj / user.phh
1 <?php
2 /*
3  *  brisk - Obj/user.phh
4  *
5  *  Copyright (C) 2012 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 // User flags
25 define('USER_FLAG_AUTH',     0x02);
26
27 define('USER_FLAG_MAP_AUTH', 0x0c);
28 define('USER_FLAG_LISTAUTH', 0x04);
29 define('USER_FLAG_ISOLAUTH', 0x08);
30
31 define('USER_FLAG_DBFAILED', 0x10);
32
33 //   user status
34 define('USER_FLAG_S_NORM',  0x000); // done
35 define('USER_FLAG_S_PAU',   0x100); // done
36 define('USER_FLAG_S_OUT',   0x200); // done
37 define('USER_FLAG_S_DOG',   0x300); // done
38 define('USER_FLAG_S_EAT',   0x400); // done
39 define('USER_FLAG_S_WRK',   0x500); // done
40 define('USER_FLAG_S_SMK',   0x600); // done
41 define('USER_FLAG_S_EYE',   0x700); // done
42 define('USER_FLAG_S_RABB',  0x800); // done
43 define('USER_FLAG_S_SOCC',  0x900); // done
44 define('USER_FLAG_S_BABY',  0xa00); // done
45 define('USER_FLAG_S_MOP',   0xb00); // done
46
47 define('USER_FLAG_S_ALL',   0xf00); // done
48
49 /* type of user normal, supporter etc ... */
50 define('USER_FLAG_TY_ALL',     0xff0000); // done
51 define('USER_FLAG_TY_NORM',    0x010000); // done
52 define('USER_FLAG_TY_SUPER',   0x020000); // done
53 //  ... other usefull status ...
54 define('USER_FLAG_TY_SUSPEND', 0x400000); // done
55 define('USER_FLAG_TY_DISABLE', 0x800000); // done
56
57 // 240 is the right value, 600 is for fwrite error test
58 define('RD_ENDTIME_DELTA',  240);
59 define('RD_KEEPALIVE_TOUT',   4);
60
61 $S_load_stat = array( 'rU_heavy'      => 0,
62                       'lL_laccgarb'   => 0,
63                       'wU_lacc_upd'   => 0,
64                       'wR_garbage'    => 0,
65                       'wR_minusone'   => 0,
66                       'wR_the_end'    => 0 );
67
68 $mlang_indrd = array( 
69                      'btn_backtotab'  => array('it' => ' torna ai tavoli ',
70                                                'en' => ' back to tables '),
71                      'btn_btotabsup'  => array('it' => ' grazie della donazione, torna ai tavoli ',
72                                                'en' => ' thank you for donation, back to tables ') 
73                      );
74
75 class User {
76   var $room;       // reference to the room where the user is registered
77   var $idx;        // index in the users array when you are in game
78   var $idx_orig;   // index in the users array when you aren't in game
79   var $code;       // authentication code
80   var $name;       // name of the user
81   var $sess;       // session of the user
82   var $ip;         // ip of the user
83   var $lacc;       // last access (for the cleanup)
84   var $laccwr;     // last access (for the cleanup)
85   var $bantime;    // timeout to temporary ban
86   var $stat;       // status (outdoor, room, table, game, ...)
87   var $subst;      // substatus for each status   
88   var $step;       // step of the current status
89   var $trans_step; // step to enable transition between pages (disable == -1)
90
91   var $rd_socket;  // socket handle of push stream
92   var $rd_endtime; // end time for push stream
93   var $rd_stat;    // actual status of push stream
94   var $rd_subst;   // actual substatus of push stream
95   var $rd_step;    // actual step of push stream
96   var $rd_from;    // referer
97   var $rd_scristp; // current script step (for each session) 
98   var $rd_kalive;  // if no message are sent after RD_KEEPALIVE_TOUT secs we send a keepalive from server
99   var $rd_cache;   // place where store failed fwrite data
100   var $rd_zls;     // zlibstream object handle if compressed stream, else FALSE
101
102   var $comm;       // commands array
103   // var $asta_card;  // 
104   // var $asta_pnt;   //
105   // var $handpt;     // Total card points at the beginning of the current hand.
106   // var $exitislock; // Player can exit from the table ?
107
108   // FIXME: the table_orig field must be removed after table field verify of index management (in spawned table
109   //        it is allways ZERO
110   var $table;      // id of the current table when you are in game
111   var $table_orig; // id of the current table when you aren't in game
112   var $table_pos;  // idx on the table
113   var $table_token;// token that identify a game on a table
114   var $flags;      // Bitfield with: AUTHENTICATE: 0x02 
115   var $the_end;    // Flag to change the end of the session
116
117   var $chat_lst;      // Last chat line
118   var $chattime;      // Array of chat times
119   var $chat_cur;      // Current chat line number
120   var $chat_ban;      // Time for ban chat
121   var $chat_dlt;      // Delta t for ban
122   var $shm_sz;
123
124   const BASE = "";    // basepath for absolute web references
125
126   function User() {
127   }
128
129   function create(&$room, $idx, $name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
130     if (($thiz = new User()) == FALSE)
131       return (FALSE);
132
133     $thiz->room       = &$room;
134     $thiz->idx        = $idx;
135     $thiz->idx_orig   = $idx;
136     $thiz->code       = -1;
137     $thiz->name       = $name;
138     $thiz->sess       = $sess;
139     $thiz->ip         = $ip;
140     $thiz->lacc       = time();
141     $thiz->laccwr     = time();
142     $thiz->bantime    = 0;
143     $thiz->stat       = $stat;
144     $thiz->subst      = $subst;
145     $thiz->step       = 1;
146     $thiz->trans_step = -1;
147     $thiz->comm       = array();
148
149     $thiz->rd_socket  = NULL;
150     $thiz->rd_endtime = -1;
151     $thiz->rd_stat    = -1;
152     $thiz->rd_subst   = "";
153     $thiz->rd_step    = -1;
154     $thiz->rd_from    = "";
155     $thiz->rd_scristp = -1;
156     $thiz->rd_kalive  = -1;
157     $thiz->rd_cache   = "";
158     $thiz->rd_zls     = FALSE;
159
160     $thiz->asta_card  = -2;
161     $thiz->asta_pnt   = -1;
162     $thiz->handpt     = -1;
163     $thiz->exitislock = TRUE;
164
165     $thiz->flags      = 0x00;
166     
167     $thiz->chattime   = array_fill(0, CHAT_N, 0);
168     $thiz->chat_cur   = 0;
169     $thiz->chat_lst   = "";
170     $thiz->chat_ban   = 0;
171     $thiz->chat_dlt   = 0;
172
173     $thiz->table_orig = $table;
174     $thiz->table      = $table;
175     $thiz->table_pos  = -1;
176     $thiz->table_token= "";
177     $thiz->shm_sz = SHM_DIMS_U_MIN;
178     return ($thiz);
179   }
180
181   function copy($from)
182   {
183     $this->idx        = $from->idx;
184     $this->idx_orig   = $from->idx;
185     $this->code       = $from->code;
186     $this->name       = $from->name;
187     $this->sess       = $from->sess;
188     $this->ip         = $from->ip;
189     $this->lacc       = $from->lacc;
190     $this->laccwr     = $from->laccwr;
191     $this->bantime    = $from->bantime;
192     $this->stat       = $from->stat;
193     $this->subst      = $from->subst;
194     $this->step       = $from->step;
195     $this->trans_step = $from->trans_step;
196     $this->comm       = array();
197
198     $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N)); 
199     for ($i = $i_start ; $i < $from->step ; $i++) {
200         $ii = $i % COMM_N;
201         if (isset($from->comm[$ii])) {
202             $this->comm[$ii] = $from->comm[$ii];
203         }
204     }
205     $this->asta_card  = $from->asta_card;
206     $this->asta_pnt   = $from->asta_pnt;
207     $this->handpt     = $from->handpt;
208     $this->exitislock = $from->exitislock;
209
210     $this->flags      = $from->flags;
211
212     $this->chattime   = array();
213     for ($i = 0 ; $i < CHAT_N ; $i++)
214       $this->chattime[$i] = $from->chattime[$i];
215     $this->chat_cur   = $from->chat_cur;
216     $this->chat_lst   = $from->chat_lst;
217     $this->chat_ban   = $from->chat_ban;
218     $this->chat_dlt   = $from->chat_dlt;
219
220     $this->table_orig = $from->table_orig;
221     $this->table      = $from->table;
222     $this->table_pos  = $from->table_pos;
223     $this->table_token = $from->table_token;
224     $this->the_end    = $from->the_end;
225     $this->shm_sz     = $from->shm_sz;
226     return (TRUE);
227   }
228
229
230   function myclone($from)
231   {
232     if (($thiz = new User()) == FALSE)
233       return (FALSE);
234     
235     $thiz->copy($from);
236
237     return ($thiz);
238   }
239   
240   function spawn($from, $table, $table_pos)
241   {
242     if (($thiz = new User()) == FALSE)
243       return (FALSE);
244     
245     $thiz->idx        = $from->idx;
246     $thiz->idx_orig   = $from->idx;
247     $thiz->code       = $from->code;
248     $thiz->name       = $from->name;
249     $thiz->sess       = $from->sess;
250     $thiz->ip         = $from->ip;
251     $thiz->lacc       = $from->lacc;
252     $thiz->laccwr     = $from->laccwr;
253     $thiz->bantime    = $from->bantime;
254     $thiz->stat       = $from->stat;
255     $thiz->subst      = $from->subst;
256     $thiz->step       = $from->step;
257     $thiz->trans_step = $from->trans_step;
258     $thiz->comm       = array();
259
260     /*
261     $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N)); 
262     for ($i = $i_start ; $i < $from->step ; $i++) {
263       log_wr("TRY PUSH:".$i);
264       $ii = $i % COMM_N;
265       $thiz->comm[$ii]   = $from->comm[$ii];
266     }
267     */
268     $thiz->asta_card  = $from->asta_card;
269     $thiz->asta_pnt   = $from->asta_pnt;
270     $thiz->handpt     = $from->handpt;
271     $thiz->exitislock = $from->exitislock;
272     $thiz->the_end    = $from->the_end;
273
274     $thiz->flags      = $from->flags;
275
276     $thiz->chattime   = array_fill(0, CHAT_N, 0);
277     $thiz->chat_cur   = 0;
278     $thiz->chat_lst   = "";
279     $thiz->chat_ban   = 0;
280     $thiz->chat_dlt   = 0;
281
282
283     $thiz->table_orig = $table;
284     $thiz->table      = 0;
285     $thiz->table_pos  = $table_pos;
286     $thiz->table_token = $from->table_token;
287     $thiz->shm_sz      = $from->shm_sz;
288
289     return ($thiz);
290   }
291
292   function rd_data_set($curtime, $enc, $stat, $subst, $step, $from)
293   {
294       $this->rd_endtime = $curtime + RD_ENDTIME_DELTA;
295       $this->rd_stat    = $stat;
296       $this->rd_subst   = $subst;
297       $this->rd_step    = $step;
298       $this->rd_from    = $from;
299       $this->rd_scristp = 0;
300       $this->rd_kalive  = $curtime + RD_KEEPALIVE_TOUT;
301       $this->rd_zls     = ZLibStream::create($enc);
302   }
303
304   function rd_socket_get() {
305       return ($this->rd_socket);
306   }
307
308   function rd_socket_set($sock) {
309       if ($sock == NULL) {
310           if ($this->rd_zls) {
311               $this->rd_zls->destroy();
312               $this->rd_zls = FALSE;
313           }
314       }
315       $this->rd_socket = $sock;
316   }
317
318   function rd_kalive_get()
319   {
320       return ($this->rd_kalive);
321   }
322
323   function rd_kalive_set($tm)
324   {
325       $this->rd_kalive = $tm;
326   }
327
328   function rd_kalive_is_expired($tm)
329   {
330       // printf("rd_kalive %d tm %d\n", $this->rd_kalive, $tm);
331       return ($this->rd_kalive < $tm);
332   }
333
334   function rd_endtime_is_expired($tm)
335   {
336       // printf("rd_endtime %d tm %d\n", $this->rd_kalive, $tm);
337       return ($this->rd_endtime < $tm);
338   }
339
340   function rd_kalive_reset($tm)
341   {
342       $this->rd_kalive = $tm + RD_KEEPALIVE_TOUT;
343   }
344
345   function rd_cache_get()
346   {
347       return ($this->rd_cache);
348   }
349
350   function rd_cache_set($cache)
351   {
352       $this->rd_cache = $cache;
353   }
354
355   function rd_zls_get()
356   {
357       return ($this->rd_zls);
358   }
359
360   function idx_get() {
361       return ($this->idx);
362   }
363
364   function code_get() {
365       return ($this->code);
366   }
367   
368   function stat_set($stat) {
369     log_main("sess: [".$this->sess. "] NEW STAT: [".$stat."]"); 
370     $this->stat = "$stat";
371     
372     /*
373     if (validate_sess($this->sess)) {
374       if (file_exists(PROXY_PATH) == FALSE)
375         mkdir(PROXY_PATH, 0775, TRUE);
376       $fp = @fopen(PROXY_PATH."/".$this->sess.".stat", 'w');
377       fwrite($fp, sprintf("%s\n",$this->stat));
378       fclose($fp);
379     }
380     */
381   }
382
383   function step_set($step) 
384   {
385       $this->step = $step & 0x7fffffff;
386       
387       return (TRUE);
388   }
389
390   function step_inc($delta = 1) {
391       $this->step += $delta;
392       /* modularization because unpack() not manage unsigned 32bit int correctly */
393       $this->step &= 0x7fffffff;
394       
395       return TRUE;
396   }
397
398
399   function save_step() 
400   {
401       do {
402           if (validate_sess($this->sess) == FALSE)
403               break;
404           if (file_exists(PROXY_PATH) == FALSE)
405               mkdir(PROXY_PATH, 0775, TRUE);
406           if (($fp = @fopen(PROXY_PATH."/".$this->sess.".step", 'w')) == FALSE)
407               break;
408           fwrite($fp, pack("LL",$this->step, $this->idx));
409           fclose($fp);
410           
411           log_main("step_set [".$this->sess. "] [".$this->step."]"); 
412           
413           return (TRUE);
414       } while (0);
415       
416       return (FALSE);
417   }
418   
419   static function load_step($sess) 
420   {
421       $fp = FALSE;
422       do {
423           if (validate_sess($sess) == FALSE)
424               break;
425           
426           if (file_exists(PROXY_PATH) == FALSE)
427               mkdir(PROXY_PATH, 0775, TRUE);
428           if (($fp = @fopen(PROXY_PATH."/".$sess.".step", 'rb')) == FALSE)
429               break;
430           if (($s = fread($fp, 8)) == FALSE)
431               break;
432           if (mb_strlen($s, "ASCII") != 8)
433               break;
434           $arr = unpack('Ls/Li', $s);
435           fclose($fp);
436           
437           // log_rd2("A0: ".$arr[0]."  A1: ".$arr[1]);
438           return ($arr);
439       } while (0);
440       
441       if ($fp != FALSE)
442           fclose($fp);
443       
444       log_rd2("STEP_GET [".$sess."]: return false ");
445       
446       return (FALSE);
447   }
448
449   static function unproxy_step($sess) {
450       log_rd2("UNPROXY: ".PROXY_PATH."/".$sess.".step");
451       if (file_exists(PROXY_PATH) == FALSE)
452           return;
453       @unlink(PROXY_PATH."/".$sess.".step");
454   }
455
456   function reset() {
457     $curtime = time();
458     log_legal($curtime, 'xxx', $this, "STAT:LOGOUT", '');
459
460     $tmp_sess = $this->sess;
461     $this->sess = "";
462     self::unproxy_step($tmp_sess);
463     $this->name = "";  // OK here
464     while (array_pop($this->comm) != NULL);
465     $this->step = 0;
466     $this->chattime = array_fill(0, CHAT_N, 0);
467     $this->chat_cur = 0;
468     $this->chat_lst = "";
469     $this->chat_ban = 0;
470     $this->chat_dlt = 0;
471     $this->the_end = FALSE;
472   }
473
474   function myname_innerHTML()
475   {
476       $class_id = ($this->flags & USER_FLAG_AUTH) + 1;
477       
478       return (sprintf('$("myname").innerHTML = "<span class=\"au%d\">%s</span>";', $class_id, 
479                       xcape($this->name,ENT_COMPAT,"UTF-8")));
480   }
481
482   /* INDEX_RD_IFRA PORT */
483
484   static function blocking_error($is_unrecoverable)
485   {
486       log_crit("BLOCKING_ERROR UNREC: ".($is_unrecoverable ? "TRUE" : "FALSE"));
487       return (sprintf(($is_unrecoverable ? 'xstm.stop(); ' : '').'window.onbeforeunload = null; window.onunload = null; document.location.assign("index.php");'));
488   }
489   
490   // FIXME TO SUPPORT iframe
491   protected function page_sync($sess, $page, $table_idx, $table_token)
492   {
493       // log_rd2("page_sync:".var_export(debug_backtrace()));
494       
495       log_rd2("PAGE_SYNC");
496       printf("xXx USER::PAGE_SYNC [%s]\n", get_class($this));
497       return (sprintf('createCookie("table_idx", %d, 24*365, cookiepath); createCookie("table_token", "%s", 24*365, cookiepath); xstm.stop(); window.onunload = null; window.onbeforeunload = null; document.location.assign("%s");', $table_idx, $table_token, $page));
498   }
499
500
501
502
503   protected function maincheck($cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step, $splashdate, $table_idx, $table_token)
504   {
505       GLOBAL $G_lang, $mlang_indrd;
506       // GLOBAL $first_loop;
507       GLOBAL $G_with_splash, $G_splash_content, $G_splash_interval, $G_splash_idx;
508       GLOBAL $G_splash_w, $G_splash_h, $G_splash_timeout;
509       $CO_splashdate = "CO_splashdate".$G_splash_idx;
510       $$CO_splashdate = $splashdate;
511       
512       GLOBAL $S_load_stat;
513       
514       log_rd("maincheck begin");
515       
516       $ret = FALSE;
517       $curtime = time();
518       
519       /* Nothing changed, return. */
520       if ($cur_step == $this->step) 
521           return (FALSE);
522       
523       log_rd2("do other cur_stat[".$cur_stat."] user->stat[".$this->stat."] cur_step[".$cur_step."] user_step[".$this->step."]");
524       
525       if ($cur_step == -1) {
526           /*
527            *  if $cur_step == -1 load the current state from the main struct
528            */
529           
530           $S_load_stat['wR_minusone']++;
531           
532           // if ($this->the_end == TRUE) {
533           // log_rd2("main_check: the end".var_export(debug_backtrace()));
534           // }
535           
536           if ($this->trans_step != -1) {
537               log_rd2("TRANS USATO ".$this->trans_step);
538               $cur_step = $this->trans_step;
539               $this->trans_step = -1;
540           }
541           else {
542               log_rd2("TRANS NON ATTIVATO");
543           }
544       }
545       
546       
547       /* this part I suppose is read only on $this->room structure */
548       if ($cur_step == -1) {
549           log_rd2("PRE-NEWSTAT: ".$this->stat);
550           
551           if ($this->stat == 'room') {
552               log_rd("roomma ".$this->step);
553               $curtime = time();
554               
555               if ($G_with_splash &&
556                   ($$CO_splashdate < $curtime - $G_splash_interval ||
557                    $$CO_splashdate > $curtime)) {
558                   $is_super = $this->flags & USER_FLAG_TY_SUPER;
559                   $ret .=  show_notify_ex(str_replace("\n", " ", $G_splash_content[$G_lang]), 
560                                           ($is_super ? 0 : $G_splash_timeout), 
561                                           $mlang_indrd[($is_super ? 'btn_btotabsup' : 'btn_backtotab')][$G_lang], 
562                                           $G_splash_w, $G_splash_h, true, 
563                                           ($is_super ? 0 : $G_splash_timeout));
564                   $ret .= sprintf('|createCookie("CO_splashdate%d", %d, 24*365, cookiepath);', $G_splash_idx, $curtime);
565               }
566               $ret .= $this->room->show_room($this->step, $this);
567               
568               // TODO uncomment and test
569               /* NOTE the sets went common */
570               $new_stat =  $this->stat;
571               $new_subst = $this->subst;
572               $new_step =  $this->step;
573           }
574           /***************
575            *             *
576            *    TABLE    *
577            *             *
578            ***************/
579           else if ($this->stat == 'table') {
580               log_load("RESYNC");
581               printf("xXx USER::MAINCHECK1 [%s]\n", get_class($this));
582
583               return ($this->page_sync($this->sess, "briskin5/index.php", $this->table, $this->table_token));
584           }
585           log_rd2("NEWSTAT: ".$this->stat);
586       } /* if ($cur_step == -1) { */
587       else {
588           /* $sem = Room::lock_data(FALSE); */
589           $S_load_stat['rU_heavy']++;
590           
591           if ($cur_step < $this->step) {
592               do {
593                   if ($cur_step + COMM_N < $this->step) {
594                       if (($cur_stat != $this->stat)) {
595                           $to_stat = $this->stat;
596                           /* Room::unlock_data($sem); */
597                           log_load("RESYNC");
598                           printf("xXx USER::MAINCHECK2 [%s]\n", get_class($this));
599                           return ($this->page_sync($this->sess, ($to_stat == "table" ? "briskin5/index.php" : "index.php"), $this->table, $this->table_token));
600                       }
601                       log_rd2("lost history, refresh from scratch");
602                       $new_step = -1;
603                       break;
604                   } 
605                   for ($i = $cur_step ; $i < $this->step ; $i++) {
606                       $ii = $i % COMM_N;
607                       log_rd2("ADDED TO THE STREAM: ".$this->comm[$ii]);
608                       $ret .= $this->comm[$ii];
609                   }
610                   $new_stat =  $this->stat;
611                   $new_subst = $this->subst;
612                   $new_step =  $this->step;
613               } while (0);
614               
615               log_rd2($this->step, 'index_rd.php: after ret set');
616               
617               if ($this->the_end == TRUE) {
618                   log_rd2("LOGOUT BYE BYE!!");
619                   log_auth($this->sess, "Explicit logout.");
620                   
621                   if ($this->the_end == TRUE) {
622                       $this->reset();
623                       
624                       if ($this->subst == 'sitdown') {
625                           log_load("ROOM WAKEUP");
626                           $this->room->room_wakeup($this);
627                       }
628                       else if ($this->subst == 'standup')
629                           $this->room->room_outstandup($this);
630                       else
631                           log_rd2("LOGOUT FROM WHAT ???");
632                       
633                   } /* if ($this->the_end == TRUE) { ... */
634               } /* if ($this->the_end == TRUE) { ... */
635           } /* if ($cur_step < $this->step) { */
636           
637           /* Room::unlock_data($sem); */
638       }  /* else of if ($cur_step == -1) { */
639       
640     
641       return ($ret);
642   }  //   function maincheck (...
643
644   public static function stream_fini($init_string, $is_unrecoverable)
645 {
646     $cla = get_called_class();
647     printf("xXx user::stream_fini\n");
648
649     // IF IFRAME THEN:
650     $body = "";
651     $body .= sprintf("<html>
652 <head>
653 <script type=\"text/javascript\" src=\"%scommons.js\"></script>
654 <script type=\"text/javascript\" src=\"%sxynt-streaming-ifra.js\"></script>
655 <script type=\"text/javascript\">
656 var xynt_streaming = \"ready\";", self::base_get(), self::base_get());
657     $body .= sprintf("
658 window.onload = function () { if (xynt_streaming != \"ready\") { xynt_streaming.reload(); } };
659 </script>
660 </head>
661 <body>");
662     $body .= sprintf("<!-- \n%s -->\n", $init_string);
663     $body .= sprintf("<script id='hs%d' type='text/javascript'><!--
664 push(\"%s\");
665 // -->
666 </script>", 0, escpush(static::blocking_error($is_unrecoverable)) );
667     // ELSE IF XHR THEN:
668     // return (static::blocking_error($is_unrecoverable));
669     return ($body);
670 }
671
672 /*
673  *  MAIN
674  */
675
676 /*
677    FROM THE EXTERN 
678    sess
679    stat
680    step
681 */
682 function stream_init($init_string, $enc, &$header_out, &$body, $get, $post, $cookie)
683 {
684     $curtime = time();
685     
686     printf("CLASS: [%s] base: [%s]\n", get_class($this), self::base_get());
687     
688     // (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);
689
690     if ($enc != 'plain')
691         $header_out['Content-Encoding'] = $enc;
692     $header_out['Cache-Control'] = 'no-cache, must-revalidate';     // HTTP/1.1
693     $header_out['Expires']       = 'Mon, 26 Jul 1997 05:00:00 GMT'; // Date in the past
694     $header_out['Content-type']  = 'text/html; charset="utf-8"';
695     
696     log_load("index_rd_ifra_init.php");
697     
698     if (($from  = gpcs_var('from', $get, $post, $cookie)) === FALSE)
699         $from = "";
700     if (($stat  = gpcs_var('stat', $get, $post, $cookie)) === FALSE) 
701         $stat = "";
702     if (($subst = gpcs_var('subst', $get, $post, $cookie)) === FALSE) 
703         $subst = "";
704     if (($step  = gpcs_var('step', $get, $post, $cookie)) === FALSE) 
705         unset($step);
706     
707     $this->rd_data_set($curtime, $enc, $stat, $subst, $step, $from);
708     $cc = get_called_class();
709     
710     $body .= sprintf("<html>
711 <head>
712 <script type=\"text/javascript\" src=\"%scommons.js\"></script>
713 <script type=\"text/javascript\" src=\"%sxynt-streaming-ifra.js\"></script>
714 <script type=\"text/javascript\">
715 var xynt_streaming = \"ready\";", self::base_get(), self::base_get());
716     if ($this->rd_scristp > 0)
717         $body .= sprintf("last_clean = %d;\n", ($this->rd_scristp-1));
718     $body .= sprintf("
719 window.onload = function () { if (xynt_streaming != \"ready\") { xynt_streaming.reload(); } };
720 </script> 
721 </head>
722 <body>");
723     $body .= sprintf("<!-- \n%s -->\n", $init_string);
724     
725     return TRUE;
726   }
727
728 function stream_main(&$body, $get, $post, $cookie)
729 {
730     GLOBAL $G_splash_idx;
731
732     $CO_splashdate = "CO_splashdate".$G_splash_idx;
733     if (($splashdate = gpcs_var("$CO_splashdate", $get, $post, $cookie)) === FALSE)
734         $splashdate = ""; 
735     if (($table_idx = gpcs_var("table_idx", $get, $post, $cookie)) === FALSE)
736         $table_idx = ""; 
737     if (($table_token = gpcs_var("table_token", $get, $post, $cookie)) === FALSE)
738         $table_token = ""; 
739
740     log_rd2("FROM OUTSIDE - STAT: ".$this->rd_stat." SUBST: ".$this->rd_subst." STEP: ".$this->rd_step." FROM: ".$this->rd_from);
741     
742     
743     $pre_main = gettimeofday(TRUE);
744     
745     $old_stat  = $this->rd_stat;
746     $old_subst = $this->rd_subst;
747     $old_step  = $this->rd_step;
748     printf("xXx PRE : rd_step %d\n", $this->rd_step);
749     if (($ret = $this->maincheck($old_stat, $old_subst, $old_step, $this->rd_stat, $this->rd_subst, $this->rd_step, $splashdate, $table_idx, $table_token)) != FALSE) {
750         $body .= sprintf("<script id='hs%d' type='text/javascript'><!--
751 push(\"%s\");
752 // -->
753 </script>", $this->rd_scristp++, escpush($ret) );
754         
755         log_rd2(0, 'index_rd.php: after mop_flush (begin: '.sprintf("%f", $pre_main).')');
756     }
757     printf("xXx POST: rd_step %d\n", $this->rd_step);
758
759     return TRUE;
760 }
761
762 function stream_keepalive($with_ping)
763 {
764     $ret =  (sprintf("<script id='hs%d' type='text/javascript'><!--
765 push(%s);
766 // -->
767 </script>", $this->rd_scristp++, ($with_ping ? "\"act_ping();\"" : "null")));
768
769     return ($ret);
770 }
771
772 static function base_get()
773 {
774     $c = get_called_class();
775     printf("CALLED_CLASS: [%s]\n", $c);
776     return $c::BASE;
777 }
778
779 } // end class User
780
781
782 ?>