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

Documentation generated on Mon, 11 Mar 2019 10:17:23 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.