3 define('XMLRPC_REQUEST', true);
5 // Some browser-embedded clients send cookies. We don't want them.
8 // A bug in PHP < 5.2.2 makes $HTTP_RAW_POST_DATA not set by default,
9 // but we can do it ourself.
10 if ( !isset( $HTTP_RAW_POST_DATA ) ) {
11 $HTTP_RAW_POST_DATA = file_get_contents( 'php://input' );
14 # fix for mozBlog and other cases where '<?xml' isn't on the very first line
15 if ( isset($HTTP_RAW_POST_DATA) )
16 $HTTP_RAW_POST_DATA = trim($HTTP_RAW_POST_DATA);
18 include('./wp-config.php');
20 if ( isset( $_GET['rsd'] ) ) { // http://archipelago.phrasewise.com/rsd
21 header('Content-Type: text/xml; charset=' . get_option('blog_charset'), true);
23 <?php echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>'; ?>
24 <rsd version="1.0" xmlns="http://archipelago.phrasewise.com/rsd">
26 <engineName>WordPress</engineName>
27 <engineLink>http://wordpress.org/</engineLink>
28 <homePageLink><?php bloginfo_rss('url') ?></homePageLink>
30 <api name="WordPress" blogID="1" preferred="true" apiLink="<?php bloginfo_rss('wpurl') ?>/xmlrpc.php" />
31 <api name="Movable Type" blogID="1" preferred="false" apiLink="<?php bloginfo_rss('wpurl') ?>/xmlrpc.php" />
32 <api name="MetaWeblog" blogID="1" preferred="false" apiLink="<?php bloginfo_rss('wpurl') ?>/xmlrpc.php" />
33 <api name="Blogger" blogID="1" preferred="false" apiLink="<?php bloginfo_rss('wpurl') ?>/xmlrpc.php" />
34 <api name="Atom" blogID="" preferred="false" apiLink="<?php echo apply_filters('atom_service_url', (get_bloginfo('url')."/wp-app.php/service"))?>" />
42 include_once(ABSPATH . 'wp-admin/includes/admin.php');
43 include_once(ABSPATH . WPINC . '/class-IXR.php');
45 // Turn off all warnings and errors.
46 // error_reporting(0);
48 $post_default_title = ""; // posts submitted via the xmlrpc interface get that title
52 function logIO($io,$msg) {
53 global $xmlrpc_logging;
54 if ($xmlrpc_logging) {
55 $fp = fopen("../xmlrpc.log","a+");
56 $date = gmdate("Y-m-d H:i:s ");
57 $iot = ($io == "I") ? " Input: " : " Output: ";
58 fwrite($fp, "\n\n".$date.$iot.$msg);
64 function starify($string) {
66 return str_repeat('*', $i);
69 if ( isset($HTTP_RAW_POST_DATA) )
70 logIO("I", $HTTP_RAW_POST_DATA);
72 class wp_xmlrpc_server extends IXR_Server {
74 function wp_xmlrpc_server() {
75 $this->methods = array(
77 'wp.getPage' => 'this:wp_getPage',
78 'wp.getPages' => 'this:wp_getPages',
79 'wp.newPage' => 'this:wp_newPage',
80 'wp.deletePage' => 'this:wp_deletePage',
81 'wp.editPage' => 'this:wp_editPage',
82 'wp.getPageList' => 'this:wp_getPageList',
83 'wp.getAuthors' => 'this:wp_getAuthors',
84 'wp.getCategories' => 'this:mw_getCategories', // Alias
85 'wp.newCategory' => 'this:wp_newCategory',
86 'wp.deleteCategory' => 'this:wp_deleteCategory',
87 'wp.suggestCategories' => 'this:wp_suggestCategories',
88 'wp.uploadFile' => 'this:mw_newMediaObject', // Alias
89 'wp.getCommentCount' => 'this:wp_getCommentCount',
90 'wp.getPostStatusList' => 'this:wp_getPostStatusList',
91 'wp.getPageStatusList' => 'this:wp_getPageStatusList',
94 'blogger.getUsersBlogs' => 'this:blogger_getUsersBlogs',
95 'blogger.getUserInfo' => 'this:blogger_getUserInfo',
96 'blogger.getPost' => 'this:blogger_getPost',
97 'blogger.getRecentPosts' => 'this:blogger_getRecentPosts',
98 'blogger.getTemplate' => 'this:blogger_getTemplate',
99 'blogger.setTemplate' => 'this:blogger_setTemplate',
100 'blogger.newPost' => 'this:blogger_newPost',
101 'blogger.editPost' => 'this:blogger_editPost',
102 'blogger.deletePost' => 'this:blogger_deletePost',
104 // MetaWeblog API (with MT extensions to structs)
105 'metaWeblog.newPost' => 'this:mw_newPost',
106 'metaWeblog.editPost' => 'this:mw_editPost',
107 'metaWeblog.getPost' => 'this:mw_getPost',
108 'metaWeblog.getRecentPosts' => 'this:mw_getRecentPosts',
109 'metaWeblog.getCategories' => 'this:mw_getCategories',
110 'metaWeblog.newMediaObject' => 'this:mw_newMediaObject',
112 // MetaWeblog API aliases for Blogger API
113 // see http://www.xmlrpc.com/stories/storyReader$2460
114 'metaWeblog.deletePost' => 'this:blogger_deletePost',
115 'metaWeblog.getTemplate' => 'this:blogger_getTemplate',
116 'metaWeblog.setTemplate' => 'this:blogger_setTemplate',
117 'metaWeblog.getUsersBlogs' => 'this:blogger_getUsersBlogs',
120 'mt.getCategoryList' => 'this:mt_getCategoryList',
121 'mt.getRecentPostTitles' => 'this:mt_getRecentPostTitles',
122 'mt.getPostCategories' => 'this:mt_getPostCategories',
123 'mt.setPostCategories' => 'this:mt_setPostCategories',
124 'mt.supportedMethods' => 'this:mt_supportedMethods',
125 'mt.supportedTextFilters' => 'this:mt_supportedTextFilters',
126 'mt.getTrackbackPings' => 'this:mt_getTrackbackPings',
127 'mt.publishPost' => 'this:mt_publishPost',
130 'pingback.ping' => 'this:pingback_ping',
131 'pingback.extensions.getPingbacks' => 'this:pingback_extensions_getPingbacks',
133 'demo.sayHello' => 'this:sayHello',
134 'demo.addTwoNumbers' => 'this:addTwoNumbers'
136 $this->methods = apply_filters('xmlrpc_methods', $this->methods);
137 $this->IXR_Server($this->methods);
140 function sayHello($args) {
144 function addTwoNumbers($args) {
147 return $number1 + $number2;
150 function login_pass_ok($user_login, $user_pass) {
151 if (!user_pass_ok($user_login, $user_pass)) {
152 $this->error = new IXR_Error(403, __('Bad login/pass combination.'));
158 function escape(&$array) {
161 if(!is_array($array)) {
162 return($wpdb->escape($array));
165 foreach ( (array) $array as $k => $v ) {
167 $this->escape($array[$k]);
168 } else if (is_object($v)) {
171 $array[$k] = $wpdb->escape($v);
177 function get_custom_fields($post_id) {
178 $post_id = (int) $post_id;
180 $custom_fields = array();
182 foreach ( (array) has_meta($post_id) as $meta ) {
183 // Don't expose protected fields.
184 if ( strpos($meta['meta_key'], '_wp_') === 0 ) {
188 $custom_fields[] = array(
189 "id" => $meta['meta_id'],
190 "key" => $meta['meta_key'],
191 "value" => $meta['meta_value']
195 return $custom_fields;
198 function set_custom_fields($post_id, $fields) {
199 $post_id = (int) $post_id;
201 foreach ( (array) $fields as $meta ) {
202 if ( isset($meta['id']) ) {
203 $meta['id'] = (int) $meta['id'];
205 if ( isset($meta['key']) ) {
206 update_meta($meta['id'], $meta['key'], $meta['value']);
209 delete_meta($meta['id']);
213 $_POST['metakeyinput'] = $meta['key'];
214 $_POST['metavalue'] = $meta['value'];
221 * WordPress XML-RPC API
224 function wp_getPage($args) {
225 $this->escape($args);
227 $blog_id = (int) $args[0];
228 $page_id = (int) $args[1];
229 $username = $args[2];
230 $password = $args[3];
232 if(!$this->login_pass_ok($username, $password)) {
233 return($this->error);
236 set_current_user( 0, $username );
237 if( !current_user_can( 'edit_page', $page_id ) )
238 return new IXR_Error( 401, __( 'Sorry, you can not edit this page.' ) );
240 do_action('xmlrpc_call', 'wp.getPage');
243 $page = get_page($page_id);
245 // If we found the page then format the data.
246 if($page->ID && ($page->post_type == "page")) {
247 // Get all of the page content and link.
248 $full_page = get_extended($page->post_content);
249 $link = post_permalink($page->ID);
251 // Get info the page parent if there is one.
253 if(!empty($page->post_parent)) {
254 $parent = get_page($page->post_parent);
255 $parent_title = $parent->post_title;
258 // Determine comment and ping settings.
259 $allow_comments = ("open" == $page->comment_status) ? 1 : 0;
260 $allow_pings = ("open" == $page->ping_status) ? 1 : 0;
263 $page_date = mysql2date("Ymd\TH:i:s", $page->post_date);
264 $page_date_gmt = mysql2date("Ymd\TH:i:s", $page->post_date_gmt);
266 // Pull the categories info together.
267 $categories = array();
268 foreach(wp_get_post_categories($page->ID) as $cat_id) {
269 $categories[] = get_cat_name($cat_id);
272 // Get the author info.
273 $author = get_userdata($page->post_author);
275 $page_struct = array(
276 "dateCreated" => new IXR_Date($page_date),
277 "userid" => $page->post_author,
278 "page_id" => $page->ID,
279 "page_status" => $page->post_status,
280 "description" => $full_page["main"],
281 "title" => $page->post_title,
283 "permaLink" => $link,
284 "categories" => $categories,
285 "excerpt" => $page->post_excerpt,
286 "text_more" => $full_page["extended"],
287 "mt_allow_comments" => $allow_comments,
288 "mt_allow_pings" => $allow_pings,
289 "wp_slug" => $page->post_name,
290 "wp_password" => $page->post_password,
291 "wp_author" => $author->display_name,
292 "wp_page_parent_id" => $page->post_parent,
293 "wp_page_parent_title" => $parent_title,
294 "wp_page_order" => $page->menu_order,
295 "wp_author_id" => $author->ID,
296 "wp_author_display_name" => $author->display_name,
297 "date_created_gmt" => new IXR_Date($page_date_gmt),
298 "custom_fields" => $this->get_custom_fields($page_id)
301 return($page_struct);
303 // If the page doesn't exist indicate that.
305 return(new IXR_Error(404, __("Sorry, no such page.")));
310 * WordPress XML-RPC API
313 function wp_getPages($args) {
314 $this->escape($args);
316 $blog_id = (int) $args[0];
317 $username = $args[1];
318 $password = $args[2];
320 if(!$this->login_pass_ok($username, $password)) {
321 return($this->error);
324 set_current_user( 0, $username );
325 if( !current_user_can( 'edit_pages' ) )
326 return new IXR_Error( 401, __( 'Sorry, you can not edit pages.' ) );
328 do_action('xmlrpc_call', 'wp.getPages');
330 // Lookup info on pages.
331 $pages = get_pages();
332 $num_pages = count($pages);
334 // If we have pages, put together their info.
335 if($num_pages >= 1) {
336 $pages_struct = array();
338 for($i = 0; $i < $num_pages; $i++) {
339 $page = wp_xmlrpc_server::wp_getPage(array(
340 $blog_id, $pages[$i]->ID, $username, $password
342 $pages_struct[] = $page;
345 return($pages_struct);
347 // If no pages were found return an error.
354 * WordPress XML-RPC API
357 function wp_newPage($args) {
358 // Items not escaped here will be escaped in newPost.
359 $username = $this->escape($args[1]);
360 $password = $this->escape($args[2]);
364 if(!$this->login_pass_ok($username, $password)) {
365 return($this->error);
368 do_action('xmlrpc_call', 'wp.newPage');
370 // Set the user context and check if they are allowed
372 $user = set_current_user(0, $username);
373 if(!current_user_can("publish_pages")) {
374 return(new IXR_Error(401, __("Sorry, you can not add new pages.")));
377 // Mark this as content for a page.
378 $args[3]["post_type"] = "page";
380 // Let mw_newPost do all of the heavy lifting.
381 return($this->mw_newPost($args));
385 * WordPress XML-RPC API
388 function wp_deletePage($args) {
389 $this->escape($args);
391 $blog_id = (int) $args[0];
392 $username = $args[1];
393 $password = $args[2];
394 $page_id = (int) $args[3];
396 if(!$this->login_pass_ok($username, $password)) {
397 return($this->error);
400 do_action('xmlrpc_call', 'wp.deletePage');
402 // Get the current page based on the page_id and
403 // make sure it is a page and not a post.
404 $actual_page = wp_get_single_post($page_id, ARRAY_A);
407 || ($actual_page["post_type"] != "page")
409 return(new IXR_Error(404, __("Sorry, no such page.")));
412 // Set the user context and make sure they can delete pages.
413 set_current_user(0, $username);
414 if(!current_user_can("delete_page", $page_id)) {
415 return(new IXR_Error(401, __("Sorry, you do not have the right to delete this page.")));
418 // Attempt to delete the page.
419 $result = wp_delete_post($page_id);
421 return(new IXR_Error(500, __("Failed to delete the page.")));
428 * WordPress XML-RPC API
431 function wp_editPage($args) {
432 // Items not escaped here will be escaped in editPost.
433 $blog_id = (int) $args[0];
434 $page_id = (int) $this->escape($args[1]);
435 $username = $this->escape($args[2]);
436 $password = $this->escape($args[3]);
440 if(!$this->login_pass_ok($username, $password)) {
441 return($this->error);
444 do_action('xmlrpc_call', 'wp.editPage');
446 // Get the page data and make sure it is a page.
447 $actual_page = wp_get_single_post($page_id, ARRAY_A);
450 || ($actual_page["post_type"] != "page")
452 return(new IXR_Error(404, __("Sorry, no such page.")));
455 // Set the user context and make sure they are allowed to edit pages.
456 set_current_user(0, $username);
457 if(!current_user_can("edit_page", $page_id)) {
458 return(new IXR_Error(401, __("Sorry, you do not have the right to edit this page.")));
461 // Mark this as content for a page.
462 $content["post_type"] = "page";
464 // Arrange args in the way mw_editPost understands.
473 // Let mw_editPost do all of the heavy lifting.
474 return($this->mw_editPost($args));
478 * WordPress XML-RPC API
481 function wp_getPageList($args) {
484 $this->escape($args);
486 $blog_id = (int) $args[0];
487 $username = $args[1];
488 $password = $args[2];
490 if(!$this->login_pass_ok($username, $password)) {
491 return($this->error);
494 set_current_user( 0, $username );
495 if( !current_user_can( 'edit_pages' ) )
496 return new IXR_Error( 401, __( 'Sorry, you can not edit pages.' ) );
498 do_action('xmlrpc_call', 'wp.getPageList');
500 // Get list of pages ids and titles
501 $page_list = $wpdb->get_results("
503 post_title page_title,
504 post_parent page_parent_id,
508 WHERE post_type = 'page'
512 // The date needs to be formated properly.
513 $num_pages = count($page_list);
514 for($i = 0; $i < $num_pages; $i++) {
515 $post_date = mysql2date("Ymd\TH:i:s", $page_list[$i]->post_date);
516 $post_date_gmt = mysql2date("Ymd\TH:i:s", $page_list[$i]->post_date_gmt);
518 $page_list[$i]->dateCreated = new IXR_Date($post_date);
519 $page_list[$i]->date_created_gmt = new IXR_Date($post_date_gmt);
521 unset($page_list[$i]->post_date_gmt);
522 unset($page_list[$i]->post_date);
529 * WordPress XML-RPC API
532 function wp_getAuthors($args) {
534 $this->escape($args);
536 $blog_id = (int) $args[0];
537 $username = $args[1];
538 $password = $args[2];
540 if(!$this->login_pass_ok($username, $password)) {
541 return($this->error);
544 set_current_user(0, $username);
545 if(!current_user_can("edit_posts")) {
546 return(new IXR_Error(401, __("Sorry, you can not edit posts on this blog.")));
549 do_action('xmlrpc_call', 'wp.getAuthors');
552 foreach( (array) get_users_of_blog() as $row ) {
554 "user_id" => $row->user_id,
555 "user_login" => $row->user_login,
556 "display_name" => $row->display_name
564 * WordPress XML-RPC API
567 function wp_newCategory($args) {
568 $this->escape($args);
570 $blog_id = (int) $args[0];
571 $username = $args[1];
572 $password = $args[2];
573 $category = $args[3];
575 if(!$this->login_pass_ok($username, $password)) {
576 return($this->error);
579 do_action('xmlrpc_call', 'wp.newCategory');
581 // Set the user context and make sure they are
582 // allowed to add a category.
583 set_current_user(0, $username);
584 if(!current_user_can("manage_categories")) {
585 return(new IXR_Error(401, __("Sorry, you do not have the right to add a category.")));
588 // If no slug was provided make it empty so that
589 // WordPress will generate one.
590 if(empty($category["slug"])) {
591 $category["slug"] = "";
594 // If no parent_id was provided make it empty
595 // so that it will be a top level page (no parent).
596 if ( !isset($category["parent_id"]) )
597 $category["parent_id"] = "";
599 // If no description was provided make it empty.
600 if(empty($category["description"])) {
601 $category["description"] = "";
604 $new_category = array(
605 "cat_name" => $category["name"],
606 "category_nicename" => $category["slug"],
607 "category_parent" => $category["parent_id"],
608 "category_description" => $category["description"]
611 $cat_id = wp_insert_category($new_category);
613 return(new IXR_Error(500, __("Sorry, the new category failed.")));
620 * WordPress XML-RPC API
623 function wp_deleteCategory($args) {
624 $this->escape($args);
626 $blog_id = (int) $args[0];
627 $username = $args[1];
628 $password = $args[2];
629 $category_id = (int) $args[3];
631 if( !$this->login_pass_ok( $username, $password ) ) {
635 do_action('xmlrpc_call', 'wp.deleteCategory');
637 set_current_user(0, $username);
638 if( !current_user_can("manage_categories") ) {
639 return new IXR_Error( 401, __( "Sorry, you do not have the right to delete a category." ) );
642 return wp_delete_category( $category_id );
647 * WordPress XML-RPC API
648 * wp_suggestCategories
650 function wp_suggestCategories($args) {
651 $this->escape($args);
653 $blog_id = (int) $args[0];
654 $username = $args[1];
655 $password = $args[2];
656 $category = $args[3];
657 $max_results = (int) $args[4];
659 if(!$this->login_pass_ok($username, $password)) {
660 return($this->error);
663 set_current_user(0, $username);
664 if( !current_user_can( 'edit_posts' ) )
665 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts to this blog in order to view categories.' ) );
667 do_action('xmlrpc_call', 'wp.suggestCategories');
669 $category_suggestions = array();
670 $args = array('get' => 'all', 'number' => $max_results, 'name__like' => $category);
671 foreach ( (array) get_categories($args) as $cat ) {
672 $category_suggestions[] = array(
673 "category_id" => $cat->cat_ID,
674 "category_name" => $cat->cat_name
678 return($category_suggestions);
681 function wp_getCommentCount( $args ) {
682 $this->escape($args);
684 $blog_id = (int) $args[0];
685 $username = $args[1];
686 $password = $args[2];
687 $post_id = (int) $args[3];
689 if( !$this->login_pass_ok( $username, $password ) ) {
690 return new IXR_Error( 403, __( 'Bad login/pass combination.' ) );
693 set_current_user( 0, $username );
694 if( !current_user_can( 'edit_posts' ) ) {
695 return new IXR_Error( 403, __( 'You are not allowed access to details about comments.' ) );
698 do_action('xmlrpc_call', 'wp.getCommentCount');
700 return get_comment_count( $post_id );
704 function wp_getPostStatusList( $args ) {
705 $this->escape( $args );
707 $blog_id = (int) $args[0];
708 $username = $args[1];
709 $password = $args[2];
711 if( !$this->login_pass_ok( $username, $password ) ) {
712 return new IXR_Error( 403, __( 'Bad login/pass combination.' ) );
715 set_current_user( 0, $username );
716 if( !current_user_can( 'edit_posts' ) ) {
717 return new IXR_Error( 403, __( 'You are not allowed access to details about this blog.' ) );
720 do_action('xmlrpc_call', 'wp.getPostStatusList');
722 return get_post_statuses( );
726 function wp_getPageStatusList( $args ) {
727 $this->escape( $args );
729 $blog_id = (int) $args[0];
730 $username = $args[1];
731 $password = $args[2];
733 if( !$this->login_pass_ok( $username, $password ) ) {
734 return new IXR_Error( 403, __( 'Bad login/pass combination.' ) );
737 set_current_user( 0, $username );
738 if( !current_user_can( 'edit_posts' ) ) {
739 return new IXR_Error( 403, __( 'You are not allowed access to details about this blog.' ) );
742 do_action('xmlrpc_call', 'wp.getPageStatusList');
744 return get_page_statuses( );
748 /* Blogger API functions
749 * specs on http://plant.blogger.com/api and http://groups.yahoo.com/group/bloggerDev/
753 /* blogger.getUsersBlogs will make more sense once we support multiple blogs */
754 function blogger_getUsersBlogs($args) {
756 $this->escape($args);
758 $user_login = $args[1];
759 $user_pass = $args[2];
761 if (!$this->login_pass_ok($user_login, $user_pass)) {
765 do_action('xmlrpc_call', 'blogger.getUsersBlogs');
767 set_current_user(0, $user_login);
768 $is_admin = current_user_can('level_8');
771 'isAdmin' => $is_admin,
772 'url' => get_option('home') . '/',
774 'blogName' => get_option('blogname')
777 return array($struct);
781 /* blogger.getUsersInfo gives your client some info about you, so you don't have to */
782 function blogger_getUserInfo($args) {
784 $this->escape($args);
786 $user_login = $args[1];
787 $user_pass = $args[2];
789 if (!$this->login_pass_ok($user_login, $user_pass)) {
793 set_current_user( 0, $user_login );
794 if( !current_user_can( 'edit_posts' ) )
795 return new IXR_Error( 401, __( 'Sorry, you do not have access to user data on this blog.' ) );
797 do_action('xmlrpc_call', 'blogger.getUserInfo');
799 $user_data = get_userdatabylogin($user_login);
802 'nickname' => $user_data->nickname,
803 'userid' => $user_data->ID,
804 'url' => $user_data->user_url,
805 'lastname' => $user_data->last_name,
806 'firstname' => $user_data->first_name
813 /* blogger.getPost ...gets a post */
814 function blogger_getPost($args) {
816 $this->escape($args);
818 $post_ID = (int) $args[1];
819 $user_login = $args[2];
820 $user_pass = $args[3];
822 if (!$this->login_pass_ok($user_login, $user_pass)) {
826 set_current_user( 0, $user_login );
827 if( !current_user_can( 'edit_post', $post_ID ) )
828 return new IXR_Error( 401, __( 'Sorry, you can not edit this post.' ) );
830 do_action('xmlrpc_call', 'blogger.getPost');
832 $post_data = wp_get_single_post($post_ID, ARRAY_A);
834 $categories = implode(',', wp_get_post_categories($post_ID));
836 $content = '<title>'.stripslashes($post_data['post_title']).'</title>';
837 $content .= '<category>'.$categories.'</category>';
838 $content .= stripslashes($post_data['post_content']);
841 'userid' => $post_data['post_author'],
842 'dateCreated' => new IXR_Date(mysql2date('Ymd\TH:i:s', $post_data['post_date'])),
843 'content' => $content,
844 'postid' => $post_data['ID']
851 /* blogger.getRecentPosts ...gets recent posts */
852 function blogger_getRecentPosts($args) {
854 $this->escape($args);
856 $blog_ID = (int) $args[1]; /* though we don't use it yet */
857 $user_login = $args[2];
858 $user_pass = $args[3];
859 $num_posts = $args[4];
861 if (!$this->login_pass_ok($user_login, $user_pass)) {
865 do_action('xmlrpc_call', 'blogger.getRecentPosts');
867 $posts_list = wp_get_recent_posts($num_posts);
869 set_current_user( 0, $user_login );
872 $this->error = new IXR_Error(500, __('Either there are no posts, or something went wrong.'));
876 foreach ($posts_list as $entry) {
877 if( !current_user_can( 'edit_post', $entry['ID'] ) )
880 $post_date = mysql2date('Ymd\TH:i:s', $entry['post_date']);
881 $categories = implode(',', wp_get_post_categories($entry['ID']));
883 $content = '<title>'.stripslashes($entry['post_title']).'</title>';
884 $content .= '<category>'.$categories.'</category>';
885 $content .= stripslashes($entry['post_content']);
888 'userid' => $entry['post_author'],
889 'dateCreated' => new IXR_Date($post_date),
890 'content' => $content,
891 'postid' => $entry['ID'],
896 $recent_posts = array();
897 for ($j=0; $j<count($struct); $j++) {
898 array_push($recent_posts, $struct[$j]);
901 return $recent_posts;
905 /* blogger.getTemplate returns your blog_filename */
906 function blogger_getTemplate($args) {
908 $this->escape($args);
910 $blog_ID = (int) $args[1];
911 $user_login = $args[2];
912 $user_pass = $args[3];
913 $template = $args[4]; /* could be 'main' or 'archiveIndex', but we don't use it */
915 if (!$this->login_pass_ok($user_login, $user_pass)) {
919 do_action('xmlrpc_call', 'blogger.getTemplate');
921 set_current_user(0, $user_login);
922 if ( !current_user_can('edit_themes') ) {
923 return new IXR_Error(401, __('Sorry, this user can not edit the template.'));
926 /* warning: here we make the assumption that the blog's URL is on the same server */
927 $filename = get_option('home') . '/';
928 $filename = preg_replace('#https?://.+?/#', $_SERVER['DOCUMENT_ROOT'].'/', $filename);
930 $f = fopen($filename, 'r');
931 $content = fread($f, filesize($filename));
934 /* so it is actually editable with a windows/mac client */
935 // FIXME: (or delete me) do we really want to cater to bad clients at the expense of good ones by BEEPing up their line breaks? commented. $content = str_replace("\n", "\r\n", $content);
941 /* blogger.setTemplate updates the content of blog_filename */
942 function blogger_setTemplate($args) {
944 $this->escape($args);
946 $blog_ID = (int) $args[1];
947 $user_login = $args[2];
948 $user_pass = $args[3];
950 $template = $args[5]; /* could be 'main' or 'archiveIndex', but we don't use it */
952 if (!$this->login_pass_ok($user_login, $user_pass)) {
956 do_action('xmlrpc_call', 'blogger.setTemplate');
958 set_current_user(0, $user_login);
959 if ( !current_user_can('edit_themes') ) {
960 return new IXR_Error(401, __('Sorry, this user can not edit the template.'));
963 /* warning: here we make the assumption that the blog's URL is on the same server */
964 $filename = get_option('home') . '/';
965 $filename = preg_replace('#https?://.+?/#', $_SERVER['DOCUMENT_ROOT'].'/', $filename);
967 if ($f = fopen($filename, 'w+')) {
968 fwrite($f, $content);
971 return new IXR_Error(500, __('Either the file is not writable, or something wrong happened. The file has not been updated.'));
978 /* blogger.newPost ...creates a new post */
979 function blogger_newPost($args) {
981 $this->escape($args);
983 $blog_ID = (int) $args[1]; /* though we don't use it yet */
984 $user_login = $args[2];
985 $user_pass = $args[3];
989 if (!$this->login_pass_ok($user_login, $user_pass)) {
993 do_action('xmlrpc_call', 'blogger.newPost');
995 $cap = ($publish) ? 'publish_posts' : 'edit_posts';
996 $user = set_current_user(0, $user_login);
997 if ( !current_user_can($cap) )
998 return new IXR_Error(401, __('Sorry, you are not allowed to post on this blog.'));
1000 $post_status = ($publish) ? 'publish' : 'draft';
1002 $post_author = $user->ID;
1004 $post_title = xmlrpc_getposttitle($content);
1005 $post_category = xmlrpc_getpostcategory($content);
1006 $post_content = xmlrpc_removepostdata($content);
1008 $post_date = current_time('mysql');
1009 $post_date_gmt = current_time('mysql', 1);
1011 $post_data = compact('blog_ID', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status');
1013 $post_ID = wp_insert_post($post_data);
1014 if ( is_wp_error( $post_ID ) )
1015 return new IXR_Error(500, $post_ID->get_error_message());
1018 return new IXR_Error(500, __('Sorry, your entry could not be posted. Something wrong happened.'));
1020 $this->attach_uploads( $post_ID, $post_content );
1022 logIO('O', "Posted ! ID: $post_ID");
1027 /* blogger.editPost ...edits a post */
1028 function blogger_editPost($args) {
1030 $this->escape($args);
1032 $post_ID = (int) $args[1];
1033 $user_login = $args[2];
1034 $user_pass = $args[3];
1035 $content = $args[4];
1036 $publish = $args[5];
1038 if (!$this->login_pass_ok($user_login, $user_pass)) {
1039 return $this->error;
1042 do_action('xmlrpc_call', 'blogger.editPost');
1044 $actual_post = wp_get_single_post($post_ID,ARRAY_A);
1046 if (!$actual_post) {
1047 return new IXR_Error(404, __('Sorry, no such post.'));
1050 $this->escape($actual_post);
1052 set_current_user(0, $user_login);
1053 if ( !current_user_can('edit_post', $post_ID) )
1054 return new IXR_Error(401, __('Sorry, you do not have the right to edit this post.'));
1056 extract($actual_post, EXTR_SKIP);
1058 if ( ('publish' == $post_status) && !current_user_can('publish_posts') )
1059 return new IXR_Error(401, __('Sorry, you do not have the right to publish this post.'));
1061 $post_title = xmlrpc_getposttitle($content);
1062 $post_category = xmlrpc_getpostcategory($content);
1063 $post_content = xmlrpc_removepostdata($content);
1065 $postdata = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt');
1067 $result = wp_update_post($postdata);
1070 return new IXR_Error(500, __('For some strange yet very annoying reason, this post could not be edited.'));
1072 $this->attach_uploads( $ID, $post_content );
1078 /* blogger.deletePost ...deletes a post */
1079 function blogger_deletePost($args) {
1080 $this->escape($args);
1082 $post_ID = (int) $args[1];
1083 $user_login = $args[2];
1084 $user_pass = $args[3];
1085 $publish = $args[4];
1087 if (!$this->login_pass_ok($user_login, $user_pass)) {
1088 return $this->error;
1091 do_action('xmlrpc_call', 'blogger.deletePost');
1093 $actual_post = wp_get_single_post($post_ID,ARRAY_A);
1095 if (!$actual_post) {
1096 return new IXR_Error(404, __('Sorry, no such post.'));
1099 set_current_user(0, $user_login);
1100 if ( !current_user_can('edit_post', $post_ID) )
1101 return new IXR_Error(401, __('Sorry, you do not have the right to delete this post.'));
1103 $result = wp_delete_post($post_ID);
1106 return new IXR_Error(500, __('For some strange yet very annoying reason, this post could not be deleted.'));
1114 /* MetaWeblog API functions
1115 * specs on wherever Dave Winer wants them to be
1118 /* metaweblog.newPost creates a post */
1119 function mw_newPost($args) {
1120 $this->escape($args);
1122 $blog_ID = (int) $args[0]; // we will support this in the near future
1123 $user_login = $args[1];
1124 $user_pass = $args[2];
1125 $content_struct = $args[3];
1126 $publish = $args[4];
1128 if (!$this->login_pass_ok($user_login, $user_pass)) {
1129 return $this->error;
1131 $user = set_current_user(0, $user_login);
1133 do_action('xmlrpc_call', 'metaWeblog.newPost');
1135 $cap = ( $publish ) ? 'publish_posts' : 'edit_posts';
1136 $error_message = __( 'Sorry, you are not allowed to publish posts on this blog.' );
1137 $post_type = 'post';
1138 if( !empty( $content_struct['post_type'] ) ) {
1139 if( $content_struct['post_type'] == 'page' ) {
1140 $cap = ( $publish ) ? 'publish_pages' : 'edit_pages';
1141 $error_message = __( 'Sorry, you are not allowed to publish pages on this blog.' );
1142 $post_type = 'page';
1144 elseif( $content_struct['post_type'] == 'post' ) {
1145 // This is the default, no changes needed
1148 // No other post_type values are allowed here
1149 return new IXR_Error( 401, __( 'Invalid post type.' ) );
1153 if( !current_user_can( $cap ) ) {
1154 return new IXR_Error( 401, $error_message );
1157 // Let WordPress generate the post_name (slug) unless
1158 // one has been provided.
1160 if(isset($content_struct["wp_slug"])) {
1161 $post_name = $content_struct["wp_slug"];
1164 // Only use a password if one was given.
1165 if(isset($content_struct["wp_password"])) {
1166 $post_password = $content_struct["wp_password"];
1169 // Only set a post parent if one was provided.
1170 if(isset($content_struct["wp_page_parent_id"])) {
1171 $post_parent = $content_struct["wp_page_parent_id"];
1174 // Only set the menu_order if it was provided.
1175 if(isset($content_struct["wp_page_order"])) {
1176 $menu_order = $content_struct["wp_page_order"];
1179 $post_author = $user->ID;
1181 // If an author id was provided then use it instead.
1183 isset($content_struct["wp_author_id"])
1184 && ($user->ID != $content_struct["wp_author_id"])
1186 switch($post_type) {
1188 if(!current_user_can("edit_others_posts")) {
1189 return(new IXR_Error(401, __("You are not allowed to post as this user")));
1193 if(!current_user_can("edit_others_pages")) {
1194 return(new IXR_Error(401, __("You are not allowed to create pages as this user")));
1198 return(new IXR_Error(401, __("Invalid post type.")));
1201 $post_author = $content_struct["wp_author_id"];
1204 $post_title = $content_struct['title'];
1205 $post_content = apply_filters( 'content_save_pre', $content_struct['description'] );
1207 $post_status = $publish ? 'publish' : 'draft';
1209 if( isset( $content_struct["{$post_type}_status"] ) ) {
1210 switch( $content_struct["{$post_type}_status"] ) {
1214 $post_status = $content_struct["{$post_type}_status"];
1217 // Pending is only valid for posts, not pages.
1218 if( $post_type === 'post' ) {
1219 $post_status = $content_struct["{$post_type}_status"];
1223 $post_status = $publish ? 'publish' : 'draft';
1228 $post_excerpt = $content_struct['mt_excerpt'];
1229 $post_more = $content_struct['mt_text_more'];
1231 $tags_input = $content_struct['mt_keywords'];
1233 if(isset($content_struct["mt_allow_comments"])) {
1234 if(!is_numeric($content_struct["mt_allow_comments"])) {
1235 switch($content_struct["mt_allow_comments"]) {
1237 $comment_status = "closed";
1240 $comment_status = "open";
1243 $comment_status = get_option("default_comment_status");
1248 switch((int) $content_struct["mt_allow_comments"]) {
1251 $comment_status = "closed";
1254 $comment_status = "open";
1257 $comment_status = get_option("default_comment_status");
1263 $comment_status = get_option("default_comment_status");
1266 if(isset($content_struct["mt_allow_pings"])) {
1267 if(!is_numeric($content_struct["mt_allow_pings"])) {
1268 switch($content_struct['mt_allow_pings']) {
1270 $ping_status = "closed";
1273 $ping_status = "open";
1276 $ping_status = get_option("default_ping_status");
1281 switch((int) $content_struct["mt_allow_pings"]) {
1283 $ping_status = "closed";
1286 $ping_status = "open";
1289 $ping_status = get_option("default_ping_status");
1295 $ping_status = get_option("default_ping_status");
1299 $post_content = $post_content . "\n<!--more-->\n" . $post_more;
1302 $to_ping = $content_struct['mt_tb_ping_urls'];
1303 if ( is_array($to_ping) )
1304 $to_ping = implode(' ', $to_ping);
1306 // Do some timestamp voodoo
1307 if ( !empty( $content_struct['date_created_gmt'] ) )
1308 $dateCreated = str_replace( 'Z', '', $content_struct['date_created_gmt']->getIso() ) . 'Z'; // We know this is supposed to be GMT, so we're going to slap that Z on there by force
1309 elseif ( !empty( $content_struct['dateCreated']) )
1310 $dateCreated = $content_struct['dateCreated']->getIso();
1312 if ( !empty( $dateCreated ) ) {
1313 $post_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
1314 $post_date_gmt = iso8601_to_datetime($dateCreated, GMT);
1316 $post_date = current_time('mysql');
1317 $post_date_gmt = current_time('mysql', 1);
1320 $catnames = $content_struct['categories'];
1321 logIO('O', 'Post cats: ' . var_export($catnames,true));
1322 $post_category = array();
1324 if (is_array($catnames)) {
1325 foreach ($catnames as $cat) {
1326 $post_category[] = get_cat_ID($cat);
1330 // We've got all the data -- post it:
1331 $postdata = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'comment_status', 'ping_status', 'to_ping', 'post_type', 'post_name', 'post_password', 'post_parent', 'menu_order', 'tags_input');
1333 $post_ID = wp_insert_post($postdata);
1334 if ( is_wp_error( $post_ID ) )
1335 return new IXR_Error(500, $post_ID->get_error_message());
1338 return new IXR_Error(500, __('Sorry, your entry could not be posted. Something wrong happened.'));
1341 if ( isset($content_struct['custom_fields']) ) {
1342 $this->set_custom_fields($post_ID, $content_struct['custom_fields']);
1345 $this->attach_uploads( $post_ID, $post_content );
1347 logIO('O', "Posted ! ID: $post_ID");
1349 return strval($post_ID);
1352 function attach_uploads( $post_ID, $post_content ) {
1355 // find any unattached files
1356 $attachments = $wpdb->get_results( "SELECT ID, guid FROM {$wpdb->posts} WHERE post_parent = '-1' AND post_type = 'attachment'" );
1357 if( is_array( $attachments ) ) {
1358 foreach( $attachments as $file ) {
1359 if( strpos( $post_content, $file->guid ) !== false ) {
1360 $wpdb->query( "UPDATE {$wpdb->posts} SET post_parent = '$post_ID' WHERE ID = '{$file->ID}'" );
1366 /* metaweblog.editPost ...edits a post */
1367 function mw_editPost($args) {
1369 $this->escape($args);
1371 $post_ID = (int) $args[0];
1372 $user_login = $args[1];
1373 $user_pass = $args[2];
1374 $content_struct = $args[3];
1375 $publish = $args[4];
1377 if (!$this->login_pass_ok($user_login, $user_pass)) {
1378 return $this->error;
1380 $user = set_current_user(0, $user_login);
1382 do_action('xmlrpc_call', 'metaWeblog.editPost');
1384 $cap = ( $publish ) ? 'publish_posts' : 'edit_posts';
1385 $error_message = __( 'Sorry, you are not allowed to publish posts on this blog.' );
1386 $post_type = 'post';
1387 if( !empty( $content_struct['post_type'] ) ) {
1388 if( $content_struct['post_type'] == 'page' ) {
1389 $cap = ( $publish ) ? 'publish_pages' : 'edit_pages';
1390 $error_message = __( 'Sorry, you are not allowed to publish pages on this blog.' );
1391 $post_type = 'page';
1393 elseif( $content_struct['post_type'] == 'post' ) {
1394 // This is the default, no changes needed
1397 // No other post_type values are allowed here
1398 return new IXR_Error( 401, __( 'Invalid post type.' ) );
1402 if( !current_user_can( $cap ) ) {
1403 return new IXR_Error( 401, $error_message );
1406 $postdata = wp_get_single_post($post_ID, ARRAY_A);
1408 // If there is no post data for the give post id, stop
1409 // now and return an error. Other wise a new post will be
1410 // created (which was the old behavior).
1411 if(empty($postdata["ID"])) {
1412 return(new IXR_Error(404, __("Invalid post id.")));
1415 $this->escape($postdata);
1416 extract($postdata, EXTR_SKIP);
1418 // Let WordPress manage slug if none was provided.
1420 if(isset($content_struct["wp_slug"])) {
1421 $post_name = $content_struct["wp_slug"];
1424 // Only use a password if one was given.
1425 if(isset($content_struct["wp_password"])) {
1426 $post_password = $content_struct["wp_password"];
1429 // Only set a post parent if one was given.
1430 if(isset($content_struct["wp_page_parent_id"])) {
1431 $post_parent = $content_struct["wp_page_parent_id"];
1434 // Only set the menu_order if it was given.
1435 if(isset($content_struct["wp_page_order"])) {
1436 $menu_order = $content_struct["wp_page_order"];
1439 $post_author = $postdata["post_author"];
1441 // Only set the post_author if one is set.
1443 isset($content_struct["wp_author_id"])
1444 && ($user->ID != $content_struct["wp_author_id"])
1446 switch($post_type) {
1448 if(!current_user_can("edit_others_posts")) {
1449 return(new IXR_Error(401, __("You are not allowed to change the post author as this user.")));
1453 if(!current_user_can("edit_others_pages")) {
1454 return(new IXR_Error(401, __("You are not allowed to change the page author as this user.")));
1458 return(new IXR_Error(401, __("Invalid post type.")));
1461 $post_author = $content_struct["wp_author_id"];
1464 if(isset($content_struct["mt_allow_comments"])) {
1465 if(!is_numeric($content_struct["mt_allow_comments"])) {
1466 switch($content_struct["mt_allow_comments"]) {
1468 $comment_status = "closed";
1471 $comment_status = "open";
1474 $comment_status = get_option("default_comment_status");
1479 switch((int) $content_struct["mt_allow_comments"]) {
1482 $comment_status = "closed";
1485 $comment_status = "open";
1488 $comment_status = get_option("default_comment_status");
1494 if(isset($content_struct["mt_allow_pings"])) {
1495 if(!is_numeric($content_struct["mt_allow_pings"])) {
1496 switch($content_struct["mt_allow_pings"]) {
1498 $ping_status = "closed";
1501 $ping_status = "open";
1504 $ping_status = get_option("default_ping_status");
1509 switch((int) $content_struct["mt_allow_pings"]) {
1511 $ping_status = "closed";
1514 $ping_status = "open";
1517 $ping_status = get_option("default_ping_status");
1523 $post_title = $content_struct['title'];
1524 $post_content = apply_filters( 'content_save_pre', $content_struct['description'] );
1525 $catnames = $content_struct['categories'];
1527 $post_category = array();
1529 if (is_array($catnames)) {
1530 foreach ($catnames as $cat) {
1531 $post_category[] = get_cat_ID($cat);
1535 $post_excerpt = $content_struct['mt_excerpt'];
1536 $post_more = $content_struct['mt_text_more'];
1538 $post_status = $publish ? 'publish' : 'draft';
1539 if( isset( $content_struct["{$post_type}_status"] ) ) {
1540 switch( $content_struct["{$post_type}_status"] ) {
1544 $post_status = $content_struct["{$post_type}_status"];
1547 // Pending is only valid for posts, not pages.
1548 if( $post_type === 'post' ) {
1549 $post_status = $content_struct["{$post_type}_status"];
1553 $post_status = $publish ? 'publish' : 'draft';
1558 $tags_input = $content_struct['mt_keywords'];
1560 if ( ('publish' == $post_status) ) {
1561 if ( ( 'page' == $post_type ) && !current_user_can('publish_pages') )
1562 return new IXR_Error(401, __('Sorry, you do not have the right to publish this page.'));
1563 else if ( !current_user_can('publish_posts') )
1564 return new IXR_Error(401, __('Sorry, you do not have the right to publish this post.'));
1568 $post_content = $post_content . "\n<!--more-->\n" . $post_more;
1571 $to_ping = $content_struct['mt_tb_ping_urls'];
1572 if ( is_array($to_ping) )
1573 $to_ping = implode(' ', $to_ping);
1575 // Do some timestamp voodoo
1576 if ( !empty( $content_struct['date_created_gmt'] ) )
1577 $dateCreated = str_replace( 'Z', '', $content_struct['date_created_gmt']->getIso() ) . 'Z'; // We know this is supposed to be GMT, so we're going to slap that Z on there by force
1578 elseif ( !empty( $content_struct['dateCreated']) )
1579 $dateCreated = $content_struct['dateCreated']->getIso();
1581 if ( !empty( $dateCreated ) ) {
1582 $post_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
1583 $post_date_gmt = iso8601_to_datetime($dateCreated, GMT);
1585 $post_date = $postdata['post_date'];
1586 $post_date_gmt = $postdata['post_date_gmt'];
1589 // We've got all the data -- post it:
1590 $newpost = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'comment_status', 'ping_status', 'post_date', 'post_date_gmt', 'to_ping', 'post_name', 'post_password', 'post_parent', 'menu_order', 'post_author', 'tags_input');
1592 $result = wp_update_post($newpost);
1594 return new IXR_Error(500, __('Sorry, your entry could not be edited. Something wrong happened.'));
1597 if ( isset($content_struct['custom_fields']) ) {
1598 $this->set_custom_fields($post_ID, $content_struct['custom_fields']);
1601 $this->attach_uploads( $ID, $post_content );
1603 logIO('O',"(MW) Edited ! ID: $post_ID");
1609 /* metaweblog.getPost ...returns a post */
1610 function mw_getPost($args) {
1612 $this->escape($args);
1614 $post_ID = (int) $args[0];
1615 $user_login = $args[1];
1616 $user_pass = $args[2];
1618 if (!$this->login_pass_ok($user_login, $user_pass)) {
1619 return $this->error;
1622 set_current_user( 0, $user_login );
1623 if( !current_user_can( 'edit_post', $post_ID ) )
1624 return new IXR_Error( 401, __( 'Sorry, you can not edit this post.' ) );
1626 do_action('xmlrpc_call', 'metaWeblog.getPost');
1628 $postdata = wp_get_single_post($post_ID, ARRAY_A);
1630 if ($postdata['post_date'] != '') {
1631 $post_date = mysql2date('Ymd\TH:i:s', $postdata['post_date']);
1632 $post_date_gmt = mysql2date('Ymd\TH:i:s', $postdata['post_date_gmt']);
1634 $categories = array();
1635 $catids = wp_get_post_categories($post_ID);
1636 foreach($catids as $catid)
1637 $categories[] = get_cat_name($catid);
1639 $tagnames = array();
1640 $tags = wp_get_post_tags( $post_ID );
1641 if ( !empty( $tags ) ) {
1642 foreach ( $tags as $tag )
1643 $tagnames[] = $tag->name;
1644 $tagnames = implode( ', ', $tagnames );
1649 $post = get_extended($postdata['post_content']);
1650 $link = post_permalink($postdata['ID']);
1652 // Get the author info.
1653 $author = get_userdata($postdata['post_author']);
1655 $allow_comments = ('open' == $postdata['comment_status']) ? 1 : 0;
1656 $allow_pings = ('open' == $postdata['ping_status']) ? 1 : 0;
1658 // Consider future posts as published
1659 if( $postdata['post_status'] === 'future' ) {
1660 $postdata['post_status'] = 'publish';
1664 'dateCreated' => new IXR_Date($post_date),
1665 'userid' => $postdata['post_author'],
1666 'postid' => $postdata['ID'],
1667 'description' => $post['main'],
1668 'title' => $postdata['post_title'],
1670 'permaLink' => $link,
1671 // commented out because no other tool seems to use this
1672 // 'content' => $entry['post_content'],
1673 'categories' => $categories,
1674 'mt_excerpt' => $postdata['post_excerpt'],
1675 'mt_text_more' => $post['extended'],
1676 'mt_allow_comments' => $allow_comments,
1677 'mt_allow_pings' => $allow_pings,
1678 'mt_keywords' => $tagnames,
1679 'wp_slug' => $postdata['post_name'],
1680 'wp_password' => $postdata['post_password'],
1681 'wp_author_id' => $author->ID,
1682 'wp_author_display_name' => $author->display_name,
1683 'date_created_gmt' => new IXR_Date($post_date_gmt),
1684 'post_status' => $postdata['post_status'],
1685 'custom_fields' => $this->get_custom_fields($post_ID)
1690 return new IXR_Error(404, __('Sorry, no such post.'));
1695 /* metaweblog.getRecentPosts ...returns recent posts */
1696 function mw_getRecentPosts($args) {
1698 $this->escape($args);
1700 $blog_ID = (int) $args[0];
1701 $user_login = $args[1];
1702 $user_pass = $args[2];
1703 $num_posts = (int) $args[3];
1705 if (!$this->login_pass_ok($user_login, $user_pass)) {
1706 return $this->error;
1709 do_action('xmlrpc_call', 'metaWeblog.getRecentPosts');
1711 $posts_list = wp_get_recent_posts($num_posts);
1714 $this->error = new IXR_Error(500, __('Either there are no posts, or something went wrong.'));
1715 return $this->error;
1718 set_current_user( 0, $user_login );
1720 foreach ($posts_list as $entry) {
1721 if( !current_user_can( 'edit_post', $entry['ID'] ) )
1724 $post_date = mysql2date('Ymd\TH:i:s', $entry['post_date']);
1725 $post_date_gmt = mysql2date('Ymd\TH:i:s', $entry['post_date_gmt']);
1727 $categories = array();
1728 $catids = wp_get_post_categories($entry['ID']);
1729 foreach($catids as $catid) {
1730 $categories[] = get_cat_name($catid);
1733 $tagnames = array();
1734 $tags = wp_get_post_tags( $entry['ID'] );
1735 if ( !empty( $tags ) ) {
1736 foreach ( $tags as $tag ) {
1737 $tagnames[] = $tag->name;
1739 $tagnames = implode( ', ', $tagnames );
1744 $post = get_extended($entry['post_content']);
1745 $link = post_permalink($entry['ID']);
1747 // Get the post author info.
1748 $author = get_userdata($entry['post_author']);
1750 $allow_comments = ('open' == $entry['comment_status']) ? 1 : 0;
1751 $allow_pings = ('open' == $entry['ping_status']) ? 1 : 0;
1753 // Consider future posts as published
1754 if( $entry['post_status'] === 'future' ) {
1755 $entry['post_status'] = 'publish';
1759 'dateCreated' => new IXR_Date($post_date),
1760 'userid' => $entry['post_author'],
1761 'postid' => $entry['ID'],
1762 'description' => $post['main'],
1763 'title' => $entry['post_title'],
1765 'permaLink' => $link,
1766 // commented out because no other tool seems to use this
1767 // 'content' => $entry['post_content'],
1768 'categories' => $categories,
1769 'mt_excerpt' => $entry['post_excerpt'],
1770 'mt_text_more' => $post['extended'],
1771 'mt_allow_comments' => $allow_comments,
1772 'mt_allow_pings' => $allow_pings,
1773 'mt_keywords' => $tagnames,
1774 'wp_slug' => $entry['post_name'],
1775 'wp_password' => $entry['post_password'],
1776 'wp_author_id' => $author->ID,
1777 'wp_author_display_name' => $author->display_name,
1778 'date_created_gmt' => new IXR_Date($post_date_gmt),
1779 'post_status' => $entry['post_status'],
1780 'custom_fields' => $this->get_custom_fields($entry['ID'])
1785 $recent_posts = array();
1786 for ($j=0; $j<count($struct); $j++) {
1787 array_push($recent_posts, $struct[$j]);
1790 return $recent_posts;
1794 /* metaweblog.getCategories ...returns the list of categories on a given blog */
1795 function mw_getCategories($args) {
1797 $this->escape($args);
1799 $blog_ID = (int) $args[0];
1800 $user_login = $args[1];
1801 $user_pass = $args[2];
1803 if (!$this->login_pass_ok($user_login, $user_pass)) {
1804 return $this->error;
1807 set_current_user( 0, $user_login );
1808 if( !current_user_can( 'edit_posts' ) )
1809 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this blog in order to view categories.' ) );
1811 do_action('xmlrpc_call', 'metaWeblog.getCategories');
1813 $categories_struct = array();
1815 if ( $cats = get_categories('get=all') ) {
1816 foreach ( $cats as $cat ) {
1817 $struct['categoryId'] = $cat->term_id;
1818 $struct['parentId'] = $cat->parent;
1819 $struct['description'] = $cat->name;
1820 $struct['categoryName'] = $cat->name;
1821 $struct['htmlUrl'] = wp_specialchars(get_category_link($cat->term_id));
1822 $struct['rssUrl'] = wp_specialchars(get_category_rss_link(false, $cat->term_id, $cat->name));
1824 $categories_struct[] = $struct;
1828 return $categories_struct;
1832 /* metaweblog.newMediaObject uploads a file, following your settings */
1833 function mw_newMediaObject($args) {
1834 // adapted from a patch by Johann Richard
1835 // http://mycvs.org/archives/2004/06/30/file-upload-to-wordpress-in-ecto/
1839 $blog_ID = (int) $args[0];
1840 $user_login = $wpdb->escape($args[1]);
1841 $user_pass = $wpdb->escape($args[2]);
1844 $name = sanitize_file_name( $data['name'] );
1845 $type = $data['type'];
1846 $bits = $data['bits'];
1848 logIO('O', '(MW) Received '.strlen($bits).' bytes');
1850 if ( !$this->login_pass_ok($user_login, $user_pass) )
1851 return $this->error;
1853 do_action('xmlrpc_call', 'metaWeblog.newMediaObject');
1855 set_current_user(0, $user_login);
1856 if ( !current_user_can('upload_files') ) {
1857 logIO('O', '(MW) User does not have upload_files capability');
1858 $this->error = new IXR_Error(401, __('You are not allowed to upload files to this site.'));
1859 return $this->error;
1862 if ( $upload_err = apply_filters( "pre_upload_error", false ) )
1863 return new IXR_Error(500, $upload_err);
1865 if(!empty($data["overwrite"]) && ($data["overwrite"] == true)) {
1866 // Get postmeta info on the object.
1867 $old_file = $wpdb->get_row("
1870 WHERE post_title = '{$name}'
1871 AND post_type = 'attachment'
1874 // Delete previous file.
1875 wp_delete_attachment($old_file->ID);
1877 // Make sure the new name is different by pre-pending the
1878 // previous post id.
1879 $filename = preg_replace("/^wpid\d+-/", "", $name);
1880 $name = "wpid{$old_file->ID}-{$filename}";
1883 $upload = wp_upload_bits($name, $type, $bits);
1884 if ( ! empty($upload['error']) ) {
1885 $errorString = sprintf(__('Could not write file %1$s (%2$s)'), $name, $upload['error']);
1886 logIO('O', '(MW) ' . $errorString);
1887 return new IXR_Error(500, $errorString);
1889 // Construct the attachment array
1890 // attach to post_id -1
1892 $attachment = array(
1893 'post_title' => $name,
1894 'post_content' => '',
1895 'post_type' => 'attachment',
1896 'post_parent' => $post_id,
1897 'post_mime_type' => $type,
1898 'guid' => $upload[ 'url' ]
1902 $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $post_id );
1903 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
1905 return apply_filters( 'wp_handle_upload', array( 'file' => $name, 'url' => $upload[ 'url' ], 'type' => $type ) );
1909 /* MovableType API functions
1910 * specs on http://www.movabletype.org/docs/mtmanual_programmatic.html
1913 /* mt.getRecentPostTitles ...returns recent posts' titles */
1914 function mt_getRecentPostTitles($args) {
1916 $this->escape($args);
1918 $blog_ID = (int) $args[0];
1919 $user_login = $args[1];
1920 $user_pass = $args[2];
1921 $num_posts = (int) $args[3];
1923 if (!$this->login_pass_ok($user_login, $user_pass)) {
1924 return $this->error;
1927 do_action('xmlrpc_call', 'mt.getRecentPostTitles');
1929 $posts_list = wp_get_recent_posts($num_posts);
1932 $this->error = new IXR_Error(500, __('Either there are no posts, or something went wrong.'));
1933 return $this->error;
1936 set_current_user( 0, $user_login );
1938 foreach ($posts_list as $entry) {
1939 if( !current_user_can( 'edit_post', $entry['ID'] ) )
1942 $post_date = mysql2date('Ymd\TH:i:s', $entry['post_date']);
1943 $post_date_gmt = mysql2date('Ymd\TH:i:s', $entry['post_date_gmt']);
1946 'dateCreated' => new IXR_Date($post_date),
1947 'userid' => $entry['post_author'],
1948 'postid' => $entry['ID'],
1949 'title' => $entry['post_title'],
1950 'date_created_gmt' => new IXR_Date($post_date_gmt)
1955 $recent_posts = array();
1956 for ($j=0; $j<count($struct); $j++) {
1957 array_push($recent_posts, $struct[$j]);
1960 return $recent_posts;
1964 /* mt.getCategoryList ...returns the list of categories on a given blog */
1965 function mt_getCategoryList($args) {
1967 $this->escape($args);
1969 $blog_ID = (int) $args[0];
1970 $user_login = $args[1];
1971 $user_pass = $args[2];
1973 if (!$this->login_pass_ok($user_login, $user_pass)) {
1974 return $this->error;
1977 set_current_user( 0, $user_login );
1978 if( !current_user_can( 'edit_posts' ) )
1979 return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this blog in order to view categories.' ) );
1981 do_action('xmlrpc_call', 'mt.getCategoryList');
1983 $categories_struct = array();
1985 if ( $cats = get_categories('hide_empty=0&hierarchical=0') ) {
1986 foreach ($cats as $cat) {
1987 $struct['categoryId'] = $cat->term_id;
1988 $struct['categoryName'] = $cat->name;
1990 $categories_struct[] = $struct;
1994 return $categories_struct;
1998 /* mt.getPostCategories ...returns a post's categories */
1999 function mt_getPostCategories($args) {
2001 $this->escape($args);
2003 $post_ID = (int) $args[0];
2004 $user_login = $args[1];
2005 $user_pass = $args[2];
2007 if (!$this->login_pass_ok($user_login, $user_pass)) {
2008 return $this->error;
2011 set_current_user( 0, $user_login );
2012 if( !current_user_can( 'edit_post', $post_ID ) )
2013 return new IXR_Error( 401, __( 'Sorry, you can not edit this post.' ) );
2015 do_action('xmlrpc_call', 'mt.getPostCategories');
2017 $categories = array();
2018 $catids = wp_get_post_categories(intval($post_ID));
2019 // first listed category will be the primary category
2021 foreach($catids as $catid) {
2022 $categories[] = array(
2023 'categoryName' => get_cat_name($catid),
2024 'categoryId' => (string) $catid,
2025 'isPrimary' => $isPrimary
2034 /* mt.setPostCategories ...sets a post's categories */
2035 function mt_setPostCategories($args) {
2037 $this->escape($args);
2039 $post_ID = (int) $args[0];
2040 $user_login = $args[1];
2041 $user_pass = $args[2];
2042 $categories = $args[3];
2044 if (!$this->login_pass_ok($user_login, $user_pass)) {
2045 return $this->error;
2048 do_action('xmlrpc_call', 'mt.setPostCategories');
2050 set_current_user(0, $user_login);
2051 if ( !current_user_can('edit_post', $post_ID) )
2052 return new IXR_Error(401, __('Sorry, you can not edit this post.'));
2054 foreach($categories as $cat) {
2055 $catids[] = $cat['categoryId'];
2058 wp_set_post_categories($post_ID, $catids);
2064 /* mt.supportedMethods ...returns an array of methods supported by this server */
2065 function mt_supportedMethods($args) {
2067 do_action('xmlrpc_call', 'mt.supportedMethods');
2069 $supported_methods = array();
2070 foreach($this->methods as $key=>$value) {
2071 $supported_methods[] = $key;
2074 return $supported_methods;
2078 /* mt.supportedTextFilters ...returns an empty array because we don't
2079 support per-post text filters yet */
2080 function mt_supportedTextFilters($args) {
2081 do_action('xmlrpc_call', 'mt.supportedTextFilters');
2082 return apply_filters('xmlrpc_text_filters', array());
2086 /* mt.getTrackbackPings ...returns trackbacks sent to a given post */
2087 function mt_getTrackbackPings($args) {
2091 $post_ID = intval($args);
2093 do_action('xmlrpc_call', 'mt.getTrackbackPings');
2095 $actual_post = wp_get_single_post($post_ID, ARRAY_A);
2097 if (!$actual_post) {
2098 return new IXR_Error(404, __('Sorry, no such post.'));
2101 $comments = $wpdb->get_results("SELECT comment_author_url, comment_content, comment_author_IP, comment_type FROM $wpdb->comments WHERE comment_post_ID = $post_ID");
2107 $trackback_pings = array();
2108 foreach($comments as $comment) {
2109 if ( 'trackback' == $comment->comment_type ) {
2110 $content = $comment->comment_content;
2111 $title = substr($content, 8, (strpos($content, '</strong>') - 8));
2112 $trackback_pings[] = array(
2113 'pingTitle' => $title,
2114 'pingURL' => $comment->comment_author_url,
2115 'pingIP' => $comment->comment_author_IP
2120 return $trackback_pings;
2124 /* mt.publishPost ...sets a post's publish status to 'publish' */
2125 function mt_publishPost($args) {
2127 $this->escape($args);
2129 $post_ID = (int) $args[0];
2130 $user_login = $args[1];
2131 $user_pass = $args[2];
2133 if (!$this->login_pass_ok($user_login, $user_pass)) {
2134 return $this->error;
2137 do_action('xmlrpc_call', 'mt.publishPost');
2139 set_current_user(0, $user_login);
2140 if ( !current_user_can('edit_post', $post_ID) )
2141 return new IXR_Error(401, __('Sorry, you can not edit this post.'));
2143 $postdata = wp_get_single_post($post_ID,ARRAY_A);
2145 $postdata['post_status'] = 'publish';
2148 $cats = wp_get_post_categories($post_ID);
2149 $postdata['post_category'] = $cats;
2150 $this->escape($postdata);
2152 $result = wp_update_post($postdata);
2159 /* PingBack functions
2160 * specs on www.hixie.ch/specs/pingback/pingback
2163 /* pingback.ping gets a pingback and registers it */
2164 function pingback_ping($args) {
2167 do_action('xmlrpc_call', 'pingback.ping');
2169 $this->escape($args);
2171 $pagelinkedfrom = $args[0];
2172 $pagelinkedto = $args[1];
2176 $pagelinkedfrom = str_replace('&', '&', $pagelinkedfrom);
2177 $pagelinkedto = str_replace('&', '&', $pagelinkedto);
2178 $pagelinkedto = str_replace('&', '&', $pagelinkedto);
2180 // Check if the page linked to is in our site
2181 $pos1 = strpos($pagelinkedto, str_replace(array('http://www.','http://','https://www.','https://'), '', get_option('home')));
2183 return new IXR_Error(0, __('Is there no link to us?'));
2185 // let's find which post is linked to
2186 // FIXME: does url_to_postid() cover all these cases already?
2187 // if so, then let's use it and drop the old code.
2188 $urltest = parse_url($pagelinkedto);
2189 if ($post_ID = url_to_postid($pagelinkedto)) {
2190 $way = 'url_to_postid()';
2191 } elseif (preg_match('#p/[0-9]{1,}#', $urltest['path'], $match)) {
2192 // the path defines the post_ID (archives/p/XXXX)
2193 $blah = explode('/', $match[0]);
2194 $post_ID = (int) $blah[1];
2195 $way = 'from the path';
2196 } elseif (preg_match('#p=[0-9]{1,}#', $urltest['query'], $match)) {
2197 // the querystring defines the post_ID (?p=XXXX)
2198 $blah = explode('=', $match[0]);
2199 $post_ID = (int) $blah[1];
2200 $way = 'from the querystring';
2201 } elseif (isset($urltest['fragment'])) {
2202 // an #anchor is there, it's either...
2203 if (intval($urltest['fragment'])) {
2204 // ...an integer #XXXX (simpliest case)
2205 $post_ID = (int) $urltest['fragment'];
2206 $way = 'from the fragment (numeric)';
2207 } elseif (preg_match('/post-[0-9]+/',$urltest['fragment'])) {
2208 // ...a post id in the form 'post-###'
2209 $post_ID = preg_replace('/[^0-9]+/', '', $urltest['fragment']);
2210 $way = 'from the fragment (post-###)';
2211 } elseif (is_string($urltest['fragment'])) {
2212 // ...or a string #title, a little more complicated
2213 $title = preg_replace('/[^a-z0-9]/i', '.', $urltest['fragment']);
2214 $sql = "SELECT ID FROM $wpdb->posts WHERE post_title RLIKE '$title'";
2215 if (! ($post_ID = $wpdb->get_var($sql)) ) {
2216 // returning unknown error '0' is better than die()ing
2217 return new IXR_Error(0, '');
2219 $way = 'from the fragment (title)';
2222 // TODO: Attempt to extract a post ID from the given URL
2223 return new IXR_Error(33, __('The specified target URL cannot be used as a target. It either doesn\'t exist, or it is not a pingback-enabled resource.'));
2225 $post_ID = (int) $post_ID;
2228 logIO("O","(PB) URL='$pagelinkedto' ID='$post_ID' Found='$way'");
2230 $post = get_post($post_ID);
2232 if ( !$post ) // Post_ID not found
2233 return new IXR_Error(33, __('The specified target URL cannot be used as a target. It either doesn\'t exist, or it is not a pingback-enabled resource.'));
2235 if ( $post_ID == url_to_postid($pagelinkedfrom) )
2236 return new IXR_Error(0, __('The source URL and the target URL cannot both point to the same resource.'));
2238 // Check if pings are on
2239 if ( 'closed' == $post->ping_status )
2240 return new IXR_Error(33, __('The specified target URL cannot be used as a target. It either doesn\'t exist, or it is not a pingback-enabled resource.'));
2242 // Let's check that the remote site didn't already pingback this entry
2243 $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$post_ID' AND comment_author_url = '$pagelinkedfrom'");
2245 if ( $wpdb->num_rows ) // We already have a Pingback from this URL
2246 return new IXR_Error(48, __('The pingback has already been registered.'));
2248 // very stupid, but gives time to the 'from' server to publish !
2251 // Let's check the remote site
2252 $linea = wp_remote_fopen( $pagelinkedfrom );
2254 return new IXR_Error(16, __('The source URL does not exist.'));
2256 $linea = apply_filters('pre_remote_source', $linea, $pagelinkedto);
2258 // Work around bug in strip_tags():
2259 $linea = str_replace('<!DOC', '<DOC', $linea);
2260 $linea = preg_replace( '/[\s\r\n\t]+/', ' ', $linea ); // normalize spaces
2261 $linea = preg_replace( "/ <(h1|h2|h3|h4|h5|h6|p|th|td|li|dt|dd|pre|caption|input|textarea|button|body)[^>]*>/", "\n\n", $linea );
2263 preg_match('|<title>([^<]*?)</title>|is', $linea, $matchtitle);
2264 $title = $matchtitle[1];
2265 if ( empty( $title ) )
2266 return new IXR_Error(32, __('We cannot find a title on that page.'));
2268 $linea = strip_tags( $linea, '<a>' ); // just keep the tag we need
2270 $p = explode( "\n\n", $linea );
2272 $preg_target = preg_quote($pagelinkedto);
2274 foreach ( $p as $para ) {
2275 if ( strpos($para, $pagelinkedto) !== false ) { // it exists, but is it a link?
2276 preg_match("|<a[^>]+?".$preg_target."[^>]*>([^>]+?)</a>|", $para, $context);
2278 // If the URL isn't in a link context, keep looking
2279 if ( empty($context) )
2282 // We're going to use this fake tag to mark the context in a bit
2283 // the marker is needed in case the link text appears more than once in the paragraph
2284 $excerpt = preg_replace('|\</?wpcontext\>|', '', $para);
2286 // prevent really long link text
2287 if ( strlen($context[1]) > 100 )
2288 $context[1] = substr($context[1], 0, 100) . '...';
2290 $marker = '<wpcontext>'.$context[1].'</wpcontext>'; // set up our marker
2291 $excerpt= str_replace($context[0], $marker, $excerpt); // swap out the link for our marker
2292 $excerpt = strip_tags($excerpt, '<wpcontext>'); // strip all tags but our context marker
2293 $excerpt = trim($excerpt);
2294 $preg_marker = preg_quote($marker);
2295 $excerpt = preg_replace("|.*?\s(.{0,100}$preg_marker.{0,100})\s.*|s", '$1', $excerpt);
2296 $excerpt = strip_tags($excerpt); // YES, again, to remove the marker wrapper
2301 if ( empty($context) ) // Link to target not found
2302 return new IXR_Error(17, __('The source URL does not contain a link to the target URL, and so cannot be used as a source.'));
2304 $pagelinkedfrom = str_replace('&', '&', $pagelinkedfrom);
2306 $context = '[...] ' . wp_specialchars( $excerpt ) . ' [...]';
2307 $pagelinkedfrom = $wpdb->escape( $pagelinkedfrom );
2309 $comment_post_ID = (int) $post_ID;
2310 $comment_author = $title;
2311 $this->escape($comment_author);
2312 $comment_author_url = $pagelinkedfrom;
2313 $comment_content = $context;
2314 $this->escape($comment_content);
2315 $comment_type = 'pingback';
2317 $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_content', 'comment_type');
2319 $comment_ID = wp_new_comment($commentdata);
2320 do_action('pingback_post', $comment_ID);
2322 return sprintf(__('Pingback from %1$s to %2$s registered. Keep the web talking! :-)'), $pagelinkedfrom, $pagelinkedto);
2326 /* pingback.extensions.getPingbacks returns an array of URLs
2327 that pingbacked the given URL
2328 specs on http://www.aquarionics.com/misc/archives/blogite/0198.html */
2329 function pingback_extensions_getPingbacks($args) {
2333 do_action('xmlrpc_call', 'pingback.extensions.getPingsbacks');
2335 $this->escape($args);
2339 $post_ID = url_to_postid($url);
2341 // We aren't sure that the resource is available and/or pingback enabled
2342 return new IXR_Error(33, __('The specified target URL cannot be used as a target. It either doesn\'t exist, or it is not a pingback-enabled resource.'));
2345 $actual_post = wp_get_single_post($post_ID, ARRAY_A);
2347 if (!$actual_post) {
2348 // No such post = resource not found
2349 return new IXR_Error(32, __('The specified target URL does not exist.'));
2352 $comments = $wpdb->get_results("SELECT comment_author_url, comment_content, comment_author_IP, comment_type FROM $wpdb->comments WHERE comment_post_ID = $post_ID");
2358 $pingbacks = array();
2359 foreach($comments as $comment) {
2360 if ( 'pingback' == $comment->comment_type )
2361 $pingbacks[] = $comment->comment_author_url;
2369 $wp_xmlrpc_server = new wp_xmlrpc_server();