xref: /dokuwiki/lib/exe/css.php (revision 615960fefc1892e98e1e20725b11d535324f8c34)
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
978a6aeb1SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
101c2d1019SAndreas Gohrif(!defined('NOSESSION')) define('NOSESSION',true); // we do not use a session or authentication here (better caching)
1178a6aeb1SAndreas Gohrrequire_once(DOKU_INC.'inc/init.php');
1278a6aeb1SAndreas Gohrrequire_once(DOKU_INC.'inc/pageutils.php');
1378a6aeb1SAndreas Gohrrequire_once(DOKU_INC.'inc/io.php');
141c2d1019SAndreas Gohrrequire_once(DOKU_INC.'inc/confutils.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;
33*615960feSTom N Harris    switch ($_REQUEST['s']) {
34*615960feSTom N Harris        case 'print':
35*615960feSTom N Harris        case 'feed':
36*615960feSTom N Harris            $style = $_REQUEST['s'];
37*615960feSTom N Harris        break;
38*615960feSTom N Harris        default:
39*615960feSTom N Harris            $style = '';
40*615960feSTom N Harris        break;
41*615960feSTom N Harris    }
4278a6aeb1SAndreas Gohr
4378a6aeb1SAndreas Gohr    // The generated script depends on some dynamic options
444ad141d6SMichael Klier chi@chimeric.de    $cache = getCacheName('styles'.$conf['template'].$print,'.css');
4578a6aeb1SAndreas Gohr
46519b3173SAndreas Gohr    // load template styles
47519b3173SAndreas Gohr    $tplstyles = array();
48519b3173SAndreas Gohr    if(@file_exists(DOKU_TPLINC.'style.ini')){
49519b3173SAndreas Gohr        $ini = parse_ini_file(DOKU_TPLINC.'style.ini',true);
50519b3173SAndreas Gohr        foreach($ini['stylesheets'] as $file => $mode){
51519b3173SAndreas Gohr            $tplstyles[$mode][DOKU_TPLINC.$file] = DOKU_TPL;
52519b3173SAndreas Gohr        }
53519b3173SAndreas Gohr    }
54519b3173SAndreas Gohr
5578a6aeb1SAndreas Gohr    // Array of needed files and their web locations, the latter ones
5678a6aeb1SAndreas Gohr    // are needed to fix relative paths in the stylesheets
5778a6aeb1SAndreas Gohr    $files   = array();
58ea40e5efSmtbrains    if (isset($tplstyles['all'])) $files = array_merge($files, $tplstyles['all']);
59*615960feSTom N Harris    if(!empty($style)){
60*615960feSTom N Harris        $files[DOKU_INC.'lib/styles/'.$style.'.css'] = DOKU_BASE.'lib/styles/';
615b77caf4SAndreas Gohr        // load plugin, template, user styles
62*615960feSTom N Harris        $files = array_merge($files, css_pluginstyles($style));
63*615960feSTom N Harris        if (isset($tplstyles[$style])) $files = array_merge($files, $tplstyles[$style]);
64*615960feSTom N Harris        $files[DOKU_CONF.'user'.$style.'.css'] = '';
6578a6aeb1SAndreas Gohr    }else{
6678a6aeb1SAndreas Gohr        $files[DOKU_INC.'lib/styles/style.css'] = DOKU_BASE.'lib/styles/';
677c96e92fSAndreas Gohr        if($conf['spellchecker']){
687c96e92fSAndreas Gohr            $files[DOKU_INC.'lib/styles/spellcheck.css'] = DOKU_BASE.'lib/styles/';
697c96e92fSAndreas Gohr        }
705b77caf4SAndreas Gohr        // load plugin, template, user styles
715b77caf4SAndreas Gohr        $files = array_merge($files, css_pluginstyles('screen'));
721f5663fdSchris        if (isset($tplstyles['screen'])) $files = array_merge($files, $tplstyles['screen']);
7378a6aeb1SAndreas Gohr        if($lang['direction'] == 'rtl'){
741f5663fdSchris            if (isset($tplstyles['rtl'])) $files = array_merge($files, $tplstyles['rtl']);
7578a6aeb1SAndreas Gohr        }
7678a6aeb1SAndreas Gohr        $files[DOKU_CONF.'userstyle.css'] = '';
7778a6aeb1SAndreas Gohr    }
7878a6aeb1SAndreas Gohr
7938f56bffSBen Coburn    // check cache age & handle conditional request
8007525e80SBen Coburn    header('Cache-Control: public, max-age=3600');
8138f56bffSBen Coburn    header('Pragma: public');
8278a6aeb1SAndreas Gohr    if(css_cacheok($cache,array_keys($files))){
8338f56bffSBen Coburn        http_conditionalRequest(filemtime($cache));
84cf6894dfSAndreas Gohr        if($conf['allowdebug']) header("X-CacheUsed: $cache");
8578a6aeb1SAndreas Gohr        readfile($cache);
8678a6aeb1SAndreas Gohr        return;
8738f56bffSBen Coburn    } else {
8838f56bffSBen Coburn        http_conditionalRequest(time());
8978a6aeb1SAndreas Gohr    }
9078a6aeb1SAndreas Gohr
9178a6aeb1SAndreas Gohr    // start output buffering and build the stylesheet
9278a6aeb1SAndreas Gohr    ob_start();
9378a6aeb1SAndreas Gohr
94d15166e5SAndreas Gohr    // print the default classes for interwiki links and file downloads
951c2d1019SAndreas Gohr    css_interwiki();
96d15166e5SAndreas Gohr    css_filetypes();
971c2d1019SAndreas Gohr
9878a6aeb1SAndreas Gohr    // load files
9978a6aeb1SAndreas Gohr    foreach($files as $file => $location){
10078a6aeb1SAndreas Gohr        print css_loadfile($file, $location);
10178a6aeb1SAndreas Gohr    }
10278a6aeb1SAndreas Gohr
10378a6aeb1SAndreas Gohr    // end output buffering and get contents
10478a6aeb1SAndreas Gohr    $css = ob_get_contents();
10578a6aeb1SAndreas Gohr    ob_end_clean();
10678a6aeb1SAndreas Gohr
1076e69c1baSAndreas Gohr    // apply style replacements
1086e69c1baSAndreas Gohr    $css = css_applystyle($css);
1096e69c1baSAndreas Gohr
11078a6aeb1SAndreas Gohr    // compress whitespace and comments
11178a6aeb1SAndreas Gohr    if($conf['compress']){
11278a6aeb1SAndreas Gohr        $css = css_compress($css);
11378a6aeb1SAndreas Gohr    }
11478a6aeb1SAndreas Gohr
11578a6aeb1SAndreas Gohr    // save cache file
11678a6aeb1SAndreas Gohr    io_saveFile($cache,$css);
11778a6aeb1SAndreas Gohr
11878a6aeb1SAndreas Gohr    // finally send output
11978a6aeb1SAndreas Gohr    print $css;
12078a6aeb1SAndreas Gohr}
12178a6aeb1SAndreas Gohr
12278a6aeb1SAndreas Gohr/**
12378a6aeb1SAndreas Gohr * Checks if a CSS Cache file still is valid
12478a6aeb1SAndreas Gohr *
12578a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
12678a6aeb1SAndreas Gohr */
12778a6aeb1SAndreas Gohrfunction css_cacheok($cache,$files){
1280df6f150SAndreas Gohr    if($_REQUEST['purge']) return false; //support purge request
1290df6f150SAndreas Gohr
13078a6aeb1SAndreas Gohr    $ctime = @filemtime($cache);
13178a6aeb1SAndreas Gohr    if(!$ctime) return false; //There is no cache
13278a6aeb1SAndreas Gohr
13378a6aeb1SAndreas Gohr    // some additional files to check
134c591aabeSAndreas Gohr    $files[] = DOKU_CONF.'dokuwiki.php';
135c591aabeSAndreas Gohr    $files[] = DOKU_CONF.'local.php';
1366e69c1baSAndreas Gohr    $files[] = DOKU_TPLINC.'style.ini';
13778a6aeb1SAndreas Gohr    $files[] = __FILE__;
13878a6aeb1SAndreas Gohr
13978a6aeb1SAndreas Gohr    // now walk the files
14078a6aeb1SAndreas Gohr    foreach($files as $file){
14178a6aeb1SAndreas Gohr        if(@filemtime($file) > $ctime){
14278a6aeb1SAndreas Gohr            return false;
14378a6aeb1SAndreas Gohr        }
14478a6aeb1SAndreas Gohr    }
14578a6aeb1SAndreas Gohr    return true;
14678a6aeb1SAndreas Gohr}
14778a6aeb1SAndreas Gohr
14878a6aeb1SAndreas Gohr/**
1496e69c1baSAndreas Gohr * Does placeholder replacements in the style according to
1506e69c1baSAndreas Gohr * the ones defined in a templates style.ini file
1516e69c1baSAndreas Gohr *
1526e69c1baSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
1536e69c1baSAndreas Gohr */
1546e69c1baSAndreas Gohrfunction css_applystyle($css){
1556e69c1baSAndreas Gohr    if(@file_exists(DOKU_TPLINC.'style.ini')){
156519b3173SAndreas Gohr        $ini = parse_ini_file(DOKU_TPLINC.'style.ini',true);
157519b3173SAndreas Gohr        $css = strtr($css,$ini['replacements']);
1586e69c1baSAndreas Gohr    }
1596e69c1baSAndreas Gohr    return $css;
1606e69c1baSAndreas Gohr}
1616e69c1baSAndreas Gohr
1626e69c1baSAndreas Gohr/**
1631c2d1019SAndreas Gohr * Prints classes for interwikilinks
1641c2d1019SAndreas Gohr *
1651c2d1019SAndreas Gohr * Interwiki links have two classes: 'interwiki' and 'iw_$name>' where
1661c2d1019SAndreas Gohr * $name is the identifier given in the config. All Interwiki links get
1671c2d1019SAndreas Gohr * an default style with a default icon. If a special icon is available
1681c2d1019SAndreas Gohr * for an interwiki URL it is set in it's own class. Both classes can be
1691c2d1019SAndreas Gohr * overwritten in the template or userstyles.
1701c2d1019SAndreas Gohr *
1711c2d1019SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
1721c2d1019SAndreas Gohr */
1731c2d1019SAndreas Gohrfunction css_interwiki(){
1741c2d1019SAndreas Gohr
1751c2d1019SAndreas Gohr    // default style
1761c2d1019SAndreas Gohr    echo 'a.interwiki {';
1771c2d1019SAndreas Gohr    echo ' background: transparent url('.DOKU_BASE.'lib/images/interwiki.png) 0px 1px no-repeat;';
1781c2d1019SAndreas Gohr    echo ' padding-left: 16px;';
1791c2d1019SAndreas Gohr    echo '}';
1801c2d1019SAndreas Gohr
1811c2d1019SAndreas Gohr    // additional styles when icon available
1821c2d1019SAndreas Gohr    $iwlinks = getInterwiki();
1831c2d1019SAndreas Gohr    foreach(array_keys($iwlinks) as $iw){
1849d2ddea4SAndreas Gohr        $class = preg_replace('/[^_\-a-z0-9]+/i','_',$iw);
1851c2d1019SAndreas Gohr        if(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.png')){
1869d2ddea4SAndreas Gohr            echo "a.iw_$class {";
1871c2d1019SAndreas Gohr            echo '  background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.png)';
1881c2d1019SAndreas Gohr            echo '}';
1891c2d1019SAndreas Gohr        }elseif(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.gif')){
1909d2ddea4SAndreas Gohr            echo "a.iw_$class {";
1911c2d1019SAndreas Gohr            echo '  background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.gif)';
1921c2d1019SAndreas Gohr            echo '}';
1931c2d1019SAndreas Gohr        }
1941c2d1019SAndreas Gohr    }
195d15166e5SAndreas Gohr}
1961c2d1019SAndreas Gohr
197d15166e5SAndreas Gohr/**
198d15166e5SAndreas Gohr * Prints classes for file download links
199d15166e5SAndreas Gohr *
200d15166e5SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
201d15166e5SAndreas Gohr */
202d15166e5SAndreas Gohrfunction css_filetypes(){
203d15166e5SAndreas Gohr
204d15166e5SAndreas Gohr    // default style
205d15166e5SAndreas Gohr    echo 'a.mediafile {';
206d15166e5SAndreas Gohr    echo ' background: transparent url('.DOKU_BASE.'lib/images/fileicons/file.png) 0px 1px no-repeat;';
2075b77caf4SAndreas Gohr    echo ' padding-left: 18px;';
2085b77caf4SAndreas Gohr    echo ' padding-bottom: 1px;';
209d15166e5SAndreas Gohr    echo '}';
210d15166e5SAndreas Gohr
211d15166e5SAndreas Gohr    // additional styles when icon available
212d15166e5SAndreas Gohr    $mimes = getMimeTypes();
213d15166e5SAndreas Gohr    foreach(array_keys($mimes) as $mime){
2149d2ddea4SAndreas Gohr        $class = preg_replace('/[^_\-a-z0-9]+/i','_',$mime);
215d15166e5SAndreas Gohr        if(@file_exists(DOKU_INC.'lib/images/fileicons/'.$mime.'.png')){
2169d2ddea4SAndreas Gohr            echo "a.mf_$class {";
217d15166e5SAndreas Gohr            echo '  background-image: url('.DOKU_BASE.'lib/images/fileicons/'.$mime.'.png)';
218d15166e5SAndreas Gohr            echo '}';
219d15166e5SAndreas Gohr        }elseif(@file_exists(DOKU_INC.'lib/images/fileicons/'.$mime.'.gif')){
2209d2ddea4SAndreas Gohr            echo "a.mf_$class {";
221d15166e5SAndreas Gohr            echo '  background-image: url('.DOKU_BASE.'lib/images/fileicons/'.$mime.'.gif)';
222d15166e5SAndreas Gohr            echo '}';
223d15166e5SAndreas Gohr        }
224d15166e5SAndreas Gohr    }
2251c2d1019SAndreas Gohr}
2261c2d1019SAndreas Gohr
2271c2d1019SAndreas Gohr/**
22878a6aeb1SAndreas Gohr * Loads a given file and fixes relative URLs with the
22978a6aeb1SAndreas Gohr * given location prefix
23078a6aeb1SAndreas Gohr */
23178a6aeb1SAndreas Gohrfunction css_loadfile($file,$location=''){
23278a6aeb1SAndreas Gohr    if(!@file_exists($file)) return '';
23378a6aeb1SAndreas Gohr    $css = io_readFile($file);
23478a6aeb1SAndreas Gohr    if(!$location) return $css;
23578a6aeb1SAndreas Gohr
23615c394afSAndreas Gohr    $css = preg_replace('#(url\([ \'"]*)((?!/|http://|https://| |\'|"))#','\\1'.$location.'\\3',$css);
23778a6aeb1SAndreas Gohr    return $css;
23878a6aeb1SAndreas Gohr}
23978a6aeb1SAndreas Gohr
24015c394afSAndreas Gohr
24178a6aeb1SAndreas Gohr/**
24278a6aeb1SAndreas Gohr * Returns a list of possible Plugin Styles (no existance check here)
24378a6aeb1SAndreas Gohr *
24478a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
24578a6aeb1SAndreas Gohr */
24678a6aeb1SAndreas Gohrfunction css_pluginstyles($mode='screen'){
24778a6aeb1SAndreas Gohr    $list = array();
24878a6aeb1SAndreas Gohr    $plugins = plugin_list();
24978a6aeb1SAndreas Gohr    foreach ($plugins as $p){
25078a6aeb1SAndreas Gohr        if($mode == 'print'){
25178a6aeb1SAndreas Gohr            $list[DOKU_PLUGIN."$p/print.css"]  = DOKU_BASE."lib/plugins/$p/";
252ea40e5efSmtbrains            $list[DOKU_PLUGIN."$p/all.css"]  = DOKU_BASE."lib/plugins/$p/";
253*615960feSTom N Harris        }elseif($mode == 'feed'){
254*615960feSTom N Harris            $list[DOKU_PLUGIN."$p/feed.css"]  = DOKU_BASE."lib/plugins/$p/";
255*615960feSTom N Harris            $list[DOKU_PLUGIN."$p/all.css"]  = DOKU_BASE."lib/plugins/$p/";
25678a6aeb1SAndreas Gohr        }else{
25778a6aeb1SAndreas Gohr            $list[DOKU_PLUGIN."$p/style.css"]  = DOKU_BASE."lib/plugins/$p/";
25878a6aeb1SAndreas Gohr            $list[DOKU_PLUGIN."$p/screen.css"] = DOKU_BASE."lib/plugins/$p/";
259ea40e5efSmtbrains            $list[DOKU_PLUGIN."$p/all.css"]  = DOKU_BASE."lib/plugins/$p/";
26078a6aeb1SAndreas Gohr        }
26178a6aeb1SAndreas Gohr    }
26278a6aeb1SAndreas Gohr    return $list;
26378a6aeb1SAndreas Gohr}
26478a6aeb1SAndreas Gohr
26578a6aeb1SAndreas Gohr/**
26678a6aeb1SAndreas Gohr * Very simple CSS optimizer
26778a6aeb1SAndreas Gohr *
26878a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
26978a6aeb1SAndreas Gohr */
27078a6aeb1SAndreas Gohrfunction css_compress($css){
271fd7c2db0SAndreas Gohr    //strip comments through a callback
272fd7c2db0SAndreas Gohr    $css = preg_replace_callback('#(/\*)(.*?)(\*/)#s','css_comment_cb',$css);
273fd7c2db0SAndreas Gohr
274247c1c5dSAndreas Gohr    //strip (incorrect but common) one line comments
275fd7c2db0SAndreas Gohr    $css = preg_replace('/(?<!:)\/\/.*$/m','',$css);
276247c1c5dSAndreas Gohr
27778a6aeb1SAndreas Gohr    // strip whitespaces
27878a6aeb1SAndreas Gohr    $css = preg_replace('![\r\n\t ]+!',' ',$css);
2795646f690SAndreas Gohr    $css = preg_replace('/ ?([:;,{}\/]) ?/','\\1',$css);
28078a6aeb1SAndreas Gohr
28178a6aeb1SAndreas Gohr    // shorten colors
28278a6aeb1SAndreas Gohr    $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);
28378a6aeb1SAndreas Gohr
28478a6aeb1SAndreas Gohr    return $css;
28578a6aeb1SAndreas Gohr}
28678a6aeb1SAndreas Gohr
287c00aef76SAndreas Gohr/**
288c00aef76SAndreas Gohr * Callback for css_compress()
289c00aef76SAndreas Gohr *
290c00aef76SAndreas Gohr * Keeps short comments (< 5 chars) to maintain typical browser hacks
291c00aef76SAndreas Gohr *
292c00aef76SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
293c00aef76SAndreas Gohr */
294c00aef76SAndreas Gohrfunction css_comment_cb($matches){
295c00aef76SAndreas Gohr    if(strlen($matches[2]) > 4) return '';
296c00aef76SAndreas Gohr    return $matches[0];
297c00aef76SAndreas Gohr}
29878a6aeb1SAndreas Gohr
29978a6aeb1SAndreas Gohr//Setup VIM: ex: et ts=4 enc=utf-8 :
30078a6aeb1SAndreas Gohr?>
301