/** * 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); } }
/** * {@inheritdoc} * * @return $this|self */ public function setBoundary($boundary) : self { $this->message->setBoundary($boundary); return $this; }