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