Added a protection to not insert invalid emails.

This commit is contained in:
Juan Basso 2011-03-01 19:46:20 -03:00
parent 6a76931961
commit 03b4ce070b
2 changed files with 46 additions and 0 deletions

View file

@ -367,9 +367,13 @@ class CakeEmail {
* @param mixed $email
* @param mixed $name
* @return void
* @thrown SocketException
*/
protected function _setEmail($varName, $email, $name) {
if (!is_array($email)) {
if (!Validation::email($email)) {
throw new SocketException(__('Invalid email: "%s"', $email));
}
if ($name === null) {
$this->{$varName} = array($email => $email);
} else {
@ -380,8 +384,14 @@ class CakeEmail {
$list = array();
foreach ($email as $key => $value) {
if (is_int($key)) {
if (!Validation::email($value)) {
throw new SocketException(__('Invalid email: "%s"', $value));
}
$list[$value] = $value;
} else {
if (!Validation::email($key)) {
throw new SocketException(__('Invalid email: "%s"', $key));
}
$list[$key] = $value;
}
}
@ -417,6 +427,9 @@ class CakeEmail {
*/
protected function _addEmail($varName, $email, $name) {
if (!is_array($email)) {
if (!Validation::email($email)) {
throw new SocketException(__('Invalid email: "%s"', $email));
}
if ($name === null) {
$this->{$varName}[$email] = $email;
} else {
@ -427,8 +440,14 @@ class CakeEmail {
$list = array();
foreach ($email as $key => $value) {
if (is_int($key)) {
if (!Validation::email($value)) {
throw new SocketException(__('Invalid email: "%s"', $value));
}
$list[$value] = $value;
} else {
if (!Validation::email($key)) {
throw new SocketException(__('Invalid email: "%s"', $key));
}
$list[$key] = $value;
}
}

View file

@ -118,6 +118,33 @@ class CakeEmailTest extends CakeTestCase {
$this->assertIdentical($this->CakeEmail->getTo(), $expected);
}
/**
* Data provider function for testBuildInvalidData
*
* @return array
*/
public static function invalidEmails() {
return array(
array(1.0),
array(''),
array('string'),
array('<tag>'),
array('some@one.whereis'),
array(array('ok@cakephp.org', 1.0, '', 'string'))
);
}
/**
* testBuildInvalidData
*
* @dataProvider invalidEmails
* @expectedException SocketException
* return void
*/
public function testInvalidEmail($value) {
$this->CakeEmail->setTo($value);
}
/**
* testFormatAddress method
*