4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5 * Copyright (C) 2002-2010 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-2010 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);
79 $manager->notify('SendTrackback', array ('tb_id' => $itemid, 'urls' => & $trackbacks));
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 = new BLOG($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;
198 $closed = (intval(_getStructVal($struct, 'mt_allow_comments')) == 1) ? 0 : 1;
200 // $comments = $old['closed'];
201 $closed = $old['closed'];
204 // $res = _edititem($itemid, $username, $password, $catid, $title, $content, $more, $wasdraft, $publish, $comments);
205 $res = _edititem($itemid, $username, $password, $catid, $title, $content, $more, $wasdraft, $publish, $closed);
207 $trackbacks = array();
208 $tblist = $struct->structmem('mt_tb_ping_urls');
209 if ($tblist && ($tblist->kindOf() == "array") && ($tblist->arraysize() > 0)) {
211 for ($i = 0; $i < $tblist->arraysize(); $i++) {
212 $trackbacks[] = _getArrayVal($tblist, $i);
215 $manager->notify('SendTrackback', array ('tb_id' => $itemid, 'urls' => & $trackbacks));
221 // metaWeblog.newMediaObject
222 $f_metaWeblog_newMediaObject_sig = array(array(
224 $xmlrpcStruct, // "url" element
227 $xmlrpcString, // blogid
228 $xmlrpcString, // username
229 $xmlrpcString, // password
230 $xmlrpcStruct // 'name', 'type' and 'bits'
232 $f_metaWeblog_newMediaObject_doc = 'Uploads a file to to the media library of the user';
233 function f_metaWeblog_newMediaObject($m) {
234 $blogid = _getScalar($m, 0);
235 $username = _getScalar($m, 1);
236 $password = _getScalar($m, 2);
238 $struct = $m->getParam(3);
239 $name = _getStructVal($struct, 'name');
240 $type = _getStructVal($struct, 'type');
241 $bits = _getStructVal($struct, 'bits');
243 return _newMediaObject($blogid, $username, $password, array('name' => $name, 'type' => $type, 'bits' => $bits));
246 // metaWeblog.getRecentPosts
247 $f_metaWeblog_getRecentPosts_sig = array(array(
249 $xmlrpcStruct, // array of structs
252 $xmlrpcString, // blogid
253 $xmlrpcString, // username
254 $xmlrpcString, // password
255 $xmlrpcInt // number of posts
257 $f_metaWeblog_getRecentPosts_doc = 'Returns recent weblog items.';
258 function f_metaWeblog_getRecentPosts($m) {
259 $blogid = _getScalar($m, 0);
260 $username = _getScalar($m, 1);
261 $password = _getScalar($m, 2);
262 $amount = intval(_getScalar($m, 3));
264 return _getRecentItemsMetaWeblog($blogid, $username, $password, $amount);
267 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 = new BLOG($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 = sql_query($query);
300 while ($row = sql_fetch_assoc($r)) {
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;
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 = new BLOG($blogid);
354 $data = $info['bits']; // decoding was done transparantly by xmlrpclib
357 if (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 = explode (',', $CONF['AllowedTypes']);
365 foreach ( $allowedtypes as $type )
366 if (eregi("\." .$type. "$",$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
373 // always use private media library of member
374 $collection = $mem->getID();
376 // prefix filename with current date (YYYY-MM-DD-)
377 // this to avoid nameclashes
378 if ($CONF['MediaPrefix'])
379 $filename = strftime("%Y%m%d-", time()) . $filename;
381 $res = MEDIA::addMediaObjectRaw($collection, $filename, $data);
383 return _error(10, $res);
386 $urlstruct = new xmlrpcval(array(
387 "url" => new xmlrpcval($CONF['MediaURL'] . $collection. '/' . $filename,'string')
390 return new xmlrpcresp($urlstruct);
393 function _categoryList($blogid, $username, $password) {
396 if (!$mem->login($username, $password))
397 return _error(1,"Could not log in");
399 // check if on team and blog exists
400 if (!BLOG::existsID($blogid))
401 return _error(2,"No such blog ($blogid)");
402 if (!$mem->teamRights($blogid))
403 return _error(3,"Not a team member");
405 $b = new BLOG($blogid);
407 $categorystruct = array();
409 $query = "SELECT cname, cdesc, catid"
410 . ' FROM '.sql_table('category')
411 . " WHERE cblog=" . intval($blogid)
413 $r = sql_query($query);
415 while ($obj = sql_fetch_object($r)) {
417 $categorystruct[$obj->cname] = new xmlrpcval(
419 "description" => new xmlrpcval($obj->cdesc,"string"),
420 "htmlUrl" => new xmlrpcval($b->getURL() . "?catid=" . $obj->catid ,"string"),
421 "rssUrl" => new xmlrpcval("","string")
427 return new xmlrpcresp(new xmlrpcval( $categorystruct , "struct"));
432 function _mw_getPost($itemid, $username, $password) {
437 if (!$mem->login($username, $password))
438 return _error(1,"Could not log in");
440 // 2. check if allowed
441 if (!$manager->existsItem($itemid,1,1))
442 return _error(6,"No such item ($itemid)");
443 $blogid = getBlogIDFromItemID($itemid);
444 if (!$mem->teamRights($blogid))
445 return _error(3,"Not a team member");
447 // 3. return the item
448 $item =& $manager->getItem($itemid,1,1); // (also allow drafts and future items)
450 $b = new BLOG($blogid);
451 if ($b->convertBreaks()) {
452 $item['body'] = removeBreaks($item['body']);
453 $item['more'] = removeBreaks($item['more']);
456 $categoryname = $b->getCategoryName($item['catid']);
458 $newstruct = new xmlrpcval(array(
459 "dateCreated" => new xmlrpcval(iso8601_encode($item['timestamp']),"dateTime.iso8601"),
460 "userid" => new xmlrpcval($item['authorid'],"string"),
461 "blogid" => new xmlrpcval($blogid,"string"),
462 "postid" => new xmlrpcval($itemid, "string"),
463 "description" => new xmlrpcval($item['body'],"string"),
464 "title" => new xmlrpcval($item['title'],"string"),
465 "categories" => new xmlrpcval(
467 new xmlrpcval($categoryname, "string")
471 "mt_text_more" => new xmlrpcval($item['more'], "string"),
472 "mt_allow_comments" => new xmlrpcval($item['closed'] ? 0 : 1, "int"),
473 "mt_allow_pings" => new xmlrpcval(1, "int")
476 //TODO: add "String link" to struct?
477 //TODO: add "String permaLink" to struct?
479 return new xmlrpcresp($newstruct);
483 $functionDefs = array_merge($functionDefs,
485 "metaWeblog.newPost" =>
487 "function" => "f_metaWeblog_newPost",
488 "signature" => $f_metaWeblog_newPost_sig,
489 "docstring" => $f_metaWeblog_newPost_doc
492 "metaWeblog.getCategories" =>
494 "function" => "f_metaWeblog_getCategories",
495 "signature" => $f_metaWeblog_getCategories_sig,
496 "docstring" => $f_metaWeblog_getCategories_doc
499 "metaWeblog.getPost" =>
501 "function" => "f_metaWeblog_getPost",
502 "signature" => $f_metaWeblog_getPost_sig,
503 "docstring" => $f_metaWeblog_getPost_doc
506 "metaWeblog.editPost" =>
508 "function" => "f_metaWeblog_editPost",
509 "signature" => $f_metaWeblog_editPost_sig,
510 "docstring" => $f_metaWeblog_editPost_doc
513 'metaWeblog.newMediaObject' =>
515 'function' => 'f_metaWeblog_newMediaObject',
516 'signature' => $f_metaWeblog_newMediaObject_sig,
517 'docstring' => $f_metaWeblog_newMediaObject_doc
520 'metaWeblog.getRecentPosts' =>
522 'function' => 'f_metaWeblog_getRecentPosts',
523 'signature' => $f_metaWeblog_getRecentPosts_sig,
524 'docstring' => $f_metaWeblog_getRecentPosts_doc