1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based picture gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008 Piwigo Team http://piwigo.org | |
---|
6 | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | |
---|
7 | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | |
---|
8 | // +-----------------------------------------------------------------------+ |
---|
9 | // | This program is free software; you can redistribute it and/or modify | |
---|
10 | // | it under the terms of the GNU General Public License as published by | |
---|
11 | // | the Free Software Foundation | |
---|
12 | // | | |
---|
13 | // | This program is distributed in the hope that it will be useful, but | |
---|
14 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
15 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
16 | // | General Public License for more details. | |
---|
17 | // | | |
---|
18 | // | You should have received a copy of the GNU General Public License | |
---|
19 | // | along with this program; if not, write to the Free Software | |
---|
20 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
21 | // | USA. | |
---|
22 | // +-----------------------------------------------------------------------+ |
---|
23 | |
---|
24 | |
---|
25 | require_once 'smarty/libs/Smarty.class.php'; |
---|
26 | |
---|
27 | // migrate lang:XXX |
---|
28 | // sed "s/{lang:\([^}]\+\)}/{\'\1\'|@translate}/g" my_template.tpl |
---|
29 | // migrate change root level vars {XXX} |
---|
30 | // sed "s/{pwg_root}/{ROOT_URL}/g" my_template.tpl |
---|
31 | // migrate change root level vars {XXX} |
---|
32 | // sed "s/{\([a-zA-Z_]\+\)}/{$\1}/g" my_template.tpl |
---|
33 | // migrate all |
---|
34 | // cat my_template.tpl | sed "s/{lang:\([^}]\+\)}/{\'\1\'|@translate}/g" | sed "s/{pwg_root}/{ROOT_URL}/g" | sed "s/{\([a-zA-Z_]\+\)}/{$\1}/g" |
---|
35 | |
---|
36 | |
---|
37 | class Template { |
---|
38 | |
---|
39 | var $smarty; |
---|
40 | |
---|
41 | var $output = ''; |
---|
42 | |
---|
43 | // Hash of filenames for each template handle. |
---|
44 | var $files = array(); |
---|
45 | |
---|
46 | function Template($root = ".", $theme= "") |
---|
47 | { |
---|
48 | global $conf; |
---|
49 | |
---|
50 | $this->smarty = new Smarty; |
---|
51 | $this->smarty->debugging = $conf['debug_template']; |
---|
52 | //$this->smarty->force_compile = true; |
---|
53 | |
---|
54 | if ( isset($conf['compiled_template_dir'] ) ) |
---|
55 | { |
---|
56 | $compile_dir = $conf['compiled_template_dir']; |
---|
57 | } |
---|
58 | else |
---|
59 | { |
---|
60 | $compile_dir = $conf['local_data_dir']; |
---|
61 | if ( !is_dir($compile_dir) ) |
---|
62 | { |
---|
63 | mkdir( $compile_dir, 0777); |
---|
64 | file_put_contents($compile_dir.'/index.htm', ''); |
---|
65 | } |
---|
66 | $compile_dir .= '/templates_c'; |
---|
67 | } |
---|
68 | if ( !is_dir($compile_dir) ) |
---|
69 | { |
---|
70 | mkdir( $compile_dir, 0777 ); |
---|
71 | file_put_contents($compile_dir.'/index.htm', ''); |
---|
72 | } |
---|
73 | |
---|
74 | $this->smarty->compile_dir = $compile_dir; |
---|
75 | |
---|
76 | $this->smarty->assign_by_ref( 'pwg', new PwgTemplateAdapter() ); |
---|
77 | $this->smarty->register_modifier( 'translate', array('Template', 'mod_translate') ); |
---|
78 | $this->smarty->register_modifier( 'explode', array('Template', 'mod_explode') ); |
---|
79 | |
---|
80 | if ( !empty($theme) ) |
---|
81 | { |
---|
82 | include($root.'/theme/'.$theme.'/themeconf.inc.php'); |
---|
83 | $this->smarty->assign('themeconf', $themeconf); |
---|
84 | } |
---|
85 | |
---|
86 | $this->set_template_dir($root); |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | * Sets the template root directory for this Template object. |
---|
91 | */ |
---|
92 | function set_template_dir($dir) |
---|
93 | { |
---|
94 | $this->smarty->template_dir = $dir; |
---|
95 | |
---|
96 | $real_dir = realpath($dir); |
---|
97 | $compile_id = crc32( $real_dir===false ? $dir : $real_dir); |
---|
98 | $this->smarty->compile_id = sprintf('%08X', $compile_id ); |
---|
99 | } |
---|
100 | |
---|
101 | /** |
---|
102 | * Gets the template root directory for this Template object. |
---|
103 | */ |
---|
104 | function get_template_dir() |
---|
105 | { |
---|
106 | return $this->smarty->template_dir; |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | * Deletes all compiled templates. |
---|
111 | */ |
---|
112 | function delete_compiled_templates() |
---|
113 | { |
---|
114 | $save_compile_id = $this->smarty->compile_id; |
---|
115 | $this->smarty->compile_id = null; |
---|
116 | $this->smarty->clear_compiled_tpl(); |
---|
117 | $this->smarty->compile_id = $save_compile_id; |
---|
118 | file_put_contents($this->smarty->compile_dir.'/index.htm', ''); |
---|
119 | } |
---|
120 | |
---|
121 | function get_themeconf($val) |
---|
122 | { |
---|
123 | $tc = $this->smarty->get_template_vars('themeconf'); |
---|
124 | return isset($tc[$val]) ? $tc[$val] : ''; |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | * Sets the template filename for handle. |
---|
129 | */ |
---|
130 | function set_filename($handle, $filename) |
---|
131 | { |
---|
132 | return $this->set_filenames( array($handle=>$filename) ); |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * Sets the template filenames for handles. $filename_array should be a |
---|
137 | * hash of handle => filename pairs. |
---|
138 | */ |
---|
139 | function set_filenames($filename_array) |
---|
140 | { |
---|
141 | if (!is_array($filename_array)) |
---|
142 | { |
---|
143 | return false; |
---|
144 | } |
---|
145 | |
---|
146 | reset($filename_array); |
---|
147 | while(list($handle, $filename) = each($filename_array)) |
---|
148 | { |
---|
149 | if (is_null($filename)) |
---|
150 | unset( $this->files[$handle] ); |
---|
151 | else |
---|
152 | $this->files[$handle] = $filename; |
---|
153 | } |
---|
154 | return true; |
---|
155 | } |
---|
156 | |
---|
157 | /** see smarty assign http://www.smarty.net/manual/en/api.assign.php */ |
---|
158 | function assign($tpl_var, $value = null) |
---|
159 | { |
---|
160 | $this->smarty->assign( $tpl_var, $value ); |
---|
161 | } |
---|
162 | |
---|
163 | /** |
---|
164 | * Inserts the uncompiled code for $handle as the value of $varname in the |
---|
165 | * root-level. This can be used to effectively include a template in the |
---|
166 | * middle of another template. |
---|
167 | * This is equivalent to assign($varname, $this->parse($handle, true)) |
---|
168 | */ |
---|
169 | function assign_var_from_handle($varname, $handle) |
---|
170 | { |
---|
171 | $this->assign($varname, $this->parse($handle, true)); |
---|
172 | return true; |
---|
173 | } |
---|
174 | |
---|
175 | /** see smarty append http://www.smarty.net/manual/en/api.append.php */ |
---|
176 | function append($tpl_var, $value=null, $merge=false) |
---|
177 | { |
---|
178 | $this->smarty->append( $tpl_var, $value, $merge ); |
---|
179 | } |
---|
180 | |
---|
181 | /** |
---|
182 | * Root-level variable concatenation. Appends a string to an existing |
---|
183 | * variable assignment with the same name. |
---|
184 | */ |
---|
185 | function concat($tpl_var, $value) |
---|
186 | { |
---|
187 | $old_val = & $this->smarty->get_template_vars($tpl_var); |
---|
188 | if ( isset($old_val) ) |
---|
189 | { |
---|
190 | $old_val .= $value; |
---|
191 | } |
---|
192 | else |
---|
193 | { |
---|
194 | $this->assign($tpl_var, $value); |
---|
195 | } |
---|
196 | } |
---|
197 | |
---|
198 | /** see smarty append http://www.smarty.net/manual/en/api.clear_assign.php */ |
---|
199 | function clear_assign($tpl_var) |
---|
200 | { |
---|
201 | $this->smarty->clear_assign( $tpl_var ); |
---|
202 | } |
---|
203 | |
---|
204 | /** see smarty get_template_vars http://www.smarty.net/manual/en/api.get_template_vars.php */ |
---|
205 | function &get_template_vars($name=null) |
---|
206 | { |
---|
207 | return $this->smarty->get_template_vars( $name ); |
---|
208 | } |
---|
209 | |
---|
210 | |
---|
211 | /** |
---|
212 | * Load the file for the handle, eventually compile the file and run the compiled |
---|
213 | * code. This will add the output to the results or return the result if $return |
---|
214 | * is true. |
---|
215 | */ |
---|
216 | function parse($handle, $return=false) |
---|
217 | { |
---|
218 | if ( !isset($this->files[$handle]) ) |
---|
219 | { |
---|
220 | die("Template->parse(): Couldn't load template file for handle $handle"); |
---|
221 | } |
---|
222 | |
---|
223 | $this->smarty->assign( 'ROOT_URL', get_root_url() ); |
---|
224 | $this->smarty->assign( 'TAG_INPUT_ENABLED', |
---|
225 | ((is_adviser()) ? 'disabled="disabled" onclick="return false;"' : '')); |
---|
226 | $v = $this->smarty->fetch($this->files[$handle], null, null, false); |
---|
227 | if ($return) |
---|
228 | { |
---|
229 | return $v; |
---|
230 | } |
---|
231 | $this->output .= $v; |
---|
232 | } |
---|
233 | |
---|
234 | /** |
---|
235 | * Load the file for the handle, eventually compile the file and run the compiled |
---|
236 | * code. This will print out the results of executing the template. |
---|
237 | */ |
---|
238 | function pparse($handle) |
---|
239 | { |
---|
240 | $this->parse($handle, false); |
---|
241 | echo $this->output; |
---|
242 | $this->output=''; |
---|
243 | |
---|
244 | } |
---|
245 | |
---|
246 | |
---|
247 | /** flushes the output */ |
---|
248 | function p() |
---|
249 | { |
---|
250 | $start = get_moment(); |
---|
251 | |
---|
252 | echo $this->output; |
---|
253 | $this->output=''; |
---|
254 | |
---|
255 | if ($this->smarty->debugging) |
---|
256 | { |
---|
257 | global $t2; |
---|
258 | $this->smarty->assign( |
---|
259 | array( |
---|
260 | 'AAAA_DEBUG_OUTPUT_TIME__' => get_elapsed_time($start, get_moment()), |
---|
261 | 'AAAA_DEBUG_TOTAL_TIME__' => get_elapsed_time($t2, get_moment()) |
---|
262 | ) |
---|
263 | ); |
---|
264 | require_once(SMARTY_CORE_DIR . 'core.display_debug_console.php'); |
---|
265 | echo smarty_core_display_debug_console(null, $this->smarty); |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | /** |
---|
270 | * translate variable modifier - translates a text to the currently loaded |
---|
271 | * language |
---|
272 | */ |
---|
273 | /*static*/ function mod_translate($text) |
---|
274 | { |
---|
275 | return l10n($text); |
---|
276 | } |
---|
277 | |
---|
278 | /** |
---|
279 | * explode variable modifier - similar to php explode |
---|
280 | * 'Yes;No'|@explode:';' -> array('Yes', 'No') |
---|
281 | */ |
---|
282 | /*static*/ function mod_explode($text, $delimiter=',') |
---|
283 | { |
---|
284 | return explode($delimiter, $text); |
---|
285 | } |
---|
286 | } |
---|
287 | |
---|
288 | /** |
---|
289 | * This class contains basic functions that can be called directly from the |
---|
290 | * templates in the form $pwg->l10n('edit') |
---|
291 | */ |
---|
292 | class PwgTemplateAdapter |
---|
293 | { |
---|
294 | function l10n($text) |
---|
295 | { |
---|
296 | return l10n($text); |
---|
297 | } |
---|
298 | |
---|
299 | function l10n_dec($s, $p, $v) |
---|
300 | { |
---|
301 | return l10n_dec($s, $p, $v); |
---|
302 | } |
---|
303 | |
---|
304 | function sprintf() |
---|
305 | { |
---|
306 | $args = func_get_args(); |
---|
307 | return call_user_func_array('sprintf', $args ); |
---|
308 | } |
---|
309 | } |
---|
310 | |
---|
311 | ?> |
---|