Location: PHPKode > projects > Stratos > stratos-1.0rc1/plugins/QuickAuth/controllers/QuickAuthController.php
<?php

// ------------------------------------------------------------
// Stratos PHP Framework
// Copyright (c) 2006-2007 Sephira Software, LLC
// 
// This file is subject to the Stratos PHP Framework license
// which you should have received along with this file. The
// license is also accessible on the web at the following URI:
//   http://www.stratosframework.com/wiki/Manual/License
// If you did not receive a copy of the Stratos PHP Framework
// license or you are unable to obtain it through the web,
// please send an e-mail to hide@address.com so a copy
// can be sent to you.
// ------------------------------------------------------------

/**
 * @author Joshua Carnett
 * @copyright Copyright (c) 2006-2007 Sephira Software, LLC
 * @license http://www.stratosframework.com/wiki/Manual/License
 * @package QuickAuth
 * @subpackage actions
 */

/**
 * QuickAuthController
 * 
 * @package QuickAuth
 * @subpackage actions
 */
class QuickAuthController
{
    var $qap;
    var $conf;
    
    function start()
    {
        Stratos::setMasterView('Stratos:cp-layout.php');
        
        $this->qap =& Stratos::getPlugin('QuickAuth');
        $this->conf = $this->qap->getConfig();
        
        return array(
            'qap' => $this->qap,
            'conf' => $this->conf
        );
    }
    
    function index()
    {
        return Stratos::forward('QuickAuth/getUsers');
    }
    
    function login( $QuickAuth_user = null, $QuickAuth_pass = null )
    {
        Stratos::enableMasterView(false);
        Stratos::setView('QuickAuth:QuickAuthController.login.php');
        
        if ( count($this->conf['users']) == 1
            && isset($this->conf['users']['admin']) )
        {
            Stratos::putFlash('This appears to be your first time logging in. '
                . 'The default username and password are both "admin". You '
                . 'should change this after logging in by going to the '
                . 'QuickAuth control panel.', 'notice');
        }
        
        if ( $QuickAuth_user && $QuickAuth_pass )
        {
            $hash = md5($QuickAuth_pass);
            
            if ( isset($this->conf['users'][$QuickAuth_user])
                && $this->conf['users'][$QuickAuth_user] == $hash )
            {
                $_SESSION['QuickAuth_user_' . QUICK_AUTH_SITE_ID] = $QuickAuth_user;
                $_SESSION['QuickAuth_pass_' . QUICK_AUTH_SITE_ID] = $hash;
                
                if ( isset($_SESSION['QuickAuth_from_' . QUICK_AUTH_SITE_ID]) )
                {
                    Stratos::redirect($_SESSION['QuickAuth_from_' . QUICK_AUTH_SITE_ID]);
                }
                else
                {
                    Stratos::redirect('./index.php');
                }
            }
            else
            {
                unset($_SESSION['QuickAuth_user_' . QUICK_AUTH_SITE_ID]);
                unset($_SESSION['QuickAuth_pass_' . QUICK_AUTH_SITE_ID]);
                
                Stratos::putFlash('Invalid username and/or password.', 'error');
            }
        }
    }
    
    function logout()
    {
        Stratos::enableMasterView(false);
        Stratos::setView('QuickAuth:QuickAuthController.logout.php');
        
        unset($_SESSION['QuickAuth_user_' . QUICK_AUTH_SITE_ID]);
        unset($_SESSION['QuickAuth_pass_' . QUICK_AUTH_SITE_ID]);
        
        if ( strpos($_SESSION['QuickAuth_from_' . QUICK_AUTH_SITE_ID], 'QuickAuth/login') !== false )
        {
            Stratos::redirect($_SESSION['QuickAuth_from_' . QUICK_AUTH_SITE_ID]);
        }
        else
        {
            Stratos::redirect('./index.php');
        }
    }
    
    function getUsers()
    {
        Stratos::setView('QuickAuth:QuickAuthController.getUsers.php');
        
        $users = array_keys($this->conf['users']);
        if ( !$users ) $users = array();
        
        sort($users);
        
        return array(
            'users' => $users
        );
    }
    
    function addUser( $name = null, $password = null )
    {
        if ( $name && $password )
        {
            if ( !isset($this->conf['users'][$name]) )
            {
                $this->conf['users'][$name] = md5($password);
                
                $sconf = Stratos::getConfig();
                $sconf->set('QuickAuth', $this->conf);
                $res = $sconf->save();
                
                if ( $res )
                {
                    Stratos::putFlash('User "' . $name . '" has been added '
                        . 'successfully.', 'success');
                }
                else
                {
                    Stratos::putFlash('An error occurred while writing the '
                        . 'configuration file. Please make sure '
                        . 'that your PHP configuration allows the file to be '
                        . 'written.', 'error');
                }
            }
            else
            {
                Stratos::putFlash('The user "' . $name . '" already exists.',
                    'error');
            }
        }
        else
        {
            Stratos::putFlash('You must supply both a user name and a password '
                . 'to add a user.', 'error');
        }
        
        return $this->getUsers();
    }
    
    function removeUser( $name, $confirm = true )
    {
        if ( isset($this->conf['users'][$name]) )
        {
            if ( count($this->conf['users']) > 1 )
            {
                if ( $confirm )
                {
                    unset($this->conf['users'][$name]);
                    
                    $sconf = Stratos::getConfig();
                    $sconf->set('QuickAuth', $this->conf);
                    $res = $sconf->save();
                    
                    if ( $res )
                    {
                        Stratos::putFlash('User "' . $name . '" removed '
                            . 'successfully.', 'success');
                    }
                    else
                    {
                        Stratos::putFlash('An error occurred while writing the '
                            . 'configuration file. Please make sure '
                            . 'that your PHP configuration allows this file to '
                            . 'be written.', 'error');
                    }
                }
            }
            else
            {
                Stratos::putFlash('Because this is the last user, you may not '
                    . 'delete it without first creating another.', 'error');
            }
        }
        else
        {
            Stratos::putFlash('That is not a valid user name.', 'error');
        }
        
        Stratos::redirect('QuickAuth/getUsers');
    }
}

?>
Return current item: Stratos