[pLog-svn] r1870 - in plog/trunk: . class/action class/controller class/view templates/misc

oscar at devel.plogworld.net oscar at devel.plogworld.net
Mon Apr 18 20:15:41 GMT 2005


Author: oscar
Date: 2005-04-18 20:15:41 +0000 (Mon, 18 Apr 2005)
New Revision: 1870

Added:
   plog/trunk/class/action/addtrackbackaction.class.php
   plog/trunk/class/view/trackbackview.class.php
   plog/trunk/templates/misc/trackback.template
Modified:
   plog/trunk/class/controller/controllermap.properties.php
   plog/trunk/trackback.php
Log:
cleaned up the trackback.php script and moved all the logic to class/action/addtrackbackaction.class.php so that the implementation is exactly the same as with any other feature of the blog. The new class TrackbackView takes care of rendering responses to the trackback client, based on the contents of the templates/misc/trackback.template file (didn't know where to put it)

Next step will consist in integrating the bayesian anti-spam filter (I'm quite close anyway)

Added: plog/trunk/class/action/addtrackbackaction.class.php
===================================================================
--- plog/trunk/class/action/addtrackbackaction.class.php	2005-04-18 19:24:32 UTC (rev 1869)
+++ plog/trunk/class/action/addtrackbackaction.class.php	2005-04-18 20:15:41 UTC (rev 1870)
@@ -0,0 +1,130 @@
+<?php
+
+	include_once( PLOG_CLASS_PATH."class/action/action.class.php" );
+	include_once( PLOG_CLASS_PATH."class/view/trackbackview.class.php" );
+    include_once( PLOG_CLASS_PATH."class/config/config.class.php" );
+    include_once( PLOG_CLASS_PATH."class/data/textfilter.class.php" );
+    include_once( PLOG_CLASS_PATH."class/net/http/httpvars.class.php" );
+	include_once( PLOG_CLASS_PATH."class/dao/articlenotifications.class.php" );
+	include_once( PLOG_CLASS_PATH."class/dao/articles.class.php" );
+	include_once( PLOG_CLASS_PATH."class/dao/blogs.class.php" );
+	include_once( PLOG_CLASS_PATH."class/template/cachecontrol.class.php" );
+    include_once( PLOG_CLASS_PATH."class/plugin/pluginmanager.class.php" );
+	include_once( PLOG_CLASS_PATH."class/net/client.class.php" );
+    include_once( PLOG_CLASS_PATH."class/dao/trackbacks.class.php" );
+	include_once( PLOG_CLASS_PATH."class/data/validator/stringvalidator.class.php" );	
+	
+	/**
+	 * Class that takes care of adding trackbacks
+	 *
+	 * \ingroup Action
+	 * @private
+	 */
+	class AddTrackbackAction extends Action
+	{
+	
+		function AddTrackbackAction( $actionInfo, $request )
+		{
+			$this->Action( $actionInfo, $request );
+			
+			// we need certain data
+			$this->registerFieldValidator( "id", new IntegerValidator());
+			$this->registerFieldValidator( "url", new StringValidator());
+			$this->setValidationErrorView( new TrackbackView( "Error incorrect parameters",
+															  true ));
+		}
+				
+		/**
+		 * @private
+		 * @static
+		 */
+		function log( $message )
+		{
+			$logger =& LoggerManager::getLogger( "trackback" );
+			$logger->debug( $message );
+		}
+		
+		function perform()
+		{
+			// for security, we will strip _ANY_ html tag from the tags
+			$tf = new TextFilter();
+			$blogName  = $tf->filterAllHTML( $this->_request->getValue( "blog_name" ));
+			$excerpt   = $tf->filterAllHTML( $this->_request->getValue( "excerpt" ));
+			$title     = $tf->filterAllHTML( $this->_request->getValue( "title" ));
+			$articleId = $this->_request->getValue( "id" );
+			$url       = $tf->filterAllHTML( $this->_request->getValue( "url" ));
+			
+			$this->log( "** Incoming request **" );
+			$this->log( "Blog name = ".$blogName );
+			$this->log( "Excerpt = ".$excerpt );
+			$this->log( "Title = ".$title );
+			$this->log( "Article ID = ".$articleId );
+			$this->log( "url = ".$url );
+
+			// try to see if the article is correct
+			$articles = new Articles();
+			$article = $articles->getBlogArticle( $articleId );
+			if( !$article ) {
+				$this->log( "ERROR: Incorrect error identifier" );
+				$this->_view = new TrackbackView( "Incorrect article identifier", true );
+				return( false );
+			}
+	
+			// try to load the blog info too, as we are going to need it
+			$blogs = new Blogs();
+			$blogInfo = $blogs->getBlogInfo( $article->getBlog());
+	
+			// a bit of protection...
+			if( !$blogInfo ) {
+				$this->log( "ERROR: Article id ".$article->getId()." points to blog ".$article->getBlog()." that doesn't exist!" );
+				$this->_view = new TrackbackView( "The blog does not exist", true );
+				return( false );
+			}
+	
+			// if the blog is disabled, then we shoulnd't take trackbacks...
+			if( $blogInfo->getStatus() != BLOG_STATUS_ACTIVE ) {
+				$this->log( "ERROR: The blog ".$blogInfo->getBlog()." is set as disabled and cannot receive trackbacks!" );
+				$this->_view = new TrackbackView( "The blog is not active", true );
+				return( false );
+			}
+	
+			// if everything went fine, load the plugins so that we can throw some events...
+			$pm =& PluginManager::getPluginManager();
+			$pm->loadPlugins();
+			// and also configure the BlogInfo and UserInfo objects so that they know
+			// who threw the events...
+			$pm->setBlogInfo( $blogInfo );
+			$userInfo = $blogInfo->getOwnerInfo();
+			$pm->setUserInfo( $userInfo );
+	
+			// receives the request and adds it to the database
+			$trackbacks = new TrackBacks();
+			// create teh trackback object
+			$now = new Timestamp();
+			$ip = Client::getIp();
+			$trackback = new Trackback( $url, $title, $articleId, $excerpt, $blogName, $now->getTimestamp(), $ip );
+			// throw the event in case somebody is listening to it!
+			$pm->notifyEvent( EVENT_PRE_TRACKBACK_ADD, Array( "trackback" => &$trackback ));
+			$result = $trackbacks->addTrackBack( $trackback );
+			if( !$result ) {
+				$this->log( "There was an error saving the trackback!" );
+			}
+			// throw the post event too...
+			$pm->notifyEvent( EVENT_POST_TRACKBACK_ADD, Array( "trackback" => &$trackback ));
+			
+			// everything went fine so let's create a normal view, without a message 
+			// (the message is not needed if there is no error)
+			$this->_view = new TrackbackView( "", false );			
+
+			// notify the user that a new trackback has been received, if the article was
+			// configured to receive notifications
+			$notifier = new ArticleNotifications();
+			$notifier->notifyUsers( $article->getId(), $blogInfo);
+	
+			// clear the blog cache
+			CacheControl::resetBlogCache( $article->getBlog());
+			
+			$this->log( "** End **" );
+		}
+	}
+?>
\ No newline at end of file

Modified: plog/trunk/class/controller/controllermap.properties.php
===================================================================
--- plog/trunk/class/controller/controllermap.properties.php	2005-04-18 19:24:32 UTC (rev 1869)
+++ plog/trunk/class/controller/controllermap.properties.php	2005-04-18 20:15:41 UTC (rev 1870)
@@ -14,6 +14,8 @@
     $actions["Sample"] = "SampleAction";
     // add a new comment
     $actions["AddComment"] = "AddCommentAction";
+	// add a new trackback
+	$actions["AddTrackback"] = "AddTrackbackAction";
     // loads the template specified
     $actions["Template"] = "TemplateAction";
     // shows the trackback information of an article

Added: plog/trunk/class/view/trackbackview.class.php
===================================================================
--- plog/trunk/class/view/trackbackview.class.php	2005-04-18 19:24:32 UTC (rev 1869)
+++ plog/trunk/class/view/trackbackview.class.php	2005-04-18 20:15:41 UTC (rev 1870)
@@ -0,0 +1,57 @@
+<?php
+
+	include_once( PLOG_CLASS_PATH."class/view/view.class.php" );
+	include_once( PLOG_CLASS_PATH."class/template/templatesets/templatesetstorage.class.php" );
+	include_once( PLOG_CLASS_PATH."class/template/templateservice.class.php" );	
+	
+    /**
+     * \ingroup View
+     * @private
+     *
+     * Provides XML-based responses to trackback requests
+     */
+    class TrackbackView extends View
+    {
+    
+        var $_profile;
+
+    	function TrackbackView( $message, $error = false )
+        {
+			$this->View();
+		
+			$ts = new TemplateSetStorage();
+
+			// we need to overwrite the $this->_template object with the Template object of our choice...
+            $this->_profile = $profile;
+			$templateService = new TemplateService();
+            $this->_template = $templateService->Template( 'trackback', 'misc' );
+			
+			// set the correct content type
+            $this->setContentType( 'text/xml' );
+			
+			$this->setValue( "message", $message );
+			if( $error ) $errorCode = 1;
+			else $errorCode = 0;
+			$this->setValue( "error", $errorCode );
+        }
+		
+		/**
+		 * View::render() does not implement any rendering logic so we'll have to provide our own
+		 * @private
+		 */
+		function render()
+		{
+			parent::render();
+		
+			// pass all the data to the template
+			$this->_template->assign( $this->_params->getAsArray());
+			// and render it
+			$response = $this->_template->fetch();
+			
+			AddTrackbackAction::log( "*** Sending response ***" );
+			AddTrackbackAction::log(  $response );
+			
+			print( $response );
+		}
+    }
+?>
\ No newline at end of file

Added: plog/trunk/templates/misc/trackback.template
===================================================================
--- plog/trunk/templates/misc/trackback.template	2005-04-18 19:24:32 UTC (rev 1869)
+++ plog/trunk/templates/misc/trackback.template	2005-04-18 20:15:41 UTC (rev 1870)
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="iso-8859-1"?>
+<response>
+<error>{$error}</error>
+{if $error}
+<message>{$message}</message>
+{/if}
+</response>
\ No newline at end of file

Modified: plog/trunk/trackback.php
===================================================================
--- plog/trunk/trackback.php	2005-04-18 19:24:32 UTC (rev 1869)
+++ plog/trunk/trackback.php	2005-04-18 20:15:41 UTC (rev 1870)
@@ -3,196 +3,18 @@
     if (!defined( "PLOG_CLASS_PATH" )) {
         define( "PLOG_CLASS_PATH", dirname(__FILE__)."/");
     }
+	
+	include_once( PLOG_CLASS_PATH."class/net/http/httpvars.class.php" );
 
-    include_once( PLOG_CLASS_PATH."class/dao/trackbacks.class.php" );
-    include_once( PLOG_CLASS_PATH."class/config/properties.class.php" );
-    include_once( PLOG_CLASS_PATH."class/config/config.class.php" );
-    include_once( PLOG_CLASS_PATH."class/data/textfilter.class.php" );
-    include_once( PLOG_CLASS_PATH."class/net/http/httpvars.class.php" );
-	include_once( PLOG_CLASS_PATH."class/dao/articlenotifications.class.php" );
-	include_once( PLOG_CLASS_PATH."class/dao/articles.class.php" );
-	include_once( PLOG_CLASS_PATH."class/dao/blogs.class.php" );
-	include_once( PLOG_CLASS_PATH."class/template/cachecontrol.class.php" );
-    include_once( PLOG_CLASS_PATH."class/plugin/pluginmanager.class.php" );
-	include_once( PLOG_CLASS_PATH."class/net/client.class.php" );
-
     //
-    // set this to 'true' if you want this script to log whatever it is
-    // doing... (useful for debugging)
+	// set appender for the "trackback" logger to "file" in
+	// config/logging.properties.php if you'd like this code to log some debug
+	// messages to tmp/trackback.log.
     //
-    define( "TRACKBACK_DEBUG_ENABLED", true );
-
-	/**
-	 * shorthand function for creating an error response to a client
-	 *
-	 * @param message the error message
-	 * @return returns the xml error message
-	 */
-    function errorResponse( $message )
-    {
-        $result = '<?xml version="1.0" encoding="iso-8859-1"?>';
-        $result .= '<response>';
-        $result .= '<error>1</error>';
-        $result .= '<message>'.$message.'</message>';
-        $result .= '</response>';
-
-        return $result;
-    }
 	
-	/**
-	 * dumps a whole request to the log file
-	 *
-	 * @param request
-	 * @return always true
-	 */
-	function dumpRequest( $request ) 
-	{
-
-		trackbackLog( "-- dump of trackback request --" );
-		$params = $request->getAsArray();
-		foreach( $params as $key => $value )
-		{
-			trackbackLog( "  $key = $value" );
-		}
-		trackbackLog( "-- end of dump --" );
-		
-		return true;
-	}
+	$request = HttpVars::getRequest();
+	$request["op"] = "AddTrackback";
+	HttpVars::setRequest( $request );
 	
-	/**
-	 * shorthand function for getting a logger and sending a message to the log file
-	 *
-	 * @param message
-	 * @return always true
-	 * @see LoggerManager
-	 */
-	function trackbackLog( $message )
-	{
-		if( TRACKBACK_DEBUG_ENABLED ) {
-			$logger =& LoggerManager::getLogger( "trackback" );
-			$logger->debug( $message );
-		}
-		
-		return true;
-	}
-	
-	/**
-	 *
-	 * whoa believe it or not, in pLog you can also find php code that is not within a class! :-)
-	 *
-	 * perhaps the code below should be cleaned up and put into a TrackbackServer class or something
-	 * like that but since it works the way it is, we'll leave it like it is!
-	 *
-	 */
-
-    // prepare everything...
-    $config =& Config::getConfig();
-    // get the post and get parameters
-    trackbackLog( "** Request received" );
-    $params = new Properties( HttpVars::getRequest());
-    dumpRequest( $params );
-
-    // check that they are correct and quit if they're not
-    if( $params->getValue("id") == "" || $params->getValue("id") <= 0 ) {
-        $result = errorResponse( "Incorrect or missing id parameter." );
-        print($result);
-        trackbackLog( "Sending error response: $result" );
-        trackbackLog( "** End" );
-        die;
-    }
-
-    if( $params->getValue( "url" ) == "" ) {
-        $result = errorResponse( "The url parameter must be present." );
-        print($result);
-        trackbackLog( "Sending error response: $result" );
-        trackbackLog( "** End" );
-        die;
-    }
-
-    if( !$config->getValue( "trackback_server_enabled" )) {
-		trackbackLog( "Trackback server disabled by administrator" );
-        $result = errorResponse( "Trackback feature has been disabled by the administrator." );
-		die( $result );	
-	}
-
-	// for security, we will strip _ANY_ html tag from the tags
-	$tf = new TextFilter();
-	$blogName  = $tf->filterAllHTML( $params->getValue( "blog_name" ));
-	$excerpt   = $tf->filterAllHTML( $params->getValue( "excerpt" ));
-	$title     = $tf->filterAllHTML( $params->getValue( "title" ));
-	$articleId = $params->getValue( "id" );
-	$url       = $tf->filterAllHTML( $params->getValue( "url" ));
-		
-	// try to see if the article is correct
-	$articles = new Articles();
-	$article = $articles->getBlogArticle( $articleId );
-	if( !$article ) {
-		trackbackLog( "ERROR: Incorrect error identifier" );
-		$result = errorResponse( "Incorrect article identifier" );
-		die( $result );
-	}
-	
-	// try to load the blog info too, as we are going to need it
-	$blogs = new Blogs();
-	$blogInfo = $blogs->getBlogInfo( $article->getBlog());
-	
-	// a bit of protection...
-	if( !$blogInfo ) {
-		trackbackLog( "ERROR: Article id ".$article->getId()." points to blog ".$article->getBlog()." that doesn't exist!" );
-		$result = errorResponse( "The blog does not exist" );
-		die( $result );
-	}
-	
-	// if the blog is disabled, then we shoulnd't take trackbacks...
-	if( $blogInfo->getStatus() != BLOG_STATUS_ACTIVE ) {
-		trackbackLog( "ERROR: The blog ".$blogInfo->getBlog()." is set as disabled and cannot receive trackbacks!" );
-		$result = errorResponse( "The blog is not active" );
-		die( $result );
-	}
-	
-	// if everything went fine, load the plugins so that we can throw some events...
-    $pm =& PluginManager::getPluginManager();
-    $pm->loadPlugins();
-	// and also configure the BlogInfo and UserInfo objects so that they know
-	// who threw the events...
-    $pm->setBlogInfo( $blogInfo );
-	$userInfo = $blogInfo->getOwnerInfo();
-    $pm->setUserInfo( $userInfo );
-	
-	// receives the request and adds it to the database
-	$trackbacks = new TrackBacks();
-	// create teh trackback object
-	$now = new Timestamp();
-	$ip = Client::getIp();
-	$trackback = new Trackback( $url, $title, $articleId, $excerpt, $blogName, $now->getTimestamp(), $ip );
-	// throw the event in case somebody is listening to it!
-	$pm->notifyEvent( EVENT_PRE_TRACKBACK_ADD, Array( "trackback" => &$trackback ));
-	$result = $trackbacks->addTrackBack( $trackback );
-	if( !$result ) {
-		trackbackLog( "There was an error saving the trackback!" );
-	}
-	// throw the post event too...
-	$pm->notifyEvent( EVENT_POST_TRACKBACK_ADD, Array( "trackback" => &$trackback ));
-
-	// result
-	$result = '<?xml version="1.0" encoding="iso-8859-1"?>';
-	$result .= '<response>';
-	$result .= '<error>0</error>';
-	$result .= '</response>';
-
-	// notify the user that a new trackback has been received, if the article was
-	// configured to receive notifications
-	$notifier = new ArticleNotifications();
-	$notifier->notifyUsers( $article->getId(), $blogInfo);
-	
-	// clear the blog cache
-	CacheControl::resetBlogCache( $article->getBlog());
-
-    // return the result
-    print($result);
-
-    trackbackLog( "Sending response: $result" );
-    trackbackLog( "** End" );
-	
-	die();
+	include_once( PLOG_CLASS_PATH."index.php" );
 ?>
\ No newline at end of file




More information about the pLog-svn mailing list