smarty_internal_resource_php.php 3.5 KB

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