Skip to content

Commit

Permalink
Updates for the new database structure, fixed #4
Browse files Browse the repository at this point in the history
  • Loading branch information
getpinga committed Oct 28, 2024
1 parent 8c166a1 commit 5ca3394
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 223 deletions.
37 changes: 13 additions & 24 deletions automation/errp_dns.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,21 @@ function updateExpiredDomainNameservers($pdo) {
$ns1 = $config['ns1'];
$ns2 = $config['ns2'];

// Prepare the SQL to check if ns1 exists
$sqlCheck = "SELECT id FROM namingo_host WHERE name = :name";
$stmtCheck = $pdo->prepare($sqlCheck);
// Prepare the SQL to update ns1, ns2, and set ns3 to ns5 to NULL
$sqlUpdate = "
UPDATE namingo_domain
SET ns1 = :ns1, ns2 = :ns2, ns3 = NULL, ns4 = NULL, ns5 = NULL
WHERE id = :domain_id
";
$stmtUpdate = $pdo->prepare($sqlUpdate);

// Get or insert ns1 and ns2
$host_id_1 = getOrInsertHost($pdo, $ns1);
$host_id_2 = getOrInsertHost($pdo, $ns2);
// Bind parameters for ns1, ns2, and domain_id
$stmtUpdate->bindParam(':ns1', $ns1);
$stmtUpdate->bindParam(':ns2', $ns2);
$stmtUpdate->bindParam(':domain_id', $domain['id'], PDO::PARAM_INT);

// Remove existing mappings from namingo_domain_host_map
$sqlDelete = "DELETE FROM namingo_domain_host_map WHERE domain_id = :domain_id";
$stmtDelete = $pdo->prepare($sqlDelete);
$stmtDelete->bindParam(':domain_id', $domain['id']);
$stmtDelete->execute();

// Insert new mappings for host_id_1 and host_id_2
$sqlInsertMap = "INSERT INTO namingo_domain_host_map (domain_id, host_id) VALUES (:domain_id, :host_id)";
$stmtInsertMap = $pdo->prepare($sqlInsertMap);

// Insert mapping for ns1
$stmtInsertMap->bindParam(':domain_id', $domain['id']);
$stmtInsertMap->bindParam(':host_id', $host_id_1);
$stmtInsertMap->execute();

// Insert mapping for ns2
$stmtInsertMap->bindParam(':host_id', $host_id_2);
$stmtInsertMap->execute();
// Execute the update
$stmtUpdate->execute();

// Send EPP update to registry
$epp = connectEpp("generic", $config);
Expand Down
29 changes: 0 additions & 29 deletions automation/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,33 +100,4 @@ function setupLogger($logFilePath, $channelName = 'app') {
$log->pushHandler($fileHandler);

return $log;
}

/**
* Get or insert a nameserver and return the host_id.
*
* @param PDO $pdo Database connection
* @param string $ns The nameserver to check or insert
* @return int The host_id of the nameserver
*/
function getOrInsertHost($pdo, $ns) {
// Prepare the SQL to check if ns exists
$sqlCheck = "SELECT id FROM namingo_host WHERE name = :name";
$stmtCheck = $pdo->prepare($sqlCheck);

// Check if the nameserver exists
$stmtCheck->bindParam(':name', $ns);
$stmtCheck->execute();
$host = $stmtCheck->fetch(PDO::FETCH_ASSOC);

if ($host) {
return $host['id'];
} else {
// Insert new nameserver if not found
$sqlInsert = "INSERT INTO namingo_host (name, clid, crid, crdate) VALUES (:name, 0, 0, NOW(3))";
$stmtInsert = $pdo->prepare($sqlInsert);
$stmtInsert->bindParam(':name', $ns);
$stmtInsert->execute();
return $pdo->lastInsertId();
}
}
97 changes: 29 additions & 68 deletions automation/includes/WHMCS.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public function __construct(\PDO $pdo, $full, $hdl)

public function generateFull(): void
{
// Query to get id, registrant, name, crdate, exdate from namingo_domain
$sqlDomain = "SELECT d.id, d.registrant, d.name, DATE_FORMAT(d.crdate, '%Y-%m-%dT%H:%i:%sZ') AS crdate, DATE_FORMAT(d.exdate, '%Y-%m-%dT%H:%i:%sZ') AS exdate FROM namingo_domain d";
// Query to get id, registrant, admin, tech, billing, name, crdate, exdate from namingo_domain
$sqlDomain = "SELECT d.id, d.registrant, d.admin, d.tech, d.billing, d.name, DATE_FORMAT(d.crdate, '%Y-%m-%dT%H:%i:%sZ') AS crdate, DATE_FORMAT(d.exdate, '%Y-%m-%dT%H:%i:%sZ') AS exdate FROM namingo_domain d";
$stmtDomain = $this->pdo->prepare($sqlDomain);
$stmtDomain->execute();
$domains = $stmtDomain->fetchAll(\PDO::FETCH_ASSOC);
Expand All @@ -49,62 +49,36 @@ public function generateFull(): void

// Prepare to store contacts by type, including the identifier
$domain['contacts'] = [
'admin' => null,
'tech' => null,
'billing' => null,
'registrant' => $domain['registrant'] ?? null,
'admin' => $domain['admin'] ?? null,
'tech' => $domain['tech'] ?? null,
'billing' => $domain['billing'] ?? null,
];

// Collect contact IDs
$contactIds = array_filter([$domain['registrant'], $domain['admin'], $domain['tech'], $domain['billing']]);

// Get contact_id/type pairs from namingo_domain_contact_map
$sqlContacts = "SELECT contact_id, type FROM namingo_domain_contact_map WHERE domain_id = :domain_id";
// Prepare the SQL to fetch identifiers for all contact IDs in one query
$sqlContacts = "SELECT id, identifier FROM namingo_contact WHERE id IN (" . implode(',', array_fill(0, count($contactIds), '?')) . ")";
$stmtContacts = $this->pdo->prepare($sqlContacts);
$stmtContacts->bindParam(':domain_id', $domainId, \PDO::PARAM_INT);
$stmtContacts->execute();
$contacts = $stmtContacts->fetchAll(\PDO::FETCH_ASSOC);

// Assign the contact IDs based on their type and prepare to fetch identifiers
$contactIds = [$domain['registrant']];
foreach ($contacts as $contact) {
$type = strtolower($contact['type']);
if (in_array($type, ['admin', 'tech', 'billing'])) {
$domain['contacts'][$type] = $contact['contact_id'];
$contactIds[] = $contact['contact_id'];
}
}
$stmtContacts->execute($contactIds);

// Ensure we only have unique contact IDs
$contactIds = array_unique($contactIds);

// Prepare query to get identifiers from namingo_contact for registrant and other contact_ids
if (!empty($contactIds)) {
$placeholders = implode(',', array_fill(0, count($contactIds), '?'));
$sqlIdentifiers = "SELECT id, identifier FROM namingo_contact WHERE id IN ($placeholders)";
$stmtIdentifiers = $this->pdo->prepare($sqlIdentifiers);

// Execute with direct array of contact IDs
$stmtIdentifiers->execute($contactIds);
$identifiers = $stmtIdentifiers->fetchAll(\PDO::FETCH_ASSOC);

// Map identifiers to contact types
$identifierMap = [];
foreach ($identifiers as $identifier) {
$identifierMap[$identifier['id']] = $identifier['identifier'];
}

// Replace contact IDs with identifiers in the contacts array
foreach ($domain['contacts'] as $type => $contactId) {
if ($contactId && isset($identifierMap[$contactId])) {
$domain['contacts'][$type] = $identifierMap[$contactId];
}
}

// Also map the registrant to its identifier
if (isset($identifierMap[$domain['registrant']])) {
$domain['registrant'] = $identifierMap[$domain['registrant']];
}

// Add to the domains array for CSV writing
$this->writeToCsv($domain);
// Map the identifiers to contact IDs
$identifiers = [];
while ($row = $stmtContacts->fetch(\PDO::FETCH_ASSOC)) {
$identifiers[$row['id']] = $row['identifier'];
}

// Replace the contact IDs with identifiers
$domain['contacts'] = [
'registrant' => $identifiers[$domain['registrant']] ?? null,
'admin' => $identifiers[$domain['admin']] ?? null,
'tech' => $identifiers[$domain['tech']] ?? null,
'billing' => $identifiers[$domain['billing']] ?? null,
];

// Add to the domains array for CSV writing
$this->writeToCsv($domain);
}
}

Expand All @@ -121,7 +95,7 @@ private function writeToCsv(array $domain): void
$nextDueDate = ''; // Assuming there is no `nextduedate` from your current data

// Get handles for the CSV, use empty string if they do not exist
$rtHandle = $domain['registrant'] ?? '';
$rtHandle = $domain['contacts']['registrant'] ?? '';
$tcHandle = $domain['contacts']['tech'] ?? '';
$acHandle = $domain['contacts']['admin'] ?? '';
$bcHandle = $domain['contacts']['billing'] ?? '';
Expand All @@ -144,20 +118,7 @@ public function generateHDL(): void
{
// Query the database to get data from both tables
$sql = "
SELECT
nc.id AS aid,
nc.identifier,
nc.voice AS phone,
nc.fax,
nc.email,
ncp.name,
ncp.street1 AS address,
ncp.city,
ncp.sp AS state,
ncp.pc AS postcode,
ncp.cc AS country
FROM namingo_contact nc
LEFT JOIN namingo_contact_postalInfo ncp ON nc.id = ncp.contact_id
SELECT identifier, voice AS phone, fax, email, name, street1 AS address, city, sp AS state, pc AS postcode, cc AS country FROM namingo_contact
";

$stmt = $this->pdo->prepare($sql);
Expand Down
2 changes: 1 addition & 1 deletion automation/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

// Loop through domains and send reminder email and EPP command to update nameservers
foreach ($rows as $row) {
if ($row['custom_2'] == 0) {
if ($row['validation'] == 0) {
$domain_name = $row['name'];
$registrant_email = $row['email'];
$token = $row['validation_log'];
Expand Down
1 change: 1 addition & 0 deletions rdap/config.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ return [
'db_password' => 'your_password',
'registry_url' => 'https://example.com/rdap-terms',
'rdap_url' => 'https://rdap.example.com',
'privacy' => true,
'rately' => false,
'limit' => 1000,
'period' => 60,
Expand Down
24 changes: 13 additions & 11 deletions rdap/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ function setupLogger($logFilePath, $channelName = 'app') {
}

function mapContactToVCard($contactDetails, $role, $c) {
$redacted = 'REDACTED FOR PRIVACY';

return [
'objectClassName' => 'entity',
'handle' => [$contactDetails['identifier']],
Expand All @@ -50,20 +52,20 @@ function mapContactToVCard($contactDetails, $role, $c) {
"vcard",
[
['version', new stdClass(), 'text', '4.0'],
["fn", new stdClass(), 'text', $contactDetails['name']],
["org", $contactDetails['org']],
["fn", new stdClass(), 'text', $c['privacy'] ? $redacted : $contactDetails['name']],
["org", new stdClass(), 'text', $c['privacy'] ? $redacted : $contactDetails['org']],
["adr", [
"", // Post office box
$contactDetails['street1'], // Extended address
$contactDetails['street2'], // Street address
$contactDetails['city'], // Locality
$contactDetails['sp'], // Region
$contactDetails['pc'], // Postal code
strtoupper($contactDetails['cc']) // Country name
$c['privacy'] ? $redacted : $contactDetails['street1'], // Extended address
$c['privacy'] ? $redacted : $contactDetails['street2'], // Street address
$c['privacy'] ? $redacted : $contactDetails['city'], // Locality
$c['privacy'] ? $redacted : $contactDetails['sp'], // Region
$c['privacy'] ? $redacted : $contactDetails['pc'], // Postal code
$c['privacy'] ? $redacted : strtoupper($contactDetails['cc']) // Country name
]],
["tel", $contactDetails['voice'], ["type" => "voice"]],
["tel", $contactDetails['fax'], ["type" => "fax"]],
["email", $contactDetails['email']],
["tel", new stdClass(), 'text', $c['privacy'] ? $redacted : $contactDetails['voice'], ["type" => "voice"]],
["tel", new stdClass(), 'text', $c['privacy'] ? $redacted : $contactDetails['fax'], ["type" => "fax"]],
["email", new stdClass(), 'text', $c['privacy'] ? $redacted : $contactDetails['email']],
]
],
];
Expand Down
91 changes: 35 additions & 56 deletions rdap/start_rdap.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,58 +247,38 @@ function handleDomainQuery($request, $response, $pdo, $domainName, $c, $log) {
return;
}

$metaQuery = "SELECT * FROM namingo_domain_meta WHERE domain_id = :domain_id";
$stmtMeta = $pdo->prepare($metaQuery);
$stmtMeta->bindParam(':domain_id', $domainDetails['id'], PDO::PARAM_INT);
$stmtMeta->execute();
$domainMeta = $stmtMeta->fetch(PDO::FETCH_ASSOC);

// Query 4: Get registrant details
$stmt4 = $pdo->prepare("SELECT namingo_contact.id,namingo_contact.identifier,namingo_contact_postalInfo.name,namingo_contact_postalInfo.org,namingo_contact_postalInfo.street1,namingo_contact_postalInfo.street2,namingo_contact_postalInfo.street3,namingo_contact_postalInfo.city,namingo_contact_postalInfo.sp,namingo_contact_postalInfo.pc,namingo_contact_postalInfo.cc,namingo_contact.voice,namingo_contact.voice_x,namingo_contact.fax,namingo_contact.fax_x,namingo_contact.email,namingo_contact_postalInfo.type FROM namingo_contact,namingo_contact_postalInfo WHERE namingo_contact.id=:registrant AND namingo_contact_postalInfo.contact_id=namingo_contact.id");
$stmt4 = $pdo->prepare("SELECT id,identifier,name,org,street1,street2,street3,city,sp,pc,cc,voice,fax,email FROM namingo_contact WHERE id=:registrant");
$stmt4->bindParam(':registrant', $domainDetails['registrant'], PDO::PARAM_INT);
$stmt4->execute();
$registrantDetails = $stmt4->fetch(PDO::FETCH_ASSOC);

// Query 5: Get admin, billing and tech contacts
$stmtMap = $pdo->prepare("SELECT contact_id, type FROM namingo_domain_contact_map WHERE domain_id = :domain_id");
$stmtMap->bindParam(':domain_id', $domainDetails['id'], PDO::PARAM_INT);
$stmtMap->execute();
$contactMap = $stmtMap->fetchAll(PDO::FETCH_ASSOC);

$adminDetails = [];
$techDetails = [];
$billingDetails = [];

foreach ($contactMap as $map) {
$stmtDetails = $pdo->prepare("SELECT namingo_contact.id, namingo_contact.identifier, namingo_contact_postalInfo.name, namingo_contact_postalInfo.org, namingo_contact_postalInfo.street1, namingo_contact_postalInfo.street2, namingo_contact_postalInfo.street3, namingo_contact_postalInfo.city, namingo_contact_postalInfo.sp, namingo_contact_postalInfo.pc, namingo_contact_postalInfo.cc, namingo_contact.voice, namingo_contact.voice_x, namingo_contact.fax, namingo_contact.fax_x, namingo_contact.email,namingo_contact_postalInfo.type FROM namingo_contact, namingo_contact_postalInfo WHERE namingo_contact.id = :contact_id AND namingo_contact_postalInfo.contact_id = namingo_contact.id");
$stmtDetails->bindParam(':contact_id', $map['contact_id'], PDO::PARAM_INT);
$stmtDetails->execute();

$contactDetails = $stmtDetails->fetch(PDO::FETCH_ASSOC);

switch ($map['type']) {
case 'admin':
$adminDetails[] = $contactDetails;
break;
case 'tech':
$techDetails[] = $contactDetails;
break;
case 'billing':
$billingDetails[] = $contactDetails;
break;
}
}
// Query 5: Get admin, billing and tech contacts
$stmt5a = $pdo->prepare("SELECT id,identifier,name,org,street1,street2,street3,city,sp,pc,cc,voice,fax,email FROM namingo_contact WHERE id=:admin");
$stmt5a->bindParam(':admin', $domainDetails['admin'], PDO::PARAM_INT);
$stmt5a->execute();
$adminDetails = $stmt5a->fetch(PDO::FETCH_ASSOC);

$stmt5b = $pdo->prepare("SELECT id,identifier,name,org,street1,street2,street3,city,sp,pc,cc,voice,fax,email FROM namingo_contact WHERE id=:tech");
$stmt5b->bindParam(':tech', $domainDetails['tech'], PDO::PARAM_INT);
$stmt5b->execute();
$techDetails = $stmt5b->fetch(PDO::FETCH_ASSOC);

$stmt5c = $pdo->prepare("SELECT id,identifier,name,org,street1,street2,street3,city,sp,pc,cc,voice,fax,email FROM namingo_contact WHERE id=:billing");
$stmt5c->bindParam(':billing', $domainDetails['billing'], PDO::PARAM_INT);
$stmt5c->execute();
$billingDetails = $stmt5c->fetch(PDO::FETCH_ASSOC);

// Query 6: Get nameservers
$stmt6 = $pdo->prepare("
SELECT namingo_host.name, namingo_host.id as host_id
FROM namingo_domain_host_map, namingo_host
WHERE namingo_domain_host_map.domain_id = :domain_id
AND namingo_domain_host_map.host_id = namingo_host.id
");
$stmt6->bindParam(':domain_id', $domainDetails['id'], PDO::PARAM_INT);
$stmt6->execute();
$nameservers = $stmt6->fetchAll(PDO::FETCH_ASSOC);
$nameservers = [];
for ($i = 1; $i <= 5; $i++) {
if (!empty($domainDetails["ns$i"])) {
$nameservers[] = [
'name' => $domainDetails["ns$i"],
'host_id' => $i // Use the index as a stand-in for host_id
];
}
}

$statusQuery = "SELECT status FROM namingo_domain_status WHERE domain_id = :domain_id";
$stmtStatus = $pdo->prepare($statusQuery);
Expand Down Expand Up @@ -327,7 +307,6 @@ function handleDomainQuery($request, $response, $pdo, $domainName, $c, $log) {
}

// Construct the RDAP response in JSON format
$domainDetails['registrant_contact_id'] = $domainMeta['registrant_contact_id'];
$rdapResponse = [
'rdapConformance' => [
'rdap_level_0',
Expand Down Expand Up @@ -382,15 +361,15 @@ function handleDomainQuery($request, $response, $pdo, $domainName, $c, $log) {
[
mapContactToVCard($registrantDetails, 'registrant', $c)
],
array_map(function ($contact) use ($c) {
return mapContactToVCard($contact, 'admin', $c);
}, $adminDetails),
array_map(function ($contact) use ($c) {
return mapContactToVCard($contact, 'tech', $c);
}, $techDetails),
array_map(function ($contact) use ($c) {
return mapContactToVCard($contact, 'billing', $c);
}, $billingDetails)
[
mapContactToVCard($adminDetails, 'admin', $c)
],
[
mapContactToVCard($techDetails, 'tech', $c)
],
[
mapContactToVCard($billingDetails, 'billing', $c)
]
),
'events' => $events,
'handle' => $domainDetails['id'] . '',
Expand All @@ -410,7 +389,7 @@ function handleDomainQuery($request, $response, $pdo, $domainName, $c, $log) {
'nameservers' => array_map(function ($nameserverDetails) use ($c) {
return [
'objectClassName' => 'nameserver',
'handle' => 'H' . $nameserverDetails['host_id'],
'handle' => 'H' . $nameserverDetails['name'],
'ldhName' => $nameserverDetails['name'],
'links' => [
[
Expand Down
Loading

0 comments on commit 5ca3394

Please sign in to comment.