Commit 6010306d authored by Judah MW's avatar Judah MW
Browse files

interface: Added DNSControl tab, form, template and logic to DNS SOA

This gives the user the ability to configure a DNSControl provider and
validates the information before serialising it as JSON and saving it in
the DB.
parent c42fb03a
Pipeline #9374 failed with stage
......@@ -231,6 +231,19 @@ class page_action extends tform_actions {
$app->tpl->setVar("edit_disabled", 0);
}
// DNSControl
if (!empty($this->dataRecord['dnscontrol_provider'])) {
$dnscontrol_provider = json_decode($this->dataRecord['dnscontrol_provider'], true);
$dnscontrol_provider_name = array_keys($dnscontrol_provider)[0];
$app->tpl->setVar('dnscontrol_provider_name', $dnscontrol_provider_name);
switch($this->dataRecord['dnscontrol_provider_type']) {
// If we implement a lot of providers, we may want to make this a class method
case 'CLOUDFLAREAPI':
$app->tpl->setVar('CLOUDFLAREAPI_apiuser', $dnscontrol_provider[$dnscontrol_provider_name]['apiuser']);
$app->tpl->setVar('CLOUDFLAREAPI_apikey', $dnscontrol_provider[$dnscontrol_provider_name]['apikey']);
break;
}
}
parent::onShowEnd();
}
......@@ -307,6 +320,70 @@ function onSubmit() {
//* server_id must be > 0
if(isset($this->dataRecord["server_id"]) && $this->dataRecord["server_id"] < 1) $app->tform->errorMessage .= $app->lng("server_id_0_error_txt");
} elseif($app->tform->getCurrentTab() == 'dns_dnscontrol') {
// Get or generate the DNSControl provider name
if (empty($this->dataRecord['dnscontrol_provider_name']) && $this->dataRecord['dnscontrol_provider_type'] != 'none') {
$origin = $app->db->queryOneRecord('SELECT origin FROM dns_soa WHERE id = ?', $this->dataRecord['id'])['origin'];
// Remove trailing dot for root zone
$origin = substr($origin, 0, -1);
$dnscontrol_provider = $this->dataRecord['dnscontrol_provider_type'] . '_' . $origin;
// Escape . and - in provider name so it can be used later in JS
$dnscontrol_provider = str_replace(array('.', '-'), array('_', '__'), $dnscontrol_provider);
} else {
$dnscontrol_provider = $this->dataRecord['dnscontrol_provider_name'];
}
unset($this->dataRecord['dnscontrol_provider_name']);
switch($this->dataRecord['dnscontrol_provider_type']) {
// If we implement a lot of providers, we may want to make this a class method
case 'CLOUDFLAREAPI':
// We have to validate manually here as the fields aren't defined in forms/dns_soa.tform.php
$dnscontrol_array = array();
$dnscontrol_array[$dnscontrol_provider] = array();
$dnscontrol_array[$dnscontrol_provider]['apiuser'] = $app->tform->filterField(
'apiuser',
$this->dataRecord['CLOUDFLAREAPI_apiuser'],
array(array('event' => 'SAVE', 'type' => 'TRIM')),
'SAVE'
);
$app->tform->validateField(
'apiuser',
$dnscontrol_array[$dnscontrol_provider]['apiuser'],
array(
array('type' => 'NOTEMPTY', 'errmsg' => 'CLOUDFLAREAPI_apiuser_error_empty'),
array('type' => 'ISEMAIL', 'errmsg' => 'CLOUDFLAREAPI_apiuser_error_invalid')
)
);
$dnscontrol_array[$dnscontrol_provider]['apikey'] = $app->tform->filterField(
'apikey',
$this->dataRecord['CLOUDFLAREAPI_apikey'],
array(array('event' => 'SAVE', 'type' => 'TRIM')),
'SAVE'
);
$app->tform->validateField(
'apikey',
$dnscontrol_array[$dnscontrol_provider]['apikey'],
array(
array('type' => 'NOTEMPTY', 'errmsg' => 'CLOUDFLAREAPI_apikey_error_empty'),
array('type' => 'REGEX', 'regex' => '/^[a-zA-Z0-9]{1,255}$/', 'errmsg' => 'CLOUDFLAREAPI_apikey_error_invalid')
)
);
break;
case 'none':
// Delete the provider
$this->dataRecord['dnscontrol_provider'] = '';
break;
default:
$app->tform->errorMessage .= $app->tform->lng('dnscontrol_provider_error_invalid').'<br />';
break;
}
// Unset all namespaced provider-specific fields
unset($this->dataRecord['CLOUDFLAREAPI_apiuser']);
unset($this->dataRecord['CLOUDFLAREAPI_apikey']);
$this->dataRecord['dnscontrol_provider'] = json_encode($dnscontrol_array, JSON_PRETTY_PRINT);
}
parent::onSubmit();
}
......
......@@ -323,6 +323,38 @@ $form["tabs"]['dns_soa'] = array (
)
);
$form["tabs"]['dns_dnscontrol'] = array(
'title' => "DNSControl",
'width' => 100,
'template' => "templates/dns_dnscontrol_edit.htm",
'fields' => array(
//#################################
// Begin Datatable fields
//#################################
'dnscontrol_provider' => array(
'datatype' => 'TEXT',
'formtype' => 'TEXTAREA',
'filters' => array(
0 => array('event' => 'SAVE', 'type' => 'STRIPTAGS'),
1 => array('event' => 'SAVE', 'type' => 'TRIM'),
),
'default' => '',
'value' => '',
'maxlength' => '10000'
),
'dnscontrol_provider_type' => array(
'datatype' => 'VARCHAR',
'formtype' => 'SELECT',
'default' => 'none',
'maxlength' => '255',
'value' => array('none' => 'None', 'CLOUDFLAREAPI' => 'Cloudflare')
)
//#################################
// END Datatable fields
//#################################
)
);
// show update acl to admins only.
if(!$app->auth->is_admin()) unset($form["tabs"]['dns_soa']['fields']['update_acl']);
......
......@@ -42,4 +42,13 @@ $wb['error_not_allowed_server_id'] = 'The selected server is not allowed for thi
$wb['soa_cannot_be_changed_txt'] = 'The Zone (SOA) can not be changed. Please contact your administrator to change the zone.';
$wb['configuration_error_txt'] = 'CONFIGURATION ERROR';
$wb['dnssec_algo_txt'] = 'DNSSEC Algorithm';
$wb['dnscontrol_provider_type_txt'] = 'DNSControl Provider';
$wb['dnscontrol_provider_docs_txt'] = 'DNSControl Provider Documentation';
$wb['dnscontrol_provider_name_txt'] = 'DNSControl Provider Name';
$wb['dnscontrol_provider_error_invalid'] = 'DNSControl provider is invalid.';
$wb['CLOUDFLAREAPI_apiuser_error_invalid'] = 'Invalid email address for Cloudflare apiuser.';
$wb['CLOUDFLAREAPI_apiuser_error_empty'] = 'Cloudflare apiuser is empty.';
$wb['CLOUDFLAREAPI_apikey_error_invalid'] = 'Invalid character in Cloudflare apikey.';
$wb['CLOUDFLAREAPI_apikey_error_empty'] = 'Cloudflare apikey is empty.';
?>
<div class="form-group">
<label for="dnscontrol_provider_type" class="col-sm-3 control-label">{tmpl_var name='dnscontrol_provider_type_txt'}</label>
<div class="col-sm-9">
<select name="dnscontrol_provider_type" id="dnscontrol_provider_type" class="form-control">{tmpl_var name='dnscontrol_provider_type'}</select>
<p><a href="https://stackexchange.github.io/dnscontrol/provider-list" target="_blank">{tmpl_var name='dnscontrol_provider_docs_txt'}</a></p>
</div>
</div>
<div class="form-group">
<label for="dnscontrol_provider_name" class="col-sm-3 control-label">{tmpl_var name='dnscontrol_provider_name_txt'}</label>
<div class="col-sm-9">
<div class="checkbox">&nbsp; &nbsp;{tmpl_var name='dnscontrol_provider_name'}</div>
</div>
</div>
<div class="form-group">
<label for="CLOUDFLAREAPI_apiuser" class="col-sm-3 control-label">apiuser</label>
<div class="col-sm-9">
<input type="text" name="CLOUDFLAREAPI_apiuser" id="CLOUDFLAREAPI_apiuser" value="{tmpl_var name='CLOUDFLAREAPI_apiuser'}" class="form-control" />
</div>
</div>
<div class="form-group">
<label for="CLOUDFLAREAPI_apikey" class="col-sm-3 control-label">apikey</label>
<div class="col-sm-9">
<input type="text" name="CLOUDFLAREAPI_apikey" id="CLOUDFLAREAPI_apikey" value="{tmpl_var name='CLOUDFLAREAPI_apikey'}" class="form-control" />
</div>
</div>
<input type="hidden" name="id" value="{tmpl_var name='id'}">
<input type="hidden" name="dnscontrol_provider_name" value="{tmpl_var name='dnscontrol_provider_name'}">
<div class="clear">
<div class="right">
<button class="btn btn-default formbutton-success" type="button" value="{tmpl_var name='btn_save_txt'}" data-submit-form="pageForm" data-form-action="dns/dns_soa_edit.php">{tmpl_var name='btn_save_txt'}</button>
<button class="btn btn-default formbutton-default" type="button" value="{tmpl_var name='btn_cancel_txt'}" data-load-content="dns/dns_soa_list.php">{tmpl_var name='btn_cancel_txt'}</button>
</div>
</div>
Supports Markdown
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