/** * @param $itemtype * @param $items_id * @param $main_items_id * @param $main_itemtype * @param $entities_id * @param $IP **/ function createNetworkNameFromItem($itemtype, $items_id, $main_items_id, $main_itemtype, $entities_id, $IP) { global $migration; // Using gethostbyaddr() allows us to define its reald internet name according to its IP. // But each gethostbyaddr() may reach several milliseconds. With very large number of // Networkports or NetworkeEquipment, the migration may take several minutes or hours ... //$computerName = gethostbyaddr($IP); $computerName = $IP; if ($computerName != $IP) { $position = strpos($computerName, "."); $name = substr($computerName, 0, $position); $domain = substr($computerName, $position + 1); $query = "SELECT `id`\n FROM `glpi_fqdns`\n WHERE `fqdn` = '{$domain}'"; $result = $DB->query($query); if ($DB->numrows($result) == 1) { $data = $DB->fetch_assoc($result); $domainID = $data['id']; } } else { $name = "migration-" . str_replace('.', '-', $computerName); $domainID = 0; } $IPaddress = new IPAddress(); if ($IPaddress->setAddressFromString($IP)) { $input = array('name' => $name, 'fqdns_id' => $domainID, 'entities_id' => $entities_id, 'items_id' => $items_id, 'itemtype' => $itemtype); $networknames_id = $migration->insertInTable('glpi_networknames', $input); $input = $IPaddress->setArrayFromAddress(array('entities_id' => $entities_id, 'itemtype' => 'NetworkName', 'items_id' => $networknames_id), "version", "name", "binary"); $migration->insertInTable($IPaddress->getTable(), $input); } else { // Don't add the NetworkName if the address is not valid addNetworkPortMigrationError($items_id, 'invalid_address'); logNetworkPortError('invalid IP address', $items_id, $main_itemtype, $main_items_id, "{$IP}"); } unset($IPaddress); }
/** * @param $input **/ function prepareInput($input) { // In case of entity transfer, $input['network'] is not defined if (!isset($input['network']) && isset($this->fields['network'])) { $input['network'] = $this->fields['network']; } // In case of entity transfer, $input['gateway'] is not defined if (!isset($input['gateway']) && isset($this->fields['gateway'])) { $input['gateway'] = $this->fields['gateway']; } // If $this->fields["id"] is not set then, we are adding a new network // Or if $this->fields["network"] != $input["network"] we a updating the network $address = new IPAddress(); $netmask = new IPNetmask(); // Don't validate an empty network if (empty($input["network"])) { return array('error' => __('Invalid network address'), 'input' => false); } if (!isset($this->fields["id"]) || !isset($this->fields["network"]) || $input["network"] != $this->fields["network"]) { $network = explode("/", $input["network"]); if (count($network) != 2) { return array('error' => __('Invalid input format for the network'), 'input' => false); } if (!$address->setAddressFromString(trim($network[0]))) { return array('error' => __('Invalid network address'), 'input' => false); } if (!$netmask->setNetmaskFromString(trim($network[1]), $address->getVersion())) { return array('error' => __('Invalid subnet mask'), 'input' => false); } // After checking that address and netmask are valid, modify the address to be the "real" // network address : the first address of the network. This is not required for SQL, but // that looks better for the human self::computeNetworkRangeFromAdressAndNetmask($address, $netmask, $address); // Now, we look for already existing same network inside the database $params = array("address" => $address, "netmask" => $netmask); if (isset($this->fields["id"])) { $params["exclude IDs"] = $this->fields["id"]; } if (isset($this->fields["entities_id"])) { $entities_id = $this->fields["entities_id"]; } else { if (isset($input["entities_id"])) { $entities_id = $input["entities_id"]; } else { $entities_id = -1; } } // TODO : what is the best way ? recursive or not ? $sameNetworks = self::searchNetworks("equals", $params, $entities_id, false); // Check unicity ! if ($sameNetworks && count($sameNetworks) > 0) { return array('error' => __('Network already defined in visible entities'), 'input' => false); } // Then, update $input to reflect the network and the netmask $input = $address->setArrayFromAddress($input, "version", "address", "address"); $input = $netmask->setArrayFromAddress($input, "", "netmask", "netmask"); // We check to see if the network is modified $previousAddress = new IPAddress(); $previousAddress->setAddressFromArray($this->fields, "version", "address", "address"); $previousNetmask = new IPNetmask(); $previousNetmask->setAddressFromArray($this->fields, "version", "netmask", "netmask"); if ($previousAddress->equals($address) && $previousNetmask->equals($netmask)) { $this->networkUpdate = false; } else { $this->networkUpdate = true; } } else { // If netmask and address are not modified, then, load them from DB to check the validity // of the gateway $this->networkUpdate = false; $address->setAddressFromArray($this->fields, "version", "address", "address"); $netmask->setAddressFromArray($this->fields, "version", "netmask", "netmask"); $entities_id = $this->fields['entities_id']; } // Update class for the CommonImplicitTree update ... $this->data_for_implicit_update = array('address' => $address, 'netmask' => $netmask, 'entities_id' => $entities_id); $returnValue = array(); // If the gateway has been altered, or the network information (address or netmask) changed, // then, we must revalidate the gateway ! if (!isset($this->fields["gateway"]) || $input["gateway"] != $this->fields["gateway"] || $this->networkUpdate) { $gateway = new IPAddress(); if (!empty($input["gateway"])) { if (!$gateway->setAddressFromString($input["gateway"]) || !self::checkIPFromNetwork($gateway, $address, $netmask)) { $returnValue['error'] = __('Invalid gateway address'); if (!empty($this->fields["gateway"])) { if (!$gateway->setAddressFromString($this->fields["gateway"]) || !self::checkIPFromNetwork($gateway, $address, $netmask)) { $gateway->disableAddress(); } } else { $gateway->disableAddress(); } } } $input = $gateway->setArrayFromAddress($input, "", "gateway", "gateway"); } $returnValue['input'] = $input; return $returnValue; }
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); }
/** * Check float and decimal values * * @param $display display or not messages in and addAfterRedirect (true by default) * * @return input the data checked **/ function filterValues($display = true) { // MoYo : comment it because do not understand why filtering is disable // if (in_array('CommonDBRelation', class_parents($this))) { // return true; // } //Type mismatched fields $fails = array(); if (isset($this->input) && is_array($this->input) && count($this->input)) { foreach ($this->input as $key => $value) { $unset = false; $regs = array(); $searchOption = $this->getSearchOptionByField('field', $key); if (isset($searchOption['datatype']) && (is_null($value) || $value == '' || $value == 'NULL')) { switch ($searchOption['datatype']) { case 'date': case 'datetime': // don't use $unset', because this is not a failure $this->input[$key] = 'NULL'; break; } } else { if (isset($searchOption['datatype']) && !is_null($value) && $value != '' && $value != 'NULL') { switch ($searchOption['datatype']) { case 'integer': case 'count': case 'number': case 'decimal': $value = str_replace(',', '.', $value); if ($searchOption['datatype'] == 'decimal') { $this->input[$key] = floatval(Toolbox::cleanDecimal($value)); } else { $this->input[$key] = intval(Toolbox::cleanInteger($value)); } if (!is_numeric($this->input[$key])) { $unset = true; } break; case 'bool': if (!in_array($value, array(0, 1))) { $unset = true; } break; case 'ip': $address = new IPAddress(); if (!$address->setAddressFromString($value)) { $unset = true; } else { if (!$address->is_ipv4()) { $unset = true; } } break; case 'mac': preg_match("/([0-9a-fA-F]{1,2}([:-]|\$)){6}\$/", $value, $regs); if (empty($regs)) { $unset = true; } // Define the MAC address to lower to reduce complexity of SQL queries $this->input[$key] = strtolower($value); break; case 'date': case 'datetime': // Date is already "reformat" according to getDateFormat() $pattern = "/^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})"; $pattern .= "([_][01][0-9]|2[0-3]:[0-5][0-9]:[0-5]?[0-9])?/"; preg_match($pattern, $value, $regs); if (empty($regs)) { $unset = true; } break; case 'itemtype': //Want to insert an itemtype, but the associated class doesn't exists if (!class_exists($value)) { $unset = true; } case 'email': case 'string': if (strlen($value) > 255) { $this->input[$key] = substr($value, 0, 254); } break; default: //Plugins can implement their own checks if (!$this->checkSpecificValues($searchOption['datatype'], $value)) { $unset = true; } // Copy value if check have update it $this->input[$key] = $value; break; } } } if ($unset) { $fails[] = $searchOption['name']; unset($this->input[$key]); } } } if ($display && count($fails)) { //Display a message to indicate that one or more value where filtered //TRANS: %s is the list of the failed fields $message = sprintf(__('%1$s: %2$s'), __('At least one field has an incorrect value'), implode(',', $fails)); Session::addMessageAfterRedirect($message, INFO, true); } }