OSDN Git Service

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