22
33require_once 'includes/common.inc.php ' ;
44
5+ if ($ redis ) {
56
6- try {
7- // Get keys from Redis according to server-config.
8- $ keys = $ redis ->keys ($ server ['filter ' ]);
9- } catch (Exception $ e ) {
10- die ('ERROR: Can \'t connect to Redis ' );
11- }
7+ $ keys = $ redis ->keys ($ server ['filter ' ]);
128
13- sort ($ keys );
9+ sort ($ keys );
1410
15- $ namespaces = array (); // Array to hold our top namespaces.
11+ $ namespaces = array (); // Array to hold our top namespaces.
1612
17- // Build an array of nested arrays containing all our namespaces and containing keys.
18- foreach ($ keys as $ key ) {
19- // Ignore keys that are to long (Redis supports keys that can be way to long to put in an url).
20- if (strlen ($ key ) > $ config ['maxkeylen ' ]) {
21- continue ;
22- }
13+ // Build an array of nested arrays containing all our namespaces and containing keys.
14+ foreach ($ keys as $ key ) {
15+ // Ignore keys that are to long (Redis supports keys that can be way to long to put in an url).
16+ if (strlen ($ key ) > $ config ['maxkeylen ' ]) {
17+ continue ;
18+ }
2319
24- $ key = explode ($ server ['seperator ' ], $ key );
20+ $ key = explode ($ server ['seperator ' ], $ key );
2521
26- // $d will be a reference to the current namespace.
27- $ d = &$ namespaces ;
22+ // $d will be a reference to the current namespace.
23+ $ d = &$ namespaces ;
2824
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- }
25+ // We loop though all the namespaces for this key creating the array for each.
26+ // Each time updating $d to be a reference to the last namespace so we can create the next one in it.
27+ for ($ i = 0 ; $ i < (count ($ key ) - 1 ); ++$ i ) {
28+ if (!isset ($ d [$ key [$ i ]])) {
29+ $ d [$ key [$ i ]] = array ();
30+ }
3531
36- $ d = &$ d [$ key [$ i ]];
37- }
32+ $ d = &$ d [$ key [$ i ]];
33+ }
3834
39- // Nodes containing an item named __phpredisadmin__ are also a key, not just a directory.
40- // This means that creating an actual key named __phpredisadmin__ will make this bug.
41- $ d [$ key [count ($ key ) - 1 ]] = array ('__phpredisadmin__ ' => true );
35+ // Nodes containing an item named __phpredisadmin__ are also a key, not just a directory.
36+ // This means that creating an actual key named __phpredisadmin__ will make this bug.
37+ $ d [$ key [count ($ key ) - 1 ]] = array ('__phpredisadmin__ ' => true );
4238
43- // Unset $d so we don't accidentally overwrite it somewhere else.
44- unset($ d );
45- }
39+ // Unset $d so we don't accidentally overwrite it somewhere else.
40+ unset($ d );
41+ }
42+
43+ // Recursive function used to print the namespaces.
44+ function print_namespace ($ item , $ name , $ fullkey , $ islast ) {
45+ global $ config , $ server , $ redis ;
46+
47+ // Is this also a key and not just a namespace?
48+ if (isset ($ item ['__phpredisadmin__ ' ])) {
49+ // Unset it so we won't loop over it when printing this namespace.
50+ unset($ item ['__phpredisadmin__ ' ]);
51+
52+ $ type = $ redis ->type ($ fullkey );
53+ $ class = array ();
54+ $ len = false ;
55+
56+ if (isset ($ _GET ['key ' ]) && ($ fullkey == $ _GET ['key ' ])) {
57+ $ class [] = 'current ' ;
58+ }
59+ if ($ islast ) {
60+ $ class [] = 'last ' ;
61+ }
62+
63+ // Get the number of items in the key.
64+ if (!isset ($ config ['faster ' ]) || !$ config ['faster ' ]) {
65+ switch ($ type ) {
66+ case 'hash ' :
67+ $ len = $ redis ->hLen ($ fullkey );
68+ break ;
69+
70+ case 'list ' :
71+ $ len = $ redis ->lLen ($ fullkey );
72+ break ;
73+
74+ case 'set ' :
75+ // This is currently the only way to do this, this can be slow since we need to retrieve all keys
76+ $ len = count ($ redis ->sMembers ($ fullkey ));
77+ break ;
78+
79+ case 'zset ' :
80+ // This is currently the only way to do this, this can be slow since we need to retrieve all keys
81+ $ len = count ($ redis ->zRange ($ fullkey , 0 , -1 ));
82+ break ;
83+ }
84+ }
85+
86+
87+ ?>
88+ <li<?php echo empty ($ class ) ? '' : ' class=" ' .implode (' ' , $ class ).'" ' ?> >
89+ <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>
90+ </li>
91+ <?php
92+ }
4693
94+ // Does this namespace also contain subkeys?
95+ if (count ($ item ) > 0 ) {
96+ ?>
97+ <li class="folder<?php echo ($ fullkey === '' ) ? '' : ' collapsed ' ?> <?php echo $ islast ? ' last ' : '' ?> ">
98+ <div class="icon"><?php echo format_html ($ name )?> <span class="info">(<?php echo count ($ item )?> )</span>
99+ <?php if ($ 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 } ?>
100+ </div><ul>
101+ <?php
102+
103+ $ l = count ($ item );
104+
105+ foreach ($ item as $ childname => $ childitem ) {
106+ // $fullkey will be empty on the first call.
107+ if ($ fullkey === '' ) {
108+ $ childfullkey = $ childname ;
109+ } else {
110+ $ childfullkey = $ fullkey .$ server ['seperator ' ].$ childname ;
111+ }
112+
113+ print_namespace ($ childitem , $ childname , $ childfullkey , (--$ l == 0 ));
114+ }
115+
116+ ?>
117+ </ul>
118+ </li>
119+ <?php
120+ }
121+ }
122+
123+ } // if redis
47124
48125
49126// This is basically the same as the click code in index.js.
62139
63140
64141
65-
66-
67-
68- // Recursive function used to print the namespaces.
69- function print_namespace ($ item , $ name , $ fullkey , $ islast ) {
70- global $ config , $ server , $ redis ;
71-
72- // Is this also a key and not just a namespace?
73- if (isset ($ item ['__phpredisadmin__ ' ])) {
74- // Unset it so we won't loop over it when printing this namespace.
75- unset($ item ['__phpredisadmin__ ' ]);
76-
77- $ type = $ redis ->type ($ fullkey );
78- $ class = array ();
79- $ len = false ;
80-
81- if (isset ($ _GET ['key ' ]) && ($ fullkey == $ _GET ['key ' ])) {
82- $ class [] = 'current ' ;
83- }
84- if ($ islast ) {
85- $ class [] = 'last ' ;
86- }
87-
88- // Get the number of items in the key.
89- if (!isset ($ config ['faster ' ]) || !$ config ['faster ' ]) {
90- switch ($ type ) {
91- case 'hash ' :
92- $ len = $ redis ->hLen ($ fullkey );
93- break ;
94-
95- case 'list ' :
96- $ len = $ redis ->lLen ($ fullkey );
97- break ;
98-
99- case 'set ' :
100- // This is currently the only way to do this, this can be slow since we need to retrieve all keys
101- $ len = count ($ redis ->sMembers ($ fullkey ));
102- break ;
103-
104- case 'zset ' :
105- // This is currently the only way to do this, this can be slow since we need to retrieve all keys
106- $ len = count ($ redis ->zRange ($ fullkey , 0 , -1 ));
107- break ;
108- }
109- }
110-
111-
112- ?>
113- <li<?php echo empty ($ class ) ? '' : ' class=" ' .implode (' ' , $ class ).'" ' ?> >
114- <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>
115- </li>
116- <?php
117- }
118-
119- // Does this namespace also contain subkeys?
120- if (count ($ item ) > 0 ) {
121- ?>
122- <li class="folder<?php echo ($ fullkey === '' ) ? '' : ' collapsed ' ?> <?php echo $ islast ? ' last ' : '' ?> ">
123- <div class="icon"><?php echo format_html ($ name )?> <span class="info">(<?php echo count ($ item )?> )</span>
124- <?php if ($ 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 } ?>
125- </div><ul>
126- <?php
127-
128- $ l = count ($ item );
129-
130- foreach ($ item as $ childname => $ childitem ) {
131- // $fullkey will be empty on the first call.
132- if ($ fullkey === '' ) {
133- $ childfullkey = $ childname ;
134- } else {
135- $ childfullkey = $ fullkey .$ server ['seperator ' ].$ childname ;
136- }
137-
138- print_namespace ($ childitem , $ childname , $ childfullkey , (--$ l == 0 ));
139- }
140-
141- ?>
142- </ul>
143- </li>
144- <?php
145- }
146- }
147-
148-
149-
150-
151142$ page ['css ' ][] = 'index ' ;
152143$ page ['js ' ][] = 'index ' ;
153144
@@ -166,6 +157,8 @@ function print_namespace($item, $name, $fullkey, $islast) {
166157</select>
167158</p>
168159
160+ <?php if ($ redis ): ?>
161+
169162<p>
170163<?php if (isset ($ login )) { ?>
171164<a href="logout.php"><img src="images/logout.png" width="16" height="16" title="Logout" alt="[L]"></a>
@@ -192,6 +185,10 @@ function print_namespace($item, $name, $fullkey, $islast) {
192185</ul>
193186</div><!-- #keys -->
194187
188+ <?php else : ?>
189+ <div style="color:red">Can't connect to this server</div>
190+ <?php endif ; ?>
191+
195192<div id="frame">
196193<iframe src="<?php echo format_html ($ iframe )?> " id="iframe" frameborder="0" scrolling="0"></iframe>
197194</div><!-- #frame -->
0 commit comments