OSDN Git Service

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