Completed
Push — master ( 9d13eb...060bbf )
by Antonio Carlos
06:41
created

Countries::makeCountryFromString()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 9.2
cc 4
eloc 7
nc 6
nop 1
1
<?php
2
3
namespace PragmaRX\Firewall\Repositories;
4
5
use Illuminate\Support\Collection;
6
use PragmaRX\Firewall\Support\ServiceInstances;
7
use PragmaRX\Support\IpAddress;
8
9
class Countries
10
{
11
    use ServiceInstances;
12
13
    protected $all = [
14
        'ad'   => 'Andorra, Principality of',
15
        'ae'   => 'United Arab Emirates',
16
        'af'   => 'Afghanistan, Islamic State of',
17
        'ag'   => 'Antigua and Barbuda',
18
        'ai'   => 'Anguilla',
19
        'al'   => 'Albania',
20
        'am'   => 'Armenia',
21
        'an'   => 'Netherlands Antilles',
22
        'ao'   => 'Angola',
23
        'aq'   => 'Antarctica',
24
        'ar'   => 'Argentina',
25
        'arpa' => 'Old style Arpanet',
26
        'as'   => 'American Samoa',
27
        'at'   => 'Austria',
28
        'au'   => 'Australia',
29
        'aw'   => 'Aruba',
30
        'az'   => 'Azerbaidjan',
31
        'ba'   => 'Bosnia-Herzegovina',
32
        'bb'   => 'Barbados',
33
        'bd'   => 'Bangladesh',
34
        'be'   => 'Belgium',
35
        'bf'   => 'Burkina Faso',
36
        'bg'   => 'Bulgaria',
37
        'bh'   => 'Bahrain',
38
        'bi'   => 'Burundi',
39
        'bj'   => 'Benin',
40
        'bm'   => 'Bermuda',
41
        'bn'   => 'Brunei Darussalam',
42
        'bo'   => 'Bolivia',
43
        'br'   => 'Brazil',
44
        'bs'   => 'Bahamas',
45
        'bt'   => 'Bhutan',
46
        'bv'   => 'Bouvet Island',
47
        'bw'   => 'Botswana',
48
        'by'   => 'Belarus',
49
        'bz'   => 'Belize',
50
        'ca'   => 'Canada',
51
        'cc'   => 'Cocos (Keeling) Islands',
52
        'cf'   => 'Central African Republic',
53
        'cd'   => 'Congo, The Democratic Republic of the',
54
        'cg'   => 'Congo',
55
        'ch'   => 'Switzerland',
56
        'ci'   => 'Ivory Coast (Cote D\'Ivoire)',
57
        'ck'   => 'Cook Islands',
58
        'cl'   => 'Chile',
59
        'cm'   => 'Cameroon',
60
        'cn'   => 'China',
61
        'co'   => 'Colombia',
62
        'com'  => 'Commercial',
63
        'cr'   => 'Costa Rica',
64
        'cs'   => 'Former Czechoslovakia',
65
        'cu'   => 'Cuba',
66
        'cv'   => 'Cape Verde',
67
        'cx'   => 'Christmas Island',
68
        'cy'   => 'Cyprus',
69
        'cz'   => 'Czech Republic',
70
        'de'   => 'Germany',
71
        'dj'   => 'Djibouti',
72
        'dk'   => 'Denmark',
73
        'dm'   => 'Dominica',
74
        'do'   => 'Dominican Republic',
75
        'dz'   => 'Algeria',
76
        'ec'   => 'Ecuador',
77
        'edu'  => 'Educational',
78
        'ee'   => 'Estonia',
79
        'eg'   => 'Egypt',
80
        'eh'   => 'Western Sahara',
81
        'er'   => 'Eritrea',
82
        'es'   => 'Spain',
83
        'et'   => 'Ethiopia',
84
        'fi'   => 'Finland',
85
        'fj'   => 'Fiji',
86
        'fk'   => 'Falkland Islands',
87
        'fm'   => 'Micronesia',
88
        'fo'   => 'Faroe Islands',
89
        'fr'   => 'France',
90
        'fx'   => 'France (European Territory)',
91
        'ga'   => 'Gabon',
92
        'gb'   => 'Great Britain',
93
        'gd'   => 'Grenada',
94
        'ge'   => 'Georgia',
95
        'gf'   => 'French Guyana',
96
        'gh'   => 'Ghana',
97
        'gi'   => 'Gibraltar',
98
        'gl'   => 'Greenland',
99
        'gm'   => 'Gambia',
100
        'gn'   => 'Guinea',
101
        'gov'  => 'USA Government',
102
        'gp'   => 'Guadeloupe (French)',
103
        'gq'   => 'Equatorial Guinea',
104
        'gr'   => 'Greece',
105
        'gs'   => 'S. Georgia & S. Sandwich Isls.',
106
        'gt'   => 'Guatemala',
107
        'gu'   => 'Guam (USA)',
108
        'gw'   => 'Guinea Bissau',
109
        'gy'   => 'Guyana',
110
        'hk'   => 'Hong Kong',
111
        'hm'   => 'Heard and McDonald Islands',
112
        'hn'   => 'Honduras',
113
        'hr'   => 'Croatia',
114
        'ht'   => 'Haiti',
115
        'hu'   => 'Hungary',
116
        'id'   => 'Indonesia',
117
        'ie'   => 'Ireland',
118
        'il'   => 'Israel',
119
        'in'   => 'India',
120
        'int'  => 'International',
121
        'io'   => 'British Indian Ocean Territory',
122
        'iq'   => 'Iraq',
123
        'ir'   => 'Iran',
124
        'is'   => 'Iceland',
125
        'it'   => 'Italy',
126
        'jm'   => 'Jamaica',
127
        'jo'   => 'Jordan',
128
        'jp'   => 'Japan',
129
        'ke'   => 'Kenya',
130
        'kg'   => 'Kyrgyz Republic (Kyrgyzstan)',
131
        'kh'   => 'Cambodia, Kingdom of',
132
        'ki'   => 'Kiribati',
133
        'km'   => 'Comoros',
134
        'kn'   => 'Saint Kitts & Nevis Anguilla',
135
        'kp'   => 'North Korea',
136
        'kr'   => 'South Korea',
137
        'kw'   => 'Kuwait',
138
        'ky'   => 'Cayman Islands',
139
        'kz'   => 'Kazakhstan',
140
        'la'   => 'Laos',
141
        'lb'   => 'Lebanon',
142
        'lc'   => 'Saint Lucia',
143
        'li'   => 'Liechtenstein',
144
        'lk'   => 'Sri Lanka',
145
        'lr'   => 'Liberia',
146
        'ls'   => 'Lesotho',
147
        'lt'   => 'Lithuania',
148
        'lu'   => 'Luxembourg',
149
        'lv'   => 'Latvia',
150
        'ly'   => 'Libya',
151
        'ma'   => 'Morocco',
152
        'mc'   => 'Monaco',
153
        'md'   => 'Moldavia',
154
        'mg'   => 'Madagascar',
155
        'mh'   => 'Marshall Islands',
156
        'mil'  => 'USA Military',
157
        'mk'   => 'Macedonia',
158
        'ml'   => 'Mali',
159
        'mm'   => 'Myanmar',
160
        'mn'   => 'Mongolia',
161
        'mo'   => 'Macau',
162
        'mp'   => 'Northern Mariana Islands',
163
        'mq'   => 'Martinique (French)',
164
        'mr'   => 'Mauritania',
165
        'ms'   => 'Montserrat',
166
        'mt'   => 'Malta',
167
        'mu'   => 'Mauritius',
168
        'mv'   => 'Maldives',
169
        'mw'   => 'Malawi',
170
        'mx'   => 'Mexico',
171
        'my'   => 'Malaysia',
172
        'mz'   => 'Mozambique',
173
        'na'   => 'Namibia',
174
        'nato' => 'NATO (this was purged in 1996 - see hq.nato.int)',
175
        'nc'   => 'New Caledonia (French)',
176
        'ne'   => 'Niger',
177
        'net'  => 'Network',
178
        'nf'   => 'Norfolk Island',
179
        'ng'   => 'Nigeria',
180
        'ni'   => 'Nicaragua',
181
        'nl'   => 'Netherlands',
182
        'no'   => 'Norway',
183
        'np'   => 'Nepal',
184
        'nr'   => 'Nauru',
185
        'nt'   => 'Neutral Zone',
186
        'nu'   => 'Niue',
187
        'nz'   => 'New Zealand',
188
        'om'   => 'Oman',
189
        'org'  => 'Non-Profit Making Organisations (sic)',
190
        'pa'   => 'Panama',
191
        'pe'   => 'Peru',
192
        'pf'   => 'Polynesia (French)',
193
        'pg'   => 'Papua New Guinea',
194
        'ph'   => 'Philippines',
195
        'pk'   => 'Pakistan',
196
        'pl'   => 'Poland',
197
        'pm'   => 'Saint Pierre and Miquelon',
198
        'pn'   => 'Pitcairn Island',
199
        'pr'   => 'Puerto Rico',
200
        'pt'   => 'Portugal',
201
        'pw'   => 'Palau',
202
        'py'   => 'Paraguay',
203
        'qa'   => 'Qatar',
204
        're'   => 'Reunion (French)',
205
        'ro'   => 'Romania',
206
        'ru'   => 'Russian Federation',
207
        'rw'   => 'Rwanda',
208
        'sa'   => 'Saudi Arabia',
209
        'sb'   => 'Solomon Islands',
210
        'sc'   => 'Seychelles',
211
        'sd'   => 'Sudan',
212
        'se'   => 'Sweden',
213
        'sg'   => 'Singapore',
214
        'sh'   => 'Saint Helena',
215
        'si'   => 'Slovenia',
216
        'sj'   => 'Svalbard and Jan Mayen Islands',
217
        'sk'   => 'Slovak Republic',
218
        'sl'   => 'Sierra Leone',
219
        'sm'   => 'San Marino',
220
        'sn'   => 'Senegal',
221
        'so'   => 'Somalia',
222
        'sr'   => 'Suriname',
223
        'st'   => 'Saint Tome (Sao Tome) and Principe',
224
        'su'   => 'Former USSR',
225
        'sv'   => 'El Salvador',
226
        'sy'   => 'Syria',
227
        'sz'   => 'Swaziland',
228
        'tc'   => 'Turks and Caicos Islands',
229
        'td'   => 'Chad',
230
        'tf'   => 'French Southern Territories',
231
        'tg'   => 'Togo',
232
        'th'   => 'Thailand',
233
        'tj'   => 'Tadjikistan',
234
        'tk'   => 'Tokelau',
235
        'tm'   => 'Turkmenistan',
236
        'tn'   => 'Tunisia',
237
        'to'   => 'Tonga',
238
        'tp'   => 'East Timor',
239
        'tr'   => 'Turkey',
240
        'tt'   => 'Trinidad and Tobago',
241
        'tv'   => 'Tuvalu',
242
        'tw'   => 'Taiwan',
243
        'tz'   => 'Tanzania',
244
        'ua'   => 'Ukraine',
245
        'ug'   => 'Uganda',
246
        'uk'   => 'United Kingdom',
247
        'um'   => 'USA Minor Outlying Islands',
248
        'us'   => 'United States',
249
        'uy'   => 'Uruguay',
250
        'uz'   => 'Uzbekistan',
251
        'va'   => 'Holy See (Vatican City State)',
252
        'vc'   => 'Saint Vincent & Grenadines',
253
        've'   => 'Venezuela',
254
        'vg'   => 'Virgin Islands (British)',
255
        'vi'   => 'Virgin Islands (USA)',
256
        'vn'   => 'Vietnam',
257
        'vu'   => 'Vanuatu',
258
        'wf'   => 'Wallis and Futuna Islands',
259
        'ws'   => 'Samoa',
260
        'ye'   => 'Yemen',
261
        'yt'   => 'Mayotte',
262
        'yu'   => 'Yugoslavia',
263
        'za'   => 'South Africa',
264
        'zm'   => 'Zambia',
265
        'zr'   => 'Zaire',
266
        'zw'   => 'Zimbabwe',
267
    ];
268
269
    public function all()
270
    {
271
        return new Collection($this->all);
272
    }
273
274
    /**
275
     * Get the GeoIp instance.
0 ignored issues
show
Bug introduced by
The type PragmaRX\Firewall\Repositories\GeoIp was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
276
     *
277
     * @return GeoIp
278
     */
279
    public function getGeoIp()
280
    {
281
        return $this->geoIp();
282
    }
283
284
    public function isValid($cc)
285
    {
286
        $cc = strtolower(str_replace('country:', '', $cc));
287
288
        return $this->all()->has($cc);
289
    }
290
291
    /**
292
     * Get country code from an IP address.
293
     *
294
     * @param $ip_address
295
     *
296
     * @return bool|string
297
     */
298
    public function getCountryFromIp($ip_address)
299
    {
300
        if ($geo = $this->geoIp()->searchAddr($ip_address)) {
301
            return strtolower($geo['country_code']);
302
        }
303
304
        return false;
305
    }
306
307
    /**
308
     * Make a country info from a string.
309
     *
310
     * @param $country
311
     *
312
     * @return string
313
     */
314
    public function makeCountryFromString($country)
315
    {
316
        if ($ips = $this->ipAddress()->isCidr($country)) {
317
            $country = $ips[0];
318
        }
319
320
        if ($this->validCountry($country)) {
321
            return $country;
322
        }
323
324
        if ($this->dataRepository()->ipIsValid($country)) {
325
            $country = $this->getCountryFromIp($country);
326
        }
327
328
        return "country:{$country}";
329
    }
330
331
    /**
332
     * Set the data repository instance.
333
     *
334
     * @param \PragmaRX\Firewall\Repositories\DataRepository $dataRepository
335
     */
336
    public function setDataRepository($dataRepository)
337
    {
338
        $this->dataRepository = $dataRepository;
0 ignored issues
show
Bug Best Practice introduced by
The property dataRepository does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
339
    }
340
341
    /**
342
     * Check if a string is a valid country info.
343
     *
344
     * @param $country
345
     *
346
     * @return bool
347
     */
348
    public function validCountry($country)
349
    {
350
        $country = strtolower($country);
351
352
        if ($this->config()->get('enable_country_search')) {
353
            if (starts_with($country, 'country:') && $this->isValid($country)) {
354
                return true;
355
            }
356
        }
357
358
        return false;
359
    }
360
}
361