PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/core/Associates/SwiftMailer/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Plugins/PopBeforeSmtpPlugin.php

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