From 4428b8e98d68b7756a0de813475e049c193ca3b9 Mon Sep 17 00:00:00 2001 From: Jeremy Kauffman Date: Fri, 25 Nov 2016 16:06:58 -0500 Subject: [PATCH] always send headers, tweak header functions --- controller/Controller.class.php | 31 ++++++++++++++++--------------- view/Response.class.php | 13 +++++++++---- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/controller/Controller.class.php b/controller/Controller.class.php index e05b7fa5..1879cac8 100644 --- a/controller/Controller.class.php +++ b/controller/Controller.class.php @@ -21,25 +21,26 @@ class Controller throw new Exception('use response::setheader instead of returning headers'); } - if ($viewTemplate === null) - { - return; - } - if (!$viewTemplate) { - throw new LogicException('All execute methods must return a template or NULL.'); + if ($viewTemplate !== null) + { + throw new LogicException('All execute methods must return a template or NULL.'); + } + } + else + { + $layout = !(isset($viewParameters['_no_layout']) && $viewParameters['_no_layout']); + unset($viewParameters['_no_layout']); + + $layoutParams = $viewParameters[View::LAYOUT_PARAMS] ?? []; + unset($viewParameters[View::LAYOUT_PARAMS]); + + $content = View::render($viewTemplate, $viewParameters + ['fullPage' => true]); + + Response::setContent($layout ? View::render('layout/basic', ['content' => $content] + $layoutParams) : $content); } - $layout = !(isset($viewParameters['_no_layout']) && $viewParameters['_no_layout']); - unset($viewParameters['_no_layout']); - - $layoutParams = $viewParameters[View::LAYOUT_PARAMS] ?? []; - unset($viewParameters[View::LAYOUT_PARAMS]); - - $content = View::render($viewTemplate, $viewParameters + ['fullPage' => true]); - - Response::setContent($layout ? View::render('layout/basic', ['content' => $content] + $layoutParams) : $content); Response::setDefaultSecurityHeaders(); if (Request::isGzipAccepted()) { diff --git a/view/Response.class.php b/view/Response.class.php index 5a285d04..901a5fde 100644 --- a/view/Response.class.php +++ b/view/Response.class.php @@ -168,13 +168,18 @@ class Response } public static function setDownloadHttpHeaders($name, $type = null, $size = null, $noSniff = true) + { + static::setBinaryHttpHeaders($type, $size, $noSniff); + static::setHeader('Content-Disposition', 'attachment;filename=' . $name); + } + + public static function setBinaryHttpHeaders($type, $size = null, $noSniff = true) { static::setGzipResponseContent(false); // in case its already compressed static::setHeaders(array_filter([ - 'Content-Disposition' => 'attachment;filename=' . $name, - 'Content-Type' => $type ? 'application/zip' : null, - 'Content-Length' => $size ?: null, - 'X-Content-Type-Options' => $noSniff ? 'nosniff' : null, + static::HEADER_CONTENT_TYPE => $type, + static::HEADER_CONTENT_LENGTH => $size ?: null, + static::HEADER_CONTENT_TYPE_OPTIONS => $noSniff ? 'nosniff' : null, ])); }