Created
June 25, 2022 05:45
-
-
Save trvswgnr/26218f577f6c4a42820a6f26a5006416 to your computer and use it in GitHub Desktop.
Class for a valid blacklist item, i.e. a special string that is either a valid email address, IPv4 address, web domain, or country code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class BlacklistItem { | |
private $value; | |
public function __construct($string) { | |
$string = trim($string); | |
if (self::isValid($string)) { | |
$this->value = $string; | |
} else { | |
throw new \InvalidArgumentException('$string must be a valid IP address, email, country, or web domain.'); | |
} | |
$this->value = (string) $string; | |
return $string; | |
} | |
protected static function isValid($string) { | |
$countries = self::getCountries(); | |
$is_valid_country = in_array(strtoupper($string), array_keys($countries)) || in_array(strtolower($string), array_map('strtolower', array_values($countries))); | |
$is_valid_email = strlen($string) <= 318 && preg_match('/^([a-z0-9._*-]{0,63}[a-z0-9*]+@([a-z0-9*-]{1,63}\.){1,4}([a-z0-9*]\.?){1,63})$/i', $string); | |
$is_valid_ipv4 = strlen($string) <= 16 && preg_match('/^[*0-2]?[*0-9]?[*0-9]?\.?[*0-2]?[*0-9]?[*0-9]?\.?[*0-2]?[*0-9]?[*0-9]?\.?[*0-2]?[*0-9]?[*0-9]?$/i', $string); | |
$is_invalid_ipv4 = !$is_valid_ipv4 && strlen($string) <= 16 && preg_match('/^[0-9.*]+$/i', $string); | |
$is_valid_domain = !$is_invalid_ipv4 && strlen($string) <= 255 && preg_match('/^([*a-z0-9][*a-z0-9-]{0,61}[*a-z0-9]\.?|[*a-z0-9]\.?){1,4}\.[*a-z0-9]{1,63}$/i', $string); | |
return $is_valid_email || $is_valid_ipv4 || $is_valid_domain; | |
} | |
public function __toString () { | |
return $this->value; | |
} | |
public static function create($string) { | |
return new self($string); | |
} | |
public static function getCountries() { | |
return array( | |
'AW' => 'Aruba', | |
'AF' => 'Afghanistan', | |
'AO' => 'Angola', | |
'AI' => 'Anguilla', | |
'AX' => 'Åland Islands', | |
'AL' => 'Albania', | |
'AD' => 'Andorra', | |
'AE' => 'United Arab Emirates', | |
'AR' => 'Argentina', | |
'AM' => 'Armenia', | |
'AS' => 'American Samoa', | |
'AQ' => 'Antarctica', | |
'TF' => 'French Southern and Antarctic Lands', | |
'AG' => 'Antigua and Barbuda', | |
'AU' => 'Australia', | |
'AT' => 'Austria', | |
'AZ' => 'Azerbaijan', | |
'BI' => 'Burundi', | |
'BE' => 'Belgium', | |
'BJ' => 'Benin', | |
'BF' => 'Burkina Faso', | |
'BD' => 'Bangladesh', | |
'BG' => 'Bulgaria', | |
'BH' => 'Bahrain', | |
'BS' => 'Bahamas', | |
'BA' => 'Bosnia and Herzegovina', | |
'BL' => 'Saint Barthélemy', | |
'BY' => 'Belarus', | |
'BZ' => 'Belize', | |
'BM' => 'Bermuda', | |
'BO' => 'Bolivia', | |
'BR' => 'Brazil', | |
'BB' => 'Barbados', | |
'BN' => 'Brunei', | |
'BT' => 'Bhutan', | |
'BV' => 'Bouvet Island', | |
'BW' => 'Botswana', | |
'CF' => 'Central African Republic', | |
'CA' => 'Canada', | |
'CC' => 'Cocos (Keeling) Islands', | |
'CH' => 'Switzerland', | |
'CL' => 'Chile', | |
'CN' => 'China', | |
'CI' => 'Ivory Coast', | |
'CM' => 'Cameroon', | |
'CD' => 'DR Congo', | |
'CG' => 'Republic of the Congo', | |
'CK' => 'Cook Islands', | |
'CO' => 'Colombia', | |
'KM' => 'Comoros', | |
'CV' => 'Cape Verde', | |
'CR' => 'Costa Rica', | |
'CU' => 'Cuba', | |
'CW' => 'Curaçao', | |
'CX' => 'Christmas Island', | |
'KY' => 'Cayman Islands', | |
'CY' => 'Cyprus', | |
'CZ' => 'Czech Republic', | |
'DE' => 'Germany', | |
'DJ' => 'Djibouti', | |
'DM' => 'Dominica', | |
'DK' => 'Denmark', | |
'DO' => 'Dominican Republic', | |
'DZ' => 'Algeria', | |
'EC' => 'Ecuador', | |
'EG' => 'Egypt', | |
'ER' => 'Eritrea', | |
'EH' => 'Western Sahara', | |
'ES' => 'Spain', | |
'EE' => 'Estonia', | |
'ET' => 'Ethiopia', | |
'FI' => 'Finland', | |
'FJ' => 'Fiji', | |
'FK' => 'Falkland Islands', | |
'FR' => 'France', | |
'FO' => 'Faroe Islands', | |
'FM' => 'Micronesia', | |
'GA' => 'Gabon', | |
'GB' => 'United Kingdom', | |
'GE' => 'Georgia', | |
'GG' => 'Guernsey', | |
'GH' => 'Ghana', | |
'GI' => 'Gibraltar', | |
'GN' => 'Guinea', | |
'GP' => 'Guadeloupe', | |
'GM' => 'Gambia', | |
'GW' => 'Guinea-Bissau', | |
'GQ' => 'Equatorial Guinea', | |
'GR' => 'Greece', | |
'GD' => 'Grenada', | |
'GL' => 'Greenland', | |
'GT' => 'Guatemala', | |
'GF' => 'French Guiana', | |
'GU' => 'Guam', | |
'GY' => 'Guyana', | |
'HK' => 'Hong Kong', | |
'HM' => 'Heard Island and McDonald Islands', | |
'HN' => 'Honduras', | |
'HR' => 'Croatia', | |
'HT' => 'Haiti', | |
'HU' => 'Hungary', | |
'ID' => 'Indonesia', | |
'IM' => 'Isle of Man', | |
'IN' => 'India', | |
'IO' => 'British Indian Ocean Territory', | |
'IE' => 'Ireland', | |
'IR' => 'Iran', | |
'IQ' => 'Iraq', | |
'IS' => 'Iceland', | |
'IL' => 'Israel', | |
'IT' => 'Italy', | |
'JM' => 'Jamaica', | |
'JE' => 'Jersey', | |
'JO' => 'Jordan', | |
'JP' => 'Japan', | |
'KZ' => 'Kazakhstan', | |
'KE' => 'Kenya', | |
'KG' => 'Kyrgyzstan', | |
'KH' => 'Cambodia', | |
'KI' => 'Kiribati', | |
'KN' => 'Saint Kitts and Nevis', | |
'KR' => 'South Korea', | |
'XK' => 'Kosovo', | |
'KW' => 'Kuwait', | |
'LA' => 'Laos', | |
'LB' => 'Lebanon', | |
'LR' => 'Liberia', | |
'LY' => 'Libya', | |
'LC' => 'Saint Lucia', | |
'LI' => 'Liechtenstein', | |
'LK' => 'Sri Lanka', | |
'LS' => 'Lesotho', | |
'LT' => 'Lithuania', | |
'LU' => 'Luxembourg', | |
'LV' => 'Latvia', | |
'MO' => 'Macau', | |
'MF' => 'Saint Martin', | |
'MA' => 'Morocco', | |
'MC' => 'Monaco', | |
'MD' => 'Moldova', | |
'MG' => 'Madagascar', | |
'MV' => 'Maldives', | |
'MX' => 'Mexico', | |
'MH' => 'Marshall Islands', | |
'MK' => 'Macedonia', | |
'ML' => 'Mali', | |
'MT' => 'Malta', | |
'MM' => 'Myanmar', | |
'ME' => 'Montenegro', | |
'MN' => 'Mongolia', | |
'MP' => 'Northern Mariana Islands', | |
'MZ' => 'Mozambique', | |
'MR' => 'Mauritania', | |
'MS' => 'Montserrat', | |
'MQ' => 'Martinique', | |
'MU' => 'Mauritius', | |
'MW' => 'Malawi', | |
'MY' => 'Malaysia', | |
'YT' => 'Mayotte', | |
'NA' => 'Namibia', | |
'NC' => 'New Caledonia', | |
'NE' => 'Niger', | |
'NF' => 'Norfolk Island', | |
'NG' => 'Nigeria', | |
'NI' => 'Nicaragua', | |
'NU' => 'Niue', | |
'NL' => 'Netherlands', | |
'NO' => 'Norway', | |
'NP' => 'Nepal', | |
'NR' => 'Nauru', | |
'NZ' => 'New Zealand', | |
'OM' => 'Oman', | |
'PK' => 'Pakistan', | |
'PA' => 'Panama', | |
'PN' => 'Pitcairn Islands', | |
'PE' => 'Peru', | |
'PH' => 'Philippines', | |
'PW' => 'Palau', | |
'PG' => 'Papua New Guinea', | |
'PL' => 'Poland', | |
'PR' => 'Puerto Rico', | |
'KP' => 'North Korea', | |
'PT' => 'Portugal', | |
'PY' => 'Paraguay', | |
'PS' => 'Palestine', | |
'PF' => 'French Polynesia', | |
'QA' => 'Qatar', | |
'RE' => 'Réunion', | |
'RO' => 'Romania', | |
'RU' => 'Russia', | |
'RW' => 'Rwanda', | |
'SA' => 'Saudi Arabia', | |
'SD' => 'Sudan', | |
'SN' => 'Senegal', | |
'SG' => 'Singapore', | |
'GS' => 'South Georgia', | |
'SJ' => 'Svalbard and Jan Mayen', | |
'SB' => 'Solomon Islands', | |
'SL' => 'Sierra Leone', | |
'SV' => 'El Salvador', | |
'SM' => 'San Marino', | |
'SO' => 'Somalia', | |
'PM' => 'Saint Pierre and Miquelon', | |
'RS' => 'Serbia', | |
'SS' => 'South Sudan', | |
'ST' => 'São Tomé and Príncipe', | |
'SR' => 'Suriname', | |
'SK' => 'Slovakia', | |
'SI' => 'Slovenia', | |
'SE' => 'Sweden', | |
'SZ' => 'Swaziland', | |
'SX' => 'Sint Maarten', | |
'SC' => 'Seychelles', | |
'SY' => 'Syria', | |
'TC' => 'Turks and Caicos Islands', | |
'TD' => 'Chad', | |
'TG' => 'Togo', | |
'TH' => 'Thailand', | |
'TJ' => 'Tajikistan', | |
'TK' => 'Tokelau', | |
'TM' => 'Turkmenistan', | |
'TL' => 'Timor-Leste', | |
'TO' => 'Tonga', | |
'TT' => 'Trinidad and Tobago', | |
'TN' => 'Tunisia', | |
'TR' => 'Turkey', | |
'TV' => 'Tuvalu', | |
'TW' => 'Taiwan', | |
'TZ' => 'Tanzania', | |
'UG' => 'Uganda', | |
'UA' => 'Ukraine', | |
'UM' => 'United States Minor Outlying Islands', | |
'UY' => 'Uruguay', | |
'US' => 'United States', | |
'UZ' => 'Uzbekistan', | |
'VA' => 'Vatican City', | |
'VC' => 'Saint Vincent and the Grenadines', | |
'VE' => 'Venezuela', | |
'VG' => 'British Virgin Islands', | |
'VI' => 'United States Virgin Islands', | |
'VN' => 'Vietnam', | |
'VU' => 'Vanuatu', | |
'WF' => 'Wallis and Futuna', | |
'WS' => 'Samoa', | |
'YE' => 'Yemen', | |
'ZA' => 'South Africa', | |
'ZM' => 'Zambia', | |
'ZW' => 'Zimbabwe', | |
'BQ' => 'Caribbean Netherlands', | |
'SH' => 'Saint Helena', | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment