comparison Page.inc @ 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 1d4c980f4255
comparison
equal deleted inserted replaced
86:b9654b9c4a66 88:7a9c45b53866
2 include_once 'ScriptIncludeCache.inc'; 2 include_once 'ScriptIncludeCache.inc';
3 3
4 /// @cond 4 /// @cond
5 $baseDir = dirname(__FILE__); 5 $baseDir = dirname(__FILE__);
6 $cache = ScriptIncludeCache::instance(__FILE__); 6 $cache = ScriptIncludeCache::instance(__FILE__);
7 $cache->includeOnce('Options.inc', dirname(__FILE__)); 7 $cache->includeOnce('Options.inc', $baseDir);
8 $cache->includeOnce('Validator.inc', $baseDir);
8 /// @endcond 9 /// @endcond
9 10
10 class PageContent 11 class PageContent
11 { 12 {
12 public $headers = array(); 13 public $headers = array();
74 * 75 *
75 * @return bool if this page may be compressed 76 * @return bool if this page may be compressed
76 */ 77 */
77 function mayCompress() 78 function mayCompress()
78 { 79 {
80 if (!array_key_exists('HTTP_ACCEPT_ENCODING', $_SERVER))
81 return false;
79 return (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')); 82 return (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'));
80 } 83 }
81 84
82 /** 85 /**
83 * Decide wether or not this page may be validated. 86 * Decide wether or not this page may be validated.
87 * 90 *
88 * @return bool if this page may be validated 91 * @return bool if this page may be validated
89 */ 92 */
90 function mayValidate() 93 function mayValidate()
91 { 94 {
92 return !$_GET['novalidate']; 95 if (!VALIDATE)
96 return false;
97 if (array_key_exists('novalidate', $_GET))
98 return !$_GET['novalidate'];
99 if (!array_key_exists('HTTP_USER_AGENT', $_SERVER))
100 return false;
101 //UserAgent should be W3C_Validator/1.3
102 return !startswith($_SERVER['HTTP_USER_AGENT'], 'W3C');
93 } 103 }
94 104
95 105
96 /** 106 /**
97 * Turns on compression for this page 107 * Turns on compression for this page
126 { 136 {
127 if ($this->cacheCheck()) { 137 if ($this->cacheCheck()) {
128 $this->cache->CheckHttpModified(); 138 $this->cache->CheckHttpModified();
129 } 139 }
130 $res = $this->generateContent(); 140 $res = $this->generateContent();
141 if ($this->mayValidate()) {
142 $request = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
143 $validator = new Validator($request);
144 if (!$validator->check())
145 throw new LogicException('The page could be generated, but contained errors');
146 }
131 if ($this->mayCompress()) { 147 if ($this->mayCompress()) {
132 $this->startCompression(); 148 $this->startCompression();
133 } 149 }
134 $t = gettype($res); 150 $t = gettype($res);
135 if ($t === "string") { 151 if ($t === "string") {
136 $res = new Content($res); 152 $res = new PageContent($res);
137 } 153 }
138 elseif (get_class($res) !== "PageContent") { 154 elseif (get_class($res) !== "PageContent") {
139 throw new InvalidArgumentException("generateContent returned an unexpected type"); 155 throw new InvalidArgumentException("generateContent returned an unexpected type");
140 } 156 }
141 return $res; 157 return $res;