3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
4 * Copyright (C) 2002-2004 The Nucleus Group
\r
6 * This program is free software; you can redistribute it and/or
\r
7 * modify it under the terms of the GNU General Public License
\r
8 * as published by the Free Software Foundation; either version 2
\r
9 * of the License, or (at your option) any later version.
\r
10 * (see nucleus/documentation/index.html#license for more info)
\r
12 * This script is provides an XML-RPC [1] interface to Nucleus [2].
\r
14 * At this time, the Blogger API [3], the metaWeblog API [4] and
\r
15 * parts of the Movable Type API [5] are implemented
\r
17 * This script uses the the 'XML-RPC for PHP v1.02' implementation [6]
\r
18 * All other code was written by Wouter Demuynck [7]
\r
20 * [1] http://www.xmlrpc.com/
\r
21 * [2] http://nucleuscms.org/
\r
22 * [3] http://plant.blogger.com/api/
\r
23 * [4] http://www.xmlrpc.com/metaWeblogApi
\r
24 * [5] http://www.movabletype.org/docs/mtmanual_programmatic.html
\r
25 * [6] http://phpxmlrpc.sourceforge.net/
\r
26 * [7] http://demuynck.org/
\r
29 * The Blogger API: (more info in the documentation)
\r
33 * blogger.getUsersBlogs
\r
34 * blogger.deletePost
\r
35 * blogger.getRecentPosts
\r
37 * blogger.getUserInfo
\r
38 * blogger.getTemplate
\r
39 * blogger.setTemplate
\r
41 * Note: The getUserInfo response contains an empty 'lastname' and the full name as
\r
43 * Note: Blogger API methods only affect the body field of items
\r
45 * The metaWeblog API (more info in documentation)
\r
47 * metaWeblog.newPost
\r
48 * metaWeblog.getPost
\r
49 * metaWeblog.editPost
\r
50 * metaWeblog.getCategories
\r
51 * metaWeblog.newMediaObject
\r
52 * metaWeblog.getRecentPosts
\r
54 * Note: metaWeblog API methods only affect the body and title fields of items.
\r
55 * the extended part is left untouched (and empty for new posts)
\r
57 * The Movable Type API
\r
59 * mt.supportedMethods
\r
62 include("../../config.php"); // include Nucleus libs and code
\r
63 include($DIR_LIBS . "xmlrpc.inc.php");
\r
64 include($DIR_LIBS . "xmlrpcs.inc.php");
\r
66 /* definition of available methods */
\r
68 $functionDefs = array();
\r
70 // load server functions
\r
71 include('api_blogger.inc.php');
\r
72 include('api_metaweblog.inc.php');
\r
73 // include('api_nucleus.inc.php'); // uncomment if you still want to use the nucleus.* methods
\r
74 include('api_mt.inc.php');
\r
78 $s = new xmlrpc_server( $functionDefs );
\r
81 /* ------------------------------ private functions ---------------------------------- */
\r
84 * Adds an item to the given blog. Username and password are required to login
\r
86 function _addItem($blogid, $username, $password, $title, $body, $more, $publish, $closed, $catname = "") {
\r
87 $blog = new BLOG($blogid);
\r
88 $timestamp = $blog->getCorrectTime();
\r
89 return _addDatedItem($blogid, $username, $password, $title, $body, $more, $publish, $closed, $timestamp, 0, $catname);
\r
93 * Adds item to blog, with time of item given
\r
95 function _addDatedItem($blogid, $username, $password, $title, $body, $more, $publish, $closed, $timestamp, $future, $catname = "") {
\r
97 $mem = new MEMBER();
\r
99 if (!$mem->login($username, $password))
\r
100 return _error(1,"Could not log in");
\r
102 // 2. check if allowed to add to blog
\r
103 if (!BLOG::existsID($blogid))
\r
104 return _error(2,"No such blog ($blogid)");
\r
105 if (!$mem->teamRights($blogid))
\r
106 return _error(3,"Not a team member");
\r
108 return _error(4,"Cannot add empty items!");
\r
110 // 3. calculate missing vars
\r
111 $blog = new BLOG($blogid);
\r
113 // get category id (or id for default category when false category)
\r
114 $catid = $blog->getCategoryIdFromName($catname);
\r
123 $title = mb_convert_encoding($title, _CHARSET, "UTF-8");
\r
124 $body = mb_convert_encoding($body, _CHARSET, "UTF-8");
\r
127 $itemid = $blog->additem($catid, $title, $body, $more, $blogid, $mem->getID(), $timestamp, $closed, $draft);
\r
129 // [TODO] ping weblogs.com ?
\r
131 return new xmlrpcresp(new xmlrpcval($itemid,"string"));
\r
135 * Updates an item. Username and password are required to login
\r
137 function _edititem($itemid, $username, $password, $catid, $title, $body, $more, $wasdraft, $publish, $closed) {
\r
141 $mem = new MEMBER();
\r
142 if (!$mem->login($username, $password))
\r
143 return _error(1,"Could not log in");
\r
145 // 2. check if allowed to add to blog
\r
146 if (!$manager->existsItem($itemid,1,1))
\r
147 return _error(6,"No such item ($itemid)");
\r
148 if (!$mem->canAlterItem($itemid))
\r
149 return _error(7,"Not allowed to alter item");
\r
152 ITEM::update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, 0);
\r
154 return new xmlrpcresp(new xmlrpcval(1,"boolean"));
\r
158 * Gives the list of blogs to which the user with given name and password has access
\r
160 function _getUsersBlogs($username, $password) {
\r
162 $mem = new MEMBER();
\r
163 if (!$mem->login($username, $password))
\r
164 return _error(1,"Could not log in");
\r
166 // 2. Get list of blogs
\r
168 $structarray = array();
\r
169 $query = "SELECT bnumber, bname, burl"
\r
170 . ' FROM '.sql_table('blog').', '.sql_table('team')
\r
171 . " WHERE tblog=bnumber and tmember=" . $mem->getID()
\r
172 . " ORDER BY bname";
\r
173 $r = sql_query($query);
\r
175 while ($obj = mysql_fetch_object($r)) {
\r
177 $blogurl = $obj->burl;
\r
179 $blogurl = 'http://';
\r
181 $newstruct = new xmlrpcval(array(
\r
182 "url" => new xmlrpcval($blogurl,"string"),
\r
183 "blogid" => new xmlrpcval($obj->bnumber,"string"),
\r
184 "blogName" => new xmlrpcval($obj->bname,"string")
\r
186 array_push($structarray, $newstruct);
\r
189 return new xmlrpcresp(new xmlrpcval( $structarray , "array"));
\r
193 function _getUserInfo($username, $password) {
\r
195 $mem = new MEMBER();
\r
196 if (!$mem->login($username, $password))
\r
197 return _error(1,"Could not log in");
\r
199 // 3. return the info
\r
200 // Structure returned has nickname, userid, url, email, lastname, firstname
\r
202 $newstruct = new xmlrpcval(array(
\r
203 "nickname" => new xmlrpcval($mem->getDisplayName(),"string"),
\r
204 "userid" => new xmlrpcval($mem->getID(),"string"),
\r
205 "url" => new xmlrpcval($mem->getURL(),"string"),
\r
206 "email" => new xmlrpcval($mem->getEmail(),"string"),
\r
207 "lastname" => new xmlrpcval("","string"),
\r
208 "firstname" => new xmlrpcval($mem->getRealName(),"string")
\r
211 return new xmlrpcresp($newstruct);
\r
219 function _deleteItem($itemid, $username, $password) {
\r
223 $mem = new MEMBER();
\r
224 if (!$mem->login($username, $password))
\r
225 return _error(1,"Could not log in");
\r
227 // 2. check if allowed
\r
228 if (!$manager->existsItem($itemid,1,1))
\r
229 return _error(6,"No such item ($itemid)");
\r
230 $blogid = getBlogIDFromItemID($itemid);
\r
231 if (!$mem->teamRights($blogid))
\r
232 return _error(3,"Not a team member");
\r
235 ITEM::delete($itemid);
\r
237 return new xmlrpcresp(new xmlrpcval(1,"boolean"));
\r
241 * Returns a template
\r
243 function _getSkinPart($blogid, $username, $password, $type) {
\r
245 $mem = new MEMBER();
\r
246 if (!$mem->login($username, $password))
\r
247 return _error(1,"Could not log in");
\r
249 // 2. check if allowed
\r
250 if (!BLOG::existsID($blogid))
\r
251 return _error(2,"No such blog ($blogid)");
\r
252 if (!$mem->teamRights($blogid))
\r
253 return _error(3,"Not a team member");
\r
255 // 3. return skin part
\r
256 $blog = new BLOG($blogid);
\r
257 $skin = new SKIN($blog->getDefaultSkin());
\r
258 return new xmlrpcresp(new xmlrpcval($skin->getContent($type),"string"));
\r
262 function _setSkinPart($blogid, $username, $password, $content, $type) {
\r
264 $mem = new MEMBER();
\r
265 if (!$mem->login($username, $password))
\r
266 return _error(1,"Could not log in");
\r
268 // 2. check if allowed
\r
269 if (!BLOG::existsID($blogid))
\r
270 return _error(2,"No such blog ($blogid)");
\r
271 if (!$mem->teamRights($blogid))
\r
272 return _error(3,"Not a team member");
\r
274 // 3. update skin part
\r
275 $blog = new BLOG($blogid);
\r
276 $skin = new SKIN($blog->getDefaultSkin());
\r
277 $skin->update($type, $content);
\r
279 return new xmlrpcresp(new xmlrpcval(1,'boolean'));
\r
283 * Some convenience methods
\r
286 function _getScalar($m, $idx) {
\r
287 $v = $m->getParam($idx);
\r
288 return $v->scalarval();
\r
291 function _getStructVal($struct, $key) {
\r
292 $t = $struct->structmem($key);
\r
293 return $t->scalarval();
\r
296 function _getArrayVal($a, $idx) {
\r
297 $t = $a->arraymem(0);
\r
298 return $t->scalarval();
\r
302 * Returns an XML-RPC error response
\r
303 * $err is the error number (>0, will be added to $xmlrpcerruser)
\r
305 function _error($err, $msg) {
\r
306 global $xmlrpcerruser;
\r
307 return new xmlrpcresp(0, $xmlrpcerruser + $err, $msg);
\r