1: <?php
2:
3: 4: 5: 6: 7: 8: 9:
10:
11: 12: 13: 14: 15: 16: 17: 18:
19: class Swift_Plugins_PopBeforeSmtpPlugin implements Swift_Events_TransportChangeListener, Swift_Plugins_Pop_Pop3Connection
20: {
21:
22: private $_connection;
23:
24:
25: private $_host;
26:
27:
28: private $_port;
29:
30:
31: private $_crypto;
32:
33:
34: private $_username;
35:
36:
37: private $_password;
38:
39:
40: private $_socket;
41:
42:
43: private $_timeout = 10;
44:
45:
46: private $_transport;
47:
48: 49: 50: 51: 52: 53: 54:
55: public function __construct($host, $port = 110, $crypto = null)
56: {
57: $this->_host = $host;
58: $this->_port = $port;
59: $this->_crypto = $crypto;
60: }
61:
62: 63: 64: 65: 66: 67: 68: 69: 70:
71: public static function newInstance($host, $port = 110, $crypto = null)
72: {
73: return new self($host, $port, $crypto);
74: }
75:
76: 77: 78: 79: 80:
81: public function setConnection(Swift_Plugins_Pop_Pop3Connection $connection)
82: {
83: $this->_connection = $connection;
84:
85: return $this;
86: }
87:
88: 89: 90: 91: 92:
93: public function bindSmtp(Swift_Transport $smtp)
94: {
95: $this->_transport = $smtp;
96: }
97:
98: 99: 100: 101: 102:
103: public function setTimeout($timeout)
104: {
105: $this->_timeout = (int) $timeout;
106:
107: return $this;
108: }
109:
110: 111: 112: 113: 114:
115: public function setUsername($username)
116: {
117: $this->_username = $username;
118:
119: return $this;
120: }
121:
122: 123: 124: 125: 126:
127: public function setPassword($password)
128: {
129: $this->_password = $password;
130:
131: return $this;
132: }
133:
134: 135: 136: 137: 138:
139: public function connect()
140: {
141: if (isset($this->_connection)) {
142: $this->_connection->connect();
143: } else {
144: if (!isset($this->_socket)) {
145: if (!$socket = fsockopen(
146: $this->_getHostString(), $this->_port, $errno, $errstr, $this->_timeout))
147: {
148: throw new Swift_Plugins_Pop_Pop3Exception(
149: sprintf('Failed to connect to POP3 host [%s]: %s', $this->_host, $errstr)
150: );
151: }
152: $this->_socket = $socket;
153:
154: if (false === $greeting = fgets($this->_socket)) {
155: throw new Swift_Plugins_Pop_Pop3Exception(
156: sprintf('Failed to connect to POP3 host [%s]', trim($greeting))
157: );
158: }
159:
160: $this->_assertOk($greeting);
161:
162: if ($this->_username) {
163: $this->_command(sprintf("USER %s\r\n", $this->_username));
164: $this->_command(sprintf("PASS %s\r\n", $this->_password));
165: }
166: }
167: }
168: }
169:
170: 171: 172:
173: public function disconnect()
174: {
175: if (isset($this->_connection)) {
176: $this->_connection->disconnect();
177: } else {
178: $this->_command("QUIT\r\n");
179: if (!fclose($this->_socket)) {
180: throw new Swift_Plugins_Pop_Pop3Exception(
181: sprintf('POP3 host [%s] connection could not be stopped', $this->_host)
182: );
183: }
184: $this->_socket = null;
185: }
186: }
187:
188: 189: 190: 191: 192:
193: public function beforeTransportStarted(Swift_Events_TransportChangeEvent $evt)
194: {
195: if (isset($this->_transport)) {
196: if ($this->_transport !== $evt->getTransport()) {
197: return;
198: }
199: }
200:
201: $this->connect();
202: $this->disconnect();
203: }
204:
205: 206: 207:
208: public function transportStarted(Swift_Events_TransportChangeEvent $evt)
209: {
210: }
211:
212: 213: 214:
215: public function beforeTransportStopped(Swift_Events_TransportChangeEvent $evt)
216: {
217: }
218:
219: 220: 221:
222: public function transportStopped(Swift_Events_TransportChangeEvent $evt)
223: {
224: }
225:
226:
227:
228: private function _command($command)
229: {
230: if (!fwrite($this->_socket, $command)) {
231: throw new Swift_Plugins_Pop_Pop3Exception(
232: sprintf('Failed to write command [%s] to POP3 host', trim($command))
233: );
234: }
235:
236: if (false === $response = fgets($this->_socket)) {
237: throw new Swift_Plugins_Pop_Pop3Exception(
238: sprintf('Failed to read from POP3 host after command [%s]', trim($command))
239: );
240: }
241:
242: $this->_assertOk($response);
243:
244: return $response;
245: }
246:
247: private function _assertOk($response)
248: {
249: if (substr($response, 0, 3) != '+OK') {
250: throw new Swift_Plugins_Pop_Pop3Exception(
251: sprintf('POP3 command failed [%s]', trim($response))
252: );
253: }
254: }
255:
256: private function _getHostString()
257: {
258: $host = $this->_host;
259: switch (strtolower($this->_crypto)) {
260: case 'ssl':
261: $host = 'ssl://' . $host;
262: break;
263:
264: case 'tls':
265: $host = 'tls://' . $host;
266: break;
267: }
268:
269: return $host;
270: }
271: }
272: