/** function buildTicket - Builds,and returns, the major structure of the ticket to be entered. * * @param $i mail ID * @param $options array options * * @return ticket fields array */ function buildTicket($i, $options = array()) { $play_rules = isset($options['play_rules']) && $options['play_rules']; $head = $this->getHeaders($i); // Get Header Info Return Array Of Headers // **Key Are (subject,to,toOth,toNameOth,from,fromName) $tkt = array(); $tkt['_blacklisted'] = false; // Detect if it is a mail reply $glpi_message_match = "/GLPI-([0-9]+)\\.[0-9]+\\.[0-9]+@\\w*/"; // Check if email not send by GLPI : if yes -> blacklist if (preg_match($glpi_message_match, $head['message_id'], $match)) { $tkt['_blacklisted'] = true; return $tkt; } // max size = 0 : no import attachments if ($this->fields['filesize_max'] > 0) { if (is_writable(GLPI_DOC_DIR . "/_tmp/")) { $_FILES = $this->getAttached($i, GLPI_DOC_DIR . "/_tmp/", $this->fields['filesize_max']); } else { logInFile('mailgate', GLPI_DOC_DIR . "/_tmp/ is not writable"); } } // Who is the user ? $tkt['_users_id_requester'] = User::getOrImportByEmail($head['from']); $tkt["_users_id_requester_notif"]['use_notification'] = 1; if (!$tkt['_users_id_requester']) { $tkt["_users_id_requester_notif"]['alternative_email'] = $head['from']; } // Add to and cc as additional observer if user found if (count($head['ccs'])) { foreach ($head['ccs'] as $cc) { if ($cc != $head['from'] && strcasecmp($cc, $this->fields['name']) && ($tmp = User::getOrImportByEmail($cc)) > 0) { $tkt['_additional_observers'][] = array('users_id' => $tmp, 'use_notification' => 1); } } } if (count($head['tos'])) { foreach ($head['tos'] as $to) { if ($to != $head['from'] && strcasecmp($to, $this->fields['name']) && ($tmp = User::getOrImportByEmail($to, false)) > 0) { $tkt['_additional_observers'][] = array('users_id' => $tmp, 'use_notification' => 1); } } } // Auto_import $tkt['_auto_import'] = 1; // For followup : do not check users_id = login user $tkt['_do_not_check_users_id'] = 1; $body = $this->getBody($i); // Do it before using charset variable $head['subject'] = $this->decodeMimeString($head['subject']); $tkt['_head'] = $head; if (!empty($this->charset) && !$this->body_converted) { $body = encodeInUtf8($body, $this->charset); $this->body_converted = true; } if (!seems_utf8($body)) { $tkt['content'] = encodeInUtf8($body); } else { $tkt['content'] = $body; } // Add message from getAttached if ($this->addtobody) { $tkt['content'] .= $this->addtobody; } // See In-Reply-To field if (isset($head['in_reply_to'])) { if (preg_match($glpi_message_match, $head['in_reply_to'], $match)) { $tkt['tickets_id'] = intval($match[1]); } } // See in References if (!isset($tkt['tickets_id']) && isset($head['references'])) { if (preg_match($glpi_message_match, $head['references'], $match)) { $tkt['tickets_id'] = intval($match[1]); } } // See in title if (!isset($tkt['tickets_id']) && preg_match('/\\[GLPI #(\\d+)\\]/', $head['subject'], $match)) { $tkt['tickets_id'] = intval($match[1]); } // Found ticket link if (isset($tkt['tickets_id'])) { // it's a reply to a previous ticket $job = new Ticket(); // Check if ticket exists and users_id exists in GLPI /// TODO check if users_id have right to add a followup to the ticket if ($job->getFromDB($tkt['tickets_id']) && $job->fields['status'] != 'closed' && ($tkt['_users_id_requester'] > 0 || Ticket_User::isAlternateEmailForTicket($tkt['tickets_id'], $head['from']))) { $content = explode("\n", $tkt['content']); $tkt['content'] = ""; $first_comment = true; $to_keep = array(); foreach ($content as $ID => $val) { if (isset($val[0]) && $val[0] == '>') { // Delete line at the top of the first comment if ($first_comment) { $first_comment = false; if (isset($to_keep[$ID - 1])) { unset($to_keep[$ID - 1]); } } } else { $to_keep[$ID] = $ID; } } foreach ($to_keep as $ID) { $tkt['content'] .= $content[$ID] . "\n"; } // Do not play rules for followups : WRONG : play rules only for refuse options //$play_rules = false; } else { // => to handle link in Ticket->post_addItem() $tkt['_linkedto'] = $tkt['tickets_id']; unset($tkt['tickets_id']); } } $tkt['name'] = $this->textCleaner($head['subject']); if (!isset($tkt['tickets_id'])) { // Which entity ? //$tkt['entities_id']=$this->fields['entities_id']; //$tkt['Subject']= $head['subject']; // not use for the moment // Medium $tkt['urgency'] = "3"; // No hardware associated $tkt['itemtype'] = ""; // Mail request type } else { // Reopen if needed $tkt['add_reopen'] = 1; } $tkt['requesttypes_id'] = RequestType::getDefault('mail'); $tkt['content'] = clean_cross_side_scripting_deep(html_clean($tkt['content'])); if ($play_rules) { $rule_options['ticket'] = $tkt; $rule_options['headers'] = $head; $rule_options['mailcollector'] = $options['mailgates_id']; $rule_options['_users_id_requester'] = $tkt['_users_id_requester']; $rulecollection = new RuleMailCollectorCollection(); $output = $rulecollection->processAllRules(array(), array(), $rule_options); // New ticket : compute all if (!isset($tkt['tickets_id'])) { foreach ($output as $key => $value) { $tkt[$key] = $value; } } else { // Followup only copy refuse data $tobecopied = array('_refuse_email_no_response', '_refuse_email_with_response'); foreach ($tobecopied as $val) { if (isset($output[$val])) { $tkt[$val] = $output[$val]; } } } } $tkt = addslashes_deep($tkt); return $tkt; }
function get_update_content($DB, $table, $from, $limit, $conv_utf8) { $content = ""; $DB->query("SET NAMES latin1"); $result = $DB->query("SELECT *\n FROM `{$table}`\n LIMIT {$from}, {$limit}"); if ($result) { while ($row = $DB->fetch_assoc($result)) { if (isset($row["id"])) { $insert = "UPDATE `{$table}`\n SET "; foreach ($row as $key => $val) { $insert .= " `" . $key . "` = "; if (!isset($val)) { $insert .= "NULL,"; } else { if ($val != "") { if ($conv_utf8) { // Gestion users AD qui sont deja en UTF8 if ($table != "glpi_users" || !seems_utf8($val)) { $val = encodeInUtf8($val); } } $insert .= "'" . addslashes($val) . "',"; } else { $insert .= "'',"; } } } $insert = preg_replace("/,\$/", "", $insert); $insert .= " WHERE `id` = '" . $row["id"] . "' "; $insert .= ";\n"; $content .= $insert; } } } return $content; }
/** * Do XML export **/ function DoXML() { global $DB; $fp = fopen($this->FilePath, 'wb'); fputs($fp, "<?xml version=\"1.0\"?>\n"); fputs($fp, "<dataxml>\n"); foreach ($this->SqlString as $strqry) { if ($strqry == "") { $this->IsError = 1; $this->ErrorString = "Error the query can't be a null string"; return -1; } $result = $DB->query($strqry); if ($result == FALSE) { $this->IsError = 1; $this->ErrorString = "Error in SQL Query : " . $strqry; return -1; } // OK... let's create XML ;) fputs($fp, " <fields>\n"); $i = 0; $FieldsVector = array(); while ($i < $DB->num_fields($result)) { $name = $DB->field_name($result, $i); fputs($fp, " <field>" . $name . "</field>\n"); $FieldsVector[] = $name; $i++; } fputs($fp, " </fields>\n"); // And NOW the Data ... fputs($fp, " <rows>\n"); while ($row = $DB->fetch_array($result)) { fputs($fp, " <row>\n"); for ($j = 0; $j < $i; $j++) { $FieldName = ""; // Name of TAG $Attributes = ""; switch ($this->Type) { case 1: $FieldName = "data"; break; case 2: $FieldName = "data" . $j; break; case 3: $FieldName = $FieldsVector[$j]; break; case 4: $FieldName = "data"; $Attributes = " fieldname=\"" . $FieldsVector[$j] . "\""; } fputs($fp, " <" . $FieldName . $Attributes . ">" . encodeInUtf8(htmlspecialchars($row[$j])) . "</" . $FieldName . ">\n"); } fputs($fp, " </row>\n"); } fputs($fp, " </rows>\n"); $DB->free_result($result); } fputs($fp, "</dataxml>"); //OK free ... ;) fclose($fp); }
/** * 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)); } } } }