3 * Smarty Internal Plugin Resource File
6 * @subpackage TemplateResources
12 * Smarty Internal Plugin Resource File
13 * Implements the file system as resource for Smarty templates
16 * @subpackage TemplateResources
18 class Smarty_Internal_Resource_File extends Smarty_Resource
21 * build template filepath by traversing the template_dir array
23 * @param Smarty_Template_Source $source source object
24 * @param Smarty_Internal_Template $_template template object
26 * @return string fully qualified filepath
27 * @throws SmartyException
29 protected function buildFilepath(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
31 $file = $source->name;
33 if ($file[ 0 ] == '/' || $file[ 1 ] == ':') {
34 $file = $source->smarty->_realpath($file, true);
35 return is_file($file) ? $file : false;
37 // go relative to a given template?
38 if ($file[ 0 ] == '.' && $_template && $_template->_isSubTpl() &&
39 preg_match('#^[.]{1,2}[\\\/]#', $file)
41 if ($_template->parent->source->type != 'file' && $_template->parent->source->type != 'extends' &&
42 !isset($_template->parent->_cache[ 'allow_relative_path' ])
44 throw new SmartyException("Template '{$file}' cannot be relative to template of resource type '{$_template->parent->source->type}'");
47 $path = $source->smarty->_realpath(dirname($_template->parent->source->filepath) . $source->smarty->ds . $file);
48 // files relative to a template only get one shot
49 return is_file($path) ? $path : false;
51 // normalize $source->smarty->ds
52 if (strpos($file, $source->smarty->ds == '/' ? '\\' : '/') !== false) {
53 $file = str_replace($source->smarty->ds == '/' ? '\\' : '/', $source->smarty->ds, $file);
56 $_directories = $source->smarty->getTemplateDir(null, $source->isConfig);
57 // template_dir index?
58 if ($file[ 0 ] == '[' && preg_match('#^\[([^\]]+)\](.+)$#', $file, $fileMatch)) {
59 $file = $fileMatch[ 2 ];
60 $_indices = explode(',', $fileMatch[ 1 ]);
61 $_index_dirs = array();
62 foreach ($_indices as $index) {
63 $index = trim($index);
65 if (isset($_directories[ $index ])) {
66 $_index_dirs[] = $_directories[ $index ];
67 } elseif (is_numeric($index)) {
69 $index = (int) $index;
70 if (isset($_directories[ $index ])) {
71 $_index_dirs[] = $_directories[ $index ];
73 // try at location index
74 $keys = array_keys($_directories);
75 if (isset($_directories[ $keys[ $index ] ])) {
76 $_index_dirs[] = $_directories[ $keys[ $index ] ];
81 if (empty($_index_dirs)) {
85 $_directories = $_index_dirs;
89 // relative file name?
90 foreach ($_directories as $_directory) {
91 $path = $_directory . $file;
93 return (strpos($path, '.' . $source->smarty->ds) !== false) ? $source->smarty->_realpath($path) : $path;
96 if (!isset($_index_dirs)) {
97 // Could be relative to cwd
98 $path = $source->smarty->_realpath($file, true);
103 // Use include path ?
104 if ($source->smarty->use_include_path) {
105 return $source->smarty->ext->_getIncludePath->getIncludePath($_directories, $file, $source->smarty);
111 * populate Source Object with meta data from Resource
113 * @param Smarty_Template_Source $source source object
114 * @param Smarty_Internal_Template $_template template object
116 public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
118 $source->filepath = $this->buildFilepath($source, $_template);
120 if ($source->filepath !== false) {
121 if (isset($source->smarty->security_policy) && is_object($source->smarty->security_policy)) {
122 $source->smarty->security_policy->isTrustedResourceDir($source->filepath, $source->isConfig);
124 $source->exists = true;
125 $source->uid = sha1($source->filepath . ($source->isConfig ? $source->smarty->_joined_config_dir :
126 $source->smarty->_joined_template_dir));
127 $source->timestamp = filemtime($source->filepath);
129 $source->timestamp = $source->exists = false;
134 * populate Source Object with timestamp and exists from Resource
136 * @param Smarty_Template_Source $source source object
138 public function populateTimestamp(Smarty_Template_Source $source)
140 if (!$source->exists) {
141 $source->timestamp = $source->exists = is_file($source->filepath);
143 if ($source->exists) {
144 $source->timestamp = filemtime($source->filepath);
149 * Load template's source from file into current template object
151 * @param Smarty_Template_Source $source source object
153 * @return string template source
154 * @throws SmartyException if source cannot be loaded
156 public function getContent(Smarty_Template_Source $source)
158 if ($source->exists) {
159 return file_get_contents($source->filepath);
161 throw new SmartyException('Unable to read ' . ($source->isConfig ? 'config' : 'template') .
162 " {$source->type} '{$source->name}'");
166 * Determine basename for compiled filename
168 * @param Smarty_Template_Source $source source object
170 * @return string resource's basename
172 public function getBasename(Smarty_Template_Source $source)
174 return basename($source->filepath);