|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of SwiftMailer. |
|
5
|
|
|
* (c) 2004-2009 Chris Corbyn |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Makes sure a connection to a POP3 host has been established prior to connecting to SMTP. |
|
13
|
|
|
* |
|
14
|
|
|
* @author Chris Corbyn |
|
15
|
|
|
*/ |
|
16
|
|
|
class Swift_Plugins_PopBeforeSmtpPlugin implements Swift_Events_TransportChangeListener, Swift_Plugins_Pop_Pop3Connection |
|
|
|
|
|
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* A delegate connection to use (mostly a test hook) |
|
20
|
|
|
* |
|
21
|
|
|
* @var Swift_Plugins_Pop_Pop3Connection |
|
22
|
|
|
*/ |
|
23
|
|
|
private $_connection; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Hostname of the POP3 server |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
private $_host; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Port number to connect on |
|
34
|
|
|
* |
|
35
|
|
|
* @var int |
|
36
|
|
|
*/ |
|
37
|
|
|
private $_port; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Encryption type to use (if any) |
|
41
|
|
|
* |
|
42
|
|
|
* @var null|string |
|
43
|
|
|
*/ |
|
44
|
|
|
private $_crypto; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Username to use (if any) |
|
48
|
|
|
* |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
private $_username; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Password to use (if any) |
|
55
|
|
|
* |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
private $_password; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Established connection via TCP socket |
|
62
|
|
|
* |
|
63
|
|
|
* @var resource |
|
64
|
|
|
*/ |
|
65
|
|
|
private $_socket; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Connect timeout in seconds |
|
69
|
|
|
* |
|
70
|
|
|
* @var int |
|
71
|
|
|
*/ |
|
72
|
|
|
private $_timeout = 10; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* SMTP Transport to bind to |
|
76
|
|
|
* |
|
77
|
|
|
* @var Swift_Transport |
|
78
|
|
|
*/ |
|
79
|
|
|
private $_transport; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Create a new PopBeforeSmtpPlugin for $host and $port. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $host |
|
85
|
|
|
* @param int $port |
|
86
|
|
|
* @param string $crypto as "tls" or "ssl" |
|
87
|
|
|
*/ |
|
88
|
4 |
|
public function __construct($host, $port = 110, $crypto = null) |
|
89
|
|
|
{ |
|
90
|
4 |
|
$this->_host = $host; |
|
91
|
4 |
|
$this->_port = $port; |
|
92
|
4 |
|
$this->_crypto = $crypto; |
|
93
|
4 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Create a new PopBeforeSmtpPlugin for $host and $port. |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $host |
|
99
|
|
|
* @param int $port |
|
100
|
|
|
* @param string $crypto as "tls" or "ssl" |
|
101
|
|
|
* |
|
102
|
|
|
* @return self |
|
103
|
|
|
*/ |
|
104
|
|
|
public static function newInstance($host, $port = 110, $crypto = null) |
|
105
|
|
|
{ |
|
106
|
|
|
return new self($host, $port, $crypto); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Set a Pop3Connection to delegate to instead of connecting directly. |
|
111
|
|
|
* |
|
112
|
|
|
* @param Swift_Plugins_Pop_Pop3Connection $connection |
|
113
|
|
|
* |
|
114
|
|
|
* @return $this |
|
115
|
|
|
*/ |
|
116
|
4 |
|
public function setConnection(Swift_Plugins_Pop_Pop3Connection $connection) |
|
117
|
|
|
{ |
|
118
|
4 |
|
$this->_connection = $connection; |
|
119
|
|
|
|
|
120
|
4 |
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Bind this plugin to a specific SMTP transport instance. |
|
125
|
|
|
* |
|
126
|
|
|
* @param Swift_Transport |
|
127
|
|
|
*/ |
|
128
|
2 |
|
public function bindSmtp(Swift_Transport $smtp) |
|
129
|
|
|
{ |
|
130
|
2 |
|
$this->_transport = $smtp; |
|
131
|
2 |
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Set the connection timeout in seconds (default 10). |
|
135
|
|
|
* |
|
136
|
|
|
* @param int $timeout |
|
137
|
|
|
* |
|
138
|
|
|
* @return $this |
|
139
|
|
|
*/ |
|
140
|
|
|
public function setTimeout($timeout) |
|
141
|
|
|
{ |
|
142
|
|
|
$this->_timeout = (int)$timeout; |
|
143
|
|
|
|
|
144
|
|
|
return $this; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Set the username to use when connecting (if needed). |
|
149
|
|
|
* |
|
150
|
|
|
* @param string $username |
|
151
|
|
|
* |
|
152
|
|
|
* @return $this |
|
153
|
|
|
*/ |
|
154
|
|
|
public function setUsername($username) |
|
155
|
|
|
{ |
|
156
|
|
|
$this->_username = $username; |
|
157
|
|
|
|
|
158
|
|
|
return $this; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Set the password to use when connecting (if needed). |
|
163
|
|
|
* |
|
164
|
|
|
* @param string $password |
|
165
|
|
|
* |
|
166
|
|
|
* @return $this |
|
167
|
|
|
*/ |
|
168
|
|
|
public function setPassword($password) |
|
169
|
|
|
{ |
|
170
|
|
|
$this->_password = $password; |
|
171
|
|
|
|
|
172
|
|
|
return $this; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Connect to the POP3 host and authenticate. |
|
177
|
|
|
* |
|
178
|
|
|
* @throws Swift_Plugins_Pop_Pop3Exception if connection fails |
|
179
|
|
|
*/ |
|
180
|
3 |
|
public function connect() |
|
181
|
|
|
{ |
|
182
|
3 |
|
if (isset($this->_connection)) { |
|
183
|
3 |
|
$this->_connection->connect(); |
|
184
|
3 |
|
} else { |
|
185
|
|
|
if (!isset($this->_socket)) { |
|
186
|
|
|
if (!$socket = fsockopen( |
|
187
|
|
|
$this->_getHostString(), $this->_port, $errno, $errstr, $this->_timeout |
|
188
|
|
|
) |
|
189
|
|
|
) { |
|
190
|
|
|
throw new Swift_Plugins_Pop_Pop3Exception( |
|
191
|
|
|
sprintf('Failed to connect to POP3 host [%s]: %s', $this->_host, $errstr) |
|
192
|
|
|
); |
|
193
|
|
|
} |
|
194
|
|
|
$this->_socket = $socket; |
|
195
|
|
|
|
|
196
|
|
|
if (false === $greeting = fgets($this->_socket)) { |
|
197
|
|
|
throw new Swift_Plugins_Pop_Pop3Exception( |
|
198
|
|
|
sprintf('Failed to connect to POP3 host [%s]', trim($greeting)) |
|
199
|
|
|
); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
$this->_assertOk($greeting); |
|
203
|
|
|
|
|
204
|
|
|
if ($this->_username) { |
|
205
|
|
|
$this->_command(sprintf("USER %s\r\n", $this->_username)); |
|
206
|
|
|
$this->_command(sprintf("PASS %s\r\n", $this->_password)); |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
3 |
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Disconnect from the POP3 host. |
|
214
|
|
|
*/ |
|
215
|
3 |
|
public function disconnect() |
|
216
|
|
|
{ |
|
217
|
3 |
|
if (isset($this->_connection)) { |
|
218
|
3 |
|
$this->_connection->disconnect(); |
|
219
|
3 |
|
} else { |
|
220
|
|
|
$this->_command("QUIT\r\n"); |
|
221
|
|
|
if (!fclose($this->_socket)) { |
|
222
|
|
|
throw new Swift_Plugins_Pop_Pop3Exception( |
|
223
|
|
|
sprintf('POP3 host [%s] connection could not be stopped', $this->_host) |
|
224
|
|
|
); |
|
225
|
|
|
} |
|
226
|
|
|
$this->_socket = null; |
|
227
|
|
|
} |
|
228
|
3 |
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Invoked just before a Transport is started. |
|
232
|
|
|
* |
|
233
|
|
|
* @param Swift_Events_TransportChangeEvent $evt |
|
234
|
|
|
*/ |
|
235
|
4 |
|
public function beforeTransportStarted(Swift_Events_TransportChangeEvent $evt) |
|
236
|
|
|
{ |
|
237
|
|
|
if ( |
|
238
|
4 |
|
isset($this->_transport) |
|
239
|
4 |
|
&& |
|
240
|
2 |
|
$this->_transport !== $evt->getTransport() |
|
241
|
4 |
|
) { |
|
242
|
1 |
|
return; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
3 |
|
$this->connect(); |
|
246
|
3 |
|
$this->disconnect(); |
|
247
|
3 |
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Not used. |
|
251
|
|
|
* |
|
252
|
|
|
* @param Swift_Events_TransportChangeEvent $evt |
|
253
|
|
|
*/ |
|
254
|
|
|
public function transportStarted(Swift_Events_TransportChangeEvent $evt) |
|
255
|
|
|
{ |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* Not used. |
|
260
|
|
|
* |
|
261
|
|
|
* @param Swift_Events_TransportChangeEvent $evt |
|
262
|
|
|
*/ |
|
263
|
|
|
public function beforeTransportStopped(Swift_Events_TransportChangeEvent $evt) |
|
264
|
|
|
{ |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* Not used. |
|
269
|
|
|
* |
|
270
|
|
|
* @param Swift_Events_TransportChangeEvent $evt |
|
271
|
|
|
*/ |
|
272
|
|
|
public function transportStopped(Swift_Events_TransportChangeEvent $evt) |
|
273
|
|
|
{ |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
private function _command($command) |
|
277
|
|
|
{ |
|
278
|
|
|
if (!fwrite($this->_socket, $command)) { |
|
279
|
|
|
throw new Swift_Plugins_Pop_Pop3Exception( |
|
280
|
|
|
sprintf('Failed to write command [%s] to POP3 host', trim($command)) |
|
281
|
|
|
); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
if (false === $response = fgets($this->_socket)) { |
|
285
|
|
|
throw new Swift_Plugins_Pop_Pop3Exception( |
|
286
|
|
|
sprintf('Failed to read from POP3 host after command [%s]', trim($command)) |
|
287
|
|
|
); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
$this->_assertOk($response); |
|
291
|
|
|
|
|
292
|
|
|
return $response; |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
private function _assertOk($response) |
|
296
|
|
|
{ |
|
297
|
|
|
if (0 !== strpos($response, '+OK')) { |
|
298
|
|
|
throw new Swift_Plugins_Pop_Pop3Exception( |
|
299
|
|
|
sprintf('POP3 command failed [%s]', trim($response)) |
|
300
|
|
|
); |
|
301
|
|
|
} |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
private function _getHostString() |
|
305
|
|
|
{ |
|
306
|
|
|
$host = $this->_host; |
|
307
|
|
|
switch (Swift::strtolowerWithStaticCache($this->_crypto)) { |
|
308
|
|
|
case 'ssl': |
|
309
|
|
|
$host = 'ssl://' . $host; |
|
310
|
|
|
break; |
|
311
|
|
|
|
|
312
|
|
|
case 'tls': |
|
313
|
|
|
$host = 'tls://' . $host; |
|
314
|
|
|
break; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
return $host; |
|
318
|
|
|
} |
|
319
|
|
|
} |
|
320
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.