Skip to content

Instantly share code, notes, and snippets.

@gregdotca
Created September 6, 2019 23:03
Show Gist options
  • Save gregdotca/a2c05bca2dc21d3a09ebaa6856d05b44 to your computer and use it in GitHub Desktop.
Save gregdotca/a2c05bca2dc21d3a09ebaa6856d05b44 to your computer and use it in GitHub Desktop.
PHP script (with form) to update the Pi-hole whitelist and blacklist entries
<?php
//////////////////////////////////////////////////
// This script is still a work in progress...
//////////////////////////////////////////////////
// Whitelist: list=w / Blacklist: list=b
// Add: action=a / Delete: action=d
// Domain: domain=example.com
// Example: http://pihole/wb.php?list=w&action=a&example.com (This will add example.com to the Pi-hole whitelist)
//////////////////////////////////////////////////
//////////////////////////////////////////////////
// Settings
//////////////////////////////////////////////////
$pihole_url = 'http://pihole';
//////////////////////////////////////////////////
$list = $_REQUEST['list'];
$action = $_REQUEST['action'];
$domain = $_REQUEST['domain'];
if (isset($_SERVER['QUERY_STRING']) && $list != '' && $action != '' && $domain != '') {
if (substr($domain, 0, 4) === 'www.') {
$domains[0] = $domain;
$domains[1] = substr($domain, 4);
} else {
$domains[0] = $domain;
$domains[1] = 'www.' . $domain;
}
foreach ($domains AS $each_domain) {
if ($action === 'a') {
exec("sudo pihole -$list $each_domain", $output, $status);
} elseif ($action === 'd') {
exec("sudo pihole -$list -d $each_domain", $output, $status);
}
}
$wl_or_bl = ($list === 'w' ? 'WHITELIST' : 'BLACKLIST');
$a_or_d = ($action === 'a' ? 'ADD' : 'DELETE');
$a_or_d_undo = ($action === 'a' ? 'd' : 'a');
print("<pre>");
for ($i = 0; $i < sizeof($output); $i++) {
print(str_replace("\u{001b}[K", "", $output[$i]) . "<br />\n");
}
print("</pre>");
} elseif ($_SERVER['QUERY_STRING'] != '') {
$error_message = "<BR><strong><font color='#FF0000'>Something went wrong. Please try again.</font></strong><BR><BR>";
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
</head>
<body onLoad="document.forms[0].elements[2].focus()">
<font color="#000000" size="2" face="Verdana, Arial, Helvetica, sans-serif">
<?php if (isset($error_message) && $error_message != '') echo $error_message; ?>
<h3>Update Pi-hole's Whitelist or Blacklist</h3>
<form method="post">
<p>
<label>Which list do you want to update?</label>
<select name="list">
<option value="w"<?php if ($list == 'w') echo ' selected'; ?>>Whitelist</option>
<option value="b"<?php if ($list == 'b') echo ' selected'; ?>>Blacklist</option>
</select>
</p>
<p>
<label>Do you want to add or delete the domain?</label>
<select name="action">
<option value="a"<?php if ($action == 'a') echo ' selected'; ?>>Add</option>
<option value="d"<?php if ($action == 'd') echo ' selected'; ?>>Delete</option>
</select>
</p>
<p>
<label>Domain: </label>
<input type="text" name="domain" value="<?php echo $domain; ?>">
</p>
<p>
<button type="submit">Submit</button>
</p>
</form>
</font>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment