Validator::isEmail()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * This file is part of the php-epp2 library.
5
 *
6
 * (c) Gunter Grodotzki <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE file
9
 * that was distributed with this source code.
10
 */
11
12
namespace AfriCC\EPP;
13
14
/**
15
 * various validating methods needed to create sane EPP requests
16
 */
17
class Validator
18
{
19
    const TYPE_IPV4 = 1;
20
    const TYPE_IPV6 = 2;
21
22
    /**
23
     * hashmap country-code -> country name (english)
24
     *
25
     * @see https://gist.github.com/vxnick/380904
26
     *
27
     * @var array
28
     */
29
    protected static $countries = [
30
        'AF' => 'Afghanistan',
31
        'AX' => 'Aland Islands',
32
        'AL' => 'Albania',
33
        'DZ' => 'Algeria',
34
        'AS' => 'American Samoa',
35
        'AD' => 'Andorra',
36
        'AO' => 'Angola',
37
        'AI' => 'Anguilla',
38
        'AQ' => 'Antarctica',
39
        'AG' => 'Antigua And Barbuda',
40
        'AR' => 'Argentina',
41
        'AM' => 'Armenia',
42
        'AW' => 'Aruba',
43
        'AU' => 'Australia',
44
        'AT' => 'Austria',
45
        'AZ' => 'Azerbaijan',
46
        'BS' => 'Bahamas',
47
        'BH' => 'Bahrain',
48
        'BD' => 'Bangladesh',
49
        'BB' => 'Barbados',
50
        'BY' => 'Belarus',
51
        'BE' => 'Belgium',
52
        'BZ' => 'Belize',
53
        'BJ' => 'Benin',
54
        'BM' => 'Bermuda',
55
        'BT' => 'Bhutan',
56
        'BO' => 'Bolivia',
57
        'BA' => 'Bosnia And Herzegovina',
58
        'BW' => 'Botswana',
59
        'BV' => 'Bouvet Island',
60
        'BR' => 'Brazil',
61
        'IO' => 'British Indian Ocean Territory',
62
        'BN' => 'Brunei Darussalam',
63
        'BG' => 'Bulgaria',
64
        'BF' => 'Burkina Faso',
65
        'BI' => 'Burundi',
66
        'KH' => 'Cambodia',
67
        'CM' => 'Cameroon',
68
        'CA' => 'Canada',
69
        'CV' => 'Cape Verde',
70
        'KY' => 'Cayman Islands',
71
        'CF' => 'Central African Republic',
72
        'TD' => 'Chad',
73
        'CL' => 'Chile',
74
        'CN' => 'China',
75
        'CX' => 'Christmas Island',
76
        'CC' => 'Cocos (Keeling) Islands',
77
        'CO' => 'Colombia',
78
        'KM' => 'Comoros',
79
        'CG' => 'Congo',
80
        'CD' => 'Congo, Democratic Republic',
81
        'CK' => 'Cook Islands',
82
        'CR' => 'Costa Rica',
83
        'CI' => 'Cote D\'Ivoire',
84
        'HR' => 'Croatia',
85
        'CU' => 'Cuba',
86
        'CY' => 'Cyprus',
87
        'CZ' => 'Czech Republic',
88
        'DK' => 'Denmark',
89
        'DJ' => 'Djibouti',
90
        'DM' => 'Dominica',
91
        'DO' => 'Dominican Republic',
92
        'EC' => 'Ecuador',
93
        'EG' => 'Egypt',
94
        'SV' => 'El Salvador',
95
        'GQ' => 'Equatorial Guinea',
96
        'ER' => 'Eritrea',
97
        'EE' => 'Estonia',
98
        'ET' => 'Ethiopia',
99
        'FK' => 'Falkland Islands (Malvinas)',
100
        'FO' => 'Faroe Islands',
101
        'FJ' => 'Fiji',
102
        'FI' => 'Finland',
103
        'FR' => 'France',
104
        'GF' => 'French Guiana',
105
        'PF' => 'French Polynesia',
106
        'TF' => 'French Southern Territories',
107
        'GA' => 'Gabon',
108
        'GM' => 'Gambia',
109
        'GE' => 'Georgia',
110
        'DE' => 'Germany',
111
        'GH' => 'Ghana',
112
        'GI' => 'Gibraltar',
113
        'GR' => 'Greece',
114
        'GL' => 'Greenland',
115
        'GD' => 'Grenada',
116
        'GP' => 'Guadeloupe',
117
        'GU' => 'Guam',
118
        'GT' => 'Guatemala',
119
        'GG' => 'Guernsey',
120
        'GN' => 'Guinea',
121
        'GW' => 'Guinea-Bissau',
122
        'GY' => 'Guyana',
123
        'HT' => 'Haiti',
124
        'HM' => 'Heard Island & Mcdonald Islands',
125
        'VA' => 'Holy See (Vatican City State)',
126
        'HN' => 'Honduras',
127
        'HK' => 'Hong Kong',
128
        'HU' => 'Hungary',
129
        'IS' => 'Iceland',
130
        'IN' => 'India',
131
        'ID' => 'Indonesia',
132
        'IR' => 'Iran, Islamic Republic Of',
133
        'IQ' => 'Iraq',
134
        'IE' => 'Ireland',
135
        'IM' => 'Isle Of Man',
136
        'IL' => 'Israel',
137
        'IT' => 'Italy',
138
        'JM' => 'Jamaica',
139
        'JP' => 'Japan',
140
        'JE' => 'Jersey',
141
        'JO' => 'Jordan',
142
        'KZ' => 'Kazakhstan',
143
        'KE' => 'Kenya',
144
        'KI' => 'Kiribati',
145
        'KR' => 'Korea',
146
        'KW' => 'Kuwait',
147
        'KG' => 'Kyrgyzstan',
148
        'LA' => 'Lao People\'s Democratic Republic',
149
        'LV' => 'Latvia',
150
        'LB' => 'Lebanon',
151
        'LS' => 'Lesotho',
152
        'LR' => 'Liberia',
153
        'LY' => 'Libyan Arab Jamahiriya',
154
        'LI' => 'Liechtenstein',
155
        'LT' => 'Lithuania',
156
        'LU' => 'Luxembourg',
157
        'MO' => 'Macao',
158
        'MK' => 'Macedonia',
159
        'MG' => 'Madagascar',
160
        'MW' => 'Malawi',
161
        'MY' => 'Malaysia',
162
        'MV' => 'Maldives',
163
        'ML' => 'Mali',
164
        'MT' => 'Malta',
165
        'MH' => 'Marshall Islands',
166
        'MQ' => 'Martinique',
167
        'MR' => 'Mauritania',
168
        'MU' => 'Mauritius',
169
        'YT' => 'Mayotte',
170
        'MX' => 'Mexico',
171
        'FM' => 'Micronesia, Federated States Of',
172
        'MD' => 'Moldova',
173
        'MC' => 'Monaco',
174
        'MN' => 'Mongolia',
175
        'ME' => 'Montenegro',
176
        'MS' => 'Montserrat',
177
        'MA' => 'Morocco',
178
        'MZ' => 'Mozambique',
179
        'MM' => 'Myanmar',
180
        'NA' => 'Namibia',
181
        'NR' => 'Nauru',
182
        'NP' => 'Nepal',
183
        'NL' => 'Netherlands',
184
        'AN' => 'Netherlands Antilles',
185
        'NC' => 'New Caledonia',
186
        'NZ' => 'New Zealand',
187
        'NI' => 'Nicaragua',
188
        'NE' => 'Niger',
189
        'NG' => 'Nigeria',
190
        'NU' => 'Niue',
191
        'NF' => 'Norfolk Island',
192
        'MP' => 'Northern Mariana Islands',
193
        'NO' => 'Norway',
194
        'OM' => 'Oman',
195
        'PK' => 'Pakistan',
196
        'PW' => 'Palau',
197
        'PS' => 'Palestinian Territory, Occupied',
198
        'PA' => 'Panama',
199
        'PG' => 'Papua New Guinea',
200
        'PY' => 'Paraguay',
201
        'PE' => 'Peru',
202
        'PH' => 'Philippines',
203
        'PN' => 'Pitcairn',
204
        'PL' => 'Poland',
205
        'PT' => 'Portugal',
206
        'PR' => 'Puerto Rico',
207
        'QA' => 'Qatar',
208
        'RE' => 'Reunion',
209
        'RO' => 'Romania',
210
        'RU' => 'Russian Federation',
211
        'RW' => 'Rwanda',
212
        'BL' => 'Saint Barthelemy',
213
        'SH' => 'Saint Helena',
214
        'KN' => 'Saint Kitts And Nevis',
215
        'LC' => 'Saint Lucia',
216
        'MF' => 'Saint Martin',
217
        'PM' => 'Saint Pierre And Miquelon',
218
        'VC' => 'Saint Vincent And Grenadines',
219
        'WS' => 'Samoa',
220
        'SM' => 'San Marino',
221
        'ST' => 'Sao Tome And Principe',
222
        'SA' => 'Saudi Arabia',
223
        'SN' => 'Senegal',
224
        'RS' => 'Serbia',
225
        'SC' => 'Seychelles',
226
        'SL' => 'Sierra Leone',
227
        'SG' => 'Singapore',
228
        'SK' => 'Slovakia',
229
        'SI' => 'Slovenia',
230
        'SB' => 'Solomon Islands',
231
        'SO' => 'Somalia',
232
        'ZA' => 'South Africa',
233
        'GS' => 'South Georgia And Sandwich Isl.',
234
        'ES' => 'Spain',
235
        'LK' => 'Sri Lanka',
236
        'SD' => 'Sudan',
237
        'SR' => 'Suriname',
238
        'SJ' => 'Svalbard And Jan Mayen',
239
        'SZ' => 'Swaziland',
240
        'SE' => 'Sweden',
241
        'CH' => 'Switzerland',
242
        'SY' => 'Syrian Arab Republic',
243
        'TW' => 'Taiwan',
244
        'TJ' => 'Tajikistan',
245
        'TZ' => 'Tanzania',
246
        'TH' => 'Thailand',
247
        'TL' => 'Timor-Leste',
248
        'TG' => 'Togo',
249
        'TK' => 'Tokelau',
250
        'TO' => 'Tonga',
251
        'TT' => 'Trinidad And Tobago',
252
        'TN' => 'Tunisia',
253
        'TR' => 'Turkey',
254
        'TM' => 'Turkmenistan',
255
        'TC' => 'Turks And Caicos Islands',
256
        'TV' => 'Tuvalu',
257
        'UG' => 'Uganda',
258
        'UA' => 'Ukraine',
259
        'AE' => 'United Arab Emirates',
260
        'GB' => 'United Kingdom',
261
        'US' => 'United States',
262
        'UM' => 'United States Outlying Islands',
263
        'UY' => 'Uruguay',
264
        'UZ' => 'Uzbekistan',
265
        'VU' => 'Vanuatu',
266
        'VE' => 'Venezuela',
267
        'VN' => 'Viet Nam',
268
        'VG' => 'Virgin Islands, British',
269
        'VI' => 'Virgin Islands, U.S.',
270
        'WF' => 'Wallis And Futuna',
271
        'EH' => 'Western Sahara',
272
        'YE' => 'Yemen',
273
        'ZM' => 'Zambia',
274
        'ZW' => 'Zimbabwe',
275
    ];
276
277
    /**
278
     * returns version of IP address, or false if not an IP
279
     *
280
     * @param string $ip
281
     *
282
     * @return bool|int
283
     */
284
    public static function getIPType($ip)
285
    {
286
        if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
287
            return self::TYPE_IPV4;
288
        } elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
289
            return self::TYPE_IPV6;
290
        } else {
291
            return false;
292
        }
293
    }
294
295
    /**
296
     * returns true if hostname is usuable
297
     *
298
     * @author velcrow
299
     *
300
     * @see http://stackoverflow.com/a/4694816
301
     *
302
     * @param string $hostname
303
     *
304
     * @return bool
305
     */
306
    public static function isHostname($hostname)
307
    {
308
        if (preg_match('/^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*$/i', $hostname) && //valid chars check
309
            preg_match('/^.{1,253}$/', $hostname) && //overall length check
310
            preg_match('/^[^\.]{1,63}(\.[^\.]{1,63})*$/', $hostname) && //length of each label
311
            strpos($hostname, '.') // has at least two labels (SLD + TLD)
312
        ) {
313
            return true;
314
        }
315
316
        return false;
317
    }
318
319
    /**
320
     * returns true if email is usuable
321
     *
322
     * @param string $email
323
     */
324
    public static function isEmail($email)
325
    {
326
        $pos = strrpos($email, '@');
327
        if ($pos === false) {
328
            return false;
329
        }
330
331
        $ascii_email = substr($email, 0, $pos) . '@' . idn_to_ascii(substr($email, $pos + 1), IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46);
332
333
        return (bool) filter_var($ascii_email, FILTER_VALIDATE_EMAIL);
334
    }
335
336
    /**
337
     * returns country name if country code is known, false if unknown
338
     *
339
     * @param string $country_code
340
     *
341
     * @return bool|string
342
     */
343
    public static function isCountryCode($country_code)
344
    {
345
        // actually we don't need to check for any conventions...
346
        $country_code = strtoupper($country_code);
347
348
        if (isset(self::$countries[$country_code])) {
349
            return self::$countries[$country_code];
350
        }
351
352
        return false;
353
    }
354
}
355