xref: /dokuwiki/lib/exe/css.php (revision 5161ec70c42b1ec1a351e3c3836290bf29059422)
178a6aeb1SAndreas Gohr<?php
278a6aeb1SAndreas Gohr/**
378a6aeb1SAndreas Gohr * DokuWiki StyleSheet creator
478a6aeb1SAndreas Gohr *
578a6aeb1SAndreas Gohr * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
678a6aeb1SAndreas Gohr * @author     Andreas Gohr <andi@splitbrain.org>
778a6aeb1SAndreas Gohr */
878a6aeb1SAndreas Gohr
9d0a27cb0SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/../../');
101c2d1019SAndreas Gohrif(!defined('NOSESSION')) define('NOSESSION',true); // we do not use a session or authentication here (better caching)
1198bda4fdSAndreas Gohrif(!defined('DOKU_DISABLE_GZIP_OUTPUT')) define('DOKU_DISABLE_GZIP_OUTPUT',1); // we gzip ourself here
126c47a78cSAnika Henkeif(!defined('NL')) define('NL',"\n");
1382edff4dSGerry Weißbachif(!defined('DEFAULT_FLAVOUR')) define('DEFAULT_FLAVOUR',"style");
1478a6aeb1SAndreas Gohrrequire_once(DOKU_INC.'inc/init.php');
1578a6aeb1SAndreas Gohr
1678a6aeb1SAndreas Gohr// Main (don't run when UNIT test)
1778a6aeb1SAndreas Gohrif(!defined('SIMPLE_TEST')){
1878a6aeb1SAndreas Gohr    header('Content-Type: text/css; charset=utf-8');
1978a6aeb1SAndreas Gohr    css_out();
2078a6aeb1SAndreas Gohr}
2178a6aeb1SAndreas Gohr
2278a6aeb1SAndreas Gohr
2378a6aeb1SAndreas Gohr// ---------------------- functions ------------------------------
2478a6aeb1SAndreas Gohr
2578a6aeb1SAndreas Gohr/**
2678a6aeb1SAndreas Gohr * Output all needed Styles
2778a6aeb1SAndreas Gohr *
2878a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
2978a6aeb1SAndreas Gohr */
3078a6aeb1SAndreas Gohrfunction css_out(){
3178a6aeb1SAndreas Gohr    global $conf;
3278a6aeb1SAndreas Gohr    global $lang;
3309edb711SAndreas Gohr    global $config_cascade;
34bfd0f597STom N Harris    global $INPUT;
3509edb711SAndreas Gohr
36de4634ecSGerry Weißbach    if ($INPUT->str('s') == 'feed') {
37de4634ecSGerry Weißbach        $mediatypes = array('feed');
388930b69dSGerry Weißbach        $type = 'feed';
39de4634ecSGerry Weißbach    } else {
408b3ff808SGerry Weißbach        $mediatypes = array('screen', 'all', 'print', 'handheld');
418930b69dSGerry Weißbach        $type = '';
42de4634ecSGerry Weißbach    }
43de4634ecSGerry Weißbach
4420a2375aSAndreas Gohr    // decide from where to get the template
4520a2375aSAndreas Gohr    $tpl = trim(preg_replace('/[^\w-]+/','',$INPUT->str('t')));
4620a2375aSAndreas Gohr    if(!$tpl) $tpl = $conf['template'];
4720a2375aSAndreas Gohr
48*5161ec70SGerry Weißbach    // decide from where to get the flavour
4982edff4dSGerry Weißbach    $flavour = trim(preg_replace('/[^\w-]+/','',$INPUT->str('f')));
5082edff4dSGerry Weißbach    if(!$flavour) $flavour = DEFAULT_FLAVOUR;
51de4634ecSGerry Weißbach
5220a2375aSAndreas Gohr    // load styl.ini
5382edff4dSGerry Weißbach    $styleini = css_styleini($tpl, $flavour, $INPUT->bool('preview'));
5420a2375aSAndreas Gohr
55afb2c082SAndreas Gohr    // cache influencers
56259571aaSMichal Koutný    $tplinc = tpl_incdir($tpl);
57afb2c082SAndreas Gohr    $cache_files = getConfigFiles('main');
58afb2c082SAndreas Gohr    $cache_files[] = $tplinc.'style.ini';
59afb2c082SAndreas Gohr    $cache_files[] = DOKU_CONF."tpl/$tpl/style.ini";
6082edff4dSGerry Weißbach    $cache_files[] = DOKU_CONF."tpl/$tpl/ini/".$flavour.".ini";
61afb2c082SAndreas Gohr    $cache_files[] = __FILE__;
624d6524b8SAndreas Gohr    if($INPUT->bool('preview')) $cache_files[] = $conf['cachedir'].'/preview.ini';
63afb2c082SAndreas Gohr
6478a6aeb1SAndreas Gohr    // Array of needed files and their web locations, the latter ones
6578a6aeb1SAndreas Gohr    // are needed to fix relative paths in the stylesheets
66ef36714bSGerry Weißbach    // We only need to build the list for the requested flavour.
67ef36714bSGerry Weißbach    $media_files = array();
6814977bd2SMichael Hamann    foreach($mediatypes as $mediatype) {
69ef36714bSGerry Weißbach        $files = array();
70ef36714bSGerry Weißbach        if ( $flavour === DEFAULT_FLAVOUR ) {
71318cd03eSAnika Henke            // load core styles
72ef36714bSGerry Weißbach            $files[DOKU_INC.'lib/styles/'.$mediatype.'.css'] = DOKU_BASE.'lib/styles/';
73af4684acSAndreas Gohr
7443576758SAndreas Gohr            // load jQuery-UI theme
756c47a78cSAnika Henke            if ($mediatype == 'screen') {
76ef36714bSGerry Weißbach                $files[DOKU_INC.'lib/scripts/jquery/jquery-ui-theme/smoothness.css'] = DOKU_BASE.'lib/scripts/jquery/jquery-ui-theme/';
776c47a78cSAnika Henke            }
78318cd03eSAnika Henke            // load plugin styles
79ef36714bSGerry Weißbach            $files = array_merge($files, css_pluginstyles($mediatype));
80318cd03eSAnika Henke            // load template styles
81afb2c082SAndreas Gohr            if (isset($styleini['stylesheets'][$mediatype])) {
82ef36714bSGerry Weißbach                $files = array_merge($files, $styleini['stylesheets'][$mediatype]);
8309edb711SAndreas Gohr            }
84318cd03eSAnika Henke            // load user styles
857b909d5eSGerrit Uitslag            if(!empty($config_cascade['userstyle'][$mediatype])) {
867b909d5eSGerrit Uitslag                foreach($config_cascade['userstyle'][$mediatype] as $userstyle) {
87ef36714bSGerry Weißbach                    $files[$userstyle] = DOKU_BASE;
887b909d5eSGerrit Uitslag                }
89318cd03eSAnika Henke            }
90ef36714bSGerry Weißbach        } else if (isset($styleini['stylesheets'][$mediatype])) {
91ef36714bSGerry Weißbach            // only allow the predefined media types
92ef36714bSGerry Weißbach            $files = $styleini['stylesheets'][$mediatype];
93ef36714bSGerry Weißbach        }
9478a6aeb1SAndreas Gohr
95ef36714bSGerry Weißbach        // Let plugins decide to either put more styles here or to remove some
96ef36714bSGerry Weißbach        $media_files[$mediatype] = css_filewrapper($mediatype, $flavour, $files);
97ef36714bSGerry Weißbach        $CSSEvt = new Doku_Event('CSS_STYLES_INCLUDED', $media_files[$mediatype]);
98ef36714bSGerry Weißbach
99ef36714bSGerry Weißbach        // Make it preventable.
100ef36714bSGerry Weißbach        if ( $CSSEvt->advise_before() ) {
101ef36714bSGerry Weißbach            $cache_files = array_merge($cache_files, array_keys($media_files[$mediatype]['files']));
102ef36714bSGerry Weißbach        } else {
103ef36714bSGerry Weißbach            // unset if prevented. Nothing will be printed for this mediatype.
104ef36714bSGerry Weißbach            unset($media_files[$mediatype]);
10514977bd2SMichael Hamann        }
1066619f42eSAdrian Lang
107ef36714bSGerry Weißbach        // finish event.
108ef36714bSGerry Weißbach        $CSSEvt->advise_after();
109ef36714bSGerry Weißbach    }
110ef36714bSGerry Weißbach
111ef36714bSGerry Weißbach    // The generated script depends on some dynamic options
1128930b69dSGerry Weißbach    $cache = new cache('styles'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'].DOKU_BASE.$tpl.$flavour.$type,'.css');
113ef36714bSGerry Weißbach    $cache->_event = 'CSS_CACHE_USE';
114ef36714bSGerry Weißbach
11538f56bffSBen Coburn    // check cache age & handle conditional request
1166619f42eSAdrian Lang    // This may exit if a cache can be used
1178d721324SGerry Weißbach    http_cached($cache->cache,
1188d721324SGerry Weißbach                $cache->useCache(array('files' => $cache_files)));
11978a6aeb1SAndreas Gohr
1203899c2ecSMichael Hamann    // start output buffering
1213899c2ecSMichael Hamann    ob_start();
1223899c2ecSMichael Hamann
123ef36714bSGerry Weißbach    if ( $flavour === DEFAULT_FLAVOUR ) {
124ef36714bSGerry Weißbach        // Fire CSS_STYLES_INCLUDED for one last time to let the
125ef36714bSGerry Weißbach        // plugins decide whether to include the DW default styles.
126ef36714bSGerry Weißbach        // This can be done by preventing the Default.
127ef36714bSGerry Weißbach        $media_files['DW_DEFAULT'] = css_filewrapper('DW_DEFAULT');
128ef36714bSGerry Weißbach        trigger_event('CSS_STYLES_INCLUDED', $media_files['DW_DEFAULT'], 'css_defaultstyles');
129ef36714bSGerry Weißbach    }
130ef36714bSGerry Weißbach
1316c47a78cSAnika Henke    // build the stylesheet
13214977bd2SMichael Hamann    foreach ($mediatypes as $mediatype) {
13378a6aeb1SAndreas Gohr
134ef36714bSGerry Weißbach        // Check if there is a wrapper set for this type.
135ef36714bSGerry Weißbach        if ( !isset($media_files[$mediatype]) ) {
136ef36714bSGerry Weißbach            continue;
13778a6aeb1SAndreas Gohr        }
13878a6aeb1SAndreas Gohr
139ef36714bSGerry Weißbach        $cssData = $media_files[$mediatype];
140ef36714bSGerry Weißbach
141ef36714bSGerry Weißbach        // Print the styles.
142ef36714bSGerry Weißbach        print NL;
143ef36714bSGerry Weißbach        if ( $cssData['encapsulate'] === true ) print $cssData['encapsulationPrefix'] . ' {';
144ef36714bSGerry Weißbach        print '/* START '.$cssData['mediatype'].' styles */'.NL;
145ef36714bSGerry Weißbach
1466c47a78cSAnika Henke        // load files
147ef36714bSGerry Weißbach        foreach($cssData['files'] as $file => $location){
14872a66eb7SAndreas Gohr            $display = str_replace(fullpath(DOKU_INC), '', fullpath($file));
149ef36714bSGerry Weißbach            print "\n/* XXXXXXXXX $display XXXXXXXXX */\n";
150ef36714bSGerry Weißbach            print css_loadfile($file, $location);
1516c47a78cSAnika Henke        }
152ef36714bSGerry Weißbach
153ef36714bSGerry Weißbach        print NL;
154ef36714bSGerry Weißbach        if ( $cssData['encapsulate'] === true ) print '} /* /@media ';
155ef36714bSGerry Weißbach        else print '/*';
156ef36714bSGerry Weißbach        print ' END '.$cssData['mediatype'].' styles */'.NL;
1576c47a78cSAnika Henke    }
158ef36714bSGerry Weißbach
15978a6aeb1SAndreas Gohr    // end output buffering and get contents
16078a6aeb1SAndreas Gohr    $css = ob_get_contents();
16178a6aeb1SAndreas Gohr    ob_end_clean();
16278a6aeb1SAndreas Gohr
163f8fb2d18SAndreas Gohr    // strip any source maps
164f8fb2d18SAndreas Gohr    stripsourcemaps($css);
165f8fb2d18SAndreas Gohr
1666e69c1baSAndreas Gohr    // apply style replacements
167afb2c082SAndreas Gohr    $css = css_applystyle($css, $styleini['replacements']);
1686e69c1baSAndreas Gohr
16972a66eb7SAndreas Gohr    // parse less
17072a66eb7SAndreas Gohr    $css = css_parseless($css);
171d4a1ece8SAndreas Gohr
17278a6aeb1SAndreas Gohr    // compress whitespace and comments
17378a6aeb1SAndreas Gohr    if($conf['compress']){
17478a6aeb1SAndreas Gohr        $css = css_compress($css);
17578a6aeb1SAndreas Gohr    }
17678a6aeb1SAndreas Gohr
177809d3ba5SAndreas Gohr    // embed small images right into the stylesheet
178809d3ba5SAndreas Gohr    if($conf['cssdatauri']){
179809d3ba5SAndreas Gohr        $base = preg_quote(DOKU_BASE,'#');
180809d3ba5SAndreas Gohr        $css = preg_replace_callback('#(url\([ \'"]*)('.$base.')(.*?(?:\.(png|gif)))#i','css_datauri',$css);
181809d3ba5SAndreas Gohr    }
182809d3ba5SAndreas Gohr
1836619f42eSAdrian Lang    http_cached_finish($cache->cache, $css);
18478a6aeb1SAndreas Gohr}
18578a6aeb1SAndreas Gohr
18678a6aeb1SAndreas Gohr/**
18772a66eb7SAndreas Gohr * Uses phpless to parse LESS in our CSS
18872a66eb7SAndreas Gohr *
18972a66eb7SAndreas Gohr * most of this function is error handling to show a nice useful error when
19072a66eb7SAndreas Gohr * LESS compilation fails
19172a66eb7SAndreas Gohr *
192253d4b48SGerrit Uitslag * @param string $css
19372a66eb7SAndreas Gohr * @return string
19472a66eb7SAndreas Gohr */
19572a66eb7SAndreas Gohrfunction css_parseless($css) {
1967d247a3cSGerrit Uitslag    global $conf;
1977d247a3cSGerrit Uitslag
19872a66eb7SAndreas Gohr    $less = new lessc();
199343a31d8SAndreas Gohr    $less->importDir = array(DOKU_INC);
2007d247a3cSGerrit Uitslag    $less->setPreserveComments(!$conf['compress']);
20130f686ebSChristopher Smith
20230f686ebSChristopher Smith    if (defined('DOKU_UNITTEST')){
20330f686ebSChristopher Smith        $less->importDir[] = TMP_DIR;
20430f686ebSChristopher Smith    }
20530f686ebSChristopher Smith
20672a66eb7SAndreas Gohr    try {
20772a66eb7SAndreas Gohr        return $less->compile($css);
20872a66eb7SAndreas Gohr    } catch(Exception $e) {
20972a66eb7SAndreas Gohr        // get exception message
21072a66eb7SAndreas Gohr        $msg = str_replace(array("\n", "\r", "'"), array(), $e->getMessage());
21172a66eb7SAndreas Gohr
21272a66eb7SAndreas Gohr        // try to use line number to find affected file
21372a66eb7SAndreas Gohr        if(preg_match('/line: (\d+)$/', $msg, $m)){
21472a66eb7SAndreas Gohr            $msg = substr($msg, 0, -1* strlen($m[0])); //remove useless linenumber
21572a66eb7SAndreas Gohr            $lno = $m[1];
21672a66eb7SAndreas Gohr
21772a66eb7SAndreas Gohr            // walk upwards to last include
21872a66eb7SAndreas Gohr            $lines = explode("\n", $css);
21972a66eb7SAndreas Gohr            for($i=$lno-1; $i>=0; $i--){
22072a66eb7SAndreas Gohr                if(preg_match('/\/(\* XXXXXXXXX )(.*?)( XXXXXXXXX \*)\//', $lines[$i], $m)){
22172a66eb7SAndreas Gohr                    // we found it, add info to message
22272a66eb7SAndreas Gohr                    $msg .= ' in '.$m[2].' at line '.($lno-$i);
22372a66eb7SAndreas Gohr                    break;
22472a66eb7SAndreas Gohr                }
22572a66eb7SAndreas Gohr            }
22672a66eb7SAndreas Gohr        }
22772a66eb7SAndreas Gohr
22872a66eb7SAndreas Gohr        // something went wrong
22972a66eb7SAndreas Gohr        $error = 'A fatal error occured during compilation of the CSS files. '.
23072a66eb7SAndreas Gohr            'If you recently installed a new plugin or template it '.
23172a66eb7SAndreas Gohr            'might be broken and you should try disabling it again. ['.$msg.']';
23272a66eb7SAndreas Gohr
23372a66eb7SAndreas Gohr        echo ".dokuwiki:before {
23472a66eb7SAndreas Gohr            content: '$error';
23572a66eb7SAndreas Gohr            background-color: red;
23672a66eb7SAndreas Gohr            display: block;
23772a66eb7SAndreas Gohr            background-color: #fcc;
23872a66eb7SAndreas Gohr            border-color: #ebb;
23972a66eb7SAndreas Gohr            color: #000;
24072a66eb7SAndreas Gohr            padding: 0.5em;
24172a66eb7SAndreas Gohr        }";
24272a66eb7SAndreas Gohr
24372a66eb7SAndreas Gohr        exit;
24472a66eb7SAndreas Gohr    }
24572a66eb7SAndreas Gohr}
24672a66eb7SAndreas Gohr
24772a66eb7SAndreas Gohr/**
2486e69c1baSAndreas Gohr * Does placeholder replacements in the style according to
2496e69c1baSAndreas Gohr * the ones defined in a templates style.ini file
2506e69c1baSAndreas Gohr *
251d4a1ece8SAndreas Gohr * This also adds the ini defined placeholders as less variables
252d4a1ece8SAndreas Gohr * (sans the surrounding __ and with a ini_ prefix)
253d4a1ece8SAndreas Gohr *
2546e69c1baSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
255253d4b48SGerrit Uitslag *
256253d4b48SGerrit Uitslag * @param string $css
257253d4b48SGerrit Uitslag * @param array $replacements  array(placeholder => value)
258253d4b48SGerrit Uitslag * @return string
2596e69c1baSAndreas Gohr */
260afb2c082SAndreas Gohrfunction css_applystyle($css, $replacements) {
261cbe37079SAndreas Gohr    // we convert ini replacements to LESS variable names
262cbe37079SAndreas Gohr    // and build a list of variable: value; pairs
263d4a1ece8SAndreas Gohr    $less = '';
264afb2c082SAndreas Gohr    foreach((array) $replacements as $key => $value) {
265cbe37079SAndreas Gohr        $lkey = trim($key, '_');
266cbe37079SAndreas Gohr        $lkey = '@ini_'.$lkey;
267cbe37079SAndreas Gohr        $less .= "$lkey: $value;\n";
268cbe37079SAndreas Gohr
269afb2c082SAndreas Gohr        $replacements[$key] = $lkey;
270d4a1ece8SAndreas Gohr    }
271c51b334eSAndreas Gohr
272cbe37079SAndreas Gohr    // we now replace all old ini replacements with LESS variables
273afb2c082SAndreas Gohr    $css = strtr($css, $replacements);
274cbe37079SAndreas Gohr
275cbe37079SAndreas Gohr    // now prepend the list of LESS variables as the very first thing
276c51b334eSAndreas Gohr    $css = $less.$css;
2776e69c1baSAndreas Gohr    return $css;
2786e69c1baSAndreas Gohr}
2796e69c1baSAndreas Gohr
2806e69c1baSAndreas Gohr/**
281afb2c082SAndreas Gohr * Load style ini contents
2820ac69508SAnika Henke *
283afb2c082SAndreas Gohr * Loads and merges style.ini files from template and config and prepares
284afb2c082SAndreas Gohr * the stylesheet modes
285afb2c082SAndreas Gohr *
286afb2c082SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
287253d4b48SGerrit Uitslag *
288afb2c082SAndreas Gohr * @param string $tpl the used template
2894d6524b8SAndreas Gohr * @param bool   $preview load preview replacements
290afb2c082SAndreas Gohr * @return array with keys 'stylesheets' and 'replacements'
2910e6f9f08SAnika Henke */
29282edff4dSGerry Weißbachfunction css_styleini($tpl, $flavour=DEFAULT_FLAVOUR, $preview=false) {
2934d6524b8SAndreas Gohr    global $conf;
2944d6524b8SAndreas Gohr
295afb2c082SAndreas Gohr    $stylesheets = array(); // mode, file => base
296afb2c082SAndreas Gohr    $replacements = array(); // placeholder => value
2970ac69508SAnika Henke
29882edff4dSGerry Weißbach    // load template style.ini replacements first
29982edff4dSGerry Weißbach    // they should be the base for the colors - if not the default flavour
300afb2c082SAndreas Gohr    $incbase = tpl_incdir($tpl);
301afb2c082SAndreas Gohr    $webbase = tpl_basedir($tpl);
30282edff4dSGerry Weißbach    $ini = $incbase.DEFAULT_FLAVOUR.'.ini';
30382edff4dSGerry Weißbach    if($flavour != DEFAULT_FLAVOUR && file_exists($ini)) {
30482edff4dSGerry Weißbach        $data = parse_ini_file($ini, true);
30582edff4dSGerry Weißbach        // replacements
30682edff4dSGerry Weißbach        if(is_array($data['replacements'])) {
30782edff4dSGerry Weißbach            $replacements = array_merge($replacements, css_fixreplacementurls($data['replacements'], $webbase));
30882edff4dSGerry Weißbach        }
30982edff4dSGerry Weißbach    }
31082edff4dSGerry Weißbach
31182edff4dSGerry Weißbach    // load template's style.ini
31282edff4dSGerry Weißbach    $ini = $incbase.$flavour.'.ini';
313afb2c082SAndreas Gohr    if(file_exists($ini)){
314afb2c082SAndreas Gohr        $data = parse_ini_file($ini, true);
3150ac69508SAnika Henke
316afb2c082SAndreas Gohr        // stylesheets
317afb2c082SAndreas Gohr        if(is_array($data['stylesheets'])) foreach($data['stylesheets'] as $file => $mode){
318afb2c082SAndreas Gohr            $stylesheets[$mode][$incbase.$file] = $webbase;
319afb2c082SAndreas Gohr        }
320afb2c082SAndreas Gohr        // replacements
32182edff4dSGerry Weißbach        if(isset($data['replacements']) && is_array($data['replacements'])){
32247f862d1SChristopher Smith            $replacements = array_merge($replacements, css_fixreplacementurls($data['replacements'],$webbase));
3230ac69508SAnika Henke        }
3240ac69508SAnika Henke    }
325afb2c082SAndreas Gohr
326afb2c082SAndreas Gohr    // load configs's style.ini
327afb2c082SAndreas Gohr    $webbase = DOKU_BASE;
3288c5aad7bSAnika Henke    $ini = DOKU_CONF."tpl/$tpl/style.ini";
3298c5aad7bSAnika Henke    $incbase = dirname($ini).'/';
330afb2c082SAndreas Gohr    if(file_exists($ini)){
331afb2c082SAndreas Gohr        $data = parse_ini_file($ini, true);
332afb2c082SAndreas Gohr
33382edff4dSGerry Weißbach        // stylesheets - but only if we are on the default flavour
33482edff4dSGerry Weißbach        if($flavour === DEFAULT_FLAVOUR && isset($data['stylesheets']) && is_array($data['stylesheets'])) foreach($data['stylesheets'] as $file => $mode){
335afb2c082SAndreas Gohr            $stylesheets[$mode][$incbase.$file] = $webbase;
3360ac69508SAnika Henke        }
337afb2c082SAndreas Gohr        // replacements
33806c9ee33SMarius van Witzenburg        if(isset($data['replacements']) && is_array($data['replacements'])){
33947f862d1SChristopher Smith            $replacements = array_merge($replacements, css_fixreplacementurls($data['replacements'],$webbase));
340afb2c082SAndreas Gohr        }
341afb2c082SAndreas Gohr    }
342afb2c082SAndreas Gohr
3434d6524b8SAndreas Gohr    // allow replacement overwrites in preview mode
3444d6524b8SAndreas Gohr    if($preview) {
3454d6524b8SAndreas Gohr        $webbase = DOKU_BASE;
3464d6524b8SAndreas Gohr        $ini     = $conf['cachedir'].'/preview.ini';
3474d6524b8SAndreas Gohr        if(file_exists($ini)) {
3484d6524b8SAndreas Gohr            $data = parse_ini_file($ini, true);
3494d6524b8SAndreas Gohr            // replacements
3504d6524b8SAndreas Gohr            if(is_array($data['replacements'])) {
3514d6524b8SAndreas Gohr                $replacements = array_merge($replacements, css_fixreplacementurls($data['replacements'], $webbase));
3524d6524b8SAndreas Gohr            }
3534d6524b8SAndreas Gohr        }
3544d6524b8SAndreas Gohr    }
3554d6524b8SAndreas Gohr
356afb2c082SAndreas Gohr    return array(
357afb2c082SAndreas Gohr        'stylesheets' => $stylesheets,
358afb2c082SAndreas Gohr        'replacements' => $replacements
359afb2c082SAndreas Gohr    );
3600e6f9f08SAnika Henke}
3610e6f9f08SAnika Henke
3621e2c5948SChristopher Smith/**
3631e2c5948SChristopher Smith * Amend paths used in replacement relative urls, refer FS#2879
3641e2c5948SChristopher Smith *
3651e2c5948SChristopher Smith * @author Chris Smith <chris@jalakai.co.uk>
366253d4b48SGerrit Uitslag *
367253d4b48SGerrit Uitslag * @param array $replacements with key-value pairs
368253d4b48SGerrit Uitslag * @param string $location
369253d4b48SGerrit Uitslag * @return array
3701e2c5948SChristopher Smith */
37147f862d1SChristopher Smithfunction css_fixreplacementurls($replacements, $location) {
37247f862d1SChristopher Smith    foreach($replacements as $key => $value) {
37347f862d1SChristopher Smith        $replacements[$key] = preg_replace('#(url\([ \'"]*)(?!/|data:|http://|https://| |\'|")#','\\1'.$location,$value);
37447f862d1SChristopher Smith    }
37547f862d1SChristopher Smith    return $replacements;
37647f862d1SChristopher Smith}
37747f862d1SChristopher Smith
3780e6f9f08SAnika Henke/**
379ef36714bSGerry Weißbach * Wrapper for the files, content and mediatype for the event CSS_STYLES_INCLUDED
380ef36714bSGerry Weißbach *
381ef36714bSGerry Weißbach * @author Gerry Weißbach <gerry.w@gammaproduction.de>
382ef36714bSGerry Weißbach *
383ef36714bSGerry Weißbach * @param string $mediatype type ofthe current media files/content set
384ef36714bSGerry Weißbach * @param array $files set of files that define the current mediatype
385ef36714bSGerry Weißbach * @return array
386ef36714bSGerry Weißbach */
387ef36714bSGerry Weißbachfunction css_filewrapper($mediatype, $flavour=DEFAULT_FLAVOUR, $files=array()){
388ef36714bSGerry Weißbach    return array(
389ef36714bSGerry Weißbach            'files'                 => $files,
390ef36714bSGerry Weißbach            'mediatype'             => $mediatype,
391ef36714bSGerry Weißbach            'flavour'               => $flavour,
392ef36714bSGerry Weißbach            'encapsulate'           => in_array($mediatype, array('screen', 'print', 'handheld')),
393ef36714bSGerry Weißbach            'encapsulationPrefix'   => '@media '.$mediatype
394ef36714bSGerry Weißbach        );
395ef36714bSGerry Weißbach}
396ef36714bSGerry Weißbach
397ef36714bSGerry Weißbach/**
398ef36714bSGerry Weißbach * Prints the @media encapsulated default styles of DokuWiki
399ef36714bSGerry Weißbach *
400ef36714bSGerry Weißbach * @author Gerry Weißbach <gerry.w@gammaproduction.de>
401ef36714bSGerry Weißbach *
402ef36714bSGerry Weißbach * This function is being called by a CSS_STYLES_INCLUDED event
403ef36714bSGerry Weißbach * The event can be distinguished by the mediatype which is:
404ef36714bSGerry Weißbach *   DW_DEFAULT
405ef36714bSGerry Weißbach */
406ef36714bSGerry Weißbachfunction css_defaultstyles(){
407ef36714bSGerry Weißbach    // print the default classes for interwiki links and file downloads
408ef36714bSGerry Weißbach    print '@media screen {';
409ef36714bSGerry Weißbach    css_interwiki();
410ef36714bSGerry Weißbach    css_filetypes();
411ef36714bSGerry Weißbach    print '}';
412ef36714bSGerry Weißbach}
413ef36714bSGerry Weißbach
414ef36714bSGerry Weißbach/**
4151c2d1019SAndreas Gohr * Prints classes for interwikilinks
4161c2d1019SAndreas Gohr *
4171c2d1019SAndreas Gohr * Interwiki links have two classes: 'interwiki' and 'iw_$name>' where
4181c2d1019SAndreas Gohr * $name is the identifier given in the config. All Interwiki links get
4191c2d1019SAndreas Gohr * an default style with a default icon. If a special icon is available
4201c2d1019SAndreas Gohr * for an interwiki URL it is set in it's own class. Both classes can be
4211c2d1019SAndreas Gohr * overwritten in the template or userstyles.
4221c2d1019SAndreas Gohr *
4231c2d1019SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
4241c2d1019SAndreas Gohr */
4251c2d1019SAndreas Gohrfunction css_interwiki(){
4261c2d1019SAndreas Gohr
4271c2d1019SAndreas Gohr    // default style
4281c2d1019SAndreas Gohr    echo 'a.interwiki {';
4291c2d1019SAndreas Gohr    echo ' background: transparent url('.DOKU_BASE.'lib/images/interwiki.png) 0px 1px no-repeat;';
4307b4ea081Smarklundeberg    echo ' padding: 1px 0px 1px 16px;';
4311c2d1019SAndreas Gohr    echo '}';
4321c2d1019SAndreas Gohr
4331c2d1019SAndreas Gohr    // additional styles when icon available
4341c2d1019SAndreas Gohr    $iwlinks = getInterwiki();
4351c2d1019SAndreas Gohr    foreach(array_keys($iwlinks) as $iw){
4369d2ddea4SAndreas Gohr        $class = preg_replace('/[^_\-a-z0-9]+/i','_',$iw);
43779e79377SAndreas Gohr        if(file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.png')){
4389d2ddea4SAndreas Gohr            echo "a.iw_$class {";
4391c2d1019SAndreas Gohr            echo '  background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.png)';
4401c2d1019SAndreas Gohr            echo '}';
44179e79377SAndreas Gohr        }elseif(file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.gif')){
4429d2ddea4SAndreas Gohr            echo "a.iw_$class {";
4431c2d1019SAndreas Gohr            echo '  background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.gif)';
4441c2d1019SAndreas Gohr            echo '}';
4451c2d1019SAndreas Gohr        }
4461c2d1019SAndreas Gohr    }
447d15166e5SAndreas Gohr}
4481c2d1019SAndreas Gohr
449d15166e5SAndreas Gohr/**
450d15166e5SAndreas Gohr * Prints classes for file download links
451d15166e5SAndreas Gohr *
452d15166e5SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
453d15166e5SAndreas Gohr */
454d15166e5SAndreas Gohrfunction css_filetypes(){
455d15166e5SAndreas Gohr
456d15166e5SAndreas Gohr    // default style
457035e07f1SKate Arzamastseva    echo '.mediafile {';
458d15166e5SAndreas Gohr    echo ' background: transparent url('.DOKU_BASE.'lib/images/fileicons/file.png) 0px 1px no-repeat;';
4595b77caf4SAndreas Gohr    echo ' padding-left: 18px;';
4605b77caf4SAndreas Gohr    echo ' padding-bottom: 1px;';
461d15166e5SAndreas Gohr    echo '}';
462d15166e5SAndreas Gohr
463d15166e5SAndreas Gohr    // additional styles when icon available
46427bf7924STom N Harris    // scan directory for all icons
46527bf7924STom N Harris    $exts = array();
46627bf7924STom N Harris    if($dh = opendir(DOKU_INC.'lib/images/fileicons')){
46727bf7924STom N Harris        while(false !== ($file = readdir($dh))){
46827bf7924STom N Harris            if(preg_match('/([_\-a-z0-9]+(?:\.[_\-a-z0-9]+)*?)\.(png|gif)/i',$file,$match)){
46927bf7924STom N Harris                $ext = strtolower($match[1]);
47027bf7924STom N Harris                $type = '.'.strtolower($match[2]);
47127bf7924STom N Harris                if($ext!='file' && (!isset($exts[$ext]) || $type=='.png')){
47227bf7924STom N Harris                    $exts[$ext] = $type;
473d15166e5SAndreas Gohr                }
474d15166e5SAndreas Gohr            }
4751c2d1019SAndreas Gohr        }
47627bf7924STom N Harris        closedir($dh);
47727bf7924STom N Harris    }
47827bf7924STom N Harris    foreach($exts as $ext=>$type){
47927bf7924STom N Harris        $class = preg_replace('/[^_\-a-z0-9]+/','_',$ext);
480035e07f1SKate Arzamastseva        echo ".mf_$class {";
48127bf7924STom N Harris        echo '  background-image: url('.DOKU_BASE.'lib/images/fileicons/'.$ext.$type.')';
48227bf7924STom N Harris        echo '}';
48327bf7924STom N Harris    }
48427bf7924STom N Harris}
4851c2d1019SAndreas Gohr
4861c2d1019SAndreas Gohr/**
48778a6aeb1SAndreas Gohr * Loads a given file and fixes relative URLs with the
48878a6aeb1SAndreas Gohr * given location prefix
489253d4b48SGerrit Uitslag *
490253d4b48SGerrit Uitslag * @param string $file file system path
491253d4b48SGerrit Uitslag * @param string $location
492253d4b48SGerrit Uitslag * @return string
49378a6aeb1SAndreas Gohr */
49478a6aeb1SAndreas Gohrfunction css_loadfile($file,$location=''){
49512ffbbc3SChristopher Smith    $css_file = new DokuCssFile($file);
49612ffbbc3SChristopher Smith    return $css_file->load($location);
49712ffbbc3SChristopher Smith}
49812ffbbc3SChristopher Smith
4991e2c5948SChristopher Smith/**
5001e2c5948SChristopher Smith *  Helper class to abstract loading of css/less files
5011e2c5948SChristopher Smith *
5021e2c5948SChristopher Smith *  @author Chris Smith <chris@jalakai.co.uk>
5031e2c5948SChristopher Smith */
50412ffbbc3SChristopher Smithclass DokuCssFile {
50512ffbbc3SChristopher Smith
5061e2c5948SChristopher Smith    protected $filepath;             // file system path to the CSS/Less file
5071e2c5948SChristopher Smith    protected $location;             // base url location of the CSS/Less file
508fd18b5f4SGerrit Uitslag    protected $relative_path = null;
50912ffbbc3SChristopher Smith
51012ffbbc3SChristopher Smith    public function __construct($file) {
51112ffbbc3SChristopher Smith        $this->filepath = $file;
51212ffbbc3SChristopher Smith    }
51312ffbbc3SChristopher Smith
5141e2c5948SChristopher Smith    /**
5151e2c5948SChristopher Smith     * Load the contents of the css/less file and adjust any relative paths/urls (relative to this file) to be
5161e2c5948SChristopher Smith     * relative to the dokuwiki root: the web root (DOKU_BASE) for most files; the file system root (DOKU_INC)
5171e2c5948SChristopher Smith     * for less files.
5181e2c5948SChristopher Smith     *
5191e2c5948SChristopher Smith     * @param   string   $location   base url for this file
5201e2c5948SChristopher Smith     * @return  string               the CSS/Less contents of the file
5211e2c5948SChristopher Smith     */
52212ffbbc3SChristopher Smith    public function load($location='') {
52379e79377SAndreas Gohr        if (!file_exists($this->filepath)) return '';
52412ffbbc3SChristopher Smith
52512ffbbc3SChristopher Smith        $css = io_readFile($this->filepath);
52678a6aeb1SAndreas Gohr        if (!$location) return $css;
52778a6aeb1SAndreas Gohr
52812ffbbc3SChristopher Smith        $this->location = $location;
529de737055SChristopher Smith
53012ffbbc3SChristopher Smith        $css = preg_replace_callback('#(url\( *)([\'"]?)(.*?)(\2)( *\))#',array($this,'replacements'),$css);
53112ffbbc3SChristopher Smith        $css = preg_replace_callback('#(@import\s+)([\'"])(.*?)(\2)#',array($this,'replacements'),$css);
532809d3ba5SAndreas Gohr
53378a6aeb1SAndreas Gohr        return $css;
53478a6aeb1SAndreas Gohr    }
53578a6aeb1SAndreas Gohr
5361e2c5948SChristopher Smith    /**
5371e2c5948SChristopher Smith     * Get the relative file system path of this file, relative to dokuwiki's root folder, DOKU_INC
5381e2c5948SChristopher Smith     *
5391e2c5948SChristopher Smith     * @return string   relative file system path
5401e2c5948SChristopher Smith     */
541fd18b5f4SGerrit Uitslag    protected function getRelativePath(){
54212ffbbc3SChristopher Smith
54312ffbbc3SChristopher Smith        if (is_null($this->relative_path)) {
54412ffbbc3SChristopher Smith            $basedir = array(DOKU_INC);
5451e2c5948SChristopher Smith
5461e2c5948SChristopher Smith            // during testing, files may be found relative to a second base dir, TMP_DIR
54712ffbbc3SChristopher Smith            if (defined('DOKU_UNITTEST')) {
54812ffbbc3SChristopher Smith                $basedir[] = realpath(TMP_DIR);
54912ffbbc3SChristopher Smith            }
55012ffbbc3SChristopher Smith
55173f25ac0SAndreas Gohr            $basedir = array_map('preg_quote_cb', $basedir);
55273f25ac0SAndreas Gohr            $regex = '/^('.join('|',$basedir).')/';
55312ffbbc3SChristopher Smith            $this->relative_path = preg_replace($regex, '', dirname($this->filepath));
55412ffbbc3SChristopher Smith        }
55512ffbbc3SChristopher Smith
55612ffbbc3SChristopher Smith        return $this->relative_path;
55712ffbbc3SChristopher Smith    }
55812ffbbc3SChristopher Smith
5591e2c5948SChristopher Smith    /**
5601e2c5948SChristopher Smith     * preg_replace callback to adjust relative urls from relative to this file to relative
5611e2c5948SChristopher Smith     * to the appropriate dokuwiki root location as described in the code
5621e2c5948SChristopher Smith     *
5631e2c5948SChristopher Smith     * @param  array    see http://php.net/preg_replace_callback
5641e2c5948SChristopher Smith     * @return string   see http://php.net/preg_replace_callback
5651e2c5948SChristopher Smith     */
56612ffbbc3SChristopher Smith    public function replacements($match) {
567de737055SChristopher Smith
5681e2c5948SChristopher Smith        // not a relative url? - no adjustment required
569de737055SChristopher Smith        if (preg_match('#^(/|data:|https?://)#',$match[3])) {
570de737055SChristopher Smith            return $match[0];
571de737055SChristopher Smith        }
5721e2c5948SChristopher Smith        // a less file import? - requires a file system location
573de737055SChristopher Smith        else if (substr($match[3],-5) == '.less') {
574de737055SChristopher Smith            if ($match[3]{0} != '/') {
57512ffbbc3SChristopher Smith                $match[3] = $this->getRelativePath() . '/' . $match[3];
576de737055SChristopher Smith            }
577de737055SChristopher Smith        }
5781e2c5948SChristopher Smith        // everything else requires a url adjustment
579de737055SChristopher Smith        else {
58012ffbbc3SChristopher Smith            $match[3] = $this->location . $match[3];
581de737055SChristopher Smith        }
582de737055SChristopher Smith
583de737055SChristopher Smith        return join('',array_slice($match,1));
584de737055SChristopher Smith    }
58512ffbbc3SChristopher Smith}
586de737055SChristopher Smith
587809d3ba5SAndreas Gohr/**
5884eb5f931SChristopher Smith * Convert local image URLs to data URLs if the filesize is small
589809d3ba5SAndreas Gohr *
590809d3ba5SAndreas Gohr * Callback for preg_replace_callback
591253d4b48SGerrit Uitslag *
592253d4b48SGerrit Uitslag * @param array $match
593253d4b48SGerrit Uitslag * @return string
594809d3ba5SAndreas Gohr */
595809d3ba5SAndreas Gohrfunction css_datauri($match){
59628f4004cSAndreas Gohr    global $conf;
59728f4004cSAndreas Gohr
598809d3ba5SAndreas Gohr    $pre   = unslash($match[1]);
599809d3ba5SAndreas Gohr    $base  = unslash($match[2]);
600809d3ba5SAndreas Gohr    $url   = unslash($match[3]);
601809d3ba5SAndreas Gohr    $ext   = unslash($match[4]);
602809d3ba5SAndreas Gohr
603809d3ba5SAndreas Gohr    $local = DOKU_INC.$url;
604809d3ba5SAndreas Gohr    $size  = @filesize($local);
60528f4004cSAndreas Gohr    if($size && $size < $conf['cssdatauri']){
606809d3ba5SAndreas Gohr        $data = base64_encode(file_get_contents($local));
607809d3ba5SAndreas Gohr    }
608809d3ba5SAndreas Gohr    if($data){
6096a5d6817SAnika Henke        $url = 'data:image/'.$ext.';base64,'.$data;
610809d3ba5SAndreas Gohr    }else{
611809d3ba5SAndreas Gohr        $url = $base.$url;
612809d3ba5SAndreas Gohr    }
613809d3ba5SAndreas Gohr    return $pre.$url;
614809d3ba5SAndreas Gohr}
615809d3ba5SAndreas Gohr
61615c394afSAndreas Gohr
61778a6aeb1SAndreas Gohr/**
61878a6aeb1SAndreas Gohr * Returns a list of possible Plugin Styles (no existance check here)
61978a6aeb1SAndreas Gohr *
62078a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
621253d4b48SGerrit Uitslag *
622253d4b48SGerrit Uitslag * @param string $mediatype
623253d4b48SGerrit Uitslag * @return array
62478a6aeb1SAndreas Gohr */
625318cd03eSAnika Henkefunction css_pluginstyles($mediatype='screen'){
62678a6aeb1SAndreas Gohr    $list = array();
62778a6aeb1SAndreas Gohr    $plugins = plugin_list();
62878a6aeb1SAndreas Gohr    foreach ($plugins as $p){
629318cd03eSAnika Henke        $list[DOKU_PLUGIN."$p/$mediatype.css"]  = DOKU_BASE."lib/plugins/$p/";
630d4a1ece8SAndreas Gohr        $list[DOKU_PLUGIN."$p/$mediatype.less"]  = DOKU_BASE."lib/plugins/$p/";
631318cd03eSAnika Henke        // alternative for screen.css
632318cd03eSAnika Henke        if ($mediatype=='screen') {
63378a6aeb1SAndreas Gohr            $list[DOKU_PLUGIN."$p/style.css"]  = DOKU_BASE."lib/plugins/$p/";
634d4a1ece8SAndreas Gohr            $list[DOKU_PLUGIN."$p/style.less"]  = DOKU_BASE."lib/plugins/$p/";
63578a6aeb1SAndreas Gohr        }
63678a6aeb1SAndreas Gohr    }
63778a6aeb1SAndreas Gohr    return $list;
63878a6aeb1SAndreas Gohr}
63978a6aeb1SAndreas Gohr
64078a6aeb1SAndreas Gohr/**
64178a6aeb1SAndreas Gohr * Very simple CSS optimizer
64278a6aeb1SAndreas Gohr *
64378a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
644253d4b48SGerrit Uitslag *
645253d4b48SGerrit Uitslag * @param string $css
646253d4b48SGerrit Uitslag * @return string
64778a6aeb1SAndreas Gohr */
64878a6aeb1SAndreas Gohrfunction css_compress($css){
649fd7c2db0SAndreas Gohr    //strip comments through a callback
650fd7c2db0SAndreas Gohr    $css = preg_replace_callback('#(/\*)(.*?)(\*/)#s','css_comment_cb',$css);
651fd7c2db0SAndreas Gohr
652247c1c5dSAndreas Gohr    //strip (incorrect but common) one line comments
653fe5a50c3SAndreas Gohr    $css = preg_replace_callback('/^.*\/\/.*$/m','css_onelinecomment_cb',$css);
654247c1c5dSAndreas Gohr
65578a6aeb1SAndreas Gohr    // strip whitespaces
65678a6aeb1SAndreas Gohr    $css = preg_replace('![\r\n\t ]+!',' ',$css);
657f5379589SChristopher Smith    $css = preg_replace('/ ?([;,{}\/]) ?/','\\1',$css);
658f5379589SChristopher Smith    $css = preg_replace('/ ?: /',':',$css);
65978a6aeb1SAndreas Gohr
660205907a7Sfurun    // number compression
661205907a7Sfurun    $css = preg_replace('/([: ])0+(\.\d+?)0*((?:pt|pc|in|mm|cm|em|ex|px)\b|%)(?=[^\{]*[;\}])/', '$1$2$3', $css); // "0.1em" to ".1em", "1.10em" to "1.1em"
662205907a7Sfurun    $css = preg_replace('/([: ])\.(0)+((?:pt|pc|in|mm|cm|em|ex|px)\b|%)(?=[^\{]*[;\}])/', '$1$2', $css); // ".0em" to "0"
663205907a7Sfurun    $css = preg_replace('/([: ]0)0*(\.0*)?((?:pt|pc|in|mm|cm|em|ex|px)(?=[^\{]*[;\}])\b|%)/', '$1', $css); // "0.0em" to "0"
664205907a7Sfurun    $css = preg_replace('/([: ]\d+)(\.0*)((?:pt|pc|in|mm|cm|em|ex|px)(?=[^\{]*[;\}])\b|%)/', '$1$3', $css); // "1.0em" to "1em"
665205907a7Sfurun    $css = preg_replace('/([: ])0+(\d+|\d*\.\d+)((?:pt|pc|in|mm|cm|em|ex|px)(?=[^\{]*[;\}])\b|%)/', '$1$2$3', $css); // "001em" to "1em"
666205907a7Sfurun
667205907a7Sfurun    // shorten attributes (1em 1em 1em 1em -> 1em)
668205907a7Sfurun    $css = preg_replace('/(?<![\w\-])((?:margin|padding|border|border-(?:width|radius)):)([\w\.]+)( \2)+(?=[;\}]| !)/', '$1$2', $css); // "1em 1em 1em 1em" to "1em"
669205907a7Sfurun    $css = preg_replace('/(?<![\w\-])((?:margin|padding|border|border-(?:width)):)([\w\.]+) ([\w\.]+) \2 \3(?=[;\}]| !)/', '$1$2 $3', $css); // "1em 2em 1em 2em" to "1em 2em"
670205907a7Sfurun
67178a6aeb1SAndreas Gohr    // shorten colors
672205907a7Sfurun    $css = preg_replace("/#([0-9a-fA-F]{1})\\1([0-9a-fA-F]{1})\\2([0-9a-fA-F]{1})\\3(?=[^\{]*[;\}])/", "#\\1\\2\\3", $css);
67378a6aeb1SAndreas Gohr
67478a6aeb1SAndreas Gohr    return $css;
67578a6aeb1SAndreas Gohr}
67678a6aeb1SAndreas Gohr
677c00aef76SAndreas Gohr/**
678c00aef76SAndreas Gohr * Callback for css_compress()
679c00aef76SAndreas Gohr *
680c00aef76SAndreas Gohr * Keeps short comments (< 5 chars) to maintain typical browser hacks
681c00aef76SAndreas Gohr *
682c00aef76SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
683253d4b48SGerrit Uitslag *
684253d4b48SGerrit Uitslag * @param array $matches
685253d4b48SGerrit Uitslag * @return string
686c00aef76SAndreas Gohr */
687c00aef76SAndreas Gohrfunction css_comment_cb($matches){
688c00aef76SAndreas Gohr    if(strlen($matches[2]) > 4) return '';
689c00aef76SAndreas Gohr    return $matches[0];
690c00aef76SAndreas Gohr}
69178a6aeb1SAndreas Gohr
692fe5a50c3SAndreas Gohr/**
693fe5a50c3SAndreas Gohr * Callback for css_compress()
694fe5a50c3SAndreas Gohr *
695fe5a50c3SAndreas Gohr * Strips one line comments but makes sure it will not destroy url() constructs with slashes
696fe5a50c3SAndreas Gohr *
697253d4b48SGerrit Uitslag * @param array $matches
698fe5a50c3SAndreas Gohr * @return string
699fe5a50c3SAndreas Gohr */
700fe5a50c3SAndreas Gohrfunction css_onelinecomment_cb($matches) {
701fe5a50c3SAndreas Gohr    $line = $matches[0];
702fe5a50c3SAndreas Gohr
703fe5a50c3SAndreas Gohr    $i = 0;
704fe5a50c3SAndreas Gohr    $len = strlen($line);
705918a4468SAndreas Gohr
706fe5a50c3SAndreas Gohr    while ($i< $len){
707fe5a50c3SAndreas Gohr        $nextcom = strpos($line, '//', $i);
708fe5a50c3SAndreas Gohr        $nexturl = stripos($line, 'url(', $i);
709fe5a50c3SAndreas Gohr
710fe5a50c3SAndreas Gohr        if($nextcom === false) {
711fe5a50c3SAndreas Gohr            // no more comments, we're done
712918a4468SAndreas Gohr            $i = $len;
713fe5a50c3SAndreas Gohr            break;
714fe5a50c3SAndreas Gohr        }
715fe5a50c3SAndreas Gohr
716918a4468SAndreas Gohr        // keep any quoted string that starts before a comment
717918a4468SAndreas Gohr        $nextsqt = strpos($line, "'", $i);
718918a4468SAndreas Gohr        $nextdqt = strpos($line, '"', $i);
719918a4468SAndreas Gohr        if(min($nextsqt, $nextdqt) < $nextcom) {
720918a4468SAndreas Gohr            $skipto = false;
721918a4468SAndreas Gohr            if($nextsqt !== false && ($nextdqt === false || $nextsqt < $nextdqt)) {
722918a4468SAndreas Gohr                $skipto = strpos($line, "'", $nextsqt+1) +1;
723918a4468SAndreas Gohr            } else if ($nextdqt !== false) {
724918a4468SAndreas Gohr                $skipto = strpos($line, '"', $nextdqt+1) +1;
725918a4468SAndreas Gohr            }
726918a4468SAndreas Gohr
727918a4468SAndreas Gohr            if($skipto !== false) {
728918a4468SAndreas Gohr                $i = $skipto;
729918a4468SAndreas Gohr                continue;
730918a4468SAndreas Gohr            }
731918a4468SAndreas Gohr        }
732918a4468SAndreas Gohr
733918a4468SAndreas Gohr        if($nexturl === false || $nextcom < $nexturl) {
734918a4468SAndreas Gohr            // no url anymore, strip comment and be done
735918a4468SAndreas Gohr            $i = $nextcom;
736918a4468SAndreas Gohr            break;
737918a4468SAndreas Gohr        }
738918a4468SAndreas Gohr
739918a4468SAndreas Gohr        // we have an upcoming url
740918a4468SAndreas Gohr        $i = strpos($line, ')', $nexturl);
741918a4468SAndreas Gohr    }
742918a4468SAndreas Gohr
743918a4468SAndreas Gohr    return substr($line, 0, $i);
744fe5a50c3SAndreas Gohr}
745fe5a50c3SAndreas Gohr
746e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
747