Commit 25ed1e23 authored by Carsten  Rose's avatar Carsten Rose
Browse files

Bug 7539: Copy to clipboard not reliable. This commit fixes problems with...

Bug 7539: Copy to clipboard not reliable. This commit fixes problems with 'copy file content' to clipboard. Refs #7539
parent 9dbfb722
Pipeline #1254 passed with stage
in 1 minute and 37 seconds
......@@ -183,7 +183,7 @@ const ERROR_MISSING_OPEN_DELIMITER = 1060;
const ERROR_MISSING_CLOSE_DELIMITER = 1061;
const ERROR_EXPECTED_ARRAY = 1062;
const ERROR_REPORT_FAILED_ACTION = 1063;
const ERROR_MISSING_CONTENT = 1064;
const ERROR_MISSING_TABLE_NAME = 1065;
const ERROR_MISSING_TABLE = 1066;
const ERROR_RECORD_NOT_FOUND = 1067;
......@@ -274,6 +274,7 @@ const ERROR_DOWNLOAD_UNEXPECTED_MIME_TYPE = 1703;
const ERROR_DOWNLOAD_UNEXPECTED_NUMBER_OF_SOURCES = 1704;
const ERROR_DOWNLOAD_FILE_NOT_READABLE = 1705;
const ERROR_DOWNLOAD_FOPEN_BLOCKED = 1706;
const ERROR_DOWNLOAD_JSON_CONVERT = 1707;
// Excel
const ERROR_EXCEL_POSITION_ARGUMENT_EMPTY = 1800;
......
......@@ -113,7 +113,7 @@ class Download {
// 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);
throw new DownloadException("Error reading file $filename. Not found or no permission", ERROR_DOWNLOAD_FILE_NOT_READABLE);
}
}
......@@ -182,24 +182,41 @@ class Download {
*
* @param $file
* @param $outputFilename
* @throws downloadException
*/
private function outputFile($file, $outputFilename) {
$length = filesize($file);
$json = '';
$flagJson = ($this->getOutputFormat() === DOWNLOAD_OUTPUT_FORMAT_JSON);
$outputFilename = $this->targetFilenameExtension($file, $outputFilename, $mimetype);
$outputFilename = $this->targetFilenameExtension($file, $outputFilename, $mimeType);
$outputFilename = Sanitize::safeFilename($outputFilename); // be sure that there are no problematic chars in the filename. E.g. MacOS X don't like spaces for downloads.
header("Content-type: $mimetype");
if ($flagJson) {
if (false === ($json = json_encode([JSON_TEXT => file_get_contents($file)]))) {
throw new DownloadException(json_encode(
[ERROR_MESSAGE_TO_USER => 'Error converting to JSON',
ERROR_MESSAGE_SUPPORT => "json_last_error()=" . json_last_error() . ", File=" . $file]), ERROR_DOWNLOAD_JSON_CONVERT);
}
$length = strlen($json);
$mimeType = 'application/json';
} else {
$length = filesize($file);
}
header("Content-type: $mimeType");
header("Content-Length: $length");
// If defined as 'attachment': PDFs are not shown inside the browser (if user configured that). Instead, always a 'save as'-dialog appears (Chrome, FF)
// header("Content-Disposition: attachment; filename=$outputFilename");
header("Content-Disposition: inline; filename=\"$outputFilename\"; name=\"$outputFilename\"");
if ($flagJson) {
// If defined as 'attachment': PDFs are not shown inside the browser (if user configured that). Instead, always a 'save as'-dialog appears (Chrome, FF)
// header("Content-Disposition: attachment; filename=$outputFilename");
header("Content-Disposition: inline; filename=\"$outputFilename\"; name=\"$outputFilename\"");
}
header("Pragma: no-cache");
header("Expires: 0");
if ($this->getOutputFormat() === DOWNLOAD_OUTPUT_FORMAT_JSON) {
print json_encode([JSON_TEXT => file_get_contents($file)]);
if ($flagJson) {
print $json;
} else {
readfile($file);
}
......@@ -267,7 +284,7 @@ class Download {
$htmlText = $this->getEvaluatedBodytext($uid, $urlParam);
$tmpFilename = HelperFile::tempnam() . '.html';
$tmpFile = fopen($tmpFilename, "w") or die('Cannot create file: '.$tmpFilename);
$tmpFile = fopen($tmpFilename, "w") or die('Cannot create file: ' . $tmpFilename);
fwrite($tmpFile, $htmlText);
fclose($tmpFile);
......@@ -309,15 +326,15 @@ class Download {
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/
private function getEvaluatedBodyText($uid, $urlParam) {
foreach($urlParam as $key => $paramValue) {
foreach ($urlParam as $key => $paramValue) {
$this->store->setVar($key, $paramValue, STORE_SIP);
}
$dbT3 = $this->store->getVar(SYSTEM_DB_NAME_T3,STORE_SYSTEM);
$dbT3 = $this->store->getVar(SYSTEM_DB_NAME_T3, STORE_SYSTEM);
$sql = "SELECT bodytext FROM $dbT3.tt_content WHERE uid = ?";
$tt_content = $this->db->sql($sql, ROW_EXPECT_1, [$uid]);
$qfq = new QuickFormQuery([T3DATA_BODYTEXT => $tt_content[T3DATA_BODYTEXT]], false,false);
$qfq = new QuickFormQuery([T3DATA_BODYTEXT => $tt_content[T3DATA_BODYTEXT]], false, false);
return $qfq->process();
}
......
......@@ -1420,7 +1420,7 @@ EOF;
$vars[NAME_BOOTSTRAP_BUTTON] = '0';
// Download links should be by default SIP encoded.
if (($vars[NAME_SIP])===false) {
if (($vars[NAME_SIP]) === false) {
$vars[NAME_SIP] = "1";
}
......@@ -1436,6 +1436,9 @@ EOF;
*/
private function buildCopyToClipboardLate($vars) {
$jsAction = '';
$source = '';
if ($vars[NAME_BOOTSTRAP_BUTTON] == '') {
$vars = $this->buildBootstrapButton($vars, '');
}
......@@ -1456,7 +1459,8 @@ EOF;
if ($vars[NAME_COPY_TO_CLIPBOARD] !== '') {
$jsAction = 'onClick';
$source = "{text: '" . OnString::replaceByUnicode($vars[NAME_COPY_TO_CLIPBOARD]) . "' }";
} elseif ($vars[NAME_FILE] !== '') {
} elseif (isset($vars[NAME_COLLECT_ELEMENTS][0])) {
$jsAction = 'onmousedown';
$tmpUrlParam = array();
......@@ -1476,6 +1480,8 @@ EOF;
}
$source = "{uri: '" . API_DIR . '/' . API_DOWNLOAD_PHP . '?s=' . $paramArray[SIP_SIP] . "'}";
} else {
throw new UserReportException("Missing content for 'copy to clipboard'", ERROR_MISSING_CONTENT);
}
// Depending on 'text' or 'file' different JS class are necessary.
......
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