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
50 define('USER_FLAG_S_ALL', 0xf00); // done
52 /* type of user normal, supporter etc ... */
53 define('USER_FLAG_TY_ALL', 0xff0000); // done
54 define('USER_FLAG_TY_NORM', 0x010000); // done
55 define('USER_FLAG_TY_SUPER', 0x020000); // done
56 // ... other usefull status ...
57 define('USER_FLAG_TY_SUSPEND', 0x400000); // done
58 define('USER_FLAG_TY_DISABLE', 0x800000); // done
60 // 240 is the right value, 600 is for fwrite error test
61 define('RD_ENDTIME_DELTA', 240);
62 define('RD_KEEPALIVE_TOUT', 4);
64 $S_load_stat = array( 'rU_heavy' => 0,
72 'btn_backtotab' => array('it' => ' torna ai tavoli ',
73 'en' => ' back to tables '),
74 'btn_btotabsup' => array('it' => ' grazie della donazione, torna ai tavoli ',
75 'en' => ' thank you for donation, back to tables ')
79 var $room; // reference to the room where the user is registered
80 var $idx; // index in the users array when you are in game
81 var $idx_orig; // index in the users array when you aren't in game
82 var $code; // authentication code
83 var $name; // name of the user
84 var $sess; // session of the user
85 var $ip; // ip of the user
86 var $lacc; // last access (for the cleanup)
87 var $laccwr; // last access (for the cleanup)
88 var $bantime; // timeout to temporary ban
89 var $stat; // status (outdoor, room, table, game, ...)
90 var $subst; // substatus for each status
91 var $step; // step of the current status
92 var $trans_step; // step to enable transition between pages (disable == -1)
94 var $rd_socket; // socket handle of push stream
95 var $rd_endtime; // end time for push stream
96 var $rd_stat; // actual status of push stream
97 var $rd_subst; // actual substatus of push stream
98 var $rd_step; // actual step of push stream
99 var $rd_from; // referer
100 var $rd_scristp; // current script step (for each session)
101 var $rd_kalive; // if no message are sent after RD_KEEPALIVE_TOUT secs we send a keepalive from server
102 var $rd_cache; // place where store failed fwrite data
103 var $rd_zls; // zlibstream object handle if compressed stream, else FALSE
104 var $rd_transp; // class that define stream encapsulation type (iframe, xhr, ...)
106 var $comm; // commands array
107 // var $asta_card; //
109 // var $handpt; // Total card points at the beginning of the current hand.
110 // var $exitislock; // Player can exit from the table ?
112 // FIXME: the table_orig field must be removed after table field verify of index management (in spawned table
113 // it is allways ZERO
114 var $table; // id of the current table when you are in game
115 var $table_orig; // id of the current table when you aren't in game
116 var $table_pos; // idx on the table
117 var $table_token;// token that identify a game on a table
118 var $flags; // Bitfield with: AUTHENTICATE: 0x02
119 var $the_end; // Flag to change the end of the session
121 var $chat_lst; // Last chat line
122 var $chattime; // Array of chat times
123 var $chat_cur; // Current chat line number
124 var $chat_ban; // Time for ban chat
125 var $chat_dlt; // Delta t for ban
128 const BASE = ""; // basepath for absolute web references
133 function create(&$room, $idx, $name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
134 if (($thiz = new User()) == FALSE)
137 $thiz->room = &$room;
139 $thiz->idx_orig = $idx;
144 $thiz->lacc = time();
145 $thiz->laccwr = time();
148 $thiz->subst = $subst;
150 $thiz->trans_step = -1;
151 $thiz->comm = array();
153 $thiz->rd_socket = NULL;
154 $thiz->rd_endtime = -1;
156 $thiz->rd_subst = "";
159 $thiz->rd_scristp = -1;
160 $thiz->rd_kalive = -1;
161 $thiz->rd_cache = "";
162 $thiz->rd_zls = FALSE;
163 $thiz->rd_transp = NULL;
165 $thiz->asta_card = -2;
166 $thiz->asta_pnt = -1;
168 $thiz->exitislock = TRUE;
172 $thiz->chattime = array_fill(0, CHAT_N, 0);
174 $thiz->chat_lst = "";
178 $thiz->table_orig = $table;
179 $thiz->table = $table;
180 $thiz->table_pos = -1;
181 $thiz->table_token= "";
182 $thiz->shm_sz = SHM_DIMS_U_MIN;
188 $this->idx = $from->idx;
189 $this->idx_orig = $from->idx;
190 $this->code = $from->code;
191 $this->name = $from->name;
192 $this->sess = $from->sess;
193 $this->ip = $from->ip;
194 $this->lacc = $from->lacc;
195 $this->laccwr = $from->laccwr;
196 $this->bantime = $from->bantime;
197 $this->stat = $from->stat;
198 $this->subst = $from->subst;
199 $this->step = $from->step;
200 $this->trans_step = $from->trans_step;
201 $this->comm = array();
203 $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N));
204 for ($i = $i_start ; $i < $from->step ; $i++) {
206 if (isset($from->comm[$ii])) {
207 $this->comm[$ii] = $from->comm[$ii];
210 $this->asta_card = $from->asta_card;
211 $this->asta_pnt = $from->asta_pnt;
212 $this->handpt = $from->handpt;
213 $this->exitislock = $from->exitislock;
215 $this->flags = $from->flags;
217 $this->chattime = array();
218 for ($i = 0 ; $i < CHAT_N ; $i++)
219 $this->chattime[$i] = $from->chattime[$i];
220 $this->chat_cur = $from->chat_cur;
221 $this->chat_lst = $from->chat_lst;
222 $this->chat_ban = $from->chat_ban;
223 $this->chat_dlt = $from->chat_dlt;
225 $this->table_orig = $from->table_orig;
226 $this->table = $from->table;
227 $this->table_pos = $from->table_pos;
228 $this->table_token = $from->table_token;
229 $this->the_end = $from->the_end;
230 $this->shm_sz = $from->shm_sz;
235 function myclone($from)
237 if (($thiz = new User()) == FALSE)
245 function spawn($from, $table, $table_pos)
247 if (($thiz = new User()) == FALSE)
250 $thiz->idx = $from->idx;
251 $thiz->idx_orig = $from->idx;
252 $thiz->code = $from->code;
253 $thiz->name = $from->name;
254 $thiz->sess = $from->sess;
255 $thiz->ip = $from->ip;
256 $thiz->lacc = $from->lacc;
257 $thiz->laccwr = $from->laccwr;
258 $thiz->bantime = $from->bantime;
259 $thiz->stat = $from->stat;
260 $thiz->subst = $from->subst;
261 $thiz->step = $from->step;
262 $thiz->trans_step = $from->trans_step;
263 $thiz->comm = array();
266 $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N));
267 for ($i = $i_start ; $i < $from->step ; $i++) {
268 log_wr("TRY PUSH:".$i);
270 $thiz->comm[$ii] = $from->comm[$ii];
273 $thiz->asta_card = $from->asta_card;
274 $thiz->asta_pnt = $from->asta_pnt;
275 $thiz->handpt = $from->handpt;
276 $thiz->exitislock = $from->exitislock;
277 $thiz->the_end = $from->the_end;
279 $thiz->flags = $from->flags;
281 $thiz->chattime = array_fill(0, CHAT_N, 0);
283 $thiz->chat_lst = "";
288 $thiz->table_orig = $table;
290 $thiz->table_pos = $table_pos;
291 $thiz->table_token = $from->table_token;
292 $thiz->shm_sz = $from->shm_sz;
297 function rd_data_set($curtime, $enc, $stat, $subst, $step, $from)
299 $this->rd_endtime = $curtime + RD_ENDTIME_DELTA;
300 $this->rd_stat = $stat;
301 $this->rd_subst = $subst;
302 $this->rd_step = $step;
303 $this->rd_from = $from;
304 $this->rd_scristp = 0;
305 $this->rd_kalive = $curtime + RD_KEEPALIVE_TOUT;
306 $this->rd_zls = ZLibStream::create($enc);
307 $this->rd_transp = new Transport_iframe();
310 function rd_socket_get() {
311 return ($this->rd_socket);
314 function rd_socket_set($sock) {
317 $this->rd_zls->destroy();
318 $this->rd_zls = FALSE;
321 $this->rd_socket = $sock;
324 function rd_kalive_get()
326 return ($this->rd_kalive);
329 function rd_kalive_set($tm)
331 $this->rd_kalive = $tm;
334 function rd_kalive_is_expired($tm)
336 // printf("rd_kalive %d tm %d\n", $this->rd_kalive, $tm);
337 return ($this->rd_kalive < $tm);
340 function rd_endtime_is_expired($tm)
342 // printf("rd_endtime %d tm %d\n", $this->rd_kalive, $tm);
343 return ($this->rd_endtime < $tm);
346 function rd_kalive_reset($tm)
348 $this->rd_kalive = $tm + RD_KEEPALIVE_TOUT;
351 function rd_cache_get()
353 return ($this->rd_cache);
356 function rd_cache_set($cache)
358 $this->rd_cache = $cache;
361 function rd_zls_get()
363 return ($this->rd_zls);
370 function code_get() {
371 return ($this->code);
374 function stat_set($stat) {
375 log_main("sess: [".$this->sess. "] NEW STAT: [".$stat."]");
376 $this->stat = "$stat";
379 if (validate_sess($this->sess)) {
380 if (file_exists(PROXY_PATH) == FALSE)
381 mkdir(PROXY_PATH, 0775, TRUE);
382 $fp = @fopen(PROXY_PATH."/".$this->sess.".stat", 'w');
383 fwrite($fp, sprintf("%s\n",$this->stat));
389 function step_set($step)
391 $this->step = $step & 0x7fffffff;
396 function step_inc($delta = 1) {
397 $this->step += $delta;
398 /* modularization because unpack() not manage unsigned 32bit int correctly */
399 $this->step &= 0x7fffffff;
408 if (validate_sess($this->sess) == FALSE)
410 if (file_exists(PROXY_PATH) == FALSE)
411 mkdir(PROXY_PATH, 0775, TRUE);
412 if (($fp = @fopen(PROXY_PATH."/".$this->sess.".step", 'w')) == FALSE)
414 fwrite($fp, pack("LL",$this->step, $this->idx));
417 log_main("step_set [".$this->sess. "] [".$this->step."]");
425 static function load_step($sess)
429 if (validate_sess($sess) == FALSE)
432 if (file_exists(PROXY_PATH) == FALSE)
433 mkdir(PROXY_PATH, 0775, TRUE);
434 if (($fp = @fopen(PROXY_PATH."/".$sess.".step", 'rb')) == FALSE)
436 if (($s = fread($fp, 8)) == FALSE)
438 if (mb_strlen($s, "ASCII") != 8)
440 $arr = unpack('Ls/Li', $s);
443 // log_rd2("A0: ".$arr[0]." A1: ".$arr[1]);
450 log_rd2("STEP_GET [".$sess."]: return false ");
455 static function unproxy_step($sess) {
456 log_rd2("UNPROXY: ".PROXY_PATH."/".$sess.".step");
457 if (file_exists(PROXY_PATH) == FALSE)
459 @unlink(PROXY_PATH."/".$sess.".step");
464 log_legal($curtime, 'xxx', $this, "STAT:LOGOUT", '');
466 $tmp_sess = $this->sess;
468 self::unproxy_step($tmp_sess);
469 $this->name = ""; // OK here
470 while (array_pop($this->comm) != NULL);
472 $this->chattime = array_fill(0, CHAT_N, 0);
474 $this->chat_lst = "";
477 $this->the_end = FALSE;
480 function myname_innerHTML()
482 $class_id = ($this->flags & USER_FLAG_AUTH) + 1;
484 return (sprintf('$("myname").innerHTML = "<span class=\"au%d\">%s</span>";', $class_id,
485 xcape($this->name,ENT_COMPAT,"UTF-8")));
488 /* INDEX_RD_IFRA PORT */
490 static function blocking_error($is_unrecoverable)
492 log_crit("BLOCKING_ERROR UNREC: ".($is_unrecoverable ? "TRUE" : "FALSE"));
493 return (sprintf(($is_unrecoverable ? 'xstm.stop(); ' : '').'window.onbeforeunload = null; window.onunload = null; document.location.assign("index.php");'));
496 // FIXME TO SUPPORT iframe
497 protected function page_sync($sess, $page, $table_idx, $table_token)
499 // log_rd2("page_sync:".var_export(debug_backtrace()));
501 log_rd2("PAGE_SYNC");
502 printf("xXx USER::PAGE_SYNC [%s]\n", get_class($this));
503 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));
509 protected function maincheck($cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step, $splashdate, $table_idx, $table_token)
511 GLOBAL $G_lang, $mlang_indrd;
512 // GLOBAL $first_loop;
513 GLOBAL $G_with_splash, $G_splash_content, $G_splash_interval, $G_splash_idx;
514 GLOBAL $G_splash_w, $G_splash_h, $G_splash_timeout;
515 $CO_splashdate = "CO_splashdate".$G_splash_idx;
516 $$CO_splashdate = $splashdate;
520 log_rd("maincheck begin");
525 /* Nothing changed, return. */
526 if ($cur_step == $this->step)
529 log_rd2("do other cur_stat[".$cur_stat."] user->stat[".$this->stat."] cur_step[".$cur_step."] user_step[".$this->step."]");
531 if ($cur_step == -1) {
533 * if $cur_step == -1 load the current state from the main struct
536 $S_load_stat['wR_minusone']++;
538 // if ($this->the_end == TRUE) {
539 // log_rd2("main_check: the end".var_export(debug_backtrace()));
542 if ($this->trans_step != -1) {
543 log_rd2("TRANS USATO ".$this->trans_step);
544 $cur_step = $this->trans_step;
545 $this->trans_step = -1;
548 log_rd2("TRANS NON ATTIVATO");
553 /* this part I suppose is read only on $this->room structure */
554 if ($cur_step == -1) {
555 log_rd2("PRE-NEWSTAT: ".$this->stat);
557 if ($this->stat == 'room') {
558 log_rd("roomma ".$this->step);
561 if ($G_with_splash &&
562 ($$CO_splashdate < $curtime - $G_splash_interval ||
563 $$CO_splashdate > $curtime)) {
564 $is_super = $this->flags & USER_FLAG_TY_SUPER;
565 $ret .= show_notify_ex(str_replace("\n", " ", $G_splash_content[$G_lang]),
566 ($is_super ? 0 : $G_splash_timeout),
567 $mlang_indrd[($is_super ? 'btn_btotabsup' : 'btn_backtotab')][$G_lang],
568 $G_splash_w, $G_splash_h, true,
569 ($is_super ? 0 : $G_splash_timeout));
570 $ret .= sprintf('|createCookie("CO_splashdate%d", %d, 24*365, cookiepath);', $G_splash_idx, $curtime);
572 $ret .= $this->room->show_room($this->step, $this);
574 // TODO uncomment and test
575 /* NOTE the sets went common */
576 $new_stat = $this->stat;
577 $new_subst = $this->subst;
578 $new_step = $this->step;
585 else if ($this->stat == 'table') {
587 printf("xXx USER::MAINCHECK1 [%s]\n", get_class($this));
589 return ($this->page_sync($this->sess, "briskin5/index.php", $this->table, $this->table_token));
591 log_rd2("NEWSTAT: ".$this->stat);
592 } /* if ($cur_step == -1) { */
594 /* $sem = Room::lock_data(FALSE); */
595 $S_load_stat['rU_heavy']++;
597 if ($cur_step < $this->step) {
599 if ($cur_step + COMM_N < $this->step) {
600 if (($cur_stat != $this->stat)) {
601 $to_stat = $this->stat;
602 /* Room::unlock_data($sem); */
604 printf("xXx USER::MAINCHECK2 [%s]\n", get_class($this));
605 return ($this->page_sync($this->sess, ($to_stat == "table" ? "briskin5/index.php" : "index.php"), $this->table, $this->table_token));
607 log_rd2("lost history, refresh from scratch");
611 for ($i = $cur_step ; $i < $this->step ; $i++) {
613 log_rd2("ADDED TO THE STREAM: ".$this->comm[$ii]);
614 $ret .= $this->comm[$ii];
616 $new_stat = $this->stat;
617 $new_subst = $this->subst;
618 $new_step = $this->step;
621 log_rd2($this->step, 'index_rd.php: after ret set');
623 if ($this->the_end == TRUE) {
624 log_rd2("LOGOUT BYE BYE!!");
625 log_auth($this->sess, "Explicit logout.");
627 if ($this->the_end == TRUE) {
630 if ($this->subst == 'sitdown') {
631 log_load("ROOM WAKEUP");
632 $this->room->room_wakeup($this);
634 else if ($this->subst == 'standup')
635 $this->room->room_outstandup($this);
637 log_rd2("LOGOUT FROM WHAT ???");
639 } /* if ($this->the_end == TRUE) { ... */
640 } /* if ($this->the_end == TRUE) { ... */
641 } /* if ($cur_step < $this->step) { */
643 /* Room::unlock_data($sem); */
644 } /* else of if ($cur_step == -1) { */
648 } // function maincheck (...
650 public static function stream_fini($init_string, $is_unrecoverable)
652 printf("xXx user::stream_fini\n");
654 // FIXME: dynamic "Transport_" type
655 $tans_class = "Transport_iframe";
656 $body = $tans_class::fini($init_string, static::blocking_error($is_unrecoverable));
659 // return (static::blocking_error($is_unrecoverable));
673 function stream_init($init_string, $enc, &$header_out, &$body, $get, $post, $cookie)
677 printf("CLASS: [%s] base: [%s]\n", get_class($this), self::base_get());
679 // (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);
681 log_load("index_rd_ifra_init.php");
683 if (($from = gpcs_var('from', $get, $post, $cookie)) === FALSE)
685 if (($stat = gpcs_var('stat', $get, $post, $cookie)) === FALSE)
687 if (($subst = gpcs_var('subst', $get, $post, $cookie)) === FALSE)
689 if (($step = gpcs_var('step', $get, $post, $cookie)) === FALSE)
692 $this->rd_data_set($curtime, $enc, $stat, $subst, $step, $from);
694 $body .= $this->rd_transp->init($enc, &$header_out, $init_string, self::base_get(), $this->rd_scristp);
699 function stream_main(&$body, $get, $post, $cookie)
701 GLOBAL $G_splash_idx;
703 $CO_splashdate = "CO_splashdate".$G_splash_idx;
704 if (($splashdate = gpcs_var("$CO_splashdate", $get, $post, $cookie)) === FALSE)
706 if (($table_idx = gpcs_var("table_idx", $get, $post, $cookie)) === FALSE)
708 if (($table_token = gpcs_var("table_token", $get, $post, $cookie)) === FALSE)
711 log_rd2("FROM OUTSIDE - STAT: ".$this->rd_stat." SUBST: ".$this->rd_subst." STEP: ".$this->rd_step." FROM: ".$this->rd_from);
714 $pre_main = gettimeofday(TRUE);
716 $old_stat = $this->rd_stat;
717 $old_subst = $this->rd_subst;
718 $old_step = $this->rd_step;
719 printf("xXx PRE : rd_step %d\n", $this->rd_step);
720 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) {
721 $body .= $this->rd_transp->chunk( $this->rd_scristp++, $ret);
722 log_rd2(0, 'index_rd.php: after mop_flush (begin: '.sprintf("%f", $pre_main).')');
724 printf("xXx POST: rd_step %d\n", $this->rd_step);
729 function stream_keepalive($with_ping)
731 return ($this->rd_transp->chunk( $this->rd_scristp++, ($with_ping ? "act_ping();" : NULL)));
734 static function base_get()
736 $c = get_called_class();
737 printf("CALLED_CLASS: [%s]\n", $c);