61 lines
1.6 KiB
PHP
61 lines
1.6 KiB
PHP
<?php
|
|
|
|
const SUBDOMAIN_REGEX = '^[a-z0-9]{4,63}$';
|
|
|
|
function regListUserDomains() {
|
|
if (isset($_SESSION['id']))
|
|
return query('select', 'registry', ['username' => $_SESSION['id']], 'domain');
|
|
return [];
|
|
}
|
|
|
|
function regCheckDomainPossession($domain) {
|
|
if (in_array($domain, regListUserDomains(), true) !== true)
|
|
output(403, 'You don\'t own this domain on the registry.');
|
|
}
|
|
|
|
function regDeleteDomain($domain) {
|
|
// Delete domain from registry file
|
|
$path = CONF['reg']['suffixes_path'] . '/' . regParseDomain($domain)['suffix'] . 'zone';
|
|
$content = file_get_contents($path);
|
|
if ($content === false)
|
|
output(500, 'Failed to read current registry file.');
|
|
$content = preg_replace('/^(?:[a-z0-9._-]+\.)?' . preg_quote($domain, '/') . '[\t ]+.+$/Dm', '', $content);
|
|
if (file_put_contents($path, $content) === false)
|
|
output(500, 'Failed to write new registry file.');
|
|
|
|
try {
|
|
DB->beginTransaction();
|
|
|
|
$conditions = [
|
|
'domain' => $domain,
|
|
'username' => $_SESSION['id'],
|
|
];
|
|
|
|
insert('registry-history', [
|
|
'domain' => $domain,
|
|
'creation' => query('select', 'registry', $conditions, 'creation')[0],
|
|
'expiration' => date('Y-m'),
|
|
]);
|
|
|
|
query('delete', 'registry', $conditions);
|
|
|
|
DB->commit();
|
|
} catch (Exception $e) {
|
|
DB->rollback();
|
|
output(500, 'Database error.', [$e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
function regParseDomain($domain) {
|
|
$parts = explode('.', $domain, 2);
|
|
$subdomain = $parts[0];
|
|
$suffix = $parts[1];
|
|
|
|
if (array_key_exists($suffix, CONF['reg']['suffixes']) !== true)
|
|
output(403, 'This suffix doesn\'t exist.');
|
|
|
|
return [
|
|
'subdomain' => $subdomain,
|
|
'suffix' => $suffix,
|
|
];
|
|
}
|