]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiUser/HttpAuthUpper.php
private
[SourceForge/phpwiki.git] / lib / WikiUser / HttpAuthUpper.php
1 <?php
2
3 /*
4  * Copyright (C) 2004,2007 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.
26  *    Usually Basic by some auth module (ldap, mysql, ...), but also NTLM or Digest.
27  *    Then just use this username and do nothing.
28  * 2) The webserver location is not protected, so we enforce basic HTTP Protection
29  *    by sending a 401 error and let the client display the login dialog.
30  *    This makes only sense if HttpAuth is the last method in USER_AUTH_ORDER,
31  *    since the other methods cannot be transparently called after this enforced
32  *    external dialog.
33  *    Try the available auth methods (most likely Bogo) and sent this header back.
34  *    header('Authorization: Basic '.base64_encode("$userid:$passwd")."\r\n";
35  */
36 class _HttpAuthUpperPassUser
37     extends _PassUser
38 {
39     function _HttpAuthUpperPassUser($UserName = '', $prefs = false)
40     {
41         if ($prefs) $this->_prefs = $prefs;
42         if (!isset($this->_prefs->_method))
43             _PassUser::_PassUser($UserName);
44         if ($UserName)
45             $this->_userid = $UserName;
46         $this->_authmethod = 'HttpAuthUpper';
47
48         // Is this double check really needed?
49         // It is not expensive so we keep it for now.
50         if ($this->userExists()) {
51             return $this;
52         } else {
53             return $GLOBALS['ForbiddenUser'];
54         }
55     }
56
57     // FIXME! This doesn't work yet!
58     // Allow httpauth by other method: Admin for now only
59     function _fake_auth($userid, $passwd)
60     {
61         return false;
62
63         /*
64         header('WWW-Authenticate: Basic realm="' . WIKI_NAME . '"');
65         header("Authorization: Basic " . base64_encode($userid . ":" . $passwd));
66         if (!isset($_SERVER))
67             $_SERVER =& $GLOBALS['HTTP_SERVER_VARS'];
68         $GLOBALS['REMOTE_USER'] = $userid;
69         $_SERVER['PHP_AUTH_USER'] = $userid;
70         $_SERVER['PHP_AUTH_PW'] = $passwd;
71         //$GLOBALS['request']->setStatus(200);
72         */
73     }
74
75     function logout()
76     {
77         if (!isset($_SERVER))
78             $_SERVER =& $GLOBALS['HTTP_SERVER_VARS'];
79         // Maybe we should random the realm to really force a logout.
80         // But the next login will fail.
81         // better_srand(); $realm = microtime().rand();
82         // TODO: On AUTH_TYPE=NTLM this will fail. Only Basic supported so far.
83         header('WWW-Authenticate: Basic realm="' . WIKI_NAME . '"');
84         if (strstr(php_sapi_name(), 'apache'))
85             header('HTTP/1.0 401 Unauthorized');
86         else
87             header("Status: 401 Access Denied"); //IIS and CGI need that
88         unset($GLOBALS['REMOTE_USER']);
89         unset($_SERVER['PHP_AUTH_USER']);
90         unset($_SERVER['PHP_AUTH_PW']);
91     }
92
93     function _http_username()
94     {
95         if (!isset($_SERVER))
96             $_SERVER =& $GLOBALS['HTTP_SERVER_VARS'];
97         if (!empty($_SERVER['PHP_AUTH_USER']))
98             return $_SERVER['PHP_AUTH_USER'];
99         if (!empty($_SERVER['REMOTE_USER']))
100             return $_SERVER['REMOTE_USER'];
101         if (!empty($GLOBALS['HTTP_ENV_VARS']['REMOTE_USER']))
102             return $GLOBALS['HTTP_ENV_VARS']['REMOTE_USER'];
103         if (!empty($GLOBALS['REMOTE_USER']))
104             return $GLOBALS['REMOTE_USER'];
105         // IIS + Basic
106         if (!empty($_SERVER['HTTP_AUTHORIZATION'])) {
107             list($userid, $passwd) = explode(':',
108                 base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
109             return $userid;
110         }
111         return '';
112     }
113
114     // special: force upcase username
115     function UserName()
116     {
117         if (!empty($this->_userid)) {
118             $this->_userid = strtoupper($this->_userid);
119             return strtoupper($this->_userid);
120         }
121         return '';
122     }
123
124     // force http auth authorization
125     function userExists()
126     {
127         if (!isset($_SERVER))
128             $_SERVER =& $GLOBALS['HTTP_SERVER_VARS'];
129         $username = strtoupper($this->_http_username());
130         if (strstr($username, "\\")
131             and isset($_SERVER['AUTH_TYPE'])
132                 and $_SERVER['AUTH_TYPE'] == 'NTLM'
133         ) {
134             // allow domain\user, change userid to domain/user
135             $username = str_ireplace("\\\\", "\\", $username); // php bug with _SERVER
136             $username = str_ireplace("\\", SUBPAGE_SEPARATOR, $username);
137             $this->_userid = str_ireplace("\\", SUBPAGE_SEPARATOR, $this->_userid);
138         }
139         // FIXME: if AUTH_TYPE = NTLM there's a domain\\name <> domain\name mismatch
140         if (empty($username)
141             or strtolower($username) != strtolower($this->_userid)
142         ) {
143             $this->logout();
144             $user = $GLOBALS['ForbiddenUser'];
145             $user->_userid = $this->_userid = "";
146             $this->_level = WIKIAUTH_FORBIDDEN;
147             return $user;
148             //exit;
149         }
150         $this->_userid = strtoupper($username);
151         // we should check if he is a member of admin,
152         // because HttpAuth has its own logic.
153         $this->_level = WIKIAUTH_USER;
154         if ($this->isAdmin())
155             $this->_level = WIKIAUTH_ADMIN;
156         return $this;
157     }
158
159     // ignore password, this is checked by the webservers http auth.
160     function checkPass($submitted_password)
161     {
162         return $this->userExists()
163             ? ($this->isAdmin() ? WIKIAUTH_ADMIN : WIKIAUTH_USER)
164             : WIKIAUTH_ANON;
165     }
166
167     function mayChangePass()
168     {
169         return false;
170     }
171 }
172
173 // Local Variables:
174 // mode: php
175 // tab-width: 8
176 // c-basic-offset: 4
177 // c-hanging-comment-ender-p: nil
178 // indent-tabs-mode: nil
179 // End: