api_blogger.inc.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
00004  * Copyright (C) 2002-2007 The Nucleus Group
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  * (see nucleus/documentation/index.html#license for more info)
00011  */
00012 
00023         // blogger.newPost
00024         $f_blogger_newPost_sig = array(array(
00025                         // return type
00026                         $xmlrpcString,  // itemid of the new item
00027 
00028                         // params:
00029                         $xmlrpcString,  // appkey (ignored)
00030                         $xmlrpcString,  // blogid
00031                         $xmlrpcString,  // username
00032                         $xmlrpcString,  // password
00033                         $xmlrpcString,  // content
00034                         $xmlrpcBoolean, // publish boolean (set to false to create draft)
00035 
00036                 ));
00037         $f_blogger_newPost_doc = "Adds a new item to the given blog. Adds it as a draft when publish is false";
00038         function f_blogger_newPost($m) {
00039                 $blogid = _getScalar($m,1);
00040                 $username = _getScalar($m,2);
00041                 $password = _getScalar($m,3);
00042                 $content = _getScalar($m,4);
00043                 $publish = _getScalar($m,5);
00044 
00045                 $title = blogger_extractTitle($content);
00046                 $category = blogger_extractCategory($content);
00047                 $content = blogger_removeSpecialTags($content);
00048 
00049                 return _addItem($blogid, $username, $password, $title, $content, '', $publish, 0, $category);
00050         }
00051 
00052         // blogger.editPost
00053         $f_blogger_editPost_sig = array(array(
00054                         // return type
00055                         $xmlrpcBoolean, // true or false
00056 
00057                         // params:
00058                         $xmlrpcString,  // appkey (ignored)
00059                         $xmlrpcString,  // postid
00060                         $xmlrpcString,  // username
00061                         $xmlrpcString,  // password
00062                         $xmlrpcString,  // content
00063                         $xmlrpcBoolean, // publish boolean (only considered when dealing with a draft)
00064 
00065                 ));
00066         $f_blogger_editPost_doc = "Edits an item of a blog";
00067         function f_blogger_editPost($m) {
00068                 global $manager;
00069 
00070                 $itemid = intval(_getScalar($m,1));
00071                 $username = _getScalar($m,2);
00072                 $password = _getScalar($m,3);
00073                 $content = _getScalar($m,4);
00074                 $publish = _getScalar($m,5);
00075 
00076                 $title = blogger_extractTitle($content);
00077                 $category = blogger_extractCategory($content);
00078                 $content = blogger_removeSpecialTags($content);
00079 
00080                 // get old title and extended part
00081                 if (!$manager->existsItem($itemid,1,1))
00082                         return _error(6,"No such item ($itemid)");
00083                 $old =& $manager->getItem($itemid,1,1);
00084 
00085                 $blogid = getBlogIDFromItemID($itemid);
00086 
00087                 $blog = new BLOG($blogid);
00088                 $catid = $blog->getCategoryIdFromName($category);
00089 
00090                 if ($old['draft'] && $publish) {
00091                         $wasdraft = 1;
00092                         $publish = 1;
00093                 } else {
00094                         $wasdraft = 0;
00095                 }
00096 
00097                 return _edititem($itemid, $username, $password, $catid, $title, $content, $old['more'], $wasdraft, $publish, $old['closed']);
00098         }
00099 
00100 
00101         // blogger.getUsersBlogs
00102         $f_blogger_getUsersBlogs_sig = array(array(
00103                         // return type
00104                         $xmlrpcArray,   // array containing structs containing blog info
00105 
00106                         // params:
00107                         $xmlrpcString,  // appkey (ignored)
00108                         $xmlrpcString,  // username
00109                         $xmlrpcString,  // password
00110                 ));
00111         $f_blogger_getUsersBlogs_doc = "Returns a list of all the blogs where the given member is on the team";
00112         function f_blogger_getUsersBlogs($m) {
00113                 $username = _getScalar($m,1);
00114                 $password = _getScalar($m,2);
00115 
00116                 return _getUsersBlogs($username, $password);
00117         }
00118 
00119         // blogger.getRecentPosts
00120         $f_blogger_getRecentPosts_sig = array(array(
00121                         // return type
00122                         $xmlrpcArray,   // array of strucs (representing items)
00123 
00124                         // params
00125                         $xmlrpcString,  // appkey (ignored)
00126                         $xmlrpcString,  // blogid
00127                         $xmlrpcString,  // username
00128                         $xmlrpcString,  // password
00129                         $xmlrpcInt,     // amount of items to return (max = 20)
00130                 ));
00131         $f_blogger_getRecentPosts_doc = "Returns a maximum of 20 recent items";
00132         function f_blogger_getRecentPosts($m) {
00133                 $blogid = _getScalar($m, 1);
00134                 $username = _getScalar($m, 2);
00135                 $password = _getScalar($m, 3);
00136                 $amount = _getScalar($m, 4);
00137 
00138                 return _getRecentItemsBlogger($blogid, $username, $password, $amount);
00139         }
00140 
00141 
00142         // blogger.getPost
00143         $f_blogger_getPost_sig = array(array(
00144                         // return type
00145                         $xmlrpcStruct,  // A struct representing the item
00146 
00147                         // params
00148                         $xmlrpcString,  // appkey (ignored)
00149                         $xmlrpcString,  // postid
00150                         $xmlrpcString,  // username
00151                         $xmlrpcString,  // password
00152                 ));
00153         $f_blogger_getPost_doc = "Returns an item (only the item body!)";
00154         function f_blogger_getPost($m) {
00155                 $postid = _getScalar($m, 1);
00156                 $username = _getScalar($m, 2);
00157                 $password = _getScalar($m, 3);
00158 
00159                 return _getItemBlogger($postid, $username, $password);
00160         }
00161 
00162 
00163         // blogger.deletePost
00164         $f_blogger_deletePost_sig = array(array(
00165                         // return type
00166                         $xmlrpcBoolean, // boolean (ok or not ok)
00167 
00168                         // params
00169                         $xmlrpcString,  // appkey (ignored)
00170                         $xmlrpcString,  // postid
00171                         $xmlrpcString,  // username
00172                         $xmlrpcString,  // password
00173                         $xmlrpcBoolean, // publish (ignored)
00174                 ));
00175         $f_blogger_deletePost_doc = "Deletes an item";
00176         function f_blogger_deletePost($m) {
00177                 $postid = _getScalar($m,1);
00178                 $username = _getScalar($m, 2);
00179                 $password = _getScalar($m, 3);
00180 
00181                 return _deleteItem($postid, $username, $password);
00182         }
00183 
00184         // blogger.getTemplate
00185         $f_blogger_getTemplate_sig = array(array(
00186                         // return type
00187                         $xmlrpcString,  // the template
00188 
00189                         // params
00190                         $xmlrpcString,  // appkey (ignored)
00191                         $xmlrpcString,  // blogid
00192                         $xmlrpcString,  // username
00193                         $xmlrpcString,  // password
00194                         $xmlrpcString,  // type of template (main/archiveIndex)
00195                                 ));
00196         $f_blogger_getTemplate_doc = "Returns the required part of the default skin for the given blog";
00197         function f_blogger_getTemplate($m) {
00198                 $blogid = _getScalar($m,1);
00199                 $username = _getScalar($m,2);
00200                 $password = _getScalar($m,3);
00201                 $type = _getScalar($m,4);
00202 
00203                 switch($type) {
00204                         case "main":
00205                                 $type = "index";
00206                                 break;
00207                         case "archiveIndex":
00208                                 $type = "archivelist";
00209                                 break;
00210                 }
00211 
00212                 return _getSkinPart($blogid, $username, $password, $type);
00213         }
00214 
00215         // blogger.setTemplate
00216         $f_blogger_setTemplate_sig = array(array(
00217                         // return type
00218                         $xmlrpcBoolean, // OK or not OK
00219 
00220                         // params
00221                         $xmlrpcString,  // appkey (ignored)
00222                         $xmlrpcString,  // blogid
00223                         $xmlrpcString,  // username
00224                         $xmlrpcString,  // password
00225                         $xmlrpcString,  // template contents
00226                         $xmlrpcString,  // type of template (main/archiveIndex)
00227                         ));
00228         $f_blogger_setTemplate_doc = "Changes a part of the default skin for the selected blog";
00229         function f_blogger_setTemplate($m) {
00230                 $blogid = _getScalar($m,1);
00231                 $username = _getScalar($m,2);
00232                 $password = _getScalar($m,3);
00233                 $content = _getScalar($m,4);
00234                 $type = _getScalar($m,5);
00235 
00236                 switch($type) {
00237                         case "main":
00238                                 $type = "index";
00239                                 break;
00240                         case "archiveIndex":
00241                                 $type = "archivelist";
00242                                 break;
00243                 }
00244 
00245                 return _setSkinPart($blogid, $username, $password, $content, $type);
00246         }
00247 
00248         // blogger.getUserInfo
00249         $f_blogger_getUserInfo_sig = array(array(
00250                         // return type
00251                         $xmlrpcStruct,  // Struct
00252 
00253                         // params
00254                         $xmlrpcString,  // appkey (ignored)
00255                         $xmlrpcString,  // username
00256                         $xmlrpcString,  // password
00257                         ));
00258         $f_blogger_getUserInfo_doc = "Returns info on the user";
00259         function f_blogger_getUserInfo($m) {
00260                 $username = _getScalar($m,1);
00261                 $password = _getScalar($m,2);
00262 
00263                 return _getUserInfo($username, $password);
00264         }
00265 
00266 
00270         function _getRecentItemsBlogger($blogid, $username, $password, $amount) {
00271 
00272                 $blogid = intval($blogid);
00273                 $amount = intval($amount);
00274 
00275                 // 1. login
00276                 $mem = new MEMBER();
00277                 if (!$mem->login($username, $password))
00278                         return _error(1,"Could not log in");
00279 
00280                 // 2. check if allowed
00281                 if (!BLOG::existsID($blogid))
00282                         return _error(2,"No such blog ($blogid)");
00283                 if (!$mem->teamRights($blogid))
00284                         return _error(3,"Not a team member");
00285                 $amount = intval($amount);
00286                 if (($amount < 1) or ($amount > 20))
00287                         return _error(5,"Amount parameter must be in range 1..20");
00288 
00289                 // 3. create and return list of recent items
00290                 // Struct returned has dateCreated, userid, blogid and content
00291 
00292                 $blog = new BLOG($blogid);
00293 
00294                 $structarray = array();         // the array in which the structs will be stored
00295 
00296                 $query = "SELECT mname, ibody, iauthor, ibody, inumber, ititle as title, itime, cname as category"
00297                            .' FROM '.sql_table('item').', '.sql_table('category').', '.sql_table('member')
00298                            ." WHERE iblog=$blogid and icat=catid and iauthor=mnumber"
00299                            ." ORDER BY itime DESC"
00300                            ." LIMIT $amount";
00301                 $r = sql_query($query);
00302 
00303                 while ($row = mysql_fetch_assoc($r)) {
00304 
00305                         // remove linebreaks if needed
00306                         if ($blog->convertBreaks())
00307                                 $row['ibody'] = removeBreaks($row['ibody']);
00308 
00309                         $content = blogger_specialTags($row) . $row['ibody'];
00310 
00311                         $newstruct = new xmlrpcval(array(
00312                                 "userid" => new xmlrpcval($row['iauthor'],"string"),
00313                                 "dateCreated" => new xmlrpcval(iso8601_encode(strtotime($row['itime'])),"dateTime.iso8601"),
00314                                 "blogid" => new xmlrpcval($blogid,"string"),
00315                                 "content" => new xmlrpcval($content,"string"),
00316                                 "postid" => new xmlrpcval($row['inumber'],"string"),
00317                                 "authorName" => new xmlrpcval($row['mname'],'string'),
00318                                 "title" => new xmlrpcval($row['title'],'string'),
00319                         ),'struct');
00320                         array_push($structarray, $newstruct);
00321                 }
00322 
00323                 return new xmlrpcresp(new xmlrpcval( $structarray , "array"));
00324 
00325         }
00326 
00330         function _getItemBlogger($itemid, $username, $password) {
00331                 global $manager;
00332 
00333                 // 1. login
00334                 $mem = new MEMBER();
00335                 if (!$mem->login($username, $password))
00336                         return _error(1,"Could not log in");
00337 
00338                 // 2. check if allowed
00339                 if (!$manager->existsItem($itemid,1,1))
00340                         return _error(6,"No such item ($itemid)");
00341                 $blogid = getBlogIDFromItemID($itemid);
00342                 if (!$mem->teamRights($blogid))
00343                         return _error(3,"Not a team member");
00344 
00345                 // 3. return the item
00346                 // Structure returned has dateCreated, userid, blogid and content
00347 
00348                 $item =& $manager->getItem($itemid,1,1); // (also allow drafts and future items)
00349                 $blog = new BLOG($blogid);
00350 
00351                 // get category
00352                 $item['category'] = $blog->getCategoryName($item['catid']);
00353 
00354                 // remove linebreaks if needed
00355                 if ($blog->convertBreaks())
00356                         $item['body'] = removeBreaks($item['body']);
00357 
00358                 $content = blogger_specialTags($item) . $item['body'];
00359 
00360                 $newstruct = new xmlrpcval(array(
00361                         "dateCreated" => new xmlrpcval(iso8601_encode($item['timestamp']),"dateTime.iso8601"),
00362                         "userid" => new xmlrpcval($item['authorid'],"string"),
00363                         "blogid" => new xmlrpcval($blogid,"string"),
00364                         "content" => new xmlrpcval($content,"string")
00365                 ),'struct');
00366 
00367                 return new xmlrpcresp($newstruct);
00368 
00369 
00370         }
00371 
00372 
00373         function blogger_extractTitle($body) {
00374                 return blogger_matchTag('title',$body);
00375         }
00376 
00377         function blogger_extractCategory($body) {
00378                 return blogger_matchTag('category',$body);
00379         }
00380 
00381         function blogger_matchTag($tag, $body) {
00382                 if (preg_match("/<" . $tag .">(.+?)<\/".$tag.">/is",$body,$match))
00383                         return $match[1];
00384                 else
00385                         return "";
00386         }
00387 
00388         function blogger_removeSpecialTags($body) {
00389                 $body = preg_replace("/<title>(.+?)<\/title>/","",$body);
00390                 $body = preg_replace("/<category>(.+?)<\/category>/","",$body);
00391                 return trim($body);
00392         }
00393 
00394         function blogger_specialTags($item) {
00395                 $result = "<title>". $item['title']."</title>";
00396                 $result .= "<category>".$item['category']."</category>";
00397                 return $result;
00398         }
00399 
00400 
00401 
00402         $functionDefs = array_merge($functionDefs,
00403                 array(
00404                          "blogger.getUsersBlogs" =>
00405                          array( "function" => "f_blogger_getUsersBlogs",
00406                                 "signature" => $f_blogger_getUsersBlogs_sig,
00407                                 "docstring" => $f_blogger_getUsersBlogs_doc),
00408 
00409                          "blogger.newPost" =>
00410                          array( "function" => "f_blogger_newPost",
00411                                 "signature" => $f_blogger_newPost_sig,
00412                                 "docstring" => $f_blogger_newPost_doc),
00413 
00414                          "blogger.editPost" =>
00415                          array( "function" => "f_blogger_editPost",
00416                                 "signature" => $f_blogger_editPost_sig,
00417                                 "docstring" => $f_blogger_editPost_doc),
00418 
00419                          "blogger.deletePost" =>
00420                          array( "function" => "f_blogger_deletePost",
00421                                 "signature" => $f_blogger_deletePost_sig,
00422                                 "docstring" => $f_blogger_deletePost_doc),
00423 
00424                          "blogger.getPost" =>
00425                          array( "function" => "f_blogger_getPost",
00426                                 "signature" => $f_blogger_getPost_sig,
00427                                 "docstring" => $f_blogger_getPost_doc),
00428 
00429                          "blogger.getRecentPosts" =>
00430                          array( "function" => "f_blogger_getRecentPosts",
00431                                 "signature" => $f_blogger_getRecentPosts_sig,
00432                                 "docstring" => $f_blogger_getRecentPosts_doc),
00433 
00434                          "blogger.getUserInfo" =>
00435                          array( "function" => "f_blogger_getUserInfo",
00436                                 "signature" => $f_blogger_getUserInfo_sig,
00437                                 "docstring" => $f_blogger_getUserInfo_doc),
00438 
00439                          "blogger.getTemplate" =>
00440                          array( "function" => "f_blogger_getTemplate",
00441                                 "signature" => $f_blogger_getTemplate_sig,
00442                                 "docstring" => $f_blogger_getTemplate_doc),
00443 
00444                          "blogger.setTemplate" =>
00445                          array( "function" => "f_blogger_setTemplate",
00446                                 "signature" => $f_blogger_setTemplate_sig,
00447                                 "docstring" => $f_blogger_setTemplate_doc)
00448 
00449                 )
00450         );
00451 
00452 
00453 ?>



Generated on Wed Jun 25 17:25:57 2008 by  doxygen 1.5.5