PlainContentEncoder.php 4.2KB

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