Compare commits
32 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
01f58d749b | ||
|
|
64f2262093 | ||
|
|
8ef95cb4d1 | ||
|
|
076213aedb | ||
|
|
1546532b39 | ||
|
|
f1fe6587b4 | ||
|
|
2d6d43b62c | ||
|
|
09cf864045 | ||
|
|
4ae7983706 | ||
|
|
3142e44b3e | ||
|
|
4bdb050a50 | ||
|
|
c44053c276 | ||
|
|
caf91dd247 | ||
|
|
ffc6493c8d | ||
|
|
e5f61fd275 | ||
|
|
6859340968 | ||
|
|
970d1dba45 | ||
|
|
9e6449290f | ||
|
|
0c6ba34623 | ||
|
|
fbf4583b3e | ||
|
|
18686e11ac | ||
|
|
00a520f3c5 | ||
|
|
6cd0863499 | ||
|
|
5407b4f7c6 | ||
|
|
c58034524e | ||
|
|
94f4f9d5e6 | ||
|
|
dac9e94304 | ||
|
|
51e2f84d07 | ||
|
|
42efc47796 | ||
|
|
36624ae87a | ||
|
|
08697f2e7b | ||
|
|
2f9746c32b |
5
.gitignore
vendored
5
.gitignore
vendored
@@ -1,13 +1,14 @@
|
|||||||
_site
|
_site
|
||||||
.gh-pages
|
.gh-pages
|
||||||
.idea
|
.idea
|
||||||
|
GeoLite2-City.mmdb
|
||||||
|
apigen.phar
|
||||||
|
box.phar
|
||||||
build
|
build
|
||||||
composer.lock
|
composer.lock
|
||||||
composer.phar
|
composer.phar
|
||||||
phpunit.xml
|
phpunit.xml
|
||||||
geoip2.phar
|
|
||||||
geoip2-php.sublime-*
|
geoip2-php.sublime-*
|
||||||
vendor/
|
vendor/
|
||||||
*.sw?
|
*.sw?
|
||||||
t.php
|
|
||||||
*.old
|
*.old
|
||||||
|
|||||||
41
CHANGELOG.md
41
CHANGELOG.md
@@ -1,11 +1,46 @@
|
|||||||
CHANGELOG
|
CHANGELOG
|
||||||
=========
|
=========
|
||||||
|
|
||||||
|
2.1.1 (2014-12-03)
|
||||||
|
------------------
|
||||||
|
|
||||||
|
* The 2.1.0 Phar builds included a shebang line, causing issues when loading
|
||||||
|
it as a library. This has been corrected. GitHub #33.
|
||||||
|
|
||||||
|
2.1.0 (2014-10-29)
|
||||||
|
------------------
|
||||||
|
|
||||||
|
* Update ApiGen dependency to version that isn't broken on case sensitive
|
||||||
|
file systems.
|
||||||
|
* Added support for the GeoIP2 Anonymous IP database. The
|
||||||
|
`GeoIP2\Database\Reader` class now has an `anonymousIp` method which returns
|
||||||
|
a `GeoIP2\Model\AnonymousIp` object.
|
||||||
|
* Boolean attributes like those in the `GeoIP2\Record\Traits` class now return
|
||||||
|
`false` instead of `null` when they were not true.
|
||||||
|
|
||||||
|
2.0.0 (2014-09-22)
|
||||||
|
------------------
|
||||||
|
|
||||||
|
* First production release.
|
||||||
|
|
||||||
|
0.9.0 (2014-09-15)
|
||||||
|
------------------
|
||||||
|
|
||||||
|
* IMPORTANT: The deprecated `omni()` and `cityIspOrg()` methods have been
|
||||||
|
removed from `GeoIp2\WebService\Client`.
|
||||||
|
|
||||||
|
0.8.1 (2014-09-12)
|
||||||
|
------------------
|
||||||
|
|
||||||
|
* The check added to the `GeoIP2\Database\Reader` lookup methods in 0.8.0 did
|
||||||
|
not work with the GeoIP2 City Database Subset by Continent with World
|
||||||
|
Countries. This has been fixed. Fixes GitHub issue #23.
|
||||||
|
|
||||||
0.8.0 (2014-09-10)
|
0.8.0 (2014-09-10)
|
||||||
------------------
|
------------------
|
||||||
|
|
||||||
* The `GeoIP2\Database\Reader` lookup methods (e.g., `city()`, `isp()`) now
|
* The `GeoIp2\Database\Reader` lookup methods (e.g., `city()`, `isp()`) now
|
||||||
throw an `BadMethodCallException` if they are used with a database that
|
throw a `BadMethodCallException` if they are used with a database that
|
||||||
does not match the method. In particular, doing a `city()` lookup on a
|
does not match the method. In particular, doing a `city()` lookup on a
|
||||||
GeoIP2 Country database will result in an exception, and vice versa.
|
GeoIP2 Country database will result in an exception, and vice versa.
|
||||||
* A `metadata()` method has been added to the `GeoIP2\Database\Reader` class.
|
* A `metadata()` method has been added to the `GeoIP2\Database\Reader` class.
|
||||||
@@ -18,7 +53,7 @@ CHANGELOG
|
|||||||
|
|
||||||
* The web service client API has been updated for the v2.1 release of the web
|
* The web service client API has been updated for the v2.1 release of the web
|
||||||
service. In particular, the `cityIspOrg` and `omni` methods on
|
service. In particular, the `cityIspOrg` and `omni` methods on
|
||||||
`GeoIP2\WebService\Client` should be considered deprecated. The `city`
|
`GeoIp2\WebService\Client` should be considered deprecated. The `city`
|
||||||
method now provides all of the data formerly provided by `cityIspOrg`, and
|
method now provides all of the data formerly provided by `cityIspOrg`, and
|
||||||
the `omni` method has been replaced by the `insights` method.
|
the `omni` method has been replaced by the `insights` method.
|
||||||
* Support was added for GeoIP2 Connection Type, Domain and ISP databases.
|
* Support was added for GeoIP2 Connection Type, Domain and ISP databases.
|
||||||
|
|||||||
23
README.md
23
README.md
@@ -26,7 +26,7 @@ You should now have the file `composer.phar` in your project directory.
|
|||||||
Run in your project root:
|
Run in your project root:
|
||||||
|
|
||||||
```
|
```
|
||||||
php composer.phar require geoip2/geoip2:~0.8.0
|
php composer.phar require geoip2/geoip2:~2.0
|
||||||
```
|
```
|
||||||
|
|
||||||
You should now have the files `composer.json` and `composer.lock` as well as
|
You should now have the files `composer.json` and `composer.lock` as well as
|
||||||
@@ -114,6 +114,24 @@ print($record->location->longitude . "\n"); // -93.2323
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Anonymous IP Example ###
|
||||||
|
|
||||||
|
```php
|
||||||
|
<?php
|
||||||
|
require_once 'vendor/autoload.php';
|
||||||
|
use GeoIp2\Database\Reader;
|
||||||
|
|
||||||
|
// This creates the Reader object, which should be reused across
|
||||||
|
// lookups.
|
||||||
|
$reader = new Reader('/usr/local/share/GeoIP/GeoIP2-Anonymous-IP.mmdb');
|
||||||
|
|
||||||
|
$record = $reader->anonymousIp('128.101.101.101');
|
||||||
|
|
||||||
|
if ($record->isAnonymous) { print "anon\n"; }
|
||||||
|
print($record->ipAddress . "\n"); // '128.101.101.101'
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
### Connection-Type Example ###
|
### Connection-Type Example ###
|
||||||
|
|
||||||
```php
|
```php
|
||||||
@@ -238,9 +256,6 @@ for details on what data each end point may return.
|
|||||||
The only piece of data which is always returned is the `ipAddress`
|
The only piece of data which is always returned is the `ipAddress`
|
||||||
attribute in the `GeoIp2\Record\Traits` record.
|
attribute in the `GeoIp2\Record\Traits` record.
|
||||||
|
|
||||||
Every record class attribute has a corresponding predicate method so you can
|
|
||||||
check to see if the attribute is set.
|
|
||||||
|
|
||||||
## Integration with GeoNames ##
|
## Integration with GeoNames ##
|
||||||
|
|
||||||
[GeoNames](http://www.geonames.org/) offers web services and downloadable
|
[GeoNames](http://www.geonames.org/) offers web services and downloadable
|
||||||
|
|||||||
26
bin/benchmark.php
Executable file
26
bin/benchmark.php
Executable file
@@ -0,0 +1,26 @@
|
|||||||
|
#!/usr/bin/env php
|
||||||
|
|
||||||
|
<?php
|
||||||
|
|
||||||
|
require_once '../vendor/autoload.php';
|
||||||
|
|
||||||
|
use GeoIp2\Database\Reader;
|
||||||
|
use GeoIp2\Exception\AddressNotFoundException;
|
||||||
|
|
||||||
|
$reader = new Reader('GeoLite2-City.mmdb');
|
||||||
|
$count = 40000;
|
||||||
|
$startTime = microtime(true);
|
||||||
|
for ($i = 0; $i < $count; $i++) {
|
||||||
|
$ip = long2ip(rand(0, pow(2, 32) -1));
|
||||||
|
try {
|
||||||
|
$t = $reader->city($ip);
|
||||||
|
} catch (AddressNotFoundException $e) {
|
||||||
|
}
|
||||||
|
if ($i % 1000 == 0) {
|
||||||
|
print($i . ' ' . $ip . "\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$endTime = microtime(true);
|
||||||
|
|
||||||
|
$duration = $endTime - $startTime;
|
||||||
|
print('Requests per second: ' . $count / $duration . "\n");
|
||||||
@@ -1,63 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
TAG=$1
|
|
||||||
|
|
||||||
if [ -z $TAG ]; then
|
|
||||||
echo "Please specify a tag"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -n "$(git status --porcelain)" ]; then
|
|
||||||
echo ". is not clean." >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ ! -d .gh-pages ]; then
|
|
||||||
echo "Checking out gh-pages in .gh-pages"
|
|
||||||
git clone -b gh-pages git@git.maxmind.com:GeoIP2-php .gh-pages
|
|
||||||
cd .gh-pages
|
|
||||||
else
|
|
||||||
echo "Updating .gh-pages"
|
|
||||||
cd .gh-pages
|
|
||||||
git pull
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -n "$(git status --porcelain)" ]; then
|
|
||||||
echo ".gh-pages is not clean" >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
../vendor/bin/apigen --quiet --download --title "GeoIP2 PHP API $TAG" --source ../src --destination doc/$TAG
|
|
||||||
|
|
||||||
PAGE=index.md
|
|
||||||
cat <<EOF > $PAGE
|
|
||||||
---
|
|
||||||
layout: default
|
|
||||||
title: MaxMind GeoIP2 PHP API
|
|
||||||
language: php
|
|
||||||
version: $TAG
|
|
||||||
---
|
|
||||||
|
|
||||||
EOF
|
|
||||||
|
|
||||||
cat ../README.md >> $PAGE
|
|
||||||
|
|
||||||
git add doc/
|
|
||||||
git commit -m "Updated for $TAG" -a
|
|
||||||
|
|
||||||
read -e -p "Push to origin? " SHOULD_PUSH
|
|
||||||
|
|
||||||
if [ "$SHOULD_PUSH" != "y" ]; then
|
|
||||||
echo "Aborting"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# If we don't push directly to github, the page doesn't get built for some
|
|
||||||
# reason.
|
|
||||||
git push git@github.com:maxmind/GeoIP2-php.git
|
|
||||||
git push
|
|
||||||
|
|
||||||
cd ..
|
|
||||||
git tag -a $TAG
|
|
||||||
git push
|
|
||||||
git push --tags
|
|
||||||
4
box.json
4
box.json
@@ -13,6 +13,8 @@
|
|||||||
"finder": [
|
"finder": [
|
||||||
{
|
{
|
||||||
"name": [
|
"name": [
|
||||||
|
"LICENSE",
|
||||||
|
"LICENSE.*",
|
||||||
"*.php",
|
"*.php",
|
||||||
"*.pem",
|
"*.pem",
|
||||||
"*.pem.md5"
|
"*.pem.md5"
|
||||||
@@ -27,8 +29,8 @@
|
|||||||
"in": "vendor"
|
"in": "vendor"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
|
||||||
"directories": ["compat", "src/"],
|
"directories": ["compat", "src/"],
|
||||||
"git-version": "git-version",
|
"git-version": "git-version",
|
||||||
|
"shebang": "",
|
||||||
"stub": true
|
"stub": true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,13 +14,11 @@
|
|||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"guzzle/guzzle": "3.*",
|
"guzzle/guzzle": "3.*",
|
||||||
"maxmind-db/reader": "~0.3.2",
|
"maxmind-db/reader": "~1.0",
|
||||||
"php": ">=5.3.1"
|
"php": ">=5.3.1"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"apigen/apigen": "~2.8.0",
|
"phpunit/phpunit": "4.2.*",
|
||||||
"nette/nette": "~2.1.3",
|
|
||||||
"phpunit/phpunit": "4.1.*",
|
|
||||||
"satooshi/php-coveralls": "dev-master"
|
"satooshi/php-coveralls": "dev-master"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
|
|||||||
16
dev-bin/phar-test.php
Executable file
16
dev-bin/phar-test.php
Executable file
@@ -0,0 +1,16 @@
|
|||||||
|
#!/usr/bin/env php
|
||||||
|
|
||||||
|
<?php
|
||||||
|
require_once 'geoip2.phar';
|
||||||
|
use GeoIp2\Database\Reader;
|
||||||
|
|
||||||
|
$reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb');
|
||||||
|
|
||||||
|
$record = $reader->city('81.2.69.160');
|
||||||
|
|
||||||
|
if ( $record->country->isoCode === 'GB' ) {
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
print('Problem with Phar!');
|
||||||
|
exit(1);
|
||||||
111
dev-bin/release.sh
Executable file
111
dev-bin/release.sh
Executable file
@@ -0,0 +1,111 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
TAG=$1
|
||||||
|
|
||||||
|
if [ -z $TAG ]; then
|
||||||
|
echo "Please specify a tag"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -f geoip2.phar ]; then
|
||||||
|
rm geoip2.phar
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "$(git status --porcelain)" ]; then
|
||||||
|
echo ". is not clean." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -d vendor ]; then
|
||||||
|
rm -fr vendor
|
||||||
|
fi
|
||||||
|
|
||||||
|
php composer.phar self-update
|
||||||
|
php composer.phar update --no-dev
|
||||||
|
|
||||||
|
# We currently use a custom version of Box due to
|
||||||
|
# https://github.com/box-project/box2/issues/88. There are PRs from Greg with
|
||||||
|
# the fixes.
|
||||||
|
#
|
||||||
|
# if [ ! -f box.phar ]; then
|
||||||
|
# wget -O box.phar "https://github.com/kherge-archive/Box/releases/download/2.4.4/box-2.4.4.phar"
|
||||||
|
# fi
|
||||||
|
|
||||||
|
../box2/bin/box build
|
||||||
|
./dev-bin/phar-test.php
|
||||||
|
|
||||||
|
# Download test deps
|
||||||
|
php composer.phar update
|
||||||
|
|
||||||
|
./vendor/bin/phpunit
|
||||||
|
|
||||||
|
if [ ! -d .gh-pages ]; then
|
||||||
|
echo "Checking out gh-pages in .gh-pages"
|
||||||
|
git clone -b gh-pages git@git.maxmind.com:GeoIP2-php .gh-pages
|
||||||
|
pushd .gh-pages
|
||||||
|
else
|
||||||
|
echo "Updating .gh-pages"
|
||||||
|
pushd .gh-pages
|
||||||
|
git pull
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "$(git status --porcelain)" ]; then
|
||||||
|
echo ".gh-pages is not clean" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# We no longer have apigen as a dependency in Composer as releases are
|
||||||
|
# sporadically deleted upstream and compatibility is often broken on patch
|
||||||
|
# releases.
|
||||||
|
if [ ! -f apigen.phar ]; then
|
||||||
|
wget -O apigen.phar "https://github.com/apigen/apigen/releases/download/v4.0.0-RC3/apigen-4.0.0-RC3.phar"
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
cat <<EOF > apigen.neon
|
||||||
|
destination: doc/$TAG
|
||||||
|
|
||||||
|
source:
|
||||||
|
- ../src
|
||||||
|
|
||||||
|
title: "GeoIP2 PHP API $TAG"
|
||||||
|
EOF
|
||||||
|
|
||||||
|
php apigen.phar generate
|
||||||
|
|
||||||
|
|
||||||
|
PAGE=index.md
|
||||||
|
cat <<EOF > $PAGE
|
||||||
|
---
|
||||||
|
layout: default
|
||||||
|
title: MaxMind GeoIP2 PHP API
|
||||||
|
language: php
|
||||||
|
version: $TAG
|
||||||
|
---
|
||||||
|
|
||||||
|
EOF
|
||||||
|
|
||||||
|
cat ../README.md >> $PAGE
|
||||||
|
|
||||||
|
git add doc/
|
||||||
|
git commit -m "Updated for $TAG" -a
|
||||||
|
|
||||||
|
read -e -p "Push to origin? " SHOULD_PUSH
|
||||||
|
|
||||||
|
if [ "$SHOULD_PUSH" != "y" ]; then
|
||||||
|
echo "Aborting"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# If we don't push directly to github, the page doesn't get built for some
|
||||||
|
# reason.
|
||||||
|
git push git@github.com:maxmind/GeoIP2-php.git
|
||||||
|
git push
|
||||||
|
|
||||||
|
popd
|
||||||
|
|
||||||
|
git tag -a $TAG
|
||||||
|
git push
|
||||||
|
git push --tags
|
||||||
Submodule maxmind-db updated: 1d28147617...e53ed8cde6
@@ -8,13 +8,9 @@ use MaxMind\Db\Reader as DbReader;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Instances of this class provide a reader for the GeoIP2 database format.
|
* Instances of this class provide a reader for the GeoIP2 database format.
|
||||||
* IP addresses can be looked up using the <code>country</code>
|
* IP addresses can be looked up using the database specific methods.
|
||||||
* and <code>city</code> methods. We also provide <code>cityIspOrg</code>
|
|
||||||
* and <code>omni</code> methods to ease compatibility with the web service
|
|
||||||
* client, although we may offer the ability to specify additional databases
|
|
||||||
* to replicate these web services in the future (e.g., the ISP/Org database).
|
|
||||||
*
|
*
|
||||||
* **Usage**
|
* ## Usage ##
|
||||||
*
|
*
|
||||||
* The basic API for this class is the same for every database. First, you
|
* The basic API for this class is the same for every database. First, you
|
||||||
* create a reader object, specifying a file name. You then call the method
|
* create a reader object, specifying a file name. You then call the method
|
||||||
@@ -25,7 +21,7 @@ use MaxMind\Db\Reader as DbReader;
|
|||||||
* the method you called. This model in turn contains multiple record classes,
|
* the method you called. This model in turn contains multiple record classes,
|
||||||
* each of which represents part of the data returned by the database. If
|
* each of which represents part of the data returned by the database. If
|
||||||
* the database does not contain the requested information, the attributes
|
* the database does not contain the requested information, the attributes
|
||||||
* on the record class will have a <code>null</code> value.
|
* on the record class will have a `null` value.
|
||||||
*
|
*
|
||||||
* If the address is not in the database, an
|
* If the address is not in the database, an
|
||||||
* {@link \GeoIp2\Exception\AddressNotFoundException} exception will be
|
* {@link \GeoIp2\Exception\AddressNotFoundException} exception will be
|
||||||
@@ -71,11 +67,7 @@ class Reader implements ProviderInterface
|
|||||||
*/
|
*/
|
||||||
public function city($ipAddress)
|
public function city($ipAddress)
|
||||||
{
|
{
|
||||||
return $this->modelFor(
|
return $this->modelFor('City', 'City', $ipAddress);
|
||||||
'City',
|
|
||||||
array('GeoLite2-City', 'GeoIP2-City'),
|
|
||||||
$ipAddress
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -92,13 +84,42 @@ class Reader implements ProviderInterface
|
|||||||
*/
|
*/
|
||||||
public function country($ipAddress)
|
public function country($ipAddress)
|
||||||
{
|
{
|
||||||
return $this->modelFor(
|
return $this->modelFor('Country', 'Country', $ipAddress);
|
||||||
'Country',
|
}
|
||||||
array('GeoLite2-Country', 'GeoIP2-Country'),
|
|
||||||
|
/**
|
||||||
|
* This method returns a GeoIP2 Anonymous IP model.
|
||||||
|
*
|
||||||
|
* @param string $ipAddress IPv4 or IPv6 address as a string.
|
||||||
|
*
|
||||||
|
* @return \GeoIp2\Model\AnonymousIp
|
||||||
|
*
|
||||||
|
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
|
||||||
|
* not in the database.
|
||||||
|
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
|
||||||
|
* is corrupt or invalid
|
||||||
|
*/
|
||||||
|
public function anonymousIp($ipAddress)
|
||||||
|
{
|
||||||
|
return $this->flatModelFor(
|
||||||
|
'AnonymousIp',
|
||||||
|
'GeoIP2-Anonymous-IP',
|
||||||
$ipAddress
|
$ipAddress
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method returns a GeoIP2 Connection Type model.
|
||||||
|
*
|
||||||
|
* @param string $ipAddress IPv4 or IPv6 address as a string.
|
||||||
|
*
|
||||||
|
* @return \GeoIp2\Model\ConnectionType
|
||||||
|
*
|
||||||
|
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
|
||||||
|
* not in the database.
|
||||||
|
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
|
||||||
|
* is corrupt or invalid
|
||||||
|
*/
|
||||||
public function connectionType($ipAddress)
|
public function connectionType($ipAddress)
|
||||||
{
|
{
|
||||||
return $this->flatModelFor(
|
return $this->flatModelFor(
|
||||||
@@ -108,6 +129,18 @@ class Reader implements ProviderInterface
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method returns a GeoIP2 Domain model.
|
||||||
|
*
|
||||||
|
* @param string $ipAddress IPv4 or IPv6 address as a string.
|
||||||
|
*
|
||||||
|
* @return \GeoIp2\Model\Domain
|
||||||
|
*
|
||||||
|
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
|
||||||
|
* not in the database.
|
||||||
|
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
|
||||||
|
* is corrupt or invalid
|
||||||
|
*/
|
||||||
public function domain($ipAddress)
|
public function domain($ipAddress)
|
||||||
{
|
{
|
||||||
return $this->flatModelFor(
|
return $this->flatModelFor(
|
||||||
@@ -117,6 +150,18 @@ class Reader implements ProviderInterface
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method returns a GeoIP2 ISP model.
|
||||||
|
*
|
||||||
|
* @param string $ipAddress IPv4 or IPv6 address as a string.
|
||||||
|
*
|
||||||
|
* @return \GeoIp2\Model\Isp
|
||||||
|
*
|
||||||
|
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
|
||||||
|
* not in the database.
|
||||||
|
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
|
||||||
|
* is corrupt or invalid
|
||||||
|
*/
|
||||||
public function isp($ipAddress)
|
public function isp($ipAddress)
|
||||||
{
|
{
|
||||||
return $this->flatModelFor(
|
return $this->flatModelFor(
|
||||||
@@ -126,16 +171,9 @@ class Reader implements ProviderInterface
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function modelFor($class, $types, $ipAddress)
|
private function modelFor($class, $type, $ipAddress)
|
||||||
{
|
{
|
||||||
if (!in_array($this->metadata()->databaseType, $types)) {
|
$record = $this->getRecord($class, $type, $ipAddress);
|
||||||
$method = lcfirst($class);
|
|
||||||
throw new \BadMethodCallException(
|
|
||||||
"The $method method cannot be used to open a "
|
|
||||||
. $this->metadata()->databaseType . " database"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
$record = $this->getRecord($ipAddress);
|
|
||||||
|
|
||||||
$record['traits']['ip_address'] = $ipAddress;
|
$record['traits']['ip_address'] = $ipAddress;
|
||||||
$class = "GeoIp2\\Model\\" . $class;
|
$class = "GeoIp2\\Model\\" . $class;
|
||||||
@@ -145,15 +183,7 @@ class Reader implements ProviderInterface
|
|||||||
|
|
||||||
private function flatModelFor($class, $type, $ipAddress)
|
private function flatModelFor($class, $type, $ipAddress)
|
||||||
{
|
{
|
||||||
if ($this->metadata()->databaseType !== $type) {
|
$record = $this->getRecord($class, $type, $ipAddress);
|
||||||
$method = lcfirst($class);
|
|
||||||
throw new \BadMethodCallException(
|
|
||||||
"The $method method cannot be used to open a "
|
|
||||||
. $this->metadata()->databaseType . " database"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
$record = $this->getRecord($ipAddress);
|
|
||||||
|
|
||||||
$record['ip_address'] = $ipAddress;
|
$record['ip_address'] = $ipAddress;
|
||||||
$class = "GeoIp2\\Model\\" . $class;
|
$class = "GeoIp2\\Model\\" . $class;
|
||||||
@@ -161,8 +191,15 @@ class Reader implements ProviderInterface
|
|||||||
return new $class($record);
|
return new $class($record);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getRecord($ipAddress)
|
private function getRecord($class, $type, $ipAddress)
|
||||||
{
|
{
|
||||||
|
if (strpos($this->metadata()->databaseType, $type) === false) {
|
||||||
|
$method = lcfirst($class);
|
||||||
|
throw new \BadMethodCallException(
|
||||||
|
"The $method method cannot be used to open a "
|
||||||
|
. $this->metadata()->databaseType . " database"
|
||||||
|
);
|
||||||
|
}
|
||||||
$record = $this->dbReader->get($ipAddress);
|
$record = $this->dbReader->get($ipAddress);
|
||||||
if ($record === null) {
|
if ($record === null) {
|
||||||
throw new AddressNotFoundException(
|
throw new AddressNotFoundException(
|
||||||
|
|||||||
@@ -23,7 +23,15 @@ abstract class AbstractModel implements \JsonSerializable
|
|||||||
*/
|
*/
|
||||||
protected function get($field)
|
protected function get($field)
|
||||||
{
|
{
|
||||||
return isset($this->raw[$field]) ? $this->raw[$field] : null;
|
if (isset($this->raw[$field])) {
|
||||||
|
return $this->raw[$field];
|
||||||
|
} else {
|
||||||
|
if (preg_match('/^is_/', $field)) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
50
src/GeoIp2/Model/AnonymousIp.php
Normal file
50
src/GeoIp2/Model/AnonymousIp.php
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace GeoIp2\Model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class provides the GeoIP2 Anonymous IP model.
|
||||||
|
*
|
||||||
|
* @property boolean $isAnonymous This is true if the IP address belongs to
|
||||||
|
* any sort of anonymous network.
|
||||||
|
*
|
||||||
|
* @property boolean $isAnonymousVpn This is true if the IP address belongs to
|
||||||
|
* an anonymous VPN system.
|
||||||
|
*
|
||||||
|
* @property boolean $isHostingProvider This is true if the IP address belongs
|
||||||
|
* to a hosting provider.
|
||||||
|
*
|
||||||
|
* @property boolean $isPublicProxy This is true if the IP address belongs to
|
||||||
|
* a public proxy.
|
||||||
|
*
|
||||||
|
* @property boolean $isTorExitNode This is true if the IP address is a Tor
|
||||||
|
* exit node.
|
||||||
|
*
|
||||||
|
* @property string $ipAddress The IP address that the data in the model is
|
||||||
|
* for.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class AnonymousIp extends AbstractModel
|
||||||
|
{
|
||||||
|
protected $isAnonymous;
|
||||||
|
protected $isAnonymousVpn;
|
||||||
|
protected $isHostingProvider;
|
||||||
|
protected $isPublicProxy;
|
||||||
|
protected $isTorExitNode;
|
||||||
|
protected $ipAddress;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ignore
|
||||||
|
*/
|
||||||
|
public function __construct($raw)
|
||||||
|
{
|
||||||
|
parent::__construct($raw);
|
||||||
|
|
||||||
|
$this->isAnonymous = $this->get('is_anonymous');
|
||||||
|
$this->isAnonymousVpn = $this->get('is_anonymous_vpn');
|
||||||
|
$this->isHostingProvider = $this->get('is_hosting_provider');
|
||||||
|
$this->isPublicProxy = $this->get('is_public_proxy');
|
||||||
|
$this->isTorExitNode = $this->get('is_tor_exit_node');
|
||||||
|
$this->ipAddress = $this->get('ip_address');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -25,7 +25,11 @@ abstract class AbstractRecord implements \JsonSerializable
|
|||||||
if ($this->__isset($attr)) {
|
if ($this->__isset($attr)) {
|
||||||
return $this->record[$key];
|
return $this->record[$key];
|
||||||
} elseif ($this->validAttribute($attr)) {
|
} elseif ($this->validAttribute($attr)) {
|
||||||
return null;
|
if (preg_match('/^is_/', $key)) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new \RuntimeException("Unknown attribute: $attr");
|
throw new \RuntimeException("Unknown attribute: $attr");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,13 +30,12 @@ use Guzzle\Http\Exception\ServerErrorResponseException;
|
|||||||
* The web service may not return any information for an entire record, in
|
* The web service may not return any information for an entire record, in
|
||||||
* which case all of the attributes for that record class will be empty.
|
* which case all of the attributes for that record class will be empty.
|
||||||
*
|
*
|
||||||
* **Usage**
|
* ## Usage ##
|
||||||
*
|
*
|
||||||
* The basic API for this class is the same for all of the web service end
|
* The basic API for this class is the same for all of the web service end
|
||||||
* points. First you create a web service object with your MaxMind
|
* points. First you create a web service object with your MaxMind `$userId`
|
||||||
* <code>$userId</code> and <code>$licenseKey</code>, then you call the method
|
* and `$licenseKey`, then you call the method corresponding to a specific end
|
||||||
* corresponding to a specific end point, passing it the IP address you want
|
* point, passing it the IP address you want to look up.
|
||||||
* to look up.
|
|
||||||
*
|
*
|
||||||
* If the request succeeds, the method call will return a model class for
|
* If the request succeeds, the method call will return a model class for
|
||||||
* the end point you called. This model in turn contains multiple record
|
* the end point you called. This model in turn contains multiple record
|
||||||
@@ -111,40 +110,6 @@ class Client implements ProviderInterface
|
|||||||
return $this->responseFor('city', 'City', $ipAddress);
|
return $this->responseFor('city', 'City', $ipAddress);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* This method calls the GeoIP2 Precision: City endpoint.
|
|
||||||
*
|
|
||||||
* @param string $ipAddress IPv4 or IPv6 address as a string. If no
|
|
||||||
* address is provided, the address that the web service is called
|
|
||||||
* from will be used.
|
|
||||||
*
|
|
||||||
* @return \GeoIp2\Model\City
|
|
||||||
*
|
|
||||||
* @throws \GeoIp2\Exception\AddressNotFoundException if the address you
|
|
||||||
* provided is not in our database (e.g., a private address).
|
|
||||||
* @throws \GeoIp2\Exception\AuthenticationException if there is a problem
|
|
||||||
* with the user ID or license key that you provided.
|
|
||||||
* @throws \GeoIp2\Exception\OutOfQueriesException if your account is out
|
|
||||||
* of queries.
|
|
||||||
* @throws \GeoIp2\Exception\InvalidRequestException} if your request was
|
|
||||||
* received by the web service but is invalid for some other reason.
|
|
||||||
* This may indicate an issue with this API. Please report the error to
|
|
||||||
* MaxMind.
|
|
||||||
* @throws \GeoIp2\Exception\HttpException if an unexpected HTTP error
|
|
||||||
* code or message was returned. This could indicate a problem with the
|
|
||||||
* connection between your server and the web service or that the web
|
|
||||||
* service returned an invalid document or 500 error code.
|
|
||||||
* @throws \GeoIp2\Exception\GeoIp2Exception This serves as the parent
|
|
||||||
* class to the above exceptions. It will be thrown directly if a 200
|
|
||||||
* status code is returned but the body is invalid.
|
|
||||||
*
|
|
||||||
* @deprecated deprecated since version 0.7.0
|
|
||||||
*/
|
|
||||||
public function cityIspOrg($ipAddress = 'me')
|
|
||||||
{
|
|
||||||
return $this->city($ipAddress);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method calls the GeoIP2 Precision: Country endpoint.
|
* This method calls the GeoIP2 Precision: Country endpoint.
|
||||||
*
|
*
|
||||||
@@ -203,48 +168,12 @@ class Client implements ProviderInterface
|
|||||||
* @throws \GeoIp2\Exception\GeoIp2Exception This serves as the parent
|
* @throws \GeoIp2\Exception\GeoIp2Exception This serves as the parent
|
||||||
* class to the above exceptions. It will be thrown directly if a 200
|
* class to the above exceptions. It will be thrown directly if a 200
|
||||||
* status code is returned but the body is invalid.
|
* status code is returned but the body is invalid.
|
||||||
*
|
|
||||||
* @deprecated deprecated since version 0.7.0
|
|
||||||
*/
|
*/
|
||||||
public function insights($ipAddress = 'me')
|
public function insights($ipAddress = 'me')
|
||||||
{
|
{
|
||||||
return $this->responseFor('insights', 'Insights', $ipAddress);
|
return $this->responseFor('insights', 'Insights', $ipAddress);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* This method calls the GeoIP2 Precision: Insights (prev. Omni) endpoint.
|
|
||||||
*
|
|
||||||
* @param string $ipAddress IPv4 or IPv6 address as a string. If no
|
|
||||||
* address is provided, the address that the web service is called
|
|
||||||
* from will be used.
|
|
||||||
*
|
|
||||||
* @return \GeoIp2\Model\Insights
|
|
||||||
*
|
|
||||||
* @throws \GeoIp2\Exception\AddressNotFoundException if the address you
|
|
||||||
* provided is not in our database (e.g., a private address).
|
|
||||||
* @throws \GeoIp2\Exception\AuthenticationException if there is a problem
|
|
||||||
* with the user ID or license key that you provided.
|
|
||||||
* @throws \GeoIp2\Exception\OutOfQueriesException if your account is out
|
|
||||||
* of queries.
|
|
||||||
* @throws \GeoIp2\Exception\InvalidRequestException} if your request was
|
|
||||||
* received by the web service but is invalid for some other reason.
|
|
||||||
* This may indicate an issue with this API. Please report the error to
|
|
||||||
* MaxMind.
|
|
||||||
* @throws \GeoIp2\Exception\HttpException if an unexpected HTTP error
|
|
||||||
* code or message was returned. This could indicate a problem with the
|
|
||||||
* connection between your server and the web service or that the web
|
|
||||||
* service returned an invalid document or 500 error code.
|
|
||||||
* @throws \GeoIp2\Exception\GeoIp2Exception This serves as the parent
|
|
||||||
* class to the above exceptions. It will be thrown directly if a 200
|
|
||||||
* status code is returned but the body is invalid.
|
|
||||||
*
|
|
||||||
* @deprecated deprecated since version 0.7.0
|
|
||||||
*/
|
|
||||||
public function omni($ipAddress = 'me')
|
|
||||||
{
|
|
||||||
return $this->insights($ipAddress);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function responseFor($endpoint, $class, $ipAddress)
|
private function responseFor($endpoint, $class, $ipAddress)
|
||||||
{
|
{
|
||||||
$uri = implode('/', array($this->baseUri(), $endpoint, $ipAddress));
|
$uri = implode('/', array($this->baseUri(), $endpoint, $ipAddress));
|
||||||
|
|||||||
@@ -88,6 +88,20 @@ class ReaderTest extends \PHPUnit_Framework_TestCase
|
|||||||
$reader->close();
|
$reader->close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testAnonymousIp()
|
||||||
|
{
|
||||||
|
$reader = new Reader('maxmind-db/test-data/GeoIP2-Anonymous-IP-Test.mmdb');
|
||||||
|
$ipAddress = '1.2.0.1';
|
||||||
|
|
||||||
|
$record = $reader->anonymousIp($ipAddress);
|
||||||
|
$this->assertSame(true, $record->isAnonymous);
|
||||||
|
$this->assertSame(true, $record->isAnonymousVpn);
|
||||||
|
$this->assertSame(false, $record->isHostingProvider);
|
||||||
|
$this->assertSame(false, $record->isPublicProxy);
|
||||||
|
$this->assertSame(false, $record->isTorExitNode);
|
||||||
|
$this->assertEquals($ipAddress, $record->ipAddress);
|
||||||
|
$reader->close();
|
||||||
|
}
|
||||||
|
|
||||||
public function testConnectionType()
|
public function testConnectionType()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ class InsightsTest extends \PHPUnit_Framework_TestCase
|
|||||||
'autonomous_system_organization' => 'AS Organization',
|
'autonomous_system_organization' => 'AS Organization',
|
||||||
'domain' => 'example.com',
|
'domain' => 'example.com',
|
||||||
'ip_address' => '1.2.3.4',
|
'ip_address' => '1.2.3.4',
|
||||||
'is_satellite_provider' => 1,
|
'is_satellite_provider' => true,
|
||||||
'isp' => 'Comcast',
|
'isp' => 'Comcast',
|
||||||
'organization' => 'Blorg',
|
'organization' => 'Blorg',
|
||||||
'user_type' => 'college',
|
'user_type' => 'college',
|
||||||
@@ -130,6 +130,18 @@ class InsightsTest extends \PHPUnit_Framework_TestCase
|
|||||||
'$model->traits'
|
'$model->traits'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$this->assertSame(
|
||||||
|
true,
|
||||||
|
$model->traits->isSatelliteProvider,
|
||||||
|
'$model->traits->isSatelliteProvider is true'
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertSame(
|
||||||
|
false,
|
||||||
|
$model->traits->isAnonymousProxy,
|
||||||
|
'$model->traits->isAnonymousProxy is false'
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
22,
|
22,
|
||||||
$model->maxmind->queriesRemaining,
|
$model->maxmind->queriesRemaining,
|
||||||
|
|||||||
@@ -201,19 +201,16 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function testInsights()
|
public function testInsights()
|
||||||
{
|
{
|
||||||
|
|
||||||
$methods = array('omni', 'insights');
|
$record = $this->client($this->getResponse('1.2.3.4'))
|
||||||
foreach ($methods as $method) {
|
->insights('1.2.3.4');
|
||||||
$record = $this->client($this->getResponse('1.2.3.4'))
|
|
||||||
->$method('1.2.3.4');
|
|
||||||
|
|
||||||
$this->assertInstanceOf('GeoIp2\Model\Insights', $record);
|
$this->assertInstanceOf('GeoIp2\Model\Insights', $record);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
42,
|
42,
|
||||||
$record->continent->geonameId,
|
$record->continent->geonameId,
|
||||||
'continent geoname_id is 42'
|
'continent geoname_id is 42'
|
||||||
);
|
);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCity()
|
public function testCity()
|
||||||
@@ -230,7 +227,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$this->assertInstanceOf(
|
$this->assertInstanceOf(
|
||||||
'GeoIp2\Model\City',
|
'GeoIp2\Model\City',
|
||||||
$client->cityIspOrg('me'),
|
$client->city('me'),
|
||||||
'can set ip parameter to me'
|
'can set ip parameter to me'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user