1 |
matthys |
15 |
<?php |
2 |
|
|
/* This file is part of BBClone (A PHP based Web Counter on Steroids) |
3 |
|
|
* |
4 |
matthys |
16 |
* SVN FILE $Id$ |
5 |
matthys |
15 |
* |
6 |
matthys |
16 |
* Copyright (C) 2001-2013, the BBClone Team (see doc/authors.txt for details) |
7 |
matthys |
15 |
* |
8 |
|
|
* This program is free software: you can redistribute it and/or modify |
9 |
|
|
* it under the terms of the GNU General Public License as published by |
10 |
|
|
* the Free Software Foundation, either version 3 of the License, or |
11 |
|
|
* (at your option) any later version. |
12 |
|
|
* |
13 |
|
|
* This program is distributed in the hope that it will be useful, |
14 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 |
|
|
* GNU General Public License for more details. |
17 |
|
|
* |
18 |
|
|
* See doc/copying.txt for details |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
////////////////////////////////////////// |
22 |
|
|
// Plug-in: Extension look-up by IP2EXT // |
23 |
|
|
////////////////////////////////////////// |
24 |
|
|
|
25 |
|
|
function bbc_extension_plugin($host, $addr) { |
26 |
|
|
global $BBC_IP2EXT_PATH; |
27 |
|
|
|
28 |
|
|
// generic extensions which need to be looked up first |
29 |
|
|
$gen_ext = array( |
30 |
|
|
"ac", "aero", "ag", "arpa", "as", "biz", "cc", "cd", "com", "coop", "cx", "edu", "eu", "gb", "gov", "gs", "info", |
31 |
|
|
"int", "mil", "ms", "museum", "name", "net", "nu", "org", "pro", "sc", "st", "su", "tk", "to", "tv", "vu", "ws" |
32 |
|
|
); |
33 |
|
|
// hosts with reliable country extension don't need to be looked up |
34 |
|
|
$cnt_ext = array( |
35 |
|
|
"ad", "ae", "af", "ai", "al", "am", "an", "ao", "aq", "ar", "at", "au", "aw", "az", "ba", "bb", "bd", "be", "bf", |
36 |
|
|
"bg", "bh", "bi", "bj", "bm", "bn", "bo", "br", "bs", "bt", "bv", "bw", "by", "bz", "ca", "cf", "cg", "ch", "ci", |
37 |
|
|
"ck", "cl", "cm", "cn", "co", "cr", "cs", "cu", "cv", "cy", "cz", "de", "dj", "dk", "dm", "do", "dz", "ec", "ee", |
38 |
|
|
"eg", "eh", "er", "es", "et", "fi", "fj", "fk", "fm", "fo", "fr", "ga", "gd", "ge", "gf", "gg", "gh", "gi", "gl", |
39 |
|
|
"gm", "gn", "gp", "gq", "gr", "gt", "gu", "gw", "gy", "hk", "hm", "hn", "hr", "ht", "hu", "id", "ie", "il", "im", |
40 |
|
|
"in", "io", "iq", "ir", "is", "it", "je", "jm", "jo", "jp", "ke", "kg", "kh", "ki", "km", "kn", "kp", "kr", "kw", |
41 |
|
|
"ky", "kz", "la", "lb", "lc", "li", "lk", "lr", "ls", "lt", "lu", "lv", "ly", "ma", "mc", "md", "mg", "mh", "mk", |
42 |
|
|
"ml", "mm", "mn", "mo", "mp", "mq", "mr", "mt", "mu", "mv", "mw", "mx", "my", "mz", "na", "nc", "ne", "nf", "ng", |
43 |
|
|
"ni", "nl", "no", "np", "nr", "nz", "om", "pa", "pe", "pf", "pg", "ph", "pk", "pl", "pm", "pn", "pr", "ps", "pt", |
44 |
|
|
"pw", "py", "qa", "re", "ro", "ru", "rw", "sa", "sb", "sd", "se", "sg", "sh", "si", "sj", "sk", "sl", "sm", "sn", |
45 |
|
|
"so", "sr", "sv", "sy", "sz", "tc", "td", "tf", "tg", "th", "tj", "tl", "tm", "tn", "tp", "tr", "tt", "tw", "tz", |
46 |
|
|
"ua", "ug", "uk", "um", "us", "uy", "uz", "va", "vc", "ve", "vg", "vi", "vn", "wf", "ye", "yt", "yu", "za", "zm", |
47 |
|
|
"zr", "zw" |
48 |
|
|
); |
49 |
|
|
|
50 |
|
|
$file = $BBC_IP2EXT_PATH.(substr($addr, 0, strpos($addr, ".")).".inc"); |
51 |
|
|
$ext = strtolower(substr($host, (strrpos($host, ".") + 1))); |
52 |
|
|
|
53 |
|
|
// Don't look up if there's already a country extension |
54 |
|
|
if (in_array($ext, $cnt_ext)) return $ext; |
55 |
|
|
if (!is_readable($file)) return ""; |
56 |
|
|
|
57 |
|
|
$long = ip2long($addr); |
58 |
|
|
$long = sprintf("%u", $long); |
59 |
|
|
$fp = fopen($file, "rb"); |
60 |
|
|
|
61 |
|
|
$db_ext = ""; |
62 |
|
|
while (($range = fgetcsv($fp, 64, "|")) !== false) { |
63 |
|
|
if (($long >= $range[1]) && ($long <= ($range[1] + $range[2] - 1))) { |
64 |
|
|
// don't hose our stats if the database returns an unexpected extension |
65 |
|
|
$db_ext = (in_array($range[0], $cnt_ext) || in_array($range[0], $gen_ext)) ? $range[0] : ""; |
66 |
|
|
break; |
67 |
|
|
} |
68 |
|
|
} |
69 |
|
|
fclose($fp); |
70 |
|
|
|
71 |
|
|
return $db_ext; |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
?> |