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)
13 * This file contains definitions for the methods of the metaWeblog API
18 $f_metaWeblog_newPost_sig = array(array(
20 $xmlrpcString, // itemid of the new item
23 $xmlrpcString, // blogid
24 $xmlrpcString, // username
25 $xmlrpcString, // password
26 $xmlrpcStruct, // content
27 $xmlrpcBoolean, // publish boolean (set to false to create draft)
30 $f_metaWeblog_newPost_doc = "Adds a new item to the given blog. Adds it as a draft when publish is false";
31 function f_metaWeblog_newPost($m) {
34 $blogid = _getScalar($m,0);
35 $username = _getScalar($m,1);
36 $password = _getScalar($m,2);
37 $struct = $m->getParam(3);
39 $content = _getStructVal($struct, 'description');
40 $more = _getStructVal($struct, 'mt_text_more');
41 $title = _getStructVal($struct, 'title');
43 // category is optional (thus: be careful)!
44 $catlist = $struct->structmem('categories');
45 if ($catlist && ($catlist->kindOf() == "array") && ($catlist->arraysize() > 0))
46 $category = _getArrayVal($catlist, 0);
49 // $comments = (int) _getStructVal($struct, 'mt_allow_comments') ? 0 : 1;
50 $comments = $struct->structmem('mt_allow_comments');
52 $closed = (intval(_getStructVal($struct, 'mt_allow_comments')) == 1) ? 0 : 1;
56 $publish = _getScalar($m,4);
60 // $res = _addItem($blogid, $username, $password, $title, $content, $more, $publish, $comments, $category);
61 $res = _addItem($blogid, $username, $password, $title, $content, $more, $publish, $closed, $category);
64 $trackbacks = array();
65 $tblist = $struct->structmem('mt_tb_ping_urls');
66 if ($tblist && ($tblist->kindOf() == "array") && ($tblist->arraysize() > 0)) {
68 for ($i = 0; $i < $tblist->arraysize(); $i++) {
69 $trackbacks[] = _getArrayVal($tblist, $i);
74 'urls' => &$trackbacks
76 $manager->notify('SendTrackback', $param);
83 // metaWeblog.getCategories
84 $f_metaWeblog_getCategories_sig = array(array(
86 $xmlrpcStruct, // categories for blog
89 $xmlrpcString, // blogid
90 $xmlrpcString, // username
91 $xmlrpcString, // password
94 $f_metaWeblog_getCategories_doc = "Returns the categories for a given blog";
95 function f_metaWeblog_getCategories($m) {
96 $blogid = _getScalar($m,0);
97 $username = _getScalar($m,1);
98 $password = _getScalar($m,2);
100 return _categoryList($blogid, $username, $password);
104 // metaWeblog.getPost
105 $f_metaWeblog_getPost_sig = array(array(
107 $xmlrpcStruct, // the juice
110 $xmlrpcString, // itemid
111 $xmlrpcString, // username
112 $xmlrpcString, // password
115 $f_metaWeblog_getPost_doc = "Retrieves a post";
116 function f_metaWeblog_getPost($m) {
117 $itemid = _getScalar($m,0);
118 $username = _getScalar($m,1);
119 $password = _getScalar($m,2);
121 return _mw_getPost($itemid, $username, $password);
125 // metaWeblog.editPost
126 $f_metaWeblog_editPost_sig = array(array(
128 $xmlrpcBoolean, // true
131 $xmlrpcString, // itemid
132 $xmlrpcString, // username
133 $xmlrpcString, // password
134 $xmlrpcStruct, // content
135 $xmlrpcBoolean, // publish boolean (set to false to create draft)
138 $f_metaWeblog_editPost_doc = "Edits an item";
139 function f_metaWeblog_editPost($m) {
142 $itemid = _getScalar($m,0);
143 $username = _getScalar($m,1);
144 $password = _getScalar($m,2);
147 $struct = $m->getParam(3);
148 $content = _getStructVal($struct, 'description');
149 $title = _getStructVal($struct, 'title');
151 // category is optional (thus: be careful)!
152 $catlist = $struct->structmem('categories');
153 if ($catlist && ($catlist->kindOf() == "array") && ($catlist->arraysize() > 0)) {
154 $category = _getArrayVal($catlist, 0);
157 $publish = _getScalar($m,4);
160 // get old title and extended part
161 if (!$manager->existsItem($itemid,1,1))
162 return _error(6,"No such item ($itemid)");
163 $blogid = getBlogIDFromItemID($itemid);
165 $old =& $manager->getItem($itemid,1,1);
169 // leave category unchanged when not present
170 $catid = $old['catid'];
174 $blog = new BLOG($blogid);
175 $catid = $blog->getCategoryIdFromName($category);
178 if ($old['draft'] && $publish) {
185 $more = $struct->structmem('mt_text_more');
187 $more = _getStructVal($struct, 'mt_text_more');
189 $more = $old['more'];
192 $comments = $struct->structmem('mt_allow_comments');
194 // $comments = (int) _getStructVal($struct, 'mt_allow_comments') ? 0 : 1;
195 $closed = (intval(_getStructVal($struct, 'mt_allow_comments')) == 1) ? 0 : 1;
197 // $comments = $old['closed'];
198 $closed = $old['closed'];
201 // $res = _edititem($itemid, $username, $password, $catid, $title, $content, $more, $wasdraft, $publish, $comments);
202 $res = _edititem($itemid, $username, $password, $catid, $title, $content, $more, $wasdraft, $publish, $closed);
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);
214 'urls' => &$trackbacks
216 $manager->notify('SendTrackback', $param);
222 // metaWeblog.newMediaObject
223 $f_metaWeblog_newMediaObject_sig = array(array(
225 $xmlrpcStruct, // "url" element
228 $xmlrpcString, // blogid
229 $xmlrpcString, // username
230 $xmlrpcString, // password
231 $xmlrpcStruct // 'name', 'type' and 'bits'
233 $f_metaWeblog_newMediaObject_doc = 'Uploads a file to to the media library of the user';
234 function f_metaWeblog_newMediaObject($m) {
235 $blogid = _getScalar($m, 0);
236 $username = _getScalar($m, 1);
237 $password = _getScalar($m, 2);
239 $struct = $m->getParam(3);
240 $name = _getStructVal($struct, 'name');
241 $type = _getStructVal($struct, 'type');
242 $bits = _getStructVal($struct, 'bits');
244 return _newMediaObject($blogid, $username, $password, array('name' => $name, 'type' => $type, 'bits' => $bits));
247 // metaWeblog.getRecentPosts
248 $f_metaWeblog_getRecentPosts_sig = array(array(
250 $xmlrpcStruct, // array of structs
253 $xmlrpcString, // blogid
254 $xmlrpcString, // username
255 $xmlrpcString, // password
256 $xmlrpcInt // number of posts
258 $f_metaWeblog_getRecentPosts_doc = 'Returns recent weblog items.';
259 function f_metaWeblog_getRecentPosts($m) {
260 $blogid = _getScalar($m, 0);
261 $username = _getScalar($m, 1);
262 $password = _getScalar($m, 2);
263 $amount = intval(_getScalar($m, 3));
265 return _getRecentItemsMetaWeblog($blogid, $username, $password, $amount);
268 function _getRecentItemsMetaWeblog($blogid, $username, $password, $amount) {
270 $blogid = intval($blogid);
271 $amount = intval($amount);
275 if (!$mem->login($username, $password))
276 return _error(1,"Could not log in");
278 // 2. check if allowed
279 if (!BLOG::existsID($blogid))
280 return _error(2,"No such blog ($blogid)");
281 if (!$mem->teamRights($blogid))
282 return _error(3,"Not a team member");
283 $amount = intval($amount);
284 if (($amount < 1) or ($amount > 20))
285 return _error(5,"Amount parameter must be in range 1..20");
287 // 3. create and return list of recent items
288 // Struct returned has dateCreated, userid, blogid and content
290 $blog = new BLOG($blogid);
292 $structarray = array(); // the array in which the structs will be stored
294 $query = "SELECT mname, ibody, imore, iauthor, ibody, inumber, ititle as title, itime, cname as category, iclosed"
295 .' FROM '.sql_table('item').', '.sql_table('category').', '.sql_table('member')
296 ." WHERE iblog=$blogid and icat=catid and iauthor=mnumber"
297 ." ORDER BY itime DESC"
299 $r = sql_query($query);
301 while ($row = sql_fetch_assoc($r)) {
303 // remove linebreaks if needed
304 if ($blog->convertBreaks()) {
305 $row['ibody'] = removeBreaks($row['ibody']);
306 $row['imore'] = removeBreaks($row['imore']);
309 $newstruct = new xmlrpcval(array(
310 "dateCreated" => new xmlrpcval(iso8601_encode(strtotime($row['itime'])),"dateTime.iso8601"),
311 "userid" => new xmlrpcval($row['iauthor'],"string"),
312 "blogid" => new xmlrpcval($blogid,"string"),
313 "postid" => new xmlrpcval($row['inumber'],"string"),
314 "description" => new xmlrpcval($row['ibody'],"string"),
315 "title" => new xmlrpcval($row['title'],"string"),
316 "categories" => new xmlrpcval(
318 new xmlrpcval($row['category'], "string")
323 "mt_text_more" => new xmlrpcval($row['imore'], "string"),
324 "mt_allow_comments" => new xmlrpcval($row['iclosed'] ? 0 : 1, "int"),
325 "mt_allow_pings" => new xmlrpcval(1, "int")
329 //TODO: String permaLink?
332 array_push($structarray, $newstruct);
335 return new xmlrpcresp(new xmlrpcval( $structarray , "array"));
338 function _newMediaObject($blogid, $username, $password, $info) {
339 global $CONF, $DIR_MEDIA, $DIR_LIBS;
343 if (!$mem->login($username, $password))
344 return _error(1,'Could not log in');
346 // - check if team member
347 if (!BLOG::existsID($blogid))
348 return _error(2,"No such blog ($blogid)");
349 if (!$mem->teamRights($blogid))
350 return _error(3,'Not a team member');
352 $b = new BLOG($blogid);
355 $data = $info['bits']; // decoding was done transparantly by xmlrpclib
358 if (strlen($data) > $CONF['MaxUploadSize'])
359 return _error(9, 'filesize is too big');
362 // - check if filetype is allowed (check filename)
363 $filename = $info['name'];
365 $allowedtypes = explode (',', $CONF['AllowedTypes']);
366 foreach ( $allowedtypes as $type )
368 //if (eregi("\." .$type. "$",$filename)) $ok = 1;
369 if (preg_match("#\." .$type. "$#i",$filename)) $ok = 1;
372 _error(8, 'Filetype is not allowed');
374 // - add file to media library
375 //include_once($DIR_LIBS . 'MEDIA.php'); // media classes
376 include_libs('MEDIA.php',true,false);
378 // always use private media library of member
379 $collection = $mem->getID();
381 // prefix filename with current date (YYYY-MM-DD-)
382 // this to avoid nameclashes
383 if ($CONF['MediaPrefix'])
384 $filename = strftime("%Y%m%d-", time()) . $filename;
386 $res = MEDIA::addMediaObjectRaw($collection, $filename, $data);
388 return _error(10, $res);
391 $urlstruct = new xmlrpcval(array(
392 "url" => new xmlrpcval($CONF['MediaURL'] . $collection. '/' . $filename,'string')
395 return new xmlrpcresp($urlstruct);
398 function _categoryList($blogid, $username, $password) {
401 if (!$mem->login($username, $password))
402 return _error(1,"Could not log in");
404 // check if on team and blog exists
405 if (!BLOG::existsID($blogid))
406 return _error(2,"No such blog ($blogid)");
407 if (!$mem->teamRights($blogid))
408 return _error(3,"Not a team member");
410 $b = new BLOG($blogid);
412 $categorystruct = array();
414 $query = "SELECT cname, cdesc, catid"
415 . ' FROM '.sql_table('category')
416 . " WHERE cblog=" . intval($blogid)
418 $r = sql_query($query);
420 while ($obj = sql_fetch_object($r)) {
422 $categorystruct[$obj->cname] = new xmlrpcval(
424 "description" => new xmlrpcval($obj->cdesc,"string"),
425 "htmlUrl" => new xmlrpcval($b->getURL() . "?catid=" . $obj->catid ,"string"),
426 "rssUrl" => new xmlrpcval("","string")
432 return new xmlrpcresp(new xmlrpcval( $categorystruct , "struct"));
437 function _mw_getPost($itemid, $username, $password) {
442 if (!$mem->login($username, $password))
443 return _error(1,"Could not log in");
445 // 2. check if allowed
446 if (!$manager->existsItem($itemid,1,1))
447 return _error(6,"No such item ($itemid)");
448 $blogid = getBlogIDFromItemID($itemid);
449 if (!$mem->teamRights($blogid))
450 return _error(3,"Not a team member");
452 // 3. return the item
453 $item =& $manager->getItem($itemid,1,1); // (also allow drafts and future items)
455 $b = new BLOG($blogid);
456 if ($b->convertBreaks()) {
457 $item['body'] = removeBreaks($item['body']);
458 $item['more'] = removeBreaks($item['more']);
461 $categoryname = $b->getCategoryName($item['catid']);
463 $newstruct = new xmlrpcval(array(
464 "dateCreated" => new xmlrpcval(iso8601_encode($item['timestamp']),"dateTime.iso8601"),
465 "userid" => new xmlrpcval($item['authorid'],"string"),
466 "blogid" => new xmlrpcval($blogid,"string"),
467 "postid" => new xmlrpcval($itemid, "string"),
468 "description" => new xmlrpcval($item['body'],"string"),
469 "title" => new xmlrpcval($item['title'],"string"),
470 "categories" => new xmlrpcval(
472 new xmlrpcval($categoryname, "string")
476 "mt_text_more" => new xmlrpcval($item['more'], "string"),
477 "mt_allow_comments" => new xmlrpcval($item['closed'] ? 0 : 1, "int"),
478 "mt_allow_pings" => new xmlrpcval(1, "int")
481 //TODO: add "String link" to struct?
482 //TODO: add "String permaLink" to struct?
484 return new xmlrpcresp($newstruct);
488 $functionDefs = array_merge($functionDefs,
490 "metaWeblog.newPost" =>
492 "function" => "f_metaWeblog_newPost",
493 "signature" => $f_metaWeblog_newPost_sig,
494 "docstring" => $f_metaWeblog_newPost_doc
497 "metaWeblog.getCategories" =>
499 "function" => "f_metaWeblog_getCategories",
500 "signature" => $f_metaWeblog_getCategories_sig,
501 "docstring" => $f_metaWeblog_getCategories_doc
504 "metaWeblog.getPost" =>
506 "function" => "f_metaWeblog_getPost",
507 "signature" => $f_metaWeblog_getPost_sig,
508 "docstring" => $f_metaWeblog_getPost_doc
511 "metaWeblog.editPost" =>
513 "function" => "f_metaWeblog_editPost",
514 "signature" => $f_metaWeblog_editPost_sig,
515 "docstring" => $f_metaWeblog_editPost_doc
518 'metaWeblog.newMediaObject' =>
520 'function' => 'f_metaWeblog_newMediaObject',
521 'signature' => $f_metaWeblog_newMediaObject_sig,
522 'docstring' => $f_metaWeblog_newMediaObject_doc
525 'metaWeblog.getRecentPosts' =>
527 'function' => 'f_metaWeblog_getRecentPosts',
528 'signature' => $f_metaWeblog_getRecentPosts_sig,
529 'docstring' => $f_metaWeblog_getRecentPosts_doc