/formtools/modules/swift_mailer/php4/Swift/Authenticator/PopB4Smtp/Pop3Connection.php

https://bitbucket.org/lihorne/tedxuw · PHP · 175 lines · 86 code · 7 blank · 82 comment · 10 complexity · 6569a504fd85de480e24d29cda59a6ab MD5 · raw file

  1. <?php
  2. /**
  3. * Swift Mailer PopB4Smtp Pop3 Connection component
  4. * Please read the LICENSE file
  5. * @author Chris Corbyn <chris@w3style.co.uk>
  6. * @package Swift_Authenticator
  7. * @license GNU Lesser General Public License
  8. */
  9. require_once dirname(__FILE__) . "/../../ClassLoader.php";
  10. Swift_ClassLoader::load("Swift_Errors");
  11. if (!defined("SWIFT_AUTH_POP_ENC_SSL")) define("SWIFT_AUTH_POP_ENC_SSL", 1);
  12. if (!defined("SWIFT_AUTH_POP_ENC_OFF")) define("SWIFT_AUTH_POP_ENC_OFF", 0);
  13. /**
  14. * Swift PopB4Smtp Authenticator Connection Component for the POP3 server
  15. * Provides a I/O wrapper for the POP3 connection
  16. * @package Swift_Authenticator
  17. * @author Chris Corbyn <chris@w3style.co.uk>
  18. */
  19. class Swift_Authenticator_PopB4Smtp_Pop3Connection
  20. {
  21. /**
  22. * Constant for no encyption
  23. */
  24. var $ENC_OFF = SWIFT_AUTH_POP_ENC_OFF;
  25. /**
  26. * Constant for SSL encryption
  27. */
  28. var $ENC_SSL = SWIFT_AUTH_POP_ENC_SSL;
  29. /**
  30. * The server to connect to (IP or FQDN)
  31. * @var string
  32. */
  33. var $server = null;
  34. /**
  35. * The port to connect to
  36. * @var int
  37. */
  38. var $port = null;
  39. /**
  40. * The open connection resource from fsockopen()
  41. * @var resource
  42. */
  43. var $handle = null;
  44. /**
  45. * Constructor
  46. * @param string The name of the POP3 server
  47. * @param int The port for the POP3 service
  48. * @param int The level of encryption to use
  49. */
  50. function Swift_Authenticator_PopB4Smtp_Pop3Connection($server="localhost", $port=110, $encryption=0)
  51. {
  52. $this->setServer($server);
  53. $this->setPort($port);
  54. $this->setEncryption($encryption);
  55. }
  56. /**
  57. * Set the server name
  58. * @param string The IP or FQDN of the POP3 server
  59. */
  60. function setServer($server)
  61. {
  62. $this->server = (string) $server;
  63. }
  64. /**
  65. * Set the port number for the POP3 server
  66. * @param int
  67. */
  68. function setPort($port)
  69. {
  70. $this->port = (int) $port;
  71. }
  72. /**
  73. * Get the server name
  74. * @return string
  75. */
  76. function getServer()
  77. {
  78. return $this->server;
  79. }
  80. /**
  81. * Get the remote port number
  82. * @return int
  83. */
  84. function getPort()
  85. {
  86. return $this->port;
  87. }
  88. /**
  89. * Set the level of enryption to use (see ENC_OFF or ENC_SSL)
  90. * @param int The constant for the encryption level
  91. */
  92. function setEncryption($enc)
  93. {
  94. $this->encryption = (int) $enc;
  95. }
  96. /**
  97. * Get the current encryption level set (corresponds to ENC_SSL or ENC_OFF)
  98. * @return int
  99. */
  100. function getEncryption()
  101. {
  102. return $this->encryption;
  103. }
  104. /**
  105. * Check if the response is a +OK response
  106. * @return boolean
  107. */
  108. function assertOk($line)
  109. {
  110. if (substr($line, 0, 3) != "+OK")
  111. Swift_Errors::trigger(new Swift_ConnectionException("The POP3 server did not suitably respond with a +OK response. " .
  112. "[" . $line . "]"));
  113. }
  114. /**
  115. * Try to open the connection
  116. * @throws Swift_ConnectionException If the connection will not start
  117. */
  118. function start()
  119. {
  120. $url = $this->getServer();
  121. if ($this->getEncryption() == $this->ENC_SSL) $url = "ssl://" . $url;
  122. if ((false === $this->handle = fsockopen($url, $this->getPort(), $errno, $errstr, $timeout)))
  123. {
  124. Swift_Errors::trigger(new Swift_ConnectionException(
  125. "The POP3 connection failed to start. The error string returned from fsockopen() is [" . $errstr . "] #" . $errno));
  126. return;
  127. }
  128. }
  129. /**
  130. * Try to close the connection
  131. */
  132. function stop()
  133. {
  134. if ($this->handle !== null)
  135. {
  136. fclose($this->handle);
  137. }
  138. $this->handle = null;
  139. }
  140. /**
  141. * Return the unread buffer contents
  142. * @return string
  143. * @throws Swift_ConnectionException If the connection will not allow data to be read
  144. */
  145. function read()
  146. {
  147. if (false === $response = fgets($this->handle))
  148. {
  149. Swift_Errors::trigger(new Swift_ConnectionException("Data could not be read from the POP3 connection."));
  150. return;
  151. }
  152. return trim($response);
  153. }
  154. /**
  155. * Write a command to the remote socket
  156. * @param string the command to send (without CRLF)
  157. * @throws Swift_ConnectionException If the command cannot be written
  158. */
  159. function write($command)
  160. {
  161. if (false !== fwrite($this->handle, $command . "\r\n"))
  162. {
  163. Swift_Errors::trigger(new Swift_ConnectionException(
  164. "Data could not be written to the POP3 connection."));
  165. return;
  166. }
  167. }
  168. }