<?php
/*******ajax函数*********/
function user_add_fav(){
	if(  is_user_logged_in() ){
	if( isset($_POST['action']) ){
		if( $_POST['action'] == 'add_to_fav' ){
			global $wpdb, $current_user;
			get_currentuserinfo();
			$uid = $current_user->ID;
			if(isset($_POST['fav'])){
				$post=$_POST['fav'];
				add_fav($post,$uid);
			}else{
				echo 'n';
			}
		}else{
			echo 'n';
		}
	}
	}else{
		echo 'l';
	}
	die;
}
add_action('wp_ajax_nopriv_add_to_fav', 'user_add_fav');
add_action("wp_ajax_add_to_fav", "user_add_fav");

add_action("wp_ajax_vote_post", "add_votes_options");
add_action("wp_ajax_nopriv_vote_post", "add_votes_options");
function add_votes_options() {

if( isset($_POST['action']) && ($_POST['action'] == 'vote_post') ){

	$postid = (int)$_POST['postid'];
	if( !$postid ){
		echo 'e';
		die(0);
	}
	$voted = $_COOKIE["smzdm_voted_".$postid];
	if( $voted ){
		echo 'h';
		die(0);
	}
	$ip = $_SERVER['REMOTE_ADDR'];
	$rating = $_POST['rating'];
	if(  is_user_logged_in() ){
		global $wpdb, $current_user;
		get_currentuserinfo();
		$uid = $current_user->ID;
	}else{
		$uid='';
	}
	if($rating=='up'){
		$rating='up';
	}else{
		$rating='down';
	}
	
	$voted = add_vote($postid,$uid,$ip,$rating);
	if($voted=='y'){
		setcookie("smzdm_voted_" . $postid,$rating, time() + 3000000, '/');
		echo 'y';
		die(0);
	}
	if($voted=='h'){
		setcookie("smzdm_voted_" . $postid,$rating, time() + 3000000, '/');
		echo 'h';
		die(0);
	}
	if($voted=='e'){
		echo 'n';
		die(0);
	}
}else{
	echo 'e';
}
die(0);
}
?>