public
Description: Is It Christmas?
Homepage: http://isitchristmas.com
Clone URL: git://github.com/isit/christmas.git
christmas / hostip.php
100644 548 lines (476 sloc) 18.339 kb
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
<?php
 
/**********************************************************************************
This is from the source to the hostip.info API, adapted for IIC.
Visit hostip.info to learn more about this community-driven geolocation database.
**********************************************************************************/
 
/************************************************************************\
|* Connect to the database
\************************************************************************/
function DBconnect($server, $user, $passwd, $dbase)
{
global $db_server, $db_user, $db_passwd, $db_dbase;
$db_server = $server;
$db_user = $user;
$db_passwd = $passwd;
$db_dbase = $dbase;
 
  global $db;
  $db = mysql_pconnect($db_server, $db_user, $db_passwd) ||
         die("Cannot connect to the database");
  
  mysql_select_db($db_dbase) ||
  die("Cannot select database '$db_dbase'");
  
  return $db;
}
 
/***********************************************************************\
|* Code to check private netblocks (and maybe bogons in the future)
\***********************************************************************/
function isPrivate($ip)
{
list($a,$b,$c,$d) = sscanf($ip, "%d.%d.%d.%d");
if ( $a === null || $b === null || $c === null || $d === null ||
$a == 10 ||
($a == 172 && $b >= 16 && $b <= 31) ||
($a == 192 && $b == 168) ||
$a == 239 ||
$a == 0 ||
$a == 127)
{
return true;
}
 
return false;
}
 
/*************************************************************************\
|* Code to lookup an IP address in the database and return an associative
|* array contining country,city,and country code, assuming these are known
\*************************************************************************/
function ipLocate($_ip)
{
$ip = trim($_ip);
 
/*********************************************************************\
|* Get the country and city (if available) for this /24
\*********************************************************************/
list($a,$b,$c,$d) = sscanf($ip, "%d.%d.%d.%d");
 
/*********************************************************************\
|* First check for illegal values
\*********************************************************************/
if(isPrivate($ip))
{
$info = array("a"=>$a, "b"=>$b, "c"=>$c, "country"=>"0", "city"=>"0",
"countryName"=>"(Private Address)",
"cityName"=>"(Private Address)",
"countryCode"=>"XX");
return $info;
}
 
/*********************************************************************\
|* Check it against the database for countries
\*********************************************************************/
$sql = "SELECT * FROM ip4_$a WHERE b=$b AND c=$c";
$result = mysql_query($sql);
 
if (@mysql_num_rows($result) == 0)
{
/*****************************************************************\
|* Return with an 'unknown' if we can't find the a,b,c records
\*****************************************************************/
$info = array("a"=>$a, "b"=>$b, "c"=>$c, "country"=>"", "city"=>"",
"countryName"=>"(Unknown Country?)",
"cityName"=>"(Unknown City?)",
"countryCode"=>"XX");
 
// mysql_query("INSERT INTO perform_api (cron, ip, hit) VALUES (NOW(), '$ip', 0)");
}
else
{
$info = mysql_fetch_assoc($result);
 
/*****************************************************************\
|* Get the country and city from the DB as well. Not done using a
|* LEFT JOIN since it seems to take longer(!) than doing the two
|* SELECT's
\*****************************************************************/
$sql = "SELECT name,lat,lng FROM cityByCountry WHERE city=".$info["city"];
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$info["cityName"] = ($row[0] == "") ? "(Unknown city)"
: urldecode($row[0]);
$info["lat"] = $row[1];
$info["lng"] = $row[2];
 
$sql = "SELECT name,code FROM countries WHERE id=".$info["country"];
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
 
if($row["name"] == "")
{
$info["countryName"] = "(Unknown country)";
}
else
{
$info["countryName"] = $row["name"];
}
 
if($row["code"] == "")
{
$info["countryCode"] = "QQ";
}
else
{
$info["countryCode"] = $row["code"];
}
 
// mysql_query("INSERT INTO perform_api (cron, ip, hit) VALUES (NOW(), '$ip', 1)");
}
 
return $info;
}
 
function ipFastCoord($ip)
{
/*********************************************************************\
|* Get the country and city (if available) for this /24
\*********************************************************************/
list($a,$b,$c,$d) = sscanf($ip, "%d.%d.%d.%d");
 
/*********************************************************************\
|* First check for illegal values
\*********************************************************************/
if(isPrivate($ip))
{
return null;
}
 
/*********************************************************************\
|* Check it against the database for countries
\*********************************************************************/
$sql = "SELECT city FROM ip4_$a WHERE b=$b AND c=$c";
$result = mysql_query($sql);
if(@mysql_num_rows($result) == 0)
{
return null;
}
        $info = mysql_fetch_assoc($result);
 
        /*****************************************************************\
|* Get the country and city from the DB as well. Not done using a
|* LEFT JOIN since it seems to take longer(!) than doing the two
|* SELECT's
\*****************************************************************/
        $sql = "SELECT lat,lng FROM cityByCountry WHERE city=".$info["city"];
        $result = mysql_query($sql);
        $row = mysql_fetch_row($result);
        $info["lat"] = $row[0];
        $info["lng"] = $row[1];
 
return $info;
}
 
function getLocation($info)
{
        /*************************************************************************\
|* Get the current location - with verbiage for missing ones
\*************************************************************************/
        if ($info["country"] == "") {
$outStr = " ... actually we haven't a clue.";
} else if ($info["country"] == "0") {
$outStr = "Private block address.";
} else if ($info["city"] == 0) {
$outStr = $info["countryName"];
        } else {
$outStr = $info["cityName"].", " . htmlentities($info["countryName"]);
        }
 
return $outStr;
}
 
function getCountry($id)
{
        /*************************************************************************\
|* Get the country info
\*************************************************************************/
        $sql = "SELECT * FROM countries WHERE id=$id";
        $result = mysql_Query($sql);
        return mysql_fetch_assoc($result);
}
 
function getTotalEntryCount()
{
        /*************************************************************************\
|* Get total entries count
\*************************************************************************/
$sql = "SELECT COUNT(*) FROM city_xref";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
 
return $row[0];
}
 
function getFlag($info)
{
        /*************************************************************************\
|* Get the flag name
\*************************************************************************/
$prefix = "/images/flags";
if ($info["country"] == "0")
{
return $prefix."/priv-".$info["a"].".gif";
}
else
{
         return $prefix."/".strtolower($info["countryCode"]).".gif";
}
}
 
function setFeedback($ip, $v)
{
if($v == "") return "";
 
/*********************************************************************\
|* First check for illegal values
\*********************************************************************/
if(isPrivate($ip))
{
return "Cannot vote a private address.";
}
 
/*************************************************************************\
|* Work out the variables
\*************************************************************************/
$y = ($v == "Y")? 1 : 0;
$n = ($v == "N")? 1 : 0;
 
/*************************************************************************\
|* Check we haven't done any locations previously today
\*************************************************************************/
$sql = "SELECT cron FROM perform WHERE ip='$ip' AND YEARWEEK(NOW()) = YEARWEEK(cron)";
$result = mysql_query($sql);
if (mysql_num_rows($result) != 0)
{
return "You can only vote once per week.";
}
else
{
$sql = "INSERT INTO perform (cron,ip,Y,N) VALUES(NOW(),'$ip',$y,$n)";
mysql_query($sql);
 
return "Thank you for your feedback!";
}
}
 
function add($cid, $state, $ip)
{
/*************************************************************************\
|* Get the details of the designated city
\*************************************************************************/
$sql = "SELECT * FROM city WHERE id=$cid";
$result = mysql_query($sql);
$M = mysql_fetch_assoc($result);
 
$country = $M["country"];
$name = $M["name"];
$lat = $M["lat"];
$lng = $M["lng"];
 
/*************************************************************************\
|* Check to see if the city exists already in the cityByCountry database
\*************************************************************************/
$sql = "SELECT * FROM cityByCountry
WHERE name='$name' AND country=$country";
$result = mysql_query($sql);
if (mysql_num_rows($result) == 0)
{
mysql_query("LOCK TABLES cityByCountry");
 
$sql = "SELECT MAX(city) FROM cityByCountry";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$cnum = $row[0] + 1;
 
$sql = "INSERT INTO cityByCountry (city,country,name,lat,lng,state)
VALUES ($cnum, $country,'$name',$lat,$lng,'$state')";
mysql_query($sql);
 
$sql = "SELECT * FROM cityByCountry
WHERE name='$name' AND country=$country";
$result = mysql_query($sql);
$C = mysql_fetch_array($result);
mysql_query("UNLOCK TABLES");
}
else
{
$C = mysql_fetch_array($result);
}
 
/*************************************************************************\
|* So we now have $C as a valid cityByCountry item
\*************************************************************************/
$city = $C["city"];
 
/*************************************************************************\
|* If we have an 'unknown' ip address, mail
\*************************************************************************/
if ($ip == "unknown")
{
ob_start();
var_dump($_SERVER);
$server = ob_get_contents();
ob_end_clean();
 
list($a,$b,$c,$d) = sscanf($ip, "%d.%d.%d.%d");
 
$sql = "SELECT * FROM ip4_$a WHERE b=$b AND c=$c";
$result = mysql_query($sql);
$exist = mysql_fetch_assoc($result);
 
mail ("updates@hostip.info", "Found 'unknown' IP address",
"city : $name\n".
"newcity : $newcity\n".
"ip : $ip\n".
"country : $country\n\n\n".
"scripts/bin/ipshow $ip\n".
"scripts/bin/ipcity $ip $city $country\n".
"scripts/bin/ipnewcity $ip '$newcity' $country\n\n\n".
$server."\n\n".$exist);
}
 
/*************************************************************************\
|* If the referrer is not from here, then mail me as well
\*************************************************************************/
/*
if (strstr($_SERVER[HTTP_REFERER], "hostip.info") == "")
{
ob_start();
var_dump($_SERVER);
$server = ob_get_contents();
ob_end_clean();
 
list($a,$b,$c,$d) = sscanf($ip, "%d.%d.%d.%d");
 
$sql = "SELECT * FROM ip4_$a WHERE b=$b AND c=$c";
$result = mysql_query($sql);
 
ob_start();
var_dump(mysql_fetch_assoc($result));
$exist = ob_get_contents();
ob_end_clean();
mail ("updates@hostip.info", "Referrer not set - check site",
"city : $name\n".
"newcity : $newcity\n".
"ip : $ip\n".
"country : $country\n\n\n".
"scripts/bin/ipshow $ip\n".
"scripts/bin/ipcity $ip $city $country\n".
"scripts/bin/ipnewcity $ip '$newcity' $country\n\n\n".
$server."\n\n".$exist);
}
*/
/*************************************************************************\
|* Update the record for this IP address
\*************************************************************************/
# $fp = fopen("/tmp/add.log", "a");
# fputs($fp,"$ip|".$_SERVER["REMOTE_ADDR"]."|$city||$country|".date("Y-m-d H:i:s")."\n");
# fclose($fp);
 
list($a,$b,$c,$d) = sscanf($ip, "%d.%d.%d.%d");
 
$sql = "SELECT COUNT(*) FROM ip4_$a WHERE b=$b AND c=$c";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$num = $row[0];
 
if ($num == 0)
{
mysql_query("INSERT INTO ip4_$a (b,c,country,city,cron) VALUES
($b,$c,$country,$city,NOW())");
}
else
{
mysql_query("UPDATE ip4_$a
SET country=$country, city=$city
WHERE b=$b AND c=$c");
}
 
mysql_query("INSERT INTO ip4_add (cron, a, b, c, d, country, city)
VALUES (NOW(), $a, $b, $c, $d, $country, $city)");
}
 
/************************************************************************\
|* Centralised code to get the remote IP address, even if it's behind a
|* proxy server.
\************************************************************************/
 
function getIp()
{
if ($_SERVER["HTTP_X_FORWARDED_FOR"] != "")
{
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
// proxies _can_ return multiple ips
$ip_array = explode(",", $ip);
$ip = $ip_array[0];
 
$nums = sscanf($ip, "%d.%d.%d.%d");
if ($nums[0] === null ||
$nums[1] === null ||
$nums[2] === null ||
$nums[3] === null ||
$nums[0] == 10 ||
($nums[0] == 172 && $nums[1] >= 16 && $nums[1] <= 31) ||
($nums[0] == 192 && $nums[1] == 168) ||
$nums[0] == 239 ||
$nums[0] == 0 ||
$nums[0] == 127)
$ip = $_SERVER["REMOTE_ADDR"];
}
else if ($_SERVER["HTTP_CLIENT_IP"] != "")
{
$ip = $_SERVER["HTTP_CLIENT_IP"];
$nums = sscanf($ip, "%d.%d.%d.%d");
if ($nums[0] === null ||
$nums[1] === null ||
$nums[2] === null ||
$nums[3] === null ||
$nums[0] == 10 ||
($nums[0] == 172 && $nums[1] >= 16 && $nums[1] <= 31) ||
($nums[0] == 192 && $nums[1] == 168) ||
$nums[0] == 239 ||
$nums[0] == 0 ||
$nums[0] == 127)
$ip = $_SERVER["REMOTE_ADDR"];
}
else
$ip = $_SERVER["REMOTE_ADDR"];
 
return $ip;
}
 
/*************************************************************************\
|* Code to lookup an IP address in the database and return an associative
|* array contining country,city,and country code, assuming these are known
\*************************************************************************/
function ipRoughLocate($ip)
{
/*********************************************************************\
|* Get the country and city (if available) for this /24
\*********************************************************************/
list($a,$b,$c,$d) = sscanf($ip, "%d.%d.%d.%d");
$guessed = "false";
 
/*********************************************************************\
|* First check for illegal values
\*********************************************************************/
if(isPrivate($ip))
{
$info = array("a"=>$a, "b"=>$b, "c"=>$c, "country"=>"0",
"city"=>"0",
"countryName"=>"(Private Address)",
"cityName"=>"(Private Address)",
"countryCode"=>"XX");
$info["guessed"] = $guessed;
return $info;
}
 
/*********************************************************************\
|* Check it against the database for countries
\*********************************************************************/
$sql = "SELECT * FROM ip4_$a WHERE b=$b AND c=$c";
$result = mysql_query($sql);
 
if (mysql_num_rows($result) == 0)
{
$info = array("a"=>$a, "b"=>$b, "c"=>$c, "country"=>"", "city"=>"",
"countryName"=>"(Unknown Country?)",
"cityName"=>"(Unknown City?)",
"countryCode"=>"XX");
$info["guessed"] = $guessed;
}
else
{
$info = mysql_fetch_assoc($result);
 
/*****************************************************************\
|* If the city is '0', then try again, with a broader range of
|* possible 'c' matches
\*****************************************************************/
if ($info["city"] == 0)
{
$lo = ($c < 8) ? 0 : $c-8;
$hi = ($c > 246) ? 255 : $c+8;
 
$sql = "SELECT * FROM ip4_$a
WHERE b=$b
AND c >= $lo
AND c <= $hi
AND city != 0";
$result = mysql_query($sql);
if (mysql_num_rows($result) > 0)
$info = mysql_fetch_assoc($result);
 
$guessed = "true";
}
 
/*****************************************************************\
|* Get the country and city from the DB as well. Not done using a
|* LEFT JOIN since it seems to take longer(!) than doing the two
|* SELECT's
\*****************************************************************/
$sql = "SELECT name,lat,lng FROM cityByCountry WHERE city=".$info["city"];
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$info["cityName"] = ($row[0] == "") ? "(Unknown city)" : $row[0];
                $info["lat"] = $row[1];
                $info["lng"] = $row[2];
 
$sql = "SELECT name,code FROM countries WHERE id=".$info["country"];
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$info["countryName"] = ($row["name"] == "") ?
"(Unknown country)" : $row["name"];
$info["countryCode"] = ($row["code"] == "") ?
"QQ" : $row["code"];
 
$info["guessed"] = $guessed;
}
 
return $info;
}
 
?>