Merge branch 'maint/7.0'
[ninja.git] / application / vendor / swiftmailer / classes / Swift / Mime / ContentEncoder / PlainContentEncoder.php
blob4a725d856129d0e29d35fdee3f01dc531a878545
1 <?php
3 /*
4 * This file is part of SwiftMailer.
5 * (c) 2004-2009 Chris Corbyn
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
11 //@require 'Swift/Mime/ContentEncoder.php';
12 //@require 'Swift/InputByteStream.php';
13 //@require 'Swift/OutputByteStream.php';
15 /**
16 * Handles binary/7/8-bit Transfer Encoding in Swift Mailer.
17 * @package Swift
18 * @subpackage Mime
19 * @author Chris Corbyn
21 class Swift_Mime_ContentEncoder_PlainContentEncoder
22 implements Swift_Mime_ContentEncoder
25 /**
26 * The name of this encoding scheme (probably 7bit or 8bit).
27 * @var string
28 * @access private
30 private $_name;
32 /**
33 * True if canonical transformations should be done.
34 * @var boolean
35 * @access private
37 private $_canonical;
39 /**
40 * Creates a new PlainContentEncoder with $name (probably 7bit or 8bit).
41 * @param string $name
42 * @param boolean $canonical If canonicalization transformation should be done.
44 public function __construct($name, $canonical = false)
46 $this->_name = $name;
47 $this->_canonical = $canonical;
50 /**
51 * Encode a given string to produce an encoded string.
52 * @param string $string
53 * @param int $firstLineOffset, ignored
54 * @param int $maxLineLength - 0 means no wrapping will occur
55 * @return string
57 public function encodeString($string, $firstLineOffset = 0,
58 $maxLineLength = 0)
60 if ($this->_canonical)
62 $string = $this->_canonicalize($string);
64 return $this->_safeWordWrap($string, $maxLineLength, "\r\n");
67 /**
68 * Encode stream $in to stream $out.
69 * @param Swift_OutputByteStream $in
70 * @param Swift_InputByteStream $out
71 * @param int $firstLineOffset, ignored
72 * @param int $maxLineLength, optional, 0 means no wrapping will occur
74 public function encodeByteStream(
75 Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0,
76 $maxLineLength = 0)
78 $leftOver = '';
79 while (false !== $bytes = $os->read(8192))
81 $toencode = $leftOver . $bytes;
82 if ($this->_canonical)
84 $toencode = $this->_canonicalize($toencode);
86 $wrapped = $this->_safeWordWrap($toencode, $maxLineLength, "\r\n");
87 $lastLinePos = strrpos($wrapped, "\r\n");
88 $leftOver = substr($wrapped, $lastLinePos);
89 $wrapped = substr($wrapped, 0, $lastLinePos);
91 $is->write($wrapped);
93 if (strlen($leftOver))
95 $is->write($leftOver);
99 /**
100 * Get the name of this encoding scheme.
101 * @return string
103 public function getName()
105 return $this->_name;
109 * Not used.
111 public function charsetChanged($charset)
115 // -- Private methods
118 * A safer (but weaker) wordwrap for unicode.
119 * @param string $string
120 * @param int $length
121 * @param string $le
122 * @return string
123 * @access private
125 private function _safeWordwrap($string, $length = 75, $le = "\r\n")
127 if (0 >= $length)
129 return $string;
132 $originalLines = explode($le, $string);
134 $lines = array();
135 $lineCount = 0;
137 foreach ($originalLines as $originalLine)
139 $lines[] = '';
140 $currentLine =& $lines[$lineCount++];
142 //$chunks = preg_split('/(?<=[\ \t,\.!\?\-&\+\/])/', $originalLine);
143 $chunks = preg_split('/(?<=\s)/', $originalLine);
145 foreach ($chunks as $chunk)
147 if (0 != strlen($currentLine)
148 && strlen($currentLine . $chunk) > $length)
150 $lines[] = '';
151 $currentLine =& $lines[$lineCount++];
153 $currentLine .= $chunk;
157 return implode("\r\n", $lines);
161 * Canonicalize string input (fix CRLF).
162 * @param string $string
163 * @return string
164 * @access private
166 private function _canonicalize($string)
168 return str_replace(
169 array("\r\n", "\r", "\n"),
170 array("\n", "\n", "\r\n"),
171 $string