Factorizes the content type / content cache headers, and improves caching of static...
authorVincent Zanotti <vincent.zanotti@m4x.org>
Sun, 8 Nov 2009 03:37:09 +0000 (04:37 +0100)
committerVincent Zanotti <vincent.zanotti@m4x.org>
Sun, 8 Nov 2009 18:27:10 +0000 (19:27 +0100)
Signed-off-by: Vincent Zanotti <vincent.zanotti@m4x.org>
classes/plfeed.php
classes/plglobals.php
classes/plpage.php
classes/plvcard.php
classes/plwizard.php
include/platal.inc.php
modules/core.php

index 7d660e5..19be252 100644 (file)
@@ -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);
index a3e62b2..67cd8b8 100644 (file)
@@ -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.
      */
index cc932a6..f444708 100644 (file)
@@ -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');
index 67e56a1..7ba0042 100644 (file)
@@ -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()) {
index f630020..a14970a 100644 (file)
@@ -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 {
index cffcb58..ef02627 100644 (file)
@@ -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:
 ?>
index 0db98a5..56dd9e2 100644 (file)
@@ -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;