Skip to content

Commit 907ddd2

Browse files
authored
Merge pull request #138 from maxmind/greg/network
Add network support
2 parents eac52bc + 9cf4f40 commit 907ddd2

File tree

17 files changed

+225
-27
lines changed

17 files changed

+225
-27
lines changed

.travis.yml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
sudo: false
33
language: php
44
dist: trusty
5+
56
matrix:
67
include:
7-
- php: '5.4'
8-
- php: '5.5'
98
- php: '5.6'
109
- php: '7.0'
1110
- php: '7.1'
@@ -14,22 +13,32 @@ matrix:
1413
env:
1514
- RUN_LINTER=1
1615
- RUN_SNYK=1
17-
- php: nightly
16+
- php: '7.4snapshot'
17+
- php: 'master'
18+
- php: 'nightly'
19+
fast_finish: true
1820
allow_failures:
19-
- php: nightly
21+
- php: '7.4snapshot'
22+
- php: 'master'
23+
- php: 'nightly'
24+
2025
before_install:
2126
- composer install --dev -n --prefer-source
2227
- phpenv rehash
2328
- "if [[ $RUN_SNYK && $SNYK_TOKEN ]]; then sudo apt-get install -y nodejs; npm install -g snyk; fi"
29+
2430
install:
2531
- "if [[ $RUN_SNYK && $SNYK_TOKEN ]]; then snyk test --org=maxmind; fi"
32+
2633
script:
2734
- mkdir -p build/logs
2835
- "if [[ $RUN_LINTER ]]; then vendor/bin/php-cs-fixer fix --verbose --diff --dry-run --config=.php_cs; fi"
2936
- "if [[ $RUN_LINTER ]]; then vendor/bin/phpcs --standard=PSR2 src/; fi"
3037
- vendor/bin/phpunit -c phpunit.xml.dist
38+
3139
after_script:
3240
- php vendor/bin/coveralls
41+
3342
after_success:
3443
- "if [[ $RUN_SNYK && $SNYK_TOKEN && $TRAVIS_BRANCH == 'master' && $TRAVIS_PULL_REQUEST == 'false' ]]; then snyk monitor --org=maxmind --project-name=maxmind/GeoIP2-php; fi"
3544
notifications:

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
CHANGELOG
22
=========
33

4-
2.9.1
4+
2.10.0
55
------------------
66

7+
* PHP 5.6 or greater is now required.
8+
* The `network` property was added to `GeoIp2\Record\Traits`,
9+
`GeoIp2\Model\AnonymousIp`, `GeoIp2\Model\Asn`,
10+
`GeoIp2\Model\ConnectionType`, `Geoip2\Model\Domain`,
11+
and `GeoIp2\Model\Isp`. This is a string in CIDR format representing the
12+
largest network where all of the properties besides `ipAddress` have the
13+
same value.
714
* Updated documentation of anonymizer properties - `isAnonymousVpn`
815
and `isHostingProvider` - to be more descriptive.
916

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ print($record->postal->code . "\n"); // '55455'
140140
print($record->location->latitude . "\n"); // 44.9733
141141
print($record->location->longitude . "\n"); // -93.2323
142142

143+
print($record->traits->network . "\n"); // '128.101.101.101/32'
144+
143145
```
144146

145147
### Anonymous IP Example ###
@@ -157,6 +159,7 @@ $record = $reader->anonymousIp('128.101.101.101');
157159

158160
if ($record->isAnonymous) { print "anon\n"; }
159161
print($record->ipAddress . "\n"); // '128.101.101.101'
162+
print($record->network . "\n"); // '128.101.101.101/32'
160163

161164
```
162165

@@ -175,6 +178,7 @@ $record = $reader->connectionType('128.101.101.101');
175178

176179
print($record->connectionType . "\n"); // 'Corporate'
177180
print($record->ipAddress . "\n"); // '128.101.101.101'
181+
print($record->network . "\n"); // '128.101.101.101/32'
178182

179183
```
180184

@@ -193,6 +197,7 @@ $record = $reader->domain('128.101.101.101');
193197

194198
print($record->domain . "\n"); // 'umn.edu'
195199
print($record->ipAddress . "\n"); // '128.101.101.101'
200+
print($record->network . "\n"); // '128.101.101.101/32'
196201

197202
```
198203

@@ -228,6 +233,8 @@ print($record->location->accuracyRadius . "\n"); // 50
228233
print($record->location->latitude . "\n"); // 44.9733
229234
print($record->location->longitude . "\n"); // -93.2323
230235

236+
print($record->traits->network . "\n"); // '128.101.101.101/32'
237+
231238
```
232239

233240
### ISP Example ###
@@ -249,6 +256,7 @@ print($record->isp . "\n"); // 'University of Minnesota'
249256
print($record->organization . "\n"); // 'University of Minnesota'
250257

251258
print($record->ipAddress . "\n"); // '128.101.101.101'
259+
print($record->network . "\n"); // '128.101.101.101/32'
252260

253261
```
254262

@@ -299,6 +307,8 @@ print($record->postal->code . "\n"); // '55455'
299307
print($record->location->latitude . "\n"); // 44.9733
300308
print($record->location->longitude . "\n"); // -93.2323
301309

310+
print($record->traits->network . "\n"); // '128.101.101.101/32'
311+
302312
```
303313

304314
## Values to use for Database or Array Keys ##
@@ -376,7 +386,7 @@ to the client API, please see
376386

377387
## Requirements ##
378388

379-
This library requires PHP 5.4 or greater.
389+
This library requires PHP 5.6 or greater.
380390

381391
This library also relies on the [MaxMind DB Reader](https://github.com/maxmind/MaxMind-DB-Reader-php).
382392

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
}
1414
],
1515
"require": {
16-
"maxmind-db/reader": "~1.0",
16+
"maxmind-db/reader": "~1.5",
1717
"maxmind/web-service-common": "~0.5",
18-
"php": ">=5.4"
18+
"php": ">=5.6",
19+
"ext-json": "*"
1920
},
2021
"require-dev": {
2122
"friendsofphp/php-cs-fixer": "2.*",

examples/benchmark.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
require __DIR__ . '/../vendor/autoload.php';
4+
5+
use GeoIp2\Database\Reader;
6+
7+
srand(0);
8+
9+
$reader = new Reader('GeoIP2-City.mmdb');
10+
$count = 500000;
11+
$startTime = microtime(true);
12+
for ($i = 0; $i < $count; ++$i) {
13+
$ip = long2ip(rand(0, pow(2, 32) - 1));
14+
try {
15+
$t = $reader->city($ip);
16+
} catch (\GeoIp2\Exception\AddressNotFoundException $e) {
17+
}
18+
if ($i % 10000 === 0) {
19+
echo $i . ' ' . $ip . "\n";
20+
}
21+
}
22+
$endTime = microtime(true);
23+
24+
$duration = $endTime - $startTime;
25+
echo 'Requests per second: ' . $count / $duration . "\n";

src/Database/Reader.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
class Reader implements ProviderInterface
3535
{
3636
private $dbReader;
37+
private $dbType;
3738
private $locales;
3839

3940
/**
@@ -51,6 +52,7 @@ public function __construct(
5152
$locales = ['en']
5253
) {
5354
$this->dbReader = new DbReader($filename);
55+
$this->dbType = $this->dbReader->metadata()->databaseType;
5456
$this->locales = $locales;
5557
}
5658

@@ -212,34 +214,36 @@ public function isp($ipAddress)
212214

213215
private function modelFor($class, $type, $ipAddress)
214216
{
215-
$record = $this->getRecord($class, $type, $ipAddress);
217+
list($record, $prefixLen) = $this->getRecord($class, $type, $ipAddress);
216218

217219
$record['traits']['ip_address'] = $ipAddress;
220+
$record['traits']['prefix_len'] = $prefixLen;
221+
218222
$class = 'GeoIp2\\Model\\' . $class;
219223

220224
return new $class($record, $this->locales);
221225
}
222226

223227
private function flatModelFor($class, $type, $ipAddress)
224228
{
225-
$record = $this->getRecord($class, $type, $ipAddress);
229+
list($record, $prefixLen) = $this->getRecord($class, $type, $ipAddress);
226230

227231
$record['ip_address'] = $ipAddress;
232+
$record['prefix_len'] = $prefixLen;
228233
$class = 'GeoIp2\\Model\\' . $class;
229234

230235
return new $class($record);
231236
}
232237

233238
private function getRecord($class, $type, $ipAddress)
234239
{
235-
if (strpos($this->metadata()->databaseType, $type) === false) {
240+
if (strpos($this->dbType, $type) === false) {
236241
$method = lcfirst($class);
237242
throw new \BadMethodCallException(
238-
"The $method method cannot be used to open a "
239-
. $this->metadata()->databaseType . ' database'
243+
"The $method method cannot be used to open a {$this->dbType} database"
240244
);
241245
}
242-
$record = $this->dbReader->get($ipAddress);
246+
list($record, $prefixLen) = $this->dbReader->getWithPrefixLen($ipAddress);
243247
if ($record === null) {
244248
throw new AddressNotFoundException(
245249
"The address $ipAddress is not in the database."
@@ -259,7 +263,7 @@ private function getRecord($class, $type, $ipAddress)
259263
);
260264
}
261265

262-
return $record;
266+
return [$record, $prefixLen];
263267
}
264268

265269
/**

src/Model/AnonymousIp.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace GeoIp2\Model;
44

5+
use GeoIp2\Util;
6+
57
/**
68
* This class provides the GeoIP2 Anonymous IP model.
79
*
@@ -19,6 +21,9 @@
1921
* exit node.
2022
* @property-read string $ipAddress The IP address that the data in the model is
2123
* for.
24+
* @property-read string $network The network in CIDR notation associated with
25+
* the record. In particular, this is the largest network where all of the
26+
* fields besides $ipAddress have the same value.
2227
*/
2328
class AnonymousIp extends AbstractModel
2429
{
@@ -28,6 +33,7 @@ class AnonymousIp extends AbstractModel
2833
protected $isPublicProxy;
2934
protected $isTorExitNode;
3035
protected $ipAddress;
36+
protected $network;
3137

3238
/**
3339
* @ignore
@@ -43,6 +49,8 @@ public function __construct($raw)
4349
$this->isHostingProvider = $this->get('is_hosting_provider');
4450
$this->isPublicProxy = $this->get('is_public_proxy');
4551
$this->isTorExitNode = $this->get('is_tor_exit_node');
46-
$this->ipAddress = $this->get('ip_address');
52+
$ipAddress = $this->get('ip_address');
53+
$this->ipAddress = $ipAddress;
54+
$this->network = Util::cidr($ipAddress, $this->get('prefix_len'));
4755
}
4856
}

src/Model/Asn.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace GeoIp2\Model;
44

5+
use GeoIp2\Util;
6+
57
/**
68
* This class provides the GeoLite2 ASN model.
79
*
@@ -12,12 +14,16 @@
1214
* address.
1315
* @property-read string $ipAddress The IP address that the data in the model is
1416
* for.
17+
* @property-read string $network The network in CIDR notation associated with
18+
* the record. In particular, this is the largest network where all of the
19+
* fields besides $ipAddress have the same value.
1520
*/
1621
class Asn extends AbstractModel
1722
{
1823
protected $autonomousSystemNumber;
1924
protected $autonomousSystemOrganization;
2025
protected $ipAddress;
26+
protected $network;
2127

2228
/**
2329
* @ignore
@@ -30,6 +36,8 @@ public function __construct($raw)
3036
$this->autonomousSystemNumber = $this->get('autonomous_system_number');
3137
$this->autonomousSystemOrganization =
3238
$this->get('autonomous_system_organization');
33-
$this->ipAddress = $this->get('ip_address');
39+
$ipAddress = $this->get('ip_address');
40+
$this->ipAddress = $ipAddress;
41+
$this->network = Util::cidr($ipAddress, $this->get('prefix_len'));
3442
}
3543
}

src/Model/ConnectionType.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace GeoIp2\Model;
44

5+
use GeoIp2\Util;
6+
57
/**
68
* This class provides the GeoIP2 Connection-Type model.
79
*
@@ -10,11 +12,15 @@
1012
* Additional values may be added in the future.
1113
* @property-read string $ipAddress The IP address that the data in the model is
1214
* for.
15+
* @property-read string $network The network in CIDR notation associated with
16+
* the record. In particular, this is the largest network where all of the
17+
* fields besides $ipAddress have the same value.
1318
*/
1419
class ConnectionType extends AbstractModel
1520
{
1621
protected $connectionType;
1722
protected $ipAddress;
23+
protected $network;
1824

1925
/**
2026
* @ignore
@@ -26,6 +32,8 @@ public function __construct($raw)
2632
parent::__construct($raw);
2733

2834
$this->connectionType = $this->get('connection_type');
29-
$this->ipAddress = $this->get('ip_address');
35+
$ipAddress = $this->get('ip_address');
36+
$this->ipAddress = $ipAddress;
37+
$this->network = Util::cidr($ipAddress, $this->get('prefix_len'));
3038
}
3139
}

src/Model/Domain.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace GeoIp2\Model;
44

5+
use GeoIp2\Util;
6+
57
/**
68
* This class provides the GeoIP2 Domain model.
79
*
@@ -10,11 +12,15 @@
1012
* "example.co.uk", not "foo.example.com".
1113
* @property-read string $ipAddress The IP address that the data in the model is
1214
* for.
15+
* @property-read string $network The network in CIDR notation associated with
16+
* the record. In particular, this is the largest network where all of the
17+
* fields besides $ipAddress have the same value.
1318
*/
1419
class Domain extends AbstractModel
1520
{
1621
protected $domain;
1722
protected $ipAddress;
23+
protected $network;
1824

1925
/**
2026
* @ignore
@@ -26,6 +32,8 @@ public function __construct($raw)
2632
parent::__construct($raw);
2733

2834
$this->domain = $this->get('domain');
29-
$this->ipAddress = $this->get('ip_address');
35+
$ipAddress = $this->get('ip_address');
36+
$this->ipAddress = $ipAddress;
37+
$this->network = Util::cidr($ipAddress, $this->get('prefix_len'));
3038
}
3139
}

0 commit comments

Comments
 (0)