Skip to content

Commit

Permalink
Merge branch 'surelygroup-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas committed Feb 2, 2018
2 parents c9f30ce + 92dd252 commit afe0ece
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 88 deletions.
17 changes: 5 additions & 12 deletions .travis.yml
Expand Up @@ -6,22 +6,16 @@ cache:
- vendor
- $HOME/.composer/cache
php:
- 5.3
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2

matrix:
include:
- php: 5.3
- php: 7.1
env: COMPOSER_FLAGS='--prefer-lowest --prefer-stable'

before_script:
- pecl install xmldiff
- echo "xdebug.max_nesting_level=1000" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
- if [[ $TRAVIS_PHP_VERSION = '5.6' ]]; then PHPUNIT_FLAGS="--coverage-clover=coverage.clover"; else PHPUNIT_FLAGS=""; fi
- if [[ $TRAVIS_PHP_VERSION != '5.6' ]]; then phpenv config-rm xdebug.ini; fi
- if [[ $TRAVIS_PHP_VERSION = '7.1' ]] ; then PHPUNIT_FLAGS="--coverage-clover=coverage.clover"; else phpenv config-rm xdebug.ini; fi
- composer self-update
- composer update $COMPOSER_FLAGS

Expand All @@ -30,5 +24,4 @@ script:

after_script:
- wget https://scrutinizer-ci.com/ocular.phar
- if [[ $TRAVIS_PHP_VERSION = '5.6' ]] ; then php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi

- if [[ $TRAVIS_PHP_VERSION = '7.1' ]] ; then php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -11,8 +11,8 @@
}
],
"require": {
"php": "^5.3.2|^7.0",
"swiftmailer/swiftmailer": "^4.2|^5.0"
"php": "^7.1",
"swiftmailer/swiftmailer": "^6.0"
},
"require-dev": {
"phpunit/phpunit": "^4.0|^5.0"
Expand Down
Expand Up @@ -6,13 +6,13 @@ class EndOfPartReachedException extends \Exception
{
protected $data = array();

public function __construct($data)
public function __construct(string $data)
{
$this->data = $data;
parent::__construct();
}

public function getData()
public function getData(): string
{
return $this->data;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Goetas/Mail/ToSwiftMailParser/Mime/ContentDecoder.php
Expand Up @@ -4,7 +4,7 @@

class ContentDecoder
{
public function decode($string, $from)
public function decode(string $string, string $from): string
{
if ($from == "base64") {
return base64_decode($string);
Expand Down
13 changes: 9 additions & 4 deletions lib/Goetas/Mail/ToSwiftMailParser/Mime/HeaderDecoder.php
Expand Up @@ -4,9 +4,14 @@

class HeaderDecoder
{
public static $decodeWindows1252 = false;
private $decodeWindows1252 = false;

public function decode($string)
public function __construct($decodeWindows1252 = false)
{
$this->decodeWindows1252 = $decodeWindows1252;
}

public function decode(string $string): string
{
/* Take out any spaces between multiple encoded words. */
$string = preg_replace('|\?=\s+=\?|', '?==?', $string);
Expand All @@ -24,7 +29,7 @@ public function decode($string)
}

$orig_charset = substr($string, $pos + 2, $d1 - $pos - 2);
if (self::$decodeWindows1252 && mb_strtolower($orig_charset) == 'iso-8859-1') {
if ($this->decodeWindows1252 && mb_strtolower($orig_charset) == 'iso-8859-1') {
$orig_charset = 'windows-1252';
}

Expand Down Expand Up @@ -66,7 +71,7 @@ public function decode($string)
return $out . substr($string, $old_pos);
}

public static function convertCharset($str, $orig, $to)
private static function convertCharset(string $str, string $orig, string $to)
{
//@todo convert charset
return $str;
Expand Down
143 changes: 81 additions & 62 deletions lib/Goetas/Mail/ToSwiftMailParser/MimeParser.php
Expand Up @@ -7,25 +7,67 @@

class MimeParser
{
private const SWIFT_CONTAINER_CACHE_KEY = 'cache';
private const SWIFT_CONTAINER_ID_GENERATOR_KEY = 'mime.idgenerator';

protected $removeHeaders = array("Received", "From", "X-Original-To", "MIME-Version", "Received-SPF", "Delivered-To");
protected $allowedHeaders = array("return-path", "subject");
private $cache;
private $grammar;
/**
* @var ContentDecoder
*/
private $contentDecoder;

/**
* @var HeaderDecoder
*/
private $headerDecoder;

/**
* @var \Swift_DependencyContainer
*/
private $swiftContainer;

public function __construct(array $allowedHeaders = array(), array $removeHeaders = array())
{
$this->cache = \Swift_DependencyContainer::getInstance()->lookup('cache');
$this->grammar = \Swift_DependencyContainer::getInstance()->lookup('mime.grammar');
$this->contentDecoder = new ContentDecoder ();
$this->headerDecoder = new HeaderDecoder ();

$this->allowedHeaders = array_merge($this->allowedHeaders, $allowedHeaders);
$this->removeHeaders = array_merge($this->removeHeaders, $removeHeaders);
}

public function parseString($string, $fillHeaders = false, \Swift_Mime_MimeEntity $message = null)
public function setSwiftDependencyContainer(\Swift_DependencyContainer $swiftContainer)
{
$this->swiftContainer = $swiftContainer;
}

private function getSwiftDependencyContainer(): \Swift_DependencyContainer
{
if ($this->swiftContainer === null) {
$this->swiftContainer = \Swift_DependencyContainer::getInstance();
}
return $this->swiftContainer;
}

private function getIdGenertor(): \Swift_IdGenerator
{
return $this->getSwiftDependencyContainer()->lookup(self::SWIFT_CONTAINER_ID_GENERATOR_KEY);
}

private function getCache(): \Swift_KeyCache
{
return $this->getSwiftDependencyContainer()->lookup(self::SWIFT_CONTAINER_CACHE_KEY);
}

public function parseFile(string $path, bool $fillHeaders = false, \Swift_Mime_SimpleMimeEntity $message = null): \Swift_Mime_SimpleMimeEntity
{
$fp = fopen($path, "rb");
$message = $this->parseStream($fp, $fillHeaders, $message);
fclose($fp);
return $message;
}

public function parseString(string $string, bool $fillHeaders = false, \Swift_Mime_SimpleMimeEntity $message = null): \Swift_Mime_SimpleMimeEntity
{
$fp = fopen("php://memory", "wb");
fwrite($fp, $string);
Expand All @@ -36,13 +78,9 @@ public function parseString($string, $fillHeaders = false, \Swift_Mime_MimeEntit
}

/**
*
* @param stream $stream
* @param boolean $fillHeaders (default to false)
* @param \Swift_Mime_MimeEntity $message (default to null)
* @return Swift_Mime_MimeEntity|\Swift_Message
*/
public function parseStream($stream, $fillHeaders = false, \Swift_Mime_MimeEntity $message = null)
public function parseStream($stream, bool $fillHeaders = false, \Swift_Mime_SimpleMimeEntity $message = null): \Swift_Mime_SimpleMimeEntity
{
$partHeaders = $this->extractHeaders($stream);

Expand All @@ -66,7 +104,7 @@ public function parseStream($stream, $fillHeaders = false, \Swift_Mime_MimeEntit
return $message;
}

protected function extractHeaders($stream)
protected function extractHeaders($stream): array
{
$headers = array();
$hName = null;
Expand All @@ -93,7 +131,7 @@ protected function extractHeaders($stream)
return $headers;
}

private function filterHeaders(array $headers)
private function filterHeaders(array $headers): array
{
foreach ($headers as $header => $values) {
if (in_array(strtolower($header), $this->removeHeaders) && !in_array(strtolower($header), $this->allowedHeaders)) {
Expand All @@ -103,7 +141,7 @@ private function filterHeaders(array $headers)
return $headers;
}

protected function parseParts($stream, $partHeaders)
protected function parseParts($stream, array $partHeaders): array
{
$parts = array();
$contentType = $this->extractValueHeader($this->getContentType($partHeaders));
Expand Down Expand Up @@ -161,7 +199,7 @@ protected function parseParts($stream, $partHeaders)
return $parts;
}

private function extractValueHeader($header)
private function extractValueHeader($header): string
{
$pos = stripos($header, ';');
if ($pos !== false) {
Expand All @@ -171,7 +209,7 @@ private function extractValueHeader($header)
}
}

private function getContentType(array $partHeaders)
private function getContentType(array $partHeaders): string
{
if (array_key_exists('content-type', $partHeaders)) {
return $partHeaders['content-type'];
Expand All @@ -180,7 +218,7 @@ private function getContentType(array $partHeaders)
return '';
}

private function extractHeaderParts($header)
private function extractHeaderParts(string $header): array
{
if (stripos($header, ';') !== false) {

Expand All @@ -199,26 +237,31 @@ private function extractHeaderParts($header)
return array();
}
}
protected function extractPart($stream, $boundary, $encoding)

/**
* @throws Exception\EndOfMultiPartReachedException
* @throws Exception\EndOfPartReachedException
*/
protected function extractPart($stream, ?string $boundary, string $encoding): void
{
$rows = array();
while (!feof($stream)) {
$row = fgets($stream);

if ($boundary !== null) {
if (strpos($row, "--$boundary--") === 0) {
throw new Exception\EndOfMultiPartReachedException ($this->contentDecoder->decode(implode("", $rows), $encoding));
throw new Exception\EndOfMultiPartReachedException($this->contentDecoder->decode(implode("", $rows), $encoding));
}
if (strpos($row, "--$boundary") === 0) {
throw new Exception\EndOfPartReachedException ($this->contentDecoder->decode(implode("", $rows), $encoding));
throw new Exception\EndOfPartReachedException($this->contentDecoder->decode(implode("", $rows), $encoding));
}
}
$rows [] = $row;
}
throw new Exception\EndOfMultiPartReachedException ($this->contentDecoder->decode(implode("", $rows), $encoding));
throw new Exception\EndOfMultiPartReachedException($this->contentDecoder->decode(implode("", $rows), $encoding));
}

private function getTransferEncoding(array $partHeaders)
private function getTransferEncoding(array $partHeaders): string
{
if (array_key_exists('content-transfer-encoding', $partHeaders)) {
return $partHeaders ['content-transfer-encoding'];
Expand All @@ -227,12 +270,7 @@ private function getTransferEncoding(array $partHeaders)
return '';
}

/**
*
* @param array $headersRaw
* @return \Swift_Mime_HeaderSet
*/
protected function createHeadersSet(array $headersRaw)
protected function createHeadersSet(array $headersRaw): \Swift_Mime_SimpleHeaderSet
{
$headers = \Swift_DependencyContainer::getInstance()->lookup('mime.headerset');

Expand All @@ -251,7 +289,7 @@ protected function createHeadersSet(array $headersRaw)
}
break;
case "date":
$headers->addDateHeader($name, strtotime($value));
$headers->addDateHeader($name, new \DateTime($value));
break;
case "to":
case "from":
Expand Down Expand Up @@ -282,53 +320,48 @@ protected function createHeadersSet(array $headersRaw)
return $headers;
}

protected function createMessage(array $message, \Swift_Mime_MimeEntity $entity)
protected function createMessage(array $message, \Swift_Mime_SimpleMimeEntity $entity): void
{
if (stripos($message ["type"], 'multipart/') !== false) {

if (strpos($message ["type"], '/alternative')) {
$nestingLevel = \Swift_Mime_MimeEntity::LEVEL_ALTERNATIVE;
$nestingLevel = \Swift_Mime_SimpleMimeEntity::LEVEL_ALTERNATIVE;
} elseif (strpos($message ["type"], '/related')) {
$nestingLevel = \Swift_Mime_MimeEntity::LEVEL_RELATED;
$nestingLevel = \Swift_Mime_SimpleMimeEntity::LEVEL_RELATED;
} elseif (strpos($message ["type"], '/mixed')) {
$nestingLevel = \Swift_Mime_MimeEntity::LEVEL_MIXED;
$nestingLevel = \Swift_Mime_SimpleMimeEntity::LEVEL_MIXED;
}

$childrens = array();
$childs = array();
foreach ($message ["parts"] as $part) {

$headers = $this->createHeadersSet($part ["headers"]);
$encoder = $this->getEncoder($this->getTransferEncoding($part ["headers"]));
$headers = $this->createHeadersSet($part["headers"]);
$encoder = $this->getEncoder($this->getTransferEncoding($part["headers"]));

if (stripos($part ["type"], 'multipart/') !== false) {
$newEntity = new \Swift_Mime_MimePart ($headers, $encoder, $this->cache, $this->grammar);
if (stripos($part["type"], 'multipart/') !== false) {
$newEntity = new \Swift_Mime_MimePart ($headers, $encoder, $this->getCache(), $this->getIdGenertor());
} else {
$newEntity = new \Swift_Mime_SimpleMimeEntity ($headers, $encoder, $this->cache, $this->grammar);
$newEntity = new \Swift_Mime_SimpleMimeEntity ($headers, $encoder, $this->getCache(), $this->getIdGenertor());
}

$this->createMessage($part, $newEntity);

$ref = new \ReflectionObject ($newEntity);
$m = $ref->getMethod('_setNestingLevel');
$m = $ref->getMethod('setNestingLevel');
$m->setAccessible(true);
$m->invoke($newEntity, $nestingLevel);

$childrens [] = $newEntity;
$childs [] = $newEntity;
}

$entity->setContentType($part ["type"]);
$entity->setChildren($childrens);
$entity->setContentType($part["type"]);
$entity->setChildren($childs);
} else {
$entity->setBody($message ["body"], $message ["type"]);
}
}

/**
*
* @param string $type
* @return \Swift_Mime_ContentEncoder
*/
protected function getEncoder($type)
protected function getEncoder(string $type): \Swift_Mime_ContentEncoder
{
switch ($type) {
case "base64" :
Expand All @@ -345,18 +378,4 @@ protected function getEncoder($type)
break;
}
}

/**
*
* @param string $path
* The file containg a MIME message
* @return \Swift_Message
*/
public function parseFile($path, $fillHeaders = false, \Swift_Mime_MimeEntity $message = null)
{
$fp = fopen($path, "rb");
$message = $this->parseStream($fp, $fillHeaders, $message);
fclose($fp);
return $message;
}
}

0 comments on commit afe0ece

Please sign in to comment.