PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/application/vendor/Swift/classes/Swift/Transport/Esmtp/AuthHandler.php

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