summaryrefslogtreecommitdiffstats
path: root/src/usr/local/www/diag_confbak.php
blob: bc9cc517561cf25a0e3c692c8d7a2b5439952bb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<?php
/* $Id$ */
/*
	diag_confbak.php
	Copyright (C) 2005 Colin Smith
	Copyright (C) 2010 Jim Pingle
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	1. Redistributions of source code must retain the above copyright notice,
	   this list of conditions and the following disclaimer.

	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.

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

/*
	pfSense_MODULE:	config
*/

##|+PRIV
##|*IDENT=page-diagnostics-configurationhistory
##|*NAME=Diagnostics: Configuration History page
##|*DESCR=Allow access to the 'Diagnostics: Configuration History' page.
##|*MATCH=diag_confbak.php*
##|-PRIV

require("guiconfig.inc");

if (isset($_POST['backupcount'])) {
	if (is_numeric($_POST['backupcount']) && ($_POST['backupcount'] >= 0)) {
		$config['system']['backupcount'] = $_POST['backupcount'];
		$changedescr = $config['system']['backupcount'];
	} else {
		unset($config['system']['backupcount']);
		$changedescr = "(platform default)";
	}
	write_config("Changed backup revision count to {$changedescr}");
} elseif ($_POST) {
	if (!isset($_POST['confirm']) || ($_POST['confirm'] != gettext("Confirm")) || (!isset($_POST['newver']) && !isset($_POST['rmver']))) {
		header("Location: diag_confbak.php");
		return;
	}

	conf_mount_rw();
	$confvers = unserialize(file_get_contents($g['cf_conf_path'] . '/backup/backup.cache'));
	if ($_POST['newver'] != "") {
		if (config_restore($g['conf_path'] . '/backup/config-' . $_POST['newver'] . '.xml') == 0) {
			$savemsg = sprintf(gettext('Successfully reverted to timestamp %1$s with description "%2$s".'), date(gettext("n/j/y H:i:s"), $_POST['newver']), htmlspecialchars($confvers[$_POST['newver']]['description']));
		} else {
			$savemsg = gettext("Unable to revert to the selected configuration.");
		}
	}
	if ($_POST['rmver'] != "") {
		unlink_if_exists($g['conf_path'] . '/backup/config-' . $_POST['rmver'] . '.xml');
		$savemsg = sprintf(gettext('Deleted backup with timestamp %1$s and description "%2$s".'), date(gettext("n/j/y H:i:s"), $_POST['rmver']), htmlspecialchars($confvers[$_POST['rmver']]['description']));
	}
	conf_mount_ro();
}

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

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

	header("Content-Type: application/octet-stream");
	header("Content-Disposition: attachment; filename={$exp_name}");
	header("Content-Length: $exp_size");
	echo $exp_data;
	exit;
}

if (($_GET['diff'] == 'Diff') && isset($_GET['oldtime']) && isset($_GET['newtime']) &&
    (is_numeric($_GET['oldtime'])) &&
	(is_numeric($_GET['newtime']) || ($_GET['newtime'] == 'current'))) {
	$diff = "";
	$oldfile = $g['conf_path'] . '/backup/config-' . $_GET['oldtime'] . '.xml';
	$oldtime = $_GET['oldtime'];
	if ($_GET['newtime'] == 'current') {
		$newfile = $g['conf_path'] . '/config.xml';
		$newtime = $config['revision']['time'];
	} else {
		$newfile = $g['conf_path'] . '/backup/config-' . $_GET['newtime'] . '.xml';
		$newtime = $_GET['newtime'];
	}
	if (file_exists($oldfile) && file_exists($newfile)) {
		exec("/usr/bin/diff -u " . escapeshellarg($oldfile) . " " . escapeshellarg($newfile), $diff);
	}
}

cleanup_backupcache(false);
$confvers = get_backups();
unset($confvers['versions']);

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

if($savemsg)
	print_info_box($savemsg);
?>
	<?php if ($diff):?>
		<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>
		<pre><?php foreach ($diff as $line) {
			switch (substr($line, 0, 1)) {
				case "+":
					$color = "#caffd3";
					break;
				case "-":
					$color = "#ffe8e8";
					break;
				case "@":
					$color = "#a0a0a0";
					break;
				default:
					$color = "#ffffff";
			}

			print '<span style="background-color: '.$color .'">'. htmlentities($line) .'</span><br/>';
		}
		?></pre>
<?php endif?>
<?PHP if ($_GET["newver"] || $_GET["rmver"]):?>
	<h2><?=gettext("Confirm Action")?></h2>
	<form action="diag_confbak.php" method="post">
		<div class="alert alert-danger">
			<p><?=gettext("Please confirm you wish to ")?>
			<?PHP
				if (!empty($_GET["newver"])) {
					echo gettext("restore from Configuration Backup");
					$target_config = $_GET["newver"]?>
				<input type="hidden" name="newver" value="<?PHP echo htmlspecialchars($_GET["newver"])?>" />
			<?PHP
				} elseif (!empty($_GET["rmver"])) {
					echo gettext("remove Configuration Backup");
					$target_config = $_GET["rmver"]?>
				<input type="hidden" name="rmver" value="<?PHP echo htmlspecialchars($_GET["rmver"])?>" />
			<?PHP
				} ?>
				<?PHP echo gettext("revert to configuration from ")?> <?=date(gettext("n/j/y H:i:s"), $target_config)?>
				<br />
				<input type="submit" name="confirm" value="<?PHP echo gettext("Confirm")?>" />
			</p>
		</div>
	</form>
<?PHP else:?>
<?php
	$tab_array = array();
	$tab_array[0] = array(gettext("Config History"), true, "diag_confbak.php");
	$tab_array[1] = array(gettext("Backup/Restore"), false, "diag_backup.php");
	display_top_tabs($tab_array);
?>
		<form action="diag_confbak.php" method="post">
			<div class="form-group">
				<label for="backupcount" class="col-sm-2 control-label"><?=gettext("Backup Count")?></label>
				<div class="col-sm-10">
					<input name="backupcount" type="number" class="form-control" size="5" value="<?=htmlspecialchars($config['system']['backupcount'])?>" />
					<?=gettext("Maximum number of old configurations to keep. By default this is 30 for a full install or 5 on NanoBSD.")?>
				</div>
			</div>

			<div class="form-group">
				<div class="col-sm-offset-2 col-sm-10">
					<input name="Submit" type="submit" class="btn btn-primary" value="<?=gettext("Save")?>" />
					<p><?=gettext("Current space used by backups: ")?><?=exec("/usr/bin/du -sh /conf/backup | /usr/bin/awk '{print $1;}'")?></p>
				</div>
			</div>
		</form>
<?php if (!is_array($confvers)): ?>
	<?php print_info_box(gettext("No backups found."))?>
<?php else: ?>
	<form action="diag_confbak.php" method="get">
	<div class="table-responsive">
	<table class="table table-striped table-hover">
	<thead>
		<tr>
			<th><input type="submit" name="diff" class="btn btn-default" value="<?=gettext("Diff")?>" /></th>
			<th><?=gettext("Date")?></th>
			<th><?=gettext("Version")?></th>
			<th><?=gettext("Size")?></th>
			<th><?=gettext("Configuration Change")?></th>
			<th></th>
		</tr>
		</thead>

		<tbody>
		<tr>
			<td>
				<input type="radio" name="oldtime" disabled="disabled" />
				<input type="radio" name="newtime" value="current" <?=($_GET['newtime']==$version['time'] ? ' checked="checked"' : '')?>/>
			</td>
			<td><?=date(gettext("n/j/y H:i:s"), $config['revision']['time'])?></td>
			<td><?=$config['version']?></td>
			<td><?=format_bytes(filesize("/conf/config.xml"))?></td>
			<td><?=$config['revision']['description']?></td>
			<td><i><?=gettext("Current")?></i></td>
		</tr>
		<?php
			foreach($confvers as $version):
				if($version['time'] != 0)
					$date = date(gettext("n/j/y H:i:s"), $version['time']);
				else
					$date = gettext("Unknown");
		?>
		<tr>
			<td>
				<div id="mainarea">
					<form action="diag_confbak.php" method="post">
					<table class="tabcont" align="center" width="100%" border="0" cellpadding="6" cellspacing="0" summary="tabcont">

<?php if ($_GET["newver"] || $_GET["rmver"]): ?>
						<tr>
							<td colspan="2" valign="top" class="listtopic"><?php echo gettext("Confirm Action"); ?></td>
						</tr>
						<tr>
							<td width="22%" valign="top" class="vncell">&nbsp;</td>
							<td width="78%" class="vtable">

								<strong><?php echo gettext("Please confirm the selected action"); ?></strong>:
								<br />
								<br /><strong><?php echo gettext("Action"); ?>:</strong>
							<?php if (!empty($_GET["newver"])) {
								echo gettext("Restore from Configuration Backup");
								$target_config = $_GET["newver"]; ?>
								<input type="hidden" name="newver" value="<?php echo htmlspecialchars($_GET["newver"]); ?>" />
							<?php } elseif (!empty($_GET["rmver"])) {
								echo gettext("Remove Configuration Backup");
								$target_config = $_GET["rmver"]; ?>
								<input type="hidden" name="rmver" value="<?php echo htmlspecialchars($_GET["rmver"]); ?>" />
							<?php } ?>
								<br /><strong><?php echo gettext("Target Configuration"); ?>:</strong>
								<?php echo sprintf(gettext('Timestamp %1$s'), date(gettext("n/j/y H:i:s"), $target_config)); ?>
								<br /><input type="submit" name="confirm" value="<?php echo gettext("Confirm"); ?>" />
							</td>
						</tr>
<?php else: ?>

						<tr>
							<td width="10%">&nbsp;</td>
							<td width="15%" valign="top"><?=gettext("Backup Count");?></td>
							<td width="10%">
								<input name="backupcount" type="text" class="formfld unknown" size="5" value="<?=htmlspecialchars($config['system']['backupcount']);?>"/>
							</td>
							<td width="60%">
								<?= gettext("Enter the number of older configurations to keep in the local backup cache. By default this is 30 for a full install or 5 on NanoBSD."); ?>
							</td>
							<td width= "5%"><input name="save" type="submit" class="formbtn" value="<?=gettext("Save"); ?>" /></td>
						</tr>
						<tr>
							<td class="vncell">&nbsp;</td>
							<td colspan="4" class="vncell">
								<?= gettext("NOTE: Be aware of how much space is consumed by backups before adjusting this value. Current space used by backups: "); ?> <?= exec("/usr/bin/du -sh /conf/backup | /usr/bin/awk '{print $1;}'") ?>
							</td>
						</tr>
					</table>
					</form>
					<form action="diag_confbak.php" method="get">
					<table class="tabcont" align="center" width="100%" border="0" cellpadding="6" cellspacing="0" summary="difference">
						<?php if (is_array($confvers)): ?>
						<tr>
							<td colspan="7" class="list">
								<?= gettext("To view the differences between an older configuration and a newer configuration, select the older configuration using the left column of radio options and select the newer configuration in the right column, then press the Diff button."); ?>
								<br /><br />
							</td>
						</tr>
						<tr>
							<td width="5%" colspan="2" valign="middle" align="center" class="list nowrap"><input type="submit" name="diff" value="<?=gettext("Diff"); ?>" /></td>
							<td width="20%" class="listhdrr"><?=gettext("Date");?></td>
							<td width="5%" class="listhdrr"><?=gettext("Version");?></td>
							<td width="5%" class="listhdrr"><?=gettext("Size");?></td>
							<td width="60%" class="listhdrr"><?=gettext("Configuration Change");?></td>
							<td width="5%" class="list">&nbsp;</td>
						</tr>
						<tr valign="top">
							<td valign="middle" class="list nowrap"></td>
							<td class="list">
								<input type="radio" name="newtime" value="current" />
							</td>
							<td class="listlr"> <?= date(gettext("n/j/y H:i:s"), $config['revision']['time']) ?></td>
							<td class="listr"> <?= $config['version'] ?></td>
							<td class="listr"> <?= format_bytes(filesize("/conf/config.xml")) ?></td>
							<td class="listr"> <?= htmlspecialchars($config['revision']['description']) ?></td>
							<td valign="middle" class="list nowrap"><b><?=gettext("Current");?></b></td>
						</tr>
						<?php
							$c = 0;
							foreach ($confvers as $version):
								if ($version['time'] != 0) {
									$date = date(gettext("n/j/y H:i:s"), $version['time']);
								} else {
									$date = gettext("Unknown");
								}
						?>
						<tr valign="top">
							<td class="list">
								<input type="radio" name="oldtime" value="<?php echo $version['time'];?>" />
							</td>
							<td class="list">
								<?php if ($c < (count($confvers) - 1)) { ?>
								<input type="radio" name="newtime" value="<?php echo $version['time'];?>" />
								<?php } else { ?>
								&nbsp;
								<?php }
								$c++; ?>
							</td>
							<td class="listlr"> <?= $date ?></td>
							<td class="listr"> <?= $version['version'] ?></td>
							<td class="listr"> <?= format_bytes($version['filesize']) ?></td>
							<td class="listr"> <?= htmlspecialchars($version['description']) ?></td>
							<td valign="middle" class="list nowrap">
								<a href="diag_confbak.php?newver=<?=$version['time'];?>">
									<img src="/themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" width="17" height="17" border="0" alt="<?=gettext("Revert to this configuration");?>" title="<?=gettext("Revert to this configuration");?>" />
								</a>
								<a href="diag_confbak.php?rmver=<?=$version['time'];?>">
									<img src="/themes/<?= $g['theme']; ?>/images/icons/icon_x.gif" width="17" height="17" border="0" alt="<?=gettext("Remove this backup");?>" title="<?=gettext("Remove this backup");?>" />
								</a>
								<a href="diag_confbak.php?getcfg=<?=$version['time'];?>">
									<img src="/themes/<?= $g['theme']; ?>/images/icons/icon_down.gif" width="17" height="17" border="0" alt="<?=gettext("Download this backup");?>" title="<?=gettext("Download this backup");?>" />
								</a>
							</td>
						</tr>
						<?php endforeach; ?>
						<tr>
							<td colspan="2"><input type="submit" name="diff" value="<?=gettext("Diff"); ?>" /></td>
							<td colspan="5"></td>
						</tr>
						<?php else: ?>
						<tr>
							<td>
								<?php print_info_box(gettext("No backups found.")); ?>
							</td>
						</tr>
						<?php endif; ?>
<?php endif; ?>
					</table>
					</form>
				</div>
			</td>
		</tr>
		<?php endforeach?>
		</tbody>
		<tfoot>
		<tr>
			<td colspan="6"><input type="submit" name="diff" class="btn btn-default" value="<?=gettext("Compare selected")?>" /></td>
		</tr>
	<?php endif; ?>
<?php endif?>
	</table>
	</div>
	</form>
<?php include("foot.inc")?>
OpenPOWER on IntegriCloud