Skip to content

Commit 49df42b

Browse files
committed
- Update to Laravel 5
- Support IPv6 - Log address not found exceptions - Supports a custom default location
1 parent a197fcf commit 49df42b

File tree

6 files changed

+170
-88
lines changed

6 files changed

+170
-88
lines changed

README.md

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
# GeoIP for Laravel 4
1+
# GeoIP for Laravel 5
22

33
[![Latest Stable Version](https://poser.pugx.org/torann/geoip/v/stable.png)](https://packagist.org/packages/torann/geoip) [![Total Downloads](https://poser.pugx.org/torann/geoip/downloads.png)](https://packagist.org/packages/torann/geoip)
44

5-
Determine the geographical location of website visitors based on their IP addresses.
5+
Determine the geographical location of website visitors based on their IP addresses. [Homepage](http://lyften.com/projects/laravel-geoip/)
66

77
----------
88

99
## Installation
1010

11-
- [GeoIP for Laravel 4 on Packagist](https://packagist.org/packages/torann/geoip)
12-
- [GeoIP for Laravel 4 on GitHub](https://github.com/torann/laravel-hashids)
11+
- [GeoIP for Laravel 5 on Packagist](https://packagist.org/packages/torann/geoip)
12+
- [GeoIP for Laravel 5 on GitHub](https://github.com/torann/laravel-hashids)
13+
- [Laravel 4 Installation](https://github.com/Torann/laravel-4-geoip/tree/0.1.1)
1314

1415

1516
To get the latest version of GeoIP simply require it in your `composer.json` file.
1617

1718
~~~
18-
"torann/geoip": "0.1.*@dev"
19+
"torann/geoip": "0.2.*@dev"
1920
~~~
2021

2122
You'll then need to run `composer install` to download it and have the autoloader updated.
2223

23-
Once GeoIP is installed you need to register the service provider with the application. Open up `app/config/app.php` and find the `providers` key.
24+
Once GeoIP is installed you need to register the service provider with the application. Open up `config/app.php` and find the `providers` key.
2425

2526
~~~php
2627
'providers' => array(
@@ -30,7 +31,7 @@ Once GeoIP is installed you need to register the service provider with the appli
3031
)
3132
~~~
3233

33-
GeoIP also ships with a facade which provides the static syntax for creating collections. You can register the facade in the `aliases` key of your `app/config/app.php` file.
34+
GeoIP also ships with a facade which provides the static syntax for creating collections. You can register the facade in the `aliases` key of your `config/app.php` file.
3435

3536
~~~php
3637
'aliases' => array(
@@ -40,12 +41,17 @@ GeoIP also ships with a facade which provides the static syntax for creating col
4041
)
4142
~~~
4243

43-
Create configuration file using artisan
44+
### Publish the configurations
45+
46+
Run this on the command line from the root of your project:
4447

4548
~~~
46-
$ php artisan config:publish torann/geoip
49+
$ php artisan vendor:publish
4750
~~~
4851

52+
A configuration file will be publish to `config/geoip.php`
53+
54+
4955
## Usage
5056

5157
Get the location data for a website visitor:
@@ -59,7 +65,7 @@ $location = GeoIP::getLocation();
5965
Getting the location data for a given IP:
6066

6167
```php
62-
$location = GeoIP::getLocation( '232.223.11.11' );
68+
$location = GeoIP::getLocation('232.223.11.11');
6369
```
6470

6571
### Example Data
@@ -80,15 +86,22 @@ array (
8086
);
8187
```
8288

83-
#### Note
89+
#### Default Location
8490

85-
In the case that a location is not found the fallback location will be returned with the `default` parameter set to `true`. In a future release I'll make the default location customizable. For not it is New Haven, CT.
91+
In the case that a location is not found the fallback location will be returned with the `default` parameter set to `true`. To set your own default change it in the configurations `config/geoip.php`
8692

8793
## Services
8894

89-
9095
### [MaxMind](http://www.maxmind.com)
9196

92-
- **Database Service**: To use the database version of MaxMind services download the `GeoLite2-City.mmdb` from [http://dev.maxmind.com/geoip/geoip2/geolite2/](http://dev.maxmind.com/geoip/geoip2/geolite2/) and extract it to `/app/database/maxmind/`. And that's it.
97+
- **Database Service**: To use the database version of MaxMind services download the `GeoLite2-City.mmdb` from [http://dev.maxmind.com/geoip/geoip2/geolite2/](http://dev.maxmind.com/geoip/geoip2/geolite2/) and extract it to `database/maxmind/`. And that's it.
98+
99+
## Change Log
100+
101+
#### v0.2.0
93102

103+
- Update to Laravel 5
104+
- Support IPv6
105+
- Log address not found exceptions
106+
- Supports a custom default location
94107

composer.json

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "torann/geoip",
33
"description": "Supports the two main GeoIP services (infoDB and Maxmind).",
4-
"keywords": ["laravel", "laravel4", "geoip", "location", "geolocation"],
4+
"keywords": ["laravel", "laravel 5", "geoip", "location", "geolocation"],
55
"license": "BSD 2-Clause",
66
"authors": [
77
{
@@ -11,18 +11,17 @@
1111
],
1212
"require": {
1313
"php": ">=5.3.0",
14-
"illuminate/support": "~4.1",
15-
"geoip2/geoip2": "0.6.*"
14+
"illuminate/support": "~5.0",
15+
"geoip2/geoip2": "2.1.1"
1616
},
1717
"autoload": {
18-
"psr-0": {
19-
"Torann\\GeoIP": "src/"
18+
"psr-4": {
19+
"Torann\\GeoIP\\": "src/Torann/GeoIP"
2020
}
2121
},
2222
"extra": {
2323
"branch-alias": {
24-
"dev-master": "0.1-dev"
24+
"dev-master": "0.2-dev"
2525
}
26-
},
27-
"minimum-stability": "dev"
26+
}
2827
}

src/Torann/GeoIP/GeoIP.php

Lines changed: 80 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
use GeoIp2\Database\Reader;
44
use GeoIp2\WebService\Client;
55

6+
use Monolog\Logger;
7+
use Monolog\Handler\StreamHandler;
8+
69
use GeoIp2\Exception\AddressNotFoundException;
710

811
use Illuminate\Config\Repository;
@@ -84,6 +87,13 @@ public function __construct(Repository $config, SessionStore $session)
8487
$this->config = $config;
8588
$this->session = $session;
8689

90+
// Set custom default location
91+
$this->default_location = array_merge(
92+
$this->default_location,
93+
$this->config->get('geoip.default_location', array())
94+
);
95+
96+
// Set IP
8797
$this->remote_ip = $this->default_location['ip'] = $this->getClientIP();
8898
}
8999

@@ -121,6 +131,7 @@ function getLocation($ip = null )
121131
*
122132
* @param string $ip Optional
123133
* @return array
134+
* @throws \Exception
124135
*/
125136
private function find($ip = null)
126137
{
@@ -136,10 +147,16 @@ private function find($ip = null)
136147
}
137148

138149
// Check if the ip is not local or empty
139-
if($this->checkIp($ip)) {
150+
if($this->checkIp($ip))
151+
{
152+
// Get service name
153+
$service = 'locate_'.$this->config->get('geoip.service');
140154

141-
// Call default service
142-
$service = 'locate_'.$this->config->get('geoip::service');
155+
// Check for valid service
156+
if (! method_exists($this, $service))
157+
{
158+
throw new \Exception("GeoIP Service not support or setup.");
159+
}
143160

144161
return $this->$service($ip);
145162
}
@@ -155,41 +172,46 @@ private function find($ip = null)
155172
*/
156173
private function locate_maxmind($ip)
157174
{
158-
$settings = $this->config->get('geoip::maxmind');
175+
$settings = $this->config->get('geoip.maxmind');
159176

160177
if($settings['type'] === 'web_service') {
161178
$maxmind = new Client($settings['user_id'], $settings['license_key']);
162179
}
163180
else {
164-
$maxmind = new Reader(app_path().'/database/maxmind/GeoLite2-City.mmdb');
165-
}
166-
167-
// Attempt to get location
168-
try {
169-
$record = $maxmind->city($ip);
170-
}
171-
catch(AddressNotFoundException $e)
172-
{
173-
return $this->default_location;
181+
$maxmind = new Reader(base_path().'/database/maxmind/GeoLite2-City.mmdb');
174182
}
175183

176-
$location = array(
177-
"ip" => $ip,
178-
"isoCode" => $record->country->isoCode,
179-
"country" => $record->country->name,
180-
"city" => $record->city->name,
181-
"state" => $record->mostSpecificSubdivision->isoCode,
182-
"postal_code" => $record->postal->code,
183-
"lat" => $record->location->latitude,
184-
"lon" => $record->location->longitude,
185-
"timezone" => $record->location->timeZone,
186-
"continent" => $record->continent->code,
187-
"default" => false
188-
);
189-
190-
unset($record);
191-
192-
return $location;
184+
try {
185+
$record = $maxmind->city($ip);
186+
187+
$location = array(
188+
"ip" => $ip,
189+
"isoCode" => $record->country->isoCode,
190+
"country" => $record->country->name,
191+
"city" => $record->city->name,
192+
"state" => $record->mostSpecificSubdivision->isoCode,
193+
"postal_code" => $record->postal->code,
194+
"lat" => $record->location->latitude,
195+
"lon" => $record->location->longitude,
196+
"timezone" => $record->location->timeZone,
197+
"continent" => $record->continent->code,
198+
"default" => false
199+
);
200+
}
201+
catch (AddressNotFoundException $e)
202+
{
203+
$location = $this->default_location;
204+
205+
$logFile = 'geoip';
206+
207+
$log = new Logger($logFile);
208+
$log->pushHandler(new StreamHandler(storage_path()."/logs/{$logFile}.log", Logger::ERROR));
209+
$log->addError($e);
210+
}
211+
212+
unset($record);
213+
214+
return $location;
193215
}
194216

195217
/**
@@ -234,24 +256,33 @@ private function getClientIP()
234256
*/
235257
private function checkIp($ip)
236258
{
237-
$longip = ip2long($ip);
238-
239-
if (!empty($ip)) {
240-
241-
foreach ($this->reserved_ips as $r)
242-
{
243-
$min = ip2long($r[0]);
244-
$max = ip2long($r[1]);
245-
246-
if ($longip >= $min && $longip <= $max) {
247-
return false;
248-
}
249-
}
250-
251-
return true;
252-
}
253-
254-
return false;
259+
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4))
260+
{
261+
$longip = ip2long($ip);
262+
263+
if (! empty($ip))
264+
{
265+
foreach ($this->reserved_ips as $r)
266+
{
267+
$min = ip2long($r[0]);
268+
$max = ip2long($r[1]);
269+
270+
if ($longip >= $min && $longip <= $max)
271+
{
272+
return false;
273+
}
274+
}
275+
276+
return true;
277+
}
278+
}
279+
280+
else if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6))
281+
{
282+
return true;
283+
}
284+
285+
return false;
255286
}
256287

257288
}

src/Torann/GeoIP/GeoIPServiceProvider.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ class GeoIPServiceProvider extends ServiceProvider {
1818
*/
1919
public function boot()
2020
{
21-
$this->package('torann/geoip');
22-
23-
// Temp to use in closure.
24-
$app = $this->app;
21+
$this->publishes([
22+
__DIR__.'/../../config/geoip.php' => config_path('geoip.php'),
23+
]);
2524
}
2625

2726
/**

src/config/config.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/config/geoip.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
return array(
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Service
8+
|--------------------------------------------------------------------------
9+
|
10+
| Current only supports 'maxmind'.
11+
|
12+
*/
13+
14+
'service' => 'maxmind',
15+
16+
/*
17+
|--------------------------------------------------------------------------
18+
| Services settings
19+
|--------------------------------------------------------------------------
20+
|
21+
| Service specific settings.
22+
|
23+
*/
24+
25+
'maxmind' => array(
26+
'type' => 'database', // database or web_service
27+
'user_id' => env('GEOIP_USER_ID'),
28+
'license_key' => env('GEOIP_LICENSE_KEY')
29+
),
30+
31+
/*
32+
|--------------------------------------------------------------------------
33+
| Default Location
34+
|--------------------------------------------------------------------------
35+
|
36+
| Return when a location is not found.
37+
|
38+
*/
39+
40+
'default_location' => array (
41+
"ip" => "127.0.0.0",
42+
"isoCode" => "US",
43+
"country" => "United States",
44+
"city" => "New Haven",
45+
"state" => "CT",
46+
"postal_code" => "06510",
47+
"lat" => 41.31,
48+
"lon" => -72.92,
49+
"timezone" => "America/New_York",
50+
"continent" => "NA"
51+
),
52+
53+
);

0 commit comments

Comments
 (0)