Upload of rudimentary automation scripts (partial)

This commit is contained in:
Pinga 2023-08-06 18:12:38 +03:00
parent 80c20c9370
commit df996ebb8a
4 changed files with 298 additions and 5 deletions

View file

@ -0,0 +1,56 @@
<?php
try {
// Create a new PDO connection
$c = require 'config.php';
$dsn = "mysql:host={$c['mysql_host']};dbname={$c['mysql_database']};port={$c['mysql_port']}";
$dbh = new PDO($dsn, $c['mysql_username'], $c['mysql_password']);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Prepare and execute the SQL statement to select unused hosts
$stmt = $dbh->prepare("SELECT `h`.`id`,`h`.`name` FROM `host` AS `h`
LEFT JOIN `domain_host_map` AS `m` ON `h`.`id` = `m`.`host_id`
WHERE `m`.`host_id` IS NULL AND `h`.`domain_id` IS NULL AND `h`.`crdate` < (NOW() - INTERVAL 1 MONTH)");
$stmt->execute();
$ids = [];
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$ids[] = $row['id'];
}
// Delete associated records from various tables for hosts
if (!empty($ids)) {
$placeholders = implode(',', array_fill(0, count($ids), '?'));
$dbh->prepare("DELETE FROM `host_status` WHERE `host_id` IN ($placeholders)")->execute($ids);
$dbh->prepare("DELETE FROM `host_addr` WHERE `host_id` IN ($placeholders)")->execute($ids);
$dbh->prepare("DELETE FROM `host` WHERE `id` IN ($placeholders)")->execute($ids);
}
// Prepare and execute the SQL statement to select unused contacts
$stmt = $dbh->prepare("SELECT `c`.`id`,`c`.`identifier` FROM `contact` AS `c`
LEFT JOIN `domain_contact_map` AS `m` ON `c`.`id` = `m`.`contact_id`
LEFT JOIN `domain` AS `d` ON `c`.`id` = `d`.`registrant`
WHERE `m`.`contact_id` IS NULL AND `d`.`registrant` IS NULL AND `c`.`crdate` < (NOW() - INTERVAL 1 MONTH)");
$stmt->execute();
$contact_ids = [];
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$contact_ids[] = $row['id'];
}
// Delete associated records from various tables for contacts
if (!empty($contact_ids)) {
$placeholders = implode(',', array_fill(0, count($contact_ids), '?'));
$dbh->prepare("DELETE FROM `contact_status` WHERE `contact_id` IN ($placeholders)")->execute($contact_ids);
$dbh->prepare("DELETE FROM `contact_postalInfo` WHERE `contact_id` IN ($placeholders)")->execute($contact_ids);
$dbh->prepare("DELETE FROM `contact_authInfo` WHERE `contact_id` IN ($placeholders)")->execute($contact_ids);
$dbh->prepare("DELETE FROM `contact` WHERE `id` IN ($placeholders)")->execute($contact_ids);
}
} catch (PDOException $e) {
// Handle database errors
die("Database error: " . $e->getMessage());
} catch (Exception $e) {
// Handle other types of errors
die("Error: " . $e->getMessage());
}

View file

@ -0,0 +1,207 @@
<?php
// Connect to the MySQL database using PDO
$c = require 'config.php';
$dsn = "mysql:host={$c['mysql_host']};dbname={$c['mysql_database']};port={$c['mysql_port']}";
$dbh = new PDO($dsn, $c['mysql_username'], $c['mysql_password']);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Auto-Renew Grace Period
$auto_renew = 0;
if ($auto_renew) {
$sth_autorenewperiod = $dbh->prepare("SELECT `id`, `name`, `tldid`, `exdate`, `clid` FROM `domain` WHERE CURRENT_TIMESTAMP > `exdate` AND `rgpstatus` IS NULL");
$sth_autorenewperiod->execute();
while ($row = $sth_autorenewperiod->fetch(PDO::FETCH_ASSOC)) {
$domain_id = $row['id'];
$name = $row['name'];
$tldid = $row['tldid'];
$exdate = $row['exdate'];
$clid = $row['clid'];
$sth_status = $dbh->prepare("SELECT `status` FROM `domain_status` WHERE `domain_id` = ?");
$sth_status->execute([$domain_id]);
$set_autorenewPeriod = 1;
while ($status_row = $sth_status->fetch(PDO::FETCH_ASSOC)) {
$status = $status_row['status'];
if (preg_match("/(serverUpdateProhibited|serverDeleteProhibited)$/", $status) || preg_match("/^pending/", $status)) {
$set_autorenewPeriod = 0;
continue;
}
}
if ($set_autorenewPeriod) {
list($registrar_balance, $creditLimit) = $dbh->query("SELECT `accountBalance`, `creditLimit` FROM `registrar` WHERE `id` = '$clid' LIMIT 1")->fetch(PDO::FETCH_NUM);
$price = $dbh->query("SELECT `m12` FROM `domain_price` WHERE `tldid` = '$tldid' AND `command` = 'renew' LIMIT 1")->fetchColumn();
if (($registrar_balance + $creditLimit) > $price) {
$dbh->exec("UPDATE `domain` SET `rgpstatus` = 'autoRenewPeriod', `exdate` = DATE_ADD(`exdate`, INTERVAL 12 MONTH), `autoRenewPeriod` = '12', `renewedDate` = `exdate` WHERE `id` = '$domain_id'");
$dbh->exec("UPDATE `registrar` SET `accountBalance` = (`accountBalance` - $price) WHERE `id` = '$clid'");
$dbh->exec("INSERT INTO `payment_history` (`registrar_id`, `date`, `description`, `amount`) VALUES('$clid', CURRENT_TIMESTAMP, 'autoRenew domain $name for period 12 MONTH', '-$price')");
list($to) = $dbh->query("SELECT `exdate` FROM `domain` WHERE `id` = '$domain_id' LIMIT 1")->fetch(PDO::FETCH_NUM);
$sth = $dbh->prepare("INSERT INTO `statement` (`registrar_id`, `date`, `command`, `domain_name`, `length_in_months`, `from`, `to`, `amount`) VALUES(?, CURRENT_TIMESTAMP, ?, ?, ?, ?, ?, ?)");
$sth->execute([$clid, 'autoRenew', $name, '12', $exdate, $to, $price]);
if (!$dbh->query("SELECT `id` FROM `statistics` WHERE `date` = CURDATE()")->fetchColumn()) {
$dbh->exec("INSERT IGNORE INTO `statistics` (`date`) VALUES(CURDATE())");
}
$dbh->exec("UPDATE `statistics` SET `renewed_domains` = `renewed_domains` + 1 WHERE `date` = CURDATE()");
} else {
$dbh->exec("DELETE FROM `domain_status` WHERE `domain_id` = '$domain_id'");
$dbh->exec("UPDATE `domain` SET `rgpstatus` = 'redemptionPeriod', `delTime` = `exdate` WHERE `id` = '$domain_id'");
$dbh->exec("INSERT INTO `domain_status` (`domain_id`, `status`) VALUES('$domain_id', 'pendingDelete')");
}
}
echo date("Y-m-d H:i:s") . " - $domain_id\t|\t$name\t|\trgpStatus:autoRenewPeriod exdate:$exdate\n";
}
} else {
$grace_period = 30;
$sth_graceperiod = $dbh->prepare("SELECT `id`, `name`, `exdate` FROM `domain` WHERE CURRENT_TIMESTAMP > DATE_ADD(`exdate`, INTERVAL $grace_period DAY) AND `rgpstatus` IS NULL");
$sth_graceperiod->execute();
while ($row = $sth_graceperiod->fetch(PDO::FETCH_ASSOC)) {
$domain_id = $row['id'];
$name = $row['name'];
$exdate = $row['exdate'];
$sth_status = $dbh->prepare("SELECT `status` FROM `domain_status` WHERE `domain_id` = ?");
$sth_status->execute([$domain_id]);
$set_graceperiod = 1;
while ($status_row = $sth_status->fetch(PDO::FETCH_ASSOC)) {
$status = $status_row['status'];
if (preg_match("/(serverUpdateProhibited|serverDeleteProhibited)$/", $status) || preg_match("/^pending/", $status)) {
$set_graceperiod = 0;
continue;
}
}
if ($set_graceperiod) {
$dbh->exec("DELETE FROM `domain_status` WHERE `domain_id` = '$domain_id'");
$dbh->exec("UPDATE `domain` SET `rgpstatus` = 'redemptionPeriod', `delTime` = DATE_ADD(`exdate`, INTERVAL $grace_period DAY) WHERE `id` = '$domain_id'");
$dbh->exec("INSERT INTO `domain_status` (`domain_id`, `status`) VALUES('$domain_id', 'pendingDelete')");
}
echo date("Y-m-d H:i:s") . " - $domain_id\t|\t$name\t|\trgpStatus:redemptionPeriod exdate:$exdate\n";
}
}
try {
// clean autoRenewPeriod after 45 days
$sql1 = "UPDATE `domain` SET `rgpstatus` = NULL WHERE CURRENT_TIMESTAMP > DATE_ADD(`exdate`, INTERVAL 45 DAY) AND `rgpstatus` = 'autoRenewPeriod'";
$dbh->exec($sql1);
// clean addPeriod after 5 days
$sql2 = "UPDATE `domain` SET `rgpstatus` = NULL WHERE CURRENT_TIMESTAMP > DATE_ADD(`crdate`, INTERVAL 5 DAY) AND `rgpstatus` = 'addPeriod'";
$dbh->exec($sql2);
// clean renewPeriod after 5 days
$sql3 = "UPDATE `domain` SET `rgpstatus` = NULL WHERE CURRENT_TIMESTAMP > DATE_ADD(`renewedDate`, INTERVAL 5 DAY) AND `rgpstatus` = 'renewPeriod'";
$dbh->exec($sql3);
// clean transferPeriod after 5 days
$sql4 = "UPDATE `domain` SET `rgpstatus` = NULL WHERE CURRENT_TIMESTAMP > DATE_ADD(`trdate`, INTERVAL 5 DAY) AND `rgpstatus` = 'transferPeriod'";
$dbh->exec($sql4);
} catch (PDOException $e) {
die("Error: " . $e->getMessage());
}
// Pending Delete
// The current value of the redemptionPeriod is 30 calendar days.
$sth_pendingdelete = $dbh->prepare("SELECT `id`, `name`, `exdate` FROM `domain` WHERE CURRENT_TIMESTAMP > DATE_ADD(`delTime`, INTERVAL 30 DAY) AND `rgpstatus` = 'redemptionPeriod'");
$sth_pendingdelete->execute();
while ($row = $sth_pendingdelete->fetch(PDO::FETCH_ASSOC)) {
$domain_id = $row['id'];
$name = $row['name'];
$exdate = $row['exdate'];
$sth_status = $dbh->prepare("SELECT `status` FROM `domain_status` WHERE `domain_id` = ?");
$sth_status->execute([$domain_id]);
$set_pendingDelete = 1;
while ($status_row = $sth_status->fetch(PDO::FETCH_ASSOC)) {
$status = $status_row['status'];
if (preg_match("/(serverUpdateProhibited|serverDeleteProhibited)$/", $status)) {
$set_pendingDelete = 0;
continue;
}
}
if ($set_pendingDelete) {
$dbh->exec("UPDATE `domain` SET `rgpstatus` = 'pendingDelete' WHERE `id` = '$domain_id'");
}
echo date("Y-m-d H:i:s") . " - $domain_id\t|\t$name\t|\trgpStatus:pendingDelete exdate:$exdate\n";
}
// Pending Restore
$sth_pendingRestore = $dbh->prepare("SELECT `id`, `name`, `exdate` FROM `domain` WHERE `rgpstatus` = 'pendingRestore' AND (CURRENT_TIMESTAMP > DATE_ADD(`resTime`, INTERVAL 7 DAY))");
$sth_pendingRestore->execute();
while ($row = $sth_pendingRestore->fetch(PDO::FETCH_ASSOC)) {
$domain_id = $row['id'];
$name = $row['name'];
$exdate = $row['exdate'];
$dbh->exec("UPDATE `domain` SET `rgpstatus` = 'redemptionPeriod' WHERE `id` = '$domain_id'");
echo date("Y-m-d H:i:s") . " - $domain_id\t|\t$name\t|\tback to redemptionPeriod from pendingRestore exdate:$exdate\n";
}
// Domain Deletion
$sth_delete = $dbh->prepare("SELECT `id`, `name`, `exdate` FROM `domain` WHERE CURRENT_TIMESTAMP > DATE_ADD(`delTime`, INTERVAL 35 DAY) AND `rgpstatus` = 'pendingDelete'");
$sth_delete->execute();
while ($row = $sth_delete->fetch(PDO::FETCH_ASSOC)) {
$domain_id = $row['id'];
$name = $row['name'];
$exdate = $row['exdate'];
$sth_status = $dbh->prepare("SELECT `status` FROM `domain_status` WHERE `domain_id` = ?");
$sth_status->execute([$domain_id]);
$delete_domain = 0;
while ($status_row = $sth_status->fetch(PDO::FETCH_ASSOC)) {
$status = $status_row['status'];
if ($status == 'pendingDelete') {
$delete_domain = 1;
}
if (preg_match("/(serverUpdateProhibited|serverDeleteProhibited)$/", $status)) {
$delete_domain = 0;
continue;
}
}
if ($delete_domain) {
// Actual deletion process
$sth = $dbh->prepare("SELECT `id` FROM `host` WHERE `domain_id` = ?");
$sth->execute([$domain_id]);
while ($host_row = $sth->fetch(PDO::FETCH_ASSOC)) {
$host_id = $host_row['id'];
$dbh->exec("DELETE FROM `host_addr` WHERE `host_id` = '$host_id'");
$dbh->exec("DELETE FROM `host_status` WHERE `host_id` = '$host_id'");
$dbh->exec("DELETE FROM `domain_host_map` WHERE `host_id` = '$host_id'");
}
$dbh->exec("DELETE FROM `domain_contact_map` WHERE `domain_id` = '$domain_id'");
$dbh->exec("DELETE FROM `domain_host_map` WHERE `domain_id` = '$domain_id'");
$dbh->exec("DELETE FROM `domain_authInfo` WHERE `domain_id` = '$domain_id'");
$dbh->exec("DELETE FROM `domain_status` WHERE `domain_id` = '$domain_id'");
$dbh->exec("DELETE FROM `host` WHERE `domain_id` = '$domain_id'");
$sth = $dbh->prepare("DELETE FROM `domain` WHERE `id` = ?");
$sth->execute([$domain_id]);
if ($sth->errorCode() !== '00000') {
$errorInfo = $sth->errorInfo();
echo 'Numele de domeniu nu a fost sters cred ca este vre-o legatura cu alte obiecte: ' . $errorInfo[2];
} else {
if (!$dbh->query("SELECT `id` FROM `statistics` WHERE `date` = CURDATE()")->fetchColumn()) {
$dbh->exec("INSERT IGNORE INTO `statistics` (`date`) VALUES(CURDATE())");
}
$dbh->exec("UPDATE `statistics` SET `deleted_domains` = `deleted_domains` + 1 WHERE `date` = CURDATE()");
}
}
echo date("Y-m-d H:i:s") . " - $domain_id\t|\t$name\t|\tdomain:Deleted exdate:$exdate\n";
}

30
automation/statistics.php Normal file
View file

@ -0,0 +1,30 @@
<?php
try {
// Connect to the MySQL database using PDO
$c = require 'config.php';
$dsn = "mysql:host={$c['mysql_host']};dbname={$c['mysql_database']};port={$c['mysql_port']}";
$dbh = new PDO($dsn, $c['mysql_username'], $c['mysql_password']);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Check if there's an entry for the current date
$query = "SELECT `id` FROM `statistics` WHERE `date` = CURDATE()";
$curdate_id = $dbh->query($query)->fetchColumn();
if (!$curdate_id) {
$dbh->exec("INSERT IGNORE INTO `statistics` (`date`) VALUES(CURDATE())");
}
// Get the total number of domains
$total_domains = $dbh->query("SELECT COUNT(`id`) AS `total_domains` FROM `domain`")->fetchColumn();
// Update the statistics table with the total number of domains for the current date
$dbh->exec("UPDATE `statistics` SET `total_domains` = '$total_domains' WHERE `date` = CURDATE()");
} catch (PDOException $e) {
// Handle database errors
die("Database error: " . $e->getMessage());
} catch (Exception $e) {
// Handle other types of errors
die("Error: " . $e->getMessage());
}

View file

@ -20,9 +20,9 @@ $sth->execute();
while (list($id, $tld) = $sth->fetch(PDO::FETCH_NUM)) {
$tldRE = preg_quote($tld, '/');
$outFile = fopen("/var/named/named{$tld}.zone", 'w') or print "Unable to open file '/var/named/named{$tld}.zone'.\n";
fwrite($outFile, "\$TTL\t1H\n\@\tIN\tSOA\t{$ns1}.\tpostmaster{$tld}. (\n\t$timestamp\n\t3H\n\t1H\n\t1W\n\t1D\n\t)\n\n");
fwrite($outFile, "\@\t1H\tIN\tNS\t{$ns1}.\n");
fwrite($outFile, "\@\t1H\tIN\tNS\t{$ns2}.\n");
fwrite($outFile, "\$TTL\t1H\n@\tIN\tSOA\t{$ns1}.\tpostmaster{$tld}. (\n\t$timestamp\n\t3H\n\t1H\n\t1W\n\t1D\n\t)\n\n");
fwrite($outFile, "@\t1H\tIN\tNS\t{$ns1}.\n");
fwrite($outFile, "@\t1H\tIN\tNS\t{$ns2}.\n");
// Select all the hosts
$sth2 = $dbh->prepare("SELECT DISTINCT `domain`.`id`, `domain`.`name`, `domain`.`exdate`, `rgpstatus`, `domain`.`name`
@ -39,7 +39,7 @@ while (list($id, $tld) = $sth->fetch(PDO::FETCH_NUM)) {
$status_id = $dbh->query("SELECT `id` FROM `domain_status` WHERE `domain_id` = '$did' AND `status` LIKE '%Hold' LIMIT 1")->fetchColumn();
if ($status_id) continue;
$dname = trim($dname, "$tldRE.");
$dname = '@' if ($dname == "$tld.");
$dname = ($dname == "$tld.") ? '@' : $dname;
$hname = trim($hname, "$tldRE.");
fwrite($outFile, "$dname\tIN\tNS\t$hname\n");
}
@ -58,7 +58,7 @@ while (list($id, $tld) = $sth->fetch(PDO::FETCH_NUM)) {
$status_id = $dbh->query("SELECT `id` FROM `domain_status` WHERE `domain_id` = '$did' AND `status` LIKE '%Hold' LIMIT 1")->fetchColumn();
if ($status_id) continue;
$hname = trim($hname, "$tldRE.");
$hname = '@' if ($hname == "$tld.");
$hname = ($hname == "$tld.") ? '@' : $hname;
if ($type == 'v4') {
fwrite($outFile, "$hname\tIN\tA\t$addr\n");
} else {