Initial xentral_oss_20.3.c9ffacf
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\TransferSmartyTemplate\Smarty;
|
||||
|
||||
use Smarty_Security;
|
||||
|
||||
/**
|
||||
* @see https://www.smarty.net/docs/en/advanced.features.tpl#advanced.features.security
|
||||
*/
|
||||
final class SmartySecurity extends Smarty_Security
|
||||
{
|
||||
/** @var array $php_functions Allowed PHP functions */
|
||||
public $php_functions = ['isset', 'empty', 'count', 'sizeof', 'in_array', 'is_array', 'time'];
|
||||
|
||||
/** @var array $php_modifiers Allowed PHP modifiers */
|
||||
public $php_modifiers = ['count', 'nl2br'];
|
||||
|
||||
/** @var int $php_handling Remove native PHP tags */
|
||||
public $php_handling = \Smarty::PHP_REMOVE;
|
||||
}
|
||||
@@ -0,0 +1,239 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\TransferSmartyTemplate\Smarty;
|
||||
|
||||
use Smarty_Internal_Template;
|
||||
use Xentral\Modules\TransferSmartyTemplate\Exception\TransferTemplateUserException;
|
||||
use Xentral\Modules\TransferSmartyTemplate\TemplateHelper\CommonHelper;
|
||||
use Xentral\Modules\TransferSmartyTemplate\TemplateHelper\CsvHelper;
|
||||
use Xentral\Modules\TransferSmartyTemplate\TemplateHelper\HtmlUrlHelper;
|
||||
use Xentral\Modules\TransferSmartyTemplate\TemplateHelper\XmlHelper;
|
||||
|
||||
final class SmartyTemplateHelper
|
||||
{
|
||||
/** @var XmlHelper $xmlHelper */
|
||||
private $xmlHelper;
|
||||
|
||||
/** @var CsvHelper $csvHelper */
|
||||
private $csvHelper;
|
||||
|
||||
/** @var HtmlUrlHelper $htmlHelper */
|
||||
private $htmlHelper;
|
||||
|
||||
/** @var CommonHelper $commonHelper */
|
||||
private $commonHelper;
|
||||
|
||||
/**
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->xmlHelper = new XmlHelper();
|
||||
$this->csvHelper = new CsvHelper();
|
||||
$this->htmlHelper = new HtmlUrlHelper();
|
||||
$this->commonHelper = new CommonHelper();
|
||||
}
|
||||
|
||||
/**
|
||||
* @example {escapeXml}{$evilString}{/escapeXml}
|
||||
* @example {escapeXml charset="UTF-32"}{$evilString}{/escapeXml}
|
||||
*
|
||||
* @param array $params
|
||||
* @param mixed $content
|
||||
* @param Smarty_Internal_Template $template
|
||||
* @param bool $repeat
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function compileBlockEscapeXml($params, $content, $template, &$repeat)
|
||||
{
|
||||
// Only output on closing tag @see https://www.smarty.net/docs/en/plugins.block.functions.tpl
|
||||
if ($repeat === true) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$charset = isset($params['charset']) && !empty($params['charset']) ? (string)$params['charset'] : 'UTF-8';
|
||||
|
||||
return $this->xmlHelper->escapeXml($content, $charset);
|
||||
}
|
||||
|
||||
/**
|
||||
* @example {escapeXml}{$evilString}{/escapeXml}
|
||||
* @example {escapeXml charset="UTF-32"}{$evilString}{/escapeXml}
|
||||
*
|
||||
* @param array $params
|
||||
* @param mixed $content
|
||||
* @param Smarty_Internal_Template $template
|
||||
* @param bool $repeat
|
||||
*
|
||||
* @throws TransferTemplateUserException
|
||||
*/
|
||||
public function compileBlockError($params, $content, $template, &$repeat)
|
||||
{
|
||||
if ($repeat === true) {
|
||||
return null;
|
||||
}
|
||||
|
||||
throw new TransferTemplateUserException($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* @example `{cdata}Long text data{/cdata}` => Ausgabe `<![CDATA[Long text data]]>`
|
||||
*
|
||||
* @param array $params
|
||||
* @param mixed $content
|
||||
* @param Smarty_Internal_Template $template
|
||||
* @param bool $repeat
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function compileBlockCdata($params, $content, $template, &$repeat)
|
||||
{
|
||||
// Only output on closing tag @see https://www.smarty.net/docs/en/plugins.block.functions.tpl
|
||||
if ($repeat === true) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->xmlHelper->createXmlCdataSection($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* @example {$url|urlEncode}
|
||||
*
|
||||
* @param mixed $content
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function compileModifierEncodeUrl($content)
|
||||
{
|
||||
return $this->htmlHelper->encodeUrl($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* @example {$url|urlDecode}
|
||||
*
|
||||
* @param mixed $content
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function compileModifierDecodeUrl($content)
|
||||
{
|
||||
return $this->htmlHelper->decodeUrl($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* @example `{"Inhalt"|cdata}` => Ausgabe `<![CDATA[Inhalt]]>`
|
||||
*
|
||||
* @param string $content
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function compileModifierCdata($content)
|
||||
{
|
||||
return $this->xmlHelper->createXmlCdataSection($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $content
|
||||
*/
|
||||
public function compileModifierError($content)
|
||||
{
|
||||
throw new TransferTemplateUserException($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* @example {$value|escapeXml}
|
||||
*
|
||||
* @param string $content
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function compileModifierEscapeXml($content)
|
||||
{
|
||||
return $this->xmlHelper->escapeXml($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* @example {$value|quoteCsv}
|
||||
* @example {$value|quoteCsv:"'"}
|
||||
*
|
||||
*
|
||||
* @param string $content
|
||||
* @param string $quoteChar
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function compileModifierQuoteCsv($content, $quoteChar = '"')
|
||||
{
|
||||
if (empty($quoteChar)) {
|
||||
$quoteChar = '"';
|
||||
}
|
||||
|
||||
return $this->csvHelper->quoteCsv($content, (string)$quoteChar);
|
||||
}
|
||||
|
||||
/**
|
||||
* @example {$value|htmlEntitiesDecode}
|
||||
*
|
||||
* @param string $content
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function compileModifierDecodeHtmlEntities($content)
|
||||
{
|
||||
return $this->htmlHelper->decodeHtmlEntities($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* @example {$value|htmlSpecialCharsDecode}
|
||||
*
|
||||
* @param string $content
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function compileModifierDecodeHtmlSpecialChars($content)
|
||||
{
|
||||
return $this->htmlHelper->decodeHtmlSpecialChars($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts HTML-BR-Tags to line breaks
|
||||
*
|
||||
* @example {$value|br2nl}
|
||||
*
|
||||
* @param string $content
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function compileModifierBr2Nl($content)
|
||||
{
|
||||
return $this->htmlHelper->convertBr2LineBreak($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* @example {$var|replaceLineBreaks}
|
||||
* @example {$var|replaceLineBreaks:"ZEILENUMBRUCH"}
|
||||
*
|
||||
* @param string $string
|
||||
* @param string $replaceChar
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function compileModifierReplaceLineBreaks($string, $replaceChar = ' ')
|
||||
{
|
||||
return $this->commonHelper->replaceLineBreaks($string, $replaceChar);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dumps a template variable
|
||||
*
|
||||
* @example {$var|dump}
|
||||
*
|
||||
* @param mixed $data
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function compileModifierDumpVariable($data)
|
||||
{
|
||||
return $this->commonHelper->dumpVariable($data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\TransferSmartyTemplate\Smarty;
|
||||
|
||||
use Exception;
|
||||
use Smarty;
|
||||
use Xentral\Modules\TransferSmartyTemplate\Exception\FilesystemFailureException;
|
||||
use Xentral\Modules\TransferSmartyTemplate\Exception\InvalidArgumentException;
|
||||
use Xentral\Modules\TransferSmartyTemplate\Exception\TransferTemplateException;
|
||||
|
||||
final class SmartyWrapper
|
||||
{
|
||||
/** @var Smarty $smarty */
|
||||
private $smarty;
|
||||
|
||||
/**
|
||||
* @param Smarty $smarty
|
||||
*/
|
||||
public function __construct(Smarty $smarty)
|
||||
{
|
||||
$this->smarty = $smarty;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type plugin type
|
||||
* @param string $name name of template tag
|
||||
* @param callback $callback PHP callback to register
|
||||
*
|
||||
* @return \Smarty|\Smarty_Internal_Template
|
||||
* @throws \SmartyException
|
||||
*/
|
||||
public function registerPlugin($type, $name, $callback)
|
||||
{
|
||||
return $this->smarty->registerPlugin($type, $name, $callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see Smarty_Internal_Template::fetch()
|
||||
*
|
||||
* @param string $template
|
||||
*
|
||||
* @throws TransferTemplateException
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function fetch($template = null)
|
||||
{
|
||||
try {
|
||||
return $this->smarty->fetch($template);
|
||||
} catch (Exception $exception) {
|
||||
throw new TransferTemplateException($exception->getMessage(), $exception->getCode(), $exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see Smarty_Internal_Data::assign()
|
||||
*
|
||||
* @param array|string $tplVar
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function assign($tplVar, $value = null)
|
||||
{
|
||||
$this->smarty->assign($tplVar, $value, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see Smarty_Internal_Data::append()
|
||||
*
|
||||
* @param array|string $tplVar
|
||||
* @param mixed $value
|
||||
* @param bool $merge
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function append($tplVar, $value = null, $merge = false)
|
||||
{
|
||||
$this->smarty->append($tplVar, $value, $merge, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|array $tplVar
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function clearAssign($tplVar)
|
||||
{
|
||||
$this->smarty->clearAssign($tplVar);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $varName
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getTemplateVars($varName = null)
|
||||
{
|
||||
return $this->smarty->getTemplateVars($varName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $segment
|
||||
* @param string $templateDir
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
* @throws FilesystemFailureException
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function addTemplateDir($segment, $templateDir)
|
||||
{
|
||||
if (empty($segment)) {
|
||||
throw new InvalidArgumentException('Required parameter $segment is empty.');
|
||||
}
|
||||
|
||||
if (!is_dir($templateDir)) {
|
||||
if (!mkdir($templateDir, 0777, true) && !is_dir($templateDir)) {
|
||||
throw new FilesystemFailureException(
|
||||
sprintf('Can not create directory "%s" for segment "%s".', $templateDir, $segment)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$this->smarty->addTemplateDir($templateDir, $segment, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $segment
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getTemplateDir($segment)
|
||||
{
|
||||
return $this->smarty->getTemplateDir($segment, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getTemplateDirs()
|
||||
{
|
||||
return $this->smarty->getTemplateDir(null, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $template
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function existsTemplate($segment, $template)
|
||||
{
|
||||
try {
|
||||
$resourceName = sprintf('[%s]%s', $segment, $template);
|
||||
return $this->smarty->templateExists($resourceName);
|
||||
} catch (Exception $exception) {
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user