xref: /dokuwiki/lib/exe/css.php (revision dc7da772c0bd95eaee1f2fc25d29cb5108ff0809)
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
90db5771eSMichael Großeuse dokuwiki\Cache\Cache;
100db5771eSMichael Große
11d0a27cb0SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/../../');
121c2d1019SAndreas Gohrif(!defined('NOSESSION')) define('NOSESSION',true); // we do not use a session or authentication here (better caching)
1398bda4fdSAndreas Gohrif(!defined('DOKU_DISABLE_GZIP_OUTPUT')) define('DOKU_DISABLE_GZIP_OUTPUT',1); // we gzip ourself here
146c47a78cSAnika Henkeif(!defined('NL')) define('NL',"\n");
1578a6aeb1SAndreas Gohrrequire_once(DOKU_INC.'inc/init.php');
1678a6aeb1SAndreas Gohr
1778a6aeb1SAndreas Gohr// Main (don't run when UNIT test)
1878a6aeb1SAndreas Gohrif(!defined('SIMPLE_TEST')){
1978a6aeb1SAndreas Gohr    header('Content-Type: text/css; charset=utf-8');
2078a6aeb1SAndreas Gohr    css_out();
2178a6aeb1SAndreas Gohr}
2278a6aeb1SAndreas Gohr
2378a6aeb1SAndreas Gohr
2478a6aeb1SAndreas Gohr// ---------------------- functions ------------------------------
2578a6aeb1SAndreas Gohr
2678a6aeb1SAndreas Gohr/**
2778a6aeb1SAndreas Gohr * Output all needed Styles
2878a6aeb1SAndreas Gohr *
2978a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
3078a6aeb1SAndreas Gohr */
3178a6aeb1SAndreas Gohrfunction css_out(){
3278a6aeb1SAndreas Gohr    global $conf;
3378a6aeb1SAndreas Gohr    global $lang;
3409edb711SAndreas Gohr    global $config_cascade;
35bfd0f597STom N Harris    global $INPUT;
3609edb711SAndreas Gohr
37de4634ecSGerry Weißbach    if ($INPUT->str('s') == 'feed') {
38de4634ecSGerry Weißbach        $mediatypes = array('feed');
39de4634ecSGerry Weißbach        $type = 'feed';
40de4634ecSGerry Weißbach    } else {
415d48a5eaSGerry Weißbach        $mediatypes = array('screen', 'all', 'print', 'speech');
42de4634ecSGerry Weißbach        $type = '';
43de4634ecSGerry Weißbach    }
44de4634ecSGerry Weißbach
4520a2375aSAndreas Gohr    // decide from where to get the template
4620a2375aSAndreas Gohr    $tpl = trim(preg_replace('/[^\w-]+/','',$INPUT->str('t')));
4720a2375aSAndreas Gohr    if(!$tpl) $tpl = $conf['template'];
4820a2375aSAndreas Gohr
4914cd40ecSChang Zhao    // load style.ini
50fb1f9089SMichael Große    $styleUtil = new \dokuwiki\StyleUtils();
51fb1f9089SMichael Große    $styleini = $styleUtil->cssStyleini($tpl, $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";
58afb2c082SAndreas Gohr    $cache_files[] = __FILE__;
594d6524b8SAndreas Gohr    if($INPUT->bool('preview')) $cache_files[] = $conf['cachedir'].'/preview.ini';
60afb2c082SAndreas Gohr
6178a6aeb1SAndreas Gohr    // Array of needed files and their web locations, the latter ones
6278a6aeb1SAndreas Gohr    // are needed to fix relative paths in the stylesheets
63ef36714bSGerry Weißbach    $media_files = array();
6414977bd2SMichael Hamann    foreach($mediatypes as $mediatype) {
65ef36714bSGerry Weißbach        $files = array();
663e32b194SGerry Weißbach
67318cd03eSAnika Henke        // load core styles
68ef36714bSGerry Weißbach        $files[DOKU_INC.'lib/styles/'.$mediatype.'.css'] = DOKU_BASE.'lib/styles/';
69af4684acSAndreas Gohr
7043576758SAndreas Gohr        // load jQuery-UI theme
716c47a78cSAnika Henke        if ($mediatype == 'screen') {
7264159a61SAndreas Gohr            $files[DOKU_INC.'lib/scripts/jquery/jquery-ui-theme/smoothness.css'] =
7364159a61SAndreas Gohr                DOKU_BASE.'lib/scripts/jquery/jquery-ui-theme/';
746c47a78cSAnika Henke        }
75318cd03eSAnika Henke        // load plugin styles
76ef36714bSGerry Weißbach        $files = array_merge($files, css_pluginstyles($mediatype));
77318cd03eSAnika Henke        // load template styles
78afb2c082SAndreas Gohr        if (isset($styleini['stylesheets'][$mediatype])) {
79ef36714bSGerry Weißbach            $files = array_merge($files, $styleini['stylesheets'][$mediatype]);
8009edb711SAndreas Gohr        }
81318cd03eSAnika Henke        // load user styles
827b909d5eSGerrit Uitslag        if(!empty($config_cascade['userstyle'][$mediatype])) {
837b909d5eSGerrit Uitslag            foreach($config_cascade['userstyle'][$mediatype] as $userstyle) {
84ef36714bSGerry Weißbach                $files[$userstyle] = DOKU_BASE;
857b909d5eSGerrit Uitslag            }
86318cd03eSAnika Henke        }
8778a6aeb1SAndreas Gohr
88ef36714bSGerry Weißbach        // Let plugins decide to either put more styles here or to remove some
893e32b194SGerry Weißbach        $media_files[$mediatype] = css_filewrapper($mediatype, $files);
90ef36714bSGerry Weißbach        $CSSEvt = new Doku_Event('CSS_STYLES_INCLUDED', $media_files[$mediatype]);
91ef36714bSGerry Weißbach
92ef36714bSGerry Weißbach        // Make it preventable.
93ef36714bSGerry Weißbach        if ( $CSSEvt->advise_before() ) {
94ef36714bSGerry Weißbach            $cache_files = array_merge($cache_files, array_keys($media_files[$mediatype]['files']));
95ef36714bSGerry Weißbach        } else {
96ef36714bSGerry Weißbach            // unset if prevented. Nothing will be printed for this mediatype.
97ef36714bSGerry Weißbach            unset($media_files[$mediatype]);
9814977bd2SMichael Hamann        }
996619f42eSAdrian Lang
100ef36714bSGerry Weißbach        // finish event.
101ef36714bSGerry Weißbach        $CSSEvt->advise_after();
102ef36714bSGerry Weißbach    }
103ef36714bSGerry Weißbach
104ef36714bSGerry Weißbach    // The generated script depends on some dynamic options
1050db5771eSMichael Große    $cache = new Cache(
10664159a61SAndreas Gohr        'styles' .
10764159a61SAndreas Gohr        $_SERVER['HTTP_HOST'] .
10864159a61SAndreas Gohr        $_SERVER['SERVER_PORT'] .
10964159a61SAndreas Gohr        $INPUT->bool('preview') .
11064159a61SAndreas Gohr        DOKU_BASE .
11164159a61SAndreas Gohr        $tpl .
11264159a61SAndreas Gohr        $type,
11364159a61SAndreas Gohr        '.css'
11464159a61SAndreas Gohr    );
115*dc7da772SMichael Große    $cache->setEvent('CSS_CACHE_USE');
116ef36714bSGerry Weißbach
11738f56bffSBen Coburn    // check cache age & handle conditional request
1186619f42eSAdrian Lang    // This may exit if a cache can be used
119e7c68c4dSGerry Weißbach    $cache_ok = $cache->useCache(array('files' => $cache_files));
120e7c68c4dSGerry Weißbach    http_cached($cache->cache, $cache_ok);
12178a6aeb1SAndreas Gohr
1223899c2ecSMichael Hamann    // start output buffering
1233899c2ecSMichael Hamann    ob_start();
1243899c2ecSMichael Hamann
125ef36714bSGerry Weißbach    // Fire CSS_STYLES_INCLUDED for one last time to let the
126ef36714bSGerry Weißbach    // plugins decide whether to include the DW default styles.
127ef36714bSGerry Weißbach    // This can be done by preventing the Default.
128ef36714bSGerry Weißbach    $media_files['DW_DEFAULT'] = css_filewrapper('DW_DEFAULT');
129ef36714bSGerry Weißbach    trigger_event('CSS_STYLES_INCLUDED', $media_files['DW_DEFAULT'], 'css_defaultstyles');
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/**
281ef36714bSGerry Weißbach * Wrapper for the files, content and mediatype for the event CSS_STYLES_INCLUDED
282ef36714bSGerry Weißbach *
283ef36714bSGerry Weißbach * @author Gerry Weißbach <gerry.w@gammaproduction.de>
284ef36714bSGerry Weißbach *
285ef36714bSGerry Weißbach * @param string $mediatype type ofthe current media files/content set
286ef36714bSGerry Weißbach * @param array $files set of files that define the current mediatype
287ef36714bSGerry Weißbach * @return array
288ef36714bSGerry Weißbach */
2893e32b194SGerry Weißbachfunction css_filewrapper($mediatype, $files=array()){
290ef36714bSGerry Weißbach    return array(
291ef36714bSGerry Weißbach            'files'                 => $files,
292ef36714bSGerry Weißbach            'mediatype'             => $mediatype,
293cf35519cSGerry Weißbach            'encapsulate'           => $mediatype != 'all',
294ef36714bSGerry Weißbach            'encapsulationPrefix'   => '@media '.$mediatype
295ef36714bSGerry Weißbach        );
296ef36714bSGerry Weißbach}
297ef36714bSGerry Weißbach
298ef36714bSGerry Weißbach/**
299ef36714bSGerry Weißbach * Prints the @media encapsulated default styles of DokuWiki
300ef36714bSGerry Weißbach *
301ef36714bSGerry Weißbach * @author Gerry Weißbach <gerry.w@gammaproduction.de>
302ef36714bSGerry Weißbach *
303ef36714bSGerry Weißbach * This function is being called by a CSS_STYLES_INCLUDED event
304ef36714bSGerry Weißbach * The event can be distinguished by the mediatype which is:
305ef36714bSGerry Weißbach *   DW_DEFAULT
306ef36714bSGerry Weißbach */
307ef36714bSGerry Weißbachfunction css_defaultstyles(){
308ef36714bSGerry Weißbach    // print the default classes for interwiki links and file downloads
309ef36714bSGerry Weißbach    print '@media screen {';
310ef36714bSGerry Weißbach    css_interwiki();
311ef36714bSGerry Weißbach    css_filetypes();
312ef36714bSGerry Weißbach    print '}';
313ef36714bSGerry Weißbach}
314ef36714bSGerry Weißbach
315ef36714bSGerry Weißbach/**
3161c2d1019SAndreas Gohr * Prints classes for interwikilinks
3171c2d1019SAndreas Gohr *
3181c2d1019SAndreas Gohr * Interwiki links have two classes: 'interwiki' and 'iw_$name>' where
3191c2d1019SAndreas Gohr * $name is the identifier given in the config. All Interwiki links get
3201c2d1019SAndreas Gohr * an default style with a default icon. If a special icon is available
3211c2d1019SAndreas Gohr * for an interwiki URL it is set in it's own class. Both classes can be
3221c2d1019SAndreas Gohr * overwritten in the template or userstyles.
3231c2d1019SAndreas Gohr *
3241c2d1019SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
3251c2d1019SAndreas Gohr */
3261c2d1019SAndreas Gohrfunction css_interwiki(){
3271c2d1019SAndreas Gohr
3281c2d1019SAndreas Gohr    // default style
3291c2d1019SAndreas Gohr    echo 'a.interwiki {';
3301c2d1019SAndreas Gohr    echo ' background: transparent url('.DOKU_BASE.'lib/images/interwiki.png) 0px 1px no-repeat;';
3317b4ea081Smarklundeberg    echo ' padding: 1px 0px 1px 16px;';
3321c2d1019SAndreas Gohr    echo '}';
3331c2d1019SAndreas Gohr
3341c2d1019SAndreas Gohr    // additional styles when icon available
3351c2d1019SAndreas Gohr    $iwlinks = getInterwiki();
3361c2d1019SAndreas Gohr    foreach(array_keys($iwlinks) as $iw){
3379d2ddea4SAndreas Gohr        $class = preg_replace('/[^_\-a-z0-9]+/i','_',$iw);
33879e79377SAndreas Gohr        if(file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.png')){
3399d2ddea4SAndreas Gohr            echo "a.iw_$class {";
3401c2d1019SAndreas Gohr            echo '  background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.png)';
3411c2d1019SAndreas Gohr            echo '}';
34279e79377SAndreas Gohr        }elseif(file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.gif')){
3439d2ddea4SAndreas Gohr            echo "a.iw_$class {";
3441c2d1019SAndreas Gohr            echo '  background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.gif)';
3451c2d1019SAndreas Gohr            echo '}';
3461c2d1019SAndreas Gohr        }
3471c2d1019SAndreas Gohr    }
348d15166e5SAndreas Gohr}
3491c2d1019SAndreas Gohr
350d15166e5SAndreas Gohr/**
351d15166e5SAndreas Gohr * Prints classes for file download links
352d15166e5SAndreas Gohr *
353d15166e5SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
354d15166e5SAndreas Gohr */
355d15166e5SAndreas Gohrfunction css_filetypes(){
356d15166e5SAndreas Gohr
357d15166e5SAndreas Gohr    // default style
358035e07f1SKate Arzamastseva    echo '.mediafile {';
359d15166e5SAndreas Gohr    echo ' background: transparent url('.DOKU_BASE.'lib/images/fileicons/file.png) 0px 1px no-repeat;';
3605b77caf4SAndreas Gohr    echo ' padding-left: 18px;';
3615b77caf4SAndreas Gohr    echo ' padding-bottom: 1px;';
362d15166e5SAndreas Gohr    echo '}';
363d15166e5SAndreas Gohr
364d15166e5SAndreas Gohr    // additional styles when icon available
36527bf7924STom N Harris    // scan directory for all icons
36627bf7924STom N Harris    $exts = array();
36727bf7924STom N Harris    if($dh = opendir(DOKU_INC.'lib/images/fileicons')){
36827bf7924STom N Harris        while(false !== ($file = readdir($dh))){
36927bf7924STom N Harris            if(preg_match('/([_\-a-z0-9]+(?:\.[_\-a-z0-9]+)*?)\.(png|gif)/i',$file,$match)){
37027bf7924STom N Harris                $ext = strtolower($match[1]);
37127bf7924STom N Harris                $type = '.'.strtolower($match[2]);
37227bf7924STom N Harris                if($ext!='file' && (!isset($exts[$ext]) || $type=='.png')){
37327bf7924STom N Harris                    $exts[$ext] = $type;
374d15166e5SAndreas Gohr                }
375d15166e5SAndreas Gohr            }
3761c2d1019SAndreas Gohr        }
37727bf7924STom N Harris        closedir($dh);
37827bf7924STom N Harris    }
37927bf7924STom N Harris    foreach($exts as $ext=>$type){
38027bf7924STom N Harris        $class = preg_replace('/[^_\-a-z0-9]+/','_',$ext);
381035e07f1SKate Arzamastseva        echo ".mf_$class {";
38227bf7924STom N Harris        echo '  background-image: url('.DOKU_BASE.'lib/images/fileicons/'.$ext.$type.')';
38327bf7924STom N Harris        echo '}';
38427bf7924STom N Harris    }
38527bf7924STom N Harris}
3861c2d1019SAndreas Gohr
3871c2d1019SAndreas Gohr/**
38878a6aeb1SAndreas Gohr * Loads a given file and fixes relative URLs with the
38978a6aeb1SAndreas Gohr * given location prefix
390253d4b48SGerrit Uitslag *
391253d4b48SGerrit Uitslag * @param string $file file system path
392253d4b48SGerrit Uitslag * @param string $location
393253d4b48SGerrit Uitslag * @return string
39478a6aeb1SAndreas Gohr */
39578a6aeb1SAndreas Gohrfunction css_loadfile($file,$location=''){
39612ffbbc3SChristopher Smith    $css_file = new DokuCssFile($file);
39712ffbbc3SChristopher Smith    return $css_file->load($location);
39812ffbbc3SChristopher Smith}
39912ffbbc3SChristopher Smith
4001e2c5948SChristopher Smith/**
4011e2c5948SChristopher Smith *  Helper class to abstract loading of css/less files
4021e2c5948SChristopher Smith *
4031e2c5948SChristopher Smith *  @author Chris Smith <chris@jalakai.co.uk>
4041e2c5948SChristopher Smith */
40512ffbbc3SChristopher Smithclass DokuCssFile {
40612ffbbc3SChristopher Smith
4071e2c5948SChristopher Smith    protected $filepath;             // file system path to the CSS/Less file
4081e2c5948SChristopher Smith    protected $location;             // base url location of the CSS/Less file
409fd18b5f4SGerrit Uitslag    protected $relative_path = null;
41012ffbbc3SChristopher Smith
41112ffbbc3SChristopher Smith    public function __construct($file) {
41212ffbbc3SChristopher Smith        $this->filepath = $file;
41312ffbbc3SChristopher Smith    }
41412ffbbc3SChristopher Smith
4151e2c5948SChristopher Smith    /**
4161e2c5948SChristopher Smith     * Load the contents of the css/less file and adjust any relative paths/urls (relative to this file) to be
4171e2c5948SChristopher Smith     * relative to the dokuwiki root: the web root (DOKU_BASE) for most files; the file system root (DOKU_INC)
4181e2c5948SChristopher Smith     * for less files.
4191e2c5948SChristopher Smith     *
4201e2c5948SChristopher Smith     * @param   string   $location   base url for this file
4211e2c5948SChristopher Smith     * @return  string               the CSS/Less contents of the file
4221e2c5948SChristopher Smith     */
42312ffbbc3SChristopher Smith    public function load($location='') {
42479e79377SAndreas Gohr        if (!file_exists($this->filepath)) return '';
42512ffbbc3SChristopher Smith
42612ffbbc3SChristopher Smith        $css = io_readFile($this->filepath);
42778a6aeb1SAndreas Gohr        if (!$location) return $css;
42878a6aeb1SAndreas Gohr
42912ffbbc3SChristopher Smith        $this->location = $location;
430de737055SChristopher Smith
43112ffbbc3SChristopher Smith        $css = preg_replace_callback('#(url\( *)([\'"]?)(.*?)(\2)( *\))#',array($this,'replacements'),$css);
43212ffbbc3SChristopher Smith        $css = preg_replace_callback('#(@import\s+)([\'"])(.*?)(\2)#',array($this,'replacements'),$css);
433809d3ba5SAndreas Gohr
43478a6aeb1SAndreas Gohr        return $css;
43578a6aeb1SAndreas Gohr    }
43678a6aeb1SAndreas Gohr
4371e2c5948SChristopher Smith    /**
4381e2c5948SChristopher Smith     * Get the relative file system path of this file, relative to dokuwiki's root folder, DOKU_INC
4391e2c5948SChristopher Smith     *
4401e2c5948SChristopher Smith     * @return string   relative file system path
4411e2c5948SChristopher Smith     */
442fd18b5f4SGerrit Uitslag    protected function getRelativePath(){
44312ffbbc3SChristopher Smith
44412ffbbc3SChristopher Smith        if (is_null($this->relative_path)) {
44512ffbbc3SChristopher Smith            $basedir = array(DOKU_INC);
4461e2c5948SChristopher Smith
4471e2c5948SChristopher Smith            // during testing, files may be found relative to a second base dir, TMP_DIR
44812ffbbc3SChristopher Smith            if (defined('DOKU_UNITTEST')) {
44912ffbbc3SChristopher Smith                $basedir[] = realpath(TMP_DIR);
45012ffbbc3SChristopher Smith            }
45112ffbbc3SChristopher Smith
45273f25ac0SAndreas Gohr            $basedir = array_map('preg_quote_cb', $basedir);
45373f25ac0SAndreas Gohr            $regex = '/^('.join('|',$basedir).')/';
45412ffbbc3SChristopher Smith            $this->relative_path = preg_replace($regex, '', dirname($this->filepath));
45512ffbbc3SChristopher Smith        }
45612ffbbc3SChristopher Smith
45712ffbbc3SChristopher Smith        return $this->relative_path;
45812ffbbc3SChristopher Smith    }
45912ffbbc3SChristopher Smith
4601e2c5948SChristopher Smith    /**
4611e2c5948SChristopher Smith     * preg_replace callback to adjust relative urls from relative to this file to relative
4621e2c5948SChristopher Smith     * to the appropriate dokuwiki root location as described in the code
4631e2c5948SChristopher Smith     *
4641e2c5948SChristopher Smith     * @param  array    see http://php.net/preg_replace_callback
4651e2c5948SChristopher Smith     * @return string   see http://php.net/preg_replace_callback
4661e2c5948SChristopher Smith     */
46712ffbbc3SChristopher Smith    public function replacements($match) {
468de737055SChristopher Smith
4691e2c5948SChristopher Smith        // not a relative url? - no adjustment required
470de737055SChristopher Smith        if (preg_match('#^(/|data:|https?://)#',$match[3])) {
471de737055SChristopher Smith            return $match[0];
472de737055SChristopher Smith        }
4731e2c5948SChristopher Smith        // a less file import? - requires a file system location
474de737055SChristopher Smith        else if (substr($match[3],-5) == '.less') {
475de737055SChristopher Smith            if ($match[3]{0} != '/') {
47612ffbbc3SChristopher Smith                $match[3] = $this->getRelativePath() . '/' . $match[3];
477de737055SChristopher Smith            }
478de737055SChristopher Smith        }
4791e2c5948SChristopher Smith        // everything else requires a url adjustment
480de737055SChristopher Smith        else {
48112ffbbc3SChristopher Smith            $match[3] = $this->location . $match[3];
482de737055SChristopher Smith        }
483de737055SChristopher Smith
484de737055SChristopher Smith        return join('',array_slice($match,1));
485de737055SChristopher Smith    }
48612ffbbc3SChristopher Smith}
487de737055SChristopher Smith
488809d3ba5SAndreas Gohr/**
4894eb5f931SChristopher Smith * Convert local image URLs to data URLs if the filesize is small
490809d3ba5SAndreas Gohr *
491809d3ba5SAndreas Gohr * Callback for preg_replace_callback
492253d4b48SGerrit Uitslag *
493253d4b48SGerrit Uitslag * @param array $match
494253d4b48SGerrit Uitslag * @return string
495809d3ba5SAndreas Gohr */
496809d3ba5SAndreas Gohrfunction css_datauri($match){
49728f4004cSAndreas Gohr    global $conf;
49828f4004cSAndreas Gohr
499809d3ba5SAndreas Gohr    $pre   = unslash($match[1]);
500809d3ba5SAndreas Gohr    $base  = unslash($match[2]);
501809d3ba5SAndreas Gohr    $url   = unslash($match[3]);
502809d3ba5SAndreas Gohr    $ext   = unslash($match[4]);
503809d3ba5SAndreas Gohr
504809d3ba5SAndreas Gohr    $local = DOKU_INC.$url;
505809d3ba5SAndreas Gohr    $size  = @filesize($local);
50628f4004cSAndreas Gohr    if($size && $size < $conf['cssdatauri']){
507809d3ba5SAndreas Gohr        $data = base64_encode(file_get_contents($local));
508809d3ba5SAndreas Gohr    }
5098f34cf3dSMichael Große    if (!empty($data)){
5106a5d6817SAnika Henke        $url = 'data:image/'.$ext.';base64,'.$data;
511809d3ba5SAndreas Gohr    }else{
512809d3ba5SAndreas Gohr        $url = $base.$url;
513809d3ba5SAndreas Gohr    }
514809d3ba5SAndreas Gohr    return $pre.$url;
515809d3ba5SAndreas Gohr}
516809d3ba5SAndreas Gohr
51715c394afSAndreas Gohr
51878a6aeb1SAndreas Gohr/**
51978a6aeb1SAndreas Gohr * Returns a list of possible Plugin Styles (no existance check here)
52078a6aeb1SAndreas Gohr *
52178a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
522253d4b48SGerrit Uitslag *
523253d4b48SGerrit Uitslag * @param string $mediatype
524253d4b48SGerrit Uitslag * @return array
52578a6aeb1SAndreas Gohr */
526318cd03eSAnika Henkefunction css_pluginstyles($mediatype='screen'){
52778a6aeb1SAndreas Gohr    $list = array();
52878a6aeb1SAndreas Gohr    $plugins = plugin_list();
52978a6aeb1SAndreas Gohr    foreach ($plugins as $p){
530318cd03eSAnika Henke        $list[DOKU_PLUGIN."$p/$mediatype.css"]  = DOKU_BASE."lib/plugins/$p/";
531d4a1ece8SAndreas Gohr        $list[DOKU_PLUGIN."$p/$mediatype.less"]  = DOKU_BASE."lib/plugins/$p/";
532318cd03eSAnika Henke        // alternative for screen.css
533318cd03eSAnika Henke        if ($mediatype=='screen') {
53478a6aeb1SAndreas Gohr            $list[DOKU_PLUGIN."$p/style.css"]  = DOKU_BASE."lib/plugins/$p/";
535d4a1ece8SAndreas Gohr            $list[DOKU_PLUGIN."$p/style.less"]  = DOKU_BASE."lib/plugins/$p/";
53678a6aeb1SAndreas Gohr        }
53778a6aeb1SAndreas Gohr    }
53878a6aeb1SAndreas Gohr    return $list;
53978a6aeb1SAndreas Gohr}
54078a6aeb1SAndreas Gohr
54178a6aeb1SAndreas Gohr/**
54278a6aeb1SAndreas Gohr * Very simple CSS optimizer
54378a6aeb1SAndreas Gohr *
54478a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
545253d4b48SGerrit Uitslag *
546253d4b48SGerrit Uitslag * @param string $css
547253d4b48SGerrit Uitslag * @return string
54878a6aeb1SAndreas Gohr */
54978a6aeb1SAndreas Gohrfunction css_compress($css){
550fd7c2db0SAndreas Gohr    //strip comments through a callback
551fd7c2db0SAndreas Gohr    $css = preg_replace_callback('#(/\*)(.*?)(\*/)#s','css_comment_cb',$css);
552fd7c2db0SAndreas Gohr
553247c1c5dSAndreas Gohr    //strip (incorrect but common) one line comments
554fe5a50c3SAndreas Gohr    $css = preg_replace_callback('/^.*\/\/.*$/m','css_onelinecomment_cb',$css);
555247c1c5dSAndreas Gohr
55678a6aeb1SAndreas Gohr    // strip whitespaces
55778a6aeb1SAndreas Gohr    $css = preg_replace('![\r\n\t ]+!',' ',$css);
558f5379589SChristopher Smith    $css = preg_replace('/ ?([;,{}\/]) ?/','\\1',$css);
559f5379589SChristopher Smith    $css = preg_replace('/ ?: /',':',$css);
56078a6aeb1SAndreas Gohr
561205907a7Sfurun    // number compression
56264159a61SAndreas Gohr    $css = preg_replace(
56364159a61SAndreas Gohr        '/([: ])0+(\.\d+?)0*((?:pt|pc|in|mm|cm|em|ex|px)\b|%)(?=[^\{]*[;\}])/',
56464159a61SAndreas Gohr        '$1$2$3',
56564159a61SAndreas Gohr        $css
56664159a61SAndreas Gohr    ); // "0.1em" to ".1em", "1.10em" to "1.1em"
56764159a61SAndreas Gohr    $css = preg_replace(
56864159a61SAndreas Gohr        '/([: ])\.(0)+((?:pt|pc|in|mm|cm|em|ex|px)\b|%)(?=[^\{]*[;\}])/',
56964159a61SAndreas Gohr        '$1$2',
57064159a61SAndreas Gohr        $css
57164159a61SAndreas Gohr    ); // ".0em" to "0"
57264159a61SAndreas Gohr    $css = preg_replace(
57364159a61SAndreas Gohr        '/([: ]0)0*(\.0*)?((?:pt|pc|in|mm|cm|em|ex|px)(?=[^\{]*[;\}])\b|%)/',
57464159a61SAndreas Gohr        '$1',
57564159a61SAndreas Gohr        $css
57664159a61SAndreas Gohr    ); // "0.0em" to "0"
57764159a61SAndreas Gohr    $css = preg_replace(
57864159a61SAndreas Gohr        '/([: ]\d+)(\.0*)((?:pt|pc|in|mm|cm|em|ex|px)(?=[^\{]*[;\}])\b|%)/',
57964159a61SAndreas Gohr        '$1$3',
58064159a61SAndreas Gohr        $css
58164159a61SAndreas Gohr    ); // "1.0em" to "1em"
58264159a61SAndreas Gohr    $css = preg_replace(
58364159a61SAndreas Gohr        '/([: ])0+(\d+|\d*\.\d+)((?:pt|pc|in|mm|cm|em|ex|px)(?=[^\{]*[;\}])\b|%)/',
58464159a61SAndreas Gohr        '$1$2$3',
58564159a61SAndreas Gohr        $css
58664159a61SAndreas Gohr    ); // "001em" to "1em"
587205907a7Sfurun
588205907a7Sfurun    // shorten attributes (1em 1em 1em 1em -> 1em)
58964159a61SAndreas Gohr    $css = preg_replace(
59064159a61SAndreas Gohr        '/(?<![\w\-])((?:margin|padding|border|border-(?:width|radius)):)([\w\.]+)( \2)+(?=[;\}]| !)/',
59164159a61SAndreas Gohr        '$1$2',
59264159a61SAndreas Gohr        $css
59364159a61SAndreas Gohr    ); // "1em 1em 1em 1em" to "1em"
59464159a61SAndreas Gohr    $css = preg_replace(
59564159a61SAndreas Gohr        '/(?<![\w\-])((?:margin|padding|border|border-(?:width)):)([\w\.]+) ([\w\.]+) \2 \3(?=[;\}]| !)/',
59664159a61SAndreas Gohr        '$1$2 $3',
59764159a61SAndreas Gohr        $css
59864159a61SAndreas Gohr    ); // "1em 2em 1em 2em" to "1em 2em"
599205907a7Sfurun
60078a6aeb1SAndreas Gohr    // shorten colors
60164159a61SAndreas Gohr    $css = preg_replace(
60264159a61SAndreas Gohr        "/#([0-9a-fA-F]{1})\\1([0-9a-fA-F]{1})\\2([0-9a-fA-F]{1})\\3(?=[^\{]*[;\}])/",
60364159a61SAndreas Gohr        "#\\1\\2\\3",
60464159a61SAndreas Gohr        $css
60564159a61SAndreas Gohr    );
60678a6aeb1SAndreas Gohr
60778a6aeb1SAndreas Gohr    return $css;
60878a6aeb1SAndreas Gohr}
60978a6aeb1SAndreas Gohr
610c00aef76SAndreas Gohr/**
611c00aef76SAndreas Gohr * Callback for css_compress()
612c00aef76SAndreas Gohr *
613c00aef76SAndreas Gohr * Keeps short comments (< 5 chars) to maintain typical browser hacks
614c00aef76SAndreas Gohr *
615c00aef76SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
616253d4b48SGerrit Uitslag *
617253d4b48SGerrit Uitslag * @param array $matches
618253d4b48SGerrit Uitslag * @return string
619c00aef76SAndreas Gohr */
620c00aef76SAndreas Gohrfunction css_comment_cb($matches){
621c00aef76SAndreas Gohr    if(strlen($matches[2]) > 4) return '';
622c00aef76SAndreas Gohr    return $matches[0];
623c00aef76SAndreas Gohr}
62478a6aeb1SAndreas Gohr
625fe5a50c3SAndreas Gohr/**
626fe5a50c3SAndreas Gohr * Callback for css_compress()
627fe5a50c3SAndreas Gohr *
628fe5a50c3SAndreas Gohr * Strips one line comments but makes sure it will not destroy url() constructs with slashes
629fe5a50c3SAndreas Gohr *
630253d4b48SGerrit Uitslag * @param array $matches
631fe5a50c3SAndreas Gohr * @return string
632fe5a50c3SAndreas Gohr */
633fe5a50c3SAndreas Gohrfunction css_onelinecomment_cb($matches) {
634fe5a50c3SAndreas Gohr    $line = $matches[0];
635fe5a50c3SAndreas Gohr
636fe5a50c3SAndreas Gohr    $i = 0;
637fe5a50c3SAndreas Gohr    $len = strlen($line);
638918a4468SAndreas Gohr
639fe5a50c3SAndreas Gohr    while ($i< $len){
640fe5a50c3SAndreas Gohr        $nextcom = strpos($line, '//', $i);
641fe5a50c3SAndreas Gohr        $nexturl = stripos($line, 'url(', $i);
642fe5a50c3SAndreas Gohr
643fe5a50c3SAndreas Gohr        if($nextcom === false) {
644fe5a50c3SAndreas Gohr            // no more comments, we're done
645918a4468SAndreas Gohr            $i = $len;
646fe5a50c3SAndreas Gohr            break;
647fe5a50c3SAndreas Gohr        }
648fe5a50c3SAndreas Gohr
649918a4468SAndreas Gohr        // keep any quoted string that starts before a comment
650918a4468SAndreas Gohr        $nextsqt = strpos($line, "'", $i);
651918a4468SAndreas Gohr        $nextdqt = strpos($line, '"', $i);
652918a4468SAndreas Gohr        if(min($nextsqt, $nextdqt) < $nextcom) {
653918a4468SAndreas Gohr            $skipto = false;
654918a4468SAndreas Gohr            if($nextsqt !== false && ($nextdqt === false || $nextsqt < $nextdqt)) {
655918a4468SAndreas Gohr                $skipto = strpos($line, "'", $nextsqt+1) +1;
656918a4468SAndreas Gohr            } else if ($nextdqt !== false) {
657918a4468SAndreas Gohr                $skipto = strpos($line, '"', $nextdqt+1) +1;
658918a4468SAndreas Gohr            }
659918a4468SAndreas Gohr
660918a4468SAndreas Gohr            if($skipto !== false) {
661918a4468SAndreas Gohr                $i = $skipto;
662918a4468SAndreas Gohr                continue;
663918a4468SAndreas Gohr            }
664918a4468SAndreas Gohr        }
665918a4468SAndreas Gohr
666918a4468SAndreas Gohr        if($nexturl === false || $nextcom < $nexturl) {
667918a4468SAndreas Gohr            // no url anymore, strip comment and be done
668918a4468SAndreas Gohr            $i = $nextcom;
669918a4468SAndreas Gohr            break;
670918a4468SAndreas Gohr        }
671918a4468SAndreas Gohr
672918a4468SAndreas Gohr        // we have an upcoming url
673918a4468SAndreas Gohr        $i = strpos($line, ')', $nexturl);
674918a4468SAndreas Gohr    }
675918a4468SAndreas Gohr
676918a4468SAndreas Gohr    return substr($line, 0, $i);
677fe5a50c3SAndreas Gohr}
678fe5a50c3SAndreas Gohr
679e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
680