From 0584e9d74bed3a80f03032a054476c7dcd3e00ea Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Wed, 24 Feb 2016 13:27:42 +0100 Subject: [PATCH] (legacy) refactor services_unbound.php --- src/www/services_unbound.php | 565 ++++++++++++++++------------------- 1 file changed, 253 insertions(+), 312 deletions(-) diff --git a/src/www/services_unbound.php b/src/www/services_unbound.php index 2f6e929ab..0ebacd49f 100644 --- a/src/www/services_unbound.php +++ b/src/www/services_unbound.php @@ -1,30 +1,30 @@ - All rights reserved. + Copyright (C) 2014-2016 Deciso B.V. + Copyright (C) 2014 Warren Baker + All rights reserved. - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: - 1. Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. - THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, - INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY - AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, - OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - POSSIBILITY OF SUCH DAMAGE. + THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. */ require_once("guiconfig.inc"); @@ -34,317 +34,258 @@ require_once("system.inc"); require_once("pfsense-utils.inc"); require_once("interfaces.inc"); -if (!is_array($config['unbound'])) - $config['unbound'] = array(); +if (empty($config['unbound']) || !is_array($config['unbound'])) { + $config['unbound'] = array(); +} $a_unboundcfg =& $config['unbound']; -if (isset($config['unbound']['enable'])) - $pconfig['enable'] = true; -if (isset($config['unbound']['dnssec'])) - $pconfig['dnssec'] = true; -if (isset($config['unbound']['forwarding'])) - $pconfig['forwarding'] = true; -if (isset($config['unbound']['regdhcp'])) - $pconfig['regdhcp'] = true; -if (isset($config['unbound']['regdhcpstatic'])) - $pconfig['regdhcpstatic'] = true; -if (isset($config['unbound']['txtsupport'])) - $pconfig['txtsupport'] = true; +if ($_SERVER['REQUEST_METHOD'] === 'GET') { + $pconfig = array(); + // boolean values + $pconfig['enable'] = isset($a_unboundcfg['enable']); + $pconfig['dnssec'] = isset($a_unboundcfg['dnssec']); + $pconfig['forwarding'] = isset($a_unboundcfg['forwarding']); + $pconfig['regdhcp'] = isset($a_unboundcfg['regdhcp']); + $pconfig['regdhcpstatic'] = isset($a_unboundcfg['regdhcpstatic']); + $pconfig['txtsupport'] = isset($a_unboundcfg['txtsupport']); + // text values + $pconfig['port'] = !empty($a_unboundcfg['port']) ? $a_unboundcfg['port'] : null; + $pconfig['custom_options'] = !empty($a_unboundcfg['custom_options']) ? $a_unboundcfg['custom_options'] : null; + // array types + $pconfig['active_interface'] = !empty($a_unboundcfg['active_interface']) ? explode(",", $a_unboundcfg['active_interface']) : array(); + $pconfig['outgoing_interface'] = !empty($a_unboundcfg['outgoing_interface']) ? explode(",", $a_unboundcfg['outgoing_interface']) : array(); +} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') { + $input_errors = array(); + $pconfig = $_POST; -$pconfig['port'] = $config['unbound']['port']; -$pconfig['custom_options'] = $config['unbound']['custom_options']; + if (!empty($pconfig['apply'])) { + services_unbound_configure(); + clear_subsystem_dirty('unbound'); + /* Update resolv.conf in case the interface bindings exclude localhost. */ + system_resolvconf_generate(); + header("Location: services_unbound.php"); + exit; + } else { + // perform validations + if (isset($pconfig['enable']) && isset($config['dnsmasq']['enable'])) { + $input_errors[] = gettext("The DNS Forwarder is still active. Disable it before enabling the DNS Resolver."); + } + if (empty($pconfig['active_interface'])) { + $input_errors[] = gettext("A single network interface needs to be selected for the DNS Resolver to bind to."); + } + if (empty($pconfig['outgoing_interface'])) { + $input_errors[] = gettext("A single outgoing network interface needs to be selected for the DNS Resolver to use for outgoing DNS requests."); + } + if (!is_port($pconfig['port'])) { + $input_errors[] = gettext("You must specify a valid port number."); + } -if (empty($config['unbound']['active_interface'])) - $pconfig['active_interface'] = array(); -else - $pconfig['active_interface'] = explode(",", $config['unbound']['active_interface']); -if (empty($config['unbound']['outgoing_interface'])) - $pconfig['outgoing_interface'] = array(); -else - $pconfig['outgoing_interface'] = explode(",", $config['unbound']['outgoing_interface']); + if (count($input_errors) == 0) { + // save form data + // text types + if (!empty($pconfig['port'])) { + $a_unboundcfg['port'] = $pconfig['port']; + } elseif (isset($a_unboundcfg['port'])) { + unset($a_unboundcfg['port']); + } + $a_unboundcfg['custom_options'] = !empty($pconfig['custom_options']) ? str_replace("\r\n", "\n", $pconfig['custom_options']) : null; + // boolean values + $a_unboundcfg['enable'] = !empty($pconfig['enable']); + $a_unboundcfg['dnssec'] = !empty($pconfig['dnssec']); + $a_unboundcfg['forwarding'] = !empty($pconfig['forwarding']); + $a_unboundcfg['regdhcp'] = !empty($pconfig['regdhcp']); + $a_unboundcfg['regdhcpstatic'] = !empty($pconfig['regdhcpstatic']); + $a_unboundcfg['txtsupport'] = !empty($pconfig['txtsupport']); -if ($_POST) { - unset($input_errors); + // array types + $a_unboundcfg['active_interface'] = !empty( $pconfig['active_interface']) ? implode(",", $pconfig['active_interface']) : array(); + $a_unboundcfg['outgoing_interface'] = !empty( $pconfig['outgoing_interface']) ? implode(",", $pconfig['outgoing_interface']) : array(); - if ($_POST['apply']) { - $retval = services_unbound_configure(); - $savemsg = get_std_save_message(); - if ($retval == 0) { - clear_subsystem_dirty('unbound'); - } - /* Update resolv.conf in case the interface bindings exclude localhost. */ - system_resolvconf_generate(); - } else { - $pconfig = $_POST; - - if (isset($_POST['enable']) && isset($config['dnsmasq']['enable'])) - $input_errors[] = gettext("The DNS Forwarder is still active. Disable it before enabling the DNS Resolver."); - - if (empty($_POST['active_interface'])) - $input_errors[] = gettext("A single network interface needs to be selected for the DNS Resolver to bind to."); - - if (empty($_POST['outgoing_interface'])) - $input_errors[] = gettext("A single outgoing network interface needs to be selected for the DNS Resolver to use for outgoing DNS requests."); - - if ($_POST['port']) - if (is_port($_POST['port'])) - $a_unboundcfg['port'] = $_POST['port']; - else - $input_errors[] = gettext("You must specify a valid port number."); - else if (isset($config['unbound']['port'])) - unset($config['unbound']['port']); - - if (isset($_POST['enable'])) - $a_unboundcfg['enable'] = true; - else - unset($a_unboundcfg['enable']); - if (isset($_POST['dnssec'])) - $a_unboundcfg['dnssec'] = true; - else - unset($a_unboundcfg['dnssec']); - if (isset($_POST['forwarding'])) - $a_unboundcfg['forwarding'] = true; - else - unset($a_unboundcfg['forwarding']); - if (isset($_POST['regdhcp'])) - $a_unboundcfg['regdhcp'] = true; - else - unset($a_unboundcfg['regdhcp']); - if (isset($_POST['regdhcpstatic'])) - $a_unboundcfg['regdhcpstatic'] = true; - else - unset($a_unboundcfg['regdhcpstatic']); - if (isset($_POST['txtsupport'])) - $a_unboundcfg['txtsupport'] = true; - else - unset($a_unboundcfg['txtsupport']); - if (is_array($_POST['active_interface']) && !empty($_POST['active_interface'])) - $a_unboundcfg['active_interface'] = implode(",", $_POST['active_interface']); - - if (is_array($_POST['outgoing_interface']) && !empty($_POST['outgoing_interface'])) - $a_unboundcfg['outgoing_interface'] = implode(",", $_POST['outgoing_interface']); - - $a_unboundcfg['custom_options'] = str_replace("\r\n", "\n", $_POST['custom_options']); - - if (!$input_errors) { - write_config("DNS Resolver configured."); - mark_subsystem_dirty('unbound'); - } - } + write_config("DNS Resolver configured."); + mark_subsystem_dirty('unbound'); + header("Location: services_unbound.php"); + exit; + } + } } + $service_hook = 'unbound'; - +legacy_html_escape_form_data($pconfig); include_once("head.inc"); - ?> - - +
+
+
+ 0) print_input_errors($input_errors); ?> + +
+ " . gettext("You must apply the changes in order for them to take effect."));?>
+ +
+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + +
+ /> + +
+ + +
+ + +
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

- onclick="enable_change(false)" /> -
-

-
-

- /> -

- -

-
- - -

- -

-
- - -

- -

-

- /> -
-

-

- /> -

-

- /> -
-
','')?>

-

- /> -
-
','');?>

-

- /> -
-

-
-
> - " /> - -
-
> -
-
-
-
-
- " onclick="enable_change(true)" /> -
- - -
-


-
','');?>
-

-
- - - - - - - - - - - - + + +
+ /> + +
+ /> + +
+ /> + + +
+ /> + + +
+ /> + +
+
> + " /> - +
+
> +
+
+
+
+
+ " /> +
+
+
+
+
','');?> +

+
+
+
+ +
+
+