#!/usr/bin/perl ################################################### # -*- kwotes.pl -*- # ################################################### # TOD: Put some interesting shit here, perhaps # # something about kwotes being GPL # ################################################### # bring in the config require "kwotes.conf.pl"; # bring in the required libs require "kwotes-lib.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; # populate %vars with ENV foreach my $key (keys %ENV) { $vars{$key} = $ENV{$key}; } # send the HTML header send_html_header(); # add information that is displayed on every page $vars{KWOTE_COUNT} = get_kwote_count(); $vars{KWOTE_BACKUP_COUNT} = get_kwote_backup_count(); ############ # 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 my $sort = ($FORM{"o"} eq "date") ? "submit_dt" : ( ($FORM{"o"} eq "rating") ? "rating" : undef); # 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"}; # get the kwote my $dbh = get_db_connection(); my @rows = list_kwotes($dbh, $sort, $sort_order, $max_returned, $start_index, $search_string); # 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; # add the search header if it was a search if (defined($search_string)) { $main_content .= wrap_template("html/content-search.html", %vars); } # get the navigation template my $navigation_template = undef; # forward, no back if ($start_index<=0 && @rows>=$max_returned && ($start_index+$max_returned)<$max_records) { $navigation_template = "html/content-list-navigate-no-back.html"; # forward and back } elsif ($start_index>0 && @rows>=$max_returned && ($start_index+$max_returned)<$max_records ) { $navigation_template = "html/content-list-navigate.html"; # back only } elsif ($start_index>0 && @rows<$max_returned) { $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);