#!/usr/local/bin/perl
# ==============================================================
#
#    Report a bad link  version '2.1.1a'
#    Links SQL code copyright Gossamer Threads
#            http://www.gossamer-threads.com
#    Modifications and code segments copyright PUGDOG Enterprises, Inc.
#            http://www.pugdog.com
#    Updates for Links SQL 2.1.1 Compatiblity by Andy 
#            http://www.ace-installer.com 
#
# ==============================================================

# Load required modules.
# ---------------------------------------------------
    use strict;
    use lib '/home/hsasia/public_html/cgi-bin/admin';			## you need to set this to your own local path!
    use Links qw/$DB $IN $USER $CFG/;						## initialize the Database, CGI and other basic objects.
    use Links::SiteHTML;
    use Links::Authenticate;
    use Links::Plugins;
    use vars qw/$BAD_LINK_CFG/;

#    use GT::Plugins qw/STOP CONTINUE/;
#    use GT::Mail;  
#    $|++;
#    Links::reset_env( { load_user => 1 } ) if ($Links::PERSIST);

    local $SIG{__DIE__} = \&Links::fatal;

    Links::init('/home/hsasia/public_html/cgi-bin/admin');
	
    Links::init_user();
	


&main();

sub main {

# Define the variables
# ---------------------------------------------------
	my ($id, $db_links, $rec, $confirm);

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

	## check to see if an ID was submitted and that it was numeric
    if (!(defined $id) or !($id =~ /^\d+$/)) {
		print $IN->header();
		print Links::SiteHTML::display('error', {error => "Invalid id: $id"});
		return;
    } 
	## Check to see if the ID/Link record exists

	$db_links   = $DB->table('Links');		## first grab a new db handle
	
	$rec = $db_links->get ($id);			## see if the ID record exists

	if (! $rec) {							## handle errors
			print $IN->header();
			print Links::SiteHTML::display ('error', { error => Links::language ('JUMP_INVALIDID', $id) });
			return;			
	}

	#print "The ID was found, the rec is ", %$rec, "<br><BR>";

    if (!($confirm)) {
		$rec->{'Status'} = '';
		print $IN->header();
		print Links::SiteHTML::display ('bad_link', $rec );		return;
    } 


	## Only waste the CPU after we've got a good link in the Links database.
	##
	## Need to put the configuration variables into the $CFG hash, or similar, and then 
	## they are globally available, and can be assigned/passed in a hash between routines.
	## Works with persistence -- mod_perl speedyCGI, etc
	
	$BAD_LINK_CFG  = Links::Plugins::get_plugin_user_cfg ('Bad_Link');	
	my $bad_links_table = $BAD_LINK_CFG->{'table_name'};

	# now, see if the record exists in the Bad_Links database

	my  $db_bad_links	= $DB->table($bad_links_table);
	my  $rec2			= $db_bad_links->get ($id);
	my  $dynamic;

	if ($db_bad_links->hits) {
			$rec->{'Status'} = qq|
					Thank you for taking the time to report the link <B>$rec->{'Title'}</B> <P>\n
					It's already been recorded, and and it's status is: &nbsp;<B>$rec2->{'Status'}</B>
				|;
	
			print $IN->header();
			print Links::SiteHTML::display ('bad_link', $rec );
			return;			
	
	} else {		
				$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
					|;

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

				$db_bad_links->add ( {  LinkID => $id,
										URL    => $rec->{'URL'},
										Title  => $rec->{'Title'},
										IP     => $ENV{'REMOTE_ADDR'}
									}  );

				print $IN->header();
				print Links::SiteHTML::display ('bad_link', $rec );
				return;	
	}

} ## end of main