# Blosxom Plugin: refer # Author(s): Steve Wills # Version: 0.1 # Documentation: See the bottom of this file or type: perldoc theme package refer; # --- Configurable variables ----- # edit these (must be inside 'quotes'): my $sqluser = 'user'; # db username my $sqlpasswd = 'pass'; # db password my $sqlhost = 'localhost'; # MySQL server (frequently 'localhost') my $sqldb = 'refer'; # database name my $sqltable = 'refer'; # any name you like - Refer will create the table # Fill these in to halt the recording of unwanted referrals # (e.g., an overly frequent google search, or a robot that # inserts a referrer for every page it visits) by matching a # distinct phrase. my @exclude = qw ! viagra example.com !; # Filenames or directories that match those in the following # list won't be recorded my @pexclude = qw ! rss rdf css !; # Host names or IPs that match those in the following list won't be # recorded my @iexclude = qw ! annoying.host.example.com 192.168.0.1 127.0.0.1 !; # Your web domain, without 'www.', e.g., 'textism.com' my $mydomain = 'example.com'; # --- End Configurable variables ----- use Socket; use DBI; use DBD::mysql; sub start { $mydomain = lc($mydomain); my $uri = dbPrep('REQUEST_URI'); my $ip = ipPrep('REMOTE_ADDR'); my $ref = dbPrep('HTTP_REFERER'); $ref =~ s/^http:\/\/[^\.]*\.?$mydomain//; $ref =~ s/^www\.//; foreach my $r (@exclude) { if ( $ref =~ m/$r/i) { $ref = ''; } } foreach my $u (@pexclude) { if ( $uri =~ m/$u/i) { $uri = ''; } } foreach my $i (@iexclude) { if ( $ip =~ m/$i/i) { $ip = ''; } } $ref =~ s/.*$mydomain.*//; $ref =~ s/\/(index\.html?|index\.php)?$//; if ( (defined $ref) and ($ref ne '') and ($ref ne 'null') and (defined $uri) and ($uri ne '') and ($uri ne 'null') and (defined $ip) and ($ip ne '') and ($ip ne 'null') ) { $ref =~ s,http://,,; # setup database connection my $dbh = DBI->connect_cached( "dbi:mysql:$sqldb@$sqlhost", $sqluser, $sqlpasswd, { PrintError => 1, AutoCommit => 1, } ) or die $DBI::errstr; my $query = qq|insert into $sqltable set time=NOW(), page=?, host=?, refer=?|; my $sth = $dbh->prepare($query); $sth->execute($uri, $ip, $ref); } } sub dbPrep() { my $foo = shift; my $foo = $ENV{$foo}; $foo =~ s/^\s+//; $foo =~ s/\s+$//; $foo = qq|$foo|; $foo = lc($foo); } sub ipPrep() { my $foo = shift; $foo = $ENV{$foo}; $foo = gethostbyaddr(inet_aton($foo),AF_INET); $foo =~ s/^\s+//; $foo =~ s/\s+$//; $foo = qq|$foo|; $foo = lc($foo); } 1; __END__ =head1 NAME Blosxom Plug-in: refer =head1 SYNOPSIS This plugin is a port of the refer.php code from Refer (http://www.textism.com/tools/refer/) =head1 VERSION 0.1 =head1 AUTHOR Steve Wills =head1 SEE ALSO http://www.textism.com/tools/refer/ =head1 BUGS Address bug reports and comments to the author. =head1 LICENSE Copyright 2003, Steve Wills (This license is the same as Blosxom's) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.