Skip to content

Commit d3a82fa

Browse files
committed
Add workaround for expired CCP DNS API sessions
1 parent c065cb6 commit d3a82fa

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

functions.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const VERSION = '4.0';
44
const SUCCESS = 'success';
5-
5+
const USERAGENT = "dynamic-dns-netcup-api/".VERSION." (by stecklars)";
66

77
//Check passed options
88
$shortopts = "q4:6:c:vh";
@@ -81,6 +81,7 @@ function initializeCurlHandlerPostNetcupAPI($request)
8181
$ch = curl_init(APIURL);
8282
$curlOptions = array(
8383
CURLOPT_POST => 1,
84+
CURLOPT_USERAGENT => USERAGENT,
8485
CURLOPT_TIMEOUT => 30,
8586
CURLOPT_RETURNTRANSFER => 1,
8687
CURLOPT_FAILONERROR => 1,
@@ -96,6 +97,7 @@ function initializeCurlHandlerGetIP($url)
9697
{
9798
$ch = curl_init($url);
9899
$curlOptions = array(
100+
CURLOPT_USERAGENT => USERAGENT,
99101
CURLOPT_TIMEOUT => 30,
100102
CURLOPT_RETURNTRANSFER => 1,
101103
CURLOPT_FAILONERROR => 1
@@ -149,8 +151,9 @@ function retryCurlRequest($ch, $tryCount, $tryLimit)
149151
}
150152

151153
// Sends $request to netcup Domain API and returns the result
152-
function sendRequest($request)
154+
function sendRequest($request, $apiSessionRetry = false)
153155
{
156+
154157
$ch = initializeCurlHandlerPostNetcupAPI($request);
155158
$result = curl_exec($ch);
156159

@@ -168,12 +171,26 @@ function sendRequest($request)
168171
exit(1);
169172
}
170173

174+
$result = json_decode($result, true);
175+
176+
if ($result['statuscode'] === 4001 && $apiSessionRetry === false) {
177+
outputWarning("Received API error 4001: The session id is not in a valid format. Most likely the session expired. Logging in again and retrying once.");
178+
$newApisessionid = login(CUSTOMERNR, APIKEY, APIPASSWORD);
179+
180+
global $apisessionid;
181+
$apisessionid = $newApisessionid;
182+
183+
$request = json_decode($request, true);
184+
$request['param']['apisessionid'] = $newApisessionid;
185+
$request = json_encode($request);
186+
187+
return sendRequest($request, true);
188+
}
189+
171190
// If everything seems to be ok, proceed...
172191
curl_close($ch);
173192
unset($ch);
174193

175-
$result = json_decode($result, true);
176-
177194
return $result;
178195
}
179196

0 commit comments

Comments
 (0)