2012-07-04 07:23:08 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Smarty Internal Plugin Config
|
|
|
|
*
|
2014-09-07 13:38:28 +02:00
|
|
|
* @package Smarty
|
2012-07-04 07:23:08 +02:00
|
|
|
* @subpackage Config
|
2014-09-07 13:38:28 +02:00
|
|
|
* @author Uwe Tews
|
2012-07-04 07:23:08 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Smarty Internal Plugin Config
|
|
|
|
* Main class for config variables
|
|
|
|
*
|
2014-09-07 13:38:28 +02:00
|
|
|
* @package Smarty
|
2012-07-04 07:23:08 +02:00
|
|
|
* @subpackage Config
|
|
|
|
* @ignore
|
|
|
|
*/
|
2014-09-07 13:38:28 +02:00
|
|
|
class Smarty_Internal_Config
|
|
|
|
{
|
2012-07-04 07:23:08 +02:00
|
|
|
/**
|
2014-09-07 13:38:28 +02:00
|
|
|
* Smarty instance
|
2012-07-04 07:23:08 +02:00
|
|
|
*
|
|
|
|
* @var Smarty object
|
|
|
|
*/
|
|
|
|
public $smarty = null;
|
|
|
|
/**
|
|
|
|
* Object of config var storage
|
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
public $data = null;
|
|
|
|
/**
|
|
|
|
* Config resource
|
2014-09-07 13:38:28 +02:00
|
|
|
*
|
2012-07-04 07:23:08 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $config_resource = null;
|
|
|
|
/**
|
|
|
|
* Compiled config file
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $compiled_config = null;
|
|
|
|
/**
|
|
|
|
* filepath of compiled config file
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $compiled_filepath = null;
|
|
|
|
/**
|
|
|
|
* Filemtime of compiled config Filemtime
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public $compiled_timestamp = null;
|
|
|
|
/**
|
|
|
|
* flag if compiled config file is invalid and must be (re)compiled
|
2014-09-07 13:38:28 +02:00
|
|
|
*
|
2012-07-04 07:23:08 +02:00
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $mustCompile = null;
|
|
|
|
/**
|
|
|
|
* Config file compiler object
|
|
|
|
*
|
|
|
|
* @var Smarty_Internal_Config_File_Compiler object
|
|
|
|
*/
|
|
|
|
public $compiler_object = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor of config file object
|
|
|
|
*
|
|
|
|
* @param string $config_resource config file resource name
|
2014-09-07 13:38:28 +02:00
|
|
|
* @param Smarty $smarty Smarty instance
|
|
|
|
* @param object $data object for config vars storage
|
2012-07-04 07:23:08 +02:00
|
|
|
*/
|
|
|
|
public function __construct($config_resource, $smarty, $data = null)
|
|
|
|
{
|
|
|
|
$this->data = $data;
|
|
|
|
$this->smarty = $smarty;
|
|
|
|
$this->config_resource = $config_resource;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the compiled filepath
|
|
|
|
*
|
|
|
|
* @return string the compiled filepath
|
|
|
|
*/
|
|
|
|
public function getCompiledFilepath()
|
|
|
|
{
|
|
|
|
return $this->compiled_filepath === null ?
|
2014-09-07 13:38:28 +02:00
|
|
|
($this->compiled_filepath = $this->buildCompiledFilepath()) :
|
|
|
|
$this->compiled_filepath;
|
2012-07-04 07:23:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get file path.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function buildCompiledFilepath()
|
|
|
|
{
|
|
|
|
$_compile_id = isset($this->smarty->compile_id) ? preg_replace('![^\w\|]+!', '_', $this->smarty->compile_id) : null;
|
|
|
|
$_flag = (int) $this->smarty->config_read_hidden + (int) $this->smarty->config_booleanize * 2
|
2014-09-07 13:38:28 +02:00
|
|
|
+ (int) $this->smarty->config_overwrite * 4;
|
|
|
|
$_filepath = sha1(realpath($this->source->filepath) . $_flag);
|
2012-07-04 07:23:08 +02:00
|
|
|
// if use_sub_dirs, break file into directories
|
|
|
|
if ($this->smarty->use_sub_dirs) {
|
|
|
|
$_filepath = substr($_filepath, 0, 2) . DS
|
2014-09-07 13:38:28 +02:00
|
|
|
. substr($_filepath, 2, 2) . DS
|
|
|
|
. substr($_filepath, 4, 2) . DS
|
|
|
|
. $_filepath;
|
2012-07-04 07:23:08 +02:00
|
|
|
}
|
|
|
|
$_compile_dir_sep = $this->smarty->use_sub_dirs ? DS : '^';
|
|
|
|
if (isset($_compile_id)) {
|
|
|
|
$_filepath = $_compile_id . $_compile_dir_sep . $_filepath;
|
|
|
|
}
|
|
|
|
$_compile_dir = $this->smarty->getCompileDir();
|
2014-09-07 13:38:28 +02:00
|
|
|
|
2012-07-04 07:23:08 +02:00
|
|
|
return $_compile_dir . $_filepath . '.' . basename($this->source->name) . '.config' . '.php';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-09-07 13:38:28 +02:00
|
|
|
* Returns the timestamp of the compiled file
|
2012-07-04 07:23:08 +02:00
|
|
|
*
|
|
|
|
* @return integer the file timestamp
|
|
|
|
*/
|
|
|
|
public function getCompiledTimestamp()
|
|
|
|
{
|
|
|
|
return $this->compiled_timestamp === null
|
|
|
|
? ($this->compiled_timestamp = (file_exists($this->getCompiledFilepath())) ? filemtime($this->getCompiledFilepath()) : false)
|
|
|
|
: $this->compiled_timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns if the current config file must be compiled
|
|
|
|
* It does compare the timestamps of config source and the compiled config and checks the force compile configuration
|
|
|
|
*
|
|
|
|
* @return boolean true if the file must be compiled
|
|
|
|
*/
|
|
|
|
public function mustCompile()
|
|
|
|
{
|
|
|
|
return $this->mustCompile === null ?
|
2014-09-07 13:38:28 +02:00
|
|
|
$this->mustCompile = ($this->smarty->force_compile || $this->getCompiledTimestamp() === false || $this->smarty->compile_check && $this->getCompiledTimestamp() < $this->source->timestamp) :
|
2012-07-04 07:23:08 +02:00
|
|
|
$this->mustCompile;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the compiled config file
|
|
|
|
* It checks if the config file must be compiled or just read the compiled version
|
|
|
|
*
|
|
|
|
* @return string the compiled config file
|
|
|
|
*/
|
|
|
|
public function getCompiledConfig()
|
|
|
|
{
|
|
|
|
if ($this->compiled_config === null) {
|
|
|
|
// see if template needs compiling.
|
|
|
|
if ($this->mustCompile()) {
|
|
|
|
$this->compileConfigSource();
|
|
|
|
} else {
|
|
|
|
$this->compiled_config = file_get_contents($this->getCompiledFilepath());
|
|
|
|
}
|
|
|
|
}
|
2014-09-07 13:38:28 +02:00
|
|
|
|
2012-07-04 07:23:08 +02:00
|
|
|
return $this->compiled_config;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compiles the config files
|
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function compileConfigSource()
|
|
|
|
{
|
|
|
|
// compile template
|
|
|
|
if (!is_object($this->compiler_object)) {
|
|
|
|
// load compiler
|
|
|
|
$this->compiler_object = new Smarty_Internal_Config_File_Compiler($this->smarty);
|
|
|
|
}
|
|
|
|
// compile locking
|
|
|
|
if ($this->smarty->compile_locking) {
|
|
|
|
if ($saved_timestamp = $this->getCompiledTimestamp()) {
|
|
|
|
touch($this->getCompiledFilepath());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// call compiler
|
|
|
|
try {
|
|
|
|
$this->compiler_object->compileSource($this);
|
2014-09-07 13:38:28 +02:00
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
2012-07-04 07:23:08 +02:00
|
|
|
// restore old timestamp in case of error
|
|
|
|
if ($this->smarty->compile_locking && $saved_timestamp) {
|
|
|
|
touch($this->getCompiledFilepath(), $saved_timestamp);
|
|
|
|
}
|
|
|
|
throw $e;
|
|
|
|
}
|
2014-09-07 13:38:28 +02:00
|
|
|
// compiling succeeded
|
2012-07-04 07:23:08 +02:00
|
|
|
// write compiled template
|
|
|
|
Smarty_Internal_Write_File::writeFile($this->getCompiledFilepath(), $this->getCompiledConfig(), $this->smarty);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* load config variables
|
|
|
|
*
|
2014-09-07 13:38:28 +02:00
|
|
|
* @param mixed $sections array of section names, single section or null
|
|
|
|
* @param string $scope global,parent or local
|
|
|
|
*
|
|
|
|
* @throws Exception
|
2012-07-04 07:23:08 +02:00
|
|
|
*/
|
|
|
|
public function loadConfigVars($sections = null, $scope = 'local')
|
|
|
|
{
|
|
|
|
if ($this->data instanceof Smarty_Internal_Template) {
|
|
|
|
$this->data->properties['file_dependency'][sha1($this->source->filepath)] = array($this->source->filepath, $this->source->timestamp, 'file');
|
|
|
|
}
|
|
|
|
if ($this->mustCompile()) {
|
|
|
|
$this->compileConfigSource();
|
|
|
|
}
|
|
|
|
// pointer to scope
|
|
|
|
if ($scope == 'local') {
|
|
|
|
$scope_ptr = $this->data;
|
|
|
|
} elseif ($scope == 'parent') {
|
|
|
|
if (isset($this->data->parent)) {
|
|
|
|
$scope_ptr = $this->data->parent;
|
|
|
|
} else {
|
|
|
|
$scope_ptr = $this->data;
|
|
|
|
}
|
|
|
|
} elseif ($scope == 'root' || $scope == 'global') {
|
|
|
|
$scope_ptr = $this->data;
|
|
|
|
while (isset($scope_ptr->parent)) {
|
|
|
|
$scope_ptr = $scope_ptr->parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$_config_vars = array();
|
|
|
|
include($this->getCompiledFilepath());
|
|
|
|
// copy global config vars
|
|
|
|
foreach ($_config_vars['vars'] as $variable => $value) {
|
|
|
|
if ($this->smarty->config_overwrite || !isset($scope_ptr->config_vars[$variable])) {
|
|
|
|
$scope_ptr->config_vars[$variable] = $value;
|
|
|
|
} else {
|
|
|
|
$scope_ptr->config_vars[$variable] = array_merge((array) $scope_ptr->config_vars[$variable], (array) $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// scan sections
|
|
|
|
if (!empty($sections)) {
|
2014-09-07 13:38:28 +02:00
|
|
|
foreach ((array) $sections as $this_section) {
|
|
|
|
if (isset($_config_vars['sections'][$this_section])) {
|
2012-07-04 07:23:08 +02:00
|
|
|
foreach ($_config_vars['sections'][$this_section]['vars'] as $variable => $value) {
|
|
|
|
if ($this->smarty->config_overwrite || !isset($scope_ptr->config_vars[$variable])) {
|
|
|
|
$scope_ptr->config_vars[$variable] = $value;
|
|
|
|
} else {
|
|
|
|
$scope_ptr->config_vars[$variable] = array_merge((array) $scope_ptr->config_vars[$variable], (array) $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set Smarty property in template context
|
|
|
|
*
|
2014-09-07 13:38:28 +02:00
|
|
|
* @param string $property_name property name
|
|
|
|
* @param mixed $value value
|
|
|
|
*
|
2012-07-04 07:23:08 +02:00
|
|
|
* @throws SmartyException if $property_name is not valid
|
|
|
|
*/
|
|
|
|
public function __set($property_name, $value)
|
|
|
|
{
|
|
|
|
switch ($property_name) {
|
|
|
|
case 'source':
|
|
|
|
case 'compiled':
|
|
|
|
$this->$property_name = $value;
|
2014-09-07 13:38:28 +02:00
|
|
|
|
2012-07-04 07:23:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new SmartyException("invalid config property '$property_name'.");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get Smarty property in template context
|
|
|
|
*
|
2014-09-07 13:38:28 +02:00
|
|
|
* @param string $property_name property name
|
|
|
|
*
|
|
|
|
* @return \Smarty_Config_Source|\Smarty_Template_Compiled
|
2012-07-04 07:23:08 +02:00
|
|
|
* @throws SmartyException if $property_name is not valid
|
|
|
|
*/
|
|
|
|
public function __get($property_name)
|
|
|
|
{
|
|
|
|
switch ($property_name) {
|
|
|
|
case 'source':
|
|
|
|
if (empty($this->config_resource)) {
|
|
|
|
throw new SmartyException("Unable to parse resource name \"{$this->config_resource}\"");
|
|
|
|
}
|
|
|
|
$this->source = Smarty_Resource::config($this);
|
2014-09-07 13:38:28 +02:00
|
|
|
|
2012-07-04 07:23:08 +02:00
|
|
|
return $this->source;
|
|
|
|
|
|
|
|
case 'compiled':
|
|
|
|
$this->compiled = $this->source->getCompiled($this);
|
2014-09-07 13:38:28 +02:00
|
|
|
|
2012-07-04 07:23:08 +02:00
|
|
|
return $this->compiled;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new SmartyException("config attribute '$property_name' does not exist.");
|
|
|
|
}
|
|
|
|
}
|