File: Renderer.php

package info (click to toggle)
bamboo 1.2-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 580 kB
  • ctags: 1,338
  • sloc: php: 4,061; makefile: 44; sh: 36
file content (247 lines) | stat: -rw-r--r-- 6,397 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php

/****
 *
 * Renderer Class
 * Controls rendering of a page, static or dynamic.
 * 
 ****/	

require_once('Properties.php');
require_once('Page.php');

class Renderer {

var $prop; # the default decorator for the site.

# predefined types and filters: the order matters.
var $filters = array(
	'txt' => array('text2html','symbols'),
	'raw' => array('pre'),
	'html' => array('symbols'),
	'mod' => '',
);

function Renderer(&$prop) {
	$this->prop = &$prop;
}

function displayError($title, $contents) {
	$content = "<font color=red>Error:</font><br>$contents";
	$this->display($title, $content);
}

function display($title, &$content) {
	global $root, $decoratorurl, $nav, $ps, $robot, $static, $page;
	
	require_once(dirname(__FILE__) . '/decorators/common.php');
	$title = $this->prop->getGlobal('title-prefix') . $title;
	
	$decor = $this->getDecorator();
	$decoratorurl = $decor->url;
	if (is_file($decor->template)) {
		$nav_hide = true;
		include($decor->template);
	}
	else {
		echo $content;
	}
}

function renderPage(&$page,$printable=false,$content='',$skipfilters=false) {
	global $root, $decoratorurl, $nav, $ps, $robot, $static;
	
	require_once(dirname(__FILE__) . '/decorators/common.php');

	$decor = $this->getDecorator($page->get('decorator'));
	if ($content == '') {
		$content = & $this->getRenderedContent($page);
		if ($page->type == 'php') {
			$content = & $this->exec_php_file($page->phpfile);
		}
		
		# if content empty, return default index.
		if ($content == '') {
			if (is_file($decor->index))
				$content = $this->exec_php_file($decor->index);
			else
				$content = $this->indexinfo($page);
		}
	}

	$title   = $page->get('title');
	$title   = $this->prop->getGlobal('title-prefix') . $title;

	if ($page->fnf) {
		$this->displayError("File not found", "That file does not exist.");
		return;	
	}

	$allowed = explode(' ',$this->prop->getGlobal('allowed-types'));
	if (!in_array($page->type,$allowed)) {
		$this->displayError(_("Not allowed"), _("Cannot display page. The following type is not allowed: ") . $page->type);
		return;	
	}
	
	$decoratorurl = $decor->url;
			
	if (!$printable && is_file($decor->template)) {
		# standard template
		header("Last-Modified: " . gmdate("D, d M Y H:i:s", $page->get('mtime')) . " GMT");
		include($decor->template);
	}
	else {
		# page without any template
		echo "<html>\n<head>\n<title>$title</title>\n</head>\n<body>\n";
		echo $content;
		echo "\n</body>\n</html>";
	}
}

function &exec_php_file($phpfile) {
	if ($phpfile == '') return;

	ob_start();
	include($phpfile);
  	$data = &ob_get_contents();
	ob_end_clean();
	return $data;
}

function &getRenderedContent(&$page) {
	$content = $page->get('content');
	$type = $page->get('type');
	ob_start();
	if (isset($this->filters[$type])) {
		foreach($this->filters[$type] as $f) {
			$this->applyFilter($f, $content, $page);
		}	
	}
	echo $content;
	$data = &ob_get_contents();
	ob_end_clean();
	return $data;
}

/**
 style handler
 This function is called on ?action=style requests.
 it dumps the correct css file, setting the headers correctly
 for content type and caching. with d=<decoratorname> in the url,
 you can override the decorator property of the given page.
 **/
function style(&$page) {
	global $root, $decoratorurl, $nav, $ps, $robot, $static;

	// send mime type, this is a css file.
	header('Content-type: text/css');
	
	// send cache headers. we want the style page cached.
	$offset = 60*60*3; // 3 hours
	$expires = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
	header("Cache-Control: public");
	header($expires);
	
	// load the decorator
	require_once(dirname(__FILE__) . '/decorators/common.php');
	$decorator_name = get('d',$page->get('decorator'));
	$decor = $this->getDecorator($decorator_name);
	$decoratorurl = $decor->url;

	// set last modified
	$lastmod = "Last-Modified: " . gmdate("D, d M Y H:i:s", filemtime($decor->style)) . " GMT";
	header($lastmod);

	// run style.php
	include($decor->style);
}

function applyFilter($filter, &$content, &$page) {
	require_once("filters/$filter.php");
	$filter_object = new $filter();
	$filter_object->apply($content,$page);
}

function getDecorator($name = '') {
	$d = new Decorator();
	if ($name == '')
		$d->name = $this->prop->getGlobal('decorator');
	else
		$d->name = $name;
		
	// try possible dirs, until we find a decorator
	// this is kinda silly, we should just decide
	// where it must go.
	
	// map url => directory
	$docroot = $this->prop->getGlobal('sitedocroot');
	$urlroot = $this->prop->getGlobal('siteroot');
	$libfile = dirname(__FILE__);
	$liburl  = $this->prop->getGlobal('liburl');
	$candidates = array(
		"$urlroot/.decorators/$d->name"  => "$docroot/.decorators/$d->name",
		"$urlroot/$d->name"  => "$docroot/$d->name",
		"$liburl/decorators/$d->name"  => "$libfile/decorators/$d->name",
	);
	foreach($candidates as $url => $dir) {
		if (is_dir($dir)) break;
	}
		 
	$d->url = $url;
	$d->template = "$dir/template.php";
	$d->style = "$dir/style.php";
	if (file_exists("$dir/index.php"))
		$d->index = "$dir/index.php";
	$d->error = "$dir/error.php";
	
	return $d;
}

function displayFnf(&$page) {
	$d = $this->getDecorator();
	
	$fnfpath = $page->path;

	while($page->fnf) {
		$page = $page->parent();
	}
	$path = $page->path ? $page->path : '/';
	$newpagelink = alink($path . "?a=edit&ea=newedit&path=" . urlencode($fnfpath), _("Create this page"));
	
	if (is_file($d->error)) {
		ob_start();
		include($d->error);
		$content = &ob_get_contents();
		ob_end_clean();
	}
	else {
		$content = "<p>404 Page not found</p><p>The web page you seek<br />Cannot be located, but<br />Countless more exist.</p>";
		$content .= $newpagelink;
	}
	$this->display("File not found", $content);
}

function indexinfo(&$page) {
	$children = $page->children();
	foreach($children as $child) {
		$ret .= "<dl class=index>\n";
		$ret .= "<dt>" . alink($child->path .'/',$child->get('title')) . "</dt>\n";
		$ret .= "<dd>" . $child->get('description') . "</dd>\n";
		$ret .= "</dl>\n";
	}
	return $ret;
}


} // end class

class Decorator {
	var $index;
	var $style;
	var $template;
	var $name;
	var $url;	
}

return;
?>