From a405b91e42040108c6c4a4a368ddd7a89ed41bc6 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Sun, 13 Dec 2020 22:06:16 +0900 Subject: [PATCH] Add RawDisplayHandler and fix RSS/Atom not producing the correct headers --- classes/context/Context.class.php | 10 +++++++--- classes/display/DisplayHandler.class.php | 20 ++++++++++++++++++++ classes/display/RawDisplayHandler.php | 20 ++++++++++++++++++++ common/autoload.php | 1 + modules/rss/rss.view.php | 2 +- 5 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 classes/display/RawDisplayHandler.php diff --git a/classes/context/Context.class.php b/classes/context/Context.class.php index b467ed6e4..c009a9f4a 100644 --- a/classes/context/Context.class.php +++ b/classes/context/Context.class.php @@ -1021,10 +1021,14 @@ class Context * @param string $method Response method. [HTML|XMLRPC|JSON] * @return void */ - public static function setResponseMethod($method = 'HTML') + public static function setResponseMethod($method = 'HTML', $content_type = null) { - $methods = array('HTML' => 1, 'XMLRPC' => 1, 'JSON' => 1, 'JS_CALLBACK' => 1); + $methods = array('HTML' => 1, 'XMLRPC' => 1, 'JSON' => 1, 'JS_CALLBACK' => 1, 'RAW' => 1); self::$_instance->response_method = isset($methods[$method]) ? $method : 'HTML'; + if ($content_type) + { + self::$_instance->response_content_type = $content_type; + } } /** @@ -1040,7 +1044,7 @@ class Context } $method = self::getRequestMethod(); - $methods = array('HTML' => 1, 'XMLRPC' => 1, 'JSON' => 1, 'JS_CALLBACK' => 1); + $methods = array('HTML' => 1, 'XMLRPC' => 1, 'JSON' => 1, 'JS_CALLBACK' => 1, 'RAW' => 1); return isset($methods[$method]) ? $method : 'HTML'; } diff --git a/classes/display/DisplayHandler.class.php b/classes/display/DisplayHandler.class.php index 77c4f876d..1e7c61458 100644 --- a/classes/display/DisplayHandler.class.php +++ b/classes/display/DisplayHandler.class.php @@ -52,6 +52,10 @@ class DisplayHandler extends Handler $this->gz_enabled = FALSE; } } + elseif(Context::getResponseMethod() == 'RAW') + { + $handler = new RawDisplayHandler(); + } else { $handler = new HTMLDisplayHandler(); @@ -114,6 +118,10 @@ class DisplayHandler extends Handler { self::_printXMLHeader(); } + elseif(Context::getResponseMethod() == 'RAW' && $content_type = Context::get('response_content_type')) + { + self::_printCustomContentTypeHeader($content_type); + } else { self::_printHTMLHeader(); @@ -329,6 +337,18 @@ class DisplayHandler extends Handler { header("Content-Type: application/json; charset=UTF-8"); } + + /** + * print a custom Content-Type header. + * + * @param string $content_type + * @return void + */ + public static function _printCustomContentTypeHeader($content_type) + { + $charset = (strpos($content_type, 'text/') === 0) ? '; charset=UTF-8' : ''; + header('Content-Type: ' . $content_type . $charset); + } /** * print a HTTP HEADER for HTML, which is encoded in UTF-8 diff --git a/classes/display/RawDisplayHandler.php b/classes/display/RawDisplayHandler.php new file mode 100644 index 000000000..903400673 --- /dev/null +++ b/classes/display/RawDisplayHandler.php @@ -0,0 +1,20 @@ +getTemplatePath(); + $tpl_file = $oModule->getTemplateFile(); + if ($tpl_path && $tpl_file) + { + $oTemplate = TemplateHandler::getInstance(); + $output = $oTemplate->compile($tpl_path, $tpl_file); + } + else + { + $output = ''; + } + return $output; + } +} diff --git a/common/autoload.php b/common/autoload.php index 3e4d8395a..04bf3c3f1 100644 --- a/common/autoload.php +++ b/common/autoload.php @@ -53,6 +53,7 @@ $GLOBALS['RX_AUTOLOAD_FILE_MAP'] = array_change_key_case(array( 'HTMLDisplayHandler' => 'classes/display/HTMLDisplayHandler.php', 'JSCallbackDisplayHandler' => 'classes/display/JSCallbackDisplayHandler.php', 'JSONDisplayHandler' => 'classes/display/JSONDisplayHandler.php', + 'RawDisplayHandler' => 'classes/display/RawDisplayHandler.php', 'VirtualXMLDisplayHandler' => 'classes/display/VirtualXMLDisplayHandler.php', 'XMLDisplayHandler' => 'classes/display/XMLDisplayHandler.php', 'EditorHandler' => 'classes/editor/EditorHandler.class.php', diff --git a/modules/rss/rss.view.php b/modules/rss/rss.view.php index 18599d652..38d454063 100644 --- a/modules/rss/rss.view.php +++ b/modules/rss/rss.view.php @@ -187,7 +187,7 @@ class rssView extends rss Context::set('info', $info); // Set XML Output - Context::setResponseMethod('XMLRPC'); + Context::setResponseMethod('RAW', 'text/xml'); $this->setTemplatePath($this->module_path . 'tpl/format'); $this->setTemplateFile($template); }