Created
January 6, 2023 10:15
-
-
Save maartendekeizer/90d2e9dfc6f9ff2005c4c4e89d243d20 to your computer and use it in GitHub Desktop.
Migratie DNS records from OpenProvider to OXXA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$opBaseUrl = 'https://api.openprovider.eu/v1beta'; | |
$opUsername = '***'; | |
$opPassword = '***'; | |
$oxxaBaseUrl = 'https://api.oxxa.com/command.php'; | |
$oxxaUsername = '***'; | |
$oxxaPassword = '***'; | |
$domainName = $argv[1]; | |
$domainTld = $argv[2]; | |
$opApiCall = $opBaseUrl . '/auth/login'; | |
$opCh = curl_init($opApiCall); | |
curl_setopt($opCh, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($opCh, CURLOPT_POSTFIELDS, json_encode(['username' => $opUsername, 'password' => $opPassword, 'ip' => '0.0.0.0'])); | |
$opResponse = curl_exec($opCh); | |
echo 'Response from OpenProvider: ' . $opResponse . PHP_EOL; | |
$opData = json_decode($opResponse, true); | |
$opToken = $opData['data']['token']; | |
curl_close($opCh); | |
$opApiCall = $opBaseUrl . '/dns/zones/' . $domainName . '.' . $domainTld . '?with_records=true'; | |
echo $opApiCall . PHP_EOL; | |
$opCh = curl_init($opApiCall); | |
curl_setopt($opCh, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $opToken]); | |
curl_setopt($opCh, CURLOPT_RETURNTRANSFER, true); | |
$opResponse = curl_exec($opCh); | |
echo 'Response from OpenProvider: ' . $opResponse . PHP_EOL; | |
$opData = json_decode($opResponse, true); | |
curl_close($opCh); | |
echo PHP_EOL; | |
echo PHP_EOL; | |
foreach ($opData['data']['records'] as $opRecord) { | |
echo 'Handling record: ' . json_encode($opRecord) . PHP_EOL; | |
if ($opRecord['type'] === 'NS') { | |
echo 'NS record, no action needed' . PHP_EOL . PHP_EOL; | |
continue; | |
} | |
if ($opRecord['type'] === 'SOA') { | |
echo 'NS record, no action needed' . PHP_EOL . PHP_EOL; | |
continue; | |
} | |
echo 'Copy record to Oxxa' . PHP_EOL; | |
$name = $opRecord['name']; | |
if (substr($name, -1 * strlen($domainName . '.' . $domainTld)) === ($domainName . '.' . $domainTld)) { | |
$name = substr($name, 0, strlen($name) - strlen($domainName . '.' . $domainTld)); | |
$name = rtrim($name, '.'); | |
} | |
$oxxaUrl = $oxxaBaseUrl . '?' . http_build_query(array_filter([ | |
'apiuser' => $oxxaUsername, | |
'apipassword' => $oxxaPassword, | |
'command' => 'dnsrecord_add', | |
'sld' => $domainName, | |
'tld' => $domainTld, | |
'value' => $name, | |
'type' => $opRecord['type'], | |
'data' => $opRecord['value'], | |
'priority' => isset($opRecord['prio']) ? $opRecord['prio'] : null, | |
'ttl' => $opRecord['ttl'] | |
])); | |
//echo $oxxaUrl . PHP_EOL; | |
$oxxaCh = curl_init($oxxaUrl); | |
curl_setopt($oxxaCh, CURLOPT_RETURNTRANSFER, true); | |
$oxxaResponse = curl_exec($oxxaCh); | |
$dom = new DOMDocument('1.0', 'UTF-8'); | |
$dom->loadXML($oxxaResponse); | |
$oxxaStatusCode = ($dom->getElementsByTagName('status_code')->length === 1) ? $dom->getElementsByTagName('status_code')->item(0)->textContent : null; | |
$oxxaStatusDescription = ($dom->getElementsByTagName('status_description')->length === 1) ? $dom->getElementsByTagName('status_description')->item(0)->textContent : null; | |
if ($oxxaStatusCode === null || $oxxaStatusDescription === null) { | |
echo 'Full response from Oxxa: ' . $oxxaResponse . PHP_EOL; | |
} else { | |
echo 'Response from Oxxa: ' . $oxxaStatusCode. ' - ' . $oxxaStatusDescription . PHP_EOL; | |
} | |
curl_close($oxxaCh); | |
echo PHP_EOL; | |
} | |
echo 'Done' . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment