/** * Encode stream $in to stream $out. * * @param Swift_OutputByteStream $os * @param Swift_InputByteStream $is * @param int $firstLineOffset * @param int $maxLineLength, optional, 0 indicates the default of 76 bytes */ public function encodeByteStream(Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0, $maxLineLength = 0) { if (0 >= $maxLineLength || 76 < $maxLineLength) { $maxLineLength = 76; } $remainder = 0; while (false !== ($bytes = $os->read(8190))) { $encoded = base64_encode($bytes); $encodedTransformed = ''; $thisMaxLineLength = $maxLineLength - $remainder - $firstLineOffset; while ($thisMaxLineLength < strlen($encoded)) { $encodedTransformed .= substr($encoded, 0, $thisMaxLineLength) . "\r\n"; $firstLineOffset = 0; $encoded = substr($encoded, $thisMaxLineLength); $thisMaxLineLength = $maxLineLength; $remainder = 0; } if (0 < ($remainingLength = strlen($encoded))) { $remainder += $remainingLength; $encodedTransformed .= $encoded; $encoded = null; } $is->write($encodedTransformed); } }
/** * Encode $in to $out. * * @param Swift_OutputByteStream $os to read from * @param Swift_InputByteStream $is to write to * @param integer $firstLineOffset * @param integer $maxLineLength 0 indicates the default length for this encoding */ public function encodeByteStream(Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0, $maxLineLength = 0) { $string = ''; while (false !== ($bytes = $os->read(8192))) { $string .= $bytes; } $is->write($this->encodeString($string)); }
/** * Encode stream $in to stream $out. * * @param Swift_OutputByteStream $os * @param Swift_InputByteStream $is * @param int $firstLineOffset * @param int $maxLineLength, optional, 0 indicates the default of 76 bytes */ public function encodeByteStream(Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0, $maxLineLength = 0) { if (0 >= $maxLineLength || 76 < $maxLineLength) { $maxLineLength = 76; } $remainder = 0; $base64ReadBufferRemainderBytes = null; // To reduce memory usage, the output buffer is streamed to the input buffer like so: // Output Stream => base64encode => wrap line length => Input Stream // HOWEVER it's important to note that base64_encode() should only be passed whole triplets of data (except for the final chunk of data) // otherwise it will assume the input data has *ended* and it will incorrectly pad/terminate the base64 data mid-stream. // We use $base64ReadBufferRemainderBytes to carry over 1-2 "remainder" bytes from the each chunk from OutputStream and pre-pend those onto the // chunk of bytes read in the next iteration. // When the OutputStream is empty, we must flush any remainder bytes. while (true) { $readBytes = $os->read(8192); $atEOF = $readBytes === false; if ($atEOF) { $streamTheseBytes = $base64ReadBufferRemainderBytes; } else { $streamTheseBytes = $base64ReadBufferRemainderBytes . $readBytes; } $base64ReadBufferRemainderBytes = null; $bytesLength = strlen($streamTheseBytes); if ($bytesLength === 0) { // no data left to encode break; } // if we're not on the last block of the ouput stream, make sure $streamTheseBytes ends with a complete triplet of data // and carry over remainder 1-2 bytes to the next loop iteration if (!$atEOF) { $excessBytes = $bytesLength % 3; if ($excessBytes !== 0) { $base64ReadBufferRemainderBytes = substr($streamTheseBytes, -$excessBytes); $streamTheseBytes = substr($streamTheseBytes, 0, $bytesLength - $excessBytes); } } $encoded = base64_encode($streamTheseBytes); $encodedTransformed = ''; $thisMaxLineLength = $maxLineLength - $remainder - $firstLineOffset; while ($thisMaxLineLength < strlen($encoded)) { $encodedTransformed .= substr($encoded, 0, $thisMaxLineLength) . "\r\n"; $firstLineOffset = 0; $encoded = substr($encoded, $thisMaxLineLength); $thisMaxLineLength = $maxLineLength; $remainder = 0; } if (0 < ($remainingLength = strlen($encoded))) { $remainder += $remainingLength; $encodedTransformed .= $encoded; $encoded = null; } $is->write($encodedTransformed); if ($atEOF) { break; } } }
/** * Encode $in to $out. * * @param Swift_OutputByteStream $os to read from * @param Swift_InputByteStream $is to write to * @param int $firstLineOffset * @param int $maxLineLength 0 indicates the default length for this encoding * * @throws RuntimeException */ public function encodeByteStream(Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0, $maxLineLength = 0) { if ($this->charset !== 'utf-8') { throw new RuntimeException(sprintf('Charset "%s" not supported. NativeQpContentEncoder only supports "utf-8"', $this->charset)); } $string = ''; while (false !== ($bytes = $os->read(8192))) { $string .= $bytes; } $is->write($this->encodeString($string)); }
/** * Overwrite this character stream using the byte sequence in the byte stream. * @param Swift_OutputByteStream $os output stream to read from */ public function importByteStream(Swift_OutputByteStream $os) { if (!isset($this->_charReader)) { $this->_charReader = $this->_charReaderFactory ->getReaderFor($this->_charset); } $startLength = $this->_charReader->getInitialByteSize(); while (false !== $bytes = $os->read($startLength)) { $c = array(); for ($i = 0, $len = strlen($bytes); $i < $len; ++$i) { $c[] = self::$_byteMap[$bytes[$i]]; } $size = count($c); $need = $this->_charReader ->validateByteSequence($c, $size); if ($need > 0 && false !== $bytes = $os->read($need)) { for ($i = 0, $len = strlen($bytes); $i < $len; ++$i) { $c[] = self::$_byteMap[$bytes[$i]]; } } $this->_array[] = $c; ++$this->_array_size; } }
/** * Encode stream $in to stream $out. * * @param Swift_OutputByteStream $os * @param Swift_InputByteStream $is * @param int $firstLineOffset ignored * @param int $maxLineLength optional, 0 means no wrapping will occur */ 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); } }
/** * Set a ByteStream into the cache under $itemKey for the namespace $nsKey. * * @see MODE_WRITE, MODE_APPEND * * @param string $nsKey * @param string $itemKey * @param Swift_OutputByteStream $os * @param int $mode */ public function importFromByteStream($nsKey, $itemKey, Swift_OutputByteStream $os, $mode) { $this->_prepareCache($nsKey); switch ($mode) { case self::MODE_WRITE: $this->clearKey($nsKey, $itemKey); case self::MODE_APPEND: if (!$this->hasKey($nsKey, $itemKey)) { $this->_contents[$nsKey][$itemKey] = ''; } while (false !== ($bytes = $os->read(8192))) { $this->_contents[$nsKey][$itemKey] .= $bytes; } break; default: throw new Swift_SwiftException('Invalid mode [' . $mode . '] used to set nsKey=' . $nsKey . ', itemKey=' . $itemKey); } }
/** * Encode stream $in to stream $out. * * @param Swift_OutputByteStream $in * @param Swift_InputByteStream $out * @param int $firstLineOffset ignored * @param int $maxLineLength ignored */ public function encodeByteStream(Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0, $maxLineLength = 0) { while (false !== ($bytes = $os->read(8192))) { $is->write($bytes); } }
/** * @see Swift_CharacterStream::importByteStream() * * @param Swift_OutputByteStream $os */ public function importByteStream(Swift_OutputByteStream $os) { $this->flushContents(); $blocks = 512; $os->setReadPointer(0); while (false !== ($read = $os->read($blocks))) { $this->write($read); } }
/** * Set a ByteStream into the cache under $itemKey for the namespace $nsKey. * * @see MODE_WRITE, MODE_APPEND * * @param string $nsKey * @param string $itemKey * @param Swift_OutputByteStream $os * @param int $mode * * @throws Swift_IoException */ public function importFromByteStream($nsKey, $itemKey, Swift_OutputByteStream $os, $mode) { $this->_prepareCache($nsKey); switch ($mode) { case self::MODE_WRITE: $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_START); break; case self::MODE_APPEND: $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_END); break; default: throw new Swift_SwiftException('Invalid mode [' . $mode . '] used to set nsKey=' . $nsKey . ', itemKey=' . $itemKey); break; } while (false !== ($bytes = $os->read(8192))) { fwrite($fp, $bytes); } $this->_freeHandle($nsKey, $itemKey); }
private function _readStream(Swift_OutputByteStream $os) { $string = ''; while (false !== ($bytes = $os->read(8192))) { $string .= $bytes; } return $string; }
/** * Merges an OutputByteStream to Swift_Message. * * @param Swift_OutputByteStream $fromStream * @param Swift_Message $message */ protected function streamToMime(Swift_OutputByteStream $fromStream, Swift_Message $message) { $bufferLength = 78; $headerData = ''; $fromStream->setReadPointer(0); while (($buffer = $fromStream->read($bufferLength)) !== false) { $headerData .= $buffer; if (false !== strpos($buffer, "\r\n\r\n")) { break; } } $headersPosEnd = strpos($headerData, "\r\n\r\n"); $headerData = trim($headerData); $headerData = substr($headerData, 0, $headersPosEnd); $headerLines = explode("\r\n", $headerData); unset($headerData); $headers = array(); $currentHeaderName = ''; foreach ($headerLines as $headerLine) { // Line separated if (ctype_space($headerLines[0]) || false === strpos($headerLine, ':')) { $headers[$currentHeaderName] .= ' ' . trim($headerLine); continue; } $header = explode(':', $headerLine, 2); $currentHeaderName = strtolower($header[0]); $headers[$currentHeaderName] = trim($header[1]); } $messageStream = new Swift_ByteStream_TemporaryFileByteStream(); $messageStream->addFilter($this->replacementFactory->createFilter("\r\n", "\n"), 'CRLF to LF'); $messageStream->addFilter($this->replacementFactory->createFilter("\n", "\r\n"), 'LF to CRLF'); $messageHeaders = $message->getHeaders(); // No need to check for 'application/pkcs7-mime', as this is always base64 if ('multipart/signed;' === substr($headers['content-type'], 0, 17)) { if (!preg_match('/boundary=("[^"]+"|(?:[^\\s]+|$))/is', $headers['content-type'], $contentTypeData)) { throw new Swift_SwiftException('Failed to find Boundary parameter'); } $boundary = trim($contentTypeData['1'], '"'); $boundaryLen = strlen($boundary); // Skip the header and CRLF CRLF $fromStream->setReadPointer($headersPosEnd + 4); while (false !== ($buffer = $fromStream->read($bufferLength))) { $messageStream->write($buffer); } $messageStream->commit(); $messageHeaders->remove('Content-Transfer-Encoding'); $message->setContentType($headers['content-type']); $message->setBoundary($boundary); $message->setBody($messageStream); } else { $fromStream->setReadPointer($headersPosEnd + 4); if (null === $this->headerFactory) { $this->headerFactory = Swift_DependencyContainer::getInstance()->lookup('mime.headerfactory'); } $message->setContentType($headers['content-type']); $messageHeaders->set($this->headerFactory->createTextHeader('Content-Transfer-Encoding', $headers['content-transfer-encoding'])); $messageHeaders->set($this->headerFactory->createTextHeader('Content-Disposition', $headers['content-disposition'])); while (false !== ($buffer = $fromStream->read($bufferLength))) { $messageStream->write($buffer); } $messageStream->commit(); $message->setBody($messageStream); } }
private function readStream(Swift_OutputByteStream $os) { $string = ''; while (false !== ($bytes = $os->read(8192))) { $string .= $bytes; } $os->setReadPointer(0); return $string; }
/** * Overwrite this character stream using the byte sequence in the byte stream. * @param Swift_OutputByteStream $os output stream to read from */ public function importByteStream(Swift_OutputByteStream $os) { if (!isset($this->_charReader)) { $this->_charReader = $this->_charReaderFactory ->getReaderFor($this->_charset); } $startLength = $this->_charReader->getInitialByteSize(); while (false !== $bytes = $os->read($startLength)) { $c = array_values(unpack('C*', $bytes)); $size = count($c); $need = $this->_charReader ->validateByteSequence($c, $size); if ($need > 0 && false !== $bytes = $os->read($need)) { // try another optimisation (array_values call unneeded) $c = array_merge($c, unpack('C*', $bytes)); } $this->_array[] = $c; ++$this->_array_size; } }