Compare commits

..

No commits in common. "master" and "v2.0.0" have entirely different histories.

17 changed files with 94 additions and 364 deletions

4
.gitignore vendored
View File

@ -2,13 +2,13 @@ _site
.gh-pages
.idea
GeoLite2-City.mmdb
apigen.phar
box.phar
build
composer.lock
composer.phar
phpunit.xml
geoip2.phar
geoip2-php.sublime-*
vendor/
*.sw?
t.php
*.old

View File

@ -1,23 +1,6 @@
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)
------------------

View File

@ -114,24 +114,6 @@ 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 ###
```php
@ -256,6 +238,9 @@ for details on what data each end point may return.
The only piece of data which is always returned is the `ipAddress`
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 ##
[GeoNames](http://www.geonames.org/) offers web services and downloadable
@ -305,7 +290,7 @@ supported.
This library works and is tested with HHVM.
This library also relies on the [Guzzle3 HTTP client](https://github.com/guzzle/guzzle3)
This library also relies on the [Guzzle HTTP client](http://guzzlephp.org/)
and the [MaxMind DB Reader](https://github.com/maxmind/MaxMind-DB-Reader-php).
If you are using PHP 5.3 with an autoloader besides Composer, you must load
@ -314,7 +299,7 @@ If you are using PHP 5.3 with an autoloader besides Composer, you must load
## Contributing ##
Patches and pull requests are encouraged. All code should follow the
PSR-2 style guidelines. Please include unit tests whenever possible. You may obtain the test data for the maxmind-db folder by running `git submodule update --init --recursive` or adding `--recursive` to your initial clone, or from https://github.com/maxmind/MaxMind-DB
PSR-2 style guidelines. Please include unit tests whenever possible.
## Versioning ##

63
bin/release.sh Executable file
View File

@ -0,0 +1,63 @@
#!/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

View File

@ -13,8 +13,6 @@
"finder": [
{
"name": [
"LICENSE",
"LICENSE.*",
"*.php",
"*.pem",
"*.pem.md5"
@ -29,8 +27,8 @@
"in": "vendor"
}
],
"directories": ["compat", "src/"],
"git-version": "git-version",
"shebang": false,
"stub": true
}

View File

@ -18,6 +18,8 @@
"php": ">=5.3.1"
},
"require-dev": {
"apigen/apigen": "~2.8.0",
"nette/nette": "~2.1.3",
"phpunit/phpunit": "4.2.*",
"satooshi/php-coveralls": "dev-master"
},

View File

@ -1,15 +0,0 @@
#!/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);

View File

@ -1,112 +0,0 @@
#!/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
if [ ! -f box.phar ]; then
wget -O box.phar "https://github.com/box-project/box2/releases/download/2.5.0/box-2.5.0.phar"
fi
php box.phar build
PHAR_TEST=$(./dev-bin/phar-test.php)
if [[ -n $PHAR_TEST ]]; then
echo "Phar test outputed non-empty string: $PHAR_TEST"
exit 1
fi
# 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

@ -1 +1 @@
Subproject commit e53ed8cde6951be3bbd0fdb300d69c898b399a97
Subproject commit 1d2814761753aefe32311cb917ab1e4f16d9fd7e

View File

@ -8,9 +8,13 @@ use MaxMind\Db\Reader as DbReader;
/**
* Instances of this class provide a reader for the GeoIP2 database format.
* IP addresses can be looked up using the database specific methods.
* IP addresses can be looked up using the <code>country</code>
* 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
* create a reader object, specifying a file name. You then call the method
@ -21,7 +25,7 @@ use MaxMind\Db\Reader as DbReader;
* 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
* the database does not contain the requested information, the attributes
* on the record class will have a `null` value.
* on the record class will have a <code>null</code> value.
*
* If the address is not in the database, an
* {@link \GeoIp2\Exception\AddressNotFoundException} exception will be
@ -87,39 +91,6 @@ class Reader implements ProviderInterface
return $this->modelFor('Country', 'Country', $ipAddress);
}
/**
* 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
);
}
/**
* 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)
{
return $this->flatModelFor(
@ -129,18 +100,6 @@ 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)
{
return $this->flatModelFor(
@ -150,18 +109,6 @@ 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)
{
return $this->flatModelFor(

View File

@ -2,6 +2,7 @@
namespace GeoIp2\Model;
/**
* @ignore
*/
@ -22,15 +23,7 @@ abstract class AbstractModel implements \JsonSerializable
*/
protected function get($field)
{
if (isset($this->raw[$field])) {
return $this->raw[$field];
} else {
if (preg_match('/^is_/', $field)) {
return false;
} else {
return null;
}
}
return isset($this->raw[$field]) ? $this->raw[$field] : null;
}
/**

View File

@ -1,50 +0,0 @@
<?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');
}
}

View File

@ -25,11 +25,7 @@ abstract class AbstractRecord implements \JsonSerializable
if ($this->__isset($attr)) {
return $this->record[$key];
} elseif ($this->validAttribute($attr)) {
if (preg_match('/^is_/', $key)) {
return false;
} else {
return null;
}
return null;
} else {
throw new \RuntimeException("Unknown attribute: $attr");
}

View File

@ -30,12 +30,13 @@ use Guzzle\Http\Exception\ServerErrorResponseException;
* 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.
*
* ## Usage ##
* **Usage**
*
* 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 `$userId`
* and `$licenseKey`, then you call the method corresponding to a specific end
* point, passing it the IP address you want to look up.
* points. First you create a web service object with your MaxMind
* <code>$userId</code> and <code>$licenseKey</code>, then you call the method
* corresponding to a specific end point, passing it the IP address you want
* to look up.
*
* 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
@ -51,8 +52,6 @@ class Client implements ProviderInterface
private $locales;
private $host;
private $guzzleClient;
private $timeout;
private $connectTimeout;
/**
* Constructor.
@ -64,17 +63,13 @@ class Client implements ProviderInterface
* @param string $host Optional host parameter
* @param object $guzzleClient Optional Guzzle client to use (to facilitate
* unit testing).
* @param string $timeout Total transaction timeout in seconds
* @param string $connectTimeout Initial connection timeout in seconds
*/
public function __construct(
$userId,
$licenseKey,
$locales = array('en'),
$host = 'geoip.maxmind.com',
$guzzleClient = null,
$timeout = null,
$connectTimeout = null
$guzzleClient = null
) {
$this->userId = $userId;
$this->licenseKey = $licenseKey;
@ -82,8 +77,6 @@ class Client implements ProviderInterface
$this->host = $host;
// To enable unit testing
$this->guzzleClient = $guzzleClient;
$this->timeout = $timeout;
$this->connectTimeout = $connectTimeout;
}
/**
@ -188,14 +181,7 @@ class Client implements ProviderInterface
$client = $this->guzzleClient ?
$this->guzzleClient : new GuzzleClient();
$options = array();
if ($this->timeout !== null) {
$options['timeout'] = $this->timeout;
}
if ($this->connectTimeout !== null) {
$options['connect_timeout'] = $this->connectTimeout;
}
$request = $client->get($uri, array('Accept' => 'application/json'), $options);
$request = $client->get($uri, array('Accept' => 'application/json'));
$request->setAuth($this->userId, $this->licenseKey);
$this->setUserAgent($request);

View File

@ -88,20 +88,6 @@ class ReaderTest extends \PHPUnit_Framework_TestCase
$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()
{

View File

@ -62,7 +62,7 @@ class InsightsTest extends \PHPUnit_Framework_TestCase
'autonomous_system_organization' => 'AS Organization',
'domain' => 'example.com',
'ip_address' => '1.2.3.4',
'is_satellite_provider' => true,
'is_satellite_provider' => 1,
'isp' => 'Comcast',
'organization' => 'Blorg',
'user_type' => 'college',
@ -130,18 +130,6 @@ class InsightsTest extends \PHPUnit_Framework_TestCase
'$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(
22,
$model->maxmind->queriesRemaining,

View File

@ -419,9 +419,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
'abcdef123456',
array('en'),
'geoip.maxmind.com',
$guzzleClient,
27,
72
$guzzleClient
);
$client->country('1.2.3.4');
@ -448,21 +446,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
$this->assertStringMatchesFormat(
'GeoIP2 PHP API (Guzzle%s)',
$request->getHeader('User-Agent') . '',
'request sets Accept header to GeoIP2 PHP API (Guzzle%s)'
);
$curlOptions = $request->getCurlOptions();
$this->assertEquals(
'27000',
$curlOptions[CURLOPT_TIMEOUT_MS],
'request sets Curl Option Timeout to 27 seconds'
);
$this->assertEquals(
'72000',
$curlOptions[CURLOPT_CONNECTTIMEOUT_MS],
'request sets Curl Option Connect Timeout to 72 seconds'
'request sets Accept header to application/json'
);
}
@ -480,10 +464,6 @@ class ClientTest extends \PHPUnit_Framework_TestCase
$locales,
'geoip.maxmind.com',
$guzzleClient
// intentionally not specifying the below, to ensure backwards compatibility
//,
// 1, // optional timeout
// 1 // optional connect timeout
);
return $client;