Skip to content

Commit 90aaba0

Browse files
First commit
1 parent 19125de commit 90aaba0

15 files changed

Lines changed: 3044 additions & 2 deletions

.gitattributes

Lines changed: 0 additions & 2 deletions
This file was deleted.

Controllers/IP2Location_test.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php namespace App\Controllers;
2+
3+
use App\Libraries\IP2Location_lib;
4+
use App\Models\IP2Location_model;
5+
6+
date_default_timezone_set('Etc/UTC');
7+
8+
define('IP2LOCATION_DATABASE_TABLE', 'YOUR IP2LOCATION TABLE NAME');
9+
10+
class IP2Location_test extends BaseController {
11+
public function index() {
12+
$ipl = new IP2Location_lib();
13+
14+
// BIN Database
15+
$countryCode = $ipl->getCountryCode('8.8.8.8');
16+
17+
echo '<p>Country code for 8.8.8.8: ' . $countryCode . '</p>';
18+
19+
echo '
20+
<div>You can download the latest BIN database at
21+
<ul>
22+
<li><a href="https://lite.ip2location.com">IP2Location LITE BIN Database (Free)</a></li>
23+
<li><a href="https://www.ip2location.com">IP2Location BIN Database (Comprehensive)</a></li>
24+
</ul>
25+
</div>';
26+
27+
// Web Service
28+
echo '<pre>';
29+
print_r ($ipl->getWebService('8.8.8.8'));
30+
echo '</pre>';
31+
32+
// MySQL Query
33+
$db = model('IP2Location_model', false);
34+
$data = $db->lookup('8.8.8.8');
35+
echo '<pre>';
36+
print_r ($data);
37+
echo '</pre>';
38+
}
39+
}

LICENSE.TXT

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

Libraries/IP2Location/Country.php

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
namespace IP2Location;
4+
5+
/**
6+
* Country class.
7+
*/
8+
class Country
9+
{
10+
/**
11+
* Unable to locate CSV file.
12+
*
13+
* @var int
14+
*/
15+
public const EXCEPTION_FILE_NOT_EXISTS = 10000;
16+
17+
/**
18+
* Invalid CSV file.
19+
*
20+
* @var int
21+
*/
22+
public const EXCEPTION_INVALID_CSV = 10001;
23+
24+
/**
25+
* Unable to read the CSV file.
26+
*
27+
* @var int
28+
*/
29+
public const EXCEPTION_UNABLE_TO_OPEN_CSV = 10002;
30+
31+
/**
32+
* No record found.
33+
*
34+
* @var int
35+
*/
36+
public const EXCEPTION_NO_RECORD = 10003;
37+
38+
/**
39+
* Fields from CSV.
40+
*
41+
* @var array
42+
*/
43+
protected $fields = [];
44+
45+
/**
46+
* Records from CSV.
47+
*
48+
* @var array
49+
*/
50+
protected $records = [];
51+
52+
/**
53+
* Constructor.
54+
*
55+
* @param string $csv Path to CSV file
56+
*
57+
* @throws \Exception
58+
*/
59+
public function __construct($csv)
60+
{
61+
if (!file_exists($csv)) {
62+
throw new \Exception(__CLASS__ . ': The CSV file "' . $csv . '" is not found.', self::EXCEPTION_FILE_NOT_EXISTS);
63+
}
64+
65+
$file = fopen($csv, 'r');
66+
67+
if (!$file) {
68+
throw new \Exception(__CLASS__ . ': Unable to read "' . $csv . '".', self::EXCEPTION_UNABLE_TO_OPEN_CSV);
69+
}
70+
71+
$line = 1;
72+
73+
while (!feof($file)) {
74+
$data = fgetcsv($file);
75+
76+
if (!$data) {
77+
++$line;
78+
continue;
79+
}
80+
81+
// Parse the CSV fields
82+
if ($line == 1) {
83+
if ($data[0] != 'country_code') {
84+
throw new \Exception(__CLASS__ . ': Invalid country information CSV file.', self::EXCEPTION_INVALID_CSV);
85+
}
86+
87+
$this->fields = $data;
88+
} else {
89+
$this->records[$data[0]] = $data;
90+
}
91+
92+
++$line;
93+
}
94+
}
95+
96+
/**
97+
* Get the country information.
98+
*
99+
* @param string $countryCode The country ISO 3166 country code.
100+
*
101+
* @throws \Exception
102+
*
103+
* @return array
104+
*/
105+
public function getCountryInfo($countryCode = '')
106+
{
107+
if (empty($this->records)) {
108+
throw new \Exception(__CLASS__ . ': No record available.', self::EXCEPTION_NO_RECORD);
109+
}
110+
111+
$results = [];
112+
113+
if ($countryCode) {
114+
if (!isset($this->records[$countryCode])) {
115+
return [];
116+
}
117+
118+
for ($i = 0; $i < \count($this->fields); ++$i) {
119+
$results[$this->fields[$i]] = $this->records[$countryCode][$i];
120+
}
121+
122+
return $results;
123+
}
124+
125+
foreach ($this->records as $record) {
126+
$data = [];
127+
128+
for ($i = 0; $i < \count($this->fields); ++$i) {
129+
$data[$this->fields[$i]] = $record[$i];
130+
}
131+
132+
$results[] = $data;
133+
}
134+
135+
return $results;
136+
}
137+
}

0 commit comments

Comments
 (0)