forked from erikdubbelboer/phpRedisAdmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit.php
186 lines (138 loc) · 5.39 KB
/
edit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
require_once 'includes/common.inc.php';
// Are we editing or creating a new key?
$edit = false;
if (isset($_GET['key'], $_GET['type'])) {
if (($_GET['type'] == 'string') ||
(($_GET['type'] == 'hash') && isset($_GET['hkey'])) ||
(($_GET['type'] == 'list') && isset($_GET['index'])) ||
(($_GET['type'] == 'set' ) && isset($_GET['value'])) ||
(($_GET['type'] == 'zset') && isset($_GET['value']))) {
$edit = true;
}
}
if (isset($_POST['type'], $_POST['key'], $_POST['value'])) {
// Don't allow keys that are to long (Redis supports keys that can be way to long to put in an url).
if (strlen($_POST['key']) > $config['maxkeylen']) {
die('ERROR: Your key is to long (max length is '.$config['maxkeylen'].')');
}
// String
if ($_POST['type'] == 'string') {
$redis->set($_POST['key'], $_POST['value']);
}
// Hash
else if (($_POST['type'] == 'hash') && isset($_POST['hkey'])) {
if (strlen($_POST['hkey']) > $config['maxkeylen']) {
die('ERROR: Your hash key is to long (max length is '.$config['maxkeylen'].')');
}
if ($edit && !$redis->hExists($_POST['key'], $_POST['hkey'])) {
$redis->hDel($_POST['key'], $_GET['hkey']);
}
$redis->hSet($_POST['key'], $_POST['hkey'], $_POST['value']);
}
// List
else if (($_POST['type'] == 'list') && isset($_POST['index'])) {
$size = $redis->lLen($_POST['key']);
if (($_POST['index'] == '') ||
($_POST['index'] == $size) ||
($_POST['index'] == -1)) {
// Push it at the end
$redis->rPush($_POST['key'], $_POST['value']);
} else if (($_POST['index'] >= 0) &&
($_POST['index'] < $size)) {
// Overwrite an index
$redis->lSet($_POST['key'], $_POST['index'], $_POST['value']);
} else {
die('ERROR: Out of bounds index');
}
}
// Set
else if ($_POST['type'] == 'set') {
if ($_POST['value'] != $_POST['oldvalue']) {
// The only way to edit a Set value is to add it and remove the old value.
$redis->sRem($_POST['key'], $_POST['oldvalue']);
$redis->sAdd($_POST['key'], $_POST['value']);
}
}
// ZSet
else if (($_POST['type'] == 'zset') && isset($_POST['score'])) {
if ($_POST['value'] != $_POST['oldvalue']) {
// The only way to edit a ZSet value is to add it and remove the old value.
$redis->zRem($_POST['key'], $_POST['oldvalue']);
$redis->zAdd($_POST['key'], $_POST['score'], $_POST['value']);
}
}
// Refresh the top so the key tree is updated.
require 'includes/header.inc.php';
?>
<script>
top.location.href = top.location.pathname+'?view&s=<?php echo $server['id']?>&key=<?php echo urlencode($_POST['key'])?>';
</script>
<?php
require 'includes/footer.inc.php';
die;
}
// Get the current value.
$value = '';
if ($edit) {
// String
if ($_GET['type'] == 'string') {
$value = $redis->get($_GET['key']);
}
// Hash
else if (($_GET['type'] == 'hash') && isset($_GET['hkey'])) {
$value = $redis->hGet($_GET['key'], $_GET['hkey']);
}
// List
else if (($_GET['type'] == 'list') && isset($_GET['index'])) {
$value = $redis->lIndex($_GET['key'], $_GET['index']);
}
// Set, ZSet
else if ((($_GET['type'] == 'set') || ($_GET['type'] == 'zset')) && isset($_GET['value'])) {
$value = $_GET['value'];
}
}
$page['css'][] = 'frame';
$page['js'][] = 'frame';
require 'includes/header.inc.php';
?>
<h2><?php echo $edit ? 'Edit' : 'Add'?></h2>
<form action="<?php echo format_html($_SERVER['REQUEST_URI'])?>" method="post">
<p>
<label for="type">Type:</label>
<select name="type" id="type">
<option value="string" <?php echo (isset($_GET['type']) && ($_GET['type'] == 'string')) ? 'selected="selected"' : ''?>>String</option>
<option value="hash" <?php echo (isset($_GET['type']) && ($_GET['type'] == 'hash' )) ? 'selected="selected"' : ''?>>Hash</option>
<option value="list" <?php echo (isset($_GET['type']) && ($_GET['type'] == 'list' )) ? 'selected="selected"' : ''?>>List</option>
<option value="set" <?php echo (isset($_GET['type']) && ($_GET['type'] == 'set' )) ? 'selected="selected"' : ''?>>Set</option>
<option value="zset" <?php echo (isset($_GET['type']) && ($_GET['type'] == 'zset' )) ? 'selected="selected"' : ''?>>ZSet</option>
</select>
</p>
<p>
<label for="key">Key:</label>
<input type="text" name="key" id="key" size="30" maxlength="30" <?php echo isset($_GET['key']) ? 'value="'.format_html($_GET['key']).'"' : ''?>>
</p>
<p id="hkeyp">
<label for="khey">Hash key:</label>
<input type="text" name="hkey" id="hkey" size="30" maxlength="30" <?php echo isset($_GET['hkey']) ? 'value="'.format_html($_GET['hkey']).'"' : ''?>>
</p>
<p id="indexp">
<label for="index">Index:</label>
<input type="text" name="index" id="index" size="30" <?php echo isset($_GET['index']) ? 'value="'.format_html($_GET['index']).'"' : ''?>> <span class="info">empty to append, -1 to prepend</span>
</p>
<p id="scorep">
<label for="score">Score:</label>
<input type="text" name="score" id="score" size="30" <?php echo isset($_GET['score']) ? 'value="'.format_html($_GET['score']).'"' : ''?>>
</p>
<p>
<label for="value">Value:</label>
<textarea name="value" id="value" cols="80" rows="20"><?php echo format_html($value)?></textarea>
</p>
<input type="hidden" name="oldvalue" value="<?php echo format_html($value)?>">
<p>
<input type="submit" class="button" value="<?php echo $edit ? 'Edit' : 'Add'?>">
</p>
</form>
<?php
require 'includes/footer.inc.php';
?>