} else { if (isset($_POST["restore"])) { $peripheral->check($_POST["id"], DELETE); $peripheral->restore($_POST); Event::log($_POST["id"], "peripherals", 4, "inventory", sprintf(__('%s restores an item'), $_SESSION["glpiname"])); $peripheral->redirectToList(); } else { if (isset($_POST["purge"])) { $peripheral->check($_POST["id"], PURGE); $peripheral->delete($_POST, 1); Event::log($_POST["id"], "peripherals", 4, "inventory", sprintf(__('%s purges an item'), $_SESSION["glpiname"])); $peripheral->redirectToList(); } else { if (isset($_POST["update"])) { $peripheral->check($_POST["id"], UPDATE); $peripheral->update($_POST); Event::log($_POST["id"], "peripherals", 4, "inventory", sprintf(__('%s updates an item'), $_SESSION["glpiname"])); Html::back(); } else { if (isset($_POST["unglobalize"])) { $peripheral->check($_POST["id"], UPDATE); Computer_Item::unglobalizeItem($peripheral); Event::log($_POST["id"], "peripherals", 4, "inventory", sprintf(__('%s sets unitary management'), $_SESSION["glpiname"])); Html::redirect($CFG_GLPI["root_doc"] . "/front/peripheral.form.php?id=" . $_POST["id"]); } else { Html::header(Peripheral::getTypeName(Session::getPluralNumber()), $_SERVER['PHP_SELF'], "assets", "peripheral"); $peripheral->display(array('id' => $_GET["id"], 'withtemplate' => $_GET["withtemplate"])); Html::footer(); } } }
/** * * Import peripherals from OCS * @since 1.0 * @param $cfg_ocs OCSNG mode configuration * @param $computers_id computer's id in GLPI * @param $ocsid computer's id in OCS * @param $ocsservers_id OCS server id * @param $entity the entity in which the peripheral will be created * @param $dohistory record in history link between peripheral and computer */ static function importPeripheral($cfg_ocs, $computers_id, $ocsservers_id, $ocsid, $entity, $dohistory) { global $PluginOcsinventoryngDBocs, $DB; self::checkOCSconnection($ocsservers_id); if ($cfg_ocs["import_periph"]) { $already_processed = array(); $p = new Peripheral(); $conn = new Computer_Item(); $query = "SELECT DISTINCT `CAPTION`, `MANUFACTURER`, `INTERFACE`, `TYPE`\n FROM `inputs`\n WHERE `HARDWARE_ID` = '{$ocsid}'\n AND `CAPTION` <> ''"; $result = $PluginOcsinventoryngDBocs->query($query); if ($PluginOcsinventoryngDBocs->numrows($result) > 0) { while ($line = $PluginOcsinventoryngDBocs->fetch_array($result)) { $line = Toolbox::clean_cross_side_scripting_deep(Toolbox::addslashes_deep($line)); $periph = array(); $periph["name"] = self::encodeOcsDataInUtf8($cfg_ocs["ocs_db_utf8"], $line["CAPTION"]); //Look for a monitor with the same name (and serial if possible) already connected //to this computer $query = "SELECT `p`.`id`, `gci`.`is_deleted`\n FROM `glpi_printers` as `p`, `glpi_computers_items` as `gci`\n WHERE `p`.`id` = `gci`.`items_id`\n AND `gci`.`is_dynamic`='1'\n AND `computers_id`='{$computers_id}'\n AND `itemtype`='Peripheral'\n AND `p`.`name`='" . $periph["name"] . "'"; $results = $DB->query($query); $id = false; $lock = false; if ($DB->numrows($results) > 0) { $id = $DB->result($results, 0, 'id'); $lock = $DB->result($results, 0, 'is_deleted'); } if (!$id) { // Clean peripheral object $p->reset(); if ($line["MANUFACTURER"] != "NULL") { $periph["brand"] = self::encodeOcsDataInUtf8($cfg_ocs["ocs_db_utf8"], $line["MANUFACTURER"]); } if ($line["INTERFACE"] != "NULL") { $periph["comment"] = self::encodeOcsDataInUtf8($cfg_ocs["ocs_db_utf8"], $line["INTERFACE"]); } $periph["peripheraltypes_id"] = Dropdown::importExternal('PeripheralType', $line["TYPE"]); $id_periph = 0; if ($cfg_ocs["import_periph"] == 1) { //Config says : manage peripherals as global //check if peripherals already exists in GLPI $periph["is_global"] = 1; $query = "SELECT `id`\n FROM `glpi_peripherals`\n WHERE `name` = '" . $periph["name"] . "'\n AND `is_global` = '1'\n AND `entities_id` = '{$entity}'"; $result_search = $DB->query($query); if ($DB->numrows($result_search) > 0) { //Periph is already in GLPI //Do not import anything just get periph ID for link $id_periph = $DB->result($result_search, 0, "id"); } else { $input = $periph; if ($cfg_ocs["states_id_default"] > 0) { $input["states_id"] = $cfg_ocs["states_id_default"]; } $input["entities_id"] = $entity; $id_periph = $p->add($input); } } else { if ($cfg_ocs["import_periph"] == 2) { //Config says : manage peripherals as single units //Import all peripherals as non global. $input = $periph; $input["is_global"] = 0; if ($cfg_ocs["states_id_default"] > 0) { $input["states_id"] = $cfg_ocs["states_id_default"]; } $input["entities_id"] = $entity; $id_periph = $p->add($input); } } if ($id_periph) { $already_processed[] = $id_periph; $conn = new Computer_Item(); if ($connID = $conn->add(array('computers_id' => $computers_id, 'itemtype' => 'Peripheral', 'items_id' => $id_periph, '_no_history' => !$dohistory, 'is_dynamic' => 1))) { //Update column "is_deleted" set value to 0 and set status to default $input = array(); $input["id"] = $id_periph; $input["is_deleted"] = 0; $input["entities_id"] = $entity; if ($cfg_ocs["states_id_default"] > 0) { $input["states_id"] = $cfg_ocs["states_id_default"]; } $p->update($input); } } } else { $already_processed[] = $id; } } } //Look for all monitors, not locked, not linked to the computer anymore $query = "SELECT `id`\n FROM `glpi_computers_items`\n WHERE `itemtype`='Peripheral'\n AND `computers_id`='{$computers_id}'\n AND `is_dynamic`='1'\n AND `is_deleted`='0'"; if (!empty($already_processed)) { $query .= "AND `items_id` NOT IN (" . implode(',', $already_processed) . ")"; } foreach ($DB->request($query) as $data) { //Delete all connexions $conn->delete(array('id' => $data['id'], '_ocsservers_id' => $ocsservers_id), true); } } }
/** * * Import peripherals from OCS * @since 1.0 * @param $cfg_ocs OCSNG mode configuration * @param $computers_id computer's id in GLPI * @param $ocsid computer's id in OCS * @param $ocsservers_id OCS server id * @param $entity the entity in which the peripheral will be created * @param $dohistory record in history link between peripheral and computer */ static function importPeripheral($cfg_ocs, $computers_id, $ocsservers_id, $ocsComputer, $entity, $dohistory) { global $DB; $already_processed = array(); $p = new Peripheral(); $conn = new Computer_Item(); if (isset($ocsComputer["INPUTS"])) { if (count($ocsComputer["INPUTS"]) > 0) { foreach ($ocsComputer["INPUTS"] as $peripheral) { if ($peripheral["CAPTION"] !== '') { $peripherals[] = $peripheral; } } if (count($peripherals) > 0) { foreach ($peripherals as $peripheral) { $peripheral = Toolbox::clean_cross_side_scripting_deep(Toolbox::addslashes_deep($peripheral)); $periph = array(); $periph["name"] = self::encodeOcsDataInUtf8($cfg_ocs["ocs_db_utf8"], $peripheral["CAPTION"]); //Look for a monitor with the same name (and serial if possible) already connected //to this computer $query = "SELECT `p`.`id`, `gci`.`is_deleted`\n FROM `glpi_printers` as `p`, `glpi_computers_items` as `gci`\n WHERE `p`.`id` = `gci`.`items_id`\n AND `gci`.`is_dynamic`='1'\n AND `computers_id`='{$computers_id}'\n AND `itemtype`='Peripheral'\n AND `p`.`name`='" . $periph["name"] . "'"; $results = $DB->query($query); $id = false; $lock = false; if ($DB->numrows($results) > 0) { $id = $DB->result($results, 0, 'id'); $lock = $DB->result($results, 0, 'is_deleted'); } if (!$id) { // Clean peripheral object $p->reset(); if ($peripheral["MANUFACTURER"] != "NULL") { $periph["brand"] = self::encodeOcsDataInUtf8($cfg_ocs["ocs_db_utf8"], $peripheral["MANUFACTURER"]); } if ($peripheral["INTERFACE"] != "NULL") { $periph["comment"] = self::encodeOcsDataInUtf8($cfg_ocs["ocs_db_utf8"], $peripheral["INTERFACE"]); } $periph["peripheraltypes_id"] = Dropdown::importExternal('PeripheralType', $peripheral["TYPE"]); $id_periph = 0; if ($cfg_ocs["import_periph"] == 1) { //Config says : manage peripherals as global //check if peripherals already exists in GLPI $periph["is_global"] = 1; $query = "SELECT `id`\n FROM `glpi_peripherals`\n WHERE `name` = '" . $periph["name"] . "'\n AND `is_global` = '1'\n AND `entities_id` = '{$entity}'"; $result_search = $DB->query($query); if ($DB->numrows($result_search) > 0) { //Periph is already in GLPI //Do not import anything just get periph ID for link $id_periph = $DB->result($result_search, 0, "id"); } else { $input = $periph; if ($cfg_ocs["states_id_default"] > 0) { $input["states_id"] = $cfg_ocs["states_id_default"]; } $input["entities_id"] = $entity; $id_periph = $p->add($input); } } else { if ($cfg_ocs["import_periph"] == 2) { //Config says : manage peripherals as single units //Import all peripherals as non global. $input = $periph; $input["is_global"] = 0; if ($cfg_ocs["states_id_default"] > 0) { $input["states_id"] = $cfg_ocs["states_id_default"]; } $input["entities_id"] = $entity; $id_periph = $p->add($input); } } if ($id_periph) { $already_processed[] = $id_periph; $conn = new Computer_Item(); if ($connID = $conn->add(array('computers_id' => $computers_id, 'itemtype' => 'Peripheral', 'items_id' => $id_periph, '_no_history' => !$dohistory, 'is_dynamic' => 1))) { //Update column "is_deleted" set value to 0 and set status to default $input = array(); $input["id"] = $id_periph; $input["is_deleted"] = 0; $input["entities_id"] = $entity; if ($cfg_ocs["states_id_default"] > 0) { $input["states_id"] = $cfg_ocs["states_id_default"]; } $p->update($input); } } } else { $already_processed[] = $id; } } } } } //Look for all peripherals, not locked, not linked to the computer anymore $query = "SELECT `id`\n FROM `glpi_computers_items`\n WHERE `itemtype`='Peripheral'\n AND `computers_id`='{$computers_id}'\n AND `is_dynamic`='1'\n AND `is_deleted`='0'"; if (!empty($already_processed)) { $query .= "AND `items_id` NOT IN (" . implode(',', $already_processed) . ")"; } foreach ($DB->request($query) as $data) { // Delete all connexions //Get OCS configuration $ocs_config = PluginOcsinventoryngOcsServer::getConfig($ocsservers_id); //Get the management mode for this device $mode = PluginOcsinventoryngOcsServer::getDevicesManagementMode($ocs_config, 'Peripheral'); $decoConf = $ocs_config["deconnection_behavior"]; //Change status if : // 1 : the management mode IS NOT global // 2 : a deconnection's status have been defined // 3 : unique with serial if ($mode >= 2 && strlen($decoConf) > 0) { //Delete periph from glpi if ($decoConf == "delete") { $query = "DELETE\n FROM `glpi_computers_items`\n WHERE `id`='" . $data['id'] . "'"; $result = $DB->query($query); //Put periph in dustbin } else { if ($decoConf == "trash") { $query = "UPDATE\n `glpi_computers_items`\n SET `is_deleted` = 1\n WHERE `id`='" . $data['id'] . "'"; $result = $DB->query($query); } } } // foreach ($DB->request($query) as $data){ // Delete all connexions // $conn->delete(array('id' => $data['id'], // '_ocsservers_id' => $ocsservers_id), true); } }
/** * Prepare input datas for adding the relation * * Overloaded to check is Disconnect needed (during OCS sync) * and to manage autoupdate feature * *@param $input datas used to add the item * *@return the modified $input array * **/ function prepareInputForAdd($input) { global $DB, $CFG_GLPI, $LANG; switch ($input['itemtype']) { case 'Monitor': $item = new Monitor(); $ocstab = 'import_monitor'; break; case 'Phone': // shoul really never occurs as OCS doesn't sync phone $item = new Phone(); $ocstab = ''; break; case 'Printer': $item = new Printer(); $ocstab = 'import_printer'; break; case 'Peripheral': $item = new Peripheral(); $ocstab = 'import_peripheral'; break; default: return false; } if (!$item->getFromDB($input['items_id'])) { return false; } if (!$item->getField('is_global')) { // Handle case where already used, should never happen (except from OCS sync) $query = "SELECT `id`, `computers_id`\n FROM `glpi_computers_items`\n WHERE `glpi_computers_items`.`items_id` = '" . $input['items_id'] . "'\n AND `glpi_computers_items`.`itemtype` = '" . $input['itemtype'] . "'"; $result = $DB->query($query); while ($data = $DB->fetch_assoc($result)) { $temp = clone $this; $temp->delete($data); if ($ocstab) { OcsServer::deleteInOcsArray($data["computers_id"], $data["id"], $ocstab); } } // Autoupdate some fields - should be in post_addItem (here to avoid more DB access) $comp = new Computer(); if ($comp->getFromDB($input['computers_id'])) { $updates = array(); if ($CFG_GLPI["is_location_autoupdate"] && $comp->fields['locations_id'] != $item->getField('locations_id')) { $updates['locations_id'] = addslashes($comp->fields['locations_id']); addMessageAfterRedirect($LANG['computers'][48], true); } if ($CFG_GLPI["is_user_autoupdate"] && $comp->fields['users_id'] != $item->getField('users_id') || $CFG_GLPI["is_group_autoupdate"] && $comp->fields['groups_id'] != $item->getField('groups_id')) { if ($CFG_GLPI["is_user_autoupdate"]) { $updates['users_id'] = $comp->fields['users_id']; } if ($CFG_GLPI["is_group_autoupdate"]) { $updates['groups_id'] = $comp->fields['groups_id']; } addMessageAfterRedirect($LANG['computers'][50], true); } if ($CFG_GLPI["is_contact_autoupdate"] && ($comp->fields['contact'] != $item->getField('contact') || $comp->fields['contact_num'] != $item->getField('contact_num'))) { $updates['contact'] = addslashes($comp->fields['contact']); $updates['contact_num'] = addslashes($comp->fields['contact_num']); addMessageAfterRedirect($LANG['computers'][49], true); } if ($CFG_GLPI["state_autoupdate_mode"] < 0 && $comp->fields['states_id'] != $item->getField('states_id')) { $updates['states_id'] = $comp->fields['states_id']; addMessageAfterRedirect($LANG['computers'][56], true); } if ($CFG_GLPI["state_autoupdate_mode"] > 0 && $item->getField('states_id') != $CFG_GLPI["state_autoupdate_mode"]) { $updates['states_id'] = $CFG_GLPI["state_autoupdate_mode"]; } if (count($updates)) { $updates['id'] = $input['items_id']; $item->update($updates); } } } return $input; }
/** * Import the devices for a computer * * @param $itemtype integer : item type * @param $entity integer : entity of the computer * @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_periph array : already imported periph * @param $dohistory boolean : log changes ? * * @return Nothing (void). **/ static function updatePeripherals($itemtype, $entity, $computers_id, $ocsid, $ocsservers_id, $cfg_ocs, $import_periph, $dohistory) { global $DB, $DBocs; self::checkOCSconnection($ocsservers_id); $do_clean = false; $connID = 0; //Tag for data since 0.70 for the import_monitor array. $count_monitor = count($import_periph); switch ($itemtype) { case 'Monitor': if ($cfg_ocs["import_monitor"]) { //Update data in import_monitor array for 0.70 if (!in_array(self::IMPORT_TAG_070, $import_periph)) { foreach ($import_periph as $key => $val) { $monitor_tag = $val; //delete old value self::deleteInOcsArray($computers_id, $key, "import_monitor"); //search serial when it exists $monitor_serial = ""; $query_monitor_id = "SELECT `items_id`\n FROM `glpi_computers_items`\n WHERE `id` = '{$key}'"; $result_monitor_id = $DB->query($query_monitor_id); if ($DB->numrows($result_monitor_id) == 1) { //get monitor Id $id_monitor = $DB->result($result_monitor_id, 0, "items_id"); $query_monitor_serial = "SELECT `serial`\n FROM `glpi_monitors`\n WHERE `id` = '{$id_monitor}'"; $result_monitor_serial = $DB->query($query_monitor_serial); //get serial if ($DB->numrows($result_monitor_serial) == 1) { $monitor_serial = $DB->result($result_monitor_serial, 0, "serial"); } } //concat name + serial $monitor_tag .= $monitor_serial; //add new value (serial + name when its possible) self::addToOcsArray($computers_id, array($key => $monitor_tag), "import_monitor"); //Update the array with the new value of the monitor $import_periph[$key] = $monitor_tag; } //add the tag for the array version's self::addToOcsArray($computers_id, array(0 => self::IMPORT_TAG_070), "import_monitor"); } $do_clean = true; $m = new Monitor(); $query = "SELECT DISTINCT `CAPTION`, `MANUFACTURER`, `DESCRIPTION`, `SERIAL`, `TYPE`\n FROM `monitors`\n WHERE `HARDWARE_ID` = '{$ocsid}'"; $result = $DBocs->query($query); $lines = array(); $checkserial = true; // First pass - check if all serial present if ($DBocs->numrows($result) > 0) { while ($line = $DBocs->fetch_array($result)) { if (empty($line["SERIAL"])) { $checkserial = false; } $lines[] = clean_cross_side_scripting_deep(addslashes_deep($line)); } } if (count($lines) > 0 && ($cfg_ocs["import_monitor"] <= 2 || $checkserial)) { foreach ($lines as $line) { $mon = array(); $mon["name"] = $line["CAPTION"]; if (empty($line["CAPTION"]) && !empty($line["MANUFACTURER"])) { $mon["name"] = $line["MANUFACTURER"]; } if (empty($line["CAPTION"]) && !empty($line["TYPE"])) { if (!empty($line["MANUFACTURER"])) { $mon["name"] .= " "; } $mon["name"] .= $line["TYPE"]; } $mon["serial"] = $line["SERIAL"]; $checkMonitor = $mon["name"]; if (!empty($mon["serial"])) { $checkMonitor .= $mon["serial"]; } if (!empty($mon["name"])) { $id = array_search(stripslashes($checkMonitor), $import_periph); } if ($id === false) { // Clean monitor object $m->reset(); $mon["manufacturers_id"] = Dropdown::importExternal('Manufacturer', $line["MANUFACTURER"]); if ($cfg_ocs["import_monitor_comment"]) { $mon["comment"] = $line["DESCRIPTION"]; } $id_monitor = 0; if ($cfg_ocs["import_monitor"] == 1) { //Config says : manage monitors as global //check if monitors already exists in GLPI $mon["is_global"] = 1; $query = "SELECT `id`\n FROM `glpi_monitors`\n WHERE `name` = '" . $mon["name"] . "'\n AND `is_global` = '1'\n AND `entities_id` = '{$entity}'"; $result_search = $DB->query($query); if ($DB->numrows($result_search) > 0) { //Periph is already in GLPI //Do not import anything just get periph ID for link $id_monitor = $DB->result($result_search, 0, "id"); } else { $input = $mon; if ($cfg_ocs["states_id_default"] > 0) { $input["states_id"] = $cfg_ocs["states_id_default"]; } $input["entities_id"] = $entity; $id_monitor = $m->add($input); } } else { if ($cfg_ocs["import_monitor"] >= 2) { //Config says : manage monitors as single units //Import all monitors as non global. $mon["is_global"] = 0; // Try to find a monitor with the same serial. if (!empty($mon["serial"])) { $query = "SELECT `id`\n FROM `glpi_monitors`\n WHERE `serial` LIKE '%" . $mon["serial"] . "%'\n AND `is_global` = '0'\n AND `entities_id` = '{$entity}'"; $result_search = $DB->query($query); if ($DB->numrows($result_search) == 1) { //Monitor founded $id_monitor = $DB->result($result_search, 0, "id"); } } //Search by serial failed, search by name if ($cfg_ocs["import_monitor"] == 2 && !$id_monitor) { //Try to find a monitor with no serial, the same name and not already connected. if (!empty($mon["name"])) { $query = "SELECT `glpi_monitors`.`id`\n FROM `glpi_monitors`\n LEFT JOIN `glpi_computers_items`\n ON (`glpi_computers_items`.`itemtype`='Monitor'\n AND `glpi_computers_items`.`items_id`\n =`glpi_monitors`.`id`)\n WHERE `serial` = ''\n AND `name` = '" . $mon["name"] . "'\n AND `is_global` = '0'\n AND `entities_id` = '{$entity}'\n AND `glpi_computers_items`.`computers_id` IS NULL"; $result_search = $DB->query($query); if ($DB->numrows($result_search) == 1) { $id_monitor = $DB->result($result_search, 0, "id"); } } } if (!$id_monitor) { $input = $mon; if ($cfg_ocs["states_id_default"] > 0) { $input["states_id"] = $cfg_ocs["states_id_default"]; } $input["entities_id"] = $entity; $id_monitor = $m->add($input); } } } // ($cfg_ocs["import_monitor"] >= 2) if ($id_monitor) { //Import unique : Disconnect monitor on other computer done in Connect function $conn = new Computer_Item(); $connID = $conn->add(array('computers_id' => $computers_id, 'itemtype' => 'Monitor', 'items_id' => $id_monitor, '_no_history' => !$dohistory)); if (!in_array(self::IMPORT_TAG_070, $import_periph)) { self::addToOcsArray($computers_id, array(0 => self::IMPORT_TAG_070), "import_monitor"); } if ($connID > 0) { // sanity check - Add can fail self::addToOcsArray($computers_id, array($connID => $checkMonitor), "import_monitor"); } $count_monitor++; //Update column "is_deleted" set value to 0 and set status to default $input = array(); $old = new Monitor(); if ($old->getFromDB($id_monitor)) { if ($old->fields["is_deleted"]) { $input["is_deleted"] = 0; } if ($cfg_ocs["states_id_default"] > 0 && $old->fields["states_id"] != $cfg_ocs["states_id_default"]) { $input["states_id"] = $cfg_ocs["states_id_default"]; } if (empty($old->fields["name"]) && !empty($mon["name"])) { $input["name"] = $mon["name"]; } if (empty($old->fields["serial"]) && !empty($mon["serial"])) { $input["serial"] = $mon["serial"]; } if (count($input)) { $input["id"] = $id_monitor; $input['entities_id'] = $entity; $m->update($input); } } } } else { // found in array unset($import_periph[$id]); } } // end foreach } if (in_array(self::IMPORT_TAG_070, $import_periph)) { //unset the version Tag unset($import_periph[0]); } } break; case 'Printer': if ($cfg_ocs["import_printer"]) { $do_clean = true; $query = "SELECT *\n FROM `printers`\n WHERE `HARDWARE_ID` = '{$ocsid}'"; $result = $DBocs->query($query); $p = new Printer(); if ($DBocs->numrows($result) > 0) { while ($line = $DBocs->fetch_array($result)) { $line = clean_cross_side_scripting_deep(addslashes_deep($line)); $print = array(); // TO TEST : PARSE NAME to have real name. if (!seems_utf8($line["NAME"])) { $print["name"] = encodeInUtf8($line["NAME"]); } else { $print["name"] = $line["NAME"]; } if (empty($print["name"])) { $print["name"] = $line["DRIVER"]; } $management_process = $cfg_ocs["import_printer"]; //Params for the dictionnary $params['name'] = $print['name']; $params['manufacturer'] = ""; $params['DRIVER'] = $line['DRIVER']; $params['PORT'] = $line['PORT']; if (!empty($print["name"])) { $rulecollection = new RuleDictionnaryPrinterCollection(); $res_rule = addslashes_deep($rulecollection->processAllRules($params, array(), array())); if (!isset($res_rule["_ignore_ocs_import"]) || !$res_rule["_ignore_ocs_import"]) { foreach ($res_rule as $key => $value) { if ($value != '' && $value[0] != '_') { $print[$key] = $value; } } // if (isset($res_rule['is_global'])) { // logDebug($res_rule); // } if (isset($res_rule['is_global'])) { if (!$res_rule['is_global']) { $management_process = 2; } else { $management_process = 1; } } if (!in_array(stripslashes($print["name"]), $import_periph)) { // Clean printer object $p->reset(); $print["comment"] = $line["PORT"] . "\r\n" . $line["DRIVER"]; self::analizePrinterPorts($print, $line["PORT"]); $id_printer = 0; if ($management_process == 1) { //Config says : manage printers as global //check if printers already exists in GLPI $print["is_global"] = MANAGEMENT_GLOBAL; $query = "SELECT `id`\n FROM `glpi_printers`\n WHERE `name` = '" . $print["name"] . "'\n AND `is_global` = '1'\n AND `entities_id` = '{$entity}'"; $result_search = $DB->query($query); if ($DB->numrows($result_search) > 0) { //Periph is already in GLPI //Do not import anything just get periph ID for link $id_printer = $DB->result($result_search, 0, "id"); } else { $input = $print; if ($cfg_ocs["states_id_default"] > 0) { $input["states_id"] = $cfg_ocs["states_id_default"]; } $input["entities_id"] = $entity; // if (isset($res_rule['is_global'])) { // logDebug("global",$input); // } $id_printer = $p->add($input); } } else { if ($management_process == 2) { //Config says : manage printers as single units //Import all printers as non global. $input = $print; $input["is_global"] = MANAGEMENT_UNITARY; if ($cfg_ocs["states_id_default"] > 0) { $input["states_id"] = $cfg_ocs["states_id_default"]; } $input["entities_id"] = $entity; // if (isset($res_rule['is_global'])) { // logDebug("unitary",$input); // } $id_printer = $p->add($input); } } if ($id_printer) { $conn = new Computer_Item(); $connID = $conn->add(array('computers_id' => $computers_id, 'itemtype' => 'Printer', 'items_id' => $id_printer, '_no_history' => !$dohistory)); if ($connID > 0) { // sanity check - Add can fail self::addToOcsArray($computers_id, array($connID => $print["name"]), "import_printer"); } //Update column "is_deleted" set value to 0 and set status to default $input = array(); $input["id"] = $id_printer; $input["is_deleted"] = 0; $input["entities_id"] = $entity; if ($cfg_ocs["states_id_default"] > 0) { $input["states_id"] = $cfg_ocs["states_id_default"]; } $p->update($input); } } else { $id = array_search(stripslashes($print["name"]), $import_periph); unset($import_periph[$id]); } } } } } } break; case 'Peripheral': if ($cfg_ocs["import_periph"]) { $do_clean = true; $p = new Peripheral(); $query = "SELECT DISTINCT `CAPTION`, `MANUFACTURER`, `INTERFACE`, `TYPE`\n FROM `inputs`\n WHERE `HARDWARE_ID` = '{$ocsid}'\n AND `CAPTION` <> ''"; $result = $DBocs->query($query); if ($DBocs->numrows($result) > 0) { while ($line = $DBocs->fetch_array($result)) { $line = clean_cross_side_scripting_deep(addslashes_deep($line)); $periph = array(); if (!seems_utf8($line["CAPTION"])) { $periph["name"] = encodeInUtf8($line["CAPTION"]); } else { $periph["name"] = $line["CAPTION"]; } if (!in_array(stripslashes($periph["name"]), $import_periph)) { // Clean peripheral object $p->reset(); if ($line["MANUFACTURER"] != "NULL") { $periph["brand"] = $line["MANUFACTURER"]; } if ($line["INTERFACE"] != "NULL") { $periph["comment"] = $line["INTERFACE"]; } $periph["peripheraltypes_id"] = Dropdown::importExternal('PeripheralType', $line["TYPE"]); $id_periph = 0; if ($cfg_ocs["import_periph"] == 1) { //Config says : manage peripherals as global //check if peripherals already exists in GLPI $periph["is_global"] = 1; $query = "SELECT `id`\n FROM `glpi_peripherals`\n WHERE `name` = '" . $periph["name"] . "'\n AND `is_global` = '1'\n AND `entities_id` = '{$entity}'"; $result_search = $DB->query($query); if ($DB->numrows($result_search) > 0) { //Periph is already in GLPI //Do not import anything just get periph ID for link $id_periph = $DB->result($result_search, 0, "id"); } else { $input = $periph; if ($cfg_ocs["states_id_default"] > 0) { $input["states_id"] = $cfg_ocs["states_id_default"]; } $input["entities_id"] = $entity; $id_periph = $p->add($input); } } else { if ($cfg_ocs["import_periph"] == 2) { //Config says : manage peripherals as single units //Import all peripherals as non global. $input = $periph; $input["is_global"] = 0; if ($cfg_ocs["states_id_default"] > 0) { $input["states_id"] = $cfg_ocs["states_id_default"]; } $input["entities_id"] = $entity; $id_periph = $p->add($input); } } if ($id_periph) { $conn = new Computer_Item(); if ($connID = $conn->add(array('computers_id' => $computers_id, 'itemtype' => 'Peripheral', 'items_id' => $id_periph, '_no_history' => !$dohistory))) { self::addToOcsArray($computers_id, array($connID => $periph["name"]), "import_peripheral"); //Update column "is_deleted" set value to 0 and set status to default $input = array(); $input["id"] = $id_periph; $input["is_deleted"] = 0; $input["entities_id"] = $entity; if ($cfg_ocs["states_id_default"] > 0) { $input["states_id"] = $cfg_ocs["states_id_default"]; } $p->update($input); } } } else { $id = array_search(stripslashes($periph["name"]), $import_periph); unset($import_periph[$id]); } } } } break; } // Disconnect Unexisting Items not found in OCS if ($do_clean && count($import_periph)) { $conn = new Computer_Item(); foreach ($import_periph as $key => $val) { switch ($itemtype) { case 'Monitor': // Only if sync done if ($cfg_ocs["import_monitor"] <= 2 || $checkserial) { $conn->delete(array('id' => $key, '_ocsservers_id' => $ocsservers_id)); self::deleteInOcsArray($computers_id, $key, "import_monitor"); } break; case 'Printer': $conn->delete(array('id' => $key, '_ocsservers_id' => $ocsservers_id)); self::deleteInOcsArray($computers_id, $key, "import_printer"); break; case 'Peripheral': $conn->delete(array('id' => $key, '_ocsservers_id' => $ocsservers_id)); self::deleteInOcsArray($computers_id, $key, "import_peripheral"); break; default: $conn->delete(array('id' => $key, '_ocsservers_id' => $ocsservers_id)); } } } }