Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 15th, 2012  |  syntax: None  |  size: 8.01 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. namespace Sypex\Geo;
  3.  
  4. /***************************************************************************\
  5. | Sypex Geo                  version 1.0.0 beta 2                           |
  6. | (c)2006 zapimir            zapimir@zapimir.net       http://sypex.net/    |
  7. | (c)2006 BINOVATOR          info@sypex.net                                 |
  8. |---------------------------------------------------------------------------|
  9. |     created: 2006.10.17 18:33              modified: 2006.10.26 22:07     |
  10. |---------------------------------------------------------------------------|
  11. | This program is free software; you can redistribute it and/or             |
  12. | modify it under the terms of the GNU General Public License               |
  13. | as published by the Free Software Foundation; either version 2            |
  14. | of the License, or (at your option) any later version.                    |
  15. |                                                                           |
  16. | This program is distributed in the hope that it will be useful,           |
  17. | but WITHOUT ANY WARRANTY; without even the implied warranty of            |
  18. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             |
  19. | GNU General Public License for more details.                              |
  20. |                                                                           |
  21. | You should have received a copy of the GNU General Public License         |
  22. | along with this program; if not, write to the Free Software               |
  23. | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA. |
  24. \***************************************************************************/
  25.  
  26. class SypexGeo
  27. {
  28.         var $info;
  29.         var $index;
  30.         var $num2cc;
  31.         var $database;
  32.  
  33.         function SypexGeo($filename)
  34.         {
  35.                 $mem = null;
  36.                 $mem = new \Jamm\Memory\APCObject('sypex');
  37.                 $this->info = $mem->read('info');
  38.                 $this->index = $mem->read('index');
  39.                 $this->database = $mem->read('database');
  40.                 if (empty($this->info) || empty($this->index) || empty($this->database))
  41.                 {
  42.                         $fh = fopen($filename, 'rb');
  43.                         $this->info = unpack('Cidx_len/Nitems', fread($fh, 5));
  44.                         $this->info['range'] = ceil($this->info['items']/($this->info['idx_len']+1));
  45.                         $this->info['idx_len'] *= 5;
  46.                         $this->info['items'] *= 5;
  47.                         $this->info['from'] = $this->info['idx_len']+5;
  48.                         $this->index = fread($fh, $this->info['idx_len']);
  49.                         $this->database = fread($fh, $this->info['items']);
  50.                         fclose($fh);
  51.  
  52.                         if (is_object($mem))
  53.                         {
  54.                                 $mem->save('info', $this->info, 259200);
  55.                                 $mem->save('index', $this->index, 259200);
  56.                                 $mem->save('database', $this->database, 259200);
  57.                         }
  58.                 }
  59.  
  60.                 $this->num2cc = array(
  61.                         '', 'AP', 'EU', 'AD', 'AE', 'AF', 'AG', 'AI', 'AL', 'AM', 'AN', 'AO', 'AQ',
  62.                         'AR', 'AS', 'AT', 'AU', 'AW', 'AZ', 'BA', 'BB', 'BD', 'BE', 'BF', 'BG', 'BH',
  63.                         'BI', 'BJ', 'BM', 'BN', 'BO', 'BR', 'BS', 'BT', 'BV', 'BW', 'BY', 'BZ', 'CA',
  64.                         'CC', 'CD', 'CF', 'CG', 'CH', 'CI', 'CK', 'CL', 'CM', 'CN', 'CO', 'CR', 'CU',
  65.                         'CV', 'CX', 'CY', 'CZ', 'DE', 'DJ', 'DK', 'DM', 'DO', 'DZ', 'EC', 'EE', 'EG',
  66.                         'EH', 'ER', 'ES', 'ET', 'FI', 'FJ', 'FK', 'FM', 'FO', 'FR', 'FX', 'GA', 'GB',
  67.                         'GD', 'GE', 'GF', 'GH', 'GI', 'GL', 'GM', 'GN', 'GP', 'GQ', 'GR', 'GS', 'GT',
  68.                         'GU', 'GW', 'GY', 'HK', 'HM', 'HN', 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IN',
  69.                         'IO', 'IQ', 'IR', 'IS', 'IT', 'JM', 'JO', 'JP', 'KE', 'KG', 'KH', 'KI', 'KM',
  70.                         'KN', 'KP', 'KR', 'KW', 'KY', 'KZ', 'LA', 'LB', 'LC', 'LI', 'LK', 'LR', 'LS',
  71.                         'LT', 'LU', 'LV', 'LY', 'MA', 'MC', 'MD', 'MG', 'MH', 'MK', 'ML', 'MM', 'MN',
  72.                         'MO', 'MP', 'MQ', 'MR', 'MS', 'MT', 'MU', 'MV', 'MW', 'MX', 'MY', 'MZ', 'NA',
  73.                         'NC', 'NE', 'NF', 'NG', 'NI', 'NL', 'NO', 'NP', 'NR', 'NU', 'NZ', 'OM', 'PA',
  74.                         'PE', 'PF', 'PG', 'PH', 'PK', 'PL', 'PM', 'PN', 'PR', 'PS', 'PT', 'PW', 'PY',
  75.                         'QA', 'RE', 'RO', 'RU', 'RW', 'SA', 'SB', 'SC', 'SD', 'SE', 'SG', 'SH', 'SI',
  76.                         'SJ', 'SK', 'SL', 'SM', 'SN', 'SO', 'SR', 'ST', 'SV', 'SY', 'SZ', 'TC', 'TD',
  77.                         'TF', 'TG', 'TH', 'TJ', 'TK', 'TM', 'TN', 'TO', 'TL', 'TR', 'TT', 'TV', 'TW',
  78.                         'TZ', 'UA', 'UG', 'UM', 'US', 'UY', 'UZ', 'VA', 'VC', 'VE', 'VG', 'VI', 'VN',
  79.                         'VU', 'WF', 'WS', 'YE', 'YT', 'RS', 'ZA', 'ZM', 'ME', 'ZW', 'A1', 'A2', 'O1',
  80.                         'AX', 'GG', 'IM', 'JE'
  81.                 );
  82.                 $this->num2title = array(
  83.                         '', 'AP', 'EU', 'Andorra', 'Arab Emirates', 'Afghanistan', 'Antigua', 'AI', 'Albania', 'Armenia', 'Netherlands Antilles', 'Angola', 'AQ',
  84.                         'Argentina', 'American Samoa', 'Austria', 'Australia', 'Aruba', 'Azerbaijan', 'Bosnia and Herzegovina', 'Barbados', 'Bangladesh', 'Belgium', 'BF', 'Bulgaria', 'Bahrain',
  85.                         'BI', 'BJ', 'Bermuda', 'Brunei', 'Bolivia', 'Brazil', 'Bahamas', 'Bhutan', 'BV', 'Botswana', 'Belarus', 'Belize', 'Canada',
  86.                         'Cocos Islands', 'CD', 'CF', 'CG', 'Switzerland', 'CI', 'Cook Islands', 'Chile', 'Cameroon', 'China', 'Colombia', 'Costa Rica', 'Cuba',
  87.                         'CV', 'CX', 'Cyprus', 'Czech Republic', 'Germany', 'DJ', 'Denmark', 'Dominica', 'Dominican Republic', 'Algeria', 'Ecuador', 'Estonia', 'Egypt',
  88.                         'EH', 'ER', 'Spain', 'ET', 'Finland', 'Fiji', 'FK', 'FM', 'Faroe Islands', 'France', 'FX', 'Gabon', 'United Kingdom',
  89.                         'Grenada', 'Georgia', 'GF', 'Ghana', 'Gibraltar', 'Greenland', 'GM', 'GN', 'Guadeloupe', 'GQ', 'Greece', 'GS', 'Guatemala',
  90.                         'Guam', 'GW', 'Guyana', 'Hong Kong', 'HM', 'Honduras', 'Croatia', 'Haiti', 'Hungary', 'Indonesia', 'Ireland', 'Israel', 'India',
  91.                         'British Indian Ocean Territory', 'IQ', 'Iran', 'Iceland', 'Italy', 'Jamaica', 'Jordan', 'Japan', 'Kenya', 'Kyrgyzstan', 'Cambodia', 'KI', 'KM',
  92.                         'St. Kitts and Nevis', 'KP', 'South Korea', 'Kuwait', 'Cayman Islands', 'Kazakhstan', 'Laos', 'Lebanon', 'LC', 'Liechtenstein', 'Sri Lanka', 'LR', 'Lesotho',
  93.                         'Lithuania', 'Luxembourg', 'Latvia', 'LY', 'Morocco', 'Monaco', 'Moldova', 'MG', 'Marshall Islands', 'Macedonia', 'ML', 'Burma', 'Mongolia',
  94.                         'Macau', 'Northern Mariana Islands', 'MQ', 'MR', 'MS', 'Malta', 'Mauritius', 'Maldives', 'MW', 'Mexico', 'Malaysia', 'MZ', 'Namibia',
  95.                         'New Caledonia', 'NE', 'NF', 'Nigeria', 'Nicaragua', 'Netherlands', 'Norway', 'Nepal', 'NR', 'NU', 'New Zealand', 'Oman', 'Panama',
  96.                         'Peru', 'French Polynesia', 'Papua New Guinea', 'Philippines', 'Pakistan', 'Poland', 'PM', 'PN', 'Puerto Rico', 'PS', 'Portugal', 'Palau', 'Paraguay',
  97.                         'Qatar', 'RE', 'Romania', 'Russia', 'RW', 'Saudi Arabia', 'SB', 'SC', 'Sudan', 'Sweden', 'Singapore', 'SH', 'Slovenia',
  98.                         'SJ', 'Slovakia', 'SL', 'San Marino', 'Senegal', 'SO', 'SR', 'ST', 'Ev Salvador', 'SY', 'Swaziland', 'Turks and Caicos Islands', 'Chad',
  99.                         'TF', 'TG', 'Thailand', 'Tajikistan', 'TK', 'TM', 'Tunisia', 'TO', 'TL', 'Turkey', 'Trinidad and Tobago', 'TV', 'Taiwan',
  100.                         'Tanzania', 'Ukraine', 'Uganda', 'UM', 'United States', 'Uruguay', 'Uzbekistan', 'Vatican City', 'VC', 'Venezuela', 'VG', 'Virgin Islands', 'Vietnam',
  101.                         'VU', 'WF', 'Samoa', 'Yemen', 'YT', 'RS', 'South Africa', 'Zambia', 'ME', 'Zimbabwe', 'A1', 'A2', 'O1',
  102.                         'AX', 'GG', 'IM', 'JE'
  103.                 );
  104.         }
  105.  
  106.         function search($str, $ipn, $max, $return_type = 1)
  107.         {
  108.                 $min = 0;
  109.                 $ipn1 = $ipn{0};
  110.                 $exit = $return_type==1 ? 25 : 50;
  111.                 for ($i = 1; $i < 20; $i++)
  112.                 {
  113.                         $offset = floor(($min+$max)/10)*5;
  114.                         if ($ipn > substr($str, $offset, 4))
  115.                         {
  116.                                 $min = $offset;
  117.                         }
  118.                         else
  119.                         {
  120.                                 $max = $offset;
  121.                         }
  122.                         if ($max-$min < $exit)
  123.                         {
  124.                                 break;
  125.                         }
  126.                 }
  127.                 for ($i = $min; $i <= $max; $i += 5)
  128.                 {
  129.                         if ($ipn < substr($str, $i, 4))
  130.                         {
  131.                                 break;
  132.                         }
  133.                 }
  134.                 return $return_type==1 ? $i : ord(substr($str, $i-1, 1));
  135.         }
  136.  
  137.         function get_num($ip)
  138.         {
  139.                 $ipn = pack('N', ip2long($ip));
  140.                 $part = $this->search($this->index, $ipn, $this->info['idx_len']-5, 1);
  141.                 $min = 0;
  142.                 $max = $this->info['items']-5;
  143.                 if ($part < $this->info['idx_len'])
  144.                 {
  145.                         if ($part >= 0) $min = $part*$this->info['range'];
  146.                         $max = ($part+5)*$this->info['range'];
  147.                 }
  148.                 else
  149.                 {
  150.                         $min = ($part-5)*$this->info['range'];
  151.                 }
  152.                 $len = $max-$min+5;
  153.                 return $this->search(substr($this->database, $min, $len), $ipn, $len, 2);
  154.         }
  155.  
  156.         function get_cc($ip)
  157.         {
  158.                 return strtolower($this->num2cc[$this->get_num($ip)]);
  159.         }
  160.  
  161.         function get_title($ip)
  162.         {
  163.                 return $this->num2title[$this->get_num($ip)];
  164.         }
  165.  
  166.         function close()
  167.         {
  168.                 unset($this->info);
  169.                 unset($this->index);
  170.                 unset($this->num2cc);
  171.                 unset($this->database);
  172.         }
  173. }