OSDN Git Service

FIX:変数名の誤記を修正
[nucleus-jp/nucleus-next.git] / nucleus / xmlrpc / api_metaweblog.inc.php
1 <?php
2
3 /*
4  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5  * Copyright (C) 2002-2012 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 /**
15  *      This file contains definitions for the methods of the metaWeblog API
16  *
17  * @license http://nucleuscms.org/license.txt GNU General Public License
18  * @copyright Copyright (C) 2002-2012 The Nucleus Group
19  * @version $Id: api_metaweblog.inc.php 1534 2011-06-22 06:13:23Z sakamocchi $
20  */
21
22
23         // metaWeblog.newPost
24         $f_metaWeblog_newPost_sig = array(array(
25                         // return type
26                         $xmlrpcString,  // itemid of the new item
27
28                         // params:
29                         $xmlrpcString,  // blogid
30                         $xmlrpcString,  // username
31                         $xmlrpcString,  // password
32                         $xmlrpcStruct,  // content
33                         $xmlrpcBoolean, // publish boolean (set to false to create draft)
34
35                 ));
36         $f_metaWeblog_newPost_doc = "Adds a new item to the given blog. Adds it as a draft when publish is false";
37         function f_metaWeblog_newPost($m) {
38                 global $manager;
39                 
40                 $blogid =                       _getScalar($m,0);
41                 $username =             _getScalar($m,1);
42                 $password =             _getScalar($m,2);
43                 $struct =                       $m->getParam(3);
44                 
45                 $content =              _getStructVal($struct, 'description');
46                 $more =                 _getStructVal($struct, 'mt_text_more');
47                 $title =                _getStructVal($struct, 'title');
48
49                 // category is optional (thus: be careful)!
50                 $catlist = $struct->structmem('categories');
51                 if ($catlist && ($catlist->kindOf() == "array") && ($catlist->arraysize() > 0))
52                         $category = _getArrayVal($catlist, 0);
53                 
54                 //  mt_allow_comments is optional (thus: be careful)!
55                 $c = (int) _getStructVal($struct, 'mt_allow_comments');
56                 if ($c==''){
57                         $comments =     0;
58                 }
59                 else {
60                         $comments = $c;
61                 }
62                 
63                 $publish = _getScalar($m,4);
64
65
66                 // Add item
67                 $res = _addItem($blogid, $username, $password, $title, $content, $more, $publish, $comments, $category);
68                 
69                 // Handle trackbacks
70                 $trackbacks = array();
71                 $tblist = $struct->structmem('mt_tb_ping_urls');
72                 if ($tblist && ($tblist->kindOf() == "array") && ($tblist->arraysize() > 0)) {
73                         
74                         for ($i = 0; $i < $tblist->arraysize(); $i++) {
75                                 $trackbacks[] = _getArrayVal($tblist, $i);
76                         }
77                         
78                         $manager->notify('SendTrackback', array ('tb_id' => $itemid, 'urls' => & $trackbacks));
79                 }
80
81                 return $res;
82         }
83
84
85         // metaWeblog.getCategories
86         $f_metaWeblog_getCategories_sig = array(array(
87                 // return
88                 $xmlrpcStruct,  // categories for blog
89
90                 // params
91                 $xmlrpcString,  // blogid
92                 $xmlrpcString,  // username
93                 $xmlrpcString,  // password
94
95         ));
96         $f_metaWeblog_getCategories_doc = "Returns the categories for a given blog";
97         function f_metaWeblog_getCategories($m) {
98                 $blogid =       _getScalar($m,0);
99                 $username =     _getScalar($m,1);
100                 $password =     _getScalar($m,2);
101
102                 return _categoryList($blogid, $username, $password);
103         }
104
105
106         // metaWeblog.getPost
107         $f_metaWeblog_getPost_sig = array(array(
108                 // return
109                 $xmlrpcStruct,  // the juice
110
111                 // params
112                 $xmlrpcString,  // itemid
113                 $xmlrpcString,  // username
114                 $xmlrpcString,  // password
115
116         ));
117         $f_metaWeblog_getPost_doc = "Retrieves a post";
118         function f_metaWeblog_getPost($m) {
119                 $itemid =       _getScalar($m,0);
120                 $username =     _getScalar($m,1);
121                 $password =     _getScalar($m,2);
122
123                 return _mw_getPost($itemid, $username, $password);
124         }
125
126
127         // metaWeblog.editPost
128         $f_metaWeblog_editPost_sig = array(array(
129                         // return type
130                         $xmlrpcBoolean, // true
131
132                         // params:
133                         $xmlrpcString,  // itemid
134                         $xmlrpcString,  // username
135                         $xmlrpcString,  // password
136                         $xmlrpcStruct,  // content
137                         $xmlrpcBoolean, // publish boolean (set to false to create draft)
138
139                 ));
140         $f_metaWeblog_editPost_doc = "Edits an item";
141         function f_metaWeblog_editPost($m) {
142                 global $manager;
143
144                 $itemid =                       _getScalar($m,0);
145                 $username =             _getScalar($m,1);
146                 $password =             _getScalar($m,2);
147
148                 $category = '';
149                 $struct =                       $m->getParam(3);
150                         $content =              _getStructVal($struct, 'description');
151                         $title =                _getStructVal($struct, 'title');
152
153                         // category is optional (thus: be careful)!
154                         $catlist = $struct->structmem('categories');
155                         if ($catlist && ($catlist->kindOf() == "array") && ($catlist->arraysize() > 0)) {
156                                 $category = _getArrayVal($catlist, 0);
157                         }
158
159                 $publish = _getScalar($m,4);
160
161                 
162                 // get old title and extended part
163                 if (!$manager->existsItem($itemid,1,1))
164                         return _error(6,"No such item ($itemid)");
165                 $blogid = getBlogIDFromItemID($itemid);
166
167                 $old =& $manager->getItem($itemid,1,1);
168
169                 if ($category == '')
170                 {
171                         // leave category unchanged when not present
172                         $catid = $old['catid'];
173                 }
174                 else
175                 {
176                         $blog =& $manager->getBlog($blogid);
177                         $catid = $blog->getCategoryIdFromName($category);
178                 }
179
180                 if ($old['draft'] && $publish) {
181                         $wasdraft = 1;
182                         $publish = 1;
183                 } else {
184                         $wasdraft = 0;
185                 }
186
187                 $more = $struct->structmem('mt_text_more');
188                 if ($more) {
189                         $more = _getStructVal($struct, 'mt_text_more');
190                 } else {
191                         $more = $old['more'];
192                 }
193                 
194                 $comments = $struct->structmem('mt_allow_comments');
195                 if ($comments) {
196                         $comments = (int) _getStructVal($struct, 'mt_allow_comments') ? 0 : 1;
197                 } else {
198                         $comments = $old['closed'];
199                 }
200
201                 $res = _edititem($itemid, $username, $password, $catid, $title, $content, $more, $wasdraft, $publish, $comments);
202
203                 // Handle trackbacks
204                 $trackbacks = array();
205                 $tblist = $struct->structmem('mt_tb_ping_urls');
206                 if ($tblist && ($tblist->kindOf() == "array") && ($tblist->arraysize() > 0)) {
207                         
208                         for ($i = 0; $i < $tblist->arraysize(); $i++) {
209                                 $trackbacks[] = _getArrayVal($tblist, $i);
210                         }
211                         
212                         $manager->notify('SendTrackback', array ('tb_id' => $itemid, 'urls' => & $trackbacks));
213                 }
214
215                 return $res;
216         }
217
218         // metaWeblog.newMediaObject
219         $f_metaWeblog_newMediaObject_sig = array(array(
220                 //  return type
221                 $xmlrpcStruct,          // "url" element
222
223                 // params
224                 $xmlrpcString,          // blogid
225                 $xmlrpcString,          // username
226                 $xmlrpcString,          // password
227                 $xmlrpcStruct           // 'name', 'type' and 'bits'
228         ));
229         $f_metaWeblog_newMediaObject_doc = 'Uploads a file to to the media library of the user';
230         function f_metaWeblog_newMediaObject($m) {
231                 $blogid         = _getScalar($m, 0);
232                 $username       = _getScalar($m, 1);
233                 $password       = _getScalar($m, 2);
234
235                 $struct         = $m->getParam(3);
236                         $name   = _getStructVal($struct, 'name');
237                         $type   = _getStructVal($struct, 'type');
238                         $bits   = _getStructVal($struct, 'bits');
239
240                 return _newMediaObject($blogid, $username, $password, array('name' => $name, 'type' => $type, 'bits' => $bits));
241         }
242
243         // metaWeblog.getRecentPosts
244         $f_metaWeblog_getRecentPosts_sig = array(array(
245                 // return type
246                 $xmlrpcStruct,          // array of structs
247
248                 // params
249                 $xmlrpcString,          // blogid
250                 $xmlrpcString,          // username
251                 $xmlrpcString,          // password
252                 $xmlrpcInt                      // number of posts
253         ));
254         $f_metaWeblog_getRecentPosts_doc = 'Returns recent weblog items.';
255         function f_metaWeblog_getRecentPosts($m) {
256                 $blogid         = _getScalar($m, 0);
257                 $username       = _getScalar($m, 1);
258                 $password       = _getScalar($m, 2);
259                 $amount         = intval(_getScalar($m, 3));
260
261                 return _getRecentItemsMetaWeblog($blogid, $username, $password, $amount);
262         }
263
264         function _getRecentItemsMetaWeblog($blogid, $username, $password, $amount) {
265                 global $manager;
266                 
267                 $blogid = intval($blogid);
268                 $amount = intval($amount);
269
270                 // 1. login
271                 $mem = new Member();
272                 if (!$mem->login($username, $password))
273                         return _error(1,"Could not log in");
274
275                 // 2. check if allowed
276                 if (!Blog::existsID($blogid))
277                         return _error(2,"No such blog ($blogid)");
278                 if (!$mem->teamRights($blogid))
279                         return _error(3,"Not a team member");
280                 $amount = intval($amount);
281                 if (($amount < 1) or ($amount > 20))
282                         return _error(5,"Amount parameter must be in range 1..20");
283
284                 // 3. create and return list of recent items
285                 // Struct returned has dateCreated, userid, blogid and content
286
287                 $blog =& $manager->getBlog($blogid);
288
289                 $structarray = array();         // the array in which the structs will be stored
290
291                 $query = "SELECT mname, ibody, imore, iauthor, ibody, inumber, ititle as title, itime, cname as category, iclosed"
292                            .' FROM '.sql_table('item').', '.sql_table('category').', '.sql_table('member')
293                            ." WHERE iblog=$blogid and icat=catid and iauthor=mnumber"
294                            ." ORDER BY itime DESC"
295                            ." LIMIT $amount";
296                 $r = DB::getResult($query);
297
298                 foreach ( $r as $row ) {
299
300                         // remove linebreaks if needed
301                         if ($blog->convertBreaks()) {
302                                 $row['ibody'] = removeBreaks($row['ibody']);
303                                 $row['imore'] = removeBreaks($row['imore']);
304                         }
305
306                         $newstruct = new xmlrpcval(array(
307                                 "dateCreated" => new xmlrpcval(iso8601_encode(strtotime($row['itime'])),"dateTime.iso8601"),
308                                 "userid" => new xmlrpcval($row['iauthor'],"string"),
309                                 "blogid" => new xmlrpcval($blogid,"string"),
310                                 "postid" => new xmlrpcval($row['inumber'],"string"),
311                                 "description" => new xmlrpcval($row['ibody'],"string"),
312                                 "title" => new xmlrpcval($row['title'],"string"),
313                                 "categories" => new xmlrpcval(
314                                                 array(
315                                                         new xmlrpcval($row['category'], "string")
316                                                 )
317                                                 ,"array"),
318                                 
319                                                 
320                                 "mt_text_more"          => new xmlrpcval($row['imore'], "string"),
321                                 "mt_allow_comments" => new xmlrpcval($row['iclosed'] ? 0 : 1, "int"),
322                                 "mt_allow_pings"        => new xmlrpcval(1, "int")
323                         ),'struct');
324
325                 //TODO: String link?
326                 //TODO: String permaLink?
327
328
329                         array_push($structarray, $newstruct);
330                 }
331
332                 return new xmlrpcresp(new xmlrpcval( $structarray , "array"));
333         }
334
335         function _newMediaObject($blogid, $username, $password, $info) {
336                 global $CONF, $DIR_MEDIA, $DIR_LIBS, $manager;
337
338                 // - login
339                 $mem = new Member();
340                 if (!$mem->login($username, $password))
341                         return _error(1,'Could not log in');
342
343                 // - check if team member
344                 if (!Blog::existsID($blogid))
345                         return _error(2,"No such blog ($blogid)");
346                 if (!$mem->teamRights($blogid))
347                         return _error(3,'Not a team member');
348
349                 $b =& $manager->getBlog($blogid);
350
351                 // - decode data
352                 $data = $info['bits']; // decoding was done transparantly by xmlrpclib
353
354                 // - check filesize
355                 if (i18n::strlen($data) > $CONF['MaxUploadSize'])
356                         return _error(9, 'filesize is too big');
357
358
359                 // - check if filetype is allowed (check filename)
360                 $filename = $info['name'];
361                 $ok = 0;
362                 $allowedtypes = preg_split('#,#', $CONF['AllowedTypes']);
363                 foreach ( $allowedtypes as $type )
364                 {
365                         //if (eregi("\." .$type. "$",$filename)) $ok = 1;
366                         if (preg_match("#\." .$type. "$#i",$filename)) $ok = 1;
367                 }
368                 if (!$ok)
369                         _error(8, 'Filetype is not allowed');
370
371                 // - add file to media library
372                 //include_once($DIR_LIBS . 'MEDIA.php');        // media classes
373                 include_libs('MEDIA.php',true,false);
374
375                 // always use private media library of member
376                 $collection = $mem->getID();
377
378                 // prefix filename with current date (YYYY-MM-DD-)
379                 // this to avoid nameclashes
380                 if ( $CONF['MediaPrefix'] )
381                 {
382                         $filename = i18n::formatted_datetime("%Y%m%d-", time()) . $filename;
383                 }
384                 
385                 $res = Media::addMediaObjectRaw($collection, $filename, $data);
386                 if ($res)
387                         return _error(10, $res);
388
389                 // - return URL
390                 $urlstruct = new xmlrpcval(array(
391                         "url" => new xmlrpcval($CONF['MediaURL'] . $collection. '/' . $filename,'string')
392                 ),'struct');
393
394                 return new xmlrpcresp($urlstruct);
395         }
396
397         function _categoryList($blogid, $username, $password) {
398                 global $manager;
399                 
400                 // 1. login
401                 $mem = new Member();
402                 if (!$mem->login($username, $password))
403                         return _error(1,"Could not log in");
404
405                 // check if on team and blog exists
406                 if (!Blog::existsID($blogid))
407                         return _error(2,"No such blog ($blogid)");
408                 if (!$mem->teamRights($blogid))
409                         return _error(3,"Not a team member");
410
411                 $b =& $manager->getBlog($blogid);
412
413                 $categorystruct = array();
414
415                 $query =  "SELECT cname, cdesc, catid"
416                                 . ' FROM '.sql_table('category')
417                                 . " WHERE cblog=" . intval($blogid)
418                                 . " ORDER BY cname";
419                 $r = DB::getResult($query);
420
421                 foreach ( $r as $row ) {
422
423                         $categorystruct[$row['cname']] = new xmlrpcval(
424                                 array(
425                                         "description" => new xmlrpcval($row['cdesc'],"string"),
426                                         "htmlUrl" => new xmlrpcval($b->getURL() . "?catid=" . $row['catid'] ,"string"),
427                                         "rssUrl" => new xmlrpcval("","string")
428                                 )
429                         ,'struct');
430                 }
431
432
433                 return new xmlrpcresp(new xmlrpcval( $categorystruct , "struct"));
434
435         }
436
437
438         function _mw_getPost($itemid, $username, $password) {
439                 global $manager;
440
441                 // 1. login
442                 $mem = new Member();
443                 if (!$mem->login($username, $password))
444                         return _error(1,"Could not log in");
445
446                 // 2. check if allowed
447                 if (!$manager->existsItem($itemid,1,1))
448                         return _error(6,"No such item ($itemid)");
449                 $blogid = getBlogIDFromItemID($itemid);
450                 if (!$mem->teamRights($blogid))
451                         return _error(3,"Not a team member");
452
453                 // 3. return the item
454                 $item =& $manager->getItem($itemid,1,1); // (also allow drafts and future items)
455
456                 $b =& $manager->getBlog($blogid);
457                 if ($b->convertBreaks()) {
458                         $item['body'] = removeBreaks($item['body']);
459                         $item['more'] = removeBreaks($item['more']);
460                 }
461
462                 $categoryname = $b->getCategoryName($item['catid']);
463
464                 $newstruct = new xmlrpcval(array(
465                         "dateCreated" => new xmlrpcval(iso8601_encode($item['timestamp']),"dateTime.iso8601"),
466                         "userid" => new xmlrpcval($item['authorid'],"string"),
467                         "blogid" => new xmlrpcval($blogid,"string"),
468                         "postid" => new xmlrpcval($itemid, "string"),
469                         "description" => new xmlrpcval($item['body'],"string"),
470                         "title" => new xmlrpcval($item['title'],"string"),
471                         "categories" => new xmlrpcval(
472                                         array(
473                                                 new xmlrpcval($categoryname, "string")
474                                         )
475                                         ,"array"),
476                                 
477                         "mt_text_more"          => new xmlrpcval($item['more'], "string"),
478                         "mt_allow_comments" => new xmlrpcval($item['closed'] ? 0 : 1, "int"),
479                         "mt_allow_pings"        => new xmlrpcval(1, "int")
480                 ),'struct');
481
482                 //TODO: add "String link" to struct?
483                 //TODO: add "String permaLink" to struct?
484
485                 return new xmlrpcresp($newstruct);
486
487         }
488
489         $functionDefs = array_merge($functionDefs,
490                 array(
491                          "metaWeblog.newPost" =>
492                          array(
493                                 "function" => "f_metaWeblog_newPost",
494                                 "signature" => $f_metaWeblog_newPost_sig,
495                                 "docstring" => $f_metaWeblog_newPost_doc
496                          ),
497
498                          "metaWeblog.getCategories" =>
499                          array(
500                                 "function" => "f_metaWeblog_getCategories",
501                                 "signature" => $f_metaWeblog_getCategories_sig,
502                                 "docstring" => $f_metaWeblog_getCategories_doc
503                          ),
504
505                          "metaWeblog.getPost" =>
506                          array(
507                                 "function" => "f_metaWeblog_getPost",
508                                 "signature" => $f_metaWeblog_getPost_sig,
509                                 "docstring" => $f_metaWeblog_getPost_doc
510                          ),
511
512                          "metaWeblog.editPost" =>
513                          array(
514                                 "function" => "f_metaWeblog_editPost",
515                                 "signature" => $f_metaWeblog_editPost_sig,
516                                 "docstring" => $f_metaWeblog_editPost_doc
517                          ),
518
519                          'metaWeblog.newMediaObject' =>
520                          array(
521                                 'function' => 'f_metaWeblog_newMediaObject',
522                                 'signature' => $f_metaWeblog_newMediaObject_sig,
523                                 'docstring' => $f_metaWeblog_newMediaObject_doc
524                          ),
525
526                          'metaWeblog.getRecentPosts' =>
527                          array(
528                                 'function' => 'f_metaWeblog_getRecentPosts',
529                                 'signature' => $f_metaWeblog_getRecentPosts_sig,
530                                 'docstring' => $f_metaWeblog_getRecentPosts_doc
531                          )
532
533                 )
534         );
535 ?>