/**
  *
  * Import printers from OCS
  * @since 1.0
  * @param $cfg_ocs OCSNG mode configuration
  * @param $computers_id computer's id in GLPI
  * @param $ocsservers_id OCS server id
  * @param $ocsComputer
  * @param $entity the entity in which the printer will be created
  * @internal param computer $ocsid 's id in OCS
  */
 static function importPrinter($cfg_ocs, $computers_id, $ocsservers_id, $ocsComputer, $entity)
 {
     global $DB, $CFG_GLPI;
     $already_processed = array();
     $conn = new Computer_Item();
     $p = new Printer();
     foreach ($ocsComputer as $printer) {
         $printer = Toolbox::clean_cross_side_scripting_deep(Toolbox::addslashes_deep($printer));
         $print = array();
         // TO TEST : PARSE NAME to have real name.
         $print['name'] = self::encodeOcsDataInUtf8($cfg_ocs["ocs_db_utf8"], $printer['NAME']);
         if (empty($print["name"])) {
             $print["name"] = $printer["DRIVER"];
         }
         $management_process = $cfg_ocs["import_printer"];
         //Params for the dictionnary
         $params['name'] = $print['name'];
         $params['manufacturer'] = "";
         $params['DRIVER'] = $printer['DRIVER'];
         $params['PORT'] = $printer['PORT'];
         if (!empty($print["name"])) {
             $rulecollection = new RuleDictionnaryPrinterCollection();
             $res_rule = Toolbox::addslashes_deep($rulecollection->processAllRules(Toolbox::stripslashes_deep($params), array(), array()));
             if (!isset($res_rule["_ignore_import"]) || !$res_rule["_ignore_import"]) {
                 foreach ($res_rule as $key => $value) {
                     if ($value != '' && $value[0] != '_') {
                         $print[$key] = $value;
                     }
                 }
                 if (isset($res_rule['is_global'])) {
                     if (!$res_rule['is_global']) {
                         $management_process = 2;
                     } else {
                         $management_process = 1;
                     }
                 }
                 //Look for a printer 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`\n                                     AND `computers_id`='{$computers_id}'\n                                     AND `itemtype`='Printer'\n                                     AND `p`.`name`='" . $print["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 printer object
                     $p->reset();
                     $print["comment"] = $printer["PORT"] . "\r\n" . $printer["DRIVER"];
                     self::analizePrinterPorts($print, $printer["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' ";
                         if ($CFG_GLPI['transfers_id_auto'] < 1) {
                             $query .= " 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");
                             $already_processed[] = $id_printer;
                         } else {
                             $input = $print;
                             if ($cfg_ocs["states_id_default"] > 0) {
                                 $input["states_id"] = $cfg_ocs["states_id_default"];
                             }
                             $input["entities_id"] = $entity;
                             $id_printer = $p->add($input, array(), $cfg_ocs['history_printer']);
                         }
                     } 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;
                             $input['is_dynamic'] = 1;
                             $id_printer = $p->add($input, array(), $cfg_ocs['history_printer']);
                         }
                     }
                     if ($id_printer) {
                         $already_processed[] = $id_printer;
                         $conn = new Computer_Item();
                         $connID = $conn->add(array('computers_id' => $computers_id, 'itemtype' => 'Printer', 'items_id' => $id_printer, 'is_dynamic' => 1), array(), $cfg_ocs['history_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, $cfg_ocs['history_printer']);
                     }
                 } else {
                     $already_processed[] = $id;
                 }
             }
         }
     }
     //Look for all printers, not locked, not linked to the computer anymore
     $query = "SELECT `id`\n                    FROM `glpi_computers_items`\n                    WHERE `itemtype`='Printer'\n                       AND `computers_id`='{$computers_id}'\n                       AND `is_dynamic`\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 = self::getConfig($ocsservers_id);
         //Get the management mode for this device
         $mode = self::getDevicesManagementMode($ocs_config, 'Printer');
         $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'] . "'";
                 $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'] . "'";
                     $DB->query($query);
                 }
             }
         }
         // foreach ($DB->request($query) as $data){
         // Delete all connexions
         // $conn->delete(array('id'             => $data['id'],
         // '_ocsservers_id' => $ocsservers_id), true);
     }
 }
Example #2
0
 /**
  *
  * Import printers 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 printer will be created
  * @param $dohistory record in history link between printer and computer
  */
 static function importPrinter($cfg_ocs, $computers_id, $ocsservers_id, $ocsid, $entity, $dohistory)
 {
     global $PluginOcsinventoryngDBocs, $DB;
     self::checkOCSconnection($ocsservers_id);
     if ($cfg_ocs["import_printer"]) {
         $already_processed = array();
         $conn = new Computer_Item();
         $query = "SELECT*\n                     FROM `printers`\n                     WHERE `HARDWARE_ID` = '{$ocsid}'";
         $result = $PluginOcsinventoryngDBocs->query($query);
         $p = new Printer();
         if ($PluginOcsinventoryngDBocs->numrows($result) > 0) {
             while ($line = $PluginOcsinventoryngDBocs->fetch_array($result)) {
                 $line = Toolbox::clean_cross_side_scripting_deep(Toolbox::addslashes_deep($line));
                 $print = array();
                 // TO TEST : PARSE NAME to have real name.
                 $print['name'] = self::encodeOcsDataInutf8($cfg_ocs["ocs_db_utf8"], $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 = Toolbox::addslashes_deep($rulecollection->processAllRules(Toolbox::stripslashes_deep($params), array(), array()));
                     if (!isset($res_rule["_ignore_import"]) || !$res_rule["_ignore_import"]) {
                         foreach ($res_rule as $key => $value) {
                             if ($value != '' && $value[0] != '_') {
                                 $print[$key] = $value;
                             }
                         }
                         if (isset($res_rule['is_global'])) {
                             if (!$res_rule['is_global']) {
                                 $management_process = 2;
                             } else {
                                 $management_process = 1;
                             }
                         }
                         //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`='Printer'\n                                  AND `p`.`name`='" . $print["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 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");
                                     $already_processed[] = $id_printer;
                                 } else {
                                     $input = $print;
                                     if ($cfg_ocs["states_id_default"] > 0) {
                                         $input["states_id"] = $cfg_ocs["states_id_default"];
                                     }
                                     $input["entities_id"] = $entity;
                                     $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;
                                     $input['is_dynamic'] = 1;
                                     $id_printer = $p->add($input);
                                 }
                             }
                             if ($id_printer) {
                                 $already_processed[] = $id_printer;
                                 $conn = new Computer_Item();
                                 $connID = $conn->add(array('computers_id' => $computers_id, 'itemtype' => 'Printer', 'items_id' => $id_printer, '_no_history' => !$dohistory, 'is_dynamic' => 1));
                                 //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 {
                             $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`='Printer'\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 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));
             }
         }
     }
 }