<?php



function create_poll($question,$answers){


  if (!file_exists('content/polls')){
    if (!my_mkdir('content/polls')){
      return false;
    }
  }

  $nr = 0;
  while (file_exists('content/polls/question_'.$nr)){
    $nr++;
  }

  if (file_exists('content/polls/question')){
    rename('content/polls/question','content/polls/question_'.$nr);
  }
  if (file_exists('content/polls/votes')){
    rename('content/polls/votes','content/polls/votes_'.$nr);
  }
  if (file_exists('content/polls/result')){
    rename('content/polls/result','content/polls/result_'.$nr);
  }
  if (file_exists('content/polls/time')){
    rename('content/polls/time','content/polls/time_'.$nr);
  }
  if (file_exists('content/polls/remote')){
    rename('content/polls/remote','content/polls/remote_'.$nr);
  }

  $question=str_replace("\n",' ',$question);

  if ($fh = fopen('content/polls/question', 'w')){
    fwrite($fh, $question."\n");
    foreach ($answers as $answer){
      $answer=str_replace("\n",' ',$answer);
      fwrite($fh, $answer."\n");
    }
    fclose($fh);
    if ($fh = fopen('content/polls/time', 'w')){
	$time = time();
	fwrite($fh,'start: '.$time."\n");
	fclose($fh);
    }
    return true;
  }
  return false;
}


function read_poll(){
  return @file('content/polls/question');
}


function add_vote($answer){
  if (isset($answer)){
      $remote = $_SERVER['REMOTE_ADDR'];
      if (!remote_addr_exists('content/polls/remote',$remote)){
	  add_remote_addr('content/polls/remote',$remote);

	  if ($fh = fopen('content/polls/votes', 'a')){
	      fwrite($fh, $answer."\n");
	      fclose($fh);
	      if ($fh = fopen('content/polls/time', 'a')){
		  $time = time();
		  fwrite($fh,$time."\n");
		  fclose($fh);
	      }
	      return true;
	  }
      }
  }
  return false;
}


function remote_addr_exists($file,$remote){
    $addr = @file($file);
    if (is_array($addr)){
	foreach ($addr as $a){
	    $a = rtrim($a);
	    if ($a == $remote){return true;}
	}
	return in_array($remote,$addr);
    }
    return false;
}


function add_remote_addr($file,$remote){
    if (!remote_addr_exists($file,$remote)){
	if ($fh = fopen($file, 'a')){
	    fwrite($fh, $remote."\n");
	    fclose($fh);
	}
    }	
}


function poll_result(&$poll,$nr=null){
    if ($nr != null){
	if (file_exists('content/polls/result_'.$nr)){
	    $poll=file('content/polls/result_'.$nr);
	    return true;
	}
	return false;
    }
  //echo "get poll result!!!!<br>";
  if (@filemtime('content/polls/result')>
      @filemtime('content/polls/votes')){
      $poll=file('content/polls/result');
      return true;
  }
  else{
    if (make_poll_result()){
      $poll=file('content/polls/result');
      //      poll_result($poll);
      return true;
    }
  }
  return false;
}


function make_poll_result(){
  // echo "make poll result!!!!<br>";
  if ($poll = read_poll()){
    $question = array_shift($poll);
  }
  if (!is_array($poll)){
      return false;
  }
  $total = 0;
  $votes = array();
  foreach ($poll as $a){
    $votes[$a] = 0;
  }
  $answers = @file('content/polls/votes');
  if (is_array($answers)){
    foreach ($answers as $answer){
      // echo "$answer--<br>";
      $answer=rtrim($answer);
      $votes[$poll[$answer]]++;
      $total++;
    }
  }
  if ($fh = @fopen('content/polls/result', 'w')){
    fwrite($fh,$question);
    fwrite($fh,$total."\n");
    foreach ($votes as $a => $v){
	$pct=0;
	if ($total>0){
	    $pct = round($v/$total*100);
	}
	fwrite($fh,rtrim($a).': '.$v.' ('.$pct."%)\n");
/*
	$width=round($pct/3);
	fwrite($fh,"<td width='$width' bgcolor='#000000'></td>");
	fwrite($fh,'<td>'.rtrim($a).': '.$v.' ('.$pct."%)</td>\n");
*/
    }
    fclose($fh);
    // echo "OK!!!!<br>";
    return true;
  }
  return false;
}



?>