Project

General

Profile

Download (8.91 KB) Statistics
| Branch: | Tag: | Revision:
1 1d2d6b3c Colin Smith
<?php
2
/*
3 6173d1f5 Colin Fleming
 * diag_confbak.php
4 c5d81585 Renato Botelho
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2016 Electric Sheep Fencing, LLC
7
 * Copyright (c) 2005 Colin Smith
8
 * All rights reserved.
9
 *
10 b12ea3fb Renato Botelho
 * 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 c5d81585 Renato Botelho
 *
14 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
15 c5d81585 Renato Botelho
 *
16 b12ea3fb Renato Botelho
 * 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 7dc1383a Stephen Beaver
 */
22 1d2d6b3c Colin Smith
23 6b07c15a Matthew Grooms
##|+PRIV
24
##|*IDENT=page-diagnostics-configurationhistory
25 5230f468 jim-p
##|*NAME=Diagnostics: Configuration History
26 6b07c15a Matthew Grooms
##|*DESCR=Allow access to the 'Diagnostics: Configuration History' page.
27
##|*MATCH=diag_confbak.php*
28
##|-PRIV
29
30 c81ef6e2 Phil Davis
require_once("guiconfig.inc");
31 2f8d0729 Bill Marquette
32 e1ebe9e2 jim-p
if (isset($_POST['backupcount'])) {
33 f208e969 stilez
	if (is_numericint($_POST['backupcount'])) {
34 e1ebe9e2 jim-p
		$config['system']['backupcount'] = $_POST['backupcount'];
35
		$changedescr = $config['system']['backupcount'];
36
	} else {
37
		unset($config['system']['backupcount']);
38 ff30e319 bruno
		$changedescr = gettext("(platform default)");
39 e1ebe9e2 jim-p
	}
40 ff30e319 bruno
	write_config(sprintf(gettext("Changed backup revision count to %s"), $changedescr));
41 dbeffa2f Stephen Beaver
} elseif ($_GET) {
42
	if (!isset($_GET['newver']) && !isset($_GET['rmver']) && !isset($_GET['getcfg']) && !isset($_GET['diff'])) {
43 b6513591 jim-p
		header("Location: diag_confbak.php");
44
		return;
45
	}
46 2f8d0729 Bill Marquette
47 91bfbd00 Scott Ullrich
	conf_mount_rw();
48 1d478d96 Colin Smith
	$confvers = unserialize(file_get_contents($g['cf_conf_path'] . '/backup/backup.cache'));
49 0da0d43e Phil Davis
50 dbeffa2f Stephen Beaver
	if ($_GET['newver'] != "") {
51
		if (config_restore($g['conf_path'] . '/backup/config-' . $_GET['newver'] . '.xml') == 0) {
52
			$savemsg = sprintf(gettext('Successfully reverted to timestamp %1$s with description "%2$s".'), date(gettext("n/j/y H:i:s"), $_GET['newver']), htmlspecialchars($confvers[$_GET['newver']]['description']));
53 5f601060 Phil Davis
		} else {
54 b6513591 jim-p
			$savemsg = gettext("Unable to revert to the selected configuration.");
55 5f601060 Phil Davis
		}
56 b6513591 jim-p
	}
57 dbeffa2f Stephen Beaver
	if ($_GET['rmver'] != "") {
58
		unlink_if_exists($g['conf_path'] . '/backup/config-' . $_GET['rmver'] . '.xml');
59
		$savemsg = sprintf(gettext('Deleted backup with timestamp %1$s and description "%2$s".'), date(gettext("n/j/y H:i:s"), $_GET['rmver']), htmlspecialchars($confvers[$_GET['rmver']]['description']));
60 b6513591 jim-p
	}
61 91bfbd00 Scott Ullrich
	conf_mount_ro();
62 2f8d0729 Bill Marquette
}
63
64 5f601060 Phil Davis
if ($_GET['getcfg'] != "") {
65 3b635066 jim-p
	$_GET['getcfg'] = basename($_GET['getcfg']);
66 9f9b88e2 jim-p
	$file = $g['conf_path'] . '/backup/config-' . $_GET['getcfg'] . '.xml';
67
68
	$exp_name = urlencode("config-{$config['system']['hostname']}.{$config['system']['domain']}-{$_GET['getcfg']}.xml");
69
	$exp_data = file_get_contents($file);
70
	$exp_size = strlen($exp_data);
71
72
	header("Content-Type: application/octet-stream");
73
	header("Content-Disposition: attachment; filename={$exp_name}");
74
	header("Content-Length: $exp_size");
75
	echo $exp_data;
76
	exit;
77
}
78
79 5f601060 Phil Davis
if (($_GET['diff'] == 'Diff') && isset($_GET['oldtime']) && isset($_GET['newtime']) &&
80
    (is_numeric($_GET['oldtime'])) &&
81 288a2a0f Phil Davis
    (is_numeric($_GET['newtime']) || ($_GET['newtime'] == 'current'))) {
82 957e2f1f jim-p
	$diff = "";
83
	$oldfile = $g['conf_path'] . '/backup/config-' . $_GET['oldtime'] . '.xml';
84
	$oldtime = $_GET['oldtime'];
85
	if ($_GET['newtime'] == 'current') {
86
		$newfile = $g['conf_path'] . '/config.xml';
87
		$newtime = $config['revision']['time'];
88
	} else {
89
		$newfile = $g['conf_path'] . '/backup/config-' . $_GET['newtime'] . '.xml';
90
		$newtime = $_GET['newtime'];
91
	}
92
	if (file_exists($oldfile) && file_exists($newfile)) {
93
		exec("/usr/bin/diff -u " . escapeshellarg($oldfile) . " " . escapeshellarg($newfile), $diff);
94
	}
95
}
96
97 e1ebe9e2 jim-p
cleanup_backupcache(false);
98 359893b0 Colin Smith
$confvers = get_backups();
99
unset($confvers['versions']);
100 1d2d6b3c Colin Smith
101 0f298138 k-paulius
$pgtitle = array(gettext("Diagnostics"), htmlspecialchars(gettext("Backup & Restore")), gettext("Config History"));
102 b63695db Scott Ullrich
include("head.inc");
103
104 dbeffa2f Stephen Beaver
if ($savemsg) {
105
	print_info_box($savemsg, 'success');
106
}
107
108 1d191646 Phil Davis
$tab_array = array();
109 0f298138 k-paulius
$tab_array[] = array(htmlspecialchars(gettext("Backup & Restore")), false, "diag_backup.php");
110 2b38c46b k-paulius
$tab_array[] = array(gettext("Config History"), true, "diag_confbak.php");
111 1d191646 Phil Davis
display_top_tabs($tab_array);
112
113 dbeffa2f Stephen Beaver
if ($diff) {
114 f79f9497 Sjon Hortensius
?>
115 dbeffa2f Stephen Beaver
<div class="panel panel-default">
116 95fa5cce Phil Davis
	<div class="panel-heading">
117
		<h2 class="panel-title">
118 3d7a8696 k-paulius
			<?=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))?>
119 95fa5cce Phil Davis
		</h2>
120
	</div>
121 dbeffa2f Stephen Beaver
	<div class="panel-body table-responsive">
122
	<!-- This table is left un-bootstrapped to maintain the original diff format output -->
123
		<table style="padding-top: 4px; padding-bottom: 4px; vertical-align:middle;">
124 f79f9497 Sjon Hortensius
125
<?php
126 dbeffa2f Stephen Beaver
	foreach ($diff as $line) {
127
		switch (substr($line, 0, 1)) {
128
			case "+":
129
				$color = "#caffd3";
130
				break;
131
			case "-":
132
				$color = "#ffe8e8";
133
				break;
134
			case "@":
135
				$color = "#a0a0a0";
136
				break;
137
			default:
138
				$color = "#ffffff";
139
		}
140
?>
141
			<tr>
142 91419f70 NewEraCracker
				<td class="diff-text" style="vertical-align:middle; background-color:<?=$color;?>; white-space:pre-wrap;"><?=htmlentities($line)?></td>
143 dbeffa2f Stephen Beaver
			</tr>
144 0da0d43e Phil Davis
<?php
145 dbeffa2f Stephen Beaver
	}
146 f79f9497 Sjon Hortensius
?>
147 dbeffa2f Stephen Beaver
		</table>
148
	</div>
149
</div>
150 0da0d43e Phil Davis
<?php
151 dbeffa2f Stephen Beaver
}
152 f79f9497 Sjon Hortensius
153 89333d37 jim-p
$form = new Form(false);
154 b6513591 jim-p
155 ca55edc3 stilez
$section = new Form_Section('Configuration Backup Cache Settings', 'configsettings', COLLAPSIBLE|SEC_CLOSED);
156 b6513591 jim-p
157 dbeffa2f Stephen Beaver
$section->addInput(new Form_Input(
158
	'backupcount',
159
	'Backup Count',
160
	'number',
161
	$config['system']['backupcount']
162 16b17c15 stilez
))->setHelp('Maximum number of old configurations to keep in the cache, 0 for no backups, or leave blank for the default value (' . $g['default_config_backup_count'] . ' for the current platform).');
163 dbeffa2f Stephen Beaver
164
$space = exec("/usr/bin/du -sh /conf/backup | /usr/bin/awk '{print $1;}'");
165
166
$section->addInput(new Form_StaticText(
167
	'Current space used by backups',
168
	$space
169
));
170
171 89333d37 jim-p
$section->addInput(new Form_Button(
172
	'Submit',
173 827a3812 jim-p
	gettext("Save"),
174
	null,
175
	'fa-save'
176
))->addClass('btn-primary');
177 89333d37 jim-p
178 dbeffa2f Stephen Beaver
$form->add($section);
179
180
print($form);
181
182
if (is_array($confvers)) {
183 e47f1893 Phil Davis
?>
184
<div>
185 c95dabdd Stephen Beaver
	<div class="infoblock blockopen">
186 f6aebbcc NewEraCracker
		<?php print_info_box(
187 e47f1893 Phil Davis
			gettext(
188
				'To view the differences between an older configuration and a newer configuration, ' .
189
				'select the older configuration using the left column of radio options and select the newer configuration in the right column, ' .
190
				'then press the "Diff" button.'),
191 f6aebbcc NewEraCracker
			'info', false); ?>
192 e47f1893 Phil Davis
	</div>
193
</div>
194
<?php
195 0da0d43e Phil Davis
}
196
?>
197
198 dbeffa2f Stephen Beaver
<form action="diag_confbak.php" method="get">
199 12537fdf Phil Davis
	<div class="table-responsive">
200 dbeffa2f Stephen Beaver
		<table class="table table-striped table-hover table-condensed">
201 0da0d43e Phil Davis
<?php
202 dbeffa2f Stephen Beaver
if (is_array($confvers)):
203
?>
204
			<thead>
205
				<tr>
206
					<th colspan="2">
207 827a3812 jim-p
						<button type="submit" name="diff" class="btn btn-info btn-xs" value="<?=gettext("Diff"); ?>">
208
							<i class="fa fa-exchange icon-embed-btn"></i>
209
							<?=gettext("Diff"); ?>
210
						</button>
211 dbeffa2f Stephen Beaver
					</th>
212
					<th><?=gettext("Date")?></th>
213
					<th><?=gettext("Version")?></th>
214
					<th><?=gettext("Size")?></th>
215
					<th><?=gettext("Configuration Change")?></th>
216
					<th><?=gettext("Actions")?></th>
217
				</tr>
218
			</thead>
219
			<tbody>
220
				<!-- First row is the current configuration -->
221 ee10332b NewEraCracker
				<tr style="vertical-align:top;">
222 dbeffa2f Stephen Beaver
					<td></td>
223
					<td>
224
						<input type="radio" name="newtime" value="current" />
225
					</td>
226
					<td><?= date(gettext("n/j/y H:i:s"), $config['revision']['time']) ?></td>
227
					<td><?= $config['version'] ?></td>
228
					<td><?= format_bytes(filesize("/conf/config.xml")) ?></td>
229
					<td><?= htmlspecialchars($config['revision']['description']) ?></td>
230
					<td><?=gettext("Current configuration")?></td>
231
				</tr>
232
<?php
233 2d911f2c Phil Davis
	// And now for the table of prior backups
234 dbeffa2f Stephen Beaver
	$c = 0;
235
	foreach ($confvers as $version):
236
		if ($version['time'] != 0) {
237
			$date = date(gettext("n/j/y H:i:s"), $version['time']);
238
		} else {
239
			$date = gettext("Unknown");
240
		}
241
?>
242
				<tr>
243
					<td>
244
						<input type="radio" name="oldtime" value="<?=$version['time']?>" />
245
					</td>
246
					<td>
247
<?php
248
		if ($c < (count($confvers) - 1)) {
249
?>
250
								<input type="radio" name="newtime" value="<?=$version['time']?>" />
251 0da0d43e Phil Davis
<?php
252 dbeffa2f Stephen Beaver
		}
253 0da0d43e Phil Davis
		$c++;
254 dbeffa2f Stephen Beaver
?>
255
					</td>
256
					<td><?= $date ?></td>
257
					<td><?= $version['version'] ?></td>
258
					<td><?= format_bytes($version['filesize']) ?></td>
259
					<td><?= htmlspecialchars($version['description']) ?></td>
260
					<td>
261 34ee6639 NOYB
						<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.")?>')"></a>
262 0da0d43e Phil Davis
						<a class="fa fa-download"	title="<?=gettext('Download config')?>"	href="diag_confbak.php?getcfg=<?=$version['time']?>"></a>
263 33f0b0d5 Stephen Beaver
						<a class="fa fa-trash"		title="<?=gettext('Delete config')?>"	href="diag_confbak.php?rmver=<?=$version['time']?>"></a>
264 dbeffa2f Stephen Beaver
					</td>
265
				</tr>
266 0da0d43e Phil Davis
<?php
267 dbeffa2f Stephen Beaver
	endforeach;
268
?>
269
				<tr>
270 27d6a45b jim-p
					<td colspan="2">
271
						<button type="submit" name="diff" class="btn btn-info btn-xs" value="<?=gettext("Diff"); ?>">
272
							<i class="fa fa-exchange icon-embed-btn"></i>
273
							<?=gettext("Diff"); ?>
274
						</button>
275
					</td>
276 dbeffa2f Stephen Beaver
					<td colspan="5"></td>
277
				</tr>
278 0da0d43e Phil Davis
<?php
279 dbeffa2f Stephen Beaver
else:
280
	print_info_box(gettext("No backups found."), 'danger');
281
endif;
282
?>
283
			</tbody>
284
		</table>
285 217cdb81 NOYB
	</div>
286
</form>
287 dbeffa2f Stephen Beaver
288 b782c904 Phil Davis
<?php include("foot.inc");