Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
composer.lock
ipipfree.ipdb
vendor
48 changes: 28 additions & 20 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
{
"name": "ipip/db",
"type": "library",
"description": "IPIP.net officially supported IP database ipdb format parsing library",
"keywords": ["ipip.net","geoip","geolocation", "geo","ip","ipdb"],
"homepage": "https://www.ipip.net",
"license": "Apache-2.0",
"authors": [
{
"name": "IPIP.net",
"email": "[email protected]",
"homepage": "https://www.ipip.net"
}
],
"require": {
"php": ">=5.4.0"
},
"autoload": {
"psr-4": {
"ipip\\db\\": "src/ipip/db/"
}
"name": "ipip/db",
"type": "library",
"description": "IPIP.net officially supported IP database ipdb format parsing library",
"keywords": [
"ipip.net",
"geoip",
"geolocation",
"geo",
"ip",
"ipdb"
],
"homepage": "https://www.ipip.net",
"license": "Apache-2.0",
"authors": [
{
"name": "IPIP.net",
"email": "[email protected]",
"homepage": "https://www.ipip.net"
}
],
"require": {
"php": ">=5.4.0",
"ext-json": "*"
},
"autoload": {
"psr-4": {
"ipip\\db\\": "src/ipip/db/"
}
}
}
20 changes: 6 additions & 14 deletions src/ipip/db/BaseStation.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,25 @@

namespace ipip\db;

class BaseStation
class BaseStation extends Proxy
{
public $reader = NULL;

public function __construct($db)
{
$this->reader = new Reader($db);
}

public function find($ip, $language)
{
return $this->reader->find($ip, $language);
return $this->getReader()->find($ip, $language);
}

public function findMap($ip, $language)
{
return $this->reader->findMap($ip, $language);
return $this->getReader()->findMap($ip, $language);
}

public function findInfo($ip, $language)
{
$map = $this->findMap($ip, $language);
if (NULL === $map)
{
return NULL;
if (null === $map){
return null;
}

return new BaseStationInfo($map);
}
}
}
5 changes: 2 additions & 3 deletions src/ipip/db/BaseStationInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ class BaseStationInfo

public function __construct(array $data)
{
foreach ($data AS $field => $value)
{
foreach($data AS $field => $value){
$this->{$field} = $value;
}
}
Expand All @@ -29,4 +28,4 @@ public function __get($name)
{
return $this->{$name};
}
}
}
25 changes: 11 additions & 14 deletions src/ipip/db/City.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,30 @@

namespace ipip\db;

class City
class City extends Proxy
{
public $reader = NULL;

public function __construct($db)
{
$this->reader = new Reader($db);
}

/**
* @param string $ip
* @param string $language
* @return array|null
*/
public function find($ip, $language)
{
return $this->reader->find($ip, $language);
return $this->getReader()->find($ip, $language);
}

public function findMap($ip, $language)
{
return $this->reader->findMap($ip, $language);
return $this->getReader()->findMap($ip, $language);
}

public function findInfo($ip, $language)
{
$map = $this->findMap($ip, $language);
if (NULL === $map)
{
return NULL;
if (null === $map){
return null;
}

return new CityInfo($map);
}
}
}
20 changes: 6 additions & 14 deletions src/ipip/db/District.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,25 @@

namespace ipip\db;

class District
class District extends Proxy
{
public $reader = NULL;

public function __construct($db)
{
$this->reader = new Reader($db);
}

public function find($ip, $language)
{
return $this->reader->find($ip, $language);
return $this->getReader()->find($ip, $language);
}

public function findMap($ip, $language)
{
return $this->reader->findMap($ip, $language);
return $this->getReader()->findMap($ip, $language);
}

public function findInfo($ip, $language)
{
$map = $this->findMap($ip, $language);
if (NULL === $map)
{
return NULL;
if (null === $map){
return null;
}

return new DistrictInfo($map);
}
}
}
20 changes: 6 additions & 14 deletions src/ipip/db/IDC.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,25 @@

namespace ipip\db;

class IDC
class IDC extends Proxy
{
public $reader = NULL;

public function __construct($db)
{
$this->reader = new Reader($db);
}

public function find($ip, $language)
{
return $this->reader->find($ip, $language);
return $this->getReader()->find($ip, $language);
}

public function findMap($ip, $language)
{
return $this->reader->findMap($ip, $language);
return $this->getReader()->findMap($ip, $language);
}

public function findInfo($ip, $language)
{
$map = $this->findMap($ip, $language);
if (NULL === $map)
{
return NULL;
if (null === $map){
return null;
}

return new IDCInfo($map);
}
}
}
67 changes: 67 additions & 0 deletions src/ipip/db/IpDbReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/**
* @site https://www.ipip.net
* @desc Parse IP library in ipdb format
* @copyright IPIP.net
*/

namespace ipip\db;

class IpDbReader extends Reader
{
private $file;

private $database;

/**
* Reader constructor.
* @param $database
* @throws \Exception
*/
public function __construct($database)
{
$this->database = $database;

if (is_readable($this->database) === false){
throw new \InvalidArgumentException("The IP Database file \"{$this->database}\" does not exist or is not readable.");
}

$this->file = @fopen($this->database, 'rb');
if ($this->file === false){
throw new \InvalidArgumentException("IP Database File opening \"{$this->database}\".");
}

$this->init();
}

/**
* @inheritDoc
*/
protected function computeFileSize()
{
return @filesize($this->database);
}

/**
* @inheritDoc
*/
protected function read($offset, $length)
{
if (0 !== fseek($this->file, $offset)){
return false;
}

return fread($this->file, $length);
}

/**
* @inheritDoc
*/
public function close()
{
if (is_resource($this->file)){
fclose($this->file);
}
}
}
66 changes: 66 additions & 0 deletions src/ipip/db/IpUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace ipip\db;

/**
* Class IpUtils
* @package ipip\db
*/
class IpUtils
{
/**
* 判断是否为一个合法的IP地址
*
* @param string string [必须] 需要判断的字符;
* @return bool;
*/
public static function isIp($ip)
{
return false !== filter_var($ip, FILTER_VALIDATE_IP);
}

/**
* 判断是否是ipv4
*
* @param string $ip
* @return bool
*/
public static function isIp4($ip)
{
return false !== filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
}

/**
* 判断是否是ipv6
*
* @param string $ip
* @return bool
*/
public static function isIp6($ip)
{
return false !== filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
}

/**
* 判断是否是内网ip
*
* @param string $ip
* @return bool
*/
public static function isPrivateIp($ip)
{
return false === filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)
&& false !== filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE);
}

/**
* 判断是否是公网ip
*
* @param string $ip
* @return bool
*/
public static function isPublicIp($ip)
{
return false !== static::isPrivateIp($ip);
}
}
Loading