<?php
include '../env.php';
require_once(LIB.'common.inc.php');
require_once(LIB.'Post.class.php');
$post = new Post();
if (! empty($_POST['id'])) {
	$log_ret = _a_log('theme', 'modify', array(
		'target_key' => $_POST['id'],
		'detail' => serialize($_POST)
	));
	if (can_action('edit_theme') && $log_ret) {
		$ret = $post->updateTheme(
			$_POST['id'],
			@$_POST['name'],
			@$_POST['description'],
			@$_POST['key1'],
			@$_POST['key2'],
			@$_POST['key3']
		);
		_a_log_write($log_ret, array('success' => $ret === FALSE ? '0' : '1'));
	} else {
		_a_log_fail('theme', 'modify', 'invalid auth', array(
			'target_key' => $_POST['id']
		));
		$ret = FALSE;
	}
} else {
	$log_ret = _a_log('theme', 'create', array(
		'target_key' => @$_POST['name'],
		'detail' => serialize($_POST)
	));
	if (can_action('create_theme') && $log_ret) {
		$ret = $post->createTheme(
			@$_POST['name'],
			@$_POST['description'],
			@$_POST['key1'],
			@$_POST['key2'],
			@$_POST['key3']
		);

		_a_log_write($log_ret, array('success' => $ret === FALSE ? '0' : '1'));
	} else {
		_a_log_fail('theme', 'create', 'invalid auth', array(
			'target_key' => @$_POST['name']
		));
		$ret = FALSE;
	}
}
if (! empty($_POST['no_ajax'])) {
	header('Location: '.BASE_URL.'theme/'.@$_POST['name']);
} else {
	echo json_encode($ret);
}
?>