4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5 * Copyright (C) 2002-2009 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-2009 The Nucleus Group
19 * @version $Id: api_metaweblog.inc.php 1860 2012-05-19 03:47:59Z 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 $data = array('tb_id' => $itemid, 'urls' => & $trackbacks);
79 $manager->notify('SendTrackback', $data);
86 // metaWeblog.getCategories
87 $f_metaWeblog_getCategories_sig = array(array(
89 $xmlrpcStruct, // categories for blog
92 $xmlrpcString, // blogid
93 $xmlrpcString, // username
94 $xmlrpcString, // password
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);
103 return _categoryList($blogid, $username, $password);
107 // metaWeblog.getPost
108 $f_metaWeblog_getPost_sig = array(array(
110 $xmlrpcStruct, // the juice
113 $xmlrpcString, // itemid
114 $xmlrpcString, // username
115 $xmlrpcString, // password
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);
124 return _mw_getPost($itemid, $username, $password);
128 // metaWeblog.editPost
129 $f_metaWeblog_editPost_sig = array(array(
131 $xmlrpcBoolean, // true
134 $xmlrpcString, // itemid
135 $xmlrpcString, // username
136 $xmlrpcString, // password
137 $xmlrpcStruct, // content
138 $xmlrpcBoolean, // publish boolean (set to false to create draft)
141 $f_metaWeblog_editPost_doc = "Edits an item";
142 function f_metaWeblog_editPost($m) {
145 $itemid = _getScalar($m,0);
146 $username = _getScalar($m,1);
147 $password = _getScalar($m,2);
150 $struct = $m->getParam(3);
151 $content = _getStructVal($struct, 'description');
152 $title = _getStructVal($struct, 'title');
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);
160 $publish = _getScalar($m,4);
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);
168 $old =& $manager->getItem($itemid,1,1);
172 // leave category unchanged when not present
173 $catid = $old['catid'];
177 $blog =& $manager->getBlog($blogid);
178 $catid = $blog->getCategoryIdFromName($category);
181 if ($old['draft'] && $publish) {
188 $more = $struct->structmem('mt_text_more');
190 $more = _getStructVal($struct, 'mt_text_more');
192 $more = $old['more'];
195 $comments = $struct->structmem('mt_allow_comments');
197 $comments = (int) _getStructVal($struct, 'mt_allow_comments') ? 0 : 1;
199 $comments = $old['closed'];
202 $res = _edititem($itemid, $username, $password, $catid, $title, $content, $more, $wasdraft, $publish, $comments);
205 $trackbacks = array();
206 $tblist = $struct->structmem('mt_tb_ping_urls');
207 if ($tblist && ($tblist->kindOf() == "array") && ($tblist->arraysize() > 0)) {
209 for ($i = 0; $i < $tblist->arraysize(); $i++) {
210 $trackbacks[] = _getArrayVal($tblist, $i);
213 $data = array('tb_id' => $itemid, 'urls' => & $trackbacks);
214 $manager->notify('SendTrackback', $data);
220 // metaWeblog.newMediaObject
221 $f_metaWeblog_newMediaObject_sig = array(array(
223 $xmlrpcStruct, // "url" element
226 $xmlrpcString, // blogid
227 $xmlrpcString, // username
228 $xmlrpcString, // password
229 $xmlrpcStruct // 'name', 'type' and 'bits'
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);
237 $struct = $m->getParam(3);
238 $name = _getStructVal($struct, 'name');
239 $type = _getStructVal($struct, 'type');
240 $bits = _getStructVal($struct, 'bits');
242 return _newMediaObject($blogid, $username, $password, array('name' => $name, 'type' => $type, 'bits' => $bits));
245 // metaWeblog.getRecentPosts
246 $f_metaWeblog_getRecentPosts_sig = array(array(
248 $xmlrpcStruct, // array of structs
251 $xmlrpcString, // blogid
252 $xmlrpcString, // username
253 $xmlrpcString, // password
254 $xmlrpcInt // number of posts
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));
263 return _getRecentItemsMetaWeblog($blogid, $username, $password, $amount);
266 function _getRecentItemsMetaWeblog($blogid, $username, $password, $amount) {
269 $blogid = intval($blogid);
270 $amount = intval($amount);
274 if (!$mem->login($username, $password))
275 return _error(1,"Could not log in");
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");
286 // 3. create and return list of recent items
287 // Struct returned has dateCreated, userid, blogid and content
289 $blog =& $manager->getBlog($blogid);
291 $structarray = array(); // the array in which the structs will be stored
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"
298 $r = DB::getResult($query);
300 foreach ( $r as $row ) {
302 // remove linebreaks if needed
303 if ($blog->convertBreaks()) {
304 $row['ibody'] = removeBreaks($row['ibody']);
305 $row['imore'] = removeBreaks($row['imore']);
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(
317 new xmlrpcval($row['category'], "string")
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")
328 //TODO: String permaLink?
331 array_push($structarray, $newstruct);
334 return new xmlrpcresp(new xmlrpcval( $structarray , "array"));
337 function _newMediaObject($blogid, $username, $password, $info) {
338 global $CONF, $DIR_MEDIA, $DIR_LIBS, $manager;
342 if (!$mem->login($username, $password))
343 return _error(1,'Could not log in');
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');
351 $b =& $manager->getBlog($blogid);
354 $data = $info['bits']; // decoding was done transparantly by xmlrpclib
357 if (i18n::strlen($data) > $CONF['MaxUploadSize'])
358 return _error(9, 'filesize is too big');
361 // - check if filetype is allowed (check filename)
362 $filename = $info['name'];
364 $allowedtypes = preg_split('#,#', $CONF['AllowedTypes']);
365 foreach ( $allowedtypes as $type )
367 //if (eregi("\." .$type. "$",$filename)) $ok = 1;
368 if (preg_match("#\." .$type. "$#i",$filename)) $ok = 1;
371 _error(8, 'Filetype is not allowed');
373 // - add file to media library
374 //include_once($DIR_LIBS . 'MEDIA.php'); // media classes
375 include_libs('MEDIA.php',true,false);
377 // always use private media library of member
378 $collection = $mem->getID();
380 // prefix filename with current date (YYYY-MM-DD-)
381 // this to avoid nameclashes
382 if ( $CONF['MediaPrefix'] )
384 $filename = i18n::formatted_datetime("%Y%m%d-", time()) . $filename;
387 $res = Media::addMediaObjectRaw($collection, $filename, $data);
389 return _error(10, $res);
392 $urlstruct = new xmlrpcval(array(
393 "url" => new xmlrpcval($CONF['MediaURL'] . $collection. '/' . $filename,'string')
396 return new xmlrpcresp($urlstruct);
399 function _categoryList($blogid, $username, $password) {
404 if (!$mem->login($username, $password))
405 return _error(1,"Could not log in");
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");
413 $b =& $manager->getBlog($blogid);
415 $categorystruct = array();
417 $query = "SELECT cname, cdesc, catid"
418 . ' FROM '.sql_table('category')
419 . " WHERE cblog=" . intval($blogid)
421 $r = DB::getResult($query);
423 foreach ( $r as $row ) {
425 $categorystruct[$row['cname']] = new xmlrpcval(
427 "description" => new xmlrpcval($row['cdesc'],"string"),
428 "htmlUrl" => new xmlrpcval($b->getURL() . "?catid=" . $row['catid'] ,"string"),
429 "rssUrl" => new xmlrpcval("","string")
435 return new xmlrpcresp(new xmlrpcval( $categorystruct , "struct"));
440 function _mw_getPost($itemid, $username, $password) {
445 if (!$mem->login($username, $password))
446 return _error(1,"Could not log in");
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");
455 // 3. return the item
456 $item =& $manager->getItem($itemid,1,1); // (also allow drafts and future items)
458 $b =& $manager->getBlog($blogid);
459 if ($b->convertBreaks()) {
460 $item['body'] = removeBreaks($item['body']);
461 $item['more'] = removeBreaks($item['more']);
464 $categoryname = $b->getCategoryName($item['catid']);
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(
475 new xmlrpcval($categoryname, "string")
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")
484 //TODO: add "String link" to struct?
485 //TODO: add "String permaLink" to struct?
487 return new xmlrpcresp($newstruct);
491 $functionDefs = array_merge($functionDefs,
493 "metaWeblog.newPost" =>
495 "function" => "f_metaWeblog_newPost",
496 "signature" => $f_metaWeblog_newPost_sig,
497 "docstring" => $f_metaWeblog_newPost_doc
500 "metaWeblog.getCategories" =>
502 "function" => "f_metaWeblog_getCategories",
503 "signature" => $f_metaWeblog_getCategories_sig,
504 "docstring" => $f_metaWeblog_getCategories_doc
507 "metaWeblog.getPost" =>
509 "function" => "f_metaWeblog_getPost",
510 "signature" => $f_metaWeblog_getPost_sig,
511 "docstring" => $f_metaWeblog_getPost_doc
514 "metaWeblog.editPost" =>
516 "function" => "f_metaWeblog_editPost",
517 "signature" => $f_metaWeblog_editPost_sig,
518 "docstring" => $f_metaWeblog_editPost_doc
521 'metaWeblog.newMediaObject' =>
523 'function' => 'f_metaWeblog_newMediaObject',
524 'signature' => $f_metaWeblog_newMediaObject_sig,
525 'docstring' => $f_metaWeblog_newMediaObject_doc
528 'metaWeblog.getRecentPosts' =>
530 'function' => 'f_metaWeblog_getRecentPosts',
531 'signature' => $f_metaWeblog_getRecentPosts_sig,
532 'docstring' => $f_metaWeblog_getRecentPosts_doc