<?php /**/ ?><?php // Created by Agilo (agilo3@gmail.com).

 // Poll
 
$file       "poll_file";                   // The file where the votes will be kept.  Make sure that it is CHMOD'ed to 777!
 
$question   "What's the prettiest color?"// The poll question.
 
$answers[0] = "Yellow";                      // The poll answers.
 
$answers[1] = "Blue";
 
$answers[2] = "Green";
 
$answers[3] = "Red";
 
$answers[4] = "Black";
 
$answers[5] = "White";
 
$answers[6] = "Orange";
 
$answers[7] = "Pink";
 
$answers[8] = "Other..";

 
// Admin (IMPORTANT: see the admin.php file!)
 
$show_admin_link 1;           // Show the admin link?  0 = no, 1 = yes.
 
$admin           "admin.php"// The admin file.



 // Do not edit below unless you know what you're doing!
 // ----------------------------------------------------
 
class poll {
  function 
check() { // Check if already voted.
   
global $file;

   if (
file_exists($file)) {
    
$thefile file($file);
    foreach(
$thefile as $line) {
     
$val explode("|"$line);
     
$ip $_SERVER['REMOTE_ADDR'];
     if (
preg_match("/$ip/"$val[1])) return 1;
    }
   }
  }

  function 
save() { // Validate for saving.
   
echo "Saving.. \n";

   if (
poll::check()) {
    echo 
"failed!<br /><br /><br /><b>You have already voted!</b>";
   } else {
    
poll::saving();
   }
  }

  function 
saving() { // Actual save.
   
global $file;

   
$vote preg_replace("/answ/"""$_POST['answer']);
   if (
is_numeric($vote)) {
    if (!
file_exists($file)) {
     if(!@
touch($file)) echo "  <br />Unable to create file: $file<br />\n";
     if(!@
chmod($file0777)) echo "  Unable to chmod file: $file<br />\n";
    }
    
$poll_file = @fopen($file"a+");
    
$data "\n".$vote."|".$_SERVER['REMOTE_ADDR'];
    if(!@
fwrite($poll_file$data)) {
     echo 
"  <br /><strong>Error while saving!</strong><br />Unable to write to $file<br />\n";
     
$ret 1;
    };
    @
fclose($poll_file);

    if (empty(
$ret)) echo "    <br /><br /><strong>Vote saved!</strong><br /><br /><br />\n".
                          
'    <br /><br /><br /><a href="?results">View results</a>'."\n";
   } else {
    
$sec_log "security_log";
    echo 
"    <strong>failed!</strong><br /><br />Security error!<br /><br /><br />IP address (".
         
$_SERVER['REMOTE_ADDR'].") <a href=\"$sec_log\">logged</a>.<br />\n";

    if (!
file_exists($sec_log)) {
     if(!@
touch($sec_log)) echo "  Unable to create file: $sec_log<br />\n";
     if(!@
chmod($sec_log0777)) echo "  Unable to chmod file: $sec_log<br />\n";
    }
    
$security_file = @fopen($sec_log"a+");
    
$error_log "\n".date("d-m-Y h:iA")."  -  Security warning: ".$_SERVER['REMOTE_ADDR'].
                 
" tried to by-pass security while saving a vote!";
    if(!@
fwrite($security_file$error_log)) {
     echo 
"  <br /><strong>Error!</strong><br />Unable to write to $file<br />\n";
    }
    @
fclose($security_file);
   }
  }

  function 
output() { // Spew out the poll.
   
global $question$answers$show_admin_link$admin;

   if (
poll::check()){
    
poll::results();
   } else {
    if (!empty(
$question)){
     if (empty(
$_POST['answer'])){
      echo 
"<form action=\".\" method=\"post\">\n   <div id=\"poll\">";
      echo 
"<h2>$question</h2>\n    <div id=\"answers\">\n";
      for (
$i=0$i<count($answers); $i++) {
       echo 
'     <input type="radio" name="answer" id="id'.$i.'" value="answ'.$i.'" />'.
            
' <label for="id'.$i.'">'.$answers[$i]."</label><br />\n";
      }
       echo 
"    </div>\n    <br />\n".
            
'    <input type="submit" name="submit" value="-- Vote! --" /> '."\n".
            
"   </div>\n  </form>\n".
            
'  <br /><br /><br /><a href="?results">View results</a>';
       if (!empty(
$show_admin_link)) echo ' | <a href="'.$admin.'">Admin</a>';
       echo 
"\n";
     } else { 
poll::save(); }
    } else { echo 
'    <strong>Error!</strong> &nbsp;No poll question!'; }
   }
  }

  function 
results() { // Calculate and show results.
   
global $file$question$answers;
   
$poll_vote = Array();

   if (
file_exists($file)) {
    
$votes file($file);
    foreach (
$votes as $thevote) {
     
$vote explode("|"$thevote);
     if (isset(
$vote[1])) $poll_vote[] = Array("0"=>$vote[0],"1"=>$vote[1]);
    }
    
$tv count($poll_vote);
    if (
$tv==0) echo "<b>No votes have been made yet!</b><br /><br />\n";

    
// This method is very slow and crude, but affective.  I will accept corrections or improvements to this line:
    
for ($i=0$i<$tv$i++) for ($j=0$j<count($answers); $j++) if (isset($poll_vote[$i][0]) && $poll_vote[$i][0] == $j$answ[$j]++;
   } else {
    echo 
"<b>No votes have been made yet!</b><br /><br />\n";
   }
   echo 
"<h2>$question</h2>\n";
   echo 
"  <table border=\"0\" cellspacing=\"0\" width=\"600\" summary=\"This table shows the votes that have been made.\">\n";
   for (
$i=0$i<count($answers); $i++) {
    if (
$answers[$i]) $percent = @round(number_format((($answ[$i]*100)/$tv),2));
    if (empty(
$percent)) $percent 0;
    if (
$answ[$i]<1$vote_txt "0 votes"; elseif($answ[$i]==1$vote_txt " vote"; else $vote_txt " votes";
    
$total += $answ[$i];
    echo 
'   <tr><td class="left">'.$answers[$i]."</td><td class=\"right\">".$percent." %</td><td class=\"votes\">".$answ[$i].
         
$vote_txt."</td><td class=\"votes\"><img src=\"bar.gif\" alt=\"\" height=\"10\" width=\"".round($percent/2)."\" /></td></tr>\n";
   }
   echo 
"  </table>\n  <br /><small>Total votes: ".$total."</small>\n";
  }
 }



 
// Rough fallback method in XHTML 1.0 Strict.  Can be adjusted:
 
if (!preg_match("/index|admin|main/"$_SERVER['PHP_SELF'])) {
  if (
stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml")) {
   
header("Content-type: application/xhtml+xml");
   echo 
"<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">\n";
  } else {
   
header("Content-type: text/html");
   echo 
"<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">\n";
  }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
<head>
<title> Poll: error </title>
<meta name="language" content="English" />
<meta name="author"   content="Agilo" />
</head>

<body>

<div>
 <strong>Error!</strong>&nbsp; File called directly!<br /><br /><br />

 You must <strong>include</strong> this file and call the output function.<br /><br />

 Example:<br />
 <pre style="width: 23em; background-color: #DDDDDD">
  <code>
   &lt;?php
    include("<?= $_SERVER['PHP_SELF'?>");
    if (isset($_GET['results'])) {
     poll::results();
    } else {
     poll::output();
    }
   ?&gt;
  </code>
 </pre>
</div>

</body>
</html>
<?php
 
}
?>