Skip to content

Commit

Permalink
Fixed returned value in $parm->input that must be false and not []
Browse files Browse the repository at this point in the history
set version to 1.5.2
  • Loading branch information
tomolimo committed Feb 5, 2021
1 parent 285504b commit 9ee9539
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 127 deletions.
177 changes: 79 additions & 98 deletions hook.php
Original file line number Diff line number Diff line change
@@ -1,37 +1,14 @@
<?php

if (!function_exists('arTableExists')) {
function arTableExists($table) {
global $DB;
if (method_exists( $DB, 'tableExists')) {
return $DB->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
*/
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',
Expand All @@ -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;
Expand Down Expand Up @@ -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]);
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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;
}


Expand All @@ -183,6 +162,7 @@ public static function plugin_pre_item_add_mailanalyzer_followup($parm) {
}
}


/**
* Summary of getTextFromHtml
* gets bare text content from HTML
Expand All @@ -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
Expand All @@ -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
]);

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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]);
}
Expand Down Expand Up @@ -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
Expand All @@ -393,68 +374,68 @@ 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']];
}
}
}

// 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
$thread_index = bin2hex(substr(imap_base64($fetchheader['thread-index']), 6, 16 ));
$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];
Expand All @@ -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();
}

}
Expand Down
Loading

0 comments on commit 9ee9539

Please sign in to comment.