smarty_internal_resource_file.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Resource File
  4. *
  5. * @package Smarty
  6. * @subpackage TemplateResources
  7. * @author Uwe Tews
  8. * @author Rodney Rehm
  9. */
  10. /**
  11. * Smarty Internal Plugin Resource File
  12. * Implements the file system as resource for Smarty templates
  13. *
  14. * @package Smarty
  15. * @subpackage TemplateResources
  16. */
  17. class Smarty_Internal_Resource_File extends Smarty_Resource
  18. {
  19. /**
  20. * populate Source Object with meta data from Resource
  21. *
  22. * @param Smarty_Template_Source $source source object
  23. * @param Smarty_Internal_Template $_template template object
  24. */
  25. public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
  26. {
  27. $source->filepath = $this->buildFilepath($source, $_template);
  28. if ($source->filepath !== false) {
  29. if (is_object($source->smarty->security_policy)) {
  30. $source->smarty->security_policy->isTrustedResourceDir($source->filepath);
  31. }
  32. $source->uid = sha1(realpath($source->filepath));
  33. if ($source->smarty->compile_check && !isset($source->timestamp)) {
  34. $source->timestamp = @filemtime($source->filepath);
  35. $source->exists = !!$source->timestamp;
  36. }
  37. }
  38. }
  39. /**
  40. * populate Source Object with timestamp and exists from Resource
  41. *
  42. * @param Smarty_Template_Source $source source object
  43. */
  44. public function populateTimestamp(Smarty_Template_Source $source)
  45. {
  46. $source->timestamp = @filemtime($source->filepath);
  47. $source->exists = !!$source->timestamp;
  48. }
  49. /**
  50. * Load template's source from file into current template object
  51. *
  52. * @param Smarty_Template_Source $source source object
  53. *
  54. * @return string template source
  55. * @throws SmartyException if source cannot be loaded
  56. */
  57. public function getContent(Smarty_Template_Source $source)
  58. {
  59. if ($source->timestamp) {
  60. return file_get_contents($source->filepath);
  61. }
  62. if ($source instanceof Smarty_Config_Source) {
  63. throw new SmartyException("Unable to read config {$source->type} '{$source->name}'");
  64. }
  65. throw new SmartyException("Unable to read template {$source->type} '{$source->name}'");
  66. }
  67. /**
  68. * Determine basename for compiled filename
  69. *
  70. * @param Smarty_Template_Source $source source object
  71. *
  72. * @return string resource's basename
  73. */
  74. public function getBasename(Smarty_Template_Source $source)
  75. {
  76. $_file = $source->name;
  77. if (($_pos = strpos($_file, ']')) !== false) {
  78. $_file = substr($_file, $_pos + 1);
  79. }
  80. return basename($_file);
  81. }
  82. }