<?php
include("settings.inc.php");
if (isset($mode)) {
   switch ($mode) {
       // If user voted, process their vote:
       case "vote":
          if (isset($vote)) {
             $vote_allow = 1;
             $file_contents = file($current_poll);
             if (((isset($vote_logging))) && (($vote_logging == 1) || ($vote_logging == 3))) {
                foreach ($file_contents as $line) {
                        if (eregi($REMOTE_ADDR, $line)) {
                           echo $already_voted;
                           display_form();
                           $set_already_voted = 1;
                           $vote_allow = 0;
                           break;
                        }
                }
             }
             if (((isset($vote_logging))) && (($vote_logging == 2) || ($vote_logging == 3))) {
                if ((isset($voted)) && ($voted == "on")) {
                   if (!$set_already_voted) {
                      echo $already_voted;
                      display_form();
                   }
                   $vote_allow = 0;
                   break;
                }
             }
             if ($vote_allow) {
                $poll_string = $vote . "|" . $REMOTE_ADDR . "\n";
                $fp = fopen($current_poll, "a");
                $string_size = strlen($poll_string);
                if (fputs($fp, $poll_string, $string_size)) {
                   if (((isset($vote_logging))) && (($vote_logging == 2) || ($vote_logging == 3))) {
                      setcookie ("voted", "on", time()+$time_toban);
                   }
                   echo $vote_cast . "<br>";
                   display_form();
                }
                fclose($fp);
             }
          }
          break;
       // If user asked to show results, display results:
       case "results":
          $poll_contents = file($current_poll);
          // How many total votes we have;
          $total_votes = sizeof($poll_contents);
          $answers = file($config);
          if ($total_votes == 0) {
            echo $no_votes_yet;
            display_form();
          }
          else {
              echo "Ratings:<br>";
              $size_answers = sizeof($answers) - 1;
              $votes[$size_answers] = 0;
              for ($current_number = 1; $current_number <= $size_answers; $current_number++) {
                  foreach ($poll_contents as $line) {
                          $line_parts = explode("|", $line);
                          if ($line_parts[0] == $current_number) {
                             $votes[$current_number]++;
                          }
                  }
                  $percentage = $votes[$current_number]/$total_votes*100;
                  $graph_width = 2 * $votes[$current_number];
                  echo "<img src=tick.gif height=10 width=" . $graph_width . ">&nbsp;&nbsp;&nbsp;" . round($percentage, 2) . " %&nbsp;&nbsp;&nbsp;" . $answers[$current_number] . ": " . $votes[$current_number]/1 . " votes.<br>";
              }
          echo "Total votes: " . $total_votes;
          }
          break;

       case "previous":
            $file_contents = file($archive_poll);
            if (sizeof($file_contents) == 0) {
               echo $no_previous;
               display_form();
            }
            else {
                 for ($i=0;$i<sizeof($file_contents);$i++) {
                     $each_poll = explode("|", $file_contents[$i]);
                     echo $each_poll[0] . "<br>" . $each_poll[1] . "<hr>";
                 }
            }
            break;

       // If no proper mode selected, display voting form;
       default: display_form(); break;

   }
}
// If no mode selected, display voting form;
if (!isset($mode)) {
   display_form();
}

function display_form() {
         global $config;
         global $no_voting;
         global $PHP_SELF;
         echo "<form name=poll action=" . $PHP_SELF . " method=post>";
         $answers = file($config);
         if (sizeof($answers) == 0) {
            echo $no_voting;
         }
         else {
              $current_number = 1;
              $size_answers = sizeof($answers) - 1;
              // Display question;
              echo $answers[0] . "<br>";
              // Display answers;
              for ($current_number = 1; $current_number <= $size_answers; $current_number++) {
                  echo "<input type=radio name=vote value=" . $current_number . ">" . $answers[$current_number] . "<br>";
              }
              echo "<input type=hidden name=mode value=vote>";
              echo "<br><input type=submit value=vote>";
              echo "</form>";
              echo "<a href=" . $PHP_SELF . "?mode=results>Risultati</a>";
              echo "&nbsp;&nbsp;";
             /* echo "<a href=" . $PHP_SELF . "?mode=previous>Previous polls</a>"; */
         }
}
?>

