if (!rejectIPrequest($request['requestId'], $request['adminComment'])) {
        print '<div class="alert alert alert-danger">' . _('Cannot update request') . '!</div>';
        updateLogTable('Cannot reject IP request', 'Cannot reject IP request for request id ' . $request['requestId'] . '!', 2);
    } else {
        print '<div class="alert alert-success">' . _('Request has beed rejected') . '!</div>';
        updateLogTable('Request has beed rejected!', 'IP request id ' . $request['requestId'] . ' (' . $request['ip_addr'] . ') has been rejected!', 1);
        /* transform IP address */
        $request['ip_addr'] = Transform2decimal($request['ip_addr']);
    }
} else {
    /* first check all the variables */
    $subnet = getSubnetDetailsById($request['subnetId']);
    $subnet['subnet'] = Transform2long($subnet['subnet']);
    $subnet = $subnet['subnet'] . "/" . $subnet['mask'];
    /* verify IP address and subnet */
    $validIP = VerifyIpAddress($request['ip_addr'], $subnet);
    if (!empty($validIP)) {
        die('<div class="alert alert alert-danger">' . $validIP . '</div>');
    }
    /* verify that it is not yet used */
    if (checkDuplicate($request['ip_addr'], $subnet)) {
        die('<div class="alert alert alert-danger">' . _('IP address') . ' ' . $request['ip_addr'] . ' ' . _('already in use') . '!<br>* ' . _('First available IP address is') . ' <b>' . Transform2long(getFirstAvailableIPAddress($request['subnet'])) . '</a>!</div>');
    }
    /* verify that it is not yet used */
    if (checkDuplicate($request['ip_addr'], $request['subnetId'])) {
        die('<div class="alert alert alert-danger">' . _('IP address') . ' ' . $request['ip_addr'] . ' ' . _('already in use') . '!<br>* ' . _('First available IP address is') . ' <b>' . Transform2long(getFirstAvailableIPAddress($request['subnetId'])) . '</a>!</div>');
    }
    /* transform IP address */
    $request['ip_addr'] = Transform2decimal($request['ip_addr']);
    if (!acceptIPrequest($request)) {
        die('<div class="alert alert alert-danger">' . _('Cannot confirm IP address') . '!</div>');
Example #2
0
/**
 * CSV import IP address
 *
 *		provided input is CSV line!
 */
function importCSVline($line, $subnetId)
{
    global $database;
    /* get subnet details by Id */
    $subnetDetails = getSubnetDetailsById($subnetId);
    $subnet = Transform2long($subnetDetails['subnet']) . "/" . $subnetDetails['mask'];
    /* verify! */
    $err = VerifyIpAddress($line[0], $subnet);
    if ($err) {
        return _('Wrong IP address') . ' - ' . $err . ' - ' . $line[0];
    }
    /* check for duplicates */
    if (checkDuplicate($line[0], $subnetId)) {
        return _('IP address already exists') . ' - ' . $line[0];
    }
    /* get custom fields */
    $myFields = getCustomFields('ipaddresses');
    if (sizeof($myFields) > 0) {
        $import['fieldName'] = "";
        $import['fieldValue'] = "";
        $m = 9;
        foreach ($myFields as $field) {
            //escape chars
            $line[$m] = mysqli_real_escape_string($database, $line[$m]);
            $import['fieldName'] .= ",`{$field['name']}`";
            $import['fieldValue'] .= ",'{$line[$m]}'";
            $m++;
        }
    }
    /* escape chars */
    foreach ($line as $k => $l) {
        $line[$k] = mysqli_real_escape_string($database, $l);
    }
    /* all ok, set query */
    $query = "insert into ipaddresses ";
    $query .= "(`subnetId`, `ip_addr`, `state`, `description`, `dns_name`, `mac`, `owner`, `switch`, `port`, `note` {$import['fieldName']} ) ";
    $query .= "values ";
    $query .= "('{$subnetId}','" . Transform2decimal($line[0]) . "', '{$line['1']}','{$line['2']}','{$line['3']}','{$line['4']}','{$line['5']}','{$line['6']}','{$line['7']}','{$line['8']}' {$import['fieldValue']});";
    /*
    	print "<pre>";
    	print_r($line);
    	die('alert alert-danger');
    */
    /* set log details */
    $log = prepareLogFromArray($line);
    /* execute */
    try {
        $database->executeQuery($query);
    } catch (Exception $e) {
        $error = $e->getMessage();
    }
    if (!isset($e)) {
        updateLogTable('CSV import of IP address ' . $line[1] . ' succeeded', $log, 0);
        return true;
    } else {
        updateLogTable('CSV import of IP address ' . $line[1] . ' failed', $log, 2);
        return $error;
    }
}
/* verify post */
CheckReferrer();
# get old subnet details
$subnetOld = getSubnetDetailsById($_POST['subnetId']);
# get all site settings
$settings = getAllSettings();
/* get section details */
$section = getSectionDetailsById($subnetOld['sectionId']);
/* 
 * now we need to check each host against new subnet
 */
$ipaddresses = getIpAddressesBySubnetIdSort($_POST['subnetId'], "ip_addr", "asc");
# get all IP addresses
foreach ($ipaddresses as $ip) {
    # check against new subnet
    $error = VerifyIpAddress(transform2long($ip['ip_addr']), transform2long($subnetOld['subnet']) . "/" . $_POST['newMask']);
    if (!$error) {
    } else {
        $errors[] = $error;
    }
}
/* ask must be > 8 */
if ($_POST['newMask'] < 8) {
    die('<div class="alert alert-danger">' . _('New mask must be at least /8') . '!</div>');
}
/* 
 * if strict mode is enabled check that is is still inside master subnet!
 */
if ($section['strictMode'] == 1) {
    if (!($overlap = verifySubnetNesting($subnetOld['masterSubnetId'], transform2long($subnetOld['subnet']) . "/" . $_POST['newMask'])) && $subnetOld['masterSubnetId'] != 0) {
        # get master details
 if (isset($_POST['unique'])) {
     if ($_POST['unique'] == "1" && strlen($_POST['dns_name']) > 0) {
         # check if unique
         if (!isHostUnique($_POST['dns_name'])) {
             die('<div class="alert alert-danger">' . _('Hostname is not unique') . '!</div>');
         }
     }
 }
 /* verify ip address */
 if ($ip['action'] == "move") {
     $subnet = getSubnetDetailsById($_REQUEST['newSubnet']);
     $subnet = transform2long($subnet['subnet']) . "/" . $subnet['mask'];
     $verify = VerifyIpAddress($ip['ip_addr'], $subnet, $nostrict);
     $ip['newSubnet'] = $_REQUEST['newSubnet'];
 } else {
     $verify = VerifyIpAddress($ip['ip_addr'], $ip['subnet'], $nostrict);
 }
 /* if errors are present print them, else execute query! */
 if ($verify) {
     die('<div class="alert alert-danger">' . _('Error') . ': ' . $verify . ' (' . $ip['ip_addr'] . ')</div>');
 } else {
     /* set update for update */
     $ip['type'] = "single";
     /* check for duplicate entry! needed only in case new IP address is added, otherwise the code is locked! */
     if ($ip['action'] == "add") {
         if (checkDuplicate($ip['ip_addr'], $ip['subnetId'])) {
             die('<div class="alert alert-danger">' . _('IP address') . ' ' . $ip['ip_addr'] . ' ' . _('already existing in database') . '!</div>');
         }
     }
     /* check for duplicate entry on edit! */
     if ($ip['action'] == "edit") {
Example #5
0
/**
 * CSV import IP address
 *
 *		provided input is CSV line!
 */
function importCSVline($line, $subnetId)
{
    /* array */
    $line = explode(",", $line);
    global $db;
    # get variables from config file
    $database = new database($db['host'], $db['user'], $db['pass'], $db['name']);
    /* get subnet details by Id */
    $subnetDetails = getSubnetDetailsById($subnetId);
    $subnet = Transform2long($subnetDetails['subnet']) . "/" . $subnetDetails['mask'];
    /* verify! */
    if (VerifyIpAddress($line[0], $subnet)) {
        return _('Wrong IP address') . ' - ' . $line[0];
    }
    /* check for duplicates */
    if (checkDuplicate($line[0], $subnetId)) {
        return _('IP address already exists') . ' - ' . $line[0];
    }
    /* reformat state */
    switch ($line[5]) {
        case "Active":
            $line[5] = "1";
            break;
        case "active":
            $line[5] = "1";
            break;
        case "Reserved":
            $line[5] = "2";
            break;
        case "reserved":
            $line[5] = "2";
            break;
        case "Offline":
            $line[5] = "0";
            break;
        case "offline":
            $line[5] = "0";
            break;
    }
    /* reformat switch! */
    $switch = getSwitchDetailsByHostname($line[7]);
    /* get custom fields */
    $myFields = getCustomIPaddrFields();
    if (sizeof($myFields) > 0) {
        $import['fieldName'] = "";
        $import['fieldValue'] = "";
        $m = 9;
        foreach ($myFields as $field) {
            $import['fieldName'] .= ",`{$field['name']}`";
            $import['fieldValue'] .= ",'{$line[$m]}'";
            $m++;
        }
    }
    /* all ok, set query */
    $query = "insert into ipaddresses ";
    $query .= "(`subnetId`, `ip_addr`, `state`, `description`, `dns_name`, `mac`, `owner`, `switch`, `port`, `note` {$import['fieldName']} ) ";
    $query .= "values ";
    $query .= "('{$subnetId}','" . Transform2decimal($line[0]) . "', '{$line['1']}','{$line['2']}','{$line['3']}','{$line['4']}','{$line['5']}','{$line['6']}','{$switch['id']}','{$line['8']}' {$import['fieldValue']});";
    /* set log details */
    $log = prepareLogFromArray($line);
    /* execute */
    try {
        $database->executeQuery($query);
    } catch (Exception $e) {
        $error = $e->getMessage();
    }
    if (!isset($e)) {
        updateLogTable('CSV import of IP address ' . $line[1] . ' succeeded', $log, 0);
        return true;
    } else {
        updateLogTable('CSV import of IP address ' . $line[1] . ' failed', $log, 2);
        return $error;
    }
}
# get all site settings
$settings = getAllSettings();
/* get section details */
$section = getSectionDetailsById($subnetOld['sectionId']);
/* 
 * now we need to check each host against new subnet
 */
$ipaddresses = getIpAddressesBySubnetIdSort($_POST['subnetId'], "ip_addr", "asc");
# get all IP addresses
# check if it possible
if (!($newSubnet = getSubnetNetworkAddress(transform2long($subnetOld['subnet']) . "/" . $_POST['newMask']))) {
    $errors[] = _("New subnet not possible") . "!";
}
foreach ($ipaddresses as $ip) {
    # check against new subnet
    $error = VerifyIpAddress(transform2long($ip['ip_addr']), $newSubnet);
    if (!$error) {
    } else {
        $errors[] = $error;
    }
}
# Check if new subnet size doesn't overlap
if ($section['strictMode'] == 1) {
    if ($overlap = verifyResizedSubnetOverlapping($subnetOld, $newSubnet)) {
        $errors[] = $overlap;
    }
}
/* ask must be > 8 */
if ($_POST['newMask'] < 8) {
    die('<div class="alert alert-danger">' . _('New mask must be at least /8') . '!</div>');
}