This repository was archived by the owner on Nov 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.php
227 lines (180 loc) · 5.97 KB
/
update.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
221
222
223
224
225
226
227
<?php
require_once("includes/settings.php");
require_once("includes/classes.php");
require_once("includes/logic.php");
// Prepare ajax result
function make_result($success, $text, $debug=null)
{
$result = array();
$result['success'] = $success;
$result['text'] = $text;
$result['debug'] = '';
if (DEBUG && $debug != null) {
$result['debug'] = $debug;
$fh = fopen("debug.log", 'a');
if ( $fh ) {
fwrite($fh, "\n\n" . date('r') . ":\n" . $debug);
fclose($fh);
}
}
return $result;
}
// Return ajax result
function send_result(&$result)
{
echo json_encode($result);
}
if (DEBUG) {
$fh = fopen("debug.log", 'a');
if ( $fh ) {
fwrite($fh, "\n\n" . date('r') . ": update.php called.\n");
fclose($fh);
}
}
$result = array();
$type = $_POST['type'];
unset($_POST['type']);
// Validate post information
if (!$_SERVER['REQUEST_METHOD'] || $_SERVER['REQUEST_METHOD'] != 'POST' || !isset($type))
{
/* Check referrer, if from index.php then throw a 404 */
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
exit;
}
$databases = array();
$backups = array();
$configurations = array();
$dbh = null;
if ($type != "settings") {
connect_to_database($dbh);
if (!$dbh)
{
$result = make_result(false, "Unable to connect to database.", mysql_error($dbh));
send_result($result);
return;
}
}
$debug = " ";
$result = null;
switch ($type)
{
case PREFIX_CONFIG_MODIFY:
$obj = new Configuration($_POST);
if (save_configuration($dbh, $obj, false, $debug))
$result = make_result(true, "Configuraton saved.", $debug);
else
$result = make_result(false, "Failed to save configuration.", $debug);
break;
case PREFIX_CONFIG_NEW:
$configuratons = array();
initialize_configurations($dbh, $configurations);
foreach ($configurations as $c)
{
if ($c->name == $_POST['name'])
{
$result = make_result(false, "A configuration with that name already exists.");
send_result($result);
exit;
}
}
unset($_POST['id']);
$obj = new Configuration($_POST);
if (save_configuration($dbh, $obj, true, $debug))
$result = make_result(true, "Configuration saved.", $debug);
else
$result = make_result(false, "Failed to save configuration.", $debug);
break;
case PREFIX_CONFIG_DELETE:
if (!$_POST['configurations'])
$result = make_result(false, "No configurations were selected.");
else
{
if (delete_configurations($dbh, $_POST['configurations'], $debug))
$result = make_result(true, "The selected configurations were deleted.", $debug);
else
$result = make_result(false, "Could not delete configurations.", $debug);
}
break;
case PREFIX_BACKUP_FROM_CONF:
case PREFIX_BACKUP_ONETIME:
$configuration = null;
if ($type == "backup")
{
$configuratons = array();
initialize_configurations($dbh, $configurations);
$configuration = $configurations[$_POST['id']];
} else {
$configuration = new Configuration($_POST);
}
if (!$configuration)
$result = make_result(false, "Failed to load configuration for backup.");
else
{
if (create_backup($dbh, $configuration, $output, $debug))
{
$result = make_result(true, "Backup created successfully.", $debug);
}
else
{
$lines = explode("\n", $output);
$line = end($lines);
$result = make_result(false, "Backup failed:<br />\n$line", $debug);
}
}
break;
case PREFIX_EXISTING_BACKUP:
$list = array();
foreach($_POST as $config) {
foreach($config as $backup) {
if ($backup != 'all')
$list[] = $backup;
}
}
if (!$list)
make_result(true, "No backups were selected.");
else
{
$configurations = array();
initialize_configurations($dbh, $configurations);
$backups = array();
initialize_backups($dbh, $backups, $configurations);
$debug = implode(", ", $list) . "\n";
if (delete_backups($dbh, "backups", $list, $backups, $debug))
$result = make_result(true, "Successfully deleted backups.", $debug);
else
$result = make_result(false, "An error occurred while deleting backups.", $debug);
}
break;
case "settings":
$error_text = "";
if (!write_config_file($_POST, $error_text))
{
if (!$error_text)
$result = make_result(false, "Internal error: Failed to write config file.");
else
$result = make_result(false, $error_text);
} else
$result = make_result(true, "Updated configuration.");
break;
case "s3":
$id = $_POST['id'];
$configurations = array();
initialize_configurations($dbh, $configurations);
$backups = array();
initialize_backups($dbh, $backups, $configurations);
$backup = $backups[$id];
$configuration = $backup->configuration;
$error_text = "";
if (upload_to_s3($dbh, $configuration, $backup, $error_text, $debug))
{
$result = make_result(true, "{$backup->filename} was uploaded to S3.");
} else {
$result = make_result(false, $error_text);
}
break;
default:
$result = make_result(true, "Not yet implemented :(", $type);
break;
}
send_result($result);
?>