#!/usr/bin/php -q
<?PHP
/* Copyright 2005-2022, Lime Technology
 * Copyright 2012-2022, Bergware International.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2,
 * as published by the Free Software Foundation.
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 */
?>
<?
$docroot = $docroot ?? $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp';

require_once "$docroot/webGui/include/Wrappers.php";
extract(parse_plugin_cfg('dynamix',true));

// add translations
$_SERVER['REQUEST_URI'] = 'settings';
$login_locale = $display['locale'];
require_once "$docroot/webGui/include/Translations.php";

function write(...$messages){
  $com = curl_init();
  curl_setopt_array($com,[
    CURLOPT_URL => 'http://localhost/pub/changes?buffer_length=1',
    CURLOPT_UNIX_SOCKET_PATH => '/var/run/nginx.socket',
    CURLOPT_POST => 1,
    CURLOPT_RETURNTRANSFER => true
  ]);
  foreach ($messages as $message) {
    curl_setopt($com, CURLOPT_POSTFIELDS, $message);
    curl_exec($com);
  }
  curl_close($com);
}

unset($argv[$argc-1]); // remove nchan parameter
$file = $argv[1];
$path = realpath('/etc/wireguard'.($argv[2]??''));
$root = '/boot/config/wireguard';

$style = ["<style>"];
$style[] = "pre h2{text-decoration:underline}";
$style[] = "input#download{margin:0 0 10px 0}";
$style[] = "pre.config{font-family:bitstream;margin:0;border:none}";
$style[] = "img{display:block;margin:20px 0}";
$style[] = "img:hover{transform:scale(1.1)}";
$style[] = "</style>";

$script = ["<script>"];
$script[] = "function config_cleanUp(id,file) {";
$script[] = "  if (document.hasFocus()) {";
$script[] = "    $('#'+id).val(\""._('Download')."\").prop('disabled',false);";
$script[] = "    $.post('/webGui/include/Download.php',{cmd:'delete',file:file});";
$script[] = "  } else {";
$script[] = "    setTimeout(function(){config_cleanUp(id,file);},1000);";
$script[] = "  }";
$script[] = "}";
$script[] = "function config_download(id,source,file) {";
$script[] = "  $('#'+id).val(\""._('Downloading')."...\").prop('disabled',true);";
$script[] = "  $.post('/webGui/include/Download.php',{cmd:'save',source:source+'.conf',file:file,opts:'qj'},function(){";
$script[] = "    $.post('/webGui/include/Download.php',{cmd:'save',source:source+'.png',file:file,opts:'qj'},function(zip){";
$script[] = "      location = zip;";
$script[] = "      setTimeout(function(){config_cleanUp(id,file);},1000);";
$script[] = "    });";
$script[] = "  });";
$script[] = "}";
$script[] = "</script>";

$html = [];
$html[] = "<h2>".($argv[2] ? _('Remote peer configuration') : _('Local server configuration'))."</h2>";
$html[] = "<div><input type='button' id='download' value=\""._('Download')."\" onclick=\"config_download(this.id,'$path/$file','$file.zip')\"></div>";
$html[] = "<pre class='config'>";
$html[] = @file_get_contents("$path/$file.conf");
$html[] = "\n";
$html[] = "</pre>";
if (is_file("$path/$file.png")) {
  @unlink("$docroot/$file.png");
  symlink("$path/$file.png", "$docroot/$file.png");
  $html[] = "<img src=\"/$file.png?v=".filemtime("$path/$file.png")."\">";
}

write(implode($style).implode($script).implode($html),'_DONE_','');
?>
