diff --git a/src/messenger/webim/js/compiled/request.js b/src/messenger/webim/js/compiled/request.js index badd050f..1b687178 100644 --- a/src/messenger/webim/js/compiled/request.js +++ b/src/messenger/webim/js/compiled/request.js @@ -1,8 +1,13 @@ -var mibewRequestedScripts=[],mibewHandlers=[],mibewHandlersDependences=[]; -function mibewMakeRequest(){mibewDoLoadScript(mibewRequestUrl+"&rnd="+Math.random(),"responseScript")} -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 \ No newline at end of file diff --git a/src/messenger/webim/libs/getcode.php b/src/messenger/webim/libs/getcode.php index a58e67c1..74f96156 100644 --- a/src/messenger/webim/libs/getcode.php +++ b/src/messenger/webim/libs/getcode.php @@ -17,6 +17,7 @@ function generate_button($title, $locale, $style, $invitationstyle, $group, $inner, $showhost, $forcesecure, $modsecurity) { + global $visitorcookie; $link = get_app_location($showhost, $forcesecure) . "/client.php"; if ($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 .= '*1000; var mibewRequestUrl = \''; $temp .= get_app_location($showhost, $forcesecure); - $temp .= '/request.php?entry=\' + escape(document.referrer) + \'&lang=ru\''; } diff --git a/src/messenger/webim/libs/track.php b/src/messenger/webim/libs/track.php index 678ecdad..d3405f38 100644 --- a/src/messenger/webim/libs/track.php +++ b/src/messenger/webim/libs/track.php @@ -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) { $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']; +} ?> \ No newline at end of file diff --git a/src/messenger/webim/request.php b/src/messenger/webim/request.php index fd91c2b2..2338ba58 100644 --- a/src/messenger/webim/request.php +++ b/src/messenger/webim/request.php @@ -23,30 +23,52 @@ require_once('libs/request.php'); $invited = FALSE; $operator = array(); +$response = array(); if (Settings::get('enabletracking') == '1') { - $entry = isset($_GET['entry']) ? $_GET['entry'] : ""; - $referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ""; + $entry = isset($_GET['entry']) ? $_GET['entry'] : ""; + $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'])) { - $invited = invitation_check($_SESSION['visitorid']); - $visitorid = track_visitor($_SESSION['visitorid'], $entry, $referer); - } - else { - $visitorid = track_visitor_start($entry, $referer); - } + // Check if session start + if (isset($_SESSION['visitorid']) + && preg_match('/^[0-9]+$/', $_SESSION['visitorid'])) { + // Session started. Track visitor + $invited = invitation_check($_SESSION['visitorid']); + $visitorid = track_visitor($_SESSION['visitorid'], $entry, $referer); + } 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); + $visitor = track_get_visitor_by_id($visitorid); + $user_id = $visitor['userid']; + } + } - if ($visitorid) { - $_SESSION['visitorid'] = $visitorid; - } + if ($visitorid) { + $_SESSION['visitorid'] = $visitorid; + } - if ($invited !== FALSE) { - $operator = operator_by_id($invited); - } + if ($invited !== FALSE) { + $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) { $response['load']['mibewInvitationScript'] = get_app_location(true, is_secure_request()) . '/js/compiled/invite.js'; $response['handlers'][] = 'mibewInviteOnResponse';