4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5 * Copyright (C) 2002-2012 The Nucleus Group
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)
15 * This file contains definitions for the methods of the metaWeblog API
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 $
24 $f_metaWeblog_newPost_sig = array(array(
26 $xmlrpcString, // itemid of the new item
29 $xmlrpcString, // blogid
30 $xmlrpcString, // username
31 $xmlrpcString, // password
32 $xmlrpcStruct, // content
33 $xmlrpcBoolean, // publish boolean (set to false to create draft)
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) {
40 $blogid = _getScalar($m,0);
41 $username = _getScalar($m,1);
42 $password = _getScalar($m,2);
43 $struct = $m->getParam(3);
45 $content = _getStructVal($struct, 'description');
46 $more = _getStructVal($struct, 'mt_text_more');
47 $title = _getStructVal($struct, 'title');
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);
54 // mt_allow_comments is optional (thus: be careful)!
55 $c = (int) _getStructVal($struct, 'mt_allow_comments');
63 $publish = _getScalar($m,4);
67 $res = _addItem($blogid, $username, $password, $title, $content, $more, $publish, $comments, $category);
70 $trackbacks = array();
71 $tblist = $struct->structmem('mt_tb_ping_urls');
72 if ($tblist && ($tblist->kindOf() == "array") && ($tblist->arraysize() > 0)) {
74 for ($i = 0; $i < $tblist->arraysize(); $i++) {
75 $trackbacks[] = _getArrayVal($tblist, $i);
78 $manager->notify('SendTrackback', array ('tb_id' => $itemid, 'urls' => & $trackbacks));
85 // metaWeblog.getCategories
86 $f_metaWeblog_getCategories_sig = array(array(
88 $xmlrpcStruct, // categories for blog
91 $xmlrpcString, // blogid
92 $xmlrpcString, // username
93 $xmlrpcString, // password
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);
102 return _categoryList($blogid, $username, $password);
106 // metaWeblog.getPost
107 $f_metaWeblog_getPost_sig = array(array(
109 $xmlrpcStruct, // the juice
112 $xmlrpcString, // itemid
113 $xmlrpcString, // username
114 $xmlrpcString, // password
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);
123 return _mw_getPost($itemid, $username, $password);
127 // metaWeblog.editPost
128 $f_metaWeblog_editPost_sig = array(array(
130 $xmlrpcBoolean, // true
133 $xmlrpcString, // itemid
134 $xmlrpcString, // username
135 $xmlrpcString, // password
136 $xmlrpcStruct, // content
137 $xmlrpcBoolean, // publish boolean (set to false to create draft)
140 $f_metaWeblog_editPost_doc = "Edits an item";
141 function f_metaWeblog_editPost($m) {
144 $itemid = _getScalar($m,0);
145 $username = _getScalar($m,1);
146 $password = _getScalar($m,2);
149 $struct = $m->getParam(3);
150 $content = _getStructVal($struct, 'description');
151 $title = _getStructVal($struct, 'title');
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);
159 $publish = _getScalar($m,4);
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);
167 $old =& $manager->getItem($itemid,1,1);
171 // leave category unchanged when not present
172 $catid = $old['catid'];
176 $blog =& $manager->getBlog($blogid);
177 $catid = $blog->getCategoryIdFromName($category);
180 if ($old['draft'] && $publish) {
187 $more = $struct->structmem('mt_text_more');
189 $more = _getStructVal($struct, 'mt_text_more');
191 $more = $old['more'];
194 $comments = $struct->structmem('mt_allow_comments');
196 $comments = (int) _getStructVal($struct, 'mt_allow_comments') ? 0 : 1;
198 $comments = $old['closed'];
201 $res = _edititem($itemid, $username, $password, $catid, $title, $content, $more, $wasdraft, $publish, $comments);
204 $trackbacks = array();
205 $tblist = $struct->structmem('mt_tb_ping_urls');
206 if ($tblist && ($tblist->kindOf() == "array") && ($tblist->arraysize() > 0)) {
208 for ($i = 0; $i < $tblist->arraysize(); $i++) {
209 $trackbacks[] = _getArrayVal($tblist, $i);
212 $manager->notify('SendTrackback', array ('tb_id' => $itemid, 'urls' => & $trackbacks));
218 // metaWeblog.newMediaObject
219 $f_metaWeblog_newMediaObject_sig = array(array(
221 $xmlrpcStruct, // "url" element
224 $xmlrpcString, // blogid
225 $xmlrpcString, // username
226 $xmlrpcString, // password
227 $xmlrpcStruct // 'name', 'type' and 'bits'
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);
235 $struct = $m->getParam(3);
236 $name = _getStructVal($struct, 'name');
237 $type = _getStructVal($struct, 'type');
238 $bits = _getStructVal($struct, 'bits');
240 return _newMediaObject($blogid, $username, $password, array('name' => $name, 'type' => $type, 'bits' => $bits));
243 // metaWeblog.getRecentPosts
244 $f_metaWeblog_getRecentPosts_sig = array(array(
246 $xmlrpcStruct, // array of structs
249 $xmlrpcString, // blogid
250 $xmlrpcString, // username
251 $xmlrpcString, // password
252 $xmlrpcInt // number of posts
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));
261 return _getRecentItemsMetaWeblog($blogid, $username, $password, $amount);
264 function _getRecentItemsMetaWeblog($blogid, $username, $password, $amount) {
267 $blogid = intval($blogid);
268 $amount = intval($amount);
272 if (!$mem->login($username, $password))
273 return _error(1,"Could not log in");
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");
284 // 3. create and return list of recent items
285 // Struct returned has dateCreated, userid, blogid and content
287 $blog =& $manager->getBlog($blogid);
289 $structarray = array(); // the array in which the structs will be stored
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"
296 $r = DB::getResult($query);
298 foreach ( $r as $row ) {
300 // remove linebreaks if needed
301 if ($blog->convertBreaks()) {
302 $row['ibody'] = removeBreaks($row['ibody']);
303 $row['imore'] = removeBreaks($row['imore']);
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(
315 new xmlrpcval($row['category'], "string")
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")
326 //TODO: String permaLink?
329 array_push($structarray, $newstruct);
332 return new xmlrpcresp(new xmlrpcval( $structarray , "array"));
335 function _newMediaObject($blogid, $username, $password, $info) {
336 global $CONF, $DIR_MEDIA, $DIR_LIBS, $manager;
340 if (!$mem->login($username, $password))
341 return _error(1,'Could not log in');
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');
349 $b =& $manager->getBlog($blogid);
352 $data = $info['bits']; // decoding was done transparantly by xmlrpclib
355 if (i18n::strlen($data) > $CONF['MaxUploadSize'])
356 return _error(9, 'filesize is too big');
359 // - check if filetype is allowed (check filename)
360 $filename = $info['name'];
362 $allowedtypes = preg_split('#,#', $CONF['AllowedTypes']);
363 foreach ( $allowedtypes as $type )
365 //if (eregi("\." .$type. "$",$filename)) $ok = 1;
366 if (preg_match("#\." .$type. "$#i",$filename)) $ok = 1;
369 _error(8, 'Filetype is not allowed');
371 // - add file to media library
372 //include_once($DIR_LIBS . 'MEDIA.php'); // media classes
373 include_libs('MEDIA.php',true,false);
375 // always use private media library of member
376 $collection = $mem->getID();
378 // prefix filename with current date (YYYY-MM-DD-)
379 // this to avoid nameclashes
380 if ( $CONF['MediaPrefix'] )
382 $filename = i18n::formatted_datetime("%Y%m%d-", time()) . $filename;
385 $res = Media::addMediaObjectRaw($collection, $filename, $data);
387 return _error(10, $res);
390 $urlstruct = new xmlrpcval(array(
391 "url" => new xmlrpcval($CONF['MediaURL'] . $collection. '/' . $filename,'string')
394 return new xmlrpcresp($urlstruct);
397 function _categoryList($blogid, $username, $password) {
402 if (!$mem->login($username, $password))
403 return _error(1,"Could not log in");
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");
411 $b =& $manager->getBlog($blogid);
413 $categorystruct = array();
415 $query = "SELECT cname, cdesc, catid"
416 . ' FROM '.sql_table('category')
417 . " WHERE cblog=" . intval($blogid)
419 $r = DB::getResult($query);
421 foreach ( $r as $row ) {
423 $categorystruct[$row['cname']] = new xmlrpcval(
425 "description" => new xmlrpcval($row['cdesc'],"string"),
426 "htmlUrl" => new xmlrpcval($b->getURL() . "?catid=" . $row['catid'] ,"string"),
427 "rssUrl" => new xmlrpcval("","string")
433 return new xmlrpcresp(new xmlrpcval( $categorystruct , "struct"));
438 function _mw_getPost($itemid, $username, $password) {
443 if (!$mem->login($username, $password))
444 return _error(1,"Could not log in");
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");
453 // 3. return the item
454 $item =& $manager->getItem($itemid,1,1); // (also allow drafts and future items)
456 $b =& $manager->getBlog($blogid);
457 if ($b->convertBreaks()) {
458 $item['body'] = removeBreaks($item['body']);
459 $item['more'] = removeBreaks($item['more']);
462 $categoryname = $b->getCategoryName($item['catid']);
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(
473 new xmlrpcval($categoryname, "string")
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")
482 //TODO: add "String link" to struct?
483 //TODO: add "String permaLink" to struct?
485 return new xmlrpcresp($newstruct);
489 $functionDefs = array_merge($functionDefs,
491 "metaWeblog.newPost" =>
493 "function" => "f_metaWeblog_newPost",
494 "signature" => $f_metaWeblog_newPost_sig,
495 "docstring" => $f_metaWeblog_newPost_doc
498 "metaWeblog.getCategories" =>
500 "function" => "f_metaWeblog_getCategories",
501 "signature" => $f_metaWeblog_getCategories_sig,
502 "docstring" => $f_metaWeblog_getCategories_doc
505 "metaWeblog.getPost" =>
507 "function" => "f_metaWeblog_getPost",
508 "signature" => $f_metaWeblog_getPost_sig,
509 "docstring" => $f_metaWeblog_getPost_doc
512 "metaWeblog.editPost" =>
514 "function" => "f_metaWeblog_editPost",
515 "signature" => $f_metaWeblog_editPost_sig,
516 "docstring" => $f_metaWeblog_editPost_doc
519 'metaWeblog.newMediaObject' =>
521 'function' => 'f_metaWeblog_newMediaObject',
522 'signature' => $f_metaWeblog_newMediaObject_sig,
523 'docstring' => $f_metaWeblog_newMediaObject_doc
526 'metaWeblog.getRecentPosts' =>
528 'function' => 'f_metaWeblog_getRecentPosts',
529 'signature' => $f_metaWeblog_getRecentPosts_sig,
530 'docstring' => $f_metaWeblog_getRecentPosts_doc