From 6a4227d0caa1e11514d0120fa418369e63b733f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kucha=C5=99?= Date: Tue, 29 Dec 2015 01:24:28 +0100 Subject: [PATCH 1/7] loader: Fixed to be able to load Contact class (fixed indention back to tabs) --- MailLibrary/loader.php | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/MailLibrary/loader.php b/MailLibrary/loader.php index 998517c..e11bb40 100644 --- a/MailLibrary/loader.php +++ b/MailLibrary/loader.php @@ -6,24 +6,24 @@ require_once "exceptions.php"; spl_autoload_register(function ($type) { - static $paths = array( - 'greeny\maillibrary\connection' => 'Connection.php', - 'greeny\maillibrary\mailbox' => 'Mailbox.php', - 'greeny\maillibrary\selection' => 'Selection.php', - 'greeny\maillibrary\mail' => 'Mail.php', - 'greeny\maillibrary\contactlist' => 'ContactList.php', - 'greeny\maillibrary\contact' => 'Contact.php', - 'greeny\maillibrary\attachment' => 'Attachment.php', - 'greeny\maillibrary\structures\istructure' => 'Structures/IStructure.php', - 'greeny\maillibrary\structures\imapstructure' => 'Structures/ImapStructure.php', - 'greeny\maillibrary\drivers\idriver' => 'Drivers/IDriver.php', - 'greeny\maillibrary\drivers\imapdriver' => 'Drivers/ImapDriver.php', - 'greeny\maillibrary\extensions\maillibraryextension' => 'Extensions/MailLibraryExtension.php', - ); + static $paths = array( + 'greeny\maillibrary\connection' => 'Connection.php', + 'greeny\maillibrary\mailbox' => 'Mailbox.php', + 'greeny\maillibrary\selection' => 'Selection.php', + 'greeny\maillibrary\mail' => 'Mail.php', + 'greeny\maillibrary\contactlist' => 'ContactList.php', + 'greeny\maillibrary\contact' => 'Contact.php', + 'greeny\maillibrary\attachment' => 'Attachment.php', + 'greeny\maillibrary\structures\istructure' => 'Structures/IStructure.php', + 'greeny\maillibrary\structures\imapstructure' => 'Structures/ImapStructure.php', + 'greeny\maillibrary\drivers\idriver' => 'Drivers/IDriver.php', + 'greeny\maillibrary\drivers\imapdriver' => 'Drivers/ImapDriver.php', + 'greeny\maillibrary\extensions\maillibraryextension' => 'Extensions/MailLibraryExtension.php', + ); - $type = ltrim(strtolower($type), '\\'); // PHP namespace bug #49143 + $type = ltrim(strtolower($type), '\\'); // PHP namespace bug #49143 - if (isset($paths[$type])) { - require_once __DIR__ . '/' . $paths[$type]; - } + if (isset($paths[$type])) { + require_once __DIR__ . '/' . $paths[$type]; + } }); \ No newline at end of file From efd4f8ddeac960a6f6024038417d08f307421e17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kucha=C5=99?= Date: Tue, 29 Dec 2015 00:01:10 +0100 Subject: [PATCH 2/7] Contact: fixed uninitialized properties --- MailLibrary/ContactList.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MailLibrary/ContactList.php b/MailLibrary/ContactList.php index a408bca..2f2a362 100644 --- a/MailLibrary/ContactList.php +++ b/MailLibrary/ContactList.php @@ -11,9 +11,9 @@ class ContactList implements Iterator, Countable { /** @var Contact[] */ - protected $contacts; + protected $contacts = []; - protected $builtContacts; + protected $builtContacts = []; public function addContact($mailbox = NULL, $host = NULL, $personal = NULL, $adl = NULL) { From ae6e5600b294881f03dc1b3f38e0ab9883bd42b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kucha=C5=99?= Date: Tue, 29 Dec 2015 00:23:51 +0100 Subject: [PATCH 3/7] tests: added how to run tests and introduced tests config files --- README.md | 15 ++++++++++++++- tests/php-unix.ini | 5 +++++ tests/php-windows.ini | 5 +++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 tests/php-unix.ini create mode 100644 tests/php-windows.ini diff --git a/README.md b/README.md index 79fc5d9..08f3980 100644 --- a/README.md +++ b/README.md @@ -3,4 +3,17 @@ MailLibrary A PHP library for downloading mails from server. -Documentation can be found at http://greeny.github.io/MailLibrary/. \ No newline at end of file +Documentation can be found at http://greeny.github.io/MailLibrary/. + +Testing +------- + +Install dependencies using composer and then run following in library root directory. + +````cmd +# Unix +vendor\bin\tester -c tests\php-unix.ini tests + +# Windows +vendor\bin\tester -c tests\php-windows.ini tests +```` diff --git a/tests/php-unix.ini b/tests/php-unix.ini new file mode 100644 index 0000000..c758598 --- /dev/null +++ b/tests/php-unix.ini @@ -0,0 +1,5 @@ +[PHP] +;extension_dir = "./ext" +extension=mbstring.so +extension=imap.so +date.timezone = "Europe/Prague" diff --git a/tests/php-windows.ini b/tests/php-windows.ini new file mode 100644 index 0000000..f601ad4 --- /dev/null +++ b/tests/php-windows.ini @@ -0,0 +1,5 @@ +[PHP] +extension_dir = "./ext" +extension=php_mbstring.dll +extension=php_imap.dll +date.timezone = "Europe/Prague" From 7a880955454ee82d21430936859532826f30b42b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kucha=C5=99?= Date: Tue, 29 Dec 2015 00:28:03 +0100 Subject: [PATCH 4/7] composer: added forgotten library dependency --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 8b0751c..16c6036 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,8 @@ "files": ["MailLibrary/loader.php"] }, "require": { - "php": ">= 5.3.0" + "php": ">= 5.3.0", + "ext-imap": "*" }, "require-dev": { "nette/tester": "@dev" From 8ab921d2ac31a79baec41c84e87337cd221d0edc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kucha=C5=99?= Date: Tue, 29 Dec 2015 00:34:30 +0100 Subject: [PATCH 5/7] Mail: getHeader now returns NULL when header not available --- MailLibrary/Mail.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/MailLibrary/Mail.php b/MailLibrary/Mail.php index 0431948..4991d55 100644 --- a/MailLibrary/Mail.php +++ b/MailLibrary/Mail.php @@ -124,7 +124,12 @@ public function getHeaders() public function getHeader($name) { $this->headers !== NULL || $this->initializeHeaders(); - return $this->headers[$this->formatHeaderName($name)]; + $index = $this->formatHeaderName($name); + if(isset($this->headers[$index])) { + return $this->headers[$index]; + } else { + return NULL; + } } /** From 1b615f1744be278463e9f920b04037a35af38501 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kucha=C5=99?= Date: Tue, 29 Dec 2015 00:44:12 +0100 Subject: [PATCH 6/7] ImapDriver: getHeaders fix headers parser bug, now properly handles whitespaces, normalizes UTF-8 strings and trims headers content --- MailLibrary/Drivers/ImapDriver.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/MailLibrary/Drivers/ImapDriver.php b/MailLibrary/Drivers/ImapDriver.php index eb059e7..c6831a1 100644 --- a/MailLibrary/Drivers/ImapDriver.php +++ b/MailLibrary/Drivers/ImapDriver.php @@ -10,6 +10,7 @@ use greeny\MailLibrary\Mailbox; use greeny\MailLibrary\Structures\IStructure; use greeny\MailLibrary\Structures\ImapStructure; +use Nette\Utils\Strings; use greeny\MailLibrary\Mail; use DateTime; @@ -227,18 +228,21 @@ public function checkFilter($key, $value = NULL) { public function getHeaders($mailId) { $raw = imap_fetchheader($this->resource, $mailId, FT_UID); - $lines = explode("\n", $raw); + $lines = explode("\n", Strings::fixEncoding($raw)); $headers = array(); $lastHeader = NULL; + + // normalize headers foreach($lines as $line) { - if(mb_substr($line, 0, 1, 'UTF-8') === " ") { - $headers[$lastHeader] .= $line; + $firstCharacter = mb_substr($line, 0, 1, 'UTF-8'); // todo: correct assumption that string must be UTF-8 encoded? + if(preg_match('/[\pZ\pC]/u', $firstCharacter) === 1) { // search for UTF-8 whitespaces + $headers[$lastHeader] .= " " . Strings::trim($line); } else { $parts = explode(':', $line); - $name = $parts[0]; + $name = Strings::trim($parts[0]); unset($parts[0]); - $headers[$name] = implode(':', $parts); + $headers[$name] = Strings::trim(implode(':', $parts)); $lastHeader = $name; } } @@ -254,7 +258,7 @@ public function getHeaders($mailId) $text = ''; foreach($decoded as $part) { if($part->charset !== 'UTF-8' && $part->charset !== 'default') { - $text .= mb_convert_encoding($part->text, 'UTF-8', $part->charset); + $text .= @mb_convert_encoding($part->text, 'UTF-8', $part->charset); // todo: handle this more properly } else { $text .= $part->text; } From ae4c5e894ab6cebc5813002ca9bb09ed35c3ceb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kucha=C5=99?= Date: Tue, 29 Dec 2015 01:07:21 +0100 Subject: [PATCH 7/7] composer: added nette/utils as dependency (needed for work with UTF-8 strings) --- composer.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 16c6036..d04e0ac 100644 --- a/composer.json +++ b/composer.json @@ -13,9 +13,10 @@ }, "require": { "php": ">= 5.3.0", - "ext-imap": "*" + "ext-imap": "*", + "nette/utils": "~2.2" }, "require-dev": { "nette/tester": "@dev" } -} \ No newline at end of file +}