settings = get_settings('doc_referers');
$this->wpdb_tables();
if($this->settings['table_version'] != $this->table_version)
{
$this->make_tables();
$this->added_tables = true;
}
}
function admin_footer()
{
update_option('doc_referers', $this->settings);
}
function admin_menu()
{
$pfile = basename(dirname(__FILE__)) . '/' . basename(__FILE__);
add_submenu_page('edit.php', 'Referers', 'Referers', 0, $pfile, array(&$this, 'plugin_content'));
}
function wpdb_tables() {
global $wpdb, $table_prefix;
$wpdb->doc_referers = "{$table_prefix}referer_visitLog";
$wpdb->doc_blacklist = "{$table_prefix}referer_blacklist";
}
function make_tables() {
global $wpdb, $table_prefix;
if(!include_once(ABSPATH . 'wp-admin/upgrade-functions.php')) {
die(_e('There is was error adding the required tables to the database. Please refer to the documentation regarding this issue.', 'DOC_Referers'));
}
$qry = "CREATE TABLE " . $wpdb->doc_blacklist . " (
ID int(11) NOT NULL auto_increment,
blogID varchar(32) NOT NULL default '',
URL varchar(250) NOT NULL default '',
t timestamp(14) NOT NULL,
PRIMARY KEY (ID),
KEY blogID (blogID,URL),
KEY URL (URL)
);";
$wpdb->query( $qry );
$qry = "CREATE TABLE " . $wpdb->doc_referers . " (
blogID char( 32 ) default NULL ,
visitID int( 11 ) NOT NULL AUTO_INCREMENT ,
visitTime timestamp( 14 ) NOT NULL ,
visitURL char( 250 ) default NULL ,
referingURL char( 250 ) default NULL ,
baseDomain char( 250 ) default NULL ,
refpost int( 11 ) NOT NULL default '0',
visitTimes int( 10 ) NOT NULL default '0',
dayofmonth smallint( 2 ) NOT NULL default '0',
PRIMARY KEY ( visitID ) ,
KEY blogID ( blogID ) ,
KEY refpost ( refpost ) ,
KEY dayofmonth ( dayofmonth )
);
";
$wpdb->query( $qry );
$this->settings['table_version'] = $this->table_version;
update_option('doc_referers', $this->settings);
}
function makeHiddenVals( $day, $order, $num, $more, $ignoreDIRECT, $visitID, $internal )
{
$fields = array( "day", "order", "num", "more", "ignoreDIRECT", "visitID", "internal" );
reset( $fields );
while( list( $key, $field ) = each( $fields ) )
{
if( $field == 'action' )
{
$sep = '?';
}
else
{
$sep = '&';
}
if( $_GET[ $field ] != '' )
$vals .= "\n";
}
return $vals;
}
function makeURL( $var, $val )
{
$fields = array( "action", "day", "order", "num", "more", "ignoreDIRECT", "visitID", "internal" );
reset( $fields );
while( list( $key, $field ) = each( $fields ) )
{
$sep = '&';
if( $field != $var )
{
if( $_GET[ $field ] != '' )
$url .= $sep.$field."=".$_GET[ $field ];
}
else
{
$url .= $sep.$var."=".$val;
}
}
return $url;
}
function plugin_content()
{
global $wpdb;
$action = $_GET[ 'action' ];
$day = $_GET[ 'day' ];
$del = $_GET[ 'del' ];
$num = $_GET[ 'num' ];
$more = $_GET[ 'more' ];
$order = $_GET[ 'order' ];
$ignoreDIRECT = $_GET[ 'ignoreDIRECT' ];
$internal = $_GET[ 'internal' ];
if( $ignoreDIRECT == '' ) {
$ignoreDIRECT = get_option( 'ignoreDIRECT' );
} else {
if( get_option( 'ignoreDIRECT' ) == false ) {
add_option( 'ignoreDIRECT', $ignoreDIRECT );
} else {
update_option( 'ignoreDIRECT', $ignoreDIRECT );
}
}
if( $internal == '' ) {
$internal = get_option( 'doc_referers_internal' );
} else {
if( get_option( 'doc_referers_internal' ) == false ) {
add_option( 'doc_referers_internal', $internal );
} else {
update_option( 'doc_referers_internal', $internal );
}
}
if( $action == '' )
{
$action = 'listday';
$day = date( 'j' );
}
if( $day == '' )
$day = date( 'j' );
print '
';
if( $action == 'Delete' )
{
if( is_array( $del ) )
{
reset( $del );
while( list( $key, $val ) = each( $del ) )
{
$query = "DELETE FROM " . $wpdb->doc_referers . "
WHERE visitID = '".$val."'";
$result = $wpdb->query($query);
}
}
$action = "listday";
}
elseif( $action == 'deletedirect' )
{
$query = "DELETE FROM " . $wpdb->doc_referers . " WHERE dayofmonth='".$day."' AND referingURL = 'DIRECT'";
$result = $wpdb->query($query);
printf ("Records deleted: %d\n", $wpdb->rows_affected);
$action = "listday";
}
elseif( $action == 'Add To Blacklist' )
{
if( is_array( $del ) )
{
reset( $del );
while( list( $key, $val ) = each( $del ) )
{
$query = "SELECT referingURL FROM " . $wpdb->doc_referers . " WHERE visitID = '".$val."'";
$result=$wpdb->get_var( $query );
if( $result )
{
$query = "INSERT INTO " . $wpdb->doc_blacklist . " VALUES( NULL, 0, '".$result."', NOW() )";
$result = $wpdb->query($query);
}
}
}
$action = "listday";
}
elseif( $action == 'deleteblacklist' )
{
if( is_array( $del ) )
{
reset( $del );
while( list( $key, $val ) = each( $del ) )
{
$query = "DELETE FROM " . $wpdb->doc_blacklist . " WHERE ID='".$val."'";
$result = $wpdb->query($query);
}
}
$action = "blacklist";
}
switch( $action )
{
case "blacklist":
$query = "SELECT * FROM " . $wpdb->doc_blacklist;
$result = $wpdb->get_results($query, ARRAY_A );
if( $result )
{
print "
Referer Blacklist
";
print "
";
}
else
{
print "No URLs in blacklist yet!";
}
break;
case "listday":
$query = "select visitTimes,referingURL,date_format( visitTime, '%k:%i' ) as visitTime2, visitURL, visitID from " . $wpdb->doc_referers . " where dayofmonth='".$day."'";
if( $internal == 'yes' )
$query .= " and referingURL NOT LIKE '".get_settings( "siteurl" )."%'";
if( $ignoreDIRECT == 'yes' )
$query .= " and referingURL != 'DIRECT'";
if( $order == '' || $order == 'time' )
{
$query .= " order by visitTime desc";
}
elseif( $order == 'hits' )
{
$query .= " order by visitTimes desc";
}
elseif( $order == 'url' )
{
$query .= " order by visitURL desc";
}
if( $num == '' )
{
$num = 0;
}
if( $more == '' || $more == '0' )
$more = '30';
$query .= " limit $num,$more";
$result = $wpdb->get_results($query, ARRAY_A );
$rows = $wpdb->num_rows;
print "
Referers
";
if( $result ) {
// javascript from http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_10105441.html and
// http://members.aol.com/grassblad/html/chkAllBut.html
print "";
$c = $num+1;
$nav = "
";
$nav .= "
Month View | ";
$nav .= "
makeURL( "num", 0 )."'>Top";
if( $ignoreDIRECT == 'yes' )
{
$nav .= " |
makeURL( "ignoreDIRECT", 'no' )."'>Display DIRECT requests";
}
else
{
$nav .= " |
makeURL( "ignoreDIRECT", 'yes' )."'>Hide DIRECT requests";
}
if( $internal == 'yes' )
{
$nav .= " |
makeURL( "internal", 'no' )."'>Display internal requests";
}
else
{
$nav .= " |
makeURL( "internal", 'yes' )."'>Hide internal requests";
}
if( $num >= 10 )
{
if( $num > $more )
{
$nav .= " |
makeURL( "num", ( $num - $more ) )."'>Previous $more";
}
}
else
{
$nav .= " | Previous";
}
if( $rows >= $more )
{
$nav .= " |
makeURL( "num", ($num + $more) )."'>Next $more";
}
else
{
$nav .= " | Next";
}
$nav .= " |
makeURL( "more", ($more + 10) )."'>More Hits";
$nav .= " |
makeURL( "more", ($more - 10) )."'>Less Hits";
$nav .= "
";
print $nav;
$today = date( 'd' );
if( $day > $today )
{
$month = date( 'F', mktime (0,0,0,date("m")-1,date("d"), date("Y")) );
}
else
{
$month = date( 'F' );
}
print "
";
print "
";
} else {
print "
No Referers found today!
";
print "
- Month View displays the last month of stats.
";
if( $ignoreDIRECT == 'yes' )
print "- Display DIRECT hits - some browsers don't report what page they come from. You're hiding this information right now.
";
if( $internal == 'yes' )
print "- Display internal hits - it's not always very interesting where people wander around your blog. You're hiding this information right now.
";
print "
";
}
break;
default:
$query = "select sum( visitTimes ) as c, dayofmonth from " . $wpdb->doc_referers . " ";
$query .= "group by " . $wpdb->doc_referers . ".dayofmonth";
$result = $wpdb->get_results($query, ARRAY_A );
if( $result )
{
$c = 0;
$col = 'ccc';
print "
Referers
";
print "
";
print "";
print "Day | Hits | ";
while( list( $key, $row1 ) = each( $result ) )
{
if( $col == 'f5f5f5' )
{
$col = 'ffffff';
}
else
{
$col = 'f5f5f5';
}
print "
---|
".$row1[ 'dayofmonth']." | ".$row1[ 'c']." | ";
$c++;
if( $c == '15' )
{
print " ";
print " | ";
print "";
print "Day | Hits | ";
}
}
print "
---|
";
print " |
";
print "
View Blacklist";
}
else
{
print "There are no referers for your site! Wait until Google indexes you!";
}
}
print "
";
}
function template_redirect() {
global $wpdb;
$wpdb->hide_errors();
// delete tomorrow's referers today
$tomorrow = date( "j", mktime (0,0,0,date("m") ,date("d")+1,date("Y")) );
$sec = date( "s" );
$hour = date( "G" );
if( $sec == 30 && $hour < 2 )
{
$sql = "delete from " . $wpdb->doc_referers . " WHERE dayofmonth = '$tomorrow'"; // delete referers from a (month + 1 day) ago.
$wpdb->query($sql);
}
$ref = $_SERVER["HTTP_REFERER"];
$currentURL = $_SERVER[ 'REQUEST_URI' ];
$fullCurrentURL = "http://" . $_SERVER[ 'HTTP_HOST' ] . $_SERVER[ 'REQUEST_URI' ];
if( $ref == '' )
{
$ref = "DIRECT";
}
$found = false;
if( $currentURL[ strlen( $currentURL ) -1 ] == '/' )
{
$found = true;
}
else
{
$count_files = array( "wp-admin" );
reset( $count_files );
while( list( $key, $val ) = each( $count_files ) )
{
$pos = strpos( $currentURL, $val );
if( $pos == true )
{
$found = true;
}
}
if( $found == true )
{
// Don't bother going further - no need to record request!
return;
}
}
$ref = $wpdb->escape($ref);
if( $ref ) {
$realReferer = true;
$ignorePages = Array( 'lastupdated.php', 'b2rdf.php', 'b2rss2.php', 'b2bookmarklet.php', 'b2referers.php', 'b2commentspopup.php' );
foreach ($ignorePages as $ignoresite){
if (stristr($currentURL, $ignoresite)){
$realReferer = false;
}
}
$ignore = Array(
'http://www.myelin.co.nz/ecosystem/bot.php',
'http://radio.xmlstoragesystem.com/rcsPublic/',
'http://blogdex.media.mit.edu//',
'http://subhonker6.userland.com/rcsPublic/',
'mastadonte.com',
'http://blo.gs/ping.php'
);
foreach ($ignore as $ignoresite){
if (stristr($ref, $ignoresite)){
$realReferer = false;
}
}
$checkRef = true;
// Do we need to check the referer? If it's from a known site we can save some cycles.
$checkReflist = array( "direct", "http://www.technorati.com", "http://www.google", "http://www.yahoo", "http://www.linux.ie", "http://blogs.linux.ie", "http://blo.gs" );
reset( $checkReflist );
while( list( $key, $val ) = each( $checkReflist ) )
{
$p = strpos( strtolower( $url ), $val );
if( $p !== false )
{
$checkRef = false;
}
}
$doubleCheckReferers = 0; // must make this an option
if( $realReferer && $checkRef && $ref != 'DIRECT' && $doubleCheckReferers)
{
//this is so that the page up until the call to
//logReferer will get shown before it tries to check
//back against the refering URL.
flush();
$goodReferer = 0;
$fp = @fopen ($ref, "r");
if ($fp){
socket_set_timeout($fp, 5);
$c = 0;
while (!feof ($fp) || $c > 5) {
$page .= trim(fgets($fp, 4096));
$c++;
}
fclose( $fp );
if (strstr($page,$fullCurrentURL)){
$goodReferer = 1;
}
}
if(!$goodReferer){
$realReferer = false;
}
}
if( $realReferer == true && $ref != 'DIRECT' )
{
$query = "SELECT ID FROM " . $wpdb->doc_blacklist . " WHERE URL like '%$ref%'";
$result = $wpdb->get_var( $query );
if( $result )
{
$ref = "DIRECT";
}
}
$ua = getenv( 'HTTP_USER_AGENT' );
$useragents = array( "http://www.syndic8.com", "http://dir.com/pompos.html", "NaverBot-1.0", "http://help.yahoo.com/help/us/ysearch/slurp", "http://www.google.com/bot.html", "http://www.blogdigger.com/", "http://search.msn.com/msnbot.htm", "Feedster, LLC.", "http://www.breakingblogs.com/timbo_bot.html", "fastbuzz.com", "http://www.pubsub.com/", "http://www.bloglines.com", "http://www.drupal.org/", "Ask Jeeves/Teoma", "ia_archiver", "http://minutillo.com/steve/feedonfeeds/", "larbin_2", "lmspider", "kinjabot", "lickBot 2.0", "Downes/Referrers", "daypopbot", "www.globalspec.com" );
reset( $useragents );
while( list( $key, $val ) = each( $useragents ) )
{
if( strpos( $ua, $val ) !== false )
{
$realReferer = false;
}
}
if( $realReferer )
{
if( $ref == 'DIRECT' )
{
$anchor = $ref;
}
else
{
$anchor = preg_replace("/http:\/\//i", "", $ref);
$anchor = preg_replace("/^www\./i", "", $anchor);
$anchor = preg_replace("/\/.*/i", "", $anchor);
}
$today = date( "d" );
if( strstr( $ref, 'bloglines.com' ) )
$ref = "http://www.bloglines.com/";
$sql = "UPDATE " . $wpdb->doc_referers . "
SET visitTimes = visitTimes + 1
WHERE dayofmonth = '$today'
AND referingURL = '$ref'
AND visitURL = '$currentURL'";
$result = $wpdb->query( $sql );
if( $result == false )
{
$sql ="INSERT INTO " . $wpdb->doc_referers . " (referingURL,visitURL,refpost, visitTimes, dayofmonth)
VALUES ('$ref','$currentURL','$p','1', '$today')";
$result = $wpdb->query( $sql );
}
}
}
$wpdb->show_errors();
}
}
$doc_referer = new DOC_Referers();
?>