changeset 86:b9654b9c4a66

Make headers a part of the content, rather than setting them directly.
author Tom Fredrik "BFG" Klaussen <bfg@blenning.no>
date Mon, 15 Oct 2012 18:32:56 +0200
parents 590e6950fa7f
children 0add7b7f1505 7a9c45b53866
files Flag.inc Page.inc Sitemap.inc flag.php sitemap.php
diffstat 5 files changed, 58 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/Flag.inc	Mon Oct 15 16:53:29 2012 +0200
+++ b/Flag.inc	Mon Oct 15 18:32:56 2012 +0200
@@ -78,7 +78,8 @@
       errorPage('Resource not found', 404);
     }
     else {
-      header("Content-Type: image/png");
+      $flag = new PageContent($flag);
+      $flag->addHeader("Content-Type: image/png");
       return $flag;
     }
   }
--- a/Page.inc	Mon Oct 15 16:53:29 2012 +0200
+++ b/Page.inc	Mon Oct 15 18:32:56 2012 +0200
@@ -7,6 +7,27 @@
 $cache->includeOnce('Options.inc', dirname(__FILE__));
 /// @endcond
 
+class PageContent
+{
+  public $headers = array();
+  public $content;
+
+  function __construct($content = "")
+  {
+    $this->content = $content;
+  }
+
+  function addHeader($txt)
+  {
+    array_push($this->headers, $txt);
+  }
+
+  function __toString()
+  {
+    return $this->content;
+  }
+}
+
 /**
  * Master class for generating a page
  */
@@ -59,6 +80,20 @@
   }
 
   /**
+   * Decide wether or not this page may be validated.
+   *
+   * Normally this is a check for the option novalidate, but this may
+   * be overridden
+   *
+   * @return bool if this page may be validated
+   */
+  function mayValidate()
+  {
+    return !$_GET['novalidate'];
+  }
+
+
+  /**
    * Turns on compression for this page
    *
    * @note This may not be reversed
@@ -96,6 +131,22 @@
     if ($this->mayCompress()) {
       $this->startCompression();
     }
+    $t = gettype($res);
+    if ($t === "string") {
+      $res = new Content($res);
+    }
+    elseif (get_class($res) !== "PageContent") {
+      throw new InvalidArgumentException("generateContent returned an unexpected type");
+    }
+    return $res;
+  }
+
+  function display()
+  {
+    $res = $this->genPage();
+    foreach ($res->headers as $header) {
+      header($header);
+    }
     print $res;
   }
 
--- a/Sitemap.inc	Mon Oct 15 16:53:29 2012 +0200
+++ b/Sitemap.inc	Mon Oct 15 18:32:56 2012 +0200
@@ -103,8 +103,9 @@
 
     $out .= '</urlset>';
 
-    header('Content-type: application/xml');
-    return $out;
+    $res = new PageContent($out);
+    $res->addHeader('Content-type: application/xml');
+    return $res;
   }
 }
 ?>
--- a/flag.php	Mon Oct 15 16:53:29 2012 +0200
+++ b/flag.php	Mon Oct 15 18:32:56 2012 +0200
@@ -13,7 +13,7 @@
 
 try {
   $flag = new Flag($scriptCache);
-  $flag->genPage();
+  $flag->display();
 }
 catch (Exception $e) {
   errorPage($e, StatusCodes::HTTP_INTERNAL_SERVER_ERROR);
--- a/sitemap.php	Mon Oct 15 16:53:29 2012 +0200
+++ b/sitemap.php	Mon Oct 15 18:32:56 2012 +0200
@@ -13,7 +13,7 @@
 
 try {
   $sitemap = new Sitemap("master.xml");
-  $sitemap->genPage();
+  $sitemap->display();
 }
 catch (Exception $e) {
   errorPage($e, StatusCodes::HTTP_INTERNAL_SERVER_ERROR);