QpContentEncoder.php 3.4KB

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