AuthHandler.php 5.8 KB

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