1: <?php
2: namespace Module\IP2Country;
3:
4: require_once(DIRBASE . '/modules/enabled/file-system-database/class.fsdb.php');
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: class Converter {
41: private $db;
42:
43: private $upperRange = array(
44: 50000000, 100000000, 150000000, 200000000, 250000000, 300000000,
45: 350000000, 400000000, 450000000, 500000000, 550000000, 600000000,
46: 650000000, 700000000, 750000000, 800000000, 850000000, 900000000,
47: 950000000, 1000000000, 1050000000, 1100000000, 1150000000, 1200000000,
48: 1250000000, 1300000000, 1350000000, 1400000000, 1450000000, 1500000000,
49: 1550000000, 1600000000, 1650000000, 1700000000, 1750000000, 1800000000,
50: 1850000000, 1900000000, 1950000000, 2000000000, 2050000000, 2100000000,
51: 2150000000, 2200000000, 2250000000, 2300000000, 2350000000, 2400000000,
52: 2450000000, 2500000000, 2550000000, 2600000000, 2650000000, 2700000000,
53: 2750000000, 2800000000, 2850000000, 2900000000, 2950000000, 3000000000,
54: 3050000000, 3100000000, 3150000000, 3200000000, 3250000000, 3300000000,
55: 3350000000, 3400000000, 3450000000, 3500000000, 3550000000, 3600000000,
56: 3650000000, 3700000000, 3750000000, 3800000000, 3850000000, 3900000000,
57: 3950000000, 4000000000
58: );
59:
60: public function __construct() {
61: $this->db = new \FSDB\Connection('./ipData/');
62: }
63:
64: public function getCountryCode($ip) {
65: $dec = ip2long($ip);
66: $data = $this->db->select($this->getTable($ip), "*");
67: foreach ($data as $row) {
68: if ($row->startIP <= $dec && $row->endIP >= $dec) {
69: return $row->countryCode;
70: }
71: }
72:
73: return false;
74: }
75:
76: public function getTable($ip) {
77: $dec = ip2long($ip);
78:
79: foreach ($this->upperRange as $limit) {
80: if ($dec >= $limit - 50000000 && $dec <= $limit) {
81: return (string) $limit;
82: }
83: }
84:
85: return false;
86: }
87: }