template_file_cache.php

来自「这是php编的论坛的原代码」· PHP 代码 · 共 528 行 · 第 1/2 页

PHP
528
字号
<?php
/***************************************************************************
 *                              template.inc
 *                            -------------------
 *   begin                : Saturday, Feb 13, 2001
 *   copyright            : (C) 2001 The phpBB Group
 *   email                : support@phpbb.com
 *
 *   $Id: template_file_cache.php,v 1.1.1.1 2003/02/11 22:27:32 wei.gao Exp $
 *
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

/**
 * Template class. By Nathan Codding of the phpBB group.
 * The interface was originally inspired by PHPLib templates,
 * and the template file formats are quite similar.
 *
 */

class Template {
	var $classname = 'Template';

	// variable that holds all the data we'll be substituting into
	// the compiled templates.
	// ...
	// This will end up being a multi-dimensional array like this:
	// $this->_tpldata[block.][iteration#][child.][iteration#][child2.][iteration#][variablename] == value
	// if it's a root-level variable, it'll be like this:
	// $this->_tpldata[.][0][varname] == value
	var $_tpldata = array();

	// Hash of filenames for each template handle.
	var $files = array();

	// Root template directory.
	var $root = '';

	// this will hash handle names to the compiled code for that handle.
	var $compiled_code = array();

	// This will hold the uncompiled code for that handle.
	var $uncompiled_code = array();

	/**
	 * Constructor. Simply sets the root dir.
	 *
	 */
	function Template($root = '.')
	{
		global $board_config, $db;

		$this->set_rootdir($root);
		$this->db = $db;
	}

	/**
	 * Destroys this template object. Should be called when you're done with it, in order
	 * to clear out the template data so you can load/parse a new template set.
	 */
	function destroy()
	{
		$this->_tpldata = array();
	}

	/**
	 * Sets the template root directory for this Template object.
	 */
	function set_rootdir($dir)
	{
		global $phpbb_root_path;

		if (!is_dir($dir))
		{
			return false;
		}
//		echo "<br />" . 
		$this->root = phpbb_realpath($dir);
        $this->cachedir = phpbb_realpath($dir . '/cache/');
		return true;
	}

	/**
	 * Sets the template filenames for handles. $filename_array
	 * should be a hash of handle => filename pairs.
	 */
	function set_filenames($filename_array)
	{
		if ( !is_array($filename_array) )
		{
			return false;
		}

		$template_names = '';
		@reset($filename_array);
		while ( list($handle, $filename) = @each($filename_array) )
		{
			$this->filename[$handle] = $filename;
			$this->files[$handle] = $this->make_filename($filename);
		}

		return true;
	}


	/**
	 * Load the file for the handle, compile the file,
	 * and run the compiled code. This will print out
	 * the results of executing the template.
	 */
	function pparse($handle)
	{
		global $phpEx;
//echo "<br />" . 
		$cache_file = $this->cachedir . '/' . $this->filename[$handle] . '.' . $phpEx;

		if( @filemtime($cache_file) == @filemtime($this->files[$handle]) )
		{
			$_str = '';
			include($cache_file);

			if ( $_str != '' )
			{
				echo $_str;
			}
		}
		else 
		{
			if ( !$this->loadfile($handle) )
			{
				die("Template->pparse(): Couldn't load template file for handle $handle");
			}

			//
			// Actually compile the code now.
			//
			$this->compiled_code[$handle] = $this->compile($this->uncompiled_code[$handle]);

			$fp = fopen($cache_file, 'w+');
			fwrite ($fp, '<?php' . "\n" . $this->compiled_code[$handle] . "\n?" . '>');
			fclose($fp);

			touch($cache_file, filemtime($this->files[$handle]));
			@chmod($cache_file, 0777);

			eval($this->compiled_code[$handle]);
		}
	
		return true;
	}

	/**
	 * Inserts the uncompiled code for $handle as the
	 * value of $varname in the root-level. This can be used
	 * to effectively include a template in the middle of another
	 * template.
	 * Note that all desired assignments to the variables in $handle should be done
	 * BEFORE calling this function.
	 */
	function assign_var_from_handle($varname, $handle)
	{
		global $phpEx;

		$cache_file = $this->cachedir . $this->filename[$handle] . '.' . $phpEx;

		if( @filemtime($cache_file) == @filemtime($this->files[$handle]) )
		{
			$_str = '';
			include($cache_file);
		}
		else 
		{
			if ( !$this->loadfile($handle) )
			{
				die("Template->pparse(): Couldn't load template file for handle $handle");
			}

			$code = $this->compile($this->uncompiled_code[$handle], true, '_str');

			$fp = fopen($cache_file, 'w+');
			fwrite ($fp, '<?php' . "\n" . $code . "\n?" . '>');
			fclose($fp);

			touch($cache_file, filemtime($this->files[$handle]));
			@chmod($cache_file, 0777);

			// Compile It, With The "no Echo Statements" Option On.
			$_str = '';
			// evaluate the variable assignment.
			eval($code);
		
		}

		// assign the value of the generated variable to the given varname.
		$this->assign_var($varname, $_str);

		return true;
	}

	/**
	 * Block-level variable assignment. Adds a new block iteration with the given
	 * variable assignments. Note that this should only be called once per block
	 * iteration.
	 */
	function assign_block_vars($blockname, $vararray)
	{
		if (strstr($blockname, '.'))
		{
			// Nested block.
			$blocks = explode('.', $blockname);
			$blockcount = sizeof($blocks) - 1;
			$str = '$this->_tpldata';
			for ($i = 0; $i < $blockcount; $i++)
			{
				$str .= '[\'' . $blocks[$i] . '.\']';
				eval('$lastiteration = sizeof(' . $str . ') - 1;');
				$str .= '[' . $lastiteration . ']';
			}
			// Now we add the block that we're actually assigning to.
			// We're adding a new iteration to this block with the given
			// variable assignments.
			$str .= '[\'' . $blocks[$blockcount] . '.\'][] = $vararray;';

			// Now we evaluate this assignment we've built up.
			eval($str);
		}
		else
		{
			// Top-level block.
			// Add a new iteration to this block with the variable assignments
			// we were given.
			$this->_tpldata[$blockname . '.'][] = $vararray;
		}

		return true;
	}

	/**
	 * Root-level variable assignment. Adds to current assignments, overriding
	 * any existing variable assignment with the same name.
	 */
	function assign_vars($vararray)
	{
		reset ($vararray);
		while (list($key, $val) = each($vararray))
		{
			$this->_tpldata['.'][0][$key] = $val;
		}

		return true;
	}

	/**
	 * Root-level variable assignment. Adds to current assignments, overriding
	 * any existing variable assignment with the same name.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?