#!/usr/bin/perl use strict; use CGI qw(:all); my $BASE = "http://www.forgetmenots.comm/images"; =for MySQL DROP TABLE votes; CREATE TABLE votes ( disk TINYINT DEFAULT 1 NOT NULL, image TINYINT DEFAULT 1 NOT NULL, when TIMESTAMP DEFAULT 0 NOT NULL, host CHAR(16) DEFAULT '127.0.0.1' NOT NULL, vote TINYINT, INDEX (disk, image, when, host) ) =cut print header, start_html("Am I a forget-me-not?"), h1("Am I a forget-me-not?"); my $previous_vote = ""; my $disk; my $image; { defined (my $votedisk = param("disk")) or last; defined (my $voteimage = param("image")) or last; defined (my $rating = param("rating")) or last; $votedisk =~ /\A[1-5]\z/ or last; $voteimage =~ /\A\d{3}\z/ and $voteimage >= 1 and $voteimage <= 100 or last; $rating =~ /\A([1-9]|10|abstain)\z/ or last; require DBI; my $dbh = DBI->connect("dbi:mysql:merlyn_amiforgetmenot", "username", "password", { RaiseError => 1 }); ## verify no recent previous vote if ($rating ne "abstain" and not $dbh->selectrow_array('SELECT count(*) FROM votes ' . 'WHERE disk = ? AND image = ? AND host = ? ' . 'AND when > DATE_SUB(NOW(), INTERVAL 1 HOUR)', undef, $votedisk, $voteimage, $ENV{REMOTE_ADDR})) { ## store vote $dbh->do("INSERT INTO votes (disk, image, when, host, vote) ". "VALUES (?, ?, now(), ?, ?)", undef, $votedisk, $voteimage, $ENV{REMOTE_ADDR}, $rating); } ## get average vote, count my ($average, $count) = $dbh->selectrow_array('SELECT avg(vote), count(*) FROM votes ' . 'WHERE disk = ? AND image = ? ' . 'GROUP BY disk, image', undef, $votedisk, $voteimage); $average = defined $average ? (sprintf "%.1f", $average) : "?"; $count = (not defined $count or $count == 0) ? "no votes" : ($count == 1) ? "1 vote" : "$count votes"; $previous_vote = table({-border => 1}, Tr(td({align => 'center'}, "What others thought", br, $average, br, "based on $count", br, "You rated it: $rating", br, img({src => "$BASE/Disk$votedisk/Thumb/$voteimage.jpg"}), ))); if ($voteimage > 99) { $image = "001"; if ($votedisk > 5) { $disk = 1; } else { $disk = $votedisk + 1; } } else { $image = sprintf "%03d", $voteimage + 1; $disk = $votedisk; } $dbh->disconnect; # not needed in mod_perl } unless ($disk) { BEGIN { srand; } $disk = 1 + int rand 5; $image = sprintf "%03d", 1 + int rand 100; } param("disk", $disk); param("image", $image); param("rating", "abstain"); print start_form, table({-border => 0, -colspacing => 0, -colpadding => 2}, Tr(td({-valign => 'top'}, $previous_vote), td(table(Tr(td({-align => 'right'}, p("Tell me, is this a", a({-href => "/cgi/go/http://www.forgetmenots.comm/"}, "Forget-me-not"), "or not?"), radio_group(-name => "rating", -values => [1..10, 'abstain'], -rows => 1), hidden("disk"), hidden("image")), td(submit("vote"))), Tr(td( ## "disk $disk image $image
", # debug img({src => "$BASE/Disk$disk/Small/$image.jpg"})), td(table(Tr(td("see more:")), map { Tr(td(a({-href => "/cgi/go/$BASE/Disk$disk/$_/$image.jpg"}, $_))) } qw(Thumb Small Medium Large Exlarge)))))))), end_form; print p("Please note that images may be copyrighted by their", "respective owners, and that this voting system", "is not affiliated in any way with the Forget-Me-Nots site,", "or related to (no matter how inspired by) the", a({-href => "/cgi/go/http://www.amihotornot.com/"}, "Am I Hot Or Not?"), "site."), print end_html;