xref: /dokuwiki/lib/exe/css.php (revision c66972f2cb89e65a8bbf8e39d42e8e479f7eb8dc)
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
1278a6aeb1SAndreas Gohrrequire_once(DOKU_INC.'inc/init.php');
1378a6aeb1SAndreas Gohrrequire_once(DOKU_INC.'inc/pageutils.php');
14758447cfSAndreas Gohrrequire_once(DOKU_INC.'inc/httputils.php');
1578a6aeb1SAndreas Gohrrequire_once(DOKU_INC.'inc/io.php');
161c2d1019SAndreas Gohrrequire_once(DOKU_INC.'inc/confutils.php');
1778a6aeb1SAndreas Gohr
1878a6aeb1SAndreas Gohr// Main (don't run when UNIT test)
1978a6aeb1SAndreas Gohrif(!defined('SIMPLE_TEST')){
2078a6aeb1SAndreas Gohr    header('Content-Type: text/css; charset=utf-8');
2178a6aeb1SAndreas Gohr    css_out();
2278a6aeb1SAndreas Gohr}
2378a6aeb1SAndreas Gohr
2478a6aeb1SAndreas Gohr
2578a6aeb1SAndreas Gohr// ---------------------- functions ------------------------------
2678a6aeb1SAndreas Gohr
2778a6aeb1SAndreas Gohr/**
2878a6aeb1SAndreas Gohr * Output all needed Styles
2978a6aeb1SAndreas Gohr *
3078a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
3178a6aeb1SAndreas Gohr */
3278a6aeb1SAndreas Gohrfunction css_out(){
3378a6aeb1SAndreas Gohr    global $conf;
3478a6aeb1SAndreas Gohr    global $lang;
35615960feSTom N Harris    $style = '';
36*c66972f2SAdrian Lang    if (isset($_REQUEST['s']) &&
37*c66972f2SAdrian Lang        in_array($_REQUEST['s'], array('all', 'print', 'feed'))) {
38*c66972f2SAdrian Lang        $style = $_REQUEST['s'];
39615960feSTom N Harris    }
4078a6aeb1SAndreas Gohr
41f7589b08SChris Smith    $tpl = trim(preg_replace('/[^\w-]+/','',$_REQUEST['t']));
42124af657SAndreas Gohr    if($tpl){
43124af657SAndreas Gohr        $tplinc = DOKU_INC.'lib/tpl/'.$tpl.'/';
44124af657SAndreas Gohr        $tpldir = DOKU_BASE.'lib/tpl/'.$tpl.'/';
45124af657SAndreas Gohr    }else{
46124af657SAndreas Gohr        $tplinc = DOKU_TPLINC;
47124af657SAndreas Gohr        $tpldir = DOKU_TPL;
48124af657SAndreas Gohr    }
49124af657SAndreas Gohr
5078a6aeb1SAndreas Gohr    // The generated script depends on some dynamic options
51e3e6ab3cSGina Haeussge    $cache = getCacheName('styles'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'].DOKU_BASE.$tplinc.$style,'.css');
5278a6aeb1SAndreas Gohr
53519b3173SAndreas Gohr    // load template styles
54519b3173SAndreas Gohr    $tplstyles = array();
55124af657SAndreas Gohr    if(@file_exists($tplinc.'style.ini')){
56124af657SAndreas Gohr        $ini = parse_ini_file($tplinc.'style.ini',true);
57519b3173SAndreas Gohr        foreach($ini['stylesheets'] as $file => $mode){
58124af657SAndreas Gohr            $tplstyles[$mode][$tplinc.$file] = $tpldir;
59519b3173SAndreas Gohr        }
60519b3173SAndreas Gohr    }
61519b3173SAndreas 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
6478a6aeb1SAndreas Gohr    $files   = array();
657aaa4c46Smartin.tschofen    //if (isset($tplstyles['all'])) $files = array_merge($files, $tplstyles['all']);
66615960feSTom N Harris    if(!empty($style)){
67615960feSTom N Harris        $files[DOKU_INC.'lib/styles/'.$style.'.css'] = DOKU_BASE.'lib/styles/';
685b77caf4SAndreas Gohr        // load plugin, template, user styles
69615960feSTom N Harris        $files = array_merge($files, css_pluginstyles($style));
70615960feSTom N Harris        if (isset($tplstyles[$style])) $files = array_merge($files, $tplstyles[$style]);
71c4af4cb6SAndreas Gohr        $files[DOKU_CONF.'user'.$style.'.css'] = DOKU_BASE;
7278a6aeb1SAndreas Gohr    }else{
7378a6aeb1SAndreas Gohr        $files[DOKU_INC.'lib/styles/style.css'] = DOKU_BASE.'lib/styles/';
745b77caf4SAndreas Gohr        // load plugin, template, user styles
755b77caf4SAndreas Gohr        $files = array_merge($files, css_pluginstyles('screen'));
761f5663fdSchris        if (isset($tplstyles['screen'])) $files = array_merge($files, $tplstyles['screen']);
7778a6aeb1SAndreas Gohr        if($lang['direction'] == 'rtl'){
781f5663fdSchris            if (isset($tplstyles['rtl'])) $files = array_merge($files, $tplstyles['rtl']);
7978a6aeb1SAndreas Gohr        }
80c4af4cb6SAndreas Gohr        $files[DOKU_CONF.'userstyle.css'] = DOKU_BASE;
8178a6aeb1SAndreas Gohr    }
8278a6aeb1SAndreas Gohr
8338f56bffSBen Coburn    // check cache age & handle conditional request
8407525e80SBen Coburn    header('Cache-Control: public, max-age=3600');
8538f56bffSBen Coburn    header('Pragma: public');
86124af657SAndreas Gohr    if(css_cacheok($cache,array_keys($files),$tplinc)){
8738f56bffSBen Coburn        http_conditionalRequest(filemtime($cache));
88cf6894dfSAndreas Gohr        if($conf['allowdebug']) header("X-CacheUsed: $cache");
89ca2b464bSChris Smith
90ca2b464bSChris Smith        // finally send output
9198bda4fdSAndreas Gohr        if ($conf['gzip_output'] && http_gzip_valid($cache)) {
92ca2b464bSChris Smith          header('Vary: Accept-Encoding');
93ca2b464bSChris Smith          header('Content-Encoding: gzip');
9498bda4fdSAndreas Gohr          readfile($cache.".gz");
95ca2b464bSChris Smith        } else {
96ca2b464bSChris Smith          if (!http_sendfile($cache)) readfile($cache);
97ca2b464bSChris Smith        }
98ca2b464bSChris Smith
9978a6aeb1SAndreas Gohr        return;
10038f56bffSBen Coburn    } else {
10138f56bffSBen Coburn        http_conditionalRequest(time());
10278a6aeb1SAndreas Gohr    }
10378a6aeb1SAndreas Gohr
10478a6aeb1SAndreas Gohr    // start output buffering and build the stylesheet
10578a6aeb1SAndreas Gohr    ob_start();
10678a6aeb1SAndreas Gohr
107d15166e5SAndreas Gohr    // print the default classes for interwiki links and file downloads
1081c2d1019SAndreas Gohr    css_interwiki();
109d15166e5SAndreas Gohr    css_filetypes();
1101c2d1019SAndreas Gohr
11178a6aeb1SAndreas Gohr    // load files
11278a6aeb1SAndreas Gohr    foreach($files as $file => $location){
11378a6aeb1SAndreas Gohr        print css_loadfile($file, $location);
11478a6aeb1SAndreas Gohr    }
11578a6aeb1SAndreas Gohr
11678a6aeb1SAndreas Gohr    // end output buffering and get contents
11778a6aeb1SAndreas Gohr    $css = ob_get_contents();
11878a6aeb1SAndreas Gohr    ob_end_clean();
11978a6aeb1SAndreas Gohr
1206e69c1baSAndreas Gohr    // apply style replacements
121124af657SAndreas Gohr    $css = css_applystyle($css,$tplinc);
1226e69c1baSAndreas Gohr
12378a6aeb1SAndreas Gohr    // compress whitespace and comments
12478a6aeb1SAndreas Gohr    if($conf['compress']){
12578a6aeb1SAndreas Gohr        $css = css_compress($css);
12678a6aeb1SAndreas Gohr    }
12778a6aeb1SAndreas Gohr
12878a6aeb1SAndreas Gohr    // save cache file
12978a6aeb1SAndreas Gohr    io_saveFile($cache,$css);
130c8317406SAndreas Gohr    if(function_exists('gzopen')) io_saveFile("$cache.gz",$css);
13178a6aeb1SAndreas Gohr
13278a6aeb1SAndreas Gohr    // finally send output
13398bda4fdSAndreas Gohr    if ($conf['gzip_output']) {
134ca2b464bSChris Smith      header('Vary: Accept-Encoding');
135ca2b464bSChris Smith      header('Content-Encoding: gzip');
136ca2b464bSChris Smith      print gzencode($css,9,FORCE_GZIP);
137ca2b464bSChris Smith    } else {
13878a6aeb1SAndreas Gohr      print $css;
13978a6aeb1SAndreas Gohr    }
140ca2b464bSChris Smith}
14178a6aeb1SAndreas Gohr
14278a6aeb1SAndreas Gohr/**
14378a6aeb1SAndreas Gohr * Checks if a CSS Cache file still is valid
14478a6aeb1SAndreas Gohr *
14578a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
14678a6aeb1SAndreas Gohr */
147124af657SAndreas Gohrfunction css_cacheok($cache,$files,$tplinc){
148f8121585SChris Smith    global $config_cascade;
149f8121585SChris Smith
150*c66972f2SAdrian Lang    if(isset($_REQUEST['purge'])) return false; //support purge request
1510df6f150SAndreas Gohr
15278a6aeb1SAndreas Gohr    $ctime = @filemtime($cache);
15378a6aeb1SAndreas Gohr    if(!$ctime) return false; //There is no cache
15478a6aeb1SAndreas Gohr
15578a6aeb1SAndreas Gohr    // some additional files to check
156f8121585SChris Smith    $files = array_merge($files, getConfigFiles('main'));
157124af657SAndreas Gohr    $files[] = $tplinc.'style.ini';
15878a6aeb1SAndreas Gohr    $files[] = __FILE__;
15978a6aeb1SAndreas Gohr
16078a6aeb1SAndreas Gohr    // now walk the files
16178a6aeb1SAndreas Gohr    foreach($files as $file){
16278a6aeb1SAndreas Gohr        if(@filemtime($file) > $ctime){
16378a6aeb1SAndreas Gohr            return false;
16478a6aeb1SAndreas Gohr        }
16578a6aeb1SAndreas Gohr    }
16678a6aeb1SAndreas Gohr    return true;
16778a6aeb1SAndreas Gohr}
16878a6aeb1SAndreas Gohr
16978a6aeb1SAndreas Gohr/**
1706e69c1baSAndreas Gohr * Does placeholder replacements in the style according to
1716e69c1baSAndreas Gohr * the ones defined in a templates style.ini file
1726e69c1baSAndreas Gohr *
1736e69c1baSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
1746e69c1baSAndreas Gohr */
175124af657SAndreas Gohrfunction css_applystyle($css,$tplinc){
176124af657SAndreas Gohr    if(@file_exists($tplinc.'style.ini')){
177124af657SAndreas Gohr        $ini = parse_ini_file($tplinc.'style.ini',true);
178519b3173SAndreas Gohr        $css = strtr($css,$ini['replacements']);
1796e69c1baSAndreas Gohr    }
1806e69c1baSAndreas Gohr    return $css;
1816e69c1baSAndreas Gohr}
1826e69c1baSAndreas Gohr
1836e69c1baSAndreas Gohr/**
1841c2d1019SAndreas Gohr * Prints classes for interwikilinks
1851c2d1019SAndreas Gohr *
1861c2d1019SAndreas Gohr * Interwiki links have two classes: 'interwiki' and 'iw_$name>' where
1871c2d1019SAndreas Gohr * $name is the identifier given in the config. All Interwiki links get
1881c2d1019SAndreas Gohr * an default style with a default icon. If a special icon is available
1891c2d1019SAndreas Gohr * for an interwiki URL it is set in it's own class. Both classes can be
1901c2d1019SAndreas Gohr * overwritten in the template or userstyles.
1911c2d1019SAndreas Gohr *
1921c2d1019SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
1931c2d1019SAndreas Gohr */
1941c2d1019SAndreas Gohrfunction css_interwiki(){
1951c2d1019SAndreas Gohr
1961c2d1019SAndreas Gohr    // default style
1971c2d1019SAndreas Gohr    echo 'a.interwiki {';
1981c2d1019SAndreas Gohr    echo ' background: transparent url('.DOKU_BASE.'lib/images/interwiki.png) 0px 1px no-repeat;';
1991c2d1019SAndreas Gohr    echo ' padding-left: 16px;';
2001c2d1019SAndreas Gohr    echo '}';
2011c2d1019SAndreas Gohr
2021c2d1019SAndreas Gohr    // additional styles when icon available
2031c2d1019SAndreas Gohr    $iwlinks = getInterwiki();
2041c2d1019SAndreas Gohr    foreach(array_keys($iwlinks) as $iw){
2059d2ddea4SAndreas Gohr        $class = preg_replace('/[^_\-a-z0-9]+/i','_',$iw);
2061c2d1019SAndreas Gohr        if(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.png')){
2079d2ddea4SAndreas Gohr            echo "a.iw_$class {";
2081c2d1019SAndreas Gohr            echo '  background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.png)';
2091c2d1019SAndreas Gohr            echo '}';
2101c2d1019SAndreas Gohr        }elseif(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.gif')){
2119d2ddea4SAndreas Gohr            echo "a.iw_$class {";
2121c2d1019SAndreas Gohr            echo '  background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.gif)';
2131c2d1019SAndreas Gohr            echo '}';
2141c2d1019SAndreas Gohr        }
2151c2d1019SAndreas Gohr    }
216d15166e5SAndreas Gohr}
2171c2d1019SAndreas Gohr
218d15166e5SAndreas Gohr/**
219d15166e5SAndreas Gohr * Prints classes for file download links
220d15166e5SAndreas Gohr *
221d15166e5SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
222d15166e5SAndreas Gohr */
223d15166e5SAndreas Gohrfunction css_filetypes(){
224d15166e5SAndreas Gohr
225d15166e5SAndreas Gohr    // default style
226d15166e5SAndreas Gohr    echo 'a.mediafile {';
227d15166e5SAndreas Gohr    echo ' background: transparent url('.DOKU_BASE.'lib/images/fileicons/file.png) 0px 1px no-repeat;';
2285b77caf4SAndreas Gohr    echo ' padding-left: 18px;';
2295b77caf4SAndreas Gohr    echo ' padding-bottom: 1px;';
230d15166e5SAndreas Gohr    echo '}';
231d15166e5SAndreas Gohr
232d15166e5SAndreas Gohr    // additional styles when icon available
23327bf7924STom N Harris    // scan directory for all icons
23427bf7924STom N Harris    $exts = array();
23527bf7924STom N Harris    if($dh = opendir(DOKU_INC.'lib/images/fileicons')){
23627bf7924STom N Harris        while(false !== ($file = readdir($dh))){
23727bf7924STom N Harris            if(preg_match('/([_\-a-z0-9]+(?:\.[_\-a-z0-9]+)*?)\.(png|gif)/i',$file,$match)){
23827bf7924STom N Harris                $ext = strtolower($match[1]);
23927bf7924STom N Harris                $type = '.'.strtolower($match[2]);
24027bf7924STom N Harris                if($ext!='file' && (!isset($exts[$ext]) || $type=='.png')){
24127bf7924STom N Harris                    $exts[$ext] = $type;
242d15166e5SAndreas Gohr                }
243d15166e5SAndreas Gohr            }
2441c2d1019SAndreas Gohr        }
24527bf7924STom N Harris        closedir($dh);
24627bf7924STom N Harris    }
24727bf7924STom N Harris    foreach($exts as $ext=>$type){
24827bf7924STom N Harris        $class = preg_replace('/[^_\-a-z0-9]+/','_',$ext);
24927bf7924STom N Harris        echo "a.mf_$class {";
25027bf7924STom N Harris        echo '  background-image: url('.DOKU_BASE.'lib/images/fileicons/'.$ext.$type.')';
25127bf7924STom N Harris        echo '}';
25227bf7924STom N Harris    }
25327bf7924STom N Harris}
2541c2d1019SAndreas Gohr
2551c2d1019SAndreas Gohr/**
25678a6aeb1SAndreas Gohr * Loads a given file and fixes relative URLs with the
25778a6aeb1SAndreas Gohr * given location prefix
25878a6aeb1SAndreas Gohr */
25978a6aeb1SAndreas Gohrfunction css_loadfile($file,$location=''){
26078a6aeb1SAndreas Gohr    if(!@file_exists($file)) return '';
26178a6aeb1SAndreas Gohr    $css = io_readFile($file);
26278a6aeb1SAndreas Gohr    if(!$location) return $css;
26378a6aeb1SAndreas Gohr
26415c394afSAndreas Gohr    $css = preg_replace('#(url\([ \'"]*)((?!/|http://|https://| |\'|"))#','\\1'.$location.'\\3',$css);
26578a6aeb1SAndreas Gohr    return $css;
26678a6aeb1SAndreas Gohr}
26778a6aeb1SAndreas Gohr
26815c394afSAndreas Gohr
26978a6aeb1SAndreas Gohr/**
27078a6aeb1SAndreas Gohr * Returns a list of possible Plugin Styles (no existance check here)
27178a6aeb1SAndreas Gohr *
27278a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
27378a6aeb1SAndreas Gohr */
27478a6aeb1SAndreas Gohrfunction css_pluginstyles($mode='screen'){
275208c0215SAndreas Gohr    global $lang;
27678a6aeb1SAndreas Gohr    $list = array();
27778a6aeb1SAndreas Gohr    $plugins = plugin_list();
27878a6aeb1SAndreas Gohr    foreach ($plugins as $p){
2797aaa4c46Smartin.tschofen        if($mode == 'all'){
280ea40e5efSmtbrains            $list[DOKU_PLUGIN."$p/all.css"]  = DOKU_BASE."lib/plugins/$p/";
2817aaa4c46Smartin.tschofen        }elseif($mode == 'print'){
2827aaa4c46Smartin.tschofen            $list[DOKU_PLUGIN."$p/print.css"]  = DOKU_BASE."lib/plugins/$p/";
283615960feSTom N Harris        }elseif($mode == 'feed'){
284615960feSTom N Harris            $list[DOKU_PLUGIN."$p/feed.css"]  = DOKU_BASE."lib/plugins/$p/";
28578a6aeb1SAndreas Gohr        }else{
28678a6aeb1SAndreas Gohr            $list[DOKU_PLUGIN."$p/style.css"]  = DOKU_BASE."lib/plugins/$p/";
28778a6aeb1SAndreas Gohr            $list[DOKU_PLUGIN."$p/screen.css"] = DOKU_BASE."lib/plugins/$p/";
28878a6aeb1SAndreas Gohr        }
289208c0215SAndreas Gohr        if($lang['direction'] == 'rtl'){
290208c0215SAndreas Gohr            $list[DOKU_PLUGIN."$p/rtl.css"] = DOKU_BASE."lib/plugins/$p/";
291208c0215SAndreas Gohr        }
29278a6aeb1SAndreas Gohr    }
29378a6aeb1SAndreas Gohr    return $list;
29478a6aeb1SAndreas Gohr}
29578a6aeb1SAndreas Gohr
29678a6aeb1SAndreas Gohr/**
29778a6aeb1SAndreas Gohr * Very simple CSS optimizer
29878a6aeb1SAndreas Gohr *
29978a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
30078a6aeb1SAndreas Gohr */
30178a6aeb1SAndreas Gohrfunction css_compress($css){
302fd7c2db0SAndreas Gohr    //strip comments through a callback
303fd7c2db0SAndreas Gohr    $css = preg_replace_callback('#(/\*)(.*?)(\*/)#s','css_comment_cb',$css);
304fd7c2db0SAndreas Gohr
305247c1c5dSAndreas Gohr    //strip (incorrect but common) one line comments
306fd7c2db0SAndreas Gohr    $css = preg_replace('/(?<!:)\/\/.*$/m','',$css);
307247c1c5dSAndreas Gohr
30878a6aeb1SAndreas Gohr    // strip whitespaces
30978a6aeb1SAndreas Gohr    $css = preg_replace('![\r\n\t ]+!',' ',$css);
3105646f690SAndreas Gohr    $css = preg_replace('/ ?([:;,{}\/]) ?/','\\1',$css);
31178a6aeb1SAndreas Gohr
31278a6aeb1SAndreas Gohr    // shorten colors
31378a6aeb1SAndreas 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);
31478a6aeb1SAndreas Gohr
31578a6aeb1SAndreas Gohr    return $css;
31678a6aeb1SAndreas Gohr}
31778a6aeb1SAndreas Gohr
318c00aef76SAndreas Gohr/**
319c00aef76SAndreas Gohr * Callback for css_compress()
320c00aef76SAndreas Gohr *
321c00aef76SAndreas Gohr * Keeps short comments (< 5 chars) to maintain typical browser hacks
322c00aef76SAndreas Gohr *
323c00aef76SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
324c00aef76SAndreas Gohr */
325c00aef76SAndreas Gohrfunction css_comment_cb($matches){
326c00aef76SAndreas Gohr    if(strlen($matches[2]) > 4) return '';
327c00aef76SAndreas Gohr    return $matches[0];
328c00aef76SAndreas Gohr}
32978a6aeb1SAndreas Gohr
33078a6aeb1SAndreas Gohr//Setup VIM: ex: et ts=4 enc=utf-8 :
331