Files
thetool/application/Domain/DomainController.php
2024-05-10 21:03:01 +00:00

209 lines
6.8 KiB
PHP

<?php
class DomainController extends mfBaseController {
private User $me;
private string $INWX_USER = INWX_USER;
private string $INWX_PASS = INWX_PASS;
private string $PLESK_USER = PLESK_HOST;
private string $PLESK_AUTH = PLESK_AUTH;
private Inwx $inwx;
private Plesk $plesk;
protected function init(): void {
$me = new User();
$me->loadMe();
$this->layout()->set("me", $me);
$this->me = $me;
if (!$this->me->isAdmin()) {
$this->redirect("dashboard");
}
$this->inwx = new Inwx($this->INWX_USER, $this->INWX_PASS);
$this->plesk = new Plesk($this->PLESK_USER, $this->PLESK_AUTH);
}
protected function indexAction(): void {
$JSGlobals = ["BASE_URL" => self::getUrl("Domain"),
"DASHBOARD_URL" => self::getUrl("Dashboard"),
"MFAPPNAME" => MFAPPNAME_SLUG,
"PAGE_TITLE" => "Domains",
"PATH" => [
["text" => MFAPPNAME_SLUG, "href" => self::getUrl("Dashboard")],
["text" => "Domain Management", "href" => self::getUrl("Domain")],
["text" => "Domains"]
],
"DOMAIN_API_URL" => self::getUrl("Domain/api"),
];
$this->layout()->set("vueViewName", "Domain");
$this->layout()->set("JSGlobals", $JSGlobals);
$this->layout()->setTemplate("VueViews/Vue");
}
protected function apiAction() {
$do = $this->request->do;
if ($do !== "getConfig" && !$this->me->is("employee")) {
$this->redirect("dashboard");
}
switch ($do) {
case "importAllDomains":
$return = $this->importAllDomains();
break;
case "getDomains":
$return = $this->getAllDomains();
break;
case "getDomainContacts":
$return = $this->getDomainContacts();
break;
case "getDnsRecords":
$return = $this->getDnsRecords();
break;
case "checkDomain":
$return = $this->checkDomain();
break;
default:
$return = false;
break;
}
if (!$return) {
$return = [
"status" => "error",
"message" => "Invalid request."
];
}
die(json_encode($return));
}
protected function importAllDomains(): array {
try {
$inwxContact = $this->inwx->contactList();
$pleskDomains = $this->plesk->getAllDomains();
$inwxDomains = $this->inwx->domainList();
$domains = [];
$pleskDomainsArray = [];
foreach ($pleskDomains as $pleskDomain) {
$pleskDomainsArray[$pleskDomain['name']] = $pleskDomain;
}
foreach ($inwxDomains as $inwxDomain) {
if (isset($pleskDomainsArray[$inwxDomain['domain']])) {
$inwxDomain['plesk'] = [
"id" => $pleskDomainsArray[$inwxDomain['domain']]['id'],
"hosting_type" => $pleskDomainsArray[$inwxDomain['domain']]['hosting_type'],
"created" => strtotime($pleskDomainsArray[$inwxDomain['domain']]['created'])
];
}
$domains[] = $inwxDomain;
}
$domainsImport = DomainModel::importDomains($domains);
$contactsImport = DomainContactModel::importDomainContacts($inwxContact);
return [
"status" => "success",
"importMessages" => [
$domainsImport['message'],
$contactsImport['message']
],
];
} catch (Exception $e) {
$this->log->error("Error while importing domains: " . $e->getMessage());
return [
"status" => "error",
"message" => "Error while importing domains: " . $e->getMessage()
];
}
}
private function getAllDomains(): array {
$json = json_decode(file_get_contents('php://input'), true);
$filters = $json['filters'] ?? [];
$order = $json['order'] ?? [];
$page = $json['pagination']['page'] ?? 1;
$perPage = $json['pagination']['per_page'] ?? 10;
$domains = DomainModel::getAllDomains($filters, $perPage, $perPage * $page - $perPage, $order);
$filtered_available = DomainModel::countDomains($filters);
$totalRows = DomainModel::countDomains([]);
return [
"rows" => $domains,
"pagination" => [
"page" => $page,
"total_pages" => ceil($filtered_available / $perPage),
"per_page" => $perPage,
"filtered_available" => intval($filtered_available),
"total_rows" => intval($totalRows)
]
];
}
private function getDnsRecords() {
if (!empty($this->request->domain)) {
return ["status" => "error", "message" => "No domain specified."];
}
$domain = $this->request->domain;
return array_merge(
dns_get_record($domain, DNS_TXT),
dns_get_record($domain, DNS_A),
dns_get_record($domain, DNS_CNAME),
dns_get_record($domain, DNS_MX),
dns_get_record($domain, DNS_NS),
dns_get_record($domain, DNS_SOA),
dns_get_record($domain, DNS_SRV),
dns_get_record($domain, DNS_AAAA),
);
}
private function getDomainContacts(): array {
$domainContacts = [];
$dbDomainContacts = DomainContactModel::getAllDomainContacts();
foreach ($dbDomainContacts as $dbDomainContact) {
$domainContacts[$dbDomainContact['inwxRoId']] = $dbDomainContact;
}
return $domainContacts;
}
private function checkDomain(): array {
$domain = $this->request->domain;
if(empty($domain)) {
return ["status" => "error", "message" => "No domain or tld specified."];
}
try {
$domainCheck = $this->inwx->domainCheck($domain);
if($domainCheck['domain'][0]['status'] === "free") {
$domainPrice = $this->inwx->domainGetDomainPrice($domain, "reg");
} else {
$domainPrice = $this->inwx->domainGetDomainPrice($domain, "transfer");
}
$domainCheck['domain'][0]['price'] = $domainPrice;
return $domainCheck['domain'][0];
} catch (Exception $e) {
$this->log->error("Error while checking domain: " . $e->getMessage());
return ["status" => "error", "message" => "Error while checking domain: " . $e->getMessage()];
}
}
}