; } /* mt.getPostCategories ...returns a post's categories */ function mt_getPostCategories($args) { $this->escape($args); $post_ID = (int) $args[0]; $user_login = $args[1]; $user_pass = $args[2]; if (!$this->login_pass_ok($user_login, $user_pass)) { return $this->error; } set_current_user( 0, $user_login ); if( !current_user_can( 'edit_post', $post_ID ) ) return new IXR_Error( 401, __( 'Sorry, you can not edit this post.' ) ); do_action('xmlrpc_call', 'mt.getPostCategories'); $categories = array(); $catids = wp_get_post_categories(intval($post_ID)); // first listed category will be the primary category $isPrimary = true; foreach($catids as $catid) { $categories[] = array( 'categoryName' => get_cat_name($catid), 'categoryId' => (string) $catid, 'isPrimary' => $isPrimary ); $isPrimary = false; } return $categories; } /* mt.setPostCategories ...sets a post's categories */ function mt_setPostCategories($args) { $this->escape($args); $post_ID = (int) $args[0]; $user_login = $args[1]; $user_pass = $args[2]; $categories = $args[3]; if (!$this->login_pass_ok($user_login, $user_pass)) { return $this->error; } do_action('xmlrpc_call', 'mt.setPostCategories'); set_current_user(0, $user_login); if ( !current_user_can('edit_post', $post_ID) ) return new IXR_Error(401, __('Sorry, you can not edit this post.')); foreach($categories as $cat) { $catids[] = $cat['categoryId']; } wp_set_post_categories($post_ID, $catids); return true; } /* mt.supportedMethods ...returns an array of methods supported by this server */ function mt_supportedMethods($args) { do_action('xmlrpc_call', 'mt.supportedMethods'); $supported_methods = array(); foreach($this->methods as $key=>$value) { $supported_methods[] = $key; } return $supported_methods; } /* mt.supportedTextFilters ...returns an empty array because we don't support per-post text filters yet */ function mt_supportedTextFilters($args) { do_action('xmlrpc_call', 'mt.supportedTextFilters'); return apply_filters('xmlrpc_text_filters', array()); } /* mt.getTrackbackPings ...returns trackbacks sent to a given post */ function mt_getTrackbackPings($args) { global $wpdb; $post_ID = intval($args); do_action('xmlrpc_call', 'mt.getTrackbackPings'); $actual_post = wp_get_single_post($post_ID, ARRAY_A); if (!$actual_post) { return new IXR_Error(404, __('Sorry, no such post.')); } $comments = $wpdb->get_results( $wpdb->prepare("SELECT comment_author_url, comment_content, comment_author_IP, comment_type FROM $wpdb->comments WHERE comment_post_ID = %d", $post_ID) ); if (!$comments) { return array(); } $trackback_pings = array(); foreach($comments as $comment) { if ( 'trackback' == $comment->comment_type ) { $content = $comment->comment_content; $title = substr($content, 8, (strpos($content, '') - 8)); $trackback_pings[] = array( 'pingTitle' => $title, 'pingURL' => $comment->comment_author_url, 'pingIP' => $comment->comment_author_IP ); } } return $trackback_pings; } /* mt.publishPost ...sets a post's publish status to 'publish' */ function mt_publishPost($args) { $this->escape($args); $post_ID = (int) $args[0]; $user_login = $args[1]; $user_pass = $args[2]; if (!$this->login_pass_ok($user_login, $user_pass)) { return $this->error; } do_action('xmlrpc_call', 'mt.publishPost'); set_current_user(0, $user_login); if ( !current_user_can('edit_post', $post_ID) ) return new IXR_Error(401, __('Sorry, you can not edit this post.')); $postdata = wp_get_single_post($post_ID,ARRAY_A); $postdata['post_status'] = 'publish'; // retain old cats $cats = wp_get_post_categories($post_ID); $postdata['post_category'] = $cats; $this->escape($postdata); $result = wp_update_post($postdata); return $result; } /* PingBack functions * specs on www.hixie.ch/specs/pingback/pingback */ /* pingback.ping gets a pingback and registers it */ function pingback_ping($args) { global $wpdb; do_action('xmlrpc_call', 'pingback.ping'); $this->escape($args); $pagelinkedfrom = $args[0]; $pagelinkedto = $args[1]; $title = ''; $pagelinkedfrom = str_replace('&', '&', $pagelinkedfrom); $pagelinkedto = str_replace('&', '&', $pagelinkedto); $pagelinkedto = str_replace('&', '&', $pagelinkedto); // Check if the page linked to is in our site $pos1 = strpos($pagelinkedto, str_replace(array('http://www.','http://','https://www.','https://'), '', get_option('home'))); if( !$pos1 ) return new IXR_Error(0, __('Is there no link to us?')); // let's find which post is linked to // FIXME: does url_to_postid() cover all these cases already? // if so, then let's use it and drop the old code. $urltest = parse_url($pagelinkedto); if ($post_ID = url_to_postid($pagelinkedto)) { $way = 'url_to_postid()'; } elseif (preg_match('#p/[0-9]{1,}#', $urltest['path'], $match)) { // the path defines the post_ID (archives/p/XXXX) $blah = explode('/', $match[0]); $post_ID = (int) $blah[1]; $way = 'from the path'; } elseif (preg_match('#p=[0-9]{1,}#', $urltest['query'], $match)) { // the querystring defines the post_ID (?p=XXXX) $blah = explode('=', $match[0]); $post_ID = (int) $blah[1]; $way = 'from the querystring'; } elseif (isset($urltest['fragment'])) { // an #anchor is there, it's either... if (intval($urltest['fragment'])) { // ...an integer #XXXX (simpliest case) $post_ID = (int) $urltest['fragment']; $way = 'from the fragment (numeric)'; } elseif (preg_match('/post-[0-9]+/',$urltest['fragment'])) { // ...a post id in the form 'post-###' $post_ID = preg_replace('/[^0-9]+/', '', $urltest['fragment']); $way = 'from the fragment (post-###)'; } elseif (is_string($urltest['fragment'])) { // ...or a string #title, a little more complicated $title = preg_replace('/[^a-z0-9]/i', '.', $urltest['fragment']); $sql = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title RLIKE %s", $title); if (! ($post_ID = $wpdb->get_var($sql)) ) { // returning unknown error '0' is better than die()ing return new IXR_Error(0, ''); } $way = 'from the fragment (title)'; } } else { // TODO: Attempt to extract a post ID from the given URL 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.')); } $post_ID = (int) $post_ID; logIO("O","(PB) URL='$pagelinkedto' ID='$post_ID' Found='$way'"); $post = get_post($post_ID); if ( !$post ) // Post_ID not found 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.')); if ( $post_ID == url_to_postid($pagelinkedfrom) ) return new IXR_Error(0, __('The source URL and the target URL cannot both point to the same resource.')); // Check if pings are on if ( 'closed' == $post->ping_status ) 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.')); // Let's check that the remote site didn't already pingback this entry $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_author_url = %s", $post_ID, $pagelinkedfrom) ); if ( $wpdb->num_rows ) // We already have a Pingback from this URL return new IXR_Error(48, __('The pingback has already been registered.')); // very stupid, but gives time to the 'from' server to publish ! sleep(1); // Let's check the remote site $linea = wp_remote_fopen( $pagelinkedfrom ); if ( !$linea ) return new IXR_Error(16, __('The source URL does not exist.')); $linea = apply_filters('pre_remote_source', $linea, $pagelinkedto); // Work around bug in strip_tags(): $linea = str_replace(']*>/", "\n\n", $linea ); preg_match('|