QpContentEncoder.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Handles Quoted Printable (QP) Transfer Encoding in Swift Mailer.
  11. * @package Swift
  12. * @subpackage Mime
  13. * @author Chris Corbyn
  14. */
  15. class Swift_Mime_ContentEncoder_QpContentEncoder extends Swift_Encoder_QpEncoder implements Swift_Mime_ContentEncoder
  16. {
  17. protected $_dotEscape;
  18. /**
  19. * Creates a new QpContentEncoder for the given CharacterStream.
  20. * @param Swift_CharacterStream $charStream to use for reading characters
  21. * @param Swift_StreamFilter $filter if canonicalization should occur
  22. * @param boolean $dotEscape if dot stuffing workaround must be enabled
  23. */
  24. public function __construct(Swift_CharacterStream $charStream, Swift_StreamFilter $filter = null, $dotEscape=false)
  25. {
  26. $this->_dotEscape = $dotEscape;
  27. parent::__construct($charStream, $filter);
  28. }
  29. public function __sleep()
  30. {
  31. return array('_charStream', '_filter', '_dotEscape');
  32. }
  33. protected function getSafeMapShareId()
  34. {
  35. return get_class($this).($this->_dotEscape ? '.dotEscape' : '');
  36. }
  37. protected function initSafeMap()
  38. {
  39. parent::initSafeMap();
  40. if ($this->_dotEscape) {
  41. /* Encode . as =2e for buggy remote servers */
  42. unset($this->_safeMap[0x2e]);
  43. }
  44. }
  45. /**
  46. * Encode stream $in to stream $out.
  47. * QP encoded strings have a maximum line length of 76 characters.
  48. * If the first line needs to be shorter, indicate the difference with
  49. * $firstLineOffset.
  50. * @param Swift_OutputByteStream $os output stream
  51. * @param Swift_InputByteStream $is input stream
  52. * @param int $firstLineOffset
  53. * @param int $maxLineLength
  54. */
  55. public function encodeByteStream(Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0, $maxLineLength = 0)
  56. {
  57. if ($maxLineLength > 76 || $maxLineLength <= 0) {
  58. $maxLineLength = 76;
  59. }
  60. $thisLineLength = $maxLineLength - $firstLineOffset;
  61. $this->_charStream->flushContents();
  62. $this->_charStream->importByteStream($os);
  63. $currentLine = '';
  64. $prepend = '';
  65. $size=$lineLen=0;
  66. while (false !== $bytes = $this->_nextSequence()) {
  67. //If we're filtering the input
  68. if (isset($this->_filter)) {
  69. //If we can't filter because we need more bytes
  70. while ($this->_filter->shouldBuffer($bytes)) {
  71. //Then collect bytes into the buffer
  72. if (false === $moreBytes = $this->_nextSequence(1)) {
  73. break;
  74. }
  75. foreach ($moreBytes as $b) {
  76. $bytes[] = $b;
  77. }
  78. }
  79. //And filter them
  80. $bytes = $this->_filter->filter($bytes);
  81. }
  82. $enc = $this->_encodeByteSequence($bytes, $size);
  83. if ($currentLine && $lineLen+$size >= $thisLineLength) {
  84. $is->write($prepend . $this->_standardize($currentLine));
  85. $currentLine = '';
  86. $prepend = "=\r\n";
  87. $thisLineLength = $maxLineLength;
  88. $lineLen=0;
  89. }
  90. $lineLen+=$size;
  91. $currentLine .= $enc;
  92. }
  93. if (strlen($currentLine)) {
  94. $is->write($prepend . $this->_standardize($currentLine));
  95. }
  96. }
  97. /**
  98. * Get the name of this encoding scheme.
  99. * Returns the string 'quoted-printable'.
  100. * @return string
  101. */
  102. public function getName()
  103. {
  104. return 'quoted-printable';
  105. }
  106. }