From 88edc27e4584d61b5fb4f6bd9b2a25c2d9450845 Mon Sep 17 00:00:00 2001 From: Pinga <121483313+getpinga@users.noreply.github.com> Date: Tue, 15 Aug 2023 17:15:18 +0300 Subject: [PATCH] Initial ICANN reporting script uploaded --- automation/config.php | 7 ++- automation/reporting.php | 127 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 automation/reporting.php diff --git a/automation/config.php b/automation/config.php index 36ef637..13959cb 100644 --- a/automation/config.php +++ b/automation/config.php @@ -9,7 +9,7 @@ return [ 'db_username' => 'your_username', 'db_password' => 'your_password', - // Escrow Configuration + // Escrow Configuration 'escrow_deposit_path' => '/opt/escrow', 'escrow_deleteXML' => false, 'escrow_RDEupload' => false, @@ -22,4 +22,9 @@ return [ 'escrow_report_url' => 'https://ry-api.icann.org/report/', 'escrow_report_username' => 'your_username', 'escrow_report_password' => 'your_password', + + // Reporting Configuration + 'reporting_upload' => false, + 'reporting_username' => 'your_username', + 'reporting_password' => 'your_password', ]; \ No newline at end of file diff --git a/automation/reporting.php b/automation/reporting.php new file mode 100644 index 0000000..ce9fe4f --- /dev/null +++ b/automation/reporting.php @@ -0,0 +1,127 @@ + PDO::ERRMODE_EXCEPTION, + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, + PDO::ATTR_EMULATE_PREPARES => false, +]; + +try { + $dbh = new PDO($dsn, $c['db_username'], $c['db_password'], $options); +} catch (PDOException $e) { + die("Connection failed: " . $e->getMessage()); +} + +// Fetch all TLDs +$query = "SELECT tld FROM domain_tld"; +$tlds = $dbh->query($query)->fetchAll(PDO::FETCH_COLUMN); + +foreach ($tlds as $tld) { + // Construct activity data for each TLD + $activityData[] = [ + 'operational-registrars' => getOperationalRegistrars($dbh, $tld), + 'ramp-up-registrars' => getRampUpRegistrars($dbh, $tld), + 'pre-ramp-up-registrars' => getPreRampUpRegistrars($dbh, $tld), + 'zfa-passwords' => getZfaPasswords($dbh, $tld), + // ... continue for all other headers + ]; + + // Loop through registrars and get transaction data + $registrars = getRegistrars($dbh); + foreach ($registrars as $registrar) { + $transactionData[] = [ + 'registrar-name' => $registrar['name'], + 'iana-id' => $registrar['iana_id'], + 'total-domains' => $registrar['total_domains'], + 'net-adds-1-yr' => getNetAdds1Yr($dbh, $registrar), + // ... continue for all other headers + ]; + } + + // Write data to CSV + writeCSV("{$tld}-activity-" . date('Ym') . "-en.csv", $activityData); + writeCSV("{$tld}-transactions-" . date('Ym') . "-en.csv", $transactionData); + + // Upload if the $c['reporting_upload'] variable is true + if ($c['reporting_upload']) { + // Calculate the period (previous month from now) + $previousMonth = date('Ym', strtotime('-1 month')); + + // Paths to the files you created + $activityFile = "{$tld}-activity-" . $previousMonth . "-en.csv"; + $transactionFile = "{$tld}-transactions-" . $previousMonth . "-en.csv"; + + // URLs for upload + $activityUploadUrl = 'https://ry-api.icann.org/report/registry-functions-activity/' . $tld . '/' . $previousMonth; + $transactionUploadUrl = 'https://ry-api.icann.org/report/registrar-transactions/' . $tld . '/' . $previousMonth; + + // Perform the upload + uploadFile($activityUploadUrl, $activityFile, $c['reporting_username'], $c['reporting_password']); + uploadFile($transactionUploadUrl, $transactionFile, $c['reporting_username'], $c['reporting_password']); + } + +} + +// HELPER FUNCTIONS +function getOperationalRegistrars($dbh, $tld) { + $stmt = $dbh->prepare("SELECT COUNT(*) FROM registrars WHERE status = 'operational' AND tld = ?"); + $stmt->execute([$tld]); + return $stmt->fetchColumn(); +} + +function getRegistrars($dbh) { + return $dbh->query("SELECT name, iana_id, total_domains FROM registrars")->fetchAll(); +} + +function writeCSV($filename, $data) { + $file = fopen($filename, 'w'); + fputcsv($file, array_keys($data[0])); + foreach ($data as $row) { + fputcsv($file, $row); + } + fclose($file); +} + +function getRampUpRegistrars($dbh, $tld) { + // Placeholder: Replace with actual query/logic + return 0; +} + +function getPreRampUpRegistrars($dbh, $tld) { + // Placeholder: Replace with actual query/logic + return 0; +} + +function getZfaPasswords($dbh, $tld) { + // Placeholder: Replace with actual query/logic + return 0; +} + +// Sample transaction data helper functions: +function getNetAdds1Yr($dbh, $registrar) { + // Placeholder: Replace with actual query/logic + return 0; +} + +// Upload function using cURL +function uploadFile($url, $filePath, $username, $password) { + $ch = curl_init(); + + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password); + curl_setopt($ch, CURLOPT_PUT, 1); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filePath)); + curl_setopt($ch, CURLOPT_INFILE, fopen($filePath, 'r')); + + $response = curl_exec($ch); + if (curl_errno($ch)) { + echo 'Error:' . curl_error($ch); + } + + curl_close($ch); +} \ No newline at end of file