diag_confbak.php 14.4 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
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    $cnf = OPNsense\Core\Config::getInstance();
    $confvers = $cnf->getBackups(true);
36 37 38 39 40 41
    if (isset($config['system']['backupcount'])) {
        $pconfig['backupcount'] = $config['system']['backupcount'];
    } else {
        # XXX fallback value for older configs
        $pconfig['backupcount'] = 60;
    }
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
    if (!empty($_GET['getcfg'])) {
        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;
                exit;
            }
        }
    } elseif (!empty($_GET['diff']) && isset($_GET['oldtime']) && isset($_GET['newtime'])
          && is_numeric($_GET['oldtime']) && (is_numeric($_GET['newtime']) || ($_GET['newtime'] == 'current'))) {
        $oldfile = '';
        $newfile = '';
        // search filenames to compare
        foreach ($confvers as $filename => $revision) {
            if ($revision['time'] == $_GET['oldtime']) {
                $oldfile = $filename;
            }
            if ($revision['time'] == $_GET['newtime']) {
                $newfile = $filename;
            }
        }

        $diff = '';

        $oldtime = $_GET['oldtime'];
        $oldcheck = $oldtime;

        if ($_GET['newtime'] == 'current') {
            $newfile = '/conf/config.xml';
            $newtime = $config['revision']['time'];
        } else {
            $newtime = $_GET['newtime'];
            $newcheck = $newtime;
        }

        if (file_exists($oldfile) && file_exists($newfile)) {
            exec("/usr/bin/diff -u " . escapeshellarg($oldfile) . " " . escapeshellarg($newfile), $diff);
        }
    }
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
88 89 90 91
    $input_errors = array();
    $pconfig = $_POST;

    if (!empty($pconfig['save'])) {
92 93
        if (!isset($pconfig['backupcount']) || !is_numeric($pconfig['backupcount']) || $pconfig['backupcount'] <= 0) {
            $input_errors[] = gettext('Backup count must be greater than zero.');
94 95 96
        }
        if (count($input_errors) == 0) {
            $config['system']['backupcount'] = $pconfig['backupcount'];
97
            write_config('Changed backup revision count');
98 99 100 101
            $savemsg = get_std_save_message();
        }
    }

102 103
    $cnf = OPNsense\Core\Config::getInstance();
    $confvers = $cnf->getBackups(true);
104 105

    if (!empty($_POST['act']) && $_POST['act'] == "revert") {
106 107
        foreach ($confvers as $filename => $revision) {
            if (isset($revision['time']) && $revision['time'] == $_POST['time']) {
108
                if (config_restore($filename) == 0) {
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
                    $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;
            }
        }
    }
Ad Schellevis's avatar
Ad Schellevis committed
130 131 132 133 134
}

include("head.inc");
?>

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
<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({
Fabian Franz's avatar
Fabian Franz committed
164
          type:BootstrapDialog.TYPE_DANGER,
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
          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>
Ad Schellevis's avatar
Ad Schellevis committed
185 186


187 188 189
<body>
  <?php
    include("fbegin.inc");
190 191 192 193
    if (isset($input_errors) && count($input_errors) > 0) {
        print_input_errors($input_errors);
    }
    if ($savemsg) {
194
      print_info_box($savemsg);
195
    }
196 197 198 199 200
  ?>
  <section class="page-content-main">
    <div class="container-fluid">
      <div class="row">
        <section class="col-xs-12">
201 202 203 204 205 206 207 208 209
          <form method="post" id="iform">
            <input type="hidden" id="time" name="time" value="" />
            <input type="hidden" id="action" name="act" value="" />
            <div class="content-box tab-content table-responsive __mb">
              <table class="table table-striped">
                <tr>
                  <th colspan="2" valign="top" class="listtopic"><?=gettext("Backup Count"); ?></th>
                </tr>
                <tr>
210 211
                  <td><input name="backupcount" type="text" class="formfld unknown" size="5" value="<?=htmlspecialchars($pconfig['backupcount']);?>"/></td>
                  <td><?= gettext("Enter the number of older configurations to keep in the local backup cache."); ?></td>
212 213 214 215 216
                </tr>
                <tr>
                  <td>
                    <input name="save" type="submit" class="btn btn-primary" value="<?=gettext("Save"); ?>" />
                  </td>
217
                  <td>
218 219 220 221
                    <?= gettext('Be aware of how much space is consumed by backups before adjusting this value.'); ?>
                    <?php if (count($confvers)) {
                      print gettext('Current space used:') . ' ' . exec("/usr/bin/du -sh /conf/backup | /usr/bin/awk '{print $1;}'");
                    } ?>
222 223 224 225 226 227
                  </td>
                </tr>
              </table>
            </div>
          </form>
          <?php if ($diff): ?>
228 229 230 231 232 233 234 235 236 237 238 239 240
          <div class="content-box tab-content table-responsive __mb" style="overflow: scroll;">
            <table class="table table-striped">
              <tr>
                <th colspan="2" valign="top" class="listtopic">
                  <?= sprintf(
                        gettext('Configuration diff from %s to %s'),
                        date(gettext('n/j/y H:i:s'), $oldtime),
                        date(gettext('n/j/y H:i:s'), $newtime)
                      ); ?>
	        </th>
              </tr>
              <tr>
                <td>
241
<?php
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
                  foreach ($diff as $line):
                    switch (substr($line, 0, 1)) {
                      case '+':
                        $color = '#3bbb33';
                        break;
                      case '-':
                        $color = '#c13928';
                        break;
                      case '@':
                        $color = '#3bb9c3';
                        break;
                      default:
                        $color = '#000000';
                    } ?>
                  <span style="color: <?=$color;?>; white-space: pre-wrap; font-family: monospace;"><?=htmlentities($line);?></span><br/>
<?php endforeach; ?>
                </td>
              </tr>
            </table>
          </div>
262
          <?php endif; ?>
263
          <?php if (count($confvers)): ?>
264 265 266 267 268 269
          <form method="get">
          <section>
            <div class="content-box">
              <header class="content-box-head container-fluid">
                <h3><?=gettext('History');?></h3>
              </header>
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
              <table class="table table-responsive">
                <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."); ?>
                  </td>
                </tr>
              </table>
              <table class="table table-striped">
                <thead>
                  <tr>
                    <th colspan="2"><?=gettext("Diff");?></th>
                    <th><?=gettext("Date");?></th>
                    <th><?=gettext("Version");?></th>
                    <th><?=gettext("Size");?></th>
                    <th><?=gettext("Configuration Change");?></th>
                    <th>&nbsp;</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <td></td>
                    <td>
                      <input type="radio" name="newtime" value="current" <?= !isset($newcheck) || $newcheck == 'current' ? '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']['username']}: {$config['revision']['description']}" ?></td>
                    <td><b><?=gettext("Current");?></b></td>
                  </tr>
305
<?php
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
                $last = count($confvers);
                $curr = 1;
                foreach($confvers as $version):?>
                  <tr>
                    <td>
                      <input type="radio" name="oldtime" value="<?=$version['time'];?>" <?= (!isset($oldcheck) && $curr == 1)  || (isset($oldcheck) && $oldcheck == $version['time']) ? 'checked="checked"' : '' ?>/>
                    </td>
                    <td>
                      <?php if ($curr != $last): ?>
                      <input type="radio" name="newtime" value="<?=$version['time'];?>" <?= isset($newcheck) && $newcheck == $version['time'] ? 'checked="checked"' : ''?>/>
                      <?php endif ?>
                    </td>
                    <td> <?= date(gettext("n/j/y H:i:s"), $version['time']) ?></td>
                    <td> <?= $version['version'] ?></td>
                    <td> <?= format_bytes($version['filesize']) ?></td>
                    <td> <?= "{$version['username']}: {$version['description']}" ?></td>
                    <td>
                      <a data-id="<?=$version['time'];?>" href="#" class="act_revert btn btn-default btn-xs" data-toggle="tooltip" title="<?=gettext("Revert to this configuration");?>">
                         <span class="glyphicon glyphicon-log-in"></span>
325
                       </a>
326 327 328 329 330 331 332 333
                       <a data-id="<?=$version['time'];?>" href="#" class="act_delete btn btn-default btn-xs" data-toggle="tooltip" title="<?=gettext("Remove this backup");?>" >
                         <span class="fa fa-trash text-muted"></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>
                  </tr>
334
<?php
335 336 337 338
                $curr++;
                endforeach;?>
                </tbody>
              </table>
339 340 341
            </div>
          </section>
        </form>
342
        <?php endif ?>
343 344 345 346
      </section>
    </div>
  </div>
</section>
347
<?php include("foot.inc"); ?>