+
+ function chatt_send($user, $mesg)
+ {
+ GLOBAL $mlang_brisk, $G_lang;
+
+ if ($user->stat == 'table') {
+ $table = &$this->table[$user->table];
+ }
+
+ $user_mesg = substr($mesg,6);
+
+ $curtime = time();
+
+ $dt = date("H:i ", $curtime);
+ if (strncmp($user_mesg, "/nick ", 6) == 0) {
+ log_main($user->sess." chatt_send BEGIN");
+
+ if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
+ $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
+ $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_brisk['nickmust'][$G_lang]);
+ $user->step_inc();
+
+ return;
+ }
+ $user_mesg = "COMMAND ".$user_mesg;
+ // Search dup name
+ // change
+ // update local graph
+ // update remote graphs
+ for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
+ $user_cur = $this->user[$i];
+ // if ($user_cur->sess == '' || $user_cur->stat != 'room')
+ if ($user_cur->sess == '')
+ continue;
+ if (strcasecmp($user_cur->name, $name_new) == 0) {
+ $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
+
+ $premsg = sprintf($mlang_brisk['nickdupl'][$G_lang], xcape($name_new));
+ $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $premsg);
+ $user->step_inc();
+ break;
+ }
+ }
+ if ($i == BIN5_MAX_PLAYERS) {
+ if ($user->flags & USER_FLAG_AUTH && strcasecmp($user->name,$name_new) != 0) {
+ if ($this->table[$user->table]->auth_only == TRUE) {
+ $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
+ $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"<b>%s</b>");', $dt, NICKSERV, $mlang_brisk['authchan'][$G_lang]);
+ $user->step_inc();
+ return;
+ }
+ else {
+ $user->flags &= ~USER_FLAG_AUTH; // Remove auth if name changed
+ }
+ }
+
+ $user->name = $name_new;
+
+ log_main($user->sess." chatt_send start set");
+
+
+ for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
+ log_main($user->sess." chatt_send set loop");
+
+ $user_cur = &$this->user[$i];
+ if ($user_cur->sess == '')
+ continue;
+
+ if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
+ $table = &$this->table[$user->table];
+
+ $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
+ $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
+ $this->user[$table->player[($user_cur->table_pos) % BIN5_PLAYERS_N]]->flags,
+ xcape($this->user[$table->player[($user_cur->table_pos) % BIN5_PLAYERS_N]]->name),
+
+ $this->user[$table->player[($user_cur->table_pos+1) % BIN5_PLAYERS_N]]->flags,
+ xcape($this->user[$table->player[($user_cur->table_pos+1) % BIN5_PLAYERS_N]]->name),
+
+ $this->user[$table->player[($user_cur->table_pos+2) % BIN5_PLAYERS_N]]->flags,
+ xcape($this->user[$table->player[($user_cur->table_pos+2) % BIN5_PLAYERS_N]]->name),
+
+ (BIN5_PLAYERS_N == 3 ? 0: $this->user[$table->player[($user_cur->table_pos+3) % BIN5_PLAYERS_N]]->flags),
+ (BIN5_PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3) % BIN5_PLAYERS_N]]->name)),
+
+ (BIN5_PLAYERS_N == 3 ? 0: $this->user[$table->player[($user_cur->table_pos+4) % BIN5_PLAYERS_N]]->flags),
+ (BIN5_PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4) % BIN5_PLAYERS_N]]->name)));
+ if ($user_cur == $user) {
+ $user_cur->comm[$user_cur->step % COMM_N] .= $user_cur->myname_innerHTML();
+ }
+ $user_cur->step_inc();
+ }
+ }
+ }
+ }
+ else {
+ for ($i = 0 ; $i < ($user->stat == 'room' ? BIN5_MAX_PLAYERS : BIN5_PLAYERS_N) ; $i++) {
+ if ($user->stat == 'room') {
+ $user_cur = &$this->user[$i];
+ if ($user_cur->sess == '' || $user_cur->stat != 'room')
+ continue;
+ }
+ else {
+ $user_cur = &$this->user[$table->player[$i]];
+ }
+
+ $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
+ $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s", [%d, "%s"],"%s");',
+ $dt, $user->flags, xcape($user->name), xcape($user_mesg));
+ $user_cur->step_inc();
+ }
+ log_legal($curtime, $user->ip, $user, ($user->stat == 'room' ? 'room' : 'table '.$user->table_orig),$user_mesg);
+ }
+ }
+
+ function table_wakeup($user)
+ {
+ $table = &$this->table[0];
+
+ log_main("BIN5_WAKEUP begin function table stat: ".$user->stat." subst: ".$user->subst);
+
+ $curtime = time();
+
+ log_main("BIN5_WAKEUP from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
+
+ for ($i = 0 ; $i < $table->player_n ; $i++) {
+ $user_cur = &$this->user[$i];
+ log_main("PREIMPOST INLOOP name: ".$user_cur->name);
+
+ if ($user_cur == $user)
+ $user_cur->subst = "shutdowner";
+ else
+ $user_cur->subst = "shutdowned";
+ $user_cur->laccwr = $curtime;
+
+ $ret = "gst.st = ".($user_cur->step+1)."; ";
+ $ret .= 'gst.st_loc++; xstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");|';
+
+ log_wr($user_cur->sess." BIN5_WAKEUP: ".$ret);
+ $user_cur->comm[$user_cur->step % COMM_N] = $ret;
+ $user_cur->step_inc();
+ }
+
+ $this->the_end = TRUE;
+ }
+
+ /*
+ * If all players are freezed the room garbage_manager clean up table and users.
+ */
+ function is_abandoned()
+ {
+ $is_ab = TRUE;
+ $curtime = time();
+
+ $table = &$this->table[0];
+
+ for ($i = 0 ; $i < $table->player_n ; $i++) {
+ $user_cur = &$this->user[$i];
+
+ if ($user_cur->lacc + (EXPIRE_TIME_RD * 2) >= $curtime) {
+ $is_ab = FALSE;
+ break;
+ }
+ }
+
+ return ($is_ab);
+ }
+
+ static function request_mgr(&$s_a_p, $header, &$header_out, &$new_socket, $path, $addr, $get, $post, $cookie)
+ {
+ GLOBAL $G_black_list;
+
+ printf("NEW_SOCKET (root): %d\n", intval($new_socket));
+
+ $enc = get_encoding($header);
+ if (isset($header['User-Agent'])) {
+ if (strstr($header['User-Agent'], "MSIE")) {
+ $transp_type = "htmlfile";
+ }
+ else {
+ $transp_type = "xhr";
+ }
+ }
+ else {
+ $transp_type = "iframe";
+ }
+ force_no_cache($header_out);
+
+ if (($table_idx = gpcs_var('table_idx', $get, $post, $cookie)) === FALSE)
+ unset($table_idx);
+
+ if (($table_token = gpcs_var('table_token', $get, $post, $cookie)) === FALSE)
+ unset($table_token);
+
+
+ switch ($path) {
+ case "":
+ case "index.php":
+ ob_start();
+ bin5_index_main($transp_type, $header_out, $addr, $get, $post, $cookie);
+ $content = ob_get_contents();
+ ob_end_clean();
+
+ $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
+ return TRUE;
+
+ break;
+ case "index_wr.php":
+ // FIXME $content can be unset
+ if (isset($table_idx) && isset($table_token)) {
+ if (($bri = $s_a_p->app->match_get($table_idx, $table_token)) != FALSE) {
+ ob_start();
+ bin5_index_wr_main($bri, $addr, $get, $post, $cookie);
+ $content = ob_get_contents();
+ ob_end_clean();
+ }
+ else {
+ $content = "Bin5 Load data error";
+ }
+ }
+ else {
+ $content = "Bin5 Load data error";
+ }
+ $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
+ return TRUE;
+
+ break;
+ case "index_rd_ifra.php":
+ if (($transp = gpcs_var('transp', $get, $post, $cookie)) === FALSE)
+ $transp = "iframe";
+ if ($transp == 'websocket')
+ $enc = 'plain';
+
+ do {
+ if (!isset($table_idx)
+ || !isset($table_token)
+ || !isset($cookie['sess'])
+ || ($bri = $s_a_p->app->match_get($table_idx, $table_token)) == NULL
+ || (($user = $bri->get_user($cookie['sess'], $idx)) == FALSE)) {
+
+ $content = Bin5_user::stream_fini($transp_type, $s_a_p->rndstr, TRUE);
+ $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
+
+ return TRUE;
+ break;
+ }
+ // close a previous opened index_read_ifra socket, if exists
+ if (($prev = $user->rd_socket_get()) != NULL) {
+ $s_a_p->socks_unset($user->rd_socket_get());
+ fclose($user->rd_socket_get());
+ printf("CLOSE AND OPEN AGAIN ON IFRA2\n");
+ $user->rd_socket_set(NULL);
+ }
+
+ $content = "";
+ $user->stream_init($s_a_p->rndstr, $enc, $header, $header_out, $content, $get, $post, $cookie);
+
+ $response = headers_render($header_out, -1).$user->chunked_content($content);
+ $response_l = mb_strlen($response, "ASCII");
+
+ $wret = @fwrite($new_socket, $response, $response_l);
+ if ($wret < $response_l) {
+ printf("TROUBLES WITH FWRITE: %d\n", $wret);
+ $user->rd_cache_set(mb_substr($content, $wret, $response_l - $wret, "ASCII"));
+ }
+ else {
+ $user->rd_cache_set("");
+ }
+ fflush($new_socket);
+
+
+ $s_a_p->socks_set($new_socket, $user, NULL);
+ $user->rd_socket_set($new_socket);
+ printf(" - qui ci siamo - ");
+ return TRUE;
+ } while (FALSE);
+
+ return FALSE;
+ break;
+
+ default:
+ return FALSE;
+ break;
+ }
+
+ return (FALSE);