Skip to content
This repository has been archived by the owner on Nov 17, 2021. It is now read-only.

Commit

Permalink
#403 - Use of EmailValidator for versions of PHP >= 5.3
Browse files Browse the repository at this point in the history
  • Loading branch information
egulias authored and fabpot committed Mar 23, 2015
1 parent 613354e commit 6776d29
Show file tree
Hide file tree
Showing 34 changed files with 207 additions and 397 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -15,7 +15,8 @@
}
],
"require": {
"php": ">=5.3.3"
"php": ">=5.3.3",
"egulias/email-validator": "~1.2"
},
"require-dev": {
"mockery/mockery": "~0.9.1"
Expand Down
10 changes: 6 additions & 4 deletions lib/classes/Swift/Mime/Attachment.php
Expand Up @@ -8,6 +8,8 @@
* file that was distributed with this source code.
*/

use Egulias\EmailValidator\EmailValidator;

/**
* An attachment, in a multipart message.
*
Expand All @@ -24,12 +26,12 @@ class Swift_Mime_Attachment extends Swift_Mime_SimpleMimeEntity
* @param Swift_Mime_HeaderSet $headers
* @param Swift_Mime_ContentEncoder $encoder
* @param Swift_KeyCache $cache
* @param Swift_Mime_Grammar $grammar
* @param array $mimeTypes optional
* @param EmailValidator $emailValidator
* @param array $mimeTypes
*/
public function __construct(Swift_Mime_HeaderSet $headers, Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache, Swift_Mime_Grammar $grammar, $mimeTypes = array())
public function __construct(Swift_Mime_HeaderSet $headers, Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache, EmailValidator $emailValidator, $mimeTypes = array())
{
parent::__construct($headers, $encoder, $cache, $grammar);
parent::__construct($headers, $encoder, $cache, $emailValidator);
$this->setDisposition('attachment');
$this->setContentType('application/octet-stream');
$this->_mimeTypes = $mimeTypes;
Expand Down
7 changes: 4 additions & 3 deletions lib/classes/Swift/Mime/EmbeddedFile.php
Expand Up @@ -8,6 +8,8 @@
* file that was distributed with this source code.
*/

use Egulias\EmailValidator\EmailValidator;

/**
* An embedded file, in a multipart message.
*
Expand All @@ -21,12 +23,11 @@ class Swift_Mime_EmbeddedFile extends Swift_Mime_Attachment
* @param Swift_Mime_HeaderSet $headers
* @param Swift_Mime_ContentEncoder $encoder
* @param Swift_KeyCache $cache
* @param Swift_Mime_Grammar $grammar
* @param array $mimeTypes optional
*/
public function __construct(Swift_Mime_HeaderSet $headers, Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache, Swift_Mime_Grammar $grammar, $mimeTypes = array())
public function __construct(Swift_Mime_HeaderSet $headers, Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache, EmailValidator $emailValidator, $mimeTypes = array())
{
parent::__construct($headers, $encoder, $cache, $grammar, $mimeTypes);
parent::__construct($headers, $encoder, $cache, $emailValidator, $mimeTypes);
$this->setDisposition('inline');
$this->setId($this->getId());
}
Expand Down
176 changes: 0 additions & 176 deletions lib/classes/Swift/Mime/Grammar.php

This file was deleted.

66 changes: 23 additions & 43 deletions lib/classes/Swift/Mime/Headers/AbstractHeader.php
Expand Up @@ -15,20 +15,15 @@
*/
abstract class Swift_Mime_Headers_AbstractHeader implements Swift_Mime_Header
{
const PHRASE_PATTERN = '(?:(?:(?:(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))*(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))|(?:(?:[ \t]*(?:\r\n))?[ \t])))?[a-zA-Z0-9!#\$%&\'\*\+\-\/=\?\^_`\{\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*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))*(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))|(?:(?:[ \t]*(?:\r\n))?[ \t])))?))+?)';

/**
* The name of this Header.
*
* @var string
*/
private $_name;

/**
* The Grammar used for this Header.
*
* @var Swift_Mime_Grammar
*/
private $_grammar;

/**
* The Encoder used to encode this Header.
*
Expand Down Expand Up @@ -64,16 +59,6 @@ abstract class Swift_Mime_Headers_AbstractHeader implements Swift_Mime_Header
*/
private $_cachedValue = null;

/**
* Creates a new Header.
*
* @param Swift_Mime_Grammar $grammar
*/
public function __construct(Swift_Mime_Grammar $grammar)
{
$this->setGrammar($grammar);
}

/**
* Set the character set used in this Header.
*
Expand Down Expand Up @@ -143,27 +128,6 @@ public function getEncoder()
return $this->_encoder;
}

/**
* Set the grammar used for the header.
*
* @param Swift_Mime_Grammar $grammar
*/
public function setGrammar(Swift_Mime_Grammar $grammar)
{
$this->_grammar = $grammar;
$this->setCachedValue(null);
}

/**
* Get the grammar used for this Header.
*
* @return Swift_Mime_Grammar
*/
public function getGrammar()
{
return $this->_grammar;
}

/**
* Get the name of this header (e.g. charset).
*
Expand Down Expand Up @@ -247,13 +211,12 @@ protected function createPhrase(Swift_Mime_Header $header, $string, $charset, Sw
// Treat token as exactly what was given
$phraseStr = $string;
// If it's not valid
if (!preg_match('/^'.$this->getGrammar()->getDefinition('phrase').'$/D', $phraseStr)) {

if (!preg_match('/^'.self::PHRASE_PATTERN.'$/D', $phraseStr)) {
// .. but it is just ascii text, try escaping some characters
// and make it a quoted-string
if (preg_match('/^'.$this->getGrammar()->getDefinition('text').'*$/D', $phraseStr)) {
$phraseStr = $this->getGrammar()->escapeSpecials(
$phraseStr, array('"'), $this->getGrammar()->getSpecials()
);
if (preg_match('/^[\x00-\x08\x0B\x0C\x0E-\x7F]*$/D', $phraseStr)) {
$phraseStr = $this->escapeSpecials($phraseStr, array('"'));
$phraseStr = '"'.$phraseStr.'"';
} else {
// ... otherwise it needs encoding
Expand All @@ -270,6 +233,23 @@ protected function createPhrase(Swift_Mime_Header $header, $string, $charset, Sw
return $phraseStr;
}

/**
* Escape special characters in a string (convert to quoted-pairs).
*
* @param string $token
* @param string[] $include additional chars to escape
*
* @return string
*/
private function escapeSpecials($token, $include = array())
{
foreach (array_merge(array('\\'), $include) as $char) {
$token = str_replace($char, '\\'.$char, $token);
}

return $token;
}

/**
* Encode needed word tokens within a string of input.
*
Expand Down
4 changes: 1 addition & 3 deletions lib/classes/Swift/Mime/Headers/DateHeader.php
Expand Up @@ -33,12 +33,10 @@ class Swift_Mime_Headers_DateHeader extends Swift_Mime_Headers_AbstractHeader
* </code>
*
* @param string $name of Header
* @param Swift_Mime_Grammar $grammar
*/
public function __construct($name, Swift_Mime_Grammar $grammar)
public function __construct($name)
{
$this->setFieldName($name);
parent::__construct($grammar);
}

/**
Expand Down

0 comments on commit 6776d29

Please sign in to comment.