xref: /dokuwiki/lib/exe/css.php (revision ef36714b78f2a947428d002dfcf4696c73bd0a5d)
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');
38de4634ecSGerry Weißbach    } else {
398b3ff808SGerry Weißbach        $mediatypes = array('screen', 'all', 'print', 'handheld');
40de4634ecSGerry Weißbach    }
41de4634ecSGerry Weißbach
4220a2375aSAndreas Gohr    // decide from where to get the template
4320a2375aSAndreas Gohr    $tpl = trim(preg_replace('/[^\w-]+/','',$INPUT->str('t')));
4420a2375aSAndreas Gohr    if(!$tpl) $tpl = $conf['template'];
4520a2375aSAndreas Gohr
4682edff4dSGerry Weißbach    // decide from where to get the template
4782edff4dSGerry Weißbach    $flavour = trim(preg_replace('/[^\w-]+/','',$INPUT->str('f')));
4882edff4dSGerry Weißbach    if(!$flavour) $flavour = DEFAULT_FLAVOUR;
49de4634ecSGerry Weißbach
5020a2375aSAndreas Gohr    // load styl.ini
5182edff4dSGerry Weißbach    $styleini = css_styleini($tpl, $flavour, $INPUT->bool('preview'));
5220a2375aSAndreas Gohr
53afb2c082SAndreas Gohr    // cache influencers
54259571aaSMichal Koutný    $tplinc = tpl_incdir($tpl);
55afb2c082SAndreas Gohr    $cache_files = getConfigFiles('main');
56afb2c082SAndreas Gohr    $cache_files[] = $tplinc.'style.ini';
57afb2c082SAndreas Gohr    $cache_files[] = DOKU_CONF."tpl/$tpl/style.ini";
5882edff4dSGerry Weißbach    $cache_files[] = DOKU_CONF."tpl/$tpl/ini/".$flavour.".ini";
59afb2c082SAndreas Gohr    $cache_files[] = __FILE__;
604d6524b8SAndreas Gohr    if($INPUT->bool('preview')) $cache_files[] = $conf['cachedir'].'/preview.ini';
61afb2c082SAndreas Gohr
6278a6aeb1SAndreas Gohr    // Array of needed files and their web locations, the latter ones
6378a6aeb1SAndreas Gohr    // are needed to fix relative paths in the stylesheets
64*ef36714bSGerry Weißbach    // We only need to build the list for the requested flavour.
65*ef36714bSGerry Weißbach    $media_files = array();
6614977bd2SMichael Hamann    foreach($mediatypes as $mediatype) {
67*ef36714bSGerry Weißbach        $files = array();
68*ef36714bSGerry Weißbach        if ( $flavour === DEFAULT_FLAVOUR ) {
69318cd03eSAnika Henke            // load core styles
70*ef36714bSGerry Weißbach            $files[DOKU_INC.'lib/styles/'.$mediatype.'.css'] = DOKU_BASE.'lib/styles/';
71af4684acSAndreas Gohr
7243576758SAndreas Gohr            // load jQuery-UI theme
736c47a78cSAnika Henke            if ($mediatype == 'screen') {
74*ef36714bSGerry Weißbach                $files[DOKU_INC.'lib/scripts/jquery/jquery-ui-theme/smoothness.css'] = DOKU_BASE.'lib/scripts/jquery/jquery-ui-theme/';
756c47a78cSAnika Henke            }
76318cd03eSAnika Henke            // load plugin styles
77*ef36714bSGerry Weißbach            $files = array_merge($files, css_pluginstyles($mediatype));
78318cd03eSAnika Henke            // load template styles
79afb2c082SAndreas Gohr            if (isset($styleini['stylesheets'][$mediatype])) {
80*ef36714bSGerry Weißbach                $files = array_merge($files, $styleini['stylesheets'][$mediatype]);
8109edb711SAndreas Gohr            }
82318cd03eSAnika Henke            // load user styles
837b909d5eSGerrit Uitslag            if(!empty($config_cascade['userstyle'][$mediatype])) {
847b909d5eSGerrit Uitslag                foreach($config_cascade['userstyle'][$mediatype] as $userstyle) {
85*ef36714bSGerry Weißbach                    $files[$userstyle] = DOKU_BASE;
867b909d5eSGerrit Uitslag                }
87318cd03eSAnika Henke            }
88*ef36714bSGerry Weißbach        } else if (isset($styleini['stylesheets'][$mediatype])) {
89*ef36714bSGerry Weißbach            // only allow the predefined media types
90*ef36714bSGerry Weißbach            $files = $styleini['stylesheets'][$mediatype];
91*ef36714bSGerry Weißbach        }
9278a6aeb1SAndreas Gohr
93*ef36714bSGerry Weißbach        // Let plugins decide to either put more styles here or to remove some
94*ef36714bSGerry Weißbach        $media_files[$mediatype] = css_filewrapper($mediatype, $flavour, $files);
95*ef36714bSGerry Weißbach    	$CSSEvt = new Doku_Event('CSS_STYLES_INCLUDED', $media_files[$mediatype]);
96*ef36714bSGerry Weißbach
97*ef36714bSGerry Weißbach        // Make it preventable.
98*ef36714bSGerry Weißbach    	if ( $CSSEvt->advise_before() ) {
99*ef36714bSGerry Weißbach            $cache_files = array_merge($cache_files, array_keys($media_files[$mediatype]['files']));
100*ef36714bSGerry Weißbach    	} else {
101*ef36714bSGerry Weißbach        	// unset if prevented. Nothing will be printed for this mediatype.
102*ef36714bSGerry Weißbach        	unset($media_files[$mediatype]);
10314977bd2SMichael Hamann    	}
1046619f42eSAdrian Lang
105*ef36714bSGerry Weißbach    	// finish event.
106*ef36714bSGerry Weißbach    	$CSSEvt->advise_after();
107*ef36714bSGerry Weißbach    }
108*ef36714bSGerry Weißbach
109*ef36714bSGerry Weißbach    // The generated script depends on some dynamic options
110*ef36714bSGerry Weißbach    $cache = new cache('styles'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'].DOKU_BASE.$tpl.md5(serialize($cache_files)),'.css');
111*ef36714bSGerry Weißbach    $cache->_event = 'CSS_CACHE_USE';
112*ef36714bSGerry Weißbach
11338f56bffSBen Coburn    // check cache age & handle conditional request
1146619f42eSAdrian Lang    // This may exit if a cache can be used
115*ef36714bSGerry Weißbach    $cache_ok = $cache->useCache(array('files' => $cache_files));
116*ef36714bSGerry Weißbach    http_cached($cache->cache, $cache_ok);
11778a6aeb1SAndreas Gohr
1183899c2ecSMichael Hamann    // start output buffering
1193899c2ecSMichael Hamann    ob_start();
1203899c2ecSMichael Hamann
121*ef36714bSGerry Weißbach    if ( $flavour === DEFAULT_FLAVOUR ) {
122*ef36714bSGerry Weißbach        // Fire CSS_STYLES_INCLUDED for one last time to let the
123*ef36714bSGerry Weißbach        // plugins decide whether to include the DW default styles.
124*ef36714bSGerry Weißbach        // This can be done by preventing the Default.
125*ef36714bSGerry Weißbach        $media_files['DW_DEFAULT'] = css_filewrapper('DW_DEFAULT');
126*ef36714bSGerry Weißbach        trigger_event('CSS_STYLES_INCLUDED', $media_files['DW_DEFAULT'], 'css_defaultstyles');
127*ef36714bSGerry Weißbach    }
128*ef36714bSGerry Weißbach
1296c47a78cSAnika Henke    // build the stylesheet
13014977bd2SMichael Hamann    foreach ($mediatypes as $mediatype) {
13178a6aeb1SAndreas Gohr
132*ef36714bSGerry Weißbach        // Check if there is a wrapper set for this type.
133*ef36714bSGerry Weißbach        if ( !isset($media_files[$mediatype]) ) {
134*ef36714bSGerry Weißbach            continue;
13578a6aeb1SAndreas Gohr        }
13678a6aeb1SAndreas Gohr
137*ef36714bSGerry Weißbach        $cssData = $media_files[$mediatype];
138*ef36714bSGerry Weißbach
139*ef36714bSGerry Weißbach        // Print the styles.
140*ef36714bSGerry Weißbach    	print NL;
141*ef36714bSGerry Weißbach    	if ( $cssData['encapsulate'] === true ) print $cssData['encapsulationPrefix'] . ' {';
142*ef36714bSGerry Weißbach    	print '/* START '.$cssData['mediatype'].' styles */'.NL;
143*ef36714bSGerry Weißbach
1446c47a78cSAnika Henke        // load files
145*ef36714bSGerry Weißbach        foreach($cssData['files'] as $file => $location){
14672a66eb7SAndreas Gohr            $display = str_replace(fullpath(DOKU_INC), '', fullpath($file));
147*ef36714bSGerry Weißbach            print "\n/* XXXXXXXXX $display XXXXXXXXX */\n";
148*ef36714bSGerry Weißbach            print css_loadfile($file, $location);
1496c47a78cSAnika Henke        }
150*ef36714bSGerry Weißbach
151*ef36714bSGerry Weißbach    	print NL;
152*ef36714bSGerry Weißbach    	if ( $cssData['encapsulate'] === true ) print '} /* /@media ';
153*ef36714bSGerry Weißbach    	else print '/*';
154*ef36714bSGerry Weißbach    	print ' END '.$cssData['mediatype'].' styles */'.NL;
1556c47a78cSAnika Henke    }
156*ef36714bSGerry Weißbach
15778a6aeb1SAndreas Gohr    // end output buffering and get contents
15878a6aeb1SAndreas Gohr    $css = ob_get_contents();
15978a6aeb1SAndreas Gohr    ob_end_clean();
16078a6aeb1SAndreas Gohr
161f8fb2d18SAndreas Gohr    // strip any source maps
162f8fb2d18SAndreas Gohr    stripsourcemaps($css);
163f8fb2d18SAndreas Gohr
1646e69c1baSAndreas Gohr    // apply style replacements
165afb2c082SAndreas Gohr    $css = css_applystyle($css, $styleini['replacements']);
1666e69c1baSAndreas Gohr
16772a66eb7SAndreas Gohr    // parse less
16872a66eb7SAndreas Gohr    $css = css_parseless($css);
169d4a1ece8SAndreas Gohr
17078a6aeb1SAndreas Gohr    // compress whitespace and comments
17178a6aeb1SAndreas Gohr    if($conf['compress']){
17278a6aeb1SAndreas Gohr        $css = css_compress($css);
17378a6aeb1SAndreas Gohr    }
17478a6aeb1SAndreas Gohr
175809d3ba5SAndreas Gohr    // embed small images right into the stylesheet
176809d3ba5SAndreas Gohr    if($conf['cssdatauri']){
177809d3ba5SAndreas Gohr        $base = preg_quote(DOKU_BASE,'#');
178809d3ba5SAndreas Gohr        $css = preg_replace_callback('#(url\([ \'"]*)('.$base.')(.*?(?:\.(png|gif)))#i','css_datauri',$css);
179809d3ba5SAndreas Gohr    }
180809d3ba5SAndreas Gohr
1816619f42eSAdrian Lang    http_cached_finish($cache->cache, $css);
18278a6aeb1SAndreas Gohr}
18378a6aeb1SAndreas Gohr
18478a6aeb1SAndreas Gohr/**
18572a66eb7SAndreas Gohr * Uses phpless to parse LESS in our CSS
18672a66eb7SAndreas Gohr *
18772a66eb7SAndreas Gohr * most of this function is error handling to show a nice useful error when
18872a66eb7SAndreas Gohr * LESS compilation fails
18972a66eb7SAndreas Gohr *
190253d4b48SGerrit Uitslag * @param string $css
19172a66eb7SAndreas Gohr * @return string
19272a66eb7SAndreas Gohr */
19372a66eb7SAndreas Gohrfunction css_parseless($css) {
1947d247a3cSGerrit Uitslag    global $conf;
1957d247a3cSGerrit Uitslag
19672a66eb7SAndreas Gohr    $less = new lessc();
197343a31d8SAndreas Gohr    $less->importDir = array(DOKU_INC);
1987d247a3cSGerrit Uitslag    $less->setPreserveComments(!$conf['compress']);
19930f686ebSChristopher Smith
20030f686ebSChristopher Smith    if (defined('DOKU_UNITTEST')){
20130f686ebSChristopher Smith        $less->importDir[] = TMP_DIR;
20230f686ebSChristopher Smith    }
20330f686ebSChristopher Smith
20472a66eb7SAndreas Gohr    try {
20572a66eb7SAndreas Gohr        return $less->compile($css);
20672a66eb7SAndreas Gohr    } catch(Exception $e) {
20772a66eb7SAndreas Gohr        // get exception message
20872a66eb7SAndreas Gohr        $msg = str_replace(array("\n", "\r", "'"), array(), $e->getMessage());
20972a66eb7SAndreas Gohr
21072a66eb7SAndreas Gohr        // try to use line number to find affected file
21172a66eb7SAndreas Gohr        if(preg_match('/line: (\d+)$/', $msg, $m)){
21272a66eb7SAndreas Gohr            $msg = substr($msg, 0, -1* strlen($m[0])); //remove useless linenumber
21372a66eb7SAndreas Gohr            $lno = $m[1];
21472a66eb7SAndreas Gohr
21572a66eb7SAndreas Gohr            // walk upwards to last include
21672a66eb7SAndreas Gohr            $lines = explode("\n", $css);
21772a66eb7SAndreas Gohr            for($i=$lno-1; $i>=0; $i--){
21872a66eb7SAndreas Gohr                if(preg_match('/\/(\* XXXXXXXXX )(.*?)( XXXXXXXXX \*)\//', $lines[$i], $m)){
21972a66eb7SAndreas Gohr                    // we found it, add info to message
22072a66eb7SAndreas Gohr                    $msg .= ' in '.$m[2].' at line '.($lno-$i);
22172a66eb7SAndreas Gohr                    break;
22272a66eb7SAndreas Gohr                }
22372a66eb7SAndreas Gohr            }
22472a66eb7SAndreas Gohr        }
22572a66eb7SAndreas Gohr
22672a66eb7SAndreas Gohr        // something went wrong
22772a66eb7SAndreas Gohr        $error = 'A fatal error occured during compilation of the CSS files. '.
22872a66eb7SAndreas Gohr            'If you recently installed a new plugin or template it '.
22972a66eb7SAndreas Gohr            'might be broken and you should try disabling it again. ['.$msg.']';
23072a66eb7SAndreas Gohr
23172a66eb7SAndreas Gohr        echo ".dokuwiki:before {
23272a66eb7SAndreas Gohr            content: '$error';
23372a66eb7SAndreas Gohr            background-color: red;
23472a66eb7SAndreas Gohr            display: block;
23572a66eb7SAndreas Gohr            background-color: #fcc;
23672a66eb7SAndreas Gohr            border-color: #ebb;
23772a66eb7SAndreas Gohr            color: #000;
23872a66eb7SAndreas Gohr            padding: 0.5em;
23972a66eb7SAndreas Gohr        }";
24072a66eb7SAndreas Gohr
24172a66eb7SAndreas Gohr        exit;
24272a66eb7SAndreas Gohr    }
24372a66eb7SAndreas Gohr}
24472a66eb7SAndreas Gohr
24572a66eb7SAndreas Gohr/**
2466e69c1baSAndreas Gohr * Does placeholder replacements in the style according to
2476e69c1baSAndreas Gohr * the ones defined in a templates style.ini file
2486e69c1baSAndreas Gohr *
249d4a1ece8SAndreas Gohr * This also adds the ini defined placeholders as less variables
250d4a1ece8SAndreas Gohr * (sans the surrounding __ and with a ini_ prefix)
251d4a1ece8SAndreas Gohr *
2526e69c1baSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
253253d4b48SGerrit Uitslag *
254253d4b48SGerrit Uitslag * @param string $css
255253d4b48SGerrit Uitslag * @param array $replacements  array(placeholder => value)
256253d4b48SGerrit Uitslag * @return string
2576e69c1baSAndreas Gohr */
258afb2c082SAndreas Gohrfunction css_applystyle($css, $replacements) {
259cbe37079SAndreas Gohr    // we convert ini replacements to LESS variable names
260cbe37079SAndreas Gohr    // and build a list of variable: value; pairs
261d4a1ece8SAndreas Gohr    $less = '';
262afb2c082SAndreas Gohr    foreach((array) $replacements as $key => $value) {
263cbe37079SAndreas Gohr        $lkey = trim($key, '_');
264cbe37079SAndreas Gohr        $lkey = '@ini_'.$lkey;
265cbe37079SAndreas Gohr        $less .= "$lkey: $value;\n";
266cbe37079SAndreas Gohr
267afb2c082SAndreas Gohr        $replacements[$key] = $lkey;
268d4a1ece8SAndreas Gohr    }
269c51b334eSAndreas Gohr
270cbe37079SAndreas Gohr    // we now replace all old ini replacements with LESS variables
271afb2c082SAndreas Gohr    $css = strtr($css, $replacements);
272cbe37079SAndreas Gohr
273cbe37079SAndreas Gohr    // now prepend the list of LESS variables as the very first thing
274c51b334eSAndreas Gohr    $css = $less.$css;
2756e69c1baSAndreas Gohr    return $css;
2766e69c1baSAndreas Gohr}
2776e69c1baSAndreas Gohr
2786e69c1baSAndreas Gohr/**
279afb2c082SAndreas Gohr * Load style ini contents
2800ac69508SAnika Henke *
281afb2c082SAndreas Gohr * Loads and merges style.ini files from template and config and prepares
282afb2c082SAndreas Gohr * the stylesheet modes
283afb2c082SAndreas Gohr *
284afb2c082SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
285253d4b48SGerrit Uitslag *
286afb2c082SAndreas Gohr * @param string $tpl the used template
2874d6524b8SAndreas Gohr * @param bool   $preview load preview replacements
288afb2c082SAndreas Gohr * @return array with keys 'stylesheets' and 'replacements'
2890e6f9f08SAnika Henke */
29082edff4dSGerry Weißbachfunction css_styleini($tpl, $flavour=DEFAULT_FLAVOUR, $preview=false) {
2914d6524b8SAndreas Gohr    global $conf;
2924d6524b8SAndreas Gohr
293afb2c082SAndreas Gohr    $stylesheets = array(); // mode, file => base
294afb2c082SAndreas Gohr    $replacements = array(); // placeholder => value
2950ac69508SAnika Henke
29682edff4dSGerry Weißbach    // load template style.ini replacements first
29782edff4dSGerry Weißbach    // they should be the base for the colors - if not the default flavour
298afb2c082SAndreas Gohr    $incbase = tpl_incdir($tpl);
299afb2c082SAndreas Gohr    $webbase = tpl_basedir($tpl);
30082edff4dSGerry Weißbach    $ini = $incbase.DEFAULT_FLAVOUR.'.ini';
30182edff4dSGerry Weißbach    if($flavour != DEFAULT_FLAVOUR && file_exists($ini)) {
30282edff4dSGerry Weißbach        $data = parse_ini_file($ini, true);
30382edff4dSGerry Weißbach        // replacements
30482edff4dSGerry Weißbach        if(is_array($data['replacements'])) {
30582edff4dSGerry Weißbach            $replacements = array_merge($replacements, css_fixreplacementurls($data['replacements'], $webbase));
30682edff4dSGerry Weißbach        }
30782edff4dSGerry Weißbach    }
30882edff4dSGerry Weißbach
30982edff4dSGerry Weißbach    // load template's style.ini
31082edff4dSGerry Weißbach    $ini = $incbase.$flavour.'.ini';
311afb2c082SAndreas Gohr    if(file_exists($ini)){
312afb2c082SAndreas Gohr        $data = parse_ini_file($ini, true);
3130ac69508SAnika Henke
314afb2c082SAndreas Gohr        // stylesheets
315afb2c082SAndreas Gohr        if(is_array($data['stylesheets'])) foreach($data['stylesheets'] as $file => $mode){
316afb2c082SAndreas Gohr            $stylesheets[$mode][$incbase.$file] = $webbase;
317afb2c082SAndreas Gohr        }
318afb2c082SAndreas Gohr        // replacements
31982edff4dSGerry Weißbach        if(isset($data['replacements']) && is_array($data['replacements'])){
32047f862d1SChristopher Smith            $replacements = array_merge($replacements, css_fixreplacementurls($data['replacements'],$webbase));
3210ac69508SAnika Henke        }
3220ac69508SAnika Henke    }
323afb2c082SAndreas Gohr
324afb2c082SAndreas Gohr    // load configs's style.ini
325afb2c082SAndreas Gohr    $webbase = DOKU_BASE;
3268c5aad7bSAnika Henke    $ini = DOKU_CONF."tpl/$tpl/style.ini";
3278c5aad7bSAnika Henke    $incbase = dirname($ini).'/';
328afb2c082SAndreas Gohr    if(file_exists($ini)){
329afb2c082SAndreas Gohr        $data = parse_ini_file($ini, true);
330afb2c082SAndreas Gohr
33182edff4dSGerry Weißbach        // stylesheets - but only if we are on the default flavour
33282edff4dSGerry Weißbach        if($flavour === DEFAULT_FLAVOUR && isset($data['stylesheets']) && is_array($data['stylesheets'])) foreach($data['stylesheets'] as $file => $mode){
333afb2c082SAndreas Gohr            $stylesheets[$mode][$incbase.$file] = $webbase;
3340ac69508SAnika Henke        }
335afb2c082SAndreas Gohr        // replacements
33606c9ee33SMarius van Witzenburg        if(isset($data['replacements']) && is_array($data['replacements'])){
33747f862d1SChristopher Smith            $replacements = array_merge($replacements, css_fixreplacementurls($data['replacements'],$webbase));
338afb2c082SAndreas Gohr        }
339afb2c082SAndreas Gohr    }
340afb2c082SAndreas Gohr
3414d6524b8SAndreas Gohr    // allow replacement overwrites in preview mode
3424d6524b8SAndreas Gohr    if($preview) {
3434d6524b8SAndreas Gohr        $webbase = DOKU_BASE;
3444d6524b8SAndreas Gohr        $ini     = $conf['cachedir'].'/preview.ini';
3454d6524b8SAndreas Gohr        if(file_exists($ini)) {
3464d6524b8SAndreas Gohr            $data = parse_ini_file($ini, true);
3474d6524b8SAndreas Gohr            // replacements
3484d6524b8SAndreas Gohr            if(is_array($data['replacements'])) {
3494d6524b8SAndreas Gohr                $replacements = array_merge($replacements, css_fixreplacementurls($data['replacements'], $webbase));
3504d6524b8SAndreas Gohr            }
3514d6524b8SAndreas Gohr        }
3524d6524b8SAndreas Gohr    }
3534d6524b8SAndreas Gohr
354afb2c082SAndreas Gohr    return array(
355afb2c082SAndreas Gohr        'stylesheets' => $stylesheets,
356afb2c082SAndreas Gohr        'replacements' => $replacements
357afb2c082SAndreas Gohr    );
3580e6f9f08SAnika Henke}
3590e6f9f08SAnika Henke
3601e2c5948SChristopher Smith/**
3611e2c5948SChristopher Smith * Amend paths used in replacement relative urls, refer FS#2879
3621e2c5948SChristopher Smith *
3631e2c5948SChristopher Smith * @author Chris Smith <chris@jalakai.co.uk>
364253d4b48SGerrit Uitslag *
365253d4b48SGerrit Uitslag * @param array $replacements with key-value pairs
366253d4b48SGerrit Uitslag * @param string $location
367253d4b48SGerrit Uitslag * @return array
3681e2c5948SChristopher Smith */
36947f862d1SChristopher Smithfunction css_fixreplacementurls($replacements, $location) {
37047f862d1SChristopher Smith    foreach($replacements as $key => $value) {
37147f862d1SChristopher Smith        $replacements[$key] = preg_replace('#(url\([ \'"]*)(?!/|data:|http://|https://| |\'|")#','\\1'.$location,$value);
37247f862d1SChristopher Smith    }
37347f862d1SChristopher Smith    return $replacements;
37447f862d1SChristopher Smith}
37547f862d1SChristopher Smith
3760e6f9f08SAnika Henke/**
377*ef36714bSGerry Weißbach * Wrapper for the files, content and mediatype for the event CSS_STYLES_INCLUDED
378*ef36714bSGerry Weißbach *
379*ef36714bSGerry Weißbach * @author Gerry Weißbach <gerry.w@gammaproduction.de>
380*ef36714bSGerry Weißbach *
381*ef36714bSGerry Weißbach * @param string $mediatype type ofthe current media files/content set
382*ef36714bSGerry Weißbach * @param array $files set of files that define the current mediatype
383*ef36714bSGerry Weißbach * @return array
384*ef36714bSGerry Weißbach */
385*ef36714bSGerry Weißbachfunction css_filewrapper($mediatype, $flavour=DEFAULT_FLAVOUR, $files=array()){
386*ef36714bSGerry Weißbach    return array(
387*ef36714bSGerry Weißbach            'files'                 => $files,
388*ef36714bSGerry Weißbach	        'mediatype'             => $mediatype,
389*ef36714bSGerry Weißbach	        'flavour'               => $flavour,
390*ef36714bSGerry Weißbach	        'encapsulate'           => in_array($mediatype, array('screen', 'print', 'handheld')),
391*ef36714bSGerry Weißbach	        'encapsulationPrefix'   => '@media '.$mediatype
392*ef36714bSGerry Weißbach        );
393*ef36714bSGerry Weißbach}
394*ef36714bSGerry Weißbach
395*ef36714bSGerry Weißbach/**
396*ef36714bSGerry Weißbach * Prints the @media encapsulated default styles of DokuWiki
397*ef36714bSGerry Weißbach *
398*ef36714bSGerry Weißbach * @author Gerry Weißbach <gerry.w@gammaproduction.de>
399*ef36714bSGerry Weißbach *
400*ef36714bSGerry Weißbach * This function is being called by a CSS_STYLES_INCLUDED event
401*ef36714bSGerry Weißbach * The event can be distinguished by the mediatype which is:
402*ef36714bSGerry Weißbach *   DW_DEFAULT
403*ef36714bSGerry Weißbach */
404*ef36714bSGerry Weißbachfunction css_defaultstyles(){
405*ef36714bSGerry Weißbach	// print the default classes for interwiki links and file downloads
406*ef36714bSGerry Weißbach    print '@media screen {';
407*ef36714bSGerry Weißbach    css_interwiki();
408*ef36714bSGerry Weißbach    css_filetypes();
409*ef36714bSGerry Weißbach    print '}';
410*ef36714bSGerry Weißbach}
411*ef36714bSGerry Weißbach
412*ef36714bSGerry Weißbach/**
4131c2d1019SAndreas Gohr * Prints classes for interwikilinks
4141c2d1019SAndreas Gohr *
4151c2d1019SAndreas Gohr * Interwiki links have two classes: 'interwiki' and 'iw_$name>' where
4161c2d1019SAndreas Gohr * $name is the identifier given in the config. All Interwiki links get
4171c2d1019SAndreas Gohr * an default style with a default icon. If a special icon is available
4181c2d1019SAndreas Gohr * for an interwiki URL it is set in it's own class. Both classes can be
4191c2d1019SAndreas Gohr * overwritten in the template or userstyles.
4201c2d1019SAndreas Gohr *
4211c2d1019SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
4221c2d1019SAndreas Gohr */
4231c2d1019SAndreas Gohrfunction css_interwiki(){
4241c2d1019SAndreas Gohr
4251c2d1019SAndreas Gohr    // default style
4261c2d1019SAndreas Gohr    echo 'a.interwiki {';
4271c2d1019SAndreas Gohr    echo ' background: transparent url('.DOKU_BASE.'lib/images/interwiki.png) 0px 1px no-repeat;';
4287b4ea081Smarklundeberg    echo ' padding: 1px 0px 1px 16px;';
4291c2d1019SAndreas Gohr    echo '}';
4301c2d1019SAndreas Gohr
4311c2d1019SAndreas Gohr    // additional styles when icon available
4321c2d1019SAndreas Gohr    $iwlinks = getInterwiki();
4331c2d1019SAndreas Gohr    foreach(array_keys($iwlinks) as $iw){
4349d2ddea4SAndreas Gohr        $class = preg_replace('/[^_\-a-z0-9]+/i','_',$iw);
43579e79377SAndreas Gohr        if(file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.png')){
4369d2ddea4SAndreas Gohr            echo "a.iw_$class {";
4371c2d1019SAndreas Gohr            echo '  background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.png)';
4381c2d1019SAndreas Gohr            echo '}';
43979e79377SAndreas Gohr        }elseif(file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.gif')){
4409d2ddea4SAndreas Gohr            echo "a.iw_$class {";
4411c2d1019SAndreas Gohr            echo '  background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.gif)';
4421c2d1019SAndreas Gohr            echo '}';
4431c2d1019SAndreas Gohr        }
4441c2d1019SAndreas Gohr    }
445d15166e5SAndreas Gohr}
4461c2d1019SAndreas Gohr
447d15166e5SAndreas Gohr/**
448d15166e5SAndreas Gohr * Prints classes for file download links
449d15166e5SAndreas Gohr *
450d15166e5SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
451d15166e5SAndreas Gohr */
452d15166e5SAndreas Gohrfunction css_filetypes(){
453d15166e5SAndreas Gohr
454d15166e5SAndreas Gohr    // default style
455035e07f1SKate Arzamastseva    echo '.mediafile {';
456d15166e5SAndreas Gohr    echo ' background: transparent url('.DOKU_BASE.'lib/images/fileicons/file.png) 0px 1px no-repeat;';
4575b77caf4SAndreas Gohr    echo ' padding-left: 18px;';
4585b77caf4SAndreas Gohr    echo ' padding-bottom: 1px;';
459d15166e5SAndreas Gohr    echo '}';
460d15166e5SAndreas Gohr
461d15166e5SAndreas Gohr    // additional styles when icon available
46227bf7924STom N Harris    // scan directory for all icons
46327bf7924STom N Harris    $exts = array();
46427bf7924STom N Harris    if($dh = opendir(DOKU_INC.'lib/images/fileicons')){
46527bf7924STom N Harris        while(false !== ($file = readdir($dh))){
46627bf7924STom N Harris            if(preg_match('/([_\-a-z0-9]+(?:\.[_\-a-z0-9]+)*?)\.(png|gif)/i',$file,$match)){
46727bf7924STom N Harris                $ext = strtolower($match[1]);
46827bf7924STom N Harris                $type = '.'.strtolower($match[2]);
46927bf7924STom N Harris                if($ext!='file' && (!isset($exts[$ext]) || $type=='.png')){
47027bf7924STom N Harris                    $exts[$ext] = $type;
471d15166e5SAndreas Gohr                }
472d15166e5SAndreas Gohr            }
4731c2d1019SAndreas Gohr        }
47427bf7924STom N Harris        closedir($dh);
47527bf7924STom N Harris    }
47627bf7924STom N Harris    foreach($exts as $ext=>$type){
47727bf7924STom N Harris        $class = preg_replace('/[^_\-a-z0-9]+/','_',$ext);
478035e07f1SKate Arzamastseva        echo ".mf_$class {";
47927bf7924STom N Harris        echo '  background-image: url('.DOKU_BASE.'lib/images/fileicons/'.$ext.$type.')';
48027bf7924STom N Harris        echo '}';
48127bf7924STom N Harris    }
48227bf7924STom N Harris}
4831c2d1019SAndreas Gohr
4841c2d1019SAndreas Gohr/**
48578a6aeb1SAndreas Gohr * Loads a given file and fixes relative URLs with the
48678a6aeb1SAndreas Gohr * given location prefix
487253d4b48SGerrit Uitslag *
488253d4b48SGerrit Uitslag * @param string $file file system path
489253d4b48SGerrit Uitslag * @param string $location
490253d4b48SGerrit Uitslag * @return string
49178a6aeb1SAndreas Gohr */
49278a6aeb1SAndreas Gohrfunction css_loadfile($file,$location=''){
49312ffbbc3SChristopher Smith    $css_file = new DokuCssFile($file);
49412ffbbc3SChristopher Smith    return $css_file->load($location);
49512ffbbc3SChristopher Smith}
49612ffbbc3SChristopher Smith
4971e2c5948SChristopher Smith/**
4981e2c5948SChristopher Smith *  Helper class to abstract loading of css/less files
4991e2c5948SChristopher Smith *
5001e2c5948SChristopher Smith *  @author Chris Smith <chris@jalakai.co.uk>
5011e2c5948SChristopher Smith */
50212ffbbc3SChristopher Smithclass DokuCssFile {
50312ffbbc3SChristopher Smith
5041e2c5948SChristopher Smith    protected $filepath;             // file system path to the CSS/Less file
5051e2c5948SChristopher Smith    protected $location;             // base url location of the CSS/Less file
506fd18b5f4SGerrit Uitslag    protected $relative_path = null;
50712ffbbc3SChristopher Smith
50812ffbbc3SChristopher Smith    public function __construct($file) {
50912ffbbc3SChristopher Smith        $this->filepath = $file;
51012ffbbc3SChristopher Smith    }
51112ffbbc3SChristopher Smith
5121e2c5948SChristopher Smith    /**
5131e2c5948SChristopher Smith     * Load the contents of the css/less file and adjust any relative paths/urls (relative to this file) to be
5141e2c5948SChristopher Smith     * relative to the dokuwiki root: the web root (DOKU_BASE) for most files; the file system root (DOKU_INC)
5151e2c5948SChristopher Smith     * for less files.
5161e2c5948SChristopher Smith     *
5171e2c5948SChristopher Smith     * @param   string   $location   base url for this file
5181e2c5948SChristopher Smith     * @return  string               the CSS/Less contents of the file
5191e2c5948SChristopher Smith     */
52012ffbbc3SChristopher Smith    public function load($location='') {
52179e79377SAndreas Gohr        if (!file_exists($this->filepath)) return '';
52212ffbbc3SChristopher Smith
52312ffbbc3SChristopher Smith        $css = io_readFile($this->filepath);
52478a6aeb1SAndreas Gohr        if (!$location) return $css;
52578a6aeb1SAndreas Gohr
52612ffbbc3SChristopher Smith        $this->location = $location;
527de737055SChristopher Smith
52812ffbbc3SChristopher Smith        $css = preg_replace_callback('#(url\( *)([\'"]?)(.*?)(\2)( *\))#',array($this,'replacements'),$css);
52912ffbbc3SChristopher Smith        $css = preg_replace_callback('#(@import\s+)([\'"])(.*?)(\2)#',array($this,'replacements'),$css);
530809d3ba5SAndreas Gohr
53178a6aeb1SAndreas Gohr        return $css;
53278a6aeb1SAndreas Gohr    }
53378a6aeb1SAndreas Gohr
5341e2c5948SChristopher Smith    /**
5351e2c5948SChristopher Smith     * Get the relative file system path of this file, relative to dokuwiki's root folder, DOKU_INC
5361e2c5948SChristopher Smith     *
5371e2c5948SChristopher Smith     * @return string   relative file system path
5381e2c5948SChristopher Smith     */
539fd18b5f4SGerrit Uitslag    protected function getRelativePath(){
54012ffbbc3SChristopher Smith
54112ffbbc3SChristopher Smith        if (is_null($this->relative_path)) {
54212ffbbc3SChristopher Smith            $basedir = array(DOKU_INC);
5431e2c5948SChristopher Smith
5441e2c5948SChristopher Smith            // during testing, files may be found relative to a second base dir, TMP_DIR
54512ffbbc3SChristopher Smith            if (defined('DOKU_UNITTEST')) {
54612ffbbc3SChristopher Smith                $basedir[] = realpath(TMP_DIR);
54712ffbbc3SChristopher Smith            }
54812ffbbc3SChristopher Smith
54973f25ac0SAndreas Gohr            $basedir = array_map('preg_quote_cb', $basedir);
55073f25ac0SAndreas Gohr            $regex = '/^('.join('|',$basedir).')/';
55112ffbbc3SChristopher Smith            $this->relative_path = preg_replace($regex, '', dirname($this->filepath));
55212ffbbc3SChristopher Smith        }
55312ffbbc3SChristopher Smith
55412ffbbc3SChristopher Smith        return $this->relative_path;
55512ffbbc3SChristopher Smith    }
55612ffbbc3SChristopher Smith
5571e2c5948SChristopher Smith    /**
5581e2c5948SChristopher Smith     * preg_replace callback to adjust relative urls from relative to this file to relative
5591e2c5948SChristopher Smith     * to the appropriate dokuwiki root location as described in the code
5601e2c5948SChristopher Smith     *
5611e2c5948SChristopher Smith     * @param  array    see http://php.net/preg_replace_callback
5621e2c5948SChristopher Smith     * @return string   see http://php.net/preg_replace_callback
5631e2c5948SChristopher Smith     */
56412ffbbc3SChristopher Smith    public function replacements($match) {
565de737055SChristopher Smith
5661e2c5948SChristopher Smith        // not a relative url? - no adjustment required
567de737055SChristopher Smith        if (preg_match('#^(/|data:|https?://)#',$match[3])) {
568de737055SChristopher Smith            return $match[0];
569de737055SChristopher Smith        }
5701e2c5948SChristopher Smith        // a less file import? - requires a file system location
571de737055SChristopher Smith        else if (substr($match[3],-5) == '.less') {
572de737055SChristopher Smith            if ($match[3]{0} != '/') {
57312ffbbc3SChristopher Smith                $match[3] = $this->getRelativePath() . '/' . $match[3];
574de737055SChristopher Smith            }
575de737055SChristopher Smith        }
5761e2c5948SChristopher Smith        // everything else requires a url adjustment
577de737055SChristopher Smith        else {
57812ffbbc3SChristopher Smith            $match[3] = $this->location . $match[3];
579de737055SChristopher Smith        }
580de737055SChristopher Smith
581de737055SChristopher Smith        return join('',array_slice($match,1));
582de737055SChristopher Smith    }
58312ffbbc3SChristopher Smith}
584de737055SChristopher Smith
585809d3ba5SAndreas Gohr/**
5864eb5f931SChristopher Smith * Convert local image URLs to data URLs if the filesize is small
587809d3ba5SAndreas Gohr *
588809d3ba5SAndreas Gohr * Callback for preg_replace_callback
589253d4b48SGerrit Uitslag *
590253d4b48SGerrit Uitslag * @param array $match
591253d4b48SGerrit Uitslag * @return string
592809d3ba5SAndreas Gohr */
593809d3ba5SAndreas Gohrfunction css_datauri($match){
59428f4004cSAndreas Gohr    global $conf;
59528f4004cSAndreas Gohr
596809d3ba5SAndreas Gohr    $pre   = unslash($match[1]);
597809d3ba5SAndreas Gohr    $base  = unslash($match[2]);
598809d3ba5SAndreas Gohr    $url   = unslash($match[3]);
599809d3ba5SAndreas Gohr    $ext   = unslash($match[4]);
600809d3ba5SAndreas Gohr
601809d3ba5SAndreas Gohr    $local = DOKU_INC.$url;
602809d3ba5SAndreas Gohr    $size  = @filesize($local);
60328f4004cSAndreas Gohr    if($size && $size < $conf['cssdatauri']){
604809d3ba5SAndreas Gohr        $data = base64_encode(file_get_contents($local));
605809d3ba5SAndreas Gohr    }
606809d3ba5SAndreas Gohr    if($data){
6076a5d6817SAnika Henke        $url = 'data:image/'.$ext.';base64,'.$data;
608809d3ba5SAndreas Gohr    }else{
609809d3ba5SAndreas Gohr        $url = $base.$url;
610809d3ba5SAndreas Gohr    }
611809d3ba5SAndreas Gohr    return $pre.$url;
612809d3ba5SAndreas Gohr}
613809d3ba5SAndreas Gohr
61415c394afSAndreas Gohr
61578a6aeb1SAndreas Gohr/**
61678a6aeb1SAndreas Gohr * Returns a list of possible Plugin Styles (no existance check here)
61778a6aeb1SAndreas Gohr *
61878a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
619253d4b48SGerrit Uitslag *
620253d4b48SGerrit Uitslag * @param string $mediatype
621253d4b48SGerrit Uitslag * @return array
62278a6aeb1SAndreas Gohr */
623318cd03eSAnika Henkefunction css_pluginstyles($mediatype='screen'){
62478a6aeb1SAndreas Gohr    $list = array();
62578a6aeb1SAndreas Gohr    $plugins = plugin_list();
62678a6aeb1SAndreas Gohr    foreach ($plugins as $p){
627318cd03eSAnika Henke        $list[DOKU_PLUGIN."$p/$mediatype.css"]  = DOKU_BASE."lib/plugins/$p/";
628d4a1ece8SAndreas Gohr        $list[DOKU_PLUGIN."$p/$mediatype.less"]  = DOKU_BASE."lib/plugins/$p/";
629318cd03eSAnika Henke        // alternative for screen.css
630318cd03eSAnika Henke        if ($mediatype=='screen') {
63178a6aeb1SAndreas Gohr            $list[DOKU_PLUGIN."$p/style.css"]  = DOKU_BASE."lib/plugins/$p/";
632d4a1ece8SAndreas Gohr            $list[DOKU_PLUGIN."$p/style.less"]  = DOKU_BASE."lib/plugins/$p/";
63378a6aeb1SAndreas Gohr        }
63478a6aeb1SAndreas Gohr    }
63578a6aeb1SAndreas Gohr    return $list;
63678a6aeb1SAndreas Gohr}
63778a6aeb1SAndreas Gohr
63878a6aeb1SAndreas Gohr/**
63978a6aeb1SAndreas Gohr * Very simple CSS optimizer
64078a6aeb1SAndreas Gohr *
64178a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
642253d4b48SGerrit Uitslag *
643253d4b48SGerrit Uitslag * @param string $css
644253d4b48SGerrit Uitslag * @return string
64578a6aeb1SAndreas Gohr */
64678a6aeb1SAndreas Gohrfunction css_compress($css){
647fd7c2db0SAndreas Gohr    //strip comments through a callback
648fd7c2db0SAndreas Gohr    $css = preg_replace_callback('#(/\*)(.*?)(\*/)#s','css_comment_cb',$css);
649fd7c2db0SAndreas Gohr
650247c1c5dSAndreas Gohr    //strip (incorrect but common) one line comments
651fe5a50c3SAndreas Gohr    $css = preg_replace_callback('/^.*\/\/.*$/m','css_onelinecomment_cb',$css);
652247c1c5dSAndreas Gohr
65378a6aeb1SAndreas Gohr    // strip whitespaces
65478a6aeb1SAndreas Gohr    $css = preg_replace('![\r\n\t ]+!',' ',$css);
655f5379589SChristopher Smith    $css = preg_replace('/ ?([;,{}\/]) ?/','\\1',$css);
656f5379589SChristopher Smith    $css = preg_replace('/ ?: /',':',$css);
65778a6aeb1SAndreas Gohr
658205907a7Sfurun    // number compression
659205907a7Sfurun    $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"
660205907a7Sfurun    $css = preg_replace('/([: ])\.(0)+((?:pt|pc|in|mm|cm|em|ex|px)\b|%)(?=[^\{]*[;\}])/', '$1$2', $css); // ".0em" to "0"
661205907a7Sfurun    $css = preg_replace('/([: ]0)0*(\.0*)?((?:pt|pc|in|mm|cm|em|ex|px)(?=[^\{]*[;\}])\b|%)/', '$1', $css); // "0.0em" to "0"
662205907a7Sfurun    $css = preg_replace('/([: ]\d+)(\.0*)((?:pt|pc|in|mm|cm|em|ex|px)(?=[^\{]*[;\}])\b|%)/', '$1$3', $css); // "1.0em" to "1em"
663205907a7Sfurun    $css = preg_replace('/([: ])0+(\d+|\d*\.\d+)((?:pt|pc|in|mm|cm|em|ex|px)(?=[^\{]*[;\}])\b|%)/', '$1$2$3', $css); // "001em" to "1em"
664205907a7Sfurun
665205907a7Sfurun    // shorten attributes (1em 1em 1em 1em -> 1em)
666205907a7Sfurun    $css = preg_replace('/(?<![\w\-])((?:margin|padding|border|border-(?:width|radius)):)([\w\.]+)( \2)+(?=[;\}]| !)/', '$1$2', $css); // "1em 1em 1em 1em" to "1em"
667205907a7Sfurun    $css = preg_replace('/(?<![\w\-])((?:margin|padding|border|border-(?:width)):)([\w\.]+) ([\w\.]+) \2 \3(?=[;\}]| !)/', '$1$2 $3', $css); // "1em 2em 1em 2em" to "1em 2em"
668205907a7Sfurun
66978a6aeb1SAndreas Gohr    // shorten colors
670205907a7Sfurun    $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);
67178a6aeb1SAndreas Gohr
67278a6aeb1SAndreas Gohr    return $css;
67378a6aeb1SAndreas Gohr}
67478a6aeb1SAndreas Gohr
675c00aef76SAndreas Gohr/**
676c00aef76SAndreas Gohr * Callback for css_compress()
677c00aef76SAndreas Gohr *
678c00aef76SAndreas Gohr * Keeps short comments (< 5 chars) to maintain typical browser hacks
679c00aef76SAndreas Gohr *
680c00aef76SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
681253d4b48SGerrit Uitslag *
682253d4b48SGerrit Uitslag * @param array $matches
683253d4b48SGerrit Uitslag * @return string
684c00aef76SAndreas Gohr */
685c00aef76SAndreas Gohrfunction css_comment_cb($matches){
686c00aef76SAndreas Gohr    if(strlen($matches[2]) > 4) return '';
687c00aef76SAndreas Gohr    return $matches[0];
688c00aef76SAndreas Gohr}
68978a6aeb1SAndreas Gohr
690fe5a50c3SAndreas Gohr/**
691fe5a50c3SAndreas Gohr * Callback for css_compress()
692fe5a50c3SAndreas Gohr *
693fe5a50c3SAndreas Gohr * Strips one line comments but makes sure it will not destroy url() constructs with slashes
694fe5a50c3SAndreas Gohr *
695253d4b48SGerrit Uitslag * @param array $matches
696fe5a50c3SAndreas Gohr * @return string
697fe5a50c3SAndreas Gohr */
698fe5a50c3SAndreas Gohrfunction css_onelinecomment_cb($matches) {
699fe5a50c3SAndreas Gohr    $line = $matches[0];
700fe5a50c3SAndreas Gohr
701fe5a50c3SAndreas Gohr    $i = 0;
702fe5a50c3SAndreas Gohr    $len = strlen($line);
703918a4468SAndreas Gohr
704fe5a50c3SAndreas Gohr    while ($i< $len){
705fe5a50c3SAndreas Gohr        $nextcom = strpos($line, '//', $i);
706fe5a50c3SAndreas Gohr        $nexturl = stripos($line, 'url(', $i);
707fe5a50c3SAndreas Gohr
708fe5a50c3SAndreas Gohr        if($nextcom === false) {
709fe5a50c3SAndreas Gohr            // no more comments, we're done
710918a4468SAndreas Gohr            $i = $len;
711fe5a50c3SAndreas Gohr            break;
712fe5a50c3SAndreas Gohr        }
713fe5a50c3SAndreas Gohr
714918a4468SAndreas Gohr        // keep any quoted string that starts before a comment
715918a4468SAndreas Gohr        $nextsqt = strpos($line, "'", $i);
716918a4468SAndreas Gohr        $nextdqt = strpos($line, '"', $i);
717918a4468SAndreas Gohr        if(min($nextsqt, $nextdqt) < $nextcom) {
718918a4468SAndreas Gohr            $skipto = false;
719918a4468SAndreas Gohr            if($nextsqt !== false && ($nextdqt === false || $nextsqt < $nextdqt)) {
720918a4468SAndreas Gohr                $skipto = strpos($line, "'", $nextsqt+1) +1;
721918a4468SAndreas Gohr            } else if ($nextdqt !== false) {
722918a4468SAndreas Gohr                $skipto = strpos($line, '"', $nextdqt+1) +1;
723918a4468SAndreas Gohr            }
724918a4468SAndreas Gohr
725918a4468SAndreas Gohr            if($skipto !== false) {
726918a4468SAndreas Gohr                $i = $skipto;
727918a4468SAndreas Gohr                continue;
728918a4468SAndreas Gohr            }
729918a4468SAndreas Gohr        }
730918a4468SAndreas Gohr
731918a4468SAndreas Gohr        if($nexturl === false || $nextcom < $nexturl) {
732918a4468SAndreas Gohr            // no url anymore, strip comment and be done
733918a4468SAndreas Gohr            $i = $nextcom;
734918a4468SAndreas Gohr            break;
735918a4468SAndreas Gohr        }
736918a4468SAndreas Gohr
737918a4468SAndreas Gohr        // we have an upcoming url
738918a4468SAndreas Gohr        $i = strpos($line, ')', $nexturl);
739918a4468SAndreas Gohr    }
740918a4468SAndreas Gohr
741918a4468SAndreas Gohr    return substr($line, 0, $i);
742fe5a50c3SAndreas Gohr}
743fe5a50c3SAndreas Gohr
744e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
745