<?php
/*
* ***************************************************************************************************
*
* File name: downloaddump.php
*
* Copyright © 2017 Alessandro Quintiliani
*
* This file is part of MultiDump package.
*
* MultiDump is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MultiDump is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MultiDump package. If not, see <http://www.gnu.org/licenses/>.
*
* ***************************************************************************************************
*/
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING & ~E_STRICT);
include_once ("Class.MultiDump.php");
$log = new LogDeltaTime("log", "logpostvars.txt", 1);
$oddmp = new MultiDump ();
if(isset($_POST) && $_POST)
{
$typeCompression = $_POST[$oddmp->getPostVarCompressionType()];
$allTablesToArchive = unserialize(json_decode(urldecode($_POST[$oddmp->getPostVarAllTablesToArchive()])));
$dbnametoarchive = $_POST[$oddmp->getPostVarDbArchived()];
$log->wlog("compression type requested: $typeCompression");
$log->wlog("total number tables received via POST: " . count($allTablesToArchive));
if( $typeCompression && $allTablesToArchive && $dbnametoarchive)
{
### download all database tables dumped
$log->wlog("download all database tables requested");
$log->wlog("database selected for compression: $dbnametoarchive");
$log->wlog("start list tables db $dbnametoarchive to archive:");
foreach($allTablesToArchive as $tableToArchive)
{
$log->wlog("table name candidated to archive: $tableToArchive");
}
$log->wlog("end list tables db $dbnametoarchive to archive");
$typeDbToDump = $_POST[$oddmp->getPostVarTypeDb()];
$oddmp->setDumpParameters ();
$pobjref = $oddmp->getDumpParamsRef ();
$dirfiledumped = $pobjref->getParamValue('folder_dump_files');
$log->wlog("directory with all dump tables: $dirfiledumped");
//$parameter_format_dump_file = 'dump_'.$typeDbToDump.'_output_file_format';
$parameter_format_dump_file = $oddmp->getDumpOutputFileFormat();
$log->wlog("parameter_format_dump_file : $parameter_format_dump_file");
$list_files_db_tables = array_filter($allTablesToArchive,
function($var) use ($dbnametoarchive)
{
return preg_match("/\b^$dbnametoarchive\\.\b/i", $var);
});
$archivedDbFileName = $dbnametoarchive . "_" . date('Ymd-His');
if($typeCompression == "zip")
{
$compressedDbFile = $archivedDbFileName . ".zip";
$log->wlog("start creation zip archive with name $compressedDbFile");
$zip = new ZipArchive();
$zip->open($dirfiledumped . DIRECTORY_SEPARATOR . $compressedDbFile, ZipArchive::CREATE);
$zip->addEmptyDir($archivedDbFileName);
set_time_limit($oddmp->getTimeoutLimit());
foreach($list_files_db_tables as $file_table_dumped)
{
list($dbname,$tableNameDumped) = explode(".", $file_table_dumped, 2);
$list_replacements = array($dbname,$tableNameDumped);
$format_dump_file_to_add = $pobjref->parseParamPlaceHoldersString($parameter_format_dump_file,$list_replacements);
$iterator = new RecursiveDirectoryIterator($dirfiledumped);
$regexdumpfile = new RegexIterator($iterator, "/".$format_dump_file_to_add."/", RecursiveRegexIterator::GET_MATCH);
$list_dump_file_matched = iterator_to_array($regexdumpfile, false);
$dump_file_to_add = $list_dump_file_matched[0][0];
$full_path_file_to_add = $dirfiledumped . DIRECTORY_SEPARATOR . $dump_file_to_add;
$full_path_file_added_to_zip = $archivedDbFileName . DIRECTORY_SEPARATOR . $dump_file_to_add;
$log->wlog("file $full_path_file_to_add added to zip $compressedDbFile (added: $full_path_file_added_to_zip)");
$zip->addFile($full_path_file_to_add, $full_path_file_added_to_zip);
}
$zip->close();
$log->wlog("end creation zip archive with final name $archivedDbFileName");
}
elseif($typeCompression == "tgz")
{
$log->wlog("start creation tar archive with final name $archivedDbFileName.tar.gz");
$tarName = $archivedDbFileName . ".tar";
try
{
$pathToTarName = $dirfiledumped . DIRECTORY_SEPARATOR . $tarName;
$atgz = new PharData($pathToTarName);
$atgz->addEmptyDir($archivedDbFileName);
$log->wlog("added empty directory $archivedDbFileName to $tarName before compression");
foreach($list_files_db_tables as $file_table_dumped)
{
list($dbname,$tableNameDumped) = explode(".", $file_table_dumped, 2);
$list_replacements = array($dbname,$tableNameDumped);
$format_dump_file_to_add = $pobjref->parseParamPlaceHoldersString($parameter_format_dump_file,$list_replacements);
$iterator = new RecursiveDirectoryIterator($dirfiledumped);
$regexdumpfile = new RegexIterator($iterator, "/".$format_dump_file_to_add."/", RecursiveRegexIterator::GET_MATCH);
$list_dump_file_matched = iterator_to_array($regexdumpfile, false);
$dump_file_to_add = $list_dump_file_matched[0][0];
$full_path_file_to_add = $dirfiledumped . DIRECTORY_SEPARATOR . $dump_file_to_add;
$full_path_file_added_to_tgz = $archivedDbFileName . DIRECTORY_SEPARATOR . $dump_file_to_add;
$log->wlog("file $full_path_file_to_add added to tgz $compressedDbFile (added: $full_path_file_added_to_tgz)");
$atgz->addFile($full_path_file_to_add, $full_path_file_added_to_tgz);
}
set_time_limit($oddmp->getTimeoutLimit());
$atgz->compress(Phar::GZ);
$log->wlog("archive file $tarName.tar compressed to $tarName.tar.gz");
}
catch(Exception $e){
echo "Exception : " . $e;
}
$log->wlog("end creation tar archive with final name $archivedDbFileName.tar.gz");
}
ignore_user_abort(true);
# headers disabling cache browser
header("Expires: on, 01 Jan 1970 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
# headers download file compressed
header('Content-Description: File Transfer');
//header('Content - Type: application / octet - stream');
header("Content-Transfer-Encoding: binary");
if($typeCompression == "zip")
{
header('Content-Type: application/zip');
$compressedDbFile = $archivedDbFileName . ".zip";
}
elseif($typeCompression == "tgz")
{
header('Content-type: application');
$compressedDbFile = $archivedDbFileName . ".tar.gz";
}
$full_path_to_compressed_file = $dirfiledumped . DIRECTORY_SEPARATOR . $compressedDbFile;
$log->wlog("downloading file $full_path_to_compressed_file");
header('Content-Disposition: attachment; filename="'.basename($compressedDbFile).'"');
header('Content-Length: ' . filesize($full_path_to_compressed_file));
readfile($full_path_to_compressed_file);
unlink($full_path_to_compressed_file);
if($typeCompression == "tgz")
{
unlink($pathToTarName);
}
}
}
elseif( isset($_GET) && $_GET )
{
### download single dump file
$filedumped = urldecode($_GET[$oddmp->getPostVarDumpFile()]);
$typeCompression = $_GET[$oddmp->getPostVarCompressionType()];
$log->wlog("compression type requested: $typeCompression");
$log->wlog("downloading file $filedumped with one table dumped");
if(file_exists($filedumped) && $typeCompression)
{
$log->wlog("file $filedumped exists");
$dirfiledumped = pathinfo($filedumped, PATHINFO_DIRNAME);
$filename = pathinfo($filedumped, PATHINFO_BASENAME);
$extension_filename = pathinfo($filedumped, PATHINFO_EXTENSION);
# headers disabling cache browser
header("Expires: on, 01 Jan 1970 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
$zip = new ZipArchive();
$tmp_file_dump = tempnam($dirfiledumped,'tmp_');
$zip->open($tmp_file_dump, ZipArchive::CREATE);
//$download_file_dump = file_get_contents($filedumped);
$log->wlog("path to the file to add in the zip: $filedumped");
$log->wlog("file $filedumped compressed to zip format");
set_time_limit($oddmp->getTimeoutLimit());
$zip->addFile($filedumped, basename($filedumped));
$zip->close();
# header download file compressed
header('Content-Description: File Transfer');
if($typeCompression == "zip")
{
header('Content-Type: application/zip');
$filerenamed = str_replace("sql","zip",$filename);
}
elseif($typeCompression == "gzip")
{
header('Content-type: application/x-gzip');
$filerenamed = str_replace("sql","gzip",$filename);
}
$filedumpcompressed = $dirfiledumped . DIRECTORY_SEPARATOR . $filerenamed;
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: binary");
header('Content-Disposition: attachment; filename="'.$filerenamed.'"');
header('Content-Length: ' . filesize($tmp_file_dump));
readfile($tmp_file_dump);
unlink($tmp_file_dump);
}
}
unset($oddmp);
$log->end();
unset($log);
|