Commit 5016916e authored by Carsten  Rose's avatar Carsten Rose
Browse files

Fixes #7138

parent c52435d3
Pipeline #1077 passed with stage
in 1 minute and 58 seconds
...@@ -212,8 +212,8 @@ const ERROR_STORE_VALUE_ALREADY_CODPIED = 1200; ...@@ -212,8 +212,8 @@ const ERROR_STORE_VALUE_ALREADY_CODPIED = 1200;
const ERROR_STORE_KEY_EXIST = 1201; const ERROR_STORE_KEY_EXIST = 1201;
// I/O Error // I/O Error
const ERROR_IO_READ_FILE = 1300; const ERROR_IO_COPY = 1300;
const ERROR_IO_COPY = 1301;
const ERROR_IO_RMDIR = 1302; const ERROR_IO_RMDIR = 1302;
const ERROR_IO_WRITE = 1303; const ERROR_IO_WRITE = 1303;
const ERROR_IO_OPEN = 1304; const ERROR_IO_OPEN = 1304;
...@@ -227,8 +227,11 @@ const ERROR_IO_CREATE_FILE = 1311; ...@@ -227,8 +227,11 @@ const ERROR_IO_CREATE_FILE = 1311;
const ERROR_IO_COPY_FILE = 1312; const ERROR_IO_COPY_FILE = 1312;
const ERROR_IO_FILE_NOT_FOUND = 1313; const ERROR_IO_FILE_NOT_FOUND = 1313;
const ERROR_IO_CHMOD = 1314; const ERROR_IO_CHMOD = 1314;
const ERROR_PDF2SVG = 1315; const ERROR_IO_READ_FILE = 1315;
const ERROR_PDF2JPEG = 1316; const ERROR_IO_WRITE_FILE = 1316;
const ERROR_PDF2SVG = 1320;
const ERROR_PDF2JPEG = 1321;
//Report //Report
const ERROR_UNKNOWN_LINK_QUALIFIER = 1400; const ERROR_UNKNOWN_LINK_QUALIFIER = 1400;
...@@ -1425,8 +1428,9 @@ const DOWNLOAD_OUTPUT_FORMAT_RAW = 'raw'; ...@@ -1425,8 +1428,9 @@ const DOWNLOAD_OUTPUT_FORMAT_RAW = 'raw';
const DOWNLOAD_OUTPUT_FORMAT_JSON = 'json'; const DOWNLOAD_OUTPUT_FORMAT_JSON = 'json';
const JSON_TEXT = 'text'; const JSON_TEXT = 'text';
const OUTPUT_MODE_DIRECT = 'direct'; const OUTPUT_MODE_DIRECT = 'direct'; // Send the file direct via readfile() to the browser
const OUTPUT_MODE_FILE = 'file'; const OUTPUT_MODE_FILE = 'file'; // return a filename
const OUTPUT_MODE_COPY_TO_FILE = 'copyToFile'; // create a local copy return a filename
const EXCEL_WORKSHEET = 'worksheet'; const EXCEL_WORKSHEET = 'worksheet';
const EXCEL_MODE = 'mode'; const EXCEL_MODE = 'mode';
......
...@@ -874,7 +874,7 @@ class Save { ...@@ -874,7 +874,7 @@ class Save {
$fileDestinationSplit = $pathFileName . '.split/split' . $ext; $fileDestinationSplit = $pathFileName . '.split/split' . $ext;
} }
Support::mkDirParent($fileDestinationSplit); HelperFile::mkDirParent($fileDestinationSplit);
// Save CWD // Save CWD
$cwd = getcwd(); $cwd = getcwd();
......
...@@ -589,7 +589,7 @@ class FormAction { ...@@ -589,7 +589,7 @@ class FormAction {
continue; continue;
} }
Support::mkDirParent($rowDest[$key]); HelperFile::mkDirParent($rowDest[$key]);
HelperFile::copy($val, $rowDest[$key]); HelperFile::copy($val, $rowDest[$key]);
} }
} }
......
...@@ -149,7 +149,7 @@ class HelperFile { ...@@ -149,7 +149,7 @@ class HelperFile {
public static function pathInfo($pathFileName) { public static function pathInfo($pathFileName) {
$vars = array(); $vars = array();
$pathParts = pathinfo($pathFileName); $pathParts = path($pathFileName);
$vars[VAR_FILENAME] = $pathFileName; $vars[VAR_FILENAME] = $pathFileName;
if (isset($pathParts['basename'])) { if (isset($pathParts['basename'])) {
...@@ -246,7 +246,7 @@ class HelperFile { ...@@ -246,7 +246,7 @@ class HelperFile {
public static function errorGetLastAsString() { public static function errorGetLastAsString() {
if (NULL === ($errors = error_get_last())) { if (NULL === ($errors = error_get_last())) {
return ''; return 'error_get_last(): no error';
} }
return $errors['type'] . ' - ' . $errors['message']; return $errors['type'] . ' - ' . $errors['message'];
...@@ -298,7 +298,7 @@ class HelperFile { ...@@ -298,7 +298,7 @@ class HelperFile {
if (false === rmdir($tempDir)) { if (false === rmdir($tempDir)) {
$msg = self::errorGetLastAsString() . " - rmdir($tempDir)"; $msg = self::errorGetLastAsString() . " - rmdir($tempDir)";
throw new UserFormException(json_encode([ERROR_MESSAGE_TO_USER => 'unlink failed', ERROR_MESSAGE_SUPPORT => $msg]), ERROR_IO_RMDIR); throw new UserFormException(json_encode([ERROR_MESSAGE_TO_USER => 'rmdir failed', ERROR_MESSAGE_SUPPORT => $msg]), ERROR_IO_RMDIR);
} }
return true; return true;
...@@ -316,7 +316,7 @@ class HelperFile { ...@@ -316,7 +316,7 @@ class HelperFile {
if (false === rename($oldname, $newname)) { if (false === rename($oldname, $newname)) {
$msg = self::errorGetLastAsString() . " - rename($oldname ,$newname)"; $msg = self::errorGetLastAsString() . " - rename($oldname ,$newname)";
throw new UserFormException(json_encode([ERROR_MESSAGE_TO_USER => 'unlink failed', ERROR_MESSAGE_SUPPORT => $msg]), ERROR_IO_RMDIR); throw new UserFormException(json_encode([ERROR_MESSAGE_TO_USER => 'unlink failed', ERROR_MESSAGE_SUPPORT => $msg]), ERROR_IO_RENAME);
} }
return true; return true;
...@@ -332,11 +332,69 @@ class HelperFile { ...@@ -332,11 +332,69 @@ class HelperFile {
*/ */
public static function copy($source, $dest) { public static function copy($source, $dest) {
self::mkDirParent($dest);
if (!is_file($dest)) {
touch($dest);
}
if (false === copy($source, $dest)) { if (false === copy($source, $dest)) {
$msg = self::errorGetLastAsString() . " - copy($source, $dest)";
throw new UserFormException(json_encode([ERROR_MESSAGE_TO_USER => 'unlink failed', ERROR_MESSAGE_SUPPORT => $msg]), ERROR_IO_RMDIR); if (!is_readable($source)) {
throw new UserFormException(json_encode([ERROR_MESSAGE_TO_USER => 'copy failed', ERROR_MESSAGE_SUPPORT => "Can't read file '$source'"]), ERROR_IO_READ_FILE);
}
if (!is_writeable($dest)) {
throw new UserFormException(json_encode([ERROR_MESSAGE_TO_USER => 'copy failed', ERROR_MESSAGE_SUPPORT => "Can't write to file '$dest'"]), ERROR_IO_WRITE_FILE);
}
$msg = self::errorGetLastAsString(); // Often, there is no specific error string.
$msg .= " - copy($source, $dest)";
throw new UserFormException(json_encode([ERROR_MESSAGE_TO_USER => 'copy failed', ERROR_MESSAGE_SUPPORT => $msg]), ERROR_IO_COPY);
} }
return true; return true;
} }
}
\ No newline at end of file /**
* Creates all necessary directories in $pathFileName, but not the last part, the filename. A filename has to be
* specified.
*
* @param string $pathFileName Path with Filename
* @param bool|int $chmodDir false if not explicit set
* @throws UserFormException
*/
public static function mkDirParent($pathFileName, $chmodDir = false) {
$path = "";
// Teile "Directory/File.Extension" auf
$pathParts = pathinfo($pathFileName);
// Zerlege Pfad in einzelne Directories
$arr = explode("/", $pathParts["dirname"]);
// Durchlaufe die einzelnen Dirs und überprüfe ob sie angelegt sind.
// Wenn nicht, lege sie an.
foreach ($arr as $part) {
$path .= $part;
// Check ob der Pfad ein Link ist
if ("link" == @filetype($path)) {
if ("0" == ($path1 = readlink($path))) {
throw new UserFormException("Can't create '$pathFileName': '$path' contains an invalid link.", ERROR_IO_INVALID_LINK);
}
} else {
if (file_exists($path)) {
if ("dir" != filetype($path)) {
throw new UserFormException("Can't create '$pathFileName': There is already a file with the same name as '$path'", ERROR_IO_DIR_EXIST_AS_FILE);
}
} else {
mkdir($path);
HelperFile::chmod($path, $chmodDir);
}
}
$path .= "/";
}
}
}
...@@ -38,7 +38,7 @@ class Logger { ...@@ -38,7 +38,7 @@ class Logger {
if (!$handle = fopen($filename, $mode)) { if (!$handle = fopen($filename, $mode)) {
// If open fails, maybe the directory does not exist. Create it: // If open fails, maybe the directory does not exist. Create it:
Support::mkDirParent($filename); HelperFile::mkDirParent($filename);
// Try to open the file a second time. // Try to open the file a second time.
if (!$handle = fopen($filename, $mode)) { if (!$handle = fopen($filename, $mode)) {
......
...@@ -1234,47 +1234,6 @@ class Support { ...@@ -1234,47 +1234,6 @@ class Support {
return $path . DIRECTORY_SEPARATOR . $file; return $path . DIRECTORY_SEPARATOR . $file;
} }
/**
* Creates all necessary directories in $pathFileName, but not the last part, the filename. A filename has to be
* specified.
*
* @param string $pathFileName Path with Filename
* @param bool|int $chmodDir false if not explicit set
* @throws UserFormException
*/
public static function mkDirParent($pathFileName, $chmodDir = false) {
$path = "";
// Teile "Directory/File.Extension" auf
$pathParts = pathinfo($pathFileName);
// Zerlege Pfad in einzelne Directories
$arr = explode("/", $pathParts["dirname"]);
// Durchlaufe die einzelnen Dirs und überprüfe ob sie angelegt sind.
// Wenn nicht, lege sie an.
foreach ($arr as $part) {
$path .= $part;
// Check ob der Pfad ein Link ist
if ("link" == @filetype($path)) {
if ("0" == ($path1 = readlink($path))) {
throw new UserFormException("Can't create '$pathFileName': '$path' contains an invalid link.", ERROR_IO_INVALID_LINK);
}
} else {
if (file_exists($path)) {
if ("dir" != filetype($path)) {
throw new UserFormException("Can't create '$pathFileName': There is already a file with the same name as '$path'", ERROR_IO_DIR_EXIST_AS_FILE);
}
} else {
mkdir($path);
HelperFile::chmod($path, $chmodDir);
}
}
$path .= "/";
}
}
/** /**
* @param $srcFile * @param $srcFile
* @param $pathFileName * @param $pathFileName
...@@ -1292,7 +1251,7 @@ class Support { ...@@ -1292,7 +1251,7 @@ class Support {
} }
} }
Support::mkDirParent($pathFileName, $chmodDir); HelperFile::mkDirParent($pathFileName, $chmodDir);
// Do not use 'rename' - might cause trouble if src and dest are on different filesystems. // Do not use 'rename' - might cause trouble if src and dest are on different filesystems.
HelperFile::copy($srcFile, $pathFileName); HelperFile::copy($srcFile, $pathFileName);
......
...@@ -110,7 +110,7 @@ class Download { ...@@ -110,7 +110,7 @@ class Download {
// Remove empty entries. Might happen if there was no upload // Remove empty entries. Might happen if there was no upload
$files = OnArray::removeEmptyElementsFromArray($files); $files = OnArray::removeEmptyElementsFromArray($files);
// Check that all files exist and readable // Check that all files exist and are readable
foreach ($files AS $filename) { foreach ($files AS $filename) {
if (!is_readable($filename)) { if (!is_readable($filename)) {
throw new downloadException("Error reading file $filename. Not found or no permission", ERROR_DOWNLOAD_FILE_NOT_READABLE); throw new downloadException("Error reading file $filename. Not found or no permission", ERROR_DOWNLOAD_FILE_NOT_READABLE);
...@@ -121,12 +121,6 @@ class Download { ...@@ -121,12 +121,6 @@ class Download {
return ''; return '';
} }
// Need to create a separate result file, even if it is just a single file (#6929)
$concatFile = HelperFile::tempnam();
if (false === $concatFile) {
throw new DownloadException('Error creating output file.', ERROR_DOWNLOAD_CREATE_NEW_FILE);
}
// Check that all files are of type 'application/pdf' // Check that all files are of type 'application/pdf'
foreach ($files AS $filename) { foreach ($files AS $filename) {
$mimetype = mime_content_type($filename); $mimetype = mime_content_type($filename);
...@@ -135,13 +129,22 @@ class Download { ...@@ -135,13 +129,22 @@ class Download {
} }
} }
$files = OnArray::arrayEscapeshellarg($files); if (count($files) == 1) {
return $files[0];
}
$files = OnArray::arrayEscapeshellarg($files);
$inputFiles = implode(' ', $files); $inputFiles = implode(' ', $files);
if (trim($inputFiles) == '') { if (trim($inputFiles) == '') {
throw new DownloadException('No files to concatenate.', ERROR_DOWNLOAD_NO_FILES); throw new DownloadException('No files to concatenate.', ERROR_DOWNLOAD_NO_FILES);
} }
// Need to create a separate result file, even if it is just a single file (#6929)
$concatFile = HelperFile::tempnam();
if (false === $concatFile) {
throw new DownloadException('Error creating output file.', ERROR_DOWNLOAD_CREATE_NEW_FILE);
}
$cmd = "pdftk $inputFiles cat output $concatFile"; $cmd = "pdftk $inputFiles cat output $concatFile";
if ($this->downloadDebugLog != '') { if ($this->downloadDebugLog != '') {
...@@ -369,8 +372,8 @@ class Download { ...@@ -369,8 +372,8 @@ class Download {
* *
* @param array $vars [ DOWNLOAD_EXPORT_FILENAME, DOWNLOAD_MODE, SIP_DOWNLOAD_PARAMETER ] * @param array $vars [ DOWNLOAD_EXPORT_FILENAME, DOWNLOAD_MODE, SIP_DOWNLOAD_PARAMETER ]
* *
* @param string $outputMode OUTPUT_MODE_DIRECT | OUTPUT_MODE_FILE * @param string $outputMode OUTPUT_MODE_DIRECT | OUTPUT_MODE_FILE | OUTPUT_MODE_COPY_TO_FILE
* @return string Filename of the generated file. The filename only points to a real existing filename with $outputMode=OUTPUT_MODE_FILE * @return string Filename of the generated file. The filename only points to a real existing filename with $outputMode=OUTPUT_MODE_FILE | OUTPUT_MODE_COPY_TO_FILE
* @throws CodeException * @throws CodeException
* @throws DbException * @throws DbException
* @throws DownloadException * @throws DownloadException
...@@ -470,6 +473,11 @@ class Download { ...@@ -470,6 +473,11 @@ class Download {
case OUTPUT_MODE_FILE: case OUTPUT_MODE_FILE:
break; break;
case OUTPUT_MODE_COPY_TO_FILE:
HelperFile::copy($filename, $vars[DOWNLOAD_EXPORT_FILENAME]);
HelperFile::cleanTempFiles([$filename]);
break;
case OUTPUT_MODE_DIRECT: case OUTPUT_MODE_DIRECT:
$this->outputFile($filename, $vars[DOWNLOAD_EXPORT_FILENAME]); $this->outputFile($filename, $vars[DOWNLOAD_EXPORT_FILENAME]);
HelperFile::cleanTempFiles([$filename]); HelperFile::cleanTempFiles([$filename]);
......
...@@ -391,6 +391,7 @@ class Report { ...@@ -391,6 +391,7 @@ class Report {
usort($ary, $sortFn); usort($ary, $sortFn);
error_reporting($errorSet); error_reporting($errorSet);
error_clear_last();
} }
} }
...@@ -810,9 +811,9 @@ class Report { ...@@ -810,9 +811,9 @@ class Report {
} elseif ($pathFileName !== $sanitizedFileName) { } elseif ($pathFileName !== $sanitizedFileName) {
throw new UserReportException("The provided filename '$pathFileName' does not meet sanitize criteria. Use '$sanitizedFileName' instead.", ERROR_INVALID_SAVE_PDF_FILENAME); throw new UserReportException("The provided filename '$pathFileName' does not meet sanitize criteria. Use '$sanitizedFileName' instead.", ERROR_INVALID_SAVE_PDF_FILENAME);
} else { } else {
$vars[DOWNLOAD_EXPORT_FILENAME] = $sanitizedFileName;
$download = new Download(); $download = new Download();
$file = $download->process($vars, OUTPUT_MODE_FILE); $download->process($vars, OUTPUT_MODE_COPY_TO_FILE);
Support::moveFile($file, $pathFileName, true);
} }
break; break;
......
...@@ -169,7 +169,7 @@ class Thumbnail { ...@@ -169,7 +169,7 @@ class Thumbnail {
// Indicates a running thumbnail rendering process. // Indicates a running thumbnail rendering process.
if (false === touch($pathFilenameThumbnail)) { if (false === touch($pathFilenameThumbnail)) {
// Be sure that the target directory exist // Be sure that the target directory exist
Support::mkDirParent($pathFilenameThumbnail); HelperFile::mkDirParent($pathFilenameThumbnail);
if (false === touch($pathFilenameThumbnail)) { if (false === touch($pathFilenameThumbnail)) {
throw new UserReportException('Could not create file: ' . OnString::strrstr($pathFilenameSource, '/'), ERROR_IO_CREATE_FILE); throw new UserReportException('Could not create file: ' . OnString::strrstr($pathFilenameSource, '/'), ERROR_IO_CREATE_FILE);
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment