Fix creating records without the zonename. Sort the zones. Fix creation of disabled records

This commit is contained in:
Mark Schouten 2016-08-04 14:46:32 +02:00
parent 82819bf33b
commit ef7f47e3f4
2 changed files with 36 additions and 9 deletions

View file

@ -126,17 +126,15 @@ class Zone {
} }
} }
public function addrrset($name, $type, $content, $disbled, $ttl = 3600) { public function addrrset($name, $type, $content, $disabled = FALSE, $ttl = 3600) {
if ($this->getrrset($name, $type) !== FALSE) { if ($this->getrrset($name, $type) !== FALSE) {
throw new Exception("This rrset already exists."); throw new Exception("This rrset already exists.");
} }
$rrset = new RRSet($name, $type, $content, $ttl); $rrset = new RRSet($name, $type, $content, $disabled, $ttl);
array_push($this->rrsets, $rrset); array_push($this->rrsets, $rrset);
} }
public function addrecord($name, $type, $content, $disabled = FALSE, $ttl = 3600) { public function addrecord($name, $type, $content, $disabled = FALSE, $ttl = 3600) {
$found = FALSE;
$rrset = $this->getrrset($name, $type); $rrset = $this->getrrset($name, $type);
if ($rrset) { if ($rrset) {
@ -227,7 +225,7 @@ class Zone {
} }
class RRSet { class RRSet {
public function __construct($name = '', $type = '', $content = '', $ttl = 3600) { public function __construct($name = '', $type = '', $content = '', $disabled = FALSE, $ttl = 3600) {
$this->name = $name; $this->name = $name;
$this->type = $type; $this->type = $type;
$this->ttl = $ttl; $this->ttl = $ttl;
@ -236,7 +234,7 @@ class RRSet {
$this->comments = Array(); $this->comments = Array();
if (isset($content) and $content != '') { if (isset($content) and $content != '') {
$this->addRecord($content); $this->addRecord($content, $disabled);
} }
} }

View file

@ -21,7 +21,7 @@ function is_ascii($string) {
} }
function _valid_label($name) { function _valid_label($name) {
return is_ascii($name) && ( bool ) preg_match("/^([-.a-z0-9_\/\*]+)?$/i", $name ); return is_ascii($name) && ( bool ) preg_match("/^([-.a-z0-9_\/\*]+)?.$/i", $name );
} }
function decode_record_id($id) { function decode_record_id($id) {
@ -161,6 +161,7 @@ case "listslaves":
array_push($return, $zone->export()); array_push($return, $zone->export());
} }
} }
usort($return, "zone_compare");
jtable_respond($return); jtable_respond($return);
break; break;
@ -172,7 +173,6 @@ case "listrecords":
foreach ($records as &$record) { foreach ($records as &$record) {
$record['id'] = json_encode($record); $record['id'] = json_encode($record);
} }
unset($record);
usort($records, "record_compare"); usort($records, "record_compare");
jtable_respond($records); jtable_respond($records);
break; break;
@ -297,7 +297,36 @@ case "update":
case "createrecord": case "createrecord":
$zone = new Zone(); $zone = new Zone();
$zone->parse($api->loadzone($_GET['zoneid'])); $zone->parse($api->loadzone($_GET['zoneid']));
$record = $zone->addrecord($_POST['name'], $_POST['type'], $_POST['content'], $_POST['disabled'], $_POST['ttl']);
$name = isset($_POST['name']) ? $_POST['name'] : '';
$type = $_POST['type'];
$content = $_POST['content'];
if ('' == $name) {
$name = $zone->name;
} elseif (string_ends_with($name, '.')) {
# "absolute" name, shouldn't append zone[name] - but check.
$name = substr($name, 0, -1);
if (!string_ends_with($name, $zone->name)) {
jtable_respond(null, 'error', "Name $name not in zone ".$zone->name);
}
} else if (!string_ends_with($name, $zone->name)) {
$name = $name . '.' . $zone->name;
}
if (!_valid_label($name)) {
jtable_respond(null, 'error', "Please only use [a-z0-9_/.-]");
}
if (!$type) {
jtable_respond(null, 'error', "Require a type");
}
if (!is_ascii($content)) {
jtable_respond(null, 'error', "Please only use ASCII-characters in your fields");
}
$record = $zone->addrecord($name, $type, $content, $_POST['disabled'], $_POST['ttl']);
$api->savezone($zone->export()); $api->savezone($zone->export());
jtable_respond($record, 'single'); jtable_respond($record, 'single');