3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2002-2010 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)
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
63 * @license http://nucleuscms.org/license.txt GNU General Public License
64 * @copyright Copyright (C) 2002-2010 The Nucleus Group
66 * @version $NucleusJP: server.php,v 1.8.2.1 2007/09/07 07:12:42 kimitake Exp $
69 require("../../config.php"); // include Nucleus libs and code
70 include($DIR_LIBS . "xmlrpc.inc.php");
71 include($DIR_LIBS . "xmlrpcs.inc.php");
73 /* define xmlrpc settings */
74 $xmlrpc_internalencoding = _CHARSET;
75 $xmlrpc_defencoding = 'UTF-8';
77 /* definition of available methods */
79 $functionDefs = array();
81 // load server functions
82 include('api_blogger.inc.php');
83 include('api_metaweblog.inc.php');
84 // include('api_nucleus.inc.php'); // uncomment if you still want to use the nucleus.* methods
85 include('api_mt.inc.php');
89 $s = new xmlrpc_server( $functionDefs );
92 /* ------------------------------ private functions ---------------------------------- */
95 * Adds an item to the given blog. Username and password are required to login
97 function _addItem($blogid, $username, $password, $title, $body, $more, $publish, $closed, $catname = "") {
98 $blog = new BLOG($blogid);
99 $timestamp = $blog->getCorrectTime();
100 return _addDatedItem($blogid, $username, $password, $title, $body, $more, $publish, $closed, $timestamp, 0, $catname);
104 * Adds item to blog, with time of item given
106 function _addDatedItem($blogid, $username, $password, $title, $body, $more, $publish, $closed, $timestamp, $future, $catname = "") {
110 if (!$mem->login($username, $password))
111 return _error(1,"Could not log in");
113 // 2. check if allowed to add to blog
114 if (!BLOG::existsID($blogid))
115 return _error(2,"No such blog ($blogid)");
116 if (!$mem->teamRights($blogid))
117 return _error(3,"Not a team member");
119 return _error(4,"Cannot add empty items!");
121 // 3. calculate missing vars
122 $blog = new BLOG($blogid);
124 // get category id (or id for default category when false category)
125 $catid = $blog->getCategoryIdFromName($catname);
135 $itemid = $blog->additem($catid, $title, $body, $more, $blogid, $mem->getID(), $timestamp, $closed, $draft);
137 // [TODO] ping weblogs.com ?
139 return new xmlrpcresp(new xmlrpcval($itemid,"string"));
143 * Updates an item. Username and password are required to login
145 function _edititem($itemid, $username, $password, $catid, $title, $body, $more, $wasdraft, $publish, $closed) {
150 if (!$mem->login($username, $password))
151 return _error(1,"Could not log in");
153 // 2. check if allowed to add to blog
154 if (!$manager->existsItem($itemid,1,1))
155 return _error(6,"No such item ($itemid)");
156 if (!$mem->canAlterItem($itemid))
157 return _error(7,"Not allowed to alter item");
160 ITEM::update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, 0);
162 return new xmlrpcresp(new xmlrpcval(1,"boolean"));
166 * Gives the list of blogs to which the user with given name and password has access
168 function _getUsersBlogs($username, $password) {
171 if (!$mem->login($username, $password))
172 return _error(1,"Could not log in");
174 // 2. Get list of blogs
176 $structarray = array();
177 $query = "SELECT bnumber, bname, burl"
178 . ' FROM '.sql_table('blog').', '.sql_table('team')
179 . " WHERE tblog=bnumber and tmember=" . $mem->getID()
181 $r = sql_query($query);
183 while ($obj = sql_fetch_object($r)) {
185 $blogurl = $obj->burl;
187 $blogurl = 'http://';
189 $newstruct = new xmlrpcval(array(
190 "url" => new xmlrpcval($blogurl,"string"),
191 "blogid" => new xmlrpcval($obj->bnumber,"string"),
192 "blogName" => new xmlrpcval($obj->bname,"string")
194 array_push($structarray, $newstruct);
197 return new xmlrpcresp(new xmlrpcval( $structarray , "array"));
201 function _getUserInfo($username, $password) {
204 if (!$mem->login($username, $password))
205 return _error(1,"Could not log in");
207 // 3. return the info
208 // Structure returned has nickname, userid, url, email, lastname, firstname
210 $newstruct = new xmlrpcval(array(
211 "nickname" => new xmlrpcval($mem->getDisplayName(),"string"),
212 "userid" => new xmlrpcval($mem->getID(),"string"),
213 "url" => new xmlrpcval($mem->getURL(),"string"),
214 "email" => new xmlrpcval($mem->getEmail(),"string"),
215 "lastname" => new xmlrpcval("","string"),
216 "firstname" => new xmlrpcval($mem->getRealName(),"string")
219 return new xmlrpcresp($newstruct);
227 function _deleteItem($itemid, $username, $password) {
232 if (!$mem->login($username, $password))
233 return _error(1,"Could not log in");
235 // 2. check if allowed
236 if (!$manager->existsItem($itemid,1,1))
237 return _error(6,"No such item ($itemid)");
238 $blogid = getBlogIDFromItemID($itemid);
239 if (!$mem->teamRights($blogid))
240 return _error(3,"Not a team member");
243 ITEM::delete($itemid);
245 return new xmlrpcresp(new xmlrpcval(1,"boolean"));
251 function _getSkinPart($blogid, $username, $password, $type) {
254 if (!$mem->login($username, $password))
255 return _error(1,"Could not log in");
257 // 2. check if allowed
258 if (!BLOG::existsID($blogid))
259 return _error(2,"No such blog ($blogid)");
260 if (!$mem->teamRights($blogid))
261 return _error(3,"Not a team member");
263 // 3. return skin part
264 $blog = new BLOG($blogid);
265 $skin = new SKIN($blog->getDefaultSkin());
266 return new xmlrpcresp(new xmlrpcval($skin->getContent($type),"string"));
270 function _setSkinPart($blogid, $username, $password, $content, $type) {
273 if (!$mem->login($username, $password))
274 return _error(1,"Could not log in");
276 // 2. check if allowed
277 if (!BLOG::existsID($blogid))
278 return _error(2,"No such blog ($blogid)");
279 if (!$mem->teamRights($blogid))
280 return _error(3,"Not a team member");
282 // 3. update skin part
283 $blog = new BLOG($blogid);
284 $skin = new SKIN($blog->getDefaultSkin());
285 $skin->update($type, $content);
287 return new xmlrpcresp(new xmlrpcval(1,'boolean'));
291 * Some convenience methods
294 function _getScalar($m, $idx) {
295 $v = $m->getParam($idx);
296 return $v->scalarval();
299 function _getStructVal($struct, $key) {
300 $t = $struct->structmem($key);
302 return ''; // no such struct value
304 return $t->scalarval();
307 function _getArrayVal($a, $idx) {
308 $t = $a->arraymem($idx);
309 return $t->scalarval();
313 * Returns an XML-RPC error response
314 * $err is the error number (>0, will be added to $xmlrpcerruser)
316 function _error($err, $msg) {
317 global $xmlrpcerruser;
318 return new xmlrpcresp(0, $xmlrpcerruser + $err, $msg);