gzip compression management added
[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   static function load_data($id, $sess) 
475   {
476       log_main("load_data: id [".$id."] sess [".($sess == FALSE ? "FALSE" : $sess)."] ");
477       
478       do {
479           if (($tok = @ftok(FTOK_PATH."/user".$id, "C")) == -1) {
480               log_main("ftok failed");
481               break;
482           }
483           
484           if (($shm_sz = sharedmem_sz($tok)) == -1) {
485               log_main("shmop_open failed");
486           }
487           
488           if ($shm_sz == -1)
489               $shm_sz = SHM_DIMS_U_MIN;
490           
491           if ($shm = shm_attach($tok, $shm_sz)) {
492               if (($user = @shm_get_var($shm, $tok)) == FALSE) {
493                   break;
494               }
495               if ($sess != FALSE && $user->sess != $sess) {
496                   break;
497               }
498               log_only("user ==  ".($user == FALSE ?   "FALSE" : "TRUE")."  user ===  ".($user === FALSE ? "FALSE" : "TRUE")."  user isset ".(isset($user) ?   "TRUE" : "FALSE"));
499               
500               if ($user == FALSE) {
501                   log_only("INIT MAIN DATA");
502                   
503                   // SHSPLIT FIXME: init_data for User class ??
504                   $user = self::create($id, "", "");
505                   if (@shm_put_var($shm, $tok, $user) == FALSE) {
506                       log_only("PUT_VAR FALLITA ".strlen(serialize($user)));
507                       log_only(serialize($user));
508                   }
509                   log_shme("User::save_data2");
510               }
511               else {
512                   if ($sess != FALSE) {
513                       /*
514                        *  NOTE:  this part is for check only, theoretically 
515                        *         user->step anch proxy_step are set allways at the same value
516                        */
517                       $old_step = $user->step;
518                       $arr = self::load_step($sess);
519                       $user->step = $arr['s'];
520                       if ($old_step != $user->step) {
521                           log_crit("steps are diffetents User->step ".$user->step." Old_step: ".$old_step);
522                       }
523                   }
524               }
525               $user->shm_sz = $shm_sz;
526               
527               shm_detach($shm);
528           }
529           
530           //  
531           // SHSPLIT: load users from the shared memory
532           //
533           return ($user);
534       } while (0);
535       
536       return (FALSE);
537   }
538   
539
540   static function save_data($user, $id) 
541   {
542       GLOBAL $sess;
543       
544       $shm =   FALSE;
545       
546       if (($tok = @ftok(FTOK_PATH."/user".$id, "C")) == -1) {
547           return (FALSE);
548       }
549       while ($user->shm_sz < SHM_DIMS_U_MAX) {
550           if (($shm = shm_attach($tok, $user->shm_sz)) == FALSE)
551               break;
552           
553           // log_only("PUT_VAR DI ".strlen(serialize($user)));
554           if (@shm_put_var($shm, $tok, $user) != FALSE) {
555               shm_detach($shm);
556               if ($user->sess != "")
557                   $user->save_step();
558
559               log_shme("User::save_data");
560
561               log_main("User[".$id."] saved.");
562               return (TRUE);
563           }
564           if (shm_remove($shm) === FALSE) {
565               log_only("REMOVE FALLITA");
566               break;
567           }
568           shm_detach($shm);
569           $user->shm_sz += SHM_DIMS_U_DLT;
570       } 
571       
572       if ($shm)
573           shm_detach($shm);
574       
575       return (FALSE);
576   }
577
578   function myname_innerHTML()
579   {
580       $class_id = ($this->flags & USER_FLAG_AUTH) + 1;
581       
582       return (sprintf('$("myname").innerHTML = "<span class=\"au%d\">%s</span>";', $class_id, 
583                       xcape($this->name,ENT_COMPAT,"UTF-8")));
584   }
585
586   /* INDEX_RD_IFRA PORT */
587
588   protected function blocking_error($is_unrecoverable)
589   {
590       GLOBAL $is_page_streaming;
591       
592       $is_page_streaming = TRUE;
593       log_crit("BLOCKING_ERROR UNREC: ".($is_unrecoverable ? "TRUE" : "FALSE"));
594       return (sprintf(($is_unrecoverable ? 'hstm.stop(); ' : '').'window.onbeforeunload = null; window.onunload = null; document.location.assign("index.php");'));
595   }
596   
597   // FIXME TO SUPPORT iframe
598   protected function page_sync($sess, $page, $table_idx, $table_token)
599   {
600       GLOBAL $is_page_streaming;
601       
602       // log_rd2("page_sync:".var_export(debug_backtrace()));
603       
604       $is_page_streaming = TRUE;
605       
606       log_rd2("PAGE_SYNC");
607       printf("xXx USER::PAGE_SYNC [%s]\n", get_class($this));
608       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));
609   }
610
611
612
613
614   protected function maincheck($cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step, $splashdate, $table_idx, $table_token)
615   {
616       GLOBAL $G_lang, $mlang_indrd, $is_page_streaming;
617       // GLOBAL $first_loop;
618       GLOBAL $G_with_splash, $G_splash_content, $G_splash_interval, $G_splash_idx;
619       GLOBAL $G_splash_w, $G_splash_h, $G_splash_timeout;
620       $CO_splashdate = "CO_splashdate".$G_splash_idx;
621       $$CO_splashdate = $splashdate;
622       
623       GLOBAL $S_load_stat;
624       
625       log_rd("maincheck begin");
626       
627       $ret = FALSE;
628       $curtime = time();
629       
630       /* Nothing changed, return. */
631       if ($cur_step == $this->step) 
632           return (FALSE);
633       
634       log_rd2("do other cur_stat[".$cur_stat."] user->stat[".$this->stat."] cur_step[".$cur_step."] user_step[".$this->step."]");
635       
636       if ($cur_step == -1) {
637           /*
638            *  if $cur_step == -1 load the current state from the main struct
639            */
640           
641           $S_load_stat['wR_minusone']++;
642           
643           if ($this->the_end == TRUE) { 
644               // log_rd2("main_check: the end".var_export(debug_backtrace()));
645               $is_page_streaming = TRUE;
646           }
647           
648           if ($this->trans_step != -1) {
649               log_rd2("TRANS USATO ".$this->trans_step);
650               $cur_step = $this->trans_step;
651               $this->trans_step = -1;
652           }
653           else {
654               log_rd2("TRANS NON ATTIVATO");
655           }
656       }
657       
658       
659       /* this part I suppose is read only on $this->room structure */
660       if ($cur_step == -1) {
661           log_rd2("PRE-NEWSTAT: ".$this->stat);
662           
663           if ($this->stat == 'room') {
664               log_rd("roomma ".$this->step);
665               $curtime = time();
666               
667               if ($G_with_splash &&
668                   ($$CO_splashdate < $curtime - $G_splash_interval ||
669                    $$CO_splashdate > $curtime)) {
670                   $is_super = $this->flags & USER_FLAG_TY_SUPER;
671                   $ret .=  show_notify_ex(str_replace("\n", " ", $G_splash_content[$G_lang]), 
672                                           ($is_super ? 0 : $G_splash_timeout), 
673                                           $mlang_indrd[($is_super ? 'btn_btotabsup' : 'btn_backtotab')][$G_lang], 
674                                           $G_splash_w, $G_splash_h, true, 
675                                           ($is_super ? 0 : $G_splash_timeout));
676                   $ret .= sprintf('|createCookie("CO_splashdate%d", %d, 24*365, cookiepath);', $G_splash_idx, $curtime);
677               }
678               $ret .= $this->room->show_room($this->step, $this);
679               
680               // TODO uncomment and test
681               /* NOTE the sets went common */
682               $new_stat =  $this->stat;
683               $new_subst = $this->subst;
684               $new_step =  $this->step;
685           }
686           /***************
687            *             *
688            *    TABLE    *
689            *             *
690            ***************/
691           else if ($this->stat == 'table') {
692               log_load("RESYNC");
693               printf("xXx USER::MAINCHECK1 [%s]\n", get_class($this));
694
695               return ($this->page_sync($this->sess, "briskin5/index.php", $this->table, $this->table_token));
696           }
697           log_rd2("NEWSTAT: ".$this->stat);
698       } /* if ($cur_step == -1) { */
699       else {
700           /* $sem = Room::lock_data(FALSE); */
701           $S_load_stat['rU_heavy']++;
702           
703           if ($cur_step < $this->step) {
704               do {
705                   if ($cur_step + COMM_N < $this->step) {
706                       if (($cur_stat != $this->stat)) {
707                           $to_stat = $this->stat;
708                           /* Room::unlock_data($sem); */
709                           log_load("RESYNC");
710                           printf("xXx USER::MAINCHECK2 [%s]\n", get_class($this));
711                           return ($this->page_sync($this->sess, ($to_stat == "table" ? "briskin5/index.php" : "index.php"), $this->table, $this->table_token));
712                       }
713                       log_rd2("lost history, refresh from scratch");
714                       $new_step = -1;
715                       break;
716                   } 
717                   for ($i = $cur_step ; $i < $this->step ; $i++) {
718                       $ii = $i % COMM_N;
719                       log_rd2("ADDED TO THE STREAM: ".$this->comm[$ii]);
720                       $ret .= $this->comm[$ii];
721                   }
722                   $new_stat =  $this->stat;
723                   $new_subst = $this->subst;
724                   $new_step =  $this->step;
725               } while (0);
726               
727               log_rd2($this->step, 'index_rd.php: after ret set');
728               
729               if ($this->the_end == TRUE) {
730                   log_rd2("LOGOUT BYE BYE!!");
731                   log_auth($this->sess, "Explicit logout.");
732                   
733                   if ($this->the_end == TRUE) {
734                       $this->reset();
735                       
736                       if ($this->subst == 'sitdown') {
737                           log_load("ROOM WAKEUP");
738                           $this->room->room_wakeup($this);
739                       }
740                       else if ($this->subst == 'standup')
741                           $this->room->room_outstandup($this);
742                       else
743                           log_rd2("LOGOUT FROM WHAT ???");
744                       
745                   } /* if ($this->the_end == TRUE) { ... */
746               } /* if ($this->the_end == TRUE) { ... */
747           } /* if ($cur_step < $this->step) { */
748           
749           /* Room::unlock_data($sem); */
750       }  /* else of if ($cur_step == -1) { */
751       
752     
753       return ($ret);
754   }  //   function maincheck (...
755
756   public static function stream_fini($init_string, $is_unrecoverable)
757 {
758     // IF IFRAME THEN:
759     $body = "";
760     $body .= sprintf("<html>
761 <head>
762 <script type=\"text/javascript\" src=\"%scommons.js\"></script>
763 <script type=\"text/javascript\" src=\"%sxynt-http-streaming-ifra.js\"></script>
764 <script type=\"text/javascript\">
765 var http_streaming = \"ready\";", self::base_get(), self::base_get());
766     $body .= sprintf("
767 window.onload = function () { if (http_streaming != \"ready\") { http_streaming.reload(); } };
768 </script>
769 </head>
770 <body>");
771     $body .= sprintf("<!-- \n%s -->\n", $init_string);
772     $body .= sprintf("<script id='hs%d' type='text/javascript'><!--
773 push(\"%s\");
774 // -->
775 </script>", 0, escpush(self::blocking_error($is_unrecoverable)) );
776     // ELSE IF XHR THEN:
777     // return (self::blocking_error($is_unrecoverable));
778     return ($body);
779 }
780
781 /*
782  *  MAIN
783  */
784
785 /*
786    FROM THE EXTERN 
787    sess
788    stat
789    step
790 */
791 function stream_init($init_string, $enc, &$header_out, &$body, $get, $post, $cookie)
792 {
793     $curtime = time();
794     
795     printf("CLASS: [%s] base: [%s]\n", get_class($this), self::base_get());
796     
797     $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);
798
799     if ($enc != 'plain')
800         $header_out['Content-Encoding'] = $enc;
801     $header_out['Cache-Control'] = 'no-cache, must-revalidate';     // HTTP/1.1
802     $header_out['Expires']       = 'Mon, 26 Jul 1997 05:00:00 GMT'; // Date in the past
803     $header_out['Content-type']  = 'text/html; charset="utf-8"';
804     
805     log_load("index_rd_ifra_init.php");
806     
807     if (($from  = gpcs_var('from', $get, $post, $cookie)) === FALSE)
808         $from = "";
809     if (($stat  = gpcs_var('stat', $get, $post, $cookie)) === FALSE) 
810         $stat = "";
811     if (($subst = gpcs_var('subst', $get, $post, $cookie)) === FALSE) 
812         $subst = "";
813     if (($step  = gpcs_var('step', $get, $post, $cookie)) === FALSE) 
814         unset($step);
815     
816     $this->rd_data_set($curtime, $enc, $stat, $subst, $step, $from);
817     $cc = get_called_class();
818     
819     $body .= sprintf("<html>
820 <head>
821 <script type=\"text/javascript\" src=\"%scommons.js\"></script>
822 <script type=\"text/javascript\" src=\"%sxynt-http-streaming-ifra.js\"></script>
823 <script type=\"text/javascript\">
824 var http_streaming = \"ready\";", self::base_get(), self::base_get());
825     if ($this->rd_scristp > 0)
826         $body .= sprintf("last_clean = %d;\n", ($this->rd_scristp-1));
827     $body .= sprintf("
828 window.onload = function () { if (http_streaming != \"ready\") { http_streaming.reload(); } };
829 </script> 
830 </head>
831 <body>");
832     $body .= sprintf("<!-- \n%s -->\n", $init_string);
833     
834     return TRUE;
835   }
836
837 function stream_main(&$body, $get, $post, $cookie)
838 {
839     GLOBAL $is_page_streaming, $G_splash_idx;
840
841     $CO_splashdate = "CO_splashdate".$G_splash_idx;
842     if (($splashdate = gpcs_var("$CO_splashdate", $get, $post, $cookie)) === FALSE)
843         $splashdate = ""; 
844     if (($table_idx = gpcs_var("table_idx", $get, $post, $cookie)) === FALSE)
845         $table_idx = ""; 
846     if (($table_token = gpcs_var("table_token", $get, $post, $cookie)) === FALSE)
847         $table_token = ""; 
848
849     $is_page_streaming = FALSE;
850     log_rd2("FROM OUTSIDE - STAT: ".$this->rd_stat." SUBST: ".$this->rd_subst." STEP: ".$this->rd_step." FROM: ".$this->rd_from. "IS_PAGE:" . $is_page_streaming);
851     
852     
853     $pre_main = gettimeofday(TRUE);
854     
855     $old_stat  = $this->rd_stat;
856     $old_subst = $this->rd_subst;
857     $old_step  = $this->rd_step;
858     printf("xXx PRE : rd_step %d\n", $this->rd_step);
859     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) {
860         $body .= sprintf("<script id='hs%d' type='text/javascript'><!--
861 push(\"%s\");
862 // -->
863 </script>", $this->rd_scristp++, escpush($ret) );
864         
865         log_rd2(0, 'index_rd.php: after mop_flush (begin: '.sprintf("%f", $pre_main).')');
866         if ($is_page_streaming) 
867             return TRUE;
868     }
869     printf("xXx POST: rd_step %d\n", $this->rd_step);
870
871     return TRUE;
872 }
873
874 function stream_keepalive($with_ping)
875 {
876     $ret =  (sprintf("<script id='hs%d' type='text/javascript'><!--
877 push(%s);
878 // -->
879 </script>", $this->rd_scristp++, ($with_ping ? "\"act_ping();\"" : "null")));
880
881     return ($ret);
882 }
883
884 static function base_get()
885 {
886     $c = get_called_class();
887     printf("CALLED_CLASS: [%s]\n", $c);
888     return $c::BASE;
889 }
890
891 } // end class User
892
893
894 ?>