/framework/vendor/swift/lib/classes/Swift/Mime/ContentEncoder/PlainContentEncoder.php

http://zoop.googlecode.com/ · PHP · 175 lines · 86 code · 21 blank · 68 comment · 8 complexity · 957bdcd0eac25cae54757a8121102e7e MD5 · raw file

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