PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.3.0
Defender Security – Malware Scanner, Login Security & Firewall v6.3.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / traits / country.php

country.php in Defender Security – Malware Scanner, Login Security & Firewall 6.3.0, at src/traits/country.php

334 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Country helper.
4 *
5 * @package WP_Defender\Traits
6 */
7
8 namespace WP_Defender\Traits;
9
10 use RuntimeException;
11 use WP_Defender\Extra\GeoIp;
12 use WP_DEFENDER_VENDOR\MaxMind\Db\Reader\InvalidDatabaseException;
13 use WP_Defender\Model\Setting\Blacklist_Lockout;
14
15 trait Country {
16
17 /**
18 * Get country of the current user.
19 *
20 * @param string $ip IPV4 or IPV6 address.
21 *
22 * @return array|bool
23 * @throws RuntimeException Thrown when a dependency cannot be resolved.
24 * @throws InvalidDatabaseException Thrown for unexpected data is found in DB.
25 */
26 public function get_current_country( $ip ) {
27 if ( defender_is_wp_cli() ) {
28 // Never catch if from cli.
29 return false;
30 }
31 $service = wd_di()->get( \WP_Defender\Component\Blacklist_Lockout::class );
32 if ( ! $service->is_geodb_downloaded() ) {
33 return false;
34 }
35 if ( ! filter_var( $ip, FILTER_VALIDATE_IP ) ) {
36 return false;
37 }
38 $model = wd_di()->get( Blacklist_Lockout::class );
39 // Additional check if MaxMind dir is deleted.
40 if ( ! is_string( $model->geodb_path ) || ! is_file( $model->geodb_path ) ) {
41 return false;
42 }
43 $helper = new GeoIp( $model->geodb_path );
44
45 // Todo: add check for unhandled exceptions.
46 return $helper->ip_to_country( $ip );
47 }
48
49 /**
50 * Copy the list from https://gist.github.com/DHS/1340150.
51 *
52 * @return array
53 * @since 2.8.0 Add hook.
54 */
55 public function countries_list(): array {
56 return apply_filters(
57 'wd_countries',
58 array(
59 'AD' => 'Andorra',
60 'AF' => 'Afghanistan',
61 'AG' => 'Antigua and Barbuda',
62 'AI' => 'Anguilla',
63 'AL' => 'Albania',
64 'AM' => 'Armenia',
65 'AO' => 'Angola',
66 'AP' => 'Asia/Pacific Region',
67 'AQ' => 'Antarctica',
68 'AR' => 'Argentina',
69 'AS' => 'American Samoa',
70 'AT' => 'Austria',
71 'AU' => 'Australia',
72 'AW' => 'Aruba',
73 'AX' => 'Aland Islands',
74 'AZ' => 'Azerbaijan',
75 'BA' => 'Bosnia and Herzegovina',
76 'BB' => 'Barbados',
77 'BD' => 'Bangladesh',
78 'BE' => 'Belgium',
79 'BF' => 'Burkina Faso',
80 'BG' => 'Bulgaria',
81 'BH' => 'Bahrain',
82 'BI' => 'Burundi',
83 'BJ' => 'Benin',
84 'BL' => 'Saint Barthelemy',
85 'BM' => 'Bermuda',
86 'BN' => 'Brunei Darussalam',
87 'BO' => 'Bolivia',
88 'BQ' => 'Bonaire, Saint Eustatius and Saba',
89 'BR' => 'Brazil',
90 'BS' => 'Bahamas',
91 'BT' => 'Bhutan',
92 'BV' => 'Bouvet Island',
93 'BW' => 'Botswana',
94 'BY' => 'Belarus',
95 'BZ' => 'Belize',
96 'CA' => 'Canada',
97 'CC' => 'Cocos (Keeling) Islands',
98 'CD' => 'Congo, The Democratic Republic of the',
99 'CF' => 'Central African Republic',
100 'CG' => 'Congo',
101 'CH' => 'Switzerland',
102 'CI' => "Cote d'Ivoire",
103 'CK' => 'Cook Islands',
104 'CL' => 'Chile',
105 'CM' => 'Cameroon',
106 'CN' => 'China',
107 'CO' => 'Colombia',
108 'CR' => 'Costa Rica',
109 'CU' => 'Cuba',
110 'CV' => 'Cape Verde',
111 'CW' => 'Curacao',
112 'CX' => 'Christmas Island',
113 'CY' => 'Cyprus',
114 'CZ' => 'Czech Republic',
115 'DE' => 'Germany',
116 'DJ' => 'Djibouti',
117 'DK' => 'Denmark',
118 'DM' => 'Dominica',
119 'DO' => 'Dominican Republic',
120 'DZ' => 'Algeria',
121 'EC' => 'Ecuador',
122 'EE' => 'Estonia',
123 'EG' => 'Egypt',
124 'EH' => 'Western Sahara',
125 'ER' => 'Eritrea',
126 'ES' => 'Spain',
127 'ET' => 'Ethiopia',
128 'EU' => 'Europe',
129 'FI' => 'Finland',
130 'FJ' => 'Fiji',
131 'FK' => 'Falkland Islands (Malvinas)',
132 'FM' => 'Micronesia, Federated States of',
133 'FO' => 'Faroe Islands',
134 'FR' => 'France',
135 'GA' => 'Gabon',
136 'GB' => 'United Kingdom',
137 'GD' => 'Grenada',
138 'GE' => 'Georgia',
139 'GF' => 'French Guiana',
140 'GG' => 'Guernsey',
141 'GH' => 'Ghana',
142 'GI' => 'Gibraltar',
143 'GL' => 'Greenland',
144 'GM' => 'Gambia',
145 'GN' => 'Guinea',
146 'GP' => 'Guadeloupe',
147 'GQ' => 'Equatorial Guinea',
148 'GR' => 'Greece',
149 'GS' => 'South Georgia and the South Sandwich Islands',
150 'GT' => 'Guatemala',
151 'GU' => 'Guam',
152 'GW' => 'Guinea-Bissau',
153 'GY' => 'Guyana',
154 'HK' => 'Hong Kong',
155 'HM' => 'Heard Island and McDonald Islands',
156 'HN' => 'Honduras',
157 'HR' => 'Croatia',
158 'HT' => 'Haiti',
159 'HU' => 'Hungary',
160 'ID' => 'Indonesia',
161 'IE' => 'Ireland',
162 'IL' => 'Israel',
163 'IM' => 'Isle of Man',
164 'IN' => 'India',
165 'IO' => 'British Indian Ocean Territory',
166 'IQ' => 'Iraq',
167 'IR' => 'Iran, Islamic Republic of',
168 'IS' => 'Iceland',
169 'IT' => 'Italy',
170 'JE' => 'Jersey',
171 'JM' => 'Jamaica',
172 'JO' => 'Jordan',
173 'JP' => 'Japan',
174 'KE' => 'Kenya',
175 'KG' => 'Kyrgyzstan',
176 'KH' => 'Cambodia',
177 'KI' => 'Kiribati',
178 'KM' => 'Comoros',
179 'KN' => 'Saint Kitts and Nevis',
180 'KP' => "Korea, Democratic People's Republic of",
181 'KR' => 'Korea, Republic of',
182 'KW' => 'Kuwait',
183 'KY' => 'Cayman Islands',
184 'KZ' => 'Kazakhstan',
185 'LA' => "Lao People's Democratic Republic",
186 'LB' => 'Lebanon',
187 'LC' => 'Saint Lucia',
188 'LI' => 'Liechtenstein',
189 'LK' => 'Sri Lanka',
190 'LR' => 'Liberia',
191 'LS' => 'Lesotho',
192 'LT' => 'Lithuania',
193 'LU' => 'Luxembourg',
194 'LV' => 'Latvia',
195 'LY' => 'Libyan Arab Jamahiriya',
196 'MA' => 'Morocco',
197 'MC' => 'Monaco',
198 'MD' => 'Moldova, Republic of',
199 'ME' => 'Montenegro',
200 'MF' => 'Saint Martin',
201 'MG' => 'Madagascar',
202 'MH' => 'Marshall Islands',
203 'MK' => 'Macedonia',
204 'ML' => 'Mali',
205 'MM' => 'Myanmar',
206 'MN' => 'Mongolia',
207 'MO' => 'Macao',
208 'MP' => 'Northern Mariana Islands',
209 'MQ' => 'Martinique',
210 'MR' => 'Mauritania',
211 'MS' => 'Montserrat',
212 'MT' => 'Malta',
213 'MU' => 'Mauritius',
214 'MV' => 'Maldives',
215 'MW' => 'Malawi',
216 'MX' => 'Mexico',
217 'MY' => 'Malaysia',
218 'MZ' => 'Mozambique',
219 'NA' => 'Namibia',
220 'NC' => 'New Caledonia',
221 'NE' => 'Niger',
222 'NF' => 'Norfolk Island',
223 'NG' => 'Nigeria',
224 'NI' => 'Nicaragua',
225 'NL' => 'Netherlands',
226 'NO' => 'Norway',
227 'NP' => 'Nepal',
228 'NR' => 'Nauru',
229 'NU' => 'Niue',
230 'NZ' => 'New Zealand',
231 'OM' => 'Oman',
232 'PA' => 'Panama',
233 'PE' => 'Peru',
234 'PF' => 'French Polynesia',
235 'PG' => 'Papua New Guinea',
236 'PH' => 'Philippines',
237 'PK' => 'Pakistan',
238 'PL' => 'Poland',
239 'PM' => 'Saint Pierre and Miquelon',
240 'PN' => 'Pitcairn',
241 'PR' => 'Puerto Rico',
242 'PS' => 'Palestinian Territory',
243 'PT' => 'Portugal',
244 'PW' => 'Palau',
245 'PY' => 'Paraguay',
246 'QA' => 'Qatar',
247 'RE' => 'Reunion',
248 'RO' => 'Romania',
249 'RS' => 'Serbia',
250 'RU' => 'Russian Federation',
251 'RW' => 'Rwanda',
252 'SA' => 'Saudi Arabia',
253 'SB' => 'Solomon Islands',
254 'SC' => 'Seychelles',
255 'SD' => 'Sudan',
256 'SE' => 'Sweden',
257 'SG' => 'Singapore',
258 'SH' => 'Saint Helena',
259 'SI' => 'Slovenia',
260 'SJ' => 'Svalbard and Jan Mayen',
261 'SK' => 'Slovakia',
262 'SL' => 'Sierra Leone',
263 'SM' => 'San Marino',
264 'SN' => 'Senegal',
265 'SO' => 'Somalia',
266 'SR' => 'Suriname',
267 'SS' => 'South Sudan',
268 'ST' => 'Sao Tome and Principe',
269 'SV' => 'El Salvador',
270 'SX' => 'Sint Maarten',
271 'SY' => 'Syrian Arab Republic',
272 'SZ' => 'Swaziland',
273 'TC' => 'Turks and Caicos Islands',
274 'TD' => 'Chad',
275 'TF' => 'French Southern Territories',
276 'TG' => 'Togo',
277 'TH' => 'Thailand',
278 'TJ' => 'Tajikistan',
279 'TK' => 'Tokelau',
280 'TL' => 'Timor-Leste',
281 'TM' => 'Turkmenistan',
282 'TN' => 'Tunisia',
283 'TO' => 'Tonga',
284 'TR' => 'Turkey',
285 'TT' => 'Trinidad and Tobago',
286 'TV' => 'Tuvalu',
287 'TW' => 'Taiwan',
288 'TZ' => 'Tanzania, United Republic of',
289 'UA' => 'Ukraine',
290 'UG' => 'Uganda',
291 'AE' => 'United Arab Emirates',
292 'UM' => 'United States Minor Outlying Islands',
293 'US' => 'United States',
294 'UY' => 'Uruguay',
295 'UZ' => 'Uzbekistan',
296 'VA' => 'Holy See (Vatican City State)',
297 'VC' => 'Saint Vincent and the Grenadines',
298 'VE' => 'Venezuela',
299 'VG' => 'Virgin Islands, British',
300 'VI' => 'Virgin Islands, U.S.',
301 'VN' => 'Vietnam',
302 'VU' => 'Vanuatu',
303 'WF' => 'Wallis and Futuna',
304 'WS' => 'Samoa',
305 'YE' => 'Yemen',
306 'YT' => 'Mayotte',
307 'ZA' => 'South Africa',
308 'ZM' => 'Zambia',
309 'ZW' => 'Zimbabwe',
310 'A1' => 'Anonymous Proxy',
311 'A2' => 'Satellite Provider',
312 'O1' => 'Other Country',
313 )
314 );
315 }
316
317 /**
318 * Generic method to return country detail of an IP.
319 *
320 * @param string $ip IPV4 or IPV6 address.
321 *
322 * @return bool|array false on failure to fetch and array on success.
323 */
324 public function ip_to_country( $ip ) {
325 $country = $this->get_current_country( $ip );
326 if ( ! $country ) {
327 // since 3.8.0.
328 $country = apply_filters( 'wd_ip_to_country_api', $country, $ip );
329 }
330
331 return $country;
332 }
333 }
334