mirror of
https://github.com/pi-hole/web.git
synced 2026-04-26 11:50:09 +01:00
Execute pihole through wrapper
All different exec() calls basically just calls sudo pihole, with various different syntaxes. Using a wrapper function allows sanitization of arguments for better safety. Signed-off-by: Samu Voutilainen <smar@smar.fi>
This commit is contained in:
@@ -52,4 +52,33 @@ if(!function_exists('hash_equals')) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* More safely execute a command with pihole shell script.
|
||||
*
|
||||
* For example,
|
||||
*
|
||||
* pihole_execute("-h");
|
||||
*
|
||||
* would execute command
|
||||
*
|
||||
* sudo pihole -h
|
||||
*
|
||||
* and returns output of that command as a string.
|
||||
*
|
||||
* @param $argument_string String of arguments to run pihole with.
|
||||
* @param $error_on_failure If true, a warning is raised if command execution fails. Defaults to true.
|
||||
*/
|
||||
function pihole_execute($argument_string, $error_on_failure = true) {
|
||||
$escaped = escapeshellcmd($argument_string);
|
||||
$output = null;
|
||||
$return_status = -1;
|
||||
$command = "sudo pihole " . $escaped;
|
||||
exec($command, $output, $return_status);
|
||||
if($return_status !== 0)
|
||||
{
|
||||
trigger_error("Executing {$command} failed.", E_USER_WARNING);
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user