-
-
Notifications
You must be signed in to change notification settings - Fork 667
/
export.php
220 lines (157 loc) · 4.59 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
require_once 'includes/common.inc.php';
global $redis, $config, $csrfToken, $server;
// Export to redis-cli commands
function export_redis($key, $filter = false, $transform = false) {
global $redis;
$type = $redis->type($key);
// we rename the keys as necessary
if($filter !== false && $transform !== false)
$outputKey = str_replace($filter, $transform, $key);
else
$outputKey = $key;
// String
if ($type == 'string') {
echo 'SET "',addslashes($outputKey),'" "',addslashes($redis->get($key)),'"',PHP_EOL;
}
// Hash
else if ($type == 'hash') {
$values = $redis->hGetAll($key);
foreach ($values as $k => $v) {
echo 'HSET "',addslashes($outputKey),'" "',addslashes($k),'" "',addslashes($v),'"',PHP_EOL;
}
}
// List
else if ($type == 'list') {
$size = $redis->lLen($key);
for ($i = 0; $i < $size; ++$i) {
echo 'RPUSH "',addslashes($outputKey),'" "',addslashes($redis->lIndex($key, $i)),'"',PHP_EOL;
}
}
// Set
else if ($type == 'set') {
$values = $redis->sMembers($key);
foreach ($values as $v) {
echo 'SADD "',addslashes($outputKey),'" "',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($outputKey),'" ',$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.'"');
$filter = !empty($_POST['filter']) ? trim($_POST['filter']) : false;
$transform = !empty($_POST['transform']) ? trim($_POST['transform']) : false;
// 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) {
// if we have a filter and no match, nothing to do
if($filter !== false && stripos($key, $filter) === false)
continue;
// we rename the keys as necessary
if($filter !== false && $transform !== false)
$outputKey = str_replace($filter, $transform, $key);
else
$outputKey = $key;
$vals[$outputKey] = 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) {
// if we have a filter and no match, we skip
if($filter !== false && stripos($key, $filter) === false)
continue;
export_redis($key, $filter, $transform);
}
}
}
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(getRelativePath('export.php'))?>" method="post">
<input type="hidden" name="csrf" value="<?php echo $csrfToken; ?>" />
<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>
<?php if (!isset($_GET['key'])): ?>
<p>
<label for="filter">Filter:</label>
<input type="text" name="filter" />
</p>
<p>
<label for="transform">Tranform:</label>
<input type="text" name="transform" />
</p>
<?php endif; ?>
<input type="submit" class="button" value="Export">
</form>
<?php
require 'includes/footer.inc.php';
?>