]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiUser/HttpAuth.php
php_closing_tag [PSR-2] The closing ?> tag MUST be omitted from files containing...
[SourceForge/phpwiki.git] / lib / WikiUser / HttpAuth.php
1 <?php //-*-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         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';
44
45         // Is this double check really needed?
46         // It is not expensive so we keep it for now.
47         if ($this->userExists()) {
48             return $this;
49         } else {
50             return $GLOBALS['ForbiddenUser'];
51         }
52     }
53
54     // FIXME! This doesn't work yet!
55     // Allow httpauth by other method: Admin for now only
56     function _fake_auth($userid, $passwd) {
57             return false;
58
59         header('WWW-Authenticate: Basic realm="'.WIKI_NAME.'"');
60         header("Authorization: Basic ".base64_encode($userid.":".$passwd));
61         if (!isset($_SERVER))
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);
67     }
68
69     function logout() {
70         if (!isset($_SERVER))
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');
79         else
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']);
84     }
85
86     function _http_username() {
87         if (!isset($_SERVER))
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'];
97         // IIS + Basic
98         if (!empty($_SERVER['HTTP_AUTHORIZATION'])) {
99             list($userid, $passwd) = explode(':',
100                 base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
101             return $userid;
102         }
103         return '';
104     }
105
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')
114         {
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);
119         }
120         // FIXME: if AUTH_TYPE = NTLM there's a domain\\name <> domain\name mismatch
121         if (empty($username)
122             or strtolower($username) != strtolower($this->_userid))
123         {
124             $this->logout();
125             $user = $GLOBALS['ForbiddenUser'];
126             $user->_userid = $this->_userid =  "";
127             $this->_level = WIKIAUTH_FORBIDDEN;
128             return $user;
129             //exit;
130         }
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;
137         return $this;
138     }
139
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)
144             : WIKIAUTH_ANON;
145     }
146
147     function mayChangePass() {
148         return false;
149     }
150 }
151
152 // Local Variables:
153 // mode: php
154 // tab-width: 8
155 // c-basic-offset: 4
156 // c-hanging-comment-ender-p: nil
157 // indent-tabs-mode: nil
158 // End: