OSDN Git Service

0266c09462d5d6dc78851a617ae78d23a85c4825
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / xmlrpc / server.php
1 <?php
2 /*
3  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4  * Copyright (C) 2002-2010 The Nucleus Group
5  *
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)
11  */
12
13 /**
14  * This script is provides an XML-RPC [1] interface to Nucleus [2].
15  *
16  * At this time, the Blogger API [3], the metaWeblog API [4] and
17  * parts of the Movable Type API [5] are implemented
18  *
19  * This script uses the the 'XML-RPC for PHP v1.02' implementation [6]
20  * All other code was written by Wouter Demuynck [7]
21  *
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/
29  *
30  *
31  * The Blogger API: (more info in the documentation)
32  *
33  *      blogger.newPost
34  *      blogger.editPost
35  *      blogger.getUsersBlogs
36  *      blogger.deletePost
37  *      blogger.getRecentPosts
38  *      blogger.getPost
39  *      blogger.getUserInfo
40  *      blogger.getTemplate
41  *      blogger.setTemplate
42  *
43  *      Note: The getUserInfo response contains an empty 'lastname' and the full name as
44  *       'firstname'
45  * Note: Blogger API methods only affect the body field of items
46  *
47  * The metaWeblog API (more info in documentation)
48  *
49  * metaWeblog.newPost
50  * metaWeblog.getPost
51  * metaWeblog.editPost
52  * metaWeblog.getCategories
53  * metaWeblog.newMediaObject
54  * metaWeblog.getRecentPosts
55  *
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)
58  *
59  * The Movable Type API
60  *
61  * mt.supportedMethods
62  *
63  * @license http://nucleuscms.org/license.txt GNU General Public License
64  * @copyright Copyright (C) 2002-2010 The Nucleus Group
65  * @version $Id$
66  * @version $NucleusJP: server.php,v 1.8.2.1 2007/09/07 07:12:42 kimitake Exp $
67  */
68 $CONF = array();
69 require("../../config.php");    // include Nucleus libs and code
70 //include($DIR_LIBS . "xmlrpc.inc.php");
71 //include($DIR_LIBS . "xmlrpcs.inc.php");
72 include_libs('xmlrpc.inc.php',false,false);
73 include_libs('xmlrpcs.inc.php',false,false);
74
75 /* define xmlrpc settings */
76 $xmlrpc_internalencoding = _CHARSET;
77 $xmlrpc_defencoding = 'UTF-8';
78
79 /* definition of available methods */
80
81 $functionDefs = array();
82
83 // load server functions
84 include('api_blogger.inc.php');
85 include('api_metaweblog.inc.php');
86 // include('api_nucleus.inc.php'); // uncomment if you still want to use the nucleus.* methods
87 include('api_mt.inc.php');
88
89
90 // create server
91 $s = new xmlrpc_server( $functionDefs );
92
93
94 /* ------------------------------ private functions ---------------------------------- */
95
96 /**
97   * Adds an item to the given blog. Username and password are required to login
98   */
99 function _addItem($blogid, $username, $password, $title, $body, $more, $publish, $closed, $catname = "") {
100         $blog = new BLOG($blogid);
101         $timestamp = $blog->getCorrectTime();
102         return _addDatedItem($blogid, $username, $password, $title, $body, $more, $publish, $closed, $timestamp, 0, $catname);
103 }
104
105 /**
106   * Adds item to blog, with time of item given
107   */
108 function _addDatedItem($blogid, $username, $password, $title, $body, $more, $publish, $closed, $timestamp, $future, $catname = "") {
109         // 1. login
110         $mem = new MEMBER();
111
112         if (!$mem->login($username, $password))
113                 return _error(1,"Could not log in");
114
115         // 2. check if allowed to add to blog
116         if (!BLOG::existsID($blogid))
117                 return _error(2,"No such blog ($blogid)");
118         if (!$mem->teamRights($blogid))
119                 return _error(3,"Not a team member");
120         if (!trim($body))
121                 return _error(4,"Cannot add empty items!");
122
123         // 3. calculate missing vars
124         $blog = new BLOG($blogid);
125
126         // get category id (or id for default category when false category)
127         $catid = $blog->getCategoryIdFromName($catname);
128
129         if ($publish == 1)
130                 $draft = 0;
131         else
132                 $draft = 1;
133         if ($closed != 1)
134                 $closed = 0;
135
136         // 4. add to blog
137         $itemid = $blog->additem($catid, $title, $body, $more, $blogid, $mem->getID(), $timestamp, $closed, $draft);
138
139         // [TODO] ping weblogs.com ?
140
141         return new xmlrpcresp(new xmlrpcval($itemid,"string"));
142 }
143
144 /**
145   * Updates an item. Username and password are required to login
146   */
147 function _edititem($itemid, $username, $password, $catid, $title, $body, $more, $wasdraft, $publish, $closed) {
148         global $manager;
149
150         // 1. login
151         $mem = new MEMBER();
152         if (!$mem->login($username, $password))
153                 return _error(1,"Could not log in");
154
155         // 2. check if allowed to add to blog
156         if (!$manager->existsItem($itemid,1,1))
157                 return _error(6,"No such item ($itemid)");
158         if (!$mem->canAlterItem($itemid))
159                 return _error(7,"Not allowed to alter item");
160
161         // 3. update item
162         ITEM::update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, 0);
163
164         return new xmlrpcresp(new xmlrpcval(1,"boolean"));
165 }
166
167 /**
168   * Gives the list of blogs to which the user with given name and password has access
169   */
170 function _getUsersBlogs($username, $password) {
171         // 1. Try to login
172         $mem = new MEMBER();
173         if (!$mem->login($username, $password))
174                 return _error(1,"Could not log in");
175
176         // 2. Get list of blogs
177
178         $structarray = array();
179         $query =  "SELECT bnumber, bname, burl"
180                         . ' FROM '.sql_table('blog').', '.sql_table('team')
181                         . " WHERE tblog=bnumber and tmember=" . $mem->getID()
182                         . " ORDER BY bname";
183         $r = sql_query($query);
184
185         while ($obj = sql_fetch_object($r)) {
186                 if ($obj->burl)
187                         $blogurl = $obj->burl;
188                 else
189                         $blogurl = 'http://';
190
191                 $newstruct = new xmlrpcval(array(
192                         "url" => new xmlrpcval($blogurl,"string"),
193                         "blogid" => new xmlrpcval($obj->bnumber,"string"),
194                         "blogName" => new xmlrpcval($obj->bname,"string")
195                 ),'struct');
196                 array_push($structarray, $newstruct);
197         }
198
199         return new xmlrpcresp(new xmlrpcval( $structarray , "array"));
200 }
201
202
203 function _getUserInfo($username, $password) {
204         // 1. login
205         $mem = new MEMBER();
206         if (!$mem->login($username, $password))
207                 return _error(1,"Could not log in");
208
209         // 3. return the info
210         // Structure returned has nickname, userid, url, email, lastname, firstname
211
212         $newstruct = new xmlrpcval(array(
213                 "nickname" => new xmlrpcval($mem->getDisplayName(),"string"),
214                 "userid" => new xmlrpcval($mem->getID(),"string"),
215                 "url" => new xmlrpcval($mem->getURL(),"string"),
216                 "email" => new xmlrpcval($mem->getEmail(),"string"),
217                 "lastname" => new xmlrpcval("","string"),
218                 "firstname" => new xmlrpcval($mem->getRealName(),"string")
219         ),'struct');
220
221         return new xmlrpcresp($newstruct);
222
223
224 }
225
226 /**
227   * deletes an item
228   */
229 function _deleteItem($itemid, $username, $password) {
230         global $manager;
231
232         // 1. login
233         $mem = new MEMBER();
234         if (!$mem->login($username, $password))
235                 return _error(1,"Could not log in");
236
237         // 2. check if allowed
238         if (!$manager->existsItem($itemid,1,1))
239                 return _error(6,"No such item ($itemid)");
240         $blogid = getBlogIDFromItemID($itemid);
241         if (!$mem->teamRights($blogid))
242                 return _error(3,"Not a team member");
243
244         // delete the item
245         ITEM::delete($itemid);
246
247         return new xmlrpcresp(new xmlrpcval(1,"boolean"));
248 }
249
250 /**
251   * Returns a template
252   */
253 function _getSkinPart($blogid, $username, $password, $type) {
254         // 1. login
255         $mem = new MEMBER();
256         if (!$mem->login($username, $password))
257                 return _error(1,"Could not log in");
258
259         // 2. check if allowed
260         if (!BLOG::existsID($blogid))
261                 return _error(2,"No such blog ($blogid)");
262         if (!$mem->teamRights($blogid))
263                 return _error(3,"Not a team member");
264
265         // 3. return skin part
266         $blog = new BLOG($blogid);
267         $skin = new SKIN($blog->getDefaultSkin());
268         return new xmlrpcresp(new xmlrpcval($skin->getContent($type),"string"));
269
270 }
271
272 function _setSkinPart($blogid, $username, $password, $content, $type) {
273         // 1. login
274         $mem = new MEMBER();
275         if (!$mem->login($username, $password))
276                 return _error(1,"Could not log in");
277
278         // 2. check if allowed
279         if (!BLOG::existsID($blogid))
280                 return _error(2,"No such blog ($blogid)");
281         if (!$mem->teamRights($blogid))
282                 return _error(3,"Not a team member");
283
284         // 3. update skin part
285         $blog = new BLOG($blogid);
286         $skin = new SKIN($blog->getDefaultSkin());
287         $skin->update($type, $content);
288
289         return new xmlrpcresp(new xmlrpcval(1,'boolean'));
290 }
291
292 /**
293   * Some convenience methods
294   */
295
296 function _getScalar($m, $idx) {
297         $v = $m->getParam($idx);
298         return $v->scalarval();
299 }
300
301 function _getStructVal($struct, $key) {
302         $t = $struct->structmem($key);
303         if (!$t) 
304                 return '';      // no such struct value
305         else
306                 return $t->scalarval();
307 }
308
309 function _getArrayVal($a, $idx) {
310         $t = $a->arraymem($idx);
311         return $t->scalarval();
312 }
313
314 /**
315   * Returns an XML-RPC error response
316   * $err is the error number (>0, will be added to $xmlrpcerruser)
317   */
318 function _error($err, $msg) {
319         global $xmlrpcerruser;
320         return new xmlrpcresp(0, $xmlrpcerruser + $err, $msg);
321 }
322 ?>