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_SUSPEND', 0x400000); // done
63 define('USER_FLAG_TY_DISABLE', 0x800000); // done
65 // 240 is the right value, 600 is for fwrite error test
66 define('RD_ENDTIME_DELTA', 240);
67 define('RD_KEEPALIVE_TOUT', 4);
69 $S_load_stat = array( 'rU_heavy' => 0,
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 ')
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)
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, ...)
111 var $comm; // commands array
112 // var $asta_card; //
114 // var $handpt; // Total card points at the beginning of the current hand.
115 // var $exitislock; // Player can exit from the table ?
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 $rec; // field with user db record or FALSE
125 var $the_end; // Flag to change the end of the session
127 var $chat_lst; // Last chat line
128 var $chattime; // Array of chat times
129 var $chat_cur; // Current chat line number
130 var $chat_ban; // Time for ban chat
131 var $chat_dlt; // Delta t for ban
134 const BASE = ""; // basepath for absolute web references
139 static function create(&$room, $idx, $name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
140 if (($thiz = new User()) == FALSE)
143 $thiz->room = &$room;
145 $thiz->idx_orig = $idx;
150 $thiz->lacc = time();
151 $thiz->laccwr = time();
154 $thiz->subst = $subst;
156 $thiz->trans_step = -1;
157 $thiz->comm = array();
159 $thiz->rd_socket = NULL;
160 $thiz->rd_endtime = -1;
162 $thiz->rd_subst = "";
165 $thiz->rd_scristp = -1;
166 $thiz->rd_kalive = -1;
167 $thiz->rd_cache = "";
168 $thiz->rd_zls = FALSE;
169 $thiz->rd_transp = NULL;
171 $thiz->asta_card = -2;
172 $thiz->asta_pnt = -1;
174 $thiz->exitislock = TRUE;
179 $thiz->chattime = array_fill(0, CHAT_N, 0);
181 $thiz->chat_lst = "";
185 $thiz->table_orig = $table;
186 $thiz->table = $table;
187 $thiz->table_pos = -1;
188 $thiz->table_token= "";
189 $thiz->shm_sz = SHM_DIMS_U_MIN;
195 $this->idx = $from->idx;
196 $this->idx_orig = $from->idx;
197 $this->code = $from->code;
198 $this->name = $from->name;
199 $this->sess = $from->sess;
200 $this->ip = $from->ip;
201 $this->lacc = $from->lacc;
202 $this->laccwr = $from->laccwr;
203 $this->bantime = $from->bantime;
204 $this->stat = $from->stat;
205 $this->subst = $from->subst;
206 $this->step = $from->step;
207 $this->trans_step = $from->trans_step;
208 $this->comm = array();
210 $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N));
211 for ($i = $i_start ; $i < $from->step ; $i++) {
213 if (isset($from->comm[$ii])) {
214 $this->comm[$ii] = $from->comm[$ii];
217 $this->asta_card = $from->asta_card;
218 $this->asta_pnt = $from->asta_pnt;
219 $this->handpt = $from->handpt;
220 $this->exitislock = $from->exitislock;
222 $this->flags = $from->flags;
223 $this->rec = $from->rec;
225 $this->chattime = array();
226 for ($i = 0 ; $i < CHAT_N ; $i++)
227 $this->chattime[$i] = $from->chattime[$i];
228 $this->chat_cur = $from->chat_cur;
229 $this->chat_lst = $from->chat_lst;
230 $this->chat_ban = $from->chat_ban;
231 $this->chat_dlt = $from->chat_dlt;
233 $this->table_orig = $from->table_orig;
234 $this->table = $from->table;
235 $this->table_pos = $from->table_pos;
236 $this->table_token = $from->table_token;
237 $this->the_end = $from->the_end;
238 $this->shm_sz = $from->shm_sz;
243 static function myclone($from)
245 if (($thiz = new User()) == FALSE)
253 static function spawn($from, $table, $table_pos)
255 if (($thiz = new User()) == FALSE)
258 $thiz->idx = $from->idx;
259 $thiz->idx_orig = $from->idx;
260 $thiz->code = $from->code;
261 $thiz->name = $from->name;
262 $thiz->sess = $from->sess;
263 $thiz->ip = $from->ip;
264 $thiz->lacc = $from->lacc;
265 $thiz->laccwr = $from->laccwr;
266 $thiz->bantime = $from->bantime;
267 $thiz->stat = $from->stat;
268 $thiz->subst = $from->subst;
269 $thiz->step = $from->step;
270 $thiz->trans_step = $from->trans_step;
271 $thiz->comm = array();
274 $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N));
275 for ($i = $i_start ; $i < $from->step ; $i++) {
276 log_wr("TRY PUSH:".$i);
278 $thiz->comm[$ii] = $from->comm[$ii];
281 $thiz->asta_card = $from->asta_card;
282 $thiz->asta_pnt = $from->asta_pnt;
283 $thiz->handpt = $from->handpt;
284 $thiz->exitislock = $from->exitislock;
285 $thiz->the_end = $from->the_end;
287 $thiz->flags = $from->flags;
288 $thiz->rec = $from->rec;
290 $thiz->chattime = array_fill(0, CHAT_N, 0);
292 $thiz->chat_lst = "";
297 $thiz->table_orig = $table;
299 $thiz->table_pos = $table_pos;
300 $thiz->table_token = $from->table_token;
301 $thiz->shm_sz = $from->shm_sz;
306 function flags_set($flags, $mask)
308 $flags_old = $this->flags & (~$mask);
309 $this->flags = ($flags_old | ($flags & $mask));
314 if (($bdb = BriskDB::create()) == FALSE) {
317 return ($bdb->user_prefs_update($this->code, ($this->flags & (USER_FLAG_TY_ALL | USER_FLAG_MAP_AUTH)),
318 $this->rec->supp_comp));
321 function rd_data_set($curtime, $transp, $enc, $stat, $subst, $step, $from)
323 $this->rd_endtime = $curtime + RD_ENDTIME_DELTA;
324 $this->rd_stat = $stat;
325 $this->rd_subst = $subst;
326 $this->rd_step = $step;
327 $this->rd_from = $from;
328 $this->rd_scristp = 0;
329 $this->rd_kalive = $curtime + RD_KEEPALIVE_TOUT;
330 $this->rd_zls = ZLibStream::create($enc);
331 $this->rd_transp = Transport::create($transp);
334 function rd_socket_get() {
335 return ($this->rd_socket);
338 function rd_socket_set($sock) {
341 $this->rd_zls->destroy();
342 $this->rd_zls = FALSE;
345 $this->rd_socket = $sock;
348 function rd_kalive_get()
350 return ($this->rd_kalive);
353 function rd_kalive_set($tm)
355 $this->rd_kalive = $tm;
358 function rd_kalive_is_expired($tm)
360 // printf("rd_kalive %d tm %d\n", $this->rd_kalive, $tm);
361 return ($this->rd_kalive < $tm);
364 function rd_endtime_is_expired($tm)
366 // printf("rd_endtime %d tm %d\n", $this->rd_kalive, $tm);
367 return ($this->rd_endtime < $tm);
370 function rd_kalive_reset($tm)
372 $this->rd_kalive = $tm + RD_KEEPALIVE_TOUT;
375 function rd_cache_get()
377 return ($this->rd_cache);
380 function rd_cache_set($cache)
382 $this->rd_cache = $cache;
385 function rd_zls_get()
387 return ($this->rd_zls);
394 function code_get() {
395 return ($this->code);
398 function stat_set($stat) {
399 log_main("sess: [".$this->sess. "] NEW STAT: [".$stat."]");
400 $this->stat = "$stat";
403 if (validate_sess($this->sess)) {
404 if (file_exists(PROXY_PATH) == FALSE)
405 mkdir(PROXY_PATH, 0775, TRUE);
406 $fp = @fopen(PROXY_PATH."/".$this->sess.".stat", 'w');
407 fwrite($fp, sprintf("%s\n",$this->stat));
413 function step_set($step)
415 $this->step = $step & 0x7fffffff;
420 function step_inc($delta = 1) {
421 $this->step += $delta;
422 /* modularization because unpack() not manage unsigned 32bit int correctly */
423 $this->step &= 0x7fffffff;
432 if (validate_sess($this->sess) == FALSE)
434 if (file_exists(PROXY_PATH) == FALSE)
435 mkdir(PROXY_PATH, 0775, TRUE);
436 if (($fp = @fopen(PROXY_PATH."/".$this->sess.".step", 'w')) == FALSE)
438 fwrite($fp, pack("LL",$this->step, $this->idx));
441 log_main("step_set [".$this->sess. "] [".$this->step."]");
449 static function load_step($sess)
453 if (validate_sess($sess) == FALSE)
456 if (file_exists(PROXY_PATH) == FALSE)
457 mkdir(PROXY_PATH, 0775, TRUE);
458 if (($fp = @fopen(PROXY_PATH."/".$sess.".step", 'rb')) == FALSE)
460 if (($s = fread($fp, 8)) == FALSE)
462 if (mb_strlen($s, "ASCII") != 8)
464 $arr = unpack('Ls/Li', $s);
467 // log_rd2("A0: ".$arr[0]." A1: ".$arr[1]);
474 log_rd2("STEP_GET [".$sess."]: return false ");
479 static function unproxy_step($sess) {
480 log_rd2("UNPROXY: ".PROXY_PATH."/".$sess.".step");
481 if (file_exists(PROXY_PATH) == FALSE)
483 @unlink(PROXY_PATH."/".$sess.".step");
488 log_legal($curtime, $this->ip, $this, "STAT:LOGOUT", '');
490 $tmp_sess = $this->sess;
492 self::unproxy_step($tmp_sess);
493 $this->name = ""; // OK here
494 while (array_pop($this->comm) != NULL);
496 $this->chattime = array_fill(0, CHAT_N, 0);
498 $this->chat_lst = "";
501 $this->the_end = FALSE;
504 function myname_innerHTML()
506 $class_id = ($this->flags & USER_FLAG_AUTH) + 1;
508 return (sprintf('$("myname").innerHTML = "<span class=\"au%d\">%s</span>";', $class_id,
509 xcape($this->name,ENT_COMPAT,"UTF-8")));
512 /* INDEX_RD_IFRA PORT */
514 static function blocking_error($is_unrecoverable)
516 log_crit("BLOCKING_ERROR UNREC: ".($is_unrecoverable ? "TRUE" : "FALSE"));
517 return (sprintf(($is_unrecoverable ? 'xstm.stop(); ' : '').'window.onbeforeunload = null; window.onunload = null; document.location.assign("index.php");'));
520 // FIXME TO SUPPORT iframe
521 protected function page_sync($sess, $page, $table_idx, $table_token)
523 // log_rd2("page_sync:".var_export(debug_backtrace()));
525 log_rd2("PAGE_SYNC");
526 printf("xXx USER::PAGE_SYNC [%s]\n", get_class($this));
527 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));
533 protected function maincheck($cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step, $splashdate, $table_idx, $table_token)
535 GLOBAL $G_lang, $mlang_indrd;
536 // GLOBAL $first_loop;
537 GLOBAL $G_with_splash, $G_splash_content, $G_splash_interval, $G_splash_idx;
538 GLOBAL $G_splash_w, $G_splash_h, $G_splash_timeout;
539 $CO_splashdate = "CO_splashdate".$G_splash_idx;
540 $$CO_splashdate = $splashdate;
544 log_rd("maincheck begin");
549 /* Nothing changed, return. */
550 if ($cur_step == $this->step)
553 log_rd2("do other cur_stat[".$cur_stat."] user->stat[".$this->stat."] cur_step[".$cur_step."] user_step[".$this->step."]");
555 if ($cur_step == -1) {
557 * if $cur_step == -1 load the current state from the main struct
560 $S_load_stat['wR_minusone']++;
562 // if ($this->the_end == TRUE) {
563 // log_rd2("main_check: the end".var_export(debug_backtrace()));
566 if ($this->trans_step != -1) {
567 log_rd2("TRANS USATO ".$this->trans_step);
568 $cur_step = $this->trans_step;
569 $this->trans_step = -1;
572 log_rd2("TRANS NON ATTIVATO");
577 /* this part I suppose is read only on $this->room structure */
578 if ($cur_step == -1) {
579 log_rd2("PRE-NEWSTAT: ".$this->stat);
581 if ($this->stat == 'room') {
582 log_rd("roomma ".$this->step);
585 if ($G_with_splash &&
586 ($$CO_splashdate < $curtime - $G_splash_interval ||
587 $$CO_splashdate > $curtime)) {
588 $is_super = $this->flags & USER_FLAG_TY_SUPER;
589 $ret .= show_notify_ex(str_replace("\n", " ", $G_splash_content[$G_lang]),
590 ($is_super ? 0 : $G_splash_timeout),
591 $mlang_indrd[($is_super ? 'btn_btotabsup' : 'btn_backtotab')][$G_lang],
592 $G_splash_w, $G_splash_h, true,
593 ($is_super ? 0 : $G_splash_timeout));
594 $ret .= sprintf('|createCookie("CO_splashdate%d", %d, 24*365, cookiepath);', $G_splash_idx, $curtime);
596 $ret .= $this->room->show_room($this->step, $this);
598 // TODO uncomment and test
599 /* NOTE the sets went common */
600 $new_stat = $this->stat;
601 $new_subst = $this->subst;
602 $new_step = $this->step;
609 else if ($this->stat == 'table') {
611 printf("xXx USER::MAINCHECK1 [%s]\n", get_class($this));
613 return ($this->page_sync($this->sess, "briskin5/index.php", $this->table, $this->table_token));
615 log_rd2("NEWSTAT: ".$this->stat);
616 } /* if ($cur_step == -1) { */
618 /* $sem = Room::lock_data(FALSE); */
619 $S_load_stat['rU_heavy']++;
621 if ($cur_step < $this->step) {
623 if ($cur_step + COMM_N < $this->step) {
624 if (($cur_stat != $this->stat)) {
625 $to_stat = $this->stat;
626 /* Room::unlock_data($sem); */
628 printf("xXx USER::MAINCHECK2 [%s]\n", get_class($this));
629 return ($this->page_sync($this->sess, ($to_stat == "table" ? "briskin5/index.php" : "index.php"), $this->table, $this->table_token));
631 log_rd2("lost history, refresh from scratch");
635 for ($i = $cur_step ; $i < $this->step ; $i++) {
637 log_rd2("ADDED TO THE STREAM: ".$this->comm[$ii]);
638 $ret .= $this->comm[$ii];
640 $new_stat = $this->stat;
641 $new_subst = $this->subst;
642 $new_step = $this->step;
645 log_rd2($this->step, 'index_rd.php: after ret set');
647 if ($this->the_end == TRUE) {
648 log_rd2("LOGOUT BYE BYE!!");
649 log_auth($this->sess, "Explicit logout.");
651 if ($this->the_end == TRUE) {
654 if ($this->subst == 'sitdown') {
655 log_load("ROOM WAKEUP");
656 $this->room->room_wakeup($this);
658 else if ($this->subst == 'standup')
659 $this->room->room_outstandup($this);
661 log_rd2("LOGOUT FROM WHAT ???");
663 } /* if ($this->the_end == TRUE) { ... */
664 } /* if ($this->the_end == TRUE) { ... */
665 } /* if ($cur_step < $this->step) { */
667 /* Room::unlock_data($sem); */
668 } /* else of if ($cur_step == -1) { */
672 } // function maincheck (...
674 public static function stream_fini($transp, $init_string, $is_unrecoverable)
676 printf("xXx user::stream_fini\n");
678 // FIXME: dynamic "Transport_" type
679 $trans_class = Transport::gettype($transp);
680 $body = $trans_class::fini($init_string, self::base_get(), static::blocking_error($is_unrecoverable));
683 // return (static::blocking_error($is_unrecoverable));
697 function stream_init($init_string, $enc, &$header_out, &$body, $get, $post, $cookie)
701 printf("CLASS: [%s] base: [%s]\n", get_class($this), self::base_get());
703 log_load("index_rd_ifra_init.php");
705 if (($from = gpcs_var('from', $get, $post, $cookie)) === FALSE)
707 if (($stat = gpcs_var('stat', $get, $post, $cookie)) === FALSE)
709 if (($subst = gpcs_var('subst', $get, $post, $cookie)) === FALSE)
711 if (($step = gpcs_var('step', $get, $post, $cookie)) === FALSE)
713 if (($transp = gpcs_var('transp', $get, $post, $cookie)) === FALSE)
716 $this->rd_data_set($curtime, $transp, $enc, $stat, $subst, $step, $from);
718 $body .= $this->rd_transp->init($enc, &$header_out, $init_string, self::base_get(), $this->rd_scristp);
723 function stream_main(&$body, $get, $post, $cookie)
725 GLOBAL $G_splash_idx;
727 $CO_splashdate = "CO_splashdate".$G_splash_idx;
728 if (($splashdate = gpcs_var("$CO_splashdate", $get, $post, $cookie)) === FALSE)
730 if (($table_idx = gpcs_var("table_idx", $get, $post, $cookie)) === FALSE)
732 if (($table_token = gpcs_var("table_token", $get, $post, $cookie)) === FALSE)
735 log_rd2("FROM OUTSIDE - STAT: ".$this->rd_stat." SUBST: ".$this->rd_subst." STEP: ".$this->rd_step." FROM: ".$this->rd_from);
738 $pre_main = gettimeofday(TRUE);
740 $old_stat = $this->rd_stat;
741 $old_subst = $this->rd_subst;
742 $old_step = $this->rd_step;
743 printf("xXx PRE : rd_step %d\n", $this->rd_step);
744 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) {
745 $body .= $this->rd_transp->chunk( $this->rd_scristp++, $ret);
746 log_rd2(0, 'index_rd.php: after mop_flush (begin: '.sprintf("%f", $pre_main).')');
748 printf("xXx POST: rd_step %d\n", $this->rd_step);
753 function stream_keepalive($with_ping)
755 return ($this->rd_transp->chunk( $this->rd_scristp++, ($with_ping ? "act_ping();" : NULL)));
758 static function base_get()
760 $c = get_called_class();
761 printf("CALLED_CLASS: [%s]\n", $c);
765 function is_supp_custom()
767 if ($this->rec != FALSE) {
768 if ($this->flags & USER_FLAG_TY_SUPER) {
772 if ($this->rec->last_dona > 1356994800) {