2020-03-26 20:45:39 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once 'config.php';
|
|
|
|
|
|
|
|
/**
|
2020-03-27 14:02:21 +01:00
|
|
|
* Manages ad blocking (dnsmasq) configuration
|
2020-03-26 20:45:39 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
function DisplayAdBlockConfig()
|
|
|
|
{
|
2023-09-16 11:46:11 +02:00
|
|
|
$status = new \RaspAP\Messages\StatusMessage;
|
2020-03-29 01:15:25 +01:00
|
|
|
$enabled = false;
|
2020-09-19 23:26:52 +02:00
|
|
|
$custom_enabled = false;
|
2020-03-27 14:02:21 +01:00
|
|
|
|
|
|
|
if (!RASPI_MONITOR_ENABLED) {
|
|
|
|
if (isset($_POST['saveadblocksettings'])) {
|
|
|
|
if ($_POST['adblock-enable'] == "1") {
|
2020-03-29 01:15:25 +01:00
|
|
|
$config = 'conf-file=' .RASPI_ADBLOCK_LISTPATH .'domains.txt'.PHP_EOL;
|
|
|
|
$config.= 'addn-hosts=' .RASPI_ADBLOCK_LISTPATH .'hostnames.txt'.PHP_EOL;
|
|
|
|
} elseif ($_POST['adblock-enable'] == "0") {
|
|
|
|
$config = null;
|
2020-03-27 14:02:21 +01:00
|
|
|
}
|
2020-09-19 23:26:52 +02:00
|
|
|
if ($_POST['adblock-custom-enable'] == "1") {
|
|
|
|
// validate custom hosts input
|
|
|
|
$lines = preg_split('/\r\n|\n|\r/', trim($_POST['adblock-custom-hosts']));
|
2020-09-20 10:42:13 +02:00
|
|
|
if (!in_array("", $lines, true)) {
|
|
|
|
foreach ($lines as $line) {
|
|
|
|
$ip_host = preg_split('/\s+/', $line);
|
|
|
|
$index++;
|
|
|
|
if (!filter_var($ip_host[0], FILTER_VALIDATE_IP)) {
|
|
|
|
$errors .= _('Invalid custom IP address found on line '.$index);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!validate_host($ip_host[1])) {
|
|
|
|
$errors .= _('Invalid custom host found on line '.$index);
|
|
|
|
break;
|
|
|
|
}
|
2020-09-19 23:26:52 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-20 10:42:13 +02:00
|
|
|
file_put_contents("/tmp/dnsmasq_custom", $_POST['adblock-custom-hosts'].PHP_EOL);
|
2020-09-19 23:26:52 +02:00
|
|
|
system("sudo cp /tmp/dnsmasq_custom " .RASPI_ADBLOCK_LISTPATH .'custom.txt', $return);
|
|
|
|
$config.= 'addn-hosts=' .RASPI_ADBLOCK_LISTPATH .'custom.txt'.PHP_EOL;
|
|
|
|
$custom_enabled = true;
|
|
|
|
}
|
2020-03-27 14:02:21 +01:00
|
|
|
|
2020-09-19 23:26:52 +02:00
|
|
|
if (empty($errors)) {
|
|
|
|
file_put_contents("/tmp/dnsmasqdata", $config);
|
|
|
|
system('sudo cp /tmp/dnsmasqdata '.RASPI_ADBLOCK_CONFIG, $return);
|
|
|
|
if ($return == 0) {
|
|
|
|
$status->addMessage('Adblock configuration updated successfully', 'success');
|
|
|
|
} else {
|
|
|
|
$status->addMessage('Adblock configuration failed to be updated.', 'danger');
|
|
|
|
}
|
2020-03-27 14:02:21 +01:00
|
|
|
} else {
|
2020-09-19 23:26:52 +02:00
|
|
|
$status->addMessage($errors, 'danger');
|
2020-03-27 14:02:21 +01:00
|
|
|
}
|
2020-03-29 01:15:25 +01:00
|
|
|
} elseif (isset($_POST['restartadblock']) || isset($_POST['startadblock'])) {
|
|
|
|
exec('sudo /bin/systemctl restart dnsmasq.service', $dnsmasq, $return);
|
|
|
|
if ($return == 0) {
|
|
|
|
$status->addMessage('Adblock restart successful', 'success');
|
|
|
|
} else {
|
|
|
|
$status->addMessage('Adblock failed to restart.', 'danger');
|
|
|
|
}
|
2020-03-27 14:02:21 +01:00
|
|
|
}
|
|
|
|
}
|
2020-03-26 20:45:39 +01:00
|
|
|
|
2020-03-29 01:15:25 +01:00
|
|
|
exec('cat '. RASPI_ADBLOCK_CONFIG, $return);
|
|
|
|
$arrConf = ParseConfig($return);
|
|
|
|
if (sizeof($arrConf) > 0) {
|
|
|
|
$enabled = true;
|
|
|
|
}
|
|
|
|
|
2020-03-26 20:45:39 +01:00
|
|
|
exec('pidof dnsmasq | wc -l', $dnsmasq);
|
|
|
|
$dnsmasq_state = ($dnsmasq[0] > 0);
|
2020-03-29 01:15:25 +01:00
|
|
|
$serviceStatus = $dnsmasq_state && $enabled ? "up" : "down";
|
2020-03-27 14:02:21 +01:00
|
|
|
|
2023-09-03 09:47:51 +02:00
|
|
|
if (file_exists(RASPI_ADBLOCK_LISTPATH .'custom.txt')) {
|
|
|
|
$adblock_custom_content = file_get_contents(RASPI_ADBLOCK_LISTPATH .'custom.txt');
|
|
|
|
} else {
|
|
|
|
$adblock_custom_content = '';
|
|
|
|
}
|
2021-02-02 13:26:14 +01:00
|
|
|
$adblock_log = '';
|
2022-03-31 11:47:55 +02:00
|
|
|
exec('sudo chmod o+r '.RASPI_DHCPCD_LOG);
|
|
|
|
$handle = fopen(RASPI_DHCPCD_LOG, "r");
|
2021-02-02 13:26:14 +01:00
|
|
|
if ($handle) {
|
|
|
|
while (($line = fgets($handle)) !== false) {
|
2023-10-06 14:42:33 +02:00
|
|
|
if (preg_match('/(is 0.0.0.0)|(using only locally-known addresses)/', $line)) {
|
2021-02-02 13:26:14 +01:00
|
|
|
$adblock_log .= $line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose($handle);
|
|
|
|
} else {
|
|
|
|
$adblock_log = "Unable to open log file";
|
|
|
|
}
|
|
|
|
|
2020-03-26 20:45:39 +01:00
|
|
|
echo renderTemplate(
|
|
|
|
"adblock", compact(
|
2020-04-03 08:22:45 +02:00
|
|
|
"status",
|
|
|
|
"serviceStatus",
|
|
|
|
"dnsmasq_state",
|
2020-09-19 23:26:52 +02:00
|
|
|
"enabled",
|
2021-02-02 13:26:14 +01:00
|
|
|
"custom_enabled",
|
|
|
|
"adblock_custom_content",
|
|
|
|
"adblock_log"
|
2020-03-26 20:45:39 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|