edit.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. require_once 'includes/common.inc.php';
  3. // Are we editing or creating a new key?
  4. $edit = false;
  5. if (isset($_GET['key'], $_GET['type'])) {
  6. if (($_GET['type'] == 'string') ||
  7. (($_GET['type'] == 'hash') && isset($_GET['hkey'])) ||
  8. (($_GET['type'] == 'list') && isset($_GET['index'])) ||
  9. (($_GET['type'] == 'set' ) && isset($_GET['value'])) ||
  10. (($_GET['type'] == 'zset') && isset($_GET['value']))) {
  11. $edit = true;
  12. }
  13. }
  14. if (isset($_POST['type'], $_POST['key'], $_POST['value'])) {
  15. // Don't allow keys that are to long (Redis supports keys that can be way to long to put in an url).
  16. if (strlen($_POST['key']) > $config['maxkeylen']) {
  17. die('ERROR: Your key is to long (max length is '.$config['maxkeylen'].')');
  18. }
  19. $key = input_convert($_POST['key']);
  20. $value = input_convert($_POST['value']);
  21. $value = encodeOrDecode('save', $key, $value);
  22. if ($value === false || is_null($value)) {
  23. die('ERROR: could not encode value');
  24. }
  25. // String
  26. if ($_POST['type'] == 'string') {
  27. $redis->set($key, $value);
  28. }
  29. // Hash
  30. else if (($_POST['type'] == 'hash') && isset($_POST['hkey'])) {
  31. if (strlen($_POST['hkey']) > $config['maxkeylen']) {
  32. die('ERROR: Your hash key is to long (max length is '.$config['maxkeylen'].')');
  33. }
  34. if ($edit && !$redis->hExists($key, input_convert($_POST['hkey']))) {
  35. $redis->hDel($key, input_convert($_GET['hkey']));
  36. }
  37. $redis->hSet($key, input_convert($_POST['hkey']), $value);
  38. }
  39. // List
  40. else if (($_POST['type'] == 'list') && isset($_POST['index'])) {
  41. $size = $redis->lLen($key);
  42. if (($_POST['index'] == '') ||
  43. ($_POST['index'] == $size) ||
  44. ($_POST['index'] == -1)) {
  45. // Push it at the end
  46. $redis->rPush($key, $value);
  47. } else if (($_POST['index'] >= 0) &&
  48. ($_POST['index'] < $size)) {
  49. // Overwrite an index
  50. $redis->lSet($key, input_convert($_POST['index']), $value);
  51. } else {
  52. die('ERROR: Out of bounds index');
  53. }
  54. }
  55. // Set
  56. else if ($_POST['type'] == 'set') {
  57. if ($_POST['value'] != $_POST['oldvalue']) {
  58. // The only way to edit a Set value is to add it and remove the old value.
  59. $redis->sRem($key, encodeOrDecode('save', $key, input_convert($_POST['oldvalue'])));
  60. $redis->sAdd($key, $value);
  61. }
  62. }
  63. // ZSet
  64. else if (($_POST['type'] == 'zset') && isset($_POST['score'])) {
  65. // The only way to edit a ZSet value is to add it and remove the old value.
  66. $redis->zRem($key, encodeOrDecode('save', $key, input_convert($_POST['oldvalue'])));
  67. $redis->zAdd($key, input_convert($_POST['score']), $value);
  68. }
  69. // Refresh the top so the key tree is updated.
  70. require 'includes/header.inc.php';
  71. ?>
  72. <script>
  73. top.location.href = top.location.pathname+'?view&s=<?php echo $server['id']?>&d=<?php echo $server['db']?>&key=<?php echo urlencode($_POST['key'])?>';
  74. </script>
  75. <?php
  76. require 'includes/footer.inc.php';
  77. die;
  78. }
  79. // Get the current value.
  80. $value = '';
  81. if ($edit) {
  82. // String
  83. if ($_GET['type'] == 'string') {
  84. $value = $redis->get($_GET['key']);
  85. }
  86. // Hash
  87. else if (($_GET['type'] == 'hash') && isset($_GET['hkey'])) {
  88. $value = $redis->hGet($_GET['key'], $_GET['hkey']);
  89. }
  90. // List
  91. else if (($_GET['type'] == 'list') && isset($_GET['index'])) {
  92. $value = $redis->lIndex($_GET['key'], $_GET['index']);
  93. }
  94. // Set, ZSet
  95. else if ((($_GET['type'] == 'set') || ($_GET['type'] == 'zset')) && isset($_GET['value'])) {
  96. $value = $_GET['value'];
  97. }
  98. $value = encodeOrDecode('load', $_GET['key'], $value);
  99. }
  100. $page['css'][] = 'frame';
  101. $page['js'][] = 'frame';
  102. require 'includes/header.inc.php';
  103. ?>
  104. <h2><?php echo $edit ? 'Edit' : 'Add'?></h2>
  105. <form action="<?php echo format_html($_SERVER['REQUEST_URI'])?>" method="post">
  106. <p>
  107. <label for="type">Type:</label>
  108. <select name="type" id="type">
  109. <option value="string" <?php echo (isset($_GET['type']) && ($_GET['type'] == 'string')) ? 'selected="selected"' : ''?>>String</option>
  110. <option value="hash" <?php echo (isset($_GET['type']) && ($_GET['type'] == 'hash' )) ? 'selected="selected"' : ''?>>Hash</option>
  111. <option value="list" <?php echo (isset($_GET['type']) && ($_GET['type'] == 'list' )) ? 'selected="selected"' : ''?>>List</option>
  112. <option value="set" <?php echo (isset($_GET['type']) && ($_GET['type'] == 'set' )) ? 'selected="selected"' : ''?>>Set</option>
  113. <option value="zset" <?php echo (isset($_GET['type']) && ($_GET['type'] == 'zset' )) ? 'selected="selected"' : ''?>>ZSet</option>
  114. </select>
  115. </p>
  116. <p>
  117. <label for="key">Key:</label>
  118. <input type="text" name="key" id="key" size="30" <?php echo isset($_GET['key']) ? 'value="'.format_html($_GET['key']).'"' : ''?>>
  119. </p>
  120. <p id="hkeyp">
  121. <label for="khey">Hash key:</label>
  122. <input type="text" name="hkey" id="hkey" size="30" <?php echo isset($_GET['hkey']) ? 'value="'.format_html($_GET['hkey']).'"' : ''?>>
  123. </p>
  124. <p id="indexp">
  125. <label for="index">Index:</label>
  126. <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>
  127. </p>
  128. <p id="scorep">
  129. <label for="score">Score:</label>
  130. <input type="text" name="score" id="score" size="30" <?php echo isset($_GET['score']) ? 'value="'.format_html($_GET['score']).'"' : ''?>>
  131. </p>
  132. <p>
  133. <label for="value">Value:</label>
  134. <textarea name="value" id="value" cols="80" rows="20"><?php echo format_html($value)?></textarea>
  135. </p>
  136. <input type="hidden" name="oldvalue" value="<?php echo format_html($value)?>">
  137. <p>
  138. <input type="submit" class="button" value="<?php echo $edit ? 'Edit' : 'Add'?>">
  139. </p>
  140. </form>
  141. <?php
  142. require 'includes/footer.inc.php';
  143. ?>