2015-08-23 22:38:18 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Smarty Resource Plugin
|
|
|
|
*
|
|
|
|
* @package Smarty
|
|
|
|
* @subpackage TemplateResources
|
|
|
|
* @author Rodney Rehm
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Smarty Resource Plugin
|
|
|
|
* Base implementation for resource plugins
|
|
|
|
*
|
|
|
|
* @package Smarty
|
|
|
|
* @subpackage TemplateResources
|
|
|
|
*/
|
|
|
|
abstract class Smarty_Resource
|
|
|
|
{
|
|
|
|
/**
|
2016-01-12 02:47:38 +01:00
|
|
|
* Source is bypassing compiler
|
2015-08-23 22:38:18 +02:00
|
|
|
*
|
2016-01-12 02:47:38 +01:00
|
|
|
* @var boolean
|
2015-08-23 22:38:18 +02:00
|
|
|
*/
|
2016-01-12 02:47:38 +01:00
|
|
|
public $uncompiled = false;
|
|
|
|
|
2015-08-23 22:38:18 +02:00
|
|
|
/**
|
2016-01-12 02:47:38 +01:00
|
|
|
* Source must be recompiled on every occasion
|
2015-08-23 22:38:18 +02:00
|
|
|
*
|
2016-01-12 02:47:38 +01:00
|
|
|
* @var boolean
|
2015-08-23 22:38:18 +02:00
|
|
|
*/
|
2016-01-12 02:47:38 +01:00
|
|
|
public $recompiled = false;
|
|
|
|
|
2015-08-23 22:38:18 +02:00
|
|
|
/**
|
2016-01-12 02:47:38 +01:00
|
|
|
* resource types provided by the core
|
2015-08-23 22:38:18 +02:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-01-12 02:47:38 +01:00
|
|
|
public static $sysplugins = array('file' => 'smarty_internal_resource_file.php',
|
|
|
|
'string' => 'smarty_internal_resource_string.php',
|
|
|
|
'extends' => 'smarty_internal_resource_extends.php',
|
|
|
|
'stream' => 'smarty_internal_resource_stream.php',
|
|
|
|
'eval' => 'smarty_internal_resource_eval.php',
|
|
|
|
'php' => 'smarty_internal_resource_php.php');
|
|
|
|
|
2015-08-23 22:38:18 +02:00
|
|
|
/**
|
2016-01-12 02:47:38 +01:00
|
|
|
* Flag if resource does implement populateCompiledFilepath() method
|
2015-08-23 22:38:18 +02:00
|
|
|
*
|
2016-01-12 02:47:38 +01:00
|
|
|
* @var bool
|
2015-08-23 22:38:18 +02:00
|
|
|
*/
|
2016-01-12 02:47:38 +01:00
|
|
|
public $hasCompiledHandler = false;
|
2015-08-23 22:38:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of the Class to compile this resource's contents with
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $compiler_class = 'Smarty_Internal_SmartyTemplateCompiler';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of the Class to tokenize this resource's contents with
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $template_lexer_class = 'Smarty_Internal_Templatelexer';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of the Class to parse this resource's contents with
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $template_parser_class = 'Smarty_Internal_Templateparser';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load template's source into current template object
|
|
|
|
*
|
|
|
|
* @param Smarty_Template_Source $source source object
|
|
|
|
*
|
|
|
|
* @return string template source
|
|
|
|
* @throws SmartyException if source cannot be loaded
|
|
|
|
*/
|
|
|
|
abstract public function getContent(Smarty_Template_Source $source);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* populate Source Object with meta data from Resource
|
|
|
|
*
|
|
|
|
* @param Smarty_Template_Source $source source object
|
|
|
|
* @param Smarty_Internal_Template $_template template object
|
|
|
|
*/
|
|
|
|
abstract public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* populate Source Object with timestamp and exists from Resource
|
|
|
|
*
|
|
|
|
* @param Smarty_Template_Source $source source object
|
|
|
|
*/
|
|
|
|
public function populateTimestamp(Smarty_Template_Source $source)
|
|
|
|
{
|
|
|
|
// intentionally left blank
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* modify resource_name according to resource handlers specifications
|
|
|
|
*
|
|
|
|
* @param Smarty $smarty Smarty instance
|
|
|
|
* @param string $resource_name resource_name to make unique
|
2016-01-12 02:47:38 +01:00
|
|
|
* @param boolean $isConfig flag for config resource
|
2015-08-23 22:38:18 +02:00
|
|
|
*
|
|
|
|
* @return string unique resource name
|
|
|
|
*/
|
2016-01-12 02:47:38 +01:00
|
|
|
public function buildUniqueResourceName(Smarty $smarty, $resource_name, $isConfig = false)
|
2015-08-23 22:38:18 +02:00
|
|
|
{
|
2016-01-12 02:47:38 +01:00
|
|
|
if ($isConfig) {
|
|
|
|
if (!isset($smarty->_joined_config_dir)) {
|
|
|
|
$smarty->getTemplateDir(null, true);
|
2015-08-23 22:38:18 +02:00
|
|
|
}
|
2016-01-12 02:47:38 +01:00
|
|
|
return get_class($this) . '#' . $smarty->_joined_config_dir . '#' . $resource_name;
|
2015-08-23 22:38:18 +02:00
|
|
|
} else {
|
2016-01-12 02:47:38 +01:00
|
|
|
if (!isset($smarty->_joined_template_dir)) {
|
|
|
|
$smarty->getTemplateDir();
|
2015-08-23 22:38:18 +02:00
|
|
|
}
|
2016-01-12 02:47:38 +01:00
|
|
|
return get_class($this) . '#' . $smarty->_joined_template_dir . '#' . $resource_name;
|
2015-08-23 22:38:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine basename for compiled filename
|
|
|
|
*
|
|
|
|
* @param Smarty_Template_Source $source source object
|
|
|
|
*
|
|
|
|
* @return string resource's basename
|
|
|
|
*/
|
2016-01-12 02:47:38 +01:00
|
|
|
public function getBasename(Smarty_Template_Source $source)
|
2015-08-23 22:38:18 +02:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load Resource Handler
|
|
|
|
*
|
|
|
|
* @param Smarty $smarty smarty object
|
|
|
|
* @param string $type name of the resource
|
|
|
|
*
|
|
|
|
* @throws SmartyException
|
|
|
|
* @return Smarty_Resource Resource Handler
|
|
|
|
*/
|
|
|
|
public static function load(Smarty $smarty, $type)
|
|
|
|
{
|
|
|
|
// try smarty's cache
|
2016-01-12 02:47:38 +01:00
|
|
|
if (isset($smarty->_cache['resource_handlers'][$type])) {
|
|
|
|
return $smarty->_cache['resource_handlers'][$type];
|
2015-08-23 22:38:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// try registered resource
|
|
|
|
if (isset($smarty->registered_resources[$type])) {
|
2016-01-12 02:47:38 +01:00
|
|
|
return $smarty->_cache['resource_handlers'][$type] =
|
|
|
|
$smarty->registered_resources[$type] instanceof Smarty_Resource ? $smarty->registered_resources[$type] :
|
|
|
|
new Smarty_Internal_Resource_Registered();
|
2015-08-23 22:38:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// try sysplugins dir
|
|
|
|
if (isset(self::$sysplugins[$type])) {
|
2016-01-12 02:47:38 +01:00
|
|
|
$_resource_class = 'Smarty_Internal_Resource_' . ucfirst($type);
|
|
|
|
return $smarty->_cache['resource_handlers'][$type] = new $_resource_class();
|
2015-08-23 22:38:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// try plugins dir
|
|
|
|
$_resource_class = 'Smarty_Resource_' . ucfirst($type);
|
|
|
|
if ($smarty->loadPlugin($_resource_class)) {
|
|
|
|
if (class_exists($_resource_class, false)) {
|
2016-01-12 02:47:38 +01:00
|
|
|
return $smarty->_cache['resource_handlers'][$type] = new $_resource_class();
|
2015-08-23 22:38:18 +02:00
|
|
|
} else {
|
2016-01-12 02:47:38 +01:00
|
|
|
$smarty->registerResource($type,
|
|
|
|
array("smarty_resource_{$type}_source", "smarty_resource_{$type}_timestamp",
|
|
|
|
"smarty_resource_{$type}_secure", "smarty_resource_{$type}_trusted"));
|
2015-08-23 22:38:18 +02:00
|
|
|
// give it another try, now that the resource is registered properly
|
|
|
|
return self::load($smarty, $type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// try streams
|
|
|
|
$_known_stream = stream_get_wrappers();
|
|
|
|
if (in_array($type, $_known_stream)) {
|
|
|
|
// is known stream
|
|
|
|
if (is_object($smarty->security_policy)) {
|
|
|
|
$smarty->security_policy->isTrustedStream($type);
|
|
|
|
}
|
2016-01-12 02:47:38 +01:00
|
|
|
return $smarty->_cache['resource_handlers'][$type] = new Smarty_Internal_Resource_Stream();
|
2015-08-23 22:38:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: try default_(template|config)_handler
|
|
|
|
|
|
|
|
// give up
|
|
|
|
throw new SmartyException("Unknown resource type '{$type}'");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* extract resource_type and resource_name from template_resource and config_resource
|
|
|
|
* @note "C:/foo.tpl" was forced to file resource up till Smarty 3.1.3 (including).
|
|
|
|
*
|
|
|
|
* @param string $resource_name template_resource or config_resource to parse
|
|
|
|
* @param string $default_resource the default resource_type defined in $smarty
|
|
|
|
*
|
2016-01-12 02:47:38 +01:00
|
|
|
* @return array with parsed resource name and type
|
2015-08-23 22:38:18 +02:00
|
|
|
*/
|
2016-01-12 02:47:38 +01:00
|
|
|
public static function parseResourceName($resource_name, $default_resource)
|
2015-08-23 22:38:18 +02:00
|
|
|
{
|
2016-01-12 02:47:38 +01:00
|
|
|
if (preg_match('/^([A-Za-z0-9_\-]{2,})[:]/', $resource_name, $match)) {
|
|
|
|
$type = $match[1];
|
|
|
|
$name = substr($resource_name, strlen($match[0]));
|
|
|
|
} else {
|
2015-08-23 22:38:18 +02:00
|
|
|
// no resource given, use default
|
|
|
|
// or single character before the colon is not a resource type, but part of the filepath
|
|
|
|
$type = $default_resource;
|
|
|
|
$name = $resource_name;
|
|
|
|
}
|
2016-01-12 02:47:38 +01:00
|
|
|
return array($name, $type);
|
2015-08-23 22:38:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* modify template_resource according to resource handlers specifications
|
|
|
|
*
|
2016-01-12 02:47:38 +01:00
|
|
|
* @param \Smarty_Internal_Template|\Smarty $obj Smarty instance
|
|
|
|
* @param string $template_resource template_resource to extract resource handler and name of
|
2015-08-23 22:38:18 +02:00
|
|
|
*
|
|
|
|
* @return string unique resource name
|
|
|
|
*/
|
2016-01-12 02:47:38 +01:00
|
|
|
public static function getUniqueTemplateName($obj, $template_resource)
|
2015-08-23 22:38:18 +02:00
|
|
|
{
|
2016-01-12 02:47:38 +01:00
|
|
|
$smarty = $obj->_objType == 2 ? $obj->smarty : $obj;
|
|
|
|
list($name, $type) = self::parseResourceName($template_resource, $smarty->default_resource_type);
|
2015-08-23 22:38:18 +02:00
|
|
|
// TODO: optimize for Smarty's internal resource types
|
2016-01-12 02:47:38 +01:00
|
|
|
$resource = Smarty_Resource::load($smarty, $type);
|
2015-08-23 22:38:18 +02:00
|
|
|
// go relative to a given template?
|
2016-01-12 02:47:38 +01:00
|
|
|
$_file_is_dotted = $name[0] == '.' && ($name[1] == '.' || $name[1] == '/');
|
|
|
|
if ($obj->_objType == 2 && $_file_is_dotted &&
|
|
|
|
($obj->source->type == 'file' || $obj->parent->source->type == 'extends')
|
|
|
|
) {
|
|
|
|
$name = dirname($obj->source->filepath) . DS . $name;
|
2015-08-23 22:38:18 +02:00
|
|
|
}
|
2016-01-12 02:47:38 +01:00
|
|
|
return $resource->buildUniqueResourceName($smarty, $name);
|
2015-08-23 22:38:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* initialize Source Object for given resource
|
2016-01-12 02:47:38 +01:00
|
|
|
* wrapper for backward compatibility to versions < 3.1.22
|
2015-08-23 22:38:18 +02:00
|
|
|
* Either [$_template] or [$smarty, $template_resource] must be specified
|
|
|
|
*
|
|
|
|
* @param Smarty_Internal_Template $_template template object
|
|
|
|
* @param Smarty $smarty smarty object
|
|
|
|
* @param string $template_resource resource identifier
|
|
|
|
*
|
|
|
|
* @return Smarty_Template_Source Source Object
|
|
|
|
*/
|
2016-01-12 02:47:38 +01:00
|
|
|
public static function source(Smarty_Internal_Template $_template = null, Smarty $smarty = null,
|
|
|
|
$template_resource = null)
|
2015-08-23 22:38:18 +02:00
|
|
|
{
|
2016-01-12 02:47:38 +01:00
|
|
|
return Smarty_Template_Source::load($_template, $smarty, $template_resource);
|
2015-08-23 22:38:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|