PEEL Shopping
Open source ecommerce : PEEL Shopping
smarty_internal_template.php
Go to the documentation of this file.
1 <?php
23 
28  public $cache_id = null;
33  public $compile_id = null;
38  public $caching = null;
43  public $cache_lifetime = null;
48  public $template_resource = null;
53  public $mustCompile = null;
58  public $has_nocache_code = false;
63  public $properties = array('file_dependency' => array(),
64  'nocache_hash' => '',
65  'function' => array());
70  public $required_plugins = array('compiled' => array(), 'nocache' => array());
75  public $smarty = null;
80  public $block_data = array();
85  public $variable_filters = array();
90  public $used_tags = array();
95  public $allow_relative_path = false;
100  public $_capture_stack = array(0 => array());
101 
116  public function __construct($template_resource, $smarty, $_parent = null, $_cache_id = null, $_compile_id = null, $_caching = null, $_cache_lifetime = null)
117  {
118  $this->smarty = &$smarty;
119  // Smarty parameter
120  $this->cache_id = $_cache_id === null ? $this->smarty->cache_id : $_cache_id;
121  $this->compile_id = $_compile_id === null ? $this->smarty->compile_id : $_compile_id;
122  $this->caching = $_caching === null ? $this->smarty->caching : $_caching;
123  if ($this->caching === true)
124  $this->caching = Smarty::CACHING_LIFETIME_CURRENT;
125  $this->cache_lifetime = $_cache_lifetime === null ? $this->smarty->cache_lifetime : $_cache_lifetime;
126  $this->parent = $_parent;
127  // Template resource
128  $this->template_resource = $template_resource;
129  // copy block data of template inheritance
130  if ($this->parent instanceof Smarty_Internal_Template) {
131  $this->block_data = $this->parent->block_data;
132  }
133  }
134 
142  public function mustCompile()
143  {
144  if (!$this->source->exists) {
145  if ($this->parent instanceof Smarty_Internal_Template) {
146  $parent_resource = " in '$this->parent->template_resource}'";
147  } else {
148  $parent_resource = '';
149  }
150  throw new SmartyException("Unable to load template {$this->source->type} '{$this->source->name}'{$parent_resource}");
151  }
152  if ($this->mustCompile === null) {
153  $this->mustCompile = (!$this->source->uncompiled && ($this->smarty->force_compile || $this->source->recompiled || $this->compiled->timestamp === false ||
154  ($this->smarty->compile_check && $this->compiled->timestamp < $this->source->timestamp)));
155  }
156  return $this->mustCompile;
157  }
158 
164  public function compileTemplateSource()
165  {
166  if (!$this->source->recompiled) {
167  $this->properties['file_dependency'] = array();
168  if ($this->source->components) {
169  // uses real resource for file dependency
170  $source = end($this->source->components);
171  $this->properties['file_dependency'][$this->source->uid] = array($this->source->filepath, $this->source->timestamp, $source->type);
172  } else {
173  $this->properties['file_dependency'][$this->source->uid] = array($this->source->filepath, $this->source->timestamp, $this->source->type);
174  }
175  }
176  if ($this->smarty->debugging) {
178  }
179  // compile locking
180  if ($this->smarty->compile_locking && !$this->source->recompiled) {
181  if ($saved_timestamp = $this->compiled->timestamp) {
182  touch($this->compiled->filepath);
183  }
184  }
185  // call compiler
186  try {
187  $code = $this->compiler->compileTemplate($this);
188  } catch (Exception $e) {
189  // restore old timestamp in case of error
190  if ($this->smarty->compile_locking && !$this->source->recompiled && $saved_timestamp) {
191  touch($this->compiled->filepath, $saved_timestamp);
192  }
193  throw $e;
194  }
195  // compiling succeded
196  if (!$this->source->recompiled && $this->compiler->write_compiled_code) {
197  // write compiled template
198  $_filepath = $this->compiled->filepath;
199  if ($_filepath === false)
200  throw new SmartyException('getCompiledFilepath() did not return a destination to save the compiled template to');
201  Smarty_Internal_Write_File::writeFile($_filepath, $code, $this->smarty);
202  $this->compiled->exists = true;
203  $this->compiled->isCompiled = true;
204  }
205  if ($this->smarty->debugging) {
207  }
208  // release compiler object to free memory
209  unset($this->compiler);
210  }
211 
217  public function writeCachedContent($content)
218  {
219  if ($this->source->recompiled || !($this->caching == Smarty::CACHING_LIFETIME_CURRENT || $this->caching == Smarty::CACHING_LIFETIME_SAVED)) {
220  // don't write cache file
221  return false;
222  }
223  $this->properties['cache_lifetime'] = $this->cache_lifetime;
224  $this->properties['unifunc'] = 'content_' . str_replace('.', '_', uniqid('', true));
225  $content = $this->createTemplateCodeFrame($content, true);
226  $_smarty_tpl = $this;
227  eval("?>" . $content);
228  $this->cached->valid = true;
229  $this->cached->processed = true;
230  return $this->cached->write($this, $content);
231  }
232 
245  public function getSubTemplate($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $parent_scope)
246  {
247  // already in template cache?
248  if ($this->smarty->allow_ambiguous_resources) {
249  $_templateId = Smarty_Resource::getUniqueTemplateName($this->smarty, $template) . $cache_id . $compile_id;
250  } else {
251  $_templateId = $this->smarty->joined_template_dir . '#' . $template . $cache_id . $compile_id;
252  }
253 
254  if (isset($_templateId[150])) {
255  $_templateId = sha1($_templateId);
256  }
257  if (isset($this->smarty->template_objects[$_templateId])) {
258  // clone cached template object because of possible recursive call
259  $tpl = clone $this->smarty->template_objects[$_templateId];
260  $tpl->parent = $this;
261  $tpl->caching = $caching;
262  $tpl->cache_lifetime = $cache_lifetime;
263  } else {
264  $tpl = new $this->smarty->template_class($template, $this->smarty, $this, $cache_id, $compile_id, $caching, $cache_lifetime);
265  }
266  // get variables from calling scope
267  if ($parent_scope == Smarty::SCOPE_LOCAL) {
268  $tpl->tpl_vars = $this->tpl_vars;
269  $tpl->tpl_vars['smarty'] = clone $this->tpl_vars['smarty'];
270  } elseif ($parent_scope == Smarty::SCOPE_PARENT) {
271  $tpl->tpl_vars = &$this->tpl_vars;
272  } elseif ($parent_scope == Smarty::SCOPE_GLOBAL) {
273  $tpl->tpl_vars = &Smarty::$global_tpl_vars;
274  } elseif (($scope_ptr = $this->getScopePointer($parent_scope)) == null) {
275  $tpl->tpl_vars = &$this->tpl_vars;
276  } else {
277  $tpl->tpl_vars = &$scope_ptr->tpl_vars;
278  }
279  $tpl->config_vars = $this->config_vars;
280  if (!empty($data)) {
281  // set up variable values
282  foreach ($data as $_key => $_val) {
283  $tpl->tpl_vars[$_key] = new Smarty_variable($_val);
284  }
285  }
286  return $tpl->fetch(null, null, null, null, false, false, true);
287  }
288 
302  public function setupInlineSubTemplate($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $parent_scope, $hash)
303  {
304  $tpl = new $this->smarty->template_class($template, $this->smarty, $this, $cache_id, $compile_id, $caching, $cache_lifetime);
305  $tpl->properties['nocache_hash'] = $hash;
306  // get variables from calling scope
307  if ($parent_scope == Smarty::SCOPE_LOCAL ) {
308  $tpl->tpl_vars = $this->tpl_vars;
309  $tpl->tpl_vars['smarty'] = clone $this->tpl_vars['smarty'];
310  } elseif ($parent_scope == Smarty::SCOPE_PARENT) {
311  $tpl->tpl_vars = &$this->tpl_vars;
312  } elseif ($parent_scope == Smarty::SCOPE_GLOBAL) {
313  $tpl->tpl_vars = &Smarty::$global_tpl_vars;
314  } elseif (($scope_ptr = $this->getScopePointer($parent_scope)) == null) {
315  $tpl->tpl_vars = &$this->tpl_vars;
316  } else {
317  $tpl->tpl_vars = &$scope_ptr->tpl_vars;
318  }
319  $tpl->config_vars = $this->config_vars;
320  if (!empty($data)) {
321  // set up variable values
322  foreach ($data as $_key => $_val) {
323  $tpl->tpl_vars[$_key] = new Smarty_variable($_val);
324  }
325  }
326  return $tpl;
327  }
328 
329 
337  public function createTemplateCodeFrame($content = '', $cache = false)
338  {
339  $plugins_string = '';
340  // include code for plugins
341  if (!$cache) {
342  if (!empty($this->required_plugins['compiled'])) {
343  $plugins_string = '<?php ';
344  foreach ($this->required_plugins['compiled'] as $tmp) {
345  foreach ($tmp as $data) {
346  $file = addslashes($data['file']);
347  if (is_Array($data['function'])){
348  $plugins_string .= "if (!is_callable(array('{$data['function'][0]}','{$data['function'][1]}'))) include '{$file}';\n";
349  } else {
350  $plugins_string .= "if (!is_callable('{$data['function']}')) include '{$file}';\n";
351  }
352  }
353  }
354  $plugins_string .= '?>';
355  }
356  if (!empty($this->required_plugins['nocache'])) {
357  $this->has_nocache_code = true;
358  $plugins_string .= "<?php echo '/*%%SmartyNocache:{$this->properties['nocache_hash']}%%*/<?php \$_smarty = \$_smarty_tpl->smarty; ";
359  foreach ($this->required_plugins['nocache'] as $tmp) {
360  foreach ($tmp as $data) {
361  $file = addslashes($data['file']);
362  if (is_Array($data['function'])){
363  $plugins_string .= addslashes("if (!is_callable(array('{$data['function'][0]}','{$data['function'][1]}'))) include '{$file}';\n");
364  } else {
365  $plugins_string .= addslashes("if (!is_callable('{$data['function']}')) include '{$file}';\n");
366  }
367  }
368  }
369  $plugins_string .= "?>/*/%%SmartyNocache:{$this->properties['nocache_hash']}%%*/';?>\n";
370  }
371  }
372  // build property code
373  $this->properties['has_nocache_code'] = $this->has_nocache_code;
374  $output = '';
375  if (!$this->source->recompiled) {
376  $output = "<?php /*%%SmartyHeaderCode:{$this->properties['nocache_hash']}%%*/";
377  if ($this->smarty->direct_access_security) {
378  $output .= "if(!defined('SMARTY_DIR')) exit('no direct access allowed');\n";
379  }
380  }
381  if ($cache) {
382  // remove compiled code of{function} definition
383  unset($this->properties['function']);
384  if (!empty($this->smarty->template_functions)) {
385  // copy code of {function} tags called in nocache mode
386  foreach ($this->smarty->template_functions as $name => $function_data) {
387  if (isset($function_data['called_nocache'])) {
388  foreach ($function_data['called_functions'] as $func_name) {
389  $this->smarty->template_functions[$func_name]['called_nocache'] = true;
390  }
391  }
392  }
393  foreach ($this->smarty->template_functions as $name => $function_data) {
394  if (isset($function_data['called_nocache'])) {
395  unset($function_data['called_nocache'], $function_data['called_functions'], $this->smarty->template_functions[$name]['called_nocache']);
396  $this->properties['function'][$name] = $function_data;
397  }
398  }
399  }
400  }
401  $this->properties['version'] = Smarty::SMARTY_VERSION;
402  if (!isset($this->properties['unifunc'])) {
403  $this->properties['unifunc'] = 'content_' . str_replace('.', '_', uniqid('', true));
404  }
405  if (!$this->source->recompiled) {
406  $output .= "\$_valid = \$_smarty_tpl->decodeProperties(" . var_export($this->properties, true) . ',' . ($cache ? 'true' : 'false') . "); /*/%%SmartyHeaderCode%%*/?>\n";
407  $output .= '<?php if ($_valid && !is_callable(\'' . $this->properties['unifunc'] . '\')) {function ' . $this->properties['unifunc'] . '($_smarty_tpl) {?>';
408  }
409  $output .= $plugins_string;
410  $output .= $content;
411  if (!$this->source->recompiled) {
412  $output .= '<?php }} ?>';
413  }
414  return $output;
415  }
416 
427  public function decodeProperties($properties, $cache = false)
428  {
429  $this->has_nocache_code = $properties['has_nocache_code'];
430  $this->properties['nocache_hash'] = $properties['nocache_hash'];
431  if (isset($properties['cache_lifetime'])) {
432  $this->properties['cache_lifetime'] = $properties['cache_lifetime'];
433  }
434  if (isset($properties['file_dependency'])) {
435  $this->properties['file_dependency'] = array_merge($this->properties['file_dependency'], $properties['file_dependency']);
436  }
437  if (!empty($properties['function'])) {
438  $this->properties['function'] = array_merge($this->properties['function'], $properties['function']);
439  $this->smarty->template_functions = array_merge($this->smarty->template_functions, $properties['function']);
440  }
441  $this->properties['version'] = (isset($properties['version'])) ? $properties['version'] : '';
442  $this->properties['unifunc'] = $properties['unifunc'];
443  // check file dependencies at compiled code
444  $is_valid = true;
445  if ($this->properties['version'] != Smarty::SMARTY_VERSION) {
446  $is_valid = false;
447  } else if (((!$cache && $this->smarty->compile_check && empty($this->compiled->_properties) && !$this->compiled->isCompiled) || $cache && ($this->smarty->compile_check === true || $this->smarty->compile_check === Smarty::COMPILECHECK_ON)) && !empty($this->properties['file_dependency'])) {
448  foreach ($this->properties['file_dependency'] as $_file_to_check) {
449  if ($_file_to_check[2] == 'file' || $_file_to_check[2] == 'php') {
450  if ($this->source->filepath == $_file_to_check[0] && isset($this->source->timestamp)) {
451  // do not recheck current template
452  $mtime = $this->source->timestamp;
453  } else {
454  // file and php types can be checked without loading the respective resource handlers
455  $mtime = @filemtime($_file_to_check[0]);
456  }
457  } elseif ($_file_to_check[2] == 'string') {
458  continue;
459  } else {
460  $source = Smarty_Resource::source(null, $this->smarty, $_file_to_check[0]);
461  $mtime = $source->timestamp;
462  }
463  if (!$mtime || $mtime > $_file_to_check[1]) {
464  $is_valid = false;
465  break;
466  }
467  }
468  }
469  if ($cache) {
470  // CACHING_LIFETIME_SAVED cache expiry has to be validated here since otherwise we'd define the unifunc
471  if ($this->caching === Smarty::CACHING_LIFETIME_SAVED &&
472  $this->properties['cache_lifetime'] >= 0 &&
473  (time() > ($this->cached->timestamp + $this->properties['cache_lifetime']))) {
474  $is_valid = false;
475  }
476  $this->cached->valid = $is_valid;
477  } else {
478  $this->mustCompile = !$is_valid; }
479  // store data in reusable Smarty_Template_Compiled
480  if (!$cache) {
481  $this->compiled->_properties = $properties;
482  }
483  return $is_valid;
484  }
485 
493  public function createLocalArrayVariable($tpl_var, $nocache = false, $scope = Smarty::SCOPE_LOCAL)
494  {
495  if (!isset($this->tpl_vars[$tpl_var])) {
496  $this->tpl_vars[$tpl_var] = new Smarty_variable(array(), $nocache, $scope);
497  } else {
498  $this->tpl_vars[$tpl_var] = clone $this->tpl_vars[$tpl_var];
499  if ($scope != Smarty::SCOPE_LOCAL) {
500  $this->tpl_vars[$tpl_var]->scope = $scope;
501  }
502  if (!(is_array($this->tpl_vars[$tpl_var]->value) || $this->tpl_vars[$tpl_var]->value instanceof ArrayAccess)) {
503  settype($this->tpl_vars[$tpl_var]->value, 'array');
504  }
505  }
506  }
507 
514  public function &getScope($scope)
515  {
516  if ($scope == Smarty::SCOPE_PARENT && !empty($this->parent)) {
517  return $this->parent->tpl_vars;
518  } elseif ($scope == Smarty::SCOPE_ROOT && !empty($this->parent)) {
519  $ptr = $this->parent;
520  while (!empty($ptr->parent)) {
521  $ptr = $ptr->parent;
522  }
523  return $ptr->tpl_vars;
524  } elseif ($scope == Smarty::SCOPE_GLOBAL) {
526  }
527  $null = null;
528  return $null;
529  }
530 
537  public function getScopePointer($scope)
538  {
539  if ($scope == Smarty::SCOPE_PARENT && !empty($this->parent)) {
540  return $this->parent;
541  } elseif ($scope == Smarty::SCOPE_ROOT && !empty($this->parent)) {
542  $ptr = $this->parent;
543  while (!empty($ptr->parent)) {
544  $ptr = $ptr->parent;
545  }
546  return $ptr;
547  }
548  return null;
549  }
550 
557  public function _count($value)
558  {
559  if (is_array($value) === true || $value instanceof Countable) {
560  return count($value);
561  } elseif ($value instanceof IteratorAggregate) {
562  // Note: getIterator() returns a Traversable, not an Iterator
563  // thus rewind() and valid() methods may not be present
564  return iterator_count($value->getIterator());
565  } elseif ($value instanceof Iterator) {
566  return iterator_count($value);
567  } elseif ($value instanceof PDOStatement) {
568  return $value->rowCount();
569  } elseif ($value instanceof Traversable) {
570  return iterator_count($value);
571  } elseif ($value instanceof ArrayAccess) {
572  if ($value->offsetExists(0)) {
573  return 1;
574  }
575  } elseif (is_object($value)) {
576  return count($value);
577  }
578  return 0;
579  }
580 
585  public function capture_error()
586  {
587  throw new SmartyException("Not matching {capture} open/close in \"{$this->template_resource}\"");
588  }
589 
596  public function clearCache($exp_time=null)
597  {
599  return $this->cached->handler->clear($this->smarty, $this->template_name, $this->cache_id, $this->compile_id, $exp_time);
600  }
601 
608  public function __set($property_name, $value)
609  {
610  switch ($property_name) {
611  case 'source':
612  case 'compiled':
613  case 'cached':
614  case 'compiler':
615  $this->$property_name = $value;
616  return;
617 
618  // FIXME: routing of template -> smarty attributes
619  default:
620  if (property_exists($this->smarty, $property_name)) {
621  $this->smarty->$property_name = $value;
622  return;
623  }
624  }
625 
626  throw new SmartyException("invalid template property '$property_name'.");
627  }
628 
634  public function __get($property_name)
635  {
636  switch ($property_name) {
637  case 'source':
638  if (strlen($this->template_resource) == 0) {
639  throw new SmartyException('Missing template name');
640  }
641  $this->source = Smarty_Resource::source($this);
642  // cache template object under a unique ID
643  // do not cache eval resources
644  if ($this->source->type != 'eval') {
645  if ($this->smarty->allow_ambiguous_resources) {
646  $_templateId = $this->source->unique_resource . $this->cache_id . $this->compile_id;
647  } else {
648  $_templateId = $this->smarty->joined_template_dir . '#' . $this->template_resource . $this->cache_id . $this->compile_id;
649  }
650 
651  if (isset($_templateId[150])) {
652  $_templateId = sha1($_templateId);
653  }
654  $this->smarty->template_objects[$_templateId] = $this;
655  }
656  return $this->source;
657 
658  case 'compiled':
659  $this->compiled = $this->source->getCompiled($this);
660  return $this->compiled;
661 
662  case 'cached':
663  if (!class_exists('Smarty_Template_Cached')) {
664  include SMARTY_SYSPLUGINS_DIR . 'smarty_cacheresource.php';
665  }
666  $this->cached = new Smarty_Template_Cached($this);
667  return $this->cached;
668 
669  case 'compiler':
670  $this->smarty->loadPlugin($this->source->compiler_class);
671  $this->compiler = new $this->source->compiler_class($this->source->template_lexer_class, $this->source->template_parser_class, $this->smarty);
672  return $this->compiler;
673 
674  // FIXME: routing of template -> smarty attributes
675  default:
676  if (property_exists($this->smarty, $property_name)) {
677  return $this->smarty->$property_name;
678  }
679  }
680 
681  throw new SmartyException("template property '$property_name' does not exist.");
682  }
683 
688  public function __destruct()
689  {
690  if ($this->smarty->cache_locking && isset($this->cached) && $this->cached->is_locked) {
691  $this->cached->handler->releaseLock($this->smarty, $this->cached);
692  }
693  }
694 
695 }
696 
697 ?>
__set($property_name, $value)
set Smarty property in template context
foreach(array('date1', 'date2', 'type', 'renewals', 'width') as $item) $data
Definition: chart-data.php:29
const SCOPE_ROOT
getSubTemplate($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $parent_scope)
Template code runtime function to get subtemplate content.
__construct($template_resource, $smarty, $_parent=null, $_cache_id=null, $_compile_id=null, $_caching=null, $_cache_lifetime=null)
Create template data object.
getScopePointer($scope)
Get parent or root of template parent chain.
setupInlineSubTemplate($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $parent_scope, $hash)
Template code runtime function to set up an inline subtemplate.
static end_compile($template)
End logging of compile time.
static invalidLoadedCache(Smarty $smarty)
Invalid Loaded Cache Files.
const SCOPE_GLOBAL
static $global_tpl_vars
#@-
const SCOPE_PARENT
__get($property_name)
get Smarty property in template context
if(!empty($_GET['id'])) if(isset($_POST['form_name'], $_POST['form_subject'], $_POST['form_text'], $_POST['form_lang'])&&empty($_GET['id'])) if(empty($_GET['id'])) $tpl
const CACHING_LIFETIME_CURRENT
static start_compile($template)
Start logging of compile time.
_count($value)
[util function] counts an array, arrayaccess/traversable or PDOStatement object
static source(Smarty_Internal_Template $_template=null, Smarty $smarty=null, $template_resource=null)
initialize Source Object for given resource
& getScope($scope)
Template code runtime function to get pointer to template variable array of requested scope...
createLocalArrayVariable($tpl_var, $nocache=false, $scope=Smarty::SCOPE_LOCAL)
Template code runtime function to create a local Smarty variable for array assignments.
createTemplateCodeFrame($content= '', $cache=false)
Create code frame for compiled and cached templates.
static getUniqueTemplateName($smarty, $template_resource)
modify resource_name according to resource handlers specifications
const CACHING_LIFETIME_SAVED
if(strlen($date2)== '10') if($type== 'users-by-age'&&a_priv('admin_users', true)) elseif($type== 'forums-count'&&a_priv('admin_content', true)) elseif($type== 'forums-categories'&&a_priv('admin_content', true)) elseif($type== 'users-count'&&a_priv('admin_users', true)) elseif($type== 'product-categories'&&a_priv('admin_products', true)) elseif($type== 'users-by-sex'&&a_priv('admin_users', true)) elseif($type== 'users-by-country'&&a_priv('admin_users', true)) elseif($type== 'sales'&&a_priv('admin_sales', true))
Definition: chart-data.php:160
__destruct()
Template data object destrutor.
capture_error()
runtime error not matching capture tags
compileTemplateSource()
Compiles the template.
const SCOPE_LOCAL
define variable scopes
writeCachedContent($content)
Writes the cached template output.
mustCompile()
Returns if the current template must be compiled by the Smarty compiler.
const SMARTY_VERSION
#@+ constant definitions
static writeFile($_filepath, $_contents, Smarty $smarty)
Writes file in a safe way to disk.
clearCache($exp_time=null)
Empty cache for this template.

This documentation for Open ecommerce PEEL Shopping and PEEL.fr has been generated by Doxygen on Thu Oct 15 2015 14:41:17 - Peel ecommerce is a product of Agence web Advisto SAS. All rights reserved.