Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit b2dd28d

Browse files
committed
Add project
0 parents  commit b2dd28d

File tree

8 files changed

+1209
-0
lines changed

8 files changed

+1209
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
examples/tmp

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2017 Ivan Dudarev, https://github.com/ddrv <ivan@ddrv.ru>
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# IP Tool
2+
Define data by IP Address
3+
4+
# Install
5+
Add to `composer.json`:
6+
```json
7+
{
8+
"require": {
9+
"ddrv/iptool":"*"
10+
}
11+
}
12+
```
13+
And run
14+
```text
15+
php composer.phar install
16+
```
17+
18+
# Create your IP addresses database
19+
See examples for create database:
20+
* Geo from Maxmind GeoLite2 Country `examples/geoFromMaxmindCountry.php`
21+
22+
# Use
23+
```php
24+
/**
25+
* Initialization IP Tool
26+
*/
27+
$iptool = new \Ddrv\Iptool\Iptool('/path/to/iptool.database');
28+
```
29+
30+
```php
31+
/**
32+
* Get information about created database
33+
*/
34+
print_r($iptool->about());
35+
```
36+
```text
37+
Array
38+
(
39+
[created] => 1507199627
40+
[author] => Ivan Dudarev
41+
[license] => MIT
42+
[networks] => Array
43+
(
44+
[count] => 276148
45+
[data] => Array
46+
(
47+
[country] => Array
48+
(
49+
[0] => code
50+
[1] => name
51+
)
52+
53+
)
54+
55+
)
56+
57+
)
58+
```
59+
```php
60+
/**
61+
* Search IP Address data
62+
*/
63+
print_r($iptool->find('81.32.17.89'));
64+
```
65+
```text
66+
Array
67+
(
68+
[network] => Array
69+
(
70+
[0] => 81.32.0.0
71+
[1] => 81.48.0.0
72+
)
73+
74+
[data] => Array
75+
(
76+
[country] => Array
77+
(
78+
[code] => es
79+
[name] => Spain
80+
)
81+
82+
)
83+
84+
)
85+
```

composer.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "ddrv/iptool",
3+
"version":"1.0.0",
4+
"require":{
5+
"php":">=5.3.0",
6+
"ext-pdo": "*",
7+
"ext-pdo_sqlite": "*"
8+
},
9+
"type": "library",
10+
"description": "Define data by IP Address",
11+
"keywords": ["iptool", "ip", "geo", "geoip", "info", "data"],
12+
"license": "MIT",
13+
"authors": [
14+
{
15+
"name": "Ivan Dudarev",
16+
"email": "ivan@ddrv.ru",
17+
"homepage": "https://ddrv.ru"
18+
}
19+
],
20+
"autoload": {
21+
"psr-4": {
22+
"Ddrv\\Iptool\\": "src/"
23+
}
24+
}
25+
}
1.33 MB
Binary file not shown.

examples/geoFromMaxmindCountry.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
ini_set("memory_limit", "64M");
3+
require_once(__DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'src'.DIRECTORY_SEPARATOR.'Converter.php');
4+
require_once(__DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'src'.DIRECTORY_SEPARATOR.'Iptool.php');
5+
$tmpDir = __DIR__.DIRECTORY_SEPARATOR.'tmp';
6+
$converter = new \Ddrv\Iptool\Converter($tmpDir);
7+
$dbFile = __DIR__.DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.'iptool.geo.country.dat';
8+
9+
$url = 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country-CSV.zip';
10+
$tmpFile = $tmpDir . DIRECTORY_SEPARATOR . 'tmp.zip';
11+
copy($url,$tmpFile);
12+
$zip = new ZipArchive();
13+
if ($zip->open($tmpFile) !== true) die;
14+
$i = -1;
15+
$zipPath = null;
16+
do {
17+
$i++;
18+
$csv = $zip->getNameIndex($i);
19+
preg_match('/(?<file>(?<zipPath>.*)\/GeoLite2-Country-Blocks-IPv4\.csv)$/ui', $csv, $m);
20+
} while ($i < $zip->numFiles && empty($m['file']));
21+
$zipPath = $m['zipPath'];
22+
$zip->close();
23+
24+
$locations = 'zip://' . $tmpFile . '#'.$zipPath.DIRECTORY_SEPARATOR.'GeoLite2-Country-Locations-en.csv';
25+
$geo = 'zip://' . $tmpFile . '#' . $m['file'];
26+
27+
/**
28+
* Set author.
29+
*/
30+
$converter->setAuthor('Ivan Dudarev');
31+
32+
/**
33+
* Set license.
34+
*/
35+
$converter->setLicense('MIT');
36+
37+
/**
38+
* Add source files.
39+
*/
40+
$converter->addCSV('locations',$locations);
41+
$converter->addCSV('geo',$geo);
42+
43+
/**
44+
* Add register Country.
45+
**/
46+
$country = array(
47+
'code' => array(
48+
'type' => 'string',
49+
'column' => 4,
50+
'transform' => 'low',
51+
),
52+
'name' => array(
53+
'type' => 'string',
54+
'column' => 5,
55+
),
56+
);
57+
$converter->addRegister('country','locations',0, $country);
58+
59+
/**
60+
* Add networks.
61+
*/
62+
$geo = array(
63+
'country' => 1,
64+
);
65+
$converter->addNetworks('geo', 'inetnum', 0, 0, $geo);
66+
67+
/**
68+
* Create Database.
69+
*/
70+
$converter->create($dbFile);
71+
72+
/**
73+
* Delete temporary file
74+
*/
75+
unlink($tmpFile);
76+
77+
/**
78+
* Get information about created database
79+
*/
80+
$iptool = new \Ddrv\Iptool\Iptool($dbFile);
81+
print_r($iptool->about());
82+
83+
/**
84+
* Search IP Address data
85+
*/
86+
print_r($iptool->find('81.32.17.89'));

0 commit comments

Comments
 (0)