Replace "client.php" with a controller

This commit is contained in:
Dmitriy Simushev 2014-06-02 11:27:19 +00:00
parent 0cdf90e24a
commit f7dd6a8d72
6 changed files with 242 additions and 208 deletions

View File

@ -1,203 +0,0 @@
<?php
/*
* Copyright 2005-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Import namespaces and classes of the core
use Mibew\Settings;
use Mibew\Thread;
use Mibew\Style\ChatStyle;
// Initialize libraries
require_once(dirname(__FILE__) . '/libs/init.php');
if (Settings::get('enablessl') == "1" && Settings::get('forcessl') == "1") {
if (!is_secure_request()) {
$requested = $_SERVER['PHP_SELF'];
if ($_SERVER['REQUEST_METHOD'] == 'GET' && $_SERVER['QUERY_STRING']) {
header("Location: " . get_app_location(true, true) . "/client.php?" . $_SERVER['QUERY_STRING']);
} else {
die("only https connections are handled");
}
exit;
}
}
// Initialize chat style which is currently used in system
$chat_style = new ChatStyle(ChatStyle::getCurrentStyle());
// Do not support old browsers at all
if (get_remote_level($_SERVER['HTTP_USER_AGENT']) == 'old') {
// Create page array
$page = array_merge_recursive(
setup_logo()
);
$chat_style->render('nochat', $page);
exit;
}
$action = verify_param("act", "/^(invitation|mailthread)$/", "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
$chat_style->render('chat', $page);
exit;
}
}
if (!isset($_GET['token']) || !isset($_GET['thread'])) {
$thread = null;
if (isset($_SESSION['threadid'])) {
$thread = Thread::reopen($_SESSION['threadid']);
}
if (!$thread) {
// Load group info
$group_id = "";
$group_name = "";
$group = null;
if (Settings::get('enablegroups') == '1') {
$group_id = verify_param("group", "/^\d{1,8}$/", "");
if ($group_id) {
$group = group_by_id($group_id);
if (!$group) {
$group_id = "";
} else {
$group_name = get_group_name($group);
}
}
}
// Get operator code
$operator_code = empty($_GET['operator_code']) ? '' : $_GET['operator_code'];
if (!preg_match("/^[A-z0-9_]+$/", $operator_code)) {
$operator_code = false;
}
// Get visitor info
$visitor = visitor_from_request();
$info = get_get_param('info');
$email = get_get_param('email');
// Get referrer
$referrer = isset($_GET['url'])
? $_GET['url']
: (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : "");
if (isset($_GET['referrer']) && $_GET['referrer']) {
$referrer .= "\n" . $_GET['referrer'];
}
// Check if there are online operators
if (!has_online_operators($group_id)) {
// Display leave message page
$page = array_merge_recursive(
setup_logo($group),
setup_leavemessage(
$visitor['name'],
$email,
$group_id,
$info,
$referrer
)
);
$page['leaveMessageOptions'] = json_encode($page['leaveMessage']);
$chat_style->render('chat', $page);
exit;
}
// Get invitation info
if (Settings::get('enabletracking')) {
$invitation_state = invitation_state($_SESSION['visitorid']);
$visitor_is_invited = $invitation_state['invited'];
} else {
$visitor_is_invited = false;
}
// Get operator info
$requested_operator = false;
if ($operator_code) {
$requested_operator = operator_by_code($operator_code);
}
// Check if survey should be displayed
if (Settings::get('enablepresurvey') == '1' && !$visitor_is_invited && !$requested_operator) {
// Display prechat survey
$page = array_merge_recursive(
setup_logo($group),
setup_survey(
$visitor['name'],
$email,
$group_id,
$info,
$referrer
)
);
$page['surveyOptions'] = json_encode($page['survey']);
$chat_style->render('chat', $page);
exit;
}
// Start chat thread
$thread = chat_start_for_user(
$group_id,
$requested_operator,
$visitor['id'],
$visitor['name'],
$referrer,
$info
);
}
$thread_id = $thread->id;
$token = $thread->lastToken;
$chat_style_name = verify_param("style", "/^\w+$/", "");
$redirect_to = MIBEW_WEB_ROOT . "/client.php?thread=" . intval($thread_id)
. "&token=" . urlencode($token)
. ($chat_style_name ? "&style=" . urlencode($chat_style_name) : "");
header("Location: " . $redirect_to);
exit;
}
$token = verify_param("token", "/^\d{1,8}$/");
$thread_id = verify_param("thread", "/^\d{1,8}$/");
$thread = Thread::load($thread_id, $token);
if (!$thread) {
die("wrong thread");
}
$page = setup_chatview_for_user($thread);
if ($action == "mailthread") {
$chat_style->render('mail', $page);
} else {
// Build js application options
$page['chatOptions'] = json_encode($page['chat']);
// Expand page
$chat_style->render('chat', $page);
}

View File

@ -526,14 +526,14 @@ function setup_chatview_for_user(Thread $thread)
$params = "thread=" . $thread->id . "&amp;token=" . $thread->lastToken; $params = "thread=" . $thread->id . "&amp;token=" . $thread->lastToken;
// Set link to send mail page // Set link to send mail page
$data['chat']['links']['mail'] = MIBEW_WEB_ROOT . "/client.php?" $data['chat']['links']['mail'] = MIBEW_WEB_ROOT . "/chat?"
. $params . $params
. "&amp;act=mailthread"; . "&amp;act=mailthread";
// Set SSL link // Set SSL link
if (Settings::get('enablessl') == "1" && !is_secure_request()) { if (Settings::get('enablessl') == "1" && !is_secure_request()) {
$data['chat']['links']['ssl'] = get_app_location(true, true) $data['chat']['links']['ssl'] = get_app_location(true, true)
. "/client.php?" . "/chat?"
. $params; . $params;
} }

View File

@ -0,0 +1,232 @@
<?php
/*
* Copyright 2005-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Mibew\Controller\Chat\User;
use Mibew\Controller\Chat\AbstractController;
use Mibew\Http\Exception\BadRequestException;
use Mibew\Settings;
use Mibew\Thread;
use Symfony\Component\HttpFoundation\Request;
/**
* Contains all actions which are related with user's chat window.
*/
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.
*/
public function indexAction(Request $request)
{
// Check if we should force the user to use SSL
$ssl_redirect = $this->sslRedirect($request);
if ($ssl_redirect !== false) {
return $ssl_redirect;
}
// Do not support old browsers at all
if (get_remote_level($request->headers->get('User-Agent')) == 'old') {
// Create page array
$page = array_merge_recursive(
setup_logo()
);
return $this->render('nochat', $page);
}
$action = $request->query->get('act');
if (!in_array($action, array('invitation', 'mailthread'))) {
$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);
if ($action == 'mailthread') {
return $this->render('mail', $page);
} else {
// Build js application options
$page['chatOptions'] = json_encode($page['chat']);
// Expand page
return $this->render('chat', $page);
}
}
protected function startChat(Request $request)
{
$thread = null;
if (isset($_SESSION['threadid'])) {
$thread = Thread::reopen($_SESSION['threadid']);
}
if (!$thread) {
// Load group info
$group_id = '';
$group_name = '';
$group = null;
if (Settings::get('enablegroups') == '1') {
$group_id = $request->query->get('group');
if (!preg_match("/^\d{1,10}$/", $group_id)) {
$group_id = false;
}
if ($group_id) {
$group = group_by_id($group_id);
if (!$group) {
$group_id = false;
} else {
$group_name = get_group_name($group);
}
}
}
// Get operator code
$operator_code = $request->query->get('operator_code');
if (!preg_match("/^[A-z0-9_]+$/", $operator_code)) {
$operator_code = false;
}
// Get visitor info
$visitor = visitor_from_request();
$info = $request->query->get('info');
$email = $request->query->get('email');
// Get referrer
$referrer = $request->query->get('url', $request->headers->get('referer'));
if ($request->query->get('referrer')) {
$referrer .= "\n" . $request->query->get('referrer');
}
// Check if there are online operators
if (!has_online_operators($group_id)) {
// Display leave message page
$page = array_merge_recursive(
setup_logo($group),
setup_leavemessage(
$visitor['name'],
$email,
$group_id,
$info,
$referrer
)
);
$page['leaveMessageOptions'] = json_encode($page['leaveMessage']);
return $this->render('chat', $page);
}
// Get invitation info
if (Settings::get('enabletracking')) {
$invitation_state = invitation_state($_SESSION['visitorid']);
$visitor_is_invited = $invitation_state['invited'];
} else {
$visitor_is_invited = false;
}
// Get operator info
$requested_operator = false;
if ($operator_code) {
$requested_operator = operator_by_code($operator_code);
}
// Check if survey should be displayed
if (Settings::get('enablepresurvey') == '1' && !$visitor_is_invited && !$requested_operator) {
// Display prechat survey
$page = array_merge_recursive(
setup_logo($group),
setup_survey(
$visitor['name'],
$email,
$group_id,
$info,
$referrer
)
);
$page['surveyOptions'] = json_encode($page['survey']);
return $this->render('chat', $page);
}
// Start chat thread
$thread = chat_start_for_user(
$group_id,
$requested_operator,
$visitor['id'],
$visitor['name'],
$referrer,
$info
);
}
$path_args = array(
'thread' => intval($thread->id),
'token' => urlencode($thread->lastToken),
);
$chat_style_name = $request->query->get('style');
if (preg_match("/^\w+$/", $chat_style_name)) {
$path_args['style'] = $chat_style_name;
}
return $this->redirect($this->generateUrl('chat_user', $path_args));
}
}

View File

@ -118,8 +118,7 @@ class WidgetController extends AbstractController
$response_data['data']['invitation'] = array( $response_data['data']['invitation'] = array(
'operatorName' => htmlspecialchars($operator_name), 'operatorName' => htmlspecialchars($operator_name),
'avatarUrl' => htmlspecialchars($operator['vcavatar']), 'avatarUrl' => htmlspecialchars($operator['vcavatar']),
'threadUrl' => ($request->getUriForPath('/client.php') 'threadUrl' => $this->generateUrl('chat_user', array('act' => 'invitation')),
. '?act=invitation'),
'acceptCaption' => getlocal('invitation.accept.caption'), 'acceptCaption' => getlocal('invitation.accept.caption'),
); );

View File

@ -54,7 +54,7 @@ function generate_button(
$disable_invitation $disable_invitation
) { ) {
$app_location = get_app_location($show_host, $force_secure); $app_location = get_app_location($show_host, $force_secure);
$link = $app_location . "/client.php"; $link = $app_location . "/chat";
if ($locale) { if ($locale) {
$link = append_query($link, "locale=$locale"); $link = append_query($link, "locale=$locale");
} }

View File

@ -13,6 +13,12 @@ chat_operator_redirect:
_controller: Mibew\Controller\Chat\Operator\RedirectController::indexAction _controller: Mibew\Controller\Chat\Operator\RedirectController::indexAction
_access_check: Mibew\AccessControl\Check\LoggedInCheck _access_check: Mibew\AccessControl\Check\LoggedInCheck
## User's chat
chat_user:
path: /chat
defaults:
_controller: Mibew\Controller\Chat\User\ChatController::indexAction
# Pages that are available for all users # Pages that are available for all users
button: button:
path: /b path: /b