Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Symfony\Component\Mime\Crypto;
13
 
14
use Symfony\Component\Mime\Exception\RuntimeException;
15
use Symfony\Component\Mime\Message;
16
 
17
/**
18
 * @author Sebastiaan Stok <s.stok@rollerscapes.net>
19
 */
20
final class SMimeEncrypter extends SMime
21
{
22
    private string|array $certs;
23
    private int $cipher;
24
 
25
    /**
26
     * @param string|string[] $certificate The path (or array of paths) of the file(s) containing the X.509 certificate(s)
27
     * @param int|null        $cipher      A set of algorithms used to encrypt the message. Must be one of these PHP constants: https://www.php.net/manual/en/openssl.ciphers.php
28
     */
29
    public function __construct(string|array $certificate, int $cipher = null)
30
    {
31
        if (!\extension_loaded('openssl')) {
32
            throw new \LogicException('PHP extension "openssl" is required to use SMime.');
33
        }
34
 
35
        if (\is_array($certificate)) {
36
            $this->certs = array_map($this->normalizeFilePath(...), $certificate);
37
        } else {
38
            $this->certs = $this->normalizeFilePath($certificate);
39
        }
40
 
41
        $this->cipher = $cipher ?? \OPENSSL_CIPHER_AES_256_CBC;
42
    }
43
 
44
    public function encrypt(Message $message): Message
45
    {
46
        $bufferFile = tmpfile();
47
        $outputFile = tmpfile();
48
 
49
        $this->iteratorToFile($message->toIterable(), $bufferFile);
50
 
51
        if (!@openssl_pkcs7_encrypt(stream_get_meta_data($bufferFile)['uri'], stream_get_meta_data($outputFile)['uri'], $this->certs, [], 0, $this->cipher)) {
52
            throw new RuntimeException(sprintf('Failed to encrypt S/Mime message. Error: "%s".', openssl_error_string()));
53
        }
54
 
55
        $mimePart = $this->convertMessageToSMimePart($outputFile, 'application', 'pkcs7-mime');
56
        $mimePart->getHeaders()
57
            ->addTextHeader('Content-Transfer-Encoding', 'base64')
58
            ->addParameterizedHeader('Content-Disposition', 'attachment', ['name' => 'smime.p7m'])
59
        ;
60
 
61
        return new Message($message->getHeaders(), $mimePart);
62
    }
63
}