xref: /dokuwiki/lib/exe/css.php (revision ca2b464bb4f7cab9b83cd6e2508c6079e3f948cc)
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)
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;
33615960feSTom N Harris    switch ($_REQUEST['s']) {
347aaa4c46Smartin.tschofen        case 'all':
35615960feSTom N Harris        case 'print':
36615960feSTom N Harris        case 'feed':
37615960feSTom N Harris            $style = $_REQUEST['s'];
38615960feSTom N Harris        break;
39615960feSTom N Harris        default:
40615960feSTom N Harris            $style = '';
41615960feSTom N Harris        break;
42615960feSTom N Harris    }
4378a6aeb1SAndreas Gohr
44f7589b08SChris Smith    $tpl = trim(preg_replace('/[^\w-]+/','',$_REQUEST['t']));
45124af657SAndreas Gohr    if($tpl){
46124af657SAndreas Gohr        $tplinc = DOKU_INC.'lib/tpl/'.$tpl.'/';
47124af657SAndreas Gohr        $tpldir = DOKU_BASE.'lib/tpl/'.$tpl.'/';
48124af657SAndreas Gohr    }else{
49124af657SAndreas Gohr        $tplinc = DOKU_TPLINC;
50124af657SAndreas Gohr        $tpldir = DOKU_TPL;
51124af657SAndreas Gohr    }
52124af657SAndreas Gohr
5378a6aeb1SAndreas Gohr    // The generated script depends on some dynamic options
54e3e6ab3cSGina Haeussge    $cache = getCacheName('styles'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'].DOKU_BASE.$tplinc.$style,'.css');
5578a6aeb1SAndreas Gohr
56519b3173SAndreas Gohr    // load template styles
57519b3173SAndreas Gohr    $tplstyles = array();
58124af657SAndreas Gohr    if(@file_exists($tplinc.'style.ini')){
59124af657SAndreas Gohr        $ini = parse_ini_file($tplinc.'style.ini',true);
60519b3173SAndreas Gohr        foreach($ini['stylesheets'] as $file => $mode){
61124af657SAndreas Gohr            $tplstyles[$mode][$tplinc.$file] = $tpldir;
62519b3173SAndreas Gohr        }
63519b3173SAndreas Gohr    }
64519b3173SAndreas Gohr
6578a6aeb1SAndreas Gohr    // Array of needed files and their web locations, the latter ones
6678a6aeb1SAndreas Gohr    // are needed to fix relative paths in the stylesheets
6778a6aeb1SAndreas Gohr    $files   = array();
687aaa4c46Smartin.tschofen    //if (isset($tplstyles['all'])) $files = array_merge($files, $tplstyles['all']);
69615960feSTom N Harris    if(!empty($style)){
70615960feSTom N Harris        $files[DOKU_INC.'lib/styles/'.$style.'.css'] = DOKU_BASE.'lib/styles/';
715b77caf4SAndreas Gohr        // load plugin, template, user styles
72615960feSTom N Harris        $files = array_merge($files, css_pluginstyles($style));
73615960feSTom N Harris        if (isset($tplstyles[$style])) $files = array_merge($files, $tplstyles[$style]);
74c4af4cb6SAndreas Gohr        $files[DOKU_CONF.'user'.$style.'.css'] = DOKU_BASE;
7578a6aeb1SAndreas Gohr    }else{
7678a6aeb1SAndreas Gohr        $files[DOKU_INC.'lib/styles/style.css'] = DOKU_BASE.'lib/styles/';
777c96e92fSAndreas Gohr        if($conf['spellchecker']){
787c96e92fSAndreas Gohr            $files[DOKU_INC.'lib/styles/spellcheck.css'] = DOKU_BASE.'lib/styles/';
797c96e92fSAndreas Gohr        }
805b77caf4SAndreas Gohr        // load plugin, template, user styles
815b77caf4SAndreas Gohr        $files = array_merge($files, css_pluginstyles('screen'));
821f5663fdSchris        if (isset($tplstyles['screen'])) $files = array_merge($files, $tplstyles['screen']);
8378a6aeb1SAndreas Gohr        if($lang['direction'] == 'rtl'){
841f5663fdSchris            if (isset($tplstyles['rtl'])) $files = array_merge($files, $tplstyles['rtl']);
8578a6aeb1SAndreas Gohr        }
86c4af4cb6SAndreas Gohr        $files[DOKU_CONF.'userstyle.css'] = DOKU_BASE;
8778a6aeb1SAndreas Gohr    }
8878a6aeb1SAndreas Gohr
8938f56bffSBen Coburn    // check cache age & handle conditional request
9007525e80SBen Coburn    header('Cache-Control: public, max-age=3600');
9138f56bffSBen Coburn    header('Pragma: public');
92124af657SAndreas Gohr    if(css_cacheok($cache,array_keys($files),$tplinc)){
9338f56bffSBen Coburn        http_conditionalRequest(filemtime($cache));
94cf6894dfSAndreas Gohr        if($conf['allowdebug']) header("X-CacheUsed: $cache");
95*ca2b464bSChris Smith
96*ca2b464bSChris Smith        // finally send output
97*ca2b464bSChris Smith        if (http_accepts_gzip() && http_gzip_valid($cache)) {
98*ca2b464bSChris Smith          header('Vary: Accept-Encoding');
99*ca2b464bSChris Smith          header('Content-Encoding: gzip');
100*ca2b464bSChris Smith          if (!http_sendfile($cache.'.gz')) readfile($cache.".gz");
101*ca2b464bSChris Smith#        } else if (http_accepts_deflate()) {
102*ca2b464bSChris Smith#          header('Vary: Accept-Encoding');
103*ca2b464bSChris Smith#          header('Content-Encoding: deflate');
104*ca2b464bSChris Smith#          readfile($cache.".zip");
105*ca2b464bSChris Smith        } else {
106*ca2b464bSChris Smith          if (!http_sendfile($cache)) readfile($cache);
107*ca2b464bSChris Smith        }
108*ca2b464bSChris Smith
10978a6aeb1SAndreas Gohr        return;
11038f56bffSBen Coburn    } else {
11138f56bffSBen Coburn        http_conditionalRequest(time());
11278a6aeb1SAndreas Gohr    }
11378a6aeb1SAndreas Gohr
11478a6aeb1SAndreas Gohr    // start output buffering and build the stylesheet
11578a6aeb1SAndreas Gohr    ob_start();
11678a6aeb1SAndreas Gohr
117d15166e5SAndreas Gohr    // print the default classes for interwiki links and file downloads
1181c2d1019SAndreas Gohr    css_interwiki();
119d15166e5SAndreas Gohr    css_filetypes();
1201c2d1019SAndreas Gohr
12178a6aeb1SAndreas Gohr    // load files
12278a6aeb1SAndreas Gohr    foreach($files as $file => $location){
12378a6aeb1SAndreas Gohr        print css_loadfile($file, $location);
12478a6aeb1SAndreas Gohr    }
12578a6aeb1SAndreas Gohr
12678a6aeb1SAndreas Gohr    // end output buffering and get contents
12778a6aeb1SAndreas Gohr    $css = ob_get_contents();
12878a6aeb1SAndreas Gohr    ob_end_clean();
12978a6aeb1SAndreas Gohr
1306e69c1baSAndreas Gohr    // apply style replacements
131124af657SAndreas Gohr    $css = css_applystyle($css,$tplinc);
1326e69c1baSAndreas Gohr
13378a6aeb1SAndreas Gohr    // compress whitespace and comments
13478a6aeb1SAndreas Gohr    if($conf['compress']){
13578a6aeb1SAndreas Gohr        $css = css_compress($css);
13678a6aeb1SAndreas Gohr    }
13778a6aeb1SAndreas Gohr
13878a6aeb1SAndreas Gohr    // save cache file
13978a6aeb1SAndreas Gohr    io_saveFile($cache,$css);
140*ca2b464bSChris Smith    copy($cache,"compress.zlib://$cache.gz");
14178a6aeb1SAndreas Gohr
14278a6aeb1SAndreas Gohr    // finally send output
143*ca2b464bSChris Smith    if (http_accepts_gzip()) {
144*ca2b464bSChris Smith      header('Vary: Accept-Encoding');
145*ca2b464bSChris Smith      header('Content-Encoding: gzip');
146*ca2b464bSChris Smith      print gzencode($css,9,FORCE_GZIP);
147*ca2b464bSChris Smith#    } else if (http_accepts_deflate()) {
148*ca2b464bSChris Smith#      header('Vary: Accept-Encoding');
149*ca2b464bSChris Smith#      header('Content-Encoding: deflate');
150*ca2b464bSChris Smith#      print gzencode($css,9,FORCE_DEFLATE);
151*ca2b464bSChris Smith    } else {
15278a6aeb1SAndreas Gohr      print $css;
15378a6aeb1SAndreas Gohr    }
154*ca2b464bSChris Smith}
15578a6aeb1SAndreas Gohr
15678a6aeb1SAndreas Gohr/**
15778a6aeb1SAndreas Gohr * Checks if a CSS Cache file still is valid
15878a6aeb1SAndreas Gohr *
15978a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
16078a6aeb1SAndreas Gohr */
161124af657SAndreas Gohrfunction css_cacheok($cache,$files,$tplinc){
1620df6f150SAndreas Gohr    if($_REQUEST['purge']) return false; //support purge request
1630df6f150SAndreas Gohr
16478a6aeb1SAndreas Gohr    $ctime = @filemtime($cache);
16578a6aeb1SAndreas Gohr    if(!$ctime) return false; //There is no cache
16678a6aeb1SAndreas Gohr
16778a6aeb1SAndreas Gohr    // some additional files to check
168c591aabeSAndreas Gohr    $files[] = DOKU_CONF.'dokuwiki.php';
169c591aabeSAndreas Gohr    $files[] = DOKU_CONF.'local.php';
170124af657SAndreas Gohr    $files[] = $tplinc.'style.ini';
17178a6aeb1SAndreas Gohr    $files[] = __FILE__;
17278a6aeb1SAndreas Gohr
17378a6aeb1SAndreas Gohr    // now walk the files
17478a6aeb1SAndreas Gohr    foreach($files as $file){
17578a6aeb1SAndreas Gohr        if(@filemtime($file) > $ctime){
17678a6aeb1SAndreas Gohr            return false;
17778a6aeb1SAndreas Gohr        }
17878a6aeb1SAndreas Gohr    }
17978a6aeb1SAndreas Gohr    return true;
18078a6aeb1SAndreas Gohr}
18178a6aeb1SAndreas Gohr
18278a6aeb1SAndreas Gohr/**
1836e69c1baSAndreas Gohr * Does placeholder replacements in the style according to
1846e69c1baSAndreas Gohr * the ones defined in a templates style.ini file
1856e69c1baSAndreas Gohr *
1866e69c1baSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
1876e69c1baSAndreas Gohr */
188124af657SAndreas Gohrfunction css_applystyle($css,$tplinc){
189124af657SAndreas Gohr    if(@file_exists($tplinc.'style.ini')){
190124af657SAndreas Gohr        $ini = parse_ini_file($tplinc.'style.ini',true);
191519b3173SAndreas Gohr        $css = strtr($css,$ini['replacements']);
1926e69c1baSAndreas Gohr    }
1936e69c1baSAndreas Gohr    return $css;
1946e69c1baSAndreas Gohr}
1956e69c1baSAndreas Gohr
1966e69c1baSAndreas Gohr/**
1971c2d1019SAndreas Gohr * Prints classes for interwikilinks
1981c2d1019SAndreas Gohr *
1991c2d1019SAndreas Gohr * Interwiki links have two classes: 'interwiki' and 'iw_$name>' where
2001c2d1019SAndreas Gohr * $name is the identifier given in the config. All Interwiki links get
2011c2d1019SAndreas Gohr * an default style with a default icon. If a special icon is available
2021c2d1019SAndreas Gohr * for an interwiki URL it is set in it's own class. Both classes can be
2031c2d1019SAndreas Gohr * overwritten in the template or userstyles.
2041c2d1019SAndreas Gohr *
2051c2d1019SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
2061c2d1019SAndreas Gohr */
2071c2d1019SAndreas Gohrfunction css_interwiki(){
2081c2d1019SAndreas Gohr
2091c2d1019SAndreas Gohr    // default style
2101c2d1019SAndreas Gohr    echo 'a.interwiki {';
2111c2d1019SAndreas Gohr    echo ' background: transparent url('.DOKU_BASE.'lib/images/interwiki.png) 0px 1px no-repeat;';
2121c2d1019SAndreas Gohr    echo ' padding-left: 16px;';
2131c2d1019SAndreas Gohr    echo '}';
2141c2d1019SAndreas Gohr
2151c2d1019SAndreas Gohr    // additional styles when icon available
2161c2d1019SAndreas Gohr    $iwlinks = getInterwiki();
2171c2d1019SAndreas Gohr    foreach(array_keys($iwlinks) as $iw){
2189d2ddea4SAndreas Gohr        $class = preg_replace('/[^_\-a-z0-9]+/i','_',$iw);
2191c2d1019SAndreas Gohr        if(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.png')){
2209d2ddea4SAndreas Gohr            echo "a.iw_$class {";
2211c2d1019SAndreas Gohr            echo '  background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.png)';
2221c2d1019SAndreas Gohr            echo '}';
2231c2d1019SAndreas Gohr        }elseif(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.gif')){
2249d2ddea4SAndreas Gohr            echo "a.iw_$class {";
2251c2d1019SAndreas Gohr            echo '  background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.gif)';
2261c2d1019SAndreas Gohr            echo '}';
2271c2d1019SAndreas Gohr        }
2281c2d1019SAndreas Gohr    }
229d15166e5SAndreas Gohr}
2301c2d1019SAndreas Gohr
231d15166e5SAndreas Gohr/**
232d15166e5SAndreas Gohr * Prints classes for file download links
233d15166e5SAndreas Gohr *
234d15166e5SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
235d15166e5SAndreas Gohr */
236d15166e5SAndreas Gohrfunction css_filetypes(){
237d15166e5SAndreas Gohr
238d15166e5SAndreas Gohr    // default style
239d15166e5SAndreas Gohr    echo 'a.mediafile {';
240d15166e5SAndreas Gohr    echo ' background: transparent url('.DOKU_BASE.'lib/images/fileicons/file.png) 0px 1px no-repeat;';
2415b77caf4SAndreas Gohr    echo ' padding-left: 18px;';
2425b77caf4SAndreas Gohr    echo ' padding-bottom: 1px;';
243d15166e5SAndreas Gohr    echo '}';
244d15166e5SAndreas Gohr
245d15166e5SAndreas Gohr    // additional styles when icon available
246d15166e5SAndreas Gohr    $mimes = getMimeTypes();
247d15166e5SAndreas Gohr    foreach(array_keys($mimes) as $mime){
2489d2ddea4SAndreas Gohr        $class = preg_replace('/[^_\-a-z0-9]+/i','_',$mime);
249d15166e5SAndreas Gohr        if(@file_exists(DOKU_INC.'lib/images/fileicons/'.$mime.'.png')){
2509d2ddea4SAndreas Gohr            echo "a.mf_$class {";
251d15166e5SAndreas Gohr            echo '  background-image: url('.DOKU_BASE.'lib/images/fileicons/'.$mime.'.png)';
252d15166e5SAndreas Gohr            echo '}';
253d15166e5SAndreas Gohr        }elseif(@file_exists(DOKU_INC.'lib/images/fileicons/'.$mime.'.gif')){
2549d2ddea4SAndreas Gohr            echo "a.mf_$class {";
255d15166e5SAndreas Gohr            echo '  background-image: url('.DOKU_BASE.'lib/images/fileicons/'.$mime.'.gif)';
256d15166e5SAndreas Gohr            echo '}';
257d15166e5SAndreas Gohr        }
258d15166e5SAndreas Gohr    }
2591c2d1019SAndreas Gohr}
2601c2d1019SAndreas Gohr
2611c2d1019SAndreas Gohr/**
26278a6aeb1SAndreas Gohr * Loads a given file and fixes relative URLs with the
26378a6aeb1SAndreas Gohr * given location prefix
26478a6aeb1SAndreas Gohr */
26578a6aeb1SAndreas Gohrfunction css_loadfile($file,$location=''){
26678a6aeb1SAndreas Gohr    if(!@file_exists($file)) return '';
26778a6aeb1SAndreas Gohr    $css = io_readFile($file);
26878a6aeb1SAndreas Gohr    if(!$location) return $css;
26978a6aeb1SAndreas Gohr
27015c394afSAndreas Gohr    $css = preg_replace('#(url\([ \'"]*)((?!/|http://|https://| |\'|"))#','\\1'.$location.'\\3',$css);
27178a6aeb1SAndreas Gohr    return $css;
27278a6aeb1SAndreas Gohr}
27378a6aeb1SAndreas Gohr
27415c394afSAndreas Gohr
27578a6aeb1SAndreas Gohr/**
27678a6aeb1SAndreas Gohr * Returns a list of possible Plugin Styles (no existance check here)
27778a6aeb1SAndreas Gohr *
27878a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
27978a6aeb1SAndreas Gohr */
28078a6aeb1SAndreas Gohrfunction css_pluginstyles($mode='screen'){
281208c0215SAndreas Gohr    global $lang;
28278a6aeb1SAndreas Gohr    $list = array();
28378a6aeb1SAndreas Gohr    $plugins = plugin_list();
28478a6aeb1SAndreas Gohr    foreach ($plugins as $p){
2857aaa4c46Smartin.tschofen        if($mode == 'all'){
286ea40e5efSmtbrains            $list[DOKU_PLUGIN."$p/all.css"]  = DOKU_BASE."lib/plugins/$p/";
2877aaa4c46Smartin.tschofen        }elseif($mode == 'print'){
2887aaa4c46Smartin.tschofen            $list[DOKU_PLUGIN."$p/print.css"]  = DOKU_BASE."lib/plugins/$p/";
289615960feSTom N Harris        }elseif($mode == 'feed'){
290615960feSTom N Harris            $list[DOKU_PLUGIN."$p/feed.css"]  = DOKU_BASE."lib/plugins/$p/";
29178a6aeb1SAndreas Gohr        }else{
29278a6aeb1SAndreas Gohr            $list[DOKU_PLUGIN."$p/style.css"]  = DOKU_BASE."lib/plugins/$p/";
29378a6aeb1SAndreas Gohr            $list[DOKU_PLUGIN."$p/screen.css"] = DOKU_BASE."lib/plugins/$p/";
29478a6aeb1SAndreas Gohr        }
295208c0215SAndreas Gohr        if($lang['direction'] == 'rtl'){
296208c0215SAndreas Gohr            $list[DOKU_PLUGIN."$p/rtl.css"] = DOKU_BASE."lib/plugins/$p/";
297208c0215SAndreas Gohr        }
29878a6aeb1SAndreas Gohr    }
29978a6aeb1SAndreas Gohr    return $list;
30078a6aeb1SAndreas Gohr}
30178a6aeb1SAndreas Gohr
30278a6aeb1SAndreas Gohr/**
30378a6aeb1SAndreas Gohr * Very simple CSS optimizer
30478a6aeb1SAndreas Gohr *
30578a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
30678a6aeb1SAndreas Gohr */
30778a6aeb1SAndreas Gohrfunction css_compress($css){
308fd7c2db0SAndreas Gohr    //strip comments through a callback
309fd7c2db0SAndreas Gohr    $css = preg_replace_callback('#(/\*)(.*?)(\*/)#s','css_comment_cb',$css);
310fd7c2db0SAndreas Gohr
311247c1c5dSAndreas Gohr    //strip (incorrect but common) one line comments
312fd7c2db0SAndreas Gohr    $css = preg_replace('/(?<!:)\/\/.*$/m','',$css);
313247c1c5dSAndreas Gohr
31478a6aeb1SAndreas Gohr    // strip whitespaces
31578a6aeb1SAndreas Gohr    $css = preg_replace('![\r\n\t ]+!',' ',$css);
3165646f690SAndreas Gohr    $css = preg_replace('/ ?([:;,{}\/]) ?/','\\1',$css);
31778a6aeb1SAndreas Gohr
31878a6aeb1SAndreas Gohr    // shorten colors
31978a6aeb1SAndreas 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);
32078a6aeb1SAndreas Gohr
32178a6aeb1SAndreas Gohr    return $css;
32278a6aeb1SAndreas Gohr}
32378a6aeb1SAndreas Gohr
324c00aef76SAndreas Gohr/**
325c00aef76SAndreas Gohr * Callback for css_compress()
326c00aef76SAndreas Gohr *
327c00aef76SAndreas Gohr * Keeps short comments (< 5 chars) to maintain typical browser hacks
328c00aef76SAndreas Gohr *
329c00aef76SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
330c00aef76SAndreas Gohr */
331c00aef76SAndreas Gohrfunction css_comment_cb($matches){
332c00aef76SAndreas Gohr    if(strlen($matches[2]) > 4) return '';
333c00aef76SAndreas Gohr    return $matches[0];
334c00aef76SAndreas Gohr}
33578a6aeb1SAndreas Gohr
33678a6aeb1SAndreas Gohr//Setup VIM: ex: et ts=4 enc=utf-8 :
33778a6aeb1SAndreas Gohr?>
338