2b28adb6c4a78764f564ce441bc729556fcd71f9
[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 // User flags
25 define('USER_FLAG_AUTH',     0x02);
26
27 define('USER_FLAG_MAP_AUTH', 0x0c);
28 define('USER_FLAG_LISTAUTH', 0x04);
29 define('USER_FLAG_ISOLAUTH', 0x08);
30
31 define('USER_FLAG_DBFAILED', 0x10);
32
33 //   user status
34 define('USER_FLAG_S_NORM',  0x000); // done
35 define('USER_FLAG_S_PAU',   0x100); // done
36 define('USER_FLAG_S_OUT',   0x200); // done
37 define('USER_FLAG_S_DOG',   0x300); // done
38 define('USER_FLAG_S_EAT',   0x400); // done
39 define('USER_FLAG_S_WRK',   0x500); // done
40 define('USER_FLAG_S_SMK',   0x600); // done
41 define('USER_FLAG_S_EYE',   0x700); // done
42 define('USER_FLAG_S_RABB',  0x800); // done
43 define('USER_FLAG_S_SOCC',  0x900); // done
44 define('USER_FLAG_S_BABY',  0xa00); // done
45 define('USER_FLAG_S_MOP',   0xb00); // done
46
47 define('USER_FLAG_S_ALL',   0xf00); // done
48
49 /* type of user normal, supporter etc ... */
50 define('USER_FLAG_TY_ALL',     0xff0000); // done
51 define('USER_FLAG_TY_NORM',    0x010000); // done
52 define('USER_FLAG_TY_SUPER',   0x020000); // done
53 //  ... other usefull status ...
54 define('USER_FLAG_TY_SUSPEND', 0x400000); // done
55 define('USER_FLAG_TY_DISABLE', 0x800000); // done
56
57 // 240 is the right value, 600 is for fwrite error test
58 define('RD_ENDTIME_DELTA',  240);
59 define('RD_KEEPALIVE_TOUT',   4);
60
61 $S_load_stat = array( 'rU_heavy'      => 0,
62                       'lL_laccgarb'   => 0,
63                       'wU_lacc_upd'   => 0,
64                       'wR_garbage'    => 0,
65                       'wR_minusone'   => 0,
66                       'wR_the_end'    => 0 );
67
68 $mlang_indrd = array( 
69                      'btn_backtotab'  => array('it' => ' torna ai tavoli ',
70                                                'en' => ' back to tables '),
71                      'btn_btotabsup'  => array('it' => ' grazie della donazione, torna ai tavoli ',
72                                                'en' => ' thank you for donation, back to tables ') 
73                      );
74
75 class User {
76   var $room;       // reference to the room where the user is registered
77   var $idx;        // index in the users array when you are in game
78   var $idx_orig;   // index in the users array when you aren't in game
79   var $code;       // authentication code
80   var $name;       // name of the user
81   var $sess;       // session of the user
82   var $ip;         // ip of the user
83   var $lacc;       // last access (for the cleanup)
84   var $laccwr;     // last access (for the cleanup)
85   var $bantime;    // timeout to temporary ban
86   var $stat;       // status (outdoor, room, table, game, ...)
87   var $subst;      // substatus for each status   
88   var $step;       // step of the current status
89   var $trans_step; // step to enable transition between pages (disable == -1)
90
91   var $rd_socket;  // socket handle of push stream
92   var $rd_endtime; // end time for push stream
93   var $rd_stat;    // actual status of push stream
94   var $rd_subst;   // actual substatus of push stream
95   var $rd_step;    // actual step of push stream
96   var $rd_from;    // referer
97   var $rd_scristp; // current script step (for each session) 
98   var $rd_kalive;  // if no message are sent after RD_KEEPALIVE_TOUT secs we send a keepalive from server
99   var $rd_cache;   // place where store failed fwrite data
100
101   var $comm;       // commands array
102   // var $asta_card;  // 
103   // var $asta_pnt;   //
104   // var $handpt;     // Total card points at the beginning of the current hand.
105   // var $exitislock; // Player can exit from the table ?
106
107   // FIXME: the table_orig field must be removed after table field verify of index management (in spawned table
108   //        it is allways ZERO
109   var $table;      // id of the current table when you are in game
110   var $table_orig; // id of the current table when you aren't in game
111   var $table_pos;  // idx on the table
112   var $table_token;// token that identify a game on a table
113   var $flags;      // Bitfield with: AUTHENTICATE: 0x02 
114   var $the_end;    // Flag to change the end of the session
115
116   var $chat_lst;      // Last chat line
117   var $chattime;      // Array of chat times
118   var $chat_cur;      // Current chat line number
119   var $chat_ban;      // Time for ban chat
120   var $chat_dlt;      // Delta t for ban
121   var $shm_sz;
122
123   const BASE = "";    // basepath for absolute web references
124
125   function User() {
126   }
127
128   function create(&$room, $idx, $name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
129     if (($thiz = new User()) == FALSE)
130       return (FALSE);
131
132     $thiz->room       = &$room;
133     $thiz->idx        = $idx;
134     $thiz->idx_orig   = $idx;
135     $thiz->code       = -1;
136     $thiz->name       = $name;
137     $thiz->sess       = $sess;
138     $thiz->ip         = $ip;
139     $thiz->lacc       = time();
140     $thiz->laccwr     = time();
141     $thiz->bantime    = 0;
142     $thiz->stat       = $stat;
143     $thiz->subst      = $subst;
144     $thiz->step       = 1;
145     $thiz->trans_step = -1;
146     $thiz->comm       = array();
147
148     $thiz->rd_socket  = NULL;
149     $thiz->rd_endtime = -1;
150     $thiz->rd_stat    = -1;
151     $thiz->rd_subst   = "";
152     $thiz->rd_step    = -1;
153     $thiz->rd_from    = "";
154     $thiz->rd_scristp = -1;
155     $thiz->rd_kalive  = -1;
156     $thiz->rd_cache   = "";
157
158     $thiz->asta_card  = -2;
159     $thiz->asta_pnt   = -1;
160     $thiz->handpt     = -1;
161     $thiz->exitislock = TRUE;
162
163     $thiz->flags      = 0x00;
164     
165     $thiz->chattime   = array_fill(0, CHAT_N, 0);
166     $thiz->chat_cur   = 0;
167     $thiz->chat_lst   = "";
168     $thiz->chat_ban   = 0;
169     $thiz->chat_dlt   = 0;
170
171     $thiz->table_orig = $table;
172     $thiz->table      = $table;
173     $thiz->table_pos  = -1;
174     $thiz->table_token= "";
175     $thiz->shm_sz = SHM_DIMS_U_MIN;
176     return ($thiz);
177   }
178
179   function copy($from)
180   {
181     $this->idx        = $from->idx;
182     $this->idx_orig   = $from->idx;
183     $this->code       = $from->code;
184     $this->name       = $from->name;
185     $this->sess       = $from->sess;
186     $this->ip         = $from->ip;
187     $this->lacc       = $from->lacc;
188     $this->laccwr     = $from->laccwr;
189     $this->bantime    = $from->bantime;
190     $this->stat       = $from->stat;
191     $this->subst      = $from->subst;
192     $this->step       = $from->step;
193     $this->trans_step = $from->trans_step;
194     $this->comm       = array();
195
196     $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N)); 
197     for ($i = $i_start ; $i < $from->step ; $i++) {
198         $ii = $i % COMM_N;
199         if (isset($from->comm[$ii])) {
200             $this->comm[$ii] = $from->comm[$ii];
201         }
202     }
203     $this->asta_card  = $from->asta_card;
204     $this->asta_pnt   = $from->asta_pnt;
205     $this->handpt     = $from->handpt;
206     $this->exitislock = $from->exitislock;
207
208     $this->flags      = $from->flags;
209
210     $this->chattime   = array();
211     for ($i = 0 ; $i < CHAT_N ; $i++)
212       $this->chattime[$i] = $from->chattime[$i];
213     $this->chat_cur   = $from->chat_cur;
214     $this->chat_lst   = $from->chat_lst;
215     $this->chat_ban   = $from->chat_ban;
216     $this->chat_dlt   = $from->chat_dlt;
217
218     $this->table_orig = $from->table_orig;
219     $this->table      = $from->table;
220     $this->table_pos  = $from->table_pos;
221     $this->table_token = $from->table_token;
222     $this->the_end    = $from->the_end;
223     $this->shm_sz     = $from->shm_sz;
224     return (TRUE);
225   }
226
227
228   function myclone($from)
229   {
230     if (($thiz = new User()) == FALSE)
231       return (FALSE);
232     
233     $thiz->copy($from);
234
235     return ($thiz);
236   }
237   
238   function spawn($from, $table, $table_pos)
239   {
240     if (($thiz = new User()) == FALSE)
241       return (FALSE);
242     
243     $thiz->idx        = $from->idx;
244     $thiz->idx_orig   = $from->idx;
245     $thiz->code       = $from->code;
246     $thiz->name       = $from->name;
247     $thiz->sess       = $from->sess;
248     $thiz->ip         = $from->ip;
249     $thiz->lacc       = $from->lacc;
250     $thiz->laccwr     = $from->laccwr;
251     $thiz->bantime    = $from->bantime;
252     $thiz->stat       = $from->stat;
253     $thiz->subst      = $from->subst;
254     $thiz->step       = $from->step;
255     $thiz->trans_step = $from->trans_step;
256     $thiz->comm       = array();
257
258     /*
259     $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N)); 
260     for ($i = $i_start ; $i < $from->step ; $i++) {
261       log_wr("TRY PUSH:".$i);
262       $ii = $i % COMM_N;
263       $thiz->comm[$ii]   = $from->comm[$ii];
264     }
265     */
266     $thiz->asta_card  = $from->asta_card;
267     $thiz->asta_pnt   = $from->asta_pnt;
268     $thiz->handpt     = $from->handpt;
269     $thiz->exitislock = $from->exitislock;
270     $thiz->the_end    = $from->the_end;
271
272     $thiz->flags      = $from->flags;
273
274     $thiz->chattime   = array_fill(0, CHAT_N, 0);
275     $thiz->chat_cur   = 0;
276     $thiz->chat_lst   = "";
277     $thiz->chat_ban   = 0;
278     $thiz->chat_dlt   = 0;
279
280
281     $thiz->table_orig = $table;
282     $thiz->table      = 0;
283     $thiz->table_pos  = $table_pos;
284     $thiz->table_token = $from->table_token;
285     $thiz->shm_sz      = $from->shm_sz;
286
287     return ($thiz);
288   }
289
290   function rd_data_set($curtime, $stat, $subst, $step, $from)
291   {
292       $this->rd_endtime = $curtime + RD_ENDTIME_DELTA;
293       $this->rd_stat    = $stat;
294       $this->rd_subst   = $subst;
295       $this->rd_step    = $step;
296       $this->rd_from    = $from;
297       $this->rd_scristp = 0;
298       $this->rd_kalive  = $curtime + RD_KEEPALIVE_TOUT;
299   }
300
301   function rd_socket_get() {
302       return ($this->rd_socket);
303   }
304
305   function rd_socket_set($sock) {
306       $this->rd_socket = $sock;
307   }
308
309   function rd_kalive_get()
310   {
311       return ($this->rd_kalive);
312   }
313
314   function rd_kalive_set($tm)
315   {
316       $this->rd_kalive = $tm;
317   }
318
319   function rd_kalive_is_expired($tm)
320   {
321       // printf("rd_kalive %d tm %d\n", $this->rd_kalive, $tm);
322       return ($this->rd_kalive < $tm);
323   }
324
325   function rd_endtime_is_expired($tm)
326   {
327       // printf("rd_endtime %d tm %d\n", $this->rd_kalive, $tm);
328       return ($this->rd_endtime < $tm);
329   }
330
331   function rd_kalive_reset($tm)
332   {
333       $this->rd_kalive = $tm + RD_KEEPALIVE_TOUT;
334   }
335
336   function rd_cache_get()
337   {
338       return ($this->rd_cache);
339   }
340
341   function rd_cache_set($cache)
342   {
343       $this->rd_cache = $cache;
344   }
345
346   function idx_get() {
347       return ($this->idx);
348   }
349
350   function code_get() {
351       return ($this->code);
352   }
353   
354   function stat_set($stat) {
355     log_main("sess: [".$this->sess. "] NEW STAT: [".$stat."]"); 
356     $this->stat = "$stat";
357     
358     /*
359     if (validate_sess($this->sess)) {
360       if (file_exists(PROXY_PATH) == FALSE)
361         mkdir(PROXY_PATH, 0775, TRUE);
362       $fp = @fopen(PROXY_PATH."/".$this->sess.".stat", 'w');
363       fwrite($fp, sprintf("%s\n",$this->stat));
364       fclose($fp);
365     }
366     */
367   }
368
369   function step_set($step) 
370   {
371       $this->step = $step & 0x7fffffff;
372       
373       return (TRUE);
374   }
375
376   function step_inc($delta = 1) {
377       $this->step += $delta;
378       /* modularization because unpack() not manage unsigned 32bit int correctly */
379       $this->step &= 0x7fffffff;
380       
381       return TRUE;
382   }
383
384
385   function save_step() 
386   {
387       do {
388           if (validate_sess($this->sess) == FALSE)
389               break;
390           if (file_exists(PROXY_PATH) == FALSE)
391               mkdir(PROXY_PATH, 0775, TRUE);
392           if (($fp = @fopen(PROXY_PATH."/".$this->sess.".step", 'w')) == FALSE)
393               break;
394           fwrite($fp, pack("LL",$this->step, $this->idx));
395           fclose($fp);
396           
397           log_main("step_set [".$this->sess. "] [".$this->step."]"); 
398           
399           return (TRUE);
400       } while (0);
401       
402       return (FALSE);
403   }
404   
405   static function load_step($sess) 
406   {
407       $fp = FALSE;
408       do {
409           if (validate_sess($sess) == FALSE)
410               break;
411           
412           if (file_exists(PROXY_PATH) == FALSE)
413               mkdir(PROXY_PATH, 0775, TRUE);
414           if (($fp = @fopen(PROXY_PATH."/".$sess.".step", 'rb')) == FALSE)
415               break;
416           if (($s = fread($fp, 8)) == FALSE)
417               break;
418           if (mb_strlen($s, "ASCII") != 8)
419               break;
420           $arr = unpack('Ls/Li', $s);
421           fclose($fp);
422           
423           // log_rd2("A0: ".$arr[0]."  A1: ".$arr[1]);
424           return ($arr);
425       } while (0);
426       
427       if ($fp != FALSE)
428           fclose($fp);
429       
430       log_rd2("STEP_GET [".$sess."]: return false ");
431       
432       return (FALSE);
433   }
434
435   static function unproxy_step($sess) {
436       log_rd2("UNPROXY: ".PROXY_PATH."/".$sess.".step");
437       if (file_exists(PROXY_PATH) == FALSE)
438           return;
439       @unlink(PROXY_PATH."/".$sess.".step");
440   }
441
442   function reset() {
443     $curtime = time();
444     log_legal($curtime, 'xxx', $this, "STAT:LOGOUT", '');
445
446     $tmp_sess = $this->sess;
447     $this->sess = "";
448     self::unproxy_step($tmp_sess);
449     $this->name = "";  // OK here
450     while (array_pop($this->comm) != NULL);
451     $this->step = 0;
452     $this->chattime = array_fill(0, CHAT_N, 0);
453     $this->chat_cur = 0;
454     $this->chat_lst = "";
455     $this->chat_ban = 0;
456     $this->chat_dlt = 0;
457     $this->the_end = FALSE;
458   }
459
460   static function load_data($id, $sess) 
461   {
462       log_main("load_data: id [".$id."] sess [".($sess == FALSE ? "FALSE" : $sess)."] ");
463       
464       do {
465           if (($tok = @ftok(FTOK_PATH."/user".$id, "C")) == -1) {
466               log_main("ftok failed");
467               break;
468           }
469           
470           if (($shm_sz = sharedmem_sz($tok)) == -1) {
471               log_main("shmop_open failed");
472           }
473           
474           if ($shm_sz == -1)
475               $shm_sz = SHM_DIMS_U_MIN;
476           
477           if ($shm = shm_attach($tok, $shm_sz)) {
478               if (($user = @shm_get_var($shm, $tok)) == FALSE) {
479                   break;
480               }
481               if ($sess != FALSE && $user->sess != $sess) {
482                   break;
483               }
484               log_only("user ==  ".($user == FALSE ?   "FALSE" : "TRUE")."  user ===  ".($user === FALSE ? "FALSE" : "TRUE")."  user isset ".(isset($user) ?   "TRUE" : "FALSE"));
485               
486               if ($user == FALSE) {
487                   log_only("INIT MAIN DATA");
488                   
489                   // SHSPLIT FIXME: init_data for User class ??
490                   $user = self::create($id, "", "");
491                   if (@shm_put_var($shm, $tok, $user) == FALSE) {
492                       log_only("PUT_VAR FALLITA ".strlen(serialize($user)));
493                       log_only(serialize($user));
494                   }
495                   log_shme("User::save_data2");
496               }
497               else {
498                   if ($sess != FALSE) {
499                       /*
500                        *  NOTE:  this part is for check only, theoretically 
501                        *         user->step anch proxy_step are set allways at the same value
502                        */
503                       $old_step = $user->step;
504                       $arr = self::load_step($sess);
505                       $user->step = $arr['s'];
506                       if ($old_step != $user->step) {
507                           log_crit("steps are diffetents User->step ".$user->step." Old_step: ".$old_step);
508                       }
509                   }
510               }
511               $user->shm_sz = $shm_sz;
512               
513               shm_detach($shm);
514           }
515           
516           //  
517           // SHSPLIT: load users from the shared memory
518           //
519           return ($user);
520       } while (0);
521       
522       return (FALSE);
523   }
524   
525
526   static function save_data($user, $id) 
527   {
528       GLOBAL $sess;
529       
530       $shm =   FALSE;
531       
532       if (($tok = @ftok(FTOK_PATH."/user".$id, "C")) == -1) {
533           return (FALSE);
534       }
535       while ($user->shm_sz < SHM_DIMS_U_MAX) {
536           if (($shm = shm_attach($tok, $user->shm_sz)) == FALSE)
537               break;
538           
539           // log_only("PUT_VAR DI ".strlen(serialize($user)));
540           if (@shm_put_var($shm, $tok, $user) != FALSE) {
541               shm_detach($shm);
542               if ($user->sess != "")
543                   $user->save_step();
544
545               log_shme("User::save_data");
546
547               log_main("User[".$id."] saved.");
548               return (TRUE);
549           }
550           if (shm_remove($shm) === FALSE) {
551               log_only("REMOVE FALLITA");
552               break;
553           }
554           shm_detach($shm);
555           $user->shm_sz += SHM_DIMS_U_DLT;
556       } 
557       
558       if ($shm)
559           shm_detach($shm);
560       
561       return (FALSE);
562   }
563
564   function myname_innerHTML()
565   {
566       $class_id = ($this->flags & USER_FLAG_AUTH) + 1;
567       
568       return (sprintf('$("myname").innerHTML = "<span class=\"au%d\">%s</span>";', $class_id, 
569                       xcape($this->name,ENT_COMPAT,"UTF-8")));
570   }
571
572   /* INDEX_RD_IFRA PORT */
573
574   protected function blocking_error($is_unrecoverable)
575   {
576       GLOBAL $is_page_streaming;
577       
578       $is_page_streaming = TRUE;
579       log_crit("BLOCKING_ERROR UNREC: ".($is_unrecoverable ? "TRUE" : "FALSE"));
580       return (sprintf(($is_unrecoverable ? 'hstm.stop(); ' : '').'window.onbeforeunload = null; window.onunload = null; document.location.assign("index.php");'));
581   }
582   
583   // FIXME TO SUPPORT iframe
584   protected function page_sync($sess, $page, $table_idx, $table_token)
585   {
586       GLOBAL $is_page_streaming;
587       
588       // log_rd2("page_sync:".var_export(debug_backtrace()));
589       
590       $is_page_streaming = TRUE;
591       
592       log_rd2("PAGE_SYNC");
593       printf("xXx USER::PAGE_SYNC [%s]\n", get_class($this));
594       return (sprintf('createCookie("table_idx", %d, 24*365, cookiepath); createCookie("table_token", "%s", 24*365, cookiepath); hstm.stop(); window.onunload = null; window.onbeforeunload = null; document.location.assign("%s");', $table_idx, $table_token, $page));
595   }
596
597
598
599
600   protected function maincheck($cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step, $splashdate, $table_idx, $table_token)
601   {
602       GLOBAL $G_lang, $mlang_indrd, $is_page_streaming;
603       // GLOBAL $first_loop;
604       GLOBAL $G_with_splash, $G_splash_content, $G_splash_interval, $G_splash_idx;
605       GLOBAL $G_splash_w, $G_splash_h, $G_splash_timeout;
606       $CO_splashdate = "CO_splashdate".$G_splash_idx;
607       $$CO_splashdate = $splashdate;
608       
609       GLOBAL $S_load_stat;
610       
611       log_rd("maincheck begin");
612       
613       $ret = FALSE;
614       $curtime = time();
615       
616       /* Nothing changed, return. */
617       if ($cur_step == $this->step) 
618           return (FALSE);
619       
620       log_rd2("do other cur_stat[".$cur_stat."] user->stat[".$this->stat."] cur_step[".$cur_step."] user_step[".$this->step."]");
621       
622       if ($cur_step == -1) {
623           /*
624            *  if $cur_step == -1 load the current state from the main struct
625            */
626           
627           $S_load_stat['wR_minusone']++;
628           
629           if ($this->the_end == TRUE) { 
630               // log_rd2("main_check: the end".var_export(debug_backtrace()));
631               $is_page_streaming = TRUE;
632           }
633           
634           if ($this->trans_step != -1) {
635               log_rd2("TRANS USATO ".$this->trans_step);
636               $cur_step = $this->trans_step;
637               $this->trans_step = -1;
638           }
639           else {
640               log_rd2("TRANS NON ATTIVATO");
641           }
642       }
643       
644       
645       /* this part I suppose is read only on $this->room structure */
646       if ($cur_step == -1) {
647           log_rd2("PRE-NEWSTAT: ".$this->stat);
648           
649           if ($this->stat == 'room') {
650               log_rd("roomma ".$this->step);
651               $curtime = time();
652               
653               if ($G_with_splash &&
654                   ($$CO_splashdate < $curtime - $G_splash_interval ||
655                    $$CO_splashdate > $curtime)) {
656                   $is_super = $this->flags & USER_FLAG_TY_SUPER;
657                   $ret .=  show_notify_ex(str_replace("\n", " ", $G_splash_content[$G_lang]), 
658                                           ($is_super ? 0 : $G_splash_timeout), 
659                                           $mlang_indrd[($is_super ? 'btn_btotabsup' : 'btn_backtotab')][$G_lang], 
660                                           $G_splash_w, $G_splash_h, true, 
661                                           ($is_super ? 0 : $G_splash_timeout));
662                   $ret .= sprintf('|createCookie("CO_splashdate%d", %d, 24*365, cookiepath);', $G_splash_idx, $curtime);
663               }
664               $ret .= $this->room->show_room($this->step, $this);
665               
666               // TODO uncomment and test
667               /* NOTE the sets went common */
668               $new_stat =  $this->stat;
669               $new_subst = $this->subst;
670               $new_step =  $this->step;
671           }
672           /***************
673            *             *
674            *    TABLE    *
675            *             *
676            ***************/
677           else if ($this->stat == 'table') {
678               log_load("RESYNC");
679               printf("xXx USER::MAINCHECK1 [%s]\n", get_class($this));
680
681               return ($this->page_sync($this->sess, "briskin5/index.php", $this->table, $this->table_token));
682           }
683           log_rd2("NEWSTAT: ".$this->stat);
684       } /* if ($cur_step == -1) { */
685       else {
686           /* $sem = Room::lock_data(FALSE); */
687           $S_load_stat['rU_heavy']++;
688           
689           if ($cur_step < $this->step) {
690               do {
691                   if ($cur_step + COMM_N < $this->step) {
692                       if (($cur_stat != $this->stat)) {
693                           $to_stat = $this->stat;
694                           /* Room::unlock_data($sem); */
695                           log_load("RESYNC");
696                           printf("xXx USER::MAINCHECK2 [%s]\n", get_class($this));
697                           return ($this->page_sync($this->sess, ($to_stat == "table" ? "briskin5/index.php" : "index.php"), $this->table, $this->table_token));
698                       }
699                       log_rd2("lost history, refresh from scratch");
700                       $new_step = -1;
701                       break;
702                   } 
703                   for ($i = $cur_step ; $i < $this->step ; $i++) {
704                       $ii = $i % COMM_N;
705                       log_rd2("ADDED TO THE STREAM: ".$this->comm[$ii]);
706                       $ret .= $this->comm[$ii];
707                   }
708                   $new_stat =  $this->stat;
709                   $new_subst = $this->subst;
710                   $new_step =  $this->step;
711               } while (0);
712               
713               log_rd2($this->step, 'index_rd.php: after ret set');
714               
715               if ($this->the_end == TRUE) {
716                   log_rd2("LOGOUT BYE BYE!!");
717                   log_auth($this->sess, "Explicit logout.");
718                   
719                   if ($this->the_end == TRUE) {
720                       $this->reset();
721                       
722                       if ($this->subst == 'sitdown') {
723                           log_load("ROOM WAKEUP");
724                           $this->room->room_wakeup($this);
725                       }
726                       else if ($this->subst == 'standup')
727                           $this->room->room_outstandup($this);
728                       else
729                           log_rd2("LOGOUT FROM WHAT ???");
730                       
731                   } /* if ($this->the_end == TRUE) { ... */
732               } /* if ($this->the_end == TRUE) { ... */
733           } /* if ($cur_step < $this->step) { */
734           
735           /* Room::unlock_data($sem); */
736       }  /* else of if ($cur_step == -1) { */
737       
738     
739       return ($ret);
740   }  //   function maincheck (...
741
742   public static function stream_fini($init_string, $is_unrecoverable)
743 {
744     // IF IFRAME THEN:
745     $body = "";
746     $body .= sprintf("<html>
747 <head>
748 <script type=\"text/javascript\" src=\"%scommons.js\"></script>
749 <script type=\"text/javascript\" src=\"%sxynt-http-streaming-ifra.js\"></script>
750 <script type=\"text/javascript\">
751 var http_streaming = \"ready\";", self::base_get(), self::base_get());
752     $body .= sprintf("
753 window.onload = function () { if (http_streaming != \"ready\") { http_streaming.reload(); } };
754 </script>
755 </head>
756 <body>");
757     $body .= sprintf("<!-- \n%s -->\n", $init_string);
758     $body .= sprintf("<script id='hs%d' type='text/javascript'><!--
759 push(\"%s\");
760 // -->
761 </script>", 0, escpush(self::blocking_error($is_unrecoverable)) );
762     // ELSE IF XHR THEN:
763     // return (self::blocking_error($is_unrecoverable));
764     return ($body);
765 }
766
767 /*
768  *  MAIN
769  */
770
771 /*
772    FROM THE EXTERN 
773    sess
774    stat
775    step
776 */
777 function stream_init($init_string, &$header_out, &$body, $get, $post, $cookie)
778 {
779     $curtime = time();
780
781     printf("CLASS: [%s] base: [%s]\n", get_class($this), self::base_get());
782
783     $is_page_streaming = FALSE; // (webservers_exceeded() || stristr($HTTP_USER_AGENT, "Mozilla/5.0 (Windows NT 6.1; rv:5.0)") || stristr($HTTP_USER_AGENT, "MSIE") || stristr($HTTP_USER_AGENT, "CHROME") ? TRUE : FALSE);
784
785     $header_out['Cache-Control'] = 'no-cache, must-revalidate';     // HTTP/1.1
786     $header_out['Expires']       = 'Mon, 26 Jul 1997 05:00:00 GMT'; // Date in the past
787     $header_out['Content-type']  = 'text/html; charset="utf-8"';
788
789     log_load("index_rd_ifra_init.php");
790
791     if (($from  = gpcs_var('from', $get, $post, $cookie)) === FALSE)
792         $from = "";
793     if (($stat  = gpcs_var('stat', $get, $post, $cookie)) === FALSE) 
794         $stat = "";
795     if (($subst = gpcs_var('subst', $get, $post, $cookie)) === FALSE) 
796         $subst = "";
797     if (($step  = gpcs_var('step', $get, $post, $cookie)) === FALSE) 
798         unset($step);
799     
800     $this->rd_data_set($curtime, $stat, $subst, $step, $from);
801     $cc = get_called_class();
802
803     $body .= sprintf("<html>
804 <head>
805 <script type=\"text/javascript\" src=\"%scommons.js\"></script>
806 <script type=\"text/javascript\" src=\"%sxynt-http-streaming-ifra.js\"></script>
807 <script type=\"text/javascript\">
808 var http_streaming = \"ready\";", self::base_get(), self::base_get());
809     if ($this->rd_scristp > 0)
810         $body .= sprintf("last_clean = %d;\n", ($this->rd_scristp-1));
811     $body .= sprintf("
812 window.onload = function () { if (http_streaming != \"ready\") { http_streaming.reload(); } };
813 </script> 
814 </head>
815 <body>");
816     $body .= sprintf("<!-- \n%s -->\n", $init_string);
817
818     return TRUE;
819 }
820
821 function stream_main(&$body, $get, $post, $cookie)
822 {
823     GLOBAL $is_page_streaming, $G_splash_idx;
824
825     $CO_splashdate = "CO_splashdate".$G_splash_idx;
826     if (($splashdate = gpcs_var("$CO_splashdate", $get, $post, $cookie)) === FALSE)
827         $splashdate = ""; 
828     if (($table_idx = gpcs_var("table_idx", $get, $post, $cookie)) === FALSE)
829         $table_idx = ""; 
830     if (($table_token = gpcs_var("table_token", $get, $post, $cookie)) === FALSE)
831         $table_token = ""; 
832
833     $is_page_streaming = FALSE;
834     log_rd2("FROM OUTSIDE - STAT: ".$this->rd_stat." SUBST: ".$this->rd_subst." STEP: ".$this->rd_step." FROM: ".$this->rd_from. "IS_PAGE:" . $is_page_streaming);
835     
836     
837     $pre_main = gettimeofday(TRUE);
838     
839     $old_stat  = $this->rd_stat;
840     $old_subst = $this->rd_subst;
841     $old_step  = $this->rd_step;
842     printf("xXx PRE : rd_step %d\n", $this->rd_step);
843     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) {
844         $body .= sprintf("<script id='hs%d' type='text/javascript'><!--
845 push(\"%s\");
846 // -->
847 </script>", $this->rd_scristp++, escpush($ret) );
848         
849         log_rd2(0, 'index_rd.php: after mop_flush (begin: '.sprintf("%f", $pre_main).')');
850         if ($is_page_streaming) 
851             return TRUE;
852     }
853     printf("xXx POST: rd_step %d\n", $this->rd_step);
854
855     return TRUE;
856 }
857
858 function stream_keepalive($with_ping)
859 {
860     $ret =  (sprintf("<script id='hs%d' type='text/javascript'><!--
861 push(%s);
862 // -->
863 </script>", $this->rd_scristp++, ($with_ping ? "\"act_ping();\"" : "null")));
864
865     return ($ret);
866 }
867
868 static function base_get()
869 {
870     $c = get_called_class();
871     printf("CALLED_CLASS: [%s]\n", $c);
872     return $c::BASE;
873 }
874
875 } // end class User
876
877
878 ?>