1 <?php rcs_id('$Id: WikiUser.php,v 1.10 2002-01-19 07:21:58 dairiki Exp $');
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.
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.
12 define('WIKIAUTH_ANON', 0);
13 define('WIKIAUTH_BOGO', 1);
14 define('WIKIAUTH_USER', 2); // currently unused.
15 define('WIKIAUTH_ADMIN', 10);
25 function WikiUser (&$request) {
26 $this->_request = &$request;
28 // Restore from session state.
31 $login_args = $request->getArg('login');
33 $request->setArg('login', false);
34 if ($request->get('REQUEST_METHOD') == 'POST')
35 $this->_handleLoginPost($login_args);
39 function _handleLoginPost ($postargs) {
40 if (!is_array($postargs))
43 $keys = array('userid', 'password', 'require_level', 'login', 'logout', 'cancel');
44 foreach ($keys as $key)
45 $args[$key] = isset($postargs[$key]) ? $postargs[$key] : false;
47 $require_level = max(0, min(WIKIAUTH_ADMIN, (int) $require_level));
55 return; // user hit cancel button.
56 if (!$login && !$userid)
59 if ($this->attemptLogin($userid, $password, $require_level))
60 return; // login succeeded
62 if ($this->_pwcheck($userid, $password))
63 $failmsg = _("Insufficient permissions.");
64 elseif ($password !== false)
65 $failmsg = _("Invalid password or userid.");
69 $this->showLoginForm($require_level, $userid, $failmsg);
74 function requireAuth ($require_level) {
75 if ($require_level > $this->_level)
76 $this->showLoginForm($require_level);
79 function showLoginForm ($require_level = 0, $default_user = false, $fail_message = '') {
81 include_once('lib/Template.php');
83 $login = new WikiTemplate('login');
85 $login->qreplace('REQUIRE', $require_level);
87 if (!empty($default_user))
88 $login->qreplace('DEFAULT_USERID', $default_user);
89 elseif (!empty($this->_failed_userid))
90 $login->qreplace('DEFAULT_USERID', $this->_failed_userid);
93 $login->qreplace('FAILURE_MESSAGE', $fail_message);
95 // FIXME: Need message: You must sign/log in before you can '%s' '%s'.
96 $top = new WikiTemplate('top');
97 $top->replace('TITLE', _("Sign In"));
98 $top->replace('HEADER', _("Please Sign In"));
100 $top->printExpansion($login);
105 * Logout the current user (if any).
108 $this->_level = false;
109 $this->_userid = false;
116 * @param $userid string Username.
117 * @param $password string Password.
118 * @return bool True iff log in was successful.
120 function attemptLogin ($userid, $password = false, $require_level = 0) {
121 $level = $this->_pwcheck ($userid, $password);
122 if ($level === false) {
126 if ($level < $require_level) {
127 // insufficient access
132 $this->_login($userid, $level);
133 return $this->isSignedIn();
139 return ( $this->isSignedIn()
141 : $this->_request->get('REMOTE_ADDR') );
144 function getAuthenticatedId() {
145 return ( $this->isAuthenticated()
147 : $this->_request->get('REMOTE_ADDR') );
150 function isSignedIn () {
151 return $this->_level >= WIKIAUTH_BOGO;
154 function isAuthenticated () {
155 return $this->_level >= WIKIAUTH_USER;
158 function isAdmin () {
159 return $this->_level == WIKIAUTH_ADMIN;
163 * Login with given access level
165 * No check for correct password is done.
167 function _login ($userid, $level = WIKIAUTH_BOGO) {
168 $this->_userid = $userid;
169 $this->_level = $level;
176 function _pwcheck ($userid, $passwd) {
177 global $WikiNameRegexp;
179 if (!empty($userid) && $userid == ADMIN_USER) {
180 if (!empty($passwd) && $passwd == ADMIN_PASSWD)
181 return WIKIAUTH_ADMIN;
184 elseif (ALLOW_BOGO_LOGIN
185 && preg_match('/\A' . $WikiNameRegexp . '\z/', $userid)) {
186 return WIKIAUTH_BOGO;
193 // This is a bit of a hack:
194 function setPreferences ($prefs) {
195 $req = &$this->_request;
196 $req->setCookieVar('WIKI_PREFS', $prefs, 365); // expire in a year.
199 function getPreferences () {
200 $req = &$this->_request;
202 $prefs = array('edit_area.width' => 80,
203 'edit_area.height' => 22);
205 $saved = $req->getCookieVar('WIKI_PREFS');
207 if (is_array($saved)) {
208 foreach ($saved as $key => $val) {
209 if (isset($prefs[$key]) && !empty($val))
214 // Some sanity checks. (FIXME: should move somewhere else)
215 if (!($prefs['edit_area.width'] >= 30 && $prefs['edit_area.width'] <= 150))
216 $prefs['edit_area.width'] = 80;
217 if (!($prefs['edit_area.height'] >= 5 && $prefs['edit_area.height'] <= 80))
218 $prefs['edit_area.height'] = 22;
224 function _copy($saved) {
225 if (!is_array($saved) || !isset($saved['userid']) || !isset($saved['level']))
228 $this->_userid = $saved['userid'];
229 $this->_level = $saved['level'];
233 function _restore () {
234 $req = &$this->_request;
236 if ( $this->_copy($req->getSessionVar('auth_state')) )
241 // Default state: logged out.
242 $this->_userid = false;
243 $this->_level = false;
247 $req = &$this->_request;
250 $saved = array('userid' => $this->_userid,
251 'level' => $this->_level);
253 $req->setSessionVar('auth_state', $saved);
259 if (empty($this->_userid) || empty($this->_level)) {
260 // This is okay if truly logged out.
261 return $this->_userid === false && $this->_level === false;
263 // User is logged in...
265 // Check for valid authlevel.
266 if (!in_array($this->_level, array(WIKIAUTH_BOGO, WIKIAUTH_USER, WIKIAUTH_ADMIN)))
269 // Check for valid userid.
270 if (!is_string($this->_userid))
280 // c-hanging-comment-ender-p: nil
281 // indent-tabs-mode: nil