<?php
if (isset($function_vote_already_included)) return;
$function_vote_already_included = 1;

function vote_check_ip($id=0, $ip="", $table="exit_pool_stat") {
	global $db, $SERVER_ADDR, $REMOTE_ADDR;
	$t=mktime(0, 0, 0, date("m"), date("d"), date("Y"));
	$ip=(!$ip ? "$SERVER_ADDR:$REMOTE_ADDR" : $ip);
	return is_val($table, "where ip='$ip' and date_created>=$t and pool_id='$id'");
}

function vote_save($id=0, $table="exit_pool_stat", $table_v="exit_pool_variant") {
	global $db, $SERVER_ADDR, $REMOTE_ADDR;
	$ip="$SERVER_ADDR:$REMOTE_ADDR";
	$pool_id=get_val($table_v, "p_id", "where id='$id'");
	if (!vote_check_ip($pool_id, $ip, $table) and is_val($table_v, "where id='$id'")) {
		$data=array(
			"date_created"	=> time(),
			"pool_id"	=> $pool_id,
			"ip"		=> $ip,
		);
		$fields="date_created, pool_id, ip";
		$sql=array();
		$sql[]=get_sql_insert($table, $fields, $data);
		$sql[]="update $table_v set count=count+1 where id='$id'";
		$db->execute_all($sql);
	}
}

function vote_delete_old($table="exit_pool_stat") {
	global $db;
	$t=mktime(0, 0, 0, date("m"), date("d"), date("Y"));
	$sql="delete from $table where date_created<$t";
	$db->execute($sql);
}
?>