/** * Converts stdClass object into WiseChatMessage object. * * @param stdClass $messageRawData * * @return WiseChatMessage */ private function populateData($messageRawData) { $message = new WiseChatMessage(); if (strlen($messageRawData->id) > 0) { $message->setId(intval($messageRawData->id)); } $message->setAdmin($messageRawData->admin == '1'); $message->setUserName($messageRawData->user); $message->setChannelName($messageRawData->channel); if (strlen($messageRawData->chat_user_id) > 0) { $message->setUserId(intval($messageRawData->chat_user_id)); } $message->setText($messageRawData->text); $message->setIp($messageRawData->ip); if (strlen($messageRawData->time) > 0) { $message->setTime(intval($messageRawData->time)); } if (strlen($messageRawData->user_id) > 0) { $message->setWordPressUserId(intval($messageRawData->user_id)); } return $message; }
/** * Returns rendered message content. * * @param WiseChatMessage $message * * @return string HTML source */ private function getRenderedMessageContent($message) { $formattedMessage = htmlspecialchars($message->getText(), ENT_QUOTES, 'UTF-8'); /** @var WiseChatLinksPostFilter $linksFilter */ $linksFilter = WiseChatContainer::get('rendering/filters/post/WiseChatLinksPostFilter'); $formattedMessage = $linksFilter->filter($formattedMessage, $this->options->isOptionEnabled('allow_post_links')); /** @var WiseChatAttachmentsPostFilter $attachmentsFilter */ $attachmentsFilter = WiseChatContainer::get('rendering/filters/post/WiseChatAttachmentsPostFilter'); $formattedMessage = $attachmentsFilter->filter($formattedMessage, $this->options->isOptionEnabled('enable_attachments_uploader'), $this->options->isOptionEnabled('allow_post_links')); /** @var WiseChatImagesPostFilter $imagesFilter */ $imagesFilter = WiseChatContainer::get('rendering/filters/post/WiseChatImagesPostFilter'); $formattedMessage = $imagesFilter->filter($formattedMessage, $this->options->isOptionEnabled('allow_post_images'), $this->options->isOptionEnabled('allow_post_links')); /** @var WiseChatYouTubePostFilter $youTubeFilter */ $youTubeFilter = WiseChatContainer::get('rendering/filters/post/WiseChatYouTubePostFilter'); $formattedMessage = $youTubeFilter->filter($formattedMessage, $this->options->isOptionEnabled('enable_youtube'), $this->options->isOptionEnabled('allow_post_links'), $this->options->getIntegerOption('youtube_width', 186), $this->options->getIntegerOption('youtube_height', 105)); if ($this->options->isOptionEnabled('enable_twitter_hashtags')) { /** @var WiseChatHashtagsPostFilter $hashTagsFilter */ $hashTagsFilter = WiseChatContainer::get('rendering/filters/post/WiseChatHashtagsPostFilter'); $formattedMessage = $hashTagsFilter->filter($formattedMessage); } if ($this->options->isOptionEnabled('emoticons_enabled', true)) { /** @var WiseChatEmoticonsFilter $emoticonsFilter */ $emoticonsFilter = WiseChatContainer::get('rendering/filters/post/WiseChatEmoticonsFilter'); $formattedMessage = $emoticonsFilter->filter($formattedMessage); } if ($this->options->isOptionEnabled('multiline_support')) { $formattedMessage = str_replace("\n", '<br />', $formattedMessage); } return $formattedMessage; }
/** * Publishes a message in the given channel of the chat and returns it. * * @param WiseChatUser $user Author of the message * @param WiseChatChannel $channel A channel to publish in * @param string $text Content of the message * @param boolean $isAdmin Indicates whether to mark the message as admin-owned * * @return WiseChatMessage|null * @throws Exception On validation error */ public function addMessage($user, $channel, $text, $isAdmin = false) { $text = trim($text); $filteredMessage = $text; // basic validation: if ($user === null) { throw new Exception('User cannot be null'); } if ($channel === null) { throw new Exception('Channel cannot be null'); } if ($this->bansService->isIpAddressBanned($user->getIp())) { throw new Exception($this->options->getOption('message_error_3', 'You were banned from posting messages')); } // use bad words filtering: if ($this->options->isOptionEnabled('filter_bad_words')) { WiseChatContainer::load('rendering/filters/pre/WiseChatFilter'); $badWordsFilterReplacement = $this->options->getOption('bad_words_replacement_text'); $filteredMessage = WiseChatFilter::filter($filteredMessage, strlen($badWordsFilterReplacement) > 0 ? $badWordsFilterReplacement : null); } // auto-ban feature: if ($this->options->isOptionEnabled('enable_autoban') && $filteredMessage != $text) { $counter = $this->abuses->incrementAndGetAbusesCounter(); $threshold = $this->options->getIntegerOption('autoban_threshold', 3); if ($counter >= $threshold && $threshold > 0) { $duration = $this->options->getIntegerOption('autoban_duration', 1440); $this->bansService->banIpAddress($user->getIp(), $this->bansService->getDurationFromString($duration . 'm')); $this->abuses->clearAbusesCounter(); } } // flood prevention feature: if ($this->options->isOptionEnabled('enable_flood_control')) { $floodControlThreshold = $this->options->getIntegerOption('flood_control_threshold', 200); $floodControlTimeFrame = $this->options->getIntegerOption('flood_control_time_frame', 1); if ($floodControlThreshold > 0 && $floodControlTimeFrame > 0) { $messagesAmount = $this->messagesDAO->getNumberByCriteria(WiseChatMessagesCriteria::build()->setIp($user->getIp())->setMinimumTime(time() - $floodControlTimeFrame * 60)); if ($messagesAmount > $floodControlThreshold) { $duration = $this->options->getIntegerOption('flood_control_ban_duration', 1440); $this->bansService->banIpAddress($user->getIp(), $this->bansService->getDurationFromString($duration . 'm')); } } } // go through the custom filters: $filterChain = WiseChatContainer::get('services/WiseChatFilterChain'); $filteredMessage = $filterChain->filter($filteredMessage); // cut the message: $messageMaxLength = $this->options->getIntegerOption('message_max_length', 100); if ($messageMaxLength > 0) { $filteredMessage = substr($filteredMessage, 0, $messageMaxLength); } // convert images and links into proper shortcodes and download images (if enabled): /** @var WiseChatLinksPreFilter $linksPreFilter */ $linksPreFilter = WiseChatContainer::get('rendering/filters/pre/WiseChatLinksPreFilter'); $filteredMessage = $linksPreFilter->filter($filteredMessage, $this->options->isOptionEnabled('allow_post_images'), $this->options->isOptionEnabled('enable_youtube')); $message = new WiseChatMessage(); $message->setTime(time()); $message->setAdmin($isAdmin); $message->setUserName($user->getName()); $message->setUserId($user->getId()); $message->setText($filteredMessage); $message->setChannelName($channel->getName()); $message->setIp($user->getIp()); if ($user->getWordPressId() !== null) { $message->setWordPressUserId($user->getWordPressId()); } $message = $this->messagesDAO->save($message); // mark attachments created by the links pre-filter: $createdAttachments = $linksPreFilter->getCreatedAttachments(); if (count($createdAttachments) > 0) { $this->attachmentsService->markAttachmentsWithDetails($createdAttachments, $channel->getName(), $message->getId()); } return $message; }