Development

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

You must first sign up to be able to contribute.

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

Revision 10270, 4.0 kB (checked in by Carl.Vondrick, 6 years ago)

1.1: added deprecated warnings to all sfCompatPlugin classes (closes #3882)

  • 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  * WARNING: This class is deprecated and will be removed in symfony 1.2.
17  *
18  * @package    symfony
19  * @subpackage validator
20  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
21  * @author     Sean Kerr <sean@code-box.org>
22  * @version    SVN: $Id$
23  * @deprecated Deprecated since symfony 1.1
24  */
25 class sfEmailValidator extends sfValidator
26 {
27   /**
28    * Executes this validator.
29    *
30    * @param mixed A file or parameter value/array
31    * @param error An error message reference
32    *
33    * @return bool true, if this validator executes successfully, otherwise false
34    */
35   public function execute(&$value, &$error)
36   {
37     $strict = $this->getParameterHolder()->get('strict');
38     if ($strict == true)
39     {
40       $re = '/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i';
41     }
42     else
43     {
44       /* Cal Henderson: http://iamcal.com/publish/articles/php/parsing_email/pdf/
45        * The long regular expression below is made by the following code
46        * fragment:
47        *
48        *   $qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
49        *   $dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
50        *   $atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'
51        *         . '\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
52        *   $quoted_pair = '\\x5c\\x00-\\x7f';
53        *   $domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d";
54        *   $quoted_string = "\\x22($qtext|$quoted_pair)*\\x22";
55        *   $domain_ref = $atom;
56        *   $sub_domain = "($domain_ref|$domain_literal)";
57        *   $word = "($atom|$quoted_string)";
58        *   $domain = "$sub_domain(\\x2e$sub_domain)*";
59        *   $local_part = "$word(\\x2e$word)*";
60        *   $addr_spec = "$local_part\\x40$domain";
61        */
62
63       $re = '/^([^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-'
64            .'\\x5d\\x7f-\\xff]+|\\x22([^\\x0d\\x22\\x5c\\x80-\\xff]|\\x5c\\x00-'
65            .'\\x7f)*\\x22)(\\x2e([^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-'
66            .'\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+|\\x22([^\\x0d\\x22\\x5c\\x80'
67            .'-\\xff]|\\x5c\\x00-\\x7f)*\\x22))*\\x40([^\\x00-\\x20\\x22\\x28\\x29'
68            .'\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+|\\x5b([^'
69            .'\\x0d\\x5b-\\x5d\\x80-\\xff]|\\x5c\\x00-\\x7f)*\\x5d)(\\x2e([^\\x00-'
70            .'\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-'
71            .'\\xff]+|\\x5b([^\\x0d\\x5b-\\x5d\\x80-\\xff]|\\x5c\\x00-\\x7f)*'
72            .'\\x5d))*$/'
73       ;
74     }
75
76     if (!preg_match($re, $value))
77     {
78       $error = $this->getParameterHolder()->get('email_error');
79       return false;
80     }
81
82     $checkDomain = $this->getParameterHolder()->get('check_domain');
83     if ($checkDomain && function_exists('checkdnsrr'))
84     {
85       $tokens = explode('@', $value);
86       if (!checkdnsrr($tokens[1], 'MX') && !checkdnsrr($tokens[1], 'A'))
87       {
88         $error = $this->getParameterHolder()->get('email_error');
89
90         return false;
91       }
92     }
93
94     return true;
95   }
96
97   /**
98    * Initializes this validator.
99    *
100    * @param sfContext The current application context
101    * @param array   An associative array of initialization parameters
102    *
103    * @return bool true, if initialization completes successfully, otherwise false
104    */
105   public function initialize($context, $parameters = null)
106   {
107     // initialize parent
108     parent::initialize($context);
109
110     // set defaults
111     $this->getParameterHolder()->set('strict',       true);
112     $this->getParameterHolder()->set('check_domain', false);
113     $this->getParameterHolder()->set('email_error''Invalid input');
114
115     $this->getParameterHolder()->add($parameters);
116
117     return true;
118   }
119 }
120
Note: See TracBrowser for help on using the browser.