|
|
|
|
File: [Development] / advokit-installer / TemplateEngine.class.php
(download)
/
(as text)
Revision: 1.5, Tue Oct 5 03:49:55 2004 UTC (5 years, 11 months ago) by chrishiller Branch: MAIN CVS Tags: r1-x-dev, r0-9-9, HEAD Changes since 1.4: +4 -1 lines applying c scott ananian's patches |
<?
# ======================================================================
# AdvoKit -- a campaign managment tool
# Copyright (C) 2004 OrchidSuites, Inc. (info@orchidsuites.net)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the AFFERO GENERAL PUBLIC LICENSE
# as published by Affero, Inc.; either version 1
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# AFFERO GENERAL PUBLIC LICENSE for more details.
#
# You should have received a copy of the AFFERO GENERAL PUBLIC LICENSE
# along with this program; if not, write to Affero, Inc. at
# 510 Third Street - Suite 225, San Francisco, CA 94107, USA
# or visit <http://www.affero.org>.
# ======================================================================
# ======================================================================
# Wrapper for template engine. Current only supports Smarty, but
# you can change that.
# ======================================================================
# This is included because the functions within must be
# globally accessible.
require "$t_root".DIRSEP."tabindex.inc.php";
require "$t_root".DIRSEP."stripspaces.inc.php";
class TemplateEngine
{
var $name;
var $logger;
var $te;
var $te_dir;
var $login_no_cookies;
var $login_cookie_name;
var $auto_html_escape;
var $last_auto_html_escape;
var $tplvars;
#.................................................. TemplateEngine
#
# Create an instance of the template engine.
#
function TemplateEngine( &$config, &$logger )
{
$this->logger =& $logger;
$this->name = $config->get( "te_name" );
$this->auto_html_escape = $config->get( "te_auto_html_escape" );
$this->login_no_cookies = $config->get( "login_no_cookies" );
$this->login_cookie_name = $config->get( "login_cookie_name" );
$this->te_dir = $config->get( 'te_dir' );
if( $this->te_dir )
{
$this->te_dir .= DIRSEP;
}
$this->tplvars = array();
if( "smarty" == $this->name )
{
if( $this->te_dir )
{
define( 'SMARTY_DIR', $this->te_dir );
}
require $this->te_dir.'Smarty.class.php';
$this->te = new Smarty();
$this->te->use_sub_dirs = FALSE;
$te_root = $config->get( "te_root" );
$this->te->template_dir = $te_root.DIRSEP.$config->get( "te_tpls_raw" );
$this->te->compile_dir = $te_root.DIRSEP.$config->get( "te_tpls_compiled" );
$this->te->config_dir = $te_root.DIRSEP.$config->get( "te_tpl_configs" );
$this->te->debugging = $config->get( "te_debugging_on" );
$this->te->debug_tpl = $config->get( "te_debugging_tpl" );
$this->te->compile_check = $config->get( "te_compile_if_new" );
$this->te->force_compile = $config->get( "te_compile_always" );
$this->te->caching = $config->get( "te_caching_on" );
$this->te->cache_dir = $config->get( "te_tpls_cached" );
$this->te->use_sub_dirs = $config->get( "te_use_sub_dirs" );
$this->te->cache_lifetime = $config->get( "te_cache_timeout" );
$this->te->left_delimiter = $config->get( "te_left_delimiter" );
$this->te->right_delimiter = $config->get( "te_right_delimiter" );
if( $config->get( "te_security_level" ) == "high" )
{
$this->te->security = true;
$this->te->secure_dir =
array( $te_root.DIRSEP.$config->get( "te_tpls_raw" ),
$te_root.DIRSEP.$config->get( "te_tpls_compiled" ),
$te_root.DIRSEP.$config->get( "te_tpl_configs" ),
$te_root.DIRSEP.$config->get( "te_tpls_cached" ) );
}
# The following statements may use the files
# included (required) at the top of this file.
if( $config->get( "strip_leading_spaces" ) )
{
$this->registerPrefilter( "stripLeadingSpaces" );
}
if( $config->get( "strip_tab_indexes" )
|| $config->get( "add_tab_indexes" )
)
{
# You have to register them in this order, since
# you want to strip before adding.
if( $config->get( "strip_tab_indexes" ) )
{
$this->registerPostfilter( "stripTabIndexes" );
}
if( $config->get( "add_tab_indexes" ) )
{
$this->registerPostfilter( "addTabIndexes" );
}
}
$this->logger->debug( __FILE__, "Created instance of '".$this->name."' template engine." );
}
else
{
$this->logger->error( __FILE__, "Specified template engine '$this->name' not found. Can't create instance!" );
}
}
#.................................................. render
#
# Render the specified template. The $info parameter
# is only used if the template engine needs additional
# information in order to render the specified template.
#
function render( $template, $info=NULL )
{
if( "smarty" == $this->name )
{
$this->te->display( "file:$template" );
}
else
{
$this->logger->error( __FILE__, "Error trying to render '$template'." );
}
}
#.................................................. getstring
#
# Render the specified template as a string.
#
function getstring( $template, $info=NULL )
{
if( "smarty" == $this->name )
{
return $this->te->fetch( "file:$template" );
}
else
{
$this->logger->error( __FILE__, "Error trying to render '$template' as a string." );
}
return NULL;
}
#.................................................. registerPostfilter
#
# Register a function to filter templates after they
# have been processed.
#
function registerPostfilter( $filtername )
{
if( "smarty" == $this->name )
{
$this->te->register_postfilter( $filtername );
}
else
{
$this->logger->error( __FILE__, "Error trying to register postfilter '$filtername'. No template engine named '".$this->name."'" );
}
}
#.................................................. registerPrefilter
#
# Register a function to filter templates before they
# have been processed.
#
function registerPrefilter( $filtername )
{
if( "smarty" == $this->name )
{
$this->te->register_prefilter( $filtername );
}
else
{
$this->logger->error( __FILE__, "Error trying to register prefilter '$filtername'. No template engine named '".$this->name."'" );
}
}
#.................................................. set
#
# Set the value of the specified variable
#
function set( $key, $value )
{
if( "smarty" == $this->name )
{
$this->tplvars[$key] = $value;
if( $this->auto_html_escape )
{
$this->te->assign( $key, $this->htmlEscape( $value ) );
}
else
{
$this->te->assign( $key, $value );
}
}
else
{
$this->logger->error( __FILE__, "Error trying to set value in template." );
}
}
#.................................................. hasValue
#
# Return TRUE if the variable was set in the template.
#
function hasValue( $key )
{
return isset( $this->tplvars[$key] );
}
#.................................................. setWithDefault
#
# Set the value of the specified variable. If $value
# is empty, use $default instead.
#
function setWithDefault( $key, $value, $default )
{
if( "smarty" == $this->name )
{
if( isset( $value ) && $value != "" )
{
if( $this->auto_html_escape )
{
$this->te->assign( $key, $this->htmlEscape( $value ) );
}
else
{
$this->te->assign( $key, $value );
}
}
else
{
if( $this->auto_html_escape )
{
$this->te->assign( $key, $this->htmlEscape( $default ) );
}
else
{
$this->te->assign( $key, $default );
}
}
}
else
{
$this->logger->error( __FILE__, "Error trying to render '$template' as a string." );
}
}
#.................................................. getAutoHtmlEscape
#
# Return the value of $this->auto_html_escape
#
function getAutoHtmlEscape()
{
return $this->auto_html_escape;
}
#.................................................. setAutoHtmlEscape
#
# Set the value of $this->auto_html_escape
#
function setAutoHtmlEscape( $value )
{
$this->auto_html_escape = $value;
}
#.................................................. restoreAutoHtmlEscape
#
# Restore value from $this->last_auto_html_escape
#
function restoreAutoHtmlEscape()
{
$this->auto_html_escape = $this->last_auto_html_escape;
}
#.................................................. setAndSaveAutoHtmlEscape
#
# Set the value of $this->auto_html_escape
# TRUE escapes, FALSE is passthru
#
function setAndSaveAutoHtmlEscape( $value )
{
$this->last_auto_html_escape = $this->auto_html_escape;
$this->auto_html_escape = $value;
}
#.................................................. htmlEscape
#
# Convert characters such as "<" to their corresponding
# HTML escape sequences.
#
function htmlEscape( &$item )
{
if( is_array( $item ) )
{
foreach( $item as $key => $value )
{
$item[$key] = $this->htmlEscape( $value );
}
return $item;
}
else
{
// escape newline characters, too, in case we put a value from
// a TEXTAREA into an <input type="hidden"> value attribute.
return str_replace(array("\x0A","\x0D"),array(" "," "),
htmlspecialchars( $item, ENT_QUOTES ));
}
}
}
?>
| cvsadmin@voter2voter.org | CVS Snapshots (updated daily) |