/** Generate bigdump : Get next IP address **/ function getNextIP() { global $IP; $IP[3] = max(1, ($IP[3] + 1) % 254); if ($IP[3] == 1) { $IP[2] = max(1, ($IP[2] + 1) % 255); if ($IP[2] == 0) { $IP[1] = max(1, ($IP[1] + 1) % 255); if ($IP[1] == 0) { $IP[0] = max(1, ($IP[0] + 1) % 255); } } } // Create IPnetwork if ($IP[3] == 1) { $net = new IPNetwork(); $net->add(array('entities_id' => 0, 'is_recursive' => 1, 'name' => $IP[0] . "." . $IP[1] . "." . $IP[2] . ".0", 'addressable' => 1, 'network' => $IP[0] . "." . $IP[1] . "." . $IP[2] . ".0/255.255.255.0", 'gateway' => $IP[0] . "." . $IP[1] . "." . $IP[2] . ".254")); } return array("ip" => $IP[0] . "." . $IP[1] . "." . $IP[2] . "." . $IP[3], "gateway" => $IP[0] . "." . $IP[1] . "." . $IP[2] . ".254", "subnet" => $IP[0] . "." . $IP[1] . "." . $IP[2] . ".0", "netwmask" => "255.255.255.0"); }
/** * @param $ipaddress IPAddress object **/ static function addIPAddress(IPAddress $ipaddress) { $linkObject = new self(); $input = array('ipaddresses_id' => $ipaddress->getID()); $entity = $ipaddress->getEntityID(); $ipnetworks_ids = IPNetwork::searchNetworksContainingIP($ipaddress, $entity); if ($ipnetworks_ids !== false) { // Beware that invalid IPaddresses don't have any valid address ! foreach (IPNetwork::searchNetworksContainingIP($ipaddress, $entity) as $ipnetworks_id) { $input['ipnetworks_id'] = $ipnetworks_id; $linkObject->add($input); } } }
/** * Get available display options array * * @since version 0.84 * * @return all the options **/ static function getAvailableDisplayOptions() { $options[__('Global displays')] = array('characteristics' => array('name' => __('Characteristics'), 'default' => true), 'internet' => array('name' => __('Internet information'), 'default' => true), 'dynamic_import' => array('name' => __('Automatic inventory'), 'default' => false)); $options[__('Common options')] = NetworkPortInstantiation::getGlobalInstantiationNetworkPortDisplayOptions(); $options[__('Internet information')] = array('names' => array('name' => NetworkName::getTypeName(2), 'default' => false), 'aliases' => array('name' => NetworkAlias::getTypeName(2), 'default' => false), 'ipaddresses' => array('name' => IPAddress::getTypeName(2), 'default' => true), 'ipnetworks' => array('name' => IPNetwork::getTypeName(2), 'default' => true)); foreach (self::getNetworkPortInstantiations() as $portType) { $portTypeName = $portType::getTypeName(0); $options[$portTypeName] = $portType::getInstantiationNetworkPortDisplayOptions(); } return $options; }
/** * Print the network name form * * @param $ID integer ID of the item * @param $options array * - target for the Form * - withtemplate template or basic computer * *@return Nothing (display) **/ function showForm($ID, $options = array()) { global $CFG_GLPI; $this->initForm($ID, $options); $recursiveItems = $this->recursivelyGetItems(); if (count($recursiveItems) != 0) { $lastItem = $recursiveItems[count($recursiveItems) - 1]; $options['entities_id'] = $lastItem->getField('entities_id'); } $this->showFormHeader($options); echo "<tr class='tab_bg_1'><td>"; if (count($recursiveItems) > 0) { $this->displayRecursiveItems($recursiveItems, 'Type'); } echo "</td>\n<td colspan='3'>"; if (!($ID > 0)) { echo "<input type='hidden' name='items_id' value='" . $this->fields["items_id"] . "'>\n"; echo "<input type='hidden' name='itemtype' value='" . $this->fields["itemtype"] . "'>\n"; } $this->displayRecursiveItems($recursiveItems, "Link"); if (count($recursiveItems) > 0 && $this->canUpdate()) { Html::showSimpleForm($this->getFormURL(), 'unaffect', _sx('button', 'Dissociate'), array('id' => $ID)); } echo "</td></tr>\n"; echo "<tr class='tab_bg_1'>"; echo "<td>" . __('Name') . "</td><td>\n"; Html::autocompletionTextField($this, "name"); echo "</td>\n"; echo "<td>" . FQDN::getTypeName(1) . "</td><td>"; Dropdown::show(getItemTypeForTable(getTableNameForForeignKeyField("fqdns_id")), array('value' => $this->fields["fqdns_id"], 'name' => 'fqdns_id', 'entity' => $this->getEntityID(), 'displaywith' => array('view'))); echo "</td>\n</tr>\n"; echo "<tr class='tab_bg_1'>"; echo "<td>" . IPAddress::getTypeName(Session::getPluralNumber()); IPAddress::showAddChildButtonForItemForm($this, '_ipaddresses'); echo "</td>"; echo "<td>"; IPAddress::showChildsForItemForm($this, '_ipaddresses'); echo "</td>\n"; echo "<td rowspan='3'>" . __('Comments') . "</td>"; echo "<td rowspan='3'><textarea cols='45' rows='4' name='comment' >" . $this->fields["comment"]; echo "</textarea></td>\n"; echo "</tr>"; echo "<tr class='tab_bg_1'>"; echo "<td>" . IPNetwork::getTypeName(Session::getPluralNumber()) . "</td><td>"; _e('IP network is not included in the database. However, you can see current available networks.'); echo "</td></tr>"; echo "<tr class='tab_bg_1'><td> </td><td>"; IPNetwork::showIPNetworkProperties($this->getEntityID()); echo "</td></tr>\n"; $this->showFormButtons($options); return true; }
/** * Update all Network Organisation * * @param $ADDTODISPLAYPREF **/ function updateNetworkFramework(&$ADDTODISPLAYPREF) { global $DB, $migration; $ADDTODISPLAYPREF['FQDN'] = array(11); $ADDTODISPLAYPREF['WifiNetwork'] = array(10); $ADDTODISPLAYPREF['NetworkPortMigration'] = array(); $ADDTODISPLAYPREF['IPNetwork'] = array(14, 10, 11, 12, 13); $ADDTODISPLAYPREF['NetworkName'] = array(12, 13); $optionIndex = 10; foreach (NetworkPortMigration::getMotives() as $key => $name) { $ADDTODISPLAYPREF['NetworkPortMigration'][] = $optionIndex++; } $migration->displayMessage(sprintf(__('Data migration - %s'), 'Network framework')); $originTables = array(); foreach (array('glpi_networkports', 'glpi_networkequipments') as $table) { $originTables[$table] = 'origin_' . $table; } if (!TableExists('origin_glpi_networkequipments')) { // remove of mac field from glpi_networkequipments is done at the end of migration // framework process if (!FieldExists('glpi_networkequipments', 'mac')) { // Nothing to be done : migration of NetworkPort already OK ! // But don't add display preference for NetworkPortMigration if none exists if (!TableExists('glpi_networkportmigrations')) { unset($ADDTODISPLAYPREF['NetworkPortMigration']); } $migration->displayWarning('Network Framework already migrated: nothing to be done !', false); return; } foreach ($originTables as $table => $originTable) { if (!TableExists($originTable) && TableExists($table)) { $migration->copyTable($table, $originTable); $migration->displayWarning("To be safe, we are working on {$originTable}. " . "It is a copy of {$table}", false); } } } // Remove all tables created by any previous migration $new_network_ports = array('glpi_fqdns', 'glpi_ipaddresses', 'glpi_ipaddresses_ipnetworks', 'glpi_ipnetworks', 'glpi_networkaliases', 'glpi_networknames', 'glpi_networkportaggregates', 'glpi_networkportdialups', 'glpi_networkportethernets', 'glpi_networkportlocals', 'glpi_networkportmigrations', 'glpi_networkportwifis', 'glpi_wifinetworks'); foreach ($new_network_ports as $table) { $migration->dropTable($table); } // Create the glpi_networkportmigrations that is a copy of origin_glpi_networkports $query = "CREATE TABLE `glpi_networkportmigrations` LIKE `origin_glpi_networkports`"; $DB->queryOrDie($query, "0.84 create glpi_networkportmigrations"); // And add the error motive fields foreach (NetworkPortMigration::getMotives() as $key => $name) { $migration->addField('glpi_networkportmigrations', $key, 'bool'); } $migration->migrationOneTable('glpi_networkportmigrations'); $migration->displayMessage(sprintf(__('Data migration - %s'), 'glpi_fqdns')); // Adding FQDN table if (!TableExists('glpi_fqdns')) { $query = "CREATE TABLE `glpi_fqdns` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `entities_id` int(11) NOT NULL DEFAULT '0',\n `is_recursive` tinyint(1) NOT NULL DEFAULT '0',\n `name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,\n `fqdn` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,\n `comment` text COLLATE utf8_unicode_ci,\n PRIMARY KEY (`id`),\n KEY `entities_id` (`entities_id`),\n KEY `name` (`name`),\n KEY `fqdn` (`fqdn`),\n KEY `is_recursive` (`is_recursive`)\n ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci"; $DB->queryOrDie($query, "0.84 create glpi_fqdns"); $fqdn = new FQDN(); // Then, populate it from domains (beware that "domains" can be FQDNs and Windows workgroups) $query = "SELECT DISTINCT LOWER(`name`) AS name, `comment`\n FROM `glpi_domains`"; foreach ($DB->request($query) as $domain) { $domainName = $domain['name']; // We ensure that domains have at least 1 dote to be sure it is not a Windows workgroup if (strpos($domainName, '.') !== false && FQDN::checkFQDN($domainName)) { $migration->insertInTable($fqdn->getTable(), array('entities_id' => 0, 'name' => $domainName, 'fqdn' => $domainName, 'comment' => $domain['comment'])); } } } $migration->displayMessage(sprintf(__('Data migration - %s'), 'glpi_ipaddresses')); // Adding IPAddress table if (!TableExists('glpi_ipaddresses')) { $query = "CREATE TABLE `glpi_ipaddresses` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `entities_id` int(11) NOT NULL DEFAULT '0',\n `items_id` int(11) NOT NULL DEFAULT '0',\n `itemtype` varchar(100) COLLATE utf8_unicode_ci NOT NULL,\n `version` tinyint unsigned DEFAULT '0',\n `name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,\n `binary_0` int unsigned NOT NULL DEFAULT '0',\n `binary_1` int unsigned NOT NULL DEFAULT '0',\n `binary_2` int unsigned NOT NULL DEFAULT '0',\n `binary_3` int unsigned NOT NULL DEFAULT '0',\n PRIMARY KEY (`id`),\n KEY `entities_id` (`entities_id`),\n KEY `textual` (`name`),\n KEY `binary` (`binary_0`, `binary_1`, `binary_2`, `binary_3`),\n KEY `item` (`itemtype`, `items_id`)\n ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci"; $DB->queryOrDie($query, "0.84 create glpi_ipaddresses"); } $migration->displayMessage(sprintf(__('Change of the database layout - %s'), 'glpi_wifinetworks')); // Adding WifiNetwork table if (!TableExists('glpi_wifinetworks')) { $query = "CREATE TABLE `glpi_wifinetworks` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `entities_id` int(11) NOT NULL DEFAULT '0',\n `is_recursive` tinyint(1) NOT NULL DEFAULT '0',\n `name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,\n `essid` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,\n `mode` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL\n COMMENT 'ad-hoc, access_point',\n `comment` text COLLATE utf8_unicode_ci,\n PRIMARY KEY (`id`),\n KEY `entities_id` (`entities_id`),\n KEY `essid` (`essid`),\n KEY `name` (`name`)\n ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci"; $DB->queryOrDie($query, "0.84 create glpi_wifinetworks"); } $migration->displayMessage(sprintf(__('Data migration - %s'), "glpi_ipnetworks")); // Adding IPNetwork table if (!TableExists('glpi_ipnetworks')) { $query = "CREATE TABLE `glpi_ipnetworks` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `entities_id` int(11) NOT NULL DEFAULT '0',\n `is_recursive` tinyint(1) NOT NULL DEFAULT '0',\n `ipnetworks_id` int(11) NOT NULL DEFAULT '0',\n `completename` text COLLATE utf8_unicode_ci,\n `level` int(11) NOT NULL DEFAULT '0',\n `ancestors_cache` longtext COLLATE utf8_unicode_ci,\n `sons_cache` longtext COLLATE utf8_unicode_ci,\n `addressable` tinyint(1) NOT NULL DEFAULT '0',\n `version` tinyint unsigned DEFAULT '0',\n `name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,\n `address` varchar(40) COLLATE utf8_unicode_ci DEFAULT NULL,\n `address_0` int unsigned NOT NULL DEFAULT '0',\n `address_1` int unsigned NOT NULL DEFAULT '0',\n `address_2` int unsigned NOT NULL DEFAULT '0',\n `address_3` int unsigned NOT NULL DEFAULT '0',\n `netmask` varchar(40) COLLATE utf8_unicode_ci DEFAULT NULL,\n `netmask_0` int unsigned NOT NULL DEFAULT '0',\n `netmask_1` int unsigned NOT NULL DEFAULT '0',\n `netmask_2` int unsigned NOT NULL DEFAULT '0',\n `netmask_3` int unsigned NOT NULL DEFAULT '0',\n `gateway` varchar(40) COLLATE utf8_unicode_ci DEFAULT NULL,\n `gateway_0` int unsigned NOT NULL DEFAULT '0',\n `gateway_1` int unsigned NOT NULL DEFAULT '0',\n `gateway_2` int unsigned NOT NULL DEFAULT '0',\n `gateway_3` int unsigned NOT NULL DEFAULT '0',\n `comment` text COLLATE utf8_unicode_ci,\n PRIMARY KEY (`id`),\n KEY `network_definition` (`entities_id`,`address`,`netmask`),\n KEY `address` (`address_0`, `address_1`, `address_2`, `address_3`),\n KEY `netmask` (`netmask_0`, `netmask_1`, `netmask_2`, `netmask_3`),\n KEY `gateway` (`gateway_0`, `gateway_1`, `gateway_2`, `gateway_3`),\n KEY `name` (`name`)\n ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci"; $DB->queryOrDie($query, "0.84 create glpi_ipnetworks"); // Retrieve all the networks from the current network ports and add them to the IPNetworks $query = "SELECT DISTINCTROW INET_NTOA(INET_ATON(`ip`)&INET_ATON(`netmask`)) AS address,\n `netmask`, `gateway`, `entities_id`\n FROM `origin_glpi_networkports`\n ORDER BY `gateway` DESC"; $address = new IPAddress(); $netmask = new IPNetmask(); $gateway = new IPAddress(); $network = new IPNetwork(); foreach ($DB->request($query) as $entry) { $address = $entry['address']; $netmask = $entry['netmask']; $gateway = $entry['gateway']; $entities_id = $entry['entities_id']; if (empty($address) || $address == '0.0.0.0' || empty($netmask) || $netmask == '0.0.0.0' || $netmask == '255.255.255.255') { continue; } if ($gateway == '0.0.0.0') { $gateway = ''; } $networkDefinition = "{$address}/{$netmask}"; $networkName = $networkDefinition . (empty($gateway) ? "" : " - " . $gateway); $input = array('entities_id' => $entities_id, 'name' => $networkName, 'network' => $networkDefinition, 'gateway' => $gateway, 'ipnetworks_id' => 0, 'addressable' => 1, 'completename' => $networkName, 'level' => 1); $preparedInput = $network->prepareInput($input); if (is_array($preparedInput['input'])) { $input = $preparedInput['input']; if (isset($preparedInput['error'])) { $query = "SELECT id, items_id, itemtype\n FROM origin_glpi_networkports\n WHERE INET_NTOA(INET_ATON(`ip`)&INET_ATON(`netmask`)) = '{$address}'\n AND `netmask` = '{$netmask}'\n AND `gateway` = '{$gateway}'\n AND `entities_id` = '{$entities_id}'"; $result = $DB->query($query); foreach ($DB->request($query) as $data) { addNetworkPortMigrationError($data['id'], 'invalid_gateway'); logNetworkPortError('network warning', $data['id'], $data['itemtype'], $data['items_id'], $preparedInput['error']); } } $migration->insertInTable($network->getTable(), $input); } else { if (isset($preparedInput['error'])) { $query = "SELECT id, items_id, itemtype\n FROM origin_glpi_networkports\n WHERE INET_NTOA(INET_ATON(`ip`)&INET_ATON(`netmask`)) = '" . $entry['address'] . "'\n AND `netmask` = '{$netmask}'\n AND `gateway` = '{$gateway}'\n AND `entities_id` = '{$entities_id}'"; $result = $DB->query($query); foreach ($DB->request($query) as $data) { addNetworkPortMigrationError($data['id'], 'invalid_network'); logNetworkPortError('network error', $data['id'], $data['itemtype'], $data['items_id'], $preparedInput['error']); } } } } } $migration->displayMessage(sprintf(__('Data migration - %s'), "glpi_ipnetworks_vlans")); // Adding IPNetwork table if (!TableExists('glpi_ipnetworks_vlans')) { $query = "CREATE TABLE `glpi_ipnetworks_vlans` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `ipnetworks_id` int(11) NOT NULL DEFAULT '0',\n `vlans_id` int(11) NOT NULL DEFAULT '0',\n PRIMARY KEY (`id`),\n UNIQUE KEY `link` (`ipnetworks_id`, `vlans_id`)\n ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"; $DB->queryOrDie($query, "0.84 create glpi_ipnetworks_vlans"); } $migration->displayMessage(sprintf(__('Data migration - %s'), "glpi_networknames")); // Adding NetworkName table if (!TableExists('glpi_networknames')) { $query = "CREATE TABLE `glpi_networknames` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `entities_id` int(11) NOT NULL DEFAULT '0',\n `items_id` int(11) NOT NULL DEFAULT '0',\n `itemtype` varchar(100) COLLATE utf8_unicode_ci NOT NULL,\n `name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,\n `comment` text COLLATE utf8_unicode_ci,\n `fqdns_id` int(11) NOT NULL DEFAULT '0',\n `is_deleted` tinyint(1) NOT NULL DEFAULT '0',\n `is_dynamic` tinyint(1) NOT NULL DEFAULT '0',\n PRIMARY KEY (`id`),\n KEY `entities_id` (`entities_id`),\n KEY `FQDN` (`name`,`fqdns_id`),\n KEY `name` (`name`),\n KEY `item` (`itemtype`, `items_id`),\n KEY `fqdns_id` (`fqdns_id`)\n ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci"; $DB->queryOrDie($query, "0.84 create glpi_networknames"); // Retrieve all the networks from the current network ports and add them to the IPNetworks $query = "SELECT `ip`, `id`, `entities_id`, `itemtype`, `items_id`\n FROM `origin_glpi_networkports`\n WHERE `ip` <> ''"; foreach ($DB->request($query) as $entry) { if (empty($entry["ip"])) { continue; } createNetworkNameFromItem('NetworkPort', $entry['id'], $entry['items_id'], $entry['itemtype'], $entry['entities_id'], $entry["ip"]); } } $migration->displayMessage(sprintf(__('Change of the database layout - %s'), "glpi_networkaliases")); // Adding NetworkAlias table if (!TableExists('glpi_networkaliases')) { $query = "CREATE TABLE `glpi_networkaliases` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `entities_id` int(11) NOT NULL DEFAULT '0',\n `networknames_id` int(11) NOT NULL DEFAULT '0',\n `name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,\n `fqdns_id` int(11) NOT NULL DEFAULT '0',\n `comment` text COLLATE utf8_unicode_ci,\n PRIMARY KEY (`id`),\n KEY `entities_id` (`entities_id`),\n KEY `name` (`name`),\n KEY `networknames_id` (`networknames_id`)\n ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci"; $DB->queryOrDie($query, "0.84 create glpi_networkaliases"); } $migration->displayMessage(sprintf(__('Data migration - %s'), "glpi_ipaddresses_ipnetworks")); // Adding IPAddress_IPNetwork table if (!TableExists('glpi_ipaddresses_ipnetworks')) { $query = "CREATE TABLE `glpi_ipaddresses_ipnetworks` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `ipaddresses_id` int(11) NOT NULL DEFAULT '0',\n `ipnetworks_id` int(11) NOT NULL DEFAULT '0',\n PRIMARY KEY (`id`),\n UNIQUE KEY `unicity` (`ipaddresses_id`,`ipnetworks_id`),\n KEY `ipnetworks_id` (`ipnetworks_id`),\n KEY `ipaddresses_id` (`ipaddresses_id`)\n ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"; $DB->queryOrDie($query, "0.84 create glpi_ipaddresses_ipnetworks"); } $migration->displayMessage(sprintf(__('Change of the database layout - %s'), "glpi_networkinterfaces")); // Update NetworkPorts $migration->addField('glpi_networkports', 'instantiation_type', 'string', array('after' => 'name', 'update' => "'NetworkPortEthernet'")); $migration->displayMessage(sprintf(__('Data migration - %s'), "glpi_networkports")); // Retrieve all the networks from the current network ports and add them to the IPNetwork $query = "SELECT *\n FROM `glpi_networkinterfaces`"; foreach ($DB->request($query) as $entry) { $instantiation_type = ""; switch ($entry['name']) { case 'Local': $instantiation_type = "NetworkPortLocal"; break; case 'Ethernet': $instantiation_type = "NetworkPortEthernet"; break; case 'Wifi': $instantiation_type = "NetworkPortWifi"; break; case 'Dialup': $instantiation_type = "NetworkPortDialup"; break; default: if (preg_match('/TX/i', $entry['name']) || preg_match('/SX/i', $entry['name']) || preg_match('/Ethernet/i', $entry['name'])) { $instantiation_type = "NetworkPortEthernet"; } break; } /// In case of unknown Interface Type, we should have to set instantiation_type to '' /// Thus we should be able to convert it later to correct type (ethernet, wifi, loopback ...) if (!empty($instantiation_type)) { $query = "UPDATE `glpi_networkports`\n SET `instantiation_type` = '{$instantiation_type}'\n WHERE `id` IN (SELECT `id`\n FROM `origin_glpi_networkports`\n WHERE `networkinterfaces_id` = '" . $entry['id'] . "')"; $DB->queryOrDie($query, "0.84 update instantiation_type field of glpi_networkports"); // Clear $instantiation_type for next check inside the loop unset($instantiation_type); } } foreach (array('ip', 'gateway', 'netmask', 'netpoints_id', 'networkinterfaces_id', 'subnet') as $field) { $migration->dropField('glpi_networkports', $field); } foreach (array('ip', 'mac') as $field) { $migration->dropField('glpi_networkequipments', $field); } $migration->displayMessage(sprintf(__('Data migration - %s'), 'Index mac field and transform address mac to lower')); $query = "UPDATE `glpi_networkports`\n SET `mac` = LOWER(`mac`)"; $DB->queryOrDie($query, "0.84 transforme MAC to lower case"); $migration->addKey('glpi_networkports', 'mac'); $migration->displayMessage(sprintf(__('Data migration - %s'), 'Update migration of interfaces errors')); $query = "SELECT id\n FROM `glpi_networkports`\n WHERE `instantiation_type` = ''"; foreach ($DB->request($query) as $networkPortID) { addNetworkPortMigrationError($networkPortID['id'], 'unknown_interface_type'); } $migration->displayMessage(sprintf(__('Change of the database layout - %s'), "glpi_networkportethernets")); // Adding NetworkPortEthernet table if (!TableExists('glpi_networkportethernets')) { $query = "CREATE TABLE `glpi_networkportethernets` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `networkports_id` int(11) NOT NULL DEFAULT '0',\n `items_devicenetworkcards_id` int(11) NOT NULL DEFAULT '0',\n `netpoints_id` int(11) NOT NULL DEFAULT '0',\n `type` varchar(10) COLLATE utf8_unicode_ci DEFAULT '' COMMENT 'T, LX, SX',\n `speed` int(11) NOT NULL DEFAULT '10' COMMENT 'Mbit/s: 10, 100, 1000, 10000',\n PRIMARY KEY (`id`),\n UNIQUE KEY `networkports_id` (`networkports_id`),\n KEY `card` (`items_devicenetworkcards_id`),\n KEY `netpoint` (`netpoints_id`),\n KEY `type` (`type`),\n KEY `speed` (`speed`)\n ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci"; $DB->queryOrDie($query, "0.84 create glpi_networkportethernets"); $port = new NetworkPortEthernet(); updateNetworkPortInstantiation($port, array('`netpoints_id`' => 'netpoints_id'), true); } $migration->displayMessage(sprintf(__('Change of the database layout - %s'), "glpi_networkportwifis")); // Adding NetworkPortWifi table if (!TableExists('glpi_networkportwifis')) { $query = "CREATE TABLE `glpi_networkportwifis` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `networkports_id` int(11) NOT NULL DEFAULT '0',\n `items_devicenetworkcards_id` int(11) NOT NULL DEFAULT '0',\n `wifinetworks_id` int(11) NOT NULL DEFAULT '0',\n `networkportwifis_id` int(11) NOT NULL DEFAULT '0'\n COMMENT 'only usefull in case of Managed node',\n `version` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL\n COMMENT 'a, a/b, a/b/g, a/b/g/n, a/b/g/n/y',\n `mode` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL\n COMMENT 'ad-hoc, managed, master, repeater, secondary, monitor, auto',\n PRIMARY KEY (`id`),\n UNIQUE KEY `networkports_id` (`networkports_id`),\n KEY `card` (`items_devicenetworkcards_id`),\n KEY `essid` (`wifinetworks_id`),\n KEY `version` (`version`),\n KEY `mode` (`mode`)\n ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci"; $DB->queryOrDie($query, "0.84 create glpi_networkportwifis"); $port = new NetworkPortWifi(); updateNetworkPortInstantiation($port, array(), true); } $migration->displayMessage(sprintf(__('Change of the database layout - %s'), "glpi_networkportlocals")); // Adding NetworkPortLocal table if (!TableExists('glpi_networkportlocals')) { $query = "CREATE TABLE `glpi_networkportlocals` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `networkports_id` int(11) NOT NULL DEFAULT '0',\n PRIMARY KEY (`id`),\n UNIQUE KEY `networkports_id` (`networkports_id`)\n ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci"; $DB->queryOrDie($query, "0.84 create glpi_networkportlocals"); $port = new NetworkPortLocal(); updateNetworkPortInstantiation($port, array(), false); } $migration->displayMessage(sprintf(__('Change of the database layout - %s'), "glpi_networkportdialups")); // Adding NetworkPortDialup table if (!TableExists('glpi_networkportdialups')) { $query = "CREATE TABLE `glpi_networkportdialups` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `networkports_id` int(11) NOT NULL DEFAULT '0',\n PRIMARY KEY (`id`),\n UNIQUE KEY `networkports_id` (`networkports_id`)\n ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci"; $DB->queryOrDie($query, "0.84 create glpi_networkportdialups"); $port = new NetworkPortDialup(); updateNetworkPortInstantiation($port, array(), true); } $migration->displayMessage(sprintf(__('Change of the database layout - %s'), "glpi_networkportaggregates")); // Adding NetworkPortAggregate table if (!TableExists('glpi_networkportaggregates')) { $query = "CREATE TABLE `glpi_networkportaggregates` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `networkports_id` int(11) NOT NULL DEFAULT '0',\n `networkports_id_list` TEXT DEFAULT NULL\n COMMENT 'array of associated networkports_id',\n PRIMARY KEY (`id`),\n UNIQUE KEY `networkports_id` (`networkports_id`)\n ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci"; $DB->queryOrDie($query, "0.84 create glpi_networkportaggregates"); // Transform NetworkEquipment local MAC address as a networkport that aggregates all ports $query = "SELECT *\n FROM `origin_glpi_networkequipments`\n WHERE `mac` != ''\n OR `ip` != ''"; $port_input = array('itemtype' => 'NetworkEquipment', 'logical_number' => '0', 'name' => 'management', 'instantiation_type' => 'NetworkPortAggregate'); foreach ($DB->request($query) as $equipment) { $networkequipments_id = $equipment['id']; $query = "SELECT `id`, `ip`, `mac`\n FROM `origin_glpi_networkports`\n WHERE `itemtype` = 'NetworkEquipment'\n AND `items_id` = '{$networkequipments_id}'\n AND (`ip` = '" . $equipment['ip'] . "'\n OR `mac` = '" . $equipment['mac'] . "')"; $both = array(); $mac = array(); $ip = array(); foreach ($DB->request($query) as $ports) { if ($ports['ip'] == $equipment['ip']) { if ($ports['mac'] == $equipment['mac']) { $both[] = $ports['id']; } else { $ip[] = $ports['id']; } } else { $mac[] = $ports['id']; } } if (count($both) != 1) { // Only add a NetworkPort if there is 0 or more than one element ! $port_input['items_id'] = $networkequipments_id; $port_input['entities_id'] = $equipment['entities_id']; $port_input['is_recursive'] = $equipment['is_recursive']; $port_input['mac'] = strtolower($equipment['mac']); $networkports_id = $migration->insertInTable('glpi_networkports', $port_input); $aggregate_input = array(); $aggregate_input['networkports_id'] = $networkports_id; $aggregate_input['networkports_id_list'] = exportArrayToDB($both); $migration->insertInTable('glpi_networkportaggregates', $aggregate_input); createNetworkNameFromItem('NetworkPort', $networkports_id, $equipment['id'], 'NetworkEquipment', $equipment['entities_id'], $equipment['ip']); foreach ($both as $aggregated_networkports_id) { $query = "DELETE\n FROM `glpi_networknames`\n WHERE `itemtype` = 'NetworkPort'\n AND `items_id` = '{$aggregated_networkports_id}'"; $DB->query($query); $query = "UPDATE `glpi_networkports`\n SET `mac` = ''\n WHERE `id` = '{$aggregated_networkports_id}'"; $DB->query($query); } } } } $migration->displayMessage(sprintf(__('Change of the database layout - %s'), "glpi_networkportaliases")); // Adding NetworkPortAlias table if (!TableExists('glpi_networkportaliases')) { $query = "CREATE TABLE `glpi_networkportaliases` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `networkports_id` int(11) NOT NULL DEFAULT '0',\n `networkports_id_alias` int(11) NOT NULL DEFAULT '0',\n PRIMARY KEY (`id`),\n UNIQUE KEY `networkports_id` (`networkports_id`),\n KEY `networkports_id_alias` (`networkports_id_alias`)\n ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci"; $DB->queryOrDie($query, "0.84 create glpi_networkportaliases"); // New element, so, we don't need to create items } $migration->addField('glpi_networkports_vlans', 'tagged', 'bool', array('value' => '0')); $migration->addField('glpi_vlans', 'entities_id', 'integer', array('value' => '0', 'after' => 'id')); $migration->addKey('glpi_vlans', 'entities_id'); $migration->addField('glpi_vlans', 'is_recursive', 'bool', array('value' => '0', 'after' => 'entities_id', 'update' => '1')); $migration->addKey('glpi_vlans', 'tag'); $migration->displayMessage(sprintf(__('Data migration - %s'), 'Update connections between IPAddress and IPNetwork')); // Here, we are sure that there is only IPv4 addresses. So, the SQL requests are simplified $query = "SELECT `id`, `address_3`, `netmask_3`\n FROM `glpi_ipnetworks`"; if ($network_result = $DB->query($query)) { unset($query); while ($ipnetwork_row = $DB->fetch_assoc($network_result)) { $ipnetworks_id = $ipnetwork_row['id']; $netmask = floatval($ipnetwork_row['netmask_3']); $address = floatval($ipnetwork_row['address_3']) & $netmask; $query = "SELECT `id`\n FROM `glpi_ipaddresses`\n WHERE (`glpi_ipaddresses`.`binary_3` & '{$netmask}') = {$address}\n AND `glpi_ipaddresses`.`version` = '4'\n GROUP BY `items_id`"; if ($ipaddress_result = $DB->query($query)) { unset($query); while ($link = $DB->fetch_assoc($ipaddress_result)) { $query = "INSERT INTO `glpi_ipaddresses_ipnetworks`\n (`ipaddresses_id`, `ipnetworks_id`)\n VALUES ('" . $link['id'] . "', '{$ipnetworks_id}')"; $DB->query($query); unset($query); } } } } $migration->displayMessage(sprintf(__('Change of the database layout - %s'), 'Drop table glpi_networkportmigrations if empty')); if (countElementsInTable("glpi_networkportmigrations") == 0) { $migration->dropTable("glpi_networkportmigrations"); $migration->dropTable("glpi_networkportinterfaces"); unset($ADDTODISPLAYPREF['NetworkPortMigration']); } // We migrate glpi_networkequipments: mac field presence is used to check if framework has // already been migrated $migration->migrationOneTable('glpi_networkequipments'); foreach ($originTables as $table) { $migration->dropTable($table); } }
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); }
/** * @param $row HTMLTableRow object (default NULL) * @param $item CommonDBTM object (default NULL) * @param $father HTMLTableCell object (default NULL) * @param $options array **/ static function getHTMLTableCellsForItem(HTMLTableRow $row = NULL, CommonDBTM $item = NULL, HTMLTableCell $father = NULL, array $options = array()) { global $DB, $CFG_GLPI; if ($item !== NULL && $item->getType() == 'IPNetwork') { $queries = array(); foreach ($CFG_GLPI["networkport_types"] as $itemtype) { $table = getTableForItemType($itemtype); $queries[] = "(SELECT ADDR.`binary_0` AS binary_0,\n ADDR.`binary_1` AS binary_1,\n ADDR.`binary_2` AS binary_2,\n ADDR.`binary_3` AS binary_3,\n ADDR.`name` AS ip,\n ADDR.`id` AS id,\n ADDR.`itemtype` AS addr_item_type,\n ADDR.`items_id` AS addr_item_id,\n `glpi_entities`.`completename` AS entity,\n NAME.`id` AS name_id,\n PORT.`id` AS port_id,\n ITEM.`id` AS item_id,\n '{$itemtype}' AS item_type\n FROM `glpi_ipaddresses_ipnetworks` AS LINK\n JOIN `glpi_ipaddresses` AS ADDR ON (ADDR.`id` = LINK.`ipaddresses_id`\n AND ADDR.`itemtype` = 'NetworkName'\n AND ADDR.`is_deleted` = '0')\n LEFT JOIN `glpi_entities` ON (ADDR.`entities_id` = `glpi_entities`.`id`)\n JOIN `glpi_networknames` AS NAME ON (NAME.`id` = ADDR.`items_id`\n AND NAME.`itemtype` = 'NetworkPort')\n JOIN `glpi_networkports` AS PORT ON (NAME.`items_id` = PORT.`id`\n AND PORT.`itemtype` = '{$itemtype}')\n JOIN `{$table}` AS ITEM ON (ITEM.`id` = PORT.`items_id`)\n WHERE LINK.`ipnetworks_id` = '" . $item->getID() . "')"; } $queries[] = "(SELECT ADDR.`binary_0` AS binary_0,\n ADDR.`binary_1` AS binary_1,\n ADDR.`binary_2` AS binary_2,\n ADDR.`binary_3` AS binary_3,\n ADDR.`name` AS ip,\n ADDR.`id` AS id,\n ADDR.`itemtype` AS addr_item_type,\n ADDR.`items_id` AS addr_item_id,\n `glpi_entities`.`completename` AS entity,\n NAME.`id` AS name_id,\n PORT.`id` AS port_id,\n NULL AS item_id,\n NULL AS item_type\n FROM `glpi_ipaddresses_ipnetworks` AS LINK\n JOIN `glpi_ipaddresses` AS ADDR ON (ADDR.`id` = LINK.`ipaddresses_id`\n AND ADDR.`itemtype` = 'NetworkName'\n AND ADDR.`is_deleted` = '0')\n LEFT JOIN `glpi_entities` ON (ADDR.`entities_id` = `glpi_entities`.`id`)\n JOIN `glpi_networknames` AS NAME ON (NAME.`id` = ADDR.`items_id`\n AND NAME.`itemtype` = 'NetworkPort')\n JOIN `glpi_networkports` AS PORT\n ON (NAME.`items_id` = PORT.`id`\n AND PORT.`itemtype`\n NOT IN ('" . implode("', '", $CFG_GLPI["networkport_types"]) . "'))\n WHERE LINK.`ipnetworks_id` = '" . $item->getID() . "')"; $queries[] = "(SELECT ADDR.`binary_0` AS binary_0,\n ADDR.`binary_1` AS binary_1,\n ADDR.`binary_2` AS binary_2,\n ADDR.`binary_3` AS binary_3,\n ADDR.`name` AS ip,\n ADDR.`id` AS id,\n ADDR.`itemtype` AS addr_item_type,\n ADDR.`items_id` AS addr_item_id,\n `glpi_entities`.`completename` AS entity,\n NAME.`id` AS name_id,\n NULL AS port_id,\n NULL AS item_id,\n NULL AS item_type\n FROM `glpi_ipaddresses_ipnetworks` AS LINK\n JOIN `glpi_ipaddresses` AS ADDR ON (ADDR.`id` = LINK.`ipaddresses_id`\n AND ADDR.`itemtype` = 'NetworkName'\n AND ADDR.`is_deleted` = '0')\n LEFT JOIN `glpi_entities` ON (ADDR.`entities_id` = `glpi_entities`.`id`)\n JOIN `glpi_networknames` AS NAME ON (NAME.`id` = ADDR.`items_id`\n AND NAME.`itemtype` != 'NetworkPort')\n WHERE LINK.`ipnetworks_id` = '" . $item->getID() . "')"; $queries[] = "(SELECT ADDR.`binary_0` AS binary_0,\n ADDR.`binary_1` AS binary_1,\n ADDR.`binary_2` AS binary_2,\n ADDR.`binary_3` AS binary_3,\n ADDR.`name` AS ip,\n ADDR.`id` AS id,\n ADDR.`itemtype` AS addr_item_type,\n ADDR.`items_id` AS addr_item_id,\n `glpi_entities`.`completename` AS entity,\n NULL AS name_id,\n NULL AS port_id,\n NULL AS item_id,\n NULL AS item_type\n FROM `glpi_ipaddresses_ipnetworks` AS LINK\n JOIN `glpi_ipaddresses` AS ADDR ON (ADDR.`id` = LINK.`ipaddresses_id`\n AND ADDR.`itemtype` != 'NetworkName'\n AND ADDR.`is_deleted` = '0')\n LEFT JOIN `glpi_entities` ON (ADDR.`entities_id` = `glpi_entities`.`id`)\n WHERE LINK.`ipnetworks_id` = '" . $item->getID() . "')"; $query = implode('UNION ', $queries); if ($options['order'] == 'ip' || $options['order'] == 'itemtype') { $query .= " ORDER BY binary_0, binary_1, binary_2, binary_3"; } if (isset($options['SQL_options'])) { $query .= "\n" . $options['SQL_options']; } $canedit = isset($options['canedit']) && $options['canedit']; $options['createRow'] = false; $address = new self(); $ipaddress = new self(); $networkname = new NetworkName(); $networkport = new NetworkPort(); $item = NULL; foreach ($DB->request($query) as $line) { unset($row); if ($options['order'] == 'itemtype' && !empty($line['item_type'])) { $row = $options['group_' . $line['item_type']]->createRow(); } if (!isset($row)) { $row = $options['group_None']->createRow(); } $ip_header = $row->getGroup()->getSuperHeaderByName('IP Address'); $item_header = $row->getGroup()->getHeaderByName('Item', 'Item'); $port_header = $row->getGroup()->getHeaderByName('Item', 'NetworkPort'); $name_header = $row->getGroup()->getHeaderByName('Item', 'NetworkName'); $entity_header = $row->getGroup()->getHeaderByName('Item', 'Entity'); $row->addCell($ip_header, $line['ip'], $father); if (!empty($line['name_id'])) { $networkname->getFromDB($line['name_id']); $row->addCell($name_header, $networkname->getLink(), $father); if (!empty($line['port_id'])) { $networkport->getFromDB($line['port_id']); $row->addCell($port_header, $networkport->getLink(), $father); if (!empty($line['item_id']) && !empty($line['item_type'])) { $itemtype = $line['item_type']; $item = new $itemtype(); $item->getFromDB($line['item_id']); $row->addCell($item_header, $item->getLink(), $father); } } $row->addCell($entity_header, $line['entity'], $father); } else { if (!empty($line['addr_item_id']) && !empty($line['addr_item_type'])) { $itemtype = $line['addr_item_type']; $item = new $itemtype(); $item->getFromDB($line['addr_item_id']); if ($item instanceof CommonDBChild) { $items = $item->recursivelyGetItems(); $elements = array($item->getLink()); foreach ($items as $item_) { $elements[] = $item_->getLink(); } $row->addCell($item_header, implode(' > ', $elements), $father); } else { $row->addCell($item_header, $item->getLink(), $father); } $row->addCell($entity_header, $line['entity'], $father); } } } } else { if (isset($options['dont_display']['IPAddress'])) { return; } $header = $row->getGroup()->getHeaderByName('Internet', __CLASS__); if (!$header) { return; } if (empty($item)) { if (empty($father)) { return; } $item = $father->getItem(); } $query = "SELECT `id`\n FROM `glpi_ipaddresses`\n WHERE `items_id` = '" . $item->getID() . "'\n AND `itemtype` = '" . $item->getType() . "'\n AND `is_deleted` = '0'"; $canedit = isset($options['canedit']) && $options['canedit']; $createRow = isset($options['createRow']) && $options['createRow']; $options['createRow'] = false; $address = new self(); foreach ($DB->request($query) as $ipaddress) { if ($address->getFromDB($ipaddress['id'])) { if ($createRow) { $row = $row->createRow(); } $content = $address->fields['name']; $this_cell = $row->addCell($header, $content, $father); if (isset($options['display_isDynamic']) && $options['display_isDynamic']) { $dyn_header = $row->getGroup()->getHeaderByName('Internet', __CLASS__ . '_dynamic'); $this_cell = $row->addCell($dyn_header, Dropdown::getYesNo($address->fields['is_dynamic']), $this_cell); } IPNetwork::getHTMLTableCellsForItem($row, $address, $this_cell, $options); } } } }
GLPI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GLPI. If not, see <http://www.gnu.org/licenses/>. -------------------------------------------------------------------------- */ /** @file * @brief */ include '../inc/includes.php'; header("Content-Type: text/html; charset=UTF-8"); Html::header_nocache(); $network = new IPNetwork(); if ($network->can($_POST['ipnetworks_id'], 'r')) { echo "<br>\n"; echo "<a href='" . $network->getLinkURL() . "'>" . $network->fields['completename'] . "</a><br>\n"; $address = $network->getAddress()->getTextual(); $netmask = $network->getNetmask()->getTextual(); $gateway = $network->getGateway()->getTextual(); $start = new IPAddress(); $end = new IPAddress(); $network->computeNetworkRange($start, $end); //TRANS: %1$s is address, %2$s is netmask printf(__('IP network: %1$s/%2$s') . "<br>\n", $address, $netmask); printf(__('First/last addresses: %1$s/%2$s'), $start->getTextual(), $end->getTextual()); if (!empty($gateway)) { echo "<br>\n"; printf(__('Gateway: %s') . "\n", $gateway);
/** * @param $port IPNetwork object **/ static function showForIPNetwork(IPNetwork $port) { global $DB, $CFG_GLPI; $ID = $port->getID(); if (!$port->can($ID, READ)) { return false; } $canedit = $port->canEdit($ID); $rand = mt_rand(); $query = "SELECT `" . self::getTable() . "`.id as assocID,\n `glpi_vlans`.*\n FROM `" . self::getTable() . "`\n LEFT JOIN `glpi_vlans`\n ON (`" . self::getTable() . "`.`vlans_id` = `glpi_vlans`.`id`)\n WHERE `ipnetworks_id` = '{$ID}'"; $result = $DB->query($query); $vlans = array(); $used = array(); if ($number = $DB->numrows($result)) { while ($line = $DB->fetch_assoc($result)) { $used[$line["id"]] = $line["id"]; $vlans[$line["assocID"]] = $line; } } if ($canedit) { echo "<div class='firstbloc'>\n"; echo "<form method='post' action='" . static::getFormURL() . "'>\n"; echo "<table class='tab_cadre_fixe'>\n"; echo "<tr><th>" . __('Associate a VLAN') . "</th></tr>"; echo "<tr class='tab_bg_1'><td class='center'>"; echo "<input type='hidden' name='ipnetworks_id' value='{$ID}'>"; Vlan::dropdown(array('used' => $used)); echo " <input type='submit' name='add' value='" . _sx('button', 'Associate') . "' class='submit'>"; echo "</td></tr>\n"; echo "</table>\n"; Html::closeForm(); echo "</div>\n"; } echo "<div class='spaced'>"; if ($canedit && $number) { Html::openMassiveActionsForm('mass' . __CLASS__ . $rand); $massiveactionparams = array('num_displayed' => $number, 'container' => 'mass' . __CLASS__ . $rand); Html::showMassiveActions($massiveactionparams); } echo "<table class='tab_cadre_fixehov'>"; $header_begin = "<tr>"; $header_top = ''; $header_bottom = ''; $header_end = ''; if ($canedit && $number) { $header_top .= "<th width='10'>" . Html::getCheckAllAsCheckbox('mass' . __CLASS__ . $rand); $header_top .= "</th>"; $header_bottom .= "<th width='10'>" . Html::getCheckAllAsCheckbox('mass' . __CLASS__ . $rand); $header_bottom .= "</th>"; } $header_end .= "<th>" . __('Name') . "</th>"; $header_end .= "<th>" . __('Entity') . "</th>"; $header_end .= "<th>" . __('ID TAG') . "</th>"; $header_end .= "</tr>"; echo $header_begin . $header_top . $header_end; $used = array(); foreach ($vlans as $data) { echo "<tr class='tab_bg_1'>"; if ($canedit) { echo "<td>"; Html::showMassiveActionCheckBox(__CLASS__, $data["assocID"]); echo "</td>"; } $name = $data["name"]; if ($_SESSION["glpiis_ids_visible"] || empty($data["name"])) { $name = sprintf(__('%1$s (%2$s)'), $name, $data["id"]); } echo "<td class='center b'>\n <a href='" . $CFG_GLPI["root_doc"] . "/front/vlan.form.php?id=" . $data["id"] . "'>" . $name . "</a>"; echo "</td>"; echo "<td class='center'>" . Dropdown::getDropdownName("glpi_entities", $data["entities_id"]); echo "<td class='numeric'>" . $data["tag"] . "</td>"; echo "</tr>"; } if ($number) { echo $header_begin . $header_bottom . $header_end; } echo "</table>"; if ($canedit && $number) { $massiveactionparams['ontop'] = false; Html::showMassiveActions($massiveactionparams); Html::closeForm(); } echo "</div>"; }
This file is part of GLPI. GLPI is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. GLPI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GLPI. If not, see <http://www.gnu.org/licenses/>. -------------------------------------------------------------------------- */ /** @file * @brief */ include '../inc/includes.php'; if (isset($_POST['reinit_network'])) { if (Session::haveRight('internet', 'w') && Session::isViewAllEntities()) { IPNetwork::recreateTree(); Session::addMessageAfterRedirect(__('Successfully recreated network tree')); Html::back(); } else { Html::displayRightError(); } } $dropdown = new IPNetwork(); include GLPI_ROOT . "/front/dropdown.common.form.php";
/** * Override title function to display the link to reinitialisation of the network tree **/ function title() { parent::title(); if (Session::haveRight('internet', UPDATE) && Session::isViewAllEntities()) { echo "<div class='spaced' id='tabsbody'>"; echo "<table class='tab_cadre_fixe'>"; echo "<tr><td class='center'>"; Html::showSimpleForm(IPNetwork::getFormURL(), 'reinit_network', __('Reinit the network topology')); echo "</td></tr>"; echo "</table>"; echo "</div>"; } }
You should have received a copy of the GNU General Public License along with GLPI. If not, see <http://www.gnu.org/licenses/>. -------------------------------------------------------------------------- */ /** @file * @brief */ include '../inc/includes.php'; Session::checkSeveralRightsOr(array("networking" => "w", "internet" => "w")); if (!TableExists('glpi_networkportmigrations')) { Session::addMessageAfterRedirect(__('You don\'t need the "migration cleaner" tool anymore...')); Html::redirect($CFG_GLPI["root_doc"] . "/front/central.php"); } Html::header(__('Migration cleaner'), $_SERVER['PHP_SELF'], "utils", "migration"); echo "<div class='spaced' id='tabsbody'>"; echo "<table class='tab_cadre_fixe'>"; echo "<tr><th>" . __('"Migration cleaner" tool') . "</td></tr>"; if (Session::haveRight('internet', 'w') && Session::isViewAllEntities()) { echo "<tr class='tab_bg_1'><td class='center'>"; Html::showSimpleForm(IPNetwork::getFormURL(), 'reinit_network', __('Reinit the network topology')); echo "</td></tr>"; } if (Session::haveRight('networking', 'w')) { echo "<tr class='tab_bg_1'><td class='center'>"; echo "<a href='" . $CFG_GLPI['root_doc'] . "/front/networkportmigration.php'>" . __('Clean the network port migration errors') . "</a>"; echo "</td></tr>"; } echo "</table>"; echo "</div>"; Html::footer();
function manageNetworkPort($inventory_networkports, $computers_id, $no_history) { global $DB; $networkPort = new NetworkPort(); $networkName = new NetworkName(); $iPAddress = new IPAddress(); $iPNetwork = new IPNetwork(); $networkPortEthernet = new NetworkPortEthernet(); $item_DeviceNetworkCard = new Item_DeviceNetworkCard(); foreach ($inventory_networkports as $a_networkport) { if ($a_networkport['mac'] != '') { $a_networkports = $networkPort->find("`mac`='" . $a_networkport['mac'] . "'\n AND `itemtype`='PluginFusioninventoryUnmanaged'", "", 1); if (count($a_networkports) > 0) { $input = current($a_networkports); $unmanageds_id = $input['items_id']; $input['logical_number'] = $a_networkport['logical_number']; $input['itemtype'] = 'Computer'; $input['items_id'] = $computers_id; $input['is_dynamic'] = 1; $input['name'] = $a_networkport['name']; $networkPort->update($input, !$no_history); $pfUnmanaged = new PluginFusioninventoryUnmanaged(); $pfUnmanaged->delete(array('id' => $unmanageds_id), 1); } } } // end get port from unknwon device $db_networkport = array(); if ($no_history === FALSE) { $query = "SELECT `id`, `name`, `mac`, `instantiation_type`, `logical_number`\n FROM `glpi_networkports`\n WHERE `items_id` = '{$computers_id}'\n AND `itemtype`='Computer'\n AND `is_dynamic`='1'"; $result = $DB->query($query); while ($data = $DB->fetch_assoc($result)) { $idtmp = $data['id']; unset($data['id']); if (is_null($data['mac'])) { $data['mac'] = ''; } if (preg_match("/[^a-zA-Z0-9 \\-_\\(\\)]+/", $data['name'])) { $data['name'] = Toolbox::addslashes_deep($data['name']); } $db_networkport[$idtmp] = array_map('strtolower', $data); } } $simplenetworkport = array(); foreach ($inventory_networkports as $key => $a_networkport) { // Add ipnetwork if not exist if ($a_networkport['gateway'] != '' && $a_networkport['netmask'] != '' && $a_networkport['subnet'] != '') { if (countElementsInTable('glpi_ipnetworks', "`address`='" . $a_networkport['subnet'] . "'\n AND `netmask`='" . $a_networkport['netmask'] . "'\n AND `gateway`='" . $a_networkport['gateway'] . "'\n AND `entities_id`='" . $_SESSION["plugin_fusioninventory_entity"] . "'") == 0) { $input_ipanetwork = array('name' => $a_networkport['subnet'] . '/' . $a_networkport['netmask'] . ' - ' . $a_networkport['gateway'], 'network' => $a_networkport['subnet'] . ' / ' . $a_networkport['netmask'], 'gateway' => $a_networkport['gateway'], 'entities_id' => $_SESSION["plugin_fusioninventory_entity"]); $iPNetwork->add($input_ipanetwork, array(), !$no_history); } } // End add ipnetwork $a_field = array('name', 'mac', 'instantiation_type'); foreach ($a_field as $field) { if (isset($a_networkport[$field])) { $simplenetworkport[$key][$field] = $a_networkport[$field]; } } } foreach ($simplenetworkport as $key => $arrays) { $arrayslower = array_map('strtolower', $arrays); foreach ($db_networkport as $keydb => $arraydb) { $logical_number = $arraydb['logical_number']; unset($arraydb['logical_number']); if ($arrayslower == $arraydb) { if ($inventory_networkports[$key]['logical_number'] != $logical_number) { $input = array(); $input['id'] = $keydb; $input['logical_number'] = $inventory_networkports[$key]['logical_number']; $networkPort->update($input, !$no_history); } // Add / update instantiation_type if (isset($inventory_networkports[$key]['instantiation_type'])) { if ($inventory_networkports[$key]['instantiation_type'] == 'NetworkPortEthernet') { $portsethernet = $networkPortEthernet->find("`networkports_id`='" . $keydb . "'", '', 1); if (count($portsethernet) == 1) { $portethernet = current($portsethernet); $input = $portethernet; } else { $input = array('networkports_id' => $keydb); } if (isset($inventory_networkports[$key]['speed'])) { $input['speed'] = $inventory_networkports[$key]['speed']; $input['speed_other_value'] = $inventory_networkports[$key]['speed']; } if (isset($inventory_networkports[$key]['mac'])) { $networkcards = $item_DeviceNetworkCard->find("`mac`='" . $inventory_networkports[$key]['mac'] . "' " . " AND `itemtype`='Computer'" . " AND `items_id`='" . $computers_id . "'", '', 1); if (count($networkcards) == 1) { $networkcard = current($networkcards); $input['items_devicenetworkcards_id'] = $networkcard['id']; } } $input['_no_history'] = $no_history; if (isset($input['id'])) { $networkPortEthernet->update($input); } else { $networkPortEthernet->add($input); } } } // Get networkname $a_networknames_find = current($networkName->find("`items_id`='" . $keydb . "'\n AND `itemtype`='NetworkPort'", "", 1)); if (!isset($a_networknames_find['id'])) { $a_networkport['entities_id'] = $_SESSION["plugin_fusioninventory_entity"]; $a_networkport['items_id'] = $computers_id; $a_networkport['itemtype'] = "Computer"; $a_networkport['is_dynamic'] = 1; $a_networkport['_no_history'] = $no_history; $a_networkport['items_id'] = $keydb; unset($a_networkport['_no_history']); $a_networkport['is_recursive'] = 0; $a_networkport['itemtype'] = 'NetworkPort'; unset($a_networkport['name']); $a_networkport['_no_history'] = $no_history; $a_networknames_id = $networkName->add($a_networkport, array(), !$no_history); $a_networknames_find['id'] = $a_networknames_id; } // Same networkport, verify ipaddresses $db_addresses = array(); $query = "SELECT `id`, `name` FROM `glpi_ipaddresses`\n WHERE `items_id` = '" . $a_networknames_find['id'] . "'\n AND `itemtype`='NetworkName'"; $result = $DB->query($query); while ($data = $DB->fetch_assoc($result)) { $db_addresses[$data['id']] = $data['name']; } $a_computerinventory_ipaddress = $inventory_networkports[$key]['ipaddress']; foreach ($a_computerinventory_ipaddress as $key2 => $arrays2) { foreach ($db_addresses as $keydb2 => $arraydb2) { if ($arrays2 == $arraydb2) { unset($a_computerinventory_ipaddress[$key2]); unset($db_addresses[$keydb2]); break; } } } if (count($a_computerinventory_ipaddress) == 0 and count($db_addresses) == 0) { // Nothing to do } else { if (count($db_addresses) != 0) { // Delete ip address in DB foreach (array_keys($db_addresses) as $idtmp) { $iPAddress->delete(array('id' => $idtmp), 1); } } if (count($a_computerinventory_ipaddress) != 0) { foreach ($a_computerinventory_ipaddress as $ip) { $input = array(); $input['items_id'] = $a_networknames_find['id']; $input['itemtype'] = 'NetworkName'; $input['name'] = $ip; $input['is_dynamic'] = 1; $iPAddress->add($input, array(), !$no_history); } } } unset($db_networkport[$keydb]); unset($simplenetworkport[$key]); unset($inventory_networkports[$key]); break; } } } if (count($inventory_networkports) == 0 and count($db_networkport) == 0) { // Nothing to do } else { if (count($db_networkport) != 0) { // Delete networkport in DB foreach ($db_networkport as $idtmp => $data) { $networkPort->delete(array('id' => $idtmp), 1); } } if (count($inventory_networkports) != 0) { foreach ($inventory_networkports as $a_networkport) { $a_networkport['entities_id'] = $_SESSION["plugin_fusioninventory_entity"]; $a_networkport['items_id'] = $computers_id; $a_networkport['itemtype'] = "Computer"; $a_networkport['is_dynamic'] = 1; $a_networkport['_no_history'] = $no_history; $a_networkport['items_id'] = $networkPort->add($a_networkport, array(), !$no_history); unset($a_networkport['_no_history']); $a_networkport['is_recursive'] = 0; $a_networkport['itemtype'] = 'NetworkPort'; unset($a_networkport['name']); $a_networkport['_no_history'] = $no_history; $a_networknames_id = $networkName->add($a_networkport, array(), !$no_history); foreach ($a_networkport['ipaddress'] as $ip) { $input = array(); $input['items_id'] = $a_networknames_id; $input['itemtype'] = 'NetworkName'; $input['name'] = $ip; $input['is_dynamic'] = 1; $input['_no_history'] = $no_history; $iPAddress->add($input, array(), !$no_history); } if (isset($a_networkport['instantiation_type'])) { if ($a_networkport['instantiation_type'] == 'NetworkPortEthernet') { $input = array('networkports_id' => $a_networkport['items_id']); if (isset($a_networkport['speed'])) { $input['speed'] = $a_networkport['speed']; $input['speed_other_value'] = $a_networkport['speed']; } if (isset($a_networkport['mac'])) { $networkcards = $item_DeviceNetworkCard->find("`mac`='" . $a_networkport['mac'] . "' " . " AND `itemtype`='Computer'" . " AND `items_id`='" . $computers_id . "'", '', 1); if (count($networkcards) == 1) { $networkcard = current($networkcards); $input['items_devicenetworkcards_id'] = $networkcard['id']; } } $input['_no_history'] = $no_history; $networkPortEthernet->add($input); } } } } } }
static function pdfForItem(PluginPdfSimplePDF $pdf, CommonDBTM $item) { global $DB; $ID = $item->getField('id'); $type = get_class($item); $query = "SELECT `glpi_networkports`.`id`\n FROM `glpi_networkports`\n WHERE `items_id` = '" . $ID . "'\n AND `itemtype` = '" . $type . "'\n ORDER BY `name`, `logical_number`"; $pdf->setColumnsSize(100); if ($result = $DB->query($query)) { $nb_connect = $DB->numrows($result); if (!$nb_connect) { $pdf->displayTitle('<b>0 ' . __('No network port found') . '</b>'); } else { $pdf->displayTitle('<b>' . sprintf(__('%1$s: %2$d'), _n('Network port', 'Network ports', $nb_connect), $nb_connect . "</b>")); while ($devid = $DB->fetch_row($result)) { $netport = new NetworkPort(); $netport->getfromDB(current($devid)); $instantiation_type = $netport->fields["instantiation_type"]; $instname = call_user_func(array($instantiation_type, 'getTypeName')); $pdf->displayTitle('<b>' . $instname . '</b>'); $pdf->displayLine('<b>' . sprintf(__('%1$s: %2$s'), '#</b>', $netport->fields["logical_number"])); $pdf->displayLine('<b>' . sprintf(__('%1$s: %2$s'), __('Name') . '</b>', $netport->fields["name"])); $contact = new NetworkPort(); $netport2 = new NetworkPort(); $add = __('Not connected.'); if ($cid = $contact->getContact($netport->fields["id"])) { if ($netport2->getFromDB($cid) && ($device2 = getItemForItemtype($netport2->fields["itemtype"]))) { if ($device2->getFromDB($netport2->fields["items_id"])) { $add = $netport2->getName() . ' ' . __('on') . ' ' . $device2->getName() . ' (' . $device2->getTypeName() . ')'; } } } if ($instantiation_type == 'NetworkPortEthernet') { $pdf->displayLine('<b>' . sprintf(__('%1$s: %2$s'), __('Connected to') . '</b>', $add)); $netportethernet = new NetworkPortEthernet(); $speed = $type = ''; if ($netportethernet->getFromDB($netport->fields['id'])) { $speed = NetworkPortEthernet::getPortSpeed($netportethernet->fields['speed']); $type = NetworkPortEthernet::getPortTypeName($netportethernet->fields['type']); } $pdf->displayLine('<b>' . sprintf(__('%1$s: %2$s'), __('Ethernet port speed') . '</b>', $speed)); $pdf->displayLine('<b>' . sprintf(__('%1$s: %2$s'), __('Ethernet port type') . '</b>', $type)); $netpoint = new Netpoint(); $outlet = ''; if ($netpoint->getFromDB($netportethernet->fields['netpoints_id'])) { $outlet = $netpoint->fields['name']; } $pdf->displayLine('<b>' . sprintf(__('%1$s: %2$s'), __('Network outlet') . '</b>', $outlet)); } $pdf->displayLine('<b>' . sprintf(__('%1$s: %2$s'), __('MAC') . '</b>', $netport->fields["mac"])); $sqlip = "LEFT JOIN `glpi_networknames`\n ON (`glpi_ipaddresses`.`items_id` = `glpi_networknames`.`id`\n AND `glpi_ipaddresses`.`entities_id`\n = '" . $_SESSION['glpiactive_entity'] . "')\n WHERE `glpi_networknames`.`items_id` = '" . $netport->fields["id"] . "'"; $ipname = ''; $ip = new IPAddress(); if ($ip->getFromDBByQuery($sqlip)) { $ipname = $ip->fields['name']; $pdf->displayLine('<b>' . sprintf(__('%1$s: %2$s'), __('ip') . '</b>', $ipname)); $sql = "SELECT `glpi_ipaddresses_ipnetworks`.`ipnetworks_id`\n FROM `glpi_ipaddresses_ipnetworks`\n LEFT JOIN `glpi_ipnetworks`\n ON (`glpi_ipaddresses_ipnetworks`.`ipnetworks_id` = `glpi_ipnetworks`.`id`)\n WHERE `glpi_ipaddresses_ipnetworks`.`ipaddresses_id` = '" . $ip->getID() . "'"; $res = $DB->query($sql); if ($res) { while ($row = $DB->fetch_assoc($res)) { $ipnetwork = new IPNetwork(); if ($ipnetwork->getFromDB($row['ipnetworks_id'])) { $pdf->displayLine('<b>' . sprintf(__('%1$s: %2$s'), __('IP network') . '</b>', $ipnetwork->fields['address'])); $pdf->displayLine('<b>' . sprintf(__('%1$s: %2$s'), __('Subnet mask') . '</b>', $ipnetwork->fields['netmask'])); $pdf->displayLine('<b>' . sprintf(__('%1$s: %2$s'), __('Gateway') . '</b>', $ipnetwork->fields['gateway'])); } } } } } // each port } // Found } // Query $pdf->displaySpace(); }