add more phpdoc to DomainZones ApiCommand; minor fixes in DirOptions and DirProtections

Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann
2018-11-18 15:07:06 +01:00
parent d2024e06ff
commit 776bb56b24
3 changed files with 81 additions and 37 deletions

View File

@@ -18,55 +18,78 @@
class DomainZones extends ApiCommand implements ResourceEntity
{
/**
* add a new dns zone for a given domain by id or domainname
*
* @param int $id
* optional domain id
* @param string $domainname
* optional domain name
* @param string $record
* optional, default empty
* @param string $type
* optional, zone-entry type (A, AAAA, TXT, etc.), default 'A'
* @param int $prio
* optional, priority, default empty
* @param string $content
* optional, default empty
* @param int $ttl
* optional, default 18000
*
* @access admin, customer
* @throws Exception
* @return array
*/
public function add()
{
if (Settings::Get('system.dnsenabled') != '1') {
throw new Exception("DNS server not enabled on this system", 405);
}
$id = $this->getParam('id', true, 0);
$dn_optional = ($id <= 0 ? false : true);
$domainname = $this->getParam('domainname', $dn_optional, '');
// get requested domain
$result = $this->apiCall('SubDomains.get', array(
'id' => $id,
'domainname' => $domainname
));
$id = $result['id'];
// parameters
$record = $this->getParam('record', true, null);
$type = $this->getParam('type', true, 'A');
$prio = $this->getParam('prio', true, null);
$content = $this->getParam('content', true, null);
$ttl = $this->getParam('ttl', true, 18000);
if ($result['parentdomainid'] != '0') {
throw new Exception("DNS zones can only be generated for the main domain, not for subdomains", 406);
}
if ($result['subisbinddomain'] != '1') {
standard_error('dns_domain_nodns', '', true);
}
$idna_convert = new idna_convert_wrapper();
$domain = $idna_convert->encode($result['domain']);
// select all entries
$sel_stmt = Database::prepare("SELECT * FROM `" . TABLE_DOMAIN_DNS . "` WHERE domain_id = :did");
Database::pexecute($sel_stmt, array(
'did' => $id
), true, true);
$dom_entries = $sel_stmt->fetchAll(PDO::FETCH_ASSOC);
// validation
$errors = array();
if (empty($record)) {
$record = "@";
}
$record = trim(strtolower($record));
if ($record != '@' && $record != '*') {
// validate record
if (strpos($record, '--') !== false) {
@@ -80,28 +103,28 @@ class DomainZones extends ApiCommand implements ResourceEntity
}
// convert entry
$record = $idna_convert->encode($record);
if ($add_wildcard_again) {
$record = '*.' . $record;
}
if (strlen($record) > 63) {
$errors[] = $this->lng['error']['dns_record_toolong'];
}
}
}
// TODO regex validate content for invalid characters
if ($ttl <= 0) {
$ttl = 18000;
}
$content = trim($content);
if (empty($content)) {
$errors[] = $this->lng['error']['dns_content_empty'];
}
// types
if ($type == 'A' && filter_var($content, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) {
$errors[] = $this->lng['error']['dns_arec_noipv4'];
@@ -202,7 +225,7 @@ class DomainZones extends ApiCommand implements ResourceEntity
$content .= '.';
}
}
$new_entry = array(
'record' => $record,
'type' => $type,
@@ -212,7 +235,7 @@ class DomainZones extends ApiCommand implements ResourceEntity
'domain_id' => (int) $id
);
ksort($new_entry);
// check for duplicate
foreach ($dom_entries as $existing_entry) {
// compare json-encoded string of array
@@ -235,7 +258,7 @@ class DomainZones extends ApiCommand implements ResourceEntity
break;
}
}
if (empty($errors)) {
$ins_stmt = Database::prepare("
INSERT INTO `" . TABLE_DOMAIN_DNS . "` SET
@@ -248,14 +271,14 @@ class DomainZones extends ApiCommand implements ResourceEntity
");
Database::pexecute($ins_stmt, $new_entry, true, true);
$new_entry_id = Database::lastInsertId();
// add temporary to the entries-array (no reread of DB necessary)
$new_entry['id'] = $new_entry_id;
$dom_entries[] = $new_entry;
// re-generate bind configs
inserttask('4');
$result = $this->apiCall('DomainZones.get', array(
'id' => $id
));
@@ -269,11 +292,11 @@ class DomainZones extends ApiCommand implements ResourceEntity
* return a domain-dns entry by either id or domainname
*
* @param int $id
* optional, the domain-id
* optional, the domain id
* @param string $domainname
* optional, the domainname
* optional, the domain name
*
* @access admin
* @access admin, customer
* @throws Exception
* @return array
*/
@@ -282,61 +305,82 @@ class DomainZones extends ApiCommand implements ResourceEntity
if (Settings::Get('system.dnsenabled') != '1') {
throw new Exception("DNS server not enabled on this system", 405);
}
$id = $this->getParam('id', true, 0);
$dn_optional = ($id <= 0 ? false : true);
$domainname = $this->getParam('domainname', $dn_optional, '');
// get requested domain
$result = $this->apiCall('SubDomains.get', array(
'id' => $id,
'domainname' => $domainname
));
$id = $result['id'];
if ($result['parentdomainid'] != '0') {
throw new Exception("DNS zones can only be generated for the main domain, not for subdomains", 406);
}
if ($result['subisbinddomain'] != '1') {
standard_error('dns_domain_nodns', '', true);
}
$zone = createDomainZone($id);
$zonefile = (string) $zone;
$this->logger()->logAction($this->isAdmin() ? ADM_ACTION : USR_ACTION, LOG_NOTICE, "[API] get dns-zone for '" . $result['domain'] . "'");
return $this->response(200, "successfull", explode("\n", $zonefile));
}
/**
* You cannot update a dns zone entry.
* You need to delete it and re-add it.
*/
public function update()
{
throw new Exception('You cannot update a dns zone entry. You need to delete it and re-add it.', 303);
}
/**
* You cannot list dns zones.
* To get all domains use Domains.listing() or SubDomains.listing()
*/
public function listing()
{
throw new Exception('You cannot list dns zones. To get all domains use Domains.listing() or SubDomains.listing()', 303);
}
/**
* deletes a domain-dns entry by id
*
* @param int $entry_id
* @param int $id
* optional, the domain id
* @param string $domainname
* optional, the domain name
*
* @access admin, customer
* @throws Exception
* @return bool
*/
public function delete()
{
if (Settings::Get('system.dnsenabled') != '1') {
throw new Exception("DNS server not enabled on this system", 405);
}
$entry_id = $this->getParam('entry_id');
$id = $this->getParam('id', true, 0);
$dn_optional = ($id <= 0 ? false : true);
$domainname = $this->getParam('domainname', $dn_optional, '');
// get requested domain
$result = $this->apiCall('SubDomains.get', array(
'id' => $id,
'domainname' => $domainname
));
$id = $result['id'];
$del_stmt = Database::prepare("DELETE FROM `" . TABLE_DOMAIN_DNS . "` WHERE `id` = :id AND `domain_id` = :did");
Database::pexecute($del_stmt, array(
'id' => $entry_id,