/**
  * 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;
 }
Beispiel #2
0
 /**
  * Creates or updates the user and returns it.
  *
  * @param WiseChatUser $user
  *
  * @return WiseChatUser
  * @throws Exception On validation error
  */
 public function save($user)
 {
     global $wpdb;
     // low-level validation:
     if ($user->getName() === null) {
         throw new Exception('Name of the user cannot equal null');
     }
     if ($user->getSessionId() === null) {
         throw new Exception('Session ID of the user cannot equal null');
     }
     // prepare user data:
     $table = WiseChatInstaller::getUsersTable();
     $columns = array('name' => $user->getName(), 'session_id' => $user->getSessionId(), 'data' => json_encode($user->getData()), 'ip' => $user->getIp());
     // update or insert:
     if ($user->getId() !== null) {
         $columns['wp_id'] = $user->getWordPressId();
         $wpdb->update($table, $columns, array('id' => $user->getId()), '%s', '%d');
     } else {
         if ($user->getWordPressId() > 0) {
             $columns['wp_id'] = $user->getWordPressId();
         }
         $columns['created'] = time();
         $wpdb->insert($table, $columns);
         $user->setId($wpdb->insert_id);
     }
     return $user;
 }