template_db_cache.php

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

PHP
534
字号
<?php
/***************************************************************************
 *                              template.inc
 *                            -------------------
 *   begin                : Saturday, Feb 13, 2001
 *   copyright            : (C) 2001 The phpBB Group
 *   email                : support@phpbb.com
 *
 *   $Id: template_db_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 = '.', $_board_config = false, $_db = false)
	{
		$this->set_rootdir($root);
		$this->db = $_db;

		$this->pparse_order = array();
	}

	/**
	 * 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)
	{
		if (!is_dir($dir))
		{
			return false;
		}

		$this->root = $dir;
		return true;
	}

	/**
	 * Sets the template filenames for handles. $filename_array
	 * should be a hash of handle => filename pairs.
	 */
	function set_filenames($filename_array)
	{
		global $table_prefix;

		if ( !is_array($filename_array) )
		{
			return false;
		}

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

		$sql = "SELECT * 
			FROM " . $table_prefix . "template_cache 
			WHERE template_name IN ($template_names)";
		if ( $result = $this->db->sql_query($sql) )
		{
			while( $row = $this->db->sql_fetchrow($result) )
			{
				if( $row['template_cached'] == filemtime($row['template_name']) )
				{
					$this->compiled_code[$row['template_handle']] = $row['template_compile'];
					$this->echo_compiled[$row['template_handle']] = $row['template_echo'];
				}
			} 
		} 

		$this->db->sql_freeresult();

		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 $table_prefix;

		if( empty($this->compiled_code[$handle]) )
		{
			if ( !$this->loadfile($handle) )
			{
				die("Template->pparse(): Couldn't load template file for handle $handle");
			}

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

			$sql = "REPLACE INTO " . $table_prefix . "template_cache (template_name, template_handle, template_cached, template_compile) VALUES ('" . addslashes($this->files[$handle]) . "', '" . addslashes($handle) . "', " . filemtime($this->files[$handle]) . ", '" . addslashes($this->compiled_code[$handle]) . "')";
			if ( !($result = $this->db->sql_query($sql)) )
			{
				die("Couldn't insert template into cache!");
			}

		}

		$_str = "";
		eval($this->compiled_code[$handle]);

		if( $_str != "" ) 
		{
			echo $_str;
		}

		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 $table_prefix;

		if( empty($this->compiled_code[$handle]) )
		{
			if ( !$this->loadfile($handle) )
			{
				die("Template->pparse(): Couldn't load template file for handle $handle");
			}

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

			$sql = "REPLACE INTO " . $table_prefix . "template_cache (template_name, template_handle, template_echo, template_cached, template_compile) VALUES ('" . addslashes($this->files[$handle]) . "', '" . addslashes($handle) . "', 0, " . filemtime($this->files[$handle]) . ", '" . addslashes($code) . "')";
			if ( !($result = $this->db->sql_query($sql)) )
			{
				die("Couldn't insert template into cache!");
			}
		}
		else
		{
			$code = $this->compiled_code[$handle];
		}

		// 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;
	}

	/**

⌨️ 快捷键说明

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