Пример #1
0
 /**
  * Get the Device list name the user is allowed to edit
  *
  * @return array (group of dropdown) of array (itemtype => localized name)
  **/
 static function getDeviceItemTypes()
 {
     global $CFG_GLPI;
     static $optgroup = NULL;
     if (!Session::haveRight('device', 'r')) {
         return array();
     }
     if (is_null($optgroup)) {
         $optgroup = array(_n('Component', 'Components', 2) => array('DeviceMotherboard' => DeviceMotherboard::getTypeName(2), 'DeviceProcessor' => DeviceProcessor::getTypeName(2), 'DeviceNetworkCard' => DeviceNetworkCard::getTypeName(2), 'DeviceMemory' => DeviceMemory::getTypeName(2), 'DeviceHardDrive' => DeviceHardDrive::getTypeName(2), 'DeviceDrive' => DeviceDrive::getTypeName(2), 'DeviceControl' => DeviceControl::getTypeName(2), 'DeviceGraphicCard' => DeviceGraphicCard::getTypeName(2), 'DeviceSoundCard' => DeviceSoundCard::getTypeName(2), 'DeviceCase' => DeviceCase::getTypeName(2), 'DevicePowerSupply' => DevicePowerSupply::getTypeName(2), 'DevicePci' => DevicePci::getTypeName(2)));
     }
     return $optgroup;
 }
 /**
  * Import the devices for a computer
  *
  * @param $devicetype integer : device type
  * @param $computers_id integer : glpi computer id.
  * @param $ocsComputer
  * @param $plugin_ocsinventoryng_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
  * @return Nothing .
  * @internal param int $ocsid : ocs computer id (ID).
  * @internal param bool $dohistory : log changes?
  *
  */
 static function updateDevices($devicetype, $computers_id, $ocsComputer, $plugin_ocsinventoryng_ocsservers_id, $cfg_ocs, $import_device, $import_ip)
 {
     $prevalue = $devicetype . self::FIELD_SEPARATOR;
     $do_clean = false;
     $comp = new Computer();
     $comp->getFromDB($computers_id);
     $entities_id = $comp->fields['entities_id'];
     switch ($devicetype) {
         case "PluginOcsinventoryngItem_DeviceBiosdata":
             $CompDevice = new $devicetype();
             //Bios
             $do_clean = true;
             $bios["designation"] = $ocsComputer["BVERSION"];
             $bios["assettag"] = $ocsComputer["ASSETTAG"];
             $bios["entities_id"] = $entities_id;
             //$date = str_replace("/", "-", $ocsComputer['BIOS']["BDATE"]);
             //$date = date("Y-m-d", strtotime($date));
             $bios["date"] = $ocsComputer["BDATE"];
             $bios["manufacturers_id"] = Dropdown::importExternal('Manufacturer', self::encodeOcsDataInUtf8($cfg_ocs['ocs_db_utf8'], $ocsComputer["SMANUFACTURER"]));
             if (!in_array(stripslashes($prevalue . $bios["designation"]), $import_device)) {
                 $DeviceBios = new PluginOcsinventoryngDeviceBiosdata();
                 $bios_id = $DeviceBios->import($bios);
                 if ($bios_id) {
                     $CompDevice->add(array('items_id' => $computers_id, 'itemtype' => 'Computer', 'plugin_ocsinventoryng_devicebiosdatas_id' => $bios_id, 'is_dynamic' => 1, 'entities_id' => $entities_id), array(), $cfg_ocs['history_devices']);
                 }
             } else {
                 $tmp = array_search(stripslashes($prevalue . $bios["designation"]), $import_device);
                 unset($import_device[$tmp]);
             }
             break;
         case "Item_DeviceMemory":
             //MEMORIES
             $CompDevice = new $devicetype();
             $do_clean = true;
             foreach ($ocsComputer as $line2) {
                 $line2 = Toolbox::clean_cross_side_scripting_deep(Toolbox::addslashes_deep($line2));
                 if (isset($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["size_default"] = $line2["CAPACITY"];
                     $ram["entities_id"] = $entities_id;
                     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) {
                             $CompDevice->add(array('items_id' => $computers_id, 'itemtype' => 'Computer', 'entities_id' => $entities_id, 'devicememories_id' => $ram_id, 'size' => $line2["CAPACITY"], 'is_dynamic' => 1), array(), $cfg_ocs['history_devices']);
                         }
                     } else {
                         $tmp = array_search(stripslashes($prevalue . $ram["designation"]), $import_device);
                         list($type, $id) = explode(self::FIELD_SEPARATOR, $tmp);
                         $CompDevice->update(array('id' => $id, 'size' => $line2["CAPACITY"]), $cfg_ocs['history_devices']);
                         unset($import_device[$tmp]);
                     }
                 }
             }
             break;
         case "Item_DeviceHardDrive":
             $CompDevice = new $devicetype();
             //Disque Dur
             $do_clean = true;
             foreach ($ocsComputer as $line2) {
                 $line2 = Toolbox::clean_cross_side_scripting_deep(Toolbox::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;
                     }
                     $dd["entities_id"] = $entities_id;
                     if (!in_array(stripslashes($prevalue . $dd["designation"]), $import_device)) {
                         $dd["capacity_default"] = $line2["DISKSIZE"];
                         $DeviceHardDrive = new DeviceHardDrive();
                         $dd_id = $DeviceHardDrive->import($dd);
                         if ($dd_id) {
                             $CompDevice->add(array('items_id' => $computers_id, 'itemtype' => 'Computer', 'entities_id' => $entities_id, 'deviceharddrives_id' => $dd_id, 'serial' => $line2["SERIALNUMBER"], 'capacity' => $line2["DISKSIZE"], 'is_dynamic' => 1), array(), $cfg_ocs['history_devices']);
                         }
                     } else {
                         $tmp = array_search(stripslashes($prevalue . $dd["designation"]), $import_device);
                         list($type, $id) = explode(self::FIELD_SEPARATOR, $tmp);
                         $CompDevice->update(array('id' => $id, 'capacity' => $line2["DISKSIZE"], 'serial' => $line2["SERIALNUMBER"]), $cfg_ocs['history_devices']);
                         unset($import_device[$tmp]);
                     }
                 }
             }
             break;
         case "Item_DeviceDrive":
             $CompDevice = new $devicetype();
             //lecteurs
             $do_clean = true;
             foreach ($ocsComputer as $line2) {
                 $line2 = Toolbox::clean_cross_side_scripting_deep(Toolbox::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";
                         }
                     }
                     $stor["entities_id"] = $entities_id;
                     if (!in_array(stripslashes($prevalue . $stor["designation"]), $import_device)) {
                         $DeviceDrive = new DeviceDrive();
                         $stor_id = $DeviceDrive->import($stor);
                         if ($stor_id) {
                             $CompDevice->add(array('items_id' => $computers_id, 'itemtype' => 'Computer', 'entities_id' => $entities_id, 'devicedrives_id' => $stor_id, 'is_dynamic' => 1), array(), $cfg_ocs['history_devices']);
                         }
                     } else {
                         $tmp = array_search(stripslashes($prevalue . $stor["designation"]), $import_device);
                         unset($import_device[$tmp]);
                     }
                 }
             }
             break;
         case "Item_DevicePci":
             if (isset($ocsComputer['MODEMS'])) {
                 $CompDevice = new $devicetype();
                 //Modems
                 $do_clean = true;
                 foreach ($ocsComputer['MODEMS'] as $line2) {
                     $line2 = Toolbox::clean_cross_side_scripting_deep(Toolbox::addslashes_deep($line2));
                     $mdm["designation"] = $line2["NAME"];
                     $mdm["entities_id"] = $entities_id;
                     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) {
                             $CompDevice->add(array('items_id' => $computers_id, 'itemtype' => 'Computer', 'entities_id' => $entities_id, 'devicepcis_id' => $mdm_id, 'is_dynamic' => 1), array(), $cfg_ocs['history_devices']);
                         }
                     } else {
                         $tmp = array_search(stripslashes($prevalue . $mdm["designation"]), $import_device);
                         unset($import_device[$tmp]);
                     }
                 }
             }
             //Ports
             if (isset($ocsComputer['PORTS'])) {
                 $CompDevice = new $devicetype();
                 foreach ($ocsComputer['PORTS'] as $line2) {
                     $line2 = Toolbox::clean_cross_side_scripting_deep(Toolbox::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"];
                         }
                     }
                     $port["entities_id"] = $entities_id;
                     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) {
                                 $CompDevice->add(array('items_id' => $computers_id, 'itemtype' => 'Computer', 'entities_id' => $entities_id, 'devicepcis_id' => $port_id, 'is_dynamic' => 1), array(), $cfg_ocs['history_devices']);
                             }
                         } else {
                             $tmp = array_search(stripslashes($prevalue . $port["designation"]), $import_device);
                             unset($import_device[$tmp]);
                         }
                     }
                 }
             }
             //Slots
             if (isset($ocsComputer['SLOTS'])) {
                 $CompDevice = new $devicetype();
                 $do_clean = true;
                 foreach ($ocsComputer['SLOTS'] as $line2) {
                     $line2 = Toolbox::clean_cross_side_scripting_deep(Toolbox::addslashes_deep($line2));
                     if ($line2['NAME']) {
                         if (!$cfg_ocs["ocs_db_utf8"] && !Toolbox::seems_utf8($line2["NAME"])) {
                             $line2["NAME"] = Toolbox::encodeInUtf8($line2["NAME"]);
                         }
                         $pci["entities_id"] = $entities_id;
                         $pci["designation"] = $line2["NAME"];
                         if (!in_array(stripslashes($prevalue . $pci["designation"]), $import_device)) {
                             if (!empty($line2["DESCRIPTION"])) {
                                 $pci["comment"] = $line2["DESCRIPTION"];
                             }
                             $DevicePci = new DevicePci();
                             $pci_id = $DevicePci->import($pci);
                             if ($pci_id) {
                                 $CompDevice->add(array('items_id' => $computers_id, 'itemtype' => 'Computer', 'entities_id' => $entities_id, 'devicepcis_id' => $pci_id, 'is_dynamic' => 1), array(), $cfg_ocs['history_devices']);
                             }
                         } else {
                             $id = array_search(stripslashes($prevalue . $pci["designation"]), $import_device);
                             unset($import_device[$id]);
                         }
                     }
                 }
             }
             break;
         case "Item_DeviceProcessor":
             $CompDevice = new $devicetype();
             //Processeurs:
             $do_clean = true;
             foreach ($ocsComputer as $line2) {
                 $line2 = Toolbox::clean_cross_side_scripting_deep(Toolbox::addslashes_deep($line2));
                 $processor = array();
                 $processor["designation"] = $line2["TYPE"];
                 if (!is_numeric($line2["SPEED"])) {
                     $line2["SPEED"] = 0;
                 }
                 $processor["manufacturers_id"] = Dropdown::importExternal('Manufacturer', self::encodeOcsDataInUtf8($cfg_ocs['ocs_db_utf8'], $line2["MANUFACTURER"]));
                 $processor["frequency_default"] = $line2["SPEED"];
                 $processor["nbcores_default"] = $line2["CORES"];
                 //$processor["nbthreads_default"] = $line2["LOGICAL_CPUS"];
                 $processor["frequence"] = $line2["CURRENT_SPEED"];
                 $processor["entities_id"] = $entities_id;
                 if (!in_array(stripslashes($prevalue . $processor["designation"]), $import_device)) {
                     $DeviceProcessor = new DeviceProcessor();
                     $proc_id = $DeviceProcessor->import($processor);
                     if ($proc_id) {
                         $CompDevice->add(array('items_id' => $computers_id, 'itemtype' => 'Computer', 'entities_id' => $entities_id, 'deviceprocessors_id' => $proc_id, 'frequency' => $line2["SPEED"], 'is_dynamic' => 1), array(), $cfg_ocs['history_devices']);
                     }
                 } else {
                     $tmp = array_search(stripslashes($prevalue . $processor["designation"]), $import_device);
                     list($type, $id) = explode(self::FIELD_SEPARATOR, $tmp);
                     $CompDevice->update(array('id' => $id, 'frequency' => $line2["SPEED"]), $cfg_ocs['history_devices']);
                     unset($import_device[$tmp]);
                 }
             }
             break;
         case "Item_DeviceNetworkCard":
             //Carte reseau
             PluginOcsinventoryngNetworkPort::importNetwork($cfg_ocs, $ocsComputer, $computers_id, $entities_id);
             break;
         case "Item_DeviceGraphicCard":
             $CompDevice = new $devicetype();
             //carte graphique
             $do_clean = true;
             foreach ($ocsComputer as $line2) {
                 $line2 = Toolbox::clean_cross_side_scripting_deep(Toolbox::addslashes_deep($line2));
                 if ($line2['NAME']) {
                     $video["designation"] = $line2["NAME"];
                     $video["entities_id"] = $entities_id;
                     if (!is_numeric($line2["MEMORY"])) {
                         $line2["MEMORY"] = 0;
                     }
                     if (!in_array(stripslashes($prevalue . $video["designation"]), $import_device)) {
                         $video["memory_default"] = $line2["MEMORY"];
                         $DeviceGraphicCard = new DeviceGraphicCard();
                         $video_id = $DeviceGraphicCard->import($video);
                         if ($video_id) {
                             $CompDevice->add(array('items_id' => $computers_id, 'itemtype' => 'Computer', 'entities_id' => $entities_id, 'devicegraphiccards_id' => $video_id, 'memory' => $line2["MEMORY"], 'is_dynamic' => 1), array(), $cfg_ocs['history_devices']);
                         }
                     } else {
                         $tmp = array_search(stripslashes($prevalue . $video["designation"]), $import_device);
                         list($type, $id) = explode(self::FIELD_SEPARATOR, $tmp);
                         $CompDevice->update(array('id' => $id, 'memory' => $line2["MEMORY"]), $cfg_ocs['history_devices']);
                         unset($import_device[$tmp]);
                     }
                 }
             }
             break;
         case "Item_DeviceSoundCard":
             $CompDevice = new $devicetype();
             //carte son
             $do_clean = true;
             foreach ($ocsComputer as $line2) {
                 $line2 = Toolbox::clean_cross_side_scripting_deep(Toolbox::addslashes_deep($line2));
                 if ($line2['NAME']) {
                     if (!$cfg_ocs["ocs_db_utf8"] && !Toolbox::seems_utf8($line2["NAME"])) {
                         $line2["NAME"] = Toolbox::encodeInUtf8($line2["NAME"]);
                     }
                     $snd["entities_id"] = $entities_id;
                     $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) {
                             $CompDevice->add(array('items_id' => $computers_id, 'itemtype' => 'Computer', 'entities_id' => $entities_id, 'devicesoundcards_id' => $snd_id, 'is_dynamic' => 1), array(), $cfg_ocs['history_devices']);
                         }
                     } else {
                         $id = array_search(stripslashes($prevalue . $snd["designation"]), $import_device);
                         unset($import_device[$id]);
                     }
                 }
             }
             break;
         case "Item_DeviceMotherboard":
             $CompDevice = new $devicetype();
             //Bios
             $do_clean = true;
             $mb["designation"] = $ocsComputer["MMODEL"];
             $mb["entities_id"] = $entities_id;
             $mb["manufacturers_id"] = Dropdown::importExternal('Manufacturer', self::encodeOcsDataInUtf8($cfg_ocs['ocs_db_utf8'], $ocsComputer["MMANUFACTURER"]));
             if (!in_array(stripslashes($prevalue . $mb["designation"]), $import_device)) {
                 $DeviceMB = new DeviceMotherboard();
                 $devicemotherboards_id = $DeviceMB->import($mb);
                 if ($devicemotherboards_id) {
                     $serial = $ocsComputer["MSN"];
                     $CompDevice->add(array('items_id' => $computers_id, 'itemtype' => 'Computer', 'devicemotherboards_id' => $devicemotherboards_id, 'is_dynamic' => 1, 'serial' => $serial, 'entities_id' => $entities_id), array(), $cfg_ocs['history_devices']);
                 }
             } else {
                 $tmp = array_search(stripslashes($prevalue . $mb["designation"]), $import_device);
                 unset($import_device[$tmp]);
             }
             break;
         case "Item_DeviceControl":
             //controllers
             $do_clean = true;
             $CompDevice = new $devicetype();
             foreach ($ocsComputer as $line2) {
                 $line2 = Toolbox::clean_cross_side_scripting_deep(Toolbox::addslashes_deep($line2));
                 if ($line2['NAME']) {
                     if (!$cfg_ocs["ocs_db_utf8"] && !Toolbox::seems_utf8($line2["NAME"])) {
                         $line2["NAME"] = Toolbox::encodeInUtf8($line2["NAME"]);
                     }
                     $ctrl["entities_id"] = $entities_id;
                     $ctrl["designation"] = $line2["NAME"];
                     //TODO : OCS TYPE = IDE Controller
                     // GLPI : interface = IDE
                     //$ctrl["interfacetypes_id"] = $line2["TYPE"];
                     $ctrl["manufacturers_id"] = Dropdown::importExternal('Manufacturer', self::encodeOcsDataInUtf8($cfg_ocs['ocs_db_utf8'], $line2["MANUFACTURER"]));
                     if (!in_array(stripslashes($prevalue . $ctrl["designation"]), $import_device)) {
                         if (!empty($line2["DESCRIPTION"])) {
                             $ctrl["comment"] = $line2["DESCRIPTION"];
                         }
                         $DeviceControl = new DeviceControl();
                         $ctrl_id = $DeviceControl->import($ctrl);
                         if ($ctrl_id) {
                             $CompDevice->add(array('items_id' => $computers_id, 'itemtype' => 'Computer', 'entities_id' => $entities_id, 'devicecontrols_id' => $ctrl_id, 'is_dynamic' => 1), array(), $cfg_ocs['history_devices']);
                         }
                     } else {
                         $id = array_search(stripslashes($prevalue . $ctrl["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 = new $devicetype();
                 $CompDevice->delete(array('id' => $id, '_no_history' => !$cfg_ocs['history_devices']), true, $cfg_ocs['history_devices']);
             }
         }
     }
     //TODO Import IP
     if ($do_clean && count($import_ip) && $devicetype == "Item_DeviceNetworkCard") {
         foreach ($import_ip as $key => $val) {
             if ($key > 0) {
                 $netport = new NetworkPort();
                 $netport->delete(array('id' => $key, '_no_history' => !$cfg_ocs['history_network']), 0, $cfg_ocs['history_network']);
             }
         }
     }
     //Alimentation
     //Carte mere
 }