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;
const ERROR_STORE_KEY_EXIST = 1201;
// I/O Error
const ERROR_IO_READ_FILE = 1300;
const ERROR_IO_COPY = 1301;
const ERROR_IO_COPY = 1300;
const ERROR_IO_RMDIR = 1302;
const ERROR_IO_WRITE = 1303;
const ERROR_IO_OPEN = 1304;
......@@ -227,8 +227,11 @@ const ERROR_IO_CREATE_FILE = 1311;
const ERROR_IO_COPY_FILE = 1312;
const ERROR_IO_FILE_NOT_FOUND = 1313;
const ERROR_IO_CHMOD = 1314;
const ERROR_PDF2SVG = 1315;
const ERROR_PDF2JPEG = 1316;
const ERROR_IO_READ_FILE = 1315;
const ERROR_IO_WRITE_FILE = 1316;
const ERROR_PDF2SVG = 1320;
const ERROR_PDF2JPEG = 1321;
//Report
const ERROR_UNKNOWN_LINK_QUALIFIER = 1400;
......@@ -1425,8 +1428,9 @@ const DOWNLOAD_OUTPUT_FORMAT_RAW = 'raw';
const DOWNLOAD_OUTPUT_FORMAT_JSON = 'json';
const JSON_TEXT = 'text';
const OUTPUT_MODE_DIRECT = 'direct';
const OUTPUT_MODE_FILE = 'file';
const OUTPUT_MODE_DIRECT = 'direct'; // Send the file direct via readfile() to the browser
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_MODE = 'mode';
......
......@@ -874,7 +874,7 @@ class Save {
$fileDestinationSplit = $pathFileName . '.split/split' . $ext;
}
Support::mkDirParent($fileDestinationSplit);
HelperFile::mkDirParent($fileDestinationSplit);
// Save CWD
$cwd = getcwd();
......
......@@ -589,7 +589,7 @@ class FormAction {
continue;
}
Support::mkDirParent($rowDest[$key]);
HelperFile::mkDirParent($rowDest[$key]);
HelperFile::copy($val, $rowDest[$key]);
}
}
......
......@@ -149,7 +149,7 @@ class HelperFile {
public static function pathInfo($pathFileName) {
$vars = array();
$pathParts = pathinfo($pathFileName);
$pathParts = path($pathFileName);
$vars[VAR_FILENAME] = $pathFileName;
if (isset($pathParts['basename'])) {
......@@ -246,7 +246,7 @@ class HelperFile {
public static function errorGetLastAsString() {
if (NULL === ($errors = error_get_last())) {
return '';
return 'error_get_last(): no error';
}
return $errors['type'] . ' - ' . $errors['message'];
......@@ -298,7 +298,7 @@ class HelperFile {
if (false === 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;
......@@ -316,7 +316,7 @@ class HelperFile {
if (false === 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;
......@@ -332,11 +332,69 @@ class HelperFile {
*/
public static function copy($source, $dest) {
self::mkDirParent($dest);
if (!is_file($dest)) {
touch($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;
}
}
\ 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 {
if (!$handle = fopen($filename, $mode)) {
// 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.
if (!$handle = fopen($filename, $mode)) {
......
......@@ -1234,47 +1234,6 @@ class Support {
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 $pathFileName
......@@ -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.
HelperFile::copy($srcFile, $pathFileName);
......
......@@ -110,7 +110,7 @@ class Download {
// Remove empty entries. Might happen if there was no upload
$files = OnArray::removeEmptyElementsFromArray($files);
// Check that all files exist and readable
// Check that all files exist and are readable
foreach ($files AS $filename) {
if (!is_readable($filename)) {
throw new downloadException("Error reading file $filename. Not found or no permission", ERROR_DOWNLOAD_FILE_NOT_READABLE);
......@@ -121,12 +121,6 @@ class Download {
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'
foreach ($files AS $filename) {
$mimetype = mime_content_type($filename);
......@@ -135,13 +129,22 @@ class Download {
}
}
$files = OnArray::arrayEscapeshellarg($files);
if (count($files) == 1) {
return $files[0];
}
$files = OnArray::arrayEscapeshellarg($files);
$inputFiles = implode(' ', $files);
if (trim($inputFiles) == '') {
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";
if ($this->downloadDebugLog != '') {
......@@ -369,8 +372,8 @@ class Download {
*
* @param array $vars [ DOWNLOAD_EXPORT_FILENAME, DOWNLOAD_MODE, SIP_DOWNLOAD_PARAMETER ]
*
* @param string $outputMode OUTPUT_MODE_DIRECT | OUTPUT_MODE_FILE
* @return string Filename of the generated file. The filename only points to a real existing filename with $outputMode=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 | OUTPUT_MODE_COPY_TO_FILE
* @throws CodeException
* @throws DbException
* @throws DownloadException
......@@ -470,6 +473,11 @@ class Download {
case OUTPUT_MODE_FILE:
break;
case OUTPUT_MODE_COPY_TO_FILE:
HelperFile::copy($filename, $vars[DOWNLOAD_EXPORT_FILENAME]);
HelperFile::cleanTempFiles([$filename]);
break;
case OUTPUT_MODE_DIRECT:
$this->outputFile($filename, $vars[DOWNLOAD_EXPORT_FILENAME]);
HelperFile::cleanTempFiles([$filename]);
......
......@@ -391,6 +391,7 @@ class Report {
usort($ary, $sortFn);
error_reporting($errorSet);
error_clear_last();
}
}
......@@ -810,9 +811,9 @@ class Report {
} elseif ($pathFileName !== $sanitizedFileName) {
throw new UserReportException("The provided filename '$pathFileName' does not meet sanitize criteria. Use '$sanitizedFileName' instead.", ERROR_INVALID_SAVE_PDF_FILENAME);
} else {
$vars[DOWNLOAD_EXPORT_FILENAME] = $sanitizedFileName;
$download = new Download();
$file = $download->process($vars, OUTPUT_MODE_FILE);
Support::moveFile($file, $pathFileName, true);
$download->process($vars, OUTPUT_MODE_COPY_TO_FILE);
}
break;
......
......@@ -169,7 +169,7 @@ class Thumbnail {
// Indicates a running thumbnail rendering process.
if (false === touch($pathFilenameThumbnail)) {
// Be sure that the target directory exist
Support::mkDirParent($pathFilenameThumbnail);
HelperFile::mkDirParent($pathFilenameThumbnail);
if (false === touch($pathFilenameThumbnail)) {
throw new UserReportException('Could not create file: ' . OnString::strrstr($pathFilenameSource, '/'), ERROR_IO_CREATE_FILE);
}
......
Markdown is supported
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