diff --git a/hook.php b/hook.php index 3783ef4..6c0c6c0 100644 --- a/hook.php +++ b/hook.php @@ -1,28 +1,5 @@ tableExists($table); - } else { - return TableExists($table); - } - } -} - - -if (!function_exists('arFieldExists')) { - function arFieldExists($table, $field, $usecache = true) { - global $DB; - if (method_exists( $DB, 'fieldExists')) { - return $DB->fieldExists($table, $field, $usecache); - } else { - return FieldExists($table, $field, $usecache); - } - } -} - /** * Summary of plugin_mailanalyzer_install * @return boolean @@ -30,8 +7,8 @@ function arFieldExists($table, $field, $usecache = true) { function plugin_mailanalyzer_install() { global $DB; - if (!arTableExists("glpi_plugin_mailanalyzer_message_id")) { - $query = "CREATE TABLE `glpi_plugin_mailanalyzer_message_id` ( + if (!$DB->tableExists("glpi_plugin_mailanalyzer_message_id")) { + $query = "CREATE TABLE `glpi_plugin_mailanalyzer_message_id` ( `id` INT(10) NOT NULL AUTO_INCREMENT, `message_id` VARCHAR(255) NOT NULL DEFAULT '0', `ticket_id` INT(10) NOT NULL DEFAULT '0', @@ -43,7 +20,7 @@ function plugin_mailanalyzer_install() { ENGINE=MyISAM; "; - $DB->query($query) or die("error creating glpi_plugin_mailanalyzer_message_id " . $DB->error()); + $DB->query($query) or die("error creating glpi_plugin_mailanalyzer_message_id " . $DB->error()); } return true; @@ -79,35 +56,35 @@ class PluginMailAnalyzer { */ static function getAdditionnalHeaders($marubox, $mid) { - $head = []; - $header = explode("\n", imap_fetchheader($marubox, $mid)); + $head = []; + $header = explode("\n", imap_fetchheader($marubox, $mid)); if (is_array($header) && count($header)) { foreach ($header as $line) { if (preg_match("/^([^: ]*):\\s*/i", $line) - || preg_match("/^\\s(.*)/i", $line ) ) { - // separate name and value + || preg_match("/^\\s(.*)/i", $line ) ) { + // separate name and value if (preg_match("/^([^: ]*): (.*)/i", $line, $arg)) { $key = Toolbox::strtolower($arg[1]); if (!isset($head[$key])) { - $head[$key] = ''; + $head[$key] = ''; } else if ($head[$key] != '') { - $head[$key] .= "\n"; + $head[$key] .= "\n"; } - $head[$key] .= trim($arg[2]); + $head[$key] .= trim($arg[2]); } else if (preg_match("/^\\s(.*)/i", $line, $arg) && !empty($key)) { if (!isset($head[$key])) { - $head[$key] = ''; + $head[$key] = ''; } else if ($head[$key] != '') { - $head[$key] .= "\n"; + $head[$key] .= "\n"; } - $head[$key] .= trim($arg[1]); + $head[$key] .= trim($arg[1]); } else if (preg_match("/^([^:]*):/i", $line, $arg)) { $key = Toolbox::strtolower($arg[1]); @@ -116,9 +93,10 @@ static function getAdditionnalHeaders($marubox, $mid) { } } } - return $head; + return $head; } + /** * Search for current email in order to get its msg num, that will be stored in $mailgate->{$mailgate->pluginmailanalyzer_mid_field}. * The only way to find the right email in the current mailbox is to look for "message-id" property @@ -131,7 +109,7 @@ static function getAdditionnalHeaders($marubox, $mid) { static function getHeaderAndMsgNum($mailgate, $message_id) { for ($locMsgNum = 1; $locMsgNum <= $mailgate->getTotalMails(); $locMsgNum++) { - $fetchheader = PluginMailAnalyzer::getAdditionnalHeaders($mailgate->marubox, $locMsgNum); + $fetchheader = PluginMailAnalyzer::getAdditionnalHeaders($mailgate->marubox, $locMsgNum); if (isset($fetchheader['message-id']) && $fetchheader['message-id'] == $message_id) { $mailgate->{$mailgate->pluginmailanalyzer_uid_field} = $locMsgNum; // by default if ($mailgate->pluginmailanalyzer_is_uid) { @@ -141,9 +119,10 @@ static function getHeaderAndMsgNum($mailgate, $message_id) { } } - return []; // returns an empty array if not found, in this case, $mailgate->{$mailgate->pluginmailanalyzer_mid_field} is not changed + return []; // returns an empty array if not found, in this case, $mailgate->{$mailgate->pluginmailanalyzer_mid_field} is not changed } + /** * Create default mailgate * @param int $mailgate_id is the id of the mail collector in GLPI DB @@ -152,13 +131,13 @@ static function getHeaderAndMsgNum($mailgate, $message_id) { */ static function openMailgate($mailgate_id) { - $mailgate = new MailCollector(); - $mailgate->getFromDB($mailgate_id); + $mailgate = new MailCollector(); + $mailgate->getFromDB($mailgate_id); self::setUIDField($mailgate); - $mailgate->{$mailgate->pluginmailanalyzer_uid_field} = -1; - $mailgate->connect(); + $mailgate->{$mailgate->pluginmailanalyzer_uid_field} = -1; + $mailgate->connect(); - return $mailgate; + return $mailgate; } @@ -183,6 +162,7 @@ public static function plugin_pre_item_add_mailanalyzer_followup($parm) { } } + /** * Summary of getTextFromHtml * gets bare text content from HTML @@ -199,6 +179,7 @@ public static function getTextFromHtml($str) { return $ret; } + /** * Summary of getUserOnBehalfOf * search for ##From if it exists, then try to find users_id from DB @@ -220,20 +201,20 @@ public static function getUserOnBehalfOf($str) { // else we try with name and firstname in this order $matches = $matches[0]; if (isset($matches['email'])) { - $where2 = ['glpi_useremails.email' => $matches['email']]; + $where = ['glpi_useremails.email' => $matches['email']]; } else { - $where2 = ['AND' => ['glpi_users.realname' => $DB->escape(trim( $matches['last'] )), - 'glpi_users.firstname' => $DB->escape(trim( $matches['first'] )), - 'glpi_useremails.is_default' => 1 - ]]; + $where = ['AND' => ['glpi_users.realname' => $DB->escape(trim( $matches['last'] )), + 'glpi_users.firstname' => $DB->escape(trim( $matches['first'] )), + 'glpi_useremails.is_default' => 1 + ]]; } - $where2['AND']['glpi_users.is_active'] = 1; - $where2['AND']['glpi_users.is_deleted'] = 0; + $where['AND']['glpi_users.is_active'] = 1; + $where['AND']['glpi_users.is_deleted'] = 0; $res = $DB->request([ 'SELECT' => 'glpi_users.id', 'FROM' => 'glpi_users', 'RIGHT JOIN'=> ['glpi_useremails' => ['FKEY' => ['glpi_useremails' => 'users_id', 'glpi_users' => 'id']]], - 'WHERE' => $where2, + 'WHERE' => $where, 'LIMIT' => 1 ]); @@ -253,10 +234,10 @@ public static function getUserOnBehalfOf($str) { * @return void */ public static function plugin_pre_item_add_mailanalyzer($parm) { - global $DB, $GLOBALS; + global $DB, $GLOBALS; if (isset($parm->input['_head'])) { - // this ticket have been created via email receiver. + // this ticket have been created via email receiver. // change requester if needed // search for ##From if it exists, then try to find real requester from DB @@ -283,29 +264,29 @@ public static function plugin_pre_item_add_mailanalyzer($parm) { $mailgate = $GLOBALS['mailgate']; self::setUIDField($mailgate); } else { - // mailgate is not open. Called by cron - // then locally create a mailgate - $mailgate = PluginMailAnalyzer::openMailgate($parm->input['_mailgate']); + // mailgate is not open. Called by cron + // then locally create a mailgate + $mailgate = PluginMailAnalyzer::openMailgate($parm->input['_mailgate']); $local_mailgate = true; } - // try to get Thread-Index from email header - $fetchheader = PluginMailAnalyzer::getHeaderAndMsgNum($mailgate, $parm->input['_head']['message_id']); + // try to get Thread-Index from email header + $fetchheader = PluginMailAnalyzer::getHeaderAndMsgNum($mailgate, $parm->input['_head']['message_id']); - // we must check if this email has not been received yet! - // test if 'message-id' is in the DB - $res = $DB->request('glpi_plugin_mailanalyzer_message_id', + // we must check if this email has not been received yet! + // test if 'message-id' is in the DB + $res = $DB->request('glpi_plugin_mailanalyzer_message_id', + [ + 'AND' => [ - 'AND' => - [ - 'ticket_id' => ['!=', 0], - 'message_id' => $parm->input['_head']['message_id'] - ] - ]); + 'ticket_id' => ['!=', 0], + 'message_id' => $parm->input['_head']['message_id'] + ] + ]); if ($row = $res->next()) { // email already received // must prevent ticket creation - $parm->input = [ ]; + $parm->input = false; //[ ]; // as Ticket creation is cancelled, then email is not deleted from mailbox // then we need to set deletion flag to true to this email from mailbox folder @@ -319,18 +300,18 @@ public static function plugin_pre_item_add_mailanalyzer($parm) { return; } - // search for 'Thread-Index' - $references = []; + // search for 'Thread-Index' + $references = []; if (isset($fetchheader['thread-index'])) { // exemple of thread-index : ac5rwreerb4gv3pcr8gdflszrsqhoa== // explanations to decode this property: http://msdn.microsoft.com/en-us/library/ee202481%28v=exchg.80%29.aspx $references[] = bin2hex(substr(imap_base64($fetchheader['thread-index']), 6, 16 )); } - // this ticket has been created via an email receiver. - // we have to check if references can be found in DB. + // this ticket has been created via an email receiver. + // we have to check if references can be found in DB. if (isset($parm->input['_head']['references'])) { - // we may have a forwarded email that looks like reply-to + // we may have a forwarded email that looks like reply-to if (preg_match_all('/<.*?>/', $parm->input['_head']['references'], $matches)) { $references = array_merge($references, $matches[0]); } @@ -381,7 +362,7 @@ public static function plugin_pre_item_add_mailanalyzer($parm) { ]); // prevent Ticket creation. Unfortunately it will return an error to receiver when started manually from web page - $parm->input = []; // empty array... + $parm->input = false; // []; // empty array... // as Ticket creation is cancelled, then email is not deleted from mailbox // then we need to set deletion flag to true to this email from mailbox folder @@ -393,6 +374,7 @@ public static function plugin_pre_item_add_mailanalyzer($parm) { } return; + } else { // ticket creation, but linked to the closed one... $parm->input['_link'] = ['link' => '1', 'tickets_id_1' => '0', 'tickets_id_2' => $row['ticket_id']]; @@ -400,51 +382,50 @@ public static function plugin_pre_item_add_mailanalyzer($parm) { } } - // can't find ref into DB, then this is a new ticket, in this case insert refs and message_id into DB - $references[] = $parm->input['_head']['message_id']; + // can't find ref into DB, then this is a new ticket, in this case insert refs and message_id into DB + $references[] = $parm->input['_head']['message_id']; - // this is a new ticket - // then add references and message_id to DB + // this is a new ticket + // then add references and message_id to DB foreach ($references as $ref) { $res = $DB->request('glpi_plugin_mailanalyzer_message_id', ['message_id' => $ref]); if (count($res) <= 0) { $DB->insert('glpi_plugin_mailanalyzer_message_id', ['message_id' => $ref]); } - //$query = "INSERT IGNORE INTO glpi_plugin_mailanalyzer_message_id (message_id, ticket_id) VALUES ('".$ref."', 0);"; - //$DB->query($query); + } } } - /** - * Summary of plugin_item_add_mailanalyzer - * @param mixed $parm - */ + /** + * Summary of plugin_item_add_mailanalyzer + * @param mixed $parm + */ public static function plugin_item_add_mailanalyzer($parm) { - global $DB; + global $DB; $messages_id = []; if (isset($parm->input['_head'])) { - // this ticket have been created via email receiver. - // update the ticket ID for the message_id only for newly created tickets (ticket_id == 0) + // this ticket have been created via email receiver. + // update the ticket ID for the message_id only for newly created tickets (ticket_id == 0) $messages_id[] = $parm->input['_head']['message_id']; - $fetchheader = []; + $fetchheader = []; $local_mailgate = false; if (isset($GLOBALS['mailgate'])) { // mailgate has been open by web page call, then use it $mailgate = $GLOBALS['mailgate']; self::setUIDField($mailgate); } else { - $mailgate = PluginMailAnalyzer::openMailgate($parm->input['_mailgate']); + $mailgate = PluginMailAnalyzer::openMailgate($parm->input['_mailgate']); $local_mailgate = true; } - // try to get Thread-Index from email header - $fetchheader = PluginMailAnalyzer::getHeaderAndMsgNum($mailgate, $parm->input['_head']['message_id']); + // try to get Thread-Index from email header + $fetchheader = PluginMailAnalyzer::getHeaderAndMsgNum($mailgate, $parm->input['_head']['message_id']); - // search for 'Thread-Index: ' + // search for 'Thread-Index: ' if (isset($fetchheader['thread-index'])) { // exemple of thread-index : Ac5rWReeRb4gv3pCR8GDflsZrsqhoA== // explanations to decode this property: http://msdn.microsoft.com/en-us/library/ee202481%28v=exchg.80%29.aspx @@ -452,9 +433,9 @@ public static function plugin_item_add_mailanalyzer($parm) { $messages_id[] = $thread_index; } - // search for references + // search for references if (isset($parm->input['_head']['references'])) { - // we may have a forwarded email that looks like reply-to + // we may have a forwarded email that looks like reply-to $references = []; if (preg_match_all('/<.*?>/', $parm->input['_head']['references'], $matches)) { $references = $matches[0]; @@ -472,15 +453,15 @@ public static function plugin_item_add_mailanalyzer($parm) { 'WHERE' => ['AND' => [ - 'ticket_id' => 0, + 'ticket_id' => 0, 'message_id' => $messages_id ] ] ]); - // close mailgate only if localy open + // close mailgate only if localy open if ($local_mailgate) { - $mailgate->close_mailbox(); + $mailgate->close_mailbox(); } } diff --git a/mailanalyzer.xml b/mailanalyzer.xml index 056f49b..5c799bc 100644 --- a/mailanalyzer.xml +++ b/mailanalyzer.xml @@ -6,11 +6,14 @@ https://raw.githubusercontent.com/tomolimo/mailanalyzer/master/plugin.png - + + + https://github.com/tomolimo/mailanalyzer @@ -22,11 +25,11 @@ - 1.5.1 + 1.5.2 9.3 - 1.4.2 + 1.4.3 9.2 diff --git a/setup.php b/setup.php index aff06a9..95b2b4b 100644 --- a/setup.php +++ b/setup.php @@ -1,49 +1,49 @@ 'PluginMailAnalyzer']); + Plugin::registerClass('PluginMailAnalyzer', ['classname' => 'PluginMailAnalyzer']); - $PLUGIN_HOOKS['csrf_compliant']['mailanalyzer'] = true; + $PLUGIN_HOOKS['csrf_compliant']['mailanalyzer'] = true; - $PLUGIN_HOOKS['pre_item_add']['mailanalyzer'] = [ - 'Ticket' => ['PluginMailAnalyzer', 'plugin_pre_item_add_mailanalyzer'], - 'TicketFollowup' => ['PluginMailAnalyzer', 'plugin_pre_item_add_mailanalyzer_followup'] - ]; + $PLUGIN_HOOKS['pre_item_add']['mailanalyzer'] = [ + 'Ticket' => ['PluginMailAnalyzer', 'plugin_pre_item_add_mailanalyzer'], + 'TicketFollowup' => ['PluginMailAnalyzer', 'plugin_pre_item_add_mailanalyzer_followup'] + ]; - $PLUGIN_HOOKS['item_add']['mailanalyzer'] = [ - 'Ticket' => ['PluginMailAnalyzer', 'plugin_item_add_mailanalyzer'] - ]; + $PLUGIN_HOOKS['item_add']['mailanalyzer'] = [ + 'Ticket' => ['PluginMailAnalyzer', 'plugin_item_add_mailanalyzer'] + ]; } // Get the name and the version of the plugin - Needed function plugin_version_mailanalyzer() { - global $LANG; - - return ['name' => 'Mail Analyzer', - 'version' => PLUGIN_MAILANALYSER_VERSION, - 'author' => 'Olivier Moron', - 'license' => 'GPLv2+', - 'homepage' => 'https://github.com/tomolimo/mailanalyzer', - 'minGlpiVersion' => '9.3']; + global $LANG; + + return ['name' => 'Mail Analyzer', + 'version' => PLUGIN_MAILANALYSER_VERSION, + 'author' => 'Olivier Moron', + 'license' => 'GPLv2+', + 'homepage' => 'https://github.com/tomolimo/mailanalyzer', + 'minGlpiVersion' => '9.3']; } // Optional : check prerequisites before install : may print errors or add to message after redirect function plugin_mailanalyzer_check_prerequisites() { - if (version_compare(GLPI_VERSION, '9.3', 'lt')) { - echo "This plugin requires GLPI >= 9.3"; - return false; - } else { - return true; - } + if (version_compare(GLPI_VERSION, '9.3', 'lt')) { + echo "This plugin requires GLPI >= 9.3"; + return false; + } else { + return true; + } } function plugin_mailanalyzer_check_config() { - return true; + return true; }