/** * Checks whether given message is an admin command and executes it if so. * * @param WiseChatUser $user * @param WiseChatUser $systemUser * @param WiseChatChannel $channel Name of the channel * @param string $message Content of the possible command * * @return boolean True if the message is processed and is not needed to be displayed */ public function resolve($user, $systemUser, $channel, $message) { if ($this->isPotentialCommand($message) && $this->usersDAO->isWpUserAdminLogged()) { // print typed command (visible only for admins): $this->messagesService->addMessage($user, $channel, $message, true); // execute command: $resolver = $this->getCommandResolver($channel, $message); if ($resolver !== null) { $resolver->execute(); } else { $this->messagesService->addMessage($systemUser, $channel, 'Command not found', true); } return true; } return false; }
/** * Returns rendered user name for given message. * * @param WiseChatMessage $message * * @return string HTML source */ public function getRenderedUserName($message) { $formattedUserName = $message->getUserName(); $displayMode = $this->options->getIntegerOption('link_wp_user_name', 0); $styles = ''; if ($displayMode > 0) { $messageUser = $message->getUser(); if ($this->options->isOptionEnabled('allow_change_text_color') && $messageUser !== null && strlen($messageUser->getDataProperty('textColor')) > 0) { $styles = sprintf('style="color: %s"', $messageUser->getDataProperty('textColor')); } } if ($displayMode === 1) { $linkUserNameTemplate = $this->options->getOption('link_user_name_template', null); $wpUser = $message->getWordPressUserId() != null ? $this->usersDAO->getWpUserByID($message->getWordPressUserId()) : null; $userNameLink = null; if ($linkUserNameTemplate != null) { $variables = array('id' => $wpUser !== null ? $wpUser->ID : '', 'username' => $wpUser !== null ? $wpUser->user_login : $message->getUserName(), 'displayname' => $wpUser !== null ? $wpUser->display_name : $message->getUserName()); $userNameLink = $this->getTemplatedString($variables, $linkUserNameTemplate); } else { if ($wpUser !== null) { $userNameLink = get_author_posts_url($wpUser->ID, $wpUser->display_name); } } if ($userNameLink != null) { $formattedUserName = sprintf("<a href='%s' target='_blank' rel='nofollow' %s>%s</a>", $userNameLink, $styles, $formattedUserName); } } else { if ($displayMode === 2) { $replyTag = '@' . $formattedUserName . ':'; $title = htmlspecialchars($this->options->getOption('message_insert_into_message', 'Insert into message') . ': ' . $replyTag, ENT_COMPAT); $formattedUserName = sprintf("<a href='javascript://' class='wcMessageUserReplyTo' %s title='%s'>%s</a>", $styles, $title, $formattedUserName); } } return $formattedUserName; }
/** * 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'); }
/** * Returns all messages from the given channel and (optionally) beginning from the given offset. * Limit, order and admin messages inclusion are taken from the plugin's options. * * @param string $channelName Name of the channel * @param integer $fromId Begin from specific message ID * * @return WiseChatMessage[] */ public function getAllByChannelNameAndOffset($channelName, $fromId = null) { $orderMode = $this->options->getEncodedOption('messages_order', ''); $criteria = new WiseChatMessagesCriteria(); $criteria->setChannelName($channelName); $criteria->setOffsetId($fromId); $criteria->setIncludeAdminMessages($this->usersDAO->isWpUserAdminLogged()); $criteria->setLimit($this->options->getIntegerOption('messages_limit', 100)); $criteria->setOrderMode($orderMode == WiseChatMessagesCriteria::ORDER_DESCENDING ? $orderMode : WiseChatMessagesCriteria::ORDER_ASCENDING); return $this->messagesDAO->getAllByCriteria($criteria); }
/** * @param string $userName * * @return WiseChatUser */ private function createUserAndSave($userName) { WiseChatContainer::load('model/WiseChatUser'); // construct username and user object: $user = new WiseChatUser(); $user->setName($userName); $user->setSessionId($this->userSessionDAO->getSessionId()); $user->setIp($this->getRemoteAddress()); if ($this->options->isOptionEnabled('collect_user_stats', true)) { $this->fillWithGeoDetails($user); } // save user in DB and in the session: $this->usersDAO->save($user); $this->userSessionDAO->set(self::SESSION_KEY_USER_ID, $user->getId()); return $user; }
/** * Sets text color for messages typed by the current user. * * @param string $color * * @throws Exception If an error occurred */ public function setUserTextColor($color) { if (!$this->authentication->isAuthenticated()) { throw new Exception('Unsupported operation'); } if ($color != 'null' && !preg_match("/^#[a-fA-F0-9]{6}\$/", $color)) { throw new Exception('Invalid color signature'); } if ($color == 'null') { $color = ''; } $user = $this->authentication->getUser(); $user->setDataProperty('textColor', $color); $this->usersDAO->save($user); $this->userEvents->resetEventTracker('usersList'); $this->actions->publishAction('setMessagesProperty', array('chatUserId' => $user->getId(), 'propertyName' => 'textColor', 'propertyValue' => $color)); }
/** * Converts an array of stdClass objects into an array of WiseChatMessage objects. * * @param array $messagesRaw * * @return WiseChatMessage[] */ private function populateMultiData($messagesRaw) { if (!is_array($messagesRaw)) { return array(); } $messages = array(); $messagesToComplete = array(); foreach ($messagesRaw as $messageRaw) { $message = $this->populateData($messageRaw); $messagesToComplete[$message->getUserId()][] = $message; $messages[] = $message; } $users = $this->usersDAO->getAll(array_keys($messagesToComplete)); foreach ($users as $user) { if (array_key_exists($user->getId(), $messagesToComplete)) { foreach ($messagesToComplete[$user->getId()] as $message) { $message->setUser($user); } } } return $messages; }
/** * Determines whether the chat is restricted for anonymous users. * * @return boolean */ public function isChatRestrictedForAnonymousUsers() { return $this->options->getOption('access_mode') == 1 && !$this->usersDAO->isWpUserLogged(); }
/** * Returns chat HTML for given channel. * * @param string|null $channelName * * @return string * @throws Exception */ public function getRenderedChat($channelName = null) { $channel = $this->service->createAndGetChannel($this->service->getValidChatChannelName($channelName)); if ($this->service->isChatRestrictedForAnonymousUsers()) { return $this->renderer->getRenderedAccessDenied($this->options->getOption('message_error_4', 'Only logged in users are allowed to enter the chat'), 'wcAccessDenied'); } if (!$this->service->isChatOpen()) { return $this->renderer->getRenderedAccessDenied($this->options->getOption('message_error_5', 'The chat is closed now'), 'wcChatClosed'); } if ($this->service->isChatChannelFull($channel)) { return $this->renderer->getRenderedAccessDenied($this->options->getOption('message_error_6', 'The chat is full now. Try again later.'), 'wcChatFull'); } if ($this->service->isChatChannelsLimitReached($channel)) { return $this->renderer->getRenderedAccessDenied($this->options->getOption('message_error_10', 'You cannot enter the chat due to the limit of channels you can participate simultaneously.'), 'wcChatChannelLimitFull'); } if ($this->service->hasUserToBeForcedToEnterName()) { if ($this->getPostParam('wcUserNameSelection') !== null) { try { $this->authentication->authenticate($this->getPostParam('wcUserName')); } catch (Exception $e) { return $this->renderer->getRenderedUserNameForm($e->getMessage()); } } else { return $this->renderer->getRenderedUserNameForm(); } } if ($this->service->hasUserToBeAuthorizedInChannel($channel)) { if ($this->getPostParam('wcChannelAuthorization') !== null) { if (!$this->service->authorize($channel, $this->getPostParam('wcChannelPassword'))) { return $this->renderer->getRenderedPasswordAuthorization($this->options->getOption('message_error_9', 'Invalid password.')); } } else { return $this->renderer->getRenderedPasswordAuthorization(); } } $chatId = $this->service->getChatID(); $this->userService->startUpMaintenance($channel); $this->bansService->startUpMaintenance(); $this->messagesService->startUpMaintenance($channel); $messages = $this->messagesService->getAllByChannelNameAndOffset($channel->getName()); $renderedMessages = ''; $lastId = 0; foreach ($messages as $message) { // omit non-admin messages: if ($message->isAdmin() && !$this->usersDAO->isWpUserAdminLogged()) { continue; } $renderedMessages .= $this->renderer->getRenderedMessage($message); if ($lastId < $message->getId()) { $lastId = $message->getId(); } } $lastAction = $this->actionsDAO->getLast(); $jsOptions = array('chatId' => $chatId, 'channelId' => $channel->getId(), 'nowTime' => gmdate('c', time()), 'lastId' => $lastId, 'checksum' => $this->getCheckSum(), 'lastActionId' => $lastAction !== null ? $lastAction->getId() : 0, 'baseDir' => $this->options->getBaseDir(), 'emoticonsBaseURL' => $this->options->getEmoticonsBaseURL(), 'apiEndpointBase' => $this->getEndpointBase(), 'messagesRefreshTime' => intval($this->options->getEncodedOption('messages_refresh_time', 3000)), 'messagesOrder' => $this->options->getEncodedOption('messages_order', '') == 'descending' ? 'descending' : 'ascending', 'enableTitleNotifications' => $this->options->isOptionEnabled('enable_title_notifications'), 'soundNotification' => $this->options->getEncodedOption('sound_notification'), 'messagesTimeMode' => $this->options->getEncodedOption('messages_time_mode'), 'channelUsersLimit' => $this->options->getIntegerOption('channel_users_limit', 0), 'messages' => array('message_sending' => $this->options->getEncodedOption('message_sending', 'Sending ...'), 'hint_message' => $this->options->getEncodedOption('hint_message'), 'messageSecAgo' => $this->options->getEncodedOption('message_sec_ago', 'sec. ago'), 'messageMinAgo' => $this->options->getEncodedOption('message_min_ago', 'min. ago'), 'messageYesterday' => $this->options->getEncodedOption('message_yesterday', 'yesterday'), 'messageUnsupportedTypeOfFile' => $this->options->getEncodedOption('message_error_7', 'Unsupported type of file.'), 'messageSizeLimitError' => $this->options->getEncodedOption('message_error_8', 'The size of the file exceeds allowed limit.')), 'userSettings' => $this->userSettingsDAO->getAll(), 'attachmentsValidFileFormats' => $this->attachmentsService->getAllowedFormats(), 'attachmentsSizeLimit' => $this->attachmentsService->getSizeLimit(), 'imagesSizeLimit' => $this->options->getIntegerOption('images_size_limit', 3145728)); $templater = new WiseChatTemplater($this->options->getPluginBaseDir()); $templater->setTemplateFile(WiseChatThemes::getInstance()->getMainTemplate()); $data = array('chatId' => $chatId, 'baseDir' => $this->options->getBaseDir(), 'messages' => $renderedMessages, 'themeStyles' => $this->options->getBaseDir() . WiseChatThemes::getInstance()->getCss(), 'showMessageSubmitButton' => $this->options->isOptionEnabled('show_message_submit_button'), 'showEmoticonInsertButton' => $this->options->isOptionEnabled('show_emoticon_insert_button', true), 'messageSubmitButtonCaption' => $this->options->getEncodedOption('message_submit_button_caption', 'Send'), 'showUsersList' => $this->options->isOptionEnabled('show_users'), 'usersList' => $this->options->isOptionEnabled('show_users') ? $this->renderer->getRenderedUsersList($channel) : '', 'showUsersCounter' => $this->options->isOptionEnabled('show_users_counter'), 'channelUsersLimit' => $this->options->getIntegerOption('channel_users_limit', 0), 'totalUsers' => $this->channelUsersDAO->getAmountOfUsersInChannel($channel->getId()), 'showUserName' => $this->options->isOptionEnabled('show_user_name'), 'currentUserName' => htmlentities($this->authentication->getUserNameOrEmptyString(), ENT_QUOTES, 'UTF-8'), 'isCurrentUserNameNotEmpty' => $this->authentication->isAuthenticated(), 'inputControlsTopLocation' => $this->options->getEncodedOption('input_controls_location') == 'top', 'inputControlsBottomLocation' => $this->options->getEncodedOption('input_controls_location') == '', 'showCustomizationsPanel' => $this->options->isOptionEnabled('allow_change_user_name') && !$this->usersDAO->isWpUserLogged() || $this->options->isOptionEnabled('allow_mute_sound') && strlen($this->options->getEncodedOption('sound_notification')) > 0 || $this->options->isOptionEnabled('allow_change_text_color'), 'allowChangeUserName' => $this->options->isOptionEnabled('allow_change_user_name') && !$this->usersDAO->isWpUserLogged(), 'allowMuteSound' => $this->options->isOptionEnabled('allow_mute_sound') && strlen($this->options->getEncodedOption('sound_notification')) > 0, 'allowChangeTextColor' => $this->options->isOptionEnabled('allow_change_text_color'), 'allowToSendMessages' => !$this->options->isOptionEnabled('read_only_for_anonymous', false) || $this->usersDAO->isWpUserLogged(), 'messageCustomize' => $this->options->getEncodedOption('message_customize', 'Customize'), 'messageName' => $this->options->getEncodedOption('message_name', 'Name'), 'messageSave' => $this->options->getEncodedOption('message_save', 'Save'), 'messageReset' => $this->options->getEncodedOption('message_reset', 'Reset'), 'messageMuteSounds' => $this->options->getEncodedOption('message_mute_sounds', 'Mute sounds'), 'messageTextColor' => $this->options->getEncodedOption('message_text_color', 'Text color'), 'messageTotalUsers' => $this->options->getEncodedOption('message_total_users', 'Total users'), 'messagePictureUploadHint' => $this->options->getEncodedOption('message_picture_upload_hint', 'Upload a picture'), 'messageAttachFileHint' => $this->options->getEncodedOption('message_attach_file_hint', 'Attach a file'), 'messageInsertEmoticon' => $this->options->getEncodedOption('message_insert_emoticon', 'Insert an emoticon'), 'windowTitle' => $this->options->getEncodedOption('window_title', ''), 'enableAttachmentsPanel' => $this->options->isOptionEnabled('enable_images_uploader') || $this->options->isOptionEnabled('enable_attachments_uploader'), 'enableImagesUploader' => $this->options->isOptionEnabled('enable_images_uploader'), 'enableAttachmentsUploader' => $this->options->isOptionEnabled('enable_attachments_uploader'), 'attachmentsExtensionsList' => $this->attachmentsService->getAllowedExtensionsList(), 'multilineSupport' => $this->options->isOptionEnabled('multiline_support'), 'hintMessage' => $this->options->getEncodedOption('hint_message'), 'messageMaxLength' => $this->options->getIntegerOption('message_max_length', 100), 'jsOptions' => json_encode($jsOptions), 'messagesOrder' => $this->options->getEncodedOption('messages_order', '') == 'descending' ? 'descending' : 'ascending', 'cssDefinitions' => $this->cssRenderer->getCssDefinition($chatId), 'customCssDefinitions' => $this->cssRenderer->getCustomCssDefinition()); $data = array_merge($data, $this->userSettingsDAO->getAll()); if ($this->authentication->isAuthenticated()) { $data = array_merge($data, $this->authentication->getUser()->getData()); } return $templater->render($data); }
private function checkUserRight($rightName) { if (!$this->usersDAO->hasCurrentWpUserRight($rightName)) { throw new WiseChatUnauthorizedAccessException('Not enough privileges to execute this request'); } }