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