Commit 447bb4f5 authored by Ad Schellevis's avatar Ad Schellevis Committed by Franco Fichtner

refactor services_opendns.php

(cherry picked from commit 2d8f854e)
parent 1d6caaf8
<?php <?php
/* /*
Copyright (c) 2015 Franco Fichtner <franco@opnsense.org> Copyright (c) 2015 Franco Fichtner <franco@opnsense.org>
Copyright (c) 2008 Tellnet AG Copyright (c) 2008 Tellnet AG
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met: modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, 1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer. this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright 2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution. documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 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 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE. POSSIBILITY OF SUCH DAMAGE.
*/ */
require_once("guiconfig.inc"); require_once("guiconfig.inc");
...@@ -33,203 +33,200 @@ require_once("pfsense-utils.inc"); ...@@ -33,203 +33,200 @@ require_once("pfsense-utils.inc");
require_once("services.inc"); require_once("services.inc");
require_once("interfaces.inc"); require_once("interfaces.inc");
if (!is_array($config['opendns'])) { if (empty($config['opendns']) || !is_array($config['opendns'])) {
$config['opendns'] = array(); $config['opendns'] = array();
} }
$pconfig['enable'] = isset($config['opendns']['enable']); if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$pconfig['username'] = $config['opendns']['username']; $pconfig['enable'] = isset($config['opendns']['enable']);
$pconfig['password'] = $config['opendns']['password']; $pconfig['username'] = !empty($config['opendns']['username']) ? $config['opendns']['username'] : null;
$pconfig['host'] = $config['opendns']['host']; $pconfig['password'] = !empty($config['opendns']['password']) ? $config['opendns']['password'] : null;
$pconfig['host'] = !empty($config['opendns']['host']) ? $config['opendns']['host'] : null;
if ($_POST) { } elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
unset($input_errors); $input_errors = array();
$pconfig = $_POST; $pconfig = $_POST;
/* input validation */ /* input validation */
$reqdfields = array(); $reqdfields = array();
$reqdfieldsn = array(); $reqdfieldsn = array();
if ($_POST['enable']) { if (!empty($pconfig['enable'])) {
$reqdfields = array_merge($reqdfields, explode(" ", "host username password")); $reqdfields = array_merge($reqdfields, explode(" ", "host username password"));
$reqdfieldsn = array_merge($reqdfieldsn, explode(",", "Network,Username,Password")); $reqdfieldsn = array_merge($reqdfieldsn, explode(",", "Network,Username,Password"));
} }
do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors); do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
if (($_POST['host'] && !is_domain($_POST['host']))) { if (!empty($pconfig['host']) && !is_domain($pconfig['host'])) {
$input_errors[] = 'The host name contains invalid characters.'; $input_errors[] = 'The host name contains invalid characters.';
} }
if (($_POST['username'] && empty($_POST['username']))) { if (empty($pconfig['username'])) {
$input_errors[] = 'The username cannot be empty.'; $input_errors[] = 'The username cannot be empty.';
} }
if ($_POST['test']) { if (!empty($pconfig['test'])) {
$ch = curl_init(); $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, sprintf( 'https://updates.opendns.com/nic/update?hostname=%s', $pconfig['host'])); curl_setopt($ch, CURLOPT_URL, sprintf( 'https://updates.opendns.com/nic/update?hostname=%s', $pconfig['host']));
curl_setopt($ch, CURLOPT_USERPWD, sprintf('%s:%s', $pconfig['username'], $pconfig['password'])); curl_setopt($ch, CURLOPT_USERPWD, sprintf('%s:%s', $pconfig['username'], $pconfig['password']));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch); $output = curl_exec($ch);
curl_close($ch); curl_close($ch);
$test_results = explode("\r\n", $output); $test_results = explode("\r\n", $output);
} elseif (!$input_errors) { } elseif (count($input_errors) == 0) {
$refresh = $pconfig['enable'] != $config['opendns']['enable']; $refresh = $pconfig['enable'] != $config['opendns']['enable'];
$config['opendns']['enable'] = $_POST['enable'] ? true : false; $config['opendns']['enable'] = !empty($pconfig['enable']);
$config['opendns']['username'] = $_POST['username']; $config['opendns']['username'] = $pconfig['username'];
$config['opendns']['password'] = $_POST['password']; $config['opendns']['password'] = $pconfig['password'];
$config['opendns']['host'] = $_POST['host']; $config['opendns']['host'] = $pconfig['host'];
if ($refresh) { if ($refresh) {
if ($config['opendns']['enable']) { if ($config['opendns']['enable']) {
unset($config['system']['dnsserver']); $config['system']['dnsserver'] = array();
$v4_server = array('208.67.222.222', '208.67.220.220'); $v4_server = array('208.67.222.222', '208.67.220.220');
$v6_server = array('2620:0:ccc::2', '2620:0:ccd::2'); $v6_server = array('2620:0:ccc::2', '2620:0:ccd::2');
if (isset($config['system']['prefer_ipv4'])) { if (isset($config['system']['prefer_ipv4'])) {
$config['system']['dnsserver'][] = $v4_server[0]; $config['system']['dnsserver'][] = $v4_server[0];
$config['system']['dnsserver'][] = $v4_server[1]; $config['system']['dnsserver'][] = $v4_server[1];
if (isset($config['system']['ipv6allow'])) { if (isset($config['system']['ipv6allow'])) {
$config['system']['dnsserver'][] = $v6_server[0]; $config['system']['dnsserver'][] = $v6_server[0];
$config['system']['dnsserver'][] = $v6_server[1]; $config['system']['dnsserver'][] = $v6_server[1];
} }
} else { } else {
if (isset($config['system']['ipv6allow'])) { if (isset($config['system']['ipv6allow'])) {
$config['system']['dnsserver'][] = $v6_server[0]; $config['system']['dnsserver'][] = $v6_server[0];
$config['system']['dnsserver'][] = $v6_server[1]; $config['system']['dnsserver'][] = $v6_server[1];
} }
$config['system']['dnsserver'][] = $v4_server[0]; $config['system']['dnsserver'][] = $v4_server[0];
$config['system']['dnsserver'][] = $v4_server[1]; $config['system']['dnsserver'][] = $v4_server[1];
} }
$config['system']['dnsallowoverride'] = false; $config['system']['dnsallowoverride'] = false;
} else { } else {
unset($config['system']['dnsserver']); $config['system']['dnsserver'] = array();
$config['system']['dnsserver'][] = ''; $config['system']['dnsserver'][] = '';
$config['system']['dnsallowoverride'] = true; $config['system']['dnsallowoverride'] = true;
} }
} }
write_config('OpenDNS filter configuration change'); write_config('OpenDNS filter configuration change');
if ($refresh) { if ($refresh) {
$retval = system_resolvconf_generate(); $retval = system_resolvconf_generate();
$savemsg = get_std_save_message(); $savemsg = get_std_save_message();
} }
} }
} }
legacy_html_escape_form_data($pconfig);
include 'head.inc'; include 'head.inc';
?> ?>
<body> <body>
<?php include 'fbegin.inc'; ?> <?php include 'fbegin.inc'; ?>
<section class="page-content-main"> <section class="page-content-main">
<div class="container-fluid"> <div class="container-fluid">
<div class="row">
<div class="row"> <?php
<?php if (isset($input_errors) && count($input_errors) > 0) {
if (isset($input_errors) && count($input_errors) > 0) { print_input_errors($input_errors);
print_input_errors($input_errors); }
} if (isset($savemsg)) {
if (isset($savemsg)) { print_info_box($savemsg);
print_info_box($savemsg); }?>
} <section class="col-xs-12">
?> <div class="content-box table-responsive">
<section class="col-xs-12"> <form method="post">
<table class="table table-striped">
<div class="content-box table-responsive"> <thead>
<tr>
<form action="services_opendns.php" method="post"> <td width="22%"><strong><?=gettext('OpenDNS Setup'); ?></strong></td>
<table width="100%" border="0" cellpadding="6" cellspacing="0" summary="main area" class="table table-striped"> <td width="78%" align="right">
<thead> <small><?=gettext("full help"); ?> </small>
<tr> <i class="fa fa-toggle-off text-danger" style="cursor: pointer;" id="show_all_help_page" type="button"></i></a>
<th colspan="2" valign="top" class="listtopic"><?=gettext('OpenDNS Setup'); ?></th> &nbsp;
</tr> </td>
</thead> </tr>
<tbody> </thead>
<tr> <tbody>
<td width="22%" valign="top" class="vncellreq"><?=gettext('Enable'); ?></td> <tr>
<td width="78%" class="vtable"> <td><a id="help_for_enable" href="#" class="showhelp"><i class="fa fa-info-circle"></i></a> <?=gettext('Enable'); ?></td>
<input name="enable" type="checkbox" id="enable" value="yes" <?php if ($pconfig['enable']) { echo 'checked="checked"'; } ?>" /> <td>
<strong><?=gettext('Filter DNS requests using OpenDNS'); ?></strong> <input name="enable" type="checkbox" id="enable" value="yes" <?=!empty($pconfig['enable']) ? 'checked="checked"' : "";?> />
<br /> <strong><?=gettext('Filter DNS requests using OpenDNS'); ?></strong>
<br /> <div class="hidden" for="help_for_enable">
<span class="vexpl"> <?=gettext(sprintf(
<?=gettext(sprintf( 'Enabling the OpenDNS service will overwrite DNS servers configured ' .
'Enabling the OpenDNS service will overwrite DNS servers configured ' . 'via the General Setup page as well as ignore any DNS servers learned ' .
'via the General Setup page as well as ignore any DNS servers learned ' . 'by DHCP/PPP on WAN and use the DNS servers from %s instead.',
'by DHCP/PPP on WAN and use the DNS servers from %s instead.', '<a href="http://www.opendns.com" target="_blank">OpenDNS.com</a>'
'<a href="http://www.opendns.com" target="_blank">OpenDNS.com</a>' )); ?>
)); ?> </div>
</span> </td>
</td> </tr>
</tr> <tr>
<tr> <td><a id="help_for_username" href="#" class="showhelp"><i class="fa fa-info-circle"></i></a> <?=gettext('Username'); ?></td>
<td width="22%" valign="top" class="vncell"><?=gettext('Username'); ?></td> <td>
<td width="78%" class="vtable"> <input name="username" type="text" id="username" size="20" value="<?=$pconfig['username'];?>" />
<input name="username" type="text" id="username" size="20" value="<?=htmlspecialchars($pconfig['username']);?>" /> <div class="hidden" for="help_for_username">
<br /> <?=gettext(
<span class="vexpl"> 'Signon Username to log into your OpenDNS dashboard. ' .
<?=gettext( 'It is used to automatically update the IP address of ' .
'Signon Username to log into your OpenDNS dashboard. ' . 'the registered network.'
'It is used to automatically update the IP address of ' . ); ?>
'the registered network.' </div>
); ?> </td>
</span> </tr>
</td> <tr>
</tr> <td><i class="fa fa-info-circle text-muted"></i> <?=gettext('Password'); ?></td>
<tr> <td>
<td width="22%" valign="top" class="vncell"><?=gettext('Password'); ?></td> <input name="password" type="password" id="password" size="20" value="<?=$pconfig['password'];?>" />
<td width="78%" class="vtable"> </td>
<input name="password" type="password" id="password" size="20" value="<?=htmlspecialchars($pconfig['password']);?>" /> </tr>
</td> <tr>
</tr> <td><a id="help_for_host" href="#" class="showhelp"><i class="fa fa-info-circle"></i></a> <?=gettext('Network'); ?></td>
<tr> <td>
<td width="22%" valign="top" class="vncell"><?=gettext('Network'); ?></td> <input name="host" type="text" id="host" size="30" value="<?=$pconfig['host'];?>" />
<td width="78%" class="vtable"> <div class="hidden" for="help_for_host">
<input name="host" type="text" id="host" size="30" value="<?=htmlspecialchars($pconfig['host']);?>" /> <?=gettext(sprintf(
<br /> 'Enter the network name configured on the %s under ' .
<span class="vexpl"> '\'Manage your networks\'. Used to update the node\'s ' .
<?=gettext(sprintf( 'IP address whenever the WAN interface changes its IP address.',
'Enter the network name configured on the %s under ' . '<a href="https://www.opendns.com/dashboard/networks/" target="_blank">' .
'\'Manage your networks\'. Used to update the node\'s ' . gettext('Networks Dashboard of OpenDNS') .'</a>'
'IP address whenever the WAN interface changes its IP address.', )); ?>
'<a href="https://www.opendns.com/dashboard/networks/" target="_blank">' . </div>
gettext('Networks Dashboard of OpenDNS') .'</a>' </td>
)); ?> </tr>
</span> <?php
</td> if (isset($test_results) && is_array($test_results)): ?>
</tr> <tr>
<?php if (is_array($test_results)): ?> <td><i class="fa fa-info-circle text-muted"></i> <?=gettext('Test result');?></td>
<tr> <td>
<td width="22%" valign="top"><?=gettext('Test result');?></td> <?php
<td width="78%"> foreach ($test_results as $result) {
<?php if (!strlen($result)) {
foreach ($test_results as $result) { continue;
if (!strlen($result)) { }
continue;
} echo sprintf(
'<span class="glyphicon glyphicon-%s"></span> %s<br />',
echo sprintf( strpos($result, 'good') === 0 ? 'ok text-success' : 'remove text-danger',
'<span class="glyphicon glyphicon-%s"></span> %s<br />', $result
strpos($result, 'good') === 0 ? 'ok text-success' : 'remove text-danger', );
$result }?>
); </td>
} </tr>
?> <?php
</td> endif; ?>
</tr> <tr>
<?php endif; ?> <td>&nbsp;</td>
<tr> <td>
<td width="22%" valign="top">&nbsp;</td> <input name="submit" type="submit" class="btn btn-primary" value="<?=gettext('Save');?>" />
<td width="78%"> <input name="test" type="submit" class="btn btn-primary" value="<?=gettext('Test/Update');?>" />
<input name="submit" type="submit" class="btn btn-primary" value="<?=gettext('Save');?>" /> </td>
<input name="test" type="submit" class="btn btn-primary" value="<?=gettext('Test/Update');?>" /> </tr>
</td> </tbody>
</tr> </table>
</tbody> </form>
</table> </div>
</form> </section>
</div> </div>
</section> </div>
</div>
</div>
</section> </section>
<?php include 'foot.inc'; ?> <?php include 'foot.inc'; ?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment