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