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 | } |
---|
802 | |
---|
803 | /** |
---|
804 | * The "get_combined_scripts" function returns HTML tag of combined scripts. |
---|
805 | * It can returns a placeholder for delayed JS files combination and minification. |
---|
806 | * |
---|
807 | * @param array $params |
---|
808 | * - load (required) |
---|
809 | */ |
---|
810 | function func_get_combined_scripts($params) |
---|
811 | { |
---|
812 | if (!isset($params['load'])) |
---|
813 | { |
---|
814 | trigger_error("get_combined_scripts: missing 'load' parameter", E_USER_ERROR); |
---|
815 | } |
---|
816 | $load = $params['load']=='header' ? 0 : 1; |
---|
817 | $content = array(); |
---|
818 | |
---|
819 | if ($load==0) |
---|
820 | { |
---|
821 | return self::COMBINED_SCRIPTS_TAG; |
---|
822 | } |
---|
823 | else |
---|
824 | { |
---|
825 | $scripts = $this->scriptLoader->get_footer_scripts(); |
---|
826 | foreach ($scripts[0] as $script) |
---|
827 | { |
---|
828 | $content[]= |
---|
829 | '<script type="text/javascript" src="' |
---|
830 | . self::make_script_src($script) |
---|
831 | .'"></script>'; |
---|
832 | } |
---|
833 | if (count($this->scriptLoader->inline_scripts)) |
---|
834 | { |
---|
835 | $content[]= '<script type="text/javascript">//<![CDATA[ |
---|
836 | '; |
---|
837 | $content = array_merge($content, $this->scriptLoader->inline_scripts); |
---|
838 | $content[]= '//]]></script>'; |
---|
839 | } |
---|
840 | |
---|
841 | if (count($scripts[1])) |
---|
842 | { |
---|
843 | $content[]= '<script type="text/javascript">'; |
---|
844 | $content[]= '(function() { |
---|
845 | var s,after = document.getElementsByTagName(\'script\')[document.getElementsByTagName(\'script\').length-1];'; |
---|
846 | foreach ($scripts[1] as $id => $script) |
---|
847 | { |
---|
848 | $content[]= |
---|
849 | 's=document.createElement(\'script\'); s.type=\'text/javascript\'; s.async=true; s.src=\'' |
---|
850 | . self::make_script_src($script) |
---|
851 | .'\';'; |
---|
852 | $content[]= 'after = after.parentNode.insertBefore(s, after);'; |
---|
853 | } |
---|
854 | $content[]= '})();'; |
---|
855 | $content[]= '</script>'; |
---|
856 | } |
---|
857 | } |
---|
858 | return implode("\n", $content); |
---|
859 | } |
---|
860 | |
---|
861 | /** |
---|
862 | * Returns clean relative URL to script file. |
---|
863 | * |
---|
864 | * @param Combinable $script |
---|
865 | * @return string |
---|
866 | */ |
---|
867 | private static function make_script_src($script) |
---|
868 | { |
---|
869 | $ret = ''; |
---|
870 | if ( $script->is_remote() ) |
---|
871 | $ret = $script->path; |
---|
872 | else |
---|
873 | { |
---|
874 | $ret = get_root_url().$script->path; |
---|
875 | if ($script->version!==false) |
---|
876 | { |
---|
877 | $ret.= '?v'. ($script->version ? $script->version : PHPWG_VERSION); |
---|
878 | } |
---|
879 | } |
---|
880 | // trigger the event for eventual use of a cdn |
---|
881 | $ret = trigger_event('combined_script', $ret, $script); |
---|
882 | return embellish_url($ret); |
---|
883 | } |
---|
884 | |
---|
885 | /** |
---|
886 | * The "footer_script" block allows to add runtime script in the HTML page. |
---|
887 | * |
---|
888 | * @param array $params |
---|
889 | * - require (optional) comma separated list of script ids |
---|
890 | * @param string $content |
---|
891 | */ |
---|
892 | function block_footer_script($params, $content) |
---|
893 | { |
---|
894 | $content = trim($content); |
---|
895 | if ( !empty($content) ) |
---|
896 | { // second call |
---|
897 | |
---|
898 | $this->scriptLoader->add_inline( |
---|
899 | $content, |
---|
900 | empty($params['require']) ? array() : explode(',', $params['require']) |
---|
901 | ); |
---|
902 | } |
---|
903 | } |
---|
904 | |
---|
905 | /** |
---|
906 | * The "combine_css" function allows inclusion of a css file in the current page. |
---|
907 | * The engine will combine several css files into a single one. |
---|
908 | * |
---|
909 | * @param array $params |
---|
910 | * - id (optional) used to deal with multiple inclusions from plugins |
---|
911 | * - path (required) |
---|
912 | * - version (optional) used to force a browser refresh |
---|
913 | * - order (optional) |
---|
914 | * - template (optional) set to true to allow smarty syntax in the css file |
---|
915 | */ |
---|
916 | function func_combine_css($params) |
---|
917 | { |
---|
918 | if (empty($params['path'])) |
---|
919 | { |
---|
920 | fatal_error('combine_css missing path'); |
---|
921 | } |
---|
922 | |
---|
923 | if (!isset($params['id'])) |
---|
924 | { |
---|
925 | $params['id'] = md5($params['path']); |
---|
926 | } |
---|
927 | |
---|
928 | $this->cssLoader->add($params['id'], $params['path'], isset($params['version']) ? $params['version'] : 0, (int)@$params['order'], (bool)@$params['template']); |
---|
929 | } |
---|
930 | |
---|
931 | /** |
---|
932 | * The "get_combined_scripts" function returns a placeholder for delayed |
---|
933 | * CSS files combination and minification. |
---|
934 | * |
---|
935 | * @param array $params (unused) |
---|
936 | */ |
---|
937 | function func_get_combined_css($params) |
---|
938 | { |
---|
939 | return self::COMBINED_CSS_TAG; |
---|
940 | } |
---|
941 | |
---|
942 | /** |
---|
943 | * Declares a Smarty prefilter from a plugin, allowing it to modify template |
---|
944 | * source before compilation and without changing core files. |
---|
945 | * They will be processed by weight ascending. |
---|
946 | * @see http://www.smarty.net/manual/en/advanced.features.prefilters.php |
---|
947 | * |
---|
948 | * @param string $handle |
---|
949 | * @param Callable $callback |
---|
950 | * @param int $weight |
---|
951 | */ |
---|
952 | function set_prefilter($handle, $callback, $weight=50) |
---|
953 | { |
---|
954 | $this->external_filters[$handle][$weight][] = array('pre', $callback); |
---|
955 | ksort($this->external_filters[$handle]); |
---|
956 | } |
---|
957 | |
---|
958 | /** |
---|
959 | * Declares a Smarty postfilter. |
---|
960 | * They will be processed by weight ascending. |
---|
961 | * @see http://www.smarty.net/manual/en/advanced.features.postfilters.php |
---|
962 | * |
---|
963 | * @param string $handle |
---|
964 | * @param Callable $callback |
---|
965 | * @param int $weight |
---|
966 | */ |
---|
967 | function set_postfilter($handle, $callback, $weight=50) |
---|
968 | { |
---|
969 | $this->external_filters[$handle][$weight][] = array('post', $callback); |
---|
970 | ksort($this->external_filters[$handle]); |
---|
971 | } |
---|
972 | |
---|
973 | /** |
---|
974 | * Declares a Smarty outputfilter. |
---|
975 | * They will be processed by weight ascending. |
---|
976 | * @see http://www.smarty.net/manual/en/advanced.features.outputfilters.php |
---|
977 | * |
---|
978 | * @param string $handle |
---|
979 | * @param Callable $callback |
---|
980 | * @param int $weight |
---|
981 | */ |
---|
982 | function set_outputfilter($handle, $callback, $weight=50) |
---|
983 | { |
---|
984 | $this->external_filters[$handle][$weight][] = array('output', $callback); |
---|
985 | ksort($this->external_filters[$handle]); |
---|
986 | } |
---|
987 | |
---|
988 | /** |
---|
989 | * Register the filters for the tpl file. |
---|
990 | * |
---|
991 | * @param string $handle |
---|
992 | */ |
---|
993 | function load_external_filters($handle) |
---|
994 | { |
---|
995 | if (isset($this->external_filters[$handle])) |
---|
996 | { |
---|
997 | $compile_id = ''; |
---|
998 | foreach ($this->external_filters[$handle] as $filters) |
---|
999 | { |
---|
1000 | foreach ($filters as $filter) |
---|
1001 | { |
---|
1002 | list($type, $callback) = $filter; |
---|
1003 | $compile_id .= $type.( is_array($callback) ? implode('', $callback) : $callback ); |
---|
1004 | $this->smarty->registerFilter($type, $callback); |
---|
1005 | } |
---|
1006 | } |
---|
1007 | $this->smarty->compile_id .= '.'.base_convert(crc32($compile_id), 10, 36); |
---|
1008 | } |
---|
1009 | } |
---|
1010 | |
---|
1011 | /** |
---|
1012 | * Unregister the filters for the tpl file. |
---|
1013 | * |
---|
1014 | * @param string $handle |
---|
1015 | */ |
---|
1016 | function unload_external_filters($handle) |
---|
1017 | { |
---|
1018 | if (isset($this->external_filters[$handle])) |
---|
1019 | { |
---|
1020 | foreach ($this->external_filters[$handle] as $filters) |
---|
1021 | { |
---|
1022 | foreach ($filters as $filter) |
---|
1023 | { |
---|
1024 | list($type, $callback) = $filter; |
---|
1025 | $this->smarty->unregisterFilter($type, $callback); |
---|
1026 | } |
---|
1027 | } |
---|
1028 | } |
---|
1029 | } |
---|
1030 | |
---|
1031 | /** |
---|
1032 | * @toto : description of Template::prefilter_white_space |
---|
1033 | * |
---|
1034 | * @param string $source |
---|
1035 | * @param Smarty $smarty |
---|
1036 | * @param return string |
---|
1037 | */ |
---|
1038 | static function prefilter_white_space($source, $smarty) |
---|
1039 | { |
---|
1040 | $ld = $smarty->left_delimiter; |
---|
1041 | $rd = $smarty->right_delimiter; |
---|
1042 | $ldq = preg_quote($ld, '#'); |
---|
1043 | $rdq = preg_quote($rd, '#'); |
---|
1044 | |
---|
1045 | $regex = array(); |
---|
1046 | $tags = array('if','foreach','section','footer_script'); |
---|
1047 | foreach($tags as $tag) |
---|
1048 | { |
---|
1049 | $regex[] = "#^[ \t]+($ldq$tag"."[^$ld$rd]*$rdq)\s*$#m"; |
---|
1050 | $regex[] = "#^[ \t]+($ldq/$tag$rdq)\s*$#m"; |
---|
1051 | } |
---|
1052 | $tags = array('include','else','combine_script','html_head'); |
---|
1053 | foreach($tags as $tag) |
---|
1054 | { |
---|
1055 | $regex[] = "#^[ \t]+($ldq$tag"."[^$ld$rd]*$rdq)\s*$#m"; |
---|
1056 | } |
---|
1057 | $source = preg_replace( $regex, "$1", $source); |
---|
1058 | return $source; |
---|
1059 | } |
---|
1060 | |
---|
1061 | /** |
---|
1062 | * Postfilter used when $conf['compiled_template_cache_language'] is true. |
---|
1063 | * |
---|
1064 | * @param string $source |
---|
1065 | * @param Smarty $smarty |
---|
1066 | * @param return string |
---|
1067 | */ |
---|
1068 | static function postfilter_language($source, $smarty) |
---|
1069 | { |
---|
1070 | // replaces echo PHP_STRING_LITERAL; with the string literal value |
---|
1071 | $source = preg_replace_callback( |
---|
1072 | '/\\<\\?php echo ((?:\'(?:(?:\\\\.)|[^\'])*\')|(?:"(?:(?:\\\\.)|[^"])*"));\\?\\>\\n/', |
---|
1073 | create_function('$matches', 'eval(\'$tmp=\'.$matches[1].\';\');return $tmp;'), |
---|
1074 | $source); |
---|
1075 | return $source; |
---|
1076 | } |
---|
1077 | |
---|
1078 | /** |
---|
1079 | * Prefilter used to add theme local CSS files. |
---|
1080 | * |
---|
1081 | * @param string $source |
---|
1082 | * @param Smarty $smarty |
---|
1083 | * @param return string |
---|
1084 | */ |
---|
1085 | static function prefilter_local_css($source, $smarty) |
---|
1086 | { |
---|
1087 | $css = array(); |
---|
1088 | foreach ($smarty->getTemplateVars('themes') as $theme) |
---|
1089 | { |
---|
1090 | $f = PWG_LOCAL_DIR.'css/'.$theme['id'].'-rules.css'; |
---|
1091 | if (file_exists(PHPWG_ROOT_PATH.$f)) |
---|
1092 | { |
---|
1093 | $css[] = "{combine_css path='$f' order=10}"; |
---|
1094 | } |
---|
1095 | } |
---|
1096 | $f = PWG_LOCAL_DIR.'css/rules.css'; |
---|
1097 | if (file_exists(PHPWG_ROOT_PATH.$f)) |
---|
1098 | { |
---|
1099 | $css[] = "{combine_css path='$f' order=10}"; |
---|
1100 | } |
---|
1101 | |
---|
1102 | if (!empty($css)) |
---|
1103 | { |
---|
1104 | $source = str_replace("\n{get_combined_css}", "\n".implode( "\n", $css )."\n{get_combined_css}", $source); |
---|
1105 | } |
---|
1106 | |
---|
1107 | return $source; |
---|
1108 | } |
---|
1109 | |
---|
1110 | /** |
---|
1111 | * Loads the configuration file from a theme directory and returns it. |
---|
1112 | * |
---|
1113 | * @param string $dir |
---|
1114 | * @return array |
---|
1115 | */ |
---|
1116 | function load_themeconf($dir) |
---|
1117 | { |
---|
1118 | global $themeconfs, $conf; |
---|
1119 | |
---|
1120 | $dir = realpath($dir); |
---|
1121 | if (!isset($themeconfs[$dir])) |
---|
1122 | { |
---|
1123 | $themeconf = array(); |
---|
1124 | include($dir.'/themeconf.inc.php'); |
---|
1125 | // Put themeconf in cache |
---|
1126 | $themeconfs[$dir] = $themeconf; |
---|
1127 | } |
---|
1128 | return $themeconfs[$dir]; |
---|
1129 | } |
---|
1130 | |
---|
1131 | /** |
---|
1132 | * Registers a button to be displayed on picture page. |
---|
1133 | * |
---|
1134 | * @param string $content |
---|
1135 | * @param int $rank |
---|
1136 | */ |
---|
1137 | function add_picture_button($content, $rank=BUTTONS_RANK_NEUTRAL) |
---|
1138 | { |
---|
1139 | $this->picture_buttons[$rank][] = $content; |
---|
1140 | } |
---|
1141 | |
---|
1142 | /** |
---|
1143 | * Registers a button to be displayed on index pages. |
---|
1144 | * |
---|
1145 | * @param string $content |
---|
1146 | * @param int $rank |
---|
1147 | */ |
---|
1148 | function add_index_button($content, $rank=BUTTONS_RANK_NEUTRAL) |
---|
1149 | { |
---|
1150 | $this->index_buttons[$rank][] = $content; |
---|
1151 | } |
---|
1152 | |
---|
1153 | /** |
---|
1154 | * Assigns PLUGIN_PICTURE_BUTTONS template variable with registered picture buttons. |
---|
1155 | */ |
---|
1156 | function parse_picture_buttons() |
---|
1157 | { |
---|
1158 | if (!empty($this->picture_buttons)) |
---|
1159 | { |
---|
1160 | ksort($this->picture_buttons); |
---|
1161 | $this->assign('PLUGIN_PICTURE_BUTTONS', |
---|
1162 | array_reduce( |
---|
1163 | $this->picture_buttons, |
---|
1164 | create_function('$v,$w', 'return array_merge($v, $w);'), |
---|
1165 | array() |
---|
1166 | )); |
---|
1167 | } |
---|
1168 | } |
---|
1169 | |
---|
1170 | /** |
---|
1171 | * Assigns PLUGIN_INDEX_BUTTONS template variable with registered index buttons. |
---|
1172 | */ |
---|
1173 | function parse_index_buttons() |
---|
1174 | { |
---|
1175 | if (!empty($this->index_buttons)) |
---|
1176 | { |
---|
1177 | ksort($this->index_buttons); |
---|
1178 | $this->assign('PLUGIN_INDEX_BUTTONS', |
---|
1179 | array_reduce( |
---|
1180 | $this->index_buttons, |
---|
1181 | create_function('$v,$w', 'return array_merge($v, $w);'), |
---|
1182 | array() |
---|
1183 | )); |
---|
1184 | } |
---|
1185 | } |
---|
1186 | } |
---|
1187 | |
---|
1188 | |
---|
1189 | /** |
---|
1190 | * This class contains basic functions that can be called directly from the |
---|
1191 | * templates in the form $pwg->l10n('edit') |
---|
1192 | */ |
---|
1193 | class PwgTemplateAdapter |
---|
1194 | { |
---|
1195 | /** |
---|
1196 | * @deprecated use "translate" modifier |
---|
1197 | */ |
---|
1198 | function l10n($text) |
---|
1199 | { |
---|
1200 | return l10n($text); |
---|
1201 | } |
---|
1202 | |
---|
1203 | /** |
---|
1204 | * @deprecated use "translate_dec" modifier |
---|
1205 | */ |
---|
1206 | function l10n_dec($s, $p, $v) |
---|
1207 | { |
---|
1208 | return l10n_dec($s, $p, $v); |
---|
1209 | } |
---|
1210 | |
---|
1211 | /** |
---|
1212 | * @deprecated use "translate" or "sprintf" modifier |
---|
1213 | */ |
---|
1214 | function sprintf() |
---|
1215 | { |
---|
1216 | $args = func_get_args(); |
---|
1217 | return call_user_func_array('sprintf', $args ); |
---|
1218 | } |
---|
1219 | |
---|
1220 | /** |
---|
1221 | * @param string $type |
---|
1222 | * @param array $img |
---|
1223 | * @return DerivativeImage |
---|
1224 | */ |
---|
1225 | function derivative($type, $img) |
---|
1226 | { |
---|
1227 | return new DerivativeImage($type, $img); |
---|
1228 | } |
---|
1229 | |
---|
1230 | /** |
---|
1231 | * @param string $type |
---|
1232 | * @param array $img |
---|
1233 | * @return string |
---|
1234 | */ |
---|
1235 | function derivative_url($type, $img) |
---|
1236 | { |
---|
1237 | return DerivativeImage::url($type, $img); |
---|
1238 | } |
---|
1239 | } |
---|
1240 | |
---|
1241 | |
---|
1242 | /** |
---|
1243 | * A Combinable represents a JS or CSS file ready for cobination and minification. |
---|
1244 | */ |
---|
1245 | class Combinable |
---|
1246 | { |
---|
1247 | /** @var string */ |
---|
1248 | public $id; |
---|
1249 | /** @var string */ |
---|
1250 | public $path; |
---|
1251 | /** @var string */ |
---|
1252 | public $version; |
---|
1253 | /** @var bool */ |
---|
1254 | public $is_template; |
---|
1255 | |
---|
1256 | /** |
---|
1257 | * @param string $id |
---|
1258 | * @param string $path |
---|
1259 | * @param string $version |
---|
1260 | */ |
---|
1261 | function __construct($id, $path, $version=0) |
---|
1262 | { |
---|
1263 | $this->id = $id; |
---|
1264 | $this->set_path($path); |
---|
1265 | $this->version = $version; |
---|
1266 | $this->is_template = false; |
---|
1267 | } |
---|
1268 | |
---|
1269 | /** |
---|
1270 | * @param string $path |
---|
1271 | */ |
---|
1272 | function set_path($path) |
---|
1273 | { |
---|
1274 | if (!empty($path)) |
---|
1275 | $this->path = $path; |
---|
1276 | } |
---|
1277 | |
---|
1278 | /** |
---|
1279 | * @return bool |
---|
1280 | */ |
---|
1281 | function is_remote() |
---|
1282 | { |
---|
1283 | return url_is_remote($this->path) || strncmp($this->path, '//', 2)==0; |
---|
1284 | } |
---|
1285 | } |
---|
1286 | |
---|
1287 | /** |
---|
1288 | * Implementation of Combinable for JS files. |
---|
1289 | */ |
---|
1290 | final class Script extends Combinable |
---|
1291 | { |
---|
1292 | /** @var int 0,1,2 */ |
---|
1293 | public $load_mode; |
---|
1294 | /** @var array */ |
---|
1295 | public $precedents; |
---|
1296 | /** @var array */ |
---|
1297 | public $extra; |
---|
1298 | |
---|
1299 | /** |
---|
1300 | * @param int 0,1,2 |
---|
1301 | * @param string $id |
---|
1302 | * @param string $path |
---|
1303 | * @param string $version |
---|
1304 | * @param array $precedents |
---|
1305 | */ |
---|
1306 | function __construct($load_mode, $id, $path, $version=0, $precedents=array()) |
---|
1307 | { |
---|
1308 | parent::__construct($id, $path, $version); |
---|
1309 | $this->load_mode = $load_mode; |
---|
1310 | $this->precedents = $precedents; |
---|
1311 | $this->extra = array(); |
---|
1312 | } |
---|
1313 | } |
---|
1314 | |
---|
1315 | /** |
---|
1316 | * Implementation of Combinable for CSS files. |
---|
1317 | */ |
---|
1318 | final class Css extends Combinable |
---|
1319 | { |
---|
1320 | /** @var int */ |
---|
1321 | public $order; |
---|
1322 | |
---|
1323 | /** |
---|
1324 | * @param string $id |
---|
1325 | * @param string $path |
---|
1326 | * @param string $version |
---|
1327 | * @param int $order |
---|
1328 | */ |
---|
1329 | function __construct($id, $path, $version=0, $order=0) |
---|
1330 | { |
---|
1331 | parent::__construct($id, $path, $version); |
---|
1332 | $this->order = $order; |
---|
1333 | } |
---|
1334 | } |
---|
1335 | |
---|
1336 | |
---|
1337 | /** |
---|
1338 | * Manages a list of CSS files and combining them in a unique file. |
---|
1339 | */ |
---|
1340 | class CssLoader |
---|
1341 | { |
---|
1342 | /** @param Css[] */ |
---|
1343 | private $registered_css; |
---|
1344 | /** @param int used to keep declaration order */ |
---|
1345 | private $counter; |
---|
1346 | |
---|
1347 | function __construct() |
---|
1348 | { |
---|
1349 | $this->clear(); |
---|
1350 | } |
---|
1351 | |
---|
1352 | function clear() |
---|
1353 | { |
---|
1354 | $this->registered_css = array(); |
---|
1355 | $this->counter = 0; |
---|
1356 | } |
---|
1357 | |
---|
1358 | /** |
---|
1359 | * @return Combinable[] array of combined CSS. |
---|
1360 | */ |
---|
1361 | function get_css() |
---|
1362 | { |
---|
1363 | uasort($this->registered_css, array('CssLoader', 'cmp_by_order')); |
---|
1364 | $combiner = new FileCombiner('css', $this->registered_css); |
---|
1365 | return $combiner->combine(); |
---|
1366 | } |
---|
1367 | |
---|
1368 | /** |
---|
1369 | * Callback for CSS files sorting. |
---|
1370 | */ |
---|
1371 | private static function cmp_by_order($a, $b) |
---|
1372 | { |
---|
1373 | return $a->order - $b->order; |
---|
1374 | } |
---|
1375 | |
---|
1376 | /** |
---|
1377 | * Adds a new file, if a file with the same $id already exsists, the one with |
---|
1378 | * the higher $order or higher $version is kept. |
---|
1379 | * |
---|
1380 | * @param string $id |
---|
1381 | * @param string $path |
---|
1382 | * @param string $version |
---|
1383 | * @param int $order |
---|
1384 | * @param bool $is_template |
---|
1385 | */ |
---|
1386 | function add($id, $path, $version=0, $order=0, $is_template=false) |
---|
1387 | { |
---|
1388 | if (!isset($this->registered_css[$id])) |
---|
1389 | { |
---|
1390 | // costum order as an higher impact than declaration order |
---|
1391 | $css = new Css($id, $path, $version, $order*1000+$this->counter); |
---|
1392 | $css->is_template = $is_template; |
---|
1393 | $this->registered_css[$id] = $css; |
---|
1394 | $this->counter++; |
---|
1395 | } |
---|
1396 | else |
---|
1397 | { |
---|
1398 | $css = $this->registered_css[$id]; |
---|
1399 | if ($css->order<$order || version_compare($css->version, $version)<0) |
---|
1400 | { |
---|
1401 | unset($this->registered_css[$id]); |
---|
1402 | $this->add($id, $path, $version, $order, $is_template); |
---|
1403 | } |
---|
1404 | } |
---|
1405 | } |
---|
1406 | } |
---|
1407 | |
---|
1408 | |
---|
1409 | /** |
---|
1410 | * Manage a list of required scripts for a page, by optimizing their loading location (head, footer, async) |
---|
1411 | * and later on by combining them in a unique file respecting at the same time dependencies. |
---|
1412 | */ |
---|
1413 | class ScriptLoader |
---|
1414 | { |
---|
1415 | /** @var Script[] */ |
---|
1416 | private $registered_scripts; |
---|
1417 | /** @var string[] */ |
---|
1418 | public $inline_scripts; |
---|
1419 | |
---|
1420 | /** @var bool */ |
---|
1421 | private $did_head; |
---|
1422 | /** @var bool */ |
---|
1423 | private $head_done_scripts; |
---|
1424 | /** @var bool */ |
---|
1425 | private $did_footer; |
---|
1426 | |
---|
1427 | private static $known_paths = array( |
---|
1428 | 'core.scripts' => 'themes/default/js/scripts.js', |
---|
1429 | 'jquery' => 'themes/default/js/jquery.min.js', |
---|
1430 | 'jquery.ui' => 'themes/default/js/ui/minified/jquery.ui.core.min.js', |
---|
1431 | 'jquery.ui.effect' => 'themes/default/js/ui/minified/jquery.ui.effect.min.js', |
---|
1432 | ); |
---|
1433 | |
---|
1434 | private static $ui_core_dependencies = array( |
---|
1435 | 'jquery.ui.widget' => array('jquery'), |
---|
1436 | 'jquery.ui.position' => array('jquery'), |
---|
1437 | 'jquery.ui.mouse' => array('jquery', 'jquery.ui', 'jquery.ui.widget'), |
---|
1438 | ); |
---|
1439 | |
---|
1440 | function __construct() |
---|
1441 | { |
---|
1442 | $this->clear(); |
---|
1443 | } |
---|
1444 | |
---|
1445 | function clear() |
---|
1446 | { |
---|
1447 | $this->registered_scripts = array(); |
---|
1448 | $this->inline_scripts = array(); |
---|
1449 | $this->head_done_scripts = array(); |
---|
1450 | $this->did_head = $this->did_footer = false; |
---|
1451 | } |
---|
1452 | |
---|
1453 | /** |
---|
1454 | * @return bool |
---|
1455 | */ |
---|
1456 | function did_head() |
---|
1457 | { |
---|
1458 | return $this->did_head; |
---|
1459 | } |
---|
1460 | |
---|
1461 | /** |
---|
1462 | * @return Script[] |
---|
1463 | */ |
---|
1464 | function get_all() |
---|
1465 | { |
---|
1466 | return $this->registered_scripts; |
---|
1467 | } |
---|
1468 | |
---|
1469 | /** |
---|
1470 | * @param string $code |
---|
1471 | * @param string[] $require |
---|
1472 | */ |
---|
1473 | function add_inline($code, $require) |
---|
1474 | { |
---|
1475 | !$this->did_footer || trigger_error("Attempt to add inline script but the footer has been written", E_USER_WARNING); |
---|
1476 | if(!empty($require)) |
---|
1477 | { |
---|
1478 | foreach ($require as $id) |
---|
1479 | { |
---|
1480 | if(!isset($this->registered_scripts[$id])) |
---|
1481 | $this->load_known_required_script($id, 1) or fatal_error("inline script not found require $id"); |
---|
1482 | $s = $this->registered_scripts[$id]; |
---|
1483 | if($s->load_mode==2) |
---|
1484 | $s->load_mode=1; // until now the implementation does not allow executing inline script depending on another async script |
---|
1485 | } |
---|
1486 | } |
---|
1487 | $this->inline_scripts[] = $code; |
---|
1488 | } |
---|
1489 | |
---|
1490 | /** |
---|
1491 | * @param string $id |
---|
1492 | * @param int $load_mode |
---|
1493 | * @param string[] $require |
---|
1494 | * @param string $path |
---|
1495 | * @param string $version |
---|
1496 | */ |
---|
1497 | function add($id, $load_mode, $require, $path, $version=0) |
---|
1498 | { |
---|
1499 | if ($this->did_head && $load_mode==0) |
---|
1500 | { |
---|
1501 | trigger_error("Attempt to add script $id but the head has been written", E_USER_WARNING); |
---|
1502 | } |
---|
1503 | elseif ($this->did_footer) |
---|
1504 | { |
---|
1505 | trigger_error("Attempt to add script $id but the footer has been written", E_USER_WARNING); |
---|
1506 | } |
---|
1507 | if (! isset( $this->registered_scripts[$id] ) ) |
---|
1508 | { |
---|
1509 | $script = new Script($load_mode, $id, $path, $version, $require); |
---|
1510 | self::fill_well_known($id, $script); |
---|
1511 | $this->registered_scripts[$id] = $script; |
---|
1512 | |
---|
1513 | // Load or modify all UI core files |
---|
1514 | if ($id == 'jquery.ui' and $script->path == self::$known_paths['jquery.ui']) |
---|
1515 | { |
---|
1516 | foreach (self::$ui_core_dependencies as $script_id => $required_ids) |
---|
1517 | $this->add($script_id, $load_mode, $required_ids, null, $version); |
---|
1518 | } |
---|
1519 | |
---|
1520 | // Try to load undefined required script |
---|
1521 | foreach ($script->precedents as $script_id) |
---|
1522 | { |
---|
1523 | if (! isset( $this->registered_scripts[$script_id] ) ) |
---|
1524 | $this->load_known_required_script($script_id, $load_mode); |
---|
1525 | } |
---|
1526 | } |
---|
1527 | else |
---|
1528 | { |
---|
1529 | $script = $this->registered_scripts[$id]; |
---|
1530 | if (count($require)) |
---|
1531 | { |
---|
1532 | $script->precedents = array_unique( array_merge($script->precedents, $require) ); |
---|
1533 | } |
---|
1534 | $script->set_path($path); |
---|
1535 | if ($version && version_compare($script->version, $version)<0 ) |
---|
1536 | $script->version = $version; |
---|
1537 | if ($load_mode < $script->load_mode) |
---|
1538 | $script->load_mode = $load_mode; |
---|
1539 | } |
---|
1540 | } |
---|
1541 | |
---|
1542 | /** |
---|
1543 | * Returns combined scripts loaded in header. |
---|
1544 | * |
---|
1545 | * @return Combinable[] |
---|
1546 | */ |
---|
1547 | function get_head_scripts() |
---|
1548 | { |
---|
1549 | self::check_load_dep($this->registered_scripts); |
---|
1550 | foreach( array_keys($this->registered_scripts) as $id ) |
---|
1551 | { |
---|
1552 | $this->compute_script_topological_order($id); |
---|
1553 | } |
---|
1554 | |
---|
1555 | uasort($this->registered_scripts, array('ScriptLoader', 'cmp_by_mode_and_order')); |
---|
1556 | |
---|
1557 | foreach( $this->registered_scripts as $id => $script) |
---|
1558 | { |
---|
1559 | if ($script->load_mode > 0) |
---|
1560 | break; |
---|
1561 | if ( !empty($script->path) ) |
---|
1562 | $this->head_done_scripts[$id] = $script; |
---|
1563 | else |
---|
1564 | trigger_error("Script $id has an undefined path", E_USER_WARNING); |
---|
1565 | } |
---|
1566 | $this->did_head = true; |
---|
1567 | return self::do_combine($this->head_done_scripts, 0); |
---|
1568 | } |
---|
1569 | |
---|
1570 | /** |
---|
1571 | * Returns combined scripts loaded in footer. |
---|
1572 | * |
---|
1573 | * @return Combinable[] |
---|
1574 | */ |
---|
1575 | function get_footer_scripts() |
---|
1576 | { |
---|
1577 | if (!$this->did_head) |
---|
1578 | { |
---|
1579 | self::check_load_dep($this->registered_scripts); |
---|
1580 | } |
---|
1581 | $this->did_footer = true; |
---|
1582 | $todo = array(); |
---|
1583 | foreach( $this->registered_scripts as $id => $script) |
---|
1584 | { |
---|
1585 | if (!isset($this->head_done_scripts[$id])) |
---|
1586 | { |
---|
1587 | $todo[$id] = $script; |
---|
1588 | } |
---|
1589 | } |
---|
1590 | |
---|
1591 | foreach( array_keys($todo) as $id ) |
---|
1592 | { |
---|
1593 | $this->compute_script_topological_order($id); |
---|
1594 | } |
---|
1595 | |
---|
1596 | uasort($todo, array('ScriptLoader', 'cmp_by_mode_and_order')); |
---|
1597 | |
---|
1598 | $result = array( array(), array() ); |
---|
1599 | foreach( $todo as $id => $script) |
---|
1600 | { |
---|
1601 | $result[$script->load_mode-1][$id] = $script; |
---|
1602 | } |
---|
1603 | return array( self::do_combine($result[0],1), self::do_combine($result[1],2) ); |
---|
1604 | } |
---|
1605 | |
---|
1606 | /** |
---|
1607 | * @param Script[] $scripts |
---|
1608 | * @param int $load_mode |
---|
1609 | * @return Combinable[] |
---|
1610 | */ |
---|
1611 | private static function do_combine($scripts, $load_mode) |
---|
1612 | { |
---|
1613 | $combiner = new FileCombiner('js', $scripts); |
---|
1614 | return $combiner->combine(); |
---|
1615 | } |
---|
1616 | |
---|
1617 | /** |
---|
1618 | * Checks dependencies among Scripts. |
---|
1619 | * Checks that if B depends on A, then B->load_mode >= A->load_mode in order to respect execution order. |
---|
1620 | * |
---|
1621 | * @param Script[] $scripts |
---|
1622 | */ |
---|
1623 | private static function check_load_dep($scripts) |
---|
1624 | { |
---|
1625 | global $conf; |
---|
1626 | do |
---|
1627 | { |
---|
1628 | $changed = false; |
---|
1629 | foreach( $scripts as $id => $script) |
---|
1630 | { |
---|
1631 | $load = $script->load_mode; |
---|
1632 | foreach( $script->precedents as $precedent) |
---|
1633 | { |
---|
1634 | if ( !isset($scripts[$precedent] ) ) |
---|
1635 | continue; |
---|
1636 | if ( $scripts[$precedent]->load_mode > $load ) |
---|
1637 | { |
---|
1638 | $scripts[$precedent]->load_mode = $load; |
---|
1639 | $changed = true; |
---|
1640 | } |
---|
1641 | if ($load==2 && $scripts[$precedent]->load_mode==2 && ($scripts[$precedent]->is_remote() or !$conf['template_combine_files']) ) |
---|
1642 | {// we are async -> a predecessor cannot be async unlesss it can be merged; otherwise script execution order is not guaranteed |
---|
1643 | $scripts[$precedent]->load_mode = 1; |
---|
1644 | $changed = true; |
---|
1645 | } |
---|
1646 | } |
---|
1647 | } |
---|
1648 | } |
---|
1649 | while ($changed); |
---|
1650 | } |
---|
1651 | |
---|
1652 | /** |
---|
1653 | * Fill a script dependancies with the known jQuery UI scripts. |
---|
1654 | * |
---|
1655 | * @param string $id in FileCombiner::$known_paths |
---|
1656 | * @param Script $script |
---|
1657 | */ |
---|
1658 | private static function fill_well_known($id, $script) |
---|
1659 | { |
---|
1660 | if ( empty($script->path) && isset(self::$known_paths[$id])) |
---|
1661 | { |
---|
1662 | $script->path = self::$known_paths[$id]; |
---|
1663 | } |
---|
1664 | if ( strncmp($id, 'jquery.', 7)==0 ) |
---|
1665 | { |
---|
1666 | $required_ids = array('jquery'); |
---|
1667 | |
---|
1668 | if ( strncmp($id, 'jquery.ui.effect-', 17)==0 ) |
---|
1669 | { |
---|
1670 | $required_ids = array('jquery', 'jquery.ui.effect'); |
---|
1671 | |
---|
1672 | if ( empty($script->path) ) |
---|
1673 | $script->path = dirname(self::$known_paths['jquery.ui.effect'])."/$id.min.js"; |
---|
1674 | } |
---|
1675 | elseif ( strncmp($id, 'jquery.ui.', 10)==0 ) |
---|
1676 | { |
---|
1677 | if ( !isset(self::$ui_core_dependencies[$id]) ) |
---|
1678 | $required_ids = array_merge(array('jquery', 'jquery.ui'), array_keys(self::$ui_core_dependencies)); |
---|
1679 | |
---|
1680 | if ( empty($script->path) ) |
---|
1681 | $script->path = dirname(self::$known_paths['jquery.ui'])."/$id.min.js"; |
---|
1682 | } |
---|
1683 | |
---|
1684 | foreach ($required_ids as $required_id) |
---|
1685 | { |
---|
1686 | if ( !in_array($required_id, $script->precedents ) ) |
---|
1687 | $script->precedents[] = $required_id; |
---|
1688 | } |
---|
1689 | } |
---|
1690 | } |
---|
1691 | |
---|
1692 | /** |
---|
1693 | * Add a known jQuery UI script to loaded scripts. |
---|
1694 | * |
---|
1695 | * @param string $id in FileCombiner::$known_paths |
---|
1696 | * @param int $load_mode |
---|
1697 | * @return bool |
---|
1698 | */ |
---|
1699 | private function load_known_required_script($id, $load_mode) |
---|
1700 | { |
---|
1701 | if ( isset(self::$known_paths[$id]) or strncmp($id, 'jquery.ui.', 10)==0 ) |
---|
1702 | { |
---|
1703 | $this->add($id, $load_mode, array(), null); |
---|
1704 | return true; |
---|
1705 | } |
---|
1706 | return false; |
---|
1707 | } |
---|
1708 | |
---|
1709 | /** |
---|
1710 | * Compute script order depending on dependencies. |
---|
1711 | * Assigned to $script->extra['order']. |
---|
1712 | * |
---|
1713 | * @param string $script_id |
---|
1714 | * @param int $recursion_limiter |
---|
1715 | * @return int |
---|
1716 | */ |
---|
1717 | private function compute_script_topological_order($script_id, $recursion_limiter=0) |
---|
1718 | { |
---|
1719 | if (!isset($this->registered_scripts[$script_id])) |
---|
1720 | { |
---|
1721 | trigger_error("Undefined script $script_id is required by someone", E_USER_WARNING); |
---|
1722 | return 0; |
---|
1723 | } |
---|
1724 | $recursion_limiter<5 or fatal_error("combined script circular dependency"); |
---|
1725 | $script = $this->registered_scripts[$script_id]; |
---|
1726 | if (isset($script->extra['order'])) |
---|
1727 | return $script->extra['order']; |
---|
1728 | if (count($script->precedents) == 0) |
---|
1729 | return ($script->extra['order'] = 0); |
---|
1730 | $max = 0; |
---|
1731 | foreach( $script->precedents as $precedent) |
---|
1732 | $max = max($max, $this->compute_script_topological_order($precedent, $recursion_limiter+1) ); |
---|
1733 | $max++; |
---|
1734 | return ($script->extra['order'] = $max); |
---|
1735 | } |
---|
1736 | |
---|
1737 | /** |
---|
1738 | * Callback for scripts sorter. |
---|
1739 | */ |
---|
1740 | private static function cmp_by_mode_and_order($s1, $s2) |
---|
1741 | { |
---|
1742 | $ret = $s1->load_mode - $s2->load_mode; |
---|
1743 | if ($ret) return $ret; |
---|
1744 | |
---|
1745 | $ret = $s1->extra['order'] - $s2->extra['order']; |
---|
1746 | if ($ret) return $ret; |
---|
1747 | |
---|
1748 | if ($s1->extra['order']==0 and ($s1->is_remote() xor $s2->is_remote()) ) |
---|
1749 | { |
---|
1750 | return $s1->is_remote() ? -1 : 1; |
---|
1751 | } |
---|
1752 | return strcmp($s1->id,$s2->id); |
---|
1753 | } |
---|
1754 | } |
---|
1755 | |
---|
1756 | |
---|
1757 | /** |
---|
1758 | * Allows merging of javascript and css files into a single one. |
---|
1759 | */ |
---|
1760 | final class FileCombiner |
---|
1761 | { |
---|
1762 | /** @var string 'js' or 'css' */ |
---|
1763 | private $type; |
---|
1764 | /** @var bool */ |
---|
1765 | private $is_css; |
---|
1766 | /** @var Combinable[] */ |
---|
1767 | private $combinables; |
---|
1768 | |
---|
1769 | /** |
---|
1770 | * @param string $type 'js' or 'css' |
---|
1771 | * @param Combinable[] $combinables |
---|
1772 | */ |
---|
1773 | function __construct($type, $combinables=array()) |
---|
1774 | { |
---|
1775 | $this->type = $type; |
---|
1776 | $this->is_css = $type=='css'; |
---|
1777 | $this->combinables = $combinables; |
---|
1778 | } |
---|
1779 | |
---|
1780 | /** |
---|
1781 | * Deletes all combined files from cache directory. |
---|
1782 | */ |
---|
1783 | static function clear_combined_files() |
---|
1784 | { |
---|
1785 | $dir = opendir(PHPWG_ROOT_PATH.PWG_COMBINED_DIR); |
---|
1786 | while ($file = readdir($dir)) |
---|
1787 | { |
---|
1788 | if ( get_extension($file)=='js' || get_extension($file)=='css') |
---|
1789 | unlink(PHPWG_ROOT_PATH.PWG_COMBINED_DIR.$file); |
---|
1790 | } |
---|
1791 | closedir($dir); |
---|
1792 | } |
---|
1793 | |
---|
1794 | /** |
---|
1795 | * @param Combinable|Combinable[] $combinable |
---|
1796 | */ |
---|
1797 | function add($combinable) |
---|
1798 | { |
---|
1799 | if (is_array($combinable)) |
---|
1800 | { |
---|
1801 | $this->combinables = array_merge($this->combinables, $combinable); |
---|
1802 | } |
---|
1803 | else |
---|
1804 | { |
---|
1805 | $this->combinables[] = $combinable; |
---|
1806 | } |
---|
1807 | } |
---|
1808 | |
---|
1809 | /** |
---|
1810 | * @return Combinable[] |
---|
1811 | */ |
---|
1812 | function combine() |
---|
1813 | { |
---|
1814 | global $conf; |
---|
1815 | $force = false; |
---|
1816 | if (is_admin() && ($this->is_css || !$conf['template_compile_check']) ) |
---|
1817 | { |
---|
1818 | $force = (isset($_SERVER['HTTP_CACHE_CONTROL']) && strpos($_SERVER['HTTP_CACHE_CONTROL'], 'max-age=0') !== false) |
---|
1819 | || (isset($_SERVER['HTTP_PRAGMA']) && strpos($_SERVER['HTTP_PRAGMA'], 'no-cache')); |
---|
1820 | } |
---|
1821 | |
---|
1822 | $result = array(); |
---|
1823 | $pending = array(); |
---|
1824 | $ini_key = $this->is_css ? array(get_absolute_root_url(false)): array(); //because for css we modify bg url; |
---|
1825 | $key = $ini_key; |
---|
1826 | |
---|
1827 | foreach ($this->combinables as $combinable) |
---|
1828 | { |
---|
1829 | if ($combinable->is_remote()) |
---|
1830 | { |
---|
1831 | $this->flush_pending($result, $pending, $key, $force); |
---|
1832 | $key = $ini_key; |
---|
1833 | $result[] = $combinable; |
---|
1834 | continue; |
---|
1835 | } |
---|
1836 | elseif (!$conf['template_combine_files']) |
---|
1837 | { |
---|
1838 | $this->flush_pending($result, $pending, $key, $force); |
---|
1839 | $key = $ini_key; |
---|
1840 | } |
---|
1841 | |
---|
1842 | $key[] = $combinable->path; |
---|
1843 | $key[] = $combinable->version; |
---|
1844 | if ($conf['template_compile_check']) |
---|
1845 | $key[] = filemtime( PHPWG_ROOT_PATH . $combinable->path ); |
---|
1846 | $pending[] = $combinable; |
---|
1847 | } |
---|
1848 | $this->flush_pending($result, $pending, $key, $force); |
---|
1849 | return $result; |
---|
1850 | } |
---|
1851 | |
---|
1852 | /** |
---|
1853 | * Process a set of pending files. |
---|
1854 | * |
---|
1855 | * @param array &$result |
---|
1856 | * @param array &$pending |
---|
1857 | * @param string[] $key |
---|
1858 | * @param bool $force |
---|
1859 | */ |
---|
1860 | private function flush_pending(&$result, &$pending, $key, $force) |
---|
1861 | { |
---|
1862 | if (count($pending)>1) |
---|
1863 | { |
---|
1864 | $key = join('>', $key); |
---|
1865 | $file = PWG_COMBINED_DIR . base_convert(crc32($key),10,36) . '.' . $this->type; |
---|
1866 | if ($force || !file_exists(PHPWG_ROOT_PATH.$file) ) |
---|
1867 | { |
---|
1868 | $output = ''; |
---|
1869 | foreach ($pending as $combinable) |
---|
1870 | { |
---|
1871 | $output .= "/*BEGIN $combinable->path */\n"; |
---|
1872 | $output .= $this->process_combinable($combinable, true, $force); |
---|
1873 | $output .= "\n"; |
---|
1874 | } |
---|
1875 | mkgetdir( dirname(PHPWG_ROOT_PATH.$file) ); |
---|
1876 | file_put_contents( PHPWG_ROOT_PATH.$file, $output ); |
---|
1877 | @chmod(PHPWG_ROOT_PATH.$file, 0644); |
---|
1878 | } |
---|
1879 | $result[] = new Combinable("combi", $file, false); |
---|
1880 | } |
---|
1881 | elseif ( count($pending)==1) |
---|
1882 | { |
---|
1883 | $this->process_combinable($pending[0], false, $force); |
---|
1884 | $result[] = $pending[0]; |
---|
1885 | } |
---|
1886 | $key = array(); |
---|
1887 | $pending = array(); |
---|
1888 | } |
---|
1889 | |
---|
1890 | /** |
---|
1891 | * Process one combinable file. |
---|
1892 | * |
---|
1893 | * @param Combinable $combinable |
---|
1894 | * @param bool $return_content |
---|
1895 | * @param bool $force |
---|
1896 | * @return null|string |
---|
1897 | */ |
---|
1898 | private function process_combinable($combinable, $return_content, $force) |
---|
1899 | { |
---|
1900 | global $conf; |
---|
1901 | if ($combinable->is_template) |
---|
1902 | { |
---|
1903 | if (!$return_content) |
---|
1904 | { |
---|
1905 | $key = array($combinable->path, $combinable->version); |
---|
1906 | if ($conf['template_compile_check']) |
---|
1907 | $key[] = filemtime( PHPWG_ROOT_PATH . $combinable->path ); |
---|
1908 | $file = PWG_COMBINED_DIR . 't' . base_convert(crc32(implode(',',$key)),10,36) . '.' . $this->type; |
---|
1909 | if (!$force && file_exists(PHPWG_ROOT_PATH.$file) ) |
---|
1910 | { |
---|
1911 | $combinable->path = $file; |
---|
1912 | $combinable->version = false; |
---|
1913 | return; |
---|
1914 | } |
---|
1915 | } |
---|
1916 | |
---|
1917 | global $template; |
---|
1918 | $handle = $this->type. '.' .$combinable->id; |
---|
1919 | $template->set_filename($handle, realpath(PHPWG_ROOT_PATH.$combinable->path)); |
---|
1920 | trigger_action( 'combinable_preparse', $template, $combinable, $this); //allow themes and plugins to set their own vars to template ... |
---|
1921 | $content = $template->parse($handle, true); |
---|
1922 | |
---|
1923 | if ($this->is_css) |
---|
1924 | $content = self::process_css($content, $combinable->path ); |
---|
1925 | else |
---|
1926 | $content = self::process_js($content, $combinable->path ); |
---|
1927 | |
---|
1928 | if ($return_content) |
---|
1929 | return $content; |
---|
1930 | file_put_contents( PHPWG_ROOT_PATH.$file, $content ); |
---|
1931 | $combinable->path = $file; |
---|
1932 | } |
---|
1933 | elseif ($return_content) |
---|
1934 | { |
---|
1935 | $content = file_get_contents(PHPWG_ROOT_PATH . $combinable->path); |
---|
1936 | if ($this->is_css) |
---|
1937 | $content = self::process_css($content, $combinable->path ); |
---|
1938 | else |
---|
1939 | $content = self::process_js($content, $combinable->path ); |
---|
1940 | return $content; |
---|
1941 | } |
---|
1942 | } |
---|
1943 | |
---|
1944 | /** |
---|
1945 | * Process a JS file. |
---|
1946 | * |
---|
1947 | * @param string $js file content |
---|
1948 | * @param string $file |
---|
1949 | * @return string |
---|
1950 | */ |
---|
1951 | private static function process_js($js, $file) |
---|
1952 | { |
---|
1953 | if (strpos($file, '.min')===false and strpos($file, '.packed')===false ) |
---|
1954 | { |
---|
1955 | require_once(PHPWG_ROOT_PATH.'include/jshrink.class.php'); |
---|
1956 | try { $js = JShrink_Minifier::minify($js); } catch(Exception $e) {} |
---|
1957 | } |
---|
1958 | return trim($js, " \t\r\n;").";\n"; |
---|
1959 | } |
---|
1960 | |
---|
1961 | /** |
---|
1962 | * Process a CSS file. |
---|
1963 | * |
---|
1964 | * @param string $css file content |
---|
1965 | * @param string $file |
---|
1966 | * @return string |
---|
1967 | */ |
---|
1968 | private static function process_css($css, $file) |
---|
1969 | { |
---|
1970 | $css = self::process_css_rec($css, dirname($file)); |
---|
1971 | if (strpos($file, '.min')===false and version_compare(PHP_VERSION, '5.2.4', '>=')) |
---|
1972 | { |
---|
1973 | require_once(PHPWG_ROOT_PATH.'include/cssmin.class.php'); |
---|
1974 | $css = CssMin::minify($css, array('Variables'=>false)); |
---|
1975 | } |
---|
1976 | $css = trigger_event('combined_css_postfilter', $css); |
---|
1977 | return $css; |
---|
1978 | } |
---|
1979 | |
---|
1980 | /** |
---|
1981 | * Resolves relative links in CSS file. |
---|
1982 | * |
---|
1983 | * @param string $css file content |
---|
1984 | * @param string $dir |
---|
1985 | * @return string |
---|
1986 | */ |
---|
1987 | private static function process_css_rec($css, $dir) |
---|
1988 | { |
---|
1989 | static $PATTERN_URL = "#url\(\s*['|\"]{0,1}(.*?)['|\"]{0,1}\s*\)#"; |
---|
1990 | static $PATTERN_IMPORT = "#@import\s*['|\"]{0,1}(.*?)['|\"]{0,1};#"; |
---|
1991 | |
---|
1992 | if (preg_match_all($PATTERN_URL, $css, $matches, PREG_SET_ORDER)) |
---|
1993 | { |
---|
1994 | $search = $replace = array(); |
---|
1995 | foreach ($matches as $match) |
---|
1996 | { |
---|
1997 | if ( !url_is_remote($match[1]) && $match[1][0] != '/' && strpos($match[1], 'data:image/')===false) |
---|
1998 | { |
---|
1999 | $relative = $dir . "/$match[1]"; |
---|
2000 | $search[] = $match[0]; |
---|
2001 | $replace[] = 'url('.embellish_url(get_absolute_root_url(false).$relative).')'; |
---|
2002 | } |
---|
2003 | } |
---|
2004 | $css = str_replace($search, $replace, $css); |
---|
2005 | } |
---|
2006 | |
---|
2007 | if (preg_match_all($PATTERN_IMPORT, $css, $matches, PREG_SET_ORDER)) |
---|
2008 | { |
---|
2009 | $search = $replace = array(); |
---|
2010 | foreach ($matches as $match) |
---|
2011 | { |
---|
2012 | $search[] = $match[0]; |
---|
2013 | $sub_css = file_get_contents(PHPWG_ROOT_PATH . $dir . "/$match[1]"); |
---|
2014 | $replace[] = self::process_css_rec($sub_css, dirname($dir . "/$match[1]") ); |
---|
2015 | } |
---|
2016 | $css = str_replace($search, $replace, $css); |
---|
2017 | } |
---|
2018 | return $css; |
---|
2019 | } |
---|
2020 | } |
---|
2021 | |
---|
2022 | ?> |
---|