[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/net/xmlrpc/ -> xmlrpcserver.class.php (source)

   1  <?php
   2  
   3      lt_include( PLOG_CLASS_PATH."class/net/xmlrpc/IXR_Library.lib.php" );
   4      lt_include( PLOG_CLASS_PATH."class/config/config.class.php" );
   5      lt_include( PLOG_CLASS_PATH."class/database/db.class.php" );
   6      lt_include( PLOG_CLASS_PATH."class/dao/users.class.php");
   7      lt_include( PLOG_CLASS_PATH."class/dao/article.class.php");
   8      lt_include( PLOG_CLASS_PATH."class/dao/articles.class.php");
   9      lt_include( PLOG_CLASS_PATH."class/dao/articlecategories.class.php");
  10      lt_include( PLOG_CLASS_PATH."class/dao/users.class.php" );
  11      lt_include( PLOG_CLASS_PATH."class/dao/blogs.class.php" );
  12      lt_include( PLOG_CLASS_PATH."class/template/cachecontrol.class.php" );
  13      lt_include( PLOG_CLASS_PATH."class/gallery/dao/galleryalbums.class.php" );
  14      lt_include( PLOG_CLASS_PATH."class/gallery/dao/galleryresources.class.php" );
  15      lt_include( PLOG_CLASS_PATH."class/plugin/pluginmanager.class.php" );
  16          
  17      if( !defined( "ADMIN_PERMISSION" )) 
  18          define( "ADMIN_PERMISSION", 1 );
  19      if( !defined( "BLOG_PERMISSION" )) 
  20          define( "BLOG_PERMISSION", 2 );    
  21  
  22      class XmlRpcServer extends IXR_Server
  23      {
  24  		function XmlRpcServer()
  25          {
  26              $config =& Config::getConfig();
  27              if ($config->getValue("xmlrpc_api_enabled"))
  28              {
  29                  $this->IXR_Server(
  30                      array (
  31                          "blogger.newPost"           => "this:newPost",
  32                          "blogger.getPost"           => "this:getPost",
  33                          "blogger.editPost"          => "this:editPost",
  34                          "blogger.deletePost"        => "this:deletePost",
  35                          "blogger.getRecentPosts"    => "this:getRecentPosts",
  36                          "blogger.getUserInfo"       => "this:getUserInfo",
  37                          "blogger.getUsersBlogs"     => "this:getUsersBlogs",
  38                          "metaWeblog.newPost"        => "this:metaWeblogNewPost",
  39                          "metaWeblog.editPost"       => "this:metaWeblogEditPost",
  40                          "metaWeblog.getPost"        => "this:metaWeblogGetPost",
  41                          "metaWeblog.getRecentPosts" => "this:metaWeblogGetRecentPosts",
  42                          "metaWeblog.getCategories"  => "this:metaWeblogGetCategories",
  43                          "metaWeblog.newMediaObject" => "this:metaWeblogNewMediaObject",    
  44                          "metaWeblog.getUsersBlogs"  => "this:getUsersBlogs",
  45                          "mt.getCategoryList"        => "this:mtGetCategoryList",
  46                          "mt.supportedTextFilters"   => "this:mtSupportedTextFilters", 
  47                          "mt.getPostCategories"      => "this:mtGetPostCategories",
  48                          "mt.setPostCategories"      => "this:mtSetPostCategories"
  49                          ));
  50              } 
  51              else {
  52                      // xmlrpc_api disabled, no methods configured
  53                  $this->IXR_Server( Array ());
  54              }            
  55          }
  56      
  57  		function newPost( $args )
  58          {
  59              $users = new Users();
  60              $articles = new Articles();
  61              $category = new ArticleCategories();
  62              $blogsG = new Blogs();
  63          
  64              $appkey     = $args[0];
  65              $blogid     = $args[1];
  66              $username   = $args[2];
  67              $password   = $args[3];
  68              $content    = $args[4];
  69              $publish    = $args[5]; // true post&publish | false post only
  70                  /*
  71               int postid
  72                  */
  73  
  74                  // -mhe todo security
  75  
  76              $userInfo = $users->getUserInfo( $username, $password );
  77  
  78              if (!$userInfo) {
  79                  return new IXR_Error(-1, 'You did not provide the correct username and/or password');
  80              }
  81  
  82              if(!$blogId){
  83                  $blogs = $userInfo->getBlogs();
  84                  if(!$blogs){
  85                      return new IXR_Error(-1, "This user doesn't have access to any blogs.");
  86                  }
  87                  $blogid = $blogs[0]->getId();
  88              }
  89  
  90              $blogInfo = $blogsG->getBlogInfo( $blogid );
  91              if( !$blogInfo ) {
  92                  return new IXR_Error(-1, 'Error loading blog' );
  93              }
  94              
  95                  // check this user's permissions before proceeding
  96              if( !$this->userHasPermission( $userInfo, $blogInfo, "add_post" )) {
  97                  return new IXR_Error(-1, 'This user does not have enough permissions' );
  98              }        
  99              
 100              if ($publish) {
 101                  $status = POST_STATUS_PUBLISHED;
 102              } 
 103              else {
 104                  $status = POST_STATUS_DRAFT;
 105              }
 106              
 107                  // Get the default category
 108              $cats = $category->getBlogCategories($blogid);
 109              
 110                  // some protection again blogs without categories
 111              if( !$cats ) {                
 112                  return new IXR_Error(-1, 'This blog does not have any categories!');
 113              }
 114              
 115              foreach($cats as $cat) {
 116                  $idCategory = $cat->getId();
 117                      // Stop here, we have a category
 118                  break;
 119              }
 120  
 121                  // ecto sends the topic as <title>blah blah</title>rest of the body
 122              if( preg_match( "/<title>(.*)<\/title>(.*)/i", $content, $matches )) {
 123                  $title = $matches[1];
 124                  $content = $matches[2];
 125              }
 126              else {
 127                  $dummy = explode("\n", $content);
 128                  if( count( $dummy ) == 1 ) {
 129                      $title = substr( $content, 0, 60 );
 130                  }
 131                  else {
 132                      $title = $dummy[0];
 133                      unset($dummy[0]);
 134                      $content = implode("\n", $dummy);
 135                      unset($dummy);
 136                  }
 137              }
 138              
 139              $article = new Article(
 140                  $title,
 141                  $content, // text
 142                  Array( $idCategory ), // catid
 143                  $userInfo->getId(), // userid
 144                  $blogid, // blogid
 145                  $status,
 146                  0, // numread
 147                  Array( "comments_enabled" => true ) // enable comments
 148                  );
 149              
 150              $article->setDate(date("YmdHis"));
 151              
 152                  // Get the plugin manager
 153              $plugMgr =& PluginManager::getPluginManager();
 154              $plugMgr->setBlogInfo( $blogInfo);
 155              $plugMgr->setUserInfo( $userInfo );
 156              $plugMgr->loadPlugins();
 157                  // Send the PRE_POST_POST_ADD message
 158              $plugMgr->notifyEvent( EVENT_PRE_POST_ADD, Array( "article" => &$article ));
 159              
 160              $postid = $articles->addArticle($article);
 161              
 162              if ($postid == 0) {
 163                  return new IXR_Error(-1, 'Internal error occurred while creating your post!');
 164              }
 165                  // The post was successful
 166                  // Send the EVENT_POST_POST_ADD messages to the plugins
 167              $plugMgr->notifyEvent( EVENT_POST_POST_ADD, Array( "article" => &$article ));
 168              CacheControl::resetBlogCache( $blogid );
 169              
 170              $blogSettings = $blogInfo->getSettings();
 171              
 172                  // Add article notifcations if this is specified by the default setting.
 173              if ($blogSettings->getValue( "default_send_notification" ))
 174              {
 175                  lt_include( PLOG_CLASS_PATH."class/dao/articlenotifications.class.php" );
 176                  
 177                  $artNotifications = new ArticleNotifications();
 178                  $artNotifications->addNotification( $postid, $blogid, $userInfo->getId());
 179              }
 180              
 181              $this->setResponseCharset( $blogInfo );
 182              
 183              return sprintf( "%d", $postid );
 184          }
 185          
 186          function metaWeblogNewPost($args)
 187          {
 188              $users = new Users();
 189              $articles = new Articles();
 190              $category = new ArticleCategories();
 191              $blogsG = new Blogs();
 192  
 193              $blogid     = $args[0];
 194              $username   = $args[1];
 195              $password   = $args[2];
 196              $content    = $args[3];
 197              $publish    = $args[4]; // true post&publish | false post only
 198                  /*
 199               int postid
 200                  */
 201      
 202              $userInfo = $users->getUserInfo( $username, $password);
 203      
 204              if( !$userInfo ) {
 205                  return new IXR_Error(-1, 'You did not provide the correct username and/or password');
 206              }    
 207              if ($publish) {
 208                  $status = POST_STATUS_PUBLISHED;
 209              } 
 210              else {
 211                  $status = POST_STATUS_DRAFT;
 212              }
 213  
 214              $blogInfo = $blogsG->getBlogInfo( $blogid );
 215              if( !$this->userHasPermission( $userInfo, $blogInfo, "add_post" )) {
 216                  return new IXR_Error(-1, 'This user does not have enough permissions' );
 217              }
 218                  
 219              $title = $content["title"];
 220                  
 221                  // Check to see if the MovableType extnensions have been added
 222              $mt_excerpt = "";
 223              $mt_text_more = "";
 224              $mt_allow_comments = "";
 225              if( isset( $content["mt_excerpt"] ))
 226                  $mt_excerpt = $content["mt_excerpt"]; 
 227              if( isset( $content["mt_text_more"] ))
 228                  $mt_text_more = $content["mt_text_more"]; 
 229              if( isset( $content["mt_allow_comments"] ))
 230                  $mt_allow_comments = $content["mt_allow_comments"]; 
 231                  
 232              if ( $mt_text_more != NULL && trim($mt_text_more != ""))
 233              {
 234                  $body = $content["description"] . POST_EXTENDED_TEXT_MODIFIER . $mt_text_more;
 235              }
 236              else
 237              {
 238                  $body = $content["description"];
 239              }
 240              $catList = NULL;
 241              if( isset( $content["categories"] ))
 242                  $catList = $content["categories"];
 243  
 244              $categoryName = NULL;
 245      
 246                  //
 247                  // :KLUDGE:
 248                  // not exactly the smartest and fastest bit of code ever but it seems to work :-)
 249                  //
 250              $categories = Array();
 251              $cats = $category->getBlogCategories($blogid);            
 252      
 253                  // some protection again blogs without categories
 254              if( !$cats ) {
 255                  return new IXR_Error(-1, 'This blog does not have any categories!');
 256              }
 257      
 258              if ( $catList != NULL )
 259              {
 260                  foreach( $catList as $categoryName ) {
 261                      foreach( $cats as $blogCategory ) {
 262                          $categoryName = trim($categoryName);
 263                          if ( strcmp( $categoryName, $blogCategory->getName()) == 0 )
 264                          {
 265                              $categories[] = $blogCategory->getId();
 266                          }
 267                      }
 268                  }
 269              }
 270              else {
 271                      // if no category, let's pick a random one
 272                  $blogCategory = array_pop( $cats );
 273                  $categories[] = $blogCategory->getId();
 274              }
 275      
 276              $userInfo = $users->getUserInfoFromUsername( $username );
 277                  
 278                  // Initially assume that comments are enabled
 279              $enableComments = true;
 280                  
 281                  // Was a setting specified in the MovableType fields?
 282              if ($mt_allow_comments != NULL)
 283              {
 284                  $enableComments = $mt_allow_comments;
 285              }
 286                  
 287                  
 288              $article = new Article(
 289                  $title,
 290                  $body, // text
 291                  $categories, // catid
 292                  $userInfo->getId(), // userid
 293                  $blogid, // blogid
 294                  $status,
 295                  0, // numread
 296                  Array( "comments_enabled" => $enableComments ) 
 297                  );
 298      
 299              $dateCreated = NULL;
 300              if( isset( $content['dateCreated'] ))
 301                  $dateCreated = $content['dateCreated'];
 302                 
 303                  // there must be a bug in the xmlrpc library, we're getting an object in $dateCreated
 304                  // that does not have a type or anyhting, but it still is an object... kinda weird. Anyway,
 305                  // clients like ecto do not allow to change the time an article is posted so this is not 
 306                  // too annoying, *yet*
 307              if (!empty($dateCreated))
 308              {
 309                      // Convert the UTC time to local time, since articleDate is in local time
 310                  $ar = localtime ( $dateCreated->getTimestamp() );
 311                  $ar[5] += 1900; $ar[4]++;
 312                  $localTimeStamp = gmmktime ( $ar[2], $ar[1], $ar[0], $ar[4], $ar[3], $ar[5], $ar[8] );
 313                  $articleDate = date("YmdHis", $localTimeStamp);
 314              } else
 315              {
 316                  $articleDate = date("YmdHis");
 317              }
 318                  
 319              $article->setDate($articleDate);
 320                  
 321                  // Get the plugin manager
 322              $plugMgr =& PluginManager::getPluginManager();
 323              $plugMgr->setBlogInfo( $blogInfo );
 324              $plugMgr->setUserInfo( $userInfo );
 325              $plugMgr->loadPlugins();
 326                  // Send the PRE_POST_POST_ADD message
 327              $plugMgr->notifyEvent( EVENT_PRE_POST_ADD, Array( "article" => &$article ));            
 328                  
 329              $postid = $articles->addArticle($article);
 330              if ($postid == 0){
 331                  return new IXR_Error(-1, 'Internal error occurred while creating your post!');
 332              }
 333                  // The post was successful
 334                      
 335                  // Send the EVENT_POST_POST_ADD messages to the plugins
 336              $plugMgr->notifyEvent( EVENT_POST_POST_ADD, Array( "article" => &$article ));
 337                      
 338              CacheControl::resetBlogCache( $blogid );
 339                      
 340              $blogSettings = $blogInfo->getSettings();
 341                      
 342                  // Add article notifcations if this is specified by the default setting.
 343              if ($blogSettings->getValue( "default_send_notification" ))
 344              {
 345                  lt_include( PLOG_CLASS_PATH."class/dao/articlenotifications.class.php" );
 346      
 347                  $artNotifications = new ArticleNotifications();
 348                  $artNotifications->addNotification( $postid, $blogid, $userInfo->getId());
 349              }
 350  
 351              $this->setResponseCharset( $blogInfo );
 352                     
 353              return sprintf( "%d", $postid );
 354          }
 355  
 356      
 357              /**
 358               * @private
 359               * sets the character set for responses
 360               */
 361  		function setResponseCharset( $blog )
 362          {
 363              $locale = $blog->getLocale();
 364              $this->defencoding = $locale->getCharset();
 365          }
 366              
 367  
 368              /** 
 369               * NOTE: this method does not perform permission checking since if it did,
 370               * it would be impossible to post: no categories would be available if the
 371               * view_categories is not available. This is in line with the browser-based UI,
 372               * there it is not necessary to have this permission in order to post new articles,
 373               * only add_post is needed
 374               */
 375  	    function metaWeblogGetCategories($args)
 376          {
 377              $users = new Users();
 378              $category = new ArticleCategories();
 379              $blogsG = new Blogs();
 380      
 381              $blogid     = $args[0];
 382              $username   = $args[1];
 383              $password   = $args[2];
 384  
 385              $auth = $users->authenticateUser( $username, $password );
 386  
 387              if (!$auth){
 388                  return new IXR_Error(-1, 'You did not provide the correct username and/or password');
 389              }
 390              $blogInfo = $blogsG->getBlogInfo( $blogid );
 391              if( !$blogInfo ) {
 392                  return new IXR_Error(-1, 'Incorrect blog id');
 393              }
 394              
 395              $cats = $category->getBlogCategories($blogid);
 396              $url = $blogInfo->getBlogRequestGenerator();
 397              $ret = array();    
 398              foreach($cats as $cat)
 399              {
 400                  $dummy                   = array();
 401                  $dummy["description"]    = $cat->getDescription();
 402                  
 403                      // disable the generation of xhtml content or else the IXR_XMLRPC library will
 404                      // escape things twice!
 405                  $url->setXHTML( false );
 406                  
 407                  $dummy["htmlUrl"]        = $url->categoryLink( $cat );
 408                  $dummy["rssUrl"]         = "http://";
 409                  $ret[$cat->getName()]    = $dummy;
 410              }
 411              
 412              $this->setResponseCharset( $blogInfo );
 413              
 414              return $ret;
 415          }
 416  
 417  	    function mtGetCategoryList($args)
 418          {
 419              $users = new Users();
 420              $category = new ArticleCategories();
 421              $blogsG = new Blogs();
 422      
 423              $blogid     = $args[0];
 424              $username   = $args[1];
 425              $password   = $args[2];
 426  
 427              $auth = $users->authenticateUser( $username, $password );
 428  
 429              if (!$auth){
 430                  return new IXR_Error(-1, 'You did not provide the correct username and/or password');
 431              }
 432              $blogInfo = $blogsG->getBlogInfo( $blogid );
 433              if( !$blogInfo ) {
 434                  return new IXR_Error(-1, 'Incorrect blog id');
 435              }
 436              
 437              $cats = $category->getBlogCategories($blogid);
 438              $url = $blogInfo->getBlogRequestGenerator();
 439              $ret = array();    
 440              foreach($cats as $cat)
 441              {
 442                  $dummy                   = array();
 443                  $dummy["categoryId"]     = $cat->getId();
 444                  $dummy["categoryName"]   = $cat->getName();
 445                  
 446                  $ret[]                   = $dummy;
 447              }
 448              
 449              $this->setResponseCharset( $blogInfo );
 450              
 451              return $ret;
 452          }
 453  
 454  	    function getPost($args)
 455          {
 456              lt_include( PLOG_CLASS_PATH."class/data/timestamp.class.php" );        
 457          
 458              $users = new Users();
 459              $articles = new Articles();
 460      
 461              $appkey     = $args[0];
 462              $postid     = $args[1];
 463              $username   = $args[2];
 464              $password   = $args[3];
 465  
 466              /*
 467                  "userid" =>
 468                  "dateCreated" =>
 469                  "content" =>
 470                  "postid" =>
 471              */
 472  
 473              $userInfo = $users->getUserInfo($username,$password);
 474              if( !$userInfo ) {
 475                  return new IXR_Error(-1, 'You did not provide the correct username and/or password');
 476              }                
 477              $item = $articles->getBlogArticle($postid,
 478                                                -1, // blogId
 479                                                true, // includeHiddenFields
 480                                                -1, // date
 481                                                -1, // categoryId
 482                                                $userInfo->getId());
 483              $dateObject = $item->getDateObject();
 484                  // Get the unix time stamp 
 485              $time = $dateObject->getTimestamp(DATE_FORMAT_UNIXTIME);
 486              
 487              $dummy                  = array();
 488              $userInfo               = $item->getUserInfo();
 489              $dummy["userid"]        = $userInfo->getId();
 490              $dummy["dateCreated"]   = new IXR_Date($time);
 491              $dummy["content"]       = $item->getTopic() . "\r\n" . $item->getText(false) . " ";
 492              $dummy["postid"]        = $item->getId();
 493              
 494              $blogInfo = $item->getBlogInfo();
 495              
 496                  // check the permissions
 497              if( !$this->userHasPermission( $userInfo, $blogInfo, "view_posts" )) {
 498                  return new IXR_Error(-1, 'This user does not have enough permissions' );
 499              }                
 500              
 501              $this->setResponseCharset( $blogInfo );
 502              
 503              return $dummy;
 504          }
 505  
 506  	    function metaWeblogGetPost($args)
 507          {
 508              $users = new Users();
 509              $articles = new Articles();
 510      
 511              $postid     = $args[0];
 512              $username   = $args[1];
 513              $password   = $args[2];
 514  
 515              $userInfo = $users->getUserInfo( $username, $password );
 516  
 517              if( !$userInfo ){
 518                  return new IXR_Error(-1, 'You did not provide the correct username and/or password');
 519              }
 520  
 521              lt_include( PLOG_CLASS_PATH."class/data/timestamp.class.php" );
 522              
 523              $item = $articles->getBlogArticle($postid,
 524                                                -1, // blogId
 525                                                true, // includeHiddenFields
 526                                                -1, // date
 527                                                -1, // categoryId
 528                                                $userInfo->getId());
 529              
 530                  // check if the article is valid
 531              if( !$item ) {
 532                  return( new IXR_Error(-1, 'The article is not valid' ));
 533              }
 534              
 535                  // check permissions
 536              $blogInfo = $item->getBlogInfo();                
 537              if( !$this->userHasPermission( $userInfo, $blogInfo, "view_posts" )) {
 538                  return( new IXR_Error(-1, 'This user does not have enough permissions' ));
 539              }    
 540              
 541              $dateObject = $item->getDateObject();
 542                  // Get the unix time stamp 
 543              $time = $dateObject->getTimestamp(DATE_FORMAT_UNIXTIME);
 544              
 545              $articleCat = $item->getCategory();
 546              
 547              $blogId = $item->getBlog();
 548              $blogs = new Blogs();
 549              $url = $blogInfo->getBlogRequestGenerator();
 550              
 551              $dummy                  = array();
 552              $userInfo               = $item->getUserInfo();
 553              $dummy["userid"]        = $userInfo->getId();
 554              $dummy["dateCreated"]   = new IXR_Date($time);
 555              $dummy["title"]         = $item->getTopic();
 556              
 557              $blogSettings = $blogInfo->getSettings();
 558              
 559              $dummy["description"]   = $item->getIntroText(); 
 560              
 561              $dummy["postid"]        = $item->getId();
 562              
 563              $dummy["link"]          = $url->postLink( $item );
 564              $dummy["permaLink"]     = $url->postPermalink( $item );
 565  
 566              $catArray               = array();
 567              foreach( $item->getCategories() as $category ) {
 568                  $catArray[]             = $category->getName();
 569              }
 570              $dummy["categories"]      = $catArray;
 571  
 572                  // The MovableType Extensions
 573              $dummy["mt_text_more"]       = $item->getExtendedText(); 
 574              $dummy["mt_allow_comments"]  = $item->getCommentsEnabled(); 
 575                  
 576      
 577              $this->setResponseCharset( $blogInfo );
 578      
 579              return $dummy;
 580          }
 581  
 582  	    function editPost($args)
 583          {
 584              $users = new Users();
 585              $articles = new Articles();
 586              $blogsG = new Blogs();
 587  
 588              $appkey     = $args[0];
 589              $postid     = $args[1];
 590              $username   = $args[2];
 591              $password   = $args[3];
 592              $content    = $args[4];
 593              $publish    = $args[5];
 594  
 595              /*
 596                  boolean, true or false
 597              */
 598  
 599              $userInfo = $users->getUserInfo( $username, $password );
 600              if( !$userInfo ) {
 601                  return new IXR_Error(-1, 'You did not provide the correct username and/or password');
 602              }
 603  
 604                  // fake topic
 605              $dummy = explode("\n", $content);
 606              if( count($dummy) == 1 ) {
 607                  $title = substr( $content, 0, 60 );
 608              }
 609              else {
 610                  $title = $dummy[0];
 611                  unset($dummy[0]);
 612                  $content = implode("\n", $dummy);
 613                  unset($dummy);
 614              }
 615  
 616              $article = $articles->getBlogArticle($postid,
 617                                                   -1, // blogId
 618                                                   true, // includeHiddenFields
 619                                                   -1, // date
 620                                                   -1, // categoryId
 621                                                   $userInfo->getId());
 622      
 623              if( !$article ) {
 624                  return( new IXR_Error(-1, 'The article id is not correct' ));
 625              }
 626                  
 627              $blogInfo = $article->getBlogInfo();                
 628                  
 629                  // check the permissions
 630              if( !$this->userHasPermission( $userInfo, $blogInfo, "update_post" )) {
 631                  return new IXR_Error(-1, 'This user does not have enough permissions' );
 632              }                
 633      
 634              if ($publish) {
 635                  $status = POST_STATUS_PUBLISHED;
 636              } 
 637              else {
 638                  $status = POST_STATUS_DRAFT;
 639              }
 640      
 641              $article->setText($content);
 642              $article->setTopic($title);
 643              $article->setStatus($status);
 644  
 645                  // Get the plugin manager
 646              $plugMgr =& PluginManager::getPluginManager();
 647              $plugMgr->setBlogInfo( $blogInfo );
 648              $plugMgr->setUserInfo( $userInfo );
 649              $plugMgr->loadPlugins();
 650                  // Send the EVENT_PRE_POST_UPDATE message
 651              $plugMgr->notifyEvent( EVENT_PRE_POST_UPDATE, Array( "article" => &$article ));            
 652  
 653              $articles->updateArticle($article);
 654  
 655                  // Send the EVENT_POST_POST_UPDATE messages to the plugins
 656              $plugMgr->notifyEvent( EVENT_POST_POST_UPDATE, Array( "article" => &$article ));                
 657  
 658              CacheControl::resetBlogCache( $blogInfo->getId());
 659  
 660              $this->setResponseCharset( $blogInfo );
 661  
 662              return true;
 663          }
 664  
 665  	    function metaWeblogEditPost($args)
 666          {
 667              $users = new Users();
 668              $articles = new Articles();
 669              $category = new ArticleCategories();
 670  
 671              $postid     = $args[0];
 672              $username   = $args[1];
 673              $password   = $args[2];
 674              $content    = $args[3];
 675              $publish    = $args[4];
 676  
 677              /*
 678                  boolean, true or false
 679              */
 680  
 681              $userInfo = $users->getUserInfo( $username, $password );
 682              if( !$userInfo ) {
 683                  return new IXR_Error(-1, 'You did not provide the correct username and/or password');
 684              }
 685              if ($publish) {
 686                  $status = POST_STATUS_PUBLISHED;
 687              } 
 688              else {
 689                  $status = POST_STATUS_DRAFT;
 690              }            
 691  
 692              $title = $content["title"];
 693                  
 694                  // Check to see if the MovableType extnensions have been added
 695              $mt_excerpt = $content["mt_excerpt"]; 
 696              $mt_text_more = $content["mt_text_more"]; 
 697              $mt_allow_comments = $content["mt_allow_comments"]; 
 698                  
 699              if ( $mt_text_more != NULL && trim($mt_text_more) != "") {
 700                  $body = $content["description"] . POST_EXTENDED_TEXT_MODIFIER . $mt_text_more;
 701              }
 702              else {
 703                  $body = $content["description"];
 704              }
 705  
 706              $article = $articles->getBlogArticle($postid,
 707                                                   -1, // blogId
 708                                                   true, // includeHiddenFields
 709                                                   -1, // date
 710                                                   -1, // categoryId
 711                                                   $userInfo->getId());
 712      
 713                  // check that the article is valid
 714              if( !$article ) {
 715                  return( new IXR_Error(-1, 'Incorrect article id' ));                    
 716              }
 717                  
 718                  // see that the user can update articles
 719              $blogid = $article->getBlog();
 720              $blogInfo = $article->getBlogInfo();
 721              if( !$this->userHasPermission( $userInfo, $blogInfo, "update_post" )) {
 722                  return( new IXR_Error(-1, 'This user does not have enough permissions' ));
 723              }
 724  
 725              $catList = NULL;
 726              if ( array_key_exists( "categories",  $content ) ) {
 727                  $catList = $content["categories"];
 728              }
 729                  //
 730                  // :KLUDGE:
 731                  // not exactly the smartest and fastest bit of code ever but it seems to work :-)
 732                  //
 733              $categories = Array();
 734              $cats = $category->getBlogCategories($blogid);
 735              if ( $catList != NULL )
 736              {
 737                  foreach( $catList as $categoryName ) {
 738                      foreach( $cats as $blogCategory ) {
 739                          $categoryName = trim($categoryName);
 740                          if ( strcmp( $categoryName, $blogCategory->getName()) == 0 )
 741                          {
 742                              $categories[] = $blogCategory->getId();
 743                          }
 744                      }
 745                  }
 746                  $article->setCategoryIds( $categories );
 747              }
 748              else if ( count($article->getCategories()) == 0) {
 749                      // Only assign a new category if there isn't one   
 750                      
 751                      // if no category, let's pick a random one
 752                  $blogCategory = array_pop( $cats );
 753                  $categories[] = $blogCategory->getId();
 754                      
 755                  $article->setCategoryIds( $categories );
 756              }
 757  
 758              $article->setText($body);
 759              $article->setTopic($title);
 760              $article->setStatus($status);
 761  
 762                  // Get the plugin manager
 763              $plugMgr =& PluginManager::getPluginManager();
 764              $plugMgr->setBlogInfo( $blogInfo );
 765              $plugMgr->setUserInfo( $userInfo );
 766              $plugMgr->loadPlugins();
 767                  // Send the EVENT_PRE_POST_UPDATE message
 768              $plugMgr->notifyEvent( EVENT_PRE_POST_UPDATE, Array( "article" => &$article ));            
 769  
 770              $articles->updateArticle($article);
 771  
 772                  // Send the EVENT_POST_POST_UPDATE messages to the plugins
 773              $plugMgr->notifyEvent( EVENT_POST_POST_UPDATE, Array( "article" => &$article ));                
 774  
 775              CacheControl::resetBlogCache( $blogid );            
 776      
 777              $this->setResponseCharset( $blogInfo );
 778  
 779              return true;
 780          }
 781  
 782  	    function deletePost($args)
 783          {
 784              $users = new Users();
 785              $articles = new Articles();
 786              $blogsG = new Blogs();
 787  
 788              $appkey     = $args[0];
 789              $postid     = $args[1];
 790              $username   = $args[2];
 791              $password   = $args[3];
 792              $publish    = $args[4];
 793  
 794              $userInfo = $users->getUserInfo( $username, $password );
 795              if( !$userInfo ) {
 796                  return new IXR_Error(-1, 'You did not provide the correct username and/or password');
 797              }
 798              $article = $articles->getBlogArticle($postid,
 799                                                   -1, // blogId
 800                                                   true, // includeHiddenFields
 801                                                   -1, // date
 802                                                   -1, // categoryId
 803                                                   $userInfo->getId());
 804      
 805                  // check if the article that was pulled is valid at all
 806              if( !$article ) {
 807                  return( new IXR_Error(-1, 'The article id is not correct' ));
 808              }
 809                  
 810                  // check the permissions
 811              $blogInfo = $article->getBlogInfo();
 812              if( !$this->userHasPermission( $userInfo, $blogInfo, "update_post" )) {
 813                  return( new IXR_Error(-1, 'This user does not have enough permissions' ));
 814              }                
 815  
 816                  // Get the plugin manager
 817              $plugMgr =& PluginManager::getPluginManager();
 818              $plugMgr->setBlogInfo( $blogInfo );
 819              $plugMgr->setUserInfo( $userInfo );
 820              $plugMgr->loadPlugins();
 821                  // Send the EVENT_PRE_POST_DELETE message
 822              $plugMgr->notifyEvent( EVENT_PRE_POST_DELETE, Array( "article" => &$article ));            
 823  
 824              $articles->deleteArticle(
 825                  $postid,
 826                  $userInfo->getId(), // userid
 827                  $article->getBlog()
 828                  );
 829  
 830                  // Send the EVENT_POST_POST_DELETE messages to the plugins
 831              $plugMgr->notifyEvent( EVENT_POST_POST_DELETE, Array( "article" => &$article ));                
 832  
 833              CacheControl::resetBlogCache( $blogInfo->getId());
 834  
 835              $this->setResponseCharset( $blogInfo );
 836  
 837              return true;
 838          }
 839  
 840  	    function getRecentPosts($args)
 841          {
 842              $users = new Users();
 843              $articles = new Articles();
 844              $blogs = new Blogs();
 845      
 846              /*
 847                  "userid" =>
 848                  "dateCreated" =>
 849                  "content" =>
 850                  "postid" =>
 851              */
 852              $appkey     = $args[0];
 853              $blogid     = $args[1];
 854              $username   = $args[2];
 855              $password   = $args[3];
 856              $number     = $args[4];
 857  
 858              $userInfo = $users->getUserInfo($username,$password);
 859              if( !$userInfo ){
 860                  return new IXR_Error(-1, 'You did not provide the correct username and/or password');
 861              }
 862              $blogInfo = $blogs->getBlogInfo( $blogid );
 863              if( !$blogInfo ) {
 864                  return new IXR_Error(-1, 'Incorrect blog id');                    
 865              }
 866                  
 867                  // check this user's permissions
 868              if( !$this->userHasPermission( $userInfo, $blogInfo, "view_posts" )) {
 869                  return new IXR_Error(-1, 'This user does not have enough permissions' );
 870              }
 871              
 872              $ret = array();
 873              $list = $articles->getBlogArticles(
 874                  $blogid,
 875                  -1, // date
 876                  $number,  // amount
 877                  -1  // any category id
 878                  );
 879      
 880              foreach( $list as $item ) {
 881                  $dateObject = $item->getDateObject();
 882                  lt_include( PLOG_CLASS_PATH."class/data/timestamp.class.php" );
 883                      // Get the unix time stamp 
 884                  $time = $dateObject->getTimestamp(DATE_FORMAT_UNIXTIME);
 885  
 886                  $dummy                  = array();
 887                  $userInfo               = $item->getUserInfo();
 888                  $dummy["userid"]        = $userInfo->getId();
 889                  $dummy["dateCreated"]   = new IXR_Date($time);
 890                  $dummy["content"]       = $item->getTopic() . "\r\n" . $item->getText(false) . " ";
 891                  $dummy["postid"]        = $item->getId();
 892  
 893                  $ret[]                  = $dummy;
 894              }
 895      
 896              $this->setResponseCharset( $blogInfo );
 897      
 898              return $ret;
 899          }
 900  
 901  	    function metaWeblogGetRecentPosts($args)
 902          {
 903              $users = new Users();
 904              $articles = new Articles();
 905  
 906              $blogid     = $args[0];
 907              $username   = $args[1];
 908              $password   = $args[2];
 909              $number     = $args[3];
 910  
 911              $userInfo = $users->getUserInfo( $username, $password );
 912  
 913              if( !$userInfo ) {
 914                  return new IXR_Error(-1, 'You did not provide the correct username and/or password');
 915              }
 916              $ret = array();
 917              $list = $articles->getBlogArticles(
 918                  $blogid,  
 919                  -1,  // date
 920                  $number, // number of articles
 921                  -1  // category id
 922                  );
 923  
 924              $blogs = new Blogs();
 925              $blogInfo = $blogs->getBlogInfo( $blogid );
 926      
 927                  // check if the blog is valid
 928              if( !$blogInfo ) {
 929                  return new IXR_Error(-1, 'The blog identifier is not valid' );
 930              }
 931                  
 932                  // check this user's permissions
 933              if( !$this->userHasPermission( $userInfo, $blogInfo, "view_posts" )) {
 934                  return new IXR_Error(-1, 'This user does not have enough permissions' );
 935              }                
 936      
 937              $url = $blogInfo->getBlogRequestGenerator();
 938  
 939              $blogSettings = $blogInfo->getSettings();
 940  
 941              foreach($list as $item)
 942              {
 943                  $dateObject = $item->getDateObject();
 944                  lt_include( PLOG_CLASS_PATH."class/data/timestamp.class.php" );
 945                      // Get the unix time stamp 
 946                  $time = $dateObject->getTimestamp( DATE_FORMAT_UNIXTIME );
 947  
 948                  $articleCat = $item->getCategory();
 949  
 950                  $dummy                  = array();
 951                  $userInfo               = $item->getUserInfo();
 952                  $dummy["userid"]        = $userInfo->getId();
 953                  $dummy["dateCreated"]   = new IXR_Date($time);
 954                  $dummy["title"]         = $item->getTopic(); 
 955  
 956                  $dummy["description"]   = $item->getIntroText(); 
 957                  $dummy["postid"]        = $item->getId();
 958  
 959                  $dummy["link"]          = $url->postLink( $item );
 960                  $dummy["permaLink"]     = $url->postPermalink( $item );
 961  
 962                  $catArray               = array();
 963                  foreach( $item->getCategories() as $category ) {
 964                      $catArray[]             = $category->getName();
 965                  }
 966                  $dummy["categories"]      = $catArray;
 967  
 968                      // The MovableType Extensions
 969                  $dummy["mt_text_more"]       = $item->getExtendedText(); 
 970                  $dummy["mt_allow_comments"]  = $item->getCommentsEnabled(); 
 971                  
 972      
 973                  $this->setResponseCharset( $blogInfo );
 974  
 975                  $ret[]                  = $dummy;
 976              }
 977              return $ret;
 978          }
 979  
 980  	    function metaWeblogNewMediaObject($args)
 981          {
 982              $users = new Users();
 983              $articles = new Articles();
 984              $blogsG = new Blogs();
 985  
 986              $blogid     = $args[0];
 987              $username   = $args[1];
 988              $password   = $args[2];
 989              $file       = $args[3];
 990  
 991              $userInfo = $users->getUserInfo( $username, $password );
 992              if( !$userInfo ) {
 993                  return new IXR_Error(-1, 'You did not provide the correct username and/or password');
 994              }        
 995                  // check if the blog id is valid
 996              $blogInfo = $blogsG->getBlogInfo( $blogid );
 997              if( !$blogInfo ) {
 998                  return new IXR_Error(-1, 'The blog id is not valid' );
 999              }
1000                  
1001                  // and now check if the user has enough access to upload resources
1002              if( !$this->userHasPermission( $userInfo, $blogInfo, "add_resource" )) {
1003                  return new IXR_Error(-1, 'This user does not have enough permissions' );
1004              }
1005          
1006                  // Save this file to the tmp directory
1007  
1008                  // Create a temp file
1009                  // Get the temp directory
1010                  /**if (!$tmp_dir = get_cfg_var('upload_tmp_dir')) {
1011                      $tmp_dir = dirname(tempnam('/tmp', ''));
1012                  }*/
1013              $config =& Config::getConfig();
1014              $tmp_dir = $config->getTempFolder();
1015  
1016                  // Remove all characters that would need to be urlencoded
1017                  // This may not be necessary, but this was causing problems when given file
1018                  // names with spaces in it.
1019              $tempFile = ereg_replace("[^a-zA-Z0-9._-]", "_", basename($file['name']));
1020                  // Make the tmp name
1021              $tempFile = $tmp_dir . '/' . $tempFile;
1022  
1023                  // Open the file
1024              if (!$handle = fopen( $tempFile, "wb" ) ) {
1025                  return new IXR_Error(-1, 'Could not open temp file');
1026              }    
1027  
1028                  // It appears that the data has already been decoded, no need to call base64_decode
1029              $decodedBits = $file['bits'];
1030                  // Write the data to the file
1031              if ( fwrite( $handle, $decodedBits ) === false ) {
1032                  return new IXR_Error(-1, 'Could not write to temp file');
1033              }
1034  
1035                  // Close the file
1036              fclose($handle);
1037  
1038                  // let the gallery library do its work...
1039              $resources = new GalleryResources();
1040  
1041                  // Get the first album for this blog
1042              $albums = new GalleryAlbums();
1043                  // get the list of albums for this blog
1044              $albumArray = $albums->getUserAlbums( $blogid );
1045              if ( $albumArray == NULL || count( $albumArray ) == 0 ) {
1046                  return new IXR_Error(-1, 'Could not find album');
1047              }
1048  
1049                  // Add the resource to the first album
1050              $resId = $resources->addResourceFromDisk( $blogid, $albumArray[0]->getId(),
1051                                                        basename($file['name']), $tempFile );
1052                  // Get the resource from the id
1053              $resource = $resources->getResource( $resId, $blogid, $albumArray[0]->getId() );
1054                  // Now we need to get the url for the resource
1055              $url = $blogInfo->getBlogRequestGenerator();
1056  
1057              $responseStruct               = array();
1058  
1059              $responseStruct['url'] = $url->resourceDownloadLink( $resource );
1060      
1061              $this->setResponseCharset( $blogInfo );
1062  
1063              return $responseStruct;
1064          }    
1065  
1066  	    function getUserInfo($args)
1067          {
1068              $appkeyp    = $args[0];
1069              $username   = $args[1];
1070              $password   = $args[2];
1071  
1072              $users = new Users();
1073  
1074              $userInfo = $users->getUserInfo( $username, $password );
1075  
1076              if (!$userInfo){
1077                  return new IXR_Error(-1, 'You did not provide the correct username and/or password');
1078              }
1079  
1080              $ret                = array();
1081              $ret["nickname"]    = $userInfo->getUsername();
1082              $ret["firstname"]   = $userInfo->getUsername();
1083              $ret["lastname"]    = "";
1084              $ret["email"]       = $userInfo->getEmail();
1085              $ret["userid"]      = $userInfo->getId();
1086              $ret["url"]         = "";
1087  
1088                  // set the response encoding according to one of the blogs owned by this user
1089              $userBlogs = $users->getUsersBlogs( $userInfo->getId(), BLOG_STATUS_ACTIVE );
1090              if( count($userBlogs) > 0 ) {
1091                  $blogInfo = array_pop( $userBlogs );
1092                  $this->setResponseCharset( $blogInfo );                    
1093              }
1094  
1095              return $ret;
1096          }
1097  
1098  	    function getUsersBlogs($args)
1099          {
1100              $users = new Users();
1101              $category = new ArticleCategories();
1102  
1103              $appkey     = $args[0];
1104              $username   = $args[1];
1105              $password   = $args[2];
1106                  /*
1107                  "blogName" =>
1108                  "url" =>
1109                  "blogid" =>
1110                  */
1111  
1112              $userInfo = $users->getUserInfo( $username, $password );
1113  
1114              if (!$userInfo){
1115                  return new IXR_Error(-1, 'You did not provide the correct username and/or password');
1116              }
1117              $blogs = $users->getUsersBlogs($userInfo->getId(), BLOG_STATUS_ACTIVE );
1118              $ret = array();
1119              foreach($blogs as $blog)
1120              {
1121                  $dummy              = array();
1122                  $dummy["blogid"]    = $blog->_id;
1123                  $dummy["blogName"]  = $blog->_blog;
1124                  $url = $blog->getBlogRequestGenerator();
1125                  $dummy["url"]       = $url->blogLink();
1126                  $ret[]              = $dummy;
1127              }
1128  
1129                  // set the encoding as long as we've got at least one blog
1130              if( count( $blogs ) > 0 ) {
1131                  $blogInfo = $blogs[0];
1132                  $this->setResponseCharset( $blogInfo );
1133              }
1134  
1135              return $ret;
1136          }    
1137          
1138  	    function mtSupportedTextFilters($args)
1139          {
1140              $ret = array();
1141              return $ret;
1142          }
1143          
1144  	    function mtGetPostCategories($args)
1145          {
1146              $users = new Users();
1147              $articles = new Articles();
1148      
1149              $postid     = $args[0];
1150              $username   = $args[1];
1151              $password   = $args[2];
1152  
1153              $userInfo = $users->getUserInfo( $username, $password );
1154  
1155              if( !$userInfo ){
1156                  return new IXR_Error(-1, 'You did not provide the correct username and/or password');
1157              }
1158  
1159              lt_include( PLOG_CLASS_PATH."class/data/timestamp.class.php" );
1160  
1161              $item = $articles->getBlogArticle($postid,
1162                                                -1, // blogId
1163                                                true, // includeHiddenFields
1164                                                -1, // date
1165                                                -1, // categoryId
1166                                                $userInfo->getId());
1167      
1168                  // check if the article is valid
1169              if( !$item ) {
1170                  return( new IXR_Error(-1, 'The article id is not valid' ));
1171              }
1172                  
1173                  // and permissions
1174              $blogInfo = $item->getBlogInfo();
1175              if( !$this->userHasPermission( $userInfo, $blogInfo, "view_posts" )) {
1176                  return new IXR_Error(-1, 'This user does not have enough permissions' );
1177              }
1178  
1179              $catArray               = array();
1180              foreach( $item->getCategories() as $category ) {
1181                  $dummy                   = array();
1182                  $dummy["categoryId"]     = $category->getId();
1183                  $dummy["categoryName"]   = $category->getName();
1184  
1185                  $catArray[]              = $dummy;
1186  
1187              }
1188      
1189              $this->setResponseCharset( $blogInfo );
1190      
1191              return $catArray;
1192          }
1193      
1194          function mtSetPostCategories($args)
1195          {
1196              $users = new Users();
1197              $articles = new Articles();
1198      
1199              $postid     = $args[0];
1200              $username   = $args[1];
1201              $password   = $args[2];
1202              $categories = $args[3];
1203  
1204              $userInfo = $users->getUserInfo( $username, $password );
1205  
1206              if( !$userInfo ) {
1207                  return new IXR_Error(-1, 'You did not provide the correct username and/or password');
1208              }
1209              lt_include( PLOG_CLASS_PATH."class/data/timestamp.class.php" );
1210  
1211              $article = $articles->getBlogArticle($postid,
1212                                                   -1, // blogId
1213                                                   true, // includeHiddenFields
1214                                                   -1, // date
1215                                                   -1, // categoryId
1216                                                   $userInfo->getId());
1217                  
1218                  // check that the article is valid
1219              if( !$article ) {
1220                  return( new IXR_Error(-1, 'The article id is not correct' ));
1221              }
1222  
1223      
1224                  // check the permissions
1225              $blogId = $article->getBlog();
1226              $blogInfo = $article->getBlogInfo();    
1227              if( !$this->userHasPermission( $userInfo, $blogInfo, "update_post" )) {
1228                  return new IXR_Error(-1, 'This user does not have enough permissions' );
1229              }    
1230  
1231              $articleCategories = new ArticleCategories();
1232  
1233  
1234              $catArray      = Array();
1235  
1236              if ( $categories != NULL )
1237              {
1238                  foreach( $categories as $category ) {
1239                          // Get the category object for the category
1240                      $catArray[] = $category["categoryId"];
1241                  }
1242              }
1243                  
1244              $article->setCategoryIds($catArray);
1245      
1246                  // Get the plugin manager
1247              $plugMgr =& PluginManager::getPluginManager();
1248              $plugMgr->setBlogInfo( $blogInfo );
1249              $plugMgr->setUserInfo( $userInfo );
1250              $plugMgr->loadPlugins();
1251                  // Send the EVENT_PRE_POST_UPDATE message
1252              $plugMgr->notifyEvent( EVENT_PRE_POST_UPDATE, Array( "article" => &$article ));            
1253  
1254              $articles->updateArticle($article);
1255  
1256                  // Send the EVENT_POST_POST_UPDATE messages to the plugins
1257              $plugMgr->notifyEvent( EVENT_POST_POST_UPDATE, Array( "article" => &$article ));                
1258  
1259              CacheControl::resetBlogCache( $blogId );            
1260      
1261              $this->setResponseCharset( $blogInfo );
1262      
1263              return true;
1264          }
1265      
1266          /**
1267           * Extra helper method to check permissions
1268           *
1269           * @param user A UserInfo object
1270           * @param blog A BlogInfo object
1271           * @param permName Name of the permission
1272           * @param mode Either BLOG_PERMISSION or ADMIN_PERMISSION, depending on whether
1273           * we're checking the user's permissions in this blog or an admin permission
1274           */
1275  		function userHasPermission( $userInfo, $blogInfo, $permName, $mode = BLOG_PERMISSION )
1276          {            
1277              // check for the permission, whether the user is the blog owner or
1278              // whether the user is a site administrator
1279              $hasPermission = false;
1280              if( $mode == BLOG_PERMISSION ) {
1281                  $hasPermission = ( 
1282                      $userInfo->hasPermissionByName( $permName, $blogInfo->getId()) ||
1283                      $blogInfo->getOwnerId() == $userInfo->getId() ||
1284                      $userInfo->hasPermissionByName( "edit_blog_admin_mode", 0 )
1285                  );
1286              }
1287              else {                
1288                  $hasPermission = ( $userInfo->hasPermissionByName( $permName, 0 ));
1289              }
1290              
1291              return( $hasPermission );
1292          }    
1293  
1294      }
1295  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics