[ Index ]

PHP Cross Reference of Nucleus CMS 3.32

title

Body

[close]

/nucleus/xmlrpc/ -> api_metaweblog.inc.php (source)

   1  <?php
   2  
   3  /*
   4   * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
   5   * Copyright (C) 2002-2007 The Nucleus Group
   6   *
   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)
  12   */
  13  
  14  /**
  15   *    This file contains definitions for the methods of the metaWeblog API
  16   *
  17   * @license http://nucleuscms.org/license.txt GNU General Public License
  18   * @copyright Copyright (C) 2002-2007 The Nucleus Group
  19   * @version $Id: api_metaweblog.inc.php 1116 2007-02-03 08:24:29Z kimitake $
  20   */
  21  
  22  
  23      // metaWeblog.newPost
  24      $f_metaWeblog_newPost_sig = array(array(
  25              // return type
  26              $xmlrpcString,    // itemid of the new item
  27  
  28              // params:
  29              $xmlrpcString,    // blogid
  30              $xmlrpcString,    // username
  31              $xmlrpcString,    // password
  32              $xmlrpcStruct,    // content
  33              $xmlrpcBoolean,    // publish boolean (set to false to create draft)
  34  
  35          ));
  36      $f_metaWeblog_newPost_doc = "Adds a new item to the given blog. Adds it as a draft when publish is false";
  37  	function f_metaWeblog_newPost($m) {
  38          global $manager;
  39          
  40          $blogid =             _getScalar($m,0);
  41          $username =         _getScalar($m,1);
  42          $password =         _getScalar($m,2);
  43          $struct =             $m->getParam(3);
  44          
  45          $content =         _getStructVal($struct, 'description');
  46          $more =         _getStructVal($struct, 'mt_text_more');
  47          $title =         _getStructVal($struct, 'title');
  48  
  49          // category is optional (thus: be careful)!
  50          $catlist = $struct->structmem('categories');
  51          if ($catlist && ($catlist->kindOf() == "array") && ($catlist->arraysize() > 0))
  52              $category = _getArrayVal($catlist, 0);
  53          
  54          
  55          $comments = (int) _getStructVal($struct, 'mt_allow_comments') ? 0 : 1;
  56          $publish = _getScalar($m,4);
  57  
  58  
  59          // Add item
  60          $res = _addItem($blogid, $username, $password, $title, $content, $more, $publish, $comments, $category);
  61          
  62          // Handle trackbacks
  63          $trackbacks = array();
  64          $tblist = $struct->structmem('mt_tb_ping_urls');
  65          if ($tblist && ($tblist->kindOf() == "array") && ($tblist->arraysize() > 0)) {
  66              
  67              for ($i = 0; $i < $tblist->arraysize(); $i++) {
  68                  $trackbacks[] = _getArrayVal($tblist, $i);
  69              }
  70              
  71              $manager->notify('SendTrackback', array ('tb_id' => $itemid, 'urls' => & $trackbacks));
  72          }
  73  
  74          return $res;
  75      }
  76  
  77  
  78      // metaWeblog.getCategories
  79      $f_metaWeblog_getCategories_sig = array(array(
  80          // return
  81          $xmlrpcStruct,    // categories for blog
  82  
  83          // params
  84          $xmlrpcString,    // blogid
  85          $xmlrpcString,    // username
  86          $xmlrpcString,    // password
  87  
  88      ));
  89      $f_metaWeblog_getCategories_doc = "Returns the categories for a given blog";
  90  	function f_metaWeblog_getCategories($m) {
  91          $blogid =    _getScalar($m,0);
  92          $username =    _getScalar($m,1);
  93          $password =    _getScalar($m,2);
  94  
  95          return _categoryList($blogid, $username, $password);
  96      }
  97  
  98  
  99      // metaWeblog.getPost
 100      $f_metaWeblog_getPost_sig = array(array(
 101          // return
 102          $xmlrpcStruct,    // the juice
 103  
 104          // params
 105          $xmlrpcString,    // itemid
 106          $xmlrpcString,    // username
 107          $xmlrpcString,    // password
 108  
 109      ));
 110      $f_metaWeblog_getPost_doc = "Retrieves a post";
 111  	function f_metaWeblog_getPost($m) {
 112          $itemid =    _getScalar($m,0);
 113          $username =    _getScalar($m,1);
 114          $password =    _getScalar($m,2);
 115  
 116          return _mw_getPost($itemid, $username, $password);
 117      }
 118  
 119  
 120      // metaWeblog.editPost
 121      $f_metaWeblog_editPost_sig = array(array(
 122              // return type
 123              $xmlrpcBoolean,    // true
 124  
 125              // params:
 126              $xmlrpcString,    // itemid
 127              $xmlrpcString,    // username
 128              $xmlrpcString,    // password
 129              $xmlrpcStruct,    // content
 130              $xmlrpcBoolean,    // publish boolean (set to false to create draft)
 131  
 132          ));
 133      $f_metaWeblog_editPost_doc = "Edits an item";
 134  	function f_metaWeblog_editPost($m) {
 135          global $manager;
 136  
 137          $itemid =             _getScalar($m,0);
 138          $username =         _getScalar($m,1);
 139          $password =         _getScalar($m,2);
 140  
 141          $category = '';
 142          $struct =             $m->getParam(3);
 143              $content =         _getStructVal($struct, 'description');
 144              $title =         _getStructVal($struct, 'title');
 145  
 146              // category is optional (thus: be careful)!
 147              $catlist = $struct->structmem('categories');
 148              if ($catlist && ($catlist->kindOf() == "array") && ($catlist->arraysize() > 0)) {
 149                  $category = _getArrayVal($catlist, 0);
 150              }
 151  
 152          $publish = _getScalar($m,4);
 153  
 154          
 155          // get old title and extended part
 156          if (!$manager->existsItem($itemid,1,1))
 157              return _error(6,"No such item ($itemid)");
 158          $blogid = getBlogIDFromItemID($itemid);
 159  
 160          $old =& $manager->getItem($itemid,1,1);
 161  
 162          if ($category == '')
 163          {
 164              // leave category unchanged when not present
 165              $catid = $old['catid'];
 166          }
 167          else
 168          {
 169              $blog = new BLOG($blogid);
 170              $catid = $blog->getCategoryIdFromName($category);
 171          }
 172  
 173          if ($old['draft'] && $publish) {
 174              $wasdraft = 1;
 175              $publish = 1;
 176          } else {
 177              $wasdraft = 0;
 178          }
 179  
 180          $more = $struct->structmem('mt_text_more');
 181          if ($more) {
 182              $more = _getStructVal($struct, 'mt_text_more');
 183          } else {
 184              $more = $old['more'];
 185          }
 186          
 187          $comments = $struct->structmem('mt_allow_comments');
 188          if ($comments) {
 189              $comments = (int) _getStructVal($struct, 'mt_allow_comments') ? 0 : 1;
 190          } else {
 191              $comments = $old['closed'];
 192          }
 193  
 194          $res = _edititem($itemid, $username, $password, $catid, $title, $content, $more, $wasdraft, $publish, $comments);
 195  
 196          // Handle trackbacks
 197          $trackbacks = array();
 198          $tblist = $struct->structmem('mt_tb_ping_urls');
 199          if ($tblist && ($tblist->kindOf() == "array") && ($tblist->arraysize() > 0)) {
 200              
 201              for ($i = 0; $i < $tblist->arraysize(); $i++) {
 202                  $trackbacks[] = _getArrayVal($tblist, $i);
 203              }
 204              
 205              $manager->notify('SendTrackback', array ('tb_id' => $itemid, 'urls' => & $trackbacks));
 206          }
 207  
 208          return $res;
 209      }
 210  
 211      // metaWeblog.newMediaObject
 212      $f_metaWeblog_newMediaObject_sig = array(array(
 213          //  return type
 214          $xmlrpcStruct,        // "url" element
 215  
 216          // params
 217          $xmlrpcString,        // blogid
 218          $xmlrpcString,        // username
 219          $xmlrpcString,        // password
 220          $xmlrpcStruct        // 'name', 'type' and 'bits'
 221      ));
 222      $f_metaWeblog_newMediaObject_doc = 'Uploads a file to to the media library of the user';
 223  	function f_metaWeblog_newMediaObject($m) {
 224          $blogid        = _getScalar($m, 0);
 225          $username    = _getScalar($m, 1);
 226          $password     = _getScalar($m, 2);
 227  
 228          $struct        = $m->getParam(3);
 229              $name    = _getStructVal($struct, 'name');
 230              $type    = _getStructVal($struct, 'type');
 231              $bits    = _getStructVal($struct, 'bits');
 232  
 233          return _newMediaObject($blogid, $username, $password, array('name' => $name, 'type' => $type, 'bits' => $bits));
 234      }
 235  
 236      // metaWeblog.getRecentPosts
 237      $f_metaWeblog_getRecentPosts_sig = array(array(
 238          // return type
 239          $xmlrpcStruct,        // array of structs
 240  
 241          // params
 242          $xmlrpcString,        // blogid
 243          $xmlrpcString,        // username
 244          $xmlrpcString,        // password
 245          $xmlrpcInt            // number of posts
 246      ));
 247      $f_metaWeblog_getRecentPosts_doc = 'Returns recent weblog items.';
 248  	function f_metaWeblog_getRecentPosts($m) {
 249          $blogid        = _getScalar($m, 0);
 250          $username    = _getScalar($m, 1);
 251          $password     = _getScalar($m, 2);
 252          $amount        = intval(_getScalar($m, 3));
 253  
 254          return _getRecentItemsMetaWeblog($blogid, $username, $password, $amount);
 255      }
 256  
 257  	function _getRecentItemsMetaWeblog($blogid, $username, $password, $amount) {
 258  
 259          $blogid = intval($blogid);
 260          $amount = intval($amount);
 261  
 262          // 1. login
 263          $mem = new MEMBER();
 264          if (!$mem->login($username, $password))
 265              return _error(1,"Could not log in");
 266  
 267          // 2. check if allowed
 268          if (!BLOG::existsID($blogid))
 269              return _error(2,"No such blog ($blogid)");
 270          if (!$mem->teamRights($blogid))
 271              return _error(3,"Not a team member");
 272          $amount = intval($amount);
 273          if (($amount < 1) or ($amount > 20))
 274              return _error(5,"Amount parameter must be in range 1..20");
 275  
 276          // 3. create and return list of recent items
 277          // Struct returned has dateCreated, userid, blogid and content
 278  
 279          $blog = new BLOG($blogid);
 280  
 281          $structarray = array();        // the array in which the structs will be stored
 282  
 283          $query = "SELECT mname, ibody, imore, iauthor, ibody, inumber, ititle as title, itime, cname as category, iclosed"
 284                 .' FROM '.sql_table('item').', '.sql_table('category').', '.sql_table('member')
 285                 ." WHERE iblog=$blogid and icat=catid and iauthor=mnumber"
 286                 ." ORDER BY itime DESC"
 287                 ." LIMIT $amount";
 288          $r = sql_query($query);
 289  
 290          while ($row = mysql_fetch_assoc($r)) {
 291  
 292              // remove linebreaks if needed
 293              if ($blog->convertBreaks()) {
 294                  $row['ibody'] = removeBreaks($row['ibody']);
 295                  $row['imore'] = removeBreaks($row['imore']);
 296              }
 297  
 298              $newstruct = new xmlrpcval(array(
 299                  "dateCreated" => new xmlrpcval(iso8601_encode(strtotime($row['itime'])),"dateTime.iso8601"),
 300                  "userid" => new xmlrpcval($row['iauthor'],"string"),
 301                  "blogid" => new xmlrpcval($blogid,"string"),
 302                  "postid" => new xmlrpcval($row['inumber'],"string"),
 303                  "description" => new xmlrpcval($row['ibody'],"string"),
 304                  "title" => new xmlrpcval($row['title'],"string"),
 305                  "categories" => new xmlrpcval(
 306                          array(
 307                              new xmlrpcval($row['category'], "string")
 308                          )
 309                          ,"array"),
 310                  
 311                          
 312                  "mt_text_more"         => new xmlrpcval($row['imore'], "string"),
 313                  "mt_allow_comments" => new xmlrpcval($row['iclosed'] ? 0 : 1, "int"),
 314                  "mt_allow_pings"     => new xmlrpcval(1, "int")
 315              ),'struct');
 316  
 317          //TODO: String link?
 318          //TODO: String permaLink?
 319  
 320  
 321              array_push($structarray, $newstruct);
 322          }
 323  
 324          return new xmlrpcresp(new xmlrpcval( $structarray , "array"));
 325      }
 326  
 327  	function _newMediaObject($blogid, $username, $password, $info) {
 328          global $CONF, $DIR_MEDIA, $DIR_LIBS;
 329  
 330          // - login
 331          $mem = new MEMBER();
 332          if (!$mem->login($username, $password))
 333              return _error(1,'Could not log in');
 334  
 335          // - check if team member
 336          if (!BLOG::existsID($blogid))
 337              return _error(2,"No such blog ($blogid)");
 338          if (!$mem->teamRights($blogid))
 339              return _error(3,'Not a team member');
 340  
 341          $b = new BLOG($blogid);
 342  
 343          // - decode data
 344          $data = $info['bits']; // decoding was done transparantly by xmlrpclib
 345  
 346          // - check filesize
 347          if (strlen($data) > $CONF['MaxUploadSize'])
 348              return _error(9, 'filesize is too big');
 349  
 350  
 351          // - check if filetype is allowed (check filename)
 352          $filename = $info['name'];
 353          $ok = 0;
 354          $allowedtypes = explode (',', $CONF['AllowedTypes']);
 355          foreach ( $allowedtypes as $type )
 356              if (eregi("\." .$type. "$",$filename)) $ok = 1;
 357          if (!$ok)
 358              _error(8, 'Filetype is not allowed');
 359  
 360          // - add file to media library
 361          include_once($DIR_LIBS . 'MEDIA.php');    // media classes
 362  
 363          // always use private media library of member
 364          $collection = $mem->getID();
 365  
 366          // prefix filename with current date (YYYY-MM-DD-)
 367          // this to avoid nameclashes
 368          if ($CONF['MediaPrefix'])
 369              $filename = strftime("%Y%m%d-", time()) . $filename;
 370  
 371          $res = MEDIA::addMediaObjectRaw($collection, $filename, $data);
 372          if ($res)
 373              return _error(10, $res);
 374  
 375          // - return URL
 376          $urlstruct = new xmlrpcval(array(
 377              "url" => new xmlrpcval($CONF['MediaURL'] . $collection. '/' . $filename,'string')
 378          ),'struct');
 379  
 380          return new xmlrpcresp($urlstruct);
 381      }
 382  
 383  	function _categoryList($blogid, $username, $password) {
 384          // 1. login
 385          $mem = new MEMBER();
 386          if (!$mem->login($username, $password))
 387              return _error(1,"Could not log in");
 388  
 389          // check if on team and blog exists
 390          if (!BLOG::existsID($blogid))
 391              return _error(2,"No such blog ($blogid)");
 392          if (!$mem->teamRights($blogid))
 393              return _error(3,"Not a team member");
 394  
 395          $b = new BLOG($blogid);
 396  
 397          $categorystruct = array();
 398  
 399          $query =  "SELECT cname, cdesc, catid"
 400                  . ' FROM '.sql_table('category')
 401                  . " WHERE cblog=" . intval($blogid)
 402                  . " ORDER BY cname";
 403          $r = sql_query($query);
 404  
 405          while ($obj = mysql_fetch_object($r)) {
 406  
 407              $categorystruct[$obj->cname] = new xmlrpcval(
 408                  array(
 409                      "description" => new xmlrpcval($obj->cdesc,"string"),
 410                      "htmlUrl" => new xmlrpcval($b->getURL() . "?catid=" . $obj->catid ,"string"),
 411                      "rssUrl" => new xmlrpcval("","string")
 412                  )
 413              ,'struct');
 414          }
 415  
 416  
 417          return new xmlrpcresp(new xmlrpcval( $categorystruct , "struct"));
 418  
 419      }
 420  
 421  
 422  	function _mw_getPost($itemid, $username, $password) {
 423          global $manager;
 424  
 425          // 1. login
 426          $mem = new MEMBER();
 427          if (!$mem->login($username, $password))
 428              return _error(1,"Could not log in");
 429  
 430          // 2. check if allowed
 431          if (!$manager->existsItem($itemid,1,1))
 432              return _error(6,"No such item ($itemid)");
 433          $blogid = getBlogIDFromItemID($itemid);
 434          if (!$mem->teamRights($blogid))
 435              return _error(3,"Not a team member");
 436  
 437          // 3. return the item
 438          $item =& $manager->getItem($itemid,1,1); // (also allow drafts and future items)
 439  
 440          $b = new BLOG($blogid);
 441          if ($b->convertBreaks()) {
 442              $item['body'] = removeBreaks($item['body']);
 443              $item['more'] = removeBreaks($item['more']);
 444          }
 445  
 446          $categoryname = $b->getCategoryName($item['catid']);
 447  
 448          $newstruct = new xmlrpcval(array(
 449              "dateCreated" => new xmlrpcval(iso8601_encode($item['timestamp']),"dateTime.iso8601"),
 450              "userid" => new xmlrpcval($item['authorid'],"string"),
 451              "blogid" => new xmlrpcval($blogid,"string"),
 452