<?php

    include_once( PLOG_CLASS_PATH."class/security/pipelinefilter.class.php" );
    include_once( PLOG_CLASS_PATH."class/template/template.class.php" );

    class SecretBlogFilter extends PipelineFilter 
    {

        function SecretBlogFilter( $pipelineRequest )
        {
            $this->PipelineFilter( $pipelineRequest );
        }    

        function filter()
        {        
            // get some info
            $blogInfo     = $this->_pipelineRequest->getBlogInfo();
            $blogSettings = $blogInfo->getSettings();
            $request      = $this->_pipelineRequest->getHttpRequest();
			$session      = HttpVars::getSession();
            
        	// is the plugin enabled? If not, we can quit right away
        	if( !$blogSettings->getValue( "plugin_secretblog_enabled" )) {
	            $result =  new PipelineResult( true );
    	        return( $result );
        	}
            
            // if this is already rejected, there is no reason to do anything here
            if ( $this->_pipelineRequest->getRejectedState())
                return new PipelineResult();
                
            // there are three possible situations:
            // - user not authenticated
            // - user not authenticated but blogPassword parameter in the request
            // - user authenticated
            if( $this->isUserAuthenticated( $blogInfo )) { 	          	
	            $result =  new PipelineResult( true );            
            }
            else {
            	// is there a "blogPassword" parameter in the session?
            	$password = $request->getValue( "blogPassword" );
            	if( $password ) {
            		// validate the password compared to whatever was stored in the blog settings
            		$blogSettings = $blogInfo->getSettings();
            		$blogPassword = $blogSettings->getValue( "plugin_secretblog_password" );
            		if( $blogPassword == md5( $password )) {
            			// and return everything ok...
            			$result = new PipelineResult( true );
            			// set the password in the session
            			$session["blogPassword"] = $blogPassword;
						HttpVars::setSession( $session );
            		}
            		else {
            			// password wasn't correct, try again!
						$result = new PipelineResult( false, 600, "Error" );
						$result->setView( $this->getPasswordFormView( $blogInfo ));
            		}
            	}
            	else {
            		// no password, no authentication info in the session!
					$result = new PipelineResult( false, 601, "Error" );
					$result->setView( $this->getPasswordFormView( $blogInfo ));                        	
            	}            
            }

			return( $result );
        }
        
        /**
         * @private
         * Returns true if the user has already been authenticated for this session
         */
        function isUserAuthenticated( $blogInfo )
        {
        	// and look for our information
			$session = HttpVars::getSession();
        	if( isset( $session["blogPassword"] )) {
        		$sessionPassword = $session["blogPassword"];
        		$blogSettings = $blogInfo->getSettings();
        		$blogPassword = $blogSettings->getValue( "plugin_secretblog_password" );
        		if( $sessionPassword == $blogPassword )
        			return true;
        		else
        			return false;
        	}
        	else
        		return false;
        }
        
        /**
         * @private
         * Shows the authentication form
         */
        function getPasswordFormView( $blogInfo )
        {
			include_once( PLOG_CLASS_PATH."plugins/secretblog/class/view/secretblogpluginpasswordformview.class.php" );	
			$view = new SecretBlogPluginPasswordFormView ($blogInfo );
			$view->setValue( "locale", $blogInfo->getLocale() );
			$view->setValue( "url", $blogInfo->getBlogRequestGenerator());
			$view->setValue( "blog", $blogInfo );
			
			return( $view );
        }
	}
?>