NewsFeaturesDownloadsDevelopmentSupportAbout Us

lifetype/branches/lifetype-1.0.6/trackback.php

Go to the documentation of this file.
00001 <?php
00002 
00003     if (!defined( "PLOG_CLASS_PATH" )) {
00004         define( "PLOG_CLASS_PATH", dirname(__FILE__)."/");
00005     }
00006 
00007     include_once( PLOG_CLASS_PATH."class/dao/trackbacks.class.php" );
00008     include_once( PLOG_CLASS_PATH."class/config/properties.class.php" );
00009     include_once( PLOG_CLASS_PATH."class/config/config.class.php" );
00010     include_once( PLOG_CLASS_PATH."class/data/textfilter.class.php" );
00011     include_once( PLOG_CLASS_PATH."class/net/http/httpvars.class.php" );
00012         include_once( PLOG_CLASS_PATH."class/dao/articlenotifications.class.php" );
00013         include_once( PLOG_CLASS_PATH."class/dao/articles.class.php" );
00014         include_once( PLOG_CLASS_PATH."class/dao/blogs.class.php" );
00015         include_once( PLOG_CLASS_PATH."class/template/cachecontrol.class.php" );
00016     include_once( PLOG_CLASS_PATH."class/plugin/pluginmanager.class.php" );     
00017 
00018     //
00019     // set this to 'true' if you want this script to log whatever it is
00020     // doing... (useful for debugging)
00021     //
00022     define( "TRACKBACK_DEBUG_ENABLED", true );
00023 
00030     function errorResponse( $message )
00031     {
00032         $result = '<?xml version="1.0" encoding="iso-8859-1"?>';
00033         $result .= '<response>';
00034         $result .= '<error>1</error>';
00035         $result .= '<message>'.$message.'</message>';
00036         $result .= '</response>';
00037 
00038         return $result;
00039     }
00040         
00047         function dumpRequest( $request ) 
00048         {
00049 
00050                 trackbackLog( "-- dump of trackback request --" );
00051                 $params = $request->getAsArray();
00052                 foreach( $params as $key => $value )
00053                 {
00054                         trackbackLog( "  $key = $value" );
00055                 }
00056                 trackbackLog( "-- end of dump --" );
00057                 
00058                 return true;
00059         }
00060         
00068         function trackbackLog( $message )
00069         {
00070                 if( TRACKBACK_DEBUG_ENABLED ) {
00071                         $logger =& LoggerManager::getLogger( "trackback" );
00072                         $logger->debug( $message );
00073                 }
00074                 
00075                 return true;
00076         }
00077         
00087     // prepare everything...
00088     $config =& Config::getConfig();
00089     // get the post and get parameters
00090     trackbackLog( "** Request received" );
00091     $params = new Properties( HttpVars::getRequest());
00092     dumpRequest( $params );
00093 
00094     // check that they are correct and quit if they're not
00095     if( $params->getValue("id") == "" || $params->getValue("id") <= 0 ) {
00096         $result = errorResponse( "Incorrect or missing id parameter." );
00097         print($result);
00098         trackbackLog( "Sending error response: $result" );
00099         trackbackLog( "** End" );
00100         die;
00101     }
00102 
00103     if( $params->getValue( "url" ) == "" ) {
00104         $result = errorResponse( "The url parameter must be present." );
00105         print($result);
00106         trackbackLog( "Sending error response: $result" );
00107         trackbackLog( "** End" );
00108         die;
00109     }
00110 
00111     if( !$config->getValue( "trackback_server_enabled" )) {
00112                 trackbackLog( "Trackback server disabled by administrator" );
00113         $result = errorResponse( "Trackback feature has been disabled by the administrator." );
00114                 die( $result ); 
00115         }
00116 
00117         // for security, we will strip _ANY_ html tag from the tags
00118         $tf = new TextFilter();
00119         $blogName  = $tf->filterAllHTML( $params->getValue( "blog_name" ));
00120         $excerpt   = $tf->filterAllHTML( $params->getValue( "excerpt" ));
00121         $title     = $tf->filterAllHTML( $params->getValue( "title" ));
00122         $articleId = $params->getValue( "id" );
00123         $url       = $tf->filterAllHTML( $params->getValue( "url" ));
00124                 
00125         // try to see if the article is correct
00126         $articles = new Articles();
00127         $article = $articles->getBlogArticle( $articleId );
00128         if( !$article ) {
00129                 trackbackLog( "ERROR: Incorrect error identifier" );
00130                 $result = errorResponse( "Incorrect article identifier" );
00131                 die( $result );
00132         }
00133         
00134         // try to load the blog info too, as we are going to need it
00135         $blogs = new Blogs();
00136         $blogInfo = $blogs->getBlogInfo( $article->getBlog());
00137         
00138         // a bit of protection...
00139         if( !$blogInfo ) {
00140                 trackbackLog( "ERROR: Article id ".$article->getId()." points to blog ".$article->getBlog()." that doesn't exist!" );
00141                 $result = errorResponse( "The blog does not exist" );
00142                 die( $result );
00143         }
00144         
00145         // if the blog is disabled, then we shoulnd't take trackbacks...
00146         if( $blogInfo->getStatus() != BLOG_STATUS_ACTIVE ) {
00147                 trackbackLog( "ERROR: The blog ".$blogInfo->getBlog()." is set as disabled and cannot receive trackbacks!" );
00148                 $result = errorResponse( "The blog is not active" );
00149                 die( $result );
00150         }
00151         
00152         // if everything went fine, load the plugins so that we can throw some events...
00153     $pm =& PluginManager::getPluginManager();
00154     $pm->loadPlugins();
00155         // and also configure the BlogInfo and UserInfo objects so that they know
00156         // who threw the events...
00157     $pm->setBlogInfo( $blogInfo );
00158         $userInfo = $blogInfo->getOwnerInfo();
00159     $pm->setUserInfo( $userInfo );
00160         
00161         // receives the request and adds it to the database
00162         $trackbacks = new TrackBacks();
00163         // create teh trackback object
00164         $now = new Timestamp();
00165         $trackback = new Trackback( $url, $title, $articleId, $excerpt, $blogName, $now->getTimestamp());
00166         // throw the event in case somebody is listening to it!
00167         $pm->notifyEvent( EVENT_PRE_TRACKBACK_ADD, Array( "trackback" => &$trackback ));
00168         $result = $trackbacks->addTrackBack( $trackback );
00169         if( !$result ) {
00170                 trackbackLog( "There was an error saving the trackback!" );
00171         }
00172         // throw the post event too...
00173         $pm->notifyEvent( EVENT_POST_TRACKBACK_ADD, Array( "trackback" => &$trackback ));
00174 
00175 
00176         // notify the user that a new trackback has been received, if the article was
00177         // configured to receive notifications
00178         // but first make sure, the trackback was not removed by some plugins like validatetrackback...
00179     if( $trackbacks->getArticleTrackback( $trackback->getId() ) ) {
00180         $notifier = new ArticleNotifications();
00181         $notifier->notifyUsers( $article->getId(), $blogInfo);
00182 
00183         // result
00184         $result = '<?xml version="1.0" encoding="iso-8859-1"?>';
00185         $result .= '<response>';
00186         $result .= '<error>0</error>';
00187         $result .= '</response>';
00188     } else {
00189         // result 
00190         $result = errorResponse( "Forbidden trackback" );
00191     }
00192         
00193         // clear the blog cache
00194         CacheControl::resetBlogCache( $article->getBlog());
00195 
00196     // return the result
00197     print($result);
00198 
00199     trackbackLog( "Sending response: $result" );
00200     trackbackLog( "** End" );
00201         
00202         die();
00203 ?>