source: trunk/include/template.class.php @ 13170

Last change on this file since 13170 was 13170, checked in by patdenice, 12 years ago

multisize

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