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");
27 define('USER_FLAG_AUTH', 0x02);
29 define('USER_FLAG_MAP_AUTH', 0x0c);
30 define('USER_FLAG_LISTAUTH', 0x04);
31 define('USER_FLAG_ISOLAUTH', 0x08);
33 define('USER_FLAG_DBFAILED', 0x10);
36 define('USER_FLAG_S_NORM', 0x00); // done
37 define('USER_FLAG_S_PAU', 0x01); // done
38 define('USER_FLAG_S_OUT', 0x02); // done
39 define('USER_FLAG_S_DOG', 0x03); // done
40 define('USER_FLAG_S_EAT', 0x04); // done
41 define('USER_FLAG_S_WRK', 0x05); // done
42 define('USER_FLAG_S_SMK', 0x06); // done
43 define('USER_FLAG_S_EYE', 0x07); // done
44 define('USER_FLAG_S_RABB', 0x08); // done
45 define('USER_FLAG_S_SOCC', 0x09); // done
46 define('USER_FLAG_S_BABY', 0x0a); // done
47 define('USER_FLAG_S_MOP', 0x0b); // done
48 define('USER_FLAG_S_BABBO', 0x0c); // done
49 define('USER_FLAG_S_RENNA', 0x0d); // done
50 define('USER_FLAG_S_PUPAZ', 0x0e); // done
51 define('USER_FLAG_S_VISCH', 0x0f); // done
53 define('USER_FLAG_S_ALL', 0xff); // done
55 /* does is user moderationg brisk ? */
56 define('USER_FLAG_MODER', 0x0100); // done
58 /* type of user normal, supporter etc ... */
59 define('USER_FLAG_TY_ALL', 0xff0000); // done
60 define('USER_FLAG_TY_NORM', 0x010000); // done
61 define('USER_FLAG_TY_SUPER', 0x020000); // done
62 define('USER_FLAG_TY_CERT', 0x040000); // done
63 // ... other usefull status ...
64 define('USER_FLAG_TY_SUSPEND', 0x400000); // done
65 define('USER_FLAG_TY_DISABLE', 0x800000); // done
67 // 240 is the right value, 600 is for fwrite error test
68 define('RD_ENDTIME_DELTA', 240);
69 define('RD_KEEPALIVE_TOUT', 4);
71 $S_load_stat = array( 'rU_heavy' => 0,
79 'btn_backtotab' => array('it' => ' torna ai tavoli ',
80 'en' => ' back to tables '),
81 'btn_btotabsup' => array('it' => ' grazie della donazione, torna ai tavoli ',
82 'en' => ' thank you for donation, back to tables ')
86 var $room; // reference to the room where the user is registered
87 var $idx; // index in the users array when you are in game
88 var $idx_orig; // index in the users array when you aren't in game
89 var $code; // authentication code
90 var $name; // name of the user
91 var $sess; // session of the user
92 var $ip; // ip of the user
93 var $lacc; // last access (for the cleanup)
94 var $laccwr; // last access (for the cleanup)
95 var $bantime; // timeout to temporary ban
96 var $stat; // status (outdoor, room, table, game, ...)
97 var $subst; // substatus for each status
98 var $step; // step of the current status
99 var $trans_step; // step to enable transition between pages (disable == -1)
101 var $rd_socket; // socket handle of push stream
102 var $rd_endtime; // end time for push stream
103 var $rd_stat; // actual status of push stream
104 var $rd_subst; // actual substatus of push stream
105 var $rd_step; // actual step of push stream
106 var $rd_from; // referer
107 var $rd_scristp; // current script step (for each session)
108 var $rd_kalive; // if no message are sent after RD_KEEPALIVE_TOUT secs we send a keepalive from server
109 var $rd_cache; // place where store failed fwrite data
110 var $rd_zls; // zlibstream object handle if compressed stream, else FALSE
111 var $rd_transp; // class that define stream encapsulation type (iframe, xhr, ...)
113 var $comm; // commands array
114 // var $asta_card; //
116 // var $handpt; // Total card points at the beginning of the current hand.
117 // var $exitislock; // Player can exit from the table ?
119 // FIXME: the table_orig field must be removed after table field verify of index management (in spawned table
120 // it is allways ZERO
121 var $table; // id of the current table when you are in game
122 var $table_orig; // id of the current table when you aren't in game
123 var $table_pos; // idx on the table
124 var $table_token;// token that identify a game on a table
126 var $flags; // Bitfield with: AUTHENTICATE: 0x02
127 var $flags_vlt; // Bitfield of volatile flags valid for this session only
128 var $rec; // field with user db record or FALSE
129 var $the_end; // Flag to change the end of the session
131 var $chat_lst; // Last chat line
132 var $chattime; // Array of chat times
133 var $chat_cur; // Current chat line number
134 var $chat_ban; // Time for ban chat
135 var $chat_dlt; // Delta t for ban
138 const BASE = ""; // basepath for absolute web references
143 static function create(&$room, $idx, $name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
144 if (($thiz = new User()) == FALSE)
147 $thiz->room = &$room;
149 $thiz->idx_orig = $idx;
154 $thiz->lacc = time();
155 $thiz->laccwr = time();
158 $thiz->subst = $subst;
160 $thiz->trans_step = -1;
161 $thiz->comm = array();
163 $thiz->rd_socket = NULL;
164 $thiz->rd_endtime = -1;
166 $thiz->rd_subst = "";
169 $thiz->rd_scristp = -1;
170 $thiz->rd_kalive = -1;
171 $thiz->rd_cache = "";
172 $thiz->rd_zls = FALSE;
173 $thiz->rd_transp = NULL;
175 $thiz->asta_card = -2;
176 $thiz->asta_pnt = -1;
178 $thiz->exitislock = TRUE;
181 $thiz->flags_vlt = 0x00;
184 $thiz->chattime = array_fill(0, CHAT_N, 0);
186 $thiz->chat_lst = "";
190 $thiz->table_orig = $table;
191 $thiz->table = $table;
192 $thiz->table_pos = -1;
193 $thiz->table_token= "";
194 $thiz->shm_sz = SHM_DIMS_U_MIN;
200 $this->idx = $from->idx;
201 $this->idx_orig = $from->idx;
202 $this->code = $from->code;
203 $this->name = $from->name;
204 $this->sess = $from->sess;
205 $this->ip = $from->ip;
206 $this->lacc = $from->lacc;
207 $this->laccwr = $from->laccwr;
208 $this->bantime = $from->bantime;
209 $this->stat = $from->stat;
210 $this->subst = $from->subst;
211 $this->step = $from->step;
212 $this->trans_step = $from->trans_step;
213 $this->comm = array();
215 $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N));
216 for ($i = $i_start ; $i < $from->step ; $i++) {
218 if (isset($from->comm[$ii])) {
219 $this->comm[$ii] = $from->comm[$ii];
222 $this->asta_card = $from->asta_card;
223 $this->asta_pnt = $from->asta_pnt;
224 $this->handpt = $from->handpt;
225 $this->exitislock = $from->exitislock;
227 $this->flags = $from->flags;
228 $this->flags_vlt = $from->flags_vlt;
229 $this->rec = $from->rec;
231 $this->chattime = array();
232 for ($i = 0 ; $i < CHAT_N ; $i++)
233 $this->chattime[$i] = $from->chattime[$i];
234 $this->chat_cur = $from->chat_cur;
235 $this->chat_lst = $from->chat_lst;
236 $this->chat_ban = $from->chat_ban;
237 $this->chat_dlt = $from->chat_dlt;
239 $this->table_orig = $from->table_orig;
240 $this->table = $from->table;
241 $this->table_pos = $from->table_pos;
242 $this->table_token = $from->table_token;
243 $this->the_end = $from->the_end;
244 $this->shm_sz = $from->shm_sz;
249 static function myclone($from)
251 if (($thiz = new User()) == FALSE)
259 static function spawn($from, $table, $table_pos)
261 if (($thiz = new User()) == FALSE)
264 $thiz->idx = $from->idx;
265 $thiz->idx_orig = $from->idx;
266 $thiz->code = $from->code;
267 $thiz->name = $from->name;
268 $thiz->sess = $from->sess;
269 $thiz->ip = $from->ip;
270 $thiz->lacc = $from->lacc;
271 $thiz->laccwr = $from->laccwr;
272 $thiz->bantime = $from->bantime;
273 $thiz->stat = $from->stat;
274 $thiz->subst = $from->subst;
275 $thiz->step = $from->step;
276 $thiz->trans_step = $from->trans_step;
277 $thiz->comm = array();
280 $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N));
281 for ($i = $i_start ; $i < $from->step ; $i++) {
282 log_wr("TRY PUSH:".$i);
284 $thiz->comm[$ii] = $from->comm[$ii];
287 $thiz->asta_card = $from->asta_card;
288 $thiz->asta_pnt = $from->asta_pnt;
289 $thiz->handpt = $from->handpt;
290 $thiz->exitislock = $from->exitislock;
291 $thiz->the_end = $from->the_end;
293 $thiz->flags = $from->flags;
294 $thiz->flags_vlt = $from->flags_vlt;
295 $thiz->rec = $from->rec;
297 $thiz->chattime = array_fill(0, CHAT_N, 0);
299 $thiz->chat_lst = "";
304 $thiz->table_orig = $table;
306 $thiz->table_pos = $table_pos;
307 $thiz->table_token = $from->table_token;
308 $thiz->shm_sz = $from->shm_sz;
313 function flags_get($mask)
315 return ($this->flags & $mask);
318 function flags_set($flags, $mask)
320 $flags_old = $this->flags;
321 $this->flags = (($flags_old & (~$mask)) | ($flags & $mask));
323 return ($flags_old != $this->flags);
326 function flags_vlt_get($mask)
328 return ($this->flags_vlt & $mask);
331 function flags_vlt_set($flags_vlt, $mask)
333 $flags_vlt_old = $this->flags_vlt;
334 $this->flags_vlt = (($flags_vlt_old & (~$mask)) | ($flags_vlt & $mask));
336 return ($flags_vlt_old != $this->flags_vlt);
341 if (($bdb = BriskDB::create()) == FALSE) {
344 return ($bdb->user_prefs_update($this->code, ($this->flags & (USER_FLAG_TY_ALL | USER_FLAG_MAP_AUTH)),
345 $this->rec->supp_comp));
348 function rd_data_set($curtime, $transp, $enc, $stat, $subst, $step, $from)
350 $this->rd_endtime = $curtime + RD_ENDTIME_DELTA;
351 $this->rd_stat = $stat;
352 $this->rd_subst = $subst;
353 $this->rd_step = $step;
354 $this->rd_from = $from;
355 $this->rd_scristp = 0;
356 $this->rd_kalive = $curtime + RD_KEEPALIVE_TOUT;
357 $this->rd_zls = ZLibStream::create($enc);
358 $this->rd_transp = Transport::create($transp);
361 function rd_socket_get() {
362 return ($this->rd_socket);
365 function rd_socket_set($sock) {
368 $this->rd_zls->destroy();
369 $this->rd_zls = FALSE;
372 $this->rd_socket = $sock;
375 function rd_kalive_get()
377 return ($this->rd_kalive);
380 function rd_kalive_set($tm)
382 $this->rd_kalive = $tm;
385 function rd_kalive_is_expired($tm)
387 // printf("rd_kalive %d tm %d\n", $this->rd_kalive, $tm);
388 return ($this->rd_kalive < $tm);
391 function rd_endtime_is_expired($tm)
393 // printf("rd_endtime %d tm %d\n", $this->rd_kalive, $tm);
394 return ($this->rd_endtime < $tm);
397 function rd_kalive_reset($tm)
399 $this->rd_kalive = $tm + RD_KEEPALIVE_TOUT;
402 function rd_cache_get()
404 return ($this->rd_cache);
407 function rd_cache_set($cache)
409 $this->rd_cache = $cache;
412 function rd_zls_get()
414 return ($this->rd_zls);
421 function code_get() {
422 return ($this->code);
425 function stat_set($stat) {
426 log_main("sess: [".$this->sess. "] NEW STAT: [".$stat."]");
427 $this->stat = "$stat";
430 if (validate_sess($this->sess)) {
431 if (file_exists(PROXY_PATH) == FALSE)
432 mkdir(PROXY_PATH, 0775, TRUE);
433 $fp = @fopen(PROXY_PATH."/".$this->sess.".stat", 'w');
434 fwrite($fp, sprintf("%s\n",$this->stat));
440 function step_set($step)
442 $this->step = $step & 0x7fffffff;
447 function step_inc($delta = 1) {
448 $this->step += $delta;
449 /* modularization because unpack() not manage unsigned 32bit int correctly */
450 $this->step &= 0x7fffffff;
459 if (validate_sess($this->sess) == FALSE)
461 if (file_exists(PROXY_PATH) == FALSE)
462 mkdir(PROXY_PATH, 0775, TRUE);
463 if (($fp = @fopen(PROXY_PATH."/".$this->sess.".step", 'w')) == FALSE)
465 fwrite($fp, pack("LL",$this->step, $this->idx));
468 log_main("step_set [".$this->sess. "] [".$this->step."]");
476 static function load_step($sess)
480 if (validate_sess($sess) == FALSE)
483 if (file_exists(PROXY_PATH) == FALSE)
484 mkdir(PROXY_PATH, 0775, TRUE);
485 if (($fp = @fopen(PROXY_PATH."/".$sess.".step", 'rb')) == FALSE)
487 if (($s = fread($fp, 8)) == FALSE)
489 if (mb_strlen($s, "ASCII") != 8)
491 $arr = unpack('Ls/Li', $s);
494 // log_rd2("A0: ".$arr[0]." A1: ".$arr[1]);
501 log_rd2("STEP_GET [".$sess."]: return false ");
506 static function unproxy_step($sess) {
507 log_rd2("UNPROXY: ".PROXY_PATH."/".$sess.".step");
508 if (file_exists(PROXY_PATH) == FALSE)
510 @unlink(PROXY_PATH."/".$sess.".step");
515 log_legal($curtime, $this->ip, $this, "STAT:LOGOUT", '');
517 $tmp_sess = $this->sess;
519 self::unproxy_step($tmp_sess);
520 $this->name = ""; // OK here
521 while (array_pop($this->comm) != NULL);
522 $this->flags_vlt = 0;
524 $this->chattime = array_fill(0, CHAT_N, 0);
526 $this->chat_lst = "";
529 $this->the_end = FALSE;
532 function myname_innerHTML()
534 $class_id = ($this->flags & USER_FLAG_AUTH) + 1;
536 return (sprintf('$("myname").innerHTML = "<span class=\"au%d\">%s</span>";', $class_id,
537 xcape($this->name,ENT_COMPAT,"UTF-8")));
540 /* INDEX_RD_IFRA PORT */
542 static function blocking_error($is_unrecoverable)
544 log_crit("BLOCKING_ERROR UNREC: ".($is_unrecoverable ? "TRUE" : "FALSE"));
545 return (sprintf(($is_unrecoverable ? 'xstm.stop(); ' : '').'window.onbeforeunload = null; window.onunload = null; document.location.assign("index.php");'));
548 // FIXME TO SUPPORT iframe
549 protected function page_sync($sess, $page, $table_idx, $table_token)
551 // log_rd2("page_sync:".var_export(debug_backtrace()));
553 log_rd2("PAGE_SYNC");
554 printf("xXx USER::PAGE_SYNC [%s]\n", get_class($this));
555 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));
561 protected function maincheck($cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step, $splashdate, $table_idx, $table_token)
563 GLOBAL $G_lang, $mlang_indrd;
564 // GLOBAL $first_loop;
565 GLOBAL $G_with_splash, $G_splash_content, $G_splash_interval, $G_splash_idx;
566 GLOBAL $G_splash_w, $G_splash_h, $G_splash_timeout;
567 $CO_splashdate = "CO_splashdate".$G_splash_idx;
568 $$CO_splashdate = $splashdate;
572 log_rd("maincheck begin");
577 /* Nothing changed, return. */
578 if ($cur_step == $this->step)
581 log_rd2("do other cur_stat[".$cur_stat."] user->stat[".$this->stat."] cur_step[".$cur_step."] user_step[".$this->step."]");
583 if ($cur_step == -1) {
585 * if $cur_step == -1 load the current state from the main struct
588 $S_load_stat['wR_minusone']++;
590 // if ($this->the_end == TRUE) {
591 // log_rd2("main_check: the end".var_export(debug_backtrace()));
594 if ($this->trans_step != -1) {
595 log_rd2("TRANS USATO ".$this->trans_step);
596 $cur_step = $this->trans_step;
597 $this->trans_step = -1;
600 log_rd2("TRANS NON ATTIVATO");
605 /* this part I suppose is read only on $this->room structure */
606 if ($cur_step == -1) {
607 log_rd2("PRE-NEWSTAT: ".$this->stat);
609 if ($this->flags_vlt_get(USER_FLAG_MODER)) {
610 $ret .= sprintf('moderate(true);');
612 if ($this->stat == 'room') {
613 log_rd("roomma ".$this->step);
616 if ($G_with_splash &&
617 ($$CO_splashdate < $curtime - $G_splash_interval ||
618 $$CO_splashdate > $curtime)) {
619 $is_super = $this->flags_get(USER_FLAG_TY_SUPER);
620 $ret .= show_notify_ex(str_replace("\n", " ", $G_splash_content[$G_lang]),
621 ($is_super ? 0 : $G_splash_timeout),
622 // $mlang_indrd[($is_super ? 'btn_btotabsup' : 'btn_backtotab')][$G_lang],
623 $mlang_indrd['btn_backtotab'][$G_lang],
624 $G_splash_w, $G_splash_h, true,
625 ($is_super ? 0 : $G_splash_timeout));
626 $ret .= sprintf('|createCookie("CO_splashdate%d", %d, 24*365, cookiepath);', $G_splash_idx, $curtime);
628 $ret .= $this->room->show_room($this->step, $this);
630 // TODO uncomment and test
631 /* NOTE the sets went common */
632 $new_stat = $this->stat;
633 $new_subst = $this->subst;
634 $new_step = $this->step;
641 else if ($this->stat == 'table') {
643 printf("xXx USER::MAINCHECK1 [%s]\n", get_class($this));
645 return ($this->page_sync($this->sess, "briskin5/index.php", $this->table, $this->table_token));
647 log_rd2("NEWSTAT: ".$this->stat);
648 } /* if ($cur_step == -1) { */
650 /* $sem = Room::lock_data(FALSE); */
651 $S_load_stat['rU_heavy']++;
653 if ($cur_step < $this->step) {
655 if ($cur_step + COMM_N < $this->step) {
656 if (($cur_stat != $this->stat)) {
657 $to_stat = $this->stat;
658 /* Room::unlock_data($sem); */
660 printf("xXx USER::MAINCHECK2 [%s]\n", get_class($this));
661 return ($this->page_sync($this->sess, ($to_stat == "table" ? "briskin5/index.php" : "index.php"), $this->table, $this->table_token));
663 log_rd2("lost history, refresh from scratch");
667 for ($i = $cur_step ; $i < $this->step ; $i++) {
669 log_rd2("ADDED TO THE STREAM: ".$this->comm[$ii]);
670 $ret .= $this->comm[$ii];
672 $new_stat = $this->stat;
673 $new_subst = $this->subst;
674 $new_step = $this->step;
677 log_rd2($this->step, 'index_rd.php: after ret set');
679 if ($this->the_end == TRUE) {
680 log_rd2("LOGOUT BYE BYE!!");
681 log_auth($this->sess, "Explicit logout.");
683 if ($this->the_end == TRUE) {
686 if ($this->subst == 'sitdown') {
687 log_load("ROOM WAKEUP");
688 $this->room->room_wakeup($this);
690 else if ($this->subst == 'standup')
691 $this->room->room_outstandup($this);
693 log_rd2("LOGOUT FROM WHAT ???");
695 } /* if ($this->the_end == TRUE) { ... */
696 } /* if ($this->the_end == TRUE) { ... */
697 } /* if ($cur_step < $this->step) { */
699 /* Room::unlock_data($sem); */
700 } /* else of if ($cur_step == -1) { */
704 } // function maincheck (...
706 public static function stream_fini($transp, $init_string, $is_unrecoverable)
708 printf("xXx user::stream_fini\n");
710 // FIXME: dynamic "Transport_" type
711 $trans_class = Transport::gettype($transp);
712 $body = $trans_class::fini($init_string, self::base_get(), static::blocking_error($is_unrecoverable));
715 // return (static::blocking_error($is_unrecoverable));
729 function stream_init($init_string, $enc, &$header_out, &$body, $get, $post, $cookie)
733 printf("CLASS: [%s] base: [%s]\n", get_class($this), self::base_get());
735 log_load("index_rd_ifra_init.php");
737 if (($from = gpcs_var('from', $get, $post, $cookie)) === FALSE)
739 if (($stat = gpcs_var('stat', $get, $post, $cookie)) === FALSE)
741 if (($subst = gpcs_var('subst', $get, $post, $cookie)) === FALSE)
743 if (($step = gpcs_var('step', $get, $post, $cookie)) === FALSE)
745 if (($transp = gpcs_var('transp', $get, $post, $cookie)) === FALSE)
748 $this->rd_data_set($curtime, $transp, $enc, $stat, $subst, $step, $from);
750 $body .= $this->rd_transp->init($enc, &$header_out, $init_string, self::base_get(), $this->rd_scristp);
755 function stream_main(&$body, $get, $post, $cookie)
757 GLOBAL $G_splash_idx;
759 $CO_splashdate = "CO_splashdate".$G_splash_idx;
760 if (($splashdate = gpcs_var("$CO_splashdate", $get, $post, $cookie)) === FALSE)
762 if (($table_idx = gpcs_var("table_idx", $get, $post, $cookie)) === FALSE)
764 if (($table_token = gpcs_var("table_token", $get, $post, $cookie)) === FALSE)
767 log_rd2("FROM OUTSIDE - STAT: ".$this->rd_stat." SUBST: ".$this->rd_subst." STEP: ".$this->rd_step." FROM: ".$this->rd_from);
770 $pre_main = gettimeofday(TRUE);
772 $old_stat = $this->rd_stat;
773 $old_subst = $this->rd_subst;
774 $old_step = $this->rd_step;
775 printf("xXx PRE : rd_step %d\n", $this->rd_step);
776 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) {
777 $body .= $this->rd_transp->chunk( $this->rd_scristp++, $ret);
778 log_rd2(0, 'index_rd.php: after mop_flush (begin: '.sprintf("%f", $pre_main).')');
780 printf("xXx POST: rd_step %d\n", $this->rd_step);
785 function stream_keepalive($with_ping)
787 return ($this->rd_transp->chunk( $this->rd_scristp++, ($with_ping ? "act_ping();" : NULL)));
790 static function base_get()
792 $c = get_called_class();
793 printf("CALLED_CLASS: [%s]\n", $c);
797 function is_supp_custom()
799 if ($this->rec != FALSE) {
800 if ($this->flags & USER_FLAG_TY_SUPER) {
804 if ($this->rec->last_dona > 1356994800) {