PageRenderTime 32ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/vendor/swift/lib/classes/Swift/Plugins/PopBeforeSmtpPlugin.php

http://zoop.googlecode.com/
PHP | 288 lines | 160 code | 38 blank | 90 comment | 14 complexity | 5661bc27faa15b2d3a3e3b1b63398078 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. //@require 'Swift/Events/TransportChangeListener.php';
  10. //@require 'Swift/Events/TransportChangeEvent.php';
  11. /**
  12. * Makes sure a connection to a POP3 host has been established prior to connecting to SMTP.
  13. *
  14. * @package Swift
  15. * @subpackage Plugins
  16. *
  17. * @author Chris Corbyn
  18. */
  19. class Swift_Plugins_PopBeforeSmtpPlugin
  20. implements Swift_Events_TransportChangeListener, Swift_Plugins_Pop_Pop3Connection
  21. {
  22. /** A delegate connection to use (mostly a test hook) */
  23. private $_connection;
  24. /** Hostname of the POP3 server */
  25. private $_host;
  26. /** Port number to connect on */
  27. private $_port;
  28. /** Encryption type to use (if any) */
  29. private $_crypto;
  30. /** Username to use (if any) */
  31. private $_username;
  32. /** Password to use (if any) */
  33. private $_password;
  34. /** Established connection via TCP socket */
  35. private $_socket;
  36. /** Connect timeout in seconds */
  37. private $_timeout = 10;
  38. /** SMTP Transport to bind to */
  39. private $_transport;
  40. /**
  41. * Create a new PopBeforeSmtpPlugin for $host and $port.
  42. *
  43. * @param string $host
  44. * @param int $port
  45. * @param string $cypto as "tls" or "ssl"
  46. */
  47. public function __construct($host, $port = 110, $crypto = null)
  48. {
  49. $this->_host = $host;
  50. $this->_port = $port;
  51. $this->_crypto = $crypto;
  52. }
  53. /**
  54. * Create a new PopBeforeSmtpPlugin for $host and $port.
  55. *
  56. * @param string $host
  57. * @param int $port
  58. * @param string $cypto as "tls" or "ssl"
  59. *
  60. * @return Swift_Plugins_PopBeforeSmtpPlugin
  61. */
  62. public static function newInstance($host, $port = 110, $crypto = null)
  63. {
  64. return new self($host, $port, $crypto);
  65. }
  66. /**
  67. * Set a Pop3Connection to delegate to instead of connecting directly.
  68. *
  69. * @param Swift_Plugins_Pop_Pop3Connection $connection
  70. */
  71. public function setConnection(Swift_Plugins_Pop_Pop3Connection $connection)
  72. {
  73. $this->_connection = $connection;
  74. return $this;
  75. }
  76. /**
  77. * Bind this plugin to a specific SMTP transport instance.
  78. *
  79. * @param Swift_Transport
  80. */
  81. public function bindSmtp(Swift_Transport $smtp)
  82. {
  83. $this->_transport = $smtp;
  84. }
  85. /**
  86. * Set the connection timeout in seconds (default 10).
  87. *
  88. * @param int $timeout
  89. */
  90. public function setTimeout($timeout)
  91. {
  92. $this->_timeout = (int) $timeout;
  93. return $this;
  94. }
  95. /**
  96. * Set the username to use when connecting (if needed).
  97. *
  98. * @param string $username
  99. */
  100. public function setUsername($username)
  101. {
  102. $this->_username = $username;
  103. return $this;
  104. }
  105. /**
  106. * Set the password to use when connecting (if needed).
  107. *
  108. * @param string $password
  109. */
  110. public function setPassword($password)
  111. {
  112. $this->_password = $password;
  113. return $this;
  114. }
  115. /**
  116. * Connect to the POP3 host and authenticate.
  117. *
  118. * @throws Swift_Plugins_Pop_Pop3Exception if connection fails
  119. */
  120. public function connect()
  121. {
  122. if (isset($this->_connection))
  123. {
  124. $this->_connection->connect();
  125. }
  126. else
  127. {
  128. if (!isset($this->_socket))
  129. {
  130. if (!$socket = fsockopen(
  131. $this->_getHostString(), $this->_port, $errno, $errstr, $this->_timeout))
  132. {
  133. throw new Swift_Plugins_Pop_Pop3Exception(
  134. sprintf('Failed to connect to POP3 host [%s]: %s', $this->_host, $errstr)
  135. );
  136. }
  137. $this->_socket = $socket;
  138. if (false === $greeting = fgets($this->_socket))
  139. {
  140. throw new Swift_Plugins_Pop_Pop3Exception(
  141. sprintf('Failed to connect to POP3 host [%s]', trim($greeting))
  142. );
  143. }
  144. $this->_assertOk($greeting);
  145. if ($this->_username)
  146. {
  147. $this->_command(sprintf("USER %s\r\n", $this->_username));
  148. $this->_command(sprintf("PASS %s\r\n", $this->_password));
  149. }
  150. }
  151. }
  152. }
  153. /**
  154. * Disconnect from the POP3 host.
  155. */
  156. public function disconnect()
  157. {
  158. if (isset($this->_connection))
  159. {
  160. $this->_connection->disconnect();
  161. }
  162. else
  163. {
  164. $this->_command("QUIT\r\n");
  165. if (!fclose($this->_socket))
  166. {
  167. throw new Swift_Plugins_Pop_Pop3Exception(
  168. sprintf('POP3 host [%s] connection could not be stopped', $this->_host)
  169. );
  170. }
  171. $this->_socket = null;
  172. }
  173. }
  174. /**
  175. * Invoked just before a Transport is started.
  176. *
  177. * @param Swift_Events_TransportChangeEvent $evt
  178. */
  179. public function beforeTransportStarted(Swift_Events_TransportChangeEvent $evt)
  180. {
  181. if (isset($this->_transport))
  182. {
  183. if ($this->_transport !== $evt->getTransport())
  184. {
  185. return;
  186. }
  187. }
  188. $this->connect();
  189. $this->disconnect();
  190. }
  191. /**
  192. * Not used.
  193. */
  194. public function transportStarted(Swift_Events_TransportChangeEvent $evt)
  195. {
  196. }
  197. /**
  198. * Not used.
  199. */
  200. public function beforeTransportStopped(Swift_Events_TransportChangeEvent $evt)
  201. {
  202. }
  203. /**
  204. * Not used.
  205. */
  206. public function transportStopped(Swift_Events_TransportChangeEvent $evt)
  207. {
  208. }
  209. // -- Private Methods
  210. private function _command($command)
  211. {
  212. if (!fwrite($this->_socket, $command))
  213. {
  214. throw new Swift_Plugins_Pop_Pop3Exception(
  215. sprintf('Failed to write command [%s] to POP3 host', trim($command))
  216. );
  217. }
  218. if (false === $response = fgets($this->_socket))
  219. {
  220. throw new Swift_Plugins_Pop_Pop3Exception(
  221. sprintf('Failed to read from POP3 host after command [%s]', trim($command))
  222. );
  223. }
  224. $this->_assertOk($response);
  225. return $response;
  226. }
  227. private function _assertOk($response)
  228. {
  229. if (substr($response, 0, 3) != '+OK')
  230. {
  231. throw new Swift_Plugins_Pop_Pop3Exception(
  232. sprintf('POP3 command failed [%s]', trim($response))
  233. );
  234. }
  235. }
  236. private function _getHostString()
  237. {
  238. $host = $this->_host;
  239. switch (strtolower($this->_crypto))
  240. {
  241. case 'ssl':
  242. $host = 'ssl://' . $host;
  243. break;
  244. case 'tls':
  245. $host = 'tls://' . $host;
  246. break;
  247. }
  248. return $host;
  249. }
  250. }