From a286fc7adaddd6aadd16dc69595fe929809d84b3 Mon Sep 17 00:00:00 2001 From: Vincent Zanotti Date: Sun, 8 Nov 2009 04:37:09 +0100 Subject: [PATCH] Factorizes the content type / content cache headers, and improves caching of static resources. Signed-off-by: Vincent Zanotti --- classes/plfeed.php | 2 +- classes/plglobals.php | 7 +++++++ classes/plpage.php | 4 ++-- classes/plvcard.php | 5 +---- classes/plwizard.php | 2 +- include/platal.inc.php | 37 +++++++++++++++++++++++++++++++++++++ modules/core.php | 9 ++++----- 7 files changed, 53 insertions(+), 13 deletions(-) diff --git a/classes/plfeed.php b/classes/plfeed.php index 7d660e5..19be252 100644 --- a/classes/plfeed.php +++ b/classes/plfeed.php @@ -107,7 +107,7 @@ abstract class PlFeed implements PlIterator } $page->assign('rss_hash', $token); - header('Content-Type: application/rss+xml; charset=utf8'); + pl_content_headers("application/rss+xml"); $this->iterator = $this->fetch($user); $page->coreTpl('feed.' . $type . '.tpl', NO_SKIN); $page->assign_by_ref('feed', $this); diff --git a/classes/plglobals.php b/classes/plglobals.php index a3e62b2..67cd8b8 100644 --- a/classes/plglobals.php +++ b/classes/plglobals.php @@ -106,6 +106,13 @@ class PlGlobals public $cookie_ns = 'ORG'; public $cookie_path = '/'; + /** Cache duration for static and dynamic cacheable content generated by + * plat/al. Defaults to a week for static content, and an hour for dynamic + * content. + */ + public $static_cache_duration = 604800; + public $dynamic_cache_duration = 3600; + /** You must give a list of file to load. * The filenames given are relatives to the config path of your plat/al installation. */ diff --git a/classes/plpage.php b/classes/plpage.php index cc932a6..f444708 100644 --- a/classes/plpage.php +++ b/classes/plpage.php @@ -156,7 +156,7 @@ abstract class PlPage extends Smarty $this->addJsLink('wiki.js'); header("Accept-Charset: utf-8"); if (Env::v('forceXml')) { - header("Content-Type: text/xml; charset=utf-8"); + pl_content_headers("text/xml"); } if (!$globals->debug) { @@ -335,7 +335,7 @@ abstract class PlPage extends Smarty // {{{ function jsonDisplay protected function jsonDisplay() { - header("Content-type: text/javascript; charset=utf-8"); + pl_content_headers("text/javascript"); array_walk_recursive($this->_jsonVars, "escape_xorgDB"); $jsonbegin = Env::v('jsonBegin'); $jsonend = Env::v('jsonEnd'); diff --git a/classes/plvcard.php b/classes/plvcard.php index 67e56a1..7ba0042 100644 --- a/classes/plvcard.php +++ b/classes/plvcard.php @@ -829,14 +829,11 @@ abstract class PlVCard */ public function show() { - header("Pragma: "); - header("Cache-Control: "); - /* XXX: RFC2425 defines the mime content-type text/directory. * VCard inherits this type as a profile type. Maybe test/x-vcard * could be better. To be checked. */ - header("Content-type: text/directory; profile=vCard; charset=" . self::$charset); + pl_cached_dynamic_content_headers("text/directory; profile=vCard", "utf-8"); $it = $this->fetch(); while ($item = $it->next()) { diff --git a/classes/plwizard.php b/classes/plwizard.php index f630020..a14970a 100644 --- a/classes/plwizard.php +++ b/classes/plwizard.php @@ -222,7 +222,7 @@ class PlWizard $page = $this->getPage($curpage); } if ($mode == 'ajax') { - header('Content-Type: text/html; charset=utf-8'); + pl_content_headers("text/html"); $smarty->changeTpl($this->layout, NO_SKIN); $smarty->assign('wiz_run_ajax', true); } else { diff --git a/include/platal.inc.php b/include/platal.inc.php index cffcb58..ef02627 100644 --- a/include/platal.inc.php +++ b/include/platal.inc.php @@ -176,5 +176,42 @@ function pl_entity_decode($text, $mode = ENT_COMPAT) return html_entity_decode($text, $mode, 'UTF-8'); } +/** + * Adds content type headers; by default the encoding used is utf-8. + */ +function pl_content_headers($content_type, $encoding = 'utf-8') +{ + if (is_null($encoding)) { + header("Content-Type: $content_type"); + } else { + header("Content-Type: $content_type; charset=$encoding"); + } +} + +/** + * Adds content type and caching headers for content generated by plat/al. The + * cache duration defaults to the global static_cache_duration. No encoding is + * applied by default. + */ +function pl_cached_content_headers($content_type, $encoding = null, $cache_duration = -1) +{ + global $globals; + $cache_duration = ($cache_duration < 0 ? $globals->static_cache_duration : $cache_duration); + + header("Cache-Control: max-age=$cache_duration"); + header("Expires: " . gmdate('D, d M Y H:i:s', time() + $cache_duration) . " GMT"); + pl_content_headers($content_type, $encoding); +} + +/** + * Same as above, but applying an expiration time suitable for cacheable dynamic + * content (eg. photos, logos, ...). + */ +function pl_cached_dynamic_content_headers($content_type, $encoding = null) +{ + global $globals; + pl_cached_content_headers($content_type, $encoding, $globals->dynamic_cache_duration); +} + // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: ?> diff --git a/modules/core.php b/modules/core.php index 0db98a5..56dd9e2 100644 --- a/modules/core.php +++ b/modules/core.php @@ -75,9 +75,8 @@ class CoreModule extends PLModule function handler_favicon(&$page) { global $globals; - $data = file_get_contents($globals->spoolroot . '/htdocs/images/favicon.ico'); - header('Content-Type: image/x-icon'); - echo $data; + pl_cached_content_headers("image/x-icon"); + readfile($globals->spoolroot . '/htdocs/images/favicon.ico'); exit; } @@ -95,7 +94,7 @@ class CoreModule extends PLModule } if (count($disallowed_uris) > 0) { - header('Content-Type: text/plain'); + pl_cached_content_headers("text/plain"); echo "User-agent: *\n"; foreach ($disallowed_uris as $uri) { echo "Disallow: $uri\n"; @@ -171,7 +170,7 @@ class CoreModule extends PLModule /// Shared handler for wiki syntax result preview function handler_wiki_preview(&$page, $action = 'title') { - header('Content-Type: text/html; charset=utf-8'); + pl_content_headers("text/html"); $text = Env::v('text'); echo MiniWiki::wikiToHtml($text, $action == 'title'); exit; -- 2.1.4