Advertisement
tolikpunkoff

specdiap.php

Jul 15th, 2018
594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.10 KB | None | 0 0
  1. <?php
  2.     ini_set('error_reporting', E_ALL);
  3.     ini_set('display_errors', 1);
  4.     ini_set('display_startup_errors', 1);
  5.    
  6.     header('Content-type: text/plain; charset=utf8');
  7.    
  8.     //регулярное выражение для IP
  9.     $ip_pattern="#(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)#";
  10.  
  11.     $spec_list = array(                                    
  12.                     array ("0.0.0.0","0.255.255.255", "Current network"),
  13.                     array ("255.255.255.255","255.255.255.255", "Broadcast"),
  14.                     array ("255.0.0.0","255.255.255.255", "Reserved by the IETF, broadcast"),
  15.                     array ("10.0.0.0","10.255.255.255", "Private network"),
  16.                     array ("100.64.0.0","100.127.255.255", "Shared Address Space"),
  17.                     array ("127.0.0.0","127.255.255.255", "Loopback"),
  18.                     array ("169.254.0.0","169.254.255.255", "Link-local"),
  19.                     array ("172.16.0.0","172.31.255.255", "Private network"),
  20.                     array ("192.0.0.0","192.0.0.7", "DS-Lite"),
  21.                     array ("192.0.0.170","192.0.0.170", "NAT64"),
  22.                     array ("192.0.0.171","192.0.0.171", "DNS64"),
  23.                     array ("192.0.2.0","192.0.2.255", "Documentation example"),
  24.                     array ("192.0.0.0","192.0.0.255", "Reserved by the IETF"),                                     
  25.                     array ("192.88.99.0","192.88.99.255", "Anycast"),
  26.                     array ("192.88.99.1","192.88.99.1", "IPv6 to IPv4 Incapsulation"),
  27.                     array ("192.168.0.0","192.168.255.255", "Private network"),
  28.                     array ("198.51.100.0","198.51.100.255", "Documentation example"),
  29.                     array ("198.18.0.0","198.19.255.255", "Test IP"),
  30.                     array ("203.0.113.0","203.0.113.255", "Documentation example"),
  31.                     array ("224.0.0.0","224.255.255.255", "Multicast"),
  32.                     array ("240.0.0.0","240.255.255.255", "Future reserved")                   
  33.                     );
  34.  
  35.     function isip($ip_str) //соответствие данных формату IP
  36.     {  
  37.         global $ip_pattern;
  38.         $ret=FALSE;
  39.         if (preg_match($ip_pattern,$ip_str))
  40.         {
  41.             $ret=TRUE;
  42.         }
  43.         return $ret;
  44.     }
  45.     function chkdiapip ($user_ip, $ip_from, $ip_to) //попадает ли ip в нужный диапазон
  46.     {
  47.         return ( ip2long($user_ip)>=ip2long($ip_from) && ip2long($user_ip)<=ip2long($ip_to) );
  48.     }
  49.    
  50.     function get_spec_diap ($user_ip, $listspec)
  51.     {
  52.         for ($i=0;$i<sizeof($listspec);$i++)
  53.         {
  54.             $item = $listspec[$i];
  55.             if (chkdiapip($user_ip, $item[0], $item[1]))
  56.             {
  57.                 return $item[0]."\t".$item[1]."\t".$item[2]."\t\n";
  58.             }
  59.         }
  60.        
  61.         return -1;
  62.     }
  63.    
  64.     function printspecdiap ($listspec)
  65.     {
  66.         for ($i=0;$i<sizeof($listspec);$i++)
  67.         {
  68.             $item = $listspec[$i];
  69.             echo $item[0]."\t\t".$item[1]."\t\t\t".$item[2], "\t\t\t\n";
  70.         }
  71.     }
  72.    
  73.     //---------------------------------------------------------------------------
  74.     if (isset($_GET['ip']))
  75.     {
  76.         $ip = $_GET['ip'];
  77.         if (!isip($ip))
  78.         {
  79.             echo "No correct IP address";
  80.             die();
  81.         }
  82.         $retspec = get_spec_diap ($ip, $spec_list);
  83.         if ($retspec == -1)
  84.         {
  85.             echo "IP ".$ip." not in special diapason.";
  86.         }
  87.         else
  88.         {
  89.             echo "IP ".$ip." ".$retspec;
  90.         }
  91.     }
  92.     else
  93.     {
  94.         echo "Test script for detecting IPv4 special diapasons\n";
  95.         echo "Use specdiap.php?ip=<ip address>\n\n";
  96.        
  97.         echo "Diapasons list:\n";
  98.         printspecdiap($spec_list);
  99.     }
  100.  
  101. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement