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) {
39 if ($prefs) $this->_prefs = $prefs;
40 if (!isset($this->_prefs->_method))
41 _PassUser::_PassUser($UserName);
42 if ($UserName) $this->_userid = $UserName;
43 $this->_authmethod = 'HttpAuth';
45 // Is this double check really needed?
46 // It is not expensive so we keep it for now.
47 if ($this->userExists()) {
50 return $GLOBALS['ForbiddenUser'];
54 // FIXME! This doesn't work yet!
55 // Allow httpauth by other method: Admin for now only
56 function _fake_auth($userid, $passwd) {
59 header('WWW-Authenticate: Basic realm="'.WIKI_NAME.'"');
60 header("Authorization: Basic ".base64_encode($userid.":".$passwd));
62 $_SERVER =& $GLOBALS['HTTP_SERVER_VARS'];
63 $GLOBALS['REMOTE_USER'] = $userid;
64 $_SERVER['PHP_AUTH_USER'] = $userid;
65 $_SERVER['PHP_AUTH_PW'] = $passwd;
66 //$GLOBALS['request']->setStatus(200);
71 $_SERVER =& $GLOBALS['HTTP_SERVER_VARS'];
72 // Maybe we should random the realm to really force a logout.
73 // But the next login will fail.
74 // better_srand(); $realm = microtime().rand();
75 // TODO: On AUTH_TYPE=NTLM this will fail. Only Basic supported so far.
76 header('WWW-Authenticate: Basic realm="'.WIKI_NAME.'"');
77 if (strstr(php_sapi_name(), 'apache'))
78 header('HTTP/1.0 401 Unauthorized');
80 header("Status: 401 Access Denied"); //IIS and CGI need that
81 unset($GLOBALS['REMOTE_USER']);
82 unset($_SERVER['PHP_AUTH_USER']);
83 unset($_SERVER['PHP_AUTH_PW']);
86 function _http_username() {
88 $_SERVER =& $GLOBALS['HTTP_SERVER_VARS'];
89 if (!empty($_SERVER['PHP_AUTH_USER']))
90 return $_SERVER['PHP_AUTH_USER'];
91 if (!empty($_SERVER['REMOTE_USER']))
92 return $_SERVER['REMOTE_USER'];
93 if (!empty($GLOBALS['HTTP_ENV_VARS']['REMOTE_USER']))
94 return $GLOBALS['HTTP_ENV_VARS']['REMOTE_USER'];
95 if (!empty($GLOBALS['REMOTE_USER']))
96 return $GLOBALS['REMOTE_USER'];
98 if (!empty($_SERVER['HTTP_AUTHORIZATION'])) {
99 list($userid, $passwd) = explode(':',
100 base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
106 // force http auth authorization
107 function userExists() {
108 if (!isset($_SERVER))
109 $_SERVER =& $GLOBALS['HTTP_SERVER_VARS'];
110 $username = $this->_http_username();
111 if (strstr($username, "\\")
112 and isset($_SERVER['AUTH_TYPE'])
113 and $_SERVER['AUTH_TYPE'] == 'NTLM')
115 // allow domain\user, change userid to domain/user
116 $username = str_ireplace("\\\\", "\\", $username); // php bug with _SERVER
117 $username = str_ireplace("\\", SUBPAGE_SEPARATOR, $username);
118 $this->_userid = str_ireplace("\\", SUBPAGE_SEPARATOR, $this->_userid);
120 // FIXME: if AUTH_TYPE = NTLM there's a domain\\name <> domain\name mismatch
122 or strtolower($username) != strtolower($this->_userid))
125 $user = $GLOBALS['ForbiddenUser'];
126 $user->_userid = $this->_userid = "";
127 $this->_level = WIKIAUTH_FORBIDDEN;
131 $this->_userid = $username;
132 // we should check if he is a member of admin,
133 // because HttpAuth has its own logic.
134 $this->_level = WIKIAUTH_USER;
135 if ($this->isAdmin())
136 $this->_level = WIKIAUTH_ADMIN;
140 // ignore password, this is checked by the webservers http auth.
141 function checkPass($submitted_password) {
142 return $this->userExists()
143 ? ($this->isAdmin() ? WIKIAUTH_ADMIN : WIKIAUTH_USER)
147 function mayChangePass() {
156 // c-hanging-comment-ender-p: nil
157 // indent-tabs-mode: nil