comparison Page.inc.php @ 134:b6b4a58c7625

Using .inc.php rather than just .inc for include files.
author Tom Fredrik Blenning <bfg@bfgconsult.no>
date Sun, 22 Jan 2023 19:22:00 +0100
parents Page.inc@df158368051e
children
comparison
equal deleted inserted replaced
133:00255ca89459 134:b6b4a58c7625
1 <?php
2 include_once 'ScriptIncludeCache.inc.php';
3
4 /// @cond
5 $baseDir = dirname(__FILE__);
6 $cache = ScriptIncludeCache::instance(__FILE__);
7 $cache->includeOnce('OnlineBufferValidator.inc.php', $baseDir);
8 $cache->includeOnce('Options.inc.php', $baseDir);
9 /// @endcond
10
11 /**
12 * Container for headers and content, used for compounding these
13 */
14 class PageContent
15 {
16 /// The headers for the page
17 public $headers = array();
18 /// The content of the page
19 public $content;
20
21 /**
22 * Constructs a PageContent object containing only content
23 *
24 * @param $content The content of this page
25 */
26 function __construct($content = "")
27 {
28 $this->content = $content;
29 }
30
31 /**
32 * Sets a header
33 *
34 * @param $headername Name of the header
35 * @param $value Value for the header
36 */
37 function setHeader($headername, $value)
38 {
39 $this->headers[$headername] = $value;
40 }
41
42 /**
43 * Returns a string representation of this object, containing only
44 * the content
45 *
46 * @return the content of the page
47 */
48 function __toString()
49 {
50 return $this->content;
51 }
52 }
53
54 /**
55 * Master class for generating a page
56 */
57 abstract class Page
58 {
59 private $cache;
60 private $variants = array();
61
62 function addVariant($value)
63 {
64 array_push($this->variants, $value);
65 }
66
67 function getVariants()
68 {
69 return $this->variants;
70 }
71
72 /**
73 * Constructs a page
74 *
75 * @param $cache optionally sets a cache
76 */
77 function __construct($cache = null)
78 {
79 $this->setCache($cache);
80 }
81
82 /**
83 * Set the cache
84 *
85 * @param $cache The cache object
86 */
87 protected function setCache($cache)
88 {
89 $this->cache = $cache;
90 }
91
92 /**
93 * Get the cache
94 *
95 * @return The cache object
96 */
97 protected function getCache()
98 {
99 return $this->cache;
100 }
101
102 /**
103 * Decide wether or not this page may be compressed.
104 *
105 * Normally this is a check for http headers, but some pages
106 * eg. pictures may not want to be compressed, and may override this
107 * function.
108 *
109 * @return bool if this page may be compressed
110 */
111 function mayCompress()
112 {
113 if (COMPRESSION_DISABLED)
114 return false;
115 //We want to add the variant even if we don't serve with compression.
116 $this->addVariant('Accept-Encoding');
117 if (!array_key_exists('HTTP_ACCEPT_ENCODING', $_SERVER))
118 return false;
119 return (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'));
120 }
121
122 /**
123 * Decide wether or not this page may be validated.
124 *
125 * Normally this is a check for the option novalidate, but this may
126 * be overridden
127 *
128 * @return bool if this page may be validated
129 */
130 function mayValidate()
131 {
132 if (!VALIDATE)
133 return false;
134 if (array_key_exists('novalidate', $_GET))
135 return !$_GET['novalidate'];
136 if (!array_key_exists('HTTP_USER_AGENT', $_SERVER))
137 return false;
138 //UserAgent should be W3C_Validator/1.3
139 return !startswith($_SERVER['HTTP_USER_AGENT'], 'W3C');
140 }
141
142
143 /**
144 * Turns on compression for this page
145 *
146 * @note This may not be reversed
147 */
148 function startCompression()
149 {
150 ob_start("ob_gzhandler");
151 }
152
153 /**
154 * Generates the actual content of the page
155 *
156 * @return the content buffer
157 */
158 abstract function generateContent();
159
160 /**
161 * Finishes all necessary processing to determine the cacheset of this page.
162 *
163 * @return bool if this page may be cached
164 */
165 abstract function cacheCheck();
166
167 /**
168 * Generates an appropriate response to the request.
169 *
170 * Eg. 304 NOT CHANGED, error message or the actual content
171 */
172 function genPage()
173 {
174 $cacheable = $this->cacheCheck();
175 if ($cacheable == Cacheable::YES) {
176 if (!CheckHttpModified($this->cache))
177 return false;
178 }
179 $res = $this->generateContent();
180 if ($cacheable == Cacheable::UNDETERMINED) {
181 $cacheable = $this->cacheCheck();
182 if ($cacheable == Cacheable::YES) {
183 if (!CheckHttpModified($this->cache))
184 return false;
185 }
186 }
187
188 if ($this->mayValidate()) {
189 /*
190 $request = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
191 $validator = new OnlineUriValidator($request);
192 */
193 $validator = new OnlineBufferValidator($res);
194 if (!$validator->check())
195 throw new LogicException('The page could be generated, but contained errors');
196 }
197 if ($this->mayCompress()) {
198 $this->startCompression();
199 }
200 $t = gettype($res);
201 if ($t === "string") {
202 $res = new PageContent($res);
203 }
204 elseif (get_class($res) !== "PageContent") {
205 throw new InvalidArgumentException("generateContent returned an unexpected type");
206 }
207 if ($variants = $this->getVariants()) {
208 $res->setHeader('Vary', join(",", $variants));
209 }
210 return $res;
211 }
212
213 /**
214 * Displays the result from genPage.
215 *
216 * Printing headers and content.
217 */
218 function display()
219 {
220 $res = $this->genPage();
221 if ($res) {
222 foreach ($res->headers as $header => $value) {
223 header("${header}: ${value}");
224 }
225 print $res;
226 }
227 }
228
229
230 }
231 ?>