2007-09-16 17:37:33 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2008-02-25 03:10:37 +01:00
|
|
|
* @package cms
|
2007-09-16 17:37:33 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2008-02-25 03:10:37 +01:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* The referring urls are recorded in the Referrer field.
|
|
|
|
*
|
|
|
|
* Information about the users browser version and operating system is also recorded.
|
|
|
|
*
|
|
|
|
* @package cms
|
2007-09-16 17:37:33 +02:00
|
|
|
*/
|
|
|
|
class PageView extends DataObject {
|
|
|
|
|
|
|
|
static $db = array(
|
|
|
|
"IP" => "Varchar(255)",
|
|
|
|
"Browser" => "Varchar(255)",
|
|
|
|
"BrowserVersion" => "Decimal",
|
|
|
|
"FromExternal" => "Boolean",
|
|
|
|
"Referrer" => "Varchar(255)",
|
|
|
|
"SearchEngine" => "Boolean",
|
|
|
|
"Keywords" => "Varchar(255)",
|
|
|
|
"OS" => "Varchar(255)"
|
|
|
|
);
|
|
|
|
|
|
|
|
static $has_one = array(
|
|
|
|
"Page" => "SiteTree",
|
|
|
|
"User" => "Member"
|
|
|
|
);
|
|
|
|
|
|
|
|
static $has_many = array();
|
|
|
|
|
|
|
|
static $many_many = array();
|
|
|
|
|
|
|
|
static $belongs_many_many = array();
|
|
|
|
|
|
|
|
static $defaults = array();
|
|
|
|
|
2007-09-16 17:40:27 +02:00
|
|
|
protected $hitdata = null;
|
2008-02-25 03:10:37 +01:00
|
|
|
|
2007-09-16 17:37:33 +02:00
|
|
|
function init() {
|
2007-10-05 03:58:21 +02:00
|
|
|
$browscap = new Browscap();
|
|
|
|
$this->hitdata = $browscap->getBrowser(null, true);
|
2007-09-16 17:37:33 +02:00
|
|
|
}
|
2008-02-25 03:10:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gathers data for this page view and writes
|
|
|
|
* it to the data source.
|
|
|
|
*/
|
2007-09-16 17:37:33 +02:00
|
|
|
function record() {
|
|
|
|
$this->init();
|
2008-02-25 03:10:37 +01:00
|
|
|
$this->recordBrowser();
|
|
|
|
$this->recordOS();
|
|
|
|
$this->recordUserID();
|
|
|
|
$this->recordPageID();
|
|
|
|
$this->recordIP();
|
|
|
|
$this->recordFromExternal();
|
|
|
|
$this->recordReferrer();
|
2007-09-16 17:37:33 +02:00
|
|
|
$this->write(true);
|
|
|
|
}
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
private function recordFromExternal() {
|
|
|
|
$http_host = "http://".$_SERVER['HTTP_HOST'];
|
|
|
|
if (isset($_SERVER['HTTP_REFERER']) && !strstr($_SERVER['HTTP_REFERER'], $http_host) && $_SERVER['HTTP_REFERER'] != null)
|
|
|
|
$this->FromExternal = 1;
|
2007-09-16 17:37:33 +02:00
|
|
|
}
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
private function recordBrowser() {
|
|
|
|
if (isset($this->hitdata['Browser']))
|
|
|
|
$this->Browser = $this->hitdata['Browser'];
|
2007-09-16 17:40:27 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
if (isset($this->hitdata['Version']))
|
|
|
|
$this->BrowserVersion = $this->hitdata['Version'];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function recordReferrer() {
|
|
|
|
if(isset($_SERVER['HTTP_REFERER'])) $this->Referrer = $_SERVER['HTTP_REFERER'];
|
2007-09-16 17:37:33 +02:00
|
|
|
}
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
private function recordOS() {
|
2007-10-05 03:58:21 +02:00
|
|
|
if(isset($this->hitdata['Platform']))
|
2008-02-25 03:10:37 +01:00
|
|
|
$this->OS = $this->hitdata['Platform'];
|
2007-09-16 17:37:33 +02:00
|
|
|
}
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
private function recordUserID() {
|
2007-09-16 17:37:33 +02:00
|
|
|
$isLogged = Session::get('loggedInAs');
|
2008-02-25 03:10:37 +01:00
|
|
|
$id = ($isLogged) ? $isLogged : -1;
|
|
|
|
$this->UserID = $id;
|
2007-09-16 17:37:33 +02:00
|
|
|
}
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
private function recordPageID() {
|
2007-09-16 17:37:33 +02:00
|
|
|
$currentPage = Director::currentPage();
|
2008-02-25 03:10:37 +01:00
|
|
|
if ($currentPage) $this->PageID = $currentPage->ID;
|
2007-09-16 17:37:33 +02:00
|
|
|
}
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
private function recordIP() {
|
|
|
|
$this->IP = (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
|
|
|
|
? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
|
2007-09-16 17:37:33 +02:00
|
|
|
}
|
2008-02-25 03:10:37 +01:00
|
|
|
|
2007-09-16 17:37:33 +02:00
|
|
|
}
|
|
|
|
|
2007-09-16 17:40:27 +02:00
|
|
|
?>
|