54 if (null !== $encryptCertificate) {
59 ->lookup(
'transport.replacementfactory');
61 $this->signOptions = PKCS7_DETACHED;
64 if (defined(
'OPENSSL_CIPHER_AES_128_CBC')) {
65 $this->encryptCipher = OPENSSL_CIPHER_AES_128_CBC;
67 $this->encryptCipher = OPENSSL_CIPHER_RC2_128;
79 public static function newInstance($certificate = null, $privateKey = null)
81 return new self($certificate, $privateKey);
98 $this->signCertificate =
'file://'.str_replace(
'\\',
'/', realpath($certificate));
100 if (null !== $privateKey) {
101 if (is_array($privateKey)) {
102 $this->signPrivateKey = $privateKey;
103 $this->signPrivateKey[0] =
'file://'.str_replace(
'\\',
'/', realpath($privateKey[0]));
105 $this->signPrivateKey =
'file://'.str_replace(
'\\',
'/', realpath($privateKey));
111 $this->extraCerts = str_replace(
'\\',
'/', realpath(
$extraCerts));
130 if (is_array($recipientCerts)) {
131 $this->encryptCert = array();
133 foreach ($recipientCerts as $cert) {
134 $this->encryptCert[] =
'file://'.str_replace(
'\\',
'/', realpath($cert));
137 $this->encryptCert =
'file://'.str_replace(
'\\',
'/', realpath($recipientCerts));
140 if (null !== $cipher) {
141 $this->encryptCipher = $cipher;
208 if (null === $this->signCertificate && null === $this->encryptCert) {
232 return array(
'Content-Type',
'Content-Transfer-Encoding',
'Content-Disposition');
244 $mimeEntity->toByteStream($messageStream);
245 $messageStream->commit();
247 if (null !== $this->signCertificate && null !== $this->encryptCert) {
250 if ($this->signThenEncrypt) {
257 }
elseif ($this->signCertificate !== null) {
274 $messageHeaders = $mimeEntity->getHeaders();
275 $messageHeaders->remove(
'Message-ID');
276 $messageHeaders->remove(
'Date');
277 $messageHeaders->remove(
'Subject');
278 $messageHeaders->remove(
'MIME-Version');
279 $messageHeaders->remove(
'To');
280 $messageHeaders->remove(
'From');
296 if (null !== $this->extraCerts) {
300 if (!call_user_func_array(
'openssl_pkcs7_sign', $args)) {
301 throw new Swift_IoException(sprintf(
'Failed to sign S/Mime message. Error: "%s".', openssl_error_string()));
318 throw new Swift_IoException(sprintf(
'Failed to encrypt S/Mime message. Error: "%s".', openssl_error_string()));
330 $bufferLength = 4096;
332 $filteredStream->addFilter($this->replacementFactory->createFilter(
"\r\n",
"\n"),
'CRLF to LF');
333 $filteredStream->addFilter($this->replacementFactory->createFilter(
"\n",
"\r\n"),
'LF to CRLF');
335 while (
false !== ($buffer = $fromStream->
read($bufferLength))) {
336 $filteredStream->write($buffer);
339 $filteredStream->flushBuffers();
341 while (
false !== ($buffer = $filteredStream->read($bufferLength))) {
342 $toStream->
write($buffer);
361 while (($buffer = $fromStream->
read($bufferLength)) !==
false) {
362 $headerData .= $buffer;
364 if (
false !== strpos($buffer,
"\r\n\r\n")) {
369 $headersPosEnd = strpos($headerData,
"\r\n\r\n");
370 $headerData = trim($headerData);
371 $headerData = substr($headerData, 0, $headersPosEnd);
372 $headerLines = explode(
"\r\n", $headerData);
376 $currentHeaderName =
'';
378 foreach ($headerLines as $headerLine) {
380 if (ctype_space($headerLines[0]) ||
false === strpos($headerLine,
':')) {
381 $headers[$currentHeaderName] .=
' '.trim($headerLine);
385 $header = explode(
':', $headerLine, 2);
386 $currentHeaderName = strtolower($header[0]);
387 $headers[$currentHeaderName] = trim($header[1]);
391 $messageStream->addFilter($this->replacementFactory->createFilter(
"\r\n",
"\n"),
'CRLF to LF');
392 $messageStream->addFilter($this->replacementFactory->createFilter(
"\n",
"\r\n"),
'LF to CRLF');
397 if (
'multipart/signed;' === substr($headers[
'content-type'], 0, 17)) {
398 if (!preg_match(
'/boundary=("[^"]+"|(?:[^\s]+|$))/is', $headers[
'content-type'], $contentTypeData)) {
402 $boundary = trim($contentTypeData[
'1'],
'"');
403 $boundaryLen = strlen($boundary);
408 while (
false !== ($buffer = $fromStream->
read($bufferLength))) {
409 $messageStream->write($buffer);
412 $messageStream->commit();
414 $messageHeaders->remove(
'Content-Transfer-Encoding');
417 $message->
setBody($messageStream);
421 if (null === $this->headerFactory) {
426 $messageHeaders->set($this->headerFactory->createTextHeader(
'Content-Transfer-Encoding', $headers[
'content-transfer-encoding']));
427 $messageHeaders->set($this->headerFactory->createTextHeader(
'Content-Disposition', $headers[
'content-disposition']));
429 while (
false !== ($buffer = $fromStream->
read($bufferLength))) {
430 $messageStream->write($buffer);
433 $messageStream->commit();
434 $message->
setBody($messageStream);