#!/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"; # we're strict package Main; use strict; # is this getting called by the "delete" cronjob? if ($ARGV[0] eq "cleanup") { exit Lib::cleanup(); } # parse the form data my %FORM = Lib::parse_form(); # some vars my $action = $FORM{"action"}; my $main_content; my %vars; my $template = ($FORM{"template"}) ? $FORM{"template"} : $Conf::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} = 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(); $vars{TEMPLATE_DIR} = "templates/$template"; ############ # 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); ############ # 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 "") { $vars{TITLE} = "An Error Occured"; $vars{ERROR_MESSAGE} = "No text entered"; $main_content = Lib::wrap_template($template, $Lib::CONTENT_ERROR, %vars); } else { # add the kwote my $dbh = Lib::get_db_connection(); my $kid = Lib::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: ".$DBI::errstr; $main_content = Lib::wrap_template($template, $Lib::CONTENT_ERROR, %vars); # all was good } else { $vars{TITLE} = "Kwote Added"; $vars{KWOTE_ID} = $kid; $main_content = Lib::wrap_template($template, $Lib::CONTENT_ADD_THANKS, %vars); } } ########## # action: show } elsif ($action eq "show") { # get the kwote my $dbh = Lib::get_db_connection(); my $kwote = Lib::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 = Lib::wrap_template($template, $Lib::CONTENT_ERROR, %vars); # 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'}); $vars{KWOTE_TEXT_PLAIN} = $kwote->{'content'}; $vars{KWOTE_RATING} = $kwote->{'rating'}; $main_content = Lib::wrap_template($template, $Lib::KWOTE_EVEN, %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 = Lib::get_db_connection(); my @rows = Lib::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 = $Lib::NAVIGATION_NO_BACK; # forward and back } elsif ($FORM{"o"} ne "random" && $start_index>0 && @rows>=$max_returned && ($start_index+$max_returned)<$max_records ) { $navigation_template = $Lib::NAVIGATION; # back only } elsif ($FORM{"o"} ne "random" && $start_index>0 && @rows<$max_returned) { $navigation_template = $Lib::NAVIGATION_NO_FORWARD; } # wrap the navigation template $main_content .= Lib::wrap_template($template, $navigation_template, %vars); # wrap up the "before list" template $main_content .= Lib::wrap_template($template, $Lib::BEFORE_LIST, %vars); # 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; $vars{KWOTE_ID} = $row->{'id'}; $vars{KWOTE_TEXT_HTML} = Lib::html_escape($row->{'content'}); $vars{KWOTE_TEXT_XML} = Lib::xml_escape($row->{'content'}); $vars{KWOTE_TEXT_PLAIN} = $row->{'content'}; $vars{KWOTE_RATING} = $row->{'rating'}; $main_content .= Lib::wrap_template($template, $kwote_template, %vars); } } # wrap up the "after list" template $main_content .= Lib::wrap_template($template, $Lib::AFTER_LIST, %vars); # wrap the navigation template $main_content .= Lib::wrap_template($template, $navigation_template, %vars); ########## # action: search (show the search page) } elsif ($action eq "search") { $vars{TITLE} = "Search"; $main_content = Lib::wrap_template($template, $Lib::CONTENT_SEARCH, %vars); ########## # action: love } elsif ($action eq "love") { Lib::vote($ENV{"REMOTE_ADDR"}, $FORM{"kid"}, "1"); $vars{TITLE} = "Love"; $main_content = "Vote Counted"; ########## # action: hate } elsif ($action eq "hate") { Lib::vote($ENV{"REMOTE_ADDR"}, $FORM{"kid"}, "-1"); $vars{TITLE} = "Hate"; $main_content = "Vote Counted"; ########## # action: captcha } elsif ($action eq "captcha") { Lib::serve_captcha($FORM{'cid'}); exit; ########## # show the homepage } else { $vars{TITLE} = "The Better kwote Database"; $main_content = Lib::wrap_template($template, $Lib::CONTENT_DEFAULT, %vars); } # send the HTML print Lib::get_template_headers($template)."\n\n"; Lib::render_template($template, $Lib::HEADER, %vars); print STDOUT $main_content; Lib::render_template($template, $Lib::FOOTER, %vars);