PageRenderTime 38ms CodeModel.GetById 18ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/vendor/swift/lib/classes/Swift/Transport/Esmtp/AuthHandler.php

http://zoop.googlecode.com/
PHP | 262 lines | 114 code | 30 blank | 118 comment | 6 complexity | f9001d6205520b89bea8ef1ef9869d7b MD5 | raw file
  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//@require 'Swift/TransportException.php';
 12//@require 'Swift/Transport/EsmtpHandler.php';
 13//@require 'Swift/Transport/SmtpAgent.php';
 14
 15/**
 16 * An ESMTP handler for AUTH support.
 17 * @package Swift
 18 * @subpackage Transport
 19 * @author Chris Corbyn
 20 */
 21class Swift_Transport_Esmtp_AuthHandler implements Swift_Transport_EsmtpHandler
 22{
 23  
 24  /**
 25   * Authenticators available to process the request.
 26   * @var Swift_Transport_Esmtp_Authenticator[]
 27   * @access private
 28   */
 29  private $_authenticators = array();
 30  
 31  /**
 32   * The username for authentication.
 33   * @var string
 34   * @access private
 35   */
 36  private $_username;
 37  
 38  /**
 39   * The password for authentication.
 40   * @var string
 41   * @access private
 42   */
 43  private $_password;
 44  
 45  /**
 46   * The auth mode for authentication.
 47   * @var string
 48   * @access private
 49   */
 50  private $_auth_mode;
 51  
 52  /**
 53   * The ESMTP AUTH parameters available.
 54   * @var string[]
 55   * @access private
 56   */
 57  private $_esmtpParams = array();
 58  
 59  /**
 60   * Create a new AuthHandler with $authenticators for support.
 61   * @param Swift_Transport_Esmtp_Authenticator[] $authenticators
 62   */
 63  public function __construct(array $authenticators)
 64  {
 65    $this->setAuthenticators($authenticators);
 66  }
 67  
 68  /**
 69   * Set the Authenticators which can process a login request.
 70   * @param Swift_Transport_Esmtp_Authenticator[] $authenticators
 71   */
 72  public function setAuthenticators(array $authenticators)
 73  {
 74    $this->_authenticators = $authenticators;
 75  }
 76  
 77  /**
 78   * Get the Authenticators which can process a login request.
 79   * @return Swift_Transport_Esmtp_Authenticator[]
 80   */
 81  public function getAuthenticators()
 82  {
 83    return $this->_authenticators;
 84  }
 85  
 86  /**
 87   * Set the username to authenticate with.
 88   * @param string $username
 89   */
 90  public function setUsername($username)
 91  {
 92    $this->_username = $username;
 93  }
 94  
 95  /**
 96   * Get the username to authenticate with.
 97   * @return string
 98   */
 99  public function getUsername()
100  {
101    return $this->_username;
102  }
103  
104  /**
105   * Set the password to authenticate with.
106   * @param string $password
107   */
108  public function setPassword($password)
109  {
110    $this->_password = $password;
111  }
112  
113  /**
114   * Get the password to authenticate with.
115   * @return string
116   */
117  public function getPassword()
118  {
119    return $this->_password;
120  }
121  
122  /**
123   * Set the auth mode to use to authenticate.
124   * @param string $mode
125   */
126  public function setAuthMode($mode)
127  {
128    $this->_auth_mode = $mode;
129  }
130  
131  /**
132   * Get the auth mode to use to authenticate.
133   * @return string
134   */
135  public function getAuthMode()
136  {
137    return $this->_auth_mode;
138  }
139  
140  /**
141   * Get the name of the ESMTP extension this handles.
142   * @return boolean
143   */
144  public function getHandledKeyword()
145  {
146    return 'AUTH';
147  }
148  
149  /**
150   * Set the parameters which the EHLO greeting indicated.
151   * @param string[] $parameters
152   */
153  public function setKeywordParams(array $parameters)
154  {
155    $this->_esmtpParams = $parameters;
156  }
157  
158  /**
159   * Runs immediately after a EHLO has been issued.
160   * @param Swift_Transport_SmtpAgent $agent to read/write
161   */
162  public function afterEhlo(Swift_Transport_SmtpAgent $agent)
163  {
164    if ($this->_username)
165    {
166      $count = 0;
167      foreach ($this->_getAuthenticatorsForAgent() as $authenticator)
168      {
169        if (in_array(strtolower($authenticator->getAuthKeyword()),
170          array_map('strtolower', $this->_esmtpParams)))
171        {
172          $count++;
173          if ($authenticator->authenticate($agent, $this->_username, $this->_password))
174          {
175            return;
176          }
177        }
178      }
179      throw new Swift_TransportException(
180        'Failed to authenticate on SMTP server with username "' .
181        $this->_username . '" using ' . $count . ' possible authenticators'
182        );
183    }
184  }
185  
186  /**
187   * Not used.
188   */
189  public function getMailParams()
190  {
191    return array();
192  }
193  
194  /**
195   * Not used.
196   */
197  public function getRcptParams()
198  {
199    return array();
200  }
201  
202  /**
203   * Not used.
204   */
205  public function onCommand(Swift_Transport_SmtpAgent $agent,
206    $command, $codes = array(), &$failedRecipients = null, &$stop = false)
207  {
208  }
209    
210  /**
211   * Returns +1, -1 or 0 according to the rules for usort().
212   * This method is called to ensure extensions can be execute in an appropriate order.
213   * @param string $esmtpKeyword to compare with
214   * @return int
215   */
216  public function getPriorityOver($esmtpKeyword)
217  {
218    return 0;
219  }
220  
221  /**
222   * Returns an array of method names which are exposed to the Esmtp class.
223   * @return string[]
224   */
225  public function exposeMixinMethods()
226  {
227    return array('setUsername', 'getUsername', 'setPassword', 'getPassword', 'setAuthMode', 'getAuthMode');
228  }
229  
230  /**
231   * Not used.
232   */
233  public function resetState()
234  {
235  }
236  
237  // -- Protected methods
238  
239  /**
240   * Returns the authenticator list for the given agent.
241   * @param  Swift_Transport_SmtpAgent $agent
242   * @return array
243   * @access protected
244   */
245  protected function _getAuthenticatorsForAgent()
246  {
247    if (!$mode = strtolower($this->_auth_mode))
248    {
249      return $this->_authenticators;
250    }
251
252    foreach ($this->_authenticators as $authenticator)
253    {
254      if (strtolower($authenticator->getAuthKeyword()) == $mode)
255      {
256        return array($authenticator);
257      }
258    }
259
260    throw new Swift_TransportException('Auth mode '.$mode.' is invalid');
261  }
262}