PlainContentEncoder.php 4.2KB

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