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