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 Quoted Printable (Q) Header Encoding in Swift Mailer.
13 *
14 * @author Chris Corbyn
15 */
16class Swift_Mime_HeaderEncoder_QpHeaderEncoder extends Swift_Encoder_QpEncoder implements Swift_Mime_HeaderEncoder
17{
18    /**
19     * Creates a new QpHeaderEncoder for the given CharacterStream.
20     *
21     * @param Swift_CharacterStream $charStream to use for reading characters
22     */
23    public function __construct(Swift_CharacterStream $charStream)
24    {
25        parent::__construct($charStream);
26    }
27
28    protected function initSafeMap()
29    {
30        foreach (array_merge(
31            range(0x61, 0x7A), range(0x41, 0x5A),
32            range(0x30, 0x39), array(0x20, 0x21, 0x2A, 0x2B, 0x2D, 0x2F)
33        ) as $byte) {
34            $this->safeMap[$byte] = chr($byte);
35        }
36    }
37
38    /**
39     * Get the name of this encoding scheme.
40     *
41     * Returns the string 'Q'.
42     *
43     * @return string
44     */
45    public function getName()
46    {
47        return 'Q';
48    }
49
50    /**
51     * Takes an unencoded string and produces a QP encoded string from it.
52     *
53     * @param string $string          string to encode
54     * @param int    $firstLineOffset optional
55     * @param int    $maxLineLength   optional, 0 indicates the default of 76 chars
56     *
57     * @return string
58     */
59    public function encodeString($string, $firstLineOffset = 0, $maxLineLength = 0)
60    {
61        return str_replace(array(' ', '=20', "=\r\n"), array('_', '_', "\r\n"),
62            parent::encodeString($string, $firstLineOffset, $maxLineLength)
63        );
64    }
65}
66