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