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