Skip to content
This repository
Newer
Older
100644 331 lines (240 sloc) 8.963 kb
bfc4c5f0 »
2004-02-27 Initial Commit.
1 <?php
2
3 /*
f362a038 »
2005-01-03 copyright update
4 Copyright (C) 2003-2005 UseBB Team
98e8605c »
2004-04-05 Changed project web site URL to http://www.usebb.net.
5 http://www.usebb.net
3f54de48 »
2004-02-27 Added Header keyword to php files.
6
7 $Header$
19338509 »
2004-02-28 Fixed bug 906106 with undefined arrays.
8
bfc4c5f0 »
2004-02-27 Initial Commit.
9 This file is part of UseBB.
7e4335dc »
2004-02-28 Cosmetic changes with spaces and tabs in header of PHP files.
10
19338509 »
2004-02-28 Fixed bug 906106 with undefined arrays.
11 UseBB is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
7e4335dc »
2004-02-28 Cosmetic changes with spaces and tabs in header of PHP files.
15
19338509 »
2004-02-28 Fixed bug 906106 with undefined arrays.
16 UseBB is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with UseBB; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
bfc4c5f0 »
2004-02-27 Initial Commit.
24 */
25
26 //
27 // Die when called directly in browser
28 //
29 if ( !defined('INCLUDED') )
30 exit();
31
32 //
33 // Create the template handlers
34 //
35 class template {
36
37 //
38 // Variables
39 //
ef1394f9 »
2005-06-16 templater fixes
40 var $content_type = '';
41 var $character_encoding = '';
c472f84a »
2005-04-27 Added active topics in RSS format.
42 var $parse_special_templates_only = false;
e514e98e »
2005-06-16 - Major template engine improvements and code cleanups.
43 var $loaded_sections = array();
44 var $templates = array();
45 var $requests = array();
46 var $global_vars = array();
d495bb6f »
2005-06-17 admin start
47 var $raw_contents = array();
e514e98e »
2005-06-16 - Major template engine improvements and code cleanups.
48 var $body = '';
49
50 //
a2cd3b52 »
2005-06-17 removed obsolete parameter
51 // Activate gzip compression if needed, BEFORE doing a session_start()
52 //
53 function template() {
54
55 global $functions;
56
9daa5397 »
2005-06-27 @ini_get
57 if ( !defined('NO_GZIP') && ( $functions->get_config('output_compression') === 2 || $functions->get_config('output_compression') === 3 ) && !@ini_get('zlib.output_compression') )
a2cd3b52 »
2005-06-17 removed obsolete parameter
58 ob_start('ob_gzhandler');
59
60 }
61
62 //
91d048d8 »
2004-12-21 removed duplicate template section loading code with new function
63 // Load a given template section in the template array
64 //
65 function load_section($section) {
fa92bb3f »
2004-03-28 New template_config table + implementation of get_config() in templat…
66
c9b5378d »
2004-11-02 OMG HUGE update. Check Changelog.txt. ;)
67 global $functions;
fa92bb3f »
2004-03-28 New template_config table + implementation of get_config() in templat…
68
91d048d8 »
2004-12-21 removed duplicate template section loading code with new function
69 if ( !in_array($section, $this->loaded_sections) ) {
fa92bb3f »
2004-03-28 New template_config table + implementation of get_config() in templat…
70
91d048d8 »
2004-12-21 removed duplicate template section loading code with new function
71 $templates_file = ROOT_PATH.'templates/'.$functions->get_config('template').'/'.$section.'.tpl.php';
c9b5378d »
2004-11-02 OMG HUGE update. Check Changelog.txt. ;)
72 if ( !file_exists($templates_file) || !is_readable($templates_file) )
78132656 »
2005-04-26 usebb_die() calls replaced by trigger_error().
73 trigger_error('Unable to load '.$section.' templates file for set "'.$functions->get_config('template').'"!');
c9b5378d »
2004-11-02 OMG HUGE update. Check Changelog.txt. ;)
74 else
75 require($templates_file);
fa92bb3f »
2004-03-28 New template_config table + implementation of get_config() in templat…
76
403060ec »
2005-03-18 really split template sections
77 $this->templates[$section] = $templates;
c9b5378d »
2004-11-02 OMG HUGE update. Check Changelog.txt. ;)
78 $this->loaded_sections[] = $section;
79 unset($templates);
fa92bb3f »
2004-03-28 New template_config table + implementation of get_config() in templat…
80
81 }
82
91d048d8 »
2004-12-21 removed duplicate template section loading code with new function
83 }
84
85 //
86 // Get the template config
87 //
88 function get_config($setting) {
89
55f2ecc4 »
2005-04-28 Added ability to hide warnings for configuration and template parser.
90 global $functions;
91
1c7632de »
2005-06-16 templater fixes
92 $this->load_section('global');
93
14cc7d6d »
2005-07-22 num_rows and isset removing
94 if ( array_key_exists($setting, $this->templates['global']['config']) )
403060ec »
2005-03-18 really split template sections
95 return $this->templates['global']['config'][$setting];
55f2ecc4 »
2005-04-28 Added ability to hide warnings for configuration and template parser.
96 elseif ( !$functions->get_config('hide_undefined_template_setting_warnings') )
78132656 »
2005-04-26 usebb_die() calls replaced by trigger_error().
97 trigger_error('The template configuration variable "'.$setting.'" does not exist!');
55f2ecc4 »
2005-04-28 Added ability to hide warnings for configuration and template parser.
98 else
99 return '';
fa92bb3f »
2004-03-28 New template_config table + implementation of get_config() in templat…
100
101 }
102
91d048d8 »
2004-12-21 removed duplicate template section loading code with new function
103 //
104 // Parse a template
105 //
c472f84a »
2005-04-27 Added active topics in RSS format.
106 function parse($name, $section, $variables=array(), $is_special=false) {
bfc4c5f0 »
2004-02-27 Initial Commit.
107
c9b5378d »
2004-11-02 OMG HUGE update. Check Changelog.txt. ;)
108 global $functions;
bfc4c5f0 »
2004-02-27 Initial Commit.
109
c472f84a »
2005-04-27 Added active topics in RSS format.
110 if ( $this->parse_special_templates_only && !$is_special )
111 return;
112
91d048d8 »
2004-12-21 removed duplicate template section loading code with new function
113 //
114 // Load the template set
115 //
116 $this->load_section($section);
bfc4c5f0 »
2004-02-27 Initial Commit.
117
55f2ecc4 »
2005-04-28 Added ability to hide warnings for configuration and template parser.
118 if ( !array_key_exists($name, $this->templates[$section]) ) {
119
120 if ( !$functions->get_config('hide_undefined_template_warnings') )
121 trigger_error('Unable to load "'.$name.'" template in '.$section.' templates file for set "'.$functions->get_config('template').'"!');
122 else
123 $this->templates[$section][$name] = '';
124
125 }
c9b5378d »
2004-11-02 OMG HUGE update. Check Changelog.txt. ;)
126
e4e39295 »
2005-03-12 better template parser - nicer debug info
127 $this->requests[] = array(
403060ec »
2005-03-18 really split template sections
128 'section' => $section,
e4e39295 »
2005-03-12 better template parser - nicer debug info
129 'template_name' => $name,
130 'variables' => ( is_array($variables) && count($variables) ) ? $variables : array()
131 );
132
133 }
134
135 //
136 // Add global template variables
137 //
12759f22 »
2005-05-20 Fixed bug which keeps 'board closed' in location bar for admins.
138 function add_global_vars($variables, $override=false) {
e4e39295 »
2005-03-12 better template parser - nicer debug info
139
9cfd1048 »
2005-03-19 location_bar is in global templates
140 foreach ( $variables as $key => $val ) {
6017b650 »
2004-11-02 Check if template variable is not an array.
141
12759f22 »
2005-05-20 Fixed bug which keeps 'board closed' in location bar for admins.
142 if ( $override || !array_key_exists($key, $this->global_vars) )
9cfd1048 »
2005-03-19 location_bar is in global templates
143 $this->global_vars[$key] = $val;
6017b650 »
2004-11-02 Check if template variable is not an array.
144
145 }
bfc4c5f0 »
2004-02-27 Initial Commit.
146
147 }
148
91d048d8 »
2004-12-21 removed duplicate template section loading code with new function
149 //
150 // Set the page title
151 //
e4e39295 »
2005-03-12 better template parser - nicer debug info
152 function set_page_title($page_title) {
bfc4c5f0 »
2004-02-27 Initial Commit.
153
9cfd1048 »
2005-03-19 location_bar is in global templates
154 global $functions;
155
156 $this->add_global_vars(array(
157 'page_title' => strip_tags($page_title),
cc1ae2c7 »
2005-04-03 removed buttons
158 'location_bar' => '<a href="'.$functions->make_url('index.php').'">'.unhtml($functions->get_config('board_name')).'</a>'.$this->get_config('locationbar_item_delimiter').$page_title
12759f22 »
2005-05-20 Fixed bug which keeps 'board closed' in location bar for admins.
159 ), true);
bfc4c5f0 »
2004-02-27 Initial Commit.
160
161 }
162
91d048d8 »
2004-12-21 removed duplicate template section loading code with new function
163 //
d495bb6f »
2005-06-17 admin start
164 // Add raw content, used for the ACP
165 //
166 function add_raw_content($content) {
167
168 $this->requests[] = array(
169 'raw' => true,
170 'num' => count($this->raw_contents)
171 );
172
173 $this->raw_contents[] = $content;
174
175 }
176
177 //
194a4d90 »
2005-06-20 Added auto focus feature using improved template engine.
178 // Set the Javascript onload statement
179 //
180 function set_js_onload($statement) {
181
182 $this->add_global_vars(array(
183 'js_onload' => ' onload="javascript:'.$statement.'"'
184 ), true);
185
186 }
187
188 //
91d048d8 »
2004-12-21 removed duplicate template section loading code with new function
189 // Output the page body
190 //
a2cd3b52 »
2005-06-17 removed obsolete parameter
191 function body() {
bfc4c5f0 »
2004-02-27 Initial Commit.
192
d495bb6f »
2005-06-17 admin start
193 global $db, $functions, $timer, $lang, $session;
bfc4c5f0 »
2004-02-27 Initial Commit.
194
195 //
1c7632de »
2005-06-16 templater fixes
196 // Eventually set the content type and charset
197 //
198 $content_type = $this->get_config('content_type');
ef1394f9 »
2005-06-16 templater fixes
199 if ( empty($this->content_type) )
200 $this->content_type = ( !empty($content_type) ) ? $content_type : 'text/html';
201 if ( empty($this->character_encoding) )
202 $this->character_encoding = ( !empty($lang['character_encoding']) ) ? $lang['character_encoding'] : 'iso-8859-1';
1c7632de »
2005-06-16 templater fixes
203
204 //
e514e98e »
2005-06-16 - Major template engine improvements and code cleanups.
205 // Set content type and charset
206 //
207 header('Content-Type: '.$this->content_type.'; charset='.$this->character_encoding);
208
209 //
bfc4c5f0 »
2004-02-27 Initial Commit.
210 // Debug features
211 //
a2cd3b52 »
2005-06-17 removed obsolete parameter
212 if ( $functions->get_config('debug') ) {
bfc4c5f0 »
2004-02-27 Initial Commit.
213
214 //
215 // Timer for checking parsetime
216 //
217 $timer['end'] = explode(' ', microtime());
218 $timer['end'] = (float)$timer['end'][1] + (float)$timer['end'][0];
7c41fdec »
2005-06-22 changed rounding of parse time
219 $parsetime = round($timer['end'] - $timer['begin'], 4);
bfc4c5f0 »
2004-02-27 Initial Commit.
220
9c782bac »
2005-03-14 Cleaned up debug message creation and hide server load on Windows etc.
221 $debug_info = array();
222 $debug_info[] = $lang['ParseTime'].': '.$parsetime.' s';
223 if ( $serverload = $functions->get_server_load() )
224 $debug_info[] = $lang['ServerLoad'].': '.$serverload;
225 $debug_info[] = $lang['TemplateSections'].': '.count($this->loaded_sections);
226 $debug_info[] = $lang['SQLQueries'].': '.count($db->get_used_queries());
76731fc6 »
2004-08-24 Server load in template parser moved to function class.
227
7ceccd68 »
2004-11-03 Added feature 1055684: Hide avatars/signatures setting.
228 if ( $functions->get_config('debug') === 1 ) {
378f9303 »
2004-04-04 Debug now has short (1) and full (2) level.
229
230 //
231 // List parsetime and queries in short
232 //
cc1ae2c7 »
2005-04-03 removed buttons
233 $debug_info_small = sprintf($this->get_config('debug_info_small'), join($this->get_config('item_delimiter'), $debug_info));
cfd5c20e »
2005-03-12 debug - stupid bugs :p
234 $debug_info_large = '';
ea42fc9d »
2004-04-04 Implemented timezone and DST handling.
235
7ceccd68 »
2004-11-03 Added feature 1055684: Hide avatars/signatures setting.
236 } elseif ( $functions->get_config('debug') === 2 ) {
378f9303 »
2004-04-04 Debug now has short (1) and full (2) level.
237
238 //
239 // Lists parsetime and queries fully
240 //
cfd5c20e »
2005-03-12 debug - stupid bugs :p
241 $debug_info_small = '';
cc1ae2c7 »
2005-04-03 removed buttons
242 $debug_info_large = sprintf($this->get_config('debug_info_large'), '<div><strong>'.$lang['DebugMode'].'</strong>'.$this->get_config('item_delimiter').join($this->get_config('item_delimiter'), $debug_info).':</div><textarea rows="10" cols="50" readonly="readonly">'.unhtml(join("\n\n", $db->get_used_queries())).'</textarea>');
ea42fc9d »
2004-04-04 Implemented timezone and DST handling.
243
378f9303 »
2004-04-04 Debug now has short (1) and full (2) level.
244 }
bfc4c5f0 »
2004-02-27 Initial Commit.
245
cfd5c20e »
2005-03-12 debug - stupid bugs :p
246 } else {
247
248 $debug_info_small = '';
249 $debug_info_large = '';
250
e4e39295 »
2005-03-12 better template parser - nicer debug info
251 }
cfd5c20e »
2005-03-12 debug - stupid bugs :p
252 $this->add_global_vars(array(
253 'debug_info_small' => $debug_info_small,
254 'debug_info_large' => $debug_info_large
255 ));
e4e39295 »
2005-03-12 better template parser - nicer debug info
256
257 //
e514e98e »
2005-06-16 - Major template engine improvements and code cleanups.
258 // Add some global template variables such as content type and charset
259 //
260 $this->add_global_vars(array(
261 'content_type' => $this->content_type,
262 'character_encoding' => $this->character_encoding,
263 'language_code' => ( !empty($lang['language_code']) ) ? $lang['language_code'] : 'en',
264 'text_direction' => ( !empty($lang['text_direction']) ) ? $lang['text_direction'] : 'ltr',
265 'img_dir' => ROOT_PATH.'templates/'.$functions->get_config('template').'/gfx/',
d495bb6f »
2005-06-17 admin start
266 'css_url' => ROOT_PATH.'templates/'.$functions->get_config('template').'/styles.css',
267 'acp_css_head_link' => ( $session->sess_info['location'] == 'admin' ) ? '<link rel="stylesheet" type="text/css" href="'.ROOT_PATH.'templates/'.$functions->get_config('template').'/admin.css" />' : '',
194a4d90 »
2005-06-20 Added auto focus feature using improved template engine.
268 'js_onload' => ''
e514e98e »
2005-06-16 - Major template engine improvements and code cleanups.
269 ));
270
271 //
e4e39295 »
2005-03-12 better template parser - nicer debug info
272 // Parse all templates
273 //
274 foreach ( $this->requests as $request ) {
275
d495bb6f »
2005-06-17 admin start
276 if ( isset($request['raw']) ) {
277
278 $this->body .= "\n".$this->raw_contents[$request['num']]."\n";
279 continue;
280
281 }
282
403060ec »
2005-03-18 really split template sections
283 $current_template = $this->templates[$request['section']][$request['template_name']];
86467f2e »
2005-04-23 some template parser improvements
284 $finds = $replaces = array();
3f920a16 »
2005-06-15 CSS has now been detached from the templating system
285
e692af1a »
2005-03-18 language vars prior to system vars
286 if ( preg_match('#\{l_[a-zA-Z]+\}#', $current_template) ) {
287
279efccc »
2005-03-24 Added feature #370: Date and Time Translation.
288 foreach ( $lang as $key => $val ) {
289
86467f2e »
2005-04-23 some template parser improvements
290 if ( !is_array($val) ) {
291
292 $finds[] = '#\{l_'.preg_quote($key, '#').'\}#';
293 $replaces[] = $val;
294
295 }
279efccc »
2005-03-24 Added feature #370: Date and Time Translation.
296
297 }
e692af1a »
2005-03-18 language vars prior to system vars
298
299 }
3f920a16 »
2005-06-15 CSS has now been detached from the templating system
300
9cfd1048 »
2005-03-19 location_bar is in global templates
301 $request['variables'] = array_merge($this->global_vars, $request['variables']);
3f920a16 »
2005-06-15 CSS has now been detached from the templating system
302
86467f2e »
2005-04-23 some template parser improvements
303 foreach ( $request['variables'] as $key => $val ) {
304
305 $finds[] = '#\{'.preg_quote($key, '#').'\}#';
306 $replaces[] = $val;
307
308 }
3f920a16 »
2005-06-15 CSS has now been detached from the templating system
309
310 foreach ( $replaces as $key => $val )
83ec348c »
2005-04-23 some template parser improvements
311 $replaces[$key] = preg_replace(array('#\{#', '#\}#'), array('&#123;', '&#125;'), $val);
3f920a16 »
2005-06-15 CSS has now been detached from the templating system
312
86467f2e »
2005-04-23 some template parser improvements
313 $current_template = preg_replace($finds, $replaces, $current_template);
e4e39295 »
2005-03-12 better template parser - nicer debug info
314 $this->body .= $current_template;
51d90fad »
2004-04-13 Optimalizations in template.php, debug also works without </body> tag…
315
bfc4c5f0 »
2004-02-27 Initial Commit.
316 }
317
318 //
a82bd90e »
2004-04-25 Multiple levels of output compression: none, compress whitespace, gzi…
319 // Compression and output
bfc4c5f0 »
2004-02-27 Initial Commit.
320 //
7ceccd68 »
2004-11-03 Added feature 1055684: Hide avatars/signatures setting.
321 if ( $functions->get_config('output_compression') === 1 || $functions->get_config('output_compression') === 3 )
c9b5378d »
2004-11-02 OMG HUGE update. Check Changelog.txt. ;)
322 $this->body = $functions->compress_sourcecode($this->body);
bfc4c5f0 »
2004-02-27 Initial Commit.
323
c9b5378d »
2004-11-02 OMG HUGE update. Check Changelog.txt. ;)
324 echo $this->body;
d95e270b »
2004-08-22 Fixed bug 997622: Source code compression strips newlines out of text…
325
bfc4c5f0 »
2004-02-27 Initial Commit.
326 }
327
328 }
329
378f9303 »
2004-04-04 Debug now has short (1) and full (2) level.
330 ?>
Something went wrong with that request. Please try again.