Project

General

Profile

Download (8.89 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
	write_config(sprintf(gettext("Changed backup revision count to %s"), $changedescr));
41
} elseif ($_GET) {
42
	if (!isset($_GET['newver']) && !isset($_GET['rmver']) && !isset($_GET['getcfg']) && !isset($_GET['diff'])) {
43
		header("Location: diag_confbak.php");
44
		return;
45
	}
46

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

    
49
	if ($_GET['newver'] != "") {
50
		if (config_restore($g['conf_path'] . '/backup/config-' . $_GET['newver'] . '.xml') == 0) {
51
			$savemsg = sprintf(gettext('Successfully reverted to timestamp %1$s with description "%2$s".'), date(gettext("n/j/y H:i:s"), $_GET['newver']), htmlspecialchars($confvers[$_GET['newver']]['description']));
52
		} else {
53
			$savemsg = gettext("Unable to revert to the selected configuration.");
54
		}
55
	}
56
	if ($_GET['rmver'] != "") {
57
		unlink_if_exists($g['conf_path'] . '/backup/config-' . $_GET['rmver'] . '.xml');
58
		$savemsg = sprintf(gettext('Deleted backup with timestamp %1$s and description "%2$s".'), date(gettext("n/j/y H:i:s"), $_GET['rmver']), htmlspecialchars($confvers[$_GET['rmver']]['description']));
59
	}
60
}
61

    
62
if ($_GET['getcfg'] != "") {
63
	$_GET['getcfg'] = basename($_GET['getcfg']);
64
	$file = $g['conf_path'] . '/backup/config-' . $_GET['getcfg'] . '.xml';
65

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

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

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

    
95
cleanup_backupcache(false);
96
$confvers = get_backups();
97
unset($confvers['versions']);
98

    
99
$pgtitle = array(gettext("Diagnostics"), htmlspecialchars(gettext("Backup & Restore")), gettext("Config History"));
100
include("head.inc");
101

    
102
if ($savemsg) {
103
	print_info_box($savemsg, 'success');
104
}
105

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

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

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

    
151
$form = new Form(false);
152

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

    
155
$section->addInput(new Form_Input(
156
	'backupcount',
157
	'Backup Count',
158
	'number',
159
	$config['system']['backupcount']
160
))->setHelp('Maximum number of old configurations to keep in the cache, 0 for no backups, or leave blank for the default value (' . $g['default_config_backup_count'] . ' for the current platform).');
161

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

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

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

    
176
$form->add($section);
177

    
178
print($form);
179

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

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

    
286
<?php include("foot.inc");
(8-8/225)