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).
|
| $Source: /cvs_backup/e107_0.8/e107_handlers/e107_class.php,v $
| $Revision: 1.25 $
| $Date: 2008-11-25 16:26:02 $
| $Author: mcfly_e107 $
| $Revision: 1.26 $
| $Date: 2009-01-04 20:55:37 $
| $Author: e107steved $
+----------------------------------------------------------------------------+
*/
@@ -395,19 +395,20 @@ class e107
}
// 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
function ipEncode($ip, $div=':')
{
$ret = '';
$divider = '';
if (strstr($ip,':'))
if (strpos($ip,':') !== FALSE)
{ // Its IPV6 (could have an IP4 'tail')
if (strstr($ip,'.'))
if (strpos($ip,'.') !== FALSE)
{ // IPV4 'tail' to deal with
$temp = strrpos($ip,':') +1;
$ip4 = substr($ip,$temp);
$ip = substr($ip,0, $temp).$this->ip4_encode($ip4);
$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);
@@ -432,7 +433,7 @@ class e107
}
return $ret;
}
if (strstr($ip,'.'))
if (strpos($ip,'.') !== FALSE)
{ // Its IPV4
$ipa = explode('.', $ip);
$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
|
| $Source: /cvs_backup/e107_0.8/e107_plugins/log/log.php,v $
| $Revision: 1.5 $
| $Date: 2007-11-11 12:10:54 $
| $Revision: 1.6 $
| $Date: 2009-01-04 20:55:43 $
| $Author: e107steved $
+----------------------------------------------------------------------------+
*/
@@ -27,6 +27,8 @@
// res= res
// err_direct - optional error flag
// 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);
@@ -144,6 +146,7 @@ if(!strstr($ipAddresses, $ip))
require_once("loginfo.php");
}
$siteTotal ++;
$info_data = var_export($pageInfo, true);
//$date_stamp = date("z:Y", time()); // Same as '$date' variable
@@ -169,12 +172,12 @@ if ($p_handle)
}
function getip($mode=TRUE)
{
if (getenv('HTTP_X_FORWARDED_FOR'))
// Get current IP address - return as a hex-encoded string
function getip()
{
$ip = $_SERVER['REMOTE_ADDR'];
if (getenv('HTTP_X_FORWARDED_FOR'))
{
if (preg_match("#^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})#", getenv('HTTP_X_FORWARDED_FOR'), $ip3))
{
$ip2 = array('#^0\..*#',
@@ -188,22 +191,43 @@ function getip($mode=TRUE)
$ip = preg_replace($ip2, $ip, $ip3[1]);
}
}
else
{
$ip = $_SERVER['REMOTE_ADDR'];
}
if ($ip == "")
{
$ip = "x.x.x.x";
}
if($mode)
{
if (strpos($ip, ':') === FALSE)
{ // Its an IPV4 address - return it as 32-character packed hex string
$ipa = explode(".", $ip);
return sprintf('%02x%02x%02x%02x', $ipa[0], $ipa[1], $ipa[2], $ipa[3]);
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
{
return $ip;
$ret .= sprintf('%04x',hexdec($f));
}
}
return $ret;
}
}