Menu="NetworkSettings"
Title="Interface Rules"
Tag="icon-network"
Cond="file_exists('/boot/config/network-rules.cfg')"
---
<?PHP
/* Copyright 2021, Lime Technology
 * Copyright 2021, 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.
 */
?>
<?
$cfg = '/boot/config/network-rules.cfg';

function strip($item) {
  [$key,$val] = explode('"',$item);
  return $val;
}

exec("awk '/NAME=\"eth/{print $4 $8}' $cfg",$rules);
exec("grep -Po '^# \K.*' $cfg",$info);

$link = []; $i = 0;
foreach ($rules as $rule) {
  [$mac,$eth] = array_map('strip',my_explode(',',$rule));
  $link[$eth]['mac'] = $mac;
  $link[$eth]['info'] = $info[$i++];
}
ksort($link,SORT_NATURAL);
?>
<style>
span[id^="info-"]{margin-left:20px}
<?if ($themes1):?>
span.status.vhshift{margin-right:-10px!important}
<?endif;?>
</style>
<script>
var info = {};
<?foreach ($link as $eth => $val):?>
info['<?='_'.str_replace(':','',$val['mac'])?>'] = "<?=$val['info']?>";
<?endforeach;?>

function check_rules(form) {
  var mac = [];
  $(form).find('select[name^="eth"]').each(function(){mac.push($(this).val());});
  if (mac.same()) {
    swal({title:"_(MAC address mismatch)_",text:"_(Rules contain duplicate MAC address assignments)_",type:'error',html:true,confirmButtonText:"_(Ok)_"});
    return false;
  } else {
    return true;
  }
}
function update_info(id,mac) {
  $('#info-'+id).html(info['_'+mac.replace(/:/g,'')]);
}
<?if (!$tabbed):?>
function toggleRules() {
  var tag = '#tag_rules';
  var view = '#rules_table';
  if ($(view).is(':visible')) {
    $(tag).removeClass('fa-chevron-up').addClass('fa-chevron-down');
    $.cookie(view,'hide',{expires:3650});
  } else {
    $(tag).removeClass('fa-chevron-down').addClass('fa-chevron-up');
    $.removeCookie(view);
  }
  $(view).toggle('slow');
}
$(function(){
  $('div#title').eq(-2).find('span.left').append("<span class='status vhshift'><i id='tag_rules' class='fa fa-fw fa-chevron-up' style='cursor:pointer' onclick='toggleRules()'></i></span>");
  if (!$.cookie('#rules_table')) $('#rules_table').show(); else {$('#rules_table').hide(); $('#tag_rules').removeClass('fa-chevron-up').addClass('fa-chevron-down');}
});
<?else:?>
$(function(){
  $('#rules_table').show();
});
<?endif;?>
</script>
<div markdown="1" id="rules_table" style="display:none">
<form markdown="1" name="network_rules" method="POST" action="/update.php" target="progressFrame" onsubmit="return check_rules(this)">
<input type="hidden" name="#file" value="not-used">
<input type="hidden" name="#include" value="/webGui/include/update.rules.php">
<input type="hidden" name="#cfg" value="<?=$cfg?>">
<?foreach ($link as $eth => $file):?>

_(Interface)_ <?=$eth?>:
: <select name="<?=$eth?>" onchange="update_info(this.name,this.value)">
  <?foreach ($link as $my => $val):?>
  <?=mk_option($file['mac'],$val['mac'],strtoupper($val['mac']));?>
  <?endforeach;?>
  </select><span id="info-<?=$eth?>"><?=$file['info']?></span>

<?endforeach;?>

&nbsp;
: <input type="submit" value="_(Apply)_" disabled><input type="button" value="_(Done)_" onclick="done()">
</form>

:eth_network_rules_help:

<?if (file_exists('/tmp/network-rules.tmp')):?>
<br><span class="error" style="text-align:center;padding:12px 0">_(Please **Reboot** system to make new rules active)_</span>
<?endif;?>
</div>
