smarty_internal_resource_php.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. */
  11. /**
  12. * Smarty Internal Plugin Resource PHP
  13. */
  14. class Smarty_Internal_Resource_PHP {
  15. /**
  16. * Class constructor, enable short open tags
  17. */
  18. public function __construct($smarty)
  19. {
  20. $this->smarty = $smarty;
  21. ini_set('short_open_tag', '1');
  22. }
  23. // properties
  24. public $usesCompiler = false;
  25. public $isEvaluated = false;
  26. /**
  27. * Return flag if template source is existing
  28. *
  29. * @return boolean true
  30. */
  31. public function isExisting($template)
  32. {
  33. if ($template->getTemplateFilepath() === false) {
  34. return false;
  35. } else {
  36. return true;
  37. }
  38. }
  39. /**
  40. * Get filepath to template source
  41. *
  42. * @param object $_template template object
  43. * @return string filepath to template source file
  44. */
  45. public function getTemplateFilepath($_template)
  46. {
  47. $_filepath = $_template->buildTemplateFilepath ();
  48. if ($_template->security) {
  49. $_template->smarty->security_handler->isTrustedResourceDir($_filepath);
  50. }
  51. $_template->templateUid = sha1($_filepath);
  52. return $_filepath;
  53. }
  54. /**
  55. * Get timestamp to template source
  56. *
  57. * @param object $_template template object
  58. * @return integer timestamp of template source file
  59. */
  60. public function getTemplateTimestamp($_template)
  61. {
  62. return filemtime($_template->getTemplateFilepath());
  63. }
  64. /**
  65. * Read template source from file
  66. *
  67. * @param object $_template template object
  68. * @return string content of template source file
  69. */
  70. public function getTemplateSource($_template)
  71. {
  72. if (file_exists($_template->getTemplateFilepath())) {
  73. $_template->template_source = file_get_contents($_template->getTemplateFilepath());
  74. return true;
  75. } else {
  76. return false;
  77. }
  78. }
  79. /**
  80. * Get filepath to compiled template
  81. *
  82. * @param object $_template template object
  83. * @return boolean return false as compiled template is not stored
  84. */
  85. public function getCompiledFilepath($_template)
  86. {
  87. // no filepath for PHP templates
  88. return false;
  89. }
  90. /**
  91. * renders the PHP template
  92. */
  93. public function renderUncompiled($_smarty_template)
  94. {
  95. if (!$this->smarty->allow_php_templates) {
  96. throw new Exception("PHP templates are disabled");
  97. }
  98. if ($this->getTemplateFilepath($_smarty_template) === false) {
  99. throw new Exception("Unable to load template \"{$_smarty_template->resource_type} : {$_smarty_template->resource_name}\"");
  100. }
  101. // prepare variables
  102. $_smarty_ptr = $_smarty_template;
  103. do {
  104. foreach ($_smarty_ptr->tpl_vars as $_smarty_var => $_smarty_var_object) {
  105. if (isset($_smarty_var_object->value)) {
  106. $$_smarty_var = $_smarty_var_object->value;
  107. }
  108. }
  109. $_smarty_ptr = $_smarty_ptr->parent;
  110. } while ($_smarty_ptr != null);
  111. unset ($_smarty_var, $_smarty_var_object, $_smarty_ptr);
  112. // include PHP template
  113. include($this->getTemplateFilepath($_smarty_template));
  114. return;
  115. }
  116. }
  117. ?>