Project

General

Profile

Download (9.15 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-2024 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_set_path('system/backupcount', $_POST['backupcount']);
43
			$changedescr = config_get_path('system/backupcount');
44
		} elseif (empty($_POST['backupcount'])) {
45
			config_del_path('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_data(file_get_contents(g_get('cf_conf_path') . '/backup/backup.cache'), []);
53

    
54
if ($_POST['newver'] != "") {
55
	if (config_restore(g_get('conf_path') . '/backup/config-' . $_POST['newver'] . '.xml', htmlspecialchars($confvers[$_POST['newver']]['description']))) {
56
		$savemsg = sprintf(gettext('Successfully reverted configuration to timestamp %1$s with description "%2$s".%3$s%3$sTo activate the changes, manually reboot or apply/reload relevant features.'), date(gettext("n/j/y H:i:s"), $_POST['newver']), htmlspecialchars($confvers[$_POST['newver']]['description']), '<br/>');
57
	} else {
58
		$savemsg = gettext("Unable to revert to the selected configuration.");
59
	}
60
}
61

    
62
if ($_POST['rmver'] != "") {
63
	unlink_if_exists(g_get('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
	send_user_download('file',
70
				g_get('conf_path') . '/backup/config-' . $_REQUEST['getcfg'] . '.xml',
71
				'config-' . config_get_path('system/hostname') . '.' . config_get_path('system/domain') . "-{$_REQUEST['getcfg']}.xml");
72
}
73

    
74
if (($_REQUEST['compare'] == 'compare') && isset($_REQUEST['oldtime']) && isset($_REQUEST['newtime']) &&
75
    (is_numeric($_REQUEST['oldtime'])) &&
76
    (is_numeric($_REQUEST['newtime']) || ($_REQUEST['newtime'] == 'current'))) {
77
	$diff = "";
78
	$oldfile = g_get('conf_path') . '/backup/config-' . $_REQUEST['oldtime'] . '.xml';
79
	$oldtime = $_REQUEST['oldtime'];
80
	if ($_REQUEST['newtime'] == 'current') {
81
		$newfile = g_get('conf_path') . '/config.xml';
82
		$newtime = config_get_path('revision/time');
83
	} else {
84
		$newfile = g_get('conf_path') . '/backup/config-' . $_REQUEST['newtime'] . '.xml';
85
		$newtime = $_REQUEST['newtime'];
86
	}
87
	if (file_exists($oldfile) && file_exists($newfile)) {
88
		exec("/usr/bin/diff -u " . escapeshellarg($oldfile) . " " . escapeshellarg($newfile), $diff);
89
	}
90
}
91

    
92
cleanup_backupcache(false);
93
$confvers = get_backups();
94
unset($confvers['versions']);
95

    
96
$pgtitle = [gettext('Diagnostics'), htmlspecialchars(gettext('Backup & Restore')), gettext('Config History')];
97
$pglinks = ['', 'diag_backup.php', '@self'];
98
include('head.inc');
99

    
100
if ($input_errors) {
101
	print_input_errors($input_errors);
102
}
103

    
104
if ($savemsg) {
105
	print_info_box($savemsg, 'success');
106
}
107

    
108
$tab_array = array();
109
$tab_array[] = [htmlspecialchars(gettext('Backup & Restore')), false, "diag_backup.php"];
110
$tab_array[] = [gettext('Config History'), true, 'diag_confbak.php'];
111
display_top_tabs($tab_array);
112

    
113
if ($diff):
114
?>
115
<div class="panel panel-default">
116
	<div class="panel-heading">
117
		<h2 class="panel-title">
118
			<?=sprintf(gettext('Configuration Difference 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
		</h2>
120
	</div>
121
	<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

    
125
<?php
126
$colors = [
127
	'+' => '#caffd3',
128
	'-' => '#ffe8e8',
129
	'@' => '#a0a0a0'
130
];
131
	foreach ($diff as $line):
132
?>
133
			<tr>
134
				<td class="diff-text" style="vertical-align:middle; background-color:<?=$colors[substr($line, 0, 1)] ?? '#ffffff'?>; white-space:pre-wrap;"><?=htmlentities($line)?></td>
135
			</tr>
136
<?php
137
	endforeach;
138
?>
139
		</table>
140
	</div>
141
</div>
142
<?php
143
endif;
144

    
145
$form = new Form(false);
146

    
147
$section = new Form_Section(gettext('Configuration Backup Settings'), 'configsettings');
148

    
149
$section->addInput(new Form_Input(
150
	'backupcount',
151
	gettext('Maximum Backups'),
152
	'number',
153
	config_get_path('system/backupcount'),
154
	['min' => '0', 'placeholder' => g_get('default_config_backup_count')]
155
))->setHelp(gettext('Maximum number of old configuration backups to keep in the cache, 0 for no backups, or leave blank for the default value.'));
156

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

    
159
$section->addInput(new Form_StaticText(
160
	gettext('Used Space'),
161
	$space
162
));
163

    
164
$section->addInput(new Form_Button(
165
	'Submit',
166
	gettext('Save'),
167
	null,
168
	'fa-solid fa-save'
169
))->addClass('btn-primary');
170

    
171
$form->add($section);
172

    
173
print($form);
174

    
175
if (is_array($confvers)) {
176
?>
177
<div>
178
	<div class="infoblock blockopen">
179
		<?php print_info_box(
180
			gettext(
181
				'To view the differences between an older configuration and a newer configuration, ' .
182
				'select the older configuration using the left column of radio options and select the newer configuration in the right column, ' .
183
				'then press the "Compare" button.'), 'info', false); ?>
184
	</div>
185
</div>
186
<?php
187
}
188
?>
189

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

    
280
<?php include("foot.inc");
(13-13/232)