Пример #1
0
/**
 * Converts IPv6s to numbers.  This makes ban checks much easier.
 *
 * @param string $ip ip address to be converted
 * @return int[] array
 */
function convertIPv6toInts($ip)
{
    static $expanded = array();
    // Check if we have done this already.
    if (isset($expanded[$ip])) {
        return $expanded[$ip];
    }
    // Expand the IP out.
    $expanded_ip = explode(':', expandIPv6($ip));
    $new_ip = array();
    foreach ($expanded_ip as $int) {
        $new_ip[] = hexdec($int);
    }
    // Save this incase of repeated use.
    $expanded[$ip] = $new_ip;
    return $expanded[$ip];
}
Пример #2
0
/**
 * Convert a single IP to a ranged IP.
 *
 * - internal function used to convert a user-readable format to a format suitable for the database.
 *
 * @param string $fullip
 * @return array|string 'unknown' if the ip in the input was '255.255.255.255'
 */
function ip2range($fullip)
{
    // If its IPv6, validate it first.
    if (isValidIPv6($fullip) !== false) {
        $ip_parts = explode(':', expandIPv6($fullip, false));
        $ip_array = array();
        if (count($ip_parts) != 8) {
            return array();
        }
        for ($i = 0; $i < 8; $i++) {
            if ($ip_parts[$i] == '*') {
                $ip_array[$i] = array('low' => '0', 'high' => hexdec('ffff'));
            } elseif (preg_match('/^([0-9A-Fa-f]{1,4})\\-([0-9A-Fa-f]{1,4})$/', $ip_parts[$i], $range) == 1) {
                $ip_array[$i] = array('low' => hexdec($range[1]), 'high' => hexdec($range[2]));
            } elseif (is_numeric(hexdec($ip_parts[$i]))) {
                $ip_array[$i] = array('low' => hexdec($ip_parts[$i]), 'high' => hexdec($ip_parts[$i]));
            }
        }
        return $ip_array;
    }
    // Pretend that 'unknown' is 255.255.255.255. (since that can't be an IP anyway.)
    if ($fullip == 'unknown') {
        $fullip = '255.255.255.255';
    }
    $ip_parts = explode('.', $fullip);
    $ip_array = array();
    if (count($ip_parts) != 4) {
        return array();
    }
    for ($i = 0; $i < 4; $i++) {
        if ($ip_parts[$i] == '*') {
            $ip_array[$i] = array('low' => '0', 'high' => '255');
        } elseif (preg_match('/^(\\d{1,3})\\-(\\d{1,3})$/', $ip_parts[$i], $range) == 1) {
            $ip_array[$i] = array('low' => $range[1], 'high' => $range[2]);
        } elseif (is_numeric($ip_parts[$i])) {
            $ip_array[$i] = array('low' => $ip_parts[$i], 'high' => $ip_parts[$i]);
        }
    }
    // Makes it simpiler to work with.
    $ip_array[4] = array('low' => 0, 'high' => 0);
    $ip_array[5] = array('low' => 0, 'high' => 0);
    $ip_array[6] = array('low' => 0, 'high' => 0);
    $ip_array[7] = array('low' => 0, 'high' => 0);
    return $ip_array;
}