@@ -15,527 +15,527 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | abstract class Swift_Transport_AbstractSmtpTransport implements Swift_Transport |
| 17 | 17 | { |
| 18 | - /** Input-Output buffer for sending/receiving SMTP commands and responses */ |
|
| 19 | - protected $buffer; |
|
| 20 | - |
|
| 21 | - /** Connection status */ |
|
| 22 | - protected $started = false; |
|
| 23 | - |
|
| 24 | - /** The domain name to use in HELO command */ |
|
| 25 | - protected $domain = '[127.0.0.1]'; |
|
| 26 | - |
|
| 27 | - /** The event dispatching layer */ |
|
| 28 | - protected $eventDispatcher; |
|
| 29 | - |
|
| 30 | - protected $addressEncoder; |
|
| 31 | - |
|
| 32 | - /** Whether the PIPELINING SMTP extension is enabled (RFC 2920) */ |
|
| 33 | - protected $pipelining = null; |
|
| 34 | - |
|
| 35 | - /** The pipelined commands waiting for response */ |
|
| 36 | - protected $pipeline = []; |
|
| 37 | - |
|
| 38 | - /** Source Ip */ |
|
| 39 | - protected $sourceIp; |
|
| 40 | - |
|
| 41 | - /** Return an array of params for the Buffer */ |
|
| 42 | - abstract protected function getBufferParams(); |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Creates a new EsmtpTransport using the given I/O buffer. |
|
| 46 | - * |
|
| 47 | - * @param string $localDomain |
|
| 48 | - */ |
|
| 49 | - public function __construct(Swift_Transport_IoBuffer $buf, Swift_Events_EventDispatcher $dispatcher, $localDomain = '127.0.0.1', Swift_AddressEncoder $addressEncoder = null) |
|
| 50 | - { |
|
| 51 | - $this->buffer = $buf; |
|
| 52 | - $this->eventDispatcher = $dispatcher; |
|
| 53 | - $this->addressEncoder = $addressEncoder ?? new Swift_AddressEncoder_IdnAddressEncoder(); |
|
| 54 | - $this->setLocalDomain($localDomain); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Set the name of the local domain which Swift will identify itself as. |
|
| 59 | - * |
|
| 60 | - * This should be a fully-qualified domain name and should be truly the domain |
|
| 61 | - * you're using. |
|
| 62 | - * |
|
| 63 | - * If your server does not have a domain name, use the IP address. This will |
|
| 64 | - * automatically be wrapped in square brackets as described in RFC 5321, |
|
| 65 | - * section 4.1.3. |
|
| 66 | - * |
|
| 67 | - * @param string $domain |
|
| 68 | - * |
|
| 69 | - * @return $this |
|
| 70 | - */ |
|
| 71 | - public function setLocalDomain($domain) |
|
| 72 | - { |
|
| 73 | - if ('[' !== substr($domain, 0, 1)) { |
|
| 74 | - if (filter_var($domain, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
|
| 75 | - $domain = '['.$domain.']'; |
|
| 76 | - } elseif (filter_var($domain, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
|
| 77 | - $domain = '[IPv6:'.$domain.']'; |
|
| 78 | - } |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - $this->domain = $domain; |
|
| 82 | - |
|
| 83 | - return $this; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Get the name of the domain Swift will identify as. |
|
| 88 | - * |
|
| 89 | - * If an IP address was specified, this will be returned wrapped in square |
|
| 90 | - * brackets as described in RFC 5321, section 4.1.3. |
|
| 91 | - * |
|
| 92 | - * @return string |
|
| 93 | - */ |
|
| 94 | - public function getLocalDomain() |
|
| 95 | - { |
|
| 96 | - return $this->domain; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Sets the source IP. |
|
| 101 | - * |
|
| 102 | - * @param string $source |
|
| 103 | - */ |
|
| 104 | - public function setSourceIp($source) |
|
| 105 | - { |
|
| 106 | - $this->sourceIp = $source; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Returns the IP used to connect to the destination. |
|
| 111 | - * |
|
| 112 | - * @return string |
|
| 113 | - */ |
|
| 114 | - public function getSourceIp() |
|
| 115 | - { |
|
| 116 | - return $this->sourceIp; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - public function setAddressEncoder(Swift_AddressEncoder $addressEncoder) |
|
| 120 | - { |
|
| 121 | - $this->addressEncoder = $addressEncoder; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - public function getAddressEncoder() |
|
| 125 | - { |
|
| 126 | - return $this->addressEncoder; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Start the SMTP connection. |
|
| 131 | - */ |
|
| 132 | - public function start() |
|
| 133 | - { |
|
| 134 | - if (!$this->started) { |
|
| 135 | - if ($evt = $this->eventDispatcher->createTransportChangeEvent($this)) { |
|
| 136 | - $this->eventDispatcher->dispatchEvent($evt, 'beforeTransportStarted'); |
|
| 137 | - if ($evt->bubbleCancelled()) { |
|
| 138 | - return; |
|
| 139 | - } |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - try { |
|
| 143 | - $this->buffer->initialize($this->getBufferParams()); |
|
| 144 | - } catch (Swift_TransportException $e) { |
|
| 145 | - $this->throwException($e); |
|
| 146 | - } |
|
| 147 | - $this->readGreeting(); |
|
| 148 | - $this->doHeloCommand(); |
|
| 149 | - |
|
| 150 | - if ($evt) { |
|
| 151 | - $this->eventDispatcher->dispatchEvent($evt, 'transportStarted'); |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - $this->started = true; |
|
| 155 | - } |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Test if an SMTP connection has been established. |
|
| 160 | - * |
|
| 161 | - * @return bool |
|
| 162 | - */ |
|
| 163 | - public function isStarted() |
|
| 164 | - { |
|
| 165 | - return $this->started; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * Send the given Message. |
|
| 170 | - * |
|
| 171 | - * Recipient/sender data will be retrieved from the Message API. |
|
| 172 | - * The return value is the number of recipients who were accepted for delivery. |
|
| 173 | - * |
|
| 174 | - * @param string[] $failedRecipients An array of failures by-reference |
|
| 175 | - * |
|
| 176 | - * @return int |
|
| 177 | - */ |
|
| 178 | - public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null) |
|
| 179 | - { |
|
| 180 | - if (!$this->isStarted()) { |
|
| 181 | - $this->start(); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - $sent = 0; |
|
| 185 | - $failedRecipients = (array) $failedRecipients; |
|
| 186 | - |
|
| 187 | - if ($evt = $this->eventDispatcher->createSendEvent($this, $message)) { |
|
| 188 | - $this->eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed'); |
|
| 189 | - if ($evt->bubbleCancelled()) { |
|
| 190 | - return 0; |
|
| 191 | - } |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - if (!$reversePath = $this->getReversePath($message)) { |
|
| 195 | - $this->throwException(new Swift_TransportException('Cannot send message without a sender address')); |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - $to = (array) $message->getTo(); |
|
| 199 | - $cc = (array) $message->getCc(); |
|
| 200 | - $bcc = (array) $message->getBcc(); |
|
| 201 | - $tos = array_merge($to, $cc, $bcc); |
|
| 202 | - |
|
| 203 | - $message->setBcc([]); |
|
| 204 | - |
|
| 205 | - try { |
|
| 206 | - $sent += $this->sendTo($message, $reversePath, $tos, $failedRecipients); |
|
| 207 | - } finally { |
|
| 208 | - $message->setBcc($bcc); |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - if ($evt) { |
|
| 212 | - if ($sent == \count($to) + \count($cc) + \count($bcc)) { |
|
| 213 | - $evt->setResult(Swift_Events_SendEvent::RESULT_SUCCESS); |
|
| 214 | - } elseif ($sent > 0) { |
|
| 215 | - $evt->setResult(Swift_Events_SendEvent::RESULT_TENTATIVE); |
|
| 216 | - } else { |
|
| 217 | - $evt->setResult(Swift_Events_SendEvent::RESULT_FAILED); |
|
| 218 | - } |
|
| 219 | - $evt->setFailedRecipients($failedRecipients); |
|
| 220 | - $this->eventDispatcher->dispatchEvent($evt, 'sendPerformed'); |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - $message->generateId(); //Make sure a new Message ID is used |
|
| 224 | - |
|
| 225 | - return $sent; |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - /** |
|
| 229 | - * Stop the SMTP connection. |
|
| 230 | - */ |
|
| 231 | - public function stop() |
|
| 232 | - { |
|
| 233 | - if ($this->started) { |
|
| 234 | - if ($evt = $this->eventDispatcher->createTransportChangeEvent($this)) { |
|
| 235 | - $this->eventDispatcher->dispatchEvent($evt, 'beforeTransportStopped'); |
|
| 236 | - if ($evt->bubbleCancelled()) { |
|
| 237 | - return; |
|
| 238 | - } |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - try { |
|
| 242 | - $this->executeCommand("QUIT\r\n", [221]); |
|
| 243 | - } catch (Swift_TransportException $e) { |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - try { |
|
| 247 | - $this->buffer->terminate(); |
|
| 248 | - |
|
| 249 | - if ($evt) { |
|
| 250 | - $this->eventDispatcher->dispatchEvent($evt, 'transportStopped'); |
|
| 251 | - } |
|
| 252 | - } catch (Swift_TransportException $e) { |
|
| 253 | - $this->throwException($e); |
|
| 254 | - } |
|
| 255 | - } |
|
| 256 | - $this->started = false; |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * {@inheritdoc} |
|
| 261 | - */ |
|
| 262 | - public function ping() |
|
| 263 | - { |
|
| 264 | - try { |
|
| 265 | - if (!$this->isStarted()) { |
|
| 266 | - $this->start(); |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - $this->executeCommand("NOOP\r\n", [250]); |
|
| 270 | - } catch (Swift_TransportException $e) { |
|
| 271 | - try { |
|
| 272 | - $this->stop(); |
|
| 273 | - } catch (Swift_TransportException $e) { |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - return false; |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - return true; |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - /** |
|
| 283 | - * Register a plugin. |
|
| 284 | - */ |
|
| 285 | - public function registerPlugin(Swift_Events_EventListener $plugin) |
|
| 286 | - { |
|
| 287 | - $this->eventDispatcher->bindEventListener($plugin); |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Reset the current mail transaction. |
|
| 292 | - */ |
|
| 293 | - public function reset() |
|
| 294 | - { |
|
| 295 | - $this->executeCommand("RSET\r\n", [250], $failures, true); |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - /** |
|
| 299 | - * Get the IoBuffer where read/writes are occurring. |
|
| 300 | - * |
|
| 301 | - * @return Swift_Transport_IoBuffer |
|
| 302 | - */ |
|
| 303 | - public function getBuffer() |
|
| 304 | - { |
|
| 305 | - return $this->buffer; |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * Run a command against the buffer, expecting the given response codes. |
|
| 310 | - * |
|
| 311 | - * If no response codes are given, the response will not be validated. |
|
| 312 | - * If codes are given, an exception will be thrown on an invalid response. |
|
| 313 | - * If the command is RCPT TO, and the pipeline is non-empty, no exception |
|
| 314 | - * will be thrown; instead the failing address is added to $failures. |
|
| 315 | - * |
|
| 316 | - * @param string $command |
|
| 317 | - * @param int[] $codes |
|
| 318 | - * @param string[] $failures An array of failures by-reference |
|
| 319 | - * @param bool $pipeline Do not wait for response |
|
| 320 | - * @param string $address the address, if command is RCPT TO |
|
| 321 | - * |
|
| 322 | - * @return string|null The server response, or null if pipelining is enabled |
|
| 323 | - */ |
|
| 324 | - public function executeCommand($command, $codes = [], &$failures = null, $pipeline = false, $address = null) |
|
| 325 | - { |
|
| 326 | - $failures = (array) $failures; |
|
| 327 | - $seq = $this->buffer->write($command); |
|
| 328 | - if ($evt = $this->eventDispatcher->createCommandEvent($this, $command, $codes)) { |
|
| 329 | - $this->eventDispatcher->dispatchEvent($evt, 'commandSent'); |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - $this->pipeline[] = [$command, $seq, $codes, $address]; |
|
| 333 | - |
|
| 334 | - if ($pipeline && $this->pipelining) { |
|
| 335 | - return null; |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - $response = null; |
|
| 339 | - |
|
| 340 | - while ($this->pipeline) { |
|
| 341 | - list($command, $seq, $codes, $address) = array_shift($this->pipeline); |
|
| 342 | - $response = $this->getFullResponse($seq); |
|
| 343 | - try { |
|
| 344 | - $this->assertResponseCode($response, $codes); |
|
| 345 | - } catch (Swift_TransportException $e) { |
|
| 346 | - if ($this->pipeline && $address) { |
|
| 347 | - $failures[] = $address; |
|
| 348 | - } else { |
|
| 349 | - $this->throwException($e); |
|
| 350 | - } |
|
| 351 | - } |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - return $response; |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - /** Read the opening SMTP greeting */ |
|
| 358 | - protected function readGreeting() |
|
| 359 | - { |
|
| 360 | - $this->assertResponseCode($this->getFullResponse(0), [220]); |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - /** Send the HELO welcome */ |
|
| 364 | - protected function doHeloCommand() |
|
| 365 | - { |
|
| 366 | - $this->executeCommand( |
|
| 367 | - sprintf("HELO %s\r\n", $this->domain), [250] |
|
| 368 | - ); |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - /** Send the MAIL FROM command */ |
|
| 372 | - protected function doMailFromCommand($address) |
|
| 373 | - { |
|
| 374 | - $address = $this->addressEncoder->encodeString($address); |
|
| 375 | - $this->executeCommand( |
|
| 376 | - sprintf("MAIL FROM:<%s>\r\n", $address), [250], $failures, true |
|
| 377 | - ); |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - /** Send the RCPT TO command */ |
|
| 381 | - protected function doRcptToCommand($address) |
|
| 382 | - { |
|
| 383 | - $address = $this->addressEncoder->encodeString($address); |
|
| 384 | - $this->executeCommand( |
|
| 385 | - sprintf("RCPT TO:<%s>\r\n", $address), [250, 251, 252], $failures, true, $address |
|
| 386 | - ); |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - /** Send the DATA command */ |
|
| 390 | - protected function doDataCommand(&$failedRecipients) |
|
| 391 | - { |
|
| 392 | - $this->executeCommand("DATA\r\n", [354], $failedRecipients); |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - /** Stream the contents of the message over the buffer */ |
|
| 396 | - protected function streamMessage(Swift_Mime_SimpleMessage $message) |
|
| 397 | - { |
|
| 398 | - $this->buffer->setWriteTranslations(["\r\n." => "\r\n.."]); |
|
| 399 | - try { |
|
| 400 | - $message->toByteStream($this->buffer); |
|
| 401 | - $this->buffer->flushBuffers(); |
|
| 402 | - } catch (Swift_TransportException $e) { |
|
| 403 | - $this->throwException($e); |
|
| 404 | - } |
|
| 405 | - $this->buffer->setWriteTranslations([]); |
|
| 406 | - $this->executeCommand("\r\n.\r\n", [250]); |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - /** Determine the best-use reverse path for this message */ |
|
| 410 | - protected function getReversePath(Swift_Mime_SimpleMessage $message) |
|
| 411 | - { |
|
| 412 | - $return = $message->getReturnPath(); |
|
| 413 | - $sender = $message->getSender(); |
|
| 414 | - $from = $message->getFrom(); |
|
| 415 | - $path = null; |
|
| 416 | - if (!empty($return)) { |
|
| 417 | - $path = $return; |
|
| 418 | - } elseif (!empty($sender)) { |
|
| 419 | - // Don't use array_keys |
|
| 420 | - reset($sender); // Reset Pointer to first pos |
|
| 421 | - $path = key($sender); // Get key |
|
| 422 | - } elseif (!empty($from)) { |
|
| 423 | - reset($from); // Reset Pointer to first pos |
|
| 424 | - $path = key($from); // Get key |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - return $path; |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - /** Throw a TransportException, first sending it to any listeners */ |
|
| 431 | - protected function throwException(Swift_TransportException $e) |
|
| 432 | - { |
|
| 433 | - if ($evt = $this->eventDispatcher->createTransportExceptionEvent($this, $e)) { |
|
| 434 | - $this->eventDispatcher->dispatchEvent($evt, 'exceptionThrown'); |
|
| 435 | - if (!$evt->bubbleCancelled()) { |
|
| 436 | - throw $e; |
|
| 437 | - } |
|
| 438 | - } else { |
|
| 439 | - throw $e; |
|
| 440 | - } |
|
| 441 | - } |
|
| 442 | - |
|
| 443 | - /** Throws an Exception if a response code is incorrect */ |
|
| 444 | - protected function assertResponseCode($response, $wanted) |
|
| 445 | - { |
|
| 446 | - if (!$response) { |
|
| 447 | - $this->throwException(new Swift_TransportException('Expected response code '.implode('/', $wanted).' but got an empty response')); |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - list($code) = sscanf($response, '%3d'); |
|
| 451 | - $valid = (empty($wanted) || \in_array($code, $wanted)); |
|
| 452 | - |
|
| 453 | - if ($evt = $this->eventDispatcher->createResponseEvent($this, $response, |
|
| 454 | - $valid)) { |
|
| 455 | - $this->eventDispatcher->dispatchEvent($evt, 'responseReceived'); |
|
| 456 | - } |
|
| 457 | - |
|
| 458 | - if (!$valid) { |
|
| 459 | - $this->throwException(new Swift_TransportException('Expected response code '.implode('/', $wanted).' but got code "'.$code.'", with message "'.$response.'"', $code)); |
|
| 460 | - } |
|
| 461 | - } |
|
| 462 | - |
|
| 463 | - /** Get an entire multi-line response using its sequence number */ |
|
| 464 | - protected function getFullResponse($seq) |
|
| 465 | - { |
|
| 466 | - $response = ''; |
|
| 467 | - try { |
|
| 468 | - do { |
|
| 469 | - $line = $this->buffer->readLine($seq); |
|
| 470 | - $response .= $line; |
|
| 471 | - } while (null !== $line && false !== $line && ' ' != $line[3]); |
|
| 472 | - } catch (Swift_TransportException $e) { |
|
| 473 | - $this->throwException($e); |
|
| 474 | - } catch (Swift_IoException $e) { |
|
| 475 | - $this->throwException(new Swift_TransportException($e->getMessage(), 0, $e)); |
|
| 476 | - } |
|
| 477 | - |
|
| 478 | - return $response; |
|
| 479 | - } |
|
| 480 | - |
|
| 481 | - /** Send an email to the given recipients from the given reverse path */ |
|
| 482 | - private function doMailTransaction($message, $reversePath, array $recipients, array &$failedRecipients) |
|
| 483 | - { |
|
| 484 | - $sent = 0; |
|
| 485 | - $this->doMailFromCommand($reversePath); |
|
| 486 | - foreach ($recipients as $forwardPath) { |
|
| 487 | - try { |
|
| 488 | - $this->doRcptToCommand($forwardPath); |
|
| 489 | - ++$sent; |
|
| 490 | - } catch (Swift_TransportException $e) { |
|
| 491 | - $failedRecipients[] = $forwardPath; |
|
| 492 | - } catch (Swift_AddressEncoderException $e) { |
|
| 493 | - $failedRecipients[] = $forwardPath; |
|
| 494 | - } |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - if (0 != $sent) { |
|
| 498 | - $sent += \count($failedRecipients); |
|
| 499 | - $this->doDataCommand($failedRecipients); |
|
| 500 | - $sent -= \count($failedRecipients); |
|
| 501 | - |
|
| 502 | - $this->streamMessage($message); |
|
| 503 | - } else { |
|
| 504 | - $this->reset(); |
|
| 505 | - } |
|
| 506 | - |
|
| 507 | - return $sent; |
|
| 508 | - } |
|
| 509 | - |
|
| 510 | - /** Send a message to the given To: recipients */ |
|
| 511 | - private function sendTo(Swift_Mime_SimpleMessage $message, $reversePath, array $to, array &$failedRecipients) |
|
| 512 | - { |
|
| 513 | - if (empty($to)) { |
|
| 514 | - return 0; |
|
| 515 | - } |
|
| 516 | - |
|
| 517 | - return $this->doMailTransaction($message, $reversePath, array_keys($to), |
|
| 518 | - $failedRecipients); |
|
| 519 | - } |
|
| 520 | - |
|
| 521 | - /** |
|
| 522 | - * Destructor. |
|
| 523 | - */ |
|
| 524 | - public function __destruct() |
|
| 525 | - { |
|
| 526 | - try { |
|
| 527 | - $this->stop(); |
|
| 528 | - } catch (Exception $e) { |
|
| 529 | - } |
|
| 530 | - } |
|
| 531 | - |
|
| 532 | - public function __sleep() |
|
| 533 | - { |
|
| 534 | - throw new \BadMethodCallException('Cannot serialize '.__CLASS__); |
|
| 535 | - } |
|
| 536 | - |
|
| 537 | - public function __wakeup() |
|
| 538 | - { |
|
| 539 | - throw new \BadMethodCallException('Cannot unserialize '.__CLASS__); |
|
| 540 | - } |
|
| 18 | + /** Input-Output buffer for sending/receiving SMTP commands and responses */ |
|
| 19 | + protected $buffer; |
|
| 20 | + |
|
| 21 | + /** Connection status */ |
|
| 22 | + protected $started = false; |
|
| 23 | + |
|
| 24 | + /** The domain name to use in HELO command */ |
|
| 25 | + protected $domain = '[127.0.0.1]'; |
|
| 26 | + |
|
| 27 | + /** The event dispatching layer */ |
|
| 28 | + protected $eventDispatcher; |
|
| 29 | + |
|
| 30 | + protected $addressEncoder; |
|
| 31 | + |
|
| 32 | + /** Whether the PIPELINING SMTP extension is enabled (RFC 2920) */ |
|
| 33 | + protected $pipelining = null; |
|
| 34 | + |
|
| 35 | + /** The pipelined commands waiting for response */ |
|
| 36 | + protected $pipeline = []; |
|
| 37 | + |
|
| 38 | + /** Source Ip */ |
|
| 39 | + protected $sourceIp; |
|
| 40 | + |
|
| 41 | + /** Return an array of params for the Buffer */ |
|
| 42 | + abstract protected function getBufferParams(); |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Creates a new EsmtpTransport using the given I/O buffer. |
|
| 46 | + * |
|
| 47 | + * @param string $localDomain |
|
| 48 | + */ |
|
| 49 | + public function __construct(Swift_Transport_IoBuffer $buf, Swift_Events_EventDispatcher $dispatcher, $localDomain = '127.0.0.1', Swift_AddressEncoder $addressEncoder = null) |
|
| 50 | + { |
|
| 51 | + $this->buffer = $buf; |
|
| 52 | + $this->eventDispatcher = $dispatcher; |
|
| 53 | + $this->addressEncoder = $addressEncoder ?? new Swift_AddressEncoder_IdnAddressEncoder(); |
|
| 54 | + $this->setLocalDomain($localDomain); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Set the name of the local domain which Swift will identify itself as. |
|
| 59 | + * |
|
| 60 | + * This should be a fully-qualified domain name and should be truly the domain |
|
| 61 | + * you're using. |
|
| 62 | + * |
|
| 63 | + * If your server does not have a domain name, use the IP address. This will |
|
| 64 | + * automatically be wrapped in square brackets as described in RFC 5321, |
|
| 65 | + * section 4.1.3. |
|
| 66 | + * |
|
| 67 | + * @param string $domain |
|
| 68 | + * |
|
| 69 | + * @return $this |
|
| 70 | + */ |
|
| 71 | + public function setLocalDomain($domain) |
|
| 72 | + { |
|
| 73 | + if ('[' !== substr($domain, 0, 1)) { |
|
| 74 | + if (filter_var($domain, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
|
| 75 | + $domain = '['.$domain.']'; |
|
| 76 | + } elseif (filter_var($domain, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
|
| 77 | + $domain = '[IPv6:'.$domain.']'; |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + $this->domain = $domain; |
|
| 82 | + |
|
| 83 | + return $this; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Get the name of the domain Swift will identify as. |
|
| 88 | + * |
|
| 89 | + * If an IP address was specified, this will be returned wrapped in square |
|
| 90 | + * brackets as described in RFC 5321, section 4.1.3. |
|
| 91 | + * |
|
| 92 | + * @return string |
|
| 93 | + */ |
|
| 94 | + public function getLocalDomain() |
|
| 95 | + { |
|
| 96 | + return $this->domain; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Sets the source IP. |
|
| 101 | + * |
|
| 102 | + * @param string $source |
|
| 103 | + */ |
|
| 104 | + public function setSourceIp($source) |
|
| 105 | + { |
|
| 106 | + $this->sourceIp = $source; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Returns the IP used to connect to the destination. |
|
| 111 | + * |
|
| 112 | + * @return string |
|
| 113 | + */ |
|
| 114 | + public function getSourceIp() |
|
| 115 | + { |
|
| 116 | + return $this->sourceIp; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + public function setAddressEncoder(Swift_AddressEncoder $addressEncoder) |
|
| 120 | + { |
|
| 121 | + $this->addressEncoder = $addressEncoder; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + public function getAddressEncoder() |
|
| 125 | + { |
|
| 126 | + return $this->addressEncoder; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Start the SMTP connection. |
|
| 131 | + */ |
|
| 132 | + public function start() |
|
| 133 | + { |
|
| 134 | + if (!$this->started) { |
|
| 135 | + if ($evt = $this->eventDispatcher->createTransportChangeEvent($this)) { |
|
| 136 | + $this->eventDispatcher->dispatchEvent($evt, 'beforeTransportStarted'); |
|
| 137 | + if ($evt->bubbleCancelled()) { |
|
| 138 | + return; |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + try { |
|
| 143 | + $this->buffer->initialize($this->getBufferParams()); |
|
| 144 | + } catch (Swift_TransportException $e) { |
|
| 145 | + $this->throwException($e); |
|
| 146 | + } |
|
| 147 | + $this->readGreeting(); |
|
| 148 | + $this->doHeloCommand(); |
|
| 149 | + |
|
| 150 | + if ($evt) { |
|
| 151 | + $this->eventDispatcher->dispatchEvent($evt, 'transportStarted'); |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + $this->started = true; |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Test if an SMTP connection has been established. |
|
| 160 | + * |
|
| 161 | + * @return bool |
|
| 162 | + */ |
|
| 163 | + public function isStarted() |
|
| 164 | + { |
|
| 165 | + return $this->started; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * Send the given Message. |
|
| 170 | + * |
|
| 171 | + * Recipient/sender data will be retrieved from the Message API. |
|
| 172 | + * The return value is the number of recipients who were accepted for delivery. |
|
| 173 | + * |
|
| 174 | + * @param string[] $failedRecipients An array of failures by-reference |
|
| 175 | + * |
|
| 176 | + * @return int |
|
| 177 | + */ |
|
| 178 | + public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null) |
|
| 179 | + { |
|
| 180 | + if (!$this->isStarted()) { |
|
| 181 | + $this->start(); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + $sent = 0; |
|
| 185 | + $failedRecipients = (array) $failedRecipients; |
|
| 186 | + |
|
| 187 | + if ($evt = $this->eventDispatcher->createSendEvent($this, $message)) { |
|
| 188 | + $this->eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed'); |
|
| 189 | + if ($evt->bubbleCancelled()) { |
|
| 190 | + return 0; |
|
| 191 | + } |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + if (!$reversePath = $this->getReversePath($message)) { |
|
| 195 | + $this->throwException(new Swift_TransportException('Cannot send message without a sender address')); |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + $to = (array) $message->getTo(); |
|
| 199 | + $cc = (array) $message->getCc(); |
|
| 200 | + $bcc = (array) $message->getBcc(); |
|
| 201 | + $tos = array_merge($to, $cc, $bcc); |
|
| 202 | + |
|
| 203 | + $message->setBcc([]); |
|
| 204 | + |
|
| 205 | + try { |
|
| 206 | + $sent += $this->sendTo($message, $reversePath, $tos, $failedRecipients); |
|
| 207 | + } finally { |
|
| 208 | + $message->setBcc($bcc); |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + if ($evt) { |
|
| 212 | + if ($sent == \count($to) + \count($cc) + \count($bcc)) { |
|
| 213 | + $evt->setResult(Swift_Events_SendEvent::RESULT_SUCCESS); |
|
| 214 | + } elseif ($sent > 0) { |
|
| 215 | + $evt->setResult(Swift_Events_SendEvent::RESULT_TENTATIVE); |
|
| 216 | + } else { |
|
| 217 | + $evt->setResult(Swift_Events_SendEvent::RESULT_FAILED); |
|
| 218 | + } |
|
| 219 | + $evt->setFailedRecipients($failedRecipients); |
|
| 220 | + $this->eventDispatcher->dispatchEvent($evt, 'sendPerformed'); |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + $message->generateId(); //Make sure a new Message ID is used |
|
| 224 | + |
|
| 225 | + return $sent; |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + /** |
|
| 229 | + * Stop the SMTP connection. |
|
| 230 | + */ |
|
| 231 | + public function stop() |
|
| 232 | + { |
|
| 233 | + if ($this->started) { |
|
| 234 | + if ($evt = $this->eventDispatcher->createTransportChangeEvent($this)) { |
|
| 235 | + $this->eventDispatcher->dispatchEvent($evt, 'beforeTransportStopped'); |
|
| 236 | + if ($evt->bubbleCancelled()) { |
|
| 237 | + return; |
|
| 238 | + } |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + try { |
|
| 242 | + $this->executeCommand("QUIT\r\n", [221]); |
|
| 243 | + } catch (Swift_TransportException $e) { |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + try { |
|
| 247 | + $this->buffer->terminate(); |
|
| 248 | + |
|
| 249 | + if ($evt) { |
|
| 250 | + $this->eventDispatcher->dispatchEvent($evt, 'transportStopped'); |
|
| 251 | + } |
|
| 252 | + } catch (Swift_TransportException $e) { |
|
| 253 | + $this->throwException($e); |
|
| 254 | + } |
|
| 255 | + } |
|
| 256 | + $this->started = false; |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * {@inheritdoc} |
|
| 261 | + */ |
|
| 262 | + public function ping() |
|
| 263 | + { |
|
| 264 | + try { |
|
| 265 | + if (!$this->isStarted()) { |
|
| 266 | + $this->start(); |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + $this->executeCommand("NOOP\r\n", [250]); |
|
| 270 | + } catch (Swift_TransportException $e) { |
|
| 271 | + try { |
|
| 272 | + $this->stop(); |
|
| 273 | + } catch (Swift_TransportException $e) { |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + return false; |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + return true; |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + /** |
|
| 283 | + * Register a plugin. |
|
| 284 | + */ |
|
| 285 | + public function registerPlugin(Swift_Events_EventListener $plugin) |
|
| 286 | + { |
|
| 287 | + $this->eventDispatcher->bindEventListener($plugin); |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Reset the current mail transaction. |
|
| 292 | + */ |
|
| 293 | + public function reset() |
|
| 294 | + { |
|
| 295 | + $this->executeCommand("RSET\r\n", [250], $failures, true); |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + /** |
|
| 299 | + * Get the IoBuffer where read/writes are occurring. |
|
| 300 | + * |
|
| 301 | + * @return Swift_Transport_IoBuffer |
|
| 302 | + */ |
|
| 303 | + public function getBuffer() |
|
| 304 | + { |
|
| 305 | + return $this->buffer; |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * Run a command against the buffer, expecting the given response codes. |
|
| 310 | + * |
|
| 311 | + * If no response codes are given, the response will not be validated. |
|
| 312 | + * If codes are given, an exception will be thrown on an invalid response. |
|
| 313 | + * If the command is RCPT TO, and the pipeline is non-empty, no exception |
|
| 314 | + * will be thrown; instead the failing address is added to $failures. |
|
| 315 | + * |
|
| 316 | + * @param string $command |
|
| 317 | + * @param int[] $codes |
|
| 318 | + * @param string[] $failures An array of failures by-reference |
|
| 319 | + * @param bool $pipeline Do not wait for response |
|
| 320 | + * @param string $address the address, if command is RCPT TO |
|
| 321 | + * |
|
| 322 | + * @return string|null The server response, or null if pipelining is enabled |
|
| 323 | + */ |
|
| 324 | + public function executeCommand($command, $codes = [], &$failures = null, $pipeline = false, $address = null) |
|
| 325 | + { |
|
| 326 | + $failures = (array) $failures; |
|
| 327 | + $seq = $this->buffer->write($command); |
|
| 328 | + if ($evt = $this->eventDispatcher->createCommandEvent($this, $command, $codes)) { |
|
| 329 | + $this->eventDispatcher->dispatchEvent($evt, 'commandSent'); |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + $this->pipeline[] = [$command, $seq, $codes, $address]; |
|
| 333 | + |
|
| 334 | + if ($pipeline && $this->pipelining) { |
|
| 335 | + return null; |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + $response = null; |
|
| 339 | + |
|
| 340 | + while ($this->pipeline) { |
|
| 341 | + list($command, $seq, $codes, $address) = array_shift($this->pipeline); |
|
| 342 | + $response = $this->getFullResponse($seq); |
|
| 343 | + try { |
|
| 344 | + $this->assertResponseCode($response, $codes); |
|
| 345 | + } catch (Swift_TransportException $e) { |
|
| 346 | + if ($this->pipeline && $address) { |
|
| 347 | + $failures[] = $address; |
|
| 348 | + } else { |
|
| 349 | + $this->throwException($e); |
|
| 350 | + } |
|
| 351 | + } |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + return $response; |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + /** Read the opening SMTP greeting */ |
|
| 358 | + protected function readGreeting() |
|
| 359 | + { |
|
| 360 | + $this->assertResponseCode($this->getFullResponse(0), [220]); |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + /** Send the HELO welcome */ |
|
| 364 | + protected function doHeloCommand() |
|
| 365 | + { |
|
| 366 | + $this->executeCommand( |
|
| 367 | + sprintf("HELO %s\r\n", $this->domain), [250] |
|
| 368 | + ); |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + /** Send the MAIL FROM command */ |
|
| 372 | + protected function doMailFromCommand($address) |
|
| 373 | + { |
|
| 374 | + $address = $this->addressEncoder->encodeString($address); |
|
| 375 | + $this->executeCommand( |
|
| 376 | + sprintf("MAIL FROM:<%s>\r\n", $address), [250], $failures, true |
|
| 377 | + ); |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + /** Send the RCPT TO command */ |
|
| 381 | + protected function doRcptToCommand($address) |
|
| 382 | + { |
|
| 383 | + $address = $this->addressEncoder->encodeString($address); |
|
| 384 | + $this->executeCommand( |
|
| 385 | + sprintf("RCPT TO:<%s>\r\n", $address), [250, 251, 252], $failures, true, $address |
|
| 386 | + ); |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + /** Send the DATA command */ |
|
| 390 | + protected function doDataCommand(&$failedRecipients) |
|
| 391 | + { |
|
| 392 | + $this->executeCommand("DATA\r\n", [354], $failedRecipients); |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + /** Stream the contents of the message over the buffer */ |
|
| 396 | + protected function streamMessage(Swift_Mime_SimpleMessage $message) |
|
| 397 | + { |
|
| 398 | + $this->buffer->setWriteTranslations(["\r\n." => "\r\n.."]); |
|
| 399 | + try { |
|
| 400 | + $message->toByteStream($this->buffer); |
|
| 401 | + $this->buffer->flushBuffers(); |
|
| 402 | + } catch (Swift_TransportException $e) { |
|
| 403 | + $this->throwException($e); |
|
| 404 | + } |
|
| 405 | + $this->buffer->setWriteTranslations([]); |
|
| 406 | + $this->executeCommand("\r\n.\r\n", [250]); |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + /** Determine the best-use reverse path for this message */ |
|
| 410 | + protected function getReversePath(Swift_Mime_SimpleMessage $message) |
|
| 411 | + { |
|
| 412 | + $return = $message->getReturnPath(); |
|
| 413 | + $sender = $message->getSender(); |
|
| 414 | + $from = $message->getFrom(); |
|
| 415 | + $path = null; |
|
| 416 | + if (!empty($return)) { |
|
| 417 | + $path = $return; |
|
| 418 | + } elseif (!empty($sender)) { |
|
| 419 | + // Don't use array_keys |
|
| 420 | + reset($sender); // Reset Pointer to first pos |
|
| 421 | + $path = key($sender); // Get key |
|
| 422 | + } elseif (!empty($from)) { |
|
| 423 | + reset($from); // Reset Pointer to first pos |
|
| 424 | + $path = key($from); // Get key |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + return $path; |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + /** Throw a TransportException, first sending it to any listeners */ |
|
| 431 | + protected function throwException(Swift_TransportException $e) |
|
| 432 | + { |
|
| 433 | + if ($evt = $this->eventDispatcher->createTransportExceptionEvent($this, $e)) { |
|
| 434 | + $this->eventDispatcher->dispatchEvent($evt, 'exceptionThrown'); |
|
| 435 | + if (!$evt->bubbleCancelled()) { |
|
| 436 | + throw $e; |
|
| 437 | + } |
|
| 438 | + } else { |
|
| 439 | + throw $e; |
|
| 440 | + } |
|
| 441 | + } |
|
| 442 | + |
|
| 443 | + /** Throws an Exception if a response code is incorrect */ |
|
| 444 | + protected function assertResponseCode($response, $wanted) |
|
| 445 | + { |
|
| 446 | + if (!$response) { |
|
| 447 | + $this->throwException(new Swift_TransportException('Expected response code '.implode('/', $wanted).' but got an empty response')); |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + list($code) = sscanf($response, '%3d'); |
|
| 451 | + $valid = (empty($wanted) || \in_array($code, $wanted)); |
|
| 452 | + |
|
| 453 | + if ($evt = $this->eventDispatcher->createResponseEvent($this, $response, |
|
| 454 | + $valid)) { |
|
| 455 | + $this->eventDispatcher->dispatchEvent($evt, 'responseReceived'); |
|
| 456 | + } |
|
| 457 | + |
|
| 458 | + if (!$valid) { |
|
| 459 | + $this->throwException(new Swift_TransportException('Expected response code '.implode('/', $wanted).' but got code "'.$code.'", with message "'.$response.'"', $code)); |
|
| 460 | + } |
|
| 461 | + } |
|
| 462 | + |
|
| 463 | + /** Get an entire multi-line response using its sequence number */ |
|
| 464 | + protected function getFullResponse($seq) |
|
| 465 | + { |
|
| 466 | + $response = ''; |
|
| 467 | + try { |
|
| 468 | + do { |
|
| 469 | + $line = $this->buffer->readLine($seq); |
|
| 470 | + $response .= $line; |
|
| 471 | + } while (null !== $line && false !== $line && ' ' != $line[3]); |
|
| 472 | + } catch (Swift_TransportException $e) { |
|
| 473 | + $this->throwException($e); |
|
| 474 | + } catch (Swift_IoException $e) { |
|
| 475 | + $this->throwException(new Swift_TransportException($e->getMessage(), 0, $e)); |
|
| 476 | + } |
|
| 477 | + |
|
| 478 | + return $response; |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + /** Send an email to the given recipients from the given reverse path */ |
|
| 482 | + private function doMailTransaction($message, $reversePath, array $recipients, array &$failedRecipients) |
|
| 483 | + { |
|
| 484 | + $sent = 0; |
|
| 485 | + $this->doMailFromCommand($reversePath); |
|
| 486 | + foreach ($recipients as $forwardPath) { |
|
| 487 | + try { |
|
| 488 | + $this->doRcptToCommand($forwardPath); |
|
| 489 | + ++$sent; |
|
| 490 | + } catch (Swift_TransportException $e) { |
|
| 491 | + $failedRecipients[] = $forwardPath; |
|
| 492 | + } catch (Swift_AddressEncoderException $e) { |
|
| 493 | + $failedRecipients[] = $forwardPath; |
|
| 494 | + } |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + if (0 != $sent) { |
|
| 498 | + $sent += \count($failedRecipients); |
|
| 499 | + $this->doDataCommand($failedRecipients); |
|
| 500 | + $sent -= \count($failedRecipients); |
|
| 501 | + |
|
| 502 | + $this->streamMessage($message); |
|
| 503 | + } else { |
|
| 504 | + $this->reset(); |
|
| 505 | + } |
|
| 506 | + |
|
| 507 | + return $sent; |
|
| 508 | + } |
|
| 509 | + |
|
| 510 | + /** Send a message to the given To: recipients */ |
|
| 511 | + private function sendTo(Swift_Mime_SimpleMessage $message, $reversePath, array $to, array &$failedRecipients) |
|
| 512 | + { |
|
| 513 | + if (empty($to)) { |
|
| 514 | + return 0; |
|
| 515 | + } |
|
| 516 | + |
|
| 517 | + return $this->doMailTransaction($message, $reversePath, array_keys($to), |
|
| 518 | + $failedRecipients); |
|
| 519 | + } |
|
| 520 | + |
|
| 521 | + /** |
|
| 522 | + * Destructor. |
|
| 523 | + */ |
|
| 524 | + public function __destruct() |
|
| 525 | + { |
|
| 526 | + try { |
|
| 527 | + $this->stop(); |
|
| 528 | + } catch (Exception $e) { |
|
| 529 | + } |
|
| 530 | + } |
|
| 531 | + |
|
| 532 | + public function __sleep() |
|
| 533 | + { |
|
| 534 | + throw new \BadMethodCallException('Cannot serialize '.__CLASS__); |
|
| 535 | + } |
|
| 536 | + |
|
| 537 | + public function __wakeup() |
|
| 538 | + { |
|
| 539 | + throw new \BadMethodCallException('Cannot unserialize '.__CLASS__); |
|
| 540 | + } |
|
| 541 | 541 | } |
@@ -15,178 +15,178 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Swift_Transport_LoadBalancedTransport implements Swift_Transport |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Transports which are deemed useless. |
|
| 20 | - * |
|
| 21 | - * @var Swift_Transport[] |
|
| 22 | - */ |
|
| 23 | - private $deadTransports = []; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * The Transports which are used in rotation. |
|
| 27 | - * |
|
| 28 | - * @var Swift_Transport[] |
|
| 29 | - */ |
|
| 30 | - protected $transports = []; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * The Transport used in the last successful send operation. |
|
| 34 | - * |
|
| 35 | - * @var Swift_Transport |
|
| 36 | - */ |
|
| 37 | - protected $lastUsedTransport = null; |
|
| 38 | - |
|
| 39 | - // needed as __construct is called from elsewhere explicitly |
|
| 40 | - public function __construct() |
|
| 41 | - { |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Set $transports to delegate to. |
|
| 46 | - * |
|
| 47 | - * @param Swift_Transport[] $transports |
|
| 48 | - */ |
|
| 49 | - public function setTransports(array $transports) |
|
| 50 | - { |
|
| 51 | - $this->transports = $transports; |
|
| 52 | - $this->deadTransports = []; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Get $transports to delegate to. |
|
| 57 | - * |
|
| 58 | - * @return Swift_Transport[] |
|
| 59 | - */ |
|
| 60 | - public function getTransports() |
|
| 61 | - { |
|
| 62 | - return array_merge($this->transports, $this->deadTransports); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Get the Transport used in the last successful send operation. |
|
| 67 | - * |
|
| 68 | - * @return Swift_Transport |
|
| 69 | - */ |
|
| 70 | - public function getLastUsedTransport() |
|
| 71 | - { |
|
| 72 | - return $this->lastUsedTransport; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Test if this Transport mechanism has started. |
|
| 77 | - * |
|
| 78 | - * @return bool |
|
| 79 | - */ |
|
| 80 | - public function isStarted() |
|
| 81 | - { |
|
| 82 | - return \count($this->transports) > 0; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Start this Transport mechanism. |
|
| 87 | - */ |
|
| 88 | - public function start() |
|
| 89 | - { |
|
| 90 | - $this->transports = array_merge($this->transports, $this->deadTransports); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Stop this Transport mechanism. |
|
| 95 | - */ |
|
| 96 | - public function stop() |
|
| 97 | - { |
|
| 98 | - foreach ($this->transports as $transport) { |
|
| 99 | - $transport->stop(); |
|
| 100 | - } |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * {@inheritdoc} |
|
| 105 | - */ |
|
| 106 | - public function ping() |
|
| 107 | - { |
|
| 108 | - foreach ($this->transports as $transport) { |
|
| 109 | - if (!$transport->ping()) { |
|
| 110 | - $this->killCurrentTransport(); |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - return \count($this->transports) > 0; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Send the given Message. |
|
| 119 | - * |
|
| 120 | - * Recipient/sender data will be retrieved from the Message API. |
|
| 121 | - * The return value is the number of recipients who were accepted for delivery. |
|
| 122 | - * |
|
| 123 | - * @param string[] $failedRecipients An array of failures by-reference |
|
| 124 | - * |
|
| 125 | - * @return int |
|
| 126 | - */ |
|
| 127 | - public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null) |
|
| 128 | - { |
|
| 129 | - $maxTransports = \count($this->transports); |
|
| 130 | - $sent = 0; |
|
| 131 | - $this->lastUsedTransport = null; |
|
| 132 | - |
|
| 133 | - for ($i = 0; $i < $maxTransports |
|
| 134 | - && $transport = $this->getNextTransport(); ++$i) { |
|
| 135 | - try { |
|
| 136 | - if (!$transport->isStarted()) { |
|
| 137 | - $transport->start(); |
|
| 138 | - } |
|
| 139 | - if ($sent = $transport->send($message, $failedRecipients)) { |
|
| 140 | - $this->lastUsedTransport = $transport; |
|
| 141 | - break; |
|
| 142 | - } |
|
| 143 | - } catch (Swift_TransportException $e) { |
|
| 144 | - $this->killCurrentTransport(); |
|
| 145 | - } |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - if (0 == \count($this->transports)) { |
|
| 149 | - throw new Swift_TransportException('All Transports in LoadBalancedTransport failed, or no Transports available'); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - return $sent; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Register a plugin. |
|
| 157 | - */ |
|
| 158 | - public function registerPlugin(Swift_Events_EventListener $plugin) |
|
| 159 | - { |
|
| 160 | - foreach ($this->transports as $transport) { |
|
| 161 | - $transport->registerPlugin($plugin); |
|
| 162 | - } |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Rotates the transport list around and returns the first instance. |
|
| 167 | - * |
|
| 168 | - * @return Swift_Transport |
|
| 169 | - */ |
|
| 170 | - protected function getNextTransport() |
|
| 171 | - { |
|
| 172 | - if ($next = array_shift($this->transports)) { |
|
| 173 | - $this->transports[] = $next; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - return $next; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * Tag the currently used (top of stack) transport as dead/useless. |
|
| 181 | - */ |
|
| 182 | - protected function killCurrentTransport() |
|
| 183 | - { |
|
| 184 | - if ($transport = array_pop($this->transports)) { |
|
| 185 | - try { |
|
| 186 | - $transport->stop(); |
|
| 187 | - } catch (Exception $e) { |
|
| 188 | - } |
|
| 189 | - $this->deadTransports[] = $transport; |
|
| 190 | - } |
|
| 191 | - } |
|
| 18 | + /** |
|
| 19 | + * Transports which are deemed useless. |
|
| 20 | + * |
|
| 21 | + * @var Swift_Transport[] |
|
| 22 | + */ |
|
| 23 | + private $deadTransports = []; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * The Transports which are used in rotation. |
|
| 27 | + * |
|
| 28 | + * @var Swift_Transport[] |
|
| 29 | + */ |
|
| 30 | + protected $transports = []; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * The Transport used in the last successful send operation. |
|
| 34 | + * |
|
| 35 | + * @var Swift_Transport |
|
| 36 | + */ |
|
| 37 | + protected $lastUsedTransport = null; |
|
| 38 | + |
|
| 39 | + // needed as __construct is called from elsewhere explicitly |
|
| 40 | + public function __construct() |
|
| 41 | + { |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Set $transports to delegate to. |
|
| 46 | + * |
|
| 47 | + * @param Swift_Transport[] $transports |
|
| 48 | + */ |
|
| 49 | + public function setTransports(array $transports) |
|
| 50 | + { |
|
| 51 | + $this->transports = $transports; |
|
| 52 | + $this->deadTransports = []; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Get $transports to delegate to. |
|
| 57 | + * |
|
| 58 | + * @return Swift_Transport[] |
|
| 59 | + */ |
|
| 60 | + public function getTransports() |
|
| 61 | + { |
|
| 62 | + return array_merge($this->transports, $this->deadTransports); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Get the Transport used in the last successful send operation. |
|
| 67 | + * |
|
| 68 | + * @return Swift_Transport |
|
| 69 | + */ |
|
| 70 | + public function getLastUsedTransport() |
|
| 71 | + { |
|
| 72 | + return $this->lastUsedTransport; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Test if this Transport mechanism has started. |
|
| 77 | + * |
|
| 78 | + * @return bool |
|
| 79 | + */ |
|
| 80 | + public function isStarted() |
|
| 81 | + { |
|
| 82 | + return \count($this->transports) > 0; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Start this Transport mechanism. |
|
| 87 | + */ |
|
| 88 | + public function start() |
|
| 89 | + { |
|
| 90 | + $this->transports = array_merge($this->transports, $this->deadTransports); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Stop this Transport mechanism. |
|
| 95 | + */ |
|
| 96 | + public function stop() |
|
| 97 | + { |
|
| 98 | + foreach ($this->transports as $transport) { |
|
| 99 | + $transport->stop(); |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * {@inheritdoc} |
|
| 105 | + */ |
|
| 106 | + public function ping() |
|
| 107 | + { |
|
| 108 | + foreach ($this->transports as $transport) { |
|
| 109 | + if (!$transport->ping()) { |
|
| 110 | + $this->killCurrentTransport(); |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + return \count($this->transports) > 0; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Send the given Message. |
|
| 119 | + * |
|
| 120 | + * Recipient/sender data will be retrieved from the Message API. |
|
| 121 | + * The return value is the number of recipients who were accepted for delivery. |
|
| 122 | + * |
|
| 123 | + * @param string[] $failedRecipients An array of failures by-reference |
|
| 124 | + * |
|
| 125 | + * @return int |
|
| 126 | + */ |
|
| 127 | + public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null) |
|
| 128 | + { |
|
| 129 | + $maxTransports = \count($this->transports); |
|
| 130 | + $sent = 0; |
|
| 131 | + $this->lastUsedTransport = null; |
|
| 132 | + |
|
| 133 | + for ($i = 0; $i < $maxTransports |
|
| 134 | + && $transport = $this->getNextTransport(); ++$i) { |
|
| 135 | + try { |
|
| 136 | + if (!$transport->isStarted()) { |
|
| 137 | + $transport->start(); |
|
| 138 | + } |
|
| 139 | + if ($sent = $transport->send($message, $failedRecipients)) { |
|
| 140 | + $this->lastUsedTransport = $transport; |
|
| 141 | + break; |
|
| 142 | + } |
|
| 143 | + } catch (Swift_TransportException $e) { |
|
| 144 | + $this->killCurrentTransport(); |
|
| 145 | + } |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + if (0 == \count($this->transports)) { |
|
| 149 | + throw new Swift_TransportException('All Transports in LoadBalancedTransport failed, or no Transports available'); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + return $sent; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Register a plugin. |
|
| 157 | + */ |
|
| 158 | + public function registerPlugin(Swift_Events_EventListener $plugin) |
|
| 159 | + { |
|
| 160 | + foreach ($this->transports as $transport) { |
|
| 161 | + $transport->registerPlugin($plugin); |
|
| 162 | + } |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Rotates the transport list around and returns the first instance. |
|
| 167 | + * |
|
| 168 | + * @return Swift_Transport |
|
| 169 | + */ |
|
| 170 | + protected function getNextTransport() |
|
| 171 | + { |
|
| 172 | + if ($next = array_shift($this->transports)) { |
|
| 173 | + $this->transports[] = $next; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + return $next; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Tag the currently used (top of stack) transport as dead/useless. |
|
| 181 | + */ |
|
| 182 | + protected function killCurrentTransport() |
|
| 183 | + { |
|
| 184 | + if ($transport = array_pop($this->transports)) { |
|
| 185 | + try { |
|
| 186 | + $transport->stop(); |
|
| 187 | + } catch (Exception $e) { |
|
| 188 | + } |
|
| 189 | + $this->deadTransports[] = $transport; |
|
| 190 | + } |
|
| 191 | + } |
|
| 192 | 192 | } |
@@ -15,106 +15,106 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Swift_Transport_SpoolTransport implements Swift_Transport |
| 17 | 17 | { |
| 18 | - /** The spool instance */ |
|
| 19 | - private $spool; |
|
| 20 | - |
|
| 21 | - /** The event dispatcher from the plugin API */ |
|
| 22 | - private $eventDispatcher; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Constructor. |
|
| 26 | - */ |
|
| 27 | - public function __construct(Swift_Events_EventDispatcher $eventDispatcher, Swift_Spool $spool = null) |
|
| 28 | - { |
|
| 29 | - $this->eventDispatcher = $eventDispatcher; |
|
| 30 | - $this->spool = $spool; |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Sets the spool object. |
|
| 35 | - * |
|
| 36 | - * @return $this |
|
| 37 | - */ |
|
| 38 | - public function setSpool(Swift_Spool $spool) |
|
| 39 | - { |
|
| 40 | - $this->spool = $spool; |
|
| 41 | - |
|
| 42 | - return $this; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Get the spool object. |
|
| 47 | - * |
|
| 48 | - * @return Swift_Spool |
|
| 49 | - */ |
|
| 50 | - public function getSpool() |
|
| 51 | - { |
|
| 52 | - return $this->spool; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Tests if this Transport mechanism has started. |
|
| 57 | - * |
|
| 58 | - * @return bool |
|
| 59 | - */ |
|
| 60 | - public function isStarted() |
|
| 61 | - { |
|
| 62 | - return true; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Starts this Transport mechanism. |
|
| 67 | - */ |
|
| 68 | - public function start() |
|
| 69 | - { |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Stops this Transport mechanism. |
|
| 74 | - */ |
|
| 75 | - public function stop() |
|
| 76 | - { |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * {@inheritdoc} |
|
| 81 | - */ |
|
| 82 | - public function ping() |
|
| 83 | - { |
|
| 84 | - return true; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Sends the given message. |
|
| 89 | - * |
|
| 90 | - * @param string[] $failedRecipients An array of failures by-reference |
|
| 91 | - * |
|
| 92 | - * @return int The number of sent e-mail's |
|
| 93 | - */ |
|
| 94 | - public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null) |
|
| 95 | - { |
|
| 96 | - if ($evt = $this->eventDispatcher->createSendEvent($this, $message)) { |
|
| 97 | - $this->eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed'); |
|
| 98 | - if ($evt->bubbleCancelled()) { |
|
| 99 | - return 0; |
|
| 100 | - } |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - $success = $this->spool->queueMessage($message); |
|
| 104 | - |
|
| 105 | - if ($evt) { |
|
| 106 | - $evt->setResult($success ? Swift_Events_SendEvent::RESULT_SPOOLED : Swift_Events_SendEvent::RESULT_FAILED); |
|
| 107 | - $this->eventDispatcher->dispatchEvent($evt, 'sendPerformed'); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - return 1; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Register a plugin. |
|
| 115 | - */ |
|
| 116 | - public function registerPlugin(Swift_Events_EventListener $plugin) |
|
| 117 | - { |
|
| 118 | - $this->eventDispatcher->bindEventListener($plugin); |
|
| 119 | - } |
|
| 18 | + /** The spool instance */ |
|
| 19 | + private $spool; |
|
| 20 | + |
|
| 21 | + /** The event dispatcher from the plugin API */ |
|
| 22 | + private $eventDispatcher; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Constructor. |
|
| 26 | + */ |
|
| 27 | + public function __construct(Swift_Events_EventDispatcher $eventDispatcher, Swift_Spool $spool = null) |
|
| 28 | + { |
|
| 29 | + $this->eventDispatcher = $eventDispatcher; |
|
| 30 | + $this->spool = $spool; |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Sets the spool object. |
|
| 35 | + * |
|
| 36 | + * @return $this |
|
| 37 | + */ |
|
| 38 | + public function setSpool(Swift_Spool $spool) |
|
| 39 | + { |
|
| 40 | + $this->spool = $spool; |
|
| 41 | + |
|
| 42 | + return $this; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Get the spool object. |
|
| 47 | + * |
|
| 48 | + * @return Swift_Spool |
|
| 49 | + */ |
|
| 50 | + public function getSpool() |
|
| 51 | + { |
|
| 52 | + return $this->spool; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Tests if this Transport mechanism has started. |
|
| 57 | + * |
|
| 58 | + * @return bool |
|
| 59 | + */ |
|
| 60 | + public function isStarted() |
|
| 61 | + { |
|
| 62 | + return true; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Starts this Transport mechanism. |
|
| 67 | + */ |
|
| 68 | + public function start() |
|
| 69 | + { |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Stops this Transport mechanism. |
|
| 74 | + */ |
|
| 75 | + public function stop() |
|
| 76 | + { |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * {@inheritdoc} |
|
| 81 | + */ |
|
| 82 | + public function ping() |
|
| 83 | + { |
|
| 84 | + return true; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Sends the given message. |
|
| 89 | + * |
|
| 90 | + * @param string[] $failedRecipients An array of failures by-reference |
|
| 91 | + * |
|
| 92 | + * @return int The number of sent e-mail's |
|
| 93 | + */ |
|
| 94 | + public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null) |
|
| 95 | + { |
|
| 96 | + if ($evt = $this->eventDispatcher->createSendEvent($this, $message)) { |
|
| 97 | + $this->eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed'); |
|
| 98 | + if ($evt->bubbleCancelled()) { |
|
| 99 | + return 0; |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + $success = $this->spool->queueMessage($message); |
|
| 104 | + |
|
| 105 | + if ($evt) { |
|
| 106 | + $evt->setResult($success ? Swift_Events_SendEvent::RESULT_SPOOLED : Swift_Events_SendEvent::RESULT_FAILED); |
|
| 107 | + $this->eventDispatcher->dispatchEvent($evt, 'sendPerformed'); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + return 1; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Register a plugin. |
|
| 115 | + */ |
|
| 116 | + public function registerPlugin(Swift_Events_EventListener $plugin) |
|
| 117 | + { |
|
| 118 | + $this->eventDispatcher->bindEventListener($plugin); |
|
| 119 | + } |
|
| 120 | 120 | } |
@@ -15,305 +15,305 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Swift_Transport_StreamBuffer extends Swift_ByteStream_AbstractFilterableInputStream implements Swift_Transport_IoBuffer |
| 17 | 17 | { |
| 18 | - /** A primary socket */ |
|
| 19 | - private $stream; |
|
| 20 | - |
|
| 21 | - /** The input stream */ |
|
| 22 | - private $in; |
|
| 23 | - |
|
| 24 | - /** The output stream */ |
|
| 25 | - private $out; |
|
| 26 | - |
|
| 27 | - /** Buffer initialization parameters */ |
|
| 28 | - private $params = []; |
|
| 29 | - |
|
| 30 | - /** The ReplacementFilterFactory */ |
|
| 31 | - private $replacementFactory; |
|
| 32 | - |
|
| 33 | - /** Translations performed on data being streamed into the buffer */ |
|
| 34 | - private $translations = []; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Create a new StreamBuffer using $replacementFactory for transformations. |
|
| 38 | - */ |
|
| 39 | - public function __construct(Swift_ReplacementFilterFactory $replacementFactory) |
|
| 40 | - { |
|
| 41 | - $this->replacementFactory = $replacementFactory; |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Perform any initialization needed, using the given $params. |
|
| 46 | - * |
|
| 47 | - * Parameters will vary depending upon the type of IoBuffer used. |
|
| 48 | - */ |
|
| 49 | - public function initialize(array $params) |
|
| 50 | - { |
|
| 51 | - $this->params = $params; |
|
| 52 | - switch ($params['type']) { |
|
| 53 | - case self::TYPE_PROCESS: |
|
| 54 | - $this->establishProcessConnection(); |
|
| 55 | - break; |
|
| 56 | - case self::TYPE_SOCKET: |
|
| 57 | - default: |
|
| 58 | - $this->establishSocketConnection(); |
|
| 59 | - break; |
|
| 60 | - } |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Set an individual param on the buffer (e.g. switching to SSL). |
|
| 65 | - * |
|
| 66 | - * @param string $param |
|
| 67 | - * @param mixed $value |
|
| 68 | - */ |
|
| 69 | - public function setParam($param, $value) |
|
| 70 | - { |
|
| 71 | - if (isset($this->stream)) { |
|
| 72 | - switch ($param) { |
|
| 73 | - case 'timeout': |
|
| 74 | - if ($this->stream) { |
|
| 75 | - stream_set_timeout($this->stream, $value); |
|
| 76 | - } |
|
| 77 | - break; |
|
| 78 | - |
|
| 79 | - case 'blocking': |
|
| 80 | - if ($this->stream) { |
|
| 81 | - stream_set_blocking($this->stream, 1); |
|
| 82 | - } |
|
| 83 | - } |
|
| 84 | - } |
|
| 85 | - $this->params[$param] = $value; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - public function startTLS() |
|
| 89 | - { |
|
| 90 | - // STREAM_CRYPTO_METHOD_TLS_CLIENT only allow tls1.0 connections (some php versions) |
|
| 91 | - // To support modern tls we allow explicit tls1.0, tls1.1, tls1.2 |
|
| 92 | - // Ssl3 and older are not allowed because they are vulnerable |
|
| 93 | - // @TODO make tls arguments configurable |
|
| 94 | - return stream_socket_enable_crypto($this->stream, true, STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Perform any shutdown logic needed. |
|
| 99 | - */ |
|
| 100 | - public function terminate() |
|
| 101 | - { |
|
| 102 | - if (isset($this->stream)) { |
|
| 103 | - switch ($this->params['type']) { |
|
| 104 | - case self::TYPE_PROCESS: |
|
| 105 | - fclose($this->in); |
|
| 106 | - fclose($this->out); |
|
| 107 | - proc_close($this->stream); |
|
| 108 | - break; |
|
| 109 | - case self::TYPE_SOCKET: |
|
| 110 | - default: |
|
| 111 | - fclose($this->stream); |
|
| 112 | - break; |
|
| 113 | - } |
|
| 114 | - } |
|
| 115 | - $this->stream = null; |
|
| 116 | - $this->out = null; |
|
| 117 | - $this->in = null; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Set an array of string replacements which should be made on data written |
|
| 122 | - * to the buffer. |
|
| 123 | - * |
|
| 124 | - * This could replace LF with CRLF for example. |
|
| 125 | - * |
|
| 126 | - * @param string[] $replacements |
|
| 127 | - */ |
|
| 128 | - public function setWriteTranslations(array $replacements) |
|
| 129 | - { |
|
| 130 | - foreach ($this->translations as $search => $replace) { |
|
| 131 | - if (!isset($replacements[$search])) { |
|
| 132 | - $this->removeFilter($search); |
|
| 133 | - unset($this->translations[$search]); |
|
| 134 | - } |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - foreach ($replacements as $search => $replace) { |
|
| 138 | - if (!isset($this->translations[$search])) { |
|
| 139 | - $this->addFilter( |
|
| 140 | - $this->replacementFactory->createFilter($search, $replace), $search |
|
| 141 | - ); |
|
| 142 | - $this->translations[$search] = true; |
|
| 143 | - } |
|
| 144 | - } |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Get a line of output (including any CRLF). |
|
| 149 | - * |
|
| 150 | - * The $sequence number comes from any writes and may or may not be used |
|
| 151 | - * depending upon the implementation. |
|
| 152 | - * |
|
| 153 | - * @param int $sequence of last write to scan from |
|
| 154 | - * |
|
| 155 | - * @return string |
|
| 156 | - * |
|
| 157 | - * @throws Swift_IoException |
|
| 158 | - */ |
|
| 159 | - public function readLine($sequence) |
|
| 160 | - { |
|
| 161 | - if (isset($this->out) && !feof($this->out)) { |
|
| 162 | - $line = fgets($this->out); |
|
| 163 | - if (0 == \strlen($line)) { |
|
| 164 | - $metas = stream_get_meta_data($this->out); |
|
| 165 | - if ($metas['timed_out']) { |
|
| 166 | - throw new Swift_IoException('Connection to '.$this->getReadConnectionDescription().' Timed Out'); |
|
| 167 | - } |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - return $line; |
|
| 171 | - } |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * Reads $length bytes from the stream into a string and moves the pointer |
|
| 176 | - * through the stream by $length. |
|
| 177 | - * |
|
| 178 | - * If less bytes exist than are requested the remaining bytes are given instead. |
|
| 179 | - * If no bytes are remaining at all, boolean false is returned. |
|
| 180 | - * |
|
| 181 | - * @param int $length |
|
| 182 | - * |
|
| 183 | - * @return string|bool |
|
| 184 | - * |
|
| 185 | - * @throws Swift_IoException |
|
| 186 | - */ |
|
| 187 | - public function read($length) |
|
| 188 | - { |
|
| 189 | - if (isset($this->out) && !feof($this->out)) { |
|
| 190 | - $ret = fread($this->out, $length); |
|
| 191 | - if (0 == \strlen($ret)) { |
|
| 192 | - $metas = stream_get_meta_data($this->out); |
|
| 193 | - if ($metas['timed_out']) { |
|
| 194 | - throw new Swift_IoException('Connection to '.$this->getReadConnectionDescription().' Timed Out'); |
|
| 195 | - } |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - return $ret; |
|
| 199 | - } |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /** Not implemented */ |
|
| 203 | - public function setReadPointer($byteOffset) |
|
| 204 | - { |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** Flush the stream contents */ |
|
| 208 | - protected function flush() |
|
| 209 | - { |
|
| 210 | - if (isset($this->in)) { |
|
| 211 | - fflush($this->in); |
|
| 212 | - } |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - /** Write this bytes to the stream */ |
|
| 216 | - protected function doCommit($bytes) |
|
| 217 | - { |
|
| 218 | - if (isset($this->in)) { |
|
| 219 | - $bytesToWrite = \strlen($bytes); |
|
| 220 | - $totalBytesWritten = 0; |
|
| 221 | - |
|
| 222 | - while ($totalBytesWritten < $bytesToWrite) { |
|
| 223 | - $bytesWritten = fwrite($this->in, substr($bytes, $totalBytesWritten)); |
|
| 224 | - if (false === $bytesWritten || 0 === $bytesWritten) { |
|
| 225 | - break; |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - $totalBytesWritten += $bytesWritten; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - if ($totalBytesWritten > 0) { |
|
| 232 | - return ++$this->sequence; |
|
| 233 | - } |
|
| 234 | - } |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - /** |
|
| 238 | - * Establishes a connection to a remote server. |
|
| 239 | - */ |
|
| 240 | - private function establishSocketConnection() |
|
| 241 | - { |
|
| 242 | - $host = $this->params['host']; |
|
| 243 | - if (!empty($this->params['protocol'])) { |
|
| 244 | - $host = $this->params['protocol'].'://'.$host; |
|
| 245 | - } |
|
| 246 | - $timeout = 15; |
|
| 247 | - if (!empty($this->params['timeout'])) { |
|
| 248 | - $timeout = $this->params['timeout']; |
|
| 249 | - } |
|
| 250 | - $options = []; |
|
| 251 | - if (!empty($this->params['sourceIp'])) { |
|
| 252 | - $options['socket']['bindto'] = $this->params['sourceIp'].':0'; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - if (isset($this->params['stream_context_options'])) { |
|
| 256 | - $options = array_merge($options, $this->params['stream_context_options']); |
|
| 257 | - } |
|
| 258 | - $streamContext = stream_context_create($options); |
|
| 259 | - |
|
| 260 | - set_error_handler(function ($type, $msg) { |
|
| 261 | - throw new Swift_TransportException('Connection could not be established with host '.$this->params['host'].' :'.$msg); |
|
| 262 | - }); |
|
| 263 | - try { |
|
| 264 | - $this->stream = stream_socket_client($host.':'.$this->params['port'], $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $streamContext); |
|
| 265 | - } finally { |
|
| 266 | - restore_error_handler(); |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - if (!empty($this->params['blocking'])) { |
|
| 270 | - stream_set_blocking($this->stream, 1); |
|
| 271 | - } else { |
|
| 272 | - stream_set_blocking($this->stream, 0); |
|
| 273 | - } |
|
| 274 | - stream_set_timeout($this->stream, $timeout); |
|
| 275 | - $this->in = &$this->stream; |
|
| 276 | - $this->out = &$this->stream; |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - /** |
|
| 280 | - * Opens a process for input/output. |
|
| 281 | - */ |
|
| 282 | - private function establishProcessConnection() |
|
| 283 | - { |
|
| 284 | - $command = $this->params['command']; |
|
| 285 | - $descriptorSpec = [ |
|
| 286 | - 0 => ['pipe', 'r'], |
|
| 287 | - 1 => ['pipe', 'w'], |
|
| 288 | - 2 => ['pipe', 'w'], |
|
| 289 | - ]; |
|
| 290 | - $pipes = []; |
|
| 291 | - $this->stream = proc_open($command, $descriptorSpec, $pipes); |
|
| 292 | - stream_set_blocking($pipes[2], 0); |
|
| 293 | - if ($err = stream_get_contents($pipes[2])) { |
|
| 294 | - throw new Swift_TransportException('Process could not be started ['.$err.']'); |
|
| 295 | - } |
|
| 296 | - $this->in = &$pipes[0]; |
|
| 297 | - $this->out = &$pipes[1]; |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - private function getReadConnectionDescription() |
|
| 301 | - { |
|
| 302 | - switch ($this->params['type']) { |
|
| 303 | - case self::TYPE_PROCESS: |
|
| 304 | - return 'Process '.$this->params['command']; |
|
| 305 | - break; |
|
| 306 | - |
|
| 307 | - case self::TYPE_SOCKET: |
|
| 308 | - default: |
|
| 309 | - $host = $this->params['host']; |
|
| 310 | - if (!empty($this->params['protocol'])) { |
|
| 311 | - $host = $this->params['protocol'].'://'.$host; |
|
| 312 | - } |
|
| 313 | - $host .= ':'.$this->params['port']; |
|
| 314 | - |
|
| 315 | - return $host; |
|
| 316 | - break; |
|
| 317 | - } |
|
| 318 | - } |
|
| 18 | + /** A primary socket */ |
|
| 19 | + private $stream; |
|
| 20 | + |
|
| 21 | + /** The input stream */ |
|
| 22 | + private $in; |
|
| 23 | + |
|
| 24 | + /** The output stream */ |
|
| 25 | + private $out; |
|
| 26 | + |
|
| 27 | + /** Buffer initialization parameters */ |
|
| 28 | + private $params = []; |
|
| 29 | + |
|
| 30 | + /** The ReplacementFilterFactory */ |
|
| 31 | + private $replacementFactory; |
|
| 32 | + |
|
| 33 | + /** Translations performed on data being streamed into the buffer */ |
|
| 34 | + private $translations = []; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Create a new StreamBuffer using $replacementFactory for transformations. |
|
| 38 | + */ |
|
| 39 | + public function __construct(Swift_ReplacementFilterFactory $replacementFactory) |
|
| 40 | + { |
|
| 41 | + $this->replacementFactory = $replacementFactory; |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Perform any initialization needed, using the given $params. |
|
| 46 | + * |
|
| 47 | + * Parameters will vary depending upon the type of IoBuffer used. |
|
| 48 | + */ |
|
| 49 | + public function initialize(array $params) |
|
| 50 | + { |
|
| 51 | + $this->params = $params; |
|
| 52 | + switch ($params['type']) { |
|
| 53 | + case self::TYPE_PROCESS: |
|
| 54 | + $this->establishProcessConnection(); |
|
| 55 | + break; |
|
| 56 | + case self::TYPE_SOCKET: |
|
| 57 | + default: |
|
| 58 | + $this->establishSocketConnection(); |
|
| 59 | + break; |
|
| 60 | + } |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Set an individual param on the buffer (e.g. switching to SSL). |
|
| 65 | + * |
|
| 66 | + * @param string $param |
|
| 67 | + * @param mixed $value |
|
| 68 | + */ |
|
| 69 | + public function setParam($param, $value) |
|
| 70 | + { |
|
| 71 | + if (isset($this->stream)) { |
|
| 72 | + switch ($param) { |
|
| 73 | + case 'timeout': |
|
| 74 | + if ($this->stream) { |
|
| 75 | + stream_set_timeout($this->stream, $value); |
|
| 76 | + } |
|
| 77 | + break; |
|
| 78 | + |
|
| 79 | + case 'blocking': |
|
| 80 | + if ($this->stream) { |
|
| 81 | + stream_set_blocking($this->stream, 1); |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | + $this->params[$param] = $value; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + public function startTLS() |
|
| 89 | + { |
|
| 90 | + // STREAM_CRYPTO_METHOD_TLS_CLIENT only allow tls1.0 connections (some php versions) |
|
| 91 | + // To support modern tls we allow explicit tls1.0, tls1.1, tls1.2 |
|
| 92 | + // Ssl3 and older are not allowed because they are vulnerable |
|
| 93 | + // @TODO make tls arguments configurable |
|
| 94 | + return stream_socket_enable_crypto($this->stream, true, STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Perform any shutdown logic needed. |
|
| 99 | + */ |
|
| 100 | + public function terminate() |
|
| 101 | + { |
|
| 102 | + if (isset($this->stream)) { |
|
| 103 | + switch ($this->params['type']) { |
|
| 104 | + case self::TYPE_PROCESS: |
|
| 105 | + fclose($this->in); |
|
| 106 | + fclose($this->out); |
|
| 107 | + proc_close($this->stream); |
|
| 108 | + break; |
|
| 109 | + case self::TYPE_SOCKET: |
|
| 110 | + default: |
|
| 111 | + fclose($this->stream); |
|
| 112 | + break; |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | + $this->stream = null; |
|
| 116 | + $this->out = null; |
|
| 117 | + $this->in = null; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Set an array of string replacements which should be made on data written |
|
| 122 | + * to the buffer. |
|
| 123 | + * |
|
| 124 | + * This could replace LF with CRLF for example. |
|
| 125 | + * |
|
| 126 | + * @param string[] $replacements |
|
| 127 | + */ |
|
| 128 | + public function setWriteTranslations(array $replacements) |
|
| 129 | + { |
|
| 130 | + foreach ($this->translations as $search => $replace) { |
|
| 131 | + if (!isset($replacements[$search])) { |
|
| 132 | + $this->removeFilter($search); |
|
| 133 | + unset($this->translations[$search]); |
|
| 134 | + } |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + foreach ($replacements as $search => $replace) { |
|
| 138 | + if (!isset($this->translations[$search])) { |
|
| 139 | + $this->addFilter( |
|
| 140 | + $this->replacementFactory->createFilter($search, $replace), $search |
|
| 141 | + ); |
|
| 142 | + $this->translations[$search] = true; |
|
| 143 | + } |
|
| 144 | + } |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Get a line of output (including any CRLF). |
|
| 149 | + * |
|
| 150 | + * The $sequence number comes from any writes and may or may not be used |
|
| 151 | + * depending upon the implementation. |
|
| 152 | + * |
|
| 153 | + * @param int $sequence of last write to scan from |
|
| 154 | + * |
|
| 155 | + * @return string |
|
| 156 | + * |
|
| 157 | + * @throws Swift_IoException |
|
| 158 | + */ |
|
| 159 | + public function readLine($sequence) |
|
| 160 | + { |
|
| 161 | + if (isset($this->out) && !feof($this->out)) { |
|
| 162 | + $line = fgets($this->out); |
|
| 163 | + if (0 == \strlen($line)) { |
|
| 164 | + $metas = stream_get_meta_data($this->out); |
|
| 165 | + if ($metas['timed_out']) { |
|
| 166 | + throw new Swift_IoException('Connection to '.$this->getReadConnectionDescription().' Timed Out'); |
|
| 167 | + } |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + return $line; |
|
| 171 | + } |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * Reads $length bytes from the stream into a string and moves the pointer |
|
| 176 | + * through the stream by $length. |
|
| 177 | + * |
|
| 178 | + * If less bytes exist than are requested the remaining bytes are given instead. |
|
| 179 | + * If no bytes are remaining at all, boolean false is returned. |
|
| 180 | + * |
|
| 181 | + * @param int $length |
|
| 182 | + * |
|
| 183 | + * @return string|bool |
|
| 184 | + * |
|
| 185 | + * @throws Swift_IoException |
|
| 186 | + */ |
|
| 187 | + public function read($length) |
|
| 188 | + { |
|
| 189 | + if (isset($this->out) && !feof($this->out)) { |
|
| 190 | + $ret = fread($this->out, $length); |
|
| 191 | + if (0 == \strlen($ret)) { |
|
| 192 | + $metas = stream_get_meta_data($this->out); |
|
| 193 | + if ($metas['timed_out']) { |
|
| 194 | + throw new Swift_IoException('Connection to '.$this->getReadConnectionDescription().' Timed Out'); |
|
| 195 | + } |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + return $ret; |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /** Not implemented */ |
|
| 203 | + public function setReadPointer($byteOffset) |
|
| 204 | + { |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** Flush the stream contents */ |
|
| 208 | + protected function flush() |
|
| 209 | + { |
|
| 210 | + if (isset($this->in)) { |
|
| 211 | + fflush($this->in); |
|
| 212 | + } |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + /** Write this bytes to the stream */ |
|
| 216 | + protected function doCommit($bytes) |
|
| 217 | + { |
|
| 218 | + if (isset($this->in)) { |
|
| 219 | + $bytesToWrite = \strlen($bytes); |
|
| 220 | + $totalBytesWritten = 0; |
|
| 221 | + |
|
| 222 | + while ($totalBytesWritten < $bytesToWrite) { |
|
| 223 | + $bytesWritten = fwrite($this->in, substr($bytes, $totalBytesWritten)); |
|
| 224 | + if (false === $bytesWritten || 0 === $bytesWritten) { |
|
| 225 | + break; |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + $totalBytesWritten += $bytesWritten; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + if ($totalBytesWritten > 0) { |
|
| 232 | + return ++$this->sequence; |
|
| 233 | + } |
|
| 234 | + } |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * Establishes a connection to a remote server. |
|
| 239 | + */ |
|
| 240 | + private function establishSocketConnection() |
|
| 241 | + { |
|
| 242 | + $host = $this->params['host']; |
|
| 243 | + if (!empty($this->params['protocol'])) { |
|
| 244 | + $host = $this->params['protocol'].'://'.$host; |
|
| 245 | + } |
|
| 246 | + $timeout = 15; |
|
| 247 | + if (!empty($this->params['timeout'])) { |
|
| 248 | + $timeout = $this->params['timeout']; |
|
| 249 | + } |
|
| 250 | + $options = []; |
|
| 251 | + if (!empty($this->params['sourceIp'])) { |
|
| 252 | + $options['socket']['bindto'] = $this->params['sourceIp'].':0'; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + if (isset($this->params['stream_context_options'])) { |
|
| 256 | + $options = array_merge($options, $this->params['stream_context_options']); |
|
| 257 | + } |
|
| 258 | + $streamContext = stream_context_create($options); |
|
| 259 | + |
|
| 260 | + set_error_handler(function ($type, $msg) { |
|
| 261 | + throw new Swift_TransportException('Connection could not be established with host '.$this->params['host'].' :'.$msg); |
|
| 262 | + }); |
|
| 263 | + try { |
|
| 264 | + $this->stream = stream_socket_client($host.':'.$this->params['port'], $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $streamContext); |
|
| 265 | + } finally { |
|
| 266 | + restore_error_handler(); |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + if (!empty($this->params['blocking'])) { |
|
| 270 | + stream_set_blocking($this->stream, 1); |
|
| 271 | + } else { |
|
| 272 | + stream_set_blocking($this->stream, 0); |
|
| 273 | + } |
|
| 274 | + stream_set_timeout($this->stream, $timeout); |
|
| 275 | + $this->in = &$this->stream; |
|
| 276 | + $this->out = &$this->stream; |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + /** |
|
| 280 | + * Opens a process for input/output. |
|
| 281 | + */ |
|
| 282 | + private function establishProcessConnection() |
|
| 283 | + { |
|
| 284 | + $command = $this->params['command']; |
|
| 285 | + $descriptorSpec = [ |
|
| 286 | + 0 => ['pipe', 'r'], |
|
| 287 | + 1 => ['pipe', 'w'], |
|
| 288 | + 2 => ['pipe', 'w'], |
|
| 289 | + ]; |
|
| 290 | + $pipes = []; |
|
| 291 | + $this->stream = proc_open($command, $descriptorSpec, $pipes); |
|
| 292 | + stream_set_blocking($pipes[2], 0); |
|
| 293 | + if ($err = stream_get_contents($pipes[2])) { |
|
| 294 | + throw new Swift_TransportException('Process could not be started ['.$err.']'); |
|
| 295 | + } |
|
| 296 | + $this->in = &$pipes[0]; |
|
| 297 | + $this->out = &$pipes[1]; |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + private function getReadConnectionDescription() |
|
| 301 | + { |
|
| 302 | + switch ($this->params['type']) { |
|
| 303 | + case self::TYPE_PROCESS: |
|
| 304 | + return 'Process '.$this->params['command']; |
|
| 305 | + break; |
|
| 306 | + |
|
| 307 | + case self::TYPE_SOCKET: |
|
| 308 | + default: |
|
| 309 | + $host = $this->params['host']; |
|
| 310 | + if (!empty($this->params['protocol'])) { |
|
| 311 | + $host = $this->params['protocol'].'://'.$host; |
|
| 312 | + } |
|
| 313 | + $host .= ':'.$this->params['port']; |
|
| 314 | + |
|
| 315 | + return $host; |
|
| 316 | + break; |
|
| 317 | + } |
|
| 318 | + } |
|
| 319 | 319 | } |
@@ -15,22 +15,22 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | interface Swift_Transport_Esmtp_Authenticator |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Get the name of the AUTH mechanism this Authenticator handles. |
|
| 20 | - * |
|
| 21 | - * @return string |
|
| 22 | - */ |
|
| 23 | - public function getAuthKeyword(); |
|
| 18 | + /** |
|
| 19 | + * Get the name of the AUTH mechanism this Authenticator handles. |
|
| 20 | + * |
|
| 21 | + * @return string |
|
| 22 | + */ |
|
| 23 | + public function getAuthKeyword(); |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Try to authenticate the user with $username and $password. |
|
| 27 | - * |
|
| 28 | - * @param string $username |
|
| 29 | - * @param string $password |
|
| 30 | - * |
|
| 31 | - * @return bool true if authentication worked (returning false is deprecated, throw a Swift_TransportException instead) |
|
| 32 | - * |
|
| 33 | - * @throws Swift_TransportException Allows the message to bubble up when authentication was not successful |
|
| 34 | - */ |
|
| 35 | - public function authenticate(Swift_Transport_SmtpAgent $agent, $username, $password); |
|
| 25 | + /** |
|
| 26 | + * Try to authenticate the user with $username and $password. |
|
| 27 | + * |
|
| 28 | + * @param string $username |
|
| 29 | + * @param string $password |
|
| 30 | + * |
|
| 31 | + * @return bool true if authentication worked (returning false is deprecated, throw a Swift_TransportException instead) |
|
| 32 | + * |
|
| 33 | + * @throws Swift_TransportException Allows the message to bubble up when authentication was not successful |
|
| 34 | + */ |
|
| 35 | + public function authenticate(Swift_Transport_SmtpAgent $agent, $username, $password); |
|
| 36 | 36 | } |
@@ -23,85 +23,85 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | class Swift_Transport_Esmtp_SmtpUtf8Handler implements Swift_Transport_EsmtpHandler |
| 25 | 25 | { |
| 26 | - public function __construct() |
|
| 27 | - { |
|
| 28 | - } |
|
| 26 | + public function __construct() |
|
| 27 | + { |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Get the name of the ESMTP extension this handles. |
|
| 32 | - * |
|
| 33 | - * @return string |
|
| 34 | - */ |
|
| 35 | - public function getHandledKeyword() |
|
| 36 | - { |
|
| 37 | - return 'SMTPUTF8'; |
|
| 38 | - } |
|
| 30 | + /** |
|
| 31 | + * Get the name of the ESMTP extension this handles. |
|
| 32 | + * |
|
| 33 | + * @return string |
|
| 34 | + */ |
|
| 35 | + public function getHandledKeyword() |
|
| 36 | + { |
|
| 37 | + return 'SMTPUTF8'; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Not used. |
|
| 42 | - */ |
|
| 43 | - public function setKeywordParams(array $parameters) |
|
| 44 | - { |
|
| 45 | - } |
|
| 40 | + /** |
|
| 41 | + * Not used. |
|
| 42 | + */ |
|
| 43 | + public function setKeywordParams(array $parameters) |
|
| 44 | + { |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Not used. |
|
| 49 | - */ |
|
| 50 | - public function afterEhlo(Swift_Transport_SmtpAgent $agent) |
|
| 51 | - { |
|
| 52 | - } |
|
| 47 | + /** |
|
| 48 | + * Not used. |
|
| 49 | + */ |
|
| 50 | + public function afterEhlo(Swift_Transport_SmtpAgent $agent) |
|
| 51 | + { |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Get params which are appended to MAIL FROM:<>. |
|
| 56 | - * |
|
| 57 | - * @return string[] |
|
| 58 | - */ |
|
| 59 | - public function getMailParams() |
|
| 60 | - { |
|
| 61 | - return ['SMTPUTF8']; |
|
| 62 | - } |
|
| 54 | + /** |
|
| 55 | + * Get params which are appended to MAIL FROM:<>. |
|
| 56 | + * |
|
| 57 | + * @return string[] |
|
| 58 | + */ |
|
| 59 | + public function getMailParams() |
|
| 60 | + { |
|
| 61 | + return ['SMTPUTF8']; |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * Not used. |
|
| 66 | - */ |
|
| 67 | - public function getRcptParams() |
|
| 68 | - { |
|
| 69 | - return []; |
|
| 70 | - } |
|
| 64 | + /** |
|
| 65 | + * Not used. |
|
| 66 | + */ |
|
| 67 | + public function getRcptParams() |
|
| 68 | + { |
|
| 69 | + return []; |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * Not used. |
|
| 74 | - */ |
|
| 75 | - public function onCommand(Swift_Transport_SmtpAgent $agent, $command, $codes = [], &$failedRecipients = null, &$stop = false) |
|
| 76 | - { |
|
| 77 | - } |
|
| 72 | + /** |
|
| 73 | + * Not used. |
|
| 74 | + */ |
|
| 75 | + public function onCommand(Swift_Transport_SmtpAgent $agent, $command, $codes = [], &$failedRecipients = null, &$stop = false) |
|
| 76 | + { |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - /** |
|
| 80 | - * Returns +1, -1 or 0 according to the rules for usort(). |
|
| 81 | - * |
|
| 82 | - * This method is called to ensure extensions can be execute in an appropriate order. |
|
| 83 | - * |
|
| 84 | - * @param string $esmtpKeyword to compare with |
|
| 85 | - * |
|
| 86 | - * @return int |
|
| 87 | - */ |
|
| 88 | - public function getPriorityOver($esmtpKeyword) |
|
| 89 | - { |
|
| 90 | - return 0; |
|
| 91 | - } |
|
| 79 | + /** |
|
| 80 | + * Returns +1, -1 or 0 according to the rules for usort(). |
|
| 81 | + * |
|
| 82 | + * This method is called to ensure extensions can be execute in an appropriate order. |
|
| 83 | + * |
|
| 84 | + * @param string $esmtpKeyword to compare with |
|
| 85 | + * |
|
| 86 | + * @return int |
|
| 87 | + */ |
|
| 88 | + public function getPriorityOver($esmtpKeyword) |
|
| 89 | + { |
|
| 90 | + return 0; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Not used. |
|
| 95 | - */ |
|
| 96 | - public function exposeMixinMethods() |
|
| 97 | - { |
|
| 98 | - return []; |
|
| 99 | - } |
|
| 93 | + /** |
|
| 94 | + * Not used. |
|
| 95 | + */ |
|
| 96 | + public function exposeMixinMethods() |
|
| 97 | + { |
|
| 98 | + return []; |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - /** |
|
| 102 | - * Not used. |
|
| 103 | - */ |
|
| 104 | - public function resetState() |
|
| 105 | - { |
|
| 106 | - } |
|
| 101 | + /** |
|
| 102 | + * Not used. |
|
| 103 | + */ |
|
| 104 | + public function resetState() |
|
| 105 | + { |
|
| 106 | + } |
|
| 107 | 107 | } |
@@ -22,92 +22,92 @@ |
||
| 22 | 22 | */ |
| 23 | 23 | class Swift_Transport_Esmtp_EightBitMimeHandler implements Swift_Transport_EsmtpHandler |
| 24 | 24 | { |
| 25 | - protected $encoding; |
|
| 25 | + protected $encoding; |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * @param string $encoding The parameter so send with the MAIL FROM command; |
|
| 29 | - * either "8BITMIME" or "7BIT" |
|
| 30 | - */ |
|
| 31 | - public function __construct(string $encoding = '8BITMIME') |
|
| 32 | - { |
|
| 33 | - $this->encoding = $encoding; |
|
| 34 | - } |
|
| 27 | + /** |
|
| 28 | + * @param string $encoding The parameter so send with the MAIL FROM command; |
|
| 29 | + * either "8BITMIME" or "7BIT" |
|
| 30 | + */ |
|
| 31 | + public function __construct(string $encoding = '8BITMIME') |
|
| 32 | + { |
|
| 33 | + $this->encoding = $encoding; |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Get the name of the ESMTP extension this handles. |
|
| 38 | - * |
|
| 39 | - * @return string |
|
| 40 | - */ |
|
| 41 | - public function getHandledKeyword() |
|
| 42 | - { |
|
| 43 | - return '8BITMIME'; |
|
| 44 | - } |
|
| 36 | + /** |
|
| 37 | + * Get the name of the ESMTP extension this handles. |
|
| 38 | + * |
|
| 39 | + * @return string |
|
| 40 | + */ |
|
| 41 | + public function getHandledKeyword() |
|
| 42 | + { |
|
| 43 | + return '8BITMIME'; |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Not used. |
|
| 48 | - */ |
|
| 49 | - public function setKeywordParams(array $parameters) |
|
| 50 | - { |
|
| 51 | - } |
|
| 46 | + /** |
|
| 47 | + * Not used. |
|
| 48 | + */ |
|
| 49 | + public function setKeywordParams(array $parameters) |
|
| 50 | + { |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Not used. |
|
| 55 | - */ |
|
| 56 | - public function afterEhlo(Swift_Transport_SmtpAgent $agent) |
|
| 57 | - { |
|
| 58 | - } |
|
| 53 | + /** |
|
| 54 | + * Not used. |
|
| 55 | + */ |
|
| 56 | + public function afterEhlo(Swift_Transport_SmtpAgent $agent) |
|
| 57 | + { |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Get params which are appended to MAIL FROM:<>. |
|
| 62 | - * |
|
| 63 | - * @return string[] |
|
| 64 | - */ |
|
| 65 | - public function getMailParams() |
|
| 66 | - { |
|
| 67 | - return ['BODY='.$this->encoding]; |
|
| 68 | - } |
|
| 60 | + /** |
|
| 61 | + * Get params which are appended to MAIL FROM:<>. |
|
| 62 | + * |
|
| 63 | + * @return string[] |
|
| 64 | + */ |
|
| 65 | + public function getMailParams() |
|
| 66 | + { |
|
| 67 | + return ['BODY='.$this->encoding]; |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * Not used. |
|
| 72 | - */ |
|
| 73 | - public function getRcptParams() |
|
| 74 | - { |
|
| 75 | - return []; |
|
| 76 | - } |
|
| 70 | + /** |
|
| 71 | + * Not used. |
|
| 72 | + */ |
|
| 73 | + public function getRcptParams() |
|
| 74 | + { |
|
| 75 | + return []; |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * Not used. |
|
| 80 | - */ |
|
| 81 | - public function onCommand(Swift_Transport_SmtpAgent $agent, $command, $codes = [], &$failedRecipients = null, &$stop = false) |
|
| 82 | - { |
|
| 83 | - } |
|
| 78 | + /** |
|
| 79 | + * Not used. |
|
| 80 | + */ |
|
| 81 | + public function onCommand(Swift_Transport_SmtpAgent $agent, $command, $codes = [], &$failedRecipients = null, &$stop = false) |
|
| 82 | + { |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - /** |
|
| 86 | - * Returns +1, -1 or 0 according to the rules for usort(). |
|
| 87 | - * |
|
| 88 | - * This method is called to ensure extensions can be execute in an appropriate order. |
|
| 89 | - * |
|
| 90 | - * @param string $esmtpKeyword to compare with |
|
| 91 | - * |
|
| 92 | - * @return int |
|
| 93 | - */ |
|
| 94 | - public function getPriorityOver($esmtpKeyword) |
|
| 95 | - { |
|
| 96 | - return 0; |
|
| 97 | - } |
|
| 85 | + /** |
|
| 86 | + * Returns +1, -1 or 0 according to the rules for usort(). |
|
| 87 | + * |
|
| 88 | + * This method is called to ensure extensions can be execute in an appropriate order. |
|
| 89 | + * |
|
| 90 | + * @param string $esmtpKeyword to compare with |
|
| 91 | + * |
|
| 92 | + * @return int |
|
| 93 | + */ |
|
| 94 | + public function getPriorityOver($esmtpKeyword) |
|
| 95 | + { |
|
| 96 | + return 0; |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * Not used. |
|
| 101 | - */ |
|
| 102 | - public function exposeMixinMethods() |
|
| 103 | - { |
|
| 104 | - return []; |
|
| 105 | - } |
|
| 99 | + /** |
|
| 100 | + * Not used. |
|
| 101 | + */ |
|
| 102 | + public function exposeMixinMethods() |
|
| 103 | + { |
|
| 104 | + return []; |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - /** |
|
| 108 | - * Not used. |
|
| 109 | - */ |
|
| 110 | - public function resetState() |
|
| 111 | - { |
|
| 112 | - } |
|
| 107 | + /** |
|
| 108 | + * Not used. |
|
| 109 | + */ |
|
| 110 | + public function resetState() |
|
| 111 | + { |
|
| 112 | + } |
|
| 113 | 113 | } |
@@ -15,61 +15,61 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Swift_Transport_Esmtp_Auth_CramMd5Authenticator implements Swift_Transport_Esmtp_Authenticator |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Get the name of the AUTH mechanism this Authenticator handles. |
|
| 20 | - * |
|
| 21 | - * @return string |
|
| 22 | - */ |
|
| 23 | - public function getAuthKeyword() |
|
| 24 | - { |
|
| 25 | - return 'CRAM-MD5'; |
|
| 26 | - } |
|
| 18 | + /** |
|
| 19 | + * Get the name of the AUTH mechanism this Authenticator handles. |
|
| 20 | + * |
|
| 21 | + * @return string |
|
| 22 | + */ |
|
| 23 | + public function getAuthKeyword() |
|
| 24 | + { |
|
| 25 | + return 'CRAM-MD5'; |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * {@inheritdoc} |
|
| 30 | - */ |
|
| 31 | - public function authenticate(Swift_Transport_SmtpAgent $agent, $username, $password) |
|
| 32 | - { |
|
| 33 | - try { |
|
| 34 | - $challenge = $agent->executeCommand("AUTH CRAM-MD5\r\n", [334]); |
|
| 35 | - $challenge = base64_decode(substr($challenge, 4)); |
|
| 36 | - $message = base64_encode( |
|
| 37 | - $username.' '.$this->getResponse($password, $challenge) |
|
| 38 | - ); |
|
| 39 | - $agent->executeCommand(sprintf("%s\r\n", $message), [235]); |
|
| 28 | + /** |
|
| 29 | + * {@inheritdoc} |
|
| 30 | + */ |
|
| 31 | + public function authenticate(Swift_Transport_SmtpAgent $agent, $username, $password) |
|
| 32 | + { |
|
| 33 | + try { |
|
| 34 | + $challenge = $agent->executeCommand("AUTH CRAM-MD5\r\n", [334]); |
|
| 35 | + $challenge = base64_decode(substr($challenge, 4)); |
|
| 36 | + $message = base64_encode( |
|
| 37 | + $username.' '.$this->getResponse($password, $challenge) |
|
| 38 | + ); |
|
| 39 | + $agent->executeCommand(sprintf("%s\r\n", $message), [235]); |
|
| 40 | 40 | |
| 41 | - return true; |
|
| 42 | - } catch (Swift_TransportException $e) { |
|
| 43 | - $agent->executeCommand("RSET\r\n", [250]); |
|
| 41 | + return true; |
|
| 42 | + } catch (Swift_TransportException $e) { |
|
| 43 | + $agent->executeCommand("RSET\r\n", [250]); |
|
| 44 | 44 | |
| 45 | - throw $e; |
|
| 46 | - } |
|
| 47 | - } |
|
| 45 | + throw $e; |
|
| 46 | + } |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Generate a CRAM-MD5 response from a server challenge. |
|
| 51 | - * |
|
| 52 | - * @param string $secret |
|
| 53 | - * @param string $challenge |
|
| 54 | - * |
|
| 55 | - * @return string |
|
| 56 | - */ |
|
| 57 | - private function getResponse($secret, $challenge) |
|
| 58 | - { |
|
| 59 | - if (\strlen($secret) > 64) { |
|
| 60 | - $secret = pack('H32', md5($secret)); |
|
| 61 | - } |
|
| 49 | + /** |
|
| 50 | + * Generate a CRAM-MD5 response from a server challenge. |
|
| 51 | + * |
|
| 52 | + * @param string $secret |
|
| 53 | + * @param string $challenge |
|
| 54 | + * |
|
| 55 | + * @return string |
|
| 56 | + */ |
|
| 57 | + private function getResponse($secret, $challenge) |
|
| 58 | + { |
|
| 59 | + if (\strlen($secret) > 64) { |
|
| 60 | + $secret = pack('H32', md5($secret)); |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - if (\strlen($secret) < 64) { |
|
| 64 | - $secret = str_pad($secret, 64, \chr(0)); |
|
| 65 | - } |
|
| 63 | + if (\strlen($secret) < 64) { |
|
| 64 | + $secret = str_pad($secret, 64, \chr(0)); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - $k_ipad = substr($secret, 0, 64) ^ str_repeat(\chr(0x36), 64); |
|
| 68 | - $k_opad = substr($secret, 0, 64) ^ str_repeat(\chr(0x5C), 64); |
|
| 67 | + $k_ipad = substr($secret, 0, 64) ^ str_repeat(\chr(0x36), 64); |
|
| 68 | + $k_opad = substr($secret, 0, 64) ^ str_repeat(\chr(0x5C), 64); |
|
| 69 | 69 | |
| 70 | - $inner = pack('H32', md5($k_ipad.$challenge)); |
|
| 71 | - $digest = md5($k_opad.$inner); |
|
| 70 | + $inner = pack('H32', md5($k_ipad.$challenge)); |
|
| 71 | + $digest = md5($k_opad.$inner); |
|
| 72 | 72 | |
| 73 | - return $digest; |
|
| 74 | - } |
|
| 73 | + return $digest; |
|
| 74 | + } |
|
| 75 | 75 | } |
@@ -15,31 +15,31 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Swift_Transport_Esmtp_Auth_LoginAuthenticator implements Swift_Transport_Esmtp_Authenticator |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Get the name of the AUTH mechanism this Authenticator handles. |
|
| 20 | - * |
|
| 21 | - * @return string |
|
| 22 | - */ |
|
| 23 | - public function getAuthKeyword() |
|
| 24 | - { |
|
| 25 | - return 'LOGIN'; |
|
| 26 | - } |
|
| 18 | + /** |
|
| 19 | + * Get the name of the AUTH mechanism this Authenticator handles. |
|
| 20 | + * |
|
| 21 | + * @return string |
|
| 22 | + */ |
|
| 23 | + public function getAuthKeyword() |
|
| 24 | + { |
|
| 25 | + return 'LOGIN'; |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * {@inheritdoc} |
|
| 30 | - */ |
|
| 31 | - public function authenticate(Swift_Transport_SmtpAgent $agent, $username, $password) |
|
| 32 | - { |
|
| 33 | - try { |
|
| 34 | - $agent->executeCommand("AUTH LOGIN\r\n", [334]); |
|
| 35 | - $agent->executeCommand(sprintf("%s\r\n", base64_encode($username ?? '')), [334]); |
|
| 36 | - $agent->executeCommand(sprintf("%s\r\n", base64_encode($password ?? '')), [235]); |
|
| 28 | + /** |
|
| 29 | + * {@inheritdoc} |
|
| 30 | + */ |
|
| 31 | + public function authenticate(Swift_Transport_SmtpAgent $agent, $username, $password) |
|
| 32 | + { |
|
| 33 | + try { |
|
| 34 | + $agent->executeCommand("AUTH LOGIN\r\n", [334]); |
|
| 35 | + $agent->executeCommand(sprintf("%s\r\n", base64_encode($username ?? '')), [334]); |
|
| 36 | + $agent->executeCommand(sprintf("%s\r\n", base64_encode($password ?? '')), [235]); |
|
| 37 | 37 | |
| 38 | - return true; |
|
| 39 | - } catch (Swift_TransportException $e) { |
|
| 40 | - $agent->executeCommand("RSET\r\n", [250]); |
|
| 38 | + return true; |
|
| 39 | + } catch (Swift_TransportException $e) { |
|
| 40 | + $agent->executeCommand("RSET\r\n", [250]); |
|
| 41 | 41 | |
| 42 | - throw $e; |
|
| 43 | - } |
|
| 44 | - } |
|
| 42 | + throw $e; |
|
| 43 | + } |
|
| 44 | + } |
|
| 45 | 45 | } |