<?php

	include_once( PLOG_CLASS_PATH."class/plugin/pluginbase.class.php" );
    include_once( PLOG_CLASS_PATH."class/plugin/pluginmanager.class.php" );

    /**
     * Plugin that is used to limit display of text to only the person who posted the blog
     * entry when he or she has logged in. To test, add the example to a template, then 
     * login to view the admin pages, then return to a blog page and the hidden text will 
     * be visible.
     *
     * It is exported with "auth" as its identifier.
     *
     * Usage:
     *
     * 		{assign var=poster value=$post->getUserInfo()}
     * 		{if $auth->posterIsLoggedIn($poster)}
     * 		Poster: {$auth->getPosterName($poster)}
     * 		User: {$auth->getUserName()}
     * 		{/if}
     */
    class PluginAuthentication extends PluginBase {

    	function PluginAuthentication()
        {
        	$this->PluginBase();

            $this->desc = "Used to limit display of text to only 
            	the person who posted the blog entry when he or she has logged in.
            	To test, add the example to a template, then login to view the admin pages,
                then return to a blog page and the hidden text will be visible.
                Usage:<br/><br/>
                {assign var=poster value=\$post->getUserInfo()}<br/>
				{if \$auth->posterIsLoggedIn(\$poster)}<br/>
				Poster: {\$auth->getPosterName(\$poster)}<br/>
				User: {\$auth->getUserName()}<br/>
				{/if}";
            $this->author = "Daniel Kehoe <kehoe@fortuity.com>";
        }

        /**
         * Returns "true" if the person who posted has logged in.
         *
         * @param object of type UserInfo
         * @return true or false
         */
        function posterIsLoggedIn($poster)
        { 	
        	$allow = false;
         	$sessionInfo = $_SESSION["SessionInfo"];
			$userInfo = $sessionInfo->getValue("userInfo");
			if (isset($userInfo)) {
				if ($userInfo->getId() == $poster->getId()) $allow = true;
			}
        	return $allow;
        }

        /**
         * Returns the name of the person who posted.
         *
         * @param object of type UserInfo
         * @return poster's name
         */        
        function getPosterName($poster)
        { 	
        	return $poster->getUsername();
        }

        /**
         * Returns the name of the person who is logged in.
         *
         * @return user's name
         */          
		function getUserName()
        { 	
        	$name = 'unknown';
        	$sessionInfo = $_SESSION["SessionInfo"];
			$userInfo = $sessionInfo->getValue("userInfo");
			if (isset($userInfo)) $name = $userInfo->getUsername();
        	return $name;
        }
    }

    PluginManager::registerPlugin( "auth", "PluginAuthentication" );
?>
