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