By default use filter_var() to valid email addresses in CakeEmail.

Refs #2477
This commit is contained in:
ADmad 2013-12-17 23:59:08 +05:30
parent b5d7628cbf
commit 49a9d24ca7
2 changed files with 25 additions and 15 deletions

View file

@ -16,7 +16,6 @@
* @license http://www.opensource.org/licenses/mit-license.php MIT License * @license http://www.opensource.org/licenses/mit-license.php MIT License
*/ */
App::uses('Validation', 'Utility');
App::uses('Multibyte', 'I18n'); App::uses('Multibyte', 'I18n');
App::uses('AbstractTransport', 'Network/Email'); App::uses('AbstractTransport', 'Network/Email');
App::uses('File', 'Utility'); App::uses('File', 'Utility');
@ -317,7 +316,7 @@ class CakeEmail {
/** /**
* Regex for email validation * Regex for email validation
* If null, it will use built in regex * If null, filter_var() will be used.
* *
* @var string * @var string
*/ */
@ -554,13 +553,10 @@ class CakeEmail {
* @param string|array $email * @param string|array $email
* @param string $name * @param string $name
* @return CakeEmail $this * @return CakeEmail $this
* @throws SocketException
*/ */
protected function _setEmail($varName, $email, $name) { protected function _setEmail($varName, $email, $name) {
if (!is_array($email)) { if (!is_array($email)) {
if (!Validation::email($email, false, $this->_emailPattern)) { $this->_validateEmail($email);
throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $email));
}
if ($name === null) { if ($name === null) {
$name = $email; $name = $email;
} }
@ -572,15 +568,30 @@ class CakeEmail {
if (is_int($key)) { if (is_int($key)) {
$key = $value; $key = $value;
} }
if (!Validation::email($key, false, $this->_emailPattern)) { $this->_validateEmail($key);
throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $key));
}
$list[$key] = $value; $list[$key] = $value;
} }
$this->{$varName} = $list; $this->{$varName} = $list;
return $this; return $this;
} }
/**
* Validate email address
*
* @param string $email
* @return void
* @throws SocketException If email address does not validate
*/
protected function _validateEmail($email) {
$valid = (($this->_emailPattern !== null &&
preg_match($this->_emailPattern, $email)) ||
filter_var($email, FILTER_VALIDATE_EMAIL)
);
if (!$valid) {
throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $email));
}
}
/** /**
* Set only 1 email * Set only 1 email
* *
@ -612,9 +623,7 @@ class CakeEmail {
*/ */
protected function _addEmail($varName, $email, $name) { protected function _addEmail($varName, $email, $name) {
if (!is_array($email)) { if (!is_array($email)) {
if (!Validation::email($email, false, $this->_emailPattern)) { $this->_validateEmail($email);
throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $email));
}
if ($name === null) { if ($name === null) {
$name = $email; $name = $email;
} }
@ -626,9 +635,7 @@ class CakeEmail {
if (is_int($key)) { if (is_int($key)) {
$key = $value; $key = $value;
} }
if (!Validation::email($key, false, $this->_emailPattern)) { $this->_validateEmail($key);
throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $key));
}
$list[$key] = $value; $list[$key] = $value;
} }
$this->{$varName} = array_merge($this->{$varName}, $list); $this->{$varName} = array_merge($this->{$varName}, $list);

View file

@ -271,6 +271,9 @@ class CakeEmailTest extends CakeTestCase {
); );
$this->assertSame($this->CakeEmail->to(), $expected); $this->assertSame($this->CakeEmail->to(), $expected);
$this->assertSame($this->CakeEmail, $result); $this->assertSame($this->CakeEmail, $result);
$this->setExpectedException('SocketException');
$this->CakeEmail->to(array('cake@localhost', 'CakePHP'));
} }
/** /**