| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
class strongEmailValidator extends sfValidator |
|---|
| 23 |
{ |
|---|
| 24 |
|
|---|
| 25 |
* Executes this validator. |
|---|
| 26 |
* |
|---|
| 27 |
* @param mixed A file or parameter value/array |
|---|
| 28 |
* @param error An error message reference |
|---|
| 29 |
* |
|---|
| 30 |
* @return bool true, if this validator executes successfully, otherwise false |
|---|
| 31 |
*/ |
|---|
| 32 |
public function execute(&$value, &$error) |
|---|
| 33 |
{ |
|---|
| 34 |
$strict = $this->getParameterHolder()->get('strict'); |
|---|
| 35 |
if ($strict == true) |
|---|
| 36 |
{ |
|---|
| 37 |
$re = '/^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/'; |
|---|
| 38 |
} |
|---|
| 39 |
else |
|---|
| 40 |
{ |
|---|
| 41 |
|
|---|
| 42 |
* The long regular expression below is made by the following code |
|---|
| 43 |
* fragment: |
|---|
| 44 |
* |
|---|
| 45 |
* $qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'; |
|---|
| 46 |
* $dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]'; |
|---|
| 47 |
* $atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c' |
|---|
| 48 |
* . '\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+'; |
|---|
| 49 |
* $quoted_pair = '\\x5c\\x00-\\x7f'; |
|---|
| 50 |
* $domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d"; |
|---|
| 51 |
* $quoted_string = "\\x22($qtext|$quoted_pair)*\\x22"; |
|---|
| 52 |
* $domain_ref = $atom; |
|---|
| 53 |
* $sub_domain = "($domain_ref|$domain_literal)"; |
|---|
| 54 |
* $word = "($atom|$quoted_string)"; |
|---|
| 55 |
* $domain = "$sub_domain(\\x2e$sub_domain)*"; |
|---|
| 56 |
* $local_part = "$word(\\x2e$word)*"; |
|---|
| 57 |
* $addr_spec = "$local_part\\x40$domain"; |
|---|
| 58 |
*/ |
|---|
| 59 |
|
|---|
| 60 |
$re = '/^([^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-' |
|---|
| 61 |
.'\\x5d\\x7f-\\xff]+|\\x22([^\\x0d\\x22\\x5c\\x80-\\xff]|\\x5c\\x00-' |
|---|
| 62 |
.'\\x7f)*\\x22)(\\x2e([^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-' |
|---|
| 63 |
.'\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+|\\x22([^\\x0d\\x22\\x5c\\x80' |
|---|
| 64 |
.'-\\xff]|\\x5c\\x00-\\x7f)*\\x22))*\\x40([^\\x00-\\x20\\x22\\x28\\x29' |
|---|
| 65 |
.'\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+|\\x5b([^' |
|---|
| 66 |
.'\\x0d\\x5b-\\x5d\\x80-\\xff]|\\x5c\\x00-\\x7f)*\\x5d)(\\x2e([^\\x00-' |
|---|
| 67 |
.'\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-' |
|---|
| 68 |
.'\\xff]+|\\x5b([^\\x0d\\x5b-\\x5d\\x80-\\xff]|\\x5c\\x00-\\x7f)*' |
|---|
| 69 |
.'\\x5d))*$/' |
|---|
| 70 |
; |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
if (!preg_match($re, $value)) |
|---|
| 74 |
{ |
|---|
| 75 |
$error = $this->getParameterHolder()->get('email_error'); |
|---|
| 76 |
return false; |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
$checkDomain = $this->getParameterHolder()->get('check_domain'); |
|---|
| 80 |
if ($checkDomain && function_exists('checkdnsrr')) |
|---|
| 81 |
{ |
|---|
| 82 |
$tokens = explode('@', $value); |
|---|
| 83 |
if (!checkdnsrr($tokens[1], 'MX') && !checkdnsrr($tokens[1], 'A')) |
|---|
| 84 |
{ |
|---|
| 85 |
$error = $this->getParameterHolder()->get('email_error'); |
|---|
| 86 |
|
|---|
| 87 |
return false; |
|---|
| 88 |
} |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
return true; |
|---|
| 92 |
} |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
* Initializes this validator. |
|---|
| 96 |
* |
|---|
| 97 |
* @param sfContext The current application context |
|---|
| 98 |
* @param array An associative array of initialization parameters |
|---|
| 99 |
* |
|---|
| 100 |
* @return bool true, if initialization completes successfully, otherwise false |
|---|
| 101 |
*/ |
|---|
| 102 |
public function initialize($context, $parameters = null) |
|---|
| 103 |
{ |
|---|
| 104 |
|
|---|
| 105 |
parent::initialize($context); |
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
$this->getParameterHolder()->set('strict', true); |
|---|
| 109 |
$this->getParameterHolder()->set('check_domain', false); |
|---|
| 110 |
$this->getParameterHolder()->set('email_error', 'Invalid input'); |
|---|
| 111 |
$this->getParameterHolder()->set('domain_error', 'Invalid email address domain'); |
|---|
| 112 |
$this->getParameterHolder()->add($parameters); |
|---|
| 113 |
|
|---|
| 114 |
return true; |
|---|
| 115 |
} |
|---|
| 116 |
} |
|---|
| 117 |
|
|---|