OSDN Git Service

changed copyright
[nucleus-jp/nucleus-jp-ancient.git] / euc / nucleus / xmlrpc / server.php
1 <?php\r
2 /** \r
3   * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/) \r
4   * Copyright (C) 2002-2005 The Nucleus Group\r
5   *\r
6   * This program is free software; you can redistribute it and/or\r
7   * modify it under the terms of the GNU General Public License\r
8   * as published by the Free Software Foundation; either version 2\r
9   * of the License, or (at your option) any later version.\r
10   * (see nucleus/documentation/index.html#license for more info)\r
11   *\r
12   * $Id: server.php,v 1.5 2005-03-16 08:12:06 kimitake Exp $\r
13   * $NucleusJP: server.php,v 1.3 2005/03/12 06:19:06 kimitake Exp $\r
14   *\r
15   * This script is provides an XML-RPC [1] interface to Nucleus [2].\r
16   *\r
17   * At this time, the Blogger API [3], the metaWeblog API [4] and \r
18   * parts of the Movable Type API [5] are implemented\r
19   *\r
20   * This script uses the the 'XML-RPC for PHP v1.02' implementation [6]\r
21   * All other code was written by Wouter Demuynck [7]\r
22   *\r
23   * [1] http://www.xmlrpc.com/\r
24   * [2] http://nucleuscms.org/\r
25   * [3] http://plant.blogger.com/api/\r
26   * [4] http://www.xmlrpc.com/metaWeblogApi\r
27   * [5] http://www.movabletype.org/docs/mtmanual_programmatic.html\r
28   * [6] http://phpxmlrpc.sourceforge.net/\r
29   * [7] http://demuynck.org/\r
30   *\r
31   *\r
32   * The Blogger API: (more info in the documentation)\r
33   *\r
34   *     blogger.newPost \r
35   *     blogger.editPost \r
36   *     blogger.getUsersBlogs \r
37   *     blogger.deletePost\r
38   *     blogger.getRecentPosts\r
39   *     blogger.getPost\r
40   *     blogger.getUserInfo\r
41   *     blogger.getTemplate\r
42   *     blogger.setTemplate\r
43   *\r
44   *     Note: The getUserInfo response contains an empty 'lastname' and the full name as \r
45   *       'firstname'\r
46   * Note: Blogger API methods only affect the body field of items\r
47   *\r
48   * The metaWeblog API (more info in documentation)\r
49   *\r
50   * metaWeblog.newPost\r
51   * metaWeblog.getPost\r
52   * metaWeblog.editPost\r
53   * metaWeblog.getCategories\r
54   * metaWeblog.newMediaObject\r
55   * metaWeblog.getRecentPosts\r
56   *\r
57   * Note: metaWeblog API methods only affect the body and title fields of items. \r
58   *       the extended part is left untouched (and empty for new posts)\r
59   *\r
60   * The Movable Type API\r
61   *\r
62   * mt.supportedMethods\r
63   */\r
64 $CONF = array();\r
65 include("../../config.php");    // include Nucleus libs and code\r
66 include($DIR_LIBS . "xmlrpc.inc.php");\r
67 include($DIR_LIBS . "xmlrpcs.inc.php");\r
68 \r
69 /* definition of available methods */\r
70 \r
71 $functionDefs = array();\r
72 \r
73 // load server functions \r
74 include('api_blogger.inc.php');\r
75 include('api_metaweblog.inc.php');\r
76 // include('api_nucleus.inc.php'); // uncomment if you still want to use the nucleus.* methods\r
77 include('api_mt.inc.php');\r
78 \r
79 \r
80 // create server\r
81 $s = new xmlrpc_server( $functionDefs );\r
82 \r
83 \r
84 /* ------------------------------ private functions ---------------------------------- */\r
85 \r
86 /**\r
87   * Adds an item to the given blog. Username and password are required to login\r
88   */\r
89 function _addItem($blogid, $username, $password, $title, $body, $more, $publish, $closed, $catname = "") {\r
90         $blog = new BLOG($blogid);\r
91         $timestamp = $blog->getCorrectTime();\r
92         return _addDatedItem($blogid, $username, $password, $title, $body, $more, $publish, $closed, $timestamp, 0, $catname);\r
93 }\r
94 \r
95 /** \r
96   * Adds item to blog, with time of item given\r
97   */\r
98 function _addDatedItem($blogid, $username, $password, $title, $body, $more, $publish, $closed, $timestamp, $future, $catname = "") {\r
99         // 1. login\r
100         $mem = new MEMBER();\r
101         \r
102         if (!$mem->login($username, $password))\r
103                 return _error(1,"Could not log in");\r
104 \r
105         // 2. check if allowed to add to blog\r
106         if (!BLOG::existsID($blogid))\r
107                 return _error(2,"No such blog ($blogid)");\r
108         if (!$mem->teamRights($blogid))\r
109                 return _error(3,"Not a team member");\r
110         if (!trim($body))\r
111                 return _error(4,"Cannot add empty items!");\r
112 \r
113         // 3. calculate missing vars\r
114         $blog = new BLOG($blogid);\r
115         \r
116         // get category id (or id for default category when false category)\r
117         $catid = $blog->getCategoryIdFromName($catname);\r
118 \r
119         if ($publish == 1)\r
120                 $draft = 0;\r
121         else\r
122                 $draft = 1;\r
123         if ($closed != 1)\r
124                 $closed = 0;\r
125 \r
126         $title = mb_convert_encoding($title, _CHARSET, "UTF-8");\r
127         $body = mb_convert_encoding($body, _CHARSET, "UTF-8");\r
128         \r
129         // 4. add to blog\r
130         $itemid = $blog->additem($catid, $title, $body, $more, $blogid, $mem->getID(), $timestamp, $closed, $draft);\r
131         \r
132         // [TODO] ping weblogs.com ?\r
133 \r
134         return new xmlrpcresp(new xmlrpcval($itemid,"string"));\r
135 }\r
136 \r
137 /**\r
138   * Updates an item. Username and password are required to login\r
139   */\r
140 function _edititem($itemid, $username, $password, $catid, $title, $body, $more, $wasdraft, $publish, $closed) {\r
141         global $manager;\r
142         \r
143         // 1. login\r
144         $mem = new MEMBER();\r
145         if (!$mem->login($username, $password))\r
146                 return _error(1,"Could not log in");\r
147         \r
148         // 2. check if allowed to add to blog\r
149         if (!$manager->existsItem($itemid,1,1))\r
150                 return _error(6,"No such item ($itemid)");\r
151         if (!$mem->canAlterItem($itemid))\r
152                 return _error(7,"Not allowed to alter item");\r
153                 \r
154         // 3. update item\r
155         ITEM::update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, 0);\r
156         \r
157         return new xmlrpcresp(new xmlrpcval(1,"boolean"));\r
158 }\r
159 \r
160 /**\r
161   * Gives the list of blogs to which the user with given name and password has access\r
162   */\r
163 function _getUsersBlogs($username, $password) {\r
164         // 1. Try to login\r
165         $mem = new MEMBER();\r
166         if (!$mem->login($username, $password))\r
167                 return _error(1,"Could not log in");\r
168 \r
169         // 2. Get list of blogs\r
170         \r
171         $structarray = array();\r
172         $query =  "SELECT bnumber, bname, burl"\r
173                 . ' FROM '.sql_table('blog').', '.sql_table('team')\r
174                 . " WHERE tblog=bnumber and tmember=" . $mem->getID()\r
175                 . " ORDER BY bname";\r
176         $r = sql_query($query);\r
177         \r
178         while ($obj = mysql_fetch_object($r)) {\r
179                 if ($obj->burl)\r
180                         $blogurl = $obj->burl;\r
181                 else\r
182                         $blogurl = 'http://';\r
183         \r
184                 $newstruct = new xmlrpcval(array(\r
185                         "url" => new xmlrpcval($blogurl,"string"),\r
186                         "blogid" => new xmlrpcval($obj->bnumber,"string"),\r
187                         "blogName" => new xmlrpcval($obj->bname,"string")\r
188                 ),'struct');\r
189                 array_push($structarray, $newstruct);\r
190         }\r
191         \r
192         return new xmlrpcresp(new xmlrpcval( $structarray , "array"));\r
193 }\r
194 \r
195 \r
196 function _getUserInfo($username, $password) {\r
197         // 1. login\r
198         $mem = new MEMBER();\r
199         if (!$mem->login($username, $password))\r
200                 return _error(1,"Could not log in");\r
201 \r
202         // 3. return the info\r
203         // Structure returned has nickname, userid, url, email, lastname, firstname\r
204         \r
205         $newstruct = new xmlrpcval(array(\r
206                 "nickname" => new xmlrpcval($mem->getDisplayName(),"string"),\r
207                 "userid" => new xmlrpcval($mem->getID(),"string"),\r
208                 "url" => new xmlrpcval($mem->getURL(),"string"),\r
209                 "email" => new xmlrpcval($mem->getEmail(),"string"),\r
210                 "lastname" => new xmlrpcval("","string"),\r
211                 "firstname" => new xmlrpcval($mem->getRealName(),"string")\r
212         ),'struct');\r
213         \r
214         return new xmlrpcresp($newstruct);\r
215         \r
216 \r
217 }\r
218 \r
219 /**\r
220   * deletes an item\r
221   */\r
222 function _deleteItem($itemid, $username, $password) {\r
223         global $manager;\r
224 \r
225         // 1. login\r
226         $mem = new MEMBER();\r
227         if (!$mem->login($username, $password))\r
228                 return _error(1,"Could not log in");\r
229 \r
230         // 2. check if allowed \r
231         if (!$manager->existsItem($itemid,1,1))\r
232                 return _error(6,"No such item ($itemid)");\r
233         $blogid = getBlogIDFromItemID($itemid);\r
234         if (!$mem->teamRights($blogid))\r
235                 return _error(3,"Not a team member");\r
236                 \r
237         // delete the item\r
238         ITEM::delete($itemid);  \r
239         \r
240         return new xmlrpcresp(new xmlrpcval(1,"boolean"));\r
241 }\r
242 \r
243 /**\r
244   * Returns a template\r
245   */\r
246 function _getSkinPart($blogid, $username, $password, $type) {\r
247         // 1. login\r
248         $mem = new MEMBER();\r
249         if (!$mem->login($username, $password))\r
250                 return _error(1,"Could not log in");\r
251                 \r
252         // 2. check if allowed\r
253         if (!BLOG::existsID($blogid))\r
254                 return _error(2,"No such blog ($blogid)");\r
255         if (!$mem->teamRights($blogid))\r
256                 return _error(3,"Not a team member");\r
257         \r
258         // 3. return skin part\r
259         $blog = new BLOG($blogid);\r
260         $skin = new SKIN($blog->getDefaultSkin());\r
261         return new xmlrpcresp(new xmlrpcval($skin->getContent($type),"string"));\r
262         \r
263 }\r
264 \r
265 function _setSkinPart($blogid, $username, $password, $content, $type) {\r
266         // 1. login\r
267         $mem = new MEMBER();\r
268         if (!$mem->login($username, $password))\r
269                 return _error(1,"Could not log in");\r
270                 \r
271         // 2. check if allowed\r
272         if (!BLOG::existsID($blogid))\r
273                 return _error(2,"No such blog ($blogid)");\r
274         if (!$mem->teamRights($blogid))\r
275                 return _error(3,"Not a team member");\r
276                 \r
277         // 3. update skin part\r
278         $blog = new BLOG($blogid);\r
279         $skin = new SKIN($blog->getDefaultSkin());\r
280         $skin->update($type, $content);\r
281         \r
282         return new xmlrpcresp(new xmlrpcval(1,'boolean'));\r
283 }\r
284 \r
285 /**\r
286   * Some convenience methods\r
287   */\r
288   \r
289 function _getScalar($m, $idx) {\r
290         $v = $m->getParam($idx);\r
291         return $v->scalarval();\r
292 }\r
293 \r
294 function _getStructVal($struct, $key) {\r
295         $t = $struct->structmem($key);\r
296         return $t->scalarval();\r
297 }\r
298 \r
299 function _getArrayVal($a, $idx) {\r
300         $t = $a->arraymem(0);\r
301         return $t->scalarval();\r
302 }\r
303 \r
304 /**\r
305   * Returns an XML-RPC error response\r
306   * $err is the error number (>0, will be added to $xmlrpcerruser)\r
307   */\r
308 function _error($err, $msg) {\r
309         global $xmlrpcerruser;\r
310         return new xmlrpcresp(0, $xmlrpcerruser + $err, $msg);\r
311 }\r
312 ?>\r