XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
PopBeforeSmtpPlugin.php
Go to the documentation of this file.
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 //@require 'Swift/Events/TransportChangeListener.php';
12 //@require 'Swift/Events/TransportChangeEvent.php';
13 
24 {
25 
27  private $_connection;
28 
30  private $_host;
31 
33  private $_port;
34 
36  private $_crypto;
37 
39  private $_username;
40 
42  private $_password;
43 
45  private $_socket;
46 
48  private $_timeout = 10;
49 
51  private $_transport;
52 
60  public function __construct($host, $port = 110, $crypto = null)
61  {
62  $this->_host = $host;
63  $this->_port = $port;
64  $this->_crypto = $crypto;
65  }
66 
76  public static function newInstance($host, $port = 110, $crypto = null)
77  {
78  return new self($host, $port, $crypto);
79  }
80 
86  public function setConnection(Swift_Plugins_Pop_Pop3Connection $connection)
87  {
88  $this->_connection = $connection;
89  return $this;
90  }
91 
97  public function bindSmtp(Swift_Transport $smtp)
98  {
99  $this->_transport = $smtp;
100  }
101 
107  public function setTimeout($timeout)
108  {
109  $this->_timeout = (int) $timeout;
110  return $this;
111  }
112 
118  public function setUsername($username)
119  {
120  $this->_username = $username;
121  return $this;
122  }
123 
129  public function setPassword($password)
130  {
131  $this->_password = $password;
132  return $this;
133  }
134 
140  public function connect()
141  {
142  if (isset($this->_connection))
143  {
144  $this->_connection->connect();
145  }
146  else
147  {
148  if (!isset($this->_socket))
149  {
150  if (!$socket = fsockopen(
151  $this->_getHostString(), $this->_port, $errno, $errstr, $this->_timeout))
152  {
154  sprintf('Failed to connect to POP3 host [%s]: %s', $this->_host, $errstr)
155  );
156  }
157  $this->_socket = $socket;
158 
159  if (false === $greeting = fgets($this->_socket))
160  {
162  sprintf('Failed to connect to POP3 host [%s]', trim($greeting))
163  );
164  }
165 
166  $this->_assertOk($greeting);
167 
168  if ($this->_username)
169  {
170  $this->_command(sprintf("USER %s\r\n", $this->_username));
171  $this->_command(sprintf("PASS %s\r\n", $this->_password));
172  }
173  }
174  }
175  }
176 
180  public function disconnect()
181  {
182  if (isset($this->_connection))
183  {
184  $this->_connection->disconnect();
185  }
186  else
187  {
188  $this->_command("QUIT\r\n");
189  if (!fclose($this->_socket))
190  {
192  sprintf('POP3 host [%s] connection could not be stopped', $this->_host)
193  );
194  }
195  $this->_socket = null;
196  }
197  }
198 
205  {
206  if (isset($this->_transport))
207  {
208  if ($this->_transport !== $evt->getTransport())
209  {
210  return;
211  }
212  }
213 
214  $this->connect();
215  $this->disconnect();
216  }
217 
222  {
223  }
224 
229  {
230  }
231 
236  {
237  }
238 
239  // -- Private Methods
240 
241  private function _command($command)
242  {
243  if (!fwrite($this->_socket, $command))
244  {
246  sprintf('Failed to write command [%s] to POP3 host', trim($command))
247  );
248  }
249 
250  if (false === $response = fgets($this->_socket))
251  {
253  sprintf('Failed to read from POP3 host after command [%s]', trim($command))
254  );
255  }
256 
257  $this->_assertOk($response);
258 
259  return $response;
260  }
261 
262  private function _assertOk($response)
263  {
264  if (substr($response, 0, 3) != '+OK')
265  {
267  sprintf('POP3 command failed [%s]', trim($response))
268  );
269  }
270  }
271 
272  private function _getHostString()
273  {
274  $host = $this->_host;
275  switch (strtolower($this->_crypto))
276  {
277  case 'ssl':
278  $host = 'ssl://' . $host;
279  break;
280 
281  case 'tls':
282  $host = 'tls://' . $host;
283  break;
284  }
285  return $host;
286  }
287 
288 }