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 binary/7/8-bit Transfer Encoding in Swift Mailer.
13: * @package Swift
14: * @subpackage Mime
15: * @author Chris Corbyn
16: */
17: class Swift_Mime_ContentEncoder_PlainContentEncoder implements Swift_Mime_ContentEncoder
18: {
19: /**
20: * The name of this encoding scheme (probably 7bit or 8bit).
21: * @var string
22: * @access private
23: */
24: private $_name;
25:
26: /**
27: * True if canonical transformations should be done.
28: * @var boolean
29: * @access private
30: */
31: private $_canonical;
32:
33: /**
34: * Creates a new PlainContentEncoder with $name (probably 7bit or 8bit).
35: * @param string $name
36: * @param boolean $canonical If canonicalization transformation should be done.
37: */
38: public function __construct($name, $canonical = false)
39: {
40: $this->_name = $name;
41: $this->_canonical = $canonical;
42: }
43:
44: /**
45: * Encode a given string to produce an encoded string.
46: * @param string $string
47: * @param int $firstLineOffset, ignored
48: * @param int $maxLineLength - 0 means no wrapping will occur
49: * @return string
50: */
51: public function encodeString($string, $firstLineOffset = 0, $maxLineLength = 0)
52: {
53: if ($this->_canonical) {
54: $string = $this->_canonicalize($string);
55: }
56:
57: return $this->_safeWordWrap($string, $maxLineLength, "\r\n");
58: }
59:
60: /**
61: * Encode stream $in to stream $out.
62: * @param Swift_OutputByteStream $in
63: * @param Swift_InputByteStream $out
64: * @param int $firstLineOffset, ignored
65: * @param int $maxLineLength, optional, 0 means no wrapping will occur
66: */
67: public function encodeByteStream(Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0, $maxLineLength = 0)
68: {
69: $leftOver = '';
70: while (false !== $bytes = $os->read(8192)) {
71: $toencode = $leftOver . $bytes;
72: if ($this->_canonical) {
73: $toencode = $this->_canonicalize($toencode);
74: }
75: $wrapped = $this->_safeWordWrap($toencode, $maxLineLength, "\r\n");
76: $lastLinePos = strrpos($wrapped, "\r\n");
77: $leftOver = substr($wrapped, $lastLinePos);
78: $wrapped = substr($wrapped, 0, $lastLinePos);
79:
80: $is->write($wrapped);
81: }
82: if (strlen($leftOver)) {
83: $is->write($leftOver);
84: }
85: }
86:
87: /**
88: * Get the name of this encoding scheme.
89: * @return string
90: */
91: public function getName()
92: {
93: return $this->_name;
94: }
95:
96: /**
97: * Not used.
98: */
99: public function charsetChanged($charset)
100: {
101: }
102:
103: // -- Private methods
104:
105: /**
106: * A safer (but weaker) wordwrap for unicode.
107: * @param string $string
108: * @param int $length
109: * @param string $le
110: * @return string
111: * @access private
112: */
113: private function _safeWordwrap($string, $length = 75, $le = "\r\n")
114: {
115: if (0 >= $length) {
116: return $string;
117: }
118:
119: $originalLines = explode($le, $string);
120:
121: $lines = array();
122: $lineCount = 0;
123:
124: foreach ($originalLines as $originalLine) {
125: $lines[] = '';
126: $currentLine =& $lines[$lineCount++];
127:
128: //$chunks = preg_split('/(?<=[\ \t,\.!\?\-&\+\/])/', $originalLine);
129: $chunks = preg_split('/(?<=\s)/', $originalLine);
130:
131: foreach ($chunks as $chunk) {
132: if (0 != strlen($currentLine)
133: && strlen($currentLine . $chunk) > $length)
134: {
135: $lines[] = '';
136: $currentLine =& $lines[$lineCount++];
137: }
138: $currentLine .= $chunk;
139: }
140: }
141:
142: return implode("\r\n", $lines);
143: }
144:
145: /**
146: * Canonicalize string input (fix CRLF).
147: * @param string $string
148: * @return string
149: * @access private
150: */
151: private function _canonicalize($string)
152: {
153: return str_replace(
154: array("\r\n", "\r", "\n"),
155: array("\n", "\n", "\r\n"),
156: $string
157: );
158: }
159: }
160: