/** * mt.setPostCategories : set cats for a post * * @param xmlrpcmsg XML-RPC Message * 0 postid (string): Unique identifier of the post to edit * 1 username (string): Login for a Blogger user who is member of the blog. * 2 password (string): Password for said username. */ function mt_setPostCategories($m) { // CHECK LOGIN: /** * @var User */ if (!($current_User =& xmlrpcs_login($m, 1, 2))) { // Login failed, return (last) error: return xmlrpcs_resperror(); } // GET POST: /** * @var Item */ if (!($edited_Item =& xmlrpcs_get_Item($m, 0))) { // Failed, return (last) error: return xmlrpcs_resperror(); } if (!$current_User->check_perm('item_post!CURSTATUS', 'edit', false, $edited_Item)) { // Permission denied return xmlrpcs_resperror(3); } $xcontent = $m->getParam(3); // This is now an array of structs $contentstruct = xmlrpc_decode_recurse($xcontent); logIO('Decoded xcontent'); $categories = array(); $category = NULL; foreach ($contentstruct as $catstruct) { logIO('Category ID: ' . $catstruct['categoryId']); if (!empty($catstruct['isPrimary'])) { $category = $catstruct['categoryId']; logIO('got primary category and there should only be one... ' . $category); } $categories[] = $catstruct['categoryId']; } if (empty($categories)) { return xmlrpcs_resperror(4, 'No categories specified.'); } else { if (empty($category)) { // Use first one as default: $category = $categories[0]; } } // Check if category exists and can be used: $Blog =& $edited_Item->get_Blog(); if (!xmlrpcs_check_cats($category, $Blog, $categories)) { // Error: return xmlrpcs_resperror(); } // CHECK PERMISSION: (we need perm on all categories, especially if they are in different blogs) if (!$current_User->check_perm('cats_post!' . $edited_Item->status, 'edit', false, $categories)) { // Permission denied return xmlrpcs_resperror(3); // User error 3 } logIO('Permission granted.'); logIO('Main Cat: ' . $category . ' - Other: ' . implode(',', $categories)); // UPDATE POST CATEGORIES IN DB: $edited_Item->set('main_cat_ID', $category); $edited_Item->set('extra_cat_IDs', $categories); if ($edited_Item->dbupdate() === false) { logIO('Update failed.'); return xmlrpcs_resperror(99, 'Update failed.'); } logIO('OK.'); return new xmlrpcresp(new xmlrpcval(1, 'boolean')); }
/** * Edit an Item and return an XML-RPC response * * @param Item * @param array Item properties * @param object Blog where we are going to create a new Item * @return xmlrpcmsg */ function xmlrpcs_edit_item(&$edited_Item, $params) { global $current_User, $Messages, $DB, $posttypes_perms; $params = array_merge(array('title' => NULL, 'content' => NULL, 'date' => '', 'main_cat_ID' => NULL, 'extra_cat_IDs' => NULL, 'cat_IDs' => array(), 'status' => '', 'tags' => NULL, 'excerpt' => NULL, 'item_typ_ID' => NULL, 'comment_status' => '', 'urltitle' => NULL, 'featured' => NULL, 'custom_fields' => NULL, 'order' => NULL, 'parent_ID' => NULL, 'author_ID' => NULL, 'locale' => ''), $params); $Blog =& $edited_Item->get_Blog(); logIO('Requested Blog: ' . $Blog->ID . ' - ' . $Blog->name); if (empty($Blog)) { // Blog does not exist: return xmlrpcs_resperror(); } if (is_array($params['cat_IDs']) && count($params['cat_IDs']) > 0) { // Let's use first cat for MAIN and others for EXTRA $params['main_cat_ID'] = array_shift($params['cat_IDs']); $params['extra_cat_IDs'] = $params['cat_IDs']; } if (!is_null($params['main_cat_ID']) && is_array($params['extra_cat_IDs'])) { // Check new categories logIO('Main cat ID: ' . $params['main_cat_ID']); logIO('Extra cat IDs: ' . implode(', ', $params['extra_cat_IDs'])); // Check if category exists and can be used if (!xmlrpcs_check_cats($params['main_cat_ID'], $Blog, $params['extra_cat_IDs'])) { // Permission denied return xmlrpcs_resperror(3); // User error 3 } /* * CHECK PERMISSION: (we need perm on all categories, especially if they are in different blogs) * NOTE: extra_cat_IDs array now includes main_cat_ID too, so we are actually checking ALL categories below */ if (!$current_User->check_perm('cats_post!' . $params['status'], 'edit', false, $params['extra_cat_IDs'])) { } } if (!is_null($params['item_typ_ID'])) { if (!preg_match('~^[0-9]+$~', $params['item_typ_ID'])) { // Only accept numeric values, switch to default value $params['item_typ_ID'] = NULL; } foreach ($posttypes_perms as $l_permname => $l_posttypes) { // "Reverse" the $posttypes_perms array: foreach ($l_posttypes as $ll_posttype) { $posttype2perm[$ll_posttype] = $l_permname; } } if (isset($posttype2perm[$params['item_typ_ID']])) { // Check permission for this post type if (!$current_User->check_perm('cats_' . $posttype2perm[$params['item_typ_ID']], 'edit', false, $params['extra_cat_IDs'])) { // Permission denied return xmlrpcs_resperror(3); // User error 3 } } } logIO('Post type: ' . $params['item_typ_ID']); logIO('Permission granted.'); // CHECK HTML SANITY: if (($params['title'] = check_html_sanity($params['title'], 'xmlrpc_posting')) === false) { return xmlrpcs_resperror(21, $Messages->get_string('Invalid post title, please correct these errors:', '')); } if (($params['content'] = check_html_sanity($params['content'], 'xmlrpc_posting')) === false) { return xmlrpcs_resperror(22, $Messages->get_string('Invalid post contents, please correct these errors:' . "\n", '', " // \n", 'xmlrpc')); } if (!is_null($params['title'])) { $edited_Item->set('title', $params['title']); } if (!is_null($params['content'])) { $edited_Item->set('content', $params['content']); } if (!is_null($params['urltitle'])) { $edited_Item->set('urltitle', $params['urltitle']); } if (!is_null($params['main_cat_ID']) && !is_null($params['extra_cat_IDs'])) { $edited_Item->set('main_cat_ID', $params['main_cat_ID']); $edited_Item->set('extra_cat_IDs', $params['extra_cat_IDs']); } if (!is_null($params['item_typ_ID'])) { $edited_Item->set('ptyp_ID', $params['item_typ_ID']); } if (!is_null($params['featured'])) { $edited_Item->set('featured', $params['featured']); } if (!is_null($params['order'])) { if (!(empty($params['order']) && !$edited_Item->order)) { // Do not allow 0 order if there was no order set before $edited_Item->set('order', $params['order']); } } if (!is_null($params['parent_ID'])) { $edited_Item->set('parent_ID', $params['parent_ID']); } if (!is_null($params['author_ID']) && $params['author_ID'] != $this->creator_user_ID) { // We have already checked perms to edit items created by other users $edited_Item->set('lastedit_user_ID', $params['parent_ID']); } if (!is_null($params['tags'])) { $edited_Item->set_tags_from_string($params['tags']); } if (!is_null($params['excerpt'])) { $edited_Item->set('excerpt', $params['excerpt']); } if (!empty($params['comment_status']) && $Blog->get_setting('allow_comments') != 'never' && $Blog->get_setting('disable_comments_bypost')) { // Comment status $edited_Item->set('comment_status', $params['comment_status']); } if (!empty($params['status'])) { $edited_Item->set('status', $params['status']); } if (!empty($params['date'])) { $edited_Item->set('issue_date', $params['date']); } if (!empty($params['locale'])) { $edited_Item->set('locale', $params['locale']); } logIO(var_export($edited_Item->dbchanges, true)); // UPDATE POST IN DB: $edited_Item->dbupdate(); if ($DB->error) { return xmlrpcs_resperror(99, 'Error while updating item: ' . $DB->last_error); } if (!is_null($params['custom_fields'])) { // TODO sam2kb> Add custom fields if (is_array($params['custom_fields']) && count($params['custom_fields']) > 0) { logIO('Modifying custom fields...'); foreach ($params['custom_fields'] as $field) { // id, key, value logIO('Custom field: ' . var_export($field, true)); } } else { logIO('Deleting custom fields...'); } } // Execute or schedule notifications & pings: logIO('Handling notifications...'); $edited_Item->handle_post_processing(false); logIO('OK.'); return new xmlrpcresp(new xmlrpcval(1, 'boolean')); }