OSDN Git Service

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