Implemented cross-domain invitations

This commit is contained in:
Dmitriy Simushev 2011-12-11 18:53:49 +00:00
parent 04a096fea1
commit b2167895e6
6 changed files with 74 additions and 140 deletions

View File

@ -1,68 +0,0 @@
<?php
/*
* This file is part of Mibew Messenger project.
*
* Copyright (c) 2005-2011 Mibew Messenger Community
* All rights reserved. The contents of this file are subject to the terms of
* the Eclipse Public License v1.0 which accompanies this distribution, and
* is available at http://www.eclipse.org/legal/epl-v10.html
*
* Alternatively, the contents of this file may be used under the terms of
* the GNU General Public License Version 2 or later (the "GPL"), in which case
* the provisions of the GPL are applicable instead of those above. If you wish
* to allow use of your version of this file only under the terms of the GPL, and
* not to allow others to use your version of this file under the terms of the
* EPL, indicate your decision by deleting the provisions above and replace them
* with the notice and other provisions required by the GPL.
*
* Contributors:
* Fedor Fetisov - tracking and inviting implementation
*/
require_once('libs/common.php');
require_once('libs/invitation.php');
require_once('libs/operator.php');
require_once('libs/track.php');
loadsettings();
$invited = FALSE;
$operator = array();
if ($settings['enabletracking'] == '1') {
$entry = isset($_GET['entry']) ? $_GET['entry'] : "";
$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : "";
$link = connect();
if (isset($_SESSION['visitorid']) && preg_match('/^[0-9]+$/', $_SESSION['visitorid'])) {
$invited = invitation_check($_SESSION['visitorid'], $link);
$visitorid = track_visitor($_SESSION['visitorid'], $entry, $referer, $link);
}
else {
$visitorid = track_visitor_start($entry, $referer, $link);
}
if ($visitorid) {
$_SESSION['visitorid'] = $visitorid;
}
if ($invited !== FALSE) {
$operator = operator_by_id_($invited, $link);
}
close_connection($link);
}
start_xml_output();
if ($invited !== FALSE) {
$locale = isset($_GET['lang']) ? $_GET['lang'] : '';
$operatorName = ($locale == $home_locale) ? $operator['vclocalename'] : $operator['vccommonname'];
echo "<invitation><operator>" . htmlspecialchars($operatorName) . "</operator><message>" . getlocal("invitation.message") . "</message><avatar>" . htmlspecialchars($operator['vcavatar']) . "</avatar></invitation>";
}
else {
echo "<empty/>";
}
exit;
?>

View File

@ -1,6 +1,4 @@
var mibewinviterequest,mibewinviteurl,mibewinvitetimeout,mibewinvitetimer,style=document.createElement("style");document.getElementsByTagName("head")[0].appendChild(style);window.createPopup||(style.appendChild(document.createTextNode("")),style.setAttribute("type","text/css"));var sheet=document.styleSheets[document.styleSheets.length-1];if(window.createPopup)sheet.cssText=mibewInviteStyle;else{var node=document.createTextNode(mibewInviteStyle);style.appendChild(node)}
function mibewInviteMakeRequest(a,b){mibewinviteurl=a;mibewinvitetimeout=b;window.XMLHttpRequest?mibewinviterequest=new XMLHttpRequest:window.ActiveXObject&&(mibewinviterequest=new ActiveXObject("MSXML2.XMLHTTP"));if(mibewinviterequest)mibewinviterequest.onreadystatechange=mibewInviteOnResponse;mibewInviteSendRequest(a)}function mibewInviteSendRequest(a){clearTimeout(mibewinvitetimer);mibewinviterequest.open("GET",a+"&rnd="+Math.random(1),!0);mibewinviterequest.send()}
function mibewInviteCheckReadyState(a){if(a.readyState==4&&(a.status==200||a.status==304))return!0}
function mibewInviteOnResponse(){if(mibewInviteCheckReadyState(mibewinviterequest)){var a=mibewinviterequest.responseXML.documentElement,b=a.getElementsByTagName("message");if(b[0]){var b=b[0].firstChild.data,c=a.getElementsByTagName("operator")[0]&&a.getElementsByTagName("operator")[0].firstChild!=null?a.getElementsByTagName("operator")[0].firstChild.data:void 0,d=a.getElementsByTagName("avatar")[0]&&a.getElementsByTagName("avatar")[0].firstChild!=null?a.getElementsByTagName("avatar")[0].firstChild.data:
void 0,a='<div id="mibewinvitationpopup">';a+='<div id="mibewinvitationclose"><a href="javascript:void(0);" onclick="mibewHideInvitation();">&times;</a></div>';c&&(a+='<h1 onclick="mibewOpenAgent();">'+c+"</h1>");d&&(a+='<img id="mibewinvitationavatar" src="'+d+'" title="'+c+'" alt="'+c+'" onclick="mibewOpenAgent();" />');a+='<p onclick="mibewOpenAgent();">'+b+"</p>";a+='<div style="clear: both;"></div>';if(b=document.getElementById("mibewinvitation"))b.innerHTML=a}mibewinvitetimer=setTimeout(function(){mibewInviteMakeRequest(mibewinviteurl,
mibewinvitetimeout)},mibewinvitetimeout)}}function mibewHideInvitation(){if(document.getElementById("mibewinvitationpopup"))document.getElementById("mibewinvitationpopup").style.display="none"}function mibewOpenAgent(){document.getElementById("mibewAgentButton")&&(document.getElementById("mibewAgentButton").onclick(),mibewHideInvitation())};
var style=document.createElement("style");document.getElementsByTagName("head")[0].appendChild(style);window.createPopup||(style.appendChild(document.createTextNode("")),style.setAttribute("type","text/css"));var sheet=document.styleSheets[document.styleSheets.length-1];if(window.createPopup)sheet.cssText=mibewInviteStyle;else{var node=document.createTextNode(mibewInviteStyle);style.appendChild(node)}
function mibewInviteOnResponse(a){var c=a.invitation.message,b=a.invitation.operator,d=a.invitation.avatar,a='<div id="mibewinvitationpopup"><div id="mibewinvitationclose"><a href="javascript:void(0);" onclick="mibewHideInvitation();">&times;</a></div>';b&&(a+='<h1 onclick="mibewOpenAgent();">'+b+"</h1>");d&&(a+='<img id="mibewinvitationavatar" src="'+d+'" title="'+b+'" alt="'+b+'" onclick="mibewOpenAgent();" />');a=a+('<p onclick="mibewOpenAgent();">'+c+"</p>")+'<div style="clear: both;"></div></div>'; if(c=document.getElementById("mibewinvitation"))c.innerHTML=a}
function mibewHideInvitation(){if(document.getElementById("mibewinvitationpopup"))document.getElementById("mibewinvitationpopup").style.display="none"}
function mibewOpenAgent(){document.getElementById("mibewAgentButton")&&(document.getElementById("mibewAgentButton").onclick(),mibewHideInvitation())};

View File

@ -1,8 +1,3 @@
var mibewinviterequest;
var mibewinviteurl;
var mibewinvitetimeout;
var mibewinvitetimer;
var style = document.createElement('style');
document.getElementsByTagName('head')[0].appendChild(style);
@ -19,65 +14,26 @@ if (!window.createPopup) {
sheet.cssText = mibewInviteStyle;
}
function mibewInviteMakeRequest(url, timeout)
function mibewInviteOnResponse(response)
{
mibewinviteurl = url;
mibewinvitetimeout = timeout;
if(window.XMLHttpRequest)
{
mibewinviterequest = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
mibewinviterequest = new ActiveXObject("MSXML2.XMLHTTP");
}
if (mibewinviterequest) {
mibewinviterequest.onreadystatechange = mibewInviteOnResponse;
var message = response.invitation.message;
var operator = response.invitation.operator;
var avatar = response.invitation.avatar;
var popuptext = '<div id="mibewinvitationpopup">';
popuptext += '<div id="mibewinvitationclose"><a href="javascript:void(0);" onclick="mibewHideInvitation();">&times;</a></div>';
if (operator) {
popuptext += '<h1 onclick="mibewOpenAgent();">' + operator + '</h1>';
}
if (avatar) {
popuptext += '<img id="mibewinvitationavatar" src="' + avatar + '" title="' + operator + '" alt="' + operator + '" onclick="mibewOpenAgent();" />';
}
popuptext += '<p onclick="mibewOpenAgent();">' + message + '</p>';
popuptext += '<div style="clear: both;"></div></div>';
var invitationdiv = document.getElementById("mibewinvitation");
if (invitationdiv) {
invitationdiv.innerHTML = popuptext;
}
mibewInviteSendRequest(url);
}
function mibewInviteSendRequest(url)
{
clearTimeout(mibewinvitetimer);
mibewinviterequest.open("GET", url + '&rnd=' + Math.random(1), true);
mibewinviterequest.send();
}
function mibewInviteCheckReadyState(obj)
{
if ((obj.readyState == 4) && ((obj.status == 200) || (obj.status == 304))) {return true;}
}
function mibewInviteOnResponse()
{
if(mibewInviteCheckReadyState(mibewinviterequest))
{
var response = mibewinviterequest.responseXML.documentElement;
var invite = response.getElementsByTagName('message');
if (invite[0]) {
var message = invite[0].firstChild.data;
var operator = response.getElementsByTagName('operator')[0] && response.getElementsByTagName('operator')[0].firstChild != null ? response.getElementsByTagName('operator')[0].firstChild.data : undefined;
var avatar = response.getElementsByTagName('avatar')[0] && response.getElementsByTagName('avatar')[0].firstChild != null ? response.getElementsByTagName('avatar')[0].firstChild.data : undefined;
var popuptext = '<div id="mibewinvitationpopup">';
popuptext += '<div id="mibewinvitationclose"><a href="javascript:void(0);" onclick="mibewHideInvitation();">&times;</a></div>';
if (operator) {
popuptext += '<h1 onclick="mibewOpenAgent();">' + operator + '</h1>';
}
if (avatar) {
popuptext += '<img id="mibewinvitationavatar" src="' + avatar + '" title="' + operator + '" alt="' + operator + '" onclick="mibewOpenAgent();" />';
}
popuptext += '<p onclick="mibewOpenAgent();">' + message + '</p>';
popuptext += '<div style="clear: both;"></div>';
var invitationdiv = document.getElementById("mibewinvitation");
if (invitationdiv) {
invitationdiv.innerHTML = popuptext;
}
}
mibewinvitetimer = setTimeout( function(){ mibewInviteMakeRequest(mibewinviteurl, mibewinvitetimeout) }, mibewinvitetimeout);
}
}
function mibewHideInvitation() {

View File

@ -38,10 +38,13 @@ function generate_button($title, $locale, $style, $group, $inner, $showhost, $fo
$temp = preg_replace('/^(<a )/', '\1id="mibewAgentButton" ', $temp);
$temp .= '<div id="mibewinvitation"></div><script type="text/javascript">var mibewInviteStyle = \'@import url(';
$temp .= get_app_location($showhost, $forcesecure);
$temp .= '/invite.css);\';</script><script type="text/javascript" src="';
$temp .= '/invite.css);\'; var mibewRequestTimeout = ';
$temp .= $settings['updatefrequency_tracking'];
$temp .= '*1000; var mibewRequestUrl = \'';
$temp .= get_app_location($showhost, $forcesecure);
$temp .= '/js/invite.js"></script><script type="text/javascript">mibewInviteMakeRequest(\'';
$temp .= get_app_location($showhost, $forcesecure) . '/invite.php?entry=\' + escape(document.referrer) + \'&lang=ru\', ' . $settings['updatefrequency_tracking'] . '*1000);</script>';
$temp .= '/request.php?entry=\' + escape(document.referrer) + \'&lang=ru\'</script><script type="text/javascript" src="';
$temp .= get_app_location($showhost, $forcesecure);
$temp .= '/js/request.js"></script><script type="text/javascript">mibewMakeRequest();</script>';
}
return "<!-- mibew button -->" . $temp . "<!-- / mibew button -->";
}

View File

@ -60,7 +60,7 @@ function invitation_accept($visitorid, $threadid, $link)
{
global $mysqlprefix;
$query = "update ${mysqlprefix}chatsitevisitor set threadid = " . $threadid . ", chats = chats + 1 where visitorid = '" . db_escape_string($visitorid) . "'";
$query = "update ${mysqlprefix}chatsitevisitor set threadid = " . $threadid . ", chats = chats + 1 where visitorid = " . db_escape_string($visitorid) . "";
perform_query($query, $link);
$query = "select invitedby from ${mysqlprefix}chatsitevisitor where visitorid = '" . db_escape_string($visitorid) . "'";

View File

@ -16,12 +16,57 @@
* with the notice and other provisions required by the GPL.
*
* Contributors:
* Fedor Fetisov - tracking and inviting implementation
* Dmitriy Simushev - Cross-domain javascript requests implementation
*/
require_once('libs/common.php');
require_once('libs/invitation.php');
require_once('libs/operator.php');
require_once('libs/track.php');
require_once('libs/request.php');
loadsettings();
$invited = FALSE;
$operator = array();
if ($settings['enabletracking'] == '1') {
$entry = isset($_GET['entry']) ? $_GET['entry'] : "";
$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : "";
$link = connect();
if (isset($_SESSION['visitorid']) && preg_match('/^[0-9]+$/', $_SESSION['visitorid'])) {
$invited = invitation_check($_SESSION['visitorid'], $link);
$visitorid = track_visitor($_SESSION['visitorid'], $entry, $referer, $link);
}
else {
$visitorid = track_visitor_start($entry, $referer, $link);
}
if ($visitorid) {
$_SESSION['visitorid'] = $visitorid;
}
if ($invited !== FALSE) {
$operator = operator_by_id_($invited, $link);
}
close_connection($link);
}
$response = array();
if ($invited !== FALSE) {
$response['load']['mibewInvitationScript'] = get_app_location(true, is_secure_request()) . '/js/invite.js';
$response['handlers'][] = 'mibewInviteOnResponse';
$response['dependences']['mibewInviteOnResponse'] = array('mibewInvitationScript');
$locale = isset($_GET['lang']) ? $_GET['lang'] : '';
$operatorName = ($locale == $home_locale) ? $operator['vclocalename'] : $operator['vccommonname'];
$response['data']['invitation']['operator'] = htmlspecialchars($operatorName);
$response['data']['invitation']['message'] = getlocal("invitation.message");
$response['data']['invitation']['avatar'] = htmlspecialchars($operator['vcavatar']);
}
start_js_output();
echo build_js_response($response);