2013-06-17 23:39:22 +02:00
|
|
|
<?php
|
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (C) 2007 Easter-eggs
|
|
|
|
* http://ldapsaisie.labs.libre-entreprise.org
|
|
|
|
*
|
|
|
|
* Author: See AUTHORS file in top-level directory.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
2018-10-01 12:32:05 +02:00
|
|
|
/**
|
2013-06-17 23:39:22 +02:00
|
|
|
* Manage template
|
|
|
|
*
|
|
|
|
* This class is use to manage template in LdapSaisie.
|
|
|
|
*
|
|
|
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
|
|
|
*/
|
|
|
|
class LStemplate {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* LStemplate configuration
|
|
|
|
*
|
|
|
|
* array(
|
|
|
|
* 'smarty_path' => '/path/to/Smarty.php',
|
|
|
|
* 'template_dir' => '/path/to/template/directory',
|
2013-06-19 00:53:26 +02:00
|
|
|
* 'image_dir' => '/path/to/image/directory',
|
2013-06-19 03:06:29 +02:00
|
|
|
* 'css_dir' => '/path/to/css/directory',
|
2013-06-17 23:39:22 +02:00
|
|
|
* 'compile_dir' => '/path/to/compile/directory',
|
|
|
|
* 'debug' => True,
|
|
|
|
* 'debug_smarty' => True
|
2018-10-01 12:32:05 +02:00
|
|
|
* )
|
2013-06-17 23:39:22 +02:00
|
|
|
*
|
|
|
|
**/
|
2013-06-19 00:53:26 +02:00
|
|
|
private static $config = array (
|
|
|
|
'smarty_path' => 'smarty/libs/Smarty.class.php',
|
|
|
|
'template_dir' => 'templates',
|
|
|
|
'image_dir' => 'images',
|
2013-06-19 03:06:29 +02:00
|
|
|
'css_dir' => 'css',
|
2013-06-19 00:53:26 +02:00
|
|
|
'compile_dir' => 'tmp',
|
|
|
|
'debug' => False,
|
|
|
|
'debug_smarty' => False
|
|
|
|
);
|
2013-06-17 23:39:22 +02:00
|
|
|
|
|
|
|
// Smarty object
|
|
|
|
public static $_smarty = NULL;
|
2018-10-01 12:32:05 +02:00
|
|
|
|
2013-06-17 23:39:22 +02:00
|
|
|
// Smarty version
|
|
|
|
private static $_smarty_version = NULL;
|
|
|
|
|
2013-06-18 19:04:47 +02:00
|
|
|
// Array of directories
|
|
|
|
private static $directories = array(
|
|
|
|
'local',
|
|
|
|
LS_THEME
|
|
|
|
);
|
|
|
|
|
2018-10-01 12:32:05 +02:00
|
|
|
// Registered events
|
|
|
|
private static $_events = array();
|
|
|
|
|
2013-06-17 23:39:22 +02:00
|
|
|
/**
|
|
|
|
* Start LStemplate
|
|
|
|
*
|
|
|
|
* Set configuration from parameter $config and initialize
|
|
|
|
* Smarty object.
|
|
|
|
*
|
|
|
|
* @param[in] $config array LStemplate configuration
|
|
|
|
*
|
|
|
|
* @retval boolean True on success, False instead
|
|
|
|
**/
|
|
|
|
public static function start($config) {
|
2018-10-01 12:32:05 +02:00
|
|
|
// Trigger starting event
|
|
|
|
self :: fireEvent('starting');
|
|
|
|
|
2013-06-19 00:53:26 +02:00
|
|
|
foreach ($config as $key => $value) {
|
|
|
|
self :: $config[$key] = $value;
|
|
|
|
}
|
2013-06-17 23:39:22 +02:00
|
|
|
|
2013-06-19 00:53:26 +02:00
|
|
|
if (LSsession :: includeFile(self :: $config['smarty_path'])) {
|
2013-06-17 23:39:22 +02:00
|
|
|
self :: $_smarty = new Smarty();
|
2013-06-19 00:53:26 +02:00
|
|
|
self :: $_smarty -> template_dir = self :: $config['template_dir'];
|
2013-06-17 23:39:22 +02:00
|
|
|
|
2013-06-19 00:53:26 +02:00
|
|
|
if ( ! is_writable(self :: $config['compile_dir']) ) {
|
|
|
|
die(_('LStemplate : compile directory is not writable (dir : '.self :: $config['compile_dir'].')'));
|
2013-06-17 23:39:22 +02:00
|
|
|
}
|
2013-06-19 00:53:26 +02:00
|
|
|
self :: $_smarty -> compile_dir = self :: $config['compile_dir'];
|
2013-06-17 23:39:22 +02:00
|
|
|
|
2013-06-19 00:53:26 +02:00
|
|
|
if (self :: $config['debug']) {
|
2013-06-17 23:39:22 +02:00
|
|
|
self :: $_smarty -> caching = 0;
|
|
|
|
// cache files are always regenerated
|
|
|
|
self :: $_smarty -> force_compile = TRUE;
|
|
|
|
// recompile template if it is changed
|
|
|
|
self :: $_smarty -> compile_check = TRUE;
|
2013-06-19 00:53:26 +02:00
|
|
|
if (self :: $config['debug_smarty']) {
|
2013-06-17 23:39:22 +02:00
|
|
|
// debug smarty
|
|
|
|
self :: $_smarty -> debugging = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (method_exists(self :: $_smarty,'register_function')) {
|
|
|
|
self :: $_smarty_version=2;
|
|
|
|
if (!LSsession :: loadLSclass('LStemplate_smarty2_support')) {
|
|
|
|
die(_("LStemplate : Can't load Smarty 2 support file"));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
elseif (method_exists(self :: $_smarty,'registerPlugin')) {
|
|
|
|
self :: $_smarty_version=3;
|
|
|
|
if (!LSsession :: loadLSclass('LStemplate_smarty3_support')) {
|
2013-06-18 00:26:34 +02:00
|
|
|
die(_("LStemplate : Can't load Smarty 3 support file"));
|
2013-06-17 23:39:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
die(_("LStemplate : Smarty version not recognized."));
|
|
|
|
}
|
|
|
|
|
2014-04-09 17:15:52 +02:00
|
|
|
self :: registerFunction("getFData", "LStemplate_smarty_getFData");
|
|
|
|
self :: registerFunction("tr", "LStemplate_smarty_tr");
|
|
|
|
self :: registerFunction("img", "LStemplate_smarty_img");
|
|
|
|
self :: registerFunction("css", "LStemplate_smarty_css");
|
2018-04-26 11:23:00 +02:00
|
|
|
self :: registerFunction("uniqid", "LStemplate_smarty_uniqid");
|
2014-04-09 17:15:52 +02:00
|
|
|
|
2018-10-01 12:32:05 +02:00
|
|
|
// Trigger started event
|
|
|
|
self :: fireEvent('started');
|
|
|
|
|
2013-06-17 23:39:22 +02:00
|
|
|
return True;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
die(_("LStemplate : Can't load Smarty."));
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-24 12:19:39 +02:00
|
|
|
/**
|
|
|
|
* Return the default directory path of files
|
|
|
|
*
|
|
|
|
* Return LS_THEME contanst value or 'default' if not defined
|
|
|
|
*
|
|
|
|
* @retval string The default directory path of files
|
|
|
|
**/
|
|
|
|
public static function getDefaultDir() {
|
|
|
|
if (defined('LS_THEME'))
|
|
|
|
return LS_THEME;
|
|
|
|
else
|
|
|
|
return 'default';
|
|
|
|
}
|
|
|
|
|
2013-06-18 19:04:47 +02:00
|
|
|
/**
|
|
|
|
* Return the path of the file to use
|
|
|
|
*
|
|
|
|
* @param[in] string $name The file name (eg: mail.png)
|
|
|
|
* @param[in] string $root_dir The root directory (eg: images)
|
|
|
|
* @param[in] string $default_dir The default directory (eg: default)
|
2018-05-22 18:47:08 +02:00
|
|
|
* @param[in] bool $with_nocache If true, include nocache URL param (default: false)
|
2013-06-18 19:04:47 +02:00
|
|
|
*
|
|
|
|
* @retval string The path of the file
|
|
|
|
**/
|
2018-05-22 18:47:08 +02:00
|
|
|
public static function getFilePath($file, $root_dir, $default_dir=null, $with_nocache=false) {
|
|
|
|
if ($default_dir === null)
|
2018-05-24 12:19:39 +02:00
|
|
|
$default_dir = self :: getDefaultDir();
|
2018-06-01 13:59:01 +02:00
|
|
|
$path = false;
|
2013-06-18 19:04:47 +02:00
|
|
|
foreach(self :: $directories as $dir) {
|
2018-06-01 13:59:01 +02:00
|
|
|
$path = $root_dir.'/'.$dir.'/'.$file;
|
|
|
|
if (file_exists($path)) {
|
|
|
|
break;
|
2013-06-18 19:04:47 +02:00
|
|
|
}
|
2018-06-01 13:59:01 +02:00
|
|
|
$path = false;
|
2013-06-18 19:04:47 +02:00
|
|
|
}
|
2018-06-01 13:59:01 +02:00
|
|
|
if (!$path) {
|
|
|
|
if (!$default_dir)
|
|
|
|
return;
|
|
|
|
$path = $root_dir.'/'.$default_dir.'/'.$file;
|
2013-06-18 19:04:47 +02:00
|
|
|
}
|
2018-05-22 18:47:08 +02:00
|
|
|
if ($with_nocache)
|
|
|
|
$path .= "?nocache=".self::getNoCacheFileValue($path);
|
|
|
|
return $path;
|
2013-06-18 19:04:47 +02:00
|
|
|
}
|
|
|
|
|
2013-06-19 00:53:26 +02:00
|
|
|
/**
|
|
|
|
* Return the path of the image file to use
|
|
|
|
*
|
|
|
|
* @param[in] string $image The image name (eg: mail)
|
2018-05-22 18:47:08 +02:00
|
|
|
* @param[in] bool $with_nocache If true, include nocache URL param (default: false)
|
2013-06-19 00:53:26 +02:00
|
|
|
*
|
|
|
|
* @retval string The path of the image file
|
|
|
|
**/
|
2018-05-22 18:47:08 +02:00
|
|
|
public static function getImagePath($image, $with_nocache=false) {
|
2013-06-19 00:53:26 +02:00
|
|
|
$exts=array('png','gif','jpg');
|
|
|
|
foreach($exts as $ext) {
|
2018-05-22 18:47:08 +02:00
|
|
|
$path = self :: getFilePath("$image.$ext", self :: $config['image_dir'], False, $with_nocache);
|
2013-06-19 00:53:26 +02:00
|
|
|
if ($path) return $path;
|
|
|
|
}
|
2018-05-24 12:19:39 +02:00
|
|
|
return self :: $config['image_dir']."/".self :: getDefaultDir()."/$image.png";
|
2013-06-19 00:53:26 +02:00
|
|
|
}
|
|
|
|
|
2013-06-19 03:06:29 +02:00
|
|
|
/**
|
|
|
|
* Return the path of the CSS file to use
|
|
|
|
*
|
|
|
|
* @param[in] string $css The CSS name (eg: main.css)
|
2018-05-22 18:47:08 +02:00
|
|
|
* @param[in] bool $with_nocache If true, include nocache URL param (default: false)
|
2013-06-19 03:06:29 +02:00
|
|
|
*
|
|
|
|
* @retval string The path of the CSS file
|
|
|
|
**/
|
2018-05-22 18:47:08 +02:00
|
|
|
public static function getCSSPath($css, $with_nocache=false) {
|
|
|
|
return self :: getFilePath($css, self :: $config['css_dir'], Null, $with_nocache);
|
2013-06-19 03:06:29 +02:00
|
|
|
}
|
2013-06-19 00:53:26 +02:00
|
|
|
|
2013-06-17 23:39:22 +02:00
|
|
|
/**
|
|
|
|
* Return the path of the Smarty template file to use
|
|
|
|
*
|
|
|
|
* @param[in] string $template The template name (eg: top.tpl)
|
2018-05-22 18:47:08 +02:00
|
|
|
* @param[in] bool $with_nocache If true, include nocache URL param (default: false)
|
2013-06-18 19:04:47 +02:00
|
|
|
*
|
2013-06-17 23:39:22 +02:00
|
|
|
* @retval string The path of the Smarty template file
|
|
|
|
**/
|
2018-05-22 18:47:08 +02:00
|
|
|
public static function getTemplatePath($template, $with_nocache=false) {
|
|
|
|
return self :: getFilePath($template, self :: $config['template_dir'], null, $with_nocache);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the nocache value of the specify file
|
|
|
|
*
|
|
|
|
* @param[in] string $file The file path
|
|
|
|
*
|
|
|
|
* @retval string The specified file's nocache value
|
|
|
|
**/
|
|
|
|
public static function getNoCacheFileValue($file) {
|
|
|
|
$stat = @stat($file);
|
|
|
|
if (is_array($stat) && isset($stat['mtime']))
|
|
|
|
return md5($stat['mtime']);
|
|
|
|
return md5(time());
|
2013-06-17 23:39:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the content of a Smarty template file.
|
|
|
|
*
|
|
|
|
* @param[in] string $template The template name (eg: top.tpl)
|
2013-06-18 19:04:47 +02:00
|
|
|
*
|
2013-06-17 23:39:22 +02:00
|
|
|
* @retval string The content of the Smarty template file
|
|
|
|
**/
|
|
|
|
public static function getTemplateSource($template) {
|
|
|
|
$tpl_path=self :: getTemplatePath($template);
|
|
|
|
if (!is_readable($tpl_path)) {
|
2013-06-25 11:34:42 +02:00
|
|
|
if (self :: $_smarty_version > 2) {
|
|
|
|
// No error return with Smarty3 and highter because it's call
|
|
|
|
// template name in lower first systematically
|
|
|
|
return '';
|
|
|
|
}
|
2013-06-17 23:39:22 +02:00
|
|
|
$tpl_path=self :: getTemplatePath('empty.tpl');
|
|
|
|
LSerror::addErrorCode('LStemplate_01',$template);
|
|
|
|
}
|
|
|
|
return implode('',file($tpl_path));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the timestamp of the last change of a Smarty
|
|
|
|
* template file.
|
|
|
|
*
|
|
|
|
* @param[in] string $template The template name (eg: top.tpl)
|
2013-06-18 19:04:47 +02:00
|
|
|
*
|
2013-06-17 23:39:22 +02:00
|
|
|
* @retval string The timestamp of the last change of the Smarty template file
|
|
|
|
**/
|
|
|
|
public static function getTemplateTimestamp($template) {
|
|
|
|
$tpl_path=self :: getTemplatePath($template);
|
|
|
|
if (is_file($tpl_path)) {
|
|
|
|
$time=filemtime($tpl_path);
|
|
|
|
if ($time)
|
|
|
|
return $time;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assign template variable
|
|
|
|
*
|
|
|
|
* @param[in] string $name The variable name
|
|
|
|
* @param[in] mixed $value The variable value
|
2013-06-18 19:04:47 +02:00
|
|
|
*
|
2013-06-17 23:39:22 +02:00
|
|
|
* @retval void
|
|
|
|
**/
|
|
|
|
public static function assign($name,$value) {
|
|
|
|
return self :: $_smarty -> assign($name,$value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a template
|
|
|
|
*
|
|
|
|
* @param[in] string $template The template name (eg: empty.tpl)
|
2013-06-18 19:04:47 +02:00
|
|
|
*
|
2013-06-17 23:39:22 +02:00
|
|
|
* @retval void
|
|
|
|
**/
|
|
|
|
public static function display($template) {
|
2018-10-01 12:32:05 +02:00
|
|
|
// Trigger displaying event
|
|
|
|
self :: fireEvent('displaying');
|
|
|
|
|
|
|
|
self :: $_smarty -> display("ls:$template");
|
|
|
|
|
|
|
|
// Trigger displayed event
|
|
|
|
self :: fireEvent('displayed');
|
2013-06-17 23:39:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch a template
|
|
|
|
*
|
|
|
|
* @param[in] string $template The template name (eg: empty.tpl)
|
2013-06-18 19:04:47 +02:00
|
|
|
*
|
2013-06-17 23:39:22 +02:00
|
|
|
* @retval string The template compiled
|
|
|
|
**/
|
|
|
|
public static function fetch($template) {
|
|
|
|
return self :: $_smarty -> fetch("ls:$template");
|
|
|
|
}
|
|
|
|
|
2014-04-09 17:15:52 +02:00
|
|
|
/**
|
|
|
|
* Register a template function
|
|
|
|
*
|
|
|
|
* @param[in] string $name The function name in template
|
|
|
|
* @param[in] string $function_name The function name in PHP
|
|
|
|
*
|
|
|
|
* @retval void
|
|
|
|
*/
|
|
|
|
public static function registerFunction($name,$function_name) {
|
|
|
|
LStemplate_register_function($name,$function_name);
|
|
|
|
}
|
|
|
|
|
2018-10-01 12:32:05 +02:00
|
|
|
/**
|
|
|
|
* Registered an action on a specific event
|
|
|
|
*
|
|
|
|
* @param[in] $event string The event name
|
|
|
|
* @param[in] $callable callable The callable to run on event
|
|
|
|
* @param[in] $param mixed Paremeters that will be pass to the callable
|
|
|
|
*
|
|
|
|
* @retval void
|
|
|
|
*/
|
|
|
|
function addEvent($event,$callable,$param=NULL) {
|
|
|
|
self :: $_events[$event][] = array(
|
|
|
|
'callable' => $callable,
|
|
|
|
'param' => $param,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run triggered actions on specific event
|
|
|
|
*
|
|
|
|
* @param[in] $event string Event name
|
|
|
|
*
|
|
|
|
* @retval boolean True if all triggered actions succefully runned, false otherwise
|
|
|
|
*/
|
|
|
|
function fireEvent($event) {
|
|
|
|
$return = true;
|
|
|
|
|
|
|
|
// Binding via addEvent
|
|
|
|
if (isset(self :: $_events[$event]) && is_array(self :: $_events[$event])) {
|
|
|
|
foreach (self :: $_events[$event] as $e) {
|
|
|
|
if (is_callable($e['callable'])) {
|
|
|
|
try {
|
|
|
|
call_user_func_array($e['callable'],array(&$e['param']));
|
|
|
|
}
|
|
|
|
catch(Exception $er) {
|
|
|
|
LSerror :: addErrorCode('LStemplate_03',array('callable' => getCallableName($e['callable']),'event' => $event));
|
|
|
|
$return = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LSerror :: addErrorCode('LStemplate_02',array('callable' => getCallableName($e['callable']),'event' => $event));
|
|
|
|
$return = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2013-06-17 23:39:22 +02:00
|
|
|
}
|
|
|
|
|
2013-06-18 19:04:47 +02:00
|
|
|
function LStemplate_smarty_getFData($params) {
|
|
|
|
extract($params);
|
|
|
|
echo getFData($format,$data,$meth=NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
function LStemplate_smarty_tr($params) {
|
|
|
|
extract($params);
|
|
|
|
echo __($msg);
|
|
|
|
}
|
|
|
|
|
2013-06-19 00:53:26 +02:00
|
|
|
function LStemplate_smarty_img($params) {
|
|
|
|
extract($params);
|
2013-06-19 02:17:39 +02:00
|
|
|
echo "image.php?i=$name";
|
2013-06-19 00:53:26 +02:00
|
|
|
}
|
|
|
|
|
2013-06-19 03:06:29 +02:00
|
|
|
function LStemplate_smarty_css($params) {
|
|
|
|
extract($params);
|
2018-05-22 18:47:08 +02:00
|
|
|
echo LStemplate :: getCSSPath($name, true);
|
2013-06-19 03:06:29 +02:00
|
|
|
}
|
|
|
|
|
2018-04-26 11:23:00 +02:00
|
|
|
function LStemplate_smarty_uniqid($params, &$smarty) {
|
|
|
|
if (!isset($params['var']))
|
|
|
|
$params['var'] = 'uniqid';
|
|
|
|
$smarty -> assign($params['var'], uniqid());
|
|
|
|
}
|
|
|
|
|
2013-06-17 23:39:22 +02:00
|
|
|
// Errors
|
|
|
|
LSerror :: defineError('LStemplate_01',
|
|
|
|
_("LStemplate : Template %{file} not found.")
|
|
|
|
);
|
2018-10-01 12:32:05 +02:00
|
|
|
LSerror :: defineError('LStemplate_02',
|
|
|
|
_("LStemplate : Fail to execute trigger %{callable} on event %{event} : is not callable.")
|
|
|
|
);
|
|
|
|
LSerror :: defineError('LStemplate_03',
|
|
|
|
_("LStemplate : Error during the execution of the trigger %{callable} on event %{event}.")
|
|
|
|
);
|