1<?php
2
3/**
4 * Swift Mailer Connection Base Class
5 * All connection handlers extend this abstract class
6 * Please read the LICENSE file
7 * @author Chris Corbyn <chris@w3style.co.uk>
8 * @package Swift_Connection
9 * @license GNU Lesser General Public License
10 */
11
12require_once dirname(__FILE__) . "/ClassLoader.php";
13Swift_ClassLoader::load("Swift_LogContainer");
14Swift_ClassLoader::load("Swift_Connection");
15Swift_ClassLoader::load("Swift_ConnectionException");
16
17/**
18 * Swift Connection Base Class
19 * @package Swift_Connection
20 * @author Chris Corbyn <chris@w3style.co.uk>
21 */
22abstract class Swift_ConnectionBase implements Swift_Connection
23{
24  /**
25   * Any extensions the server might support
26   * @var array
27   */
28  protected $extensions = array();
29  /**
30   * True if the connection is ESMTP.
31   * @var boolean
32   */
33  protected $isESMTP = false;
34
35  /**
36   * Set an extension which the connection reports to support
37   * @param string Extension name
38   * @param array Attributes of the extension
39   */
40  public function setExtension($name, $options=array())
41  {
42    $this->extensions[$name] = $options;
43    $log = Swift_LogContainer::getLog();
44    if ($log->hasLevel(Swift_Log::LOG_EVERYTHING))
45    {
46      $log->add("SMTP extension '" . $name . "' reported with attributes [" . implode(", ", $options) . "].");
47    }
48  }
49  /**
50   * Check if a given extension has been set as available
51   * @param string The name of the extension
52   * @return boolean
53   */
54  public function hasExtension($name)
55  {
56    return array_key_exists($name, $this->extensions);
57  }
58  /**
59   * Execute any needed logic after connecting and handshaking
60   */
61  public function postConnect(Swift $instance) {}
62  /**
63   * Get the list of attributes supported by the given extension
64   * @param string The name of the connection
65   * @return array The list of attributes
66   * @throws Swift_ConnectionException If the extension cannot be found
67   */
68  public function getAttributes($extension)
69  {
70    if ($this->hasExtension($extension))
71    {
72      return $this->extensions[$extension];
73    }
74    else
75    {
76      throw new Swift_ConnectionException(
77      "Unable to locate any attributes for the extension '" . $extension . "' since the extension cannot be found. " .
78      "Consider using hasExtension() to check.");
79    }
80  }
81  /**
82   * Returns TRUE if the connection needs a EHLO greeting.
83   * @return boolean
84   */
85  public function getRequiresEHLO()
86  {
87    return $this->isESMTP;
88  }
89  /**
90   * Set TRUE if the connection needs a EHLO greeting.
91   * @param boolean
92   */
93  public function setRequiresEHLO($set)
94  {
95    $this->isESMTP = (bool) $set;
96    $log = Swift_LogContainer::getLog();
97    if ($log->hasLevel(Swift_Log::LOG_EVERYTHING))
98    {
99      $log->add("Forcing ESMTP mode.  HELO is EHLO.");
100    }
101  }
102}
103