[ Index ] |
MailPress 7.2 |
[ Index ] [ Classes ] [ Functions ] [ Variables ] [ Constants ] [ Statistics ] |
[Summary view] [Print] [Text view]
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 (RFC 5248). 13 * 14 * @author Chris Corbyn 15 */ 16 class Swift_Transport_Esmtp_AuthHandler implements Swift_Transport_EsmtpHandler 17 { 18 /** 19 * Authenticators available to process the request. 20 * 21 * @var Swift_Transport_Esmtp_Authenticator[] 22 */ 23 private $authenticators = []; 24 25 /** 26 * The username for authentication. 27 * 28 * @var string 29 */ 30 private $username; 31 32 /** 33 * The password for authentication. 34 * 35 * @var string 36 */ 37 private $password; 38 39 /** 40 * The auth mode for authentication. 41 * 42 * @var string 43 */ 44 private $auth_mode; 45 46 /** 47 * The ESMTP AUTH parameters available. 48 * 49 * @var string[] 50 */ 51 private $esmtpParams = []; 52 53 /** 54 * Create a new AuthHandler with $authenticators for support. 55 * 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 * 66 * @param Swift_Transport_Esmtp_Authenticator[] $authenticators 67 */ 68 public function setAuthenticators(array $authenticators) 69 { 70 $this->authenticators = $authenticators; 71 } 72 73 /** 74 * Get the Authenticators which can process a login request. 75 * 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 * 86 * @param string $username 87 */ 88 public function setUsername($username) 89 { 90 $this->username = $username; 91 } 92 93 /** 94 * Get the username to authenticate with. 95 * 96 * @return string 97 */ 98 public function getUsername() 99 { 100 return $this->username; 101 } 102 103 /** 104 * Set the password to authenticate with. 105 * 106 * @param string $password 107 */ 108 public function setPassword($password) 109 { 110 $this->password = $password; 111 } 112 113 /** 114 * Get the password to authenticate with. 115 * 116 * @return string 117 */ 118 public function getPassword() 119 { 120 return $this->password; 121 } 122 123 /** 124 * Set the auth mode to use to authenticate. 125 * 126 * @param string $mode 127 */ 128 public function setAuthMode($mode) 129 { 130 $this->auth_mode = $mode; 131 } 132 133 /** 134 * Get the auth mode to use to authenticate. 135 * 136 * @return string 137 */ 138 public function getAuthMode() 139 { 140 return $this->auth_mode; 141 } 142 143 /** 144 * Get the name of the ESMTP extension this handles. 145 * 146 * @return string 147 */ 148 public function getHandledKeyword() 149 { 150 return 'AUTH'; 151 } 152 153 /** 154 * Set the parameters which the EHLO greeting indicated. 155 * 156 * @param string[] $parameters 157 */ 158 public function setKeywordParams(array $parameters) 159 { 160 $this->esmtpParams = $parameters; 161 } 162 163 /** 164 * Runs immediately after a EHLO has been issued. 165 * 166 * @param Swift_Transport_SmtpAgent $agent to read/write 167 */ 168 public function afterEhlo(Swift_Transport_SmtpAgent $agent) 169 { 170 if ($this->username) { 171 $count = 0; 172 $errors = []; 173 foreach ($this->getAuthenticatorsForAgent() as $authenticator) { 174 if (in_array(strtolower($authenticator->getAuthKeyword()), array_map('strtolower', $this->esmtpParams))) { 175 ++$count; 176 try { 177 if ($authenticator->authenticate($agent, $this->username, $this->password)) { 178 return; 179 } 180 } catch (Swift_TransportException $e) { 181 // keep the error message, but tries the other authenticators 182 $errors[] = [$authenticator->getAuthKeyword(), $e->getMessage()]; 183 } 184 } 185 } 186 187 $message = 'Failed to authenticate on SMTP server with username "'.$this->username.'" using '.$count.' possible authenticators.'; 188 foreach ($errors as $error) { 189 $message .= ' Authenticator '.$error[0].' returned '.$error[1].'.'; 190 } 191 throw new Swift_TransportException($message); 192 } 193 } 194 195 /** 196 * Not used. 197 */ 198 public function getMailParams() 199 { 200 return []; 201 } 202 203 /** 204 * Not used. 205 */ 206 public function getRcptParams() 207 { 208 return []; 209 } 210 211 /** 212 * Not used. 213 */ 214 public function onCommand(Swift_Transport_SmtpAgent $agent, $command, $codes = [], &$failedRecipients = null, &$stop = false) 215 { 216 } 217 218 /** 219 * Returns +1, -1 or 0 according to the rules for usort(). 220 * 221 * This method is called to ensure extensions can be execute in an appropriate order. 222 * 223 * @param string $esmtpKeyword to compare with 224 * 225 * @return int 226 */ 227 public function getPriorityOver($esmtpKeyword) 228 { 229 return 0; 230 } 231 232 /** 233 * Returns an array of method names which are exposed to the Esmtp class. 234 * 235 * @return string[] 236 */ 237 public function exposeMixinMethods() 238 { 239 return ['setUsername', 'getUsername', 'setPassword', 'getPassword', 'setAuthMode', 'getAuthMode']; 240 } 241 242 /** 243 * Not used. 244 */ 245 public function resetState() 246 { 247 } 248 249 /** 250 * Returns the authenticator list for the given agent. 251 * 252 * @return array 253 */ 254 protected function getAuthenticatorsForAgent() 255 { 256 if (!$mode = strtolower($this->auth_mode)) { 257 return $this->authenticators; 258 } 259 260 foreach ($this->authenticators as $authenticator) { 261 if (strtolower($authenticator->getAuthKeyword()) == $mode) { 262 return [$authenticator]; 263 } 264 } 265 266 throw new Swift_TransportException('Auth mode '.$mode.' is invalid'); 267 } 268 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Tue May 19 15:55:14 2020 | Cross-referenced by PHPXref 0.7.1 |