Project

General

Profile

Download (8.94 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
$pglinks = array("", "diag_backup.php", "@self");
101
include("head.inc");
102

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

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

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

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

    
152
$form = new Form(false);
153

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

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

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

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

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

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

    
179
print($form);
180

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

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

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