Project

General

Profile

Download (9.02 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	diag_confbak.php
5
	Copyright (C) 2005 Colin Smith
6
	Copyright (C) 2010 Jim Pingle
7
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
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 the
18
	   documentation and/or other materials provided with the distribution.
19

    
20
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
	POSSIBILITY OF SUCH DAMAGE.
30
*/
31

    
32
/*
33
	pfSense_MODULE:	config
34
*/
35

    
36
##|+PRIV
37
##|*IDENT=page-diagnostics-configurationhistory
38
##|*NAME=Diagnostics: Configuration History page
39
##|*DESCR=Allow access to the 'Diagnostics: Configuration History' page.
40
##|*MATCH=diag_confbak.php*
41
##|-PRIV
42

    
43
require("guiconfig.inc");
44

    
45
if (isset($_POST['backupcount'])) {
46
	if (is_numeric($_POST['backupcount']) && ($_POST['backupcount'] >= 0)) {
47
		$config['system']['backupcount'] = $_POST['backupcount'];
48
		$changedescr = $config['system']['backupcount'];
49
	} else {
50
		unset($config['system']['backupcount']);
51
		$changedescr = "(platform default)";
52
	}
53
	write_config("Changed backup revision count to {$changedescr}");
54
} elseif ($_GET) {
55
	if (!isset($_GET['newver']) && !isset($_GET['rmver']) && !isset($_GET['getcfg']) && !isset($_GET['diff'])) {
56
		header("Location: diag_confbak.php");
57
		return;
58
	}
59

    
60
	conf_mount_rw();
61
	$confvers = unserialize(file_get_contents($g['cf_conf_path'] . '/backup/backup.cache'));
62
	
63
	if ($_GET['newver'] != "") {
64
		if (config_restore($g['conf_path'] . '/backup/config-' . $_GET['newver'] . '.xml') == 0) {
65
			$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']));
66
		} else {
67
			$savemsg = gettext("Unable to revert to the selected configuration.");
68
		}
69
	}
70
	if ($_GET['rmver'] != "") {
71
		unlink_if_exists($g['conf_path'] . '/backup/config-' . $_GET['rmver'] . '.xml');
72
		$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']));
73
	}
74
	conf_mount_ro();
75
}
76

    
77
if ($_GET['getcfg'] != "") {
78
	$_GET['getcfg'] = basename($_GET['getcfg']);
79
	$file = $g['conf_path'] . '/backup/config-' . $_GET['getcfg'] . '.xml';
80

    
81
	$exp_name = urlencode("config-{$config['system']['hostname']}.{$config['system']['domain']}-{$_GET['getcfg']}.xml");
82
	$exp_data = file_get_contents($file);
83
	$exp_size = strlen($exp_data);
84

    
85
	header("Content-Type: application/octet-stream");
86
	header("Content-Disposition: attachment; filename={$exp_name}");
87
	header("Content-Length: $exp_size");
88
	echo $exp_data;
89
	exit;
90
}
91

    
92
if (($_GET['diff'] == 'Diff') && isset($_GET['oldtime']) && isset($_GET['newtime']) &&
93
    (is_numeric($_GET['oldtime'])) &&
94
    (is_numeric($_GET['newtime']) || ($_GET['newtime'] == 'current'))) {
95
	$diff = "";
96
	$oldfile = $g['conf_path'] . '/backup/config-' . $_GET['oldtime'] . '.xml';
97
	$oldtime = $_GET['oldtime'];
98
	if ($_GET['newtime'] == 'current') {
99
		$newfile = $g['conf_path'] . '/config.xml';
100
		$newtime = $config['revision']['time'];
101
	} else {
102
		$newfile = $g['conf_path'] . '/backup/config-' . $_GET['newtime'] . '.xml';
103
		$newtime = $_GET['newtime'];
104
	}
105
	if (file_exists($oldfile) && file_exists($newfile)) {
106
		exec("/usr/bin/diff -u " . escapeshellarg($oldfile) . " " . escapeshellarg($newfile), $diff);
107
	}
108
}
109

    
110
cleanup_backupcache(false);
111
$confvers = get_backups();
112
unset($confvers['versions']);
113

    
114
$pgtitle = array(gettext("Diagnostics"), gettext("Configuration History"));
115
include("head.inc");
116

    
117
if ($savemsg) {
118
	print_info_box($savemsg, 'success');
119
}
120

    
121
if ($diff) {
122
?>
123
<div class="panel panel-default">
124
	<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>
125
	<div class="panel-body table-responsive">
126
	<!-- This table is left un-bootstrapped to maintain the original diff format output -->
127
		<table style="padding-top: 4px; padding-bottom: 4px; vertical-align:middle;">
128

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

    
157
$tab_array = array();
158
$tab_array[] = array(gettext("Config History"), true, "diag_confbak.php");
159
$tab_array[] = array(gettext("Backup/Restore"), false, "diag_backup.php");
160
display_top_tabs($tab_array);
161

    
162
require('classes/Form.class.php');
163

    
164
$form = new Form(new Form_Button(
165
	'Submit',
166
	gettext("Save")
167
));
168

    
169
$section = new Form_Section('Saved Configurations');
170

    
171
$section->addInput(new Form_Input(
172
	'backupcount',
173
	'Backup Count',
174
	'number',
175
	$config['system']['backupcount']
176
))->setHelp('Maximum number of old configurations to keep. By default this is 30 for a full install or 5 on NanoBSD. ');
177

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

    
180
$section->addInput(new Form_StaticText(
181
	'Current space used by backups',
182
	$space
183
));
184

    
185
$form->add($section);
186

    
187
print($form);
188

    
189
if (is_array($confvers)) {
190
	print_info_box(gettext('To view the differences between an older configuration and a newer configuration, ' .
191
						   'select the older configuration using the left column of radio options and select the newer configuration in the right column, ' .
192
						   'then press the "Diff" button.'));
193
}						   
194
?>					
195
					
196
<form action="diag_confbak.php" method="get">
197
	<div class="table-resposive">
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
						<input type="submit" name="diff" class="btn btn-info btn-xs" value="<?=gettext("Diff"); ?>" />
206
					</th>
207
					<th><?=gettext("Date")?></th>
208
					<th><?=gettext("Version")?></th>
209
					<th><?=gettext("Size")?></th>
210
					<th><?=gettext("Configuration Change")?></th>
211
					<th><?=gettext("Actions")?></th>
212
				</tr>
213
			</thead>
214
			<tbody>
215
				<!-- First row is the current configuration -->
216
				<tr valign="top">
217
					<td></td>
218
					<td>
219
						<input type="radio" name="newtime" value="current" />
220
					</td>
221
					<td><?= date(gettext("n/j/y H:i:s"), $config['revision']['time']) ?></td>
222
					<td><?= $config['version'] ?></td>
223
					<td><?= format_bytes(filesize("/conf/config.xml")) ?></td>
224
					<td><?= htmlspecialchars($config['revision']['description']) ?></td>
225
					<td><?=gettext("Current configuration")?></td>
226
				</tr>
227
<?php
228
	// And now for hte table of prior backups
229
	$c = 0;
230
	foreach ($confvers as $version):
231
		if ($version['time'] != 0) {
232
			$date = date(gettext("n/j/y H:i:s"), $version['time']);
233
		} else {
234
			$date = gettext("Unknown");
235
		}
236
?>
237
				<tr>
238
					<td>
239
						<input type="radio" name="oldtime" value="<?=$version['time']?>" />
240
					</td>
241
					<td>
242
<?php
243
		if ($c < (count($confvers) - 1)) {
244
?>
245
								<input type="radio" name="newtime" value="<?=$version['time']?>" />
246
<?php 
247
		}
248
		$c++; 
249
?>
250
					</td>
251
					<td><?= $date ?></td>
252
					<td><?= $version['version'] ?></td>
253
					<td><?= format_bytes($version['filesize']) ?></td>
254
					<td><?= htmlspecialchars($version['description']) ?></td>
255
					<td>
256
						<a href="diag_confbak.php?newver=<?=$version['time']?>" class="btn btn-xs btn-success" 
257
							onclick="return confirm('<?=gettext("Are you sure you want to replace the current configuration with this backup?")?>')">
258
							<?=gettext("Revert")?>
259
						</a>
260
						<a href="diag_confbak.php?rmver=<?=$version['time']?>" class="btn btn-xs btn-danger">
261
							<?=gettext("Delete")?>
262
						</a>
263
						<a href="diag_confbak.php?getcfg=<?=$version['time']?>" class="btn btn-xs btn-default">
264
							<?=gettext("Download")?>
265
						</a>
266
					</td>
267
				</tr>
268
<?php 
269
	endforeach;
270
?>
271
				<tr>
272
					<td colspan="2"><input type="submit" name="diff" class="btn btn-info btn-xs" value="<?=gettext("Diff"); ?>" /></td>
273
					<td colspan="5"></td>
274
				</tr>
275
<?php 
276
else:
277
	print_info_box(gettext("No backups found."), 'danger');
278
endif;
279
?>
280
			</tbody>
281
		</table>
282
	</form>
283
</div>
284

    
285
<?php include("foot.inc");
(9-9/235)