TYPO3  7.6
NativeQpContentEncoder.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 {
21  private $charset;
22 
26  public function __construct($charset = null)
27  {
28  $this->charset = $charset ? $charset : 'utf-8';
29  }
30 
36  public function charsetChanged($charset)
37  {
38  $this->charset = $charset;
39  }
40 
51  public function encodeByteStream(Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0, $maxLineLength = 0)
52  {
53  if ($this->charset !== 'utf-8') {
54  throw new RuntimeException(
55  sprintf('Charset "%s" not supported. NativeQpContentEncoder only supports "utf-8"', $this->charset));
56  }
57 
58  $string = '';
59 
60  while (false !== $bytes = $os->read(8192)) {
61  $string .= $bytes;
62  }
63 
64  $is->write($this->encodeString($string));
65  }
66 
72  public function getName()
73  {
74  return 'quoted-printable';
75  }
76 
88  public function encodeString($string, $firstLineOffset = 0, $maxLineLength = 0)
89  {
90  if ($this->charset !== 'utf-8') {
91  throw new RuntimeException(
92  sprintf('Charset "%s" not supported. NativeQpContentEncoder only supports "utf-8"', $this->charset));
93  }
94 
95  return $this->_standardize(quoted_printable_encode($string));
96  }
97 
105  protected function _standardize($string)
106  {
107  // transform CR or LF to CRLF
108  $string = preg_replace('~=0D(?!=0A)|(?<!=0D)=0A~', '=0D=0A', $string);
109  // transform =0D=0A to CRLF
110  $string = str_replace(array("\t=0D=0A", ' =0D=0A', '=0D=0A'), array("=09\r\n", "=20\r\n", "\r\n"), $string);
111 
112  switch ($end = ord(substr($string, -1))) {
113  case 0x09:
114  $string = substr_replace($string, '=09', -1);
115  break;
116  case 0x20:
117  $string = substr_replace($string, '=20', -1);
118  break;
119  }
120 
121  return $string;
122  }
123 }