// loop and get all IP addresses for ping
    for ($m = 1; $m <= $max; $m++) {
        $ip[] = transform2decimal($subnet) + $m;
    }
    // remove already existing
    foreach ($addresses as $a) {
        $key = array_search($a['ip_addr'], $ip);
        if ($key !== false) {
            unset($ip[$key]);
        }
    }
    //reindex array for pinging
    $ip = array_values($ip);
} elseif ($scanType == "update") {
    // get all existing IP addresses
    $addresses = getIpAddressesBySubnetId($subnetId);
    // we should support only up to 4096 hosts!
    if (sizeof($addresses) > 4094) {
        $res['errors'] = "Scanning from GUI is only available for subnets up to /20 or 4094 hosts!";
        $res = json_encode($res);
        print_r($res);
        die;
    }
    # exclude those marked as don't ping
    $n = 0;
    $excluded = array();
    foreach ($addresses as $m => $ipaddr) {
        if ($ipaddr['excludePing'] == "1") {
            //set result
            $excluded[] = $ipaddr['ip_addr'];
            //next
/**
 *	Generate hostfile dump for /etc/hosts
 *********************************/
/* required functions */
require_once '../../functions/functions.php';
/* we dont need any errors! */
ini_set('display_errors', 0);
/* verify that user is admin */
checkAdmin();
//set filename
$filename = "phpipam_hosts_" . date("Y-m-d");
//get subnet and section descriptions
$subnets = getAllSubnetsForExport();
//get hosts
foreach ($subnets as $s) {
    $ips = getIpAddressesBySubnetId($s['id']);
    if (sizeof($ips) > 0) {
        # create new array
        $res[] = "# {$s['s_description']} (" . transform2long($s['subnet']) . "/{$s['mask']}) - " . _('Section') . " {$s['se_description']}";
        foreach ($ips as $ip) {
            # get lenth
            $diff = 17 - strlen(transform2long($ip['ip_addr']));
            # ipv6
            if ($diff < 0) {
                $diff = 3;
            }
            # write host if dns name is set!
            if (strlen($ip['dns_name']) > 0) {
                $res[] = transform2long($ip['ip_addr']) . str_repeat(" ", $diff) . "{$ip['dns_name']}";
            }
        }
$_POST['action'] = filter_user_input($_POST['action'], false, false, true);
/* must be numeric */
if (!is_numeric($_POST['subnetId'])) {
    die('<div class="alert alert-danger">' . _("Invalid ID") . '</div>');
}
/* verify that user has write permissions for subnet */
$subnetPerm = checkSubnetPermission($_POST['subnetId']);
if ($subnetPerm < 2) {
    die('<div class="alert alert-danger">' . _('You do not have permissions to truncate subnet') . '!</div>');
}
/* verify post */
CheckReferrer();
# get subnet details
$subnet = getSubnetDetailsById($_POST['subnetId']);
# get all IP addresses
$ip_addr = getIpAddressesBySubnetId($_POST['subnetId']);
?>


<!-- header -->
<div class="pHeader"><?php 
print _('Truncate subnet');
?>
</div>


<!-- content -->
<div class="pContent">

	<table class="table table-noborder table-condensed">
Beispiel #4
0
/**
 *	Get changelog entries for all IP addresses in subnet
 */
function getSubnetIPChangelogEntries($subnetId, $limit = 50)
{
    /* set query, open db connection and fetch results */
    global $database;
    // get all slave subnets
    global $removeSlaves;
    getAllSlaves($subnetId);
    $removeSlaves = array_unique($removeSlaves);
    // get all hosts and their ID's
    $ips = array();
    if (sizeof($removeSlaves) > 0) {
        foreach ($removeSlaves as $sid) {
            $stemp = getIpAddressesBySubnetId($sid);
            if (sizeof($stemp) > 0) {
                foreach ($stemp as $ipline) {
                    $ips[] = $ipline['id'];
                }
            }
        }
    }
    //if some
    if (sizeof($ips) > 0) {
        # query
        $query = "select \n\t    \t\t\t`u`.`real_name`,`o`.`id`,`o`.`ip_addr`,`o`.`description`,`o`.`id`,`o`.`subnetId`,`c`.`caction`,`c`.`cresult`,`c`.`cdate`,`c`.`cdiff` \n\t\t\t\t\tfrom `changelog` as `c`, `users` as `u`, `ipaddresses` as `o` \n\t\t\t\t\twhere `c`.`cuser` = `u`.`id` and `c`.`coid`=`o`.`id` \n\t\t\t\t\tand (";
        foreach ($ips as $ip) {
            $query .= "`c`.`coid` = '{$ip}' or ";
        }
        $query = substr($query, 0, -3);
        $query .= ") and `c`.`ctype` = 'ip_addr' order by `c`.`cid` desc limit {$limit};";
        # execute
        try {
            $res = $database->getArray($query);
        } catch (Exception $e) {
            $error = $e->getMessage();
            print "<div class='alert alert-danger'>" . _('Error') . ": {$error}</div>";
            return false;
        }
        # return result
        return $res;
    } else {
        return false;
    }
}
 $vlan = subnetGetVLANdetailsById($subnet['vlanId']);
 if (strlen($vlan['number']) > 0) {
     $vlanText = " (vlan: " . $vlan['number'];
     if (strlen($vlan['name']) > 0) {
         $vlanText .= ' - ' . $vlan['name'] . ')';
     } else {
         $vlanText .= ")";
     }
 } else {
     $vlanText = "";
 }
 $worksheet->write($lineCount, 0, transform2long($subnet['subnet']) . "/" . $subnet['mask'] . " - " . $subnet['description'] . $vlanText, $format_header);
 $worksheet->mergeCells($lineCount, 0, $lineCount, $colSize);
 $lineCount++;
 //IP addresses in subnet
 $ipaddresses = getIpAddressesBySubnetId($subnet['id']);
 //write headers
 $worksheet->write($lineCount, 0, _('ip address'), $format_title);
 $worksheet->write($lineCount, 1, _('ip state'), $format_title);
 $worksheet->write($lineCount, 2, _('description'), $format_title);
 $worksheet->write($lineCount, 3, _('hostname'), $format_title);
 $worksheet->write($lineCount, 4, _('mac'), $format_title);
 $worksheet->write($lineCount, 5, _('owner'), $format_title);
 $worksheet->write($lineCount, 6, _('switch'), $format_title);
 $worksheet->write($lineCount, 7, _('port'), $format_title);
 $worksheet->write($lineCount, 8, _('note'), $format_title);
 $m = 9;
 //custom
 if (sizeof($myFields) > 0) {
     foreach ($myFields as $myField) {
         $worksheet->write($lineCount, $m, $myField['name'], $format_title);
/**
 * Print subnets structure
 */
function printSubnets($subnets, $actions = true, $vrf = "0", $custom = array())
{
    $html = array();
    $rootId = 0;
    # root is 0
    if (sizeof($subnets) > 0) {
        foreach ($subnets as $item) {
            $children[$item['masterSubnetId']][] = $item;
        }
    }
    # loop will be false if the root has no children (i.e., an empty menu!)
    $loop = !empty($children[$rootId]);
    # initializing $parent as the root
    $parent = $rootId;
    $parent_stack = array();
    # display selected subnet as opened
    if (isset($_REQUEST['subnetId'])) {
        $allParents = getAllParents($_REQUEST['subnetId']);
    }
    # return table content (tr and td's)
    while ($loop && (($option = each($children[$parent])) || $parent > $rootId)) {
        # repeat
        $repeat = str_repeat(" - ", count($parent_stack));
        # dashes
        if (count($parent_stack) == 0) {
            $dash = "";
        } else {
            $dash = "-";
        }
        if (count($parent_stack) == 0) {
            $margin = "0px";
            $padding = "0px";
        } else {
            # padding
            $padding = "10px";
            # margin
            $margin = count($parent_stack) * 10 - 10;
            $margin = $margin * 2;
            $margin = $margin . "px";
        }
        # count levels
        $count = count($parent_stack) + 1;
        # get subnet details
        # get VLAN
        $vlan = subnetGetVLANdetailsById($option['value']['vlanId']);
        $vlan = $vlan['number'];
        if (empty($vlan) || $vlan == "0") {
            $vlan = "";
        }
        # no VLAN
        # description
        if (strlen($option['value']['description']) == 0) {
            $description = "/";
        } else {
            $description = $option['value']['description'];
        }
        # description
        # requests
        if ($option['value']['allowRequests'] == 1) {
            $requests = _("enabled");
        } else {
            $requests = "";
        }
        # request disabled
        # hosts check
        if ($option['value']['pingSubnet'] == 1) {
            $pCheck = _("enabled");
        } else {
            $pCheck = "";
        }
        # ping check disabled
        #vrf
        if ($vrf == "1") {
            # get VRF details
            if ($option['value']['vrfId'] != "0" && $option['value']['vrfId'] != "NULL") {
                $vrfTmp = getVRFDetailsById($option['value']['vrfId']);
                $vrfText = $vrfTmp['name'];
            } else {
                $vrfText = "";
            }
        }
        // <eNovance>
        // count the number of free and offline hosts
        $ipaddresses = getIpAddressesBySubnetId($option['value']['id']);
        $reservedHosts = 0;
        foreach ($ipaddresses as $ip) {
            if ($ip['state'] == "2") {
                $reservedHosts += 1;
            }
        }
        $subnetDetails = calculateSubnetDetails(count($ipaddresses), $option['value']['mask'], $option['value']['subnet']);
        $freeHosts = $subnetDetails['freehosts'];
        // </eNovance>
        # print table line
        if (strlen($option['value']['subnet']) > 0) {
            // verify permission
            $permission = checkSubnetPermission($option['value']['id']);
            // print item
            if ($permission != 0) {
                $html[] = "<tr>";
                $html[] = "\t<td class='level{$count}'><span class='structure' style='padding-left:{$padding}; margin-left:{$margin};'></span><a href='subnets/" . $option['value']['sectionId'] . "/" . $option['value']['id'] . "/'>  " . transform2long($option['value']['subnet']) . "/" . $option['value']['mask'] . "</a></td>";
                $html[] = "\t<td class='level{$count}'><span class='structure' style='padding-left:{$padding}; margin-left:{$margin};'></span> {$description}</td>";
                $html[] = "\t<td>{$vlan}</td>";
                #vrf
                if ($vrf == "1") {
                    $html[] = "\t<td>{$vrfText}</td>";
                }
                // <eNovance>
                // Removed $requests and $pCheck since it was not necessary for us
                // </eNovance>
                # custom
                if (sizeof($custom) > 0) {
                    foreach ($custom as $field) {
                        $html[] = "\t<td>" . $option['value'][$field['name']] . "</td>";
                    }
                }
                // <eNovance>
                // Set the 'reserved' and 'free' hosts columns' value
                $html[] = "<td>{$reservedHosts}</td>";
                if ($subnetDetails['freehosts_percent'] <= 5) {
                    $html[] = "<td><a href=\"subnets/" . $option['value']['sectionId'] . "/" . $option['value']['id'] . "/\" class=\"SubnetFull\">{$freeHosts}</a></td>";
                } elseif ($subnetDetails['freehosts_percent'] <= 20) {
                    $html[] = "<td><a href=\"subnets/" . $option['value']['sectionId'] . "/" . $option['value']['id'] . "/\" class=\"SubnetAlmostFull\">{$freeHosts}</a></td>";
                } else {
                    $html[] = "<td><a href=\"subnets/" . $option['value']['sectionId'] . "/" . $option['value']['id'] . "/\" class=\"SubnetNotFull\">{$freeHosts}</a></td>";
                }
                // </eNovance>
                if ($actions) {
                    $html[] = "\t<td class='actions' style='padding:0px;'>";
                    $html[] = "\t<div class='btn-group'>";
                    $html[] = "\t\t<button class='btn btn-mini editSubnet'     data-action='edit'   data-subnetid='" . $option['value']['id'] . "'  data-sectionid='" . $option['value']['sectionId'] . "'><i class='icon-gray icon-pencil'></i></button>";
                    $html[] = "\t\t<button class='btn btn-mini showSubnetPerm' data-action='show'   data-subnetid='" . $option['value']['id'] . "'  data-sectionid='" . $option['value']['sectionId'] . "'><i class='icon-gray icon-tasks'></i></button>";
                    $html[] = "\t\t<button class='btn btn-mini editSubnet'     data-action='delete' data-subnetid='" . $option['value']['id'] . "'  data-sectionid='" . $option['value']['sectionId'] . "'><i class='icon-gray icon-remove'></i></button>";
                    $html[] = "\t</div>";
                    $html[] = "\t</td>";
                }
                $html[] = "</tr>";
            }
        }
        if ($option === false) {
            $parent = array_pop($parent_stack);
        } elseif (!empty($children[$option['value']['id']])) {
            array_push($parent_stack, $option['value']['masterSubnetId']);
            $parent = $option['value']['id'];
        } else {
        }
    }
    return implode("\n", $html);
}