4 * Copyright (C) 2004 ReiniUrban
6 * This file is part of PhpWiki.
8 * PhpWiki is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * PhpWiki is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * We have two possibilities here.
25 * 1) The webserver location is already HTTP protected. Usually Basic, but also
26 * NTLM or Digest. Then just use this username and do nothing.
27 * 2) The webserver location is not protected, so we enforce basic HTTP Protection
28 * by sending a 401 error and let the client display the login dialog.
29 * This makes only sense if HttpAuth is the last method in USER_AUTH_ORDER,
30 * since the other methods cannot be transparently called after this enforced
32 * Try the available auth methods (most likely Bogo) and sent this header back.
33 * header('Authorization: Basic '.base64_encode("$userid:$passwd")."\r\n";
35 class _HttpAuthPassUser
38 function _HttpAuthPassUser($UserName = '', $prefs = false)
40 if ($prefs) $this->_prefs = $prefs;
41 if (!isset($this->_prefs->_method))
42 _PassUser::_PassUser($UserName);
43 if ($UserName) $this->_userid = $UserName;
44 $this->_authmethod = 'HttpAuth';
46 // Is this double check really needed?
47 // It is not expensive so we keep it for now.
48 if ($this->userExists()) {
51 return $GLOBALS['ForbiddenUser'];
55 // FIXME! This doesn't work yet!
56 // Allow httpauth by other method: Admin for now only
57 function _fake_auth($userid, $passwd)
61 header('WWW-Authenticate: Basic realm="' . WIKI_NAME . '"');
62 header("Authorization: Basic " . base64_encode($userid . ":" . $passwd));
64 $_SERVER =& $GLOBALS['HTTP_SERVER_VARS'];
65 $GLOBALS['REMOTE_USER'] = $userid;
66 $_SERVER['PHP_AUTH_USER'] = $userid;
67 $_SERVER['PHP_AUTH_PW'] = $passwd;
68 //$GLOBALS['request']->setStatus(200);
74 $_SERVER =& $GLOBALS['HTTP_SERVER_VARS'];
75 // Maybe we should random the realm to really force a logout.
76 // But the next login will fail.
77 // better_srand(); $realm = microtime().rand();
78 // TODO: On AUTH_TYPE=NTLM this will fail. Only Basic supported so far.
79 header('WWW-Authenticate: Basic realm="' . WIKI_NAME . '"');
80 if (strstr(php_sapi_name(), 'apache'))
81 header('HTTP/1.0 401 Unauthorized');
83 header("Status: 401 Access Denied"); //IIS and CGI need that
84 unset($GLOBALS['REMOTE_USER']);
85 unset($_SERVER['PHP_AUTH_USER']);
86 unset($_SERVER['PHP_AUTH_PW']);
89 function _http_username()
92 $_SERVER =& $GLOBALS['HTTP_SERVER_VARS'];
93 if (!empty($_SERVER['PHP_AUTH_USER']))
94 return $_SERVER['PHP_AUTH_USER'];
95 if (!empty($_SERVER['REMOTE_USER']))
96 return $_SERVER['REMOTE_USER'];
97 if (!empty($GLOBALS['HTTP_ENV_VARS']['REMOTE_USER']))
98 return $GLOBALS['HTTP_ENV_VARS']['REMOTE_USER'];
99 if (!empty($GLOBALS['REMOTE_USER']))
100 return $GLOBALS['REMOTE_USER'];
102 if (!empty($_SERVER['HTTP_AUTHORIZATION'])) {
103 list($userid, $passwd) = explode(':',
104 base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
110 // force http auth authorization
111 function userExists()
113 if (!isset($_SERVER))
114 $_SERVER =& $GLOBALS['HTTP_SERVER_VARS'];
115 $username = $this->_http_username();
116 if (strstr($username, "\\")
117 and isset($_SERVER['AUTH_TYPE'])
118 and $_SERVER['AUTH_TYPE'] == 'NTLM'
120 // allow domain\user, change userid to domain/user
121 $username = str_ireplace("\\\\", "\\", $username); // php bug with _SERVER
122 $username = str_ireplace("\\", SUBPAGE_SEPARATOR, $username);
123 $this->_userid = str_ireplace("\\", SUBPAGE_SEPARATOR, $this->_userid);
125 // FIXME: if AUTH_TYPE = NTLM there's a domain\\name <> domain\name mismatch
127 or strtolower($username) != strtolower($this->_userid)
130 $user = $GLOBALS['ForbiddenUser'];
131 $user->_userid = $this->_userid = "";
132 $this->_level = WIKIAUTH_FORBIDDEN;
136 $this->_userid = $username;
137 // we should check if he is a member of admin,
138 // because HttpAuth has its own logic.
139 $this->_level = WIKIAUTH_USER;
140 if ($this->isAdmin())
141 $this->_level = WIKIAUTH_ADMIN;
145 // ignore password, this is checked by the webservers http auth.
146 function checkPass($submitted_password)
148 return $this->userExists()
149 ? ($this->isAdmin() ? WIKIAUTH_ADMIN : WIKIAUTH_USER)
153 function mayChangePass()
163 // c-hanging-comment-ender-p: nil
164 // indent-tabs-mode: nil