-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathnamesilo-domain-sync.php
More file actions
172 lines (138 loc) · 5.21 KB
/
namesilo-domain-sync.php
File metadata and controls
172 lines (138 loc) · 5.21 KB
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
165
166
167
168
169
170
171
172
<?php
if (isset($_SERVER['REMOTE_ADDR'])) {
exit ('ERROR: Script called from web environment');
}
/*****************************************/
/* Grab Required Includes */
/*****************************************/
$module_dir = dirname(__FILE__);
chdir($module_dir);
require '../../../init.php';
require '../../../includes/functions.php';
require '../../../includes/registrarfunctions.php';
use WHMCS\Database\Capsule;
function mapDomainStatus(string $status)
{
$statusMapping = [
'Active' => 'Active',
'Expired (grace period)' => 'Grace',
'Expired (restore period)' => 'Redemption',
];
return $statusMapping[$status] ?? null;
}
/*****************************************/
/* Transaction Processor Function */
/*****************************************/
function transactionProcessor($call)
{
# Set CURL Options
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_USERAGENT => "NameSilo Domain Sync 1.3", // For use with WHMCS 6x
);
# Initialize CURL
$ch = curl_init($call);
# Import CURL options array
curl_setopt_array($ch, $options);
# Execute and store response
$content = curl_exec( $ch );
# Process any CURL errors
$curl_err = false;
if (curl_error ( $ch )) {
$curl_err = 'CURL Error: ' . curl_errno ( $ch ) . ' - ' . curl_error ( $ch );
exit ( 'CURL Error: ' . curl_errno ( $ch ) . ' - ' . curl_error ( $ch ) );
}
# Log error(s)
if ($curl_err) { $cronreport .= "Error connecting to API: $curl_err"; }
curl_close( $ch );
# Process XML result
$xml = new SimpleXMLElement($content);
$result ["expiry"] = (string) $xml->reply->expires;
$result ["status"] = (string) $xml->reply->status;
# Return result
return $result;
}
/*****************************************/
/* Get Registrar Config Options */
/*****************************************/
$params = getregistrarconfigoptions('namesilo');
/*****************************************/
/* Define Test Mode */
/*****************************************/
$ns_test_mode = @$params['Test_Mode'];
/*****************************************/
/* Define Sync Due Date */
/*****************************************/
$ns_sync_next_due_date = @$params ['Sync_Next_Due_Date'];
/*****************************************/
/* Define API Servers */
/*****************************************/
$ns_live_api_server = 'https://www.namesilo.com';
$ns_test_api_server = 'https://sandbox.namesilo.com';
/*****************************************/
/* Define API Keys */
/*****************************************/
$ns_test_api_key = @$params['Sandbox_API_Key'];
$ns_live_api_key = @$params['Live_API_Key'];
/*****************************************/
/* Check Test Mode and Assign Variables */
/*****************************************/
# Set appropriate API server
$apiServerUrl = (@$ns_test_mode == 'on') ? $ns_test_api_server : $ns_live_api_server;
# Set appropriate API key
$apiKey = (@$ns_test_mode == 'on') ? $ns_test_api_key : $ns_live_api_key;
/*****************************************/
/* Implement Updates */
/*****************************************/
# Start cron report
$cronreport = 'NameSilo Domain Sync Report<br>
---------------------------------------------------<br>
';
# Query WHMCS for a list of domains using NameSilo
$queryresult = Capsule::table("tbldomains")
->select("domain")
->where("registrar", '=', "namesilo")
->where(function ($query) {
$query
->where("status", '=', "Pending")
->orWhere("status", '=', "Active")
->orWhere("status", '=', "Grace")
->orWhere("status", '=', "Redemption");
})->get()->toArray();
# Loop through domain useing NameSilo
foreach ($queryresult as $data) {
$data = (array) $data;
$domainname = trim(strtolower($data['domain']));
# Build API call
$result = transactionProcessor($apiServerUrl . "/api/getDomainInfo?version=1&type=xml&key=$apiKey&domain=$domainname");
# Process results
if (!empty($result["expiry"])) {
$expirydate = $result ["expiry"];
if (mapDomainStatus($result ["status"]) !== null) {
Capsule::table("tbldomains")->where("domain", '=', $domainname)->update(["status" => mapDomainStatus($result["status"])]);
}
if ($expirydate) {
Capsule::table("tbldomains")->where("domain", '=', $domainname)->update(["expirydate" => $expirydate]);
if (@$ns_sync_next_due_date == 'on') {
Capsule::table("tbldomains")->where("domain", '=', $domainname)->update(["nextduedate" => $expirydate]);
}
$cronreport .= '' . 'Updated ' . $domainname . ' expiry to ' . frommysqldate ( $expirydate ) . '<br>';
}
} else {
$cronreport .= '' . 'ERROR: ' . $domainname . ' - Domain does not appear in the account at NameSilo<br>';
}
}
/*****************************************/
/* Echo to the screen */
/*****************************************/
echo $cronreport;
/*****************************************/
/* Log System Activity */
/*****************************************/
logactivity ( 'NameSilo Domain Sync Run' );
/*****************************************/
/* Send Cron Report */
/*****************************************/
sendadminnotification ( 'system', 'NameSilo Domain Syncronization Report', $cronreport );
?>