Project

General

Profile

Download (9.23 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
##|*WARN=standard-warning-root
30
##|*MATCH=diag_confbak.php*
31
##|-PRIV
32

    
33
require_once("guiconfig.inc");
34

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
161
$form = new Form(false);
162

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

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

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

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

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

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

    
189
print($form);
190

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

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

    
297
<?php include("foot.inc");
(12-12/227)