Add Domains
This commit is contained in:
196
application/Domain/DomainController.php
Normal file
196
application/Domain/DomainController.php
Normal file
@@ -0,0 +1,196 @@
|
||||
<?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;
|
||||
|
||||
$this->inwx = new Inwx($this->INWX_USER, $this->INWX_PASS);
|
||||
$this->plesk = new Plesk($this->PLESK_USER, $this->PLESK_AUTH);
|
||||
}
|
||||
|
||||
protected function indexAction(): void {
|
||||
$this->layout()->setTemplate("Domain/Index");
|
||||
}
|
||||
|
||||
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 importDomain(): void {
|
||||
// use plesk api to get all domains
|
||||
|
||||
|
||||
}
|
||||
|
||||
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'] ?? [];
|
||||
$page = $json['pagination']['page'] ?? 1;
|
||||
$perPage = $json['pagination']['per_page'] ?? 10;
|
||||
|
||||
$domains = DomainModel::getAllDomains($filters, $perPage, $perPage * $page - $perPage);
|
||||
$totalRows = DomainModel::countDomains($filters);
|
||||
|
||||
return [
|
||||
"rows" => $domains,
|
||||
"pagination" => [
|
||||
"page" => $page,
|
||||
"total_pages" => ceil($totalRows / $perPage),
|
||||
"per_page" => $perPage,
|
||||
"total_rows" => intval($totalRows)
|
||||
]
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
private function getDnsRecords() {
|
||||
if (!isset($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()];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user