Project

General

Profile

Download (9.2 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-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2019 Rubicon Communications, LLC (Netgate)
9
 * Copyright (c) 2005 Colin Smith
10
 * All rights reserved.
11
 *
12
 * Licensed under the Apache License, Version 2.0 (the "License");
13
 * you may not use this file except in compliance with the License.
14
 * You may obtain a copy of the License at
15
 *
16
 * http://www.apache.org/licenses/LICENSE-2.0
17
 *
18
 * Unless required by applicable law or agreed to in writing, software
19
 * distributed under the License is distributed on an "AS IS" BASIS,
20
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
 * See the License for the specific language governing permissions and
22
 * limitations under the License.
23
 */
24

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

    
32
require_once("guiconfig.inc");
33

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

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

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

    
53
if ($_POST['newver'] != "") {
54
	if (config_restore($g['conf_path'] . '/backup/config-' . $_POST['newver'] . '.xml') == 0) {
55
		$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']));
56
	} else {
57
		$savemsg = gettext("Unable to revert to the selected configuration.");
58
	}
59
}
60

    
61
if ($_POST['rmver'] != "") {
62
	unlink_if_exists($g['conf_path'] . '/backup/config-' . $_POST['rmver'] . '.xml');
63
	$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']));
64
}
65

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

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

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

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

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

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

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

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

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

    
120
if ($diff) {
121
?>
122
<div class="panel panel-default">
123
	<div class="panel-heading">
124
		<h2 class="panel-title">
125
			<?=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))?>
126
		</h2>
127
	</div>
128
	<div class="panel-body table-responsive">
129
	<!-- This table is left un-bootstrapped to maintain the original diff format output -->
130
		<table style="padding-top: 4px; padding-bottom: 4px; vertical-align:middle;">
131

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

    
160
$form = new Form(false);
161

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

    
164
$section->addInput(new Form_Input(
165
	'backupcount',
166
	'Backup Count',
167
	'number',
168
	$config['system']['backupcount'],
169
	['min' => '0']
170
))->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']);
171

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

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

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

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

    
188
print($form);
189

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

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

    
296
<?php include("foot.inc");
(11-11/225)