libs/sysplugins/smarty_internal_resource_file.php Quellcode

smarty_internal_resource_file.php
gehe zur Dokumentation dieser Datei
1 <?php
19 {
29  protected function buildFilepath(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
30  {
31  $file = $source->name;
32  preg_match('#^(?P<absolute>[\\\/]|[a-zA-Z]:[\\\/])|(\[(?P<index>[^\]]+)\])|(?P<rel>\.[\\\/])#', $file, $fileMatch);
33  // save basename
34  if (!empty($fileMatch['absolute'])) {
35  $file = $this->normalizePath($file);
36  return is_file($file) ? $file : false;
37  }
38  // go relative to a given template?
39  if (!empty($fileMatch['rel']) && $_template && $_template->parent instanceof Smarty_Internal_Template) {
40  if ($_template->parent->source->type != 'file' && $_template->parent->source->type != 'extends' && !$_template->parent->allow_relative_path) {
41  throw new SmartyException("Template '{$file}' cannot be relative to template of resource type '{$_template->parent->source->type}'");
42  }
43  $path = dirname($_template->parent->source->filepath) . DS . $file;
44  // normalize path
45  $path = $this->normalizePath($path);
46  // files relative to a template only get one shot
47  return is_file($path) ? $path : false;
48  }
49 
50  if ($source->isConfig) {
51  $_directories = $source->smarty->getConfigDir();
52  } else {
53  $_directories = $source->smarty->getTemplateDir();
54  }
55  // template_dir index?
56  if (!empty($fileMatch['index'])) {
57  $index = $fileMatch['index'];
58  $_directory = null;
59  // try string indexes
60  if (isset($_directories[$index])) {
61  $_directory = $_directories[$index];
62  } elseif (is_numeric($index)) {
63  // try numeric index
64  $index = (int) $index;
65  if (isset($_directories[$index])) {
66  $_directory = $_directories[$index];
67  } else {
68  // try at location index
69  $keys = array_keys($_directories);
70  $_directory = $_directories[$keys[$index]];
71  }
72  }
73  if ($_directory) {
74  preg_match('#\](.+)$#', $file, $fileMatch);
75  $path = $_directory . $fileMatch[1];
76  $path = $this->normalizePath($path);
77  if (is_file($path)) {
78  return $path;
79  }
80  } else {
81  // index not found
82  return false;
83  }
84  }
85 
86  // relative file name?
87  foreach ($_directories as $_directory) {
88  $_filepath = $_directory . $file;
89  $path = $this->normalizePath($_filepath);
90  if (is_file($path)) {
91  return $path;
92  }
93  if ($source->smarty->use_include_path && !preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $_directory)) {
94  // try PHP include_path
95  if (function_exists('stream_resolve_include_path')) {
96  $_filepath = stream_resolve_include_path($_filepath);
97  } else {
98  $_filepath = Smarty_Internal_Get_Include_Path::getIncludePath($_filepath);
99  }
100  if ($_filepath !== false) {
101  $path = $this->normalizePath($_filepath);
102  if (is_file($path)) {
103  return $path;
104  }
105  }
106  }
107  }
108  // Could be relative to cwd
109  $path = $this->normalizePath(getcwd() . DS . $file);
110  return is_file($path) ? $path : false;
111  }
112 
122  public function normalizePath($path)
123  {
124  if ($path[0] == '.') {
125  $path = getcwd() . DS . $path;
126  }
127  $path = preg_replace('#[\\\/]+([.][\\\/]+)*#', DS, $path);
128  while (strrpos($path, '.' . DS) !== false) {
129  $path = preg_replace('#([\\\/]([^\\\/]+[\\\/]){2}([.][.][\\\/]){2})|([\\\/][^\\\/]+[\\\/][.][.][\\\/])#', DS, $path);
130  }
131  return $path;
132  }
133 
142  protected function fileExists(Smarty_Template_Source $source, $file)
143  {
144  $source->timestamp = is_file($file) ? @filemtime($file) : false;
145  return $source->exists = !!$source->timestamp;
146  }
147 
154  public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
155  {
156  $source->filepath = $this->buildFilepath($source, $_template);
157 
158  if ($source->filepath !== false) {
159  if (is_object($source->smarty->security_policy)) {
160  $source->smarty->security_policy->isTrustedResourceDir($source->filepath);
161  }
162  $source->exists = true;
163  $source->uid = sha1($source->filepath);
164  if ($source->smarty->compile_check && !isset($source->timestamp)) {
165  $source->timestamp = @filemtime($source->filepath);
166  }
167  } else {
168  $source->timestamp = false;
169  $source->exists = false;
170  }
171  }
172 
178  public function populateTimestamp(Smarty_Template_Source $source)
179  {
180  $source->timestamp = $source->exists = is_file($source->filepath);
181  if ($source->exists) {
182  $source->timestamp = @filemtime($source->filepath);
183  }
184  }
185 
194  public function getContent(Smarty_Template_Source $source)
195  {
196  if ($source->timestamp) {
197  return file_get_contents($source->filepath);
198  }
199  if ($source instanceof Smarty_Config_Source) {
200  throw new SmartyException("Unable to read config {$source->type} '{$source->name}'");
201  }
202  throw new SmartyException("Unable to read template {$source->type} '{$source->name}'");
203  }
204 
212  public function getBasename(Smarty_Template_Source $source)
213  {
214  return basename($source->filepath);
215  }
216 }




Korrekturen, Hinweise und Ergänzungen

Bitte scheuen Sie sich nicht und melden Sie, was auf dieser Seite sachlich falsch oder irreführend ist, was ergänzt werden sollte, was fehlt usw. Dazu bitte oben aus dem Menü Seite den Eintrag Support Forum wählen. Es ist eine kostenlose Anmeldung erforderlich, um Anmerkungen zu posten. Unpassende Postings, Spam usw. werden kommentarlos entfernt.