xref: /dokuwiki/lib/exe/css.php (revision 6e69c1ba2d33f6a576534fc6d05a66cb1554c7d0)
1<?php
2/**
3 * DokuWiki StyleSheet creator
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <andi@splitbrain.org>
7 */
8
9if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
10if(!defined('NOSESSION')) define('NOSESSION',true); // we do not use a session or authentication here (better caching)
11require_once(DOKU_INC.'inc/init.php');
12require_once(DOKU_INC.'inc/pageutils.php');
13require_once(DOKU_INC.'inc/io.php');
14require_once(DOKU_INC.'inc/confutils.php');
15
16// Main (don't run when UNIT test)
17if(!defined('SIMPLE_TEST')){
18    header('Content-Type: text/css; charset=utf-8');
19    css_out();
20}
21
22
23// ---------------------- functions ------------------------------
24
25/**
26 * Output all needed Styles
27 *
28 * @author Andreas Gohr <andi@splitbrain.org>
29 */
30function css_out(){
31    global $conf;
32    global $lang;
33    $print = (bool) $_REQUEST['print'];   //print mode?
34
35    // The generated script depends on some dynamic options
36    $cache = getCacheName('styles'.$print,'.css');
37
38    // Array of needed files and their web locations, the latter ones
39    // are needed to fix relative paths in the stylesheets
40    $files   = array();
41    if($print){
42        $files[DOKU_TPLINC.'print.css'] = DOKU_TPL;
43        // load plugin styles
44        $files = array_merge($files, css_pluginstyles('print'));
45        $files[DOKU_CONF.'userprint.css'] = '';
46    }else{
47        $files[DOKU_INC.'lib/styles/style.css'] = DOKU_BASE.'lib/styles/';
48        //fixme spellchecker style
49        $files[DOKU_TPLINC.'layout.css'] = DOKU_TPL;
50        $files[DOKU_TPLINC.'design.css'] = DOKU_TPL;
51        if($lang['direction'] == 'rtl'){
52            $files[DOKU_TPLINC.'rtl.css'] = DOKU_TPL;
53        }
54        // load plugin styles
55        $files = array_merge($files, css_pluginstyles('screen'));
56        $files[DOKU_CONF.'userstyle.css'] = '';
57    }
58
59    // check cache age
60    if(css_cacheok($cache,array_keys($files))){
61        readfile($cache);
62        return;
63    }
64
65    // start output buffering and build the stylesheet
66    ob_start();
67
68    // print the default classes for Interwikilinks
69    css_interwiki();
70
71    // load files
72    foreach($files as $file => $location){
73        print css_loadfile($file, $location);
74    }
75
76    // end output buffering and get contents
77    $css = ob_get_contents();
78    ob_end_clean();
79
80    // apply style replacements
81    $css = css_applystyle($css);
82
83    // compress whitespace and comments
84    if($conf['compress']){
85        $css = css_compress($css);
86    }
87
88    // save cache file
89    io_saveFile($cache,$css);
90
91    // finally send output
92    print $css;
93}
94
95/**
96 * Checks if a CSS Cache file still is valid
97 *
98 * @author Andreas Gohr <andi@splitbrain.org>
99 */
100function css_cacheok($cache,$files){
101    $ctime = @filemtime($cache);
102    if(!$ctime) return false; //There is no cache
103
104    // some additional files to check
105    $files[] = DOKU_CONF.'dokuwiki.conf';
106    $files[] = DOKU_CONF.'local.conf';
107    $files[] = DOKU_TPLINC.'style.ini';
108    $files[] = __FILE__;
109
110    // now walk the files
111    foreach($files as $file){
112        if(@filemtime($file) > $ctime){
113            return false;
114        }
115    }
116    return true;
117}
118
119/**
120 * Does placeholder replacements in the style according to
121 * the ones defined in a templates style.ini file
122 *
123 * @author Andreas Gohr <andi@splitbrain.org>
124 */
125function css_applystyle($css){
126    if(@file_exists(DOKU_TPLINC.'style.ini')){
127        $ini = parse_ini_file(DOKU_TPLINC.'style.ini');
128        $css = strtr($css,$ini);
129    }
130    return $css;
131}
132
133/**
134 * Prints classes for interwikilinks
135 *
136 * Interwiki links have two classes: 'interwiki' and 'iw_$name>' where
137 * $name is the identifier given in the config. All Interwiki links get
138 * an default style with a default icon. If a special icon is available
139 * for an interwiki URL it is set in it's own class. Both classes can be
140 * overwritten in the template or userstyles.
141 *
142 * @author Andreas Gohr <andi@splitbrain.org>
143 */
144function css_interwiki(){
145
146    // default style
147    echo 'a.interwiki {';
148    echo ' background: transparent url('.DOKU_BASE.'lib/images/interwiki.png) 0px 1px no-repeat;';
149    echo ' padding-left: 16px;';
150    echo '}';
151
152    // additional styles when icon available
153    $iwlinks = getInterwiki();
154    foreach(array_keys($iwlinks) as $iw){
155        if(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.png')){
156            echo "a.iw_$iw {";
157            echo '  background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.png)';
158            echo '}';
159        }elseif(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.gif')){
160            echo "a.iw_$iw {";
161            echo '  background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.gif)';
162            echo '}';
163        }
164    }
165
166}
167
168/**
169 * Loads a given file and fixes relative URLs with the
170 * given location prefix
171 */
172function css_loadfile($file,$location=''){
173    if(!@file_exists($file)) return '';
174    $css = io_readFile($file);
175    if(!$location) return $css;
176
177    $css = preg_replace('!(url\( *)([^/])!','\\1'.$location.'\\2',$css);
178    return $css;
179}
180
181/**
182 * Returns a list of possible Plugin Styles (no existance check here)
183 *
184 * @author Andreas Gohr <andi@splitbrain.org>
185 */
186function css_pluginstyles($mode='screen'){
187    $list = array();
188    $plugins = plugin_list();
189    foreach ($plugins as $p){
190        if($mode == 'print'){
191            $list[DOKU_PLUGIN."$p/print.css"]  = DOKU_BASE."lib/plugins/$p/";
192        }else{
193            $list[DOKU_PLUGIN."$p/style.css"]  = DOKU_BASE."lib/plugins/$p/";
194            $list[DOKU_PLUGIN."$p/screen.css"] = DOKU_BASE."lib/plugins/$p/";
195        }
196    }
197    return $list;
198}
199
200/**
201 * Very simple CSS optimizer
202 *
203 * @author Andreas Gohr <andi@splitbrain.org>
204 */
205function css_compress($css){
206    // strip whitespaces
207    $css = preg_replace('![\r\n\t ]+!',' ',$css);
208    $css = preg_replace('/ ?([:;,{}]) ?/','\\1',$css);
209
210    // strip comments (ungreedy)
211    // We keep very small comments to maintain typical browser hacks
212    $css = preg_replace('!(/\*)(.{4,})(\*/)!U','',$css);
213
214    // shorten colors
215    $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);
216
217    return $css;
218}
219
220
221//Setup VIM: ex: et ts=4 enc=utf-8 :
222?>
223