#!/usr/bin/perl my $r = Apache->request; $r->content_type("text/html"); $r->send_http_header(); use DBI; use strict 'vars'; require 'ctime.pl'; use slashmod; my ($imagedir,$rootdir)=slashmod::config(); my $dbh; sub main { $dbh ||= slashmod::dbconnect(); my %FORM=slashmod::getform(); my %COOKIES=slashmod::getcookies(); my ($aid, $seclev)=("",0); if ($COOKIES{session} ne "") { ($aid,$seclev)=slashmod::getadmininfo($COOKIES{session}); } print " Slashdot Poll"; slashmod::header(); if($seclev > 1000 and $FORM{op} eq "edit") { editpoll($FORM{qid}); } elsif($seclev > 1000 and $FORM{op} eq "save") { savepoll(%FORM); } elsif(not defined $FORM{'qid'} ) { listpolls($seclev,$FORM{min}); } elsif(not defined $FORM{'aid'}) { print "
"; print "

"; slashmod::pollbooth($FORM{'qid'}); print "
"; } else { vote($FORM{'qid'},$FORM{'aid'}); print "

[ Post a Comment About this Poll ]

"; slashmod::titlebar("99%","Comments"); print "\n"; slashmod::printComments("thread",$FORM{qid},$dbh, $seclev,0); print "
\n"; } slashmod::writelog("pollbooth",$FORM{qid}); slashmod::footer(); } sub editpoll { my ($qid)=@_; # Display a form for the Question my $c=$dbh->prepare("SELECT question, voters FROM pollquestions WHERE qid='$qid'"); $c->execute(); my ($question, $voters)=("",0); ($question,$voters)=$c->fetchrow(); if(not defined $voters) { $voters=0; } $c->finish(); print "

"; my ($currentqid)=slashmod::getvar("currentqid"); print "default

\n"; $c=$dbh->prepare("SELECT answer,votes FROM pollanswers WHERE qid='$qid' ORDER BY aid"); $c->execute(); my $x=0; while(my ($answers, $votes)=$c->fetchrow) { $x++; print "
"; } $c->finish(); while($x < 8) { $x++; print "
"; } print "

"; } sub savepoll { my (%FORM)=@_; print $FORM{qid}; # Check if QID exists, and either update/insert my $q=$dbh->prepare("SELECT qid from pollquestions WHERE qid='$FORM{qid}'"); $q->execute(); if($q->fetchrow()) { $dbh->do("UPDATE pollquestions set question='$FORM{question}', voters=$FORM{voters} WHERE qid='$FORM{qid}'"); print "Updated $FORM{qid}
"; if(defined $FORM{currentqid}) { slashmod::setvar("writestatus","1"); slashmod::setvar("currentqid",$FORM{qid}); print "$FORM{qid} is now on homepage
\n"; } } else { $dbh->do("INSERT into pollquestions VALUES('$FORM{qid}','$FORM{question}',0,now())"); print "Inserted $FORM{qid}
"; if(defined $FORM{currentqid}) { slashmod::setvar("writestatus","1"); slashmod::setvar("currentqid",$FORM{qid}); print "$FORM{qid} is now on homepage
\n"; } } $q->finish(); # Loop through 1..8 and insert/update if defined for(my $x=1;$x<9;$x++) { # If aid$x defined, my ($thisaid,$thisvotes)=("aid$x","votes$x"); print "Answer=$FORM{$thisaid} "; if($FORM{$thisaid} ne "") { # Check SQL $a=$dbh->prepare("SELECT qid FROM pollanswers WHERE qid='$FORM{qid}' and aid=$x"); $a->execute(); if(my ($q)=$a->fetchrow()) { $dbh->do("UPDATE pollanswers SET answer='$FORM{$thisaid}', votes=$FORM{$thisvotes} WHERE qid='$FORM{qid}' AND aid=$x"); print "Updated
"; } else { $dbh->do("INSERT into pollanswers VALUES('$FORM{qid}',$x, '$FORM{$thisaid}', $FORM{$thisvotes})"); print "Inserted
"; } $a->finish(); } else { print "Ignored
"; } } } sub vote { my ($qid, $aid) =@_; my ($notes); if($aid>-1) { my $cursor = $dbh->prepare(" SELECT id from pollvoters where qid='$qid' AND id='$ENV{REMOTE_ADDR}'"); $cursor->execute; # print $ENV{REMOTE_ADDR}; if ($cursor->fetchrow()) { $notes="You have already voted."; } else { $notes="Your vote has been registered."; $dbh->do(" INSERT into pollvoters (qid,id,time) VALUES('$qid','$ENV{REMOTE_ADDR}',now())"); $dbh->do(" UPDATE pollquestions SET voters=(voters+1) WHERE qid='$qid'"); $dbh->do(" UPDATE pollanswers set votes=votes+1 WHERE qid='$qid' and aid='$aid'"); } $cursor->finish(); } else { $notes="Displaying results of the poll."; } my $c=$dbh->prepare("SELECT voters,question FROM pollquestions WHERE qid='$qid'"); $c->execute(); my ($totalvotes, $question, $answer, $votes); ($totalvotes,$question)=$c->fetchrow; $c->finish(); print "
"; slashmod::titlebar("99%","$question"); print ""; my $maxcursor=$dbh->prepare("SELECT max(votes) FROM pollanswers WHERE qid='$qid'"); $maxcursor->execute(); my ($maxvotes)=$maxcursor->fetchrow(); $maxcursor->finish(); $a=$dbh->prepare("SELECT answer, votes from pollanswers where qid='$qid' ORDER by aid"); $a->execute; while(($answer, $votes)=$a->fetchrow) { my ($imagewidth,$percent); $imagewidth=int (350*$votes/$maxvotes); if($imagewidth==0) { $imagewidth=1; } $percent=int (100*$votes/$totalvotes); slashmod::pollItem($answer, $imagewidth, $votes, $percent); } print "
$notes
$totalvotes total votes.

[ Voting Booth | Other Polls | Back Home ]
"; print slashmod::getblock("postvote"); print "
"; } sub listpolls { my ($seclev,$min)=@_; if($seclev > 1000) { print "[ New Poll | Admin ]"; } my $cursor = $dbh->prepare(" select qid, question, date_format(date,\"W M D\") from pollquestions order by date DESC "); $cursor->execute; my ($question, $qid,$date); slashmod::titlebar("99%","Slashdot Polls"); my $thisid; while($thisid++ < $min) { $cursor->fetchrow(); } while (($qid, $question,$date) = $cursor->fetchrow and ($min+20) > $thisid++) { print "
  • $question $date "; if($seclev > 1000) { print "(Edit)"; } } print "

    More Polls" unless not $cursor->fetchrow(); $cursor->finish; } main; $dbh->disconnect(); 0;