smarty_internal_resource_file.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. * build template filepath by traversing the template_dir array
  21. *
  22. * @param Smarty_Template_Source $source source object
  23. * @param Smarty_Internal_Template $_template template object
  24. *
  25. * @return string fully qualified filepath
  26. * @throws SmartyException
  27. */
  28. protected function buildFilepath(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
  29. {
  30. $file = $source->name;
  31. // absolute file ?
  32. if ($file[ 0 ] == '/' || $file[ 1 ] == ':') {
  33. $file = $source->smarty->_realpath($file, true);
  34. return is_file($file) ? $file : false;
  35. }
  36. // go relative to a given template?
  37. if ($file[ 0 ] == '.' && $_template && isset($_template->parent) && $_template->parent->_objType == 2 &&
  38. preg_match('#^[.]{1,2}[\\\/]#', $file)
  39. ) {
  40. if ($_template->parent->source->type != 'file' && $_template->parent->source->type != 'extends' &&
  41. !isset($_template->parent->_cache[ 'allow_relative_path' ])
  42. ) {
  43. throw new SmartyException("Template '{$file}' cannot be relative to template of resource type '{$_template->parent->source->type}'");
  44. }
  45. // normalize path
  46. $path = $source->smarty->_realpath(dirname($_template->parent->source->filepath) . DS . $file);
  47. // files relative to a template only get one shot
  48. return is_file($path) ? $path : false;
  49. }
  50. // normalize DS
  51. if (strpos($file, DS == '/' ? '\\' : '/') !== false) {
  52. $file = str_replace(DS == '/' ? '\\' : '/', DS, $file);
  53. }
  54. $_directories = $source->smarty->getTemplateDir(null, $source->isConfig);
  55. // template_dir index?
  56. if ($file[ 0 ] == '[' && preg_match('#^\[([^\]]+)\](.+)$#', $file, $fileMatch)) {
  57. $file = $fileMatch[ 2 ];
  58. $_indices = explode(',', $fileMatch[ 1 ]);
  59. $_index_dirs = array();
  60. foreach ($_indices as $index) {
  61. $index = trim($index);
  62. // try string indexes
  63. if (isset($_directories[ $index ])) {
  64. $_index_dirs[] = $_directories[ $index ];
  65. } elseif (is_numeric($index)) {
  66. // try numeric index
  67. $index = (int) $index;
  68. if (isset($_directories[ $index ])) {
  69. $_index_dirs[] = $_directories[ $index ];
  70. } else {
  71. // try at location index
  72. $keys = array_keys($_directories);
  73. if (isset($_directories[ $keys[ $index ] ])) {
  74. $_index_dirs[] = $_directories[ $keys[ $index ] ];
  75. }
  76. }
  77. }
  78. }
  79. if (empty($_index_dirs)) {
  80. // index not found
  81. return false;
  82. } else {
  83. $_directories = $_index_dirs;
  84. }
  85. }
  86. // relative file name?
  87. foreach ($_directories as $_directory) {
  88. $path = $_directory . $file;
  89. if (is_file($path)) {
  90. return (strpos($path, '.' . DS) !== false) ? $source->smarty->_realpath($path) : $path;
  91. }
  92. }
  93. if (!isset($_index_dirs)) {
  94. // Could be relative to cwd
  95. $path = $source->smarty->_realpath($file, true);
  96. if (is_file($path)) {
  97. return $path;
  98. }
  99. }
  100. // Use include path ?
  101. if ($source->smarty->use_include_path) {
  102. return $source->smarty->ext->_getIncludePath->getIncludePath($_directories, $file, $source->smarty);
  103. }
  104. return false;
  105. }
  106. /**
  107. * populate Source Object with meta data from Resource
  108. *
  109. * @param Smarty_Template_Source $source source object
  110. * @param Smarty_Internal_Template $_template template object
  111. */
  112. public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
  113. {
  114. $source->filepath = $this->buildFilepath($source, $_template);
  115. if ($source->filepath !== false) {
  116. if (isset($source->smarty->security_policy) && is_object($source->smarty->security_policy)) {
  117. $source->smarty->security_policy->isTrustedResourceDir($source->filepath, $source->isConfig);
  118. }
  119. $source->exists = true;
  120. $source->uid = sha1($source->filepath . ($source->isConfig ? $source->smarty->_joined_config_dir :
  121. $source->smarty->_joined_template_dir));
  122. $source->timestamp = filemtime($source->filepath);
  123. } else {
  124. $source->timestamp = $source->exists = false;
  125. }
  126. }
  127. /**
  128. * populate Source Object with timestamp and exists from Resource
  129. *
  130. * @param Smarty_Template_Source $source source object
  131. */
  132. public function populateTimestamp(Smarty_Template_Source $source)
  133. {
  134. if (!$source->exists) {
  135. $source->timestamp = $source->exists = is_file($source->filepath);
  136. }
  137. if ($source->exists) {
  138. $source->timestamp = filemtime($source->filepath);
  139. }
  140. }
  141. /**
  142. * Load template's source from file into current template object
  143. *
  144. * @param Smarty_Template_Source $source source object
  145. *
  146. * @return string template source
  147. * @throws SmartyException if source cannot be loaded
  148. */
  149. public function getContent(Smarty_Template_Source $source)
  150. {
  151. if ($source->exists) {
  152. return file_get_contents($source->filepath);
  153. }
  154. throw new SmartyException('Unable to read ' . ($source->isConfig ? 'config' : 'template') .
  155. " {$source->type} '{$source->name}'");
  156. }
  157. /**
  158. * Determine basename for compiled filename
  159. *
  160. * @param Smarty_Template_Source $source source object
  161. *
  162. * @return string resource's basename
  163. */
  164. public function getBasename(Smarty_Template_Source $source)
  165. {
  166. return basename($source->filepath);
  167. }
  168. }