Location: PHPKode > projects > iF.SVNAdmin > svnadmin/classes/core/Engine.class.php
<?php
/**
 * iF.SVNAdmin
 * Copyright (c) 2010 by Manuel Freiholz
 * http://www.insanefactory.com/
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; version 2
 * of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.
 */
namespace svnadmin\core {

class Engine
{
	// Holds the single instance of it selfs.
	static private $m_instance = NULL;

  /**
   * The global configuration.
   * @var IF_IniFile
   */
  private $m_config = NULL;

	// User view provider.
	private $m_userViewProvider = NULL;

	// User edit provider.
	private $m_userEditProvider = NULL;

	// Group view provider.
	private $m_groupViewProvider = NULL;

	// Group edit provider.
	private $m_groupEditProvider = NULL;
	
	// Path view provider.
	private $m_pathViewProvider = NULL;
	
	// Path edit provider.
	private $m_pathEditProvider = NULL;

  // Repository view provider.
  private $m_repositoryViewProvider = NULL;

  // Repository edit provider.
  private $m_repositoryEditProvider = NULL;
  
  // User authenticatior.
  private $m_authenticator = NULL;

  // The ACL manager.
  private $m_acl_manager = NULL;
  
  // Indicates whether one of the IViewProvider's is updateable.
  private $m_view_is_updateable = false;

	/**
	 * Private constructor, because the class is a singelton instance.
	 * @return Engine
	 */
	private function __construct()
	{
    // Load the global configuration.
    $this->m_config = new \IF_IniFile();
    $this->m_config->loadFromFile("./data/config.ini");
  }

  /**
  * Gets the singelton instance of this class.
  * @return Engine
  */
  public static function getInstance()
  {
    if( self::$m_instance == NULL )
    {
      self::$m_instance = new Engine;
    }
    return self::$m_instance;
  }

  public function getAppVersionString()
  {
    return \MAJOR_VERSION.".".\MINOR_VERSION." ".\VERSION_EXTRA;
  }

  /**
   * Gets the global configuration object.
   * @return IF_IniFile
   */
  public function getConfig()
  {
    return $this->m_config;
  }
  
  /**
   * Init global templates variables of the given template.
   * @param IF_Template
   * @return void
   */
  public function initGlobalTemplateVariables( $tpl )
  {
    if( $tpl == NULL )
    { return; }

    // Web-GUI version.
    $tpl->addReplacement("APPVERSION", self::getAppVersionString());

    // Current script file name.
    $tpl->addReplacement("SCRIPT_NAME", currentScriptFileName());

    // Authentication.
    if (self::isAuthenticationActive())
    {
      $tpl->addDefine("AUTHENTICATION_ACTIVE");
    }

    // ACL manager.
    if (self::isAclManagerActive())
    {
      $tpl->addDefine("AUTHENTICATION_ACL_ACTIVE");
    }

    // User provider.
    if( self::isUserViewActive() )
    {
      $tpl->addDefine("USER_VIEW_ACTIVE");
      if( $this->m_userViewProvider->isUpdateable() )
      {
        $this->m_view_is_updateable = true;
      }
    }
    if( self::isUserEditActive() )
    {
      $tpl->addDefine("USER_EDIT_ACTIVE");
    }

    // Group provider.
    if( self::isGroupViewActive() )
    {
      $tpl->addDefine("GROUP_VIEW_ACTIVE");
      if( $this->m_groupViewProvider->isUpdateable() )
      {
        $this->m_view_is_updateable = true;
      }
    }
    if( self::isGroupEditActive() )
    {
      $tpl->addDefine("GROUP_EDIT_ACTIVE");
    }

    // Access path provider.
    if( self::isAccessPathViewActive() )
    {
      $tpl->addDefine("ACCESSPATH_VIEW_ACTIVE");
      if( $this->m_pathViewProvider->isUpdateable() )
      {
        $this->m_view_is_updateable = true;
      }
    }
    if( self::isAccessPathEditActive() )
    {
      $tpl->addDefine("ACCESSPATH_EDIT_ACTIVE");
    }

    // Repository provider.
    if( self::isRepositoryViewActive() )
    {
      $tpl->addDefine("REPOSITORY_VIEW_ACTIVE");
      if( $this->m_repositoryViewProvider->isUpdateable() )
      {
        $this->m_view_is_updateable = true;
      }
    }
    if( self::isRepositoryEditActive() )
    {
      $tpl->addDefine("REPOSITORY_EDIT_ACTIVE");
    }

    // Is any of the providers updateable?
    if( $this->m_view_is_updateable )
    {
      $tpl->addDefine("VIEW_IS_UPDATEABLE");
    }
  }
  
  /**
   * Indicates, whether one of the views is updateable.
   *
   * @return bool
   */
  public function isViewUpdateable()
  {
    return $this->m_view_is_updateable;
  }
  
  public function forwardInvalidModule( $forward = true )
  {
    if( $forward )
    {
      header("Location: error.php?e=inactive_module");
    }
  }
  
  /**
   * Deletes the group and removes all associations of this group.
   *
   * @param Group $group
   * @return bool
   */
  public function deleteGroup($group)
  {
    if( self::getAccessPathEditProvider()->removeGroupFromAllAccessPaths($group) )
    {
      if( self::getGroupEditProvider()->deleteGroup($group) )
      {
        self::getAccessPathEditProvider()->save();
        self::getGroupEditProvider()->save();
        return true;
      }
    }
    return false;
  }
  
  /**
   * Loads the given action file and handles it.
   *
   * @param string $action The name of the action
   * @return void
   */
  public function handleAction( $action )
  {
    global $appEngine;
    global $appTemplate;
    global $appTR;
    $appTR->loadModule("actions");
    
    if( !defined('ACTION_HANDLING') )
    {
      define('ACTION_HANDLING', true);
    }

    $filename = 'actions/'.$action.'.php';
    if( file_exists( $filename ) )
    {
      $code = file_get_contents( $filename );
      eval(' ?>'.$code.'<?php ');
    }
  }
  
  /**
   * This function should take place on every page, where an authentication is required.
   * It authenticates the user.
   */
  public function checkUserAuthentication($redirect=true, $module=null, $action=null)
  {
    global $appTemplate;
    
    if( !self::isAuthenticationActive() )
    {
      // Add the required ACL defines for the authentication request.
      if ($module != null && $action != null)
        $appTemplate->addDefine("ACL_".strtoupper($module)."_".strtoupper($action));

      // The authentication is turned off.
      return true;
    }
    
    // At this place the authentication is ON.
    if( !isset($_SESSION["svnadmin_username"]) || empty($_SESSION["svnadmin_username"]) )
    {
      // The user is not logged in.
      if( $redirect )
      {
        header('Location: login.php');
        exit(1); // Fixed(1.4): Stop script execution.
      }
      return false;
    }

    // At this place the user is logged in.
    $appTemplate->addDefine("LOGGED_IN");
    $appTemplate->addReplacement("SESSION_USERNAME",$_SESSION["svnadmin_username"]);

    // Check acl permissions.
    if ($this->m_acl_manager !== null && $module !== null && $action !== null)
    {
      $b = self::checkUserAccess($module, $action);
      if (!$b)
      {
        // No permission.
        if ($redirect)
        {
          header('Location: noaccess.php?m='.$module.'&a='.$action.'');
          exit(1);
        }
        return false;
      }
      else
      {
        // Permission granted!
        $appTemplate->addDefine("ACL_".strtoupper($module)."_".strtoupper($action));
      }
    }
    return true;
  }

  /**
   * Checks whether the current logged in user has access.
   * @param string $module
   * @param string $action
   * @return bool
   */
  public function checkUserAccess($module, $action)
  {
    if (isset($_SESSION["svnadmin_username"]))
    {
      $u = new \svnadmin\core\entities\User();
      $u->name = $_SESSION["svnadmin_username"];
      return self::getAclManager()->hasPermission($u, $module, $action);
    }
    return false;
  }

  public function getSessionUsername()
  {
    if (isset($_SESSION["svnadmin_username"]))
    {
      return $_SESSION["svnadmin_username"];
    }
    return NULL;
  }

  public function hasPermission($module, $action)
  {
    return self::checkUserAccess($module, $action);
  }

  
  /**
   * Checks whether the authentication module is active.
   * @return bool
   */
  public function isAuthenticationActive()
  {
    return $this->m_authenticator == NULL ? false : true;
  }
  
  /**
   * Sets the user authenticator for the application.
   * @param IAuthenticator $objAuthenticator The authenticator implementation class.
   */
  public function setAuthenticator( $objAuthenticator )
  {
    $this->m_authenticator = $objAuthenticator;
  }
  
  /**
   * Gets the user authenticator for the application
   * @return IAuthenticator
   */
  public function getAuthenticator()
  {
    if( $this->m_authenticator != NULL )
    {
      $this->m_authenticator->init();
    }
    return $this->m_authenticator;
  }

  public function isAclManagerActive()
  {
    if (self::isAuthenticationActive())
      if ($this->m_acl_manager != NULL)
        return true;
    return false;
  }

  /**
   * Sets the ACL manager for the application.
   * @param IAclManager $aclManager The ACL manager instance.
   */
  public function setAclManager(\svnadmin\core\interfaces\IAclManager $aclManager)
  {
    $this->m_acl_manager = $aclManager;
  }

  /**
   * Gets the ACL manager of the application.
   * @return IAclManager
   */
  public function getAclManager()
  {
    if ($this->m_acl_manager != null)
    {
      $this->m_acl_manager->init();
    }
    return $this->m_acl_manager;
  }
  
  /**
   * Checks whether the user-view is active.
   * @return bool
   */
  public function isUserViewActive()
  {
    return $this->m_userViewProvider == NULL ? false : true;
  }
  
  /**
   * Sets the user view provider.
   * @param IUserViewProvider
   */
  public function setUserViewProvider( $o )
  {
    $this->m_userViewProvider = $o;
  }
  
  /**
   * Gets the user view provider.
   * @return IUserViewProvider or NULL
   */
  public function getUserViewProvider()
  {
    if( $this->m_userViewProvider != NULL )
    {
      $this->m_userViewProvider->init();
    }
    return $this->m_userViewProvider;
  }
  
  /**
   * Checks whether the user-edit is active.
   * @return bool
   */
  public function isUserEditActive()
  {
    return $this->m_userEditProvider == NULL ? false : true;
  }
  
  /**
   * Sets the user edit provider.
   * @param IUserEditProvider
   */
  public function setUserEditProvider( $o )
  {
    $this->m_userEditProvider = $o;
  }
  
  /**
   * Gets the user edit provider.
   * @return IUserEditProvider
   */
  public function getUserEditProvider()
  {
    if( $this->m_userEditProvider != NULL )
    {
      $this->m_userEditProvider->init();
    }
    return $this->m_userEditProvider;
  }
  
  public function isGroupViewActive()
  {
    return $this->m_groupViewProvider == NULL ? false : true;
  }
  
  public function setGroupViewProvider( $o )
  {
    $this->m_groupViewProvider = $o;
  }
  
  public function getGroupViewProvider()
  {
    if( $this->m_groupViewProvider != NULL )
    {
      $this->m_groupViewProvider->init();
    }
    return $this->m_groupViewProvider;
  }
  
  public function isGroupEditActive()
  {
    return $this->m_groupEditProvider == NULL ? false : true;
  }
  
  public function setGroupEditProvider( $o )
  {
    $this->m_groupEditProvider = $o;
  }
  
  public function getGroupEditProvider()
  {
    if( $this->m_groupEditProvider != NULL )
    {
      $this->m_groupEditProvider->init();
    }
    return $this->m_groupEditProvider;
  }
  
  /////
  
  public function isAccessPathViewActive()
  {
    return $this->m_pathViewProvider == NULL ? false : true;
  }
  
  public function setAccessPathViewProvider( $o )
  {
    $this->m_pathViewProvider = $o;
  }
  
  public function getAccessPathViewProvider()
  {
    if( $this->m_pathViewProvider != NULL )
    {
      $this->m_pathViewProvider->init();
    }
    return $this->m_pathViewProvider;
  }
  
  public function isAccessPathEditActive()
  {
    return $this->m_pathEditProvider == NULL ? false : true;
  }
  
  public function setAccessPathEditProvider( $o )
  {
    $this->m_pathEditProvider = $o;
  }
  
  public function getAccessPathEditProvider()
  {
    if( $this->m_pathEditProvider != NULL )
    {
      $this->m_pathEditProvider->init();
    }
    return $this->m_pathEditProvider;
  }

  /////

  public function isRepositoryViewActive()
  {
    return $this->m_repositoryViewProvider == NULL ? false : true;
  }

  public function setRepositoryViewProvider( $o )
  {
    $this->m_repositoryViewProvider = $o;
  }

  public function getRepositoryViewProvider()
  {
    if( $this->m_repositoryViewProvider != NULL )
    {
      $this->m_repositoryViewProvider->init();
    }
    return $this->m_repositoryViewProvider;
  }

  /////

  public function isRepositoryEditActive()
  {
    return $this->m_repositoryEditProvider == NULL ? false : true;
  }

  public function setRepositoryEditProvider( $o )
  {
    $this->m_repositoryEditProvider = $o;
  }

  public function getRepositoryEditProvider()
  {
    if( $this->m_repositoryEditProvider != NULL )
    {
      $this->m_repositoryEditProvider->init();
    }
    return $this->m_repositoryEditProvider;
  }
}

} // namespace
?>
Return current item: iF.SVNAdmin