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