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 | /** |
---|
25 | * @package template |
---|
26 | */ |
---|
27 | |
---|
28 | //require_once( PHPWG_ROOT_PATH .'include/smarty/libs/Smarty.class.php'); |
---|
29 | require_once( PHPWG_ROOT_PATH .'include/smarty/libs/SmartyBC.class.php'); |
---|
30 | |
---|
31 | |
---|
32 | /** default rank for buttons */ |
---|
33 | define('BUTTONS_RANK_NEUTRAL', 50); |
---|
34 | |
---|
35 | /** |
---|
36 | * This a wrapper arround Smarty classes proving various custom mechanisms for templates. |
---|
37 | */ |
---|
38 | class Template |
---|
39 | { |
---|
40 | /** @var Smarty */ |
---|
41 | var $smarty; |
---|
42 | /** @var string */ |
---|
43 | var $output = ''; |
---|
44 | |
---|
45 | /** @var string[] - Hash of filenames for each template handle. */ |
---|
46 | var $files = array(); |
---|
47 | /** @var string[] - Template extents filenames for each template handle. */ |
---|
48 | var $extents = array(); |
---|
49 | /** @var array - Templates prefilter from external sources (plugins) */ |
---|
50 | var $external_filters = array(); |
---|
51 | |
---|
52 | /** @var string - Content to add before </head> tag */ |
---|
53 | var $html_head_elements = array(); |
---|
54 | /** @var string - Runtime CSS rules */ |
---|
55 | private $html_style = ''; |
---|
56 | |
---|
57 | /** @const string */ |
---|
58 | const COMBINED_SCRIPTS_TAG = '<!-- COMBINED_SCRIPTS -->'; |
---|
59 | /** @var ScriptLoader */ |
---|
60 | var $scriptLoader; |
---|
61 | |
---|
62 | /** @const string */ |
---|
63 | const COMBINED_CSS_TAG = '<!-- COMBINED_CSS -->'; |
---|
64 | /** @var CssLoader */ |
---|
65 | var $cssLoader; |
---|
66 | |
---|
67 | /** @var array - Runtime buttons on picture page */ |
---|
68 | var $picture_buttons = array(); |
---|
69 | /** @var array - Runtime buttons on index page */ |
---|
70 | var $index_buttons = array(); |
---|
71 | |
---|
72 | |
---|
73 | /** |
---|
74 | * @var string $root |
---|
75 | * @var string $theme |
---|
76 | * @var string $path |
---|
77 | */ |
---|
78 | function __construct($root=".", $theme="", $path="template") |
---|
79 | { |
---|
80 | global $conf, $lang_info; |
---|
81 | |
---|
82 | SmartyException::$escape = false; |
---|
83 | |
---|
84 | $this->scriptLoader = new ScriptLoader; |
---|
85 | $this->cssLoader = new CssLoader; |
---|
86 | $this->smarty = new SmartyBC; |
---|
87 | $this->smarty->debugging = $conf['debug_template']; |
---|
88 | if (!$this->smarty->debugging) |
---|
89 | { |
---|
90 | $this->smarty->error_reporting = error_reporting() & ~E_NOTICE; |
---|
91 | } |
---|
92 | $this->smarty->compile_check = $conf['template_compile_check']; |
---|
93 | $this->smarty->force_compile = $conf['template_force_compile']; |
---|
94 | |
---|
95 | if (!isset($conf['data_dir_checked'])) |
---|
96 | { |
---|
97 | $dir = PHPWG_ROOT_PATH.$conf['data_location']; |
---|
98 | mkgetdir($dir, MKGETDIR_DEFAULT&~MKGETDIR_DIE_ON_ERROR); |
---|
99 | if (!is_writable($dir)) |
---|
100 | { |
---|
101 | load_language('admin.lang'); |
---|
102 | fatal_error( |
---|
103 | l10n( |
---|
104 | 'Give write access (chmod 777) to "%s" directory at the root of your Piwigo installation', |
---|
105 | $conf['data_location'] |
---|
106 | ), |
---|
107 | l10n('an error happened'), |
---|
108 | false // show trace |
---|
109 | ); |
---|
110 | } |
---|
111 | if (function_exists('pwg_query')) { |
---|
112 | conf_update_param('data_dir_checked', 1); |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | $compile_dir = PHPWG_ROOT_PATH.$conf['data_location'].'templates_c'; |
---|
117 | mkgetdir( $compile_dir ); |
---|
118 | |
---|
119 | $this->smarty->setCompileDir($compile_dir); |
---|
120 | |
---|
121 | $this->smarty->assign( 'pwg', new PwgTemplateAdapter() ); |
---|
122 | $this->smarty->registerPlugin('modifiercompiler', 'translate', array('Template', 'modcompiler_translate') ); |
---|
123 | $this->smarty->registerPlugin('modifiercompiler', 'translate_dec', array('Template', 'modcompiler_translate_dec') ); |
---|
124 | $this->smarty->registerPlugin('modifier', 'explode', array('Template', 'mod_explode') ); |
---|
125 | $this->smarty->registerPlugin('modifier', 'get_extent', array($this, 'get_extent') ); |
---|
126 | $this->smarty->registerPlugin('block', 'html_head', array($this, 'block_html_head') ); |
---|
127 | $this->smarty->registerPlugin('block', 'html_style', array($this, 'block_html_style') ); |
---|
128 | $this->smarty->registerPlugin('function', 'combine_script', array($this, 'func_combine_script') ); |
---|
129 | $this->smarty->registerPlugin('function', 'get_combined_scripts', array($this, 'func_get_combined_scripts') ); |
---|
130 | $this->smarty->registerPlugin('function', 'combine_css', array($this, 'func_combine_css') ); |
---|
131 | $this->smarty->registerPlugin('function', 'define_derivative', array($this, 'func_define_derivative') ); |
---|
132 | $this->smarty->registerPlugin('compiler', 'get_combined_css', array($this, 'func_get_combined_css') ); |
---|
133 | $this->smarty->registerPlugin('block', 'footer_script', array($this, 'block_footer_script') ); |
---|
134 | $this->smarty->registerFilter('pre', array('Template', 'prefilter_white_space') ); |
---|
135 | if ( $conf['compiled_template_cache_language'] ) |
---|
136 | { |
---|
137 | $this->smarty->registerFilter('post', array('Template', 'postfilter_language') ); |
---|
138 | } |
---|
139 | |
---|
140 | $this->smarty->setTemplateDir(array()); |
---|
141 | if ( !empty($theme) ) |
---|
142 | { |
---|
143 | $this->set_theme($root, $theme, $path); |
---|
144 | if (!defined('IN_ADMIN')) |
---|
145 | { |
---|
146 | $this->set_prefilter( 'header', array('Template', 'prefilter_local_css') ); |
---|
147 | } |
---|
148 | } |
---|
149 | else |
---|
150 | $this->set_template_dir($root); |
---|
151 | |
---|
152 | $this->smarty->assign('lang_info', $lang_info); |
---|
153 | |
---|
154 | if (!defined('IN_ADMIN') and isset($conf['extents_for_templates'])) |
---|
155 | { |
---|
156 | $tpl_extents = unserialize($conf['extents_for_templates']); |
---|
157 | $this->set_extents($tpl_extents, './template-extension/', true, $theme); |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | /** |
---|
162 | * Loads theme's parameters. |
---|
163 | * |
---|
164 | * @param string $root |
---|
165 | * @param string $theme |
---|
166 | * @param string $path |
---|
167 | * @param bool $load_css |
---|
168 | * @param bool $load_local_head |
---|
169 | */ |
---|
170 | function set_theme($root, $theme, $path, $load_css=true, $load_local_head=true) |
---|
171 | { |
---|
172 | $this->set_template_dir($root.'/'.$theme.'/'.$path); |
---|
173 | |
---|
174 | $themeconf = $this->load_themeconf($root.'/'.$theme); |
---|
175 | |
---|
176 | if (isset($themeconf['parent']) and $themeconf['parent'] != $theme) |
---|
177 | { |
---|
178 | $this->set_theme( |
---|
179 | $root, |
---|
180 | $themeconf['parent'], |
---|
181 | $path, |
---|
182 | isset($themeconf['load_parent_css']) ? $themeconf['load_parent_css'] : $load_css, |
---|
183 | isset($themeconf['load_parent_local_head']) ? $themeconf['load_parent_local_head'] : $load_local_head |
---|
184 | ); |
---|
185 | } |
---|
186 | |
---|
187 | $tpl_var = array( |
---|
188 | 'id' => $theme, |
---|
189 | 'load_css' => $load_css, |
---|
190 | ); |
---|
191 | if (!empty($themeconf['local_head']) and $load_local_head) |
---|
192 | { |
---|
193 | $tpl_var['local_head'] = realpath($root.'/'.$theme.'/'.$themeconf['local_head'] ); |
---|
194 | } |
---|
195 | $themeconf['id'] = $theme; |
---|
196 | $this->smarty->append('themes', $tpl_var); |
---|
197 | $this->smarty->append('themeconf', $themeconf, true); |
---|
198 | } |
---|
199 | |
---|
200 | /** |
---|
201 | * Adds template directory for this Template object. |
---|
202 | * Also set compile id if not exists. |
---|
203 | * |
---|
204 | * @param string $dir |
---|
205 | */ |
---|
206 | function set_template_dir($dir) |
---|
207 | { |
---|
208 | $this->smarty->addTemplateDir($dir); |
---|
209 | |
---|
210 | if (!isset($this->smarty->compile_id)) |
---|
211 | { |
---|
212 | $compile_id = "1"; |
---|
213 | $compile_id .= ($real_dir = realpath($dir))===false ? $dir : $real_dir; |
---|
214 | $this->smarty->compile_id = base_convert(crc32($compile_id), 10, 36 ); |
---|
215 | } |
---|
216 | } |
---|
217 | |
---|
218 | /** |
---|
219 | * Gets the template root directory for this Template object. |
---|
220 | * |
---|
221 | * @return string |
---|
222 | */ |
---|
223 | function get_template_dir() |
---|
224 | { |
---|
225 | return $this->smarty->getTemplateDir(); |
---|
226 | } |
---|
227 | |
---|
228 | /** |
---|
229 | * Deletes all compiled templates. |
---|
230 | */ |
---|
231 | function delete_compiled_templates() |
---|
232 | { |
---|
233 | $save_compile_id = $this->smarty->compile_id; |
---|
234 | $this->smarty->compile_id = null; |
---|
235 | $this->smarty->clearCompiledTemplate(); |
---|
236 | $this->smarty->compile_id = $save_compile_id; |
---|
237 | file_put_contents($this->smarty->getCompileDir().'/index.htm', 'Not allowed!'); |
---|
238 | } |
---|
239 | |
---|
240 | /** |
---|
241 | * Returns theme's parameter. |
---|
242 | * |
---|
243 | * @param string $val |
---|
244 | * @return mixed |
---|
245 | */ |
---|
246 | function get_themeconf($val) |
---|
247 | { |
---|
248 | $tc = $this->smarty->getTemplateVars('themeconf'); |
---|
249 | return isset($tc[$val]) ? $tc[$val] : ''; |
---|
250 | } |
---|
251 | |
---|
252 | /** |
---|
253 | * Sets the template filename for handle. |
---|
254 | * |
---|
255 | * @param string $handle |
---|
256 | * @param string $filename |
---|
257 | * @return bool |
---|
258 | */ |
---|
259 | function set_filename($handle, $filename) |
---|
260 | { |
---|
261 | return $this->set_filenames( array($handle=>$filename) ); |
---|
262 | } |
---|
263 | |
---|
264 | /** |
---|
265 | * Sets the template filenames for handles. |
---|
266 | * |
---|
267 | * @param string[] $filename_array hashmap of handle=>filename |
---|
268 | * @return true |
---|
269 | */ |
---|
270 | function set_filenames($filename_array) |
---|
271 | { |
---|
272 | if (!is_array($filename_array)) |
---|
273 | { |
---|
274 | return false; |
---|
275 | } |
---|
276 | reset($filename_array); |
---|
277 | while(list($handle, $filename) = each($filename_array)) |
---|
278 | { |
---|
279 | if (is_null($filename)) |
---|
280 | { |
---|
281 | unset($this->files[$handle]); |
---|
282 | } |
---|
283 | else |
---|
284 | { |
---|
285 | $this->files[$handle] = $this->get_extent($filename, $handle); |
---|
286 | } |
---|
287 | } |
---|
288 | return true; |
---|
289 | } |
---|
290 | |
---|
291 | /** |
---|
292 | * Sets template extention filename for handles. |
---|
293 | * |
---|
294 | * @param string $filename |
---|
295 | * @param mixed $param |
---|
296 | * @param string $dir |
---|
297 | * @param bool $overwrite |
---|
298 | * @param string $theme |
---|
299 | * @return bool |
---|
300 | */ |
---|
301 | function set_extent($filename, $param, $dir='', $overwrite=true, $theme='N/A') |
---|
302 | { |
---|
303 | return $this->set_extents(array($filename => $param), $dir, $overwrite); |
---|
304 | } |
---|
305 | |
---|
306 | /** |
---|
307 | * Sets template extentions filenames for handles. |
---|
308 | * |
---|
309 | * @param string[] $filename_array hashmap of handle=>filename |
---|
310 | * @param string $dir |
---|
311 | * @param bool $overwrite |
---|
312 | * @param string $theme |
---|
313 | * @return bool |
---|
314 | */ |
---|
315 | function set_extents($filename_array, $dir='', $overwrite=true, $theme='N/A') |
---|
316 | { |
---|
317 | if (!is_array($filename_array)) |
---|
318 | { |
---|
319 | return false; |
---|
320 | } |
---|
321 | foreach ($filename_array as $filename => $value) |
---|
322 | { |
---|
323 | if (is_array($value)) |
---|
324 | { |
---|
325 | $handle = $value[0]; |
---|
326 | $param = $value[1]; |
---|
327 | $thm = $value[2]; |
---|
328 | } |
---|
329 | elseif (is_string($value)) |
---|
330 | { |
---|
331 | $handle = $value; |
---|
332 | $param = 'N/A'; |
---|
333 | $thm = 'N/A'; |
---|
334 | } |
---|
335 | else |
---|
336 | { |
---|
337 | return false; |
---|
338 | } |
---|
339 | |
---|
340 | if ((stripos(implode('',array_keys($_GET)), '/'.$param) !== false or $param == 'N/A') |
---|
341 | and ($thm == $theme or $thm == 'N/A') |
---|
342 | and (!isset($this->extents[$handle]) or $overwrite) |
---|
343 | and file_exists($dir . $filename)) |
---|
344 | { |
---|
345 | $this->extents[$handle] = realpath($dir . $filename); |
---|
346 | } |
---|
347 | } |
---|
348 | return true; |
---|
349 | } |
---|
350 | |
---|
351 | /** |
---|
352 | * Returns template extension if exists. |
---|
353 | * |
---|
354 | * @param string $filename should be empty! |
---|
355 | * @param string $handle |
---|
356 | * @return string |
---|
357 | */ |
---|
358 | function get_extent($filename='', $handle='') |
---|
359 | { |
---|
360 | if (isset($this->extents[$handle])) |
---|
361 | { |
---|
362 | $filename = $this->extents[$handle]; |
---|
363 | } |
---|
364 | return $filename; |
---|
365 | } |
---|
366 | |
---|
367 | /** |
---|
368 | * Assigns a template variable. |
---|
369 | * @see http://www.smarty.net/manual/en/api.assign.php |
---|
370 | * |
---|
371 | * @param string|array $tpl_var can be a var name or a hashmap of variables |
---|
372 | * (in this case, do not use the _$value_ parameter) |
---|
373 | * @param mixed $value |
---|
374 | */ |
---|
375 | function assign($tpl_var, $value=null) |
---|
376 | { |
---|
377 | $this->smarty->assign( $tpl_var, $value ); |
---|
378 | } |
---|
379 | |
---|
380 | /** |
---|
381 | * Defines _$varname_ as the compiled result of _$handle_. |
---|
382 | * This can be used to effectively include a template in another template. |
---|
383 | * This is equivalent to assign($varname, $this->parse($handle, true)). |
---|
384 | * |
---|
385 | * @param string $varname |
---|
386 | * @param string $handle |
---|
387 | * @return true |
---|
388 | */ |
---|
389 | function assign_var_from_handle($varname, $handle) |
---|
390 | { |
---|
391 | $this->assign($varname, $this->parse($handle, true)); |
---|
392 | return true; |
---|
393 | } |
---|
394 | |
---|
395 | /** |
---|
396 | * Appends a new value in a template array variable, the variable is created if needed. |
---|
397 | * @see http://www.smarty.net/manual/en/api.append.php |
---|
398 | * |
---|
399 | * @param string $tpl_var |
---|
400 | * @param mixed $value |
---|
401 | * @param bool $merge |
---|
402 | */ |
---|
403 | function append($tpl_var, $value=null, $merge=false) |
---|
404 | { |
---|
405 | $this->smarty->append( $tpl_var, $value, $merge ); |
---|
406 | } |
---|
407 | |
---|
408 | /** |
---|
409 | * Performs a string concatenation. |
---|
410 | * |
---|
411 | * @param string $tpl_var |
---|
412 | * @param string $value |
---|
413 | */ |
---|
414 | function concat($tpl_var, $value) |
---|
415 | { |
---|
416 | $this->assign($tpl_var, |
---|
417 | $this->smarty->getTemplateVars($tpl_var) . $value); |
---|
418 | } |
---|
419 | |
---|
420 | /** |
---|
421 | * Removes an assigned template variable. |
---|
422 | * @see http://www.smarty.net/manual/en/api.clear_assign.php |
---|
423 | * |
---|
424 | * @param string $tpl_var |
---|
425 | */ |
---|
426 | function clear_assign($tpl_var) |
---|
427 | { |
---|
428 | $this->smarty->clearAssign( $tpl_var ); |
---|
429 | } |
---|
430 | |
---|
431 | /** |
---|
432 | * Returns an assigned template variable. |
---|
433 | * @see http://www.smarty.net/manual/en/api.get_template_vars.php |
---|
434 | * |
---|
435 | * @param string $tpl_var |
---|
436 | */ |
---|
437 | function get_template_vars($tpl_var=null) |
---|
438 | { |
---|
439 | return $this->smarty->getTemplateVars( $tpl_var ); |
---|
440 | } |
---|
441 | |
---|
442 | /** |
---|
443 | * Loads the template file of the handle, compiles it and appends the result to the output |
---|
444 | * (or returns it if _$return_ is true). |
---|
445 | * |
---|
446 | * @param string $handle |
---|
447 | * @param bool $return |
---|
448 | * @return null|string |
---|
449 | */ |
---|
450 | function parse($handle, $return=false) |
---|
451 | { |
---|
452 | if ( !isset($this->files[$handle]) ) |
---|
453 | { |
---|
454 | fatal_error("Template->parse(): Couldn't load template file for handle $handle"); |
---|
455 | } |
---|
456 | |
---|
457 | $this->smarty->assign( 'ROOT_URL', get_root_url() ); |
---|
458 | |
---|
459 | $save_compile_id = $this->smarty->compile_id; |
---|
460 | $this->load_external_filters($handle); |
---|
461 | |
---|
462 | global $conf, $lang_info; |
---|
463 | if ( $conf['compiled_template_cache_language'] and isset($lang_info['code']) ) |
---|
464 | { |
---|
465 | $this->smarty->compile_id .= '_'.$lang_info['code']; |
---|
466 | } |
---|
467 | |
---|
468 | $v = $this->smarty->fetch($this->files[$handle]); |
---|
469 | |
---|
470 | $this->smarty->compile_id = $save_compile_id; |
---|
471 | $this->unload_external_filters($handle); |
---|
472 | |
---|
473 | if ($return) |
---|
474 | { |
---|
475 | return $v; |
---|
476 | } |
---|
477 | $this->output .= $v; |
---|
478 | } |
---|
479 | |
---|
480 | /** |
---|
481 | * Loads the template file of the handle, compiles it and appends the result to the output, |
---|
482 | * then sends the output to the browser. |
---|
483 | * |
---|
484 | * @param string $handle |
---|
485 | */ |
---|
486 | function pparse($handle) |
---|
487 | { |
---|
488 | $this->parse($handle, false); |
---|
489 | $this->flush(); |
---|
490 | } |
---|
491 | |
---|
492 | /** |
---|
493 | * Load and compile JS & CSS into the template and sends the output to the browser. |
---|
494 | */ |
---|
495 | function flush() |
---|
496 | { |
---|
497 | if (!$this->scriptLoader->did_head()) |
---|
498 | { |
---|
499 | $pos = strpos( $this->output, self::COMBINED_SCRIPTS_TAG ); |
---|
500 | if ($pos !== false) |
---|
501 | { |
---|
502 | $scripts = $this->scriptLoader->get_head_scripts(); |
---|
503 | $content = array(); |
---|
504 | foreach ($scripts as $script) |
---|
505 | { |
---|
506 | $content[]= |
---|
507 | '<script type="text/javascript" src="' |
---|
508 | . self::make_script_src($script) |
---|
509 | .'"></script>'; |
---|
510 | } |
---|
511 | |
---|
512 | $this->output = substr_replace( $this->output, implode( "\n", $content ), $pos, strlen(self::COMBINED_SCRIPTS_TAG) ); |
---|
513 | } //else maybe error or warning ? |
---|
514 | } |
---|
515 | |
---|
516 | $css = $this->cssLoader->get_css(); |
---|
517 | |
---|
518 | $content = array(); |
---|
519 | foreach( $css as $combi ) |
---|
520 | { |
---|
521 | $href = embellish_url(get_root_url().$combi->path); |
---|
522 | if ($combi->version !== false) |
---|
523 | $href .= '?v' . ($combi->version ? $combi->version : PHPWG_VERSION); |
---|
524 | // trigger the event for eventual use of a cdn |
---|
525 | $href = trigger_event('combined_css', $href, $combi); |
---|
526 | $content[] = '<link rel="stylesheet" type="text/css" href="'.$href.'">'; |
---|
527 | } |
---|
528 | $this->output = str_replace(self::COMBINED_CSS_TAG, |
---|
529 | implode( "\n", $content ), |
---|
530 | $this->output ); |
---|
531 | $this->cssLoader->clear(); |
---|
532 | |
---|
533 | if ( count($this->html_head_elements) || strlen($this->html_style) ) |
---|
534 | { |
---|
535 | $search = "\n</head>"; |
---|
536 | $pos = strpos( $this->output, $search ); |
---|
537 | if ($pos !== false) |
---|
538 | { |
---|
539 | $rep = "\n".implode( "\n", $this->html_head_elements ); |
---|
540 | if (strlen($this->html_style)) |
---|
541 | { |
---|
542 | $rep.='<style type="text/css">'.$this->html_style.'</style>'; |
---|
543 | } |
---|
544 | $this->output = substr_replace( $this->output, $rep, $pos, 0 ); |
---|
545 | } //else maybe error or warning ? |
---|
546 | $this->html_head_elements = array(); |
---|
547 | $this->html_style = ''; |
---|
548 | } |
---|
549 | |
---|
550 | echo $this->output; |
---|
551 | $this->output=''; |
---|
552 | } |
---|
553 | |
---|
554 | /** |
---|
555 | * Same as flush() but with optional debugging. |
---|
556 | * @see Template::flush() |
---|
557 | */ |
---|
558 | function p() |
---|
559 | { |
---|
560 | $this->flush(); |
---|
561 | |
---|
562 | if ($this->smarty->debugging) |
---|
563 | { |
---|
564 | global $t2; |
---|
565 | $this->smarty->assign( |
---|
566 | array( |
---|
567 | 'AAAA_DEBUG_TOTAL_TIME__' => get_elapsed_time($t2, get_moment()) |
---|
568 | ) |
---|
569 | ); |
---|
570 | Smarty_Internal_Debug::display_debug($this->smarty); |
---|
571 | } |
---|
572 | } |
---|
573 | |
---|
574 | /** |
---|
575 | * Eval a temp string to retrieve the original PHP value. |
---|
576 | * |
---|
577 | * @param string $str |
---|
578 | * @return mixed |
---|
579 | */ |
---|
580 | static function get_php_str_val($str) |
---|
581 | { |
---|
582 | if (is_string($str) && strlen($str)>1) |
---|
583 | { |
---|
584 | if ( ($str[0]=='\'' && $str[strlen($str)-1]=='\'') |
---|
585 | || ($str[0]=='"' && $str[strlen($str)-1]=='"')) |
---|
586 | { |
---|
587 | eval('$tmp='.$str.';'); |
---|
588 | return $tmp; |
---|
589 | } |
---|
590 | } |
---|
591 | return null; |
---|
592 | } |
---|
593 | |
---|
594 | /** |
---|
595 | * "translate" variable modifier. |
---|
596 | * Usage : |
---|
597 | * - {'Comment'|translate} |
---|
598 | * - {'%d comments'|translate:$count} |
---|
599 | * @see l10n() |
---|
600 | * |
---|
601 | * @param array $params |
---|
602 | * @return string |
---|
603 | */ |
---|
604 | static function modcompiler_translate($params) |
---|
605 | { |
---|
606 | global $conf, $lang; |
---|
607 | |
---|
608 | switch (count($params)) |
---|
609 | { |
---|
610 | case 1: |
---|
611 | if ($conf['compiled_template_cache_language'] |
---|
612 | && ($key=self::get_php_str_val($params[0])) !== null |
---|
613 | && isset($lang[$key]) |
---|
614 | ) { |
---|
615 | return var_export($lang[$key], true); |
---|
616 | } |
---|
617 | return 'l10n('.$params[0].')'; |
---|
618 | |
---|
619 | default: |
---|
620 | if ($conf['compiled_template_cache_language']) |
---|
621 | { |
---|
622 | $ret = 'sprintf('; |
---|
623 | $ret .= self::modcompiler_translate( array($params[0]) ); |
---|
624 | $ret .= ','. implode(',', array_slice($params, 1)); |
---|
625 | $ret .= ')'; |
---|
626 | return $ret; |
---|
627 | } |
---|
628 | return 'l10n('.$params[0].','.implode(',', array_slice($params, 1)).')'; |
---|
629 | } |
---|
630 | } |
---|
631 | |
---|
632 | /** |
---|
633 | * "translate_dec" variable modifier. |
---|
634 | * Usage : |
---|
635 | * - {$count|translate_dec:'%d comment':'%d comments'} |
---|
636 | * @see l10n_dec() |
---|
637 | * |
---|
638 | * @param array $params |
---|
639 | * @return string |
---|
640 | */ |
---|
641 | static function modcompiler_translate_dec($params) |
---|
642 | { |
---|
643 | global $conf, $lang, $lang_info; |
---|
644 | if ($conf['compiled_template_cache_language']) |
---|
645 | { |
---|
646 | $ret = 'sprintf('; |
---|
647 | if ($lang_info['zero_plural']) |
---|
648 | { |
---|
649 | $ret .= '($tmp=('.$params[0].'))>1||$tmp==0'; |
---|
650 | } |
---|
651 | else |
---|
652 | { |
---|
653 | $ret .= '($tmp=('.$params[0].'))>1'; |
---|
654 | } |
---|
655 | $ret .= '?'; |
---|
656 | $ret .= self::modcompiler_translate( array($params[2]) ); |
---|
657 | $ret .= ':'; |
---|
658 | $ret .= self::modcompiler_translate( array($params[1]) ); |
---|
659 | $ret .= ',$tmp'; |
---|
660 | $ret .= ')'; |
---|
661 | return $ret; |
---|
662 | } |
---|
663 | return 'l10n_dec('.$params[1].','.$params[2].','.$params[0].')'; |
---|
664 | } |
---|
665 | |
---|
666 | /** |
---|
667 | * "explode" variable modifier. |
---|
668 | * Usage : |
---|
669 | * - {assign var=valueExploded value=$value|@explode:','} |
---|
670 | * |
---|
671 | * @param string $text |
---|
672 | * @param string $delimiter |
---|
673 | * @return array |
---|
674 | */ |
---|
675 | static function mod_explode($text, $delimiter=',') |
---|
676 | { |
---|
677 | return explode($delimiter, $text); |
---|
678 | } |
---|
679 | |
---|
680 | /** |
---|
681 | * The "html_head" block allows to add content just before |
---|
682 | * </head> element in the output after the head has been parsed. |
---|
683 | * |
---|
684 | * @param array $params (unused) |
---|
685 | * @param string $content |
---|
686 | */ |
---|
687 | function block_html_head($params, $content) |
---|
688 | { |
---|
689 | $content = trim($content); |
---|
690 | if ( !empty($content) ) |
---|
691 | { // second call |
---|
692 | $this->html_head_elements[] = $content; |
---|
693 | } |
---|
694 | } |
---|
695 | |
---|
696 | /** |
---|
697 | * The "html_style" block allows to add CSS juste before |
---|
698 | * </head> element in the output after the head has been parsed. |
---|
699 | * |
---|
700 | * @param array $params (unused) |
---|
701 | * @param string $content |
---|
702 | */ |
---|
703 | function block_html_style($params, $content) |
---|
704 | { |
---|
705 | $content = trim($content); |
---|
706 | if ( !empty($content) ) |
---|
707 | { // second call |
---|
708 | $this->html_style .= "\n".$content; |
---|
709 | } |
---|
710 | } |
---|
711 | |
---|
712 | /** |
---|
713 | * The "define_derivative" function allows to define derivative from tpl file. |
---|
714 | * It assigns a DerivativeParams object to _name_ template variable. |
---|
715 | * |
---|
716 | * @param array $params |
---|
717 | * - name (required) |
---|
718 | * - type (optional) |
---|
719 | * - width (required if type is empty) |
---|
720 | * - height (required if type is empty) |
---|
721 | * - crop (optional, used if type is empty) |
---|
722 | * - min_height (optional, used with crop) |
---|
723 | * - min_height (optional, used with crop) |
---|
724 | * @param Smarty $smarty |
---|
725 | */ |
---|
726 | function func_define_derivative($params, $smarty) |
---|
727 | { |
---|
728 | !empty($params['name']) or fatal_error('define_derivative missing name'); |
---|
729 | if (isset($params['type'])) |
---|
730 | { |
---|
731 | $derivative = ImageStdParams::get_by_type($params['type']); |
---|
732 | $smarty->assign( $params['name'], $derivative); |
---|
733 | return; |
---|
734 | } |
---|
735 | !empty($params['width']) or fatal_error('define_derivative missing width'); |
---|
736 | !empty($params['height']) or fatal_error('define_derivative missing height'); |
---|
737 | |
---|
738 | $w = intval($params['width']); |
---|
739 | $h = intval($params['height']); |
---|
740 | $crop = 0; |
---|
741 | $minw=null; |
---|
742 | $minh=null; |
---|
743 | |
---|
744 | if (isset($params['crop'])) |
---|
745 | { |
---|
746 | if (is_bool($params['crop'])) |
---|
747 | { |
---|
748 | $crop = $params['crop'] ? 1:0; |
---|
749 | } |
---|
750 | else |
---|
751 | { |
---|
752 | $crop = round($params['crop']/100, 2); |
---|
753 | } |
---|
754 | |
---|
755 | if ($crop) |
---|
756 | { |
---|
757 | $minw = empty($params['min_width']) ? $w : intval($params['min_width']); |
---|
758 | $minw <= $w or fatal_error('define_derivative invalid min_width'); |
---|
759 | $minh = empty($params['min_height']) ? $h : intval($params['min_height']); |
---|
760 | $minh <= $h or fatal_error('define_derivative invalid min_height'); |
---|
761 | } |
---|
762 | } |
---|
763 | |
---|
764 | $smarty->assign( $params['name'], ImageStdParams::get_custom($w, $h, $crop, $minw, $minh) ); |
---|
765 | } |
---|
766 | |
---|
767 | /** |
---|
768 | * The "combine_script" functions allows inclusion of a javascript file in the current page. |
---|
769 | * The engine will combine several js files into a single one. |
---|
770 | * |
---|
771 | * @param array $params |
---|
772 | * - id (required) |
---|
773 | * - path (required) |
---|
774 | * - load (optional) 'header', 'footer' or 'async' |
---|
775 | * - require (optional) comma separated list of script ids required to be loaded |
---|
776 | * and executed before this one |
---|
777 | * - version (optional) used to force a browser refresh |
---|
778 | */ |
---|
779 | function func_combine_script($params) |
---|
780 | { |
---|
781 | if (!isset($params['id'])) |
---|
782 | { |
---|
783 | trigger_error("combine_script: missing 'id' parameter", E_USER_ERROR); |
---|
784 | } |
---|
785 | $load = 0; |
---|
786 | if (isset($params['load'])) |
---|
787 | { |
---|
788 | switch ($params['load']) |
---|
789 | { |
---|
790 | case 'header': break; |
---|
791 | case 'footer': $load=1; break; |
---|
792 | case 'async': $load=2; break; |
---|
793 | default: trigger_error("combine_script: invalid 'load' parameter", E_USER_ERROR); |
---|
794 | } |
---|
795 | } |
---|
796 | |
---|
797 | $this->scriptLoader->add( $params['id'], $load, |
---|
798 | empty($params['require']) ? array() : explode( ',', $params['require'] ), |
---|
799 | @$params['path'], |
---|
800 | isset($params['version']) ? $params['version'] : 0, |
---|
801 | @$params['template']); |
---|
802 | } |
---|
803 | |
---|
804 | /** |
---|
805 | * The "get_combined_scripts" function returns HTML tag of combined scripts. |
---|
806 | * It can returns a placeholder for delayed JS files combination and minification. |
---|
807 | * |
---|
808 | * @param array $params |
---|
809 | * - load (required) |
---|
810 | */ |
---|
811 | function func_get_combined_scripts($params) |
---|
812 | { |
---|
813 | if (!isset($params['load'])) |
---|
814 | { |
---|
815 | trigger_error("get_combined_scripts: missing 'load' parameter", E_USER_ERROR); |
---|
816 | } |
---|
817 | $load = $params['load']=='header' ? 0 : 1; |
---|
818 | $content = array(); |
---|
819 | |
---|
820 | if ($load==0) |
---|
821 | { |
---|
822 | return self::COMBINED_SCRIPTS_TAG; |
---|
823 | } |
---|
824 | else |
---|
825 | { |
---|
826 | $scripts = $this->scriptLoader->get_footer_scripts(); |
---|
827 | foreach ($scripts[0] as $script) |
---|
828 | { |
---|
829 | $content[]= |
---|
830 | '<script type="text/javascript" src="' |
---|
831 | . self::make_script_src($script) |
---|
832 | .'"></script>'; |
---|
833 | } |
---|
834 | if (count($this->scriptLoader->inline_scripts)) |
---|
835 | { |
---|
836 | $content[]= '<script type="text/javascript">//<![CDATA[ |
---|
837 | '; |
---|
838 | $content = array_merge($content, $this->scriptLoader->inline_scripts); |
---|
839 | $content[]= '//]]></script>'; |
---|
840 | } |
---|
841 | |
---|
842 | if (count($scripts[1])) |
---|
843 | { |
---|
844 | $content[]= '<script type="text/javascript">'; |
---|
845 | $content[]= '(function() { |
---|
846 | var s,after = document.getElementsByTagName(\'script\')[document.getElementsByTagName(\'script\').length-1];'; |
---|
847 | foreach ($scripts[1] as $id => $script) |
---|
848 | { |
---|
849 | $content[]= |
---|
850 | 's=document.createElement(\'script\'); s.type=\'text/javascript\'; s.async=true; s.src=\'' |
---|
851 | . self::make_script_src($script) |
---|
852 | .'\';'; |
---|
853 | $content[]= 'after = after.parentNode.insertBefore(s, after);'; |
---|
854 | } |
---|
855 | $content[]= '})();'; |
---|
856 | $content[]= '</script>'; |
---|
857 | } |
---|
858 | } |
---|
859 | return implode("\n", $content); |
---|
860 | } |
---|
861 | |
---|
862 | /** |
---|
863 | * Returns clean relative URL to script file. |
---|
864 | * |
---|
865 | * @param Combinable $script |
---|
866 | * @return string |
---|
867 | */ |
---|
868 | private static function make_script_src($script) |
---|
869 | { |
---|
870 | $ret = ''; |
---|
871 | if ( $script->is_remote() ) |
---|
872 | $ret = $script->path; |
---|
873 | else |
---|
874 | { |
---|
875 | $ret = get_root_url().$script->path; |
---|
876 | if ($script->version!==false) |
---|
877 | { |
---|
878 | $ret.= '?v'. ($script->version ? $script->version : PHPWG_VERSION); |
---|
879 | } |
---|
880 | } |
---|
881 | // trigger the event for eventual use of a cdn |
---|
882 | $ret = trigger_event('combined_script', $ret, $script); |
---|
883 | return embellish_url($ret); |
---|
884 | } |
---|
885 | |
---|
886 | /** |
---|
887 | * The "footer_script" block allows to add runtime script in the HTML page. |
---|
888 | * |
---|
889 | * @param array $params |
---|
890 | * - require (optional) comma separated list of script ids |
---|
891 | * @param string $content |
---|
892 | */ |
---|
893 | function block_footer_script($params, $content) |
---|
894 | { |
---|
895 | $content = trim($content); |
---|
896 | if ( !empty($content) ) |
---|
897 | { // second call |
---|
898 | |
---|
899 | $this->scriptLoader->add_inline( |
---|
900 | $content, |
---|
901 | empty($params['require']) ? array() : explode(',', $params['require']) |
---|
902 | ); |
---|
903 | } |
---|
904 | } |
---|
905 | |
---|
906 | /** |
---|
907 | * The "combine_css" function allows inclusion of a css file in the current page. |
---|
908 | * The engine will combine several css files into a single one. |
---|
909 | * |
---|
910 | * @param array $params |
---|
911 | * - id (optional) used to deal with multiple inclusions from plugins |
---|
912 | * - path (required) |
---|
913 | * - version (optional) used to force a browser refresh |
---|
914 | * - order (optional) |
---|
915 | * - template (optional) set to true to allow smarty syntax in the css file |
---|
916 | */ |
---|
917 | function func_combine_css($params) |
---|
918 | { |
---|
919 | if (empty($params['path'])) |
---|
920 | { |
---|
921 | fatal_error('combine_css missing path'); |
---|
922 | } |
---|
923 | |
---|
924 | if (!isset($params['id'])) |
---|
925 | { |
---|
926 | $params['id'] = md5($params['path']); |
---|
927 | } |
---|
928 | |
---|
929 | $this->cssLoader->add($params['id'], $params['path'], isset($params['version']) ? $params['version'] : 0, (int)@$params['order'], (bool)@$params['template']); |
---|
930 | } |
---|
931 | |
---|
932 | /** |
---|
933 | * The "get_combined_scripts" function returns a placeholder for delayed |
---|
934 | * CSS files combination and minification. |
---|
935 | * |
---|
936 | * @param array $params (unused) |
---|
937 | */ |
---|
938 | function func_get_combined_css($params) |
---|
939 | { |
---|
940 | return self::COMBINED_CSS_TAG; |
---|
941 | } |
---|
942 | |
---|
943 | /** |
---|
944 | * Declares a Smarty prefilter from a plugin, allowing it to modify template |
---|
945 | * source before compilation and without changing core files. |
---|
946 | * They will be processed by weight ascending. |
---|
947 | * @see http://www.smarty.net/manual/en/advanced.features.prefilters.php |
---|
948 | * |
---|
949 | * @param string $handle |
---|
950 | * @param Callable $callback |
---|
951 | * @param int $weight |
---|
952 | */ |
---|
953 | function set_prefilter($handle, $callback, $weight=50) |
---|
954 | { |
---|
955 | $this->external_filters[$handle][$weight][] = array('pre', $callback); |
---|
956 | ksort($this->external_filters[$handle]); |
---|
957 | } |
---|
958 | |
---|
959 | /** |
---|
960 | * Declares a Smarty postfilter. |
---|
961 | * They will be processed by weight ascending. |
---|
962 | * @see http://www.smarty.net/manual/en/advanced.features.postfilters.php |
---|
963 | * |
---|
964 | * @param string $handle |
---|
965 | * @param Callable $callback |
---|
966 | * @param int $weight |
---|
967 | */ |
---|
968 | function set_postfilter($handle, $callback, $weight=50) |
---|
969 | { |
---|
970 | $this->external_filters[$handle][$weight][] = array('post', $callback); |
---|
971 | ksort($this->external_filters[$handle]); |
---|
972 | } |
---|
973 | |
---|
974 | /** |
---|
975 | * Declares a Smarty outputfilter. |
---|
976 | * They will be processed by weight ascending. |
---|
977 | * @see http://www.smarty.net/manual/en/advanced.features.outputfilters.php |
---|
978 | * |
---|
979 | * @param string $handle |
---|
980 | * @param Callable $callback |
---|
981 | * @param int $weight |
---|
982 | */ |
---|
983 | function set_outputfilter($handle, $callback, $weight=50) |
---|
984 | { |
---|
985 | $this->external_filters[$handle][$weight][] = array('output', $callback); |
---|
986 | ksort($this->external_filters[$handle]); |
---|
987 | } |
---|
988 | |
---|
989 | /** |
---|
990 | * Register the filters for the tpl file. |
---|
991 | * |
---|
992 | * @param string $handle |
---|
993 | */ |
---|
994 | function load_external_filters($handle) |
---|
995 | { |
---|
996 | if (isset($this->external_filters[$handle])) |
---|
997 | { |
---|
998 | $compile_id = ''; |
---|
999 | foreach ($this->external_filters[$handle] as $filters) |
---|
1000 | { |
---|
1001 | foreach ($filters as $filter) |
---|
1002 | { |
---|
1003 | list($type, $callback) = $filter; |
---|
1004 | $compile_id .= $type.( is_array($callback) ? implode('', $callback) : $callback ); |
---|
1005 | $this->smarty->registerFilter($type, $callback); |
---|
1006 | } |
---|
1007 | } |
---|
1008 | $this->smarty->compile_id .= '.'.base_convert(crc32($compile_id), 10, 36); |
---|
1009 | } |
---|
1010 | } |
---|
1011 | |
---|
1012 | /** |
---|
1013 | * Unregister the filters for the tpl file. |
---|
1014 | * |
---|
1015 | * @param string $handle |
---|
1016 | */ |
---|
1017 | function unload_external_filters($handle) |
---|
1018 | { |
---|
1019 | if (isset($this->external_filters[$handle])) |
---|
1020 | { |
---|
1021 | foreach ($this->external_filters[$handle] as $filters) |
---|
1022 | { |
---|
1023 | foreach ($filters as $filter) |
---|
1024 | { |
---|
1025 | list($type, $callback) = $filter; |
---|
1026 | $this->smarty->unregisterFilter($type, $callback); |
---|
1027 | } |
---|
1028 | } |
---|
1029 | } |
---|
1030 | } |
---|
1031 | |
---|
1032 | /** |
---|
1033 | * @toto : description of Template::prefilter_white_space |
---|
1034 | * |
---|
1035 | * @param string $source |
---|
1036 | * @param Smarty $smarty |
---|
1037 | * @param return string |
---|
1038 | */ |
---|
1039 | static function prefilter_white_space($source, $smarty) |
---|
1040 | { |
---|
1041 | $ld = $smarty->left_delimiter; |
---|
1042 | $rd = $smarty->right_delimiter; |
---|
1043 | $ldq = preg_quote($ld, '#'); |
---|
1044 | $rdq = preg_quote($rd, '#'); |
---|
1045 | |
---|
1046 | $regex = array(); |
---|
1047 | $tags = array('if','foreach','section','footer_script'); |
---|
1048 | foreach($tags as $tag) |
---|
1049 | { |
---|
1050 | $regex[] = "#^[ \t]+($ldq$tag"."[^$ld$rd]*$rdq)\s*$#m"; |
---|
1051 | $regex[] = "#^[ \t]+($ldq/$tag$rdq)\s*$#m"; |
---|
1052 | } |
---|
1053 | $tags = array('include','else','combine_script','html_head'); |
---|
1054 | foreach($tags as $tag) |
---|
1055 | { |
---|
1056 | $regex[] = "#^[ \t]+($ldq$tag"."[^$ld$rd]*$rdq)\s*$#m"; |
---|
1057 | } |
---|
1058 | $source = preg_replace( $regex, "$1", $source); |
---|
1059 | return $source; |
---|
1060 | } |
---|
1061 | |
---|
1062 | /** |
---|
1063 | * Postfilter used when $conf['compiled_template_cache_language'] is true. |
---|
1064 | * |
---|
1065 | * @param string $source |
---|
1066 | * @param Smarty $smarty |
---|
1067 | * @param return string |
---|
1068 | */ |
---|
1069 | static function postfilter_language($source, $smarty) |
---|
1070 | { |
---|
1071 | // replaces echo PHP_STRING_LITERAL; with the string literal value |
---|
1072 | $source = preg_replace_callback( |
---|
1073 | '/\\<\\?php echo ((?:\'(?:(?:\\\\.)|[^\'])*\')|(?:"(?:(?:\\\\.)|[^"])*"));\\?\\>\\n/', |
---|
1074 | create_function('$matches', 'eval(\'$tmp=\'.$matches[1].\';\');return $tmp;'), |
---|
1075 | $source); |
---|
1076 | return $source; |
---|
1077 | } |
---|
1078 | |
---|
1079 | /** |
---|
1080 | * Prefilter used to add theme local CSS files. |
---|
1081 | * |
---|
1082 | * @param string $source |
---|
1083 | * @param Smarty $smarty |
---|
1084 | * @param return string |
---|
1085 | */ |
---|
1086 | static function prefilter_local_css($source, $smarty) |
---|
1087 | { |
---|
1088 | $css = array(); |
---|
1089 | foreach ($smarty->getTemplateVars('themes') as $theme) |
---|
1090 | { |
---|
1091 | $f = PWG_LOCAL_DIR.'css/'.$theme['id'].'-rules.css'; |
---|
1092 | if (file_exists(PHPWG_ROOT_PATH.$f)) |
---|
1093 | { |
---|
1094 | $css[] = "{combine_css path='$f' order=10}"; |
---|
1095 | } |
---|
1096 | } |
---|
1097 | $f = PWG_LOCAL_DIR.'css/rules.css'; |
---|
1098 | if (file_exists(PHPWG_ROOT_PATH.$f)) |
---|
1099 | { |
---|
1100 | $css[] = "{combine_css path='$f' order=10}"; |
---|
1101 | } |
---|
1102 | |
---|
1103 | if (!empty($css)) |
---|
1104 | { |
---|
1105 | $source = str_replace("\n{get_combined_css}", "\n".implode( "\n", $css )."\n{get_combined_css}", $source); |
---|
1106 | } |
---|
1107 | |
---|
1108 | return $source; |
---|
1109 | } |
---|
1110 | |
---|
1111 | /** |
---|
1112 | * Loads the configuration file from a theme directory and returns it. |
---|
1113 | * |
---|
1114 | * @param string $dir |
---|
1115 | * @return array |
---|
1116 | */ |
---|
1117 | function load_themeconf($dir) |
---|
1118 | { |
---|
1119 | global $themeconfs, $conf; |
---|
1120 | |
---|
1121 | $dir = realpath($dir); |
---|
1122 | if (!isset($themeconfs[$dir])) |
---|
1123 | { |
---|
1124 | $themeconf = array(); |
---|
1125 | include($dir.'/themeconf.inc.php'); |
---|
1126 | // Put themeconf in cache |
---|
1127 | $themeconfs[$dir] = $themeconf; |
---|
1128 | } |
---|
1129 | return $themeconfs[$dir]; |
---|
1130 | } |
---|
1131 | |
---|
1132 | /** |
---|
1133 | * Registers a button to be displayed on picture page. |
---|
1134 | * |
---|
1135 | * @param string $content |
---|
1136 | * @param int $rank |
---|
1137 | */ |
---|
1138 | function add_picture_button($content, $rank=BUTTONS_RANK_NEUTRAL) |
---|
1139 | { |
---|
1140 | $this->picture_buttons[$rank][] = $content; |
---|
1141 | } |
---|
1142 | |
---|
1143 | /** |
---|
1144 | * Registers a button to be displayed on index pages. |
---|
1145 | * |
---|
1146 | * @param string $content |
---|
1147 | * @param int $rank |
---|
1148 | */ |
---|
1149 | function add_index_button($content, $rank=BUTTONS_RANK_NEUTRAL) |
---|
1150 | { |
---|
1151 | $this->index_buttons[$rank][] = $content; |
---|
1152 | } |
---|
1153 | |
---|
1154 | /** |
---|
1155 | * Assigns PLUGIN_PICTURE_BUTTONS template variable with registered picture buttons. |
---|
1156 | */ |
---|
1157 | function parse_picture_buttons() |
---|
1158 | { |
---|
1159 | if (!empty($this->picture_buttons)) |
---|
1160 | { |
---|
1161 | ksort($this->picture_buttons); |
---|
1162 | $buttons = array(); |
---|
1163 | foreach ($this->picture_buttons as $k => $row) |
---|
1164 | { |
---|
1165 | $buttons = array_merge($buttons, $row); |
---|
1166 | } |
---|
1167 | $this->assign('PLUGIN_PICTURE_BUTTONS', $buttons); |
---|
1168 | |
---|
1169 | // only for PHP 5.3 |
---|
1170 | // $this->assign('PLUGIN_PICTURE_BUTTONS', |
---|
1171 | // array_reduce( |
---|
1172 | // $this->picture_buttons, |
---|
1173 | // create_function('$v,$w', 'return array_merge($v, $w);'), |
---|
1174 | // array() |
---|
1175 | // )); |
---|
1176 | } |
---|
1177 | } |
---|
1178 | |
---|
1179 | /** |
---|
1180 | * Assigns PLUGIN_INDEX_BUTTONS template variable with registered index buttons. |
---|
1181 | */ |
---|
1182 | function parse_index_buttons() |
---|
1183 | { |
---|
1184 | if (!empty($this->index_buttons)) |
---|
1185 | { |
---|
1186 | ksort($this->index_buttons); |
---|
1187 | $buttons = array(); |
---|
1188 | foreach ($this->index_buttons as $k => $row) |
---|
1189 | { |
---|
1190 | $buttons = array_merge($buttons, $row); |
---|
1191 | } |
---|
1192 | $this->assign('PLUGIN_INDEX_BUTTONS', $buttons); |
---|
1193 | |
---|
1194 | // only for PHP 5.3 |
---|
1195 | // $this->assign('PLUGIN_INDEX_BUTTONS', |
---|
1196 | // array_reduce( |
---|
1197 | // $this->index_buttons, |
---|
1198 | // create_function('$v,$w', 'return array_merge($v, $w);'), |
---|
1199 | // array() |
---|
1200 | // )); |
---|
1201 | } |
---|
1202 | } |
---|
1203 | } |
---|
1204 | |
---|
1205 | |
---|
1206 | /** |
---|
1207 | * This class contains basic functions that can be called directly from the |
---|
1208 | * templates in the form $pwg->l10n('edit') |
---|
1209 | */ |
---|
1210 | class PwgTemplateAdapter |
---|
1211 | { |
---|
1212 | /** |
---|
1213 | * @deprecated use "translate" modifier |
---|
1214 | */ |
---|
1215 | function l10n($text) |
---|
1216 | { |
---|
1217 | return l10n($text); |
---|
1218 | } |
---|
1219 | |
---|
1220 | /** |
---|
1221 | * @deprecated use "translate_dec" modifier |
---|
1222 | */ |
---|
1223 | function l10n_dec($s, $p, $v) |
---|
1224 | { |
---|
1225 | return l10n_dec($s, $p, $v); |
---|
1226 | } |
---|
1227 | |
---|
1228 | /** |
---|
1229 | * @deprecated use "translate" or "sprintf" modifier |
---|
1230 | */ |
---|
1231 | function sprintf() |
---|
1232 | { |
---|
1233 | $args = func_get_args(); |
---|
1234 | return call_user_func_array('sprintf', $args ); |
---|
1235 | } |
---|
1236 | |
---|
1237 | /** |
---|
1238 | * @param string $type |
---|
1239 | * @param array $img |
---|
1240 | * @return DerivativeImage |
---|
1241 | */ |
---|
1242 | function derivative($type, $img) |
---|
1243 | { |
---|
1244 | return new DerivativeImage($type, $img); |
---|
1245 | } |
---|
1246 | |
---|
1247 | /** |
---|
1248 | * @param string $type |
---|
1249 | * @param array $img |
---|
1250 | * @return string |
---|
1251 | */ |
---|
1252 | function derivative_url($type, $img) |
---|
1253 | { |
---|
1254 | return DerivativeImage::url($type, $img); |
---|
1255 | } |
---|
1256 | } |
---|
1257 | |
---|
1258 | |
---|
1259 | /** |
---|
1260 | * A Combinable represents a JS or CSS file ready for cobination and minification. |
---|
1261 | */ |
---|
1262 | class Combinable |
---|
1263 | { |
---|
1264 | /** @var string */ |
---|
1265 | public $id; |
---|
1266 | /** @var string */ |
---|
1267 | public $path; |
---|
1268 | /** @var string */ |
---|
1269 | public $version; |
---|
1270 | /** @var bool */ |
---|
1271 | public $is_template; |
---|
1272 | |
---|
1273 | /** |
---|
1274 | * @param string $id |
---|
1275 | * @param string $path |
---|
1276 | * @param string $version |
---|
1277 | */ |
---|
1278 | function __construct($id, $path, $version=0) |
---|
1279 | { |
---|
1280 | $this->id = $id; |
---|
1281 | $this->set_path($path); |
---|
1282 | $this->version = $version; |
---|
1283 | $this->is_template = false; |
---|
1284 | } |
---|
1285 | |
---|
1286 | /** |
---|
1287 | * @param string $path |
---|
1288 | */ |
---|
1289 | function set_path($path) |
---|
1290 | { |
---|
1291 | if (!empty($path)) |
---|
1292 | $this->path = $path; |
---|
1293 | } |
---|
1294 | |
---|
1295 | /** |
---|
1296 | * @return bool |
---|
1297 | */ |
---|
1298 | function is_remote() |
---|
1299 | { |
---|
1300 | return url_is_remote($this->path) || strncmp($this->path, '//', 2)==0; |
---|
1301 | } |
---|
1302 | } |
---|
1303 | |
---|
1304 | /** |
---|
1305 | * Implementation of Combinable for JS files. |
---|
1306 | */ |
---|
1307 | final class Script extends Combinable |
---|
1308 | { |
---|
1309 | /** @var int 0,1,2 */ |
---|
1310 | public $load_mode; |
---|
1311 | /** @var array */ |
---|
1312 | public $precedents; |
---|
1313 | /** @var array */ |
---|
1314 | public $extra; |
---|
1315 | |
---|
1316 | /** |
---|
1317 | * @param int 0,1,2 |
---|
1318 | * @param string $id |
---|
1319 | * @param string $path |
---|
1320 | * @param string $version |
---|
1321 | * @param array $precedents |
---|
1322 | */ |
---|
1323 | function __construct($load_mode, $id, $path, $version=0, $precedents=array()) |
---|
1324 | { |
---|
1325 | parent::__construct($id, $path, $version); |
---|
1326 | $this->load_mode = $load_mode; |
---|
1327 | $this->precedents = $precedents; |
---|
1328 | $this->extra = array(); |
---|
1329 | } |
---|
1330 | } |
---|
1331 | |
---|
1332 | /** |
---|
1333 | * Implementation of Combinable for CSS files. |
---|
1334 | */ |
---|
1335 | final class Css extends Combinable |
---|
1336 | { |
---|
1337 | /** @var int */ |
---|
1338 | public $order; |
---|
1339 | |
---|
1340 | /** |
---|
1341 | * @param string $id |
---|
1342 | * @param string $path |
---|
1343 | * @param string $version |
---|
1344 | * @param int $order |
---|
1345 | */ |
---|
1346 | function __construct($id, $path, $version=0, $order=0) |
---|
1347 | { |
---|
1348 | parent::__construct($id, $path, $version); |
---|
1349 | $this->order = $order; |
---|
1350 | } |
---|
1351 | } |
---|
1352 | |
---|
1353 | |
---|
1354 | /** |
---|
1355 | * Manages a list of CSS files and combining them in a unique file. |
---|
1356 | */ |
---|
1357 | class CssLoader |
---|
1358 | { |
---|
1359 | /** @param Css[] */ |
---|
1360 | private $registered_css; |
---|
1361 | /** @param int used to keep declaration order */ |
---|
1362 | private $counter; |
---|
1363 | |
---|
1364 | function __construct() |
---|
1365 | { |
---|
1366 | $this->clear(); |
---|
1367 | } |
---|
1368 | |
---|
1369 | function clear() |
---|
1370 | { |
---|
1371 | $this->registered_css = array(); |
---|
1372 | $this->counter = 0; |
---|
1373 | } |
---|
1374 | |
---|
1375 | /** |
---|
1376 | * @return Combinable[] array of combined CSS. |
---|
1377 | */ |
---|
1378 | function get_css() |
---|
1379 | { |
---|
1380 | uasort($this->registered_css, array('CssLoader', 'cmp_by_order')); |
---|
1381 | $combiner = new FileCombiner('css', $this->registered_css); |
---|
1382 | return $combiner->combine(); |
---|
1383 | } |
---|
1384 | |
---|
1385 | /** |
---|
1386 | * Callback for CSS files sorting. |
---|
1387 | */ |
---|
1388 | private static function cmp_by_order($a, $b) |
---|
1389 | { |
---|
1390 | return $a->order - $b->order; |
---|
1391 | } |
---|
1392 | |
---|
1393 | /** |
---|
1394 | * Adds a new file, if a file with the same $id already exsists, the one with |
---|
1395 | * the higher $order or higher $version is kept. |
---|
1396 | * |
---|
1397 | * @param string $id |
---|
1398 | * @param string $path |
---|
1399 | * @param string $version |
---|
1400 | * @param int $order |
---|
1401 | * @param bool $is_template |
---|
1402 | */ |
---|
1403 | function add($id, $path, $version=0, $order=0, $is_template=false) |
---|
1404 | { |
---|
1405 | if (!isset($this->registered_css[$id])) |
---|
1406 | { |
---|
1407 | // costum order as an higher impact than declaration order |
---|
1408 | $css = new Css($id, $path, $version, $order*1000+$this->counter); |
---|
1409 | $css->is_template = $is_template; |
---|
1410 | $this->registered_css[$id] = $css; |
---|
1411 | $this->counter++; |
---|
1412 | } |
---|
1413 | else |
---|
1414 | { |
---|
1415 | $css = $this->registered_css[$id]; |
---|
1416 | if ($css->order<$order*1000 || version_compare($css->version, $version)<0) |
---|
1417 | { |
---|
1418 | unset($this->registered_css[$id]); |
---|
1419 | $this->add($id, $path, $version, $order, $is_template); |
---|
1420 | } |
---|
1421 | } |
---|
1422 | } |
---|
1423 | } |
---|
1424 | |
---|
1425 | |
---|
1426 | /** |
---|
1427 | * Manage a list of required scripts for a page, by optimizing their loading location (head, footer, async) |
---|
1428 | * and later on by combining them in a unique file respecting at the same time dependencies. |
---|
1429 | */ |
---|
1430 | class ScriptLoader |
---|
1431 | { |
---|
1432 | /** @var Script[] */ |
---|
1433 | private $registered_scripts; |
---|
1434 | /** @var string[] */ |
---|
1435 | public $inline_scripts; |
---|
1436 | |
---|
1437 | /** @var bool */ |
---|
1438 | private $did_head; |
---|
1439 | /** @var bool */ |
---|
1440 | private $head_done_scripts; |
---|
1441 | /** @var bool */ |
---|
1442 | private $did_footer; |
---|
1443 | |
---|
1444 | private static $known_paths = array( |
---|
1445 | 'core.scripts' => 'themes/default/js/scripts.js', |
---|
1446 | 'jquery' => 'themes/default/js/jquery.min.js', |
---|
1447 | 'jquery.ui' => 'themes/default/js/ui/minified/jquery.ui.core.min.js', |
---|
1448 | 'jquery.ui.effect' => 'themes/default/js/ui/minified/jquery.ui.effect.min.js', |
---|
1449 | ); |
---|
1450 | |
---|
1451 | private static $ui_core_dependencies = array( |
---|
1452 | 'jquery.ui.widget' => array('jquery'), |
---|
1453 | 'jquery.ui.position' => array('jquery'), |
---|
1454 | 'jquery.ui.mouse' => array('jquery', 'jquery.ui', 'jquery.ui.widget'), |
---|
1455 | ); |
---|
1456 | |
---|
1457 | function __construct() |
---|
1458 | { |
---|
1459 | $this->clear(); |
---|
1460 | } |
---|
1461 | |
---|
1462 | function clear() |
---|
1463 | { |
---|
1464 | $this->registered_scripts = array(); |
---|
1465 | $this->inline_scripts = array(); |
---|
1466 | $this->head_done_scripts = array(); |
---|
1467 | $this->did_head = $this->did_footer = false; |
---|
1468 | } |
---|
1469 | |
---|
1470 | /** |
---|
1471 | * @return bool |
---|
1472 | */ |
---|
1473 | function did_head() |
---|
1474 | { |
---|
1475 | return $this->did_head; |
---|
1476 | } |
---|
1477 | |
---|
1478 | /** |
---|
1479 | * @return Script[] |
---|
1480 | */ |
---|
1481 | function get_all() |
---|
1482 | { |
---|
1483 | return $this->registered_scripts; |
---|
1484 | } |
---|
1485 | |
---|
1486 | /** |
---|
1487 | * @param string $code |
---|
1488 | * @param string[] $require |
---|
1489 | */ |
---|
1490 | function add_inline($code, $require) |
---|
1491 | { |
---|
1492 | !$this->did_footer || trigger_error("Attempt to add inline script but the footer has been written", E_USER_WARNING); |
---|
1493 | if(!empty($require)) |
---|
1494 | { |
---|
1495 | foreach ($require as $id) |
---|
1496 | { |
---|
1497 | if(!isset($this->registered_scripts[$id])) |
---|
1498 | $this->load_known_required_script($id, 1) or fatal_error("inline script not found require $id"); |
---|
1499 | $s = $this->registered_scripts[$id]; |
---|
1500 | if($s->load_mode==2) |
---|
1501 | $s->load_mode=1; // until now the implementation does not allow executing inline script depending on another async script |
---|
1502 | } |
---|
1503 | } |
---|
1504 | $this->inline_scripts[] = $code; |
---|
1505 | } |
---|
1506 | |
---|
1507 | /** |
---|
1508 | * @param string $id |
---|
1509 | * @param int $load_mode |
---|
1510 | * @param string[] $require |
---|
1511 | * @param string $path |
---|
1512 | * @param string $version |
---|
1513 | */ |
---|
1514 | function add($id, $load_mode, $require, $path, $version=0, $is_template=false) |
---|
1515 | { |
---|
1516 | if ($this->did_head && $load_mode==0) |
---|
1517 | { |
---|
1518 | trigger_error("Attempt to add script $id but the head has been written", E_USER_WARNING); |
---|
1519 | } |
---|
1520 | elseif ($this->did_footer) |
---|
1521 | { |
---|
1522 | trigger_error("Attempt to add script $id but the footer has been written", E_USER_WARNING); |
---|
1523 | } |
---|
1524 | if (! isset( $this->registered_scripts[$id] ) ) |
---|
1525 | { |
---|
1526 | $script = new Script($load_mode, $id, $path, $version, $require); |
---|
1527 | $script->is_template = $is_template; |
---|
1528 | self::fill_well_known($id, $script); |
---|
1529 | $this->registered_scripts[$id] = $script; |
---|
1530 | |
---|
1531 | // Load or modify all UI core files |
---|
1532 | if ($id == 'jquery.ui' and $script->path == self::$known_paths['jquery.ui']) |
---|
1533 | { |
---|
1534 | foreach (self::$ui_core_dependencies as $script_id => $required_ids) |
---|
1535 | $this->add($script_id, $load_mode, $required_ids, null, $version); |
---|
1536 | } |
---|
1537 | |
---|
1538 | // Try to load undefined required script |
---|
1539 | foreach ($script->precedents as $script_id) |
---|
1540 | { |
---|
1541 | if (! isset( $this->registered_scripts[$script_id] ) ) |
---|
1542 | $this->load_known_required_script($script_id, $load_mode); |
---|
1543 | } |
---|
1544 | } |
---|
1545 | else |
---|
1546 | { |
---|
1547 | $script = $this->registered_scripts[$id]; |
---|
1548 | if (count($require)) |
---|
1549 | { |
---|
1550 | $script->precedents = array_unique( array_merge($script->precedents, $require) ); |
---|
1551 | } |
---|
1552 | $script->set_path($path); |
---|
1553 | if ($version && version_compare($script->version, $version)<0 ) |
---|
1554 | $script->version = $version; |
---|
1555 | if ($load_mode < $script->load_mode) |
---|
1556 | $script->load_mode = $load_mode; |
---|
1557 | } |
---|
1558 | } |
---|
1559 | |
---|
1560 | /** |
---|
1561 | * Returns combined scripts loaded in header. |
---|
1562 | * |
---|
1563 | * @return Combinable[] |
---|
1564 | */ |
---|
1565 | function get_head_scripts() |
---|
1566 | { |
---|
1567 | self::check_load_dep($this->registered_scripts); |
---|
1568 | foreach( array_keys($this->registered_scripts) as $id ) |
---|
1569 | { |
---|
1570 | $this->compute_script_topological_order($id); |
---|
1571 | } |
---|
1572 | |
---|
1573 | uasort($this->registered_scripts, array('ScriptLoader', 'cmp_by_mode_and_order')); |
---|
1574 | |
---|
1575 | foreach( $this->registered_scripts as $id => $script) |
---|
1576 | { |
---|
1577 | if ($script->load_mode > 0) |
---|
1578 | break; |
---|
1579 | if ( !empty($script->path) ) |
---|
1580 | $this->head_done_scripts[$id] = $script; |
---|
1581 | else |
---|
1582 | trigger_error("Script $id has an undefined path", E_USER_WARNING); |
---|
1583 | } |
---|
1584 | $this->did_head = true; |
---|
1585 | return self::do_combine($this->head_done_scripts, 0); |
---|
1586 | } |
---|
1587 | |
---|
1588 | /** |
---|
1589 | * Returns combined scripts loaded in footer. |
---|
1590 | * |
---|
1591 | * @return Combinable[] |
---|
1592 | */ |
---|
1593 | function get_footer_scripts() |
---|
1594 | { |
---|
1595 | if (!$this->did_head) |
---|
1596 | { |
---|
1597 | self::check_load_dep($this->registered_scripts); |
---|
1598 | } |
---|
1599 | $this->did_footer = true; |
---|
1600 | $todo = array(); |
---|
1601 | foreach( $this->registered_scripts as $id => $script) |
---|
1602 | { |
---|
1603 | if (!isset($this->head_done_scripts[$id])) |
---|
1604 | { |
---|
1605 | $todo[$id] = $script; |
---|
1606 | } |
---|
1607 | } |
---|
1608 | |
---|
1609 | foreach( array_keys($todo) as $id ) |
---|
1610 | { |
---|
1611 | $this->compute_script_topological_order($id); |
---|
1612 | } |
---|
1613 | |
---|
1614 | uasort($todo, array('ScriptLoader', 'cmp_by_mode_and_order')); |
---|
1615 | |
---|
1616 | $result = array( array(), array() ); |
---|
1617 | foreach( $todo as $id => $script) |
---|
1618 | { |
---|
1619 | $result[$script->load_mode-1][$id] = $script; |
---|
1620 | } |
---|
1621 | return array( self::do_combine($result[0],1), self::do_combine($result[1],2) ); |
---|
1622 | } |
---|
1623 | |
---|
1624 | /** |
---|
1625 | * @param Script[] $scripts |
---|
1626 | * @param int $load_mode |
---|
1627 | * @return Combinable[] |
---|
1628 | */ |
---|
1629 | private static function do_combine($scripts, $load_mode) |
---|
1630 | { |
---|
1631 | $combiner = new FileCombiner('js', $scripts); |
---|
1632 | return $combiner->combine(); |
---|
1633 | } |
---|
1634 | |
---|
1635 | /** |
---|
1636 | * Checks dependencies among Scripts. |
---|
1637 | * Checks that if B depends on A, then B->load_mode >= A->load_mode in order to respect execution order. |
---|
1638 | * |
---|
1639 | * @param Script[] $scripts |
---|
1640 | */ |
---|
1641 | private static function check_load_dep($scripts) |
---|
1642 | { |
---|
1643 | global $conf; |
---|
1644 | do |
---|
1645 | { |
---|
1646 | $changed = false; |
---|
1647 | foreach( $scripts as $id => $script) |
---|
1648 | { |
---|
1649 | $load = $script->load_mode; |
---|
1650 | foreach( $script->precedents as $precedent) |
---|
1651 | { |
---|
1652 | if ( !isset($scripts[$precedent] ) ) |
---|
1653 | continue; |
---|
1654 | if ( $scripts[$precedent]->load_mode > $load ) |
---|
1655 | { |
---|
1656 | $scripts[$precedent]->load_mode = $load; |
---|
1657 | $changed = true; |
---|
1658 | } |
---|
1659 | if ($load==2 && $scripts[$precedent]->load_mode==2 && ($scripts[$precedent]->is_remote() or !$conf['template_combine_files']) ) |
---|
1660 | {// we are async -> a predecessor cannot be async unlesss it can be merged; otherwise script execution order is not guaranteed |
---|
1661 | $scripts[$precedent]->load_mode = 1; |
---|
1662 | $changed = true; |
---|
1663 | } |
---|
1664 | } |
---|
1665 | } |
---|
1666 | } |
---|
1667 | while ($changed); |
---|
1668 | } |
---|
1669 | |
---|
1670 | /** |
---|
1671 | * Fill a script dependancies with the known jQuery UI scripts. |
---|
1672 | * |
---|
1673 | * @param string $id in FileCombiner::$known_paths |
---|
1674 | * @param Script $script |
---|
1675 | */ |
---|
1676 | private static function fill_well_known($id, $script) |
---|
1677 | { |
---|
1678 | if ( empty($script->path) && isset(self::$known_paths[$id])) |
---|
1679 | { |
---|
1680 | $script->path = self::$known_paths[$id]; |
---|
1681 | } |
---|
1682 | if ( strncmp($id, 'jquery.', 7)==0 ) |
---|
1683 | { |
---|
1684 | $required_ids = array('jquery'); |
---|
1685 | |
---|
1686 | if ( strncmp($id, 'jquery.ui.effect-', 17)==0 ) |
---|
1687 | { |
---|
1688 | $required_ids = array('jquery', 'jquery.ui.effect'); |
---|
1689 | |
---|
1690 | if ( empty($script->path) ) |
---|
1691 | $script->path = dirname(self::$known_paths['jquery.ui.effect'])."/$id.min.js"; |
---|
1692 | } |
---|
1693 | elseif ( strncmp($id, 'jquery.ui.', 10)==0 ) |
---|
1694 | { |
---|
1695 | if ( !isset(self::$ui_core_dependencies[$id]) ) |
---|
1696 | $required_ids = array_merge(array('jquery', 'jquery.ui'), array_keys(self::$ui_core_dependencies)); |
---|
1697 | |
---|
1698 | if ( empty($script->path) ) |
---|
1699 | $script->path = dirname(self::$known_paths['jquery.ui'])."/$id.min.js"; |
---|
1700 | } |
---|
1701 | |
---|
1702 | foreach ($required_ids as $required_id) |
---|
1703 | { |
---|
1704 | if ( !in_array($required_id, $script->precedents ) ) |
---|
1705 | $script->precedents[] = $required_id; |
---|
1706 | } |
---|
1707 | } |
---|
1708 | } |
---|
1709 | |
---|
1710 | /** |
---|
1711 | * Add a known jQuery UI script to loaded scripts. |
---|
1712 | * |
---|
1713 | * @param string $id in FileCombiner::$known_paths |
---|
1714 | * @param int $load_mode |
---|
1715 | * @return bool |
---|
1716 | */ |
---|
1717 | private function load_known_required_script($id, $load_mode) |
---|
1718 | { |
---|
1719 | if ( isset(self::$known_paths[$id]) or strncmp($id, 'jquery.ui.', 10)==0 ) |
---|
1720 | { |
---|
1721 | $this->add($id, $load_mode, array(), null); |
---|
1722 | return true; |
---|
1723 | } |
---|
1724 | return false; |
---|
1725 | } |
---|
1726 | |
---|
1727 | /** |
---|
1728 | * Compute script order depending on dependencies. |
---|
1729 | * Assigned to $script->extra['order']. |
---|
1730 | * |
---|
1731 | * @param string $script_id |
---|
1732 | * @param int $recursion_limiter |
---|
1733 | * @return int |
---|
1734 | */ |
---|
1735 | private function compute_script_topological_order($script_id, $recursion_limiter=0) |
---|
1736 | { |
---|
1737 | if (!isset($this->registered_scripts[$script_id])) |
---|
1738 | { |
---|
1739 | trigger_error("Undefined script $script_id is required by someone", E_USER_WARNING); |
---|
1740 | return 0; |
---|
1741 | } |
---|
1742 | $recursion_limiter<5 or fatal_error("combined script circular dependency"); |
---|
1743 | $script = $this->registered_scripts[$script_id]; |
---|
1744 | if (isset($script->extra['order'])) |
---|
1745 | return $script->extra['order']; |
---|
1746 | if (count($script->precedents) == 0) |
---|
1747 | return ($script->extra['order'] = 0); |
---|
1748 | $max = 0; |
---|
1749 | foreach( $script->precedents as $precedent) |
---|
1750 | $max = max($max, $this->compute_script_topological_order($precedent, $recursion_limiter+1) ); |
---|
1751 | $max++; |
---|
1752 | return ($script->extra['order'] = $max); |
---|
1753 | } |
---|
1754 | |
---|
1755 | /** |
---|
1756 | * Callback for scripts sorter. |
---|
1757 | */ |
---|
1758 | private static function cmp_by_mode_and_order($s1, $s2) |
---|
1759 | { |
---|
1760 | $ret = $s1->load_mode - $s2->load_mode; |
---|
1761 | if ($ret) return $ret; |
---|
1762 | |
---|
1763 | $ret = $s1->extra['order'] - $s2->extra['order']; |
---|
1764 | if ($ret) return $ret; |
---|
1765 | |
---|
1766 | if ($s1->extra['order']==0 and ($s1->is_remote() xor $s2->is_remote()) ) |
---|
1767 | { |
---|
1768 | return $s1->is_remote() ? -1 : 1; |
---|
1769 | } |
---|
1770 | return strcmp($s1->id,$s2->id); |
---|
1771 | } |
---|
1772 | } |
---|
1773 | |
---|
1774 | |
---|
1775 | /** |
---|
1776 | * Allows merging of javascript and css files into a single one. |
---|
1777 | */ |
---|
1778 | final class FileCombiner |
---|
1779 | { |
---|
1780 | /** @var string 'js' or 'css' */ |
---|
1781 | private $type; |
---|
1782 | /** @var bool */ |
---|
1783 | private $is_css; |
---|
1784 | /** @var Combinable[] */ |
---|
1785 | private $combinables; |
---|
1786 | |
---|
1787 | /** |
---|
1788 | * @param string $type 'js' or 'css' |
---|
1789 | * @param Combinable[] $combinables |
---|
1790 | */ |
---|
1791 | function __construct($type, $combinables=array()) |
---|
1792 | { |
---|
1793 | $this->type = $type; |
---|
1794 | $this->is_css = $type=='css'; |
---|
1795 | $this->combinables = $combinables; |
---|
1796 | } |
---|
1797 | |
---|
1798 | /** |
---|
1799 | * Deletes all combined files from cache directory. |
---|
1800 | */ |
---|
1801 | static function clear_combined_files() |
---|
1802 | { |
---|
1803 | $dir = opendir(PHPWG_ROOT_PATH.PWG_COMBINED_DIR); |
---|
1804 | while ($file = readdir($dir)) |
---|
1805 | { |
---|
1806 | if ( get_extension($file)=='js' || get_extension($file)=='css') |
---|
1807 | unlink(PHPWG_ROOT_PATH.PWG_COMBINED_DIR.$file); |
---|
1808 | } |
---|
1809 | closedir($dir); |
---|
1810 | } |
---|
1811 | |
---|
1812 | /** |
---|
1813 | * @param Combinable|Combinable[] $combinable |
---|
1814 | */ |
---|
1815 | function add($combinable) |
---|
1816 | { |
---|
1817 | if (is_array($combinable)) |
---|
1818 | { |
---|
1819 | $this->combinables = array_merge($this->combinables, $combinable); |
---|
1820 | } |
---|
1821 | else |
---|
1822 | { |
---|
1823 | $this->combinables[] = $combinable; |
---|
1824 | } |
---|
1825 | } |
---|
1826 | |
---|
1827 | /** |
---|
1828 | * @return Combinable[] |
---|
1829 | */ |
---|
1830 | function combine() |
---|
1831 | { |
---|
1832 | global $conf; |
---|
1833 | $force = false; |
---|
1834 | if (is_admin() && ($this->is_css || !$conf['template_compile_check']) ) |
---|
1835 | { |
---|
1836 | $force = (isset($_SERVER['HTTP_CACHE_CONTROL']) && strpos($_SERVER['HTTP_CACHE_CONTROL'], 'max-age=0') !== false) |
---|
1837 | || (isset($_SERVER['HTTP_PRAGMA']) && strpos($_SERVER['HTTP_PRAGMA'], 'no-cache')); |
---|
1838 | } |
---|
1839 | |
---|
1840 | $result = array(); |
---|
1841 | $pending = array(); |
---|
1842 | $ini_key = $this->is_css ? array(get_absolute_root_url(false)): array(); //because for css we modify bg url; |
---|
1843 | $key = $ini_key; |
---|
1844 | |
---|
1845 | foreach ($this->combinables as $combinable) |
---|
1846 | { |
---|
1847 | if ($combinable->is_remote()) |
---|
1848 | { |
---|
1849 | $this->flush_pending($result, $pending, $key, $force); |
---|
1850 | $key = $ini_key; |
---|
1851 | $result[] = $combinable; |
---|
1852 | continue; |
---|
1853 | } |
---|
1854 | elseif (!$conf['template_combine_files']) |
---|
1855 | { |
---|
1856 | $this->flush_pending($result, $pending, $key, $force); |
---|
1857 | $key = $ini_key; |
---|
1858 | } |
---|
1859 | |
---|
1860 | $key[] = $combinable->path; |
---|
1861 | $key[] = $combinable->version; |
---|
1862 | if ($conf['template_compile_check']) |
---|
1863 | $key[] = filemtime( PHPWG_ROOT_PATH . $combinable->path ); |
---|
1864 | $pending[] = $combinable; |
---|
1865 | } |
---|
1866 | $this->flush_pending($result, $pending, $key, $force); |
---|
1867 | return $result; |
---|
1868 | } |
---|
1869 | |
---|
1870 | /** |
---|
1871 | * Process a set of pending files. |
---|
1872 | * |
---|
1873 | * @param array &$result |
---|
1874 | * @param array &$pending |
---|
1875 | * @param string[] $key |
---|
1876 | * @param bool $force |
---|
1877 | */ |
---|
1878 | private function flush_pending(&$result, &$pending, $key, $force) |
---|
1879 | { |
---|
1880 | if (count($pending)>1) |
---|
1881 | { |
---|
1882 | $key = join('>', $key); |
---|
1883 | $file = PWG_COMBINED_DIR . base_convert(crc32($key),10,36) . '.' . $this->type; |
---|
1884 | if ($force || !file_exists(PHPWG_ROOT_PATH.$file) ) |
---|
1885 | { |
---|
1886 | $output = ''; |
---|
1887 | foreach ($pending as $combinable) |
---|
1888 | { |
---|
1889 | $output .= "/*BEGIN $combinable->path */\n"; |
---|
1890 | $output .= $this->process_combinable($combinable, true, $force); |
---|
1891 | $output .= "\n"; |
---|
1892 | } |
---|
1893 | mkgetdir( dirname(PHPWG_ROOT_PATH.$file) ); |
---|
1894 | file_put_contents( PHPWG_ROOT_PATH.$file, $output ); |
---|
1895 | @chmod(PHPWG_ROOT_PATH.$file, 0644); |
---|
1896 | } |
---|
1897 | $result[] = new Combinable("combi", $file, false); |
---|
1898 | } |
---|
1899 | elseif ( count($pending)==1) |
---|
1900 | { |
---|
1901 | $this->process_combinable($pending[0], false, $force); |
---|
1902 | $result[] = $pending[0]; |
---|
1903 | } |
---|
1904 | $key = array(); |
---|
1905 | $pending = array(); |
---|
1906 | } |
---|
1907 | |
---|
1908 | /** |
---|
1909 | * Process one combinable file. |
---|
1910 | * |
---|
1911 | * @param Combinable $combinable |
---|
1912 | * @param bool $return_content |
---|
1913 | * @param bool $force |
---|
1914 | * @return null|string |
---|
1915 | */ |
---|
1916 | private function process_combinable($combinable, $return_content, $force) |
---|
1917 | { |
---|
1918 | global $conf; |
---|
1919 | if ($combinable->is_template) |
---|
1920 | { |
---|
1921 | if (!$return_content) |
---|
1922 | { |
---|
1923 | $key = array($combinable->path, $combinable->version); |
---|
1924 | if ($conf['template_compile_check']) |
---|
1925 | $key[] = filemtime( PHPWG_ROOT_PATH . $combinable->path ); |
---|
1926 | $file = PWG_COMBINED_DIR . 't' . base_convert(crc32(implode(',',$key)),10,36) . '.' . $this->type; |
---|
1927 | if (!$force && file_exists(PHPWG_ROOT_PATH.$file) ) |
---|
1928 | { |
---|
1929 | $combinable->path = $file; |
---|
1930 | $combinable->version = false; |
---|
1931 | return; |
---|
1932 | } |
---|
1933 | } |
---|
1934 | |
---|
1935 | global $template; |
---|
1936 | $handle = $this->type. '.' .$combinable->id; |
---|
1937 | $template->set_filename($handle, realpath(PHPWG_ROOT_PATH.$combinable->path)); |
---|
1938 | trigger_action( 'combinable_preparse', $template, $combinable, $this); //allow themes and plugins to set their own vars to template ... |
---|
1939 | $content = $template->parse($handle, true); |
---|
1940 | |
---|
1941 | if ($this->is_css) |
---|
1942 | $content = self::process_css($content, $combinable->path ); |
---|
1943 | else |
---|
1944 | $content = self::process_js($content, $combinable->path ); |
---|
1945 | |
---|
1946 | if ($return_content) |
---|
1947 | return $content; |
---|
1948 | file_put_contents( PHPWG_ROOT_PATH.$file, $content ); |
---|
1949 | $combinable->path = $file; |
---|
1950 | } |
---|
1951 | elseif ($return_content) |
---|
1952 | { |
---|
1953 | $content = file_get_contents(PHPWG_ROOT_PATH . $combinable->path); |
---|
1954 | if ($this->is_css) |
---|
1955 | $content = self::process_css($content, $combinable->path ); |
---|
1956 | else |
---|
1957 | $content = self::process_js($content, $combinable->path ); |
---|
1958 | return $content; |
---|
1959 | } |
---|
1960 | } |
---|
1961 | |
---|
1962 | /** |
---|
1963 | * Process a JS file. |
---|
1964 | * |
---|
1965 | * @param string $js file content |
---|
1966 | * @param string $file |
---|
1967 | * @return string |
---|
1968 | */ |
---|
1969 | private static function process_js($js, $file) |
---|
1970 | { |
---|
1971 | if (strpos($file, '.min')===false and strpos($file, '.packed')===false ) |
---|
1972 | { |
---|
1973 | require_once(PHPWG_ROOT_PATH.'include/jshrink.class.php'); |
---|
1974 | try { $js = JShrink_Minifier::minify($js); } catch(Exception $e) {} |
---|
1975 | } |
---|
1976 | return trim($js, " \t\r\n;").";\n"; |
---|
1977 | } |
---|
1978 | |
---|
1979 | /** |
---|
1980 | * Process a CSS file. |
---|
1981 | * |
---|
1982 | * @param string $css file content |
---|
1983 | * @param string $file |
---|
1984 | * @return string |
---|
1985 | */ |
---|
1986 | private static function process_css($css, $file) |
---|
1987 | { |
---|
1988 | $css = self::process_css_rec($css, dirname($file)); |
---|
1989 | if (strpos($file, '.min')===false and version_compare(PHP_VERSION, '5.2.4', '>=')) |
---|
1990 | { |
---|
1991 | require_once(PHPWG_ROOT_PATH.'include/cssmin.class.php'); |
---|
1992 | $css = CssMin::minify($css, array('Variables'=>false)); |
---|
1993 | } |
---|
1994 | $css = trigger_event('combined_css_postfilter', $css); |
---|
1995 | return $css; |
---|
1996 | } |
---|
1997 | |
---|
1998 | /** |
---|
1999 | * Resolves relative links in CSS file. |
---|
2000 | * |
---|
2001 | * @param string $css file content |
---|
2002 | * @param string $dir |
---|
2003 | * @return string |
---|
2004 | */ |
---|
2005 | private static function process_css_rec($css, $dir) |
---|
2006 | { |
---|
2007 | static $PATTERN_URL = "#url\(\s*['|\"]{0,1}(.*?)['|\"]{0,1}\s*\)#"; |
---|
2008 | static $PATTERN_IMPORT = "#@import\s*['|\"]{0,1}(.*?)['|\"]{0,1};#"; |
---|
2009 | |
---|
2010 | if (preg_match_all($PATTERN_URL, $css, $matches, PREG_SET_ORDER)) |
---|
2011 | { |
---|
2012 | $search = $replace = array(); |
---|
2013 | foreach ($matches as $match) |
---|
2014 | { |
---|
2015 | if ( !url_is_remote($match[1]) && $match[1][0] != '/' && strpos($match[1], 'data:image/')===false) |
---|
2016 | { |
---|
2017 | $relative = $dir . "/$match[1]"; |
---|
2018 | $search[] = $match[0]; |
---|
2019 | $replace[] = 'url('.embellish_url(get_absolute_root_url(false).$relative).')'; |
---|
2020 | } |
---|
2021 | } |
---|
2022 | $css = str_replace($search, $replace, $css); |
---|
2023 | } |
---|
2024 | |
---|
2025 | if (preg_match_all($PATTERN_IMPORT, $css, $matches, PREG_SET_ORDER)) |
---|
2026 | { |
---|
2027 | $search = $replace = array(); |
---|
2028 | foreach ($matches as $match) |
---|
2029 | { |
---|
2030 | $search[] = $match[0]; |
---|
2031 | $sub_css = file_get_contents(PHPWG_ROOT_PATH . $dir . "/$match[1]"); |
---|
2032 | $replace[] = self::process_css_rec($sub_css, dirname($dir . "/$match[1]") ); |
---|
2033 | } |
---|
2034 | $css = str_replace($search, $replace, $css); |
---|
2035 | } |
---|
2036 | return $css; |
---|
2037 | } |
---|
2038 | } |
---|
2039 | |
---|
2040 | ?> |
---|