OSDN Git Service

FIX:変数名の誤記を修正
[nucleus-jp/nucleus-next.git] / nucleus / xmlrpc / server.php
1 <?php
2 /*
3  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4  * Copyright (C) 2002-2012 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-2012 The Nucleus Group
65  * @version $Id: server.php 1622 2012-01-09 03:18:59Z sakamocchi $
66  */
67 $CONF = array();
68 $DIR_LIBS = '';
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 = i18n::get_current_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         global $manager;
101         $blog =& $manager->getBlog($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 = '0', $timestamp, $future, $catname = "") {
110         global $manager;
111         
112         // 1. login
113         $mem = new Member();
114
115         if (!$mem->login($username, $password))
116                 return _error(1,"Could not log in");
117
118         // 2. check if allowed to add to blog
119         if (!Blog::existsID($blogid))
120                 return _error(2,"No such blog ($blogid)");
121         if (!$mem->teamRights($blogid))
122                 return _error(3,"Not a team member");
123         if (!trim($body))
124                 return _error(4,"Cannot add empty items!");
125
126         // 3. calculate missing vars
127         $blog =& $manager->getBlog($blogid);
128
129         // get category id (or id for default category when false category)
130         $catid = $blog->getCategoryIdFromName($catname);
131
132         if ($publish == 1) {
133                 $draft = 0;
134         }
135         else {
136                 $draft = 1;
137         }
138         
139         // not needed because BLOG:additem has the same code
140         /*if ($closed != 1)
141         {$closed = 0;}*/
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         // 3. update item
169         Item::update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, 0);
170
171         return new xmlrpcresp(new xmlrpcval(1,"boolean"));
172 }
173
174 /**
175   * Gives the list of blogs to which the user with given name and password has access
176   */
177 function _getUsersBlogs($username, $password) {
178         // 1. Try to login
179         $mem = new Member();
180         if (!$mem->login($username, $password))
181                 return _error(1,"Could not log in");
182
183         // 2. Get list of blogs
184
185         $structarray = array();
186         $query =  "SELECT bnumber, bname, burl"
187                         . ' FROM '.sql_table('blog').', '.sql_table('team')
188                         . " WHERE tblog=bnumber and tmember=" . $mem->getID()
189                         . " ORDER BY bname";
190         $r = DB::getResult($query);
191         
192         foreach ( $r as $row )
193         {
194                 if ( $row['burl'] )
195                 {
196                         $blogurl = $row['burl'];
197                 }
198                 if ( $obj->burl )
199                 {
200                         $blogurl = $obj->burl;
201                 }
202                 else
203                 {
204                         $blogurl = 'http://';
205                 }
206                 
207                 $newstruct = new xmlrpcval(array(
208                         "url"           => new xmlrpcval($blogurl, "string"),
209                         "blogid"        => new xmlrpcval($row['bnumber'], "string"),
210                         "blogName"      => new xmlrpcval($row['bname'], "string")
211                 ),'struct');
212                 array_push($structarray, $newstruct);
213         }
214         
215         return new xmlrpcresp(new xmlrpcval( $structarray , "array"));
216 }
217
218
219 function _getUserInfo($username, $password) {
220         // 1. login
221         $mem = new Member();
222         if (!$mem->login($username, $password))
223                 return _error(1,"Could not log in");
224
225         // 3. return the info
226         // Structure returned has nickname, userid, url, email, lastname, firstname
227
228         $newstruct = new xmlrpcval(array(
229                 "nickname" => new xmlrpcval($mem->getDisplayName(),"string"),
230                 "userid" => new xmlrpcval($mem->getID(),"string"),
231                 "url" => new xmlrpcval($mem->getURL(),"string"),
232                 "email" => new xmlrpcval($mem->getEmail(),"string"),
233                 "lastname" => new xmlrpcval("","string"),
234                 "firstname" => new xmlrpcval($mem->getRealName(),"string")
235         ),'struct');
236
237         return new xmlrpcresp($newstruct);
238
239
240 }
241
242 /**
243   * deletes an item
244   */
245 function _deleteItem($itemid, $username, $password) {
246         global $manager;
247
248         // 1. login
249         $mem = new Member();
250         if (!$mem->login($username, $password))
251                 return _error(1,"Could not log in");
252
253         // 2. check if allowed
254         if (!$manager->existsItem($itemid,1,1))
255                 return _error(6,"No such item ($itemid)");
256         $blogid = getBlogIDFromItemID($itemid);
257         if (!$mem->teamRights($blogid))
258                 return _error(3,"Not a team member");
259
260         // delete the item
261         Item::delete($itemid);
262
263         return new xmlrpcresp(new xmlrpcval(1,"boolean"));
264 }
265
266 /**
267   * Returns a template
268   */
269 function _getSkinPart($blogid, $username, $password, $type) {
270         global $manager;
271         
272         // 1. login
273         $mem = new Member();
274         if (!$mem->login($username, $password))
275                 return _error(1,"Could not log in");
276
277         // 2. check if allowed
278         if (!Blog::existsID($blogid))
279                 return _error(2,"No such blog ($blogid)");
280         if (!$mem->teamRights($blogid))
281                 return _error(3,"Not a team member");
282
283         // 3. return skin part
284         $blog =& $manager->getBlog($blogid);
285         $skin = new SKIN($blog->getDefaultSkin());
286         return new xmlrpcresp(new xmlrpcval($skin->getContentFromDB($type),"string"));
287
288 }
289
290 function _setSkinPart($blogid, $username, $password, $content, $type) {
291         global $manager;
292         
293         // 1. login
294         $mem = new Member();
295         if (!$mem->login($username, $password))
296                 return _error(1,"Could not log in");
297
298         // 2. check if allowed
299         if (!Blog::existsID($blogid))
300                 return _error(2,"No such blog ($blogid)");
301         if (!$mem->teamRights($blogid))
302                 return _error(3,"Not a team member");
303
304         // 3. update skin part
305         $blog =& $manager->getBlog($blogid);
306         $skin = new SKIN($blog->getDefaultSkin());
307         $skin->update($type, $content);
308
309         return new xmlrpcresp(new xmlrpcval(1,'boolean'));
310 }
311
312 /**
313   * Some convenience methods
314   */
315
316 function _getScalar($m, $idx) {
317         $v = $m->getParam($idx);
318         return $v->scalarval();
319 }
320
321 function _getStructVal($struct, $key) {
322         $t = $struct->structmem($key);
323         if (!$t) 
324                 return '';      // no such struct value
325         else
326                 return $t->scalarval();
327 }
328
329 function _getArrayVal($a, $idx) {
330         $t = $a->arraymem($idx);
331         return $t->scalarval();
332 }
333
334 /**
335   * Returns an XML-RPC error response
336   * $err is the error number (>0, will be added to $xmlrpcerruser)
337   */
338 function _error($err, $msg) {
339         global $xmlrpcerruser;
340         return new xmlrpcresp(0, $xmlrpcerruser + $err, $msg);
341 }
342 ?>