3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
4 * Copyright (C) 2002-2005 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 * $Id: server.php,v 1.5 2005-03-16 08:12:06 kimitake Exp $
\r
13 * $NucleusJP: server.php,v 1.3 2005/03/12 06:19:06 kimitake Exp $
\r
15 * This script is provides an XML-RPC [1] interface to Nucleus [2].
\r
17 * At this time, the Blogger API [3], the metaWeblog API [4] and
\r
18 * parts of the Movable Type API [5] are implemented
\r
20 * This script uses the the 'XML-RPC for PHP v1.02' implementation [6]
\r
21 * All other code was written by Wouter Demuynck [7]
\r
23 * [1] http://www.xmlrpc.com/
\r
24 * [2] http://nucleuscms.org/
\r
25 * [3] http://plant.blogger.com/api/
\r
26 * [4] http://www.xmlrpc.com/metaWeblogApi
\r
27 * [5] http://www.movabletype.org/docs/mtmanual_programmatic.html
\r
28 * [6] http://phpxmlrpc.sourceforge.net/
\r
29 * [7] http://demuynck.org/
\r
32 * The Blogger API: (more info in the documentation)
\r
36 * blogger.getUsersBlogs
\r
37 * blogger.deletePost
\r
38 * blogger.getRecentPosts
\r
40 * blogger.getUserInfo
\r
41 * blogger.getTemplate
\r
42 * blogger.setTemplate
\r
44 * Note: The getUserInfo response contains an empty 'lastname' and the full name as
\r
46 * Note: Blogger API methods only affect the body field of items
\r
48 * The metaWeblog API (more info in documentation)
\r
50 * metaWeblog.newPost
\r
51 * metaWeblog.getPost
\r
52 * metaWeblog.editPost
\r
53 * metaWeblog.getCategories
\r
54 * metaWeblog.newMediaObject
\r
55 * metaWeblog.getRecentPosts
\r
57 * Note: metaWeblog API methods only affect the body and title fields of items.
\r
58 * the extended part is left untouched (and empty for new posts)
\r
60 * The Movable Type API
\r
62 * mt.supportedMethods
\r
65 include("../../config.php"); // include Nucleus libs and code
\r
66 include($DIR_LIBS . "xmlrpc.inc.php");
\r
67 include($DIR_LIBS . "xmlrpcs.inc.php");
\r
69 /* definition of available methods */
\r
71 $functionDefs = array();
\r
73 // load server functions
\r
74 include('api_blogger.inc.php');
\r
75 include('api_metaweblog.inc.php');
\r
76 // include('api_nucleus.inc.php'); // uncomment if you still want to use the nucleus.* methods
\r
77 include('api_mt.inc.php');
\r
81 $s = new xmlrpc_server( $functionDefs );
\r
84 /* ------------------------------ private functions ---------------------------------- */
\r
87 * Adds an item to the given blog. Username and password are required to login
\r
89 function _addItem($blogid, $username, $password, $title, $body, $more, $publish, $closed, $catname = "") {
\r
90 $blog = new BLOG($blogid);
\r
91 $timestamp = $blog->getCorrectTime();
\r
92 return _addDatedItem($blogid, $username, $password, $title, $body, $more, $publish, $closed, $timestamp, 0, $catname);
\r
96 * Adds item to blog, with time of item given
\r
98 function _addDatedItem($blogid, $username, $password, $title, $body, $more, $publish, $closed, $timestamp, $future, $catname = "") {
\r
100 $mem = new MEMBER();
\r
102 if (!$mem->login($username, $password))
\r
103 return _error(1,"Could not log in");
\r
105 // 2. check if allowed to add to blog
\r
106 if (!BLOG::existsID($blogid))
\r
107 return _error(2,"No such blog ($blogid)");
\r
108 if (!$mem->teamRights($blogid))
\r
109 return _error(3,"Not a team member");
\r
111 return _error(4,"Cannot add empty items!");
\r
113 // 3. calculate missing vars
\r
114 $blog = new BLOG($blogid);
\r
116 // get category id (or id for default category when false category)
\r
117 $catid = $blog->getCategoryIdFromName($catname);
\r
126 $title = mb_convert_encoding($title, _CHARSET, "UTF-8");
\r
127 $body = mb_convert_encoding($body, _CHARSET, "UTF-8");
\r
130 $itemid = $blog->additem($catid, $title, $body, $more, $blogid, $mem->getID(), $timestamp, $closed, $draft);
\r
132 // [TODO] ping weblogs.com ?
\r
134 return new xmlrpcresp(new xmlrpcval($itemid,"string"));
\r
138 * Updates an item. Username and password are required to login
\r
140 function _edititem($itemid, $username, $password, $catid, $title, $body, $more, $wasdraft, $publish, $closed) {
\r
144 $mem = new MEMBER();
\r
145 if (!$mem->login($username, $password))
\r
146 return _error(1,"Could not log in");
\r
148 // 2. check if allowed to add to blog
\r
149 if (!$manager->existsItem($itemid,1,1))
\r
150 return _error(6,"No such item ($itemid)");
\r
151 if (!$mem->canAlterItem($itemid))
\r
152 return _error(7,"Not allowed to alter item");
\r
155 ITEM::update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, 0);
\r
157 return new xmlrpcresp(new xmlrpcval(1,"boolean"));
\r
161 * Gives the list of blogs to which the user with given name and password has access
\r
163 function _getUsersBlogs($username, $password) {
\r
165 $mem = new MEMBER();
\r
166 if (!$mem->login($username, $password))
\r
167 return _error(1,"Could not log in");
\r
169 // 2. Get list of blogs
\r
171 $structarray = array();
\r
172 $query = "SELECT bnumber, bname, burl"
\r
173 . ' FROM '.sql_table('blog').', '.sql_table('team')
\r
174 . " WHERE tblog=bnumber and tmember=" . $mem->getID()
\r
175 . " ORDER BY bname";
\r
176 $r = sql_query($query);
\r
178 while ($obj = mysql_fetch_object($r)) {
\r
180 $blogurl = $obj->burl;
\r
182 $blogurl = 'http://';
\r
184 $newstruct = new xmlrpcval(array(
\r
185 "url" => new xmlrpcval($blogurl,"string"),
\r
186 "blogid" => new xmlrpcval($obj->bnumber,"string"),
\r
187 "blogName" => new xmlrpcval($obj->bname,"string")
\r
189 array_push($structarray, $newstruct);
\r
192 return new xmlrpcresp(new xmlrpcval( $structarray , "array"));
\r
196 function _getUserInfo($username, $password) {
\r
198 $mem = new MEMBER();
\r
199 if (!$mem->login($username, $password))
\r
200 return _error(1,"Could not log in");
\r
202 // 3. return the info
\r
203 // Structure returned has nickname, userid, url, email, lastname, firstname
\r
205 $newstruct = new xmlrpcval(array(
\r
206 "nickname" => new xmlrpcval($mem->getDisplayName(),"string"),
\r
207 "userid" => new xmlrpcval($mem->getID(),"string"),
\r
208 "url" => new xmlrpcval($mem->getURL(),"string"),
\r
209 "email" => new xmlrpcval($mem->getEmail(),"string"),
\r
210 "lastname" => new xmlrpcval("","string"),
\r
211 "firstname" => new xmlrpcval($mem->getRealName(),"string")
\r
214 return new xmlrpcresp($newstruct);
\r
222 function _deleteItem($itemid, $username, $password) {
\r
226 $mem = new MEMBER();
\r
227 if (!$mem->login($username, $password))
\r
228 return _error(1,"Could not log in");
\r
230 // 2. check if allowed
\r
231 if (!$manager->existsItem($itemid,1,1))
\r
232 return _error(6,"No such item ($itemid)");
\r
233 $blogid = getBlogIDFromItemID($itemid);
\r
234 if (!$mem->teamRights($blogid))
\r
235 return _error(3,"Not a team member");
\r
238 ITEM::delete($itemid);
\r
240 return new xmlrpcresp(new xmlrpcval(1,"boolean"));
\r
244 * Returns a template
\r
246 function _getSkinPart($blogid, $username, $password, $type) {
\r
248 $mem = new MEMBER();
\r
249 if (!$mem->login($username, $password))
\r
250 return _error(1,"Could not log in");
\r
252 // 2. check if allowed
\r
253 if (!BLOG::existsID($blogid))
\r
254 return _error(2,"No such blog ($blogid)");
\r
255 if (!$mem->teamRights($blogid))
\r
256 return _error(3,"Not a team member");
\r
258 // 3. return skin part
\r
259 $blog = new BLOG($blogid);
\r
260 $skin = new SKIN($blog->getDefaultSkin());
\r
261 return new xmlrpcresp(new xmlrpcval($skin->getContent($type),"string"));
\r
265 function _setSkinPart($blogid, $username, $password, $content, $type) {
\r
267 $mem = new MEMBER();
\r
268 if (!$mem->login($username, $password))
\r
269 return _error(1,"Could not log in");
\r
271 // 2. check if allowed
\r
272 if (!BLOG::existsID($blogid))
\r
273 return _error(2,"No such blog ($blogid)");
\r
274 if (!$mem->teamRights($blogid))
\r
275 return _error(3,"Not a team member");
\r
277 // 3. update skin part
\r
278 $blog = new BLOG($blogid);
\r
279 $skin = new SKIN($blog->getDefaultSkin());
\r
280 $skin->update($type, $content);
\r
282 return new xmlrpcresp(new xmlrpcval(1,'boolean'));
\r
286 * Some convenience methods
\r
289 function _getScalar($m, $idx) {
\r
290 $v = $m->getParam($idx);
\r
291 return $v->scalarval();
\r
294 function _getStructVal($struct, $key) {
\r
295 $t = $struct->structmem($key);
\r
296 return $t->scalarval();
\r
299 function _getArrayVal($a, $idx) {
\r
300 $t = $a->arraymem(0);
\r
301 return $t->scalarval();
\r
305 * Returns an XML-RPC error response
\r
306 * $err is the error number (>0, will be added to $xmlrpcerruser)
\r
308 function _error($err, $msg) {
\r
309 global $xmlrpcerruser;
\r
310 return new xmlrpcresp(0, $xmlrpcerruser + $err, $msg);
\r