Skip to content

Instantly share code, notes, and snippets.

@rkulow
Forked from marcelschmidtdev/upd.php
Last active December 28, 2019 22:20
Show Gist options
  • Save rkulow/3ca07fe29da7ee17621c0f6dc66c5798 to your computer and use it in GitHub Desktop.
Save rkulow/3ca07fe29da7ee17621c0f6dc66c5798 to your computer and use it in GitHub Desktop.
Fritz!Box DynDNS update script for Cloudflare
<?php
// This solution contains two options for reading the records for updating:
// 1) file-mode: reading all records from external file
// 2) parameter-mode: reading all records from url parameter 'hosts'
// It is possible to use both option simultaneous.
// Fritz!Box update URL using hosts from external file: https://example.com/upd.php?zoneId={zoneId}&key=<pass>&file={filename}&ip=<ipaddr>
// If you want to read your records from a file, specify the file name as parameter.
// Therefore the file should be in the same directory as this php file.
// The file should contain on each line one record, eg:
// abc.example.com
// def.example.com
// Fritz!Box update URL using hosts from parameter: https://example.com/upd.php?zoneId={zoneId}&key=<pass>&hosts=<domain>&ip=<ipaddr>
// If you want to update multiple records of your zone using the hosts parameter, just add them comma separated to the domain field
// e.g. "example.com,sub.example.com,sub2.example.com,*.example.com"
// The zoneId field should matches your zone id form cloudflare and the key filed your cloudflare api token.
// This token should have the permission 'DNS:Edit' set.
$ip = $_GET['ip'];
$zone_id = $_GET['zoneId'];
$key = $_GET['key'];
$hosts = array();
$api_base = 'https://api.cloudflare.com/client/v4/zones';
if ($zone_id == NULL || $key == NULL)
{
echo "Please check your provided zoneId and authentication token.<br/>";
return;
}
if (isset($_GET['hosts']) && $_GET['hosts'] != Null)
$hosts = explode(',', $_GET['hosts']);
if (isset($_GET['file']) && $_GET['file'] != NULL) {
$file_lines = file($_GET['file']);
foreach ($file_lines as $line)
{
if(trim($line) !== '')
array_push($hosts, $line);
}
}
// get record identifier
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_base . '/' . $zone_id . '/dns_records?type=A');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $key, 'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = json_decode(curl_exec($ch));
curl_close($ch);
if($response -> success == FALSE)
{
foreach ($response -> errors as $value)
{
echo 'Error fetching record identifier:<br/>';
echo $value -> message;
echo '<br/>';
}
return;
}
$records = array();
foreach ($response -> result as $value)
{
if(in_array($value -> name, $hosts))
{
array_push($records, $value);
}
}
// update record(s)
$mh = curl_multi_init();
$ch_arr = array();
$results = array();
foreach ( $records as $rec)
{
$record_name = $rec -> name;
$record_id = $rec -> id;
$put_data = array('type' => 'A', 'name' => $record_name, 'content' => $ip, 'ttl' => 120, 'proxied' => FALSE );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_base . '/' . $zone_id . '/dns_records/' . $record_id);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $key, 'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($put_data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
array_push($ch_arr, $ch);
curl_multi_add_handle($mh, $ch);
}
$running = null;
do
{
curl_multi_exec($mh, $running);
}
while($running > 0);
foreach ($ch_arr as $ch)
{
array_push($results, json_decode(curl_multi_getcontent($ch)));
curl_multi_remove_handle($mh, $ch);
}
curl_multi_close($mh);
foreach ($results as $result)
{
if($result -> success == FALSE)
{
foreach ($result -> errors as $index => $value)
{
echo 'Error updating domain ' . $hosts[$index] . ':<br/>';
echo $value -> message;
echo '<br/>';
}
return;
}
}
echo 'SUCCESS';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment