<?php
    # $Id: page.inc.php,v 1.5 2007/01/10 00:02:17 jarda Exp $

    /* Defines class Page - the superclass of all page classes
    */

    # load the configuration file
    require_once('config.inc.php');
    
    # load the database library
    require_once('database.inc.php');

    abstract class Page 
    {
        var $allow_view_for_anyone = 1;

        abstract function getRequiredAccessLevel();

        abstract function createPage();


        /* Custom initialization method - empty by default
        */
        function customInitialization()
        {
        }


        /* If the user is not logged in, jumps to the login page
        */
        function ensureUserIsLoggedIn()
        {
            $userInfo = $this->getUserInfo();

            if (!$this->userIsLoggedIn())
                $this->loginUser();
        }


        /* Finds out whether a user is logged in
        */
        function userIsLoggedIn()
        {
            if (!$this->getUserInfo())
                return False;
            else
                return True;
        }


        /* If the user cannot edit DB, displays an error message and exits the processing of the page
        */
        function ensureUserCanEditDB()
        {
            if (!$this->userCanEditDB())
            {
                echo "<h2>Access denied<h1>";
                exit();
            }
        }


        /* If the user cannot edit user accounts, displays an error message and
        * exits the processing of the page
        */
        function ensureUserCanEditUserAccounts()
        {
            if (!$this->userCanEditUserAccounts())
            {
                echo "<h2>Access denied<h1>";
                exit();
            }
        }


        /* If the user cannot edit own password, displays an error message and
        * exits the processing of the page
        */
        function ensureUserCanChangeOwnPassword()
        {
            if (!$this->userCanChangeOwnPassword())
            {
                echo "<h2>Access denied<h1>";
                exit();
            }
        }


        /* Finds out whether the user can edit benchmarks database
        */
        function userCanEditDB()
        {
            $userInfo = $this->getUserInfo();

            if (!$userInfo or !$userInfo['caneditdatabase'])
                return False;
            else
                return True;
        }


        /* Finds out whether the user can change own password
        */
        function userCanChangeOwnPassword()
        {
            $userInfo = $this->getUserInfo();

            if (!$userInfo or !$userInfo['canchangeownpassword'])
                return False;
            else
                return True;
        }


        /* Finds out whether the user can edit user accounts
        */
        function userCanEditUserAccounts()
        {
            $userInfo = $this->getUserInfo();
            
            if (!$userInfo or !$userInfo['canedituseraccounts'])
                return False;
            else
                return True;
        }


        /* Jumps to the login page
        */
        function loginUser()
        {
            header("Location: login.php"); /* Redirect browser */ 

            exit();
        }


        /* Returns info about the logged in user
        */
        function getUserInfo()
        {
            $db = connect();
            
            $sessionId = session_id();

            $query = "SELECT bDbUser.id AS userId, bDBUser.canEditDatabase AS canEditDatabase, " .
                     "bDBUser.canChangeOwnPassword AS canChangeOwnPassword, " .
                     "bDBUser.canEditUserAccounts AS canEditUserAccounts, " .
                     "bDBUser.password AS password " .
                     "FROM bDBUser, sessionUser " .
                     "WHERE sessionUser.sessionId = '$sessionId' AND " .
                     "bdbUser.id = sessionUser.userId";
                     
            $recordSet = $db->Execute($query);

            return $recordSet->FetchRow();
        }


        /* Ensures that the user has necessary permission (access level)
        */
        function ensureUserHasNecessaryPermission() {
            $requiredAccessLevel = $this->getRequiredAccessLevel();
            
            if ($requiredAccessLevel == 'view') { 
                if(!$this->allow_view_for_anyone)
                    $this->ensureUserIsLoggedIn();
            }
            elseif ($requiredAccessLevel == 'editDB')
                $this->ensureUserCanEditDB();
            elseif ($requiredAccessLevel == 'editUserAccounts')
                $this->ensureUserCanEditUserAccounts();
            elseif ($requiredAccessLevel == 'changeOwnPassword')
                $this->ensureUserCanChangeOwnPassword();
            else {
                echo "<h2>Access level info missing<h1>";
                exit();
            }
        }
        

        /* The main method of the page processing
        */
        function run()
        {
            $this->initSession();
            
            $this->ensureUserHasNecessaryPermission();
            
            $this->customInitialization();

            if (!$this->checkEnvironment())
                $this->handleInvalidEnvironment();
            else
                $this->createPage();
        }


        /* Checks sanity of the environment - empty by default
        */
        function checkEnvironment()
        {
            return True;
        }


        /* Initializes the session
        */
        function initSession()
        {
            session_name('benchmark'); 
            session_start();
        }


        /* Returns contents of HTML head
        */
        function getHead($title)
        {
            return "    <head>
        <meta http-equiv=\"content-type\" content=\"text/html\" charset=\"iso8859-2\">
        <title>$title</title>

        <style type=\"text/css\" media=\"screen\">
            div.container {
                overflow: hidden;
            }

            body {
                background-color:   white;
                color:              black;
                margin:             0px;
                padding:            0px;
            }
        
            div.leftcolumn {
                float:              left;
                /*height:             1000;*/
                width:              300;
                margin-right:       auto;
                background-color:   lightgray;
                margin-bottom: -2000px; 
                padding-bottom: 2000px;
                /*background: #ccc url(menu.png) repeat-y 50%0;*/
            }

            div.contents {
                background-color:   white;
                margin-left:        300;
                padding:            10;
            }
        </style>
    </head>";
        }
        

        /* Handles invalid environment - prints out some error message by default
        */
        function handleInvalidEnvironment()
        {
            $head = $this->getHead("Unexpected error");

            echo "<html>
$head
    <body>
    <h2>Invalid input or internal error</h2>
    </body>
<html>";

            exit();
        }
    }


    /* Creates and runs an instance of the page class
    */
    function launchPage()
    {
        # create the page class
        $pageObject = new PageClass();

        # run the page class
        $pageObject->run();
    }


    // vim:enc=iso8859-2:fenc=iso8859-2:
?>
