Project

General

Profile

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

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

    
64
require("guiconfig.inc");
65

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

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

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

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

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

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

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

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

    
135
$pgtitle = array(gettext("Diagnostics"), gettext("Configuration History"));
136
include("head.inc");
137

    
138
if ($savemsg) {
139
	print_info_box($savemsg, 'success');
140
}
141

    
142
if ($diff) {
143
?>
144
<div class="panel panel-default">
145
	<div class="panel-heading"><?=gettext("Configuration diff from ")?><?=date(gettext("n/j/y H:i:s"), $oldtime); ?><?=gettext(" to ")?><?=date(gettext("n/j/y H:i:s"), $newtime); ?></div>
146
	<div class="panel-body table-responsive">
147
	<!-- This table is left un-bootstrapped to maintain the original diff format output -->
148
		<table style="padding-top: 4px; padding-bottom: 4px; vertical-align:middle;">
149

    
150
<?php
151
	foreach ($diff as $line) {
152
		switch (substr($line, 0, 1)) {
153
			case "+":
154
				$color = "#caffd3";
155
				break;
156
			case "-":
157
				$color = "#ffe8e8";
158
				break;
159
			case "@":
160
				$color = "#a0a0a0";
161
				break;
162
			default:
163
				$color = "#ffffff";
164
		}
165
?>
166
			<tr>
167
				<td valign="middle" bgcolor="<?=$color; ?>" style="white-space: pre-wrap;"><?=htmlentities($line)?></td>
168
			</tr>
169
<?php
170
	}
171
?>
172
		</table>
173
	</div>
174
</div>
175
<?php
176
}
177

    
178
$tab_array = array();
179
$tab_array[] = array(gettext("Config History"), true, "diag_confbak.php");
180
$tab_array[] = array(gettext("Backup/Restore"), false, "diag_backup.php");
181
display_top_tabs($tab_array);
182

    
183
$form = new Form(new Form_Button(
184
	'Submit',
185
	gettext("Save")
186
));
187

    
188
$section = new Form_Section('Saved Configurations');
189

    
190
$section->addInput(new Form_Input(
191
	'backupcount',
192
	'Backup Count',
193
	'number',
194
	$config['system']['backupcount']
195
))->setHelp('Maximum number of old configurations to keep. By default this is 30 for a full install or 5 on NanoBSD. ');
196

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

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

    
204
$form->add($section);
205

    
206
print($form);
207

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

    
224
<form action="diag_confbak.php" method="get">
225
	<div class="table-resposive">
226
		<table class="table table-striped table-hover table-condensed">
227
<?php
228
if (is_array($confvers)):
229
?>
230
			<thead>
231
				<tr>
232
					<th colspan="2">
233
						<input type="submit" name="diff" class="btn btn-info btn-xs" value="<?=gettext("Diff"); ?>" />
234
					</th>
235
					<th><?=gettext("Date")?></th>
236
					<th><?=gettext("Version")?></th>
237
					<th><?=gettext("Size")?></th>
238
					<th><?=gettext("Configuration Change")?></th>
239
					<th><?=gettext("Actions")?></th>
240
				</tr>
241
			</thead>
242
			<tbody>
243
				<!-- First row is the current configuration -->
244
				<tr valign="top">
245
					<td></td>
246
					<td>
247
						<input type="radio" name="newtime" value="current" />
248
					</td>
249
					<td><?= date(gettext("n/j/y H:i:s"), $config['revision']['time']) ?></td>
250
					<td><?= $config['version'] ?></td>
251
					<td><?= format_bytes(filesize("/conf/config.xml")) ?></td>
252
					<td><?= htmlspecialchars($config['revision']['description']) ?></td>
253
					<td><?=gettext("Current configuration")?></td>
254
				</tr>
255
<?php
256
	// And now for the table of prior backups
257
	$c = 0;
258
	foreach ($confvers as $version):
259
		if ($version['time'] != 0) {
260
			$date = date(gettext("n/j/y H:i:s"), $version['time']);
261
		} else {
262
			$date = gettext("Unknown");
263
		}
264
?>
265
				<tr>
266
					<td>
267
						<input type="radio" name="oldtime" value="<?=$version['time']?>" />
268
					</td>
269
					<td>
270
<?php
271
		if ($c < (count($confvers) - 1)) {
272
?>
273
								<input type="radio" name="newtime" value="<?=$version['time']?>" />
274
<?php
275
		}
276
		$c++;
277
?>
278
					</td>
279
					<td><?= $date ?></td>
280
					<td><?= $version['version'] ?></td>
281
					<td><?= format_bytes($version['filesize']) ?></td>
282
					<td><?= htmlspecialchars($version['description']) ?></td>
283
					<td>
284
						<a class="fa fa-undo"		title="<?=gettext('Revert config')?>"	href="diag_confbak.php?newver=<?=$version['time']?>"	onclick="return confirm('<?=gettext("Are you sure you want to replace the current configuration with this backup?")?>')"></a>
285
						<a class="fa fa-download"	title="<?=gettext('Download config')?>"	href="diag_confbak.php?getcfg=<?=$version['time']?>"></a>
286
						<a class="fa fa-trash"		title="<?=gettext('Delete config')?>"	href="diag_confbak.php?rmver=<?=$version['time']?>"></a>
287
					</td>
288
				</tr>
289
<?php
290
	endforeach;
291
?>
292
				<tr>
293
					<td colspan="2"><input type="submit" name="diff" class="btn btn-info btn-xs" value="<?=gettext("Diff"); ?>" /></td>
294
					<td colspan="5"></td>
295
				</tr>
296
<?php
297
else:
298
	print_info_box(gettext("No backups found."), 'danger');
299
endif;
300
?>
301
			</tbody>
302
		</table>
303
	</form>
304
</div>
305

    
306
<?php include("foot.inc");
(8-8/229)