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