-
Notifications
You must be signed in to change notification settings - Fork 0
/
fredSync.php
164 lines (134 loc) · 5.51 KB
/
fredSync.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
require_once "fredEpp.php";
use Pinga\Tembo\FredEpp;
$config = include "config.php";
$c = $config["db"];
$registrar = "FRED";
try
{
// 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)
{
$config = json_decode($row["config"], true);
$registrar_id = $row["id"];
}
if (empty($config))
{
throw new Exception("Database cannot be accessed right now.");
}
}
catch(PDOException $e)
{
echo "Database error: " . $e->getMessage();
}
catch(Exception $e)
{
echo "General error: " . $e->getMessage();
}
function connectEpp(string $registry, $config)
{
try
{
$epp = new FredEpp();
$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, ];
$epp->connect($info);
$login = $epp->login(["clID" => $config["username"], "pw" => $config["password"],
"prefix" => "tembo", ]);
if (array_key_exists("error", $login))
{
echo "Login Error: " . $login["error"] . PHP_EOL;
exit();
}
else
{
echo "Login Result: " . $login["code"] . ": " . $login["msg"][0] . PHP_EOL;
}
return $epp;
}
catch(EppException $e)
{
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);
$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;
}
$ns = $domainInfo['ns'];
if (isset($ns[1])) {
$params = ["hostname" => $ns[1]];
$hostInfo = $epp->hostInfo($params);
$ns1 = isset($hostInfo['addr'][0]) ? $hostInfo['addr'][0] : null;
$ns2 = isset($hostInfo['addr'][1]) ? $hostInfo['addr'][1] : null;
$ns3 = isset($hostInfo['addr'][2]) ? $hostInfo['addr'][2] : null;
$ns4 = isset($hostInfo['addr'][3]) ? $hostInfo['addr'][3] : null;
} else {
$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;
}
$exDate = $domainInfo['exDate'];
$datetime = new DateTime($exDate);
$formattedExDate = $datetime->format('Y-m-d H:i:s');
$statuses = $domainInfo['status'];
$clientStatuses = ['clientDeleteProhibited', 'clientTransferProhibited', 'clientUpdateProhibited'];
$serverStatuses = ['serverDeleteProhibited', 'serverTransferProhibited', 'serverUpdateProhibited'];
// Check if all client statuses are present in the $statuses array
$clientProhibited = count(array_intersect($clientStatuses, $statuses)) === count($clientStatuses);
// Check if all server statuses are present in the $statuses array
$serverProhibited = count(array_intersect($serverStatuses, $statuses)) === count($serverStatuses);
if ($clientProhibited || $serverProhibited) {
$locked = 1;
} else {
$locked = 0;
}
// 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');
// 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);
$stmt->bindValue(':transfer_code', $domainInfo["authInfo"]);
$stmt->bindValue(':sld', $domainRow['sld']);
$stmt->bindValue(':tld', $domainRow['tld']);
// Execute the statement
$stmt->execute();
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) {
echo "Error: ", $e->getMessage();
}