-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflushdb.php
More file actions
221 lines (201 loc) · 4.4 KB
/
Copy pathflushdb.php
File metadata and controls
221 lines (201 loc) · 4.4 KB
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
221
<?php
session_start();
if (!isset($_GET['id']) || isset($_GET['id']) && $_GET['id'] != $_SESSION['id'])
{
$_SESSION['id'] = '';
die("Invalid request.");
}
if (file_exists(dirname(__FILE__)."/config.php"))
{
require_once dirname(__FILE__).'/config.php';
}
if (!$servers)
{
die("No servers in config found.");
}
// Default settings
if (!defined('DEBUG'))
{
define("DEBUG", false);
}
if (!defined('USE_MODULE_IF_LOADED'))
{
define("USE_MODULE_IF_LOADED", true);
}
// Process GET request
$server = 0;
if (isset($_GET['s']) && intval($_GET['s']) < count($servers) && intval($_GET['s']) >= -1)
{
$server = intval($_GET['s']);
}
$serverName = $servers[$server][0];
if (isset($_GET['db']))
{
$db = intval($_GET['db']);
}
if (isset($_GET['async']))
{
$async = intval($_GET['async']);
}
// Functions
function debug($var, $pre = true)
{
if (DEBUG)
{
if ($pre) echo "<pre>".PHP_EOL;
var_dump($var);
if ($pre) echo "</pre>".PHP_EOL;
}
}
// Talk to server
$error = null;
// Talk to server
if (extension_loaded("redis") && USE_MODULE_IF_LOADED)
{ // Use PHP extension
$valkey = new Redis();
$context = [];
// build tls context here
if (str_starts_with($servers[$server][1], 'tls://'))
{
$tls_stream = $tls['default-options'];
if (array_key_exists($servers[$server][0], $tls))
{
$tls_stream = array_replace($tls['default-options'], $tls[$servers[$server][0]]);
}
$context = [
'stream' => $tls_stream
];
}
try {
$rc = $valkey->connect($servers[$server][1], $servers[$server][2], 0, null, 0, 0, $context);
} catch (Exception $e) {
$error = $e->getMessage();
goto extensionEnd;
}
if ($rc === false)
{
$error = "Cannot connect to database.";
goto extensionEnd;
}
isset($servers[$server][3]) ? $pwdEntry = $servers[$server][3] : $pwdEntry = null;
if (!is_null($pwdEntry) && !empty($pwdEntry))
{
try {
$valkey->auth($pwdEntry);
} catch (Exception $e) {
$error = $e->getMessage();
goto extensionEnd;
}
}
// flush stuff here
$SYNC = null;
if (!$async) // sync flush requires the parameter to be bool(true)
{
$SYNC = true;
}
if ($db != -1) // one specific database
{
$select_rc = $valkey->select($db);
if ($select_rc === true)
{
$flush_rc = $valkey->flushDB($SYNC);
}
if ($select_rc === false || $flush_rc === false)
{
$error = "Cannot flush db$db.";
}
}
else // entire instance
{
$flushall_rc = $valkey->flushAll($SYNC);
if ($flushall_rc === false)
{
$error = "Cannot flush instance.";
}
}
extensionEnd:
if (!empty($error))
{
die($error);
}
} // End Use PHP extension
else
{ // Use Raw Socket
// Command mapping
$FLUSHDB = 'FLUSHDB';
if (isset($command[$serverName]['FLUSHDB']) && !is_null($command[$serverName]['FLUSHDB']) && !empty($command[$serverName]['FLUSHDB']))
{
$FLUSHDB = $command[$serverName]['FLUSHDB'];
}
$FLUSHALL = 'FLUSHALL';
if (isset($command[$serverName]['FLUSHALL']) && !is_null($command[$serverName]['FLUSHALL']) && !empty($command[$serverName]['FLUSHALL']))
{
$FLUSHALL = $command[$serverName]['FLUSHALL'];
}
$AUTH = 'AUTH';
if (isset($command[$serverName]['AUTH']) && !is_null($command[$serverName]['AUTH']) && !empty($command[$serverName]['AUTH']))
{
$AUTH = $command[$serverName]['AUTH'];
}
$fp = @fsockopen($servers[$server][1], $servers[$server][2], $errno, $errstr, 30);
$info = array();
if (!$fp)
{
die($errstr);
}
else
{
$vkCommand = '';
$ASYNC = '';
$numCmds = 0;
isset($servers[$server][3]) ? $pwdEntry = $servers[$server][3] : $pwdEntry = null;
if (!is_null($pwdEntry) && !empty($pwdEntry))
{
if (is_array($pwdEntry))
{
if (!isset($pwdEntry[1]) || is_null($pwdEntry[1]) || empty($pwdEntry[1]))
{
$pwdEntry[1] = '0';
}
$credentials = "$pwdEntry[0] $pwdEntry[1]";
}
else
{
$credentials = $pwdEntry;
}
$vkCommand = "$AUTH $credentials\r\n";
$numCmds++;
}
if ($async) // we want async flush
{
$ASYNC = ' ASYNC';
}
if ($db != -1) // one specific database
{
$vkCommand .= "SELECT $db\r\n$FLUSHDB$ASYNC\r\nQUIT\r\n";
$numCmds += 3;
}
else // entire instance
{
$vkCommand .= "$FLUSHALL$ASYNC\r\nQUIT\r\n";
$numCmds += 2;
}
fwrite($fp, $vkCommand);
while (!feof($fp))
{
$info[] = trim(fgets($fp));
}
fclose($fp);
}
for ($i = 0; $i < $numCmds; $i++)
{
if ($info[$i] != '+OK')
{
debug($vkCommand);
debug($info);
die($info[$i]);
}
}
} // End Use Raw Socket
$_SESSION['id'] = '';
echo "Success";