PageRenderTime 36ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wp-responder-email-autoresponder-and-newsletter-plugin/lib/classes/Swift/Plugins/PopBeforeSmtpPlugin.php

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