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