view Page.inc @ 93:8aadd7a23b68

Moved some functionality from common-functions into Http class. Reorganized Validator into a class hierarchy. Added functionality for validating with a buffer in addition to URLs.
author Tom Fredrik "BFG" Klaussen <bfg@blenning.no>
date Thu, 18 Oct 2012 16:44:48 +0200
parents 1d4c980f4255
children 2370f4450983
line wrap: on
line source

<?
include_once 'ScriptIncludeCache.inc';

/// @cond
$baseDir = dirname(__FILE__);
$cache = ScriptIncludeCache::instance(__FILE__);
$cache->includeOnce('OnlineBufferValidator.inc', $baseDir);
$cache->includeOnce('Options.inc', $baseDir);
/// @endcond

class PageContent
{
  public $headers = array();
  public $content;

  function __construct($content = "")
  {
    $this->content = $content;
  }

  function setHeader($headername, $value)
  {
    $this->headers[$headername] = $value;
  }

  function __toString()
  {
    return $this->content;
  }
}

/**
 * Master class for generating a page
 */
abstract class Page
{
  private $cache;

  /**
   * Constructs a page
   *
   * @param $cache optionally sets a cache
   */
  function __construct($cache = null)
  {
    $this->setCache($cache);
  }

  /**
   * Set the cache
   *
   * @param $cache The cache object
   */
  protected function setCache($cache)
  {
    $this->cache = $cache;
  }

  /**
   * Get the cache
   *
   * @return The cache object
   */
  protected function getCache()
  {
    return $this->cache;
  }

  /**
   * Decide wether or not this page may be compressed.
   *
   * Normally this is a check for http headers, but some pages
   * eg. pictures may not want to be compressed, and may override this
   * function.
   *
   * @return bool if this page may be compressed
   */
  function mayCompress()
  {
    if (!array_key_exists('HTTP_ACCEPT_ENCODING', $_SERVER))
      return false;
    return (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'));
  }

  /**
   * 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()
  {
    if (!VALIDATE)
      return false;
    if (array_key_exists('novalidate', $_GET))
      return !$_GET['novalidate'];
    if (!array_key_exists('HTTP_USER_AGENT', $_SERVER))
      return false;
    //UserAgent should be W3C_Validator/1.3
    return !startswith($_SERVER['HTTP_USER_AGENT'], 'W3C');
  }


  /**
   * Turns on compression for this page
   *
   * @note This may not be reversed
   */
  function startCompression()
  {
    ob_start("ob_gzhandler");
  }

  /**
   * Generates the actual content of the page
   *
   * @return the content buffer
   */
  abstract function generateContent();

  /**
   * Finishes all necessary processing to determine the cacheset of this page.
   *
   * @return bool if this page may be cached
   */
  abstract function cacheCheck();

  /**
   * Generates an appropriate response to the request.
   *
   * Eg. 302 NOT CHANGED, error message or the actual content
   */
  function genPage()
  {
    if ($this->cacheCheck()) {
      $this->cache->CheckHttpModified();
    }
    $res = $this->generateContent();
    if ($this->mayValidate()) {
      /*
      $request = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
      $validator = new OnlineUriValidator($request);
      */
      $validator = new OnlineBufferValidator($res);
      if (!$validator->check())
	throw new LogicException('The page could be generated, but contained errors');
    }
    if ($this->mayCompress()) {
      $this->startCompression();
    }
    $t = gettype($res);
    if ($t === "string") {
      $res = new PageContent($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 => $value) {
      header("${header}: ${value}");
    }
    print $res;
  }


}
?>