Commit 4a8835ee authored by Carsten  Rose's avatar Carsten Rose
Browse files

Html2Pdf.php: Add SIP support wkhtmltopdf URLs. Move cookies for wkhtmltopdf...

Html2Pdf.php: Add SIP support wkhtmltopdf URLs. Move cookies for wkhtmltopdf from commandline arguments to filebased.
parent 0205a612
......@@ -183,6 +183,7 @@ const ERROR_IO_RENAME = 1307;
const ERROR_IO_INVALID_LINK = 1308;
const ERROR_IO_DIR_EXIST_AS_FILE = 1309;
const ERROR_IO_CHDIR = 1310;
const ERROR_IO_CREATE_FILE = 1311;
//Report
const ERROR_UNKNOWN_LINK_QUALIFIER = 1400;
......@@ -897,6 +898,8 @@ const HTML2PDF_PAGEID = 'id';
const HTML2PDF_PARAM_GET = 'paramGet';
const HTML2PDF_URL_PRINT = 'urlPrint';
const SESSION_COOKIE_REFEIX = 'qfq.cookie.'; // temporary filename on server of single export file
// Class: LINK
const PARAM_DELIMITER = '|';
......
......@@ -13,8 +13,10 @@ namespace qfq;
require_once(__DIR__ . '/../store/Config.php');
require_once(__DIR__ . '/../store/Session.php');
require_once(__DIR__ . '/../store/Sip.php');
require_once(__DIR__ . '/../Constants.php');
require_once(__DIR__ . '/../helper/KeyValueStringParser.php');
require_once(__DIR__ . '/../helper/SessionCookie.php');
class Html2Pdf {
......@@ -29,15 +31,26 @@ class Html2Pdf {
*/
private $session = null;
/**
* @var \qfq\SessionCookie
*/
private $sessionCookie = null;
/**
* @var \qfq\Sip
*/
private $sip = null;
/**
* 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
* @param $phpUnit
* @throws UserFormException
* @throws \exception
*/
public function __construct(array $config = array()) {
public function __construct(array $config = array(), $phpUnit = false) {
if (count($config) == 0) {
$cfg = new Config();
......@@ -60,8 +73,9 @@ class Html2Pdf {
}
$urlParts = parse_url($config[SYSTEM_BASE_URL_PRINT]);
$this->session = Session::getInstance();
$this->session = Session::getInstance($phpUnit);
$this->sessionCookie = new SessionCookie($urlParts['host'], $urlParts['path']);
$this->sip = new Sip($phpUnit);
}
......@@ -97,20 +111,24 @@ class Html2Pdf {
}
/**
* @return string
* @param string $urlParamString
* @param array $rcArgs
* @return array
* @throws UserFormException
*/
private function wkhtmlSessionCookies() {
$result = '';
$cookieNames = array_keys($_COOKIE);
private function splitParam($urlParamString, array &$rcArgs) {
$urlParamNew = array();
foreach ($cookieNames as $name) {
if (isset($_COOKIE[$name])) {
$result .= " --cookie '" . $name . "' '" . urlencode($_COOKIE[$name]) . "'";
$urlParam = KeyValueStringParser::parse($urlParamString, '=', '&', KVP_IF_VALUE_EMPTY_COPY_KEY);
foreach ($urlParam as $key => $value) {
if (substr($key, 0, 1) == '-') {
$rcArgs[$key] = $value;
} else {
$urlParamNew[$key] = $value;
}
}
return trim($result);
return $urlParamNew;
}
/**
......@@ -123,7 +141,7 @@ class Html2Pdf {
* @throws \exception
*/
public function page2pdf($token, $url) {
$args = array();
$rcArgs = array();
$urlParamNew = array();
$urlParamString = '';
$host = '';
......@@ -143,23 +161,18 @@ class Html2Pdf {
break;
}
$urlParam = KeyValueStringParser::parse($urlParamString, '=', '&', KVP_IF_VALUE_EMPTY_COPY_KEY);
foreach ($urlParam as $key => $value) {
if (substr($key, 0, 1) == '-') {
$args[$key] = $value;
} else {
$urlParamNew[$key] = $value;
}
$urlParam = $this->splitParam($urlParamString, $rcArgs);
$rcArgs = OnArray::arrayEscapeshellarg($rcArgs);
$options = KeyValueStringParser::unparse($rcArgs, ' ', ' ');
$urlParamString = KeyValueStringParser::unparse($urlParam, '=', '&');
if ($token == TOKEN_URL_PARAM) {
$urlParamString = $this->sip->queryStringToSip($urlParamString, RETURN_URL);
}
$args = OnArray::arrayEscapeshellarg($args);
$options = KeyValueStringParser::unparse($args, ' ', ' ');
$urlParamString = KeyValueStringParser::unparse($urlParamNew, '=', '&');
if ($urlParamString != '') {
if (substr($host, -1, 1) != '/') {
$host .= '/';
}
$url = $host . '?' . $urlParamString;
$url = Support::mergeUrlComponents('', $host, $urlParamString);
}
if (substr($url, 0, 4) != 'http') {
......@@ -172,10 +185,12 @@ class Html2Pdf {
$filename = tempnam(sys_get_temp_dir(), DOWNLOAD_FILE_PREFIX);
$filenameEscape = escapeshellarg($filename);
$cookieOptions = $this->wkhtmlSessionCookies();
$cookieOptions = '--cookie-jar ' . escapeshellarg($this->sessionCookie->getFile());
// Very important: The current lock on session SESSION_NAME has to be freed, cause wkhtmltopdf will use the same
// session in a few moments and this script remains active all the time.
$this->session->close();
$cmd = "$wkhtmlToPdf $cookieOptions $options $urlPrint $filenameEscape > /tmp/wk.out 2>&1";
$cmd = "$wkhtmlToPdf $cookieOptions $options $urlPrint $filenameEscape";
$rc = 0;
$line = system($cmd, $rc);
......
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