OSDN Git Service

eeef714898297658146580d70e8d525d4093d7f8
[nucleus-jp/nucleus-jp-ancient.git] / euc / nucleus / xmlrpc / server.php
1 <?php
2 /*
3  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4  * Copyright (C) 2002-2007 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-2007 The Nucleus Group
65  * @version $Id: server.php,v 1.8 2007-03-27 12:14:05 kimitake Exp $
66  * @version $NucleusJP: server.php,v 1.8 2007/03/20 19:33:25 kmorimatsu 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
73 /* define xmlrpc settings */
74 $xmlrpc_internalencoding = 'UTF-8';
75 $xmlrpc_defencoding = 'UTF-8';
76
77 /* definition of available methods */
78
79 $functionDefs = array();
80
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');
86
87
88 // create server
89 $s = new xmlrpc_server( $functionDefs );
90
91
92 /* ------------------------------ private functions ---------------------------------- */
93
94 /**
95   * Adds an item to the given blog. Username and password are required to login
96   */
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);
101 }
102
103 /**
104   * Adds item to blog, with time of item given
105   */
106 function _addDatedItem($blogid, $username, $password, $title, $body, $more, $publish, $closed, $timestamp, $future, $catname = "") {
107         // 1. login
108         $mem = new MEMBER();
109
110         if (!$mem->login($username, $password))
111                 return _error(1,"Could not log in");
112
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");
118         if (!trim($body))
119                 return _error(4,"Cannot add empty items!");
120
121         // 3. calculate missing vars
122         $blog = new BLOG($blogid);
123
124         // get category id (or id for default category when false category)
125         $catid = $blog->getCategoryIdFromName($catname);
126
127         if ($publish == 1)
128                 $draft = 0;
129         else
130                 $draft = 1;
131         if ($closed != 1)
132                 $closed = 0;
133
134         $title = mb_convert_encoding($title, _CHARSET, "UTF-8");
135         $body = mb_convert_encoding($body, _CHARSET, "UTF-8");
136         $more = mb_convert_encoding($more, _CHARSET, "UTF-8");
137
138         // 4. add to blog
139         $itemid = $blog->additem($catid, $title, $body, $more, $blogid, $mem->getID(), $timestamp, $closed, $draft);
140
141         // [TODO] ping weblogs.com ?
142
143         return new xmlrpcresp(new xmlrpcval($itemid,"string"));
144 }
145
146 /**
147   * Updates an item. Username and password are required to login
148   */
149 function _edititem($itemid, $username, $password, $catid, $title, $body, $more, $wasdraft, $publish, $closed) {
150         global $manager;
151
152         // 1. login
153         $mem = new MEMBER();
154         if (!$mem->login($username, $password))
155                 return _error(1,"Could not log in");
156
157         // 2. check if allowed to add to blog
158         if (!$manager->existsItem($itemid,1,1))
159                 return _error(6,"No such item ($itemid)");
160         if (!$mem->canAlterItem($itemid))
161                 return _error(7,"Not allowed to alter item");
162
163         $title = mb_convert_encoding($title, _CHARSET, "EUC-JP,UTF-8");
164         $body = mb_convert_encoding($body, _CHARSET, "EUC-JP,UTF-8");
165         $more = mb_convert_encoding($more, _CHARSET, "EUC-JP,UTF-8");
166
167         // 3. update item
168         ITEM::update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, 0);
169
170         return new xmlrpcresp(new xmlrpcval(1,"boolean"));
171 }
172
173 /**
174   * Gives the list of blogs to which the user with given name and password has access
175   */
176 function _getUsersBlogs($username, $password) {
177         // 1. Try to login
178         $mem = new MEMBER();
179         if (!$mem->login($username, $password))
180                 return _error(1,"Could not log in");
181
182         // 2. Get list of blogs
183
184         $structarray = array();
185         $query =  "SELECT bnumber, bname, burl"
186                         . ' FROM '.sql_table('blog').', '.sql_table('team')
187                         . " WHERE tblog=bnumber and tmember=" . $mem->getID()
188                         . " ORDER BY bname";
189         $r = sql_query($query);
190
191         while ($obj = mysql_fetch_object($r)) {
192                 if ($obj->burl)
193                         $blogurl = $obj->burl;
194                 else
195                         $blogurl = 'http://';
196
197                 $newstruct = new xmlrpcval(array(
198                         "url" => new xmlrpcval($blogurl,"string"),
199                         "blogid" => new xmlrpcval($obj->bnumber,"string"),
200                         "blogName" => new xmlrpcval($obj->bname,"string")
201                 ),'struct');
202                 array_push($structarray, $newstruct);
203         }
204
205         return new xmlrpcresp(new xmlrpcval( $structarray , "array"));
206 }
207
208
209 function _getUserInfo($username, $password) {
210         // 1. login
211         $mem = new MEMBER();
212         if (!$mem->login($username, $password))
213                 return _error(1,"Could not log in");
214
215         // 3. return the info
216         // Structure returned has nickname, userid, url, email, lastname, firstname
217
218         $newstruct = new xmlrpcval(array(
219                 "nickname" => new xmlrpcval($mem->getDisplayName(),"string"),
220                 "userid" => new xmlrpcval($mem->getID(),"string"),
221                 "url" => new xmlrpcval($mem->getURL(),"string"),
222                 "email" => new xmlrpcval($mem->getEmail(),"string"),
223                 "lastname" => new xmlrpcval("","string"),
224                 "firstname" => new xmlrpcval($mem->getRealName(),"string")
225         ),'struct');
226
227         return new xmlrpcresp($newstruct);
228
229
230 }
231
232 /**
233   * deletes an item
234   */
235 function _deleteItem($itemid, $username, $password) {
236         global $manager;
237
238         // 1. login
239         $mem = new MEMBER();
240         if (!$mem->login($username, $password))
241                 return _error(1,"Could not log in");
242
243         // 2. check if allowed
244         if (!$manager->existsItem($itemid,1,1))
245                 return _error(6,"No such item ($itemid)");
246         $blogid = getBlogIDFromItemID($itemid);
247         if (!$mem->teamRights($blogid))
248                 return _error(3,"Not a team member");
249
250         // delete the item
251         ITEM::delete($itemid);
252
253         return new xmlrpcresp(new xmlrpcval(1,"boolean"));
254 }
255
256 /**
257   * Returns a template
258   */
259 function _getSkinPart($blogid, $username, $password, $type) {
260         // 1. login
261         $mem = new MEMBER();
262         if (!$mem->login($username, $password))
263                 return _error(1,"Could not log in");
264
265         // 2. check if allowed
266         if (!BLOG::existsID($blogid))
267                 return _error(2,"No such blog ($blogid)");
268         if (!$mem->teamRights($blogid))
269                 return _error(3,"Not a team member");
270
271         // 3. return skin part
272         $blog = new BLOG($blogid);
273         $skin = new SKIN($blog->getDefaultSkin());
274         return new xmlrpcresp(new xmlrpcval($skin->getContent($type),"string"));
275
276 }
277
278 function _setSkinPart($blogid, $username, $password, $content, $type) {
279         // 1. login
280         $mem = new MEMBER();
281         if (!$mem->login($username, $password))
282                 return _error(1,"Could not log in");
283
284         // 2. check if allowed
285         if (!BLOG::existsID($blogid))
286                 return _error(2,"No such blog ($blogid)");
287         if (!$mem->teamRights($blogid))
288                 return _error(3,"Not a team member");
289
290         // 3. update skin part
291         $blog = new BLOG($blogid);
292         $skin = new SKIN($blog->getDefaultSkin());
293         $skin->update($type, $content);
294
295         return new xmlrpcresp(new xmlrpcval(1,'boolean'));
296 }
297
298 /**
299   * Some convenience methods
300   */
301
302 function _getScalar($m, $idx) {
303         $v = $m->getParam($idx);
304         return $v->scalarval();
305 }
306
307 function _getStructVal($struct, $key) {
308         $t = $struct->structmem($key);
309         if (!$t) 
310                 return '';      // no such struct value
311         else
312                 return $t->scalarval();
313 }
314
315 function _getArrayVal($a, $idx) {
316         $t = $a->arraymem($idx);
317         return $t->scalarval();
318 }
319
320 /**
321   * Returns an XML-RPC error response
322   * $err is the error number (>0, will be added to $xmlrpcerruser)
323   */
324 function _error($err, $msg) {
325         global $xmlrpcerruser;
326         return new xmlrpcresp(0, $xmlrpcerruser + $err, $msg);
327 }
328 ?>