forked from erikdubbelboer/phpRedisAdmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
205 lines (149 loc) · 5.76 KB
/
index.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
require_once 'common.inc.php';
// Get all keys from Redis.
$keys = $redis->keys('*');
sort($keys);
$namespaces = array(); // Array to hold our top namespaces.
// Build an array of nested arrays containing all our namespaces and containing keys.
foreach ($keys as $key) {
// Ignore keys that are to long (Redis supports keys that can be way to long to put in an url).
if (strlen($key) > $config['maxkeylen']) {
continue;
}
$key = explode($config['seperator'], $key);
// $d will be a reference to the current namespace.
$d = &$namespaces;
// We loop though all the namespaces for this key creating the array for each.
// Each time updating $d to be a reference to the last namespace so we can create the next one in it.
for ($i = 0; $i < (count($key) - 1); ++$i) {
if (!isset($d[$key[$i]])) {
$d[$key[$i]] = array();
}
$d = &$d[$key[$i]];
}
// Nodes containing an item named __phpredisadmin__ are also a key, not just a directory.
// This means that creating an actual key named __phpredisadmin__ will make this bug.
$d[$key[count($key) - 1]] = array('__phpredisadmin__' => true);
// Unset $d so we don't accidentally overwrite it somewhere else.
unset($d);
}
// This is basically the same as the click code in index.js.
// Just build the url for the frame based on our own url.
if (count($_GET) == 0) {
$iframe = 'overview.php';
} else {
$iframe = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], '?') + 1);
if (strpos($iframe, '&') !== false) {
$iframe = substr_replace($iframe, '.php?', strpos($iframe, '&'), 1);
} else {
$iframe .= '.php';
}
}
// Recursive function used to print the namespaces.
function print_namespace($item, $name, $fullkey, $islast) {
global $config, $redistypes, $server, $redis;
// Is this also a key and not just a namespace?
if (isset($item['__phpredisadmin__'])) {
// Unset it so we won't loop over it when printing this namespace.
unset($item['__phpredisadmin__']);
$type = $redis->type($fullkey);
if (!isset($redistypes[$type])) {
return;
}
$type = $redistypes[$type];
$class = array();
$len = false;
if (isset($_GET['key']) && ($fullkey == $_GET['key'])) {
$class[] = 'current';
}
if ($islast) {
$class[] = 'last';
}
// Get the number of items in the key.
if (!isset($config['faster']) || !$config['faster']) {
switch ($type) {
case 'hash':
$len = $redis->hLen($fullkey);
break;
case 'list':
$len = $redis->lSize($fullkey);
break;
case 'set':
// This is currently the only way to do this, this can be slow since we need to retrieve all keys
$len = count($redis->sMembers($fullkey));
break;
case 'zset':
// This is currently the only way to do this, this can be slow since we need to retrieve all keys
$len = count($redis->zRange($fullkey, 0, -1));
break;
}
}
?>
<li<?php echo empty($class) ? '' : ' class="'.implode(' ', $class).'"'?>>
<a href="?view&s=<?php echo $server['id']?>&key=<?php echo urlencode($fullkey)?>"><?php echo format_html($name)?><?php if ($len !== false) { ?><span class="info">(<?php echo $len?>)</span><?php } ?></a>
</li>
<?php
}
// Does this namespace also contain subkeys?
if (count($item) > 0) {
?>
<li class="folder<?php echo empty($fullkey) ? '' : ' collapsed'?><?php echo $islast ? ' last' : ''?>">
<div class="icon"><?php echo format_html($name)?> <span class="info">(<?php echo count($item)?>)</span>
<?php if (!empty($fullkey)) { ?><a href="delete.php?s=<?php echo $server['id']?>&tree=<?php echo urlencode($fullkey)?>:" class="deltree"><img src="images/delete.png" width="10" height="10" title="Delete tree" alt="[X]"></a><?php } ?>
</div><ul>
<?php
$l = count($item);
foreach ($item as $childname => $childitem) {
// $fullkey will be empty on the first call.
if (empty($fullkey)) {
$childfullkey = $childname;
} else {
$childfullkey = $fullkey.$config['seperator'].$childname;
}
print_namespace($childitem, $childname, $childfullkey, (--$l == 0));
}
?>
</ul>
</li>
<?php
}
}
$page['css'][] = 'index';
$page['js'][] = 'index';
require 'header.inc.php';
?>
<div id="sidebar">
<h1 class="logo"><a href="?overview&s=<?php echo $server['id']?>">phpRedisAdmin</a></h1>
<p>
<select id="server">
<?php foreach ($config['servers'] as $i => $srv) { ?>
<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>
<?php } ?>
</select>
</p>
<p>
<?php if (isset($login)) { ?>
<a href="logout.php"><img src="images/logout.png" width="16" height="16" title="Logout" alt="[L]"></a>
<?php } ?>
<a href="?info&s=<?php echo $server['id']?>"><img src="images/info.png" width="16" height="16" title="Info" alt="[I]"></a>
<a href="?export&s=<?php echo $server['id']?>"><img src="images/export.png" width="16" height="16" title="Export" alt="[E]"></a>
<a href="?import&s=<?php echo $server['id']?>"><img src="images/import.png" width="16" height="16" title="Import" alt="[I]"></a>
</p>
<p>
<a href="?edit&s=<?php echo $server['id']?>" class="add">Add another key</a>
</p>
<p>
<input type="text" id="filter" size="24" value="type here to filter" class="info">
</p>
<div id="keys">
<ul>
<?php print_namespace($namespaces, 'Keys', '', empty($namespaces))?>
</ul>
</div><!-- #keys -->
<div id="frame">
<iframe src="<?php echo format_html($iframe)?>" id="iframe" frameborder="0" scrolling="0"></iframe>
</div><!-- #frame -->
</div><!-- #sidebar -->
<?php
require 'footer.inc.php';
?>