[pLog-svn] xmlrpc file change?

Jon Daley plogworld at jon.limedaley.com
Mon Jun 4 15:59:54 EDT 2007


Do people have objections to changing the /class/net/xmlrpc/ directory 
like this?

I thought it would be a lot cleaner than this, which is why I started on 
it.

The advantage is that each api is separate from each other, and seems 
easier to add new apis.  But, it didn't end up as clean as I thought it 
would be, so I'd understand if people didn't like it.

I am looking into porting the new gdata api, or maybe the atom api, not 
sure what.


Index: xmlrpcserver.class.php
===================================================================
...
     // include APIs
     $GLOBALS["methods"] = array();
     lt_include(PLOG_CLASS_PATH."class/net/xmlrpc/apis/blogger.php");
     lt_include(PLOG_CLASS_PATH."class/net/xmlrpc/apis/metaweblog.php");
     lt_include(PLOG_CLASS_PATH."class/net/xmlrpc/apis/movabletype.php");


     class XmlRpcServer extends IXR_Server
     {
         function XmlRpcServer()
         {
             $config =& Config::getConfig();
             if ($config->getValue("xmlrpc_api_enabled"))
             {
                 $this->IXR_Server($GLOBALS["methods"]);
             }
             else {
                     // xmlrpc_api disabled, no methods configured
                 $this->IXR_Server( Array ());
             }
         }
     }


     /**
      * sets the character set for responses
      */
     function setResponseCharset( $blog )
     {
         $locale = $blog->getLocale();
         $this->defencoding = $locale->getCharset();
     }

     /**
      * Extra helper method to check permissions
      *
      * @param user A UserInfo object
      * @param blog A BlogInfo object
      * @param permName Name of the permission
      * @param mode Either BLOG_PERMISSION or ADMIN_PERMISSION, depending on whether
      * we're checking the user's permissions in this blog or an admin permission
      */
     function userHasPermission( $userInfo, $blogInfo, $permName, $mode = BLOG_PERMISSION )
     {
             // check for the permission, whether the user is the blog owner or
             // whether the user is a site administrator
         $hasPermission = false;
         if( $mode == BLOG_PERMISSION ) {
             $hasPermission = (
                 $userInfo->hasPermissionByName( $permName, $blogInfo->getId()) ||
                 $blogInfo->getOwnerId() == $userInfo->getId() ||
                 $userInfo->hasPermissionByName( "edit_blog_admin_mode", 0 )
                 );
         }
         else {
             $hasPermission = ( $userInfo->hasPermissionByName( $permName, 0 ));
         }

         return( $hasPermission );
     }

?>





Index: apis/blogger.php
===================================================================
<?php

$GLOBALS["methods"] = array_merge($GLOBALS["methods"],
                                   array(
                                       "blogger.newPost"           => "Blogger_NewPost",
                                       "blogger.getPost"           => "Blogger_GetPost",
                                       "blogger.editPost"          => "Blogger_EditPost",
                                       "blogger.deletePost"        => "Blogger_DeletePost",
                                       "blogger.getRecentPosts"    => "Blogger_GetRecentPosts",
                                       "blogger.getUserInfo"       => "Blogger_GetUserInfo",
                                       "blogger.getUsersBlogs"     => "Blogger_GetUsersBlogs"));


function Blogger_NewPost($args){
     $users = new Users();
     $articles = new Articles();
     $category = new ArticleCategories();
     $blogsG = new Blogs();

     $appkey     = $args[0];
     $blogid     = $args[1];
     $username   = $args[2];
     $password   = $args[3];
     $content    = $args[4];
     $publish    = $args[5]; // true post&publish | false post only
         /*
 	         int postid
         */

         // -mhe todo security

     $userInfo = $users->getUserInfo( $username, $password );

     if (!$userInfo) {
         return new IXR_Error(-1, 'You did not provide the correct username and/or password');
     }

     if(!$blogId){
         $blogs = $userInfo->getBlogs();
         if(!$blogs){
             return new IXR_Error(-1, "This user doesn't have access to any blogs.");
         }
         $blogid = $blogs[0]->getId();
     }

     $blogInfo = $blogsG->getBlogInfo( $blogid );
     if( !$blogInfo ) {
         return new IXR_Error(-1, 'Error loading blog' );
     }

         // check this user's permissions before proceeding
     if( !userHasPermission( $userInfo, $blogInfo, "add_post" )) {
         return new IXR_Error(-1, 'This user does not have enough permissions' );
     }

     if ($publish) {
         $status = POST_STATUS_PUBLISHED;
     }
     else {
         $status = POST_STATUS_DRAFT;
     }

         // Get the default category
     $cats = $category->getBlogCategories($blogid);

         // some protection again blogs without categories
     if( !$cats ) {
         return new IXR_Error(-1, 'This blog does not have any categories!');
     }

     foreach($cats as $cat) {
         $idCategory = $cat->getId();
             // Stop here, we have a category
         break;
     }

         // ecto sends the topic as <title>blah blah</title>rest of the body
     if( preg_match( "/<title>(.*)<\/title>(.*)/i", $content, $matches )) {
         $title = $matches[1];
         $content = $matches[2];
     }
     else {
         $dummy = explode("\n", $content);
         if( count( $dummy ) == 1 ) {
             $title = substr( $content, 0, 60 );
         }
         else {
             $title = $dummy[0];
             unset($dummy[0]);
             $content = implode("\n", $dummy);
             unset($dummy);
         }
     }

     $article = new Article(
         $title,
         $content, // text
         Array( $idCategory ), // catid
         $userInfo->getId(), // userid
         $blogid, // blogid
         $status,
         0, // numread
         Array( "comments_enabled" => true ) // enable comments
         );

     $article->setDate(date("YmdHis"));

         // Get the plugin manager
     $plugMgr =& PluginManager::getPluginManager();
     $plugMgr->setBlogInfo( $blogInfo);
     $plugMgr->setUserInfo( $userInfo );
     $plugMgr->loadPlugins();
         // Send the PRE_POST_POST_ADD message
     $plugMgr->notifyEvent( EVENT_PRE_POST_ADD, Array( "article" => &$article ));

     $postid = $articles->addArticle($article);

     if ($postid == 0) {
         return new IXR_Error(-1, 'Internal error occurred while creating your post!');
     }
         // The post was successful
         // Send the EVENT_POST_POST_ADD messages to the plugins
     $plugMgr->notifyEvent( EVENT_POST_POST_ADD, Array( "article" => &$article ));
     CacheControl::resetBlogCache( $blogid );

     $blogSettings = $blogInfo->getSettings();

         // Add article notifcations if this is specified by the default setting.
     if ($blogSettings->getValue( "default_send_notification" ))
     {
         lt_include( PLOG_CLASS_PATH."class/dao/articlenotifications.class.php" );

         $artNotifications = new ArticleNotifications();
         $artNotifications->addNotification( $postid, $blogid, $userInfo->getId());
     }

     setResponseCharset( $blogInfo );

     return sprintf( "%d", $postid );
}


function Blogger_GetPost($args){
     lt_include( PLOG_CLASS_PATH."class/data/timestamp.class.php" );

     $users = new Users();
     $articles = new Articles();

     $appkey     = $args[0];
     $postid     = $args[1];
     $username   = $args[2];
     $password   = $args[3];

         /*
 	            "userid" =>
 	            "dateCreated" =>
 	            "content" =>
 	            "postid" =>
         */

     $userInfo = $users->getUserInfo($username,$password);
     if( !$userInfo ) {
         return new IXR_Error(-1, 'You did not provide the correct username and/or password');
     }
     $item = $articles->getBlogArticle($postid,
                                       -1, // blogId
                                       true, // includeHiddenFields
                                       -1, // date
                                       -1, // categoryId
                                       $userInfo->getId());
     $dateObject = $item->getDateObject();
         // Get the unix time stamp
     $time = $dateObject->getTimestamp(DATE_FORMAT_UNIXTIME);

     $dummy                  = array();
     $userInfo               = $item->getUserInfo();
     $dummy["userid"]        = $userInfo->getId();
     $dummy["dateCreated"]   = new IXR_Date($time);
     $dummy["content"]       = $item->getTopic() . "\r\n" . $item->getText(false) . " ";
     $dummy["postid"]        = $item->getId();

     $blogInfo = $item->getBlogInfo();

         // check the permissions
     if( !userHasPermission( $userInfo, $blogInfo, "view_posts" )) {
         return new IXR_Error(-1, 'This user does not have enough permissions' );
     }

     setResponseCharset( $blogInfo );

     return $dummy;
}


function Blogger_EditPost($args){
     $users = new Users();
     $articles = new Articles();
     $blogsG = new Blogs();

     $appkey     = $args[0];
     $postid     = $args[1];
     $username   = $args[2];
     $password   = $args[3];
     $content    = $args[4];
     $publish    = $args[5];

         /*
 	            boolean, true or false
         */

     $userInfo = $users->getUserInfo( $username, $password );
     if( !$userInfo ) {
         return new IXR_Error(-1, 'You did not provide the correct username and/or password');
     }

         // fake topic
     $dummy = explode("\n", $content);
     if( count($dummy) == 1 ) {
         $title = substr( $content, 0, 60 );
     }
     else {
         $title = $dummy[0];
         unset($dummy[0]);
         $content = implode("\n", $dummy);
         unset($dummy);
     }

     $article = $articles->getBlogArticle($postid,
                                          -1, // blogId
                                          true, // includeHiddenFields
                                          -1, // date
                                          -1, // categoryId
                                          $userInfo->getId());

     if( !$article ) {
         return( new IXR_Error(-1, 'The article id is not correct' ));
     }

     $blogInfo = $article->getBlogInfo();

         // check the permissions
     if( !userHasPermission( $userInfo, $blogInfo, "update_post" )) {
         return new IXR_Error(-1, 'This user does not have enough permissions' );
     }

     if ($publish) {
         $status = POST_STATUS_PUBLISHED;
     }
     else {
         $status = POST_STATUS_DRAFT;
     }

     $article->setText($content);
     $article->setTopic($title);
     $article->setStatus($status);

         // Get the plugin manager
     $plugMgr =& PluginManager::getPluginManager();
     $plugMgr->setBlogInfo( $blogInfo );
     $plugMgr->setUserInfo( $userInfo );
     $plugMgr->loadPlugins();
         // Send the EVENT_PRE_POST_UPDATE message
     $plugMgr->notifyEvent( EVENT_PRE_POST_UPDATE, Array( "article" => &$article ));

     $articles->updateArticle($article);

         // Send the EVENT_POST_POST_UPDATE messages to the plugins
     $plugMgr->notifyEvent( EVENT_POST_POST_UPDATE, Array( "article" => &$article ));

     CacheControl::resetBlogCache( $blogInfo->getId());

     setResponseCharset( $blogInfo );

     return true;
}


function Blogger_DeletePost($args){
     $users = new Users();
     $articles = new Articles();
     $blogsG = new Blogs();

     $appkey     = $args[0];
     $postid     = $args[1];
     $username   = $args[2];
     $password   = $args[3];
     $publish    = $args[4];

     $userInfo = $users->getUserInfo( $username, $password );
     if( !$userInfo ) {
         return new IXR_Error(-1, 'You did not provide the correct username and/or password');
     }
     $article = $articles->getBlogArticle($postid,
                                          -1, // blogId
                                          true, // includeHiddenFields
                                          -1, // date
                                          -1, // categoryId
                                          $userInfo->getId());

         // check if the article that was pulled is valid at all
     if( !$article ) {
         return( new IXR_Error(-1, 'The article id is not correct' ));
     }

         // check the permissions
     $blogInfo = $article->getBlogInfo();
     if( !userHasPermission( $userInfo, $blogInfo, "update_post" )) {
         return( new IXR_Error(-1, 'This user does not have enough permissions' ));
     }

         // Get the plugin manager
     $plugMgr =& PluginManager::getPluginManager();
     $plugMgr->setBlogInfo( $blogInfo );
     $plugMgr->setUserInfo( $userInfo );
     $plugMgr->loadPlugins();
         // Send the EVENT_PRE_POST_DELETE message
     $plugMgr->notifyEvent( EVENT_PRE_POST_DELETE, Array( "article" => &$article ));

     $articles->deleteArticle(
         $postid,
         $userInfo->getId(), // userid
         $article->getBlog()
         );

         // Send the EVENT_POST_POST_DELETE messages to the plugins
     $plugMgr->notifyEvent( EVENT_POST_POST_DELETE, Array( "article" => &$article ));

     CacheControl::resetBlogCache( $blogInfo->getId());

     setResponseCharset( $blogInfo );

     return true;
}


function Blogger_GetRecentPosts($args){
     $users = new Users();
     $articles = new Articles();
     $blogs = new Blogs();

         /*
 	            "userid" =>
 	            "dateCreated" =>
 	            "content" =>
 	            "postid" =>
         */
     $appkey     = $args[0];
     $blogid     = $args[1];
     $username   = $args[2];
     $password   = $args[3];
     $number     = $args[4];

     $userInfo = $users->getUserInfo($username,$password);
     if( !$userInfo ){
         return new IXR_Error(-1, 'You did not provide the correct username and/or password');
     }
     $blogInfo = $blogs->getBlogInfo( $blogid );
     if( !$blogInfo ) {
         return new IXR_Error(-1, 'Incorrect blog id');
     }

         // check this user's permissions
     if( !userHasPermission( $userInfo, $blogInfo, "view_posts" )) {
         return new IXR_Error(-1, 'This user does not have enough permissions' );
     }

     $ret = array();
     $list = $articles->getBlogArticles(
         $blogid,
         -1, // date
         $number,  // amount
         -1  // any category id
         );

     foreach( $list as $item ) {
         $dateObject = $item->getDateObject();
         lt_include( PLOG_CLASS_PATH."class/data/timestamp.class.php" );
             // Get the unix time stamp
         $time = $dateObject->getTimestamp(DATE_FORMAT_UNIXTIME);

         $dummy                  = array();
         $userInfo               = $item->getUserInfo();
         $dummy["userid"]        = $userInfo->getId();
         $dummy["dateCreated"]   = new IXR_Date($time);
         $dummy["content"]       = $item->getTopic() . "\r\n" . $item->getText(false) . " ";
         $dummy["postid"]        = $item->getId();

         $ret[]                  = $dummy;
     }

     setResponseCharset( $blogInfo );

     return $ret;
}


function Blogger_GetUserInfo($args){
     $appkeyp    = $args[0];
     $username   = $args[1];
     $password   = $args[2];

     $users = new Users();

     $userInfo = $users->getUserInfo( $username, $password );

     if (!$userInfo){
         return new IXR_Error(-1, 'You did not provide the correct username and/or password');
     }

     $ret                = array();
     $ret["nickname"]    = $userInfo->getUsername();
     $ret["firstname"]   = $userInfo->getUsername();
     $ret["lastname"]    = "";
     $ret["email"]       = $userInfo->getEmail();
     $ret["userid"]      = $userInfo->getId();
     $ret["url"]         = "";

         // set the response encoding according to one of the blogs owned by this user
     $userBlogs = $users->getUsersBlogs( $userInfo->getId(), BLOG_STATUS_ACTIVE );
     if( count($userBlogs) > 0 ) {
         $blogInfo = array_pop( $userBlogs );
         setResponseCharset( $blogInfo );
     }

     return $ret;
}


function Blogger_GetUsersBlogs($args){
     $users = new Users();
     $category = new ArticleCategories();

     $appkey     = $args[0];
     $username   = $args[1];
     $password   = $args[2];
         /*
 	            "blogName" =>
 	            "url" =>
 	            "blogid" =>
         */

     $userInfo = $users->getUserInfo( $username, $password );

     if (!$userInfo){
         return new IXR_Error(-1, 'You did not provide the correct username and/or password');
     }
     $blogs = $users->getUsersBlogs($userInfo->getId(), BLOG_STATUS_ACTIVE );
     $ret = array();
     foreach($blogs as $blog)
     {
         $dummy              = array();
         $dummy["blogid"]    = $blog->_id;
         $dummy["blogName"]  = $blog->_blog;
         $url = $blog->getBlogRequestGenerator();
         $dummy["url"]       = $url->blogLink();
         $ret[]              = $dummy;
     }

         // set the encoding as long as we've got at least one blog
     if( count( $blogs ) > 0 ) {
         $blogInfo = $blogs[0];
         setResponseCharset( $blogInfo );
     }

     return $ret;
}

?>


More information about the pLog-svn mailing list