Location: PHPKode > projects > Buzzword > buzzword-0.8.12/contrib/blogger.php
<?php

/*
 * buzzword
 * Copyright (c) 2003 Brad Taylor, Jon Tai
 *
 * $Id: blogger.php,v 1.13 2003/12/10 00:23:27 jon Exp $
 * Implement the Blogger API
 *
 * This file is part of buzzword.
 *
 * buzzword is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * buzzword is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with buzzword; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

require_once '../includes/config.inc';

// error responses
$responses = array(
    'authFailed' => array(
        'faultCode'   => (int) 4,
        'faultString' => (string) 'Error: User authentication failed',
    ),
    'invalidPostId' => array(
        'faultCode'   => (int) 4,
        'faultString' => (string) 'Error: No such postid',
    ),
    'methodNotImplemented' => array(
        'faultCode'   => (int) 4,
        'faultString' => (string) 'Error: Method not implemented',
    ),
);

// create an xmlrpc server
if (!$server = xmlrpc_server_create())
    die('unable to create xmlrpc server');

// register functions to be exposed as xmlrpc methods
xmlrpc_server_register_method($server, 'blogger.newPost', 'buzzword_xmlrpc_newPost');
xmlrpc_server_register_method($server, 'blogger.editPost', 'buzzword_xmlrpc_editPost');
xmlrpc_server_register_method($server, 'blogger.getPost', 'buzzword_xmlrpc_getPost');
xmlrpc_server_register_method($server, 'blogger.getRecentPosts', 'buzzword_xmlrpc_getRecentPosts');
xmlrpc_server_register_method($server, 'blogger.deletePost', 'buzzword_xmlrpc_deletePost');
xmlrpc_server_register_method($server, 'blogger.getUsersBlogs', 'buzzword_xmlrpc_getUsersBlogs');
xmlrpc_server_register_method($server, 'blogger.getUserInfo', 'buzzword_xmlrpc_getUserInfo');
xmlrpc_server_register_method($server, 'blogger.getTemplate', 'buzzword_xmlrpc_getTemplate');
xmlrpc_server_register_method($server, 'blogger.setTemplate', 'buzzword_xmlrpc_setTemplate');

// workaround for a PHP bug in some versions... 
header('Content-Type: text/xml');

// call requested method and print response
echo xmlrpc_server_call_method($server, $GLOBALS['HTTP_RAW_POST_DATA'], $responses);

// clean up
xmlrpc_server_destroy($server);

function buzzword_xmlrpc_newPost($method_name, $params, $responses) {
    $appkey = FALSE;
    $blogid = FALSE;
    $username = $params[2];
    $password = $params[3];
    $content = $params[4];
    $publish = $params[5];

    // authenticate user
    if (md5($password) != ADMIN_PASSWORD)
        return $responses['authFailed'];

    define('ADMIN_LOGGED_IN', TRUE);

    // create entry
    $entry = new blog_entry(mysql_unique_key(DB_PREFIX.'blog_entries', 'entry_key'));
    $entry->created = time();
    $entry->modified = $entry->created;
    $entry->is_private = (bool) (!$publish);
    $entry->is_hidden = FALSE;
    $entry->title = 'posted via blogger';
    $entry->body = $content;
    $entry->allow_comments = TRUE;
    $entry->create();

    // return postid
    return (string) $entry->entry_key;
}

function buzzword_xmlrpc_editPost($method_name, $params, $responses) {
    $appkey = FALSE;
    $postid = $params[1];
    $username = $params[2];
    $password = $params[3];
    $content = $params[4];
    $publish = $params[5];

    // authenticate user
    if (md5($password) != ADMIN_PASSWORD)
        return $responses['authFailed'];

    define('ADMIN_LOGGED_IN', TRUE);

    // fetch entry from database
    $entry = new blog_entry((int) $postid);

    // entry doesn't exist
    if (!$entry->exists())
        return $responses['invalidPostId'];

    // update entry
    $entry->modified = time();
    $entry->is_private = (bool) (!$publish);
    $entry->body = $content;
    $entry->update();

    // return true
    return (bool) TRUE;
}

function buzzword_xmlrpc_getPost($method_name, $params, $responses) {
    $appkey = FALSE;
    $postid = $params[1];
    $username = $params[2];
    $password = $params[3];

    // authenticate user
    if (md5($password) != ADMIN_PASSWORD)
        return $responses['authFailed'];

    define('ADMIN_LOGGED_IN', TRUE);

    // fetch entry from database
    $entry = new blog_entry((int) $postid);

    // entry doesn't exist
    if (!$entry->exists())
        return $responses['invalidPostId'];

    // return entry
    $date_created = date('Ymd\TH:i:s', $entry->created);
    xmlrpc_set_type($date_created, 'datetime');

    return array(
        'content' => (string) $entry->body,
        'userid'  => (string) 1,
        'postid'  => (string) $entry->entry_key,
        'dateCreated' => $date_created,
    );
}

function buzzword_xmlrpc_getRecentPosts($method_name, $params, $responses) {
    $appkey = FALSE;
    $blogid = FALSE;
    $username = $params[2];
    $password = $params[3];
    $numposts = $params[4];

    // authenticate user
    if (md5($password) != ADMIN_PASSWORD)
        return $responses['authFailed'];

    define('ADMIN_LOGGED_IN', TRUE);

    // fetch entries from database
    $return_entries = array();
    $entries = get_blog_entries_by_date(0, time(), $numposts);
    foreach ($entries as $entry) {
        $date_created = date('Ymd\TH:i:s', $entry->created);
        xmlrpc_set_type($date_created, 'datetime');

        $return_entries[] = array(
            'content' => (string) $entry->body,
            'userid'  => (string) 1,
            'postid'  => (string) $entry->entry_key,
            'dateCreated' => $date_created,
        );
    }

    // return entries
    return $return_entries;
}

function buzzword_xmlrpc_deletePost($method_name, $params, $responses) {
    $appkey = FALSE;
    $postid = $params[1];
    $username = $params[2];
    $password = $params[3];
    $publish = $params[4];

    // authenticate user
    if (md5($password) != ADMIN_PASSWORD)
        return $responses['authFailed'];

    define('ADMIN_LOGGED_IN', TRUE);

    // fetch entry from database
    $entry = new blog_entry((int) $postid);

    // entry doesn't exist
    if (!$entry->exists())
        return $responses['invalidPostId'];

    // destroy entry
    $entry->destroy();

    // return true
    return (bool) TRUE;
}

function buzzword_xmlrpc_getUsersBlogs($method_name, $params, $responses) {
    $appkey = FALSE;
    $username = $params[1];
    $password = $params[2];

    // authenticate user
    if (md5($password) != ADMIN_PASSWORD)
        return $responses['authFailed'];

    // return array containing a single element
    return array(
        array(
            'url'      => (string) 'http://'.$_SERVER['HTTP_HOST'],
            'blogid'   => (string) 1,
            'blogName' => (string) 'Blog',
        ),
    );
}

function buzzword_xmlrpc_getUserInfo($method_name, $params, $responses) {
    $appkey = FALSE;
    $username = $params[1];
    $password = $params[2];

    // authenticate user
    if (md5($password) != ADMIN_PASSWORD)
        return $responses['authFailed'];

    // return array containing dummy information
    return array(
        'userid'    => (string) 1,
        'firstname' => (string) $username,
        'lastname'  => (string) $username,
        'nickname'  => (string) $username,
        'email'     => (string) $username.'@'.$_SERVER['HTTP_HOST'],
        'url'       => (string) 'http://'.$_SERVER['HTTP_HOST'],
    );
}

// unimplemented
function buzzword_xmlrpc_getTemplate($method_name, $params, $responses) {
    return $responses['methodNotImplemented'];
}

// unimplemented
function buzzword_xmlrpc_setTemplate($method_name, $params, $responses) {
    return $responses['methodNotImplemented'];
}

?>
Return current item: Buzzword