1<?php
2
3/**
4 * Swift Mailer PopB4Smtp Authenticator Mechanism
5 * Please read the LICENSE file
6 * @author Chris Corbyn <chris@w3style.co.uk>
7 * @package Swift_Authenticator
8 * @license GNU Lesser General Public License
9 */
10
11require_once dirname(__FILE__) . "/../ClassLoader.php";
12Swift_ClassLoader::load("Swift_Authenticator");
13Swift_ClassLoader::load("Swift_LogContainer");
14
15/**
16 * Swift PopB4Smtp Authenticator
17 * This form of authentication requires a quick connection to be made with a POP3 server before finally connecting to SMTP
18 * @package Swift_Authenticator
19 * @author Chris Corbyn <chris@w3style.co.uk>
20 */
21class Swift_Authenticator_PopB4Smtp implements Swift_Authenticator
22{
23  protected $connection = null;
24  /**
25   * Constructor
26   * @param mixed Swift_Authenticator_PopB4Smtp_Pop3Connection or string FQDN of POP3 server
27   * @param int The remote port number
28   * @param int The level of encryption to use
29   */
30  public function __construct($conn=null, $port=110, $encryption=0)
31  {
32    if (is_object($conn)) $this->connection = $conn;
33    else
34    {
35      Swift_ClassLoader::load("Swift_Authenticator_PopB4Smtp_Pop3Connection");
36      $this->connection = new Swift_Authenticator_PopB4Smtp_Pop3Connection($conn, $port, $encryption);
37    }
38  }
39  /**
40   * Try to authenticate using the username and password
41   * Returns false on failure
42   * @param string The username
43   * @param string The password
44   * @param Swift The instance of Swift this authenticator is used in
45   * @return boolean
46   */
47  public function isAuthenticated($user, $pass, Swift $swift)
48  {
49    $log = Swift_LogContainer::getLog();
50    if ($log->hasLevel(Swift_Log::LOG_EVERYTHING))
51    {
52      $log->add("Trying POP3 Before SMTP authentication.  Disconnecting from SMTP first.");
53    }
54    $swift->disconnect();
55    try {
56      $this->connection->start();
57      $this->connection->assertOk($this->connection->read());
58      $this->connection->write("USER " . $user);
59      $this->connection->assertOk($this->connection->read());
60      $this->connection->write("PASS " . $pass);
61      $this->connection->assertOk($this->connection->read());
62      $this->connection->write("QUIT");
63      $this->connection->assertOk($this->connection->read());
64      $this->connection->stop();
65    } catch (Swift_ConnectionException $e) {
66      if ($log->hasLevel(Swift_Log::LOG_ERRORS))
67      {
68        $log->add($e->getMessage(),Swift_Log::ERROR);
69        $log->add("POP3 authentication failed.");
70      }
71      return false;
72    }
73    $options = $swift->getOptions();
74    $swift->setOptions($options | Swift::NO_POST_CONNECT);
75    $swift->connect();
76    $swift->setOptions($options);
77    return true;
78  }
79  /**
80   * Return the name of the AUTH extension this is for
81   * @return string
82   */
83  public function getAuthExtensionName()
84  {
85    return "*PopB4Smtp";
86  }
87}
88