includes/functions_template.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Sat, 10 Mar 2012 00:18:56 +0400
changeset 15 8804e4d3de74
parent 0 ceef6d64c21d
permissions -rw-r--r--
Added online gamers
indvdum@0
     1
<?php
indvdum@0
     2
/**
indvdum@0
     3
*
indvdum@0
     4
* @package phpBB3
indvdum@0
     5
* @version $Id$
indvdum@0
     6
* @copyright (c) 2005 phpBB Group, sections (c) 2001 ispi of Lincoln Inc
indvdum@0
     7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
indvdum@0
     8
*
indvdum@0
     9
*/
indvdum@0
    10
indvdum@0
    11
/**
indvdum@0
    12
* @ignore
indvdum@0
    13
*/
indvdum@0
    14
if (!defined('IN_PHPBB'))
indvdum@0
    15
{
indvdum@0
    16
	exit;
indvdum@0
    17
}
indvdum@0
    18
indvdum@0
    19
/**
indvdum@0
    20
* Extension of template class - Functions needed for compiling templates only.
indvdum@0
    21
*
indvdum@0
    22
* psoTFX, phpBB Development Team - Completion of file caching, decompilation
indvdum@0
    23
* routines and implementation of conditionals/keywords and associated changes
indvdum@0
    24
*
indvdum@0
    25
* The interface was inspired by PHPLib templates,  and the template file (formats are
indvdum@0
    26
* quite similar)
indvdum@0
    27
*
indvdum@0
    28
* The keyword/conditional implementation is currently based on sections of code from
indvdum@0
    29
* the Smarty templating engine (c) 2001 ispi of Lincoln, Inc. which is released
indvdum@0
    30
* (on its own and in whole) under the LGPL. Section 3 of the LGPL states that any code
indvdum@0
    31
* derived from an LGPL application may be relicenced under the GPL, this applies
indvdum@0
    32
* to this source
indvdum@0
    33
*
indvdum@0
    34
* DEFINE directive inspired by a request by Cyberalien
indvdum@0
    35
*
indvdum@0
    36
* @package phpBB3
indvdum@0
    37
*/
indvdum@0
    38
class template_compile
indvdum@0
    39
{
indvdum@0
    40
	var $template;
indvdum@0
    41
indvdum@0
    42
	// Various storage arrays
indvdum@0
    43
	var $block_names = array();
indvdum@0
    44
	var $block_else_level = array();
indvdum@0
    45
indvdum@0
    46
	/**
indvdum@0
    47
	* constuctor
indvdum@0
    48
	*/
indvdum@0
    49
	function template_compile(&$template)
indvdum@0
    50
	{
indvdum@0
    51
		$this->template = &$template;
indvdum@0
    52
	}
indvdum@0
    53
indvdum@0
    54
	/**
indvdum@0
    55
	* Load template source from file
indvdum@0
    56
	* @access private
indvdum@0
    57
	*/
indvdum@0
    58
	function _tpl_load_file($handle, $store_in_db = false)
indvdum@0
    59
	{
indvdum@0
    60
		// Try and open template for read
indvdum@0
    61
		if (!file_exists($this->template->files[$handle]))
indvdum@0
    62
		{
indvdum@0
    63
			trigger_error("template->_tpl_load_file(): File {$this->template->files[$handle]} does not exist or is empty", E_USER_ERROR);
indvdum@0
    64
		}
indvdum@0
    65
indvdum@0
    66
		$this->template->compiled_code[$handle] = $this->compile(trim(@file_get_contents($this->template->files[$handle])));
indvdum@0
    67
indvdum@0
    68
		// Actually compile the code now.
indvdum@0
    69
		$this->compile_write($handle, $this->template->compiled_code[$handle]);
indvdum@0
    70
indvdum@0
    71
		// Store in database if required...
indvdum@0
    72
		if ($store_in_db)
indvdum@0
    73
		{
indvdum@0
    74
			global $db, $user;
indvdum@0
    75
indvdum@0
    76
			$sql_ary = array(
indvdum@0
    77
				'template_id'			=> $this->template->files_template[$handle],
indvdum@0
    78
				'template_filename'		=> $this->template->filename[$handle],
indvdum@0
    79
				'template_included'		=> '',
indvdum@0
    80
				'template_mtime'		=> time(),
indvdum@0
    81
				'template_data'			=> trim(@file_get_contents($this->template->files[$handle])),
indvdum@0
    82
			);
indvdum@0
    83
indvdum@0
    84
			$sql = 'INSERT INTO ' . STYLES_TEMPLATE_DATA_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
indvdum@0
    85
			$db->sql_query($sql);
indvdum@0
    86
		}
indvdum@0
    87
	}
indvdum@0
    88
indvdum@0
    89
	/**
indvdum@0
    90
	* Remove any PHP tags that do not belong, these regular expressions are derived from
indvdum@0
    91
	* the ones that exist in zend_language_scanner.l
indvdum@0
    92
	* @access private
indvdum@0
    93
	*/
indvdum@0
    94
	function remove_php_tags(&$code)
indvdum@0
    95
	{
indvdum@0
    96
		// This matches the information gathered from the internal PHP lexer
indvdum@0
    97
		$match = array(
indvdum@0
    98
			'#<([\?%])=?.*?\1>#s',
indvdum@0
    99
			'#<script\s+language\s*=\s*(["\']?)php\1\s*>.*?</script\s*>#s',
indvdum@0
   100
			'#<\?php(?:\r\n?|[ \n\t]).*?\?>#s'
indvdum@0
   101
		);
indvdum@0
   102
indvdum@0
   103
		$code = preg_replace($match, '', $code);
indvdum@0
   104
	}
indvdum@0
   105
indvdum@0
   106
	/**
indvdum@0
   107
	* The all seeing all doing compile method. Parts are inspired by or directly from Smarty
indvdum@0
   108
	* @access private
indvdum@0
   109
	*/
indvdum@0
   110
	function compile($code, $no_echo = false, $echo_var = '')
indvdum@0
   111
	{
indvdum@0
   112
		global $config;
indvdum@0
   113
indvdum@0
   114
		if ($echo_var)
indvdum@0
   115
		{
indvdum@0
   116
			global $$echo_var;
indvdum@0
   117
		}
indvdum@0
   118
indvdum@0
   119
		// Remove any "loose" php ... we want to give admins the ability
indvdum@0
   120
		// to switch on/off PHP for a given template. Allowing unchecked
indvdum@0
   121
		// php is a no-no. There is a potential issue here in that non-php
indvdum@0
   122
		// content may be removed ... however designers should use entities
indvdum@0
   123
		// if they wish to display < and >
indvdum@0
   124
		$this->remove_php_tags($code);
indvdum@0
   125
indvdum@0
   126
		// Pull out all block/statement level elements and separate plain text
indvdum@0
   127
		preg_match_all('#<!-- PHP -->(.*?)<!-- ENDPHP -->#s', $code, $matches);
indvdum@0
   128
		$php_blocks = $matches[1];
indvdum@0
   129
		$code = preg_replace('#<!-- PHP -->.*?<!-- ENDPHP -->#s', '<!-- PHP -->', $code);
indvdum@0
   130
indvdum@0
   131
		preg_match_all('#<!-- INCLUDE (\{\$?[A-Z0-9\-_]+\}|[a-zA-Z0-9\_\-\+\./]+) -->#', $code, $matches);
indvdum@0
   132
		$include_blocks = $matches[1];
indvdum@0
   133
		$code = preg_replace('#<!-- INCLUDE (?:\{\$?[A-Z0-9\-_]+\}|[a-zA-Z0-9\_\-\+\./]+) -->#', '<!-- INCLUDE -->', $code);
indvdum@0
   134
indvdum@0
   135
		preg_match_all('#<!-- INCLUDEPHP ([a-zA-Z0-9\_\-\+\./]+) -->#', $code, $matches);
indvdum@0
   136
		$includephp_blocks = $matches[1];
indvdum@0
   137
		$code = preg_replace('#<!-- INCLUDEPHP [a-zA-Z0-9\_\-\+\./]+ -->#', '<!-- INCLUDEPHP -->', $code);
indvdum@0
   138
indvdum@0
   139
		preg_match_all('#<!-- ([^<].*?) (.*?)? ?-->#', $code, $blocks, PREG_SET_ORDER);
indvdum@0
   140
indvdum@0
   141
		$text_blocks = preg_split('#<!-- [^<].*? (?:.*?)? ?-->#', $code);
indvdum@0
   142
indvdum@0
   143
		for ($i = 0, $j = sizeof($text_blocks); $i < $j; $i++)
indvdum@0
   144
		{
indvdum@0
   145
			$this->compile_var_tags($text_blocks[$i]);
indvdum@0
   146
		}
indvdum@0
   147
		$compile_blocks = array();
indvdum@0
   148
indvdum@0
   149
		for ($curr_tb = 0, $tb_size = sizeof($blocks); $curr_tb < $tb_size; $curr_tb++)
indvdum@0
   150
		{
indvdum@0
   151
			$block_val = &$blocks[$curr_tb];
indvdum@0
   152
indvdum@0
   153
			switch ($block_val[1])
indvdum@0
   154
			{
indvdum@0
   155
				case 'BEGIN':
indvdum@0
   156
					$this->block_else_level[] = false;
indvdum@0
   157
					$compile_blocks[] = '<?php ' . $this->compile_tag_block($block_val[2]) . ' ?>';
indvdum@0
   158
				break;
indvdum@0
   159
indvdum@0
   160
				case 'BEGINELSE':
indvdum@0
   161
					$this->block_else_level[sizeof($this->block_else_level) - 1] = true;
indvdum@0
   162
					$compile_blocks[] = '<?php }} else { ?>';
indvdum@0
   163
				break;
indvdum@0
   164
indvdum@0
   165
				case 'END':
indvdum@0
   166
					array_pop($this->block_names);
indvdum@0
   167
					$compile_blocks[] = '<?php ' . ((array_pop($this->block_else_level)) ? '}' : '}}') . ' ?>';
indvdum@0
   168
				break;
indvdum@0
   169
indvdum@0
   170
				case 'IF':
indvdum@0
   171
					$compile_blocks[] = '<?php ' . $this->compile_tag_if($block_val[2], false) . ' ?>';
indvdum@0
   172
				break;
indvdum@0
   173
indvdum@0
   174
				case 'ELSE':
indvdum@0
   175
					$compile_blocks[] = '<?php } else { ?>';
indvdum@0
   176
				break;
indvdum@0
   177
indvdum@0
   178
				case 'ELSEIF':
indvdum@0
   179
					$compile_blocks[] = '<?php ' . $this->compile_tag_if($block_val[2], true) . ' ?>';
indvdum@0
   180
				break;
indvdum@0
   181
indvdum@0
   182
				case 'ENDIF':
indvdum@0
   183
					$compile_blocks[] = '<?php } ?>';
indvdum@0
   184
				break;
indvdum@0
   185
indvdum@0
   186
				case 'DEFINE':
indvdum@0
   187
					$compile_blocks[] = '<?php ' . $this->compile_tag_define($block_val[2], true) . ' ?>';
indvdum@0
   188
				break;
indvdum@0
   189
indvdum@0
   190
				case 'UNDEFINE':
indvdum@0
   191
					$compile_blocks[] = '<?php ' . $this->compile_tag_define($block_val[2], false) . ' ?>';
indvdum@0
   192
				break;
indvdum@0
   193
indvdum@0
   194
				case 'INCLUDE':
indvdum@0
   195
					$temp = array_shift($include_blocks);
indvdum@0
   196
indvdum@0
   197
					// Dynamic includes
indvdum@0
   198
					// Cheap match rather than a full blown regexp, we already know
indvdum@0
   199
					// the format of the input so just use string manipulation.
indvdum@0
   200
					if ($temp[0] == '{')
indvdum@0
   201
					{
indvdum@0
   202
						$file = false;
indvdum@0
   203
indvdum@0
   204
						if ($temp[1] == '$')
indvdum@0
   205
						{
indvdum@0
   206
							$var = substr($temp, 2, -1);
indvdum@0
   207
							//$file = $this->template->_tpldata['DEFINE']['.'][$var];
indvdum@0
   208
							$temp = "\$this->_tpldata['DEFINE']['.']['$var']";
indvdum@0
   209
						}
indvdum@0
   210
						else
indvdum@0
   211
						{
indvdum@0
   212
							$var = substr($temp, 1, -1);
indvdum@0
   213
							//$file = $this->template->_rootref[$var];
indvdum@0
   214
							$temp = "\$this->_rootref['$var']";
indvdum@0
   215
						}
indvdum@0
   216
					}
indvdum@0
   217
					else
indvdum@0
   218
					{
indvdum@0
   219
						$file = $temp;
indvdum@0
   220
					}
indvdum@0
   221
indvdum@0
   222
					$compile_blocks[] = '<?php ' . $this->compile_tag_include($temp) . ' ?>';
indvdum@0
   223
indvdum@0
   224
					// No point in checking variable includes
indvdum@0
   225
					if ($file)
indvdum@0
   226
					{
indvdum@0
   227
						$this->template->_tpl_include($file, false);
indvdum@0
   228
					}
indvdum@0
   229
				break;
indvdum@0
   230
indvdum@0
   231
				case 'INCLUDEPHP':
indvdum@0
   232
					$compile_blocks[] = ($config['tpl_allow_php']) ? '<?php ' . $this->compile_tag_include_php(array_shift($includephp_blocks)) . ' ?>' : '';
indvdum@0
   233
				break;
indvdum@0
   234
indvdum@0
   235
				case 'PHP':
indvdum@0
   236
					$compile_blocks[] = ($config['tpl_allow_php']) ? '<?php ' . array_shift($php_blocks) . ' ?>' : '';
indvdum@0
   237
				break;
indvdum@0
   238
indvdum@0
   239
				default:
indvdum@0
   240
					$this->compile_var_tags($block_val[0]);
indvdum@0
   241
					$trim_check = trim($block_val[0]);
indvdum@0
   242
					$compile_blocks[] = (!$no_echo) ? ((!empty($trim_check)) ? $block_val[0] : '') : ((!empty($trim_check)) ? $block_val[0] : '');
indvdum@0
   243
				break;
indvdum@0
   244
			}
indvdum@0
   245
		}
indvdum@0
   246
indvdum@0
   247
		$template_php = '';
indvdum@0
   248
		for ($i = 0, $size = sizeof($text_blocks); $i < $size; $i++)
indvdum@0
   249
		{
indvdum@0
   250
			$trim_check_text = trim($text_blocks[$i]);
indvdum@0
   251
			$template_php .= (!$no_echo) ? (($trim_check_text != '') ? $text_blocks[$i] : '') . ((isset($compile_blocks[$i])) ? $compile_blocks[$i] : '') : (($trim_check_text != '') ? $text_blocks[$i] : '') . ((isset($compile_blocks[$i])) ? $compile_blocks[$i] : '');
indvdum@0
   252
		}
indvdum@0
   253
indvdum@0
   254
		// Remove unused opening/closing tags
indvdum@0
   255
		$template_php = str_replace(' ?><?php ', ' ', $template_php);
indvdum@0
   256
indvdum@0
   257
		// Now add a newline after each php closing tag which already has a newline
indvdum@0
   258
		// PHP itself strips a newline if a closing tag is used (this is documented behaviour) and it is mostly not intended by style authors to remove newlines
indvdum@0
   259
		$template_php = preg_replace('#\?\>([\r\n])#', '?>\1\1', $template_php);
indvdum@0
   260
indvdum@0
   261
		// There will be a number of occasions where we switch into and out of
indvdum@0
   262
		// PHP mode instantaneously. Rather than "burden" the parser with this
indvdum@0
   263
		// we'll strip out such occurences, minimising such switching
indvdum@0
   264
		if ($no_echo)
indvdum@0
   265
		{
indvdum@0
   266
			return "\$$echo_var .= '" . $template_php . "'";
indvdum@0
   267
		}
indvdum@0
   268
indvdum@0
   269
		return $template_php;
indvdum@0
   270
	}
indvdum@0
   271
indvdum@0
   272
	/**
indvdum@0
   273
	* Compile variables
indvdum@0
   274
	* @access private
indvdum@0
   275
	*/
indvdum@0
   276
	function compile_var_tags(&$text_blocks)
indvdum@0
   277
	{
indvdum@0
   278
		// change template varrefs into PHP varrefs
indvdum@0
   279
		$varrefs = array();
indvdum@0
   280
indvdum@0
   281
		// This one will handle varrefs WITH namespaces
indvdum@0
   282
		preg_match_all('#\{((?:[a-z0-9\-_]+\.)+)(\$)?([A-Z0-9\-_]+)\}#', $text_blocks, $varrefs, PREG_SET_ORDER);
indvdum@0
   283
indvdum@0
   284
		foreach ($varrefs as $var_val)
indvdum@0
   285
		{
indvdum@0
   286
			$namespace = $var_val[1];
indvdum@0
   287
			$varname = $var_val[3];
indvdum@0
   288
			$new = $this->generate_block_varref($namespace, $varname, true, $var_val[2]);
indvdum@0
   289
indvdum@0
   290
			$text_blocks = str_replace($var_val[0], $new, $text_blocks);
indvdum@0
   291
		}
indvdum@0
   292
indvdum@0
   293
		// This will handle the remaining root-level varrefs
indvdum@0
   294
		// transform vars prefixed by L_ into their language variable pendant if nothing is set within the tpldata array
indvdum@0
   295
		if (strpos($text_blocks, '{L_') !== false)
indvdum@0
   296
		{
indvdum@0
   297
			$text_blocks = preg_replace('#\{L_([A-Z0-9\-_]+)\}#', "<?php echo ((isset(\$this->_rootref['L_\\1'])) ? \$this->_rootref['L_\\1'] : ((isset(\$user->lang['\\1'])) ? \$user->lang['\\1'] : '{ \\1 }')); ?>", $text_blocks);
indvdum@0
   298
		}
indvdum@0
   299
indvdum@0
   300
		// Handle addslashed language variables prefixed with LA_
indvdum@0
   301
		// If a template variable already exist, it will be used in favor of it...
indvdum@0
   302
		if (strpos($text_blocks, '{LA_') !== false)
indvdum@0
   303
		{
indvdum@0
   304
			$text_blocks = preg_replace('#\{LA_([A-Z0-9\-_]+)\}#', "<?php echo ((isset(\$this->_rootref['LA_\\1'])) ? \$this->_rootref['LA_\\1'] : ((isset(\$this->_rootref['L_\\1'])) ? addslashes(\$this->_rootref['L_\\1']) : ((isset(\$user->lang['\\1'])) ? addslashes(\$user->lang['\\1']) : '{ \\1 }'))); ?>", $text_blocks);
indvdum@0
   305
		}
indvdum@0
   306
indvdum@0
   307
		// Handle remaining varrefs
indvdum@0
   308
		$text_blocks = preg_replace('#\{([A-Z0-9\-_]+)\}#', "<?php echo (isset(\$this->_rootref['\\1'])) ? \$this->_rootref['\\1'] : ''; ?>", $text_blocks);
indvdum@0
   309
		$text_blocks = preg_replace('#\{\$([A-Z0-9\-_]+)\}#', "<?php echo (isset(\$this->_tpldata['DEFINE']['.']['\\1'])) ? \$this->_tpldata['DEFINE']['.']['\\1'] : ''; ?>", $text_blocks);
indvdum@0
   310
indvdum@0
   311
		return;
indvdum@0
   312
	}
indvdum@0
   313
indvdum@0
   314
	/**
indvdum@0
   315
	* Compile blocks
indvdum@0
   316
	* @access private
indvdum@0
   317
	*/
indvdum@0
   318
	function compile_tag_block($tag_args)
indvdum@0
   319
	{
indvdum@0
   320
		$no_nesting = false;
indvdum@0
   321
indvdum@0
   322
		// Is the designer wanting to call another loop in a loop?
indvdum@0
   323
		if (strpos($tag_args, '!') === 0)
indvdum@0
   324
		{
indvdum@0
   325
			// Count the number of ! occurrences (not allowed in vars)
indvdum@0
   326
			$no_nesting = substr_count($tag_args, '!');
indvdum@0
   327
			$tag_args = substr($tag_args, $no_nesting);
indvdum@0
   328
		}
indvdum@0
   329
indvdum@0
   330
		// Allow for control of looping (indexes start from zero):
indvdum@0
   331
		// foo(2)    : Will start the loop on the 3rd entry
indvdum@0
   332
		// foo(-2)   : Will start the loop two entries from the end
indvdum@0
   333
		// foo(3,4)  : Will start the loop on the fourth entry and end it on the fifth
indvdum@0
   334
		// foo(3,-4) : Will start the loop on the fourth entry and end it four from last
indvdum@0
   335
		if (preg_match('#^([^()]*)\(([\-\d]+)(?:,([\-\d]+))?\)$#', $tag_args, $match))
indvdum@0
   336
		{
indvdum@0
   337
			$tag_args = $match[1];
indvdum@0
   338
indvdum@0
   339
			if ($match[2] < 0)
indvdum@0
   340
			{
indvdum@0
   341
				$loop_start = '($_' . $tag_args . '_count ' . $match[2] . ' < 0 ? 0 : $_' . $tag_args . '_count ' . $match[2] . ')';
indvdum@0
   342
			}
indvdum@0
   343
			else
indvdum@0
   344
			{
indvdum@0
   345
				$loop_start = '($_' . $tag_args . '_count < ' . $match[2] . ' ? $_' . $tag_args . '_count : ' . $match[2] . ')';
indvdum@0
   346
			}
indvdum@0
   347
indvdum@0
   348
			if (strlen($match[3]) < 1 || $match[3] == -1)
indvdum@0
   349
			{
indvdum@0
   350
				$loop_end = '$_' . $tag_args . '_count';
indvdum@0
   351
			}
indvdum@0
   352
			else if ($match[3] >= 0)
indvdum@0
   353
			{
indvdum@0
   354
				$loop_end = '(' . ($match[3] + 1) . ' > $_' . $tag_args . '_count ? $_' . $tag_args . '_count : ' . ($match[3] + 1) . ')';
indvdum@0
   355
			}
indvdum@0
   356
			else //if ($match[3] < -1)
indvdum@0
   357
			{
indvdum@0
   358
				$loop_end = '$_' . $tag_args . '_count' . ($match[3] + 1);
indvdum@0
   359
			}
indvdum@0
   360
		}
indvdum@0
   361
		else
indvdum@0
   362
		{
indvdum@0
   363
			$loop_start = 0;
indvdum@0
   364
			$loop_end = '$_' . $tag_args . '_count';
indvdum@0
   365
		}
indvdum@0
   366
indvdum@0
   367
		$tag_template_php = '';
indvdum@0
   368
		array_push($this->block_names, $tag_args);
indvdum@0
   369
indvdum@0
   370
		if ($no_nesting !== false)
indvdum@0
   371
		{
indvdum@0
   372
			// We need to implode $no_nesting times from the end...
indvdum@0
   373
			$block = array_slice($this->block_names, -$no_nesting);
indvdum@0
   374
		}
indvdum@0
   375
		else
indvdum@0
   376
		{
indvdum@0
   377
			$block = $this->block_names;
indvdum@0
   378
		}
indvdum@0
   379
indvdum@0
   380
		if (sizeof($block) < 2)
indvdum@0
   381
		{
indvdum@0
   382
			// Block is not nested.
indvdum@0
   383
			$tag_template_php = '$_' . $tag_args . "_count = (isset(\$this->_tpldata['$tag_args'])) ? sizeof(\$this->_tpldata['$tag_args']) : 0;";
indvdum@0
   384
			$varref = "\$this->_tpldata['$tag_args']";
indvdum@0
   385
		}
indvdum@0
   386
		else
indvdum@0
   387
		{
indvdum@0
   388
			// This block is nested.
indvdum@0
   389
			// Generate a namespace string for this block.
indvdum@0
   390
			$namespace = implode('.', $block);
indvdum@0
   391
indvdum@0
   392
			// Get a reference to the data array for this block that depends on the
indvdum@0
   393
			// current indices of all parent blocks.
indvdum@0
   394
			$varref = $this->generate_block_data_ref($namespace, false);
indvdum@0
   395
indvdum@0
   396
			// Create the for loop code to iterate over this block.
indvdum@0
   397
			$tag_template_php = '$_' . $tag_args . '_count = (isset(' . $varref . ')) ? sizeof(' . $varref . ') : 0;';
indvdum@0
   398
		}
indvdum@0
   399
indvdum@0
   400
		$tag_template_php .= 'if ($_' . $tag_args . '_count) {';
indvdum@0
   401
indvdum@0
   402
		/**
indvdum@0
   403
		* The following uses foreach for iteration instead of a for loop, foreach is faster but requires PHP to make a copy of the contents of the array which uses more memory
indvdum@0
   404
		* <code>
indvdum@0
   405
		*	if (!$offset)
indvdum@0
   406
		*	{
indvdum@0
   407
		*		$tag_template_php .= 'foreach (' . $varref . ' as $_' . $tag_args . '_i => $_' . $tag_args . '_val){';
indvdum@0
   408
		*	}
indvdum@0
   409
		* </code>
indvdum@0
   410
		*/
indvdum@0
   411
indvdum@0
   412
		$tag_template_php .= 'for ($_' . $tag_args . '_i = ' . $loop_start . '; $_' . $tag_args . '_i < ' . $loop_end . '; ++$_' . $tag_args . '_i){';
indvdum@0
   413
		$tag_template_php .= '$_'. $tag_args . '_val = &' . $varref . '[$_'. $tag_args. '_i];';
indvdum@0
   414
indvdum@0
   415
		return $tag_template_php;
indvdum@0
   416
	}
indvdum@0
   417
indvdum@0
   418
	/**
indvdum@0
   419
	* Compile IF tags - much of this is from Smarty with
indvdum@0
   420
	* some adaptions for our block level methods
indvdum@0
   421
	* @access private
indvdum@0
   422
	*/
indvdum@0
   423
	function compile_tag_if($tag_args, $elseif)
indvdum@0
   424
	{
indvdum@0
   425
		// Tokenize args for 'if' tag.
indvdum@0
   426
		preg_match_all('/(?:
indvdum@0
   427
			"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"         |
indvdum@0
   428
			\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'     |
indvdum@0
   429
			[(),]                                  |
indvdum@0
   430
			[^\s(),]+)/x', $tag_args, $match);
indvdum@0
   431
indvdum@0
   432
		$tokens = $match[0];
indvdum@0
   433
		$is_arg_stack = array();
indvdum@0
   434
indvdum@0
   435
		for ($i = 0, $size = sizeof($tokens); $i < $size; $i++)
indvdum@0
   436
		{
indvdum@0
   437
			$token = &$tokens[$i];
indvdum@0
   438
indvdum@0
   439
			switch ($token)
indvdum@0
   440
			{
indvdum@0
   441
				case '!==':
indvdum@0
   442
				case '===':
indvdum@0
   443
				case '<<':
indvdum@0
   444
				case '>>':
indvdum@0
   445
				case '|':
indvdum@0
   446
				case '^':
indvdum@0
   447
				case '&':
indvdum@0
   448
				case '~':
indvdum@0
   449
				case ')':
indvdum@0
   450
				case ',':
indvdum@0
   451
				case '+':
indvdum@0
   452
				case '-':
indvdum@0
   453
				case '*':
indvdum@0
   454
				case '/':
indvdum@0
   455
				case '@':
indvdum@0
   456
				break;
indvdum@0
   457
indvdum@0
   458
				case '==':
indvdum@0
   459
				case 'eq':
indvdum@0
   460
					$token = '==';
indvdum@0
   461
				break;
indvdum@0
   462
indvdum@0
   463
				case '!=':
indvdum@0
   464
				case '<>':
indvdum@0
   465
				case 'ne':
indvdum@0
   466
				case 'neq':
indvdum@0
   467
					$token = '!=';
indvdum@0
   468
				break;
indvdum@0
   469
indvdum@0
   470
				case '<':
indvdum@0
   471
				case 'lt':
indvdum@0
   472
					$token = '<';
indvdum@0
   473
				break;
indvdum@0
   474
indvdum@0
   475
				case '<=':
indvdum@0
   476
				case 'le':
indvdum@0
   477
				case 'lte':
indvdum@0
   478
					$token = '<=';
indvdum@0
   479
				break;
indvdum@0
   480
indvdum@0
   481
				case '>':
indvdum@0
   482
				case 'gt':
indvdum@0
   483
					$token = '>';
indvdum@0
   484
				break;
indvdum@0
   485
indvdum@0
   486
				case '>=':
indvdum@0
   487
				case 'ge':
indvdum@0
   488
				case 'gte':
indvdum@0
   489
					$token = '>=';
indvdum@0
   490
				break;
indvdum@0
   491
indvdum@0
   492
				case '&&':
indvdum@0
   493
				case 'and':
indvdum@0
   494
					$token = '&&';
indvdum@0
   495
				break;
indvdum@0
   496
indvdum@0
   497
				case '||':
indvdum@0
   498
				case 'or':
indvdum@0
   499
					$token = '||';
indvdum@0
   500
				break;
indvdum@0
   501
indvdum@0
   502
				case '!':
indvdum@0
   503
				case 'not':
indvdum@0
   504
					$token = '!';
indvdum@0
   505
				break;
indvdum@0
   506
indvdum@0
   507
				case '%':
indvdum@0
   508
				case 'mod':
indvdum@0
   509
					$token = '%';
indvdum@0
   510
				break;
indvdum@0
   511
indvdum@0
   512
				case '(':
indvdum@0
   513
					array_push($is_arg_stack, $i);
indvdum@0
   514
				break;
indvdum@0
   515
indvdum@0
   516
				case 'is':
indvdum@0
   517
					$is_arg_start = ($tokens[$i-1] == ')') ? array_pop($is_arg_stack) : $i-1;
indvdum@0
   518
					$is_arg	= implode('	', array_slice($tokens,	$is_arg_start, $i -	$is_arg_start));
indvdum@0
   519
indvdum@0
   520
					$new_tokens	= $this->_parse_is_expr($is_arg, array_slice($tokens, $i+1));
indvdum@0
   521
indvdum@0
   522
					array_splice($tokens, $is_arg_start, sizeof($tokens), $new_tokens);
indvdum@0
   523
indvdum@0
   524
					$i = $is_arg_start;
indvdum@0
   525
indvdum@0
   526
				// no break
indvdum@0
   527
indvdum@0
   528
				default:
indvdum@0
   529
					if (preg_match('#^((?:[a-z0-9\-_]+\.)+)?(\$)?(?=[A-Z])([A-Z0-9\-_]+)#s', $token, $varrefs))
indvdum@0
   530
					{
indvdum@0
   531
						$token = (!empty($varrefs[1])) ? $this->generate_block_data_ref(substr($varrefs[1], 0, -1), true, $varrefs[2]) . '[\'' . $varrefs[3] . '\']' : (($varrefs[2]) ? '$this->_tpldata[\'DEFINE\'][\'.\'][\'' . $varrefs[3] . '\']' : '$this->_rootref[\'' . $varrefs[3] . '\']');
indvdum@0
   532
					}
indvdum@0
   533
					else if (preg_match('#^\.((?:[a-z0-9\-_]+\.?)+)$#s', $token, $varrefs))
indvdum@0
   534
					{
indvdum@0
   535
						// Allow checking if loops are set with .loopname
indvdum@0
   536
						// It is also possible to check the loop count by doing <!-- IF .loopname > 1 --> for example
indvdum@0
   537
						$blocks = explode('.', $varrefs[1]);
indvdum@0
   538
indvdum@0
   539
						// If the block is nested, we have a reference that we can grab.
indvdum@0
   540
						// If the block is not nested, we just go and grab the block from _tpldata
indvdum@0
   541
						if (sizeof($blocks) > 1)
indvdum@0
   542
						{
indvdum@0
   543
							$block = array_pop($blocks);
indvdum@0
   544
							$namespace = implode('.', $blocks);
indvdum@0
   545
							$varref = $this->generate_block_data_ref($namespace, true);
indvdum@0
   546
indvdum@0
   547
							// Add the block reference for the last child.
indvdum@0
   548
							$varref .= "['" . $block . "']";
indvdum@0
   549
						}
indvdum@0
   550
						else
indvdum@0
   551
						{
indvdum@0
   552
							$varref = '$this->_tpldata';
indvdum@0
   553
indvdum@0
   554
							// Add the block reference for the last child.
indvdum@0
   555
							$varref .= "['" . $blocks[0] . "']";
indvdum@0
   556
						}
indvdum@0
   557
						$token = "sizeof($varref)";
indvdum@0
   558
					}
indvdum@0
   559
					else if (!empty($token))
indvdum@0
   560
					{
indvdum@0
   561
						$token = '(' . $token . ')';
indvdum@0
   562
					}
indvdum@0
   563
indvdum@0
   564
				break;
indvdum@0
   565
			}
indvdum@0
   566
		}
indvdum@0
   567
indvdum@0
   568
		// If there are no valid tokens left or only control/compare characters left, we do skip this statement
indvdum@0
   569
		if (!sizeof($tokens) || str_replace(array(' ', '=', '!', '<', '>', '&', '|', '%', '(', ')'), '', implode('', $tokens)) == '')
indvdum@0
   570
		{
indvdum@0
   571
			$tokens = array('false');
indvdum@0
   572
		}
indvdum@0
   573
		return (($elseif) ? '} else if (' : 'if (') . (implode(' ', $tokens) . ') { ');
indvdum@0
   574
	}
indvdum@0
   575
indvdum@0
   576
	/**
indvdum@0
   577
	* Compile DEFINE tags
indvdum@0
   578
	* @access private
indvdum@0
   579
	*/
indvdum@0
   580
	function compile_tag_define($tag_args, $op)
indvdum@0
   581
	{
indvdum@0
   582
		preg_match('#^((?:[a-z0-9\-_]+\.)+)?\$(?=[A-Z])([A-Z0-9_\-]*)(?: = (\'?)([^\']*)(\'?))?$#', $tag_args, $match);
indvdum@0
   583
indvdum@0
   584
		if (empty($match[2]) || (!isset($match[4]) && $op))
indvdum@0
   585
		{
indvdum@0
   586
			return '';
indvdum@0
   587
		}
indvdum@0
   588
indvdum@0
   589
		if (!$op)
indvdum@0
   590
		{
indvdum@0
   591
			return 'unset(' . (($match[1]) ? $this->generate_block_data_ref(substr($match[1], 0, -1), true, true) . '[\'' . $match[2] . '\']' : '$this->_tpldata[\'DEFINE\'][\'.\'][\'' . $match[2] . '\']') . ');';
indvdum@0
   592
		}
indvdum@0
   593
indvdum@0
   594
		// Are we a string?
indvdum@0
   595
		if ($match[3] && $match[5])
indvdum@0
   596
		{
indvdum@0
   597
			$match[4] = str_replace(array('\\\'', '\\\\', '\''), array('\'', '\\', '\\\''), $match[4]);
indvdum@0
   598
indvdum@0
   599
			// Compile reference, we allow template variables in defines...
indvdum@0
   600
			$match[4] = $this->compile($match[4]);
indvdum@0
   601
indvdum@0
   602
			// Now replace the php code
indvdum@0
   603
			$match[4] = "'" . str_replace(array('<?php echo ', '; ?>'), array("' . ", " . '"), $match[4]) . "'";
indvdum@0
   604
		}
indvdum@0
   605
		else
indvdum@0
   606
		{
indvdum@0
   607
			preg_match('#true|false|\.#i', $match[4], $type);
indvdum@0
   608
indvdum@0
   609
			switch (strtolower($type[0]))
indvdum@0
   610
			{
indvdum@0
   611
				case 'true':
indvdum@0
   612
				case 'false':
indvdum@0
   613
					$match[4] = strtoupper($match[4]);
indvdum@0
   614
				break;
indvdum@0
   615
indvdum@0
   616
				case '.':
indvdum@0
   617
					$match[4] = doubleval($match[4]);
indvdum@0
   618
				break;
indvdum@0
   619
indvdum@0
   620
				default:
indvdum@0
   621
					$match[4] = intval($match[4]);
indvdum@0
   622
				break;
indvdum@0
   623
			}
indvdum@0
   624
		}
indvdum@0
   625
indvdum@0
   626
		return (($match[1]) ? $this->generate_block_data_ref(substr($match[1], 0, -1), true, true) . '[\'' . $match[2] . '\']' : '$this->_tpldata[\'DEFINE\'][\'.\'][\'' . $match[2] . '\']') . ' = ' . $match[4] . ';';
indvdum@0
   627
	}
indvdum@0
   628
indvdum@0
   629
	/**
indvdum@0
   630
	* Compile INCLUDE tag
indvdum@0
   631
	* @access private
indvdum@0
   632
	*/
indvdum@0
   633
	function compile_tag_include($tag_args)
indvdum@0
   634
	{
indvdum@0
   635
		// Process dynamic includes
indvdum@0
   636
		if ($tag_args[0] == '$')
indvdum@0
   637
		{
indvdum@0
   638
			return "if (isset($tag_args)) { \$this->_tpl_include($tag_args); }";
indvdum@0
   639
		}
indvdum@0
   640
indvdum@0
   641
		return "\$this->_tpl_include('$tag_args');";
indvdum@0
   642
	}
indvdum@0
   643
indvdum@0
   644
	/**
indvdum@0
   645
	* Compile INCLUDE_PHP tag
indvdum@0
   646
	* @access private
indvdum@0
   647
	*/
indvdum@0
   648
	function compile_tag_include_php($tag_args)
indvdum@0
   649
	{
indvdum@0
   650
		return "\$this->_php_include('$tag_args');";
indvdum@0
   651
	}
indvdum@0
   652
indvdum@0
   653
	/**
indvdum@0
   654
	* parse expression
indvdum@0
   655
	* This is from Smarty
indvdum@0
   656
	* @access private
indvdum@0
   657
	*/
indvdum@0
   658
	function _parse_is_expr($is_arg, $tokens)
indvdum@0
   659
	{
indvdum@0
   660
		$expr_end = 0;
indvdum@0
   661
		$negate_expr = false;
indvdum@0
   662
indvdum@0
   663
		if (($first_token = array_shift($tokens)) == 'not')
indvdum@0
   664
		{
indvdum@0
   665
			$negate_expr = true;
indvdum@0
   666
			$expr_type = array_shift($tokens);
indvdum@0
   667
		}
indvdum@0
   668
		else
indvdum@0
   669
		{
indvdum@0
   670
			$expr_type = $first_token;
indvdum@0
   671
		}
indvdum@0
   672
indvdum@0
   673
		switch ($expr_type)
indvdum@0
   674
		{
indvdum@0
   675
			case 'even':
indvdum@0
   676
				if (@$tokens[$expr_end] == 'by')
indvdum@0
   677
				{
indvdum@0
   678
					$expr_end++;
indvdum@0
   679
					$expr_arg = $tokens[$expr_end++];
indvdum@0
   680
					$expr = "!(($is_arg / $expr_arg) % $expr_arg)";
indvdum@0
   681
				}
indvdum@0
   682
				else
indvdum@0
   683
				{
indvdum@0
   684
					$expr = "!($is_arg & 1)";
indvdum@0
   685
				}
indvdum@0
   686
			break;
indvdum@0
   687
indvdum@0
   688
			case 'odd':
indvdum@0
   689
				if (@$tokens[$expr_end] == 'by')
indvdum@0
   690
				{
indvdum@0
   691
					$expr_end++;
indvdum@0
   692
					$expr_arg = $tokens[$expr_end++];
indvdum@0
   693
					$expr = "(($is_arg / $expr_arg) % $expr_arg)";
indvdum@0
   694
				}
indvdum@0
   695
				else
indvdum@0
   696
				{
indvdum@0
   697
					$expr = "($is_arg & 1)";
indvdum@0
   698
				}
indvdum@0
   699
			break;
indvdum@0
   700
indvdum@0
   701
			case 'div':
indvdum@0
   702
				if (@$tokens[$expr_end] == 'by')
indvdum@0
   703
				{
indvdum@0
   704
					$expr_end++;
indvdum@0
   705
					$expr_arg = $tokens[$expr_end++];
indvdum@0
   706
					$expr = "!($is_arg % $expr_arg)";
indvdum@0
   707
				}
indvdum@0
   708
			break;
indvdum@0
   709
		}
indvdum@0
   710
indvdum@0
   711
		if ($negate_expr)
indvdum@0
   712
		{
indvdum@0
   713
			$expr = "!($expr)";
indvdum@0
   714
		}
indvdum@0
   715
indvdum@0
   716
		array_splice($tokens, 0, $expr_end, $expr);
indvdum@0
   717
indvdum@0
   718
		return $tokens;
indvdum@0
   719
	}
indvdum@0
   720
indvdum@0
   721
	/**
indvdum@0
   722
	* Generates a reference to the given variable inside the given (possibly nested)
indvdum@0
   723
	* block namespace. This is a string of the form:
indvdum@0
   724
	* ' . $this->_tpldata['parent'][$_parent_i]['$child1'][$_child1_i]['$child2'][$_child2_i]...['varname'] . '
indvdum@0
   725
	* It's ready to be inserted into an "echo" line in one of the templates.
indvdum@0
   726
	* NOTE: expects a trailing "." on the namespace.
indvdum@0
   727
	* @access private
indvdum@0
   728
	*/
indvdum@0
   729
	function generate_block_varref($namespace, $varname, $echo = true, $defop = false)
indvdum@0
   730
	{
indvdum@0
   731
		// Strip the trailing period.
indvdum@0
   732
		$namespace = substr($namespace, 0, -1);
indvdum@0
   733
indvdum@0
   734
		// Get a reference to the data block for this namespace.
indvdum@0
   735
		$varref = $this->generate_block_data_ref($namespace, true, $defop);
indvdum@0
   736
		// Prepend the necessary code to stick this in an echo line.
indvdum@0
   737
indvdum@0
   738
		// Append the variable reference.
indvdum@0
   739
		$varref .= "['$varname']";
indvdum@0
   740
		$varref = ($echo) ? "<?php echo $varref; ?>" : ((isset($varref)) ? $varref : '');
indvdum@0
   741
indvdum@0
   742
		return $varref;
indvdum@0
   743
	}
indvdum@0
   744
indvdum@0
   745
	/**
indvdum@0
   746
	* Generates a reference to the array of data values for the given
indvdum@0
   747
	* (possibly nested) block namespace. This is a string of the form:
indvdum@0
   748
	* $this->_tpldata['parent'][$_parent_i]['$child1'][$_child1_i]['$child2'][$_child2_i]...['$childN']
indvdum@0
   749
	*
indvdum@0
   750
	* If $include_last_iterator is true, then [$_childN_i] will be appended to the form shown above.
indvdum@0
   751
	* NOTE: does not expect a trailing "." on the blockname.
indvdum@0
   752
	* @access private
indvdum@0
   753
	*/
indvdum@0
   754
	function generate_block_data_ref($blockname, $include_last_iterator, $defop = false)
indvdum@0
   755
	{
indvdum@0
   756
		// Get an array of the blocks involved.
indvdum@0
   757
		$blocks = explode('.', $blockname);
indvdum@0
   758
		$blockcount = sizeof($blocks) - 1;
indvdum@0
   759
indvdum@0
   760
		// DEFINE is not an element of any referenced variable, we must use _tpldata to access it
indvdum@0
   761
		if ($defop)
indvdum@0
   762
		{
indvdum@0
   763
			$varref = '$this->_tpldata[\'DEFINE\']';
indvdum@0
   764
			// Build up the string with everything but the last child.
indvdum@0
   765
			for ($i = 0; $i < $blockcount; $i++)
indvdum@0
   766
			{
indvdum@0
   767
				$varref .= "['" . $blocks[$i] . "'][\$_" . $blocks[$i] . '_i]';
indvdum@0
   768
			}
indvdum@0
   769
			// Add the block reference for the last child.
indvdum@0
   770
			$varref .= "['" . $blocks[$blockcount] . "']";
indvdum@0
   771
			// Add the iterator for the last child if requried.
indvdum@0
   772
			if ($include_last_iterator)
indvdum@0
   773
			{
indvdum@0
   774
				$varref .= '[$_' . $blocks[$blockcount] . '_i]';
indvdum@0
   775
			}
indvdum@0
   776
			return $varref;
indvdum@0
   777
		}
indvdum@0
   778
		else if ($include_last_iterator)
indvdum@0
   779
		{
indvdum@0
   780
			return '$_'. $blocks[$blockcount] . '_val';
indvdum@0
   781
		}
indvdum@0
   782
		else
indvdum@0
   783
		{
indvdum@0
   784
			return '$_'. $blocks[$blockcount - 1] . '_val[\''. $blocks[$blockcount]. '\']';
indvdum@0
   785
		}
indvdum@0
   786
	}
indvdum@0
   787
indvdum@0
   788
	/**
indvdum@0
   789
	* Write compiled file to cache directory
indvdum@0
   790
	* @access private
indvdum@0
   791
	*/
indvdum@0
   792
	function compile_write($handle, $data)
indvdum@0
   793
	{
indvdum@0
   794
		global $phpEx;
indvdum@0
   795
indvdum@0
   796
		$filename = $this->template->cachepath . str_replace('/', '.', $this->template->filename[$handle]) . '.' . $phpEx;
indvdum@0
   797
indvdum@0
   798
		$data = "<?php if (!defined('IN_PHPBB')) exit;" . ((strpos($data, '<?php') === 0) ? substr($data, 5) : ' ?>' . $data);
indvdum@0
   799
indvdum@0
   800
		if ($fp = @fopen($filename, 'wb'))
indvdum@0
   801
		{
indvdum@0
   802
			@flock($fp, LOCK_EX);
indvdum@0
   803
			@fwrite ($fp, $data);
indvdum@0
   804
			@flock($fp, LOCK_UN);
indvdum@0
   805
			@fclose($fp);
indvdum@0
   806
indvdum@0
   807
			phpbb_chmod($filename, CHMOD_READ | CHMOD_WRITE);
indvdum@0
   808
		}
indvdum@0
   809
indvdum@0
   810
		return;
indvdum@0
   811
	}
indvdum@0
   812
}
indvdum@0
   813
indvdum@0
   814
?>