/** * get subnet details */ public function getSubnet() { /** * all subnets */ if ($this->all) { //get subnet by id $res = fetchAllSubnets(); } elseif ($this->sectionId) { //id must be set and numberic if (is_null($this->sectionId) || !is_numeric($this->sectionId)) { throw new Exception('Invalid section Id - ' . $this->sectionId); } //get all subnets in section $res = fetchSubnets($this->sectionId); //throw new exception if not existing if (sizeof($res) == 0) { //check if section exists if (sizeof(getSectionDetailsById($this->sectionId)) == 0) { throw new Exception('Section not existing'); } } } elseif ($this->id) { //id must be set and numberic if (is_null($this->id) || !is_numeric($this->id)) { throw new Exception('Subnet id not existing - ' . $this->id); } //get subnet by id $res = getSubnetDetailsById($this->id); //throw new exception if not existing if (sizeof($res) == 0) { throw new Exception('Subnet not existing'); } } elseif ($this->name) { //id must be set and numberic if (is_null($this->name) || strlen($this->name) == 0) { throw new Exception('Invalid subnet name - ' . $this->name); } //get subnet by id $res = getSubnetDetailsByName($this->name); //throw new exception if not existing if (sizeof($res) == 0) { throw new Exception('Subnet not existing'); } } else { throw new Exception('Selector missing'); } //create object from results foreach ($res as $key => $line) { $this->{$key} = $line; } //output format $format = $this->format; //remove input parameters from output unset($this->all); //remove from result array unset($this->format); unset($this->name); unset($this->id); unset($this->sectionId); //convert object to array $result = $this->toArray($this, $format); //return result return $result; }
/** * Search subnets */ function searchSubnets($searchterm, $searchTermEdited = "") { global $database; # get custom subnet fields $myFields = getCustomFields('subnets'); $custom = ''; if (sizeof($myFields) > 0) { /* set inserts for custom */ foreach ($myFields as $myField) { $custom .= ' or `' . $myField['name'] . '` like "%' . $searchterm . '%" '; } } /* set query */ if ($searchTermEdited['low'] == 0 && $searchTermEdited['high'] == 0) { $query[] = 'select * from `subnets` where `description` like "%' . $searchterm . '%" ' . $custom . ';'; } else { $query[] = 'select * from `subnets` where `description` like "%' . $searchterm . '%" or `subnet` between "' . $searchTermEdited['low'] . '" and "' . $searchTermEdited['high'] . '" ' . $custom . ';'; } /* search inside subnets even if IP does not exist! */ if ($searchTermEdited['low'] == $searchTermEdited['high']) { $allSubnets = fetchAllSubnets(); foreach ($allSubnets as $s) { // first verify address type $type = IdentifyAddress($s['subnet']); if ($type == "IPv4") { require_once 'PEAR/Net/IPv4.php'; $net = Net_IPv4::parseAddress(transform2long($s['subnet']) . '/' . $s['mask']); if ($searchTermEdited['low'] > transform2decimal($net->network) && $searchTermEdited['low'] < transform2decimal($net->broadcast)) { $query[] = "select * from `subnets` where `id` = {$s['id']}; \n"; } } } } /* execute each query */ foreach ($query as $q) { try { $search[] = $database->getArray($q); } catch (Exception $e) { $error = $e->getMessage(); } } /* filter results - remove blank */ $search = array_filter($search); /* die if errors */ if (isset($error)) { print "<div class='alert alert-danger'>" . _('Error') . ": {$error}</div>"; return false; } /* return result */ return $search; }
<div class="pContent"> <!-- IP address request form --> <form class="manageRequestEdit" name="manageRequestEdit"> <!-- edit IP address table --> <table id="manageRequestEdit" class="table table-striped table-condensed"> <!-- Section --> <tr> <th><?php print _('Requested subnet'); ?> </th> <td> <select name="subnetId" id="subnetId" class="form-control input-sm input-w-auto"> <?php $subnets = fetchAllSubnets(); foreach ($subnets as $subnet) { /* show only subnets that allow IP exporting */ if ($subnet['allowRequests'] == 1) { if ($request['subnetId'] == $subnet['id']) { print '<option value="' . $subnet['id'] . '" selected>' . Transform2long($subnet['subnet']) . '/' . $subnet['mask'] . ' [' . $subnet['description'] . ']</option>'; } else { print '<option value="' . $subnet['id'] . '">' . Transform2long($subnet['subnet']) . '/' . $subnet['mask'] . ' [' . $subnet['description'] . ']</option>'; } } } ?> </select> </td> </tr> <!-- IP address -->