X-Git-Url: http://git.roojs.org/?a=blobdiff_plain;f=webkitpdf.php;h=4c470b51e303e4f487026ab73dc2ef2ada23b8f6;hb=6cef2d9683e5f19fb32cc92a5c7f6bb41b9b3f1c;hp=8992ee632bbb70676f9881f53a98c922b87ea549;hpb=7c2729064a1fa2a8fe9515e1367c6f266a8d4e9c;p=app.webkitpdf diff --git a/webkitpdf.php b/webkitpdf.php index 8992ee6..4c470b5 100644 --- a/webkitpdf.php +++ b/webkitpdf.php @@ -30,20 +30,70 @@ class WebkitPdf require_once 'System.php'; - $xvfb = System::which('xvfb'); + $timeout= System::which('timeout'); + if (empty($timeout)) { + $this->h404("missing timeout"); + } + + $xvfb = System::which('xvfb-run'); if (empty($xvfb)) { $this->h404("missing xvfb"); } - $xvfb = System::which('webkitpdf'); - if (empty($xvfb)) { + $webkitpdf = System::which('webkitpdf'); + if (empty($webkitpdf )) { $this->h404("missing webkitpdf (compile it..)"); } - // max delay 4 seconds.? - $delay = empty($_REQUEST['delay']) ? 1 : max(4,$_REQUEST['delay']); + // max delay 20 seconds.? bit generous.. + $delay = empty($_REQUEST['delay']) ? 1 : max(20,$_REQUEST['delay']); + + //?? allow injections? + // not yet.. + + $outpr = tempnam(ini_get('session.save_path'), 'webkitTMP'); + unlink($outpr); + $out = $outpr .'.pdf'; + + // can take 2-5 minutes... + + $cmd = "timeout 300s $xvfb --auto-servernum $webkitpdf " . + " --url " . escapeshellarg($_REQUEST['url']) . ' ' . + " --pdf " . $out . ' '; + + $res = `$cmd`; + + if (!file_exists($out)) { + $this->h404("Failed to create file $cmd ==> $res"); + } + header('Content-type: application/octet-stream'); + header("Expires: "); + header("Cache-Control: must-revalidate, post-check=0,pre-check=0"); + header("Pragma: public"); + header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT"); + + //header('Content-length: '. filesize($out)); + header('Content-Disposition: attachment; filename="' . htmlspecialchars(basename($out)). '"'); + ini_set('display_errors', 0); + + $fh = fopen($out, 'rb'); + fpassthru($fh); + + fclose($fh); + + + unlink($out); + exit; + + } + + function h404($msg) + { + header("HTTP/1.0 404 Not Found"); + echo htmlspecialchars($msg); + exit; } } \ No newline at end of file