Passed
Push — master ( e8f6e3...bf4671 )
by Antonio Carlos
21:13 queued 14:51
created

Countries::getCountryFromIp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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