Skip to content

Commit 061f607

Browse files
committed
Supported IP2Location IPTools class.
1 parent cdf7dbc commit 061f607

File tree

4 files changed

+224
-3
lines changed

4 files changed

+224
-3
lines changed

LICENSE.TXT

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020 IP2Location.com
3+
Copyright (c) 2022 IP2Location.com
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ For CakePHP 4.x
1212
2. Download latest IP2Location BIN database
1313
- IP2Location free LITE database at https://lite.ip2location.com
1414
- IP2Location commercial database at https://www.ip2location.com
15-
3. Unzip and copy the BIN file into */vendor/ip2location/ip2location-cakephp/src/Data* folder.
15+
3. Unzip and copy the BIN file into */vendor/ip2location/ip2location-cakephp/src/Data* folder.
1616
4. Rename the BIN file to IP2LOCATION.BIN.
1717

18-
**Note:** The plugin has included an old BIN database for your testing and development purpose.
18+
**Note:** The plugin has included an old BIN database for your testing and development purpose.
1919
You may want to download a latest copy of BIN database as the URL stated above.
2020
The BIN database refers to the binary file ended with .BIN extension, but not the CSV format.
2121
Please select the right package for download.
@@ -103,6 +103,19 @@ class TestsController extends AppController
103103
echo '<pre>';
104104
print_r ($record);
105105
echo '</pre>';
106+
107+
var_dump($IP2Location->isIpv4('8.8.8.8'));echo '<br>';
108+
var_dump($IP2Location->isIpv6('2001:4860:4860::8888'));echo '<br>';
109+
print_r($IP2Location->ipv4ToDecimal('8.8.8.8'));echo '<br>';
110+
print_r($IP2Location->decimalToIpv4(134744072));echo '<br>';
111+
print_r($IP2Location->ipv6ToDecimal('2001:4860:4860::8888'));echo '<br>';
112+
print_r($IP2Location->decimalToIpv6('42541956123769884636017138956568135816'));echo '<br>';
113+
print_r($IP2Location->ipv4ToCidr('8.0.0.0', '8.255.255.255'));echo '<br>';
114+
print_r($IP2Location->cidrToIpv4('8.0.0.0/8'));echo '<br>';
115+
print_r($IP2Location->ipv6ToCidr('2002:0000:0000:1234:abcd:ffff:c0a8:0000', '2002:0000:0000:1234:ffff:ffff:ffff:ffff'));echo '<br>';
116+
print_r($IP2Location->cidrToIpv6('2002::1234:abcd:ffff:c0a8:101/64'));echo '<br>';
117+
print_r($IP2Location->compressIpv6('2002:0000:0000:1234:FFFF:FFFF:FFFF:FFFF'));echo '<br>';
118+
print_r($IP2Location->expandIpv6('2002::1234:FFFF:FFFF:FFFF:FFFF'));echo '<br>';
106119
}
107120
108121
}

src/Controller/IP2LocationCoresController.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,76 @@ public function getWebService($ip)
6767
return $records;
6868
}
6969

70+
public function isIpv4($ip)
71+
{
72+
$ipTools = new \IP2Location\IpTools();
73+
return $ipTools->isIpv4($ip);
74+
}
75+
76+
public function isIpv6($ip)
77+
{
78+
$ipTools = new \IP2Location\IpTools();
79+
return $ipTools->isIpv6($ip);
80+
}
81+
82+
public function ipv4ToDecimal($ip)
83+
{
84+
$ipTools = new \IP2Location\IpTools();
85+
return $ipTools->ipv4ToDecimal($ip);
86+
}
87+
88+
public function ipv6ToDecimal($ip)
89+
{
90+
$ipTools = new \IP2Location\IpTools();
91+
return $ipTools->ipv6ToDecimal($ip);
92+
}
93+
94+
public function decimalToIpv4($num)
95+
{
96+
$ipTools = new \IP2Location\IpTools();
97+
return $ipTools->decimalToIpv4($num);
98+
}
99+
100+
public function decimalToIpv6($num)
101+
{
102+
$ipTools = new \IP2Location\IpTools();
103+
return $ipTools->decimalToIpv6($num);
104+
}
105+
106+
public function ipv4ToCidr($ipFrom, $ipTo)
107+
{
108+
$ipTools = new \IP2Location\IpTools();
109+
return $ipTools->ipv4ToCidr($ipFrom, $ipTo);
110+
}
111+
112+
public function cidrToIpv4($cidr)
113+
{
114+
$ipTools = new \IP2Location\IpTools();
115+
return $ipTools->cidrToIpv4($cidr);
116+
}
117+
118+
public function ipv6ToCidr($ipFrom, $ipTo)
119+
{
120+
$ipTools = new \IP2Location\IpTools();
121+
return $ipTools->ipv6ToCidr($ipFrom, $ipTo);
122+
}
123+
124+
public function cidrToIpv6($cidr)
125+
{
126+
$ipTools = new \IP2Location\IpTools();
127+
return $ipTools->cidrToIpv6($cidr);
128+
}
129+
130+
public function compressIpv6($ipv6)
131+
{
132+
$ipTools = new \IP2Location\IpTools();
133+
return $ipTools->compressIpv6($ipv6);
134+
}
135+
136+
public function expandIpv6($ipv6)
137+
{
138+
$ipTools = new \IP2Location\IpTools();
139+
return $ipTools->expandIpv6($ipv6);
140+
}
141+
70142
}

tests/ControllerTest.php

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,140 @@ public function testGetWebService() {
2727
$record['country_code'],
2828
);
2929
}
30+
31+
public function testIpv4()
32+
{
33+
$IP2Location = new IP2LocationCoresController();
34+
$this->assertTrue($IP2Location->isIpv4('8.8.8.8'));
35+
}
36+
37+
public function testInvalidIpv4()
38+
{
39+
$IP2Location = new IP2LocationCoresController();
40+
$this->assertFalse($IP2Location->isIpv4('8.8.8.555'));
41+
}
42+
43+
public function testIpv6()
44+
{
45+
$IP2Location = new IP2LocationCoresController();
46+
$this->assertTrue($IP2Location->isIpv6('2001:4860:4860::8888'));
47+
}
48+
49+
public function testInvalidIpv6()
50+
{
51+
$IP2Location = new IP2LocationCoresController();
52+
$this->assertFalse($IP2Location->isIpv6('2001:4860:4860::ZZZZ'));
53+
}
54+
55+
public function testIpv4Decimal()
56+
{
57+
$IP2Location = new IP2LocationCoresController();
58+
$this->assertEquals(
59+
134744072,
60+
$IP2Location->ipv4ToDecimal('8.8.8.8')
61+
);
62+
}
63+
64+
public function testDecimalIpv4()
65+
{
66+
$IP2Location = new IP2LocationCoresController();
67+
$this->assertEquals(
68+
'8.8.8.8',
69+
$IP2Location->decimalToIpv4('134744072')
70+
);
71+
}
72+
73+
public function testIpv6Decimal()
74+
{
75+
$IP2Location = new IP2LocationCoresController();
76+
$this->assertEquals(
77+
'42541956123769884636017138956568135816',
78+
$IP2Location->ipv6ToDecimal('2001:4860:4860::8888')
79+
);
80+
}
81+
82+
public function testDecimalIpv6()
83+
{
84+
$IP2Location = new IP2LocationCoresController();
85+
$this->assertEquals(
86+
'2001:4860:4860::8888',
87+
$IP2Location->decimalToIpv6('42541956123769884636017138956568135816')
88+
);
89+
}
90+
91+
public function testIpv4ToCidr()
92+
{
93+
$IP2Location = new IP2LocationCoresController();
94+
$this->assertEqualsCanonicalizing(
95+
['8.0.0.0/8'],
96+
$IP2Location->ipv4ToCidr('8.0.0.0', '8.255.255.255')
97+
);
98+
}
99+
100+
public function testCidrToIpv4()
101+
{
102+
$IP2Location = new IP2LocationCoresController();
103+
$this->assertEqualsCanonicalizing(
104+
[
105+
'ip_start' => '8.0.0.0',
106+
'ip_end' => '8.255.255.255',
107+
],
108+
$IP2Location->cidrToIpv4('8.0.0.0/8')
109+
);
110+
}
111+
112+
public function testIpv6ToCidr()
113+
{
114+
$IP2Location = new IP2LocationCoresController();
115+
$this->assertEqualsCanonicalizing(
116+
[
117+
'2002::1234:abcd:ffff:c0a8:0/109',
118+
'2002::1234:abcd:ffff:c0b0:0/108',
119+
'2002::1234:abcd:ffff:c0c0:0/106',
120+
'2002::1234:abcd:ffff:c100:0/104',
121+
'2002::1234:abcd:ffff:c200:0/103',
122+
'2002::1234:abcd:ffff:c400:0/102',
123+
'2002::1234:abcd:ffff:c800:0/101',
124+
'2002::1234:abcd:ffff:d000:0/100',
125+
'2002::1234:abcd:ffff:e000:0/99',
126+
'2002:0:0:1234:abce::/79',
127+
'2002:0:0:1234:abd0::/76',
128+
'2002:0:0:1234:abe0::/75',
129+
'2002:0:0:1234:ac00::/70',
130+
'2002:0:0:1234:b000::/68',
131+
'2002:0:0:1234:c000::/66',
132+
],
133+
$IP2Location->ipv6ToCidr('2002:0000:0000:1234:abcd:ffff:c0a8:0000', '2002:0000:0000:1234:ffff:ffff:ffff:ffff')
134+
);
135+
}
136+
137+
public function testCidrToIpv6()
138+
{
139+
$IP2Location = new IP2LocationCoresController();
140+
$this->assertEqualsCanonicalizing(
141+
[
142+
'ip_start' => '2002:0000:0000:1234:abcd:ffff:c0a8:0101',
143+
'ip_end' => '2002:0000:0000:1234:ffff:ffff:ffff:ffff',
144+
],
145+
$IP2Location->cidrToIpv6('2002::1234:abcd:ffff:c0a8:101/64')
146+
);
147+
}
148+
149+
public function testCompressIpv6()
150+
{
151+
$IP2Location = new IP2LocationCoresController();
152+
$this->assertEquals(
153+
'2002::1234:ffff:ffff:ffff:ffff',
154+
$IP2Location->compressIpv6('2002:0000:0000:1234:ffff:ffff:ffff:ffff')
155+
);
156+
}
157+
158+
public function testExpandIpv6()
159+
{
160+
$IP2Location = new IP2LocationCoresController();
161+
$this->assertEquals(
162+
'2002:0000:0000:1234:ffff:ffff:ffff:ffff',
163+
$IP2Location->expandIpv6('2002::1234:ffff:ffff:ffff:ffff')
164+
);
165+
}
30166
}

0 commit comments

Comments
 (0)