Subversion Repositories Applications.papyrus

Rev

Rev 1713 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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