Make a controller for widget gateway

This commit is contained in:
Dmitriy Simushev 2014-05-07 11:03:29 +00:00
parent 7374d5f752
commit 049dff0952
5 changed files with 148 additions and 140 deletions

View File

@ -0,0 +1,138 @@
<?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;
use Mibew\Settings;
use Mibew\Thread;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* Processes widget requests.
*/
class WidgetController extends AbstractController
{
/**
* Provides a gateway for widget requests.
*
* @param Request $request
* @return string Rendered page content
*/
public function indexAction(Request $request)
{
$operator = array();
$response_data = array(
'load' => array(),
'handlers' => array(),
'dependences' => array(),
'data' => array(),
);
if (Settings::get('enabletracking') == '1') {
$entry = $request->query->get('entry', '');
$referer = $request->server->get('HTTP_REFERER', '');
$user_id = $request->query->get('user_id', false);
// Check if session was started
if (isset($_SESSION['visitorid']) && preg_match('/^[0-9]+$/', $_SESSION['visitorid'])) {
// Session was started. Just track the visitor.
$visitor_id = track_visitor($_SESSION['visitorid'], $entry, $referer);
$visitor = track_get_visitor_by_id($visitor_id);
} else {
$visitor = track_get_visitor_by_user_id($user_id);
if ($visitor !== false) {
// Session is not started but the visitor exists in
// database. Probably third-party cookies are disabled by
// the browser. Use tracking by local cookie at target site.
$visitor_id = track_visitor($visitor['visitorid'], $entry, $referer);
} else {
// Start tracking session
$visitor_id = track_visitor_start($entry, $referer);
$visitor = track_get_visitor_by_id($visitor_id);
}
}
if ($visitor_id) {
$_SESSION['visitorid'] = $visitor_id;
}
if ($user_id === false) {
// Update local cookie value at target site
$response_data['handlers'][] = 'updateUserId';
$response_data['dependences']['updateUserId'] = array();
$response_data['data']['user']['id'] = $visitor['userid'];
}
// Get invitation state
$invitation_state = invitation_state($visitor_id);
// Check if invitation is closed
if (!$invitation_state['invited'] && !empty($_SESSION['invitation_threadid'])) {
$response_data['handlers'][] = 'invitationClose';
$response_data['dependences']['invitationClose'] = array();
unset($_SESSION['invitation_threadid']);
}
// Check if the visitor is just invited to chat
$is_invited = $invitation_state['invited']
&& (empty($_SESSION['invitation_threadid'])
? true
: ($_SESSION['invitation_threadid'] != $invitation_state['threadid']));
if ($is_invited) {
// Load invitation thread
$thread = Thread::load($invitation_state['threadid']);
// Get operator info
$operator = operator_by_id($thread->agentId);
$locale = $request->query->get('locale', '');
$operator_name = ($locale == HOME_LOCALE)
? $operator['vclocalename']
: $operator['vccommonname'];
// Show invitation dialog at widget side
$response_data['handlers'][] = 'invitationCreate';
$response_data['dependences']['invitationCreate'] = array();
$response_data['data']['invitation'] = array(
'operatorName' => htmlspecialchars($operator_name),
'avatarUrl' => htmlspecialchars($operator['vcavatar']),
'threadUrl' => ($request->getUriForPath('/client.php')
. '?act=invitation'),
'acceptCaption' => getlocal('invitation.accept.caption'),
);
$_SESSION['invitation_threadid'] = $thread->id;
}
// Check if the visitor rejects invitation
if ($invitation_state['invited'] && $request->query->get('invitation_rejected')) {
invitation_reject($visitor_id);
}
}
// Builds JSONP response
$response = new JsonResponse($response_data);
$response->setCallback("Mibew.Objects.widget.onResponse");
// Add headers to overcome third-party cookies problem.
$response->headers->set('P3P', 'CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');
return $response;
}
}

View File

@ -58,6 +58,11 @@ function start_html_output()
header("Content-type: text/html" . (isset($charset) ? "; charset=" . $charset : ""));
}
/**
* Sends headers that are needed for JS responses.
*
* @deprecated
*/
function start_js_output()
{
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
@ -251,31 +256,3 @@ function add_params($servlet, $params)
return $servlet;
}
/**
* Builds JSONP response to the Mibew widget
*
* @param array $response Response data. It can contain following items:
* - 'load': associative array, specify files which must be loaded. Array keys
* are file aliases and values are URLs. One can use file alias to specify
* dependences (described below).
* - 'handlers': array, handlers which must be called (described below).
* - 'dependences': array, specify dependences between handlers and loaded
* files. Array keys are handlers names and values are arrays of file aliases
* from load item. Handler function will call only after all specified files
* loaded.
* - 'data': associative array, arbitrary structure which will be passed to all
* functions, specified in 'handlers' item.
* @return string JSONP response that ready to send to the widget
*/
function build_widget_response($response)
{
$result = $response + array(
'load' => array(),
'handlers' => array(),
'dependences' => array(),
'data' => array(),
);
return "Mibew.Objects.widget.onResponse(" . json_encode($result) . ");";
}

View File

@ -109,7 +109,7 @@ function generate_button(
$widget_data['requestTimeout'] = Settings::get('updatefrequency_tracking') * 1000;
// URL for requests
$widget_data['requestURL'] = $app_location . '/widget.php';
$widget_data['requestURL'] = $app_location . '/widget';
// Locale for invitation
$widget_data['locale'] = $locale;

View File

@ -1,3 +1,7 @@
license:
path: /license
defaults: { _controller: Mibew\Controller\LicenseController::indexAction }
widget_gateway:
path: /widget
defaults: { _controller: Mibew\Controller\WidgetController::indexAction }

View File

@ -1,111 +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;
// Initialize libraries
require_once(dirname(__FILE__) . '/libs/init.php');
$operator = array();
$response = array();
if (Settings::get('enabletracking') == '1') {
$entry = isset($_GET['entry']) ? $_GET['entry'] : "";
$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : "";
$user_id = isset($_GET['user_id']) ? $_GET['user_id'] : false;
// Check if session start
if (isset($_SESSION['visitorid']) && preg_match('/^[0-9]+$/', $_SESSION['visitorid'])) {
// Session started. Track visitor
$visitor_id = track_visitor($_SESSION['visitorid'], $entry, $referer);
$visitor = track_get_visitor_by_id($visitor_id);
} else {
$visitor = track_get_visitor_by_user_id($user_id);
if ($visitor !== false) {
// Session not started but visitor exist in database.
// Probably third-party cookies disabled by the browser.
// Use tracking by local cookie at target site
$visitor_id = track_visitor($visitor['visitorid'], $entry, $referer);
} else {
// Start tracking session
$visitor_id = track_visitor_start($entry, $referer);
$visitor = track_get_visitor_by_id($visitor_id);
}
}
if ($visitor_id) {
$_SESSION['visitorid'] = $visitor_id;
}
if ($user_id === false) {
// Update local cookie value at target site
$response['handlers'][] = 'updateUserId';
$response['dependences']['updateUserId'] = array();
$response['data']['user']['id'] = $visitor['userid'];
}
// Get invitation state
$invitation_state = invitation_state($visitor_id);
// Check if invitation closed
if (!$invitation_state['invited'] && !empty($_SESSION['invitation_threadid'])) {
$response['handlers'][] = 'invitationClose';
$response['dependences']['invitationClose'] = array();
unset($_SESSION['invitation_threadid']);
}
// Check if visitor just invited to chat
$is_invited = $invitation_state['invited']
&& (empty($_SESSION['invitation_threadid'])
? true
: ($_SESSION['invitation_threadid'] != $invitation_state['threadid']));
if ($is_invited) {
// Load invitation thread
$thread = Thread::load($invitation_state['threadid']);
// Get operator info
$operator = operator_by_id($thread->agentId);
$locale = isset($_GET['locale']) ? $_GET['locale'] : '';
$operator_name = ($locale == HOME_LOCALE)
? $operator['vclocalename']
: $operator['vccommonname'];
// Show invitation dialog at widget side
$response['handlers'][] = 'invitationCreate';
$response['dependences']['invitationCreate'] = array();
$response['data']['invitation'] = array(
'operatorName' => htmlspecialchars($operator_name),
'avatarUrl' => htmlspecialchars($operator['vcavatar']),
'threadUrl' => (get_app_location(true, is_secure_request())
. '/client.php?act=invitation'),
'acceptCaption' => getlocal('invitation.accept.caption'),
);
$_SESSION['invitation_threadid'] = $thread->id;
}
// Check if visitor reject invitation
if ($invitation_state['invited'] && !empty($_GET['invitation_rejected'])) {
invitation_reject($visitor_id);
}
}
start_js_output();
echo build_widget_response($response);