You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_internal_resource_file.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 && $_template->_isSubTpl() &&
  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) . DIRECTORY_SEPARATOR . $file);
  47. // files relative to a template only get one shot
  48. return is_file($path) ? $path : false;
  49. }
  50. // normalize DIRECTORY_SEPARATOR
  51. if (strpos($file, DIRECTORY_SEPARATOR === '/' ? '\\' : '/') !== false) {
  52. $file = str_replace(DIRECTORY_SEPARATOR === '/' ? '\\' : '/', DIRECTORY_SEPARATOR, $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, '.' . DIRECTORY_SEPARATOR) !== 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. * @throws \SmartyException
  113. */
  114. public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
  115. {
  116. $source->filepath = $this->buildFilepath($source, $_template);
  117. if ($source->filepath !== false) {
  118. if (isset($source->smarty->security_policy) && is_object($source->smarty->security_policy)) {
  119. $source->smarty->security_policy->isTrustedResourceDir($source->filepath, $source->isConfig);
  120. }
  121. $source->exists = true;
  122. $source->uid = sha1($source->filepath . ($source->isConfig ? $source->smarty->_joined_config_dir :
  123. $source->smarty->_joined_template_dir));
  124. $source->timestamp = filemtime($source->filepath);
  125. } else {
  126. $source->timestamp = $source->exists = false;
  127. }
  128. }
  129. /**
  130. * populate Source Object with timestamp and exists from Resource
  131. *
  132. * @param Smarty_Template_Source $source source object
  133. */
  134. public function populateTimestamp(Smarty_Template_Source $source)
  135. {
  136. if (!$source->exists) {
  137. $source->timestamp = $source->exists = is_file($source->filepath);
  138. }
  139. if ($source->exists) {
  140. $source->timestamp = filemtime($source->filepath);
  141. }
  142. }
  143. /**
  144. * Load template's source from file into current template object
  145. *
  146. * @param Smarty_Template_Source $source source object
  147. *
  148. * @return string template source
  149. * @throws SmartyException if source cannot be loaded
  150. */
  151. public function getContent(Smarty_Template_Source $source)
  152. {
  153. if ($source->exists) {
  154. return file_get_contents($source->filepath);
  155. }
  156. throw new SmartyException('Unable to read ' . ($source->isConfig ? 'config' : 'template') .
  157. " {$source->type} '{$source->name}'");
  158. }
  159. /**
  160. * Determine basename for compiled filename
  161. *
  162. * @param Smarty_Template_Source $source source object
  163. *
  164. * @return string resource's basename
  165. */
  166. public function getBasename(Smarty_Template_Source $source)
  167. {
  168. return basename($source->filepath);
  169. }
  170. }