includes/functions.php
changeset 128 01955bf53f96
parent 125 fb31c951d3a2
child 129 0b5244001799
equal deleted inserted replaced
127:a2b20a832447 128:01955bf53f96
  2844      Size after compression:  $size_after bytes
  2844      Size after compression:  $size_after bytes
  2845      -->\n<html", $html);
  2845      -->\n<html", $html);
  2846   return $html;
  2846   return $html;
  2847 }
  2847 }
  2848 
  2848 
       
  2849 /**
       
  2850  * For an input range of numbers (like 25-256) returns an array filled with all numbers in the range, inclusive.
       
  2851  * @param string
       
  2852  * @return array
       
  2853  */
       
  2854 
       
  2855 function int_range($range)
       
  2856 {
       
  2857   if ( strval(intval($range)) == $range )
       
  2858     return $range;
       
  2859   if ( !preg_match('/^[0-9]+(-[0-9]+)?$/', $range) )
       
  2860     return false;
       
  2861   $ends = explode('-', $range);
       
  2862   if ( count($ends) != 2 )
       
  2863     return $range;
       
  2864   $ret = array();
       
  2865   if ( $ends[1] < $ends[0] )
       
  2866     $ends = array($ends[1], $ends[0]);
       
  2867   else if ( $ends[0] == $ends[1] )
       
  2868     return array($ends[0]);
       
  2869   for ( $i = $ends[0]; $i <= $ends[1]; $i++ )
       
  2870   {
       
  2871     $ret[] = $i;
       
  2872   }
       
  2873   return $ret;
       
  2874 }
       
  2875 
       
  2876 /**
       
  2877  * Parses a range or series of IP addresses, and returns the raw addresses. Only parses ranges in the last two octets to prevent DOSing.
       
  2878  * Syntax for ranges: x.x.x.x; x|y.x.x.x; x.x.x-z.x; x.x.x-z|p.q|y
       
  2879  * @param string IP address range string
       
  2880  * @return array
       
  2881  */
       
  2882 
       
  2883 function parse_ip_range($range)
       
  2884 {
       
  2885   $octets = explode('.', $range);
       
  2886   if ( count($octets) != 4 )
       
  2887     // invalid range
       
  2888     return $range;
       
  2889   $i = 0;
       
  2890   $possibilities = array( 0 => array(), 1 => array(), 2 => array(), 3 => array() );
       
  2891   foreach ( $octets as $octet )
       
  2892   {
       
  2893     $existing =& $possibilities[$i];
       
  2894     $inner = explode('|', $octet);
       
  2895     foreach ( $inner as $bit )
       
  2896     {
       
  2897       if ( $i >= 2 )
       
  2898       {
       
  2899         $bits = int_range($bit);
       
  2900         if ( $bits === false )
       
  2901           return false;
       
  2902         else if ( !is_array($bits) )
       
  2903           $existing[] = intval($bits);
       
  2904         else
       
  2905           $existing = array_merge($existing, $bits);
       
  2906       }
       
  2907       else
       
  2908       {
       
  2909         $bit = intval($bit);
       
  2910         $existing[] = $bit;
       
  2911       }
       
  2912     }
       
  2913     $existing = array_unique($existing);
       
  2914     $i++;
       
  2915   }
       
  2916   $ips = array();
       
  2917   
       
  2918   // The only way to combine all those possibilities. ;-)
       
  2919   foreach ( $possibilities[0] as $oc1 )
       
  2920     foreach ( $possibilities[1] as $oc2 )
       
  2921       foreach ( $possibilities[2] as $oc3 )
       
  2922         foreach ( $possibilities[3] as $oc4 )
       
  2923           $ips[] = "$oc1.$oc2.$oc3.$oc4";
       
  2924         
       
  2925   return $ips;
       
  2926 }
       
  2927 
  2849 //die('<pre>Original:  01010101010100101010100101010101011010'."\nProcessed: ".uncompress_bitfield(compress_bitfield('01010101010100101010100101010101011010')).'</pre>');
  2928 //die('<pre>Original:  01010101010100101010100101010101011010'."\nProcessed: ".uncompress_bitfield(compress_bitfield('01010101010100101010100101010101011010')).'</pre>');
  2850 
  2929 
  2851 ?>
  2930 ?>