Added feature #15998: Create a new API to send mails based on SwiftMailer to replace...
[Packages/TYPO3.CMS.git] / typo3 / contrib / swiftmailer / classes / Swift / Mime / HeaderEncoder / QpHeaderEncoder.php
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 require_once dirname(__FILE__) . '/../HeaderEncoder.php';
12 require_once dirname(__FILE__) . '/../../Encoder/QpEncoder.php';
13 require_once dirname(__FILE__) . '/../../CharacterStream.php';
14
15 /**
16 * Handles Quoted Printable (Q) Header Encoding in Swift Mailer.
17 * @package Swift
18 * @subpackage Mime
19 * @author Chris Corbyn
20 */
21 class Swift_Mime_HeaderEncoder_QpHeaderEncoder extends Swift_Encoder_QpEncoder
22 implements Swift_Mime_HeaderEncoder
23 {
24
25 private static $_headerSafeMap = array();
26
27 /**
28 * Creates a new QpHeaderEncoder for the given CharacterStream.
29 * @param Swift_CharacterStream $charStream to use for reading characters
30 */
31 public function __construct(Swift_CharacterStream $charStream)
32 {
33 parent::__construct($charStream);
34 if (empty(self::$_headerSafeMap))
35 {
36 foreach (array_merge(
37 range(0x61, 0x7A), range(0x41, 0x5A),
38 range(0x30, 0x39), array(0x20, 0x21, 0x2A, 0x2B, 0x2D, 0x2F)
39 ) as $byte)
40 {
41 self::$_headerSafeMap[$byte] = chr($byte);
42 }
43 }
44 }
45
46 /**
47 * Get the name of this encoding scheme.
48 * Returns the string 'Q'.
49 * @return string
50 */
51 public function getName()
52 {
53 return 'Q';
54 }
55
56 /**
57 * Takes an unencoded string and produces a Q encoded string from it.
58 * @param string $string to encode
59 * @param int $firstLineOffset, optional
60 * @param int $maxLineLength, optional, 0 indicates the default of 76 chars
61 * @return string
62 */
63 public function encodeString($string, $firstLineOffset = 0,
64 $maxLineLength = 0)
65 {
66 return str_replace(array(' ', '=20', "=\r\n"), array('_', '_', "\r\n"),
67 parent::encodeString($string, $firstLineOffset, $maxLineLength)
68 );
69 }
70
71 // -- Overridden points of extension
72
73 /**
74 * Encode the given byte array into a verbatim QP form.
75 * @param int[] $bytes
76 * @return string
77 * @access protected
78 */
79 protected function _encodeByteSequence(array $bytes, &$size)
80 {
81 $ret = '';
82 $size=0;
83 foreach ($bytes as $b)
84 {
85 if (isset(self::$_headerSafeMap[$b]))
86 {
87 $ret .= self::$_headerSafeMap[$b];
88 ++$size;
89 }
90 else
91 {
92 $ret .= self::$_qpMap[$b];
93 $size+=3;
94 }
95 }
96 return $ret;
97 }
98
99 }