#!/usr/bin/perl
use strict;
use warnings;

use DBI;
use CGI;
use LWP::Simple;

# Handle to the database so I can store and retrieve the comments
my $db_name="DBI:mysql:testuser";
my $db_user="testuser";
my $db_pass="XXXX";

my $dbh = DBI->connect($db_name, $db_user, $db_pass);

# Handle to work with CGI scripts
my $q = new CGI;

# submits the data to the database
sub submit_data
{
   # Getting the name and comment values from the form
   my $name = $q->param('name');
   my $comment = $q->param('comment');
   # Doing some simple tests for validity
   if (length($name) <= 1)
   {
      print "<p>You must enter a name!</p>";
      return;
   }
   if (length($comment) <= 1)
   {
      print "<p>You must enter a comment!</p>";
      return;
   }
   # Replacing quotes by escaped-quotes for SQL query
   $name =~ s/'/\\'/g;
   $name =~ s/"/\\"/g;
   $comment =~ s/'/\\'/g;
   $comment =~ s/'/\\"/g;
   # Creating and executing the SQL query to insert data
   my $db_query = "insert into comments values (now(), '$name', '$comment')";
   my $query = $dbh->prepare($db_query);
   $query->execute;
   # Deleting all entries to present a clean form
   $q->delete_all;
}

# Gets the comments from the database
sub get_comments
{
   # Creating and executing the SQL search query
   my $db_query = "select date,name,comment from comments order by date desc";
   my $query = $dbh->prepare($db_query);
   $query->execute;
   # Wrapping the query results into list items
   print "<ul>";
   while (my @data=$query->fetchrow)
   {
      my ($date, $name, $comment) = @data;
      print "<li>On $date $name said<br />$comment</li>";
   }
   print "</ul>";
}

# Need to create a form to get name and comment from the user
sub create_form
{
    print $q->startform("post", "/cgi-bin/comments.pl");
    
    print "Name: ",
          $q->textfield(-name=>'name',
                        -default=>'',
                        -size=>50,
                        -maxlength=>80);

    print $q->p;

    print "Comment: ", "<br>",
          $q->textarea(-name=>'comment',
                       -default=>'',
                       -rows=>3,
                       -columns=>50);
    print $q->p;

    print $q->hidden(-name=>'submitted',
                     -default=>'true');

    print $q->submit(-name=>'submit',
                     -value=>'Gimme your comment');
    print $q->reset(-name=>'reset',
                    -value=>'Reset');

    print $q->endform;
}

# Start of main program

# Printing HTML header. Note that we do not print the standard
# HTML head and body tags any longer. Instead we use the quoted
# web page, which will itself contain head etc tags. That way
# any scripts and style sheets of the quoted page will be loaded
# as well.
print $q->header;
# print $q->start_html(-title=>"Comment Page");


# Download a web page and storing it in a variable
my $base_url = "www.shu.edu";
my $webpage = get("http://$base_url/");
# Replacing absolute links by fully referenced links with the right
# base url. This is not perfect, but should work reasonably well in
# most situations. Note that it will not work on relative links, only
# in absolute links.
$webpage =~ s/href="\//href="http:\/\/$base_url\//g;
$webpage =~ s/href='\//href='http:\/\/$base_url\//g;
$webpage =~ s/src="\//src="http:\/\/$base_url\//g;
$webpage =~ s/src='\//src='http:\/\/$base_url\//g;
print "$webpage";

# Now printing the comment portion of the page, after the quoted
# web page
print $q->h1("Comment Page");

# This checks if a user arrived at the page via a url or whether they
# clicked the submit button of the form. Only if they clicked submit
# will the hidden parameter submitted be true.
if ($q->param('submitted'))
{
   &submit_data;
}

print $q->hr;
&create_form;
print $q->hr;
&get_comments;

print $q->end_html;
