[pLog-svn] r648 - plog/trunk

oscar at devel.plogworld.net oscar at devel.plogworld.net
Mon Jan 3 00:49:33 GMT 2005


Author: oscar
Date: 2005-01-03 00:49:32 +0000 (Mon, 03 Jan 2005)
New Revision: 648

Modified:
   plog/trunk/xmlrpc.php
Log:
added support for the metaWeblog API, needs some final touches though...


Modified: plog/trunk/xmlrpc.php
===================================================================
--- plog/trunk/xmlrpc.php	2005-01-03 00:00:38 UTC (rev 647)
+++ plog/trunk/xmlrpc.php	2005-01-03 00:49:32 UTC (rev 648)
@@ -2,44 +2,47 @@
 
     /*
 
-    // working with pLOG 0.2 // 0.3
+	// working with pLOG 0.2 // 0.3
 
         mhe at ltcgroup.de
         todo
             - security, can delete/post/update on blog ? ACL
 
-    hints
+	hints
             - blogid/categoryid hardcoded in "blogid", check int _encode()
             - newline unix/windows/mac, currently hardcoded \r\n
 
-    history
-        2003-12-27: added improved xmlrpc error handling
-        2003-12-28: fixed hardcoded topic length
-        2004-01-21: added xmlrpc_api_enabled config check
+	history
+	    2003-12-27: added improved xmlrpc error handling
+	    2003-12-28: fixed hardcoded topic length
+	    2004-01-21: added xmlrpc_api_enabled config check
     */
 
-    if (!defined( "PLOG_CLASS_PATH" )) {
-        define( "PLOG_CLASS_PATH", dirname(__FILE__)."/");
+	if (!defined( "PLOG_CLASS_PATH" )) {
+    	define( "PLOG_CLASS_PATH", dirname(__FILE__)."/");
     }
 
+
     // include required classes, dont know what, but it works for me :-)
     include_once( PLOG_CLASS_PATH."class/object/object.class.php" );
     include_once( PLOG_CLASS_PATH."class/net/xmlrpc/IXR_Library.lib.php" );
     include_once( PLOG_CLASS_PATH."class/config/config.class.php" );
     include_once( PLOG_CLASS_PATH."class/database/db.class.php" );
-    include_once(PLOG_CLASS_PATH."class/dao/users.class.php");
-    include_once(PLOG_CLASS_PATH."class/dao/article.class.php");
-    include_once(PLOG_CLASS_PATH."class/dao/articles.class.php");
-    include_once(PLOG_CLASS_PATH."class/dao/articlecategories.class.php");
-    include_once(PLOG_CLASS_PATH."class/dao/users.class.php" );
-	include_once( PLOG_CLASS_PATH."class/data/textfilter.class.php" );
+    include_once( PLOG_CLASS_PATH."class/dao/users.class.php");
+    include_once( PLOG_CLASS_PATH."class/dao/article.class.php");
+    include_once( PLOG_CLASS_PATH."class/dao/articles.class.php");
+    include_once( PLOG_CLASS_PATH."class/dao/articlecategories.class.php");
+	include_once( PLOG_CLASS_PATH."class/dao/users.class.php" );
+	include_once( PLOG_CLASS_PATH."class/dao/blogs.class.php" );
+	include_once( PLOG_CLASS_PATH."class/template/cachecontrol.class.php" );
+	
 
     // init database
     $_db = new Db;
     $adodb = $_db->getDb();
 
     // config object
-    $config = new Config;
+    $config =& Config::getConfig();
 
     // users object
     $users = new Users;
@@ -49,32 +52,12 @@
 
     // category object
     $category = new ArticleCategories;
+    
+    // blog object
+    $blogsG = new Blogs;
 
-    function _encode($blogid, $category)
-    {
-        $max = 8;
 
-        $hash = 0;
-        for($i=1;$i<$max-strlen($category);$i++)
-        {
-            $hash .= "0";
-        }
-        $hash = $blogid . $hash . $category;
-        return $hash;
-    }
 
-    function _decode_blogid($hash)
-    {
-        $max = 8;
-        return substr($hash, 0, strlen($hash)-$max);
-    }
-
-    function _decode_category($hash)
-    {
-        $max = 8;
-        return substr($hash, strlen($hash)-$max)+0;
-    }
-
     function newPost($args)
     {
         global $users, $articles;
@@ -102,9 +85,18 @@
                 $status = POST_STATUS_PUBLISHED;
             } else
             {
-                $status = POST_STATUS_PUBLISHED;
+                $status = POST_STATUS_DRAFT;
             }
 
+            // Get the default category
+            $cats = $category->getBlogCategories($blogid);
+            foreach($cats as $cat)
+            {
+                $idCategory = $cat->_id;
+                // Stop here, we have a category
+                break;
+            }
+
             // fake topic
             $dummy = explode("\n", $content);
             $title = $dummy[0];
@@ -117,27 +109,189 @@
             $article = new Article(
                 $title,
                 $content, // text
-                _decode_category($blogid), // catid
+                $idCategory, // catid
                 $erg->_id, // userid
-                _decode_blogid($blogid), // blogid
+                $blogid, // blogid
                 $status,
-                0 // numread
+                0, // numread
+                Array( "comments_enabled" => true ) // enable comments
             );
             $article->setDate(date("YmdHis"));
             $postid = $articles->addArticle($article);
-        if ($postid != 0)
+	    if ($postid != 0)
+	    {
+		CacheControl::resetBlogCache( $blogid );	    
+		return sprintf( "%d", $postid );
+	    } else
+	    {
+		return new IXR_Error(-1, 'Internal error occured creating your post!');
+	    }
+        } else
         {
-        return $postid;
+            return new IXR_Error(-1, 'You did not provide the correct password');
+        }
+    }
+
+    function metaWeblogNewPost($args)
+    {
+        global $users, $articles, $category;
+        $blogid     = $args[0];
+        $username   = $args[1];
+        $password   = $args[2];
+        $content    = $args[3];
+        $publish    = $args[4]; // true post&publish | false post only
+        /*
+         int postid
+        */
+
+        // -mhe todo security
+
+        $erg = $users->getUserInfo(
+            $username,
+            $password
+        );
+
+        if ($erg != false)
+        {
+            if ($publish)
+            {
+                $status = POST_STATUS_PUBLISHED;
+            } else
+            {
+                $status = POST_STATUS_DRAFT;
+            }
+            
+            // Get the default category
+            //$cats = $category->getBlogCategories($blogid);
+            //foreach($cats as $cat)
+            //{
+            //    $idCategory = $cat->_id;
+            //    // Stop here, we have a category
+            //    break;
+            //}
+            
+            $title = $content["title"];
+            $body = $content["description"];
+            $catList = $content["categories"];
+            $categoryName = NULL;
+            if ( $catList != NULL )
+            {
+                foreach( $catList as $catName )
+                {
+                    // Only use the first category
+                    $categoryName = $catName;
+                    break;
+                }
+            }
+
+            $categories = Array();
+            if ( $categoryName != NULL )
+            {
+                $cats = $category->getBlogCategories($blogid);
+                foreach($cats as $cat)
+                {
+                    if ( strcmp( $cat->_name, $categoryName ) == 0 )
+                    {
+                        $idCategory = $cat->_id;
+                        break;
+                    }
+                }
+            }
+            
+            //
+            // :TODO: 
+            // here we should take into account that users can specify multiple categoires per
+            //post!!!
+            //
+            $categories[] = $idCategory;
+
+            $article = new Article(
+                $title,
+                $body, // text
+                $categories, // catid
+                $erg->_id, // userid
+                $blogid, // blogid
+                $status,
+                0, // numread
+                Array( "comments_enabled" => true ) // enable comments
+            );
+
+           $dateCreated = $content['dateCreated'];
+           
+           // there must be a bug in the xmlrpc library, we're getting an object in $dateCreated
+           // that does not have a type or anyhting, but it still is an object... kinda weird. Anyway,
+           // clients like ecto do not allow to change the time an article is posted so this is not 
+           // too annoying, *yet*
+           /* if (!empty($dateCreated))
+            {
+               // Convert the UTC time to local time, since articleDate is in local time
+               $ar = localtime ( $dateCreated->getTimestamp() );
               $ar[5] += 1900; $ar[4]++;
               $localTimeStamp = gmmktime ( $ar[2], $ar[1], $ar[0], $ar[4], $ar[3], $ar[5], $ar[8] );
+               $articleDate = date("YmdHis", $localTimeStamp);
+            } else
+            {
+               $articleDate = date("YmdHis");
+            }
+            
+            $article->setDate($articleDate);*/
+            $postid = $articles->addArticle($article);
+	    if ($postid != 0)
+	    {
+		CacheControl::resetBlogCache( $blogid );	    
+		return sprintf( "%d", $postid );
+	    } else
+	    {
+		return new IXR_Error(-1, 'Internal error occured creating your post!');
+	    }
         } else
         {
-        return new IXR_Error(-1, 'Internal error occured creating your post!');
+            return new IXR_Error(-1, 'You did not provide the correct password');
         }
+    }
+
+    function metaWeblogGetCategories($args)
+    {
+        global $users, $category, $blogsG;
+        $blogid     = $args[0];
+        $username   = $args[1];
+        $password   = $args[2];
+        /*
+         "description" =>
+         "htmlUrl" =>
+         "rssUrl" =>
+        */
+
+
+        $erg = $users->getUserInfo(
+            $username,
+            $password
+        );
+
+        if ($erg != false)
+        {
+            $cats = $category->getBlogCategories($blogid);
+            $ret = array();
+            foreach($cats as $cat)
+            {
+                $dummy                   = array();
+                $dummy["description"]    = $cat->_name;
+                
+                // We can't use $cat->_url becuase it is not set, so 
+                // we have to do this in long way
+                $blogInfo = $blogsG->getBlogInfo( $blogid );
+                $url = $blogInfo->getBlogRequestGenerator();
+                
+                $dummy["htmlUrl"]        = $url->categoryLink( $cat );
+                $dummy["rssUrl"]         = "http://";
+                $ret[$cat->_name]              = $dummy;
+            }
+            return $ret;
         } else
         {
             return new IXR_Error(-1, 'You did not provide the correct password');
         }
     }
 
+
     function getPost($args)
     {
         global $users, $articles;
@@ -178,6 +332,62 @@
         }
     }
 
+    function metaWeblogGetPost($args)
+    {
+        global $users, $articles;
+        $postid     = $args[0];
+        $username   = $args[1];
+        $password   = $args[2];
+
+        /*
+            "userid" =>
+            "dateCreated" =>
+            "content" =>
+            "postid" =>
+        */
+
+        $erg = $users->getUserInfo($username,$password);
+        if ($erg != false)
+        {
+            $item = $articles->getUserArticle($postid, $erg->_id);
+            $time = mktime(
+                substr($item->_date,8,2),
+                substr($item->_date,10,2),
+                substr($item->_date,12,2),
+                substr($item->_date,4,2),
+                substr($item->_date,6,2),
+                substr($item->_date,0,4)
+            );
+            
+            $articleCat = $item->getCategory();
+            
+            $blogId = $item->getBlog();
+            $blogs = new Blogs();
+            $blogInfo = $blogs->getBlogInfo( $blogId );
+            $url = $blogInfo->getBlogRequestGenerator();
+
+            $dummy                  = array();
+            $dummy["userid"]        = $item->_userInfo->_id;
+            $dummy["dateCreated"]   = new IXR_Date($time);
+            $dummy["title"]         = $item->_topic;
+            $dummy["description"]   = $item->_text;
+            $dummy["postid"]        = $item->_id;
+            
+            $dummy["link"]          = $url->postLink( $item );
+            $dummy["permaLink"]     = $url->postPermalink( $item );
+            
+            $catArray               = array();
+            foreach( $item->getCategories() as $category ) {
+                $catArray[]             = $category->getName();
+            }
+            $dummy["categories"]      = $catArray;
+            return $dummy;
+        } else
+        {
+            return new IXR_Error(-1, 'You did not provide the correct password');
+        }
+    }
+
     function editPost($args)
     {
         global $users, $articles;
@@ -217,6 +427,8 @@
             $article->setStatus($status);
 
             $articles->updateArticle($article);
+            
+    		CacheControl::resetBlogCache( $blogid );            
 
             return true;
         } else
@@ -225,6 +437,51 @@
         }
     }
 
+    function metaWeblogEditPost($args)
+    {
+        global $users, $articles, $category;
+
+        $postid     = $args[0];
+        $username   = $args[1];
+        $password   = $args[2];
+        $content    = $args[3];
+        $publish    = $args[4];
+
+        /*
+            boolean, true or false
+        */
+
+        $erg = $users->getUserInfo($username,$password);
+        if ($erg != false)
+        {
+            if ($publish)
+            {
+                $status = POST_STATUS_PUBLISHED;
+            } else
+            {
+                $status = POST_STATUS_DRAFT;
+            }
+    
+
+            $title = $content["title"];
+            $body = $content["description"];
+
+            $article = $articles->getUserArticle($postid);
+            $article->setText($body);
+            $article->setTopic($title);
+            $article->setStatus($status);
+
+            $articles->updateArticle($article);
+            
+    		CacheControl::resetBlogCache( $blogid );            
+
+            return true;
+        } else
+        {
+            return new IXR_Error(-1, 'You did not provide the correct password');
+        }
+    }
+
     function deletePost($args)
     {
         global $users, $articles;
@@ -247,6 +504,9 @@
                 $article->getBlog(),
                 $forever = true // kick 4eva
             );
+            
+    		CacheControl::resetBlogCache( $blogid );            
+            
             return true;
         } else
         {
@@ -274,11 +534,9 @@
         {
             $ret = array();
             $list = $articles->getBlogArticles(
-                _decode_blogid($blogid),
+                $blogid,
                 -1,
-                $number,
-                _decode_category($blogid),
-                0
+                $number
             );
 
             foreach($list as $item)
@@ -307,6 +565,70 @@
         }
     }
 
+    function metaWeblogGetRecentPosts($args)
+    {
+        global $users, $articles;
+        /*
+            "userid" =>
+            "dateCreated" =>
+            "content" =>
+            "postid" =>
+        */
+        $blogid     = $args[0];
+        $username   = $args[1];
+        $password   = $args[2];
+        $number     = $args[3];
+
+        $erg = $users->getUserInfo($username,$password);
+        if ($erg != false)
+        {
+            $ret = array();
+            $list = $articles->getBlogArticles(
+                $blogid,
+                -1,
+                $number
+            );
+
+            foreach($list as $item)
+            {
+                $time = mktime(
+                    substr($item->_date,8,2),
+                    substr($item->_date,10,2),
+                    substr($item->_date,12,2),
+                    substr($item->_date,4,2),
+                    substr($item->_date,6,2),
+                    substr($item->_date,0,4)
+                );
+
+                $articleCat = $item->getCategory();
+
+                $dummy                  = array();
+                $dummy["userid"]        = $item->_userInfo->_id;
+                $dummy["dateCreated"]   = new IXR_Date($time);
+                $dummy["title"]         = $item->_topic; 
+                $dummy["description"]   = $item->_text;
+                $dummy["postid"]        = $item->_id;
+
+                $blogId = $item->getBlog();
+                $blogs = new Blogs();
+                $blogInfo = $blogs->getBlogInfo( $blogId );
+                $url = $blogInfo->getBlogRequestGenerator();
+                $dummy["link"]          = $url->postLink( $item );
+                $dummy["permaLink"]     = $url->postPermalink( $item );
+
+                $catArray               = array();
+                $catArray[]             = $articleCat->getName();
+                $dummy["categories"]      = $catArray;
+
+                $ret[]                  = $dummy;
+            }
+            return $ret;
+        } else
+        {
+            return new IXR_Error(-1, 'You did not provide the correct password');
+        }
+    }
+
     function getUserInfo($args)
     {
         $appkeyp    = $args[0];
@@ -320,9 +642,9 @@
         "userid" =>
         "url" =>
         */
-
-        $users = new Users();
-
+		
+		$users = new Users();
+		
         $erg = $users->getUserInfo(
             $username,
             $password
@@ -367,15 +689,12 @@
             $ret = array();
             foreach($blogs as $blog)
             {
-                $cats = $category->getBlogCategories($blog->_id);
-                foreach($cats as $cat)
-                {
-                    $dummy              = array();
-                    $dummy["blogid"]    = _encode($blog->_id, $cat->_id);
-                    $dummy["blogName"]  = "/" . $blog->_blog . "/" . $cat->_name;
-                    $dummy["url"]       = "http://";
-                    $ret[]              = $dummy;
-                }
+                $dummy              = array();
+                $dummy["blogid"]    = $blog->_id;
+                $dummy["blogName"]  = $blog->_blog;
+                $url = $blog->getBlogRequestGenerator();
+                $dummy["url"]       = $url->blogLink();
+                $ret[]              = $dummy;
             }
             return $ret;
         } else
@@ -386,26 +705,30 @@
 
     if ($config->getValue("xmlrpc_api_enabled"))
     {
-    $xmlrpc = new IXR_Server(
-            array (
-            "blogger.newPost"           => "newPost",
-            "blogger.getUserInfo"       => "getUserInfo",
-            "blogger.getPost"           => "getPost",
-            "blogger.editPost"          => "editPost",
-            "blogger.deletePost"        => "deletePost",
-            "blogger.getRecentPosts"    => "getRecentPosts",
-            "blogger.getUserInfo"       => "getUserInfo",
-            "blogger.getUsersBlogs"     => "getUsersBlogs"
-            )
-    );
+	$xmlrpc = new IXR_Server(
+    	    array (
+        	"blogger.newPost"           => "newPost",
+        	"blogger.getUserInfo"       => "getUserInfo",
+        	"blogger.getPost"           => "getPost",
+        	"blogger.editPost"          => "editPost",
+        	"blogger.deletePost"        => "deletePost",
+        	"blogger.getRecentPosts"    => "getRecentPosts",
+        	"blogger.getUserInfo"       => "getUserInfo",
+        	"blogger.getUsersBlogs"     => "getUsersBlogs",
+            "metaWeblog.newPost"        => "metaWeblogNewPost",
+            "metaWeblog.editPost"       => "metaWeblogEditPost",
+            "metaWeblog.getPost"        => "metaWeblogGetPost",
+            "metaWeblog.getRecentPosts" => "metaWeblogGetRecentPosts",
+            "metaWeblog.getCategories"  => "metaWeblogGetCategories"
+    	    )
+	);
     } else
     {
-    // xmlrpc_api disabled, no methods configured
-    $xmlrpc = new IXR_Server(
-            array (
-            )
-    );
-
+	// xmlrpc_api disabled, no methods configured
+	$xmlrpc = new IXR_Server(
+    	    array (
+    	    )
+	);
+    
     }
-
 ?>
\ No newline at end of file




More information about the pLog-svn mailing list