Project

General

Profile

Download (9.11 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-2019 Rubicon Communications, LLC (Netgate)
7
 * Copyright (c) 2005 Colin Smith
8
 * All rights reserved.
9
 *
10
 * 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
 *
14
 * http://www.apache.org/licenses/LICENSE-2.0
15
 *
16
 * 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
 */
22

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

    
30
require_once("guiconfig.inc");
31

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

    
37
	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
}
48

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

    
51
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
	}
57
}
58

    
59
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

    
68
	$exp_name = urlencode("config-{$config['system']['hostname']}.{$config['system']['domain']}-{$_REQUEST['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
if (($_REQUEST['diff'] == 'Diff') && isset($_REQUEST['oldtime']) && isset($_REQUEST['newtime']) &&
80
    (is_numeric($_REQUEST['oldtime'])) &&
81
    (is_numeric($_REQUEST['newtime']) || ($_REQUEST['newtime'] == 'current'))) {
82
	$diff = "";
83
	$oldfile = $g['conf_path'] . '/backup/config-' . $_REQUEST['oldtime'] . '.xml';
84
	$oldtime = $_REQUEST['oldtime'];
85
	if ($_REQUEST['newtime'] == 'current') {
86
		$newfile = $g['conf_path'] . '/config.xml';
87
		$newtime = $config['revision']['time'];
88
	} else {
89
		$newfile = $g['conf_path'] . '/backup/config-' . $_REQUEST['newtime'] . '.xml';
90
		$newtime = $_REQUEST['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
cleanup_backupcache(false);
98
$confvers = get_backups();
99
unset($confvers['versions']);
100

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

    
105
if ($input_errors) {
106
	print_input_errors($input_errors);
107
}
108

    
109
if ($savemsg) {
110
	print_info_box($savemsg, 'success');
111
}
112

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

    
118
if ($diff) {
119
?>
120
<div class="panel panel-default">
121
	<div class="panel-heading">
122
		<h2 class="panel-title">
123
			<?=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
		</h2>
125
	</div>
126
	<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

    
130
<?php
131
	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
				<td class="diff-text" style="vertical-align:middle; background-color:<?=$color;?>; white-space:pre-wrap;"><?=htmlentities($line)?></td>
148
			</tr>
149
<?php
150
	}
151
?>
152
		</table>
153
	</div>
154
</div>
155
<?php
156
}
157

    
158
$form = new Form(false);
159

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

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

    
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
$section->addInput(new Form_Button(
178
	'Submit',
179
	gettext("Save"),
180
	null,
181
	'fa-save'
182
))->addClass('btn-primary');
183

    
184
$form->add($section);
185

    
186
print($form);
187

    
188
if (is_array($confvers)) {
189
?>
190
<div>
191
	<div class="infoblock blockopen">
192
		<?php print_info_box(
193
			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
			'info', false); ?>
198
	</div>
199
</div>
200
<?php
201
}
202
?>
203

    
204
<form action="diag_confbak.php" method="get">
205
	<div class="table-responsive">
206
		<table class="table table-striped table-hover table-condensed">
207
<?php
208
if (is_array($confvers)):
209
?>
210
			<thead>
211
				<tr>
212
					<th colspan="2">
213
						<button type="submit" name="diff" class="btn btn-info btn-xs" value="Diff">
214
							<i class="fa fa-exchange icon-embed-btn"></i>
215
							<?=gettext("Diff"); ?>
216
						</button>
217
					</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
				<tr style="vertical-align:top;">
228
					<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
	// And now for the table of prior backups
240
	$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
<?php
258
		}
259
		$c++;
260
?>
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
						<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
						<a class="fa fa-download"	title="<?=gettext('Download config')?>"	href="diag_confbak.php?getcfg=<?=$version['time']?>"></a>
269
						<a class="fa fa-trash"		title="<?=gettext('Delete config')?>"	href="diag_confbak.php?rmver=<?=$version['time']?>" usepost></a>
270
					</td>
271
				</tr>
272
<?php
273
	endforeach;
274
?>
275
				<tr>
276
					<td colspan="2">
277
						<button type="submit" name="diff" class="btn btn-info btn-xs" value="Diff">
278
							<i class="fa fa-exchange icon-embed-btn"></i>
279
							<?=gettext("Diff"); ?>
280
						</button>
281
					</td>
282
					<td colspan="5"></td>
283
				</tr>
284
<?php
285
else:
286
	print_info_box(gettext("No backups found."), 'danger');
287
endif;
288
?>
289
			</tbody>
290
		</table>
291
	</div>
292
</form>
293

    
294
<?php include("foot.inc");
(11-11/234)