Project

General

Profile

Download (10.6 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
 * Redistribution and use in source and binary forms, with or without
11
 * modification, are permitted provided that the following conditions are met:
12
 *
13
 * 1. Redistributions of source code must retain the above copyright notice,
14
 *    this list of conditions and the following disclaimer.
15
 *
16
 * 2. Redistributions in binary form must reproduce the above copyright
17
 *    notice, this list of conditions and the following disclaimer in
18
 *    the documentation and/or other materials provided with the
19
 *    distribution.
20
 *
21
 * 3. All advertising materials mentioning features or use of this software
22
 *    must display the following acknowledgment:
23
 *    "This product includes software developed by the pfSense Project
24
 *    for use in the pfSense® software distribution. (http://www.pfsense.org/).
25
 *
26
 * 4. The names "pfSense" and "pfSense Project" must not be used to
27
 *    endorse or promote products derived from this software without
28
 *    prior written permission. For written permission, please contact
29
 *    coreteam@pfsense.org.
30
 *
31
 * 5. Products derived from this software may not be called "pfSense"
32
 *    nor may "pfSense" appear in their names without prior written
33
 *    permission of the Electric Sheep Fencing, LLC.
34
 *
35
 * 6. Redistributions of any form whatsoever must retain the following
36
 *    acknowledgment:
37
 *
38
 * "This product includes software developed by the pfSense Project
39
 * for use in the pfSense software distribution (http://www.pfsense.org/).
40
 *
41
 * THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
42
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
45
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
52
 * OF THE POSSIBILITY OF SUCH DAMAGE.
53
 */
54

    
55
##|+PRIV
56
##|*IDENT=page-diagnostics-configurationhistory
57
##|*NAME=Diagnostics: Configuration History
58
##|*DESCR=Allow access to the 'Diagnostics: Configuration History' page.
59
##|*MATCH=diag_confbak.php*
60
##|-PRIV
61

    
62
require_once("guiconfig.inc");
63

    
64
if (isset($_POST['backupcount'])) {
65
	if (is_numericint($_POST['backupcount'])) {
66
		$config['system']['backupcount'] = $_POST['backupcount'];
67
		$changedescr = $config['system']['backupcount'];
68
	} else {
69
		unset($config['system']['backupcount']);
70
		$changedescr = gettext("(platform default)");
71
	}
72
	write_config(sprintf(gettext("Changed backup revision count to %s"), $changedescr));
73
} elseif ($_GET) {
74
	if (!isset($_GET['newver']) && !isset($_GET['rmver']) && !isset($_GET['getcfg']) && !isset($_GET['diff'])) {
75
		header("Location: diag_confbak.php");
76
		return;
77
	}
78

    
79
	conf_mount_rw();
80
	$confvers = unserialize(file_get_contents($g['cf_conf_path'] . '/backup/backup.cache'));
81

    
82
	if ($_GET['newver'] != "") {
83
		if (config_restore($g['conf_path'] . '/backup/config-' . $_GET['newver'] . '.xml') == 0) {
84
			$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']));
85
		} else {
86
			$savemsg = gettext("Unable to revert to the selected configuration.");
87
		}
88
	}
89
	if ($_GET['rmver'] != "") {
90
		unlink_if_exists($g['conf_path'] . '/backup/config-' . $_GET['rmver'] . '.xml');
91
		$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']));
92
	}
93
	conf_mount_ro();
94
}
95

    
96
if ($_GET['getcfg'] != "") {
97
	$_GET['getcfg'] = basename($_GET['getcfg']);
98
	$file = $g['conf_path'] . '/backup/config-' . $_GET['getcfg'] . '.xml';
99

    
100
	$exp_name = urlencode("config-{$config['system']['hostname']}.{$config['system']['domain']}-{$_GET['getcfg']}.xml");
101
	$exp_data = file_get_contents($file);
102
	$exp_size = strlen($exp_data);
103

    
104
	header("Content-Type: application/octet-stream");
105
	header("Content-Disposition: attachment; filename={$exp_name}");
106
	header("Content-Length: $exp_size");
107
	echo $exp_data;
108
	exit;
109
}
110

    
111
if (($_GET['diff'] == 'Diff') && isset($_GET['oldtime']) && isset($_GET['newtime']) &&
112
    (is_numeric($_GET['oldtime'])) &&
113
    (is_numeric($_GET['newtime']) || ($_GET['newtime'] == 'current'))) {
114
	$diff = "";
115
	$oldfile = $g['conf_path'] . '/backup/config-' . $_GET['oldtime'] . '.xml';
116
	$oldtime = $_GET['oldtime'];
117
	if ($_GET['newtime'] == 'current') {
118
		$newfile = $g['conf_path'] . '/config.xml';
119
		$newtime = $config['revision']['time'];
120
	} else {
121
		$newfile = $g['conf_path'] . '/backup/config-' . $_GET['newtime'] . '.xml';
122
		$newtime = $_GET['newtime'];
123
	}
124
	if (file_exists($oldfile) && file_exists($newfile)) {
125
		exec("/usr/bin/diff -u " . escapeshellarg($oldfile) . " " . escapeshellarg($newfile), $diff);
126
	}
127
}
128

    
129
cleanup_backupcache(false);
130
$confvers = get_backups();
131
unset($confvers['versions']);
132

    
133
$pgtitle = array(gettext("Diagnostics"), htmlspecialchars(gettext("Backup & Restore")), gettext("Config History"));
134
include("head.inc");
135

    
136
if ($savemsg) {
137
	print_info_box($savemsg, 'success');
138
}
139

    
140
$tab_array = array();
141
$tab_array[] = array(htmlspecialchars(gettext("Backup & Restore")), false, "diag_backup.php");
142
$tab_array[] = array(gettext("Config History"), true, "diag_confbak.php");
143
display_top_tabs($tab_array);
144

    
145
if ($diff) {
146
?>
147
<div class="panel panel-default">
148
	<div class="panel-heading">
149
		<h2 class="panel-title">
150
			<?=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))?>
151
		</h2>
152
	</div>
153
	<div class="panel-body table-responsive">
154
	<!-- This table is left un-bootstrapped to maintain the original diff format output -->
155
		<table style="padding-top: 4px; padding-bottom: 4px; vertical-align:middle;">
156

    
157
<?php
158
	foreach ($diff as $line) {
159
		switch (substr($line, 0, 1)) {
160
			case "+":
161
				$color = "#caffd3";
162
				break;
163
			case "-":
164
				$color = "#ffe8e8";
165
				break;
166
			case "@":
167
				$color = "#a0a0a0";
168
				break;
169
			default:
170
				$color = "#ffffff";
171
		}
172
?>
173
			<tr>
174
				<td class="diff-text" style="vertical-align:middle; background-color:<?=$color;?>; white-space:pre-wrap;"><?=htmlentities($line)?></td>
175
			</tr>
176
<?php
177
	}
178
?>
179
		</table>
180
	</div>
181
</div>
182
<?php
183
}
184

    
185
$form = new Form(false);
186

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

    
189
$section->addInput(new Form_Input(
190
	'backupcount',
191
	'Backup Count',
192
	'number',
193
	$config['system']['backupcount']
194
))->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).');
195

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

    
198
$section->addInput(new Form_StaticText(
199
	'Current space used by backups',
200
	$space
201
));
202

    
203
$section->addInput(new Form_Button(
204
	'Submit',
205
	gettext("Save"),
206
	null,
207
	'fa-save'
208
))->addClass('btn-primary');
209

    
210
$form->add($section);
211

    
212
print($form);
213

    
214
if (is_array($confvers)) {
215
?>
216
<div>
217
	<div class="infoblock blockopen">
218
		<?php print_info_box(
219
			gettext(
220
				'To view the differences between an older configuration and a newer configuration, ' .
221
				'select the older configuration using the left column of radio options and select the newer configuration in the right column, ' .
222
				'then press the "Diff" button.'),
223
			'info', false); ?>
224
	</div>
225
</div>
226
<?php
227
}
228
?>
229

    
230
<form action="diag_confbak.php" method="get">
231
	<div class="table-responsive">
232
		<table class="table table-striped table-hover table-condensed">
233
<?php
234
if (is_array($confvers)):
235
?>
236
			<thead>
237
				<tr>
238
					<th colspan="2">
239
						<button type="submit" name="diff" class="btn btn-info btn-xs" value="<?=gettext("Diff"); ?>">
240
							<i class="fa fa-exchange icon-embed-btn"></i>
241
							<?=gettext("Diff"); ?>
242
						</button>
243
					</th>
244
					<th><?=gettext("Date")?></th>
245
					<th><?=gettext("Version")?></th>
246
					<th><?=gettext("Size")?></th>
247
					<th><?=gettext("Configuration Change")?></th>
248
					<th><?=gettext("Actions")?></th>
249
				</tr>
250
			</thead>
251
			<tbody>
252
				<!-- First row is the current configuration -->
253
				<tr style="vertical-align:top;">
254
					<td></td>
255
					<td>
256
						<input type="radio" name="newtime" value="current" />
257
					</td>
258
					<td><?= date(gettext("n/j/y H:i:s"), $config['revision']['time']) ?></td>
259
					<td><?= $config['version'] ?></td>
260
					<td><?= format_bytes(filesize("/conf/config.xml")) ?></td>
261
					<td><?= htmlspecialchars($config['revision']['description']) ?></td>
262
					<td><?=gettext("Current configuration")?></td>
263
				</tr>
264
<?php
265
	// And now for the table of prior backups
266
	$c = 0;
267
	foreach ($confvers as $version):
268
		if ($version['time'] != 0) {
269
			$date = date(gettext("n/j/y H:i:s"), $version['time']);
270
		} else {
271
			$date = gettext("Unknown");
272
		}
273
?>
274
				<tr>
275
					<td>
276
						<input type="radio" name="oldtime" value="<?=$version['time']?>" />
277
					</td>
278
					<td>
279
<?php
280
		if ($c < (count($confvers) - 1)) {
281
?>
282
								<input type="radio" name="newtime" value="<?=$version['time']?>" />
283
<?php
284
		}
285
		$c++;
286
?>
287
					</td>
288
					<td><?= $date ?></td>
289
					<td><?= $version['version'] ?></td>
290
					<td><?= format_bytes($version['filesize']) ?></td>
291
					<td><?= htmlspecialchars($version['description']) ?></td>
292
					<td>
293
						<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>
294
						<a class="fa fa-download"	title="<?=gettext('Download config')?>"	href="diag_confbak.php?getcfg=<?=$version['time']?>"></a>
295
						<a class="fa fa-trash"		title="<?=gettext('Delete config')?>"	href="diag_confbak.php?rmver=<?=$version['time']?>"></a>
296
					</td>
297
				</tr>
298
<?php
299
	endforeach;
300
?>
301
				<tr>
302
					<td colspan="2">
303
						<button type="submit" name="diff" class="btn btn-info btn-xs" value="<?=gettext("Diff"); ?>">
304
							<i class="fa fa-exchange icon-embed-btn"></i>
305
							<?=gettext("Diff"); ?>
306
						</button>
307
					</td>
308
					<td colspan="5"></td>
309
				</tr>
310
<?php
311
else:
312
	print_info_box(gettext("No backups found."), 'danger');
313
endif;
314
?>
315
			</tbody>
316
		</table>
317
	</div>
318
</form>
319

    
320
<?php include("foot.inc");
(8-8/227)