Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiple collector issues; fixes #7709, #7787 and #7790 #7748

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 56 additions & 69 deletions inc/mailcollector.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@
use LitEmoji\LitEmoji;
use Laminas\Mail\Address;
use Laminas\Mail\Header\AbstractAddressList;
use Laminas\Mail\Header\ContentDisposition;
use Laminas\Mail\Header\ContentType;
use Laminas\Mail\Storage\Message;
use Laminas\Mime\Mime as Laminas_Mime;

/**
* MailCollector class
Expand Down Expand Up @@ -1032,7 +1033,11 @@ function buildTicket($uid, \Laminas\Mail\Storage\Message $message, $options = []
$tkt['_do_not_check_users_id'] = 1;
$body = $this->getBody($message);

$subject = $message->subject;
try {
$subject = $message->getHeader('subject')->getFieldValue();
} catch (Laminas\Mail\Storage\Exception\InvalidArgumentException $e) {
$subject = null;
}
$tkt['_message'] = $message;

if (!Toolbox::seems_utf8($body)) {
Expand Down Expand Up @@ -1483,16 +1488,17 @@ private function getRecursiveAttached(\Laminas\Mail\Storage\Part $part, $path, $
);
}
} else {
if (!isset($part->contentDisposition)) {
//not an attachment
if (!$part->getHeaders()->has('content-type')
|| !(($content_type_header = $part->getHeader('content-type')) instanceof ContentType)) {
return false; // Ignore attachements with no content-type
}
$content_type = $content_type_header->getType();

if (!$part->getHeaders()->has('content-disposition') && preg_match('/^text\/.+/', $content_type)) {
// Ignore attachements with no content-disposition only if they corresponds to a text part.
// Indeed, some mail clients (like some Outlook versions) does not set any content-disposition
// header on inlined images.
return false;
} else {
if (strtok($part->contentDisposition, ';') != Laminas_Mime::DISPOSITION_ATTACHMENT
&& strtok($part->contentDisposition, ';') != Laminas_Mime::DISPOSITION_INLINE
) {
//not an attachment
return false;
}
}

// fix monoparted mail
Expand All @@ -1501,45 +1507,20 @@ private function getRecursiveAttached(\Laminas\Mail\Storage\Part $part, $path, $
}

$filename = '';
if (!isset($part->contentType)) {
Toolbox::logWarning('Current part does not have a content type.');
//content type missing
return false;
}

$header_type = $part->getHeader('contentType');
$content_type = $header_type->getType();

// get filename of attachment if present
// if there are any dparameters present in this part
if (isset($part->dparameters)) {
foreach ($part->getHeader('dparameters') as $dparam) {
if ((Toolbox::strtoupper($dparam->attribute) == 'NAME')
|| (Toolbox::strtoupper($dparam->attribute) == 'FILENAME')) {
$filename = $dparam->value;
}
}
}

// if there are any parameters present in this part
// Try to get filename from Content-Disposition header
if (empty($filename)
&& isset($part->parameters)) {
foreach ($part->getHeader('parameters') as $param) {
if ((Toolbox::strtoupper($param->attribute) == 'NAME')
|| (Toolbox::strtoupper($param->attribute) == 'FILENAME')) {
$filename = $param->value;
}
}
&& $part->getHeaders()->has('content-disposition')
&& ($content_disp_header = $part->getHeader('content-disposition')) instanceof ContentDisposition) {
$filename = $content_disp_header->getParameter('filename') ?? '';
}

// Try to get filename from Content-Type header
if (empty($filename)) {
$params = $header_type->getParameters();
if (isset($params['name'])) {
$filename = $params['name'];
}
$filename = $content_type_header->getParameter('name') ?? '';
}

// part come without correct filename in [d]parameters - generate trivial one
// part come without correct filename in headers - generate trivial one
// (inline images case for example)
if ((empty($filename) || !Document::isValidDoc($filename))) {
$tmp_filename = "doc_$subpart.".str_replace('image/', '', $content_type);
Expand Down Expand Up @@ -1652,20 +1633,19 @@ function getBody(\Laminas\Mail\Storage\Message $message) {
} else {
//if message is multipart, check for html contents then text contents
foreach (new RecursiveIteratorIterator($message) as $part) {
try {
if (strtok($part->contentType, ';') == 'text/html') {
$this->body_is_html = true;
$content = $this->getDecodedContent($part);
//do not check for text part if we found html one.
break;
}
if (strtok($part->contentType, ';') == 'text/plain' && $content === null) {
$this->body_is_html = false;
$content = $this->getDecodedContent($part);
}
} catch (Exception $e) {
// ignore
$catched = true;
if (!$part->getHeaders()->has('content-type')
|| !(($content_type = $part->getHeader('content-type')) instanceof ContentType)) {
continue;
}
if ($content_type->getType() == 'text/html') {
$this->body_is_html = true;
$content = $this->getDecodedContent($part);
//do not check for text part if we found html one.
break;
}
if ($content_type->getType() == 'text/plain' && $content === null) {
$this->body_is_html = false;
$content = $this->getDecodedContent($part);
}
}
}
Expand Down Expand Up @@ -2041,18 +2021,25 @@ public function getDecodedContent(\Laminas\Mail\Storage\Part $part) {
break;
}

try {
$contentTypePart = $part->getHeader('contentType');
$contentType = $contentTypePart->getType();
} catch (\Laminas\Mail\Storage\Exception\InvalidArgumentException $e) {
//no ContentType header, switch to acceptable default
$contentType = "text/plain";
} finally {
if (preg_match('/^text\//', $contentType) && ($encoding = mb_detect_encoding($contents)) != 'UTF-8') {
$contents = Toolbox::encodeInUtf8(
$contents,
(isset($contentTypePart) ? $contentTypePart->getEncoding() : $encoding)
);
if (!$part->getHeaders()->has('content-type')
|| !(($content_type = $part->getHeader('content-type')) instanceof ContentType)
| preg_match('/^text\//', $content_type->getType()) !== 1) {
return $contents; // No charset conversion content type header is not set or content is not text/*
}

$charset = $content_type->getParameter('charset');
if (strtoupper($charset) != 'UTF-8') {
if (in_array($charset, array_map('strtoupper', mb_list_encodings()))) {
$contents = mb_convert_encoding($contents, 'UTF-8', $charset);
} else {
// Convert Windows charsets names
if (preg_match('/^WINDOWS-\d{4}$/', $charset)) {
$charset = preg_replace('/^WINDOWS-(\d{4})$/', 'CP$1', $charset);
}

if ($converted = iconv($charset, 'UTF-8//TRANSLIT', $contents)) {
$contents = $converted;
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/emails-tests/00-unicode-smileys.eml
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ ont-family: arial, helvetica, sans-serif;"><strong><em><br></em></strong></=
span></div></div></div><span name=3D"x"></span><br></div></div></body></htm=
l>
------=_Part_3412096_1985743406.1547656904833
Content-Type: image/png; name=logoteclib.png
Content-Disposition: attachment; filename=logoteclib.png
Content-Type: image/png; name=00-logoteclib.png
Content-Disposition: attachment; filename=00-logoteclib.png
Content-Transfer-Encoding: base64
Content-ID: <3242f466bb5076c49a176740dfd0a1d18f1ca954@zimbra>

Expand Down
8 changes: 4 additions & 4 deletions tests/emails-tests/01-inline-images.eml
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ Content-Transfer-Encoding: 7bit
<html><body><div style="font-family: times new roman, new york, times, serif; font-size: 12pt; color: #000000"><div>Image 1</div><div><img src="cid:6f1f48de7c56cc3412e74008ad9f7c640091f5e3@zimbra" data-mce-src="cid:6f1f48de7c56cc3412e74008ad9f7c640091f5e3@zimbra"></div><div><br></div><div>Image 2</div><div><br></div><div><img src="cid:372381462ade792e8f775235219c7dc07899a321@zimbra" data-mce-src="cid:372381462ade792e8f775235219c7dc07899a321@zimbra"></div></div></body></html>
------=_Part_1757884_1267006027.1528365951028
Content-Type: image/png;
name="=?utf-8?Q?Screenshot-2018-4-12_Observatoire_-_France_tr=C3=A8s_haut_d=C3=A9bit=2Epng?="
name="=?utf-8?Q?01-Screenshot-2018-4-12_Observatoire_-_France_tr=C3=A8s_haut_d=C3=A9bit=2Epng?="
Content-Disposition: attachment;
filename="=?utf-8?Q?Screenshot-2018-4-12_Observatoire_-_France_tr=C3=A8s_haut_d=C3=A9bit=2Epng?="
filename="=?utf-8?Q?01-Screenshot-2018-4-12_Observatoire_-_France_tr=C3=A8s_haut_d=C3=A9bit=2Epng?="
Content-Transfer-Encoding: base64
Content-ID: <6f1f48de7c56cc3412e74008ad9f7c640091f5e3@zimbra>

Expand Down Expand Up @@ -4676,8 +4676,8 @@ BRHRer3OzRdDWXk0GrHgCcQdJchE0i73j2SFqf9exsNRBL7k9YT2h6qMitlMqSjrui770xeJ7oAh
9y1gUsUHAINoMsnRsYeApfzMAHqSqot8LwgCFp+CH/p8PifXdZnWHQQB54gQOEIjQ21+ZVmWOxOR
M2L+vtVq5WZHy3ha/D803/3zTNOXRwAAAABJRU5ErkJggg==
------=_Part_1757884_1267006027.1528365951028
Content-Type: image/jpeg; name=test.JPG
Content-Disposition: attachment; filename=test.JPG
Content-Type: image/jpeg; name=01-test.JPG
Content-Disposition: attachment; filename=01-test.JPG
Content-Transfer-Encoding: base64
Content-ID: <372381462ade792e8f775235219c7dc07899a321@zimbra>

Expand Down
71 changes: 71 additions & 0 deletions tests/emails-tests/13-koi8-r-encoded-body.eml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
Return-Path: <normal@glpi-project.org>
Received: from EUR04-VI1-obe.outbound.protection.outlook.com ([40.107.8.139])
by glpi-project.org (Kerio Connect 9.2.1) with ESMTPS
(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256 bits))
for unittests@glpi-project.org;
Thu, 16 Jul 2020 13:05:13 +0300
Received: from HE1PR04MB3116.eurprd04.prod.outlook.com (2603:10a6:7:21::11) by
HE1PR04MB3114.eurprd04.prod.outlook.com (2603:10a6:7:20::32) with Microsoft
SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id
15.20.3174.23; Thu, 16 Jul 2020 10:05:11 +0000
Received: from HE1PR04MB3116.eurprd04.prod.outlook.com
([fe80::90c8:bdd6:ea02:c40a]) by HE1PR04MB3116.eurprd04.prod.outlook.com
([fe80::90c8:bdd6:ea02:c40a%6]) with mapi id 15.20.3174.025; Thu, 16 Jul 2020
10:05:11 +0000
From: =?koi8-r?B?7cnIwcnMIPPB2sHOz9c=?= <normal@glpi-project.org>
To: <unittests@glpi-project.org>
Subject: =?windows-1251?B?7/Du4uXw6uA=?=
Thread-Topic: =?windows-1251?B?7/Du4uXw6uA=?=
Thread-Index: AQHWW1fCqpuqsQi/pk+Y/7pyDCKNEakJ+n+F
Date: Thu, 16 Jul 2020 10:05:11 +0000
Message-ID: <d00e2009-63b3-47be-bb40-5752204023d2@email.android.com>
References: <9d349a1b-40b9-47bd-a4d9-f55073f5488a@email.android.com>
Accept-Language: ru-RU, en-US
Content-Language: ru-RU
Content-Type: multipart/alternative;
boundary="_000_d00e200963b347bebb405752204023d2emailandroidcom_"
MIME-Version: 1.0

--_000_d00e200963b347bebb405752204023d2emailandroidcom_
Content-Type: text/plain; charset="koi8-r"
Content-Transfer-Encoding: quoted-printable

=F4=C5=D3=D4 =D4=C5=D3=D4 =D4=C5=D3=D4
---------- =F0=C5=D2=C5=C1=C4=D2=C5=D3=CF=D7=C1=CE=CE=CF=C5 =D3=CF=CF=C2=DD=
=C5=CE=C9=C5 ----------
=EF=D4: =ED=C9=C8=C1=C9=CC =F3=C1=DA=C1=CE=CF=D7 <normal@glpi-project.org>
=E4=C1=D4=C1: 16 =C9=C0=CC. 2020 =C7. 12:59
=F4=C5=CD=C1: Test from eml
=EB=CF=CD=D5: unittests@glpi-project.org
=EB=CF=D0=C9=D1:

Eml test ticket

--_000_d00e200963b347bebb405752204023d2emailandroidcom_
Content-Type: text/html; charset="koi8-r"
Content-Transfer-Encoding: quoted-printable

<html>
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dkoi8-r">
<meta content=3D"text/html; charset=3Dutf-8">
</head>
<body>
<div dir=3D"auto">=F4=C5=D3=D4 =D4=C5=D3=D4 =D4=C5=D3=D4</div>
<div class=3D"gmail_quote">---------- =F0=C5=D2=C5=C1=C4=D2=C5=D3=CF=D7=C1=
=CE=CE=CF=C5 =D3=CF=CF=C2=DD=C5=CE=C9=C5 ----------<br>
=EF=D4: =ED=C9=C8=C1=C9=CC =F3=C1=DA=C1=CE=CF=D7 &lt;normal@glpi-project.org&=
gt;<br>
=E4=C1=D4=C1: 16 =C9=C0=CC. 2020 =C7. 12:59<br>
=F4=C5=CD=C1: Test from eml<br>
=EB=CF=CD=D5: unittests@glpi-project.org<br>
=EB=CF=D0=C9=D1: <br>
<br type=3D"attribution">
</div>
<div>
<div dir=3D"auto">Eml test ticket</div>
</div>
</body>
</html>

--_000_d00e200963b347bebb405752204023d2emailandroidcom_--
Loading