00001 <?php 00002 00003 /* 00004 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/) 00005 * Copyright (C) 2002-2007 The Nucleus Group 00006 * 00007 * This program is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU General Public License 00009 * as published by the Free Software Foundation; either version 2 00010 * of the License, or (at your option) any later version. 00011 * (see nucleus/documentation/index.html#license for more info) 00012 */ 00013 00024 // metaWeblog.newPost 00025 $f_metaWeblog_newPost_sig = array(array( 00026 // return type 00027 $xmlrpcString, // itemid of the new item 00028 00029 // params: 00030 $xmlrpcString, // blogid 00031 $xmlrpcString, // username 00032 $xmlrpcString, // password 00033 $xmlrpcStruct, // content 00034 $xmlrpcBoolean, // publish boolean (set to false to create draft) 00035 00036 )); 00037 $f_metaWeblog_newPost_doc = "Adds a new item to the given blog. Adds it as a draft when publish is false"; 00038 function f_metaWeblog_newPost($m) { 00039 global $manager; 00040 00041 $blogid = _getScalar($m,0); 00042 $username = _getScalar($m,1); 00043 $password = _getScalar($m,2); 00044 $struct = $m->getParam(3); 00045 00046 $content = _getStructVal($struct, 'description'); 00047 $more = _getStructVal($struct, 'mt_text_more'); 00048 $title = _getStructVal($struct, 'title'); 00049 00050 // category is optional (thus: be careful)! 00051 $catlist = $struct->structmem('categories'); 00052 if ($catlist && ($catlist->kindOf() == "array") && ($catlist->arraysize() > 0)) 00053 $category = _getArrayVal($catlist, 0); 00054 00055 00056 $comments = (int) _getStructVal($struct, 'mt_allow_comments') ? 0 : 1; 00057 $publish = _getScalar($m,4); 00058 00059 00060 // Add item 00061 $res = _addItem($blogid, $username, $password, $title, $content, $more, $publish, $comments, $category); 00062 00063 // Handle trackbacks 00064 $trackbacks = array(); 00065 $tblist = $struct->structmem('mt_tb_ping_urls'); 00066 if ($tblist && ($tblist->kindOf() == "array") && ($tblist->arraysize() > 0)) { 00067 00068 for ($i = 0; $i < $tblist->arraysize(); $i++) { 00069 $trackbacks[] = _getArrayVal($tblist, $i); 00070 } 00071 00072 $manager->notify('SendTrackback', array ('tb_id' => $itemid, 'urls' => & $trackbacks)); 00073 } 00074 00075 return $res; 00076 } 00077 00078 00079 // metaWeblog.getCategories 00080 $f_metaWeblog_getCategories_sig = array(array( 00081 // return 00082 $xmlrpcStruct, // categories for blog 00083 00084 // params 00085 $xmlrpcString, // blogid 00086 $xmlrpcString, // username 00087 $xmlrpcString, // password 00088 00089 )); 00090 $f_metaWeblog_getCategories_doc = "Returns the categories for a given blog"; 00091 function f_metaWeblog_getCategories($m) { 00092 $blogid = _getScalar($m,0); 00093 $username = _getScalar($m,1); 00094 $password = _getScalar($m,2); 00095 00096 return _categoryList($blogid, $username, $password); 00097 } 00098 00099 00100 // metaWeblog.getPost 00101 $f_metaWeblog_getPost_sig = array(array( 00102 // return 00103 $xmlrpcStruct, // the juice 00104 00105 // params 00106 $xmlrpcString, // itemid 00107 $xmlrpcString, // username 00108 $xmlrpcString, // password 00109 00110 )); 00111 $f_metaWeblog_getPost_doc = "Retrieves a post"; 00112 function f_metaWeblog_getPost($m) { 00113 $itemid = _getScalar($m,0); 00114 $username = _getScalar($m,1); 00115 $password = _getScalar($m,2); 00116 00117 return _mw_getPost($itemid, $username, $password); 00118 } 00119 00120 00121 // metaWeblog.editPost 00122 $f_metaWeblog_editPost_sig = array(array( 00123 // return type 00124 $xmlrpcBoolean, // true 00125 00126 // params: 00127 $xmlrpcString, // itemid 00128 $xmlrpcString, // username 00129 $xmlrpcString, // password 00130 $xmlrpcStruct, // content 00131 $xmlrpcBoolean, // publish boolean (set to false to create draft) 00132 00133 )); 00134 $f_metaWeblog_editPost_doc = "Edits an item"; 00135 function f_metaWeblog_editPost($m) { 00136 global $manager; 00137 00138 $itemid = _getScalar($m,0); 00139 $username = _getScalar($m,1); 00140 $password = _getScalar($m,2); 00141 00142 $category = ''; 00143 $struct = $m->getParam(3); 00144 $content = _getStructVal($struct, 'description'); 00145 $title = _getStructVal($struct, 'title'); 00146 00147 // category is optional (thus: be careful)! 00148 $catlist = $struct->structmem('categories'); 00149 if ($catlist && ($catlist->kindOf() == "array") && ($catlist->arraysize() > 0)) { 00150 $category = _getArrayVal($catlist, 0); 00151 } 00152 00153 $publish = _getScalar($m,4); 00154 00155 00156 // get old title and extended part 00157 if (!$manager->existsItem($itemid,1,1)) 00158 return _error(6,"No such item ($itemid)"); 00159 $blogid = getBlogIDFromItemID($itemid); 00160 00161 $old =& $manager->getItem($itemid,1,1); 00162 00163 if ($category == '') 00164 { 00165 // leave category unchanged when not present 00166 $catid = $old['catid']; 00167 } 00168 else 00169 { 00170 $blog = new BLOG($blogid); 00171 $catid = $blog->getCategoryIdFromName($category); 00172 } 00173 00174 if ($old['draft'] && $publish) { 00175 $wasdraft = 1; 00176 $publish = 1; 00177 } else { 00178 $wasdraft = 0; 00179 } 00180 00181 $more = $struct->structmem('mt_text_more'); 00182 if ($more) { 00183 $more = _getStructVal($struct, 'mt_text_more'); 00184 } else { 00185 $more = $old['more']; 00186 } 00187 00188 $comments = $struct->structmem('mt_allow_comments'); 00189 if ($comments) { 00190 $comments = (int) _getStructVal($struct, 'mt_allow_comments') ? 0 : 1; 00191 } else { 00192 $comments = $old['closed']; 00193 } 00194 00195 $res = _edititem($itemid, $username, $password, $catid, $title, $content, $more, $wasdraft, $publish, $comments); 00196 00197 // Handle trackbacks 00198 $trackbacks = array(); 00199 $tblist = $struct->structmem('mt_tb_ping_urls'); 00200 if ($tblist && ($tblist->kindOf() == "array") && ($tblist->arraysize() > 0)) { 00201 00202 for ($i = 0; $i < $tblist->arraysize(); $i++) { 00203 $trackbacks[] = _getArrayVal($tblist, $i); 00204 } 00205 00206 $manager->notify('SendTrackback', array ('tb_id' => $itemid, 'urls' => & $trackbacks)); 00207 } 00208 00209 return $res; 00210 } 00211 00212 // metaWeblog.newMediaObject 00213 $f_metaWeblog_newMediaObject_sig = array(array( 00214 // return type 00215 $xmlrpcStruct, // "url" element 00216 00217 // params 00218 $xmlrpcString, // blogid 00219 $xmlrpcString, // username 00220 $xmlrpcString, // password 00221 $xmlrpcStruct // 'name', 'type' and 'bits' 00222 )); 00223 $f_metaWeblog_newMediaObject_doc = 'Uploads a file to to the media library of the user'; 00224 function f_metaWeblog_newMediaObject($m) { 00225 $blogid = _getScalar($m, 0); 00226 $username = _getScalar($m, 1); 00227 $password = _getScalar($m, 2); 00228 00229 $struct = $m->getParam(3); 00230 $name = _getStructVal($struct, 'name'); 00231 $type = _getStructVal($struct, 'type'); 00232 $bits = _getStructVal($struct, 'bits'); 00233 00234 return _newMediaObject($blogid, $username, $password, array('name' => $name, 'type' => $type, 'bits' => $bits)); 00235 } 00236 00237 // metaWeblog.getRecentPosts 00238 $f_metaWeblog_getRecentPosts_sig = array(array( 00239 // return type 00240 $xmlrpcStruct, // array of structs 00241 00242 // params 00243 $xmlrpcString, // blogid 00244 $xmlrpcString, // username 00245 $xmlrpcString, // password 00246 $xmlrpcInt // number of posts 00247 )); 00248 $f_metaWeblog_getRecentPosts_doc = 'Returns recent weblog items.'; 00249 function f_metaWeblog_getRecentPosts($m) { 00250 $blogid = _getScalar($m, 0); 00251 $username = _getScalar($m, 1); 00252 $password = _getScalar($m, 2); 00253 $amount = intval(_getScalar($m, 3)); 00254 00255 return _getRecentItemsMetaWeblog($blogid, $username, $password, $amount); 00256 } 00257 00258 function _getRecentItemsMetaWeblog($blogid, $username, $password, $amount) { 00259 00260 $blogid = intval($blogid); 00261 $amount = intval($amount); 00262 00263 // 1. login 00264 $mem = new MEMBER(); 00265 if (!$mem->login($username, $password)) 00266 return _error(1,"Could not log in"); 00267 00268 // 2. check if allowed 00269 if (!BLOG::existsID($blogid)) 00270 return _error(2,"No such blog ($blogid)"); 00271 if (!$mem->teamRights($blogid)) 00272 return _error(3,"Not a team member"); 00273 $amount = intval($amount); 00274 if (($amount < 1) or ($amount > 20)) 00275 return _error(5,"Amount parameter must be in range 1..20"); 00276 00277 // 3. create and return list of recent items 00278 // Struct returned has dateCreated, userid, blogid and content 00279 00280 $blog = new BLOG($blogid); 00281 00282 $structarray = array(); // the array in which the structs will be stored 00283 00284 $query = "SELECT mname, ibody, imore, iauthor, ibody, inumber, ititle as title, itime, cname as category, iclosed" 00285 .' FROM '.sql_table('item').', '.sql_table('category').', '.sql_table('member') 00286 ." WHERE iblog=$blogid and icat=catid and iauthor=mnumber" 00287 ." ORDER BY itime DESC" 00288 ." LIMIT $amount"; 00289 $r = sql_query($query); 00290 00291 while ($row = mysql_fetch_assoc($r)) { 00292 00293 // remove linebreaks if needed 00294 if ($blog->convertBreaks()) { 00295 $row['ibody'] = removeBreaks($row['ibody']); 00296 $row['imore'] = removeBreaks($row['imore']); 00297 } 00298 00299 $newstruct = new xmlrpcval(array( 00300 "dateCreated" => new xmlrpcval(iso8601_encode(strtotime($row['itime'])),"dateTime.iso8601"), 00301 "userid" => new xmlrpcval($row['iauthor'],"string"), 00302 "blogid" => new xmlrpcval($blogid,"string"), 00303 "postid" => new xmlrpcval($row['inumber'],"string"), 00304 "description" => new xmlrpcval($row['ibody'],"string"), 00305 "title" => new xmlrpcval($row['title'],"string"), 00306 "categories" => new xmlrpcval( 00307 array( 00308 new xmlrpcval($row['category'], "string") 00309 ) 00310 ,"array"), 00311 00312 00313 "mt_text_more" => new xmlrpcval($row['imore'], "string"), 00314 "mt_allow_comments" => new xmlrpcval($row['iclosed'] ? 0 : 1, "int"), 00315 "mt_allow_pings" => new xmlrpcval(1, "int") 00316 ),'struct'); 00317 00318 //TODO: String link? 00319 //TODO: String permaLink? 00320 00321 00322 array_push($structarray, $newstruct); 00323 } 00324 00325 return new xmlrpcresp(new xmlrpcval( $structarray , "array")); 00326 } 00327 00328 function _newMediaObject($blogid, $username, $password, $info) { 00329 global $CONF, $DIR_MEDIA, $DIR_LIBS; 00330 00331 // - login 00332 $mem = new MEMBER(); 00333 if (!$mem->login($username, $password)) 00334 return _error(1,'Could not log in'); 00335 00336 // - check if team member 00337 if (!BLOG::existsID($blogid)) 00338 return _error(2,"No such blog ($blogid)"); 00339 if (!$mem->teamRights($blogid)) 00340 return _error(3,'Not a team member'); 00341 00342 $b = new BLOG($blogid); 00343 00344 // - decode data 00345 $data = $info['bits']; // decoding was done transparantly by xmlrpclib 00346 00347 // - check filesize 00348 if (strlen($data) > $CONF['MaxUploadSize']) 00349 return _error(9, 'filesize is too big'); 00350 00351 00352 // - check if filetype is allowed (check filename) 00353 $filename = $info['name']; 00354 $ok = 0; 00355 $allowedtypes = explode (',', $CONF['AllowedTypes']); 00356 foreach ( $allowedtypes as $type ) 00357 if (eregi("\." .$type. "$",$filename)) $ok = 1; 00358 if (!$ok) 00359 _error(8, 'Filetype is not allowed'); 00360 00361 // - add file to media library 00362 include_once($DIR_LIBS . 'MEDIA.php'); // media classes 00363 00364 // always use private media library of member 00365 $collection = $mem->getID(); 00366 00367 // prefix filename with current date (YYYY-MM-DD-) 00368 // this to avoid nameclashes 00369 if ($CONF['MediaPrefix']) 00370 $filename = strftime("%Y%m%d-", time()) . $filename; 00371 00372 $res = MEDIA::addMediaObjectRaw($collection, $filename, $data); 00373 if ($res) 00374 return _error(10, $res); 00375 00376 // - return URL 00377 $urlstruct = new xmlrpcval(array( 00378 "url" => new xmlrpcval($CONF['MediaURL'] . $collection. '/' . $filename,'string') 00379 ),'struct'); 00380 00381 return new xmlrpcresp($urlstruct); 00382 } 00383 00384 function _categoryList($blogid, $username, $password) { 00385 // 1. login 00386 $mem = new MEMBER(); 00387 if (!$mem->login($username, $password)) 00388 return _error(1,"Could not log in"); 00389 00390 // check if on team and blog exists 00391 if (!BLOG::existsID($blogid)) 00392 return _error(2,"No such blog ($blogid)"); 00393 if (!$mem->teamRights($blogid)) 00394 return _error(3,"Not a team member"); 00395 00396 $b = new BLOG($blogid); 00397 00398 $categorystruct = array(); 00399 00400 $query = "SELECT cname, cdesc, catid" 00401 . ' FROM '.sql_table('category') 00402 . " WHERE cblog=" . intval($blogid) 00403 . " ORDER BY cname"; 00404 $r = sql_query($query); 00405 00406 while ($obj = mysql_fetch_object($r)) { 00407 00408 $categorystruct[$obj->cname] = new xmlrpcval( 00409 array( 00410 "description" => new xmlrpcval($obj->cdesc,"string"), 00411 "htmlUrl" => new xmlrpcval($b->getURL() . "?catid=" . $obj->catid ,"string"), 00412 "rssUrl" => new xmlrpcval("","string") 00413 ) 00414 ,'struct'); 00415 } 00416 00417 00418 return new xmlrpcresp(new xmlrpcval( $categorystruct , "struct")); 00419 00420 } 00421 00422 00423 function _mw_getPost($itemid, $username, $password) { 00424 global $manager; 00425 00426 // 1. login 00427 $mem = new MEMBER(); 00428 if (!$mem->login($username, $password)) 00429 return _error(1,"Could not log in"); 00430 00431 // 2. check if allowed 00432 if (!$manager->existsItem($itemid,1,1)) 00433 return _error(6,"No such item ($itemid)"); 00434 $blogid = getBlogIDFromItemID($itemid); 00435 if (!$mem->teamRights($blogid)) 00436 return _error(3,"Not a team member"); 00437 00438 // 3. return the item 00439 $item =& $manager->getItem($itemid,1,1); // (also allow drafts and future items) 00440 00441 $b = new BLOG($blogid); 00442 if ($b->convertBreaks()) { 00443 $item['body'] = removeBreaks($item['body']); 00444 $item['more'] = removeBreaks($item['more']); 00445 } 00446 00447 $categoryname = $b->getCategoryName($item['catid']); 00448 00449 $newstruct = new xmlrpcval(array( 00450 "dateCreated" => new xmlrpcval(iso8601_encode($item['timestamp']),"dateTime.iso8601"), 00451 "userid" => new xmlrpcval($item['authorid'],"string"), 00452 "blogid" => new xmlrpcval($blogid,"string"), 00453 "postid" => new xmlrpcval($itemid, "string"), 00454 "description" => new xmlrpcval($item['body'],"string"), 00455 "title" => new xmlrpcval($item['title'],"string"), 00456 "categories" => new xmlrpcval( 00457 array( 00458 new xmlrpcval($categoryname, "string") 00459 ) 00460 ,"array"), 00461 00462 "mt_text_more" => new xmlrpcval($item['more'], "string"), 00463 "mt_allow_comments" => new xmlrpcval($item['closed'] ? 0 : 1, "int"), 00464 "mt_allow_pings" => new xmlrpcval(1, "int") 00465 ),'struct'); 00466 00467 //TODO: add "String link" to struct? 00468 //TODO: add "String permaLink" to struct? 00469 00470 return new xmlrpcresp($newstruct); 00471 00472 } 00473 00474 $functionDefs = array_merge($functionDefs, 00475 array( 00476 "metaWeblog.newPost" => 00477 array( 00478 "function" => "f_metaWeblog_newPost", 00479 "signature" => $f_metaWeblog_newPost_sig, 00480 "docstring" => $f_metaWeblog_newPost_doc 00481 ), 00482 00483 "metaWeblog.getCategories" => 00484 array( 00485 "function" => "f_metaWeblog_getCategories", 00486 "signature" => $f_metaWeblog_getCategories_sig, 00487 "docstring" => $f_metaWeblog_getCategories_doc 00488 ), 00489 00490 "metaWeblog.getPost" => 00491 array( 00492 "function" => "f_metaWeblog_getPost", 00493 "signature" => $f_metaWeblog_getPost_sig, 00494 "docstring" => $f_metaWeblog_getPost_doc 00495 ), 00496 00497 "metaWeblog.editPost" => 00498 array( 00499 "function" => "f_metaWeblog_editPost", 00500 "signature" => $f_metaWeblog_editPost_sig, 00501 "docstring" => $f_metaWeblog_editPost_doc 00502 ), 00503 00504 'metaWeblog.newMediaObject' => 00505 array( 00506 'function' => 'f_metaWeblog_newMediaObject', 00507 'signature' => $f_metaWeblog_newMediaObject_sig, 00508 'docstring' => $f_metaWeblog_newMediaObject_doc 00509 ), 00510 00511 'metaWeblog.getRecentPosts' => 00512 array( 00513 'function' => 'f_metaWeblog_getRecentPosts', 00514 'signature' => $f_metaWeblog_getRecentPosts_sig, 00515 'docstring' => $f_metaWeblog_getRecentPosts_doc 00516 ) 00517 00518 ) 00519 ); 00520 ?>