diff --git a/src/mibew/libs/chat.php b/src/mibew/libs/chat.php index 7c2af4ce..7ffc4f3c 100644 --- a/src/mibew/libs/chat.php +++ b/src/mibew/libs/chat.php @@ -523,8 +523,6 @@ function setup_chatview_for_user(Thread $thread) 'isAgent' => false, ); - $params = "thread=" . $thread->id . "&token=" . $thread->lastToken; - // Set link to send mail page $data['chat']['links']['mail'] = MIBEW_WEB_ROOT . "/chat" . '/' . $thread->id . '/' . $thread->lastToken . '/mail'; @@ -532,8 +530,7 @@ function setup_chatview_for_user(Thread $thread) // Set SSL link if (Settings::get('enablessl') == "1" && !is_secure_request()) { $data['chat']['links']['ssl'] = get_app_location(true, true) - . "/chat?" - . $params; + . '/chat/' . $thread->id . '/' . $thread->lastToken; } return $data; diff --git a/src/mibew/libs/classes/Mibew/Controller/Chat/User/ChatController.php b/src/mibew/libs/classes/Mibew/Controller/Chat/User/ChatController.php index 03ff928b..a3da0eac 100644 --- a/src/mibew/libs/classes/Mibew/Controller/Chat/User/ChatController.php +++ b/src/mibew/libs/classes/Mibew/Controller/Chat/User/ChatController.php @@ -18,7 +18,7 @@ namespace Mibew\Controller\Chat\User; use Mibew\Controller\Chat\AbstractController; -use Mibew\Http\Exception\BadRequestException; +use Mibew\Http\Exception\NotFoundException; use Mibew\Settings; use Mibew\Thread; use Symfony\Component\HttpFoundation\Request; @@ -32,12 +32,38 @@ class ChatController extends AbstractController * Process chat pages. * * @param Request $request Incoming request. - * @return string|\Symfony\Component\HttpFoundation\RedirectResponse Rendered - * page content or a redirect response. - * @throws BadRequestException If the thread cannot be loaded by some - * reasons. + * @return string Rendered page content or a redirect response. + * @throws NotFoundException If the thread with specified ID and token is + * not found. */ public function indexAction(Request $request) + { + $thread_id = $request->attributes->getInt('thread_id'); + $token = $request->attributes->get('token'); + + $thread = Thread::load($thread_id, $token); + if (!$thread) { + throw new NotFoundException('The thread is not found.'); + } + + $page = setup_chatview_for_user($thread); + + // Build js application options + $page['chatOptions'] = json_encode($page['chat']); + + // Expand page + return $this->render('chat', $page); + } + + /** + * Starts the chat. + * + * @param Request $request Incoming request. + * @return string|\Symfony\Component\HttpFoundation\RedirectResponse + * Rendered page content or a redirect response. + * @todo Split the action into pieces. + */ + public function startAction(Request $request) { // Check if we should force the user to use SSL $ssl_redirect = $this->sslRedirect($request); @@ -55,66 +81,13 @@ class ChatController extends AbstractController return $this->render('nochat', $page); } - $action = $request->query->get('act'); - if ($action != 'invitation') { - $action = 'default'; - } - - if ($action == 'invitation' && Settings::get('enabletracking')) { - // Check if user invited to chat - $invitation_state = invitation_state($_SESSION['visitorid']); - - if ($invitation_state['invited'] && $invitation_state['threadid']) { - $thread = Thread::load($invitation_state['threadid']); - - // Prepare page - $page = setup_invitation_view($thread); - - // Build js application options - $page['invitationOptions'] = json_encode($page['invitation']); - - // Expand page - return $this->render('chat', $page); - } - } - - if (!$request->query->has('token') || !$request->query->has('thread')) { - return $this->startChat($request); - } - - // Get and validate thread id - $thread_id = $request->query->get('thread'); - if (!preg_match("/^\d{1,10}$/", $thread_id)) { - throw new BadRequestException('Wrong value of "thread" argument.'); - } - - // Get token and verify it - $token = $request->query->get('token'); - if (!preg_match("/^\d{1,10}$/", $token)) { - throw new BadRequestException('Wrong value of "token" argument.'); - } - - $thread = Thread::load($thread_id, $token); - if (!$thread) { - throw new BadRequestException('Wrong thread.'); - } - - $page = setup_chatview_for_user($thread); - - // Build js application options - $page['chatOptions'] = json_encode($page['chat']); - - // Expand page - return $this->render('chat', $page); - } - - protected function startChat(Request $request) - { $thread = null; + // Try to get thread from the session if (isset($_SESSION['threadid'])) { $thread = Thread::reopen($_SESSION['threadid']); } + // Create new thread if (!$thread) { // Load group info $group_id = ''; @@ -214,7 +187,7 @@ class ChatController extends AbstractController ); } $path_args = array( - 'thread' => intval($thread->id), + 'thread_id' => intval($thread->id), 'token' => urlencode($thread->lastToken), ); @@ -225,4 +198,44 @@ class ChatController extends AbstractController return $this->redirect($this->generateUrl('chat_user', $path_args)); } + + /** + * Process chat in an invitation block. + * + * @param Request $request Incoming request. + * @return string|\Symfony\Component\HttpFoundation\RedirectResponse Rendered + * page content or a redirect response. + */ + public function invitationAction(Request $request) + { + // Check if an user tries to use invitation functionality when it's + // disabled. + if (!Settings::get('enabletracking')) { + return $this->redirect($this->generateUrl('chat_user_start')); + } + + // Check if we should force the user to use SSL. + $ssl_redirect = $this->sslRedirect($request); + if ($ssl_redirect !== false) { + return $ssl_redirect; + } + + // Check if user invited to chat. + $invitation_state = invitation_state($_SESSION['visitorid']); + + if (!$invitation_state['invited'] || !$invitation_state['threadid']) { + return $this->redirect($this->generateUrl('chat_user_start')); + } + + $thread = Thread::load($invitation_state['threadid']); + + // Prepare page + $page = setup_invitation_view($thread); + + // Build js application options + $page['invitationOptions'] = json_encode($page['invitation']); + + // Expand page + return $this->render('chat', $page); + } } diff --git a/src/mibew/libs/classes/Mibew/Controller/WidgetController.php b/src/mibew/libs/classes/Mibew/Controller/WidgetController.php index ddfd1968..1f2d8185 100644 --- a/src/mibew/libs/classes/Mibew/Controller/WidgetController.php +++ b/src/mibew/libs/classes/Mibew/Controller/WidgetController.php @@ -118,7 +118,7 @@ class WidgetController extends AbstractController $response_data['data']['invitation'] = array( 'operatorName' => htmlspecialchars($operator_name), 'avatarUrl' => htmlspecialchars($operator['vcavatar']), - 'threadUrl' => $this->generateUrl('chat_user', array('act' => 'invitation')), + 'threadUrl' => $this->generateUrl('chat_user_invitation'), 'acceptCaption' => getlocal('invitation.accept.caption'), ); diff --git a/src/mibew/libs/routing.yml b/src/mibew/libs/routing.yml index 5a90923b..9b6aab14 100644 --- a/src/mibew/libs/routing.yml +++ b/src/mibew/libs/routing.yml @@ -38,9 +38,17 @@ chat_operator_redirection_links: ## User's chat chat_user: - path: /chat + path: /chat/{thread_id}/{token} defaults: _controller: Mibew\Controller\Chat\User\ChatController::indexAction + requirements: + thread_id: \d{1,10} + token: \d{1,10} + +chat_user_invitation: + path: /chat/invitation + defaults: + _controller: Mibew\Controller\Chat\User\ChatController::invitationAction chat_user_mail: path: /chat/{thread_id}/{token}/mail @@ -60,6 +68,11 @@ chat_user_mail_send: token: \d{1,10} methods: [POST] +chat_user_start: + path: /chat + defaults: + _controller: Mibew\Controller\Chat\User\ChatController::startAction + # Pages that are available for all users button: path: /b