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 'Swift/Mime/ContentEncoder.php';
12//@require 'Swift/Encoder/QpEncoder.php';
13//@require 'Swift/InputByteStrean.php';
14//@require 'Swift/OutputByteStream.php';
15//@require 'Swift/CharacterStream.php';
16
17/**
18 * Handles Quoted Printable (QP) Transfer Encoding in Swift Mailer.
19 * @package Swift
20 * @subpackage Mime
21 * @author Chris Corbyn
22 */
23class Swift_Mime_ContentEncoder_QpContentEncoder extends Swift_Encoder_QpEncoder
24  implements Swift_Mime_ContentEncoder
25{
26
27  /**
28   * Creates a new QpContentEncoder for the given CharacterStream.
29   * @param Swift_CharacterStream $charStream to use for reading characters
30   * @param Swift_StreamFilter $filter if canonicalization should occur
31   */
32  public function __construct(Swift_CharacterStream $charStream,
33    Swift_StreamFilter $filter = null)
34  {
35    parent::__construct($charStream, $filter);
36  }
37
38  /**
39   * Encode stream $in to stream $out.
40   * QP encoded strings have a maximum line length of 76 characters.
41   * If the first line needs to be shorter, indicate the difference with
42   * $firstLineOffset.
43   * @param Swift_OutputByteStream $os output stream
44   * @param Swift_InputByteStream $is input stream
45   * @param int $firstLineOffset
46   * @param int $maxLineLength
47   */
48  public function encodeByteStream(
49    Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0,
50    $maxLineLength = 0)
51  {
52    if ($maxLineLength > 76 || $maxLineLength <= 0)
53    {
54      $maxLineLength = 76;
55    }
56
57    $thisLineLength = $maxLineLength - $firstLineOffset;
58
59    $this->_charStream->flushContents();
60    $this->_charStream->importByteStream($os);
61
62    $currentLine = '';
63    $prepend = '';
64    $size=$lineLen=0;
65
66    while (false !== $bytes = $this->_nextSequence())
67    {
68      //If we're filtering the input
69      if (isset($this->_filter))
70      {
71        //If we can't filter because we need more bytes
72        while ($this->_filter->shouldBuffer($bytes))
73        {
74          //Then collect bytes into the buffer
75          if (false === $moreBytes = $this->_nextSequence(1))
76          {
77            break;
78          }
79
80          foreach ($moreBytes as $b)
81          {
82            $bytes[] = $b;
83          }
84        }
85        //And filter them
86        $bytes = $this->_filter->filter($bytes);
87      }
88
89      $enc = $this->_encodeByteSequence($bytes, $size);
90      if ($currentLine && $lineLen+$size >= $thisLineLength)
91      {
92        $is->write($prepend . $this->_standardize($currentLine));
93        $currentLine = '';
94        $prepend = "=\r\n";
95        $thisLineLength = $maxLineLength;
96        $lineLen=0;
97      }
98      $lineLen+=$size;
99      $currentLine .= $enc;
100    }
101    if (strlen($currentLine))
102    {
103      $is->write($prepend . $this->_standardize($currentLine));
104    }
105  }
106
107  /**
108   * Get the name of this encoding scheme.
109   * Returns the string 'quoted-printable'.
110   * @return string
111   */
112  public function getName()
113  {
114    return 'quoted-printable';
115  }
116
117}
118