summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuy Brand <gb@isis.u-strasbg.fr>2008-10-12 21:27:28 +0200
committerGuy Brand <gb@isis.u-strasbg.fr>2008-10-12 21:27:28 +0200
commitdc14c6d1104eea9f954b6aa31c0383a55186e177 (patch)
tree895ac4b9118c7854f9c1c9c684c0f7e074394e82
parentcddd152cd07fff794aa5023d9cd6e61e26ab433c (diff)
downloadrpg-dc14c6d1104eea9f954b6aa31c0383a55186e177.tar.gz
rpg-dc14c6d1104eea9f954b6aa31c0383a55186e177.tar.bz2
Fix IPv4 regexp and add IPv6 regexp (clientIP)
darcs-hash:20081012192728-19e2d-5219ad7bf461a758b62ee311598937ecd958916b.gz
-rw-r--r--inc/common.php22
1 files changed, 21 insertions, 1 deletions
diff --git a/inc/common.php b/inc/common.php
index 1532c523a..27b5663b9 100644
--- a/inc/common.php
+++ b/inc/common.php
@@ -594,11 +594,31 @@ function clientIP($single=false){
if(!empty($_SERVER['HTTP_X_REAL_IP']))
$ip = array_merge($ip,explode(',',$_SERVER['HTTP_X_REAL_IP']));
+ // some IPv4/v6 regexps borrowed from Feyd
+ // see: http://forums.devnetwork.net/viewtopic.php?f=38&t=53479
+ $dec_octet = '(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|[0-9])';
+ $hex_digit = '[A-Fa-f0-9]';
+ $h16 = "{$hex_digit}{1,4}";
+ $IPv4Address = "$dec_octet\\.$dec_octet\\.$dec_octet\\.$dec_octet";
+ $ls32 = "(?:$h16:$h16|$IPv4Address)";
+ $IPv6Address =
+ "(?:(?:{$IPv4Address})|(?:".
+ "(?:$h16:){6}$ls32" .
+ "|::(?:$h16:){5}$ls32" .
+ "|(?:$h16)?::(?:$h16:){4}$ls32" .
+ "|(?:(?:$h16:){0,1}$h16)?::(?:$h16:){3}$ls32" .
+ "|(?:(?:$h16:){0,2}$h16)?::(?:$h16:){2}$ls32" .
+ "|(?:(?:$h16:){0,3}$h16)?::(?:$h16:){1}$ls32" .
+ "|(?:(?:$h16:){0,4}$h16)?::$ls32" .
+ "|(?:(?:$h16:){0,5}$h16)?::$h16" .
+ "|(?:(?:$h16:){0,6}$h16)?::" .
+ ")(?:\\/(?:12[0-8]|1[0-1][0-9]|[1-9][0-9]|[0-9]))?)";
+
// remove any non-IP stuff
$cnt = count($ip);
$match = array();
for($i=0; $i<$cnt; $i++){
- if(preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/',$ip[$i],$match)) {
+ if(preg_match("/^$IPv4Address$/",$ip[$i],$match) || preg_match("/^$IPv6Address$/",$ip[$i],$match)) {
$ip[$i] = $match[0];
} else {
$ip[$i] = '';