Skip to content

Commit

Permalink
firewall: GeoIP alias edit rework #1860
Browse files Browse the repository at this point in the history
  • Loading branch information
fichtner committed Oct 25, 2017
1 parent 4976bfd commit b3d1e05
Showing 1 changed file with 79 additions and 61 deletions.
140 changes: 79 additions & 61 deletions src/www/firewall_aliases_edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ function geoip_countries()
$result[$code] = $name;
}
}
uasort($result, function($a, $b) {return strcasecmp($a, $b);});
return $result;
}

Expand Down Expand Up @@ -181,36 +182,41 @@ function geoip_regions()
if (isset($pconfig['submit'])) {
$input_errors = array();
// validate data
$country_codes = array_keys(geoip_countries());
foreach ($pconfig['host_url'] as &$detail_entry) {
$ipaddr_count = 0;
$domain_alias_count = 0;
foreach (explode('-', $detail_entry) as $tmpaddr) {
if (is_ipaddr($tmpaddr)) {
$ipaddr_count++;
} elseif (trim($tmpaddr) != "") {
$domain_alias_count++;
}
}
if ($pconfig['type'] == 'host') {
if ($ipaddr_count > 1) {
$input_errors[] = sprintf(gettext('Entry "%s" seems to contain a list of addresses, please use a network type alias to define ranges.'), $detail_entry) ;
} elseif (!is_domain($detail_entry) && !is_ipaddr($detail_entry) && !is_alias($detail_entry)) {
$input_errors[] = sprintf(gettext('Entry "%s" is not a valid hostname or IP address.'), $detail_entry) ;
}
} elseif ($pconfig['type'] == 'port') {
$detail_entry = str_replace("-", ":", $detail_entry);
if (!is_port($detail_entry) && !is_portrange($detail_entry) && !is_alias($detail_entry)) {
$input_errors[] = sprintf(gettext('Entry "%s" is not a valid port number.'), $detail_entry) ;
}
} elseif ($pconfig['type'] == 'geoip') {
if (!in_array($detail_entry, $country_codes)) {
$input_errors[] = sprintf(gettext('Entry "%s" is not a valid country code.'), $detail_entry) ;

if (empty($pconfig['host_url'])) {
$input_errors[] = gettext('At least one alias entry must be supplied.');
} else {
$country_codes = array_keys(geoip_countries());
foreach ($pconfig['host_url'] as &$detail_entry) {
$ipaddr_count = 0;
$domain_alias_count = 0;
foreach (explode('-', $detail_entry) as $tmpaddr) {
if (is_ipaddr($tmpaddr)) {
$ipaddr_count++;
} elseif (trim($tmpaddr) != "") {
$domain_alias_count++;
}
}
} elseif ($pconfig['type'] == 'network') {
if (!is_alias($detail_entry) && !is_ipaddr($detail_entry) && !is_subnet($detail_entry)
&& !($ipaddr_count == 2 && $domain_alias_count == 0)) {
$input_errors[] = sprintf(gettext('Entry "%s" is not a valid network or IP address.'), $detail_entry) ;
if ($pconfig['type'] == 'host') {
if ($ipaddr_count > 1) {
$input_errors[] = sprintf(gettext('Entry "%s" seems to contain a list of addresses, please use a network type alias to define ranges.'), $detail_entry);
} elseif (!is_domain($detail_entry) && !is_ipaddr($detail_entry) && !is_alias($detail_entry)) {
$input_errors[] = sprintf(gettext('Entry "%s" is not a valid hostname or IP address.'), $detail_entry);
}
} elseif ($pconfig['type'] == 'port') {
$detail_entry = str_replace("-", ":", $detail_entry);
if (!is_port($detail_entry) && !is_portrange($detail_entry) && !is_alias($detail_entry)) {
$input_errors[] = sprintf(gettext('Entry "%s" is not a valid port number.'), $detail_entry);
}
} elseif ($pconfig['type'] == 'geoip') {
if (!in_array($detail_entry, $country_codes)) {
$input_errors[] = sprintf(gettext('Entry "%s" is not a valid country code.'), $detail_entry);
}
} elseif ($pconfig['type'] == 'network') {
if (!is_alias($detail_entry) && !is_ipaddr($detail_entry) && !is_subnet($detail_entry)
&& !($ipaddr_count == 2 && $domain_alias_count == 0)) {
$input_errors[] = sprintf(gettext('Entry "%s" is not a valid network or IP address.'), $detail_entry);
}
}
}
}
Expand Down Expand Up @@ -408,10 +414,6 @@ function addFieldTypeAhead() {
$(".act-removerow").click(removeRow);
// link typeahead to new item
addFieldTypeAhead();
// link geoip list to new item
$(".geoip_list").change(function(){
$(this).parent().find('input').val($(this).val());
});
});

$(".act-removerow").click(removeRow);
Expand All @@ -430,9 +432,9 @@ function toggleType() {
$('.act-removerow').removeClass('hidden');
}
$("#proto").addClass("hidden");
$(".geoip_list").addClass("hidden");
$(".geoip_table").addClass("hidden");
$(".not_geoip_table").removeClass("hidden");
$(".host_url").removeClass("hidden");
$(".geoip_list > option").remove();
switch($("#typeSelect").val()) {
case 'urltable':
$("#detailsHeading1").html("<?=gettext("URL");?>");
Expand All @@ -457,17 +459,9 @@ function toggleType() {
break;
case 'geoip':
$("#proto").removeClass("hidden");
$(".geoip_list").removeClass("hidden");
$(".geoip_table").removeClass("hidden");
$(".not_geoip_table").addClass("hidden");
$(".host_url").addClass("hidden");
$("#detailsHeading1").html("<?=gettext("Country");?>");
$("#countries > option").clone().appendTo('.geoip_list');
$('.geoip_list').each(function(){
var url_item = $(this).parent().find('input').val();
$(this).val(url_item);
});
$('.geoip_list').change(function(){
$(this).parent().find('input').val($(this).val());
});
break;
}
$(".fld_detail").typeahead("destroy");
Expand All @@ -487,6 +481,10 @@ function toggleType() {
document.all_aliases[$(this).data('type')].push($(this).val())
});

$('.region_toggle').click(function () {
$('.region_' + $(this).attr('data')).click();
});

toggleType();
});
</script>
Expand All @@ -502,16 +500,6 @@ function toggleType() {
endforeach;
endif;
?>
</select>

<!-- push all available countries in a hidden select box for geoip -->
<select class="hidden" id="countries">
<?php
foreach (geoip_countries() as $code => $name):?>
<option value="<?=$code;?>"><?=$name;?></option>
<?php
endforeach;
?>
</select>

<section class="page-content-main">
Expand Down Expand Up @@ -605,7 +593,7 @@ function toggleType() {
<tr>
<td><div id="addressnetworkport"><i class="fa fa-info-circle text-muted"></i> <?= gettext('Aliases') ?></div></td>
<td>
<table class="table table-striped table-condensed" id="detailTable">
<table class="table table-striped table-condensed not_geoip_table" id="detailTable">
<thead>
<tr>
<th></th>
Expand All @@ -621,11 +609,6 @@ function toggleType() {
<td>
<div style="cursor:pointer;" class="act-removerow btn btn-default btn-xs" alt="remove"><span class="glyphicon glyphicon-minus"></span></div>
</td>
<td>
<select class="geoip_list hidden">
</select>
<input type="text" class="host_url fld_detail" name="host_url[]" value="<?=$aliasurl;?>"/>
</td>
<td>
<input type="text" class="form-control" name="detail[]" value="<?= isset($pconfig['detail'][$aliasid])?$pconfig['detail'][$aliasid]:"";?>">
</td>
Expand All @@ -648,6 +631,41 @@ function toggleType() {
</tr>
</tfoot>
</table>
<?php
$where = geoip_regions();
$unique = array_unique($where);
uasort($unique , function($a, $b) {return strcasecmp($a, $b);});
foreach ($unique as $region): ?>
<table class="table table-striped table-condensed geoip_table">
<tr>
<td colspan="3">
<input type="checkbox" class="region_toggle" data="<?= html_safe($region) ?>" />
<strong><?= $region ?> (<?= gettext('toggle all') ?>)</strong>
</td>
</tr>
<tr>
<?php $i = 0; foreach (geoip_countries() as $code => $name):
if ($region == $where[$code]): ?>
<td>
<input type="checkbox" name="host_url[]" class="region_<?= html_safe($region) ?>" value="<?= html_safe($code) ?>" <?= in_array($code, $pconfig['host_url']) ? 'checked="checked"' : '' ?>/>
<strong><?= $name ?></strong>
</td>
<?php $i += 1;
if ($i % 3 == 0): ?>
</tr>
<tr>
<?php endif;
endif;
endforeach; ?>
<?php for ($i %= 3; $i < 3; $i += 1): ?>
</td><td>
<?php
endfor; ?>
</td>
</tr>
</table>
<br class="geoip_table"/>
<?php endforeach; ?>
</td>
</tr>
<tr>
Expand Down

0 comments on commit b3d1e05

Please sign in to comment.