Merge pull request #1751 from marcosgdf/optimization

Optimized is_ip dolibarr function
This commit is contained in:
Laurent Destailleur 2014-07-26 14:05:14 +02:00
commit 76d4dee411

View File

@ -2,6 +2,7 @@
/* Copyright (C) 2008-2011 Laurent Destailleur <eldy@users.sourceforge.net> /* Copyright (C) 2008-2011 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2008-2012 Regis Houssin <regis.houssin@capnetworks.com> * Copyright (C) 2008-2012 Regis Houssin <regis.houssin@capnetworks.com>
* Copyright (C) 2008 Raphael Bertrand (Resultic) <raphael.bertrand@resultic.fr> * Copyright (C) 2008 Raphael Bertrand (Resultic) <raphael.bertrand@resultic.fr>
* Copyright (C) 2014 Marcos García <marcosgdf@gmail.com>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -1363,20 +1364,23 @@ function getListOfModels($db,$type,$maxfilenamelength=0)
/** /**
* This function evaluates a string that should be a valid IPv4 * This function evaluates a string that should be a valid IPv4
* *
* @param string $ip IP Address * @param string $ip IP Address
* @return It returns 0 if $ip is not a valid IPv4 * @return int 0 if not valid, 1 if valid and public IP, 2 if valid and private range IP
* 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
*/ */
function is_ip($ip) 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; //First we test if it is a valid IPv4
if (sprintf("%u",ip2long($ip)) == sprintf("%u",ip2long('255.255.255.255'))) return 0; if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
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; //Then we test if it is not a private range
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 (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)) {
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;
return 1; }
return 2;
}
return 0;
} }
/** /**