From b2167895e675e3e817a5893a2bec95365c8689e4 Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Sun, 11 Dec 2011 18:53:49 +0000 Subject: [PATCH] Implemented cross-domain invitations --- src/messenger/webim/invite.php | 68 --------------------- src/messenger/webim/js/invite.js | 10 ++-- src/messenger/webim/js/source/invite.js | 80 ++++++------------------- src/messenger/webim/libs/getcode.php | 9 ++- src/messenger/webim/libs/invitation.php | 2 +- src/messenger/webim/request.php | 45 ++++++++++++++ 6 files changed, 74 insertions(+), 140 deletions(-) delete mode 100644 src/messenger/webim/invite.php diff --git a/src/messenger/webim/invite.php b/src/messenger/webim/invite.php deleted file mode 100644 index 3f7161f8..00000000 --- a/src/messenger/webim/invite.php +++ /dev/null @@ -1,68 +0,0 @@ -" . htmlspecialchars($operatorName) . "" . getlocal("invitation.message") . "" . htmlspecialchars($operator['vcavatar']) . ""; -} -else { - echo ""; -} - -exit; -?> \ No newline at end of file diff --git a/src/messenger/webim/js/invite.js b/src/messenger/webim/js/invite.js index 89bf9da6..1593f86b 100644 --- a/src/messenger/webim/js/invite.js +++ b/src/messenger/webim/js/invite.js @@ -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='
';a+='';c&&(a+='

'+c+"

");d&&(a+=''+c+'');a+='

'+b+"

";a+='
';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='
';b&&(a+='

'+b+"

");d&&(a+=''+b+'');a=a+('

'+c+"

")+'
'; 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())}; \ No newline at end of file diff --git a/src/messenger/webim/js/source/invite.js b/src/messenger/webim/js/source/invite.js index 28b399ec..4f36a2ea 100644 --- a/src/messenger/webim/js/source/invite.js +++ b/src/messenger/webim/js/source/invite.js @@ -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 = '
'; + popuptext += ''; + if (operator) { + popuptext += '

' + operator + '

'; + } + if (avatar) { + popuptext += '' + operator + ''; + } + popuptext += '

' + message + '

'; + popuptext += '
'; + 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 = '
'; - popuptext += ''; - if (operator) { - popuptext += '

' + operator + '

'; - } - if (avatar) { - popuptext += '' + operator + ''; - } - popuptext += '

' + message + '

'; - popuptext += '
'; - var invitationdiv = document.getElementById("mibewinvitation"); - if (invitationdiv) { - invitationdiv.innerHTML = popuptext; - } - } - mibewinvitetimer = setTimeout( function(){ mibewInviteMakeRequest(mibewinviteurl, mibewinvitetimeout) }, mibewinvitetimeout); - } } function mibewHideInvitation() { diff --git a/src/messenger/webim/libs/getcode.php b/src/messenger/webim/libs/getcode.php index 475ec3c6..db248ecf 100644 --- a/src/messenger/webim/libs/getcode.php +++ b/src/messenger/webim/libs/getcode.php @@ -38,10 +38,13 @@ function generate_button($title, $locale, $style, $group, $inner, $showhost, $fo $temp = preg_replace('/^('; + $temp .= '/request.php?entry=\' + escape(document.referrer) + \'&lang=ru\''; } return "" . $temp . ""; } diff --git a/src/messenger/webim/libs/invitation.php b/src/messenger/webim/libs/invitation.php index 5a27af92..d5a1fd9e 100644 --- a/src/messenger/webim/libs/invitation.php +++ b/src/messenger/webim/libs/invitation.php @@ -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) . "'"; diff --git a/src/messenger/webim/request.php b/src/messenger/webim/request.php index 6a4eb6ca..cfdd6950 100644 --- a/src/messenger/webim/request.php +++ b/src/messenger/webim/request.php @@ -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);