<?php

    /*
        Vote

        Aktion: PHP Scripte für die armen dieser Welt
        Der Erlös geht für mein Pausenbrot drauf 

        Copyright (c) 2004 by Phillip 'Firebird' Berndt
    */
    
    $voteFrage = 'Wie heißt der Bürgermeister von Wesel?';
    
    $voteAntwort['Esel']         = 1;
    $voteAntwort['Heinz Müller'] = 5;
    $voteAntwort['Fritz']        = 1;
    
    // So, ab hier änderst du nix mehr *hau*
    if(!session_id())
        session_start();
        session_destroy();
    if(!isset($_SESSION['voted']) && isset($_POST['vote4']) && isset($voteAntwort[$_POST['vote4']]))
    {
        $thisFile         = file_get_contents(__FILE__);
        $newVoteCount     = $voteAntwort[$_POST['vote4']] + 1;
        $thisFile         = preg_replace('/\$voteAntwort\[\'('.$_POST['vote4'].')\'\](\s*)= (.*);/iU', '$voteAntwort[\'$1\']$2= '.$newVoteCount.';', $thisFile);

        fwrite(fopen(__FILE__, 'wb'), $thisFile);
        header('Location: '.$_SERVER['REQUEST_URI']);
        
        $_SESSION['voted'] = true;
        die();
    }
    
    echo('<form method="post" action="'.$_SERVER['REQUEST_URI'].'">');
    echo('<b>'.htmlentities($voteFrage).'</b><br />');
    
    $voteAntworten = array_sum($voteAntwort);
    
    foreach($voteAntwort as $antwort => $votes)
    {
        if($voteAntworten)
             $prozent = round($votes / $voteAntworten * 100, 2);
        else $prozent = 0;            
        $antwort = htmlentities($antwort);
        echo('<input type="radio" name="vote4" value="'.$antwort.'" /> '.$antwort.' ('.$prozent.' % der Stimmen)<br />');
    }
    
    echo('<input type="submit" action="action" value="Vote" /></form>');
    
?>
