forked from FreePBX/backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.backup_restore.php
222 lines (201 loc) · 6.58 KB
/
page.backup_restore.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
<?php
if (!defined('FREEPBX_IS_AUTH')) { die('No direct script access allowed'); }
$get_vars = array(
'action' => '',
'display' => '',
'id' => '',
'path' => '',
'restore_path' => '',
'restore_source' => '',
'restore' => '',
'submit' => '',
'upload' => ''
);
foreach ($get_vars as $k => $v) {
$var[$k] = isset($_REQUEST[$k]) ? $_REQUEST[$k] : $v;
}
$message = "";
//set action to delete if delete was pressed instead of submit
if ($var['submit'] == _('Download') && $var['action'] == 'backup_list') {
$var['action'] = 'download';
}
//set action to view if only id is set
if ($var['action'] == '' && $var['id']) {
$var['action'] = 'browseserver';
}
//action actions
switch ($var['action']) {
case 'download':
$var['restore_path'] = backup_restore_locate_file($var['id'], $var['restore_path']);
$_SESSION['backup_restore_path'] = $var['restore_path'];
download_file($var['restore_path']);
break;
case 'upload':
//make sure our file was uploaded
if (!is_uploaded_file($_FILES['upload']['tmp_name'])) {
$message = _('Error uploading file!');
$var['action'] = '';
break;
}
//ensure uploaded file is a valid tar file
exec(fpbx_which('tar') . ' -tf ' . $_FILES['upload']['tmp_name'], $array, $ret_code);
if ($ret_code !== 0) {
$message = _('Error verifying uploaded file!');
$var['action'] = '';
break;
}
$dest = $amp_conf['ASTSPOOLDIR']
. '/tmp/'
. 'backuptmp-suser-'
. time() . '-'
. basename($_FILES['upload']['name']);
move_uploaded_file($_FILES['upload']['tmp_name'], $dest);
//$var['restore_path'] = $dest;
$_SESSION['backup_restore_path'] = $dest;
break;
case 'list_dir':
echo json_encode(backup_jstree_list_dir($var['id'], $var['path']));
exit;
break;
case 'backup_list':
//prepare file + ensure that its local
if(!isset($_SESSION['backup_restore_path'])) {
$var['restore_path'] = backup_restore_locate_file($var['id'], $var['restore_path']);
/*
* being that this is an absolute file path
* and being that we arent going to be sanitizing/sanity checking this path anymore
* store it in the session so that the user cant manipulate it
*/
$_SESSION['backup_restore_path'] = $var['restore_path'];
}
break;
case 'restore':
case 'restore_post':
case 'restore_get':
//dont clear $_SESSION['backup_restore_path'] which happnes every action
//that doesnt have a case here
break;
default:
//if backup_restore_path is already set, we probobly dont want that any more. delete it
if (isset($_SESSION['backup_restore_path'])) {
unset($_SESSION['backup_restore_path']);
}
break;
}
//view actions
switch ($var['action']) {
case 'browseserver':
$content = load_view(dirname(__FILE__) . '/views/restore/browseserver.php', $var);
break;
case 'upload':
case 'backup_list':
$var['servers'] = backup_get_server('all');
$var['templates'] = backup_get_template('all_detailed');
//transalate variables
//TODO: make this anonymous once we require php 5.3
function callback(&$var) {
$var = backup__($var);
}
array_walk_recursive($var['servers'], 'callback');
array_walk_recursive($var['templates'], 'callback');
if (is_array($_SESSION['backup_restore_path'])) {
//TODO: if $var['restore_path'] is an array, that means it contains an error + error
// message. Do something with the error meesage
echo _('Invalid backup for or undefined error');
break;
}
//try to get a manifest, and continue if we did
if ($var['manifest'] = backup_get_manifest_tarball($_SESSION['backup_restore_path'])) {
$content = load_view(dirname(__FILE__) . '/views/restore/backup_list.php', $var);
break;
}
//we didnt get a manifest. Maybe this is a legacy backup?
$var['restore_path'] = backup_migrate_legacy($_SESSION['backup_restore_path']);
$var['manifest'] = backup_get_manifest_tarball($var['restore_path']);
if ($var['restore_path'] && $var['manifest']) {
$_SESSION['backup_restore_path'] = $var['restore_path'];
echo load_view(dirname(__FILE__) . '/views/restore/backup_list.php', $var);
break;
}
//still here? oops, something is really broken
$message = _('Invalid backup for or undefined error');
dbug($_SESSION['backup_restore_path'], $var);
break;
case 'restore_post':
while (ob_get_level()) {
ob_end_clean();
}
$_SESSION['backup_restore_data'] = $var['restore'];
exit();
break;
case 'restore':
case 'restore_get':
$headers = getallheaders();
$reconnect = (isset($headers['Last-Event-ID']) && ($headers['Last-Event-ID'] === 'bb2ac0b8da1f64a3498af147ba43fc10'));
//if action is restore_get, get restore data from session
$restore = $var['action'] == 'restore_get'
? $_SESSION['backup_restore_data']
: $var['restore'];
//dont stop until were all done
//restore will compelte EVEN IS USER NAVIGATES AWAY FROM PAGE!!
ignore_user_abort(true);
//clear all buffers, those will interfere with the stream
while (ob_get_level()) {
ob_end_clean();
}
ob_start();
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
$cmd = $amp_conf['AMPBIN'] . '/restore.php --restore='
. escapeshellarg($_SESSION['backup_restore_path'])
. ' --items='
. base64_encode(json_encode($restore))
. ' 2>&1';
if(!$reconnect){
//start running backup
$run = popen($cmd, 'r');
while (($msg = fgets($run)) !== false) {
//dbug('backup', $msg);
//send results back to the user
backup_log($msg);
}
pclose($run);
}else{
backup_log(_("Browser reconnected realtime updates are unavailable."));
$ret = 0;
while($ret == 0){
exec("pgrep restore.php", $output, $ret);
backup_log(_("The Restore script is still running"));
sleep(5);
}
backup_log(_("The Restore script Finished You can close this window"));
}
//send messgae to browser that were done
backup_log('END');
exit();
break;
default:
$content = load_view(dirname(__FILE__) . '/views/restore/restore.php', $var);
break;
}
$heading = _("Restore");
?>
<div class="container-fluid">
<h1><?php echo $heading ?></h1>
<div class = "display full-border">
<div class="row">
<div class="col-sm-12">
<div class="fpbx-container">
<div class="display no-border">
<?php if(!empty($message)) { ?>
<div class="alert alert-danger" role="alert"><?php echo $message?></div>
<?php } ?>
<?php echo $content ?>
</div>
</div>
</div>
</div>
</div>
</div>
</br>
</br>