index.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. require_once 'includes/common.inc.php';
  3. if($redis) {
  4. if (!empty($server['keys'])) {
  5. $keys = $redis->keys($server['filter']);
  6. } else {
  7. $next = 0;
  8. $keys = array();
  9. while (true) {
  10. $r = $redis->scan($next, 'MATCH', $server['filter'], 'COUNT', $server['scansize']);
  11. $next = $r[0];
  12. $keys = array_merge($keys, $r[1]);
  13. if ($next == 0) {
  14. break;
  15. }
  16. }
  17. }
  18. sort($keys);
  19. $namespaces = array(); // Array to hold our top namespaces.
  20. // Build an array of nested arrays containing all our namespaces and containing keys.
  21. foreach ($keys as $key) {
  22. // Ignore keys that are to long (Redis supports keys that can be way to long to put in an url).
  23. if (strlen($key) > $config['maxkeylen']) {
  24. continue;
  25. }
  26. $key = explode($server['seperator'], $key);
  27. // $d will be a reference to the current namespace.
  28. $d = &$namespaces;
  29. // We loop though all the namespaces for this key creating the array for each.
  30. // Each time updating $d to be a reference to the last namespace so we can create the next one in it.
  31. for ($i = 0; $i < (count($key) - 1); ++$i) {
  32. if (!isset($d[$key[$i]])) {
  33. $d[$key[$i]] = array();
  34. }
  35. $d = &$d[$key[$i]];
  36. }
  37. // Nodes containing an item named __phpredisadmin__ are also a key, not just a directory.
  38. // This means that creating an actual key named __phpredisadmin__ will make this bug.
  39. $d[$key[count($key) - 1]] = array('__phpredisadmin__' => true);
  40. // Unset $d so we don't accidentally overwrite it somewhere else.
  41. unset($d);
  42. }
  43. // Recursive function used to print the namespaces.
  44. function print_namespace($item, $name, $fullkey, $islast) {
  45. global $config, $server, $redis;
  46. // Is this also a key and not just a namespace?
  47. if (isset($item['__phpredisadmin__'])) {
  48. // Unset it so we won't loop over it when printing this namespace.
  49. unset($item['__phpredisadmin__']);
  50. $class = array();
  51. $len = false;
  52. if (isset($_GET['key']) && ($fullkey == $_GET['key'])) {
  53. $class[] = 'current';
  54. }
  55. if ($islast) {
  56. $class[] = 'last';
  57. }
  58. // Get the number of items in the key.
  59. if (!isset($config['faster']) || !$config['faster']) {
  60. switch ($redis->type($fullkey)) {
  61. case 'hash':
  62. $len = $redis->hLen($fullkey);
  63. break;
  64. case 'list':
  65. $len = $redis->lLen($fullkey);
  66. break;
  67. case 'set':
  68. $len = $redis->sCard($fullkey);
  69. break;
  70. case 'zset':
  71. $len = $redis->zCard($fullkey);
  72. break;
  73. }
  74. }
  75. ?>
  76. <li<?php echo empty($class) ? '' : ' class="'.implode(' ', $class).'"'?>>
  77. <a href="?view&amp;s=<?php echo $server['id']?>&amp;d=<?php echo $server['db']?>&amp;key=<?php echo urlencode($fullkey)?>"><?php echo format_html($name)?><?php if ($len !== false) { ?><span class="info">(<?php echo $len?>)</span><?php } ?></a>
  78. </li>
  79. <?php
  80. }
  81. // Does this namespace also contain subkeys?
  82. if (count($item) > 0) {
  83. ?>
  84. <li class="folder<?php echo ($fullkey === '') ? '' : ' collapsed'?><?php echo $islast ? ' last' : ''?>">
  85. <div class="icon"><?php echo format_html($name)?>&nbsp;<span class="info">(<?php echo count($item)?>)</span>
  86. <?php if ($fullkey !== '') { ?><a href="delete.php?s=<?php echo $server['id']?>&amp;d=<?php echo $server['db']?>&amp;tree=<?php echo urlencode($fullkey)?>:" class="deltree"><img src="images/delete.png" width="10" height="10" title="Delete tree" alt="[X]"></a><?php } ?>
  87. </div><ul>
  88. <?php
  89. $l = count($item);
  90. foreach ($item as $childname => $childitem) {
  91. // $fullkey will be empty on the first call.
  92. if ($fullkey === '') {
  93. $childfullkey = $childname;
  94. } else {
  95. $childfullkey = $fullkey.$server['seperator'].$childname;
  96. }
  97. print_namespace($childitem, $childname, $childfullkey, (--$l == 0));
  98. }
  99. ?>
  100. </ul>
  101. </li>
  102. <?php
  103. }
  104. }
  105. } // if redis
  106. // This is basically the same as the click code in index.js.
  107. // Just build the url for the frame based on our own url.
  108. if (count($_GET) == 0) {
  109. $iframe = 'overview.php';
  110. } else {
  111. $iframe = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], '?') + 1);
  112. if (strpos($iframe, '&') !== false) {
  113. $iframe = substr_replace($iframe, '.php?', strpos($iframe, '&'), 1);
  114. } else {
  115. $iframe .= '.php';
  116. }
  117. }
  118. $page['css'][] = 'index';
  119. $page['js'][] = 'index';
  120. $page['js'][] = 'jquery-cookie';
  121. require 'includes/header.inc.php';
  122. ?>
  123. <div id="sidebar">
  124. <h1 class="logo"><a href="?overview&amp;s=<?php echo $server['id']?>&amp;d=<?php echo $server['db']?>">phpRedisAdmin</a></h1>
  125. <p>
  126. <select id="server">
  127. <?php foreach ($config['servers'] as $i => $srv) { ?>
  128. <option value="<?php echo $i?>" <?php echo ($server['id'] == $i) ? 'selected="selected"' : ''?>><?php echo isset($srv['name']) ? format_html($srv['name']) : $srv['host'].':'.$srv['port']?></option>
  129. <?php } ?>
  130. </select>
  131. <?php if($redis) { ?>
  132. <?php
  133. if (isset($server['databases'])) {
  134. $databases = $server['databases'];
  135. } else {
  136. $databases = $redis->config('GET', 'databases');
  137. $databases = $databases['databases'];
  138. }
  139. if ($databases > 1) { ?>
  140. <select id="database">
  141. <?php for ($d = 0; $d < $databases; ++$d) { ?>
  142. <option value="<?php echo $d?>" <?php echo ($server['db'] == $d) ? 'selected="selected"' : ''?>>database <?php echo $d?></option>
  143. <?php } ?>
  144. </select>
  145. <?php } ?>
  146. </p>
  147. <p>
  148. <?php if (isset($login)) { ?>
  149. <a href="logout.php"><img src="images/logout.png" width="16" height="16" title="Logout" alt="[L]"></a>
  150. <?php } ?>
  151. <a href="?info&amp;s=<?php echo $server['id']?>&amp;d=<?php echo $server['db']?>"><img src="images/info.png" width="16" height="16" title="Info" alt="[I]"></a>
  152. <a href="?export&amp;s=<?php echo $server['id']?>&amp;d=<?php echo $server['db']?>"><img src="images/export.png" width="16" height="16" title="Export" alt="[E]"></a>
  153. <a href="?import&amp;s=<?php echo $server['id']?>&amp;d=<?php echo $server['db']?>"><img src="images/import.png" width="16" height="16" title="Import" alt="[I]"></a>
  154. <?php if (isset($server['flush']) && $server['flush']) { ?>
  155. <a href="?flush&amp;s=<?php echo $server['id']?>&amp;d=<?php echo $server['db']?>" id="flush"><img src="images/flush.png" width="16" height="16" title="Flush" alt="[F]"></a>
  156. <?php } ?>
  157. </p>
  158. <p>
  159. <a href="?edit&amp;s=<?php echo $server['id']?>&amp;d=<?php echo $server['db']?>" class="add">Add another key</a>
  160. </p>
  161. <p>
  162. <input type="text" id="server_filter" size="14" value="<?php echo format_html($server['filter']); ?>" placeholder="type here to server filter" class="info">
  163. <button id="btn_server_filter">Filter!</button>
  164. </p>
  165. <p>
  166. <input type="text" id="filter" size="24" value="type here to filter" placeholder="type here to filter" class="info">
  167. </p>
  168. <div id="keys">
  169. <ul>
  170. <?php print_namespace($namespaces, 'Keys', '', empty($namespaces))?>
  171. </ul>
  172. </div><!-- #keys -->
  173. <?php } else { ?>
  174. </p>
  175. <div style="color:red">Can't connect to this server</div>
  176. <?php } ?>
  177. </div><!-- #sidebar -->
  178. <div id="resize"></div>
  179. <div id="resize-layover"></div>
  180. <div id="frame">
  181. <iframe src="<?php echo format_html($iframe)?>" id="iframe" frameborder="0" scrolling="0"></iframe>
  182. </div><!-- #frame -->
  183. <?php
  184. require 'includes/footer.inc.php';
  185. ?>