2008-02-27 03:31:51 +01:00
|
|
|
<?php
|
|
|
|
// +-----------------------------------------------------------------------+
|
2008-04-05 00:57:23 +02:00
|
|
|
// | Piwigo - a PHP based picture gallery |
|
|
|
|
// +-----------------------------------------------------------------------+
|
|
|
|
// | Copyright(C) 2008 Piwigo Team http://piwigo.org |
|
|
|
|
// | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net |
|
|
|
|
// | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick |
|
|
|
|
// +-----------------------------------------------------------------------+
|
|
|
|
// | 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 |
|
2008-02-27 03:31:51 +01:00
|
|
|
// | |
|
|
|
|
// | 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 GNU |
|
|
|
|
// | General Public License for more details. |
|
|
|
|
// | |
|
|
|
|
// | You should have received a copy of the GNU General Public License |
|
|
|
|
// | along with this program; if not, write to the Free Software |
|
|
|
|
// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
|
|
|
|
// | USA. |
|
|
|
|
// +-----------------------------------------------------------------------+
|
|
|
|
|
|
|
|
|
2008-03-23 01:04:46 +01:00
|
|
|
require_once 'smarty/libs/Smarty.class.php';
|
2008-02-27 03:31:51 +01:00
|
|
|
|
|
|
|
// migrate lang:XXX
|
|
|
|
// sed "s/{lang:\([^}]\+\)}/{\'\1\'|@translate}/g" my_template.tpl
|
|
|
|
// migrate change root level vars {XXX}
|
|
|
|
// sed "s/{pwg_root}/{ROOT_URL}/g" my_template.tpl
|
|
|
|
// migrate change root level vars {XXX}
|
|
|
|
// sed "s/{\([a-zA-Z_]\+\)}/{$\1}/g" my_template.tpl
|
|
|
|
// migrate all
|
|
|
|
// cat my_template.tpl | sed "s/{lang:\([^}]\+\)}/{\'\1\'|@translate}/g" | sed "s/{pwg_root}/{ROOT_URL}/g" | sed "s/{\([a-zA-Z_]\+\)}/{$\1}/g"
|
|
|
|
|
|
|
|
|
|
|
|
class Template {
|
|
|
|
|
|
|
|
var $smarty;
|
|
|
|
|
|
|
|
var $output = '';
|
|
|
|
|
|
|
|
// Hash of filenames for each template handle.
|
|
|
|
var $files = array();
|
|
|
|
|
2008-05-08 03:13:13 +02:00
|
|
|
// used by html_head smarty block to add content before </head>
|
|
|
|
var $html_head_elements = array();
|
|
|
|
|
2008-02-27 03:31:51 +01:00
|
|
|
function Template($root = ".", $theme= "")
|
|
|
|
{
|
|
|
|
global $conf;
|
|
|
|
|
|
|
|
$this->smarty = new Smarty;
|
|
|
|
$this->smarty->debugging = $conf['debug_template'];
|
|
|
|
|
|
|
|
if ( isset($conf['compiled_template_dir'] ) )
|
|
|
|
{
|
|
|
|
$compile_dir = $conf['compiled_template_dir'];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$compile_dir = $conf['local_data_dir'];
|
|
|
|
if ( !is_dir($compile_dir) )
|
|
|
|
{
|
|
|
|
mkdir( $compile_dir, 0777);
|
|
|
|
file_put_contents($compile_dir.'/index.htm', '');
|
|
|
|
}
|
|
|
|
$compile_dir .= '/templates_c';
|
|
|
|
}
|
|
|
|
if ( !is_dir($compile_dir) )
|
|
|
|
{
|
|
|
|
mkdir( $compile_dir, 0777 );
|
|
|
|
file_put_contents($compile_dir.'/index.htm', '');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->smarty->compile_dir = $compile_dir;
|
|
|
|
|
|
|
|
$this->smarty->assign_by_ref( 'pwg', new PwgTemplateAdapter() );
|
|
|
|
$this->smarty->register_modifier( 'translate', array('Template', 'mod_translate') );
|
2008-03-20 01:35:36 +01:00
|
|
|
$this->smarty->register_modifier( 'explode', array('Template', 'mod_explode') );
|
2008-05-08 03:13:13 +02:00
|
|
|
$this->smarty->register_block('html_head', array(&$this, 'block_html_head') );
|
2008-08-22 02:45:03 +02:00
|
|
|
$this->smarty->register_prefilter( array('Template', 'prefilter_white_space') );
|
2008-05-08 03:13:13 +02:00
|
|
|
if ( $conf['compiled_template_cache_language'] )
|
|
|
|
{
|
2008-08-22 02:45:03 +02:00
|
|
|
$this->smarty->register_prefilter( array('Template', 'prefilter_language') );
|
2008-05-08 03:13:13 +02:00
|
|
|
}
|
2008-02-27 03:31:51 +01:00
|
|
|
|
2008-03-06 01:25:47 +01:00
|
|
|
if ( !empty($theme) )
|
|
|
|
{
|
|
|
|
include($root.'/theme/'.$theme.'/themeconf.inc.php');
|
|
|
|
$this->smarty->assign('themeconf', $themeconf);
|
|
|
|
}
|
2008-02-27 03:31:51 +01:00
|
|
|
|
2008-03-13 02:43:45 +01:00
|
|
|
$this->set_template_dir($root);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the template root directory for this Template object.
|
|
|
|
*/
|
|
|
|
function set_template_dir($dir)
|
|
|
|
{
|
|
|
|
$this->smarty->template_dir = $dir;
|
|
|
|
|
|
|
|
$real_dir = realpath($dir);
|
|
|
|
$compile_id = crc32( $real_dir===false ? $dir : $real_dir);
|
2008-05-08 03:13:13 +02:00
|
|
|
$this->smarty->compile_id = base_convert($compile_id, 10, 36 );
|
2008-03-13 02:43:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the template root directory for this Template object.
|
|
|
|
*/
|
|
|
|
function get_template_dir()
|
|
|
|
{
|
|
|
|
return $this->smarty->template_dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes all compiled templates.
|
|
|
|
*/
|
|
|
|
function delete_compiled_templates()
|
|
|
|
{
|
|
|
|
$save_compile_id = $this->smarty->compile_id;
|
|
|
|
$this->smarty->compile_id = null;
|
|
|
|
$this->smarty->clear_compiled_tpl();
|
|
|
|
$this->smarty->compile_id = $save_compile_id;
|
|
|
|
file_put_contents($this->smarty->compile_dir.'/index.htm', '');
|
2008-02-27 03:31:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function get_themeconf($val)
|
|
|
|
{
|
|
|
|
$tc = $this->smarty->get_template_vars('themeconf');
|
|
|
|
return isset($tc[$val]) ? $tc[$val] : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the template filename for handle.
|
|
|
|
*/
|
|
|
|
function set_filename($handle, $filename)
|
|
|
|
{
|
|
|
|
return $this->set_filenames( array($handle=>$filename) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the template filenames for handles. $filename_array should be a
|
|
|
|
* hash of handle => filename pairs.
|
|
|
|
*/
|
|
|
|
function set_filenames($filename_array)
|
|
|
|
{
|
2008-08-16 03:32:37 +02:00
|
|
|
global $conf;
|
2008-02-27 03:31:51 +01:00
|
|
|
if (!is_array($filename_array))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
reset($filename_array);
|
2008-07-14 23:42:40 +02:00
|
|
|
$tpl_extension = isset($conf['extents_for_templates']) ?
|
|
|
|
unserialize($conf['extents_for_templates']) : array();
|
2008-02-27 03:31:51 +01:00
|
|
|
while(list($handle, $filename) = each($filename_array))
|
|
|
|
{
|
|
|
|
if (is_null($filename))
|
|
|
|
unset( $this->files[$handle] );
|
|
|
|
else
|
2008-07-14 23:42:40 +02:00
|
|
|
{
|
2008-08-16 03:32:37 +02:00
|
|
|
$this->files[$handle] = $filename;
|
2008-07-14 23:42:40 +02:00
|
|
|
foreach ($tpl_extension as $file => $conditions)
|
|
|
|
{
|
|
|
|
$localtpl = './template-extension/' . $file;
|
2008-08-16 03:32:37 +02:00
|
|
|
if ($handle == $conditions[0] and
|
|
|
|
(stripos(implode('/',array_flip($_GET)),$conditions[1])>0
|
2008-07-14 23:42:40 +02:00
|
|
|
or $conditions[1] == 'N/A')
|
|
|
|
and file_exists($localtpl))
|
|
|
|
{ /* examples: Are best_rated, created-monthly-calendar, list, ... set? */
|
2008-08-16 03:32:37 +02:00
|
|
|
$this->files[$handle] = '../.' . $localtpl;
|
2008-07-14 23:42:40 +02:00
|
|
|
/* assign their tpl-extension */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-02-27 03:31:51 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2008-08-16 03:32:37 +02:00
|
|
|
|
2008-03-20 01:35:36 +01:00
|
|
|
/** see smarty assign http://www.smarty.net/manual/en/api.assign.php */
|
|
|
|
function assign($tpl_var, $value = null)
|
|
|
|
{
|
|
|
|
$this->smarty->assign( $tpl_var, $value );
|
|
|
|
}
|
2008-03-23 01:04:46 +01:00
|
|
|
|
2008-03-20 01:35:36 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
* This is equivalent to assign($varname, $this->parse($handle, true))
|
|
|
|
*/
|
|
|
|
function assign_var_from_handle($varname, $handle)
|
|
|
|
{
|
|
|
|
$this->assign($varname, $this->parse($handle, true));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** see smarty append http://www.smarty.net/manual/en/api.append.php */
|
|
|
|
function append($tpl_var, $value=null, $merge=false)
|
|
|
|
{
|
|
|
|
$this->smarty->append( $tpl_var, $value, $merge );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Root-level variable concatenation. Appends a string to an existing
|
|
|
|
* variable assignment with the same name.
|
|
|
|
*/
|
|
|
|
function concat($tpl_var, $value)
|
|
|
|
{
|
|
|
|
$old_val = & $this->smarty->get_template_vars($tpl_var);
|
|
|
|
if ( isset($old_val) )
|
|
|
|
{
|
|
|
|
$old_val .= $value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->assign($tpl_var, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** see smarty append http://www.smarty.net/manual/en/api.clear_assign.php */
|
|
|
|
function clear_assign($tpl_var)
|
|
|
|
{
|
|
|
|
$this->smarty->clear_assign( $tpl_var );
|
|
|
|
}
|
|
|
|
|
|
|
|
/** see smarty get_template_vars http://www.smarty.net/manual/en/api.get_template_vars.php */
|
|
|
|
function &get_template_vars($name=null)
|
|
|
|
{
|
|
|
|
return $this->smarty->get_template_vars( $name );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-27 03:31:51 +01:00
|
|
|
/**
|
|
|
|
* Load the file for the handle, eventually compile the file and run the compiled
|
|
|
|
* code. This will add the output to the results or return the result if $return
|
|
|
|
* is true.
|
|
|
|
*/
|
|
|
|
function parse($handle, $return=false)
|
|
|
|
{
|
|
|
|
if ( !isset($this->files[$handle]) )
|
|
|
|
{
|
|
|
|
die("Template->parse(): Couldn't load template file for handle $handle");
|
|
|
|
}
|
|
|
|
|
2008-03-23 01:04:46 +01:00
|
|
|
$this->smarty->assign( 'ROOT_URL', get_root_url() );
|
|
|
|
$this->smarty->assign( 'TAG_INPUT_ENABLED',
|
|
|
|
((is_adviser()) ? 'disabled="disabled" onclick="return false;"' : ''));
|
2008-05-08 03:13:13 +02:00
|
|
|
|
|
|
|
global $conf, $lang_info;
|
|
|
|
if ( $conf['compiled_template_cache_language'] and isset($lang_info['code']) )
|
|
|
|
{
|
|
|
|
$save_compile_id = $this->smarty->compile_id;
|
|
|
|
$this->smarty->compile_id .= '.'.$lang_info['code'];
|
|
|
|
}
|
2008-08-16 03:32:37 +02:00
|
|
|
|
2008-03-23 01:04:46 +01:00
|
|
|
$v = $this->smarty->fetch($this->files[$handle], null, null, false);
|
2008-08-16 03:32:37 +02:00
|
|
|
|
2008-05-08 03:13:13 +02:00
|
|
|
if (isset ($save_compile_id) )
|
|
|
|
{
|
|
|
|
$this->smarty->compile_id = $save_compile_id;
|
|
|
|
}
|
2008-08-16 03:32:37 +02:00
|
|
|
|
2008-03-01 14:12:07 +01:00
|
|
|
if ($return)
|
2008-02-27 03:31:51 +01:00
|
|
|
{
|
2008-03-01 14:12:07 +01:00
|
|
|
return $v;
|
2008-02-27 03:31:51 +01:00
|
|
|
}
|
2008-03-01 14:12:07 +01:00
|
|
|
$this->output .= $v;
|
2008-02-27 03:31:51 +01:00
|
|
|
}
|
|
|
|
|
2008-03-01 14:12:07 +01:00
|
|
|
/**
|
|
|
|
* Load the file for the handle, eventually compile the file and run the compiled
|
|
|
|
* code. This will print out the results of executing the template.
|
|
|
|
*/
|
|
|
|
function pparse($handle)
|
|
|
|
{
|
|
|
|
$this->parse($handle, false);
|
2008-05-08 03:13:13 +02:00
|
|
|
$this->flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
function flush()
|
|
|
|
{
|
|
|
|
if ( count($this->html_head_elements) )
|
|
|
|
{
|
|
|
|
$search = "\n</head>";
|
|
|
|
$pos = strpos( $this->output, $search );
|
|
|
|
if ($pos !== false)
|
|
|
|
{
|
|
|
|
$this->output = substr_replace( $this->output, "\n".implode( "\n", $this->html_head_elements ), $pos, 0 );
|
|
|
|
} //else maybe error or warning ?
|
|
|
|
$this->html_head_elements = array();
|
|
|
|
}
|
2008-03-01 14:12:07 +01:00
|
|
|
echo $this->output;
|
|
|
|
$this->output='';
|
|
|
|
}
|
|
|
|
|
|
|
|
/** flushes the output */
|
2008-02-27 03:31:51 +01:00
|
|
|
function p()
|
|
|
|
{
|
2008-03-01 14:12:07 +01:00
|
|
|
$start = get_moment();
|
|
|
|
|
2008-05-08 03:13:13 +02:00
|
|
|
$this->flush();
|
2008-03-01 14:12:07 +01:00
|
|
|
|
|
|
|
if ($this->smarty->debugging)
|
|
|
|
{
|
|
|
|
global $t2;
|
|
|
|
$this->smarty->assign(
|
|
|
|
array(
|
|
|
|
'AAAA_DEBUG_OUTPUT_TIME__' => get_elapsed_time($start, get_moment()),
|
|
|
|
'AAAA_DEBUG_TOTAL_TIME__' => get_elapsed_time($t2, get_moment())
|
|
|
|
)
|
|
|
|
);
|
|
|
|
require_once(SMARTY_CORE_DIR . 'core.display_debug_console.php');
|
|
|
|
echo smarty_core_display_debug_console(null, $this->smarty);
|
|
|
|
}
|
2008-02-27 03:31:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-03-20 01:35:36 +01:00
|
|
|
* translate variable modifier - translates a text to the currently loaded
|
|
|
|
* language
|
2008-02-27 03:31:51 +01:00
|
|
|
*/
|
2008-03-20 01:35:36 +01:00
|
|
|
/*static*/ function mod_translate($text)
|
2008-02-27 03:31:51 +01:00
|
|
|
{
|
2008-03-20 01:35:36 +01:00
|
|
|
return l10n($text);
|
2008-02-27 03:31:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-03-20 01:35:36 +01:00
|
|
|
* explode variable modifier - similar to php explode
|
2008-03-23 01:04:46 +01:00
|
|
|
* 'Yes;No'|@explode:';' -> array('Yes', 'No')
|
2008-02-27 03:31:51 +01:00
|
|
|
*/
|
2008-03-20 01:35:36 +01:00
|
|
|
/*static*/ function mod_explode($text, $delimiter=',')
|
2008-02-27 03:31:51 +01:00
|
|
|
{
|
2008-03-20 01:35:36 +01:00
|
|
|
return explode($delimiter, $text);
|
2008-02-27 03:31:51 +01:00
|
|
|
}
|
2008-08-16 03:32:37 +02:00
|
|
|
|
2008-05-08 03:13:13 +02:00
|
|
|
/**
|
|
|
|
* This smarty "html_head" block allows to add content just before
|
|
|
|
* </head> element in the output after the head has been parsed. This is
|
|
|
|
* handy in order to respect strict standards when <style> and <link>
|
2008-08-16 03:32:37 +02:00
|
|
|
* html elements must appear in the <head> element
|
2008-05-08 03:13:13 +02:00
|
|
|
*/
|
|
|
|
function block_html_head($params, $content, &$smarty, &$repeat)
|
|
|
|
{
|
|
|
|
$content = trim($content);
|
|
|
|
if ( !empty($content) )
|
|
|
|
{ // second call
|
|
|
|
if ( empty($this->output) )
|
|
|
|
{//page header not parsed yet
|
|
|
|
$this->append('head_elements', $content);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->html_head_elements[] = $content;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-08-16 03:32:37 +02:00
|
|
|
|
2008-08-22 02:45:03 +02:00
|
|
|
/*static */ function prefilter_white_space($source, &$smarty)
|
|
|
|
{
|
|
|
|
$ld = $smarty->left_delimiter;
|
|
|
|
$rd = $smarty->right_delimiter;
|
|
|
|
$ldq = preg_quote($ld, '#');
|
|
|
|
$rdq = preg_quote($rd, '#');
|
|
|
|
|
|
|
|
$regex = array();
|
|
|
|
$tags = array('if', 'foreach', 'section');
|
|
|
|
foreach($tags as $tag)
|
|
|
|
{
|
|
|
|
array_push($regex, "#^\s+($ldq$tag"."[^$ld$rd]*$rdq)\s*$#m");
|
|
|
|
array_push($regex, "#^\s+($ldq/$tag$rdq)\s*$#m");
|
|
|
|
}
|
|
|
|
$tags = array('include', 'else', 'html_head');
|
|
|
|
foreach($tags as $tag)
|
|
|
|
{
|
|
|
|
array_push($regex, "#^\s+($ldq$tag"."[^$ld$rd]*$rdq)\s*$#m");
|
|
|
|
}
|
|
|
|
$source = preg_replace( $regex, "$1", $source);
|
|
|
|
return $source;
|
|
|
|
}
|
|
|
|
|
2008-05-08 03:13:13 +02:00
|
|
|
/**
|
2008-08-16 03:32:37 +02:00
|
|
|
* Smarty prefilter to allow caching (whenever possible) language strings
|
2008-05-08 03:13:13 +02:00
|
|
|
* from templates.
|
|
|
|
*/
|
2008-08-22 02:45:03 +02:00
|
|
|
/*static */ function prefilter_language($source, &$smarty)
|
2008-05-08 03:13:13 +02:00
|
|
|
{
|
|
|
|
global $lang;
|
2008-08-22 02:45:03 +02:00
|
|
|
$ldq = preg_quote($smarty->left_delimiter, '~');
|
|
|
|
$rdq = preg_quote($smarty->right_delimiter, '~');
|
2008-08-16 03:32:37 +02:00
|
|
|
|
2008-05-08 03:13:13 +02:00
|
|
|
$regex = "~$ldq *\'([^'$]+)\'\|@translate *$rdq~";
|
|
|
|
$source = preg_replace( $regex.'e', 'isset($lang[\'$1\']) ? $lang[\'$1\'] : \'$0\'', $source);
|
|
|
|
|
|
|
|
$regex = "~$ldq *\'([^'$]+)\'\|@translate\|~";
|
|
|
|
$source = preg_replace( $regex.'e', 'isset($lang[\'$1\']) ? \'{\'.var_export($lang[\'$1\'],true).\'|\' : \'$0\'', $source);
|
|
|
|
|
|
|
|
return $source;
|
|
|
|
}
|
2008-02-27 03:31:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class contains basic functions that can be called directly from the
|
|
|
|
* templates in the form $pwg->l10n('edit')
|
|
|
|
*/
|
|
|
|
class PwgTemplateAdapter
|
|
|
|
{
|
|
|
|
function l10n($text)
|
|
|
|
{
|
|
|
|
return l10n($text);
|
|
|
|
}
|
|
|
|
|
|
|
|
function l10n_dec($s, $p, $v)
|
|
|
|
{
|
|
|
|
return l10n_dec($s, $p, $v);
|
|
|
|
}
|
|
|
|
|
|
|
|
function sprintf()
|
|
|
|
{
|
|
|
|
$args = func_get_args();
|
|
|
|
return call_user_func_array('sprintf', $args );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|