forked from stecklars/dynamic-dns-netcup-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
274 lines (219 loc) · 7.04 KB
/
functions.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
require_once 'config.php';
//Declare possbile options
$quiet = false;
//Check passed options
if(isset($argv)){
foreach ($argv as $option) {
if ($option === "--quiet") {
$quiet = true;
}
}
}
const SUCCESS = 'success';
// Sends $request to netcup Domain API and returns the result
function sendRequest($request)
{
$ch = curl_init(APIURL);
$curlOptions = array(
CURLOPT_POST => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
CURLOPT_POSTFIELDS => $request,
);
curl_setopt_array($ch, $curlOptions);
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result, true);
return $result;
}
//Outputs $text to Stdout
function outputStdout($message)
{
global $quiet;
//If quiet option is set, don't output anything on stdout
if ($quiet === true) {
return;
}
$date = date("Y/m/d H:i:s O");
$output = sprintf("[%s][NOTICE] %s\n", $date, $message);
echo $output;
}
//Outputs warning to stderr
function outputWarning($message)
{
$date = date("Y/m/d H:i:s O");
$output = sprintf("[%s][WARNING] %s\n", $date, $message);
fwrite(STDERR, $output);
}
//Outputs error to Stderr
function outputStderr($message)
{
$date = date("Y/m/d H:i:s O");
$output = sprintf("[%s][ERROR] %s\n", $date, $message);
fwrite(STDERR, $output);
}
//Returns current public IPv4 address.
function getCurrentPublicIPv4()
{
$publicIP = rtrim(file_get_contents('https://api.ipify.org'));
//Let's check that this is really a IPv4 address, just in case...
if (filter_var($publicIP, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
return $publicIP;
}
outputWarning("https://api.ipify.org didn't return a valid IPv4 address. Trying fallback API https://ip4.seeip.org");
//If IP is invalid, try another API
//The API adds an empty line, so we remove that with rtrim
$publicIP = rtrim(file_get_contents('https://ip4.seeip.org'));
//Let's check the result of the second API
if (filter_var($publicIP, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
return $publicIP;
}
//Still no valid IP?
return false;
}
//Returns current public IPv6 address
function getCurrentPublicIPv6()
{
$publicIP = rtrim(file_get_contents('https://ip6.seeip.org'));
if (filter_var($publicIP, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
return $publicIP;
}
outputWarning("https://ip6.seeip.org didn't return a valid IPv6 address.");
//If IP is invalid, try another API
$publicIP = rtrim(file_get_contents('https://v6.ident.me/'));
//Let's check the result of the second API
if (filter_var($publicIP, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
return $publicIP;
}
//Still no valid IP?
return false;
}
//Login into netcup domain API and returns Apisessionid
function login($customernr, $apikey, $apipassword)
{
$logindata = array(
'action' => 'login',
'param' =>
array(
'customernumber' => $customernr,
'apikey' => $apikey,
'apipassword' => $apipassword,
),
);
$request = json_encode($logindata);
$result = sendRequest($request);
if ($result['status'] === SUCCESS) {
return $result['responsedata']['apisessionid'];
}
outputStderr(sprintf("Error while logging in: %s Exiting.", $result['longmessage']));
return false;
}
//Logout of netcup domain API, returns boolean
function logout($customernr, $apikey, $apisessionid)
{
$logoutdata = array(
'action' => 'logout',
'param' =>
array(
'customernumber' => $customernr,
'apikey' => $apikey,
'apisessionid' => $apisessionid,
),
);
$request = json_encode($logoutdata);
$result = sendRequest($request);
if ($result['status'] === SUCCESS) {
return true;
}
outputStderr(sprintf("Error while logging out: %s Exiting.", $result['longmessage']));
return false;
}
//Get info about dns zone from netcup domain API, returns result
function infoDnsZone($domainname, $customernr, $apikey, $apisessionid)
{
$infoDnsZoneData = array(
'action' => 'infoDnsZone',
'param' =>
array(
'domainname' => $domainname,
'customernumber' => $customernr,
'apikey' => $apikey,
'apisessionid' => $apisessionid,
),
);
$request = json_encode($infoDnsZoneData);
$result = sendRequest($request);
if ($result['status'] === SUCCESS) {
return $result;
}
outputStderr(sprintf("Error while getting DNS Zone info: %s Exiting.", $result['longmessage']));
return false;
}
//Get info about dns records from netcup domain API, returns result
function infoDnsRecords($domainname, $customernr, $apikey, $apisessionid)
{
$infoDnsRecordsData = array(
'action' => 'infoDnsRecords',
'param' =>
array(
'domainname' => $domainname,
'customernumber' => $customernr,
'apikey' => $apikey,
'apisessionid' => $apisessionid,
),
);
$request = json_encode($infoDnsRecordsData);
$result = sendRequest($request);
if ($result['status'] === SUCCESS) {
return $result;
}
outputStderr(sprintf("Error while getting DNS Record info: %s Exiting.", $result['longmessage']));
return false;
}
//Updates DNS Zone using the netcup domain API and returns boolean
function updateDnsZone($domainname, $customernr, $apikey, $apisessionid, $dnszone)
{
$updateDnsZoneData = array(
'action' => 'updateDnsZone',
'param' =>
array(
'domainname' => $domainname,
'customernumber' => $customernr,
'apikey' => $apikey,
'apisessionid' => $apisessionid,
'dnszone' => $dnszone,
),
);
$request = json_encode($updateDnsZoneData);
$result = sendRequest($request);
if ($result['status'] === SUCCESS) {
return true;
}
outputStderr(sprintf("Error while updating DNS Zone: %s Exiting.", $result['longmessage']));
return false;
}
//Updates DNS records using the netcup domain API and returns boolean
function updateDnsRecords($domainname, $customernr, $apikey, $apisessionid, $dnsrecords)
{
$updateDnsZoneData = array(
'action' => 'updateDnsRecords',
'param' =>
array(
'domainname' => $domainname,
'customernumber' => $customernr,
'apikey' => $apikey,
'apisessionid' => $apisessionid,
'dnsrecordset' => array(
'dnsrecords' => $dnsrecords,
),
),
);
$request = json_encode($updateDnsZoneData);
$result = sendRequest($request);
if ($result['status'] === SUCCESS) {
return true;
}
outputStderr(sprintf("Error while updating DNS Records: %s Exiting.", $result['longmessage']));
return false;
}