237 lines
6.4 KiB
Perl
Executable File
237 lines
6.4 KiB
Perl
Executable File
#!/usr/bin/perl
|
|
|
|
###################################################
|
|
# -*- kwotes.pl -*- #
|
|
###################################################
|
|
# 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 :) #
|
|
###################################################
|
|
|
|
# bring in the required libs
|
|
require "kwotes-lib.pl";
|
|
|
|
# bring in the config
|
|
require "kwotes.conf.pl";
|
|
|
|
# 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;
|
|
my $template = ($FORM{"template"}) ? $FORM{"template"} : $DEFAULT_TEMPLATE;
|
|
|
|
# populate %vars with ENV
|
|
foreach my $key (keys %ENV) {
|
|
$vars{$key} = $ENV{$key};
|
|
}
|
|
|
|
# add information that can be displayed on every page
|
|
$vars{KWOTE_COUNT} = get_kwote_count();
|
|
$vars{KWOTE_BACKUP_COUNT} = get_kwote_backup_count();
|
|
$vars{SITE_NAME} = $SITE_NAME;
|
|
$vars{TAG_LINE} = get_tagline();
|
|
$vars{TEMPLATE_DIR} = "templates/$template";
|
|
|
|
# send header
|
|
send_html_header();
|
|
|
|
############
|
|
# action: add (show add form)
|
|
if ($action eq "add") {
|
|
$vars{TITLE} = "Add Kwote";
|
|
$main_content = wrap_template($template, "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($template, "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($template, "content-error.html",%vars);
|
|
|
|
# all was good
|
|
} else {
|
|
$vars{TITLE} = "Kwote Added";
|
|
$vars{KWOTE_ID} = $kid;
|
|
$main_content = wrap_template($template, "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($template, "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($template, "content-show-kwote.html", %vars);
|
|
|
|
}
|
|
|
|
##########
|
|
# action: latest
|
|
} elsif ($action eq "list") {
|
|
|
|
# what are we sorting on
|
|
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()";
|
|
}
|
|
|
|
# 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"};
|
|
|
|
# rating requirements?
|
|
my $max_rating = $FORM{"maxr"};
|
|
my $min_rating = $FORM{"minr"};
|
|
|
|
# get the kwote
|
|
my $dbh = get_db_connection();
|
|
my @rows = list_kwotes(
|
|
$dbh,
|
|
$sort,
|
|
$sort_order,
|
|
$max_returned,
|
|
$start_index,
|
|
$search_string,
|
|
$min_rating,
|
|
$max_rating
|
|
);
|
|
|
|
# setup these vars
|
|
$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;
|
|
$vars{MAX_RATING} = $max_rating;
|
|
$vars{MIN_RATING} = $min_rating;
|
|
|
|
# get the navigation template
|
|
my $navigation_template = undef;
|
|
|
|
# forward, no back
|
|
if ($FORM{"o"} ne "random" && $start_index<=0 && @rows>=$max_returned
|
|
&& ($start_index+$max_returned)<$max_records) {
|
|
$navigation_template = "content-list-navigate-no-back.html";
|
|
|
|
# forward and back
|
|
} elsif ($FORM{"o"} ne "random" && $start_index>0 && @rows>=$max_returned
|
|
&& ($start_index+$max_returned)<$max_records ) {
|
|
$navigation_template = "content-list-navigate.html";
|
|
|
|
# back only
|
|
} elsif ($FORM{"o"} ne "random" && $start_index>0 && @rows<$max_returned) {
|
|
$navigation_template = "content-list-navigate-no-forward.html";
|
|
}
|
|
|
|
# wrap the navigation template
|
|
$main_content .= wrap_template($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} = html_escape($row->{'content'});
|
|
$vars{KWOTE_TEXT_XML} = xml_escape($row->{'content'});
|
|
$vars{KWOTE_TEXT_PLAIN} = $row->{'content'};
|
|
$vars{KWOTE_RATING} = $row->{'rating'};
|
|
$main_content .= wrap_template($template, "content-show-kwote.html", %vars);
|
|
}
|
|
|
|
}
|
|
|
|
# wrap the navigation template
|
|
$main_content .= wrap_template($template, $navigation_template, %vars);
|
|
|
|
##########
|
|
# action: search (show the search page)
|
|
} elsif ($action eq "search") {
|
|
$vars{TITLE} = "Search";
|
|
$main_content = wrap_template($template, "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($template, "content-default.html", %vars);
|
|
|
|
}
|
|
|
|
# finish the HTML
|
|
render_template($template, "header.html", %vars);
|
|
print STDOUT $main_content;
|
|
render_template($template, "footer.html", %vars);
|