ini_set('html_errors', '0');
require '../_base.php';
gb::authenticate();
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
gb_admin::error_rsp('405 Method Not Allowed', '405 Method Not Allowed');
}
try {
# parse input
static $spec_fields = array('name' => '', 'version' => '(work)', 'commit' => 'bool(false)');
static $state_fields = array('mimeType' => ':trim', 'title' => ':trim', 'slug' => ':trim', 'body' => '', 'tags' => '[]', 'categories' => '[]', 'published' => '@GBDateTime', 'author' => '@GBAuthor', 'commentsOpen' => 'bool', 'pingbackOpen' => 'bool', 'draft' => 'bool');
$input = gb_input::process(array_merge($spec_fields, $state_fields));
# find post
$created = false;
if ($input['name'] !== null) {
if (!($post = GBPost::findByName($input['name'], $input['version']))) {
gb_admin::error_rsp('Post ' . r($input['name']) . ' not found');
}
} else {
$post = new GBPost();
$created = true;
}
# set post state
$modified_state = array();
foreach ($state_fields as $k => $discard) {
$v = $input[$k];
if ($v !== null && $post->{$k} !== $v) {
if ($k === 'body') {
$post->setRawBody($v);
$modified_state[$k] = $post->rawBody();
} else {
$post->{$k} = $v;