fossbilling-epp-isnic/ISNICSync.php

146 lines
5 KiB
PHP
Raw Permalink Normal View History

2023-07-13 12:39:15 +00:00
<?php
2023-07-16 04:55:23 +00:00
require_once "eppClient.php";
2023-07-13 12:39:15 +00:00
2023-07-16 04:55:23 +00:00
use Pinga\Tembo\eppClient;
2023-07-13 12:39:15 +00:00
$config = include "config.php";
$c = $config["db"];
$registrar = "ISNIC";
2023-07-13 12:39:15 +00:00
try {
2023-07-13 12:39:15 +00:00
// Establish the PDO connection
$dsn = $c["type"] . ":host=" . $c["host"] . ";port=" . $c["port"] . ";dbname=" . $c["name"];
$pdo = new PDO($dsn, $c["user"], $c["password"]);
// Use a prepared statement to prevent SQL injection
$stmt = $pdo->prepare("SELECT * FROM tld_registrar WHERE registrar = :registrar");
// Bind the $registrar value to the :registrar parameter
$stmt->bindValue(":registrar", $registrar);
// Execute the prepared statement
$stmt->execute();
// Fetch all rows from the result set
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$config = [];
foreach ($rows as $row) {
2023-07-13 12:39:15 +00:00
$config = json_decode($row["config"], true);
$registrar_id = $row["id"];
}
if (empty($config)) {
2023-07-13 12:39:15 +00:00
throw new Exception("Database cannot be accessed right now.");
}
} catch (PDOException $e) {
2023-07-13 12:39:15 +00:00
echo "Database error: " . $e->getMessage();
} catch (Exception $e) {
2023-07-13 12:39:15 +00:00
echo "General error: " . $e->getMessage();
}
function connectEpp(string $registry, $config)
{
try {
2023-07-16 04:55:23 +00:00
$epp = new eppClient();
2023-07-13 12:39:15 +00:00
$info = [
"host" => $config["host"],
"port" => $config["port"], "timeout" => 30, "tls" => "1.3", "bind" => false, "bindip" => "1.2.3.4:0", "verify_peer" => false, "verify_peer_name" => false,
"verify_host" => false, "cafile" => "", "local_cert" => $config["ssl_cert"], "local_pk" => $config["ssl_key"], "passphrase" => "", "allow_self_signed" => true,
];
2023-07-13 12:39:15 +00:00
$epp->connect($info);
$login = $epp->login([
"clID" => $config["username"], "pw" => $config["password"],
"prefix" => "tembo",
]);
if (array_key_exists("error", $login)) {
2023-07-13 12:39:15 +00:00
echo "Login Error: " . $login["error"] . PHP_EOL;
exit();
} else {
2023-07-13 12:39:15 +00:00
echo "Login Result: " . $login["code"] . ": " . $login["msg"][0] . PHP_EOL;
}
return $epp;
} catch (EppException $e) {
2023-07-13 12:39:15 +00:00
return "Error : " . $e->getMessage();
}
}
try {
// Fetch all domains
$stmt = $pdo->prepare('SELECT sld, tld FROM service_domain WHERE tld_registrar_id = :registrar');
$stmt->bindValue(':registrar', $registrar_id);
$stmt->execute();
$domains = $stmt->fetchAll(PDO::FETCH_ASSOC);
2023-07-13 12:39:15 +00:00
$epp = connectEpp("generic", $config);
foreach ($domains as $domainRow) {
// Combine sld and tld into a single domain name
$domain = $domainRow['sld'] . $domainRow['tld'];
$params = ["domainname" => $domain];
$domainInfo = $epp->domainInfo($params);
if (array_key_exists("error", $domainInfo)) {
echo "DomainInfo Error: " . $domainInfo["error"] . " (" . $domain . ")" . PHP_EOL;
continue;
}
2023-07-13 12:39:15 +00:00
$ns = $domainInfo['ns'];
$ns1 = isset($ns[1]) ? $ns[1] : null;
$ns2 = isset($ns[2]) ? $ns[2] : null;
$ns3 = isset($ns[3]) ? $ns[3] : null;
$ns4 = isset($ns[4]) ? $ns[4] : null;
2023-07-13 12:42:39 +00:00
$exDate = $domainInfo['exDate'];
$datetime = new DateTime($exDate);
$formattedExDate = $datetime->format('Y-m-d H:i:s');
2023-07-13 12:42:39 +00:00
$statuses = $domainInfo['status'];
2023-07-13 12:39:15 +00:00
2023-07-13 12:42:39 +00:00
$clientStatuses = ['clientDeleteProhibited', 'clientTransferProhibited', 'clientUpdateProhibited'];
$serverStatuses = ['serverDeleteProhibited', 'serverTransferProhibited', 'serverUpdateProhibited'];
2023-07-13 12:39:15 +00:00
2023-07-13 12:42:39 +00:00
// Check if all client statuses are present in the $statuses array
$clientProhibited = count(array_intersect($clientStatuses, $statuses)) === count($clientStatuses);
2023-07-13 12:39:15 +00:00
2023-07-13 12:42:39 +00:00
// Check if all server statuses are present in the $statuses array
$serverProhibited = count(array_intersect($serverStatuses, $statuses)) === count($serverStatuses);
2023-07-13 12:39:15 +00:00
2023-07-13 12:42:39 +00:00
if ($clientProhibited || $serverProhibited) {
$locked = 1;
2023-07-13 12:42:39 +00:00
} else {
$locked = 0;
2023-07-13 12:42:39 +00:00
}
2023-07-13 12:39:15 +00:00
2023-07-13 12:42:39 +00:00
// Prepare the UPDATE statement
$stmt = $pdo->prepare('UPDATE service_domain SET ns1 = :ns1, ns2 = :ns2, ns3 = :ns3, ns4 = :ns4, expires_at = :expires_at, locked = :locked, transfer_code = :transfer_code WHERE sld = :sld AND tld = :tld');
2023-07-13 12:39:15 +00:00
2023-07-13 12:42:39 +00:00
// Bind the values to the statement
$stmt->bindValue(':ns1', $ns1);
$stmt->bindValue(':ns2', $ns2);
$stmt->bindValue(':ns3', $ns3);
$stmt->bindValue(':ns4', $ns4);
$stmt->bindValue(':expires_at', $formattedExDate);
$stmt->bindValue(':locked', $locked);
2023-07-13 12:39:15 +00:00
$stmt->bindValue(':transfer_code', $domainInfo["authInfo"]);
2023-07-13 12:42:39 +00:00
$stmt->bindValue(':sld', $domainRow['sld']);
$stmt->bindValue(':tld', $domainRow['tld']);
2023-07-13 12:39:15 +00:00
// Execute the statement
$stmt->execute();
2023-07-13 12:39:15 +00:00
echo "Update successful for domain: " . $domain . PHP_EOL;
}
$logout = $epp->logout();
echo "Logout Result: " . $logout["code"] . ": " . $logout["msg"][0] . PHP_EOL;
} catch (PDOException $e) {
echo "Database error: " . $e->getMessage();
} catch (EppException $e) {
2023-07-13 12:39:15 +00:00
echo "Error: ", $e->getMessage();
}