2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
9 * LICENSE: This source file is subject to version 3.01 of the PHP license
10 * that is available through the world-wide-web at the following URI:
11 * http://www.php.net/license/3_01.txt. If you did not receive a copy of
12 * the PHP License and are unable to obtain it through the web, please
13 * send a note to license@php.net so we can mail you a copy immediately.
15 * @category Authentication
17 * @author Yavor Shahpasov <yavo@netsmart.com.cy>
18 * @author Adam Ashley <aashley@php.net>
19 * @copyright 2001-2006 The PHP Group
20 * @license http://www.php.net/license/3_01.txt PHP License 3.01
21 * @version CVS: $Id: Controller.php 237449 2007-06-12 03:11:27Z aashley $
22 * @link http://pear.php.net/package/Auth
23 * @since File available since Release 1.3.0
27 * Controlls access to a group of php access
28 * and redirects to a predefined login page as
33 * include_once('Auth.php');
34 * include_once('Auth/Controller.php');
35 * $_auth = new Auth('File', 'passwd');
36 * $authController = new Auth_Controller($_auth, 'login.php', 'index.php');
37 * $authController->start();
42 * include_once('Auth.php');
43 * include_once('Auth/Controller.php');
44 * $_auth = new Auth('File', 'passwd');
45 * $authController = new Auth_Controller($_auth, 'login.php', 'index.php');
46 * $authController->start();
47 * if( $authController->isAuthorised() ){
48 * $authController->redirectBack();
52 * @category Authentication
53 * @author Yavor Shahpasov <yavo@netsmart.com.cy>
54 * @author Adam Ashley <aashley@php.net>
55 * @copyright 2001-2006 The PHP Group
56 * @license http://www.php.net/license/3_01.txt PHP License 3.01
57 * @version Release: @package_version@ File: $Revision: 237449 $
58 * @link http://pear.php.net/package/Auth
59 * @since Class available since Release 1.3.0
67 * The Auth instance this controller is managing
80 * The default index page to use when the caller page is not set
87 * If this is set to true after a succesfull login the
88 * Auth_Controller::redirectBack() is invoked automatically
92 var $autoRedirectBack = false;
95 // {{{ Auth_Controller() [constructor]
100 * @param Auth An auth instance
101 * @param string The login page
102 * @param string The default page to go to if return page is not set
103 * @param array Some rules about which urls need to be sent to the login page
105 * @todo Add a list of urls which need redirection
107 function Auth_Controller(&$auth_obj, $login='login.php', $default='index.php', $accessList=array())
109 $this->auth =& $auth_obj;
110 $this->_loginPage = $login;
111 $this->_defaultPage = $default;
113 if (!empty($_GET['return']) && $_GET['return'] && !strstr($_GET['return'], $this->_loginPage)) {
114 $this->auth->setAuthData('returnUrl', $_GET['return']);
117 if(!empty($_GET['authstatus']) && $this->auth->status == '') {
118 $this->auth->status = $_GET['authstatus'];
123 // {{{ setAutoRedirectBack()
126 * Enables auto redirection when login is done
128 * @param bool Sets the autoRedirectBack flag to this
129 * @see Auth_Controller::autoRedirectBack
132 function setAutoRedirectBack($flag = true)
134 $this->autoRedirectBack = $flag;
138 // {{{ redirectBack()
141 * Redirects Back to the calling page
145 function redirectBack()
147 // If redirectback go there
148 // else go to the default page
150 $returnUrl = $this->auth->getAuthData('returnUrl');
152 $returnUrl = $this->_defaultPage;
155 // Add some entropy to the return to make it unique
156 // avoind problems with cached pages and proxies
157 if(strpos($returnUrl, '?') === false) {
160 $returnUrl .= uniqid('');
162 // Track the auth status
163 if($this->auth->status != '') {
164 $url .= '&authstatus='.$this->auth->status;
166 header('Location:'.$returnUrl);
167 print("You could not be redirected to <a href=\"$returnUrl\">$returnUrl</a>");
171 // {{{ redirectLogin()
174 * Redirects to the login Page if not authorised
176 * put return page on the query or in auth
180 function redirectLogin()
182 // Go to the login Page
184 // For Auth, put some check to avoid infinite redirects, this should at least exclude
187 $url = $this->_loginPage;
188 if(strpos($url, '?') === false) {
192 if(!strstr($_SERVER['PHP_SELF'], $this->_loginPage)) {
193 $url .= 'return='.urlencode($_SERVER['PHP_SELF']);
196 // Track the auth status
197 if($this->auth->status != '') {
198 $url .= '&authstatus='.$this->auth->status;
201 header('Location:'.$url);
202 print("You could not be redirected to <a href=\"$url\">$url</a>");
209 * Starts the Auth Procedure
211 * If the page requires login the user is redirected to the login page
212 * otherwise the Auth::start is called to initialize Auth
215 * @todo Implement an access list which specifies which urls/pages need login and which do not
219 // Check the accessList here
220 // ACL should be a list of urls with allow/deny
221 // If allow set allowLogin to false
222 // Some wild card matching should be implemented ?,*
223 if(!strstr($_SERVER['PHP_SELF'], $this->_loginPage) && !$this->auth->checkAuth()) {
224 $this->redirectLogin();
226 $this->auth->start();
227 // Logged on and on login page
228 if(strstr($_SERVER['PHP_SELF'], $this->_loginPage) && $this->auth->checkAuth()){
229 $this->autoRedirectBack ?
230 $this->redirectBack() :
239 // {{{ isAuthorised()
242 * Checks is the user is logged on
243 * @see Auth::checkAuth()
245 function isAuthorised()
247 return($this->auth->checkAuth());
255 * @see Auth::checkAuth()
259 return($this->auth->checkAuth());
267 * @see Auth::logout()
271 return($this->auth->logout());
279 * @see Auth::getUsername()
281 function getUsername()
283 return($this->auth->getUsername());
291 * @see Auth::getStatus()
295 return($this->auth->getStatus());