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

/modules/email/vendor/swift/classes/Swift/Plugins/PopBeforeSmtpPlugin.php

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