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\Mailer\Transport\Smtp\Stream;
13
 
14
use Symfony\Component\Mailer\Exception\TransportException;
15
 
16
/**
17
 * A stream supporting remote sockets.
18
 *
19
 * @author Fabien Potencier <fabien@symfony.com>
20
 * @author Chris Corbyn
21
 *
22
 * @internal
23
 */
24
final class SocketStream extends AbstractStream
25
{
26
    private string $url;
27
    private string $host = 'localhost';
28
    private int $port = 465;
29
    private float $timeout;
30
    private bool $tls = true;
31
    private ?string $sourceIp = null;
32
    private array $streamContextOptions = [];
33
 
34
    /**
35
     * @return $this
36
     */
37
    public function setTimeout(float $timeout): static
38
    {
39
        $this->timeout = $timeout;
40
 
41
        return $this;
42
    }
43
 
44
    public function getTimeout(): float
45
    {
46
        return $this->timeout ?? (float) \ini_get('default_socket_timeout');
47
    }
48
 
49
    /**
50
     * Literal IPv6 addresses should be wrapped in square brackets.
51
     *
52
     * @return $this
53
     */
54
    public function setHost(string $host): static
55
    {
56
        $this->host = $host;
57
 
58
        return $this;
59
    }
60
 
61
    public function getHost(): string
62
    {
63
        return $this->host;
64
    }
65
 
66
    /**
67
     * @return $this
68
     */
69
    public function setPort(int $port): static
70
    {
71
        $this->port = $port;
72
 
73
        return $this;
74
    }
75
 
76
    public function getPort(): int
77
    {
78
        return $this->port;
79
    }
80
 
81
    /**
82
     * Sets the TLS/SSL on the socket (disables STARTTLS).
83
     *
84
     * @return $this
85
     */
86
    public function disableTls(): static
87
    {
88
        $this->tls = false;
89
 
90
        return $this;
91
    }
92
 
93
    public function isTLS(): bool
94
    {
95
        return $this->tls;
96
    }
97
 
98
    /**
99
     * @return $this
100
     */
101
    public function setStreamOptions(array $options): static
102
    {
103
        $this->streamContextOptions = $options;
104
 
105
        return $this;
106
    }
107
 
108
    public function getStreamOptions(): array
109
    {
110
        return $this->streamContextOptions;
111
    }
112
 
113
    /**
114
     * Sets the source IP.
115
     *
116
     * IPv6 addresses should be wrapped in square brackets.
117
     *
118
     * @return $this
119
     */
120
    public function setSourceIp(string $ip): static
121
    {
122
        $this->sourceIp = $ip;
123
 
124
        return $this;
125
    }
126
 
127
    /**
128
     * Returns the IP used to connect to the destination.
129
     */
130
    public function getSourceIp(): ?string
131
    {
132
        return $this->sourceIp;
133
    }
134
 
135
    public function initialize(): void
136
    {
137
        $this->url = $this->host.':'.$this->port;
138
        if ($this->tls) {
139
            $this->url = 'ssl://'.$this->url;
140
        }
141
        $options = [];
142
        if ($this->sourceIp) {
143
            $options['socket']['bindto'] = $this->sourceIp.':0';
144
        }
145
        if ($this->streamContextOptions) {
146
            $options = array_merge($options, $this->streamContextOptions);
147
        }
148
        // do it unconditionnally as it will be used by STARTTLS as well if supported
149
        $options['ssl']['crypto_method'] ??= \STREAM_CRYPTO_METHOD_TLS_CLIENT | \STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT | \STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT;
150
        $streamContext = stream_context_create($options);
151
 
152
        $timeout = $this->getTimeout();
153
        set_error_handler(function ($type, $msg) {
154
            throw new TransportException(sprintf('Connection could not be established with host "%s": ', $this->url).$msg);
155
        });
156
        try {
157
            $this->stream = stream_socket_client($this->url, $errno, $errstr, $timeout, \STREAM_CLIENT_CONNECT, $streamContext);
158
        } finally {
159
            restore_error_handler();
160
        }
161
 
162
        stream_set_blocking($this->stream, true);
163
        stream_set_timeout($this->stream, $timeout);
164
        $this->in = &$this->stream;
165
        $this->out = &$this->stream;
166
    }
167
 
168
    public function startTLS(): bool
169
    {
170
        set_error_handler(function ($type, $msg) {
171
            throw new TransportException('Unable to connect with STARTTLS: '.$msg);
172
        });
173
        try {
174
            return stream_socket_enable_crypto($this->stream, true);
175
        } finally {
176
            restore_error_handler();
177
        }
178
    }
179
 
180
    public function terminate(): void
181
    {
182
        if (null !== $this->stream) {
183
            fclose($this->stream);
184
        }
185
 
186
        parent::terminate();
187
    }
188
 
189
    protected function getReadConnectionDescription(): string
190
    {
191
        return $this->url;
192
    }
193
}