#!/usr/bin/env php getZone($domain); if (!$zone) { if ($verbose) echo "Domain $domain not found\n" . PHP_EOL; $encounteredErrors += 1; continue; } $records = $api->getZoneDnsRecords($zone['id'], ['name' => $recordName]); $record = $records && $records[0]['name'] == $recordName ? $records[0] : null; if (!$record) { if ($verbose) echo "No existing record [$recordName] found. Creating a new one\n" . PHP_EOL; $ret = $api->createDnsRecord($zone['id'], 'A', $recordName, $ip, ['ttl' => $config['ttl']]); if ($ret) $updatedDomains[] = $pseudoDomainName; } elseif ($record['type'] != 'A' || $record['content'] != $ip || $record['ttl'] != $config['ttl']) { if ($verbose) echo "Updating record [$recordName] from [{$record['content']}] to [$ip].\n" . PHP_EOL; $ret = $api->updateDnsRecord($zone['id'], $record['id'], [ 'type' => 'A', 'name' => $recordName, 'content' => $ip, 'ttl' => $config['ttl'], ]); if ($ret) $updatedDomains[] = $pseudoDomainName; } else { if ($verbose) echo "Record [$recordName] appears OK. No need to update.\n" . PHP_EOL; } } catch (Exception $e) { if ($verbose) echo "Error: " . $e->getMessage() . "\n" . PHP_EOL; $encounteredErrors += 1; } } $date = new DateTime("now", new DateTimeZone('Europe/Amsterdam')); if (sizeof($updatedDomains) > 0) { echo '[' . $date->format('d-m-Y H:i:s') . ']: ' . sizeof($updatedDomains) . ' out of ' . sizeof($config['domainnames']) . ' domains updated to ' . $ip . ' [' . implode(', ', $updatedDomains) . ']' . PHP_EOL; } else { echo '[' . $date->format('d-m-Y H:i:s') . ']: No changes made' . ($encounteredErrors > 0 ? ', however with ' . $encounteredErrors . ' errors!' : '') . PHP_EOL; } function getIP($protocol) { /* $prefixes = ['ipv4' => 'ipv4.', 'ipv6' => 'ipv6.', 'auto' => '']; if (!isset($prefixes[$protocol])) { throw new Exception('Invalid "protocol" config value.'); } return trim(file_get_contents('http://' . $prefixes[$protocol] . 'icanhazip.com')); */return trim(file_get_contents('https://api.ipify.org')); } ?>