1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based picture gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008-2010 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(PHPWG_ROOT_PATH.'include/smarty/libs/Smarty.class.php'); |
---|
26 | |
---|
27 | |
---|
28 | class Template { |
---|
29 | |
---|
30 | var $smarty; |
---|
31 | |
---|
32 | var $output = ''; |
---|
33 | |
---|
34 | // Hash of filenames for each template handle. |
---|
35 | var $files = array(); |
---|
36 | |
---|
37 | // Template extents filenames for each template handle. |
---|
38 | var $extents = array(); |
---|
39 | |
---|
40 | // Templates prefilter from external sources (plugins) |
---|
41 | var $external_filters = array(); |
---|
42 | |
---|
43 | // used by html_head smarty block to add content before </head> |
---|
44 | var $html_head_elements = array(); |
---|
45 | |
---|
46 | const COMBINED_SCRIPTS_TAG = '<!-- COMBINED_SCRIPTS -->'; |
---|
47 | var $scriptLoader; |
---|
48 | |
---|
49 | const COMBINED_CSS_TAG = '<!-- COMBINED_CSS -->'; |
---|
50 | var $css_by_priority = array(); |
---|
51 | |
---|
52 | function Template($root = ".", $theme= "", $path = "template") |
---|
53 | { |
---|
54 | global $conf, $lang_info; |
---|
55 | |
---|
56 | $this->scriptLoader = new ScriptLoader; |
---|
57 | $this->smarty = new Smarty; |
---|
58 | $this->smarty->debugging = $conf['debug_template']; |
---|
59 | $this->smarty->compile_check = $conf['template_compile_check']; |
---|
60 | $this->smarty->force_compile = $conf['template_force_compile']; |
---|
61 | |
---|
62 | if (!isset($conf['local_data_dir_checked'])) |
---|
63 | { |
---|
64 | mkgetdir($conf['local_data_dir'], MKGETDIR_DEFAULT&~MKGETDIR_DIE_ON_ERROR); |
---|
65 | if (!is_writable($conf['local_data_dir'])) |
---|
66 | { |
---|
67 | load_language('admin.lang'); |
---|
68 | fatal_error( |
---|
69 | sprintf( |
---|
70 | l10n('Give write access (chmod 777) to "%s" directory at the root of your Piwigo installation'), |
---|
71 | basename($conf['local_data_dir']) |
---|
72 | ), |
---|
73 | l10n('an error happened'), |
---|
74 | false // show trace |
---|
75 | ); |
---|
76 | } |
---|
77 | if (function_exists('pwg_query')) { |
---|
78 | conf_update_param('local_data_dir_checked', 'true'); |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | $compile_dir = $conf['local_data_dir'].'/templates_c'; |
---|
83 | mkgetdir( $compile_dir ); |
---|
84 | |
---|
85 | $this->smarty->compile_dir = $compile_dir; |
---|
86 | |
---|
87 | $this->smarty->assign_by_ref( 'pwg', new PwgTemplateAdapter() ); |
---|
88 | $this->smarty->register_modifier( 'translate', array('Template', 'mod_translate') ); |
---|
89 | $this->smarty->register_modifier( 'explode', array('Template', 'mod_explode') ); |
---|
90 | $this->smarty->register_modifier( 'get_extent', array(&$this, 'get_extent') ); |
---|
91 | $this->smarty->register_block('html_head', array(&$this, 'block_html_head') ); |
---|
92 | $this->smarty->register_function('combine_script', array(&$this, 'func_combine_script') ); |
---|
93 | $this->smarty->register_function('get_combined_scripts', array(&$this, 'func_get_combined_scripts') ); |
---|
94 | $this->smarty->register_function('combine_css', array(&$this, 'func_combine_css') ); |
---|
95 | $this->smarty->register_compiler_function('get_combined_css', array(&$this, 'func_get_combined_css') ); |
---|
96 | $this->smarty->register_block('footer_script', array(&$this, 'block_footer_script') ); |
---|
97 | $this->smarty->register_function('known_script', array(&$this, 'func_known_script') ); |
---|
98 | $this->smarty->register_prefilter( array('Template', 'prefilter_white_space') ); |
---|
99 | if ( $conf['compiled_template_cache_language'] ) |
---|
100 | { |
---|
101 | $this->smarty->register_prefilter( array('Template', 'prefilter_language') ); |
---|
102 | } |
---|
103 | |
---|
104 | $this->smarty->template_dir = array(); |
---|
105 | if ( !empty($theme) ) |
---|
106 | { |
---|
107 | $this->set_theme($root, $theme, $path); |
---|
108 | $this->set_prefilter( 'header', array('Template', 'prefilter_local_css') ); |
---|
109 | } |
---|
110 | else |
---|
111 | $this->set_template_dir($root); |
---|
112 | |
---|
113 | $this->smarty->assign('lang_info', $lang_info); |
---|
114 | |
---|
115 | if (!defined('IN_ADMIN') and isset($conf['extents_for_templates'])) |
---|
116 | { |
---|
117 | $tpl_extents = unserialize($conf['extents_for_templates']); |
---|
118 | $this->set_extents($tpl_extents, './template-extension/', true, $theme); |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * Load theme's parameters. |
---|
124 | */ |
---|
125 | function set_theme($root, $theme, $path, $load_css=true, $load_local_head=true) |
---|
126 | { |
---|
127 | $this->set_template_dir($root.'/'.$theme.'/'.$path); |
---|
128 | |
---|
129 | $themeconf = $this->load_themeconf($root.'/'.$theme); |
---|
130 | |
---|
131 | if (isset($themeconf['parent']) and $themeconf['parent'] != $theme) |
---|
132 | { |
---|
133 | $this->set_theme( |
---|
134 | $root, |
---|
135 | $themeconf['parent'], |
---|
136 | $path, |
---|
137 | isset($themeconf['load_parent_css']) ? $themeconf['load_parent_css'] : $load_css, |
---|
138 | isset($themeconf['load_parent_local_head']) ? $themeconf['load_parent_local_head'] : $load_local_head |
---|
139 | ); |
---|
140 | } |
---|
141 | |
---|
142 | $tpl_var = array( |
---|
143 | 'id' => $theme, |
---|
144 | 'load_css' => $load_css, |
---|
145 | ); |
---|
146 | if (!empty($themeconf['local_head']) and $load_local_head) |
---|
147 | { |
---|
148 | $tpl_var['local_head'] = realpath($root.'/'.$theme.'/'.$themeconf['local_head'] ); |
---|
149 | } |
---|
150 | $themeconf['id'] = $theme; |
---|
151 | $this->smarty->append('themes', $tpl_var); |
---|
152 | $this->smarty->append('themeconf', $themeconf, true); |
---|
153 | } |
---|
154 | |
---|
155 | /** |
---|
156 | * Add template directory for this Template object. |
---|
157 | * Set compile id if not exists. |
---|
158 | */ |
---|
159 | function set_template_dir($dir) |
---|
160 | { |
---|
161 | $this->smarty->template_dir[] = $dir; |
---|
162 | |
---|
163 | if (!isset($this->smarty->compile_id)) |
---|
164 | { |
---|
165 | $real_dir = realpath($dir); |
---|
166 | $compile_id = crc32( $real_dir===false ? $dir : $real_dir); |
---|
167 | $this->smarty->compile_id = base_convert($compile_id, 10, 36 ); |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | /** |
---|
172 | * Gets the template root directory for this Template object. |
---|
173 | */ |
---|
174 | function get_template_dir() |
---|
175 | { |
---|
176 | return $this->smarty->template_dir; |
---|
177 | } |
---|
178 | |
---|
179 | /** |
---|
180 | * Deletes all compiled templates. |
---|
181 | */ |
---|
182 | function delete_compiled_templates() |
---|
183 | { |
---|
184 | $save_compile_id = $this->smarty->compile_id; |
---|
185 | $this->smarty->compile_id = null; |
---|
186 | $this->smarty->clear_compiled_tpl(); |
---|
187 | $this->smarty->compile_id = $save_compile_id; |
---|
188 | file_put_contents($this->smarty->compile_dir.'/index.htm', 'Not allowed!'); |
---|
189 | FileCombiner::clear_combined_files(); |
---|
190 | } |
---|
191 | |
---|
192 | function get_themeconf($val) |
---|
193 | { |
---|
194 | $tc = $this->smarty->get_template_vars('themeconf'); |
---|
195 | return isset($tc[$val]) ? $tc[$val] : ''; |
---|
196 | } |
---|
197 | |
---|
198 | /** |
---|
199 | * Sets the template filename for handle. |
---|
200 | */ |
---|
201 | function set_filename($handle, $filename) |
---|
202 | { |
---|
203 | return $this->set_filenames( array($handle=>$filename) ); |
---|
204 | } |
---|
205 | |
---|
206 | /** |
---|
207 | * Sets the template filenames for handles. $filename_array should be a |
---|
208 | * hash of handle => filename pairs. |
---|
209 | */ |
---|
210 | function set_filenames($filename_array) |
---|
211 | { |
---|
212 | if (!is_array($filename_array)) |
---|
213 | { |
---|
214 | return false; |
---|
215 | } |
---|
216 | reset($filename_array); |
---|
217 | while(list($handle, $filename) = each($filename_array)) |
---|
218 | { |
---|
219 | if (is_null($filename)) |
---|
220 | { |
---|
221 | unset($this->files[$handle]); |
---|
222 | } |
---|
223 | else |
---|
224 | { |
---|
225 | $this->files[$handle] = $this->get_extent($filename, $handle); |
---|
226 | } |
---|
227 | } |
---|
228 | return true; |
---|
229 | } |
---|
230 | |
---|
231 | /** |
---|
232 | * Sets template extention filename for handles. |
---|
233 | */ |
---|
234 | function set_extent($filename, $param, $dir='', $overwrite=true, $theme='N/A') |
---|
235 | { |
---|
236 | return $this->set_extents(array($filename => $param), $dir, $overwrite); |
---|
237 | } |
---|
238 | |
---|
239 | /** |
---|
240 | * Sets template extentions filenames for handles. |
---|
241 | * $filename_array should be an hash of filename => array( handle, param) or filename => handle |
---|
242 | */ |
---|
243 | function set_extents($filename_array, $dir='', $overwrite=true, $theme='N/A') |
---|
244 | { |
---|
245 | if (!is_array($filename_array)) |
---|
246 | { |
---|
247 | return false; |
---|
248 | } |
---|
249 | foreach ($filename_array as $filename => $value) |
---|
250 | { |
---|
251 | if (is_array($value)) |
---|
252 | { |
---|
253 | $handle = $value[0]; |
---|
254 | $param = $value[1]; |
---|
255 | $thm = $value[2]; |
---|
256 | } |
---|
257 | elseif (is_string($value)) |
---|
258 | { |
---|
259 | $handle = $value; |
---|
260 | $param = 'N/A'; |
---|
261 | $thm = 'N/A'; |
---|
262 | } |
---|
263 | else |
---|
264 | { |
---|
265 | return false; |
---|
266 | } |
---|
267 | |
---|
268 | if ((stripos(implode('',array_keys($_GET)), '/'.$param) !== false or $param == 'N/A') |
---|
269 | and ($thm == $theme or $thm == 'N/A') |
---|
270 | and (!isset($this->extents[$handle]) or $overwrite) |
---|
271 | and file_exists($dir . $filename)) |
---|
272 | { |
---|
273 | $this->extents[$handle] = realpath($dir . $filename); |
---|
274 | } |
---|
275 | } |
---|
276 | return true; |
---|
277 | } |
---|
278 | |
---|
279 | /** return template extension if exists */ |
---|
280 | function get_extent($filename='', $handle='') |
---|
281 | { |
---|
282 | if (isset($this->extents[$handle])) |
---|
283 | { |
---|
284 | $filename = $this->extents[$handle]; |
---|
285 | } |
---|
286 | return $filename; |
---|
287 | } |
---|
288 | |
---|
289 | /** see smarty assign http://www.smarty.net/manual/en/api.assign.php */ |
---|
290 | function assign($tpl_var, $value = null) |
---|
291 | { |
---|
292 | $this->smarty->assign( $tpl_var, $value ); |
---|
293 | } |
---|
294 | |
---|
295 | /** |
---|
296 | * Inserts the uncompiled code for $handle as the value of $varname in the |
---|
297 | * root-level. This can be used to effectively include a template in the |
---|
298 | * middle of another template. |
---|
299 | * This is equivalent to assign($varname, $this->parse($handle, true)) |
---|
300 | */ |
---|
301 | function assign_var_from_handle($varname, $handle) |
---|
302 | { |
---|
303 | $this->assign($varname, $this->parse($handle, true)); |
---|
304 | return true; |
---|
305 | } |
---|
306 | |
---|
307 | /** see smarty append http://www.smarty.net/manual/en/api.append.php */ |
---|
308 | function append($tpl_var, $value=null, $merge=false) |
---|
309 | { |
---|
310 | $this->smarty->append( $tpl_var, $value, $merge ); |
---|
311 | } |
---|
312 | |
---|
313 | /** |
---|
314 | * Root-level variable concatenation. Appends a string to an existing |
---|
315 | * variable assignment with the same name. |
---|
316 | */ |
---|
317 | function concat($tpl_var, $value) |
---|
318 | { |
---|
319 | $old_val = & $this->smarty->get_template_vars($tpl_var); |
---|
320 | if ( isset($old_val) ) |
---|
321 | { |
---|
322 | $old_val .= $value; |
---|
323 | } |
---|
324 | else |
---|
325 | { |
---|
326 | $this->assign($tpl_var, $value); |
---|
327 | } |
---|
328 | } |
---|
329 | |
---|
330 | /** see smarty append http://www.smarty.net/manual/en/api.clear_assign.php */ |
---|
331 | function clear_assign($tpl_var) |
---|
332 | { |
---|
333 | $this->smarty->clear_assign( $tpl_var ); |
---|
334 | } |
---|
335 | |
---|
336 | /** see smarty get_template_vars http://www.smarty.net/manual/en/api.get_template_vars.php */ |
---|
337 | function &get_template_vars($name=null) |
---|
338 | { |
---|
339 | return $this->smarty->get_template_vars( $name ); |
---|
340 | } |
---|
341 | |
---|
342 | |
---|
343 | /** |
---|
344 | * Load the file for the handle, eventually compile the file and run the compiled |
---|
345 | * code. This will add the output to the results or return the result if $return |
---|
346 | * is true. |
---|
347 | */ |
---|
348 | function parse($handle, $return=false) |
---|
349 | { |
---|
350 | if ( !isset($this->files[$handle]) ) |
---|
351 | { |
---|
352 | fatal_error("Template->parse(): Couldn't load template file for handle $handle"); |
---|
353 | } |
---|
354 | |
---|
355 | $this->smarty->assign( 'ROOT_URL', get_root_url() ); |
---|
356 | |
---|
357 | $save_compile_id = $this->smarty->compile_id; |
---|
358 | $this->load_external_filters($handle); |
---|
359 | |
---|
360 | global $conf, $lang_info; |
---|
361 | if ( $conf['compiled_template_cache_language'] and isset($lang_info['code']) ) |
---|
362 | { |
---|
363 | $this->smarty->compile_id .= '.'.$lang_info['code']; |
---|
364 | } |
---|
365 | |
---|
366 | $v = $this->smarty->fetch($this->files[$handle], null, null, false); |
---|
367 | |
---|
368 | $this->smarty->compile_id = $save_compile_id; |
---|
369 | $this->unload_external_filters($handle); |
---|
370 | |
---|
371 | if ($return) |
---|
372 | { |
---|
373 | return $v; |
---|
374 | } |
---|
375 | $this->output .= $v; |
---|
376 | } |
---|
377 | |
---|
378 | /** |
---|
379 | * Load the file for the handle, eventually compile the file and run the compiled |
---|
380 | * code. This will print out the results of executing the template. |
---|
381 | */ |
---|
382 | function pparse($handle) |
---|
383 | { |
---|
384 | $this->parse($handle, false); |
---|
385 | $this->flush(); |
---|
386 | } |
---|
387 | |
---|
388 | function flush() |
---|
389 | { |
---|
390 | if (!$this->scriptLoader->did_head()) |
---|
391 | { |
---|
392 | $pos = strpos( $this->output, self::COMBINED_SCRIPTS_TAG ); |
---|
393 | if ($pos !== false) |
---|
394 | { |
---|
395 | $scripts = $this->scriptLoader->get_head_scripts(); |
---|
396 | $content = array(); |
---|
397 | foreach ($scripts as $script) |
---|
398 | { |
---|
399 | $content[]= |
---|
400 | '<script type="text/javascript" src="' |
---|
401 | . self::make_script_src($script) |
---|
402 | .'"></script>'; |
---|
403 | } |
---|
404 | |
---|
405 | $this->output = substr_replace( $this->output, "\n".implode( "\n", $content ), $pos, strlen(self::COMBINED_SCRIPTS_TAG) ); |
---|
406 | } //else maybe error or warning ? |
---|
407 | } |
---|
408 | |
---|
409 | if(!empty($this->css_by_priority)) |
---|
410 | { |
---|
411 | ksort($this->css_by_priority); |
---|
412 | |
---|
413 | global $conf; |
---|
414 | $css = array(); |
---|
415 | if ($conf['template_combine_files']) |
---|
416 | { |
---|
417 | $combiner = new FileCombiner('css'); |
---|
418 | foreach ($this->css_by_priority as $files) |
---|
419 | { |
---|
420 | foreach ($files as $file_ver) |
---|
421 | $combiner->add( $file_ver[0], $file_ver[1] ); |
---|
422 | } |
---|
423 | if ( $combiner->combine( $out_file, $out_version) ) |
---|
424 | $css[] = array($out_file, $out_version); |
---|
425 | } |
---|
426 | else |
---|
427 | { |
---|
428 | foreach ($this->css_by_priority as $files) |
---|
429 | $css = array_merge($css, $files); |
---|
430 | } |
---|
431 | |
---|
432 | $content = array(); |
---|
433 | foreach( $css as $file_ver ) |
---|
434 | { |
---|
435 | $href = get_root_url() . $file_ver[0]; |
---|
436 | if ($file_ver[1] !== false) |
---|
437 | $href .= '?v' . ($file_ver[1] ? $file_ver[1] : PHPWG_VERSION); |
---|
438 | // trigger the event for eventual use of a cdn |
---|
439 | $href = trigger_event('combined_css', $href, $file_ver[0], $file_ver[1]); |
---|
440 | $content[] = '<link rel="stylesheet" type="text/css" href="'.$href.'">'; |
---|
441 | } |
---|
442 | $this->output = str_replace(self::COMBINED_CSS_TAG, |
---|
443 | implode( "\n", $content ), |
---|
444 | $this->output ); |
---|
445 | $this->css_by_priority = array(); |
---|
446 | } |
---|
447 | |
---|
448 | if ( count($this->html_head_elements) ) |
---|
449 | { |
---|
450 | $search = "\n</head>"; |
---|
451 | $pos = strpos( $this->output, $search ); |
---|
452 | if ($pos !== false) |
---|
453 | { |
---|
454 | $this->output = substr_replace( $this->output, "\n".implode( "\n", $this->html_head_elements ), $pos, 0 ); |
---|
455 | } //else maybe error or warning ? |
---|
456 | $this->html_head_elements = array(); |
---|
457 | } |
---|
458 | |
---|
459 | echo $this->output; |
---|
460 | $this->output=''; |
---|
461 | } |
---|
462 | |
---|
463 | /** flushes the output */ |
---|
464 | function p() |
---|
465 | { |
---|
466 | $this->flush(); |
---|
467 | |
---|
468 | if ($this->smarty->debugging) |
---|
469 | { |
---|
470 | global $t2; |
---|
471 | $this->smarty->assign( |
---|
472 | array( |
---|
473 | 'AAAA_DEBUG_TOTAL_TIME__' => get_elapsed_time($t2, get_moment()) |
---|
474 | ) |
---|
475 | ); |
---|
476 | require_once(SMARTY_CORE_DIR . 'core.display_debug_console.php'); |
---|
477 | echo smarty_core_display_debug_console(null, $this->smarty); |
---|
478 | } |
---|
479 | } |
---|
480 | |
---|
481 | /** |
---|
482 | * translate variable modifier - translates a text to the currently loaded |
---|
483 | * language |
---|
484 | */ |
---|
485 | static function mod_translate($text) |
---|
486 | { |
---|
487 | return l10n($text); |
---|
488 | } |
---|
489 | |
---|
490 | /** |
---|
491 | * explode variable modifier - similar to php explode |
---|
492 | * 'Yes;No'|@explode:';' -> array('Yes', 'No') |
---|
493 | */ |
---|
494 | static function mod_explode($text, $delimiter=',') |
---|
495 | { |
---|
496 | return explode($delimiter, $text); |
---|
497 | } |
---|
498 | |
---|
499 | /** |
---|
500 | * This smarty "html_head" block allows to add content just before |
---|
501 | * </head> element in the output after the head has been parsed. This is |
---|
502 | * handy in order to respect strict standards when <style> and <link> |
---|
503 | * html elements must appear in the <head> element |
---|
504 | */ |
---|
505 | function block_html_head($params, $content, &$smarty, &$repeat) |
---|
506 | { |
---|
507 | $content = trim($content); |
---|
508 | if ( !empty($content) ) |
---|
509 | { // second call |
---|
510 | $this->html_head_elements[] = $content; |
---|
511 | } |
---|
512 | } |
---|
513 | |
---|
514 | /** |
---|
515 | * This smarty "known_script" functions allows to insert well known java scripts |
---|
516 | * such as prototype, jquery, etc... only once. Examples: |
---|
517 | * {known_script id="jquery" src="{$ROOT_URL}template-common/lib/jquery.packed.js"} |
---|
518 | */ |
---|
519 | function func_known_script($params, &$smarty ) |
---|
520 | { |
---|
521 | if (!isset($params['id'])) |
---|
522 | { |
---|
523 | $smarty->trigger_error("known_script: missing 'id' parameter"); |
---|
524 | return; |
---|
525 | } |
---|
526 | $id = $params['id']; |
---|
527 | trigger_error("known_script is deprecated $id ".@$params['src'], E_USER_WARNING); |
---|
528 | if ('jquery'==$id) |
---|
529 | { |
---|
530 | $this->scriptLoader->add($id, 0, array(), null); |
---|
531 | return; |
---|
532 | } |
---|
533 | if (! isset( $this->known_scripts[$id] ) ) |
---|
534 | { |
---|
535 | if (!isset($params['src'])) |
---|
536 | { |
---|
537 | $smarty->trigger_error("known_script: missing 'src' parameter"); |
---|
538 | return; |
---|
539 | } |
---|
540 | $this->known_scripts[$id] = $params['src']; |
---|
541 | $content = '<script type="text/javascript" src="'.$params['src'].'"></script>'; |
---|
542 | if (isset($params['now']) and $params['now'] and empty($this->output) ) |
---|
543 | { |
---|
544 | return $content; |
---|
545 | } |
---|
546 | $repeat = false; |
---|
547 | $this->block_html_head(null, $content, $smarty, $repeat); |
---|
548 | } |
---|
549 | } |
---|
550 | |
---|
551 | function func_combine_script($params, &$smarty) |
---|
552 | { |
---|
553 | if (!isset($params['id'])) |
---|
554 | { |
---|
555 | $smarty->trigger_error("combine_script: missing 'id' parameter", E_USER_ERROR); |
---|
556 | } |
---|
557 | $load = 0; |
---|
558 | if (isset($params['load'])) |
---|
559 | { |
---|
560 | switch ($params['load']) |
---|
561 | { |
---|
562 | case 'header': break; |
---|
563 | case 'footer': $load=1; break; |
---|
564 | case 'async': $load=2; break; |
---|
565 | default: $smarty->trigger_error("combine_script: invalid 'load' parameter", E_USER_ERROR); |
---|
566 | } |
---|
567 | } |
---|
568 | $this->scriptLoader->add( $params['id'], $load, |
---|
569 | empty($params['require']) ? array() : explode( ',', $params['require'] ), |
---|
570 | @$params['path'], |
---|
571 | isset($params['version']) ? $params['version'] : 0 ); |
---|
572 | } |
---|
573 | |
---|
574 | |
---|
575 | function func_get_combined_scripts($params, &$smarty) |
---|
576 | { |
---|
577 | if (!isset($params['load'])) |
---|
578 | { |
---|
579 | $smarty->trigger_error("get_combined_scripts: missing 'load' parameter", E_USER_ERROR); |
---|
580 | } |
---|
581 | $load = $params['load']=='header' ? 0 : 1; |
---|
582 | $content = array(); |
---|
583 | |
---|
584 | if ($load==0) |
---|
585 | { |
---|
586 | return self::COMBINED_SCRIPTS_TAG; |
---|
587 | } |
---|
588 | else |
---|
589 | { |
---|
590 | $scripts = $this->scriptLoader->get_footer_scripts(); |
---|
591 | foreach ($scripts[0] as $script) |
---|
592 | { |
---|
593 | $content[]= |
---|
594 | '<script type="text/javascript" src="' |
---|
595 | . self::make_script_src($script) |
---|
596 | .'"></script>'; |
---|
597 | } |
---|
598 | if (count($this->scriptLoader->inline_scripts)) |
---|
599 | { |
---|
600 | $content[]= '<script type="text/javascript">//<![CDATA[ |
---|
601 | '; |
---|
602 | $content = array_merge($content, $this->scriptLoader->inline_scripts); |
---|
603 | $content[]= '//]]></script>'; |
---|
604 | } |
---|
605 | |
---|
606 | if (count($scripts[1])) |
---|
607 | { |
---|
608 | $content[]= '<script type="text/javascript">'; |
---|
609 | $content[]= '(function() { |
---|
610 | var after = document.getElementsByTagName(\'script\')[document.getElementsByTagName(\'script\').length-1]; |
---|
611 | var s;'; |
---|
612 | foreach ($scripts[1] as $id => $script) |
---|
613 | { |
---|
614 | $content[]= |
---|
615 | 's=document.createElement(\'script\'); s.type=\'text/javascript\'; s.async=true; s.src=\'' |
---|
616 | . self::make_script_src($script) |
---|
617 | .'\';'; |
---|
618 | $content[]= 'after = after.parentNode.insertBefore(s, after);'; |
---|
619 | } |
---|
620 | $content[]= '})();'; |
---|
621 | $content[]= '</script>'; |
---|
622 | } |
---|
623 | } |
---|
624 | return implode("\n", $content); |
---|
625 | } |
---|
626 | |
---|
627 | |
---|
628 | private static function make_script_src( $script ) |
---|
629 | { |
---|
630 | $ret = ''; |
---|
631 | if ( $script->is_remote() ) |
---|
632 | $ret = $script->path; |
---|
633 | else |
---|
634 | { |
---|
635 | $ret = get_root_url().$script->path; |
---|
636 | if ($script->version!==false) |
---|
637 | { |
---|
638 | $ret.= '?v'. ($script->version ? $script->version : PHPWG_VERSION); |
---|
639 | } |
---|
640 | } |
---|
641 | // trigger the event for eventual use of a cdn |
---|
642 | $ret = trigger_event('combined_script', $ret, $script); |
---|
643 | return $ret; |
---|
644 | } |
---|
645 | |
---|
646 | function block_footer_script($params, $content, &$smarty, &$repeat) |
---|
647 | { |
---|
648 | $content = trim($content); |
---|
649 | if ( !empty($content) ) |
---|
650 | { // second call |
---|
651 | $this->scriptLoader->add_inline( $content, @$params['require'] ); |
---|
652 | } |
---|
653 | } |
---|
654 | |
---|
655 | function func_combine_css($params, &$smarty) |
---|
656 | { |
---|
657 | !empty($params['path']) || fatal_error('combine_css missing path'); |
---|
658 | $order = (int)@$params['order']; |
---|
659 | $version = isset($params['version']) ? $params['version'] : 0; |
---|
660 | $this->css_by_priority[$order][] = array( $params['path'], $version); |
---|
661 | } |
---|
662 | |
---|
663 | function func_get_combined_css($params, &$smarty) |
---|
664 | { |
---|
665 | return 'echo '.var_export(self::COMBINED_CSS_TAG,true); |
---|
666 | } |
---|
667 | |
---|
668 | |
---|
669 | /** |
---|
670 | * This function allows to declare a Smarty prefilter from a plugin, thus allowing |
---|
671 | * it to modify template source before compilation and without changing core files |
---|
672 | * They will be processed by weight ascending. |
---|
673 | * http://www.smarty.net/manual/en/advanced.features.prefilters.php |
---|
674 | */ |
---|
675 | function set_prefilter($handle, $callback, $weight=50) |
---|
676 | { |
---|
677 | $this->external_filters[$handle][$weight][] = array('prefilter', $callback); |
---|
678 | ksort($this->external_filters[$handle]); |
---|
679 | } |
---|
680 | |
---|
681 | function set_postfilter($handle, $callback, $weight=50) |
---|
682 | { |
---|
683 | $this->external_filters[$handle][$weight][] = array('postfilter', $callback); |
---|
684 | ksort($this->external_filters[$handle]); |
---|
685 | } |
---|
686 | |
---|
687 | function set_outputfilter($handle, $callback, $weight=50) |
---|
688 | { |
---|
689 | $this->external_filters[$handle][$weight][] = array('outputfilter', $callback); |
---|
690 | ksort($this->external_filters[$handle]); |
---|
691 | } |
---|
692 | |
---|
693 | /** |
---|
694 | * This function actually triggers the filters on the tpl files. |
---|
695 | * Called in the parse method. |
---|
696 | * http://www.smarty.net/manual/en/advanced.features.prefilters.php |
---|
697 | */ |
---|
698 | function load_external_filters($handle) |
---|
699 | { |
---|
700 | if (isset($this->external_filters[$handle])) |
---|
701 | { |
---|
702 | $compile_id = ''; |
---|
703 | foreach ($this->external_filters[$handle] as $filters) |
---|
704 | { |
---|
705 | foreach ($filters as $filter) |
---|
706 | { |
---|
707 | list($type, $callback) = $filter; |
---|
708 | $compile_id .= $type.( is_array($callback) ? implode('', $callback) : $callback ); |
---|
709 | call_user_func(array($this->smarty, 'register_'.$type), $callback); |
---|
710 | } |
---|
711 | } |
---|
712 | $this->smarty->compile_id .= '.'.base_convert(crc32($compile_id), 10, 36); |
---|
713 | } |
---|
714 | } |
---|
715 | |
---|
716 | function unload_external_filters($handle) |
---|
717 | { |
---|
718 | if (isset($this->external_filters[$handle])) |
---|
719 | { |
---|
720 | foreach ($this->external_filters[$handle] as $filters) |
---|
721 | { |
---|
722 | foreach ($filters as $filter) |
---|
723 | { |
---|
724 | list($type, $callback) = $filter; |
---|
725 | call_user_func(array($this->smarty, 'unregister_'.$type), $callback); |
---|
726 | } |
---|
727 | } |
---|
728 | } |
---|
729 | } |
---|
730 | |
---|
731 | static function prefilter_white_space($source, &$smarty) |
---|
732 | { |
---|
733 | $ld = $smarty->left_delimiter; |
---|
734 | $rd = $smarty->right_delimiter; |
---|
735 | $ldq = preg_quote($ld, '#'); |
---|
736 | $rdq = preg_quote($rd, '#'); |
---|
737 | |
---|
738 | $regex = array(); |
---|
739 | $tags = array('if','foreach','section','footer_script'); |
---|
740 | foreach($tags as $tag) |
---|
741 | { |
---|
742 | array_push($regex, "#^[ \t]+($ldq$tag"."[^$ld$rd]*$rdq)\s*$#m"); |
---|
743 | array_push($regex, "#^[ \t]+($ldq/$tag$rdq)\s*$#m"); |
---|
744 | } |
---|
745 | $tags = array('include','else','combine_script','html_head'); |
---|
746 | foreach($tags as $tag) |
---|
747 | { |
---|
748 | array_push($regex, "#^[ \t]+($ldq$tag"."[^$ld$rd]*$rdq)\s*$#m"); |
---|
749 | } |
---|
750 | $source = preg_replace( $regex, "$1", $source); |
---|
751 | return $source; |
---|
752 | } |
---|
753 | |
---|
754 | /** |
---|
755 | * Smarty prefilter to allow caching (whenever possible) language strings |
---|
756 | * from templates. |
---|
757 | */ |
---|
758 | static function prefilter_language($source, &$smarty) |
---|
759 | { |
---|
760 | global $lang; |
---|
761 | $ldq = preg_quote($smarty->left_delimiter, '~'); |
---|
762 | $rdq = preg_quote($smarty->right_delimiter, '~'); |
---|
763 | |
---|
764 | $regex = "~$ldq *\'([^'$]+)\'\|@translate *$rdq~"; |
---|
765 | $source = preg_replace( $regex.'e', 'isset($lang[\'$1\']) ? $lang[\'$1\'] : \'$0\'', $source); |
---|
766 | |
---|
767 | $regex = "~$ldq *\'([^'$]+)\'\|@translate\|~"; |
---|
768 | $source = preg_replace( $regex.'e', 'isset($lang[\'$1\']) ? \'{\'.var_export($lang[\'$1\'],true).\'|\' : \'$0\'', $source); |
---|
769 | |
---|
770 | $regex = "~($ldq *assign +var=.+ +value=)\'([^'$]+)\'\|@translate~e"; |
---|
771 | $source = preg_replace( $regex, 'isset($lang[\'$2\']) ? \'$1\'.var_export($lang[\'$2\'],true) : \'$0\'', $source); |
---|
772 | |
---|
773 | return $source; |
---|
774 | } |
---|
775 | |
---|
776 | static function prefilter_local_css($source, &$smarty) |
---|
777 | { |
---|
778 | $css = array(); |
---|
779 | foreach ($smarty->get_template_vars('themes') as $theme) |
---|
780 | { |
---|
781 | $f = 'local/css/'.$theme['id'].'-rules.css'; |
---|
782 | if (file_exists(PHPWG_ROOT_PATH.$f)) |
---|
783 | { |
---|
784 | array_push($css, "{combine_css path='$f' order=10}"); |
---|
785 | } |
---|
786 | } |
---|
787 | $f = 'local/css/rules.css'; |
---|
788 | if (file_exists(PHPWG_ROOT_PATH.$f)) |
---|
789 | { |
---|
790 | array_push($css, "{combine_css path='$f' order=10}"); |
---|
791 | } |
---|
792 | |
---|
793 | if (!empty($css)) |
---|
794 | { |
---|
795 | $source = str_replace("\n{get_combined_css}", "\n".implode( "\n", $css )."\n{get_combined_css}", $source); |
---|
796 | } |
---|
797 | |
---|
798 | return $source; |
---|
799 | } |
---|
800 | |
---|
801 | function load_themeconf($dir) |
---|
802 | { |
---|
803 | global $themeconfs, $conf; |
---|
804 | |
---|
805 | $dir = realpath($dir); |
---|
806 | if (!isset($themeconfs[$dir])) |
---|
807 | { |
---|
808 | $themeconf = array(); |
---|
809 | include($dir.'/themeconf.inc.php'); |
---|
810 | // Put themeconf in cache |
---|
811 | $themeconfs[$dir] = $themeconf; |
---|
812 | } |
---|
813 | return $themeconfs[$dir]; |
---|
814 | } |
---|
815 | } |
---|
816 | |
---|
817 | |
---|
818 | /** |
---|
819 | * This class contains basic functions that can be called directly from the |
---|
820 | * templates in the form $pwg->l10n('edit') |
---|
821 | */ |
---|
822 | class PwgTemplateAdapter |
---|
823 | { |
---|
824 | function l10n($text) |
---|
825 | { |
---|
826 | return l10n($text); |
---|
827 | } |
---|
828 | |
---|
829 | function l10n_dec($s, $p, $v) |
---|
830 | { |
---|
831 | return l10n_dec($s, $p, $v); |
---|
832 | } |
---|
833 | |
---|
834 | function sprintf() |
---|
835 | { |
---|
836 | $args = func_get_args(); |
---|
837 | return call_user_func_array('sprintf', $args ); |
---|
838 | } |
---|
839 | } |
---|
840 | |
---|
841 | |
---|
842 | final class Script |
---|
843 | { |
---|
844 | public $id; |
---|
845 | public $load_mode; |
---|
846 | public $precedents = array(); |
---|
847 | public $path; |
---|
848 | public $version; |
---|
849 | public $extra = array(); |
---|
850 | |
---|
851 | function Script($load_mode, $id, $path, $version, $precedents) |
---|
852 | { |
---|
853 | $this->id = $id; |
---|
854 | $this->load_mode = $load_mode; |
---|
855 | $this->id = $id; |
---|
856 | $this->set_path($path); |
---|
857 | $this->version = $version; |
---|
858 | $this->precedents = $precedents; |
---|
859 | } |
---|
860 | |
---|
861 | function set_path($path) |
---|
862 | { |
---|
863 | if (!empty($path)) |
---|
864 | $this->path = $path; |
---|
865 | } |
---|
866 | |
---|
867 | function is_remote() |
---|
868 | { |
---|
869 | return url_is_remote( $this->path ); |
---|
870 | } |
---|
871 | } |
---|
872 | |
---|
873 | |
---|
874 | /** Manage a list of required scripts for a page, by optimizing their loading location (head, bottom, async) |
---|
875 | and later on by combining them in a unique file respecting at the same time dependencies.*/ |
---|
876 | class ScriptLoader |
---|
877 | { |
---|
878 | private $registered_scripts; |
---|
879 | public $inline_scripts; |
---|
880 | |
---|
881 | private $did_head; |
---|
882 | private $head_done_scripts; |
---|
883 | |
---|
884 | private static $known_paths = array( |
---|
885 | 'core.scripts' => 'themes/default/js/scripts.js', |
---|
886 | 'jquery' => 'themes/default/js/jquery.min.js', |
---|
887 | 'jquery.ui' => 'themes/default/js/ui/packed/ui.core.packed.js' |
---|
888 | ); |
---|
889 | |
---|
890 | function __construct() |
---|
891 | { |
---|
892 | $this->clear(); |
---|
893 | } |
---|
894 | |
---|
895 | function clear() |
---|
896 | { |
---|
897 | $this->registered_scripts = array(); |
---|
898 | $this->inline_scripts = array(); |
---|
899 | $this->head_done_scripts = array(); |
---|
900 | $this->did_head = false; |
---|
901 | } |
---|
902 | |
---|
903 | function get_all() |
---|
904 | { |
---|
905 | return $this->registered_scripts; |
---|
906 | } |
---|
907 | |
---|
908 | function add_inline($code, $require) |
---|
909 | { |
---|
910 | if(!empty($require)) |
---|
911 | { |
---|
912 | if(!isset($this->registered_scripts[$require])) |
---|
913 | fatal_error("inline script not found require $require"); |
---|
914 | $s = $this->registered_scripts[$require]; |
---|
915 | if($s->load_mode==2) |
---|
916 | $s->load_mode=1; // until now the implementation does not allow executing inline script depending on another async script |
---|
917 | } |
---|
918 | $this->inline_scripts[] = $code; |
---|
919 | } |
---|
920 | |
---|
921 | function add($id, $load_mode, $require, $path, $version=0) |
---|
922 | { |
---|
923 | if ($this->did_head && $load_mode==0 ) |
---|
924 | { |
---|
925 | trigger_error("Attempt to add a new script $id but the head has been written", E_USER_WARNING); |
---|
926 | } |
---|
927 | if (! isset( $this->registered_scripts[$id] ) ) |
---|
928 | { |
---|
929 | $script = new Script($load_mode, $id, $path, $version, $require); |
---|
930 | self::fill_well_known($id, $script); |
---|
931 | $this->registered_scripts[$id] = $script; |
---|
932 | } |
---|
933 | else |
---|
934 | { |
---|
935 | $script = & $this->registered_scripts[$id]; |
---|
936 | if (count($require)) |
---|
937 | { |
---|
938 | $script->precedents = array_unique( array_merge($script->precedents, $require) ); |
---|
939 | } |
---|
940 | $script->set_path($path); |
---|
941 | if ($version && version_compare($script->version, $version)<0 ) |
---|
942 | $script->version = $version; |
---|
943 | if ($load_mode < $script->load_mode) |
---|
944 | $script->load_mode = $load_mode; |
---|
945 | } |
---|
946 | } |
---|
947 | |
---|
948 | function did_head() |
---|
949 | { |
---|
950 | return $this->did_head; |
---|
951 | } |
---|
952 | |
---|
953 | function get_head_scripts() |
---|
954 | { |
---|
955 | self::check_load_dep($this->registered_scripts); |
---|
956 | foreach( array_keys($this->registered_scripts) as $id ) |
---|
957 | { |
---|
958 | $this->compute_script_topological_order($id); |
---|
959 | } |
---|
960 | |
---|
961 | uasort($this->registered_scripts, array('ScriptLoader', 'cmp_by_mode_and_order')); |
---|
962 | |
---|
963 | foreach( $this->registered_scripts as $id => $script) |
---|
964 | { |
---|
965 | if ($script->load_mode > 0) |
---|
966 | break; |
---|
967 | if ( !empty($script->path) ) |
---|
968 | $this->head_done_scripts[$id] = $script; |
---|
969 | else |
---|
970 | trigger_error("Script $id has an undefined path", E_USER_WARNING); |
---|
971 | } |
---|
972 | $this->did_head = true; |
---|
973 | return self::do_combine($this->head_done_scripts, 0); |
---|
974 | } |
---|
975 | |
---|
976 | function get_footer_scripts() |
---|
977 | { |
---|
978 | $todo = array(); |
---|
979 | foreach( $this->registered_scripts as $id => $script) |
---|
980 | { |
---|
981 | if (!isset($this->head_done_scripts[$id])) |
---|
982 | { |
---|
983 | $todo[$id] = $script; |
---|
984 | } |
---|
985 | } |
---|
986 | |
---|
987 | foreach( array_keys($todo) as $id ) |
---|
988 | { |
---|
989 | $this->compute_script_topological_order($id); |
---|
990 | } |
---|
991 | |
---|
992 | uasort($todo, array('ScriptLoader', 'cmp_by_mode_and_order')); |
---|
993 | |
---|
994 | $result = array( array(), array() ); |
---|
995 | foreach( $todo as $id => $script) |
---|
996 | { |
---|
997 | $result[$script->load_mode-1][$id] = $script; |
---|
998 | } |
---|
999 | return array( self::do_combine($result[0],1), self::do_combine($result[1],2) ); |
---|
1000 | } |
---|
1001 | |
---|
1002 | private static function do_combine($scripts, $load_mode) |
---|
1003 | { |
---|
1004 | global $conf; |
---|
1005 | if (count($scripts)<2 or !$conf['template_combine_files']) |
---|
1006 | return $scripts; |
---|
1007 | $combiner = new FileCombiner('js'); |
---|
1008 | $result = array(); |
---|
1009 | foreach ($scripts as $script) |
---|
1010 | { |
---|
1011 | if ($script->is_remote()) |
---|
1012 | { |
---|
1013 | if ( $combiner->combine( $out_file, $out_version) ) |
---|
1014 | { |
---|
1015 | $results[] = new Script($load_mode, 'combi', $out_file, $out_version, array() ); |
---|
1016 | } |
---|
1017 | $results[] = $script; |
---|
1018 | } |
---|
1019 | else |
---|
1020 | $combiner->add( $script->path, $script->version ); |
---|
1021 | } |
---|
1022 | if ( $combiner->combine( $out_file, $out_version) ) |
---|
1023 | { |
---|
1024 | $results[] = new Script($load_mode, 'combi', $out_file, $out_version, array() ); |
---|
1025 | } |
---|
1026 | return $results; |
---|
1027 | } |
---|
1028 | |
---|
1029 | // checks that if B depends on A, then B->load_mode >= A->load_mode in order to respect execution order |
---|
1030 | private static function check_load_dep($scripts) |
---|
1031 | { |
---|
1032 | global $conf; |
---|
1033 | do |
---|
1034 | { |
---|
1035 | $changed = false; |
---|
1036 | foreach( $scripts as $id => $script) |
---|
1037 | { |
---|
1038 | $load = $script->load_mode; |
---|
1039 | if ($load==0) |
---|
1040 | continue; |
---|
1041 | foreach( $script->precedents as $precedent) |
---|
1042 | { |
---|
1043 | if ( !isset($scripts[$precedent] ) ) |
---|
1044 | continue; |
---|
1045 | if ( $scripts[$precedent]->load_mode > $load ) |
---|
1046 | { |
---|
1047 | $scripts[$precedent]->load_mode = $load; |
---|
1048 | $changed = true; |
---|
1049 | } |
---|
1050 | if ($load==2 && $scripts[$precedent]->load_mode==2 && ($scripts[$precedent]->is_remote() or !$conf['template_combine_files']) ) |
---|
1051 | {// we are async -> a predecessor cannot be async unlesss it can be merged; otherwise script execution order is not guaranteed |
---|
1052 | $scripts[$precedent]->load_mode = 1; |
---|
1053 | $changed = true; |
---|
1054 | } |
---|
1055 | } |
---|
1056 | } |
---|
1057 | } |
---|
1058 | while ($changed); |
---|
1059 | } |
---|
1060 | |
---|
1061 | |
---|
1062 | private static function fill_well_known($id, $script) |
---|
1063 | { |
---|
1064 | if ( empty($script->path) && isset(self::$known_paths[$id])) |
---|
1065 | { |
---|
1066 | $script->path = self::$known_paths[$id]; |
---|
1067 | } |
---|
1068 | if ( strncmp($id, 'jquery.', 7)==0 ) |
---|
1069 | { |
---|
1070 | if ( !in_array('jquery', $script->precedents ) ) |
---|
1071 | $script->precedents[] = 'jquery'; |
---|
1072 | if ( strncmp($id, 'jquery.ui.', 10)==0 && !in_array('jquery.ui', $script->precedents ) ) |
---|
1073 | $script->precedents[] = 'jquery.ui'; |
---|
1074 | } |
---|
1075 | } |
---|
1076 | |
---|
1077 | private function compute_script_topological_order($script_id, $recursion_limiter=0) |
---|
1078 | { |
---|
1079 | if (!isset($this->registered_scripts[$script_id])) |
---|
1080 | { |
---|
1081 | trigger_error("Undefined script $script_id is required by someone", E_USER_WARNING); |
---|
1082 | return 0; |
---|
1083 | } |
---|
1084 | $recursion_limiter<5 or fatal_error("combined script circular dependency"); |
---|
1085 | $script = & $this->registered_scripts[$script_id]; |
---|
1086 | if (isset($script->extra['order'])) |
---|
1087 | return $script->extra['order']; |
---|
1088 | if (count($script->precedents) == 0) |
---|
1089 | return ($script->extra['order'] = 0); |
---|
1090 | $max = 0; |
---|
1091 | foreach( $script->precedents as $precedent) |
---|
1092 | $max = max($max, $this->compute_script_topological_order($precedent, $recursion_limiter+1) ); |
---|
1093 | $max++; |
---|
1094 | return ($script->extra['order'] = $max); |
---|
1095 | } |
---|
1096 | |
---|
1097 | private static function cmp_by_mode_and_order($s1, $s2) |
---|
1098 | { |
---|
1099 | $ret = $s1->load_mode - $s2->load_mode; |
---|
1100 | if ($ret) return $ret; |
---|
1101 | |
---|
1102 | $ret = $s1->extra['order'] - $s2->extra['order']; |
---|
1103 | if ($ret) return $ret; |
---|
1104 | |
---|
1105 | if ($s1->extra['order']==0 and ($s1->is_remote() xor $s2->is_remote()) ) |
---|
1106 | { |
---|
1107 | return $s1->is_remote() ? -1 : 1; |
---|
1108 | } |
---|
1109 | return strcmp($s1->id,$s2->id); |
---|
1110 | } |
---|
1111 | } |
---|
1112 | |
---|
1113 | |
---|
1114 | /*Allows merging of javascript and css files into a single one.*/ |
---|
1115 | final class FileCombiner |
---|
1116 | { |
---|
1117 | const OUT_SUB_DIR = 'local/combined/'; |
---|
1118 | private $type; // js or css |
---|
1119 | private $files = array(); |
---|
1120 | private $versions = array(); |
---|
1121 | |
---|
1122 | function FileCombiner($type) |
---|
1123 | { |
---|
1124 | $this->type = $type; |
---|
1125 | } |
---|
1126 | |
---|
1127 | static function clear_combined_files() |
---|
1128 | { |
---|
1129 | $dir = opendir(PHPWG_ROOT_PATH.self::OUT_SUB_DIR); |
---|
1130 | while ($file = readdir($dir)) |
---|
1131 | { |
---|
1132 | if ( get_extension($file)=='js' || get_extension($file)=='css') |
---|
1133 | unlink(PHPWG_ROOT_PATH.self::OUT_SUB_DIR.$file); |
---|
1134 | } |
---|
1135 | closedir($dir); |
---|
1136 | } |
---|
1137 | |
---|
1138 | function add($file, $version) |
---|
1139 | { |
---|
1140 | $this->files[] = $file; |
---|
1141 | $this->versions[] = $version; |
---|
1142 | } |
---|
1143 | |
---|
1144 | function clear() |
---|
1145 | { |
---|
1146 | $this->files = array(); |
---|
1147 | $this->versions = array(); |
---|
1148 | } |
---|
1149 | |
---|
1150 | function combine(&$out_file, &$out_version) |
---|
1151 | { |
---|
1152 | if (count($this->files) == 0) |
---|
1153 | { |
---|
1154 | return false; |
---|
1155 | } |
---|
1156 | if (count($this->files) == 1) |
---|
1157 | { |
---|
1158 | $out_file = $this->files[0]; |
---|
1159 | $out_version = $this->versions[0]; |
---|
1160 | $this->clear(); |
---|
1161 | return 1; |
---|
1162 | } |
---|
1163 | |
---|
1164 | $is_css = $this->type == "css"; |
---|
1165 | global $conf; |
---|
1166 | $key = array(); |
---|
1167 | if ($is_css) |
---|
1168 | $key[] = get_absolute_root_url(false);//because we modify bg url |
---|
1169 | for ($i=0; $i<count($this->files); $i++) |
---|
1170 | { |
---|
1171 | $key[] = $this->files[$i]; |
---|
1172 | $key[] = $this->versions[$i]; |
---|
1173 | if ($conf['template_compile_check']) $key[] = filemtime( PHPWG_ROOT_PATH . $this->files[$i] ); |
---|
1174 | } |
---|
1175 | $key = join('>', $key); |
---|
1176 | |
---|
1177 | $file = base_convert(crc32($key),10,36); |
---|
1178 | $file = self::OUT_SUB_DIR . $file . '.' . $this->type; |
---|
1179 | |
---|
1180 | $exists = file_exists( PHPWG_ROOT_PATH . $file ); |
---|
1181 | if ($exists) |
---|
1182 | { |
---|
1183 | $is_reload = |
---|
1184 | (isset($_SERVER['HTTP_CACHE_CONTROL']) && strpos($_SERVER['HTTP_CACHE_CONTROL'], 'max-age=0') !== false) |
---|
1185 | || (isset($_SERVER['HTTP_PRAGMA']) && strpos($_SERVER['HTTP_PRAGMA'], 'no-cache')); |
---|
1186 | if (is_admin() && $is_reload) |
---|
1187 | {// the user pressed F5 in the browser |
---|
1188 | if ($is_css || $conf['template_compile_check']==false) |
---|
1189 | $exists = false; // we foce regeneration of css because @import sub-files are never checked for modification |
---|
1190 | } |
---|
1191 | } |
---|
1192 | |
---|
1193 | if ($exists) |
---|
1194 | { |
---|
1195 | $out_file = $file; |
---|
1196 | $out_version = false; |
---|
1197 | $this->clear(); |
---|
1198 | return 2; |
---|
1199 | } |
---|
1200 | |
---|
1201 | $output = "/* ".join("\n", $this->files)."*/\n"; |
---|
1202 | foreach ($this->files as $input_file) |
---|
1203 | { |
---|
1204 | $output .= "/* BEGIN $input_file */\n"; |
---|
1205 | if ($is_css) |
---|
1206 | $output .= self::process_css($input_file); |
---|
1207 | else |
---|
1208 | $output .= self::process_js($input_file); |
---|
1209 | $output .= "\n"; |
---|
1210 | } |
---|
1211 | |
---|
1212 | file_put_contents( PHPWG_ROOT_PATH . $file, $output ); |
---|
1213 | $out_file = $file; |
---|
1214 | $out_version = false; |
---|
1215 | $this->clear(); |
---|
1216 | return 2; |
---|
1217 | } |
---|
1218 | |
---|
1219 | private static function process_js($file) |
---|
1220 | { |
---|
1221 | $js = file_get_contents(PHPWG_ROOT_PATH . $file); |
---|
1222 | if (strpos($file, '.min')===false and strpos($file, '.packed')===false ) |
---|
1223 | { |
---|
1224 | require_once(PHPWG_ROOT_PATH.'include/jsmin.class.php'); |
---|
1225 | try { $js = JSMin::minify($js); } catch(Exception $e) {} |
---|
1226 | } |
---|
1227 | return $js; |
---|
1228 | } |
---|
1229 | |
---|
1230 | private static function process_css($file) |
---|
1231 | { |
---|
1232 | $css = self::process_css_rec($file); |
---|
1233 | require_once(PHPWG_ROOT_PATH.'include/cssmin.class.php'); |
---|
1234 | $css = CssMin::minify($css, array('emulate-css3-variables'=>false)); |
---|
1235 | $css = trigger_event('combined_css_postfilter', $css); |
---|
1236 | return $css; |
---|
1237 | } |
---|
1238 | |
---|
1239 | private static function process_css_rec($file) |
---|
1240 | { |
---|
1241 | static $PATTERN = "#url\(\s*['|\"]{0,1}(.*?)['|\"]{0,1}\s*\)#"; |
---|
1242 | $css = file_get_contents(PHPWG_ROOT_PATH . $file); |
---|
1243 | if (preg_match_all($PATTERN, $css, $matches, PREG_SET_ORDER)) |
---|
1244 | { |
---|
1245 | $search = $replace = array(); |
---|
1246 | foreach ($matches as $match) |
---|
1247 | { |
---|
1248 | if ( !url_is_remote($match[1]) || $match[1][0] != '/') |
---|
1249 | { |
---|
1250 | $relative = dirname($file) . "/$match[1]"; |
---|
1251 | $search[] = $match[0]; |
---|
1252 | $replace[] = 'url('.embellish_url(get_absolute_root_url(false).$relative).')'; |
---|
1253 | } |
---|
1254 | } |
---|
1255 | $css = str_replace($search, $replace, $css); |
---|
1256 | } |
---|
1257 | |
---|
1258 | $imports = preg_match_all("#@import\s*['|\"]{0,1}(.*?)['|\"]{0,1};#", $css, $matches, PREG_SET_ORDER); |
---|
1259 | if ($imports) |
---|
1260 | { |
---|
1261 | $search = $replace = array(); |
---|
1262 | foreach ($matches as $match) |
---|
1263 | { |
---|
1264 | $search[] = $match[0]; |
---|
1265 | $replace[] = self::process_css_rec(dirname($file) . "/$match[1]"); |
---|
1266 | } |
---|
1267 | $css = str_replace($search, $replace, $css); |
---|
1268 | } |
---|
1269 | return $css; |
---|
1270 | } |
---|
1271 | } |
---|
1272 | |
---|
1273 | ?> |
---|