Project

General

Profile

Download (8.83 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-2016 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 (is_numericint($_POST['backupcount'])) {
34
		$config['system']['backupcount'] = $_POST['backupcount'];
35
		$changedescr = $config['system']['backupcount'];
36
	} else {
37
		unset($config['system']['backupcount']);
38
		$changedescr = gettext("(platform default)");
39
	}
40

    
41
	write_config(sprintf(gettext("Changed backup revision count to %s"), $changedescr));
42
}
43

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

    
46
if ($_POST['newver'] != "") {
47
	if (config_restore($g['conf_path'] . '/backup/config-' . $_POST['newver'] . '.xml') == 0) {
48
		$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']));
49
	} else {
50
		$savemsg = gettext("Unable to revert to the selected configuration.");
51
	}
52
}
53

    
54
if ($_POST['rmver'] != "") {
55
	unlink_if_exists($g['conf_path'] . '/backup/config-' . $_POST['rmver'] . '.xml');
56
	$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']));
57
}
58

    
59
if ($_REQUEST['getcfg'] != "") {
60
	$_REQUEST['getcfg'] = basename($_REQUEST['getcfg']);
61
	$file = $g['conf_path'] . '/backup/config-' . $_REQUEST['getcfg'] . '.xml';
62

    
63
	$exp_name = urlencode("config-{$config['system']['hostname']}.{$config['system']['domain']}-{$_GET['getcfg']}.xml");
64
	$exp_data = file_get_contents($file);
65
	$exp_size = strlen($exp_data);
66

    
67
	header("Content-Type: application/octet-stream");
68
	header("Content-Disposition: attachment; filename={$exp_name}");
69
	header("Content-Length: $exp_size");
70
	echo $exp_data;
71
	exit;
72
}
73

    
74
if (($_REQUEST['diff'] == 'Diff') && isset($_REQUEST['oldtime']) && isset($_REQUEST['newtime']) &&
75
    (is_numeric($_REQUEST['oldtime'])) &&
76
    (is_numeric($_REQUEST['newtime']) || ($_REQUEST['newtime'] == 'current'))) {
77
	$diff = "";
78
	$oldfile = $g['conf_path'] . '/backup/config-' . $_REQUEST['oldtime'] . '.xml';
79
	$oldtime = $_REQUEST['oldtime'];
80
	if ($_REQUEST['newtime'] == 'current') {
81
		$newfile = $g['conf_path'] . '/config.xml';
82
		$newtime = $config['revision']['time'];
83
	} else {
84
		$newfile = $g['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 = array(gettext("Diagnostics"), htmlspecialchars(gettext("Backup & Restore")), gettext("Config History"));
97
$pglinks = array("", "diag_backup.php", "@self");
98
include("head.inc");
99

    
100
if ($savemsg) {
101
	print_info_box($savemsg, 'success');
102
}
103

    
104
$tab_array = array();
105
$tab_array[] = array(htmlspecialchars(gettext("Backup & Restore")), false, "diag_backup.php");
106
$tab_array[] = array(gettext("Config History"), true, "diag_confbak.php");
107
display_top_tabs($tab_array);
108

    
109
if ($diff) {
110
?>
111
<div class="panel panel-default">
112
	<div class="panel-heading">
113
		<h2 class="panel-title">
114
			<?=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))?>
115
		</h2>
116
	</div>
117
	<div class="panel-body table-responsive">
118
	<!-- This table is left un-bootstrapped to maintain the original diff format output -->
119
		<table style="padding-top: 4px; padding-bottom: 4px; vertical-align:middle;">
120

    
121
<?php
122
	foreach ($diff as $line) {
123
		switch (substr($line, 0, 1)) {
124
			case "+":
125
				$color = "#caffd3";
126
				break;
127
			case "-":
128
				$color = "#ffe8e8";
129
				break;
130
			case "@":
131
				$color = "#a0a0a0";
132
				break;
133
			default:
134
				$color = "#ffffff";
135
		}
136
?>
137
			<tr>
138
				<td class="diff-text" style="vertical-align:middle; background-color:<?=$color;?>; white-space:pre-wrap;"><?=htmlentities($line)?></td>
139
			</tr>
140
<?php
141
	}
142
?>
143
		</table>
144
	</div>
145
</div>
146
<?php
147
}
148

    
149
$form = new Form(false);
150

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

    
153
$section->addInput(new Form_Input(
154
	'backupcount',
155
	'Backup Count',
156
	'number',
157
	$config['system']['backupcount']
158
))->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']);
159

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

    
162
$section->addInput(new Form_StaticText(
163
	'Current space used by backups',
164
	$space
165
));
166

    
167
$section->addInput(new Form_Button(
168
	'Submit',
169
	gettext("Save"),
170
	null,
171
	'fa-save'
172
))->addClass('btn-primary');
173

    
174
$form->add($section);
175

    
176
print($form);
177

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

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

    
284
<?php include("foot.inc");
(8-8/223)