Created
January 25, 2009 10:48
-
-
Save riaf/51749 to your computer and use it in GitHub Desktop.
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
<?php | |
/** | |
* customGoogleAnalytics | |
* | |
* @author riaf <[email protected]> | |
* @license New BSD License | |
* @version $Id$ | |
*/ | |
/** | |
* +++ Example +++ | |
* .htaccess | |
* | |
* <code> | |
* RewriteEngine on | |
* RewriteRule ^(.*)$ customGoogleAnalytics.php?$1 [L] | |
* </code> | |
*/ | |
$filename = isset($_SERVER['QUERY_STRING'])? $_SERVER['QUERY_STRING']: '-'; | |
$options = array( | |
'utmwv' => 1, | |
'utmn' => mt_rand(1000000000, 9999999999), | |
'utmcs' => 'UTF-8', | |
'utmul' => 'ja', | |
'utmsr' => '-', | |
'utmsc' => '-', | |
'utmje' => '-', | |
'utmfl' => '-', | |
// 'utmcr' => 1, | |
'utmdt' => '-', | |
'utmhn' => isset($_SERVER['HTTP_HOST'])? $_SERVER['HTTP_HOST']: 'riaf.jp', | |
'utmr' => '-', | |
'utmp' => $filename, | |
'utmac' => 'UA-XXXXXX-X', | |
'utmcc' => get_utmcc(), | |
); | |
// var_dump($options); | |
$url = 'http://www.google-analytics.com/__utm.gif?'. http_build_query($options); | |
$request = 'GET '. $url. " HTTP/1.1\r\n"; | |
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && !empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])){ | |
$request .= 'Accept-language: '. $_SERVER['HTTP_ACCEPT_LANGUAGE']. "\r\n"; | |
} | |
if(isset($_SERVER['HTTP_USER_AGENT']) && !empty($_SERVER['HTTP_USER_AGENT'])){ | |
$request .= 'User-Agent: '. $_SERVER['HTTP_USER_AGENT']. "\r\n"; | |
} | |
$request .= "\r\n"; | |
$s = @fsockopen('www.google-analytics.com', 80, $errno, $errstr, 5); | |
$response = ''; | |
if($s){ | |
fwrite($s, $request); | |
stream_set_timeout($s, 5); | |
while(!feof($s)){ | |
$response .= fgets($s, 4096); | |
} | |
fclose($s); | |
} | |
if($filename{0} == '/'){ | |
list(, $filename) = explode('/', $filename, 2); | |
} | |
if(is_public_file(dirname(__FILE__), $filename)){ | |
// recommend -> PEAR::File_Archive | |
header('Content-Disposition: attachment; filename="'. basename($filename). '"'); | |
header('Content-Type: application/octet-stream'); | |
header('Content-Transfer-Encoding: binary'); | |
header('Content-Length: '.filesize($filename)); | |
readfile($filename); | |
} else die('File Not Found'); | |
function get_utmcc(){ | |
if(isset($_COOKIE['__utma'])){ | |
list($ses,,,,, $id) = explode('.', $_COOKIE['__utma']); | |
} else { | |
$ses = mt_rand(10000000, 99999999); | |
$id = mt_rand(1, 9); | |
} | |
$now = time(); | |
$__utma = isset($_COOKIE['__utma'])? $_COOKIE['__utma']: | |
implode('.', array($ses, mt_rand(1000000000, 2147483647), $now, $now, $now, $id)); | |
$__utmb = isset($_COOKIE['__utmb'])? $_COOKIE['__utmb']: $ses; | |
$__utmc = isset($_COOKIE['__utmc'])? $_COOKIE['__utmc']: $ses; | |
$__utmz = isset($_COOKIE['__utmz'])? $_COOKIE['__utmz']: | |
implode('.', array($ses, $now, $id, 2, 'utmccn=(direct)|utmcsr=(direct)|utmcmd=(none)')); | |
return str_replace(array('=', '|', '+', ';'), array('%3D', '%7C', '%2B', '%3B'), | |
sprintf('__utma=%s;+__utmb=%s;+__utmc=%s;+__utmz=%s;+', $__utma, $__utmb, $__utmc, $__utmz)); | |
} | |
/** | |
* is_public_file | |
* | |
* @author Keita Arai | |
* @see http://labs.unoh.net/2007/11/post_108.html | |
*/ | |
function is_public_file($public_dir, $path) | |
{ | |
if(!is_string($path)){ | |
return false; | |
} | |
$realpath = realpath($path); | |
if($realpath === false){ | |
return false; | |
} | |
if(file_exists($realpath) === false){ | |
return false; | |
} | |
if(strncmp($public_dir, $realpath, strlen($public_dir)) === false){ | |
return false; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment