<?php

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

class Web2 {
	
	public function __construct() {
		
	}
	
	public function PostMetaWebLog( $domain, $endpoint, $blog_id, $username, $password, $title, $content ) {
		$meta = new MetaWebLogAPI( $domain, $endpoint, $blog_id, $username, $password );
		$meta->Post( $title, $content );
	}
	
	public function PostOwnWordpress( $domain, $folder, $username, $password, $title, $content ) {
		$this->PostMetaWebLog( $domain, "$folder/xmlrpc.php", 1, $username, $password, $title, $content );
	}
	
	public function PostWordpress( $username, $password, $prefix, $title, $content ) {
		$this->PostMetaWebLog( "$prefix.wordpress.com", '/xmlrpc.php', 1, $username, $password, $title, $content );
	}
	
	public function PostXanga( $username, $password, $title, $content ) {
		$this->PostMetaWebLog( 'api.xanga.com', '/metaweblogapi', 1, $username, $password, $title, $content );
	}
	
	public function PostTypePad( $username, $password, $blog_id, $title, $content ) {
		$this->PostMetaWebLog( 'www.typepad.com', '/t/api', $blog_id, $username, $password, $title, $content );
	}
	
	public function PostBlogger( $username, $password, $title, $content ) {
		$blogger = new Blogger( $username, $password );
		$blogger->createPost( $title, $content, False );
	}
	
	public function PostVox( $vox_email, $title, $content ) {
		mail( $vox_email, $title, $content );
	}
	
	public function PostPosterous( $posterous_email, $posterous_password, $title, $content ) {
		$url = 'http://posterous.com/api/newpost';

		$request_data = http_build_query(
			array(
				'title' => $title,
				'body' => $content
			)
		);
		
		$c = curl_init( $url );
		curl_setopt($c, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ; 
		curl_setopt($c, CURLOPT_USERPWD, "$posterous_email:$posterous_password"); 
		curl_setopt($c, CURLOPT_POST, true);
		curl_setopt($c, CURLOPT_POSTFIELDS, $request_data);
		curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
		$result = curl_exec($c);
		$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
		curl_close($c);
	}
	
	public function PostLiveJournal( $username, $password, $title, $content ) {
		$req = new xmlrpcmsg( 'LJ.XMLRPC.postevent' );

		$struct = new xmlrpcval( array(
			'username' => new xmlrpcval( $username, 'string' ),
			'password' => new xmlrpcval( $password, 'string' ),
			'event' => new xmlrpcval( $content, 'string' ),
			'subject' => new xmlrpcval( $title, 'string' ),
			'lineendings' => new xmlrpcval( 'pc', 'string' ),
			'year' => new xmlrpcval( date( 'Y' ), 'int' ),
			'mon' => new xmlrpcval( date( 'n' ), 'int' ),
			'day' => new xmlrpcval( date( 'j' ), 'int' ),
			'hour' => new xmlrpcval( date( 'G' ), 'int'),
			'min' => new xmlrpcval( date( 'i' ), 'int' )
		), 'struct' );

		$req->addParam( $struct );

		$cl = new xmlrpc_client( '/interface/xmlrpc', 'livejournal.com', 80 );
		$cl->send( $req );
	}
	
	public function PostTumblr( $email, $password, $title, $content ) {
		// Prepare POST request
		$request_data = http_build_query(
		    array(
		        'email'     => $email,
		        'password'  => $password,
		        'type'      => 'regular',
		        'title'     => $title,
		        'body'      => $content,
		        'generator' => 'API'
		    )
		);
		
		// Send the POST request (with cURL)
		$c = curl_init('http://www.tumblr.com/api/write');
		curl_setopt($c, CURLOPT_POST, true);
		curl_setopt($c, CURLOPT_POSTFIELDS, $request_data);
		curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
		$result = curl_exec($c);
		$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
		curl_close($c);
	}
	
	public function PostMultiply( $username, $password, $title, $content ) {
		$url = "http://$username.multiply.com/api/post";

		$request_data = http_build_query(
			array(
				'api_key' => '3wZftoA',
				'password' => $password,
				'type' => 'journal',
				'subject' => $title,
				'body' => $content
			)
		);
		
		$c = curl_init( $url );
		curl_setopt($c, CURLOPT_POST, true);
		curl_setopt($c, CURLOPT_POSTFIELDS, $request_data);
		curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
		$result = curl_exec($c);
		$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
		curl_close($c);
	}
	
	public function TestMetaWebLog( $domain, $endpoint, $blog_id, $username, $password ) {
		$meta = new MetaWebLogAPI( $domain, $endpoint, $blog_id, $username, $password );
		return $meta->Test();
	}
	
	public function TestWordpress( $username, $password, $prefix ) {
		return $this->TestMetaWebLog( "$prefix.wordpress.com", '/xmlrpc.php', null, $username, $password );
	}
	
	public function TestOwnWordpress1( $domain, $folder, $username, $password ) {
		return $this->TestMetaWebLog( $domain, "$folder/xmlrpc.php", null, $username, $password );
	}
	
	public function TestOwnWordpress2( $domain, $folder, $username, $password ) {
		return $this->TestMetaWebLog( $domain, "$folder/xmlrpc.php", null, $username, $password );
	}
	
	public function TestOwnWordpress3( $domain, $folder, $username, $password ) {
		return $this->TestMetaWebLog( $domain, "$folder/xmlrpc.php", null, $username, $password );
	}
	
	public function TestOwnWordpress4( $domain, $folder, $username, $password ) {
		return $this->TestMetaWebLog( $domain, "$folder/xmlrpc.php", null, $username, $password );
	}
	
	public function TestOwnWordpress5( $domain, $folder, $username, $password ) {
		return $this->TestMetaWebLog( $domain, "$folder/xmlrpc.php", null, $username, $password );
	}
	
	public function TestOwnWordpress6( $domain, $folder, $username, $password ) {
		return $this->TestMetaWebLog( $domain, "$folder/xmlrpc.php", null, $username, $password );
	}
	
	public function TestOwnWordpress7( $domain, $folder, $username, $password ) {
		return $this->TestMetaWebLog( $domain, "$folder/xmlrpc.php", null, $username, $password );
	}
	
	public function TestOwnWordpress8( $domain, $folder, $username, $password ) {
		return $this->TestMetaWebLog( $domain, "$folder/xmlrpc.php", null, $username, $password );
	}
	
	public function TestOwnWordpress9( $domain, $folder, $username, $password ) {
		return $this->TestMetaWebLog( $domain, "$folder/xmlrpc.php", null, $username, $password );
	}
	
	public function TestOwnWordpress10( $domain, $folder, $username, $password ) {
		return $this->TestMetaWebLog( $domain, "$folder/xmlrpc.php", null, $username, $password );
	}
	
	public function TestTypepad( $username, $password, $blog_id ) {
		$meta = new MetaWebLogAPI( "www.typepad.com", '/t/api', $blog_id, $username, $password );
		return $meta->TestGetPosts();
	}
	
	public function TestBlogger( $username, $password ) {
		try {
			$blogger = new Blogger( $username, $password );
			return true;
		} catch ( Exception $ex ) {
			return false;
		}
	}
	
	public function TestLiveJournal ( $username, $password ) {
		$req = new xmlrpcmsg( 'LJ.XMLRPC.login' );

		$struct = new xmlrpcval( array(
			'username' => new xmlrpcval( $username, 'string' ),
			'password' => new xmlrpcval( $password, 'string' ),
		), 'struct' );

		$req->addParam( $struct );

		$cl = new xmlrpc_client( '/interface/xmlrpc', 'livejournal.com', 80, 'http11' );
		$ans = $cl->send( $req );
		
		try {
			if ( $ans->val == false ) {
				return false;
			}
			
			return true;
		} catch ( Exception $ex ) {
			return false;
		}
	}
	
	public function TestMultiply( $username, $password ) {
		$url = "http://$username.multiply.com/api/read";

		$request_data = http_build_query(
			array(
				'api_key' => '3wZftoA',
				'password' => $password,
				'type' => 'journal'
			)
		);
		
		$c = curl_init( $url );
		curl_setopt($c, CURLOPT_POST, true);
		curl_setopt($c, CURLOPT_POSTFIELDS, $request_data);
		curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
		$result = curl_exec($c);
		$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
		
		if ( $status == 200 ) {
			return true;
		}
		
		return false;
	}
	
	public function TestXanga( $username, $password ) {
		$meta = new MetaWebLogAPI( 'api.xanga.com', '/metaweblogapi', 1, $username, $password );
		return $meta->TestGetPosts();
	}
	
	public function TestTumblr( $username, $password ) {
		// Prepare POST request
		$request_data = http_build_query(
		    array(
		        'email'     => $username,
		        'password'  => $password
		    )
		);
		
		// Send the POST request (with cURL)
		$c = curl_init('http://www.tumblr.com/api/authenticate');
		curl_setopt($c, CURLOPT_POST, true);
		curl_setopt($c, CURLOPT_POSTFIELDS, $request_data);
		curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
		$result = curl_exec($c);
		$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
		curl_close($c);
		
		if ( $result == 'Invalid credentials.' ) {
			return false;
		}
		
		return true;
	}
	
	public function TestPosterous( $email, $password ) {
		$url = 'http://posterous.com/api/getsites';

		$c = curl_init( $url );
		curl_setopt($c, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ; 
		curl_setopt($c, CURLOPT_USERPWD, "$email:$password"); 
		curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
		$result = curl_exec($c);
		$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
		curl_close($c);
		
		return $status == 200;
	}
	
}

?>