Exemple #1
0
/**
 * ipCalc calculations
 */
function calculateIpCalcResult($cidr)
{
    /* first verify address type */
    $type = IdentifyAddress($cidr);
    /* IPv4 */
    if ($type == "IPv4") {
        $net = Net_IPv4::parseAddress($cidr);
        //set ip address type
        $out['Type'] = 'IPv4';
        //calculate network details
        $out['IP address'] = $net->ip;
        // 192.168.0.50
        $out['Network'] = $net->network;
        // 192.168.0.0
        $out['Broadcast'] = $net->broadcast;
        // 192.168.255.255
        $out['Subnet bitmask'] = $net->bitmask;
        // 16
        $out['Subnet netmask'] = $net->netmask;
        // 255.255.0.0
        $out['Subnet wildcard'] = long2ip(~ip2long($net->netmask));
        //0.0.255.255
        //calculate min/max IP address
        $out['Min host IP'] = long2ip(ip2long($out['Network']) + 1);
        $out['Max host IP'] = long2ip(ip2long($out['Broadcast']) - 1);
        $out['Number of hosts'] = ip2long($out['Broadcast']) - ip2long($out['Min host IP']);
        //subnet class
        $out['Subnet Class'] = checkIpv4AddressType($out['Network'], $out['Broadcast']);
        //if IP == subnet clear the Host fields
        if ($out['IP address'] == $out['Network']) {
            $out['IP address'] = "/";
        }
    } else {
        //set ip address type
        $out['Type'] = 'IPv6';
        //calculate network details
        /*         $out['Host address']              = Net_IPv6::removeNetmaskSpec ( $cidr );  */
        $out['Host address'] = $cidr;
        $out['Host address'] = Net_IPv6::compress($out['Host address'], 1);
        $out['Host address (uncompressed)'] = Net_IPv6::uncompress($out['Host address']);
        $mask = Net_IPv6::getNetmaskSpec($cidr);
        $subnet = Net_IPv6::getNetmask($cidr);
        $out['Subnet prefix'] = Net_IPv6::compress($subnet) . '/' . $mask;
        $out['Prefix length'] = Net_IPv6::getNetmaskSpec($cidr);
        // get reverse DNS entries
        $out['Host Reverse DNS'] = calculateReverseDNS6($out['Host address (uncompressed)']);
        $out['Subnet Reverse DNS'] = calculateReverseDNS6($subnet, $mask);
        //if IP == subnet clear the Host fields and Host Reverse DNS
        if ($out['Host address'] == $out['Subnet prefix']) {
            $out['Host address'] = '/';
            $out['Host address (uncompressed)'] = '/';
            unset($out['Host Reverse DNS']);
        }
        //min / max hosts
        $maxIp = gmp_strval(gmp_add(gmp_sub(gmp_pow(2, 128 - $mask), 1), ip2long6($subnet)));
        $out['Min host IP'] = long2ip6(gmp_strval(gmp_add(ip2long6($subnet), 1)));
        $out['Max host IP'] = long2ip6($maxIp);
        $out['Number of hosts'] = MaxHosts($mask, 1);
        //address type
        $out['Address type'] = Net_IPv6::getAddressType($cidr);
        $out['Address type'] = checkIpv6AddressType($out['Address type']);
    }
    /* return results */
    return $out;
}
 function ipv6_in_range($ip, $range_ip)
 {
     $pieces = explode("/", $range_ip, 2);
     $left_piece = $pieces[0];
     $right_piece = $pieces[1];
     // Extract out the main IP pieces
     $ip_pieces = explode("::", $left_piece, 2);
     $main_ip_piece = $ip_pieces[0];
     $last_ip_piece = $ip_pieces[1];
     // Pad out the shorthand entries.
     $main_ip_pieces = explode(":", $main_ip_piece);
     foreach ($main_ip_pieces as $key => $val) {
         $main_ip_pieces[$key] = str_pad($main_ip_pieces[$key], 4, "0", STR_PAD_LEFT);
     }
     // Create the first and last pieces that will denote the IPV6 range.
     $first = $main_ip_pieces;
     $last = $main_ip_pieces;
     // Check to see if the last IP block (part after ::) is set
     $last_piece = "";
     $size = count($main_ip_pieces);
     if (trim($last_ip_piece) != "") {
         $last_piece = str_pad($last_ip_piece, 4, "0", STR_PAD_LEFT);
         // Build the full form of the IPV6 address considering the last IP block set
         for ($i = $size; $i < 7; $i++) {
             $first[$i] = "0000";
             $last[$i] = "ffff";
         }
         $main_ip_pieces[7] = $last_piece;
     } else {
         // Build the full form of the IPV6 address
         for ($i = $size; $i < 8; $i++) {
             $first[$i] = "0000";
             $last[$i] = "ffff";
         }
     }
     // Rebuild the final long form IPV6 address
     $first = ip2long6(implode(":", $first));
     $last = ip2long6(implode(":", $last));
     $in_range = $ip >= $first && $ip <= $last;
     return $in_range;
 }
Exemple #3
0
 /**
  * Transform IP address from dotted to decimal (10.2.1.0 -> 167903488)
  *
  * @access public
  * @param mixed $address
  * @return void
  */
 public function transform_to_decimal($address)
 {
     if ($this->identify_address($address) == "IPv4") {
         return sprintf("%u", ip2long($address));
     } else {
         return ip2long6($address);
     }
 }
/**
 * Transform IP address from dotted to decimal (10.2.1.0 -> 167903488)
 */
function Transform2decimal($ip)
{
    if (IdentifyAddress($ip) == "IPv4") {
        return sprintf("%u", ip2long($ip));
    } else {
        return ip2long6($ip);
    }
}
function userlogin()
{
    $ip = getip();
    // If there's no IP a script is being ran from CLI. Any checks here will fail, skip all.
    if ($ip == "") {
        return;
    }
    global $CURUSER;
    unset($GLOBALS["CURUSER"]);
    //Check IP bans
    if (is_ipv6($ip)) {
        $nip = ip2long6($ip);
    } else {
        $nip = ip2long($ip);
    }
    $res = SQL_Query_exec("SELECT * FROM bans");
    while ($row = mysql_fetch_assoc($res)) {
        $banned = false;
        if (is_ipv6($row["first"]) && is_ipv6($row["last"]) && is_ipv6($ip)) {
            $row["first"] = ip2long6($row["first"]);
            $row["last"] = ip2long6($row["last"]);
            $banned = bccomp($row["first"], $nip) != -1 && bccomp($row["last"], $nip) != -1;
        } else {
            $row["first"] = ip2long($row["first"]);
            $row["last"] = ip2long($row["last"]);
            $banned = $nip >= $row["first"] && $nip <= $row["last"];
        }
        if ($banned) {
            header("HTTP/1.0 403 Forbidden");
            echo "<html><head><title>Forbidden</title></head><body><h1>Forbidden</h1>Unauthorized IP address.<br />" . "Reason for banning: {$row['comment']}</body></html>";
            die;
        }
    }
    //Check The Cookie and get CURUSER details
    if (strlen($_COOKIE["pass"]) != 40 || !is_numeric($_COOKIE["uid"])) {
        logoutcookie();
        return;
    }
    //Get User Details And Permissions
    $res = SQL_Query_exec("SELECT * FROM users INNER JOIN groups ON users.class=groups.group_id WHERE id={$_COOKIE['uid']} AND users.enabled='yes' AND users.status = 'confirmed'");
    $row = mysql_fetch_assoc($res);
    if (!$row || sha1($row["id"] . $row["secret"] . $row["password"] . $ip . $row["secret"]) != $_COOKIE["pass"]) {
        logoutcookie();
        return;
    }
    $where = where($_SERVER["SCRIPT_FILENAME"], $row["id"], 0);
    SQL_Query_exec("UPDATE users SET last_access='" . get_date_time() . "', ip=" . sqlesc($ip) . ", page=" . sqlesc($where) . " WHERE id=" . $row["id"]);
    $GLOBALS["CURUSER"] = $row;
    unset($row);
}
Exemple #6
0
 /**
  * Calculates IPv6 from cidr
  *
  * @access private
  * @param mixed $cidr
  * @return void
  */
 private function calculate_IPv6_calc_results($cidr)
 {
     # initialize subnets Class
     $Subnets = new Subnets($this->Database);
     # Initialize PEAR NET object
     $this->initialize_pear_net_IPv6();
     # set ip address type
     $out['Type'] = 'IPv6';
     # calculate network details
     $out['Host address'] = $cidr;
     $out['Host address'] = $this->Net_IPv6->compress($out['Host address'], 1);
     $out['Host address (uncompressed)'] = $this->Net_IPv6->uncompress($out['Host address']);
     $mask = $this->Net_IPv6->getNetmaskSpec($cidr);
     $subnet = $this->Net_IPv6->getNetmask($cidr);
     $out['Subnet prefix'] = $this->Net_IPv6->compress($subnet) . '/' . $mask;
     $out['Prefix length'] = $this->Net_IPv6->getNetmaskSpec($cidr);
     # get reverse DNS entries
     $out['Host Reverse DNS'] = $this->reverse_IPv6($out['Host address (uncompressed)']);
     $out['Subnet Reverse DNS'] = $this->reverse_IPv6($subnet, $mask);
     # if IP == subnet clear the Host fields and Host Reverse DNS
     if ($out['Host address'] == $out['Subnet prefix']) {
         $out['Host address'] = '/';
         $out['Host address (uncompressed)'] = '/';
         unset($out['Host Reverse DNS']);
     }
     # /min / max hosts
     $maxIp = gmp_strval(gmp_add(gmp_pow(2, 128 - $mask), ip2long6($subnet)));
     $out['Min host IP'] = $subnet;
     $out['Max host IP'] = long2ip6($maxIp);
     $out['Number of hosts'] = $Subnets->get_max_hosts($mask, "IPv6");
     # set address type
     $out['Address type'] = $this->get_ipv6_address_type($cidr);
     # result
     return $out;
 }