Advertisement
Blade83

IPv4 Filter

Jun 1st, 2014
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.81 KB | None | 0 0
  1. <?php
  2.  
  3. /*----------------------------------------------------------------------------------
  4. ** File:                ipfilter.class.php
  5. ** Description:         PHP class for test matching an IP with a spezified filter
  6. ** Version:             1.0
  7. ** Author:              $ Blade83
  8. ** Email:               johannes_kraemer@gmx.de
  9. ** Date:                01.06.2014
  10. **----------------------------------------------------------------------------------
  11. ** COPYRIGHT (c) 2008-2014 JOHANNES KRÄMER
  12. ** The source code included in this file is free software; you can
  13. ** redistribute it and/or modify it under the terms of the GNU General Public
  14. ** License as published by the Free Software Foundation. This license can be
  15. ** read at:  http://www.opensource.org/licenses/gpl-license.php
  16. */
  17.  
  18. class IPFilter
  19. {
  20.     /*
  21.      * Define IP Filter types
  22.     */
  23.     private static
  24.         $_IP_TYPE_SINGLE        = 'single',
  25.         $_IP_TYPE_WILDCARD      = 'wildcard',
  26.         $_IP_TYPE_MASK          = 'mask',
  27.         $_IP_TYPE_RANGE         = 'range';
  28.  
  29.     private
  30.         $_allowed_ips           = array(),
  31.         $_ip_type               = null,
  32.         $_ip                    = null;
  33.  
  34.     /*
  35.      * Constructor of IPFilter class
  36.      * @RETURN void
  37.     */
  38.     public function __construct()
  39.     {
  40.         if(!function_exists('ip2long'))           die('Function ip2long() not found!');
  41.         if(!function_exists('filter_var'))        die('Function filter_var() not found!');
  42.         if(!function_exists('call_user_func'))    die('Function call_user_func() not found!');
  43.     }
  44.  
  45.     /*
  46.      * Set an IP Filter (single|wildcard|mask|range)
  47.      * @PARAM1 array $allowed_ips
  48.      * @RETURN void
  49.     */
  50.     public function setFilter($allowed_ips)
  51.     {
  52.         $this->_allowed_ips = $allowed_ips;
  53.     }
  54.  
  55.     /*
  56.      * Validate an IP
  57.      * @PARAM1 string $ip
  58.      * @RETURN bool
  59.     */
  60.     public function checkIP($ip)
  61.     {
  62.         if (count($this->_allowed_ips)==0)
  63.         {
  64.             die("IPFilter: No Filter available!");
  65.         }
  66.         $this->_ip = $ip;
  67.         if(filter_var($this->_ip, FILTER_VALIDATE_IP))
  68.         {
  69.             foreach($this->_allowed_ips as $allowed_ip)
  70.             {
  71.                 if (@call_user_func(array($this,'_sub_checker_'.$this->_judge_ip_type($allowed_ip)), $allowed_ip, $this->_ip))
  72.                 {
  73.                     return true;
  74.                 }
  75.             }
  76.             return false;
  77.         }
  78.         else
  79.         {
  80.             die('IP('.$this->_ip.') Syntax error');
  81.         }
  82.     }
  83.  
  84.     /*
  85.      * Getter Method for checked IP
  86.      * @RETURN string ip
  87.     */
  88.     public function getCheckedIP()
  89.     {
  90.         return $this->_ip;
  91.     }
  92.  
  93.     /*
  94.      * Getter Method for matching Filter Type of the checked IP
  95.      * @RETURN string ip
  96.     */
  97.     public function getFilterTypeFromCheckedIP()
  98.     {
  99.         return $this->_ip_type;
  100.     }
  101.  
  102.     /*
  103.      * @PARAM1 string $ip
  104.      * @RETURN mixed [bool or string] IP Type (single|wildcard|mask|range)
  105.     */
  106.     private function _judge_ip_type($ip)
  107.     {
  108.         if (strpos($ip, '*'))
  109.         {
  110.             $this->_ip_type = 'wildard';
  111.             return self::$_IP_TYPE_WILDCARD;
  112.         }
  113.         if (strpos($ip, '/'))
  114.         {
  115.             $this->_ip_type = 'mask';
  116.             return self::$_IP_TYPE_MASK;
  117.         }
  118.         if (strpos($ip, '-'))
  119.         {
  120.             $this->_ip_type = 'range';
  121.             return self::$_IP_TYPE_RANGE;
  122.         }
  123.         if (ip2long($ip))
  124.         {
  125.             $this->_ip_type = 'single';
  126.             return self::$_IP_TYPE_SINGLE;
  127.         }
  128.         $this->_ip_type = null;
  129.         return false;
  130.     }
  131.  
  132.     /*
  133.      * @PARAM1 string $allowed_ip
  134.      * @PARAM2 string $ip
  135.      * @RETURN bool
  136.     */
  137.     private function _sub_checker_single($allowed_ip, $ip)
  138.     {
  139.         return (ip2long($allowed_ip) == ip2long($ip));
  140.     }
  141.  
  142.     /*
  143.      * Checks if is an IP in a Wildcard range
  144.      * @PARAM1 string $allowed_ip
  145.      * @PARAM2 string $ip
  146.      * @RETURN bool
  147.     */
  148.     private function _sub_checker_wildcard($allowed_ip, $ip)
  149.     {
  150.         $allowed_ip_arr = explode('.', $allowed_ip);
  151.         $ip_arr = explode('.', $ip);
  152.         for($i=0; $i<count($allowed_ip_arr); $i++)
  153.         {
  154.             if ($allowed_ip_arr[$i]=='*')
  155.             {
  156.                 return true;
  157.             }
  158.             else
  159.             {
  160.                 if (false==($allowed_ip_arr[$i]==$ip_arr[$i]))
  161.                 {
  162.                     return false;
  163.                 }
  164.             }
  165.         }
  166.     }
  167.  
  168.     /*
  169.      * Checks if is an IP in a Subnetmask
  170.      * @PARAM1 string $allowed_ip
  171.      * @PARAM2 string $ip
  172.      * @RETURN bool
  173.     */
  174.     private function _sub_checker_mask($allowed_ip, $ip)
  175.     {
  176.         list($allowed_ip_ip, $allowed_ip_mask) = explode('/', $allowed_ip);
  177.         $begin = (ip2long($allowed_ip_ip) & ip2long($allowed_ip_mask)) + 1;
  178.         $end = (ip2long($allowed_ip_ip) | (~ip2long($allowed_ip_mask))) + 1;
  179.         $ip = ip2long($ip);
  180.         return ($ip>=$begin && $ip<=$end);
  181.     }
  182.  
  183.     /*
  184.      * Checks if is an IP in a IP Range
  185.      * @PARAM1 string $allowed_ip
  186.      * @PARAM2 string $ip
  187.      * @RETURN bool
  188.     */
  189.     private function _sub_checker_range($allowed_ip, $ip)
  190.     {
  191.         list($begin, $end) = explode('-', $allowed_ip);
  192.         $ip = ip2long($ip);
  193.         return ($ip>=ip2long(trim($begin)) && $ip<=ip2long(trim($end)));
  194.     }
  195. }
  196.  
  197.  
  198. /////////////
  199. // EXAMPLE //
  200. /////////////
  201.  
  202. $IPFilter = new IPFilter();
  203. $IPFilter->setFilter(
  204.     array(
  205.         '127.0.0.1',                        # single IP
  206.        '127.0.0.2',                        # single IP
  207.        '171.0.0.*',                        # Wildcard
  208.        '172.0.*.*',                        # Wildcard
  209.        '173.*.*.*',                        # Wildcard
  210.        #'255.255.255.0/255.255.255.255',    # Subnet Mask
  211.        '192.0.0.1 - 192.188.0.1',          # IP Range
  212.    )
  213. );
  214.  
  215.  
  216. if ( $IPFilter->checkIP('127.0.0.1')===true )
  217. {
  218.     echo 'IP('.$IPFilter->getCheckedIP().') is valid with Filter. Filtertype: '.$IPFilter->getFilterTypeFromCheckedIP().'<br>';
  219. }
  220. else
  221. {
  222.     echo 'IP('.$IPFilter->getCheckedIP().') is not valid with Filter<br>';
  223. }
  224.  
  225. if ( $IPFilter->checkIP('127.0.0.2')===true )
  226. {
  227.     echo 'IP('.$IPFilter->getCheckedIP().') is valid with Filter. Filtertype: '.$IPFilter->getFilterTypeFromCheckedIP().'<br>';
  228. }
  229. else
  230. {
  231.     echo 'IP('.$IPFilter->getCheckedIP().') is not valid with Filter<br>';
  232. }
  233.  
  234. if ( $IPFilter->checkIP('171.0.0.125')===true )
  235. {
  236.     echo 'IP('.$IPFilter->getCheckedIP().') is valid with Filter. Filtertype: '.$IPFilter->getFilterTypeFromCheckedIP().'<br>';
  237. }
  238. else
  239. {
  240.     echo 'IP('.$IPFilter->getCheckedIP().') is not valid with Filter<br>';
  241. }
  242.  
  243. if ( $IPFilter->checkIP('172.0.55.11')===true )
  244. {
  245.     echo 'IP('.$IPFilter->getCheckedIP().') is valid with Filter. Filtertype: '.$IPFilter->getFilterTypeFromCheckedIP().'<br>';
  246. }
  247. else
  248. {
  249.     echo 'IP('.$IPFilter->getCheckedIP().') is not valid with Filter<br>';
  250. }
  251.  
  252. if ( $IPFilter->checkIP('173.77.99.88')===true )
  253. {
  254.     echo 'IP('.$IPFilter->getCheckedIP().') is valid with Filter. Filtertype: '.$IPFilter->getFilterTypeFromCheckedIP().'<br>';
  255. }
  256. else
  257. {
  258.     echo 'IP('.$IPFilter->getCheckedIP().') is not valid with Filter<br>';
  259. }
  260.  
  261. if ( $IPFilter->checkIP('192.168.1.1')===true )
  262. {
  263.     echo 'IP('.$IPFilter->getCheckedIP().') is valid with Filter. Filtertype: '.$IPFilter->getFilterTypeFromCheckedIP().'<br>';
  264. }
  265. else
  266. {
  267.     echo 'IP('.$IPFilter->getCheckedIP().') is not valid with Filter<br>';
  268. }
  269.  
  270. if ( $IPFilter->checkIP('192.168.1.260')===true )
  271. {
  272.     echo 'IP('.$IPFilter->getCheckedIP().') is valid with Filter. Filtertype: '.$IPFilter->getFilterTypeFromCheckedIP().'<br>';
  273. }
  274. else
  275. {
  276.     echo 'IP('.$IPFilter->getCheckedIP().') is not valid with Filter<br>';
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement