$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>');
        updateLogTable('Cannot confirm IP address!', 'Cannot accept IP request for request id ' . Transform2long($request['requestId']) . '!', 2);
    } else {
        print '<div class="alert alert-success">' . _('IP address request confirmed and added to database') . '!</div>';
        updateLogTable('IP address request confirmed and added to database!', 'IP request id ' . $request['requestId'] . ' (' . Transform2long($request['ip_addr']) . ') has been accepted!', 0);
    }
}
/* send email */
$subnet = getSubnetDetailsById($request['subnetId']);
$subnet['subnet'] = Transform2long($subnet['subnet']);
$subnet = $subnet['subnet'] . "/" . $subnet['mask'];
include_once 'manageRequestResultMail.php';
Beispiel #2
0
/**	
 * Reformat incomplete IPv6 address to decimal for search!
 */
function reformatIPv6forSearch($ip)
{
    //split network and subnet part
    $ip = explode("/", $ip);
    //if subnet is not provided we are looking for host!
    if (sizeof($ip) < 2) {
        $return['low'] = Transform2decimal($ip[0]);
        $return['high'] = Transform2decimal($ip[0]);
    }
    //if network part ends with :: we must search the complete provided subnet!
    $lastChars = substr($ip[0], -2);
    if ($lastChars == "::") {
        $return['low'] = Transform2decimal($ip[0]);
        //set highest IP address
        $subnet = substr($ip[0], 0, -2);
        $subnet = Transform2decimal($subnet);
        //calculate all possible hosts in subnet mask
        $maskHosts = gmp_strval(gmp_sub(gmp_pow(2, 128 - $ip[1]), 1));
        $return['high'] = gmp_strval(gmp_add($return['low'], $maskHosts));
    }
    return $return;
}
Beispiel #3
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;
    }
}
                        print "' href='tools/search/{$dnsResolved['name']}' ";
                        if (strlen($dnsResolved['name']) != 0) {
                            print "rel='tooltip' data-container='body' title='" . _('Search same hostnames in db') . "'";
                        }
                        print ">\t<i class='fa fa-gray fa-search'></i></a>";
                        print "<a class='mail_ipaddress   btn btn-xs btn-default          ' href='#' data-id='" . $ipaddress[$n]['id'] . "' rel='tooltip' data-container='body' title='" . _('Send mail notification') . "'>\t\t\t\t<i class='fa fa-gray fa-envelope-o'></i></a>";
                        print "<a class='delete_ipaddress btn btn-xs btn-default disabled' rel='tooltip' data-container='body' title='" . _('Delete IP address (disabled)') . "'>\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class='fa fa-gray fa-times'></i></a>";
                    }
                }
                print "\t</div>";
                print "</td>";
                print '</tr>' . "\n";
                /*	if last one return ip address and broadcast IP 
                 ****************************************************/
                if ($n == $m) {
                    $unused = FindUnusedIpAddresses($ipaddresses[$n]['ip_addr'], Transform2decimal($SubnetParsed['broadcast']), $type, 1, "broadcast", $SubnetDetails['mask']);
                    if ($unused) {
                        print '<tr class="th"><td></td><td colspan="' . $colspan['unused'] . '" class="unused">' . $unused['ip'] . ' (' . $unused['hosts'] . ')</td><td colspan=2></td></tr>' . "\n";
                    }
                }
            }
            /* next IP address for free check */
            $n++;
        }
        $c++;
    }
}
?>

</table>	<!-- end IP address table -->
Beispiel #5
0
/**
 * Write new changelog
 */
function writeChangelog($ctype, $action, $result, $old, $new)
{
    /* set query, open db connection and fetch results */
    global $database;
    # get settings
    $settings = getAllSettings();
    if ($settings['enableChangelog'] == 1) {
        # get user details
        $cuser = getActiveUserDetails();
        # unset unneeded values and format
        if ($ctype == "ip_addr") {
            unset($new['action'], $new['subnet'], $new['type']);
        } elseif ($ctype == "subnet") {
            $new['id'] = $new['subnetId'];
            unset($new['action'], $new['subnetId'], $new['location'], $new['vrfIdOld'], $new['permissions']);
            # if section does not change
            if ($new['sectionId'] == $new['sectionIdNew']) {
                unset($new['sectionIdNew']);
                unset($new['sectionId']);
                unset($old['sectionId']);
            } else {
                $old['sectionIdNew'] = $old['sectionId'];
            }
            //transform subnet
            if (strlen($new['subnet']) > 0) {
                $new['subnet'] = Transform2decimal(substr($new['subnet'], 0, strpos($new['subnet'], "/")));
            }
        } elseif ($ctype == "section") {
            unset($new['action']);
        }
        # calculate diff
        if ($action == "edit") {
            //old - checkboxes
            foreach ($old as $k => $v) {
                if (!isset($new[$k]) && $v == 1) {
                    $new[$k] = 0;
                }
            }
            foreach ($new as $k => $v) {
                //change
                if ($old[$k] != $v && $old[$k] != str_replace("\\'", "'", $v)) {
                    //empty
                    if (strlen(@$old[$k]) == 0) {
                        $old[$k] = "NULL";
                    }
                    if (strlen(@$v) == 0) {
                        $v = "NULL";
                    }
                    //state
                    if ($k == 'state') {
                        $old[$k] = reformatIPStateText($old[$k]);
                        $v = reformatIPStateText($v);
                    } elseif ($k == 'sectionIdNew') {
                        //get old and new device
                        if ($old[$k] != "NULL") {
                            $dev = getSectionDetailsById($old[$k]);
                            $old[$k] = $dev['name'];
                        }
                        if ($v != "NULL") {
                            $dev = getSectionDetailsById($v);
                            $v = $dev['name'];
                        }
                    } elseif ($k == "masterSubnetId") {
                        if ($old[$k] == 0) {
                            $old[$k] = "Root";
                        } else {
                            $dev = getSubnetDetailsById($old[$k]);
                            $old[$k] = transform2long($dev['subnet']) . "/{$dev['mask']} [{$dev['description']}]";
                        }
                        if ($v == 0) {
                            $v = "Root";
                        } else {
                            $dev = getSubnetDetailsById($v);
                            $v = transform2long($dev['subnet']) . "/{$dev['mask']} [{$dev['description']}]";
                        }
                    } elseif ($k == 'switch') {
                        if ($old[$k] == 0) {
                            $old[$k] = "None";
                        } elseif ($old[$k] != "NULL") {
                            $dev = getDeviceDetailsById($old[$k]);
                            $old[$k] = $dev['hostname'];
                        }
                        if ($v == 0) {
                            $v = "None";
                        }
                        if ($v != "NULL") {
                            $dev = getDeviceDetailsById($v);
                            $v = $dev['hostname'];
                        }
                    } elseif ($k == 'vlanId') {
                        //get old and new device
                        if ($old[$k] == 0) {
                            $old[$k] = "None";
                        } elseif ($old[$k] != "NULL") {
                            $dev = getVLANById($old[$k]);
                            $old[$k] = $dev['name'] . " [{$dev['number']}]";
                        }
                        if ($v == 0) {
                            $v = "None";
                        } elseif ($v != "NULL") {
                            $dev = getVLANById($v);
                            $v = $dev['name'] . " [{$dev['number']}]";
                        }
                    } elseif ($k == 'vrfId') {
                        //get old and new device
                        if ($old[$k] == 0) {
                            $old[$k] = "None";
                        } elseif ($old[$k] != "NULL") {
                            $dev = getVRFDetailsById($old[$k]);
                            $old[$k] = $dev['name'] . " [{$dev['description']}]";
                        }
                        if ($v == 0) {
                            $v = "None";
                        } elseif ($v != "NULL") {
                            $dev = getVRFDetailsById($v);
                            $v = $dev['name'] . " [{$dev['description']}]";
                        }
                    } elseif ($k == 'masterSection') {
                        if ($old[$k] == 0) {
                            $old[$k] = "Root";
                        } else {
                            $dev = getSectionDetailsById($old[$k]);
                            $old[$k] = "{$dev['name']}";
                        }
                        if ($v == 0) {
                            $v = "Root";
                        } else {
                            $dev = getSectionDetailsById($v);
                            $v = "{$dev['name']}";
                        }
                    } elseif ($k == "permissions") {
                        # get old and compare
                        $new['permissions'] = str_replace("\\", "", $new['permissions']);
                        //Remove /
                        # Get all groups:
                        $groups = getAllGroups();
                        $groups = rekeyGroups($groups);
                        # reformat:
                        $newp = json_decode($new['permissions']);
                        $v = '';
                        foreach ($newp as $ke => $p) {
                            $v .= "<br>" . $groups[$ke]['g_name'] . " : " . parsePermissions($p);
                        }
                        $old[$k] = "";
                    }
                    $log["[{$k}]"] = "{$old[$k]} => {$v}";
                }
            }
        } elseif ($action == "add") {
            $log['[create]'] = "{$ctype} created";
        } elseif ($action == "delete") {
            $log['[delete]'] = "{$ctype} deleted";
            $new['id'] = $old['id'];
        } elseif ($action == "truncate") {
            $log['[truncate]'] = "Subnet truncated";
        } elseif ($action == "resize") {
            $log['[resize]'] = "Subnet Resized";
            $log['[New mask]'] = "/" . $new['mask'];
        } elseif ($action == "perm_change") {
            # get old and compare
            $new['permissions_change'] = str_replace("\\", "", $new['permissions_change']);
            //Remove /
            # Get all groups:
            $groups = getAllGroups();
            $groups = rekeyGroups($groups);
            # reformat
            if ($new['permissions_change'] != "null") {
                $newp = json_decode($new['permissions_change']);
                foreach ($newp as $k => $p) {
                    $log['[Permissions]'] .= "<br>" . $groups[$k]['g_name'] . " : " . parsePermissions($p);
                }
            }
        }
        //if change happened write it!
        if (isset($log)) {
            # format change
            foreach (@$log as $k => $l) {
                $changelog .= "{$k} {$l}\n";
            }
            $changelog = $database->real_escape_string(trim($changelog));
            # set insert query
            $query = "insert into `changelog` (`ctype`,`coid`,`cuser`,`caction`,`cresult`,`cdate`,`cdiff`) values ('{$ctype}', '{$new['id']}', '{$cuser['id']}', '{$action}', '{$result}', NOW(), '{$changelog}');";
            # execute
            try {
                $database->executeQuery($query);
            } catch (Exception $e) {
                $error = $e->getMessage();
                return true;
            }
            # mail it!
            # all good
            return true;
        }
    } else {
        return true;
    }
}
Beispiel #6
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;
    }
}
/**
 * set insert / update / delete query for adding IP address
 * based on provided array
 */
function SetInsertQuery($ip)
{
    /* First we need to get custom fields! */
    $myFields = getCustomIPaddrFields();
    $myFieldsInsert['query'] = '';
    $myFieldsInsert['values'] = '';
    // <eNovance>
    // Finds the GLPI id of a new ip address.
    if ($ip['action'] == "add" and $ip['glpiId'] == '') {
        global $db;
        /* Find the glpiId of the newly added ip addresse */
        $database = new database($db['glpi_host'], $db['glpi_user'], $db['glpi_pass'], $db['glpi_name']);
        $query = "SELECT DISTINCT glpi_networkports.items_id ";
        $query .= "FROM glpi_networkports ";
        $query .= "INNER JOIN glpi_computers ON glpi_networkports.ip = '" . $ip['ip_addr'] . "' ";
        $query .= "AND glpi_computers.is_deleted = 0 ";
        $query .= "AND glpi_networkports.items_id = glpi_computers.id;";
        $glpiId = $database->getRow($query);
        if (count($glpiId > 0)) {
            $ip['glpiId'] = $glpiId[0];
        }
    }
    // </eNovance>
    if (sizeof($myFields) > 0) {
        /* set inserts for custom */
        foreach ($myFields as $myField) {
            $myFieldsInsert['query'] .= ', `' . $myField['name'] . '`';
            $myFieldsInsert['values'] .= ", '" . $ip[$myField['name']] . "'";
        }
    }
    /* insert */
    if ($ip['action'] == "add") {
        $query = "insert into `ipaddresses` ";
        $query .= "(`subnetId`,`description`,`ip_addr`, `dns_name`,`mac`, `owner`, `state`, `switch`, `port`, `note`, `excludePing` " . $myFieldsInsert['query'] . ") ";
        $query .= "values ";
        $query .= "('" . $ip['subnetId'] . "', '" . $ip['description'] . "', '" . Transform2decimal($ip['ip_addr']) . "', " . "\n";
        $query .= " '" . $ip['dns_name'] . "', '" . $ip['mac'] . "', '" . $ip['owner'] . "', '" . $ip['state'] . "', " . "\n";
        $query .= " '" . $ip['switch'] . "', '" . $ip['port'] . "', '" . $ip['note'] . "', '" . @$ip['excludePing'] . "' " . $myFieldsInsert['values'] . ");";
    } else {
        if ($ip['action'] == "edit" && $ip['type'] == "series") {
            $query = "update `ipaddresses` ";
            $query .= "set `ip_addr` = '" . Transform2decimal($ip['ip_addr']) . "', ";
            $query .= "`description` = '" . $ip['description'] . "', ";
            $query .= "`dns_name` = '" . $ip['dns_name'] . "' ,";
            $query .= "`mac` = '" . $ip['mac'] . "' ,";
            $query .= "`owner` = '" . $ip['owner'] . "' ,";
            $query .= "`state` = '" . $ip['state'] . "',";
            $query .= "`switch` = '" . $ip['switch'] . "',";
            $query .= "`port` = '" . $ip['port'] . "',";
            $query .= "`excludePing` = '" . @$ip['excludePing'] . "',";
            # custom!
            foreach ($myFields as $myField) {
                $query .= "`" . $myField['name'] . "` = '" . $ip[$myField['name']] . "',";
            }
            $query .= "`note` = '" . $ip['note'] . "' ";
            $query .= "where `subnetId` = '" . $ip['subnetId'] . "' and `ip_addr` = '" . Transform2decimal($ip['ip_addr']) . "';";
        } else {
            if ($ip['action'] == "edit") {
                $query = "update ipaddresses ";
                $query .= "set `ip_addr` = '" . Transform2decimal($ip['ip_addr']) . "', `description` = '" . $ip['description'] . "', `dns_name` = '" . $ip['dns_name'] . "' , `mac` = '" . $ip['mac'] . "', " . "\n";
                #custom!
                foreach ($myFields as $myField) {
                    $query .= "`" . $myField['name'] . "` = '" . $ip[$myField['name']] . "',";
                }
                $query .= "`owner` = '" . $ip['owner'] . "' , `state` = '" . $ip['state'] . "', `switch` = '" . $ip['switch'] . "', " . "\n";
                $query .= "`port` = '" . $ip['port'] . "', `note` = '" . $ip['note'] . "', `excludePing` = '" . @$ip['excludePing'] . "' ";
                $query .= "where `id` = '" . $ip['id'] . "';";
            } else {
                if ($ip['action'] == "delete" && $ip['type'] == "series") {
                    $query = "delete from ipaddresses where `subnetId` = '" . $ip['subnetId'] . "' and `ip_addr` = '" . Transform2decimal($ip['ip_addr']) . "';";
                } else {
                    if ($ip['action'] == "delete") {
                        $query = "delete from ipaddresses where `id` = '" . $ip['id'] . "';";
                    } else {
                        if ($ip['action'] == "move") {
                            $query = "update `ipaddresses` set `subnetId` = '{$ip['newSubnet']}' where `id` = '{$ip['id']}';";
                        }
                    }
                }
            }
        }
    }
    /* return query */
    return $query;
}