/** * Add new subnet - set query */ function setModifySubnetDetailsQuery($subnetDetails, $api) { # add new subnet if ($subnetDetails['action'] == "add") { # api? if ($api) { $query = 'insert into subnets ' . "\n"; $query .= '(`subnet`, `mask`, `sectionId`, `description`, `vlanId`, `vrfId`, `masterSubnetId`, `allowRequests`, `showName`, `permissions`, `discoverSubnet`, `pingSubnet`) ' . "\n"; $query .= 'values (' . "\n"; $query .= ' "' . $subnetDetails['subnet'] . '", ' . "\n"; $query .= ' "' . $subnetDetails['mask'] . '", ' . "\n"; $query .= ' "' . $subnetDetails['sectionId'] . '", ' . "\n"; $query .= ' "' . $subnetDetails['description'] . '", ' . "\n"; $query .= ' "' . $subnetDetails['vlanId'] . '", ' . "\n"; $query .= ' "' . $subnetDetails['vrfId'] . '", ' . "\n"; $query .= ' "' . $subnetDetails['masterSubnetId'] . '", ' . "\n"; $query .= '' . isCheckbox($subnetDetails['allowRequests']) . ',' . "\n"; $query .= '' . isCheckbox($subnetDetails['showName']) . ',' . "\n"; $query .= ' "' . $subnetDetails['permissions'] . '", ' . "\n"; $query .= '' . isCheckbox($subnetDetails['discoverSubnet']) . ',' . "\n"; $query .= '' . isCheckbox($subnetDetails['pingSubnet']) . '' . "\n"; $query .= ' );'; } else { # remove netmask and calculate decimal values! $subnetDetails['subnet_temp'] = explode("/", $subnetDetails['subnet']); $subnetDetails['subnet'] = Transform2decimal($subnetDetails['subnet_temp'][0]); $subnetDetails['mask'] = $subnetDetails['subnet_temp'][1]; # custom fields $myFields = getCustomFields('subnets'); $myFieldsInsert['query'] = ''; $myFieldsInsert['values'] = ''; if (sizeof($myFields) > 0) { /* set inserts for custom */ foreach ($myFields as $myField) { # empty? if (strlen($subnetDetails[$myField['name']]) == 0) { $myFieldsInsert['query'] .= ', `' . $myField['name'] . '`'; $myFieldsInsert['values'] .= ", NULL"; } else { $myFieldsInsert['query'] .= ', `' . $myField['name'] . '`'; $myFieldsInsert['values'] .= ", '" . $subnetDetails[$myField['name']] . "'"; } } } $query = 'insert into subnets ' . "\n"; # is folder? if ($subnetDetails['isFolder']) { $query .= '(`isFolder`,`subnet`, `mask`, `sectionId`, `description`, `vlanId`, `vrfId`, `masterSubnetId`, `allowRequests`, `showName`, `permissions`, `discoverSubnet`, `pingSubnet` ' . $myFieldsInsert['query'] . ') ' . "\n"; $query .= 'values (' . "\n"; $query .= '1, ' . "\n"; } else { $query .= '(`subnet`, `mask`, `sectionId`, `description`, `vlanId`, `vrfId`, `masterSubnetId`, `allowRequests`, `showName`, `permissions`, `discoverSubnet`, `pingSubnet` ' . $myFieldsInsert['query'] . ') ' . "\n"; $query .= 'values (' . "\n"; } $query .= ' "' . $subnetDetails['subnet'] . '", ' . "\n"; $query .= ' "' . $subnetDetails['mask'] . '", ' . "\n"; $query .= ' "' . $subnetDetails['sectionId'] . '", ' . "\n"; $query .= ' "' . $subnetDetails['description'] . '", ' . "\n"; $query .= ' "' . $subnetDetails['vlanId'] . '", ' . "\n"; $query .= ' "' . $subnetDetails['vrfId'] . '", ' . "\n"; $query .= ' "' . $subnetDetails['masterSubnetId'] . '", ' . "\n"; $query .= '' . isCheckbox($subnetDetails['allowRequests']) . ',' . "\n"; $query .= '' . isCheckbox($subnetDetails['showName']) . ',' . "\n"; $query .= ' "' . $subnetDetails['permissions'] . '", ' . "\n"; $query .= '' . isCheckbox($subnetDetails['discoverSubnet']) . ',' . "\n"; $query .= '' . isCheckbox($subnetDetails['pingSubnet']) . '' . "\n"; $query .= $myFieldsInsert['values']; $query .= ' );'; } } else { if ($subnetDetails['action'] == "delete") { /* get ALL slave subnets, then remove all subnets and IP addresses */ global $removeSlaves; getAllSlaves($subnetDetails['subnetId']); $removeSlaves = array_unique($removeSlaves); $query = ""; foreach ($removeSlaves as $slave) { $query .= 'delete from `subnets` where `id` = "' . $slave . '"; ' . "\n"; $query .= 'delete from `ipaddresses` where `subnetId` = "' . $slave . '"; ' . "\n"; } } else { if ($subnetDetails['action'] == "edit") { # custom fields $myFields = getCustomFields('subnets'); $myFieldsInsert['query'] = ''; if (sizeof($myFields) > 0) { /* set inserts for custom */ foreach ($myFields as $myField) { if (strlen($subnetDetails[$myField['name']]) == 0) { $myFieldsInsert['query'] .= ', `' . $myField['name'] . '` = NULL '; } else { $myFieldsInsert['query'] .= ', `' . $myField['name'] . '` = "' . $subnetDetails[$myField['name']] . '" '; } } } $query = 'update subnets set ' . "\n"; $query .= '`description` = "' . $subnetDetails['description'] . '", ' . "\n"; if ($subnetDetails['sectionId'] != $subnetDetails['sectionIdNew']) { $query .= '`sectionId` = "' . $subnetDetails['sectionIdNew'] . '", ' . "\n"; } $query .= '`vlanId` = "' . $subnetDetails['vlanId'] . '", ' . "\n"; $query .= '`vrfId` = "' . $subnetDetails['vrfId'] . '", ' . "\n"; $query .= '`masterSubnetId` = "' . $subnetDetails['masterSubnetId'] . '", ' . "\n"; $query .= '`allowRequests` = "' . isCheckbox($subnetDetails['allowRequests']) . '", ' . "\n"; $query .= '`showName` = "' . isCheckbox($subnetDetails['showName']) . '", ' . "\n"; $query .= '`discoverSubnet` = "' . isCheckbox($subnetDetails['discoverSubnet']) . '", ' . "\n"; $query .= '`pingSubnet` = "' . isCheckbox($subnetDetails['pingSubnet']) . '" ' . "\n"; $query .= $myFieldsInsert['query']; $query .= 'where id = "' . $subnetDetails['subnetId'] . '"; ' . "\n"; # if section changes if ($subnetDetails['sectionId'] != $subnetDetails['sectionIdNew']) { # add querry to change slaves! global $removeSlaves; getAllSlaves($subnetDetails['subnetId']); $removeSlaves = array_unique($removeSlaves); foreach ($removeSlaves as $slave) { if ($subnetDetails['subnetId'] != $slave) { $query .= 'update `subnets` set `sectionId` = "' . $subnetDetails['sectionIdNew'] . '" where `id` = "' . $slave . '"; ' . "\n"; } } } # if vrf changes if ($subnetDetails['vrfId'] != $subnetDetails['vrfIdOld']) { # add querry to change vrfId! global $removeSlaves; getAllSlaves($subnetDetails['subnetId']); $removeSlaves = array_unique($removeSlaves); foreach ($removeSlaves as $slave) { $query .= 'update `subnets` set `vrfId` = "' . $subnetDetails['vrfId'] . '" where `id` = "' . $slave . '"; ' . "\n"; } } } else { } } } # return query return $query; }
function getAllSlaves($subnetId, $multi = false) { # check cache if ($vtmp = checkCache("allslaves", $subnetId . "_{$multi}")) { return $vtmp; } else { global $removeSlaves; $end = false; # breaks while $removeSlaves[] = $subnetId; # first # db global $db; # get variables from config file $database = new database($db['host'], $db['user'], $db['pass'], $db['name']); while ($end == false) { /* get all immediate slaves */ $query = "select * from `subnets` where `masterSubnetId` = '{$subnetId}' order by `id` asc; "; /* execute query */ try { $slaves2 = $database->getArray($query); } catch (Exception $e) { $error = $e->getMessage(); print "<div class='alert alert-danger'>" . _('Error') . ": {$error}</div>"; return false; } # we have more slaves if (sizeof($slaves2) != 0) { # recursive foreach ($slaves2 as $slave) { $removeSlaves[] = $slave['id']; getAllSlaves($slave['id']); $end = true; } } else { $end = true; } } # save cache if (sizeof($removeSlaves) > 0) { writeCache("allslaves", $subnetId . "_{$multi}", $removeSlaves); } } }
<!-- description --> <tr> <td colspan="2"><hr></td> </tr> <tr> <td><?php print _('Select new subnet'); ?> :</td> <td> <select name="newSubnet" class="ip_addr form-control input-sm input-w-auto"> <?php /* get ALL slave subnets, then remove all subnets and IP addresses */ global $removeSlaves; getAllSlaves($subnetId); $removeSlaves = array_unique($removeSlaves); foreach ($removeSlaves as $subnetId) { $subnet = getSubnetDetailsById($subnetId); print "<option value='{$subnet['id']}'>{$subnet['description']} (" . Transform2long($subnet['subnet']) . "/{$subnet['mask']})</option>"; } ?> </select> </td> </tr> </table> <!-- end edit ip address table --> </form> <!-- end IP address edit form -->
require_once '../../functions/functions.php'; /* verify that user is logged in */ isUserAuthenticated(true); /* verify that user is admin */ checkAdmin(); /* verify post */ CheckReferrer(); /* get posted permissions */ foreach ($_POST as $key => $val) { if (substr($key, 0, 5) == "group") { if ($val != "0") { $perm[substr($key, 5)] = $val; } } } /* save to json */ $update['permissions'] = json_encode($perm); /* id */ $update['subnetId'] = $_POST['subnetId']; /* get ALL slave subnet id's, then remove all subnets and IP addresses */ global $removeSlaves; getAllSlaves($_POST['subnetId'], true); $update['slaves'] = array_unique($removeSlaves); /* do action! */ if (updateSubnetPermissions($update)) { if (sizeof($update['slaves']) > 1) { print '<div class="alert alert-success">' . _('Subnet permissions set for subnet and underlying subnets') . '!</div>'; } else { print '<div class="alert alert-success">' . _('Subnet permissions set') . '!</div>'; } }
/** * 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; } }
/** * Get all ip addresses in requested subnet by provided Id, sort by fieldname and direction! */ function getIpAddressesBySubnetIdslavesSort($subnetId, $fieldName = "subnetId", $direction = "asc") { global $db; # get variables from config file /* get ALL slave subnets, then remove all subnets and IP addresses */ global $removeSlaves; getAllSlaves($subnetId); $removeSlaves = array_unique($removeSlaves); /* set query, open db connection and fetch results */ $query = 'select * from `ipaddresses` where subnetId = "" '; foreach ($removeSlaves as $subnetId2) { if ($subnetId2 != $subnetId) { # ignore orphaned $query .= " or `subnetId` = '{$subnetId2}' "; } } $query .= 'order by `' . $fieldName . '` ' . $direction . ';'; $database = new database($db['host'], $db['user'], $db['pass'], $db['name']); /* execute */ try { $ipaddresses = $database->getArray($query); } catch (Exception $e) { $error = $e->getMessage(); print "<div class='alert alert-error'>" . _('Error') . ":{$error}</div>"; return false; } $database->close(); /* return ip address array */ return $ipaddresses; }
} } # check for name length - 2 is minimum! if (strlen($_POST['description']) < 2 && $_POST['action'] != "delete") { die("<div class='alert alert alert-danger'>" . _('Folder name must have at least 2 characters') . "!</div>"); } # set folder flag! $_POST['isFolder'] = true; # failed if ($_POST['action'] == "delete" && !isset($_POST['deleteconfirm'])) { # for ajax to prevent reload print "<div style='display:none'>alert alert-danger</div>"; # result print "<div class='alert alert-warning'>"; # print what will be deleted getAllSlaves($_POST['subnetId'], false); $removeSlaves = array_unique($removeSlaves); # check if folder? $foldercnt = 0; $subnetcnt = 0; foreach ($removeSlaves as $s) { $f = getSubnetDetailsById($s); if ($f['isFolder'] == 1) { $foldercnt++; } else { $subnetcnt++; } } $ipcnt = countAllSlaveIPAddresses($_POST['subnetId']); print "<strong>" . _("Warning") . "</strong>: " . _("I will delete") . ":<ul>"; print "\t<li>{$foldercnt} " . _("folders") . "</li>";