Directly manipulate regex.list from PHP to avoid detour over bash. This needs read/write permissions of the PHP user on "/etc/pihole/regex.list" !

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER
2018-06-23 15:50:19 +02:00
parent 3f7246968e
commit 1935544183
5 changed files with 37 additions and 9 deletions

View File

@@ -34,12 +34,10 @@ switch($type) {
}
break;
case "wild":
if(!isset($_POST["auditlog"]))
echo exec("sudo pihole -wild -q ${_POST['domain']}");
else
if(file_put_contents($regexfile, $_POST['domain']."\n", FILE_APPEND) === FALSE)
{
echo exec("sudo pihole -wild -q -n ${_POST['domain']}");
echo exec("sudo pihole -a audit ${_POST['domain']}");
$err = error_get_last()["message"];
echo "Unable to add regex \"".htmlspecialchars($_POST['domain'])."\" to ${regexfile}<br>Error message: $err";
}
case "audit":
echo exec("sudo pihole -a audit ${_POST['domain']}");

View File

@@ -11,6 +11,7 @@ $ERRORLOG = getenv('PHP_ERROR_LOG');
if (empty($ERRORLOG)) {
$ERRORLOG = '/var/log/lighttpd/error.log';
}
$regexfile = "/etc/pihole/regex.list";
function pi_log($message) {
error_log(date('Y-m-d H:i:s') . ': ' . $message . "\n", 3, $GLOBALS['ERRORLOG']);
@@ -133,6 +134,14 @@ function list_verify($type) {
{
log_and_die("Not allowed!");
}
check_domain();
// Don't check if the added item is a
// valid domain for regex expressions
// Regex filters are validated by FTL
// on import and skipped if invalid
if($_POST['list'] !== "wild")
{
check_domain();
}
}
?>

View File

@@ -44,7 +44,10 @@ function getListContent($listname) {
unset($list[$i]);
}
return $list;
// Re-index list after possible unset() activity
$newlist = array_values($list);
return $newlist;
}

View File

@@ -22,7 +22,24 @@ switch($type) {
exec("sudo pihole -b -q -d ${_POST['domain']}");
break;
case "wild":
exec("sudo pihole -wild -q -d ${_POST['domain']}");
if(($list = file_get_contents($regexfile)) === FALSE)
{
$err = error_get_last()["message"];
echo "Unable to read ${regexfile}<br>Error message: $err";
}
// Replace regex with empty line ...
$list = str_replace($_POST['domain'], '', $list);
// ... and remove all empty lines from the file
$tmp = explode("\n", $list);
$tmp = array_filter($tmp);
$list = implode("\n", $tmp);
if(file_put_contents($regexfile, $list) === FALSE)
{
$err = error_get_last()["message"];
echo "Unable to remove regex \"".htmlspecialchars($_POST['domain'])."\" from ${regexfile}<br>Error message: $err";
}
break;
}