Example #1
0
 /**
  * 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;
 }
 /**
  * Saves attachments in the Media Library and attaches them to the end of the message.
  *
  * @param WiseChatChannel $channel
  * @param array $attachments Array of attachments
  *
  * @return array Array consisting of the two elements: a shortcode representing the attachments and array of IDs of created attachments
  */
 private function saveAttachments($channel, $attachments)
 {
     if (!is_array($attachments) || count($attachments) === 0) {
         return array(null, array());
     }
     WiseChatContainer::load('rendering/filters/WiseChatShortcodeConstructor');
     $firstAttachment = $attachments[0];
     $data = $firstAttachment['data'];
     $data = substr($data, strpos($data, ",") + 1);
     $decodedData = base64_decode($data);
     $attachmentShortcode = null;
     $attachmentIds = array();
     if ($this->options->isOptionEnabled('enable_images_uploader') && $firstAttachment['type'] === 'image') {
         $image = $this->imagesService->saveImage($decodedData);
         if (is_array($image)) {
             $attachmentShortcode = ' ' . WiseChatShortcodeConstructor::getImageShortcode($image['id'], $image['image'], $image['image-th'], '_');
             $attachmentIds = array($image['id']);
         }
     }
     if ($this->options->isOptionEnabled('enable_attachments_uploader') && $firstAttachment['type'] === 'file') {
         $fileName = $firstAttachment['name'];
         $file = $this->attachmentsService->saveAttachment($fileName, $decodedData, $channel->getName());
         if (is_array($file)) {
             $attachmentShortcode = ' ' . WiseChatShortcodeConstructor::getAttachmentShortcode($file['id'], $file['file'], $fileName);
             $attachmentIds = array($file['id']);
         }
     }
     return array($attachmentShortcode, $attachmentIds);
 }
 /**
  * Returns list of allowed attachment formats. 
  *
  * @return array
  */
 public function getAllowedFormats()
 {
     $validFormats = array();
     if ($this->options->isOptionEnabled('enable_attachments_uploader')) {
         $formats = $this->options->getEncodedOption('attachments_file_formats');
         $formatsSplited = preg_split('/,/', $formats);
         if (is_array($formatsSplited)) {
             foreach ($formatsSplited as $format) {
                 $proposedFormat = strtolower(trim($format));
                 if (!in_array($proposedFormat, $this->securityExcludedFormats)) {
                     $validFormats[] = $proposedFormat;
                 }
             }
         }
     }
     return $validFormats;
 }
 /**
  * Sets a new name for current user.
  *
  * @param string $userName A new username to set
  *
  * @return string New username
  * @throws Exception On validation error
  */
 public function changeUserName($userName)
 {
     if (!$this->options->isOptionEnabled('allow_change_user_name') || $this->usersDAO->getCurrentWpUser() !== null || !$this->authentication->isAuthenticated()) {
         throw new Exception('Unsupported operation');
     }
     $userName = $this->authentication->validateUserName($userName);
     $user = $this->authentication->getUser();
     // set new username and refresh it:
     $user->setName($userName);
     $this->usersDAO->save($user);
     $this->refreshNewUserName($user);
     $this->authentication->setOriginalUserName($userName);
     return $userName;
 }
 /**
  * @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;
 }
 /**
  * Callback method for displaying list of checkboxes with a hint.
  *
  * @param array $args Array containing keys: id, name, hint, options
  *
  * @return null
  */
 public function checkboxesCallback($args)
 {
     $id = $args['id'];
     $hint = $args['hint'];
     $options = $args['options'];
     $defaults = $this->getDefaultValues();
     $defaultValue = array_key_exists($id, $defaults) ? $defaults[$id] : '';
     $values = $this->options->getOption($id, $defaultValue);
     $parentId = $this->getFieldParent($id);
     $html = '';
     foreach ($options as $key => $value) {
         $html .= sprintf('<label><input type="checkbox" value="%s" name="%s[%s][]" %s %s data-parent-field="%s" />%s</label>&nbsp;&nbsp; ', $key, WiseChatOptions::OPTIONS_NAME, $id, in_array($value, $values) ? 'checked="1"' : '', $parentId != null && !$this->options->isOptionEnabled($parentId, false) ? 'disabled="1"' : '', $parentId != null ? $parentId : '', $value);
     }
     printf($html);
     if (strlen($hint) > 0) {
         printf('<p class="description">%s</p>', $hint);
     }
 }
Example #7
0
 /**
  * Determines if the current user has to enter his/her name.
  *
  * @return bool
  */
 public function hasUserToBeForcedToEnterName()
 {
     return $this->options->isOptionEnabled('force_user_name_selection') && !$this->authentication->isAuthenticated();
 }
Example #8
0
 /**
  * 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);
 }
Example #9
0
 /**
  * @throws WiseChatUnauthorizedAccessException
  */
 private function checkUserWriteAuthorization()
 {
     if ($this->options->isOptionEnabled('read_only_for_anonymous', false) && !$this->usersDAO->isWpUserLogged()) {
         throw new WiseChatUnauthorizedAccessException('No write permission');
     }
 }