Count per Day - Version 3.2.6

Version Description

  • New: posts only parameters for [CPD_MOST_VISITED_POSTS]
  • New: don't count password protected posts without password
  • New: translate widget titles if possible
  • New: flags of Cambodia, Nigeria and Sudan
  • Bugfix: massbot post list has not show all post
  • Bugfix: security fix, XSS
  • Bugfix: problems while adding widgets
  • Bugfix: start session only in backend
  • Bugfix: empty database query
Download this release

Release Info

Developer Tom Braider
Plugin Icon 128x128 Count per Day
Version 3.2.6
Comparing to
See all releases

Code changes from version 3.2.5 to 3.2.6

Files changed (15) hide show
  1. ajax.php +4 -4
  2. counter-core.php +78 -71
  3. counter.css +31 -22
  4. counter.php +30 -16
  5. geoip/GeoIP.dat +0 -0
  6. geoip/cpd_geoip.inc +0 -713
  7. geoip/geoip.inc +433 -221
  8. geoip/geoip.php +35 -16
  9. img/cpd_flags.png +0 -0
  10. locale/cpd-sr_RS.mo +0 -0
  11. locale/cpd-sr_RS.po +1119 -0
  12. massbots.php +8 -8
  13. notes.php +3 -3
  14. readme.txt +57 -32
  15. userperspan.php +10 -8
ajax.php CHANGED
@@ -5,7 +5,7 @@ if ( $_GET['f'] == 'count' )
5
if ( empty($_GET['time']) || time() - $_GET['time'] > 20 )
6
{
7
header("HTTP/1.0 403 Forbidden");
8
- die();
9
}
10
11
if (!session_id()) session_start();
@@ -27,11 +27,11 @@ if ( $_GET['f'] == 'count' )
27
{
28
echo $f.'===';
29
if ( $f == 'getUserPerDay' )
30
- eval('echo $count_per_day->getUserPerDay('.$count_per_day->options['dashboard_last_days'].');');
31
else if ( $f == 'show' )
32
- eval('echo $count_per_day->show("", "", false, false, '.$page.');');
33
else
34
- eval('echo $count_per_day->'.$f.'();');
35
echo '|';
36
}
37
}
5
if ( empty($_GET['time']) || time() - $_GET['time'] > 20 )
6
{
7
header("HTTP/1.0 403 Forbidden");
8
+ die('wrong request');
9
}
10
11
if (!session_id()) session_start();
27
{
28
echo $f.'===';
29
if ( $f == 'getUserPerDay' )
30
+ echo $count_per_day->getUserPerDay($count_per_day->options['dashboard_last_days']);
31
else if ( $f == 'show' )
32
+ echo $count_per_day->show("", "", false, false, '.$page.');
33
else
34
+ echo call_user_func('$count_per_day->'.$f);
35
echo '|';
36
}
37
}
counter-core.php CHANGED
@@ -147,7 +147,8 @@ function init()
147
add_action('admin_enqueue_scripts', array(&$this,'addThickbox'));
148
149
// Session
150
- add_action('init', array(&$this,'startSession'), 1);
151
152
$this->aton = 'INET_ATON';
153
$this->ntoa = 'INET_NTOA';
@@ -159,11 +160,11 @@ function init()
159
function addPostTypesColumns()
160
{
161
$post_types = get_post_types(array('public'=>true),'objects');
162
- foreach ($post_types as $post_type )
163
{
164
$name = trim($post_type->name);
165
- add_action('manage_'.trim($name).'s_custom_column', array(&$this,'cpdColumnContent'), 10, 2);
166
- add_filter('manage_edit-'.trim($name).'_columns', array(&$this,'cpdColumn'));
167
}
168
}
169
@@ -179,17 +180,6 @@ function addThickbox()
179
wp_enqueue_script('cpd_flot', $this->dir.'/js/jquery.flot.min.js', 'jQuery');
180
}
181
182
- function cpdReadsOrderby( $vars )
183
- {
184
- if ( isset($vars['orderby']) && $vars['orderby'] == 'cpd_reads' )
185
- {
186
- $vars = array_merge( $vars, array(
187
- 'meta_key' => 'cpd_reads',
188
- 'orderby' => 'meta_value_num'
189
- ));
190
- }
191
- return $vars;
192
- }
193
194
/**
195
* starts session to provide WP variables to "addons"
@@ -204,7 +194,7 @@ function startSession()
204
/**
205
* get result from database
206
* @param string $kind kind of result
207
- * @param string $sql sql query
208
* @param string $func name for debug info
209
*/
210
function mysqlQuery( $kind = '', $sql, $func = '' )
@@ -214,14 +204,26 @@ function mysqlQuery( $kind = '', $sql, $func = '' )
214
return;
215
$t = microtime(true);
216
$con = $wpdb->dbh;
217
- $preparedSql = $wpdb->prepare($sql, null);
218
$r = false;
219
if ($kind == 'var')
220
$r = $wpdb->get_var( $preparedSql );
221
else if ($kind == 'count')
222
{
223
- $sql = 'SELECT COUNT(*) FROM ('.trim($sql,';').') t';
224
- $r = $wpdb->get_var( $wpdb->prepare($sql, null) );
225
}
226
else if ($kind == 'rows')
227
{
@@ -262,6 +264,10 @@ function checkInstalledVersion()
262
*/
263
function anonymize_ip( $ip )
264
{
265
if ( $this->options['debug'] )
266
$this->queries[] = 'called Function: <b style="color:blue">anonymize_ip</b> IP: <code>'.$ip.'</code>';
267
if ($this->options['anoip'])
@@ -360,7 +366,7 @@ function checkVersion()
360
if (!empty($_GET['networkwide']))
361
{
362
$old_blog = $wpdb->blogid;
363
- $blogids = $wpdb->get_col($wpdb->prepare("SELECT blog_id FROM $wpdb->blogs", null));
364
foreach ($blogids as $blog_id)
365
{
366
// create tables in all sub blogs
@@ -401,11 +407,12 @@ function createTables()
401
`client` varchar(150) NOT NULL,
402
`date` date NOT NULL,
403
`page` mediumint(9) NOT NULL,
404
`referer` varchar(100) NOT NULL,
405
PRIMARY KEY (`id`),
406
KEY `idx_page` (`page`),
407
KEY `idx_dateip` (`date`,`ip`) )
408
- $charset_collate;";
409
$this->mysqlQuery('', $sql, 'createTables '.__LINE__);
410
411
// update fields in old table
@@ -438,15 +445,12 @@ function createTables()
438
$sql .= 'ADD KEY `idx_dateip` (`date`,`ip`), ADD KEY `idx_page` (`page`)';
439
$this->mysqlQuery('', $sql, 'make keys '.__LINE__);
440
441
- // if GeoIP installed we need row "country"
442
- if ( class_exists('CpdGeoIp') )
443
- {
444
- $this->mysqlQuery('', "SELECT country FROM `$cpd_c` LIMIT 1", 'check country '.__LINE__);
445
- if ((int) mysql_errno() == 1054)
446
- $this->mysqlQuery('', "ALTER TABLE `$cpd_c` ADD `country` CHAR(2) NOT NULL", 'make country '.__LINE__);
447
- }
448
449
- // referrer
450
$this->mysqlQuery('', "SELECT referer FROM `$cpd_c` LIMIT 1", 'check referer '.__LINE__);
451
if ((int) mysql_errno() == 1054)
452
$this->mysqlQuery('', "ALTER TABLE `$cpd_c` ADD `referer` VARCHAR(100) NOT NULL", 'make referer '.__LINE__);
@@ -567,7 +571,7 @@ function addAjaxScript()
567
<script type="text/javascript">
568
// Count per Day
569
//<![CDATA[
570
- jQuery(document).ready( function($)
571
{
572
jQuery.get('{$this->dir}/ajax.php?f=count&page={$this->page}&time={$time}', function(text)
573
{
@@ -664,9 +668,9 @@ function updateOptions()
664
'dashboard_last_days' => 7,
665
'show_in_lists' => 1,
666
'chart_days' => 60,
667
- 'chart_height' => 100,
668
'countries' => 20,
669
- 'startdate' => '2000-01-01',
670
'startcount' => '',
671
'startreads' => '',
672
'anoip' => 0,
@@ -702,39 +706,6 @@ function dashboardWidgetSetup()
702
wp_add_dashboard_widget( 'cpdDashboardWidget', 'Count per Day', array(&$this,'dashboardWidget') );
703
}
704
705
- /**
706
- * add counter column to page/post lists
707
- */
708
- function cpdColumn($defaults)
709
- {
710
- if ( $this->options['show_in_lists'] )
711
- $defaults['cpd_reads'] = '<img src="'.$this->img('cpd_menu.gif').'" alt="'.__('Reads', 'cpd').'" title="'.__('Reads', 'cpd').'" style="width:12px;height:12px;" />';
712
- return $defaults;
713
- }
714
-
715
- function cpdSortableColumns($columns)
716
- {
717
- // meta column id => sortby value used in query
718
- $custom = array('cpd_reads' => 'cpd_reads');
719
- return wp_parse_args($custom, $columns);
720
- }
721
-
722
- /**
723
- * adds content to the counter column
724
- */
725
- function cpdColumnContent($column_name, $id = 0)
726
- {
727
- global $wpdb;
728
- if( $column_name == 'cpd_reads' )
729
- {
730
- $c = $this->mysqlQuery('count', "SELECT 1 FROM $wpdb->cpd_counter WHERE page='$id'", 'cpdColumn_'.$id.'_'.__LINE__);
731
- $coll = get_option('count_per_day_posts');
732
- if ( $coll && isset($coll['p'.$id]) )
733
- $c += $coll['p'.$id];
734
- echo $c;
735
- }
736
- }
737
-
738
/**
739
* gets image recource with given name
740
*/
@@ -748,7 +719,7 @@ function img( $r )
748
*/
749
function screenLayoutColumns($columns, $screen)
750
{
751
- if ($screen == $this->pagehook)
752
$columns[$this->pagehook] = 4;
753
return $columns;
754
}
@@ -856,8 +827,8 @@ function onShowPage()
856
</div>
857
<script type="text/javascript">
858
//<![CDATA[
859
- jQuery(document).ready( function($) {
860
- $('.if-js-closed').removeClass('if-js-closed').addClass('closed');
861
postboxes.add_postbox_toggles('<?php echo $this->pagehook; ?>');
862
});
863
//]]>
@@ -918,10 +889,20 @@ function shortUserPerMonth() { return $this->getUserPerMonth(true, true); }
918
function shortUserPerPost() { return $this->getUserPerPost(0, true, true); }
919
function shortCountries() { return $this->getCountries(0, true, false, true); }
920
function shortCountriesUsers(){ return $this->getCountries(0, true, true, true); }
921
- function shortMostVisitedPosts(){ return $this->getMostVisitedPosts(0, 0, true, false, true); }
922
function shortReferers() { return $this->getReferers(0, true, 0); }
923
function shortDayWithMostReads(){ return $this->getDayWithMostReads(true, true); }
924
function shortDayWithMostUsers(){ return $this->getDayWithMostUsers(true, true); }
925
function shortGetSearches( $atts )
926
{
927
extract( shortcode_atts( array(
@@ -1451,7 +1432,7 @@ function getSearchString()
1451
{
1452
if (empty($_SERVER['HTTP_REFERER']))
1453
return false;
1454
- $ref = parse_url(rawurldecode($_SERVER['HTTP_REFERER']));
1455
if ( empty($ref['host']) || empty($ref['query']) )
1456
return false;
1457
$keys = array('p','q','s','query','search','prev','qkw','qry');
@@ -1459,11 +1440,37 @@ function getSearchString()
1459
foreach ($keys as $key)
1460
if (isset($query[$key]))
1461
$search = str_ireplace(array('/search?q=','/images?q='), '', $query[$key]);
1462
- $search = strip_tags($search);
1463
if (empty($search) || is_numeric($search)) // non WordPress postID
1464
$search = '';
1465
return trim($search);
1466
}
1467
1468
1469
} // class
147
add_action('admin_enqueue_scripts', array(&$this,'addThickbox'));
148
149
// Session
150
+ if (strpos($_SERVER['SCRIPT_NAME'], '/wp-admin/') !== false )
151
+ add_action('init', array(&$this,'startSession'), 1);
152
153
$this->aton = 'INET_ATON';
154
$this->ntoa = 'INET_NTOA';
160
function addPostTypesColumns()
161
{
162
$post_types = get_post_types(array('public'=>true),'objects');
163
+ foreach ($post_types as $post_type )
164
{
165
$name = trim($post_type->name);
166
+ add_action('manage_'.$name.'s_custom_column', array(&$this,'cpdColumnContent'), 10, 2);
167
+ add_filter('manage_edit-'.$name.'_columns', array(&$this,'cpdColumn'));
168
}
169
}
170
180
wp_enqueue_script('cpd_flot', $this->dir.'/js/jquery.flot.min.js', 'jQuery');
181
}
182
183
184
/**
185
* starts session to provide WP variables to "addons"
194
/**
195
* get result from database
196
* @param string $kind kind of result
197
+ * @param string|array $sql sql query [and args]
198
* @param string $func name for debug info
199
*/
200
function mysqlQuery( $kind = '', $sql, $func = '' )
204
return;
205
$t = microtime(true);
206
$con = $wpdb->dbh;
207
+
208
+ if ( is_array($sql) )
209
+ {
210
+ $sql = array_shift($sql);
211
+ $args = $sql;
212
+ $preparedSql = $wpdb->prepare($sql, $args);
213
+ }
214
+ else
215
+ $preparedSql = $sql;
216
+
217
+ if (empty($preparedSql))
218
+ return;
219
+
220
$r = false;
221
if ($kind == 'var')
222
$r = $wpdb->get_var( $preparedSql );
223
else if ($kind == 'count')
224
{
225
+ // $sql = 'SELECT COUNT(*) FROM ('.trim($sql,';').') t';
226
+ $r = $wpdb->get_var('SELECT COUNT(*) FROM ('.trim($preparedSql,';').') t');
227
}
228
else if ($kind == 'rows')
229
{
264
*/
265
function anonymize_ip( $ip )
266
{
267
+ // not on IPv6
268
+ if (strpos($ip,':'))
269
+ return $ip;
270
+
271
if ( $this->options['debug'] )
272
$this->queries[] = 'called Function: <b style="color:blue">anonymize_ip</b> IP: <code>'.$ip.'</code>';
273
if ($this->options['anoip'])
366
if (!empty($_GET['networkwide']))
367
{
368
$old_blog = $wpdb->blogid;
369
+ $blogids = $wpdb->get_col($wpdb->prepare("SELECT blog_id FROM %s", $wpdb->blogs));
370
foreach ($blogids as $blog_id)
371
{
372
// create tables in all sub blogs
407
`client` varchar(150) NOT NULL,
408
`date` date NOT NULL,
409
`page` mediumint(9) NOT NULL,
410
+ `country` CHAR(2) NOT NULL,
411
`referer` varchar(100) NOT NULL,
412
PRIMARY KEY (`id`),
413
KEY `idx_page` (`page`),
414
KEY `idx_dateip` (`date`,`ip`) )
415
+ $charset_collate";
416
$this->mysqlQuery('', $sql, 'createTables '.__LINE__);
417
418
// update fields in old table
445
$sql .= 'ADD KEY `idx_dateip` (`date`,`ip`), ADD KEY `idx_page` (`page`)';
446
$this->mysqlQuery('', $sql, 'make keys '.__LINE__);
447
448
+ // column country
449
+ $this->mysqlQuery('', "SELECT country FROM `$cpd_c` LIMIT 1", 'check country '.__LINE__);
450
+ if ((int) mysql_errno() == 1054)
451
+ $this->mysqlQuery('', "ALTER TABLE `$cpd_c` ADD `country` CHAR(2) NOT NULL", 'make country '.__LINE__);
452
453
+ // column referrer
454
$this->mysqlQuery('', "SELECT referer FROM `$cpd_c` LIMIT 1", 'check referer '.__LINE__);
455
if ((int) mysql_errno() == 1054)
456
$this->mysqlQuery('', "ALTER TABLE `$cpd_c` ADD `referer` VARCHAR(100) NOT NULL", 'make referer '.__LINE__);
571
<script type="text/javascript">
572
// Count per Day
573
//<![CDATA[
574
+ jQuery(document).ready( function()
575
{
576
jQuery.get('{$this->dir}/ajax.php?f=count&page={$this->page}&time={$time}', function(text)
577
{
668
'dashboard_last_days' => 7,
669
'show_in_lists' => 1,
670
'chart_days' => 60,
671
+ 'chart_height' => 200,
672
'countries' => 20,
673
+ 'startdate' => '',
674
'startcount' => '',
675
'startreads' => '',
676
'anoip' => 0,
706
wp_add_dashboard_widget( 'cpdDashboardWidget', 'Count per Day', array(&$this,'dashboardWidget') );
707
}
708
709
/**
710
* gets image recource with given name
711
*/
719
*/
720
function screenLayoutColumns($columns, $screen)
721
{
722
+ if ( isset($this->pagehook) && $screen == $this->pagehook )
723
$columns[$this->pagehook] = 4;
724
return $columns;
725
}
827
</div>
828
<script type="text/javascript">
829
//<![CDATA[
830
+ jQuery(document).ready( function() {
831
+ jQuery('.if-js-closed').removeClass('if-js-closed').addClass('closed');
832
postboxes.add_postbox_toggles('<?php echo $this->pagehook; ?>');
833
});
834
//]]>
889
function shortUserPerPost() { return $this->getUserPerPost(0, true, true); }
890
function shortCountries() { return $this->getCountries(0, true, false, true); }
891
function shortCountriesUsers(){ return $this->getCountries(0, true, true, true); }
892
+ // function shortMostVisitedPosts(){ return $this->getMostVisitedPosts(0, 0, true, false, true); }
893
function shortReferers() { return $this->getReferers(0, true, 0); }
894
function shortDayWithMostReads(){ return $this->getDayWithMostReads(true, true); }
895
function shortDayWithMostUsers(){ return $this->getDayWithMostUsers(true, true); }
896
+ function shortMostVisitedPosts( $atts )
897
+ {
898
+ extract( shortcode_atts( array(
899
+ 'days' => 0,
900
+ 'limit' => 0,
901
+ 'postsonly' => 0,
902
+ 'posttypes' => ''
903
+ ), $atts) );
904
+ return $this->getMostVisitedPosts( $days, $limit, true, $postsonly, false, $posttypes );
905
+ }
906
function shortGetSearches( $atts )
907
{
908
extract( shortcode_atts( array(
1432
{
1433
if (empty($_SERVER['HTTP_REFERER']))
1434
return false;
1435
+ $ref = parse_url(rawurldecode(wp_strip_all_tags($_SERVER['HTTP_REFERER'])));
1436
if ( empty($ref['host']) || empty($ref['query']) )
1437
return false;
1438
$keys = array('p','q','s','query','search','prev','qkw','qry');
1440
foreach ($keys as $key)
1441
if (isset($query[$key]))
1442
$search = str_ireplace(array('/search?q=','/images?q='), '', $query[$key]);
1443
+ if (isset($search))
1444
+ $search = wp_strip_all_tags($search);
1445
if (empty($search) || is_numeric($search)) // non WordPress postID
1446
$search = '';
1447
return trim($search);
1448
}
1449
1450
+ /**
1451
+ * add counter column to page/post lists
1452
+ */
1453
+ function cpdColumn($defaults)
1454
+ {
1455
+ if ( $this->options['show_in_lists'] )
1456
+ $defaults['cpd_reads'] = '<img src="'.$this->img('cpd_menu.gif').'" alt="'.__('Reads', 'cpd').'" title="'.__('Reads', 'cpd').'" style="width:12px;height:12px;" />';
1457
+ return $defaults;
1458
+ }
1459
+
1460
+ /**
1461
+ * adds content to the counter column
1462
+ */
1463
+ function cpdColumnContent($column_name, $id = 0)
1464
+ {
1465
+ global $wpdb;
1466
+ if( $column_name == 'cpd_reads' )
1467
+ {
1468
+ $c = $this->mysqlQuery('count', "SELECT 1 FROM $wpdb->cpd_counter WHERE page='$id'", 'cpdColumn_'.$id.'_'.__LINE__);
1469
+ $coll = get_option('count_per_day_posts');
1470
+ if ( $coll && isset($coll['p'.$id]) )
1471
+ $c += $coll['p'.$id];
1472
+ echo $c;
1473
+ }
1474
+ }
1475
1476
} // class
counter.css CHANGED
@@ -85,26 +85,26 @@ h2 .cpd_icon {
85
vertical-align: text-bottom;
86
padding: 0 !important;
87
}
88
- .cpd_backup{background-position: 0 0}
89
- .cpd_clean{background-position: 0 -46px}
90
- .cpd_collection{background-position: 0 -92px}
91
- .cpd_computer{background-position: 0 -138px}
92
- .cpd_calendar{background-position: 0 -184px}
93
- .cpd_day{background-position: 0 -230px}
94
- .cpd_geoip{background-position: 0 -276px}
95
- .cpd_help{background-position: 0 -322px}
96
- .cpd_massbots{background-position: 0 -368px}
97
- .cpd_online{background-position: 0 -414px}
98
- .cpd_post{background-position: 0 -460px}
99
- .cpd_reads{background-position: 0 -506px}
100
- .cpd_referrer{background-position: 0 -552px}
101
- .cpd_reset{background-position: 0 -598px}
102
- .cpd_settings{background-position: 0 -644px}
103
- .cpd_summary{background-position: 0 -690px}
104
- .cpd_tools{background-position: 0 -736px}
105
- .cpd_uninstall{background-position: 0 -782px}
106
- .cpd_update{background-position: 0 -828px}
107
- .cpd_user{background-position: 0 -874px}
108
109
/* dashboard */
110
@@ -121,7 +121,9 @@ h2 .cpd_icon {
121
}
122
123
#cpd-flot {
124
overflow: hidden;
125
}
126
127
#cpd-tooltip {
@@ -209,7 +211,7 @@ h2 .cpd_icon {
209
.cpd_widget_item .widefat {
210
width: 235px !important;
211
margin-left: 15px;
212
-
213
}
214
215
/* thickbox */
@@ -268,7 +270,7 @@ h2 .cpd_icon {
268
color: #ff0;
269
}
270
271
- .cpd-notes input {
272
width: 97%;
273
font: 12px Verdana, Arial, Helvetica, sans-serif;
274
}
@@ -433,3 +435,10 @@ h2 .cpd_icon {
433
.cpd-flag-cm{background-position:0 -1586px;}
434
.cpd-flag-mn{background-position:0 -1599px;}
435
.cpd-flag-kz{background-position:0 -1612px;}
85
vertical-align: text-bottom;
86
padding: 0 !important;
87
}
88
+ .cpd_backup{background-position: 0 0}
89
+ .cpd_clean{background-position: 0 -46px}
90
+ .cpd_collection{background-position: 0 -92px}
91
+ .cpd_computer{background-position: 0 -138px}
92
+ .cpd_calendar{background-position: 0 -184px}
93
+ .cpd_day{background-position: 0 -230px}
94
+ .cpd_geoip{background-position: 0 -276px}
95
+ .cpd_help{background-position: 0 -322px}
96
+ .cpd_massbots{background-position: 0 -368px}
97
+ .cpd_online{background-position: 0 -414px}
98
+ .cpd_post{background-position: 0 -460px}
99
+ .cpd_reads{background-position: 0 -506px}
100
+ .cpd_referrer{background-position: 0 -552px}
101
+ .cpd_reset{background-position: 0 -598px}
102
+ .cpd_settings{background-position: 0 -644px}
103
+ .cpd_summary{background-position: 0 -690px}
104
+ .cpd_tools{background-position: 0 -736px}
105
+ .cpd_uninstall{background-position: 0 -782px}
106
+ .cpd_update{background-position: 0 -828px}
107
+ .cpd_user{background-position: 0 -874px}
108
109
/* dashboard */
110
121
}
122
123
#cpd-flot {
124
+ clear: both;
125
overflow: hidden;
126
+ margin-top: 5px;
127
}
128
129
#cpd-tooltip {
211
.cpd_widget_item .widefat {
212
width: 235px !important;
213
margin-left: 15px;
214
+
215
}
216
217
/* thickbox */
270
color: #ff0;
271
}
272
273
+ .cpd-notes input {
274
width: 97%;
275
font: 12px Verdana, Arial, Helvetica, sans-serif;
276
}
435
.cpd-flag-cm{background-position:0 -1586px;}
436
.cpd-flag-mn{background-position:0 -1599px;}
437
.cpd-flag-kz{background-position:0 -1612px;}
438
+ .cpd-flag-ng{background-position:0 -1625px;}
439
+ .cpd-flag-kh{background-position:0 -1638px;}
440
+ .cpd-flag-sd{background-position:0 -1651px;}
441
+
442
+
443
+
444
+
counter.php CHANGED
@@ -3,16 +3,19 @@
3
Plugin Name: Count Per Day
4
Plugin URI: http://www.tomsdimension.de/wp-plugins/count-per-day
5
Description: Counter, shows reads and visitors per page; today, yesterday, last week, last months ... on dashboard, per shortcode or in widget.
6
- Version: 3.2.5
7
License: Postcardware
8
Author: Tom Braider
9
Author URI: http://www.tomsdimension.de
10
*/
11
12
$cpd_dir_name = 'count-per-day';
13
- $cpd_version = '3.2.5';
14
15
- $cpd_path = str_replace('/', DIRECTORY_SEPARATOR, ABSPATH.PLUGINDIR.'/'.$cpd_dir_name.'/');
16
include_once($cpd_path.'counter-core.php');
17
18
/**
@@ -53,7 +56,7 @@ function show( $before='', $after=' reads', $show = true, $count = true, $page =
53
// get count from collection
54
$c = $this->getCollectedPostReads($page);
55
// add current data
56
- $c += $this->mysqlQuery('var', $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->cpd_counter WHERE page='$page'", null), 'show '.__LINE__);
57
if ($show)
58
echo $before.$c.$after;
59
else
@@ -103,8 +106,8 @@ function count( $x, $page = 'x' )
103
.'page: <code>'.$page.'</code> '
104
.'userlevel: <code>'.$userlevel.'</code>';
105
106
- // only count if: non bot, Logon is ok
107
- if ( !$isBot && $countUser && isset($page) )
108
{
109
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
110
{
@@ -116,8 +119,8 @@ function count( $x, $page = 'x' )
116
$real_ip = $_SERVER['REMOTE_ADDR'];
117
118
$userip = $this->anonymize_ip($real_ip);
119
- $client = ($this->options['referers']) ? $_SERVER['HTTP_USER_AGENT'] : '';
120
- $referer = ($this->options['referers'] && isset($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : '';
121
if ($this->options['referers_cut'])
122
$referer = substr( $referer, 0, strpos($referer,'?') );
123
@@ -130,7 +133,7 @@ function count( $x, $page = 'x' )
130
{
131
// with GeoIP addon save country
132
$gi = cpd_geoip_open($cpd_path.'geoip/GeoIP.dat', GEOIP_STANDARD);
133
- $country = strtolower(cpd_geoip_country_code_by_addr($gi, $userip));
134
$this->mysqlQuery('', $wpdb->prepare("INSERT INTO $wpdb->cpd_counter (page, ip, client, date, country, referer)
135
VALUES (%s, $this->aton(%s), %s, %s, %s, %s)", $page, $userip, $client, $date, $country, $referer), 'count insert '.__LINE__);
136
}
@@ -665,7 +668,7 @@ function getUserPerDay( $days = 0, $return = false )
665
* @param integer $limit count of posts (last posts)
666
* @param boolean $postsonly don't show categories and taxonomies
667
*/
668
- function getMostVisitedPosts( $days = 0, $limit = 0, $frontend = false, $postsonly = false, $return = false )
669
{
670
global $wpdb;
671
if ( $days == 0 )
@@ -674,6 +677,15 @@ function getMostVisitedPosts( $days = 0, $limit = 0, $frontend = false, $postson
674
$limit = $this->options['dashboard_last_posts'];
675
$date = date_i18n('Y-m-d', current_time('timestamp') - 86400 * $days);
676
677
if ($postsonly)
678
$sql = $wpdb->prepare("
679
SELECT COUNT(c.id) count,
@@ -684,6 +696,7 @@ function getMostVisitedPosts( $days = 0, $limit = 0, $frontend = false, $postson
684
ON p.id = c.page
685
WHERE c.date >= %s
686
AND c.page > 0
687
GROUP BY c.page
688
ORDER BY count DESC
689
LIMIT %d",
@@ -790,6 +803,7 @@ function getVisitedPostsOnDay( $date = 0, $limit = 0, $show_form = true, $show_n
790
$date = $_GET['daytoshow'];
791
else if ( $date == 0 )
792
$date = date_i18n('Y-m-d');
793
if ( $limit == 0 )
794
$limit = $this->options['dashboard_last_posts'];
795
@@ -1258,12 +1272,12 @@ class CountPerDay_Widget extends WP_Widget
1258
echo '<span id="cpd_number_'.$k.'" class="cpd-r">';
1259
// parameters only for special functions
1260
if ( $f == 'getUserPerDay' )
1261
- eval('echo $count_per_day->getUserPerDay('.$count_per_day->options['dashboard_last_days'].');');
1262
else if ( $f == 'show' )
1263
- eval('echo $count_per_day->show("","",false,false);');
1264
else
1265
- eval('echo $count_per_day->'.$f.'();');
1266
- echo '</span>'.$instance[$k.'_name'].':</li>';
1267
}
1268
}
1269
}
@@ -1318,7 +1332,8 @@ class CountPerDay_Widget extends WP_Widget
1318
'getuseronline_name' => __('Visitors currently online', 'cpd'),
1319
'getfirstcount_name' => __('Counter starts on', 'cpd')
1320
);
1321
- $instance = wp_parse_args( (array) $instance, $default );
1322
1323
// title field
1324
$field_id = $this->get_field_id('title');
@@ -1362,7 +1377,6 @@ class CountPerDay_Widget extends WP_Widget
1362
?>
1363
<script type="text/javascript">
1364
//<![CDATA[
1365
- jQuery.noConflict();
1366
jQuery(document).ready(function(){
1367
jQuery('#cpdwidgetlist<?php echo $field_id ?>').sortable({
1368
items: 'li:not(.cpd_widget_title)',
3
Plugin Name: Count Per Day
4
Plugin URI: http://www.tomsdimension.de/wp-plugins/count-per-day
5
Description: Counter, shows reads and visitors per page; today, yesterday, last week, last months ... on dashboard, per shortcode or in widget.
6
+ Version: 3.2.6
7
License: Postcardware
8
Author: Tom Braider
9
Author URI: http://www.tomsdimension.de
10
*/
11
12
$cpd_dir_name = 'count-per-day';
13
+ $cpd_version = '3.2.6';
14
15
+ if (strpos($_SERVER['SERVER_NAME'], '.test'))
16
+ $cpd_path = str_replace('/', DIRECTORY_SEPARATOR, ABSPATH.PLUGINDIR.'/'.$cpd_dir_name.'/');
17
+ else
18
+ $cpd_path = dirname(__FILE__).'/';
19
include_once($cpd_path.'counter-core.php');
20
21
/**
56
// get count from collection
57
$c = $this->getCollectedPostReads($page);
58
// add current data
59
+ $c += $this->mysqlQuery('var', $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->cpd_counter WHERE page = %d", $page), 'show '.__LINE__);
60
if ($show)
61
echo $before.$c.$after;
62
else
106
.'page: <code>'.$page.'</code> '
107
.'userlevel: <code>'.$userlevel.'</code>';
108
109
+ // only count if: non bot, Logon is ok, no password required or ok
110
+ if ( !$isBot && $countUser && isset($page) && !post_password_required($page) )
111
{
112
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
113
{
119
$real_ip = $_SERVER['REMOTE_ADDR'];
120
121
$userip = $this->anonymize_ip($real_ip);
122
+ $client = ($this->options['referers']) ? wp_strip_all_tags($_SERVER['HTTP_USER_AGENT']) : '';
123
+ $referer = ($this->options['referers'] && isset($_SERVER['HTTP_REFERER'])) ? wp_strip_all_tags($_SERVER['HTTP_REFERER']) : '';
124
if ($this->options['referers_cut'])
125
$referer = substr( $referer, 0, strpos($referer,'?') );
126
133
{
134
// with GeoIP addon save country
135
$gi = cpd_geoip_open($cpd_path.'geoip/GeoIP.dat', GEOIP_STANDARD);
136
+ $country = strtolower(cpd_geoip_country_code_by_addr_v6($gi, $userip));
137
$this->mysqlQuery('', $wpdb->prepare("INSERT INTO $wpdb->cpd_counter (page, ip, client, date, country, referer)
138
VALUES (%s, $this->aton(%s), %s, %s, %s, %s)", $page, $userip, $client, $date, $country, $referer), 'count insert '.__LINE__);
139
}
668
* @param integer $limit count of posts (last posts)
669
* @param boolean $postsonly don't show categories and taxonomies
670
*/
671
+ function getMostVisitedPosts( $days = 0, $limit = 0, $frontend = false, $postsonly = false, $return = false, $posttypes = '' )
672
{
673
global $wpdb;
674
if ( $days == 0 )
677
$limit = $this->options['dashboard_last_posts'];
678
$date = date_i18n('Y-m-d', current_time('timestamp') - 86400 * $days);
679
680
+ if ($posttypes)
681
+ {
682
+ $types = str_replace(' ', '', $posttypes);
683
+ $types = str_replace(',', "','", $types);
684
+ $postsonly = 1;
685
+ }
686
+ else
687
+ $types = false;
688
+
689
if ($postsonly)
690
$sql = $wpdb->prepare("
691
SELECT COUNT(c.id) count,
696
ON p.id = c.page
697
WHERE c.date >= %s
698
AND c.page > 0
699
+ ".( ($types) ? "AND p.post_type IN ('$types')" : '' )."
700
GROUP BY c.page
701
ORDER BY count DESC
702
LIMIT %d",
803
$date = $_GET['daytoshow'];
804
else if ( $date == 0 )
805
$date = date_i18n('Y-m-d');
806
+ $date = wp_strip_all_tags($date);
807
if ( $limit == 0 )
808
$limit = $this->options['dashboard_last_posts'];
809
1272
echo '<span id="cpd_number_'.$k.'" class="cpd-r">';
1273
// parameters only for special functions
1274
if ( $f == 'getUserPerDay' )
1275
+ echo $count_per_day->getUserPerDay($count_per_day->options['dashboard_last_days']);
1276
else if ( $f == 'show' )
1277
+ echo $count_per_day->show('', '', false, false);
1278
else
1279
+ echo call_user_func( array(&$count_per_day, $f) );
1280
+ echo '</span>'.__($instance[$k.'_name']).':</li>';
1281
}
1282
}
1283
}
1332
'getuseronline_name' => __('Visitors currently online', 'cpd'),
1333
'getfirstcount_name' => __('Counter starts on', 'cpd')
1334
);
1335
+ if (empty($instance['order']))
1336
+ $instance = array_merge( (array) $instance, $default );
1337
1338
// title field
1339
$field_id = $this->get_field_id('title');
1377
?>
1378
<script type="text/javascript">
1379
//<![CDATA[
1380
jQuery(document).ready(function(){
1381
jQuery('#cpdwidgetlist<?php echo $field_id ?>').sortable({
1382
items: 'li:not(.cpd_widget_title)',
geoip/GeoIP.dat CHANGED
Binary file
geoip/cpd_geoip.inc DELETED
@@ -1,713 +0,0 @@
1
- <?php
2
-
3
- /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
4
- /* geoip.inc
5
- *
6
- * Copyright (C) 2007 MaxMind LLC
7
- *
8
- * This library is free software; you can redistribute it and/or
9
- * modify it under the terms of the GNU Lesser General Public
10
- * License as published by the Free Software Foundation; either
11
- * version 2.1 of the License, or (at your option) any later version.
12
- *
13
- * This library 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 GNU
16
- * Lesser General Public License for more details.
17
- *
18
- * You should have received a copy of the GNU Lesser General Public
19
- * License along with this library; if not, write to the Free Software
20
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
- */
22
-
23
- define("GEOIP_COUNTRY_BEGIN", 16776960);
24
- define("GEOIP_STATE_BEGIN_REV0", 16700000);
25
- define("GEOIP_STATE_BEGIN_REV1", 16000000);
26
- define("GEOIP_STANDARD", 0);
27
- define("GEOIP_MEMORY_CACHE", 1);
28
- define("GEOIP_SHARED_MEMORY", 2);
29
- define("STRUCTURE_INFO_MAX_SIZE", 20);
30
- define("DATABASE_INFO_MAX_SIZE", 100);
31
- define("GEOIP_COUNTRY_EDITION", 106);
32
- define("GEOIP_PROXY_EDITION", 8);
33
- define("GEOIP_ASNUM_EDITION", 9);
34
- define("GEOIP_NETSPEED_EDITION", 10);
35
- define("GEOIP_REGION_EDITION_REV0", 112);
36
- define("GEOIP_REGION_EDITION_REV1", 3);
37
- define("GEOIP_CITY_EDITION_REV0", 111);
38
- define("GEOIP_CITY_EDITION_REV1", 2);
39
- define("GEOIP_ORG_EDITION", 110);
40
- define("GEOIP_ISP_EDITION", 4);
41
- define("SEGMENT_RECORD_LENGTH", 3);
42
- define("STANDARD_RECORD_LENGTH", 3);
43
- define("ORG_RECORD_LENGTH", 4);
44
- define("MAX_RECORD_LENGTH", 4);
45
- define("MAX_ORG_RECORD_LENGTH", 300);
46
- define("GEOIP_SHM_KEY", 0x4f415401);
47
- define("US_OFFSET", 1);
48
- define("CANADA_OFFSET", 677);
49
- define("WORLD_OFFSET", 1353);
50
- define("FIPS_RANGE", 360);
51
- define("GEOIP_UNKNOWN_SPEED", 0);
52
- define("GEOIP_DIALUP_SPEED", 1);
53
- define("GEOIP_CABLEDSL_SPEED", 2);
54
- define("GEOIP_CORPORATE_SPEED", 3);
55
- define("GEOIP_DOMAIN_EDITION", 11);
56
- define("GEOIP_COUNTRY_EDITION_V6", 12);
57
- define("GEOIP_LOCATIONA_EDITION", 13);
58
- define("GEOIP_ACCURACYRADIUS_EDITION", 14);
59
- define("GEOIP_CITYCOMBINED_EDITION", 15);
60
- define("GEOIP_CITY_EDITION_REV1_V6", 30);
61
- define("GEOIP_CITY_EDITION_REV0_V6",31);
62
- define("GEOIP_NETSPEED_EDITION_REV1",32);
63
- define("GEOIP_NETSPEED_EDITION_REV1_V6",33);
64
- define("GEOIP_USERTYPE_EDITION",28);
65
- define("GEOIP_USERTYPE_EDITION_V6",29);
66
- define("GEOIP_ASNUM_EDITION_V6",21);
67
- define("GEOIP_ISP_EDITION_V6",22);
68
- define("GEOIP_ORG_EDITION_V6",23);
69
- define("GEOIP_DOMAIN_EDITION_V6",24);
70
-
71
- define("CITYCOMBINED_FIXED_RECORD", 7 );
72
-
73
- class GeoIPCpD {
74
- var $flags;
75
- var $filehandle;
76
- var $memory_buffer;
77
- var $databaseType;
78
- var $databaseSegments;
79
- var $record_length;
80
- var $shmid;
81
- var $GEOIP_COUNTRY_CODE_TO_NUMBER = array(
82
- "" => 0, "AP" => 1, "EU" => 2, "AD" => 3, "AE" => 4, "AF" => 5,
83
- "AG" => 6, "AI" => 7, "AL" => 8, "AM" => 9, "CW" => 10, "AO" => 11,
84
- "AQ" => 12, "AR" => 13, "AS" => 14, "AT" => 15, "AU" => 16, "AW" => 17,
85
- "AZ" => 18, "BA" => 19, "BB" => 20, "BD" => 21, "BE" => 22, "BF" => 23,
86
- "BG" => 24, "BH" => 25, "BI" => 26, "BJ" => 27, "BM" => 28, "BN" => 29,
87
- "BO" => 30, "BR" => 31, "BS" => 32, "BT" => 33, "BV" => 34, "BW" => 35,
88
- "BY" => 36, "BZ" => 37, "CA" => 38, "CC" => 39, "CD" => 40, "CF" => 41,
89
- "CG" => 42, "CH" => 43, "CI" => 44, "CK" => 45, "CL" => 46, "CM" => 47,
90
- "CN" => 48, "CO" => 49, "CR" => 50, "CU" => 51, "CV" => 52, "CX" => 53,
91
- "CY" => 54, "CZ" => 55, "DE" => 56, "DJ" => 57, "DK" => 58, "DM" => 59,
92
- "DO" => 60, "DZ" => 61, "EC" => 62, "EE" => 63, "EG" => 64, "EH" => 65,
93
- "ER" => 66, "ES" => 67, "ET" => 68, "FI" => 69, "FJ" => 70, "FK" => 71,
94
- "FM" => 72, "FO" => 73, "FR" => 74, "SX" => 75, "GA" => 76, "GB" => 77,
95
- "GD" => 78, "GE" => 79, "GF" => 80, "GH" => 81, "GI" => 82, "GL" => 83,
96
- "GM" => 84, "GN" => 85, "GP" => 86, "GQ" => 87, "GR" => 88, "GS" => 89,
97
- "GT" => 90, "GU" => 91, "GW" => 92, "GY" => 93, "HK" => 94, "HM" => 95,
98
- "HN" => 96, "HR" => 97, "HT" => 98, "HU" => 99, "ID" => 100, "IE" => 101,
99
- "IL" => 102, "IN" => 103, "IO" => 104, "IQ" => 105, "IR" => 106, "IS" => 107,
100
- "IT" => 108, "JM" => 109, "JO" => 110, "JP" => 111, "KE" => 112, "KG" => 113,
101
- "KH" => 114, "KI" => 115, "KM" => 116, "KN" => 117, "KP" => 118, "KR" => 119,
102
- "KW" => 120, "KY" => 121, "KZ" => 122, "LA" => 123, "LB" => 124, "LC" => 125,
103
- "LI" => 126, "LK" => 127, "LR" => 128, "LS" => 129, "LT" => 130, "LU" => 131,
104
- "LV" => 132, "LY" => 133, "MA" => 134, "MC" => 135, "MD" => 136, "MG" => 137,
105
- "MH" => 138, "MK" => 139, "ML" => 140, "MM" => 141, "MN" => 142, "MO" => 143,
106
- "MP" => 144, "MQ" => 145, "MR" => 146, "MS" => 147, "MT" => 148, "MU" => 149,
107
- "MV" => 150, "MW" => 151, "MX" => 152, "MY" => 153, "MZ" => 154, "NA" => 155,
108
- "NC" => 156, "NE" => 157, "NF" => 158, "NG" => 159, "NI" => 160, "NL" => 161,
109
- "NO" => 162, "NP" => 163, "NR" => 164, "NU" => 165, "NZ" => 166, "OM" => 167,
110
- "PA" => 168, "PE" => 169, "PF" => 170, "PG" => 171, "PH" => 172, "PK" => 173,
111
- "PL" => 174, "PM" => 175, "PN" => 176, "PR" => 177, "PS" => 178, "PT" => 179,
112
- "PW" => 180, "PY" => 181, "QA" => 182, "RE" => 183, "RO" => 184, "RU" => 185,
113
- "RW" => 186, "SA" => 187, "SB" => 188, "SC" => 189, "SD" => 190, "SE" => 191,
114
- "SG" => 192, "SH" => 193, "SI" => 194, "SJ" => 195, "SK" => 196, "SL" => 197,
115
- "SM" => 198, "SN" => 199, "SO" => 200, "SR" => 201, "ST" => 202, "SV" => 203,
116
- "SY" => 204, "SZ" => 205, "TC" => 206, "TD" => 207, "TF" => 208, "TG" => 209,
117
- "TH" => 210, "TJ" => 211, "TK" => 212, "TM" => 213, "TN" => 214, "TO" => 215,
118
- "TL" => 216, "TR" => 217, "TT" => 218, "TV" => 219, "TW" => 220, "TZ" => 221,
119
- "UA" => 222, "UG" => 223, "UM" => 224, "US" => 225, "UY" => 226, "UZ" => 227,
120
- "VA" => 228, "VC" => 229, "VE" => 230, "VG" => 231, "VI" => 232, "VN" => 233,
121
- "VU" => 234, "WF" => 235, "WS" => 236, "YE" => 237, "YT" => 238, "RS" => 239,
122
- "ZA" => 240, "ZM" => 241, "ME" => 242, "ZW" => 243, "A1" => 244, "A2" => 245,
123
- "O1" => 246, "AX" => 247, "GG" => 248, "IM" => 249, "JE" => 250, "BL" => 251,
124
- "MF" => 252, "BQ" => 253,
125
- );
126
- var $GEOIP_COUNTRY_CODES = array(
127
- "","AP","EU","AD","AE","AF","AG","AI","AL","AM","CW",
128
- "AO","AQ","AR","AS","AT","AU","AW","AZ","BA","BB",
129
- "BD","BE","BF","BG","BH","BI","BJ","BM","BN","BO",
130
- "BR","BS","BT","BV","BW","BY","BZ","CA","CC","CD",
131
- "CF","CG","CH","CI","CK","CL","CM","CN","CO","CR",
132
- "CU","CV","CX","CY","CZ","DE","DJ","DK","DM","DO",
133
- "DZ","EC","EE","EG","EH","ER","ES","ET","FI","FJ",
134
- "FK","FM","FO","FR","SX","GA","GB","GD","GE","GF",
135
- "GH","GI","GL","GM","GN","GP","GQ","GR","GS","GT",
136
- "GU","GW","GY","HK","HM","HN","HR","HT","HU","ID",
137
- "IE","IL","IN","IO","IQ","IR","IS","IT","JM","JO",
138
- "JP","KE","KG","KH","KI","KM","KN","KP","KR","KW",
139
- "KY","KZ","LA","LB","LC","LI","LK","LR","LS","LT",
140
- "LU","LV","LY","MA","MC","MD","MG","MH","MK","ML",
141
- "MM","MN","MO","MP","MQ","MR","MS","MT","MU","MV",
142
- "MW","MX","MY","MZ","NA","NC","NE","NF","NG","NI",
143
- "NL","NO","NP","NR","NU","NZ","OM","PA","PE","PF",
144
- "PG","PH","PK","PL","PM","PN","PR","PS","PT","PW",
145
- "PY","QA","RE","RO","RU","RW","SA","SB","SC","SD",
146
- "SE","SG","SH","SI","SJ","SK","SL","SM","SN","SO",
147
- "SR","ST","SV","SY","SZ","TC","TD","TF","TG","TH",
148
- "TJ","TK","TM","TN","TO","TL","TR","TT","TV","TW",
149
- "TZ","UA","UG","UM","US","UY","UZ","VA","VC","VE",
150
- "VG","VI","VN","VU","WF","WS","YE","YT","RS","ZA",
151
- "ZM","ME","ZW","A1","A2","O1","AX","GG","IM","JE",
152
- "BL","MF", "BQ");
153
- var $GEOIP_COUNTRY_CODES3 = array(
154
- "","AP","EU","AND","ARE","AFG","ATG","AIA","ALB","ARM","CUW",
155
- "AGO","ATA","ARG","ASM","AUT","AUS","ABW","AZE","BIH","BRB",
156
- "BGD","BEL","BFA","BGR","BHR","BDI","BEN","BMU","BRN","BOL",
157
- "BRA","BHS","BTN","BVT","BWA","BLR","BLZ","CAN","CCK","COD",
158
- "CAF","COG","CHE","CIV","COK","CHL","CMR","CHN","COL","CRI",
159
- "CUB","CPV","CXR","CYP","CZE","DEU","DJI","DNK","DMA","DOM",
160
- "DZA","ECU","EST","EGY","ESH","ERI","ESP","ETH","FIN","FJI",
161
- "FLK","FSM","FRO","FRA","SXM","GAB","GBR","GRD","GEO","GUF",
162
- "GHA","GIB","GRL","GMB","GIN","GLP","GNQ","GRC","SGS","GTM",
163
- "GUM","GNB","GUY","HKG","HMD","HND","HRV","HTI","HUN","IDN",
164
- "IRL","ISR","IND","IOT","IRQ","IRN","ISL","ITA","JAM","JOR",
165
- "JPN","KEN","KGZ","KHM","KIR","COM","KNA","PRK","KOR","KWT",
166
- "CYM","KAZ","LAO","LBN","LCA","LIE","LKA","LBR","LSO","LTU",
167
- "LUX","LVA","LBY","MAR","MCO","MDA","MDG","MHL","MKD","MLI",
168
- "MMR","MNG","MAC","MNP","MTQ","MRT","MSR","MLT","MUS","MDV",
169
- "MWI","MEX","MYS","MOZ","NAM","NCL","NER","NFK","NGA","NIC",
170
- "NLD","NOR","NPL","NRU","NIU","NZL","OMN","PAN","PER","PYF",
171
- "PNG","PHL","PAK","POL","SPM","PCN","PRI","PSE","PRT","PLW",
172
- "PRY","QAT","REU","ROU","RUS","RWA","SAU","SLB","SYC","SDN",
173
- "SWE","SGP","SHN","SVN","SJM","SVK","SLE","SMR","SEN","SOM",
174
- "SUR","STP","SLV","SYR","SWZ","TCA","TCD","ATF","TGO","THA",
175
- "TJK","TKL","TKM","TUN","TON","TLS","TUR","TTO","TUV","TWN",
176
- "TZA","UKR","UGA","UMI","USA","URY","UZB","VAT","VCT","VEN",
177
- "VGB","VIR","VNM","VUT","WLF","WSM","YEM","MYT","SRB","ZAF",
178
- "ZMB","MNE","ZWE","A1","A2","O1","ALA","GGY","IMN","JEY",
179
- "BLM","MAF", "BES"
180
- );
181
- var $GEOIP_COUNTRY_NAMES = array(
182
- "","Asia/Pacific Region","Europe","Andorra","United Arab Emirates","Afghanistan","Antigua and Barbuda","Anguilla","Albania","Armenia","Curacao",
183
- "Angola","Antarctica","Argentina","American Samoa","Austria","Australia","Aruba","Azerbaijan","Bosnia and Herzegovina","Barbados",
184
- "Bangladesh","Belgium","Burkina Faso","Bulgaria","Bahrain","Burundi","Benin","Bermuda","Brunei Darussalam","Bolivia",
185
- "Brazil","Bahamas","Bhutan","Bouvet Island","Botswana","Belarus","Belize","Canada","Cocos (Keeling) Islands","Congo, The Democratic Republic of the",
186
- "Central African Republic","Congo","Switzerland","Cote D'Ivoire","Cook Islands","Chile","Cameroon","China","Colombia","Costa Rica",
187
- "Cuba","Cape Verde","Christmas Island","Cyprus","Czech Republic","Germany","Djibouti","Denmark","Dominica","Dominican Republic",
188
- "Algeria","Ecuador","Estonia","Egypt","Western Sahara","Eritrea","Spain","Ethiopia","Finland","Fiji",
189
- "Falkland Islands (Malvinas)","Micronesia, Federated States of","Faroe Islands","France","Sint Maarten (Dutch part)","Gabon","United Kingdom","Grenada","Georgia","French Guiana",
190
- "Ghana","Gibraltar","Greenland","Gambia","Guinea","Guadeloupe","Equatorial Guinea","Greece","South Georgia and the South Sandwich Islands","Guatemala",
191
- "Guam","Guinea-Bissau","Guyana","Hong Kong","Heard Island and McDonald Islands","Honduras","Croatia","Haiti","Hungary","Indonesia",
192
- "Ireland","Israel","India","British Indian Ocean Territory","Iraq","Iran, Islamic Republic of","Iceland","Italy","Jamaica","Jordan",
193
- "Japan","Kenya","Kyrgyzstan","Cambodia","Kiribati","Comoros","Saint Kitts and Nevis","Korea, Democratic People's Republic of","Korea, Republic of","Kuwait",
194
- "Cayman Islands","Kazakhstan","Lao People's Democratic Republic","Lebanon","Saint Lucia","Liechtenstein","Sri Lanka","Liberia","Lesotho","Lithuania",
195
- "Luxembourg","Latvia","Libya","Morocco","Monaco","Moldova, Republic of","Madagascar","Marshall Islands","Macedonia","Mali",
196
- "Myanmar","Mongolia","Macau","Northern Mariana Islands","Martinique","Mauritania","Montserrat","Malta","Mauritius","Maldives",
197
- "Malawi","Mexico","Malaysia","Mozambique","Namibia","New Caledonia","Niger","Norfolk Island","Nigeria","Nicaragua",
198
- "Netherlands","Norway","Nepal","Nauru","Niue","New Zealand","Oman","Panama","Peru","French Polynesia",
199
- "Papua New Guinea","Philippines","Pakistan","Poland","Saint Pierre and Miquelon","Pitcairn Islands","Puerto Rico","Palestinian Territory","Portugal","Palau",
200
- "Paraguay","Qatar","Reunion","Romania","Russian Federation","Rwanda","Saudi Arabia","Solomon Islands","Seychelles","Sudan",
201
- "Sweden","Singapore","Saint Helena","Slovenia","Svalbard and Jan Mayen","Slovakia","Sierra Leone","San Marino","Senegal","Somalia","Suriname",
202
- "Sao Tome and Principe","El Salvador","Syrian Arab Republic","Swaziland","Turks and Caicos Islands","Chad","French Southern Territories","Togo","Thailand",
203
- "Tajikistan","Tokelau","Turkmenistan","Tunisia","Tonga","Timor-Leste","Turkey","Trinidad and Tobago","Tuvalu","Taiwan",
204
- "Tanzania, United Republic of","Ukraine","Uganda","United States Minor Outlying Islands","United States","Uruguay","Uzbekistan","Holy See (Vatican City State)","Saint Vincent and the Grenadines","Venezuela",
205
- "Virgin Islands, British","Virgin Islands, U.S.","Vietnam","Vanuatu","Wallis and Futuna","Samoa","Yemen","Mayotte","Serbia","South Africa",
206
- "Zambia","Montenegro","Zimbabwe","Anonymous Proxy","Satellite Provider","Other","Aland Islands","Guernsey","Isle of Man","Jersey",
207
- "Saint Barthelemy","Saint Martin", "Bonaire, Saint Eustatius and Saba"
208
- );
209
-
210
- var $GEOIP_CONTINENT_CODES = array(
211
- "--", "AS","EU","EU","AS","AS","NA","NA","EU","AS","NA",
212
- "AF","AN","SA","OC","EU","OC","NA","AS","EU","NA",
213
- "AS","EU","AF","EU","AS","AF","AF","NA","AS","SA",
214
- "SA","NA","AS","AN","AF","EU","NA","NA","AS","AF",
215
- "AF","AF","EU","AF","OC","SA","AF","AS","SA","NA",
216
- "NA","AF","AS","AS","EU","EU","AF","EU","NA","NA",
217
- "AF","SA","EU","AF","AF","AF","EU","AF","EU","OC",
218
- "SA","OC","EU","EU","NA","AF","EU","NA","AS","SA",
219
- "AF","EU","NA","AF","AF","NA","AF","EU","AN","NA",
220
- "OC","AF","SA","AS","AN","NA","EU","NA","EU","AS",
221
- "EU","AS","AS","AS","AS","AS","EU","EU","NA","AS",
222
- "AS","AF","AS","AS","OC","AF","NA","AS","AS","AS",
223
- "NA","AS","AS","AS","NA","EU","AS","AF","AF","EU",
224
- "EU","EU","AF","AF","EU","EU","AF","OC","EU","AF",
225
- "AS","AS","AS","OC","NA","AF","NA","EU","AF","AS",
226
- "AF","NA","AS","AF","AF","OC","AF","OC","AF","NA",
227
- "EU","EU","AS","OC","OC","OC","AS","NA","SA","OC",
228
- "OC","AS","AS","EU","NA","OC","NA","AS","EU","OC",
229
- "SA","AS","AF","EU","EU","AF","AS","OC","AF","AF",
230
- "EU","AS","AF","EU","EU","EU","AF","EU","AF","AF",
231
- "SA","AF","NA","AS","AF","NA","AF","AN","AF","AS",
232
- "AS","OC","AS","AF","OC","AS","EU","NA","OC","AS",
233
- "AF","EU","AF","OC","NA","SA","AS","EU","NA","SA",
234
- "NA","NA","AS","OC","OC","OC","AS","AF","EU","AF",
235
- "AF","EU","AF","--","--","--","EU","EU","EU","EU",
236
- "NA","NA","NA"
237
- );
238
-
239
- }
240
- function cpd_geoip_load_shared_mem ($file) {
241
-
242
- $fp = fopen($file, "rb");
243
- if (!$fp) {
244
- print "error opening $file: $php_errormsg\n";
245
- exit;
246
- }
247
- $s_array = fstat($fp);
248
- $size = $s_array['size'];
249
- if ($shmid = @shmop_open (GEOIP_SHM_KEY, "w", 0, 0)) {
250
- shmop_delete ($shmid);
251
- shmop_close ($shmid);
252
- }
253
- $shmid = shmop_open (GEOIP_SHM_KEY, "c", 0644, $size);
254
- shmop_write ($shmid, fread($fp, $size), 0);
255
- shmop_close ($shmid);
256
- }
257
-
258
- function cpd_setup_segments($gi){
259
- $gi->databaseType = GEOIP_COUNTRY_EDITION;
260
- $gi->record_length = STANDARD_RECORD_LENGTH;
261
- if ($gi->flags & GEOIP_SHARED_MEMORY) {
262
- $offset = @shmop_size ($gi->shmid) - 3;
263
- for ($i = 0; $i < STRUCTURE_INFO_MAX_SIZE; $i++) {
264
- $delim = @shmop_read ($gi->shmid, $offset, 3);
265
- $offset += 3;
266
- if ($delim == (chr(255).chr(255).chr(255))) {
267
- $gi->databaseType = ord(@shmop_read ($gi->shmid, $offset, 1));
268
- $offset++;
269
-
270
- if ($gi->databaseType == GEOIP_REGION_EDITION_REV0){
271
- $gi->databaseSegments = GEOIP_STATE_BEGIN_REV0;
272
- } else if ($gi->databaseType == GEOIP_REGION_EDITION_REV1){
273
- $gi->databaseSegments = GEOIP_STATE_BEGIN_REV1;
274
- } else if (($gi->databaseType == GEOIP_CITY_EDITION_REV0)||
275
- ($gi->databaseType == GEOIP_CITY_EDITION_REV1)
276
- || ($gi->databaseType == GEOIP_ORG_EDITION)
277
- || ($gi->databaseType == GEOIP_ORG_EDITION_V6)
278
- || ($gi->databaseType == GEOIP_DOMAIN_EDITION)
279
- || ($gi->databaseType == GEOIP_DOMAIN_EDITION_V6)
280
- || ($gi->databaseType == GEOIP_ISP_EDITION)
281
- || ($gi->databaseType == GEOIP_ISP_EDITION_V6)
282
- || ($gi->databaseType == GEOIP_USERTYPE_EDITION)
283
- || ($gi->databaseType == GEOIP_USERTYPE_EDITION_V6)
284
- || ($gi->databaseType == GEOIP_LOCATIONA_EDITION)
285
- || ($gi->databaseType == GEOIP_ACCURACYRADIUS_EDITION)
286
- || ($gi->databaseType == GEOIP_CITY_EDITION_REV0_V6)
287
- || ($gi->databaseType == GEOIP_CITY_EDITION_REV1_V6)
288
- || ($gi->databaseType == GEOIP_NETSPEED_EDITION_REV1)
289
- || ($gi->databaseType == GEOIP_NETSPEED_EDITION_REV1_V6)
290
- || ($gi->databaseType == GEOIP_ASNUM_EDITION)
291
- || ($gi->databaseType == GEOIP_ASNUM_EDITION_V6)){
292
- $gi->databaseSegments = 0;
293
- $buf = @shmop_read ($gi->shmid, $offset, SEGMENT_RECORD_LENGTH);
294
- for ($j = 0;$j < SEGMENT_RECORD_LENGTH;$j++){
295
- $gi->databaseSegments += (ord($buf[$j]) << ($j * 8));
296
- }
297
- if (($gi->databaseType == GEOIP_ORG_EDITION)
298
- || ($gi->databaseType == GEOIP_ORG_EDITION_V6)
299
- || ($gi->databaseType == GEOIP_DOMAIN_EDITION)
300
- || ($gi->databaseType == GEOIP_DOMAIN_EDITION_V6)
301
- || ($gi->databaseType == GEOIP_ISP_EDITION)
302
- || ($gi->databaseType == GEOIP_ISP_EDITION_V6)) {
303
- $gi->record_length = ORG_RECORD_LENGTH;
304
- }
305
- }
306
- break;
307
- } else {
308
- $offset -= 4;
309
- }
310
- }
311
- if (($gi->databaseType == GEOIP_COUNTRY_EDITION)||
312
- ($gi->databaseType == GEOIP_COUNTRY_EDITION_V6)||
313
- ($gi->databaseType == GEOIP_PROXY_EDITION)||
314
- ($gi->databaseType == GEOIP_NETSPEED_EDITION)){
315
- $gi->databaseSegments = GEOIP_COUNTRY_BEGIN;
316
- }
317
- } else {
318
- $filepos = ftell($gi->filehandle);
319
- fseek($gi->filehandle, -3, SEEK_END);
320
- for ($i = 0; $i < STRUCTURE_INFO_MAX_SIZE; $i++) {
321
- $delim = fread($gi->filehandle,3);
322
- if ($delim == (chr(255).chr(255).chr(255))){
323
- $gi->databaseType = ord(fread($gi->filehandle,1));
324
- if ($gi->databaseType == GEOIP_REGION_EDITION_REV0){
325
- $gi->databaseSegments = GEOIP_STATE_BEGIN_REV0;
326
- }
327
- else if ($gi->databaseType == GEOIP_REGION_EDITION_REV1){
328
- $gi->databaseSegments = GEOIP_STATE_BEGIN_REV1;
329
- } else if (($gi->databaseType == GEOIP_CITY_EDITION_REV0)
330
- || ($gi->databaseType == GEOIP_CITY_EDITION_REV1)
331
- || ($gi->databaseType == GEOIP_CITY_EDITION_REV0_V6)
332
- || ($gi->databaseType == GEOIP_CITY_EDITION_REV1_V6)
333
- || ($gi->databaseType == GEOIP_ORG_EDITION)
334
- || ($gi->databaseType == GEOIP_DOMAIN_EDITION)
335
- || ($gi->databaseType == GEOIP_ISP_EDITION)
336
- || ($gi->databaseType == GEOIP_ORG_EDITION_V6)
337
- || ($gi->databaseType == GEOIP_DOMAIN_EDITION_V6)
338
- || ($gi->databaseType == GEOIP_ISP_EDITION_V6)
339
- || ($gi->databaseType == GEOIP_LOCATIONA_EDITION)
340
- || ($gi->databaseType == GEOIP_ACCURACYRADIUS_EDITION)
341
- || ($gi->databaseType == GEOIP_CITY_EDITION_REV0_V6)
342
- || ($gi->databaseType == GEOIP_CITY_EDITION_REV1_V6)
343
- || ($gi->databaseType == GEOIP_NETSPEED_EDITION_REV1)
344
- || ($gi->databaseType == GEOIP_NETSPEED_EDITION_REV1_V6)
345
- || ($gi->databaseType == GEOIP_USERTYPE_EDITION)
346
- || ($gi->databaseType == GEOIP_USERTYPE_EDITION_V6)
347
- || ($gi->databaseType == GEOIP_ASNUM_EDITION)
348
- || ($gi->databaseType == GEOIP_ASNUM_EDITION_V6)){
349
- $gi->databaseSegments = 0;
350
- $buf = fread($gi->filehandle,SEGMENT_RECORD_LENGTH);
351
- for ($j = 0;$j < SEGMENT_RECORD_LENGTH;$j++){
352
- $gi->databaseSegments += (ord($buf[$j]) << ($j * 8));
353
- }
354
- if ( ( $gi->databaseType == GEOIP_ORG_EDITION )
355
- || ( $gi->databaseType == GEOIP_DOMAIN_EDITION )
356
- || ( $gi->databaseType == GEOIP_ISP_EDITION )
357
- || ( $gi->databaseType == GEOIP_ORG_EDITION_V6 )
358
- || ( $gi->databaseType == GEOIP_DOMAIN_EDITION_V6 )
359
- || ( $gi->databaseType == GEOIP_ISP_EDITION_V6 )) {
360
- $gi->record_length = ORG_RECORD_LENGTH;
361
- }
362
- }
363
- break;
364
- } else {
365
- fseek($gi->filehandle, -4, SEEK_CUR);
366
- }
367
- }
368
- if (($gi->databaseType == GEOIP_COUNTRY_EDITION)||
369
- ($gi->databaseType == GEOIP_COUNTRY_EDITION_V6)||
370
- ($gi->databaseType == GEOIP_PROXY_EDITION)||
371
- ($gi->databaseType == GEOIP_NETSPEED_EDITION)){
372
- $gi->databaseSegments = GEOIP_COUNTRY_BEGIN;
373
- }
374
- fseek($gi->filehandle,$filepos,SEEK_SET);
375
- }
376
- return $gi;
377
- }
378
-
379
- function cpd_geoip_open($filename, $flags) {
380
- $gi = new GeoIPCpD;
381
- $gi->flags = $flags;
382
- if ($gi->flags & GEOIP_SHARED_MEMORY) {
383
- $gi->shmid = @shmop_open (GEOIP_SHM_KEY, "a", 0, 0);
384
- } else {
385
- $gi->filehandle = fopen($filename,"rb") or die( "Can not open $filename\n" );
386
- if ($gi->flags & GEOIP_MEMORY_CACHE) {
387
- $s_array = fstat($gi->filehandle);
388
- $gi->memory_buffer = fread($gi->filehandle, $s_array['size']);
389
- }
390
- }
391
-
392
- $gi = cpd_setup_segments($gi);
393
- return $gi;
394
- }
395
-
396
- function cpd_geoip_close($gi) {
397
- if ($gi->flags & GEOIP_SHARED_MEMORY) {
398
- return true;
399
- }
400
-
401
- return fclose($gi->filehandle);
402
- }
403
-
404
- function cpd_geoip_country_id_by_name_v6($gi, $name) {
405
- $rec = dns_get_record($name, DNS_AAAA);
406
- if ( !$rec ) {
407
- return false;
408
- }
409
- $addr = $rec[0]["ipv6"];
410
- if (!$addr || $addr == $name) {
411
- return false;
412
- }
413
- return cpd_geoip_country_id_by_addr_v6($gi, $addr);
414
- }
415
-
416
- function cpd_geoip_country_id_by_name($gi, $name) {
417
- $addr = gethostbyname($name);
418
- if (!$addr || $addr == $name) {
419
- return false;
420
- }
421
- return cpd_geoip_country_id_by_addr($gi, $addr);
422
- }
423
-
424
- function cpd_geoip_country_code_by_name_v6($gi, $name) {
425
- $country_id = cpd_geoip_country_id_by_name_v6($gi,$name);
426
- if ($country_id !== false) {
427
- return $gi->GEOIP_COUNTRY_CODES[$country_id];
428
- }
429
- return false;
430
- }
431
-
432
- function cpd_geoip_country_code_by_name($gi, $name) {
433
- $country_id = cpd_geoip_country_id_by_name($gi,$name);
434
- if ($country_id !== false) {
435
- return $gi->GEOIP_COUNTRY_CODES[$country_id];
436
- }
437
- return false;
438
- }
439
-
440
- function cpd_geoip_country_name_by_name_v6($gi, $name) {
441
- $country_id = cpd_geoip_country_id_by_name_v6($gi,$name);
442
- if ($country_id !== false) {
443
- return $gi->GEOIP_COUNTRY_NAMES[$country_id];
444
- }
445
- return false;
446
- }
447
-
448
- function cpd_geoip_country_name_by_name($gi, $name) {
449
- $country_id = cpd_geoip_country_id_by_name($gi,$name);
450
- if ($country_id !== false) {
451
- return $gi->GEOIP_COUNTRY_NAMES[$country_id];
452
- }
453
- return false;
454
- }
455
-
456
- function cpd_geoip_country_id_by_addr_v6($gi, $addr) {
457
- $ipnum = inet_pton($addr);
458
- return cpd_geoip_seek_country_v6($gi, $ipnum) - GEOIP_COUNTRY_BEGIN;
459
- }
460
-
461
- function cpd_geoip_country_id_by_addr($gi, $addr) {
462
- $ipnum = ip2long($addr);
463
- return cpd_geoip_seek_country($gi, $ipnum) - GEOIP_COUNTRY_BEGIN;
464
- }
465
-
466
- function cpd_geoip_country_code_by_addr_v6($gi, $addr) {
467
- $country_id = cpd_geoip_country_id_by_addr_v6($gi,$addr);
468
- if ($country_id !== false) {
469
- return $gi->GEOIP_COUNTRY_CODES[$country_id];
470
- }
471
- return false;
472
- }
473
-
474
- function cpd_geoip_country_code_by_addr($gi, $addr) {
475
- if ($gi->databaseType == GEOIP_CITY_EDITION_REV1) {
476
- $record = cpd_geoip_record_by_addr($gi,$addr);
477
- if ( $record !== false ) {
478
- return $record->country_code;
479
- }
480
- } else {
481
- $country_id = cpd_geoip_country_id_by_addr($gi,$addr);
482
- if ($country_id !== false) {
483
- return $gi->GEOIP_COUNTRY_CODES[$country_id];
484
- }
485
- }
486
- return false;
487
- }
488
-
489
- function cpd_geoip_country_name_by_addr_v6($gi, $addr) {
490
- $country_id = cpd_geoip_country_id_by_addr_v6($gi,$addr);
491
- if ($country_id !== false) {
492
- return $gi->GEOIP_COUNTRY_NAMES[$country_id];
493
- }
494
- return false;
495
- }
496
-
497
- function cpd_geoip_country_name_by_addr($gi, $addr) {
498
- if ($gi->databaseType == GEOIP_CITY_EDITION_REV1) {
499
- $record = cpd_geoip_record_by_addr($gi,$addr);
500
- return $record->country_name;
501
- } else {
502
- $country_id = cpd_geoip_country_id_by_addr($gi,$addr);
503
- if ($country_id !== false) {
504
- return $gi->GEOIP_COUNTRY_NAMES[$country_id];
505
- }
506
- }
507
- return false;
508
- }
509
-
510
- function cpd_geoip_seek_country_v6($gi, $ipnum) {
511
-
512
- # arrays from unpack start with offset 1
513
- # yet another php mystery. array_merge work around
514
- # this broken behaviour
515
- $v6vec = array_merge(unpack( "C16", $ipnum));
516
-
517
- $offset = 0;
518
- for ($depth = 127; $depth >= 0; --$depth) {
519
- if ($gi->flags & GEOIP_MEMORY_CACHE) {
520
- // workaround php's broken substr, strpos, etc handling with
521
- // mbstring.func_overload and mbstring.internal_encoding
522
- $enc = mb_internal_encoding();
523
- mb_internal_encoding('ISO-8859-1');
524
-
525
- $buf = substr($gi->memory_buffer,
526
- 2 * $gi->record_length * $offset,
527
- 2 * $gi->record_length);
528
-
529
- mb_internal_encoding($enc);
530
- } elseif ($gi->flags & GEOIP_SHARED_MEMORY) {
531
- $buf = @shmop_read ($gi->shmid,
532
- 2 * $gi->record_length * $offset,
533
- 2 * $gi->record_length );
534
- } else {
535
- fseek($gi->filehandle, 2 * $gi->record_length * $offset, SEEK_SET) == 0
536
- or die("fseek failed");
537
- $buf = fread($gi->filehandle, 2 * $gi->record_length);
538
- }
539
- $x = array(0,0);
540
- for ($i = 0; $i < 2; ++$i) {
541
- for ($j = 0; $j < $gi->record_length; ++$j) {
542
- $x[$i] += ord($buf[$gi->record_length * $i + $j]) << ($j * 8);
543
- }
544
- }
545
-
546
- $bnum = 127 - $depth;
547
- $idx = $bnum >> 3;
548
- $b_mask = 1 << ( $bnum & 7 ^ 7 );
549
- if (($v6vec[$idx] & $b_mask) > 0) {
550
- if ($x[1] >= $gi->databaseSegments) {
551
- return $x[1];
552
- }
553
- $offset = $x[1];
554
- } else {
555
- if ($x[0] >= $gi->databaseSegments) {
556
- return $x[0];
557
- }
558
- $offset = $x[0];
559
- }
560
- }
561
- trigger_error("error traversing database - perhaps it is corrupt?", E_USER_ERROR);
562
- return false;
563
- }
564
-
565
- function cpd_geoip_seek_country($gi, $ipnum) {
566
- $offset = 0;
567
- for ($depth = 31; $depth >= 0; --$depth) {
568
- if ($gi->flags & GEOIP_MEMORY_CACHE) {
569
- // workaround php's broken substr, strpos, etc handling with
570
- // mbstring.func_overload and mbstring.internal_encoding
571
- $enc = mb_internal_encoding();
572
- mb_internal_encoding('ISO-8859-1');
573
-
574
- $buf = substr($gi->memory_buffer,
575
- 2 * $gi->record_length * $offset,
576
- 2 * $gi->record_length);
577
-
578
- mb_internal_encoding($enc);
579
- } elseif ($gi->flags & GEOIP_SHARED_MEMORY) {
580
- $buf = @shmop_read ($gi->shmid,
581
- 2 * $gi->record_length * $offset,
582
- 2 * $gi->record_length );
583
- } else {
584
- fseek($gi->filehandle, 2 * $gi->record_length * $offset, SEEK_SET) == 0
585
- or die("fseek failed");
586
- $buf = fread($gi->filehandle, 2 * $gi->record_length);
587
- }
588
- $x = array(0,0);
589
- for ($i = 0; $i < 2; ++$i) {
590
- for ($j = 0; $j < $gi->record_length; ++$j) {
591
- $x[$i] += ord($buf[$gi->record_length * $i + $j]) << ($j * 8);
592
- }
593
- }
594
- if ($ipnum & (1 << $depth)) {
595
- if ($x[1] >= $gi->databaseSegments) {
596
- return $x[1];
597
- }
598
- $offset = $x[1];
599
- } else {
600
- if ($x[0] >= $gi->databaseSegments) {
601
- return $x[0];
602
- }
603
- $offset = $x[0];
604
- }
605
- }
606
- trigger_error("error traversing database - perhaps it is corrupt?", E_USER_ERROR);
607
- return false;
608
- }
609
-
610
- function cpd_common_get_org($gi, $seek_org){
611
- $record_pointer = $seek_org + (2 * $gi->record_length - 1) * $gi->databaseSegments;
612
- if ($gi->flags & GEOIP_SHARED_MEMORY) {
613
- $org_buf = @shmop_read ($gi->shmid, $record_pointer, MAX_ORG_RECORD_LENGTH);
614
- } else {
615
- fseek($gi->filehandle, $record_pointer, SEEK_SET);
616
- $org_buf = fread($gi->filehandle,MAX_ORG_RECORD_LENGTH);
617
- }
618
- // workaround php's broken substr, strpos, etc handling with
619
- // mbstring.func_overload and mbstring.internal_encoding
620
- $enc = mb_internal_encoding();
621
- mb_internal_encoding('ISO-8859-1');
622
- $org_buf = substr($org_buf, 0, strpos($org_buf, "\0"));
623
- mb_internal_encoding($enc);
624
- return $org_buf;
625
- }
626
-
627
- function cpd_get_org_v6($gi,$ipnum){
628
- $seek_org = cpd_geoip_seek_country_v6($gi,$ipnum);
629
- if ($seek_org == $gi->databaseSegments) {
630
- return NULL;
631
- }
632
- return cpd_common_get_org($gi, $seek_org);
633
- }
634
-
635
- function cpd_get_org($gi,$ipnum){
636
- $seek_org = cpd_geoip_seek_country($gi,$ipnum);
637
- if ($seek_org == $gi->databaseSegments) {
638
- return NULL;
639
- }
640
- return cpd_common_get_org($gi, $seek_org);
641
- }
642
-
643
-
644
-
645
- function cpd_geoip_name_by_addr_v6 ($gi,$addr) {
646
- if ($addr == NULL) {
647
- return 0;
648
- }
649
- $ipnum = inet_pton($addr);
650
- return cpd_get_org_v6($gi, $ipnum);
651
- }
652
-
653
- function cpd_geoip_name_by_addr ($gi,$addr) {
654
- if ($addr == NULL) {
655
- return 0;
656
- }
657
- $ipnum = ip2long($addr);
658
- return cpd_get_org($gi, $ipnum);
659
- }
660
-
661
- function cpd_geoip_org_by_addr ($gi,$addr) {
662
- return cpd_geoip_name_by_addr($gi, $addr);
663
- }
664
-
665
- function cpd_get_region($gi,$ipnum){
666
- if ($gi->databaseType == GEOIP_REGION_EDITION_REV0){
667
- $seek_region = cpd_geoip_seek_country($gi,$ipnum) - GEOIP_STATE_BEGIN_REV0;
668
- if ($seek_region >= 1000){
669
- $country_code = "US";
670
- $region = chr(($seek_region - 1000)/26 + 65) . chr(($seek_region - 1000)%26 + 65);
671
- } else {
672
- $country_code = $gi->GEOIP_COUNTRY_CODES[$seek_region];
673
- $region = "";
674
- }
675
- return array ($country_code,$region);
676
- } else if ($gi->databaseType == GEOIP_REGION_EDITION_REV1) {
677
- $seek_region = cpd_geoip_seek_country($gi,$ipnum) - GEOIP_STATE_BEGIN_REV1;
678
- //print $seek_region;
679
- if ($seek_region < US_OFFSET){
680
- $country_code = "";
681
- $region = "";
682
- } else if ($seek_region < CANADA_OFFSET) {
683
- $country_code = "US";
684
- $region = chr(($seek_region - US_OFFSET)/26 + 65) . chr(($seek_region - US_OFFSET)%26 + 65);
685
- } else if ($seek_region < WORLD_OFFSET) {
686
- $country_code = "CA";
687
- $region = chr(($seek_region - CANADA_OFFSET)/26 + 65) . chr(($seek_region - CANADA_OFFSET)%26 + 65);
688
- } else {
689
- $country_code = $gi->GEOIP_COUNTRY_CODES[($seek_region - WORLD_OFFSET) / FIPS_RANGE];
690
- $region = "";
691
- }
692
- return array ($country_code,$region);
693
- }
694
- }
695
-
696
- function cpd_geoip_region_by_addr ($gi,$addr) {
697
- if ($addr == NULL) {
698
- return 0;
699
- }
700
- $ipnum = ip2long($addr);
701
- return cpd_get_region($gi, $ipnum);
702
- }
703
-
704
- function cpd_getdnsattributes ($l,$ip){
705
- $r = new Net_DNS_Resolver();
706
- $r->nameservers = array("ws1.maxmind.com");
707
- $p = $r->search($l."." . $ip .".s.maxmind.com","TXT","IN");
708
- $str = is_object($p->answer[0])?$p->answer[0]->string():'';
709
- $str = substr( $str, 1, -1 );
710
- return $str;
711
- }
712
-
713
- ?>
geoip/geoip.inc CHANGED
@@ -52,6 +52,23 @@ define("GEOIP_UNKNOWN_SPEED", 0);
52
define("GEOIP_DIALUP_SPEED", 1);
53
define("GEOIP_CABLEDSL_SPEED", 2);
54
define("GEOIP_CORPORATE_SPEED", 3);
55
56
class GeoIPCpd {
57
var $flags;
@@ -62,147 +79,163 @@ class GeoIPCpd {
62
var $record_length;
63
var $shmid;
64
var $GEOIP_COUNTRY_CODE_TO_NUMBER = array(
65
- "" => 0, "AP" => 1, "EU" => 2, "AD" => 3, "AE" => 4, "AF" => 5,
66
- "AG" => 6, "AI" => 7, "AL" => 8, "AM" => 9, "AN" => 10, "AO" => 11,
67
- "AQ" => 12, "AR" => 13, "AS" => 14, "AT" => 15, "AU" => 16, "AW" => 17,
68
- "AZ" => 18, "BA" => 19, "BB" => 20, "BD" => 21, "BE" => 22, "BF" => 23,
69
- "BG" => 24, "BH" => 25, "BI" => 26, "BJ" => 27, "BM" => 28, "BN" => 29,
70
- "BO" => 30, "BR" => 31, "BS" => 32, "BT" => 33, "BV" => 34, "BW" => 35,
71
- "BY" => 36, "BZ" => 37, "CA" => 38, "CC" => 39, "CD" => 40, "CF" => 41,
72
- "CG" => 42, "CH" => 43, "CI" => 44, "CK" => 45, "CL" => 46, "CM" => 47,
73
- "CN" => 48, "CO" => 49, "CR" => 50, "CU" => 51, "CV" => 52, "CX" => 53,
74
- "CY" => 54, "CZ" => 55, "DE" => 56, "DJ" => 57, "DK" => 58, "DM" => 59,
75
- "DO" => 60, "DZ" => 61, "EC" => 62, "EE" => 63, "EG" => 64, "EH" => 65,
76
- "ER" => 66, "ES" => 67, "ET" => 68, "FI" => 69, "FJ" => 70, "FK" => 71,
77
- "FM" => 72, "FO" => 73, "FR" => 74, "FX" => 75, "GA" => 76, "GB" => 77,
78
- "GD" => 78, "GE" => 79, "GF" => 80, "GH" => 81, "GI" => 82, "GL" => 83,
79
- "GM" => 84, "GN" => 85, "GP" => 86, "GQ" => 87, "GR" => 88, "GS" => 89,
80
- "GT" => 90, "GU" => 91, "GW" => 92, "GY" => 93, "HK" => 94, "HM" => 95,
81
- "HN" => 96, "HR" => 97, "HT" => 98, "HU" => 99, "ID" => 100, "IE" => 101,
82
- "IL" => 102, "IN" => 103, "IO" => 104, "IQ" => 105, "IR" => 106, "IS" => 107,
83
- "IT" => 108, "JM" => 109, "JO" => 110, "JP" => 111, "KE" => 112, "KG" => 113,
84
- "KH" => 114, "KI" => 115, "KM" => 116, "KN" => 117, "KP" => 118, "KR" => 119,
85
- "KW" => 120, "KY" => 121, "KZ" => 122, "LA" => 123, "LB" => 124, "LC" => 125,
86
- "LI" => 126, "LK" => 127, "LR" => 128, "LS" => 129, "LT" => 130, "LU" => 131,
87
- "LV" => 132, "LY" => 133, "MA" => 134, "MC" => 135, "MD" => 136, "MG" => 137,
88
- "MH" => 138, "MK" => 139, "ML" => 140, "MM" => 141, "MN" => 142, "MO" => 143,
89
- "MP" => 144, "MQ" => 145, "MR" => 146, "MS" => 147, "MT" => 148, "MU" => 149,
90
"MV" => 150, "MW" => 151, "MX" => 152, "MY" => 153, "MZ" => 154, "NA" => 155,
91
- "NC" => 156, "NE" => 157, "NF" => 158, "NG" => 159, "NI" => 160, "NL" => 161,
92
- "NO" => 162, "NP" => 163, "NR" => 164, "NU" => 165, "NZ" => 166, "OM" => 167,
93
- "PA" => 168, "PE" => 169, "PF" => 170, "PG" => 171, "PH" => 172, "PK" => 173,
94
- "PL" => 174, "PM" => 175, "PN" => 176, "PR" => 177, "PS" => 178, "PT" => 179,
95
- "PW" => 180, "PY" => 181, "QA" => 182, "RE" => 183, "RO" => 184, "RU" => 185,
96
- "RW" => 186, "SA" => 187, "SB" => 188, "SC" => 189, "SD" => 190, "SE" => 191,
97
- "SG" => 192, "SH" => 193, "SI" => 194, "SJ" => 195, "SK" => 196, "SL" => 197,
98
- "SM" => 198, "SN" => 199, "SO" => 200, "SR" => 201, "ST" => 202, "SV" => 203,
99
- "SY" => 204, "SZ" => 205, "TC" => 206, "TD" => 207, "TF" => 208, "TG" => 209,
100
- "TH" => 210, "TJ" => 211, "TK" => 212, "TM" => 213, "TN" => 214, "TO" => 215,
101
- "TL" => 216, "TR" => 217, "TT" => 218, "TV" => 219, "TW" => 220, "TZ" => 221,
102
- "UA" => 222, "UG" => 223, "UM" => 224, "US" => 225, "UY" => 226, "UZ" => 227,
103
"VA" => 228, "VC" => 229, "VE" => 230, "VG" => 231, "VI" => 232, "VN" => 233,
104
- "VU" => 234, "WF" => 235, "WS" => 236, "YE" => 237, "YT" => 238, "RS" => 239,
105
- "ZA" => 240, "ZM" => 241, "ME" => 242, "ZW" => 243, "A1" => 244, "A2" => 245,
106
"O1" => 246, "AX" => 247, "GG" => 248, "IM" => 249, "JE" => 250, "BL" => 251,
107
- "MF" => 252
108
);
109
var $GEOIP_COUNTRY_CODES = array(
110
- "", "AP", "EU", "AD", "AE", "AF", "AG", "AI", "AL", "AM", "AN", "AO", "AQ",
111
- "AR", "AS", "AT", "AU", "AW", "AZ", "BA", "BB", "BD", "BE", "BF", "BG", "BH",
112
- "BI", "BJ", "BM", "BN", "BO", "BR", "BS", "BT", "BV", "BW", "BY", "BZ", "CA",
113
- "CC", "CD", "CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR", "CU",
114
- "CV", "CX", "CY", "CZ", "DE", "DJ", "DK", "DM", "DO", "DZ", "EC", "EE", "EG",
115
- "EH", "ER", "ES", "ET", "FI", "FJ", "FK", "FM", "FO", "FR", "FX", "GA", "GB",
116
- "GD", "GE", "GF", "GH", "GI", "GL", "GM", "GN", "GP", "GQ", "GR", "GS", "GT",
117
- "GU", "GW", "GY", "HK", "HM", "HN", "HR", "HT", "HU", "ID", "IE", "IL", "IN",
118
- "IO", "IQ", "IR", "IS", "IT", "JM", "JO", "JP", "KE", "KG", "KH", "KI", "KM",
119
- "KN", "KP", "KR", "KW", "KY", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS",
120
- "LT", "LU", "LV", "LY", "MA", "MC", "MD", "MG", "MH", "MK", "ML", "MM", "MN",
121
- "MO", "MP", "MQ", "MR", "MS", "MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA",
122
- "NC", "NE", "NF", "NG", "NI", "NL", "NO", "NP", "NR", "NU", "NZ", "OM", "PA",
123
- "PE", "PF", "PG", "PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT", "PW", "PY",
124
- "QA", "RE", "RO", "RU", "RW", "SA", "SB", "SC", "SD", "SE", "SG", "SH", "SI",
125
- "SJ", "SK", "SL", "SM", "SN", "SO", "SR", "ST", "SV", "SY", "SZ", "TC", "TD",
126
- "TF", "TG", "TH", "TJ", "TK", "TM", "TN", "TO", "TL", "TR", "TT", "TV", "TW",
127
- "TZ", "UA", "UG", "UM", "US", "UY", "UZ", "VA", "VC", "VE", "VG", "VI", "VN",
128
- "VU", "WF", "WS", "YE", "YT", "RS", "ZA", "ZM", "ME", "ZW", "A1", "A2", "O1",
129
- "AX", "GG", "IM", "JE", "BL", "MF"
130
- );
131
var $GEOIP_COUNTRY_CODES3 = array(
132
- "","AP","EU","AND","ARE","AFG","ATG","AIA","ALB","ARM","ANT","AGO","AQ","ARG",
133
- "ASM","AUT","AUS","ABW","AZE","BIH","BRB","BGD","BEL","BFA","BGR","BHR","BDI",
134
- "BEN","BMU","BRN","BOL","BRA","BHS","BTN","BV","BWA","BLR","BLZ","CAN","CC",
135
- "COD","CAF","COG","CHE","CIV","COK","CHL","CMR","CHN","COL","CRI","CUB","CPV",
136
- "CX","CYP","CZE","DEU","DJI","DNK","DMA","DOM","DZA","ECU","EST","EGY","ESH",
137
- "ERI","ESP","ETH","FIN","FJI","FLK","FSM","FRO","FRA","FX","GAB","GBR","GRD",
138
- "GEO","GUF","GHA","GIB","GRL","GMB","GIN","GLP","GNQ","GRC","GS","GTM","GUM",
139
- "GNB","GUY","HKG","HM","HND","HRV","HTI","HUN","IDN","IRL","ISR","IND","IO",
140
- "IRQ","IRN","ISL","ITA","JAM","JOR","JPN","KEN","KGZ","KHM","KIR","COM","KNA",
141
- "PRK","KOR","KWT","CYM","KAZ","LAO","LBN","LCA","LIE","LKA","LBR","LSO","LTU",
142
- "LUX","LVA","LBY","MAR","MCO","MDA","MDG","MHL","MKD","MLI","MMR","MNG","MAC",
143
- "MNP","MTQ","MRT","MSR","MLT","MUS","MDV","MWI","MEX","MYS","MOZ","NAM","NCL",
144
- "NER","NFK","NGA","NIC","NLD","NOR","NPL","NRU","NIU","NZL","OMN","PAN","PER",
145
- "PYF","PNG","PHL","PAK","POL","SPM","PCN","PRI","PSE","PRT","PLW","PRY","QAT",
146
- "REU","ROU","RUS","RWA","SAU","SLB","SYC","SDN","SWE","SGP","SHN","SVN","SJM",
147
- "SVK","SLE","SMR","SEN","SOM","SUR","STP","SLV","SYR","SWZ","TCA","TCD","TF",
148
- "TGO","THA","TJK","TKL","TLS","TKM","TUN","TON","TUR","TTO","TUV","TWN","TZA",
149
- "UKR","UGA","UM","USA","URY","UZB","VAT","VCT","VEN","VGB","VIR","VNM","VUT",
150
- "WLF","WSM","YEM","YT","SRB","ZAF","ZMB","MNE","ZWE","A1","A2","O1",
151
- "ALA","GGY","IMN","JEY","BLM","MAF"
152
- );
153
var $GEOIP_COUNTRY_NAMES = array(
154
- "", "Asia/Pacific Region", "Europe", "Andorra", "United Arab Emirates",
155
- "Afghanistan", "Antigua and Barbuda", "Anguilla", "Albania", "Armenia",
156
- "Netherlands Antilles", "Angola", "Antarctica", "Argentina", "American Samoa",
157
- "Austria", "Australia", "Aruba", "Azerbaijan", "Bosnia and Herzegovina",
158
- "Barbados", "Bangladesh", "Belgium", "Burkina Faso", "Bulgaria", "Bahrain",
159
- "Burundi", "Benin", "Bermuda", "Brunei Darussalam", "Bolivia", "Brazil",
160
- "Bahamas", "Bhutan", "Bouvet Island", "Botswana", "Belarus", "Belize",
161
- "Canada", "Cocos (Keeling) Islands", "Congo, The Democratic Republic of the",
162
- "Central African Republic", "Congo", "Switzerland", "Cote D'Ivoire", "Cook Islands",
163
- "Chile", "Cameroon", "China", "Colombia", "Costa Rica", "Cuba", "Cape Verde",
164
- "Christmas Island", "Cyprus", "Czech Republic", "Germany", "Djibouti",
165
- "Denmark", "Dominica", "Dominican Republic", "Algeria", "Ecuador", "Estonia",
166
- "Egypt", "Western Sahara", "Eritrea", "Spain", "Ethiopia", "Finland", "Fiji",
167
- "Falkland Islands (Malvinas)", "Micronesia, Federated States of", "Faroe Islands",
168
- "France", "France, Metropolitan", "Gabon", "United Kingdom",
169
- "Grenada", "Georgia", "French Guiana", "Ghana", "Gibraltar", "Greenland",
170
- "Gambia", "Guinea", "Guadeloupe", "Equatorial Guinea", "Greece", "South Georgia and the South Sandwich Islands",
171
- "Guatemala", "Guam", "Guinea-Bissau",
172
- "Guyana", "Hong Kong", "Heard Island and McDonald Islands", "Honduras",
173
- "Croatia", "Haiti", "Hungary", "Indonesia", "Ireland", "Israel", "India",
174
- "British Indian Ocean Territory", "Iraq", "Iran, Islamic Republic of",
175
- "Iceland", "Italy", "Jamaica", "Jordan", "Japan", "Kenya", "Kyrgyzstan",
176
- "Cambodia", "Kiribati", "Comoros", "Saint Kitts and Nevis", "Korea, Democratic People's Republic of",
177
- "Korea, Republic of", "Kuwait", "Cayman Islands",
178
- "Kazakhstan", "Lao People's Democratic Republic", "Lebanon", "Saint Lucia",
179
- "Liechtenstein", "Sri Lanka", "Liberia", "Lesotho", "Lithuania", "Luxembourg",
180
- "Latvia", "Libyan Arab Jamahiriya", "Morocco", "Monaco", "Moldova, Republic of",
181
- "Madagascar", "Marshall Islands", "Macedonia",
182
- "Mali", "Myanmar", "Mongolia", "Macau", "Northern Mariana Islands",
183
- "Martinique", "Mauritania", "Montserrat", "Malta", "Mauritius", "Maldives",
184
- "Malawi", "Mexico", "Malaysia", "Mozambique", "Namibia", "New Caledonia",
185
- "Niger", "Norfolk Island", "Nigeria", "Nicaragua", "Netherlands", "Norway",
186
- "Nepal", "Nauru", "Niue", "New Zealand", "Oman", "Panama", "Peru", "French Polynesia",
187
- "Papua New Guinea", "Philippines", "Pakistan", "Poland", "Saint Pierre and Miquelon",
188
- "Pitcairn Islands", "Puerto Rico", "Palestinian Territory",
189
- "Portugal", "Palau", "Paraguay", "Qatar", "Reunion", "Romania",
190
- "Russian Federation", "Rwanda", "Saudi Arabia", "Solomon Islands",
191
- "Seychelles", "Sudan", "Sweden", "Singapore", "Saint Helena", "Slovenia",
192
- "Svalbard and Jan Mayen", "Slovakia", "Sierra Leone", "San Marino", "Senegal",
193
- "Somalia", "Suriname", "Sao Tome and Principe", "El Salvador", "Syrian Arab Republic",
194
- "Swaziland", "Turks and Caicos Islands", "Chad", "French Southern Territories",
195
- "Togo", "Thailand", "Tajikistan", "Tokelau", "Turkmenistan",
196
- "Tunisia", "Tonga", "Timor-Leste", "Turkey", "Trinidad and Tobago", "Tuvalu",
197
- "Taiwan", "Tanzania, United Republic of", "Ukraine",
198
- "Uganda", "United States Minor Outlying Islands", "United States", "Uruguay",
199
- "Uzbekistan", "Holy See (Vatican City State)", "Saint Vincent and the Grenadines",
200
- "Venezuela", "Virgin Islands, British", "Virgin Islands, U.S.",
201
- "Vietnam", "Vanuatu", "Wallis and Futuna", "Samoa", "Yemen", "Mayotte",
202
- "Serbia", "South Africa", "Zambia", "Montenegro", "Zimbabwe",
203
- "Anonymous Proxy","Satellite Provider","Other",
204
- "Aland Islands","Guernsey","Isle of Man","Jersey","Saint Barthelemy","Saint Martin"
205
);
206
}
207
208
function cpd_geoip_load_shared_mem ($file) {
@@ -223,7 +256,7 @@ function cpd_geoip_load_shared_mem ($file) {
223
shmop_close ($shmid);
224
}
225
226
- function cpd_setup_segments($gi){
227
$gi->databaseType = GEOIP_COUNTRY_EDITION;
228
$gi->record_length = STANDARD_RECORD_LENGTH;
229
if ($gi->flags & GEOIP_SHARED_MEMORY) {
@@ -242,15 +275,32 @@ function cpd_setup_segments($gi){
242
} else if (($gi->databaseType == GEOIP_CITY_EDITION_REV0)||
243
($gi->databaseType == GEOIP_CITY_EDITION_REV1)
244
|| ($gi->databaseType == GEOIP_ORG_EDITION)
245
|| ($gi->databaseType == GEOIP_ISP_EDITION)
246
- || ($gi->databaseType == GEOIP_ASNUM_EDITION)){
247
$gi->databaseSegments = 0;
248
$buf = @shmop_read ($gi->shmid, $offset, SEGMENT_RECORD_LENGTH);
249
for ($j = 0;$j < SEGMENT_RECORD_LENGTH;$j++){
250
$gi->databaseSegments += (ord($buf[$j]) << ($j * 8));
251
}
252
- if (($gi->databaseType == GEOIP_ORG_EDITION)||
253
- ($gi->databaseType == GEOIP_ISP_EDITION)) {
254
$gi->record_length = ORG_RECORD_LENGTH;
255
}
256
}
@@ -260,6 +310,7 @@ function cpd_setup_segments($gi){
260
}
261
}
262
if (($gi->databaseType == GEOIP_COUNTRY_EDITION)||
263
($gi->databaseType == GEOIP_PROXY_EDITION)||
264
($gi->databaseType == GEOIP_NETSPEED_EDITION)){
265
$gi->databaseSegments = GEOIP_COUNTRY_BEGIN;
@@ -276,18 +327,37 @@ function cpd_setup_segments($gi){
276
}
277
else if ($gi->databaseType == GEOIP_REGION_EDITION_REV1){
278
$gi->databaseSegments = GEOIP_STATE_BEGIN_REV1;
279
- } else if (($gi->databaseType == GEOIP_CITY_EDITION_REV0) ||
280
- ($gi->databaseType == GEOIP_CITY_EDITION_REV1) ||
281
- ($gi->databaseType == GEOIP_ORG_EDITION) ||
282
- ($gi->databaseType == GEOIP_ISP_EDITION) ||
283
- ($gi->databaseType == GEOIP_ASNUM_EDITION)){
284
$gi->databaseSegments = 0;
285
$buf = fread($gi->filehandle,SEGMENT_RECORD_LENGTH);
286
for ($j = 0;$j < SEGMENT_RECORD_LENGTH;$j++){
287
$gi->databaseSegments += (ord($buf[$j]) << ($j * 8));
288
}
289
- if ($gi->databaseType == GEOIP_ORG_EDITION ||
290
- $gi->databaseType == GEOIP_ISP_EDITION) {
291
$gi->record_length = ORG_RECORD_LENGTH;
292
}
293
}
@@ -297,6 +367,7 @@ function cpd_setup_segments($gi){
297
}
298
}
299
if (($gi->databaseType == GEOIP_COUNTRY_EDITION)||
300
($gi->databaseType == GEOIP_PROXY_EDITION)||
301
($gi->databaseType == GEOIP_NETSPEED_EDITION)){
302
$gi->databaseSegments = GEOIP_COUNTRY_BEGIN;
@@ -307,7 +378,7 @@ function cpd_setup_segments($gi){
307
}
308
309
function cpd_geoip_open($filename, $flags) {
310
- $gi = new GeoIPCpD;
311
$gi->flags = $flags;
312
if ($gi->flags & GEOIP_SHARED_MEMORY) {
313
$gi->shmid = @shmop_open (GEOIP_SHM_KEY, "a", 0, 0);
@@ -319,7 +390,7 @@ function cpd_geoip_open($filename, $flags) {
319
}
320
}
321
322
- $gi = cpd_setup_segments($gi);
323
return $gi;
324
}
325
@@ -331,6 +402,18 @@ function cpd_geoip_close($gi) {
331
return fclose($gi->filehandle);
332
}
333
334
function cpd_geoip_country_id_by_name($gi, $name) {
335
$addr = gethostbyname($name);
336
if (!$addr || $addr == $name) {
@@ -339,6 +422,14 @@ function cpd_geoip_country_id_by_name($gi, $name) {
339
return cpd_geoip_country_id_by_addr($gi, $addr);
340
}
341
342
function cpd_geoip_country_code_by_name($gi, $name) {
343
$country_id = cpd_geoip_country_id_by_name($gi,$name);
344
if ($country_id !== false) {
@@ -347,6 +438,14 @@ function cpd_geoip_country_code_by_name($gi, $name) {
347
return false;
348
}
349
350
function cpd_geoip_country_name_by_name($gi, $name) {
351
$country_id = cpd_geoip_country_id_by_name($gi,$name);
352
if ($country_id !== false) {
@@ -355,9 +454,22 @@ function cpd_geoip_country_name_by_name($gi, $name) {
355
return false;
356
}
357
358
function cpd_geoip_country_id_by_addr($gi, $addr) {
359
$ipnum = ip2long($addr);
360
- return cpd_geoip_seek_country($gi, $ipnum) - GEOIP_COUNTRY_BEGIN;
361
}
362
363
function cpd_geoip_country_code_by_addr($gi, $addr) {
@@ -375,6 +487,14 @@ function cpd_geoip_country_code_by_addr($gi, $addr) {
375
return false;
376
}
377
378
function cpd_geoip_country_name_by_addr($gi, $addr) {
379
if ($gi->databaseType == GEOIP_CITY_EDITION_REV1) {
380
$record = cpd_geoip_record_by_addr($gi,$addr);
@@ -388,14 +508,76 @@ function cpd_geoip_country_name_by_addr($gi, $addr) {
388
return false;
389
}
390
391
- function cpd_geoip_seek_country($gi, $ipnum) {
392
$offset = 0;
393
for ($depth = 31; $depth >= 0; --$depth) {
394
if ($gi->flags & GEOIP_MEMORY_CACHE) {
395
$buf = substr($gi->memory_buffer,
396
2 * $gi->record_length * $offset,
397
2 * $gi->record_length);
398
- } elseif ($gi->flags & GEOIP_SHARED_MEMORY) {
399
$buf = @shmop_read ($gi->shmid,
400
2 * $gi->record_length * $offset,
401
2 * $gi->record_length );
@@ -426,77 +608,107 @@ function cpd_geoip_seek_country($gi, $ipnum) {
426
return false;
427
}
428
429
- //function cpd_get_org($gi,$ipnum){
430
- // $seek_org = cpd_geoip_seek_country($gi,$ipnum);
431
- // if ($seek_org == $gi->databaseSegments) {
432
- // return NULL;
433
- // }
434
- // $record_pointer = $seek_org + (2 * $gi->record_length - 1) * $gi->databaseSegments;
435
- // if ($gi->flags & GEOIP_SHARED_MEMORY) {
436
- // $org_buf = @shmop_read ($gi->shmid, $record_pointer, MAX_ORG_RECORD_LENGTH);
437
- // } else {
438
- // fseek($gi->filehandle, $record_pointer, SEEK_SET);
439
- // $org_buf = fread($gi->filehandle,MAX_ORG_RECORD_LENGTH);
440
- // }
441
- // $org_buf = substr($org_buf, 0, strpos($org_buf, 0));
442
- // return $org_buf;
443
- //}
444
- //
445
- //function cpd_geoip_org_by_addr ($gi,$addr) {
446
- // if ($addr == NULL) {
447
- // return 0;
448
- // }
449
- // $ipnum = ip2long($addr);
450
- // return cpd_get_org($gi, $ipnum);
451
- //}
452
-
453
- //function cpd_get_region($gi,$ipnum){
454
- // if ($gi->databaseType == GEOIP_REGION_EDITION_REV0){
455
- // $seek_region = cpd_geoip_seek_country($gi,$ipnum) - GEOIP_STATE_BEGIN_REV0;
456
- // if ($seek_region >= 1000){
457
- // $country_code = "US";
458
- // $region = chr(($seek_region - 1000)/26 + 65) . chr(($seek_region - 1000)%26 + 65);
459
- // } else {
460
- // $country_code = $gi->GEOIP_COUNTRY_CODES[$seek_region];
461
- // $region = "";
462
- // }
463
- // return array ($country_code,$region);
464
- // } else if ($gi->databaseType == GEOIP_REGION_EDITION_REV1) {
465
- // $seek_region = cpd_geoip_seek_country($gi,$ipnum) - GEOIP_STATE_BEGIN_REV1;
466
- // //print $seek_region;
467
- // if ($seek_region < US_OFFSET){
468
- // $country_code = "";
469
- // $region = "";
470
- // } else if ($seek_region < CANADA_OFFSET) {
471
- // $country_code = "US";
472
- // $region = chr(($seek_region - US_OFFSET)/26 + 65) . chr(($seek_region - US_OFFSET)%26 + 65);
473
- // } else if ($seek_region < WORLD_OFFSET) {
474
- // $country_code = "CA";
475
- // $region = chr(($seek_region - CANADA_OFFSET)/26 + 65) . chr(($seek_region - CANADA_OFFSET)%26 + 65);
476
- // } else {
477
- // $country_code = $gi->GEOIP_COUNTRY_CODES[($seek_region - WORLD_OFFSET) / FIPS_RANGE];
478
- // $region = "";
479
- // }
480
- // return array ($country_code,$region);
481
- // }
482
- //}
483
-
484
- //function cpd_geoip_region_by_addr ($gi,$addr) {
485
- // if ($addr == NULL) {
486
- // return 0;
487
- // }
488
- // $ipnum = ip2long($addr);
489
- // return cpd_get_region($gi, $ipnum);
490
- //}
491
-
492
- //function cpd_getdnsattributes ($l,$ip){
493
- // $r = new Net_DNS_Resolver();
494
- // $r->nameservers = array("ws1.maxmind.com");
495
- // $p = $r->search($l."." . $ip .".s.maxmind.com","TXT","IN");
496
- // $str = is_object($p->answer[0])?$p->answer[0]->string():'';
497
- // ereg("\"(.*)\"",$str,$regs);
498
- // $str = $regs[1];
499
- // return $str;
500
- //}
501
502
?>
52
define("GEOIP_DIALUP_SPEED", 1);
53
define("GEOIP_CABLEDSL_SPEED", 2);
54
define("GEOIP_CORPORATE_SPEED", 3);
55
+ define("GEOIP_DOMAIN_EDITION", 11);
56
+ define("GEOIP_COUNTRY_EDITION_V6", 12);
57
+ define("GEOIP_LOCATIONA_EDITION", 13);
58
+ define("GEOIP_ACCURACYRADIUS_EDITION", 14);
59
+ define("GEOIP_CITYCOMBINED_EDITION", 15);
60
+ define("GEOIP_CITY_EDITION_REV1_V6", 30);
61
+ define("GEOIP_CITY_EDITION_REV0_V6",31);
62
+ define("GEOIP_NETSPEED_EDITION_REV1",32);
63
+ define("GEOIP_NETSPEED_EDITION_REV1_V6",33);
64
+ define("GEOIP_USERTYPE_EDITION",28);
65
+ define("GEOIP_USERTYPE_EDITION_V6",29);
66
+ define("GEOIP_ASNUM_EDITION_V6",21);
67
+ define("GEOIP_ISP_EDITION_V6",22);
68
+ define("GEOIP_ORG_EDITION_V6",23);
69
+ define("GEOIP_DOMAIN_EDITION_V6",24);
70
+
71
+ define("CITYCOMBINED_FIXED_RECORD", 7 );
72
73
class GeoIPCpd {
74
var $flags;
79
var $record_length;
80
var $shmid;
81
var $GEOIP_COUNTRY_CODE_TO_NUMBER = array(
82
+ "" => 0, "AP" => 1, "EU" => 2, "AD" => 3, "AE" => 4, "AF" => 5,
83
+ "AG" => 6, "AI" => 7, "AL" => 8, "AM" => 9, "CW" => 10, "AO" => 11,
84
+ "AQ" => 12, "AR" => 13, "AS" => 14, "AT" => 15, "AU" => 16, "AW" => 17,
85
+ "AZ" => 18, "BA" => 19, "BB" => 20, "BD" => 21, "BE" => 22, "BF" => 23,
86
+ "BG" => 24, "BH" => 25, "BI" => 26, "BJ" => 27, "BM" => 28, "BN" => 29,
87
+ "BO" => 30, "BR" => 31, "BS" => 32, "BT" => 33, "BV" => 34, "BW" => 35,
88
+ "BY" => 36, "BZ" => 37, "CA" => 38, "CC" => 39, "CD" => 40, "CF" => 41,
89
+ "CG" => 42, "CH" => 43, "CI" => 44, "CK" => 45, "CL" => 46, "CM" => 47,
90
+ "CN" => 48, "CO" => 49, "CR" => 50, "CU" => 51, "CV" => 52, "CX" => 53,
91
+ "CY" => 54, "CZ" => 55, "DE" => 56, "DJ" => 57, "DK" => 58, "DM" => 59,
92
+ "DO" => 60, "DZ" => 61, "EC" => 62, "EE" => 63, "EG" => 64, "EH" => 65,
93
+ "ER" => 66, "ES" => 67, "ET" => 68, "FI" => 69, "FJ" => 70, "FK" => 71,
94
+ "FM" => 72, "FO" => 73, "FR" => 74, "SX" => 75, "GA" => 76, "GB" => 77,
95
+ "GD" => 78, "GE" => 79, "GF" => 80, "GH" => 81, "GI" => 82, "GL" => 83,
96
+ "GM" => 84, "GN" => 85, "GP" => 86, "GQ" => 87, "GR" => 88, "GS" => 89,
97
+ "GT" => 90, "GU" => 91, "GW" => 92, "GY" => 93, "HK" => 94, "HM" => 95,
98
+ "HN" => 96, "HR" => 97, "HT" => 98, "HU" => 99, "ID" => 100, "IE" => 101,
99
+ "IL" => 102, "IN" => 103, "IO" => 104, "IQ" => 105, "IR" => 106, "IS" => 107,
100
+ "IT" => 108, "JM" => 109, "JO" => 110, "JP" => 111, "KE" => 112, "KG" => 113,
101
+ "KH" => 114, "KI" => 115, "KM" => 116, "KN" => 117, "KP" => 118, "KR" => 119,
102
+ "KW" => 120, "KY" => 121, "KZ" => 122, "LA" => 123, "LB" => 124, "LC" => 125,
103
+ "LI" => 126, "LK" => 127, "LR" => 128, "LS" => 129, "LT" => 130, "LU" => 131,
104
+ "LV" => 132, "LY" => 133, "MA" => 134, "MC" => 135, "MD" => 136, "MG" => 137,
105
+ "MH" => 138, "MK" => 139, "ML" => 140, "MM" => 141, "MN" => 142, "MO" => 143,
106
+ "MP" => 144, "MQ" => 145, "MR" => 146, "MS" => 147, "MT" => 148, "MU" => 149,
107
"MV" => 150, "MW" => 151, "MX" => 152, "MY" => 153, "MZ" => 154, "NA" => 155,
108
+ "NC" => 156, "NE" => 157, "NF" => 158, "NG" => 159, "NI" => 160, "NL" => 161,
109
+ "NO" => 162, "NP" => 163, "NR" => 164, "NU" => 165, "NZ" => 166, "OM" => 167,
110
+ "PA" => 168, "PE" => 169, "PF" => 170, "PG" => 171, "PH" => 172, "PK" => 173,
111
+ "PL" => 174, "PM" => 175, "PN" => 176, "PR" => 177, "PS" => 178, "PT" => 179,
112
+ "PW" => 180, "PY" => 181, "QA" => 182, "RE" => 183, "RO" => 184, "RU" => 185,
113
+ "RW" => 186, "SA" => 187, "SB" => 188, "SC" => 189, "SD" => 190, "SE" => 191,
114
+ "SG" => 192, "SH" => 193, "SI" => 194, "SJ" => 195, "SK" => 196, "SL" => 197,
115
+ "SM" => 198, "SN" => 199, "SO" => 200, "SR" => 201, "ST" => 202, "SV" => 203,
116
+ "SY" => 204, "SZ" => 205, "TC" => 206, "TD" => 207, "TF" => 208, "TG" => 209,
117
+ "TH" => 210, "TJ" => 211, "TK" => 212, "TM" => 213, "TN" => 214, "TO" => 215,
118
+ "TL" => 216, "TR" => 217, "TT" => 218, "TV" => 219, "TW" => 220, "TZ" => 221,
119
+ "UA" => 222, "UG" => 223, "UM" => 224, "US" => 225, "UY" => 226, "UZ" => 227,
120
"VA" => 228, "VC" => 229, "VE" => 230, "VG" => 231, "VI" => 232, "VN" => 233,
121
+ "VU" => 234, "WF" => 235, "WS" => 236, "YE" => 237, "YT" => 238, "RS" => 239,
122
+ "ZA" => 240, "ZM" => 241, "ME" => 242, "ZW" => 243, "A1" => 244, "A2" => 245,
123
"O1" => 246, "AX" => 247, "GG" => 248, "IM" => 249, "JE" => 250, "BL" => 251,
124
+ "MF" => 252, "BQ" => 253,
125
);
126
var $GEOIP_COUNTRY_CODES = array(
127
+ "","AP","EU","AD","AE","AF","AG","AI","AL","AM","CW",
128
+ "AO","AQ","AR","AS","AT","AU","AW","AZ","BA","BB",
129
+ "BD","BE","BF","BG","BH","BI","BJ","BM","BN","BO",
130
+ "BR","BS","BT","BV","BW","BY","BZ","CA","CC","CD",
131
+ "CF","CG","CH","CI","CK","CL","CM","CN","CO","CR",
132
+ "CU","CV","CX","CY","CZ","DE","DJ","DK","DM","DO",
133
+ "DZ","EC","EE","EG","EH","ER","ES","ET","FI","FJ",
134
+ "FK","FM","FO","FR","SX","GA","GB","GD","GE","GF",
135
+ "GH","GI","GL","GM","GN","GP","GQ","GR","GS","GT",
136
+ "GU","GW","GY","HK","HM","HN","HR","HT","HU","ID",
137
+ "IE","IL","IN","IO","IQ","IR","IS","IT","JM","JO",
138
+ "JP","KE","KG","KH","KI","KM","KN","KP","KR","KW",
139
+ "KY","KZ","LA","LB","LC","LI","LK","LR","LS","LT",
140
+ "LU","LV","LY","MA","MC","MD","MG","MH","MK","ML",
141
+ "MM","MN","MO","MP","MQ","MR","MS","MT","MU","MV",
142
+ "MW","MX","MY","MZ","NA","NC","NE","NF","NG","NI",
143
+ "NL","NO","NP","NR","NU","NZ","OM","PA","PE","PF",
144
+ "PG","PH","PK","PL","PM","PN","PR","PS","PT","PW",
145
+ "PY","QA","RE","RO","RU","RW","SA","SB","SC","SD",
146
+ "SE","SG","SH","SI","SJ","SK","SL","SM","SN","SO",
147
+ "SR","ST","SV","SY","SZ","TC","TD","TF","TG","TH",
148
+ "TJ","TK","TM","TN","TO","TL","TR","TT","TV","TW",
149
+ "TZ","UA","UG","UM","US","UY","UZ","VA","VC","VE",
150
+ "VG","VI","VN","VU","WF","WS","YE","YT","RS","ZA",
151
+ "ZM","ME","ZW","A1","A2","O1","AX","GG","IM","JE",
152
+ "BL","MF", "BQ");
153
var $GEOIP_COUNTRY_CODES3 = array(
154
+ "","AP","EU","AND","ARE","AFG","ATG","AIA","ALB","ARM","CUW",
155
+ "AGO","ATA","ARG","ASM","AUT","AUS","ABW","AZE","BIH","BRB",
156
+ "BGD","BEL","BFA","BGR","BHR","BDI","BEN","BMU","BRN","BOL",
157
+ "BRA","BHS","BTN","BVT","BWA","BLR","BLZ","CAN","CCK","COD",
158
+ "CAF","COG","CHE","CIV","COK","CHL","CMR","CHN","COL","CRI",
159
+ "CUB","CPV","CXR","CYP","CZE","DEU","DJI","DNK","DMA","DOM",
160
+ "DZA","ECU","EST","EGY","ESH","ERI","ESP","ETH","FIN","FJI",
161
+ "FLK","FSM","FRO","FRA","SXM","GAB","GBR","GRD","GEO","GUF",
162
+ "GHA","GIB","GRL","GMB","GIN","GLP","GNQ","GRC","SGS","GTM",
163
+ "GUM","GNB","GUY","HKG","HMD","HND","HRV","HTI","HUN","IDN",
164
+ "IRL","ISR","IND","IOT","IRQ","IRN","ISL","ITA","JAM","JOR",
165
+ "JPN","KEN","KGZ","KHM","KIR","COM","KNA","PRK","KOR","KWT",
166
+ "CYM","KAZ","LAO","LBN","LCA","LIE","LKA","LBR","LSO","LTU",
167
+ "LUX","LVA","LBY","MAR","MCO","MDA","MDG","MHL","MKD","MLI",
168
+ "MMR","MNG","MAC","MNP","MTQ","MRT","MSR","MLT","MUS","MDV",
169
+ "MWI","MEX","MYS","MOZ","NAM","NCL","NER","NFK","NGA","NIC",
170
+ "NLD","NOR","NPL","NRU","NIU","NZL","OMN","PAN","PER","PYF",
171
+ "PNG","PHL","PAK","POL","SPM","PCN","PRI","PSE","PRT","PLW",
172
+ "PRY","QAT","REU","ROU","RUS","RWA","SAU","SLB","SYC","SDN",
173
+ "SWE","SGP","SHN","SVN","SJM","SVK","SLE","SMR","SEN","SOM",
174
+ "SUR","STP","SLV","SYR","SWZ","TCA","TCD","ATF","TGO","THA",
175
+ "TJK","TKL","TKM","TUN","TON","TLS","TUR","TTO","TUV","TWN",
176
+ "TZA","UKR","UGA","UMI","USA","URY","UZB","VAT","VCT","VEN",
177
+ "VGB","VIR","VNM","VUT","WLF","WSM","YEM","MYT","SRB","ZAF",
178
+ "ZMB","MNE","ZWE","A1","A2","O1","ALA","GGY","IMN","JEY",
179
+ "BLM","MAF", "BES"
180
+ );
181
var $GEOIP_COUNTRY_NAMES = array(
182
+ "","Asia/Pacific Region","Europe","Andorra","United Arab Emirates","Afghanistan","Antigua and Barbuda","Anguilla","Albania","Armenia","Curacao",
183
+ "Angola","Antarctica","Argentina","American Samoa","Austria","Australia","Aruba","Azerbaijan","Bosnia and Herzegovina","Barbados",
184
+ "Bangladesh","Belgium","Burkina Faso","Bulgaria","Bahrain","Burundi","Benin","Bermuda","Brunei Darussalam","Bolivia",
185
+ "Brazil","Bahamas","Bhutan","Bouvet Island","Botswana","Belarus","Belize","Canada","Cocos (Keeling) Islands","Congo, The Democratic Republic of the",
186
+ "Central African Republic","Congo","Switzerland","Cote D'Ivoire","Cook Islands","Chile","Cameroon","China","Colombia","Costa Rica",
187
+ "Cuba","Cape Verde","Christmas Island","Cyprus","Czech Republic","Germany","Djibouti","Denmark","Dominica","Dominican Republic",
188
+ "Algeria","Ecuador","Estonia","Egypt","Western Sahara","Eritrea","Spain","Ethiopia","Finland","Fiji",
189
+ "Falkland Islands (Malvinas)","Micronesia, Federated States of","Faroe Islands","France","Sint Maarten (Dutch part)","Gabon","United Kingdom","Grenada","Georgia","French Guiana",
190
+ "Ghana","Gibraltar","Greenland","Gambia","Guinea","Guadeloupe","Equatorial Guinea","Greece","South Georgia and the South Sandwich Islands","Guatemala",
191
+ "Guam","Guinea-Bissau","Guyana","Hong Kong","Heard Island and McDonald Islands","Honduras","Croatia","Haiti","Hungary","Indonesia",
192
+ "Ireland","Israel","India","British Indian Ocean Territory","Iraq","Iran, Islamic Republic of","Iceland","Italy","Jamaica","Jordan",
193
+ "Japan","Kenya","Kyrgyzstan","Cambodia","Kiribati","Comoros","Saint Kitts and Nevis","Korea, Democratic People's Republic of","Korea, Republic of","Kuwait",
194
+ "Cayman Islands","Kazakhstan","Lao People's Democratic Republic","Lebanon","Saint Lucia","Liechtenstein","Sri Lanka","Liberia","Lesotho","Lithuania",
195
+ "Luxembourg","Latvia","Libya","Morocco","Monaco","Moldova, Republic of","Madagascar","Marshall Islands","Macedonia","Mali",
196
+ "Myanmar","Mongolia","Macau","Northern Mariana Islands","Martinique","Mauritania","Montserrat","Malta","Mauritius","Maldives",
197
+ "Malawi","Mexico","Malaysia","Mozambique","Namibia","New Caledonia","Niger","Norfolk Island","Nigeria","Nicaragua",
198
+ "Netherlands","Norway","Nepal","Nauru","Niue","New Zealand","Oman","Panama","Peru","French Polynesia",
199
+ "Papua New Guinea","Philippines","Pakistan","Poland","Saint Pierre and Miquelon","Pitcairn Islands","Puerto Rico","Palestinian Territory","Portugal","Palau",
200
+ "Paraguay","Qatar","Reunion","Romania","Russian Federation","Rwanda","Saudi Arabia","Solomon Islands","Seychelles","Sudan",
201
+ "Sweden","Singapore","Saint Helena","Slovenia","Svalbard and Jan Mayen","Slovakia","Sierra Leone","San Marino","Senegal","Somalia","Suriname",
202
+ "Sao Tome and Principe","El Salvador","Syrian Arab Republic","Swaziland","Turks and Caicos Islands","Chad","French Southern Territories","Togo","Thailand",
203
+ "Tajikistan","Tokelau","Turkmenistan","Tunisia","Tonga","Timor-Leste","Turkey","Trinidad and Tobago","Tuvalu","Taiwan",
204
+ "Tanzania, United Republic of","Ukraine","Uganda","United States Minor Outlying Islands","United States","Uruguay","Uzbekistan","Holy See (Vatican City State)","Saint Vincent and the Grenadines","Venezuela",
205
+ "Virgin Islands, British","Virgin Islands, U.S.","Vietnam","Vanuatu","Wallis and Futuna","Samoa","Yemen","Mayotte","Serbia","South Africa",
206
+ "Zambia","Montenegro","Zimbabwe","Anonymous Proxy","Satellite Provider","Other","Aland Islands","Guernsey","Isle of Man","Jersey",
207
+ "Saint Barthelemy","Saint Martin", "Bonaire, Saint Eustatius and Saba"
208
+ );
209
+
210
+ var $GEOIP_CONTINENT_CODES = array(
211
+ "--", "AS","EU","EU","AS","AS","NA","NA","EU","AS","NA",
212
+ "AF","AN","SA","OC","EU","OC","NA","AS","EU","NA",
213
+ "AS","EU","AF","EU","AS","AF","AF","NA","AS","SA",
214
+ "SA","NA","AS","AN","AF","EU","NA","NA","AS","AF",
215
+ "AF","AF","EU","AF","OC","SA","AF","AS","SA","NA",
216
+ "NA","AF","AS","AS","EU","EU","AF","EU","NA","NA",
217
+ "AF","SA","EU","AF","AF","AF","EU","AF","EU","OC",
218
+ "SA","OC","EU","EU","NA","AF","EU","NA","AS","SA",
219
+ "AF","EU","NA","AF","AF","NA","AF","EU","AN","NA",
220
+ "OC","AF","SA","AS","AN","NA","EU","NA","EU","AS",
221
+ "EU","AS","AS","AS","AS","AS","EU","EU","NA","AS",
222
+ "AS","AF","AS","AS","OC","AF","NA","AS","AS","AS",
223
+ "NA","AS","AS","AS","NA","EU","AS","AF","AF","EU",
224
+ "EU","EU","AF","AF","EU","EU","AF","OC","EU","AF",
225
+ "AS","AS","AS","OC","NA","AF","NA","EU","AF","AS",
226
+ "AF","NA","AS","AF","AF","OC","AF","OC","AF","NA",
227
+ "EU","EU","AS","OC","OC","OC","AS","NA","SA","OC",
228
+ "OC","AS","AS","EU","NA","OC","NA","AS","EU","OC",
229
+ "SA","AS","AF","EU","EU","AF","AS","OC","AF","AF",
230
+ "EU","AS","AF","EU","EU","EU","AF","EU","AF","AF",
231
+ "SA","AF","NA","AS","AF","NA","AF","AN","AF","AS",
232
+ "AS","OC","AS","AF","OC","AS","EU","NA","OC","AS",
233
+ "AF","EU","AF","OC","NA","SA","AS","EU","NA","SA",
234
+ "NA","NA","AS","OC","OC","OC","AS","AF","EU","AF",
235
+ "AF","EU","AF","--","--","--","EU","EU","EU","EU",
236
+ "NA","NA","NA"
237
);
238
+
239
}
240
241
function cpd_geoip_load_shared_mem ($file) {
256
shmop_close ($shmid);
257
}
258
259
+ function _setup_segments($gi){
260
$gi->databaseType = GEOIP_COUNTRY_EDITION;
261
$gi->record_length = STANDARD_RECORD_LENGTH;
262
if ($gi->flags & GEOIP_SHARED_MEMORY) {
275
} else if (($gi->databaseType == GEOIP_CITY_EDITION_REV0)||
276
($gi->databaseType == GEOIP_CITY_EDITION_REV1)
277
|| ($gi->databaseType == GEOIP_ORG_EDITION)
278
+ || ($gi->databaseType == GEOIP_ORG_EDITION_V6)
279
+ || ($gi->databaseType == GEOIP_DOMAIN_EDITION)
280
+ || ($gi->databaseType == GEOIP_DOMAIN_EDITION_V6)
281
|| ($gi->databaseType == GEOIP_ISP_EDITION)
282
+ || ($gi->databaseType == GEOIP_ISP_EDITION_V6)
283
+ || ($gi->databaseType == GEOIP_USERTYPE_EDITION)
284
+ || ($gi->databaseType == GEOIP_USERTYPE_EDITION_V6)
285
+ || ($gi->databaseType == GEOIP_LOCATIONA_EDITION)
286
+ || ($gi->databaseType == GEOIP_ACCURACYRADIUS_EDITION)
287
+ || ($gi->databaseType == GEOIP_CITY_EDITION_REV0_V6)
288
+ || ($gi->databaseType == GEOIP_CITY_EDITION_REV1_V6)
289
+ || ($gi->databaseType == GEOIP_NETSPEED_EDITION_REV1)
290
+ || ($gi->databaseType == GEOIP_NETSPEED_EDITION_REV1_V6)
291
+ || ($gi->databaseType == GEOIP_ASNUM_EDITION)
292
+ || ($gi->databaseType == GEOIP_ASNUM_EDITION_V6)){
293
$gi->databaseSegments = 0;
294
$buf = @shmop_read ($gi->shmid, $offset, SEGMENT_RECORD_LENGTH);
295
for ($j = 0;$j < SEGMENT_RECORD_LENGTH;$j++){
296
$gi->databaseSegments += (ord($buf[$j]) << ($j * 8));
297
}
298
+ if (($gi->databaseType == GEOIP_ORG_EDITION)
299
+ || ($gi->databaseType == GEOIP_ORG_EDITION_V6)
300
+ || ($gi->databaseType == GEOIP_DOMAIN_EDITION)
301
+ || ($gi->databaseType == GEOIP_DOMAIN_EDITION_V6)
302
+ || ($gi->databaseType == GEOIP_ISP_EDITION)
303
+ || ($gi->databaseType == GEOIP_ISP_EDITION_V6)) {
304
$gi->record_length = ORG_RECORD_LENGTH;
305
}
306
}
310
}
311
}
312
if (($gi->databaseType == GEOIP_COUNTRY_EDITION)||
313
+ ($gi->databaseType == GEOIP_COUNTRY_EDITION_V6)||
314
($gi->databaseType == GEOIP_PROXY_EDITION)||
315
($gi->databaseType == GEOIP_NETSPEED_EDITION)){
316
$gi->databaseSegments = GEOIP_COUNTRY_BEGIN;
327
}
328
else if ($gi->databaseType == GEOIP_REGION_EDITION_REV1){
329
$gi->databaseSegments = GEOIP_STATE_BEGIN_REV1;
330
+ } else if (($gi->databaseType == GEOIP_CITY_EDITION_REV0)
331
+ || ($gi->databaseType == GEOIP_CITY_EDITION_REV1)
332
+ || ($gi->databaseType == GEOIP_CITY_EDITION_REV0_V6)
333
+ || ($gi->databaseType == GEOIP_CITY_EDITION_REV1_V6)
334
+ || ($gi->databaseType == GEOIP_ORG_EDITION)
335
+ || ($gi->databaseType == GEOIP_DOMAIN_EDITION)
336
+ || ($gi->databaseType == GEOIP_ISP_EDITION)
337
+ || ($gi->databaseType == GEOIP_ORG_EDITION_V6)
338
+ || ($gi->databaseType == GEOIP_DOMAIN_EDITION_V6)
339
+ || ($gi->databaseType == GEOIP_ISP_EDITION_V6)
340
+ || ($gi->databaseType == GEOIP_LOCATIONA_EDITION)
341
+ || ($gi->databaseType == GEOIP_ACCURACYRADIUS_EDITION)
342
+ || ($gi->databaseType == GEOIP_CITY_EDITION_REV0_V6)
343
+ || ($gi->databaseType == GEOIP_CITY_EDITION_REV1_V6)
344
+ || ($gi->databaseType == GEOIP_NETSPEED_EDITION_REV1)
345
+ || ($gi->databaseType == GEOIP_NETSPEED_EDITION_REV1_V6)
346
+ || ($gi->databaseType == GEOIP_USERTYPE_EDITION)
347
+ || ($gi->databaseType == GEOIP_USERTYPE_EDITION_V6)
348
+ || ($gi->databaseType == GEOIP_ASNUM_EDITION)
349
+ || ($gi->databaseType == GEOIP_ASNUM_EDITION_V6)){
350
$gi->databaseSegments = 0;
351
$buf = fread($gi->filehandle,SEGMENT_RECORD_LENGTH);
352
for ($j = 0;$j < SEGMENT_RECORD_LENGTH;$j++){
353
$gi->databaseSegments += (ord($buf[$j]) << ($j * 8));
354
}
355
+ if ( ( $gi->databaseType == GEOIP_ORG_EDITION )
356
+ || ( $gi->databaseType == GEOIP_DOMAIN_EDITION )
357
+ || ( $gi->databaseType == GEOIP_ISP_EDITION )
358
+ || ( $gi->databaseType == GEOIP_ORG_EDITION_V6 )
359
+ || ( $gi->databaseType == GEOIP_DOMAIN_EDITION_V6 )
360
+ || ( $gi->databaseType == GEOIP_ISP_EDITION_V6 )) {
361
$gi->record_length = ORG_RECORD_LENGTH;
362
}
363
}
367
}
368
}
369
if (($gi->databaseType == GEOIP_COUNTRY_EDITION)||
370
+ ($gi->databaseType == GEOIP_COUNTRY_EDITION_V6)||
371
($gi->databaseType == GEOIP_PROXY_EDITION)||
372
($gi->databaseType == GEOIP_NETSPEED_EDITION)){
373
$gi->databaseSegments = GEOIP_COUNTRY_BEGIN;
378
}
379
380
function cpd_geoip_open($filename, $flags) {
381
+ $gi = new GeoIPCpd;
382
$gi->flags = $flags;
383
if ($gi->flags & GEOIP_SHARED_MEMORY) {
384
$gi->shmid = @shmop_open (GEOIP_SHM_KEY, "a", 0, 0);
390
}
391
}
392
393
+ $gi = _setup_segments($gi);
394
return $gi;
395
}
396
402
return fclose($gi->filehandle);
403
}
404
405
+ function cpd_geoip_country_id_by_name_v6($gi, $name) {
406
+ $rec = dns_get_record($name, DNS_AAAA);
407
+ if ( !$rec ) {
408
+ return false;
409
+ }
410
+ $addr = $rec[0]["ipv6"];
411
+ if (!$addr || $addr == $name) {
412
+ return false;
413
+ }
414
+ return cpd_geoip_country_id_by_addr_v6($gi, $addr);
415
+ }
416
+
417
function cpd_geoip_country_id_by_name($gi, $name) {
418
$addr = gethostbyname($name);
419
if (!$addr || $addr == $name) {
422
return cpd_geoip_country_id_by_addr($gi, $addr);
423
}
424
425
+ function cpd_geoip_country_code_by_name_v6($gi, $name) {
426
+ $country_id = cpd_geoip_country_id_by_name_v6($gi,$name);
427
+ if ($country_id !== false) {
428
+ return $gi->GEOIP_COUNTRY_CODES[$country_id];
429
+ }
430
+ return false;
431
+ }
432
+
433
function cpd_geoip_country_code_by_name($gi, $name) {
434
$country_id = cpd_geoip_country_id_by_name($gi,$name);
435
if ($country_id !== false) {
438
return false;
439
}
440
441
+ function cpd_geoip_country_name_by_name_v6($gi, $name) {
442
+ $country_id = cpd_geoip_country_id_by_name_v6($gi,$name);
443
+ if ($country_id !== false) {
444
+ return $gi->GEOIP_COUNTRY_NAMES[$country_id];
445
+ }
446
+ return false;
447
+ }
448
+
449
function cpd_geoip_country_name_by_name($gi, $name) {
450
$country_id = cpd_geoip_country_id_by_name($gi,$name);
451
if ($country_id !== false) {
454
return false;
455
}
456
457
+ function cpd_geoip_country_id_by_addr_v6($gi, $addr) {
458
+ $ipnum = inet_pton($addr);
459
+ return _cpd_geoip_seek_country_v6($gi, $ipnum) - GEOIP_COUNTRY_BEGIN;
460
+ }
461
+
462
function cpd_geoip_country_id_by_addr($gi, $addr) {
463
$ipnum = ip2long($addr);
464
+ return _cpd_geoip_seek_country($gi, $ipnum) - GEOIP_COUNTRY_BEGIN;
465
+ }
466
+
467
+ function cpd_geoip_country_code_by_addr_v6($gi, $addr) {
468
+ $country_id = cpd_geoip_country_id_by_addr_v6($gi,$addr);
469
+ if ($country_id !== false) {
470
+ return $gi->GEOIP_COUNTRY_CODES[$country_id];
471
+ }
472
+ return false;
473
}
474
475
function cpd_geoip_country_code_by_addr($gi, $addr) {
487
return false;
488
}
489
490
+ function cpd_geoip_country_name_by_addr_v6($gi, $addr) {
491
+ $country_id = cpd_geoip_country_id_by_addr_v6($gi,$addr);
492
+ if ($country_id !== false) {
493
+ return $gi->GEOIP_COUNTRY_NAMES[$country_id];
494
+ }
495
+ return false;
496
+ }
497
+
498
function cpd_geoip_country_name_by_addr($gi, $addr) {
499
if ($gi->databaseType == GEOIP_CITY_EDITION_REV1) {
500
$record = cpd_geoip_record_by_addr($gi,$addr);
508
return false;
509
}
510
511
+ function _cpd_geoip_seek_country_v6($gi, $ipnum) {
512
+
513
+ # arrays from unpack start with offset 1
514
+ # yet another php mystery. array_merge work around
515
+ # this broken behaviour
516
+ $v6vec = array_merge(unpack( "C16", $ipnum));
517
+
518
+ $offset = 0;
519
+ for ($depth = 127; $depth >= 0; --$depth) {
520
+ if ($gi->flags & GEOIP_MEMORY_CACHE) {
521
+ // workaround php's broken substr, strpos, etc handling with
522
+ // mbstring.func_overload and mbstring.internal_encoding
523
+ $enc = mb_internal_encoding();
524
+ mb_internal_encoding('ISO-8859-1');
525
+
526
+ $buf = substr($gi->memory_buffer,
527
+ 2 * $gi->record_length * $offset,
528
+ 2 * $gi->record_length);
529
+
530
+ mb_internal_encoding($enc);
531
+ } elseif ($gi->flags & GEOIP_SHARED_MEMORY) {
532
+ $buf = @shmop_read ($gi->shmid,
533
+ 2 * $gi->record_length * $offset,
534
+ 2 * $gi->record_length );
535
+ } else {
536
+ fseek($gi->filehandle, 2 * $gi->record_length * $offset, SEEK_SET) == 0
537
+ or die("fseek failed");
538
+ $buf = fread($gi->filehandle, 2 * $gi->record_length);
539
+ }
540
+ $x = array(0,0);
541
+ for ($i = 0; $i < 2; ++$i) {
542
+ for ($j = 0; $j < $gi->record_length; ++$j) {
543
+ $x[$i] += ord($buf[$gi->record_length * $i + $j]) << ($j * 8);
544
+ }
545
+ }
546
+
547
+ $bnum = 127 - $depth;
548
+ $idx = $bnum >> 3;
549
+ $b_mask = 1 << ( $bnum & 7 ^ 7 );
550
+ if (($v6vec[$idx] & $b_mask) > 0) {
551
+ if ($x[1] >= $gi->databaseSegments) {
552
+ return $x[1];
553
+ }
554
+ $offset = $x[1];
555
+ } else {
556
+ if ($x[0] >= $gi->databaseSegments) {
557
+ return $x[0];
558
+ }
559
+ $offset = $x[0];
560
+ }
561
+ }
562
+ trigger_error("error traversing database - perhaps it is corrupt?", E_USER_ERROR);
563
+ return false;
564
+ }
565
+
566
+ function _cpd_geoip_seek_country($gi, $ipnum) {
567
$offset = 0;
568
for ($depth = 31; $depth >= 0; --$depth) {
569
if ($gi->flags & GEOIP_MEMORY_CACHE) {
570
+ // workaround php's broken substr, strpos, etc handling with
571
+ // mbstring.func_overload and mbstring.internal_encoding
572
+ $enc = mb_internal_encoding();
573
+ mb_internal_encoding('ISO-8859-1');
574
+
575
$buf = substr($gi->memory_buffer,
576
2 * $gi->record_length * $offset,
577
2 * $gi->record_length);
578
+
579
+ mb_internal_encoding($enc);
580
+ } elseif ($gi->flags & GEOIP_SHARED_MEMORY) {
581
$buf = @shmop_read ($gi->shmid,
582
2 * $gi->record_length * $offset,
583
2 * $gi->record_length );
608
return false;
609
}
610
611
+ function _cpd_common_get_org($gi, $seek_org){
612
+ $record_pointer = $seek_org + (2 * $gi->record_length - 1) * $gi->databaseSegments;
613
+ if ($gi->flags & GEOIP_SHARED_MEMORY) {
614
+ $org_buf = @shmop_read ($gi->shmid, $record_pointer, MAX_ORG_RECORD_LENGTH);
615
+ } else {
616
+ fseek($gi->filehandle, $record_pointer, SEEK_SET);
617
+ $org_buf = fread($gi->filehandle,MAX_ORG_RECORD_LENGTH);
618
+ }
619
+ // workaround php's broken substr, strpos, etc handling with
620
+ // mbstring.func_overload and mbstring.internal_encoding
621
+ $enc = mb_internal_encoding();
622
+ mb_internal_encoding('ISO-8859-1');
623
+ $org_buf = substr($org_buf, 0, strpos($org_buf, "\0"));
624
+ mb_internal_encoding($enc);
625
+ return $org_buf;
626
+ }
627
+
628
+ function _cpd_get_org_v6($gi,$ipnum){
629
+ $seek_org = _cpd_geoip_seek_country_v6($gi,$ipnum);
630
+ if ($seek_org == $gi->databaseSegments) {
631
+ return NULL;
632
+ }
633
+ return _cpd_common_get_org($gi, $seek_org);
634
+ }
635
+
636
+ function _cpd_get_org($gi,$ipnum){
637
+ $seek_org = _cpd_geoip_seek_country($gi,$ipnum);
638
+ if ($seek_org == $gi->databaseSegments) {
639
+ return NULL;
640
+ }
641
+ return _cpd_common_get_org($gi, $seek_org);
642
+ }
643
+
644
+
645
+
646
+ function cpd_geoip_name_by_addr_v6 ($gi,$addr) {
647
+ if ($addr == NULL) {
648
+ return 0;
649
+ }
650
+ $ipnum = inet_pton($addr);
651
+ return _cpd_get_org_v6($gi, $ipnum);
652
+ }
653
+
654
+ function cpd_geoip_name_by_addr ($gi,$addr) {
655
+ if ($addr == NULL) {
656
+ return 0;
657
+ }
658
+ $ipnum = ip2long($addr);
659
+ return _cpd_get_org($gi, $ipnum);
660
+ }
661
+
662
+ function cpd_geoip_org_by_addr ($gi,$addr) {
663
+ return cpd_geoip_name_by_addr($gi, $addr);
664
+ }
665
+
666
+ function _cpd_get_region($gi,$ipnum){
667