mirror of
https://github.com/pi-hole/web.git
synced 2026-04-24 18:59:48 +01:00
Added box to Settings page + added reading routines to takeout.php
Conflicts: scripts/pi-hole/php/takeout.php
This commit is contained in:
@@ -1,91 +1,100 @@
|
||||
<?php
|
||||
|
||||
function message($code)
|
||||
function process_zip($name)
|
||||
{
|
||||
switch ($code)
|
||||
{
|
||||
case 0:
|
||||
return 'No error';
|
||||
|
||||
case 1:
|
||||
return 'Multi-disk zip archives not supported';
|
||||
|
||||
case 2:
|
||||
return 'Renaming temporary file failed';
|
||||
|
||||
case 3:
|
||||
return 'Closing zip archive failed';
|
||||
|
||||
case 4:
|
||||
return 'Seek error';
|
||||
|
||||
case 5:
|
||||
return 'Read error';
|
||||
|
||||
case 6:
|
||||
return 'Write error';
|
||||
|
||||
case 7:
|
||||
return 'CRC error';
|
||||
|
||||
case 8:
|
||||
return 'Containing zip archive was closed';
|
||||
|
||||
case 9:
|
||||
return 'No such file';
|
||||
|
||||
case 10:
|
||||
return 'File already exists';
|
||||
|
||||
case 11:
|
||||
return 'Can\'t open file';
|
||||
|
||||
case 12:
|
||||
return 'Failure to create temporary file';
|
||||
|
||||
case 13:
|
||||
return 'Zlib error';
|
||||
|
||||
case 14:
|
||||
return 'Malloc failure';
|
||||
|
||||
case 15:
|
||||
return 'Entry has been changed';
|
||||
|
||||
case 16:
|
||||
return 'Compression method not supported';
|
||||
|
||||
case 17:
|
||||
return 'Premature EOF';
|
||||
|
||||
case 18:
|
||||
return 'Invalid argument';
|
||||
|
||||
case 19:
|
||||
return 'Not a zip archive';
|
||||
|
||||
case 20:
|
||||
return 'Internal error';
|
||||
|
||||
case 21:
|
||||
return 'Zip archive inconsistent';
|
||||
|
||||
case 22:
|
||||
return 'Can\'t remove file';
|
||||
|
||||
case 23:
|
||||
return 'Entry has been deleted';
|
||||
|
||||
default:
|
||||
return 'An unknown error has occurred('.intval($code).')';
|
||||
}
|
||||
global $zip;
|
||||
$fp = $zip->getStream($name);
|
||||
if(!$fp)
|
||||
{
|
||||
echo "$name not found in provided ZIP file, skipping...";
|
||||
return;
|
||||
}
|
||||
while (!feof($fp)) {
|
||||
$contents .= fread($fp, 2);
|
||||
}
|
||||
fclose($fp);
|
||||
return $contents;
|
||||
}
|
||||
|
||||
$filename = "/tmp/pi-hole-takeout.zip";
|
||||
$zip = zip_open($filename);
|
||||
function add_to_zip($path,$name)
|
||||
{
|
||||
global $zip;
|
||||
if(file_exists($path.$name))
|
||||
$zip->addFile($path.$name,$name);
|
||||
else
|
||||
// Add empty file with thios filename
|
||||
$zip->addFromString($name, "");
|
||||
}
|
||||
|
||||
if (!is_resource($zip)) {
|
||||
exit("cannot open/create $filename<br>Error message: ".message($zip)."\n");
|
||||
if($_POST["action"] == "in")
|
||||
{
|
||||
print_r($_POST);
|
||||
print_r($_FILES);
|
||||
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');
|
||||
foreach($accepted_types as $mime_type) {
|
||||
if($mime_type == $type) {
|
||||
$okay = true;
|
||||
echo "Filetype $mime_type accepted<br>";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$continue = strtolower($name[1]) == 'zip' ? true : false;
|
||||
if(!$continue) {
|
||||
$message = "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) {
|
||||
echo "File opened successfully<br>";
|
||||
echo process_zip("blacklist.txt");
|
||||
echo process_zip("whitelist.txt");
|
||||
$zip->close();
|
||||
}
|
||||
else
|
||||
{
|
||||
die("Error opening uploaded archive!");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
die("No file transmitted.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$archive_file_name = "/var/www/html/pi-hole-takeout_".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.default");
|
||||
add_to_zip("/etc/pihole/","adlists.list");
|
||||
// $zip->addFile("/etc/pihole/setupVars.conf");
|
||||
$zip->close();
|
||||
|
||||
header("Content-type: application/zip");
|
||||
header('Content-Transfer-Encoding: binary');
|
||||
header("Content-Disposition: attachment; filename=pi-hole-takeout.zip");
|
||||
header("Content-length: " . filesize($archive_file_name));
|
||||
header("Pragma: no-cache");
|
||||
header("Expires: 0");
|
||||
ob_end_clean();
|
||||
readfile($archive_file_name);
|
||||
exit;
|
||||
}
|
||||
|
||||
$zip->addFile("/etc/pihole/whitelist.txt");
|
||||
|
||||
Reference in New Issue
Block a user