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
20 * $NucleusJP: api_metaweblog.inc.php,v 1.5 2006/07/17 20:03:45 kimitake Exp $
25 $f_metaWeblog_newPost_sig = array(array(
27 $xmlrpcString, // itemid of the new item
30 $xmlrpcString, // blogid
31 $xmlrpcString, // username
32 $xmlrpcString, // password
33 $xmlrpcStruct, // content
34 $xmlrpcBoolean, // publish boolean (set to false to create draft)
37 $f_metaWeblog_newPost_doc = "Adds a new item to the given blog. Adds it as a draft when publish is false";
38 function f_metaWeblog_newPost($m) {
41 $blogid = _getScalar($m,0);
42 $username = _getScalar($m,1);
43 $password = _getScalar($m,2);
44 $struct = $m->getParam(3);
46 $content = _getStructVal($struct, 'description');
47 $more = _getStructVal($struct, 'mt_text_more');
48 $title = _getStructVal($struct, 'title');
50 // category is optional (thus: be careful)!
51 $catlist = $struct->structmem('categories');
52 if ($catlist && ($catlist->kindOf() == "array") && ($catlist->arraysize() > 0))
53 $category = _getArrayVal($catlist, 0);
56 // $comments = (int) _getStructVal($struct, 'mt_allow_comments') ? 0 : 1;
57 $comments = $struct->structmem('mt_allow_comments');
59 $closed = (intval(_getStructVal($struct, 'mt_allow_comments')) == 1) ? 0 : 1;
63 $publish = _getScalar($m,4);
67 // $res = _addItem($blogid, $username, $password, $title, $content, $more, $publish, $comments, $category);
68 $res = _addItem($blogid, $username, $password, $title, $content, $more, $publish, $closed, $category);
71 $trackbacks = array();
72 $tblist = $struct->structmem('mt_tb_ping_urls');
73 if ($tblist && ($tblist->kindOf() == "array") && ($tblist->arraysize() > 0)) {
75 for ($i = 0; $i < $tblist->arraysize(); $i++) {
76 $trackbacks[] = _getArrayVal($tblist, $i);
81 'urls' => &$trackbacks
83 $manager->notify('SendTrackback', $param);
90 // metaWeblog.getCategories
91 $f_metaWeblog_getCategories_sig = array(array(
93 $xmlrpcStruct, // categories for blog
96 $xmlrpcString, // blogid
97 $xmlrpcString, // username
98 $xmlrpcString, // password
101 $f_metaWeblog_getCategories_doc = "Returns the categories for a given blog";
102 function f_metaWeblog_getCategories($m) {
103 $blogid = _getScalar($m,0);
104 $username = _getScalar($m,1);
105 $password = _getScalar($m,2);
107 return _categoryList($blogid, $username, $password);
111 // metaWeblog.getPost
112 $f_metaWeblog_getPost_sig = array(array(
114 $xmlrpcStruct, // the juice
117 $xmlrpcString, // itemid
118 $xmlrpcString, // username
119 $xmlrpcString, // password
122 $f_metaWeblog_getPost_doc = "Retrieves a post";
123 function f_metaWeblog_getPost($m) {
124 $itemid = _getScalar($m,0);
125 $username = _getScalar($m,1);
126 $password = _getScalar($m,2);
128 return _mw_getPost($itemid, $username, $password);
132 // metaWeblog.editPost
133 $f_metaWeblog_editPost_sig = array(array(
135 $xmlrpcBoolean, // true
138 $xmlrpcString, // itemid
139 $xmlrpcString, // username
140 $xmlrpcString, // password
141 $xmlrpcStruct, // content
142 $xmlrpcBoolean, // publish boolean (set to false to create draft)
145 $f_metaWeblog_editPost_doc = "Edits an item";
146 function f_metaWeblog_editPost($m) {
149 $itemid = _getScalar($m,0);
150 $username = _getScalar($m,1);
151 $password = _getScalar($m,2);
154 $struct = $m->getParam(3);
155 $content = _getStructVal($struct, 'description');
156 $title = _getStructVal($struct, 'title');
158 // category is optional (thus: be careful)!
159 $catlist = $struct->structmem('categories');
160 if ($catlist && ($catlist->kindOf() == "array") && ($catlist->arraysize() > 0)) {
161 $category = _getArrayVal($catlist, 0);
164 $publish = _getScalar($m,4);
167 // get old title and extended part
168 if (!$manager->existsItem($itemid,1,1))
169 return _error(6,"No such item ($itemid)");
170 $blogid = getBlogIDFromItemID($itemid);
172 $old =& $manager->getItem($itemid,1,1);
176 // leave category unchanged when not present
177 $catid = $old['catid'];
181 $blog = new BLOG($blogid);
182 $catid = $blog->getCategoryIdFromName($category);
185 if ($old['draft'] && $publish) {
192 $more = $struct->structmem('mt_text_more');
194 $more = _getStructVal($struct, 'mt_text_more');
196 $more = $old['more'];
199 $comments = $struct->structmem('mt_allow_comments');
201 // $comments = (int) _getStructVal($struct, 'mt_allow_comments') ? 0 : 1;
202 $closed = (intval(_getStructVal($struct, 'mt_allow_comments')) == 1) ? 0 : 1;
204 // $comments = $old['closed'];
205 $closed = $old['closed'];
208 // $res = _edititem($itemid, $username, $password, $catid, $title, $content, $more, $wasdraft, $publish, $comments);
209 $res = _edititem($itemid, $username, $password, $catid, $title, $content, $more, $wasdraft, $publish, $closed);
211 $trackbacks = array();
212 $tblist = $struct->structmem('mt_tb_ping_urls');
213 if ($tblist && ($tblist->kindOf() == "array") && ($tblist->arraysize() > 0)) {
215 for ($i = 0; $i < $tblist->arraysize(); $i++) {
216 $trackbacks[] = _getArrayVal($tblist, $i);
221 'urls' => &$trackbacks
223 $manager->notify('SendTrackback', $param);
229 // metaWeblog.newMediaObject
230 $f_metaWeblog_newMediaObject_sig = array(array(
232 $xmlrpcStruct, // "url" element
235 $xmlrpcString, // blogid
236 $xmlrpcString, // username
237 $xmlrpcString, // password
238 $xmlrpcStruct // 'name', 'type' and 'bits'
240 $f_metaWeblog_newMediaObject_doc = 'Uploads a file to to the media library of the user';
241 function f_metaWeblog_newMediaObject($m) {
242 $blogid = _getScalar($m, 0);
243 $username = _getScalar($m, 1);
244 $password = _getScalar($m, 2);
246 $struct = $m->getParam(3);
247 $name = _getStructVal($struct, 'name');
248 $type = _getStructVal($struct, 'type');
249 $bits = _getStructVal($struct, 'bits');
251 return _newMediaObject($blogid, $username, $password, array('name' => $name, 'type' => $type, 'bits' => $bits));
254 // metaWeblog.getRecentPosts
255 $f_metaWeblog_getRecentPosts_sig = array(array(
257 $xmlrpcStruct, // array of structs
260 $xmlrpcString, // blogid
261 $xmlrpcString, // username
262 $xmlrpcString, // password
263 $xmlrpcInt // number of posts
265 $f_metaWeblog_getRecentPosts_doc = 'Returns recent weblog items.';
266 function f_metaWeblog_getRecentPosts($m) {
267 $blogid = _getScalar($m, 0);
268 $username = _getScalar($m, 1);
269 $password = _getScalar($m, 2);
270 $amount = intval(_getScalar($m, 3));
272 return _getRecentItemsMetaWeblog($blogid, $username, $password, $amount);
275 function _getRecentItemsMetaWeblog($blogid, $username, $password, $amount) {
277 $blogid = intval($blogid);
278 $amount = intval($amount);
282 if (!$mem->login($username, $password))
283 return _error(1,"Could not log in");
285 // 2. check if allowed
286 if (!BLOG::existsID($blogid))
287 return _error(2,"No such blog ($blogid)");
288 if (!$mem->teamRights($blogid))
289 return _error(3,"Not a team member");
290 $amount = intval($amount);
291 if (($amount < 1) or ($amount > 20))
292 return _error(5,"Amount parameter must be in range 1..20");
294 // 3. create and return list of recent items
295 // Struct returned has dateCreated, userid, blogid and content
297 $blog = new BLOG($blogid);
299 $structarray = array(); // the array in which the structs will be stored
301 $query = "SELECT mname, ibody, imore, iauthor, ibody, inumber, ititle as title, itime, cname as category, iclosed"
302 .' FROM '.sql_table('item').', '.sql_table('category').', '.sql_table('member')
303 ." WHERE iblog=$blogid and icat=catid and iauthor=mnumber"
304 ." ORDER BY itime DESC"
306 $r = sql_query($query);
308 while ($row = sql_fetch_assoc($r)) {
310 // remove linebreaks if needed
311 if ($blog->convertBreaks()) {
312 $row['ibody'] = removeBreaks($row['ibody']);
313 $row['imore'] = removeBreaks($row['imore']);
316 $newstruct = new xmlrpcval(array(
317 "dateCreated" => new xmlrpcval(iso8601_encode(strtotime($row['itime'])),"dateTime.iso8601"),
318 "userid" => new xmlrpcval($row['iauthor'],"string"),
319 "blogid" => new xmlrpcval($blogid,"string"),
320 "postid" => new xmlrpcval($row['inumber'],"string"),
321 "description" => new xmlrpcval($row['ibody'],"string"),
322 "title" => new xmlrpcval($row['title'],"string"),
323 "categories" => new xmlrpcval(
325 new xmlrpcval($row['category'], "string")
330 "mt_text_more" => new xmlrpcval($row['imore'], "string"),
331 "mt_allow_comments" => new xmlrpcval($row['iclosed'] ? 0 : 1, "int"),
332 "mt_allow_pings" => new xmlrpcval(1, "int")
336 //TODO: String permaLink?
339 array_push($structarray, $newstruct);
342 return new xmlrpcresp(new xmlrpcval( $structarray , "array"));
345 function _newMediaObject($blogid, $username, $password, $info) {
346 global $CONF, $DIR_MEDIA, $DIR_LIBS;
350 if (!$mem->login($username, $password))
351 return _error(1,'Could not log in');
353 // - check if team member
354 if (!BLOG::existsID($blogid))
355 return _error(2,"No such blog ($blogid)");
356 if (!$mem->teamRights($blogid))
357 return _error(3,'Not a team member');
359 $b = new BLOG($blogid);
362 $data = $info['bits']; // decoding was done transparantly by xmlrpclib
365 if (strlen($data) > $CONF['MaxUploadSize'])
366 return _error(9, 'filesize is too big');
369 // - check if filetype is allowed (check filename)
370 $filename = $info['name'];
372 $allowedtypes = explode (',', $CONF['AllowedTypes']);
373 foreach ( $allowedtypes as $type )
375 //if (eregi("\." .$type. "$",$filename)) $ok = 1;
376 if (preg_match("#\." .$type. "$#i",$filename)) $ok = 1;
379 _error(8, 'Filetype is not allowed');
381 // - add file to media library
382 //include_once($DIR_LIBS . 'MEDIA.php'); // media classes
383 include_libs('MEDIA.php',true,false);
385 // always use private media library of member
386 $collection = $mem->getID();
388 // prefix filename with current date (YYYY-MM-DD-)
389 // this to avoid nameclashes
390 if ($CONF['MediaPrefix'])
391 $filename = strftime("%Y%m%d-", time()) . $filename;
393 $res = MEDIA::addMediaObjectRaw($collection, $filename, $data);
395 return _error(10, $res);
398 $urlstruct = new xmlrpcval(array(
399 "url" => new xmlrpcval($CONF['MediaURL'] . $collection. '/' . $filename,'string')
402 return new xmlrpcresp($urlstruct);
405 function _categoryList($blogid, $username, $password) {
408 if (!$mem->login($username, $password))
409 return _error(1,"Could not log in");
411 // check if on team and blog exists
412 if (!BLOG::existsID($blogid))
413 return _error(2,"No such blog ($blogid)");
414 if (!$mem->teamRights($blogid))
415 return _error(3,"Not a team member");
417 $b = new BLOG($blogid);
419 $categorystruct = array();
421 $query = "SELECT cname, cdesc, catid"
422 . ' FROM '.sql_table('category')
423 . " WHERE cblog=" . intval($blogid)
425 $r = sql_query($query);
427 while ($obj = sql_fetch_object($r)) {
429 $categorystruct[$obj->cname] = new xmlrpcval(
431 "description" => new xmlrpcval($obj->cdesc,"string"),
432 "htmlUrl" => new xmlrpcval($b->getURL() . "?catid=" . $obj->catid ,"string"),
433 "rssUrl" => new xmlrpcval("","string")
439 return new xmlrpcresp(new xmlrpcval( $categorystruct , "struct"));
444 function _mw_getPost($itemid, $username, $password) {
449 if (!$mem->login($username, $password))
450 return _error(1,"Could not log in");
452 // 2. check if allowed
453 if (!$manager->existsItem($itemid,1,1))
454 return _error(6,"No such item ($itemid)");
455 $blogid = getBlogIDFromItemID($itemid);
456 if (!$mem->teamRights($blogid))
457 return _error(3,"Not a team member");
459 // 3. return the item
460 $item =& $manager->getItem($itemid,1,1); // (also allow drafts and future items)
462 $b = new BLOG($blogid);
463 if ($b->convertBreaks()) {
464 $item['body'] = removeBreaks($item['body']);
465 $item['more'] = removeBreaks($item['more']);
468 $categoryname = $b->getCategoryName($item['catid']);
470 $newstruct = new xmlrpcval(array(
471 "dateCreated" => new xmlrpcval(iso8601_encode($item['timestamp']),"dateTime.iso8601"),
472 "userid" => new xmlrpcval($item['authorid'],"string"),
473 "blogid" => new xmlrpcval($blogid,"string"),
474 "postid" => new xmlrpcval($itemid, "string"),
475 "description" => new xmlrpcval($item['body'],"string"),
476 "title" => new xmlrpcval($item['title'],"string"),
477 "categories" => new xmlrpcval(
479 new xmlrpcval($categoryname, "string")
483 "mt_text_more" => new xmlrpcval($item['more'], "string"),
484 "mt_allow_comments" => new xmlrpcval($item['closed'] ? 0 : 1, "int"),
485 "mt_allow_pings" => new xmlrpcval(1, "int")
488 //TODO: add "String link" to struct?
489 //TODO: add "String permaLink" to struct?
491 return new xmlrpcresp($newstruct);
495 $functionDefs = array_merge($functionDefs,
497 "metaWeblog.newPost" =>
499 "function" => "f_metaWeblog_newPost",
500 "signature" => $f_metaWeblog_newPost_sig,
501 "docstring" => $f_metaWeblog_newPost_doc
504 "metaWeblog.getCategories" =>
506 "function" => "f_metaWeblog_getCategories",
507 "signature" => $f_metaWeblog_getCategories_sig,
508 "docstring" => $f_metaWeblog_getCategories_doc
511 "metaWeblog.getPost" =>
513 "function" => "f_metaWeblog_getPost",
514 "signature" => $f_metaWeblog_getPost_sig,
515 "docstring" => $f_metaWeblog_getPost_doc
518 "metaWeblog.editPost" =>
520 "function" => "f_metaWeblog_editPost",
521 "signature" => $f_metaWeblog_editPost_sig,
522 "docstring" => $f_metaWeblog_editPost_doc
525 'metaWeblog.newMediaObject' =>
527 'function' => 'f_metaWeblog_newMediaObject',
528 'signature' => $f_metaWeblog_newMediaObject_sig,
529 'docstring' => $f_metaWeblog_newMediaObject_doc
532 'metaWeblog.getRecentPosts' =>
534 'function' => 'f_metaWeblog_getRecentPosts',
535 'signature' => $f_metaWeblog_getRecentPosts_sig,
536 'docstring' => $f_metaWeblog_getRecentPosts_doc