1 : <?php
2 :
3 :
4 :
5 :
6 :
7 : class View {
8 :
9 :
10 :
11 : public $error_message = '';
12 :
13 :
14 :
15 : public $template = '';
16 :
17 :
18 :
19 : public $title = DEFAULT_TITLE;
20 :
21 :
22 :
23 : public $view = 'home';
24 :
25 :
26 :
27 :
28 : protected $css = array('main' => True);
29 :
30 :
31 :
32 : protected $js = array('main' => True);
33 :
34 :
35 :
36 : protected $http_header_code = 200;
37 :
38 :
39 :
40 : protected $http_header_content_type = 'text/html; charset=utf-8';
41 :
42 :
43 :
44 : protected $http_header_location = null;
45 :
46 :
47 :
48 : protected $http_header_string = 'OK';
49 :
50 :
51 :
52 : protected $http_headers = array();
53 :
54 : public function __construct()
55 : {
56 32 : $this->view = get_class($this);
57 32 : }
58 :
59 : public function preBuild($plugins)
60 : {
61 1 : if (is_array($plugins)) {
62 1 : foreach ($plugins as $plugin) {
63 1 : $plugin = $plugin.'Plugin';
64 1 : if (class_exists($plugin)) {
65 1 : $plugin = new $plugin;
66 1 : $plugin->preBuild($this);
67 1 : }
68 1 : }
69 1 : $this->build();
70 1 : foreach ($plugins as $plugin) {
71 1 : $plugin = $plugin.'Plugin';
72 1 : if (class_exists($plugin)) {
73 1 : $plugin = new $plugin;
74 1 : $plugin->postBuild($this);
75 1 : }
76 1 : }
77 1 : }
78 1 : }
79 :
80 :
81 :
82 :
83 :
84 :
85 : public function build() {
86 2 : }
87 :
88 :
89 :
90 :
91 :
92 :
93 :
94 : public function add_css($css)
95 : {
96 1 : $this->css[$css] = True;
97 1 : }
98 :
99 :
100 :
101 :
102 :
103 : public function remove_css($css)
104 : {
105 2 : $this->css[$css] = False;
106 2 : }
107 :
108 :
109 :
110 :
111 :
112 : public function add_js($js)
113 : {
114 1 : $this->js[$js] = True;
115 1 : }
116 :
117 :
118 :
119 :
120 :
121 : public function remove_js($js)
122 : {
123 2 : $this->js[$js] = False;
124 2 : }
125 :
126 :
127 :
128 :
129 :
130 :
131 :
132 :
133 : public function set_HTTP_status_code($code, $location = null)
134 : {
135 : switch ($code) {
136 10 : case 200:
137 2 : $this->http_header_string = 'OK';
138 2 : $this->http_header_code = $code;
139 2 : $this->http_header_location = null;
140 2 : break;
141 9 : case 301:
142 1 : $this->http_header_string = 'Moved Permanently';
143 1 : $this->http_header_code = $code;
144 1 : $this->http_header_location = $location;
145 1 : break;
146 8 : case 302:
147 1 : $this->http_header_string = 'Found';
148 1 : $this->http_header_code = $code;
149 1 : $this->http_header_location = $location;
150 1 : break;
151 7 : case 303:
152 1 : $this->http_header_string = 'See Other';
153 1 : $this->http_header_code = $code;
154 1 : $this->http_header_location = $location;
155 1 : break;
156 6 : case 307:
157 3 : $this->http_header_string = 'Temporary Redirect';
158 3 : $this->http_header_code = $code;
159 3 : $this->http_header_location = $location;
160 3 : break;
161 3 : case 404:
162 1 : $this->http_header_string = 'Not Found';
163 1 : $this->http_header_code = $code;
164 1 : $this->http_header_location = null;
165 1 : break;
166 2 : case 410:
167 1 : $this->http_header_string = 'Gone';
168 1 : $this->http_header_code = $code;
169 1 : $this->http_header_location = null;
170 1 : break;
171 1 : case 500:
172 1 : $this->http_header_string = 'Internal Server Error';
173 1 : $this->http_header_code = $code;
174 1 : $this->http_header_location = null;
175 1 : break;
176 : }
177 10 : }
178 :
179 :
180 :
181 :
182 :
183 :
184 : public function set_content_type($type, $charset = null)
185 : {
186 2 : $content_type = $type;
187 2 : if ($charset) {
188 1 : $content_type .= "; charset=$charset";
189 1 : }
190 2 : $this->http_header_content_type = $content_type;
191 2 : }
192 :
193 :
194 :
195 :
196 : public function set_HTTP_header($header, $value)
197 : {
198 1 : $this->http_headers[$header] = $value;
199 1 : }
200 :
201 :
202 :
203 :
204 :
205 :
206 :
207 :
208 :
209 :
210 : protected function build_HTTP_headers()
211 : {
212 4 : $return = array();
213 4 : if (300 < $this->http_header_code and $this->http_header_code < 400) {
214 :
215 2 : if (strlen($this->http_header_location)) {
216 2 : if (preg_match('/(https?):\/\/([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)'.
217 2 : '+[a-zA-Z]{2,9}(:\d{1,4})?([-\w\/#~:.?+=&%@#]*)/',
218 2 : $this->http_header_location)) {
219 :
220 1 : $return[1] = sprintf('Location: %s',$this->http_header_location);
221 1 : } else {
222 :
223 1 : $this->set_HTTP_status_code(200);
224 : }
225 2 : }
226 2 : }
227 4 : $return[0] = sprintf('HTTP/1.0 %d %s',
228 4 : $this->http_header_code,
229 4 : $this->http_header_string);
230 4 : if (strlen($this->http_header_content_type)) {
231 4 : $return[2] = sprintf('Content-type: %s', $this->http_header_content_type);
232 4 : }
233 4 : foreach ($this->http_headers as $code => $value) {
234 1 : $return[] = sprintf('%s: %s', $code, $value);
235 4 : }
236 4 : ksort($return);
237 4 : return array_reverse($return);
238 : }
239 :
240 :
241 :
242 :
243 :
244 : public function send_HTTP_headers()
245 : {
246 0 : if (False == headers_sent($file, $line)) {
247 0 : foreach ($this->build_HTTP_headers() as $header) {
248 0 : header($header);
249 0 : }
250 0 : } else {
251 0 : echo $this->debug("Warning: Headers have already been sent in file ".
252 0 : "$file on line $line\n");
253 : }
254 0 : }
255 :
256 :
257 :
258 :
259 :
260 :
261 :
262 :
263 :
264 : public function debug($string)
265 : {
266 2 : $return = '';
267 2 : if (DEBUG) {
268 :
269 0 : $return = sprintf('<strong>You are seeing this message becuase DEBUG '.
270 0 : 'is set to True in settings.php</strong><br />%s',
271 0 : $string);
272 0 : }
273 :
274 2 : return $return;
275 : }
276 :
277 :
278 :
279 :
280 :
281 :
282 :
283 :
284 : public function body()
285 : {
286 2 : $page = $this;
287 2 : ob_start();
288 2 : if (!@include ROOT_DIR."/templates/{$this->view}.php") {
289 1 : $this->debug(sprintf("<p>Sorry, the view was not found.<br />".
290 1 : "View requested was: }%s{</p>", $this->view));
291 1 : }
292 2 : $return = ob_get_contents();
293 2 : ob_end_clean();
294 2 : return $return;
295 : }
296 :
297 :
298 :
299 :
300 :
301 :
302 : public function error_message()
303 : {
304 1 : return $this->debug($this->error_message);
305 : }
306 :
307 :
308 :
309 :
310 :
311 :
312 : public function headers()
313 : {
314 3 : $return = '';
315 3 : foreach($this->css as $css => $value) {
316 3 : if ($value) {
317 2 : $return .= sprintf(" <link rel='stylesheet' ".
318 2 : "href='".WEB_ROOT."/static/%s.css' ".
319 2 : "type='text/css' />\n", $css);
320 2 : }
321 3 : }
322 3 : foreach($this->js as $js => $value) {
323 3 : if ($value) {
324 2 : $return .= sprintf(" <script src='".WEB_ROOT."/static/%s.js' ".
325 2 : "type='text/javascript'></script>\n", $js);
326 2 : }
327 3 : }
328 3 : return $return;
329 : }
330 :
331 :
332 :
333 :
334 :
335 :
336 : public function title()
337 : {
338 2 : return sprintf(" <title>%s</title>\n", $this->title) ;
339 : }
340 :
341 :
342 :
343 :
344 :
345 :
346 :
347 :
348 : public function resolve_route($route)
349 : {
350 6 : if ($route == 'home') {
351 2 : $link = '';
352 2 : } else {
353 4 : $routes = unserialize(ROUTES);
354 4 : $link = array_search($route, $routes);
355 : }
356 6 : if ($link !== False) {
357 4 : $link = sprintf(WEB_ROOT.'/%s', $link);
358 4 : }
359 6 : return $link;
360 : }
361 :
362 :
363 :
364 :
365 :
366 :
367 :
368 :
369 :
370 :
371 :
372 :
373 : public function link_to_route($route, $text='', $class=null, $id=null, $rel=null)
374 : {
375 3 : $link = $this->resolve_route($route);
376 3 : if ($link !== False) {
377 2 : $html = sprintf('<a href="%s"', $link);
378 2 : if ($class) {
379 2 : $html .= sprintf(' class="%s"', $class);
380 2 : }
381 2 : if ($id) {
382 2 : $html .= sprintf(' id="%s"', $id);
383 2 : }
384 2 : if ($rel) {
385 1 : $html .= sprintf(' rel="%s"', $rel);
386 1 : }
387 2 : $html .= sprintf('>%s</a>', $text);
388 2 : return $html;
389 : } else {
390 1 : return $text;
391 : }
392 : }
393 : }
394 :
395 :
396 :
397 :
398 :
399 :
400 : class error404 extends View {
401 : public function build()
402 : {
403 0 : $this->title = 'Page not found';
404 0 : $this->set_HTTP_status_code(404);
405 0 : }
406 : }
407 :
408 :
409 :
410 : class error500 extends View {
411 : public function build()
412 : {
413 0 : $this->title = 'Internal server error';
414 0 : $this->set_HTTP_status_code(500);
415 0 : }
416 : }
417 :
418 :
419 :
420 :
421 : class ygPlugin {
422 : public static function preBuild(View $view)
423 : {
424 1 : }
425 : public static function postBuild(View $view)
426 : {
427 1 : }
428 : }
|