scripts: code simplification

This commit is contained in:
bergware
2024-02-04 12:02:02 +01:00
parent 7772e3db38
commit 202f6fec50
4 changed files with 98 additions and 98 deletions
@@ -22,6 +22,8 @@ $login_locale = _var($display,'locale');
require_once "$docroot/webGui/include/Translations.php";
$month = [' Jan '=>'-01-',' Feb '=>'-02-',' Mar '=>'-03-',' Apr '=>'-04-',' May '=>'-05-',' Jun '=>'-06-',' Jul '=>'-07-',' Aug '=>'-08-',' Sep '=>'-09-',' Oct '=>'-10-',' Nov '=>'-11-',' Dec '=>'-12-'];
$log = "/boot/config/parity-checks.log";
$list = [];
function this_plus($val, $word, $last) {
return $val>0 ? (($val||$last)?($val.' '.$word.($last?'':', ')):'') : '';
@@ -35,10 +37,10 @@ function this_duration($time) {
$secs = $hmss%60;
return this_plus($days,_('day'),($hour|$mins|$secs)==0).this_plus($hour,_('hr'),($mins|$secs)==0).this_plus($mins,_('min'),$secs==0).this_plus($secs,_('sec'),true);
}
$log = "/boot/config/parity-checks.log";
$head = "<table style='margin-top:10px;background-color:inherit'><tr style='font-weight:bold'><td>"._('Action')."</td><td>"._('Date')."</td><td>"._('Size')."</td><td>"._('Duration')."</td><td>"._('Speed')."</td><td>"._('Status')."</td><td>"._('Errors')."</td></tr>";
$list = [];
?>
<table style='margin-top:10px;background-color:inherit'>
<tr style='font-weight:bold'><td><?=_('Action')?></td><td><?=_('Date')?></td><td><?=_('Size')?></td><td><?=_('Duration')?></td><td><?=_('Speed')?></td><td><?=_('Status')?></td><td><?=_('Errors')?></td></tr>
<?
if (file_exists($log)) {
$handle = fopen($log, 'r');
while (($row = fgets($handle))!==false) {
@@ -66,5 +68,6 @@ if ($list) {
} else {
$list[] = "<tr><td colspan='7' style='text-align:center;padding-top:12px'>"._('No parity check history present')."!</td></tr>";
}
echo $head,implode($list),"</table>";
echo implode($list);
?>
</table>
+32 -33
View File
@@ -24,34 +24,35 @@ require_once "$docroot/webGui/include/Translations.php";
$boot = "/boot/config/plugins/dynamix";
$file = $argv[1];
$cmodel = is_file("$boot/$file") ? file_get_contents("$boot/$file") : '';
?>
<style>
div.case-list{float:left;padding:10px;margin:0 45px 64px 0;height:128px;width:128px;text-align:center;cursor:pointer}
div.case-list span{position:relative;top:64px;width:auto;max-width:128px;height:128px;font-size:128px}
div.case-list span.fa{top:24px;max-width:80px;font-size:80px}
div.case-list:hover{color:#f0000c}
div.case-name{position:relative;top:74px;font-family:clear-sans!important}
div.custom-name{position:relative;top:10px;font-family:clear-sans!important}
</style>
$style = ["<style>"];
$style[] = "div.case-list{float:left;padding:10px;margin:0 45px 64px 0;height:128px;width:128px;text-align:center;cursor:pointer}";
$style[] = "div.case-list span{position:relative;top:64px;width:auto;max-width:128px;height:128px;font-size:128px}";
$style[] = "div.case-list span.fa{top:24px;max-width:80px;font-size:80px}";
$style[] = "div.case-list:hover{color:#f0000c}";
$style[] = "div.case-name{position:relative;top:74px;font-family:clear-sans!important}";
$style[] = "div.custom-name{position:relative;top:10px;font-family:clear-sans!important}";
$style[] = "</style>";
<script>
function selectDone() {
$('.sweet-alert').hide('fast').removeClass('nchan');
swal.close();
getCase();
}
function setCase(model) {
$.post('/webGui/include/SelectCase.php',{mode:'set',file:'<?=$file?>',model:model},function(){selectDone();});
}
function importFile(file) {
if (file.name.split('.').pop().toLowerCase() != 'png') return;
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function(e){$.post('/webGui/include/SelectCase.php',{mode:'file',file:'<?=$file?>',data:e.target.result},function(){selectDone();})};
}
</script>
$script = ["<script>"];
$script[] = "function selectDone() {";
$script[] = " \$('.sweet-alert').hide('fast').removeClass('nchan');";
$script[] = " swal.close();";
$script[] = " getCase();";
$script[] = "}";
$script[] = "function setCase(model) {";
$script[] = " \$.post('/webGui/include/SelectCase.php',{mode:'set',file:'$file',model:model},function(){selectDone();});";
$script[] = "}";
$script[] = "function importFile(file) {";
$script[] = " if (file.name.split('.').pop().toLowerCase() != 'png') return;";
$script[] = " var reader = new FileReader();";
$script[] = " reader.readAsDataURL(file);";
$script[] = " reader.onload = function(e){\$.post('/webGui/include/SelectCase.php',{mode:'file',file:'$file',data:e.target.result},function(){selectDone();})};";
$script[] = "}";
$script[] = "</script>";
$html = ["<div>"];
<div>
<?
$cases = file("$docroot/webGui/styles/default-cases.css",FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
foreach ($cases as $case) if (substr($case,0,6)=='.case-') $models[] = substr($case,1,strpos($case,':')-1);
natsort($models);
@@ -60,13 +61,11 @@ for ($i=0; $i < count($models); $i++) {
$name = substr($model,5);
$title = str_replace('3u-avs-10-4','3u-avs-10/4',$name);
$select = $name==$cmodel ? 'color:#e68a00' : '';
$html[] = "<div id='$name' class='case-list' style='$select' onclick='setCase(\"$name\")'><span class='$model'></span><div class='case-name'>$title</div></div>";
echo "<div id='$name' class='case-list' style='$select' onclick='setCase(\"$name\")'><span class='$model'></span><div class='case-name'>$title</div></div>\n";
}
$select = $cmodel=='case-model.png' ? 'color:#e68a00' : '';
$html[] = "<div id='Custom' class='case-list' style='$select' onclick='$(\"input#file\").trigger(\"click\")'><span class='fa fa-file-image-o'></span><div class='custom-name'>"._('custom image')."</div></div>";
$html[] = "</div></div>";
$html[] = "<input type='file' id='file' accept='.png' onchange='importFile(this.files[0])' style='display:none'>";
$html[] = "</div>";
echo implode($style),implode($script),implode($html);
?>
<div id='Custom' class='case-list' style='<?=$select?>' onclick='$("input#file").trigger("click")'><span class='fa fa-file-image-o'></span><div class='custom-name'><?=_('custom image')?></div></div>
</div></div>
<input type='file' id='file' accept='.png' onchange='importFile(this.files[0])' style='display:none'>
</div>
@@ -136,23 +136,28 @@ if ($memory_installed >= 1024) {
// If maximum < installed then roundup maximum to the next power of 2 size of installed. E.g. 6 -> 8 or 12 -> 16
$low = $memory_maximum < $memory_installed;
if ($low) $memory_maximum = pow(2,ceil(log($memory_installed)/log(2)));
?>
<style>
table.info{margin-top:10px;background-color:inherit}
table.info td:first-child{width:20%;font-weight:bold;padding-left:10px}
tr.ram,tr.port{display:none}
span.link{text-decoration:underline;cursor:pointer}
</style>
$style = "<style>table.info{margin-top:10px;background-color:inherit} table.info td:first-child{width:20%;font-weight:bold;padding-left:10px} tr.ram,tr.port{display:none} span.link{text-decoration:underline;cursor:pointer}</style>";
$list = [];
$list[] = "<table class='info'><tr><td>"._('Model').":</td><td>$model</td></tr>";
$list[] = "<tr><td>".('M/B').":</td><td>{$board['Manufacturer']} {$board['Product Name']} {$board['Version']} {$board['Serial Number']}</td></tr>";
$list[] = "<tr><td>"._('BIOS').":</td><td>{$bios['Vendor']} {$bios['Version']} {$bios['Release Date']}</td></tr>";
$list[] = "<tr><td>"._('CPU').":</td><td>$cpumodel {$cpu['Current Speed']}</td></tr>";
$list[] = "<tr><td>"._('HVM').":</td><td>$hvm</td></tr>";
$list[] = "<tr><td>"._('IOMMU').":</td><td>$iommu</td></tr>";
$list[] = "<tr><td>"._('Cache').":</td><td>".implode(', ',$cache_installed)."</td></tr>";
$list[] = "<tr><td>$memory</td><td>$memory_installed $unit $memory_type $ecc("._('max. installable capacity')." $memory_maximum $unit".($low?'*':'').")</td></tr>";
<table class='info'>
<tr><td><?=_('Model')?>:</td><td><?=$model?></td></tr>
<tr><td><?=('M/B')?>:</td><td><?="{$board['Manufacturer']} {$board['Product Name']} {$board['Version']} {$board['Serial Number']}"?></td></tr>
<tr><td><?=_('BIOS')?>:</td><td><?="{$bios['Vendor']} {$bios['Version']} {$bios['Release Date']}"?></td></tr>
<tr><td><?=_('CPU')?>:</td><td><?="$cpumodel {$cpu['Current Speed']}"?></td></tr>
<tr><td><?=_('HVM')?>:</td><td><?=$hvm?></td></tr>
<tr><td><?=_('IOMMU')?>:</td><td><?=$iommu?></td></tr>
<tr><td><?=_('Cache')?>:</td><td><?=implode(', ',$cache_installed)?></td></tr>
<tr><td><?=$memory?></td><td><?="$memory_installed $unit $memory_type $ecc("._('max. installable capacity')." $memory_maximum $unit".($low?'*':'')?>)</td></tr>
<?
foreach ($memory_devices as $device) {
if (empty($device['Type']) || $device['Type']=='Unknown') continue;
$size = preg_replace('/( .)B$/','$1iB',_var($device,'Size',0));
$list[] = "<tr class='ram'><td></td><td>".$device['Locator'].": "._var($device,'Manufacturer')." "._var($device,'Part Number').", $size "._var($device,'Type')." @ "._var($device,'Configured Memory Speed')."</td></tr>";
echo "<tr class='ram'><td></td><td>",$device['Locator'],": ",_var($device,'Manufacturer')," ",_var($device,'Part Number'),", $size ",_var($device,'Type')," @ ",_var($device,'Configured Memory Speed'),"</td></tr>";
}
exec("ls --indicator-style=none /sys/class/net|grep -Po '^(bond|eth)\d+$'",$sPorts);
@@ -167,24 +172,22 @@ foreach ($sPorts as $port) {
if (substr($port,0,4)=='bond') {
if ($link) {
$bond_mode = str_replace('Bonding Mode: ','',file("/proc/net/bonding/$port",FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES)[1]);
$list[] = "<tr class='$more'><td>$name</td><td>$port: $bond_mode, mtu $mtu</td></tr>";
echo "<tr class='$more'><td>$name</td><td>$port: $bond_mode, mtu $mtu</td></tr>";
} else {
$list[] = "<tr class='$more'><td>$name</td><td>$port: "._("bond down")."</td></tr>";
echo "<tr class='$more'><td>$name</td><td>$port: ",_('bond down'),"</td></tr>";
}
} else {
if ($link) {
$speed = file_get_contents("$int/speed");
$duplex = file_get_contents("$int/duplex");
$list[] = "<tr class='$more'><td>$name</td><td>$port: $speed Mbps, $duplex duplex, mtu $mtu</td></tr>";
echo "<tr class='$more'><td>$name</td><td>$port: $speed Mbps, $duplex duplex, mtu $mtu</td></tr>";
} else {
$list[] = "<tr class='$more'><td>$name</td><td>$port: "._("interface down")."</td></tr>";
echo "<tr class='$more'><td>$name</td><td>$port: ",_('interface down'),"</td></tr>";
}
}
}
$list[] = "<tr><td>"._('Kernel').":</td><td>$kernel</td></tr>";
$list[] = "<tr><td>"._('OpenSSL').":</td><td>$openssl</td></tr>";
$list[] = "<tr><td>"._('Uptime').":</td><td><span class='uptime'></span></td></tr></table>";
echo $style,implode($list);
?>
<tr><td><?=_('Kernel')?>:</td><td><?=$kernel?></td></tr>
<tr><td><?=_('OpenSSL')?>:</td><td><?=$openssl?></td></tr>
<tr><td><?=_('Uptime')?>:</td><td><span class='uptime'></span></td></tr>
</table>
+32 -37
View File
@@ -24,47 +24,42 @@ require_once "$docroot/webGui/include/Translations.php";
$file = $argv[1];
$path = realpath('/etc/wireguard'.($argv[2]??''));
$root = '/boot/config/wireguard';
?>
<style>
pre h2{text-decoration:underline}
input#download{margin:0 0 10px 0}
pre.config{font-family:bitstream;margin:0;border:none}
img{display:block;margin:20px 0}
img:hover{transform:scale(1.1)}
</style>
$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>
function config_cleanUp(id,file) {
if (document.hasFocus()) {
$('#'+id).val("<?=_('Download')?>").prop('disabled',false);
$.post('/webGui/include/Download.php',{cmd:'delete',file:file});
} else {
setTimeout(function(){config_cleanUp(id,file);},1000);
}
}
function config_download(id,source,file) {
$('#'+id).val("<?=_('Downloading')?>...").prop('disabled',true);
$.post('/webGui/include/Download.php',{cmd:'save',source:source+'.conf',file:file,opts:'qj'},function(){
$.post('/webGui/include/Download.php',{cmd:'save',source:source+'.png',file:file,opts:'qj'},function(zip){
location = zip;
setTimeout(function(){config_cleanUp(id,file);},1000);
});
});
}
</script>
$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>";
<h2><?=$argv[2] ? _('Remote peer configuration') : _('Local server configuration')?></h2>
<div><input type='button' id='download' value="<?=_('Download')?>" onclick="config_download(this.id,'<?="$path/$file"?>','<?=$file?>.zip')"></div>
<pre class='config'><?=@file_get_contents("$path/$file.conf")?></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")."\">";
echo "<img src=\"/$file.png?v=",filemtime("$path/$file.png"),"\">";
}
echo implode($style),implode($script),implode($html);
?>