|
indvdum@0
|
1 |
<?php
|
|
indvdum@0
|
2 |
/**
|
|
indvdum@0
|
3 |
* Apache auth plug-in for phpBB3
|
|
indvdum@0
|
4 |
*
|
|
indvdum@0
|
5 |
* Authentication plug-ins is largely down to Sergey Kanareykin, our thanks to him.
|
|
indvdum@0
|
6 |
*
|
|
indvdum@0
|
7 |
* @package login
|
|
indvdum@0
|
8 |
* @version $Id$
|
|
indvdum@0
|
9 |
* @copyright (c) 2005 phpBB Group
|
|
indvdum@0
|
10 |
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
|
indvdum@0
|
11 |
*
|
|
indvdum@0
|
12 |
*/
|
|
indvdum@0
|
13 |
|
|
indvdum@0
|
14 |
/**
|
|
indvdum@0
|
15 |
* @ignore
|
|
indvdum@0
|
16 |
*/
|
|
indvdum@0
|
17 |
if (!defined('IN_PHPBB'))
|
|
indvdum@0
|
18 |
{
|
|
indvdum@0
|
19 |
exit;
|
|
indvdum@0
|
20 |
}
|
|
indvdum@0
|
21 |
|
|
indvdum@0
|
22 |
/**
|
|
indvdum@0
|
23 |
* Checks whether the user is identified to apache
|
|
indvdum@0
|
24 |
* Only allow changing authentication to apache if the user is identified
|
|
indvdum@0
|
25 |
* Called in acp_board while setting authentication plugins
|
|
indvdum@0
|
26 |
*
|
|
indvdum@0
|
27 |
* @return boolean|string false if the user is identified and else an error message
|
|
indvdum@0
|
28 |
*/
|
|
indvdum@0
|
29 |
function init_apache()
|
|
indvdum@0
|
30 |
{
|
|
indvdum@0
|
31 |
global $user;
|
|
indvdum@0
|
32 |
|
|
indvdum@0
|
33 |
if (!isset($_SERVER['PHP_AUTH_USER']) || $user->data['username'] !== $_SERVER['PHP_AUTH_USER'])
|
|
indvdum@0
|
34 |
{
|
|
indvdum@0
|
35 |
return $user->lang['APACHE_SETUP_BEFORE_USE'];
|
|
indvdum@0
|
36 |
}
|
|
indvdum@0
|
37 |
return false;
|
|
indvdum@0
|
38 |
}
|
|
indvdum@0
|
39 |
|
|
indvdum@0
|
40 |
/**
|
|
indvdum@0
|
41 |
* Login function
|
|
indvdum@0
|
42 |
*/
|
|
indvdum@0
|
43 |
function login_apache(&$username, &$password)
|
|
indvdum@0
|
44 |
{
|
|
indvdum@0
|
45 |
global $db;
|
|
indvdum@0
|
46 |
|
|
indvdum@0
|
47 |
// do not allow empty password
|
|
indvdum@0
|
48 |
if (!$password)
|
|
indvdum@0
|
49 |
{
|
|
indvdum@0
|
50 |
return array(
|
|
indvdum@0
|
51 |
'status' => LOGIN_ERROR_PASSWORD,
|
|
indvdum@0
|
52 |
'error_msg' => 'NO_PASSWORD_SUPPLIED',
|
|
indvdum@0
|
53 |
'user_row' => array('user_id' => ANONYMOUS),
|
|
indvdum@0
|
54 |
);
|
|
indvdum@0
|
55 |
}
|
|
indvdum@0
|
56 |
|
|
indvdum@0
|
57 |
if (!$username)
|
|
indvdum@0
|
58 |
{
|
|
indvdum@0
|
59 |
return array(
|
|
indvdum@0
|
60 |
'status' => LOGIN_ERROR_USERNAME,
|
|
indvdum@0
|
61 |
'error_msg' => 'LOGIN_ERROR_USERNAME',
|
|
indvdum@0
|
62 |
'user_row' => array('user_id' => ANONYMOUS),
|
|
indvdum@0
|
63 |
);
|
|
indvdum@0
|
64 |
}
|
|
indvdum@0
|
65 |
|
|
indvdum@0
|
66 |
if (!isset($_SERVER['PHP_AUTH_USER']))
|
|
indvdum@0
|
67 |
{
|
|
indvdum@0
|
68 |
return array(
|
|
indvdum@0
|
69 |
'status' => LOGIN_ERROR_EXTERNAL_AUTH,
|
|
indvdum@0
|
70 |
'error_msg' => 'LOGIN_ERROR_EXTERNAL_AUTH_APACHE',
|
|
indvdum@0
|
71 |
'user_row' => array('user_id' => ANONYMOUS),
|
|
indvdum@0
|
72 |
);
|
|
indvdum@0
|
73 |
}
|
|
indvdum@0
|
74 |
|
|
indvdum@0
|
75 |
$php_auth_user = $_SERVER['PHP_AUTH_USER'];
|
|
indvdum@0
|
76 |
$php_auth_pw = $_SERVER['PHP_AUTH_PW'];
|
|
indvdum@0
|
77 |
|
|
indvdum@0
|
78 |
if (!empty($php_auth_user) && !empty($php_auth_pw))
|
|
indvdum@0
|
79 |
{
|
|
indvdum@0
|
80 |
if ($php_auth_user !== $username)
|
|
indvdum@0
|
81 |
{
|
|
indvdum@0
|
82 |
return array(
|
|
indvdum@0
|
83 |
'status' => LOGIN_ERROR_USERNAME,
|
|
indvdum@0
|
84 |
'error_msg' => 'LOGIN_ERROR_USERNAME',
|
|
indvdum@0
|
85 |
'user_row' => array('user_id' => ANONYMOUS),
|
|
indvdum@0
|
86 |
);
|
|
indvdum@0
|
87 |
}
|
|
indvdum@0
|
88 |
|
|
indvdum@0
|
89 |
$sql = 'SELECT user_id, username, user_password, user_passchg, user_email, user_type
|
|
indvdum@0
|
90 |
FROM ' . USERS_TABLE . "
|
|
indvdum@0
|
91 |
WHERE username = '" . $db->sql_escape($php_auth_user) . "'";
|
|
indvdum@0
|
92 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
93 |
$row = $db->sql_fetchrow($result);
|
|
indvdum@0
|
94 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
95 |
|
|
indvdum@0
|
96 |
if ($row)
|
|
indvdum@0
|
97 |
{
|
|
indvdum@0
|
98 |
// User inactive...
|
|
indvdum@0
|
99 |
if ($row['user_type'] == USER_INACTIVE || $row['user_type'] == USER_IGNORE)
|
|
indvdum@0
|
100 |
{
|
|
indvdum@0
|
101 |
return array(
|
|
indvdum@0
|
102 |
'status' => LOGIN_ERROR_ACTIVE,
|
|
indvdum@0
|
103 |
'error_msg' => 'ACTIVE_ERROR',
|
|
indvdum@0
|
104 |
'user_row' => $row,
|
|
indvdum@0
|
105 |
);
|
|
indvdum@0
|
106 |
}
|
|
indvdum@0
|
107 |
|
|
indvdum@0
|
108 |
// Successful login...
|
|
indvdum@0
|
109 |
return array(
|
|
indvdum@0
|
110 |
'status' => LOGIN_SUCCESS,
|
|
indvdum@0
|
111 |
'error_msg' => false,
|
|
indvdum@0
|
112 |
'user_row' => $row,
|
|
indvdum@0
|
113 |
);
|
|
indvdum@0
|
114 |
}
|
|
indvdum@0
|
115 |
|
|
indvdum@0
|
116 |
// this is the user's first login so create an empty profile
|
|
indvdum@0
|
117 |
return array(
|
|
indvdum@0
|
118 |
'status' => LOGIN_SUCCESS_CREATE_PROFILE,
|
|
indvdum@0
|
119 |
'error_msg' => false,
|
|
indvdum@0
|
120 |
'user_row' => user_row_apache($php_auth_user, $php_auth_pw),
|
|
indvdum@0
|
121 |
);
|
|
indvdum@0
|
122 |
}
|
|
indvdum@0
|
123 |
|
|
indvdum@0
|
124 |
// Not logged into apache
|
|
indvdum@0
|
125 |
return array(
|
|
indvdum@0
|
126 |
'status' => LOGIN_ERROR_EXTERNAL_AUTH,
|
|
indvdum@0
|
127 |
'error_msg' => 'LOGIN_ERROR_EXTERNAL_AUTH_APACHE',
|
|
indvdum@0
|
128 |
'user_row' => array('user_id' => ANONYMOUS),
|
|
indvdum@0
|
129 |
);
|
|
indvdum@0
|
130 |
}
|
|
indvdum@0
|
131 |
|
|
indvdum@0
|
132 |
/**
|
|
indvdum@0
|
133 |
* Autologin function
|
|
indvdum@0
|
134 |
*
|
|
indvdum@0
|
135 |
* @return array containing the user row or empty if no auto login should take place
|
|
indvdum@0
|
136 |
*/
|
|
indvdum@0
|
137 |
function autologin_apache()
|
|
indvdum@0
|
138 |
{
|
|
indvdum@0
|
139 |
global $db;
|
|
indvdum@0
|
140 |
|
|
indvdum@0
|
141 |
if (!isset($_SERVER['PHP_AUTH_USER']))
|
|
indvdum@0
|
142 |
{
|
|
indvdum@0
|
143 |
return array();
|
|
indvdum@0
|
144 |
}
|
|
indvdum@0
|
145 |
|
|
indvdum@0
|
146 |
$php_auth_user = $_SERVER['PHP_AUTH_USER'];
|
|
indvdum@0
|
147 |
$php_auth_pw = $_SERVER['PHP_AUTH_PW'];
|
|
indvdum@0
|
148 |
|
|
indvdum@0
|
149 |
if (!empty($php_auth_user) && !empty($php_auth_pw))
|
|
indvdum@0
|
150 |
{
|
|
indvdum@0
|
151 |
set_var($php_auth_user, $php_auth_user, 'string', true);
|
|
indvdum@0
|
152 |
set_var($php_auth_pw, $php_auth_pw, 'string', true);
|
|
indvdum@0
|
153 |
|
|
indvdum@0
|
154 |
$sql = 'SELECT *
|
|
indvdum@0
|
155 |
FROM ' . USERS_TABLE . "
|
|
indvdum@0
|
156 |
WHERE username = '" . $db->sql_escape($php_auth_user) . "'";
|
|
indvdum@0
|
157 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
158 |
$row = $db->sql_fetchrow($result);
|
|
indvdum@0
|
159 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
160 |
|
|
indvdum@0
|
161 |
if ($row)
|
|
indvdum@0
|
162 |
{
|
|
indvdum@0
|
163 |
return ($row['user_type'] == USER_INACTIVE || $row['user_type'] == USER_IGNORE) ? array() : $row;
|
|
indvdum@0
|
164 |
}
|
|
indvdum@0
|
165 |
|
|
indvdum@0
|
166 |
if (!function_exists('user_add'))
|
|
indvdum@0
|
167 |
{
|
|
indvdum@0
|
168 |
global $phpbb_root_path, $phpEx;
|
|
indvdum@0
|
169 |
|
|
indvdum@0
|
170 |
include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
|
|
indvdum@0
|
171 |
}
|
|
indvdum@0
|
172 |
|
|
indvdum@0
|
173 |
// create the user if he does not exist yet
|
|
indvdum@0
|
174 |
user_add(user_row_apache($php_auth_user, $php_auth_pw));
|
|
indvdum@0
|
175 |
|
|
indvdum@0
|
176 |
$sql = 'SELECT *
|
|
indvdum@0
|
177 |
FROM ' . USERS_TABLE . "
|
|
indvdum@0
|
178 |
WHERE username_clean = '" . $db->sql_escape(utf8_clean_string($php_auth_user)) . "'";
|
|
indvdum@0
|
179 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
180 |
$row = $db->sql_fetchrow($result);
|
|
indvdum@0
|
181 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
182 |
|
|
indvdum@0
|
183 |
if ($row)
|
|
indvdum@0
|
184 |
{
|
|
indvdum@0
|
185 |
return $row;
|
|
indvdum@0
|
186 |
}
|
|
indvdum@0
|
187 |
}
|
|
indvdum@0
|
188 |
|
|
indvdum@0
|
189 |
return array();
|
|
indvdum@0
|
190 |
}
|
|
indvdum@0
|
191 |
|
|
indvdum@0
|
192 |
/**
|
|
indvdum@0
|
193 |
* This function generates an array which can be passed to the user_add function in order to create a user
|
|
indvdum@0
|
194 |
*/
|
|
indvdum@0
|
195 |
function user_row_apache($username, $password)
|
|
indvdum@0
|
196 |
{
|
|
indvdum@0
|
197 |
global $db, $config, $user;
|
|
indvdum@0
|
198 |
// first retrieve default group id
|
|
indvdum@0
|
199 |
$sql = 'SELECT group_id
|
|
indvdum@0
|
200 |
FROM ' . GROUPS_TABLE . "
|
|
indvdum@0
|
201 |
WHERE group_name = '" . $db->sql_escape('REGISTERED') . "'
|
|
indvdum@0
|
202 |
AND group_type = " . GROUP_SPECIAL;
|
|
indvdum@0
|
203 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
204 |
$row = $db->sql_fetchrow($result);
|
|
indvdum@0
|
205 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
206 |
|
|
indvdum@0
|
207 |
if (!$row)
|
|
indvdum@0
|
208 |
{
|
|
indvdum@0
|
209 |
trigger_error('NO_GROUP');
|
|
indvdum@0
|
210 |
}
|
|
indvdum@0
|
211 |
|
|
indvdum@0
|
212 |
// generate user account data
|
|
indvdum@0
|
213 |
return array(
|
|
indvdum@0
|
214 |
'username' => $username,
|
|
indvdum@0
|
215 |
'user_password' => phpbb_hash($password),
|
|
indvdum@0
|
216 |
'user_email' => '',
|
|
indvdum@0
|
217 |
'group_id' => (int) $row['group_id'],
|
|
indvdum@0
|
218 |
'user_type' => USER_NORMAL,
|
|
indvdum@0
|
219 |
'user_ip' => $user->ip,
|
|
indvdum@0
|
220 |
'user_new' => ($config['new_member_post_limit']) ? 1 : 0,
|
|
indvdum@0
|
221 |
);
|
|
indvdum@0
|
222 |
}
|
|
indvdum@0
|
223 |
|
|
indvdum@0
|
224 |
/**
|
|
indvdum@0
|
225 |
* The session validation function checks whether the user is still logged in
|
|
indvdum@0
|
226 |
*
|
|
indvdum@0
|
227 |
* @return boolean true if the given user is authenticated or false if the session should be closed
|
|
indvdum@0
|
228 |
*/
|
|
indvdum@0
|
229 |
function validate_session_apache(&$user)
|
|
indvdum@0
|
230 |
{
|
|
indvdum@0
|
231 |
// Check if PHP_AUTH_USER is set and handle this case
|
|
indvdum@0
|
232 |
if (isset($_SERVER['PHP_AUTH_USER']))
|
|
indvdum@0
|
233 |
{
|
|
indvdum@0
|
234 |
$php_auth_user = '';
|
|
indvdum@0
|
235 |
set_var($php_auth_user, $_SERVER['PHP_AUTH_USER'], 'string', true);
|
|
indvdum@0
|
236 |
|
|
indvdum@0
|
237 |
return ($php_auth_user === $user['username']) ? true : false;
|
|
indvdum@0
|
238 |
}
|
|
indvdum@0
|
239 |
|
|
indvdum@0
|
240 |
// PHP_AUTH_USER is not set. A valid session is now determined by the user type (anonymous/bot or not)
|
|
indvdum@0
|
241 |
if ($user['user_type'] == USER_IGNORE)
|
|
indvdum@0
|
242 |
{
|
|
indvdum@0
|
243 |
return true;
|
|
indvdum@0
|
244 |
}
|
|
indvdum@0
|
245 |
|
|
indvdum@0
|
246 |
return false;
|
|
indvdum@0
|
247 |
}
|
|
indvdum@0
|
248 |
|
|
indvdum@0
|
249 |
?> |