<?php
// +------------------------------------------------------------------------+
// | PHP Melody version 1.6.7 ( www.phpsugar.com )
// +------------------------------------------------------------------------+
// | PHP Melody IS NOT FREE SOFTWARE
// | If you have downloaded this software from a website other
// | than www.phpsugar.com or if you have received
// | this software from someone who is not a representative of
// | phpSugar, you are involved in an illegal activity.
// | ---
// | In such case, please contact: support@phpsugar.com.
// +------------------------------------------------------------------------+
// | Developed by: phpSugar (www.phpsugar.com) / support@phpsugar.com
// | Copyright: (c) 2004-2011 PhpSugar.com. All rights reserved.
// +------------------------------------------------------------------------+

header("Cache-Control: no-cache");
header("Pragma: nocache");

@require('config.php');
@require_once('include/functions.php');
@require_once('include/user_functions.php');

$vote_sent 	= strip_tags($_REQUEST['j']);
$id_sent 	= strip_tags($_REQUEST['q']);
$ip_num 	= secure_sql($_SERVER['REMOTE_ADDR']);
$vote_sent 	= secure_sql($vote_sent);
$id_sent	= secure_sql($id_sent);

if( ! in_array($vote_sent, array(1,2,3,4,5)) || empty($ip_num) || empty($id_sent) || (ctype_alnum($id_sent) === FALSE))
{
	$new_back = 
	"<ul class=\"unit-rating\">\n".
	"<li class=\"current-rating\" style=\"width:". @number_format($current_rating/$count,2)*20 ."px;\">".$lang['rating_current'].".</li>\n".
	"<li class=\"r1-unit\">1</li>\n".
	"<li class=\"r2-unit\">2</li>\n".
	"<li class=\"r3-unit\">3</li>\n".
	"<li class=\"r4-unit\">4</li>\n".
	"<li class=\"r5-unit\">5</li>\n".
	"</ul>".
	"<span style=\"color:red; font-size: 9px;\">".$lang['rating_msg1']."</span>"; 
	
	$output = "unit_long$id_sent|$new_back";
	print $output;
	exit();
}

$video = request_video($id_sent);
if ( ! is_user_logged_in() && $video['restricted'] == '1')
{
	$new_back = 
	"<ul class=\"unit-rating\">\n".
	"<li class=\"current-rating\" style=\"width:". @number_format($current_rating/$count,2)*20 ."px;\">".$lang['rating_current'].".</li>\n".
	"<li class=\"r1-unit\">1</li>\n".
	"<li class=\"r2-unit\">2</li>\n".
	"<li class=\"r3-unit\">3</li>\n".
	"<li class=\"r4-unit\">4</li>\n".
	"<li class=\"r5-unit\">5</li>\n".
	"</ul>".
	"<span style=\"color:red; font-size: 9px;\">".$lang['rating_na']."</span>";
	
	$output = "unit_long$id_sent|$new_back";
	print $output;
	exit();
}

$sql = "SELECT total_votes, total_value, used_ips 
		FROM pm_ratings  
		WHERE id='".$id_sent."' ";
		
$query = @mysql_query($sql);
if(! $query)
{
	die('Error');
}

$numbers = mysql_fetch_assoc($query);
$checkIP = unserialize($numbers['used_ips']);
$count	 = $numbers['total_votes'];
$current_rating = $numbers['total_value'];
$sum	 = $vote_sent + $current_rating;
$tense	 = ($count == 1) ? 'vote' : 'votes';

mysql_free_result($query);

$sql = "SELECT used_ips 
		FROM pm_ratings 
		WHERE id = '". $id_sent ."'";
$result = @mysql_query($sql);
if( ! $result)
{
	die('Error');
}

$row = @mysql_fetch_assoc($result);
mysql_free_result($result);

$voted = 0;
if (count($row) > 0)
{
	if (strpos($row['used_ips'], $ip_num, 0) !== false)
	{
		$voted = 1;
	}
}

if($voted)
{
	$new_back = 
	"<ul class=\"unit-rating\">\n".
	"<li class=\"current-rating\" style=\"width:". @number_format($current_rating/$count,2)*20 ."px;\">".$lang['rating_current']."</li>\n".	"<li class=\"r1-unit\">1</li>\n".
	"<li class=\"r2-unit\">2</li>\n".
	"<li class=\"r3-unit\">3</li>\n".
	"<li class=\"r4-unit\">4</li>\n".
	"<li class=\"r5-unit\">5</li>\n".
	"</ul>".
	"<span style=\"color:red; font-size: 9px;\">".$lang['rating_msg1']."</span>";
}
else
{
	if($sum == 0)
	{
		$added = 0; 
	}
	else
	{
		$added = $count + 1; 
	}

	if(is_array($checkIP))
	{
		array_push($checkIP,$ip_num); 
	}
	else
	{
		$checkIP = array($ip_num); 
	}
	
	$insert = serialize($checkIP);
	
	$seekresult = @mysql_query("SELECT count(*) FROM pm_ratings WHERE id = '".$id_sent."'"); 
	$idexists 	= @mysql_result($seekresult, 0, 0); 

	if($idexists == true) 
	{
		$sql = "UPDATE pm_ratings 
				SET total_votes='".$added."', 
					total_value='".$sum."', 
					used_ips='".$insert."' 
				WHERE id = '".$id_sent."'";
		
		@mysql_query($sql);
	} 
	else 
	{
		$sql = "INSERT INTO pm_ratings 
				VALUES ('".$id_sent."', 0, 0, 0, '')";
		mysql_query($sql);

		$sql = "UPDATE pm_ratings 
				SET total_votes='".$added."', 
					total_value='".$sum."', 
					used_ips='".$insert."' 
				WHERE id = '".$id_sent."'";
		mysql_query($sql);
	}
	$sql = "SELECT total_votes, total_value, used_ips 
			FROM pm_ratings WHERE id='".$id_sent."'";
	
	$query = @mysql_query($sql);
	if( ! $query)
	{
		die('Error');
	}
	$numbers = mysql_fetch_assoc($query);
	$count	 = $numbers['total_votes'];
	$current_rating = $numbers['total_value'];

	$new_back = 
	"<ul class=\"unit-rating\">\n".
	"<li class=\"current-rating\" style=\"width:". @number_format($current_rating/$count,2)*20 ."px;\">".$lang['rating_current']."</li>\n".
	"<li class=\"r1-unit\">1</li>\n".
	"<li class=\"r2-unit\">2</li>\n".
	"<li class=\"r3-unit\">3</li>\n".
	"<li class=\"r4-unit\">4</li>\n".
	"<li class=\"r5-unit\">5</li>\n".
	"</ul>".
	"<span style=\"font-size: 9px;\">".$lang['rating_msg2']."</span>";
}

$output = "unit_long$id_sent|$new_back";
print $output;
exit();
?>