Private GIT

Skip to content
Snippets Groups Projects
Commit 86aa3463 authored by Gary Allan's avatar Gary Allan
Browse files

Cleanup: switch mastersubnet-dropdown to fetch_overlapping_subnets()

Cleanup: class.Menus.php
parent 53478c35
Branches
No related tags found
No related merge requests found
...@@ -16,41 +16,29 @@ $Subnets = new Subnets ($Database); ...@@ -16,41 +16,29 @@ $Subnets = new Subnets ($Database);
# verify that user is logged in # verify that user is logged in
$User->check_user_session(); $User->check_user_session();
/** /**
* Return array of valid subnets satisfying strict subnet requirements * Return array of valid subnets satisfying strict subnet requirements
* @param Subnets $Subnets [description] * @param Subnets $Subnets
* @param array $search [description] * @param integer $sectionId
* @param string $cidr [description] * @param string $cidr
* @return array [description] * @param array|string $result_fields
* @return array
*/ */
function get_strict_subnets($Subnets, $search, $cidr) { function get_strict_subnets($Subnets, $sectionId, $cidr, $result_fields="*") {
$strict_subnets = array();
if ( $Subnets->verify_cidr_address($cidr) !== true) { return array(); } if ( $Subnets->verify_cidr_address($cidr) !== true) { return array(); }
list($cidr_addr, $cidr_mask) = explode('/', $cidr); list($cidr_addr, $cidr_mask) = explode('/', $cidr);
$bmask = $Subnets->get_network_bitmasks(); $strict_subnets = $Subnets->fetch_overlapping_subnets($cidr, 'sectionId', $sectionId, $result_fields);
if (!array($strict_subnets)) return array();
$cidr_decimal = $Subnets->transform_to_decimal($cidr_addr);
$cidr_type = $Subnets->identify_address($cidr_addr);
// Calculate what the parent subnet decimal address would be for each mask and check if it exists
$search_mask = $cidr_mask - 1;
while($search_mask >= 0) {
$search_subnet= gmp_strval(gmp_and($cidr_decimal, $bmask[$cidr_type][$search_mask]['lo']));
if (is_array($search[$cidr_type][$search_mask][$search_subnet])) { foreach ($strict_subnets as $i => $subnet) {
$strict_subnets = array_merge($strict_subnets, $search[$cidr_type][$search_mask][$search_subnet]); if ($subnet->mask >= $cidr_mask) unset($strict_subnets[$i]); else break;
} }
$search_mask--;
}
return $strict_subnets; return $strict_subnets;
} }
$sectionId = isset($_GET['section']) ? (int) $_GET['section'] : 0; $sectionId = isset($_GET['section']) ? (int) $_GET['section'] : 0;
$cidr = isset($_GET['cidr']) ? $_GET['cidr'] : ''; $cidr = isset($_GET['cidr']) ? $_GET['cidr'] : '';
$previously_selected = isset($_GET['prev']) ? (int) $_GET['prev'] : -1; $previously_selected = isset($_GET['prev']) ? (int) $_GET['prev'] : -1;
...@@ -58,38 +46,39 @@ $previously_selected = isset($_GET['prev']) ? (int) $_GET['prev'] : -1; ...@@ -58,38 +46,39 @@ $previously_selected = isset($_GET['prev']) ? (int) $_GET['prev'] : -1;
$section = $Sections->fetch_section('id', $sectionId); $section = $Sections->fetch_section('id', $sectionId);
if (!is_object($section)) { return ''; } if (!is_object($section)) { return ''; }
$folders = array(); // Don't fetch all fields
$search = array(); $fields = array('id','masterSubnetId','isFolder','subnet','mask','description');
$all_subnets = $Subnets->fetch_section_subnets ($sectionId, array('id','masterSubnetId','isFolder','subnet','mask','description'));
if (!is_array($all_subnets)) $all_subnets = array();
foreach($all_subnets as $subnet) {
if ($subnet->isFolder) {
$folders[] = clone $subnet;
$subnet->disabled = 1;
} else {
$subnet->type = $Subnets->identify_address($subnet->subnet);
$search[$subnet->type][$subnet->mask][$subnet->subnet][] = $subnet;
}
}
$strict_subnets = get_strict_subnets($Subnets, $search, $cidr); $strict_subnets = get_strict_subnets($Subnets, $sectionId, $cidr, $fields);
$folders = $Subnets->fetch_multiple_objects('subnets', 'isFolder', '1', 'id', true, null, $fields);
if (!is_array($folders)) $folders = array();
// Generate HTML <options> dropdown menu // Generate HTML <options> dropdown menu
$dropdown = new MasterSubnetDropDown($Subnets, $previously_selected); $dropdown = new MasterSubnetDropDown($Subnets, $previously_selected);
// Show overlapping subnets (possible parents)
if (!empty($strict_subnets)) { if (!empty($strict_subnets)) {
$dropdown->optgroup_open(_('Strict Subnets')); $dropdown->optgroup_open(_('Strict Subnets'));
foreach($strict_subnets as $subnet) { $dropdown->subnets_add_object($subnet); } foreach($strict_subnets as $subnet) { $dropdown->subnets_add_object($subnet); }
} }
// Show folders
$dropdown->optgroup_open(_('Folders')); $dropdown->optgroup_open(_('Folders'));
foreach($folders as $folder) { $dropdown->subnets_tree_add($folder); } foreach($folders as $folder) { $dropdown->subnets_tree_add($folder); }
$dropdown->subnets_tree_render(true); $dropdown->subnets_tree_render(true);
if ($section->strictMode == 0) { if ($section->strictMode == 0) {
// Strict mode is disabled, allow nested chaos....
$all_subnets = $Subnets->fetch_section_subnets($sectionId, $fields);
if (!is_array($all_subnets)) $all_subnets = array();
foreach($all_subnets as $subnet) {
if ($subnet->isFolder) $subnet->disabled = 1; else break;
}
// Show all subnets
$dropdown->optgroup_open(_('Subnets')); $dropdown->optgroup_open(_('Subnets'));
foreach($all_subnets as $subnet) { $dropdown->subnets_tree_add($subnet); } foreach($all_subnets as $subnet) { $dropdown->subnets_tree_add($subnet); }
$dropdown->subnets_tree_render(false); $dropdown->subnets_tree_render(false);
......
...@@ -65,6 +65,8 @@ class MasterSubnetDropDown { ...@@ -65,6 +65,8 @@ class MasterSubnetDropDown {
* @param string $name * @param string $name
*/ */
public function optgroup_open($name) { public function optgroup_open($name) {
if (empty($name)) return;
$this->optgroup_close(); $this->optgroup_close();
$this->options_group = $name; $this->options_group = $name;
$this->html[] = '<optgroup label="'.$name.'">'; $this->html[] = '<optgroup label="'.$name.'">';
...@@ -74,27 +76,26 @@ class MasterSubnetDropDown { ...@@ -74,27 +76,26 @@ class MasterSubnetDropDown {
* Close an open html </optgroup> * Close an open html </optgroup>
*/ */
public function optgroup_close() { public function optgroup_close() {
if ($this->options_group) { $this->html[] = '</optgroup>'; } if (!empty($this->options_group)) $this->html[] = '</optgroup>';
$this->options_group = null; $this->options_group = null;
} }
/** /**
* Return <option> customisations * Return <option> customisations
* @param stdObject|null $subnet * @param stdObject $subnet
* @return string * @return string
*/ */
private function get_subnet_options($subnet) { private function get_subnet_options($subnet) {
$options = array(); $options = array();
// selected="selected" // selected="selected"
$id = is_object($subnet) ? $subnet->id : 0; if ($subnet->id == $this->previously_selected) {
if ($id == $this->previously_selected) {
$this->previously_selected = -1; $this->previously_selected = -1;
$options[] = 'selected="selected"'; $options[] = 'selected="selected"';
} }
// disabled // disabled
if (is_object($subnet) && isset($subnet->disabled) && $subnet->disabled == 1) { if (isset($subnet->disabled) && $subnet->disabled == 1) {
$options[] = 'disabled'; $options[] = 'disabled';
} }
...@@ -103,16 +104,13 @@ class MasterSubnetDropDown { ...@@ -103,16 +104,13 @@ class MasterSubnetDropDown {
/** /**
* Generate menu item from subnet object * Generate menu item from subnet object
* @param stdObject|null $subnet * @param stdObject $subnet
* @param integer $level (de) * @param integer $level
*/ */
public function subnets_add_object($subnet, $level = 0) { public function subnets_add_object($subnet, $level = 0) {
$options = $this->get_subnet_options($subnet); if (!is_object($subnet)) return;
if (!is_object($subnet)) { $options = $this->get_subnet_options($subnet);
$this->html[] = "<option $options value='0'>"._("Root folder")."</option>";
return;
}
if (strlen($subnet->description)>34) $subnet->description = substr($subnet->description, 0, 31) . '...'; if (strlen($subnet->description)>34) $subnet->description = substr($subnet->description, 0, 31) . '...';
$prefix = str_repeat(' - ', $level); $prefix = str_repeat(' - ', $level);
...@@ -149,7 +147,11 @@ class MasterSubnetDropDown { ...@@ -149,7 +147,11 @@ class MasterSubnetDropDown {
*/ */
public function subnets_tree_reset() { public function subnets_tree_reset() {
$this->children_by_parent_id = array(); $this->children_by_parent_id = array();
$this->subnets_by_id = array(null); $root = new stdClass ();
$root->id = 0;
$root->isFolder = 1;
$root->description = _("Root folder");
$this->subnets_by_id = array(0 => $root);
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment