xref: /dokuwiki/lib/exe/css.php (revision 8b3ff80883c84fb23348418f2f4533ed41dd8209)
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");
1378a6aeb1SAndreas Gohrrequire_once(DOKU_INC.'inc/init.php');
1478a6aeb1SAndreas Gohr
1578a6aeb1SAndreas Gohr// Main (don't run when UNIT test)
1678a6aeb1SAndreas Gohrif(!defined('SIMPLE_TEST')){
1778a6aeb1SAndreas Gohr    header('Content-Type: text/css; charset=utf-8');
1878a6aeb1SAndreas Gohr    css_out();
1978a6aeb1SAndreas Gohr}
2078a6aeb1SAndreas Gohr
2178a6aeb1SAndreas Gohr
2278a6aeb1SAndreas Gohr// ---------------------- functions ------------------------------
2378a6aeb1SAndreas Gohr
2478a6aeb1SAndreas Gohr/**
2578a6aeb1SAndreas Gohr * Output all needed Styles
2678a6aeb1SAndreas Gohr *
2778a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
2878a6aeb1SAndreas Gohr */
2978a6aeb1SAndreas Gohrfunction css_out(){
3078a6aeb1SAndreas Gohr    global $conf;
3178a6aeb1SAndreas Gohr    global $lang;
3209edb711SAndreas Gohr    global $config_cascade;
33bfd0f597STom N Harris    global $INPUT;
3409edb711SAndreas Gohr
35de4634ecSGerry Weißbach    if ($INPUT->str('s') == 'feed') {
36de4634ecSGerry Weißbach        $mediatypes = array('feed');
37de4634ecSGerry Weißbach    } else {
38*8b3ff808SGerry Weißbach        $mediatypes = array('screen', 'all', 'print', 'handheld');
39de4634ecSGerry Weißbach    }
40de4634ecSGerry Weißbach
4120a2375aSAndreas Gohr    // decide from where to get the template
4220a2375aSAndreas Gohr    $tpl = trim(preg_replace('/[^\w-]+/','',$INPUT->str('t')));
4320a2375aSAndreas Gohr    if(!$tpl) $tpl = $conf['template'];
4420a2375aSAndreas Gohr
45de4634ecSGerry Weißbach
4620a2375aSAndreas Gohr    // load styl.ini
470bfc8d5aSAnika Henke    $styleini = css_styleini($tpl, $INPUT->bool('preview'));
4820a2375aSAndreas Gohr
49afb2c082SAndreas Gohr    // cache influencers
50259571aaSMichal Koutný    $tplinc = tpl_incdir($tpl);
51afb2c082SAndreas Gohr    $cache_files = getConfigFiles('main');
52afb2c082SAndreas Gohr    $cache_files[] = $tplinc.'style.ini';
53afb2c082SAndreas Gohr    $cache_files[] = DOKU_CONF."tpl/$tpl/style.ini";
54afb2c082SAndreas Gohr    $cache_files[] = __FILE__;
554d6524b8SAndreas Gohr    if($INPUT->bool('preview')) $cache_files[] = $conf['cachedir'].'/preview.ini';
56afb2c082SAndreas Gohr
5778a6aeb1SAndreas Gohr    // Array of needed files and their web locations, the latter ones
5878a6aeb1SAndreas Gohr    // are needed to fix relative paths in the stylesheets
5978a6aeb1SAndreas Gohr    $files = array();
6014977bd2SMichael Hamann    foreach($mediatypes as $mediatype) {
6114977bd2SMichael Hamann        $files[$mediatype] = array();
62318cd03eSAnika Henke        // load core styles
6314977bd2SMichael Hamann        $files[$mediatype][DOKU_INC.'lib/styles/'.$mediatype.'.css'] = DOKU_BASE.'lib/styles/';
64af4684acSAndreas Gohr
6543576758SAndreas Gohr        // load jQuery-UI theme
666c47a78cSAnika Henke        if ($mediatype == 'screen') {
6714977bd2SMichael Hamann            $files[$mediatype][DOKU_INC.'lib/scripts/jquery/jquery-ui-theme/smoothness.css'] = DOKU_BASE.'lib/scripts/jquery/jquery-ui-theme/';
686c47a78cSAnika Henke        }
69318cd03eSAnika Henke        // load plugin styles
7014977bd2SMichael Hamann        $files[$mediatype] = array_merge($files[$mediatype], css_pluginstyles($mediatype));
71318cd03eSAnika Henke        // load template styles
72afb2c082SAndreas Gohr        if (isset($styleini['stylesheets'][$mediatype])) {
73afb2c082SAndreas Gohr            $files[$mediatype] = array_merge($files[$mediatype], $styleini['stylesheets'][$mediatype]);
7409edb711SAndreas Gohr        }
75318cd03eSAnika Henke        // load user styles
767b909d5eSGerrit Uitslag        if(!empty($config_cascade['userstyle'][$mediatype])) {
777b909d5eSGerrit Uitslag            foreach($config_cascade['userstyle'][$mediatype] as $userstyle) {
787b909d5eSGerrit Uitslag                $files[$mediatype][$userstyle] = DOKU_BASE;
797b909d5eSGerrit Uitslag            }
80318cd03eSAnika Henke        }
8178a6aeb1SAndreas Gohr
8214977bd2SMichael Hamann        $cache_files = array_merge($cache_files, array_keys($files[$mediatype]));
8314977bd2SMichael Hamann    }
846619f42eSAdrian Lang
8538f56bffSBen Coburn    // check cache age & handle conditional request
866619f42eSAdrian Lang    // This may exit if a cache can be used
876619f42eSAdrian Lang    http_cached($cache->cache,
886619f42eSAdrian Lang                $cache->useCache(array('files' => $cache_files)));
8978a6aeb1SAndreas Gohr
903899c2ecSMichael Hamann    // start output buffering
913899c2ecSMichael Hamann    ob_start();
923899c2ecSMichael Hamann
936c47a78cSAnika Henke    // build the stylesheet
9414977bd2SMichael Hamann    foreach ($mediatypes as $mediatype) {
9578a6aeb1SAndreas Gohr
96d15166e5SAndreas Gohr        // print the default classes for interwiki links and file downloads
976c47a78cSAnika Henke        if ($mediatype == 'screen') {
98cacfb606SAnika Henke            print '@media screen {';
991c2d1019SAndreas Gohr            css_interwiki();
100d15166e5SAndreas Gohr            css_filetypes();
101cacfb606SAnika Henke            print '}';
10278a6aeb1SAndreas Gohr        }
10378a6aeb1SAndreas Gohr
1046c47a78cSAnika Henke        // load files
1056c47a78cSAnika Henke        $css_content = '';
10614977bd2SMichael Hamann        foreach($files[$mediatype] as $file => $location){
10772a66eb7SAndreas Gohr            $display = str_replace(fullpath(DOKU_INC), '', fullpath($file));
10872a66eb7SAndreas Gohr            $css_content .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n";
1096c47a78cSAnika Henke            $css_content .= css_loadfile($file, $location);
1106c47a78cSAnika Henke        }
1116c47a78cSAnika Henke        switch ($mediatype) {
1126c47a78cSAnika Henke            case 'screen':
1136c47a78cSAnika Henke                print NL.'@media screen { /* START screen styles */'.NL.$css_content.NL.'} /* /@media END screen styles */'.NL;
1146c47a78cSAnika Henke                break;
1156c47a78cSAnika Henke            case 'print':
1166c47a78cSAnika Henke                print NL.'@media print { /* START print styles */'.NL.$css_content.NL.'} /* /@media END print styles */'.NL;
1176c47a78cSAnika Henke                break;
1186c47a78cSAnika Henke            case 'all':
1196c47a78cSAnika Henke            case 'feed':
1206c47a78cSAnika Henke            default:
1216c47a78cSAnika Henke                print NL.'/* START rest styles */ '.NL.$css_content.NL.'/* END rest styles */'.NL;
1226c47a78cSAnika Henke                break;
1236c47a78cSAnika Henke        }
1246c47a78cSAnika Henke    }
12578a6aeb1SAndreas Gohr    // end output buffering and get contents
12678a6aeb1SAndreas Gohr    $css = ob_get_contents();
12778a6aeb1SAndreas Gohr    ob_end_clean();
12878a6aeb1SAndreas Gohr
129f8fb2d18SAndreas Gohr    // strip any source maps
130f8fb2d18SAndreas Gohr    stripsourcemaps($css);
131f8fb2d18SAndreas Gohr
1326e69c1baSAndreas Gohr    // apply style replacements
133afb2c082SAndreas Gohr    $css = css_applystyle($css, $styleini['replacements']);
1346e69c1baSAndreas Gohr
13572a66eb7SAndreas Gohr    // parse less
13672a66eb7SAndreas Gohr    $css = css_parseless($css);
137d4a1ece8SAndreas Gohr
13878a6aeb1SAndreas Gohr    // compress whitespace and comments
13978a6aeb1SAndreas Gohr    if($conf['compress']){
14078a6aeb1SAndreas Gohr        $css = css_compress($css);
14178a6aeb1SAndreas Gohr    }
14278a6aeb1SAndreas Gohr
143809d3ba5SAndreas Gohr    // embed small images right into the stylesheet
144809d3ba5SAndreas Gohr    if($conf['cssdatauri']){
145809d3ba5SAndreas Gohr        $base = preg_quote(DOKU_BASE,'#');
146809d3ba5SAndreas Gohr        $css = preg_replace_callback('#(url\([ \'"]*)('.$base.')(.*?(?:\.(png|gif)))#i','css_datauri',$css);
147809d3ba5SAndreas Gohr    }
148809d3ba5SAndreas Gohr
1496619f42eSAdrian Lang    http_cached_finish($cache->cache, $css);
15078a6aeb1SAndreas Gohr}
15178a6aeb1SAndreas Gohr
15278a6aeb1SAndreas Gohr/**
15372a66eb7SAndreas Gohr * Uses phpless to parse LESS in our CSS
15472a66eb7SAndreas Gohr *
15572a66eb7SAndreas Gohr * most of this function is error handling to show a nice useful error when
15672a66eb7SAndreas Gohr * LESS compilation fails
15772a66eb7SAndreas Gohr *
158253d4b48SGerrit Uitslag * @param string $css
15972a66eb7SAndreas Gohr * @return string
16072a66eb7SAndreas Gohr */
16172a66eb7SAndreas Gohrfunction css_parseless($css) {
1627d247a3cSGerrit Uitslag    global $conf;
1637d247a3cSGerrit Uitslag
16472a66eb7SAndreas Gohr    $less = new lessc();
165343a31d8SAndreas Gohr    $less->importDir = array(DOKU_INC);
1667d247a3cSGerrit Uitslag    $less->setPreserveComments(!$conf['compress']);
16730f686ebSChristopher Smith
16830f686ebSChristopher Smith    if (defined('DOKU_UNITTEST')){
16930f686ebSChristopher Smith        $less->importDir[] = TMP_DIR;
17030f686ebSChristopher Smith    }
17130f686ebSChristopher Smith
17272a66eb7SAndreas Gohr    try {
17372a66eb7SAndreas Gohr        return $less->compile($css);
17472a66eb7SAndreas Gohr    } catch(Exception $e) {
17572a66eb7SAndreas Gohr        // get exception message
17672a66eb7SAndreas Gohr        $msg = str_replace(array("\n", "\r", "'"), array(), $e->getMessage());
17772a66eb7SAndreas Gohr
17872a66eb7SAndreas Gohr        // try to use line number to find affected file
17972a66eb7SAndreas Gohr        if(preg_match('/line: (\d+)$/', $msg, $m)){
18072a66eb7SAndreas Gohr            $msg = substr($msg, 0, -1* strlen($m[0])); //remove useless linenumber
18172a66eb7SAndreas Gohr            $lno = $m[1];
18272a66eb7SAndreas Gohr
18372a66eb7SAndreas Gohr            // walk upwards to last include
18472a66eb7SAndreas Gohr            $lines = explode("\n", $css);
18572a66eb7SAndreas Gohr            for($i=$lno-1; $i>=0; $i--){
18672a66eb7SAndreas Gohr                if(preg_match('/\/(\* XXXXXXXXX )(.*?)( XXXXXXXXX \*)\//', $lines[$i], $m)){
18772a66eb7SAndreas Gohr                    // we found it, add info to message
18872a66eb7SAndreas Gohr                    $msg .= ' in '.$m[2].' at line '.($lno-$i);
18972a66eb7SAndreas Gohr                    break;
19072a66eb7SAndreas Gohr                }
19172a66eb7SAndreas Gohr            }
19272a66eb7SAndreas Gohr        }
19372a66eb7SAndreas Gohr
19472a66eb7SAndreas Gohr        // something went wrong
19572a66eb7SAndreas Gohr        $error = 'A fatal error occured during compilation of the CSS files. '.
19672a66eb7SAndreas Gohr            'If you recently installed a new plugin or template it '.
19772a66eb7SAndreas Gohr            'might be broken and you should try disabling it again. ['.$msg.']';
19872a66eb7SAndreas Gohr
19972a66eb7SAndreas Gohr        echo ".dokuwiki:before {
20072a66eb7SAndreas Gohr            content: '$error';
20172a66eb7SAndreas Gohr            background-color: red;
20272a66eb7SAndreas Gohr            display: block;
20372a66eb7SAndreas Gohr            background-color: #fcc;
20472a66eb7SAndreas Gohr            border-color: #ebb;
20572a66eb7SAndreas Gohr            color: #000;
20672a66eb7SAndreas Gohr            padding: 0.5em;
20772a66eb7SAndreas Gohr        }";
20872a66eb7SAndreas Gohr
20972a66eb7SAndreas Gohr        exit;
21072a66eb7SAndreas Gohr    }
21172a66eb7SAndreas Gohr}
21272a66eb7SAndreas Gohr
21372a66eb7SAndreas Gohr/**
2146e69c1baSAndreas Gohr * Does placeholder replacements in the style according to
2156e69c1baSAndreas Gohr * the ones defined in a templates style.ini file
2166e69c1baSAndreas Gohr *
217d4a1ece8SAndreas Gohr * This also adds the ini defined placeholders as less variables
218d4a1ece8SAndreas Gohr * (sans the surrounding __ and with a ini_ prefix)
219d4a1ece8SAndreas Gohr *
2206e69c1baSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
221253d4b48SGerrit Uitslag *
222253d4b48SGerrit Uitslag * @param string $css
223253d4b48SGerrit Uitslag * @param array $replacements  array(placeholder => value)
224253d4b48SGerrit Uitslag * @return string
2256e69c1baSAndreas Gohr */
226afb2c082SAndreas Gohrfunction css_applystyle($css, $replacements) {
227cbe37079SAndreas Gohr    // we convert ini replacements to LESS variable names
228cbe37079SAndreas Gohr    // and build a list of variable: value; pairs
229d4a1ece8SAndreas Gohr    $less = '';
230afb2c082SAndreas Gohr    foreach((array) $replacements as $key => $value) {
231cbe37079SAndreas Gohr        $lkey = trim($key, '_');
232cbe37079SAndreas Gohr        $lkey = '@ini_'.$lkey;
233cbe37079SAndreas Gohr        $less .= "$lkey: $value;\n";
234cbe37079SAndreas Gohr
235afb2c082SAndreas Gohr        $replacements[$key] = $lkey;
236d4a1ece8SAndreas Gohr    }
237c51b334eSAndreas Gohr
238cbe37079SAndreas Gohr    // we now replace all old ini replacements with LESS variables
239afb2c082SAndreas Gohr    $css = strtr($css, $replacements);
240cbe37079SAndreas Gohr
241cbe37079SAndreas Gohr    // now prepend the list of LESS variables as the very first thing
242c51b334eSAndreas Gohr    $css = $less.$css;
2436e69c1baSAndreas Gohr    return $css;
2446e69c1baSAndreas Gohr}
2456e69c1baSAndreas Gohr
2466e69c1baSAndreas Gohr/**
247afb2c082SAndreas Gohr * Load style ini contents
2480ac69508SAnika Henke *
249afb2c082SAndreas Gohr * Loads and merges style.ini files from template and config and prepares
250afb2c082SAndreas Gohr * the stylesheet modes
251afb2c082SAndreas Gohr *
252afb2c082SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
253253d4b48SGerrit Uitslag *
254afb2c082SAndreas Gohr * @param string $tpl the used template
2554d6524b8SAndreas Gohr * @param bool   $preview load preview replacements
256afb2c082SAndreas Gohr * @return array with keys 'stylesheets' and 'replacements'
2570e6f9f08SAnika Henke */
2584d6524b8SAndreas Gohrfunction css_styleini($tpl, $preview=false) {
2594d6524b8SAndreas Gohr    global $conf;
2604d6524b8SAndreas Gohr
261afb2c082SAndreas Gohr    $stylesheets = array(); // mode, file => base
262afb2c082SAndreas Gohr    $replacements = array(); // placeholder => value
2630ac69508SAnika Henke
264afb2c082SAndreas Gohr    // load template's style.ini
265afb2c082SAndreas Gohr    $incbase = tpl_incdir($tpl);
266afb2c082SAndreas Gohr    $webbase = tpl_basedir($tpl);
267afb2c082SAndreas Gohr    $ini = $incbase.'style.ini';
268afb2c082SAndreas Gohr    if(file_exists($ini)){
269afb2c082SAndreas Gohr        $data = parse_ini_file($ini, true);
2700ac69508SAnika Henke
271afb2c082SAndreas Gohr        // stylesheets
272afb2c082SAndreas Gohr        if(is_array($data['stylesheets'])) foreach($data['stylesheets'] as $file => $mode){
273afb2c082SAndreas Gohr            $stylesheets[$mode][$incbase.$file] = $webbase;
274afb2c082SAndreas Gohr        }
275afb2c082SAndreas Gohr
276afb2c082SAndreas Gohr        // replacements
277afb2c082SAndreas Gohr        if(is_array($data['replacements'])){
27847f862d1SChristopher Smith            $replacements = array_merge($replacements, css_fixreplacementurls($data['replacements'],$webbase));
2790ac69508SAnika Henke        }
2800ac69508SAnika Henke    }
281afb2c082SAndreas Gohr
282afb2c082SAndreas Gohr    // load configs's style.ini
283afb2c082SAndreas Gohr    $webbase = DOKU_BASE;
2848c5aad7bSAnika Henke    $ini = DOKU_CONF."tpl/$tpl/style.ini";
2858c5aad7bSAnika Henke    $incbase = dirname($ini).'/';
286afb2c082SAndreas Gohr    if(file_exists($ini)){
287afb2c082SAndreas Gohr        $data = parse_ini_file($ini, true);
288afb2c082SAndreas Gohr
289afb2c082SAndreas Gohr        // stylesheets
29006c9ee33SMarius van Witzenburg        if(isset($data['stylesheets']) && is_array($data['stylesheets'])) foreach($data['stylesheets'] as $file => $mode){
291afb2c082SAndreas Gohr            $stylesheets[$mode][$incbase.$file] = $webbase;
2920ac69508SAnika Henke        }
293afb2c082SAndreas Gohr
294afb2c082SAndreas Gohr        // replacements
29506c9ee33SMarius van Witzenburg        if(isset($data['replacements']) && is_array($data['replacements'])){
29647f862d1SChristopher Smith            $replacements = array_merge($replacements, css_fixreplacementurls($data['replacements'],$webbase));
297afb2c082SAndreas Gohr        }
298afb2c082SAndreas Gohr    }
299afb2c082SAndreas Gohr
3004d6524b8SAndreas Gohr    // allow replacement overwrites in preview mode
3014d6524b8SAndreas Gohr    if($preview) {
3024d6524b8SAndreas Gohr        $webbase = DOKU_BASE;
3034d6524b8SAndreas Gohr        $ini     = $conf['cachedir'].'/preview.ini';
3044d6524b8SAndreas Gohr        if(file_exists($ini)) {
3054d6524b8SAndreas Gohr            $data = parse_ini_file($ini, true);
3064d6524b8SAndreas Gohr            // replacements
3074d6524b8SAndreas Gohr            if(is_array($data['replacements'])) {
3084d6524b8SAndreas Gohr                $replacements = array_merge($replacements, css_fixreplacementurls($data['replacements'], $webbase));
3094d6524b8SAndreas Gohr            }
3104d6524b8SAndreas Gohr        }
3114d6524b8SAndreas Gohr    }
3124d6524b8SAndreas Gohr
313afb2c082SAndreas Gohr    return array(
314afb2c082SAndreas Gohr        'stylesheets' => $stylesheets,
315afb2c082SAndreas Gohr        'replacements' => $replacements
316afb2c082SAndreas Gohr    );
3170e6f9f08SAnika Henke}
3180e6f9f08SAnika Henke
3191e2c5948SChristopher Smith/**
3201e2c5948SChristopher Smith * Amend paths used in replacement relative urls, refer FS#2879
3211e2c5948SChristopher Smith *
3221e2c5948SChristopher Smith * @author Chris Smith <chris@jalakai.co.uk>
323253d4b48SGerrit Uitslag *
324253d4b48SGerrit Uitslag * @param array $replacements with key-value pairs
325253d4b48SGerrit Uitslag * @param string $location
326253d4b48SGerrit Uitslag * @return array
3271e2c5948SChristopher Smith */
32847f862d1SChristopher Smithfunction css_fixreplacementurls($replacements, $location) {
32947f862d1SChristopher Smith    foreach($replacements as $key => $value) {
33047f862d1SChristopher Smith        $replacements[$key] = preg_replace('#(url\([ \'"]*)(?!/|data:|http://|https://| |\'|")#','\\1'.$location,$value);
33147f862d1SChristopher Smith    }
33247f862d1SChristopher Smith    return $replacements;
33347f862d1SChristopher Smith}
33447f862d1SChristopher Smith
3350e6f9f08SAnika Henke/**
3361c2d1019SAndreas Gohr * Prints classes for interwikilinks
3371c2d1019SAndreas Gohr *
3381c2d1019SAndreas Gohr * Interwiki links have two classes: 'interwiki' and 'iw_$name>' where
3391c2d1019SAndreas Gohr * $name is the identifier given in the config. All Interwiki links get
3401c2d1019SAndreas Gohr * an default style with a default icon. If a special icon is available
3411c2d1019SAndreas Gohr * for an interwiki URL it is set in it's own class. Both classes can be
3421c2d1019SAndreas Gohr * overwritten in the template or userstyles.
3431c2d1019SAndreas Gohr *
3441c2d1019SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
3451c2d1019SAndreas Gohr */
3461c2d1019SAndreas Gohrfunction css_interwiki(){
3471c2d1019SAndreas Gohr
3481c2d1019SAndreas Gohr    // default style
3491c2d1019SAndreas Gohr    echo 'a.interwiki {';
3501c2d1019SAndreas Gohr    echo ' background: transparent url('.DOKU_BASE.'lib/images/interwiki.png) 0px 1px no-repeat;';
3517b4ea081Smarklundeberg    echo ' padding: 1px 0px 1px 16px;';
3521c2d1019SAndreas Gohr    echo '}';
3531c2d1019SAndreas Gohr
3541c2d1019SAndreas Gohr    // additional styles when icon available
3551c2d1019SAndreas Gohr    $iwlinks = getInterwiki();
3561c2d1019SAndreas Gohr    foreach(array_keys($iwlinks) as $iw){
3579d2ddea4SAndreas Gohr        $class = preg_replace('/[^_\-a-z0-9]+/i','_',$iw);
35879e79377SAndreas Gohr        if(file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.png')){
3599d2ddea4SAndreas Gohr            echo "a.iw_$class {";
3601c2d1019SAndreas Gohr            echo '  background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.png)';
3611c2d1019SAndreas Gohr            echo '}';
36279e79377SAndreas Gohr        }elseif(file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.gif')){
3639d2ddea4SAndreas Gohr            echo "a.iw_$class {";
3641c2d1019SAndreas Gohr            echo '  background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.gif)';
3651c2d1019SAndreas Gohr            echo '}';
3661c2d1019SAndreas Gohr        }
3671c2d1019SAndreas Gohr    }
368d15166e5SAndreas Gohr}
3691c2d1019SAndreas Gohr
370d15166e5SAndreas Gohr/**
371d15166e5SAndreas Gohr * Prints classes for file download links
372d15166e5SAndreas Gohr *
373d15166e5SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
374d15166e5SAndreas Gohr */
375d15166e5SAndreas Gohrfunction css_filetypes(){
376d15166e5SAndreas Gohr
377d15166e5SAndreas Gohr    // default style
378035e07f1SKate Arzamastseva    echo '.mediafile {';
379d15166e5SAndreas Gohr    echo ' background: transparent url('.DOKU_BASE.'lib/images/fileicons/file.png) 0px 1px no-repeat;';
3805b77caf4SAndreas Gohr    echo ' padding-left: 18px;';
3815b77caf4SAndreas Gohr    echo ' padding-bottom: 1px;';
382d15166e5SAndreas Gohr    echo '}';
383d15166e5SAndreas Gohr
384d15166e5SAndreas Gohr    // additional styles when icon available
38527bf7924STom N Harris    // scan directory for all icons
38627bf7924STom N Harris    $exts = array();
38727bf7924STom N Harris    if($dh = opendir(DOKU_INC.'lib/images/fileicons')){
38827bf7924STom N Harris        while(false !== ($file = readdir($dh))){
38927bf7924STom N Harris            if(preg_match('/([_\-a-z0-9]+(?:\.[_\-a-z0-9]+)*?)\.(png|gif)/i',$file,$match)){
39027bf7924STom N Harris                $ext = strtolower($match[1]);
39127bf7924STom N Harris                $type = '.'.strtolower($match[2]);
39227bf7924STom N Harris                if($ext!='file' && (!isset($exts[$ext]) || $type=='.png')){
39327bf7924STom N Harris                    $exts[$ext] = $type;
394d15166e5SAndreas Gohr                }
395d15166e5SAndreas Gohr            }
3961c2d1019SAndreas Gohr        }
39727bf7924STom N Harris        closedir($dh);
39827bf7924STom N Harris    }
39927bf7924STom N Harris    foreach($exts as $ext=>$type){
40027bf7924STom N Harris        $class = preg_replace('/[^_\-a-z0-9]+/','_',$ext);
401035e07f1SKate Arzamastseva        echo ".mf_$class {";
40227bf7924STom N Harris        echo '  background-image: url('.DOKU_BASE.'lib/images/fileicons/'.$ext.$type.')';
40327bf7924STom N Harris        echo '}';
40427bf7924STom N Harris    }
40527bf7924STom N Harris}
4061c2d1019SAndreas Gohr
4071c2d1019SAndreas Gohr/**
40878a6aeb1SAndreas Gohr * Loads a given file and fixes relative URLs with the
40978a6aeb1SAndreas Gohr * given location prefix
410253d4b48SGerrit Uitslag *
411253d4b48SGerrit Uitslag * @param string $file file system path
412253d4b48SGerrit Uitslag * @param string $location
413253d4b48SGerrit Uitslag * @return string
41478a6aeb1SAndreas Gohr */
41578a6aeb1SAndreas Gohrfunction css_loadfile($file,$location=''){
41612ffbbc3SChristopher Smith    $css_file = new DokuCssFile($file);
41712ffbbc3SChristopher Smith    return $css_file->load($location);
41812ffbbc3SChristopher Smith}
41912ffbbc3SChristopher Smith
4201e2c5948SChristopher Smith/**
4211e2c5948SChristopher Smith *  Helper class to abstract loading of css/less files
4221e2c5948SChristopher Smith *
4231e2c5948SChristopher Smith *  @author Chris Smith <chris@jalakai.co.uk>
4241e2c5948SChristopher Smith */
42512ffbbc3SChristopher Smithclass DokuCssFile {
42612ffbbc3SChristopher Smith
4271e2c5948SChristopher Smith    protected $filepath;             // file system path to the CSS/Less file
4281e2c5948SChristopher Smith    protected $location;             // base url location of the CSS/Less file
429fd18b5f4SGerrit Uitslag    protected $relative_path = null;
43012ffbbc3SChristopher Smith
43112ffbbc3SChristopher Smith    public function __construct($file) {
43212ffbbc3SChristopher Smith        $this->filepath = $file;
43312ffbbc3SChristopher Smith    }
43412ffbbc3SChristopher Smith
4351e2c5948SChristopher Smith    /**
4361e2c5948SChristopher Smith     * Load the contents of the css/less file and adjust any relative paths/urls (relative to this file) to be
4371e2c5948SChristopher Smith     * relative to the dokuwiki root: the web root (DOKU_BASE) for most files; the file system root (DOKU_INC)
4381e2c5948SChristopher Smith     * for less files.
4391e2c5948SChristopher Smith     *
4401e2c5948SChristopher Smith     * @param   string   $location   base url for this file
4411e2c5948SChristopher Smith     * @return  string               the CSS/Less contents of the file
4421e2c5948SChristopher Smith     */
44312ffbbc3SChristopher Smith    public function load($location='') {
44479e79377SAndreas Gohr        if (!file_exists($this->filepath)) return '';
44512ffbbc3SChristopher Smith
44612ffbbc3SChristopher Smith        $css = io_readFile($this->filepath);
44778a6aeb1SAndreas Gohr        if (!$location) return $css;
44878a6aeb1SAndreas Gohr
44912ffbbc3SChristopher Smith        $this->location = $location;
450de737055SChristopher Smith
45112ffbbc3SChristopher Smith        $css = preg_replace_callback('#(url\( *)([\'"]?)(.*?)(\2)( *\))#',array($this,'replacements'),$css);
45212ffbbc3SChristopher Smith        $css = preg_replace_callback('#(@import\s+)([\'"])(.*?)(\2)#',array($this,'replacements'),$css);
453809d3ba5SAndreas Gohr
45478a6aeb1SAndreas Gohr        return $css;
45578a6aeb1SAndreas Gohr    }
45678a6aeb1SAndreas Gohr
4571e2c5948SChristopher Smith    /**
4581e2c5948SChristopher Smith     * Get the relative file system path of this file, relative to dokuwiki's root folder, DOKU_INC
4591e2c5948SChristopher Smith     *
4601e2c5948SChristopher Smith     * @return string   relative file system path
4611e2c5948SChristopher Smith     */
462fd18b5f4SGerrit Uitslag    protected function getRelativePath(){
46312ffbbc3SChristopher Smith
46412ffbbc3SChristopher Smith        if (is_null($this->relative_path)) {
46512ffbbc3SChristopher Smith            $basedir = array(DOKU_INC);
4661e2c5948SChristopher Smith
4671e2c5948SChristopher Smith            // during testing, files may be found relative to a second base dir, TMP_DIR
46812ffbbc3SChristopher Smith            if (defined('DOKU_UNITTEST')) {
46912ffbbc3SChristopher Smith                $basedir[] = realpath(TMP_DIR);
47012ffbbc3SChristopher Smith            }
47112ffbbc3SChristopher Smith
47273f25ac0SAndreas Gohr            $basedir = array_map('preg_quote_cb', $basedir);
47373f25ac0SAndreas Gohr            $regex = '/^('.join('|',$basedir).')/';
47412ffbbc3SChristopher Smith            $this->relative_path = preg_replace($regex, '', dirname($this->filepath));
47512ffbbc3SChristopher Smith        }
47612ffbbc3SChristopher Smith
47712ffbbc3SChristopher Smith        return $this->relative_path;
47812ffbbc3SChristopher Smith    }
47912ffbbc3SChristopher Smith
4801e2c5948SChristopher Smith    /**
4811e2c5948SChristopher Smith     * preg_replace callback to adjust relative urls from relative to this file to relative
4821e2c5948SChristopher Smith     * to the appropriate dokuwiki root location as described in the code
4831e2c5948SChristopher Smith     *
4841e2c5948SChristopher Smith     * @param  array    see http://php.net/preg_replace_callback
4851e2c5948SChristopher Smith     * @return string   see http://php.net/preg_replace_callback
4861e2c5948SChristopher Smith     */
48712ffbbc3SChristopher Smith    public function replacements($match) {
488de737055SChristopher Smith
4891e2c5948SChristopher Smith        // not a relative url? - no adjustment required
490de737055SChristopher Smith        if (preg_match('#^(/|data:|https?://)#',$match[3])) {
491de737055SChristopher Smith            return $match[0];
492de737055SChristopher Smith        }
4931e2c5948SChristopher Smith        // a less file import? - requires a file system location
494de737055SChristopher Smith        else if (substr($match[3],-5) == '.less') {
495de737055SChristopher Smith            if ($match[3]{0} != '/') {
49612ffbbc3SChristopher Smith                $match[3] = $this->getRelativePath() . '/' . $match[3];
497de737055SChristopher Smith            }
498de737055SChristopher Smith        }
4991e2c5948SChristopher Smith        // everything else requires a url adjustment
500de737055SChristopher Smith        else {
50112ffbbc3SChristopher Smith            $match[3] = $this->location . $match[3];
502de737055SChristopher Smith        }
503de737055SChristopher Smith
504de737055SChristopher Smith        return join('',array_slice($match,1));
505de737055SChristopher Smith    }
50612ffbbc3SChristopher Smith}
507de737055SChristopher Smith
508809d3ba5SAndreas Gohr/**
5094eb5f931SChristopher Smith * Convert local image URLs to data URLs if the filesize is small
510809d3ba5SAndreas Gohr *
511809d3ba5SAndreas Gohr * Callback for preg_replace_callback
512253d4b48SGerrit Uitslag *
513253d4b48SGerrit Uitslag * @param array $match
514253d4b48SGerrit Uitslag * @return string
515809d3ba5SAndreas Gohr */
516809d3ba5SAndreas Gohrfunction css_datauri($match){
51728f4004cSAndreas Gohr    global $conf;
51828f4004cSAndreas Gohr
519809d3ba5SAndreas Gohr    $pre   = unslash($match[1]);
520809d3ba5SAndreas Gohr    $base  = unslash($match[2]);
521809d3ba5SAndreas Gohr    $url   = unslash($match[3]);
522809d3ba5SAndreas Gohr    $ext   = unslash($match[4]);
523809d3ba5SAndreas Gohr
524809d3ba5SAndreas Gohr    $local = DOKU_INC.$url;
525809d3ba5SAndreas Gohr    $size  = @filesize($local);
52628f4004cSAndreas Gohr    if($size && $size < $conf['cssdatauri']){
527809d3ba5SAndreas Gohr        $data = base64_encode(file_get_contents($local));
528809d3ba5SAndreas Gohr    }
529809d3ba5SAndreas Gohr    if($data){
5306a5d6817SAnika Henke        $url = 'data:image/'.$ext.';base64,'.$data;
531809d3ba5SAndreas Gohr    }else{
532809d3ba5SAndreas Gohr        $url = $base.$url;
533809d3ba5SAndreas Gohr    }
534809d3ba5SAndreas Gohr    return $pre.$url;
535809d3ba5SAndreas Gohr}
536809d3ba5SAndreas Gohr
53715c394afSAndreas Gohr
53878a6aeb1SAndreas Gohr/**
53978a6aeb1SAndreas Gohr * Returns a list of possible Plugin Styles (no existance check here)
54078a6aeb1SAndreas Gohr *
54178a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
542253d4b48SGerrit Uitslag *
543253d4b48SGerrit Uitslag * @param string $mediatype
544253d4b48SGerrit Uitslag * @return array
54578a6aeb1SAndreas Gohr */
546318cd03eSAnika Henkefunction css_pluginstyles($mediatype='screen'){
54778a6aeb1SAndreas Gohr    $list = array();
54878a6aeb1SAndreas Gohr    $plugins = plugin_list();
54978a6aeb1SAndreas Gohr    foreach ($plugins as $p){
550318cd03eSAnika Henke        $list[DOKU_PLUGIN."$p/$mediatype.css"]  = DOKU_BASE."lib/plugins/$p/";
551d4a1ece8SAndreas Gohr        $list[DOKU_PLUGIN."$p/$mediatype.less"]  = DOKU_BASE."lib/plugins/$p/";
552318cd03eSAnika Henke        // alternative for screen.css
553318cd03eSAnika Henke        if ($mediatype=='screen') {
55478a6aeb1SAndreas Gohr            $list[DOKU_PLUGIN."$p/style.css"]  = DOKU_BASE."lib/plugins/$p/";
555d4a1ece8SAndreas Gohr            $list[DOKU_PLUGIN."$p/style.less"]  = DOKU_BASE."lib/plugins/$p/";
55678a6aeb1SAndreas Gohr        }
55778a6aeb1SAndreas Gohr    }
55878a6aeb1SAndreas Gohr    return $list;
55978a6aeb1SAndreas Gohr}
56078a6aeb1SAndreas Gohr
56178a6aeb1SAndreas Gohr/**
56278a6aeb1SAndreas Gohr * Very simple CSS optimizer
56378a6aeb1SAndreas Gohr *
56478a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
565253d4b48SGerrit Uitslag *
566253d4b48SGerrit Uitslag * @param string $css
567253d4b48SGerrit Uitslag * @return string
56878a6aeb1SAndreas Gohr */
56978a6aeb1SAndreas Gohrfunction css_compress($css){
570fd7c2db0SAndreas Gohr    //strip comments through a callback
571fd7c2db0SAndreas Gohr    $css = preg_replace_callback('#(/\*)(.*?)(\*/)#s','css_comment_cb',$css);
572fd7c2db0SAndreas Gohr
573247c1c5dSAndreas Gohr    //strip (incorrect but common) one line comments
574fe5a50c3SAndreas Gohr    $css = preg_replace_callback('/^.*\/\/.*$/m','css_onelinecomment_cb',$css);
575247c1c5dSAndreas Gohr
57678a6aeb1SAndreas Gohr    // strip whitespaces
57778a6aeb1SAndreas Gohr    $css = preg_replace('![\r\n\t ]+!',' ',$css);
578f5379589SChristopher Smith    $css = preg_replace('/ ?([;,{}\/]) ?/','\\1',$css);
579f5379589SChristopher Smith    $css = preg_replace('/ ?: /',':',$css);
58078a6aeb1SAndreas Gohr
581205907a7Sfurun    // number compression
582205907a7Sfurun    $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"
583205907a7Sfurun    $css = preg_replace('/([: ])\.(0)+((?:pt|pc|in|mm|cm|em|ex|px)\b|%)(?=[^\{]*[;\}])/', '$1$2', $css); // ".0em" to "0"
584205907a7Sfurun    $css = preg_replace('/([: ]0)0*(\.0*)?((?:pt|pc|in|mm|cm|em|ex|px)(?=[^\{]*[;\}])\b|%)/', '$1', $css); // "0.0em" to "0"
585205907a7Sfurun    $css = preg_replace('/([: ]\d+)(\.0*)((?:pt|pc|in|mm|cm|em|ex|px)(?=[^\{]*[;\}])\b|%)/', '$1$3', $css); // "1.0em" to "1em"
586205907a7Sfurun    $css = preg_replace('/([: ])0+(\d+|\d*\.\d+)((?:pt|pc|in|mm|cm|em|ex|px)(?=[^\{]*[;\}])\b|%)/', '$1$2$3', $css); // "001em" to "1em"
587205907a7Sfurun
588205907a7Sfurun    // shorten attributes (1em 1em 1em 1em -> 1em)
589205907a7Sfurun    $css = preg_replace('/(?<![\w\-])((?:margin|padding|border|border-(?:width|radius)):)([\w\.]+)( \2)+(?=[;\}]| !)/', '$1$2', $css); // "1em 1em 1em 1em" to "1em"
590205907a7Sfurun    $css = preg_replace('/(?<![\w\-])((?:margin|padding|border|border-(?:width)):)([\w\.]+) ([\w\.]+) \2 \3(?=[;\}]| !)/', '$1$2 $3', $css); // "1em 2em 1em 2em" to "1em 2em"
591205907a7Sfurun
59278a6aeb1SAndreas Gohr    // shorten colors
593205907a7Sfurun    $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);
59478a6aeb1SAndreas Gohr
59578a6aeb1SAndreas Gohr    return $css;
59678a6aeb1SAndreas Gohr}
59778a6aeb1SAndreas Gohr
598c00aef76SAndreas Gohr/**
599c00aef76SAndreas Gohr * Callback for css_compress()
600c00aef76SAndreas Gohr *
601c00aef76SAndreas Gohr * Keeps short comments (< 5 chars) to maintain typical browser hacks
602c00aef76SAndreas Gohr *
603c00aef76SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
604253d4b48SGerrit Uitslag *
605253d4b48SGerrit Uitslag * @param array $matches
606253d4b48SGerrit Uitslag * @return string
607c00aef76SAndreas Gohr */
608c00aef76SAndreas Gohrfunction css_comment_cb($matches){
609c00aef76SAndreas Gohr    if(strlen($matches[2]) > 4) return '';
610c00aef76SAndreas Gohr    return $matches[0];
611c00aef76SAndreas Gohr}
61278a6aeb1SAndreas Gohr
613fe5a50c3SAndreas Gohr/**
614fe5a50c3SAndreas Gohr * Callback for css_compress()
615fe5a50c3SAndreas Gohr *
616fe5a50c3SAndreas Gohr * Strips one line comments but makes sure it will not destroy url() constructs with slashes
617fe5a50c3SAndreas Gohr *
618253d4b48SGerrit Uitslag * @param array $matches
619fe5a50c3SAndreas Gohr * @return string
620fe5a50c3SAndreas Gohr */
621fe5a50c3SAndreas Gohrfunction css_onelinecomment_cb($matches) {
622fe5a50c3SAndreas Gohr    $line = $matches[0];
623fe5a50c3SAndreas Gohr
624fe5a50c3SAndreas Gohr    $i = 0;
625fe5a50c3SAndreas Gohr    $len = strlen($line);
626918a4468SAndreas Gohr
627fe5a50c3SAndreas Gohr    while ($i< $len){
628fe5a50c3SAndreas Gohr        $nextcom = strpos($line, '//', $i);
629fe5a50c3SAndreas Gohr        $nexturl = stripos($line, 'url(', $i);
630fe5a50c3SAndreas Gohr
631fe5a50c3SAndreas Gohr        if($nextcom === false) {
632fe5a50c3SAndreas Gohr            // no more comments, we're done
633918a4468SAndreas Gohr            $i = $len;
634fe5a50c3SAndreas Gohr            break;
635fe5a50c3SAndreas Gohr        }
636fe5a50c3SAndreas Gohr
637918a4468SAndreas Gohr        // keep any quoted string that starts before a comment
638918a4468SAndreas Gohr        $nextsqt = strpos($line, "'", $i);
639918a4468SAndreas Gohr        $nextdqt = strpos($line, '"', $i);
640918a4468SAndreas Gohr        if(min($nextsqt, $nextdqt) < $nextcom) {
641918a4468SAndreas Gohr            $skipto = false;
642918a4468SAndreas Gohr            if($nextsqt !== false && ($nextdqt === false || $nextsqt < $nextdqt)) {
643918a4468SAndreas Gohr                $skipto = strpos($line, "'", $nextsqt+1) +1;
644918a4468SAndreas Gohr            } else if ($nextdqt !== false) {
645918a4468SAndreas Gohr                $skipto = strpos($line, '"', $nextdqt+1) +1;
646918a4468SAndreas Gohr            }
647918a4468SAndreas Gohr
648918a4468SAndreas Gohr            if($skipto !== false) {
649918a4468SAndreas Gohr                $i = $skipto;
650918a4468SAndreas Gohr                continue;
651918a4468SAndreas Gohr            }
652918a4468SAndreas Gohr        }
653918a4468SAndreas Gohr
654918a4468SAndreas Gohr        if($nexturl === false || $nextcom < $nexturl) {
655918a4468SAndreas Gohr            // no url anymore, strip comment and be done
656918a4468SAndreas Gohr            $i = $nextcom;
657918a4468SAndreas Gohr            break;
658918a4468SAndreas Gohr        }
659918a4468SAndreas Gohr
660918a4468SAndreas Gohr        // we have an upcoming url
661918a4468SAndreas Gohr        $i = strpos($line, ')', $nexturl);
662918a4468SAndreas Gohr    }
663918a4468SAndreas Gohr
664918a4468SAndreas Gohr    return substr($line, 0, $i);
665fe5a50c3SAndreas Gohr}
666fe5a50c3SAndreas Gohr
667e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
668