NewsFeaturesDownloadsDevelopmentSupportAbout Us

lifetype-1.1.6/class/action/addtrackbackaction.class.php

Go to the documentation of this file.
00001 <?php
00002 
00003         include_once( PLOG_CLASS_PATH."class/action/action.class.php" );
00004         include_once( PLOG_CLASS_PATH."class/view/trackbackview.class.php" );
00005     include_once( PLOG_CLASS_PATH."class/config/config.class.php" );
00006     include_once( PLOG_CLASS_PATH."class/data/textfilter.class.php" );
00007     include_once( PLOG_CLASS_PATH."class/net/http/httpvars.class.php" );
00008         include_once( PLOG_CLASS_PATH."class/dao/articlenotifications.class.php" );
00009         include_once( PLOG_CLASS_PATH."class/dao/articles.class.php" );
00010         include_once( PLOG_CLASS_PATH."class/dao/blogs.class.php" );
00011         include_once( PLOG_CLASS_PATH."class/template/cachecontrol.class.php" );
00012     include_once( PLOG_CLASS_PATH."class/plugin/pluginmanager.class.php" );
00013         include_once( PLOG_CLASS_PATH."class/net/client.class.php" );
00014     include_once( PLOG_CLASS_PATH."class/dao/trackbacks.class.php" );
00015         include_once( PLOG_CLASS_PATH."class/data/validator/stringvalidator.class.php" );       
00016         include_once( PLOG_CLASS_PATH."class/data/validator/integervalidator.class.php" );
00017         include_once( PLOG_CLASS_PATH."class/security/pipeline.class.php" );
00018         
00025         class AddTrackbackAction extends Action
00026         {
00027         
00028                 function AddTrackbackAction( $actionInfo, $request )
00029                 {
00030                         $this->Action( $actionInfo, $request );
00031                         
00032                         // we need certain data
00033                         $this->registerFieldValidator( "id", new IntegerValidator());
00034                         $this->registerFieldValidator( "url", new StringValidator());
00035                         $this->setValidationErrorView( new TrackbackView( "Error incorrect parameters",
00036                                                                                                                           true ));
00037                 }
00038                                 
00043                 function tblog( $message )
00044                 {
00045                     include_once( PLOG_CLASS_PATH . "class/logger/loggermanager.class.php" );
00046 
00047                         $logger =& LoggerManager::getLogger( "trackback" );
00048                         $logger->debug( $message );
00049                 }
00050                 
00051                 function perform()
00052         {
00053                 // check if we should be receiving trackbacks at all
00054                 $config =& Config::getConfig();
00055                 if( !$config->getValue( "trackback_server_enabled", false )) {
00056                 $this->tblog( "ERROR: Trackbacks are not enabled in this site" );
00057                 $this->_view = new TrackbackView( "Trackbacks are not enabled in this site", true );
00058                 return( false );                        
00059                 }
00060                 
00061             // for security, we will strip _ANY_ html tag from the tags
00062             $tf = new TextFilter();
00063             $blogName  = $tf->filterAllHTML( $this->_request->getValue( "blog_name" ));
00064             $excerpt   = $tf->filterAllHTML( $this->_request->getValue( "excerpt" ));
00065             $title     = $tf->filterAllHTML( $this->_request->getValue( "title" ));
00066             $articleId = $this->_request->getValue( "id" );
00067             $url       = $tf->filterAllHTML( $this->_request->getValue( "url" ));
00068             
00069             $this->tblog( "** Incoming request **" );
00070             $this->tblog( "Blog name = ".$blogName );
00071             $this->tblog( "Excerpt = ".$excerpt );
00072             $this->tblog( "Title = ".$title );
00073             $this->tblog( "Article ID = ".$articleId );
00074             $this->tblog( "url = ".$url );      
00075 
00076             // try to see if the article is correct
00077             $articles = new Articles();
00078             $article = $articles->getBlogArticle( $articleId );
00079             if( !$article ) {
00080                 $this->tblog( "ERROR: Incorrect error identifier" );
00081                 $this->_view = new TrackbackView( "Incorrect article identifier", true );
00082                 return( false );
00083             }
00084     
00085             // try to load the blog info too, as we are going to need it
00086             $blogs = new Blogs();
00087             $blogInfo = $blogs->getBlogInfo( $article->getBlog());
00088     
00089             // a bit of protection...
00090             if( !$blogInfo ) {
00091                 $this->tblog( "ERROR: Article id ".$article->getId()." points to blog ".$article->getBlog()." that doesn't exist!" );
00092                 $this->_view = new TrackbackView( "The blog does not exist", true );
00093                 return( false );
00094             }
00095     
00096             // if the blog is disabled, then we shoulnd't take trackbacks...
00097             if( $blogInfo->getStatus() != BLOG_STATUS_ACTIVE ) {
00098                 $this->tblog( "ERROR: The blog ".$blogInfo->getBlog()." is set as disabled and cannot receive trackbacks!" );
00099                 $this->_view = new TrackbackView( "The blog is not active", true );
00100                 return( false );
00101             }
00102             
00103             // if everything went fine, load the plugins so that we can throw some events...
00104             $pm =& PluginManager::getPluginManager();
00105             $pm->loadPlugins();
00106             // and also configure the BlogInfo and UserInfo objects so that they know
00107             // who threw the events...
00108             $pm->setBlogInfo( $blogInfo );
00109             $userInfo = $blogInfo->getOwnerInfo();
00110             $pm->setUserInfo( $userInfo );                                  
00111             
00112             // let's take a look at the security stuff, once we've made sure that the
00113             // blog and the article are both valid
00114             $pipeline = new Pipeline( $this->_request, $blogInfo );
00115             $result = $pipeline->process();
00116             // let the sender of the trackback know that something went wrong
00117             if( !$result->isValid()) {
00118                 // use the default view
00119                 $this->tblog( "The trackback was blocked by a filter" );
00120                 $this->_view = new TrackbackView( $result->getErrorMessage(), true );
00121                 print($this->_view->render());
00122                 die();
00123             }
00124     
00125             // receives the request and adds it to the database
00126             $trackbacks = new TrackBacks();
00127             // create teh trackback object
00128             $now = new Timestamp();
00129             $ip = Client::getIp();
00130             $trackback = new Trackback( $url, 
00131                                         $title, 
00132                                         $articleId, 
00133                                         $blogInfo->getId(),
00134                                         $excerpt, 
00135                                         $blogName, 
00136                                         $now->getTimestamp(), 
00137                                         $ip );
00138 
00139             // this code probably needs some explanation... 
00140             // Basically, if the bayesian filter is configured to save spam to the database marked as spam,
00141             // we would end up with two identical trackbacks: one marked as spam and the other one not marked
00142             // as spam. The first one would be created by the spam filter and the second one would be created
00143             // by us here, so we need to know if the trackback is already there and if not, don't add it.
00144             // This also works as an additional protection feature agains repeating trackback spammers.
00145             if( !$trackbacks->getIdenticalTrackback( $trackback )) {
00146                 // throw the event in case somebody is listening to it!
00147                 $pm->notifyEvent( EVENT_PRE_TRACKBACK_ADD, Array( "trackback" => &$trackback ));
00148                 $result = $trackbacks->addTrackBack( $trackback );
00149                 if( !$result ) {
00150                     $this->tblog( "There was an error saving the trackback!" );
00151                 }
00152             }
00153             
00154             // throw the post event too...
00155             $pm->notifyEvent( EVENT_POST_TRACKBACK_ADD, Array( "trackback" => &$trackback ));
00156             
00157             // everything went fine so let's create a normal view, without a message 
00158             // (the message is not needed if there is no error)
00159             $this->_view = new TrackbackView( "", false );          
00160 
00161             // notify the user that a new trackback has been received, if the article was
00162             // configured to receive notifications
00163             // but first make sure, the trackback was not removed by some plugins like validatetrackback...
00164             if( $trackbacks->getTrackBack( $trackback->getId() ) ) {
00165                 $notifier = new ArticleNotifications();
00166                 $notifier->notifyUsers( $article->getId(), $blogInfo);
00167             } 
00168             // clear the blog cache
00169             CacheControl::resetBlogCache( $article->getBlog());
00170             
00171             $this->tblog( "** End **" );
00172                 }
00173         }
00174 ?>