Skip to content

Commit 4b4cf5c

Browse files
committed
Merge branch 'imap-migrator-fix'
2 parents a12a3d0 + ae4c5e8 commit 4b4cf5c

File tree

8 files changed

+64
-30
lines changed

8 files changed

+64
-30
lines changed

MailLibrary/ContactList.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
class ContactList implements Iterator, Countable
1212
{
1313
/** @var Contact[] */
14-
protected $contacts;
14+
protected $contacts = [];
1515

16-
protected $builtContacts;
16+
protected $builtContacts = [];
1717

1818
public function addContact($mailbox = NULL, $host = NULL, $personal = NULL, $adl = NULL)
1919
{

MailLibrary/Drivers/ImapDriver.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use greeny\MailLibrary\Mailbox;
1111
use greeny\MailLibrary\Structures\IStructure;
1212
use greeny\MailLibrary\Structures\ImapStructure;
13+
use Nette\Utils\Strings;
1314
use greeny\MailLibrary\Mail;
1415
use DateTime;
1516

@@ -227,18 +228,21 @@ public function checkFilter($key, $value = NULL) {
227228
public function getHeaders($mailId)
228229
{
229230
$raw = imap_fetchheader($this->resource, $mailId, FT_UID);
230-
$lines = explode("\n", $raw);
231+
$lines = explode("\n", Strings::fixEncoding($raw));
231232
$headers = array();
232233
$lastHeader = NULL;
234+
235+
// normalize headers
233236
foreach($lines as $line) {
234-
if(mb_substr($line, 0, 1, 'UTF-8') === " ") {
235-
$headers[$lastHeader] .= $line;
237+
$firstCharacter = mb_substr($line, 0, 1, 'UTF-8'); // todo: correct assumption that string must be UTF-8 encoded?
238+
if(preg_match('/[\pZ\pC]/u', $firstCharacter) === 1) { // search for UTF-8 whitespaces
239+
$headers[$lastHeader] .= " " . Strings::trim($line);
236240
} else {
237241
$parts = explode(':', $line);
238-
$name = $parts[0];
242+
$name = Strings::trim($parts[0]);
239243
unset($parts[0]);
240244

241-
$headers[$name] = implode(':', $parts);
245+
$headers[$name] = Strings::trim(implode(':', $parts));
242246
$lastHeader = $name;
243247
}
244248
}
@@ -254,7 +258,7 @@ public function getHeaders($mailId)
254258
$text = '';
255259
foreach($decoded as $part) {
256260
if($part->charset !== 'UTF-8' && $part->charset !== 'default') {
257-
$text .= mb_convert_encoding($part->text, 'UTF-8', $part->charset);
261+
$text .= @mb_convert_encoding($part->text, 'UTF-8', $part->charset); // todo: handle this more properly
258262
} else {
259263
$text .= $part->text;
260264
}

MailLibrary/Mail.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,12 @@ public function getHeaders()
124124
public function getHeader($name)
125125
{
126126
$this->headers !== NULL || $this->initializeHeaders();
127-
return $this->headers[$this->formatHeaderName($name)];
127+
$index = $this->formatHeaderName($name);
128+
if(isset($this->headers[$index])) {
129+
return $this->headers[$index];
130+
} else {
131+
return NULL;
132+
}
128133
}
129134

130135
/**

MailLibrary/loader.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@
66
require_once "exceptions.php";
77

88
spl_autoload_register(function ($type) {
9-
static $paths = array(
10-
'greeny\maillibrary\connection' => 'Connection.php',
11-
'greeny\maillibrary\mailbox' => 'Mailbox.php',
12-
'greeny\maillibrary\selection' => 'Selection.php',
13-
'greeny\maillibrary\mail' => 'Mail.php',
14-
'greeny\maillibrary\contactlist' => 'ContactList.php',
15-
'greeny\maillibrary\contact' => 'Contact.php',
16-
'greeny\maillibrary\attachment' => 'Attachment.php',
17-
'greeny\maillibrary\structures\istructure' => 'Structures/IStructure.php',
18-
'greeny\maillibrary\structures\imapstructure' => 'Structures/ImapStructure.php',
19-
'greeny\maillibrary\drivers\idriver' => 'Drivers/IDriver.php',
20-
'greeny\maillibrary\drivers\imapdriver' => 'Drivers/ImapDriver.php',
21-
'greeny\maillibrary\extensions\maillibraryextension' => 'Extensions/MailLibraryExtension.php',
22-
);
9+
static $paths = array(
10+
'greeny\maillibrary\connection' => 'Connection.php',
11+
'greeny\maillibrary\mailbox' => 'Mailbox.php',
12+
'greeny\maillibrary\selection' => 'Selection.php',
13+
'greeny\maillibrary\mail' => 'Mail.php',
14+
'greeny\maillibrary\contactlist' => 'ContactList.php',
15+
'greeny\maillibrary\contact' => 'Contact.php',
16+
'greeny\maillibrary\attachment' => 'Attachment.php',
17+
'greeny\maillibrary\structures\istructure' => 'Structures/IStructure.php',
18+
'greeny\maillibrary\structures\imapstructure' => 'Structures/ImapStructure.php',
19+
'greeny\maillibrary\drivers\idriver' => 'Drivers/IDriver.php',
20+
'greeny\maillibrary\drivers\imapdriver' => 'Drivers/ImapDriver.php',
21+
'greeny\maillibrary\extensions\maillibraryextension' => 'Extensions/MailLibraryExtension.php',
22+
);
2323

24-
$type = ltrim(strtolower($type), '\\'); // PHP namespace bug #49143
24+
$type = ltrim(strtolower($type), '\\'); // PHP namespace bug #49143
2525

26-
if (isset($paths[$type])) {
27-
require_once __DIR__ . '/' . $paths[$type];
28-
}
26+
if (isset($paths[$type])) {
27+
require_once __DIR__ . '/' . $paths[$type];
28+
}
2929
});

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,17 @@ MailLibrary
33

44
A PHP library for downloading mails from server.
55

6-
Documentation can be found at http://greeny.github.io/MailLibrary/.
6+
Documentation can be found at http://greeny.github.io/MailLibrary/.
7+
8+
Testing
9+
-------
10+
11+
Install dependencies using composer and then run following in library root directory.
12+
13+
````cmd
14+
# Unix
15+
vendor\bin\tester -c tests\php-unix.ini tests
16+
17+
# Windows
18+
vendor\bin\tester -c tests\php-windows.ini tests
19+
````

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
"files": ["MailLibrary/loader.php"]
1313
},
1414
"require": {
15-
"php": ">= 5.3.0"
15+
"php": ">= 5.3.0",
16+
"ext-imap": "*",
17+
"nette/utils": "~2.2"
1618
},
1719
"require-dev": {
1820
"nette/tester": "@dev"
1921
}
20-
}
22+
}

tests/php-unix.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[PHP]
2+
;extension_dir = "./ext"
3+
extension=mbstring.so
4+
extension=imap.so
5+
date.timezone = "Europe/Prague"

tests/php-windows.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[PHP]
2+
extension_dir = "./ext"
3+
extension=php_mbstring.dll
4+
extension=php_imap.dll
5+
date.timezone = "Europe/Prague"

0 commit comments

Comments
 (0)