root/trunk/patForms/Rule/Email.php

Revision 251, 6.0 kB (checked in by argh, 3 years ago)

Added french translations

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 /**
3  * patForms Rule Email
4  *
5  * $Id$
6  *
7  * @package        patForms
8  * @subpackage    Rules
9  */
10
11 /**
12  * patForms Rule Email
13  *
14  * Rule to check an eMail address. This can be done in three checks:
15  * - syntax
16  * - MX
17  * - user
18  *
19  * The MX and user check require PEAR Net_DNS. To check the
20  * user you will also need to install Net_SMTP.
21  *
22  * @package        patForms
23  * @subpackage    Rules
24  * @author        Stephan Schmidt <schst@php-tools.net>
25  * @license        LGPL, see license.txt for details
26  * @link        http://www.php-tools.net
27  */
28 class patForms_Rule_Email extends patForms_Rule
29 {
30    /**
31     * name of the rule
32     *
33     * @abstract
34     * @access    private
35     */
36     var    $ruleName = 'Email';
37
38    /**
39     * define error codes and messages for the rule
40     *
41     * @access    private
42     * @var        array    $validatorErrorCodes
43     * @todo     translate error messages
44     */
45     var    $validatorErrorCodes  =   array(
46         "C"   =>   array(
47              1   =>   "E-Mail address can only be 132 characters long.",
48              2   =>   "The E-Mail address may contain no blank spaces.",
49              3   =>   "The E-Mail address must contain only English caracters.",
50              4   =>   'The E-Mail address must contain exactly one @ symbol.',
51              5   =>   "The E-Mail address contains invalid characters.",
52              6   =>   "The E-Mail address cannot contain two successive periods.",
53              7   =>   "The input field is in an invalid format.  Please you change to a format corresponding to the pattern [name@domainname.com].",
54              8   =>   "E-Mail domain could not be found.",
55              9   =>   "The domain specified does not recognise the given email address."
56         ),
57         "de" =>    array(
58             1    =>    "Eine E-Mail Adresse darf maximal 132 Zeichen lang sein.",
59             2    =>    "Die E-Mail Adresse darf keine Leerzeichen enthalten.",
60             3    =>    "Die E-Mail Adresse darf keine Umlaute oder 'ß' enthalten.",
61             4    =>    'Eine E-Mail-Adresse muss genau ein \'@\' enthalten.',
62             5    =>    "Die E-Mail Adresse enthält ungültige Zeichen.",
63             6    =>    "Die E-Mail Adresse darf keine zwei aufeinanderfolgenden Punkte enthalten.",
64             7    =>    "Das Eingabefeld enthält kein gültiges Format. Bitte ändern Sie Ihre Eingabe entsprechend dem Muster [name@domainname.de].",       
65             8    =>    "Die Domain der E-Mail Adresse konnte nicht gefunden werden.",
66             9    =>    "Der Mailserver verweigert die Annahme von E-Mails für die E-Mail-Adresse."
67         ),
68         "fr"=>    array(
69             1    =>    "Une adresse Email ne peut contenir que 132 caractères au maximum.",
70             2    =>    "Une adresse Email ne doit pas contenir d'espaces.",
71             3    =>    "Une adresse Email ne doit pas contenir d'accents ou d'autres caractères spéciaux comme le ç.",
72             4    =>    'Une adresse Email doit contenir exactement un \'@\'.',
73             5    =>    "L'adresse Email contient des caractères invalides.",
74             6    =>    "Une adresse Email ne doit pas contenir deux ou plus de points (.) consécutifs.",
75             7    =>    "Format invalide. Veuillez entrer une adresse Email dans un format valide ([nom@hôte.fr] par exemple).",           
76             8    =>    "Le serveur hôte de l'adresse Email n'a pas pu être contacté.",
77             9    =>    "Le serveur hôte de l'adresse Email refuse les messages pour cette adresse."
78         ),
79     );
80
81    /**
82     * flag to indicate whether the MX server should be checked
83     *
84     * @var        boolean
85     * @access    private
86     */
87     var $_checkMx = false;
88
89    /**
90     * flag to indicate whether the username should be checked
91     *
92     * @var        boolean
93     * @access    private
94     */
95     var $_checkUser = false;
96
97    /**
98     * enable the MX check
99     *
100     * @access    public
101     * @param    boolean
102     */
103     function enableMxCheck($flag = true)
104     {
105         $this->_checkMx = $flag;
106     }
107     
108    /**
109     * enable the User check
110     *
111     * @access    public
112     * @param    boolean
113     */
114     function enableUserCheck($flag = true)
115     {
116         $this->_checkUser = $flag;
117     }
118     
119    /**
120     * method called by patForms or any patForms_Element to validate the
121     * element or the form.
122     *
123     * @access    public
124     * @param    object patForms    form object
125     */
126     function applyRule( &$element, $type = PATFORMS_RULE_BEFORE_VALIDATION )
127     {
128         $value = $element->getValue();
129         if (empty($value)) {
130             return true;
131         }
132         
133         if( strlen( $value ) > 132 ) {
134             $this->addValidationError(1);
135             return false;
136         }
137         
138         //    check for spaces
139         if( eregi( "[[:space:]]", $value ) ) {
140             $this->addValidationError(2);
141         }
142
143         //    check for German umlaut
144         if( eregi( "[üöäß]", $value ) ) {
145             $this->addValidationError(3);
146             return false;
147         }
148
149         //    check for more than one '@'
150         if( substr_count( $value, '@' ) != 1 ) {
151             $this->addValidationError(4);
152             return false;
153         }
154
155         //    check for valid chars in email
156         $validChars    =    preg_quote( "abcdefghijklmnopqrstuvwxyz1234567890@.+_-" );
157         if( !preg_match( "/^[".$validChars."]+$/i", $value ) ) {
158             $this->addValidationError(5);
159             return false;
160         }
161         
162         if (strstr( $value, '..' )) {
163             $this->addValidationError(6);
164             return false;
165         }
166
167         //    check format
168         if( !eregi( "^.*[^._-]@[^-.].+\..{2,}$", $value ) ) {
169             $this->addValidationError(7);
170             return false;
171         }
172
173         //    check for existing mailserver
174         if ($this->_checkMx || $this->_checkUser) {
175             require_once 'PEAR.php';
176             require_once 'Net/DNS.php';
177             require_once 'Net/DNS/Resolver.php';
178             
179             $resolver = new    Net_DNS_Resolver();
180             $domain   = substr( strchr( $value, '@' ), 1 );
181             $mxResult = $resolver->send( $domain, "MX", "IN" );
182             
183             if( PEAR::isError( $mxResult ) || empty( $mxResult->answer ) ) {
184                 $this->addValidationError(8);
185                 return false;
186             }
187
188             //    ask mailserver, whether user exists
189             if ($this->_checkUser) {
190                 require_once 'Net/SMTP.php';
191                 $found     = false;
192                 $mxServers = $mxResult->answer;
193                 $cnt       = count( $mxServers );
194                 for ($i = 0; $i < $cnt; $i++) {
195                     
196                     if (isset($mxServers[$i]->exchange)) {
197                         $smtp    =    new Net_SMTP( $mxServers[$i]->exchange );
198                         $smtp->connect();
199                         $result    =    $smtp->mailFrom( 'test@w3c.org' );
200                         if (PEAR::isError($result)) {
201                             continue;
202                         }
203                         $result    = $smtp->rcptTo( $value );
204                         if (PEAR::isError( $result )) {
205                             continue;
206                         }
207                         $found    =    true;
208                         break;
209                     }
210                 }
211
212                 if ($found === false) {
213                     $this->addValidationError(9);
214                     return false;
215                 }
216             }
217         }
218         return true;
219     }
220 }
221 ?>
Note: See TracBrowser for help on using the browser.