xref: /dokuwiki/lib/exe/css.php (revision 571b9b92283065f5bb5dd91a417b89b047efcc6b)
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',dirname(__FILE__).'/../../');
10if(!defined('NOSESSION')) define('NOSESSION',true); // we do not use a session or authentication here (better caching)
11if(!defined('DOKU_DISABLE_GZIP_OUTPUT')) define('DOKU_DISABLE_GZIP_OUTPUT',1); // we gzip ourself here
12require_once(DOKU_INC.'inc/init.php');
13
14// Main (don't run when UNIT test)
15if(!defined('SIMPLE_TEST')){
16    header('Content-Type: text/css; charset=utf-8');
17    css_out();
18}
19
20
21// ---------------------- functions ------------------------------
22
23/**
24 * Output all needed Styles
25 *
26 * @author Andreas Gohr <andi@splitbrain.org>
27 */
28function css_out(){
29    global $conf;
30    global $lang;
31    global $config_cascade;
32
33    $mediatype = 'screen';
34    if (isset($_REQUEST['s']) &&
35        in_array($_REQUEST['s'], array('all', 'print', 'feed'))) {
36        $mediatype = $_REQUEST['s'];
37    }
38
39    $tpl = trim(preg_replace('/[^\w-]+/','',$_REQUEST['t']));
40    if($tpl){
41        $tplinc = DOKU_INC.'lib/tpl/'.$tpl.'/';
42        $tpldir = DOKU_BASE.'lib/tpl/'.$tpl.'/';
43    }else{
44        $tplinc = DOKU_TPLINC;
45        $tpldir = DOKU_TPL;
46    }
47
48    // The generated script depends on some dynamic options
49    $cache = getCacheName('styles'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'].DOKU_BASE.$tplinc.$mediatype,'.css');
50
51    // load template styles
52    $tplstyles = array();
53    if(@file_exists($tplinc.'style.ini')){
54        $ini = parse_ini_file($tplinc.'style.ini',true);
55        foreach($ini['stylesheets'] as $file => $mode){
56            $tplstyles[$mode][$tplinc.$file] = $tpldir;
57        }
58    }
59
60    // Array of needed files and their web locations, the latter ones
61    // are needed to fix relative paths in the stylesheets
62    $files   = array();
63    // load core styles
64    $files[DOKU_INC.'lib/styles/'.$mediatype.'.css'] = DOKU_BASE.'lib/styles/';
65    // load jQuery-UI theme
66    $files[DOKU_INC.'lib/js/jquery/jquery-ui-theme/smoothness.css'] = DOKU_BASE.'lib/js/jquery/jquery-ui-theme/';
67    // load plugin styles
68    $files = array_merge($files, css_pluginstyles($mediatype));
69    // load template styles
70    if (isset($tplstyles[$mediatype])) {
71        $files = array_merge($files, $tplstyles[$mediatype]);
72    }
73    // if old 'default' userstyle setting exists, make it 'screen' userstyle for backwards compatibility
74    if (isset($config_cascade['userstyle']['default'])) {
75        $config_cascade['userstyle']['screen'] = $config_cascade['userstyle']['default'];
76    }
77    // load user styles
78    if(isset($config_cascade['userstyle'][$mediatype])){
79        $files[$config_cascade['userstyle'][$mediatype]] = DOKU_BASE;
80    }
81    // load rtl styles
82    // @todo: this currently adds the rtl styles only to the 'screen' media type
83    //        but 'print' and 'all' should also be supported
84    if ($mediatype=='screen') {
85        if($lang['direction'] == 'rtl'){
86            if (isset($tplstyles['rtl'])) $files = array_merge($files, $tplstyles['rtl']);
87        }
88    }
89
90    // check cache age & handle conditional request
91    header('Cache-Control: public, max-age=3600');
92    header('Pragma: public');
93    if(css_cacheok($cache,array_keys($files),$tplinc)){
94        http_conditionalRequest(filemtime($cache));
95        if($conf['allowdebug']) header("X-CacheUsed: $cache");
96
97        // finally send output
98        if ($conf['gzip_output'] && http_gzip_valid($cache)) {
99          header('Vary: Accept-Encoding');
100          header('Content-Encoding: gzip');
101          readfile($cache.".gz");
102        } else {
103          if (!http_sendfile($cache)) readfile($cache);
104        }
105
106        return;
107    } else {
108        http_conditionalRequest(time());
109    }
110
111    // start output buffering and build the stylesheet
112    ob_start();
113
114    // print the default classes for interwiki links and file downloads
115    css_interwiki();
116    css_filetypes();
117
118    // load files
119    foreach($files as $file => $location){
120        print css_loadfile($file, $location);
121    }
122
123    // end output buffering and get contents
124    $css = ob_get_contents();
125    ob_end_clean();
126
127    // apply style replacements
128    $css = css_applystyle($css,$tplinc);
129
130    // place all @import statements at the top of the file
131    $css = css_moveimports($css);
132
133    // compress whitespace and comments
134    if($conf['compress']){
135        $css = css_compress($css);
136    }
137
138    // save cache file
139    io_saveFile($cache,$css);
140    if(function_exists('gzopen')) io_saveFile("$cache.gz",$css);
141
142    // finally send output
143    if ($conf['gzip_output']) {
144      header('Vary: Accept-Encoding');
145      header('Content-Encoding: gzip');
146      print gzencode($css,9,FORCE_GZIP);
147    } else {
148      print $css;
149    }
150}
151
152/**
153 * Checks if a CSS Cache file still is valid
154 *
155 * @author Andreas Gohr <andi@splitbrain.org>
156 */
157function css_cacheok($cache,$files,$tplinc){
158    global $config_cascade;
159
160    if(isset($_REQUEST['purge'])) return false; //support purge request
161
162    $ctime = @filemtime($cache);
163    if(!$ctime) return false; //There is no cache
164
165    // some additional files to check
166    $files = array_merge($files, getConfigFiles('main'));
167    $files[] = $tplinc.'style.ini';
168    $files[] = __FILE__;
169
170    // now walk the files
171    foreach($files as $file){
172        if(@filemtime($file) > $ctime){
173            return false;
174        }
175    }
176    return true;
177}
178
179/**
180 * Does placeholder replacements in the style according to
181 * the ones defined in a templates style.ini file
182 *
183 * @author Andreas Gohr <andi@splitbrain.org>
184 */
185function css_applystyle($css,$tplinc){
186    if(@file_exists($tplinc.'style.ini')){
187        $ini = parse_ini_file($tplinc.'style.ini',true);
188        $css = strtr($css,$ini['replacements']);
189    }
190    return $css;
191}
192
193/**
194 * Prints classes for interwikilinks
195 *
196 * Interwiki links have two classes: 'interwiki' and 'iw_$name>' where
197 * $name is the identifier given in the config. All Interwiki links get
198 * an default style with a default icon. If a special icon is available
199 * for an interwiki URL it is set in it's own class. Both classes can be
200 * overwritten in the template or userstyles.
201 *
202 * @author Andreas Gohr <andi@splitbrain.org>
203 */
204function css_interwiki(){
205
206    // default style
207    echo 'a.interwiki {';
208    echo ' background: transparent url('.DOKU_BASE.'lib/images/interwiki.png) 0px 1px no-repeat;';
209    echo ' padding: 1px 0px 1px 16px;';
210    echo '}';
211
212    // additional styles when icon available
213    $iwlinks = getInterwiki();
214    foreach(array_keys($iwlinks) as $iw){
215        $class = preg_replace('/[^_\-a-z0-9]+/i','_',$iw);
216        if(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.png')){
217            echo "a.iw_$class {";
218            echo '  background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.png)';
219            echo '}';
220        }elseif(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.gif')){
221            echo "a.iw_$class {";
222            echo '  background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.gif)';
223            echo '}';
224        }
225    }
226}
227
228/**
229 * Prints classes for file download links
230 *
231 * @author Andreas Gohr <andi@splitbrain.org>
232 */
233function css_filetypes(){
234
235    // default style
236    echo 'a.mediafile {';
237    echo ' background: transparent url('.DOKU_BASE.'lib/images/fileicons/file.png) 0px 1px no-repeat;';
238    echo ' padding-left: 18px;';
239    echo ' padding-bottom: 1px;';
240    echo '}';
241
242    // additional styles when icon available
243    // scan directory for all icons
244    $exts = array();
245    if($dh = opendir(DOKU_INC.'lib/images/fileicons')){
246        while(false !== ($file = readdir($dh))){
247            if(preg_match('/([_\-a-z0-9]+(?:\.[_\-a-z0-9]+)*?)\.(png|gif)/i',$file,$match)){
248                $ext = strtolower($match[1]);
249                $type = '.'.strtolower($match[2]);
250                if($ext!='file' && (!isset($exts[$ext]) || $type=='.png')){
251                    $exts[$ext] = $type;
252                }
253            }
254        }
255        closedir($dh);
256    }
257    foreach($exts as $ext=>$type){
258        $class = preg_replace('/[^_\-a-z0-9]+/','_',$ext);
259        echo "a.mf_$class {";
260        echo '  background-image: url('.DOKU_BASE.'lib/images/fileicons/'.$ext.$type.')';
261        echo '}';
262    }
263}
264
265/**
266 * Loads a given file and fixes relative URLs with the
267 * given location prefix
268 */
269function css_loadfile($file,$location=''){
270    if(!@file_exists($file)) return '';
271    $css = io_readFile($file);
272    if(!$location) return $css;
273
274    $css = preg_replace('#(url\([ \'"]*)(?!/|http://|https://| |\'|")#','\\1'.$location,$css);
275    $css = preg_replace('#(@import\s+[\'"])(?!/|http://|https://)#', '\\1'.$location, $css);
276    return $css;
277}
278
279
280/**
281 * Returns a list of possible Plugin Styles (no existance check here)
282 *
283 * @author Andreas Gohr <andi@splitbrain.org>
284 */
285function css_pluginstyles($mediatype='screen'){
286    global $lang;
287    $list = array();
288    $plugins = plugin_list();
289    foreach ($plugins as $p){
290        $list[DOKU_PLUGIN."$p/$mediatype.css"]  = DOKU_BASE."lib/plugins/$p/";
291        // alternative for screen.css
292        if ($mediatype=='screen') {
293            $list[DOKU_PLUGIN."$p/style.css"]  = DOKU_BASE."lib/plugins/$p/";
294        }
295        if($lang['direction'] == 'rtl'){
296            $list[DOKU_PLUGIN."$p/rtl.css"] = DOKU_BASE."lib/plugins/$p/";
297        }
298    }
299    return $list;
300}
301
302/**
303 * Move all @import statements in a combined stylesheet to the top so they
304 * aren't ignored by the browser.
305 *
306 * @author Gabriel Birke <birke@d-scribe.de>
307 */
308function css_moveimports($css)
309{
310    if(!preg_match_all('/@import\s+(?:url\([^)]+\)|"[^"]+")\s*[^;]*;\s*/', $css, $matches, PREG_OFFSET_CAPTURE)) {
311        return $css;
312    }
313    $newCss  = "";
314    $imports = "";
315    $offset  = 0;
316    foreach($matches[0] as $match) {
317        $newCss  .= substr($css, $offset, $match[1] - $offset);
318        $imports .= $match[0];
319        $offset   = $match[1] + strlen($match[0]);
320    }
321    $newCss .= substr($css, $offset);
322    return $imports.$newCss;
323}
324
325/**
326 * Very simple CSS optimizer
327 *
328 * @author Andreas Gohr <andi@splitbrain.org>
329 */
330function css_compress($css){
331    //strip comments through a callback
332    $css = preg_replace_callback('#(/\*)(.*?)(\*/)#s','css_comment_cb',$css);
333
334    //strip (incorrect but common) one line comments
335    $css = preg_replace('/(?<!:)\/\/.*$/m','',$css);
336
337    // strip whitespaces
338    $css = preg_replace('![\r\n\t ]+!',' ',$css);
339    $css = preg_replace('/ ?([:;,{}\/]) ?/','\\1',$css);
340
341    // shorten colors
342    $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);
343
344    return $css;
345}
346
347/**
348 * Callback for css_compress()
349 *
350 * Keeps short comments (< 5 chars) to maintain typical browser hacks
351 *
352 * @author Andreas Gohr <andi@splitbrain.org>
353 */
354function css_comment_cb($matches){
355    if(strlen($matches[2]) > 4) return '';
356    return $matches[0];
357}
358
359//Setup VIM: ex: et ts=4 :
360