note 108812 modified in function.ip2long by danbrown

From: Date: Sat, 26 May 2012 15:25:55 +0000
Subject: note 108812 modified in function.ip2long by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-188251@lists.php.net to get a copy of this message
Hi,&#13;&#10;based on examples above I have mixed class IPFilter with netMatch function wich gives me complete class for IP4 check including CIDR IP format:&#13;&#10;&#13;&#10;&#60;?php&#13;&#10;class IP4Filter {&#13;&#10;&#13;&#10; private static $_IP_TYPE_SINGLE = &#39;single&#39;;&#13;&#10; private static $_IP_TYPE_WILDCARD = &#39;wildcard&#39;;&#13;&#10; private static $_IP_TYPE_MASK = &#39;mask&#39;;&#13;&#10; private static $_IP_TYPE_CIDR = &#39;CIDR&#39;;&#13;&#10; private static $_IP_TYPE_SECTION = &#39;section&#39;;&#13;&#10; private $_allowed_ips = array();&#13;&#10;&#13;&#10; public function __construct($allowed_ips) {&#13;&#10; $this-&#62;_allowed_ips = $allowed_ips;&#13;&#10; }&#13;&#10;&#13;&#10; public function check($ip, $allowed_ips = null) {&#13;&#10; $allowed_ips = $allowed_ips ? $allowed_ips : $this-&#62;_allowed_ips;&#13;&#10;&#13;&#10; foreach ($allowed_ips as $allowed_ip) {&#13;&#10; $type = $this-&#62;_judge_ip_type($allowed_ip);&#13;&#10; $sub_rst = call_user_func(array($this, &#39;_sub_checker_&#39; . $type), $allowed_ip, $ip);&#13;&#10;&#13;&#10; if ($sub_rst) {&#13;&#10; return true;&#13;&#10; }&#13;&#10; }&#13;&#10;&#13;&#10; return false;&#13;&#10; }&#13;&#10;&#13;&#10; private function _judge_ip_type($ip) {&#13;&#10; if (strpos($ip, &#39;*&#39;)) {&#13;&#10; return self :: $_IP_TYPE_WILDCARD;&#13;&#10; }&#13;&#10;&#13;&#10; if (strpos($ip, &#39;/&#39;)) {&#13;&#10; $tmp = explode(&#39;/&#39;, $ip);&#13;&#10; if (strpos($tmp[1], &#39;-&#39;)) {&#13;&#10; return self :: $_IP_TYPE_MASK;&#13;&#10; } else {&#13;&#10; return self :: $_IP_TYPE_CIDR;&#13;&#10; }&#13;&#10; }&#13;&#10;&#13;&#10; if (strpos($ip, &#39;-&#39;)) {&#13;&#10; return self :: $_IP_TYPE_SECTION;&#13;&#10; }&#13;&#10;&#13;&#10; if (ip2long($ip)) {&#13;&#10; return self :: $_IP_TYPE_SINGLE;&#13;&#10; }&#13;&#10;&#13;&#10; return false;&#13;&#10; }&#13;&#10;&#13;&#10; private function _sub_checker_single($allowed_ip, $ip) {&#13;&#10; return (ip2long($allowed_ip) == ip2long($ip));&#13;&#10; }&#13;&#10;&#13;&#10; private function _sub_checker_wildcard($allowed_ip, $ip) {&#13;&#10; $allowed_ip_arr = explode(&#39;.&#39;, $allowed_ip);&#13;&#10; $ip_arr = explode(&#39;.&#39;, $ip);&#13;&#10; for ($i = 0; $i &#60; count($allowed_ip_arr); $i++) {&#13;&#10; if ($allowed_ip_arr[$i] == &#39;*&#39;) {&#13;&#10; return true;&#13;&#10; } else {&#13;&#10; if (false == ($allowed_ip_arr[$i] == $ip_arr[$i])) {&#13;&#10; return false;&#13;&#10; }&#13;&#10; }&#13;&#10; }&#13;&#10; }&#13;&#10;&#13;&#10; private function _sub_checker_mask($allowed_ip, $ip) {&#13;&#10; list($allowed_ip_ip, $allowed_ip_mask) = explode(&#39;/&#39;, $allowed_ip);&#13;&#10; $begin = (ip2long($allowed_ip_ip) &#38; ip2long($allowed_ip_mask)) + 1;&#13;&#10; $end = (ip2long($allowed_ip_ip) | (~ ip2long($allowed_ip_mask))) + 1;&#13;&#10; $ip = ip2long($ip);&#13;&#10; return ($ip &#62;= $begin &#38;&#38; $ip &#60;= $end);&#13;&#10; }&#13;&#10;&#13;&#10; private function _sub_checker_section($allowed_ip, $ip) {&#13;&#10; list($begin, $end) = explode(&#39;-&#39;, $allowed_ip);&#13;&#10; $begin = ip2long($begin);&#13;&#10; $end = ip2long($end);&#13;&#10; $ip = ip2long($ip);&#13;&#10; return ($ip &#62;= $begin &#38;&#38; $ip &#60;= $end);&#13;&#10; }&#13;&#10;&#13;&#10; private function _sub_checker_CIDR($CIDR, $IP) {&#13;&#10; list ($net, $mask) = explode(&#39;/&#39;, $CIDR);&#13;&#10; return ( ip2long($IP) &#38; ~((1 &#60;&#60; (32 - $mask)) - 1) ) == ip2long($net);&#13;&#10; }&#13;&#10;&#13;&#10;}&#13;&#10;?&#62;&#13;&#10;&#13;&#10;For me this code works great, so I wanna thank to all You guys!!! --was-- Hi, based on examples above I have mixed class IPFilter with netMatch function wich gives me complete class for IP4 check including CIDR IP format: class IP4Filter { private static $_IP_TYPE_SINGLE = 'single'; private static $_IP_TYPE_WILDCARD = 'wildcard'; private static $_IP_TYPE_MASK = 'mask'; private static $_IP_TYPE_CIDR = 'CIDR'; private static $_IP_TYPE_SECTION = 'section'; private $_allowed_ips = array(); public function __construct($allowed_ips) { $this->_allowed_ips = $allowed_ips; } public function check($ip, $allowed_ips = null) { $allowed_ips = $allowed_ips ? $allowed_ips : $this->_allowed_ips; foreach ($allowed_ips as $allowed_ip) { $type = $this->_judge_ip_type($allowed_ip); $sub_rst = call_user_func(array($this, '_sub_checker_' . $type), $allowed_ip, $ip); if ($sub_rst) { return true; } } return false; } private function _judge_ip_type($ip) { if (strpos($ip, '*')) { return self :: $_IP_TYPE_WILDCARD; } if (strpos($ip, '/')) { $tmp = explode('/', $ip); if (strpos($tmp[1], '-')) { return self :: $_IP_TYPE_MASK; } else { return self :: $_IP_TYPE_CIDR; } } if (strpos($ip, '-')) { return self :: $_IP_TYPE_SECTION; } if (ip2long($ip)) { return self :: $_IP_TYPE_SINGLE; } return false; } private function _sub_checker_single($allowed_ip, $ip) { return (ip2long($allowed_ip) == ip2long($ip)); } private function _sub_checker_wildcard($allowed_ip, $ip) { $allowed_ip_arr = explode('.', $allowed_ip); $ip_arr = explode('.', $ip); for ($i = 0; $i < count($allowed_ip_arr); $i++) { if ($allowed_ip_arr[$i] == '*') { return true; } else { if (false == ($allowed_ip_arr[$i] == $ip_arr[$i])) { return false; } } } } private function _sub_checker_mask($allowed_ip, $ip) { list($allowed_ip_ip, $allowed_ip_mask) = explode('/', $allowed_ip); $begin = (ip2long($allowed_ip_ip) & ip2long($allowed_ip_mask)) + 1; $end = (ip2long($allowed_ip_ip) | (~ ip2long($allowed_ip_mask))) + 1; $ip = ip2long($ip); return ($ip >= $begin && $ip <= $end); } private function _sub_checker_section($allowed_ip, $ip) { list($begin, $end) = explode('-', $allowed_ip); $begin = ip2long($begin); $end = ip2long($end); $ip = ip2long($ip); return ($ip >= $begin && $ip <= $end); } private function _sub_checker_CIDR($CIDR, $IP) { list ($net, $mask) = explode('/', $CIDR); return ( ip2long($IP) & ~((1 << (32 - $mask)) - 1) ) == ip2long($net); } } For me this code works great, so I wanna thank to all You guys!!! http://php.net/manual/en/function.ip2long.php

« previous php.notes (#188251) next »