#!/usr/bin/perl ########################################################### # Poll.cgi - Sander van Dijk (sander@codeonrequest.nl) # This code may be used and copied for any purpose, but I would appreciate # it if you mention my name or put a link to www.codeonrequest on the site this code is used on. # Although I try to make my code bug-free I can't guarantee that # this code is perfect and that it won't cause any physical or mental damage. # Use of this code is on your own risk. ########################################################### $decimalen = "1"; $meerdere_stemmen = 1; ########################################################### $q = $ENV{QUERY_STRING}; $wie = $ENV{REMOTE_ADDR}; # Lees de huidige stand in open (STAND, "stand.txt"); foreach $a () { chop $a; $aantal[$i++] = $a; $totaal += $a; } close (STAND); if ( $q ne "" ) { # Kijk of niet al eerder gestemd is; $goed=1; if ( $meerdere_stemmen==0 ) { open (LOG, "log.txt"); foreach $ip () { chomp $ip; if ( $ip eq $wie ) { $goed=0; } } close (LOG); } # Als het IP-adres niet gevonden is, sla stem op if ( $goed==1 ) { ($naam, $waarde) = split(/=/,$q); $aantal[$waarde]++; $totaal++; open (STAND,">stand.txt"); foreach $a (@aantal) { print STAND "$a\n"; if ( $a eq "" ) { # Nog geen stem, geef 0 $procent[$j++]=0 } else { # Bereken stand bij goede stem ($procent[$j++], $dummy) = split(/\./, ($a/$totaal*100)*10**$decimalen+0.5); $procent[$j-1] = $procent[$j-1]/10**$decimalen; } } close (STAND); # Voeg IP toe if ( $meerdere_stemmen==0 ) { open (LOG, ">>log.txt"); print LOG "$wie\n"; close (LOG); } } else { # Bereken stand bij foute stem for ( $i=0;$i<=$#aantal;$i++ ) { ($procent[$i], $dummy) = split(/\./, ($aantal[$i]/$totaal*100)*10**$decimalen+0.5); $procent[$i] = $procent[$j-1]/10**$decimalen; } } } else { # Bereken stand bij geen stem for ( $i=0;$i<=$#aantal;$i++ ) { ($procent[$i], $dummy) = split(/\./, ($aantal[$i]/$totaal*100)*10**$decimalen+0.5); $procent[$i] = $procent[$j-1]/10**$decimalen; } } # Laat stand zien open (RETURN, "stand.html"); print ("Content-Type: text/html\n\n"); foreach $regel () { if ( $regel =~ /!stand(.+?)!/g ) { $nummer = $1; $tekst = "!stand$nummer!"; if ( $procent[$nummer] eq "" ) { $procent[$nummer] = 0; } $regel =~ s/$tekst/$procent[$nummer]/g; } $regel =~ s/!aantal!/$totaal/; print $regel; } close (RETURN);