OSDN Git Service

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