]> git.mxchange.org Git - friendica.git/blob - library/Smarty/libs/sysplugins/smarty_internal_resource_extends.php
add smarty engine, remove some obsolete zot1 stuff
[friendica.git] / library / Smarty / libs / sysplugins / smarty_internal_resource_extends.php
1 <?php
2 /**
3 * Smarty Internal Plugin Resource Extends
4 *
5 * @package Smarty
6 * @subpackage TemplateResources
7 * @author Uwe Tews
8 * @author Rodney Rehm
9 */
10
11 /**
12 * Smarty Internal Plugin Resource Extends
13 *
14 * Implements the file system as resource for Smarty which {extend}s a chain of template files templates
15 *
16 * @package Smarty
17 * @subpackage TemplateResources
18 */
19 class Smarty_Internal_Resource_Extends extends Smarty_Resource {
20
21     /**
22     * mbstring.overload flag
23     *
24     * @var int
25     */
26     public $mbstring_overload = 0;
27
28     /**
29     * populate Source Object with meta data from Resource
30     *
31     * @param Smarty_Template_Source   $source    source object
32     * @param Smarty_Internal_Template $_template template object
33     */
34     public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template=null)
35     {
36         $uid = '';
37         $sources = array();
38         $components = explode('|', $source->name);
39         $exists = true;
40         foreach ($components as $component) {
41             $s = Smarty_Resource::source(null, $source->smarty, $component);
42             if ($s->type == 'php') {
43                 throw new SmartyException("Resource type {$s->type} cannot be used with the extends resource type");
44             }
45             $sources[$s->uid] = $s;
46             $uid .= $s->filepath;
47             if ($_template && $_template->smarty->compile_check) {
48                 $exists = $exists && $s->exists;
49             }
50         }
51         $source->components = $sources;
52         $source->filepath = $s->filepath;
53         $source->uid = sha1($uid);
54         if ($_template && $_template->smarty->compile_check) {
55             $source->timestamp = $s->timestamp;
56             $source->exists = $exists;
57         }
58         // need the template at getContent()
59         $source->template = $_template;
60     }
61
62     /**
63     * populate Source Object with timestamp and exists from Resource
64     *
65     * @param Smarty_Template_Source $source source object
66     */
67     public function populateTimestamp(Smarty_Template_Source $source)
68     {
69         $source->exists = true;
70         foreach ($source->components as $s) {
71             $source->exists = $source->exists && $s->exists;
72         }
73         $source->timestamp = $s->timestamp;
74     }
75
76     /**
77     * Load template's source from files into current template object
78     *
79     * @param Smarty_Template_Source $source source object
80     * @return string template source
81     * @throws SmartyException if source cannot be loaded
82     */
83     public function getContent(Smarty_Template_Source $source)
84     {
85         if (!$source->exists) {
86             throw new SmartyException("Unable to read template {$source->type} '{$source->name}'");
87         }
88
89         $this->mbstring_overload = ini_get('mbstring.func_overload') & 2;
90         $_rdl = preg_quote($source->smarty->right_delimiter);
91         $_ldl = preg_quote($source->smarty->left_delimiter);
92         $_components = array_reverse($source->components);
93         $_first = reset($_components);
94         $_last = end($_components);
95
96         foreach ($_components as $_component) {
97             // register dependency
98             if ($_component != $_first) {
99                 $source->template->properties['file_dependency'][$_component->uid] = array($_component->filepath, $_component->timestamp, $_component->type);
100             }
101
102             // read content
103             $source->filepath = $_component->filepath;
104             $_content = $_component->content;
105
106             // extend sources
107             if ($_component != $_last) {
108                 if (preg_match_all("!({$_ldl}block\s(.+?){$_rdl})!", $_content, $_open) !=
109                 preg_match_all("!({$_ldl}/block{$_rdl})!", $_content, $_close)) {
110                     throw new SmartyException("unmatched {block} {/block} pairs in template {$_component->type} '{$_component->name}'");
111                 }
112                 preg_match_all("!{$_ldl}block\s(.+?){$_rdl}|{$_ldl}/block{$_rdl}|{$_ldl}\*([\S\s]*?)\*{$_rdl}!", $_content, $_result, PREG_OFFSET_CAPTURE);
113                 $_result_count = count($_result[0]);
114                 $_start = 0;
115                 while ($_start+1 < $_result_count) {
116                     $_end = 0;
117                     $_level = 1;
118                     if (($this->mbstring_overload ? mb_substr($_result[0][$_start][0],0,mb_strlen($source->smarty->left_delimiter,'latin1')+1, 'latin1') : substr($_result[0][$_start][0],0,strlen($source->smarty->left_delimiter)+1)) == $source->smarty->left_delimiter.'*') {
119                         $_start++;
120                         continue;
121                     }
122                     while ($_level != 0) {
123                         $_end++;
124                         if (($this->mbstring_overload ? mb_substr($_result[0][$_start + $_end][0],0,mb_strlen($source->smarty->left_delimiter,'latin1')+1, 'latin1') : substr($_result[0][$_start + $_end][0],0,strlen($source->smarty->left_delimiter)+1)) == $source->smarty->left_delimiter.'*') {
125                             continue;
126                         }
127                         if (!strpos($_result[0][$_start + $_end][0], '/')) {
128                             $_level++;
129                         } else {
130                             $_level--;
131                         }
132                     }
133                     $_block_content = str_replace($source->smarty->left_delimiter . '$smarty.block.parent' . $source->smarty->right_delimiter, '%%%%SMARTY_PARENT%%%%',
134                     ($this->mbstring_overload ? mb_substr($_content, $_result[0][$_start][1] + mb_strlen($_result[0][$_start][0], 'latin1'), $_result[0][$_start + $_end][1] - $_result[0][$_start][1] - + mb_strlen($_result[0][$_start][0], 'latin1'), 'latin1') : substr($_content, $_result[0][$_start][1] + strlen($_result[0][$_start][0]), $_result[0][$_start + $_end][1] - $_result[0][$_start][1] - + strlen($_result[0][$_start][0]))));
135                     Smarty_Internal_Compile_Block::saveBlockData($_block_content, $_result[0][$_start][0], $source->template, $_component->filepath);
136                     $_start = $_start + $_end + 1;
137                 }
138             } else {
139                 return $_content;
140             }
141         }
142     }
143
144     /**
145     * Determine basename for compiled filename
146     *
147     * @param Smarty_Template_Source $source source object
148     * @return string resource's basename
149     */
150     public function getBasename(Smarty_Template_Source $source)
151     {
152         return str_replace(':', '.', basename($source->filepath));
153     }
154
155 }
156
157 ?>