<?php
include '../env.php';
require_once(LIB.'common.inc.php');
require_once(LIB.'Post.class.php');
$post = new Post();

if (! can_action('view') || !is_login()) {
	_a_log_fail('f-manage', 'del', 'invalid auth');
	echo json_encode(FALSE);
	die;
}

$log_ret = _a_log2('f-manage', 'del', array(
	'detail' => serialize($_POST),
	'target_id' => @$_POST['id']
));

if ($log_ret) {
	$favo = $post->readFavoriteItemById($_SESSION['user']['id'], @$_POST['id']);
	if (empty($favo)) {
		$ret = TRUE;
	} else {
		$ret = $post->deleteFavoriteItem($favo['id']);
	}
	_a_log_write($log_ret, array('success' => $ret === FALSE ? '0' : '1'));
	if ($ret === FALSE) {
		echo json_encode($ret);
	} else {
		echo json_encode(array(
			'favorited' => FALSE
		));
	}
} else {
	echo json_encode(FALSE);
}
?>