Add Domains

This commit is contained in:
Luca Haid
2024-03-12 20:36:11 +01:00
parent 223d6fd7b7
commit 4054fa4608
9 changed files with 962 additions and 0 deletions
+9
View File
@@ -0,0 +1,9 @@
<?php
/**
* @property mixed|null $name
*/
class Domain extends mfBaseModel
{
}
+196
View 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()];
}
}
}
+133
View File
@@ -0,0 +1,133 @@
<?php
class DomainModel {
public $inwxRoId;
public $domain;
public $period;
public $crDate;
public $exDate;
public $reDate;
public $upDate;
public $transferLock;
public $status;
public $authCode;
public $registrant;
public $admin;
public $tech;
public $billing;
public $ns;
public $pleskId;
public $pleskHostingType;
public $pleskCreated;
public function __construct($data = []) {
foreach ($data as $field => $value) {
if (property_exists(get_called_class(), $field)) {
$this->$field = $value;
}
}
}
public static function importDomains($domains): array {
$db = FronkDB::singleton();
$db->query("TRUNCATE TABLE `Domain`");
$sql = /** @lang text */
"INSERT INTO `Domain` (`inwxRoId`, `domain`, `period`, `crDate`, `exDate`, `reDate`, `upDate`, `transferLock`, `status`, `authCode`, `registrant`, `admin`, `tech`, `billing`, `ns`, `pleskId`, `pleskHostingType`, `pleskCreated`) VALUES ";
$values = [];
foreach ($domains as $domain) {
$valueStr ="(" .
$domain['roId'] . ", '" .
$domain['domain'] . "', '" .
$domain['period'] . "', " .
$domain['crDate']['timestamp'] . ", " .
$domain['exDate']['timestamp'] . ", " .
$domain['reDate']['timestamp'] . ", " .
$domain['upDate']['timestamp'] . ", " .
($domain['transferLock'] ? 1 : 0) . ", '" .
$domain['status'] . "', '" .
$domain['authCode'] . "', " .
$domain['registrant'] . ", " .
$domain['admin'] . ", " .
$domain['tech'] . ", " .
$domain['billing'] . ", '" .
implode(", ", $domain['ns']) . "', ";
// Check if 'pleskId' is set
if (isset($domain['plesk']) && is_array($domain['plesk'])) {
$valueStr .= $domain['plesk']['id'] . ", ";
$valueStr .= "'" . $domain['plesk']['hosting_type'] . "', ";
$valueStr .= $domain['plesk']['created'];
} else {
$valueStr .= "NULL, NULL, NULL";
}
$values[] = $valueStr . ")";
}
$sql .= implode(", ", $values);
$db->query($sql);
return [
"message" => "Imported " . count($domains) . " domains."
];
}
/**
* Generate SQL Filter condition (space separated) for a given column.
*
* @param string|null $filterValue The filter value to match against.
* @param string $columnName The name of the column in the database table.
* @return string The SQL condition generated based on the filter value and column name.
*/
public static function generateFilterCondition(?string $filterValue, string $columnName): string {
$sql = "";
if (!empty($filterValue)) {
$filterItems = explode(" ", $filterValue);
foreach ($filterItems as $item) {
$sql .= " AND `$columnName` LIKE '%" . $item . "%'";
}
}
return $sql;
}
public static function getSqlFilter($filters): string {
$sql = isset($filters['crDate']) ? self::generateFilterCondition($filters['domain'], "domain") : "";
$sql .= isset($filters['crDate']) ? " AND `crDate` = " . $filters['crDate'] : "";
$sql .= isset($filters['exDate']) ? " AND `exDate` = " . $filters['exDate'] : "";
$sql .= isset($filters['reDate']) ? " AND `reDate` = " . $filters['reDate'] : "";
$sql .= isset($filters['upDate']) ? " AND `upDate` = " . $filters['upDate'] : "";
$sql .= isset($filters['status']) ? " AND `status` = '" . $filters['status'] . "'" : "";
$sql .= isset($filters['transferLock']) && $filters['transferLock'] == 1 ? " AND `transferLock` = true" : "";
$sql .= isset($filters['authCode']) ? self::generateFilterCondition($filters['authCode'], "authCode") : "";
$sql .= isset($filters['registrant']) && $filters['registrant'] !== 'all' ? " AND `registrant` = " . $filters['registrant'] : "";
$sql .= isset($filters['admin']) && $filters['admin'] !== 'all' ? " AND `admin` = " . $filters['admin'] : "";
$sql .= isset($filters['tech']) && $filters['tech'] !== 'all' ? " AND `tech` = " . $filters['tech'] : "";
$sql .= isset($filters['billing']) && $filters['billing'] !== 'all' ? " AND `billing` = " . $filters['billing'] : "";
$sql .= isset($filters['ns']) ? self::generateFilterCondition($filters['ns'], "ns") : "";
return $sql;
}
public static function getAllDomains($filters, $limit = null, $offset = 0): array {
$db = FronkDB::singleton();
$sql = "SELECT * FROM `Domain` WHERE 1 " . self::getSqlFilter($filters);
$sql .= $limit === null ? "" : " LIMIT " . $limit . " OFFSET " . $offset;
$result = $db->query($sql);
$rows = [];
while ($row = $result->fetch_assoc()) {
$rows[] = new DomainModel($row);
}
return $rows;
}
public static function countDomains($filters) {
$db = FronkDB::singleton();
$sql = "SELECT COUNT(*) as `total_rows` FROM `Domain` WHERE 1 " . self::getSqlFilter($filters);
$result = $db->query($sql);
return $result->fetch_assoc()['total_rows'];
}
}
@@ -0,0 +1,8 @@
<?php
/**
* @property mixed|null $name
*/
class DomainContact extends mfBaseModel {
}
@@ -0,0 +1,121 @@
<?php
class DomainContactModel {
public $id;
public $roId;
public $type;
public $name;
public $street;
public $city;
public $pc;
public $cc;
public $voice;
public $email;
public $protection;
public $usedCount;
public $verificationStatus;
public function __construct($data = []) {
foreach ($data as $field => $value) {
if (property_exists(get_called_class(), $field)) {
$this->$field = $value;
}
}
}
public static function importDomainContacts($domainContacts): array {
$db = FronkDB::singleton();
$db->query("TRUNCATE TABLE `DomainContact`");
$sql = /** @lang text */
"INSERT INTO `DomainContact` (`inwxRoId`, `type`, `name`, `street`, `city`, `pc`, `cc`, `voice`, `email`, `protection`, `verificationStatus`, `usedCount`) VALUES ";
$values = [];
foreach ($domainContacts as $domainContact) {
$valueStr = "(" .
$domainContact['roId'] . ", '" .
$domainContact['type'] . "', '" .
$domainContact['name'] . "', '" .
$domainContact['street'] . "', '" .
$domainContact['city'] . "', '" .
$domainContact['pc'] . "', '" .
$domainContact['cc'] . "', '" .
$domainContact['voice'] . "', '" .
$domainContact['email'] . "', " .
($domainContact['protection'] ? 1 : 0) . ", '" .
$domainContact['verificationStatus'] . "', ";
$valueStr .= $domainContact['usedCount'] ?? "NULL";
$valueStr .= ")";
$values[] = $valueStr;
}
$sql .= implode(", ", $values);
$db->query($sql);
return [
"message" => "Imported " . count($domainContacts) . " domain contacts."
];
}
/**
* Generate SQL Filter condition (space separated) for a given column.
*
* @param string|null $filterValue The filter value to match against.
* @param string $columnName The name of the column in the database table.
* @return string The SQL condition generated based on the filter value and column name.
*/
public static function generateFilterCondition(?string $filterValue, string $columnName): string {
$sql = "";
if (!empty($filterValue)) {
$filterItems = explode(" ", $filterValue);
foreach ($filterItems as $item) {
$sql .= " AND `$columnName` LIKE '%" . $item . "%'";
}
}
return $sql;
}
public static function getSqlFilter($filters): string {
$sql = isset($filters['roId']) ? " AND `inwxRoId` = " . $filters['roId'] : "";
$sql .= isset($filters['type']) ? " AND `type` = '" . $filters['type'] . "'" : "";
$sql .= isset($filters['name']) ? self::generateFilterCondition($filters['name'], "name") : "";
$sql .= isset($filters['street']) ? self::generateFilterCondition($filters['street'], "street") : "";
$sql .= isset($filters['city']) ? self::generateFilterCondition($filters['city'], "city") : "";
$sql .= isset($filters['pc']) ? " AND `pc` = " . $filters['pc'] : "";
$sql .= isset($filters['cc']) ? " AND `cc` = " . $filters['cc'] : "";
$sql .= isset($filters['voice']) ? " AND `voice` = " . $filters['voice'] : "";
$sql .= isset($filters['email']) ? self::generateFilterCondition($filters['email'], "email") : "";
$sql .= isset($filters['protection']) ? " AND `protection` = " . $filters['protection'] : "";
$sql .= isset($filters['usedCount']) ? " AND `usedCount` = " . $filters['usedCount'] : "";
$sql .= isset($filters['verificationStatus']) ? " AND `verificationStatus` = '" . $filters['verificationStatus'] . "'" : "";
return $sql;
}
public static function getAllDomainContacts($filters = null, $limit = null, $offset = 0, $raw_array = true): array {
$db = FronkDB::singleton();
$sql = "SELECT * FROM `DomainContact` WHERE 1 ";
$sql .= $filters === null ? "" : self::getSqlFilter($filters);
$sql .= $limit === null ? "" : " LIMIT " . $limit . " OFFSET " . $offset;
$result = $db->query($sql);
$rows = [];
while ($row = $result->fetch_assoc()) {
$rows[] = $raw_array ? $row : new DomainContactModel($row);
}
return $rows;
}
public static function countDomainContacts($filters) {
$db = FronkDB::singleton();
$sql = "SELECT COUNT(*) as `total_rows` FROM `DomainContact` WHERE 1 " . self::getSqlFilter($filters);
$result = $db->query($sql);
return $result->fetch_assoc()['total_rows'];
}
}