FIX: Email Collector Module: manage error when imap_fetchstructure return false. Previously it generated warning and fatal error because the returned value was not of type class

This commit is contained in:
Ryad ABANI
2025-10-22 11:56:07 +02:00
parent a4b78f8230
commit 38980a935c
2 changed files with 35 additions and 15 deletions

View File

@@ -850,10 +850,9 @@ class EmailCollector extends CommonObject
{
global $user;
$nberror = 0;
$nbErrors = 0;
$arrayofcollectors = $this->fetchAll($user, 1);
// Loop on each collector
foreach ($arrayofcollectors as $emailcollector) {
$result = $emailcollector->doCollectOneCollector(0);
@@ -862,11 +861,12 @@ class EmailCollector extends CommonObject
$this->error .= 'EmailCollector ID '.$emailcollector->id.':'.$emailcollector->error.'<br>';
if (!empty($emailcollector->errors)) {
$this->error .= join('<br>', $emailcollector->errors);
$nbErrors++;
}
$this->output .= 'EmailCollector ID '.$emailcollector->id.': '.$emailcollector->lastresult.'<br>';
}
return $nberror;
return $nbErrors;
}
/**
@@ -1755,7 +1755,11 @@ class EmailCollector extends CommonObject
$attachments = [];
}
} else {
$this->getmsg($connection, $imapemail); // This set global var $charset, $htmlmsg, $plainmsg, $attachments
$getMsg = $this->getmsg($connection, $imapemail); // This set global var $charset, $htmlmsg, $plainmsg, $attachments
if ($getMsg < 0) {
$this->errors = array_merge($this->errors, [$this->error]);
return $getMsg;
}
}
//print $plainmsg;
//var_dump($plainmsg); exit;
@@ -2916,7 +2920,11 @@ class EmailCollector extends CommonObject
$attachment->save($destdir.'/');
}
} else {
$this->getmsg($connection, $imapemail, $destdir);
$getMsg = $this->getmsg($connection, $imapemail, $destdir);
if ($getMsg < 0) {
$this->errors = array_merge($this->errors, [$this->error]);
return $getMsg;
}
}
$operationslog .= '<br>Project created with attachments -> id='.dol_escape_htmltag($projecttocreate->id);
@@ -3046,7 +3054,11 @@ class EmailCollector extends CommonObject
$attachment->save($destdir.'/');
}
} else {
$this->getmsg($connection, $imapemail, $destdir);
$getMsg = $this->getmsg($connection, $imapemail, $destdir);
if ($getMsg < 0) {
$this->errors = array_merge($this->errors, [$this->error]);
return $getMsg;
}
}
$operationslog .= '<br>Ticket created with attachments -> id='.dol_escape_htmltag($tickettocreate->id);
@@ -3330,9 +3342,9 @@ class EmailCollector extends CommonObject
* @param Object $mbox Structure
* @param string $mid UID email
* @param string $destdir Target dir for attachments
* @return void
* @return int
*/
private function getmsg($mbox, $mid, $destdir = '')
private function getmsg($mbox, $mid, $destdir = ''): int
{
// input $mbox = IMAP stream, $mid = message id
// output all the following:
@@ -3346,9 +3358,12 @@ class EmailCollector extends CommonObject
// BODY
$s = imap_fetchstructure($mbox, $mid, FT_UID);
if ($s === false) {
$this->errors = array_merge($this->errors, [imap_last_error()]);
return -1;
}
if (!$s->parts) {
if (empty($s->parts)) {
// simple
$this->getpart($mbox, $mid, $s, 0); // pass 0 as part-number
} else {
@@ -3357,6 +3372,8 @@ class EmailCollector extends CommonObject
$this->getpart($mbox, $mid, $p, $partno0 + 1, $destdir);
}
}
return 1;
}
/* partno string

View File

@@ -115,14 +115,16 @@ function getDParameters($part)
* @param object $mbox object connection imaap
* @return array type, filename, pos
*/
function getAttachments($jk, $mbox)
{
$structure = imap_fetchstructure($mbox, $jk, FT_UID);
function getAttachments($jk, $mbox) {
$structure = imap_fetchstructure($mbox, $jk, FT_UID); // @phan-suppress-current-line PhanTypeMismatchArgumentInternal
$parts = getParts($structure);
$fpos = 2;
$attachments = array();
$nb = count($parts);
if ($parts && $nb) {
if (!empty($parts)) {
$nb = count($parts);
for ($i = 1; $i < $nb; $i++) {
$part = $parts[$i];
@@ -139,6 +141,7 @@ function getAttachments($jk, $mbox)
$fpos++;
}
}
return $attachments;
}