TYPO3  7.6
PlainContentEncoder.php
Go to the documentation of this file.
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 
17 {
23  private $_name;
24 
30  private $_canonical;
31 
38  public function __construct($name, $canonical = false)
39  {
40  $this->_name = $name;
41  $this->_canonical = $canonical;
42  }
43 
53  public function encodeString($string, $firstLineOffset = 0, $maxLineLength = 0)
54  {
55  if ($this->_canonical) {
56  $string = $this->_canonicalize($string);
57  }
58 
59  return $this->_safeWordWrap($string, $maxLineLength, "\r\n");
60  }
61 
70  public function encodeByteStream(Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0, $maxLineLength = 0)
71  {
72  $leftOver = '';
73  while (false !== $bytes = $os->read(8192)) {
74  $toencode = $leftOver.$bytes;
75  if ($this->_canonical) {
76  $toencode = $this->_canonicalize($toencode);
77  }
78  $wrapped = $this->_safeWordWrap($toencode, $maxLineLength, "\r\n");
79  $lastLinePos = strrpos($wrapped, "\r\n");
80  $leftOver = substr($wrapped, $lastLinePos);
81  $wrapped = substr($wrapped, 0, $lastLinePos);
82 
83  $is->write($wrapped);
84  }
85  if (strlen($leftOver)) {
86  $is->write($leftOver);
87  }
88  }
89 
95  public function getName()
96  {
97  return $this->_name;
98  }
99 
103  public function charsetChanged($charset)
104  {
105  }
106 
116  private function _safeWordwrap($string, $length = 75, $le = "\r\n")
117  {
118  if (0 >= $length) {
119  return $string;
120  }
121 
122  $originalLines = explode($le, $string);
123 
124  $lines = array();
125  $lineCount = 0;
126 
127  foreach ($originalLines as $originalLine) {
128  $lines[] = '';
129  $currentLine = &$lines[$lineCount++];
130 
131  //$chunks = preg_split('/(?<=[\ \t,\.!\?\-&\+\/])/', $originalLine);
132  $chunks = preg_split('/(?<=\s)/', $originalLine);
133 
134  foreach ($chunks as $chunk) {
135  if (0 != strlen($currentLine)
136  && strlen($currentLine.$chunk) > $length) {
137  $lines[] = '';
138  $currentLine = &$lines[$lineCount++];
139  }
140  $currentLine .= $chunk;
141  }
142  }
143 
144  return implode("\r\n", $lines);
145  }
146 
154  private function _canonicalize($string)
155  {
156  return str_replace(
157  array("\r\n", "\r", "\n"),
158  array("\n", "\n", "\r\n"),
159  $string
160  );
161  }
162 }