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