Files
smarty/libs/Smarty.class.php

525 lines
20 KiB
PHP
Raw Normal View History

2001-02-10 22:46:40 +00:00
<?php
2000-08-08 17:05:38 +00:00
/*
2001-02-01 21:39:29 +00:00
* Project: Smarty: the PHP compiling template engine
* File: Smarty.class.php
* Author: Monte Ohrt <monte@ispi.net>
* Andrei Zmievski <andrei@ispi.net>
2000-11-17 21:47:41 +00:00
*
2001-02-21 20:27:08 +00:00
* Version: 1.3.0
2001-01-25 23:07:36 +00:00
* Copyright: 2001 ispi of Lincoln, Inc.
2001-02-01 21:39:29 +00:00
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
2001-01-18 20:41:43 +00:00
*
* This library is distributed in the hope that it will be useful,
2001-01-18 20:41:43 +00:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
2001-01-18 20:41:43 +00:00
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2001-01-18 20:41:43 +00:00
*
* You may contact the authors of Smarty by e-mail at:
* monte@ispi.net
* andrei@ispi.net
*
* Or, write to:
* Monte Ohrt
* CTO, ispi
* 237 S. 70th suite 220
* Lincoln, NE 68510
*
* The latest version of Smarty can be obtained from:
* http://www.phpinsider.com/
2000-08-08 17:05:38 +00:00
*
*/
require('Smarty.addons.php');
2000-08-08 17:05:38 +00:00
define("SMARTY_PHP_PASSTHRU",0);
define("SMARTY_PHP_QUOTE",1);
define("SMARTY_PHP_REMOVE",2);
define("SMARTY_PHP_ALLOW",3);
2001-02-20 21:20:08 +00:00
2000-08-08 17:05:38 +00:00
class Smarty
{
2001-02-01 21:39:29 +00:00
// public vars
var $template_dir = './templates'; // name of directory for templates
var $compile_dir = './templates_c'; // name of directory for compiled templates
var $config_dir = './configs'; // directory where config files are located
2001-02-01 21:43:04 +00:00
2001-02-05 21:10:20 +00:00
var $global_assign = array('SCRIPT_NAME'); // variables from the GLOBALS array
// that are implicitly assigned
// to all templates
2001-02-01 21:39:29 +00:00
var $compile_check = true; // whether to check for compiling step or not:
// This is generally set to false once the
// application is entered into production and
// initially compiled. Leave set to true
2001-02-05 21:10:20 +00:00
// during development. true/false default true.
2001-02-01 21:39:29 +00:00
var $force_compile = false; // force templates to compile every time.
// if cache file exists, it will
2001-02-05 21:10:20 +00:00
// override compile_check and force_compile.
// true/false. default false.
2001-02-01 21:43:04 +00:00
var $caching = false; // whether to use caching or not. true/false
var $cache_dir = './cache'; // name of directory for template cache
2001-02-01 21:39:29 +00:00
var $cache_lifetime = 3600; // number of seconds cached content will persist.
// 0 = never expires. default is one hour (3600)
2001-02-05 21:10:20 +00:00
var $tpl_file_ext = '.tpl'; // template file extention
2001-02-01 21:39:29 +00:00
2001-02-20 21:20:08 +00:00
var $php_handling = SMARTY_PHP_PASSTHRU; // how smarty handles php tags
// possible values:
// SMARTY_PHP_PASSTHRU -> echo tags as is
// SMARTY_PHP_QUOTE -> escape tags as entities
// SMARTY_PHP_REMOVE -> remove php tags
// SMARTY_PHP_ALLOW -> execute php tags
// default: SMARTY_PHP_PASSTHRU
2000-08-08 17:05:38 +00:00
var $left_delimiter = '{'; // template tag delimiters.
var $right_delimiter = '}';
var $custom_funcs = array( 'html_options' => 'smarty_func_html_options',
'html_select_date' => 'smarty_func_html_select_date',
'math' => 'smarty_func_math',
'fetch' => 'smarty_func_fetch'
);
var $custom_mods = array( 'lower' => 'strtolower',
'upper' => 'strtoupper',
'capitalize' => 'ucwords',
'escape' => 'smarty_mod_escape',
'truncate' => 'smarty_mod_truncate',
'spacify' => 'smarty_mod_spacify',
'date_format' => 'smarty_mod_date_format',
'string_format' => 'smarty_mod_string_format',
'replace' => 'smarty_mod_replace',
'strip_tags' => 'smarty_mod_strip_tags',
'default' => 'smarty_mod_default'
);
2001-02-01 21:39:29 +00:00
// internal vars
var $_error_msg = false; // error messages. true/false
var $_tpl_vars = array();
2000-11-22 16:23:19 +00:00
/*======================================================================*\
2001-02-01 21:39:29 +00:00
Function: Smarty
Purpose: Constructor
2000-11-22 16:23:19 +00:00
\*======================================================================*/
2001-02-01 21:39:29 +00:00
function Smarty()
{
2001-03-02 18:07:51 +00:00
foreach ($this->global_assign as $var_name) {
if(isset($GLOBALS[$var_name])) {
$this->assign($var_name, $GLOBALS[$var_name]);
}
}
2001-02-01 21:39:29 +00:00
}
2000-11-22 16:23:19 +00:00
2000-08-08 17:05:38 +00:00
/*======================================================================*\
2001-02-01 21:39:29 +00:00
Function: assign()
Purpose: assigns values to template variables
2000-08-08 17:05:38 +00:00
\*======================================================================*/
2001-02-01 21:39:29 +00:00
function assign($tpl_var, $value = NULL)
{
if (is_array($tpl_var)){
foreach ($tpl_var as $key => $val) {
if (!empty($key))
$this->_tpl_vars[$key] = $val;
}
} else {
if (!empty($tpl_var) && isset($value))
$this->_tpl_vars[$tpl_var] = $value;
}
}
2000-11-21 15:21:16 +00:00
/*======================================================================*\
2001-02-01 21:39:29 +00:00
Function: append
Purpose: appens values to template variables
2000-11-21 15:21:16 +00:00
\*======================================================================*/
2001-02-01 21:39:29 +00:00
function append($tpl_var, $value = NULL)
{
if (is_array($tpl_var)) {
foreach ($tpl_var as $key => $val) {
if (!empty($key)) {
if (!is_array($this->_tpl_vars[$key]))
settype($this->_tpl_vars[$key], 'array');
$this->_tpl_vars[$key][] = $val;
}
}
} else {
if (!empty($tpl_var) && isset($value)) {
if (!is_array($this->_tpl_vars[$tpl_var]))
settype($this->_tpl_vars[$tpl_var], 'array');
$this->_tpl_vars[$tpl_var][] = $value;
}
}
}
2000-11-21 15:21:16 +00:00
2000-08-08 17:05:38 +00:00
/*======================================================================*\
2001-02-01 21:39:29 +00:00
Function: clear_assign()
Purpose: clear the given assigned template variable.
2000-08-08 17:05:38 +00:00
\*======================================================================*/
2001-02-01 21:39:29 +00:00
function clear_assign($tpl_var)
{
2001-02-08 14:18:25 +00:00
if(is_array($tpl_var))
foreach($tpl_var as $curr_var)
unset($this->_tpl_vars[$curr_var]);
else
unset($this->_tpl_vars[$tpl_var]);
2001-02-01 21:39:29 +00:00
}
2000-08-08 17:05:38 +00:00
2001-02-02 16:55:55 +00:00
/*======================================================================*\
2001-02-07 20:55:39 +00:00
Function: register_function
2001-02-02 16:55:55 +00:00
Purpose: Registers custom function to be used in templates
\*======================================================================*/
2001-02-07 20:55:39 +00:00
function register_function($function, $function_impl)
2001-02-02 16:55:55 +00:00
{
$this->custom_funcs[$function] = $function_impl;
}
/*======================================================================*\
Function: unregister_function
Purpose: Unregisters custom function
\*======================================================================*/
function unregister_function($function)
{
unset($this->custom_funcs[$function]);
}
2001-02-02 16:55:55 +00:00
/*======================================================================*\
2001-02-07 20:55:39 +00:00
Function: register_modifier
2001-02-02 16:55:55 +00:00
Purpose: Registers modifier to be used in templates
\*======================================================================*/
2001-02-07 20:55:39 +00:00
function register_modifier($modifier, $modifier_impl)
2001-02-02 16:55:55 +00:00
{
$this->custom_mods[$modifier] = $modifier_impl;
}
/*======================================================================*\
Function: unregister_modifier
Purpose: Unregisters modifier
\*======================================================================*/
function unregister_modifier($modifier)
{
unset($this->custom_mods[$modifier]);
}
2001-02-02 16:55:55 +00:00
2001-02-01 21:09:17 +00:00
/*======================================================================*\
2001-02-01 21:39:29 +00:00
Function: clear_cache()
2001-02-07 20:55:39 +00:00
Purpose: clear cached content for the given template and cache id
2001-02-01 21:09:17 +00:00
\*======================================================================*/
2001-02-07 20:55:39 +00:00
function clear_cache($tpl_file, $cache_id = null)
2001-02-01 21:39:29 +00:00
{
$cache_tpl_md5 = md5(realpath($this->template_dir.'/'.$tpl_file));
2001-02-07 20:55:39 +00:00
$cache_dir = $this->cache_dir.'/'.$cache_tpl_md5;
if (!is_dir($cache_dir))
return false;
if (isset($cache_id)) {
$cache_id_md5 = md5($cache_id);
$cache_id_dir = substr($cache_id_md5, 0, 2);
$cache_file = "$cache_dir/$cache_id_dir/{$cache_tpl_md5}_$cache_id_md5.cache";
return (bool)(is_file($cache_file) && unlink($cache_file));
} else
return $this->_clear_tpl_cache_dir($cache_tpl_md5);
2001-02-01 21:39:29 +00:00
}
2001-02-01 20:21:02 +00:00
/*======================================================================*\
2001-02-01 21:39:29 +00:00
Function: clear_all_cache()
Purpose: clear the entire contents of cache (all templates)
2001-02-01 20:21:02 +00:00
\*======================================================================*/
2001-02-01 21:39:29 +00:00
function clear_all_cache()
{
if (!is_dir($this->cache_dir))
2001-02-01 21:39:29 +00:00
return false;
$dir_handle = opendir($this->cache_dir);
while ($curr_dir = readdir($dir_handle)) {
if ($curr_dir == '.' || $curr_dir == '..' ||
!is_dir($this->cache_dir.'/'.$curr_dir))
2001-02-01 21:39:29 +00:00
continue;
$this->_clear_tpl_cache_dir($curr_dir);
2001-02-01 21:39:29 +00:00
}
closedir($dir_handle);
2001-02-07 20:55:39 +00:00
return true;
2001-02-01 21:39:29 +00:00
}
2001-02-05 21:10:20 +00:00
2001-02-07 20:55:39 +00:00
2001-02-05 21:10:20 +00:00
/*======================================================================*\
Function: is_cached()
Purpose: test to see if valid cache exists for this template
\*======================================================================*/
2001-02-07 20:55:39 +00:00
function is_cached($tpl_file, $cache_id = null)
2001-02-05 21:10:20 +00:00
{
2001-02-07 23:08:00 +00:00
if (!$this->caching)
return false;
2001-02-07 20:55:39 +00:00
// cache name = template path + cache_id
$cache_tpl_md5 = md5(realpath($this->template_dir.'/'.$tpl_file));
2001-02-07 20:55:39 +00:00
$cache_id_md5 = md5($cache_id);
$cache_id_dir = substr($cache_id_md5, 0, 2);
$cache_file = $this->cache_dir."/$cache_tpl_md5/$cache_id_dir/{$cache_tpl_md5}_$cache_id_md5.cache";
if (file_exists($cache_file) &&
($this->cache_lifetime == 0 ||
(time() - filemtime($cache_file) <= $this->cache_lifetime)))
return true;
else
return false;
2001-02-05 21:10:20 +00:00
}
2001-02-01 21:39:29 +00:00
2001-02-05 21:10:20 +00:00
2000-08-08 17:05:38 +00:00
/*======================================================================*\
2001-02-01 21:39:29 +00:00
Function: clear_all_assign()
Purpose: clear all the assigned template variables.
2000-08-08 17:05:38 +00:00
\*======================================================================*/
2001-02-01 21:39:29 +00:00
function clear_all_assign()
{
$this->_tpl_vars = array();
}
2000-08-08 17:05:38 +00:00
2000-11-21 15:21:16 +00:00
/*======================================================================*\
2001-02-01 21:39:29 +00:00
Function: get_template_vars
Purpose: Returns an array containing template variables
2000-11-21 15:21:16 +00:00
\*======================================================================*/
2001-02-01 21:39:29 +00:00
function &get_template_vars()
{
return $this->_tpl_vars;
}
2000-11-21 15:21:16 +00:00
2000-08-08 17:05:38 +00:00
/*======================================================================*\
2001-02-01 21:39:29 +00:00
Function: display()
Purpose: executes & displays the template results
2000-08-08 17:05:38 +00:00
\*======================================================================*/
2001-02-07 20:55:39 +00:00
function display($tpl_file, $cache_id = null)
2001-02-01 21:39:29 +00:00
{
2001-02-07 20:55:39 +00:00
$this->fetch($tpl_file, $cache_id, true);
2001-02-01 21:39:29 +00:00
}
2000-08-08 17:05:38 +00:00
/*======================================================================*\
2001-02-01 21:39:29 +00:00
Function: fetch()
Purpose: executes & returns or displays the template results
2000-08-08 17:05:38 +00:00
\*======================================================================*/
2001-02-07 20:55:39 +00:00
function fetch($tpl_file, $cache_id = null, $display = false)
2001-02-01 21:39:29 +00:00
{
global $HTTP_SERVER_VARS;
2001-02-01 21:39:29 +00:00
if ($this->caching) {
2001-02-07 20:55:39 +00:00
// cache name = template path + cache_id
$cache_tpl_md5 = md5(realpath($this->template_dir.'/'.$tpl_file));
2001-02-07 20:55:39 +00:00
$cache_id_md5 = md5($cache_id);
$cache_id_dir = substr($cache_id_md5, 0, 2);
$cache_file = $this->cache_dir."/$cache_tpl_md5/$cache_id_dir/{$cache_tpl_md5}_$cache_id_md5.cache";
2001-02-05 21:10:20 +00:00
if (file_exists($cache_file) &&
($this->cache_lifetime == 0 ||
(time() - filemtime($cache_file) <= $this->cache_lifetime))) {
$results = $this->_read_file($cache_file);
$results = $this->_process_cached_inserts($results);
if ($display) {
echo $results;
return;
} else
return $results;
2001-02-01 21:39:29 +00:00
}
}
// compile files
$this->_compile($this->template_dir);
//assemble compile directory path to file
$_compile_file = $this->compile_dir."/".$tpl_file.".php";
extract($this->_tpl_vars);
// if we just need to display the results, don't perform output
// buffering - for speed
if ($display && !$this->caching)
include($_compile_file);
else {
ob_start();
include($_compile_file);
$results = ob_get_contents();
ob_end_clean();
}
2001-02-01 21:39:29 +00:00
if($this->caching) {
2001-02-05 21:10:20 +00:00
$this->_write_file($cache_file, $results, true);
2001-02-01 21:39:29 +00:00
$results = $this->_process_cached_inserts($results);
}
if ($display) {
2001-03-02 18:07:51 +00:00
if(isset($results)) { echo $results; }
return;
2001-03-02 18:07:51 +00:00
} else {
if(isset($results)) { return $results; }
}
2001-02-01 21:39:29 +00:00
}
2000-08-08 17:05:38 +00:00
/*======================================================================*\
2001-02-01 21:39:29 +00:00
Function: compile()
Purpose: called to compile the templates
2000-08-08 17:05:38 +00:00
\*======================================================================*/
2001-02-01 21:39:29 +00:00
function _compile($tpl_dir)
{
if($this->compile_check || $this->force_compile)
{
include_once("Smarty_Compiler.class.php");
$smarty_compile = new Smarty_Compiler;
$smarty_compile->template_dir = $this->template_dir;
$smarty_compile->compile_dir = $this->compile_dir;
$smarty_compile->config_dir = $this->config_dir;
$smarty_compile->force_compile = $this->force_compile;
$smarty_compile->caching = $this->caching;
$smarty_compile->tpl_file_ext = $this->tpl_file_ext;
$smarty_compile->php_handling = $this->php_handling;
$smarty_compile->left_delimiter = $this->left_delimiter;
$smarty_compile->right_delimiter = $this->right_delimiter;
$smarty_compile->custom_funcs = $this->custom_funcs;
$smarty_compile->custom_mods = $this->custom_mods;
if($smarty_compile->_traverse_files($tpl_dir, 0))
2001-02-01 21:39:29 +00:00
return true;
else {
$this->_error_msg = $smarty_compile->_error_msg;
2001-02-01 21:39:29 +00:00
return false;
2001-02-10 22:46:40 +00:00
}
2001-02-01 21:39:29 +00:00
} else
return false;
2001-02-01 21:39:29 +00:00
}
2000-11-27 17:39:40 +00:00
/*======================================================================*\
2001-02-01 21:39:29 +00:00
Function: _dequote
Purpose: Remove starting and ending quotes from the string
2000-11-27 17:39:40 +00:00
\*======================================================================*/
2001-02-01 21:39:29 +00:00
function _dequote($string)
{
if (($string{0} == "'" || $string{0} == '"') &&
$string{strlen($string)-1} == $string{0})
return substr($string, 1, -1);
else
return $string;
}
2000-08-08 17:05:38 +00:00
/*======================================================================*\
2001-02-01 21:39:29 +00:00
Function: _read_file()
Purpose: read in a file
2000-08-08 17:05:38 +00:00
\*======================================================================*/
2001-02-01 21:39:29 +00:00
function _read_file($filename)
2001-02-07 23:08:00 +00:00
2001-02-01 21:39:29 +00:00
{
if (!($fd = fopen($filename, 'r'))) {
2001-02-01 21:39:29 +00:00
$this->_set_error_msg("problem reading '$filename.'");
return false;
}
2001-02-07 23:08:00 +00:00
flock($fd, LOCK_SH);
2001-02-01 21:39:29 +00:00
$contents = fread($fd, filesize($filename));
fclose($fd);
return $contents;
}
2000-08-08 17:05:38 +00:00
/*======================================================================*\
2001-02-01 21:39:29 +00:00
Function: _write_file()
Purpose: write out a file
2000-08-08 17:05:38 +00:00
\*======================================================================*/
function _write_file($filename, $contents, $create_dirs = false)
2001-02-01 21:39:29 +00:00
{
if($create_dirs)
$this->_create_dir_structure(dirname($filename));
2001-02-05 21:10:20 +00:00
2001-02-07 23:08:00 +00:00
if (!($fd = fopen($filename, 'a'))) {
2001-02-01 21:39:29 +00:00
$this->_set_error_msg("problem writing '$filename.'");
return false;
}
2001-02-07 23:08:00 +00:00
flock($fd, LOCK_EX);
$fd_safe = fopen($filename, 'w');
fwrite($fd_safe, $contents);
fclose($fd_safe);
2001-02-01 21:39:29 +00:00
fclose($fd);
2001-02-07 23:08:00 +00:00
2001-02-01 21:39:29 +00:00
return true;
2001-02-05 21:10:20 +00:00
}
/*======================================================================*\
Function: _clear_tpl_cache_dir
2001-02-07 20:55:39 +00:00
Purpose: Clear the specified template cache
\*======================================================================*/
2001-02-07 20:55:39 +00:00
function _clear_tpl_cache_dir($cache_tpl_md5)
{
2001-02-07 20:55:39 +00:00
$cache_dir = $this->cache_dir.'/'.$cache_tpl_md5;
if (!is_dir($cache_dir))
return false;
$dir_handle = opendir($cache_dir);
while ($curr_dir = readdir($dir_handle)) {
$cache_path_dir = $cache_dir.'/'.$curr_dir;
if ($curr_dir == '.' || $curr_dir == '..' ||
!is_dir($cache_path_dir))
continue;
$dir_handle2 = opendir($cache_path_dir);
while ($curr_file = readdir($dir_handle2)) {
if ($curr_file == '.' || $curr_file == '..')
continue;
$cache_file = $cache_path_dir.'/'.$curr_file;
if (is_file($cache_file))
unlink($cache_file);
}
closedir($dir_handle2);
@rmdir($cache_path_dir);
}
closedir($dir_handle);
@rmdir($cache_dir);
return true;
}
/*======================================================================*\
Function: quote_replace
Purpose: Quote subpattern references
\*======================================================================*/
function quote_replace($string)
{
return preg_replace('![\\$]\d!', '\\\\\\0', $string);
}
2000-08-08 17:05:38 +00:00
/*======================================================================*\
2001-02-01 21:39:29 +00:00
Function: _set_error_msg()
Purpose: set the error message
2000-08-08 17:05:38 +00:00
\*======================================================================*/
2001-02-01 21:39:29 +00:00
function _set_error_msg($error_msg)
{
$this->_error_msg="smarty error: $error_msg";
return true;
}
2000-08-08 17:05:38 +00:00
}
2001-02-01 21:39:29 +00:00
/* vim: set expandtab: */
2000-08-08 17:05:38 +00:00
?>