smarty_internal_resource_file.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Resource File
  4. *
  5. * Implements the file system as resource for Smarty templates
  6. *
  7. * @package Smarty
  8. * @subpackage TemplateResources
  9. * @author Uwe Tews
  10. */
  11. /**
  12. * Smarty Internal Plugin Resource File
  13. */
  14. class Smarty_Internal_Resource_File {
  15. public function __construct($smarty)
  16. {
  17. $this->smarty = $smarty;
  18. }
  19. // classes used for compiling Smarty templates from file resource
  20. public $compiler_class = 'Smarty_Internal_SmartyTemplateCompiler';
  21. public $template_lexer_class = 'Smarty_Internal_Templatelexer';
  22. public $template_parser_class = 'Smarty_Internal_Templateparser';
  23. // properties
  24. public $usesCompiler = true;
  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 ($_filepath !== false) {
  49. if ($_template->security) {
  50. $_template->smarty->security_handler->isTrustedResourceDir($_filepath);
  51. }
  52. }
  53. $_template->templateUid = sha1($_filepath);
  54. return $_filepath;
  55. }
  56. /**
  57. * Get timestamp to template source
  58. *
  59. * @param object $_template template object
  60. * @return integer timestamp of template source file
  61. */
  62. public function getTemplateTimestamp($_template)
  63. {
  64. return filemtime($_template->getTemplateFilepath());
  65. }
  66. /**
  67. * Read template source from file
  68. *
  69. * @param object $_template template object
  70. * @return string content of template source file
  71. */
  72. public function getTemplateSource($_template)
  73. {
  74. // read template file
  75. if (file_exists($_template->getTemplateFilepath())) {
  76. $_template->template_source = file_get_contents($_template->getTemplateFilepath());
  77. return true;
  78. } else {
  79. return false;
  80. }
  81. }
  82. /**
  83. * Get filepath to compiled template
  84. *
  85. * @param object $_template template object
  86. * @return string return path to compiled template
  87. */
  88. public function getCompiledFilepath($_template)
  89. {
  90. $_compile_id = isset($_template->compile_id) ? preg_replace('![^\w\|]+!', '_', $_template->compile_id) : null;
  91. // calculate Uid if not already done
  92. if ($_template->templateUid == '') {
  93. $_template->getTemplateFilepath();
  94. }
  95. $_filepath = $_template->templateUid;
  96. // if use_sub_dirs, break file into directories
  97. if ($_template->smarty->use_sub_dirs) {
  98. $_filepath = substr($_filepath, 0, 2) . DS
  99. . substr($_filepath, 2, 2) . DS
  100. . substr($_filepath, 4, 2) . DS
  101. . $_filepath;
  102. }
  103. $_compile_dir_sep = $_template->smarty->use_sub_dirs ? DS : '^';
  104. if (isset($_compile_id)) {
  105. $_filepath = $_compile_id . $_compile_dir_sep . $_filepath;
  106. }
  107. if ($_template->caching) {
  108. $_cache = '.cache';
  109. } else {
  110. $_cache = '';
  111. }
  112. $_compile_dir = $_template->smarty->compile_dir;
  113. if (strpos('/\\', substr($_compile_dir, -1)) === false) {
  114. $_compile_dir .= DS;
  115. }
  116. return $_compile_dir . $_filepath . '.' . $_template->resource_type . '.' . basename($_template->resource_name) . $_cache . '.php';
  117. }
  118. }
  119. ?>