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