Project

General

Profile

Download (9.27 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 ($_POST) {
55
	if (!isset($_POST['confirm']) || ($_POST['confirm'] != gettext("Confirm")) || (!isset($_POST['newver']) && !isset($_POST['rmver']))) {
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
	if($_POST['newver'] != "") {
63
		if(config_restore($g['conf_path'] . '/backup/config-' . $_POST['newver'] . '.xml') == 0)
64
		$savemsg = sprintf(gettext('Successfully reverted to timestamp %1$s with description "%2$s".'), date(gettext("n/j/y H:i:s"), $_POST['newver']), $confvers[$_POST['newver']]['description']);
65
		else
66
			$savemsg = gettext("Unable to revert to the selected configuration.");
67
	}
68
	if($_POST['rmver'] != "") {
69
		unlink_if_exists($g['conf_path'] . '/backup/config-' . $_POST['rmver'] . '.xml');
70
		$savemsg = sprintf(gettext('Deleted backup with timestamp %1$s and description "%2$s".'), date(gettext("n/j/y H:i:s"), $_POST['rmver']),$confvers[$_POST['rmver']]['description']);
71
	}
72
	conf_mount_ro();
73
}
74

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

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

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

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

    
106
cleanup_backupcache(false);
107
$confvers = get_backups();
108
unset($confvers['versions']);
109

    
110
$pgtitle = array(gettext("Diagnostics"),gettext("Configuration History"));
111
include("head.inc");
112

    
113
if($savemsg)
114
	print_info_box($savemsg);
115
?>
116
	<?php if ($diff):?>
117
		<h3><?=gettext("Configuration diff from")?><?=date(gettext("n/j/y H:i:s"), $oldtime)?><?=gettext("to")?><?=date(gettext("n/j/y H:i:s"), $newtime)?></h3>
118
		<pre><?php foreach ($diff as $line) {
119
			switch (substr($line, 0, 1)) {
120
				case "+":
121
					$color = "#caffd3";
122
					break;
123
				case "-":
124
					$color = "#ffe8e8";
125
					break;
126
				case "@":
127
					$color = "#a0a0a0";
128
					break;
129
				default:
130
					$color = "#ffffff";
131
			}
132

    
133
			print '<span style="background-color: '.$color .'">'. htmlentities($line) .'</span><br/>';
134
		}
135
		?></pre>
136
<?php endif?>
137
<?PHP if ($_GET["newver"] || $_GET["rmver"]):?>
138
	<h2><?=gettext("Confirm Action")?></h2>
139
	<form action="diag_confbak.php" method="post">
140
		<div class="alert alert-danger">
141
			<p><?=gettext("Please confirm you wish to ")?>
142
			<?PHP
143
				if (!empty($_GET["newver"])) {
144
					echo gettext("restore from Configuration Backup");
145
					$target_config = $_GET["newver"]?>
146
				<input type="hidden" name="newver" value="<?PHP echo htmlspecialchars($_GET["newver"])?>" />
147
			<?PHP
148
				} elseif (!empty($_GET["rmver"])) {
149
					echo gettext("remove Configuration Backup");
150
					$target_config = $_GET["rmver"]?>
151
				<input type="hidden" name="rmver" value="<?PHP echo htmlspecialchars($_GET["rmver"])?>" />
152
			<?PHP
153
				} ?>
154
				<?PHP echo gettext("revert to configuration from ")?> <?=date(gettext("n/j/y H:i:s"), $target_config)?>
155
				<br />
156
				<input type="submit" name="confirm" value="<?PHP echo gettext("Confirm")?>" />
157
			</p>
158
		</div>
159
	</form>
160
<?PHP else:?>
161
<?php
162
	$tab_array = array();
163
	$tab_array[0] = array(gettext("Config History"), true, "diag_confbak.php");
164
	$tab_array[1] = array(gettext("Backup/Restore"), false, "diag_backup.php");
165
	display_top_tabs($tab_array);
166
?>
167
		<form action="diag_confbak.php" method="post">
168
			<div class="form-group">
169
				<label for="backupcount" class="col-sm-2 control-label"><?=gettext("Backup Count")?></label>
170
				<div class="col-sm-10">
171
					<input name="backupcount" type="number" class="form-control" size="5" value="<?=htmlspecialchars($config['system']['backupcount'])?>" />
172
					<?=gettext("Maximum number of old configurations to keep. By default this is 30 for a full install or 5 on NanoBSD.")?>
173
				</div>
174
			</div>
175

    
176
			<div class="form-group">
177
				<div class="col-sm-offset-2 col-sm-10">
178
					<input name="Submit" type="submit" class="btn btn-primary" value="<?=gettext("Save")?>" />
179
					<p><?=gettext("Current space used by backups: ")?><?=exec("/usr/bin/du -sh /conf/backup | /usr/bin/awk '{print $1;}'")?></p>
180
				</div>
181
			</div>
182
		</form>
183
<?php if (!is_array($confvers)): ?>
184
	<?php print_info_box(gettext("No backups found."))?>
185
<?php else: ?>
186
	<form action="diag_confbak.php" method="get">
187
	<table class="table table-striped">
188
	<thead>
189
		<tr>
190
			<th><input type="submit" name="diff" class="btn btn-default" value="<?=gettext("Diff")?>" /></th>
191
			<th><?=gettext("Date")?></th>
192
			<th><?=gettext("Version")?></th>
193
			<th><?=gettext("Size")?></th>
194
			<th><?=gettext("Configuration Change")?></th>
195
			<th></th>
196
		</tr>
197
		</thead>
198

    
199
		<tbody>
200
		<tr>
201
			<td>
202
				<input type="radio" name="oldtime" disabled="disabled" />
203
				<input type="radio" name="newtime" value="current" <?=($_GET['newtime']==$version['time'] ? ' checked="checked"' : '')?>/>
204
			</td>
205
			<td><?=date(gettext("n/j/y H:i:s"), $config['revision']['time'])?></td>
206
			<td><?=$config['version']?></td>
207
			<td><?=format_bytes(filesize("/conf/config.xml"))?></td>
208
			<td><?=$config['revision']['description']?></td>
209
			<td><i><?=gettext("Current")?></i></td>
210
		</tr>
211
		<?php
212
			foreach($confvers as $version):
213
				if($version['time'] != 0)
214
					$date = date(gettext("n/j/y H:i:s"), $version['time']);
215
				else
216
					$date = gettext("Unknown");
217
		?>
218
		<tr>
219
			<td>
220
				<input type="radio" name="oldtime" value="<?=$version['time']?>" <?=($_GET['oldtime']==$version['time'] ? ' checked="checked"' : '')?> />
221
				<input type="radio" name="newtime" value="<?=$version['time']?>" <?=($_GET['newtime']==$version['time'] ? ' checked="checked"' : '')?><?=($version == end($confvers))? 'disabled="disabled"' : ''?> />
222
			</td>
223
			<td><?=$date?></td>
224
			<td><?=$version['version']?></td>
225
			<td><?=format_bytes($version['filesize'])?></td>
226
			<td><?=$version['description']?></td>
227
			<td>
228
				<a href="diag_confbak.php?newver=<?=$version['time']?>" title="<?=gettext("Revert to this configuration")?>">
229
					<i class="icon icon-retweet"></i>
230
				</a>
231
				<a href="diag_confbak.php?rmver=<?=$version['time']?>" title="<?=gettext("Remove this backup")?>">
232
					<i class="icon icon-remove"></i>
233
				</a>
234
				<a href="diag_confbak.php?getcfg=<?=$version['time']?>" title="<?=gettext("Download this backup")?>">
235
					<i class="icon icon-download"></i>
236
				</a>
237
			</td>
238
		</tr>
239
		<?php endforeach?>
240
		</tbody>
241
		<tfoot>
242
		<tr>
243
			<td colspan="6"><input type="submit" name="diff" class="btn btn-default" value="<?=gettext("Compare selected")?>" /></td>
244
		</tr>
245
	<?php endif; ?>
246
<?php endif?>
247
	</table>
248
	</form>
249
</div>
250
<?php include("foot.inc")?>
(8-8/252)