<?php
// The source code packaged with this file is Free Software, Copyright (C) 2005 by
// Ricardo Galli <gallir at uib dot es>.
// It's licensed under the AFFERO GENERAL PUBLIC LICENSE unless stated otherwise.
// You can get copies of the licenses here:
// 		http://www.affero.org/oagpl.html
// AFFERO GENERAL PUBLIC LICENSE is also included in the file called "COPYING".

if(is_numeric($_POST['id'])){

	include_once('Smarty.class.php');
	$main_smarty = new Smarty;

	include('config.php');
	include(mnminclude.'link.php');
	include(mnminclude.'html1.php');
	include(mnminclude.'smartyvariables.php');
	
	$link = new Link;
	$link->id=$_POST['id'];
	$link->read_basic();
	
	if ($current_user->user_id == 0 && !anonymous_vote) {
	error($main_smarty->get_config_vars('PLIGG_Visual_Vote_NoAnon'));
	}

	if($current_user->user_id != $_POST['user']) {
	error($main_smarty->get_config_vars('PLIGG_Visual_Vote_BadUser'). $current_user->user_id . '-'. $_POST['user']);
	}

	$md5=md5($_POST['user'].$link->randkey);
	if($md5 !== $_POST['md5']){
	error($main_smarty->get_config_vars('PLIGG_Visual_Vote_BadKey'));
	}

	if($link->votes($current_user->user_id) > 0 || $link->reports($current_user->user_id) > 0) {
	// for some reason this needs to be here
	// i'll figure it out later
	// unless i forget
	// i'll probably forget
	// so if you read this, remind me
	// mthxbye
	// - ash
	}

	$value = $_POST['value'];
	if($_POST['unvote'] == 'true'){
	$link->remove_vote($current_user->user_id, $value);
	} else {
	if($link->votes($current_user->user_id) > 0 || $link->reports($current_user->user_id) > 0) {
		error($main_smarty->get_config_vars('PLIGG_Visual_Vote_AlreadyVoted'));
	}
	$link->insert_vote($current_user->user_id, $value);
	}

	// TODO

	if ($link->status == 'discard') {
	$link->read();
	$link->status = 'queued';
	$link->store();
	}

	if(Voting_Method == 1){
	$count=$link->votes;
	echo "$count ~--~".$_POST['id'];
	}

	if(Voting_Method == 2){
	
	$link_rating = $link->rating($link->id)/2;
	$rating_width = $link_rating * 25;
	$vote_count = $link->countvotes();
	echo $rating_width . "~" . $link_rating . "~" . $vote_count . "~<li class='one-star-noh'>1</li><li class='two-stars-noh'>2</li><li class='three-stars-noh'>3</li><li class='four-stars-noh'>4</li><li class='five-stars-noh'>5</li>";
	
	
	}

	$link->evaluate_formulas();

}

function error($mess) {
	header('Content-Type: text/plain; charset=UTF-8');
	echo "ERROR: $mess";
	die;
}
?>