[ Index ]

MailPress 7.2

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

title

Body

[close]

/mp-includes/composer/vendor/swiftmailer/swiftmailer/lib/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   Hostname or IP. Literal IPv6 addresses should be
  49       *                       wrapped in square brackets.
  50       * @param int    $port
  51       * @param string $crypto 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      /**
  61       * Set a Pop3Connection to delegate to instead of connecting directly.
  62       *
  63       * @return $this
  64       */
  65      public function setConnection(Swift_Plugins_Pop_Pop3Connection $connection)
  66      {
  67          $this->connection = $connection;
  68  
  69          return $this;
  70      }
  71  
  72      /**
  73       * Bind this plugin to a specific SMTP transport instance.
  74       */
  75      public function bindSmtp(Swift_Transport $smtp)
  76      {
  77          $this->transport = $smtp;
  78      }
  79  
  80      /**
  81       * Set the connection timeout in seconds (default 10).
  82       *
  83       * @param int $timeout
  84       *
  85       * @return $this
  86       */
  87      public function setTimeout($timeout)
  88      {
  89          $this->timeout = (int) $timeout;
  90  
  91          return $this;
  92      }
  93  
  94      /**
  95       * Set the username to use when connecting (if needed).
  96       *
  97       * @param string $username
  98       *
  99       * @return $this
 100       */
 101      public function setUsername($username)
 102      {
 103          $this->username = $username;
 104  
 105          return $this;
 106      }
 107  
 108      /**
 109       * Set the password to use when connecting (if needed).
 110       *
 111       * @param string $password
 112       *
 113       * @return $this
 114       */
 115      public function setPassword($password)
 116      {
 117          $this->password = $password;
 118  
 119          return $this;
 120      }
 121  
 122      /**
 123       * Connect to the POP3 host and authenticate.
 124       *
 125       * @throws Swift_Plugins_Pop_Pop3Exception if connection fails
 126       */
 127      public function connect()
 128      {
 129          if (isset($this->connection)) {
 130              $this->connection->connect();
 131          } else {
 132              if (!isset($this->socket)) {
 133                  if (!$socket = fsockopen(
 134                      $this->getHostString(), $this->port, $errno, $errstr, $this->timeout)) {
 135                      throw new Swift_Plugins_Pop_Pop3Exception(sprintf('Failed to connect to POP3 host [%s]: %s', $this->host, $errstr));
 136                  }
 137                  $this->socket = $socket;
 138  
 139                  if (false === $greeting = fgets($this->socket)) {
 140                      throw new Swift_Plugins_Pop_Pop3Exception(sprintf('Failed to connect to POP3 host [%s]', trim($greeting)));
 141                  }
 142  
 143                  $this->assertOk($greeting);
 144  
 145                  if ($this->username) {
 146                      $this->command(sprintf("USER %s\r\n", $this->username));
 147                      $this->command(sprintf("PASS %s\r\n", $this->password));
 148                  }
 149              }
 150          }
 151      }
 152  
 153      /**
 154       * Disconnect from the POP3 host.
 155       */
 156      public function disconnect()
 157      {
 158          if (isset($this->connection)) {
 159              $this->connection->disconnect();
 160          } else {
 161              $this->command("QUIT\r\n");
 162              if (!fclose($this->socket)) {
 163                  throw new Swift_Plugins_Pop_Pop3Exception(sprintf('POP3 host [%s] connection could not be stopped', $this->host));
 164              }
 165              $this->socket = null;
 166          }
 167      }
 168  
 169      /**
 170       * Invoked just before a Transport is started.
 171       */
 172      public function beforeTransportStarted(Swift_Events_TransportChangeEvent $evt)
 173      {
 174          if (isset($this->transport)) {
 175              if ($this->transport !== $evt->getTransport()) {
 176                  return;
 177              }
 178          }
 179  
 180          $this->connect();
 181          $this->disconnect();
 182      }
 183  
 184      /**
 185       * Not used.
 186       */
 187      public function transportStarted(Swift_Events_TransportChangeEvent $evt)
 188      {
 189      }
 190  
 191      /**
 192       * Not used.
 193       */
 194      public function beforeTransportStopped(Swift_Events_TransportChangeEvent $evt)
 195      {
 196      }
 197  
 198      /**
 199       * Not used.
 200       */
 201      public function transportStopped(Swift_Events_TransportChangeEvent $evt)
 202      {
 203      }
 204  
 205      private function command($command)
 206      {
 207          if (!fwrite($this->socket, $command)) {
 208              throw new Swift_Plugins_Pop_Pop3Exception(sprintf('Failed to write command [%s] to POP3 host', trim($command)));
 209          }
 210  
 211          if (false === $response = fgets($this->socket)) {
 212              throw new Swift_Plugins_Pop_Pop3Exception(sprintf('Failed to read from POP3 host after command [%s]', trim($command)));
 213          }
 214  
 215          $this->assertOk($response);
 216  
 217          return $response;
 218      }
 219  
 220      private function assertOk($response)
 221      {
 222          if ('+OK' != substr($response, 0, 3)) {
 223              throw new Swift_Plugins_Pop_Pop3Exception(sprintf('POP3 command failed [%s]', trim($response)));
 224          }
 225      }
 226  
 227      private function getHostString()
 228      {
 229          $host = $this->host;
 230          switch (strtolower($this->crypto)) {
 231              case 'ssl':
 232                  $host = 'ssl://'.$host;
 233                  break;
 234  
 235              case 'tls':
 236                  $host = 'tls://'.$host;
 237                  break;
 238          }
 239  
 240          return $host;
 241      }
 242  }


Generated: Tue May 19 15:55:14 2020 Cross-referenced by PHPXref 0.7.1