Skip to content

Commit

Permalink
Improve custom DNS hosts parsing
Browse files Browse the repository at this point in the history
Handle variable whitespace and comments in the custom DNS hosts file.

Also, correctly fill the "domains" field in getCustomDNSEntries output.

Signed-off-by: Nick Forrington <nick.forrington@gmail.com>
  • Loading branch information
NickRF committed Aug 10, 2024
1 parent 88acc11 commit cf54428
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions scripts/pi-hole/php/func.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,18 +200,22 @@ function getCustomDNSEntries()
$handle = fopen($customDNSFile, 'r');
if ($handle) {
while (($line = fgets($handle)) !== false) {
$line = str_replace("\r", '', $line);
$line = str_replace("\n", '', $line);
$explodedLine = explode(' ', $line);
$line = trim($line);

if (count($explodedLine) != 2) {
if (!$line || $line[0] == '#') {
continue;
}

$explodedLine = preg_split('/\s+/', $line);

if (count($explodedLine) < 2) {
continue;
}

$data = new stdClass();
$data->ip = $explodedLine[0];
$data->domain = $explodedLine[1];
$data->domains = array_slice($explodedLine, 0, -1);
$data->domains = array_slice($explodedLine, 1);
$entries[] = $data;
}

Expand Down

0 comments on commit cf54428

Please sign in to comment.