PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/system/vendor/swift/Swift/Authenticator/PopB4Smtp/Pop3Connection.php

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