Skip to content

Commit

Permalink
Updated file structures to use the latest standards.
Browse files Browse the repository at this point in the history
  • Loading branch information
ip2location committed Mar 4, 2021
1 parent 8141a1c commit ae42f6f
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 248 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ Monthly update is available for both IP2Location LITE and commercial database.

## INSTALLATION

To install this module, unzip the package and copy the following files to your web folder.
* IP2Location.php
* example.php
* databases/IP2LOCATION-LITE-DB1.BIN
Install this package using **composer** as below:

To test this installation, please browse example.php using web browser.
```
composer require ip2location/ip2location-php
```

To test this installation, please browse examples/example.php using web browser.

## USAGE

Expand Down Expand Up @@ -104,6 +105,6 @@ Below are the list of other framework library that you can install and use right

## COPYRIGHT AND LICENSE

Copyright (C) 2005-2020 by IP2Location.com
Copyright (C) 2005-2021 by IP2Location.com

License under MIT
24 changes: 13 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@
"license": "MIT",
"authors": [
{
"name": "IP2Location",
"email": "[email protected]",
"homepage": "https://www.ip2location.com"
"name": "IP2Location",
"email": "[email protected]",
"homepage": "https://www.ip2location.com"
}
],
"autoload": {
"classmap": [
"IP2Location.php"
]
},
"require-dev": {
"phpunit/phpunit": "^9"
"phpunit/phpunit": "^9.5"
},
"require": {
"php": ">=5.4.0",
"php": ">=7.2",
"ext-bcmath": "*",
"ext-curl": "*",
"ext-gmp": "*"
"ext-gmp": "*",
"ext-json": "*"
},
"autoload": {
"psr-4": {
"IP2Location\\": "src"
}
}
}
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions example.php → examples/example.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<?php

require_once 'IP2Location.php';
require 'vendor/autoload.php';
/*
Cache whole database into system memory and share among other scripts & websites
WARNING: Please make sure your system have sufficient RAM to enable this feature
*/
//$db = new \IP2Location\Database('./databases/IP2LOCATION-LITE-DB1.BIN', \IP2Location\Database::SHARED_MEMORY);
//$db = new \IP2Location\Database('./data/IP2LOCATION-LITE-DB1.BIN', \IP2Location\Database::SHARED_MEMORY);

/*
Cache the database into memory to accelerate lookup speed
WARNING: Please make sure your system have sufficient RAM to enable this feature
*/
// $db = new \IP2Location\Database('./databases/IP2LOCATION-LITE-DB1.BIN', \IP2Location\Database::MEMORY_CACHE);
// $db = new \IP2Location\Database('./data/IP2LOCATION-LITE-DB1.BIN', \IP2Location\Database::MEMORY_CACHE);

// Default file I/O lookup
$db = new \IP2Location\Database('./databases/IP2LOCATION-LITE-DB1.BIN', \IP2Location\Database::FILE_IO);
$db = new \IP2Location\Database('./data/IP2LOCATION-LITE-DB1.BIN', \IP2Location\Database::FILE_IO);

$records = $db->lookup('8.8.8.8', \IP2Location\Database::ALL);

Expand Down
221 changes: 1 addition & 220 deletions IP2Location.php → src/Database.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
<?php

/*
* Copyright (C) 2005-2020 IP2Location.com
* All Rights Reserved
*
* This library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace IP2Location;

/**
Expand All @@ -31,7 +12,7 @@ class Database
*
* @var string
*/
public const VERSION = '8.3.0';
public const VERSION = '9.0.0';

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Error field constants ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -2119,203 +2100,3 @@ private function binSearch($version, $ipNumber, $cidr = false)
return false;
}
}

/**
* IP2Location web service class.
*/
class WebService
{
/**
* No cURL extension found.
*
* @var int
*/
public const EXCEPTION_NO_CURL = 10001;

/**
* Invalid API key format.
*
* @var int
*/
public const EXCEPTION_INVALID_API_KEY = 10002;

/**
* Web service error.
*
* @var int
*/
public const EXCEPTION_WEB_SERVICE_ERROR = 10003;

/**
* Constructor.
*
* @param string $apiKey API key of your IP2Location web service
* @param string $package Supported IP2Location package from WS1 to WS24
* @param bool $useSsl Enable or disabled HTTPS connection. HTTP is faster but less secure.
*
* @throws \Exception
*/
public function __construct($apiKey, $package = 'WS1', $useSsl = false)
{
if (!\extension_loaded('curl')) {
throw new \Exception(__CLASS__ . ": Please make sure your PHP setup has the 'curl' extension enabled.", self::EXCEPTION_NO_CURL);
}

if (!preg_match('/^[0-9A-Z]{10}$/', $apiKey) && $apiKey != 'demo') {
throw new \Exception(__CLASS__ . ': Please provide a valid IP2Location web service API key.', self::EXCEPTION_INVALID_API_KEY);
}

if (!preg_match('/^WS[0-9]+$/', $package)) {
$package = 'WS1';
}

$this->apiKey = $apiKey;
$this->package = $package;
$this->useSsl = $useSsl;
}

/**
* This function will look the given IP address up in IP2Location web service.
*
* @param string $ip IP address to look up
* @param array $addOns Extra fields to return. Please refer to https://www.ip2location.com/web-service/ip2location
* @param string $language the translation for continent, country, region and city name for the addon package
*
* @throws \Exception
*
* @return array|false
*/
public function lookup($ip, $addOns = [], $language = 'en')
{
$response = $this->httpRequest('http://api.ip2location.com/v2/?' . http_build_query([
'key' => $this->apiKey,
'ip' => $ip,
'package' => $this->package,
'addon' => implode(',', $addOns),
'lang' => $language,
]));

if (($data = json_decode($response, true)) === null) {
return false;
}

if (isset($data['response'])) {
throw new \Exception(__CLASS__ . ': ' . $data['response'], self::EXCEPTION_WEB_SERVICE_ERROR);
}

return $data;
}

/**
* Get the remaing credit in your IP2Location web service account.
*
* @return int
*/
public function getCredit()
{
$response = $this->httpRequest('http://api.ip2location.com/v2/?' . http_build_query([
'key' => $this->apiKey,
'check' => true,
]));

if (($data = json_decode($response, true)) === null) {
return 0;
}

if (!isset($data['response'])) {
return 0;
}

return $data['response'];
}

/**
* Open a remote web address.
*
* @param string $url Website URL
*
* @return bool|string
*/
private function httpRequest($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);

$response = curl_exec($ch);

if (!curl_errno($ch)) {
curl_close($ch);

return $response;
}

curl_close($ch);

return false;
}
}

/**
* IpTools class.
*/
class IpTools
{
public function isIpv4($ip)
{
return (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) ? true : false;
}

public function isIpv6($ip)
{
return (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) ? true : false;
}

public function ipv4ToDecimal($ip)
{
if (!$this->isIpv4($ip)) {
return;
}

return sprintf('%u', ip2long($ip));
}

public function ipv6ToDecimal($ipv6)
{
if (!$this->isIpv6($ipv6)) {
return;
}

return (string) gmp_import(inet_pton($ipv6));
}

public function decimalToIpv4($number)
{
if (!preg_match('/^\d+$/', $number)) {
return;
}

if ($number > 4294967295) {
return;
}

return long2ip($number);
}

public function decimalToIpv6($number)
{
if (!preg_match('/^\d+$/', $number)) {
return;
}

if ($number <= 4294967295) {
return;
}

return inet_ntop(str_pad(gmp_export($number), 16, "\0", STR_PAD_LEFT));
}
}
63 changes: 63 additions & 0 deletions src/IPTools.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace IP2Location;

/**
* IpTools class.
*/
class IPTools
{
public function isIpv4($ip)
{
return (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) ? true : false;
}

public function isIpv6($ip)
{
return (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) ? true : false;
}

public function ipv4ToDecimal($ip)
{
if (!$this->isIpv4($ip)) {
return;
}

return sprintf('%u', ip2long($ip));
}

public function ipv6ToDecimal($ipv6)
{
if (!$this->isIpv6($ipv6)) {
return;
}

return (string) gmp_import(inet_pton($ipv6));
}

public function decimalToIpv4($number)
{
if (!preg_match('/^\d+$/', $number)) {
return;
}

if ($number > 4294967295) {
return;
}

return long2ip($number);
}

public function decimalToIpv6($number)
{
if (!preg_match('/^\d+$/', $number)) {
return;
}

if ($number <= 4294967295) {
return;
}

return inet_ntop(str_pad(gmp_export($number), 16, "\0", STR_PAD_LEFT));
}
}
Loading

0 comments on commit ae42f6f

Please sign in to comment.