/** * Marks the current user as authorized for given channel. * * @param WiseChatChannel $channel * * @return null */ public function markAuthorizedForChannel($channel) { $grants = $this->userSessionDAO->get(self::SESSION_KEY_USER_CHANNEL_AUTHORIZATION); if (!is_array($grants)) { $grants = array(); } $grants[$channel->getId()] = true; $this->userSessionDAO->set(self::SESSION_KEY_USER_CHANNEL_AUTHORIZATION, $grants); }
/** * Creates or updates the channel and returns it. * * @param WiseChatChannel $channel * * @return WiseChatChannel * @throws Exception On validation error */ public function save($channel) { global $wpdb; // low-level validation: if ($channel->getName() === null) { throw new Exception('Name of the channel cannot equal null'); } // prepare channel data: $table = WiseChatInstaller::getChannelsTable(); $columns = array('name' => $channel->getName(), 'password' => $channel->getPassword()); // update or insert: if ($channel->getId() !== null) { $wpdb->update($table, $columns, array('id' => $channel->getId()), '%s', '%d'); } else { $wpdb->insert($table, $columns); $channel->setId($wpdb->insert_id); } return $channel; }
/** * Bans an user by message ID. * * @param integer $messageId * @param WiseChatChannel $channel * @param string $durationString * * @throws Exception If the message or user was not found */ public function banByMessageId($messageId, $channel, $durationString = '1d') { $message = $this->messagesDAO->get($messageId); if ($message === null) { throw new Exception('Message was not found'); } $channelUser = $this->channelUsersDAO->getByUserIdAndChannelId($message->getUserId(), $channel->getId()); if ($channelUser !== null) { $user = $this->usersDAO->get($message->getUserId()); if ($user !== null) { $duration = $this->getDurationFromString($durationString); $this->banIpAddress($user->getIp(), $duration); return; } } throw new Exception('User was not found in this channel'); }
/** * Determines if the number of channels that current user participates has been reached. * * @param WiseChatChannel $channel * * @return boolean */ public function isChatChannelsLimitReached($channel) { $limit = $this->options->getIntegerOption('channels_limit', 0); if ($limit > 0) { $this->userService->refreshChannelUsersData(); $amountOfChannels = $this->channelUsersDAO->getAmountOfActiveBySessionId(session_id()); $user = $this->authentication->getUser(); if ($user === null || $channel === null || $this->channelUsersDAO->getActiveByUserIdAndChannelId($user->getId(), $channel->getId()) === null) { $amountOfChannels++; } if ($amountOfChannels > $limit) { return true; } } return false; }
/** * Marks presence of the current user in the given channel. * * @param WiseChatChannel $channel * * @return null */ private function markPresenceInChannel($channel) { $user = $this->authentication->getUser(); if ($user !== null) { $channelUser = $this->channelUsersDAO->getByUserIdAndChannelId($user->getId(), $channel->getId()); if ($channelUser === null) { $channelUser = new WiseChatChannelUser(); $channelUser->setActive(true); $channelUser->setLastActivityTime(time()); $channelUser->setUserId($user->getId()); $channelUser->setChannelId($channel->getId()); $this->channelUsersDAO->save($channelUser); } else { $channelUser->setActive(true); $channelUser->setLastActivityTime(time()); $this->channelUsersDAO->save($channelUser); } } }
/** * Returns rendered channel statistics. * * @param WiseChatChannel $channel * * @return string HTML source */ public function getRenderedChannelStats($channel) { if ($channel === null) { return 'ERROR: channel does not exist'; } $variables = array('channel' => $channel->getName(), 'messages' => $this->messagesService->getNumberByChannelName($channel->getName()), 'users' => $this->channelUsersDAO->getAmountOfUsersInChannel($channel->getId())); return $this->getTemplatedString($variables, $this->options->getOption('template', 'ERROR: TEMPLATE NOT SPECIFIED')); }