Project

General

Profile

Download (9.14 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 81299b5c Renato Botelho
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7 c5d81585 Renato Botelho
 * 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 3057a2ba jim-p
	if (!empty($_POST['backupcount']) && (!is_numericint($_POST['backupcount']) || ($_POST['backupcount'] < 0))) {
34
		$input_errors[] = gettext("Invalid Backup Count specified");
35 e1ebe9e2 jim-p
	}
36 7f4268b6 Steve Beaver
37 3057a2ba jim-p
	if (!$input_errors) {
38
		if (is_numericint($_POST['backupcount'])) {
39
			$config['system']['backupcount'] = $_POST['backupcount'];
40
			$changedescr = $config['system']['backupcount'];
41
		} elseif (empty($_POST['backupcount'])) {
42
			unset($config['system']['backupcount']);
43
			$changedescr = gettext("(platform default)");
44
		}
45
		write_config(sprintf(gettext("Changed backup revision count to %s"), $changedescr));
46
	}
47 7f4268b6 Steve Beaver
}
48 2f8d0729 Bill Marquette
49 7f4268b6 Steve Beaver
$confvers = unserialize(file_get_contents($g['cf_conf_path'] . '/backup/backup.cache'));
50 0da0d43e Phil Davis
51 7f4268b6 Steve Beaver
if ($_POST['newver'] != "") {
52
	if (config_restore($g['conf_path'] . '/backup/config-' . $_POST['newver'] . '.xml') == 0) {
53
		$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']));
54
	} else {
55
		$savemsg = gettext("Unable to revert to the selected configuration.");
56 b6513591 jim-p
	}
57 2f8d0729 Bill Marquette
}
58
59 7f4268b6 Steve Beaver
if ($_POST['rmver'] != "") {
60
	unlink_if_exists($g['conf_path'] . '/backup/config-' . $_POST['rmver'] . '.xml');
61
	$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']));
62
}
63
64
if ($_REQUEST['getcfg'] != "") {
65
	$_REQUEST['getcfg'] = basename($_REQUEST['getcfg']);
66
	$file = $g['conf_path'] . '/backup/config-' . $_REQUEST['getcfg'] . '.xml';
67 9f9b88e2 jim-p
68 cbb82e6b Steve Beaver
	$exp_name = urlencode("config-{$config['system']['hostname']}.{$config['system']['domain']}-{$_REQUEST['getcfg']}.xml");
69 9f9b88e2 jim-p
	$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 7f4268b6 Steve Beaver
if (($_REQUEST['diff'] == 'Diff') && isset($_REQUEST['oldtime']) && isset($_REQUEST['newtime']) &&
80
    (is_numeric($_REQUEST['oldtime'])) &&
81
    (is_numeric($_REQUEST['newtime']) || ($_REQUEST['newtime'] == 'current'))) {
82 957e2f1f jim-p
	$diff = "";
83 7f4268b6 Steve Beaver
	$oldfile = $g['conf_path'] . '/backup/config-' . $_REQUEST['oldtime'] . '.xml';
84
	$oldtime = $_REQUEST['oldtime'];
85
	if ($_REQUEST['newtime'] == 'current') {
86 957e2f1f jim-p
		$newfile = $g['conf_path'] . '/config.xml';
87
		$newtime = $config['revision']['time'];
88
	} else {
89 7f4268b6 Steve Beaver
		$newfile = $g['conf_path'] . '/backup/config-' . $_REQUEST['newtime'] . '.xml';
90
		$newtime = $_REQUEST['newtime'];
91 957e2f1f jim-p
	}
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 edcd7535 Phil Davis
$pglinks = array("", "diag_backup.php", "@self");
103 b63695db Scott Ullrich
include("head.inc");
104
105 3057a2ba jim-p
if ($input_errors) {
106
	print_input_errors($input_errors);
107
}
108
109 dbeffa2f Stephen Beaver
if ($savemsg) {
110
	print_info_box($savemsg, 'success');
111
}
112
113 1d191646 Phil Davis
$tab_array = array();
114 0f298138 k-paulius
$tab_array[] = array(htmlspecialchars(gettext("Backup & Restore")), false, "diag_backup.php");
115 2b38c46b k-paulius
$tab_array[] = array(gettext("Config History"), true, "diag_confbak.php");
116 1d191646 Phil Davis
display_top_tabs($tab_array);
117
118 dbeffa2f Stephen Beaver
if ($diff) {
119 f79f9497 Sjon Hortensius
?>
120 dbeffa2f Stephen Beaver
<div class="panel panel-default">
121 95fa5cce Phil Davis
	<div class="panel-heading">
122
		<h2 class="panel-title">
123 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))?>
124 95fa5cce Phil Davis
		</h2>
125
	</div>
126 dbeffa2f Stephen Beaver
	<div class="panel-body table-responsive">
127
	<!-- This table is left un-bootstrapped to maintain the original diff format output -->
128
		<table style="padding-top: 4px; padding-bottom: 4px; vertical-align:middle;">
129 f79f9497 Sjon Hortensius
130
<?php
131 dbeffa2f Stephen Beaver
	foreach ($diff as $line) {
132
		switch (substr($line, 0, 1)) {
133
			case "+":
134
				$color = "#caffd3";
135
				break;
136
			case "-":
137
				$color = "#ffe8e8";
138
				break;
139
			case "@":
140
				$color = "#a0a0a0";
141
				break;
142
			default:
143
				$color = "#ffffff";
144
		}
145
?>
146
			<tr>
147 91419f70 NewEraCracker
				<td class="diff-text" style="vertical-align:middle; background-color:<?=$color;?>; white-space:pre-wrap;"><?=htmlentities($line)?></td>
148 dbeffa2f Stephen Beaver
			</tr>
149 0da0d43e Phil Davis
<?php
150 dbeffa2f Stephen Beaver
	}
151 f79f9497 Sjon Hortensius
?>
152 dbeffa2f Stephen Beaver
		</table>
153
	</div>
154
</div>
155 0da0d43e Phil Davis
<?php
156 dbeffa2f Stephen Beaver
}
157 f79f9497 Sjon Hortensius
158 89333d37 jim-p
$form = new Form(false);
159 b6513591 jim-p
160 ca55edc3 stilez
$section = new Form_Section('Configuration Backup Cache Settings', 'configsettings', COLLAPSIBLE|SEC_CLOSED);
161 b6513591 jim-p
162 dbeffa2f Stephen Beaver
$section->addInput(new Form_Input(
163
	'backupcount',
164
	'Backup Count',
165
	'number',
166 3057a2ba jim-p
	$config['system']['backupcount'],
167
	['min' => '0']
168 4ebcee24 Steve Beaver
))->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']);
169 dbeffa2f Stephen Beaver
170
$space = exec("/usr/bin/du -sh /conf/backup | /usr/bin/awk '{print $1;}'");
171
172
$section->addInput(new Form_StaticText(
173
	'Current space used by backups',
174
	$space
175
));
176
177 89333d37 jim-p
$section->addInput(new Form_Button(
178
	'Submit',
179 827a3812 jim-p
	gettext("Save"),
180
	null,
181
	'fa-save'
182
))->addClass('btn-primary');
183 89333d37 jim-p
184 dbeffa2f Stephen Beaver
$form->add($section);
185
186
print($form);
187
188
if (is_array($confvers)) {
189 e47f1893 Phil Davis
?>
190
<div>
191 c95dabdd Stephen Beaver
	<div class="infoblock blockopen">
192 f6aebbcc NewEraCracker
		<?php print_info_box(
193 e47f1893 Phil Davis
			gettext(
194
				'To view the differences between an older configuration and a newer configuration, ' .
195
				'select the older configuration using the left column of radio options and select the newer configuration in the right column, ' .
196
				'then press the "Diff" button.'),
197 f6aebbcc NewEraCracker
			'info', false); ?>
198 e47f1893 Phil Davis
	</div>
199
</div>
200
<?php
201 0da0d43e Phil Davis
}
202
?>
203
204 dbeffa2f Stephen Beaver
<form action="diag_confbak.php" method="get">
205 12537fdf Phil Davis
	<div class="table-responsive">
206 dbeffa2f Stephen Beaver
		<table class="table table-striped table-hover table-condensed">
207 0da0d43e Phil Davis
<?php
208 dbeffa2f Stephen Beaver
if (is_array($confvers)):
209
?>
210
			<thead>
211
				<tr>
212
					<th colspan="2">
213 827a3812 jim-p
						<button type="submit" name="diff" class="btn btn-info btn-xs" value="<?=gettext("Diff"); ?>">
214
							<i class="fa fa-exchange icon-embed-btn"></i>
215
							<?=gettext("Diff"); ?>
216
						</button>
217 dbeffa2f Stephen Beaver
					</th>
218
					<th><?=gettext("Date")?></th>
219
					<th><?=gettext("Version")?></th>
220
					<th><?=gettext("Size")?></th>
221
					<th><?=gettext("Configuration Change")?></th>
222
					<th><?=gettext("Actions")?></th>
223
				</tr>
224
			</thead>
225
			<tbody>
226
				<!-- First row is the current configuration -->
227 ee10332b NewEraCracker
				<tr style="vertical-align:top;">
228 dbeffa2f Stephen Beaver
					<td></td>
229
					<td>
230
						<input type="radio" name="newtime" value="current" />
231
					</td>
232
					<td><?= date(gettext("n/j/y H:i:s"), $config['revision']['time']) ?></td>
233
					<td><?= $config['version'] ?></td>
234
					<td><?= format_bytes(filesize("/conf/config.xml")) ?></td>
235
					<td><?= htmlspecialchars($config['revision']['description']) ?></td>
236
					<td><?=gettext("Current configuration")?></td>
237
				</tr>
238
<?php
239 2d911f2c Phil Davis
	// And now for the table of prior backups
240 dbeffa2f Stephen Beaver
	$c = 0;
241
	foreach ($confvers as $version):
242
		if ($version['time'] != 0) {
243
			$date = date(gettext("n/j/y H:i:s"), $version['time']);
244
		} else {
245
			$date = gettext("Unknown");
246
		}
247
?>
248
				<tr>
249
					<td>
250
						<input type="radio" name="oldtime" value="<?=$version['time']?>" />
251
					</td>
252
					<td>
253
<?php
254
		if ($c < (count($confvers) - 1)) {
255
?>
256
								<input type="radio" name="newtime" value="<?=$version['time']?>" />
257 0da0d43e Phil Davis
<?php
258 dbeffa2f Stephen Beaver
		}
259 0da0d43e Phil Davis
		$c++;
260 dbeffa2f Stephen Beaver
?>
261
					</td>
262
					<td><?= $date ?></td>
263
					<td><?= $version['version'] ?></td>
264
					<td><?= format_bytes($version['filesize']) ?></td>
265
					<td><?= htmlspecialchars($version['description']) ?></td>
266
					<td>
267 7f4268b6 Steve Beaver
						<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>
268 0da0d43e Phil Davis
						<a class="fa fa-download"	title="<?=gettext('Download config')?>"	href="diag_confbak.php?getcfg=<?=$version['time']?>"></a>
269 7f4268b6 Steve Beaver
						<a class="fa fa-trash"		title="<?=gettext('Delete config')?>"	href="diag_confbak.php?rmver=<?=$version['time']?>" usepost></a>
270 dbeffa2f Stephen Beaver
					</td>
271
				</tr>
272 0da0d43e Phil Davis
<?php
273 dbeffa2f Stephen Beaver
	endforeach;
274
?>
275
				<tr>
276 27d6a45b jim-p
					<td colspan="2">
277
						<button type="submit" name="diff" class="btn btn-info btn-xs" value="<?=gettext("Diff"); ?>">
278
							<i class="fa fa-exchange icon-embed-btn"></i>
279
							<?=gettext("Diff"); ?>
280
						</button>
281
					</td>
282 dbeffa2f Stephen Beaver
					<td colspan="5"></td>
283
				</tr>
284 0da0d43e Phil Davis
<?php
285 dbeffa2f Stephen Beaver
else:
286
	print_info_box(gettext("No backups found."), 'danger');
287
endif;
288
?>
289
			</tbody>
290
		</table>
291 217cdb81 NOYB
	</div>
292
</form>
293 dbeffa2f Stephen Beaver
294 b782c904 Phil Davis
<?php include("foot.inc");