OSDN Git Service

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