This repository has been archived by the owner on Apr 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding dedicated download tracker table. Based on SPF stats class.
- Loading branch information
Tyler Hall
committed
Aug 18, 2009
1 parent
2e09042
commit 2d31d61
Showing
4 changed files
with
204 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
<?PHP | ||
// Track your page with... | ||
// Stats::track($some_page_title); | ||
|
||
class Download | ||
{ | ||
private static $me; | ||
|
||
private function __construct() | ||
{ | ||
|
||
} | ||
|
||
public static function track($page_title = '') | ||
{ | ||
$db = Database::getDatabase(); | ||
|
||
$dt = dater(); | ||
$referer = getenv('HTTP_REFERER'); | ||
$referer_is_local = self::refererIsLocal($referer); | ||
$url = full_url(); | ||
$search_terms = self::searchTerms(); | ||
$img_search = ''; | ||
$ip = self::getIP(); | ||
$info = self::browserInfo(); | ||
$browser_family = $info['browser']; | ||
$browser_version = $info['version']; | ||
$os = $info['platform']; | ||
$os_version = ''; | ||
$user_agent = $info['useragent']; | ||
|
||
$exec_time = defined('START_TIME') ? microtime(true) - START_TIME : 0; | ||
$num_queries = $db->numQueries(); | ||
|
||
$sql = "INSERT INTO downloads (dt, referer, referer_is_local, url, page_title, search_terms, img_search, browser_family, browser_version, os, os_version, ip, user_agent, exec_time, num_queries) | ||
VALUES (:dt, :referer, :referer_is_local, :url, :page_title, :search_terms, :img_search, :browser_family, :browser_version, :os, :os_version, :ip, :user_agent, :exec_time, :num_queries)"; | ||
$vals = array('dt' => $dt, | ||
'referer_is_local' => $referer_is_local, | ||
'referer' => $referer, | ||
'url' => $url, | ||
'page_title' => $page_title, | ||
'search_terms' => $search_terms, | ||
'img_search' => $img_search, | ||
'ip' => $ip, | ||
'browser_family' => $browser_family, | ||
'browser_version' => $browser_version, | ||
'os_version' => $os_version, | ||
'os' => $os, | ||
'user_agent' => $user_agent, | ||
'exec_time' => $exec_time, | ||
'num_queries' => $num_queries); | ||
$db->query($sql, $vals); | ||
} | ||
|
||
public static function refererIsLocal($referer = null) | ||
{ | ||
if(is_null($referer)) $referer = getenv('HTTP_REFERER'); | ||
if(!strlen($referer)) return 0; | ||
$regex_host = preg_quote(getenv('HTTP_HOST')); | ||
return (preg_match("!^https?://$regex_host!i", $referer) !== false) ? 1 : 0; | ||
} | ||
|
||
public static function getIP() | ||
{ | ||
$ip = getenv('HTTP_X_FORWARDED_FOR'); | ||
if(!$ip) $ip = getenv('HTTP_CLIENT_IP'); | ||
if(!$ip) $ip = getenv('REMOTE_ADDR'); | ||
return $ip; | ||
} | ||
|
||
public static function searchTerms($url = null) | ||
{ | ||
if(is_null($url)) $url = full_url(); | ||
// if(self::refererIsLocal($url)) return; | ||
|
||
$arr = array(); | ||
parse_str(parse_url($url, PHP_URL_QUERY), $arr); | ||
|
||
return isset($arr['q']) ? $arr['q'] : ''; | ||
} | ||
|
||
// From http://us3.php.net/get_browser comments | ||
public static function browserInfo($a_browser = false, $a_version = false, $name = false) | ||
{ | ||
$browser_list = 'msie firefox konqueror safari netscape navigator opera mosaic lynx amaya omniweb chrome avant camino flock seamonkey aol mozilla gecko'; | ||
$user_browser = strtolower(getenv('HTTP_USER_AGENT')); | ||
$this_version = $this_browser = ''; | ||
|
||
$browser_limit = strlen($user_browser); | ||
foreach(explode(' ', $browser_list) as $row) | ||
{ | ||
$row = ($a_browser !== false) ? $a_browser : $row; | ||
$n = stristr($user_browser, $row); | ||
if(!$n || !empty($this_browser)) continue; | ||
|
||
$this_browser = $row; | ||
$j = strpos($user_browser, $row) + strlen($row) + 1; | ||
for(; $j <= $browser_limit; $j++) | ||
{ | ||
$s = trim(substr($user_browser, $j, 1)); | ||
$this_version .= $s; | ||
|
||
if($s === '') break; | ||
} | ||
} | ||
|
||
if($a_browser !== false) | ||
{ | ||
$ret = false; | ||
if(strtolower($a_browser) == $this_browser) | ||
{ | ||
$ret = true; | ||
|
||
if($a_version !== false && !empty($this_version)) | ||
{ | ||
$a_sign = explode(' ', $a_version); | ||
if(version_compare($this_version, $a_sign[1], $a_sign[0]) === false) | ||
{ | ||
$ret = false; | ||
} | ||
} | ||
} | ||
|
||
return $ret; | ||
} | ||
|
||
$this_platform = ''; | ||
if(strpos($user_browser, 'linux')) | ||
{ | ||
$this_platform = 'linux'; | ||
} | ||
elseif(strpos($user_browser, 'macintosh') || strpos($user_browser, 'mac platform x')) | ||
{ | ||
$this_platform = 'mac'; | ||
} | ||
elseif(strpos($user_browser, 'windows') || strpos($user_browser, 'win32')) | ||
{ | ||
$this_platform = 'windows'; | ||
} | ||
|
||
if($name !== false) | ||
{ | ||
return $this_browser . ' ' . $this_version; | ||
} | ||
|
||
return array("browser" => $this_browser, | ||
"version" => $this_version, | ||
"platform" => $this_platform, | ||
"useragent" => $user_browser); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters