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