aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIvan Enderlin <ivan.enderlin@hoa-project.net>2015-05-26 17:42:26 +0200
committerIvan Enderlin <ivan.enderlin@hoa-project.net>2015-05-26 17:49:52 +0200
commit9baa298ce00607ffc58f704465bfb1fe590a8c24 (patch)
treed5b4b317d65238690c380f3523e559260766d944
parent0a796975f2b4c13099ba8bf1b34572da6e3855e2 (diff)
downloadMail-9baa298ce00607ffc58f704465bfb1fe590a8c24.zip
Mail-9baa298ce00607ffc58f704465bfb1fe590a8c24.tar.gz
Mail-9baa298ce00607ffc58f704465bfb1fe590a8c24.tar.bz2
Implement RFC2047 on encoders.
Encoders have a different behavior when encoding a content or a header. See RFC2047 Sections 4 and 5.
-rw-r--r--Content/Encoder/Base64.php49
-rw-r--r--Content/Encoder/Encoder.php10
-rw-r--r--Content/Encoder/QuotedPrintable.php20
-rw-r--r--Test/Unit/Content/Encoder/Base64.php25
-rw-r--r--Test/Unit/Content/Encoder/QuotedPrintable.php23
5 files changed, 82 insertions, 45 deletions
diff --git a/Content/Encoder/Base64.php b/Content/Encoder/Base64.php
index 1e5deed..b1e4877 100644
--- a/Content/Encoder/Base64.php
+++ b/Content/Encoder/Base64.php
@@ -36,11 +36,13 @@
namespace Hoa\Mail\Content\Encoder;
+use Hoa\Mail;
+
/**
* Class \Hoa\Mail\Content\Encoder\Base64.
*
- * Encode and decode a string as described in the RFC4648 and RFC2045
- * Section 6.8.
+ * Encode and decode a string as described in the RFC4648, RFC2045
+ * Section 6.8 and RFC2047.
*
* @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
@@ -50,34 +52,41 @@ class Base64 implements Encoder
/**
* Encode into base64.
*
- * @param string $string String to encode.
+ * @param string $string String to encode.
+ * @param bool $isHeaderValue Whether the string is a header value.
* @return string
*/
- public static function encode($string)
+ public static function encode($string, $isHeaderValue = false)
{
- return trim(
- chunk_split(
- base64_encode($string),
- 76,
- CRLF
- )
- );
+ $pre = null;
+ $post = null;
+
+ if (true === $isHeaderValue) {
+ $pre = '=?utf-8?B?';
+ $post = '?=';
+ }
+
+ return
+ $pre .
+ trim(
+ chunk_split(
+ base64_encode($string),
+ 76,
+ CRLF
+ )
+ ) .
+ $post;
}
/**
* Decode from base64.
*
- * @param string $string String to decode.
+ * @param string $string String to decode.
+ * @param bool $isHeaderValue Whether the string is a header value.
* @return string
*/
- public static function decode($string)
+ public static function decode($string, $isHeaderValue = false)
{
- return base64_decode(
- str_replace(
- CRLF,
- '',
- $string
- )
- );
+ throw new Mail\Exception('Not implemented.');
}
}
diff --git a/Content/Encoder/Encoder.php b/Content/Encoder/Encoder.php
index 8d63148..e3e4a6b 100644
--- a/Content/Encoder/Encoder.php
+++ b/Content/Encoder/Encoder.php
@@ -51,18 +51,20 @@ interface Encoder
/**
* Encode a string.
*
- * @param string $string String to encode.
+ * @param string $string String to encode.
+ * @param bool $isHeaderValue Whether the string is a header value.
* @return string
*/
- public static function encode($string);
+ public static function encode($string, $isHeaderValue = false);
/**
* Decode a string.
*
- * @param string $string String to decode.
+ * @param string $string String to decode.
+ * @param bool $isHeaderValue Whether the string is a header value.
* @return string
*/
- public static function decode($string);
+ public static function decode($string, $isHeaderValue = false);
}
/**
diff --git a/Content/Encoder/QuotedPrintable.php b/Content/Encoder/QuotedPrintable.php
index 34de25c..a0c5cab 100644
--- a/Content/Encoder/QuotedPrintable.php
+++ b/Content/Encoder/QuotedPrintable.php
@@ -52,11 +52,20 @@ class QuotedPrintable implements Encoder
/**
* Encode into quoted-printable format.
*
- * @param string $string String to encode.
+ * @param string $string String to encode.
+ * @param bool $isHeaderValue Whether the string is a header value.
* @return string
*/
- public static function encode($string)
+ public static function encode($string, $isHeaderValue = false)
{
+ $pre = null;
+ $post = null;
+
+ if (true === $isHeaderValue) {
+ $pre = '=?utf-8?Q?';
+ $post = '?=';
+ }
+
// RFC2045, Section 6.7, rules 1 and 2.
$string = preg_replace_callback(
// 0x00 to 0xff minus:
@@ -103,16 +112,17 @@ class QuotedPrintable implements Encoder
false
);
- return $string;
+ return $pre . $string . $post;
}
/**
* Decode from quoted-printable format.
*
- * @param string $string String to decode.
+ * @param string $string String to decode.
+ * @param bool $isHeaderValue Whether the string is a header value.
* @return string
*/
- public static function decode($string)
+ public static function decode($string, $isHeaderValue = false)
{
throw new Mail\Exception('Not implemented.');
}
diff --git a/Test/Unit/Content/Encoder/Base64.php b/Test/Unit/Content/Encoder/Base64.php
index d1cf891..625fb72 100644
--- a/Test/Unit/Content/Encoder/Base64.php
+++ b/Test/Unit/Content/Encoder/Base64.php
@@ -49,7 +49,7 @@ use Hoa\Test;
*/
class Base64 extends Test\Unit\Suite
{
- public function case_basic_encode_decode()
+ public function case_basic_encode()
{
$this
->given(
@@ -59,15 +59,10 @@ class Base64 extends Test\Unit\Suite
->when($result = SUT::encode($decoded))
->then
->string($result)
- ->isEqualTo($encoded)
-
- ->when($result = SUT::decode($result))
- ->then
- ->string($result)
- ->isEqualTo($decoded);
+ ->isEqualTo($encoded);
}
- public function case_encode_decode_76_columns_max()
+ public function case_long_encode()
{
$this
->given(
@@ -80,11 +75,19 @@ class Base64 extends Test\Unit\Suite
->when($result = SUT::encode($decoded))
->then
->string($result)
- ->isEqualTo($encoded)
+ ->isEqualTo($encoded);
+ }
- ->when($result = SUT::decode($result))
+ public function case_encode_rfc2047_sections_4_and_5()
+ {
+ $this
+ ->given(
+ $decoded = 'foobar',
+ $encoded = '=?utf-8?B?Zm9vYmFy?='
+ )
+ ->when($result = SUT::encode($decoded, true))
->then
->string($result)
- ->isEqualTo($decoded);
+ ->isEqualTo($encoded);
}
}
diff --git a/Test/Unit/Content/Encoder/QuotedPrintable.php b/Test/Unit/Content/Encoder/QuotedPrintable.php
index 9863a0b..b9f99e4 100644
--- a/Test/Unit/Content/Encoder/QuotedPrintable.php
+++ b/Test/Unit/Content/Encoder/QuotedPrintable.php
@@ -49,7 +49,7 @@ use Hoa\Test;
*/
class QuotedPrintable extends Test\Unit\Suite
{
- public function case_rfc2045_section_6_7_rule1()
+ public function case_encode_rfc2045_section_6_7_rule1()
{
$this
->given($datum = 'abc')
@@ -78,7 +78,7 @@ class QuotedPrintable extends Test\Unit\Suite
->isEqualTo('abc' . CRLF . 'def');
}
- public function case_rfc2045_section6_7_rule2()
+ public function case_encode_rfc2045_section6_7_rule2()
{
$this
->given(
@@ -99,7 +99,7 @@ class QuotedPrintable extends Test\Unit\Suite
});
}
- public function case_rfc2045_section6_7_rule3()
+ public function case_encode_rfc2045_section6_7_rule3()
{
$this
->given($tab = 'abc' . "\t\t" . 'def')
@@ -127,7 +127,7 @@ class QuotedPrintable extends Test\Unit\Suite
->isEqualTo('abc =20' . CRLF);
}
- public function case_rfc2045_section_6_7_rule4()
+ public function case_encode_rfc2045_section_6_7_rule4()
{
$this
->given($datum = 'abc' . CRLF . 'def' . CRLF . 'ghi' . CRLF)
@@ -137,7 +137,7 @@ class QuotedPrintable extends Test\Unit\Suite
->isEqualTo($datum);
}
- public function case_rfc2045_section_6_7_rule5()
+ public function case_encode_rfc2045_section_6_7_rule5()
{
$this
->given($datum = 'abc')
@@ -160,4 +160,17 @@ class QuotedPrintable extends Test\Unit\Suite
'def ghi jkl mno pqr stu vwx yz' . CRLF
);
}
+
+ public function case_encode_rfc2047_sections_4_and_5()
+ {
+ $this
+ ->given(
+ $decoded = '😄!',
+ $encoded = '=?utf-8?Q?=F0=9F=98=84!?='
+ )
+ ->when($result = SUT::encode($decoded, true))
+ ->then
+ ->string($result)
+ ->isEqualTo($encoded);
+ }
}