2003-11-15 20:11:04 +00:00
<?php
// $Id$
2004-08-21 06:42:38 +00:00
/**
* @file
* Enable users to post using applications that support XML-RPC blog APIs.
*/
2004-05-15 15:33:26 +00:00
/**
* Implementation of hook_help().
*/
2003-11-15 20:11:04 +00:00
function blogapi_help($section) {
switch ($section) {
2003-12-09 23:38:32 +00:00
case 'admin/help#blogapi':
return t('This module adds support for several XML-RPC based blogging APIs. Specifically, it currently implements the %bloggerAPI, %metaweblogAPI, and most of the %moveabletype extensions. This allows users to contribute to drupal using external GUI applications, which can often offer richer functionality that online forms based editing', array('%bloggerAPI' => '<a href="http://www.blogger.com/developers/api/1_docs/">Blogger API</a>', '%metaweblogAPI' => '<a href="http://www.xmlrpc.com/metaWeblogApi">MetaWeblog API</a>', '%moveabletype' => '<a href="http://www.movabletype.org/docs/mtmanual_programmatic.html">Moveable Type API</a>'));
2004-06-18 15:04:37 +00:00
case 'admin/modules#description':
2004-08-21 06:42:38 +00:00
return t('Enable users to post using applications that support XML-RPC blog APIs.');
2003-11-15 20:11:04 +00:00
}
}
2004-05-15 15:33:26 +00:00
/**
* Implementation of hook_xmlrpc().
*/
2003-11-15 20:11:04 +00:00
function blogapi_xmlrpc() {
$methods = array('blogger.getUsersBlogs' => array('function' => 'blogapi_get_users_blogs'),
2003-12-09 23:38:32 +00:00
'blogger.getUserInfo' => array('function' => 'blogapi_get_user_info'),
'blogger.newPost' => array('function' => 'blogapi_new_post'),
'blogger.editPost' => array('function' => 'blogapi_edit_post'),
2003-11-15 20:11:04 +00:00
'blogger.deletePost' => array('function' => 'blogapi_delete_post'),
2003-11-17 19:16:55 +00:00
'blogger.getRecentPosts' => array('function' => 'blogapi_get_recent_posts'),
2003-11-15 20:11:04 +00:00
'metaWeblog.newPost' => array('function' => 'blogapi_new_post'),
'metaWeblog.editPost' => array('function' => 'blogapi_edit_post'),
'metaWeblog.getPost' => array('function' => 'blogapi_get_post'),
'metaWeblog.newMediaObject' => array('function' => 'blogapi_new_media_object'),
2004-05-15 15:33:26 +00:00
'metaWeblog.getCategories' => array('function' => 'blogapi_get_category_list'),
2003-11-15 20:11:04 +00:00
'metaWeblog.getRecentPosts' => array('function' => 'blogapi_get_recent_posts'),
2004-06-15 17:45:44 +00:00
'mt.getRecentPostTitles' => array('function' => 'blogapi_get_recent_post_titles'),
2003-11-15 20:11:04 +00:00
'mt.getCategoryList' => array('function' => 'blogapi_get_category_list'),
'mt.getPostCategories' => array('function' => 'blogapi_get_post_categories'),
2004-08-15 18:55:03 +00:00
'mt.setPostCategories' => array('function' => 'blogapi_set_post_categories'),
'mt.supportedMethods' => array('function' => 'blogapi_supported_methods'),
'mt.supportedTextFilters' => array('function' => 'blogapi_supported_text_filters'),
'mt.getTrackbackPings' => array('function' => 'blogapi_get_trackback_pings'),
'mt.publishPost' => array('function' => 'blogapi_publish_post')
2003-11-15 20:11:04 +00:00
);
2003-11-17 19:16:55 +00:00
2003-11-15 20:11:04 +00:00
return $methods;
}
2004-05-15 15:33:26 +00:00
/**
* Blogging API callback. Finds the URL of a user's blog.
*/
2003-11-15 20:11:04 +00:00
function blogapi_get_users_blogs($req_params) {
$params = blogapi_convert($req_params);
2003-12-22 11:28:07 +00:00
// Remove unused appkey from bloggerAPI.
if (count($params) == 6) {
$params = array_slice($params, 1);
}
2003-11-15 20:11:04 +00:00
$user = blogapi_validate_user($params[1], $params[2]);
if ($user->uid) {
2004-05-22 21:32:07 +00:00
$struct = new xmlrpcval(array('url' => new xmlrpcval(url('blog/' . $user->uid, NULL, NULL, true)),
2003-11-15 20:11:04 +00:00
'blogid' => new xmlrpcval($user->uid),
'blogName' => new xmlrpcval($user->name . "'s blog")),
'struct');
2004-05-15 15:33:26 +00:00
$resp = new xmlrpcval(array($struct), 'array');
2003-11-15 20:11:04 +00:00
return new xmlrpcresp($resp);
}
else {
2003-12-09 23:38:32 +00:00
return blogapi_error($user);
2003-11-15 20:11:04 +00:00
}
}
2004-05-15 15:33:26 +00:00
/**
* Blogging API callback. Returns profile information about a user.
*/
2003-11-15 20:11:04 +00:00
function blogapi_get_user_info($req_params) {
$params = blogapi_convert($req_params);
2003-12-09 23:38:32 +00:00
2003-11-15 20:11:04 +00:00
$user = blogapi_validate_user($params[1], $params[2]);
2003-12-09 23:38:32 +00:00
2003-11-15 20:11:04 +00:00
if ($user->uid) {
2003-12-09 23:38:32 +00:00
$name = explode(' ', $user->realname ? $user->realname : $user->name, 2);
2003-11-15 20:11:04 +00:00
$struct = new xmlrpcval(array('userid' => new xmlrpcval($user->uid, 'string'),
2003-12-09 23:38:32 +00:00
'lastname' => new xmlrpcval($name[1], 'string'),
'firstname' => new xmlrpcval($name[0], 'string'),
2003-11-15 20:11:04 +00:00
'nickname' => new xmlrpcval($user->name, 'string'),
'email' => new xmlrpcval($user->mail, 'string'),
2004-05-22 21:32:07 +00:00
'url' => new xmlrpcval(url('blog/view/' . $user->uid, NULL, NULL, true), 'string')),
2003-11-15 20:11:04 +00:00
'struct');
return new xmlrpcresp($struct);
}
else {
2003-12-09 23:38:32 +00:00
return blogapi_error($user);
2003-11-15 20:11:04 +00:00
}
}
2004-05-15 15:33:26 +00:00
/**
* Blogging API callback. Inserts a new blog post as a node.
*/
2003-11-15 20:11:04 +00:00
function blogapi_new_post($req_params) {
$params = blogapi_convert($req_params);
2003-12-20 11:57:40 +00:00
// Remove unused appkey from bloggerAPI.
if (count($params) == 6) {
$params = array_slice($params, 1);
}
2003-11-15 20:11:04 +00:00
$user = blogapi_validate_user($params[1], $params[2]);
if (!$user->uid) {
2003-12-09 23:38:32 +00:00
return blogapi_error($user);
2003-11-15 20:11:04 +00:00
}
2004-08-15 18:55:03 +00:00
$edit = array();
$edit['type'] = 'blog';
$edit['uid'] = $user->uid;
$edit['name'] = $user->name;
$edit['promote'] = variable_get('node_promote_blog', 0);
$edit['comment'] = variable_get('node_comment_blog', 2);
$edit['moderate'] = variable_get('node_moderate_blog', 0);
$edit['revision'] = variable_get('node_revision_blog', 0);
$edit['format'] = FILTER_DEFAULT_FORMAT;
$edit['status'] = $params[4];
2003-11-15 20:11:04 +00:00
// check for bloggerAPI vs. metaWeblogAPI
if (is_array($params[3])) {
2004-08-15 18:55:03 +00:00
$edit['title'] = $params[3]['title'];
$edit['body'] = $params[3]['description'];
_blogapi_mt_extra($edit, $params[3]);
2003-11-15 20:11:04 +00:00
}
else {
2004-08-15 18:55:03 +00:00
$edit['title'] = blogapi_blogger_title($params[3]);
$edit['body'] = $params[3];
2003-11-15 20:11:04 +00:00
}
2003-11-17 19:16:55 +00:00
2004-08-15 18:55:03 +00:00
if (!valid_input_data($edit['title'], $edit['body'])) {
2004-05-15 15:33:26 +00:00
return blogapi_error(t('Terminated request because of suspicious input data.'));
2003-12-09 23:38:32 +00:00
}
2004-08-15 18:55:03 +00:00
$node = node_validate($edit);
2003-11-17 19:16:55 +00:00
2004-07-04 16:50:02 +00:00
if (form_get_errors()) {
return blogapi_error();
2003-11-15 20:11:04 +00:00
}
2004-05-15 15:33:26 +00:00
if (!node_access('create', $node)) {
2003-11-15 20:11:04 +00:00
return blogapi_error(message_access());
}
$nid = node_save($node);
if ($nid) {
2004-08-18 19:57:27 +00:00
watchdog('special', t('%type: added %title using blog API.', array('%type' => '<em>'. t($node->type) .'</em>', '%title' => "<em>$node->title</em>")), l(t('view'), "node/$nid"));
2003-11-15 20:11:04 +00:00
return new xmlrpcresp(new xmlrpcval($nid, 'string'));
}
return blogapi_error(t('error storing post'));
}
2004-05-15 15:33:26 +00:00
/**
* Blogging API callback. Modifies the specified blog node.
*/
2003-11-15 20:11:04 +00:00
function blogapi_edit_post($req_params) {
$params = blogapi_convert($req_params);
2003-12-09 23:38:32 +00:00
if (count($params) == 6) {
$params = array_slice($params, 1);
}
2003-11-15 20:11:04 +00:00
$user = blogapi_validate_user($params[1], $params[2]);
2003-12-09 23:38:32 +00:00
2003-11-15 20:11:04 +00:00
if (!$user->uid) {
2003-12-09 23:38:32 +00:00
return blogapi_error($user);
2003-11-15 20:11:04 +00:00
}
$node = node_load(array('nid' => $params[0]));
if (!$node) {
return blogapi_error(message_na());
}
2003-12-09 23:38:32 +00:00
if (!node_access('update', $node)) {
2003-11-15 20:11:04 +00:00
return blogapi_error(message_access());
}
2004-08-15 18:55:03 +00:00
$node->status = $params[4];
2003-11-15 20:11:04 +00:00
// check for bloggerAPI vs. metaWeblogAPI
if (is_array($params[3])) {
2004-08-15 18:55:03 +00:00
$node->title = $params[3]['title'];
$node->body = $params[3]['description'];
_blogapi_mt_extra($node, $params[3]);
2003-11-15 20:11:04 +00:00
}
else {
2004-08-15 18:55:03 +00:00
$node->title = blogapi_blogger_title($params[3]);
$node->body = $params[3];
2003-11-15 20:11:04 +00:00
}
2003-11-17 19:16:55 +00:00
2003-12-09 23:38:32 +00:00
if (!valid_input_data($title, $body)) {
2004-05-15 15:33:26 +00:00
return blogapi_error(t('Terminated request because of suspicious input data.'));
2003-12-09 23:38:32 +00:00
}
2004-07-04 16:50:02 +00:00
$node = node_validate($node);
2003-11-17 19:16:55 +00:00
2004-07-04 16:50:02 +00:00
if (form_get_errors()) {
return blogapi_error();
2003-11-15 20:11:04 +00:00
}
2003-12-09 23:38:32 +00:00
$terms = module_invoke('taxonomy', 'node_get_terms', $node->nid, 'tid');
foreach ($terms as $term) {
$node->taxonomy[] = $term->tid;
}
2003-11-15 20:11:04 +00:00
$nid = node_save($node);
if ($nid) {
2004-08-18 19:57:27 +00:00
watchdog('special', t('%type: updated %title using blog API.', array('%type' => '<em>'. t($node->type) .'</em>', '%title' => "<em>$node->title</em>")), l(t('view'), "node/$nid"));
2004-05-15 15:33:26 +00:00
return new xmlrpcresp(new xmlrpcval(true, 'boolean'));
2003-11-15 20:11:04 +00:00
}
return blogapi_error(t('error storing node'));
}
2004-05-15 15:33:26 +00:00
/**
* Blogging API callback. Returns a specified blog node.
*/
2003-11-15 20:11:04 +00:00
function blogapi_get_post($req_params) {
$params = blogapi_convert($req_params);
$user = blogapi_validate_user($params[1], $params[2]);
if (!$user->uid) {
2003-12-09 23:38:32 +00:00
return blogapi_error($user);
2003-11-15 20:11:04 +00:00
}
2003-11-17 19:16:55 +00:00
$node = node_load(array('nid' => $params[0]));
2004-08-15 18:55:03 +00:00
$blog = _blogapi_get_post($node, true);
2003-11-15 20:11:04 +00:00
return new xmlrpcresp($blog);
}
2004-05-15 15:33:26 +00:00
/**
* Blogging API callback. Removes the specified blog node.
*/
2003-11-15 20:11:04 +00:00
function blogapi_delete_post($req_params) {
$params = blogapi_convert($req_params);
2003-11-17 19:16:55 +00:00
2003-11-15 20:11:04 +00:00
$user = blogapi_validate_user($params[2], $params[3]);
if (!$user->uid) {
2003-12-09 23:38:32 +00:00
return blogapi_error($user);
2003-11-15 20:11:04 +00:00
}
$ret = node_delete(array('nid' => $params[1], 'confirm' => 1));
2004-05-15 15:33:26 +00:00
return new xmlrpcresp(new xmlrpcval(true, 'boolean'));
2003-11-15 20:11:04 +00:00
}
2004-05-15 15:33:26 +00:00
/**
* Blogging API callback. Inserts a file into Drupal.
*/
2003-11-15 20:11:04 +00:00
function blogapi_new_media_object($req_params) {
2004-06-04 18:00:48 +00:00
$params = blogapi_convert($req_params);
$user = blogapi_validate_user($params[1], $params[2]);
if (!$user->uid) {
return blogapi_error($user);
}
$name = basename($params[3]['name']);
$data = $params[3]['bits'];
if (!$data) {
return blogapi_error(t('No file sent'));
}
2004-06-04 18:50:29 +00:00
2004-06-04 18:00:48 +00:00
if (!$file = file_save_data($data, $name)) {
return blogapi_error(t('Error storing file'));
}
2004-06-04 18:50:29 +00:00
2004-06-04 18:00:48 +00:00
// Return the successful result.
$result = new xmlrpcval(array('url' => new xmlrpcval(file_create_url($file), 'string')), 'struct');
return new xmlrpcresp($result);
2003-11-15 20:11:04 +00:00
}
2004-05-15 15:33:26 +00:00
/**
* Blogging API callback. Returns a list of the taxonomy terms that can be
* associated with a blog node.
*/
2003-11-15 20:11:04 +00:00
function blogapi_get_category_list($req_params) {
2003-12-09 23:38:32 +00:00
$vocabularies = module_invoke('taxonomy', 'get_vocabularies', 'blog', 'vid');
2004-04-15 21:40:37 +00:00
$categories = array();
2003-12-09 23:38:32 +00:00
if ($vocabularies) {
foreach ($vocabularies as $vocabulary) {
2004-06-02 18:46:04 +00:00
$terms = module_invoke('taxonomy', 'get_tree', $vocabulary->vid, 0, -1);
2003-12-09 23:38:32 +00:00
foreach ($terms as $term) {
$term_name = $term->name;
foreach (module_invoke('taxonomy', 'get_parents', $term->tid, 'tid') as $parent) {
$term_name = $parent->name . '/' . $term_name;
}
$categories[] = new xmlrpcval(array('categoryName' => new xmlrpcval($term_name, 'string'),
'categoryId' => new xmlrpcval($term->tid, 'string')),
'struct');
2003-11-15 20:11:04 +00:00
}
}
}
2004-05-15 15:33:26 +00:00
return new xmlrpcresp(new xmlrpcval($categories, 'array'));
2003-11-15 20:11:04 +00:00
}
2004-05-15 15:33:26 +00:00
/**
* Blogging API callback. Returns a list of the taxonomy terms that are
* assigned to a particular node.
*/
2003-11-15 20:11:04 +00:00
function blogapi_get_post_categories($req_params) {
$params = blogapi_convert($req_params);
$user = blogapi_validate_user($params[1], $params[2]);
if (!$user->uid) {
2003-12-09 23:38:32 +00:00
return blogapi_error($user);
2003-11-15 20:11:04 +00:00
}
2003-12-09 23:38:32 +00:00
$terms = module_invoke('taxonomy', 'node_get_terms', $params[0], 'tid');
2003-11-15 20:11:04 +00:00
$categories = array();
2003-12-09 23:38:32 +00:00
foreach ($terms as $term) {
2003-11-15 20:11:04 +00:00
$term_name = $term->name;
2003-12-09 23:38:32 +00:00
foreach (module_invoke('taxonomy', 'get_parents', $term->tid, 'tid') as $parent) {
2003-11-15 20:11:04 +00:00
$term_name = $parent->name . '/' . $term_name;
}
$categories[] = new xmlrpcval(array('categoryName' => new xmlrpcval($term_name, 'string'),
'categoryId' => new xmlrpcval($term->tid, 'string'),
'isPrimary' => new xmlrpcval(true, 'boolean')),
'struct');
}
2004-05-15 15:33:26 +00:00
return new xmlrpcresp(new xmlrpcval($categories, 'array'));
2003-11-15 20:11:04 +00:00
}
2004-05-15 15:33:26 +00:00
/**
* Blogging API callback. Assigns taxonomy terms to a particular node.
*/
2003-11-15 20:11:04 +00:00
function blogapi_set_post_categories($req_params) {
$params = blogapi_convert($req_params);
$user = blogapi_validate_user($params[1], $params[2]);
if (!$user->uid) {
2003-12-09 23:38:32 +00:00
return blogapi_error($user);
2003-11-15 20:11:04 +00:00
}
$nid = $params[0];
$terms = array();
foreach ($params[3] as $category) {
$terms[] = $category['categoryId']->scalarval();
}
2003-12-09 23:38:32 +00:00
module_invoke('taxonomy', 'node_save', $nid, $terms);
2003-11-15 20:11:04 +00:00
return new xmlrpcresp(new xmlrpcval(true, 'boolean'));
}
2004-05-15 15:33:26 +00:00
/**
2004-06-15 17:45:44 +00:00
* Blogging API callback. Returns the latest few postings in a user's blog. $bodies TRUE
* <a href="http://movabletype.org/docs/mtmanual_programmatic.html#item_mt%2EgetRecentPostTitles">
* returns a bandwidth-friendly list</a>.
2004-05-15 15:33:26 +00:00
*/
2004-06-15 17:45:44 +00:00
function blogapi_get_recent_posts($req_params, $bodies = TRUE) {
2003-11-15 20:11:04 +00:00
$params = blogapi_convert($req_params);
2003-12-09 23:38:32 +00:00
// Remove unused appkey (from bloggerAPI).
if (count($params) == 5) {
$params = array_slice($params, 1);
}
2003-11-15 20:11:04 +00:00
$user = blogapi_validate_user($params[1], $params[2]);
if (!$user->uid) {
2003-12-09 23:38:32 +00:00
return blogapi_error($user);
2003-11-15 20:11:04 +00:00
}
2004-06-15 17:45:44 +00:00
$result = db_query_range('SELECT n.nid, n.title,'. ($bodies ? ' n.body,' : '') ." n.created, u.name FROM {node} n, {users} u WHERE n.uid=u.uid AND n.type = 'blog' AND n.uid = %d ORDER BY n.created DESC", $user->uid, 0, $params[3]);
2003-12-09 23:38:32 +00:00
while ($blog = db_fetch_object($result)) {
2004-08-15 18:55:03 +00:00
$xmlrpcval = _blogapi_get_post($blog, $bodies);
$blogs[] = $xmlrpcval;
2003-11-15 20:11:04 +00:00
}
2004-05-15 15:33:26 +00:00
return new xmlrpcresp(new xmlrpcval($blogs, 'array'));
2003-11-15 20:11:04 +00:00
}
2004-06-15 17:45:44 +00:00
// see above
function blogapi_get_recent_post_titles($req_params) {
return blogapi_get_recent_posts($req_params, TRUE);
}
2004-08-15 18:55:03 +00:00
/**
* Blogging API callback. Sends a list of supported methods to the client.
*/
function blogapi_supported_methods($req_params) {
$methods = array_keys(blogapi_xmlrpc());
$output = array();
foreach ($methods as $method) {
$output[] = new xmlrpcval($method, 'string');
}
return new xmlrpcresp(new xmlrpcval($output, 'array'));
}
/**
* Blogging API callback. Sends a list of available input formats.
*/
function blogapi_supported_text_filters($req_params) {
global $user;
// NOTE: we're only using anonymous' formats because the MT spec
// does not allow for per-user formats.
$formats = filter_formats();
$filters = array();
foreach ($formats as $format) {
$filter['key'] = new xmlrpcval($format->format, 'string');
$filter['label'] = new xmlrpcval($format->name, 'string');
$filters[] = new xmlrpcval($filter, 'struct');
}
return new xmlrpcresp(new xmlrpcval($filters, 'array'));
}
/**
* Blogging API callback. Can not be implemented without support from
* trackback module.
*/
function blogapi_get_trackback_pings($req_params) {
return blogapi_error(t('not implemented'));
}
/**
* Blogging API callback. Publishes the given node
*/
function blogapi_publish_post($req_params) {
$params = blogapi_convert($req_params);
$user = blogapi_validate_user($params[1], $params[2]);
$node = node_load(array('nid' => $params[0]));
if (!$node) {
return blogapi_error(t('invalid node'));
}
$node->status = 1;
if (!node_access('update', $node)) {
return blogapi_error(message_access());
}
node_save($node);
return new xmlrpcresp(new xmlrpcval(true, 'boolean'));
}
2004-05-15 15:33:26 +00:00
/**
* Process the parameters to an XMLRPC callback, and return them as an array.
*/
2003-11-15 20:11:04 +00:00
function blogapi_convert($params) {
$cparams = array();
$num_params= $params->getNumParams();
for ($i = 0; $i < $num_params; $i++) {
$sn = $params->getParam($i);
$cparams[] = $sn->getval();
}
return $cparams;
}
2004-05-15 15:33:26 +00:00
/**
* Prepare an error message for returning to the XMLRPC caller.
*/
2003-11-15 20:11:04 +00:00
function blogapi_error($message) {
global $xmlrpcusererr;
2003-11-17 19:16:55 +00:00
2004-07-04 16:50:02 +00:00
if (!is_array($message)) {
$message = array($message);
2003-12-09 23:38:32 +00:00
}
2004-07-04 16:50:02 +00:00
if ($errors = form_get_errors()) {
$message = $message + $errors;
}
$message = implode(' ', $message);
2003-12-09 23:38:32 +00:00
return new xmlrpcresp(0, $xmlrpcusererr + 1, strip_tags($message));
2003-11-15 20:11:04 +00:00
}
2004-05-15 15:33:26 +00:00
/**
* Ensure that the given user has permission to edit a blog.
*/
2003-11-15 20:11:04 +00:00
function blogapi_validate_user($username, $password) {
global $user;
2003-11-17 19:16:55 +00:00
2004-08-14 07:00:27 +00:00
$user = user_authenticate($username, $password);
2003-11-15 20:11:04 +00:00
2003-12-09 23:38:32 +00:00
if ($user->uid) {
2004-07-13 20:40:46 +00:00
if (user_access('edit own blog')) {
2003-12-09 23:38:32 +00:00
return $user;
}
else {
return message_access();
}
}
else {
return t('Wrong username or password.');
2003-11-15 20:11:04 +00:00
}
}
2004-05-15 15:33:26 +00:00
/**
* For the blogger API, extract the node title from the contents field.
*/
2003-11-15 20:11:04 +00:00
function blogapi_blogger_title(&$contents) {
2004-05-15 15:33:26 +00:00
if (eregi('<title>([^<]*)</title>', $contents, $title)) {
2003-12-09 23:38:32 +00:00
$title = strip_tags($title[0]);
2004-05-15 15:33:26 +00:00
$contents = ereg_replace('<title>[^<]*</title>', '', $contents);
2003-11-15 20:11:04 +00:00
}
else {
list($title, $rest) = explode("\n", $contents, 2);
}
return $title;
}
2004-07-06 16:54:28 +00:00
function blogapi_settings() {
$output = form_select(t('XML-RPC Engine'), 'blogapi_engine', variable_get('blogapi_engine', 0), array(0 => 'Blogger', 1 => 'MetaWeblog', 2 => 'Movabletype'), t('RSD or Really-Simple-Discovery is a mechanism which allows external blogger tools to discover the APIs they can use to interact with Drupal. The common XML-RPC engines are Blogger, MetaWeblog and Movabletype. If you are not sure which is the correct setting, choose Blogger.'));
return $output;
}
function blogapi_menu() {
global $user;
$items = array();
if ($_GET['q'] == variable_get('site_frontpage', 'node')) {
drupal_set_html_head('<link rel="EditURI" type="application/rsd+xml" title="RSD" href="' . url('blogapi/rsd', NULL, NULL, TRUE) . '" />');
}
$items[] = array('path' => 'blogapi', 'title' => t('RSD'), 'callback' => 'blogapi_blogapi', 'access' => user_access('access_content'), 'type' => MENU_CALLBACK);
return $items;
}
function blogapi_blogapi() {
switch (arg(1)) {
case 'rsd':
blogapi_rsd();
break;
default:
drupal_not_found();
break;
}
}
function blogapi_rsd() {
global $base_url;
2004-08-24 19:21:30 +00:00
$xmlrpc = $base_url .'/'. 'xmlrpc.php';
2004-07-06 16:54:28 +00:00
$base = url('', NULL, NULL, TRUE);
$blogid = 1; # until we figure out how to handle multiple bloggers
$metaweblog = 'false'; $blogger = 'false'; $mt = 'false';
if (variable_get('blogapi_engine', 0) == 0) {
$blogger = 'true';
} else if (variable_get('blogapi_engine', 0) == 1) {
$metaweblog = 'true';
} else if (variable_get('blogapi_engine', 0) == 2) {
$mt = 'true';
}
print <<<__RSD__
<?xml version="1.0"?>
<rsd version="1.0" xmlns="http://archipelago.phrasewise.com/rsd">
<service>
<engineName>Drupal</engineName>
<engineLink>http://www.drupal.org/</engineLink>
<homePageLink>$base</homePageLink>
<apis>
<api name="MetaWeblog" preferred="$metaweblog" apiLink="$xmlrpc" blogID="$blogid" />
<api name="Blogger" preferred="$blogger" apiLink="$xmlrpc" blogID="$blogid" />
<api name="Movabletype" preferred="$mt" apiLink="$xmlrpc" blogID="$blogid" />
</apis>
</service>
</rsd>
__RSD__;
}
2004-08-15 18:55:03 +00:00
/**
* Handles extra information sent by clients according to MovableType's spec.
*/
function _blogapi_mt_extra(&$node, $struct) {
if (is_array($node)) {
$was_array = true;
$node = array2object($node);
}
// mt_allow_comments
if (array_key_exists('mt_allow_comments', $struct)) {
switch ($struct['mt_allow_comments']) {
case 0:
$node->comment = 0;
break;
case 1:
$node->comment = 2;
break;
case 2:
$node->comment = 1;
break;
}
}
// merge the 3 body sections (description, mt_excerpt, mt_text_more) into
// one body
if ($struct['mt_excerpt']) {
$node->body = $struct['mt_excerpt'] .'<!--break-->'.$node->body;
}
if ($struct['mt_text_more']) {
$node->body = $node->body . '<!--extended-->' . $struct['mt_text_more'];
}
// mt_tb_ping_urls
if (function_exists('trackback_send')) {
if (is_array($struct['mt_tb_ping_urls'])) {
foreach ($struct['mt_tb_ping_urls'] as $tb_ping_url) {
$node->tb_url = $tb_ping_url->getVal();
trackback_send($node);
2004-08-16 20:32:32 +00:00
unset($node->tb_url); // make sure we don't ping twice
2004-08-15 18:55:03 +00:00
}
}
else {
$node->tb_url = $struct['mt_tb_ping_urls'];
}
}
// mt_convert_breaks
if ($struct['mt_convert_breaks']) {
$node->format = $struct['mt_convert_breaks'];
}
// dateCreated
if ($struct['dateCreated']) {
$node->created = iso8601_decode($struct['dateCreated'], 1);
}
if ($was_array) {
$node = object2array($node);
}
}
function _blogapi_get_post($node, $bodies = true) {
$xmlrpcval = array (
'userid' => new xmlrpcval($node->name, 'string'),
'dateCreated' => new xmlrpcval(iso8601_encode($node->created), 'dateTime.iso8601'),
'title' => new xmlrpcval($node->title, 'string'),
'postid' => new xmlrpcval($node->nid, 'string'),
'link' => new xmlrpcval(url('node/'.$node->nid, NULL, NULL, true)),
'permaLink' => new xmlrpcval(url('node/'.$node->nid, NULL, NULL, true)),
);
if ($bodies) {
if ($node->comment = 1) {
$comment = 2;
}
if ($node->comment = 2) {
$comment = 1;
}
$xmlrpcval['content'] = new xmlrpcval("<title>$blog->title</title>$node->body", 'string');
$xmlrpcval['description'] = new xmlrpcval($node->body, 'string');
// Add MT specific fields
2004-08-16 20:32:32 +00:00
$xmlrpcval['mt_allow_comments'] = new xmlrpcval($comment, 'string');
$xmlrpcval['mt_convert_breaks'] = new xmlrpcval($node->format, 'string');
2004-08-15 18:55:03 +00:00
}
return new xmlrpcval($xmlrpcval, 'struct');
}
2003-11-15 20:11:04 +00:00
?>