Skip to content

Commit

Permalink
[+]: "Dependency-inject local-domain into AbstractSmtpTransport"
Browse files Browse the repository at this point in the history
-> thx@c960657 -> swiftmailer@9658bab
  • Loading branch information
voku committed May 20, 2017
1 parent c9dc5ac commit 95e8959
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 75 deletions.
76 changes: 20 additions & 56 deletions lib/classes/Swift/Transport/AbstractSmtpTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* file that was distributed with this source code.
*/

use voku\helper\UTF8;

/**
* Sends Messages over SMTP.
*
Expand Down Expand Up @@ -60,12 +62,13 @@ abstract protected function _getBufferParams();
*
* @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)
{
$this->_eventDispatcher = $dispatcher;
$this->_buffer = $buf;
$this->_lookupHostname();
$this->setLocalDomain($localDomain);
}

/**
Expand All @@ -74,23 +77,35 @@ public function __construct(Swift_Transport_IoBuffer $buf, Swift_Events_EventDis
* 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 $this
*/
public function setLocalDomain($domain)
{
$this->_domain = $domain;
if (UTF8::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;
}

/**
* 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()
Expand Down Expand Up @@ -589,58 +604,7 @@ private function _sendBcc(Swift_Mime_Message $message, $reversePath, array $bcc,
return $sent;
}

/**
* Try to determine the hostname of the server this is run on
*
* @return bool
*/
private function _lookupHostname()
{
if (
!empty($_SERVER['SERVER_NAME'])
&&
$this->_isFqdn($_SERVER['SERVER_NAME'])
) {
$this->_domain = $_SERVER['SERVER_NAME'];

return true;
}

if (!empty($_SERVER['SERVER_ADDR'])) {

// Set the address literal tag (See RFC 5321, section: 4.1.3)
if (false === strpos($_SERVER['SERVER_ADDR'], ':')) {
$prefix = ''; // IPv4 addresses are not tagged.
} else {
$prefix = 'IPv6:'; // Adding prefix in case of IPv6.
}

$this->_domain = sprintf('[%s%s]', $prefix, $_SERVER['SERVER_ADDR']);

return true;
}

return false;
}

/**
* Determine is the $hostname is a fully-qualified name
*
* @param $hostname
*
* @return bool
*/
private function _isFqdn($hostname)
{
// We could do a really thorough check, but there's really no point.
$dotPos = strpos($hostname, '.');

if (false === $dotPos) {
return false;
}

return ($dotPos > 0) && ($dotPos !== strlen($hostname) - 1);
}

/**
* Destructor.
Expand Down
3 changes: 2 additions & 1 deletion lib/classes/Swift/Transport/Esmtp/AuthHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,9 @@ public function resetState()
/**
* Returns the authenticator list for the given agent.
*
*
* @return Swift_Transport_Esmtp_Authenticator[]
*
* @throws Swift_TransportException
*/
protected function _getAuthenticatorsForAgent()
{
Expand Down
19 changes: 11 additions & 8 deletions lib/classes/Swift/Transport/EsmtpTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Swift_Transport_EsmtpTransport extends Swift_Transport_AbstractSmtpTranspo
/**
* ESMTP capabilities.
*
* @var string[]
* @var string[][]
*/
private $_capabilities = array();

Expand All @@ -52,10 +52,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)
{
parent::__construct($buf, $dispatcher);
parent::__construct($buf, $dispatcher, $localDomain);
$this->setExtensionHandlers($extensionHandlers);
}

Expand Down Expand Up @@ -245,11 +246,12 @@ public function setExtensionHandlers(array $handlers)
}

/** @noinspection PhpUsageOfSilenceOperatorInspection */
/** @noinspection UsageOfSilenceOperatorInspection */
@uasort($assoc, array($this, '_sortHandlers'));
$this->_handlers = $assoc;
$this->_setHandlerParams();
$this->_handlers = $assoc;
$this->_setHandlerParams();

return $this;
return $this;
}

/**
Expand Down Expand Up @@ -321,11 +323,12 @@ public function __call($method, $args)
0 === strpos($method, 'set')
) {
return $this;
} else {
return $return;
}

return $return;
}
}

trigger_error('Call to undefined method ' . $method, E_USER_ERROR);
}

Expand Down
5 changes: 3 additions & 2 deletions lib/classes/Swift/Transport/SendmailTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
parent::__construct($buf, $dispatcher);
parent::__construct($buf, $dispatcher, $localDomain);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions lib/dependency_maps/transport_deps.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
<?php

Swift_DependencyContainer::getInstance()
->register('transport.localdomain')
// As SERVER_NAME can come from the user in certain configurations, check that
// it does not contain forbidden characters (see RFC 952 and RFC 2181). Use
// preg_replace() instead of preg_match() to prevent DoS attacks with long host names.
->asValue(!empty($_SERVER['SERVER_NAME']) && preg_replace('/(?:^\[)?[a-zA-Z0-9-:\]_]+\.?/', '', $_SERVER['SERVER_NAME']) === '' ? trim($_SERVER['SERVER_NAME'], '[]') : '127.0.0.1')

->register('transport.smtp')
->asNewInstanceOf('Swift_Transport_EsmtpTransport')
->withDependencies(array(
'transport.buffer',
array('transport.authhandler'),
'transport.eventdispatcher',
'transport.localdomain',
))

->register('transport.sendmail')
->asNewInstanceOf('Swift_Transport_SendmailTransport')
->withDependencies(array(
'transport.buffer',
'transport.eventdispatcher',
'transport.localdomain',
))

->register('transport.mail')
Expand Down
15 changes: 15 additions & 0 deletions tests/SwiftMailerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
*/
class SwiftMailerTestCase extends \PHPUnit_Framework_TestCase
{
/**
* @param $pattern
*
* @return PHPUnit_Framework_Constraint_PCREMatch
*/
public static function regExp($pattern)
{
if (!is_string($pattern)) {
Expand All @@ -16,6 +21,11 @@ public static function regExp($pattern)
return new PHPUnit_Framework_Constraint_PCREMatch($pattern);
}

/**
* @param $expected
* @param $actual
* @param string $message
*/
public function assertIdenticalBinary($expected, $actual, $message = '')
{
$constraint = new IdenticalBinaryConstraint($expected);
Expand All @@ -27,6 +37,11 @@ protected function tearDown()
\Mockery::close();
}

/**
* @param $class
*
* @return \Mockery\MockInterface
*/
protected function getMockery($class)
{
return \Mockery::mock($class);
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/Swift/Transport/AbstractSmtpEventSupportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

require_once __DIR__.'/AbstractSmtpTest.php';

/**
* Class Swift_Transport_AbstractSmtpEventSupportTest
*/
abstract class Swift_Transport_AbstractSmtpEventSupportTest extends Swift_Transport_AbstractSmtpTest
{
public function testRegisterPluginLoadsPluginInEventDispatcher()
Expand Down Expand Up @@ -551,6 +554,11 @@ public function testExceptionBubblesCanBeCancelled()
$smtp->start();
}

/**
* @param bool $stub
*
* @return \Mockery\Mock|Swift_Events_EventDispatcher
*/
protected function _createEventDispatcher($stub = true)
{
return $this->getMockery('Swift_Events_EventDispatcher')->shouldIgnoreMissing();
Expand Down
28 changes: 25 additions & 3 deletions tests/unit/Swift/Transport/AbstractSmtpTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?php

/**
* Class Swift_Transport_AbstractSmtpTest
*/
abstract class Swift_Transport_AbstractSmtpTest extends \SwiftMailerTestCase
{
/**
* Abstract test method
*
* @param $buf
*
* @return Swift_Transport
* @return Swift_Transport_AbstractSmtpTransport
*/
abstract protected function _getTransport($buf);

Expand Down Expand Up @@ -110,7 +113,7 @@ public function testStartSendsHeloToInitiate()
->andReturn("220 some.server.tld bleh\r\n");
$buf->shouldReceive('write')
->once()
->with('~^HELO .*?\r\n$~D')
->with('~^HELO example.org\r\n$~D')
->andReturn(1);
$buf->shouldReceive('readLine')
->once()
Expand Down Expand Up @@ -138,7 +141,7 @@ public function testInvalidHeloResponseCausesException()
->andReturn("220 some.server.tld bleh\r\n");
$buf->shouldReceive('write')
->once()
->with('~^HELO .*?\r\n$~D')
->with('~^HELO example.org\r\n$~D')
->andReturn(1);
$buf->shouldReceive('readLine')
->once()
Expand Down Expand Up @@ -1181,6 +1184,22 @@ public function testSendingRegeneratesMessageId()
$smtp->send($message);
}

public function testSetLocalDomain()
{
$buf = $this->_getBuffer();
$smtp = $this->_getTransport($buf);
$smtp->setLocalDomain('example.com');
$this->assertEquals('example.com', $smtp->getLocalDomain());
$smtp->setLocalDomain('192.168.0.1');
$this->assertEquals('[192.168.0.1]', $smtp->getLocalDomain());
$smtp->setLocalDomain('[192.168.0.1]');
$this->assertEquals('[192.168.0.1]', $smtp->getLocalDomain());
$smtp->setLocalDomain('fd00::');
$this->assertEquals('[IPv6:fd00::]', $smtp->getLocalDomain());
$smtp->setLocalDomain('[IPv6:fd00::]');
$this->assertEquals('[IPv6:fd00::]', $smtp->getLocalDomain());
}

protected function _getBuffer()
{
return $this->getMockery('Swift_Transport_IoBuffer')->shouldIgnoreMissing();
Expand All @@ -1191,6 +1210,9 @@ protected function _createMessage()
return $this->getMockery('Swift_Mime_Message')->shouldIgnoreMissing();
}

/**
* @param \Mockery\Mock $buf
*/
protected function _finishBuffer($buf)
{
$buf->shouldReceive('readLine')
Expand Down
14 changes: 12 additions & 2 deletions tests/unit/Swift/Transport/EsmtpTransport/ExtensionSupportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@

require_once dirname(__DIR__).'/EsmtpTransportTest.php';

/** @noinspection PhpMultipleClassesDeclarationsInOneFile */

/**
* Interface Swift_Transport_EsmtpHandlerMixin
*/
interface Swift_Transport_EsmtpHandlerMixin extends Swift_Transport_EsmtpHandler
{
public function setUsername($user);

public function setPassword($pass);
}

/** @noinspection PhpMultipleClassesDeclarationsInOneFile */

/**
* Class Swift_Transport_EsmtpTransport_ExtensionSupportTest
*/
class Swift_Transport_EsmtpTransport_ExtensionSupportTest extends Swift_Transport_EsmtpTransportTest
{
public function testExtensionHandlersAreSortedAsNeeded()
Expand Down Expand Up @@ -141,7 +151,7 @@ public function testExtensionsCanModifyMailFromParams()
{
$buf = $this->_getBuffer();
$dispatcher = $this->_createEventDispatcher();
$smtp = new Swift_Transport_EsmtpTransport($buf, array(), $dispatcher);
$smtp = new Swift_Transport_EsmtpTransport($buf, array(), $dispatcher, 'example.org');
$ext1 = $this->getMockery('Swift_Transport_EsmtpHandler')->shouldIgnoreMissing();
$ext2 = $this->getMockery('Swift_Transport_EsmtpHandler')->shouldIgnoreMissing();
$ext3 = $this->getMockery('Swift_Transport_EsmtpHandler')->shouldIgnoreMissing();
Expand Down Expand Up @@ -243,7 +253,7 @@ public function testExtensionsCanModifyRcptParams()
{
$buf = $this->_getBuffer();
$dispatcher = $this->_createEventDispatcher();
$smtp = new Swift_Transport_EsmtpTransport($buf, array(), $dispatcher);
$smtp = new Swift_Transport_EsmtpTransport($buf, array(), $dispatcher, 'example.org');
$ext1 = $this->getMockery('Swift_Transport_EsmtpHandler')->shouldIgnoreMissing();
$ext2 = $this->getMockery('Swift_Transport_EsmtpHandler')->shouldIgnoreMissing();
$ext3 = $this->getMockery('Swift_Transport_EsmtpHandler')->shouldIgnoreMissing();
Expand Down
Loading

0 comments on commit 95e8959

Please sign in to comment.