OSDN Git Service

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