-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphpip2country.class.php
294 lines (271 loc) · 8.11 KB
/
phpip2country.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
/**
* phpIp2Country class
*
* @author Mariusz Górski
* @copyright 2008 Mariusz Górski
* @name phpIp2Country
* @version 1.0
* @link http://code.google.com/p/php-ip-2-country/
*
* @todo (static?) function to get full countries list
*/
define('IP_STR',1);
define('IP_VALUE',2);
define('IP_RANGE_NUMERICAL',3);
define('IP_RANGE',4);
define('IP_REGISTRY',5);
define('IP_ASSIGNED_UNIXTIME',6);
define('IP_COUNTRY_ISO',7);
define('IP_COUNTRY_CODE',8);
define('IP_COUNTRY_NAME',9);
define('IP_INFO',10);
class phpIp2Country {
/**
* @param string $ip
* @param ip $method
*/
function __construct($ip,$dbConfig=array()){
if(!$this->chceckIpAddr($ip)){
die('Bad IP address! Should be in xxx.xxx.xxx.xxx format!');
}else{
$this->ip = $ip;
}
if(!is_array($dbConfig)){
die('Error! Database configuration not set! #1');
}else{
$this->dbConfig = $dbConfig;
}
$this->dbConnect();
$this->ipArr = $this->getIpArr();
$this->ipValue = $this->getIpValue();
$this->ipInfoArr = $this->dbGetRow($this->getIpSelectSQL());
if(!$this->ipInfoArr){
die ('Error during reciving informations about IP address!');
}else{
$this->ipInfoArr['IP_STR'] = $this->ip;
$this->ipInfoArr['IP_VALUE'] = $this->ipValue;
$this->ipInfoArr['IP_FROM_STR'] = $this->getIpFromValue($this->ipInfoArr['IP_FROM']);
$this->ipInfoArr['IP_TO_STR'] = $this->getIpFromValue($this->ipInfoArr['IP_TO']);
}
}
function __destruct(){
if($this->db)
mysql_close($this->db);
}
/**
* IP address
*
* @var string
*/
public $ip = '';
/**
* Numerical representation of IP address
* Example: (from Right to Left)
* 1.2.3.4 = 4 + (3 * 256) + (2 * 256 * 256) + (1 * 256 * 256 * 256)
* is 4 + 768 + 13,1072 + 16,777,216 = 16,909,060
* @var integer
*/
private $ipValue = NULL;
/**
* database conection configuration
* feel free to replece our db conection and use Your favorite database abstraction layer (ie. ADOdb)
*
* @var array
*/
public $dbConfig = array();
/**
* database conection object
* feel free to replece our db conection and use Your favorite database abstraction layer (ie. ADOdb)
*
* @var object
*/
public $db = false;
/**
* IP address in form of array of integer values
*
* @var string
*/
private $ipArr = array();
/**
* IP address information array
*
* @var string
*/
private $ipInfoArr = false;
/**
* returns information about IP adrress
*
* @param integer $mode
* @return mixed
*/
public function getInfo($mode=IP_INFO){
if(!in_array($mode,array( IP_STR , IP_VALUE, IP_RANGE_NUMERICAL, IP_RANGE, IP_REGISTRY, IP_ASSIGNED_UNIXTIME , IP_COUNTRY_ISO, IP_COUNTRY_CODE, IP_COUNTRY_NAME, IP_INFO, ))){
die('Error! Bad getInfo() mode!');
}else switch($mode){
case IP_STR:
return $this->ipInfoArr['IP_STR'];
break;
case IP_VALUE:
return $this->ipInfoArr['IP_VALUE'];
break;
case IP_RANGE_NUMERICAL:
return array(
'FROM' => $this->ipInfoArr['IP_FROM'],
'TO' => $this->ipInfoArr['IP_TO']
);
break;
case IP_RANGE:
return array(
'FROM' => $this->ipInfoArr['IP_FROM_STR'],
'TO' => $this->ipInfoArr['IP_TO_STR']
);
break;
case IP_REGISTRY:
return $this->ipInfoArr['REGISTRY'];
break;
case IP_ASSIGNED_UNIXTIME:
return $this->ipInfoArr['ASSIGNED'];
break;
case IP_COUNTRY_ISO:
return $this->ipInfoArr['CTRY'];
break;
case IP_COUNTRY_CODE:
return $this->ipInfoArr['CNTRY'];
break;
case IP_COUNTRY_NAME:
return $this->ipInfoArr['COUNTRY'];
break;
case IP_INFO:
default:
return $this->ipInfoArr;
break;
}
}
/**
* validate IP address
*
* @param string $ip
* @return boolean
*/
private function chceckIpAddr($ip=''){
return preg_match('/^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/i',$ip);
}
/**
* returns IP address in array of integer values
*
* @return array
*/
private function getIpArr(){
$vars = explode('.',$this->ip);
return array(
intval($vars[0]),
intval($vars[1]),
intval($vars[2]),
intval($vars[3])
);
}
/**
* returns numerical representation of IP address.
* Example: (from Right to Left)
* 1.2.3.4 = 4 + (3 * 256) + (2 * 256 * 256) + (1 * 256 * 256 * 256)
* is 4 + 768 + 13,1072 + 16,777,216 = 16,909,060
*
* @return integer
*/
private function getIpValue(){
return $this->ipArr[3] + ( $this->ipArr[2] * 256 ) + ( $this->ipArr[1] * 256 * 256 ) + ( $this->ipArr[0] * 256 * 256 * 256 );
}
/**
* returns IP numer from numerical representation.
* Example: (from Right to Left)
* 1.2.3.4 = 4 + (3 * 256) + (2 * 256 * 256) + (1 * 256 * 256 * 256)
* is 4 + 768 + 13,1072 + 16,777,216 = 16,909,060
*
* @param integer $value
* @param boolean $returnAsStr
* @return mixed
*/
private function getIpFromValue($value=0,$returnAsStr=true){
$ip[0] = floor( intval($value) / (256*256*256) );
$ip[1] = floor( ( intval($value) - $ip[0]*256*256*256 ) / (256*256) );
$ip[2] = floor( ( intval($value) -$ip[0]*256*256*256 -$ip[1]*256*256 ) / 256 );
$ip[3] = intval($value) - $ip[0]*256*256*256 - $ip[1]*256*256 - $ip[2]*256;
if($returnAsStr){
return $ip[0].'.'.$ip[1].'.'.$ip[2].'.'.$ip[3];
}else{
return $ip;
}
}
/**
* returns SQL used to get iformation from ip2country database
*
* @return string
*/
private function getIpSelectSQL(){
if(empty($this->dbConfig['tableName'])){
$this->dbConfig['tableName'] = 'ip_to_country'; //setting default mysql port name
echo "phpPp2Country table name not selected! traying default value: '".$this->dbConfig['tableName']."'";
}
return 'SELECT * FROM '.$this->dbConfig['tableName'].' WHERE IP_FROM <= '.$this->ipValue.' AND IP_TO >= '.$this->ipValue;
}
/**
* connect to database
* feel free to replece our function and use here Your favorite(s) database abstraction layer (ie. ADOdb)
*
* @return object - database conection resource
*/
private function dbConnect(){
if(is_array($this->dbConfig)){
if(empty($this->dbConfig['host'])){
$this->dbConfig['host'] = 'localhost'; //setting default mysql port name
echo "Database connection host not selected! traying default value: '".$this->dbConfig['port']."'";
}
if(intval($this->dbConfig['port']==0)){
$this->dbConfig['port'] = 3306; //setting default mysql port name
echo "Database connection port not selected! traying default value: '".$this->dbConfig['port']."'";
}
if(empty($this->dbConfig['dbUserName'])){
$this->dbConfig['dbUserName'] = 'ip_to_country'; //setting default mysql port name
echo "Database connection host not selected! traying default value: '".$this->dbConfig['dbUserName']."'";
}
if(empty($this->dbConfig['dbUserPassword'])){
$this->dbConfig['dbUserPassword'] = 'xxx'; //setting default mysql port name
echo "Database connection host not selected! traying default value: '".$this->dbConfig['dbUserPassword']."'";
}
$this->db = mysql_connect($this->dbConfig['host'].':'.$this->dbConfig['port'], $this->dbConfig['dbUserName'], $this->dbConfig['dbUserPassword']);
if (!$this->db) {
die('Database connection error: ' . mysql_error());
}else{
if(empty($this->dbConfig['dbName'])){
$this->dbConfig['dbName'] = 'ip_to_country'; //setting default mysql port name
echo "Database connection host not selected! traying default value: '".$this->dbConfig['dbName']."'";
}
if( !mysql_select_db( $this->dbConfig['dbName'] , $this->db ) ){
die("Error during selecting database '".$this->dbConfig['dbName']."' : ". mysql_error());
}else{
return true;
}
}
}else{
die('Error! Database configuration not set! #2');
}
}
/**
* executes given SQL querry and returns one row
* feel free to replece our function and use here Your favorite(s) database abstraction layer (ie. ADOdb)
*
* @param string $sql
* @return array
*/
private function dbGetRow($sql){
$result = mysql_query($sql);
if($result){
$row = mysql_fetch_assoc($result);
if($row){
return $row;
}
}
die("Error during database querry:" . mysql_error());
}
}