Mercurial > SimpleWebPresenter
changeset 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 | f468365813c9 |
| children | 2370f4450983 |
| files | Http.inc OnlineBufferValidator.inc OnlineURIValidator.inc OnlineValidator.inc Page.inc Sitemap.inc Validator.inc common-functions.inc |
| diffstat | 8 files changed, 181 insertions(+), 70 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Http.inc Thu Oct 18 16:44:48 2012 +0200 @@ -0,0 +1,77 @@ +<?php +include_once 'ScriptIncludeCache.inc'; + +/// @cond +$baseDir = dirname(__FILE__); + +$cache = ScriptIncludeCache::instance(__FILE__); +/// @endcond + +class Http +{ + static function headersToArray($response) + { + $headers = array(); + $response = trim($response); + $str = explode("\n", $response); + $headers[''] = trim($str[0]); + foreach($str as $kv) { + $p = strpos($kv, ":"); + if ($p) { + $key = substr($kv, 0, $p); + $value = trim(substr($kv, $p + 1)); + $headers[$key] = $value; + } + } + return $headers; + } + + /** + * Queries a URL for headers + * + * @param $url the url to query + * @return an associative array of all headers returned + */ + static function getHeaders($url, $timeout = 1) + { + $response = @http_head($url, array("timeout" => $timeout), $info); + + if (array_key_exists('error', $info) && $info['error']) { + throw new RequestException($info); + } + + return self::headersToArray($response); + } + + static function postHeaders($url, $params, $timeout = 1) + { + $crl = curl_init(); + + $descriptorspec = array( + 0 => array("pipe", "r"), + 1 => array("pipe", "w"), + //2 => array("file", "/tmp/error-output.txt", "a") + ); + + //We use tac, since it buffers, and we don't care about the output + //being reordered. + $process = proc_open('tac | tac', $descriptorspec, $pipes); + + curl_setopt ($crl, CURLOPT_URL, $url); + curl_setopt ($crl, CURLOPT_WRITEHEADER, $pipes[0]); + curl_setopt ($crl, CURLOPT_NOBODY, true); + curl_setopt ($crl, CURLOPT_POST, true); + curl_setopt ($crl, CURLOPT_POSTFIELDS, $params); + + curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); + curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); + $ret = curl_exec($crl); + curl_close($crl); + + fclose($pipes[0]); + $buf = fread($pipes[1], 8192); + + return self::headersToArray($buf); + } +} +?> \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OnlineBufferValidator.inc Thu Oct 18 16:44:48 2012 +0200 @@ -0,0 +1,30 @@ +<?php +include_once 'ScriptIncludeCache.inc'; + +/// @cond +$baseDir = dirname(__FILE__); +$cache = ScriptIncludeCache::instance(__FILE__); +$cache->includeOnce('Http.inc', $baseDir); +$cache->includeOnce('OnlineValidator.inc', $baseDir); +/// @endcond + + +class OnlineBufferValidator extends OnlineValidator +{ + + private $buffer; + + function __construct($buffer) + { + $this->buffer = $buffer; + } + + function check() + { + $params = array( 'fragment' => $this->buffer); + + $headers = Http::postHeaders($this->validator_url , $params, 2); + return $headers['X-W3C-Validator-Status'] === "Valid"; + } +} +?> \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OnlineURIValidator.inc Thu Oct 18 16:44:48 2012 +0200 @@ -0,0 +1,49 @@ +<?php +include_once 'ScriptIncludeCache.inc'; + +/// @cond +$baseDir = dirname(__FILE__); +$cache = ScriptIncludeCache::instance(__FILE__); +$cache->includeOnce('Http.inc', $baseDir); +$cache->includeOnce('OnlineValidator.inc', $baseDir); +/// @endcond + + +class OnlineURIValidator extends OnlineValidator +{ + private $uri; + + 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 = Http::getHeaders($this->validator_url . $query, 5); + return $headers['X-W3C-Validator-Status'] === "Valid"; + } + + function getUri() + { + return $this->uri; + } + +} +?> \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OnlineValidator.inc Thu Oct 18 16:44:48 2012 +0200 @@ -0,0 +1,15 @@ +<?php +include_once 'ScriptIncludeCache.inc'; + +/// @cond +$baseDir = dirname(__FILE__); +$cache = ScriptIncludeCache::instance(__FILE__); +$cache->includeOnce('Validator.inc', $baseDir); +/// @endcond + + +abstract class OnlineValidator extends Validator +{ + protected $validator_url = 'http://validator.w3.org/check'; +} +?> \ No newline at end of file
--- a/Page.inc Thu Oct 18 14:04:19 2012 +0200 +++ b/Page.inc Thu Oct 18 16:44:48 2012 +0200 @@ -4,8 +4,8 @@ /// @cond $baseDir = dirname(__FILE__); $cache = ScriptIncludeCache::instance(__FILE__); +$cache->includeOnce('OnlineBufferValidator.inc', $baseDir); $cache->includeOnce('Options.inc', $baseDir); -$cache->includeOnce('Validator.inc', $baseDir); /// @endcond class PageContent @@ -139,8 +139,11 @@ } $res = $this->generateContent(); if ($this->mayValidate()) { + /* $request = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; - $validator = new Validator($request); + $validator = new OnlineUriValidator($request); + */ + $validator = new OnlineBufferValidator($res); if (!$validator->check()) throw new LogicException('The page could be generated, but contained errors'); }
--- a/Sitemap.inc Thu Oct 18 14:04:19 2012 +0200 +++ b/Sitemap.inc Thu Oct 18 16:44:48 2012 +0200 @@ -4,8 +4,9 @@ /// @cond $baseDir = dirname(__FILE__); $cache = ScriptIncludeCache::instance(__FILE__); +$cache->includeOnce('Http.inc', $baseDir); +$cache->includeOnce('Page.inc', $baseDir); $cache->includeOnce('common-functions.inc', $baseDir); -$cache->includeOnce('Page.inc', dirname(__FILE__)); /// @endcond /** @@ -81,7 +82,7 @@ $optstring = opttostring($opts); $location = "${base}/${optstring}"; - $headers = getHeaders($location); + $headers = Http::getHeaders($location, 5); $location = htmlentities($location);
--- a/Validator.inc Thu Oct 18 14:04:19 2012 +0200 +++ b/Validator.inc Thu Oct 18 16:44:48 2012 +0200 @@ -4,45 +4,10 @@ /// @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; - } - +abstract class Validator { + abstract function check(); } ?> \ No newline at end of file
--- a/common-functions.inc Thu Oct 18 14:04:19 2012 +0200 +++ b/common-functions.inc Thu Oct 18 16:44:48 2012 +0200 @@ -215,35 +215,6 @@ } /** - * Queries a URL for headers - * - * @param $url the url to query - * @return an associative array of all headers returned - */ -function getHeaders($url, $timeout = 1) -{ - $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); - $headers[''] = trim($str[0]); - foreach($str as $kv) { - $p = strpos($kv, ":"); - if ($p) { - $key = substr($kv, 0, $p); - $value = trim(substr($kv, $p + 1)); - $headers[$key] = $value; - } - } - return $headers; -} - -/** * Generates the query part of an URI * * @param $opts an associative array of options
