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