<?php

require_once dirname( __FILE__ ) . '/lib/xmlrpc.inc';
require_once dirname( __FILE__ ) . '/lib/rss_fetch.inc';

class MetaWebLogAPI {
	
	private $domain;
	private $endpoint;
	private $blog_id;
	private $username;
	private $password;
	
	public function __construct( $domain, $endpoint, $blog_id, $username, $password ) {
		$this->domain = $domain;
		$this->endpoint = $endpoint;
		$this->blog_id = $blog_id;
		$this->username = $username;
		$this->password = $password;
	}
	
	public function Post( $title, $content ) {
		$req = new xmlrpcmsg( 'metaWeblog.newPost' );
		
		$req->addParam( new xmlrpcval( $this->blog_id, 'string' ) );
		$req->addParam( new xmlrpcval( $this->username, 'string' ) );
		$req->addParam( new xmlrpcval( $this->password, 'string' ) );
		
		$struct = new xmlrpcval( array(
			'title' => new xmlrpcval( $title, 'string' ),
			'description' => new xmlrpcval( $content, 'string')
		), 'struct' );
		
		$req->addParam( $struct );
		$req->addParam( new xmlrpcval( true, 'boolean' ) );
		
		$cl = new xmlrpc_client( $this->endpoint, $this->domain, 80 );
		$ans = $cl->send( $req );
	}
	
	public function Test() {
		$req = new xmlrpcmsg( 'metaWeblog.getUsersBlogs' );
		
		$req->addParam( new xmlrpcval( $this->blog_id, 'string' ) );
		$req->addParam( new xmlrpcval( $this->username, 'string' ) );
		$req->addParam( new xmlrpcval( $this->password, 'string' ) );
		
		$cl = new xmlrpc_client( $this->endpoint, $this->domain, 80, 'http11' );
		$ans = $cl->send( $req );
		try {
			if ( $ans->val == false ) {
				return false;
			}
			
			return true;
		} catch ( Exception $ex ) {
			return false;
		}
	}
	
	public function TestGetPosts() {
		$req = new xmlrpcmsg( 'metaWeblog.getRecentPosts' );
		
		$req->addParam( new xmlrpcval( $this->blog_id, 'string' ) );
		$req->addParam( new xmlrpcval( $this->username, 'string' ) );
		$req->addParam( new xmlrpcval( $this->password, 'string' ) );
		$req->addParam( new xmlrpcval( 1, 'int' ) );
		
		$cl = new xmlrpc_client( $this->endpoint, $this->domain, 80, 'http11' );
		$ans = $cl->send( $req );
		try {
			if ( $ans->val == false ) {
				return false;
			}
			
			return true;
		} catch ( Exception $ex ) {
			return false;
		}
	}
	
}

?>