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 = 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(!empty($_GET['authstatus']&& $this->auth->status == ''{
  63.             $this->auth->status = $_GET['authstatus'];
  64.         }
  65.     }
  66.     
  67.     /** 
  68.       * Enables auto redirection when login is done
  69.       * 
  70.       * @param bool Sets the autoRedirectBack flag to this
  71.       * @see Auth_Controller::autoRedirectBack
  72.       *
  73.       */
  74.     function setAutoRedirectBack($flag = true){
  75.         $this->autoRedirectBack $flag;
  76.     }
  77.     
  78.     /**
  79.       * Redirects Back to the calling page
  80.       */
  81.     function redirectBack({
  82.         // If redirectback go there
  83.         // else go to the default page
  84.         
  85.         $returnUrl $this->auth->getAuthData('returnUrl');
  86.         if(!$returnUrl{
  87.             $returnUrl $this->_defaultPage;
  88.         }
  89.         
  90.         // Add some entropy to the return to make it unique
  91.         // avoind problems with cached pages and proxies
  92.         if(strpos($returnUrl'?'=== false{
  93.             $returnUrl .= '?';
  94.         }
  95.         $returnUrl .= uniqid('');
  96.  
  97.         // Track the auth status
  98.         if($this->auth->status != ''{
  99.             $url .= '&authstatus='.$this->auth->status;
  100.         }        
  101.         header('Location:'.$returnUrl);
  102.         print("You could not be redirected to <a href=\"$returnUrl\">$returnUrl</a>");
  103.     }
  104.     
  105.     /**
  106.       * Redirects to the login Page
  107.       * if not authorised
  108.       * 
  109.       * put return page on the query or in auth
  110.       */
  111.     function redirectLogin({
  112.         // Go to the login Page
  113.         
  114.         // For Auth, put some check to avoid infinite redirects, this should at least exclude
  115.         // the login page
  116.         
  117.         $url $this->_loginPage;
  118.         if(strpos($url'?'=== false{
  119.             $url .= '?';
  120.         }
  121.  
  122.         #print "ServerPhp:".$_SERVER['PHP_SELF'];
  123.         if(!strstr($_SERVER['PHP_SELF']$this->_loginPage)) {
  124.             $url .= 'return='.urlencode($_SERVER['PHP_SELF']);
  125.         }
  126.         // Track the auth status
  127.         if($this->auth->status != ''{
  128.             $url .= '&authstatus='.$this->auth->status;
  129.         }
  130.         header('Location:'.$url);
  131.         print("You could not be redirected to <a href=\"$url\">$url</a>");
  132.     }
  133.     
  134.     /**
  135.       * Starts the Auth Procedure
  136.       *
  137.       * If the page requires login the user is redirected to the login page
  138.       * otherwise the Auth::start is called to initialize Auth
  139.       *
  140.       * @todo Implement an access list which specifies which urls/pages need login and which do not
  141.       */
  142.     function start({
  143.         // Check the accessList here
  144.         // ACL should be a list of urls with allow/deny
  145.         // If allow set allowLogin to false
  146.         // Some wild card matching should be implemented ?,*
  147.         if(!strstr($_SERVER['PHP_SELF']$this->_loginPage&& !$this->auth->checkAuth()) {
  148.             $this->redirectLogin();
  149.         else {
  150.             $this->auth->start();
  151.             // Logged on and on login page
  152.             if(strstr($_SERVER['PHP_SELF']$this->_loginPage&& $this->auth->checkAuth()){
  153.                 $this->autoRedirectBack 
  154.                     $this->redirectBack(:
  155.                     null ;
  156.             }
  157.         }
  158.         
  159.         
  160.     }
  161.   
  162.     /**
  163.       * Checks is the user is logged on
  164.       * @see Auth::checkAuth()
  165.       */
  166.     function isAuthorised({
  167.         return($this->auth->checkAuth());
  168.     }
  169.  
  170.     /**
  171.       * Proxy call to auth
  172.       * @see Auth::checkAuth()
  173.       */
  174.     function checkAuth({
  175.         return($this->auth->checkAuth());
  176.     }
  177.  
  178.     /**
  179.       * Proxy call to auth
  180.       * @see Auth::logout()
  181.       */
  182.     function logout({
  183.         return($this->auth->logout());
  184.     }
  185.  
  186.     /**
  187.       * Proxy call to auth
  188.       * @see Auth::getUsername()
  189.       */
  190.     function getUsername({
  191.         return($this->auth->getUsername());
  192.     }
  193.  
  194.     /**
  195.       * Proxy call to auth
  196.       * @see Auth::getStatus()
  197.       */
  198.     function getStatus(){
  199.         return($this->auth->getStatus());
  200.     }
  201.  
  202.  
  203. }
  204.  
  205. ?>

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