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 Base 64 Transfer Encoding in Swift Mailer. |
13
|
|
|
* |
14
|
|
|
* @author Chris Corbyn |
15
|
|
|
*/ |
16
|
|
|
class Swift_Mime_ContentEncoder_Base64ContentEncoder extends Swift_Encoder_Base64Encoder implements Swift_Mime_ContentEncoder |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Encode stream $in to stream $out. |
20
|
|
|
* |
21
|
|
|
* @param Swift_OutputByteStream $os |
22
|
|
|
* @param Swift_InputByteStream $is |
23
|
|
|
* @param int $firstLineOffset |
24
|
|
|
* @param int $maxLineLength, optional, 0 indicates the default of 76 bytes |
|
|
|
|
25
|
|
|
*/ |
26
|
15 |
|
public function encodeByteStream(Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0, $maxLineLength = 0) |
27
|
|
|
{ |
28
|
15 |
|
if (0 >= $maxLineLength || 76 < $maxLineLength) { |
29
|
14 |
|
$maxLineLength = 76; |
30
|
14 |
|
} |
31
|
|
|
|
32
|
15 |
|
$remainder = 0; |
33
|
15 |
|
$base64ReadBufferRemainderBytes = null; |
34
|
|
|
|
35
|
|
|
// To reduce memory usage, the output buffer is streamed to the input buffer like so: |
36
|
|
|
// Output Stream => base64encode => wrap line length => Input Stream |
37
|
|
|
// HOWEVER it's important to note that base64_encode() should only be passed whole triplets of data (except for the final chunk of data) |
38
|
|
|
// otherwise it will assume the input data has *ended* and it will incorrectly pad/terminate the base64 data mid-stream. |
39
|
|
|
// We use $base64ReadBufferRemainderBytes to carry over 1-2 "remainder" bytes from the each chunk from OutputStream and pre-pend those onto the |
40
|
|
|
// chunk of bytes read in the next iteration. |
41
|
|
|
// When the OutputStream is empty, we must flush any remainder bytes. |
42
|
15 |
|
while (true) { |
43
|
15 |
|
$readBytes = $os->read(8192); |
44
|
15 |
|
$atEOF = ($readBytes === false); |
45
|
|
|
|
46
|
15 |
|
if ($atEOF) { |
47
|
15 |
|
$streamTheseBytes = $base64ReadBufferRemainderBytes; |
48
|
15 |
|
} else { |
49
|
15 |
|
$streamTheseBytes = $base64ReadBufferRemainderBytes . $readBytes; |
50
|
|
|
} |
51
|
|
|
|
52
|
15 |
|
$base64ReadBufferRemainderBytes = null; |
53
|
15 |
|
$bytesLength = strlen($streamTheseBytes); |
54
|
|
|
|
55
|
15 |
|
if ($bytesLength === 0) { // no data left to encode |
56
|
9 |
|
break; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// if we're not on the last block of the output stream, make sure $streamTheseBytes ends with a complete triplet of data |
60
|
|
|
// and carry over remainder 1-2 bytes to the next loop iteration |
61
|
15 |
|
if (!$atEOF) { |
62
|
15 |
|
$excessBytes = $bytesLength % 3; |
63
|
15 |
|
if ($excessBytes !== 0) { |
64
|
9 |
|
$base64ReadBufferRemainderBytes = substr($streamTheseBytes, -$excessBytes); |
65
|
9 |
|
$streamTheseBytes = substr($streamTheseBytes, 0, $bytesLength - $excessBytes); |
66
|
9 |
|
} |
67
|
15 |
|
} |
68
|
|
|
|
69
|
15 |
|
$encoded = base64_encode($streamTheseBytes); |
70
|
15 |
|
$encodedTransformed = ''; |
71
|
15 |
|
$thisMaxLineLength = $maxLineLength - $remainder - $firstLineOffset; |
72
|
|
|
|
73
|
15 |
|
while ($thisMaxLineLength < strlen($encoded)) { |
74
|
11 |
|
$encodedTransformed .= substr($encoded, 0, $thisMaxLineLength) . "\r\n"; |
75
|
11 |
|
$firstLineOffset = 0; |
76
|
11 |
|
$encoded = substr($encoded, $thisMaxLineLength); |
77
|
11 |
|
$thisMaxLineLength = $maxLineLength; |
78
|
11 |
|
$remainder = 0; |
79
|
11 |
|
} |
80
|
|
|
|
81
|
15 |
|
if (0 < $remainingLength = strlen($encoded)) { |
82
|
15 |
|
$remainder += $remainingLength; |
83
|
15 |
|
$encodedTransformed .= $encoded; |
84
|
15 |
|
} |
85
|
|
|
|
86
|
15 |
|
$is->write($encodedTransformed); |
87
|
|
|
|
88
|
15 |
|
if ($atEOF) { |
89
|
9 |
|
break; |
90
|
|
|
} |
91
|
15 |
|
} |
92
|
15 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get the name of this encoding scheme. |
96
|
|
|
* Returns the string 'base64'. |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
57 |
|
public function getName() |
101
|
|
|
{ |
102
|
57 |
|
return 'base64'; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
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.