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