Support specifying the export filename by passing the export_root

This commit is contained in:
Isaac Connor 2021-08-05 13:28:13 -04:00
parent 91903d2822
commit bed41ca44b
3 changed files with 25 additions and 17 deletions

View File

@ -86,7 +86,9 @@ if ( canView('Events') ) {
$exportVideo, $exportVideo,
$exportMisc, $exportMisc,
$exportFormat, $exportFormat,
$exportCompress $exportCompress,
false, # structure
(isset($_REQUEST['exportFile'])?$_REQUEST['exportFile']:''),
) ) ) )
ajaxResponse(array('exportFile'=>$exportFile)); ajaxResponse(array('exportFile'=>$exportFile));
else else

View File

@ -869,7 +869,8 @@ function exportEvents(
$exportMisc, $exportMisc,
$exportFormat, $exportFormat,
$exportCompressed, $exportCompressed,
$exportStructure = false $exportStructure = false,
$export_root = 'zmExport'
) { ) {
if ( !canView('Events') ) { if ( !canView('Events') ) {
@ -890,7 +891,7 @@ function exportEvents(
ZM\Fatal('Can\'t create exports dir at \''.ZM_DIR_EXPORTS.'\''); ZM\Fatal('Can\'t create exports dir at \''.ZM_DIR_EXPORTS.'\'');
} }
chmod(ZM_DIR_EXPORTS, 0700); chmod(ZM_DIR_EXPORTS, 0700);
$export_dir = ZM_DIR_EXPORTS.'/zmExport_'.$connkey; $export_dir = ZM_DIR_EXPORTS.'/'.$export_root.($connkey?'_'.$connkey:'');
# Ensure that we are going to be able to do this. # Ensure that we are going to be able to do this.
if ( ! ( mkdir($export_dir) or file_exists($export_dir) ) ) { if ( ! ( mkdir($export_dir) or file_exists($export_dir) ) ) {
@ -904,7 +905,6 @@ function exportEvents(
return; return;
} }
$export_root = 'zmExport';
$export_listFile = 'zmFileList.txt'; $export_listFile = 'zmFileList.txt';
$exportFileList = array(); $exportFileList = array();
$html_eventMaster = ''; $html_eventMaster = '';
@ -930,7 +930,7 @@ function exportEvents(
} # end foreach event } # end foreach event
if ( !symlink(ZM_PATH_WEB.'/'.ZM_SKIN_PATH.'/js/jquery.min.js', $export_dir.'/jquery.min.js') ) if ( !symlink(ZM_PATH_WEB.'/'.ZM_SKIN_PATH.'/js/jquery.min.js', $export_dir.'/jquery.min.js') )
ZM\Error('Failed linking jquery.min.js'); ZM\Error('Failed linking '.ZM_PATH_WEB.'/'.ZM_SKIN_PATH.'/js/jquery.min.js to '.$export_dir.'/jquery.min.js');
//if ( !symlink(ZM_PATH_WEB.'/'.ZM_SKIN_PATH.'/js/video.js', $export_dir.'/video.js') ) //if ( !symlink(ZM_PATH_WEB.'/'.ZM_SKIN_PATH.'/js/video.js', $export_dir.'/video.js') )
//Error("Failed linking video.js"); //Error("Failed linking video.js");
@ -947,19 +947,21 @@ function exportEvents(
$listFile = $export_dir.'/'.$export_listFile; $listFile = $export_dir.'/'.$export_listFile;
if ( !($fp = fopen($listFile, 'w')) ) { if ( !($fp = fopen($listFile, 'w')) ) {
ZM\Fatal("Can't open event export list file '$listFile'"); ZM\Error("Can't open event export list file '$listFile'");
return false;
} }
foreach ( $exportFileList as $exportFile ) { foreach ( $exportFileList as $exportFile ) {
$exportFile = 'zmExport'.$connkey.'/'.$exportFile; $exportFile = $export_root.$connkey.'/'.$exportFile;
fwrite($fp, "$exportFile\n"); fwrite($fp, $exportFile.PHP_EOL);
} }
fwrite($fp, "$listFile\n"); fwrite($fp, $listFile.PHP_EOL);
fclose($fp); fclose($fp);
chdir(ZM_DIR_EXPORTS); chdir(ZM_DIR_EXPORTS);
$archive = ''; $archive = '';
if ( $exportFormat == 'tar' ) { if ( $exportFormat == 'tar' ) {
$archive = ZM_DIR_EXPORTS.'/'.$export_root.($connkey?'_'.$connkey:'').'.tar';
$archive = $export_root.($connkey?'_'.$connkey:'').'.tar';
$version = shell_exec('tar -v'); $version = shell_exec('tar -v');
$command = 'tar --create --dereference'; $command = 'tar --create --dereference';
@ -975,16 +977,18 @@ function exportEvents(
$command .= ' --xform=\'s#^.+/##x\''; $command .= ' --xform=\'s#^.+/##x\'';
} }
} }
$command .= ' --file='.escapeshellarg($archive); $archive_path = ZM_DIR_EXPORTS.'/'.$archive;
$command .= ' --file='.escapeshellarg($archive_path);
} elseif ( $exportFormat == 'zip' ) { } elseif ( $exportFormat == 'zip' ) {
$archive = ZM_DIR_EXPORTS.'/'.$export_root.($connkey?'_'.$connkey:'').'.zip'; $archive = $export_root.($connkey?'_'.$connkey:'').'.zip';
$archive_path = ZM_DIR_EXPORTS.'/'.$archive;
$command = 'zip '; $command = 'zip ';
$command .= ($exportStructure == 'flat' ? ' -j ' : ' -r ' ).escapeshellarg($archive); $command .= ($exportStructure == 'flat' ? ' -j ' : ' -r ' ).escapeshellarg($archive_path);
$command .= $exportCompressed ? ' -9' : ' -0'; $command .= $exportCompressed ? ' -9' : ' -0';
} // if $exportFormat } // if $exportFormat
@unlink($archive); @unlink($archive_path);
$command .= ' zmExport_' . $connkey.'/'; $command .= ' '.$export_root.($connkey?'_'.$connkey:'').'/';
exec($command, $output, $status); exec($command, $output, $status);
if ( $status ) { if ( $status ) {
ZM\Error("Command '$command' returned with status $status"); ZM\Error("Command '$command' returned with status $status");
@ -999,5 +1003,5 @@ function exportEvents(
unlink($monitorPath.'/'.$html_eventMaster); unlink($monitorPath.'/'.$html_eventMaster);
} }
return '?view=archive&type='.$exportFormat.'&connkey='.$connkey; return '?view=archive&type='.$exportFormat.'&connkey='.$connkey.'&file='.$archive;
} // end function exportEvents } // end function exportEvents

View File

@ -53,7 +53,9 @@ if ( !$mimetype ) {
} }
$connkey = isset($_REQUEST['connkey'])?$_REQUEST['connkey']:''; $connkey = isset($_REQUEST['connkey'])?$_REQUEST['connkey']:'';
$filename = "zmExport_$connkey.$file_ext"; $filename = isset($_REQUEST['file'])?$_REQUEST['file']:"zmExport_$connkey.$file_ext";
$filename = str_replace('/', '', $filename); # protect system files. must be a filename, not a path
$filename_path = ZM_DIR_EXPORTS.'/'.$filename; $filename_path = ZM_DIR_EXPORTS.'/'.$filename;
ZM\Debug("downloading archive from $filename_path"); ZM\Debug("downloading archive from $filename_path");
if ( is_readable($filename_path) ) { if ( is_readable($filename_path) ) {