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

Improve error handling #223

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
8 changes: 5 additions & 3 deletions src/Fetch/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ public function __construct(Message $message, $structure, $partIdentifier = null
} elseif (isset($parameters['name'])) {
$this->setFileName($parameters['name']);
}

$this->size = $structure->bytes;

if (isset($structure->bytes)) {
$this->size = $structure->bytes;
}

$this->mimeType = Message::typeIdToString($structure->type);

Expand Down Expand Up @@ -135,7 +137,7 @@ public function getData()
*/
public function getFileName()
{
return (isset($this->filename)) ? $this->filename : false;
return (isset($this->filename)) ? preg_replace('/[^A-Za-z0-9\-_.]/', '_', $this->filename) : false;
}

/**
Expand Down
9 changes: 8 additions & 1 deletion src/Fetch/MIME.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,15 @@ public static function decode($text, $targetCharset = 'utf-8')

foreach (imap_mime_header_decode($text) as $word) {
$ch = 'default' === $word->charset ? 'ascii' : $word->charset;
$text = $word->text;
// Use the possible false return of `mb_encoding_aliases()` to detect whether we can process the encoding
if (function_exists('mb_convert_encoding') && @mb_encoding_aliases($ch)) {
// This will strip any unrecognised characters and ensure we avoid
// "Detected an incomplete multibyte character in input string" errors
$text = mb_convert_encoding($text, $ch, $ch);
}

$result .= iconv($ch, $targetCharset, $word->text);
$result .= iconv($ch, $targetCharset, $text);
}

return $result;
Expand Down
42 changes: 30 additions & 12 deletions src/Fetch/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@ class Message
*/
public static $charsetFlag = '//TRANSLIT';

/**
* This value defines the flag set for encoding for iconv to ignore the
* iconv(): Detected an illegal character in input string.
*
* @var string
*/
public static $charsetAltFlag = '//IGNORE';

/**
* These constants can be used to easily access available flags
*/
Expand Down Expand Up @@ -230,12 +238,15 @@ protected function loadMessage()
/* First load the message overview information */

if(!is_object($messageOverview = $this->getOverview()))

return false;

$this->subject = MIME::decode($messageOverview->subject, self::$charset);
$this->date = strtotime($messageOverview->date);
$this->size = $messageOverview->size;
$subject = property_exists($messageOverview, 'subject')? $messageOverview->subject : '';
$date = property_exists($messageOverview, 'date')? $messageOverview->date : '';
$size = property_exists($messageOverview, 'size')? $messageOverview->size : '';

$this->subject = MIME::decode($subject, self::$charset . self::$charsetAltFlag);
$this->date = strtotime($date);
$this->size = $size;

foreach (self::$flagTypes as $flag)
$this->status[$flag] = ($messageOverview->$flag == 1);
Expand Down Expand Up @@ -562,15 +573,21 @@ protected function processStructure($structure, $partIdentifier = null)
}
}

if (isset($structure->parts)) { // multipart: iterate through each part
if (! empty($structure->parts)) {

foreach ($structure->parts as $partIndex => $part) {
$partId = $partIndex + 1;
if (isset($structure->subtype) && strtolower($structure->subtype) === 'rfc822') {
// rfc822: The root part is processed with the current part identifier
$this->processStructure($structure->parts[0], $partIdentifier);
} else {
// multipart: iterate through each part
foreach ($structure->parts as $partIndex => $part) {
$partId = $partIndex + 1;

if (isset($partIdentifier))
$partId = $partIdentifier . '.' . $partId;
if (isset($partIdentifier))
$partId = $partIdentifier . '.' . $partId;

$this->processStructure($part, $partId);
$this->processStructure($part, $partId);
}
}
}
}
Expand Down Expand Up @@ -672,9 +689,10 @@ protected function processAddressObject($addresses)
foreach ($addresses as $address) {
if (property_exists($address, 'mailbox') && $address->mailbox != 'undisclosed-recipients') {
$currentAddress = array();
$currentAddress['address'] = $address->mailbox . '@' . $address->host;
$host = property_exists($address, 'host')?$address->host:'';
$currentAddress['address'] = $address->mailbox . '@' . $host;
if (isset($address->personal)) {
$currentAddress['name'] = MIME::decode($address->personal, self::$charset);
$currentAddress['name'] = MIME::decode($address->personal, self::$charset . self::$charsetAltFlag);
}
$outputAddresses[] = $currentAddress;
}
Expand Down
10 changes: 10 additions & 0 deletions src/Fetch/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,16 @@ public function getMessageByUid($uid)
}
}

/**
* This function returns all the imap errors to prevent the following
* warning from imap_close and imap_expunge
* 'Unknown: Warning: MIME header encountered in non-MIME message (errflg=3)'
*/
public function getImapErrors()
{
return imap_errors();
}

/**
* This function removes all of the messages flagged for deletion from the mailbox.
*
Expand Down