123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
-
-
-
-
-
- class Swift_Mime_ContentEncoder_PlainContentEncoder
- implements Swift_Mime_ContentEncoder
- {
-
-
-
- private $_name;
-
-
-
- private $_canonical;
-
-
-
- public function __construct($name, $canonical = false)
- {
- $this->_name = $name;
- $this->_canonical = $canonical;
- }
-
-
-
- public function encodeString($string, $firstLineOffset = 0,
- $maxLineLength = 0)
- {
- if ($this->_canonical)
- {
- $string = $this->_canonicalize($string);
- }
- return $this->_safeWordWrap($string, $maxLineLength, "\r\n");
- }
-
-
-
- public function encodeByteStream(
- Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0,
- $maxLineLength = 0)
- {
- $leftOver = '';
- while (false !== $bytes = $os->read(8192))
- {
- $toencode = $leftOver . $bytes;
- if ($this->_canonical)
- {
- $toencode = $this->_canonicalize($toencode);
- }
- $wrapped = $this->_safeWordWrap($toencode, $maxLineLength, "\r\n");
- $lastLinePos = strrpos($wrapped, "\r\n");
- $leftOver = substr($wrapped, $lastLinePos);
- $wrapped = substr($wrapped, 0, $lastLinePos);
-
- $is->write($wrapped);
- }
- if (strlen($leftOver))
- {
- $is->write($leftOver);
- }
- }
-
-
-
- public function getName()
- {
- return $this->_name;
- }
-
-
-
- public function charsetChanged($charset)
- {
- }
-
-
-
-
-
- private function _safeWordwrap($string, $length = 75, $le = "\r\n")
- {
- if (0 >= $length)
- {
- return $string;
- }
-
- $originalLines = explode($le, $string);
-
- $lines = array();
- $lineCount = 0;
-
- foreach ($originalLines as $originalLine)
- {
- $lines[] = '';
- $currentLine =& $lines[$lineCount++];
-
-
- $chunks = preg_split('/(?<=\s)/', $originalLine);
-
- foreach ($chunks as $chunk)
- {
- if (0 != strlen($currentLine)
- && strlen($currentLine . $chunk) > $length)
- {
- $lines[] = '';
- $currentLine =& $lines[$lineCount++];
- }
- $currentLine .= $chunk;
- }
- }
-
- return implode("\r\n", $lines);
- }
-
-
-
- private function _canonicalize($string)
- {
- return str_replace(
- array("\r\n", "\r", "\n"),
- array("\n", "\n", "\r\n"),
- $string
- );
- }
-
- }
|