Ejemplo n.º 1
0
/**
 * 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);
    }
}
Ejemplo n.º 2
0
 /**
  * Look for "computer name" inside all databases
  *
  * @param $fqdn                     name to search (for instance : forge.indepnet.net)
  * @param $wildcard_search boolean  true if we search with wildcard (false by default)
  *
  * @return (array) each value of the array (corresponding to one NetworkPort) is an array of the
  *                 items from the master item to the NetworkPort
  **/
 static function getItemsByFQDN($fqdn, $wildcard_search = false)
 {
     $FQNDs_with_Items = array();
     if (!$wildcard_search) {
         if (!FQDN::checkFQDN($fqdn)) {
             return array();
         }
     }
     $position = strpos($fqdn, ".");
     if ($position !== false) {
         $label = strtolower(substr($fqdn, 0, $position));
         $fqdns_id = FQDN::getFQDNIDByFQDN(substr($fqdn, $position + 1), $wildcard_search);
     } else {
         $label = $fqdn;
         $fqdns_id = -1;
     }
     foreach (self::getIDsByLabelAndFQDNID($label, $fqdns_id, $wildcard_search) as $class => $IDs) {
         if ($FQDNlabel = getItemForItemtype($class)) {
             foreach ($IDs as $ID) {
                 if ($FQDNlabel->getFromDB($ID)) {
                     $FQNDs_with_Items[] = array_merge(array_reverse($FQDNlabel->recursivelyGetItems()), array(clone $FQDNlabel));
                 }
             }
         }
     }
     return $FQNDs_with_Items;
 }