PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/fsn-site-central/mediatheque/include/smarty/libs/sysplugins/smarty_internal_resource_file.php

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