Skip to content

Commit

Permalink
Merge branch 'v1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Slamdunk committed Oct 12, 2017
2 parents 0bf18d8 + 63d472b commit d2a3cd8
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 22 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,12 @@
# Change Log

## [1.0.5](https://github.com/ddeboer/imap/tree/1.0.5) (2017-10-12)
[Full Changelog](https://github.com/ddeboer/imap/compare/1.0.4...1.0.5)

**Fixed bugs:**

- Use set\_error\_handler with late exception [\#240](https://github.com/ddeboer/imap/pull/240) ([Slamdunk](https://github.com/Slamdunk))

## [1.0.4](https://github.com/ddeboer/imap/tree/1.0.4) (2017-10-11)
[Full Changelog](https://github.com/ddeboer/imap/compare/1.0.3...1.0.4)

Expand Down
19 changes: 14 additions & 5 deletions src/Message.php
Expand Up @@ -49,18 +49,27 @@ public function __construct(ImapResourceInterface $resource, int $messageNumber)
*/
private function loadStructure(ImapResourceInterface $resource, int $messageNumber): \stdClass
{
\error_clear_last();
$structure = @\imap_fetchstructure(
$errorMessage = null;
$errorNumber = 0;
\set_error_handler(function ($nr, $message) use (&$errorMessage, &$errorNumber) {
$errorMessage = $message;
$errorNumber = $nr;
});

$structure = \imap_fetchstructure(
$resource->getStream(),
$messageNumber,
\FT_UID
);
if (null !== ($lastError = \error_get_last())) {

\restore_error_handler();

if (null !== $errorMessage) {
throw new MessageDoesNotExistException(\sprintf(
'Message "%s" does not exist: %s',
$messageNumber,
$lastError['message']
), $lastError['type']);
$errorMessage
), $errorNumber);
}

if (!$structure instanceof \stdClass) {
Expand Down
27 changes: 20 additions & 7 deletions src/Message/Transcoder.php
Expand Up @@ -252,15 +252,24 @@ public static function decode(string $text, string $fromCharset): string
return $iconvDecodedText;
}

\error_clear_last();
$decodedText = @\mb_convert_encoding($text, 'UTF-8', $fromCharset);
if (null !== ($lastError = \error_get_last())) {
$errorMessage = null;
$errorNumber = 0;
\set_error_handler(function ($nr, $message) use (&$errorMessage, &$errorNumber) {
$errorMessage = $message;
$errorNumber = $nr;
});

$decodedText = \mb_convert_encoding($text, 'UTF-8', $fromCharset);

\restore_error_handler();

if (null !== $errorMessage) {
throw new UnsupportedCharsetException(\sprintf(
'Unsupported charset "%s"%s: %s',
$originalFromCharset,
($fromCharset !== $originalFromCharset) ? \sprintf(' (alias found: "%s")', $fromCharset) : '',
$lastError['message']
), $lastError['type']);
$errorMessage
), $errorNumber);
}

return $decodedText;
Expand All @@ -286,11 +295,15 @@ private static function iconvDecode($text, $originalFromCharset, $fromCharset)
return false;
}

$iconvDecodedText = @\iconv($fromCharset, 'UTF-8', $text);
\set_error_handler(function () {});

$iconvDecodedText = \iconv($fromCharset, 'UTF-8', $text);
if (false === $iconvDecodedText) {
$iconvDecodedText = @\iconv($originalFromCharset, 'UTF-8', $text);
$iconvDecodedText = \iconv($originalFromCharset, 'UTF-8', $text);
}

\restore_error_handler();

return $iconvDecodedText;
}
}
25 changes: 15 additions & 10 deletions src/Server.php
Expand Up @@ -68,25 +68,30 @@ public function __construct(
*/
public function authenticate(string $username, string $password): ConnectionInterface
{
\error_clear_last();
$resource = @\imap_open(
$errorMessage = null;
$errorNumber = 0;
\set_error_handler(function ($nr, $message) use (&$errorMessage, &$errorNumber) {
$errorMessage = $message;
$errorNumber = $nr;
});

$resource = \imap_open(
$this->getServerString(),
$username,
$password,
0,
1,
$this->parameters
);
if (null !== ($lastError = \error_get_last())) {

\restore_error_handler();

if (false === $resource || null !== $errorMessage) {
throw new AuthenticationFailedException(\sprintf(
'Authentication failed for user "%s": %s',
'Authentication failed for user "%s"%s',
$username,
$lastError['message']
), $lastError['type']);
}

if (false === $resource) {
throw new AuthenticationFailedException(\sprintf('Authentication failed for user "%s"', $username));
null !== $errorMessage ? ': ' . $errorMessage : ''
), $errorNumber);
}

$check = \imap_check($resource);
Expand Down
12 changes: 12 additions & 0 deletions tests/MessageTest.php
Expand Up @@ -5,6 +5,7 @@
namespace Ddeboer\Imap\Tests;

use Ddeboer\Imap\Exception\InvalidDateHeaderException;
use Ddeboer\Imap\Exception\MessageDoesNotExistException;
use Ddeboer\Imap\Exception\UnsupportedCharsetException;
use Ddeboer\Imap\Message;
use Ddeboer\Imap\Message\EmailAddress;
Expand Down Expand Up @@ -57,6 +58,17 @@ protected function setUp()
$this->mailbox = $this->createMailbox();
}

public function testCustomNonExistentMessageFetch()
{
$connection = $this->getConnection();
$messageNumber = 98765;

$this->expectException(MessageDoesNotExistException::class);
$this->expectExceptionMessageRegExp(\sprintf('/E_WARNING.+%s/s', \preg_quote((string) $messageNumber)));

new Message($connection->getResource(), $messageNumber);
}

public function testAlwaysKeepUnseen()
{
$this->createTestMessage($this->mailbox, 'Message A');
Expand Down

0 comments on commit d2a3cd8

Please sign in to comment.