FIX Bad deletion of email when there is several emails processed

This commit is contained in:
Laurent Destailleur 2023-04-13 15:00:34 +02:00
parent 4fc8b40b71
commit 4c51e35adc

View File

@ -127,7 +127,7 @@ class EmailCollector extends CommonObject
'entity' => array('type'=>'integer', 'label'=>'Entity', 'enabled'=>1, 'visible'=>0, 'default'=>1, 'notnull'=>1, 'index'=>1, 'position'=>20), 'entity' => array('type'=>'integer', 'label'=>'Entity', 'enabled'=>1, 'visible'=>0, 'default'=>1, 'notnull'=>1, 'index'=>1, 'position'=>20),
'ref' => array('type'=>'varchar(128)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'showoncombobox'=>1, 'index'=>1, 'position'=>10, 'searchall'=>1, 'help'=>'Example: MyCollector1', 'csslist'=>'tdoverflowmax200'), 'ref' => array('type'=>'varchar(128)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'showoncombobox'=>1, 'index'=>1, 'position'=>10, 'searchall'=>1, 'help'=>'Example: MyCollector1', 'csslist'=>'tdoverflowmax200'),
'label' => array('type'=>'varchar(255)', 'label'=>'Label', 'visible'=>1, 'enabled'=>1, 'position'=>30, 'notnull'=>-1, 'searchall'=>1, 'help'=>'Example: My Email collector', 'csslist'=>'tdoverflowmax150'), 'label' => array('type'=>'varchar(255)', 'label'=>'Label', 'visible'=>1, 'enabled'=>1, 'position'=>30, 'notnull'=>-1, 'searchall'=>1, 'help'=>'Example: My Email collector', 'csslist'=>'tdoverflowmax150'),
'description' => array('type'=>'text', 'label'=>'Description', 'visible'=>-1, 'enabled'=>1, 'position'=>60, 'notnull'=>-1, 'csslist'=>'small'), 'description' => array('type'=>'text', 'label'=>'Description', 'visible'=>-1, 'enabled'=>1, 'position'=>60, 'notnull'=>-1, 'cssview'=>'small', 'csslist'=>'small tdoverflowmax200'),
'host' => array('type'=>'varchar(255)', 'label'=>'EMailHost', 'visible'=>1, 'enabled'=>1, 'position'=>90, 'notnull'=>1, 'searchall'=>1, 'comment'=>"IMAP server", 'help'=>'Example: imap.gmail.com', 'csslist'=>'tdoverflowmax125'), 'host' => array('type'=>'varchar(255)', 'label'=>'EMailHost', 'visible'=>1, 'enabled'=>1, 'position'=>90, 'notnull'=>1, 'searchall'=>1, 'comment'=>"IMAP server", 'help'=>'Example: imap.gmail.com', 'csslist'=>'tdoverflowmax125'),
'port' => array('type'=>'varchar(10)', 'label'=>'EMailHostPort', 'visible'=>1, 'enabled'=>1, 'position'=>91, 'notnull'=>1, 'searchall'=>0, 'comment'=>"IMAP server port", 'help'=>'Example: 993', 'csslist'=>'tdoverflowmax50', 'default'=>'993'), 'port' => array('type'=>'varchar(10)', 'label'=>'EMailHostPort', 'visible'=>1, 'enabled'=>1, 'position'=>91, 'notnull'=>1, 'searchall'=>0, 'comment'=>"IMAP server port", 'help'=>'Example: 993', 'csslist'=>'tdoverflowmax50', 'default'=>'993'),
'hostcharset' => array('type'=>'varchar(16)', 'label'=>'HostCharset', 'visible'=>-1, 'enabled'=>1, 'position'=>92, 'notnull'=>0, 'searchall'=>0, 'comment'=>"IMAP server charset", 'help'=>'Example: "UTF-8" (May be "US-ASCII" with some Office365)', 'default'=>'UTF-8'), 'hostcharset' => array('type'=>'varchar(16)', 'label'=>'HostCharset', 'visible'=>-1, 'enabled'=>1, 'position'=>92, 'notnull'=>0, 'searchall'=>0, 'comment'=>"IMAP server charset", 'help'=>'Example: "UTF-8" (May be "US-ASCII" with some Office365)', 'default'=>'UTF-8'),
@ -136,13 +136,11 @@ class EmailCollector extends CommonObject
'password' => array('type'=>'password', 'label'=>'Password', 'visible'=>-1, 'enabled'=>"1", 'position'=>103, 'notnull'=>-1, 'comment'=>"IMAP password", 'help'=>'WithGMailYouCanCreateADedicatedPassword'), 'password' => array('type'=>'password', 'label'=>'Password', 'visible'=>-1, 'enabled'=>"1", 'position'=>103, 'notnull'=>-1, 'comment'=>"IMAP password", 'help'=>'WithGMailYouCanCreateADedicatedPassword'),
'oauth_service' => array('type'=>'varchar(128)', 'label'=>'oauthService', 'visible'=>-1, 'enabled'=>"getDolGlobalInt('MAIN_IMAP_USE_PHPIMAP')", 'position'=>104, 'notnull'=>0, 'index'=>1, 'comment'=>"IMAP login oauthService", 'arrayofkeyval'=>array(), 'help'=>'TokenMustHaveBeenCreated'), 'oauth_service' => array('type'=>'varchar(128)', 'label'=>'oauthService', 'visible'=>-1, 'enabled'=>"getDolGlobalInt('MAIN_IMAP_USE_PHPIMAP')", 'position'=>104, 'notnull'=>0, 'index'=>1, 'comment'=>"IMAP login oauthService", 'arrayofkeyval'=>array(), 'help'=>'TokenMustHaveBeenCreated'),
'source_directory' => array('type'=>'varchar(255)', 'label'=>'MailboxSourceDirectory', 'visible'=>-1, 'enabled'=>1, 'position'=>104, 'notnull'=>1, 'default' => 'Inbox', 'help'=>'Example: INBOX'), 'source_directory' => array('type'=>'varchar(255)', 'label'=>'MailboxSourceDirectory', 'visible'=>-1, 'enabled'=>1, 'position'=>104, 'notnull'=>1, 'default' => 'Inbox', 'help'=>'Example: INBOX'),
//'filter' => array('type'=>'text', 'label'=>'Filter', 'visible'=>1, 'enabled'=>1, 'position'=>105),
//'actiontodo' => array('type'=>'varchar(255)', 'label'=>'ActionToDo', 'visible'=>1, 'enabled'=>1, 'position'=>106),
'target_directory' => array('type'=>'varchar(255)', 'label'=>'MailboxTargetDirectory', 'visible'=>1, 'enabled'=>1, 'position'=>110, 'notnull'=>0, 'help'=>"EmailCollectorTargetDir"), 'target_directory' => array('type'=>'varchar(255)', 'label'=>'MailboxTargetDirectory', 'visible'=>1, 'enabled'=>1, 'position'=>110, 'notnull'=>0, 'help'=>"EmailCollectorTargetDir"),
'maxemailpercollect' => array('type'=>'integer', 'label'=>'MaxEmailCollectPerCollect', 'visible'=>-1, 'enabled'=>1, 'position'=>111, 'default'=>100), 'maxemailpercollect' => array('type'=>'integer', 'label'=>'MaxEmailCollectPerCollect', 'visible'=>-1, 'enabled'=>1, 'position'=>111, 'default'=>100),
'datelastresult' => array('type'=>'datetime', 'label'=>'DateLastCollectResult', 'visible'=>1, 'enabled'=>'$action != "create" && $action != "edit"', 'position'=>121, 'notnull'=>-1, 'csslist'=>'nowraponall'), 'datelastresult' => array('type'=>'datetime', 'label'=>'DateLastCollectResult', 'visible'=>1, 'enabled'=>'$action != "create" && $action != "edit"', 'position'=>121, 'notnull'=>-1, 'csslist'=>'nowraponall'),
'codelastresult' => array('type'=>'varchar(16)', 'label'=>'CodeLastResult', 'visible'=>1, 'enabled'=>'$action != "create" && $action != "edit"', 'position'=>122, 'notnull'=>-1,), 'codelastresult' => array('type'=>'varchar(16)', 'label'=>'CodeLastResult', 'visible'=>1, 'enabled'=>'$action != "create" && $action != "edit"', 'position'=>122, 'notnull'=>-1,),
'lastresult' => array('type'=>'varchar(255)', 'label'=>'LastResult', 'visible'=>1, 'enabled'=>'$action != "create" && $action != "edit"', 'position'=>123, 'notnull'=>-1, 'csslist'=>'small tdoverflowmax200'), 'lastresult' => array('type'=>'varchar(255)', 'label'=>'LastResult', 'visible'=>1, 'enabled'=>'$action != "create" && $action != "edit"', 'position'=>123, 'notnull'=>-1, 'cssview'=>'small', 'csslist'=>'small tdoverflowmax200'),
'datelastok' => array('type'=>'datetime', 'label'=>'DateLastcollectResultOk', 'visible'=>1, 'enabled'=>'$action != "create"', 'position'=>125, 'notnull'=>-1, 'csslist'=>'nowraponall'), 'datelastok' => array('type'=>'datetime', 'label'=>'DateLastcollectResultOk', 'visible'=>1, 'enabled'=>'$action != "create"', 'position'=>125, 'notnull'=>-1, 'csslist'=>'nowraponall'),
'note_public' => array('type'=>'html', 'label'=>'NotePublic', 'visible'=>0, 'enabled'=>1, 'position'=>61, 'notnull'=>-1,), 'note_public' => array('type'=>'html', 'label'=>'NotePublic', 'visible'=>0, 'enabled'=>1, 'position'=>61, 'notnull'=>-1,),
'note_private' => array('type'=>'html', 'label'=>'NotePrivate', 'visible'=>0, 'enabled'=>1, 'position'=>62, 'notnull'=>-1,), 'note_private' => array('type'=>'html', 'label'=>'NotePrivate', 'visible'=>0, 'enabled'=>1, 'position'=>62, 'notnull'=>-1,),
@ -381,11 +379,10 @@ class EmailCollector extends CommonObject
// Clear fields // Clear fields
$object->ref = "copy_of_".$object->ref; $object->ref = "copy_of_".$object->ref;
$object->title = $langs->trans("CopyOf")." ".$object->title; $object->label = $langs->trans("CopyOf")." ".$object->label;
if (empty($object->host)) { if (empty($object->host)) {
$object->host = 'imap.example.com'; $object->host = 'imap.example.com';
} }
// ...
// Clear extrafields that are unique // Clear extrafields that are unique
if (is_array($object->array_options) && count($object->array_options) > 0) { if (is_array($object->array_options) && count($object->array_options) > 0) {
$extrafields->fetch_name_optionals_label($this->table_element); $extrafields->fetch_name_optionals_label($this->table_element);
@ -878,8 +875,24 @@ class EmailCollector extends CommonObject
*/ */
private function overwritePropertiesOfObject(&$object, $actionparam, $messagetext, $subject, $header, &$operationslog) private function overwritePropertiesOfObject(&$object, $actionparam, $messagetext, $subject, $header, &$operationslog)
{ {
global $conf, $langs;
$errorforthisaction = 0; $errorforthisaction = 0;
// set output lang
$outputlangs = $langs;
$newlang = '';
if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) {
$newlang = GETPOST('lang_id', 'aZ09');
}
if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) {
$newlang = $object->thirdparty->default_lang;
}
if (!empty($newlang)) {
$outputlangs = new Translate('', $conf);
$outputlangs->setDefaultLang($newlang);
}
// Overwrite values with values extracted from source email // Overwrite values with values extracted from source email
// $this->actionparam = 'opportunity_status=123;abc=EXTRACT:BODY:....' // $this->actionparam = 'opportunity_status=123;abc=EXTRACT:BODY:....'
$arrayvaluetouse = dolExplodeIntoArray($actionparam, ';', '='); $arrayvaluetouse = dolExplodeIntoArray($actionparam, ';', '=');
@ -1000,7 +1013,8 @@ class EmailCollector extends CommonObject
if ($regforregex[1] == 'SET' || empty($valuecurrent)) { if ($regforregex[1] == 'SET' || empty($valuecurrent)) {
$valuetouse = $regforregex[2]; $valuetouse = $regforregex[2];
$substitutionarray = array(); $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $object);
complete_substitutions_array($substitutionarray, $outputlangs, $object);
$matcharray = array(); $matcharray = array();
preg_match_all('/__([a-z0-9]+(?:_[a-z0-9]+)?)__/i', $valuetouse, $matcharray); preg_match_all('/__([a-z0-9]+(?:_[a-z0-9]+)?)__/i', $valuetouse, $matcharray);
//var_dump($tmpproperty.' - '.$object->$tmpproperty.' - '.$valuetouse); var_dump($matcharray); //var_dump($tmpproperty.' - '.$object->$tmpproperty.' - '.$valuetouse); var_dump($matcharray);
@ -1081,6 +1095,9 @@ class EmailCollector extends CommonObject
$searchfilternodoltrackid = 0; $searchfilternodoltrackid = 0;
$searchfilterisanswer = 0; $searchfilterisanswer = 0;
$searchfilterisnotanswer = 0; $searchfilterisnotanswer = 0;
$searchfilterreplyto = 0;
$searchfilterexcludebody = '';
$searchfilterexcludesubject = '';
$operationslog = ''; $operationslog = '';
$now = dol_now(); $now = dol_now();
@ -1230,64 +1247,91 @@ class EmailCollector extends CommonObject
} }
if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) { if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
// Use PHPIMAP external library
$criteria = array(array('UNDELETED')); // Seems not supported by some servers $criteria = array(array('UNDELETED')); // Seems not supported by some servers
foreach ($this->filters as $rule) { foreach ($this->filters as $rule) {
if (empty($rule['status'])) { if (empty($rule['status'])) {
continue; continue;
} }
$not = '';
if (strpos($rule['rulevalue'], '!') === 0) {
// The value start with !, so we exclude the criteria
$not = 'NOT ';
}
if ($rule['type'] == 'from') {
$tmprulevaluearray = explode('*', $rule['rulevalue']);
if (count($tmprulevaluearray) >= 2) {
foreach ($tmprulevaluearray as $tmprulevalue) {
array_push($criteria, array($not."FROM" => $tmprulevalue));
}
} else {
array_push($criteria, array($not."FROM" => $rule['rulevalue']));
}
}
if ($rule['type'] == 'to') { if ($rule['type'] == 'to') {
$tmprulevaluearray = explode('*', $rule['rulevalue']); $tmprulevaluearray = explode('*', $rule['rulevalue']);
if (count($tmprulevaluearray) >= 2) { if (count($tmprulevaluearray) >= 2) {
foreach ($tmprulevaluearray as $tmprulevalue) { foreach ($tmprulevaluearray as $tmprulevalue) {
array_push($criteria, array("TO" => $tmprulevalue)); array_push($criteria, array($not."TO" => $tmprulevalue));
} }
} else { } else {
array_push($criteria, array("TO" => $rule['rulevalue'])); array_push($criteria, array($not."TO" => $rule['rulevalue']));
} }
} }
if ($rule['type'] == 'bcc') { if ($rule['type'] == 'bcc') {
array_push($criteria, array("BCC" => $rule['rulevalue'])); array_push($criteria, array($not."BCC" => $rule['rulevalue']));
} }
if ($rule['type'] == 'cc') { if ($rule['type'] == 'cc') {
array_push($criteria, array("CC" => $rule['rulevalue'])); array_push($criteria, array($not."CC" => $rule['rulevalue']));
}
if ($rule['type'] == 'from') {
array_push($criteria, array("FROM" => $rule['rulevalue']));
} }
if ($rule['type'] == 'subject') { if ($rule['type'] == 'subject') {
if (strpos($rule['rulevalue'], '!') === 0) {
//array_push($criteria, array("NOT SUBJECT" => $rule['rulevalue']));
$searchfilterexcludesubject = preg_replace('/^!/', '', $rule['rulevalue']);
} else {
array_push($criteria, array("SUBJECT" => $rule['rulevalue'])); array_push($criteria, array("SUBJECT" => $rule['rulevalue']));
} }
}
if ($rule['type'] == 'body') { if ($rule['type'] == 'body') {
if (strpos($rule['rulevalue'], '!') === 0) {
//array_push($criteria, array("NOT BODY" => $rule['rulevalue']));
$searchfilterexcludebody = preg_replace('/^!/', '', $rule['rulevalue']);
} else {
array_push($criteria, array("BODY" => $rule['rulevalue'])); array_push($criteria, array("BODY" => $rule['rulevalue']));
} }
}
if ($rule['type'] == 'header') { if ($rule['type'] == 'header') {
array_push($criteria, array("HEADER" => $rule['rulevalue'])); array_push($criteria, array($not."HEADER" => $rule['rulevalue']));
} }
/* seems not used */
/*
if ($rule['type'] == 'notinsubject') { if ($rule['type'] == 'notinsubject') {
array_push($criteria, array("SUBJECT NOT" => $rule['rulevalue'])); array_push($criteria, array($not."SUBJECT NOT" => $rule['rulevalue']));
} }
if ($rule['type'] == 'notinbody') { if ($rule['type'] == 'notinbody') {
array_push($criteria, array("BODY NOT" => $rule['rulevalue'])); array_push($criteria, array($not."BODY NOT" => $rule['rulevalue']));
} }*/
if ($rule['type'] == 'seen') { if ($rule['type'] == 'seen') {
array_push($criteria, array("SEEN")); array_push($criteria, array($not."SEEN"));
} }
if ($rule['type'] == 'unseen') { if ($rule['type'] == 'unseen') {
array_push($criteria, array("UNSEEN")); array_push($criteria, array($not."UNSEEN"));
} }
if ($rule['type'] == 'unanswered') { if ($rule['type'] == 'unanswered') {
array_push($criteria, array("UNANSWERED")); array_push($criteria, array($not."UNANSWERED"));
} }
if ($rule['type'] == 'answered') { if ($rule['type'] == 'answered') {
array_push($criteria, array("ANSWERED")); array_push($criteria, array($not."ANSWERED"));
} }
if ($rule['type'] == 'smaller') { if ($rule['type'] == 'smaller') {
array_push($criteria, array("SMALLER")); array_push($criteria, array($not."SMALLER"));
} }
if ($rule['type'] == 'larger') { if ($rule['type'] == 'larger') {
array_push($criteria, array("LARGER")); array_push($criteria, array($not."LARGER"));
} }
// Rules to filter after the search imap // Rules to filter after the search imap
@ -1310,6 +1354,10 @@ class EmailCollector extends CommonObject
if ($rule['type'] == 'isnotanswer') { if ($rule['type'] == 'isnotanswer') {
$searchfilterisnotanswer++; $searchhead .= '! /References.*@.*/'; $searchfilterisnotanswer++; $searchhead .= '! /References.*@.*/';
} }
if ($rule['type'] == 'replyto') {
$searchfilterreplyto++; $searchhead .= '/Reply-To.*'.preg_quote($rule['rulevalue'], '/').'/';
}
} }
if (empty($targetdir)) { // Use last date as filter if there is no targetdir defined. if (empty($targetdir)) { // Use last date as filter if there is no targetdir defined.
@ -1327,65 +1375,94 @@ class EmailCollector extends CommonObject
dol_syslog("IMAP search string = ".var_export($criteria, true)); dol_syslog("IMAP search string = ".var_export($criteria, true));
$search = var_export($criteria, true); $search = var_export($criteria, true);
} else { } else {
// Use native IMAP functions
$search = 'UNDELETED'; // Seems not supported by some servers $search = 'UNDELETED'; // Seems not supported by some servers
foreach ($this->filters as $rule) { foreach ($this->filters as $rule) {
if (empty($rule['status'])) { if (empty($rule['status'])) {
continue; continue;
} }
if ($rule['type'] == 'to') { // Forge the IMAP search string.
$tmprulevaluearray = explode('*', $rule['rulevalue']); // See https://www.rfc-editor.org/rfc/rfc3501
$not = '';
if (strpos($rule['rulevalue'], '!') === 0) {
// The value start with !, so we exclude the criteria
$not = 'NOT ';
}
if ($rule['type'] == 'from') {
$tmprulevaluearray = explode('*', $rule['rulevalue']); // Search on abc*def means searching on 'abc' and on 'def'
if (count($tmprulevaluearray) >= 2) { if (count($tmprulevaluearray) >= 2) {
foreach ($tmprulevaluearray as $tmprulevalue) { foreach ($tmprulevaluearray as $tmprulevalue) {
$search .= ($search ? ' ' : '').'TO "'.str_replace('"', '', $tmprulevalue).'"'; $search .= ($search ? ' ' : '').$not.'FROM "'.str_replace('"', '', $tmprulevalue).'"';
} }
} else { } else {
$search .= ($search ? ' ' : '').'TO "'.str_replace('"', '', $rule['rulevalue']).'"'; $search .= ($search ? ' ' : '').$not.'FROM "'.str_replace('"', '', $rule['rulevalue']).'"';
}
}
if ($rule['type'] == 'to') {
$tmprulevaluearray = explode('*', $rule['rulevalue']); // Search on abc*def means searching on 'abc' and on 'def'
if (count($tmprulevaluearray) >= 2) {
foreach ($tmprulevaluearray as $tmprulevalue) {
$search .= ($search ? ' ' : '').$not.'TO "'.str_replace('"', '', $tmprulevalue).'"';
}
} else {
$search .= ($search ? ' ' : '').$not.'TO "'.str_replace('"', '', $rule['rulevalue']).'"';
} }
} }
if ($rule['type'] == 'bcc') { if ($rule['type'] == 'bcc') {
$search .= ($search ? ' ' : '').'BCC'; $search .= ($search ? ' ' : '').$not.'BCC';
} }
if ($rule['type'] == 'cc') { if ($rule['type'] == 'cc') {
$search .= ($search ? ' ' : '').'CC'; $search .= ($search ? ' ' : '').$not.'CC';
}
if ($rule['type'] == 'from') {
$search .= ($search ? ' ' : '').'FROM "'.str_replace('"', '', $rule['rulevalue']).'"';
} }
if ($rule['type'] == 'subject') { if ($rule['type'] == 'subject') {
if (strpos($rule['rulevalue'], '!') === 0) {
//$search .= ($search ? ' ' : '').'NOT BODY "'.str_replace('"', '', $rule['rulevalue']).'"';
$searchfilterexcludesubject = preg_replace('/^!/', '', $rule['rulevalue']);
} else {
$search .= ($search ? ' ' : '').'SUBJECT "'.str_replace('"', '', $rule['rulevalue']).'"'; $search .= ($search ? ' ' : '').'SUBJECT "'.str_replace('"', '', $rule['rulevalue']).'"';
} }
}
if ($rule['type'] == 'body') { if ($rule['type'] == 'body') {
if (strpos($rule['rulevalue'], '!') === 0) {
//$search .= ($search ? ' ' : '').'NOT BODY "'.str_replace('"', '', $rule['rulevalue']).'"';
$searchfilterexcludebody = preg_replace('/^!/', '', $rule['rulevalue']);
} else {
$search .= ($search ? ' ' : '').'BODY "'.str_replace('"', '', $rule['rulevalue']).'"'; $search .= ($search ? ' ' : '').'BODY "'.str_replace('"', '', $rule['rulevalue']).'"';
} }
}
if ($rule['type'] == 'header') { if ($rule['type'] == 'header') {
$search .= ($search ? ' ' : '').'HEADER '.$rule['rulevalue']; $search .= ($search ? ' ' : '').$not.'HEADER '.$rule['rulevalue'];
} }
/* seems not used */
/*
if ($rule['type'] == 'notinsubject') { if ($rule['type'] == 'notinsubject') {
$search .= ($search ? ' ' : '').'SUBJECT NOT "'.str_replace('"', '', $rule['rulevalue']).'"'; $search .= ($search ? ' ' : '').'NOT SUBJECT "'.str_replace('"', '', $rule['rulevalue']).'"';
} }
if ($rule['type'] == 'notinbody') { if ($rule['type'] == 'notinbody') {
$search .= ($search ? ' ' : '').'BODY NOT "'.str_replace('"', '', $rule['rulevalue']).'"'; $search .= ($search ? ' ' : '').'NOT BODY "'.str_replace('"', '', $rule['rulevalue']).'"';
} }*/
if ($rule['type'] == 'seen') { if ($rule['type'] == 'seen') {
$search .= ($search ? ' ' : '').'SEEN'; $search .= ($search ? ' ' : '').$not.'SEEN';
} }
if ($rule['type'] == 'unseen') { if ($rule['type'] == 'unseen') {
$search .= ($search ? ' ' : '').'UNSEEN'; $search .= ($search ? ' ' : '').$not.'UNSEEN';
} }
if ($rule['type'] == 'unanswered') { if ($rule['type'] == 'unanswered') {
$search .= ($search ? ' ' : '').'UNANSWERED'; $search .= ($search ? ' ' : '').$not.'UNANSWERED';
} }
if ($rule['type'] == 'answered') { if ($rule['type'] == 'answered') {
$search .= ($search ? ' ' : '').'ANSWERED'; $search .= ($search ? ' ' : '').$not.'ANSWERED';
} }
if ($rule['type'] == 'smaller') { if ($rule['type'] == 'smaller') {
$search .= ($search ? ' ' : '').'SMALLER "'.str_replace('"', '', $rule['rulevalue']).'"'; $search .= ($search ? ' ' : '').$not.'SMALLER "'.str_replace('"', '', $rule['rulevalue']).'"';
} }
if ($rule['type'] == 'larger') { if ($rule['type'] == 'larger') {
$search .= ($search ? ' ' : '').'LARGER "'.str_replace('"', '', $rule['rulevalue']).'"'; $search .= ($search ? ' ' : '').$not.'LARGER "'.str_replace('"', '', $rule['rulevalue']).'"';
} }
// Rules to filter after the search imap // Rules to filter after the search imap
@ -1408,6 +1485,10 @@ class EmailCollector extends CommonObject
if ($rule['type'] == 'isnotanswer') { if ($rule['type'] == 'isnotanswer') {
$searchfilterisnotanswer++; $searchhead .= '! /References.*@.*/'; $searchfilterisnotanswer++; $searchhead .= '! /References.*@.*/';
} }
if ($rule['type'] == 'replyto') {
$searchfilterreplyto++; $searchhead .= '/Reply-To.*'.preg_quote($rule['rulevalue'], '/').'/';
}
} }
if (empty($targetdir)) { // Use last date as filter if there is no targetdir defined. if (empty($targetdir)) { // Use last date as filter if there is no targetdir defined.
@ -1463,7 +1544,8 @@ class EmailCollector extends CommonObject
} }
} else { } else {
// Scan IMAP inbox // Scan IMAP inbox
$arrayofemail = imap_search($connection, $search, null, $charset); $arrayofemail = imap_search($connection, $search, SE_UID, $charset);
if ($arrayofemail === false) { if ($arrayofemail === false) {
// Nothing found or search string not understood // Nothing found or search string not understood
$mapoferrrors = imap_errors(); $mapoferrrors = imap_errors();
@ -1475,6 +1557,8 @@ class EmailCollector extends CommonObject
} }
} }
$arrayofemailtodelete = array(); // Track email to delete to make the deletion at end.
// Loop on each email found // Loop on each email found
if (!$error && !empty($arrayofemail) && count($arrayofemail) > 0) { if (!$error && !empty($arrayofemail) && count($arrayofemail) > 0) {
// Loop to get part html and plain // Loop to get part html and plain
@ -1508,9 +1592,8 @@ class EmailCollector extends CommonObject
$header = $imapemail->getHeader()->raw; $header = $imapemail->getHeader()->raw;
$overview = $imapemail->getAttributes(); $overview = $imapemail->getAttributes();
} else { } else {
//$header = imap_headerinfo($connection, $imapemail); $header = imap_fetchheader($connection, $imapemail, FT_UID);
$header = imap_fetchheader($connection, $imapemail, 0); $overview = imap_fetch_overview($connection, $imapemail, FT_UID);
$overview = imap_fetch_overview($connection, $imapemail, 0);
} }
$header = preg_replace('/\r\n\s+/m', ' ', $header); // When a header line is on several lines, merge lines $header = preg_replace('/\r\n\s+/m', ' ', $header); // When a header line is on several lines, merge lines
@ -1537,7 +1620,7 @@ class EmailCollector extends CommonObject
$emailto = $this->decodeSMTPSubject($overview[0]->to); $emailto = $this->decodeSMTPSubject($overview[0]->to);
$operationslog .= '<br>** Process email '.dol_escape_htmltag($iforemailloop)." - References: ".dol_escape_htmltag($headers['References'])." - Subject: ".dol_escape_htmltag($headers['Subject']); $operationslog .= '<br>** Process email #'.dol_escape_htmltag($iforemailloop)." - ".dol_escape_htmltag((string) $imapemail)." - References: ".dol_escape_htmltag($headers['References'])." - Subject: ".dol_escape_htmltag($headers['Subject']);
dol_syslog("** Process email ".$iforemailloop." References: ".$headers['References']." Subject: ".$headers['Subject']); dol_syslog("** Process email ".$iforemailloop." References: ".$headers['References']." Subject: ".$headers['Subject']);
@ -1545,7 +1628,7 @@ class EmailCollector extends CommonObject
$trackidfoundintorecipientid = 0; $trackidfoundintorecipientid = 0;
$reg = array(); $reg = array();
// See also later list of all supported tags... // See also later list of all supported tags...
if (preg_match('/\+(thi|ctc|use|mem|sub|proj|tas|con|tic|job|pro|ord|inv|spro|sor|sin|leav|stockinv|job|surv|salary)([0-9]+)@/', $emailto, $reg)) { if (preg_match('/\+(thi|ctc|use|mem|sub|proj|tas|con|tic|pro|ord|inv|spro|sor|sin|leav|stockinv|job|surv|salary)([0-9]+)@/', $emailto, $reg)) {
$trackidfoundintorecipienttype = $reg[1]; $trackidfoundintorecipienttype = $reg[1];
$trackidfoundintorecipientid = $reg[2]; $trackidfoundintorecipientid = $reg[2];
} elseif (preg_match('/\+emailing-(\w+)@/', $emailto, $reg)) { // Can be 'emailing-test' or 'emailing-IdMailing-IdRecipient' } elseif (preg_match('/\+emailing-(\w+)@/', $emailto, $reg)) { // Can be 'emailing-test' or 'emailing-IdMailing-IdRecipient'
@ -1625,8 +1708,6 @@ class EmailCollector extends CommonObject
$ticketfoundby = ''; $ticketfoundby = '';
$candidaturefoundby = ''; $candidaturefoundby = '';
$this->db->begin();
if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) { if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
dol_syslog("msgid=".$overview['message_id']." date=".dol_print_date($overview['date'], 'dayrfc', 'gmt')." from=".$overview['from']." to=".$overview['to']." subject=".$overview['subject']); dol_syslog("msgid=".$overview['message_id']." date=".dol_print_date($overview['date'], 'dayrfc', 'gmt')." from=".$overview['from']." to=".$overview['to']." subject=".$overview['subject']);
@ -1660,7 +1741,7 @@ class EmailCollector extends CommonObject
$attachments = []; $attachments = [];
} }
} else { } else {
$this->getmsg($connection, $imapemail); $this->getmsg($connection, $imapemail); // This set global var $charset, $htmlmsg, $plainmsg, $attachments
} }
//print $plainmsg; //print $plainmsg;
//var_dump($plainmsg); exit; //var_dump($plainmsg); exit;
@ -1670,6 +1751,15 @@ class EmailCollector extends CommonObject
// Removed emojis // Removed emojis
$messagetext = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $messagetext); $messagetext = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $messagetext);
if ($searchfilterexcludebody) {
if (preg_match('/'.preg_quote($searchfilterexcludebody, '/').'/ms', $messagetext)) {
$nbemailprocessed++;
$operationslog .= '<br>Discarded - Email body contains string '.$searchfilterexcludebody;
dol_syslog(" Discarded - Email body contains string ".$searchfilterexcludebody);
continue; // Exclude email
}
}
//var_dump($plainmsg); //var_dump($plainmsg);
//var_dump($htmlmsg); //var_dump($htmlmsg);
//var_dump($messagetext); //var_dump($messagetext);
@ -1679,7 +1769,7 @@ class EmailCollector extends CommonObject
// Parse IMAP email structure // Parse IMAP email structure
/* /*
$structure = imap_fetchstructure($connection, $imapemail, 0); $structure = imap_fetchstructure($connection, $imapemail, FT_UID);
$partplain = $parthtml = -1; $partplain = $parthtml = -1;
$encodingplain = $encodinghtml = ''; $encodingplain = $encodinghtml = '';
@ -1716,7 +1806,7 @@ class EmailCollector extends CommonObject
//var_dump($parthtml); //var_dump($parthtml);
//var_dump($partplain); //var_dump($partplain);
$messagetext = imap_fetchbody($connection, $imapemail, ($parthtml != '-1' ? $parthtml : ($partplain != '-1' ? $partplain : 1)), FT_PEEK); $messagetext = imap_fetchbody($connection, $imapemail, ($parthtml != '-1' ? $parthtml : ($partplain != '-1' ? $partplain : 1)), FT_PEEK|FTP_UID);
*/ */
//var_dump($messagetext); //var_dump($messagetext);
@ -1753,6 +1843,14 @@ class EmailCollector extends CommonObject
//var_dump($msgid);exit; //var_dump($msgid);exit;
} }
if ($searchfilterexcludesubject) {
if (preg_match('/'.preg_quote($searchfilterexcludesubject, '/').'/ms', $subject)) {
$nbemailprocessed++;
$operationslog .= '<br>Discarded - Email subject contains string '.$searchfilterexcludesubject;
dol_syslog(" Discarded - Email subject contains string ".$searchfilterexcludesubject);
continue; // Exclude email
}
}
$reg = array(); $reg = array();
if (preg_match('/^(.*)<(.*)>$/', $fromstring, $reg)) { if (preg_match('/^(.*)<(.*)>$/', $fromstring, $reg)) {
@ -1771,6 +1869,7 @@ class EmailCollector extends CommonObject
} }
$fk_element_id = 0; $fk_element_type = ''; $fk_element_id = 0; $fk_element_type = '';
$this->db->begin();
$contactid = 0; $thirdpartyid = 0; $projectid = 0; $ticketid = 0; $contactid = 0; $thirdpartyid = 0; $projectid = 0; $ticketid = 0;
@ -2871,7 +2970,7 @@ class EmailCollector extends CommonObject
$result = $candidaturetocreate->create($user); $result = $candidaturetocreate->create($user);
if ($result <= 0) { if ($result <= 0) {
$errorforactions++; $errorforactions++;
$this->error = 'Failed to create ticket: '.join(', ', $candidaturetocreate->errors); $this->error = 'Failed to create candidature: '.join(', ', $candidaturetocreate->errors);
$this->errors = $candidaturetocreate->errors; $this->errors = $candidaturetocreate->errors;
} }
@ -2927,26 +3026,24 @@ class EmailCollector extends CommonObject
// Error for email or not ? // Error for email or not ?
if (!$errorforactions) { if (!$errorforactions) {
if (!empty($targetdir) && empty($mode)) { if (!empty($targetdir)) {
if (empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) { if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
dol_syslog("EmailCollector::doCollectOneCollector move message ".((string) $imapemail)." to ".$connectstringtarget, LOG_DEBUG);
$res = imap_mail_move($connection, $imapemail, $targetdir, 0);
if ($res == false) {
$errorforemail++;
$this->error = imap_last_error();
$this->errors[] = $this->error;
dol_syslog(imap_last_error());
}
} else {
// Move mail using PHP-IMAP // Move mail using PHP-IMAP
dol_syslog("EmailCollector::doCollectOneCollector move message ".($imapemail->getHeader()->get('subject'))." to ".$targetdir, LOG_DEBUG); dol_syslog("EmailCollector::doCollectOneCollector move message ".($imapemail->getHeader()->get('subject'))." to ".$targetdir, LOG_DEBUG);
if (empty($mode)) {
$imapemail->move($targetdir); $imapemail->move($targetdir);
} }
} else { } else {
if (empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) { dol_syslog("EmailCollector::doCollectOneCollector move message ".((string) $imapemail)." to ".$connectstringtarget, LOG_DEBUG);
dol_syslog("EmailCollector::doCollectOneCollector message ".((string) $imapemail)." to ".$connectstringtarget." was set to read", LOG_DEBUG); $operationslog .= '<br>Move mail '.((string) $imapemail).' - '.$msgid;
$arrayofemailtodelete[$imapemail] = $msgid;
}
} else { } else {
if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
dol_syslog("EmailCollector::doCollectOneCollector message '".($imapemail->getHeader()->get('subject'))."' using this->host=".$this->host.", this->access_type=".$this->acces_type." was set to read", LOG_DEBUG); dol_syslog("EmailCollector::doCollectOneCollector message '".($imapemail->getHeader()->get('subject'))."' using this->host=".$this->host.", this->access_type=".$this->acces_type." was set to read", LOG_DEBUG);
} else {
dol_syslog("EmailCollector::doCollectOneCollector message ".((string) $imapemail)." to ".$connectstringtarget." was set to read", LOG_DEBUG);
} }
} }
} else { } else {
@ -2990,12 +3087,34 @@ class EmailCollector extends CommonObject
$output = $langs->trans('NoNewEmailToProcess'); $output = $langs->trans('NoNewEmailToProcess');
} }
// Disconnect
if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) { if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
$client->disconnect(); $client->disconnect();
} else { } else {
foreach ($arrayofemailtodelete as $imapemail => $msgid) {
dol_syslog("EmailCollect::doCollectOneCollector delete email ".$imapemail." ".$msgid);
$operationslog .= "<br> delete email ".$imapemail." ".$msgid;
if (empty($mode) && empty($error)) {
$res = imap_mail_move($connection, $imapemail, $targetdir, CP_UID);
if ($res == false) {
$errorforemail++;
$this->error = imap_last_error();
$this->errors[] = $this->error;
$operationslog .= '<br>Error in move '.$this->error;
dol_syslog(imap_last_error());
}
}
}
if (empty($mode) && empty($error)) { if (empty($mode) && empty($error)) {
dol_syslog("Expunge", LOG_DEBUG); dol_syslog("Expunge", LOG_DEBUG);
imap_expunge($connection); // To validate any move $operationslog .= "<br>Expunge";
imap_expunge($connection); // To validate all moves
} }
imap_close($connection); imap_close($connection);
} }
@ -3036,7 +3155,7 @@ class EmailCollector extends CommonObject
* getmsg * getmsg
* *
* @param Object $mbox Structure * @param Object $mbox Structure
* @param string $mid prefix * @param string $mid UID email
* @param string $destdir Target dir for attachments * @param string $destdir Target dir for attachments
* @return array Array with number and object * @return array Array with number and object
*/ */
@ -3053,7 +3172,8 @@ class EmailCollector extends CommonObject
// add code here to get date, from, to, cc, subject... // add code here to get date, from, to, cc, subject...
// BODY // BODY
$s = imap_fetchstructure($mbox, $mid); $s = imap_fetchstructure($mbox, $mid, FT_UID);
if (!$s->parts) { if (!$s->parts) {
// simple // simple
@ -3099,8 +3219,8 @@ class EmailCollector extends CommonObject
// DECODE DATA // DECODE DATA
$data = ($partno) ? $data = ($partno) ?
imap_fetchbody($mbox, $mid, $partno) : // multipart imap_fetchbody($mbox, $mid, $partno, FT_UID) : // multipart
imap_body($mbox, $mid); // simple imap_body($mbox, $mid, FT_UID); // simple
// Any part may be encoded, even plain text messages, so check everything. // Any part may be encoded, even plain text messages, so check everything.
if ($p->encoding == 4) { if ($p->encoding == 4) {
$data = quoted_printable_decode($data); $data = quoted_printable_decode($data);
@ -3153,13 +3273,14 @@ class EmailCollector extends CommonObject
* Check if the same file name already exists in the upload folder, * Check if the same file name already exists in the upload folder,
* append increment number to the original filename * append increment number to the original filename
*/ */
while (file_exists($destdir."/" . $file_name . "." . $extension)) { while (file_exists($destdir."/".$file_name.".".$extension)) {
$file_name = (string) $file_name_original . ' (' . $num . ')'; $file_name = $file_name_original . ' (' . $num . ')';
$file_name_complete = $file_name . "." . $extension; $file_name_complete = $file_name . "." . $extension;
$destination = $destdir.'/'.$file_name_complete; $destination = $destdir.'/'.$file_name_complete;
$num++; $num++;
} }
$destination = dol_sanitizePathName($destination);
file_put_contents($destination, $data); file_put_contents($destination, $data);
} }