function _updateNetworkInfo($arrayinventory, $item_type, $id, $instanciation_type, $check_addresses) { $NetworkPort = new NetworkPort(); $port = current($NetworkPort->find("`itemtype`='{$item_type}' AND `items_id`='{$id}'" . " AND `instantiation_type`='{$instanciation_type}'", "", 1)); $port_id = 0; if (isset($port['id'])) { if (isset($arrayinventory['MAC']) and !empty($arrayinventory['MAC'])) { $input = array(); $input['id'] = $port['id']; $input['mac'] = $arrayinventory['MAC']; $NetworkPort->update($input); } $port_id = $port['id']; } else { $input = array(); $input['itemtype'] = $item_type; $input['items_id'] = $id; $input['instantiation_type'] = $instanciation_type; $input['name'] = "management"; if (isset($arrayinventory['MAC']) && !empty($arrayinventory['MAC'])) { $input['mac'] = $arrayinventory['MAC']; } $port_id = $NetworkPort->add($input); } $NetworkName = new NetworkName(); $name = current($NetworkName->find("`itemtype`='NetworkPort' AND `items_id`='" . $port_id . "'", "", 1)); $name_id = 0; if (isset($name['id'])) { $name_id = $name['id']; } else { $input = array(); $input['itemtype'] = 'NetworkPort'; $input['items_id'] = $port_id; $name_id = $NetworkName->add($input); } if (isset($arrayinventory['IP'])) { $IPAddress = new IPAddress(); if ($check_addresses) { $addresses = $IPAddress->find("`itemtype`='NetworkName'\n AND `items_id`='" . $name_id . "'", '', 1); } else { // Case of NetworkEquipment $a_ips = $IPAddress->find("`itemtype`='NetworkName'\n AND `items_id`='" . $name_id . "'\n AND `name`='" . $arrayinventory['IP'] . "'", '', 1); if (count($a_ips) > 0) { $addresses = $a_ips; } else { $addresses = array(); } } if (count($addresses) == 0) { $input = array(); $input['itemtype'] = 'NetworkName'; $input['items_id'] = $name_id; $input['name'] = $arrayinventory['IP']; $IPAddress->add($input); } else { $address = current($addresses); if ($address['name'] != $arrayinventory['IP']) { $input = array(); $input['id'] = $address['id']; $input['name'] = $arrayinventory['IP']; $IPAddress->update($input); } } } }
/** * Transfer network links * * @param $itemtype original type of transfered item * @param $ID original ID of the item * @param $newID new ID of the item * @param $ocs_computer if computer type OCS ID of the item if available **/ function transferNetworkLink($itemtype, $ID, $newID, $ocs_computer = false) { global $DB; $np = new NetworkPort(); $nn = new NetworkPort_NetworkPort(); $query = "SELECT *\n FROM `glpi_networkports`\n WHERE `items_id` = '{$ID}'\n AND `itemtype` = '{$itemtype}'"; if ($result = $DB->query($query)) { if ($DB->numrows($result) != 0) { switch ($this->options['keep_networklink']) { // Delete netport case 0: // Not a copy -> delete if ($ID == $newID) { while ($data = $DB->fetch_array($result)) { $np->delete(array('id' => $data['id'])); } // Only case of ocs link update is needed (if netports are keep nothing to do) if ($ocs_computer) { $query = "UPDATE `glpi_ocslinks`\n SET `import_ip` = NULL\n WHERE `computers_id` = '{$ID}'"; $DB->query($query); } } // Copy -> do nothing break; // Disconnect // Disconnect case 1: // Not a copy -> disconnect if ($ID == $newID) { while ($data = $DB->fetch_array($result)) { if ($nn->getFromDBForNetworkPort($data['id'])) { $nn->delete($data); } if ($data['netpoints_id']) { $netpointID = $this->transferDropdownNetpoint($data['netpoints_id']); $input['id'] = $data['id']; $input['netpoints_id'] = $netpointID; $np->update($input); } } } else { // Copy -> copy netports while ($data = $DB->fetch_array($result)) { $data = addslashes_deep($data); unset($data['id']); $data['items_id'] = $newID; $data['netpoints_id'] = $this->transferDropdownNetpoint($data['netpoints_id']); unset($np->fields); $np->add($data); } } break; // Keep network links // Keep network links default: // Copy -> Copy netpoints (do not keep links) if ($ID != $newID) { while ($data = $DB->fetch_array($result)) { unset($data['id']); $data['items_id'] = $newID; $data['netpoints_id'] = $this->transferDropdownNetpoint($data['netpoints_id']); unset($np->fields); $np->add($data); } } else { while ($data = $DB->fetch_array($result)) { // Not a copy -> only update netpoint if ($data['netpoints_id']) { $netpointID = $this->transferDropdownNetpoint($data['netpoints_id']); $input['id'] = $data['id']; $input['netpoints_id'] = $netpointID; $np->update($input); } } } } } } }
/** * @param $type * @param $model_id * @param $tab_ids * @param $location **/ static function replace($type, $model_id, $tab_ids, $location) { global $DB, $CFG_GLPI, $PLUGIN_HOOKS; $model = new PluginUninstallModel(); $model->getConfig($model_id); $overwrite = $model->fields["overwrite"]; echo "<div class='center'>"; echo "<table class='tab_cadre_fixe'><tr><th>" . __('Replacement', 'uninstall') . "</th></tr>"; echo "<tr class='tab_bg_2'><td>"; $count = 0; $tot = count($tab_ids); Html::createProgressBar(__('Please wait, replacement is running...', 'uninstall')); foreach ($tab_ids as $olditem_id => $newitem_id) { $count++; $olditem = new $type(); $olditem->getFromDB($olditem_id); $newitem = new $type(); $newitem->getFromDB($newitem_id); //Hook to perform actions before item is being replaced $olditem->fields['_newid'] = $newitem_id; $olditem->fields['_uninstall_event'] = $model_id; $olditem->fields['_action'] = 'replace'; Plugin::doHook("plugin_uninstall_replace_before", $olditem); // Retrieve informations //States if ($model->fields['states_id'] != 0) { $olditem->update(array('id' => $olditem_id, 'states_id' => $model->fields['states_id']), false); } // METHOD REPLACEMENT 1 : Archive if ($model->fields['replace_method'] == self::METHOD_PURGE) { $name_out = str_shuffle(Toolbox::getRandomString(5) . time()); $plugin = new Plugin(); if ($plugin->isActivated('PDF')) { // USE PDF EXPORT $plugin->load('pdf', true); include_once GLPI_ROOT . "/lib/ezpdf/class.ezpdf.php"; //Get all item's tabs $tab = array_keys($olditem->defineTabs()); //Tell PDF to also export item's main tab, and in first position array_unshift($tab, "_main_"); $itempdf = new $PLUGIN_HOOKS['plugin_pdf'][$type]($olditem); $out = $itempdf->generatePDF(array($olditem_id), $tab, 1, false); $name_out .= ".pdf"; } else { //TODO Which datas ? Add Defaults... $out = __('Replacement', 'uninstall') . "\r\n"; $datas = $olditem->fields; unset($datas['comment']); foreach ($datas as $k => $v) { $out .= $k . ";"; } $out .= "\r\n"; foreach ($datas as $k => $v) { $out .= $v . ";"; } // USE CSV EXPORT $name_out .= ".csv"; } // Write document $out_file = GLPI_DOC_DIR . "/_uploads/" . $name_out; $open_file = fopen($out_file, 'a'); fwrite($open_file, $out); fclose($open_file); // Compute comment text $comment = __('This document is the archive of this replaced item', 'uninstall') . " " . self::getCommentsForReplacement($olditem, false, false); // Create & Attach new document to current item $doc = new Document(); $input = array('name' => addslashes(__('Archive of old material', 'uninstall')), 'upload_file' => $name_out, 'comment' => addslashes($comment), 'add' => __('Add'), 'entities_id' => $newitem->getEntityID(), 'is_recursive' => $newitem->isRecursive(), 'link' => "", 'documentcategories_id' => 0, 'items_id' => $olditem_id, 'itemtype' => $type); //Attached the document to the old item, to generate an accurate name $document_added = $doc->add($input); //Attach the document to the new item, once the document's name is correct $docItem = new Document_Item(); $docItemId = $docItem->add(array('documents_id' => $document_added, 'itemtype' => $type, 'items_id' => (int) $newitem_id)); } // General Informations - NAME if ($model->fields["replace_name"]) { if ($overwrite || empty($newitem->fields['name'])) { $newitem->update(array('id' => $newitem_id, 'name' => $olditem->getField('name')), false); } } $data['id'] = $newitem->getID(); // General Informations - SERIAL if ($model->fields["replace_serial"]) { if ($overwrite || empty($newitem->fields['serial'])) { $newitem->update(array('id' => $newitem_id, 'serial' => $olditem->getField('serial')), false); } } // General Informations - OTHERSERIAL if ($model->fields["replace_otherserial"]) { if ($overwrite || empty($newitem->fields['otherserial'])) { $newitem->update(array('id' => $newitem_id, 'otherserial' => $olditem->getField('otherserial')), false); } } // Documents if ($model->fields["replace_documents"] && in_array($type, $CFG_GLPI["document_types"])) { $doc_item = new Document_Item(); foreach (self::getAssociatedDocuments($olditem) as $document) { $doc_item->update(array('id' => $document['assocID'], 'itemtype' => $type, 'items_id' => $newitem_id), false); } } // Contracts if ($model->fields["replace_contracts"] && in_array($type, $CFG_GLPI["contract_types"])) { $contract_item = new Contract_Item(); foreach (self::getAssociatedContracts($olditem) as $contract) { $contract_item->update(array('id' => $contract['id'], 'itemtype' => $type, 'items_id' => $newitem_id), false); } } // Infocoms if ($model->fields["replace_infocoms"] && in_array($type, $CFG_GLPI["infocom_types"])) { $infocom = new Infocom(); if ($overwrite) { // Delete current Infocoms of new item if ($infocom->getFromDBforDevice($type, $newitem_id)) { //Do not log infocom deletion in the new item's history $infocom->dohistory = false; $infocom->deleteFromDB(1); } } // Update current Infocoms of old item if ($infocom->getFromDBforDevice($type, $olditem_id)) { $infocom->update(array('id' => $infocom->getID(), 'itemtype' => $type, 'items_id' => $newitem_id), false); } } // Reservations if ($model->fields["replace_reservations"] && in_array($type, $CFG_GLPI["reservation_types"])) { $resaitem = new ReservationItem(); if ($overwrite) { // Delete current reservation of new item $resa_new = new Reservation(); $resa_new->getFromDB($newitem_id); if ($resa_new->is_reserved()) { $resa_new = new ReservationItem(); $resa_new->getFromDBbyItem($type, $newitem_id); if (count($resa_new->fields)) { $resa_new->deleteFromDB(1); } } } // Update old reservation for attribute to new item $resa_old = new Reservation(); $resa_old->getFromDB($olditem_id); if ($resa_old->is_reserved()) { $resa_old = new ReservationItem(); $resa_old->getFromDBbyItem($type, $olditem_id); if (count($resa_old->fields)) { $resa_old->update(array('id' => $resa_old->getID(), 'itemtype' => $type, 'items_id' => $newitem_id), false); } } } // User if ($model->fields["replace_users"] && in_array($type, $CFG_GLPI["linkuser_types"])) { $data = array(); $data['id'] = $newitem->getID(); if ($newitem->isField('users_id') && ($overwrite || empty($data['users_id']))) { $data['users_id'] = $olditem->getField('users_id'); } if ($newitem->isField('contact') && ($overwrite || empty($data['contact']))) { $data['contact'] = $olditem->getField('contact'); } if ($newitem->isField('contact_num') && ($overwrite || empty($data['contact_num']))) { $data['contact_num'] = $olditem->getField('contact_num'); } $newitem->update($data, false); } // Group if ($model->fields["replace_groups"] && in_array($type, $CFG_GLPI["linkgroup_types"])) { if ($newitem->isField('groups_id') && ($overwrite || empty($data['groups_id']))) { $newitem->update(array('id' => $newitem_id, 'groups_id' => $olditem->getField('groups_id')), false); } } // Tickets if ($model->fields["replace_tickets"] && in_array($type, $CFG_GLPI["ticket_types"])) { $ticket_item = new Item_Ticket(); foreach (self::getAssociatedTickets($type, $olditem_id) as $ticket) { $ticket_item->update(array('id' => $ticket['id'], 'items_id' => $newitem_id), false); } } //Array netport_types renamed in networkport_types in GLPI 0.80 if (isset($CFG_GLPI["netport_types"])) { $netport_types = $CFG_GLPI["netport_types"]; } else { $netport_types = $CFG_GLPI["networkport_types"]; } // NetPorts if ($model->fields["replace_netports"] && in_array($type, $netport_types)) { $netport_item = new NetworkPort(); foreach (self::getAssociatedNetports($type, $olditem_id) as $netport) { $netport_item->update(array('id' => $netport['id'], 'itemtype' => $type, 'items_id' => $newitem_id), false); } } // Directs connections if ($model->fields["replace_direct_connections"] && in_array($type, array('Computer'))) { $comp_item = new Computer_Item(); foreach (self::getAssociatedItems($olditem) as $itemtype => $connections) { foreach ($connections as $connection) { $comp_item->update(array('id' => $connection['id'], 'computers_id' => $newitem_id, 'itemtype' => $itemtype), false); } } } // Location if ($location != 0 && $olditem->isField('locations_id')) { $olditem->getFromDB($olditem_id); switch ($location) { case -1: break; default: $olditem->update(array('id' => $olditem_id, 'locations_id' => $location), false); break; } } $plug = new Plugin(); if ($plug->isActivated('ocsinventoryng')) { //Delete computer from OCS if ($model->fields["remove_from_ocs"] == 1) { PluginUninstallUninstall::deleteComputerInOCSByGlpiID($olditem_id); } //Delete link in glpi_ocs_link if ($model->fields["delete_ocs_link"] || $model->fields["remove_from_ocs"]) { PluginUninstallUninstall::deleteOcsLink($olditem_id); } } if ($plug->isActivated('fusioninventory')) { if ($model->fields['raz_fusioninventory']) { PluginUninstallUninstall::deleteFusionInventoryLink(get_class($olditem), $olditem_id); } } // METHOD REPLACEMENT 1 : Purge if ($model->fields['replace_method'] == self::METHOD_PURGE) { // Retrieve, Compute && Update NEW comment field $comment = self::getCommentsForReplacement($olditem, true); $comment .= "\n- " . __('See attached document', 'uninstall'); $newitem->update(array('id' => $newitem_id, 'comment' => addslashes($comment)), false); // If old item is attached in PDF/CSV // Delete AND Purge it in DB if ($document_added) { $olditem->delete(array('id' => $olditem_id), true); } } // METHOD REPLACEMENT 2 : Delete AND Comment if ($model->fields['replace_method'] == self::METHOD_DELETE_AND_COMMENT) { //Add comment on the new item first $comment = self::getCommentsForReplacement($olditem, true); $newitem->update(array('id' => $newitem_id, 'comment' => Toolbox::addslashes_deep($comment)), false); // Retrieve, Compute && Update OLD comment field $comment = self::getCommentsForReplacement($newitem, false); $olditem->update(array('id' => $olditem_id, 'comment' => Toolbox::addslashes_deep($comment)), false); // Delete OLD item from DB (not PURGE) PluginUninstallUninstall::addUninstallLog($type, $olditem_id, 'replaced_by'); $olditem->delete(array('id' => $olditem_id), 0, false); } //Plugin hook after replacement Plugin::doHook("plugin_uninstall_replace_after", $olditem); //Add history PluginUninstallUninstall::addUninstallLog($type, $newitem_id, 'replace'); Html::changeProgressBarPosition($count, $tot + 1); } Html::changeProgressBarPosition($count, $tot, __('Replacement successful', 'uninstall')); echo "</td></tr>"; echo "</table></div>"; }
function importPorts($a_inventory, $items_id) { $networkPort = new NetworkPort(); $pfNetworkPort = new PluginFusioninventoryNetworkPort(); $networkports_id = 0; foreach ($a_inventory['networkport'] as $a_port) { $a_ports_DB = current($networkPort->find("`itemtype`='Printer'\n AND `items_id`='" . $items_id . "'\n AND `instantiation_type`='NetworkPortEthernet'\n AND `logical_number` = '" . $a_port['logical_number'] . "'", '', 1)); if (!isset($a_ports_DB['id'])) { // Add port $a_port['instantiation_type'] = 'NetworkPortEthernet'; $a_port['items_id'] = $items_id; $a_port['itemtype'] = 'Printer'; $networkports_id = $networkPort->add($a_port); unset($a_port['id']); $a_pfnetworkport_DB = current($pfNetworkPort->find("`networkports_id`='" . $networkports_id . "'", '', 1)); $a_port['id'] = $a_pfnetworkport_DB['id']; $pfNetworkPort->update($a_port); } else { // Update port $networkports_id = $a_ports_DB['id']; $a_port['id'] = $a_ports_DB['id']; $networkPort->update($a_port); unset($a_port['id']); // Check if pfnetworkport exist. $a_pfnetworkport_DB = current($pfNetworkPort->find("`networkports_id`='" . $networkports_id . "'", '', 1)); $a_port['networkports_id'] = $networkports_id; if (isset($a_pfnetworkport_DB['id'])) { $a_port['id'] = $a_pfnetworkport_DB['id']; $pfNetworkPort->update($a_port); } else { $a_port['networkports_id'] = $networkports_id; $pfNetworkPort->add($a_port); } } } }
/** * @param $mac * @param $name * @param $computers_id * @param $instantiation_type * @param $inst_input * @param $ips * @param $check_name * @param $dohistory * @param $already_known_ports * @return ID */ private static function updateNetworkPort($mac, $name, $computers_id, $instantiation_type, $inst_input, $ips, $check_name, $dohistory, $already_known_ports) { global $DB; $network_port = new NetworkPort(); // Then, find or create the base NetworkPort $query = "SELECT `id`, `is_dynamic`\n FROM `glpi_networkports`\n WHERE `itemtype` = 'Computer'\n AND `items_id` = '{$computers_id}'\n AND `mac` = '{$mac}'"; // If there is virtual ports, then, filter by port's name if ($check_name) { $query .= " AND `name` = '{$name}'"; } if (count($already_known_ports) > 0) { $query .= " AND `id` NOT IN (" . implode(',', $already_known_ports) . ")"; } // We order by is_dynamic to be sure to get the static ones first ! $query .= " ORDER BY `is_dynamic`, `id`"; $ports = $DB->request($query); if ($ports->numrows() == 0) { $port_input = array('name' => $name, 'mac' => $mac, 'items_id' => $computers_id, 'itemtype' => 'Computer', '_no_history' => !$dohistory, 'instantiation_type' => $instantiation_type, '_create_children' => 1, 'is_dynamic' => 1, 'is_deleted' => 0); $networkports_id = $network_port->add($port_input, array(), $dohistory); if ($networkports_id === false) { return -1; } $inst_input['networkports_id'] = $networkports_id; $instantiation = $network_port->getInstantiation(); $instantiation->update($inst_input); unset($instantiation); } else { $line = $ports->next(); $networkports_id = $line['id']; $network_port->getFromDB($networkports_id); if (!$check_name && $network_port->fields['name'] != $name) { $port_input = array('id' => $network_port->getID(), 'name' => $name, 'is_dynamic' => 1); $network_port->update($port_input); } if ($network_port->fields['instantiation_type'] != $instantiation_type && $network_port->fields['is_dynamic'] == 1) { $network_port->switchInstantiationType($instantiation_type); $inst_input['networkports_id'] = $network_port->getID(); $instantiation = $network_port->getInstantiation(); $instantiation->add($inst_input); unset($instantiation); } if ($network_port->fields['instantiation_type'] == $instantiation_type) { $instantiation = $network_port->getInstantiation(); $inst_input['id'] = $instantiation->getID(); $inst_input['networkports_id'] = $network_port->getID(); $instantiation->update($inst_input); unset($instantiation); } } if ($network_port->isNewItem()) { return -1; } $network_name = new NetworkName(); $query = "SELECT `id`, `is_dynamic`\n FROM `glpi_networknames`\n WHERE `itemtype` = 'NetworkPort'\n AND `items_id` = '{$networkports_id}'\n ORDER BY `is_dynamic`"; if (!$ips || count($ips) == 0) { foreach ($DB->request($query) as $line) { if ($line['is_dynamic']) { $network_name->delete($line, true, $dohistory); } } } else { $names = $DB->request($query); if ($names->numrows() == 0) { $name_input = array('itemtype' => 'NetworkPort', 'items_id' => $networkports_id, 'is_dynamic' => 1, 'is_deleted' => 0, '_no_history' => !$dohistory, 'name' => 'OCS-INVENTORY-NG'); $networknames_id = $network_name->add($name_input); } else { $line = $names->next(); $networknames_id = $line['id']; foreach ($names as $line) { if ($line['is_dynamic'] == 1 && $line['id'] != $networknames_id) { $network_port->delete($line, true, $dohistory); } } } $ip_address = new IPAddress(); $already_known_addresses = array(); $query = "SELECT `id`, `name`, `is_dynamic`, `mainitems_id`\n FROM `glpi_ipaddresses`\n WHERE `itemtype` = 'NetworkName'\n AND `items_id` = '{$networknames_id}'\n ORDER BY `is_dynamic`"; foreach ($DB->request($query) as $line) { if (in_array($line['name'], $ips) && !empty($line['mainitems_id'])) { $already_known_addresses[] = $line['id']; $ips = array_diff($ips, array($line['name'])); } elseif ($line['is_dynamic'] == 1) { $ip_address->delete($line, true, $dohistory); } } } if ($ips) { foreach ($ips as $ip) { $ip_input = array('name' => $ip, 'itemtype' => 'NetworkName', 'items_id' => $networknames_id, '_no_history' => !$dohistory, 'is_dynamic' => 1, 'is_deleted' => 0); $ip_address->add($ip_input); } } return $network_port->getID(); }
/** * Transfer network links * * @param $itemtype original type of transfered item * @param $ID original ID of the item * @param $newID new ID of the item **/ function transferNetworkLink($itemtype, $ID, $newID) { global $DB; /// TODO manage with new network system $np = new NetworkPort(); $nn = new NetworkPort_NetworkPort(); $query = "SELECT `glpi_networkports`.*, `glpi_networkportethernets`.`netpoints_id`\n FROM `glpi_networkports`\n LEFT JOIN `glpi_networkportethernets`\n ON (`glpi_networkports`.`id` = `glpi_networkportethernets`.`networkports_id`)\n WHERE `glpi_networkports`.`items_id` = '{$ID}'\n AND `glpi_networkports`.`itemtype` = '{$itemtype}'"; if ($result = $DB->query($query)) { if ($DB->numrows($result) != 0) { switch ($this->options['keep_networklink']) { // Delete netport case 0: // Not a copy -> delete if ($ID == $newID) { while ($data = $DB->fetch_assoc($result)) { $np->delete(array('id' => $data['id'])); } } // Copy -> do nothing break; // Disconnect // Disconnect case 1: // Not a copy -> disconnect if ($ID == $newID) { while ($data = $DB->fetch_assoc($result)) { if ($nn->getFromDBForNetworkPort($data['id'])) { $nn->delete($data); } if ($data['netpoints_id']) { $netpointID = $this->transferDropdownNetpoint($data['netpoints_id']); $input['id'] = $data['id']; $input['netpoints_id'] = $netpointID; $np->update($input); } } } else { // Copy -> copy netports while ($data = $DB->fetch_assoc($result)) { $data = Toolbox::addslashes_deep($data); unset($data['id']); $data['items_id'] = $newID; $data['netpoints_id'] = $this->transferDropdownNetpoint($data['netpoints_id']); unset($np->fields); $np->add(toolbox::addslashes_deep($data)); } } break; // Keep network links // Keep network links default: // Copy -> Copy netpoints (do not keep links) if ($ID != $newID) { while ($data = $DB->fetch_assoc($result)) { unset($data['id']); $data['items_id'] = $newID; $data['netpoints_id'] = $this->transferDropdownNetpoint($data['netpoints_id']); unset($np->fields); $np->add(toolbox::addslashes_deep($data)); } } else { while ($data = $DB->fetch_assoc($result)) { // Not a copy -> only update netpoint if ($data['netpoints_id']) { $netpointID = $this->transferDropdownNetpoint($data['netpoints_id']); $input['id'] = $data['id']; $input['netpoints_id'] = $netpointID; $np->update($input); } } } } } } }
Html::back(); } } else { if (isset($_POST["delete"])) { $np->check($_POST['id'], 'd'); $np->delete($_POST); Event::log($_POST['id'], "networkport", 5, "inventory", sprintf(__('%s purges an item'), $_SESSION["glpiname"])); if ($item = getItemForItemtype($np->fields['itemtype'])) { Html::redirect($item->getFormURL() . '?id=' . $np->fields['items_id']); } Html::redirect($CFG_GLPI["root_doc"] . "/front/central.php"); } else { if (isset($_POST["update"])) { $np->check($_POST['id'], 'w'); $np->splitInputForElements($_POST); $np->update($_POST); $np->updateDependencies(1); Event::log($_POST["id"], "networkport", 4, "inventory", sprintf(__('%s updates an item'), $_SESSION["glpiname"])); Html::back(); } else { if (isset($_POST["disconnect"])) { $nn->check($_POST['id'], 'd'); if (isset($_POST["id"])) { $nn->delete($_POST); } Html::back(); } else { if (empty($_GET["items_id"])) { $_GET["items_id"] = ""; } if (empty($_GET["itemtype"])) {
/** * Function to import discovered device * * @param $items_id id of the device to import * * @return nothing * **/ function import($items_id, $Import = 0, $NoImport = 0) { global $DB; $NetworkPort = new NetworkPort(); $a_NetworkPorts = $NetworkPort->find("`items_id` = '" . $items_id . "'\n AND `itemtype` = 'PluginFusioninventoryUnmanaged'"); $this->getFromDB($items_id); $this->fields = Toolbox::addslashes_deep($this->fields); $data = array(); switch ($this->fields['item_type']) { case 'Printer': $Printer = new Printer(); $data["entities_id"] = $this->fields["entities_id"]; if (!empty($this->fields["name"])) { $data["name"] = $this->fields["name"]; } $data["locations_id"] = $this->fields["locations_id"]; $data["serial"] = $this->fields["serial"]; $data["otherserial"] = $this->fields["otherserial"]; $data["contact"] = $this->fields["contact"]; $data["domain"] = $this->fields["domain"]; $data["comment"] = $this->fields["comment"]; $data["is_dynamic"] = 1; $printer_id = $Printer->add($data); foreach ($a_NetworkPorts as $data_Port) { $data_Port['items_id'] = $printer_id; $data_Port['itemtype'] = $Printer->getType(); $NetworkPort->update($data_Port); } // Import SNMP $pfPrinter = new PluginFusioninventoryPrinter(); $_SESSION['glpi_plugins_fusinvsnmp_table'] = "glpi_plugin_fusioninventory_printers"; $query = "SELECT *\n FROM `glpi_plugin_fusioninventory_printers`\n WHERE `printers_id`='" . $printer_id . "' "; $result = $DB->query($query); $data = array(); if ($DB->numrows($result) > 0) { $data = $DB->fetch_assoc($result); } $data['sysdescr'] = $this->fields['sysdescr']; $data['plugin_fusioninventory_configsecurities_id'] = $this->fields['plugin_fusioninventory_configsecurities_id']; if ($DB->numrows($result) == 0) { $data['printers_id'] = $printer_id; $pfPrinter->add($data); } else { $pfPrinter->update($data); } $this->deleteFromDB($items_id, 1); $Import++; break; case 'NetworkEquipment': $NetworkEquipment = new NetworkEquipment(); $data["entities_id"] = $this->fields["entities_id"]; if (!empty($this->fields["name"])) { $data["name"] = $this->fields["name"]; } $data["locations_id"] = $this->fields["locations_id"]; $data["serial"] = $this->fields["serial"]; $data["otherserial"] = $this->fields["otherserial"]; $data["contact"] = $this->fields["contact"]; $data["domain"] = $this->fields["domain"]; $data["comment"] = $this->fields["comment"]; $data["is_dynamic"] = 1; // $data_Port = current($a_NetworkPorts); // $data["ip"] = $data_Port["ip"]; // $data["mac"] = $data_Port["mac"]; $NetworkEquipment_id = $NetworkEquipment->add($data); foreach ($a_NetworkPorts as $data_Port) { $data_Port['items_id'] = $NetworkEquipment_id; $data_Port['itemtype'] = $NetworkEquipment->getType(); $NetworkPort->update($data_Port); } $pfNetworkEquipment = new PluginFusioninventoryNetworkEquipment(); $_SESSION['glpi_plugins_fusinvsnmp_table'] = "glpi_plugin_fusioninventory_networkequipments"; $query = "SELECT *\n FROM `glpi_plugin_fusioninventory_networkequipments`\n WHERE `networkequipments_id`='" . $NetworkEquipment_id . "' "; $result = $DB->query($query); $data = array(); if ($DB->numrows($result) > 0) { $data = $DB->fetch_assoc($result); } $data['sysdescr'] = $this->fields['sysdescr']; $data['plugin_fusioninventory_configsecurities_id'] = $this->fields['plugin_fusioninventory_configsecurities_id']; if ($DB->numrows($result) == 0) { $data['networkequipments_id'] = $NetworkEquipment_id; $pfNetworkEquipment->add($data); } else { $pfNetworkEquipment->update($data); } $this->deleteFromDB($items_id, 1); $Import++; break; case 'Peripheral': $Peripheral = new Peripheral(); $data["entities_id"] = $this->fields["entities_id"]; if (!empty($this->fields["name"])) { $data["name"] = $this->fields["name"]; } $data["locations_id"] = $this->fields["locations_id"]; $data["serial"] = $this->fields["serial"]; $data["otherserial"] = $this->fields["otherserial"]; $data["contact"] = $this->fields["contact"]; $data["comment"] = $this->fields["comment"]; $data["is_dynamic"] = 1; $Peripheral_id = $Peripheral->add($data); foreach ($a_NetworkPorts as $data_Port) { $data_Port['items_id'] = $Peripheral_id; $data_Port['itemtype'] = $Peripheral->getType(); $NetworkPort->update($data_Port); } $this->deleteFromDB($items_id, 1); $Import++; break; case 'Computer': $Computer = new Computer(); $data["entities_id"] = $this->fields["entities_id"]; if (!empty($this->fields["name"])) { $data["name"] = $this->fields["name"]; } $data["locations_id"] = $this->fields["locations_id"]; $data["serial"] = $this->fields["serial"]; $data["otherserial"] = $this->fields["otherserial"]; $data["contact"] = $this->fields["contact"]; $data["domain"] = $this->fields["domain"]; $data["comment"] = $this->fields["comment"]; $data["is_dynamic"] = 1; $Computer_id = $Computer->add($data); foreach ($a_NetworkPorts as $data_Port) { $data_Port['items_id'] = $Computer_id; $data_Port['itemtype'] = $Computer->getType(); $NetworkPort->update($data_Port); } $this->deleteFromDB($items_id, 1); $Import++; break; case 'Phone': $Phone = new Phone(); $data["entities_id"] = $this->fields["entities_id"]; $data["name"] = $this->fields["name"]; $data["locations_id"] = $this->fields["locations_id"]; $data["serial"] = $this->fields["serial"]; $data["otherserial"] = $this->fields["otherserial"]; $data["contact"] = $this->fields["contact"]; $data["comment"] = $this->fields["comment"]; $data["is_dynamic"] = 1; $phone_id = $Phone->add($data); foreach ($a_NetworkPorts as $data_Port) { $data_Port['items_id'] = $phone_id; $data_Port['itemtype'] = $Phone->getType(); $NetworkPort->update($data_Port); } $this->deleteFromDB($items_id, 1); $Import++; break; default: $NoImport++; break; } return array($Import, $NoImport); }
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); } } } } } }
function importPorts($a_inventory, $items_id) { $pfNetworkporttype = new PluginFusioninventoryNetworkporttype(); $networkPort = new NetworkPort(); $pfNetworkPort = new PluginFusioninventoryNetworkPort(); $networkports_id = 0; foreach ($a_inventory['networkport'] as $a_port) { $ifType = $a_port['iftype']; if ($pfNetworkporttype->isImportType($ifType) || isset($a_inventory['aggregate'][$a_port['logical_number']]) || $ifType == '') { $a_ports_DB = current($networkPort->find("`itemtype`='NetworkEquipment'\n AND `items_id`='" . $items_id . "'\n AND `logical_number` = '" . $a_port['logical_number'] . "'", '', 1)); if (!isset($a_ports_DB['id'])) { // Add port if (isset($a_inventory['aggregate']) && isset($a_inventory['aggregate'][$a_port['logical_number']])) { $a_port['instantiation_type'] = 'NetworkPortAggregate'; } else { $a_port['instantiation_type'] = 'NetworkPortEthernet'; } $a_port['items_id'] = $items_id; $a_port['itemtype'] = 'NetworkEquipment'; $networkports_id = $networkPort->add($a_port); unset($a_port['id']); $a_pfnetworkport_DB = current($pfNetworkPort->find("`networkports_id`='" . $networkports_id . "'", '', 1)); $a_port['id'] = $a_pfnetworkport_DB['id']; $a_port['lastup'] = date('Y-m-d H:i:s'); $pfNetworkPort->update($a_port); } else { // Update port $networkports_id = $a_ports_DB['id']; $a_port['id'] = $a_ports_DB['id']; $networkPort->update($a_port); unset($a_port['id']); // Check if pfnetworkport exist. $a_pfnetworkport_DB = current($pfNetworkPort->find("`networkports_id`='" . $networkports_id . "'", '', 1)); $a_port['networkports_id'] = $networkports_id; if (isset($a_pfnetworkport_DB['id'])) { $a_port['id'] = $a_pfnetworkport_DB['id']; if ($a_port['ifstatus'] == 0 && $a_pfnetworkport_DB['ifstatus'] == 1) { $a_port['lastup'] = date('Y-m-d H:i:s'); } $pfNetworkPort->update($a_port); } else { $a_port['networkports_id'] = $networkports_id; $a_port['lastup'] = date('Y-m-d H:i:s'); $pfNetworkPort->add($a_port); } } // Connections if (isset($a_inventory['connection-lldp'][$a_port['logical_number']])) { $this->importConnectionLLDP($a_inventory['connection-lldp'][$a_port['logical_number']], $networkports_id); } else { if (isset($a_inventory['connection-mac'][$a_port['logical_number']])) { $this->importConnectionMac($a_inventory['connection-mac'][$a_port['logical_number']], $networkports_id); } } // Vlan if (isset($a_inventory['vlans'][$a_port['logical_number']])) { $this->importPortVlan($a_inventory['vlans'][$a_port['logical_number']], $networkports_id); } // Aggegation if (isset($a_inventory['aggregate']) && isset($a_inventory['aggregate'][$a_port['logical_number']])) { $this->importPortAggregate($a_inventory['aggregate'][$a_port['logical_number']], $networkports_id, $items_id); } } else { // Delete the port $a_ports_DB = current($networkPort->find("`itemtype`='NetworkEquipment'\n AND `items_id`='" . $items_id . "'\n AND `logical_number` = '" . $a_port['logical_number'] . "'", '', 1)); if (count($a_ports_DB) > 0) { $networkPort->delete($a_ports_DB); } } } }
/** * Import the devices for a computer * * @param $devicetype integer : device type * @param $computers_id integer : glpi computer id. * @param $ocsid integer : ocs computer id (ID). * @param $ocsservers_id integer : ocs server id * @param $cfg_ocs array : ocs config * @param $import_device array : already imported devices * @param $import_ip array : already imported ip * @param $dohistory boolean : log changes ? * * @return Nothing (void). **/ static function updateDevices($devicetype, $computers_id, $ocsid, $ocsservers_id, $cfg_ocs, $import_device, $import_ip, $dohistory) { global $DBocs, $DB; $prevalue = $devicetype . self::FIELD_SEPARATOR; self::checkOCSconnection($ocsservers_id); $types = Computer_Device::getDeviceTypes(); $CompDevice = new Computer_Device($types[$devicetype]); $do_clean = false; switch ($devicetype) { case self::RAM_DEVICE: //Memoire if ($cfg_ocs["import_device_memory"]) { $do_clean = true; $query2 = "SELECT *\n FROM `memories`\n WHERE `HARDWARE_ID` = '{$ocsid}'\n ORDER BY `ID`"; $result2 = $DBocs->query($query2); if ($DBocs->numrows($result2) > 0) { // Drop all memories and force no history if (!in_array(self::IMPORT_TAG_078, $import_device)) { self::addToOcsArray($computers_id, array(0 => self::IMPORT_TAG_078), "import_device"); // Clean memories for this computer if (count($import_device)) { $dohistory = false; foreach ($import_device as $key => $val) { $tmp = explode(self::FIELD_SEPARATOR, $key); if (isset($tmp[1]) && $tmp[0] == self::RAM_DEVICE) { $CompDevice->delete(array('id' => $tmp[1], '_no_history' => true, '_itemtype' => 'DeviceMemory')); self::deleteInOcsArray($computers_id, $key, "import_device"); unset($import_device[$key]); } } } } while ($line2 = $DBocs->fetch_array($result2)) { $line2 = clean_cross_side_scripting_deep(addslashes_deep($line2)); if (!empty($line2["CAPACITY"]) && $line2["CAPACITY"] != "No") { $ram["designation"] = ""; if ($line2["TYPE"] != "Empty Slot" && $line2["TYPE"] != "Unknown") { $ram["designation"] = $line2["TYPE"]; } if ($line2["DESCRIPTION"]) { if (!empty($ram["designation"])) { $ram["designation"] .= " - "; } $ram["designation"] .= $line2["DESCRIPTION"]; } if (!is_numeric($line2["CAPACITY"])) { $line2["CAPACITY"] = 0; } $ram["specif_default"] = $line2["CAPACITY"]; if (!in_array(stripslashes($prevalue . $ram["designation"]), $import_device)) { $ram["frequence"] = $line2["SPEED"]; $ram["devicememorytypes_id"] = Dropdown::importExternal('DeviceMemoryType', $line2["TYPE"]); $DeviceMemory = new DeviceMemory(); $ram_id = $DeviceMemory->import($ram); if ($ram_id) { $devID = $CompDevice->add(array('computers_id' => $computers_id, '_itemtype' => 'DeviceMemory', 'devicememories_id' => $ram_id, 'specificity' => $line2["CAPACITY"], '_no_history' => !$dohistory)); self::addToOcsArray($computers_id, array($prevalue . $devID => $prevalue . $ram["designation"]), "import_device"); } } else { $tmp = array_search(stripslashes($prevalue . $ram["designation"]), $import_device); list($type, $id) = explode(self::FIELD_SEPARATOR, $tmp); $CompDevice->update(array('id' => $id, 'specificity' => $line2["CAPACITY"], '_itemtype' => 'DeviceMemory')); unset($import_device[$tmp]); } } } } } break; case self::HDD_DEVICE: //Disque Dur if ($cfg_ocs["import_device_hdd"]) { $do_clean = true; $query2 = "SELECT *\n FROM `storages`\n WHERE `HARDWARE_ID` = '{$ocsid}'\n ORDER BY `ID`"; $result2 = $DBocs->query($query2); if ($DBocs->numrows($result2) > 0) { while ($line2 = $DBocs->fetch_array($result2)) { $line2 = clean_cross_side_scripting_deep(addslashes_deep($line2)); if (!empty($line2["DISKSIZE"]) && preg_match("/disk|spare\\sdrive/i", $line2["TYPE"])) { if ($line2["NAME"]) { $dd["designation"] = $line2["NAME"]; } else { if ($line2["MODEL"]) { $dd["designation"] = $line2["MODEL"]; } else { $dd["designation"] = "Unknown"; } } if (!is_numeric($line2["DISKSIZE"])) { $line2["DISKSIZE"] = 0; } if (!in_array(stripslashes($prevalue . $dd["designation"]), $import_device)) { $dd["specif_default"] = $line2["DISKSIZE"]; $DeviceHardDrive = new DeviceHardDrive(); $dd_id = $DeviceHardDrive->import($dd); if ($dd_id) { $devID = $CompDevice->add(array('computers_id' => $computers_id, '_itemtype' => 'DeviceHardDrive', 'deviceharddrives_id' => $dd_id, 'specificity' => $line2["DISKSIZE"], '_no_history' => !$dohistory)); self::addToOcsArray($computers_id, array($prevalue . $devID => $prevalue . $dd["designation"]), "import_device"); } } else { $tmp = array_search(stripslashes($prevalue . $dd["designation"]), $import_device); list($type, $id) = explode(self::FIELD_SEPARATOR, $tmp); $CompDevice->update(array('id' => $id, 'specificity' => $line2["DISKSIZE"], '_itemtype' => 'DeviceHardDrive')); unset($import_device[$tmp]); } } } } } break; case self::DRIVE_DEVICE: //lecteurs if ($cfg_ocs["import_device_drive"]) { $do_clean = true; $query2 = "SELECT *\n FROM `storages`\n WHERE `HARDWARE_ID` = '{$ocsid}'\n ORDER BY `ID`"; $result2 = $DBocs->query($query2); if ($DBocs->numrows($result2) > 0) { while ($line2 = $DBocs->fetch_array($result2)) { $line2 = clean_cross_side_scripting_deep(addslashes_deep($line2)); if (empty($line2["DISKSIZE"]) || !preg_match("/disk/i", $line2["TYPE"])) { if ($line2["NAME"]) { $stor["designation"] = $line2["NAME"]; } else { if ($line2["MODEL"]) { $stor["designation"] = $line2["MODEL"]; } else { $stor["designation"] = "Unknown"; } } if (!in_array(stripslashes($prevalue . $stor["designation"]), $import_device)) { $stor["specif_default"] = $line2["DISKSIZE"]; $DeviceDrive = new DeviceDrive(); $stor_id = $DeviceDrive->import($stor); if ($stor_id) { $devID = $CompDevice->add(array('computers_id' => $computers_id, '_itemtype' => 'DeviceDrive', 'devicedrives_id' => $stor_id, '_no_history' => !$dohistory)); self::addToOcsArray($computers_id, array($prevalue . $devID => $prevalue . $stor["designation"]), "import_device"); } } else { $tmp = array_search(stripslashes($prevalue . $stor["designation"]), $import_device); unset($import_device[$tmp]); } } } } } break; case self::PCI_DEVICE: //Modems if ($cfg_ocs["import_device_modem"]) { $do_clean = true; $query2 = "SELECT *\n FROM `modems`\n WHERE `HARDWARE_ID` = '{$ocsid}'\n ORDER BY `ID`"; $result2 = $DBocs->query($query2); if ($DBocs->numrows($result2) > 0) { while ($line2 = $DBocs->fetch_array($result2)) { $line2 = clean_cross_side_scripting_deep(addslashes_deep($line2)); $mdm["designation"] = $line2["NAME"]; if (!in_array(stripslashes($prevalue . $mdm["designation"]), $import_device)) { if (!empty($line2["DESCRIPTION"])) { $mdm["comment"] = $line2["TYPE"] . "\r\n" . $line2["DESCRIPTION"]; } $DevicePci = new DevicePci(); $mdm_id = $DevicePci->import($mdm); if ($mdm_id) { $devID = $CompDevice->add(array('computers_id' => $computers_id, '_itemtype' => 'DevicePci', 'devicepcis_id' => $mdm_id, '_no_history' => !$dohistory)); self::addToOcsArray($computers_id, array($prevalue . $devID => $prevalue . $mdm["designation"]), "import_device"); } } else { $tmp = array_search(stripslashes($prevalue . $mdm["designation"]), $import_device); unset($import_device[$tmp]); } } } } //Ports if ($cfg_ocs["import_device_port"]) { $query2 = "SELECT *\n FROM `ports`\n WHERE `HARDWARE_ID` = '{$ocsid}'\n ORDER BY `ID`"; $result2 = $DBocs->query($query2); if ($DBocs->numrows($result2) > 0) { while ($line2 = $DBocs->fetch_array($result2)) { $line2 = clean_cross_side_scripting_deep(addslashes_deep($line2)); $port["designation"] = ""; if ($line2["TYPE"] != "Other") { $port["designation"] .= $line2["TYPE"]; } if ($line2["NAME"] != "Not Specified") { $port["designation"] .= " " . $line2["NAME"]; } else { if ($line2["CAPTION"] != "None") { $port["designation"] .= " " . $line2["CAPTION"]; } } if (!empty($port["designation"])) { if (!in_array(stripslashes($prevalue . $port["designation"]), $import_device)) { if (!empty($line2["DESCRIPTION"]) && $line2["DESCRIPTION"] != "None") { $port["comment"] = $line2["DESCRIPTION"]; } $DevicePci = new DevicePci(); $port_id = $DevicePci->import($port); if ($port_id) { $devID = $CompDevice->add(array('computers_id' => $computers_id, '_itemtype' => 'DevicePci', 'devicepcis_id' => $port_id, '_no_history' => !$dohistory)); self::addToOcsArray($computers_id, array($prevalue . $devID => $prevalue . $port["designation"]), "import_device"); } } else { $tmp = array_search(stripslashes($prevalue . $port["designation"]), $import_device); unset($import_device[$tmp]); } } } } } break; case self::PROCESSOR_DEVICE: //Processeurs : if ($cfg_ocs["import_device_processor"]) { $do_clean = true; $query = "SELECT *\n FROM `hardware`\n WHERE `ID` = '{$ocsid}'\n ORDER BY `ID`"; $result = $DBocs->query($query); if ($DBocs->numrows($result) == 1) { $line = $DBocs->fetch_array($result); $line = clean_cross_side_scripting_deep(addslashes_deep($line)); for ($i = 0; $i < $line["PROCESSORN"]; $i++) { $processor = array(); $processor["designation"] = $line["PROCESSORT"]; if (!is_numeric($line["PROCESSORS"])) { $line["PROCESSORS"] = 0; } $processor["specif_default"] = $line["PROCESSORS"]; if (!in_array(stripslashes($prevalue . $processor["designation"]), $import_device)) { $DeviceProcessor = new DeviceProcessor(); $proc_id = $DeviceProcessor->import($processor); if ($proc_id) { $devID = $CompDevice->add(array('computers_id' => $computers_id, '_itemtype' => 'DeviceProcessor', 'deviceprocessors_id' => $proc_id, 'specificity' => $line["PROCESSORS"], '_no_history' => !$dohistory)); self::addToOcsArray($computers_id, array($prevalue . $devID => $prevalue . $processor["designation"]), "import_device"); } } else { $tmp = array_search(stripslashes($prevalue . $processor["designation"]), $import_device); list($type, $id) = explode(self::FIELD_SEPARATOR, $tmp); $CompDevice->update(array('id' => $id, 'specificity' => $line["PROCESSORS"], '_itemtype' => 'DeviceProcessor')); unset($import_device[$tmp]); } } } } break; case self::NETWORK_DEVICE: //Carte reseau if ($cfg_ocs["import_device_iface"] || $cfg_ocs["import_ip"]) { $do_clean = true; //If import_ip doesn't contain _VERSION_072_, then migrate it to the new architecture if (!in_array(self::IMPORT_TAG_072, $import_ip)) { $import_ip = self::migrateImportIP($computers_id, $import_ip); } $query2 = "SELECT *\n FROM `networks`\n WHERE `HARDWARE_ID` = '{$ocsid}'\n ORDER BY `ID`"; $result2 = $DBocs->query($query2); $i = 0; $manually_link = false; //Count old ip in GLPI $count_ip = count($import_ip); // Add network device if ($DBocs->numrows($result2) > 0) { $mac_already_imported = array(); while ($line2 = $DBocs->fetch_array($result2)) { $line2 = clean_cross_side_scripting_deep(addslashes_deep($line2)); if ($cfg_ocs["import_device_iface"]) { $network["designation"] = $line2["DESCRIPTION"]; if (!in_array($line2["MACADDR"], $mac_already_imported)) { $mac_already_imported[] = $line2["MACADDR"]; if (!in_array(stripslashes($prevalue . $network["designation"]), $import_device)) { if (!empty($line2["SPEED"])) { $network["bandwidth"] = $line2["SPEED"]; } $DeviceNetworkCard = new DeviceNetworkCard(); $net_id = $DeviceNetworkCard->import($network); if ($net_id) { $devID = $CompDevice->add(array('computers_id' => $computers_id, '_itemtype' => 'DeviceNetworkCard', 'devicenetworkcards_id' => $net_id, 'specificity' => $line2["MACADDR"], '_no_history' => !$dohistory)); self::addToOcsArray($computers_id, array($prevalue . $devID > $prevalue . $network["designation"]), "import_device"); } } else { $tmp = array_search(stripslashes($prevalue . $network["designation"]), $import_device); list($type, $id) = explode(self::FIELD_SEPARATOR, $tmp); $CompDevice->update(array('id' => $id, 'specificity' => $line2["MACADDR"], '_itemtype' => 'DeviceNetworkCard')); unset($import_device[$tmp]); } } } if (!empty($line2["IPADDRESS"]) && $cfg_ocs["import_ip"]) { $ocs_ips = explode(",", $line2["IPADDRESS"]); $ocs_ips = array_unique($ocs_ips); sort($ocs_ips); //if never imported (only 0.72 tag in array), check if existing ones match if ($count_ip == 1) { //get old IP in DB $querySelectIDandIP = "SELECT `id`, `ip`\n FROM `glpi_networkports`\n WHERE `itemtype` = 'Computer'\n AND `items_id` = '{$computers_id}'\n AND `mac` = '" . $line2["MACADDR"] . "'\n AND `name` = '" . $line2["DESCRIPTION"] . "'"; $result = $DB->query($querySelectIDandIP); if ($DB->numrows($result) > 0) { while ($data = $DB->fetch_array($result)) { //Upate import_ip column and import_ip array self::addToOcsArray($computers_id, array($data["id"] => $data["ip"] . self::FIELD_SEPARATOR . $line2["MACADDR"]), "import_ip"); $import_ip[$data["id"]] = $data["ip"] . self::FIELD_SEPARATOR . $line2["MACADDR"]; } } } $netport = array(); $netport["mac"] = $line2["MACADDR"]; $netport["networkinterfaces_id"] = Dropdown::importExternal('NetworkInterface', $line2["TYPE"]); $netport["name"] = $line2["DESCRIPTION"]; $netport["items_id"] = $computers_id; $netport["itemtype"] = 'Computer'; $netport["netmask"] = $line2["IPMASK"]; $netport["gateway"] = $line2["IPGATEWAY"]; $netport["subnet"] = $line2["IPSUBNET"]; $np = new NetworkPort(); for ($j = 0; $j < count($ocs_ips); $j++) { //First search : look for the same port (same IP and same MAC) $id_ip = array_search($ocs_ips[$j] . self::FIELD_SEPARATOR . $line2["MACADDR"], $import_ip); //Second search : IP may have change, so look only for mac address if (!$id_ip) { //Browse the whole import_ip array foreach ($import_ip as $ID => $ip) { if ($ID > 0) { $tmp = explode(self::FIELD_SEPARATOR, $ip); //Port was found by looking at the mac address if (isset($tmp[1]) && $tmp[1] == $line2["MACADDR"]) { //Remove port in import_ip self::deleteInOcsArray($computers_id, $ID, "import_ip"); self::addToOcsArray($computers_id, array($ID => $ocs_ips[$j] . self::FIELD_SEPARATOR . $line2["MACADDR"]), "import_ip"); $import_ip[$ID] = $ocs_ips[$j] . self::FIELD_SEPARATOR . $line2["MACADDR"]; $id_ip = $ID; break; } } } } $netport['_no_history'] = !$dohistory; //Update already in DB if ($id_ip > 0) { $netport["ip"] = $ocs_ips[$j]; $netport["logical_number"] = $j; $netport["id"] = $id_ip; $np->update($netport); unset($import_ip[$id_ip]); $count_ip++; } else { //If new IP found unset($np->fields["netpoints_id"]); unset($netport["id"]); unset($np->fields["id"]); $netport["ip"] = $ocs_ips[$j]; $netport["logical_number"] = $j; $newID = $np->add($netport); //ADD to array self::addToOcsArray($computers_id, array($newID => $ocs_ips[$j] . self::FIELD_SEPARATOR . $line2["MACADDR"]), "import_ip"); $count_ip++; } } } } } } break; case self::GFX_DEVICE: //carte graphique if ($cfg_ocs["import_device_gfxcard"]) { $do_clean = true; $query2 = "SELECT DISTINCT(`NAME`) AS NAME,\n `MEMORY`\n FROM `videos`\n WHERE `HARDWARE_ID` = '{$ocsid}'\n AND `NAME` != ''\n ORDER BY `ID`"; $result2 = $DBocs->query($query2); if ($DBocs->numrows($result2) > 0) { while ($line2 = $DBocs->fetch_array($result2)) { $line2 = clean_cross_side_scripting_deep(addslashes_deep($line2)); $video["designation"] = $line2["NAME"]; if (!is_numeric($line2["MEMORY"])) { $line2["MEMORY"] = 0; } if (!in_array(stripslashes($prevalue . $video["designation"]), $import_device)) { $video["specif_default"] = $line2["MEMORY"]; $DeviceGraphicCard = new DeviceGraphicCard(); $video_id = $DeviceGraphicCard->import($video); if ($video_id) { $devID = $CompDevice->add(array('computers_id' => $computers_id, '_itemtype' => 'DeviceGraphicCard', 'devicegraphiccards_id' => $video_id, 'specificity' => $line2["MEMORY"], '_no_history' => !$dohistory)); self::addToOcsArray($computers_id, array($prevalue . $devID => $prevalue . $video["designation"]), "import_device"); } } else { $tmp = array_search(stripslashes($prevalue . $video["designation"]), $import_device); list($type, $id) = explode(self::FIELD_SEPARATOR, $tmp); $CompDevice->update(array('id' => $id, 'specificity' => $line2["MEMORY"], '_itemtype' => 'DeviceGraphicCard')); unset($import_device[$tmp]); } } } } break; case self::SND_DEVICE: //carte son if ($cfg_ocs["import_device_sound"]) { $do_clean = true; $query2 = "SELECT DISTINCT(`NAME`) AS NAME,\n `DESCRIPTION`\n FROM `sounds`\n WHERE `HARDWARE_ID` = '{$ocsid}'\n AND `NAME` != ''\n ORDER BY `ID`"; $result2 = $DBocs->query($query2); if ($DBocs->numrows($result2) > 0) { while ($line2 = $DBocs->fetch_array($result2)) { $line2 = clean_cross_side_scripting_deep(addslashes_deep($line2)); if (!$cfg_ocs["ocs_db_utf8"] && !seems_utf8($line2["NAME"])) { $line2["NAME"] = encodeInUtf8($line2["NAME"]); } $snd["designation"] = $line2["NAME"]; if (!in_array(stripslashes($prevalue . $snd["designation"]), $import_device)) { if (!empty($line2["DESCRIPTION"])) { $snd["comment"] = $line2["DESCRIPTION"]; } $DeviceSoundCard = new DeviceSoundCard(); $snd_id = $DeviceSoundCard->import($snd); if ($snd_id) { $devID = $CompDevice->add(array('computers_id' => $computers_id, '_itemtype' => 'DeviceSoundCard', 'devicesoundcards_id' => $snd_id, '_no_history' => !$dohistory)); self::addToOcsArray($computers_id, array($prevalue . $devID => $prevalue . $snd["designation"]), "import_device"); } } else { $id = array_search(stripslashes($prevalue . $snd["designation"]), $import_device); unset($import_device[$id]); } } } } break; } // Delete Unexisting Items not found in OCS if ($do_clean && count($import_device)) { foreach ($import_device as $key => $val) { if (!(strpos($key, $devicetype . '$$') === false)) { list($type, $id) = explode(self::FIELD_SEPARATOR, $key); $CompDevice->delete(array('id' => $id, '_itemtype' => $types[$devicetype], '_no_history' => !$dohistory)); self::deleteInOcsArray($computers_id, $key, "import_device"); } } } if ($do_clean && count($import_ip) && $devicetype == self::NETWORK_DEVICE) { foreach ($import_ip as $key => $val) { if ($key > 0) { $netport = new NetworkPort(); $netport->delete(array('id' => $key)); self::deleteInOcsArray($computers_id, $key, "import_ip"); } } } //Alimentation //Carte mere }