Skip to content

Commit bb49845

Browse files
committed
Changed public interfaces.
1 parent 97ef394 commit bb49845

File tree

3 files changed

+89
-28
lines changed

3 files changed

+89
-28
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ $db = new \IP2Proxy\Database('./samples/IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CIT
2323
To start lookup result from database, use the following codes:
2424

2525
```
26-
$records = $db->lookup('1.0.241.135', \IP2Proxy\Database::ALL);
26+
$records = $db->getAll('1.0.241.135');
2727
```
2828

2929
Results are returned in array.

class.IP2Proxy.php

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Database {
3232
*
3333
* @var string
3434
*/
35-
const VERSION = '1.0.0';
35+
const VERSION = '1.1.0';
3636

3737
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
3838
// Error field constants ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -1147,22 +1147,12 @@ private function binSearch($version, $ipNumber) {
11471147
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
11481148

11491149
/**
1150-
* Get the database's compilation date as a string of the form 'YYYY-MM-DD'
1151-
*
1152-
* @access public
1153-
* @return string
1154-
*/
1155-
public function getDate() {
1156-
return $this->date;
1157-
}
1158-
1159-
/**
1160-
* Get the database's type (1-4)
1150+
* Get the database's package (1-4)
11611151
*
11621152
* @access public
11631153
* @return int
11641154
*/
1165-
public function getType() {
1155+
public function getPackageVersion() {
11661156
return $this->type + 1;
11671157
}
11681158

@@ -1203,6 +1193,62 @@ public function getDatabaseVersion() {
12031193
return $this->year . '.' . $this->month . '.' . $this->day;
12041194
}
12051195

1196+
/**
1197+
* Return -1, 0, 1, 2
1198+
*/
1199+
public function isProxy($ip) {
1200+
return self::lookup($ip, self::IS_PROXY);
1201+
}
1202+
1203+
/*
1204+
* Return string
1205+
*/
1206+
public function getCountryShort($ip) {
1207+
return self::lookup($ip, self::COUNTRY_CODE);
1208+
}
1209+
1210+
/*
1211+
* Return string
1212+
*/
1213+
public function getCountryLong($ip) {
1214+
return self::lookup($ip, self::COUNTRY_NAME);
1215+
}
1216+
1217+
/*
1218+
* Return string
1219+
*/
1220+
public function getRegion($ip) {
1221+
return self::lookup($ip, self::REGION_NAME);
1222+
}
1223+
1224+
/*
1225+
* Return string
1226+
*/
1227+
public function getCity($ip) {
1228+
return self::lookup($ip, self::CITY_NAME);
1229+
}
1230+
1231+
/*
1232+
* Return string
1233+
*/
1234+
public function getISP($ip) {
1235+
return self::lookup($ip, self::ISP);
1236+
}
1237+
1238+
/*
1239+
* Return string
1240+
*/
1241+
public function getProxyType($ip) {
1242+
return self::lookup($ip, self::PROXY_TYPE);
1243+
}
1244+
1245+
/*
1246+
* Return array
1247+
*/
1248+
public function getAll($ip) {
1249+
return self::lookup($ip, self::ALL);
1250+
}
1251+
12061252
/**
12071253
* This function will look the given IP address up in the database and return the result(s) asked for
12081254
*
@@ -1211,13 +1257,13 @@ public function getDatabaseVersion() {
12111257
* array whith the returned singular field names as keys and their corresponding
12121258
* values is returned.
12131259
*
1214-
* @access public
1260+
* @access protected
12151261
* @param string $ip IP address to look up
12161262
* @param int|array $fields Field(s) to return
12171263
* @param boolean $asNamed Whether to return an associative array instead
12181264
* @return mixed|array|boolean
12191265
*/
1220-
public function lookup($ip, $fields = null, $asNamed = true) {
1266+
protected function lookup($ip, $fields = null, $asNamed = true) {
12211267
// extract IP version and number
12221268
list($ipVersion, $ipNumber) = self::ipVersionAndNumber($ip);
12231269
// perform the binary search proper (if the IP address was invalid, binSearch will return false)

example.php

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,30 @@
22
require 'class.IP2Proxy.php';
33

44
$db = new \IP2Proxy\Database('./samples/IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP.SAMPLE.BIN', \IP2Proxy\Database::FILE_IO);
5-
$records = $db->lookup('1.0.241.135', \IP2Proxy\Database::ALL);
6-
7-
echo '<p><strong>IP Address: </strong>' . $records['ipAddress'] . '</p>';
8-
echo '<p><strong>IP Number: </strong>' . $records['ipNumber'] . '</p>';
9-
echo '<p><strong>IP Version: </strong>' . $records['ipVersion'] . '</p>';
10-
echo '<p><strong>Country Code: </strong>' . $records['countryCode'] . '</p>';
11-
echo '<p><strong>Country: </strong>' . $records['countryName'] . '</p>';
12-
echo '<p><strong>State: </strong>' . $records['regionName'] . '</p>';
13-
echo '<p><strong>City: </strong>' . $records['cityName'] . '</p>';
14-
echo '<p><strong>Proxy Type: </strong>' . $records['proxyType'] . '</p>';
15-
echo '<p><strong>Is Proxy: </strong>' . $records['isProxy'] . '</p>';
16-
echo '<p><strong>ISP: </strong>' . $records['isp'] . '</p>';
5+
6+
$countryCode = $db->getCountryShort('1.0.241.135');
7+
echo '<p><strong>Country Code: </strong>' . $countryCode . '</p>';
8+
9+
$countryName = $db->getCountryLong('1.0.241.135');
10+
echo '<p><strong>Country: </strong>' . $countryName . '</p>';
11+
12+
$regionName = $db->getRegion('1.0.241.135');
13+
echo '<p><strong>Region: </strong>' . $regionName . '</p>';
14+
15+
$cityName = $db->getCity('1.0.241.135');
16+
echo '<p><strong>City: </strong>' . $cityName . '</p>';
17+
18+
$isp = $db->getISP('1.0.241.135');
19+
echo '<p><strong>ISP: </strong>' . $isp . '</p>';
20+
21+
$proxyType = $db->getProxyType('1.0.241.135');
22+
echo '<p><strong>Proxy Type: </strong>' . $proxyType . '</p>';
23+
24+
$isProxy = $db->isProxy('1.0.241.135');
25+
echo '<p><strong>Is Proxy: </strong>' . $isProxy . '</p>';
26+
27+
$records = $db->getAll('1.0.241.135');
28+
29+
echo '<pre>';
30+
print_r($records);
31+
echo '</pre>';

0 commit comments

Comments
 (0)