2004-10-14 22:20:03 +00:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
###################################################
|
|
|
|
# -*- kwotes.pl -*- #
|
|
|
|
###################################################
|
2004-10-28 06:29:35 +00:00
|
|
|
# This code is distributed under GPL licensing #
|
|
|
|
# terms, here's a link to the license and stuff: #
|
|
|
|
# #
|
|
|
|
# http://www.gnu.org/licenses/gpl.txt #
|
|
|
|
# #
|
|
|
|
# have fun with it, i'm not a major perl monger #
|
|
|
|
# or anything, so be easy on me :) #
|
2004-10-14 22:20:03 +00:00
|
|
|
###################################################
|
|
|
|
|
|
|
|
# bring in the required libs
|
|
|
|
require "kwotes-lib.pl";
|
|
|
|
|
2004-10-15 19:35:43 +00:00
|
|
|
# bring in the config
|
|
|
|
require "kwotes.conf.pl";
|
|
|
|
|
2004-10-14 22:20:03 +00:00
|
|
|
# is this getting called by the "delete" cronjob?
|
|
|
|
if ($ARGV[0] eq "cleanup") {
|
|
|
|
exit cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
# parse the form data
|
|
|
|
my %FORM = parse_form();
|
|
|
|
|
|
|
|
# some vars
|
|
|
|
my $action = $FORM{"action"};
|
|
|
|
my $main_content;
|
|
|
|
my %vars;
|
|
|
|
|
|
|
|
# populate %vars with ENV
|
|
|
|
foreach my $key (keys %ENV) {
|
|
|
|
$vars{$key} = $ENV{$key};
|
|
|
|
}
|
|
|
|
|
2004-10-28 06:29:35 +00:00
|
|
|
# add information that can be displayed on every page
|
2004-10-14 22:20:03 +00:00
|
|
|
$vars{KWOTE_COUNT} = get_kwote_count();
|
|
|
|
$vars{KWOTE_BACKUP_COUNT} = get_kwote_backup_count();
|
2004-10-28 06:29:35 +00:00
|
|
|
$vars{SITE_NAME} = $SITE_NAME;
|
|
|
|
$vars{TAG_LINE} = get_tagline();
|
2004-10-14 22:20:03 +00:00
|
|
|
|
2004-10-28 06:29:35 +00:00
|
|
|
# send header
|
2004-10-15 19:35:43 +00:00
|
|
|
send_html_header();
|
|
|
|
|
2004-10-14 22:20:03 +00:00
|
|
|
############
|
|
|
|
# action: add (show add form)
|
|
|
|
if ($action eq "add") {
|
|
|
|
$vars{TITLE} = "Add Kwote";
|
|
|
|
$main_content = wrap_template("html/content-addform.html", %vars);
|
|
|
|
|
|
|
|
############
|
|
|
|
# action: doadd (add the kwote to the db)
|
|
|
|
} elsif ($action eq "doadd") {
|
|
|
|
|
|
|
|
if ($FORM{"content"} eq "") {
|
|
|
|
$vars{TITLE} = "An Error Occured";
|
|
|
|
$vars{ERROR_MESSAGE} = "No text entered";
|
|
|
|
$main_content = wrap_template("html/content-error.html",%vars);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
# add the kwote
|
|
|
|
my $dbh = get_db_connection();
|
|
|
|
my $kid = add_kwote($dbh, $FORM{"content"}, $ENV{"REMOTE_ADDR"});
|
|
|
|
|
|
|
|
# wtf? errors? in my code? noooo.
|
|
|
|
if (!defined($kid)) {
|
|
|
|
$vars{TITLE} = "An Error Occured";
|
|
|
|
$vars{ERROR_MESSAGE} = "Couldn't add kwote";
|
|
|
|
$main_content = wrap_template("html/content-error.html",%vars);
|
|
|
|
|
|
|
|
# all was good
|
|
|
|
} else {
|
|
|
|
$vars{TITLE} = "Kwote Added";
|
|
|
|
$vars{KWOTE_ID} = $kid;
|
|
|
|
$main_content = wrap_template("html/content-addform-thanks.html", %vars);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
##########
|
|
|
|
# action: show
|
|
|
|
} elsif ($action eq "show") {
|
|
|
|
|
|
|
|
# get the kwote
|
|
|
|
my $dbh = get_db_connection();
|
|
|
|
my $kwote = get_kwote($dbh, $FORM{"id"});
|
|
|
|
|
|
|
|
# wtf? errors? in my code? noooo.
|
|
|
|
if (!defined($kwote)) {
|
|
|
|
$vars{TITLE} = "Kwote Does Not Exist";
|
|
|
|
$vars{ERROR_MESSAGE} = "That kwote does not exist";
|
|
|
|
$main_content = wrap_template("html/content-error.html",%vars);
|
|
|
|
|
|
|
|
# all was good
|
|
|
|
} else {
|
|
|
|
$vars{TITLE} = "Kwote \#$kwote->{'id'}";
|
|
|
|
$vars{KWOTE_ID} = $kwote->{'id'};
|
|
|
|
$vars{KWOTE_TEXT} = html_escape($kwote->{'content'});
|
|
|
|
$vars{KWOTE_RATING} = $kwote->{'rating'};
|
|
|
|
$main_content = wrap_template("html/content-show-kwote.html", %vars);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
##########
|
|
|
|
# action: latest
|
|
|
|
} elsif ($action eq "list") {
|
|
|
|
|
|
|
|
# what are we sorting on
|
2004-10-27 01:12:22 +00:00
|
|
|
my $sort = undef;
|
|
|
|
if ($FORM{"o"} eq "date") {
|
|
|
|
$sort = "submit_dt";
|
|
|
|
} elsif ($FORM{"o"} eq "rating") {
|
|
|
|
$sort = "rating";
|
|
|
|
} elsif ($FORM{"o"} eq "random") {
|
|
|
|
$sort = "RAND()";
|
|
|
|
}
|
2004-10-14 22:20:03 +00:00
|
|
|
|
|
|
|
# get start index
|
|
|
|
my $start_index = (defined($FORM{"s"})) ? $FORM{"s"} : 0;
|
|
|
|
|
|
|
|
# get max amount
|
|
|
|
my $max_returned = (defined($FORM{"m"})) ? $FORM{"m"} : 20;
|
|
|
|
|
|
|
|
# what is the "max records" we'll consider?
|
|
|
|
my $max_records = (defined($FORM{"mr"})) ? $FORM{"mr"} : 9999999999;
|
|
|
|
|
|
|
|
# what is the "sort order"
|
|
|
|
my $sort_order = ($FORM{"so"} eq "reverse") ? "ASC" : "DESC";
|
|
|
|
|
|
|
|
# search string?
|
|
|
|
my $search_string = $FORM{"ss"};
|
|
|
|
|
2004-10-28 06:29:35 +00:00
|
|
|
# rating requirements?
|
|
|
|
my $max_rating = $FORM{"maxr"};
|
|
|
|
my $min_rating = $FORM{"minr"};
|
|
|
|
|
2004-10-14 22:20:03 +00:00
|
|
|
# get the kwote
|
|
|
|
my $dbh = get_db_connection();
|
2004-10-28 06:29:35 +00:00
|
|
|
my @rows = list_kwotes(
|
|
|
|
$dbh,
|
|
|
|
$sort,
|
|
|
|
$sort_order,
|
|
|
|
$max_returned,
|
|
|
|
$start_index,
|
|
|
|
$search_string,
|
|
|
|
$min_rating,
|
|
|
|
$max_rating
|
|
|
|
);
|
2004-10-14 22:20:03 +00:00
|
|
|
|
|
|
|
# setup these vars
|
2004-10-27 01:12:22 +00:00
|
|
|
$vars{TITLE} = "Kwotes";
|
|
|
|
$vars{ORDER} = $FORM{"o"};
|
|
|
|
$vars{NEXT_INDEX} = $start_index+$max_returned;
|
|
|
|
$vars{MAX_RETURN} = $max_returned;
|
|
|
|
$vars{LAST_INDEX} = $start_index-$max_returned;
|
|
|
|
$vars{MAX_RECORDS} = $max_records;
|
|
|
|
$vars{SEARCH_STRING} = $search_string;
|
2004-10-14 22:20:03 +00:00
|
|
|
|
|
|
|
# get the navigation template
|
|
|
|
my $navigation_template = undef;
|
|
|
|
|
|
|
|
# forward, no back
|
2004-10-28 06:29:35 +00:00
|
|
|
if ($FORM{"o"} ne "random" && $start_index<=0 && @rows>=$max_returned
|
2004-10-14 22:20:03 +00:00
|
|
|
&& ($start_index+$max_returned)<$max_records) {
|
|
|
|
$navigation_template = "html/content-list-navigate-no-back.html";
|
|
|
|
|
|
|
|
# forward and back
|
2004-10-28 06:29:35 +00:00
|
|
|
} elsif ($FORM{"o"} ne "random" && $start_index>0 && @rows>=$max_returned
|
2004-10-14 22:20:03 +00:00
|
|
|
&& ($start_index+$max_returned)<$max_records ) {
|
|
|
|
$navigation_template = "html/content-list-navigate.html";
|
|
|
|
|
|
|
|
# back only
|
2004-10-28 06:29:35 +00:00
|
|
|
} elsif ($FORM{"o"} ne "random" && $start_index>0 && @rows<$max_returned) {
|
2004-10-14 22:20:03 +00:00
|
|
|
$navigation_template = "html/content-list-navigate-no-forward.html";
|
|
|
|
}
|
|
|
|
|
|
|
|
# wrap the navigation template
|
|
|
|
$main_content .= wrap_template($navigation_template, %vars);
|
|
|
|
|
|
|
|
# loop through the results
|
|
|
|
if (defined(@rows)) {
|
|
|
|
for (my $i=0; $i<@rows && ($i+$start_index)<$max_records; $i++) {
|
|
|
|
my $row = $rows[$i];
|
|
|
|
$vars{KWOTE_ID} = $row->{'id'};
|
|
|
|
$vars{KWOTE_TEXT} = html_escape($row->{'content'});
|
|
|
|
$vars{KWOTE_RATING} = $row->{'rating'};
|
|
|
|
$main_content .= wrap_template("html/content-show-kwote.html", %vars);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
# wrap the navigation template
|
|
|
|
$main_content .= wrap_template($navigation_template, %vars);
|
|
|
|
|
|
|
|
##########
|
|
|
|
# action: search (show the search page)
|
|
|
|
} elsif ($action eq "search") {
|
|
|
|
$vars{TITLE} = "Search";
|
|
|
|
$main_content = wrap_template("html/content-search.html", %vars);
|
|
|
|
|
|
|
|
##########
|
|
|
|
# action: love
|
|
|
|
} elsif ($action eq "love") {
|
|
|
|
vote($ENV{"REMOTE_ADDR"}, $FORM{"kid"}, "1");
|
|
|
|
$vars{TITLE} = "Love";
|
|
|
|
$main_content = "Vote Counted";
|
|
|
|
|
|
|
|
##########
|
|
|
|
# action: hate
|
|
|
|
} elsif ($action eq "hate") {
|
|
|
|
vote($ENV{"REMOTE_ADDR"}, $FORM{"kid"}, "-1");
|
|
|
|
$vars{TITLE} = "Hate";
|
|
|
|
$main_content = "Vote Counted";
|
|
|
|
|
|
|
|
##########
|
|
|
|
# show the homepage
|
|
|
|
} else {
|
|
|
|
$vars{TITLE} = "The Better kwote Database";
|
|
|
|
$main_content = wrap_template("html/content-default.html", %vars);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
# finish the HTML
|
|
|
|
render_template("html/header.html", %vars);
|
|
|
|
print STDOUT $main_content;
|
|
|
|
render_template("html/footer.html", %vars);
|