Source of file Tracking.php
Size: 10,562 Bytes - Last Modified: 2021-12-23T10:05:12+00:00
/var/www/docs.ssmods.com/process/src/code/listeners/Tracking.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 | <?php namespace Milkyway\SS\SendThis\Listeners; /** * Milkyway Multimedia * Tracking.php * * @package milkyway-multimedia/ss-send-this * @author Mellisa Hankins <mell@milkywaymultimedia.com.au> */ use Milkyway\SS\SendThis\Contracts\Event; use Milkyway\SS\SendThis\Controllers\Tracker; use SendThis_Log as Log; use SendThis_Link as Link; use Cookie; use SS_Datetime; use Convert; class Tracking { public function up(Event $e, $messageId, $email, $params, $response, $log, $headers) { if (!$log) { return; } if (isset($headers->{'X-LinkData'}) && $headers->{'X-LinkData'}) { $data = $headers->{'X-LinkData'}; if (is_array($data)) { $log->Link_Data = $data; } elseif (is_object($data)) { $log->Link_Data = json_decode(json_encode($data), true); } else { @parse_str($data, $linkData); if ($linkData && count($linkData)) { $log->Link_Data = $linkData; } } unset($headers->{'X-LinkData'}); } if (!$e->mailer()->config()->tracking) { return; } if (isset($headers->{'X-TrackLinks'}) && $headers->{'X-TrackLinks'}) { $log->Track_Links = true; unset($headers->{'X-TrackLinks'}); } if (isset($headers->{'X-Links-AttachSlug'}) && $headers->{'X-Links-AttachSlug'}) { $linkData = isset($linkData) ? $linkData : isset($data) ? $data : []; if (!$log->ID || !$log->Slug) { $log->generateHash(); } if ($headers->{'X-Links-AttachSlug'} === true || $headers->{'X-Links-AttachSlug'} == 1) { if (!isset($linkData['utm_term'])) { $linkData['utm_term'] = $log->Slug; } } elseif (!isset($linkData[$headers->{'X-Links-AttachSlug'}])) { $linkData[$headers->{'X-Links-AttachSlug'}] = $log->Slug; } $log->Link_Data = $linkData; unset($headers->{'X-Links-AttachSlug'}); } } public function sending(Event $e, $messageId = '', $email = '', $params = [], $response = [], $log = null) { if (!$e->mailer()->config()->tracking) { if ($log && $params['message']->ContentType == 'text/html') { $params['message']->Body = $this->removeTracker($log, $this->trackLinks($log, $params['message']->Body)); if ($params['message']->AltBody) { $params['message']->AltBody = $this->removeTracker($log, $params['message']->Body); } } return; } if ($log && isset($params['message'])) { if ($params['message']->ContentType == 'text/plain') { $params['message']->Body = $this->removeTracker($log, $params['message']->Body); } else { $params['message']->Body = $this->insertTracker( $log, $this->trackLinks($log, $params['message']->Body) ); } if ($params['message']->AltBody) { $params['message']->AltBody = $this->removeTracker($log, $params['message']->Body); } } } public function opened(Event $e, $messageId = '', $email = '', $params = [], $response = [], $log = null) { if (!$e->mailer()->config()->tracking) { return; } $logs = []; if ($log) { $logs[] = $log; } elseif ($messageId) { $logs = Log::get()->filter('MessageID', $messageId)->sort('Created', 'ASC'); } if (!count($logs)) { return; } foreach ($logs as $log) { if (!$log->Opened) { $log->Tracker = $this->getTrackerData($params); $log->Track_Client = $this->getClientFromTracker($log->Tracker); $log->Opened = SS_Datetime::now()->Rfc2822(); } $log->Opens++; $log->write(); } } public function clicked(Event $e, $messageId = '', $email = '', $params = [], $response = [], $link = null) { if (!$e->mailer()->config()->tracking || !$link) { return; } if (!Cookie::get('tracking-email-link-' . $link->Slug)) { $link->Visits++; Cookie::set('tracking-email-link-' . $link->Slug, true); } if (!$link->Clicked) { $link->Clicked = SS_Datetime::now()->Rfc2822(); } $link->Clicks++; $link->write(); } protected function insertTracker($log, $content) { $url = singleton('director')->absoluteURL(str_replace('$Slug', urlencode($log->Slug), Tracker::config()->slug)); if (stripos($content, '</body')) { return preg_replace("/(<\/body[^>]*>)/i", '<img src="' . $url . '" alt="" />\\1', $content); } else { return $content . '<img src="' . $url . '" alt="" />'; } } protected function removeTracker($log, $content) { $url = singleton('director')->absoluteURL(str_replace('$Slug', urlencode($log->Slug), Tracker::config()->slug)); return str_replace(array_merge(['<img src="' . $url . '" alt="" />', $url]), '', $content); } protected function trackLinks($log, $content) { if (!$log->Track_Links || !count($log->LinkData)) { return $content; } if (preg_match_all("/<a\s[^>]*href=[\"|']([^\"]*)[\"|'][^>]*>(.*)<\/a>/siU", $content, $matches)) { if (isset($matches[1]) && ($urls = $matches[1])) { $id = (int)$log->ID; $replacements = []; array_unique($urls); $sorted = array_combine($urls, array_map('strlen', $urls)); arsort($sorted); foreach ($sorted as $url => $length) { if ($log->Track_Links) { $link = $log->Links()->filter('Original', Convert::raw2sql($url))->first(); if (!$link) { $link = Link::create(); $link->Original = $this->getURLWithData($log, $url); $link->LogID = $id; $link->write(); } $replacements['"' . $url . '"'] = $link->URL; $replacements["'$url'"] = $link->URL; } else { $replacements['"' . $url . '"'] = $this->getURLWithData($log, $url); $replacements["'$url'"] = $this->getURLWithData($log, $url); } } $content = str_ireplace(array_keys($replacements), array_values($replacements), $content); } } return $content; } protected function getURLWithData($log, $url) { if (!count($log->LinkData)) { return $url; } return singleton('mwm')->add_link_data($url, $log->LinkData); } public function getTrackerData($data) { $tracked = $data; if (!isset($tracked['Referrer'])) { $tracked['Referrer'] = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null; } if (isset($tracked['UserAgentString']) || isset($_SERVER['HTTP_USER_AGENT'])) { if (!isset($tracked['UserAgentString'])) { $tracked['UserAgentString'] = $_SERVER['HTTP_USER_AGENT']; } $agent = base64_encode($tracked['UserAgentString']); $response = @file_get_contents("http://www.useragentstring.com/?uas={$agent}&getJSON=all"); if ($response) { $response = json_decode($response, true); if(!empty($response['agent_type'])) { $tracked['Type'] = $response['agent_type']; } if(!empty($response['agent_name'])) { $tracked['Client'] = $response['agent_name']; } if(!empty($response['agent_version'])) { $tracked['ClientVersion'] = $response['agent_version']; } if(!empty($response['os_type'])) { $tracked['OperatingSystemBrand'] = $response['os_type']; } if(!empty($response['os_name'])) { $tracked['OperatingSystem'] = $response['os_name']; } if(!empty($response['os_versionName'])) { $tracked['OperatingSystemVersion'] = $response['os_versionName']; if(!empty($response['os_versionNumber'])) { $tracked['OperatingSystemVersion'] .= ' ' . $response['os_versionNumber']; } } } } if (isset($data['ip'])) { $geo = @file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $data['ip']); if (($geo = json_decode($geo)) && $country = $geo->geoplugin_countryName) { $tracked['Country'] = $country; } } return $tracked; } protected function getClientFromTracker($tracked) { $client = ''; if (strtolower($tracked['Type']) == 'email client') { $this->$client = $tracked['Client']; } elseif (strtolower($tracked['Type']) == 'browser' || strtolower($tracked['Type']) == 'mobile browser') { if (!preg_match('/.*[0-9]$/', $tracked['ClientFull'])) { $client = _t( 'SendThis_Log.EMAIL_CLIENT-MAC', 'Mac Client (Apple Mail or Microsoft Entourage)' ); } elseif (isset($tracked['Referrer'])) { foreach (Log::config()->web_based_clients as $name => $url) { if (preg_match("/$url/", $tracked['Referrer'])) { $client = _t( 'SendThis_Log.WEB_CLIENT-' . strtoupper(str_replace(' ', '_', $name)), $name ); break; } } } if (!$client) { $client = _t('SendThis_Log.BROWSER_BASED', 'Web Browser'); } } return $client; } } |