Skip to content

Commit

Permalink
Add 'edit' action to Custom DNS endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Butt <git@martinbutt.com>
  • Loading branch information
martinbutt committed Sep 29, 2022
1 parent 17487b3 commit 3baed89
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
5 changes: 5 additions & 0 deletions api.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@

break;

case 'edit':
$data = editCustomDNSEntry();

break;

case 'delete':
$data = deleteCustomDNSEntry();

Expand Down
3 changes: 3 additions & 0 deletions scripts/pi-hole/php/customdns.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
case 'add': echo json_encode(addCustomDNSEntry());
break;

case 'edit': echo json_encode(editCustomDNSEntry());
break;

case 'delete': echo json_encode(deleteCustomDNSEntry());
break;

Expand Down
46 changes: 46 additions & 0 deletions scripts/pi-hole/php/func.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,52 @@ function addCustomDNSEntry($ip = '', $domain = '', $reload = '', $json = true)
}
}

function editCustomDNSEntry($ip = '', $domain = '', $reload = '', $json = true)
{
try {
$ip = !empty($_REQUEST['ip']) ? trim($_REQUEST['ip']) : $ip;
$domain = !empty($_REQUEST['domain']) ? trim($_REQUEST['domain']) : $domain;
$reload = !empty($_REQUEST['reload']) ? trim($_REQUEST['reload']) : $reload;

if (empty($ip)) {
return returnError('IP must be set', $json);
}

$ipType = get_ip_type($ip);

if (!$ipType) {
return returnError('IP must be valid', $json);
}

if (empty($domain)) {
return returnError('Domain must be set', $json);
}

if (!validDomain($domain)) {
return returnError('Domain must be valid', $json);
}

$existingEntries = getCustomDNSEntries();

foreach ($existingEntries as $entry) {
if ($entry->domain == $domain) {
$old_ip = $entry->ip;
break;
}
}

if (isset($old_ip)) {
pihole_execute('-a removecustomdns '.$old_ip.' '.$domain);
}

pihole_execute('-a addcustomdns '.$ip.' '.$domain.' '.$reload);

return returnSuccess('', $json);
} catch (\Exception $ex) {
return returnError($ex->getMessage(), $json);
}
}

function deleteCustomDNSEntry()
{
try {
Expand Down

0 comments on commit 3baed89

Please sign in to comment.