From ee9f4c4032c5cbb3c6145689501b03d5e8bcfcb2 Mon Sep 17 00:00:00 2001 From: Pinga <121483313+getpinga@users.noreply.github.com> Date: Sun, 12 Nov 2023 18:58:14 +0200 Subject: [PATCH] Migration scripts will be in private repository for now --- migrations/cocca/1_registrar.php | 39 ---------- migrations/cocca/2_contact.php | 91 ---------------------- migrations/cocca/3_domain.php | 125 ------------------------------- migrations/cocca/4_host_addr.php | 44 ----------- migrations/cocca/5_charges.php | 36 --------- migrations/cocca/config.php.dist | 11 --- migrations/cocca/helpers.php | 70 ----------------- 7 files changed, 416 deletions(-) delete mode 100644 migrations/cocca/1_registrar.php delete mode 100644 migrations/cocca/2_contact.php delete mode 100644 migrations/cocca/3_domain.php delete mode 100644 migrations/cocca/4_host_addr.php delete mode 100644 migrations/cocca/5_charges.php delete mode 100644 migrations/cocca/config.php.dist delete mode 100644 migrations/cocca/helpers.php diff --git a/migrations/cocca/1_registrar.php b/migrations/cocca/1_registrar.php deleted file mode 100644 index 4f96885..0000000 --- a/migrations/cocca/1_registrar.php +++ /dev/null @@ -1,39 +0,0 @@ - PDO::ERRMODE_EXCEPTION, - PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, - PDO::ATTR_EMULATE_PREPARES => false, -]; - -try { - $pdo = new PDO($dsn, $c['db_username'], $c['db_password'], $options); -} catch (PDOException $e) { - die("Connection failed: " . $e->getMessage()); -} - -$inputFile = fopen('clients.csv', 'r'); -$headers = fgetcsv($inputFile); - -while (($row = fgetcsv($inputFile)) !== false) { - $data = array_combine($headers, $row); - - // Inserting into the 'registrar' table - $sql = "INSERT INTO registrar (name, iana_id, clid, pw, prefix, email, whois_server, rdap_server, url, abuse_email, abuse_phone, accountBalance, creditLimit, creditThreshold, thresholdType, currency, crdate, update) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())"; - $stmt = $pdo->prepare($sql); - $stmt->execute([$data['Name'], $data['ROID'], 'example_clid', 'example_pw', 'EX', $data['E-mail'], 'whois.example.com', 'rdap.example.com', 'http://example.com', 'abuse@example.com', '0', 1000.00, 0.00, 0.00, 'fixed', 'USD']); - - $registrarId = $pdo->lastInsertId(); - - // Inserting into the 'registrar_contact' table - $contactSql = "INSERT INTO registrar_contact (registrar_id, type, title, first_name, middle_name, last_name, org, street1, street2, street3, city, sp, pc, cc, voice, fax, email) VALUES (?, 'owner', ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; - $contactStmt = $pdo->prepare($contactSql); - $contactStmt->execute([$registrarId, '', '', '', '', $data['Name'], $data['Address'], '', '', '', '', '', '', $data['Country'], $data['Phone'], $data['Fax'], $data['E-mail']]); -} - -fclose($inputFile); \ No newline at end of file diff --git a/migrations/cocca/2_contact.php b/migrations/cocca/2_contact.php deleted file mode 100644 index 71197e7..0000000 --- a/migrations/cocca/2_contact.php +++ /dev/null @@ -1,91 +0,0 @@ - PDO::ERRMODE_EXCEPTION, - PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, - PDO::ATTR_EMULATE_PREPARES => false, -]; - -try { - $pdo = new PDO($dsn, $c['db_username'], $c['db_password'], $options); -} catch (PDOException $e) { - die("Connection failed: " . $e->getMessage()); -} - -// Reading CSV file -$inputFile = fopen('Registry_Contacts.csv', 'r'); - -// Skip the header row -$headers = fgetcsv($inputFile); - -while (($row = fgetcsv($inputFile)) !== false) { - $data = array_combine($headers, $row); - - // Preparing data for `contact` table - $contactData = [ - $data['roid'], // identifier - formatPhoneNumber($data['Voice']), // voice - null, // voice_x (not provided in CSV) - formatPhoneNumber($data['Fax']), // fax - null, // fax_x (not provided in CSV) - $data['E-mail'], // email - null, // nin (not provided in CSV) - null, // nin_type (not provided in CSV) - $data['registrar'], // clid - $data['registrar'], // crid - formatTimestamp($data['createdate']) // crdate - ]; - - // Inserting into `contact` table - $stmt = $pdo->prepare("INSERT INTO contact (identifier, voice, voice_x, fax, fax_x, email, nin, nin_type, clid, crid, crdate) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); - $stmt->execute($contactData); - - // Getting the last inserted contact_id - $contactId = $pdo->lastInsertId(); - - // Preparing data for `contact_postalInfo` table - $postalInfoData = [ - $contactId, // contact_id - 'int', // type (assuming 'int' for international) - $data['International Name'] ?: $data['Local Name'], // name - $data['International Organisation'] ?: $data['Local Organisation'], // org - $data['International Street1'] ?: $data['Local Street1'], // street1 - $data['International Street2'] ?: $data['Local Street2'], // street2 - $data['International Street3'] ?: $data['Local Street3'], // street3 - $data['International City'] ?: $data['Local City'], // city - $data['International State/Province'] ?: $data['Local State/Province'], // sp - $data['International Postal Code'] ?: $data['Local Postal Code'], // pc - formatCountryCode($data['International Country Code'] ?: $data['Local Country Code']) // cc - ]; - - // Inserting into `contact_postalInfo` table - $stmt = $pdo->prepare("INSERT INTO contact_postalInfo (contact_id, type, name, org, street1, street2, street3, city, sp, pc, cc) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); - $stmt->execute($postalInfoData); - - // Inserting into `contact_authInfo` table - $authInfoData = [ - $contactId, // contact_id - 'pw', // authtype - generateRandomString() // authinfo - ]; - - $stmt = $pdo->prepare("INSERT INTO contact_authInfo (contact_id, authtype, authinfo) VALUES (?, ?, ?)"); - $stmt->execute($authInfoData); - - // Inserting into `contact_status` table - $statusData = [ - $contactId, // contact_id - 'ok' // status - ]; - - $stmt = $pdo->prepare("INSERT INTO contact_status (contact_id, status) VALUES (?, ?)"); - $stmt->execute($statusData); -} - -fclose($inputFile); - diff --git a/migrations/cocca/3_domain.php b/migrations/cocca/3_domain.php deleted file mode 100644 index 2888109..0000000 --- a/migrations/cocca/3_domain.php +++ /dev/null @@ -1,125 +0,0 @@ - PDO::ERRMODE_EXCEPTION, - PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, - PDO::ATTR_EMULATE_PREPARES => false, -]; - -try { - $pdo = new PDO($dsn, $c['db_username'], $c['db_password'], $options); -} catch (PDOException $e) { - die("Connection failed: " . $e->getMessage()); -} - -$inputFile = fopen('Domains.csv', 'r'); - -// Skip the header row of the input file -$headers = fgetcsv($inputFile); - -while (($row = fgetcsv($inputFile)) !== false) { - $data = array_combine($headers, $row); - - // Inserting into the 'domain' table - $tldId = getTldId($pdo, $data['name']); - $createdOn = formatTimestamp($data['create_date']); - $expiryDate = formatTimestamp($data['expiry_date']); - $registrarId = $data['registrar_id']; - - $sql = "INSERT INTO domain (name, tldid, crdate, exdate, clid, crid) VALUES (?, ?, ?, ?, ?, ?)"; - $stmt = $pdo->prepare($sql); - $stmt->execute([$data['name'], $tldId, $createdOn, $expiryDate, $registrarId, $registrarId]); - - $domainId = $pdo->lastInsertId(); - - // Inserting into the 'secdns' table - $sql = "INSERT INTO secdns (domain_id, maxsiglife, interface, keytag, alg, digesttype, digest, flags, protocol, keydata_alg, pubkey) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; - - // Assuming default values for some fields as they are not provided in the CSV - $maxSigLife = 604800; - $interface = 'dsData'; - $alg = 5; // Default algorithm - $digestType = 1; // Default digest type - $flags = null; - $protocol = null; - $keydataAlg = null; - $pubkey = null; - - // Extract DNSSEC data from the current row - $keyTag = $data['key_tag'] ?? ''; // Using null coalescing operator for default value - $digestValue = $data['digest_value'] ?? ''; - - $stmt = $pdo->prepare($sql); - $stmt->execute([$domainId, $maxSigLife, $interface, $keyTag, $alg, $digestType, $digestValue, $flags, $protocol, $keydataAlg, $pubkey]); - - // Inserting domain status into 'domain_status' table - $status = $data['status'] ?? 'ok'; - $sql = "INSERT INTO domain_status (domain_id, status) VALUES (?, ?)"; - $stmt = $pdo->prepare($sql); - $stmt->execute([$domainId, $status]); - - // Inserting hosts into 'host' table - for ($i = 1; $i <= 4; $i++) { - if (isset($data["NameServer_$i"]) && !empty($data["NameServer_$i"])) { - $hostName = $data["NameServer_$i"]; - $registrarId = $data['registrar_id']; - $createdOn = formatTimestamp($data['create_date']); - - $sql = "INSERT INTO host (name, domain_id, clid, crid, crdate) VALUES (?, ?, ?, ?, ?)"; - $stmt = $pdo->prepare($sql); - $stmt->execute([$hostName, null, $registrarId, $registrarId, $createdOn]); - - $hostId = $pdo->lastInsertId(); - - // Inserting 'ok' status into 'host_status' table - $statusSql = "INSERT INTO host_status (host_id, status) VALUES (?, ?)"; - $statusStmt = $pdo->prepare($statusSql); - $statusStmt->execute([$hostId, 'ok']); - } - } - - // Insert into domain_contact_map - $contactTypes = ['admin' => 'admin_contact_id', 'billing' => 'billing_contact_id', 'tech' => 'tech_contact_id']; - foreach ($contactTypes as $type => $field) { - $contactIds = explode(',', $data[$field]); - foreach ($contactIds as $roid) { - $contactId = getContactIdByROID($pdo, trim($roid)); - if ($contactId !== null) { - $contactSql = "INSERT INTO domain_contact_map (domain_id, contact_id, type) VALUES (?, ?, ?)"; - $contactStmt = $pdo->prepare($contactSql); - $contactStmt->execute([$domainId, $contactId, $type]); - } - } - } - - // Insert into domain_host_map - for ($i = 1; $i <= 5; $i++) { - if (!empty($data["NameServer_$i"])) { - $hostId = getHostIdByName($pdo, $data["NameServer_$i"]); - if ($hostId !== null) { - $hostSql = "INSERT INTO domain_host_map (domain_id, host_id) VALUES (?, ?)"; - $hostStmt = $pdo->prepare($hostSql); - $hostStmt->execute([$domainId, $hostId]); - } - } - } -} - -fclose($inputFile); - -// Updating the 'host' table with domain_id -$hostsStmt = $pdo->query("SELECT id, name FROM host WHERE domain_id IS NULL"); -while ($host = $hostsStmt->fetch()) { - $domainName = strstr($host['name'], '.', true); - $domainId = getDomainIdByName($pdo, $domainName); - - if ($domainId !== null) { - $updateStmt = $pdo->prepare("UPDATE host SET domain_id = ? WHERE id = ?"); - $updateStmt->execute([$domainId, $host['id']]); - } -} \ No newline at end of file diff --git a/migrations/cocca/4_host_addr.php b/migrations/cocca/4_host_addr.php deleted file mode 100644 index 6d94368..0000000 --- a/migrations/cocca/4_host_addr.php +++ /dev/null @@ -1,44 +0,0 @@ - PDO::ERRMODE_EXCEPTION, - PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, - PDO::ATTR_EMULATE_PREPARES => false, -]; - -try { - $pdo = new PDO($dsn, $c['db_username'], $c['db_password'], $options); -} catch (PDOException $e) { - die("Connection failed: " . $e->getMessage()); -} - -// Fetching all hosts -$hostsStmt = $pdo->query("SELECT id, name FROM host"); -while ($host = $hostsStmt->fetch()) { - $hostId = $host['id']; - $hostName = $host['name']; - - // Get IPv4 address - $ipv4 = gethostbyname($hostName); - if ($ipv4 !== $hostName) { // Check if a valid IPv4 address is returned - $sql = "INSERT INTO host_addr (host_id, addr, ip) VALUES (?, ?, 'v4')"; - $stmt = $pdo->prepare($sql); - $stmt->execute([$hostId, $ipv4]); - } - - // Get IPv6 address - $dnsRecords = dns_get_record($hostName, DNS_AAAA); - foreach ($dnsRecords as $record) { - if (isset($record['ipv6'])) { - $ipv6 = $record['ipv6']; - $sql = "INSERT INTO host_addr (host_id, addr, ip) VALUES (?, ?, 'v6')"; - $stmt = $pdo->prepare($sql); - $stmt->execute([$hostId, $ipv6]); - } - } -} \ No newline at end of file diff --git a/migrations/cocca/5_charges.php b/migrations/cocca/5_charges.php deleted file mode 100644 index 2036818..0000000 --- a/migrations/cocca/5_charges.php +++ /dev/null @@ -1,36 +0,0 @@ - $data['name'] ?? '', - 'txn_on' => $data['charge_on'] ? date('c', strtotime($data['charge_on'])) : null, - 'charge' => floatval($data['charge'] ?? 0.0), - 'renewal_years' => intval($data['renewal_years'] ?? 0), - 'client_charged' => $data['new_client'] ?? '', - 'client_trid' => $data['client_trid'] ?? '', - 'autorenewal' => parseBool($data['autorenewal'] ?? ''), - 'vat_amount' => intval($data['vat_amount'] ?? 0), - ]; - - fputcsv($outputFile, $filteredRow); - } -} - -fclose($inputFile); -fclose($outputFile); \ No newline at end of file diff --git a/migrations/cocca/config.php.dist b/migrations/cocca/config.php.dist deleted file mode 100644 index b5e2a05..0000000 --- a/migrations/cocca/config.php.dist +++ /dev/null @@ -1,11 +0,0 @@ - 'mysql', - 'db_host' => 'localhost', - 'db_port' => 3306, - 'db_database' => 'your_database_name', - 'db_username' => 'your_username', - 'db_password' => 'your_password', -]; \ No newline at end of file diff --git a/migrations/cocca/helpers.php b/migrations/cocca/helpers.php deleted file mode 100644 index 694706d..0000000 --- a/migrations/cocca/helpers.php +++ /dev/null @@ -1,70 +0,0 @@ -setTime(0, 0, 0); - } - return $dt->format('Y-m-d H:i:s'); // MySQL datetime format -} - -function getTldId($pdo, $domainName) { - $tld = strrchr($domainName, '.'); - $stmt = $pdo->prepare("SELECT id FROM domain_tld WHERE tld = ?"); - $stmt->execute([$tld]); - $row = $stmt->fetch(); - return $row ? $row['id'] : null; -} - -function getDomainIdByName($pdo, $domainName) { - $stmt = $pdo->prepare("SELECT id FROM domain WHERE name = ?"); - $stmt->execute([$domainName]); - $row = $stmt->fetch(); - return $row ? $row['id'] : null; -} - -function getContactIdByROID($pdo, $roid) { - $stmt = $pdo->prepare("SELECT id FROM contact WHERE roid = ?"); - $stmt->execute([$roid]); - $row = $stmt->fetch(); - return $row ? $row['id'] : null; -} - -function getHostIdByName($pdo, $hostName) { - $stmt = $pdo->prepare("SELECT id FROM host WHERE name = ?"); - $stmt->execute([$hostName]); - $row = $stmt->fetch(); - return $row ? $row['id'] : null; -} - -function isEligibleForRenewal($status) { - $pendingStatuses = ['domain_status_pending_purge', 'domain_status_pending_delete']; - return in_array($status, $pendingStatuses); -} - -function parseBool($value) { - $value = strtolower($value); - if ($value === 'true') return true; - if ($value === 'false') return false; - return null; -} \ No newline at end of file