Initial checkin

This commit is contained in:
genuineparts 2025-06-20 19:10:23 +02:00
commit d75eb444fc
4304 changed files with 369634 additions and 0 deletions

View file

@ -0,0 +1,145 @@
<?php
namespace Smarty\Resource;
use Smarty\Exception;
use Smarty\Smarty;
use Smarty\Template;
use Smarty\Template\Source;
/**
* Smarty Resource Plugin
* Base implementation for resource plugins
* @author Rodney Rehm
*/
abstract class BasePlugin
{
/**
* resource types provided by the core
*
* @var array
*/
public static $sysplugins = [
'file' => FilePlugin::class,
'string' => StringPlugin::class,
'extends' => ExtendsPlugin::class,
'stream' => StreamPlugin::class,
'eval' => StringEval::class,
];
/**
* Source must be recompiled on every occasion
*
* @var boolean
*/
public $recompiled = false;
/**
* Flag if resource does allow compilation
*
* @return bool
*/
public function supportsCompiledTemplates(): bool {
return true;
}
/**
* Check if resource must check time stamps when loading compiled or cached templates.
* Resources like 'extends' which use source components my disable timestamp checks on own resource.
* @return bool
*/
public function checkTimestamps()
{
return true;
}
/**
* Load Resource Handler
*
* @param Smarty $smarty smarty object
* @param string $type name of the resource
*
* @return BasePlugin Resource Handler
* @throws Exception
*/
public static function load(Smarty $smarty, $type)
{
// try smarty's cache
if (isset($smarty->_resource_handlers[ $type ])) {
return $smarty->_resource_handlers[ $type ];
}
// try registered resource
if (isset($smarty->registered_resources[ $type ])) {
return $smarty->_resource_handlers[ $type ] = $smarty->registered_resources[ $type ];
}
// try sysplugins dir
if (isset(self::$sysplugins[ $type ])) {
$_resource_class = self::$sysplugins[ $type ];
return $smarty->_resource_handlers[ $type ] = new $_resource_class();
}
// try plugins dir
$_resource_class = 'Smarty_Resource_' . \smarty_ucfirst_ascii($type);
if (class_exists($_resource_class, false)) {
return $smarty->_resource_handlers[ $type ] = new $_resource_class();
}
// 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);
}
return $smarty->_resource_handlers[ $type ] = new StreamPlugin();
}
// TODO: try default_(template|config)_handler
// give up
throw new \Smarty\Exception("Unknown resource type '{$type}'");
}
/**
* Load template's source into current template object
*
* @param Source $source source object
*
* @return string template source
* @throws \Smarty\Exception if source cannot be loaded
*/
abstract public function getContent(Source $source);
/**
* populate Source Object with metadata from Resource
*
* @param Source $source source object
* @param Template|null $_template template object
*/
abstract public function populate(Source $source, ?\Smarty\Template $_template = null);
/**
* populate Source Object with timestamp and exists from Resource
*
* @param Source $source source object
*/
public function populateTimestamp(Source $source)
{
// intentionally left blank
}
/*
* Check if resource must check time stamps when when loading complied or cached templates.
* Resources like 'extends' which use source components my disable timestamp checks on own resource.
*
* @return bool
*/
/**
* Determine basename for compiled filename
*
* @param \Smarty\Template\Source $source source object
*
* @return string resource's basename
*/
public function getBasename(\Smarty\Template\Source $source)
{
return basename(preg_replace('![^\w]+!', '_', $source->name));
}
}

View file

@ -0,0 +1,105 @@
<?php
/**
* Smarty Resource Plugin
*
* @author Rodney Rehm
*/
namespace Smarty\Resource;
use Smarty\Smarty;
use Smarty\Template;
use Smarty\Template\Source;
use Smarty\Exception;
/**
* Smarty Resource Plugin
* Wrapper Implementation for custom resource plugins
*
*/
abstract class CustomPlugin extends BasePlugin {
/**
* fetch template and its modification time from data source
*
* @param string $name template name
* @param string &$source template source
* @param integer &$mtime template modification timestamp (epoch)
*/
abstract protected function fetch($name, &$source, &$mtime);
/**
* Fetch template's modification timestamp from data source
* {@internal implementing this method is optional.
* Only implement it if modification times can be accessed faster than loading the complete template source.}}
*
* @param string $name template name
*
* @return integer|boolean timestamp (epoch) the template was modified, or false if not found
*/
protected function fetchTimestamp($name) {
return null;
}
/**
* populate Source Object with metadata from Resource
*
* @param Source $source source object
* @param Template|null $_template template object
*/
public function populate(Source $source, ?Template $_template = null) {
$source->uid = sha1($source->type . ':' . $source->name);
$mtime = $this->fetchTimestamp($source->name);
if ($mtime !== null) {
$source->timestamp = $mtime;
} else {
$this->fetch($source->name, $content, $timestamp);
$source->timestamp = $timestamp ?? false;
if (isset($content)) {
$source->content = $content;
}
}
$source->exists = !!$source->timestamp;
}
/**
* Load template's source into current template object
*
* @param Source $source source object
*
* @return string template source
* @throws Exception if source cannot be loaded
*/
public function getContent(Source $source) {
$this->fetch($source->name, $content, $timestamp);
if (isset($content)) {
return $content;
}
throw new Exception("Unable to read template {$source->type} '{$source->name}'");
}
/**
* Determine basename for compiled filename
*
* @param Source $source source object
*
* @return string resource's basename
*/
public function getBasename(Source $source) {
return basename($this->generateSafeName($source->name));
}
/**
* Removes special characters from $name and limits its length to 127 characters.
*
* @param $name
*
* @return string
*/
private function generateSafeName($name): string {
return substr(preg_replace('/[^A-Za-z0-9._]/', '', (string)$name), 0, 127);
}
}

View file

@ -0,0 +1,116 @@
<?php
namespace Smarty\Resource;
use Smarty\Exception;
use Smarty\Template;
use Smarty\Template\Source;
/**
* Smarty Internal Plugin Resource Extends
* Implements the file system as resource for Smarty which {extend}s a chain of template files templates
* @author Uwe Tews
* @author Rodney Rehm
*/
class ExtendsPlugin extends BasePlugin
{
/**
* populate Source Object with metadata from Resource
*
* @param Source $source source object
* @param Template|null $_template template object
*
* @throws Exception
*/
public function populate(Source $source, ?Template $_template = null)
{
$uid = '';
$sources = array();
$components = explode('|', $source->name);
$smarty = $source->getSmarty();
$exists = true;
foreach ($components as $component) {
$_s = Source::load(null, $smarty, $component);
$sources[ $_s->uid ] = $_s;
$uid .= $_s->uid;
if ($_template) {
$exists = $exists && $_s->exists;
}
}
$source->components = $sources;
$source->uid = sha1($uid . $source->getSmarty()->_joined_template_dir);
$source->exists = $exists;
if ($_template) {
$source->timestamp = $_s->timestamp;
}
}
/**
* populate Source Object with timestamp and exists from Resource
*
* @param Source $source source object
*/
public function populateTimestamp(Source $source)
{
$source->exists = true;
/* @var Source $_s */
foreach ($source->components as $_s) {
$source->exists = $source->exists && $_s->exists;
}
$source->timestamp = $source->exists ? $_s->getTimeStamp() : false;
}
/**
* Load template's source from files into current template object
*
* @param Source $source source object
*
* @return string template source
* @throws \Smarty\Exception if source cannot be loaded
*/
public function getContent(Source $source)
{
if (!$source->exists) {
throw new \Smarty\Exception("Unable to load '{$source->type}:{$source->name}'");
}
$_components = array_reverse($source->components);
$_content = '';
/* @var Source $_s */
foreach ($_components as $_s) {
// read content
$_content .= $_s->getContent();
}
return $_content;
}
/**
* Determine basename for compiled filename
*
* @param Source $source source object
*
* @return string resource's basename
*/
public function getBasename(Source $source)
{
$search = array(':');
if (\Smarty\Smarty::$_IS_WINDOWS) {
$search = array(':', '|');
}
return str_replace($search, '.', basename($source->getResourceName()));
}
/*
* Disable timestamp checks for extends resource.
* The individual source components will be checked.
*
* @return bool
*/
/**
* @return bool
*/
public function checkTimestamps()
{
return false;
}
}

View file

@ -0,0 +1,183 @@
<?php
/**
* Smarty Internal Plugin Resource File
*
* @author Uwe Tews
* @author Rodney Rehm
*/
namespace Smarty\Resource;
use Smarty\Smarty;
use Smarty\Template;
use Smarty\Template\Source;
use Smarty\Exception;
/**
* Smarty Internal Plugin Resource File
* Implements the file system as resource for Smarty templates
*
*/
class FilePlugin extends BasePlugin {
/**
* populate Source Object with metadata from Resource
*
* @param Source $source source object
* @param Template|null $_template template object
*
* @throws Exception
*/
public function populate(Source $source, ?Template $_template = null) {
$source->uid = sha1(
$source->name . ($source->isConfig ? $source->getSmarty()->_joined_config_dir :
$source->getSmarty()->_joined_template_dir)
);
if ($path = $this->getFilePath($source->name, $source->getSmarty(), $source->isConfig)) {
if (isset($source->getSmarty()->security_policy) && is_object($source->getSmarty()->security_policy)) {
$source->getSmarty()->security_policy->isTrustedResourceDir($path, $source->isConfig);
}
$source->exists = true;
$source->timestamp = filemtime($path);
} else {
$source->timestamp = $source->exists = false;
}
}
/**
* populate Source Object with timestamp and exists from Resource
*
* @param Source $source source object
*/
public function populateTimestamp(Source $source) {
$path = $this->getFilePath($source->name, $source->getSmarty(), $source->isConfig);
if (!$source->exists) {
$source->exists = ($path !== false && is_file($path));
}
if ($source->exists && $path !== false) {
$source->timestamp = filemtime($path);
} else {
$source->timestamp = 0;
}
}
/**
* Load template's source from file into current template object
*
* @param Source $source source object
*
* @return string template source
* @throws Exception if source cannot be loaded
*/
public function getContent(Source $source) {
if ($source->exists) {
return file_get_contents($this->getFilePath($source->getResourceName(), $source->getSmarty(), $source->isConfig()));
}
throw new Exception(
'Unable to read ' . ($source->isConfig ? 'config' : 'template') .
" {$source->type} '{$source->name}'"
);
}
/**
* Determine basename for compiled filename
*
* @param Source $source source object
*
* @return string resource's basename
*/
public function getBasename(Source $source) {
return basename($source->getResourceName());
}
/**
* build template filepath by traversing the template_dir array
*
* @param $file
* @param Smarty $smarty
* @param bool $isConfig
*
* @return string fully qualified filepath
*/
public function getFilePath($file, \Smarty\Smarty $smarty, bool $isConfig = false) {
// absolute file ?
if ($file[0] === '/' || $file[1] === ':') {
$file = $smarty->_realpath($file, true);
return is_file($file) ? $file : false;
}
// normalize DIRECTORY_SEPARATOR
if (strpos($file, DIRECTORY_SEPARATOR === '/' ? '\\' : '/') !== false) {
$file = str_replace(DIRECTORY_SEPARATOR === '/' ? '\\' : '/', DIRECTORY_SEPARATOR, $file);
}
$_directories = $smarty->getTemplateDir(null, $isConfig);
// template_dir index?
if ($file[0] === '[' && preg_match('#^\[([^\]]+)\](.+)$#', $file, $fileMatch)) {
$file = $fileMatch[2];
$_indices = explode(',', $fileMatch[1]);
$_index_dirs = [];
foreach ($_indices as $index) {
$index = trim($index);
// try string indexes
if (isset($_directories[$index])) {
$_index_dirs[] = $_directories[$index];
} elseif (is_numeric($index)) {
// try numeric index
$index = (int)$index;
if (isset($_directories[$index])) {
$_index_dirs[] = $_directories[$index];
} else {
// try at location index
$keys = array_keys($_directories);
if (isset($_directories[$keys[$index]])) {
$_index_dirs[] = $_directories[$keys[$index]];
}
}
}
}
if (empty($_index_dirs)) {
// index not found
return false;
} else {
$_directories = $_index_dirs;
}
}
// relative file name?
foreach ($_directories as $_directory) {
$path = $_directory . $file;
if (is_file($path)) {
return (strpos($path, '.' . DIRECTORY_SEPARATOR) !== false) ? $smarty->_realpath($path) : $path;
}
}
if (!isset($_index_dirs)) {
// Could be relative to cwd
$path = $smarty->_realpath($file, true);
if (is_file($path)) {
return $path;
}
}
return false;
}
/**
* Returns the timestamp of the resource indicated by $resourceName, or false if it doesn't exist.
*
* @param string $resourceName
* @param Smarty $smarty
* @param bool $isConfig
*
* @return false|int
*/
public function getResourceNameTimestamp(string $resourceName, \Smarty\Smarty $smarty, bool $isConfig = false) {
if ($path = $this->getFilePath($resourceName, $smarty, $isConfig)) {
return filemtime($path);
}
return false;
}
}

View file

@ -0,0 +1,50 @@
<?php
/**
* Smarty Resource Plugin
*
* @author Rodney Rehm
*/
namespace Smarty\Resource;
use Smarty\Template;
/**
* Smarty Resource Plugin
* Base implementation for resource plugins that don't compile cache
*
*/
abstract class RecompiledPlugin extends BasePlugin {
/**
* Flag that it's an recompiled resource
*
* @var bool
*/
public $recompiled = true;
/**
* Flag if resource does allow compilation
*
* @return bool
*/
public function supportsCompiledTemplates(): bool {
return false;
}
/*
* Disable timestamp checks for recompiled resource.
*
* @return bool
*/
/**
* @return bool
*/
public function checkTimestamps() {
return false;
}
}

View file

@ -0,0 +1,71 @@
<?php
/**
* Smarty Internal Plugin Resource Stream
* Implements the streams as resource for Smarty template
*
* @author Uwe Tews
* @author Rodney Rehm
*/
namespace Smarty\Resource;
use Smarty\Smarty;
use Smarty\Template;
use Smarty\Template\Source;
/**
* Smarty Internal Plugin Resource Stream
* Implements the streams as resource for Smarty template
*
* @link https://php.net/streams
*/
class StreamPlugin extends RecompiledPlugin {
/**
* populate Source Object with meta data from Resource
*
* @param Source $source source object
* @param Template $_template template object
*
* @return void
*/
public function populate(Source $source, ?Template $_template = null) {
$source->uid = false;
$source->content = $this->getContent($source);
$source->timestamp = $source->exists = !!$source->content;
}
/**
* Load template's source from stream into current template object
*
* @param Source $source source object
*
* @return string template source
*/
public function getContent(Source $source) {
if (strpos($source->getResourceName(), '://') !== false) {
$filepath = $source->getResourceName();
} else {
$filepath = str_replace(':', '://', $source->getFullResourceName());
}
$t = '';
// the availability of the stream has already been checked in Smarty\Resource\Base::fetch()
$fp = fopen($filepath, 'r+');
if ($fp) {
while (!feof($fp) && ($current_line = fgets($fp)) !== false) {
$t .= $current_line;
}
fclose($fp);
return $t;
} else {
return false;
}
}
}

View file

@ -0,0 +1,85 @@
<?php
namespace Smarty\Resource;
use Smarty\Smarty;
/**
* Smarty Internal Plugin Resource Eval
*
* @author Uwe Tews
* @author Rodney Rehm
*/
/**
* Smarty Internal Plugin Resource Eval
* Implements the strings as resource for Smarty template
* {@internal unlike string-resources the compiled state of eval-resources is NOT saved for subsequent access}}
*
*/
class StringEval extends RecompiledPlugin
{
/**
* populate Source Object with meta data from Resource
*
* @param \Smarty\Template\Source $source source object
* @param \Smarty\Template $_template template object
*
* @return void
*/
public function populate(\Smarty\Template\Source $source, ?\Smarty\Template $_template = null)
{
$source->uid = sha1($source->name);
$source->timestamp = $source->exists = true;
}
/**
* Load template's source from $resource_name into current template object
*
* @param \Smarty\Template\Source $source source object
*
* @return string template source
*@uses decode() to decode base64 and urlencoded template_resources
*
*/
public function getContent(\Smarty\Template\Source $source)
{
return $this->decode($source->name);
}
/**
* decode base64 and urlencode
*
* @param string $string template_resource to decode
*
* @return string decoded template_resource
*/
protected function decode($string)
{
// decode if specified
if (($pos = strpos($string, ':')) !== false) {
if (!strncmp($string, 'base64', 6)) {
return base64_decode(substr($string, 7));
} elseif (!strncmp($string, 'urlencode', 9)) {
return urldecode(substr($string, 10));
}
}
return $string;
}
/**
* Determine basename for compiled filename
*
* @param \Smarty\Template\Source $source source object
*
* @return string resource's basename
*/
public function getBasename(\Smarty\Template\Source $source)
{
return '';
}
}

View file

@ -0,0 +1,94 @@
<?php
/**
* Smarty Internal Plugin Resource String
*
* @author Uwe Tews
* @author Rodney Rehm
*/
namespace Smarty\Resource;
use Smarty\Smarty;
use Smarty\Template;
use Smarty\Template\Source;
/**
* Smarty Internal Plugin Resource String
* Implements the strings as resource for Smarty template
* {@internal unlike eval-resources the compiled state of string-resources is saved for subsequent access}}
*
*/
class StringPlugin extends BasePlugin {
/**
* populate Source Object with metadata from Resource
*
* @param Source $source source object
* @param Template $_template template object
*
* @return void
*/
public function populate(Source $source, ?Template $_template = null) {
$source->uid = sha1($source->name);
$source->timestamp = $source->exists = true;
}
/**
* Load template's source from $resource_name into current template object
*
* @param Source $source source object
*
* @return string template source
* @uses decode() to decode base64 and urlencoded template_resources
*
*/
public function getContent(Source $source) {
return $this->decode($source->name);
}
/**
* decode base64 and urlencode
*
* @param string $string template_resource to decode
*
* @return string decoded template_resource
*/
protected function decode($string) {
// decode if specified
if (($pos = strpos($string, ':')) !== false) {
if (!strncmp($string, 'base64', 6)) {
return base64_decode(substr($string, 7));
} elseif (!strncmp($string, 'urlencode', 9)) {
return urldecode(substr($string, 10));
}
}
return $string;
}
/**
* Determine basename for compiled filename
* Always returns an empty string.
*
* @param Source $source source object
*
* @return string resource's basename
*/
public function getBasename(Source $source) {
return '';
}
/*
* Disable timestamp checks for string resource.
*
* @return bool
*/
/**
* @return bool
*/
public function checkTimestamps() {
return false;
}
}