#!/usr/bin/perl -w #QED: Question Editing Database #(C) 2006-2007 by Carlo Angiuli use CGI::Carp 'fatalsToBrowser'; use strict; use warnings; use DBI; use CGI; use Digest::MD5 'md5_hex'; #use MIME::Lite; # These two are imported later if needed. #use Net::SMTP; my ($mail_from, $org_name, $mysql_database, $mysql_host, $mysql_port, $mysql_user, $mysql_pass, @types, @categories, $dtbu, $dtbq, $isadmin, $isround); ########################## ##### User Variables ##### ########################## $mail_from = ''; $org_name = ""; $mysql_database = ""; $mysql_host = ""; $mysql_port = 3306; $mysql_user = ""; $mysql_pass = ""; $dtbu = ""; $dtbq = ""; @types = ("Tossup", "Bonus"); @categories = ( "Fine Arts: Music", "Fine Arts: Visual Art", "Fine Arts: Other", "Literature: Language Arts", "Literature: Literature", "Literature: Mythology", "Math: Algebra", "Math: Calculus", "Math: General", "Math: Geometry", "Math: Other", "Miscellaneous: Entertainment", "Miscellaneous: Interdisciplinary", "Miscellaneous: Sports", "Miscellaneous: Technology", "Miscellaneous: Other", "Science: Astronomy", "Science: Biology", "Science: Chemistry", "Science: Earth Science", "Science: Physics", "Social Studies: Current Events", "Social Studies: Geography", "Social Studies: U.S. History", "Social Studies: World History", "Social Studies: Other" ); ########################## ###### General Code ###### ########################## my $prgm_version = "1.40"; sub login_screen; sub gui; sub gui_home; sub edit_question; sub new_question; sub gui_edit; sub gui_list; sub gui_list_table; sub search; sub gui_about; sub gui_delete; sub gui_delete_done; sub gui_search; sub admin_backup; sub admin_gui_config; sub admin_gui_users; sub admin_gui_stats; sub admin_gui_rounds; sub round_gui_options; sub round_gui_acquire; sub round_gui_acquire_table; sub round_acquire; sub round_unacquire; sub round_gui_organize; sub round_organize; sub round_gui_order; sub round_order; sub round_gui_stats; sub round_gui_output; sub round_output; sub order_string; sub rtrim; sub ceiling; sub parseget; sub output_question; print "Content-type: text/html\n\n"; my $cgi= new CGI; my $ip = $ENV{'REMOTE_ADDR'}; my $filename = $ENV{'SCRIPT_NAME'}; my $pagename = lc($ENV{'SCRIPT_URL'}); #follows qed.pl/ in URL $pagename =~ /$filename\/(.*)/; $pagename = $1; my $query; my ($username, $password, $round); for (split(';',$ENV{'HTTP_COOKIE'})) { if (/qed=([a-zA-Z0-9]+)\|([a-f0-9]+)/) { $username = $1; $password = $2; } elsif (/qed-rnd=([a-zA-Z0-9]+)/) { $round = $1; } } #CREATE TABLE users ( # user CHAR(10), # password CHAR(32), # flags INT(11)); #The password column contains the md5 hash of the password, #or if it's a round, the number of packets it contains. # #CREATE TABLE questions ( # id INTEGER AUTO_INCREMENT PRIMARY KEY, # type TEXT, # category TEXT, # question TEXT, # answer TEXT, # owner TEXT, # round TEXT, # comment TEXT,); my $dbh = DBI->connect("DBI:mysql:database=${mysql_database};host=${mysql_host};port=${mysql_port}",$mysql_user,$mysql_pass); $query = $dbh->prepare("SELECT * FROM $dtbu WHERE user=?"); $query->execute($username); my @sqluser = $query->fetchrow_array(); $query->finish(); #A cookie is set client-side when logging in, with the format qed=(username)|(md5hash), #where the hash is calculated client-side, so only the username is transmitted plaintext. #The hash is md5(ip . md5(pass)), so only the same IP address can use that hash. if ($password ne md5_hex($ENV{'REMOTE_ADDR'} . $sqluser[1])) { login_screen; } elsif ($pagename =~ /^output=(\d{1,2})/) { round_output($1); } elsif ($pagename =~ /^delete=(\d)/) { gui_delete; } elsif ($cgi->param("cgi") && $cgi->param("delete")) { gui_delete_done; } elsif ($sqluser[2] == 0) { $round = ""; gui; } elsif ($sqluser[2] == 1) { $isadmin = 1; gui; } ########################## #### Normal Interface #### ########################## sub login_screen { print qq`
Hello, $username! You currently have $questions questions. `; if ($flagged > 1) {print qq`You have $flagged questions flagged for attention.`;} elsif ($flagged == 1) {print qq`You have 1 question flagged for attention.`;} else {print qq`You have no questions flagged for attention.`;} print qq`
Having problems? Check the help file or email Carlo.
`; } sub edit_question { my $iscomp = ""; $iscomp = " (Comp)" if $cgi->param("computational") eq "Computational"; my $id = $cgi->param("id"); my $type = $dbh->quote($cgi->param("type")); my $category = $dbh->quote($cgi->param("category") . $iscomp); my $question = $dbh->quote(rtrim($cgi->param("question"))); my $answer = $dbh->quote(rtrim($cgi->param("answer"))); my $comment = $dbh->quote($cgi->param("comment")); $dbh->do("UPDATE $dtbq SET type=$type , category=$category , question=$question , answer=$answer , comment=$comment WHERE id = $id"); } sub new_question { my $iscomp = ""; $iscomp = " (Comp)" if $cgi->param("computational") eq "Computational"; my $type = $dbh->quote($cgi->param("type")); my $category = $dbh->quote($cgi->param("category") . $iscomp); my $question = $dbh->quote(rtrim($cgi->param("question"))); my $answer = $dbh->quote(rtrim($cgi->param("answer"))); my $comment = $dbh->quote($cgi->param("comment")); $dbh->do("INSERT INTO $dtbq (id, type, category, question, answer, owner, round, comment) VALUES(NULL, $type , $category , $question , $answer , '$username', '', $comment);"); } sub gui_edit { $pagename =~ /edit=(\d*)/; print qq` `; if ($1 eq "") { print qq`