|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of SwiftMailer. |
|
5
|
|
|
* (c) 2004-2009 Chris Corbyn |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Handles Base64 (B) Header Encoding in Swift Mailer. |
|
13
|
|
|
* |
|
14
|
|
|
* @author Chris Corbyn |
|
15
|
|
|
*/ |
|
16
|
|
|
class Swift_Mime_HeaderEncoder_Base64HeaderEncoder extends Swift_Encoder_Base64Encoder implements Swift_Mime_HeaderEncoder |
|
|
|
|
|
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Get the name of this encoding scheme. |
|
20
|
|
|
* Returns the string 'B'. |
|
21
|
|
|
* |
|
22
|
|
|
* @return string |
|
23
|
|
|
*/ |
|
24
|
2 |
|
public function getName() |
|
25
|
|
|
{ |
|
26
|
2 |
|
return 'B'; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Takes an unencoded string and produces a Base64 encoded string from it. |
|
31
|
|
|
* |
|
32
|
|
|
* If the charset is iso-2022-jp, it uses mb_encode_mimeheader instead of |
|
33
|
|
|
* default encodeString, otherwise pass to the parent method. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $string string to encode |
|
36
|
|
|
* @param int $firstLineOffset |
|
37
|
|
|
* @param int $maxLineLength optional, 0 indicates the default of 76 bytes |
|
38
|
|
|
* @param string $charset |
|
39
|
|
|
* |
|
40
|
|
|
* @return string |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public function encodeString($string, $firstLineOffset = 0, $maxLineLength = 0, $charset = 'utf-8') |
|
43
|
|
|
{ |
|
44
|
1 |
|
if (Swift::strtolowerWithStaticCache($charset) === 'iso-2022-jp') { |
|
45
|
1 |
|
$old = mb_internal_encoding(); |
|
46
|
1 |
|
mb_internal_encoding('utf-8'); |
|
47
|
1 |
|
$newstring = mb_encode_mimeheader($string, $charset, $this->getName(), "\r\n"); |
|
48
|
1 |
|
mb_internal_encoding($old); |
|
49
|
|
|
|
|
50
|
1 |
|
return $newstring; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// safety measure copy-pasted from parent method |
|
54
|
|
|
if (0 >= $maxLineLength || 76 < $maxLineLength) { |
|
55
|
|
|
$maxLineLength = 76; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$cursorPosition = 0; |
|
59
|
|
|
$encoded = ''; |
|
60
|
|
|
while ($cursorPosition < strlen($string)) { |
|
61
|
|
|
$maxChunkLength = $this->maxChunkLength($firstLineOffset, $maxLineLength); |
|
62
|
|
|
if ($cursorPosition > 0 || $firstLineOffset > $maxChunkLength) { |
|
63
|
|
|
$encoded .= "\r\n"; |
|
64
|
|
|
$maxChunkLength = $this->maxChunkLength(0, $maxLineLength); |
|
65
|
|
|
} |
|
66
|
|
|
$chunk = mb_strcut($string, $cursorPosition, $maxChunkLength); |
|
67
|
|
|
$encoded .= base64_encode($chunk); |
|
68
|
|
|
$cursorPosition += strlen($chunk); |
|
69
|
|
|
} |
|
70
|
|
|
return $encoded; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Returns maximum number of bytes that can fit in a line with given |
|
75
|
|
|
* offset and maximum length if encoded with base64 |
|
76
|
|
|
* |
|
77
|
|
|
* @param int $firstLineOffset |
|
78
|
|
|
* @param int $maxLineLength |
|
79
|
|
|
* |
|
80
|
|
|
* @return int |
|
81
|
|
|
*/ |
|
82
|
|
|
private function maxChunkLength($firstLineOffset, $maxLineLength) |
|
83
|
|
|
{ |
|
84
|
|
|
return floor(($maxLineLength - $firstLineOffset) / 4) * 3; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.