PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 3.1.0
Defender Security – Malware Scanner, Login Security & Firewall v3.1.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 3.1.0, at src/traits/country.php

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