Skip to content

Commit dc8fce6

Browse files
authored
Handle permission access errors (#216)
1 parent 6f9fae0 commit dc8fce6

File tree

4 files changed

+92
-55
lines changed

4 files changed

+92
-55
lines changed

css/frame.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,13 @@ float: left;
5252
margin: 1em;
5353
}
5454

55+
56+
.exception {
57+
border: 1px solid #ff0000;
58+
background-color: rgba(255, 0, 0, 0.2);
59+
color: #880000;
60+
padding: 10px;
61+
border-radius: 5px;
62+
margin: 20px;
63+
}
64+

edit.php

Lines changed: 63 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -34,72 +34,83 @@
3434
die('ERROR: could not encode value');
3535
}
3636

37-
// String
38-
if ($_POST['type'] == 'string') {
39-
$redis->set($key, $value);
40-
}
41-
42-
// Hash
43-
else if (($_POST['type'] == 'hash') && isset($_POST['hkey'])) {
44-
if (strlen($_POST['hkey']) > $config['maxkeylen']) {
45-
die('ERROR: Your hash key is to long (max length is '.$config['maxkeylen'].')');
37+
try {
38+
// String
39+
if ($_POST['type'] == 'string') {
40+
$redis->set($key, $value);
4641
}
4742

48-
if ($edit && !$redis->hExists($key, input_convert($_POST['hkey']))) {
49-
$redis->hDel($key, input_convert($_GET['hkey']));
43+
// Hash
44+
else if (($_POST['type'] == 'hash') && isset($_POST['hkey'])) {
45+
if (strlen($_POST['hkey']) > $config['maxkeylen']) {
46+
die('ERROR: Your hash key is to long (max length is '.$config['maxkeylen'].')');
47+
}
48+
49+
if ($edit && !$redis->hExists($key, input_convert($_POST['hkey']))) {
50+
$redis->hDel($key, input_convert($_GET['hkey']));
51+
}
52+
53+
$redis->hSet($key, input_convert($_POST['hkey']), $value);
5054
}
5155

52-
$redis->hSet($key, input_convert($_POST['hkey']), $value);
53-
}
56+
// List
57+
else if (($_POST['type'] == 'list') && isset($_POST['index'])) {
58+
$size = $redis->lLen($key);
59+
60+
if (($_POST['index'] == '') ||
61+
($_POST['index'] == $size)) {
62+
// Push it at the end
63+
$redis->rPush($key, $value);
64+
} else if ($_POST['index'] == -1) {
65+
// Push it at the start
66+
$redis->lPush($key, $value);
67+
} else if (($_POST['index'] >= 0) &&
68+
($_POST['index'] < $size)) {
69+
// Overwrite an index
70+
$redis->lSet($key, input_convert($_POST['index']), $value);
71+
} else {
72+
die('ERROR: Out of bounds index');
73+
}
74+
}
5475

55-
// List
56-
else if (($_POST['type'] == 'list') && isset($_POST['index'])) {
57-
$size = $redis->lLen($key);
58-
59-
if (($_POST['index'] == '') ||
60-
($_POST['index'] == $size)) {
61-
// Push it at the end
62-
$redis->rPush($key, $value);
63-
} else if ($_POST['index'] == -1) {
64-
// Push it at the start
65-
$redis->lPush($key, $value);
66-
} else if (($_POST['index'] >= 0) &&
67-
($_POST['index'] < $size)) {
68-
// Overwrite an index
69-
$redis->lSet($key, input_convert($_POST['index']), $value);
70-
} else {
71-
die('ERROR: Out of bounds index');
76+
// Set
77+
else if ($_POST['type'] == 'set') {
78+
if ($_POST['value'] != $_POST['oldvalue']) {
79+
// The only way to edit a Set value is to add it and remove the old value.
80+
$redis->sRem($key, encodeOrDecode('save', $key, input_convert($_POST['oldvalue'])));
81+
$redis->sAdd($key, $value);
82+
}
7283
}
73-
}
7484

75-
// Set
76-
else if ($_POST['type'] == 'set') {
77-
if ($_POST['value'] != $_POST['oldvalue']) {
78-
// The only way to edit a Set value is to add it and remove the old value.
79-
$redis->sRem($key, encodeOrDecode('save', $key, input_convert($_POST['oldvalue'])));
80-
$redis->sAdd($key, $value);
85+
// ZSet
86+
else if (($_POST['type'] == 'zset') && isset($_POST['score']) && is_numeric($_POST['score'])) {
87+
// The only way to edit a ZSet value is to add it and remove the old value.
88+
$redis->zRem($key, encodeOrDecode('save', $key, input_convert($_POST['oldvalue'])));
89+
$redis->zAdd($key, input_convert($_POST['score']), $value);
8190
}
82-
}
8391

84-
// ZSet
85-
else if (($_POST['type'] == 'zset') && isset($_POST['score']) && is_numeric($_POST['score'])) {
86-
// The only way to edit a ZSet value is to add it and remove the old value.
87-
$redis->zRem($key, encodeOrDecode('save', $key, input_convert($_POST['oldvalue'])));
88-
$redis->zAdd($key, input_convert($_POST['score']), $value);
89-
}
9092

9193

94+
// Refresh the top so the key tree is updated.
95+
require 'includes/header.inc.php';
9296

93-
// Refresh the top so the key tree is updated.
94-
require 'includes/header.inc.php';
97+
?>
98+
<script>
99+
top.location.href = top.location.pathname+'?view&s=<?php echo $server['id']?>&d=<?php echo $server['db']?>&key=<?php echo urlencode($_POST['key'])?>';
100+
</script>
101+
<?php
95102

96-
?>
97-
<script>
98-
top.location.href = top.location.pathname+'?view&s=<?php echo $server['id']?>&d=<?php echo $server['db']?>&key=<?php echo urlencode($_POST['key'])?>';
99-
</script>
100-
<?php
103+
require 'includes/footer.inc.php';
104+
} catch (\Predis\Response\ServerException $th) {
105+
require 'includes/header.inc.php';
106+
?>
107+
<div class="exception">
108+
<h3><?php echo $th->getMessage() ?></h3>
109+
</div>
110+
<?php
111+
require 'includes/footer.inc.php';
112+
}
101113

102-
require 'includes/footer.inc.php';
103114
die;
104115
}
105116

index.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,13 @@ function print_namespace($item, $name, $fullkey, $islast) {
8282

8383
// Get the number of items in the key.
8484
if (!isset($config['faster']) || !$config['faster']) {
85-
switch ($redis->type($fullkey)) {
85+
$type = '';
86+
try {
87+
$type = $redis->type($fullkey);
88+
} catch (\Predis\Response\ServerException $th) {
89+
$class[] = 'empty';
90+
}
91+
switch ($type) {
8692
case 'hash':
8793
$len = $redis->hLen($fullkey);
8894
break;

view.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,18 @@
1717
die;
1818
}
1919

20-
$type = $redis->type($_GET['key']);
21-
$exists = $redis->exists($_GET['key']);
20+
$type = '';
21+
$exists = false;
22+
try {
23+
$type = $redis->type($_GET['key']);
24+
$exists = $redis->exists($_GET['key']);
25+
} catch (\Predis\Response\ServerException $th) {
26+
?>
27+
<div class="exception">
28+
<h3><?php echo $th->getMessage() ?></h3>
29+
</div>
30+
<?php
31+
}
2232

2333
$count_elements_page = isset($config['count_elements_page']) ? $config['count_elements_page'] : false;
2434
$page_num_request = isset($_GET['page']) ? (int)$_GET['page'] : 1;

0 commit comments

Comments
 (0)