Scripts

Poll zonder MySQL - Class

Naar aanleiding van de andere 'poll zonder MySQL', heb ik een versie gemaakt die je als class kan invoegen. In het bestand 'class.php' staat de class zelf. In het bestand 'index.php' staat een voorbeeld van het gebruik (wat overigens voor zich spreekt). Het bestand 'poll_votes.php' is een leeg bestand, waarin de informatie wordt opgeslagen. Dit bestand moet in dezelfde map staan als 'index.php'.

poll-zonder-mysql-class
class.php
[code]
<?php

class Poll {
	var $question = '';
	var $answers = array();
	var $name = '';
	var $poll_votes = array();
	
	function __construct() {
		$this->setPollVotes();
	}
	
	protected function setPollVotes() {
		$poll_votes = array();
		
		include('poll_votes.php');
		
		$this->poll_votes = $poll_votes;
	}
	
	protected function getPollVotes() {
		return $this->poll_votes;
	}
	
	public function setPollName($name) {
		$this->name = htmlentities($name);
	}
	
	public function getPollName() {
		return $this->name;
	}
	
	public function resetPollName() {
		$this->name = '';
	}
	
	public function setQuestion($question) {
		$this->question = $question;
	}
	
	public function getQuestion() {
		return $this->question;
	}
	
	public function resetQuestion() {
		$this->question = '';
	}
	
	public function addAnswer($answer) {
		if(is_array($answer)){
			foreach($answer as $key => $value){
				$this->answers[] = $value;
			}
		}else{
			$this->answers[] = $answer;
		}
	}
	
	public function getAnswers() {
		return $this->answers;
	}
	
	public function resetAnswers() {
		$this->answers = array();
	}
	
	public function checkIp($ip) {
		$poll_votes = $this->getPollVotes();
		
		$pollName = $this->getPollName();
		
		foreach($poll_votes as $key => $value){
			if((!empty($pollName) && $poll_votes[$key]['pollName'] == $pollName && $poll_votes[$key]['ip'] == md5($ip)) ||
				(empty($pollName) && $poll_votes[$key]['ip'] == md5($ip)) ||
				($poll_votes[$key]['pollName'] == 'BAN' && $poll_votes[$key]['ip'] == md5($ip))){
				return false;
			}
		}
		
		return true;
	}
	
	protected function addIp($ip) {
		if(!is_writable('poll_votes.php')){
			return false;
		}
		
		$file = fopen('poll_votes.php', 'rb');
		$content = str_replace('<?php ', '', fgets($file));
		$content = str_replace(' ?>', '', $content);
		fclose($file);
		
		$file = fopen('poll_votes.php', 'w+b');
		
		$new_content = '$poll_votes[] = array(\'pollName\' => \'' . $this->getPollName() . '\', \'ip\' => \'' . md5($ip) . '\');';
		
		if(!fwrite($file, '<?php ' . $content . $new_content . ' ?>')){
			fclose($file);
			
			return false;
		}
		
		fclose($file);
		
		return true;
	}
	
	public function banIp($ip) {
		if(!is_writable('poll_votes.php')){
			return false;
		}
		
		$file = fopen('poll_votes.php', 'rb');
		$content = str_replace('<?php ', '', fgets($file));
		$content = str_replace(' ?>', '', $content);
		fclose($file);
		
		$file = fopen('poll_votes.php', 'w+b');
		
		$new_content = '$poll_votes[] = array(\'pollName\' => \'BAN\', \'ip\' => \'' . md5($ip) . '\');';
		
		if(!fwrite($file, '<?php ' . $content . $new_content . ' ?>')){
			fclose($file);
			
			return false;
		}
		
		fclose($file);
		
		return true;
	}
	
	public function addVote($answer) {
		if(!$this->checkIp($_SERVER['REMOTE_ADDR'])){
			return false;
		}
		
		if(!is_numeric($answer)){
			return false;
		}
		
		if(!is_writable('poll_votes.php')){
			return false;
		}
		
		$file = fopen('poll_votes.php', 'rb');
		$content = str_replace('<?php ', '', fgets($file));
		$content = str_replace(' ?>', '', $content);
		fclose($file);
		
		$file = fopen('poll_votes.php', 'w+b');
		
		$new_content = ' $poll_votes[] = array(\'pollName\' => \'' . $this->getPollName() . '\', \'ip\' => \'' . md5($_SERVER['REMOTE_ADDR']) . '\', \'answer\' => \'' . $answer . '\');';
		
		if(!fwrite($file, '<?php ' . $content . $new_content . ' ?>')){
			fclose($file);
			
			return false;
		}
		
		fclose($file);
		
		return true;
	}
	
	public function getVotes() {
		$answers = $this->getAnswers();
		$total = 0;
		
		foreach($answers as $key => $value){
			$votes[$key]['count'] = 0;
			$votes[$key]['answer'] = $value;
		}
		
		$this->setPollVotes();
		
		$poll_votes = $this->getPollVotes();
		
		foreach($poll_votes as $key => $value){
			if($poll_votes[$key]['pollName'] == $this->getPollName()){
				$votes[$poll_votes[$key]['answer']]['count']++;
				$total++;
			}
		}
		
		foreach($answers as $key => $value){
			if($total == 0){
				$votes[$key]['percentage'] = '0';
			}else{
				$votes[$key]['percentage'] = round($votes[$key]['count'] / $total * 100, 1);
			}
		}
		
		return $votes;
	}
}

?>
[/code]

index.php
[code]
<?php

require_once('class.php');

$fPoll = new Poll;

$fPoll->setPollName('Testpoll');

$fPoll->setQuestion('Wat vind je van deze poll?');

$fPoll->addAnswer('Zeer goed');
$fPoll->addAnswer('Goed');
$fPoll->addAnswer('Matig');
$fPoll->addAnswer('Slecht');
$fPoll->addAnswer('Zeer Slecht');

if($fPoll->checkIp($_SERVER['REMOTE_ADDR'])){
	if($_SERVER['REQUEST_METHOD'] == 'POST'){
		if(!$fPoll->addVote($_POST[$fPoll->getPollName()])){
			echo 'Uw antwoord kon niet worden toegevoegd.';
		}else{
			echo 'Uw antwoord is succesvol toegevoegd.<br><br>';
			
			echo $fPoll->getQuestion() . '<br><br>';
			
			$votes = $fPoll->getVotes();
			
			foreach($votes as $key => $key){
				echo $votes[$key]['answer'] . ' - ' . $votes[$key]['count'] . ' (' . $votes[$key]['percentage'] . ' %)<br>';
			}
		}
	}else{
		echo $fPoll->getQuestion() . '<br><br>';
		
		echo '<FORM ACTION="" METHOD="POST">';
		 
		 $answers = $fPoll->getAnswers();
		 
		 foreach($answers as $key => $value){
		 	echo '<INPUT TYPE="radio" NAME="' . $fPoll->getPollName() . '" VALUE="' . $key . '"> ' . $value . '<br>';
		 }
		 
		 echo '<br><INPUT TYPE="submit" VALUE="Vote!" NAME="submit">';
		 
		echo '</FORM>';
	}
}else{
	echo $fPoll->getQuestion() . '<br><br>';
	
	$votes = $fPoll->getVotes();
	
	foreach($votes as $key => $value){
		echo $votes[$key]['answer'] . ' - ' . $votes[$key]['count'] . ' (' . $votes[$key]['percentage'] . ' %)<br>';
	}
}

?>
[/code]

Reacties

0
Nog geen reacties.