diag_confbak.php 13.2 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1
<?php
2

Ad Schellevis's avatar
Ad Schellevis committed
3
/*
4
    Copyright (C) 2014 Deciso B.V.
Ad Schellevis's avatar
Ad Schellevis committed
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
    Copyright (C) 2005 Colin Smith
    Copyright (C) 2010 Jim Pingle
    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.
*/

31
require_once("guiconfig.inc");
Ad Schellevis's avatar
Ad Schellevis committed
32

33 34 35 36 37
// list backups
$cnf = OPNsense\Core\Config::getInstance();
$confvers = $cnf->getBackups(true);


Ad Schellevis's avatar
Ad Schellevis committed
38 39 40 41 42 43 44 45 46 47
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) {
48
	if (!isset($_POST['confirm']) || ($_POST['confirm'] != "Confirm") || (!isset($_POST['newver']) && !isset($_POST['rmver']))) {
Ad Schellevis's avatar
Ad Schellevis committed
49 50 51 52 53
		header("Location: diag_confbak.php");
		return;
	}

	if($_POST['newver'] != "") {
54 55 56 57 58 59 60 61 62 63 64
		// search item by revision
		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 %1$s with description "%2$s".'), date(gettext("n/j/y H:i:s"), $_POST['newver']), $revision['description']);
				} else {
					$savemsg = gettext("Unable to revert to the selected configuration.");
				}
				break;
			}
		}
65

Ad Schellevis's avatar
Ad Schellevis committed
66 67
	}
	if($_POST['rmver'] != "") {
68 69
		// search item by revision
		foreach ($confvers as $filename => $revision) {
70
			if (isset($revision['time']) && $revision['time'] == $_POST['rmver']) {
71 72 73 74 75
				@unlink($filename);
				$savemsg = sprintf(gettext('Deleted backup with timestamp %1$s and description "%2$s".'), date(gettext("n/j/y H:i:s"), $_POST['rmver']),$revision['description']);
				break;
			}
		}
Ad Schellevis's avatar
Ad Schellevis committed
76 77 78 79
	}
}

if($_GET['getcfg'] != "") {
80 81 82 83 84 85 86 87 88 89
	foreach ($confvers as $filename => $revision) {
		if ($revision['time'] == $_GET['getcfg']) {
			$exp_name = urlencode("config-{$config['system']['hostname']}.{$config['system']['domain']}-{$_GET['getcfg']}.xml");
			$exp_data = file_get_contents($filename);
			$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;
90
			exit;
91 92
		}
	}
Ad Schellevis's avatar
Ad Schellevis committed
93 94 95 96
}

if (($_GET['diff'] == 'Diff') && isset($_GET['oldtime']) && isset($_GET['newtime'])
      && is_numeric($_GET['oldtime']) && (is_numeric($_GET['newtime']) || ($_GET['newtime'] == 'current'))) {
97 98 99
	$oldfile = "";
	$newfile = "" ;
	// search filenames to compare
100 101 102 103 104 105 106
	foreach ($confvers as $filename => $revision) {
		if ($revision['time'] == $_GET['oldtime']) {
			$oldfile = $filename;
		} elseif  ($revision['time'] == $_GET['oldtime']) {
			$newfile = $filename;
		}
	}
107

Ad Schellevis's avatar
Ad Schellevis committed
108 109 110
	$diff = "";
	$oldtime = $_GET['oldtime'];
	if ($_GET['newtime'] == 'current') {
111
		$newfile = '/conf/config.xml';
Ad Schellevis's avatar
Ad Schellevis committed
112 113 114 115
		$newtime = $config['revision']['time'];
	} else {
		$newtime = $_GET['newtime'];
	}
116

Ad Schellevis's avatar
Ad Schellevis committed
117 118 119 120 121 122 123 124 125 126 127
	if (file_exists($oldfile) && file_exists($newfile)) {
		exec("/usr/bin/diff -u " . escapeshellarg($oldfile) . " " . escapeshellarg($newfile), $diff);
	}
}


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

?>

128
<body>
Ad Schellevis's avatar
Ad Schellevis committed
129 130 131 132 133
	<?php
		include("fbegin.inc");
		if($savemsg)
			print_info_box($savemsg);
	?>
134

135
	<section class="page-content-main">
136
		<div class="container-fluid">
137
			<div class="row">
138

139
			    <section class="col-xs-12">
140

141 142 143 144 145 146
					<?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);
					?>
Ad Schellevis's avatar
Ad Schellevis committed
147

148

149
						<div class="tab-content content-box col-xs-12">
Ad Schellevis's avatar
Ad Schellevis committed
150

151 152 153 154 155
					    <div class="container-fluid tab-content">

							<div class="tab-pane active" id="system">


156
									<?php if ($diff): ?>
Ad Schellevis's avatar
Ad Schellevis committed
157
									<section class="__mb">
158
										<div classs="content-box">
Ad Schellevis's avatar
Ad Schellevis committed
159
											<header class="content-box-head container-fluid">
160 161 162 163 164 165 166
									        <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>
									    </header>

									     <div class="content-box-main">

									    <div class="table-responsive">

167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
													<table summary="diag confbak">
														<tr><td></td></tr>
														<?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";
															}
															?>
														<tr>
															<td valign="middle" bgcolor="<?php echo $color; ?>" style="white-space: pre-wrap;"><?php echo htmlentities($line);?></td>
														</tr>
														<?php } ?>
													</table>
189 190
									    </div>
									     </div>
191 192 193
										</div>
									</section>
									<?php endif; ?>
194 195 196


									<?PHP if ($_GET["newver"] || $_GET["rmver"]): ?>
197
									<form action="<?=explode("?", $_SERVER['REQUEST_URI'])[0];?>" method="post">
198
									<section>
199 200
				                        <div class="content-box">

Ad Schellevis's avatar
Ad Schellevis committed
201
				                            <header class="content-box-head container-fluid">
202 203 204 205 206 207
									        <h3><?PHP echo gettext("Confirm Action"); ?></h3>
									    </header>

									    <div class="content-box-main col-xs-12">

										    <strong><?PHP echo gettext("Please confirm the selected action"); ?></strong>:
208 209 210 211 212 213 214 215 216 217 218 219 220
												<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)); ?>
221 222 223
												<br /><input type="submit" name="confirm" class="btn btn-primary" value="<?PHP echo gettext("Confirm"); ?>" />

									    </div>
224 225 226
				                        </div>
									</section>
									</form>
227

228
									<? else: ?>
229

230 231
									<form action="<?=$_SERVER['REQUEST_URI'];?>" method="post">
									<section style="margin-bottom:15px;">
232 233
				                        <div class="content-box">

Ad Schellevis's avatar
Ad Schellevis committed
234
				                            <header class="content-box-head container-fluid">
235 236 237 238 239 240 241 242 243 244 245
									        <h3>Config history</h3>
									    </header>

									    <div class="content-box-main">

									    <div class="table-responsive">
									        <table class="table table-striped">
										        <tbody>
										        <tr>
										          <td><?=gettext("Backup Count");?></td>
										          <td><input name="backupcount" type="text" class="formfld unknown" size="5" value="<?=htmlspecialchars($config['system']['backupcount']);?>"/></td>
Ad Schellevis's avatar
Ad Schellevis committed
246
										          <td><?= gettext("Enter the number of older configurations to keep in the local backup cache. By default this is 30."); ?></td>
247 248 249 250 251 252 253 254 255 256
										          <td><input name="save" type="submit" class="btn btn-primary" value="<?=gettext("Save"); ?>" /></td>
										        </tr>
										        </tbody>
										    </table>

										    <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;}'") ?>
										    </div>
									    </div>
									    </div>
257 258 259
				                        </div>
									</section>
									</form>
260 261


262 263 264 265
									<?php if (is_array($confvers)): ?>
									<form action="<?=$_SERVER['REQUEST_URI'];?>" method="get">
									<section>
										<div class="content-box">
Ad Schellevis's avatar
Ad Schellevis committed
266
											<div class="content-box-main">
267

Ad Schellevis's avatar
Ad Schellevis committed
268
												<div class="container-fluid __mb">
269
												<?= 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."); ?>
Ad Schellevis's avatar
Ad Schellevis committed
270
												</div>
271 272


Ad Schellevis's avatar
Ad Schellevis committed
273
                                            <table class="table table-striped table-sort" summary="difference">
274 275

											<thead>
276
											<tr>
Ad Schellevis's avatar
Ad Schellevis committed
277
												<td colspan="2" valign="middle" class="list nowrap"><?=gettext("Diff");?></td>
278 279 280 281 282 283
												<td class="listhdrr"><?=gettext("Date");?></td>
												<td class="listhdrr"><?=gettext("Version");?></td>
												<td class="listhdrr"><?=gettext("Size");?></td>
												<td class="listhdrr"><?=gettext("Configuration Change");?></td>
												<td class="list">&nbsp;</td>
											</tr>
Ad Schellevis's avatar
Ad Schellevis committed
284 285
											</thead>
											<tbody>
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
											<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"> <?= $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"> <?= substr($version['description'],0,50) ?></td>
321

322
												 <td class="btn-group-table">
Ad Schellevis's avatar
Ad Schellevis committed
323 324 325
		                                            <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>
		                                            <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 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>
326
		                                        </td>
327

328
											</tr>
329
											<?php endforeach; ?>
Ad Schellevis's avatar
Ad Schellevis committed
330
											</tbody>
331
										</table>
332

Ad Schellevis's avatar
Ad Schellevis committed
333
										<div class="container-fluid">
334
										<input type="submit" name="diff" class="btn btn-primary" value="<?=gettext("Diff"); ?>" />
335 336 337 338
										</div>

										</div>
									</section>
339 340
									</form>
									<? endif; endif; ?>
341 342 343 344 345 346 347 348 349 350



					    </div>

					</div>
				</section>



351 352 353
			</div>
		</div>
	</section>
Ad Schellevis's avatar
Ad Schellevis committed
354 355


356
<?php include("foot.inc"); ?>