Skip to content

Commit

Permalink
mailattachment: move isAttachment logic to method
Browse files Browse the repository at this point in the history
  • Loading branch information
glensc committed Dec 28, 2017
1 parent cc5a7d8 commit 2b57a54
Showing 1 changed file with 42 additions and 30 deletions.
72 changes: 42 additions & 30 deletions src/Mail/MailAttachment.php
Expand Up @@ -16,12 +16,13 @@
use Eventum\Mail\Helper\DecodePart;
use Zend\Mail;
use Zend\Mail\Header\ContentType;
use Zend\Mail\Storage;
use Zend\Mail\Storage\Message;
use Zend\Mime\Part;

class MailAttachment
{
/** @var MailMessage */
/** @var MailMessage|Storage\Part\PartInterface */
private $message;

public function __construct(MailMessage $message)
Expand All @@ -34,6 +35,7 @@ public function __construct(MailMessage $message)
* inline text messages are not accounted as attachments.
*
* TODO: handle application/pgp-signature, application/ms-tnef?
*
* @see https://github.com/eventum/eventum/blob/v3.2.1/lib/eventum/class.mime_helper.php#L740-L753
*
* @return bool
Expand All @@ -45,39 +47,13 @@ public function hasAttachments()
return false;
}

$has_attachments = 0;

// check what really the attachments are
/** @var MailMessage $part */
foreach ($this->message as $part) {
$is_attachment = 0;
$disposition = $filename = null;

$ctype = $part->getHeaderField('Content-Type');
if ($part->getHeaders()->has('Content-Disposition')) {
$disposition = $part->getHeaderField('Content-Disposition');
$filename = $part->getHeaderField('Content-Disposition', 'filename');
$is_attachment = $disposition === 'attachment' || $filename;
}

if (in_array($ctype, ['text/plain', 'text/html', 'text/enriched'])) {
$has_attachments |= $is_attachment;
} elseif ($ctype === 'multipart/related') {
// multipart/related may have subparts (inline html)
$attachment = new self($part);
$has_attachments |= $attachment->hasAttachments();
} else {
// avoid treating forwarded messages as attachments
$is_attachment |= ($disposition === 'inline' && $ctype !== 'message/rfc822');
// handle inline images
$type = current(explode('/', $ctype));
$is_attachment |= $type === 'image';

$has_attachments |= $is_attachment;
if ($this->isAttachment($part)) {
return true;
}
}

return (bool)$has_attachments;
return false;
}

/**
Expand Down Expand Up @@ -145,4 +121,40 @@ public function getAttachments()

return $attachments;
}

/**
* @param MailMessage $part
* @return bool
*/
private function isAttachment(MailMessage $part)
{
$is_attachment = false;
$disposition = $filename = null;

$ctype = $part->getHeaderField('Content-Type');
if ($part->getHeaders()->has('Content-Disposition')) {
$disposition = $part->getHeaderField('Content-Disposition');
$filename = $part->getHeaderField('Content-Disposition', 'filename');
$is_attachment = $disposition === 'attachment' || $filename;
}

if (in_array($ctype, ['text/plain', 'text/html', 'text/enriched'], true)) {
return $is_attachment;
}

if ($ctype === 'multipart/related') {
// multipart/related may have subparts (inline html)
return (new self($part))->hasAttachments();
}

// avoid treating forwarded messages as attachments
if ($disposition === 'inline' && $ctype !== 'message/rfc822') {
return true;
}

// handle inline images
$type = current(explode('/', $ctype));

return $type === 'image';
}
}

0 comments on commit 2b57a54

Please sign in to comment.