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

Source for file Controller.php

Documentation is available at Controller.php

  1. <?php
  2.  
  3. /**
  4.   * Controlls access to a group of php access
  5.   * and redirects to a predefined login page as
  6.   * needed
  7.   *
  8.   * In all pages
  9.   * <code>
  10.   * include_once('Auth.php');
  11.   * include_once('Auth/Controller.php');
  12.   * $_auth = new Auth('File', 'passwd');
  13.   * $authController = new Auth_Controller($_auth, 'login.php', 'index.php');
  14.   * $authController->start();
  15.   * </code>
  16.   *
  17.   * In login.php
  18.   * <code>
  19.   * include_once('Auth.php');
  20.   * include_once('Auth/Controller.php');
  21.   * $_auth = new Auth('File', 'passwd');
  22.   * $authController = new Auth_Controller($_auth, 'login.php', 'index.php');
  23.   * $authController->start();
  24.   * if( $authController->isAuthorised() ){
  25.   *   $authController->redirectBack();
  26.   * }
  27.   * </code>
  28.   *
  29.   */
  30. class Auth_Controller {
  31.  
  32.     /** var Auth An auth instance */
  33.     var $auth_obj = null;
  34.     /** var string The login url */
  35.     var $login = null;
  36.     /** var string The default index page, used when login redirects and the caller page in not set or is the login page it's self */
  37.     var $default = null;
  38.     /** var bool If this is set to true auther a succesfull login the Auth_Controller::redirectBack() is invoked automatically */
  39.     var $autoRedirectBack = false;
  40.     
  41.     /**
  42.       * Constructor
  43.       *
  44.       * @param Auth An auth instance
  45.       * @param string The login page
  46.       * @param string The default page to go to if return page is not set
  47.       * @param array Some rules about which urls need to be sent to the login page
  48.       *
  49.       * @todo Add a list of urls which need redirection
  50.       */
  51.     function Auth_Controller(&$auth_obj$login='login.php'$default='index.php'$accessList=array()) {
  52.         #print $auth_obj;
  53.         $this->auth =$auth_obj;
  54.         $this->_loginPage $login;
  55.         $this->_defaultPage $default;
  56.         @session_start();
  57.         if (!empty($_GET['return']&& $_GET['return'&& !strstr($_GET['return']$this->_loginPage)) {
  58.             #print "Return: {$_GET['return']} <br/>";
  59.             $this->auth->setAuthData('returnUrl'$_GET['return']);
  60.         }
  61.         
  62.         //if()
  63.     }
  64.     
  65.     /** 
  66.       * Enables auto redirection when login is done
  67.       * 
  68.       * @param bool Sets the autoRedirectBack flag to this
  69.       * @see Auth_Controller::autoRedirectBack
  70.       *
  71.       */
  72.     function setAutoRedirectBack($flag = true){
  73.         $this->autoRedirectBack $flag;
  74.     }
  75.     
  76.     /**
  77.       * Redirects Back to the calling page
  78.       */
  79.     function redirectBack({
  80.         // If redirectback go there
  81.         // else go to the default page
  82.         
  83.         $returnUrl $this->auth->getAuthData('returnUrl');
  84.         if(!$returnUrl{
  85.             $returnUrl $this->_defaultPage;
  86.         }
  87.         
  88.         // Add some entropy to the return to make it unique
  89.         // avoind problems with cached pages and proxies
  90.         if(strpos($returnUrl'?'=== false{
  91.             $returnUrl .= '?';
  92.         }
  93.         $returnUrl .= uniqid('');
  94.         
  95.         header('Location:'.$returnUrl);
  96.         print("You could not be redirected to <a href=\"$returnUrl\">$returnUrl</a>");
  97.     }
  98.     
  99.     /**
  100.       * Redirects to the login Page
  101.       * if not authorised
  102.       * 
  103.       * put return page on the query or in auth
  104.       */
  105.     function redirectLogin({
  106.         // Go to the login Page
  107.         
  108.         // For Auth, put some check to avoid infinite redirects, this should at least exclude
  109.         // the login page
  110.         
  111.         $url $this->_loginPage;
  112.         if(strpos($url'?'=== false{
  113.             $url .= '?';
  114.         }
  115.  
  116.         #print "ServerPhp:".$_SERVER['PHP_SELF'];
  117.         if(!strstr($_SERVER['PHP_SELF']$this->_loginPage)) {
  118.             $url .= 'return='.urlencode($_SERVER['PHP_SELF']);
  119.         }
  120.         header('Location:'.$url);
  121.         print("You could not be redirected to <a href=\"$url\">$url</a>");
  122.     }
  123.     
  124.     /**
  125.       * Starts the Auth Procedure
  126.       *
  127.       * If the page requires login the user is redirected to the login page
  128.       * otherwise the Auth::start is called to initialize Auth
  129.       *
  130.       * @todo Implement an access list which specifies which urls/pages need login and which do not
  131.       */
  132.     function start({
  133.         // Check the accessList here
  134.         // ACL should be a list of urls with allow/deny
  135.         // Some wild card matching should be implemented ?,*
  136.         if(!strstr($_SERVER['PHP_SELF']$this->_loginPage&& !$this->auth->checkAuth()) {
  137.             $this->redirectLogin();
  138.         else {
  139.             $this->auth->start();
  140.             // Logged on and on login page
  141.             if(strstr($_SERVER['PHP_SELF']$this->_loginPage&& $this->auth->checkAuth()){
  142.                 // Should we call this here
  143.                 // or in the login page manually
  144.                 $this->autoRedirectBack 
  145.                     $this->redirectBack(:
  146.                     null ;
  147.             }
  148.         }
  149.         
  150.         
  151.     }
  152.   
  153.     /**
  154.       * Checks is the user is logged on
  155.       * @see Auth::checkAuth()
  156.       */
  157.     function isAuthorised({
  158.         return($this->auth->checkAuth());
  159.     }
  160.  
  161.     /**
  162.       * Proxy call to auth
  163.       * @see Auth::checkAuth()
  164.       */
  165.     function checkAuth({
  166.         return($this->auth->checkAuth());
  167.     }
  168.  
  169.     /**
  170.       * Proxy call to auth
  171.       * @see Auth::logout()
  172.       */
  173.     function logout({
  174.         return($this->auth->logout());
  175.     }
  176.  
  177.     /**
  178.       * Proxy call to auth
  179.       * @see Auth::getUsername()
  180.       */
  181.     function getUsername({
  182.         return($this->auth->getUsername());
  183.     }
  184.  
  185.     /**
  186.       * Proxy call to auth
  187.       * @see Auth::getStatus()
  188.       */
  189.     function getStatus(){
  190.         return($this->auth->getStatus());
  191.     }
  192.  
  193.  
  194. }
  195.  
  196. ?>

Documentation generated on Mon, 11 Mar 2019 13:52:32 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.