Skip to content

Commit 2ec3956

Browse files
Add custom per key encoding
1 parent 067db7e commit 2ec3956

File tree

6 files changed

+82
-22
lines changed

6 files changed

+82
-22
lines changed

css/common.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,8 @@ padding: 3px 0 1px 20px;
5050
background: url(../images/add.png) left center no-repeat;
5151
}
5252

53+
54+
.data {
55+
white-space: pre;
56+
}
57+

edit.php

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,17 @@
2727
die('ERROR: Your key is to long (max length is '.$config['maxkeylen'].')');
2828
}
2929

30+
$key = input_convert($_POST['key']);
31+
$value = input_convert($_POST['value']);
32+
$value = encodeOrDecode('save', $key, $value);
33+
34+
if ($value === false || is_null($value)) {
35+
die('ERROR: could not encode value');
36+
}
37+
3038
// String
3139
if ($_POST['type'] == 'string') {
32-
$redis->set(input_convert($_POST['key']), input_convert($_POST['value']));
40+
$redis->set($key, $value);
3341
}
3442

3543
// Hash
@@ -38,26 +46,26 @@
3846
die('ERROR: Your hash key is to long (max length is '.$config['maxkeylen'].')');
3947
}
4048

41-
if ($edit && !$redis->hExists(input_convert($_POST['key']), input_convert($_POST['hkey']))) {
42-
$redis->hDel(input_convert($_POST['key']), input_convert($_GET['hkey']));
49+
if ($edit && !$redis->hExists($key, input_convert($_POST['hkey']))) {
50+
$redis->hDel($key, input_convert($_GET['hkey']));
4351
}
4452

45-
$redis->hSet(input_convert($_POST['key']), input_convert($_POST['hkey']), input_convert($_POST['value']));
53+
$redis->hSet($key, input_convert($_POST['hkey']), $value);
4654
}
4755

4856
// List
4957
else if (($_POST['type'] == 'list') && isset($_POST['index'])) {
50-
$size = $redis->lLen(input_convert($_POST['key']));
58+
$size = $redis->lLen($key);
5159

5260
if (($_POST['index'] == '') ||
5361
($_POST['index'] == $size) ||
5462
($_POST['index'] == -1)) {
5563
// Push it at the end
56-
$redis->rPush(input_convert($_POST['key']), input_convert($_POST['value']));
64+
$redis->rPush($key, $value);
5765
} else if (($_POST['index'] >= 0) &&
5866
($_POST['index'] < $size)) {
5967
// Overwrite an index
60-
$redis->lSet(input_convert($_POST['key']), input_convert($_POST['index']), input_convert($_POST['value']));
68+
$redis->lSet($key, input_convert($_POST['index']), $value);
6169
} else {
6270
die('ERROR: Out of bounds index');
6371
}
@@ -67,16 +75,16 @@
6775
else if ($_POST['type'] == 'set') {
6876
if ($_POST['value'] != $_POST['oldvalue']) {
6977
// The only way to edit a Set value is to add it and remove the old value.
70-
$redis->sRem(input_convert($_POST['key']), input_convert($_POST['oldvalue']));
71-
$redis->sAdd(input_convert($_POST['key']), input_convert($_POST['value']));
78+
$redis->sRem($key, encodeOrDecode('save', $key, input_convert($_POST['oldvalue'])));
79+
$redis->sAdd($key, $value);
7280
}
7381
}
7482

7583
// ZSet
7684
else if (($_POST['type'] == 'zset') && isset($_POST['score'])) {
7785
// The only way to edit a ZSet value is to add it and remove the old value.
78-
$redis->zRem(input_convert($_POST['key']), input_convert($_POST['oldvalue']));
79-
$redis->zAdd(input_convert($_POST['key']), input_convert($_POST['score']), input_convert($_POST['value']));
86+
$redis->zRem($key, encodeOrDecode('save', $key, input_convert($_POST['oldvalue'])));
87+
$redis->zAdd($key, input_convert($_POST['score']), $value);
8088
}
8189

8290

@@ -120,6 +128,8 @@
120128
else if ((($_GET['type'] == 'set') || ($_GET['type'] == 'zset')) && isset($_GET['value'])) {
121129
$value = $_GET['value'];
122130
}
131+
132+
$value = encodeOrDecode('load', $_GET['key'], $value);
123133
}
124134

125135

includes/common.inc.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@
105105
$server['scansize'] = $config['scansize'];
106106
}
107107

108+
if (!isset($server['serialization'])) {
109+
if (isset($config['serialization'])) {
110+
$server['serialization'] = $config['serialization'];
111+
}
112+
}
113+
108114
// Setup a connection to Redis.
109115
$redis = !$server['port'] ? new Predis\Client($server['host']) : new Predis\Client('tcp://'.$server['host'].':'.$server['port']);
110116
try {

includes/config.sample.inc.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,21 @@
5454
),*/
5555

5656

57+
/*'serialization' => array(
58+
'foo*' => array( // Match like KEYS
59+
// Function called when saving to redis.
60+
'save' => function($data) { return json_encode(json_decode($data)); },
61+
// Function called when loading from redis.
62+
'load' => function($data) { return json_encode(json_decode($data), JSON_PRETTY_PRINT); },
63+
),
64+
),*/
65+
66+
5767
// You can ignore settings below this point.
5868

5969
'maxkeylen' => 100,
6070
'count_elements_page' => 100,
6171

62-
6372
// Use the old KEYS command instead of SCAN to fetch all keys.
6473
'keys' => false,
6574

includes/functions.inc.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ function format_html($str) {
1616

1717

1818
function input_convert($str) {
19+
global $server;
20+
1921
if (isset($server['charset']) && $server['charset']) {
2022
return mb_convert_encoding($str, $server['charset'], 'utf-8');
2123
} else {
@@ -82,3 +84,20 @@ function str_rand($length) {
8284
return $r;
8385
}
8486

87+
88+
function encodeOrDecode($action, $key, $data) {
89+
global $server;
90+
91+
if (isset($_GET['raw']) || !isset($server['serialization'])) {
92+
return $data;
93+
}
94+
95+
foreach ($server['serialization'] as $pattern => $closures) {
96+
if (fnmatch($pattern, $key)) {
97+
return $closures[$action]($data);
98+
}
99+
}
100+
101+
return $data;
102+
}
103+

view.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,16 @@
6363
switch ($type) {
6464
case 'string':
6565
$value = $redis->get($_GET['key']);
66+
$value = encodeOrDecode('load', $_GET['key'], $value);
6667
$size = strlen($value);
6768
break;
6869

6970
case 'hash':
7071
$values = $redis->hGetAll($_GET['key']);
71-
$size = count($values);
72+
foreach ($values as $k => $value) {
73+
$values[$k] = encodeOrDecode('load', $_GET['key'], $value);
74+
}
75+
$size = count($values);
7276
ksort($values);
7377
break;
7478

@@ -78,13 +82,19 @@
7882

7983
case 'set':
8084
$values = $redis->sMembers($_GET['key']);
81-
$size = count($values);
85+
foreach ($values as $k => $value) {
86+
$values[$k] = encodeOrDecode('load', $_GET['key'], $value);
87+
}
88+
$size = count($values);
8289
sort($values);
8390
break;
8491

8592
case 'zset':
8693
$values = $redis->zRange($_GET['key'], 0, -1);
87-
$size = count($values);
94+
foreach ($values as $k => $value) {
95+
$values[$k] = encodeOrDecode('load', $_GET['key'], $value);
96+
}
97+
$size = count($values);
8898
break;
8999
}
90100

@@ -174,7 +184,7 @@
174184
if ($type == 'string') { ?>
175185

176186
<table>
177-
<tr><td><div><?php echo nl2br(format_html($value))?></div></td><td><div>
187+
<tr><td><div class=data><?php echo format_html($value)?></div></td><td><div>
178188
<a href="edit.php?s=<?php echo $server['id']?>&amp;d=<?php echo $server['db']?>&amp;type=string&amp;key=<?php echo urlencode($_GET['key'])?>"><img src="images/edit.png" width="16" height="16" title="Edit" alt="[E]"></a>
179189
</div></td><td><div>
180190
<a href="delete.php?s=<?php echo $server['id']?>&amp;d=<?php echo $server['db']?>&amp;type=string&amp;key=<?php echo urlencode($_GET['key'])?>" class="delval"><img src="images/delete.png" width="16" height="16" title="Delete" alt="[X]"></a>
@@ -192,7 +202,7 @@
192202
<tr><th><div>Key</div></th><th><div>Value</div></th><th><div>&nbsp;</div></th><th><div>&nbsp;</div></th></tr>
193203

194204
<?php foreach ($values as $hkey => $value) { ?>
195-
<tr <?php echo $alt ? 'class="alt"' : ''?>><td><div><?php echo format_html($hkey)?></div></td><td><div><?php echo nl2br(format_html($value))?></div></td><td><div>
205+
<tr <?php echo $alt ? 'class="alt"' : ''?>><td><div><?php echo format_html($hkey)?></div></td><td><div class=data><?php echo format_html($value)?></div></td><td><div>
196206
<a href="edit.php?s=<?php echo $server['id']?>&amp;d=<?php echo $server['db']?>&amp;type=hash&amp;key=<?php echo urlencode($_GET['key'])?>&amp;hkey=<?php echo urlencode($hkey)?>"><img src="images/edit.png" width="16" height="16" title="Edit" alt="[E]"></a>
197207
</div></td><td><div>
198208
<a href="delete.php?s=<?php echo $server['id']?>&amp;d=<?php echo $server['db']?>&amp;type=hash&amp;key=<?php echo urlencode($_GET['key'])?>&amp;hkey=<?php echo urlencode($hkey)?>" class="delval"><img src="images/delete.png" width="16" height="16" title="Delete" alt="[X]"></a>
@@ -219,8 +229,9 @@
219229

220230
for ($i = $start; $i < $end; ++$i) {
221231
$value = $redis->lIndex($_GET['key'], $i);
232+
$value = encodeOrDecode('load', $_GET['key'], $value);
222233
?>
223-
<tr <?php echo $alt ? 'class="alt"' : ''?>><td><div><?php echo $i?></div></td><td><div><?php echo nl2br(format_html($value))?></div></td><td><div>
234+
<tr <?php echo $alt ? 'class="alt"' : ''?>><td><div><?php echo $i?></div></td><td><div class=data><?php echo format_html($value)?></div></td><td><div>
224235
<a href="edit.php?s=<?php echo $server['id']?>&amp;d=<?php echo $server['db']?>&amp;type=list&amp;key=<?php echo urlencode($_GET['key'])?>&amp;index=<?php echo $i?>"><img src="images/edit.png" width="16" height="16" title="Edit" alt="[E]"></a>
225236
</div></td><td><div>
226237
<a href="delete.php?s=<?php echo $server['id']?>&amp;d=<?php echo $server['db']?>&amp;type=list&amp;key=<?php echo urlencode($_GET['key'])?>&amp;index=<?php echo $i?>" class="delval"><img src="images/delete.png" width="16" height="16" title="Delete" alt="[X]"></a>
@@ -239,9 +250,9 @@
239250
<tr><th><div>Value</div></th><th><div>&nbsp;</div></th><th><div>&nbsp;</div></th></tr>
240251

241252
<?php foreach ($values as $value) {
242-
$display_value = $redis->exists($value) ? '<a href="view.php?s='.$server['id'].'&d='.$server['db'].'&key='.urlencode($value).'">'.nl2br(format_html($value)).'</a>' : nl2br(format_html($value));
253+
$display_value = $redis->exists($value) ? '<a href="view.php?s='.$server['id'].'&d='.$server['db'].'&key='.urlencode($value).'">'.format_html($value).'</a>' : format_html($value);
243254
?>
244-
<tr <?php echo $alt ? 'class="alt"' : ''?>><td><div><?php echo $display_value ?></div></td><td><div>
255+
<tr <?php echo $alt ? 'class="alt"' : ''?>><td><div class=data><?php echo $display_value ?></div></td><td><div>
245256
<a href="edit.php?s=<?php echo $server['id']?>&amp;d=<?php echo $server['db']?>&amp;type=set&amp;key=<?php echo urlencode($_GET['key'])?>&amp;value=<?php echo urlencode($value)?>"><img src="images/edit.png" width="16" height="16" title="Edit" alt="[E]"></a>
246257
</div></td><td><div>
247258
<a href="delete.php?s=<?php echo $server['id']?>&amp;d=<?php echo $server['db']?>&amp;type=set&amp;key=<?php echo urlencode($_GET['key'])?>&amp;value=<?php echo urlencode($value)?>" class="delval"><img src="images/delete.png" width="16" height="16" title="Delete" alt="[X]"></a>
@@ -260,9 +271,9 @@
260271

261272
<?php foreach ($values as $value) {
262273
$score = $redis->zScore($_GET['key'], $value);
263-
$display_value = $redis->exists($value) ? '<a href="view.php?s='.$server['id'].'&d='.$server['db'].'&key='.urlencode($value).'">'.nl2br(format_html($value)).'</a>' : nl2br(format_html($value));
274+
$display_value = $redis->exists($value) ? '<a href="view.php?s='.$server['id'].'&d='.$server['db'].'&key='.urlencode($value).'">'.format_html($value).'</a>' : format_html($value);
264275
?>
265-
<tr <?php echo $alt ? 'class="alt"' : ''?>><td><div><?php echo $score?></div></td><td><div><?php echo $display_value ?></div></td><td><div>
276+
<tr <?php echo $alt ? 'class="alt"' : ''?>><td><div><?php echo $score?></div></td><td><div class=data><?php echo $display_value ?></div></td><td><div>
266277
<a href="edit.php?s=<?php echo $server['id']?>&amp;d=<?php echo $server['db']?>&amp;type=zset&amp;key=<?php echo urlencode($_GET['key'])?>&amp;score=<?php echo $score?>&amp;value=<?php echo urlencode($value)?>"><img src="images/edit.png" width="16" height="16" title="Edit" alt="[E]"></a>
267278
<a href="delete.php?s=<?php echo $server['id']?>&amp;d=<?php echo $server['db']?>&amp;type=zset&amp;key=<?php echo urlencode($_GET['key'])?>&amp;value=<?php echo urlencode($value)?>" class="delval"><img src="images/delete.png" width="16" height="16" title="Delete" alt="[X]"></a>
268279
</div></td></tr>

0 commit comments

Comments
 (0)