2
0
forked from Wavyzz/dolibarr

swiftmailer

This commit is contained in:
Frédéric FRANCE
2018-01-21 15:55:56 +01:00
parent 3a8ceb130f
commit a34b99f3ec
191 changed files with 5164 additions and 4074 deletions

View File

@@ -16,34 +16,35 @@
abstract class Swift_Transport_AbstractSmtpTransport implements Swift_Transport
{
/** Input-Output buffer for sending/receiving SMTP commands and responses */
protected $_buffer;
protected $buffer;
/** Connection status */
protected $_started = false;
protected $started = false;
/** The domain name to use in HELO command */
protected $_domain = '[127.0.0.1]';
protected $domain = '[127.0.0.1]';
/** The event dispatching layer */
protected $_eventDispatcher;
protected $eventDispatcher;
/** Source Ip */
protected $_sourceIp;
protected $sourceIp;
/** Return an array of params for the Buffer */
abstract protected function _getBufferParams();
abstract protected function getBufferParams();
/**
* Creates a new EsmtpTransport using the given I/O buffer.
*
* @param Swift_Transport_IoBuffer $buf
* @param Swift_Events_EventDispatcher $dispatcher
* @param string $localDomain
*/
public function __construct(Swift_Transport_IoBuffer $buf, Swift_Events_EventDispatcher $dispatcher)
public function __construct(Swift_Transport_IoBuffer $buf, Swift_Events_EventDispatcher $dispatcher, $localDomain = '127.0.0.1')
{
$this->_eventDispatcher = $dispatcher;
$this->_buffer = $buf;
$this->_lookupHostname();
$this->eventDispatcher = $dispatcher;
$this->buffer = $buf;
$this->setLocalDomain($localDomain);
}
/**
@@ -52,16 +53,25 @@ abstract class Swift_Transport_AbstractSmtpTransport implements Swift_Transport
* This should be a fully-qualified domain name and should be truly the domain
* you're using.
*
* If your server doesn't have a domain name, use the IP in square
* brackets (i.e. [127.0.0.1]).
* If your server does not have a domain name, use the IP address. This will
* automatically be wrapped in square brackets as described in RFC 5321,
* section 4.1.3.
*
* @param string $domain
*
* @return Swift_Transport_AbstractSmtpTransport
* @return $this
*/
public function setLocalDomain($domain)
{
$this->_domain = $domain;
if (substr($domain, 0, 1) !== '[') {
if (filter_var($domain, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$domain = '['.$domain.']';
} elseif (filter_var($domain, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
$domain = '[IPv6:'.$domain.']';
}
}
$this->domain = $domain;
return $this;
}
@@ -69,11 +79,14 @@ abstract class Swift_Transport_AbstractSmtpTransport implements Swift_Transport
/**
* Get the name of the domain Swift will identify as.
*
* If an IP address was specified, this will be returned wrapped in square
* brackets as described in RFC 5321, section 4.1.3.
*
* @return string
*/
public function getLocalDomain()
{
return $this->_domain;
return $this->domain;
}
/**
@@ -83,7 +96,7 @@ abstract class Swift_Transport_AbstractSmtpTransport implements Swift_Transport
*/
public function setSourceIp($source)
{
$this->_sourceIp = $source;
$this->sourceIp = $source;
}
/**
@@ -93,7 +106,7 @@ abstract class Swift_Transport_AbstractSmtpTransport implements Swift_Transport
*/
public function getSourceIp()
{
return $this->_sourceIp;
return $this->sourceIp;
}
/**
@@ -101,27 +114,27 @@ abstract class Swift_Transport_AbstractSmtpTransport implements Swift_Transport
*/
public function start()
{
if (!$this->_started) {
if ($evt = $this->_eventDispatcher->createTransportChangeEvent($this)) {
$this->_eventDispatcher->dispatchEvent($evt, 'beforeTransportStarted');
if (!$this->started) {
if ($evt = $this->eventDispatcher->createTransportChangeEvent($this)) {
$this->eventDispatcher->dispatchEvent($evt, 'beforeTransportStarted');
if ($evt->bubbleCancelled()) {
return;
}
}
try {
$this->_buffer->initialize($this->_getBufferParams());
$this->buffer->initialize($this->getBufferParams());
} catch (Swift_TransportException $e) {
$this->_throwException($e);
$this->throwException($e);
}
$this->_readGreeting();
$this->_doHeloCommand();
$this->readGreeting();
$this->doHeloCommand();
if ($evt) {
$this->_eventDispatcher->dispatchEvent($evt, 'transportStarted');
$this->eventDispatcher->dispatchEvent($evt, 'transportStarted');
}
$this->_started = true;
$this->started = true;
}
}
@@ -132,7 +145,7 @@ abstract class Swift_Transport_AbstractSmtpTransport implements Swift_Transport
*/
public function isStarted()
{
return $this->_started;
return $this->started;
}
/**
@@ -141,25 +154,25 @@ abstract class Swift_Transport_AbstractSmtpTransport implements Swift_Transport
* Recipient/sender data will be retrieved from the Message API.
* The return value is the number of recipients who were accepted for delivery.
*
* @param Swift_Mime_Message $message
* @param Swift_Mime_SimpleMessage $message
* @param string[] $failedRecipients An array of failures by-reference
*
* @return int
*/
public function send(Swift_Mime_Message $message, &$failedRecipients = null)
public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
{
$sent = 0;
$failedRecipients = (array) $failedRecipients;
if ($evt = $this->_eventDispatcher->createSendEvent($this, $message)) {
$this->_eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed');
if ($evt = $this->eventDispatcher->createSendEvent($this, $message)) {
$this->eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed');
if ($evt->bubbleCancelled()) {
return 0;
}
}
if (!$reversePath = $this->_getReversePath($message)) {
$this->_throwException(new Swift_TransportException(
if (!$reversePath = $this->getReversePath($message)) {
$this->throwException(new Swift_TransportException(
'Cannot send message without a sender address'
)
);
@@ -173,8 +186,8 @@ abstract class Swift_Transport_AbstractSmtpTransport implements Swift_Transport
$message->setBcc(array());
try {
$sent += $this->_sendTo($message, $reversePath, $tos, $failedRecipients);
$sent += $this->_sendBcc($message, $reversePath, $bcc, $failedRecipients);
$sent += $this->sendTo($message, $reversePath, $tos, $failedRecipients);
$sent += $this->sendBcc($message, $reversePath, $bcc, $failedRecipients);
} catch (Exception $e) {
$message->setBcc($bcc);
throw $e;
@@ -191,7 +204,7 @@ abstract class Swift_Transport_AbstractSmtpTransport implements Swift_Transport
$evt->setResult(Swift_Events_SendEvent::RESULT_FAILED);
}
$evt->setFailedRecipients($failedRecipients);
$this->_eventDispatcher->dispatchEvent($evt, 'sendPerformed');
$this->eventDispatcher->dispatchEvent($evt, 'sendPerformed');
}
$message->generateId(); //Make sure a new Message ID is used
@@ -204,9 +217,9 @@ abstract class Swift_Transport_AbstractSmtpTransport implements Swift_Transport
*/
public function stop()
{
if ($this->_started) {
if ($evt = $this->_eventDispatcher->createTransportChangeEvent($this)) {
$this->_eventDispatcher->dispatchEvent($evt, 'beforeTransportStopped');
if ($this->started) {
if ($evt = $this->eventDispatcher->createTransportChangeEvent($this)) {
$this->eventDispatcher->dispatchEvent($evt, 'beforeTransportStopped');
if ($evt->bubbleCancelled()) {
return;
}
@@ -218,16 +231,39 @@ abstract class Swift_Transport_AbstractSmtpTransport implements Swift_Transport
}
try {
$this->_buffer->terminate();
$this->buffer->terminate();
if ($evt) {
$this->_eventDispatcher->dispatchEvent($evt, 'transportStopped');
$this->eventDispatcher->dispatchEvent($evt, 'transportStopped');
}
} catch (Swift_TransportException $e) {
$this->_throwException($e);
$this->throwException($e);
}
}
$this->_started = false;
$this->started = false;
}
/**
* {@inheritdoc}
*/
public function ping()
{
try {
if (!$this->isStarted()) {
$this->start();
}
$this->executeCommand("NOOP\r\n", array(250));
} catch (Swift_TransportException $e) {
try {
$this->stop();
} catch (Swift_TransportException $e) {
}
return false;
}
return true;
}
/**
@@ -237,7 +273,7 @@ abstract class Swift_Transport_AbstractSmtpTransport implements Swift_Transport
*/
public function registerPlugin(Swift_Events_EventListener $plugin)
{
$this->_eventDispatcher->bindEventListener($plugin);
$this->eventDispatcher->bindEventListener($plugin);
}
/**
@@ -255,7 +291,7 @@ abstract class Swift_Transport_AbstractSmtpTransport implements Swift_Transport
*/
public function getBuffer()
{
return $this->_buffer;
return $this->buffer;
}
/**
@@ -273,32 +309,32 @@ abstract class Swift_Transport_AbstractSmtpTransport implements Swift_Transport
public function executeCommand($command, $codes = array(), &$failures = null)
{
$failures = (array) $failures;
$seq = $this->_buffer->write($command);
$response = $this->_getFullResponse($seq);
if ($evt = $this->_eventDispatcher->createCommandEvent($this, $command, $codes)) {
$this->_eventDispatcher->dispatchEvent($evt, 'commandSent');
$seq = $this->buffer->write($command);
$response = $this->getFullResponse($seq);
if ($evt = $this->eventDispatcher->createCommandEvent($this, $command, $codes)) {
$this->eventDispatcher->dispatchEvent($evt, 'commandSent');
}
$this->_assertResponseCode($response, $codes);
$this->assertResponseCode($response, $codes);
return $response;
}
/** Read the opening SMTP greeting */
protected function _readGreeting()
protected function readGreeting()
{
$this->_assertResponseCode($this->_getFullResponse(0), array(220));
$this->assertResponseCode($this->getFullResponse(0), array(220));
}
/** Send the HELO welcome */
protected function _doHeloCommand()
protected function doHeloCommand()
{
$this->executeCommand(
sprintf("HELO %s\r\n", $this->_domain), array(250)
sprintf("HELO %s\r\n", $this->domain), array(250)
);
}
/** Send the MAIL FROM command */
protected function _doMailFromCommand($address)
protected function doMailFromCommand($address)
{
$this->executeCommand(
sprintf("MAIL FROM:<%s>\r\n", $address), array(250)
@@ -306,7 +342,7 @@ abstract class Swift_Transport_AbstractSmtpTransport implements Swift_Transport
}
/** Send the RCPT TO command */
protected function _doRcptToCommand($address)
protected function doRcptToCommand($address)
{
$this->executeCommand(
sprintf("RCPT TO:<%s>\r\n", $address), array(250, 251, 252)
@@ -314,27 +350,27 @@ abstract class Swift_Transport_AbstractSmtpTransport implements Swift_Transport
}
/** Send the DATA command */
protected function _doDataCommand()
protected function doDataCommand()
{
$this->executeCommand("DATA\r\n", array(354));
}
/** Stream the contents of the message over the buffer */
protected function _streamMessage(Swift_Mime_Message $message)
protected function streamMessage(Swift_Mime_SimpleMessage $message)
{
$this->_buffer->setWriteTranslations(array("\r\n." => "\r\n.."));
$this->buffer->setWriteTranslations(array("\r\n." => "\r\n.."));
try {
$message->toByteStream($this->_buffer);
$this->_buffer->flushBuffers();
$message->toByteStream($this->buffer);
$this->buffer->flushBuffers();
} catch (Swift_TransportException $e) {
$this->_throwException($e);
$this->throwException($e);
}
$this->_buffer->setWriteTranslations(array());
$this->buffer->setWriteTranslations(array());
$this->executeCommand("\r\n.\r\n", array(250));
}
/** Determine the best-use reverse path for this message */
protected function _getReversePath(Swift_Mime_Message $message)
protected function getReversePath(Swift_Mime_SimpleMessage $message)
{
$return = $message->getReturnPath();
$sender = $message->getSender();
@@ -355,10 +391,10 @@ abstract class Swift_Transport_AbstractSmtpTransport implements Swift_Transport
}
/** Throw a TransportException, first sending it to any listeners */
protected function _throwException(Swift_TransportException $e)
protected function throwException(Swift_TransportException $e)
{
if ($evt = $this->_eventDispatcher->createTransportExceptionEvent($this, $e)) {
$this->_eventDispatcher->dispatchEvent($evt, 'exceptionThrown');
if ($evt = $this->eventDispatcher->createTransportExceptionEvent($this, $e)) {
$this->eventDispatcher->dispatchEvent($evt, 'exceptionThrown');
if (!$evt->bubbleCancelled()) {
throw $e;
}
@@ -368,18 +404,18 @@ abstract class Swift_Transport_AbstractSmtpTransport implements Swift_Transport
}
/** Throws an Exception if a response code is incorrect */
protected function _assertResponseCode($response, $wanted)
protected function assertResponseCode($response, $wanted)
{
list($code) = sscanf($response, '%3d');
$valid = (empty($wanted) || in_array($code, $wanted));
if ($evt = $this->_eventDispatcher->createResponseEvent($this, $response,
if ($evt = $this->eventDispatcher->createResponseEvent($this, $response,
$valid)) {
$this->_eventDispatcher->dispatchEvent($evt, 'responseReceived');
$this->eventDispatcher->dispatchEvent($evt, 'responseReceived');
}
if (!$valid) {
$this->_throwException(
$this->throwException(
new Swift_TransportException(
'Expected response code '.implode('/', $wanted).' but got code '.
'"'.$code.'", with message "'.$response.'"',
@@ -389,18 +425,18 @@ abstract class Swift_Transport_AbstractSmtpTransport implements Swift_Transport
}
/** Get an entire multi-line response using its sequence number */
protected function _getFullResponse($seq)
protected function getFullResponse($seq)
{
$response = '';
try {
do {
$line = $this->_buffer->readLine($seq);
$line = $this->buffer->readLine($seq);
$response .= $line;
} while (null !== $line && false !== $line && ' ' != $line{3});
} while (null !== $line && false !== $line && ' ' != $line[3]);
} catch (Swift_TransportException $e) {
$this->_throwException($e);
$this->throwException($e);
} catch (Swift_IoException $e) {
$this->_throwException(
$this->throwException(
new Swift_TransportException(
$e->getMessage())
);
@@ -410,13 +446,13 @@ abstract class Swift_Transport_AbstractSmtpTransport implements Swift_Transport
}
/** Send an email to the given recipients from the given reverse path */
private function _doMailTransaction($message, $reversePath, array $recipients, array &$failedRecipients)
private function doMailTransaction($message, $reversePath, array $recipients, array &$failedRecipients)
{
$sent = 0;
$this->_doMailFromCommand($reversePath);
$this->doMailFromCommand($reversePath);
foreach ($recipients as $forwardPath) {
try {
$this->_doRcptToCommand($forwardPath);
$this->doRcptToCommand($forwardPath);
++$sent;
} catch (Swift_TransportException $e) {
$failedRecipients[] = $forwardPath;
@@ -424,8 +460,8 @@ abstract class Swift_Transport_AbstractSmtpTransport implements Swift_Transport
}
if ($sent != 0) {
$this->_doDataCommand();
$this->_streamMessage($message);
$this->doDataCommand();
$this->streamMessage($message);
} else {
$this->reset();
}
@@ -434,23 +470,23 @@ abstract class Swift_Transport_AbstractSmtpTransport implements Swift_Transport
}
/** Send a message to the given To: recipients */
private function _sendTo(Swift_Mime_Message $message, $reversePath, array $to, array &$failedRecipients)
private function sendTo(Swift_Mime_SimpleMessage $message, $reversePath, array $to, array &$failedRecipients)
{
if (empty($to)) {
return 0;
}
return $this->_doMailTransaction($message, $reversePath, array_keys($to),
return $this->doMailTransaction($message, $reversePath, array_keys($to),
$failedRecipients);
}
/** Send a message to all Bcc: recipients */
private function _sendBcc(Swift_Mime_Message $message, $reversePath, array $bcc, array &$failedRecipients)
private function sendBcc(Swift_Mime_SimpleMessage $message, $reversePath, array $bcc, array &$failedRecipients)
{
$sent = 0;
foreach ($bcc as $forwardPath => $name) {
$message->setBcc(array($forwardPath => $name));
$sent += $this->_doMailTransaction(
$sent += $this->doMailTransaction(
$message, $reversePath, array($forwardPath), $failedRecipients
);
}
@@ -458,33 +494,14 @@ abstract class Swift_Transport_AbstractSmtpTransport implements Swift_Transport
return $sent;
}
/** Try to determine the hostname of the server this is run on */
private function _lookupHostname()
{
if (!empty($_SERVER['SERVER_NAME'])
&& $this->_isFqdn($_SERVER['SERVER_NAME'])) {
$this->_domain = $_SERVER['SERVER_NAME'];
} elseif (!empty($_SERVER['SERVER_ADDR'])) {
$this->_domain = sprintf('[%s]', $_SERVER['SERVER_ADDR']);
}
}
/** Determine is the $hostname is a fully-qualified name */
private function _isFqdn($hostname)
{
// We could do a really thorough check, but there's really no point
if (false !== $dotPos = strpos($hostname, '.')) {
return ($dotPos > 0) && ($dotPos != strlen($hostname) - 1);
}
return false;
}
/**
* Destructor.
*/
public function __destruct()
{
$this->stop();
try {
$this->stop();
} catch (Exception $e) {
}
}
}

View File

@@ -40,7 +40,7 @@ class Swift_Transport_Esmtp_Auth_CramMd5Authenticator implements Swift_Transport
$challenge = $agent->executeCommand("AUTH CRAM-MD5\r\n", array(334));
$challenge = base64_decode(substr($challenge, 4));
$message = base64_encode(
$username.' '.$this->_getResponse($password, $challenge)
$username.' '.$this->getResponse($password, $challenge)
);
$agent->executeCommand(sprintf("%s\r\n", $message), array(235));
@@ -60,7 +60,7 @@ class Swift_Transport_Esmtp_Auth_CramMd5Authenticator implements Swift_Transport
*
* @return string
*/
private function _getResponse($secret, $challenge)
private function getResponse($secret, $challenge)
{
if (strlen($secret) > 64) {
$secret = pack('H32', md5($secret));

View File

@@ -13,7 +13,7 @@
/**
* Handles NTLM authentication.
*
* @author Ward Peeters <ward@coding-tech.com>
* @author Ward Peeters <ward@coding-tech.com>
*/
class Swift_Transport_Esmtp_Auth_NTLMAuthenticator implements Swift_Transport_Esmtp_Authenticator
{
@@ -38,19 +38,17 @@ class Swift_Transport_Esmtp_Auth_NTLMAuthenticator implements Swift_Transport_Es
* @param string $password
*
* @return bool
*
* @throws \LogicException
*/
public function authenticate(Swift_Transport_SmtpAgent $agent, $username, $password)
{
if (!function_exists('mcrypt_module_open')) {
throw new LogicException('The mcrypt functions need to be enabled to use the NTLM authenticator.');
}
if (!function_exists('openssl_random_pseudo_bytes')) {
if (!function_exists('openssl_encrypt')) {
throw new LogicException('The OpenSSL extension must be enabled to use the NTLM authenticator.');
}
if (!function_exists('bcmul')) {
throw new LogicException('The BCMatch functions must be enabled to use the NTLM authenticator.');
throw new LogicException('The BCMath functions must be enabled to use the NTLM authenticator.');
}
try {
@@ -60,7 +58,7 @@ class Swift_Transport_Esmtp_Auth_NTLMAuthenticator implements Swift_Transport_Es
// extra parameters for our unit cases
$timestamp = func_num_args() > 3 ? func_get_arg(3) : $this->getCorrectTimestamp(bcmul(microtime(true), '1000'));
$client = func_num_args() > 4 ? func_get_arg(4) : $this->getRandomBytes(8);
$client = func_num_args() > 4 ? func_get_arg(4) : random_bytes(8);
// Message 3 response
$this->sendMessage3($response, $username, $password, $timestamp, $client, $agent);
@@ -125,10 +123,10 @@ class Swift_Transport_Esmtp_Auth_NTLMAuthenticator implements Swift_Transport_Es
$responseHex = bin2hex($response);
$length = floor(hexdec(substr($responseHex, 28, 4)) / 256) * 2;
$offset = floor(hexdec(substr($responseHex, 32, 4)) / 256) * 2;
$challenge = $this->hex2bin(substr($responseHex, 48, 16));
$context = $this->hex2bin(substr($responseHex, 64, 16));
$targetInfoH = $this->hex2bin(substr($responseHex, 80, 16));
$targetName = $this->hex2bin(substr($responseHex, $offset, $length));
$challenge = hex2bin(substr($responseHex, 48, 16));
$context = hex2bin(substr($responseHex, 64, 16));
$targetInfoH = hex2bin(substr($responseHex, 80, 16));
$targetName = hex2bin(substr($responseHex, $offset, $length));
$offset = floor(hexdec(substr($responseHex, 88, 4)) / 256) * 2;
$targetInfoBlock = substr($responseHex, $offset);
list($domainName, $serverName, $DNSDomainName, $DNSServerName, $terminatorByte) = $this->readSubBlock($targetInfoBlock);
@@ -142,7 +140,7 @@ class Swift_Transport_Esmtp_Auth_NTLMAuthenticator implements Swift_Transport_Es
$serverName,
$DNSDomainName,
$DNSServerName,
$this->hex2bin($targetInfoBlock),
hex2bin($targetInfoBlock),
$terminatorByte,
);
}
@@ -165,7 +163,7 @@ class Swift_Transport_Esmtp_Auth_NTLMAuthenticator implements Swift_Transport_Es
while ($offset < $length) {
$blockLength = hexdec(substr(substr($block, $offset, 8), -4)) / 256;
$offset += 8;
$data[] = $this->hex2bin(substr($block, $offset, $blockLength * 2));
$data[] = hex2bin(substr($block, $offset, $blockLength * 2));
$offset += $blockLength * 2;
}
@@ -300,9 +298,14 @@ class Swift_Transport_Esmtp_Auth_NTLMAuthenticator implements Swift_Transport_Es
return explode('\\', $name);
}
list($user, $domain) = explode('@', $name);
if (false !== strpos($name, '@')) {
list($user, $domain) = explode('@', $name);
return array($domain, $user);
return array($domain, $user);
}
// no domain passed
return array('', $name);
}
/**
@@ -365,11 +368,9 @@ class Swift_Transport_Esmtp_Auth_NTLMAuthenticator implements Swift_Transport_Es
protected function getCorrectTimestamp($time)
{
// Get our timestamp (tricky!)
bcscale(0);
$time = number_format($time, 0, '.', ''); // save microtime to string
$time = bcadd($time, '11644473600000'); // add epoch time
$time = bcmul($time, 10000); // tenths of a microsecond.
$time = bcadd($time, '11644473600000', 0); // add epoch time
$time = bcmul($time, 10000, 0); // tenths of a microsecond.
$binary = $this->si2bin($time, 64); // create 64 bit binary string
$timestamp = '';
@@ -459,10 +460,11 @@ class Swift_Transport_Esmtp_Auth_NTLMAuthenticator implements Swift_Transport_Es
}
}
return $this->hex2bin(implode('', $material));
return hex2bin(implode('', $material));
}
/** HELPER FUNCTIONS */
/**
* Create our security buffer depending on length and offset.
*
@@ -538,7 +540,7 @@ class Swift_Transport_Esmtp_Auth_NTLMAuthenticator implements Swift_Transport_Es
protected function createByte($input, $bytes = 4, $isHex = true)
{
if ($isHex) {
$byte = $this->hex2bin(str_pad($input, $bytes * 2, '00'));
$byte = hex2bin(str_pad($input, $bytes * 2, '00'));
} else {
$byte = str_pad($input, $bytes, "\x00");
}
@@ -546,39 +548,19 @@ class Swift_Transport_Esmtp_Auth_NTLMAuthenticator implements Swift_Transport_Es
return $byte;
}
/**
* Create random bytes.
*
* @param $length
*
* @return string
*/
protected function getRandomBytes($length)
{
$bytes = openssl_random_pseudo_bytes($length, $strong);
if (false !== $bytes && true === $strong) {
return $bytes;
}
throw new RuntimeException('OpenSSL did not produce a secure random number.');
}
/** ENCRYPTION ALGORITHMS */
/**
* DES Encryption.
*
* @param string $value
* @param string $value An 8-byte string
* @param string $key
*
* @return string
*/
protected function desEncrypt($value, $key)
{
$cipher = mcrypt_module_open(MCRYPT_DES, '', 'ecb', '');
mcrypt_generic_init($cipher, $key, mcrypt_create_iv(mcrypt_enc_get_iv_size($cipher), MCRYPT_DEV_RANDOM));
return mcrypt_generic($cipher, $value);
return substr(openssl_encrypt($value, 'DES-ECB', $key, \OPENSSL_RAW_DATA), 0, 8);
}
/**
@@ -616,7 +598,7 @@ class Swift_Transport_Esmtp_Auth_NTLMAuthenticator implements Swift_Transport_Es
{
$input = $this->convertTo16bit($input);
return function_exists('hash') ? $this->hex2bin(hash('md4', $input)) : mhash(MHASH_MD4, $input);
return function_exists('hash') ? hex2bin(hash('md4', $input)) : mhash(MHASH_MD4, $input);
}
/**
@@ -631,22 +613,6 @@ class Swift_Transport_Esmtp_Auth_NTLMAuthenticator implements Swift_Transport_Es
return iconv('UTF-8', 'UTF-16LE', $input);
}
/**
* Hex2bin replacement for < PHP 5.4.
*
* @param string $hex
*
* @return string Binary
*/
protected function hex2bin($hex)
{
if (function_exists('hex2bin')) {
return hex2bin($hex);
} else {
return pack('H*', $hex);
}
}
/**
* @param string $message
*/
@@ -671,7 +637,7 @@ class Swift_Transport_Esmtp_Auth_NTLMAuthenticator implements Swift_Transport_Es
'Target Information Terminator',
);
$data = $this->parseMessage2($this->hex2bin($message));
$data = $this->parseMessage2(hex2bin($message));
foreach ($map as $key => $value) {
echo bin2hex($data[$key]).' - '.$data[$key].' ||| '.$value."<br />\n";
@@ -717,7 +683,7 @@ class Swift_Transport_Esmtp_Auth_NTLMAuthenticator implements Swift_Transport_Es
);
foreach ($map as $key => $value) {
echo $data[$key].' - '.$this->hex2bin($data[$key]).' ||| '.$value."<br />\n";
echo $data[$key].' - '.hex2bin($data[$key]).' ||| '.$value."<br />\n";
}
}

View File

@@ -13,7 +13,7 @@
*
* Example:
* <code>
* $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587, 'tls')
* $transport = (new Swift_SmtpTransport('smtp.gmail.com', 587, 'tls'))
* ->setAuthMode('XOAUTH2')
* ->setUsername('YOUR_EMAIL_ADDRESS')
* ->setPassword('YOUR_ACCESS_TOKEN');

View File

@@ -20,35 +20,35 @@ class Swift_Transport_Esmtp_AuthHandler implements Swift_Transport_EsmtpHandler
*
* @var Swift_Transport_Esmtp_Authenticator[]
*/
private $_authenticators = array();
private $authenticators = array();
/**
* The username for authentication.
*
* @var string
*/
private $_username;
private $username;
/**
* The password for authentication.
*
* @var string
*/
private $_password;
private $password;
/**
* The auth mode for authentication.
*
* @var string
*/
private $_auth_mode;
private $auth_mode;
/**
* The ESMTP AUTH parameters available.
*
* @var string[]
*/
private $_esmtpParams = array();
private $esmtpParams = array();
/**
* Create a new AuthHandler with $authenticators for support.
@@ -67,7 +67,7 @@ class Swift_Transport_Esmtp_AuthHandler implements Swift_Transport_EsmtpHandler
*/
public function setAuthenticators(array $authenticators)
{
$this->_authenticators = $authenticators;
$this->authenticators = $authenticators;
}
/**
@@ -77,7 +77,7 @@ class Swift_Transport_Esmtp_AuthHandler implements Swift_Transport_EsmtpHandler
*/
public function getAuthenticators()
{
return $this->_authenticators;
return $this->authenticators;
}
/**
@@ -87,7 +87,7 @@ class Swift_Transport_Esmtp_AuthHandler implements Swift_Transport_EsmtpHandler
*/
public function setUsername($username)
{
$this->_username = $username;
$this->username = $username;
}
/**
@@ -97,7 +97,7 @@ class Swift_Transport_Esmtp_AuthHandler implements Swift_Transport_EsmtpHandler
*/
public function getUsername()
{
return $this->_username;
return $this->username;
}
/**
@@ -107,7 +107,7 @@ class Swift_Transport_Esmtp_AuthHandler implements Swift_Transport_EsmtpHandler
*/
public function setPassword($password)
{
$this->_password = $password;
$this->password = $password;
}
/**
@@ -117,7 +117,7 @@ class Swift_Transport_Esmtp_AuthHandler implements Swift_Transport_EsmtpHandler
*/
public function getPassword()
{
return $this->_password;
return $this->password;
}
/**
@@ -127,7 +127,7 @@ class Swift_Transport_Esmtp_AuthHandler implements Swift_Transport_EsmtpHandler
*/
public function setAuthMode($mode)
{
$this->_auth_mode = $mode;
$this->auth_mode = $mode;
}
/**
@@ -137,7 +137,7 @@ class Swift_Transport_Esmtp_AuthHandler implements Swift_Transport_EsmtpHandler
*/
public function getAuthMode()
{
return $this->_auth_mode;
return $this->auth_mode;
}
/**
@@ -157,7 +157,7 @@ class Swift_Transport_Esmtp_AuthHandler implements Swift_Transport_EsmtpHandler
*/
public function setKeywordParams(array $parameters)
{
$this->_esmtpParams = $parameters;
$this->esmtpParams = $parameters;
}
/**
@@ -167,20 +167,20 @@ class Swift_Transport_Esmtp_AuthHandler implements Swift_Transport_EsmtpHandler
*/
public function afterEhlo(Swift_Transport_SmtpAgent $agent)
{
if ($this->_username) {
if ($this->username) {
$count = 0;
foreach ($this->_getAuthenticatorsForAgent() as $authenticator) {
foreach ($this->getAuthenticatorsForAgent() as $authenticator) {
if (in_array(strtolower($authenticator->getAuthKeyword()),
array_map('strtolower', $this->_esmtpParams))) {
array_map('strtolower', $this->esmtpParams))) {
++$count;
if ($authenticator->authenticate($agent, $this->_username, $this->_password)) {
if ($authenticator->authenticate($agent, $this->username, $this->password)) {
return;
}
}
}
throw new Swift_TransportException(
'Failed to authenticate on SMTP server with username "'.
$this->_username.'" using '.$count.' possible authenticators'
$this->username.'" using '.$count.' possible authenticators'
);
}
}
@@ -246,13 +246,13 @@ class Swift_Transport_Esmtp_AuthHandler implements Swift_Transport_EsmtpHandler
*
* @return array
*/
protected function _getAuthenticatorsForAgent()
protected function getAuthenticatorsForAgent()
{
if (!$mode = strtolower($this->_auth_mode)) {
return $this->_authenticators;
if (!$mode = strtolower($this->auth_mode)) {
return $this->authenticators;
}
foreach ($this->_authenticators as $authenticator) {
foreach ($this->authenticators as $authenticator) {
if (strtolower($authenticator->getAuthKeyword()) == $mode) {
return array($authenticator);
}

View File

@@ -20,21 +20,21 @@ class Swift_Transport_EsmtpTransport extends Swift_Transport_AbstractSmtpTranspo
*
* @var Swift_Transport_EsmtpHandler[]
*/
private $_handlers = array();
private $handlers = array();
/**
* ESMTP capabilities.
*
* @var string[]
*/
private $_capabilities = array();
private $capabilities = array();
/**
* Connection buffer parameters.
*
* @var array
*/
private $_params = array(
private $params = array(
'protocol' => 'tcp',
'host' => 'localhost',
'port' => 25,
@@ -51,10 +51,11 @@ class Swift_Transport_EsmtpTransport extends Swift_Transport_AbstractSmtpTranspo
* @param Swift_Transport_IoBuffer $buf
* @param Swift_Transport_EsmtpHandler[] $extensionHandlers
* @param Swift_Events_EventDispatcher $dispatcher
* @param string $localDomain
*/
public function __construct(Swift_Transport_IoBuffer $buf, array $extensionHandlers, Swift_Events_EventDispatcher $dispatcher)
public function __construct(Swift_Transport_IoBuffer $buf, array $extensionHandlers, Swift_Events_EventDispatcher $dispatcher, $localDomain = '127.0.0.1')
{
parent::__construct($buf, $dispatcher);
parent::__construct($buf, $dispatcher, $localDomain);
$this->setExtensionHandlers($extensionHandlers);
}
@@ -63,11 +64,11 @@ class Swift_Transport_EsmtpTransport extends Swift_Transport_AbstractSmtpTranspo
*
* @param string $host
*
* @return Swift_Transport_EsmtpTransport
* @return $this
*/
public function setHost($host)
{
$this->_params['host'] = $host;
$this->params['host'] = $host;
return $this;
}
@@ -79,7 +80,7 @@ class Swift_Transport_EsmtpTransport extends Swift_Transport_AbstractSmtpTranspo
*/
public function getHost()
{
return $this->_params['host'];
return $this->params['host'];
}
/**
@@ -87,11 +88,11 @@ class Swift_Transport_EsmtpTransport extends Swift_Transport_AbstractSmtpTranspo
*
* @param int $port
*
* @return Swift_Transport_EsmtpTransport
* @return $this
*/
public function setPort($port)
{
$this->_params['port'] = (int) $port;
$this->params['port'] = (int) $port;
return $this;
}
@@ -103,7 +104,7 @@ class Swift_Transport_EsmtpTransport extends Swift_Transport_AbstractSmtpTranspo
*/
public function getPort()
{
return $this->_params['port'];
return $this->params['port'];
}
/**
@@ -111,12 +112,12 @@ class Swift_Transport_EsmtpTransport extends Swift_Transport_AbstractSmtpTranspo
*
* @param int $timeout seconds
*
* @return Swift_Transport_EsmtpTransport
* @return $this
*/
public function setTimeout($timeout)
{
$this->_params['timeout'] = (int) $timeout;
$this->_buffer->setParam('timeout', (int) $timeout);
$this->params['timeout'] = (int) $timeout;
$this->buffer->setParam('timeout', (int) $timeout);
return $this;
}
@@ -128,7 +129,7 @@ class Swift_Transport_EsmtpTransport extends Swift_Transport_AbstractSmtpTranspo
*/
public function getTimeout()
{
return $this->_params['timeout'];
return $this->params['timeout'];
}
/**
@@ -136,16 +137,17 @@ class Swift_Transport_EsmtpTransport extends Swift_Transport_AbstractSmtpTranspo
*
* @param string $encryption
*
* @return Swift_Transport_EsmtpTransport
* @return $this
*/
public function setEncryption($encryption)
{
$encryption = strtolower($encryption);
if ('tls' == $encryption) {
$this->_params['protocol'] = 'tcp';
$this->_params['tls'] = true;
$this->params['protocol'] = 'tcp';
$this->params['tls'] = true;
} else {
$this->_params['protocol'] = $encryption;
$this->_params['tls'] = false;
$this->params['protocol'] = $encryption;
$this->params['tls'] = false;
}
return $this;
@@ -158,7 +160,7 @@ class Swift_Transport_EsmtpTransport extends Swift_Transport_AbstractSmtpTranspo
*/
public function getEncryption()
{
return $this->_params['tls'] ? 'tls' : $this->_params['protocol'];
return $this->params['tls'] ? 'tls' : $this->params['protocol'];
}
/**
@@ -166,11 +168,11 @@ class Swift_Transport_EsmtpTransport extends Swift_Transport_AbstractSmtpTranspo
*
* @param array $options
*
* @return Swift_Transport_EsmtpTransport
* @return $this
*/
public function setStreamOptions($options)
{
$this->_params['stream_context_options'] = $options;
$this->params['stream_context_options'] = $options;
return $this;
}
@@ -182,7 +184,7 @@ class Swift_Transport_EsmtpTransport extends Swift_Transport_AbstractSmtpTranspo
*/
public function getStreamOptions()
{
return $this->_params['stream_context_options'];
return $this->params['stream_context_options'];
}
/**
@@ -190,11 +192,11 @@ class Swift_Transport_EsmtpTransport extends Swift_Transport_AbstractSmtpTranspo
*
* @param string $source
*
* @return Swift_Transport_EsmtpTransport
* @return $this
*/
public function setSourceIp($source)
{
$this->_params['sourceIp'] = $source;
$this->params['sourceIp'] = $source;
return $this;
}
@@ -206,7 +208,7 @@ class Swift_Transport_EsmtpTransport extends Swift_Transport_AbstractSmtpTranspo
*/
public function getSourceIp()
{
return isset($this->_params['sourceIp']) ? $this->_params['sourceIp'] : null;
return $this->params['sourceIp'] ?? null;
}
/**
@@ -214,7 +216,7 @@ class Swift_Transport_EsmtpTransport extends Swift_Transport_AbstractSmtpTranspo
*
* @param Swift_Transport_EsmtpHandler[] $handlers
*
* @return Swift_Transport_EsmtpTransport
* @return $this
*/
public function setExtensionHandlers(array $handlers)
{
@@ -222,10 +224,11 @@ class Swift_Transport_EsmtpTransport extends Swift_Transport_AbstractSmtpTranspo
foreach ($handlers as $handler) {
$assoc[$handler->getHandledKeyword()] = $handler;
}
@uasort($assoc, array($this, '_sortHandlers'));
$this->_handlers = $assoc;
$this->_setHandlerParams();
uasort($assoc, function ($a, $b) {
return $a->getPriorityOver($b->getHandledKeyword());
});
$this->handlers = $assoc;
$this->setHandlerParams();
return $this;
}
@@ -237,7 +240,7 @@ class Swift_Transport_EsmtpTransport extends Swift_Transport_AbstractSmtpTranspo
*/
public function getExtensionHandlers()
{
return array_values($this->_handlers);
return array_values($this->handlers);
}
/**
@@ -257,7 +260,7 @@ class Swift_Transport_EsmtpTransport extends Swift_Transport_AbstractSmtpTranspo
$failures = (array) $failures;
$stopSignal = false;
$response = null;
foreach ($this->_getActiveHandlers() as $handler) {
foreach ($this->getActiveHandlers() as $handler) {
$response = $handler->onCommand(
$this, $command, $codes, $failures, $stopSignal
);
@@ -269,18 +272,16 @@ class Swift_Transport_EsmtpTransport extends Swift_Transport_AbstractSmtpTranspo
return parent::executeCommand($command, $codes, $failures);
}
// -- Mixin invocation code
/** Mixin handling method for ESMTP handlers */
public function __call($method, $args)
{
foreach ($this->_handlers as $handler) {
foreach ($this->handlers as $handler) {
if (in_array(strtolower($method),
array_map('strtolower', (array) $handler->exposeMixinMethods())
)) {
$return = call_user_func_array(array($handler, $method), $args);
// Allow fluid method calls
if (is_null($return) && substr($method, 0, 3) == 'set') {
if (null === $return && substr($method, 0, 3) == 'set') {
return $this;
} else {
return $return;
@@ -291,53 +292,53 @@ class Swift_Transport_EsmtpTransport extends Swift_Transport_AbstractSmtpTranspo
}
/** Get the params to initialize the buffer */
protected function _getBufferParams()
protected function getBufferParams()
{
return $this->_params;
return $this->params;
}
/** Overridden to perform EHLO instead */
protected function _doHeloCommand()
protected function doHeloCommand()
{
try {
$response = $this->executeCommand(
sprintf("EHLO %s\r\n", $this->_domain), array(250)
sprintf("EHLO %s\r\n", $this->domain), array(250)
);
} catch (Swift_TransportException $e) {
return parent::_doHeloCommand();
return parent::doHeloCommand();
}
if ($this->_params['tls']) {
if ($this->params['tls']) {
try {
$this->executeCommand("STARTTLS\r\n", array(220));
if (!$this->_buffer->startTLS()) {
if (!$this->buffer->startTLS()) {
throw new Swift_TransportException('Unable to connect with TLS encryption');
}
try {
$response = $this->executeCommand(
sprintf("EHLO %s\r\n", $this->_domain), array(250)
sprintf("EHLO %s\r\n", $this->domain), array(250)
);
} catch (Swift_TransportException $e) {
return parent::_doHeloCommand();
return parent::doHeloCommand();
}
} catch (Swift_TransportException $e) {
$this->_throwException($e);
$this->throwException($e);
}
}
$this->_capabilities = $this->_getCapabilities($response);
$this->_setHandlerParams();
foreach ($this->_getActiveHandlers() as $handler) {
$this->capabilities = $this->getCapabilities($response);
$this->setHandlerParams();
foreach ($this->getActiveHandlers() as $handler) {
$handler->afterEhlo($this);
}
}
/** Overridden to add Extension support */
protected function _doMailFromCommand($address)
protected function doMailFromCommand($address)
{
$handlers = $this->_getActiveHandlers();
$handlers = $this->getActiveHandlers();
$params = array();
foreach ($handlers as $handler) {
$params = array_merge($params, (array) $handler->getMailParams());
@@ -349,9 +350,9 @@ class Swift_Transport_EsmtpTransport extends Swift_Transport_AbstractSmtpTranspo
}
/** Overridden to add Extension support */
protected function _doRcptToCommand($address)
protected function doRcptToCommand($address)
{
$handlers = $this->_getActiveHandlers();
$handlers = $this->getActiveHandlers();
$params = array();
foreach ($handlers as $handler) {
$params = array_merge($params, (array) $handler->getRcptParams());
@@ -363,7 +364,7 @@ class Swift_Transport_EsmtpTransport extends Swift_Transport_AbstractSmtpTranspo
}
/** Determine ESMTP capabilities by function group */
private function _getCapabilities($ehloResponse)
private function getCapabilities($ehloResponse)
{
$capabilities = array();
$ehloResponse = trim($ehloResponse);
@@ -382,31 +383,25 @@ class Swift_Transport_EsmtpTransport extends Swift_Transport_AbstractSmtpTranspo
}
/** Set parameters which are used by each extension handler */
private function _setHandlerParams()
private function setHandlerParams()
{
foreach ($this->_handlers as $keyword => $handler) {
if (array_key_exists($keyword, $this->_capabilities)) {
$handler->setKeywordParams($this->_capabilities[$keyword]);
foreach ($this->handlers as $keyword => $handler) {
if (array_key_exists($keyword, $this->capabilities)) {
$handler->setKeywordParams($this->capabilities[$keyword]);
}
}
}
/** Get ESMTP handlers which are currently ok to use */
private function _getActiveHandlers()
private function getActiveHandlers()
{
$handlers = array();
foreach ($this->_handlers as $keyword => $handler) {
if (array_key_exists($keyword, $this->_capabilities)) {
foreach ($this->handlers as $keyword => $handler) {
if (array_key_exists($keyword, $this->capabilities)) {
$handlers[] = $handler;
}
}
return $handlers;
}
/** Custom sort for extension handler ordering */
private function _sortHandlers($a, $b)
{
return $a->getPriorityOver($b->getHandledKeyword());
}
}

View File

@@ -20,7 +20,31 @@ class Swift_Transport_FailoverTransport extends Swift_Transport_LoadBalancedTran
*
* @var Swift_Transport
*/
private $_currentTransport;
private $currentTransport;
// needed as __construct is called from elsewhere explicitly
public function __construct()
{
parent::__construct();
}
/**
* {@inheritdoc}
*/
public function ping()
{
$maxTransports = count($this->transports);
for ($i = 0; $i < $maxTransports
&& $transport = $this->getNextTransport(); ++$i) {
if ($transport->ping()) {
return true;
} else {
$this->killCurrentTransport();
}
}
return count($this->transports) > 0;
}
/**
* Send the given Message.
@@ -28,35 +52,35 @@ class Swift_Transport_FailoverTransport extends Swift_Transport_LoadBalancedTran
* Recipient/sender data will be retrieved from the Message API.
* The return value is the number of recipients who were accepted for delivery.
*
* @param Swift_Mime_Message $message
* @param Swift_Mime_SimpleMessage $message
* @param string[] $failedRecipients An array of failures by-reference
*
* @return int
*/
public function send(Swift_Mime_Message $message, &$failedRecipients = null)
public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
{
$maxTransports = count($this->_transports);
$maxTransports = count($this->transports);
$sent = 0;
$this->_lastUsedTransport = null;
$this->lastUsedTransport = null;
for ($i = 0; $i < $maxTransports
&& $transport = $this->_getNextTransport(); ++$i) {
&& $transport = $this->getNextTransport(); ++$i) {
try {
if (!$transport->isStarted()) {
$transport->start();
}
if ($sent = $transport->send($message, $failedRecipients)) {
$this->_lastUsedTransport = $transport;
$this->lastUsedTransport = $transport;
return $sent;
}
} catch (Swift_TransportException $e) {
$this->_killCurrentTransport();
$this->killCurrentTransport();
}
}
if (count($this->_transports) == 0) {
if (count($this->transports) == 0) {
throw new Swift_TransportException(
'All Transports in FailoverTransport failed, or no Transports available'
);
@@ -65,18 +89,18 @@ class Swift_Transport_FailoverTransport extends Swift_Transport_LoadBalancedTran
return $sent;
}
protected function _getNextTransport()
protected function getNextTransport()
{
if (!isset($this->_currentTransport)) {
$this->_currentTransport = parent::_getNextTransport();
if (!isset($this->currentTransport)) {
$this->currentTransport = parent::getNextTransport();
}
return $this->_currentTransport;
return $this->currentTransport;
}
protected function _killCurrentTransport()
protected function killCurrentTransport()
{
$this->_currentTransport = null;
parent::_killCurrentTransport();
$this->currentTransport = null;
parent::killCurrentTransport();
}
}

View File

@@ -20,21 +20,26 @@ class Swift_Transport_LoadBalancedTransport implements Swift_Transport
*
* @var Swift_Transport[]
*/
private $_deadTransports = array();
private $deadTransports = array();
/**
* The Transports which are used in rotation.
*
* @var Swift_Transport[]
*/
protected $_transports = array();
protected $transports = array();
/**
* The Transport used in the last successful send operation.
*
* @var Swift_Transport
*/
protected $_lastUsedTransport = null;
protected $lastUsedTransport = null;
// needed as __construct is called from elsewhere explicitly
public function __construct()
{
}
/**
* Set $transports to delegate to.
@@ -43,8 +48,8 @@ class Swift_Transport_LoadBalancedTransport implements Swift_Transport
*/
public function setTransports(array $transports)
{
$this->_transports = $transports;
$this->_deadTransports = array();
$this->transports = $transports;
$this->deadTransports = array();
}
/**
@@ -54,7 +59,7 @@ class Swift_Transport_LoadBalancedTransport implements Swift_Transport
*/
public function getTransports()
{
return array_merge($this->_transports, $this->_deadTransports);
return array_merge($this->transports, $this->deadTransports);
}
/**
@@ -64,7 +69,7 @@ class Swift_Transport_LoadBalancedTransport implements Swift_Transport
*/
public function getLastUsedTransport()
{
return $this->_lastUsedTransport;
return $this->lastUsedTransport;
}
/**
@@ -74,7 +79,7 @@ class Swift_Transport_LoadBalancedTransport implements Swift_Transport
*/
public function isStarted()
{
return count($this->_transports) > 0;
return count($this->transports) > 0;
}
/**
@@ -82,7 +87,7 @@ class Swift_Transport_LoadBalancedTransport implements Swift_Transport
*/
public function start()
{
$this->_transports = array_merge($this->_transports, $this->_deadTransports);
$this->transports = array_merge($this->transports, $this->deadTransports);
}
/**
@@ -90,44 +95,58 @@ class Swift_Transport_LoadBalancedTransport implements Swift_Transport
*/
public function stop()
{
foreach ($this->_transports as $transport) {
foreach ($this->transports as $transport) {
$transport->stop();
}
}
/**
* {@inheritdoc}
*/
public function ping()
{
foreach ($this->transports as $transport) {
if (!$transport->ping()) {
$this->killCurrentTransport();
}
}
return count($this->transports) > 0;
}
/**
* Send the given Message.
*
* Recipient/sender data will be retrieved from the Message API.
* The return value is the number of recipients who were accepted for delivery.
*
* @param Swift_Mime_Message $message
* @param Swift_Mime_SimpleMessage $message
* @param string[] $failedRecipients An array of failures by-reference
*
* @return int
*/
public function send(Swift_Mime_Message $message, &$failedRecipients = null)
public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
{
$maxTransports = count($this->_transports);
$maxTransports = count($this->transports);
$sent = 0;
$this->_lastUsedTransport = null;
$this->lastUsedTransport = null;
for ($i = 0; $i < $maxTransports
&& $transport = $this->_getNextTransport(); ++$i) {
&& $transport = $this->getNextTransport(); ++$i) {
try {
if (!$transport->isStarted()) {
$transport->start();
}
if ($sent = $transport->send($message, $failedRecipients)) {
$this->_lastUsedTransport = $transport;
$this->lastUsedTransport = $transport;
break;
}
} catch (Swift_TransportException $e) {
$this->_killCurrentTransport();
$this->killCurrentTransport();
}
}
if (count($this->_transports) == 0) {
if (count($this->transports) == 0) {
throw new Swift_TransportException(
'All Transports in LoadBalancedTransport failed, or no Transports available'
);
@@ -143,7 +162,7 @@ class Swift_Transport_LoadBalancedTransport implements Swift_Transport
*/
public function registerPlugin(Swift_Events_EventListener $plugin)
{
foreach ($this->_transports as $transport) {
foreach ($this->transports as $transport) {
$transport->registerPlugin($plugin);
}
}
@@ -153,10 +172,10 @@ class Swift_Transport_LoadBalancedTransport implements Swift_Transport
*
* @return Swift_Transport
*/
protected function _getNextTransport()
protected function getNextTransport()
{
if ($next = array_shift($this->_transports)) {
$this->_transports[] = $next;
if ($next = array_shift($this->transports)) {
$this->transports[] = $next;
}
return $next;
@@ -165,14 +184,14 @@ class Swift_Transport_LoadBalancedTransport implements Swift_Transport
/**
* Tag the currently used (top of stack) transport as dead/useless.
*/
protected function _killCurrentTransport()
protected function killCurrentTransport()
{
if ($transport = array_pop($this->_transports)) {
if ($transport = array_pop($this->transports)) {
try {
$transport->stop();
} catch (Exception $e) {
}
$this->_deadTransports[] = $transport;
$this->deadTransports[] = $transport;
}
}
}

View File

@@ -1,32 +0,0 @@
<?php
/*
* This file is part of SwiftMailer.
* (c) 2004-2009 Chris Corbyn
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* This interface intercepts calls to the mail() function.
*
* @author Chris Corbyn
*/
interface Swift_Transport_MailInvoker
{
/**
* Send mail via the mail() function.
*
* This method takes the same arguments as PHP mail().
*
* @param string $to
* @param string $subject
* @param string $body
* @param string $headers
* @param string $extraParams
*
* @return bool
*/
public function mail($to, $subject, $body, $headers = null, $extraParams = null);
}

View File

@@ -1,239 +0,0 @@
<?php
/*
* This file is part of SwiftMailer.
* (c) 2004-2009 Chris Corbyn
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Sends Messages using the mail() function.
*
* It is advised that users do not use this transport if at all possible
* since a number of plugin features cannot be used in conjunction with this
* transport due to the internal interface in PHP itself.
*
* The level of error reporting with this transport is incredibly weak, again
* due to limitations of PHP's internal mail() function. You'll get an
* all-or-nothing result from sending.
*
* @author Chris Corbyn
*/
class Swift_Transport_MailTransport implements Swift_Transport
{
/** Additional parameters to pass to mail() */
private $_extraParams = '-f%s';
/** The event dispatcher from the plugin API */
private $_eventDispatcher;
/** An invoker that calls the mail() function */
private $_invoker;
/**
* Create a new MailTransport with the $log.
*
* @param Swift_Transport_MailInvoker $invoker
* @param Swift_Events_EventDispatcher $eventDispatcher
*/
public function __construct(Swift_Transport_MailInvoker $invoker, Swift_Events_EventDispatcher $eventDispatcher)
{
$this->_invoker = $invoker;
$this->_eventDispatcher = $eventDispatcher;
}
/**
* Not used.
*/
public function isStarted()
{
return false;
}
/**
* Not used.
*/
public function start()
{
}
/**
* Not used.
*/
public function stop()
{
}
/**
* Set the additional parameters used on the mail() function.
*
* This string is formatted for sprintf() where %s is the sender address.
*
* @param string $params
*
* @return Swift_Transport_MailTransport
*/
public function setExtraParams($params)
{
$this->_extraParams = $params;
return $this;
}
/**
* Get the additional parameters used on the mail() function.
*
* This string is formatted for sprintf() where %s is the sender address.
*
* @return string
*/
public function getExtraParams()
{
return $this->_extraParams;
}
/**
* Send the given Message.
*
* Recipient/sender data will be retrieved from the Message API.
* The return value is the number of recipients who were accepted for delivery.
*
* @param Swift_Mime_Message $message
* @param string[] $failedRecipients An array of failures by-reference
*
* @return int
*/
public function send(Swift_Mime_Message $message, &$failedRecipients = null)
{
$failedRecipients = (array) $failedRecipients;
if ($evt = $this->_eventDispatcher->createSendEvent($this, $message)) {
$this->_eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed');
if ($evt->bubbleCancelled()) {
return 0;
}
}
$count = (
count((array) $message->getTo())
+ count((array) $message->getCc())
+ count((array) $message->getBcc())
);
$toHeader = $message->getHeaders()->get('To');
$subjectHeader = $message->getHeaders()->get('Subject');
if (!$toHeader) {
$this->_throwException(new Swift_TransportException('Cannot send message without a recipient'));
}
$to = $toHeader->getFieldBody();
$subject = $subjectHeader ? $subjectHeader->getFieldBody() : '';
$reversePath = $this->_getReversePath($message);
// Remove headers that would otherwise be duplicated
$message->getHeaders()->remove('To');
$message->getHeaders()->remove('Subject');
$messageStr = $message->toString();
$message->getHeaders()->set($toHeader);
$message->getHeaders()->set($subjectHeader);
// Separate headers from body
if (false !== $endHeaders = strpos($messageStr, "\r\n\r\n")) {
$headers = substr($messageStr, 0, $endHeaders)."\r\n"; //Keep last EOL
$body = substr($messageStr, $endHeaders + 4);
} else {
$headers = $messageStr."\r\n";
$body = '';
}
unset($messageStr);
if ("\r\n" != PHP_EOL) {
// Non-windows (not using SMTP)
$headers = str_replace("\r\n", PHP_EOL, $headers);
$subject = str_replace("\r\n", PHP_EOL, $subject);
$body = str_replace("\r\n", PHP_EOL, $body);
} else {
// Windows, using SMTP
$headers = str_replace("\r\n.", "\r\n..", $headers);
$subject = str_replace("\r\n.", "\r\n..", $subject);
$body = str_replace("\r\n.", "\r\n..", $body);
}
if ($this->_invoker->mail($to, $subject, $body, $headers,
sprintf($this->_extraParams, escapeshellarg($reversePath)))) {
if ($evt) {
$evt->setResult(Swift_Events_SendEvent::RESULT_SUCCESS);
$evt->setFailedRecipients($failedRecipients);
$this->_eventDispatcher->dispatchEvent($evt, 'sendPerformed');
}
} else {
$failedRecipients = array_merge(
$failedRecipients,
array_keys((array) $message->getTo()),
array_keys((array) $message->getCc()),
array_keys((array) $message->getBcc())
);
if ($evt) {
$evt->setResult(Swift_Events_SendEvent::RESULT_FAILED);
$evt->setFailedRecipients($failedRecipients);
$this->_eventDispatcher->dispatchEvent($evt, 'sendPerformed');
}
$message->generateId();
$count = 0;
}
return $count;
}
/**
* Register a plugin.
*
* @param Swift_Events_EventListener $plugin
*/
public function registerPlugin(Swift_Events_EventListener $plugin)
{
$this->_eventDispatcher->bindEventListener($plugin);
}
/** Throw a TransportException, first sending it to any listeners */
protected function _throwException(Swift_TransportException $e)
{
if ($evt = $this->_eventDispatcher->createTransportExceptionEvent($this, $e)) {
$this->_eventDispatcher->dispatchEvent($evt, 'exceptionThrown');
if (!$evt->bubbleCancelled()) {
throw $e;
}
} else {
throw $e;
}
}
/** Determine the best-use reverse path for this message */
private function _getReversePath(Swift_Mime_Message $message)
{
$return = $message->getReturnPath();
$sender = $message->getSender();
$from = $message->getFrom();
$path = null;
if (!empty($return)) {
$path = $return;
} elseif (!empty($sender)) {
$keys = array_keys($sender);
$path = array_shift($keys);
} elseif (!empty($from)) {
$keys = array_keys($from);
$path = array_shift($keys);
}
return $path;
}
}

View File

@@ -16,14 +16,14 @@
class Swift_Transport_NullTransport implements Swift_Transport
{
/** The event dispatcher from the plugin API */
private $_eventDispatcher;
private $eventDispatcher;
/**
* Constructor.
*/
public function __construct(Swift_Events_EventDispatcher $eventDispatcher)
{
$this->_eventDispatcher = $eventDispatcher;
$this->eventDispatcher = $eventDispatcher;
}
/**
@@ -50,18 +50,26 @@ class Swift_Transport_NullTransport implements Swift_Transport
{
}
/**
* {@inheritdoc}
*/
public function ping()
{
return true;
}
/**
* Sends the given message.
*
* @param Swift_Mime_Message $message
* @param Swift_Mime_SimpleMessage $message
* @param string[] $failedRecipients An array of failures by-reference
*
* @return int The number of sent emails
*/
public function send(Swift_Mime_Message $message, &$failedRecipients = null)
public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
{
if ($evt = $this->_eventDispatcher->createSendEvent($this, $message)) {
$this->_eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed');
if ($evt = $this->eventDispatcher->createSendEvent($this, $message)) {
$this->eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed');
if ($evt->bubbleCancelled()) {
return 0;
}
@@ -69,7 +77,7 @@ class Swift_Transport_NullTransport implements Swift_Transport
if ($evt) {
$evt->setResult(Swift_Events_SendEvent::RESULT_SUCCESS);
$this->_eventDispatcher->dispatchEvent($evt, 'sendPerformed');
$this->eventDispatcher->dispatchEvent($evt, 'sendPerformed');
}
$count = (
@@ -88,6 +96,6 @@ class Swift_Transport_NullTransport implements Swift_Transport
*/
public function registerPlugin(Swift_Events_EventListener $plugin)
{
$this->_eventDispatcher->bindEventListener($plugin);
$this->eventDispatcher->bindEventListener($plugin);
}
}

View File

@@ -24,7 +24,7 @@ class Swift_Transport_SendmailTransport extends Swift_Transport_AbstractSmtpTran
*
* @var array
*/
private $_params = array(
private $params = array(
'timeout' => 30,
'blocking' => 1,
'command' => '/usr/sbin/sendmail -bs',
@@ -36,10 +36,11 @@ class Swift_Transport_SendmailTransport extends Swift_Transport_AbstractSmtpTran
*
* @param Swift_Transport_IoBuffer $buf
* @param Swift_Events_EventDispatcher $dispatcher
* @param string $localDomain
*/
public function __construct(Swift_Transport_IoBuffer $buf, Swift_Events_EventDispatcher $dispatcher)
public function __construct(Swift_Transport_IoBuffer $buf, Swift_Events_EventDispatcher $dispatcher, $localDomain = '127.0.0.1')
{
parent::__construct($buf, $dispatcher);
parent::__construct($buf, $dispatcher, $localDomain);
}
/**
@@ -64,11 +65,11 @@ class Swift_Transport_SendmailTransport extends Swift_Transport_AbstractSmtpTran
*
* @param string $command
*
* @return Swift_Transport_SendmailTransport
* @return $this
*/
public function setCommand($command)
{
$this->_params['command'] = $command;
$this->params['command'] = $command;
return $this;
}
@@ -80,7 +81,7 @@ class Swift_Transport_SendmailTransport extends Swift_Transport_AbstractSmtpTran
*/
public function getCommand()
{
return $this->_params['command'];
return $this->params['command'];
}
/**
@@ -92,12 +93,12 @@ class Swift_Transport_SendmailTransport extends Swift_Transport_AbstractSmtpTran
* NOTE: If using 'sendmail -t' you will not be aware of any failures until
* they bounce (i.e. send() will always return 100% success).
*
* @param Swift_Mime_Message $message
* @param Swift_Mime_SimpleMessage $message
* @param string[] $failedRecipients An array of failures by-reference
*
* @return int
*/
public function send(Swift_Mime_Message $message, &$failedRecipients = null)
public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
{
$failedRecipients = (array) $failedRecipients;
$command = $this->getCommand();
@@ -105,18 +106,18 @@ class Swift_Transport_SendmailTransport extends Swift_Transport_AbstractSmtpTran
$count = 0;
if (false !== strpos($command, ' -t')) {
if ($evt = $this->_eventDispatcher->createSendEvent($this, $message)) {
$this->_eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed');
if ($evt = $this->eventDispatcher->createSendEvent($this, $message)) {
$this->eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed');
if ($evt->bubbleCancelled()) {
return 0;
}
}
if (false === strpos($command, ' -f')) {
$command .= ' -f'.escapeshellarg($this->_getReversePath($message));
$command .= ' -f'.escapeshellarg($this->getReversePath($message));
}
$buffer->initialize(array_merge($this->_params, array('command' => $command)));
$buffer->initialize(array_merge($this->params, array('command' => $command)));
if (false === strpos($command, ' -i') && false === strpos($command, ' -oi')) {
$buffer->setWriteTranslations(array("\r\n" => "\n", "\n." => "\n.."));
@@ -136,14 +137,14 @@ class Swift_Transport_SendmailTransport extends Swift_Transport_AbstractSmtpTran
if ($evt) {
$evt->setResult(Swift_Events_SendEvent::RESULT_SUCCESS);
$evt->setFailedRecipients($failedRecipients);
$this->_eventDispatcher->dispatchEvent($evt, 'sendPerformed');
$this->eventDispatcher->dispatchEvent($evt, 'sendPerformed');
}
$message->generateId();
} elseif (false !== strpos($command, ' -bs')) {
$count = parent::send($message, $failedRecipients);
} else {
$this->_throwException(new Swift_TransportException(
$this->throwException(new Swift_TransportException(
'Unsupported sendmail command flags ['.$command.']. '.
'Must be one of "-bs" or "-t" but can include additional flags.'
));
@@ -153,8 +154,8 @@ class Swift_Transport_SendmailTransport extends Swift_Transport_AbstractSmtpTran
}
/** Get the params to initialize the buffer */
protected function _getBufferParams()
protected function getBufferParams()
{
return $this->_params;
return $this->params;
}
}

View File

@@ -1,39 +0,0 @@
<?php
/*
* This file is part of SwiftMailer.
* (c) 2004-2009 Chris Corbyn
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* This is the implementation class for {@link Swift_Transport_MailInvoker}.
*
* @author Chris Corbyn
*/
class Swift_Transport_SimpleMailInvoker implements Swift_Transport_MailInvoker
{
/**
* Send mail via the mail() function.
*
* This method takes the same arguments as PHP mail().
*
* @param string $to
* @param string $subject
* @param string $body
* @param string $headers
* @param string $extraParams
*
* @return bool
*/
public function mail($to, $subject, $body, $headers = null, $extraParams = null)
{
if (!ini_get('safe_mode')) {
return @mail($to, $subject, $body, $headers, $extraParams);
}
return @mail($to, $subject, $body, $headers);
}
}

View File

@@ -16,18 +16,18 @@
class Swift_Transport_SpoolTransport implements Swift_Transport
{
/** The spool instance */
private $_spool;
private $spool;
/** The event dispatcher from the plugin API */
private $_eventDispatcher;
private $eventDispatcher;
/**
* Constructor.
*/
public function __construct(Swift_Events_EventDispatcher $eventDispatcher, Swift_Spool $spool = null)
{
$this->_eventDispatcher = $eventDispatcher;
$this->_spool = $spool;
$this->eventDispatcher = $eventDispatcher;
$this->spool = $spool;
}
/**
@@ -35,11 +35,11 @@ class Swift_Transport_SpoolTransport implements Swift_Transport
*
* @param Swift_Spool $spool
*
* @return Swift_Transport_SpoolTransport
* @return $this
*/
public function setSpool(Swift_Spool $spool)
{
$this->_spool = $spool;
$this->spool = $spool;
return $this;
}
@@ -51,7 +51,7 @@ class Swift_Transport_SpoolTransport implements Swift_Transport
*/
public function getSpool()
{
return $this->_spool;
return $this->spool;
}
/**
@@ -78,28 +78,36 @@ class Swift_Transport_SpoolTransport implements Swift_Transport
{
}
/**
* {@inheritdoc}
*/
public function ping()
{
return true;
}
/**
* Sends the given message.
*
* @param Swift_Mime_Message $message
* @param Swift_Mime_SimpleMessage $message
* @param string[] $failedRecipients An array of failures by-reference
*
* @return int The number of sent e-mail's
*/
public function send(Swift_Mime_Message $message, &$failedRecipients = null)
public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
{
if ($evt = $this->_eventDispatcher->createSendEvent($this, $message)) {
$this->_eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed');
if ($evt = $this->eventDispatcher->createSendEvent($this, $message)) {
$this->eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed');
if ($evt->bubbleCancelled()) {
return 0;
}
}
$success = $this->_spool->queueMessage($message);
$success = $this->spool->queueMessage($message);
if ($evt) {
$evt->setResult($success ? Swift_Events_SendEvent::RESULT_SPOOLED : Swift_Events_SendEvent::RESULT_FAILED);
$this->_eventDispatcher->dispatchEvent($evt, 'sendPerformed');
$this->eventDispatcher->dispatchEvent($evt, 'sendPerformed');
}
return 1;
@@ -112,6 +120,6 @@ class Swift_Transport_SpoolTransport implements Swift_Transport
*/
public function registerPlugin(Swift_Events_EventListener $plugin)
{
$this->_eventDispatcher->bindEventListener($plugin);
$this->eventDispatcher->bindEventListener($plugin);
}
}

View File

@@ -11,27 +11,27 @@
/**
* A generic IoBuffer implementation supporting remote sockets and local processes.
*
* @author Chris Corbyn
* @author Chris Corbyn
*/
class Swift_Transport_StreamBuffer extends Swift_ByteStream_AbstractFilterableInputStream implements Swift_Transport_IoBuffer
{
/** A primary socket */
private $_stream;
private $stream;
/** The input stream */
private $_in;
private $in;
/** The output stream */
private $_out;
private $out;
/** Buffer initialization parameters */
private $_params = array();
private $params = array();
/** The ReplacementFilterFactory */
private $_replacementFactory;
private $replacementFactory;
/** Translations performed on data being streamed into the buffer */
private $_translations = array();
private $translations = array();
/**
* Create a new StreamBuffer using $replacementFactory for transformations.
@@ -40,7 +40,7 @@ class Swift_Transport_StreamBuffer extends Swift_ByteStream_AbstractFilterableIn
*/
public function __construct(Swift_ReplacementFilterFactory $replacementFactory)
{
$this->_replacementFactory = $replacementFactory;
$this->replacementFactory = $replacementFactory;
}
/**
@@ -52,14 +52,14 @@ class Swift_Transport_StreamBuffer extends Swift_ByteStream_AbstractFilterableIn
*/
public function initialize(array $params)
{
$this->_params = $params;
$this->params = $params;
switch ($params['type']) {
case self::TYPE_PROCESS:
$this->_establishProcessConnection();
$this->establishProcessConnection();
break;
case self::TYPE_SOCKET:
default:
$this->_establishSocketConnection();
$this->establishSocketConnection();
break;
}
}
@@ -72,27 +72,26 @@ class Swift_Transport_StreamBuffer extends Swift_ByteStream_AbstractFilterableIn
*/
public function setParam($param, $value)
{
if (isset($this->_stream)) {
if (isset($this->stream)) {
switch ($param) {
case 'timeout':
if ($this->_stream) {
stream_set_timeout($this->_stream, $value);
if ($this->stream) {
stream_set_timeout($this->stream, $value);
}
break;
case 'blocking':
if ($this->_stream) {
stream_set_blocking($this->_stream, 1);
if ($this->stream) {
stream_set_blocking($this->stream, 1);
}
}
}
$this->_params[$param] = $value;
$this->params[$param] = $value;
}
public function startTLS()
{
return stream_socket_enable_crypto($this->_stream, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
return stream_socket_enable_crypto($this->stream, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
}
/**
@@ -100,22 +99,22 @@ class Swift_Transport_StreamBuffer extends Swift_ByteStream_AbstractFilterableIn
*/
public function terminate()
{
if (isset($this->_stream)) {
switch ($this->_params['type']) {
if (isset($this->stream)) {
switch ($this->params['type']) {
case self::TYPE_PROCESS:
fclose($this->_in);
fclose($this->_out);
proc_close($this->_stream);
fclose($this->in);
fclose($this->out);
proc_close($this->stream);
break;
case self::TYPE_SOCKET:
default:
fclose($this->_stream);
fclose($this->stream);
break;
}
}
$this->_stream = null;
$this->_out = null;
$this->_in = null;
$this->stream = null;
$this->out = null;
$this->in = null;
}
/**
@@ -128,19 +127,19 @@ class Swift_Transport_StreamBuffer extends Swift_ByteStream_AbstractFilterableIn
*/
public function setWriteTranslations(array $replacements)
{
foreach ($this->_translations as $search => $replace) {
foreach ($this->translations as $search => $replace) {
if (!isset($replacements[$search])) {
$this->removeFilter($search);
unset($this->_translations[$search]);
unset($this->translations[$search]);
}
}
foreach ($replacements as $search => $replace) {
if (!isset($this->_translations[$search])) {
if (!isset($this->translations[$search])) {
$this->addFilter(
$this->_replacementFactory->createFilter($search, $replace), $search
$this->replacementFactory->createFilter($search, $replace), $search
);
$this->_translations[$search] = true;
$this->translations[$search] = true;
}
}
}
@@ -153,20 +152,20 @@ class Swift_Transport_StreamBuffer extends Swift_ByteStream_AbstractFilterableIn
*
* @param int $sequence of last write to scan from
*
* @throws Swift_IoException
*
* @return string
*
* @throws Swift_IoException
*/
public function readLine($sequence)
{
if (isset($this->_out) && !feof($this->_out)) {
$line = fgets($this->_out);
if (isset($this->out) && !feof($this->out)) {
$line = fgets($this->out);
if (strlen($line) == 0) {
$metas = stream_get_meta_data($this->_out);
$metas = stream_get_meta_data($this->out);
if ($metas['timed_out']) {
throw new Swift_IoException(
'Connection to '.
$this->_getReadConnectionDescription().
$this->getReadConnectionDescription().
' Timed Out'
);
}
@@ -185,20 +184,20 @@ class Swift_Transport_StreamBuffer extends Swift_ByteStream_AbstractFilterableIn
*
* @param int $length
*
* @throws Swift_IoException
*
* @return string|bool
*
* @throws Swift_IoException
*/
public function read($length)
{
if (isset($this->_out) && !feof($this->_out)) {
$ret = fread($this->_out, $length);
if (isset($this->out) && !feof($this->out)) {
$ret = fread($this->out, $length);
if (strlen($ret) == 0) {
$metas = stream_get_meta_data($this->_out);
$metas = stream_get_meta_data($this->out);
if ($metas['timed_out']) {
throw new Swift_IoException(
'Connection to '.
$this->_getReadConnectionDescription().
$this->getReadConnectionDescription().
' Timed Out'
);
}
@@ -214,22 +213,22 @@ class Swift_Transport_StreamBuffer extends Swift_ByteStream_AbstractFilterableIn
}
/** Flush the stream contents */
protected function _flush()
protected function flush()
{
if (isset($this->_in)) {
fflush($this->_in);
if (isset($this->in)) {
fflush($this->in);
}
}
/** Write this bytes to the stream */
protected function _commit($bytes)
protected function doCommit($bytes)
{
if (isset($this->_in)) {
if (isset($this->in)) {
$bytesToWrite = strlen($bytes);
$totalBytesWritten = 0;
while ($totalBytesWritten < $bytesToWrite) {
$bytesWritten = fwrite($this->_in, substr($bytes, $totalBytesWritten));
$bytesWritten = fwrite($this->in, substr($bytes, $totalBytesWritten));
if (false === $bytesWritten || 0 === $bytesWritten) {
break;
}
@@ -238,7 +237,7 @@ class Swift_Transport_StreamBuffer extends Swift_ByteStream_AbstractFilterableIn
}
if ($totalBytesWritten > 0) {
return ++$this->_sequence;
return ++$this->sequence;
}
}
}
@@ -246,77 +245,79 @@ class Swift_Transport_StreamBuffer extends Swift_ByteStream_AbstractFilterableIn
/**
* Establishes a connection to a remote server.
*/
private function _establishSocketConnection()
private function establishSocketConnection()
{
$host = $this->_params['host'];
if (!empty($this->_params['protocol'])) {
$host = $this->_params['protocol'].'://'.$host;
$host = $this->params['host'];
if (!empty($this->params['protocol'])) {
$host = $this->params['protocol'].'://'.$host;
}
$timeout = 15;
if (!empty($this->_params['timeout'])) {
$timeout = $this->_params['timeout'];
if (!empty($this->params['timeout'])) {
$timeout = $this->params['timeout'];
}
$options = array();
if (!empty($this->_params['sourceIp'])) {
$options['socket']['bindto'] = $this->_params['sourceIp'].':0';
if (!empty($this->params['sourceIp'])) {
$options['socket']['bindto'] = $this->params['sourceIp'].':0';
}
if (isset($this->_params['stream_context_options'])) {
$options = array_merge($options, $this->_params['stream_context_options']);
if (isset($this->params['stream_context_options'])) {
$options = array_merge($options, $this->params['stream_context_options']);
}
$streamContext = stream_context_create($options);
$this->_stream = @stream_socket_client($host.':'.$this->_params['port'], $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $streamContext);
if (false === $this->_stream) {
$this->stream = @stream_socket_client($host.':'.$this->params['port'], $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $streamContext);
if (false === $this->stream) {
throw new Swift_TransportException(
'Connection could not be established with host '.$this->_params['host'].
'Connection could not be established with host '.$this->params['host'].
' ['.$errstr.' #'.$errno.']'
);
}
if (!empty($this->_params['blocking'])) {
stream_set_blocking($this->_stream, 1);
if (!empty($this->params['blocking'])) {
stream_set_blocking($this->stream, 1);
} else {
stream_set_blocking($this->_stream, 0);
stream_set_blocking($this->stream, 0);
}
stream_set_timeout($this->_stream, $timeout);
$this->_in = &$this->_stream;
$this->_out = &$this->_stream;
stream_set_timeout($this->stream, $timeout);
$this->in = &$this->stream;
$this->out = &$this->stream;
}
/**
* Opens a process for input/output.
*/
private function _establishProcessConnection()
private function establishProcessConnection()
{
$command = $this->_params['command'];
$command = $this->params['command'];
$descriptorSpec = array(
0 => array('pipe', 'r'),
1 => array('pipe', 'w'),
2 => array('pipe', 'w'),
);
$this->_stream = proc_open($command, $descriptorSpec, $pipes);
$pipes = array();
$this->stream = proc_open($command, $descriptorSpec, $pipes);
stream_set_blocking($pipes[2], 0);
if ($err = stream_get_contents($pipes[2])) {
throw new Swift_TransportException(
'Process could not be started ['.$err.']'
);
}
$this->_in = &$pipes[0];
$this->_out = &$pipes[1];
$this->in = &$pipes[0];
$this->out = &$pipes[1];
}
private function _getReadConnectionDescription()
private function getReadConnectionDescription()
{
switch ($this->_params['type']) {
switch ($this->params['type']) {
case self::TYPE_PROCESS:
return 'Process '.$this->_params['command'];
return 'Process '.$this->params['command'];
break;
case self::TYPE_SOCKET:
default:
$host = $this->_params['host'];
if (!empty($this->_params['protocol'])) {
$host = $this->_params['protocol'].'://'.$host;
$host = $this->params['host'];
if (!empty($this->params['protocol'])) {
$host = $this->params['protocol'].'://'.$host;
}
$host .= ':'.$this->_params['port'];
$host .= ':'.$this->params['port'];
return $host;
break;