Move email validation function to Mibew\Mail\Utils class

This commit is contained in:
Dmitriy Simushev 2015-03-17 10:36:45 +00:00
parent 4332d9c205
commit 313e854723
8 changed files with 21 additions and 12 deletions

View File

@ -96,7 +96,7 @@ class MailController extends AbstractController
$group = is_null($thread->groupId) ? null : group_by_id($thread->groupId); $group = is_null($thread->groupId) ? null : group_by_id($thread->groupId);
if (!$email) { if (!$email) {
$errors[] = no_field('Your email'); $errors[] = no_field('Your email');
} elseif (!is_valid_email($email)) { } elseif (!MailUtils::isValidAddress($email)) {
$errors[] = wrong_field('Your email'); $errors[] = wrong_field('Your email');
} }

View File

@ -20,6 +20,7 @@
namespace Mibew\Controller\Group; namespace Mibew\Controller\Group;
use Mibew\Http\Exception\NotFoundException; use Mibew\Http\Exception\NotFoundException;
use Mibew\Mail\Utils as MailUtils;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
/** /**
@ -136,7 +137,7 @@ class SettingsController extends AbstractController
$errors[] = no_field("Name"); $errors[] = no_field("Name");
} }
if ($email != '' && !is_valid_email($email)) { if ($email != '' && !MailUtils::isValidAddress($email)) {
$errors[] = wrong_field("E-mail"); $errors[] = wrong_field("E-mail");
} }

View File

@ -20,6 +20,7 @@
namespace Mibew\Controller\Operator; namespace Mibew\Controller\Operator;
use Mibew\Http\Exception\NotFoundException; use Mibew\Http\Exception\NotFoundException;
use Mibew\Mail\Utils as MailUtils;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
/** /**
@ -147,7 +148,7 @@ class ProfileController extends AbstractController
} }
} }
if (!$email || !is_valid_email($email)) { if (!$email || !MailUtils::isValidAddress($email)) {
$errors[] = wrong_field('E-mail'); $errors[] = wrong_field('E-mail');
} }

View File

@ -56,7 +56,7 @@ class PasswordRecoveryController extends AbstractController
if ($request->request->has('loginoremail')) { if ($request->request->has('loginoremail')) {
$login_or_email = $request->request->get('loginoremail'); $login_or_email = $request->request->get('loginoremail');
$to_restore = is_valid_email($login_or_email) $to_restore = MailUtils::isValidAddress($login_or_email)
? operator_by_email($login_or_email) ? operator_by_email($login_or_email)
: operator_by_login($login_or_email); : operator_by_login($login_or_email);
if (!$to_restore) { if (!$to_restore) {
@ -64,7 +64,7 @@ class PasswordRecoveryController extends AbstractController
} }
$email = $to_restore['vcemail']; $email = $to_restore['vcemail'];
if (count($page['errors']) == 0 && !is_valid_email($email)) { if (count($page['errors']) == 0 && !MailUtils::isValidAddress($email)) {
$page['errors'][] = "Operator hasn't set his e-mail"; $page['errors'][] = "Operator hasn't set his e-mail";
} }

View File

@ -20,6 +20,7 @@
namespace Mibew\Controller\Settings; namespace Mibew\Controller\Settings;
use Mibew\Http\Exception\BadRequestException; use Mibew\Http\Exception\BadRequestException;
use Mibew\Mail\Utils as MailUtils;
use Mibew\Settings; use Mibew\Settings;
use Mibew\Style\ChatStyle; use Mibew\Style\ChatStyle;
use Mibew\Style\InvitationStyle; use Mibew\Style\InvitationStyle;
@ -153,7 +154,7 @@ class CommonController extends AbstractController
$params['left_messages_locale'] = get_home_locale(); $params['left_messages_locale'] = get_home_locale();
} }
if ($params['email'] && !is_valid_email($params['email'])) { if ($params['email'] && !MailUtils::isValidAddress($params['email'])) {
$errors[] = getlocal('Enter a valid email address'); $errors[] = getlocal('Enter a valid email address');
} }

View File

@ -26,6 +26,17 @@ use Symfony\Component\Yaml\Parser as YamlParser;
*/ */
class Utils class Utils
{ {
/**
* Checks if the passed in e-mail address is valid.
*
* @param string $address E-mail address to check.
* @return boolean
*/
public static function isValidAddress($address)
{
return (bool)filter_var($address, FILTER_VALIDATE_EMAIL);
}
/** /**
* Builds an instance of \Swift_message. * Builds an instance of \Swift_message.
* *

View File

@ -744,7 +744,7 @@ class ThreadProcessor extends ClientSideProcessor implements
$info = $args['info']; $info = $args['info'];
$referrer = $args['referrer']; $referrer = $args['referrer'];
if (!is_valid_email($email)) { if (!MailUtils::isValidAddress($email)) {
throw new ThreadProcessorException( throw new ThreadProcessorException(
wrong_field("Your email"), wrong_field("Your email"),
ThreadProcessorException::ERROR_WRONG_EMAIL ThreadProcessorException::ERROR_WRONG_EMAIL

View File

@ -37,8 +37,3 @@ function verify_param($name, $reg_exp, $default = null)
echo "<html><head></head><body>Wrong parameter used or absent: " . $name . "</body></html>"; echo "<html><head></head><body>Wrong parameter used or absent: " . $name . "</body></html>";
exit; exit;
} }
function is_valid_email($email)
{
return (bool)filter_var($email, FILTER_VALIDATE_EMAIL);
}