From d54ab06ad4fea56d8e3cfaf950ac5a805d0aecf9 Mon Sep 17 00:00:00 2001 From: Paolo Date: Wed, 17 Sep 2025 16:53:14 +0200 Subject: [PATCH] - Removed DocumentExportLib->showContent - Adapted code to replace DocumentExportLib->showContent with DocumentExportLib->getContent + file download - DocumentExportLib now makes use of the DocumentLib --- application/controllers/Documents.php | 26 ++- .../controllers/api/frontend/v1/Documents.php | 32 +--- .../controllers/api/frontend/v1/stv/Akte.php | 3 +- application/core/FHC_Controller.php | 1 - application/libraries/DmsLib.php | 1 - application/libraries/DocumentExportLib.php | 181 +++--------------- application/libraries/DocumentLib.php | 47 ++--- 7 files changed, 79 insertions(+), 212 deletions(-) diff --git a/application/controllers/Documents.php b/application/controllers/Documents.php index 47aae7ed1..57b2daf6e 100644 --- a/application/controllers/Documents.php +++ b/application/controllers/Documents.php @@ -143,7 +143,7 @@ class Documents extends Auth_Controller $result = $this->VorlagestudiengangModel->getCurrent($xsl, $xsl_oe_kurzbz, $version); if (isError($result)) - return show_error(getError($result)); + show_error(getError($result)); if (!hasData($result)) return show_404(); @@ -226,13 +226,13 @@ class Documents extends Auth_Controller $result = $this->VorlageModel->load($xsl); if (isError($result)) - return show_error(getError($result)); + show_error(getError($result)); if (!hasData($result)) show_404(); $vorlage = current(getData($result)); if ($sign_user && !$vorlage->signierbar) - return show_error($this->p->t("stv", "grades_error_sign")); + show_error($this->p->t("stv", "grades_error_sign")); // Filename @@ -242,7 +242,7 @@ class Documents extends Auth_Controller $this->load->model('organisation/Studiengang_model', 'StudiengangModel'); $result = $this->StudiengangModel->load($this->input->post_get('stg_kz')); if (!isError($result) && hasData($result)) - $filename .= '_' . sanitizeProblemChars(current(getData($result))->kurzbzlang); + /output $filename .= '_' . sanitizeProblemChars(current(getData($result))->kurzbzlang); $this->load->model('organisation/Studiensemester_model', 'StudiensemesterModel'); $result = $this->StudiensemesterModel->load($this->input->post_get('ss')); @@ -284,11 +284,23 @@ class Documents extends Auth_Controller // XML Data $result = $this->documentexportlib->getDataURL($xml, $params); if (isError($result)) - return show_error(getError($result)); + show_error(getError($result)); $data = getData($result); - // Output - $this->documentexportlib->showContent($filename, $vorlage, $data, $xsl_oe_kurzbz, $version, $outputformat, $sign_user); + // Get the content + $contentResult = $this->documentexportlib->getContent($filename, $vorlage, $data, $xsl_oe_kurzbz, $version, $outputformat, $sign_user); + + // If an error occurred + if (isError($contentResult)) show_error(getError($contentResult)); + + $fileObj = new stdClass(); + $fileObj->file_content = getData($contentResult); + $fileObj->name = $filename,; + $fileObj->mimetype = isEmptyString($vorlage->mimetype) ? 'application/pdf' : $vorlage->mimetype; + $fileObj->disposition = 'attachment'; + + // Download + $this->outputFile($fileObj); } } diff --git a/application/controllers/api/frontend/v1/Documents.php b/application/controllers/api/frontend/v1/Documents.php index 2d2c410cf..cb76f7217 100644 --- a/application/controllers/api/frontend/v1/Documents.php +++ b/application/controllers/api/frontend/v1/Documents.php @@ -51,6 +51,8 @@ class Documents extends FHCAPI_Controller $this->loadPhrases([ 'stv' ]); + + $this->load->library('DocumentExportLib'); } /** @@ -100,23 +102,11 @@ class Documents extends FHCAPI_Controller { $akteExportData = $this->_getAkteExportData($xml, $xsl, $sign_user); - $akteData = $akteData['akteData']; - $exportData = $akteData['exportData']; - - /** - * [ - 'vorlage' => $vorlage, - 'xml_data' => $data, - 'oe_kurzbz' => $xsl_oe_kurzbz, - 'version' => $version, - 'outputformat' => $outputformat, - 'sign_user' => $sign_user - ] - */ + $akteData = $akteExportData['akteData']; + $exportData = $akteExportData['exportData']; // Output - $result = $this->documentexportlib->showContent( - $akteData['akteData']['inhalt'], + $contentResult = $this->documentexportlib->getContent( $exportData['vorlage'], $exportData['xml_data'], $exportData['oe_kurzbz'], @@ -125,7 +115,9 @@ class Documents extends FHCAPI_Controller $exportData['sign_user'] ); - $this->terminateWithSuccess(true); + $content = $this->getDataOrTerminateWithError($contentResult); + + $this->terminateWithFileOutput($akteData['mimetype'], $content, $akteData['titel'] . '.pdf'); } /** @@ -458,20 +450,13 @@ class Documents extends FHCAPI_Controller if ($sign_user && !$vorlage->signierbar) $this->terminateWithError($this->p->t("stv", "grades_error_sign")); - - $this->load->library('DocumentExportLib'); - // XML Data $result = $this->documentexportlib->getDataURL($xml, $params); $data = $this->getDataOrTerminateWithError($result); $this->documentexportlib->addArchiveToData($data); - // Output - $result = $this->documentexportlib->getContent($vorlage, $data, $xsl_oe_kurzbz, $version, $outputformat, $sign_user); - $content = $this->getDataOrTerminateWithError($result); $akteData['titel'] .= '.pdf'; - $akteData['inhalt'] = base64_encode($content); return [ 'akteData' => $akteData, @@ -487,3 +472,4 @@ class Documents extends FHCAPI_Controller ]; } } + diff --git a/application/controllers/api/frontend/v1/stv/Akte.php b/application/controllers/api/frontend/v1/stv/Akte.php index d14fb3c63..fbafea4b3 100644 --- a/application/controllers/api/frontend/v1/stv/Akte.php +++ b/application/controllers/api/frontend/v1/stv/Akte.php @@ -58,9 +58,8 @@ class Akte extends Auth_Controller if (!isEmptyString($data->inhalt)) { $fileObj = new stdClass(); - $fileObj->filename = $data->filename; $fileObj->file_content = base64_decode($data->inhalt); - $fileObj->name = $data->titel; + $fileObj->name = $data->filename; $fileObj->mimetype = $data->mimetype; $fileObj->disposition = 'attachment'; diff --git a/application/core/FHC_Controller.php b/application/core/FHC_Controller.php index dc8647272..fe0ca00e4 100644 --- a/application/core/FHC_Controller.php +++ b/application/core/FHC_Controller.php @@ -136,7 +136,6 @@ abstract class FHC_Controller extends CI_Controller /** * To download the given file represented by the fileObj parameter. * fileObj has the following structure: - * $fileObj->file_name * $fileObj->file OR $fileObj->file_content * $fileObj->name * $fileObj->mimetype diff --git a/application/libraries/DmsLib.php b/application/libraries/DmsLib.php index 31628c686..ea106cc64 100644 --- a/application/libraries/DmsLib.php +++ b/application/libraries/DmsLib.php @@ -431,7 +431,6 @@ class DmsLib $lastVersion = getData($lastVersionResult)[0]; $fileObj = new stdClass(); - $fileObj->filename = $lastVersion->filename; $fileObj->file = DMS_PATH.$lastVersion->filename; $fileObj->name = isEmptyString($file_name) ? $lastVersion->name : $file_name; $fileObj->mimetype = $lastVersion->mimetype; diff --git a/application/libraries/DocumentExportLib.php b/application/libraries/DocumentExportLib.php index 03ffbe98c..355abc82c 100644 --- a/application/libraries/DocumentExportLib.php +++ b/application/libraries/DocumentExportLib.php @@ -34,15 +34,11 @@ use SimpleXMLElement as SimpleXMLElement; * modifiing said data (sign()/setXMLTag_archivierbar()) or adding * images (addImage()) and then call output() and close(). * Now the create, output and close functions are combined into one function and adding data and images is done via parameters. - * There are now two functions getContent() and showContent() where showContent() equals to output(false) and getContent equals to output(true) * Instead of calling addDataArray, addDataXML or addDataURL just call * getDataArray, getDataXML or getDataURL respectevily and use the return - * value as $xml_data parameter in the showContent and getContent calls. + * value as $xml_data parameter in the getContent call. * Instead of calling addImages just create an array and pass it as $images - * parameter to the showContent/getContent function. - * The old setFilename() function is now a parameter in showContent(). It is - * not needed in getContent() since that function does not do anything that - * requires a filename. + * parameter to the getContent function. * To get/show a signed document just pass a valid uid as $sign_user * parameter. * @@ -63,21 +59,10 @@ use SimpleXMLElement as SimpleXMLElement; * 'name' => $imagename, * 'contenttype' => $imagecontenttype * ]]; - * $this->documentexportlib->showContent( - * $filename, - * $vorlage, - * $xml_data, - * $oe_kurzbz, - * $version, - * $outputformat, - * null, - * null, - * $images - * ); */ class DocumentExportLib { - private $unoconv_version; + private $_ci; /** * Constructor @@ -85,33 +70,10 @@ class DocumentExportLib public function __construct() { // Gets CI instance - $this->ci =& get_instance(); + $this->_ci =& get_instance(); // Load Phrases - $this->ci->load->library('PhrasesLib', ['document_export', null], 'documentExportPhrases'); - - // Which document converter has to be used - if (defined('DOCSBOX_ENABLED') && DOCSBOX_ENABLED === true) - { - // Use docsbox!! - } - else - { - exec('unoconv --version', $ret_arr); - - if(isset($ret_arr[0])) - { - $hlp = explode(' ', $ret_arr[0]); - if(isset($hlp[1])) - { - $this->unoconv_version = $hlp[1]; - } - else - show_error($this->ci->documentExportPhrases->t("document_export", "error_unoconv_version")); - } - else - show_error($this->ci->documentExportPhrases->t("document_export", "error_unoconv")); - } + $this->_ci->load->library('DocumentLib', ['document_export', null], 'DocumentExportPhrases'); } /** @@ -172,7 +134,7 @@ class DocumentExportLib $xml_data = new DOMDocument; if (!$xml_data->load($xml_url)) - return error($this->ci->documentExportPhrases->t("document_export", "error_xml_load", [ + return error($this->_ci->DocumentExportPhrases->t("document_export", "error_xml_load", [ "url" => $xml_url, "xml" => $xml, "params" => $params @@ -236,7 +198,7 @@ class DocumentExportLib $outputformat = $this->getDefaultOutputFormat($outputformat, $vorlage->mimetype); - $result = $this->createAndSignContent( + $createResult = $this->createAndSignContent( $temp_folder, $outputformat, $vorlage, @@ -247,118 +209,26 @@ class DocumentExportLib $sign_user, $sign_profile ); - if (isError($result)) { + if (isError($createResult)) { $this->close($temp_folder, $source_folder); - return $result; + return $createResult; } - $temp_filename = getData($result); + $temp_filename = getData($createResult); $fsize = filesize($temp_filename); $handle = fopen($temp_filename, 'r'); if (!$handle) - return error($this->ci->documentExportPhrases->t("document_export", "error_file_load")); - $result = fread($handle, $fsize); + return error($this->_ci->DocumentExportPhrases->t("document_export", "error_file_load")); + $fileContentResult = fread($handle, $fsize); fclose($handle); $this->close($temp_folder, $source_folder); - return success($result); + return success($fileContentResult); } /** - * Sets the headers and displays the Document. - * On failure the exit() function will be called - * - * @param string $filename - * @param stdClass $vorlage A db entry from tbl_vorlage - * @param DomDocument $xml_data - * @param string $oe_kurzbz - * @param integer|null $version (optional) - * @param string $outputformat (optional) - * @param string $sign_user (optional) Must be a valid uid - * @param string $sign_profile (optional) Signatureprofile for signing - * @param array $images (optional) Each element should have a property path, name & contenttype which are all strings - * - * @return void - */ - public function showContent( - $filename, - $vorlage, - $xml_data, - $oe_kurzbz, - $version = null, - $outputformat = null, - $sign_user = null, - $sign_profile = null, - $images = [] - ) { - $source_folder = getcwd(); - $temp_folder = sys_get_temp_dir() . '/fhcunoconv-' . uniqid(); - - $outputformat = $this->getDefaultOutputFormat($outputformat, $vorlage->mimetype); - - $result = $this->createAndSignContent( - $temp_folder, - $outputformat, - $vorlage, - $oe_kurzbz, - $version, - $xml_data, - $images, - $sign_user, - $sign_profile - ); - if (isError($result)) { - $this->close($temp_folder, $source_folder); - exit(getError($result)); - } - $temp_filename = getData($result); - - $fsize = filesize($temp_filename); - $handle = fopen($temp_filename, 'r'); - if (!$handle) { - $this->close($temp_folder, $source_folder); - exit($this->ci->documentExportPhrases->t("document_export", "error_file_load")); - } - - if (headers_sent()) { - $this->close($temp_folder, $source_folder); - exit($this->ci->documentExportPhrases->t("document_export", "error_headers")); - } - - switch ($outputformat) { - case 'pdf': - header('Content-type: application/pdf'); - header('Content-Disposition: attachment; filename="' . $filename . '.pdf"'); - header('Content-Length: ' . $fsize); - break; - - case 'doc': - header('Content-type: application/vnd.ms-word'); - header('Content-Disposition: attachment; filename="' . $filename . '.doc"'); - header('Content-Length: ' . $fsize); - break; - - case 'odt': - header('Content-type: application/vnd.oasis.opendocument.text'); - header('Content-Disposition: attachment; filename="' . $filename . '.odt"'); - header('Content-Length: ' . $fsize); - break; - default: - $this->close($temp_folder, $source_folder); - exit($this->ci->documentExportPhrases->t("document_export", "error_outputformat_missing")); - } - - while (!feof($handle)) { - echo fread($handle, 8192); - } - fclose($handle); - - $this->close($temp_folder, $source_folder); - } - - /** - * Helper function for getContent and showContent. + * Helper function for getContent * Creates the temp folder and calls create and sign functions. * * @param string $temp_folder @@ -387,13 +257,13 @@ class DocumentExportLib mkdir($temp_folder); chdir($temp_folder); - $this->ci->load->model('system/Vorlagestudiengang_model', 'VorlagestudiengangModel'); + $this->_ci->load->model('system/Vorlagestudiengang_model', 'VorlagestudiengangModel'); - $result = $this->ci->VorlagestudiengangModel->getCurrent($vorlage->vorlage_kurzbz, $oe_kurzbz, $version); + $result = $this->_ci->VorlagestudiengangModel->getCurrent($vorlage->vorlage_kurzbz, $oe_kurzbz, $version); if (isError($result)) return $result; if (!hasData($result)) - return error($this->ci->documentExportPhrases->t("document_export", "error_template_missing")); + return error($this->_ci->DocumentExportPhrases->t("document_export", "error_template_missing")); $vorlage_stg = current(getData($result)); foreach ($vorlage_stg as $k => $v) $vorlage->$k = $v; @@ -437,7 +307,7 @@ class DocumentExportLib { $content_xsl = new DOMDocument(); if (!$content_xsl->loadXML($vorlage->text)) - return error($this->ci->documentExportPhrases->t("document_export", "error_xsl_load")); + return error($this->_ci->DocumentExportPhrases->t("document_export", "error_xsl_load")); $proc = new XSLTProcessor(); $proc->importStyleSheet($content_xsl); @@ -454,7 +324,7 @@ class DocumentExportLib if ($vorlage->style) { $styles_xsl = new DOMDocument(); if (!$styles_xsl->loadXML($vorlage->style)) - return error($this->ci->documentExportPhrases->t("document_export", "error_styles_load")); + return error($this->_ci->DocumentExportPhrases->t("document_export", "error_styles_load")); $style_proc = new XSLTProcessor(); $style_proc->importStyleSheet($styles_xsl); @@ -482,7 +352,7 @@ class DocumentExportLib $tempname_zip = $temp_folder . '/out.zip'; if (!copy($zipfile, $tempname_zip)) - return error($this->ci->documentExportPhrases->t("document_export", "error_file_copy")); + return error($this->_ci->DocumentExportPhrases->t("document_export", "error_file_copy")); exec("zip $tempname_zip content.xml"); if (!is_null($styles_xsl)) @@ -502,7 +372,7 @@ class DocumentExportLib $manifest_xml = new DOMDocument; if (!$manifest_xml->loadXML($manifest)) - return error($this->ci->documentExportPhrases->t("document_export", "error_manifest")); + return error($this->_ci->DocumentExportPhrases->t("document_export", "error_manifest")); //root-node holen $root = $manifest_xml->getElementsByTagName('manifest')->item(0); @@ -557,7 +427,7 @@ class DocumentExportLib } if ($ret) - return error($this->ci->documentExportPhrases->t("document_export", "error_conv_timeout")); + return error($this->_ci->DocumentExportPhrases->t("document_export", "error_conv_timeout")); break; case 'odt': default: @@ -582,7 +452,7 @@ class DocumentExportLib protected function sign($temp_folder, $temp_filename, $outputformat, $user, $profile) { if ($outputformat != 'pdf') - return error($this->ci->documentExportPhrases->t("document_export", "error_sign_pdf")); + return error($this->_ci->DocumentExportPhrases->t("document_export", "error_sign_pdf")); // Load the File $file_data = file_get_contents($temp_filename); @@ -624,7 +494,7 @@ class DocumentExportLib $result = curl_exec($ch); if (curl_errno($ch)) { curl_close($ch); - return error($this->ci->documentExportPhrases->t("document_export", "error_sign_timeout")); + return error($this->_ci->DocumentExportPhrases->t("document_export", "error_sign_timeout")); } curl_close($ch); $resultdata = json_decode($result); @@ -637,7 +507,7 @@ class DocumentExportLib } // otherwise if it is an error - return error($resultdata->retval ?? $this->ci->documentExportPhrases->t("global", "unknown_error", ["error" => $result])); + return error($resultdata->retval ?? $this->_ci->DocumentExportPhrases->t("global", "unknown_error", ["error" => $result])); } /** @@ -714,3 +584,4 @@ class DocumentExportLib return 'pdf'; } } + diff --git a/application/libraries/DocumentLib.php b/application/libraries/DocumentLib.php index c1dd21c29..326f26bcf 100644 --- a/application/libraries/DocumentLib.php +++ b/application/libraries/DocumentLib.php @@ -4,7 +4,8 @@ if (! defined('BASEPATH')) exit('No direct script access allowed'); class DocumentLib { - private $unoconv_version; + private $_ci; + private $_unoconv_version; /** * Constructor @@ -12,7 +13,10 @@ class DocumentLib public function __construct() { // Gets CI instance - $this->ci =& get_instance(); + $this->_ci =& get_instance(); + + // Load Phrases + $this->_ci->load->library('PhrasesLib', ['document_export', null], 'DocumentExportPhrases'); // Which document converter has to be used if (defined('DOCSBOX_ENABLED') && DOCSBOX_ENABLED === true) @@ -28,13 +32,13 @@ class DocumentLib $hlp = explode(' ', $ret_arr[0]); if(isset($hlp[1])) { - $this->unoconv_version = $hlp[1]; + $this->_unoconv_version = $hlp[1]; } else - show_error('Could not get Unoconv Version'); + show_error($this->_ci->DocumentExportPhrases->t('document_export', 'error_unoconv_version')); } else - show_error('Unoconv not found - Please install Unoconv'); + show_error($this->_ci->DocumentExportPhrases->t('document_export', 'error_unoconv')); } } @@ -56,7 +60,7 @@ class DocumentLib case 'image/jpeg': case 'image/jpg': case 'image/pjpeg': - $this->_jpegtopdf($filename, $outFile); + $this->_jpegToPdf($filename, $outFile); return success($outFile); case 'application/vnd.oasis.opendocument.spreadsheet': case 'application/msword': @@ -72,23 +76,19 @@ class DocumentLib else { // Unoconv Version 0.6 seems to fail on converting TXT Files - if ($this->unoconv_version == '0.6') - return error(); + if ($this->_unoconv_version == '0.6') return error('Unoconv Version 0.6 does not convert TXT Files correctly'); } + // Convertion $ret = $this->convert($filename, $outFile, 'pdf'); - if(isSuccess($ret)) - { - return success($outFile); - } - else - { - return error(getError($ret)); - } + if(isSuccess($ret)) return success($outFile); + + // Error + return $ret; case 'application/pdf': return success($filename); default: - return error('Unknown Mimetype:'.$mimetype); + return error('Unknown Mimetype:' . $mimetype); } } @@ -101,20 +101,20 @@ class DocumentLib */ public function mergePDF($files, $outFile) { - $cmd = "gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=$outFile "; + $cmd = 'gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=$outFile '; $finfo = finfo_open(FILEINFO_MIME_TYPE); // add all pdf files to the command foreach ($files as $f) { - $cmd .= $f." "; + $cmd .= $f.' '; if (!file_exists($f)) { return error("File not found: '$f'"); } - if (finfo_file($finfo, $f) != "application/pdf") + if (finfo_file($finfo, $f) != 'application/pdf') { - return error("Wrong format(".finfo_file($finfo, $f)."): '$f'"); + return error('Wrong format('.finfo_file($finfo, $f)."): '$f'"); } } @@ -150,7 +150,7 @@ class DocumentLib } else // otherwise use unoconv { - if ($this->unoconv_version == '0.6') + if ($this->_unoconv_version == '0.6') $command = 'unoconv -f %1$s %3$s > %2$s'; else $command = 'unoconv -f %s --output %s %s 2>&1'; @@ -175,7 +175,7 @@ class DocumentLib * @param string $outfile Path to Output (pdf) File. * @return success or error object */ - private function _jpegtopdf($filename, $outfile) + private function _jpegToPdf($filename, $outfile) { if (!file_exists($filename)) return error('File does not exists'); @@ -230,3 +230,4 @@ class DocumentLib return true; } } +