1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-07 23:26:41 +02:00

More work on IPV6

This commit is contained in:
e107steved
2009-01-04 20:55:43 +00:00
parent 9aa53886b5
commit 423e0b1ffe
2 changed files with 100 additions and 75 deletions

View File

@@ -11,9 +11,9 @@
| GNU General Public License (http://gnu.org). | GNU General Public License (http://gnu.org).
| |
| $Source: /cvs_backup/e107_0.8/e107_handlers/e107_class.php,v $ | $Source: /cvs_backup/e107_0.8/e107_handlers/e107_class.php,v $
| $Revision: 1.25 $ | $Revision: 1.26 $
| $Date: 2008-11-25 16:26:02 $ | $Date: 2009-01-04 20:55:37 $
| $Author: mcfly_e107 $ | $Author: e107steved $
+----------------------------------------------------------------------------+ +----------------------------------------------------------------------------+
*/ */
@@ -395,19 +395,20 @@ class e107
} }
// Encode an IP address to internal representation. Returns string if successful; FALSE on error // Encode an IP address to internal representation. Returns string if successful; FALSE on error
// Default separates fields with ':'; set $div='' to produce a 32-char packed hex string // Default separates fields with ':'; set $div='' to produce a 32-char packed hex string
function ipEncode($ip, $div=':') function ipEncode($ip, $div=':')
{ {
$ret = ''; $ret = '';
$divider = ''; $divider = '';
if (strstr($ip,':')) if (strpos($ip,':') !== FALSE)
{ // Its IPV6 (could have an IP4 'tail') { // Its IPV6 (could have an IP4 'tail')
if (strstr($ip,'.')) if (strpos($ip,'.') !== FALSE)
{ // IPV4 'tail' to deal with { // IPV4 'tail' to deal with
$temp = strrpos($ip,':') +1; $temp = strrpos($ip,':') +1;
$ip4 = substr($ip,$temp); $ipa = explode('.',substr($ip,$temp));
$ip = substr($ip,0, $temp).$this->ip4_encode($ip4); $ip = substr($ip,0, $temp).sprintf('%02x%02x:%02x%02x', $ipa[0], $ipa[1], $ipa[2], $ipa[3]);
} }
// Now 'normalise' the address // Now 'normalise' the address
$temp = explode(':',$ip); $temp = explode(':',$ip);
@@ -432,7 +433,7 @@ class e107
} }
return $ret; return $ret;
} }
if (strstr($ip,'.')) if (strpos($ip,'.') !== FALSE)
{ // Its IPV4 { // Its IPV4
$ipa = explode('.', $ip); $ipa = explode('.', $ip);
$temp = sprintf('%02x%02x%s%02x%02x', $ipa[0], $ipa[1], $div, $ipa[2], $ipa[3]); $temp = sprintf('%02x%02x%s%02x%02x', $ipa[0], $ipa[1], $div, $ipa[2], $ipa[3]);

View File

@@ -13,8 +13,8 @@
| File locking, modified getip() 18.01.07 | File locking, modified getip() 18.01.07
| |
| $Source: /cvs_backup/e107_0.8/e107_plugins/log/log.php,v $ | $Source: /cvs_backup/e107_0.8/e107_plugins/log/log.php,v $
| $Revision: 1.5 $ | $Revision: 1.6 $
| $Date: 2007-11-11 12:10:54 $ | $Date: 2009-01-04 20:55:43 $
| $Author: e107steved $ | $Author: e107steved $
+----------------------------------------------------------------------------+ +----------------------------------------------------------------------------+
*/ */
@@ -27,6 +27,8 @@
// res= res // res= res
// err_direct - optional error flag // err_direct - optional error flag
// err_referer - referrer if came via error page // err_referer - referrer if came via error page
// Normally the file is 'silent' - iff any errors occur, they'll usually appear within the page's CSS due to the way its called
define("log_INIT", TRUE); define("log_INIT", TRUE);
@@ -41,31 +43,31 @@ $logPfile = "logs/logp_".$date.".php";
// vet resolution and colour depth some more - avoid dud values // vet resolution and colour depth some more - avoid dud values
if ($res && preg_match("#.*?((\d+)\w+?(\d+))#", $res, $match)) if ($res && preg_match("#.*?((\d+)\w+?(\d+))#", $res, $match))
{ {
$res = $match[2].'x'.$match[3]; $res = $match[2].'x'.$match[3];
} }
else else
{ {
$res = '??'; // Can't decode resolution $res = '??'; // Can't decode resolution
} }
if ($colour && preg_match("#.*?(\d+)#",$colour,$match)) if ($colour && preg_match("#.*?(\d+)#",$colour,$match))
{ {
$colour = intval($match[1]); $colour = intval($match[1]);
} }
else else
{ {
$colour='??'; $colour='??';
} }
if ($err_code = strip_tags((isset($_REQUEST['err_direct']) ? $_REQUEST['err_direct'] : ''))) if ($err_code = strip_tags((isset($_REQUEST['err_direct']) ? $_REQUEST['err_direct'] : '')))
{ {
$ref = addslashes(strip_tags(isset($_REQUEST['err_referer']) ? $_REQUEST['err_referer'] : '')); $ref = addslashes(strip_tags(isset($_REQUEST['err_referer']) ? $_REQUEST['err_referer'] : ''));
$log_string = $err_code.",".$self.",".$ref; $log_string = $err_code.",".$self.",".$ref;
// Uncomment the next two lines to create a separate CSV format log of invalid accesses - error code, entered URL, referrer // Uncomment the next two lines to create a separate CSV format log of invalid accesses - error code, entered URL, referrer
// $logname = "logs/errpages.csv"; // $logname = "logs/errpages.csv";
// $logfp = fopen($logname, 'a+'); fwrite($logfp, $log_string."\n\r"); fclose($logfp); // $logfp = fopen($logname, 'a+'); fwrite($logfp, $log_string."\n\r"); fclose($logfp);
$err_code .= ':'; $err_code .= ':';
} }
if(strstr($ref, "admin")) if(strstr($ref, "admin"))
@@ -80,10 +82,10 @@ $ip = getip();
$oldref = $ref; // backup for search string being stripped off for referer $oldref = $ref; // backup for search string being stripped off for referer
if($ref && !strstr($ref, $_SERVER['HTTP_HOST'])) if($ref && !strstr($ref, $_SERVER['HTTP_HOST']))
{ {
if(preg_match("#http://(.*?)($|/)#is", $ref, $match)) if(preg_match("#http://(.*?)($|/)#is", $ref, $match))
{ {
$ref = $match[0]; $ref = $match[0];
} }
} }
$pageDisallow = "cache|file|eself|admin"; $pageDisallow = "cache|file|eself|admin";
@@ -105,45 +107,46 @@ if(preg_match("/".$pageDisallow."/i", $pageName)) return;
$p_handle = fopen($logPfile, 'r+'); $p_handle = fopen($logPfile, 'r+');
if($p_handle && flock( $p_handle, LOCK_EX ) ) if($p_handle && flock( $p_handle, LOCK_EX ) )
{ {
$log_file_contents = ''; $log_file_contents = '';
while (!feof($p_handle)) while (!feof($p_handle))
{ // Assemble a string of data { // Assemble a string of data
$log_file_contents.= fgets($p_handle,1000); $log_file_contents.= fgets($p_handle,1000);
} }
$log_file_contents = str_replace(array('<'.'?php','?'.'>'),'',$log_file_contents); $log_file_contents = str_replace(array('<'.'?php','?'.'>'),'',$log_file_contents);
if (eval($log_file_contents) === FALSE) echo "error in log file contents<br /><br /><br /><br />"; if (eval($log_file_contents) === FALSE) echo "error in log file contents<br /><br /><br /><br />";
} }
else else
{ {
echo "Couldn't log data<br /><br /><br /><br />"; echo "Couldn't log data<br /><br /><br /><br />";
exit; exit;
} }
$flag = FALSE; $flag = FALSE;
if(array_key_exists($pageName, $pageInfo)) if(array_key_exists($pageName, $pageInfo))
{ // Existing page - just increment stats { // Existing page - just increment stats
$pageInfo[$pageName]['ttl'] ++; $pageInfo[$pageName]['ttl'] ++;
} }
else else
{ // First access of page { // First access of page
$url = preg_replace("/".$tagRemove2."/si", "", $self); $url = preg_replace("/".$tagRemove2."/si", "", $self);
if(preg_match("/".$pageDisallow."/i", $url)) return; if(preg_match("/".$pageDisallow."/i", $url)) return;
$pageInfo[$pageName] = array('url' => $url, 'ttl' => 1, 'unq' => 1); $pageInfo[$pageName] = array('url' => $url, 'ttl' => 1, 'unq' => 1);
$flag = TRUE; $flag = TRUE;
} }
if(!strstr($ipAddresses, $ip)) if(!strstr($ipAddresses, $ip))
{ /* unique visit */ { /* unique visit */
if(!$flag) if(!$flag)
{ {
$pageInfo[$pageName]['unq'] ++; $pageInfo[$pageName]['unq'] ++;
} }
$siteUnique ++; $siteUnique ++;
$ipAddresses .= $ip."."; // IP address is stored as hex string $ipAddresses .= $ip."."; // IP address is stored as hex string
require_once("loginfo.php"); require_once("loginfo.php");
} }
$siteTotal ++; $siteTotal ++;
$info_data = var_export($pageInfo, true); $info_data = var_export($pageInfo, true);
//$date_stamp = date("z:Y", time()); // Same as '$date' variable //$date_stamp = date("z:Y", time()); // Same as '$date' variable
@@ -169,42 +172,63 @@ if ($p_handle)
} }
// Get current IP address - return as a hex-encoded string
function getip($mode=TRUE) function getip()
{ {
if (getenv('HTTP_X_FORWARDED_FOR'))
{
$ip = $_SERVER['REMOTE_ADDR']; $ip = $_SERVER['REMOTE_ADDR'];
if (preg_match("#^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})#", getenv('HTTP_X_FORWARDED_FOR'), $ip3)) if (getenv('HTTP_X_FORWARDED_FOR'))
{ {
$ip2 = array('#^0\..*#', if (preg_match("#^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})#", getenv('HTTP_X_FORWARDED_FOR'), $ip3))
'#^127\..*#', // Local loopbacks {
'#^192\.168\..*#', // RFC1918 - Private Network $ip2 = array('#^0\..*#',
'#^172\.(?:1[6789]|2\d|3[01])\..*#', // RFC1918 - Private network '#^127\..*#', // Local loopbacks
'#^10\..*#', // RFC1918 - Private Network '#^192\.168\..*#', // RFC1918 - Private Network
'#^169\.254\..*#', // RFC3330 - Link-local, auto-DHCP '#^172\.(?:1[6789]|2\d|3[01])\..*#', // RFC1918 - Private network
'#^2(?:2[456789]|[345][0-9])\..*#' // Single check for Class D and Class E '#^10\..*#', // RFC1918 - Private Network
); '#^169\.254\..*#', // RFC3330 - Link-local, auto-DHCP
$ip = preg_replace($ip2, $ip, $ip3[1]); '#^2(?:2[456789]|[345][0-9])\..*#' // Single check for Class D and Class E
);
$ip = preg_replace($ip2, $ip, $ip3[1]);
}
}
if ($ip == "")
{
$ip = "x.x.x.x";
}
if (strpos($ip, ':') === FALSE)
{ // Its an IPV4 address - return it as 32-character packed hex string
$ipa = explode(".", $ip);
return str_repeat('0000',5).'ffff'.sprintf('%02x%02x%02x%02x', $ipa[0], $ipa[1], $ipa[2], $ipa[3]);
}
else
{ // Its IPV6
if (strpos($ip,'.') !== FALSE)
{ // IPV4 'tail' to deal with
$temp = strrpos($ip,':') +1;
$ipa = explode('.',substr($ip,$temp));
$ip = substr($ip,0, $temp).sprintf('%02x%02x:%02x%02x', $ipa[0], $ipa[1], $ipa[2], $ipa[3]);
}
// Now 'normalise' the address
$temp = explode(':',$ip);
$s = 8 - count($temp); // One element will of course be the blank
foreach ($temp as $f)
{
if ($f == '')
{
$ret .= '0000'; // Always put in one set of zeros for the blank
if ($s > 0)
{
$ret .= str_repeat('0000',$s);
$s = 0;
}
}
else
{
$ret .= sprintf('%04x',hexdec($f));
}
}
return $ret;
} }
}
else
{
$ip = $_SERVER['REMOTE_ADDR'];
}
if ($ip == "")
{
$ip = "x.x.x.x";
}
if($mode)
{
$ipa = explode(".", $ip);
return sprintf('%02x%02x%02x%02x', $ipa[0], $ipa[1], $ipa[2], $ipa[3]);
}
else
{
return $ip;
}
} }
?> ?>