1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | PhpWebGallery - a PHP based picture gallery | |
---|
4 | // | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net | |
---|
5 | // | Copyright (C) 2003-2008 PhpWebGallery Team - http://phpwebgallery.net | |
---|
6 | // +-----------------------------------------------------------------------+ |
---|
7 | // | file : $Id: template.class.php 2239 2008-03-01 22:02:40Z rub $ |
---|
8 | // | last update : $Date: 2008-03-01 22:02:40 +0000 (Sat, 01 Mar 2008) $ |
---|
9 | // | last modifier : $Author: rub $ |
---|
10 | // | revision : $Revision: 2239 $ |
---|
11 | // +-----------------------------------------------------------------------+ |
---|
12 | // | This program is free software; you can redistribute it and/or modify | |
---|
13 | // | it under the terms of the GNU General Public License as published by | |
---|
14 | // | the Free Software Foundation | |
---|
15 | // | | |
---|
16 | // | This program is distributed in the hope that it will be useful, but | |
---|
17 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
18 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
19 | // | General Public License for more details. | |
---|
20 | // | | |
---|
21 | // | You should have received a copy of the GNU General Public License | |
---|
22 | // | along with this program; if not, write to the Free Software | |
---|
23 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
24 | // | USA. | |
---|
25 | // +-----------------------------------------------------------------------+ |
---|
26 | |
---|
27 | |
---|
28 | require 'smarty/libs/Smarty.class.php'; |
---|
29 | |
---|
30 | // migrate lang:XXX |
---|
31 | // sed "s/{lang:\([^}]\+\)}/{\'\1\'|@translate}/g" my_template.tpl |
---|
32 | // migrate change root level vars {XXX} |
---|
33 | // sed "s/{pwg_root}/{ROOT_URL}/g" my_template.tpl |
---|
34 | // migrate change root level vars {XXX} |
---|
35 | // sed "s/{\([a-zA-Z_]\+\)}/{$\1}/g" my_template.tpl |
---|
36 | // migrate all |
---|
37 | // cat my_template.tpl | sed "s/{lang:\([^}]\+\)}/{\'\1\'|@translate}/g" | sed "s/{pwg_root}/{ROOT_URL}/g" | sed "s/{\([a-zA-Z_]\+\)}/{$\1}/g" |
---|
38 | |
---|
39 | |
---|
40 | class Template { |
---|
41 | |
---|
42 | var $smarty; |
---|
43 | |
---|
44 | var $_old; |
---|
45 | |
---|
46 | var $output = ''; |
---|
47 | |
---|
48 | // Hash of filenames for each template handle. |
---|
49 | var $files = array(); |
---|
50 | |
---|
51 | function Template($root = ".", $theme= "") |
---|
52 | { |
---|
53 | global $conf; |
---|
54 | |
---|
55 | $this->smarty = new Smarty; |
---|
56 | $this->smarty->debugging = $conf['debug_template']; |
---|
57 | //$this->smarty->force_compile = true; |
---|
58 | |
---|
59 | if ( isset($conf['compiled_template_dir'] ) ) |
---|
60 | { |
---|
61 | $compile_dir = $conf['compiled_template_dir']; |
---|
62 | } |
---|
63 | else |
---|
64 | { |
---|
65 | $compile_dir = $conf['local_data_dir']; |
---|
66 | if ( !is_dir($compile_dir) ) |
---|
67 | { |
---|
68 | mkdir( $compile_dir, 0777); |
---|
69 | file_put_contents($compile_dir.'/index.htm', ''); |
---|
70 | } |
---|
71 | $compile_dir .= '/templates_c'; |
---|
72 | } |
---|
73 | if ( !is_dir($compile_dir) ) |
---|
74 | { |
---|
75 | mkdir( $compile_dir, 0777 ); |
---|
76 | file_put_contents($compile_dir.'/index.htm', ''); |
---|
77 | } |
---|
78 | |
---|
79 | $this->smarty->compile_dir = $compile_dir; |
---|
80 | |
---|
81 | $this->smarty->template_dir = $root; |
---|
82 | $this->smarty->register_function( 'lang', array('Template', 'fn_l10n') ); |
---|
83 | |
---|
84 | $this->smarty->assign_by_ref( 'pwg', new PwgTemplateAdapter() ); |
---|
85 | $this->smarty->register_modifier( 'translate', array('Template', 'mod_translate') ); |
---|
86 | |
---|
87 | include($root.'/theme/'.$theme.'/themeconf.inc.php'); |
---|
88 | $this->smarty->assign('themeconf', $themeconf); |
---|
89 | |
---|
90 | $this->_old = & new TemplateOld($root, $theme); |
---|
91 | } |
---|
92 | |
---|
93 | /** DEPRECATED */ |
---|
94 | function get_themeconf($val) |
---|
95 | { |
---|
96 | $tc = $this->smarty->get_template_vars('themeconf'); |
---|
97 | return isset($tc[$val]) ? $tc[$val] : ''; |
---|
98 | } |
---|
99 | |
---|
100 | /** |
---|
101 | * Sets the template filename for handle. |
---|
102 | */ |
---|
103 | function set_filename($handle, $filename) |
---|
104 | { |
---|
105 | return $this->set_filenames( array($handle=>$filename) ); |
---|
106 | } |
---|
107 | |
---|
108 | /** |
---|
109 | * Sets the template filenames for handles. $filename_array should be a |
---|
110 | * hash of handle => filename pairs. |
---|
111 | */ |
---|
112 | function set_filenames($filename_array) |
---|
113 | { |
---|
114 | if (!is_array($filename_array)) |
---|
115 | { |
---|
116 | return false; |
---|
117 | } |
---|
118 | |
---|
119 | reset($filename_array); |
---|
120 | while(list($handle, $filename) = each($filename_array)) |
---|
121 | { |
---|
122 | if (is_null($filename)) |
---|
123 | unset( $this->files[$handle] ); |
---|
124 | else |
---|
125 | $this->files[$handle] = $filename; |
---|
126 | } |
---|
127 | return true; |
---|
128 | } |
---|
129 | |
---|
130 | /** |
---|
131 | * DEPRECATED - backward compatibility only; use assign |
---|
132 | */ |
---|
133 | function assign_vars($vararray) |
---|
134 | { |
---|
135 | is_array( $vararray ) || die('assign_vars parameter not array'); |
---|
136 | $this->assign( $vararray ); |
---|
137 | } |
---|
138 | |
---|
139 | /** |
---|
140 | * DEPRECATED - backward compatibility only; use assign |
---|
141 | */ |
---|
142 | function assign_var($varname, $varval) |
---|
143 | { |
---|
144 | !is_array( $varname ) || die('assign_var parameter name is array'); |
---|
145 | $this->assign( $varname, $varval ); |
---|
146 | } |
---|
147 | |
---|
148 | /** |
---|
149 | * Inserts the uncompiled code for $handle as the value of $varname in the |
---|
150 | * root-level. This can be used to effectively include a template in the |
---|
151 | * middle of another template. |
---|
152 | * This is equivalent to assign($varname, $this->parse($handle, true)) |
---|
153 | */ |
---|
154 | function assign_var_from_handle($varname, $handle) |
---|
155 | { |
---|
156 | $this->assign($varname, $this->parse($handle, true)); |
---|
157 | return true; |
---|
158 | } |
---|
159 | |
---|
160 | /** |
---|
161 | * DEPRECATED - backward compatibility only |
---|
162 | */ |
---|
163 | function assign_block_vars($blockname, $vararray) |
---|
164 | { |
---|
165 | if (strstr($blockname, '.')!==false) |
---|
166 | { |
---|
167 | $blocks = explode('.', $blockname); |
---|
168 | $blockcount = sizeof($blocks) - 1; |
---|
169 | $root_var = & $this->smarty->get_template_vars(); |
---|
170 | |
---|
171 | $str = '$root_var'; |
---|
172 | for ($i = 0; $i < $blockcount; $i++) |
---|
173 | { |
---|
174 | $str .= '[\'' . $blocks[$i] . '\']'; |
---|
175 | eval('$lastiteration = isset('.$str.') ? sizeof('.$str.')-1:0;'); |
---|
176 | $str .= '[' . $lastiteration . ']'; |
---|
177 | } |
---|
178 | $str .= '[\'' . $blocks[$blockcount] . '\'][] = $vararray;'; |
---|
179 | eval($str); |
---|
180 | } |
---|
181 | else |
---|
182 | $this->smarty->append( $blockname, $vararray ); |
---|
183 | |
---|
184 | $this->_old->assign_block_vars($blockname, $vararray); |
---|
185 | } |
---|
186 | |
---|
187 | /** |
---|
188 | * DEPRECATED - backward compatibility only |
---|
189 | */ |
---|
190 | function merge_block_vars($blockname, $vararray) |
---|
191 | { |
---|
192 | if (strstr($blockname, '.')!==false) |
---|
193 | { |
---|
194 | $blocks = explode('.', $blockname); |
---|
195 | $blockcount = count($blocks); |
---|
196 | $root_var = & $this->smarty->get_template_vars(); |
---|
197 | |
---|
198 | $str = '$root_var'; |
---|
199 | for ($i = 0; $i < $blockcount; $i++) |
---|
200 | { |
---|
201 | $str .= '[\'' . $blocks[$i] . '\']'; |
---|
202 | eval('$lastiteration = isset('.$str.') ? sizeof('.$str.')-1:-1;'); |
---|
203 | if ($lastiteration==-1) |
---|
204 | { |
---|
205 | return false; |
---|
206 | } |
---|
207 | $str .= '[' . $lastiteration . ']'; |
---|
208 | } |
---|
209 | $str = $str.'=array_merge('.$str.', $vararray);'; |
---|
210 | eval($str); |
---|
211 | } |
---|
212 | else |
---|
213 | $this->smarty->append( $blockname, $vararray, true ); |
---|
214 | |
---|
215 | $this->_old->merge_block_vars($blockname, $vararray); |
---|
216 | return true; |
---|
217 | } |
---|
218 | |
---|
219 | /** |
---|
220 | * Load the file for the handle, eventually compile the file and run the compiled |
---|
221 | * code. This will add the output to the results or return the result if $return |
---|
222 | * is true. |
---|
223 | */ |
---|
224 | function parse($handle, $return=false) |
---|
225 | { |
---|
226 | if ( !isset($this->files[$handle]) ) |
---|
227 | { |
---|
228 | die("Template->parse(): Couldn't load template file for handle $handle"); |
---|
229 | } |
---|
230 | |
---|
231 | $is_new = true; |
---|
232 | $params = array('resource_name' => $this->files[$handle], 'quiet'=>true, 'get_source'=>true); |
---|
233 | if ( $this->smarty->_fetch_resource_info($params) ) |
---|
234 | { |
---|
235 | if (!preg_match('~{(/(if|section|foreach))|\$[a-zA-Z_]+}~', @$params['source_content']) ) |
---|
236 | $is_new = false; |
---|
237 | } |
---|
238 | |
---|
239 | if ($is_new) |
---|
240 | { |
---|
241 | $this->smarty->assign( 'ROOT_URL', get_root_url() ); |
---|
242 | $this->smarty->assign( 'TAG_INPUT_ENABLED', |
---|
243 | ((is_adviser()) ? 'disabled="disabled" onclick="return false;"' : '')); |
---|
244 | $v = $this->smarty->fetch($this->files[$handle], null, null, false); |
---|
245 | } |
---|
246 | else |
---|
247 | { |
---|
248 | $this->_old->assign_vars(array('TAG_INPUT_ENABLED' => |
---|
249 | ((is_adviser()) ? 'disabled onclick="return false;"' : ''))); |
---|
250 | $this->_old->set_filename( $handle, $this->files[$handle] ); |
---|
251 | $v = $this->_old->parse($handle, true); |
---|
252 | } |
---|
253 | if ($return) |
---|
254 | { |
---|
255 | return $v; |
---|
256 | } |
---|
257 | $this->output .= $v; |
---|
258 | } |
---|
259 | |
---|
260 | /** |
---|
261 | * Load the file for the handle, eventually compile the file and run the compiled |
---|
262 | * code. This will print out the results of executing the template. |
---|
263 | */ |
---|
264 | function pparse($handle) |
---|
265 | { |
---|
266 | $this->parse($handle, false); |
---|
267 | echo $this->output; |
---|
268 | $this->output=''; |
---|
269 | |
---|
270 | } |
---|
271 | |
---|
272 | |
---|
273 | /** flushes the output */ |
---|
274 | function p() |
---|
275 | { |
---|
276 | $start = get_moment(); |
---|
277 | |
---|
278 | echo $this->output; |
---|
279 | $this->output=''; |
---|
280 | |
---|
281 | if ($this->smarty->debugging) |
---|
282 | { |
---|
283 | global $t2; |
---|
284 | $this->smarty->assign( |
---|
285 | array( |
---|
286 | 'AAAA_DEBUG_OUTPUT_TIME__' => get_elapsed_time($start, get_moment()), |
---|
287 | 'AAAA_DEBUG_TOTAL_TIME__' => get_elapsed_time($t2, get_moment()) |
---|
288 | ) |
---|
289 | ); |
---|
290 | require_once(SMARTY_CORE_DIR . 'core.display_debug_console.php'); |
---|
291 | echo smarty_core_display_debug_console(null, $this->smarty); |
---|
292 | } |
---|
293 | } |
---|
294 | |
---|
295 | /** |
---|
296 | * Root-level variable concatenation. Appends a string to an existing |
---|
297 | * variable assignment with the same name. |
---|
298 | */ |
---|
299 | function concat_var($tpl_var, $value) |
---|
300 | { |
---|
301 | $old_val = & $this->smarty->get_template_vars($tpl_var); |
---|
302 | if ( isset($old_val) ) |
---|
303 | { |
---|
304 | $old_val .= $value; |
---|
305 | $this->_old->concat_var( $tpl_var, $value ); |
---|
306 | } |
---|
307 | else |
---|
308 | { |
---|
309 | $this->assign($tpl_var, $value); |
---|
310 | } |
---|
311 | } |
---|
312 | |
---|
313 | /** see smarty assign http://www.smarty.net/manual/en/api.assign.php */ |
---|
314 | function assign($tpl_var, $value = null) |
---|
315 | { |
---|
316 | $this->smarty->assign( $tpl_var, $value ); |
---|
317 | |
---|
318 | if ( is_array($tpl_var) ) |
---|
319 | $this->_old->assign_vars( $tpl_var ); |
---|
320 | else |
---|
321 | $this->_old->assign_var( $tpl_var, $value ); |
---|
322 | } |
---|
323 | |
---|
324 | /** see smarty append http://www.smarty.net/manual/en/api.append.php */ |
---|
325 | function append($tpl_var, $value=null, $merge=false) |
---|
326 | { |
---|
327 | $this->smarty->append( $tpl_var, $value, $merge ); |
---|
328 | } |
---|
329 | |
---|
330 | /** see smarty get_template_vars http://www.smarty.net/manual/en/api.get_template_vars.php */ |
---|
331 | function &get_template_vars($name=null) |
---|
332 | { |
---|
333 | return $this->smarty->get_template_vars( $name ); |
---|
334 | } |
---|
335 | |
---|
336 | /** see smarty append http://www.smarty.net/manual/en/api.clear_assign.php */ |
---|
337 | function clear_assign($tpl_var) |
---|
338 | { |
---|
339 | $this->smarty->clear_assign( $tpl_var ); |
---|
340 | } |
---|
341 | |
---|
342 | /*static*/ function fn_l10n($params, &$smarty) |
---|
343 | { |
---|
344 | return l10n($params['t']); |
---|
345 | } |
---|
346 | |
---|
347 | /** |
---|
348 | * translate variable modifiers - translates a text to the currently loaded |
---|
349 | * language |
---|
350 | */ |
---|
351 | /*static*/ function mod_translate($text) |
---|
352 | { |
---|
353 | return l10n($text); |
---|
354 | } |
---|
355 | } |
---|
356 | |
---|
357 | /** |
---|
358 | * This class contains basic functions that can be called directly from the |
---|
359 | * templates in the form $pwg->l10n('edit') |
---|
360 | */ |
---|
361 | class PwgTemplateAdapter |
---|
362 | { |
---|
363 | function l10n($text) |
---|
364 | { |
---|
365 | return l10n($text); |
---|
366 | } |
---|
367 | |
---|
368 | function l10n_dec($s, $p, $v) |
---|
369 | { |
---|
370 | return l10n_dec($s, $p, $v); |
---|
371 | } |
---|
372 | |
---|
373 | function sprintf() |
---|
374 | { |
---|
375 | $args = func_get_args(); |
---|
376 | return call_user_func_array('sprintf', $args ); |
---|
377 | } |
---|
378 | } |
---|
379 | |
---|
380 | ?> |
---|