vendor/smarty/smarty/libs/sysplugins/smarty_internal_templatebase.php line 114

  1. <?php
  2. /**
  3.  * Smarty Internal Plugin Smarty Template  Base
  4.  * This file contains the basic shared methods for template handling
  5.  *
  6.  * @package    Smarty
  7.  * @subpackage Template
  8.  * @author     Uwe Tews
  9.  */
  10. /**
  11.  * Class with shared smarty/template methods
  12.  *
  13.  * @package    Smarty
  14.  * @subpackage Template
  15.  *
  16.  * @property int $_objType
  17.  *
  18.  * The following methods will be dynamically loaded by the extension handler when they are called.
  19.  * They are located in a corresponding Smarty_Internal_Method_xxxx class
  20.  *
  21.  * @method Smarty_Internal_TemplateBase addAutoloadFilters(mixed $filters, string $type = null)
  22.  * @method Smarty_Internal_TemplateBase addDefaultModifiers(mixed $modifiers)
  23.  * @method Smarty_Internal_TemplateBase addLiterals(mixed $literals)
  24.  * @method Smarty_Internal_TemplateBase createData(Smarty_Internal_Data $parent = null, string $name = null)
  25.  * @method array getAutoloadFilters(string $type = null)
  26.  * @method string getDebugTemplate()
  27.  * @method array getDefaultModifier()
  28.  * @method array getLiterals()
  29.  * @method array getTags(mixed $template = null)
  30.  * @method object getRegisteredObject(string $object_name)
  31.  * @method Smarty_Internal_TemplateBase registerCacheResource(string $name, Smarty_CacheResource $resource_handler)
  32.  * @method Smarty_Internal_TemplateBase registerClass(string $class_name, string $class_impl)
  33.  * @method Smarty_Internal_TemplateBase registerDefaultConfigHandler(callback $callback)
  34.  * @method Smarty_Internal_TemplateBase registerDefaultPluginHandler(callback $callback)
  35.  * @method Smarty_Internal_TemplateBase registerDefaultTemplateHandler(callback $callback)
  36.  * @method Smarty_Internal_TemplateBase registerResource(string $name, mixed $resource_handler)
  37.  * @method Smarty_Internal_TemplateBase setAutoloadFilters(mixed $filters, string $type = null)
  38.  * @method Smarty_Internal_TemplateBase setDebugTemplate(string $tpl_name)
  39.  * @method Smarty_Internal_TemplateBase setDefaultModifiers(mixed $modifiers)
  40.  * @method Smarty_Internal_TemplateBase setLiterals(mixed $literals)
  41.  * @method Smarty_Internal_TemplateBase unloadFilter(string $type, string $name)
  42.  * @method Smarty_Internal_TemplateBase unregisterCacheResource(string $name)
  43.  * @method Smarty_Internal_TemplateBase unregisterObject(string $object_name)
  44.  * @method Smarty_Internal_TemplateBase unregisterPlugin(string $type, string $name)
  45.  * @method Smarty_Internal_TemplateBase unregisterFilter(string $type, mixed $callback)
  46.  * @method Smarty_Internal_TemplateBase unregisterResource(string $name)
  47.  */
  48. abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data
  49. {
  50.     /**
  51.      * Set this if you want different sets of cache files for the same
  52.      * templates.
  53.      *
  54.      * @var string
  55.      */
  56.     public $cache_id null;
  57.     /**
  58.      * Set this if you want different sets of compiled files for the same
  59.      * templates.
  60.      *
  61.      * @var string
  62.      */
  63.     public $compile_id null;
  64.     /**
  65.      * caching enabled
  66.      *
  67.      * @var int
  68.      */
  69.     public $caching Smarty::CACHING_OFF;
  70.     /**
  71.      * check template for modifications?
  72.      *
  73.      * @var int
  74.      */
  75.     public $compile_check Smarty::COMPILECHECK_ON;
  76.     /**
  77.      * cache lifetime in seconds
  78.      *
  79.      * @var integer
  80.      */
  81.     public $cache_lifetime 3600;
  82.     /**
  83.      * Array of source information for known template functions
  84.      *
  85.      * @var array
  86.      */
  87.     public $tplFunctions = array();
  88.     /**
  89.      * universal cache
  90.      *
  91.      * @var array()
  92.      */
  93.     public $_cache = array();
  94.     /**
  95.      * fetches a rendered Smarty template
  96.      *
  97.      * @param string $template   the resource handle of the template file or template object
  98.      * @param mixed  $cache_id   cache id to be used with this template
  99.      * @param mixed  $compile_id compile id to be used with this template
  100.      * @param object $parent     next higher level of Smarty variables
  101.      *
  102.      * @throws Exception
  103.      * @throws SmartyException
  104.      * @return string rendered template output
  105.      */
  106.     public function fetch($template null$cache_id null$compile_id null$parent null)
  107.     {
  108.         $result $this->_execute($template$cache_id$compile_id$parent0);
  109.         return $result === null ob_get_clean() : $result;
  110.     }
  111.     /**
  112.      * displays a Smarty template
  113.      *
  114.      * @param string $template   the resource handle of the template file or template object
  115.      * @param mixed  $cache_id   cache id to be used with this template
  116.      * @param mixed  $compile_id compile id to be used with this template
  117.      * @param object $parent     next higher level of Smarty variables
  118.      *
  119.      * @throws \Exception
  120.      * @throws \SmartyException
  121.      */
  122.     public function display($template null$cache_id null$compile_id null$parent null)
  123.     {
  124.         // display template
  125.         $this->_execute($template$cache_id$compile_id$parent1);
  126.     }
  127.     /**
  128.      * test if cache is valid
  129.      *
  130.      * @api  Smarty::isCached()
  131.      * @link https://www.smarty.net/docs/en/api.is.cached.tpl
  132.      *
  133.      * @param null|string|\Smarty_Internal_Template $template   the resource handle of the template file or template
  134.      *                                                          object
  135.      * @param mixed                                 $cache_id   cache id to be used with this template
  136.      * @param mixed                                 $compile_id compile id to be used with this template
  137.      * @param object                                $parent     next higher level of Smarty variables
  138.      *
  139.      * @return bool cache status
  140.      * @throws \Exception
  141.      * @throws \SmartyException
  142.      */
  143.     public function isCached($template null$cache_id null$compile_id null$parent null)
  144.     {
  145.         return $this->_execute($template$cache_id$compile_id$parent2);
  146.     }
  147.     /**
  148.      * fetches a rendered Smarty template
  149.      *
  150.      * @param string $template   the resource handle of the template file or template object
  151.      * @param mixed  $cache_id   cache id to be used with this template
  152.      * @param mixed  $compile_id compile id to be used with this template
  153.      * @param object $parent     next higher level of Smarty variables
  154.      * @param string $function   function type 0 = fetch,  1 = display, 2 = isCache
  155.      *
  156.      * @return mixed
  157.      * @throws \Exception
  158.      * @throws \SmartyException
  159.      */
  160.     private function _execute($template$cache_id$compile_id$parent$function)
  161.     {
  162.         $smarty $this->_getSmartyObj();
  163.         $saveVars true;
  164.         if ($template === null) {
  165.             if (!$this->_isTplObj()) {
  166.                 throw new SmartyException($function '():Missing \'$template\' parameter');
  167.             } else {
  168.                 $template $this;
  169.             }
  170.         } elseif (is_object($template)) {
  171.             /* @var Smarty_Internal_Template $template */
  172.             if (!isset($template->_objType) || !$template->_isTplObj()) {
  173.                 throw new SmartyException($function '():Template object expected');
  174.             }
  175.         } else {
  176.             // get template object
  177.             $saveVars false;
  178.             $template $smarty->createTemplate($template$cache_id$compile_id$parent $parent $thisfalse);
  179.             if ($this->_objType === 1) {
  180.                 // set caching in template object
  181.                 $template->caching $this->caching;
  182.             }
  183.         }
  184.         // make sure we have integer values
  185.         $template->caching = (int)$template->caching;
  186.         // fetch template content
  187.         $level ob_get_level();
  188.         try {
  189.             $_smarty_old_error_level =
  190.                 isset($smarty->error_reporting) ? error_reporting($smarty->error_reporting) : null;
  191.             if ($smarty->isMutingUndefinedOrNullWarnings()) {
  192.                 $errorHandler = new Smarty_Internal_ErrorHandler();
  193.                 $errorHandler->activate();
  194.             }
  195.             if ($this->_objType === 2) {
  196.                 /* @var Smarty_Internal_Template $this */
  197.                 $template->tplFunctions $this->tplFunctions;
  198.                 $template->inheritance $this->inheritance;
  199.             }
  200.             /* @var Smarty_Internal_Template $parent */
  201.             if (isset($parent->_objType) && ($parent->_objType === 2) && !empty($parent->tplFunctions)) {
  202.                 $template->tplFunctions array_merge($parent->tplFunctions$template->tplFunctions);
  203.             }
  204.             if ($function === 2) {
  205.                 if ($template->caching) {
  206.                     // return cache status of template
  207.                     if (!isset($template->cached)) {
  208.                         $template->loadCached();
  209.                     }
  210.                     $result $template->cached->isCached($template);
  211.                     Smarty_Internal_Template::$isCacheTplObj$template->_getTemplateId() ] = $template;
  212.                 } else {
  213.                     return false;
  214.                 }
  215.             } else {
  216.                 if ($saveVars) {
  217.                     $savedTplVars $template->tpl_vars;
  218.                     $savedConfigVars $template->config_vars;
  219.                 }
  220.                 ob_start();
  221.                 $template->_mergeVars();
  222.                 if (!empty(Smarty::$global_tpl_vars)) {
  223.                     $template->tpl_vars array_merge(Smarty::$global_tpl_vars$template->tpl_vars);
  224.                 }
  225.                 $result $template->render(false$function);
  226.                 $template->_cleanUp();
  227.                 if ($saveVars) {
  228.                     $template->tpl_vars $savedTplVars;
  229.                     $template->config_vars $savedConfigVars;
  230.                 } else {
  231.                     if (!$function && !isset(Smarty_Internal_Template::$tplObjCache$template->templateId ])) {
  232.                         $template->parent null;
  233.                         $template->tpl_vars $template->config_vars = array();
  234.                         Smarty_Internal_Template::$tplObjCache$template->templateId ] = $template;
  235.                     }
  236.                 }
  237.             }
  238.             if (isset($errorHandler)) {
  239.                 $errorHandler->deactivate();
  240.             }
  241.             if (isset($_smarty_old_error_level)) {
  242.                 error_reporting($_smarty_old_error_level);
  243.             }
  244.             return $result;
  245.         } catch (Exception $e) {
  246.             while (ob_get_level() > $level) {
  247.                 ob_end_clean();
  248.             }
  249.             if (isset($errorHandler)) {
  250.                 $errorHandler->deactivate();
  251.             }
  252.             if (isset($_smarty_old_error_level)) {
  253.                 error_reporting($_smarty_old_error_level);
  254.             }
  255.             throw $e;
  256.         }
  257.     }
  258.     /**
  259.      * Registers plugin to be used in templates
  260.      *
  261.      * @api  Smarty::registerPlugin()
  262.      * @link https://www.smarty.net/docs/en/api.register.plugin.tpl
  263.      *
  264.      * @param string   $type       plugin type
  265.      * @param string   $name       name of template tag
  266.      * @param callable $callback   PHP callback to register
  267.      * @param bool     $cacheable  if true (default) this function is cache able
  268.      * @param mixed    $cache_attr caching attributes if any
  269.      *
  270.      * @return \Smarty|\Smarty_Internal_Template
  271.      * @throws \SmartyException
  272.      */
  273.     public function registerPlugin($type$name$callback$cacheable true$cache_attr null)
  274.     {
  275.         return $this->ext->registerPlugin->registerPlugin($this$type$name$callback$cacheable$cache_attr);
  276.     }
  277.     /**
  278.      * load a filter of specified type and name
  279.      *
  280.      * @api  Smarty::loadFilter()
  281.      * @link https://www.smarty.net/docs/en/api.load.filter.tpl
  282.      *
  283.      * @param string $type filter type
  284.      * @param string $name filter name
  285.      *
  286.      * @return bool
  287.      * @throws \SmartyException
  288.      */
  289.     public function loadFilter($type$name)
  290.     {
  291.         return $this->ext->loadFilter->loadFilter($this$type$name);
  292.     }
  293.     /**
  294.      * Registers a filter function
  295.      *
  296.      * @api  Smarty::registerFilter()
  297.      * @link https://www.smarty.net/docs/en/api.register.filter.tpl
  298.      *
  299.      * @param string      $type filter type
  300.      * @param callable    $callback
  301.      * @param string|null $name optional filter name
  302.      *
  303.      * @return \Smarty|\Smarty_Internal_Template
  304.      * @throws \SmartyException
  305.      */
  306.     public function registerFilter($type$callback$name null)
  307.     {
  308.         return $this->ext->registerFilter->registerFilter($this$type$callback$name);
  309.     }
  310.     /**
  311.      * Registers object to be used in templates
  312.      *
  313.      * @api  Smarty::registerObject()
  314.      * @link https://www.smarty.net/docs/en/api.register.object.tpl
  315.      *
  316.      * @param string $object_name
  317.      * @param object $object                     the referenced PHP object to register
  318.      * @param array  $allowed_methods_properties list of allowed methods (empty = all)
  319.      * @param bool   $format                     smarty argument format, else traditional
  320.      * @param array  $block_methods              list of block-methods
  321.      *
  322.      * @return \Smarty|\Smarty_Internal_Template
  323.      * @throws \SmartyException
  324.      */
  325.     public function registerObject(
  326.         $object_name,
  327.         $object,
  328.         $allowed_methods_properties = array(),
  329.         $format true,
  330.         $block_methods = array()
  331.     ) {
  332.         return $this->ext->registerObject->registerObject(
  333.             $this,
  334.             $object_name,
  335.             $object,
  336.             $allowed_methods_properties,
  337.             $format,
  338.             $block_methods
  339.         );
  340.     }
  341.     /**
  342.      * @param int $compile_check
  343.      */
  344.     public function setCompileCheck($compile_check)
  345.     {
  346.         $this->compile_check = (int)$compile_check;
  347.     }
  348.     /**
  349.      * @param int $caching
  350.      */
  351.     public function setCaching($caching)
  352.     {
  353.         $this->caching = (int)$caching;
  354.     }
  355.     /**
  356.      * @param int $cache_lifetime
  357.      */
  358.     public function setCacheLifetime($cache_lifetime)
  359.     {
  360.         $this->cache_lifetime $cache_lifetime;
  361.     }
  362.     /**
  363.      * @param string $compile_id
  364.      */
  365.     public function setCompileId($compile_id)
  366.     {
  367.         $this->compile_id $compile_id;
  368.     }
  369.     /**
  370.      * @param string $cache_id
  371.      */
  372.     public function setCacheId($cache_id)
  373.     {
  374.         $this->cache_id $cache_id;
  375.     }
  376. }