]> git.mxchange.org Git - friendica.git/blob - vendor/smarty/smarty/libs/sysplugins/smarty_internal_resource_file.php
a58771a17f91b7c23931e6ad723a4c2617b97872
[friendica.git] / vendor / smarty / smarty / libs / sysplugins / smarty_internal_resource_file.php
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 /**
12  * Smarty Internal Plugin Resource File
13  * Implements the file system as resource for Smarty templates
14  *
15  * @package    Smarty
16  * @subpackage TemplateResources
17  */
18 class Smarty_Internal_Resource_File extends Smarty_Resource
19 {
20     /**
21      * build template filepath by traversing the template_dir array
22      *
23      * @param Smarty_Template_Source    $source    source object
24      * @param  Smarty_Internal_Template $_template template object
25      *
26      * @return string fully qualified filepath
27      * @throws SmartyException
28      */
29     protected function buildFilepath(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
30     {
31         $file = $source->name;
32         // absolute file ?
33         if ($file[ 0 ] == '/' || $file[ 1 ] == ':') {
34             $file = $source->smarty->_realpath($file, true);
35             return is_file($file) ? $file : false;
36         }
37         // go relative to a given template?
38         if ($file[ 0 ] == '.' && $_template && $_template->_isSubTpl() &&
39             preg_match('#^[.]{1,2}[\\\/]#', $file)
40         ) {
41             if ($_template->parent->source->type != 'file' && $_template->parent->source->type != 'extends' &&
42                 !isset($_template->parent->_cache[ 'allow_relative_path' ])
43             ) {
44                 throw new SmartyException("Template '{$file}' cannot be relative to template of resource type '{$_template->parent->source->type}'");
45             }
46             // normalize path
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;
50         }
51         // normalize $source->smarty->ds
52         if (strpos($file, $source->smarty->ds == '/' ? '\\' : '/') !== false) {
53             $file = str_replace($source->smarty->ds == '/' ? '\\' : '/', $source->smarty->ds, $file);
54         }
55
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);
64                 // try string indexes
65                 if (isset($_directories[ $index ])) {
66                     $_index_dirs[] = $_directories[ $index ];
67                 } elseif (is_numeric($index)) {
68                     // try numeric index
69                     $index = (int) $index;
70                     if (isset($_directories[ $index ])) {
71                         $_index_dirs[] = $_directories[ $index ];
72                     } else {
73                         // try at location index
74                         $keys = array_keys($_directories);
75                         if (isset($_directories[ $keys[ $index ] ])) {
76                             $_index_dirs[] = $_directories[ $keys[ $index ] ];
77                         }
78                     }
79                 }
80             }
81             if (empty($_index_dirs)) {
82                 // index not found
83                 return false;
84             } else {
85                 $_directories = $_index_dirs;
86             }
87         }
88
89         // relative file name?
90         foreach ($_directories as $_directory) {
91             $path = $_directory . $file;
92             if (is_file($path)) {
93                 return (strpos($path, '.' . $source->smarty->ds) !== false) ? $source->smarty->_realpath($path) : $path;
94             }
95         }
96         if (!isset($_index_dirs)) {
97             // Could be relative to cwd
98             $path = $source->smarty->_realpath($file, true);
99             if (is_file($path)) {
100                 return $path;
101             }
102         }
103         // Use include path ?
104         if ($source->smarty->use_include_path) {
105             return $source->smarty->ext->_getIncludePath->getIncludePath($_directories, $file, $source->smarty);
106         }
107         return false;
108     }
109
110     /**
111      * populate Source Object with meta data from Resource
112      *
113      * @param Smarty_Template_Source   $source    source object
114      * @param Smarty_Internal_Template $_template template object
115      */
116     public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
117     {
118         $source->filepath = $this->buildFilepath($source, $_template);
119
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);
123             }
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);
128         } else {
129             $source->timestamp = $source->exists = false;
130         }
131     }
132
133     /**
134      * populate Source Object with timestamp and exists from Resource
135      *
136      * @param Smarty_Template_Source $source source object
137      */
138     public function populateTimestamp(Smarty_Template_Source $source)
139     {
140         if (!$source->exists) {
141             $source->timestamp = $source->exists = is_file($source->filepath);
142         }
143         if ($source->exists) {
144             $source->timestamp = filemtime($source->filepath);
145         }
146     }
147
148     /**
149      * Load template's source from file into current template object
150      *
151      * @param  Smarty_Template_Source $source source object
152      *
153      * @return string                 template source
154      * @throws SmartyException        if source cannot be loaded
155      */
156     public function getContent(Smarty_Template_Source $source)
157     {
158         if ($source->exists) {
159             return file_get_contents($source->filepath);
160         }
161         throw new SmartyException('Unable to read ' . ($source->isConfig ? 'config' : 'template') .
162                                   " {$source->type} '{$source->name}'");
163     }
164
165     /**
166      * Determine basename for compiled filename
167      *
168      * @param  Smarty_Template_Source $source source object
169      *
170      * @return string                 resource's basename
171      */
172     public function getBasename(Smarty_Template_Source $source)
173     {
174         return basename($source->filepath);
175     }
176 }