]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiUser.php
Refactor/cleanup of login code continues.
[SourceForge/phpwiki.git] / lib / WikiUser.php
1 <?php rcs_id('$Id: WikiUser.php,v 1.11 2002-01-23 05:10:22 dairiki Exp $');
2
3 // It is anticipated that when userid support is added to phpwiki,
4 // this object will hold much more information (e-mail, home(wiki)page,
5 // etc.) about the user.
6    
7 // There seems to be no clean way to "log out" a user when using
8 // HTTP authentication.
9 // So we'll hack around this by storing the currently logged
10 // in username and other state information in a cookie.
11
12 define('WIKIAUTH_ANON', 0);
13 define('WIKIAUTH_BOGO', 1);
14 define('WIKIAUTH_USER', 2);     // currently unused.
15 define('WIKIAUTH_ADMIN', 10);
16 define('WIKIAUTH_FORBIDDEN', 11); // Completely not allowed.
17
18 class WikiUser 
19 {
20     var $_userid = false;
21     var $_level  = false;
22
23     /**
24      * Constructor.
25      */
26     function WikiUser ($userid = false, $authlevel = false) {
27         if (isa($userid, 'WikiUser')) {
28             $this->_userid = $userid->_userid;
29             $this->_level = $userid->_level;
30         }
31         else {
32             $this->_userid = $userid;
33             $this->_level = $authlevel;
34         }
35
36         if (!$this->_ok()) {
37             // Paranoia: if state is at all inconsistent, log out...
38             $this->_userid = false;
39             $this->_level = false;
40         }
41     }
42
43     /** Invariant
44      */
45     function _ok () {
46         if (empty($this->_userid) || empty($this->_level)) {
47             // This is okay if truly logged out.
48             return $this->_userid === false && $this->_level === false;
49         }
50         // User is logged in...
51         
52         // Check for valid authlevel.
53         if (!in_array($this->_level, array(WIKIAUTH_BOGO, WIKIAUTH_USER, WIKIAUTH_ADMIN)))
54             return false;
55
56         // Check for valid userid.
57         if (!is_string($this->_userid))
58             return false;
59         return true;
60     }
61
62     function getId () {
63         
64         return ( $this->isSignedIn()
65                  ? $this->_userid
66                  : $GLOBALS['request']->get('REMOTE_ADDR') ); // FIXME: globals
67     }
68
69     function getAuthenticatedId() {
70         return ( $this->isAuthenticated()
71                  ? $this->_userid
72                  : $GLOBALS['request']->get('REMOTE_ADDR') ); // FIXME: globals
73     }
74
75     function isSignedIn () {
76         return $this->_level >= WIKIAUTH_BOGO;
77     }
78         
79     function isAuthenticated () {
80         return $this->_level >= WIKIAUTH_USER;
81     }
82          
83     function isAdmin () {
84         return $this->_level == WIKIAUTH_ADMIN;
85     }
86
87     function hasAuthority ($require_level) {
88         return $this->_level >= $require_level;
89     }
90
91     
92     function AuthCheck ($postargs) {
93         // Normalize args, and extract.
94         $keys = array('userid', 'password', 'require_level', 'login', 'logout', 'cancel');
95         foreach ($keys as $key) 
96             $args[$key] = isset($postargs[$key]) ? $postargs[$key] : false;
97         extract($args);
98         $require_level = max(0, min(WIKIAUTH_ADMIN, (int) $require_level));
99
100         if ($logout)
101             return new WikiUser; // Log out
102         elseif ($cancel)
103             return false;        // User hit cancel button.
104         elseif (!$login && !$userid)
105             return false;       // Nothing to do?
106
107         $authlevel = WikiUser::_pwcheck($userid, $password);
108         if (!$authlevel)
109             return _("Invalid password or userid.");
110         elseif ($authlevel < $require_level)
111             return _("Insufficient permissions.");
112
113         // Successful login.
114         $user = new WikiUser;
115         $user->_userid = $userid;
116         $user->_level = $authlevel;
117         return $user;
118     }
119     
120     function PrintLoginForm ($args, $fail_message = false) {
121         include_once('lib/Template.php');
122         
123         $userid = '';
124         $require_level = 0;
125         extract($args);
126         
127         $login = new WikiTemplate('login');
128         $login->replace('userid', $userid);
129         $login->replace('require_level',
130                         max(0, min(WIKIAUTH_ADMIN, (int) $require_level)));
131         if ($fail_message)
132             $login->replace('fail_message', $fail_message);
133
134         $top = new WikiTemplate('top');
135         $top->replace('TITLE', _("Sign In"));
136         $top->replace('HEADER', _("Please Sign In"));
137         
138         $top->printExpansion($login);
139     }
140         
141     /**
142      * Check password.
143      */
144     function _pwcheck ($userid, $passwd) {
145         global $WikiNameRegexp;
146         
147         if (!empty($userid) && $userid == ADMIN_USER) {
148             if (!empty($passwd) && $passwd == ADMIN_PASSWD)
149                 return WIKIAUTH_ADMIN;
150             return false;
151         }
152         elseif (ALLOW_BOGO_LOGIN
153                 && preg_match('/\A' . $WikiNameRegexp . '\z/', $userid)) {
154             return WIKIAUTH_BOGO;
155         }
156         return false;
157     }
158 }
159     
160
161 // c-basic-offset: 4
162 // c-hanging-comment-ender-p: nil
163 // indent-tabs-mode: nil
164 // End:   
165 ?>