-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflushdb.php
138 lines (124 loc) · 2.61 KB
/
flushdb.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
<?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 debug setting
if (!defined('DEBUG'))
{
define("DEBUG", false);
}
// Process GET request
$server = 0;
if (isset($_GET['s']) && intval($_GET['s']) < count($servers))
{
$server = intval($_GET['s']);
}
$serverName = $servers[$server][0];
if (isset($_GET['db']))
{
$db = intval($_GET['db']);
}
if (isset($_GET['async']))
{
$async = intval($_GET['async']);
}
// 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'];
}
// 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;
$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]);
}
}
$_SESSION['id'] = '';
echo "Success";