function showForm($ID, $options = array()) { global $CFG_GLPI, $DB; if (!self::canView()) { return false; } $this->check($ID, READ); $recursiveItems = $this->recursivelyGetItems(); if (count($recursiveItems) > 0) { $lastItem = $recursiveItems[count($recursiveItems) - 1]; $lastItem_entities_id = $lastItem->getField('entities_id'); } else { $lastItem_entities_id = $_SESSION['glpiactive_entity']; } $options['entities_id'] = $lastItem_entities_id; $this->showFormHeader($options); $options['canedit'] = false; $options['candel'] = false; $number_errors = 0; foreach (self::getMotives() as $key => $name) { if ($this->fields[$key] == 1) { $number_errors++; } } $motives = self::getMotives(); $interface_cell = "td"; $address_cell = "td"; $network_cell = "td"; $gateway_cell = "td"; $address = new IPAddress(); $netmask = new IPNetmask(); $number_real_errors = 0; if (!$address->setAddressFromString($this->fields['ip']) || !$netmask->setNetmaskFromString($this->fields['netmask'], $address->getVersion())) { unset($address); unset($netmask); } else { $network = new IPNetwork(); $params = array("address" => $address, "netmask" => $netmask); if (isset($this->fields["address"])) { $params["exclude IDs"] = $this->fields["address"]; } if (isset($this->fields["entities_id"])) { $entity = $this->fields["entities_id"]; } else { $entity = -1; } $networkports_ids = IPNetwork::searchNetworks("equals", $params, $entity, false); if (count($networkports_ids) == 0) { unset($network); } else { $network->getFromDB($networkports_ids[0]); } } if ($this->fields['unknown_interface_type'] == 1) { $options['canedit'] = true; $number_real_errors++; $interface_cell = "th"; echo "<tr class='tab_bg_1'><th>" . $motives['unknown_interface_type'] . "</th>\n" . "<td>" . __('Transform this network port to'); echo "</td><td colspan=2>"; Dropdown::showItemTypes('transform_to', NetworkPort::getNetworkPortInstantiations(), array('value' => "NetworkPortEthernet")); echo "</td></tr>\n"; } if ($this->fields['invalid_network'] == 1) { $number_real_errors++; $network_cell = "th"; $address_cell = "th"; echo "<tr class='tab_bg_1'><th>" . $motives['invalid_network'] . "</th>\n<td colspan=3>"; if (isset($network)) { printf(__('Network port information conflicting with %s'), $network->getLink()); } else { if (!isset($address) || !isset($netmask)) { _e('Invalid address or netmask'); } else { _e('No conflicting network'); } echo " <a href='" . Toolbox::getItemTypeFormURL('IPNetwork') . "'>" . __('you may have to add a network') . "</a>"; } echo "</td></tr>\n"; } if ($this->fields['invalid_gateway'] == 1) { $number_real_errors++; $gateway_cell = "th"; echo "<tr class='tab_bg_1'><th>" . $motives['invalid_gateway'] . "</th>\n<td colspan=3>"; if (isset($network)) { printf(__('Append a correct gateway to the network %s'), $network->getLink()); } else { printf(__('%1$s: %2$s'), __('Unknown network'), "<a href='" . Toolbox::getItemTypeFormURL('IPNetwork') . "'>" . __('Add a network') . "\n </a>"); } echo "</td></tr>\n"; } if ($this->fields['invalid_address'] == 1) { $number_real_errors++; $address_cell = "th"; echo "<tr class='tab_bg_1'><th>" . $motives['invalid_address'] . "</th>\n<td colspan=3>"; $networkPort = new NetworkPort(); if ($networkPort->getFromDB($this->getID())) { $number_real_errors++; echo "<a href='" . $networkPort->getLinkURL() . "'>" . __('Add a correct IP to the network port') . "</a>"; } else { _e('Unknown network port'); } echo "</td></tr>\n"; } if ($number_real_errors == 0) { echo "<tr class='tab_bg_1'><th colspan='3'>" . __('I don\'t understand why this migration error is not deleted.'); echo "</th><th>"; Html::showSimpleForm($this->getFormURL(), 'delete', __('You can delete this migration error'), array('id' => $this->getID())); echo "</th></tr>\n"; } else { echo "<tr class='tab_bg_1'><th>" . __('At all events') . "</th>\n"; echo "<td colspan='3'>"; Html::showSimpleForm($this->getFormURL(), 'delete', __('You can delete this migration error'), array('id' => $this->getID())); echo "</td></tr>\n"; } echo "<tr class='tab_bg_1'><td colspan='4'> </td></tr>\n"; echo "<tr class='tab_bg_1'><th colspan='4'>" . __('Original network port information') . "</th>" . "</tr>\n"; echo "<tr class='tab_bg_1'><td>"; $this->displayRecursiveItems($recursiveItems, 'Type'); echo "</td>\n<td>"; $this->displayRecursiveItems($recursiveItems, "Link"); echo "</td>\n"; echo "<td>" . __('Comments') . "</td>"; echo "<td class='middle'>" . $this->fields["comment"] . "</td></tr>\n"; echo "<tr class='tab_bg_1'><td>" . __('Network address') . "</td>\n"; echo "<{$network_cell}>" . $this->fields['subnet'] . "</{$network_cell}>\n"; echo "<td>" . IPNetmask::getTypeName(1) . "</td>\n"; echo "<{$network_cell}>" . $this->fields['netmask'] . "</{$network_cell}></tr>\n"; echo "<tr class='tab_bg_1'><td>" . IPAddress::getTypeName(1) . "</td>\n"; echo "<{$address_cell}>" . $this->fields['ip'] . "</{$address_cell}>\n"; echo "<td>" . __('Gateway') . "</td>\n"; echo "<{$gateway_cell}>" . $this->fields['gateway'] . "</{$gateway_cell}></tr>\n"; echo "<tr class='tab_bg_1'><td>" . __('Network interface') . "</td><{$interface_cell}>\n"; if (TableExists('glpi_networkinterfaces')) { $query = "SELECT `name`\n FROM `glpi_networkinterfaces`\n WHERE `id`='" . $this->fields['networkinterfaces_id'] . "'"; $result = $DB->query($query); if ($DB->numrows($result) > 0) { $row = $DB->fetch_assoc($result); echo $row['name']; } else { _e('Unknown interface'); } } echo "</{$interface_cell}>"; echo "<{$interface_cell}></{$interface_cell}>"; echo "<{$interface_cell}></{$interface_cell}></tr>\n"; $this->showFormButtons($options); }
/** * Search networks relative to a given network * * @param $relation type of relation ("is contained by", "equals" or "contains") * regarding the networks given as parameter * @param $condition array of elements to select the good arrays (see Parameters above) * - fields : the fields of the network we wish to retrieve (single field or array of * fields). This parameter will impact the result of the function * - address (see \ref parameterType) : the address for the query * - netmask (see \ref parameterType) : the netmask for the query * - exclude IDs : the IDs to exclude from the query (for instance, $this->getID()) * - where : filters to add to the SQL request * * @param $entityID the entity on which the selection should occur (-1 => the current active * entity) (default -1) * @param $recursive set to false to only search in current entity, otherwise, all visible * entities will be search (true by default) * @param $version version of IP to look (only use when using arrays or string as input for * address or netmask n(default 0) * * @return array of networks found. If we want request several field, the return value will be * an array of array * * \warning The order of the elements inside the result are ordered from the nearest one to the * further. (ie. 0.0.0.0 is the further of whatever network if you lool for ones that * contains the current network. **/ static function searchNetworks($relation, $condition, $entityID = -1, $recursive = true, $version = 0) { global $DB; if (empty($relation)) { return false; } if (empty($condition["fields"])) { $fields = 'id'; } else { $fields = $condition["fields"]; } if (!is_array($fields)) { $fields = array($fields); } $FIELDS = "`" . implode("`, `", $fields) . "`"; $startIndex = $version == 4 ? 3 : 1; $addressDB = array('address_0', 'address_1', 'address_2', 'address_3'); $netmaskDB = array('netmask_0', 'netmask_1', 'netmask_2', 'netmask_3'); $WHERE = ""; if (isset($condition["address"]) && isset($condition["netmask"])) { $addressPa = new IPAddress($condition["address"]); // Check version equality ... if ($version != $addressPa->getVersion()) { if ($version != 0) { return false; } $version = $addressPa->getVersion(); } $netmaskPa = new IPNetmask($condition["netmask"], $version); // Get the array of the adresses $addressPa = $addressPa->getBinary(); $netmaskPa = $netmaskPa->getBinary(); // Check the binary is valid if (!is_array($addressPa) || count($addressPa) != 4) { return false; } if (!is_array($netmaskPa) || count($netmaskPa) != 4) { return false; } $startIndex = $version == 4 ? 3 : 0; if ($relation == "equals") { for ($i = $startIndex; $i < 4; ++$i) { $WHERE .= " AND (`" . $addressDB[$i] . "` & '" . $netmaskPa[$i] . "')=\n ('" . $addressPa[$i] . "' & '" . $netmaskPa[$i] . "')\n AND ('" . $netmaskPa[$i] . "' = `" . $netmaskDB[$i] . "`)"; } } else { for ($i = $startIndex; $i < 4; ++$i) { if ($relation == "is contained by") { $globalNetmask = "'" . $netmaskPa[$i] . "'"; } else { $globalNetmask = "`" . $netmaskDB[$i] . "`"; } $WHERE .= " AND (`" . $addressDB[$i] . "` & {$globalNetmask})=\n ('" . $addressPa[$i] . "' & {$globalNetmask})\n AND ('" . $netmaskPa[$i] . "' & `" . $netmaskDB[$i] . "`)={$globalNetmask}"; } } } $WHERE = "`version`='{$version}' {$WHERE}"; if ($entityID < 0) { $entityID = $_SESSION['glpiactive_entity']; } $entitiesID = array(); switch ($relation) { case "is contained by": $ORDER_ORIENTATION = 'ASC'; if ($recursive) { $entitiesID = getSonsOf('glpi_entities', $entityID); } break; case "contains": $ORDER_ORIENTATION = 'DESC'; if ($recursive) { $entitiesID = getAncestorsOf('glpi_entities', $entityID); } break; case "equals": $ORDER_ORIENTATION = ''; if ($recursive) { $entitiesID = getSonsAndAncestorsOf('glpi_entities', $entityID); } break; } $entitiesID[] = $entityID; if (count($entitiesID) > 1) { // At least the current entity is defined $WHERE .= " AND `entities_id` IN ('" . implode("', '", $entitiesID) . "')"; } else { $WHERE .= " AND `entities_id` = '" . $entitiesID[0] . "'"; } if (!empty($condition["exclude IDs"])) { if (is_array($condition["exclude IDs"])) { if (count($condition["exclude IDs"]) > 1) { $WHERE .= " AND `id` NOT IN ('" . implode("', '", $condition["exclude IDs"]) . "')"; } else { $WHERE .= " AND `id` <> '" . $condition["exclude IDs"][0] . "'"; } } else { $WHERE .= " AND `id` <> '" . $condition["exclude IDs"] . "'"; } } $ORDER = array(); for ($i = $startIndex; $i < 4; ++$i) { $ORDER[] = "BIT_COUNT(`" . $netmaskDB[$i] . "`) {$ORDER_ORIENTATION}"; } if (!empty($condition["where"])) { $WHERE .= " AND " . $condition["where"]; } $query = "SELECT {$FIELDS}\n FROM `glpi_ipnetworks`\n WHERE {$WHERE}\n ORDER BY " . implode(', ', $ORDER); // By ordering on the netmask, we ensure that the first element is the nearest one (ie: // the last should be 0.0.0.0/0.0.0.0 of x.y.z.a/255.255.255.255 regarding the interested // element) $returnValues = array(); if ($result = $DB->query($query)) { while ($data = $DB->fetch_assoc($result)) { if (count($fields) > 1) { $returnValue = array(); foreach ($fields as $field) { $returnValue[$field] = $data[$field]; } } else { $returnValue = $data[$fields[0]]; } $returnValues[] = $returnValue; } } return $returnValues; }