1 | <?php |
||
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) |
|
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) |
|
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) |
|
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) |
||
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) |
||
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() |
|
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) |
||
257 | |||
258 | /** |
||
259 | * Not used. |
||
260 | * |
||
261 | * @param Swift_Events_TransportChangeEvent $evt |
||
262 | */ |
||
263 | public function beforeTransportStopped(Swift_Events_TransportChangeEvent $evt) |
||
266 | |||
267 | /** |
||
268 | * Not used. |
||
269 | * |
||
270 | * @param Swift_Events_TransportChangeEvent $evt |
||
271 | */ |
||
272 | public function transportStopped(Swift_Events_TransportChangeEvent $evt) |
||
275 | |||
276 | private function _command($command) |
||
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() |
||
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.