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