summaryrefslogtreecommitdiffstats
path: root/wp-inst/wp-counter.php
blob: e0121d0dbdf28af9ddbce3e45ef8bc28defba2d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php

/* $Id: wp-counter.php,v 1.5 2005/03/11 14:46:30 donncha Exp $ */

require_once('wp-blog-header.php');

function doCheckRef( $url )
{
    $check = true;
    // Do we need to check the referer? If it's from a known site we can save some cycles.
    $checklist = array( "direct", "http://www.technorati.com", "http://www.google", "http://www.yahoo", "http://www.linux.ie", "http://blogs.linux.ie", "http://blo.gs" );
    reset( $checklist );
    while( list( $key, $val ) = each( $checklist ) ) 
    { 
        $p = strpos( strtolower( $url ), $val );
        if( $p !== false )
        {
            $check = false;
        }
    }
    return $check;
}

function getreferer()
{
    global $p, $m, $cat, $site, $wpdb, $wpblog, $referer, $loc;

    $ref = $referer;

    // 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 referer_visitLog WHERE dayofmonth = '$tomorrow'"; // delete referers from a (month + 1 day) ago.
        $wpdb->query($sql);
    }

    $ua = getenv( 'HTTP_USER_AGENT' );
    $currentURL = str_replace( "http://".$_SERVER[ 'SERVER_NAME' ], '', $loc );
    $fullCurrentURL = $loc;
    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;
        }
    }

    if ($ref || $ref = strip_tags($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;
            }
        }

        $doubleCheckReferers = 0;

        $checkRef = doCheckRef( $ref );

        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 )
	{
	    $query = "SELECT ID FROM referer_blacklist WHERE URL like '%$ref%'";
	    $result = $wpdb->get_var( $query );
	    if( $result )
	    {
		$ref = "DIRECT";
	    }
	}

        $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" );

            $sql = "update referer_visitLog
                set    visitTimes = visitTimes + 1 
                where  blogID = '$wpblog' 
                AND    dayofmonth = '$today'
                AND    referingURL = '$ref'
                AND    visitURL   = '$currentURL'";
            $result = $wpdb->query( $sql );
            if( $result == false )
            {
                $sql ="insert delayed into referer_visitLog (blogID,referingURL,visitURL,refpost, visitTimes, dayofmonth)
                       values ('$wpblog','$ref','$currentURL','$p','1', '$today')";
                $result = $wpdb->query( $sql );
            }
        }
    }
}

if( $_GET[ 'page' ] )
{
    if( $referer == '' )
        if( $HTTP_GET_VARS[ 'referer' ] != '' )
        {
            $referer = $HTTP_GET_VARS[ 'referer' ];
        }
    getreferer();
    header( "Content-type: IMAGE/PNG" );
    readfile( get_settings( 'siteurl' ) . "/wp-images/wpminilogo.png" );
}
else
{
    ?>
    <!--
    if (document["referrer"] != null) 
    {
        ref = document.referrer;
    }
    else
    {
        ref = 'DIRECT';
    }
    counterDate = new Date();
    seconds = counterDate.getSeconds();
    document.write("<a  href='<?php print get_settings( 'siteurl' ) ?>/'><img width=1 height=1 src='<?php print get_settings( 'siteurl' ) ?>/wp-counter.php?page=img&loc="+document.location+"&referer="+escape(ref)+"&t="+seconds+"' border='0' title='Stats'></a>");
    // -->
    <?php
}
?>