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