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