3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2002-2004 The Nucleus Group
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 * (see nucleus/documentation/index.html#license for more info)
12 * $Id: server.php,v 1.1.1.1 2005-02-28 07:15:05 kimitake Exp $
14 * This script is provides an XML-RPC [1] interface to Nucleus [2].
16 * At this time, the Blogger API [3], the metaWeblog API [4] and
17 * parts of the Movable Type API [5] are implemented
19 * This script uses the the 'XML-RPC for PHP v1.02' implementation [6]
20 * All other code was written by Wouter Demuynck [7]
22 * [1] http://www.xmlrpc.com/
23 * [2] http://nucleuscms.org/
24 * [3] http://plant.blogger.com/api/
25 * [4] http://www.xmlrpc.com/metaWeblogApi
26 * [5] http://www.movabletype.org/docs/mtmanual_programmatic.html
27 * [6] http://phpxmlrpc.sourceforge.net/
28 * [7] http://demuynck.org/
31 * The Blogger API: (more info in the documentation)
35 * blogger.getUsersBlogs
37 * blogger.getRecentPosts
43 * Note: The getUserInfo response contains an empty 'lastname' and the full name as
45 * Note: Blogger API methods only affect the body field of items
47 * The metaWeblog API (more info in documentation)
52 * metaWeblog.getCategories
53 * metaWeblog.newMediaObject
54 * metaWeblog.getRecentPosts
56 * Note: metaWeblog API methods only affect the body and title fields of items.
57 * the extended part is left untouched (and empty for new posts)
59 * The Movable Type API
64 include("../../config.php"); // include Nucleus libs and code
65 include($DIR_LIBS . "xmlrpc.inc.php");
66 include($DIR_LIBS . "xmlrpcs.inc.php");
68 /* definition of available methods */
70 $functionDefs = array();
72 // load server functions
73 include('api_blogger.inc.php');
74 include('api_metaweblog.inc.php');
75 // include('api_nucleus.inc.php'); // uncomment if you still want to use the nucleus.* methods
76 include('api_mt.inc.php');
80 $s = new xmlrpc_server( $functionDefs );
83 /* ------------------------------ private functions ---------------------------------- */
86 * Adds an item to the given blog. Username and password are required to login
88 function _addItem($blogid, $username, $password, $title, $body, $more, $publish, $closed, $catname = "") {
89 $blog = new BLOG($blogid);
90 $timestamp = $blog->getCorrectTime();
91 return _addDatedItem($blogid, $username, $password, $title, $body, $more, $publish, $closed, $timestamp, 0, $catname);
95 * Adds item to blog, with time of item given
97 function _addDatedItem($blogid, $username, $password, $title, $body, $more, $publish, $closed, $timestamp, $future, $catname = "") {
101 if (!$mem->login($username, $password))
102 return _error(1,"Could not log in");
104 // 2. check if allowed to add to blog
105 if (!BLOG::existsID($blogid))
106 return _error(2,"No such blog ($blogid)");
107 if (!$mem->teamRights($blogid))
108 return _error(3,"Not a team member");
110 return _error(4,"Cannot add empty items!");
112 // 3. calculate missing vars
113 $blog = new BLOG($blogid);
115 // get category id (or id for default category when false category)
116 $catid = $blog->getCategoryIdFromName($catname);
126 $itemid = $blog->additem($catid, $title, $body, $more, $blogid, $mem->getID(), $timestamp, $closed, $draft);
128 // [TODO] ping weblogs.com ?
130 return new xmlrpcresp(new xmlrpcval($itemid,"string"));
134 * Updates an item. Username and password are required to login
136 function _edititem($itemid, $username, $password, $catid, $title, $body, $more, $wasdraft, $publish, $closed) {
141 if (!$mem->login($username, $password))
142 return _error(1,"Could not log in");
144 // 2. check if allowed to add to blog
145 if (!$manager->existsItem($itemid,1,1))
146 return _error(6,"No such item ($itemid)");
147 if (!$mem->canAlterItem($itemid))
148 return _error(7,"Not allowed to alter item");
151 ITEM::update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, 0);
153 return new xmlrpcresp(new xmlrpcval(1,"boolean"));
157 * Gives the list of blogs to which the user with given name and password has access
159 function _getUsersBlogs($username, $password) {
162 if (!$mem->login($username, $password))
163 return _error(1,"Could not log in");
165 // 2. Get list of blogs
167 $structarray = array();
168 $query = "SELECT bnumber, bname, burl"
169 . ' FROM '.sql_table('blog').', '.sql_table('team')
170 . " WHERE tblog=bnumber and tmember=" . $mem->getID()
172 $r = sql_query($query);
174 while ($obj = mysql_fetch_object($r)) {
176 $blogurl = $obj->burl;
178 $blogurl = 'http://';
180 $newstruct = new xmlrpcval(array(
181 "url" => new xmlrpcval($blogurl,"string"),
182 "blogid" => new xmlrpcval($obj->bnumber,"string"),
183 "blogName" => new xmlrpcval($obj->bname,"string")
185 array_push($structarray, $newstruct);
188 return new xmlrpcresp(new xmlrpcval( $structarray , "array"));
192 function _getUserInfo($username, $password) {
195 if (!$mem->login($username, $password))
196 return _error(1,"Could not log in");
198 // 3. return the info
199 // Structure returned has nickname, userid, url, email, lastname, firstname
201 $newstruct = new xmlrpcval(array(
202 "nickname" => new xmlrpcval($mem->getDisplayName(),"string"),
203 "userid" => new xmlrpcval($mem->getID(),"string"),
204 "url" => new xmlrpcval($mem->getURL(),"string"),
205 "email" => new xmlrpcval($mem->getEmail(),"string"),
206 "lastname" => new xmlrpcval("","string"),
207 "firstname" => new xmlrpcval($mem->getRealName(),"string")
210 return new xmlrpcresp($newstruct);
218 function _deleteItem($itemid, $username, $password) {
223 if (!$mem->login($username, $password))
224 return _error(1,"Could not log in");
226 // 2. check if allowed
227 if (!$manager->existsItem($itemid,1,1))
228 return _error(6,"No such item ($itemid)");
229 $blogid = getBlogIDFromItemID($itemid);
230 if (!$mem->teamRights($blogid))
231 return _error(3,"Not a team member");
234 ITEM::delete($itemid);
236 return new xmlrpcresp(new xmlrpcval(1,"boolean"));
242 function _getSkinPart($blogid, $username, $password, $type) {
245 if (!$mem->login($username, $password))
246 return _error(1,"Could not log in");
248 // 2. check if allowed
249 if (!BLOG::existsID($blogid))
250 return _error(2,"No such blog ($blogid)");
251 if (!$mem->teamRights($blogid))
252 return _error(3,"Not a team member");
254 // 3. return skin part
255 $blog = new BLOG($blogid);
256 $skin = new SKIN($blog->getDefaultSkin());
257 return new xmlrpcresp(new xmlrpcval($skin->getContent($type),"string"));
261 function _setSkinPart($blogid, $username, $password, $content, $type) {
264 if (!$mem->login($username, $password))
265 return _error(1,"Could not log in");
267 // 2. check if allowed
268 if (!BLOG::existsID($blogid))
269 return _error(2,"No such blog ($blogid)");
270 if (!$mem->teamRights($blogid))
271 return _error(3,"Not a team member");
273 // 3. update skin part
274 $blog = new BLOG($blogid);
275 $skin = new SKIN($blog->getDefaultSkin());
276 $skin->update($type, $content);
278 return new xmlrpcresp(new xmlrpcval(1,'boolean'));
282 * Some convenience methods
285 function _getScalar($m, $idx) {
286 $v = $m->getParam($idx);
287 return $v->scalarval();
290 function _getStructVal($struct, $key) {
291 $t = $struct->structmem($key);
292 return $t->scalarval();
295 function _getArrayVal($a, $idx) {
296 $t = $a->arraymem(0);
297 return $t->scalarval();
301 * Returns an XML-RPC error response
302 * $err is the error number (>0, will be added to $xmlrpcerruser)
304 function _error($err, $msg) {
305 global $xmlrpcerruser;
306 return new xmlrpcresp(0, $xmlrpcerruser + $err, $msg);