5 * Copyright (C) 2012 Matteo Nastasi
6 * mailto: nastasi@alternativeoutput.it
7 * matteo.nastasi@milug.org
8 * web: http://www.alternativeoutput.it
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.
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.
24 require_once("${G_base}Obj/transports.phh");
28 define('USER_FLAG_AUTH', 0x02);
30 define('USER_FLAG_MAP_AUTH', 0x0c);
31 define('USER_FLAG_LISTAUTH', 0x04);
32 define('USER_FLAG_ISOLAUTH', 0x08);
34 define('USER_FLAG_DBFAILED', 0x10);
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
54 define('USER_FLAG_S_ALL', 0xf00); // done
56 /* type of user normal, supporter etc ... */
57 define('USER_FLAG_TY_ALL', 0xff0000); // done
58 define('USER_FLAG_TY_NORM', 0x010000); // done
59 define('USER_FLAG_TY_SUPER', 0x020000); // done
60 define('USER_FLAG_TY_CERT', 0x040000); // done
61 // ... other usefull status ...
62 define('USER_FLAG_TY_FIRONLY', 0x200000); // done
63 define('USER_FLAG_TY_SUSPEND', 0x400000); // done
64 define('USER_FLAG_TY_DISABLE', 0x800000); // done
66 // 240 is the right value, 600 is for fwrite error test
67 define('RD_ENDTIME_DELTA', 240);
68 define('RD_KEEPALIVE_TOUT', 4);
70 $S_load_stat = array( 'rU_heavy' => 0,
78 'btn_backtotab' => array('it' => ' torna ai tavoli ',
79 'en' => ' back to tables '),
80 'btn_btotabsup' => array('it' => ' grazie della donazione, torna ai tavoli ',
81 'en' => ' thank you for donation, back to tables ')
85 var $room; // reference to the room where the user is registered
86 var $idx; // index in the users array when you are in game
87 var $idx_orig; // index in the users array when you aren't in game
88 var $code; // authentication code
89 var $name; // name of the user
90 var $sess; // session of the user
91 var $ip; // ip of the user
92 var $lacc; // last access (for the cleanup)
93 var $laccwr; // last access (for the cleanup)
94 var $bantime; // timeout to temporary ban
95 var $stat; // status (outdoor, room, table, game, ...)
96 var $subst; // substatus for each status
97 var $step; // step of the current status
98 var $trans_step; // step to enable transition between pages (disable == -1)
100 var $rd_socket; // socket handle of push stream
101 var $rd_endtime; // end time for push stream
102 var $rd_stat; // actual status of push stream
103 var $rd_subst; // actual substatus of push stream
104 var $rd_step; // actual step of push stream
105 var $rd_from; // referer
106 var $rd_scristp; // current script step (for each session)
107 var $rd_kalive; // if no message are sent after RD_KEEPALIVE_TOUT secs we send a keepalive from server
108 var $rd_cache; // place where store failed fwrite data
109 var $rd_zls; // zlibstream object handle if compressed stream, else FALSE
110 var $rd_transp; // class that define stream encapsulation type (iframe, xhr, ...)
112 var $comm; // commands array
113 // var $asta_card; //
115 // var $handpt; // Total card points at the beginning of the current hand.
116 // var $exitislock; // Player can exit from the table ?
118 // FIXME: the table_orig field must be removed after table field verify of index management (in spawned table
119 // it is allways ZERO
120 var $table; // id of the current table when you are in game
121 var $table_orig; // id of the current table when you aren't in game
122 var $table_pos; // idx on the table
123 var $table_token;// token that identify a game on a table
124 var $flags; // Bitfield with: AUTHENTICATE: 0x02
125 var $rec; // field with user db record or FALSE
126 var $the_end; // Flag to change the end of the session
128 var $chat_lst; // Last chat line
129 var $chattime; // Array of chat times
130 var $chat_cur; // Current chat line number
131 var $chat_ban; // Time for ban chat
132 var $chat_dlt; // Delta t for ban
135 const BASE = ""; // basepath for absolute web references
140 static function create(&$room, $idx, $name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
141 if (($thiz = new User()) == FALSE)
144 $thiz->room = &$room;
146 $thiz->idx_orig = $idx;
151 $thiz->lacc = time();
152 $thiz->laccwr = time();
155 $thiz->subst = $subst;
157 $thiz->trans_step = -1;
158 $thiz->comm = array();
160 $thiz->rd_socket = NULL;
161 $thiz->rd_endtime = -1;
163 $thiz->rd_subst = "";
166 $thiz->rd_scristp = -1;
167 $thiz->rd_kalive = -1;
168 $thiz->rd_cache = "";
169 $thiz->rd_zls = FALSE;
170 $thiz->rd_transp = NULL;
172 $thiz->asta_card = -2;
173 $thiz->asta_pnt = -1;
175 $thiz->exitislock = TRUE;
180 $thiz->chattime = array_fill(0, CHAT_N, 0);
182 $thiz->chat_lst = "";
186 $thiz->table_orig = $table;
187 $thiz->table = $table;
188 $thiz->table_pos = -1;
189 $thiz->table_token= "";
190 $thiz->shm_sz = SHM_DIMS_U_MIN;
196 $this->idx = $from->idx;
197 $this->idx_orig = $from->idx;
198 $this->code = $from->code;
199 $this->name = $from->name;
200 $this->sess = $from->sess;
201 $this->ip = $from->ip;
202 $this->lacc = $from->lacc;
203 $this->laccwr = $from->laccwr;
204 $this->bantime = $from->bantime;
205 $this->stat = $from->stat;
206 $this->subst = $from->subst;
207 $this->step = $from->step;
208 $this->trans_step = $from->trans_step;
209 $this->comm = array();
211 $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N));
212 for ($i = $i_start ; $i < $from->step ; $i++) {
214 if (isset($from->comm[$ii])) {
215 $this->comm[$ii] = $from->comm[$ii];
218 $this->asta_card = $from->asta_card;
219 $this->asta_pnt = $from->asta_pnt;
220 $this->handpt = $from->handpt;
221 $this->exitislock = $from->exitislock;
223 $this->flags = $from->flags;
224 $this->rec = $from->rec;
226 $this->chattime = array();
227 for ($i = 0 ; $i < CHAT_N ; $i++)
228 $this->chattime[$i] = $from->chattime[$i];
229 $this->chat_cur = $from->chat_cur;
230 $this->chat_lst = $from->chat_lst;
231 $this->chat_ban = $from->chat_ban;
232 $this->chat_dlt = $from->chat_dlt;
234 $this->table_orig = $from->table_orig;
235 $this->table = $from->table;
236 $this->table_pos = $from->table_pos;
237 $this->table_token = $from->table_token;
238 $this->the_end = $from->the_end;
239 $this->shm_sz = $from->shm_sz;
244 static function myclone($from)
246 if (($thiz = new User()) == FALSE)
254 static function spawn($from, $table, $table_pos)
256 if (($thiz = new User()) == FALSE)
259 $thiz->idx = $from->idx;
260 $thiz->idx_orig = $from->idx;
261 $thiz->code = $from->code;
262 $thiz->name = $from->name;
263 $thiz->sess = $from->sess;
264 $thiz->ip = $from->ip;
265 $thiz->lacc = $from->lacc;
266 $thiz->laccwr = $from->laccwr;
267 $thiz->bantime = $from->bantime;
268 $thiz->stat = $from->stat;
269 $thiz->subst = $from->subst;
270 $thiz->step = $from->step;
271 $thiz->trans_step = $from->trans_step;
272 $thiz->comm = array();
275 $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N));
276 for ($i = $i_start ; $i < $from->step ; $i++) {
277 log_wr("TRY PUSH:".$i);
279 $thiz->comm[$ii] = $from->comm[$ii];
282 $thiz->asta_card = $from->asta_card;
283 $thiz->asta_pnt = $from->asta_pnt;
284 $thiz->handpt = $from->handpt;
285 $thiz->exitislock = $from->exitislock;
286 $thiz->the_end = $from->the_end;
288 $thiz->flags = $from->flags;
289 $thiz->rec = $from->rec;
291 $thiz->chattime = array_fill(0, CHAT_N, 0);
293 $thiz->chat_lst = "";
298 $thiz->table_orig = $table;
300 $thiz->table_pos = $table_pos;
301 $thiz->table_token = $from->table_token;
302 $thiz->shm_sz = $from->shm_sz;
307 function flags_set($flags, $mask)
309 $flags_old = $this->flags & (~$mask);
310 $this->flags = ($flags_old | ($flags & $mask));
315 if (($bdb = BriskDB::create()) == FALSE) {
318 return ($bdb->user_prefs_update($this->code, ($this->flags & (USER_FLAG_TY_ALL | USER_FLAG_MAP_AUTH)),
319 $this->rec->supp_comp));
322 function rd_data_set($curtime, $transp, $enc, $stat, $subst, $step, $from)
324 $this->rd_endtime = $curtime + RD_ENDTIME_DELTA;
325 $this->rd_stat = $stat;
326 $this->rd_subst = $subst;
327 $this->rd_step = $step;
328 $this->rd_from = $from;
329 $this->rd_scristp = 0;
330 $this->rd_kalive = $curtime + RD_KEEPALIVE_TOUT;
331 $this->rd_zls = ZLibStream::create($enc);
332 $this->rd_transp = Transport::create($transp);
335 function rd_socket_get() {
336 return ($this->rd_socket);
339 function rd_socket_set($sock) {
342 $this->rd_zls->destroy();
343 $this->rd_zls = FALSE;
346 $this->rd_socket = $sock;
349 function rd_kalive_get()
351 return ($this->rd_kalive);
354 function rd_kalive_set($tm)
356 $this->rd_kalive = $tm;
359 function rd_kalive_is_expired($tm)
361 // printf("rd_kalive %d tm %d\n", $this->rd_kalive, $tm);
362 return ($this->rd_kalive < $tm);
365 function rd_endtime_is_expired($tm)
367 // printf("rd_endtime %d tm %d\n", $this->rd_kalive, $tm);
368 return ($this->rd_endtime < $tm);
371 function rd_kalive_reset($tm)
373 $this->rd_kalive = $tm + RD_KEEPALIVE_TOUT;
376 function rd_cache_get()
378 return ($this->rd_cache);
381 function rd_cache_set($cache)
383 $this->rd_cache = $cache;
386 function rd_zls_get()
388 return ($this->rd_zls);
395 function code_get() {
396 return ($this->code);
399 function stat_set($stat) {
400 log_main("sess: [".$this->sess. "] NEW STAT: [".$stat."]");
401 $this->stat = "$stat";
404 if (validate_sess($this->sess)) {
405 if (file_exists(PROXY_PATH) == FALSE)
406 mkdir(PROXY_PATH, 0775, TRUE);
407 $fp = @fopen(PROXY_PATH."/".$this->sess.".stat", 'w');
408 fwrite($fp, sprintf("%s\n",$this->stat));
414 function step_set($step)
416 $this->step = $step & 0x7fffffff;
421 function step_inc($delta = 1) {
422 $this->step += $delta;
423 /* modularization because unpack() not manage unsigned 32bit int correctly */
424 $this->step &= 0x7fffffff;
433 if (validate_sess($this->sess) == FALSE)
435 if (file_exists(PROXY_PATH) == FALSE)
436 mkdir(PROXY_PATH, 0775, TRUE);
437 if (($fp = @fopen(PROXY_PATH."/".$this->sess.".step", 'w')) == FALSE)
439 fwrite($fp, pack("LL",$this->step, $this->idx));
442 log_main("step_set [".$this->sess. "] [".$this->step."]");
450 static function load_step($sess)
454 if (validate_sess($sess) == FALSE)
457 if (file_exists(PROXY_PATH) == FALSE)
458 mkdir(PROXY_PATH, 0775, TRUE);
459 if (($fp = @fopen(PROXY_PATH."/".$sess.".step", 'rb')) == FALSE)
461 if (($s = fread($fp, 8)) == FALSE)
463 if (mb_strlen($s, "ASCII") != 8)
465 $arr = unpack('Ls/Li', $s);
468 // log_rd2("A0: ".$arr[0]." A1: ".$arr[1]);
475 log_rd2("STEP_GET [".$sess."]: return false ");
480 static function unproxy_step($sess) {
481 log_rd2("UNPROXY: ".PROXY_PATH."/".$sess.".step");
482 if (file_exists(PROXY_PATH) == FALSE)
484 @unlink(PROXY_PATH."/".$sess.".step");
489 log_legal($curtime, $this->ip, $this, "STAT:LOGOUT", '');
491 $tmp_sess = $this->sess;
493 self::unproxy_step($tmp_sess);
494 $this->name = ""; // OK here
495 while (array_pop($this->comm) != NULL);
497 $this->chattime = array_fill(0, CHAT_N, 0);
499 $this->chat_lst = "";
502 $this->the_end = FALSE;
505 function myname_innerHTML()
507 $class_id = ($this->flags & USER_FLAG_AUTH) + 1;
509 return (sprintf('$("myname").innerHTML = "<span class=\"au%d\">%s</span>";', $class_id,
510 xcape($this->name,ENT_COMPAT,"UTF-8")));
513 /* INDEX_RD_IFRA PORT */
515 static function blocking_error($is_unrecoverable)
517 log_crit("BLOCKING_ERROR UNREC: ".($is_unrecoverable ? "TRUE" : "FALSE"));
518 return (sprintf(($is_unrecoverable ? 'xstm.stop(); ' : '').'window.onbeforeunload = null; window.onunload = null; document.location.assign("index.php");'));
521 // FIXME TO SUPPORT iframe
522 protected function page_sync($sess, $page, $table_idx, $table_token)
524 // log_rd2("page_sync:".var_export(debug_backtrace()));
526 log_rd2("PAGE_SYNC");
527 printf("xXx USER::PAGE_SYNC [%s]\n", get_class($this));
528 return (sprintf('createCookie("table_idx", %d, 24*365, cookiepath); createCookie("table_token", "%s", 24*365, cookiepath); xstm.stop(); window.onunload = null; window.onbeforeunload = null; document.location.assign("%s");', $table_idx, $table_token, $page));
534 protected function maincheck($cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step, $splashdate, $table_idx, $table_token)
536 GLOBAL $G_lang, $mlang_indrd;
537 // GLOBAL $first_loop;
538 GLOBAL $G_with_splash, $G_splash_content, $G_splash_interval, $G_splash_idx;
539 GLOBAL $G_splash_w, $G_splash_h, $G_splash_timeout;
540 $CO_splashdate = "CO_splashdate".$G_splash_idx;
541 $$CO_splashdate = $splashdate;
545 log_rd("maincheck begin");
550 /* Nothing changed, return. */
551 if ($cur_step == $this->step)
554 log_rd2("do other cur_stat[".$cur_stat."] user->stat[".$this->stat."] cur_step[".$cur_step."] user_step[".$this->step."]");
556 if ($cur_step == -1) {
558 * if $cur_step == -1 load the current state from the main struct
561 $S_load_stat['wR_minusone']++;
563 // if ($this->the_end == TRUE) {
564 // log_rd2("main_check: the end".var_export(debug_backtrace()));
567 if ($this->trans_step != -1) {
568 log_rd2("TRANS USATO ".$this->trans_step);
569 $cur_step = $this->trans_step;
570 $this->trans_step = -1;
573 log_rd2("TRANS NON ATTIVATO");
578 /* this part I suppose is read only on $this->room structure */
579 if ($cur_step == -1) {
580 log_rd2("PRE-NEWSTAT: ".$this->stat);
582 if ($this->stat == 'room') {
583 log_rd("roomma ".$this->step);
586 if ($G_with_splash &&
587 ($$CO_splashdate < $curtime - $G_splash_interval ||
588 $$CO_splashdate > $curtime)) {
589 $is_super = $this->flags & USER_FLAG_TY_SUPER;
590 $ret .= show_notify_ex(str_replace("\n", " ", $G_splash_content[$G_lang]),
591 ($is_super ? 0 : $G_splash_timeout),
592 // $mlang_indrd[($is_super ? 'btn_btotabsup' : 'btn_backtotab')][$G_lang],
593 $mlang_indrd['btn_backtotab'][$G_lang],
594 $G_splash_w, $G_splash_h, true,
595 ($is_super ? 0 : $G_splash_timeout));
596 $ret .= sprintf('|createCookie("CO_splashdate%d", %d, 24*365, cookiepath);', $G_splash_idx, $curtime);
598 $ret .= $this->room->show_room($this->step, $this);
600 // TODO uncomment and test
601 /* NOTE the sets went common */
602 $new_stat = $this->stat;
603 $new_subst = $this->subst;
604 $new_step = $this->step;
611 else if ($this->stat == 'table') {
613 printf("xXx USER::MAINCHECK1 [%s]\n", get_class($this));
615 return ($this->page_sync($this->sess, "briskin5/index.php", $this->table, $this->table_token));
617 log_rd2("NEWSTAT: ".$this->stat);
618 } /* if ($cur_step == -1) { */
620 /* $sem = Room::lock_data(FALSE); */
621 $S_load_stat['rU_heavy']++;
623 if ($cur_step < $this->step) {
625 if ($cur_step + COMM_N < $this->step) {
626 if (($cur_stat != $this->stat)) {
627 $to_stat = $this->stat;
628 /* Room::unlock_data($sem); */
630 printf("xXx USER::MAINCHECK2 [%s]\n", get_class($this));
631 return ($this->page_sync($this->sess, ($to_stat == "table" ? "briskin5/index.php" : "index.php"), $this->table, $this->table_token));
633 log_rd2("lost history, refresh from scratch");
637 for ($i = $cur_step ; $i < $this->step ; $i++) {
639 log_rd2("ADDED TO THE STREAM: ".$this->comm[$ii]);
640 $ret .= $this->comm[$ii];
642 $new_stat = $this->stat;
643 $new_subst = $this->subst;
644 $new_step = $this->step;
647 log_rd2($this->step, 'index_rd.php: after ret set');
649 if ($this->the_end == TRUE) {
650 log_rd2("LOGOUT BYE BYE!!");
651 log_auth($this->sess, "Explicit logout.");
653 if ($this->the_end == TRUE) {
656 if ($this->subst == 'sitdown') {
657 log_load("ROOM WAKEUP");
658 $this->room->room_wakeup($this);
660 else if ($this->subst == 'standup')
661 $this->room->room_outstandup($this);
663 log_rd2("LOGOUT FROM WHAT ???");
665 } /* if ($this->the_end == TRUE) { ... */
666 } /* if ($this->the_end == TRUE) { ... */
667 } /* if ($cur_step < $this->step) { */
669 /* Room::unlock_data($sem); */
670 } /* else of if ($cur_step == -1) { */
674 } // function maincheck (...
676 public static function stream_fini($transp, $init_string, $is_unrecoverable)
678 printf("xXx user::stream_fini\n");
680 // FIXME: dynamic "Transport_" type
681 $trans_class = Transport::gettype($transp);
682 $body = $trans_class::fini($init_string, self::base_get(), static::blocking_error($is_unrecoverable));
685 // return (static::blocking_error($is_unrecoverable));
699 function stream_init($init_string, $enc, &$header_out, &$body, $get, $post, $cookie)
703 printf("CLASS: [%s] base: [%s]\n", get_class($this), self::base_get());
705 log_load("index_rd_ifra_init.php");
707 if (($from = gpcs_var('from', $get, $post, $cookie)) === FALSE)
709 if (($stat = gpcs_var('stat', $get, $post, $cookie)) === FALSE)
711 if (($subst = gpcs_var('subst', $get, $post, $cookie)) === FALSE)
713 if (($step = gpcs_var('step', $get, $post, $cookie)) === FALSE)
715 if (($transp = gpcs_var('transp', $get, $post, $cookie)) === FALSE)
718 $this->rd_data_set($curtime, $transp, $enc, $stat, $subst, $step, $from);
720 $body .= $this->rd_transp->init($enc, &$header_out, $init_string, self::base_get(), $this->rd_scristp);
725 function stream_main(&$body, $get, $post, $cookie)
727 GLOBAL $G_splash_idx;
729 $CO_splashdate = "CO_splashdate".$G_splash_idx;
730 if (($splashdate = gpcs_var("$CO_splashdate", $get, $post, $cookie)) === FALSE)
732 if (($table_idx = gpcs_var("table_idx", $get, $post, $cookie)) === FALSE)
734 if (($table_token = gpcs_var("table_token", $get, $post, $cookie)) === FALSE)
737 log_rd2("FROM OUTSIDE - STAT: ".$this->rd_stat." SUBST: ".$this->rd_subst." STEP: ".$this->rd_step." FROM: ".$this->rd_from);
740 $pre_main = gettimeofday(TRUE);
742 $old_stat = $this->rd_stat;
743 $old_subst = $this->rd_subst;
744 $old_step = $this->rd_step;
745 printf("xXx PRE : rd_step %d\n", $this->rd_step);
746 if (($ret = $this->maincheck($old_stat, $old_subst, $old_step, $this->rd_stat, $this->rd_subst, $this->rd_step, $splashdate, $table_idx, $table_token)) != FALSE) {
747 $body .= $this->rd_transp->chunk( $this->rd_scristp++, $ret);
748 log_rd2(0, 'index_rd.php: after mop_flush (begin: '.sprintf("%f", $pre_main).')');
750 printf("xXx POST: rd_step %d\n", $this->rd_step);
755 function stream_keepalive($with_ping)
757 return ($this->rd_transp->chunk( $this->rd_scristp++, ($with_ping ? "act_ping();" : NULL)));
760 static function base_get()
762 $c = get_called_class();
763 printf("CALLED_CLASS: [%s]\n", $c);
767 function is_supp_custom()
769 if ($this->rec != FALSE) {
770 if ($this->flags & USER_FLAG_TY_SUPER) {
774 if ($this->rec->last_dona > 1356994800) {