servnest/fn/reg.php

62 lines
1.7 KiB
PHP
Raw Normal View History

2023-07-17 19:15:18 +00:00
<?php declare(strict_types=1);
2021-02-17 21:48:49 +00:00
const SUBDOMAIN_REGEX = '^(?!\-)(?!..\-\-)[a-z0-9-]{4,63}(?<!\-)$';
2023-06-19 22:36:58 +00:00
function regListUserDomains(): array {
if (isset($_SESSION['id']))
return query('select', 'registry', ['username' => $_SESSION['id']], 'domain');
return [];
2021-02-18 21:40:16 +00:00
}
2023-06-19 22:36:58 +00:00
function regCheckDomainPossession(string $domain): void {
if (in_array($domain, regListUserDomains(), true) !== true)
output(403, 'You don\'t own this domain on the registry.');
2021-02-18 21:40:16 +00:00
}
2023-06-19 22:36:58 +00:00
function regDeleteDomain(string $domain, string $user_id): void {
// Delete domain from registry file
2023-01-23 00:14:59 +00:00
$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);
2023-01-23 00:14:59 +00:00
if (file_put_contents($path, $content) === false)
output(500, 'Failed to write new registry file.');
try {
DB->beginTransaction();
$conditions = [
'domain' => $domain,
'username' => $user_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()]);
}
}
2023-01-23 00:14:59 +00:00
2023-06-19 22:36:58 +00:00
function regParseDomain(string $domain): array {
2023-01-23 00:14:59 +00:00
$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,
];
}