Project

General

Profile

Download (9.14 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * diag_confbak.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2019 Rubicon Communications, LLC (Netgate)
7
 * Copyright (c) 2005 Colin Smith
8
 * All rights reserved.
9
 *
10
 * Licensed under the Apache License, Version 2.0 (the "License");
11
 * you may not use this file except in compliance with the License.
12
 * You may obtain a copy of the License at
13
 *
14
 * http://www.apache.org/licenses/LICENSE-2.0
15
 *
16
 * Unless required by applicable law or agreed to in writing, software
17
 * distributed under the License is distributed on an "AS IS" BASIS,
18
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
 * See the License for the specific language governing permissions and
20
 * limitations under the License.
21
 */
22

    
23
##|+PRIV
24
##|*IDENT=page-diagnostics-configurationhistory
25
##|*NAME=Diagnostics: Configuration History
26
##|*DESCR=Allow access to the 'Diagnostics: Configuration History' page.
27
##|*WARN=standard-warning-root
28
##|*MATCH=diag_confbak.php*
29
##|-PRIV
30

    
31
require_once("guiconfig.inc");
32

    
33
if (isset($_POST['backupcount'])) {
34
	if (!empty($_POST['backupcount']) && (!is_numericint($_POST['backupcount']) || ($_POST['backupcount'] < 0))) {
35
		$input_errors[] = gettext("Invalid Backup Count specified");
36
	}
37

    
38
	if (!$input_errors) {
39
		if (is_numericint($_POST['backupcount'])) {
40
			$config['system']['backupcount'] = $_POST['backupcount'];
41
			$changedescr = $config['system']['backupcount'];
42
		} elseif (empty($_POST['backupcount'])) {
43
			unset($config['system']['backupcount']);
44
			$changedescr = gettext("(platform default)");
45
		}
46
		write_config(sprintf(gettext("Changed backup revision count to %s"), $changedescr));
47
	}
48
}
49

    
50
$confvers = unserialize(file_get_contents($g['cf_conf_path'] . '/backup/backup.cache'));
51

    
52
if ($_POST['newver'] != "") {
53
	if (config_restore($g['conf_path'] . '/backup/config-' . $_POST['newver'] . '.xml') == 0) {
54
		$savemsg = sprintf(gettext('Successfully reverted to timestamp %1$s with description "%2$s".'), date(gettext("n/j/y H:i:s"), $_POST['newver']), htmlspecialchars($confvers[$_POST['newver']]['description']));
55
	} else {
56
		$savemsg = gettext("Unable to revert to the selected configuration.");
57
	}
58
}
59

    
60
if ($_POST['rmver'] != "") {
61
	unlink_if_exists($g['conf_path'] . '/backup/config-' . $_POST['rmver'] . '.xml');
62
	$savemsg = sprintf(gettext('Deleted backup with timestamp %1$s and description "%2$s".'), date(gettext("n/j/y H:i:s"), $_POST['rmver']), htmlspecialchars($confvers[$_POST['rmver']]['description']));
63
}
64

    
65
if ($_REQUEST['getcfg'] != "") {
66
	$_REQUEST['getcfg'] = basename($_REQUEST['getcfg']);
67
	$file = $g['conf_path'] . '/backup/config-' . $_REQUEST['getcfg'] . '.xml';
68

    
69
	$exp_name = urlencode("config-{$config['system']['hostname']}.{$config['system']['domain']}-{$_REQUEST['getcfg']}.xml");
70
	$exp_data = file_get_contents($file);
71
	$exp_size = strlen($exp_data);
72

    
73
	header("Content-Type: application/octet-stream");
74
	header("Content-Disposition: attachment; filename={$exp_name}");
75
	header("Content-Length: $exp_size");
76
	echo $exp_data;
77
	exit;
78
}
79

    
80
if (($_REQUEST['diff'] == 'Diff') && isset($_REQUEST['oldtime']) && isset($_REQUEST['newtime']) &&
81
    (is_numeric($_REQUEST['oldtime'])) &&
82
    (is_numeric($_REQUEST['newtime']) || ($_REQUEST['newtime'] == 'current'))) {
83
	$diff = "";
84
	$oldfile = $g['conf_path'] . '/backup/config-' . $_REQUEST['oldtime'] . '.xml';
85
	$oldtime = $_REQUEST['oldtime'];
86
	if ($_REQUEST['newtime'] == 'current') {
87
		$newfile = $g['conf_path'] . '/config.xml';
88
		$newtime = $config['revision']['time'];
89
	} else {
90
		$newfile = $g['conf_path'] . '/backup/config-' . $_REQUEST['newtime'] . '.xml';
91
		$newtime = $_REQUEST['newtime'];
92
	}
93
	if (file_exists($oldfile) && file_exists($newfile)) {
94
		exec("/usr/bin/diff -u " . escapeshellarg($oldfile) . " " . escapeshellarg($newfile), $diff);
95
	}
96
}
97

    
98
cleanup_backupcache(false);
99
$confvers = get_backups();
100
unset($confvers['versions']);
101

    
102
$pgtitle = array(gettext("Diagnostics"), htmlspecialchars(gettext("Backup & Restore")), gettext("Config History"));
103
$pglinks = array("", "diag_backup.php", "@self");
104
include("head.inc");
105

    
106
if ($input_errors) {
107
	print_input_errors($input_errors);
108
}
109

    
110
if ($savemsg) {
111
	print_info_box($savemsg, 'success');
112
}
113

    
114
$tab_array = array();
115
$tab_array[] = array(htmlspecialchars(gettext("Backup & Restore")), false, "diag_backup.php");
116
$tab_array[] = array(gettext("Config History"), true, "diag_confbak.php");
117
display_top_tabs($tab_array);
118

    
119
if ($diff) {
120
?>
121
<div class="panel panel-default">
122
	<div class="panel-heading">
123
		<h2 class="panel-title">
124
			<?=sprintf(gettext('Configuration Diff from %1$s to %2$s'), date(gettext("n/j/y H:i:s"), $oldtime), date(gettext("n/j/y H:i:s"), $newtime))?>
125
		</h2>
126
	</div>
127
	<div class="panel-body table-responsive">
128
	<!-- This table is left un-bootstrapped to maintain the original diff format output -->
129
		<table style="padding-top: 4px; padding-bottom: 4px; vertical-align:middle;">
130

    
131
<?php
132
	foreach ($diff as $line) {
133
		switch (substr($line, 0, 1)) {
134
			case "+":
135
				$color = "#caffd3";
136
				break;
137
			case "-":
138
				$color = "#ffe8e8";
139
				break;
140
			case "@":
141
				$color = "#a0a0a0";
142
				break;
143
			default:
144
				$color = "#ffffff";
145
		}
146
?>
147
			<tr>
148
				<td class="diff-text" style="vertical-align:middle; background-color:<?=$color;?>; white-space:pre-wrap;"><?=htmlentities($line)?></td>
149
			</tr>
150
<?php
151
	}
152
?>
153
		</table>
154
	</div>
155
</div>
156
<?php
157
}
158

    
159
$form = new Form(false);
160

    
161
$section = new Form_Section('Configuration Backup Cache Settings', 'configsettings', COLLAPSIBLE|SEC_CLOSED);
162

    
163
$section->addInput(new Form_Input(
164
	'backupcount',
165
	'Backup Count',
166
	'number',
167
	$config['system']['backupcount'],
168
	['min' => '0']
169
))->setHelp('Maximum number of old configurations to keep in the cache, 0 for no backups, or leave blank for the default value (%s for the current platform).', $g['default_config_backup_count']);
170

    
171
$space = exec("/usr/bin/du -sh /conf/backup | /usr/bin/awk '{print $1;}'");
172

    
173
$section->addInput(new Form_StaticText(
174
	'Current space used by backups',
175
	$space
176
));
177

    
178
$section->addInput(new Form_Button(
179
	'Submit',
180
	gettext("Save"),
181
	null,
182
	'fa-save'
183
))->addClass('btn-primary');
184

    
185
$form->add($section);
186

    
187
print($form);
188

    
189
if (is_array($confvers)) {
190
?>
191
<div>
192
	<div class="infoblock blockopen">
193
		<?php print_info_box(
194
			gettext(
195
				'To view the differences between an older configuration and a newer configuration, ' .
196
				'select the older configuration using the left column of radio options and select the newer configuration in the right column, ' .
197
				'then press the "Diff" button.'),
198
			'info', false); ?>
199
	</div>
200
</div>
201
<?php
202
}
203
?>
204

    
205
<form action="diag_confbak.php" method="get">
206
	<div class="table-responsive">
207
		<table class="table table-striped table-hover table-condensed">
208
<?php
209
if (is_array($confvers)):
210
?>
211
			<thead>
212
				<tr>
213
					<th colspan="2">
214
						<button type="submit" name="diff" class="btn btn-info btn-xs" value="Diff">
215
							<i class="fa fa-exchange icon-embed-btn"></i>
216
							<?=gettext("Diff"); ?>
217
						</button>
218
					</th>
219
					<th><?=gettext("Date")?></th>
220
					<th><?=gettext("Version")?></th>
221
					<th><?=gettext("Size")?></th>
222
					<th><?=gettext("Configuration Change")?></th>
223
					<th><?=gettext("Actions")?></th>
224
				</tr>
225
			</thead>
226
			<tbody>
227
				<!-- First row is the current configuration -->
228
				<tr style="vertical-align:top;">
229
					<td></td>
230
					<td>
231
						<input type="radio" name="newtime" value="current" />
232
					</td>
233
					<td><?= date(gettext("n/j/y H:i:s"), $config['revision']['time']) ?></td>
234
					<td><?= $config['version'] ?></td>
235
					<td><?= format_bytes(filesize("/conf/config.xml")) ?></td>
236
					<td><?= htmlspecialchars($config['revision']['description']) ?></td>
237
					<td><?=gettext("Current configuration")?></td>
238
				</tr>
239
<?php
240
	// And now for the table of prior backups
241
	$c = 0;
242
	foreach ($confvers as $version):
243
		if ($version['time'] != 0) {
244
			$date = date(gettext("n/j/y H:i:s"), $version['time']);
245
		} else {
246
			$date = gettext("Unknown");
247
		}
248
?>
249
				<tr>
250
					<td>
251
						<input type="radio" name="oldtime" value="<?=$version['time']?>" />
252
					</td>
253
					<td>
254
<?php
255
		if ($c < (count($confvers) - 1)) {
256
?>
257
								<input type="radio" name="newtime" value="<?=$version['time']?>" />
258
<?php
259
		}
260
		$c++;
261
?>
262
					</td>
263
					<td><?= $date ?></td>
264
					<td><?= $version['version'] ?></td>
265
					<td><?= format_bytes($version['filesize']) ?></td>
266
					<td><?= htmlspecialchars($version['description']) ?></td>
267
					<td>
268
						<a class="fa fa-undo"		title="<?=gettext('Revert config')?>"	href="diag_confbak.php?newver=<?=$version['time']?>" onclick="return confirm('<?=gettext("Confirmation Required to replace the current configuration with this backup.")?>')" usepost></a>
269
						<a class="fa fa-download"	title="<?=gettext('Download config')?>"	href="diag_confbak.php?getcfg=<?=$version['time']?>"></a>
270
						<a class="fa fa-trash"		title="<?=gettext('Delete config')?>"	href="diag_confbak.php?rmver=<?=$version['time']?>" usepost></a>
271
					</td>
272
				</tr>
273
<?php
274
	endforeach;
275
?>
276
				<tr>
277
					<td colspan="2">
278
						<button type="submit" name="diff" class="btn btn-info btn-xs" value="Diff">
279
							<i class="fa fa-exchange icon-embed-btn"></i>
280
							<?=gettext("Diff"); ?>
281
						</button>
282
					</td>
283
					<td colspan="5"></td>
284
				</tr>
285
<?php
286
else:
287
	print_info_box(gettext("No backups found."), 'danger');
288
endif;
289
?>
290
			</tbody>
291
		</table>
292
	</div>
293
</form>
294

    
295
<?php include("foot.inc");
(12-12/235)