<?php
/************************************************************************/
/* Maximus CMS: Web Portal System                                       */
/* ==============================                                       */
/*                                                                      */
/* Copyright (c) 2002 - 2006                                            */
/* http://www.php-maximus.fr                                            */
/* dev@php-maximus.org                                                  */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/
/* Based on PHP-NUKE: Web Portal System                                 */
/************************************************************************/

	$passtest = "yes";
	incoh();
	include("modules/$module_name/l_config.php");
	$ratinglid = $db->XMFnum($ratinglid);
	completevoteheader();
	if(defined('USER_ALLOW')) {
		$user2 = base64_decode($user);
		$user2 = addslashes($user2);
		$cookie = explode(":", $user2);
		cookiedecode($user);
		$ratinguser = $cookie[1];
	} else if ($ratinguser=="outside") {
		$ratinguser = "outside";
	} else {
		$ratinguser = "$anonymous";
	}
	$result = $db->sql_query("SELECT title FROM ".TABLE_LINKS_LINKS." WHERE lid='$ratinglid'");
	while ($row = $db->sql_fetchrow($result)) {
		$title = stripslashes(check_html($row['title'], "nohtml"));
		$ttitle = $title;
		/* Make sure only 1 anonymous from an IP in a single day. */
		$ip = $_SERVER["REMOTE_HOST"];
		if (empty($ip)) {
			$ip = $_SERVER["REMOTE_ADDR"];
		}
		/* Check if Rating is Null */
		if ($rating=="--") {
			$error = "nullerror";
			completevote($error);
			$passtest = "no";
		}
		/* Check if Link POSTER is voting (UNLESS Anonymous users allowed to post) */
		if ($ratinguser != $anonymous && $ratinguser != "outside") {
			$result2 = $db->sql_query("SELECT submitter from ".TABLE_LINKS_LINKS." where lid='$ratinglid'");
			while ($row2 = $db->sql_fetchrow($result2)) {
				$ratinguserDB = $row2['submitter'];
				if ($ratinguserDB==$ratinguser) {
					$error = "postervote";
					completevote($error);
					$passtest = "no";
				}
			}
		}
		/* Check if REG user is trying to vote twice. */
		if ($ratinguser!=$anonymous && $ratinguser != "outside") {
			$result3 = $db->sql_query("SELECT ratinguser from ".TABLE_LINKS_VOTEDATA." where ratinglid='$ratinglid'");
			while ($row3 = $db->sql_fetchrow($result3)) {
				$ratinguserDB = $row3['ratinguser'];
				if ($ratinguserDB==$ratinguser) {
					$error = "regflood";
					completevote($error);
					$passtest = "no";
				}
			}
		}
		/* Check if ANONYMOUS user is trying to vote more than once per day. */
		if ($ratinguser==$anonymous){
			$yesterdaytimestamp = (time()-(86400 * $anonwaitdays));
			$ytsDB = Date("Y-m-d H:i:s", $yesterdaytimestamp);
			$result4 = $db->sql_query("SELECT * FROM ".TABLE_LINKS_VOTEDATA." WHERE ratinglid='$ratinglid' AND ratinguser='$anonymous' AND ratinghostname = '$ip' AND TO_DAYS(NOW()) - TO_DAYS(ratingtimestamp) < '$anonwaitdays'");
			$anonvotecount = $db->sql_numrows($result4);
			if ($anonvotecount >= 1) {
				$error = "anonflood";
				completevote($error);
				$passtest = "no";
			}
		}
		/* Check if OUTSIDE user is trying to vote more than once per day. */
		if ($ratinguser=="outside"){
			$yesterdaytimestamp = (time()-(86400 * $outsidewaitdays));
			$ytsDB = Date("Y-m-d H:i:s", $yesterdaytimestamp);
			$result5 = $db->sql_query("SELECT * FROM ".TABLE_LINKS_VOTEDATA." WHERE ratinglid='$ratinglid' AND ratinguser='outside' AND ratinghostname = '$ip' AND TO_DAYS(NOW()) - TO_DAYS(ratingtimestamp) < '$outsidewaitdays'");
			$outsidevotecount = $db->sql_numrows($result5);
			if ($outsidevotecount >= 1) {
				$error = "outsideflood";
				completevote($error);
				$passtest = "no";
			}
		}
		/* Passed Tests */
		if ($passtest == "yes") {
			$comment = stripslashes($comment);
			if ($comment != "") {
				//update_points(16);
			}
			//update_points(15);
			/* All is well.  Add to Line Item Rate to DB. */
			$ratinglid = $db->XMFnum($ratinglid);
			$rating = $db->XMFnum($rating);
			$db->sql_query("INSERT into ".TABLE_LINKS_VOTEDATA." values (NULL,'$ratinglid', '$ratinguser', '$rating', '$ip', '$ratingcomments', now())");
			/* All is well.  Calculate Score & Add to Summary (for quick retrieval & sorting) to DB. */
			/* NOTE: If weight is modified, ALL links need to be refreshed with new weight. */
			/*	 Running a SQL statement with your modded calc for ALL links will accomplish this. */
			$voteresult = $db->sql_query("SELECT rating, ratinguser, ratingcomments FROM ".TABLE_LINKS_VOTEDATA." WHERE ratinglid = '$ratinglid'");
			$totalvotesDB = $db->sql_numrows($voteresult);
			include ("modules/$module_name/voteinclude.php");
			$lid = $db->XMFnum($lid);
			$db->sql_query("UPDATE ".TABLE_LINKS_LINKS." SET linkratingsummary='$finalrating',totalvotes='$totalvotesDB',totalcomments='$truecomments' WHERE lid = '$ratinglid'");
			$error = "none";
			completevote($error);
		}
	}
	completevotefooter($ratinglid, $ttitle, $ratinguser);
	incof();