SyncCode I4 class.WordRPC.inc.php
From MWWiki
Back to WordPress Synchronizer
<?php defined('WS') or die('Restricted access'); include_once('lib/FirePHPCore/fb.php'); include_once('lib/FirePHPCore/FirePHP.class.php'); /* This file provides XMLRPC capabilities to the WordPress uploader */ class WordRPC { /* private variables */ private $url; // url to connect to xmlrpc private $username; // login name private $password; // user's password private $blogID; // automatically obtained, specifies the user in the blog private $error; // flag that is set if an error occurs private $errormsg; // if an error occurs, this contains any accompanying message private $firephp; // for debugging /* constructor */ /** * Constructor. * * @param string $url The URL to the XML-RPC remote blog. * @param string $user The username to use to login. * @param string $pass The username's associated password. */ public function __construct($url, $user, $pass) { $this->url = $url; $this->username = $user; $this->password = $pass; $this->error = false; $this->blogID = $this->getBlogID(); $this->firephp = FirePHP::getInstance(true); } /** * Since many of the APIs require the user's blog ID in order to function, * this method handles its retrieval when the RPC is constructed. * */ private function getBlogID() { $id = $this->RPCRequest("wp.getUsersBlogs", array($this->username, $this->password)); return ($this->error() ? false : $id[0]['blogid']); } /* member functions */ /** * Returns a boolean indicating if an error occurred. * * @return bool True if an error occurred, false otherwise. */ public function error() { return $this->error; } /** * If an error occurred, this returns the error message. * * @return string The error message. */ public function errorMsg() { return $this->errormsg; } /** * Submit a new post to the RPC server. * * @param struct * @return An integer indicating the postid of the new entry. */ public function newPost($struct) { return $this->RPCRequest("metaWeblog.newPost", array($this->blogID, $this->username, $this->password, $struct, true)); } /** * This function edits a post with the given post ID. * * @param postID The ID of the post to be edited. * @param struct The array containing all the data pertaining to the post. * @return True if the edit is successful, otherwise an error is returned. */ public function editPost($postID, $struct) { return $this->RPCRequest("metaWeblog.editPost", array($postID, $this->username, $this->password, $struct, true)); } /** * Sets new categories for the given post. * * @param int $postID Integer ID of the post * @param array $categories Array of categories with their integer IDs * @return True on success, false on failure */ public function setCategories($postID, $categories) { return $this->RPCRequest("mt.setPostCategories", array($postID, $this->username, $this->password, $categories)); } /** * This will return an array of basic information regarding the most * recent posts: date created, userID, postID, and title * * @param num_posts The number of most recent posts to return. If this * parameter is not provided, 0 is the default, which * returns ALL posts. * @return An associative array containing num_posts most recent posts with * the following fields: * dateCreated; userid; postid; title; date_created_gmt */ public function getPostIDs($num_posts = 1) { return $this->RPCRequest("mt.getRecentPostTitles", array($this->blogID, $this->username, $this->password, $num_posts)); } /** * Returns all of the latest posts, according to the metaWeblog * return struct. * * @param num_posts The number of most recent posts to retrieve. * @return An associative array containing num_posts most recent * posts with the metaweblog struct. */ public function getRecentPosts($num_posts = 1) { return $this->RPCRequest("metaWeblog.getRecentPosts", array($this->blogID, $this->username, $this->password, $num_posts)); } /** * This function returns all the post information given a particular ID. * * @param postID The ID of a post for which we want to retrieve information. * @return An associative array containing all the necessary post information. */ public function getPost($postID) { return $this->RPCRequest("metaWeblog.getPost", array($postID, $this->username, $this->password)); } /** * This function deletes a post from the remote server. * * @param postID The ID of a post to be deleted. * @return True if the deletion was successful; an error message otherwise. */ public function deletePost($postID) { return $this->RPCRequest("metaWeblog.deletePost", array("null", $postID, $this->username, $this->password, 1)); } /** * Retrieves a list of all the categories on the remote blog. * * @return array An array of categories. * each element: {[categoryId], [parentId], [description], [categoryName], [htmlUrl], [rssUrl]} */ public function getCategories() { return $this->RPCRequest("wp.getCategories", array($this->blogID, $this->username, $this->password)); } /** * Adds a new category to the remote blog. * * @param array $toAdd Struct with the necessary category information. * $toAdd: {[name], [slug], [parent_id], [description]} * @return An integer corresponding to the ID of the new category. */ public function addCategory($toAdd) { return $this->RPCRequest("wp.newCategory", array($this->blogID, $this->username, $this->password, $toAdd)); } /** * Removes the specified category from the remote blog. * * @param int $catid Integer ID of the category to delete. * @return unknown */ public function deleteCategory($catid) { return $this->RPCRequest("wp.deleteCategory", array($this->blogID, $this->username, $this->password, $catid)); } /** * This function encapsulates all the XML-RPC implementation-specific protocols * * @param method The name of the RPC method to call * @param params An array of basic PHP parameters * @return An array of PHP variables pertaining to the server response, or false on failure. */ public function RPCRequest($method, $params) { $request = xmlrpc_encode_request($method, $params); $context = stream_context_create(array('http' => array( 'method' => "POST", 'header' => "Content-Type: text/xml\r\n", 'content' => $request ))); $file = @file_get_contents($this->url, false, $context); if ($file === false) { // file_get_contents failed $this->error = true; $this->errormsg = 'An unexpected error occurred. Please check your connection settings.'; return false; } // reaching this point does not necessarily mean the transaction was successful, // it merely implies that a connection was successfully established. an error // could still have occurred that was handled by the XML-RPC layer itself $results = xmlrpc_decode($file); if (is_array($results) && isset($results['faultString'])) { $this->error = true; $this->errormsg = $results['faultString']; } else { $this->error = false; } return $results; } } ?>
