From cdc40ca76fe9ae6692b3a9ece00924fe5dd8921c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garci=CC=81a=20de=20La=20Fuente?= Date: Thu, 17 Jul 2014 21:12:50 +0200 Subject: [PATCH 1/2] Optimized is_ip dolibarr function --- htdocs/core/lib/functions2.lib.php | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/htdocs/core/lib/functions2.lib.php b/htdocs/core/lib/functions2.lib.php index 4e9598d5e20..3f13e6dccaa 100644 --- a/htdocs/core/lib/functions2.lib.php +++ b/htdocs/core/lib/functions2.lib.php @@ -2,6 +2,7 @@ /* Copyright (C) 2008-2011 Laurent Destailleur * Copyright (C) 2008-2012 Regis Houssin * Copyright (C) 2008 Raphael Bertrand (Resultic) + * Copyright (C) 2014 Marcos GarcĂ­a * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -1370,13 +1371,18 @@ function getListOfModels($db,$type,$maxfilenamelength=0) */ function is_ip($ip) { - if (!preg_match("/^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/", $ip)) return 0; - if (sprintf("%u",ip2long($ip)) == sprintf("%u",ip2long('255.255.255.255'))) return 0; - if (sprintf("%u",ip2long('10.0.0.0')) <= sprintf("%u",ip2long($ip)) and sprintf("%u",ip2long($ip)) <= sprintf("%u",ip2long('10.255.255.255'))) return 2; - if (sprintf("%u",ip2long('172.16.0.0')) <= sprintf("%u",ip2long($ip)) and sprintf("%u",ip2long($ip)) <= sprintf("%u",ip2long('172.31.255.255'))) return 2; - if (sprintf("%u",ip2long('192.168.0.0')) <= sprintf("%u",ip2long($ip)) and sprintf("%u",ip2long($ip)) <= sprintf("%u",ip2long('192.168.255.255'))) return 2; - if (sprintf("%u",ip2long('169.254.0.0')) <= sprintf("%u",ip2long($ip)) and sprintf("%u",ip2long($ip)) <= sprintf("%u",ip2long('169.254.255.255'))) return 2; - return 1; + //First we test if it is a valid IPv4 + if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { + + //Then we test if it is not a private range + if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)) { + return 1; + } + + return 2; + } + + return 0; } /** From 37d4d303b33d241ab54cc60bf22e6547dc4c54fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garci=CC=81a=20de=20La=20Fuente?= Date: Thu, 17 Jul 2014 21:19:03 +0200 Subject: [PATCH 2/2] Updated is_ip comment and added a test for is_ip function --- htdocs/core/lib/functions2.lib.php | 6 ++---- test/phpunit/Functions2LibTest.php | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/htdocs/core/lib/functions2.lib.php b/htdocs/core/lib/functions2.lib.php index 3f13e6dccaa..294dbafa82e 100644 --- a/htdocs/core/lib/functions2.lib.php +++ b/htdocs/core/lib/functions2.lib.php @@ -1364,10 +1364,8 @@ function getListOfModels($db,$type,$maxfilenamelength=0) /** * This function evaluates a string that should be a valid IPv4 * - * @param string $ip IP Address - * @return It returns 0 if $ip is not a valid IPv4 - * It returns 1 if $ip is a valid IPv4 and is a public IP - * It returns 2 if $ip is a valid IPv4 and is a private lan IP + * @param string $ip IP Address + * @return int 0 if not valid, 1 if valid and public IP, 2 if valid and private range IP */ function is_ip($ip) { diff --git a/test/phpunit/Functions2LibTest.php b/test/phpunit/Functions2LibTest.php index 8e4bfe7550f..b59b9f3795c 100755 --- a/test/phpunit/Functions2LibTest.php +++ b/test/phpunit/Functions2LibTest.php @@ -154,4 +154,23 @@ class Functions2LibTest extends PHPUnit_Framework_TestCase $this->assertEquals(0,$result); } + /** + * is_ip + * + * @return void + */ + public function testIsIp() { + + //Test not valid IP + $result = is_ip('192.168.1.267'); + $this->assertEquals(0, $result); + + //Test private range IP + $result = is_ip('192.168.1.1'); + $this->assertEquals(2, $result); + + //Test public range IP + $result = is_ip('91.121.33.228'); + $this->assertEquals(1, $result); + } }