OSDN Git Service

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