PlainContentEncoder.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 binary/7/8-bit Transfer Encoding in Swift Mailer.
  11. * @package Swift
  12. * @subpackage Mime
  13. * @author Chris Corbyn
  14. */
  15. class Swift_Mime_ContentEncoder_PlainContentEncoder
  16. implements Swift_Mime_ContentEncoder
  17. {
  18. /**
  19. * The name of this encoding scheme (probably 7bit or 8bit).
  20. * @var string
  21. * @access private
  22. */
  23. private $_name;
  24. /**
  25. * True if canonical transformations should be done.
  26. * @var boolean
  27. * @access private
  28. */
  29. private $_canonical;
  30. /**
  31. * Creates a new PlainContentEncoder with $name (probably 7bit or 8bit).
  32. * @param string $name
  33. * @param boolean $canonical If canonicalization transformation should be done.
  34. */
  35. public function __construct($name, $canonical = false)
  36. {
  37. $this->_name = $name;
  38. $this->_canonical = $canonical;
  39. }
  40. /**
  41. * Encode a given string to produce an encoded string.
  42. * @param string $string
  43. * @param int $firstLineOffset, ignored
  44. * @param int $maxLineLength - 0 means no wrapping will occur
  45. * @return string
  46. */
  47. public function encodeString($string, $firstLineOffset = 0,
  48. $maxLineLength = 0)
  49. {
  50. if ($this->_canonical)
  51. {
  52. $string = $this->_canonicalize($string);
  53. }
  54. return $this->_safeWordWrap($string, $maxLineLength, "\r\n");
  55. }
  56. /**
  57. * Encode stream $in to stream $out.
  58. * @param Swift_OutputByteStream $in
  59. * @param Swift_InputByteStream $out
  60. * @param int $firstLineOffset, ignored
  61. * @param int $maxLineLength, optional, 0 means no wrapping will occur
  62. */
  63. public function encodeByteStream(
  64. Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0,
  65. $maxLineLength = 0)
  66. {
  67. $leftOver = '';
  68. while (false !== $bytes = $os->read(8192))
  69. {
  70. $toencode = $leftOver . $bytes;
  71. if ($this->_canonical)
  72. {
  73. $toencode = $this->_canonicalize($toencode);
  74. }
  75. $wrapped = $this->_safeWordWrap($toencode, $maxLineLength, "\r\n");
  76. $lastLinePos = strrpos($wrapped, "\r\n");
  77. $leftOver = substr($wrapped, $lastLinePos);
  78. $wrapped = substr($wrapped, 0, $lastLinePos);
  79. $is->write($wrapped);
  80. }
  81. if (strlen($leftOver))
  82. {
  83. $is->write($leftOver);
  84. }
  85. }
  86. /**
  87. * Get the name of this encoding scheme.
  88. * @return string
  89. */
  90. public function getName()
  91. {
  92. return $this->_name;
  93. }
  94. /**
  95. * Not used.
  96. */
  97. public function charsetChanged($charset)
  98. {
  99. }
  100. // -- Private methods
  101. /**
  102. * A safer (but weaker) wordwrap for unicode.
  103. * @param string $string
  104. * @param int $length
  105. * @param string $le
  106. * @return string
  107. * @access private
  108. */
  109. private function _safeWordwrap($string, $length = 75, $le = "\r\n")
  110. {
  111. if (0 >= $length)
  112. {
  113. return $string;
  114. }
  115. $originalLines = explode($le, $string);
  116. $lines = array();
  117. $lineCount = 0;
  118. foreach ($originalLines as $originalLine)
  119. {
  120. $lines[] = '';
  121. $currentLine =& $lines[$lineCount++];
  122. //$chunks = preg_split('/(?<=[\ \t,\.!\?\-&\+\/])/', $originalLine);
  123. $chunks = preg_split('/(?<=\s)/', $originalLine);
  124. foreach ($chunks as $chunk)
  125. {
  126. if (0 != strlen($currentLine)
  127. && strlen($currentLine . $chunk) > $length)
  128. {
  129. $lines[] = '';
  130. $currentLine =& $lines[$lineCount++];
  131. }
  132. $currentLine .= $chunk;
  133. }
  134. }
  135. return implode("\r\n", $lines);
  136. }
  137. /**
  138. * Canonicalize string input (fix CRLF).
  139. * @param string $string
  140. * @return string
  141. * @access private
  142. */
  143. private function _canonicalize($string)
  144. {
  145. return str_replace(
  146. array("\r\n", "\r", "\n"),
  147. array("\n", "\n", "\r\n"),
  148. $string
  149. );
  150. }
  151. }