Development

/branches/1.2/lib/plugins/sfCompat10Plugin/lib/validator/sfEmailValidator.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/plugins/sfCompat10Plugin/lib/validator/sfEmailValidator.class.php

Revision 7902, 3.8 kB (checked in by fabien, 7 years ago)

rename sfValidatorBase to sfValidator in sfCompat10Plugin and renamed the new sfValidator class to sfValidatorBase (closes #3103)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Date
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  * (c) 2004-2006 Sean Kerr <sean@code-box.org>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 /**
13  * sfEmailValidator verifies a parameter contains a value that qualifies as an
14  * email address.
15  *
16  * @package    symfony
17  * @subpackage validator
18  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
19  * @author     Sean Kerr <sean@code-box.org>
20  * @version    SVN: $Id$
21  */
22 class sfEmailValidator 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 = '/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i';
38     }
39     else
40     {
41       /* Cal Henderson: http://iamcal.com/publish/articles/php/parsing_email/pdf/
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     // initialize parent
105     parent::initialize($context);
106
107     // set defaults
108     $this->getParameterHolder()->set('strict',       true);
109     $this->getParameterHolder()->set('check_domain', false);
110     $this->getParameterHolder()->set('email_error''Invalid input');
111
112     $this->getParameterHolder()->add($parameters);
113
114     return true;
115   }
116 }
117
Note: See TracBrowser for help on using the browser.