PageRenderTime 23ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/smarty/sysplugins/smarty_internal_resource_php.php

https://gitlab.com/staging06/myproject
PHP | 119 lines | 54 code | 10 blank | 55 comment | 8 complexity | 1d96d6ac63ec32fb476638d6f30d3510 MD5 | raw file
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Resource PHP
  4. * Implements the file system as resource for PHP templates
  5. *
  6. * @package Smarty
  7. * @subpackage TemplateResources
  8. * @author Uwe Tews
  9. * @author Rodney Rehm
  10. */
  11. class Smarty_Internal_Resource_PHP extends Smarty_Resource_Uncompiled
  12. {
  13. /**
  14. * container for short_open_tag directive's value before executing PHP templates
  15. *
  16. * @var string
  17. */
  18. protected $short_open_tag;
  19. /**
  20. * Create a new PHP Resource
  21. */
  22. public function __construct()
  23. {
  24. $this->short_open_tag = ini_get('short_open_tag');
  25. }
  26. /**
  27. * populate Source Object with meta data from Resource
  28. *
  29. * @param Smarty_Template_Source $source source object
  30. * @param Smarty_Internal_Template $_template template object
  31. *
  32. * @return void
  33. */
  34. public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
  35. {
  36. $source->filepath = $this->buildFilepath($source, $_template);
  37. if ($source->filepath !== false) {
  38. if (is_object($source->smarty->security_policy)) {
  39. $source->smarty->security_policy->isTrustedResourceDir($source->filepath);
  40. }
  41. $source->uid = sha1($source->filepath);
  42. if ($source->smarty->compile_check) {
  43. $source->timestamp = @filemtime($source->filepath);
  44. $source->exists = !!$source->timestamp;
  45. }
  46. }
  47. }
  48. /**
  49. * populate Source Object with timestamp and exists from Resource
  50. *
  51. * @param Smarty_Template_Source $source source object
  52. *
  53. * @return void
  54. */
  55. public function populateTimestamp(Smarty_Template_Source $source)
  56. {
  57. $source->timestamp = @filemtime($source->filepath);
  58. $source->exists = !!$source->timestamp;
  59. }
  60. /**
  61. * Load template's source from file into current template object
  62. *
  63. * @param Smarty_Template_Source $source source object
  64. *
  65. * @return string template source
  66. * @throws SmartyException if source cannot be loaded
  67. */
  68. public function getContent(Smarty_Template_Source $source)
  69. {
  70. if ($source->timestamp) {
  71. return '';
  72. }
  73. throw new SmartyException("Unable to read template {$source->type} '{$source->name}'");
  74. }
  75. /**
  76. * Render and output the template (without using the compiler)
  77. *
  78. * @param Smarty_Template_Source $source source object
  79. * @param Smarty_Internal_Template $_template template object
  80. *
  81. * @return void
  82. * @throws SmartyException if template cannot be loaded or allow_php_templates is disabled
  83. */
  84. public function renderUncompiled(Smarty_Template_Source $source, Smarty_Internal_Template $_template)
  85. {
  86. if (!$source->smarty->allow_php_templates) {
  87. throw new SmartyException("PHP templates are disabled");
  88. }
  89. if (!$source->exists) {
  90. if ($_template->parent instanceof Smarty_Internal_Template) {
  91. $parent_resource = " in '{$_template->parent->template_resource}'";
  92. } else {
  93. $parent_resource = '';
  94. }
  95. throw new SmartyException("Unable to load template {$source->type} '{$source->name}'{$parent_resource}");
  96. }
  97. // prepare variables
  98. extract($_template->getTemplateVars());
  99. // include PHP template with short open tags enabled
  100. ini_set('short_open_tag', '1');
  101. /** @var Smarty_Internal_Template $_smarty_template
  102. * used in included file
  103. */
  104. $_smarty_template = $_template;
  105. include($source->filepath);
  106. ini_set('short_open_tag', $this->short_open_tag);
  107. }
  108. }