phpBB
Statistics
| Revision:

root / branches / phpBB-3_0_0 / phpBB / includes / template.php

History | View | Annotate | Download (18.4 kB)

1 741 psotfx
<?php
2 8131 acydburn
/**
3 5114 acydburn
*
4 5114 acydburn
* @package phpBB3
5 5114 acydburn
* @version $Id$
6 5238 acydburn
* @copyright (c) 2005 phpBB Group, sections (c) 2001 ispi of Lincoln Inc
7 8131 acydburn
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
8 5114 acydburn
*
9 5114 acydburn
*/
10 741 psotfx
11 5114 acydburn
/**
12 8131 acydburn
* @ignore
13 5670 acydburn
*/
14 5670 acydburn
if (!defined('IN_PHPBB'))
15 5670 acydburn
{
16 5670 acydburn
        exit;
17 5670 acydburn
}
18 5670 acydburn
19 5670 acydburn
/**
20 6058 acydburn
* Base Template class.
21 5114 acydburn
* @package phpBB3
22 2662 psotfx
*/
23 4235 psotfx
class template
24 3928 psotfx
{
25 5865 acydburn
        /** variable that holds all the data we'll be substituting into
26 5865 acydburn
        * the compiled templates. Takes form:
27 6796 davidmj
        * --> $this->_tpldata[block][iteration#][child][iteration#][child2][iteration#][variablename] == value
28 5865 acydburn
        * if it's a root-level variable, it'll be like this:
29 5865 acydburn
        * --> $this->_tpldata[.][0][varname] == value
30 5865 acydburn
        */
31 6796 davidmj
        var $_tpldata = array('.' => array(0 => array()));
32 6796 davidmj
        var $_rootref;
33 943 thefinn
34 2662 psotfx
        // Root dir and hash of filenames for each template handle.
35 2662 psotfx
        var $root = '';
36 4273 psotfx
        var $cachepath = '';
37 741 psotfx
        var $files = array();
38 7853 davidmj
        var $filename = array();
39 8697 Kellanved
        var $files_inherit = array();
40 8697 Kellanved
        var $files_template = array();
41 8697 Kellanved
        var $inherit_root = '';
42 9847 toonarmy
        var $orig_tpl_storedb;
43 9847 toonarmy
        var $orig_tpl_inherits_id;
44 741 psotfx
45 2662 psotfx
        // this will hash handle names to the compiled/uncompiled code for that handle.
46 741 psotfx
        var $compiled_code = array();
47 943 thefinn
48 5319 acydburn
        /**
49 5319 acydburn
        * Set template location
50 6312 acydburn
        * @access public
51 5319 acydburn
        */
52 5865 acydburn
        function set_template()
53 741 psotfx
        {
54 6366 acydburn
                global $phpbb_root_path, $user;
55 3662 psotfx
56 5372 acydburn
                if (file_exists($phpbb_root_path . 'styles/' . $user->theme['template_path'] . '/template'))
57 4235 psotfx
                {
58 5783 acydburn
                        $this->root = $phpbb_root_path . 'styles/' . $user->theme['template_path'] . '/template';
59 8943 acydburn
                        $this->cachepath = $phpbb_root_path . 'cache/tpl_' . str_replace('_', '-', $user->theme['template_path']) . '_';
60 9847 toonarmy
61 9847 toonarmy
                        if ($this->orig_tpl_storedb === null)
62 9839 nickvergessen
                        {
63 9839 nickvergessen
                                $this->orig_tpl_storedb = $user->theme['template_storedb'];
64 9839 nickvergessen
                        }
65 9847 toonarmy
66 9847 toonarmy
                        if ($this->orig_tpl_inherits_id === null)
67 9839 nickvergessen
                        {
68 9839 nickvergessen
                                $this->orig_tpl_inherits_id = $user->theme['template_inherits_id'];
69 9839 nickvergessen
                        }
70 9847 toonarmy
71 9839 nickvergessen
                        $user->theme['template_storedb'] = $this->orig_tpl_storedb;
72 9839 nickvergessen
                        $user->theme['template_inherits_id'] = $this->orig_tpl_inherits_id;
73 9633 nickvergessen
74 8697 Kellanved
                        if ($user->theme['template_inherits_id'])
75 8697 Kellanved
                        {
76 8697 Kellanved
                                $this->inherit_root = $phpbb_root_path . 'styles/' . $user->theme['template_inherit_path'] . '/template';
77 8697 Kellanved
                        }
78 4235 psotfx
                }
79 6112 acydburn
                else
80 6112 acydburn
                {
81 6112 acydburn
                        trigger_error('Template path could not be found: styles/' . $user->theme['template_path'] . '/template', E_USER_ERROR);
82 6112 acydburn
                }
83 741 psotfx
84 6796 davidmj
                $this->_rootref = &$this->_tpldata['.'][0];
85 6796 davidmj
86 741 psotfx
                return true;
87 741 psotfx
        }
88 741 psotfx
89 5319 acydburn
        /**
90 5319 acydburn
        * Set custom template location (able to use directory outside of phpBB)
91 6312 acydburn
        * @access public
92 5319 acydburn
        */
93 10460 naderman
        function set_custom_template($template_path, $template_name, $fallback_template_path = false)
94 5268 acydburn
        {
95 9839 nickvergessen
                global $phpbb_root_path, $user;
96 5268 acydburn
97 10036 acydburn
                // Make sure $template_path has no ending slash
98 10036 acydburn
                if (substr($template_path, -1) == '/')
99 10036 acydburn
                {
100 10036 acydburn
                        $template_path = substr($template_path, 0, -1);
101 10036 acydburn
                }
102 10036 acydburn
103 5268 acydburn
                $this->root = $template_path;
104 8943 acydburn
                $this->cachepath = $phpbb_root_path . 'cache/ctpl_' . str_replace('_', '-', $template_name) . '_';
105 6048 acydburn
106 10460 naderman
                if ($fallback_template_path !== false)
107 10150 nickvergessen
                {
108 10460 naderman
                        if (substr($fallback_template_path, -1) == '/')
109 10460 naderman
                        {
110 10460 naderman
                                $fallback_template_path = substr($fallback_template_path, 0, -1);
111 10460 naderman
                        }
112 10460 naderman
113 10460 naderman
                        $this->inherit_root = $fallback_template_path;
114 10460 naderman
                        $this->orig_tpl_inherits_id = true;
115 10150 nickvergessen
                }
116 10460 naderman
                else
117 10460 naderman
                {
118 10460 naderman
                        $this->orig_tpl_inherits_id = false;
119 10460 naderman
                }
120 10150 nickvergessen
121 10460 naderman
                // the database does not store the path or name of a custom template
122 10460 naderman
                // so there is no way we can properly store custom templates there
123 10460 naderman
                $this->orig_tpl_storedb = false;
124 10460 naderman
125 9811 acydburn
                $this->_rootref = &$this->_tpldata['.'][0];
126 9811 acydburn
127 5268 acydburn
                return true;
128 5268 acydburn
        }
129 5268 acydburn
130 5319 acydburn
        /**
131 5319 acydburn
        * Sets the template filenames for handles. $filename_array
132 5319 acydburn
        * should be a hash of handle => filename pairs.
133 6312 acydburn
        * @access public
134 5319 acydburn
        */
135 943 thefinn
        function set_filenames($filename_array)
136 741 psotfx
        {
137 3016 psotfx
                if (!is_array($filename_array))
138 741 psotfx
                {
139 943 thefinn
                        return false;
140 741 psotfx
                }
141 3016 psotfx
                foreach ($filename_array as $handle => $filename)
142 741 psotfx
                {
143 3016 psotfx
                        if (empty($filename))
144 2662 psotfx
                        {
145 5865 acydburn
                                trigger_error("template->set_filenames: Empty filename specified for $handle", E_USER_ERROR);
146 2662 psotfx
                        }
147 2662 psotfx
148 2642 psotfx
                        $this->filename[$handle] = $filename;
149 4235 psotfx
                        $this->files[$handle] = $this->root . '/' . $filename;
150 9633 nickvergessen
151 8697 Kellanved
                        if ($this->inherit_root)
152 8697 Kellanved
                        {
153 8697 Kellanved
                                $this->files_inherit[$handle] = $this->inherit_root . '/' . $filename;
154 8697 Kellanved
                        }
155 741 psotfx
                }
156 9633 nickvergessen
157 741 psotfx
                return true;
158 741 psotfx
        }
159 741 psotfx
160 5319 acydburn
        /**
161 5319 acydburn
        * Destroy template data set
162 6312 acydburn
        * @access public
163 5319 acydburn
        */
164 2662 psotfx
        function destroy()
165 2662 psotfx
        {
166 6796 davidmj
                $this->_tpldata = array('.' => array(0 => array()));
167 10217 acydburn
                $this->_rootref = &$this->_tpldata['.'][0];
168 2662 psotfx
        }
169 2662 psotfx
170 5319 acydburn
        /**
171 5965 acydburn
        * Reset/empty complete block
172 6312 acydburn
        * @access public
173 5965 acydburn
        */
174 5965 acydburn
        function destroy_block_vars($blockname)
175 5965 acydburn
        {
176 5965 acydburn
                if (strpos($blockname, '.') !== false)
177 5965 acydburn
                {
178 5965 acydburn
                        // Nested block.
179 5965 acydburn
                        $blocks = explode('.', $blockname);
180 5965 acydburn
                        $blockcount = sizeof($blocks) - 1;
181 5965 acydburn
182 5965 acydburn
                        $str = &$this->_tpldata;
183 5965 acydburn
                        for ($i = 0; $i < $blockcount; $i++)
184 5965 acydburn
                        {
185 5965 acydburn
                                $str = &$str[$blocks[$i]];
186 5965 acydburn
                                $str = &$str[sizeof($str) - 1];
187 5965 acydburn
                        }
188 5965 acydburn
189 5965 acydburn
                        unset($str[$blocks[$blockcount]]);
190 5965 acydburn
                }
191 5965 acydburn
                else
192 5965 acydburn
                {
193 5965 acydburn
                        // Top-level block.
194 5965 acydburn
                        unset($this->_tpldata[$blockname]);
195 5965 acydburn
                }
196 5965 acydburn
197 5965 acydburn
                return true;
198 5965 acydburn
        }
199 5965 acydburn
200 5965 acydburn
        /**
201 5319 acydburn
        * Display handle
202 6312 acydburn
        * @access public
203 5319 acydburn
        */
204 4740 acydburn
        function display($handle, $include_once = true)
205 741 psotfx
        {
206 8100 acydburn
                global $user, $phpbb_hook;
207 3928 psotfx
208 10891 git-gate
                if (!empty($phpbb_hook) && $phpbb_hook->call_hook(array(__CLASS__, __FUNCTION__), $handle, $include_once, $this))
209 8100 acydburn
                {
210 8106 acydburn
                        if ($phpbb_hook->hook_return(array(__CLASS__, __FUNCTION__)))
211 8100 acydburn
                        {
212 8106 acydburn
                                return $phpbb_hook->hook_return_result(array(__CLASS__, __FUNCTION__));
213 8100 acydburn
                        }
214 8100 acydburn
                }
215 8100 acydburn
216 8044 acydburn
                if (defined('IN_ERROR_HANDLER'))
217 8044 acydburn
                {
218 8044 acydburn
                        if ((E_NOTICE & error_reporting()) == E_NOTICE)
219 8044 acydburn
                        {
220 8044 acydburn
                                error_reporting(error_reporting() ^ E_NOTICE);
221 8044 acydburn
                        }
222 8044 acydburn
                }
223 8044 acydburn
224 3928 psotfx
                if ($filename = $this->_tpl_load($handle))
225 741 psotfx
                {
226 4740 acydburn
                        ($include_once) ? include_once($filename) : include($filename);
227 741 psotfx
                }
228 3928 psotfx
                else
229 3928 psotfx
                {
230 3928 psotfx
                        eval(' ?>' . $this->compiled_code[$handle] . '<?php ');
231 3928 psotfx
                }
232 2662 psotfx
233 741 psotfx
                return true;
234 741 psotfx
        }
235 943 thefinn
236 5319 acydburn
        /**
237 6048 acydburn
        * Display the handle and assign the output to a template variable or return the compiled result.
238 6312 acydburn
        * @access public
239 5319 acydburn
        */
240 5790 acydburn
        function assign_display($handle, $template_var = '', $return_content = true, $include_once = false)
241 5319 acydburn
        {
242 5319 acydburn
                ob_start();
243 5319 acydburn
                $this->display($handle, $include_once);
244 5321 acydburn
                $contents = ob_get_clean();
245 5319 acydburn
246 5319 acydburn
                if ($return_content)
247 5319 acydburn
                {
248 5319 acydburn
                        return $contents;
249 5319 acydburn
                }
250 5319 acydburn
251 5319 acydburn
                $this->assign_var($template_var, $contents);
252 6048 acydburn
253 5319 acydburn
                return true;
254 5319 acydburn
        }
255 9633 nickvergessen
256 5319 acydburn
        /**
257 5319 acydburn
        * Load a compiled template if possible, if not, recompile it
258 6312 acydburn
        * @access private
259 5319 acydburn
        */
260 3928 psotfx
        function _tpl_load(&$handle)
261 741 psotfx
        {
262 5319 acydburn
                global $user, $phpEx, $config;
263 2642 psotfx
264 10213 acydburn
                if (!isset($this->filename[$handle]))
265 10213 acydburn
                {
266 10213 acydburn
                        trigger_error("template->_tpl_load(): No file specified for handle $handle", E_USER_ERROR);
267 10213 acydburn
                }
268 10213 acydburn
269 10460 naderman
                // reload these settings to have the values they had when this object was initialised
270 10460 naderman
                // using set_template or set_custom_template, they might otherwise have been overwritten
271 10460 naderman
                // by other template class instances in between.
272 10460 naderman
                $user->theme['template_storedb'] = $this->orig_tpl_storedb;
273 10460 naderman
                $user->theme['template_inherits_id'] = $this->orig_tpl_inherits_id;
274 10460 naderman
275 5894 naderman
                $filename = $this->cachepath . str_replace('/', '.', $this->filename[$handle]) . '.' . $phpEx;
276 10213 acydburn
                $this->files_template[$handle] = (isset($user->theme['template_id'])) ? $user->theme['template_id'] : 0;
277 9633 nickvergessen
278 8697 Kellanved
                $recompile = false;
279 11124 git-gate
                if (!file_exists($filename) || @filesize($filename) === 0 || defined('DEBUG_EXTRA'))
280 8697 Kellanved
                {
281 8697 Kellanved
                        $recompile = true;
282 8697 Kellanved
                }
283 8697 Kellanved
                else if ($config['load_tplcompile'])
284 8697 Kellanved
                {
285 8697 Kellanved
                        // No way around it: we need to check inheritance here
286 8697 Kellanved
                        if ($user->theme['template_inherits_id'] && !file_exists($this->files[$handle]))
287 8697 Kellanved
                        {
288 8697 Kellanved
                                $this->files[$handle] = $this->files_inherit[$handle];
289 8697 Kellanved
                                $this->files_template[$handle] = $user->theme['template_inherits_id'];
290 8697 Kellanved
                        }
291 8697 Kellanved
                        $recompile = (@filemtime($filename) < filemtime($this->files[$handle])) ? true : false;
292 8697 Kellanved
                }
293 9633 nickvergessen
294 3885 psotfx
                // Recompile page if the original template is newer, otherwise load the compiled version
295 4517 psotfx
                if (!$recompile)
296 2662 psotfx
                {
297 3928 psotfx
                        return $filename;
298 3928 psotfx
                }
299 2662 psotfx
300 5319 acydburn
                global $db, $phpbb_root_path;
301 4235 psotfx
302 6517 acydburn
                if (!class_exists('template_compile'))
303 6517 acydburn
                {
304 6517 acydburn
                        include($phpbb_root_path . 'includes/functions_template.' . $phpEx);
305 6517 acydburn
                }
306 9633 nickvergessen
307 8697 Kellanved
                // Inheritance - we point to another template file for this one. Equality is also used for store_db
308 8697 Kellanved
                if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'] && !file_exists($this->files[$handle]))
309 8697 Kellanved
                {
310 8697 Kellanved
                        $this->files[$handle] = $this->files_inherit[$handle];
311 8697 Kellanved
                        $this->files_template[$handle] = $user->theme['template_inherits_id'];
312 8697 Kellanved
                }
313 9633 nickvergessen
314 5319 acydburn
                $compile = new template_compile($this);
315 5319 acydburn
316 3928 psotfx
                // If we don't have a file assigned to this handle, die.
317 3928 psotfx
                if (!isset($this->files[$handle]))
318 3928 psotfx
                {
319 3928 psotfx
                        trigger_error("template->_tpl_load(): No file specified for handle $handle", E_USER_ERROR);
320 3928 psotfx
                }
321 3928 psotfx
322 5722 acydburn
                // Just compile if no user object is present (happens within the installer)
323 5722 acydburn
                if (!$user)
324 5722 acydburn
                {
325 5722 acydburn
                        $compile->_tpl_load_file($handle);
326 5722 acydburn
                        return false;
327 5722 acydburn
                }
328 5722 acydburn
329 5783 acydburn
                if (isset($user->theme['template_storedb']) && $user->theme['template_storedb'])
330 4235 psotfx
                {
331 8697 Kellanved
                        $rows = array();
332 8697 Kellanved
                        $ids = array();
333 8697 Kellanved
                        // Inheritance
334 8697 Kellanved
                        if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'])
335 8697 Kellanved
                        {
336 8697 Kellanved
                                $ids[] = $user->theme['template_inherits_id'];
337 8697 Kellanved
                        }
338 8697 Kellanved
                        $ids[] = $user->theme['template_id'];
339 9633 nickvergessen
340 8697 Kellanved
                        foreach ($ids as $id)
341 8697 Kellanved
                        {
342 8697 Kellanved
                                $sql = 'SELECT *
343 7286 acydburn
                                FROM ' . STYLES_TEMPLATE_DATA_TABLE . '
344 8697 Kellanved
                                WHERE template_id = ' . $id . "
345 4940 psotfx
                                        AND (template_filename = '" . $db->sql_escape($this->filename[$handle]) . "'
346 7789 acydburn
                                                OR template_included " . $db->sql_like_expression($db->any_char . $this->filename[$handle] . ':' . $db->any_char) . ')';
347 9633 nickvergessen
348 8697 Kellanved
                                $result = $db->sql_query($sql);
349 8697 Kellanved
                                while ($row = $db->sql_fetchrow($result))
350 8697 Kellanved
                                {
351 8697 Kellanved
                                        $rows[$row['template_filename']] = $row;
352 8697 Kellanved
                                }
353 8697 Kellanved
                                $db->sql_freeresult($result);
354 8697 Kellanved
                        }
355 9633 nickvergessen
356 8697 Kellanved
                        if (sizeof($rows))
357 4343 psotfx
                        {
358 8697 Kellanved
                                foreach ($rows as $row)
359 4343 psotfx
                                {
360 8697 Kellanved
                                        $file = $this->root . '/' . $row['template_filename'];
361 8697 Kellanved
                                        $force_reload = false;
362 8697 Kellanved
                                        if ($row['template_id'] != $user->theme['template_id'])
363 7286 acydburn
                                        {
364 8697 Kellanved
                                                // make sure that we are not overlooking a file not in the db yet
365 8697 Kellanved
                                                if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'] && !file_exists($file))
366 8697 Kellanved
                                                {
367 8697 Kellanved
                                                        $file = $this->inherit_root . '/' . $row['template_filename'];
368 8697 Kellanved
                                                        $this->files[$row['template_filename']] = $file;
369 8697 Kellanved
                                                        $this->files_inherit[$row['template_filename']] = $file;
370 8697 Kellanved
                                                        $this->files_template[$row['template_filename']] = $user->theme['template_inherits_id'];
371 8697 Kellanved
                                                }
372 8697 Kellanved
                                                else if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'])
373 8697 Kellanved
                                                {
374 8697 Kellanved
                                                        // Ok, we have a situation. There is a file in the subtemplate, but nothing in the DB. We have to fix that.
375 8697 Kellanved
                                                        $force_reload = true;
376 8697 Kellanved
                                                        $this->files_template[$row['template_filename']] = $user->theme['template_inherits_id'];
377 8697 Kellanved
                                                }
378 8697 Kellanved
                                        }
379 8697 Kellanved
                                        else
380 8697 Kellanved
                                        {
381 8697 Kellanved
                                                $this->files_template[$row['template_filename']] = $user->theme['template_id'];
382 8697 Kellanved
                                        }
383 9633 nickvergessen
384 8697 Kellanved
                                        if ($force_reload || $row['template_mtime'] < filemtime($file))
385 8697 Kellanved
                                        {
386 7286 acydburn
                                                if ($row['template_filename'] == $this->filename[$handle])
387 7286 acydburn
                                                {
388 8697 Kellanved
                                                        $compile->_tpl_load_file($handle, true);
389 7286 acydburn
                                                }
390 7286 acydburn
                                                else
391 7286 acydburn
                                                {
392 8697 Kellanved
                                                        $this->files[$row['template_filename']] = $file;
393 8697 Kellanved
                                                        $this->filename[$row['template_filename']] = $row['template_filename'];
394 8697 Kellanved
                                                        $compile->_tpl_load_file($row['template_filename'], true);
395 7286 acydburn
                                                        unset($this->compiled_code[$row['template_filename']]);
396 7286 acydburn
                                                        unset($this->files[$row['template_filename']]);
397 7853 davidmj
                                                        unset($this->filename[$row['template_filename']]);
398 7286 acydburn
                                                }
399 7286 acydburn
                                        }
400 7286 acydburn
401 4343 psotfx
                                        if ($row['template_filename'] == $this->filename[$handle])
402 4343 psotfx
                                        {
403 7286 acydburn
                                                $this->compiled_code[$handle] = $compile->compile(trim($row['template_data']));
404 7286 acydburn
                                                $compile->compile_write($handle, $this->compiled_code[$handle]);
405 4343 psotfx
                                        }
406 4343 psotfx
                                        else
407 4343 psotfx
                                        {
408 7286 acydburn
                                                // Only bother compiling if it doesn't already exist
409 7286 acydburn
                                                if (!file_exists($this->cachepath . str_replace('/', '.', $row['template_filename']) . '.' . $phpEx))
410 7286 acydburn
                                                {
411 7286 acydburn
                                                        $this->filename[$row['template_filename']] = $row['template_filename'];
412 7286 acydburn
                                                        $compile->compile_write($row['template_filename'], $compile->compile(trim($row['template_data'])));
413 7286 acydburn
                                                        unset($this->filename[$row['template_filename']]);
414 7286 acydburn
                                                }
415 4343 psotfx
                                        }
416 4343 psotfx
                                }
417 4343 psotfx
                        }
418 7286 acydburn
                        else
419 7286 acydburn
                        {
420 8697 Kellanved
                                $file = $this->root . '/' . $row['template_filename'];
421 8697 Kellanved
422 8697 Kellanved
                                if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'] && !file_exists($file))
423 8697 Kellanved
                                {
424 8697 Kellanved
                                        $file = $this->inherit_root . '/' . $row['template_filename'];
425 8697 Kellanved
                                        $this->files[$row['template_filename']] = $file;
426 8697 Kellanved
                                        $this->files_inherit[$row['template_filename']] = $file;
427 8697 Kellanved
                                        $this->files_template[$row['template_filename']] = $user->theme['template_inherits_id'];
428 8697 Kellanved
                                }
429 7286 acydburn
                                // Try to load from filesystem and instruct to insert into the styles table...
430 7286 acydburn
                                $compile->_tpl_load_file($handle, true);
431 7286 acydburn
                                return false;
432 7286 acydburn
                        }
433 6048 acydburn
434 4343 psotfx
                        return false;
435 4343 psotfx
                }
436 4343 psotfx
437 5319 acydburn
                $compile->_tpl_load_file($handle);
438 4343 psotfx
                return false;
439 4343 psotfx
        }
440 4343 psotfx
441 5319 acydburn
        /**
442 5319 acydburn
        * Assign key variable pairs from an array
443 6312 acydburn
        * @access public
444 5319 acydburn
        */
445 2662 psotfx
        function assign_vars($vararray)
446 2662 psotfx
        {
447 3016 psotfx
                foreach ($vararray as $key => $val)
448 2662 psotfx
                {
449 6796 davidmj
                        $this->_rootref[$key] = $val;
450 2662 psotfx
                }
451 2662 psotfx
452 2662 psotfx
                return true;
453 2662 psotfx
        }
454 2662 psotfx
455 5319 acydburn
        /**
456 5319 acydburn
        * Assign a single variable to a single key
457 6312 acydburn
        * @access public
458 5319 acydburn
        */
459 2662 psotfx
        function assign_var($varname, $varval)
460 2662 psotfx
        {
461 6796 davidmj
                $this->_rootref[$varname] = $varval;
462 2662 psotfx
463 2662 psotfx
                return true;
464 2662 psotfx
        }
465 2882 psotfx
466 5319 acydburn
        /**
467 5319 acydburn
        * Assign key variable pairs from an array to a specified block
468 6312 acydburn
        * @access public
469 5319 acydburn
        */
470 741 psotfx
        function assign_block_vars($blockname, $vararray)
471 741 psotfx
        {
472 4920 acydburn
                if (strpos($blockname, '.') !== false)
473 741 psotfx
                {
474 943 thefinn
                        // Nested block.
475 741 psotfx
                        $blocks = explode('.', $blockname);
476 741 psotfx
                        $blockcount = sizeof($blocks) - 1;
477 2662 psotfx
478 4940 psotfx
                        $str = &$this->_tpldata;
479 4940 psotfx
                        for ($i = 0; $i < $blockcount; $i++)
480 741 psotfx
                        {
481 4940 psotfx
                                $str = &$str[$blocks[$i]];
482 4940 psotfx
                                $str = &$str[sizeof($str) - 1];
483 4940 psotfx
                        }
484 2662 psotfx
485 5566 acydburn
                        $s_row_count = isset($str[$blocks[$blockcount]]) ? sizeof($str[$blocks[$blockcount]]) : 0;
486 5566 acydburn
                        $vararray['S_ROW_COUNT'] = $s_row_count;
487 6048 acydburn
488 5118 acydburn
                        // Assign S_FIRST_ROW
489 5566 acydburn
                        if (!$s_row_count)
490 5118 acydburn
                        {
491 5118 acydburn
                                $vararray['S_FIRST_ROW'] = true;
492 5118 acydburn
                        }
493 5118 acydburn
494 5118 acydburn
                        // Now the tricky part, we always assign S_LAST_ROW and remove the entry before
495 5118 acydburn
                        // This is much more clever than going through the complete template data on display (phew)
496 5118 acydburn
                        $vararray['S_LAST_ROW'] = true;
497 5566 acydburn
                        if ($s_row_count > 0)
498 5118 acydburn
                        {
499 5566 acydburn
                                unset($str[$blocks[$blockcount]][($s_row_count - 1)]['S_LAST_ROW']);
500 5118 acydburn
                        }
501 5118 acydburn
502 741 psotfx
                        // Now we add the block that we're actually assigning to.
503 741 psotfx
                        // We're adding a new iteration to this block with the given
504 741 psotfx
                        // variable assignments.
505 5622 acydburn
                        $str[$blocks[$blockcount]][] = $vararray;
506 741 psotfx
                }
507 741 psotfx
                else
508 741 psotfx
                {
509 741 psotfx
                        // Top-level block.
510 5566 acydburn
                        $s_row_count = (isset($this->_tpldata[$blockname])) ? sizeof($this->_tpldata[$blockname]) : 0;
511 5566 acydburn
                        $vararray['S_ROW_COUNT'] = $s_row_count;
512 5118 acydburn
513 5118 acydburn
                        // Assign S_FIRST_ROW
514 5566 acydburn
                        if (!$s_row_count)
515 5118 acydburn
                        {
516 5118 acydburn
                                $vararray['S_FIRST_ROW'] = true;
517 5118 acydburn
                        }
518 5118 acydburn
519 5118 acydburn
                        // We always assign S_LAST_ROW and remove the entry before
520 5118 acydburn
                        $vararray['S_LAST_ROW'] = true;
521 5566 acydburn
                        if ($s_row_count > 0)
522 5118 acydburn
                        {
523 5566 acydburn
                                unset($this->_tpldata[$blockname][($s_row_count - 1)]['S_LAST_ROW']);
524 5118 acydburn
                        }
525 9633 nickvergessen
526 6048 acydburn
                        // Add a new iteration to this block with the variable assignments we were given.
527 5622 acydburn
                        $this->_tpldata[$blockname][] = $vararray;
528 741 psotfx
                }
529 943 thefinn
530 741 psotfx
                return true;
531 741 psotfx
        }
532 943 thefinn
533 5114 acydburn
        /**
534 5114 acydburn
        * Change already assigned key variable pair (one-dimensional - single loop entry)
535 5114 acydburn
        *
536 6595 acydburn
        * An example of how to use this function:
537 6595 acydburn
        * {@example alter_block_array.php}
538 5114 acydburn
        *
539 6595 acydburn
        * @param        string        $blockname        the blockname, for example 'loop'
540 6595 acydburn
        * @param        array        $vararray        the var array to insert/add or merge
541 6595 acydburn
        * @param        mixed        $key                Key to search for
542 5114 acydburn
        *
543 5114 acydburn
        * array: KEY => VALUE [the key/value pair to search for within the loop to determine the correct position]
544 5114 acydburn
        *
545 5114 acydburn
        * int: Position [the position to change or insert at directly given]
546 5114 acydburn
        *
547 5114 acydburn
        * If key is false the position is set to 0
548 5114 acydburn
        * If key is true the position is set to the last entry
549 8131 acydburn
        *
550 6595 acydburn
        * @param        string        $mode                Mode to execute (valid modes are 'insert' and 'change')
551 5114 acydburn
        *
552 8131 acydburn
        *        If insert, the vararray is inserted at the given position (position counting from zero).
553 5114 acydburn
        *        If change, the current block gets merged with the vararray (resulting in new key/value pairs be added and existing keys be replaced by the new value).
554 5114 acydburn
        *
555 5114 acydburn
        * Since counting begins by zero, inserting at the last position will result in this array: array(vararray, last positioned array)
556 5114 acydburn
        * and inserting at position 1 will result in this array: array(first positioned array, vararray, following vars)
557 5114 acydburn
        *
558 6595 acydburn
        * @return bool false on error, true on success
559 6312 acydburn
        * @access public
560 5114 acydburn
        */
561 5013 acydburn
        function alter_block_array($blockname, $vararray, $key = false, $mode = 'insert')
562 5013 acydburn
        {
563 5013 acydburn
                if (strpos($blockname, '.') !== false)
564 5013 acydburn
                {
565 5013 acydburn
                        // Nested blocks are not supported
566 5013 acydburn
                        return false;
567 5013 acydburn
                }
568 9633 nickvergessen
569 5013 acydburn
                // Change key to zero (change first position) if false and to last position if true
570 5013 acydburn
                if ($key === false || $key === true)
571 5013 acydburn
                {
572 5379 acydburn
                        $key = ($key === false) ? 0 : sizeof($this->_tpldata[$blockname]);
573 5013 acydburn
                }
574 5013 acydburn
575 5013 acydburn
                // Get correct position if array given
576 5013 acydburn
                if (is_array($key))
577 5013 acydburn
                {
578 5013 acydburn
                        // Search array to get correct position
579 5013 acydburn
                        list($search_key, $search_value) = @each($key);
580 5013 acydburn
581 5013 acydburn
                        $key = NULL;
582 5013 acydburn
                        foreach ($this->_tpldata[$blockname] as $i => $val_ary)
583 5013 acydburn
                        {
584 5013 acydburn
                                if ($val_ary[$search_key] === $search_value)
585 5013 acydburn
                                {
586 5013 acydburn
                                        $key = $i;
587 5013 acydburn
                                        break;
588 5013 acydburn
                                }
589 5013 acydburn
                        }
590 5013 acydburn
591 5013 acydburn
                        // key/value pair not found
592 5013 acydburn
                        if ($key === NULL)
593 5013 acydburn
                        {
594 5013 acydburn
                                return false;
595 5013 acydburn
                        }
596 5013 acydburn
                }
597 6048 acydburn
598 5013 acydburn
                // Insert Block
599 5013 acydburn
                if ($mode == 'insert')
600 5013 acydburn
                {
601 5013 acydburn
                        // Make sure we are not exceeding the last iteration
602 5379 acydburn
                        if ($key >= sizeof($this->_tpldata[$blockname]))
603 5013 acydburn
                        {
604 5013 acydburn
                                $key = sizeof($this->_tpldata[$blockname]);
605 5379 acydburn
                                unset($this->_tpldata[$blockname][($key - 1)]['S_LAST_ROW']);
606 5379 acydburn
                                $vararray['S_LAST_ROW'] = true;
607 5013 acydburn
                        }
608 5379 acydburn
                        else if ($key === 0)
609 5379 acydburn
                        {
610 5379 acydburn
                                unset($this->_tpldata[$blockname][0]['S_FIRST_ROW']);
611 5379 acydburn
                                $vararray['S_FIRST_ROW'] = true;
612 5379 acydburn
                        }
613 5379 acydburn
614 5013 acydburn
                        // Re-position template blocks
615 5013 acydburn
                        for ($i = sizeof($this->_tpldata[$blockname]); $i > $key; $i--)
616 5013 acydburn
                        {
617 5013 acydburn
                                $this->_tpldata[$blockname][$i] = $this->_tpldata[$blockname][$i-1];
618 5013 acydburn
                                $this->_tpldata[$blockname][$i]['S_ROW_COUNT'] = $i;
619 5013 acydburn
                        }
620 5013 acydburn
621 5013 acydburn
                        // Insert vararray at given position
622 5013 acydburn
                        $vararray['S_ROW_COUNT'] = $key;
623 5622 acydburn
                        $this->_tpldata[$blockname][$key] = $vararray;
624 6048 acydburn
625 5013 acydburn
                        return true;
626 5013 acydburn
                }
627 6048 acydburn
628 5013 acydburn
                // Which block to change?
629 5013 acydburn
                if ($mode == 'change')
630 5013 acydburn
                {
631 5379 acydburn
                        if ($key == sizeof($this->_tpldata[$blockname]))
632 5379 acydburn
                        {
633 5379 acydburn
                                $key--;
634 5379 acydburn
                        }
635 5379 acydburn
636 5622 acydburn
                        $this->_tpldata[$blockname][$key] = array_merge($this->_tpldata[$blockname][$key], $vararray);
637 5013 acydburn
                        return true;
638 5013 acydburn
                }
639 5967 acydburn
640 5967 acydburn
                return false;
641 5013 acydburn
        }
642 5013 acydburn
643 5319 acydburn
        /**
644 6915 acydburn
        * Include a separate template
645 6312 acydburn
        * @access private
646 5319 acydburn
        */
647 3885 psotfx
        function _tpl_include($filename, $include = true)
648 3885 psotfx
        {
649 3885 psotfx
                $handle = $filename;
650 3885 psotfx
                $this->filename[$handle] = $filename;
651 4235 psotfx
                $this->files[$handle] = $this->root . '/' . $filename;
652 8697 Kellanved
                if ($this->inherit_root)
653 8697 Kellanved
                {
654 8697 Kellanved
                        $this->files_inherit[$handle] = $this->inherit_root . '/' . $filename;
655 8697 Kellanved
                }
656 3928 psotfx
657 8697 Kellanved
                $filename = $this->_tpl_load($handle);
658 4364 ludovic_arnaud
659 3928 psotfx
                if ($include)
660 3885 psotfx
                {
661 4517 psotfx
                        global $user;
662 4517 psotfx
663 4517 psotfx
                        if ($filename)
664 4223 psotfx
                        {
665 6405 acydburn
                                include($filename);
666 4517 psotfx
                                return;
667 4223 psotfx
                        }
668 4517 psotfx
                        eval(' ?>' . $this->compiled_code[$handle] . '<?php ');
669 3885 psotfx
                }
670 3885 psotfx
        }
671 9633 nickvergessen
672 9633 nickvergessen
        /**
673 9633 nickvergessen
        * Include a php-file
674 9633 nickvergessen
        * @access private
675 9633 nickvergessen
        */
676 9633 nickvergessen
        function _php_include($filename)
677 9633 nickvergessen
        {
678 9633 nickvergessen
                global $phpbb_root_path;
679 9633 nickvergessen
680 9633 nickvergessen
                $file = $phpbb_root_path . $filename;
681 9633 nickvergessen
682 9633 nickvergessen
                if (!file_exists($file))
683 9633 nickvergessen
                {
684 9633 nickvergessen
                        // trigger_error cannot be used here, as the output already started
685 9633 nickvergessen
                        echo 'template->_php_include(): File ' . htmlspecialchars($file) . ' does not exist or is empty';
686 9633 nickvergessen
                        return;
687 9633 nickvergessen
                }
688 9633 nickvergessen
                include($file);
689 9633 nickvergessen
        }
690 741 psotfx
}
691 933 psotfx
692 2642 psotfx
?>