1 | <?php
|
---|
2 | /**
|
---|
3 | * EmailAddressValidator Class
|
---|
4 | *
|
---|
5 | * @author Dave Child <[email protected]>
|
---|
6 | * @link http://code.google.com/p/php-email-address-validation/
|
---|
7 | * @license http://www.opensource.org/licenses/bsd-license.php
|
---|
8 | * @version SVN r10 + Issue 15 fix + Issue 12 fix
|
---|
9 | */
|
---|
10 | class EmailAddressValidator {
|
---|
11 | /**
|
---|
12 | * Set true to allow addresses like me@localhost
|
---|
13 | */
|
---|
14 | public $allowLocalAddresses = false;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Check email address validity
|
---|
18 | * @param strEmailAddress Email address to be checked
|
---|
19 | * @return True if email is valid, false if not
|
---|
20 | */
|
---|
21 | public function check_email_address($strEmailAddress) {
|
---|
22 |
|
---|
23 | // If magic quotes is "on", email addresses with quote marks will
|
---|
24 | // fail validation because of added escape characters. Uncommenting
|
---|
25 | // the next three lines will allow for this issue.
|
---|
26 | //if (get_magic_quotes_gpc()) {
|
---|
27 | // $strEmailAddress = stripslashes($strEmailAddress);
|
---|
28 | //}
|
---|
29 |
|
---|
30 | // Control characters are not allowed
|
---|
31 | if (preg_match('/[\x00-\x1F\x7F-\xFF]/', $strEmailAddress)) {
|
---|
32 | return false;
|
---|
33 | }
|
---|
34 |
|
---|
35 | // Check email length - min 3 (a@a), max 256
|
---|
36 | if (!$this->check_text_length($strEmailAddress, 3, 256)) {
|
---|
37 | return false;
|
---|
38 | }
|
---|
39 |
|
---|
40 | // Split it into sections using last instance of "@"
|
---|
41 | $intAtSymbol = strrpos($strEmailAddress, '@');
|
---|
42 | if ($intAtSymbol === false) {
|
---|
43 | // No "@" symbol in email.
|
---|
44 | return false;
|
---|
45 | }
|
---|
46 | $arrEmailAddress[0] = substr($strEmailAddress, 0, $intAtSymbol);
|
---|
47 | $arrEmailAddress[1] = substr($strEmailAddress, $intAtSymbol + 1);
|
---|
48 |
|
---|
49 | // Count the "@" symbols. Only one is allowed, except where
|
---|
50 | // contained in quote marks in the local part. Quickest way to
|
---|
51 | // check this is to remove anything in quotes. We also remove
|
---|
52 | // characters escaped with backslash, and the backslash
|
---|
53 | // character.
|
---|
54 | $arrTempAddress[0] = preg_replace('/\./'
|
---|
55 | ,''
|
---|
56 | ,$arrEmailAddress[0]);
|
---|
57 | $arrTempAddress[0] = preg_replace('/"[^"]+"/'
|
---|
58 | ,''
|
---|
59 | ,$arrTempAddress[0]);
|
---|
60 | $arrTempAddress[1] = $arrEmailAddress[1];
|
---|
61 | $strTempAddress = $arrTempAddress[0] . $arrTempAddress[1];
|
---|
62 | // Then check - should be no "@" symbols.
|
---|
63 | if (strrpos($strTempAddress, '@') !== false) {
|
---|
64 | // "@" symbol found
|
---|
65 | return false;
|
---|
66 | }
|
---|
67 |
|
---|
68 | // Check local portion
|
---|
69 | if (!$this->check_local_portion($arrEmailAddress[0])) {
|
---|
70 | return false;
|
---|
71 | }
|
---|
72 |
|
---|
73 | // Check domain portion
|
---|
74 | if (!$this->check_domain_portion($arrEmailAddress[1])) {
|
---|
75 | return false;
|
---|
76 | }
|
---|
77 |
|
---|
78 | // If we're still here, all checks above passed. Email is valid.
|
---|
79 | return true;
|
---|
80 |
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Checks email section before "@" symbol for validity
|
---|
85 | * @param strLocalPortion Text to be checked
|
---|
86 | * @return True if local portion is valid, false if not
|
---|
87 | */
|
---|
88 | protected function check_local_portion($strLocalPortion) {
|
---|
89 | // Local portion can only be from 1 to 64 characters, inclusive.
|
---|
90 | // Please note that servers are encouraged to accept longer local
|
---|
91 | // parts than 64 characters.
|
---|
92 | if (!$this->check_text_length($strLocalPortion, 1, 64)) {
|
---|
93 | return false;
|
---|
94 | }
|
---|
95 | // Local portion must be:
|
---|
96 | // 1) a dot-atom (strings separated by periods)
|
---|
97 | // 2) a quoted string
|
---|
98 | // 3) an obsolete format string (combination of the above)
|
---|
99 | $arrLocalPortion = explode('.', $strLocalPortion);
|
---|
100 | for ($i = 0, $max = sizeof($arrLocalPortion); $i < $max; $i++) {
|
---|
101 | if (!preg_match('.^('
|
---|
102 | . '([A-Za-z0-9!#$%&\'*+/=?^_`{|}~-]'
|
---|
103 | . '[A-Za-z0-9!#$%&\'*+/=?^_`{|}~-]{0,63})'
|
---|
104 | .'|'
|
---|
105 | . '("[^\\\"]{0,62}")'
|
---|
106 | .')$.'
|
---|
107 | ,$arrLocalPortion[$i])) {
|
---|
108 | return false;
|
---|
109 | }
|
---|
110 | }
|
---|
111 | return true;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Checks email section after "@" symbol for validity
|
---|
116 | * @param strDomainPortion Text to be checked
|
---|
117 | * @return True if domain portion is valid, false if not
|
---|
118 | */
|
---|
119 | protected function check_domain_portion($strDomainPortion) {
|
---|
120 | // Total domain can only be from 1 to 255 characters, inclusive
|
---|
121 | if (!$this->check_text_length($strDomainPortion, 1, 255)) {
|
---|
122 | return false;
|
---|
123 | }
|
---|
124 |
|
---|
125 | // some IPv4/v6 regexps borrowed from Feyd
|
---|
126 | // see: http://forums.devnetwork.net/viewtopic.php?f=38&t=53479
|
---|
127 | $dec_octet = '(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|[0-9])';
|
---|
128 | $hex_digit = '[A-Fa-f0-9]';
|
---|
129 | $h16 = "{$hex_digit}{1,4}";
|
---|
130 | $IPv4Address = "$dec_octet\\.$dec_octet\\.$dec_octet\\.$dec_octet";
|
---|
131 | $ls32 = "(?:$h16:$h16|$IPv4Address)";
|
---|
132 | $IPv6Address =
|
---|
133 | "(?:(?:{$IPv4Address})|(?:".
|
---|
134 | "(?:$h16:){6}$ls32" .
|
---|
135 | "|::(?:$h16:){5}$ls32" .
|
---|
136 | "|(?:$h16)?::(?:$h16:){4}$ls32" .
|
---|
137 | "|(?:(?:$h16:){0,1}$h16)?::(?:$h16:){3}$ls32" .
|
---|
138 | "|(?:(?:$h16:){0,2}$h16)?::(?:$h16:){2}$ls32" .
|
---|
139 | "|(?:(?:$h16:){0,3}$h16)?::(?:$h16:){1}$ls32" .
|
---|
140 | "|(?:(?:$h16:){0,4}$h16)?::$ls32" .
|
---|
141 | "|(?:(?:$h16:){0,5}$h16)?::$h16" .
|
---|
142 | "|(?:(?:$h16:){0,6}$h16)?::" .
|
---|
143 | ")(?:\\/(?:12[0-8]|1[0-1][0-9]|[1-9][0-9]|[0-9]))?)";
|
---|
144 |
|
---|
145 | // Check if domain is IP, possibly enclosed in square brackets.
|
---|
146 | if (preg_match("/^($IPv4Address|\[$IPv4Address\]|\[$IPv6Address\])$/",
|
---|
147 | $strDomainPortion)){
|
---|
148 | return true;
|
---|
149 | } else {
|
---|
150 | $arrDomainPortion = explode('.', $strDomainPortion);
|
---|
151 | if (!$this->allowLocalAddresses && sizeof($arrDomainPortion) < 2) {
|
---|
152 | return false; // Not enough parts to domain
|
---|
153 | }
|
---|
154 | for ($i = 0, $max = sizeof($arrDomainPortion); $i < $max; $i++) {
|
---|
155 | // Each portion must be between 1 and 63 characters, inclusive
|
---|
156 | if (!$this->check_text_length($arrDomainPortion[$i], 1, 63)) {
|
---|
157 | return false;
|
---|
158 | }
|
---|
159 | if (!preg_match('/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|'
|
---|
160 | .'([A-Za-z0-9]+))$/', $arrDomainPortion[$i])) {
|
---|
161 | return false;
|
---|
162 | }
|
---|
163 | if ($i == $max - 1) { // TLD cannot be only numbers
|
---|
164 | if (strlen(preg_replace('/[0-9]/', '', $arrDomainPortion[$i])) <= 0) {
|
---|
165 | return false;
|
---|
166 | }
|
---|
167 | }
|
---|
168 | }
|
---|
169 | }
|
---|
170 | return true;
|
---|
171 | }
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Check given text length is between defined bounds
|
---|
175 | * @param strText Text to be checked
|
---|
176 | * @param intMinimum Minimum acceptable length
|
---|
177 | * @param intMaximum Maximum acceptable length
|
---|
178 | * @return True if string is within bounds (inclusive), false if not
|
---|
179 | */
|
---|
180 | protected function check_text_length($strText, $intMinimum, $intMaximum) {
|
---|
181 | // Minimum and maximum are both inclusive
|
---|
182 | $intTextLength = strlen($strText);
|
---|
183 | if (($intTextLength < $intMinimum) || ($intTextLength > $intMaximum)) {
|
---|
184 | return false;
|
---|
185 | } else {
|
---|
186 | return true;
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | }
|
---|
191 |
|
---|