Overcome third-party cookies limitation in safari 5

This commit is contained in:
Dmitriy Simushev 2013-02-11 15:57:41 +00:00
parent 7d0a71da64
commit d065656ff5
6 changed files with 148 additions and 27 deletions

View File

@ -1,8 +1,13 @@
var mibewRequestedScripts=[],mibewHandlers=[],mibewHandlersDependences=[]; /*
function mibewMakeRequest(){mibewDoLoadScript(mibewRequestUrl+"&rnd="+Math.random(),"responseScript")} This file is part of Mibew Messenger project.
function mibewOnResponse(a){var b=a.load,c=a.handlers,e=a.data,a=a.dependences;for(id in b)if(!(b[id]in mibewRequestedScripts))mibewRequestedScripts[id]=[],mibewRequestedScripts[id].url=b[id],mibewRequestedScripts[id].status="loading",mibewLoadScript(id);for(handler in a)handler in mibewHandlersDependences||(mibewHandlersDependences[handler]=a[handler]);for(b=0;b<c.length;b++){var d=c[b];if(mibewCanRunHandler(c[b]))window[d](e);else c[b]in mibewHandlers||(mibewHandlers[d]=function(){window[d](e)})}mibewCleanUpAfterRequest(); window.setTimeout(mibewMakeRequest,mibewRequestTimeout)} http://mibew.org
function mibewCleanUpAfterRequest(){document.getElementsByTagName("head")[0].removeChild(document.getElementById("responseScript"))}
function mibewDoLoadScript(a,b){var c=document.createElement("script");c.setAttribute("type","text/javascript");c.setAttribute("src",a);c.setAttribute("id",b);document.getElementsByTagName("head")[0].appendChild(c);return c} Copyright (c) 2005-2011 Mibew Messenger Community
function mibewLoadScript(a){var b=mibewDoLoadScript(mibewRequestedScripts[a].url,a);b.onload=function(){mibewScriptReady(a)};b.onreadystatechange=function(){("complete"==this.readyState||"loaded"==this.readyState)&&mibewScriptReady(a)}} License: http://mibew.org/license.php
function mibewScriptReady(a){mibewRequestedScripts[a].status="ready";for(handlerName in mibewHandlers)mibewCanRunHandler(handlerName)&&(mibewHandlers[handlerName](),delete mibewHandlers[handlerName])} */
function mibewCanRunHandler(a){for(var a=mibewHandlersDependences[a],b=0;b<a.length;b++)if("ready"!=mibewRequestedScripts[a[b]].status)return!1;return!0}; var mibewRequestedScripts=[],mibewHandlers=[],mibewHandlersDependences=[];function mibewMakeRequest(){var a=mibewReadCookie(mibewVisitorCookieName);mibewDoLoadScript(mibewRequestUrl+"&rnd="+Math.random()+(!1!==a?"&user_id="+a:""),"responseScript")}
function mibewOnResponse(a){var b=a.load,c=a.handlers,d=a.data;a=a.dependences;for(id in b)b[id]in mibewRequestedScripts||(mibewRequestedScripts[id]=[],mibewRequestedScripts[id].url=b[id],mibewRequestedScripts[id].status="loading",mibewLoadScript(id));for(handler in a)handler in mibewHandlersDependences||(mibewHandlersDependences[handler]=a[handler]);for(b=0;b<c.length;b++){var e=c[b];if(mibewCanRunHandler(c[b]))window[e](d);else c[b]in mibewHandlers||(mibewHandlers[e]=function(){window[e](d)})}mibewCleanUpAfterRequest();
window.setTimeout(mibewMakeRequest,mibewRequestTimeout)}function mibewCleanUpAfterRequest(){document.getElementsByTagName("head")[0].removeChild(document.getElementById("responseScript"))}function mibewDoLoadScript(a,b){var c=document.createElement("script");c.setAttribute("type","text/javascript");c.setAttribute("src",a);c.setAttribute("id",b);document.getElementsByTagName("head")[0].appendChild(c);return c}
function mibewLoadScript(a){var b=mibewDoLoadScript(mibewRequestedScripts[a].url,a);b.onload=function(){mibewScriptReady(a)};b.onreadystatechange=function(){("complete"==this.readyState||"loaded"==this.readyState)&&mibewScriptReady(a)}}function mibewScriptReady(a){mibewRequestedScripts[a].status="ready";for(handlerName in mibewHandlers)mibewCanRunHandler(handlerName)&&(mibewHandlers[handlerName](),delete mibewHandlers[handlerName])}
function mibewCanRunHandler(a){a=mibewHandlersDependences[a];for(var b=0;b<a.length;b++)if("ready"!=mibewRequestedScripts[a[b]].status)return!1;return!0}function mibewCreateCookie(a,b){var c=/([^\.]+\.[^\.]+)$/.exec(document.location.hostname)[1];document.cookie=""+a+"="+b+"; path=/; "+(c?"domain="+c+";":"")}function mibewReadCookie(a){var b=document.cookie.split("; ");a+="=";for(var c=!1,d=0;d<b.length;d++)if(-1!=b[d].indexOf(a)){c=b[d].substr(a.length);break}return c}
function mibewUpdateUserId(a){mibewCreateCookie(mibewVisitorCookieName,a.user.id)};

View File

@ -1,10 +1,25 @@
/**
* @preserve This file is part of Mibew Messenger project.
* http://mibew.org
*
* Copyright (c) 2005-2011 Mibew Messenger Community
* License: http://mibew.org/license.php
*/
var mibewRequestedScripts = new Array(); var mibewRequestedScripts = new Array();
var mibewHandlers = new Array(); var mibewHandlers = new Array();
var mibewHandlersDependences = new Array(); var mibewHandlersDependences = new Array();
function mibewMakeRequest() function mibewMakeRequest()
{ {
mibewDoLoadScript(mibewRequestUrl + '&rnd=' + Math.random(), 'responseScript'); // Try to get user id from local cookie
var userId = mibewReadCookie(mibewVisitorCookieName);
mibewDoLoadScript(
mibewRequestUrl + '&rnd=' + Math.random()
+ ((userId !== false) ? '&user_id=' + userId : ''),
'responseScript'
);
} }
function mibewOnResponse(response) function mibewOnResponse(response)
@ -96,3 +111,45 @@ function mibewCanRunHandler(handlerName)
} }
return true; return true;
} }
/**
* Create session cookie for top level domain with path equals to '/'.
*
* @param {String} name Cookie name
* @param {String} value Cookie value
*/
function mibewCreateCookie(name, value) {
var domainParts = /([^\.]+\.[^\.]+)$/.exec(document.location.hostname);
var domain = domainParts[1];
document.cookie = "" + name + "=" + value + "; "
+ "path=/; "
+ (domain ? ("domain=" + domain + ";") : '');
}
/**
* Try to read cookie.
*
* @param {String} name Cookie name
* @returns {String|Boolean} Cookie value or boolean false if cookie with
* specified name does not exist
*/
function mibewReadCookie(name) {
var cookies = document.cookie.split('; ');
var nameForSearch = name + '=';
var value = false;
for (var i = 0; i < cookies.length; i++) {
if (cookies[i].indexOf(nameForSearch) != -1) {
value = cookies[i].substr(nameForSearch.length);
break;
}
}
return value;
}
/**
* Update user id. API function
* @param {Object} response Data object from server
*/
function mibewUpdateUserId(response) {
mibewCreateCookie(mibewVisitorCookieName, response.user.id);
}

View File

@ -36,4 +36,9 @@ $featuresversion = '1.6.4';
*/ */
$session_prefix = md5($mysqlhost.'##'.$mysqldb.'##'.$mysqlprefix) . '_'; $session_prefix = md5($mysqlhost.'##'.$mysqldb.'##'.$mysqlprefix) . '_';
/**
* Name for cookie to track visitor
*/
$visitorcookie = "WEBIM_VisitorID";
?> ?>

View File

@ -17,6 +17,7 @@
function generate_button($title, $locale, $style, $invitationstyle, $group, $inner, $showhost, $forcesecure, $modsecurity) function generate_button($title, $locale, $style, $invitationstyle, $group, $inner, $showhost, $forcesecure, $modsecurity)
{ {
global $visitorcookie;
$link = get_app_location($showhost, $forcesecure) . "/client.php"; $link = get_app_location($showhost, $forcesecure) . "/client.php";
if ($locale) if ($locale)
$link = append_query($link, "locale=$locale"); $link = append_query($link, "locale=$locale");
@ -39,7 +40,9 @@ function generate_button($title, $locale, $style, $invitationstyle, $group, $inn
$temp .= Settings::get('updatefrequency_tracking'); $temp .= Settings::get('updatefrequency_tracking');
$temp .= '*1000; var mibewRequestUrl = \''; $temp .= '*1000; var mibewRequestUrl = \'';
$temp .= get_app_location($showhost, $forcesecure); $temp .= get_app_location($showhost, $forcesecure);
$temp .= '/request.php?entry=\' + escape(document.referrer) + \'&lang=ru\'</script><script type="text/javascript" src="'; $temp .= '/request.php?entry=\' + escape(document.referrer) + \'&lang=ru\'; ';
$temp .= ' var mibewVisitorCookieName = \''.$visitorcookie.'\'';
$temp .= '</script><script type="text/javascript" src="';
$temp .= get_app_location($showhost, $forcesecure); $temp .= get_app_location($showhost, $forcesecure);
$temp .= '/js/compiled/request.js"></script><script type="text/javascript">mibewMakeRequest();</script>'; $temp .= '/js/compiled/request.js"></script><script type="text/javascript">mibewMakeRequest();</script>';
} }

View File

@ -85,6 +85,21 @@ function track_get_visitor_by_threadid($threadid)
); );
} }
/**
* Load visitor info by user id.
*
* @param string $user_id User id
* @return boolean|array Visitor array or boolean false if visitor not exists
*/
function track_get_visitor_by_user_id($user_id) {
$db = Database::getInstance();
return $db->query(
"select * from {chatsitevisitor} where userid = ?",
array($user_id),
array('return_rows' => Database::RETURN_ONE_ROW)
);
}
function track_visit_page($visitorid, $page) function track_visit_page($visitorid, $page)
{ {
$db = Database::getInstance(); $db = Database::getInstance();
@ -196,5 +211,19 @@ function track_remove_old_tracks() {
); );
} }
/**
* Return user id by visitor id.
*
* @param int $visitorid Id of the visitor
* @return string|boolean user id or boolean false if there is no visitor with
* specified visitor id
*/
function track_get_user_id($visitorid) {
$visitor = track_get_visitor_by_id($visitorid);
if (! $visitor) {
return false;
}
return $visitor['userid'];
}
?> ?>

View File

@ -23,17 +23,33 @@ require_once('libs/request.php');
$invited = FALSE; $invited = FALSE;
$operator = array(); $operator = array();
$response = array();
if (Settings::get('enabletracking') == '1') { if (Settings::get('enabletracking') == '1') {
$entry = isset($_GET['entry']) ? $_GET['entry'] : ""; $entry = isset($_GET['entry']) ? $_GET['entry'] : "";
$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ""; $referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : "";
$user_id = isset($_GET['user_id']) ? $_GET['user_id'] : false;
if (isset($_SESSION['visitorid']) && preg_match('/^[0-9]+$/', $_SESSION['visitorid'])) { // Check if session start
if (isset($_SESSION['visitorid'])
&& preg_match('/^[0-9]+$/', $_SESSION['visitorid'])) {
// Session started. Track visitor
$invited = invitation_check($_SESSION['visitorid']); $invited = invitation_check($_SESSION['visitorid']);
$visitorid = track_visitor($_SESSION['visitorid'], $entry, $referer); $visitorid = track_visitor($_SESSION['visitorid'], $entry, $referer);
} } else {
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
$invited = invitation_check($visitor['visitorid']);
$visitorid = track_visitor($visitor['visitorid'], $entry, $referer);
} else {
// Start tracking session
$visitorid = track_visitor_start($entry, $referer); $visitorid = track_visitor_start($entry, $referer);
$visitor = track_get_visitor_by_id($visitorid);
$user_id = $visitor['userid'];
}
} }
if ($visitorid) { if ($visitorid) {
@ -44,9 +60,15 @@ if (Settings::get('enabletracking') == '1') {
$operator = operator_by_id($invited); $operator = operator_by_id($invited);
} }
if ($user_id !== false) {
// Update local cookie value at target site
$response['handlers'][] = 'mibewUpdateUserId';
$response['dependences']['mibewUpdateUserId'] = array();
$response['data']['user']['id'] = $user_id;
}
} }
$response = array();
if ($invited !== FALSE) { if ($invited !== FALSE) {
$response['load']['mibewInvitationScript'] = get_app_location(true, is_secure_request()) . '/js/compiled/invite.js'; $response['load']['mibewInvitationScript'] = get_app_location(true, is_secure_request()) . '/js/compiled/invite.js';
$response['handlers'][] = 'mibewInviteOnResponse'; $response['handlers'][] = 'mibewInviteOnResponse';