header("Location: services_captiveportal_zones.php");
    exit;
}
if (!is_array($config['captiveportal'])) {
    $config['captiveportal'] = array();
}
$a_cp =& $config['captiveportal'];
$pgtitle = array(gettext("Services"), gettext("Captive portal"), $a_cp[$cpzone]['zone']);
$shortcut_section = "captiveportal";
if ($_GET['act'] == "del") {
    $a_allowedips =& $config['captiveportal'][$cpzone]['allowedip'];
    if ($a_allowedips[$_GET['id']]) {
        $ipent = $a_allowedips[$_GET['id']];
        if (isset($config['captiveportal'][$cpzone]['enable'])) {
            $mask = !empty($ipent['sn']) ? $ipent['sn'] : 32;
            $ipfw = pfSense_ipfw_getTablestats($cpzone, 3, $ipent['ip'], $mask);
            pfSense_ipfw_Tableaction($cpzone, IP_FW_TABLE_XDEL, 3, $ipent['ip'], $mask);
            pfSense_ipfw_Tableaction($cpzone, IP_FW_TABLE_XDEL, 4, $ipent['ip'], $mask);
            if (is_array($ipfw)) {
                captiveportal_free_dn_ruleno($ipfw['dnpipe']);
                pfSense_pipe_action("pipe delete {$ipfw['dnpipe']}");
                pfSense_pipe_action("pipe delete " . ($ipfw['dnpipe'] + 1));
            }
        }
        unset($a_allowedips[$_GET['id']]);
        write_config();
        header("Location: services_captiveportal_ip.php?zone={$cpzone}");
        exit;
    }
}
include "head.inc";
}
$pgtitle = array(gettext("Services"), gettext("Captive Portal"), "Zone " . $a_cp[$cpzone]['zone'], gettext("Allowed Hostnames"));
$shortcut_section = "captiveportal";
if ($_GET['act'] == "del" && !empty($cpzone) && isset($cpzoneid)) {
    $a_allowedhostnames =& $a_cp[$cpzone]['allowedhostname'];
    if ($a_allowedhostnames[$_GET['id']]) {
        $ipent = $a_allowedhostnames[$_GET['id']];
        if (isset($a_cp[$cpzone]['enable'])) {
            if (is_ipaddr($ipent['hostname'])) {
                $ip = $ipent['hostname'];
            } else {
                $ip = gethostbyname($ipent['hostname']);
            }
            $sn = is_ipaddrv6($ip) ? 128 : 32;
            if (is_ipaddr($ip)) {
                $ipfw = pfSense_ipfw_getTablestats($cpzoneid, IP_FW_TABLE_XLISTENTRY, 3, $ip);
                if (is_array($ipfw)) {
                    captiveportal_free_dn_ruleno($ipfw['dnpipe']);
                    pfSense_pipe_action("pipe delete {$ipfw['dnpipe']}");
                    pfSense_pipe_action("pipe delete " . ($ipfw['dnpipe'] + 1));
                }
                pfSense_ipfw_Tableaction($cpzoneid, IP_FW_TABLE_XDEL, 3, $ip, $sn);
                pfSense_ipfw_Tableaction($cpzoneid, IP_FW_TABLE_XDEL, 4, $ip, $sn);
            }
        }
        unset($a_allowedhostnames[$_GET['id']]);
        write_config();
        captiveportal_allowedhostname_configure();
        header("Location: services_captiveportal_hostname.php?zone={$cpzone}");
        exit;
    }
         $oldmask = $a_allowedips[$id]['sn'];
     } else {
         $oldmask = 32;
     }
     $a_allowedips[$id] = $ip;
 } else {
     $a_allowedips[] = $ip;
 }
 allowedips_sort();
 write_config();
 if (isset($a_cp[$cpzone]['enable']) && is_module_loaded("ipfw.ko")) {
     $rules = "";
     $cpzoneid = $a_cp[$cpzone]['zoneid'];
     unset($ipfw);
     if (isset($oldip) && isset($oldmask)) {
         $ipfw = pfSense_ipfw_getTablestats($cpzoneid, 3, $oldip, $oldmask);
         $rules .= "table 3 delete {$oldip}/{$oldmask}\n";
         $rules .= "table 4 delete {$oldip}/{$oldmask}\n";
         if (is_array($ipfw)) {
             $rules .= "pipe delete {$ipfw['dnpipe']}\n";
             $rules .= "pipe delete " . ($ipfw['dnpipe'] + 1 . "\n");
         }
     }
     $rules .= captiveportal_allowedip_configure_entry($ip);
     if (is_array($ipfw)) {
         captiveportal_free_dn_ruleno($ipfw['dnpipe']);
     }
     $uniqid = uniqid("{$cpzone}_allowed");
     @file_put_contents("{$g['tmp_path']}/{$uniqid}_tmp", $rules);
     mwexec("/sbin/ipfw -x {$cpzoneid} -q {$g['tmp_path']}/{$uniqid}_tmp");
     @unlink("{$g['tmp_path']}/{$uniqid}_tmp");