From 536770caca6f33d2d7cb846878a4d3ad0aaff26a Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Thu, 31 Jan 2008 04:30:36 +0000 Subject: [PATCH] mrickerby: #2201 - fixed PageView's recording of referrers. git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@48905 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/PageView.php | 83 +++++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 37 deletions(-) diff --git a/core/model/PageView.php b/core/model/PageView.php index f2264512b..caacb2fd9 100644 --- a/core/model/PageView.php +++ b/core/model/PageView.php @@ -4,7 +4,15 @@ */ /** - * Data object that represents any page view in the system. + * Data object that logs statistics for any page view in the system. + * + * Inbound links from external websites are distinquished by a 'true' + * value in the FromExternal field. False values are + * + * The referring urls are recorded in the Referrer field. + * + * Information about the users browser version and operating system is also recorded. + * * @package cms */ class PageView extends DataObject { @@ -34,66 +42,67 @@ class PageView extends DataObject { static $defaults = array(); protected $hitdata = null; - + function init() { $browscap = new Browscap(); $this->hitdata = $browscap->getBrowser(null, true); } - + + /** + * gathers data for this page view and writes + * it to the data source. + */ function record() { $this->init(); - $this->setBrowser(); - $this->setOS(); - $this->setUserID(); - $this->setPageID(); - $this->setIP(); + $this->recordBrowser(); + $this->recordOS(); + $this->recordUserID(); + $this->recordPageID(); + $this->recordIP(); + $this->recordFromExternal(); + $this->recordReferrer(); $this->write(true); } - function sanitize($str) { - //TODO - return $str; + private function recordFromExternal() { + $http_host = "http://".$_SERVER['HTTP_HOST']; + if (!strstr($_SERVER['HTTP_REFERER'], $http_host) && $_SERVER['HTTP_REFERER'] != null) + $this->FromExternal = 1; } - function setBrowser() { - if(isset($this->hitdata['Browser'])) - $this->setField('Browser', $this->hitdata['Browser']); + private function recordBrowser() { + if (isset($this->hitdata['Browser'])) + $this->Browser = $this->hitdata['Browser']; - if(isset($this->hitdata['Version'])) - $this->setField('BrowserVersion', $this->hitdata['Version']); + if (isset($this->hitdata['Version'])) + $this->BrowserVersion = $this->hitdata['Version']; + } + + private function recordReferrer() { + $this->Referrer = $_SERVER['HTTP_REFERER']; } - function setOS() { + private function recordOS() { if(isset($this->hitdata['Platform'])) - $this->setField('OS', $this->hitdata['Platform']); + $this->OS = $this->hitdata['Platform']; } - function setUserID() { + private function recordUserID() { $isLogged = Session::get('loggedInAs'); - if($isLogged) { - $id = $isLogged; - } else { - $id = -1; - } - $this->setField('UserID', $id); + $id = ($isLogged) ? $isLogged : -1; + $this->UserID = $id; } - function setPageID() { + private function recordPageID() { $currentPage = Director::currentPage(); - if($currentPage) - $this->setField('PageID', $currentPage->ID); + if ($currentPage) $this->PageID = $currentPage->ID; } - function setIP() { - if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { - $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; - } else { - $ip = $_SERVER['REMOTE_ADDR']; - } - $this->setField('IP', $ip); + private function recordIP() { + $this->IP = (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) + ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']; } - + } - ?> \ No newline at end of file