changeset 88:7a9c45b53866

Add possibility to validate using validator.w3.org
author Tom Fredrik "BFG" Klaussen <bfg@blenning.no>
date Wed, 17 Oct 2012 20:43:07 +0200
parents b9654b9c4a66
children fd39d7d5e9be
files Page.inc Validator.inc common-functions.inc constants.inc index.php
diffstat 5 files changed, 94 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/Page.inc	Mon Oct 15 18:32:56 2012 +0200
+++ b/Page.inc	Wed Oct 17 20:43:07 2012 +0200
@@ -4,7 +4,8 @@
 /// @cond
 $baseDir = dirname(__FILE__);
 $cache = ScriptIncludeCache::instance(__FILE__);
-$cache->includeOnce('Options.inc', dirname(__FILE__));
+$cache->includeOnce('Options.inc', $baseDir);
+$cache->includeOnce('Validator.inc', $baseDir);
 /// @endcond
 
 class PageContent
@@ -76,6 +77,8 @@
    */
   function mayCompress()
   {
+    if (!array_key_exists('HTTP_ACCEPT_ENCODING', $_SERVER))
+      return false;
     return (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'));
   }
 
@@ -89,7 +92,14 @@
    */
   function mayValidate()
   {
-    return !$_GET['novalidate'];
+    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');
   }
 
 
@@ -128,12 +138,18 @@
       $this->cache->CheckHttpModified();
     }
     $res = $this->generateContent();
+    if ($this->mayValidate()) {
+      $request = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
+      $validator = new Validator($request);
+      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 Content($res);
+      $res = new PageContent($res);
     }
     elseif (get_class($res) !== "PageContent") {
       throw new InvalidArgumentException("generateContent returned an unexpected type");
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Validator.inc	Wed Oct 17 20:43:07 2012 +0200
@@ -0,0 +1,48 @@
+<?php
+include_once 'ScriptIncludeCache.inc';
+
+/// @cond
+$baseDir = dirname(__FILE__);
+$cache = ScriptIncludeCache::instance(__FILE__);
+$cache->includeOnce('common-functions.inc', $baseDir);
+/// @endcond
+
+
+class Validator {
+
+  private $uri;
+  private $validator_url = 'http://validator.w3.org/check';
+
+  static function get_url_contents($url){
+    $crl = curl_init();
+    $timeout = 5;
+
+    curl_setopt ($crl, CURLOPT_URL, $url);
+    curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
+    curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
+    $ret = curl_exec($crl);
+    curl_close($crl);
+    return $ret;
+  }
+
+  function __construct($uri)
+  {
+    $this->uri = $uri;
+  }
+
+  function check()
+  {
+    $request = urlencode($this->uri);
+    $query= '?uri=' . $request;
+
+    $headers = getHeaders($this->validator_url . $query, 2);
+    return $headers['X-W3C-Validator-Status'] === "Valid";
+  }
+
+  function getUri()
+  {
+    return $this->uri;
+  }
+
+}
+?>
\ No newline at end of file
--- a/common-functions.inc	Mon Oct 15 18:32:56 2012 +0200
+++ b/common-functions.inc	Wed Oct 17 20:43:07 2012 +0200
@@ -12,6 +12,24 @@
 $cache->includeOnce('StatusCodes.inc', $baseDir);
 /// @endcond
 
+class RequestException extends RuntimeException
+{
+  private $_info;
+
+  function __construct($info)
+  {
+    $this->_info = $info;
+    if (array_key_exists('error', $info)) {
+      parent::__construct($info['error']);
+    }
+  }
+
+  function info()
+  {
+    return $this->_info;
+  }
+}
+
 /**
  * Generates a representation for an array of key => value pairs
  *
@@ -194,9 +212,14 @@
  * @param $url the url to query
  * @return an associative array of all headers returned
  */
-function getHeaders($url)
+function getHeaders($url, $timeout = 1)
 {
-  $response = http_head($url, array("timeout" => 1), $info);
+  $response = @http_head($url, array("timeout" => $timeout), $info);
+
+  if (array_key_exists('error', $info) && $info['error']) {
+    throw new RequestException($info);
+  }
+
   $headers = array();
   $response = trim($response);
   $str = explode("\n", $response);
--- a/constants.inc	Mon Oct 15 18:32:56 2012 +0200
+++ b/constants.inc	Wed Oct 17 20:43:07 2012 +0200
@@ -13,6 +13,7 @@
 define(DUMP, 0);
 define(MAX_RECURSE, 50);
 define(CACHING, 1);
+define(VALIDATE, 0);
 
 define(ABORT_ON_LOG, TRUE);
 
--- a/index.php	Mon Oct 15 18:32:56 2012 +0200
+++ b/index.php	Wed Oct 17 20:43:07 2012 +0200
@@ -24,7 +24,7 @@
 
 try {
   $input = new InputParser(basePath() . "/master.xml", $cache);
-  $input->genPage();
+  $input->display();
 }
 catch (Exception $e) {
   errorPage($e, StatusCodes::HTTP_INTERNAL_SERVER_ERROR);