Auth
[ class tree: Auth ] [ index: Auth ] [ all elements ]

Source for file Controller.php

Documentation is available at Controller.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Martin Jansen <mj@php.net>                                  |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Controller.php,v 1.8 2006/02/28 02:19:22 aashley Exp $
  20. //
  21.  
  22. /**
  23.  * Controlls access to a group of php access
  24.  * and redirects to a predefined login page as
  25.  * needed
  26.  *
  27.  * In all pages
  28.  * <code>
  29.  * include_once('Auth.php');
  30.  * include_once('Auth/Controller.php');
  31.  * $_auth = new Auth('File', 'passwd');
  32.  * $authController = new Auth_Controller($_auth, 'login.php', 'index.php');
  33.  * $authController->start();
  34.  * </code>
  35.  *
  36.  * In login.php
  37.  * <code>
  38.  * include_once('Auth.php');
  39.  * include_once('Auth/Controller.php');
  40.  * $_auth = new Auth('File', 'passwd');
  41.  * $authController = new Auth_Controller($_auth, 'login.php', 'index.php');
  42.  * $authController->start();
  43.  * if( $authController->isAuthorised() ){
  44.  *   $authController->redirectBack();
  45.  * }
  46.  * </code>
  47.  *
  48.  * @author   Yavor Shahpasov <yavo@netsmart.com.cy>
  49.  * @author   Adam Ashley <aashley@php.net>
  50.  * @package  Auth
  51.  * @version  $Revision: 1.8 $
  52.  */
  53. {
  54.  
  55.     // {{{ properties
  56.  
  57.     /** 
  58.      * The Auth instance this controller is managing
  59.      *
  60.      * @var object Auth 
  61.      */
  62.     var $auth = null;
  63.     
  64.     /**
  65.      * The login URL
  66.      * @var string 
  67.      *  */
  68.     var $login = null;
  69.     
  70.     /**
  71.      * The default index page to use when the caller page is not set
  72.      *
  73.      * @var string 
  74.      */
  75.     var $default = null;
  76.     
  77.     /** 
  78.      * If this is set to true after a succesfull login the
  79.      * Auth_Controller::redirectBack() is invoked automatically
  80.      *
  81.      * @var boolean 
  82.      */
  83.     var $autoRedirectBack = false;
  84.  
  85.     // }}}
  86.  
  87.     // {{{ Auth_Controller() [constructor]
  88.     
  89.     /**
  90.      * Constructor
  91.      *
  92.      * @param Auth An auth instance
  93.      * @param string The login page
  94.      * @param string The default page to go to if return page is not set
  95.      * @param array Some rules about which urls need to be sent to the login page
  96.      * @return void 
  97.      * @todo Add a list of urls which need redirection
  98.      */
  99.     function Auth_Controller(&$auth_obj$login='login.php'$default='index.php'$accessList=array())
  100.     {
  101.         $this->auth =$auth_obj;
  102.         $this->_loginPage $login;
  103.         $this->_defaultPage $default;
  104.         @session_start();
  105.         if (!empty($_GET['return']&& $_GET['return'&& !strstr($_GET['return']$this->_loginPage)) {
  106.             $this->auth->setAuthData('returnUrl'$_GET['return']);
  107.         }
  108.  
  109.         if(!empty($_GET['authstatus']&& $this->auth->status == ''{
  110.             $this->auth->status = $_GET['authstatus'];
  111.         }
  112.     }
  113.  
  114.     // }}}
  115.     // {{{ setAutoRedirectBack()
  116.     
  117.     /** 
  118.      * Enables auto redirection when login is done
  119.      * 
  120.      * @param bool Sets the autoRedirectBack flag to this
  121.      * @see Auth_Controller::autoRedirectBack
  122.      * @return void 
  123.      */
  124.     function setAutoRedirectBack($flag = true)
  125.     {
  126.         $this->autoRedirectBack = $flag;
  127.     }
  128.  
  129.     // }}}
  130.     // {{{ redirectBack()
  131.     
  132.     /**
  133.      * Redirects Back to the calling page
  134.      *
  135.      * @return void 
  136.      */
  137.     function redirectBack()
  138.     {
  139.         // If redirectback go there
  140.         // else go to the default page
  141.         
  142.         $returnUrl $this->auth->getAuthData('returnUrl');
  143.         if(!$returnUrl{
  144.             $returnUrl $this->_defaultPage;
  145.         }
  146.         
  147.         // Add some entropy to the return to make it unique
  148.         // avoind problems with cached pages and proxies
  149.         if(strpos($returnUrl'?'=== false{
  150.             $returnUrl .= '?';
  151.         }
  152.         $returnUrl .= uniqid('');
  153.  
  154.         // Track the auth status
  155.         if($this->auth->status != ''{
  156.             $url .= '&authstatus='.$this->auth->status;
  157.         }        
  158.         header('Location:'.$returnUrl);
  159.         print("You could not be redirected to <a href=\"$returnUrl\">$returnUrl</a>");
  160.     }
  161.  
  162.     // }}}
  163.     // {{{ redirectLogin()
  164.     
  165.     /**
  166.       * Redirects to the login Page if not authorised
  167.       * 
  168.       * put return page on the query or in auth
  169.       *
  170.       * @return void 
  171.       */
  172.     function redirectLogin()
  173.     {
  174.         // Go to the login Page
  175.         
  176.         // For Auth, put some check to avoid infinite redirects, this should at least exclude
  177.         // the login page
  178.         
  179.         $url $this->_loginPage;
  180.         if(strpos($url'?'=== false{
  181.             $url .= '?';
  182.         }
  183.  
  184.         if(!strstr($_SERVER['PHP_SELF']$this->_loginPage)) {
  185.             $url .= 'return='.urlencode($_SERVER['PHP_SELF']);
  186.         }
  187.  
  188.         // Track the auth status
  189.         if($this->auth->status != ''{
  190.             $url .= '&authstatus='.$this->auth->status;
  191.         }
  192.  
  193.         header('Location:'.$url);
  194.         print("You could not be redirected to <a href=\"$url\">$url</a>");
  195.     }
  196.  
  197.     // }}}
  198.     // {{{ start()
  199.     
  200.     /**
  201.       * Starts the Auth Procedure
  202.       *
  203.       * If the page requires login the user is redirected to the login page
  204.       * otherwise the Auth::start is called to initialize Auth
  205.       *
  206.       * @return void 
  207.       * @todo Implement an access list which specifies which urls/pages need login and which do not
  208.       */
  209.     function start()
  210.     {
  211.         // Check the accessList here
  212.         // ACL should be a list of urls with allow/deny
  213.         // If allow set allowLogin to false
  214.         // Some wild card matching should be implemented ?,*
  215.         if(!strstr($_SERVER['PHP_SELF']$this->_loginPage&& !$this->auth->checkAuth()) {
  216.             $this->redirectLogin();
  217.         else {
  218.             $this->auth->start();
  219.             // Logged on and on login page
  220.             if(strstr($_SERVER['PHP_SELF']$this->_loginPage&& $this->auth->checkAuth()){
  221.                 $this->autoRedirectBack ? 
  222.                     $this->redirectBack(:
  223.                     null ;
  224.             }
  225.         }
  226.         
  227.         
  228.     }
  229.  
  230.     // }}}
  231.     // {{{ isAuthorised()
  232.   
  233.     /**
  234.       * Checks is the user is logged on
  235.       * @see Auth::checkAuth()
  236.       */
  237.     function isAuthorised()
  238.     {
  239.         return($this->auth->checkAuth());
  240.     }
  241.  
  242.     // }}}
  243.     // {{{ checkAuth()
  244.  
  245.     /**
  246.       * Proxy call to auth
  247.       * @see Auth::checkAuth()
  248.       */
  249.     function checkAuth()
  250.     {
  251.         return($this->auth->checkAuth());
  252.     }
  253.  
  254.     // }}}
  255.     // {{{ logout()
  256.  
  257.     /**
  258.       * Proxy call to auth
  259.       * @see Auth::logout()
  260.       */
  261.     function logout()
  262.     {
  263.         return($this->auth->logout());
  264.     }
  265.  
  266.     // }}}
  267.     // {{{ getUsername()
  268.  
  269.     /**
  270.       * Proxy call to auth
  271.       * @see Auth::getUsername()
  272.       */
  273.     function getUsername()
  274.     {
  275.         return($this->auth->getUsername());
  276.     }
  277.  
  278.     // }}}
  279.     // {{{ getStatus()
  280.  
  281.     /**
  282.       * Proxy call to auth
  283.       * @see Auth::getStatus()
  284.       */
  285.     function getStatus()
  286.     {
  287.         return($this->auth->getStatus());
  288.     }
  289.  
  290.     // }}}
  291.  
  292. }
  293.  
  294. ?>

Documentation generated on Mon, 11 Mar 2019 14:37:16 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.