SyncCode I3 class.WordRPC.inc.php

From MWWiki

Jump to: navigation, search

Back to WordPress Synchronizer

<?php
 
defined('WS') or die('Restricted access');
 
/* This file provides XMLRPC capabilities to the WordPress uploader */
 
class WordRPC {
 
  /* private variables */
 
  private $url;
  private $username;
  private $password;
  private $blogID;
 
  /* constructor */
 
  public function __construct($url, $user, $pass) {
    $this->url = $url;
    $this->username = $user;
    $this->password = $pass;
 
    $id = $this->RPCRequest("blogger.getUserInfo", 
                            array("null", $this->username, $this->password));
    $this->blogID = $id['userid'];
  }
 
  /* member functions */
 
  /**
   * 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
   */
  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",
                      'content' => $request
    )));
    $file = file_get_contents($this->url, false, $context);
    return xmlrpc_decode($file);
  }
 
  /**
   * Submit a new post to the RPC server.
   *
   * @param struct
   * @return An integer indicating the postid of the new entry.
   */
  public function newPost($struct) {
    $struct['userid'] = $this->blogID;
    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 content 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, $content) {
    return $this->RPCRequest("metaWeblog.editPost", array($postID, 
                             $this->username, $this->password, $content, true)); 
  } 
 
  /**
   * 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 = 0) {
    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("blogger.deletePost",
                              array("null", $postID, $this->username, $this->password, 1));
  }
}
 
 
?>