TYPO3 API  SVNRelease
AuthHandler.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /*
00004  * This file is part of SwiftMailer.
00005  * (c) 2004-2009 Chris Corbyn
00006  *
00007  * For the full copyright and license information, please view the LICENSE
00008  * file that was distributed with this source code.
00009  */
00010 
00011 //@require 'Swift/TransportException.php';
00012 //@require 'Swift/Transport/EsmtpHandler.php';
00013 //@require 'Swift/Transport/SmtpAgent.php';
00014 
00015 /**
00016  * An ESMTP handler for AUTH support.
00017  * @package Swift
00018  * @subpackage Transport
00019  * @author Chris Corbyn
00020  */
00021 class Swift_Transport_Esmtp_AuthHandler implements Swift_Transport_EsmtpHandler
00022 {
00023 
00024   /**
00025    * Authenticators available to process the request.
00026    * @var Swift_Transport_Esmtp_Authenticator[]
00027    * @access private
00028    */
00029   private $_authenticators = array();
00030 
00031   /**
00032    * The username for authentication.
00033    * @var string
00034    * @access private
00035    */
00036   private $_username;
00037 
00038   /**
00039    * The password for authentication.
00040    * @var string
00041    * @access private
00042    */
00043   private $_password;
00044 
00045   /**
00046    * The auth mode for authentication.
00047    * @var string
00048    * @access private
00049    */
00050   private $_auth_mode;
00051 
00052   /**
00053    * The ESMTP AUTH parameters available.
00054    * @var string[]
00055    * @access private
00056    */
00057   private $_esmtpParams = array();
00058 
00059   /**
00060    * Create a new AuthHandler with $authenticators for support.
00061    * @param Swift_Transport_Esmtp_Authenticator[] $authenticators
00062    */
00063   public function __construct(array $authenticators)
00064   {
00065     $this->setAuthenticators($authenticators);
00066   }
00067 
00068   /**
00069    * Set the Authenticators which can process a login request.
00070    * @param Swift_Transport_Esmtp_Authenticator[] $authenticators
00071    */
00072   public function setAuthenticators(array $authenticators)
00073   {
00074     $this->_authenticators = $authenticators;
00075   }
00076 
00077   /**
00078    * Get the Authenticators which can process a login request.
00079    * @return Swift_Transport_Esmtp_Authenticator[]
00080    */
00081   public function getAuthenticators()
00082   {
00083     return $this->_authenticators;
00084   }
00085 
00086   /**
00087    * Set the username to authenticate with.
00088    * @param string $username
00089    */
00090   public function setUsername($username)
00091   {
00092     $this->_username = $username;
00093   }
00094 
00095   /**
00096    * Get the username to authenticate with.
00097    * @return string
00098    */
00099   public function getUsername()
00100   {
00101     return $this->_username;
00102   }
00103 
00104   /**
00105    * Set the password to authenticate with.
00106    * @param string $password
00107    */
00108   public function setPassword($password)
00109   {
00110     $this->_password = $password;
00111   }
00112 
00113   /**
00114    * Get the password to authenticate with.
00115    * @return string
00116    */
00117   public function getPassword()
00118   {
00119     return $this->_password;
00120   }
00121 
00122   /**
00123    * Set the auth mode to use to authenticate.
00124    * @param string $mode
00125    */
00126   public function setAuthMode($mode)
00127   {
00128     $this->_auth_mode = $mode;
00129   }
00130 
00131   /**
00132    * Get the auth mode to use to authenticate.
00133    * @return string
00134    */
00135   public function getAuthMode()
00136   {
00137     return $this->_auth_mode;
00138   }
00139 
00140   /**
00141    * Get the name of the ESMTP extension this handles.
00142    * @return boolean
00143    */
00144   public function getHandledKeyword()
00145   {
00146     return 'AUTH';
00147   }
00148 
00149   /**
00150    * Set the parameters which the EHLO greeting indicated.
00151    * @param string[] $parameters
00152    */
00153   public function setKeywordParams(array $parameters)
00154   {
00155     $this->_esmtpParams = $parameters;
00156   }
00157 
00158   /**
00159    * Runs immediately after a EHLO has been issued.
00160    * @param Swift_Transport_SmtpAgent $agent to read/write
00161    */
00162   public function afterEhlo(Swift_Transport_SmtpAgent $agent)
00163   {
00164     if ($this->_username)
00165     {
00166       $count = 0;
00167       foreach ($this->_getAuthenticatorsForAgent() as $authenticator)
00168       {
00169         if (in_array(strtolower($authenticator->getAuthKeyword()),
00170           array_map('strtolower', $this->_esmtpParams)))
00171         {
00172           $count++;
00173           if ($authenticator->authenticate($agent, $this->_username, $this->_password))
00174           {
00175             return;
00176           }
00177         }
00178       }
00179       throw new Swift_TransportException(
00180         'Failed to authenticate on SMTP server with username "' .
00181         $this->_username . '" using ' . $count . ' possible authenticators'
00182         );
00183     }
00184   }
00185 
00186   /**
00187    * Not used.
00188    */
00189   public function getMailParams()
00190   {
00191     return array();
00192   }
00193 
00194   /**
00195    * Not used.
00196    */
00197   public function getRcptParams()
00198   {
00199     return array();
00200   }
00201 
00202   /**
00203    * Not used.
00204    */
00205   public function onCommand(Swift_Transport_SmtpAgent $agent,
00206     $command, $codes = array(), &$failedRecipients = null, &$stop = false)
00207   {
00208   }
00209 
00210   /**
00211    * Returns +1, -1 or 0 according to the rules for usort().
00212    * This method is called to ensure extensions can be execute in an appropriate order.
00213    * @param string $esmtpKeyword to compare with
00214    * @return int
00215    */
00216   public function getPriorityOver($esmtpKeyword)
00217   {
00218     return 0;
00219   }
00220 
00221   /**
00222    * Returns an array of method names which are exposed to the Esmtp class.
00223    * @return string[]
00224    */
00225   public function exposeMixinMethods()
00226   {
00227     return array('setUsername', 'getUsername', 'setPassword', 'getPassword', 'setAuthMode', 'getAuthMode');
00228   }
00229 
00230   /**
00231    * Not used.
00232    */
00233   public function resetState()
00234   {
00235   }
00236 
00237   // -- Protected methods
00238 
00239   /**
00240    * Returns the authenticator list for the given agent.
00241    * @param  Swift_Transport_SmtpAgent $agent
00242    * @return array
00243    * @access protected
00244    */
00245   protected function _getAuthenticatorsForAgent()
00246   {
00247     if (!$mode = strtolower($this->_auth_mode))
00248     {
00249       return $this->_authenticators;
00250     }
00251 
00252     foreach ($this->_authenticators as $authenticator)
00253     {
00254       if (strtolower($authenticator->getAuthKeyword()) == $mode)
00255       {
00256         return array($authenticator);
00257       }
00258     }
00259 
00260     throw new Swift_TransportException('Auth mode '.$mode.' is invalid');
00261   }
00262 }