Skip to content

Commit b0f4578

Browse files
per user servers, logout button
1 parent 90d1bb4 commit b0f4578

File tree

7 files changed

+78
-4
lines changed

7 files changed

+78
-4
lines changed

common.inc.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@
5656

5757

5858

59-
$i = 0;
59+
if (isset($login['servers'])) {
60+
$i = current($login['servers']);
61+
} else {
62+
$i = 0;
63+
}
64+
6065

6166
if (isset($_GET['s']) && is_numeric($_GET['s']) && ($_GET['s'] < count($config['servers']))) {
6267
$i = $_GET['s'];
@@ -66,6 +71,19 @@
6671
$server['id'] = $i;
6772

6873

74+
if (isset($login, $login['servers'])) {
75+
if (array_search($i, $login['servers']) === false) {
76+
die('You are not allowed to access this database.');
77+
}
78+
79+
foreach ($config['servers'] as $key => $ignore) {
80+
if (array_search($key, $login['servers']) === false) {
81+
unset($config['servers'][$key]);
82+
}
83+
}
84+
}
85+
86+
6987
if (!isset($server['db'])) {
7088
$server['db'] = 0;
7189
}

config.inc.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@
3636
/*'login' => array(
3737
// Username => Password
3838
// Multiple combinations can be used
39-
'username' => 'password'
39+
'admin' => array(
40+
'password' => 'adminpassword',
41+
),
42+
'guest' => array(
43+
'password' => '',
44+
'servers' => array(1) // Optional list of servers this user can access.
45+
)
4046
),*/
4147

4248

images/logout.png

657 Bytes
Loading

index.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ function print_namespace($item, $name, $fullkey, $islast) {
169169
</p>
170170

171171
<p>
172+
<?php if (isset($login)) { ?>
173+
<a href="logout.php"><img src="images/logout.png" width="16" height="16" title="Logout" alt="[L]"></a>
174+
<?php } ?>
172175
<a href="?info&amp;s=<?php echo $server['id']?>"><img src="images/info.png" width="16" height="16" title="Info" alt="[I]"></a>
173176
<a href="?export&amp;s=<?php echo $server['id']?>"><img src="images/export.png" width="16" height="16" title="Export" alt="[E]"></a>
174177
<a href="?import&amp;s=<?php echo $server['id']?>"><img src="images/import.png" width="16" height="16" title="Import" alt="[I]"></a>

js/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11

22
$(function() {
33
$('#sidebar a').click(function(e) {
4+
if (e.currentTarget.href.indexOf('/?') == -1) {
5+
return;
6+
}
7+
48
e.preventDefault();
59

610
var href;

login.inc.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
$opaque = md5('phpRedisAdmin'.$_SERVER['HTTP_USER_AGENT'].$_SERVER['REMOTE_ADDR']);
1010

1111

12-
if (empty($_SERVER['PHP_AUTH_DIGEST'])) {
12+
if (!isset($_SERVER['PHP_AUTH_DIGEST']) || empty($_SERVER['PHP_AUTH_DIGEST'])) {
1313
header('HTTP/1.1 401 Unauthorized');
1414
header('WWW-Authenticate: Digest realm="'.$realm.'",qop="auth",nonce="'.uniqid().'",opaque="'.$opaque.'"');
1515
die;
@@ -47,7 +47,10 @@
4747
die('Invalid username and/or password combination.');
4848
}
4949

50-
$password = md5($data['username'].':'.$realm.':'.$config['login'][$data['username']]);
50+
$login = $config['login'][$data['username']];
51+
$login['name'] = $data['username'];
52+
53+
$password = md5($login['name'].':'.$realm.':'.$login['password']);
5154

5255
$response = md5($password.':'.$data['nonce'].':'.$data['nc'].':'.$data['cnonce'].':'.$data['qop'].':'.md5($_SERVER['REQUEST_METHOD'].':'.$data['uri']));
5356

logout.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
4+
$needed_parts = array(
5+
'nonce' => 1,
6+
'nc' => 1,
7+
'cnonce' => 1,
8+
'qop' => 1,
9+
'username' => 1,
10+
'uri' => 1,
11+
'response' => 1
12+
);
13+
14+
$data = array();
15+
$keys = implode('|', array_keys($needed_parts));
16+
17+
preg_match_all('/('.$keys.')=(?:([\'"])([^\2]+?)\2|([^\s,]+))/', $_SERVER['PHP_AUTH_DIGEST'], $matches, PREG_SET_ORDER);
18+
19+
foreach ($matches as $m) {
20+
$data[$m[1]] = $m[3] ? $m[3] : $m[4];
21+
unset($needed_parts[$m[1]]);
22+
}
23+
24+
25+
if (!isset($_GET['nonce'])) {
26+
header('Location: logout.php?nonce='.$data['nonce']);
27+
die;
28+
}
29+
30+
31+
if ($data['nonce'] == $_GET['nonce']) {
32+
unset($_SERVER['PHP_AUTH_DIGEST']);
33+
34+
require 'login.inc.php';
35+
}
36+
37+
38+
header('Location: '.substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], 'logout.php')));
39+
40+
?>

0 commit comments

Comments
 (0)