OSDN Git Service

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