Skip to content

Instantly share code, notes, and snippets.

@e-oz
Created February 13, 2012 06:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save e-oz/1814226 to your computer and use it in GitHub Desktop.
Save e-oz/1814226 to your computer and use it in GitHub Desktop.
old sypex geo with caching
<?php
namespace Sypex\Geo;
/***************************************************************************\
| Sypex Geo version 1.0.0 beta 2 |
| (c)2006 zapimir zapimir@zapimir.net http://sypex.net/ |
| (c)2006 BINOVATOR info@sypex.net |
|---------------------------------------------------------------------------|
| created: 2006.10.17 18:33 modified: 2006.10.26 22:07 |
|---------------------------------------------------------------------------|
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
| |
| You should have received a copy of the GNU General Public License |
| along with this program; if not, write to the Free Software |
| Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA. |
\***************************************************************************/
class SypexGeo
{
var $info;
var $index;
var $num2cc;
var $database;
function SypexGeo($filename)
{
$mem = null;
$mem = new \Jamm\Memory\APCObject('sypex');
$this->info = $mem->read('info');
$this->index = $mem->read('index');
$this->database = $mem->read('database');
if (empty($this->info) || empty($this->index) || empty($this->database))
{
$fh = fopen($filename, 'rb');
$this->info = unpack('Cidx_len/Nitems', fread($fh, 5));
$this->info['range'] = ceil($this->info['items']/($this->info['idx_len']+1));
$this->info['idx_len'] *= 5;
$this->info['items'] *= 5;
$this->info['from'] = $this->info['idx_len']+5;
$this->index = fread($fh, $this->info['idx_len']);
$this->database = fread($fh, $this->info['items']);
fclose($fh);
if (is_object($mem))
{
$mem->save('info', $this->info, 259200);
$mem->save('index', $this->index, 259200);
$mem->save('database', $this->database, 259200);
}
}
$this->num2cc = array(
'', 'AP', 'EU', 'AD', 'AE', 'AF', 'AG', 'AI', 'AL', 'AM', 'AN', 'AO', 'AQ',
'AR', 'AS', 'AT', 'AU', 'AW', 'AZ', 'BA', 'BB', 'BD', 'BE', 'BF', 'BG', 'BH',
'BI', 'BJ', 'BM', 'BN', 'BO', 'BR', 'BS', 'BT', 'BV', 'BW', 'BY', 'BZ', 'CA',
'CC', 'CD', 'CF', 'CG', 'CH', 'CI', 'CK', 'CL', 'CM', 'CN', 'CO', 'CR', 'CU',
'CV', 'CX', 'CY', 'CZ', 'DE', 'DJ', 'DK', 'DM', 'DO', 'DZ', 'EC', 'EE', 'EG',
'EH', 'ER', 'ES', 'ET', 'FI', 'FJ', 'FK', 'FM', 'FO', 'FR', 'FX', 'GA', 'GB',
'GD', 'GE', 'GF', 'GH', 'GI', 'GL', 'GM', 'GN', 'GP', 'GQ', 'GR', 'GS', 'GT',
'GU', 'GW', 'GY', 'HK', 'HM', 'HN', 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IN',
'IO', 'IQ', 'IR', 'IS', 'IT', 'JM', 'JO', 'JP', 'KE', 'KG', 'KH', 'KI', 'KM',
'KN', 'KP', 'KR', 'KW', 'KY', 'KZ', 'LA', 'LB', 'LC', 'LI', 'LK', 'LR', 'LS',
'LT', 'LU', 'LV', 'LY', 'MA', 'MC', 'MD', 'MG', 'MH', 'MK', 'ML', 'MM', 'MN',
'MO', 'MP', 'MQ', 'MR', 'MS', 'MT', 'MU', 'MV', 'MW', 'MX', 'MY', 'MZ', 'NA',
'NC', 'NE', 'NF', 'NG', 'NI', 'NL', 'NO', 'NP', 'NR', 'NU', 'NZ', 'OM', 'PA',
'PE', 'PF', 'PG', 'PH', 'PK', 'PL', 'PM', 'PN', 'PR', 'PS', 'PT', 'PW', 'PY',
'QA', 'RE', 'RO', 'RU', 'RW', 'SA', 'SB', 'SC', 'SD', 'SE', 'SG', 'SH', 'SI',
'SJ', 'SK', 'SL', 'SM', 'SN', 'SO', 'SR', 'ST', 'SV', 'SY', 'SZ', 'TC', 'TD',
'TF', 'TG', 'TH', 'TJ', 'TK', 'TM', 'TN', 'TO', 'TL', 'TR', 'TT', 'TV', 'TW',
'TZ', 'UA', 'UG', 'UM', 'US', 'UY', 'UZ', 'VA', 'VC', 'VE', 'VG', 'VI', 'VN',
'VU', 'WF', 'WS', 'YE', 'YT', 'RS', 'ZA', 'ZM', 'ME', 'ZW', 'A1', 'A2', 'O1',
'AX', 'GG', 'IM', 'JE'
);
$this->num2title = array(
'', 'AP', 'EU', 'Andorra', 'Arab Emirates', 'Afghanistan', 'Antigua', 'AI', 'Albania', 'Armenia', 'Netherlands Antilles', 'Angola', 'AQ',
'Argentina', 'American Samoa', 'Austria', 'Australia', 'Aruba', 'Azerbaijan', 'Bosnia and Herzegovina', 'Barbados', 'Bangladesh', 'Belgium', 'BF', 'Bulgaria', 'Bahrain',
'BI', 'BJ', 'Bermuda', 'Brunei', 'Bolivia', 'Brazil', 'Bahamas', 'Bhutan', 'BV', 'Botswana', 'Belarus', 'Belize', 'Canada',
'Cocos Islands', 'CD', 'CF', 'CG', 'Switzerland', 'CI', 'Cook Islands', 'Chile', 'Cameroon', 'China', 'Colombia', 'Costa Rica', 'Cuba',
'CV', 'CX', 'Cyprus', 'Czech Republic', 'Germany', 'DJ', 'Denmark', 'Dominica', 'Dominican Republic', 'Algeria', 'Ecuador', 'Estonia', 'Egypt',
'EH', 'ER', 'Spain', 'ET', 'Finland', 'Fiji', 'FK', 'FM', 'Faroe Islands', 'France', 'FX', 'Gabon', 'United Kingdom',
'Grenada', 'Georgia', 'GF', 'Ghana', 'Gibraltar', 'Greenland', 'GM', 'GN', 'Guadeloupe', 'GQ', 'Greece', 'GS', 'Guatemala',
'Guam', 'GW', 'Guyana', 'Hong Kong', 'HM', 'Honduras', 'Croatia', 'Haiti', 'Hungary', 'Indonesia', 'Ireland', 'Israel', 'India',
'British Indian Ocean Territory', 'IQ', 'Iran', 'Iceland', 'Italy', 'Jamaica', 'Jordan', 'Japan', 'Kenya', 'Kyrgyzstan', 'Cambodia', 'KI', 'KM',
'St. Kitts and Nevis', 'KP', 'South Korea', 'Kuwait', 'Cayman Islands', 'Kazakhstan', 'Laos', 'Lebanon', 'LC', 'Liechtenstein', 'Sri Lanka', 'LR', 'Lesotho',
'Lithuania', 'Luxembourg', 'Latvia', 'LY', 'Morocco', 'Monaco', 'Moldova', 'MG', 'Marshall Islands', 'Macedonia', 'ML', 'Burma', 'Mongolia',
'Macau', 'Northern Mariana Islands', 'MQ', 'MR', 'MS', 'Malta', 'Mauritius', 'Maldives', 'MW', 'Mexico', 'Malaysia', 'MZ', 'Namibia',
'New Caledonia', 'NE', 'NF', 'Nigeria', 'Nicaragua', 'Netherlands', 'Norway', 'Nepal', 'NR', 'NU', 'New Zealand', 'Oman', 'Panama',
'Peru', 'French Polynesia', 'Papua New Guinea', 'Philippines', 'Pakistan', 'Poland', 'PM', 'PN', 'Puerto Rico', 'PS', 'Portugal', 'Palau', 'Paraguay',
'Qatar', 'RE', 'Romania', 'Russia', 'RW', 'Saudi Arabia', 'SB', 'SC', 'Sudan', 'Sweden', 'Singapore', 'SH', 'Slovenia',
'SJ', 'Slovakia', 'SL', 'San Marino', 'Senegal', 'SO', 'SR', 'ST', 'Ev Salvador', 'SY', 'Swaziland', 'Turks and Caicos Islands', 'Chad',
'TF', 'TG', 'Thailand', 'Tajikistan', 'TK', 'TM', 'Tunisia', 'TO', 'TL', 'Turkey', 'Trinidad and Tobago', 'TV', 'Taiwan',
'Tanzania', 'Ukraine', 'Uganda', 'UM', 'United States', 'Uruguay', 'Uzbekistan', 'Vatican City', 'VC', 'Venezuela', 'VG', 'Virgin Islands', 'Vietnam',
'VU', 'WF', 'Samoa', 'Yemen', 'YT', 'RS', 'South Africa', 'Zambia', 'ME', 'Zimbabwe', 'A1', 'A2', 'O1',
'AX', 'GG', 'IM', 'JE'
);
}
function search($str, $ipn, $max, $return_type = 1)
{
$min = 0;
$ipn1 = $ipn{0};
$exit = $return_type==1 ? 25 : 50;
for ($i = 1; $i < 20; $i++)
{
$offset = floor(($min+$max)/10)*5;
if ($ipn > substr($str, $offset, 4))
{
$min = $offset;
}
else
{
$max = $offset;
}
if ($max-$min < $exit)
{
break;
}
}
for ($i = $min; $i <= $max; $i += 5)
{
if ($ipn < substr($str, $i, 4))
{
break;
}
}
return $return_type==1 ? $i : ord(substr($str, $i-1, 1));
}
function get_num($ip)
{
$ipn = pack('N', ip2long($ip));
$part = $this->search($this->index, $ipn, $this->info['idx_len']-5, 1);
$min = 0;
$max = $this->info['items']-5;
if ($part < $this->info['idx_len'])
{
if ($part >= 0) $min = $part*$this->info['range'];
$max = ($part+5)*$this->info['range'];
}
else
{
$min = ($part-5)*$this->info['range'];
}
$len = $max-$min+5;
return $this->search(substr($this->database, $min, $len), $ipn, $len, 2);
}
function get_cc($ip)
{
return strtolower($this->num2cc[$this->get_num($ip)]);
}
function get_title($ip)
{
return $this->num2title[$this->get_num($ip)];
}
function close()
{
unset($this->info);
unset($this->index);
unset($this->num2cc);
unset($this->database);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment