Gossamer Forum
Home : Products : Gossamer Links : Version 1.x :

Report Bad Link

Quote Reply
Report Bad Link
This is a report bad link script -- to allow users to report links that are bad. I had not seen a need for this before, but the link checker is not quite ready for prime time <G>.... many links are reported dead that aren't... On a large site you might not have time to verify everything. The file should be pretty complete, just cut it apart per the directions:

Code:
#!/usr/local/bin/perl
# ==============================================================
# Report a bad link
# ==============================================================
# This is a modification/addition to the LinkSQL program. It's
# based on, and uses routines that are come with LinkSQL. This
# program will NOT run without LinkSQL. It's not meant to.
# Portions are copyrighted Gossamer-Threads, others are by me.
# This mod is freely distributable as long as the following
# Copyright notice stays attached:
#
# #################################################
# Portions Copyright 1999 PUGDOG Enterprises, Inc.
# Portions Copyright 1999 Gossamer-Threads
# Offered as Use-at-your-own-risk code.
# No warrantees are offered or implied.
# #################################################
#
# You need to first create a table
# with the structure below for 'Bad_Links'
#
# CREATE TABLE Bad_Links (
# LinkID int(11) DEFAULT '0' NOT NULL,
# URL varchar(150) NOT NULL,
# Title varchar(150) NOT NULL,
# Status varchar(255) DEFAULT 'Pending',
# TimeCheck timestamp(14),
# IP varchar(15),
# KEY LinkID (LinkID)
# );
####################################################################
# Then, edit the lines below with your databases name, user and PW
# Upload to the defs directory as 'Bad_Links.def'. The easiest thing
# is to copy the header from one of the other DEF files, making sure
# the $package and $db_table lines say "Bad_Links"
####################################################################
# Auto Generated Config File.
# Created on: Wed Oct 20 03:15:23 1999
#
# package Links: BSQL::Bad_Links;
#
# $db_name = 'links';
# $db_host = 'localhost';
# $db_driver = 'mysql';
# $db_user = '__user__';
# $db_pass = '__PW__';
# $db_key = 'LinkID';
# $db_table = 'Bad_Links';
# $db_track = '0';
#
# %db_def = (
# LinkID => ['1', 'INT', '10', '20', '1', '0', '^\d*\.?\d*$'],
# URL => ['2', 'CHAR', '40', '150', '1', '', ''],
# Title => ['3', 'CHAR', '40', '150', '1', '', ''],
# Status => ['4', 'CHAR', '40x3', '5000', '1', '', ''],
# TimeCheck => ['5', 'DATETIME', '20', '20', '0', '', '^\d{4}\-\d{2}\-\d{2}\s*\d{2}\:\d{2}\:\d{2}$']
# );
#
# %db_select_fields = (
#
# );
# %db_checkbox_fields = (
#
# );
# %db_radio_fields = (
#
# );
#
# 1; # end Bad_Links.def
#
########################################################################
# Then, in HTML_Templates.pm you need to do TWO things:
# 1) add the following subroutine:
########################################################################
# sub site_html_bad_link {
# --------------------------------------------------------
# This routine determines how the bad link report page will look like.
#
# my ($tags, $dynamic) = @_;
# my $template = defined $dynamic ? $dynamic->param('t') : undef;
# (ref $tags eq 'HASH') or croak "HTML_TEMPLATES: Argument '$tags' must be hash reference";
#
# my $output = &load_template ('bad_link.html', {
# %$tags,
# %globals
# }, undef, $template);
# defined $dynamic and &clean_output($dynamic, \$output);
# print $output;
# } # end sub site_html_bad_link
#
########################################################################
# 2) At the top of the file, add &site_html_bad_link to the end of the
# list of exported subroutines -- it's pretty obvious where to put it
#############################################################################
# Lastly: Template Instructions:
#
# I modified the site_html_rate_success subroutine above, so the easiest
# thing to do is make a copy of your rate_success.html template, and call
# it 'bad_link.html' . Replace the non-header stuff with <%Status%>
# -- that _IT_ !!! All I did was take out my "Thank you" message and
# replace it with <%Status%>
##############################################################################
# Upload this file to your LinkSQL CGI directory -- where jump.cgi, etc are
# located. chmod 755 bad_link.cgi
# Call this from your templates or pages as
# <A href="<%db_cgi_url%>/bad_link.cgi?ID=<%ID%>">Report this link as Bad</A>
#
# The only parameter the program expects -- or looks for -- is ID=nnnn
###############################################################################
#
# Still ToDo: Integrate with the URL checking routines, to allow you to
# check, verify, delete a URL.
# Right now all this does is logs the bad URL ONCE... you have to
# interact with the SQL table directly. The benefit -- you get the
# bad link reports, and there's no emails, duplicates, or spam.
##############################################################################

# Load required modules.
# ---------------------------------------------------
use CGI ();
use CGI::Carp qw/fatalsToBrowser/;
use lib 'admin';
use Links;
use Links: B_Utils;
use Links::HTML_Templates;
use Links: BSQL;
use strict;
$|++;

# Create the CGI, and DB objects.
# ---------------------------------------------------
my $in = new CGI;
my ($db, $id, $sth, $total, $rec, $offset, $goto, $track, $update, $time, $SendCGI, $Title);


# Get the Links ID number from the input.
$id = $in->param('ID');

# If an ID was submitted, see if the record exists in the Links database
# if not, return an error.

if (defined $id) {
($id =~ /^\d+$/) or &error ("Invalid id: $id"); # basic error check, can pass to &site_html_error

# Create a database handle, pass it the main links table.
$db = new Links: BSQL $LINKS{admin_root_path} . "/defs/Links.def";

$rec = $db->get_record ($id, 'HASH'); # try to get the record
$rec or &error ("Can't find Link ID: $id"); # it either returns a reference, or 'undef'

# now, see if the record exists in the Bad_Links database
# if it does, it's been reported already, if not, it must be inserted

my $db2 = new Links: BSQL $LINKS{admin_root_path} . "/defs/Bad_Links.def"; # create a new db handle and table reference
my $rec2 = $db2->get_record ($id, 'HASH'); # get the record pointer
my $dynamic; # necessary to pass to &site_html...

if ($rec2) {
print "Content-type: text/html\n\n"; # needed to prevent 'bad header error'

# The only 'unique' variable 'Status', it's the only thing the template is expecting
# that is unique to this routine. Put whatever you want to return in it

# The link is already reported, so use the data in the Bad_Link table
# use the $rec2 reference, and see if a status has been previously reported or not.

$rec2->{'Status'} = qq|
Thank you for taking the time to report the link <B>$rec2->{'Title'}</B> <P>\n
It's already been recorded, and and it's status is:  <B>$rec2->{'Status'}</B>
|;

&site_html_bad_link ( $rec2, $dynamic );
exit;
} else {
print "Content-type: text/html\n\n"; # needed to prevent 'bad header error'

# The only 'unique' variable 'Status', it's the only thing the template is expecting
# that is unique to this routine. Put whatever you want to return in it

# The link hasn't been reported before, so it doesn't exist in the Bad_Link table
# We need to use the data from the original call to the Links table.
# We create a new field 'Status' and pass it with the record. It's not written back
# to the database, so this is ok.

$rec->{'Status'} = qq|
Thank you for taking the time to report the link <B>$rec->{'Title'}</B> <P>\n
It's been recorded, and will be checked ASAP
|;

&site_html_bad_link ( $rec, $dynamic );

# need to escape certain characters or the record can't be inserted into the new table.
# EVEN THOUGH the data already came _from_ an SQL table, it's been 'cleaned'
# In my database, the only characters I need to worry about are ' " ... you might need others

$rec->{'Title'} =~ s/'/\\'/g;
$rec->{'Title'} =~ s/"/\\"/g;

$db2->do ("INSERT INTO Bad_Links (LinkID, URL, Title, IP) VALUES ($id, '$rec->{'URL'}', '$rec->{'Title'}', '$ENV{'REMOTE_ADDR'}')");

exit;
}
}
else { # Oops, no link.
&error ("No link specified!");
}


sub error {
# ------------------------------------------
#
print "Content-type: text/plain\n\n";
print "Error: $_[0]\n";
exit;
}

NOTE: by now everyone should know that a smiley face has to be replaced by ': D' no space.

ALEX: Can't you disable the smileys -- at least for the ' ' combination??

[This message has been edited by pugdog (edited October 27, 1999).]