kwotes/kwotes.pl

261 lines
7.4 KiB
Perl
Raw Normal View History

2004-10-14 22:20:03 +00:00
#!/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 :) #
2004-10-14 22:20:03 +00:00
###################################################
# bring in the required libs
require "kwotes-lib.pl";
# we're strict
package Main;
use strict;
2004-10-14 22:20:03 +00:00
# is this getting called by the "delete" cronjob?
if ($ARGV[0] eq "cleanup") {
exit Lib::cleanup();
2004-10-14 22:20:03 +00:00
}
# parse the form data
my %FORM = Lib::parse_form();
2004-10-14 22:20:03 +00:00
# some vars
my $action = $FORM{"action"};
my $main_content;
my %vars;
my $template = ($FORM{"template"}) ? $FORM{"template"} : $Conf::DEFAULT_TEMPLATE;
2004-10-14 22:20:03 +00:00
# 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} = Lib::get_kwote_count();
$vars{KWOTE_BACKUP_COUNT} = Lib::get_kwote_backup_count();
$vars{SITE_NAME} = $Conf::SITE_NAME;
$vars{TAG_LINE} = Lib::get_tagline();
2004-11-02 01:38:42 +00:00
$vars{TEMPLATE_DIR} = "templates/$template";
2004-10-14 22:20:03 +00:00
############
# action: add (show add form)
if ($action eq "add") {
$vars{TITLE} = "Add Kwote";
$vars{CAPTCHA_ID} = Lib::gen_captcha_phrase();
$vars{KWOTE_TEXT} = "";
$main_content = Lib::wrap_template($template, $Lib::CONTENT_ADD, %vars);
2004-10-14 22:20:03 +00:00
############
# action: doadd (add the kwote to the db)
} elsif ($action eq "doadd") {
# check the captcha
if (!Lib::validate_captcha($FORM{"cid"}, $FORM{"phrase"})) {
$vars{TITLE} = "Add Kwote";
$vars{ERROR_MESSAGE} = "Captcha validation failed";
$vars{CAPTCHA_ID} = Lib::gen_captcha_phrase();
$vars{KWOTE_TEXT} = $FORM{"content"};
$main_content = Lib::wrap_template($template, $Lib::CONTENT_ADD, %vars);
} elsif ($FORM{"content"} eq "") {
2004-10-14 22:20:03 +00:00
$vars{TITLE} = "An Error Occured";
$vars{ERROR_MESSAGE} = "No text entered";
$main_content = Lib::wrap_template($template, $Lib::CONTENT_ERROR, %vars);
2004-10-14 22:20:03 +00:00
} else {
# add the kwote
my $dbh = Lib::get_db_connection();
my $kid = Lib::add_kwote($dbh, $FORM{"content"}, $ENV{"REMOTE_ADDR"});
2004-10-14 22:20:03 +00:00
# wtf? errors? in my code? noooo.
if (!defined($kid)) {
$vars{TITLE} = "An Error Occured";
2004-11-04 19:57:59 +00:00
$vars{ERROR_MESSAGE} = "Couldn't add kwote: ".$DBI::errstr;
$main_content = Lib::wrap_template($template, $Lib::CONTENT_ERROR, %vars);
2004-10-14 22:20:03 +00:00
# all was good
} else {
$vars{TITLE} = "Kwote Added";
$vars{KWOTE_ID} = $kid;
$main_content = Lib::wrap_template($template, $Lib::CONTENT_ADD_THANKS, %vars);
2004-10-14 22:20:03 +00:00
}
}
##########
# action: show
} elsif ($action eq "show") {
# get the kwote
my $dbh = Lib::get_db_connection();
my $kwote = Lib::get_kwote($dbh, $FORM{"id"});
2004-10-14 22:20:03 +00:00
# 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 = Lib::wrap_template($template, $Lib::CONTENT_ERROR, %vars);
2004-10-14 22:20:03 +00:00
# all was good
} else {
$vars{TITLE} = "Kwote \#$kwote->{'id'}";
$vars{KWOTE_ID} = $kwote->{'id'};
$vars{KWOTE_TEXT_HTML} = Lib::html_escape($kwote->{'content'});
$vars{KWOTE_TEXT_XML} = Lib::xml_escape($kwote->{'content'});
2004-11-02 06:52:06 +00:00
$vars{KWOTE_TEXT_PLAIN} = $kwote->{'content'};
2004-10-14 22:20:03 +00:00
$vars{KWOTE_RATING} = $kwote->{'rating'};
$main_content = Lib::wrap_template($template, $Lib::KWOTE_EVEN, %vars);
2004-10-14 22:20:03 +00:00
}
##########
# 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()";
}
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"};
# 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 = Lib::get_db_connection();
my @rows = Lib::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
$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-11-02 01:38:42 +00:00
$vars{MAX_RATING} = $max_rating;
$vars{MIN_RATING} = $min_rating;
2004-10-14 22:20:03 +00:00
# get the navigation template
my $navigation_template = undef;
# forward, no back
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 = $Lib::NAVIGATION_NO_BACK;
2004-10-14 22:20:03 +00:00
# forward and back
} 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 = $Lib::NAVIGATION;
2004-10-14 22:20:03 +00:00
# back only
} elsif ($FORM{"o"} ne "random" && $start_index>0 && @rows<$max_returned) {
$navigation_template = $Lib::NAVIGATION_NO_FORWARD;
2004-10-14 22:20:03 +00:00
}
# wrap the navigation template
$main_content .= Lib::wrap_template($template, $navigation_template, %vars);
2004-10-14 22:20:03 +00:00
# wrap up the "before list" template
$main_content .= Lib::wrap_template($template, $Lib::BEFORE_LIST, %vars);
2004-10-14 22:20:03 +00:00
# loop through the results
if (defined(@rows)) {
for (my $i=0; $i<@rows && ($i+$start_index)<$max_records; $i++) {
my $row = $rows[$i];
my $kwote_template = (($i % 2) != 0) ? $Lib::KWOTE_ODD : $Lib::KWOTE_EVEN;
2004-10-14 22:20:03 +00:00
$vars{KWOTE_ID} = $row->{'id'};
$vars{KWOTE_TEXT_HTML} = Lib::html_escape($row->{'content'});
$vars{KWOTE_TEXT_XML} = Lib::xml_escape($row->{'content'});
2004-11-02 01:38:42 +00:00
$vars{KWOTE_TEXT_PLAIN} = $row->{'content'};
2004-10-14 22:20:03 +00:00
$vars{KWOTE_RATING} = $row->{'rating'};
$main_content .= Lib::wrap_template($template, $kwote_template, %vars);
2004-10-14 22:20:03 +00:00
}
}
# wrap up the "after list" template
$main_content .= Lib::wrap_template($template, $Lib::AFTER_LIST, %vars);
2004-10-14 22:20:03 +00:00
# wrap the navigation template
$main_content .= Lib::wrap_template($template, $navigation_template, %vars);
2004-10-14 22:20:03 +00:00
##########
# action: search (show the search page)
} elsif ($action eq "search") {
$vars{TITLE} = "Search";
$main_content = Lib::wrap_template($template, $Lib::CONTENT_SEARCH, %vars);
2004-10-14 22:20:03 +00:00
##########
# action: love
} elsif ($action eq "love") {
Lib::vote($ENV{"REMOTE_ADDR"}, $FORM{"kid"}, "1");
2004-10-14 22:20:03 +00:00
$vars{TITLE} = "Love";
$main_content = "Vote Counted";
##########
# action: hate
} elsif ($action eq "hate") {
Lib::vote($ENV{"REMOTE_ADDR"}, $FORM{"kid"}, "-1");
2004-10-14 22:20:03 +00:00
$vars{TITLE} = "Hate";
$main_content = "Vote Counted";
##########
# action: captcha
} elsif ($action eq "captcha") {
Lib::serve_captcha($FORM{'cid'});
exit;
2004-10-14 22:20:03 +00:00
##########
# show the homepage
} else {
$vars{TITLE} = "The Better kwote Database";
$main_content = Lib::wrap_template($template, $Lib::CONTENT_DEFAULT, %vars);
2004-10-14 22:20:03 +00:00
}
# send the HTML
print Lib::get_template_headers($template)."\n\n";
Lib::render_template($template, $Lib::HEADER, %vars);
2004-10-14 22:20:03 +00:00
print STDOUT $main_content;
Lib::render_template($template, $Lib::FOOTER, %vars);