Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pi-hole Teleporter #369

Merged
merged 17 commits into from
Jan 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 148 additions & 0 deletions scripts/pi-hole/php/teleporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php
require('func.php');
function process_zip($name)
{
global $zip;
$zippointer = $zip->getStream($name);
if(!$zippointer)
{
echo "$name not found in provided ZIP file, skipping...<br>";
return;
}
$contents = "";
while (!feof($zippointer)) {
$contents .= fread($zippointer, 4096);
}
fclose($zippointer);
$domains = array_filter(explode("\n",$contents));
check_domains($domains);
return $domains;
}

function add_to_zip($path,$name)
{
global $zip;
if(file_exists($path.$name))
$zip->addFile($path.$name,$name);
}

function check_domains($domains)
{
foreach($domains as $domain)
{
if(!is_valid_domain_name($domain)){
die(htmlspecialchars($domain).' is not a valid domain');
}
}
}

function getWildcardListContent() {
if(file_exists("/etc/dnsmasq.d/03-pihole-wildcard.conf"))
{
$rawList = file_get_contents("/etc/dnsmasq.d/03-pihole-wildcard.conf");
$wclist = explode("\n", $rawList);
$list = [];

foreach ($wclist as $entry) {
$expl = explode("/", $entry);
if(count($expl) == 3)
{
array_push($list,$expl[1]);
}
}

return implode("\n",array_unique($list));
}

return "";
}

if($_POST["action"] == "in")
{
if($_FILES["zip_file"]["name"])
{
$filename = $_FILES["zip_file"]["name"];
$source = $_FILES["zip_file"]["tmp_name"];
$type = $_FILES["zip_file"]["type"];

$name = explode(".", $filename);
$accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
$okay = false;
foreach($accepted_types as $mime_type) {
if($mime_type == $type) {
$okay = true;
break;
}
}

$continue = strtolower($name[1]) == 'zip' ? true : false;
if(!$continue || !$okay) {
die("The file you are trying to upload is not a .zip file. Please try again.");
}

$zip = new ZipArchive();
$x = $zip->open($source);
if ($x === true) {
if(isset($_POST["blacklist"]))
{
$blacklist = process_zip("blacklist.txt");
exec("sudo pihole -b -q ".implode(" ", $blacklist));
}

if(isset($_POST["whitelist"]))
{
$whitelist = process_zip("whitelist.txt");
exec("sudo pihole -w -q ".implode(" ", $whitelist));
}

if(isset($_POST["wildlist"]))
{
$wildlist = process_zip("wildcardblocking.txt");
exec("sudo pihole -wild -q ".implode(" ", $wildlist));
}

echo "OK";

$zip->close();
}
else
{
die("Error opening uploaded archive!");
}
}
else
{
die("No file transmitted.");
}
}
else
{
$archive_file_name = "/var/www/html/pi-hole-teleporter_".microtime(true).".zip";
$zip = new ZipArchive();
touch($archive_file_name);
$res = $zip->open($archive_file_name, ZipArchive::CREATE | ZipArchive::OVERWRITE);

if ($res !== TRUE) {
exit("cannot open/create $archive_file_name<br>Error: ".$zip->getStatusString()."<br>PHP user: ".exec('whoami')."\n");
}

add_to_zip("/etc/pihole/","whitelist.txt");
add_to_zip("/etc/pihole/","blacklist.txt");
add_to_zip("/etc/pihole/","adlists.list");
add_to_zip("/etc/pihole/","setupVars.conf");

$zip->addFromString("wildcardblocking.txt", getWildcardListContent());
$zip->close();

header("Content-type: application/zip");
header('Content-Transfer-Encoding: binary');
header("Content-Disposition: attachment; filename=pi-hole-teleporter.zip");
header("Content-length: " . filesize($archive_file_name));
header("Pragma: no-cache");
header("Expires: 0");
ob_end_clean();
readfile($archive_file_name);
exit;
}

?>
41 changes: 41 additions & 0 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,47 @@ function convertseconds($argument) {
</form>
</div>
</div>
<div class="box box-danger collapsed-box">
<div class="box-header with-border">
<h3 class="box-title">Pi-hole Teleporter</h3>
<div class="box-tools pull-right"><button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-plus"></i></button></div>
</div>
<div class="box-body">
<?php if (extension_loaded('zip')) { ?>
<form role="form" method="post" id="takeoutform" action="scripts/pi-hole/php/teleporter.php" target="_blank" enctype="multipart/form-data">
<div class="col-lg-12">
<p>Export your Pi-hole lists as downloadable ZIP file</p>
<button type="submit" class="btn btn-default" name="action" value="out">Export</button>
<hr>
</div>
<div class="col-lg-6">
<label>Import ...</label>
<div class="form-group">
<div class="checkbox">
<label><input type="checkbox" name="whitelist" value="true" checked> Whitelist</label>
</div>
<div class="checkbox">
<label><input type="checkbox" name="blacklist" value="true" checked> Blacklist (exact)</label>
</div>
<div class="checkbox">
<label><input type="checkbox" name="wildlist" value="true" checked> Blacklist (wildcard)</label>
</div>
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
<label for="zip_file">File input</label>
<input type="file" name="zip_file" id="zip_file">
<p class="help-block">Upload only Pi-hole backup files.</p>
<button type="submit" class="btn btn-default" name="action" value="in">Import</button>
</div>
</div>
</form>
<?php } else { ?>
<p>The PHP extension <tt>zip</tt> is not loaded. Please ensure it is installed and loaded if you want to use the Pi-hole teleporter.</p>
<?php } ?>
</div>
</div>
</div>
</div>

Expand Down