xref: /dokuwiki/lib/exe/css.php (revision f7284726cf0d9bf0b88b4afa25ecd92d5b98caf6)
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 extra spellchecker style?
49        $files[DOKU_TPLINC.'layout.css'] = DOKU_TPL;
50        $files[DOKU_TPLINC.'design.css'] = DOKU_TPL;
51        $files[DOKU_TPLINC.'style.css']  = DOKU_TPL;
52        if($lang['direction'] == 'rtl'){
53            $files[DOKU_TPLINC.'rtl.css'] = DOKU_TPL;
54        }
55        // load plugin styles
56        $files = array_merge($files, css_pluginstyles('screen'));
57        $files[DOKU_CONF.'userstyle.css'] = '';
58    }
59
60    // check cache age
61    if(css_cacheok($cache,array_keys($files))){
62        readfile($cache);
63        return;
64    }
65
66    // start output buffering and build the stylesheet
67    ob_start();
68
69    // print the default classes for interwiki links and file downloads
70    css_interwiki();
71    css_filetypes();
72
73    // load files
74    foreach($files as $file => $location){
75        print css_loadfile($file, $location);
76    }
77
78    // end output buffering and get contents
79    $css = ob_get_contents();
80    ob_end_clean();
81
82    // apply style replacements
83    $css = css_applystyle($css);
84
85    // compress whitespace and comments
86    if($conf['compress']){
87        $css = css_compress($css);
88    }
89
90    // save cache file
91    io_saveFile($cache,$css);
92
93    // finally send output
94    print $css;
95}
96
97/**
98 * Checks if a CSS Cache file still is valid
99 *
100 * @author Andreas Gohr <andi@splitbrain.org>
101 */
102function css_cacheok($cache,$files){
103    $ctime = @filemtime($cache);
104    if(!$ctime) return false; //There is no cache
105
106    // some additional files to check
107    $files[] = DOKU_CONF.'dokuwiki.php';
108    $files[] = DOKU_CONF.'local.php';
109    $files[] = DOKU_TPLINC.'style.ini';
110    $files[] = __FILE__;
111
112    // now walk the files
113    foreach($files as $file){
114        if(@filemtime($file) > $ctime){
115            return false;
116        }
117    }
118    return true;
119}
120
121/**
122 * Does placeholder replacements in the style according to
123 * the ones defined in a templates style.ini file
124 *
125 * @author Andreas Gohr <andi@splitbrain.org>
126 */
127function css_applystyle($css){
128    if(@file_exists(DOKU_TPLINC.'style.ini')){
129        $ini = parse_ini_file(DOKU_TPLINC.'style.ini');
130        $css = strtr($css,$ini);
131    }
132    return $css;
133}
134
135/**
136 * Prints classes for interwikilinks
137 *
138 * Interwiki links have two classes: 'interwiki' and 'iw_$name>' where
139 * $name is the identifier given in the config. All Interwiki links get
140 * an default style with a default icon. If a special icon is available
141 * for an interwiki URL it is set in it's own class. Both classes can be
142 * overwritten in the template or userstyles.
143 *
144 * @author Andreas Gohr <andi@splitbrain.org>
145 */
146function css_interwiki(){
147
148    // default style
149    echo 'a.interwiki {';
150    echo ' background: transparent url('.DOKU_BASE.'lib/images/interwiki.png) 0px 1px no-repeat;';
151    echo ' padding-left: 16px;';
152    echo '}';
153
154    // additional styles when icon available
155    $iwlinks = getInterwiki();
156    foreach(array_keys($iwlinks) as $iw){
157        $class = preg_replace('/[^_\-a-z0-9]+/i','_',$iw);
158        if(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.png')){
159            echo "a.iw_$class {";
160            echo '  background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.png)';
161            echo '}';
162        }elseif(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.gif')){
163            echo "a.iw_$class {";
164            echo '  background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.gif)';
165            echo '}';
166        }
167    }
168}
169
170/**
171 * Prints classes for file download links
172 *
173 * @author Andreas Gohr <andi@splitbrain.org>
174 */
175function css_filetypes(){
176
177    // default style
178    echo 'a.mediafile {';
179    echo ' background: transparent url('.DOKU_BASE.'lib/images/fileicons/file.png) 0px 1px no-repeat;';
180    echo ' padding-left: 16px;';
181    echo '}';
182
183    // additional styles when icon available
184    $mimes = getMimeTypes();
185    foreach(array_keys($mimes) as $mime){
186        $class = preg_replace('/[^_\-a-z0-9]+/i','_',$mime);
187        if(@file_exists(DOKU_INC.'lib/images/fileicons/'.$mime.'.png')){
188            echo "a.mf_$class {";
189            echo '  background-image: url('.DOKU_BASE.'lib/images/fileicons/'.$mime.'.png)';
190            echo '}';
191        }elseif(@file_exists(DOKU_INC.'lib/images/fileicons/'.$mime.'.gif')){
192            echo "a.mf_$class {";
193            echo '  background-image: url('.DOKU_BASE.'lib/images/fileicons/'.$mime.'.gif)';
194            echo '}';
195        }
196    }
197}
198
199/**
200 * Loads a given file and fixes relative URLs with the
201 * given location prefix
202 */
203function css_loadfile($file,$location=''){
204    if(!@file_exists($file)) return '';
205    $css = io_readFile($file);
206    if(!$location) return $css;
207
208    $css = preg_replace('!(url\( *)([^/])!','\\1'.$location.'\\2',$css);
209    return $css;
210}
211
212/**
213 * Returns a list of possible Plugin Styles (no existance check here)
214 *
215 * @author Andreas Gohr <andi@splitbrain.org>
216 */
217function css_pluginstyles($mode='screen'){
218    $list = array();
219    $plugins = plugin_list();
220    foreach ($plugins as $p){
221        if($mode == 'print'){
222            $list[DOKU_PLUGIN."$p/print.css"]  = DOKU_BASE."lib/plugins/$p/";
223        }else{
224            $list[DOKU_PLUGIN."$p/style.css"]  = DOKU_BASE."lib/plugins/$p/";
225            $list[DOKU_PLUGIN."$p/screen.css"] = DOKU_BASE."lib/plugins/$p/";
226        }
227    }
228    return $list;
229}
230
231/**
232 * Very simple CSS optimizer
233 *
234 * @author Andreas Gohr <andi@splitbrain.org>
235 */
236function css_compress($css){
237    // strip whitespaces
238    $css = preg_replace('![\r\n\t ]+!',' ',$css);
239    $css = preg_replace('/ ?([:;,{}\/]) ?/','\\1',$css);
240
241    //strip comments through a callback
242    $css = preg_replace_callback('#(/\*)(.*?)(\*/)#s','css_comment_cb',$css);
243
244    // shorten colors
245    $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);
246
247    return $css;
248}
249
250/**
251 * Callback for css_compress()
252 *
253 * Keeps short comments (< 5 chars) to maintain typical browser hacks
254 *
255 * @author Andreas Gohr <andi@splitbrain.org>
256 */
257function css_comment_cb($matches){
258    if(strlen($matches[2]) > 4) return '';
259    return $matches[0];
260}
261
262//Setup VIM: ex: et ts=4 enc=utf-8 :
263?>
264