From 0e65d382d395420f7b8d9756b0414f9e441013d5 Mon Sep 17 00:00:00 2001 From: Jan Date: Wed, 8 Nov 2017 13:46:33 +0100 Subject: [PATCH] Do not override custom Content-Type Header --- src/Response.php | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/Response.php b/src/Response.php index d63339e..803be3f 100644 --- a/src/Response.php +++ b/src/Response.php @@ -25,14 +25,19 @@ public static function init() { * adds a header for sending later * @param String $header * @param Int|null $httpcode - * @param Boolean|true $replace + * @param Boolean|true $replace (meaning of replace see: http://php.net/manual/en/function.header.php) + * @param Boolean|false $onlyIfNotExists (add the header only if no other header of this type (e.g. Content-Type) exists * @return Void */ - public static function addHeader(string $header, int $httcode=null, bool $replace=true) { - if (!isset(self::$_headers[$header]) || $replace) { - self::$_headers[$header]=Array(); + public static function addHeader(string $header, int $httcode=null, bool $replace=true, bool $onlyIfNotExists=false) { + $headerKey = explode(':', $header)[0]; + + if ($onlyIfNotExists && isset(self::$_headers[$headerKey])) return; + + if (!isset(self::$_headers[$headerKey]) || $replace) { + self::$_headers[$headerKey]=Array(); } - self::$_headers[$header][]=Array($httcode, $replace); + self::$_headers[$headerKey][]=Array($httcode, $replace, $header); } @@ -75,11 +80,11 @@ public static function addHeaderServerError() { */ public static function setOutput($data) { if ($data===null) { - self::addHeader('Content-Type: text/html; charset=UTF-8'); + self::addHeader('Content-Type: text/html; charset=UTF-8', null, true, true); self::$_output=''; } elseif (self::$_outputFormat=='json' || (!is_scalar($data) && !$data instanceof Template)) { - Response::addHeader('Content-Type: application/json; charset=UTF-8'); + Response::addHeader('Content-Type: application/json; charset=UTF-8', null, true, true); //if (is_array($data) && !isset($data['success'])) $data['success']=true; $options=null; @@ -89,7 +94,7 @@ public static function setOutput($data) { self::$_output=json_encode($data, $options); } else { - self::addHeader('Content-Type: text/html; charset=UTF-8'); + self::addHeader('Content-Type: text/html; charset=UTF-8', null, true, true); // disable layout on ajax requests if ($data instanceof Template && Request::isAjax()) $data->disableLayout(); @@ -167,9 +172,9 @@ public static function setException(\JSMF\Exception $e) { */ public static function output() { // send headers - foreach (self::$_headers as $header => $headers) { + foreach (self::$_headers as $headerKey => $headers) { foreach ($headers as $headerData) { - header($header, $headerData[1], $headerData[0]); + header($headerData[2], $headerData[1], $headerData[0]); } }