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