]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiUser/HttpAuth.php
Reformat code
[SourceForge/phpwiki.git] / lib / WikiUser / HttpAuth.php
1 <?php
2
3 /*
4  * Copyright (C) 2004 ReiniUrban
5  *
6  * This file is part of PhpWiki.
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 /**
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
31  *    external dialog.
32  *    Try the available auth methods (most likely Bogo) and sent this header back.
33  *    header('Authorization: Basic '.base64_encode("$userid:$passwd")."\r\n";
34  */
35 class _HttpAuthPassUser
36     extends _PassUser
37 {
38     function _HttpAuthPassUser($UserName = '', $prefs = false)
39     {
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';
45
46         // Is this double check really needed?
47         // It is not expensive so we keep it for now.
48         if ($this->userExists()) {
49             return $this;
50         } else {
51             return $GLOBALS['ForbiddenUser'];
52         }
53     }
54
55     // FIXME! This doesn't work yet!
56     // Allow httpauth by other method: Admin for now only
57     function _fake_auth($userid, $passwd)
58     {
59         return false;
60
61         header('WWW-Authenticate: Basic realm="' . WIKI_NAME . '"');
62         header("Authorization: Basic " . base64_encode($userid . ":" . $passwd));
63         if (!isset($_SERVER))
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);
69     }
70
71     function logout()
72     {
73         if (!isset($_SERVER))
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');
82         else
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']);
87     }
88
89     function _http_username()
90     {
91         if (!isset($_SERVER))
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'];
101         // IIS + Basic
102         if (!empty($_SERVER['HTTP_AUTHORIZATION'])) {
103             list($userid, $passwd) = explode(':',
104                 base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
105             return $userid;
106         }
107         return '';
108     }
109
110     // force http auth authorization
111     function userExists()
112     {
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'
119         ) {
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);
124         }
125         // FIXME: if AUTH_TYPE = NTLM there's a domain\\name <> domain\name mismatch
126         if (empty($username)
127             or strtolower($username) != strtolower($this->_userid)
128         ) {
129             $this->logout();
130             $user = $GLOBALS['ForbiddenUser'];
131             $user->_userid = $this->_userid = "";
132             $this->_level = WIKIAUTH_FORBIDDEN;
133             return $user;
134             //exit;
135         }
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;
142         return $this;
143     }
144
145     // ignore password, this is checked by the webservers http auth.
146     function checkPass($submitted_password)
147     {
148         return $this->userExists()
149             ? ($this->isAdmin() ? WIKIAUTH_ADMIN : WIKIAUTH_USER)
150             : WIKIAUTH_ANON;
151     }
152
153     function mayChangePass()
154     {
155         return false;
156     }
157 }
158
159 // Local Variables:
160 // mode: php
161 // tab-width: 8
162 // c-basic-offset: 4
163 // c-hanging-comment-ender-p: nil
164 // indent-tabs-mode: nil
165 // End: