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 = new Blog($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) {
266 $blogid = intval($blogid);
267 $amount = intval($amount);
271 if (!$mem->login($username, $password))
272 return _error(1,"Could not log in");
274 // 2. check if allowed
275 if (!Blog::existsID($blogid))
276 return _error(2,"No such blog ($blogid)");
277 if (!$mem->teamRights($blogid))
278 return _error(3,"Not a team member");
279 $amount = intval($amount);
280 if (($amount < 1) or ($amount > 20))
281 return _error(5,"Amount parameter must be in range 1..20");
283 // 3. create and return list of recent items
284 // Struct returned has dateCreated, userid, blogid and content
286 $blog = new Blog($blogid);
288 $structarray = array(); // the array in which the structs will be stored
290 $query = "SELECT mname, ibody, imore, iauthor, ibody, inumber, ititle as title, itime, cname as category, iclosed"
291 .' FROM '.sql_table('item').', '.sql_table('category').', '.sql_table('member')
292 ." WHERE iblog=$blogid and icat=catid and iauthor=mnumber"
293 ." ORDER BY itime DESC"
295 $r = sql_query($query);
297 while ($row = sql_fetch_assoc($r)) {
299 // remove linebreaks if needed
300 if ($blog->convertBreaks()) {
301 $row['ibody'] = removeBreaks($row['ibody']);
302 $row['imore'] = removeBreaks($row['imore']);
305 $newstruct = new xmlrpcval(array(
306 "dateCreated" => new xmlrpcval(iso8601_encode(strtotime($row['itime'])),"dateTime.iso8601"),
307 "userid" => new xmlrpcval($row['iauthor'],"string"),
308 "blogid" => new xmlrpcval($blogid,"string"),
309 "postid" => new xmlrpcval($row['inumber'],"string"),
310 "description" => new xmlrpcval($row['ibody'],"string"),
311 "title" => new xmlrpcval($row['title'],"string"),
312 "categories" => new xmlrpcval(
314 new xmlrpcval($row['category'], "string")
319 "mt_text_more" => new xmlrpcval($row['imore'], "string"),
320 "mt_allow_comments" => new xmlrpcval($row['iclosed'] ? 0 : 1, "int"),
321 "mt_allow_pings" => new xmlrpcval(1, "int")
325 //TODO: String permaLink?
328 array_push($structarray, $newstruct);
331 return new xmlrpcresp(new xmlrpcval( $structarray , "array"));
334 function _newMediaObject($blogid, $username, $password, $info) {
335 global $CONF, $DIR_MEDIA, $DIR_LIBS;
339 if (!$mem->login($username, $password))
340 return _error(1,'Could not log in');
342 // - check if team member
343 if (!Blog::existsID($blogid))
344 return _error(2,"No such blog ($blogid)");
345 if (!$mem->teamRights($blogid))
346 return _error(3,'Not a team member');
348 $b = new Blog($blogid);
351 $data = $info['bits']; // decoding was done transparantly by xmlrpclib
354 if (i18n::strlen($data) > $CONF['MaxUploadSize'])
355 return _error(9, 'filesize is too big');
358 // - check if filetype is allowed (check filename)
359 $filename = $info['name'];
361 $allowedtypes = preg_split('#,#', $CONF['AllowedTypes']);
362 foreach ( $allowedtypes as $type )
364 //if (eregi("\." .$type. "$",$filename)) $ok = 1;
365 if (preg_match("#\." .$type. "$#i",$filename)) $ok = 1;
368 _error(8, 'Filetype is not allowed');
370 // - add file to media library
371 //include_once($DIR_LIBS . 'MEDIA.php'); // media classes
372 include_libs('MEDIA.php',true,false);
374 // always use private media library of member
375 $collection = $mem->getID();
377 // prefix filename with current date (YYYY-MM-DD-)
378 // this to avoid nameclashes
379 if ( $CONF['MediaPrefix'] )
381 $filename = i18n::formatted_timedate("%Y%m%d-", time()) . $filename;
384 $res = Media::addMediaObjectRaw($collection, $filename, $data);
386 return _error(10, $res);
389 $urlstruct = new xmlrpcval(array(
390 "url" => new xmlrpcval($CONF['MediaURL'] . $collection. '/' . $filename,'string')
393 return new xmlrpcresp($urlstruct);
396 function _categoryList($blogid, $username, $password) {
399 if (!$mem->login($username, $password))
400 return _error(1,"Could not log in");
402 // check if on team and blog exists
403 if (!Blog::existsID($blogid))
404 return _error(2,"No such blog ($blogid)");
405 if (!$mem->teamRights($blogid))
406 return _error(3,"Not a team member");
408 $b = new Blog($blogid);
410 $categorystruct = array();
412 $query = "SELECT cname, cdesc, catid"
413 . ' FROM '.sql_table('category')
414 . " WHERE cblog=" . intval($blogid)
416 $r = sql_query($query);
418 while ($obj = sql_fetch_object($r)) {
420 $categorystruct[$obj->cname] = new xmlrpcval(
422 "description" => new xmlrpcval($obj->cdesc,"string"),
423 "htmlUrl" => new xmlrpcval($b->getURL() . "?catid=" . $obj->catid ,"string"),
424 "rssUrl" => new xmlrpcval("","string")
430 return new xmlrpcresp(new xmlrpcval( $categorystruct , "struct"));
435 function _mw_getPost($itemid, $username, $password) {
440 if (!$mem->login($username, $password))
441 return _error(1,"Could not log in");
443 // 2. check if allowed
444 if (!$manager->existsItem($itemid,1,1))
445 return _error(6,"No such item ($itemid)");
446 $blogid = getBlogIDFromItemID($itemid);
447 if (!$mem->teamRights($blogid))
448 return _error(3,"Not a team member");
450 // 3. return the item
451 $item =& $manager->getItem($itemid,1,1); // (also allow drafts and future items)
453 $b = new Blog($blogid);
454 if ($b->convertBreaks()) {
455 $item['body'] = removeBreaks($item['body']);
456 $item['more'] = removeBreaks($item['more']);
459 $categoryname = $b->getCategoryName($item['catid']);
461 $newstruct = new xmlrpcval(array(
462 "dateCreated" => new xmlrpcval(iso8601_encode($item['timestamp']),"dateTime.iso8601"),
463 "userid" => new xmlrpcval($item['authorid'],"string"),
464 "blogid" => new xmlrpcval($blogid,"string"),
465 "postid" => new xmlrpcval($itemid, "string"),
466 "description" => new xmlrpcval($item['body'],"string"),
467 "title" => new xmlrpcval($item['title'],"string"),
468 "categories" => new xmlrpcval(
470 new xmlrpcval($categoryname, "string")
474 "mt_text_more" => new xmlrpcval($item['more'], "string"),
475 "mt_allow_comments" => new xmlrpcval($item['closed'] ? 0 : 1, "int"),
476 "mt_allow_pings" => new xmlrpcval(1, "int")
479 //TODO: add "String link" to struct?
480 //TODO: add "String permaLink" to struct?
482 return new xmlrpcresp($newstruct);
486 $functionDefs = array_merge($functionDefs,
488 "metaWeblog.newPost" =>
490 "function" => "f_metaWeblog_newPost",
491 "signature" => $f_metaWeblog_newPost_sig,
492 "docstring" => $f_metaWeblog_newPost_doc
495 "metaWeblog.getCategories" =>
497 "function" => "f_metaWeblog_getCategories",
498 "signature" => $f_metaWeblog_getCategories_sig,
499 "docstring" => $f_metaWeblog_getCategories_doc
502 "metaWeblog.getPost" =>
504 "function" => "f_metaWeblog_getPost",
505 "signature" => $f_metaWeblog_getPost_sig,
506 "docstring" => $f_metaWeblog_getPost_doc
509 "metaWeblog.editPost" =>
511 "function" => "f_metaWeblog_editPost",
512 "signature" => $f_metaWeblog_editPost_sig,
513 "docstring" => $f_metaWeblog_editPost_doc
516 'metaWeblog.newMediaObject' =>
518 'function' => 'f_metaWeblog_newMediaObject',
519 'signature' => $f_metaWeblog_newMediaObject_sig,
520 'docstring' => $f_metaWeblog_newMediaObject_doc
523 'metaWeblog.getRecentPosts' =>
525 'function' => 'f_metaWeblog_getRecentPosts',
526 'signature' => $f_metaWeblog_getRecentPosts_sig,
527 'docstring' => $f_metaWeblog_getRecentPosts_doc