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