From fffc450e876284331bceaf2432cfc82127db8a6c Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Sun, 13 Jul 2025 21:29:34 +0200 Subject: [PATCH] organic: Fix Content-Disposition --- www/organic/external/easy-cert/operators.php | 4 +- www/organic/pdf.php | 53 ++++++++++++++++++-- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/www/organic/external/easy-cert/operators.php b/www/organic/external/easy-cert/operators.php index bb59a1f..5f87c96 100644 --- a/www/organic/external/easy-cert/operators.php +++ b/www/organic/external/easy-cert/operators.php @@ -267,9 +267,7 @@ $process = proc_open( $pipes ); fclose($pipes[0]); -while (($line = fgets($pipes[1])) !== false) { - echo $line; -} +fpassthru($pipes[1]); fclose($pipes[1]); $stderr = stream_get_contents($pipes[2]); fclose($pipes[2]); diff --git a/www/organic/pdf.php b/www/organic/pdf.php index 177441b..d19495d 100644 --- a/www/organic/pdf.php +++ b/www/organic/pdf.php @@ -176,7 +176,54 @@ if ($format === 'text') { echo "{\"type\":\"unknown\"}\n"; } } else { - header('Content-Type: application/pdf'); - $s = curl_init($url); - curl_exec($s); + $fd_spec = [ + 0 => ["pipe", "r"], // stdin + 1 => ["pipe", "w"], // stdout + 2 => ["pipe", "w"], // stderr + 3 => ["pipe", "w"], // headers + ]; + $process = proc_open(['curl', '-s', '-D', '/dev/fd/3', $url], $fd_spec, $pipes); + fclose($pipes[0]); + + $headers = []; + $status_code = null; + while (($line = fgets($pipes[3])) !== false) { + if (trim($line) === '') break; + if ($status_code === null) { + $status_code = intval(explode(' ', $line)[1]); + if ($status_code !== 200) + break; + continue; + } + [$k,$v] = explode(':', $line, 2); + $k = strtolower(trim($k)); + $v = trim($v); + $headers[$k] = $v; + } + fclose($pipes[3]); + + if ($status_code === 200 && str_starts_with($headers['content-type'], "application/pdf")) { + header('Content-Type: application/pdf'); + $content_length = null; + if (isset($headers['content-length'])) { + $content_length = intval($headers['content-length']); + header('Content-Length: ' . $headers['content-length']); + } + $parts = explode('/', $url); + $filename = $parts[sizeof($parts) - 1]; + if (isset($headers['content-disposition'])) { + preg_match('@filename="(.*?)"@', $headers['content-disposition'], $matches); + $filename = $matches[1]; + } + header('Content-Disposition: inline; filename="' . $filename . '"'); + fpassthru($pipes[1]); + } else { + header('Status: 500'); + header('Content-Length: 0'); + } + fclose($pipes[1]); + + $stderr = stream_get_contents($pipes[2]); + fclose($pipes[2]); + $return_value = proc_close($process); }