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