diff --git a/extension/qfq/api/print.php b/extension/qfq/api/print.php index 080a9c58d6fc9cb692335ce3daabc09770f87a2b..98da3af3251c41f84f8e7c1c2b21c83bf2f23181 100644 --- a/extension/qfq/api/print.php +++ b/extension/qfq/api/print.php @@ -11,126 +11,18 @@ namespace qfq; use qfq; -require_once(__DIR__ . '/../qfq/store/Config.php'); -require_once(__DIR__ . '/../qfq/Constants.php'); -require_once(__DIR__ . '/../qfq/helper/KeyValueStringParser.php'); - -const PAGEID = 'id'; -const PARAM_GET = 'paramGet'; -const URL_PRINT = 'urlPrint'; - -/** - * Create a temporary file. - * - * @return string - */ -function createEmptyFile() { - return tempnam(sys_get_temp_dir(), "webkitPrintPdf"); -} - -/** - * Set HTML Header to initiate PDF download. - * - * @param $filename - */ -function setHeader($filename) { - - header("Content-Disposition: inline; filename=\"$filename\""); - header("Content-Type: application/pdf"); - header("Content-Transfer-Encoding: binary"); -} - -/** - * Read QFQ config. Only SYSTEM_BASE_URL_PRINT and SYSTEM_WKHTMLTOPDF will be used. - * Check and get all clean _GET Parameter. Build a URL based on SYSTEM_BASE_URL_PRINT and the delivered URL params. - * - * @return array - * @throws UserFormException - * @throws \exception - */ -function init() { - $cfg = new Config(); - - $config = $cfg->readConfig(''); - $param = readCleanGetParam(); - - if (!isset($config[SYSTEM_BASE_URL_PRINT]) || $config[SYSTEM_BASE_URL_PRINT] == '') { - throw new \exception(CONFIG_INI . ' - Missing ' . SYSTEM_BASE_URL_PRINT); - } - - if (!isset($config[SYSTEM_WKHTMLTOPDF]) || $config[SYSTEM_WKHTMLTOPDF] == '') { - throw new \exception(CONFIG_INI . ' - Missing ' . SYSTEM_WKHTMLTOPDF); - } - - if (!is_executable($config[SYSTEM_WKHTMLTOPDF])) { - throw new \exception(CONFIG_INI . ' - ' . SYSTEM_WKHTMLTOPDF . '=' . $config[SYSTEM_WKHTMLTOPDF] . ' - not found or not executable.'); - } - - if (!isset($param[PAGEID]) || $param[PAGEID] == '') { - throw new \exception("Missing GET Parameter '" . PAGEID . "'."); - } - - $setup = array(); - $setup[URL_PRINT] = $config[SYSTEM_BASE_URL_PRINT] . '/?' . KeyValueStringParser::unparse($param, '=', '&'); - $setup[PAGEID] = $param[PAGEID]; - $setup[SYSTEM_WKHTMLTOPDF] = $config[SYSTEM_WKHTMLTOPDF]; - - return $setup; -} - -; - -/** - * Return an array with GET params who are clean - they do not violate $pattern. - * - * @return array - */ -function readCleanGetParam() { - - $param = array(); - $pattern = '^[\-_\.,;:\/a-zA-Z0-9]*$'; // ':alnum:' does not work here in FF - - foreach ($_GET as $key => $value) { - if (preg_match("/$pattern/", $value) === 1) { - $param[$key] = $value; - } - } - - return $param; -} - -/** - * @param array $setup - * @throws \exception - */ -function page2pdf($setup) { - - $rc = 0; - $file = createEmptyFile(); - - $cmd = $setup[SYSTEM_WKHTMLTOPDF] . " '" . $setup[URL_PRINT] . "' " . $file . " > $file.log 2>&1"; - - $line = system($cmd, $rc); - - if ($rc == 0) { - setHeader('print.' . $setup[PAGEID] . '.pdf'); - @readfile($file); - @unlink($file); - @unlink($file . '.log'); - exit; // Do an extremely hard exit here to make sure there are no more additional bytes sent (makes the delivered PDF unusable). - } else { - throw new \exception("Error [RC=$rc] $line: $cmd"); - } -} - +//require_once(__DIR__ . '/../qfq/store/Config.php'); +//require_once(__DIR__ . '/../qfq/Constants.php'); +require_once(__DIR__ . '/../qfq/report/Html2Pdf.php'); +//require_once(__DIR__ . '/../qfq/helper/KeyValueStringParser.php'); /** * Main */ try { - $setup = init(); + $html2pdf = new Html2Pdf(); - page2pdf($setup); + $html2pdf->outputHtml2Pdf(); } catch (\Exception $e) { echo "Exception: " . $e->getMessage(); diff --git a/extension/qfq/qfq/report/Html2Pdf.php b/extension/qfq/qfq/report/Html2Pdf.php new file mode 100644 index 0000000000000000000000000000000000000000..44c219e5d895141b318765de3d1119fa6ebfbe6a --- /dev/null +++ b/extension/qfq/qfq/report/Html2Pdf.php @@ -0,0 +1,134 @@ +<?php +/** + * Created by PhpStorm. + * User: crose + * Date: 4/17/17 + * Time: 10:17 PM + */ + +namespace qfq; + +require_once(__DIR__ . '/../store/Config.php'); +require_once(__DIR__ . '/../Constants.php'); +require_once(__DIR__ . '/../helper/KeyValueStringParser.php'); + + +class Html2Pdf { + + /** + * @var array + */ + private $config = array(); + + /** + * Read QFQ config. Only SYSTEM_BASE_URL_PRINT and SYSTEM_WKHTMLTOPDF will be used. + * Check and get all clean _GET Parameter. Build a URL based on SYSTEM_BASE_URL_PRINT and the delivered URL params. + * + * @param array $config + * @throws UserFormException + * @throws \exception + */ + public function __construct(array $config = array()) { + + if (count($config) == 0) { + $cfg = new Config(); + + $config = $cfg->readConfig(''); + } + + $this->config = $config; + + if (!isset($config[SYSTEM_BASE_URL_PRINT]) || $config[SYSTEM_BASE_URL_PRINT] == '') { + throw new \exception(CONFIG_INI . ' - Missing ' . SYSTEM_BASE_URL_PRINT); + } + + if (!isset($config[SYSTEM_WKHTMLTOPDF]) || $config[SYSTEM_WKHTMLTOPDF] == '') { + throw new \exception(CONFIG_INI . ' - Missing ' . SYSTEM_WKHTMLTOPDF); + } + + if (!is_executable($config[SYSTEM_WKHTMLTOPDF])) { + throw new \exception(CONFIG_INI . ' - ' . SYSTEM_WKHTMLTOPDF . '=' . $config[SYSTEM_WKHTMLTOPDF] . ' - not found or not executable.'); + } + } + + /** + * Return an array with GET params who are clean - they do not violate $pattern. + * + * @return array + */ + private function readCleanGetParam(array $get) { + + $param = array(); + $pattern = '^[\-_\.,;:\/a-zA-Z0-9]*$'; // ':alnum:' does not work here in FF + + foreach ($get as $key => $value) { + if (preg_match("/$pattern/", $value) === 1) { + $param[$key] = $value; + } + } + + return $param; + } + + /** + * Set HTML Header to initiate PDF download. + * + * @param $filename + */ + private function setHeader($filename) { + + header("Content-Disposition: inline; filename=\"$filename\""); + header("Content-Type: application/pdf"); + header("Content-Transfer-Encoding: binary"); + } + + /** + * @return string + * @throws \exception + */ + public function page2pdf(array $get) { + + if (!isset($get[HTML2PDF_PAGEID]) || $get[HTML2PDF_PAGEID] == '') { + throw new \exception("Missing GET Parameter '" . HTML2PDF_PAGEID . "'."); + } + + $urlPrint = escapeshellarg($this->config[SYSTEM_BASE_URL_PRINT] . '/?' . KeyValueStringParser::unparse($get, '=', '&')); + $wkhtml = $this->config[SYSTEM_WKHTMLTOPDF]; + + $rc = 0; + $filename = tempnam(sys_get_temp_dir(), DOWNLOAD_FILE_PREFIX); + $filenameEscape = escapeshellarg($filename); + +// $cmd = $wkhtml . " '" . $urlPrint . "' " . $filename . " > $filename.log 2>&1"; +// $cmd = "$wkhtml '$urlPrint' $filename > $filename.log 2>&1"; + $cmd = "$wkhtml $urlPrint $filenameEscape"; + + $line = system($cmd, $rc); + + if ($rc != 0) { + throw new \exception("Error [RC=$rc] $line: $cmd"); + } + + return $filename; + } + + /** + * @throws \exception + */ + public function outputHtml2Pdf() { + + $get = $this->readCleanGetParam($_GET); + $pageId = Support::setIfNotSet($get, HTML2PDF_PAGEID, 0); + + $filename = $this->page2pdf($get); + + $this->setHeader('print.' . $pageId . '.pdf'); + @readfile($filename); + @unlink($filename); +// @unlink($filename . '.log'); + + exit; // Do an extremely hard exit here to make sure there are no more additional bytes sent (makes the delivered PDF unusable). + + } + +} \ No newline at end of file