silverstripe-blog/code/MetaWeblogController.php
Damian Mooyman 477f857acf API Updated blog module to adhere to new rules around configuration. Static configurable properties (db, has_one, etc) are now private.
Also removed trailing ?>(newline) from various files, and cleaned up some redundant code (empty has_ones, etc).
Also noted in the code are places where various static properties should be refactored out in favour of using the Silverstripe configuration system instead. For now the bare mimimum work has been done in order to make the module work in 3.1
2013-04-02 11:38:09 +13:00

103 lines
2.4 KiB
PHP

<?php
require_once(BASE_PATH . '/blog/thirdparty/xmlrpc/xmlrpc.php');
require_once(BASE_PATH . '/blog/thirdparty/xmlrpc/xmlrpcs.php');
require_once(BASE_PATH . '/blog/thirdparty/xmlrpc/xmlrpc_wrappers.php');
/**
* MetaWeblogController provides the MetaWeblog API for SilverStripe blogs.
*/
class MetaWeblogController extends Controller {
function index($request) {
// Create an xmlrpc server, and set up the method calls
$service = new xmlrpc_server(array(
"blogger.getUsersBlogs" => array(
"function" => array($this, "getUsersBlogs")
),
"metaWeblog.getRecentPosts" => array(
'function' => array($this, 'getRecentPosts')
),
'metaWeblog.getCategories' => array(
'function' => array($this, 'getCategories')
)
), false);
// Use nice php functions, and call the service
$service->functions_parameters_type = 'phpvals';
$service->service();
// Tell SilverStripe not to try render a template
return false;
}
/**
* Get a list of BlogHolders the user has access to.
*/
function getUsersBlogs($appkey, $username, $password) {
$member = MemberAuthenticator::authenticate(array(
'Email' => $username,
'Password' => $password,
));
// TODO Throw approriate error.
if(!$member) die();
$blogholders = DataObject::get('BlogHolder');
$response = array();
foreach($blogholders as $bh) {
if(!$bh->canAddChildren($member)) continue;
$bgarr = array();
$bgarr['url'] = $bh->AbsoluteLink();
$bgarr['blogid'] = (int) $bh->ID;
$bgarr['blogname'] = $bh->Title;
$response[] = $bgarr;
}
return $response;
}
/**
* Get the most recent posts on a blog.
*/
function getRecentPosts($blogid, $username, $password, $numberOfPosts) {
$member = MemberAuthenticator::authenticate(array(
'Email' => $username,
'Password' => $password,
));
// TODO Throw approriate error.
if(!$member) die();
$posts = DataObject::get('BlogEntry', '"ParentID" = ' . (int) $blogid, '"Date" DESC');
$res = array();
$postsSoFar = 0;
foreach($posts as $post) {
if(!$post->canEdit($member)) continue;
$parr = array();
$parr['title'] = $post->Title;
$parr['link'] = $post->AbsoluteLink();
$parr['description'] = $post->Content;
$parr['postid'] = (int) $post->ID;
$res[] = $parr;
if(++$postsSoFar >= $numberOfPosts) break;
}
return $res;
}
function getCategories() {
//TODO dummy function
return array();
}
}