forked from erikdubbelboer/phpRedisAdmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export.php
185 lines (129 loc) · 3.36 KB
/
export.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
<?php
require_once 'includes/common.inc.php';
// Export to redis-cli commands
function export_redis($key) {
global $redis;
$type = $redis->type($key);
// String
if ($type == 'string') {
echo 'SET "',addslashes($key),'" "',addslashes($redis->get($key)),'"',PHP_EOL;
}
// Hash
else if ($type == 'hash') {
$values = $redis->hGetAll($key);
foreach ($values as $k => $v) {
echo 'HSET "',addslashes($key),'" "',addslashes($k),'" "',addslashes($v),'"',PHP_EOL;
}
}
// List
else if ($type == 'list') {
$size = $redis->lLen($key);
for ($i = 0; $i < $size; ++$i) {
echo 'RPUSH "',addslashes($key),'" "',addslashes($redis->lIndex($key, $i)),'"',PHP_EOL;
}
}
// Set
else if ($type == 'set') {
$values = $redis->sMembers($key);
foreach ($values as $v) {
echo 'SADD "',addslashes($key),'" "',addslashes($v),'"',PHP_EOL;
}
}
// ZSet
else if ($type == 'zset') {
$values = $redis->zRange($key, 0, -1);
foreach ($values as $v) {
$s = $redis->zScore($key, $v);
echo 'ZADD "',addslashes($key),'" ',$s,' "',addslashes($v),'"',PHP_EOL;
}
}
}
// Return the JSON for this key
function export_json($key) {
global $redis;
$type = $redis->type($key);
// String
if ($type == 'string') {
$value = $redis->get($key);
}
// Hash
else if ($type == 'hash') {
$value = $redis->hGetAll($key);
}
// List
else if ($type == 'list') {
$size = $redis->lLen($key);
$value = array();
for ($i = 0; $i < $size; ++$i) {
$value[] = $redis->lIndex($key, $i);
}
}
// Set
else if ($type == 'set') {
$value = $redis->sMembers($key);
}
// ZSet
else if ($type == 'zset') {
$value = $redis->zRange($key, 0, -1);
}
return $value;
}
// Export
if (isset($_POST['type'])) {
if ($_POST['type'] == 'json') {
$ext = 'js';
$ct = 'application/json';
} else {
$ext = 'redis';
$ct = 'text/plain';
}
header('Content-type: '.$ct.'; charset=utf-8');
header('Content-Disposition: inline; filename="export.'.$ext.'"');
// JSON
if ($_POST['type'] == 'json') {
// Single key
if (isset($_GET['key'])) {
echo json_encode(export_json($_GET['key']));
} else { // All keys
$keys = $redis->keys('*');
$vals = array();
foreach ($keys as $key) {
$vals[$key] = export_json($key);
}
echo json_encode($vals);
}
}
// Redis Commands
else {
// Single key
if (isset($_GET['key'])) {
export_redis($_GET['key']);
} else { // All keys
$keys = $redis->keys('*');
foreach ($keys as $key) {
export_redis($key);
}
}
}
die;
}
$page['css'][] = 'frame';
$page['js'][] = 'frame';
require 'includes/header.inc.php';
?>
<h2>Export <?php echo isset($_GET['key']) ? format_html($_GET['key']) : ''?></h2>
<form action="<?php echo format_html($_SERVER['REQUEST_URI'])?>" method="post">
<p>
<label for="type">Type:</label>
<select name="type" id="type">
<option value="redis" <?php echo (isset($_GET['type']) && ($_GET['type'] == 'redis')) ? 'selected="selected"' : ''?>>Redis</option>
<option value="json" <?php echo (isset($_GET['type']) && ($_GET['type'] == 'json' )) ? 'selected="selected"' : ''?>>JSON</option>
</select>
</p>
<p>
<input type="submit" class="button" value="Export">
</p>
</form>
<?php
require 'includes/footer.inc.php';
?>