~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to www/php/phpBB3/includes/template.php

  • Committer: William Grant
  • Date: 2009-02-23 23:47:02 UTC
  • mfrom: (1099.1.211 new-dispatch)
  • Revision ID: grantw@unimelb.edu.au-20090223234702-db4b1llly46ignwo
Merge from lp:~ivle-dev/ivle/new-dispatch.

Pretty much everything changes. Reread the setup docs. Backup your databases.
Every file is now in a different installed location, the configuration system
is rewritten, the dispatch system is rewritten, URLs are different, the
database is different, worksheets and exercises are no longer on the
filesystem, we use a templating engine, jail service protocols are rewritten,
we don't repeat ourselves, we have authorization rewritten, phpBB is gone,
and probably lots of other things that I cannot remember.

This is certainly the biggest commit I have ever made, and hopefully
the largest I ever will.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/**
3
 
*
4
 
* @package phpBB3
5
 
* @version $Id: template.php,v 1.116 2007/10/04 12:02:03 acydburn Exp $
6
 
* @copyright (c) 2005 phpBB Group, sections (c) 2001 ispi of Lincoln Inc
7
 
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
8
 
*
9
 
*/
10
 
 
11
 
/**
12
 
* @ignore
13
 
*/
14
 
if (!defined('IN_PHPBB'))
15
 
{
16
 
        exit;
17
 
}
18
 
 
19
 
/**
20
 
* Base Template class.
21
 
* @package phpBB3
22
 
*/
23
 
class template
24
 
{
25
 
        /** variable that holds all the data we'll be substituting into
26
 
        * the compiled templates. Takes form:
27
 
        * --> $this->_tpldata[block][iteration#][child][iteration#][child2][iteration#][variablename] == value
28
 
        * if it's a root-level variable, it'll be like this:
29
 
        * --> $this->_tpldata[.][0][varname] == value
30
 
        */
31
 
        var $_tpldata = array('.' => array(0 => array()));
32
 
        var $_rootref;
33
 
 
34
 
        // Root dir and hash of filenames for each template handle.
35
 
        var $root = '';
36
 
        var $cachepath = '';
37
 
        var $files = array();
38
 
        var $filename = array();
39
 
 
40
 
        // this will hash handle names to the compiled/uncompiled code for that handle.
41
 
        var $compiled_code = array();
42
 
 
43
 
        /**
44
 
        * Set template location
45
 
        * @access public
46
 
        */
47
 
        function set_template()
48
 
        {
49
 
                global $phpbb_root_path, $user;
50
 
 
51
 
                if (file_exists($phpbb_root_path . 'styles/' . $user->theme['template_path'] . '/template'))
52
 
                {
53
 
                        $this->root = $phpbb_root_path . 'styles/' . $user->theme['template_path'] . '/template';
54
 
                        $this->cachepath = $phpbb_root_path . 'cache/tpl_' . $user->theme['template_path'] . '_';
55
 
                }
56
 
                else
57
 
                {
58
 
                        trigger_error('Template path could not be found: styles/' . $user->theme['template_path'] . '/template', E_USER_ERROR);
59
 
                }
60
 
 
61
 
                $this->_rootref = &$this->_tpldata['.'][0];
62
 
 
63
 
                return true;
64
 
        }
65
 
 
66
 
        /**
67
 
        * Set custom template location (able to use directory outside of phpBB)
68
 
        * @access public
69
 
        */
70
 
        function set_custom_template($template_path, $template_name)
71
 
        {
72
 
                global $phpbb_root_path;
73
 
 
74
 
                $this->root = $template_path;
75
 
                $this->cachepath = $phpbb_root_path . 'cache/ctpl_' . $template_name . '_';
76
 
 
77
 
                return true;
78
 
        }
79
 
 
80
 
        /**
81
 
        * Sets the template filenames for handles. $filename_array
82
 
        * should be a hash of handle => filename pairs.
83
 
        * @access public
84
 
        */
85
 
        function set_filenames($filename_array)
86
 
        {
87
 
                if (!is_array($filename_array))
88
 
                {
89
 
                        return false;
90
 
                }
91
 
 
92
 
                foreach ($filename_array as $handle => $filename)
93
 
                {
94
 
                        if (empty($filename))
95
 
                        {
96
 
                                trigger_error("template->set_filenames: Empty filename specified for $handle", E_USER_ERROR);
97
 
                        }
98
 
 
99
 
                        $this->filename[$handle] = $filename;
100
 
                        $this->files[$handle] = $this->root . '/' . $filename;
101
 
                }
102
 
 
103
 
                return true;
104
 
        }
105
 
 
106
 
        /**
107
 
        * Destroy template data set
108
 
        * @access public
109
 
        */
110
 
        function destroy()
111
 
        {
112
 
                $this->_tpldata = array('.' => array(0 => array()));
113
 
        }
114
 
 
115
 
        /**
116
 
        * Reset/empty complete block
117
 
        * @access public
118
 
        */
119
 
        function destroy_block_vars($blockname)
120
 
        {
121
 
                if (strpos($blockname, '.') !== false)
122
 
                {
123
 
                        // Nested block.
124
 
                        $blocks = explode('.', $blockname);
125
 
                        $blockcount = sizeof($blocks) - 1;
126
 
 
127
 
                        $str = &$this->_tpldata;
128
 
                        for ($i = 0; $i < $blockcount; $i++)
129
 
                        {
130
 
                                $str = &$str[$blocks[$i]];
131
 
                                $str = &$str[sizeof($str) - 1];
132
 
                        }
133
 
 
134
 
                        unset($str[$blocks[$blockcount]]);
135
 
                }
136
 
                else
137
 
                {
138
 
                        // Top-level block.
139
 
                        unset($this->_tpldata[$blockname]);
140
 
                }
141
 
 
142
 
                return true;
143
 
        }
144
 
 
145
 
        /**
146
 
        * Display handle
147
 
        * @access public
148
 
        */
149
 
        function display($handle, $include_once = true)
150
 
        {
151
 
                global $user, $phpbb_hook;
152
 
 
153
 
                if (!empty($phpbb_hook) && $phpbb_hook->call_hook(array(__CLASS__, __FUNCTION__), $handle, $include_once))
154
 
                {
155
 
                        if ($phpbb_hook->hook_return(array(__CLASS__, __FUNCTION__)))
156
 
                        {
157
 
                                return $phpbb_hook->hook_return_result(array(__CLASS__, __FUNCTION__));
158
 
                        }
159
 
                }
160
 
 
161
 
                if (defined('IN_ERROR_HANDLER'))
162
 
                {
163
 
                        if ((E_NOTICE & error_reporting()) == E_NOTICE)
164
 
                        {
165
 
                                error_reporting(error_reporting() ^ E_NOTICE);
166
 
                        }
167
 
                }
168
 
 
169
 
                if ($filename = $this->_tpl_load($handle))
170
 
                {
171
 
                        ($include_once) ? include_once($filename) : include($filename);
172
 
                }
173
 
                else
174
 
                {
175
 
                        eval(' ?>' . $this->compiled_code[$handle] . '<?php ');
176
 
                }
177
 
 
178
 
                return true;
179
 
        }
180
 
 
181
 
        /**
182
 
        * Display the handle and assign the output to a template variable or return the compiled result.
183
 
        * @access public
184
 
        */
185
 
        function assign_display($handle, $template_var = '', $return_content = true, $include_once = false)
186
 
        {
187
 
                ob_start();
188
 
                $this->display($handle, $include_once);
189
 
                $contents = ob_get_clean();
190
 
 
191
 
                if ($return_content)
192
 
                {
193
 
                        return $contents;
194
 
                }
195
 
 
196
 
                $this->assign_var($template_var, $contents);
197
 
 
198
 
                return true;
199
 
        }
200
 
 
201
 
        /**
202
 
        * Load a compiled template if possible, if not, recompile it
203
 
        * @access private
204
 
        */
205
 
        function _tpl_load(&$handle)
206
 
        {
207
 
                global $user, $phpEx, $config;
208
 
 
209
 
                $filename = $this->cachepath . str_replace('/', '.', $this->filename[$handle]) . '.' . $phpEx;
210
 
 
211
 
                $recompile = (($config['load_tplcompile'] && @filemtime($filename) < filemtime($this->files[$handle])) || !file_exists($filename) || @filesize($filename) === 0) ? true : false;
212
 
 
213
 
                // Recompile page if the original template is newer, otherwise load the compiled version
214
 
                if (!$recompile)
215
 
                {
216
 
                        return $filename;
217
 
                }
218
 
 
219
 
                global $db, $phpbb_root_path;
220
 
 
221
 
                if (!class_exists('template_compile'))
222
 
                {
223
 
                        include($phpbb_root_path . 'includes/functions_template.' . $phpEx);
224
 
                }
225
 
 
226
 
                $compile = new template_compile($this);
227
 
 
228
 
                // If we don't have a file assigned to this handle, die.
229
 
                if (!isset($this->files[$handle]))
230
 
                {
231
 
                        trigger_error("template->_tpl_load(): No file specified for handle $handle", E_USER_ERROR);
232
 
                }
233
 
 
234
 
                // Just compile if no user object is present (happens within the installer)
235
 
                if (!$user)
236
 
                {
237
 
                        $compile->_tpl_load_file($handle);
238
 
                        return false;
239
 
                }
240
 
 
241
 
                if (isset($user->theme['template_storedb']) && $user->theme['template_storedb'])
242
 
                {
243
 
                        $sql = 'SELECT *
244
 
                                FROM ' . STYLES_TEMPLATE_DATA_TABLE . '
245
 
                                WHERE template_id = ' . $user->theme['template_id'] . "
246
 
                                        AND (template_filename = '" . $db->sql_escape($this->filename[$handle]) . "'
247
 
                                                OR template_included " . $db->sql_like_expression($db->any_char . $this->filename[$handle] . ':' . $db->any_char) . ')';
248
 
                        $result = $db->sql_query($sql);
249
 
                        $row = $db->sql_fetchrow($result);
250
 
 
251
 
                        if ($row)
252
 
                        {
253
 
                                do
254
 
                                {
255
 
                                        if ($row['template_mtime'] < filemtime($phpbb_root_path . 'styles/' . $user->theme['template_path'] . '/template/' . $row['template_filename']))
256
 
                                        {
257
 
                                                if ($row['template_filename'] == $this->filename[$handle])
258
 
                                                {
259
 
                                                        $compile->_tpl_load_file($handle);
260
 
                                                }
261
 
                                                else
262
 
                                                {
263
 
                                                        $this->files[$row['template_filename']] = $this->root . '/' . $row['template_filename'];
264
 
                                                        $compile->_tpl_load_file($row['template_filename']);
265
 
                                                        unset($this->compiled_code[$row['template_filename']]);
266
 
                                                        unset($this->files[$row['template_filename']]);
267
 
                                                        unset($this->filename[$row['template_filename']]);
268
 
                                                }
269
 
                                        }
270
 
 
271
 
                                        if ($row['template_filename'] == $this->filename[$handle])
272
 
                                        {
273
 
                                                $this->compiled_code[$handle] = $compile->compile(trim($row['template_data']));
274
 
                                                $compile->compile_write($handle, $this->compiled_code[$handle]);
275
 
                                        }
276
 
                                        else
277
 
                                        {
278
 
                                                // Only bother compiling if it doesn't already exist
279
 
                                                if (!file_exists($this->cachepath . str_replace('/', '.', $row['template_filename']) . '.' . $phpEx))
280
 
                                                {
281
 
                                                        $this->filename[$row['template_filename']] = $row['template_filename'];
282
 
                                                        $compile->compile_write($row['template_filename'], $compile->compile(trim($row['template_data'])));
283
 
                                                        unset($this->filename[$row['template_filename']]);
284
 
                                                }
285
 
                                        }
286
 
                                }
287
 
                                while ($row = $db->sql_fetchrow($result));
288
 
                        }
289
 
                        else
290
 
                        {
291
 
                                // Try to load from filesystem and instruct to insert into the styles table...
292
 
                                $compile->_tpl_load_file($handle, true);
293
 
                                return false;
294
 
                        }
295
 
                        $db->sql_freeresult($result);
296
 
 
297
 
                        return false;
298
 
                }
299
 
 
300
 
                $compile->_tpl_load_file($handle);
301
 
                return false;
302
 
        }
303
 
 
304
 
        /**
305
 
        * Assign key variable pairs from an array
306
 
        * @access public
307
 
        */
308
 
        function assign_vars($vararray)
309
 
        {
310
 
                foreach ($vararray as $key => $val)
311
 
                {
312
 
                        $this->_rootref[$key] = $val;
313
 
                }
314
 
 
315
 
                return true;
316
 
        }
317
 
 
318
 
        /**
319
 
        * Assign a single variable to a single key
320
 
        * @access public
321
 
        */
322
 
        function assign_var($varname, $varval)
323
 
        {
324
 
                $this->_rootref[$varname] = $varval;
325
 
 
326
 
                return true;
327
 
        }
328
 
 
329
 
        /**
330
 
        * Assign key variable pairs from an array to a specified block
331
 
        * @access public
332
 
        */
333
 
        function assign_block_vars($blockname, $vararray)
334
 
        {
335
 
                if (strpos($blockname, '.') !== false)
336
 
                {
337
 
                        // Nested block.
338
 
                        $blocks = explode('.', $blockname);
339
 
                        $blockcount = sizeof($blocks) - 1;
340
 
 
341
 
                        $str = &$this->_tpldata;
342
 
                        for ($i = 0; $i < $blockcount; $i++)
343
 
                        {
344
 
                                $str = &$str[$blocks[$i]];
345
 
                                $str = &$str[sizeof($str) - 1];
346
 
                        }
347
 
 
348
 
                        $s_row_count = isset($str[$blocks[$blockcount]]) ? sizeof($str[$blocks[$blockcount]]) : 0;
349
 
                        $vararray['S_ROW_COUNT'] = $s_row_count;
350
 
 
351
 
                        // Assign S_FIRST_ROW
352
 
                        if (!$s_row_count)
353
 
                        {
354
 
                                $vararray['S_FIRST_ROW'] = true;
355
 
                        }
356
 
 
357
 
                        // Now the tricky part, we always assign S_LAST_ROW and remove the entry before
358
 
                        // This is much more clever than going through the complete template data on display (phew)
359
 
                        $vararray['S_LAST_ROW'] = true;
360
 
                        if ($s_row_count > 0)
361
 
                        {
362
 
                                unset($str[$blocks[$blockcount]][($s_row_count - 1)]['S_LAST_ROW']);
363
 
                        }
364
 
 
365
 
                        // Now we add the block that we're actually assigning to.
366
 
                        // We're adding a new iteration to this block with the given
367
 
                        // variable assignments.
368
 
                        $str[$blocks[$blockcount]][] = $vararray;
369
 
                }
370
 
                else
371
 
                {
372
 
                        // Top-level block.
373
 
                        $s_row_count = (isset($this->_tpldata[$blockname])) ? sizeof($this->_tpldata[$blockname]) : 0;
374
 
                        $vararray['S_ROW_COUNT'] = $s_row_count;
375
 
 
376
 
                        // Assign S_FIRST_ROW
377
 
                        if (!$s_row_count)
378
 
                        {
379
 
                                $vararray['S_FIRST_ROW'] = true;
380
 
                        }
381
 
 
382
 
                        // We always assign S_LAST_ROW and remove the entry before
383
 
                        $vararray['S_LAST_ROW'] = true;
384
 
                        if ($s_row_count > 0)
385
 
                        {
386
 
                                unset($this->_tpldata[$blockname][($s_row_count - 1)]['S_LAST_ROW']);
387
 
                        }
388
 
                        
389
 
                        // Add a new iteration to this block with the variable assignments we were given.
390
 
                        $this->_tpldata[$blockname][] = $vararray;
391
 
                }
392
 
 
393
 
                return true;
394
 
        }
395
 
 
396
 
        /**
397
 
        * Change already assigned key variable pair (one-dimensional - single loop entry)
398
 
        *
399
 
        * An example of how to use this function:
400
 
        * {@example alter_block_array.php}
401
 
        *
402
 
        * @param        string  $blockname      the blockname, for example 'loop'
403
 
        * @param        array   $vararray       the var array to insert/add or merge
404
 
        * @param        mixed   $key            Key to search for
405
 
        *
406
 
        * array: KEY => VALUE [the key/value pair to search for within the loop to determine the correct position]
407
 
        *
408
 
        * int: Position [the position to change or insert at directly given]
409
 
        *
410
 
        * If key is false the position is set to 0
411
 
        * If key is true the position is set to the last entry
412
 
        *
413
 
        * @param        string  $mode           Mode to execute (valid modes are 'insert' and 'change')
414
 
        *
415
 
        *       If insert, the vararray is inserted at the given position (position counting from zero).
416
 
        *       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).
417
 
        *
418
 
        * Since counting begins by zero, inserting at the last position will result in this array: array(vararray, last positioned array)
419
 
        * and inserting at position 1 will result in this array: array(first positioned array, vararray, following vars)
420
 
        *
421
 
        * @return bool false on error, true on success
422
 
        * @access public
423
 
        */
424
 
        function alter_block_array($blockname, $vararray, $key = false, $mode = 'insert')
425
 
        {
426
 
                if (strpos($blockname, '.') !== false)
427
 
                {
428
 
                        // Nested blocks are not supported
429
 
                        return false;
430
 
                }
431
 
                
432
 
                // Change key to zero (change first position) if false and to last position if true
433
 
                if ($key === false || $key === true)
434
 
                {
435
 
                        $key = ($key === false) ? 0 : sizeof($this->_tpldata[$blockname]);
436
 
                }
437
 
 
438
 
                // Get correct position if array given
439
 
                if (is_array($key))
440
 
                {
441
 
                        // Search array to get correct position
442
 
                        list($search_key, $search_value) = @each($key);
443
 
 
444
 
                        $key = NULL;
445
 
                        foreach ($this->_tpldata[$blockname] as $i => $val_ary)
446
 
                        {
447
 
                                if ($val_ary[$search_key] === $search_value)
448
 
                                {
449
 
                                        $key = $i;
450
 
                                        break;
451
 
                                }
452
 
                        }
453
 
 
454
 
                        // key/value pair not found
455
 
                        if ($key === NULL)
456
 
                        {
457
 
                                return false;
458
 
                        }
459
 
                }
460
 
 
461
 
                // Insert Block
462
 
                if ($mode == 'insert')
463
 
                {
464
 
                        // Make sure we are not exceeding the last iteration
465
 
                        if ($key >= sizeof($this->_tpldata[$blockname]))
466
 
                        {
467
 
                                $key = sizeof($this->_tpldata[$blockname]);
468
 
                                unset($this->_tpldata[$blockname][($key - 1)]['S_LAST_ROW']);
469
 
                                $vararray['S_LAST_ROW'] = true;
470
 
                        }
471
 
                        else if ($key === 0)
472
 
                        {
473
 
                                unset($this->_tpldata[$blockname][0]['S_FIRST_ROW']);
474
 
                                $vararray['S_FIRST_ROW'] = true;
475
 
                        }
476
 
 
477
 
                        // Re-position template blocks
478
 
                        for ($i = sizeof($this->_tpldata[$blockname]); $i > $key; $i--)
479
 
                        {
480
 
                                $this->_tpldata[$blockname][$i] = $this->_tpldata[$blockname][$i-1];
481
 
                                $this->_tpldata[$blockname][$i]['S_ROW_COUNT'] = $i;
482
 
                        }
483
 
 
484
 
                        // Insert vararray at given position
485
 
                        $vararray['S_ROW_COUNT'] = $key;
486
 
                        $this->_tpldata[$blockname][$key] = $vararray;
487
 
 
488
 
                        return true;
489
 
                }
490
 
 
491
 
                // Which block to change?
492
 
                if ($mode == 'change')
493
 
                {
494
 
                        if ($key == sizeof($this->_tpldata[$blockname]))
495
 
                        {
496
 
                                $key--;
497
 
                        }
498
 
 
499
 
                        $this->_tpldata[$blockname][$key] = array_merge($this->_tpldata[$blockname][$key], $vararray);
500
 
                        return true;
501
 
                }
502
 
 
503
 
                return false;
504
 
        }
505
 
 
506
 
        /**
507
 
        * Include a separate template
508
 
        * @access private
509
 
        */
510
 
        function _tpl_include($filename, $include = true)
511
 
        {
512
 
                $handle = $filename;
513
 
                $this->filename[$handle] = $filename;
514
 
                $this->files[$handle] = $this->root . '/' . $filename;
515
 
 
516
 
                $filename = $this->_tpl_load($handle);
517
 
 
518
 
                if ($include)
519
 
                {
520
 
                        global $user;
521
 
 
522
 
                        if ($filename)
523
 
                        {
524
 
                                include($filename);
525
 
                                return;
526
 
                        }
527
 
                        eval(' ?>' . $this->compiled_code[$handle] . '<?php ');
528
 
                }
529
 
        }
530
 
}
531
 
 
532
 
?>
 
 
b'\\ No newline at end of file'