[ Index ]

MailPress 544

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/Swiftmailer/classes/Swift/Plugins/ -> PopBeforeSmtpPlugin.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of SwiftMailer.
   5   * (c) 2004-2009 Chris Corbyn
   6   *
   7   * For the full copyright and license information, please view the LICENSE
   8   * file that was distributed with this source code.
   9   */
  10  
  11  /**
  12   * Makes sure a connection to a POP3 host has been established prior to connecting to SMTP.
  13   *
  14   * @author Chris Corbyn
  15   */
  16  class Swift_Plugins_PopBeforeSmtpPlugin implements Swift_Events_TransportChangeListener, Swift_Plugins_Pop_Pop3Connection
  17  {
  18      /** A delegate connection to use (mostly a test hook) */
  19      private $_connection;
  20  
  21      /** Hostname of the POP3 server */
  22      private $_host;
  23  
  24      /** Port number to connect on */
  25      private $_port;
  26  
  27      /** Encryption type to use (if any) */
  28      private $_crypto;
  29  
  30      /** Username to use (if any) */
  31      private $_username;
  32  
  33      /** Password to use (if any) */
  34      private $_password;
  35  
  36      /** Established connection via TCP socket */
  37      private $_socket;
  38  
  39      /** Connect timeout in seconds */
  40      private $_timeout = 10;
  41  
  42      /** SMTP Transport to bind to */
  43      private $_transport;
  44  
  45      /**
  46       * Create a new PopBeforeSmtpPlugin for $host and $port.
  47       *
  48       * @param string $host
  49       * @param int    $port
  50       * @param string $crypto as "tls" or "ssl"
  51       */
  52      public function __construct($host, $port = 110, $crypto = null)
  53      {
  54          $this->_host = $host;
  55          $this->_port = $port;
  56          $this->_crypto = $crypto;
  57      }
  58  
  59      /**
  60       * Create a new PopBeforeSmtpPlugin for $host and $port.
  61       *
  62       * @param string $host
  63       * @param int    $port
  64       * @param string $crypto 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      /**
  74       * Set a Pop3Connection to delegate to instead of connecting directly.
  75       *
  76       * @param Swift_Plugins_Pop_Pop3Connection $connection
  77       *
  78       * @return Swift_Plugins_PopBeforeSmtpPlugin
  79       */
  80      public function setConnection(Swift_Plugins_Pop_Pop3Connection $connection)
  81      {
  82          $this->_connection = $connection;
  83  
  84          return $this;
  85      }
  86  
  87      /**
  88       * Bind this plugin to a specific SMTP transport instance.
  89       *
  90       * @param Swift_Transport
  91       */
  92      public function bindSmtp(Swift_Transport $smtp)
  93      {
  94          $this->_transport = $smtp;
  95      }
  96  
  97      /**
  98       * Set the connection timeout in seconds (default 10).
  99       *
 100       * @param int $timeout
 101       *
 102       * @return Swift_Plugins_PopBeforeSmtpPlugin
 103       */
 104      public function setTimeout($timeout)
 105      {
 106          $this->_timeout = (int) $timeout;
 107  
 108          return $this;
 109      }
 110  
 111      /**
 112       * Set the username to use when connecting (if needed).
 113       *
 114       * @param string $username
 115       *
 116       * @return Swift_Plugins_PopBeforeSmtpPlugin
 117       */
 118      public function setUsername($username)
 119      {
 120          $this->_username = $username;
 121  
 122          return $this;
 123      }
 124  
 125      /**
 126       * Set the password to use when connecting (if needed).
 127       *
 128       * @param string $password
 129       *
 130       * @return Swift_Plugins_PopBeforeSmtpPlugin
 131       */
 132      public function setPassword($password)
 133      {
 134          $this->_password = $password;
 135  
 136          return $this;
 137      }
 138  
 139      /**
 140       * Connect to the POP3 host and authenticate.
 141       *
 142       * @throws Swift_Plugins_Pop_Pop3Exception if connection fails
 143       */
 144      public function connect()
 145      {
 146          if (isset($this->_connection)) {
 147              $this->_connection->connect();
 148          } else {
 149              if (!isset($this->_socket)) {
 150                  if (!$socket = fsockopen(
 151                      $this->_getHostString(), $this->_port, $errno, $errstr, $this->_timeout)) {
 152                      throw new Swift_Plugins_Pop_Pop3Exception(
 153                          sprintf('Failed to connect to POP3 host [%s]: %s', $this->_host, $errstr)
 154                      );
 155                  }
 156                  $this->_socket = $socket;
 157  
 158                  if (false === $greeting = fgets($this->_socket)) {
 159                      throw new Swift_Plugins_Pop_Pop3Exception(
 160                          sprintf('Failed to connect to POP3 host [%s]', trim($greeting))
 161                      );
 162                  }
 163  
 164                  $this->_assertOk($greeting);
 165  
 166                  if ($this->_username) {
 167                      $this->_command(sprintf("USER %s\r\n", $this->_username));
 168                      $this->_command(sprintf("PASS %s\r\n", $this->_password));
 169                  }
 170              }
 171          }
 172      }
 173  
 174      /**
 175       * Disconnect from the POP3 host.
 176       */
 177      public function disconnect()
 178      {
 179          if (isset($this->_connection)) {
 180              $this->_connection->disconnect();
 181          } else {
 182              $this->_command("QUIT\r\n");
 183              if (!fclose($this->_socket)) {
 184                  throw new Swift_Plugins_Pop_Pop3Exception(
 185                      sprintf('POP3 host [%s] connection could not be stopped', $this->_host)
 186                  );
 187              }
 188              $this->_socket = null;
 189          }
 190      }
 191  
 192      /**
 193       * Invoked just before a Transport is started.
 194       *
 195       * @param Swift_Events_TransportChangeEvent $evt
 196       */
 197      public function beforeTransportStarted(Swift_Events_TransportChangeEvent $evt)
 198      {
 199          if (isset($this->_transport)) {
 200              if ($this->_transport !== $evt->getTransport()) {
 201                  return;
 202              }
 203          }
 204  
 205          $this->connect();
 206          $this->disconnect();
 207      }
 208  
 209      /**
 210       * Not used.
 211       */
 212      public function transportStarted(Swift_Events_TransportChangeEvent $evt)
 213      {
 214      }
 215  
 216      /**
 217       * Not used.
 218       */
 219      public function beforeTransportStopped(Swift_Events_TransportChangeEvent $evt)
 220      {
 221      }
 222  
 223      /**
 224       * Not used.
 225       */
 226      public function transportStopped(Swift_Events_TransportChangeEvent $evt)
 227      {
 228      }
 229  
 230      private function _command($command)
 231      {
 232          if (!fwrite($this->_socket, $command)) {
 233              throw new Swift_Plugins_Pop_Pop3Exception(
 234                  sprintf('Failed to write command [%s] to POP3 host', trim($command))
 235              );
 236          }
 237  
 238          if (false === $response = fgets($this->_socket)) {
 239              throw new Swift_Plugins_Pop_Pop3Exception(
 240                  sprintf('Failed to read from POP3 host after command [%s]', trim($command))
 241              );
 242          }
 243  
 244          $this->_assertOk($response);
 245  
 246          return $response;
 247      }
 248  
 249      private function _assertOk($response)
 250      {
 251          if (substr($response, 0, 3) != '+OK') {
 252              throw new Swift_Plugins_Pop_Pop3Exception(
 253                  sprintf('POP3 command failed [%s]', trim($response))
 254              );
 255          }
 256      }
 257  
 258      private function _getHostString()
 259      {
 260          $host = $this->_host;
 261          switch (strtolower($this->_crypto)) {
 262              case 'ssl':
 263                  $host = 'ssl://'.$host;
 264                  break;
 265  
 266              case 'tls':
 267                  $host = 'tls://'.$host;
 268                  break;
 269          }
 270  
 271          return $host;
 272      }
 273  }


Generated: Thu Apr 28 18:38:52 2016 Cross-referenced by PHPXref 0.7.1