OSDN Git Service

be966df4f86a435118bca1152ab25f6998e5eb8c
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / xmlrpc / api_mt.inc.php
1 <?php\r
2 \r
3 /** \r
4   * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/) \r
5   * Copyright (C) 2002-2005 The Nucleus Group\r
6   *\r
7   * This program is free software; you can redistribute it and/or\r
8   * modify it under the terms of the GNU General Public License\r
9   * as published by the Free Software Foundation; either version 2\r
10   * of the License, or (at your option) any later version.\r
11   * (see nucleus/documentation/index.html#license for more info)\r
12   *\r
13   * $Id: api_mt.inc.php,v 1.3 2005-03-12 06:19:06 kimitake Exp $\r
14   * $NucleusJP$\r
15   */\r
16 \r
17 /*\r
18  * This file contains definitions for the methods in the Movable Type API\r
19  *\r
20  * Wouter Demuynck 2003-08-31\r
21  */\r
22 \r
23 \r
24         // mt.supportedMethods\r
25         $f_mt_supportedMethods_sig = array(array(\r
26                         // return type\r
27                         $xmlrpcArray // array of strings\r
28                 ));\r
29         $f_mt_supportedMethods_doc = 'returns an array of supported methods';\r
30         function f_mt_supportedMethods($m) {\r
31                 $res = new xmlrpcresp(new xmlrpcval( \r
32                         array(\r
33                                 new xmlrpcval('mt.supportedMethods', 'string'),\r
34                                 new xmlrpcval('mt.supportedTextFilters', 'string'),\r
35                                 new xmlrpcval('mt.publishPost', 'string'),\r
36                                 new xmlrpcval('mt.getCategoryList', 'string'),\r
37                                 new xmlrpcval('mt.getPostCategories', 'string'),\r
38                                 new xmlrpcval('mt.setPostCategories', 'string'),\r
39                                 new xmlrpcval('mt.getRecentPostTitles', 'string'),\r
40                                 new xmlrpcval('mt.getTrackbackPings','string'),\r
41                         ), 'array')\r
42                 );\r
43                 return $res;\r
44         }\r
45         \r
46         // mt.supportedTextFilters\r
47         $f_mt_supportedTextFilters_sig = array(array(\r
48                         // return type\r
49                         $xmlrpcArray    // array of structs\r
50                 ));\r
51         $f_mt_supportedTextFilters_doc = 'returns the supported text filters';\r
52         function f_mt_supportedTextFilters($m) {\r
53                 $res = new xmlrpcresp(new xmlrpcval( \r
54                         array(\r
55                                 // no text filters in nucleus\r
56                         ), 'array')\r
57                 );\r
58                 return $res;\r
59         }\r
60         \r
61         // mt.getCategoryList\r
62         $f_mt_getCategoryList_sig = array(array(\r
63                         // return type\r
64                         $xmlrpcArray,           // array of structs\r
65                         \r
66                         // params\r
67                         $xmlrpcString,          // blogid\r
68                         $xmlrpcString,          // username\r
69                         $xmlrpcString           // password\r
70                         \r
71                 ));\r
72         $f_mt_getCategoryList_doc = 'Returns a list of all categories defined in the weblog';\r
73         function f_mt_getCategoryList($m) {\r
74                 $blogid =       _getScalar($m,0);\r
75                 $username =     _getScalar($m,1);\r
76                 $password =     _getScalar($m,2);\r
77 \r
78                 return _mt_categoryList($blogid, $username, $password);\r
79         }\r
80         \r
81         // mt.publishPost\r
82         $f_mt_publishPost_sig = array(array(\r
83                         // return type\r
84                         $xmlrpcBoolean,         // true\r
85                         \r
86                         // params\r
87                         $xmlrpcString,          // itemid\r
88                         $xmlrpcString,          // username\r
89                         $xmlrpcString           // password\r
90                 ));\r
91         $f_mt_publishPost_doc = 'Transfers an item from the "draft" state to the "published" state. For items that were published earlier, does nothing.';\r
92         function f_mt_publishPost($m) {\r
93                 $itemid         = intval(_getScalar($m, 0));\r
94                 $username       = _getScalar($m, 1);\r
95                 $password       = _getScalar($m, 2);            \r
96                 \r
97                 return _mt_publishPost($itemid, $username, $password);\r
98         }\r
99         \r
100         // mt.getPostCategories\r
101         $f_mt_getPostCategories_sig = array(array(\r
102                 // return\r
103                 $xmlrpcArray,           // array of structs\r
104                 // parameters\r
105                 $xmlrpcString,          // itemid\r
106                 $xmlrpcString,          // username\r
107                 $xmlrpcString           // password\r
108         ));\r
109         $f_mt_getPostCategories_doc = 'Returns a list of all categories to which the post is assigned.';\r
110         function f_mt_getPostCategories($m) {\r
111                 $itemid         = intval(_getScalar($m, 0));\r
112                 $username       = _getScalar($m, 1);\r
113                 $password       = _getScalar($m, 2);\r
114                 \r
115                 return _mt_getPostCategories($itemid, $username, $password);\r
116         }\r
117 \r
118         // mt.setPostCategories\r
119         $f_mt_setPostCategories_sig = array(array(\r
120                 // return\r
121                 $xmlrpcBoolean,         // true\r
122                 // parameters\r
123                 $xmlrpcString,          // itemid\r
124                 $xmlrpcString,          // username\r
125                 $xmlrpcString,          // password\r
126                 $xmlrpcArray            // categories\r
127         ));\r
128         $f_mt_setPostCategories_doc = 'Sets the categories for a post. Only the primary category will be stored';\r
129         function f_mt_setPostCategories($m) {\r
130                 $itemid         = intval(_getScalar($m, 0));\r
131                 $username       = _getScalar($m, 1);\r
132                 $password       = _getScalar($m, 2);\r
133 \r
134                 $categories = $m->getParam(3);\r
135                 $iSize = $categories->arraysize();              \r
136 \r
137                 $category = '';\r
138                 for ($i=0;$i<$iSize;$i++) {\r
139                         $struct = $categories->arraymem($i);\r
140                         $bPrimary = $struct->structmem('isPrimary');\r
141                         if ($bPrimary)\r
142                                 $bPrimary = $bPrimary->scalarval();\r
143                         else if (!$category)\r
144                                 $bPrimary = 1;  // "Using isPrimary to set the primary category is optional--\r
145                                                                 // in the absence of this flag, the first struct in the array \r
146                                                                 // will be assigned the primary category for the post." (MT doc)\r
147                         if ($bPrimary) {\r
148                                 $category = $struct->structmem('categoryId');\r
149                                 $category = $category->scalarval();\r
150                         }\r
151                         \r
152                 }\r
153                 \r
154                 return _mt_setPostCategories($itemid, $username, $password, $category);\r
155         }\r
156         \r
157         // mt.getRecentPostTitles\r
158         $f_mt_getRecentPostTitles_sig = array(array(\r
159                 // return\r
160                 $xmlrpcArray,           // array of structs\r
161                 // params\r
162                 $xmlrpcString,          // blogid\r
163                 $xmlrpcString,          // userid\r
164                 $xmlrpcString,          // password,\r
165                 $xmlrpcInt                      // number of posts\r
166         ));\r
167         $f_mt_getRecentPostTitles_doc = 'Returns a bandwidth-friendly list of the most recent posts in the system.';\r
168         function f_mt_getRecentPostTitles($m) {\r
169                 $blogid         = intval(_getScalar($m, 0));\r
170                 $username       = _getScalar($m, 1);            \r
171                 $password       = _getScalar($m, 2);\r
172                 $iAmount        = intval(_getScalar($m, 3));\r
173                 \r
174                 return _mt_getRecentPostTitles($blogid, $username, $password, $iAmount);\r
175         }\r
176 \r
177         // mt.getTrackbackPings\r
178         $f_mt_getTrackbackPings_sig = array(array(\r
179                 // return\r
180                 $xmlrpcArray,           // array of structs\r
181                 // params\r
182                 $xmlrpcString           // postid\r
183         ));\r
184         $f_mt_getTrackbackPings_doc = '(this is currently just a placeholder. It returns an empty array.)';\r
185         function f_mt_getTrackbackPings($m) {\r
186                 $itemid = intval(_getScalar($m, 0));\r
187                                 \r
188                 return new xmlrpcresp(new xmlrpcval(array(), 'array'));\r
189         }\r
190         \r
191         $functionDefs = array_merge($functionDefs,\r
192                 array(\r
193                          "mt.supportedMethods" =>\r
194                          array( "function" => "f_mt_supportedMethods",\r
195                                 "signature" => $f_mt_supportedMethods_sig,\r
196                                 "docstring" => $f_mt_supportedMethods_doc),\r
197                 \r
198                          "mt.supportedTextFilters" =>\r
199                          array( "function" => "f_mt_supportedTextFilters",\r
200                                 "signature" => $f_mt_supportedTextFilters_sig,\r
201                                 "docstring" => $f_mt_supportedTextFilters_doc),\r
202                                 \r
203                          "mt.getCategoryList" =>\r
204                          array( "function" => "f_mt_getCategoryList",\r
205                                 "signature" => $f_mt_getCategoryList_sig,\r
206                                 "docstring" => $f_mt_getCategoryList_doc),\r
207                                 \r
208                          "mt.publishPost" =>\r
209                          array( "function" => "f_mt_publishPost",\r
210                                 "signature" => $f_mt_publishPost_sig,\r
211                                 "docstring" => $f_mt_publishPost_doc),\r
212                                 \r
213                          "mt.getPostCategories" =>\r
214                          array( "function" => "f_mt_getPostCategories",\r
215                                 "signature" => $f_mt_getPostCategories_sig,\r
216                                 "docstring" => $f_mt_getPostCategories_doc),\r
217                                 \r
218                          "mt.setPostCategories" =>\r
219                          array( "function" => "f_mt_setPostCategories",\r
220                                 "signature" => $f_mt_setPostCategories_sig,\r
221                                 "docstring" => $f_mt_setPostCategories_doc),\r
222                                 \r
223                          "mt.getRecentPostTitles" =>\r
224                          array( "function" => "f_mt_getRecentPostTitles",\r
225                                 "signature" => $f_mt_getRecentPostTitles_sig,\r
226                                 "docstring" => $f_mt_getRecentPostTitles_doc),\r
227 \r
228                          "mt.getTrackbackPings" =>\r
229                          array( "function" => "f_mt_getTrackbackPings",\r
230                                 "signature" => $f_mt_getTrackbackPings_sig,\r
231                                 "docstring" => $f_mt_getTrackbackPings_doc)\r
232 \r
233                 )\r
234         );\r
235 \r
236         function _mt_setPostCategories($itemid, $username, $password, $category) {\r
237                 global $manager;\r
238 \r
239                 // login\r
240                 $mem = new MEMBER();\r
241                 if (!$mem->login($username, $password))\r
242                         return _error(1,"Could not log in");\r
243 \r
244                 // check if item exists                 \r
245                 if (!$manager->existsItem($itemid,1,1))\r
246                         return _error(6,"No such item ($itemid)");\r
247                         \r
248                 $blogid = getBlogIDFromItemID($itemid);\r
249                 $blog = new BLOG($blogid);\r
250 \r
251                 if (!$mem->canAlterItem($itemid))\r
252                         return _error(7,"Not allowed to alter item");\r
253                 \r
254                 $old =& $manager->getItem($itemid,1,1);         \r
255 \r
256                 $catid = $blog->getCategoryIdFromName($category);               \r
257 \r
258                 $publish = 0;\r
259                 if ($old['draft'] && $publish) {\r
260                         $wasdraft = 1;\r
261                         $publish = 1;\r
262                 } else {\r
263                         $wasdraft = 0;\r
264                 }\r
265                 \r
266                 return _edititem($itemid, $username, $password, $catid, $old['title'], $old['body'], $old['more'], $wasdraft, $publish, $old['closed']);\r
267         }\r
268         \r
269         \r
270         function _mt_getPostCategories($itemid, $username, $password) {\r
271                 global $manager;\r
272 \r
273                 // login\r
274                 $mem = new MEMBER();\r
275                 if (!$mem->login($username, $password))\r
276                         return _error(1,"Could not log in");\r
277 \r
278                 // check if item exists                 \r
279                 if (!$manager->existsItem($itemid,1,1))\r
280                         return _error(6,"No such item ($itemid)");\r
281 \r
282                 $blogid = getBlogIDFromItemID($itemid);\r
283                 $blog = new BLOG($blogid);\r
284 \r
285                 if (!$mem->canAlterItem($itemid))\r
286                         return _error(7, 'You are not allowed to request this information');\r
287                 \r
288                 $info =& $manager->getItem($itemid,1,1);                \r
289                 $catName = $blog->getCategoryName($info['catid']);\r
290                 \r
291                 $struct = new xmlrpcval(\r
292                         array(\r
293                                 'categoryId' => new xmlrpcval($catName, 'string'),\r
294                                 'categoryName' => new xmlrpcval($catName, 'string'),\r
295                                 'isPrimary'     => new xmlrpcval(1, 'boolean')\r
296                         ), 'struct'\r
297                 );              \r
298                 \r
299                 return new xmlrpcresp(new xmlrpcval(array($struct), 'array'));\r
300 \r
301         }\r
302         \r
303         function _mt_publishPost($itemid, $username, $password) {\r
304                 global $manager;\r
305                 \r
306                 if (!$manager->existsItem($itemid,1,1))\r
307                         return _error(6,"No such item ($itemid)");\r
308 \r
309                 // get item data\r
310                 $blogid = getBlogIDFromItemID($itemid);\r
311                 $blog = new BLOG($blogid);\r
312                 $old =& $manager->getItem($itemid,1,1);\r
313 \r
314                 return _edititem($itemid, $username, $password, $old['catid'], $old['title'], $old['body'], $old['more'], $old['draft'], 1, $old['closed']);\r
315         }\r
316         \r
317         \r
318         function _mt_categoryList($blogid, $username, $password) {\r
319                 // 1. login\r
320                 $mem = new MEMBER();\r
321                 if (!$mem->login($username, $password))\r
322                         return _error(1,"Could not log in");\r
323 \r
324                 // check if on team and blog exists\r
325                 if (!BLOG::existsID($blogid))\r
326                         return _error(2,"No such blog ($blogid)");\r
327                 if (!$mem->teamRights($blogid))\r
328                         return _error(3,"Not a team member");\r
329                         \r
330                 $b = new BLOG($blogid);\r
331                 \r
332                 $categorystruct = array();\r
333 \r
334                 $query =  "SELECT cname, cdesc, catid"\r
335                                 . ' FROM '.sql_table('category')\r
336                                 . " WHERE cblog=" . intval($blogid)\r
337                                 . " ORDER BY cname";\r
338                 $r = sql_query($query);\r
339 \r
340                 while ($obj = mysql_fetch_object($r)) {\r
341 \r
342                         $categorystruct[] = new xmlrpcval(\r
343                                 array(\r
344                                         "categoryName" => new xmlrpcval($obj->cname,"string"),\r
345                                         "categoryId" => new xmlrpcval($obj->cname,"string")\r
346                                 )\r
347                         ,'struct');\r
348                         \r
349                 }\r
350                 \r
351                 \r
352                 return new xmlrpcresp(new xmlrpcval( $categorystruct , "array"));\r
353         \r
354         }\r
355         \r
356         function _mt_getRecentPostTitles($blogid, $username, $password, $iAmount) \r
357         {\r
358                 $blogid = intval($blogid);\r
359                 $iAmount = intval($iAmount);\r
360         \r
361                 // 1. login\r
362                 $mem = new MEMBER();\r
363                 if (!$mem->login($username, $password))\r
364                         return _error(1,"Could not log in");\r
365 \r
366                 // 2. check if allowed \r
367                 if (!BLOG::existsID($blogid))\r
368                         return _error(2,"No such blog ($blogid)");\r
369                 if (!$mem->teamRights($blogid))\r
370                         return _error(3,"Not a team member");\r
371                 $iAmount = intval($iAmount);\r
372                 if ($iAmount < 1)\r
373                         return _error(5,"Amount parameter must be positive");\r
374 \r
375                 // 3. create and return list of recent items\r
376                 // Struct returned has dateCreated, userid, postid and title\r
377                 \r
378                 $blog = new BLOG($blogid);\r
379 \r
380                 $structarray = array();         // the array in which the structs will be stored\r
381 \r
382                 $query = "SELECT inumber, ititle as title, itime, iauthor"\r
383                            .' FROM '.sql_table('item')\r
384                            ." WHERE iblog=$blogid"\r
385                            ." ORDER BY itime DESC"\r
386                            ." LIMIT $iAmount";\r
387                 $r = sql_query($query);\r
388                 \r
389                 while ($row = mysql_fetch_assoc($r)) {\r
390                 \r
391                         $newstruct = new xmlrpcval(array(\r
392                                 "dateCreated" => new xmlrpcval(iso8601_encode(strtotime($row['itime'])),"dateTime.iso8601"),\r
393                                 "postid" => new xmlrpcval($row['inumber'],"string"),\r
394                                 "title" => new xmlrpcval($row['title'],"string"),\r
395                                 "userid" => new xmlrpcval($row['iauthor'],"string")\r
396                         ),'struct');\r
397                 \r
398                         array_push($structarray, $newstruct);           \r
399                 }\r
400 \r
401                 return new xmlrpcresp(new xmlrpcval( $structarray , "array"));\r
402         \r
403         }\r
404         \r
405 \r
406 \r
407 ?>\r