diff options
author | Erik Dubbelboer <erik@dubbelboer.com> | 2015-04-25 07:50:03 +0300 |
---|---|---|
committer | Erik Dubbelboer <erik@dubbelboer.com> | 2015-06-20 07:19:41 +0300 |
commit | 70c48c1c7254563b425ce2f602ee675403861244 (patch) | |
tree | 8af42b13c2195eeeea3fdf9b4ccb7593f0fed018 | |
parent | 067db7ee9993bf7b0c109cfe29fc192673d66cd8 (diff) |
Add custom per key encoding (fix #69)
-rw-r--r-- | css/common.css | 5 | ||||
-rw-r--r-- | edit.php | 32 | ||||
-rw-r--r-- | includes/common.inc.php | 6 | ||||
-rw-r--r-- | includes/config.sample.inc.php | 11 | ||||
-rw-r--r-- | includes/functions.inc.php | 19 | ||||
-rw-r--r-- | view.php | 31 |
6 files changed, 82 insertions, 22 deletions
diff --git a/css/common.css b/css/common.css index 005a792..b2a8431 100644 --- a/css/common.css +++ b/css/common.css @@ -50,3 +50,8 @@ padding: 3px 0 1px 20px; background: url(../images/add.png) left center no-repeat; } + +.data { +white-space: pre; +} + @@ -27,9 +27,17 @@ if (isset($_POST['type'], $_POST['key'], $_POST['value'])) { die('ERROR: Your key is to long (max length is '.$config['maxkeylen'].')'); } + $key = input_convert($_POST['key']); + $value = input_convert($_POST['value']); + $value = encodeOrDecode('save', $key, $value); + + if ($value === false || is_null($value)) { + die('ERROR: could not encode value'); + } + // String if ($_POST['type'] == 'string') { - $redis->set(input_convert($_POST['key']), input_convert($_POST['value'])); + $redis->set($key, $value); } // Hash @@ -38,26 +46,26 @@ if (isset($_POST['type'], $_POST['key'], $_POST['value'])) { die('ERROR: Your hash key is to long (max length is '.$config['maxkeylen'].')'); } - if ($edit && !$redis->hExists(input_convert($_POST['key']), input_convert($_POST['hkey']))) { - $redis->hDel(input_convert($_POST['key']), input_convert($_GET['hkey'])); + if ($edit && !$redis->hExists($key, input_convert($_POST['hkey']))) { + $redis->hDel($key, input_convert($_GET['hkey'])); } - $redis->hSet(input_convert($_POST['key']), input_convert($_POST['hkey']), input_convert($_POST['value'])); + $redis->hSet($key, input_convert($_POST['hkey']), $value); } // List else if (($_POST['type'] == 'list') && isset($_POST['index'])) { - $size = $redis->lLen(input_convert($_POST['key'])); + $size = $redis->lLen($key); if (($_POST['index'] == '') || ($_POST['index'] == $size) || ($_POST['index'] == -1)) { // Push it at the end - $redis->rPush(input_convert($_POST['key']), input_convert($_POST['value'])); + $redis->rPush($key, $value); } else if (($_POST['index'] >= 0) && ($_POST['index'] < $size)) { // Overwrite an index - $redis->lSet(input_convert($_POST['key']), input_convert($_POST['index']), input_convert($_POST['value'])); + $redis->lSet($key, input_convert($_POST['index']), $value); } else { die('ERROR: Out of bounds index'); } @@ -67,16 +75,16 @@ if (isset($_POST['type'], $_POST['key'], $_POST['value'])) { 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(input_convert($_POST['key']), input_convert($_POST['oldvalue'])); - $redis->sAdd(input_convert($_POST['key']), input_convert($_POST['value'])); + $redis->sRem($key, encodeOrDecode('save', $key, input_convert($_POST['oldvalue']))); + $redis->sAdd($key, $value); } } // ZSet else if (($_POST['type'] == 'zset') && isset($_POST['score'])) { // The only way to edit a ZSet value is to add it and remove the old value. - $redis->zRem(input_convert($_POST['key']), input_convert($_POST['oldvalue'])); - $redis->zAdd(input_convert($_POST['key']), input_convert($_POST['score']), input_convert($_POST['value'])); + $redis->zRem($key, encodeOrDecode('save', $key, input_convert($_POST['oldvalue']))); + $redis->zAdd($key, input_convert($_POST['score']), $value); } @@ -120,6 +128,8 @@ if ($edit) { else if ((($_GET['type'] == 'set') || ($_GET['type'] == 'zset')) && isset($_GET['value'])) { $value = $_GET['value']; } + + $value = encodeOrDecode('load', $_GET['key'], $value); } diff --git a/includes/common.inc.php b/includes/common.inc.php index 0d45753..3e02caa 100644 --- a/includes/common.inc.php +++ b/includes/common.inc.php @@ -105,6 +105,12 @@ if (!isset($server['scansize'])) { $server['scansize'] = $config['scansize']; } +if (!isset($server['serialization'])) { + if (isset($config['serialization'])) { + $server['serialization'] = $config['serialization']; + } +} + // Setup a connection to Redis. $redis = !$server['port'] ? new Predis\Client($server['host']) : new Predis\Client('tcp://'.$server['host'].':'.$server['port']); try { diff --git a/includes/config.sample.inc.php b/includes/config.sample.inc.php index 9455e67..fc21162 100644 --- a/includes/config.sample.inc.php +++ b/includes/config.sample.inc.php @@ -54,12 +54,21 @@ $config = array( ),*/ + /*'serialization' => array( + 'foo*' => array( // Match like KEYS + // Function called when saving to redis. + 'save' => function($data) { return json_encode(json_decode($data)); }, + // Function called when loading from redis. + 'load' => function($data) { return json_encode(json_decode($data), JSON_PRETTY_PRINT); }, + ), + ),*/ + + // You can ignore settings below this point. 'maxkeylen' => 100, 'count_elements_page' => 100, - // Use the old KEYS command instead of SCAN to fetch all keys. 'keys' => false, diff --git a/includes/functions.inc.php b/includes/functions.inc.php index 22e9253..89368e3 100644 --- a/includes/functions.inc.php +++ b/includes/functions.inc.php @@ -16,6 +16,8 @@ function format_html($str) { function input_convert($str) { + global $server; + if (isset($server['charset']) && $server['charset']) { return mb_convert_encoding($str, $server['charset'], 'utf-8'); } else { @@ -82,3 +84,20 @@ function str_rand($length) { return $r; } + +function encodeOrDecode($action, $key, $data) { + global $server; + + if (isset($_GET['raw']) || !isset($server['serialization'])) { + return $data; + } + + foreach ($server['serialization'] as $pattern => $closures) { + if (fnmatch($pattern, $key)) { + return $closures[$action]($data); + } + } + + return $data; +} + @@ -63,12 +63,16 @@ try { switch ($type) { case 'string': $value = $redis->get($_GET['key']); + $value = encodeOrDecode('load', $_GET['key'], $value); $size = strlen($value); break; case 'hash': $values = $redis->hGetAll($_GET['key']); - $size = count($values); + foreach ($values as $k => $value) { + $values[$k] = encodeOrDecode('load', $_GET['key'], $value); + } + $size = count($values); ksort($values); break; @@ -78,13 +82,19 @@ switch ($type) { case 'set': $values = $redis->sMembers($_GET['key']); - $size = count($values); + foreach ($values as $k => $value) { + $values[$k] = encodeOrDecode('load', $_GET['key'], $value); + } + $size = count($values); sort($values); break; case 'zset': $values = $redis->zRange($_GET['key'], 0, -1); - $size = count($values); + foreach ($values as $k => $value) { + $values[$k] = encodeOrDecode('load', $_GET['key'], $value); + } + $size = count($values); break; } @@ -174,7 +184,7 @@ if (isset($pagination)) { if ($type == 'string') { ?> <table> -<tr><td><div><?php echo nl2br(format_html($value))?></div></td><td><div> +<tr><td><div class=data><?php echo format_html($value)?></div></td><td><div> <a href="edit.php?s=<?php echo $server['id']?>&d=<?php echo $server['db']?>&type=string&key=<?php echo urlencode($_GET['key'])?>"><img src="images/edit.png" width="16" height="16" title="Edit" alt="[E]"></a> </div></td><td><div> <a href="delete.php?s=<?php echo $server['id']?>&d=<?php echo $server['db']?>&type=string&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 @@ else if ($type == 'hash') { ?> <tr><th><div>Key</div></th><th><div>Value</div></th><th><div> </div></th><th><div> </div></th></tr> <?php foreach ($values as $hkey => $value) { ?> - <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> + <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> <a href="edit.php?s=<?php echo $server['id']?>&d=<?php echo $server['db']?>&type=hash&key=<?php echo urlencode($_GET['key'])?>&hkey=<?php echo urlencode($hkey)?>"><img src="images/edit.png" width="16" height="16" title="Edit" alt="[E]"></a> </div></td><td><div> <a href="delete.php?s=<?php echo $server['id']?>&d=<?php echo $server['db']?>&type=hash&key=<?php echo urlencode($_GET['key'])?>&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 @@ else if ($type == 'list') { ?> for ($i = $start; $i < $end; ++$i) { $value = $redis->lIndex($_GET['key'], $i); + $value = encodeOrDecode('load', $_GET['key'], $value); ?> - <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> + <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> <a href="edit.php?s=<?php echo $server['id']?>&d=<?php echo $server['db']?>&type=list&key=<?php echo urlencode($_GET['key'])?>&index=<?php echo $i?>"><img src="images/edit.png" width="16" height="16" title="Edit" alt="[E]"></a> </div></td><td><div> <a href="delete.php?s=<?php echo $server['id']?>&d=<?php echo $server['db']?>&type=list&key=<?php echo urlencode($_GET['key'])?>&index=<?php echo $i?>" class="delval"><img src="images/delete.png" width="16" height="16" title="Delete" alt="[X]"></a> @@ -239,9 +250,9 @@ else if ($type == 'set') { <tr><th><div>Value</div></th><th><div> </div></th><th><div> </div></th></tr> <?php foreach ($values as $value) { - $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)); + $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); ?> - <tr <?php echo $alt ? 'class="alt"' : ''?>><td><div><?php echo $display_value ?></div></td><td><div> + <tr <?php echo $alt ? 'class="alt"' : ''?>><td><div class=data><?php echo $display_value ?></div></td><td><div> <a href="edit.php?s=<?php echo $server['id']?>&d=<?php echo $server['db']?>&type=set&key=<?php echo urlencode($_GET['key'])?>&value=<?php echo urlencode($value)?>"><img src="images/edit.png" width="16" height="16" title="Edit" alt="[E]"></a> </div></td><td><div> <a href="delete.php?s=<?php echo $server['id']?>&d=<?php echo $server['db']?>&type=set&key=<?php echo urlencode($_GET['key'])?>&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 @@ else if ($type == 'zset') { ?> <?php foreach ($values as $value) { $score = $redis->zScore($_GET['key'], $value); - $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)); + $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); ?> - <tr <?php echo $alt ? 'class="alt"' : ''?>><td><div><?php echo $score?></div></td><td><div><?php echo $display_value ?></div></td><td><div> + <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> <a href="edit.php?s=<?php echo $server['id']?>&d=<?php echo $server['db']?>&type=zset&key=<?php echo urlencode($_GET['key'])?>&score=<?php echo $score?>&value=<?php echo urlencode($value)?>"><img src="images/edit.png" width="16" height="16" title="Edit" alt="[E]"></a> <a href="delete.php?s=<?php echo $server['id']?>&d=<?php echo $server['db']?>&type=zset&key=<?php echo urlencode($_GET['key'])?>&value=<?php echo urlencode($value)?>" class="delval"><img src="images/delete.png" width="16" height="16" title="Delete" alt="[X]"></a> </div></td></tr> |