summaryrefslogtreecommitdiffstats
path: root/inc/render.inc.php
blob: 1f311868005f197ebcb5e8e9449ccfd660b598e2 (plain) (blame)
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php

define('RENDER_DEFAULT_TITLE', 'OpenSLX Admin');

require_once('inc/util.inc.php');

require_once('Mustache/Autoloader.php');
Mustache_Autoloader::register();

/**
 * HTML rendering helper class
 */
Render::init();

class Render
{

	private static $mustache = false;
	private static $body = '';
	private static $header = '';
	private static $dashboard = false;
	private static $footer = '';
	private static $title = '';
	private static $templateCache = array();
	private static $tags = array();

	public static function init()
	{
		if (self::$mustache !== false)
			Util::traceError('Called Render::init() twice!');
		$options = array();
		$tmp = '/tmp/bwlp-cache';
		$dir = is_dir($tmp);
		if (!$dir) {
			@mkdir($tmp, 0755, false);
		}
		if (($dir || is_dir($tmp)) && is_writable($tmp)) {
			$options['cache'] = $tmp;
		}
		self::$mustache = new Mustache_Engine($options);
	}

	/**
	 * Output the buffered, generated page
	 */
	public static function output()
	{
		Header('Content-Type: text/html; charset=utf-8');
		$modules = array_reverse(Module::getActivated());
		ob_start('ob_gzhandler');
		echo
		'<!DOCTYPE html>
	<html>
		<head>
			<title>', self::$title, RENDER_DEFAULT_TITLE, '</title>
			<meta charset="utf-8"> 
			<meta http-equiv="X-UA-Compatible" content="IE=edge">
			<meta name="viewport" content="width=device-width, initial-scale=1.0">
			<!-- Bootstrap -->
			<link href="style/bootstrap.min.css" rel="stylesheet" media="screen">
	';
		// Include any module specific styles
		foreach ($modules as $module) {
			$file = $module->getDir() . '/style.css';
			if (file_exists($file)) {
				echo '<link href="', $file, '" rel="stylesheet" media="screen">';
			}
		}
		echo '	
			<link href="style/default.css" rel="stylesheet" media="screen">
			<script type="text/javascript">
			var TOKEN = "' . Session::get('token') . '";
			var LANG = "'  . LANG . '";
			</script>
	',
		self::$header
		,
		'	</head>
		<body>
	',
		(self::$dashboard !== false ? self::parse('main-menu', self::$dashboard, 'main') : ''),
		'<div class="main" id="mainpage"><div class="container-fluid">
	',
		self::$body
		,
		'</div></div>
		<script src="script/jquery.js"></script>
		<script src="script/bootstrap.min.js"></script>
		<script src="script/taskmanager.js"></script>
		<script src="script/fileselect.js"></script>
	';
		foreach ($modules as $module) {
			$file = $module->getDir() . '/clientscript.js';
			if (file_exists($file)) {
				echo '<script src="', $file, '"></script>';
			}
		}
		echo
		self::$footer
		,
		'</body>
		</html>'
		;
		ob_end_flush();
	}

	/**
	 * Set the page title (title-tag)
	 */
	public static function setTitle($title)
	{
		self::$title = $title . ' - ';
	}

	/**
	 * Add raw html data to the header-section of the generated page
	 */
	public static function addHeader($html)
	{
		self::$header .= $html . "\n";
	}

	/**
	 * Add raw html data to the footer-section of the generated page (right before the closing body tag)
	 */
	public static function addFooter($html)
	{
		self::$footer .= $html . "\n";
	}

	/**
	 * Add given js script file from the script directory to the header
	 *
	 * @param string $file file name of script
	 */
	public static function addScriptTop($file)
	{
		trigger_error('Ignoring addScriptTop for ' . $file . ': Deprecated, use module-specific clientscript.js', E_USER_WARNING);
	}

	/**
	 * Add given js script file from the script directory to the bottom
	 *
	 * @param string $file file name of script
	 */
	public static function addScriptBottom($file)
	{
		trigger_error('Ignoring addScriptBottom for ' . $file . ': Deprecated, use module-specific clientscript.js', E_USER_WARNING);
	}

	/**
	 * Add the given template to the output, using the given params for placeholders in the template
	 */
	public static function addTemplate($template, $params = false, $module = false)
	{
		self::$body .= self::parse($template, $params, $module);
	}

	/**
	 * Add a dialog to the page output.
	 * 
	 * @param string $title Title of the dialog window
	 * @param boolean $next URL to next dialog step, or false to hide the next button
	 * @param string $template template used to fill the dialog body
	 * @param array $params parameters for rendering the body template
	 */
	public static function addDialog($title, $next, $template, $params = false)
	{
		self::addTemplate('dialog-generic', array(
			'title' => $title,
			'next' => $next,
			'body' => self::parse($template, $params)
		), 'main');
	}

	/**
	 * Add error message to page
	 */
	public static function addError($message)
	{
		self::addTemplate('messagebox-error', array('message' => $message));
	}

	/**
	 * Parse template with given params and return; do not add to body
	 * @param string $template name of template, relative to templates/, without .html extension
	 * @param array $params tags to render into template
	 * @param string $module name of module to load template from; defaults to currently active module
	 * @return string Rendered template
	 */
	public static function parse($template, $params = false, $module = false)
	{
		if ($module === false) {
			$module = Page::getModule()->getIdentifier();
		}
		// Load html snippet
		$html = self::getTemplate($template, $module);
		if ($html === false) {
			return '<h3>Template ' . htmlspecialchars($template) . '</h3>' . nl2br(htmlspecialchars(print_r($params, true))) . '<hr>';
		}
		if (!is_array($params)) {
			$params = array();
		}
		// Now find all language tags in this array
		if (preg_match_all('/{{(lang_.+?)}}/', $html, $out) > 0) {
			$dictionary = Dictionary::getArray($module, 'template-tags');
			$fallback = false;
			foreach ($out[1] as $tag) {
				// Add untranslated strings to the dictionary, so their tag is seen in the rendered page
				if ($fallback === false && empty($dictionary[$tag])) {
					$fallback = true; // Fallback to general dictionary of module
					$dictionary = $dictionary + Dictionary::getArray('main', 'global-tags');
				}
				if (empty($dictionary[$tag])) {
					$dictionary[$tag] = '{{' . $tag . '}}';
				}
			}
			$params = $params + $dictionary;
		}
		// Always add token to parameter list
		$params['token'] = Session::get('token');
		if (defined('LANG')) {
			// Likewise, add currently selected language (its two letter code) to params
			$params['current_lang'] = LANG;
		}
		// Add desired password field type
		$params['password_type'] = Property::getPasswordFieldType();
		// Return rendered html
		return self::$mustache->render($html, $params);
	}

	/**
	 * Open the given html tag, optionally adding the passed assoc array of params
	 */
	public static function openTag($tag, $params = false)
	{
		array_push(self::$tags, $tag);
		if (!is_array($params)) {
			self::$body .= '<' . $tag . '>';
		} else {
			self::$body .= '<' . $tag;
			foreach ($params as $key => $val) {
				self::$body .= ' ' . $key . '="' . htmlspecialchars($val) . '"';
			}
			self::$body .= '>';
		}
	}

	/**
	 * Close the given tag. Will check if it maches the tag last opened
	 */
	public static function closeTag($tag)
	{
		if (empty(self::$tags))
			Util::traceError('Tried to close tag ' . $tag . ' when no open tags exist.');
		$last = array_pop(self::$tags);
		if ($last !== $tag)
			Util::traceError('Tried to close tag ' . $tag . ' when last opened tag was ' . $last);
		self::$body .= '</' . $tag . '>';
	}

	/**
	 * Private helper: Load the given template and return it
	 */
	private static function getTemplate($template, $module)
	{
		$id = "$template/$module";
		if (isset(self::$templateCache[$id])) {
			return self::$templateCache[$id];
		}
		// Load from disk
		$data = @file_get_contents('modules/' . $module . '/templates/' . $template . '.html');
		if ($data === false) {
			$data = '<b>Non-existent/unreadable template ' . $template . ' requested!</b>';
		}
		self::$templateCache[$id] =& $data;
		return $data;
	}

	/**
	 * Create the dashboard menu
	 */
	public static function setDashboard($params)
	{
		self::$dashboard = $params;
	}

}