xref: /dokuwiki/lib/exe/css.php (revision 5646f69041af08c6131237b27bd8adedd4a717e7)
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 Interwikilinks
70    css_interwiki();
71
72    // load files
73    foreach($files as $file => $location){
74        print css_loadfile($file, $location);
75    }
76
77    // end output buffering and get contents
78    $css = ob_get_contents();
79    ob_end_clean();
80
81    // apply style replacements
82    $css = css_applystyle($css);
83
84    // compress whitespace and comments
85    if($conf['compress']){
86        $css = css_compress($css);
87    }
88
89    // save cache file
90    io_saveFile($cache,$css);
91
92    // finally send output
93    print $css;
94}
95
96/**
97 * Checks if a CSS Cache file still is valid
98 *
99 * @author Andreas Gohr <andi@splitbrain.org>
100 */
101function css_cacheok($cache,$files){
102    $ctime = @filemtime($cache);
103    if(!$ctime) return false; //There is no cache
104
105    // some additional files to check
106    $files[] = DOKU_CONF.'dokuwiki.conf';
107    $files[] = DOKU_CONF.'local.conf';
108    $files[] = DOKU_TPLINC.'style.ini';
109    $files[] = __FILE__;
110
111    // now walk the files
112    foreach($files as $file){
113        if(@filemtime($file) > $ctime){
114            return false;
115        }
116    }
117    return true;
118}
119
120/**
121 * Does placeholder replacements in the style according to
122 * the ones defined in a templates style.ini file
123 *
124 * @author Andreas Gohr <andi@splitbrain.org>
125 */
126function css_applystyle($css){
127    if(@file_exists(DOKU_TPLINC.'style.ini')){
128        $ini = parse_ini_file(DOKU_TPLINC.'style.ini');
129        $css = strtr($css,$ini);
130    }
131    return $css;
132}
133
134/**
135 * Prints classes for interwikilinks
136 *
137 * Interwiki links have two classes: 'interwiki' and 'iw_$name>' where
138 * $name is the identifier given in the config. All Interwiki links get
139 * an default style with a default icon. If a special icon is available
140 * for an interwiki URL it is set in it's own class. Both classes can be
141 * overwritten in the template or userstyles.
142 *
143 * @author Andreas Gohr <andi@splitbrain.org>
144 */
145function css_interwiki(){
146
147    // default style
148    echo 'a.interwiki {';
149    echo ' background: transparent url('.DOKU_BASE.'lib/images/interwiki.png) 0px 1px no-repeat;';
150    echo ' padding-left: 16px;';
151    echo '}';
152
153    // additional styles when icon available
154    $iwlinks = getInterwiki();
155    foreach(array_keys($iwlinks) as $iw){
156        if(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.png')){
157            echo "a.iw_$iw {";
158            echo '  background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.png)';
159            echo '}';
160        }elseif(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.gif')){
161            echo "a.iw_$iw {";
162            echo '  background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.gif)';
163            echo '}';
164        }
165    }
166
167}
168
169/**
170 * Loads a given file and fixes relative URLs with the
171 * given location prefix
172 */
173function css_loadfile($file,$location=''){
174    if(!@file_exists($file)) return '';
175    $css = io_readFile($file);
176    if(!$location) return $css;
177
178    $css = preg_replace('!(url\( *)([^/])!','\\1'.$location.'\\2',$css);
179    return $css;
180}
181
182/**
183 * Returns a list of possible Plugin Styles (no existance check here)
184 *
185 * @author Andreas Gohr <andi@splitbrain.org>
186 */
187function css_pluginstyles($mode='screen'){
188    $list = array();
189    $plugins = plugin_list();
190    foreach ($plugins as $p){
191        if($mode == 'print'){
192            $list[DOKU_PLUGIN."$p/print.css"]  = DOKU_BASE."lib/plugins/$p/";
193        }else{
194            $list[DOKU_PLUGIN."$p/style.css"]  = DOKU_BASE."lib/plugins/$p/";
195            $list[DOKU_PLUGIN."$p/screen.css"] = DOKU_BASE."lib/plugins/$p/";
196        }
197    }
198    return $list;
199}
200
201/**
202 * Very simple CSS optimizer
203 *
204 * @author Andreas Gohr <andi@splitbrain.org>
205 */
206function css_compress($css){
207    // strip whitespaces
208    $css = preg_replace('![\r\n\t ]+!',' ',$css);
209    $css = preg_replace('/ ?([:;,{}\/]) ?/','\\1',$css);
210
211    // strip comments (ungreedy)
212    // We keep very small comments to maintain typical browser hacks
213    $css = preg_replace('#(/\*)((?!\*/).){4,}(\*/)#Us','',$css);
214
215    // shorten colors
216    $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);
217
218    return $css;
219}
220
221
222//Setup VIM: ex: et ts=4 enc=utf-8 :
223?>
224