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 // ... other usefull status ...
61 define('USER_FLAG_TY_SUSPEND', 0x400000); // done
62 define('USER_FLAG_TY_DISABLE', 0x800000); // done
64 // 240 is the right value, 600 is for fwrite error test
65 define('RD_ENDTIME_DELTA', 240);
66 define('RD_KEEPALIVE_TOUT', 4);
68 $S_load_stat = array( 'rU_heavy' => 0,
76 'btn_backtotab' => array('it' => ' torna ai tavoli ',
77 'en' => ' back to tables '),
78 'btn_btotabsup' => array('it' => ' grazie della donazione, torna ai tavoli ',
79 'en' => ' thank you for donation, back to tables ')
83 var $room; // reference to the room where the user is registered
84 var $idx; // index in the users array when you are in game
85 var $idx_orig; // index in the users array when you aren't in game
86 var $code; // authentication code
87 var $name; // name of the user
88 var $sess; // session of the user
89 var $ip; // ip of the user
90 var $lacc; // last access (for the cleanup)
91 var $laccwr; // last access (for the cleanup)
92 var $bantime; // timeout to temporary ban
93 var $stat; // status (outdoor, room, table, game, ...)
94 var $subst; // substatus for each status
95 var $step; // step of the current status
96 var $trans_step; // step to enable transition between pages (disable == -1)
98 var $rd_socket; // socket handle of push stream
99 var $rd_endtime; // end time for push stream
100 var $rd_stat; // actual status of push stream
101 var $rd_subst; // actual substatus of push stream
102 var $rd_step; // actual step of push stream
103 var $rd_from; // referer
104 var $rd_scristp; // current script step (for each session)
105 var $rd_kalive; // if no message are sent after RD_KEEPALIVE_TOUT secs we send a keepalive from server
106 var $rd_cache; // place where store failed fwrite data
107 var $rd_zls; // zlibstream object handle if compressed stream, else FALSE
108 var $rd_transp; // class that define stream encapsulation type (iframe, xhr, ...)
110 var $comm; // commands array
111 // var $asta_card; //
113 // var $handpt; // Total card points at the beginning of the current hand.
114 // var $exitislock; // Player can exit from the table ?
116 // FIXME: the table_orig field must be removed after table field verify of index management (in spawned table
117 // it is allways ZERO
118 var $table; // id of the current table when you are in game
119 var $table_orig; // id of the current table when you aren't in game
120 var $table_pos; // idx on the table
121 var $table_token;// token that identify a game on a table
122 var $flags; // Bitfield with: AUTHENTICATE: 0x02
123 var $the_end; // Flag to change the end of the session
125 var $chat_lst; // Last chat line
126 var $chattime; // Array of chat times
127 var $chat_cur; // Current chat line number
128 var $chat_ban; // Time for ban chat
129 var $chat_dlt; // Delta t for ban
132 const BASE = ""; // basepath for absolute web references
137 function create(&$room, $idx, $name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
138 if (($thiz = new User()) == FALSE)
141 $thiz->room = &$room;
143 $thiz->idx_orig = $idx;
148 $thiz->lacc = time();
149 $thiz->laccwr = time();
152 $thiz->subst = $subst;
154 $thiz->trans_step = -1;
155 $thiz->comm = array();
157 $thiz->rd_socket = NULL;
158 $thiz->rd_endtime = -1;
160 $thiz->rd_subst = "";
163 $thiz->rd_scristp = -1;
164 $thiz->rd_kalive = -1;
165 $thiz->rd_cache = "";
166 $thiz->rd_zls = FALSE;
167 $thiz->rd_transp = NULL;
169 $thiz->asta_card = -2;
170 $thiz->asta_pnt = -1;
172 $thiz->exitislock = TRUE;
176 $thiz->chattime = array_fill(0, CHAT_N, 0);
178 $thiz->chat_lst = "";
182 $thiz->table_orig = $table;
183 $thiz->table = $table;
184 $thiz->table_pos = -1;
185 $thiz->table_token= "";
186 $thiz->shm_sz = SHM_DIMS_U_MIN;
192 $this->idx = $from->idx;
193 $this->idx_orig = $from->idx;
194 $this->code = $from->code;
195 $this->name = $from->name;
196 $this->sess = $from->sess;
197 $this->ip = $from->ip;
198 $this->lacc = $from->lacc;
199 $this->laccwr = $from->laccwr;
200 $this->bantime = $from->bantime;
201 $this->stat = $from->stat;
202 $this->subst = $from->subst;
203 $this->step = $from->step;
204 $this->trans_step = $from->trans_step;
205 $this->comm = array();
207 $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N));
208 for ($i = $i_start ; $i < $from->step ; $i++) {
210 if (isset($from->comm[$ii])) {
211 $this->comm[$ii] = $from->comm[$ii];
214 $this->asta_card = $from->asta_card;
215 $this->asta_pnt = $from->asta_pnt;
216 $this->handpt = $from->handpt;
217 $this->exitislock = $from->exitislock;
219 $this->flags = $from->flags;
221 $this->chattime = array();
222 for ($i = 0 ; $i < CHAT_N ; $i++)
223 $this->chattime[$i] = $from->chattime[$i];
224 $this->chat_cur = $from->chat_cur;
225 $this->chat_lst = $from->chat_lst;
226 $this->chat_ban = $from->chat_ban;
227 $this->chat_dlt = $from->chat_dlt;
229 $this->table_orig = $from->table_orig;
230 $this->table = $from->table;
231 $this->table_pos = $from->table_pos;
232 $this->table_token = $from->table_token;
233 $this->the_end = $from->the_end;
234 $this->shm_sz = $from->shm_sz;
239 function myclone($from)
241 if (($thiz = new User()) == FALSE)
249 function spawn($from, $table, $table_pos)
251 if (($thiz = new User()) == FALSE)
254 $thiz->idx = $from->idx;
255 $thiz->idx_orig = $from->idx;
256 $thiz->code = $from->code;
257 $thiz->name = $from->name;
258 $thiz->sess = $from->sess;
259 $thiz->ip = $from->ip;
260 $thiz->lacc = $from->lacc;
261 $thiz->laccwr = $from->laccwr;
262 $thiz->bantime = $from->bantime;
263 $thiz->stat = $from->stat;
264 $thiz->subst = $from->subst;
265 $thiz->step = $from->step;
266 $thiz->trans_step = $from->trans_step;
267 $thiz->comm = array();
270 $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N));
271 for ($i = $i_start ; $i < $from->step ; $i++) {
272 log_wr("TRY PUSH:".$i);
274 $thiz->comm[$ii] = $from->comm[$ii];
277 $thiz->asta_card = $from->asta_card;
278 $thiz->asta_pnt = $from->asta_pnt;
279 $thiz->handpt = $from->handpt;
280 $thiz->exitislock = $from->exitislock;
281 $thiz->the_end = $from->the_end;
283 $thiz->flags = $from->flags;
285 $thiz->chattime = array_fill(0, CHAT_N, 0);
287 $thiz->chat_lst = "";
292 $thiz->table_orig = $table;
294 $thiz->table_pos = $table_pos;
295 $thiz->table_token = $from->table_token;
296 $thiz->shm_sz = $from->shm_sz;
301 function rd_data_set($curtime, $transp, $enc, $stat, $subst, $step, $from)
303 $this->rd_endtime = $curtime + RD_ENDTIME_DELTA;
304 $this->rd_stat = $stat;
305 $this->rd_subst = $subst;
306 $this->rd_step = $step;
307 $this->rd_from = $from;
308 $this->rd_scristp = 0;
309 $this->rd_kalive = $curtime + RD_KEEPALIVE_TOUT;
310 $this->rd_zls = ZLibStream::create($enc);
311 $this->rd_transp = Transport::create($transp);
314 function rd_socket_get() {
315 return ($this->rd_socket);
318 function rd_socket_set($sock) {
321 $this->rd_zls->destroy();
322 $this->rd_zls = FALSE;
325 $this->rd_socket = $sock;
328 function rd_kalive_get()
330 return ($this->rd_kalive);
333 function rd_kalive_set($tm)
335 $this->rd_kalive = $tm;
338 function rd_kalive_is_expired($tm)
340 // printf("rd_kalive %d tm %d\n", $this->rd_kalive, $tm);
341 return ($this->rd_kalive < $tm);
344 function rd_endtime_is_expired($tm)
346 // printf("rd_endtime %d tm %d\n", $this->rd_kalive, $tm);
347 return ($this->rd_endtime < $tm);
350 function rd_kalive_reset($tm)
352 $this->rd_kalive = $tm + RD_KEEPALIVE_TOUT;
355 function rd_cache_get()
357 return ($this->rd_cache);
360 function rd_cache_set($cache)
362 $this->rd_cache = $cache;
365 function rd_zls_get()
367 return ($this->rd_zls);
374 function code_get() {
375 return ($this->code);
378 function stat_set($stat) {
379 log_main("sess: [".$this->sess. "] NEW STAT: [".$stat."]");
380 $this->stat = "$stat";
383 if (validate_sess($this->sess)) {
384 if (file_exists(PROXY_PATH) == FALSE)
385 mkdir(PROXY_PATH, 0775, TRUE);
386 $fp = @fopen(PROXY_PATH."/".$this->sess.".stat", 'w');
387 fwrite($fp, sprintf("%s\n",$this->stat));
393 function step_set($step)
395 $this->step = $step & 0x7fffffff;
400 function step_inc($delta = 1) {
401 $this->step += $delta;
402 /* modularization because unpack() not manage unsigned 32bit int correctly */
403 $this->step &= 0x7fffffff;
412 if (validate_sess($this->sess) == FALSE)
414 if (file_exists(PROXY_PATH) == FALSE)
415 mkdir(PROXY_PATH, 0775, TRUE);
416 if (($fp = @fopen(PROXY_PATH."/".$this->sess.".step", 'w')) == FALSE)
418 fwrite($fp, pack("LL",$this->step, $this->idx));
421 log_main("step_set [".$this->sess. "] [".$this->step."]");
429 static function load_step($sess)
433 if (validate_sess($sess) == FALSE)
436 if (file_exists(PROXY_PATH) == FALSE)
437 mkdir(PROXY_PATH, 0775, TRUE);
438 if (($fp = @fopen(PROXY_PATH."/".$sess.".step", 'rb')) == FALSE)
440 if (($s = fread($fp, 8)) == FALSE)
442 if (mb_strlen($s, "ASCII") != 8)
444 $arr = unpack('Ls/Li', $s);
447 // log_rd2("A0: ".$arr[0]." A1: ".$arr[1]);
454 log_rd2("STEP_GET [".$sess."]: return false ");
459 static function unproxy_step($sess) {
460 log_rd2("UNPROXY: ".PROXY_PATH."/".$sess.".step");
461 if (file_exists(PROXY_PATH) == FALSE)
463 @unlink(PROXY_PATH."/".$sess.".step");
468 log_legal($curtime, $this->ip, $this, "STAT:LOGOUT", '');
470 $tmp_sess = $this->sess;
472 self::unproxy_step($tmp_sess);
473 $this->name = ""; // OK here
474 while (array_pop($this->comm) != NULL);
476 $this->chattime = array_fill(0, CHAT_N, 0);
478 $this->chat_lst = "";
481 $this->the_end = FALSE;
484 function myname_innerHTML()
486 $class_id = ($this->flags & USER_FLAG_AUTH) + 1;
488 return (sprintf('$("myname").innerHTML = "<span class=\"au%d\">%s</span>";', $class_id,
489 xcape($this->name,ENT_COMPAT,"UTF-8")));
492 /* INDEX_RD_IFRA PORT */
494 static function blocking_error($is_unrecoverable)
496 log_crit("BLOCKING_ERROR UNREC: ".($is_unrecoverable ? "TRUE" : "FALSE"));
497 return (sprintf(($is_unrecoverable ? 'xstm.stop(); ' : '').'window.onbeforeunload = null; window.onunload = null; document.location.assign("index.php");'));
500 // FIXME TO SUPPORT iframe
501 protected function page_sync($sess, $page, $table_idx, $table_token)
503 // log_rd2("page_sync:".var_export(debug_backtrace()));
505 log_rd2("PAGE_SYNC");
506 printf("xXx USER::PAGE_SYNC [%s]\n", get_class($this));
507 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));
513 protected function maincheck($cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step, $splashdate, $table_idx, $table_token)
515 GLOBAL $G_lang, $mlang_indrd;
516 // GLOBAL $first_loop;
517 GLOBAL $G_with_splash, $G_splash_content, $G_splash_interval, $G_splash_idx;
518 GLOBAL $G_splash_w, $G_splash_h, $G_splash_timeout;
519 $CO_splashdate = "CO_splashdate".$G_splash_idx;
520 $$CO_splashdate = $splashdate;
524 log_rd("maincheck begin");
529 /* Nothing changed, return. */
530 if ($cur_step == $this->step)
533 log_rd2("do other cur_stat[".$cur_stat."] user->stat[".$this->stat."] cur_step[".$cur_step."] user_step[".$this->step."]");
535 if ($cur_step == -1) {
537 * if $cur_step == -1 load the current state from the main struct
540 $S_load_stat['wR_minusone']++;
542 // if ($this->the_end == TRUE) {
543 // log_rd2("main_check: the end".var_export(debug_backtrace()));
546 if ($this->trans_step != -1) {
547 log_rd2("TRANS USATO ".$this->trans_step);
548 $cur_step = $this->trans_step;
549 $this->trans_step = -1;
552 log_rd2("TRANS NON ATTIVATO");
557 /* this part I suppose is read only on $this->room structure */
558 if ($cur_step == -1) {
559 log_rd2("PRE-NEWSTAT: ".$this->stat);
561 if ($this->stat == 'room') {
562 log_rd("roomma ".$this->step);
565 if ($G_with_splash &&
566 ($$CO_splashdate < $curtime - $G_splash_interval ||
567 $$CO_splashdate > $curtime)) {
568 $is_super = $this->flags & USER_FLAG_TY_SUPER;
569 $ret .= show_notify_ex(str_replace("\n", " ", $G_splash_content[$G_lang]),
570 ($is_super ? 0 : $G_splash_timeout),
571 $mlang_indrd[($is_super ? 'btn_btotabsup' : 'btn_backtotab')][$G_lang],
572 $G_splash_w, $G_splash_h, true,
573 ($is_super ? 0 : $G_splash_timeout));
574 $ret .= sprintf('|createCookie("CO_splashdate%d", %d, 24*365, cookiepath);', $G_splash_idx, $curtime);
576 $ret .= $this->room->show_room($this->step, $this);
578 // TODO uncomment and test
579 /* NOTE the sets went common */
580 $new_stat = $this->stat;
581 $new_subst = $this->subst;
582 $new_step = $this->step;
589 else if ($this->stat == 'table') {
591 printf("xXx USER::MAINCHECK1 [%s]\n", get_class($this));
593 return ($this->page_sync($this->sess, "briskin5/index.php", $this->table, $this->table_token));
595 log_rd2("NEWSTAT: ".$this->stat);
596 } /* if ($cur_step == -1) { */
598 /* $sem = Room::lock_data(FALSE); */
599 $S_load_stat['rU_heavy']++;
601 if ($cur_step < $this->step) {
603 if ($cur_step + COMM_N < $this->step) {
604 if (($cur_stat != $this->stat)) {
605 $to_stat = $this->stat;
606 /* Room::unlock_data($sem); */
608 printf("xXx USER::MAINCHECK2 [%s]\n", get_class($this));
609 return ($this->page_sync($this->sess, ($to_stat == "table" ? "briskin5/index.php" : "index.php"), $this->table, $this->table_token));
611 log_rd2("lost history, refresh from scratch");
615 for ($i = $cur_step ; $i < $this->step ; $i++) {
617 log_rd2("ADDED TO THE STREAM: ".$this->comm[$ii]);
618 $ret .= $this->comm[$ii];
620 $new_stat = $this->stat;
621 $new_subst = $this->subst;
622 $new_step = $this->step;
625 log_rd2($this->step, 'index_rd.php: after ret set');
627 if ($this->the_end == TRUE) {
628 log_rd2("LOGOUT BYE BYE!!");
629 log_auth($this->sess, "Explicit logout.");
631 if ($this->the_end == TRUE) {
634 if ($this->subst == 'sitdown') {
635 log_load("ROOM WAKEUP");
636 $this->room->room_wakeup($this);
638 else if ($this->subst == 'standup')
639 $this->room->room_outstandup($this);
641 log_rd2("LOGOUT FROM WHAT ???");
643 } /* if ($this->the_end == TRUE) { ... */
644 } /* if ($this->the_end == TRUE) { ... */
645 } /* if ($cur_step < $this->step) { */
647 /* Room::unlock_data($sem); */
648 } /* else of if ($cur_step == -1) { */
652 } // function maincheck (...
654 public static function stream_fini($transp, $init_string, $is_unrecoverable)
656 printf("xXx user::stream_fini\n");
658 // FIXME: dynamic "Transport_" type
659 $trans_class = Transport::gettype($transp);
660 $body = $trans_class::fini($init_string, self::base_get(), static::blocking_error($is_unrecoverable));
663 // return (static::blocking_error($is_unrecoverable));
677 function stream_init($init_string, $enc, &$header_out, &$body, $get, $post, $cookie)
681 printf("CLASS: [%s] base: [%s]\n", get_class($this), self::base_get());
683 log_load("index_rd_ifra_init.php");
685 if (($from = gpcs_var('from', $get, $post, $cookie)) === FALSE)
687 if (($stat = gpcs_var('stat', $get, $post, $cookie)) === FALSE)
689 if (($subst = gpcs_var('subst', $get, $post, $cookie)) === FALSE)
691 if (($step = gpcs_var('step', $get, $post, $cookie)) === FALSE)
693 if (($transp = gpcs_var('transp', $get, $post, $cookie)) === FALSE)
696 $this->rd_data_set($curtime, $transp, $enc, $stat, $subst, $step, $from);
698 $body .= $this->rd_transp->init($enc, &$header_out, $init_string, self::base_get(), $this->rd_scristp);
703 function stream_main(&$body, $get, $post, $cookie)
705 GLOBAL $G_splash_idx;
707 $CO_splashdate = "CO_splashdate".$G_splash_idx;
708 if (($splashdate = gpcs_var("$CO_splashdate", $get, $post, $cookie)) === FALSE)
710 if (($table_idx = gpcs_var("table_idx", $get, $post, $cookie)) === FALSE)
712 if (($table_token = gpcs_var("table_token", $get, $post, $cookie)) === FALSE)
715 log_rd2("FROM OUTSIDE - STAT: ".$this->rd_stat." SUBST: ".$this->rd_subst." STEP: ".$this->rd_step." FROM: ".$this->rd_from);
718 $pre_main = gettimeofday(TRUE);
720 $old_stat = $this->rd_stat;
721 $old_subst = $this->rd_subst;
722 $old_step = $this->rd_step;
723 printf("xXx PRE : rd_step %d\n", $this->rd_step);
724 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) {
725 $body .= $this->rd_transp->chunk( $this->rd_scristp++, $ret);
726 log_rd2(0, 'index_rd.php: after mop_flush (begin: '.sprintf("%f", $pre_main).')');
728 printf("xXx POST: rd_step %d\n", $this->rd_step);
733 function stream_keepalive($with_ping)
735 return ($this->rd_transp->chunk( $this->rd_scristp++, ($with_ping ? "act_ping();" : NULL)));
738 static function base_get()
740 $c = get_called_class();
741 printf("CALLED_CLASS: [%s]\n", $c);