OSDN Git Service

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