Commit f10e469b authored by Ad Schellevis's avatar Ad Schellevis

(legacy) refactor diag_confbak.php

parent 6836ba36
...@@ -30,47 +30,10 @@ ...@@ -30,47 +30,10 @@
require_once("guiconfig.inc"); require_once("guiconfig.inc");
$cnf = OPNsense\Core\Config::getInstance(); if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$confvers = $cnf->getBackups(true); $cnf = OPNsense\Core\Config::getInstance();
if (isset($_POST['backupcount'])) {
if (is_numeric($_POST['backupcount']) && ($_POST['backupcount'] >= 0)) {
$config['system']['backupcount'] = $_POST['backupcount'];
} else {
unset($config['system']['backupcount']);
}
write_config(gettext('Changed backup revision count.'));
} elseif (isset($_POST['newver']) && $_POST['newver'] != '') {
foreach ($confvers as $filename => $revision) {
if (isset($revision['time']) && $revision['time'] == $_POST['newver']) {
if (config_restore($filename)== 0) {
$savemsg = sprintf(gettext('Successfully reverted to timestamp %s with description "%s".'), date(gettext("n/j/y H:i:s"), $_POST['newver']), $revision['description']);
} else {
$savemsg = gettext("Unable to revert to the selected configuration.");
}
break;
}
}
} elseif (isset($_POST['rmver']) && $_POST['rmver'] != '') {
foreach ($confvers as $filename => $revision) {
if (isset($revision['time']) && $revision['time'] == $_POST['rmver']) {
if (file_exists($filename)) {
@unlink($filename);
$savemsg = sprintf(gettext('Deleted backup with timestamp %s and description "%s".'), date(gettext("n/j/y H:i:s"), $_POST['rmver']),$revision['description']);
} else {
$savemsg = gettext("Unable to delete the selected configuration.");
}
break;
}
}
}
if (isset($_POST)) {
/* things might have changed */
$confvers = $cnf->getBackups(true); $confvers = $cnf->getBackups(true);
} if (!empty($_GET['getcfg'])) {
if (isset($_GET['getcfg']) && $_GET['getcfg'] != '') {
foreach ($confvers as $filename => $revision) { foreach ($confvers as $filename => $revision) {
if ($revision['time'] == $_GET['getcfg']) { if ($revision['time'] == $_GET['getcfg']) {
$exp_name = urlencode("config-{$config['system']['hostname']}.{$config['system']['domain']}-{$_GET['getcfg']}.xml"); $exp_name = urlencode("config-{$config['system']['hostname']}.{$config['system']['domain']}-{$_GET['getcfg']}.xml");
...@@ -84,17 +47,7 @@ if (isset($_GET['getcfg']) && $_GET['getcfg'] != '') { ...@@ -84,17 +47,7 @@ if (isset($_GET['getcfg']) && $_GET['getcfg'] != '') {
exit; exit;
} }
} }
} } elseif (!empty($_GET['diff']) && isset($_GET['oldtime']) && isset($_GET['newtime'])
$newcheck = 'current';
$oldcheck = '';
foreach ($confvers as $revision) {
/* grab first entry if any */
$oldcheck = $revision['time'];
break;
}
if (($_GET['diff'] == 'Diff') && isset($_GET['oldtime']) && isset($_GET['newtime'])
&& is_numeric($_GET['oldtime']) && (is_numeric($_GET['newtime']) || ($_GET['newtime'] == 'current'))) { && is_numeric($_GET['oldtime']) && (is_numeric($_GET['newtime']) || ($_GET['newtime'] == 'current'))) {
$oldfile = ''; $oldfile = '';
$newfile = ''; $newfile = '';
...@@ -116,7 +69,6 @@ if (($_GET['diff'] == 'Diff') && isset($_GET['oldtime']) && isset($_GET['newtime ...@@ -116,7 +69,6 @@ if (($_GET['diff'] == 'Diff') && isset($_GET['oldtime']) && isset($_GET['newtime
if ($_GET['newtime'] == 'current') { if ($_GET['newtime'] == 'current') {
$newfile = '/conf/config.xml'; $newfile = '/conf/config.xml';
$newtime = $config['revision']['time']; $newtime = $config['revision']['time'];
$newcheck = 'current';
} else { } else {
$newtime = $_GET['newtime']; $newtime = $_GET['newtime'];
$newcheck = $newtime; $newcheck = $newtime;
...@@ -125,12 +77,102 @@ if (($_GET['diff'] == 'Diff') && isset($_GET['oldtime']) && isset($_GET['newtime ...@@ -125,12 +77,102 @@ if (($_GET['diff'] == 'Diff') && isset($_GET['oldtime']) && isset($_GET['newtime
if (file_exists($oldfile) && file_exists($newfile)) { if (file_exists($oldfile) && file_exists($newfile)) {
exec("/usr/bin/diff -u " . escapeshellarg($oldfile) . " " . escapeshellarg($newfile), $diff); exec("/usr/bin/diff -u " . escapeshellarg($oldfile) . " " . escapeshellarg($newfile), $diff);
} }
}
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
$cnf = OPNsense\Core\Config::getInstance();
$confvers = $cnf->getBackups(true);
if (!empty($_POST['backupcount'])) {
if (is_numeric($_POST['backupcount']) && ($_POST['backupcount'] >= 0)) {
$config['system']['backupcount'] = $_POST['backupcount'];
} else {
unset($config['system']['backupcount']);
}
write_config(gettext('Changed backup revision count.'));
} elseif (!empty($_POST['act']) && $_POST['act'] == "revert") {
foreach ($confvers as $filename => $revision) {
if (isset($revision['time']) && $revision['time'] == $_POST['time']) {
if (config_restore($filename)== 0) {
$savemsg = sprintf(gettext('Successfully reverted to timestamp %s with description "%s".'), date(gettext("n/j/y H:i:s"), $_POST['id']), $revision['description']);
} else {
$savemsg = gettext("Unable to revert to the selected configuration.");
}
break;
}
}
} elseif (!empty($_POST['act']) && $_POST['act'] == "delete") {
foreach ($confvers as $filename => $revision) {
if (isset($revision['time']) && $revision['time'] == $_POST['time']) {
if (file_exists($filename)) {
@unlink($filename);
$savemsg = sprintf(gettext('Deleted backup with timestamp %s and description "%s".'), date(gettext("n/j/y H:i:s"), $revision['time']),$revision['description']);
} else {
$savemsg = gettext("Unable to delete the selected configuration.");
}
unset($confvers[$filename]);
break;
}
}
}
} }
include("head.inc");
include("head.inc");
?> ?>
<script type="text/javascript">
//<![CDATA[
$( document ).ready(function() {
// revert config dialog
$(".act_revert").click(function(){
var id = $(this).data('id');
BootstrapDialog.show({
type:BootstrapDialog.TYPE_INFO,
title: "<?= gettext("Action");?>",
message: "<?=gettext("Restore from Configuration Backup");?> <br/> <?=gettext('Version');?>: " + id,
buttons: [{
label: "<?= gettext("No");?>",
action: function(dialogRef) {
dialogRef.close();
}}, {
label: "<?= gettext("Yes");?>",
action: function(dialogRef) {
$("#time").val(id);
$("#action").val("revert");
$("#iform").submit()
}
}]
});
});
// delete backup dialog
$(".act_delete").click(function(){
var id = $(this).data('id');
BootstrapDialog.show({
type:BootstrapDialog.TYPE_INFO,
title: "<?= gettext("Action");?>",
message: "<?=gettext("Remove Configuration Backup");?> <br/> <?=gettext('Version');?>: " + id,
buttons: [{
label: "<?= gettext("No");?>",
action: function(dialogRef) {
dialogRef.close();
}}, {
label: "<?= gettext("Yes");?>",
action: function(dialogRef) {
$("#time").val(id);
$("#action").val("delete");
$("#iform").submit()
}
}]
});
});
});
//]]>
</script>
<body> <body>
<?php <?php
include("fbegin.inc"); include("fbegin.inc");
...@@ -141,66 +183,35 @@ include("head.inc"); ...@@ -141,66 +183,35 @@ include("head.inc");
<section class="page-content-main"> <section class="page-content-main">
<div class="container-fluid"> <div class="container-fluid">
<div class="row"> <div class="row">
<section class="col-xs-12"> <section class="col-xs-12">
<div class="container-fluid tab-content"> <div class="container-fluid">
<div class="tab-pane active" id="system"> <form method="post" id="iform">
<?php if ($_GET["newver"] || $_GET["rmver"]): ?> <input type="hidden" id="time" name="time" value="" />
<form action="<?=explode("?", $_SERVER['REQUEST_URI'])[0];?>" method="post"> <input type="hidden" id="action" name="act" value="" />
<section>
<div class="content-box">
<header class="content-box-head container-fluid">
<h3><?= gettext('Confirm Action') ?></h3>
</header>
<div class="content-box-main col-xs-12">
<strong><?= gettext('Please confirm the selected action') ?></strong>:
<br />
<br /><strong><?= gettext('Action') ?>:</strong>
<?php if (!empty($_GET["newver"])) {
echo gettext("Restore from Configuration Backup");
$target_config = $_GET["newver"]; ?>
<input type="hidden" name="newver" value="<?= htmlspecialchars($_GET['newver']) ?>" />
<?php } elseif (!empty($_GET["rmver"])) {
echo gettext("Remove Configuration Backup");
$target_config = $_GET["rmver"]; ?>
<input type="hidden" name="rmver" value="<?= htmlspecialchars($_GET['rmver']) ?>" />
<?php } ?>
<br /><strong><?= gettext('Target Configuration') ?>:</strong>
<?= sprintf(gettext('Timestamp %s'), date(gettext('n/j/y H:i:s'), $target_config)) ?>
<br /><input type="submit" name="confirm" class="btn btn-primary" value="<?= gettext('Confirm') ?>" />
</div>
</div>
</section>
</form>
<?php else: ?>
<form action="<?=$_SERVER['REQUEST_URI'];?>" method="post">
<section style="margin-bottom:15px;"> <section style="margin-bottom:15px;">
<div class="content-box"> <div class="content-box">
<header class="content-box-head container-fluid">
<h3><?=gettext('Settings');?></h3>
</header>
<div class="content-box-main"> <div class="content-box-main">
<div class="table-responsive"> <div class="table-responsive">
<table class="table table-striped"> <table class="table table-striped">
<thead>
<tr>
<th><?=gettext("Backup Count");?></th>
<th></th>
</tr>
</thead>
<tbody> <tbody>
<tr> <tr>
<td><?=gettext("Backup Count");?></td>
<td><input name="backupcount" type="text" class="formfld unknown" size="5" value="<?=htmlspecialchars($config['system']['backupcount']);?>"/></td> <td><input name="backupcount" type="text" class="formfld unknown" size="5" value="<?=htmlspecialchars($config['system']['backupcount']);?>"/></td>
<td><?= gettext("Enter the number of older configurations to keep in the local backup cache. By default this is 30."); ?></td> <td><?= gettext("Enter the number of older configurations to keep in the local backup cache. By default this is 30."); ?></td>
<td><input name="save" type="submit" class="btn btn-primary" value="<?=gettext("Save"); ?>" /></td> </tr>
<tr>
<td>
<input name="save" type="submit" class="btn btn-primary" value="<?=gettext("Save"); ?>" />
</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>
<hr/>
<div class="container-fluid"> <div class="container-fluid">
<?= 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;}'") ?> <?= 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;}'") ?>
</div> </div>
...@@ -209,21 +220,18 @@ include("head.inc"); ...@@ -209,21 +220,18 @@ include("head.inc");
</div> </div>
</section> </section>
</form> </form>
<?php if ($diff): ?> <?php if ($diff): ?>
<section style="margin-bottom:15px;"> <section style="margin-bottom:15px;">
<div class="content-box"> <div class="content-box">
<header class="content-box-head container-fluid"> <header class="content-box-head container-fluid">
<h3><?=gettext("Configuration diff from");?> <?php echo date(gettext("n/j/y H:i:s"), $oldtime); ?> <?=gettext("to");?> <?php echo date(gettext("n/j/y H:i:s"), $newtime); ?></h3> <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>
</header> </header>
<div class="content-box-main"> <div class="content-box-main">
<div class="container-fluid __mb"> <div class="container-fluid __mb">
<div class="table-responsive"> <div class="table-responsive" style="overflow: scroll;">
<table class="table table-condensed table-striped">
<table summary="Differences"> <?php
<tr><td></td></tr> foreach ($diff as $line):
<?php foreach ($diff as $line) {
switch (substr($line, 0, 1)) { switch (substr($line, 0, 1)) {
case '+': case '+':
$color = '#3bbb33'; $color = '#3bbb33';
...@@ -239,9 +247,10 @@ include("head.inc"); ...@@ -239,9 +247,10 @@ include("head.inc");
} }
?> ?>
<tr> <tr>
<td valign="middle" style="color: <?=$color;?>; white-space: pre-wrap; font-family: monospace;"><?php echo htmlentities($line);?></td> <td style="color: <?=$color;?>; white-space: pre-wrap; font-family: monospace;"><?=htmlentities($line);?></td>
</tr> </tr>
<?php } ?> <?php
endforeach;?>
</table> </table>
</div> </div>
</div> </div>
...@@ -249,87 +258,88 @@ include("head.inc"); ...@@ -249,87 +258,88 @@ include("head.inc");
</div> </div>
</section> </section>
<?php endif; ?> <?php endif; ?>
<form method="get">
<?php if (is_array($confvers)): ?>
<form action="<?=$_SERVER['REQUEST_URI'];?>" method="get">
<section> <section>
<div class="content-box"> <div class="content-box">
<header class="content-box-head container-fluid"> <header class="content-box-head container-fluid">
<h3><?=gettext('History');?></h3> <h3><?=gettext('History');?></h3>
</header> </header>
<div class="content-box-main"> <div class="content-box-main">
<div class="container-fluid __mb"> <div class="container-fluid __mb">
<button type="submit" name="diff" class="btn btn-primary pull-left" style="margin-right: 8px;" value="Diff"><?=gettext('View differences');?></button> <table class="table table-condensed">
<tr>
<td>
<button type="submit" name="diff" class="btn btn-primary pull-left" value="Diff">
<?=gettext('View differences');?>
</button>
</td>
<td>
<?= 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 button."); ?> <?= 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 button."); ?>
</td>
</tr>
</table>
</div> </div>
<table class="table table-striped">
<table class="table table-striped table-sort" summary="difference">
<thead> <thead>
<tr> <tr>
<td colspan="2" valign="middle" class="list nowrap"><?=gettext("Diff");?></td> <th colspan="2"><?=gettext("Diff");?></th>
<td class="listhdrr"><?=gettext("Date");?></td> <th><?=gettext("Date");?></th>
<td class="listhdrr"><?=gettext("Version");?></td> <th><?=gettext("Version");?></th>
<td class="listhdrr"><?=gettext("Size");?></td> <th><?=gettext("Size");?></th>
<td class="listhdrr"><?=gettext("Configuration Change");?></td> <th><?=gettext("Configuration Change");?></th>
<td class="list">&nbsp;</td> <th>&nbsp;</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr valign="top"> <tr>
<td valign="middle" class="list nowrap"></td> <td></td>
<td class="list"> <td>
<input type="radio" name="newtime" value="current" <?= $newcheck == 'current' ? 'checked="checked"' : '' ?>/> <input type="radio" name="newtime" value="current" <?= !isset($newcheck) || $newcheck == 'current' ? 'checked="checked"' : '' ?>/>
</td> </td>
<td class="listlr"> <?= date(gettext("n/j/y H:i:s"), $config['revision']['time']) ?></td> <td> <?=date(gettext("n/j/y H:i:s"), $config['revision']['time']) ?></td>
<td class="listr"> <?= $config['version'] ?></td> <td> <?=$config['version'] ?></td>
<td class="listr"> <?= format_bytes(filesize("/conf/config.xml")) ?></td> <td> <?=format_bytes(filesize("/conf/config.xml")) ?></td>
<td class="listr"> <?= "{$config['revision']['username']}: {$config['revision']['description']}" ?></td> <td> <?="{$config['revision']['username']}: {$config['revision']['description']}" ?></td>
<td valign="middle" class="list nowrap"><b><?=gettext("Current");?></b></td> <td><b><?=gettext("Current");?></b></td>
</tr> </tr>
<?php <?php
$c = 0; $i = 0;
foreach($confvers as $version): foreach($confvers as $version):?>
?> <tr>
<tr valign="top"> <td>
<td class="list"> <input type="radio" name="oldtime" value="<?=$version['time'];?>" <?= (!isset($oldcheck) && $i == 0) || (isset($oldcheck) && $oldcheck == $version['time']) ? 'checked="checked"' : '' ?>/>
<input type="radio" name="oldtime" value="<?php echo $version['time'];?>" <?= $oldcheck == $version['time'] ? 'checked="checked"' : '' ?>/>
</td> </td>
<td class="list"> <td>
<?php if ($c < (count($confvers) - 1)) { ?> <input type="radio" name="newtime" value="<?=$version['time'];?>" <?= isset($newcheck) && $newcheck == $version['time'] ? 'checked="checked"' : ''?>/>
<input type="radio" name="newtime" value="<?php echo $version['time'];?>" <?= $newcheck == $version['time'] ? 'checked="checked"' : ''?>/>
<?php } else { ?>
&nbsp;
<?php }
$c++; ?>
</td> </td>
<td class="listlr"> <?= date(gettext("n/j/y H:i:s"), $version['time']) ?></td> <td> <?= date(gettext("n/j/y H:i:s"), $version['time']) ?></td>
<td class="listr"> <?= $version['version'] ?></td> <td> <?= $version['version'] ?></td>
<td class="listr"> <?= format_bytes($version['filesize']) ?></td> <td> <?= format_bytes($version['filesize']) ?></td>
<td class="listr"> <?= "{$version['username']}: {$version['description']}" ?></td> <td> <?= "{$version['username']}: {$version['description']}" ?></td>
<td>
<td class="btn-group-table"> <a data-id="<?=$version['time'];?>" href="#" class="act_revert btn btn-default btn-xs" data-toggle="tooltip" data-placement="left" title="<?=gettext("Revert to this configuration");?>">
<a href="diag_confbak.php?newver=<?=$version['time'];?>" class="btn btn-default btn-xs" title="<?=gettext("Revert to this configuration");?>"><span class="glyphicon glyphicon-log-in"></span></a> <span class="glyphicon glyphicon-log-in"></span>
<a href="diag_confbak.php?rmver=<?=$version['time'];?>" class="btn btn-default btn-xs" title="<?=gettext("Remove this backup");?>" ><span class="glyphicon glyphicon-remove"></span></a> </a>
<a href="diag_confbak.php?getcfg=<?=$version['time'];?>" class="btn btn-default btn-xs" title="<?=gettext("Download this backup");?>"><span class="glyphicon glyphicon-download"></span></a> <a data-id="<?=$version['time'];?>" href="#" class="act_delete btn btn-default btn-xs" data-toggle="tooltip" data-placement="left" title="<?=gettext("Remove this backup");?>" >
<span class="glyphicon glyphicon-remove"></span>
</a>
<a href="diag_confbak.php?getcfg=<?=$version['time'];?>" class="btn btn-default btn-xs" title="<?=gettext("Download this backup");?>">
<span class="glyphicon glyphicon-download"></span>
</a>
</td> </td>
</tr> </tr>
<?php endforeach; ?> <?php
$i++;
endforeach;?>
</tbody> </tbody>
</table> </table>
</div> </div>
</div>
</section> </section>
</form> </form>
<?php endif; endif; ?>
</div> </div>
</section> </section>
</div> </div>
</div> </div>
</section> </section>
<?php include("foot.inc"); ?> <?php include("foot.inc"); ?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment