ytkns/src/ZoneRedirect.php
2020-06-10 16:03:13 +00:00

47 lines
1.5 KiB
PHP

<?php
namespace YTKNS;
final class ZoneRedirect {
public function __construct() {
}
public static function exists(string $subdomain): bool {
$check = DB::prepare('
SELECT COUNT(`redirect_name`) > 0
FROM `ytkns_redirects`
WHERE `redirect_name` = :subdomain
');
$check->bindValue('subdomain', $subdomain);
return $check->execute() ? (bool)$check->fetchColumn() : true;
}
public static function find(string $subdomain): ?ZoneRedirect {
$find = DB::prepare('
SELECT `redirect_name`, `redirect_target`
FROM `ytkns_redirects`
WHERE `redirect_name` = :subdomain
');
$find->bindValue('subdomain', $subdomain);
$redirect = $find->execute() ? $find->fetchObject(self::class) : false;
return $redirect ? $redirect : null;
}
public static function create(string $subdomain, string $target): void {
$create = DB::prepare('
REPLACE INTO `ytkns_redirects` (
`redirect_name`, `redirect_target`
) VALUES (
:name, :target
)
');
$create->bindValue('name', $subdomain);
$create->bindValue('target', $target);
$create->execute();
}
public function execute(): void {
http_response_code(301);
header(sprintf('Location: %s', $this->redirect_target));
}
}